[llvm] r217950 - [InstCombine] Fix wrong folding of constant comparison involving ahsr and negative quantities (PR20945).
Andrea Di Biagio
Andrea_DiBiagio at sn.scee.net
Wed Sep 17 04:32:32 PDT 2014
Author: adibiagio
Date: Wed Sep 17 06:32:31 2014
New Revision: 217950
URL: http://llvm.org/viewvc/llvm-project?rev=217950&view=rev
Log:
[InstCombine] Fix wrong folding of constant comparison involving ahsr and negative quantities (PR20945).
Example:
define i1 @foo(i32 %a) {
%shr = ashr i32 -9, %a
%cmp = icmp ne i32 %shr, -5
ret i1 %cmp
}
Before this fix, the instruction combiner wrongly thought that %shr
could have never been equal to -5. Therefore, %cmp was always folded to 'true'.
However, when %a is equal to 1, then %cmp evaluates to 'false'. Therefore,
in this example, it is not valid to fold %cmp to 'true'.
The problem was only affecting the case where the comparison was between
negative quantities where one of the quantities was obtained from arithmetic
shift of a negative constant.
This patch fixes the problem with the wrong folding (fixes PR20945).
With this patch, the 'icmp' from the example is now simplified to a
comparison between %a and 1. This still allows us to get rid of the arithmetic
shift (%shr).
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/trunk/test/Transforms/InstCombine/icmp-shr.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=217950&r1=217949&r2=217950&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Wed Sep 17 06:32:31 2014
@@ -1085,29 +1085,33 @@ Instruction *InstCombiner::FoldICmpCstSh
return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
}
+ bool IsNegative = false;
if (IsAShr) {
if (AP1.isNegative() != AP2.isNegative()) {
// Arithmetic shift will never change the sign.
return getConstant(false);
}
- // Both the constants are negative, take their positive to calculate
- // log.
+ // Both the constants are negative, take their positive to calculate log.
if (AP1.isNegative()) {
- AP1 = -AP1;
- AP2 = -AP2;
+ if (AP1.slt(AP2))
+ // Right-shifting won't increase the magnitude.
+ return getConstant(false);
+ IsNegative = true;
}
}
- if (AP1.ugt(AP2)) {
+ if (!IsNegative && AP1.ugt(AP2))
// Right-shifting will not increase the value.
return getConstant(false);
- }
// Get the distance between the highest bit that's set.
- int Shift = AP2.logBase2() - AP1.logBase2();
+ int Shift;
+ if (IsNegative)
+ Shift = (-AP2).logBase2() - (-AP1).logBase2();
+ else
+ Shift = AP2.logBase2() - AP1.logBase2();
- // Use lshr here, since we've canonicalized to +ve numbers.
- if (AP1 == AP2.lshr(Shift))
+ if (IsAShr ? AP1 == AP2.ashr(Shift) : AP1 == AP2.lshr(Shift))
return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
// Shifting const2 will never be equal to const1.
Modified: llvm/trunk/test/Transforms/InstCombine/icmp-shr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp-shr.ll?rev=217950&r1=217949&r2=217950&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp-shr.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp-shr.ll Wed Sep 17 06:32:31 2014
@@ -675,3 +675,18 @@ define i1 @nonexact_ashr_ne_noexactlog(i
%cmp = icmp ne i8 %shr, -30
ret i1 %cmp
}
+
+; Don't try to fold the entire body of function @PR20945 into a
+; single `ret i1 true` statement.
+; If %B is equal to 1, then this function would return false.
+; As a consequence, the instruction combiner is not allowed to fold %cmp
+; to 'true'. Instead, it should replace %cmp with a simpler comparison
+; between %B and 1.
+
+; CHECK-LABEL: @PR20945(
+; CHECK: icmp ne i32 %B, 1
+define i1 @PR20945(i32 %B) {
+ %shr = ashr i32 -9, %B
+ %cmp = icmp ne i32 %shr, -5
+ ret i1 %cmp
+}
More information about the llvm-commits
mailing list