[llvm] r209000 - InstSimplify: Improve handling of ashr/lshr
David Majnemer
david.majnemer at gmail.com
Fri May 16 10:14:03 PDT 2014
Author: majnemer
Date: Fri May 16 12:14:03 2014
New Revision: 209000
URL: http://llvm.org/viewvc/llvm-project?rev=209000&view=rev
Log:
InstSimplify: Improve handling of ashr/lshr
Summary:
Analyze the range of values produced by ashr/lshr cst, %V when it is
being used in an icmp.
Reviewers: nicholas
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D3774
Modified:
llvm/trunk/lib/Analysis/InstructionSimplify.cpp
llvm/trunk/test/Transforms/InstSimplify/compare.ll
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=209000&r1=208999&r2=209000&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Fri May 16 12:14:03 2014
@@ -2001,7 +2001,7 @@ static Value *SimplifyICmpInst(unsigned
// Many binary operators with constant RHS have easy to compute constant
// range. Use them to check whether the comparison is a tautology.
- uint32_t Width = CI->getBitWidth();
+ unsigned Width = CI->getBitWidth();
APInt Lower = APInt(Width, 0);
APInt Upper = APInt(Width, 0);
ConstantInt *CI2;
@@ -2038,6 +2038,13 @@ static Value *SimplifyICmpInst(unsigned
APInt NegOne = APInt::getAllOnesValue(Width);
if (CI2->getValue().ult(Width))
Upper = NegOne.lshr(CI2->getValue()) + 1;
+ } else if (match(LHS, m_LShr(m_ConstantInt(CI2), m_Value()))) {
+ // 'lshr CI2, x' produces [CI2 >> (Width-1), CI2].
+ unsigned ShiftAmount = Width - 1;
+ if (!CI2->isZero() && cast<BinaryOperator>(LHS)->isExact())
+ ShiftAmount = CI2->getValue().countTrailingZeros();
+ Lower = CI2->getValue().lshr(ShiftAmount);
+ Upper = CI2->getValue() + 1;
} else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) {
// 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2].
APInt IntMin = APInt::getSignedMinValue(Width);
@@ -2046,6 +2053,19 @@ static Value *SimplifyICmpInst(unsigned
Lower = IntMin.ashr(CI2->getValue());
Upper = IntMax.ashr(CI2->getValue()) + 1;
}
+ } else if (match(LHS, m_AShr(m_ConstantInt(CI2), m_Value()))) {
+ unsigned ShiftAmount = Width - 1;
+ if (!CI2->isZero() && cast<BinaryOperator>(LHS)->isExact())
+ ShiftAmount = CI2->getValue().countTrailingZeros();
+ if (CI2->isNegative()) {
+ // 'ashr CI2, x' produces [CI2, CI2 >> (Width-1)]
+ Lower = CI2->getValue();
+ Upper = CI2->getValue().ashr(ShiftAmount) + 1;
+ } else {
+ // 'ashr CI2, x' produces [CI2 >> (Width-1), CI2]
+ Lower = CI2->getValue().ashr(ShiftAmount);
+ Upper = CI2->getValue() + 1;
+ }
} else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) {
// 'or x, CI2' produces [CI2, UINT_MAX].
Lower = CI2->getValue();
Modified: llvm/trunk/test/Transforms/InstSimplify/compare.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/compare.ll?rev=209000&r1=208999&r2=209000&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/compare.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/compare.ll Fri May 16 12:14:03 2014
@@ -826,3 +826,43 @@ define i1 @compare_dividend(i32 %a) {
; CHECK-LABEL: @compare_dividend
; CHECK-NEXT: ret i1 false
}
+
+define i1 @lshr_ugt_false(i32 %a) {
+ %shr = lshr i32 1, %a
+ %cmp = icmp ugt i32 %shr, 1
+ ret i1 %cmp
+; CHECK-LABEL: @lshr_ugt_false
+; CHECK-NEXT: ret i1 false
+}
+
+define i1 @exact_lshr_ugt_false(i32 %a) {
+ %shr = lshr exact i32 30, %a
+ %cmp = icmp ult i32 %shr, 15
+ ret i1 %cmp
+; CHECK-LABEL: @exact_lshr_ugt_false
+; CHECK-NEXT: ret i1 false
+}
+
+define i1 @lshr_sgt_false(i32 %a) {
+ %shr = lshr i32 1, %a
+ %cmp = icmp sgt i32 %shr, 1
+ ret i1 %cmp
+; CHECK-LABEL: @lshr_sgt_false
+; CHECK-NEXT: ret i1 false
+}
+
+define i1 @ashr_sgt_false(i32 %a) {
+ %shr = ashr i32 -30, %a
+ %cmp = icmp sgt i32 %shr, -1
+ ret i1 %cmp
+; CHECK-LABEL: @ashr_sgt_false
+; CHECK-NEXT: ret i1 false
+}
+
+define i1 @exact_ashr_sgt_false(i32 %a) {
+ %shr = ashr exact i32 -30, %a
+ %cmp = icmp sgt i32 %shr, -15
+ ret i1 %cmp
+; CHECK-LABEL: @exact_ashr_sgt_false
+; CHECK-NEXT: ret i1 false
+}
More information about the llvm-commits
mailing list