llvm-capstone/clang/lib/CodeGen
Bill Wendling 9a954c6935 [Clang] Implement the 'counted_by' attribute
The 'counted_by' attribute is used on flexible array members. The
argument for the attribute is the name of the field member in the same
structure holding the count of elements in the flexible array. This
information can be used to improve the results of the array bound sanitizer
and the '__builtin_dynamic_object_size' builtin.

This example specifies the that the flexible array member 'array' has the
number of elements allocated for it in 'count':

  struct bar;
  struct foo {
    size_t count;
     /* ... */
    struct bar *array[] __attribute__((counted_by(count)));
  };

This establishes a relationship between 'array' and 'count', specifically
that 'p->array' must have *at least* 'p->count' number of elements available.
It's the user's responsibility to ensure that this relationship is maintained
through changes to the structure.

In the following, the allocated array erroneously has fewer elements than
what's specified by 'p->count'. This would result in an out-of-bounds access not
not being detected:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

The next example updates 'p->count', breaking the relationship requirement that
'p->array' must have at least 'p->count' number of elements available:

  struct foo *p;

  void foo_alloc(size_t count) {
    p = malloc(MAX(sizeof(struct foo),
                   offsetof(struct foo, array[0]) + count *
                       sizeof(struct bar *)));
    p->count = count + 42;
  }

  void use_foo(int index) {
    p->count += 42;
    p->array[index] = 0; /* The sanitizer cannot properly check this access */
  }

Reviewed By: nickdesaulniers, aaron.ballman

