Skip to content

Commit

Permalink
Transform: "icmp eq (trunc (lshr(X, cst1)), cst" to "icmp (and X, mas…
Browse files Browse the repository at this point in the history
…k), 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
  • Loading branch information
lattner committed Apr 26, 2011
1 parent 5036ce4 commit 325eeb1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
46 changes: 0 additions & 46 deletions lib/Target/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConstantInt>(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<ConstantInt>(Op1),
+ Mask->getType()));
+ }
}

{


Not having this is blocking resolving PR6627.

//===---------------------------------------------------------------------===//

This code:

typedef struct {
Expand Down
25 changes: 25 additions & 0 deletions lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntegerType>(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));
}
}
}

{
Expand Down
16 changes: 16 additions & 0 deletions test/Transforms/InstCombine/icmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 325eeb1

Please sign in to comment.