mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-11-01 00:17:01 +00:00
Constfold insertelement to undef when index is out-of-bounds
Summary: This patch adds constant folding of insertelement instruction to undef value when index operand is constant and is not less than vector size or is undef. InstCombine does not support this case, but I'm happy to add it there also if this change is accepted. Test Plan: Unittests and regression tests for ConstProp pass. Reviewers: majnemer Reviewed By: majnemer Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D9287 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235854 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -800,23 +800,30 @@ Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
|
||||
Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
|
||||
Constant *Elt,
|
||||
Constant *Idx) {
|
||||
if (isa<UndefValue>(Idx))
|
||||
return UndefValue::get(Val->getType());
|
||||
|
||||
ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
|
||||
if (!CIdx) return nullptr;
|
||||
const APInt &IdxVal = CIdx->getValue();
|
||||
|
||||
|
||||
unsigned NumElts = Val->getType()->getVectorNumElements();
|
||||
if (CIdx->uge(NumElts))
|
||||
return UndefValue::get(Val->getType());
|
||||
|
||||
SmallVector<Constant*, 16> Result;
|
||||
Type *Ty = IntegerType::get(Val->getContext(), 32);
|
||||
for (unsigned i = 0, e = Val->getType()->getVectorNumElements(); i != e; ++i){
|
||||
Result.reserve(NumElts);
|
||||
auto *Ty = Type::getInt32Ty(Val->getContext());
|
||||
uint64_t IdxVal = CIdx->getZExtValue();
|
||||
for (unsigned i = 0; i != NumElts; ++i) {
|
||||
if (i == IdxVal) {
|
||||
Result.push_back(Elt);
|
||||
continue;
|
||||
}
|
||||
|
||||
Constant *C =
|
||||
ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
|
||||
Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
|
||||
Result.push_back(C);
|
||||
}
|
||||
|
||||
|
||||
return ConstantVector::get(Result);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user