Differential Revision: https://reviews.llvm.org/D148381
2023-10-04 18:26:15 -07:00
..
Targets Reland [NVPTX] Add support for maxclusterrank in launch_bounds (#66496) (#67667) 2023-09-29 08:39:31 +02:00
ABIInfo.cpp [clang][CodeGen] Break up TargetInfo.cpp [8/8] 2023-06-17 07:14:50 +03:00
ABIInfo.h [clang][CodeGen] Break up TargetInfo.cpp [8/8] 2023-06-17 07:14:50 +03:00
ABIInfoImpl.cpp [clang][RISCV] Fix bug in ABI handling of empty structs with hard FP calling conventions in C++ 2023-08-07 10:45:22 +01:00
ABIInfoImpl.h [clang][RISCV] Fix bug in ABI handling of empty structs with hard FP calling conventions in C++ 2023-08-07 10:45:22 +01:00
Address.h [clang] Replace uses of CGBuilderTy::CreateElementBitCast (NFC) 2023-06-18 04:13:15 +03:00
BackendUtil.cpp [clang][codegen] Add a verifier IR pass before any further passes. (#68015) 2023-10-03 18:05:54 +02:00
CGAtomic.cpp [clang][CodeGen] Simplify code based on opaque pointers (#65624) 2023-09-25 11:21:24 +02:00
CGBlocks.cpp -fsanitize=function: fix MSVC hashing to sugared type (#66816) 2023-10-02 19:09:39 +02:00
CGBlocks.h [CodeGen] Remove unused member variable NextBlockInfo 2023-05-25 23:41:34 -07:00
CGBuilder.h [clang] Remove CGBuilderTy::CreateElementBitCast 2023-07-02 10:40:16 -04:00
CGBuiltin.cpp [Clang] Implement the 'counted_by' attribute 2023-10-04 18:26:15 -07:00
CGCall.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
CGCall.h Revert "Reapply: [IRGen] Emit lifetime intrinsics around temporary aggregate argument allocas" 2023-09-01 12:53:24 +02:00
CGClass.cpp [Clang][CodeGen] Fix use of CXXThisValue with StrictVTablePointers (#68169) 2023-10-04 23:41:49 +02:00
CGCleanup.cpp [SEH] Fix wrong argument passes to the call of OutlinedFinally. 2023-08-21 17:07:38 -07:00
CGCleanup.h
CGCoroutine.cpp [Clang][LLVM][Coroutines] Prevent __coro_gro from outliving __promise (#66706) 2023-09-21 22:52:05 -07:00
CGCUDANV.cpp [clang][CodeGen] Simplify code based on opaque pointers (#65624) 2023-09-25 11:21:24 +02:00
CGCUDARuntime.cpp
CGCUDARuntime.h Fix duplicate word typos; NFC 2022-11-08 07:21:23 -05:00
CGCXX.cpp [clang] Replace use of Type::getPointerTo() (NFC) 2023-06-16 22:07:32 +03:00
CGCXXABI.cpp [CodeGen] Remove Constant arguments from linkage functions, NFCI. 2023-08-17 08:28:51 +02:00
CGCXXABI.h Optimize emission of dynamic_cast to final classes. 2023-07-21 19:07:59 -07:00
CGDebugInfo.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
CGDebugInfo.h [NFC] Add checks for self-assignment. 2023-08-24 09:20:58 -07:00
CGDecl.cpp [DebugInfo] Fix incorrect dbg.declare when nrvo flag is used 2023-08-29 11:39:59 +02:00
CGDeclCXX.cpp [C++20] [Modules] Fix crash when emitting module inits for duplicated modules 2023-10-02 18:31:54 +08:00
CGException.cpp [clang][CodeGen] The eh_typeid_for intrinsic needs special care too (#65699) 2023-09-20 17:12:19 +01:00
CGExpr.cpp [Clang] Implement the 'counted_by' attribute 2023-10-04 18:26:15 -07:00
CGExprAgg.cpp [clang] Replace uses of CreateElementBitCast (NFC) 2023-06-30 17:35:36 -04:00
CGExprComplex.cpp Propagate the volatile qualifier of exp to store /load operations . 2023-09-23 19:40:24 +05:30
CGExprConstant.cpp [CodeGen] Avoid use of ConstantExpr::getZExt() (NFC) 2023-09-28 16:45:31 +02:00
CGExprCXX.cpp [C++] Implement "Deducing this" (P0847R7) 2023-10-02 14:33:02 +02:00
CGExprScalar.cpp [CodeGen] Respect pointer-overflow sanitizer for void pointers (#67772) 2023-10-04 15:16:00 +02:00
CGGPUBuiltin.cpp [AMDGPU] Non hostcall printf support for HIP 2023-06-10 09:55:00 -04:00
CGHLSLRuntime.cpp Recommit: [NFC][IR] Make Module::getGlobalList() private 2023-02-14 15:12:51 -08:00
CGHLSLRuntime.h [clang] Remove remaining uses of llvm::Optional (NFC) 2023-01-14 13:37:25 -08:00
CGLoopInfo.cpp [clang] Use std::optional instead of llvm::Optional (NFC) 2023-01-14 12:31:01 -08:00
CGLoopInfo.h
CGNonTrivialStruct.cpp [clang] Replace uses of CGBuilderTy::CreateElementBitCast (NFC) 2023-06-27 10:38:54 -04:00
CGObjC.cpp [NFC][Clang] Fix static analyzer concern 2023-08-14 07:14:32 -07:00
CGObjCGNU.cpp Stop using legacy helpers indicating typed pointer types. NFC 2023-08-02 12:08:37 +02:00
CGObjCMac.cpp [clang] Remove uses of llvm::Type::getPointerTo() (NFC) 2023-09-30 08:05:46 -04:00
CGObjCRuntime.cpp [clang][CodeGen] Simplify code based on opaque pointers (#65624) 2023-09-25 11:21:24 +02:00
CGObjCRuntime.h [clang][CodeGen] Only include ABIInfo.h where required (NFC) 2022-07-22 10:45:02 -07:00
CGOpenCLRuntime.cpp [clang][CodeGen] Simplify code based on opaque pointers (#65624) 2023-09-25 11:21:24 +02:00
CGOpenCLRuntime.h [Clang][SPIR-V] Emit target extension types for OpenCL types on SPIR-V. 2023-03-13 14:20:24 -04:00
CGOpenMPRuntime.cpp [Clang][OpenMP][OMPIRBuilder] Move Clang's OpenMP Member/MemberOf flag helpers into the OMPIRBuilder (#67844) 2023-10-03 15:20:44 +02:00
CGOpenMPRuntime.h [OpenMP] Codegen support for thread_limit on target directive for host 2023-08-26 22:18:49 -05:00
CGOpenMPRuntimeGPU.cpp Revert "[OpenMP] Introduce the initial support for OpenMP kernel language (#66844)" 2023-09-29 15:35:10 -05:00
CGOpenMPRuntimeGPU.h Revert "[OpenMP] Introduce the initial support for OpenMP kernel language (#66844)" 2023-09-29 15:35:10 -05:00
CGRecordLayout.h
CGRecordLayoutBuilder.cpp [NFC] Replace uses of Type::getPointerTo 2023-09-29 21:38:53 -04:00
CGStmt.cpp [OpenMP 5.1] Parsing and Sema support for scope directive 2023-08-24 18:13:52 -07:00
CGStmtOpenMP.cpp [Clang][OpenMP] Emit unroll directive w/o captured stmt (#65862) 2023-09-09 18:51:58 -04:00
CGValue.h [Clang] Remove typed pointer consistency assertions (NFC) 2023-06-09 09:45:43 +02:00
CGVTables.cpp [clang][RelativeVTables] Make the rtti_proxy LinkOnceODR instead of External linkage (#67755) 2023-10-03 17:05:21 -07:00
CGVTables.h [CodeGen] Remove unused declaration getOrCreateRelativeStub 2023-05-28 12:11:28 -07:00
CGVTT.cpp [Clang][CodeGen]vtable, typeinfo et al. are globals 2023-07-19 18:04:31 +01:00
CMakeLists.txt clang: add a missing dependency on ClangDriverOptions 2023-07-11 10:07:09 -07:00
CodeGenABITypes.cpp Reland "Try to implement lambdas with inalloca parameters by forwarding without use of inallocas."t 2023-07-26 16:13:36 -07:00
CodeGenAction.cpp [CodeGen] Support bitcode input containing multiple modules 2023-07-21 20:05:35 -07:00
CodeGenFunction.cpp -fsanitize=function: fix MSVC hashing to sugared type (#66816) 2023-10-02 19:09:39 +02:00
CodeGenFunction.h [Clang] Implement the 'counted_by' attribute 2023-10-04 18:26:15 -07:00
CodeGenModule.cpp -fsanitize=function: fix MSVC hashing to sugared type (#66816) 2023-10-02 19:09:39 +02:00
CodeGenModule.h Revert "[clang][CodeGen] Emit annotations for function declarations." 2023-09-13 13:22:57 +02:00
CodeGenPGO.cpp [CodeGen] Modernize BreakContinue (NFC) 2023-08-27 16:13:50 -07:00
CodeGenPGO.h [clang][CodeGenPGO] Don't use an invalid index when region counts disagree 2023-05-10 22:53:53 -04:00
CodeGenTBAA.cpp -fsanitize=function: fix MSVC hashing to sugared type (#66816) 2023-10-02 19:09:39 +02:00
CodeGenTBAA.h
CodeGenTypeCache.h [clang][CodeGen] Simplify code based on opaque pointers (#65624) 2023-09-25 11:21:24 +02:00
CodeGenTypes.cpp [PowerPC] Emit IR module flag for current float abi 2023-09-25 17:53:39 +08:00
CodeGenTypes.h [PowerPC] Emit IR module flag for current float abi 2023-09-25 17:53:39 +08:00
ConstantEmitter.h [CLANG] Fix uninitialized scalar field issues 2023-06-22 12:09:14 -07:00
ConstantInitBuilder.cpp [clang][NFC] Remove dependency on DataLayout::getPrefTypeAlignment 2023-01-13 15:01:29 +00:00
CoverageMappingGen.cpp Revert "[Coverage] Allow Clang coverage to be used with debug info correlation." 2023-09-26 20:57:09 -04:00
CoverageMappingGen.h [clang] NFCI: Use FileEntryRef in CoverageMappingGen 2023-09-06 11:15:51 -07:00
EHScopeStack.h [CodeGen] Modernize EHScopeStack::Cleanup::Flags (NFC) 2023-09-02 09:32:36 -07:00
ItaniumCXXABI.cpp [clang][CodeGen] Simplify code based on opaque pointers (#65624) 2023-09-25 11:21:24 +02:00
MacroPPCallbacks.cpp [Clang] Prepare for llvm::Optional becoming std::optional. 2022-12-20 00:41:40 +01:00
MacroPPCallbacks.h [Clang] Prepare for llvm::Optional becoming std::optional. 2022-12-20 00:41:40 +01:00
MicrosoftCXXABI.cpp [clang] Remove uses of llvm::Type::getPointerTo() (NFC) 2023-09-30 08:05:46 -04:00
ModuleBuilder.cpp [clang-repl][CUDA] Re-land: Initial interactive CUDA support for clang-repl 2023-05-27 13:54:42 +05:30
ObjectFilePCHContainerOperations.cpp [clang] set DebugCompilationDir in PCHContainer (#67744) 2023-09-29 11:30:33 -07:00
PatternInit.cpp [clang][TargetInfo] Use LangAS for getPointer{Width,Align}() 2022-11-30 20:24:01 +00:00
PatternInit.h
README.txt
SanitizerMetadata.cpp [IR] Adds Instruction::setNoSanitizeMetadata() 2023-05-19 19:18:57 +08:00
SanitizerMetadata.h [IR] Adds Instruction::setNoSanitizeMetadata() 2023-05-19 19:18:57 +08:00
SwiftCallingConv.cpp Use llvm::Log2_32 and llvm::Log2_64 instead of llvm::findLastSet (NFC) 2023-01-25 21:34:09 -08:00
TargetInfo.cpp [clang][CodeGen] Break up TargetInfo.cpp [8/8] 2023-06-17 07:14:50 +03:00
TargetInfo.h [OpenMP][DeviceRTL][AMDGPU] Support code object version 5 2023-08-29 06:35:44 -05:00
VarBypassDetector.cpp [clang] LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-08 09:12:46 -07:00
VarBypassDetector.h

IRgen optimization opportunities.

//===---------------------------------------------------------------------===//

The common pattern of
--
short x; // or char, etc
(x == 10)
--
generates an zext/sext of x which can easily be avoided.

//===---------------------------------------------------------------------===//

Bitfields accesses can be shifted to simplify masking and sign
extension. For example, if the bitfield width is 8 and it is
appropriately aligned then is is a lot shorter to just load the char
directly.

//===---------------------------------------------------------------------===//

It may be worth avoiding creation of alloca's for formal arguments
for the common situation where the argument is never written to or has
its address taken. The idea would be to begin generating code by using
the argument directly and if its address is taken or it is stored to
then generate the alloca and patch up the existing code.

In theory, the same optimization could be a win for block local
variables as long as the declaration dominates all statements in the
block.

NOTE: The main case we care about this for is for -O0 -g compile time
performance, and in that scenario we will need to emit the alloca
anyway currently to emit proper debug info. So this is blocked by
being able to emit debug information which refers to an LLVM
temporary, not an alloca.

//===---------------------------------------------------------------------===//

We should try and avoid generating basic blocks which only contain
jumps. At -O0, this penalizes us all the way from IRgen (malloc &
instruction overhead), all the way down through code generation and
assembly time.

On 176.gcc:expr.ll, it looks like over 12% of basic blocks are just
direct branches!

//===---------------------------------------------------------------------===//