diff options
| author | Sanjoy Das <sanjoy@playingwithpointers.com> | 2015-08-20 22:31:55 +0000 |
|---|---|---|
| committer | Sanjoy Das <sanjoy@playingwithpointers.com> | 2015-08-20 22:31:55 +0000 |
| commit | e472d8a57ab085370f3bfe12f9604b7566d69c92 (patch) | |
| tree | 0087abc620c8e5d81ab603ef0cb83831adafb9e8 | |
| parent | 8fa7afe683b46ab26f032497b3fab7667ff95f38 (diff) | |
| download | bcm5719-llvm-e472d8a57ab085370f3bfe12f9604b7566d69c92.tar.gz bcm5719-llvm-e472d8a57ab085370f3bfe12f9604b7566d69c92.zip | |
[InstCombine] Transform A & (L - 1) u< L --> L != 0
Summary:
This transform is never a pessimization at the IR level (since it
replaces an `icmp` with another), and has potentiall payoffs:
1. It may make the `icmp` fold away or become loop invariant.
2. It may make the `A & (L - 1)` computation dead.
This shows up in Java, in range checks generated by array accesses of
the form `a[i & (a.length - 1)]`.
Reviewers: reames, majnemer
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D12210
llvm-svn: 245635
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 13 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/icmp.ll | 20 |
2 files changed, 33 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 9ce1c3fc41b..dbf43f7769c 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3490,6 +3490,19 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { } } } + + if (BO0) { + // Transform A & (L - 1) `ult` L --> L != 0 + Value *L; + auto LSubOne = m_Add(m_Value(L), m_AllOnes()); + auto BitwiseAnd = + m_CombineOr(m_And(m_Value(), LSubOne), m_And(LSubOne, m_Value())); + + if (match(BO0, BitwiseAnd) && I.getPredicate() == ICmpInst::ICMP_ULT) { + auto *Zero = Constant::getNullValue(BO0->getType()); + return new ICmpInst(ICmpInst::ICMP_NE, L, Zero); + } + } } { Value *A, *B; diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll index edcf76d5a7d..5d97411625e 100644 --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -1603,3 +1603,23 @@ define i32 @f7(i32 %a, i32 %b) { %s = select i1 %cmp, i32 10000, i32 0 ret i32 %s } + +; CHECK: @f8( +; CHECK-NEXT: [[RESULT:%[a-z0-9]+]] = icmp ne i32 %lim, 0 +; CHECK-NEXT: ret i1 [[RESULT]] +define i1 @f8(i32 %val, i32 %lim) { + %lim.sub = add i32 %lim, -1 + %val.and = and i32 %val, %lim.sub + %r = icmp ult i32 %val.and, %lim + ret i1 %r +} + +; CHECK: @f9( +; CHECK-NEXT: [[RESULT:%[a-z0-9]+]] = icmp ne i32 %lim, 0 +; CHECK-NEXT: ret i1 [[RESULT]] +define i1 @f9(i32 %val, i32 %lim) { + %lim.sub = sub i32 %lim, 1 + %val.and = and i32 %val, %lim.sub + %r = icmp ult i32 %val.and, %lim + ret i1 %r +} |

