@@ -3827,6 +3827,28 @@ Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val,
3827
3827
return ::SimplifyInsertValueInst (Agg, Val, Idxs, Q, RecursionLimit);
3828
3828
}
3829
3829
3830
+ Value *llvm::SimplifyInsertElementInst (Value *Vec, Value *Val, Value *Idx,
3831
+ const SimplifyQuery &Q) {
3832
+ // Try to constant fold.
3833
+ auto *VecC = dyn_cast<Constant>(Vec);
3834
+ auto *ValC = dyn_cast<Constant>(Val);
3835
+ auto *IdxC = dyn_cast<Constant>(Idx);
3836
+ if (VecC && ValC && IdxC)
3837
+ return ConstantFoldInsertElementInstruction (VecC, ValC, IdxC);
3838
+
3839
+ // Fold into undef if index is out of bounds.
3840
+ if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
3841
+ uint64_t NumElements = cast<VectorType>(Vec->getType ())->getNumElements ();
3842
+
3843
+ if (CI->uge (NumElements))
3844
+ return UndefValue::get (Vec->getType ());
3845
+ }
3846
+
3847
+ // TODO: We should also fold if index is iteslf an undef.
3848
+
3849
+ return nullptr ;
3850
+ }
3851
+
3830
3852
// / Given operands for an ExtractValueInst, see if we can fold the result.
3831
3853
// / If not, this returns null.
3832
3854
static Value *SimplifyExtractValueInst (Value *Agg, ArrayRef<unsigned > Idxs,
@@ -4700,6 +4722,12 @@ Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ,
4700
4722
IV->getIndices (), Q);
4701
4723
break ;
4702
4724
}
4725
+ case Instruction::InsertElement: {
4726
+ auto *IE = cast<InsertElementInst>(I);
4727
+ Result = SimplifyInsertElementInst (IE->getOperand (0 ), IE->getOperand (1 ),
4728
+ IE->getOperand (2 ), Q);
4729
+ break ;
4730
+ }
4703
4731
case Instruction::ExtractValue: {
4704
4732
auto *EVI = cast<ExtractValueInst>(I);
4705
4733
Result = SimplifyExtractValueInst (EVI->getAggregateOperand (),
0 commit comments