Index: tools/clang/lib/CodeGen/CGDecl.cpp =================================================================== --- tools/clang/lib/CodeGen/CGDecl.cpp +++ tools/clang/lib/CodeGen/CGDecl.cpp @@ -1048,6 +1048,101 @@ return llvm::ConstantStruct::get(StructTy, Struct); } +/// Replace all padding bytes in a given constant with either a pattern byte or +/// 0x00. +static llvm::Constant *constWithPadding(CodeGenModule &CGM, bool isPattern, + llvm::Constant *constant) { + llvm::Type *OrigTy = constant->getType(); + 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 Types; + SmallVector Values; + bool Intact = true; + for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) { + unsigned CurOff = Layout->getElementOffset(i); + if (SizeSoFar < CurOff) { + assert(!STy->isPacked()); + auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar); + Types.push_back(PadTy); + if (isPattern) { + Values.push_back(patternFor(CGM, PadTy)); + } else { + Values.push_back(llvm::Constant::getNullValue(PadTy)); + } + Intact = false; + } + llvm::Constant *CurOp; + if (constant->isZeroValue()) { + CurOp = llvm::Constant::getNullValue(STy->getElementType(i)); + } else { + CurOp = cast(constant->getOperand(i)); + } + auto *NewOp = constWithPadding(CGM, isPattern, CurOp); + if (CurOp != NewOp) + Intact = false; + auto *NewOpTy = NewOp->getType(); + Values.push_back(NewOp); + Types.push_back(NewOpTy); + SizeSoFar = CurOff + DL.getTypeStoreSize(CurOp->getType()); + } + unsigned TotalSize = Layout->getSizeInBytes(); + if (SizeSoFar < TotalSize) { + auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar); + Types.push_back(PadTy); + if (isPattern) { + Values.push_back(patternFor(CGM, PadTy)); + } else { + Values.push_back(llvm::Constant::getNullValue(PadTy)); + } + Intact = false; + } + if (Intact) { + return constant; + } else { + llvm::StructType *NewSTy = + llvm::StructType::get(CGM.getLLVMContext(), Types, STy->isPacked()); + return llvm::ConstantStruct::get(NewSTy, Values); + } + } + if (OrigTy->isArrayTy() || OrigTy->isVectorTy()) { + llvm::SmallVector Values; + unsigned Size = constant->getNumOperands(); + llvm::Constant *OpValue; + Size = OrigTy->isArrayTy() ? OrigTy->getArrayNumElements() + : OrigTy->getVectorNumElements(); + if (!Size) + return constant; + if (!constant->isZeroValue()) { + for (unsigned Op = 0; Op != Size; ++Op) { + OpValue = cast(constant->getAggregateElement(Op)); + Values.push_back(constWithPadding(CGM, isPattern, OpValue)); + } + } else { + llvm::Type *ElemTy = OrigTy->isArrayTy() ? OrigTy->getArrayElementType() + : OrigTy->getVectorElementType(); + OpValue = llvm::Constant::getNullValue(ElemTy); + for (unsigned Op = 0; Op != Size; ++Op) { + Values.push_back(constWithPadding(CGM, isPattern, OpValue)); + } + } + if (Values[0]->getType() == constant->getAggregateElement(0U)->getType()) { + return constant; + } else { + if (OrigTy->isArrayTy()) { + auto *ArrayTy = llvm::ArrayType::get(Values[0]->getType(), Size); + return llvm::ConstantArray::get(ArrayTy, Values); + } else { + return llvm::ConstantVector::get(Values); + } + } + } + return constant; +} + static Address createUnnamedGlobalFrom(CodeGenModule &CGM, const VarDecl &D, CGBuilderTy &Builder, llvm::Constant *Constant, @@ -1146,7 +1241,8 @@ Address Loc, bool isVolatile, CGBuilderTy &Builder) { llvm::Type *ElTy = Loc.getElementType(); - llvm::Constant *constant = llvm::Constant::getNullValue(ElTy); + llvm::Constant *constant = constWithPadding( + CGM, /*isPattern*/ false, llvm::Constant::getNullValue(ElTy)); emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); } @@ -1154,7 +1250,8 @@ Address Loc, bool isVolatile, CGBuilderTy &Builder) { llvm::Type *ElTy = Loc.getElementType(); - llvm::Constant *constant = patternFor(CGM, ElTy); + llvm::Constant *constant = + constWithPadding(CGM, /*isPattern*/ true, patternFor(CGM, ElTy)); assert(!isa(constant)); emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant); } @@ -1170,13 +1267,19 @@ 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); + if (isa(constant)) { + if (isPattern) { + return patternFor(CGM, Ty); + } else { + return llvm::Constant::getNullValue(Ty); + } + } if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())) return constant; if (!containsUndef(constant)) @@ -1184,7 +1287,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 +1782,8 @@ case LangOptions::TrivialAutoVarInitKind::Pattern: { llvm::Type *ElTy = Loc.getElementType(); - llvm::Constant *Constant = patternFor(CGM, ElTy); + llvm::Constant *Constant = + constWithPadding(CGM, /*isPattern*/ true, patternFor(CGM, ElTy)); CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type); llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop"); llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop"); @@ -1725,8 +1829,12 @@ 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 = constWithPadding(CGM, isPattern, + 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,20 +48,23 @@ // 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_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 +// 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 { i8, [3 x i8], i32 } { i8 42, [3 x i8] c"\AA\AA\AA", i32 13371337 }, align 4 +// ZERO: @__const.test_padded_custom.custom = private unnamed_addr constant { i8, [3 x i8], i32 } { i8 42, [3 x i8] zeroinitializer, 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 { { i8, [3 x i8], i32 }, { i8, [3 x i8], i32 } } { { i8, [3 x i8], i32 } { i8 42, [3 x i8] c"\AA\AA\AA", i32 13371337 }, { i8, [3 x i8], i32 } { i8 43, [3 x i8] c"\AA\AA\AA", 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 +// PATTERN: @__const.test_bitfield_custom.custom = private unnamed_addr constant %struct.bitfield { i8 20, [3 x i8] c"\AA\AA\AA" }, align 4 // ZERO: @__const.test_bitfield_custom.custom = private unnamed_addr constant %struct.bitfield { i8 20, [3 x i8] zeroinitializer }, align 4 struct bitfield { int i : 4; int j : 2; }; // PATTERN: @__const.test_bitfieldaligned_uninit.uninit = private unnamed_addr constant %struct.bitfieldaligned { i8 -86, [3 x i8] c"\AA\AA\AA", i8 -86, [3 x i8] c"\AA\AA\AA" }, align 4 -// PATTERN: @__const.test_bitfieldaligned_custom.custom = private unnamed_addr constant %struct.bitfieldaligned { i8 4, [3 x i8] zeroinitializer, i8 1, [3 x i8] zeroinitializer }, align 4 +// PATTERN: @__const.test_bitfieldaligned_custom.custom = private unnamed_addr constant %struct.bitfieldaligned { i8 4, [3 x i8] c"\AA\AA\AA", i8 1, [3 x i8] c"\AA\AA\AA" }, align 4 // ZERO: @__const.test_bitfieldaligned_custom.custom = private unnamed_addr constant %struct.bitfieldaligned { i8 4, [3 x i8] zeroinitializer, i8 1, [3 x i8] zeroinitializer }, align 4 struct bitfieldaligned { int i : 4; int : 0; int j : 2; }; struct big { unsigned a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z; }; @@ -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_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 +// 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 { i16, i8, [1 x i8] }] [{ i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] c"\AA" }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] c"\AA" }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] c"\AA" }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] c"\AA" }], align 16 +// ZERO: @__const.test_tailpad4_custom.custom = private unnamed_addr constant [4 x { i16, i8, [1 x i8] }] [{ i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }, { i16, i8, [1 x i8] } { i16 257, i8 1, [1 x i8] zeroinitializer }], 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 @@ -121,7 +124,7 @@ // ZERO: @__const.test_matchingreverse_custom.custom = private unnamed_addr constant { i32 } { i32 61455 }, align 4 union unmatched { char c; int i; }; // PATTERN: @__const.test_unmatchedreverse_uninit.uninit = private unnamed_addr constant %union.unmatchedreverse { i32 -1431655766 }, align 4 -// PATTERN: @__const.test_unmatchedreverse_custom.custom = private unnamed_addr constant { i8, [3 x i8] } { i8 42, [3 x i8] zeroinitializer }, align 4 +// PATTERN: @__const.test_unmatchedreverse_custom.custom = private unnamed_addr constant { i8, [3 x i8] } { i8 42, [3 x i8] c"\AA\AA\AA" }, align 4 // ZERO: @__const.test_unmatched_custom.custom = private unnamed_addr constant %union.unmatched { i32 1001242351 }, align 4 union unmatchedreverse { int i; char c; }; // PATTERN: @__const.test_unmatchedfp_uninit.uninit = private unnamed_addr constant %union.unmatchedfp { double 0xFFFFFFFFFFFFFFFF }, align 8 @@ -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