From 9aab85a6a07f35a33fb703f40e3e87aca1a08735 Mon Sep 17 00:00:00 2001 From: JF Bastien Date: Fri, 13 Jul 2018 20:33:23 +0000 Subject: CodeGen: specify alignment + inbounds for automatic variable initialization Summary: Automatic variable initialization was generating default-aligned stores (which are deprecated) instead of using the known alignment from the alloca. Further, they didn't specify inbounds. Subscribers: dexonsmith, cfe-commits Differential Revision: https://reviews.llvm.org/D49209 llvm-svn: 337041 --- clang/lib/CodeGen/CGBuilder.h | 15 +++++++++++++++ clang/lib/CodeGen/CGDecl.cpp | 17 ++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) (limited to 'clang/lib/CodeGen') diff --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h index 5b9c7a3c46a..d2e5eb256d3 100644 --- a/clang/lib/CodeGen/CGBuilder.h +++ b/clang/lib/CodeGen/CGBuilder.h @@ -244,6 +244,21 @@ public: Addr.getAlignment().alignmentAtOffset(Offset)); } + using CGBuilderBaseTy::CreateConstInBoundsGEP2_32; + Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, + unsigned Idx1, const llvm::DataLayout &DL, + const llvm::Twine &Name = "") { + auto *GEP = cast(CreateConstInBoundsGEP2_32( + Addr.getElementType(), Addr.getPointer(), Idx0, Idx1, Name)); + llvm::APInt Offset( + DL.getIndexSizeInBits(Addr.getType()->getPointerAddressSpace()), 0, + /*IsSigned=*/true); + if (!GEP->accumulateConstantOffset(DL, Offset)) + llvm_unreachable("offset of GEP with constants is always computable"); + return Address(GEP, Addr.getAlignment().alignmentAtOffset( + CharUnits::fromQuantity(Offset.getSExtValue()))); + } + llvm::Value *CreateConstInBoundsByteGEP(llvm::Value *Ptr, CharUnits Offset, const llvm::Twine &Name = "") { assert(Ptr->getType()->getPointerElementType() == TypeCache.Int8Ty); diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 1e9a7d5856d..9350e88f2eb 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -888,15 +888,17 @@ static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, /// emitStoresForInitAfterMemset - For inits that /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar /// stores that would be required. -static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, - bool isVolatile, CGBuilderTy &Builder) { +static void emitStoresForInitAfterMemset(CodeGenModule &CGM, + llvm::Constant *Init, Address Loc, + bool isVolatile, + CGBuilderTy &Builder) { assert(!Init->isNullValue() && !isa(Init) && "called emitStoresForInitAfterMemset for zero or undef value."); if (isa(Init) || isa(Init) || isa(Init) || isa(Init) || isa(Init)) { - Builder.CreateDefaultAlignedStore(Init, Loc, isVolatile); + Builder.CreateStore(Init, Loc, isVolatile); return; } @@ -908,7 +910,8 @@ static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, // If necessary, get a pointer to the element and emit it. if (!Elt->isNullValue() && !isa(Elt)) emitStoresForInitAfterMemset( - Elt, Builder.CreateConstGEP2_32(Init->getType(), Loc, 0, i), + CGM, Elt, + Builder.CreateConstInBoundsGEP2_32(Loc, 0, i, CGM.getDataLayout()), isVolatile, Builder); } return; @@ -923,7 +926,8 @@ static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, // If necessary, get a pointer to the element and emit it. if (!Elt->isNullValue() && !isa(Elt)) emitStoresForInitAfterMemset( - Elt, Builder.CreateConstGEP2_32(Init->getType(), Loc, 0, i), + CGM, Elt, + Builder.CreateConstInBoundsGEP2_32(Loc, 0, i, CGM.getDataLayout()), isVolatile, Builder); } } @@ -1411,8 +1415,7 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { if (!constant->isNullValue() && !isa(constant)) { Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo(Loc.getAddressSpace())); - emitStoresForInitAfterMemset(constant, Loc.getPointer(), - isVolatile, Builder); + emitStoresForInitAfterMemset(CGM, constant, Loc, isVolatile, Builder); } } else { // Otherwise, create a temporary global with the initializer then -- cgit v1.2.3