Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit a34cab9

Browse files
committed
[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). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217950 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2ca5f03 commit a34cab9

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,29 +1085,33 @@ Instruction *InstCombiner::FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
10851085
return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
10861086
}
10871087

1088+
bool IsNegative = false;
10881089
if (IsAShr) {
10891090
if (AP1.isNegative() != AP2.isNegative()) {
10901091
// Arithmetic shift will never change the sign.
10911092
return getConstant(false);
10921093
}
1093-
// Both the constants are negative, take their positive to calculate
1094-
// log.
1094+
// Both the constants are negative, take their positive to calculate log.
10951095
if (AP1.isNegative()) {
1096-
AP1 = -AP1;
1097-
AP2 = -AP2;
1096+
if (AP1.slt(AP2))
1097+
// Right-shifting won't increase the magnitude.
1098+
return getConstant(false);
1099+
IsNegative = true;
10981100
}
10991101
}
11001102

1101-
if (AP1.ugt(AP2)) {
1103+
if (!IsNegative && AP1.ugt(AP2))
11021104
// Right-shifting will not increase the value.
11031105
return getConstant(false);
1104-
}
11051106

11061107
// Get the distance between the highest bit that's set.
1107-
int Shift = AP2.logBase2() - AP1.logBase2();
1108+
int Shift;
1109+
if (IsNegative)
1110+
Shift = (-AP2).logBase2() - (-AP1).logBase2();
1111+
else
1112+
Shift = AP2.logBase2() - AP1.logBase2();
11081113

1109-
// Use lshr here, since we've canonicalized to +ve numbers.
1110-
if (AP1 == AP2.lshr(Shift))
1114+
if (IsAShr ? AP1 == AP2.ashr(Shift) : AP1 == AP2.lshr(Shift))
11111115
return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
11121116

11131117
// Shifting const2 will never be equal to const1.

test/Transforms/InstCombine/icmp-shr.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,18 @@ define i1 @nonexact_ashr_ne_noexactlog(i8 %a) {
675675
%cmp = icmp ne i8 %shr, -30
676676
ret i1 %cmp
677677
}
678+
679+
; Don't try to fold the entire body of function @PR20945 into a
680+
; single `ret i1 true` statement.
681+
; If %B is equal to 1, then this function would return false.
682+
; As a consequence, the instruction combiner is not allowed to fold %cmp
683+
; to 'true'. Instead, it should replace %cmp with a simpler comparison
684+
; between %B and 1.
685+
686+
; CHECK-LABEL: @PR20945(
687+
; CHECK: icmp ne i32 %B, 1
688+
define i1 @PR20945(i32 %B) {
689+
%shr = ashr i32 -9, %B
690+
%cmp = icmp ne i32 %shr, -5
691+
ret i1 %cmp
692+
}

0 commit comments

Comments
 (0)