diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2018-08-16 12:52:17 +0000 |
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2018-08-16 12:52:17 +0000 |
| commit | 039f556f44e366e70d174be1026080bdb6c23995 (patch) | |
| tree | 85d988b5887f8ec9a763fe1c7bd246155c7b28a4 | |
| parent | 61cd68700927f4811e0803a135104e9c96b0d679 (diff) | |
| download | bcm5719-llvm-039f556f44e366e70d174be1026080bdb6c23995.tar.gz bcm5719-llvm-039f556f44e366e70d174be1026080bdb6c23995.zip | |
[InstCombine] move vector compare before same-shuffled ops
This is a step towards fixing PR37463:
https://bugs.llvm.org/show_bug.cgi?id=37463
llvm-svn: 339875
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 28 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/icmp-vec.ll | 19 |
2 files changed, 36 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 725e1c87e6c..a90a46dab42 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4598,6 +4598,26 @@ static Instruction *canonicalizeICmpBool(ICmpInst &I, } } +static Instruction *foldVectorCmp(CmpInst &Cmp, + InstCombiner::BuilderTy &Builder) { + // If both arguments of the cmp are shuffles that use the same mask and + // shuffle within a single vector, move the shuffle after the cmp. + Value *LHS = Cmp.getOperand(0), *RHS = Cmp.getOperand(1); + Value *V1, *V2; + Constant *M; + if (match(LHS, m_ShuffleVector(m_Value(V1), m_Undef(), m_Constant(M))) && + match(RHS, m_ShuffleVector(m_Value(V2), m_Undef(), m_Specific(M))) && + V1->getType() == V2->getType() && + (LHS->hasOneUse() || RHS->hasOneUse())) { + // cmp (shuffle V1, M), (shuffle V2, M) --> shuffle (cmp V1, V2), M + CmpInst::Predicate P = Cmp.getPredicate(); + Value *NewCmp = isa<ICmpInst>(Cmp) ? Builder.CreateICmp(P, V1, V2) + : Builder.CreateFCmp(P, V1, V2); + return new ShuffleVectorInst(NewCmp, UndefValue::get(NewCmp->getType()), M); + } + return nullptr; +} + Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { bool Changed = false; Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); @@ -4867,6 +4887,10 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { return foldICmpAddOpConst(X, Cst, I.getSwappedPredicate()); } + if (I.getType()->isVectorTy()) + if (Instruction *Res = foldVectorCmp(I, Builder)) + return Res; + return Changed ? &I : nullptr; } @@ -5301,5 +5325,9 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) return new FCmpInst(Pred, LHSExt->getOperand(0), RHSExt->getOperand(0)); + if (I.getType()->isVectorTy()) + if (Instruction *Res = foldVectorCmp(I, Builder)) + return Res; + return Changed ? &I : nullptr; } diff --git a/llvm/test/Transforms/InstCombine/icmp-vec.ll b/llvm/test/Transforms/InstCombine/icmp-vec.ll index 7c4fc8a8d8c..cb83db5f8fa 100644 --- a/llvm/test/Transforms/InstCombine/icmp-vec.ll +++ b/llvm/test/Transforms/InstCombine/icmp-vec.ll @@ -199,14 +199,12 @@ define <2 x i1> @PR27786(<2 x i8> %a) { ret <2 x i1> %cmp } -; FIXME: ; This is similar to a transform for shuffled binops: compare first, shuffle after. define <4 x i1> @same_shuffle_inputs_icmp(<4 x i8> %x, <4 x i8> %y) { ; CHECK-LABEL: @same_shuffle_inputs_icmp( -; CHECK-NEXT: [[SHUFX:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> undef, <4 x i32> <i32 3, i32 3, i32 2, i32 0> -; CHECK-NEXT: [[SHUFY:%.*]] = shufflevector <4 x i8> [[Y:%.*]], <4 x i8> undef, <4 x i32> <i32 3, i32 3, i32 2, i32 0> -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <4 x i8> [[SHUFX]], [[SHUFY]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i8> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = shufflevector <4 x i1> [[TMP1]], <4 x i1> undef, <4 x i32> <i32 3, i32 3, i32 2, i32 0> ; CHECK-NEXT: ret <4 x i1> [[CMP]] ; %shufx = shufflevector <4 x i8> %x, <4 x i8> undef, <4 x i32> < i32 3, i32 3, i32 2, i32 0 > @@ -219,9 +217,8 @@ define <4 x i1> @same_shuffle_inputs_icmp(<4 x i8> %x, <4 x i8> %y) { define <5 x i1> @same_shuffle_inputs_fcmp(<4 x float> %x, <4 x float> %y) { ; CHECK-LABEL: @same_shuffle_inputs_fcmp( -; CHECK-NEXT: [[SHUFX:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> undef, <5 x i32> <i32 0, i32 1, i32 3, i32 2, i32 0> -; CHECK-NEXT: [[SHUFY:%.*]] = shufflevector <4 x float> [[Y:%.*]], <4 x float> undef, <5 x i32> <i32 0, i32 1, i32 3, i32 2, i32 0> -; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq <5 x float> [[SHUFX]], [[SHUFY]] +; CHECK-NEXT: [[TMP1:%.*]] = fcmp oeq <4 x float> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = shufflevector <4 x i1> [[TMP1]], <4 x i1> undef, <5 x i32> <i32 0, i32 1, i32 3, i32 2, i32 0> ; CHECK-NEXT: ret <5 x i1> [[CMP]] ; %shufx = shufflevector <4 x float> %x, <4 x float> undef, <5 x i32> < i32 0, i32 1, i32 3, i32 2, i32 0 > @@ -235,8 +232,8 @@ declare void @use_v4i8(<4 x i8>) define <4 x i1> @same_shuffle_inputs_icmp_extra_use1(<4 x i8> %x, <4 x i8> %y) { ; CHECK-LABEL: @same_shuffle_inputs_icmp_extra_use1( ; CHECK-NEXT: [[SHUFX:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> undef, <4 x i32> <i32 3, i32 3, i32 3, i32 3> -; CHECK-NEXT: [[SHUFY:%.*]] = shufflevector <4 x i8> [[Y:%.*]], <4 x i8> undef, <4 x i32> <i32 3, i32 3, i32 3, i32 3> -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <4 x i8> [[SHUFX]], [[SHUFY]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt <4 x i8> [[X]], [[Y:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = shufflevector <4 x i1> [[TMP1]], <4 x i1> undef, <4 x i32> <i32 3, i32 3, i32 3, i32 3> ; CHECK-NEXT: call void @use_v4i8(<4 x i8> [[SHUFX]]) ; CHECK-NEXT: ret <4 x i1> [[CMP]] ; @@ -251,9 +248,9 @@ declare void @use_v2i8(<2 x i8>) define <2 x i1> @same_shuffle_inputs_icmp_extra_use2(<4 x i8> %x, <4 x i8> %y) { ; CHECK-LABEL: @same_shuffle_inputs_icmp_extra_use2( -; CHECK-NEXT: [[SHUFX:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> undef, <2 x i32> <i32 3, i32 2> ; CHECK-NEXT: [[SHUFY:%.*]] = shufflevector <4 x i8> [[Y:%.*]], <4 x i8> undef, <2 x i32> <i32 3, i32 2> -; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[SHUFX]], [[SHUFY]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <4 x i8> [[X:%.*]], [[Y]] +; CHECK-NEXT: [[CMP:%.*]] = shufflevector <4 x i1> [[TMP1]], <4 x i1> undef, <2 x i32> <i32 3, i32 2> ; CHECK-NEXT: call void @use_v2i8(<2 x i8> [[SHUFY]]) ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; |

