[llvm] r269109 - [InstCombine] Fold icmp ugt/ult (udiv i32 C2, X), C1.
Chad Rosier via llvm-commits
llvm-commits at lists.llvm.org
Tue May 10 13:22:10 PDT 2016
Author: mcrosier
Date: Tue May 10 15:22:09 2016
New Revision: 269109
URL: http://llvm.org/viewvc/llvm-project?rev=269109&view=rev
Log:
[InstCombine] Fold icmp ugt/ult (udiv i32 C2, X), C1.
This patch adds support for two optimizations:
icmp ugt (udiv C2, X), C1 -> icmp ule X, C2/(C1+1)
icmp ult (udiv C2, X), C1 -> icmp ugt X, C2/C1
Differential Revision: http://reviews.llvm.org/D20123
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=269109&r1=269108&r2=269109&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Tue May 10 15:22:09 2016
@@ -2093,8 +2093,28 @@ Instruction *InstCombiner::visitICmpInst
break;
}
- case Instruction::SDiv:
case Instruction::UDiv:
+ if (ConstantInt *DivLHS = dyn_cast<ConstantInt>(LHSI->getOperand(0))) {
+ Value *X = LHSI->getOperand(1);
+ APInt C1 = RHS->getValue();
+ APInt C2 = DivLHS->getValue();
+ assert(C2 != 0 && "udiv 0, X should have been simplified already.");
+ // (icmp ugt (udiv C2, X), C1) -> (icmp ule X, C2/(C1+1))
+ if (ICI.getPredicate() == ICmpInst::ICMP_UGT) {
+ assert(!C1.isMaxValue() &&
+ "icmp ugt X, UINT_MAX should have been simplified already.");
+ return new ICmpInst(ICmpInst::ICMP_ULE, X,
+ ConstantInt::get(X->getType(), C2.udiv(C1 + 1)));
+ }
+ // (icmp ult (udiv C2, X), C1) -> (icmp ugt X, C2/C1)
+ if (ICI.getPredicate() == ICmpInst::ICMP_ULT) {
+ assert(C1 != 0 && "icmp ult X, 0 should have been simplified already.");
+ return new ICmpInst(ICmpInst::ICMP_UGT, X,
+ ConstantInt::get(X->getType(), C2.udiv(C1)));
+ }
+ }
+ // fall-through
+ case Instruction::SDiv:
// Fold: icmp pred ([us]div X, C1), C2 -> range test
// Fold this div into the comparison, producing a range check.
// Determine, based on the divide type, what the range is being
@@ -2105,8 +2125,6 @@ Instruction *InstCombiner::visitICmpInst
if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
DivRHS))
return R;
- // FIXME: Handle (icmp ugt (udiv i32 CI2, A), CI) and
- // (icmp ult (udiv i32 CI2, A), CI).
break;
case Instruction::Sub: {
Modified: llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll?rev=269109&r1=269108&r2=269109&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll Tue May 10 15:22:09 2016
@@ -39,3 +39,94 @@ define i1 @test5(i32 %d) {
%cmp1 = icmp ne i32 %div, 0
ret i1 %cmp1
}
+
+; CHECK-LABEL: @test6
+; CHECK: %cmp1 = icmp ult i32 %d, 6
+define i1 @test6(i32 %d) {
+ %div = udiv i32 5, %d
+ %cmp1 = icmp ugt i32 %div, 0
+ ret i1 %cmp1
+}
+
+; (icmp ugt (udiv C1, X), C1) -> false.
+; CHECK-LABEL: @test7
+; CHECK: ret i1 false
+define i1 @test7(i32 %d) {
+ %div = udiv i32 8, %d
+ %cmp1 = icmp ugt i32 %div, 8
+ ret i1 %cmp1
+}
+
+; CHECK-LABEL: @test8
+; CHECK: %cmp1 = icmp ult i32 %d, 2
+define i1 @test8(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ugt i32 %div, 3
+ ret i1 %cmp1
+}
+
+; CHECK-LABEL: @test9
+; CHECK: %cmp1 = icmp ult i32 %d, 2
+define i1 @test9(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ugt i32 %div, 2
+ ret i1 %cmp1
+}
+
+; CHECK-LABEL: @test10
+; CHECK: %cmp1 = icmp ult i32 %d, 3
+define i1 @test10(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ugt i32 %div, 1
+ ret i1 %cmp1
+}
+
+; CHECK-LABEL: @test11
+; CHECK: %cmp1 = icmp ugt i32 %d, 4
+define i1 @test11(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ult i32 %div, 1
+ ret i1 %cmp1
+}
+
+; CHECK-LABEL: @test12
+; CHECK: %cmp1 = icmp ugt i32 %d, 2
+define i1 @test12(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ult i32 %div, 2
+ ret i1 %cmp1
+}
+
+; CHECK-LABEL: @test13
+; CHECK: %cmp1 = icmp ugt i32 %d, 1
+define i1 @test13(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ult i32 %div, 3
+ ret i1 %cmp1
+}
+
+; CHECK-LABEL: @test14
+; CHECK: %cmp1 = icmp ugt i32 %d, 1
+define i1 @test14(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ult i32 %div, 4
+ ret i1 %cmp1
+}
+
+; icmp ugt X, UINT_MAX -> false.
+; CHECK-LABEL: @test15
+; CHECK: ret i1 false
+define i1 @test15(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ugt i32 %div, -1
+ ret i1 %cmp1
+}
+
+; icmp ult X, UINT_MAX -> true.
+; CHECK-LABEL: @test16
+; CHECK: ret i1 true
+define i1 @test16(i32 %d) {
+ %div = udiv i32 4, %d
+ %cmp1 = icmp ult i32 %div, -1
+ ret i1 %cmp1
+}
More information about the llvm-commits
mailing list