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 @@ -218,6 +218,10 @@ V0->getType() != V1->getType()) return false; + // If either extract can be constant-folded, just leave it for InstCombine. + if (isa(Ext0->getOperand(0)) || isa(Ext1->getOperand(0))) + return false; + // If the scalar value 'I' is going to be re-inserted into a vector, then try // to create an extract to that same element. The extract/insert can be // reduced to a "select shuffle". diff --git a/llvm/test/Transforms/VectorCombine/X86/ignore-const.ll b/llvm/test/Transforms/VectorCombine/X86/ignore-const.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/VectorCombine/X86/ignore-const.ll @@ -0,0 +1,28 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -S -vector-combine | FileCheck %s +; +; extract,extract optimization was crashing with ConstantVector operands. +; The extracts/inserts can be folded by InstCombine and can be left alone by +; VectorCombine. + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Function Attrs: norecurse nounwind uwtable +define dso_local i32 @main() local_unnamed_addr #0 { +; CHECK-LABEL: @main( +; CHECK-NEXT: [[A:%.*]] = extractelement <8 x i32> , i32 1 +; CHECK-NEXT: [[B:%.*]] = extractelement <8 x i32> , i32 7 +; CHECK-NEXT: [[C:%.*]] = insertelement <8 x i32> , i32 [[B]], i32 2 +; CHECK-NEXT: [[D:%.*]] = extractelement <8 x i32> [[C]], i64 0 +; CHECK-NEXT: [[E:%.*]] = add i32 [[A]], [[D]] +; CHECK-NEXT: ret i32 [[E]] +; + %a = extractelement <8 x i32> , i32 1 + %b = extractelement <8 x i32> , i32 7 + %c = insertelement <8 x i32> , i32 %b, i32 2 + %d = extractelement <8 x i32> %c, i64 0 + %e = add i32 %a, %d + ret i32 %e +} +attributes #0 = { norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="core-avx2" "target-features"="+avx,+avx2,+bmi,+bmi2,+cx16,+cx8,+f16c,+fma,+fsgsbase,+fxsr,+invpcid,+lzcnt,+mmx,+movbe,+pclmul,+popcnt,+rdrnd,+sahf,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave,+xsaveopt" "unsafe-fp-math"="false" "use-soft-float"="false" }