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 assert(getBitWidth() == NewRange
.getBitWidth());
43 // Handle common cases.
44 if (empty() || Ranges
.back().getUpper().slt(NewRange
.getLower())) {
45 Ranges
.push_back(NewRange
);
48 if (NewRange
.getUpper().slt(Ranges
.front().getLower())) {
49 Ranges
.insert(Ranges
.begin(), NewRange
);
53 auto LowerBound
= lower_bound(
54 Ranges
, NewRange
, [](const ConstantRange
&a
, const ConstantRange
&b
) {
55 return a
.getLower().slt(b
.getLower());
57 if (LowerBound
!= Ranges
.end() && LowerBound
->contains(NewRange
))
61 SmallVector
<ConstantRange
, 2> ExistingTail(LowerBound
, Ranges
.end());
62 Ranges
.erase(LowerBound
, Ranges
.end());
63 // Merge consecutive ranges.
64 if (!Ranges
.empty() && NewRange
.getLower().sle(Ranges
.back().getUpper())) {
65 APInt NewLower
= Ranges
.back().getLower();
67 APIntOps::smax(NewRange
.getUpper(), Ranges
.back().getUpper());
68 Ranges
.back() = ConstantRange(NewLower
, NewUpper
);
70 Ranges
.push_back(NewRange
);
72 for (auto Iter
= ExistingTail
.begin(); Iter
!= ExistingTail
.end(); Iter
++) {
73 if (Ranges
.back().getUpper().slt(Iter
->getLower())) {
74 Ranges
.push_back(*Iter
);
76 APInt NewLower
= Ranges
.back().getLower();
78 APIntOps::smax(Iter
->getUpper(), Ranges
.back().getUpper());
79 Ranges
.back() = ConstantRange(NewLower
, NewUpper
);
84 void ConstantRangeList::subtract(const ConstantRange
&SubRange
) {
85 if (SubRange
.isEmptySet() || empty())
87 assert(!SubRange
.isFullSet() && "Do not support full set");
88 assert(SubRange
.getLower().slt(SubRange
.getUpper()));
89 assert(getBitWidth() == SubRange
.getBitWidth());
90 // Handle common cases.
91 if (Ranges
.back().getUpper().sle(SubRange
.getLower()))
93 if (SubRange
.getUpper().sle(Ranges
.front().getLower()))
96 SmallVector
<ConstantRange
, 2> Result
;
97 auto AppendRangeIfNonEmpty
= [&Result
](APInt Start
, APInt End
) {
99 Result
.push_back(ConstantRange(Start
, End
));
101 for (auto &Range
: Ranges
) {
102 if (SubRange
.getUpper().sle(Range
.getLower()) ||
103 Range
.getUpper().sle(SubRange
.getLower())) {
104 // "Range" and "SubRange" do not overlap.
106 // L---U : SubRange (Case1)
107 // L---U : SubRange (Case2)
108 Result
.push_back(Range
);
109 } else if (Range
.getLower().sle(SubRange
.getLower()) &&
110 SubRange
.getUpper().sle(Range
.getUpper())) {
111 // "Range" contains "SubRange".
114 // Note that ConstantRange::contains(ConstantRange) checks unsigned,
115 // but we need signed checking here.
116 AppendRangeIfNonEmpty(Range
.getLower(), SubRange
.getLower());
117 AppendRangeIfNonEmpty(SubRange
.getUpper(), Range
.getUpper());
118 } else if (SubRange
.getLower().sle(Range
.getLower()) &&
119 Range
.getUpper().sle(SubRange
.getUpper())) {
120 // "SubRange" contains "Range".
124 } else if (Range
.getLower().sge(SubRange
.getLower()) &&
125 Range
.getLower().sle(SubRange
.getUpper())) {
126 // "Range" and "SubRange" overlap at the left.
129 AppendRangeIfNonEmpty(SubRange
.getUpper(), Range
.getUpper());
131 // "Range" and "SubRange" overlap at the right.
134 assert(SubRange
.getLower().sge(Range
.getLower()) &&
135 SubRange
.getLower().sle(Range
.getUpper()));
136 AppendRangeIfNonEmpty(Range
.getLower(), SubRange
.getLower());
144 ConstantRangeList::unionWith(const ConstantRangeList
&CRL
) const {
145 assert(getBitWidth() == CRL
.getBitWidth() &&
146 "ConstantRangeList bitwidths don't agree!");
147 // Handle common cases.
153 ConstantRangeList Result
;
155 // "PreviousRange" tracks the lowest unioned range that is being processed.
156 // Its lower is fixed and the upper may be updated over iterations.
157 ConstantRange
PreviousRange(getBitWidth(), false);
158 if (Ranges
[i
].getLower().slt(CRL
.Ranges
[j
].getLower())) {
159 PreviousRange
= Ranges
[i
++];
161 PreviousRange
= CRL
.Ranges
[j
++];
164 // Try to union "PreviousRange" and "CR". If they are disjoint, push
165 // "PreviousRange" to the result and assign it to "CR", a new union range.
166 // Otherwise, update the upper of "PreviousRange" to cover "CR". Note that,
167 // the lower of "PreviousRange" is always less or equal the lower of "CR".
168 auto UnionAndUpdateRange
= [&PreviousRange
,
169 &Result
](const ConstantRange
&CR
) {
170 if (PreviousRange
.getUpper().slt(CR
.getLower())) {
171 Result
.Ranges
.push_back(PreviousRange
);
174 PreviousRange
= ConstantRange(
175 PreviousRange
.getLower(),
176 APIntOps::smax(PreviousRange
.getUpper(), CR
.getUpper()));
179 while (i
< size() || j
< CRL
.size()) {
180 if (j
== CRL
.size() ||
181 (i
< size() && Ranges
[i
].getLower().slt(CRL
.Ranges
[j
].getLower()))) {
182 // Merge PreviousRange with this.
183 UnionAndUpdateRange(Ranges
[i
++]);
185 // Merge PreviousRange with CRL.
186 UnionAndUpdateRange(CRL
.Ranges
[j
++]);
189 Result
.Ranges
.push_back(PreviousRange
);
194 ConstantRangeList::intersectWith(const ConstantRangeList
&CRL
) const {
195 assert(getBitWidth() == CRL
.getBitWidth() &&
196 "ConstantRangeList bitwidths don't agree!");
198 // Handle common cases.
204 ConstantRangeList Result
;
206 while (i
< size() && j
< CRL
.size()) {
207 auto &Range
= this->Ranges
[i
];
208 auto &OtherRange
= CRL
.Ranges
[j
];
210 // The intersection of two Ranges is (max(lowers), min(uppers)), and it's
211 // possible that max(lowers) > min(uppers) if they don't have intersection.
212 // Add the intersection to result only if it's non-empty.
213 // To keep simple, we don't call ConstantRange::intersectWith() as it
214 // considers the complex upper wrapped case and may result two ranges,
215 // like (2, 8) && (6, 4) = {(2, 4), (6, 8)}.
216 APInt Start
= APIntOps::smax(Range
.getLower(), OtherRange
.getLower());
217 APInt End
= APIntOps::smin(Range
.getUpper(), OtherRange
.getUpper());
219 Result
.Ranges
.push_back(ConstantRange(Start
, End
));
221 // Move to the next Range in one list determined by the uppers.
222 // For example: A = {(0, 2), (4, 8)}; B = {(-2, 5), (6, 10)}
223 // We need to intersect three pairs: A0 && B0; A1 && B0; A1 && B1.
224 if (Range
.getUpper().slt(OtherRange
.getUpper()))
232 void ConstantRangeList::print(raw_ostream
&OS
) const {
233 interleaveComma(Ranges
, OS
, [&](ConstantRange CR
) {
234 OS
<< "(" << CR
.getLower() << ", " << CR
.getUpper() << ")";
238 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
239 LLVM_DUMP_METHOD
void ConstantRangeList::dump() const {