From b580bd1eb2cedb51fdc2944ff7a42d9ac6898f4f Mon Sep 17 00:00:00 2001 From: Tanya Lattner Date: Wed, 16 Sep 2009 01:13:59 +0000 Subject: Merge 81845 from mainline. fix PR4963: folding insertvalue would sometimes turn a packed struct into an unpacked one. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_26@81980 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/ConstantFold.cpp | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'lib/VMCore/ConstantFold.cpp') diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index eda336a580..ac817d9a59 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -499,17 +499,19 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context, if (isa(Agg)) { // Insertion of constant into aggregate undef - // Optimize away insertion of undef + // Optimize away insertion of undef. if (isa(Val)) return const_cast(Agg); + // Otherwise break the aggregate undef into multiple undefs and do - // the insertion + // the insertion. const CompositeType *AggTy = cast(Agg->getType()); unsigned numOps; if (const ArrayType *AR = dyn_cast(AggTy)) numOps = AR->getNumElements(); else numOps = cast(AggTy)->getNumElements(); + std::vector Ops(numOps); for (unsigned i = 0; i < numOps; ++i) { const Type *MemberTy = AggTy->getTypeAtIndex(i); @@ -520,24 +522,27 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context, UndefValue::get(MemberTy); Ops[i] = const_cast(Op); } - if (isa(AggTy)) - return ConstantStruct::get(Context, Ops); - else - return ConstantArray::get(cast(AggTy), Ops); + + if (const StructType* ST = dyn_cast(AggTy)) + return ConstantStruct::get(Context, Ops, ST->isPacked()); + return ConstantArray::get(cast(AggTy), Ops); } + if (isa(Agg)) { // Insertion of constant into aggregate zero - // Optimize away insertion of zero + // Optimize away insertion of zero. if (Val->isNullValue()) return const_cast(Agg); + // Otherwise break the aggregate zero into multiple zeros and do - // the insertion + // the insertion. const CompositeType *AggTy = cast(Agg->getType()); unsigned numOps; if (const ArrayType *AR = dyn_cast(AggTy)) numOps = AR->getNumElements(); else numOps = cast(AggTy)->getNumElements(); + std::vector Ops(numOps); for (unsigned i = 0; i < numOps; ++i) { const Type *MemberTy = AggTy->getTypeAtIndex(i); @@ -549,13 +554,14 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context, Constant::getNullValue(MemberTy); Ops[i] = const_cast(Op); } - if (isa(AggTy)) - return ConstantStruct::get(Context, Ops); - else - return ConstantArray::get(cast(AggTy), Ops); + + if (const StructType* ST = dyn_cast(AggTy)) + return ConstantStruct::get(Context, Ops, ST->isPacked()); + return ConstantArray::get(cast(AggTy), Ops); } + if (isa(Agg) || isa(Agg)) { - // Insertion of constant into aggregate constant + // Insertion of constant into aggregate constant. std::vector Ops(Agg->getNumOperands()); for (unsigned i = 0; i < Agg->getNumOperands(); ++i) { const Constant *Op = @@ -565,12 +571,10 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context, Agg->getOperand(i); Ops[i] = const_cast(Op); } - Constant *C; - if (isa(Agg->getType())) - C = ConstantStruct::get(Context, Ops); - else - C = ConstantArray::get(cast(Agg->getType()), Ops); - return C; + + if (const StructType* ST = dyn_cast(Agg->getType())) + return ConstantStruct::get(Context, Ops, ST->isPacked()); + return ConstantArray::get(cast(Agg->getType()), Ops); } return 0; @@ -585,7 +589,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context, if (C1->getType() == Type::getPPC_FP128Ty(Context)) return 0; - // Handle UndefValue up front + // Handle UndefValue up front. if (isa(C1) || isa(C2)) { switch (Opcode) { case Instruction::Xor: -- cgit v1.2.3-18-g5258