diff options
author | Pawel Bylica <chfast@gmail.com> | 2015-04-27 09:30:49 +0000 |
---|---|---|
committer | Pawel Bylica <chfast@gmail.com> | 2015-04-27 09:30:49 +0000 |
commit | c25918a043de023f94f40f513aba26455abe01a7 (patch) | |
tree | dcbc6c7119f9d887ef6aa135fece8b60739dce54 /llvm/lib/IR/ConstantFold.cpp | |
parent | 980b92a8e65b8aa36d4ceb3eb1ff8abce5eb74fc (diff) | |
download | llvm-c25918a043de023f94f40f513aba26455abe01a7.zip llvm-c25918a043de023f94f40f513aba26455abe01a7.tar.gz llvm-c25918a043de023f94f40f513aba26455abe01a7.tar.bz2 |
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
llvm-svn: 235854
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 03d7c5e..2a52493 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -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); } |