InstSimplify: Optimize using dividend in sdiv
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 16 May 2014 16:57:04 +0000 (16:57 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 16 May 2014 16:57:04 +0000 (16:57 +0000)
Summary:
The dividend in an sdiv tells us the largest and smallest possible
results.  Use this fact to optimize comparisons against an sdiv with a
constant dividend.

Reviewers: nicholas

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D3795

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208999 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/compare.ll

index 5421a3992267882da2bdeb40afa5fb060027b3be..4cad81b1398482f8bd3d2a7cb0a4a0c7f5fc936e 100644 (file)
@@ -2020,6 +2020,10 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
       APInt NegOne = APInt::getAllOnesValue(Width);
       if (!CI2->isZero())
         Upper = NegOne.udiv(CI2->getValue()) + 1;
+    } else if (match(LHS, m_SDiv(m_ConstantInt(CI2), m_Value()))) {
+      // 'sdiv CI2, x' produces [-|CI2|, |CI2|].
+      Upper = CI2->getValue().abs() + 1;
+      Lower = (-Upper) + 1;
     } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) {
       // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2].
       APInt IntMin = APInt::getSignedMinValue(Width);
index 76cff902c96f4726caf8a6a774d074d1c4395010..0e270bbe0f97cec791ca3eefff8f50dcb507fff5 100644 (file)
@@ -817,3 +817,12 @@ define i1 @compare_always_false_ne(i16 %a) {
 ; CHECK-LABEL: @compare_always_false_ne
 ; CHECK-NEXT: ret i1 true
 }
+
+define i1 @compare_dividend(i32 %a) {
+  %div = sdiv i32 2, %a
+  %cmp = icmp eq i32 %div, 3
+  ret i1 %cmp
+
+; CHECK-LABEL: @compare_dividend
+; CHECK-NEXT: ret i1 false
+}