| 1 |
/* Subsets of a finite interval over Z. |
| 2 |
Copyright (C) 2024-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
/* A class that represents the union of finitely many intervals. |
| 21 |
The domain over which the intervals are defined is a finite integer |
| 22 |
interval [m_min, m_max], usually the range of some [u]intN_t. |
| 23 |
Supported operations are: |
| 24 |
- Complement w.r.t. the domain (invert) |
| 25 |
- Union (union_) |
| 26 |
- Intersection (intersect) |
| 27 |
- Difference / Setminus (minus). |
| 28 |
Ranges is closed under all operations: The result of all operations |
| 29 |
is a Ranges over the same domain. (As opposed to value-range.h which |
| 30 |
may ICE for some operations, see below). |
| 31 |
|
| 32 |
The representation is unique in the sense that when we have two |
| 33 |
Ranges A and B, then |
| 34 |
1) A == B <==> A.size == B.size && Ai == Bi for all i. |
| 35 |
|
| 36 |
The representation is normalized: |
| 37 |
2) Ai != {} ;; There are no empty intervals. |
| 38 |
3) Ai.hi < A{i+1}.lo ;; The Ai's are in increasing order and separated |
| 39 |
;; by at least one value (non-adjacent). |
| 40 |
The sub-intervals Ai are maintained as a std::vector. |
| 41 |
The computation of union and intersection scales like A.size * B.size |
| 42 |
i.e. Ranges is only eligible for GCC when size() has a fixed upper |
| 43 |
bound independent of the program being compiled (or there are other |
| 44 |
means to guarantee that the complexity is linearistic). |
| 45 |
In the context of AVR, we have size() <= 3. |
| 46 |
|
| 47 |
The reason why we don't use value-range.h's irange or int_range is that |
| 48 |
these use the integers Z as their domain, which makes computations like |
| 49 |
invert() quite nasty as they may ICE for common cases. Doing all |
| 50 |
these special cases (like one sub-interval touches the domain bounds) |
| 51 |
makes using value-range.h more laborious (and instable) than using our |
| 52 |
own mini Ranger. */ |
| 53 |
|
| 54 |
struct Ranges |
| 55 |
{ |
| 56 |
// This is good enough as it covers (un)signed SImode. |
| 57 |
using T = HOST_WIDE_INT; |
| 58 |
typedef T scalar_type; |
| 59 |
|
| 60 |
// Non-empty ranges. Empty sets are only used transiently; |
| 61 |
// Ranges.ranges[] doesn't use them. |
| 62 |
struct SubRange |
| 63 |
{ |
| 64 |
// Lower and upper bound, inclusively. |
| 65 |
T lo, hi; |
| 66 |
|
| 67 |
SubRange intersect (const SubRange &r) const |
| 68 |
{ |
| 69 |
if (lo >= r.lo && hi <= r.hi) |
| 70 |
return *this; |
| 71 |
else if (r.lo >= lo && r.hi <= hi) |
| 72 |
return r; |
| 73 |
else if (lo > r.hi || hi < r.lo) |
| 74 |
return SubRange { 1, 0 }; |
| 75 |
else |
| 76 |
return SubRange { std::max (lo, r.lo), std::min (hi, r.hi) }; |
| 77 |
} |
| 78 |
|
| 79 |
T cardinality () const |
| 80 |
{ |
| 81 |
return std::max<T> (0, hi - lo + 1); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
// Finitely many intervals over [m_min, m_max] that are normalized: |
| 86 |
// No empty sets, increasing order, separated by at least one value. |
| 87 |
T m_min, m_max; |
| 88 |
std::vector<SubRange> ranges; |
| 89 |
|
| 90 |
// Not used anywhere in Ranges; can be used elsewhere. |
| 91 |
// May be clobbered by set operations. |
| 92 |
int tag = -1; |
| 93 |
|
| 94 |
enum initial_range { EMPTY, ALL }; |
| 95 |
|
| 96 |
Ranges (T mi, T ma, initial_range ir) |
| 97 |
: m_min (mi), m_max (ma) |
| 98 |
{ |
| 99 |
if (ir == ALL) |
| 100 |
push (mi, ma); |
| 101 |
} |
| 102 |
|
| 103 |
// Domain is the range of some [u]intN_t. |
| 104 |
static Ranges NBitsRanges (int n_bits, bool unsigned_p, initial_range ir) |
| 105 |
{ |
| 106 |
T mask = ((T) 1 << n_bits) - 1; |
| 107 |
gcc_assert (mask > 0); |
| 108 |
T ma = mask >> ! unsigned_p; |
| 109 |
return Ranges (unsigned_p ? 0 : -ma - 1, ma, ir); |
| 110 |
} |
| 111 |
|
| 112 |
static void sort2 (Ranges &a, Ranges &b) |
| 113 |
{ |
| 114 |
if (a.size () && b.size ()) |
| 115 |
if (a.ranges[0].lo > b.ranges[0].lo) |
| 116 |
std::swap (a, b); |
| 117 |
} |
| 118 |
|
| 119 |
void print (FILE *file) const |
| 120 |
{ |
| 121 |
if (file) |
| 122 |
{ |
| 123 |
fprintf (file, " .tag%d=#%d={", tag, size ()); |
| 124 |
for (const auto &r : ranges) |
| 125 |
fprintf (file, "[ %ld, %ld ]", (long) r.lo, (long) r.hi); |
| 126 |
fprintf (file, "}\n"); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// The number of sub-intervals in .ranges. |
| 131 |
int size () const |
| 132 |
{ |
| 133 |
return (int) ranges.size (); |
| 134 |
} |
| 135 |
|
| 136 |
// Append [LO, HI] & [m_min, m_max] to .ranges provided the |
| 137 |
// former is non-empty. |
| 138 |
void push (T lo, T hi) |
| 139 |
{ |
| 140 |
lo = std::max (lo, m_min); |
| 141 |
hi = std::min (hi, m_max); |
| 142 |
|
| 143 |
if (lo <= hi) |
| 144 |
ranges.push_back (SubRange { lo, hi }); |
| 145 |
} |
| 146 |
|
| 147 |
// Append R to .ranges provided the former is non-empty. |
| 148 |
void push (const SubRange &r) |
| 149 |
{ |
| 150 |
push (r.lo, r.hi); |
| 151 |
} |
| 152 |
|
| 153 |
// Cardinality of the n-th interval. |
| 154 |
T cardinality (int n) const |
| 155 |
{ |
| 156 |
return n < size () ? ranges[n].cardinality () : 0; |
| 157 |
} |
| 158 |
|
| 159 |
// Check that *this is normalized: .ranges are non-empty, non-overlapping, |
| 160 |
// non-adjacent and increasing. |
| 161 |
bool check () const |
| 162 |
{ |
| 163 |
bool bad = size () && (ranges[0].lo < m_min |
| 164 |
|| ranges[size () - 1].hi > m_max); |
| 165 |
|
| 166 |
for (int n = 0; n < size (); ++n) |
| 167 |
{ |
| 168 |
bad |= ranges[n].lo > ranges[n].hi; |
| 169 |
bad |= n > 0 && ranges[n - 1].hi >= ranges[n].lo; |
| 170 |
} |
| 171 |
|
| 172 |
if (bad) |
| 173 |
print (dump_file); |
| 174 |
|
| 175 |
return ! bad; |
| 176 |
} |
| 177 |
|
| 178 |
// Intersect A and B according to (U Ai) & (U Bj) = U (Ai & Bj) |
| 179 |
// This has quadratic complexity, but also the nice property that |
| 180 |
// when A and B are normalized, then the result is too. |
| 181 |
void intersect (const Ranges &r) |
| 182 |
{ |
| 183 |
gcc_assert (m_min == r.m_min && m_max == r.m_max); |
| 184 |
|
| 185 |
if (this == &r) |
| 186 |
return; |
| 187 |
|
| 188 |
std::vector<SubRange> rs; |
| 189 |
std::swap (rs, ranges); |
| 190 |
|
| 191 |
for (const auto &a : rs) |
| 192 |
for (const auto &b : r.ranges) |
| 193 |
push (a.intersect (b)); |
| 194 |
} |
| 195 |
|
| 196 |
// Complement w.r.t. the domain [m_min, m_max]. |
| 197 |
void invert () |
| 198 |
{ |
| 199 |
std::vector<SubRange> rs; |
| 200 |
std::swap (rs, ranges); |
| 201 |
|
| 202 |
if (rs.size () == 0) |
| 203 |
push (m_min, m_max); |
| 204 |
else |
| 205 |
{ |
| 206 |
push (m_min, rs[0].lo - 1); |
| 207 |
|
| 208 |
for (size_t n = 1; n < rs.size (); ++n) |
| 209 |
push (rs[n - 1].hi + 1, rs[n].lo - 1); |
| 210 |
|
| 211 |
push (rs[rs.size () - 1].hi + 1, m_max); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// Set-minus. |
| 216 |
void minus (const Ranges &r) |
| 217 |
{ |
| 218 |
gcc_assert (m_min == r.m_min && m_max == r.m_max); |
| 219 |
|
| 220 |
Ranges sub = r; |
| 221 |
sub.invert (); |
| 222 |
intersect (sub); |
| 223 |
} |
| 224 |
|
| 225 |
// Union of sets. Not needed in avr.cc but added for completeness. |
| 226 |
// DeMorgan this for simplicity. |
| 227 |
void union_ (const Ranges &r) |
| 228 |
{ |
| 229 |
gcc_assert (m_min == r.m_min && m_max == r.m_max); |
| 230 |
|
| 231 |
if (this != &r) |
| 232 |
{ |
| 233 |
invert (); |
| 234 |
minus (r); |
| 235 |
invert (); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Get the truth Ranges for x <cmp> val. For example, |
| 240 |
// LT 3 will return [m_min, 2]. |
| 241 |
Ranges truth (rtx_code cmp, T val, bool strict = true) |
| 242 |
{ |
| 243 |
if (strict) |
| 244 |
{ |
| 245 |
if (avr_strict_signed_p (cmp)) |
| 246 |
gcc_assert (m_min == -m_max - 1); |
| 247 |
else if (avr_strict_unsigned_p (cmp)) |
| 248 |
gcc_assert (m_min == 0); |
| 249 |
|
| 250 |
gcc_assert (IN_RANGE (val, m_min, m_max)); |
| 251 |
} |
| 252 |
|
| 253 |
bool rev = cmp == NE || cmp == LTU || cmp == LT || cmp == GTU || cmp == GT; |
| 254 |
if (rev) |
| 255 |
cmp = reverse_condition (cmp); |
| 256 |
|
| 257 |
T lo = m_min; |
| 258 |
T hi = m_max; |
| 259 |
|
| 260 |
if (cmp == EQ) |
| 261 |
lo = hi = val; |
| 262 |
else if (cmp == LEU || cmp == LE) |
| 263 |
hi = val; |
| 264 |
else if (cmp == GEU || cmp == GE) |
| 265 |
lo = val; |
| 266 |
else |
| 267 |
gcc_unreachable (); |
| 268 |
|
| 269 |
Ranges rs (m_min, m_max, Ranges::EMPTY); |
| 270 |
rs.push (lo, hi); |
| 271 |
|
| 272 |
if (rev) |
| 273 |
rs.invert (); |
| 274 |
|
| 275 |
return rs; |
| 276 |
} |
| 277 |
|
| 278 |
}; // struct Ranges |