Index: include/llvm/IR/Instructions.h =================================================================== --- include/llvm/IR/Instructions.h +++ include/llvm/IR/Instructions.h @@ -98,9 +98,16 @@ return cast(Instruction::getType()); } - /// Get allocation size in bits. Returns None if size can't be determined, - /// e.g. in case of a VLA. - Optional getAllocationSizeInBits(const DataLayout &DL) const; + /// Get allocation size. Returns None if the size can't be determined, + /// e.g. in case of a dynamic alloca. + Optional getAllocationSize(const DataLayout &DL, + bool InBits = false) const; + + /// Get allocation size in bits. Returns None if the size can't be determined, + /// e.g. in case of a dynamic alloca. + Optional getAllocationSizeInBits(const DataLayout &DL) const { + return getAllocationSize(DL, true /* InBits */); + } /// Return the type that is being allocated by the instruction. Type *getAllocatedType() const { return AllocatedType; } Index: lib/CodeGen/SafeStack.cpp =================================================================== --- lib/CodeGen/SafeStack.cpp +++ lib/CodeGen/SafeStack.cpp @@ -218,14 +218,9 @@ }; uint64_t SafeStack::getStaticAllocaAllocationSize(const AllocaInst* AI) { - uint64_t Size = DL.getTypeAllocSize(AI->getAllocatedType()); - if (AI->isArrayAllocation()) { - auto C = dyn_cast(AI->getArraySize()); - if (!C) - return 0; - Size *= C->getZExtValue(); - } - return Size; + if (auto Size = AI->getAllocationSize(DL)) + return *Size; + return 0; } bool SafeStack::IsAccessSafe(Value *Addr, uint64_t AccessSize, Index: lib/IR/Instructions.cpp =================================================================== --- lib/IR/Instructions.cpp +++ lib/IR/Instructions.cpp @@ -49,8 +49,11 @@ //===----------------------------------------------------------------------===// Optional -AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const { - uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType()); +AllocaInst::getAllocationSize(const DataLayout &DL, bool InBits) const { + if (!getAllocatedType()->isSized()) + return None; + uint64_t Size = InBits ? DL.getTypeAllocSizeInBits(getAllocatedType()) + : DL.getTypeAllocSize(getAllocatedType()); if (isArrayAllocation()) { auto C = dyn_cast(getArraySize()); if (!C) Index: lib/Transforms/Instrumentation/AddressSanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -620,16 +620,10 @@ } uint64_t getAllocaSizeInBytes(const AllocaInst &AI) const { - uint64_t ArraySize = 1; - if (AI.isArrayAllocation()) { - const ConstantInt *CI = dyn_cast(AI.getArraySize()); - assert(CI && "non-constant array size"); - ArraySize = CI->getZExtValue(); - } - Type *Ty = AI.getAllocatedType(); - uint64_t SizeInBytes = - AI.getModule()->getDataLayout().getTypeAllocSize(Ty); - return SizeInBytes * ArraySize; + const DataLayout &DL = AI.getModule()->getDataLayout(); + Optional Size = AI.getAllocationSize(DL); + assert(Size && "Unknown alloca size."); + return *Size; } /// Check if we want (and can) handle this alloca. Index: lib/Transforms/Instrumentation/HWAddressSanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -494,15 +494,10 @@ } static uint64_t getAllocaSizeInBytes(const AllocaInst &AI) { - uint64_t ArraySize = 1; - if (AI.isArrayAllocation()) { - const ConstantInt *CI = dyn_cast(AI.getArraySize()); - assert(CI && "non-constant array size"); - ArraySize = CI->getZExtValue(); - } - Type *Ty = AI.getAllocatedType(); - uint64_t SizeInBytes = AI.getModule()->getDataLayout().getTypeAllocSize(Ty); - return SizeInBytes * ArraySize; + const DataLayout &DL = AI.getModule()->getDataLayout(); + Optional Size = AI.getAllocationSize(DL); + assert(Size && "Unknown alloca size."); + return *Size; } bool HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI,