1 //===- ConstantRangeList.cpp - ConstantRangeList implementation -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/ConstantRangeList.h"
14 bool ConstantRangeList::isOrderedRanges(ArrayRef
<ConstantRange
> RangesRef
) {
15 if (RangesRef
.empty())
17 auto Range
= RangesRef
[0];
18 if (Range
.getLower().sge(Range
.getUpper()))
20 for (unsigned i
= 1; i
< RangesRef
.size(); i
++) {
21 auto CurRange
= RangesRef
[i
];
22 auto PreRange
= RangesRef
[i
- 1];
23 if (CurRange
.getLower().sge(CurRange
.getUpper()) ||
24 CurRange
.getLower().sle(PreRange
.getUpper()))
30 std::optional
<ConstantRangeList
>
31 ConstantRangeList::getConstantRangeList(ArrayRef
<ConstantRange
> RangesRef
) {
32 if (!isOrderedRanges(RangesRef
))
34 return ConstantRangeList(RangesRef
);
37 void ConstantRangeList::insert(const ConstantRange
&NewRange
) {
38 if (NewRange
.isEmptySet())
40 assert(!NewRange
.isFullSet() && "Do not support full set");
41 assert(NewRange
.getLower().slt(NewRange
.getUpper()));
42 // Handle common cases.
43 if (empty() || Ranges
.back().getUpper().slt(NewRange
.getLower())) {
44 Ranges
.push_back(NewRange
);
48 assert(getBitWidth() == NewRange
.getBitWidth());
50 if (NewRange
.getUpper().slt(Ranges
.front().getLower())) {
51 Ranges
.insert(Ranges
.begin(), NewRange
);
55 auto LowerBound
= lower_bound(
56 Ranges
, NewRange
, [](const ConstantRange
&a
, const ConstantRange
&b
) {
57 return a
.getLower().slt(b
.getLower());
59 if (LowerBound
!= Ranges
.end() && LowerBound
->contains(NewRange
))
63 SmallVector
<ConstantRange
, 2> ExistingTail(LowerBound
, Ranges
.end());
64 Ranges
.erase(LowerBound
, Ranges
.end());
65 // Merge consecutive ranges.
66 if (!Ranges
.empty() && NewRange
.getLower().sle(Ranges
.back().getUpper())) {
67 APInt NewLower
= Ranges
.back().getLower();
69 APIntOps::smax(NewRange
.getUpper(), Ranges
.back().getUpper());
70 Ranges
.back() = ConstantRange(NewLower
, NewUpper
);
72 Ranges
.push_back(NewRange
);
74 for (auto Iter
= ExistingTail
.begin(); Iter
!= ExistingTail
.end(); Iter
++) {
75 if (Ranges
.back().getUpper().slt(Iter
->getLower())) {
76 Ranges
.push_back(*Iter
);
78 APInt NewLower
= Ranges
.back().getLower();
80 APIntOps::smax(Iter
->getUpper(), Ranges
.back().getUpper());
81 Ranges
.back() = ConstantRange(NewLower
, NewUpper
);
86 void ConstantRangeList::subtract(const ConstantRange
&SubRange
) {
87 if (SubRange
.isEmptySet() || empty())
89 assert(!SubRange
.isFullSet() && "Do not support full set");
90 assert(SubRange
.getLower().slt(SubRange
.getUpper()));
91 assert(getBitWidth() == SubRange
.getBitWidth());
92 // Handle common cases.
93 if (Ranges
.back().getUpper().sle(SubRange
.getLower()))
95 if (SubRange
.getUpper().sle(Ranges
.front().getLower()))
98 SmallVector
<ConstantRange
, 2> Result
;
99 auto AppendRangeIfNonEmpty
= [&Result
](APInt Start
, APInt End
) {
101 Result
.push_back(ConstantRange(Start
, End
));
103 for (auto &Range
: Ranges
) {
104 if (SubRange
.getUpper().sle(Range
.getLower()) ||
105 Range
.getUpper().sle(SubRange
.getLower())) {
106 // "Range" and "SubRange" do not overlap.
108 // L---U : SubRange (Case1)
109 // L---U : SubRange (Case2)
110 Result
.push_back(Range
);
111 } else if (Range
.getLower().sle(SubRange
.getLower()) &&
112 SubRange
.getUpper().sle(Range
.getUpper())) {
113 // "Range" contains "SubRange".
116 // Note that ConstantRange::contains(ConstantRange) checks unsigned,
117 // but we need signed checking here.
118 AppendRangeIfNonEmpty(Range
.getLower(), SubRange
.getLower());
119 AppendRangeIfNonEmpty(SubRange
.getUpper(), Range
.getUpper());
120 } else if (SubRange
.getLower().sle(Range
.getLower()) &&
121 Range
.getUpper().sle(SubRange
.getUpper())) {
122 // "SubRange" contains "Range".
126 } else if (Range
.getLower().sge(SubRange
.getLower()) &&
127 Range
.getLower().sle(SubRange
.getUpper())) {
128 // "Range" and "SubRange" overlap at the left.
131 AppendRangeIfNonEmpty(SubRange
.getUpper(), Range
.getUpper());
133 // "Range" and "SubRange" overlap at the right.
136 assert(SubRange
.getLower().sge(Range
.getLower()) &&
137 SubRange
.getLower().sle(Range
.getUpper()));
138 AppendRangeIfNonEmpty(Range
.getLower(), SubRange
.getLower());
146 ConstantRangeList::unionWith(const ConstantRangeList
&CRL
) const {
147 // Handle common cases.
153 assert(getBitWidth() == CRL
.getBitWidth() &&
154 "ConstantRangeList bitwidths don't agree!");
156 ConstantRangeList Result
;
158 // "PreviousRange" tracks the lowest unioned range that is being processed.
159 // Its lower is fixed and the upper may be updated over iterations.
160 ConstantRange
PreviousRange(getBitWidth(), false);
161 if (Ranges
[i
].getLower().slt(CRL
.Ranges
[j
].getLower())) {
162 PreviousRange
= Ranges
[i
++];
164 PreviousRange
= CRL
.Ranges
[j
++];
167 // Try to union "PreviousRange" and "CR". If they are disjoint, push
168 // "PreviousRange" to the result and assign it to "CR", a new union range.
169 // Otherwise, update the upper of "PreviousRange" to cover "CR". Note that,
170 // the lower of "PreviousRange" is always less or equal the lower of "CR".
171 auto UnionAndUpdateRange
= [&PreviousRange
,
172 &Result
](const ConstantRange
&CR
) {
173 if (PreviousRange
.getUpper().slt(CR
.getLower())) {
174 Result
.Ranges
.push_back(PreviousRange
);
177 PreviousRange
= ConstantRange(
178 PreviousRange
.getLower(),
179 APIntOps::smax(PreviousRange
.getUpper(), CR
.getUpper()));
182 while (i
< size() || j
< CRL
.size()) {
183 if (j
== CRL
.size() ||
184 (i
< size() && Ranges
[i
].getLower().slt(CRL
.Ranges
[j
].getLower()))) {
185 // Merge PreviousRange with this.
186 UnionAndUpdateRange(Ranges
[i
++]);
188 // Merge PreviousRange with CRL.
189 UnionAndUpdateRange(CRL
.Ranges
[j
++]);
192 Result
.Ranges
.push_back(PreviousRange
);
197 ConstantRangeList::intersectWith(const ConstantRangeList
&CRL
) const {
198 // Handle common cases.
204 assert(getBitWidth() == CRL
.getBitWidth() &&
205 "ConstantRangeList bitwidths don't agree!");
207 ConstantRangeList Result
;
209 while (i
< size() && j
< CRL
.size()) {
210 auto &Range
= this->Ranges
[i
];
211 auto &OtherRange
= CRL
.Ranges
[j
];
213 // The intersection of two Ranges is (max(lowers), min(uppers)), and it's
214 // possible that max(lowers) > min(uppers) if they don't have intersection.
215 // Add the intersection to result only if it's non-empty.
216 // To keep simple, we don't call ConstantRange::intersectWith() as it
217 // considers the complex upper wrapped case and may result two ranges,
218 // like (2, 8) && (6, 4) = {(2, 4), (6, 8)}.
219 APInt Start
= APIntOps::smax(Range
.getLower(), OtherRange
.getLower());
220 APInt End
= APIntOps::smin(Range
.getUpper(), OtherRange
.getUpper());
222 Result
.Ranges
.push_back(ConstantRange(Start
, End
));
224 // Move to the next Range in one list determined by the uppers.
225 // For example: A = {(0, 2), (4, 8)}; B = {(-2, 5), (6, 10)}
226 // We need to intersect three pairs: A0 && B0; A1 && B0; A1 && B1.
227 if (Range
.getUpper().slt(OtherRange
.getUpper()))
235 void ConstantRangeList::print(raw_ostream
&OS
) const {
236 interleaveComma(Ranges
, OS
, [&](ConstantRange CR
) {
237 OS
<< "(" << CR
.getLower() << ", " << CR
.getUpper() << ")";
241 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
242 LLVM_DUMP_METHOD
void ConstantRangeList::dump() const {