Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2457,6 +2457,68 @@ return nullptr; } +Instruction *InstCombiner::foldICmpBitCastConstant(ICmpInst &Cmp, + BitCastInst *Bitcast, + const APInt &C) { + // Folding: icmp iN X, C + // where X = bitcast (shufflevector %vec, undef, SC)) to iN + // and C is a splat of a K-bit pattern + // and SC is a constant vector = + // Into: + // %E = extractelement %vec, i32 C' + // icmp iK %E, trunc(C) + if (Bitcast->getType()->isIntegerTy()) { + Value *BCIOp = Bitcast->getOperand(0); + Value *Vec1 = nullptr; // 1st vector arg of the shufflevector + Value *Vec2 = nullptr; // 2nd vector arg of the shufflevector + Constant *Mask = nullptr; // Mask arg of the shufflevector + if (BCIOp->getType()->isIntOrIntVectorTy() && + match(BCIOp, m_ShuffleVector(m_Value(Vec1), m_Value(Vec2), + m_Constant(Mask)))) { + // Check whether every element of Mask is the same constant + ConstantInt *Elem = nullptr; + if (isa(Mask)) // zeroinitializer + Elem = Builder.getInt32(0); + else if (auto *CV = dyn_cast(Mask)) + Elem = dyn_cast_or_null(CV->getSplatValue()); + + if (Elem) { + auto *VecTy = cast(BCIOp->getType()); + auto *ValTy = cast(VecTy->getElementType()); + auto Pred = Cmp.getPredicate(); + Value *Return = nullptr; + if (C.isSplat(ValTy->getBitWidth())) { + // Fold the icmp based on the value of C + // If C is M copies of an iK sized bit pattern, + // then: + // => %E = extractelement %vec, i32 Elem + // icmp iK %SplatVal, + + // Which vector to extract from? + Value *Vec = Vec1; + const APInt &ElemVal = Elem->getValue(); + if (ElemVal.uge(VecTy->getNumElements())) { + Vec = Vec2; + Elem = ConstantInt::get(Builder.getContext(), + ElemVal - VecTy->getNumElements()); + } + Value *Extract = Builder.CreateExtractElement(Vec, Elem); + Value *NewC = ConstantInt::get(ValTy, C.trunc(ValTy->getBitWidth())); + Return = Builder.CreateICmp(Pred, Extract, NewC); + } else if (Cmp.isEquality(Pred)) { + // If C isn't a pattern, and the pred is for (in)equality + // then we can fold the icmp into true/false. + Return = (Pred == ICmpInst::ICMP_NE) ? Builder.getTrue() + : Builder.getFalse(); + } + if (Return) + return replaceInstUsesWith(Cmp, Return); + } + } + } + return nullptr; +} + /// Try to fold integer comparisons with a constant operand: icmp Pred X, C /// where X is some kind of instruction. Instruction *InstCombiner::foldICmpInstWithConstant(ICmpInst &Cmp) { @@ -2531,6 +2593,11 @@ return I; } + if (auto *BCI = dyn_cast(Cmp.getOperand(0))) { + if (Instruction *I = foldICmpBitCastConstant(Cmp, BCI, *C)) + return I; + } + if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, *C)) return I; Index: lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- lib/Transforms/InstCombine/InstCombineInternal.h +++ lib/Transforms/InstCombine/InstCombineInternal.h @@ -734,6 +734,8 @@ Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select, ConstantInt *C); + Instruction *foldICmpBitCastConstant(ICmpInst &Cmp, BitCastInst *Bitcast, + const APInt &C); Instruction *foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc, const APInt &C); Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And, Index: test/Transforms/InstCombine/icmp-bc-vec.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/icmp-bc-vec.ll @@ -0,0 +1,251 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +define i1 @test_i1_eq_0(i1 %val) { +; CHECK-LABEL: @test_i1_eq_0( +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[VAL:%.*]], true +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i1> undef, i1 %val, i32 0 + %vec = shufflevector <4 x i1> %insvec, <4 x i1> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i1> %vec to i4 + %cond = icmp eq i4 %cast, 0 + ret i1 %cond +} + +define i1 @test_i1_eq_0_2(i1 %val) { +; CHECK-LABEL: @test_i1_eq_0_2( +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[VAL:%.*]], true +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i1> undef, i1 %val, i32 2 + %vec = shufflevector <4 x i1> %insvec, <4 x i1> undef, <4 x i32> + %cast = bitcast <4 x i1> %vec to i4 + %cond = icmp eq i4 %cast, 0 + ret i1 %cond +} + +define i1 @test_i1_eq_m1(i1 %val) { +; CHECK-LABEL: @test_i1_eq_m1( +; CHECK-NEXT: ret i1 [[VAL:%.*]] +; + %insvec = insertelement <4 x i1> undef, i1 %val, i32 0 + %vec = shufflevector <4 x i1> %insvec, <4 x i1> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i1> %vec to i4 + %cond = icmp eq i4 %cast, -1 + ret i1 %cond +} + +define i1 @test_i1_ne_0(i1 %val) { +; CHECK-LABEL: @test_i1_ne_0( +; CHECK-NEXT: ret i1 [[VAL:%.*]] +; + %insvec = insertelement <4 x i1> undef, i1 %val, i32 0 + %vec = shufflevector <4 x i1> %insvec, <4 x i1> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i1> %vec to i4 + %cond = icmp ne i4 %cast, 0 + ret i1 %cond +} + +define i1 @test_i1_ne_m1(i1 %val) { +; CHECK-LABEL: @test_i1_ne_m1( +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[VAL:%.*]], true +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i1> undef, i1 %val, i32 0 + %vec = shufflevector <4 x i1> %insvec, <4 x i1> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i1> %vec to i4 + %cond = icmp ne i4 %cast, -1 + ret i1 %cond +} + +define i1 @test_i8_eq_pattern(i8 %val) { +; CHECK-LABEL: @test_i8_eq_pattern( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[VAL:%.*]], 72 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 0 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp eq i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_eq_pattern_2(i8 %val) { +; CHECK-LABEL: @test_i8_eq_pattern_2( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[VAL:%.*]], 72 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 2 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp eq i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_eq_pattern_3(<4 x i8> %invec) { +; CHECK-LABEL: @test_i8_eq_pattern_3( +; CHECK-NEXT: [[VEC:%.*]] = shufflevector <4 x i8> [[INVEC:%.*]], <4 x i8> undef, <4 x i32> +; CHECK-NEXT: [[CAST:%.*]] = bitcast <4 x i8> [[VEC]] to i32 +; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[CAST]], 1212696648 +; CHECK-NEXT: ret i1 [[COND]] +; + %vec = shufflevector <4 x i8> %invec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp eq i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_eq_pattern_4(<4 x i8> %invec) { +; CHECK-LABEL: @test_i8_eq_pattern_4( +; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i8> [[INVEC:%.*]], i32 2 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 72 +; CHECK-NEXT: ret i1 [[TMP2]] +; + %vec = shufflevector <4 x i8> %invec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp eq i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_eq_pattern_5(<4 x i8> %invec) { +; CHECK-LABEL: @test_i8_eq_pattern_5( +; CHECK-NEXT: ret i1 false +; + %vec = shufflevector <4 x i8> %invec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp eq i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_eq_pattern_6(<4 x i8> %invec1, <4 x i8> %invec2) { +; CHECK-LABEL: @test_i8_eq_pattern_6( +; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i8> [[INVEC2:%.*]], i32 2 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 72 +; CHECK-NEXT: ret i1 [[TMP2]] +; + %vec = shufflevector <4 x i8> %invec1, <4 x i8> %invec2, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp eq i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_eq_nopattern(i8 %val) { +; CHECK-LABEL: @test_i8_eq_nopattern( +; CHECK-NEXT: ret i1 false +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 0 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp eq i32 %cast, 1212696647 + ret i1 %cond +} + +define i1 @test_i8_ne_pattern(i8 %val) { +; CHECK-LABEL: @test_i8_ne_pattern( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[VAL:%.*]], 72 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 0 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ne i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_ne_pattern_2(i8 %val) { +; CHECK-LABEL: @test_i8_ne_pattern_2( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[VAL:%.*]], 72 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 2 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ne i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_ne_pattern_3(<4 x i8> %invec) { +; CHECK-LABEL: @test_i8_ne_pattern_3( +; CHECK-NEXT: [[VEC:%.*]] = shufflevector <4 x i8> [[INVEC:%.*]], <4 x i8> undef, <4 x i32> +; CHECK-NEXT: [[CAST:%.*]] = bitcast <4 x i8> [[VEC]] to i32 +; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[CAST]], 1212696648 +; CHECK-NEXT: ret i1 [[COND]] +; + %vec = shufflevector <4 x i8> %invec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ne i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_ne_pattern_4(<4 x i8> %invec) { +; CHECK-LABEL: @test_i8_ne_pattern_4( +; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i8> [[INVEC:%.*]], i32 2 +; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP1]], 72 +; CHECK-NEXT: ret i1 [[TMP2]] +; + %vec = shufflevector <4 x i8> %invec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ne i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_ne_pattern_5(<4 x i8> %invec) { +; CHECK-LABEL: @test_i8_ne_pattern_5( +; CHECK-NEXT: ret i1 true +; + %vec = shufflevector <4 x i8> %invec, <4 x i8> undef, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ne i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_ne_pattern_6(<4 x i8> %invec1, <4 x i8> %invec2) { +; CHECK-LABEL: @test_i8_ne_pattern_6( +; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i8> [[INVEC2:%.*]], i32 2 +; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP1]], 72 +; CHECK-NEXT: ret i1 [[TMP2]] +; + %vec = shufflevector <4 x i8> %invec1, <4 x i8> %invec2, <4 x i32> + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ne i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_ne_nopattern(i8 %val) { +; CHECK-LABEL: @test_i8_ne_nopattern( +; CHECK-NEXT: ret i1 true +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 0 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ne i32 %cast, 1212696647 + ret i1 %cond +} + +define i1 @test_i8_ult_pattern(i8 %val) { +; CHECK-LABEL: @test_i8_ult_pattern( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i8 [[VAL:%.*]], 72 +; CHECK-NEXT: ret i1 [[TMP1]] +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 0 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ult i32 %cast, 1212696648 + ret i1 %cond +} + +define i1 @test_i8_ult_nopattern(i8 %val) { +; CHECK-LABEL: @test_i8_ult_nopattern( +; CHECK-NEXT: [[INSVEC:%.*]] = insertelement <4 x i8> undef, i8 [[VAL:%.*]], i32 0 +; CHECK-NEXT: [[VEC:%.*]] = shufflevector <4 x i8> [[INSVEC]], <4 x i8> undef, <4 x i32> zeroinitializer +; CHECK-NEXT: [[CAST:%.*]] = bitcast <4 x i8> [[VEC]] to i32 +; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[CAST]], 1212696647 +; CHECK-NEXT: ret i1 [[COND]] +; + %insvec = insertelement <4 x i8> undef, i8 %val, i32 0 + %vec = shufflevector <4 x i8> %insvec, <4 x i8> undef, <4 x i32> zeroinitializer + %cast = bitcast <4 x i8> %vec to i32 + %cond = icmp ult i32 %cast, 1212696647 + ret i1 %cond +}