From 325eeb1cd7c3247ead1513204bc9cf4b6e883653 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 26 Apr 2011 20:18:20 +0000 Subject: Transform: "icmp eq (trunc (lshr(X, cst1)), cst" to "icmp (and X, mask), cst" when X has multiple uses. This is useful for exposing secondary optimizations, but the X86 backend isn't ready for this when X has a single use. For example, this can disable load folding. This is inching towards resolving PR6627. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130238 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/README.txt | 46 ---------------------- lib/Transforms/InstCombine/InstCombineCompares.cpp | 25 ++++++++++++ test/Transforms/InstCombine/icmp.ll | 16 ++++++++ 3 files changed, 41 insertions(+), 46 deletions(-) diff --git a/lib/Target/README.txt b/lib/Target/README.txt index 99e1252dc8..ffe3fa477b 100644 --- a/lib/Target/README.txt +++ b/lib/Target/README.txt @@ -2259,52 +2259,6 @@ icmp transform. //===---------------------------------------------------------------------===// -We should optimize this: - - %tmp = load i16* %arrayidx, align 4, !tbaa !0 - %A = trunc i16 %tmp to i8 - %cmp = icmp eq i8 %A, 127 - %B.mask = and i16 %tmp, -256 - %cmp7 = icmp eq i16 %B.mask, 17664 - %or.cond = and i1 %cmp, %cmp7 - br i1 %or.cond, label %land.lhs.true9, label %if.end - -into: - - %tmp = load i16* %arrayidx, align 4, !tbaa !0 - %0 = icmp eq i16 %tmp, 17791 - br i1 %0, label %land.lhs.true9, label %if.end - -with this patch: -Index: InstCombine/InstCombineCompares.cpp -=================================================================== ---- InstCombine/InstCombineCompares.cpp (revision 129500) -+++ InstCombine/InstCombineCompares.cpp (working copy) -@@ -2506,6 +2506,18 @@ - return &I; - } - } -+ -+ // Transform "icmp eq (trunc X), cst" to "icmp (and X, mask), cst" -+ if (Op0->hasOneUse() && match(Op0, m_Trunc(m_Value(A))) && -+ isa(Op1)) { -+ APInt MaskV = APInt::getLowBitsSet(A->getType()->getPrimitiveSizeInBits(), -+ Op0->getType()->getPrimitiveSizeInBits()); -+ Value *Mask = -+ Builder->CreateAnd(A, ConstantInt::get(A->getContext(), MaskV)); -+ return new ICmpInst(I.getPredicate(), Mask, -+ ConstantExpr::getZExt(cast(Op1), -+ Mask->getType())); -+ } - } - - { - - -Not having this is blocking resolving PR6627. - -//===---------------------------------------------------------------------===// - This code: typedef struct { diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index 50571540c1..bb9b88bfe6 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2506,6 +2506,31 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { } } + // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to + // "icmp (and X, mask), cst" + uint64_t ShAmt = 0; + ConstantInt *Cst1; + if (Op0->hasOneUse() && + match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), + m_ConstantInt(ShAmt))))) && + match(Op1, m_ConstantInt(Cst1)) && + // Only do this when A has multiple uses. This is most important to do + // when it exposes other optimizations. + !A->hasOneUse()) { + unsigned ASize =cast(A->getType())->getPrimitiveSizeInBits(); + + if (ShAmt < ASize) { + APInt MaskV = + APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits()); + MaskV <<= ShAmt; + + APInt CmpV = Cst1->getValue().zext(ASize); + CmpV <<= ShAmt; + + Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV)); + return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV)); + } + } } { diff --git a/test/Transforms/InstCombine/icmp.ll b/test/Transforms/InstCombine/icmp.ll index 7ba43680a6..099540ac74 100644 --- a/test/Transforms/InstCombine/icmp.ll +++ b/test/Transforms/InstCombine/icmp.ll @@ -494,3 +494,19 @@ define i1 @test51(i32 %X, i32 %Y) { %C = icmp sgt i32 %B, -1 ret i1 %C } + +; CHECK: @test52 +; CHECK-NEXT: and i32 %x1, 16711935 +; CHECK-NEXT: icmp eq i32 {{.*}}, 4980863 +; CHECK-NEXT: ret i1 +define i1 @test52(i32 %x1) nounwind { + %conv = and i32 %x1, 255 + %cmp = icmp eq i32 %conv, 127 + %tmp2 = lshr i32 %x1, 16 + %tmp3 = trunc i32 %tmp2 to i8 + %cmp15 = icmp eq i8 %tmp3, 76 + + %A = and i1 %cmp, %cmp15 + ret i1 %A +} + -- cgit v1.2.3