From: Igor Laevsky Date: Thu, 7 Dec 2017 15:00:52 +0000 (+0000) Subject: [InstCombine] Don't crash on out of bounds index in the insertelement X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e18338969d0ed13427863fb23fa4a5c8e8df0b14;p=llvm [InstCombine] Don't crash on out of bounds index in the insertelement Differential Revision: https://reviews.llvm.org/D40390 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320049 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/lib/Transforms/InstCombine/InstCombineVectorOps.cpp index 6c99007475c..65a96b96522 100644 --- a/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ b/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -781,6 +781,10 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { Value *ScalarOp = IE.getOperand(1); Value *IdxOp = IE.getOperand(2); + if (auto *V = SimplifyInsertElementInst( + VecOp, ScalarOp, IdxOp, SQ.getWithInstruction(&IE))) + return replaceInstUsesWith(IE, V); + // Inserting an undef or into an undefined place, remove this. if (isa(ScalarOp) || isa(IdxOp)) replaceInstUsesWith(IE, VecOp); diff --git a/test/Transforms/InstCombine/out-of-bounds-indexes.ll b/test/Transforms/InstCombine/out-of-bounds-indexes.ll index a1887d27550..02be57a4d15 100644 --- a/test/Transforms/InstCombine/out-of-bounds-indexes.ll +++ b/test/Transforms/InstCombine/out-of-bounds-indexes.ll @@ -31,3 +31,11 @@ define i128 @test_non64bit(i128 %a) { } declare void @llvm.assume(i1) + +define <4 x double> @inselt_bad_index(<4 x double> %a) { +; CHECK-LABEL: @inselt_bad_index( +; CHECK-NEXT: ret <4 x double> undef +; + %I = insertelement <4 x double> %a, double 0.0, i64 4294967296 + ret <4 x double> %I +} diff --git a/test/Transforms/InstCombine/vector_insertelt_shuffle.ll b/test/Transforms/InstCombine/vector_insertelt_shuffle.ll index c358509d690..41c6370e48e 100644 --- a/test/Transforms/InstCombine/vector_insertelt_shuffle.ll +++ b/test/Transforms/InstCombine/vector_insertelt_shuffle.ll @@ -54,10 +54,10 @@ define <4 x float> @bazz(<4 x float> %x, i32 %a) { ret <4 x float> %ins6 } +; Out of bounds index folds to undef define <4 x float> @bazzz(<4 x float> %x) { ; CHECK-LABEL: @bazzz( -; CHECK-NEXT: [[INS2:%.*]] = insertelement <4 x float> %x, float 2.000000e+00, i32 2 -; CHECK-NEXT: ret <4 x float> [[INS2]] +; CHECK-NEXT: ret <4 x float> ; %ins1 = insertelement<4 x float> %x, float 1.0, i32 5 %ins2 = insertelement<4 x float> %ins1, float 2.0, i32 2