Index: tools/clang/lib/CodeGen/CGDecl.cpp =================================================================== --- tools/clang/lib/CodeGen/CGDecl.cpp +++ tools/clang/lib/CodeGen/CGDecl.cpp @@ -969,6 +969,58 @@ return llvm::isBytewiseValue(Init); } +/// For a given type, create a binary compatible one with padding replaced by +/// byte arrays. +static llvm::Type *typeWithoutPadding(CodeGenModule &CGM, llvm::Type *OrigTy) { + if (OrigTy->isStructTy()) { + llvm::StructType *STy = dyn_cast(OrigTy); + const llvm::DataLayout &DL = CGM.getDataLayout(); + const llvm::StructLayout *Layout = DL.getStructLayout(STy); + llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext()); + unsigned SizeSoFar = 0; + SmallVector NewElems; + bool Intact = true; + for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) { + unsigned CurOff = Layout->getElementOffset(i); + if (SizeSoFar < CurOff) { + assert(!STy->isPacked()); + NewElems.push_back(llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar)); + Intact = false; + } + auto *CurTy = STy->getElementType(i); + auto *NewTy = typeWithoutPadding(CGM, CurTy); + if (CurTy != NewTy) + Intact = false; + NewElems.push_back(NewTy); + SizeSoFar = CurOff + DL.getTypeStoreSize(CurTy); + } + unsigned TotalSize = Layout->getSizeInBytes(); + if (SizeSoFar < TotalSize) { + NewElems.push_back(llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar)); + Intact = false; + } + if (Intact) + return OrigTy; + else + return llvm::StructType::get(CGM.getLLVMContext(), NewElems, + STy->isPacked()); + } + if (OrigTy->isArrayTy() || OrigTy->isVectorTy()) { + llvm::SequentialType *SeqTy = dyn_cast(OrigTy); + llvm::Type *OldETy = SeqTy->getElementType(); + llvm::Type *NewETy = typeWithoutPadding(CGM, OldETy); + if (NewETy == OldETy) { + return OrigTy; + } else { + if (OrigTy->isArrayTy()) + return llvm::ArrayType::get(NewETy, SeqTy->getNumElements()); + else + return llvm::VectorType::get(NewETy, SeqTy->getNumElements()); + } + } + return OrigTy; +} + static llvm::Constant *patternFor(CodeGenModule &CGM, llvm::Type *Ty) { // The following value is a guaranteed unmappable pointer value and has a // repeated byte-pattern which makes it easier to synthesize. We use it for @@ -1146,7 +1198,8 @@ Address Loc, bool isVolatile, CGBuilderTy &Builder) { llvm::Type *ElTy = Loc.getElementType(); - llvm::Constant *constant = llvm::Constant::getNullValue(ElTy); + llvm::Type *NoPadTy = typeWithoutPadding(CGM, ElTy); + llvm::Constant *constant = llvm::Constant::getNullValue(NoPadTy); emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); } @@ -1154,7 +1207,8 @@ Address Loc, bool isVolatile, CGBuilderTy &Builder) { llvm::Type *ElTy = Loc.getElementType(); - llvm::Constant *constant = patternFor(CGM, ElTy); + llvm::Type *NoPadTy = typeWithoutPadding(CGM, ElTy); + llvm::Constant *constant = patternFor(CGM, NoPadTy); assert(!isa(constant)); emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); } @@ -1170,13 +1224,21 @@ return false; } -static llvm::Constant *replaceUndef(llvm::Constant *constant) { +static llvm::Constant *replaceUndef(CodeGenModule &CGM, bool isPattern, + llvm::Constant *constant) { // FIXME: when doing pattern initialization, replace undef with 0xAA instead. // FIXME: also replace padding between values by creating a new struct type // which has no padding. auto *Ty = constant->getType(); - if (isa(constant)) - return llvm::Constant::getNullValue(Ty); + // llvm::errs() << "HERE: " << __LINE__ << "\n"; + if (isa(constant)) { + if (isPattern) { + llvm::Type *NoPadTy = typeWithoutPadding(CGM, Ty); + return patternFor(CGM, NoPadTy); + } else { + return llvm::Constant::getNullValue(Ty); + } + } if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())) return constant; if (!containsUndef(constant)) @@ -1184,7 +1246,7 @@ llvm::SmallVector Values(constant->getNumOperands()); for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; ++Op) { auto *OpValue = cast(constant->getOperand(Op)); - Values[Op] = replaceUndef(OpValue); + Values[Op] = replaceUndef(CGM, isPattern, OpValue); } if (Ty->isStructTy()) return llvm::ConstantStruct::get(cast(Ty), Values); @@ -1679,7 +1741,8 @@ case LangOptions::TrivialAutoVarInitKind::Pattern: { llvm::Type *ElTy = Loc.getElementType(); - llvm::Constant *Constant = patternFor(CGM, ElTy); + llvm::Type *NoPadTy = typeWithoutPadding(CGM, ElTy); + llvm::Constant *Constant = patternFor(CGM, NoPadTy); CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type); llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop"); llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop"); @@ -1725,8 +1788,11 @@ assert(!capturedByInit && "constant init contains a capturing block?"); constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D); if (constant && trivialAutoVarInit != - LangOptions::TrivialAutoVarInitKind::Uninitialized) - constant = replaceUndef(constant); + LangOptions::TrivialAutoVarInitKind::Uninitialized) { + bool isPattern = + (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern); + constant = replaceUndef(CGM, isPattern, constant); + } } if (!constant) { Index: tools/clang/test/CodeGenCXX/auto-var-init.cpp =================================================================== --- tools/clang/test/CodeGenCXX/auto-var-init.cpp +++ tools/clang/test/CodeGenCXX/auto-var-init.cpp @@ -48,14 +48,17 @@ // PATTERN: @__const.test_nullinit_braces.braces = private unnamed_addr constant %struct.nullinit { i8* inttoptr (i64 -6148914691236517206 to i8*) }, align 8 // PATTERN: @__const.test_nullinit_custom.custom = private unnamed_addr constant %struct.nullinit { i8* inttoptr (i64 -6148914691236517206 to i8*) }, align 8 struct nullinit { char* null = nullptr; }; -// PATTERN: @__const.test_padded_uninit.uninit = private unnamed_addr constant %struct.padded { i8 -86, i32 -1431655766 }, align 4 +// PATTERN: @__const.test_padded_uninit.uninit = private unnamed_addr constant { i8, [3 x i8], i32 } { i8 -86, [3 x i8] c"\AA\AA\AA", i32 -1431655766 }, align 4 // PATTERN: @__const.test_padded_custom.custom = private unnamed_addr constant %struct.padded { i8 42, i32 13371337 }, align 4 // ZERO: @__const.test_padded_custom.custom = private unnamed_addr constant %struct.padded { i8 42, i32 13371337 }, align 4 struct padded { char c; int i; }; -// PATTERN: @__const.test_paddednullinit_uninit.uninit = private unnamed_addr constant %struct.paddednullinit { i8 -86, i32 -1431655766 }, align 4 -// PATTERN: @__const.test_paddednullinit_braces.braces = private unnamed_addr constant %struct.paddednullinit { i8 -86, i32 -1431655766 }, align 4 -// PATTERN: @__const.test_paddednullinit_custom.custom = private unnamed_addr constant %struct.paddednullinit { i8 -86, i32 -1431655766 }, align 4 +// PATTERN: @__const.test_paddednullinit_uninit.uninit = private unnamed_addr constant { i8, [3 x i8], i32 } { i8 -86, [3 x i8] c"\AA\AA\AA", i32 -1431655766 }, align 4 +// PATTERN: @__const.test_paddednullinit_braces.braces = private unnamed_addr constant { i8, [3 x i8], i32 } { i8 -86, [3 x i8] c"\AA\AA\AA", i32 -1431655766 }, align 4 +// PATTERN: @__const.test_paddednullinit_custom.custom = private unnamed_addr constant { i8, [3 x i8], i32 } { i8 -86, [3 x i8] c"\AA\AA\AA", i32 -1431655766 }, align 4 struct paddednullinit { char c = 0; int i = 0; }; +// PATTERN: @__const.test_paddednested_uninit.uninit = private unnamed_addr constant { { i8, [3 x i8], i32 }, { i8, [3 x i8], i32 } } { { i8, [3 x i8], i32 } { i8 -86, [3 x i8] c"\AA\AA\AA", i32 -1431655766 }, { i8, [3 x i8], i32 } { i8 -86, [3 x i8] c"\AA\AA\AA", i32 -1431655766 } }, align 4 +// PATTERN: @__const.test_paddednested_custom.custom = private unnamed_addr constant %struct.paddednested { %struct.padded { i8 42, i32 13371337 }, %struct.padded { i8 43, i32 13371338 } }, align 4 +struct paddednested { struct padded p1, p2; }; // PATTERN: @__const.test_bitfield_uninit.uninit = private unnamed_addr constant %struct.bitfield { i8 -86, [3 x i8] c"\AA\AA\AA" }, align 4 // PATTERN: @__const.test_bitfield_custom.custom = private unnamed_addr constant %struct.bitfield { i8 20, [3 x i8] zeroinitializer }, align 4 // ZERO: @__const.test_bitfield_custom.custom = private unnamed_addr constant %struct.bitfield { i8 20, [3 x i8] zeroinitializer }, align 4 @@ -78,14 +81,14 @@ // PATTERN: @__const.test_intptr4_uninit.uninit = private unnamed_addr constant [4 x i32*] [i32* inttoptr (i64 -6148914691236517206 to i32*), i32* inttoptr (i64 -6148914691236517206 to i32*), i32* inttoptr (i64 -6148914691236517206 to i32*), i32* inttoptr (i64 -6148914691236517206 to i32*)], align 16 // PATTERN: @__const.test_intptr4_custom.custom = private unnamed_addr constant [4 x i32*] [i32* inttoptr (i64 572662306 to i32*), i32* inttoptr (i64 572662306 to i32*), i32* inttoptr (i64 572662306 to i32*), i32* inttoptr (i64 572662306 to i32*)], align 16 // ZERO: @__const.test_intptr4_custom.custom = private unnamed_addr constant [4 x i32*] [i32* inttoptr (i64 572662306 to i32*), i32* inttoptr (i64 572662306 to i32*), i32* inttoptr (i64 572662306 to i32*), i32* inttoptr (i64 572662306 to i32*)], align 16 -// PATTERN: @__const.test_tailpad4_uninit.uninit = private unnamed_addr constant [4 x %struct.tailpad] [%struct.tailpad { i16 -21846, i8 -86 }, %struct.tailpad { i16 -21846, i8 -86 }, %struct.tailpad { i16 -21846, i8 -86 }, %struct.tailpad { i16 -21846, i8 -86 }], align 16 +// PATTERN: @__const.test_tailpad4_uninit.uninit = private unnamed_addr constant [4 x { i16, i8, [1 x i8] }] [{ i16, i8, [1 x i8] } { i16 -21846, i8 -86, [1 x i8] c"\AA" }, { i16, i8, [1 x i8] } { i16 -21846, i8 -86, [1 x i8] c"\AA" }, { i16, i8, [1 x i8] } { i16 -21846, i8 -86, [1 x i8] c"\AA" }, { i16, i8, [1 x i8] } { i16 -21846, i8 -86, [1 x i8] c"\AA" }], align 16 // PATTERN: @__const.test_tailpad4_custom.custom = private unnamed_addr constant [4 x %struct.tailpad] [%struct.tailpad { i16 257, i8 1 }, %struct.tailpad { i16 257, i8 1 }, %struct.tailpad { i16 257, i8 1 }, %struct.tailpad { i16 257, i8 1 }], align 16 // ZERO: @__const.test_tailpad4_custom.custom = private unnamed_addr constant [4 x %struct.tailpad] [%struct.tailpad { i16 257, i8 1 }, %struct.tailpad { i16 257, i8 1 }, %struct.tailpad { i16 257, i8 1 }, %struct.tailpad { i16 257, i8 1 }], align 16 struct tailpad { short s; char c; }; // PATTERN: @__const.test_atomicnotlockfree_uninit.uninit = private unnamed_addr constant %struct.notlockfree { [4 x i64] [i64 -6148914691236517206, i64 -6148914691236517206, i64 -6148914691236517206, i64 -6148914691236517206] }, align 8 struct notlockfree { long long a[4]; }; -// PATTERN: @__const.test_atomicpadded_uninit.uninit = private unnamed_addr constant %struct.padded { i8 -86, i32 -1431655766 }, align 8 -// PATTERN: @__const.test_atomictailpad_uninit.uninit = private unnamed_addr constant %struct.tailpad { i16 -21846, i8 -86 }, align 4 +// PATTERN: @__const.test_atomicpadded_uninit.uninit = private unnamed_addr constant { i8, [3 x i8], i32 } { i8 -86, [3 x i8] c"\AA\AA\AA", i32 -1431655766 }, align 8 +// PATTERN: @__const.test_atomictailpad_uninit.uninit = private unnamed_addr constant { i16, i8, [1 x i8] } { i16 -21846, i8 -86, [1 x i8] c"\AA" }, align 4 // PATTERN: @__const.test_complexfloat_uninit.uninit = private unnamed_addr constant { float, float } { float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000 }, align 4 // PATTERN: @__const.test_complexfloat_braces.braces = private unnamed_addr constant { float, float } { float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000 }, align 4 // PATTERN: @__const.test_complexfloat_custom.custom = private unnamed_addr constant { float, float } { float 0xFFFFFFFFE0000000, float 0xFFFFFFFFE0000000 }, align 4 @@ -625,6 +628,29 @@ // CHECK-NEXT: store i32 13371337, i32* %[[I]], align [[ALIGN]] // CHECK-NEXT: call void @{{.*}}used{{.*}}%custom) +TEST_UNINIT(paddednested, paddednested); +// CHECK-LABEL: @test_paddednested_uninit() +// CHECK: %uninit = alloca %struct.paddednested, align +// CHECK-NEXT: call void @{{.*}}used{{.*}}%uninit) +// PATTERN-LABEL: @test_paddednested_uninit() +// PATTERN: call void @llvm.memcpy{{.*}} @__const.test_paddednested_uninit.uninit +// ZERO-LABEL: @test_paddednested_uninit() +// ZERO: call void @llvm.memset{{.*}}, i8 0, + +TEST_BRACES(paddednested, paddednested); +// CHECK-LABEL: @test_paddednested_braces() +// CHECK: %braces = alloca %struct.paddednested, align [[ALIGN:[0-9]*]] +// CHECK-NEXT: bitcast +// CHECK-NEXT: call void @llvm.memset{{.*}}(i8* align [[ALIGN]] %{{.*}}, i8 0, i64 16, i1 false) +// CHECK-NEXT: call void @{{.*}}used{{.*}}%braces) + +TEST_CUSTOM(paddednested, paddednested, { { 42, 13371337 }, { 43, 13371338 } }); +// CHECK-LABEL: @test_paddednested_custom() +// CHECK: %custom = alloca %struct.paddednested, align +// CHECK-NEXT: bitcast +// CHECK-NEXT: call void @llvm.memcpy +// CHECK-NEXT: call void @{{.*}}used{{.*}}%custom) + TEST_UNINIT(bitfield, bitfield); // CHECK-LABEL: @test_bitfield_uninit() // CHECK: %uninit = alloca %struct.bitfield, align