Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2457,6 +2457,39 @@ return nullptr; } +Instruction *InstCombiner::foldICmpBitCastConstant(ICmpInst &Cmp, + BitCastInst *Bitcast, + const APInt &C) { + // Folding: icmp iN X, C + // where X = bitcast (splat of iK) to iN + // and C is a splat of a K-bit pattern + // Match against a vector splat of an iK feeding into the + // bitcast. + auto Pred = Cmp.getPredicate(); + if (Bitcast->getType()->isIntegerTy() && Cmp.isEquality(Pred)) { + Value *BCIOp = Bitcast->getOperand(0); + Value *SplatVal = nullptr; + if (BCIOp->getType()->isIntOrIntVectorTy() && + match(BCIOp, + m_ShuffleVector( + m_InsertElement(m_Undef(), m_Value(SplatVal), m_Zero()), + m_Undef(), m_Zero()))) { + // Fold the icmp based on the value of C + // If C is a M copies of an iK sized bit pattern, + // then: + // => icmp iK %SplatVal, + auto *ValTy = cast(SplatVal->getType()); + if (C.isSplat(ValTy->getBitWidth())) { + Value *NewC = ConstantInt::get(ValTy, C.trunc(ValTy->getBitWidth())); + auto *Return = + cast(Builder.CreateICmp(Pred, SplatVal, NewC)); + 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 +2564,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-vecsplat.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/icmp-bc-vecsplat.ll @@ -0,0 +1,151 @@ +; 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_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_0(i8 %val) { +; CHECK-LABEL: @test_i8_eq_0( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[VAL:%.*]], 0 +; 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, 0 + ret i1 %cond +} + +define i1 @test_i8_eq_m1(i8 %val) { +; CHECK-LABEL: @test_i8_eq_m1( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[VAL:%.*]], -1 +; 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, -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_nopattern(i8 %val) { +; CHECK-LABEL: @test_i8_eq_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 eq i32 [[CAST]], 1 +; 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 eq i32 %cast, 1 + ret i1 %cond +} + +define i1 @test_i8_ne_0(i8 %val) { +; CHECK-LABEL: @test_i8_ne_0( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[VAL:%.*]], 0 +; 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, 0 + ret i1 %cond +} + +define i1 @test_i8_ne_m1(i8 %val) { +; CHECK-LABEL: @test_i8_ne_m1( +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[VAL:%.*]], -1 +; 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, -1 + 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_nopattern(i8 %val) { +; CHECK-LABEL: @test_i8_ne_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 ne i32 [[CAST]], 1 +; 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 ne i32 %cast, 1 + ret i1 %cond +} +