[PATCH] D40650: [InstSimplify] Fold insertelement into undef if index is out of bounds
Igor Laevsky via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 30 06:36:12 PST 2017
igor-laevsky created this revision.
This is branched from the https://reviews.llvm.org/D40390.
According to the llvm ir specification insertelement with out of bounds index is undef. This change implements this rule for the InstSimplify. It's mostly valuable as a way to prevent accidental crashes in various passes which are not expecting such indexes.
https://reviews.llvm.org/D40650
Files:
include/llvm/Analysis/InstructionSimplify.h
lib/Analysis/InstructionSimplify.cpp
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -3769,6 +3769,20 @@
return ::SimplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
}
+Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *, Value *Idx,
+ const SimplifyQuery &) {
+ // Fold into undef if index is out of bounds
+ if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
+ uint64_t IdxVal = CI->getZExtValue();
+ uint64_t NumElements = cast<VectorType>(Vec->getType())->getNumElements();
+
+ if (IdxVal <= NumElements)
+ return UndefValue::get(Vec->getType());
+ }
+
+ return nullptr;
+}
+
/// Given operands for an ExtractValueInst, see if we can fold the result.
/// If not, this returns null.
static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
@@ -4637,6 +4651,12 @@
IV->getIndices(), Q);
break;
}
+ case Instruction::InsertElement: {
+ auto *IE = cast<InsertElementInst>(I);
+ Result = SimplifyInsertElementInst(IE->getOperand(0), IE->getOperand(1),
+ IE->getOperand(2), Q);
+ break;
+ }
case Instruction::ExtractValue: {
auto *EVI = cast<ExtractValueInst>(I);
Result = SimplifyExtractValueInst(EVI->getAggregateOperand(),
Index: include/llvm/Analysis/InstructionSimplify.h
===================================================================
--- include/llvm/Analysis/InstructionSimplify.h
+++ include/llvm/Analysis/InstructionSimplify.h
@@ -161,6 +161,10 @@
Value *SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
const SimplifyQuery &Q);
+/// Given operands for an InsertElement, fold the result or return null.
+Value *SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
+ const SimplifyQuery &Q);
+
/// Given operands for an ExtractValueInst, fold the result or return null.
Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
const SimplifyQuery &Q);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40650.124930.patch
Type: text/x-patch
Size: 2222 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171130/6f9b9975/attachment.bin>
More information about the llvm-commits
mailing list