Skip to content

Commit 4ce5883

Browse files
authored
[SimplifyCFG] Fix value enumeration of a full range (#166379)
ConstantRange uses `[-1, -1)` as the canonical form of a full set. Therefore, the `for (APInt I = Lower; I != Upper; ++I)` idiom doesn't work for full ranges. This patch fixes the value enumeration in `ConstantComparesGatherer` to prevent missing values for full sets. Closes #166369.
1 parent a02e574 commit 4ce5883

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,10 @@ struct ConstantComparesGatherer {
778778
return false;
779779

780780
// Add all values from the range to the set
781-
for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
781+
APInt Tmp = Span.getLower();
782+
do
782783
Vals.push_back(ConstantInt::get(I->getContext(), Tmp));
784+
while (++Tmp != Span.getUpper());
783785

784786
UsedICmps++;
785787
return true;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
2+
; RUN: opt -S -passes=simplifycfg < %s | FileCheck %s
3+
4+
; Make sure we handle full-set ranges correctly.
5+
define void @test_i1() {
6+
; CHECK-LABEL: define void @test_i1() {
7+
; CHECK-NEXT: [[BB:.*:]]
8+
; CHECK-NEXT: ret void
9+
;
10+
bb:
11+
%icmp = icmp ugt i1 false, true
12+
br label %bb5
13+
14+
bb5:
15+
%select = select i1 %icmp, i1 %icmp, i1 false
16+
br i1 %select, label %bb5, label %bb6
17+
18+
bb6:
19+
ret void
20+
}
21+
22+
define void @test_i3() {
23+
; CHECK-LABEL: define void @test_i3() {
24+
; CHECK-NEXT: [[BB:.*:]]
25+
; CHECK-NEXT: ret void
26+
;
27+
bb:
28+
%icmp = icmp ugt i3 0, 7
29+
br label %bb5
30+
31+
bb5:
32+
%select = select i1 %icmp, i1 %icmp, i1 false
33+
br i1 %select, label %bb5, label %bb6
34+
35+
bb6:
36+
ret void
37+
}

0 commit comments

Comments
 (0)