diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp --- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp +++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp @@ -416,6 +416,11 @@ // Ignore unreachable basic blocks. if (!DT.isReachableFromEntry(&BB)) continue; + + // Fold what can be folded, to avoid Constants showing up in unexpected + // places. + MadeChange |= SimplifyInstructionsInBlock(&BB); + // Do not delete instructions under here and invalidate the iterator. // Walk the block forwards to enable simple iterative chains of transforms. // TODO: It could be more efficient to remove dead instructions diff --git a/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll b/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll --- a/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll +++ b/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll @@ -333,9 +333,7 @@ define <2 x i64> @urem_constant_op1(i64 %x) { ; CHECK-LABEL: @urem_constant_op1( -; CHECK-NEXT: [[BO_SCALAR:%.*]] = urem i64 [[X:%.*]], 2 -; CHECK-NEXT: [[BO:%.*]] = insertelement <2 x i64> , i64 [[BO_SCALAR]], i64 1 -; CHECK-NEXT: ret <2 x i64> [[BO]] +; CHECK-NEXT: ret <2 x i64> undef ; %ins = insertelement <2 x i64> undef, i64 %x, i32 1 %bo = urem <2 x i64> %ins, @@ -377,9 +375,7 @@ define <2 x i64> @srem_constant_op1(i64 %x) { ; CHECK-LABEL: @srem_constant_op1( -; CHECK-NEXT: [[BO_SCALAR:%.*]] = srem i64 [[X:%.*]], 2 -; CHECK-NEXT: [[BO:%.*]] = insertelement <2 x i64> , i64 [[BO_SCALAR]], i64 1 -; CHECK-NEXT: ret <2 x i64> [[BO]] +; CHECK-NEXT: ret <2 x i64> undef ; %ins = insertelement <2 x i64> undef, i64 %x, i32 1 %bo = srem <2 x i64> %ins, @@ -421,9 +417,7 @@ define <2 x i64> @udiv_constant_op1(i64 %x) { ; CHECK-LABEL: @udiv_constant_op1( -; CHECK-NEXT: [[BO_SCALAR:%.*]] = udiv i64 [[X:%.*]], 2 -; CHECK-NEXT: [[BO:%.*]] = insertelement <2 x i64> , i64 [[BO_SCALAR]], i64 1 -; CHECK-NEXT: ret <2 x i64> [[BO]] +; CHECK-NEXT: ret <2 x i64> undef ; %ins = insertelement <2 x i64> undef, i64 %x, i32 1 %bo = udiv <2 x i64> %ins, @@ -465,9 +459,7 @@ define <2 x i64> @sdiv_constant_op1(i64 %x) { ; CHECK-LABEL: @sdiv_constant_op1( -; CHECK-NEXT: [[BO_SCALAR:%.*]] = sdiv exact i64 [[X:%.*]], 2 -; CHECK-NEXT: [[BO:%.*]] = insertelement <2 x i64> , i64 [[BO_SCALAR]], i64 1 -; CHECK-NEXT: ret <2 x i64> [[BO]] +; CHECK-NEXT: ret <2 x i64> undef ; %ins = insertelement <2 x i64> undef, i64 %x, i32 1 %bo = sdiv exact <2 x i64> %ins, @@ -726,3 +718,15 @@ %bo = frem nnan <2 x double> %ins, ret <2 x double> %bo } + +define i32 @constant_fold_crash(<4 x i32> %x) { +; CHECK-LABEL: @constant_fold_crash( +; CHECK-NEXT: [[B:%.*]] = extractelement <4 x i32> [[X:%.*]], i32 0 +; CHECK-NEXT: [[C:%.*]] = add i32 17, [[B]] +; CHECK-NEXT: ret i32 [[C]] +; + %a = extractelement <4 x i32> , i32 1 + %b = extractelement <4 x i32> %x, i32 0 + %c = add i32 %a, %b + ret i32 %c +}