Skip to content

Commit 9a24f21

Browse files
committed
[MergeFuncs] Handle ConstantRangeList attributes
Support comparison of ConstantRangeList attributes in FunctionComparator.
1 parent 2a4c74c commit 9a24f21

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

llvm/include/llvm/Transforms/Utils/FunctionComparator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ class FunctionComparator {
317317
int cmpNumbers(uint64_t L, uint64_t R) const;
318318
int cmpAligns(Align L, Align R) const;
319319
int cmpAPInts(const APInt &L, const APInt &R) const;
320+
int cmpConstantRanges(const ConstantRange &L, const ConstantRange &R) const;
320321
int cmpAPFloats(const APFloat &L, const APFloat &R) const;
321322
int cmpMem(StringRef L, StringRef R) const;
322323

llvm/lib/Transforms/Utils/FunctionComparator.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const {
8383
return 0;
8484
}
8585

86+
int FunctionComparator::cmpConstantRanges(const ConstantRange &L,
87+
const ConstantRange &R) const {
88+
if (int Res = cmpAPInts(L.getLower(), R.getLower()))
89+
return Res;
90+
return cmpAPInts(L.getUpper(), R.getUpper());
91+
}
92+
8693
int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const {
8794
// Floats are ordered first by semantics (i.e. float, double, half, etc.),
8895
// then by value interpreted as a bitstring (aka APInt).
@@ -147,12 +154,22 @@ int FunctionComparator::cmpAttrs(const AttributeList L,
147154
if (LA.getKindAsEnum() != RA.getKindAsEnum())
148155
return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
149156

150-
const ConstantRange &LCR = LA.getRange();
151-
const ConstantRange &RCR = RA.getRange();
152-
if (int Res = cmpAPInts(LCR.getLower(), RCR.getLower()))
157+
if (int Res = cmpConstantRanges(LA.getRange(), RA.getRange()))
153158
return Res;
154-
if (int Res = cmpAPInts(LCR.getUpper(), RCR.getUpper()))
159+
continue;
160+
} else if (LA.isConstantRangeListAttribute() &&
161+
RA.isConstantRangeListAttribute()) {
162+
if (LA.getKindAsEnum() != RA.getKindAsEnum())
163+
return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
164+
165+
ArrayRef<ConstantRange> CRL = LA.getValueAsConstantRangeList();
166+
ArrayRef<ConstantRange> CRR = RA.getValueAsConstantRangeList();
167+
if (int Res = cmpNumbers(CRL.size(), CRR.size()))
155168
return Res;
169+
170+
for (const auto &[L, R] : zip(CRL, CRR))
171+
if (int Res = cmpConstantRanges(L, R))
172+
return Res;
156173
continue;
157174
}
158175
if (LA < RA)
@@ -441,9 +458,7 @@ int FunctionComparator::cmpConstants(const Constant *L,
441458
if (InRangeL) {
442459
if (!InRangeR)
443460
return 1;
444-
if (int Res = cmpAPInts(InRangeL->getLower(), InRangeR->getLower()))
445-
return Res;
446-
if (int Res = cmpAPInts(InRangeL->getUpper(), InRangeR->getUpper()))
461+
if (int Res = cmpConstantRanges(*InRangeL, *InRangeR))
447462
return Res;
448463
} else if (InRangeR) {
449464
return -1;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=mergefunc < %s | FileCheck %s
3+
4+
define internal void @test1(ptr initializes((0, 1)) %p) {
5+
; CHECK-LABEL: define internal void @test1(
6+
; CHECK-SAME: ptr initializes((0, 1)) [[P:%.*]]) {
7+
; CHECK-NEXT: store i16 0, ptr [[P]], align 2
8+
; CHECK-NEXT: ret void
9+
;
10+
store i16 0, ptr %p
11+
ret void
12+
}
13+
14+
define internal void @test2(ptr initializes((0, 1)) %p) {
15+
store i16 0, ptr %p
16+
ret void
17+
}
18+
19+
define internal void @test3(ptr initializes((0, 2)) %p) {
20+
; CHECK-LABEL: define internal void @test3(
21+
; CHECK-SAME: ptr initializes((0, 2)) [[P:%.*]]) {
22+
; CHECK-NEXT: store i16 0, ptr [[P]], align 2
23+
; CHECK-NEXT: ret void
24+
;
25+
store i16 0, ptr %p
26+
ret void
27+
}
28+
29+
define internal void @test4(ptr initializes((0, 1), (2, 3)) %p) {
30+
; CHECK-LABEL: define internal void @test4(
31+
; CHECK-SAME: ptr initializes((0, 1), (2, 3)) [[P:%.*]]) {
32+
; CHECK-NEXT: store i16 0, ptr [[P]], align 2
33+
; CHECK-NEXT: ret void
34+
;
35+
store i16 0, ptr %p
36+
ret void
37+
}
38+
39+
define void @do_calls(ptr %p) {
40+
; CHECK-LABEL: define void @do_calls(
41+
; CHECK-SAME: ptr [[P:%.*]]) {
42+
; CHECK-NEXT: call void @test1(ptr [[P]])
43+
; CHECK-NEXT: call void @test1(ptr [[P]])
44+
; CHECK-NEXT: call void @test3(ptr [[P]])
45+
; CHECK-NEXT: call void @test4(ptr [[P]])
46+
; CHECK-NEXT: ret void
47+
;
48+
call void @test1(ptr %p)
49+
call void @test2(ptr %p)
50+
call void @test3(ptr %p)
51+
call void @test4(ptr %p)
52+
ret void
53+
}

0 commit comments

Comments
 (0)