Skip to content

Commit aea386f

Browse files
committed
Merge 81845 from mainline.
fix PR4963: folding insertvalue would sometimes turn a packed struct into an unpacked one. llvm-svn: 81980
1 parent a590c65 commit aea386f

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

llvm/lib/VMCore/ConstantFold.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,19 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
499499

500500
if (isa<UndefValue>(Agg)) {
501501
// Insertion of constant into aggregate undef
502-
// Optimize away insertion of undef
502+
// Optimize away insertion of undef.
503503
if (isa<UndefValue>(Val))
504504
return const_cast<Constant*>(Agg);
505+
505506
// Otherwise break the aggregate undef into multiple undefs and do
506-
// the insertion
507+
// the insertion.
507508
const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
508509
unsigned numOps;
509510
if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
510511
numOps = AR->getNumElements();
511512
else
512513
numOps = cast<StructType>(AggTy)->getNumElements();
514+
513515
std::vector<Constant*> Ops(numOps);
514516
for (unsigned i = 0; i < numOps; ++i) {
515517
const Type *MemberTy = AggTy->getTypeAtIndex(i);
@@ -520,24 +522,27 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
520522
UndefValue::get(MemberTy);
521523
Ops[i] = const_cast<Constant*>(Op);
522524
}
523-
if (isa<StructType>(AggTy))
524-
return ConstantStruct::get(Context, Ops);
525-
else
526-
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
525+
526+
if (const StructType* ST = dyn_cast<StructType>(AggTy))
527+
return ConstantStruct::get(Context, Ops, ST->isPacked());
528+
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
527529
}
530+
528531
if (isa<ConstantAggregateZero>(Agg)) {
529532
// Insertion of constant into aggregate zero
530-
// Optimize away insertion of zero
533+
// Optimize away insertion of zero.
531534
if (Val->isNullValue())
532535
return const_cast<Constant*>(Agg);
536+
533537
// Otherwise break the aggregate zero into multiple zeros and do
534-
// the insertion
538+
// the insertion.
535539
const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
536540
unsigned numOps;
537541
if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
538542
numOps = AR->getNumElements();
539543
else
540544
numOps = cast<StructType>(AggTy)->getNumElements();
545+
541546
std::vector<Constant*> Ops(numOps);
542547
for (unsigned i = 0; i < numOps; ++i) {
543548
const Type *MemberTy = AggTy->getTypeAtIndex(i);
@@ -549,13 +554,14 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
549554
Constant::getNullValue(MemberTy);
550555
Ops[i] = const_cast<Constant*>(Op);
551556
}
552-
if (isa<StructType>(AggTy))
553-
return ConstantStruct::get(Context, Ops);
554-
else
555-
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
557+
558+
if (const StructType* ST = dyn_cast<StructType>(AggTy))
559+
return ConstantStruct::get(Context, Ops, ST->isPacked());
560+
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
556561
}
562+
557563
if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
558-
// Insertion of constant into aggregate constant
564+
// Insertion of constant into aggregate constant.
559565
std::vector<Constant*> Ops(Agg->getNumOperands());
560566
for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
561567
const Constant *Op =
@@ -565,12 +571,10 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
565571
Agg->getOperand(i);
566572
Ops[i] = const_cast<Constant*>(Op);
567573
}
568-
Constant *C;
569-
if (isa<StructType>(Agg->getType()))
570-
C = ConstantStruct::get(Context, Ops);
571-
else
572-
C = ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
573-
return C;
574+
575+
if (const StructType* ST = dyn_cast<StructType>(Agg->getType()))
576+
return ConstantStruct::get(Context, Ops, ST->isPacked());
577+
return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
574578
}
575579

576580
return 0;
@@ -585,7 +589,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context,
585589
if (C1->getType() == Type::getPPC_FP128Ty(Context))
586590
return 0;
587591

588-
// Handle UndefValue up front
592+
// Handle UndefValue up front.
589593
if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
590594
switch (Opcode) {
591595
case Instruction::Xor:

llvm/test/Assembler/insertextractvalue.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ define float @dar({{i32},{float, double}}* %p) nounwind {
2121
store {{i32},{float, double}} insertvalue ({{i32},{float, double}} zeroinitializer, double 20.0, 1, 1), {{i32},{float, double}}* %p
2222
ret float extractvalue ({{i32},{float, double}} zeroinitializer, 1, 0)
2323
}
24+
25+
26+
; PR4963
27+
define <{ i32, i32 }> @test57() {
28+
ret <{ i32, i32 }> insertvalue (<{ i32, i32 }> zeroinitializer, i32 4, 1)
29+
}

0 commit comments

Comments
 (0)