Skip to content

Commit 546f627

Browse files
committed
[SCCP] Handle some negative cases.
1 parent 6d1d962 commit 546f627

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static Value *simplifyInstruction(SCCPSolver &Solver,
307307
// Match icmp eq/ne X & NegPow2, C
308308
if (ICmp->isEquality()) {
309309
const APInt *Mask;
310-
if (match(LHS, m_And(m_Value(X), m_NegatedPower2(Mask))) &&
310+
if (match(LHS, m_OneUse(m_And(m_Value(X), m_NegatedPower2(Mask)))) &&
311311
RHSC->countr_zero() >= Mask->countr_zero()) {
312312
ConstantRange CR(*RHSC, *RHSC - *Mask);
313313
return Pred == ICmpInst::ICMP_EQ ? CR : CR.inverse();
@@ -318,6 +318,8 @@ static Value *simplifyInstruction(SCCPSolver &Solver,
318318

319319
if (auto CR = MatchTwoInstructionExactRangeCheck()) {
320320
ConstantRange LRange = GetRange(X);
321+
if (LRange.isFullSet())
322+
return nullptr;
321323
if (auto NewCR = CR->exactUnionWith(LRange.inverse())) {
322324
ICmpInst::Predicate Pred;
323325
APInt RHS;

llvm/test/Transforms/SCCP/conditions-ranges-with-undef.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ define void @val_undef_range() {
4040
; CHECK-LABEL: @val_undef_range(
4141
; CHECK-NEXT: entry:
4242
; CHECK-NEXT: [[A:%.*]] = add nuw nsw i32 undef, 0
43-
; CHECK-NEXT: [[BC_1:%.*]] = icmp ult i32 undef, 127
43+
; CHECK-NEXT: [[BC_1:%.*]] = icmp ult i32 [[A]], 127
4444
; CHECK-NEXT: br i1 [[BC_1]], label [[TRUE:%.*]], label [[FALSE:%.*]]
4545
; CHECK: true:
4646
; CHECK-NEXT: call void @use(i1 false)

llvm/test/Transforms/SCCP/relax-range-checks.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,31 @@ define i1 @relax_range_check_not_profitable(i8 range(i8 0, 6) %x) {
4848
%ret = icmp ult i8 %add, 2
4949
ret i1 %ret
5050
}
51+
52+
define i1 @relax_range_check_unknown_range(i64 %x) {
53+
; CHECK-LABEL: define i1 @relax_range_check_unknown_range(
54+
; CHECK-SAME: i64 [[X:%.*]]) {
55+
; CHECK-NEXT: [[AND:%.*]] = and i64 [[X]], -67108864
56+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i64 [[AND]], 0
57+
; CHECK-NEXT: ret i1 [[TMP1]]
58+
;
59+
%and = and i64 %x, -67108864
60+
%test = icmp eq i64 %and, 0
61+
ret i1 %test
62+
}
63+
64+
define i1 @relax_range_check_highbits_check_multiuse(i8 range(i8 2, 0) %x) {
65+
; CHECK-LABEL: define i1 @relax_range_check_highbits_check_multiuse(
66+
; CHECK-SAME: i8 range(i8 2, 0) [[X:%.*]]) {
67+
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], -2
68+
; CHECK-NEXT: call void @use(i8 [[AND]])
69+
; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[AND]], 2
70+
; CHECK-NEXT: ret i1 [[RET]]
71+
;
72+
%and = and i8 %x, -2
73+
call void @use(i8 %and)
74+
%ret = icmp eq i8 %and, 2
75+
ret i1 %ret
76+
}
77+
78+
declare void @use(i8)

0 commit comments

Comments
 (0)