1 //===- ConstantRange.cpp - ConstantRange 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 // Represent a range of possible values that may occur when the program is run
10 // for an integral value. This keeps track of a lower and upper bound for the
11 // constant, which MAY wrap around the end of the numeric range. To do this, it
12 // keeps track of a [lower, upper) bound, which specifies an interval just like
13 // STL iterators. When used with boolean values, the following are important
14 // ranges (other integral ranges use min/max values for special range values):
16 // [F, F) = {} = Empty set
19 // [T, T) = {F, T} = Full set
21 //===----------------------------------------------------------------------===//
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/IR/ConstantRange.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/Metadata.h"
31 #include "llvm/IR/Operator.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/KnownBits.h"
36 #include "llvm/Support/raw_ostream.h"
44 ConstantRange::ConstantRange(uint32_t BitWidth
, bool Full
)
45 : Lower(Full
? APInt::getMaxValue(BitWidth
) : APInt::getMinValue(BitWidth
)),
48 ConstantRange::ConstantRange(APInt V
)
49 : Lower(std::move(V
)), Upper(Lower
+ 1) {}
51 ConstantRange::ConstantRange(APInt L
, APInt U
)
52 : Lower(std::move(L
)), Upper(std::move(U
)) {
53 assert(Lower
.getBitWidth() == Upper
.getBitWidth() &&
54 "ConstantRange with unequal bit widths");
55 assert((Lower
!= Upper
|| (Lower
.isMaxValue() || Lower
.isMinValue())) &&
56 "Lower == Upper, but they aren't min or max value!");
59 ConstantRange
ConstantRange::fromKnownBits(const KnownBits
&Known
,
61 assert(!Known
.hasConflict() && "Expected valid KnownBits");
63 if (Known
.isUnknown())
64 return getFull(Known
.getBitWidth());
66 // For unsigned ranges, or signed ranges with known sign bit, create a simple
67 // range between the smallest and largest possible value.
68 if (!IsSigned
|| Known
.isNegative() || Known
.isNonNegative())
69 return ConstantRange(Known
.getMinValue(), Known
.getMaxValue() + 1);
71 // If we don't know the sign bit, pick the lower bound as a negative number
72 // and the upper bound as a non-negative one.
73 APInt Lower
= Known
.getMinValue(), Upper
= Known
.getMaxValue();
76 return ConstantRange(Lower
, Upper
+ 1);
79 KnownBits
ConstantRange::toKnownBits() const {
80 // TODO: We could return conflicting known bits here, but consumers are
81 // likely not prepared for that.
83 return KnownBits(getBitWidth());
85 // We can only retain the top bits that are the same between min and max.
86 APInt Min
= getUnsignedMin();
87 APInt Max
= getUnsignedMax();
88 KnownBits Known
= KnownBits::makeConstant(Min
);
89 if (std::optional
<unsigned> DifferentBit
=
90 APIntOps::GetMostSignificantDifferentBit(Min
, Max
)) {
91 Known
.Zero
.clearLowBits(*DifferentBit
+ 1);
92 Known
.One
.clearLowBits(*DifferentBit
+ 1);
97 ConstantRange
ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred
,
98 const ConstantRange
&CR
) {
102 uint32_t W
= CR
.getBitWidth();
105 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
106 case CmpInst::ICMP_EQ
:
108 case CmpInst::ICMP_NE
:
109 if (CR
.isSingleElement())
110 return ConstantRange(CR
.getUpper(), CR
.getLower());
112 case CmpInst::ICMP_ULT
: {
113 APInt
UMax(CR
.getUnsignedMax());
114 if (UMax
.isMinValue())
116 return ConstantRange(APInt::getMinValue(W
), std::move(UMax
));
118 case CmpInst::ICMP_SLT
: {
119 APInt
SMax(CR
.getSignedMax());
120 if (SMax
.isMinSignedValue())
122 return ConstantRange(APInt::getSignedMinValue(W
), std::move(SMax
));
124 case CmpInst::ICMP_ULE
:
125 return getNonEmpty(APInt::getMinValue(W
), CR
.getUnsignedMax() + 1);
126 case CmpInst::ICMP_SLE
:
127 return getNonEmpty(APInt::getSignedMinValue(W
), CR
.getSignedMax() + 1);
128 case CmpInst::ICMP_UGT
: {
129 APInt
UMin(CR
.getUnsignedMin());
130 if (UMin
.isMaxValue())
132 return ConstantRange(std::move(UMin
) + 1, APInt::getZero(W
));
134 case CmpInst::ICMP_SGT
: {
135 APInt
SMin(CR
.getSignedMin());
136 if (SMin
.isMaxSignedValue())
138 return ConstantRange(std::move(SMin
) + 1, APInt::getSignedMinValue(W
));
140 case CmpInst::ICMP_UGE
:
141 return getNonEmpty(CR
.getUnsignedMin(), APInt::getZero(W
));
142 case CmpInst::ICMP_SGE
:
143 return getNonEmpty(CR
.getSignedMin(), APInt::getSignedMinValue(W
));
147 ConstantRange
ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred
,
148 const ConstantRange
&CR
) {
149 // Follows from De-Morgan's laws:
151 // ~(~A union ~B) == A intersect B.
153 return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred
), CR
)
157 ConstantRange
ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred
,
159 // Computes the exact range that is equal to both the constant ranges returned
160 // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true
161 // when RHS is a singleton such as an APInt and so the assert is valid.
162 // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion
163 // returns [0,4) but makeSatisfyICmpRegion returns [0,2).
165 assert(makeAllowedICmpRegion(Pred
, C
) == makeSatisfyingICmpRegion(Pred
, C
));
166 return makeAllowedICmpRegion(Pred
, C
);
169 bool ConstantRange::areInsensitiveToSignednessOfICmpPredicate(
170 const ConstantRange
&CR1
, const ConstantRange
&CR2
) {
171 if (CR1
.isEmptySet() || CR2
.isEmptySet())
174 return (CR1
.isAllNonNegative() && CR2
.isAllNonNegative()) ||
175 (CR1
.isAllNegative() && CR2
.isAllNegative());
178 bool ConstantRange::areInsensitiveToSignednessOfInvertedICmpPredicate(
179 const ConstantRange
&CR1
, const ConstantRange
&CR2
) {
180 if (CR1
.isEmptySet() || CR2
.isEmptySet())
183 return (CR1
.isAllNonNegative() && CR2
.isAllNegative()) ||
184 (CR1
.isAllNegative() && CR2
.isAllNonNegative());
187 CmpInst::Predicate
ConstantRange::getEquivalentPredWithFlippedSignedness(
188 CmpInst::Predicate Pred
, const ConstantRange
&CR1
,
189 const ConstantRange
&CR2
) {
190 assert(CmpInst::isIntPredicate(Pred
) && CmpInst::isRelational(Pred
) &&
191 "Only for relational integer predicates!");
193 CmpInst::Predicate FlippedSignednessPred
=
194 CmpInst::getFlippedSignednessPredicate(Pred
);
196 if (areInsensitiveToSignednessOfICmpPredicate(CR1
, CR2
))
197 return FlippedSignednessPred
;
199 if (areInsensitiveToSignednessOfInvertedICmpPredicate(CR1
, CR2
))
200 return CmpInst::getInversePredicate(FlippedSignednessPred
);
202 return CmpInst::Predicate::BAD_ICMP_PREDICATE
;
205 void ConstantRange::getEquivalentICmp(CmpInst::Predicate
&Pred
,
206 APInt
&RHS
, APInt
&Offset
) const {
207 Offset
= APInt(getBitWidth(), 0);
208 if (isFullSet() || isEmptySet()) {
209 Pred
= isEmptySet() ? CmpInst::ICMP_ULT
: CmpInst::ICMP_UGE
;
210 RHS
= APInt(getBitWidth(), 0);
211 } else if (auto *OnlyElt
= getSingleElement()) {
212 Pred
= CmpInst::ICMP_EQ
;
214 } else if (auto *OnlyMissingElt
= getSingleMissingElement()) {
215 Pred
= CmpInst::ICMP_NE
;
216 RHS
= *OnlyMissingElt
;
217 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
219 getLower().isMinSignedValue() ? CmpInst::ICMP_SLT
: CmpInst::ICMP_ULT
;
221 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
223 getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE
: CmpInst::ICMP_UGE
;
226 Pred
= CmpInst::ICMP_ULT
;
227 RHS
= getUpper() - getLower();
228 Offset
= -getLower();
231 assert(ConstantRange::makeExactICmpRegion(Pred
, RHS
) == add(Offset
) &&
235 bool ConstantRange::getEquivalentICmp(CmpInst::Predicate
&Pred
,
238 getEquivalentICmp(Pred
, RHS
, Offset
);
239 return Offset
.isZero();
242 bool ConstantRange::icmp(CmpInst::Predicate Pred
,
243 const ConstantRange
&Other
) const {
244 return makeSatisfyingICmpRegion(Pred
, Other
).contains(*this);
247 /// Exact mul nuw region for single element RHS.
248 static ConstantRange
makeExactMulNUWRegion(const APInt
&V
) {
249 unsigned BitWidth
= V
.getBitWidth();
251 return ConstantRange::getFull(V
.getBitWidth());
253 return ConstantRange::getNonEmpty(
254 APIntOps::RoundingUDiv(APInt::getMinValue(BitWidth
), V
,
255 APInt::Rounding::UP
),
256 APIntOps::RoundingUDiv(APInt::getMaxValue(BitWidth
), V
,
257 APInt::Rounding::DOWN
) + 1);
260 /// Exact mul nsw region for single element RHS.
261 static ConstantRange
makeExactMulNSWRegion(const APInt
&V
) {
262 // Handle 0 and -1 separately to avoid division by zero or overflow.
263 unsigned BitWidth
= V
.getBitWidth();
265 return ConstantRange::getFull(BitWidth
);
267 APInt MinValue
= APInt::getSignedMinValue(BitWidth
);
268 APInt MaxValue
= APInt::getSignedMaxValue(BitWidth
);
269 // e.g. Returning [-127, 127], represented as [-127, -128).
271 return ConstantRange(-MaxValue
, MinValue
);
274 if (V
.isNegative()) {
275 Lower
= APIntOps::RoundingSDiv(MaxValue
, V
, APInt::Rounding::UP
);
276 Upper
= APIntOps::RoundingSDiv(MinValue
, V
, APInt::Rounding::DOWN
);
278 Lower
= APIntOps::RoundingSDiv(MinValue
, V
, APInt::Rounding::UP
);
279 Upper
= APIntOps::RoundingSDiv(MaxValue
, V
, APInt::Rounding::DOWN
);
281 return ConstantRange::getNonEmpty(Lower
, Upper
+ 1);
285 ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp
,
286 const ConstantRange
&Other
,
287 unsigned NoWrapKind
) {
288 using OBO
= OverflowingBinaryOperator
;
290 assert(Instruction::isBinaryOp(BinOp
) && "Binary operators only!");
292 assert((NoWrapKind
== OBO::NoSignedWrap
||
293 NoWrapKind
== OBO::NoUnsignedWrap
) &&
294 "NoWrapKind invalid!");
296 bool Unsigned
= NoWrapKind
== OBO::NoUnsignedWrap
;
297 unsigned BitWidth
= Other
.getBitWidth();
301 llvm_unreachable("Unsupported binary op");
303 case Instruction::Add
: {
305 return getNonEmpty(APInt::getZero(BitWidth
), -Other
.getUnsignedMax());
307 APInt SignedMinVal
= APInt::getSignedMinValue(BitWidth
);
308 APInt SMin
= Other
.getSignedMin(), SMax
= Other
.getSignedMax();
310 SMin
.isNegative() ? SignedMinVal
- SMin
: SignedMinVal
,
311 SMax
.isStrictlyPositive() ? SignedMinVal
- SMax
: SignedMinVal
);
314 case Instruction::Sub
: {
316 return getNonEmpty(Other
.getUnsignedMax(), APInt::getMinValue(BitWidth
));
318 APInt SignedMinVal
= APInt::getSignedMinValue(BitWidth
);
319 APInt SMin
= Other
.getSignedMin(), SMax
= Other
.getSignedMax();
321 SMax
.isStrictlyPositive() ? SignedMinVal
+ SMax
: SignedMinVal
,
322 SMin
.isNegative() ? SignedMinVal
+ SMin
: SignedMinVal
);
325 case Instruction::Mul
:
327 return makeExactMulNUWRegion(Other
.getUnsignedMax());
329 // Avoid one makeExactMulNSWRegion() call for the common case of constants.
330 if (const APInt
*C
= Other
.getSingleElement())
331 return makeExactMulNSWRegion(*C
);
333 return makeExactMulNSWRegion(Other
.getSignedMin())
334 .intersectWith(makeExactMulNSWRegion(Other
.getSignedMax()));
336 case Instruction::Shl
: {
337 // For given range of shift amounts, if we ignore all illegal shift amounts
338 // (that always produce poison), what shift amount range is left?
339 ConstantRange ShAmt
= Other
.intersectWith(
340 ConstantRange(APInt(BitWidth
, 0), APInt(BitWidth
, (BitWidth
- 1) + 1)));
341 if (ShAmt
.isEmptySet()) {
342 // If the entire range of shift amounts is already poison-producing,
343 // then we can freely add more poison-producing flags ontop of that.
344 return getFull(BitWidth
);
346 // There are some legal shift amounts, we can compute conservatively-correct
347 // range of no-wrap inputs. Note that by now we have clamped the ShAmtUMax
348 // to be at most bitwidth-1, which results in most conservative range.
349 APInt ShAmtUMax
= ShAmt
.getUnsignedMax();
351 return getNonEmpty(APInt::getZero(BitWidth
),
352 APInt::getMaxValue(BitWidth
).lshr(ShAmtUMax
) + 1);
353 return getNonEmpty(APInt::getSignedMinValue(BitWidth
).ashr(ShAmtUMax
),
354 APInt::getSignedMaxValue(BitWidth
).ashr(ShAmtUMax
) + 1);
359 ConstantRange
ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp
,
361 unsigned NoWrapKind
) {
362 // makeGuaranteedNoWrapRegion() is exact for single-element ranges, as
363 // "for all" and "for any" coincide in this case.
364 return makeGuaranteedNoWrapRegion(BinOp
, ConstantRange(Other
), NoWrapKind
);
367 bool ConstantRange::isFullSet() const {
368 return Lower
== Upper
&& Lower
.isMaxValue();
371 bool ConstantRange::isEmptySet() const {
372 return Lower
== Upper
&& Lower
.isMinValue();
375 bool ConstantRange::isWrappedSet() const {
376 return Lower
.ugt(Upper
) && !Upper
.isZero();
379 bool ConstantRange::isUpperWrapped() const {
380 return Lower
.ugt(Upper
);
383 bool ConstantRange::isSignWrappedSet() const {
384 return Lower
.sgt(Upper
) && !Upper
.isMinSignedValue();
387 bool ConstantRange::isUpperSignWrapped() const {
388 return Lower
.sgt(Upper
);
392 ConstantRange::isSizeStrictlySmallerThan(const ConstantRange
&Other
) const {
393 assert(getBitWidth() == Other
.getBitWidth());
396 if (Other
.isFullSet())
398 return (Upper
- Lower
).ult(Other
.Upper
- Other
.Lower
);
402 ConstantRange::isSizeLargerThan(uint64_t MaxSize
) const {
403 // If this a full set, we need special handling to avoid needing an extra bit
404 // to represent the size.
406 return MaxSize
== 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize
- 1);
408 return (Upper
- Lower
).ugt(MaxSize
);
411 bool ConstantRange::isAllNegative() const {
412 // Empty set is all negative, full set is not.
418 return !isUpperSignWrapped() && !Upper
.isStrictlyPositive();
421 bool ConstantRange::isAllNonNegative() const {
422 // Empty and full set are automatically treated correctly.
423 return !isSignWrappedSet() && Lower
.isNonNegative();
426 APInt
ConstantRange::getUnsignedMax() const {
427 if (isFullSet() || isUpperWrapped())
428 return APInt::getMaxValue(getBitWidth());
429 return getUpper() - 1;
432 APInt
ConstantRange::getUnsignedMin() const {
433 if (isFullSet() || isWrappedSet())
434 return APInt::getMinValue(getBitWidth());
438 APInt
ConstantRange::getSignedMax() const {
439 if (isFullSet() || isUpperSignWrapped())
440 return APInt::getSignedMaxValue(getBitWidth());
441 return getUpper() - 1;
444 APInt
ConstantRange::getSignedMin() const {
445 if (isFullSet() || isSignWrappedSet())
446 return APInt::getSignedMinValue(getBitWidth());
450 bool ConstantRange::contains(const APInt
&V
) const {
454 if (!isUpperWrapped())
455 return Lower
.ule(V
) && V
.ult(Upper
);
456 return Lower
.ule(V
) || V
.ult(Upper
);
459 bool ConstantRange::contains(const ConstantRange
&Other
) const {
460 if (isFullSet() || Other
.isEmptySet()) return true;
461 if (isEmptySet() || Other
.isFullSet()) return false;
463 if (!isUpperWrapped()) {
464 if (Other
.isUpperWrapped())
467 return Lower
.ule(Other
.getLower()) && Other
.getUpper().ule(Upper
);
470 if (!Other
.isUpperWrapped())
471 return Other
.getUpper().ule(Upper
) ||
472 Lower
.ule(Other
.getLower());
474 return Other
.getUpper().ule(Upper
) && Lower
.ule(Other
.getLower());
477 unsigned ConstantRange::getActiveBits() const {
481 return getUnsignedMax().getActiveBits();
484 unsigned ConstantRange::getMinSignedBits() const {
488 return std::max(getSignedMin().getSignificantBits(),
489 getSignedMax().getSignificantBits());
492 ConstantRange
ConstantRange::subtract(const APInt
&Val
) const {
493 assert(Val
.getBitWidth() == getBitWidth() && "Wrong bit width");
494 // If the set is empty or full, don't modify the endpoints.
497 return ConstantRange(Lower
- Val
, Upper
- Val
);
500 ConstantRange
ConstantRange::difference(const ConstantRange
&CR
) const {
501 return intersectWith(CR
.inverse());
504 static ConstantRange
getPreferredRange(
505 const ConstantRange
&CR1
, const ConstantRange
&CR2
,
506 ConstantRange::PreferredRangeType Type
) {
507 if (Type
== ConstantRange::Unsigned
) {
508 if (!CR1
.isWrappedSet() && CR2
.isWrappedSet())
510 if (CR1
.isWrappedSet() && !CR2
.isWrappedSet())
512 } else if (Type
== ConstantRange::Signed
) {
513 if (!CR1
.isSignWrappedSet() && CR2
.isSignWrappedSet())
515 if (CR1
.isSignWrappedSet() && !CR2
.isSignWrappedSet())
519 if (CR1
.isSizeStrictlySmallerThan(CR2
))
524 ConstantRange
ConstantRange::intersectWith(const ConstantRange
&CR
,
525 PreferredRangeType Type
) const {
526 assert(getBitWidth() == CR
.getBitWidth() &&
527 "ConstantRange types don't agree!");
529 // Handle common cases.
530 if ( isEmptySet() || CR
.isFullSet()) return *this;
531 if (CR
.isEmptySet() || isFullSet()) return CR
;
533 if (!isUpperWrapped() && CR
.isUpperWrapped())
534 return CR
.intersectWith(*this, Type
);
536 if (!isUpperWrapped() && !CR
.isUpperWrapped()) {
537 if (Lower
.ult(CR
.Lower
)) {
540 if (Upper
.ule(CR
.Lower
))
545 if (Upper
.ult(CR
.Upper
))
546 return ConstantRange(CR
.Lower
, Upper
);
554 if (Upper
.ult(CR
.Upper
))
559 if (Lower
.ult(CR
.Upper
))
560 return ConstantRange(Lower
, CR
.Upper
);
567 if (isUpperWrapped() && !CR
.isUpperWrapped()) {
568 if (CR
.Lower
.ult(Upper
)) {
569 // ------U L--- : this
571 if (CR
.Upper
.ult(Upper
))
574 // ------U L--- : this
576 if (CR
.Upper
.ule(Lower
))
577 return ConstantRange(CR
.Lower
, Upper
);
579 // ------U L--- : this
581 return getPreferredRange(*this, CR
, Type
);
583 if (CR
.Lower
.ult(Lower
)) {
586 if (CR
.Upper
.ule(Lower
))
591 return ConstantRange(Lower
, CR
.Upper
);
594 // --U L------ : this
599 if (CR
.Upper
.ult(Upper
)) {
600 // ------U L-- : this
602 if (CR
.Lower
.ult(Upper
))
603 return getPreferredRange(*this, CR
, Type
);
607 if (CR
.Lower
.ult(Lower
))
608 return ConstantRange(Lower
, CR
.Upper
);
610 // ----U L---- : this
614 if (CR
.Upper
.ule(Lower
)) {
617 if (CR
.Lower
.ult(Lower
))
622 return ConstantRange(CR
.Lower
, Upper
);
625 // --U L------ : this
627 return getPreferredRange(*this, CR
, Type
);
630 ConstantRange
ConstantRange::unionWith(const ConstantRange
&CR
,
631 PreferredRangeType Type
) const {
632 assert(getBitWidth() == CR
.getBitWidth() &&
633 "ConstantRange types don't agree!");
635 if ( isFullSet() || CR
.isEmptySet()) return *this;
636 if (CR
.isFullSet() || isEmptySet()) return CR
;
638 if (!isUpperWrapped() && CR
.isUpperWrapped())
639 return CR
.unionWith(*this, Type
);
641 if (!isUpperWrapped() && !CR
.isUpperWrapped()) {
642 // L---U and L---U : this
647 if (CR
.Upper
.ult(Lower
) || Upper
.ult(CR
.Lower
))
648 return getPreferredRange(
649 ConstantRange(Lower
, CR
.Upper
), ConstantRange(CR
.Lower
, Upper
), Type
);
651 APInt L
= CR
.Lower
.ult(Lower
) ? CR
.Lower
: Lower
;
652 APInt U
= (CR
.Upper
- 1).ugt(Upper
- 1) ? CR
.Upper
: Upper
;
654 if (L
.isZero() && U
.isZero())
657 return ConstantRange(std::move(L
), std::move(U
));
660 if (!CR
.isUpperWrapped()) {
661 // ------U L----- and ------U L----- : this
663 if (CR
.Upper
.ule(Upper
) || CR
.Lower
.uge(Lower
))
666 // ------U L----- : this
668 if (CR
.Lower
.ule(Upper
) && Lower
.ule(CR
.Upper
))
671 // ----U L---- : this
676 if (Upper
.ult(CR
.Lower
) && CR
.Upper
.ult(Lower
))
677 return getPreferredRange(
678 ConstantRange(Lower
, CR
.Upper
), ConstantRange(CR
.Lower
, Upper
), Type
);
680 // ----U L----- : this
682 if (Upper
.ult(CR
.Lower
) && Lower
.ule(CR
.Upper
))
683 return ConstantRange(CR
.Lower
, Upper
);
685 // ------U L---- : this
687 assert(CR
.Lower
.ule(Upper
) && CR
.Upper
.ult(Lower
) &&
688 "ConstantRange::unionWith missed a case with one range wrapped");
689 return ConstantRange(Lower
, CR
.Upper
);
692 // ------U L---- and ------U L---- : this
693 // -U L----------- and ------------U L : CR
694 if (CR
.Lower
.ule(Upper
) || Lower
.ule(CR
.Upper
))
697 APInt L
= CR
.Lower
.ult(Lower
) ? CR
.Lower
: Lower
;
698 APInt U
= CR
.Upper
.ugt(Upper
) ? CR
.Upper
: Upper
;
700 return ConstantRange(std::move(L
), std::move(U
));
703 std::optional
<ConstantRange
>
704 ConstantRange::exactIntersectWith(const ConstantRange
&CR
) const {
705 // TODO: This can be implemented more efficiently.
706 ConstantRange Result
= intersectWith(CR
);
707 if (Result
== inverse().unionWith(CR
.inverse()).inverse())
712 std::optional
<ConstantRange
>
713 ConstantRange::exactUnionWith(const ConstantRange
&CR
) const {
714 // TODO: This can be implemented more efficiently.
715 ConstantRange Result
= unionWith(CR
);
716 if (Result
== inverse().intersectWith(CR
.inverse()).inverse())
721 ConstantRange
ConstantRange::castOp(Instruction::CastOps CastOp
,
722 uint32_t ResultBitWidth
) const {
725 llvm_unreachable("unsupported cast type");
726 case Instruction::Trunc
:
727 return truncate(ResultBitWidth
);
728 case Instruction::SExt
:
729 return signExtend(ResultBitWidth
);
730 case Instruction::ZExt
:
731 return zeroExtend(ResultBitWidth
);
732 case Instruction::BitCast
:
734 case Instruction::FPToUI
:
735 case Instruction::FPToSI
:
736 if (getBitWidth() == ResultBitWidth
)
739 return getFull(ResultBitWidth
);
740 case Instruction::UIToFP
: {
741 // TODO: use input range if available
742 auto BW
= getBitWidth();
743 APInt Min
= APInt::getMinValue(BW
);
744 APInt Max
= APInt::getMaxValue(BW
);
745 if (ResultBitWidth
> BW
) {
746 Min
= Min
.zext(ResultBitWidth
);
747 Max
= Max
.zext(ResultBitWidth
);
749 return ConstantRange(std::move(Min
), std::move(Max
));
751 case Instruction::SIToFP
: {
752 // TODO: use input range if available
753 auto BW
= getBitWidth();
754 APInt SMin
= APInt::getSignedMinValue(BW
);
755 APInt SMax
= APInt::getSignedMaxValue(BW
);
756 if (ResultBitWidth
> BW
) {
757 SMin
= SMin
.sext(ResultBitWidth
);
758 SMax
= SMax
.sext(ResultBitWidth
);
760 return ConstantRange(std::move(SMin
), std::move(SMax
));
762 case Instruction::FPTrunc
:
763 case Instruction::FPExt
:
764 case Instruction::IntToPtr
:
765 case Instruction::PtrToInt
:
766 case Instruction::AddrSpaceCast
:
767 // Conservatively return getFull set.
768 return getFull(ResultBitWidth
);
772 ConstantRange
ConstantRange::zeroExtend(uint32_t DstTySize
) const {
773 if (isEmptySet()) return getEmpty(DstTySize
);
775 unsigned SrcTySize
= getBitWidth();
776 assert(SrcTySize
< DstTySize
&& "Not a value extension");
777 if (isFullSet() || isUpperWrapped()) {
778 // Change into [0, 1 << src bit width)
779 APInt
LowerExt(DstTySize
, 0);
780 if (!Upper
) // special case: [X, 0) -- not really wrapping around
781 LowerExt
= Lower
.zext(DstTySize
);
782 return ConstantRange(std::move(LowerExt
),
783 APInt::getOneBitSet(DstTySize
, SrcTySize
));
786 return ConstantRange(Lower
.zext(DstTySize
), Upper
.zext(DstTySize
));
789 ConstantRange
ConstantRange::signExtend(uint32_t DstTySize
) const {
790 if (isEmptySet()) return getEmpty(DstTySize
);
792 unsigned SrcTySize
= getBitWidth();
793 assert(SrcTySize
< DstTySize
&& "Not a value extension");
795 // special case: [X, INT_MIN) -- not really wrapping around
796 if (Upper
.isMinSignedValue())
797 return ConstantRange(Lower
.sext(DstTySize
), Upper
.zext(DstTySize
));
799 if (isFullSet() || isSignWrappedSet()) {
800 return ConstantRange(APInt::getHighBitsSet(DstTySize
,DstTySize
-SrcTySize
+1),
801 APInt::getLowBitsSet(DstTySize
, SrcTySize
-1) + 1);
804 return ConstantRange(Lower
.sext(DstTySize
), Upper
.sext(DstTySize
));
807 ConstantRange
ConstantRange::truncate(uint32_t DstTySize
) const {
808 assert(getBitWidth() > DstTySize
&& "Not a value truncation");
810 return getEmpty(DstTySize
);
812 return getFull(DstTySize
);
814 APInt
LowerDiv(Lower
), UpperDiv(Upper
);
815 ConstantRange
Union(DstTySize
, /*isFullSet=*/false);
817 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
818 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
819 // then we do the union with [MaxValue, Upper)
820 if (isUpperWrapped()) {
821 // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
823 if (Upper
.getActiveBits() > DstTySize
|| Upper
.countr_one() == DstTySize
)
824 return getFull(DstTySize
);
826 Union
= ConstantRange(APInt::getMaxValue(DstTySize
),Upper
.trunc(DstTySize
));
827 UpperDiv
.setAllBits();
829 // Union covers the MaxValue case, so return if the remaining range is just
831 if (LowerDiv
== UpperDiv
)
835 // Chop off the most significant bits that are past the destination bitwidth.
836 if (LowerDiv
.getActiveBits() > DstTySize
) {
837 // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv.
838 APInt Adjust
= LowerDiv
& APInt::getBitsSetFrom(getBitWidth(), DstTySize
);
843 unsigned UpperDivWidth
= UpperDiv
.getActiveBits();
844 if (UpperDivWidth
<= DstTySize
)
845 return ConstantRange(LowerDiv
.trunc(DstTySize
),
846 UpperDiv
.trunc(DstTySize
)).unionWith(Union
);
848 // The truncated value wraps around. Check if we can do better than fullset.
849 if (UpperDivWidth
== DstTySize
+ 1) {
850 // Clear the MSB so that UpperDiv wraps around.
851 UpperDiv
.clearBit(DstTySize
);
852 if (UpperDiv
.ult(LowerDiv
))
853 return ConstantRange(LowerDiv
.trunc(DstTySize
),
854 UpperDiv
.trunc(DstTySize
)).unionWith(Union
);
857 return getFull(DstTySize
);
860 ConstantRange
ConstantRange::zextOrTrunc(uint32_t DstTySize
) const {
861 unsigned SrcTySize
= getBitWidth();
862 if (SrcTySize
> DstTySize
)
863 return truncate(DstTySize
);
864 if (SrcTySize
< DstTySize
)
865 return zeroExtend(DstTySize
);
869 ConstantRange
ConstantRange::sextOrTrunc(uint32_t DstTySize
) const {
870 unsigned SrcTySize
= getBitWidth();
871 if (SrcTySize
> DstTySize
)
872 return truncate(DstTySize
);
873 if (SrcTySize
< DstTySize
)
874 return signExtend(DstTySize
);
878 ConstantRange
ConstantRange::binaryOp(Instruction::BinaryOps BinOp
,
879 const ConstantRange
&Other
) const {
880 assert(Instruction::isBinaryOp(BinOp
) && "Binary operators only!");
883 case Instruction::Add
:
885 case Instruction::Sub
:
887 case Instruction::Mul
:
888 return multiply(Other
);
889 case Instruction::UDiv
:
891 case Instruction::SDiv
:
893 case Instruction::URem
:
895 case Instruction::SRem
:
897 case Instruction::Shl
:
899 case Instruction::LShr
:
901 case Instruction::AShr
:
903 case Instruction::And
:
904 return binaryAnd(Other
);
905 case Instruction::Or
:
906 return binaryOr(Other
);
907 case Instruction::Xor
:
908 return binaryXor(Other
);
909 // Note: floating point operations applied to abstract ranges are just
910 // ideal integer operations with a lossy representation
911 case Instruction::FAdd
:
913 case Instruction::FSub
:
915 case Instruction::FMul
:
916 return multiply(Other
);
918 // Conservatively return getFull set.
923 ConstantRange
ConstantRange::overflowingBinaryOp(Instruction::BinaryOps BinOp
,
924 const ConstantRange
&Other
,
925 unsigned NoWrapKind
) const {
926 assert(Instruction::isBinaryOp(BinOp
) && "Binary operators only!");
929 case Instruction::Add
:
930 return addWithNoWrap(Other
, NoWrapKind
);
931 case Instruction::Sub
:
932 return subWithNoWrap(Other
, NoWrapKind
);
934 // Don't know about this Overflowing Binary Operation.
935 // Conservatively fallback to plain binop handling.
936 return binaryOp(BinOp
, Other
);
940 bool ConstantRange::isIntrinsicSupported(Intrinsic::ID IntrinsicID
) {
941 switch (IntrinsicID
) {
942 case Intrinsic::uadd_sat
:
943 case Intrinsic::usub_sat
:
944 case Intrinsic::sadd_sat
:
945 case Intrinsic::ssub_sat
:
946 case Intrinsic::umin
:
947 case Intrinsic::umax
:
948 case Intrinsic::smin
:
949 case Intrinsic::smax
:
951 case Intrinsic::ctlz
:
958 ConstantRange
ConstantRange::intrinsic(Intrinsic::ID IntrinsicID
,
959 ArrayRef
<ConstantRange
> Ops
) {
960 switch (IntrinsicID
) {
961 case Intrinsic::uadd_sat
:
962 return Ops
[0].uadd_sat(Ops
[1]);
963 case Intrinsic::usub_sat
:
964 return Ops
[0].usub_sat(Ops
[1]);
965 case Intrinsic::sadd_sat
:
966 return Ops
[0].sadd_sat(Ops
[1]);
967 case Intrinsic::ssub_sat
:
968 return Ops
[0].ssub_sat(Ops
[1]);
969 case Intrinsic::umin
:
970 return Ops
[0].umin(Ops
[1]);
971 case Intrinsic::umax
:
972 return Ops
[0].umax(Ops
[1]);
973 case Intrinsic::smin
:
974 return Ops
[0].smin(Ops
[1]);
975 case Intrinsic::smax
:
976 return Ops
[0].smax(Ops
[1]);
977 case Intrinsic::abs
: {
978 const APInt
*IntMinIsPoison
= Ops
[1].getSingleElement();
979 assert(IntMinIsPoison
&& "Must be known (immarg)");
980 assert(IntMinIsPoison
->getBitWidth() == 1 && "Must be boolean");
981 return Ops
[0].abs(IntMinIsPoison
->getBoolValue());
983 case Intrinsic::ctlz
: {
984 const APInt
*ZeroIsPoison
= Ops
[1].getSingleElement();
985 assert(ZeroIsPoison
&& "Must be known (immarg)");
986 assert(ZeroIsPoison
->getBitWidth() == 1 && "Must be boolean");
987 return Ops
[0].ctlz(ZeroIsPoison
->getBoolValue());
990 assert(!isIntrinsicSupported(IntrinsicID
) && "Shouldn't be supported");
991 llvm_unreachable("Unsupported intrinsic");
996 ConstantRange::add(const ConstantRange
&Other
) const {
997 if (isEmptySet() || Other
.isEmptySet())
999 if (isFullSet() || Other
.isFullSet())
1002 APInt NewLower
= getLower() + Other
.getLower();
1003 APInt NewUpper
= getUpper() + Other
.getUpper() - 1;
1004 if (NewLower
== NewUpper
)
1007 ConstantRange X
= ConstantRange(std::move(NewLower
), std::move(NewUpper
));
1008 if (X
.isSizeStrictlySmallerThan(*this) ||
1009 X
.isSizeStrictlySmallerThan(Other
))
1010 // We've wrapped, therefore, full set.
1015 ConstantRange
ConstantRange::addWithNoWrap(const ConstantRange
&Other
,
1016 unsigned NoWrapKind
,
1017 PreferredRangeType RangeType
) const {
1018 // Calculate the range for "X + Y" which is guaranteed not to wrap(overflow).
1019 // (X is from this, and Y is from Other)
1020 if (isEmptySet() || Other
.isEmptySet())
1022 if (isFullSet() && Other
.isFullSet())
1025 using OBO
= OverflowingBinaryOperator
;
1026 ConstantRange Result
= add(Other
);
1028 // If an overflow happens for every value pair in these two constant ranges,
1029 // we must return Empty set. In this case, we get that for free, because we
1030 // get lucky that intersection of add() with uadd_sat()/sadd_sat() results
1033 if (NoWrapKind
& OBO::NoSignedWrap
)
1034 Result
= Result
.intersectWith(sadd_sat(Other
), RangeType
);
1036 if (NoWrapKind
& OBO::NoUnsignedWrap
)
1037 Result
= Result
.intersectWith(uadd_sat(Other
), RangeType
);
1043 ConstantRange::sub(const ConstantRange
&Other
) const {
1044 if (isEmptySet() || Other
.isEmptySet())
1046 if (isFullSet() || Other
.isFullSet())
1049 APInt NewLower
= getLower() - Other
.getUpper() + 1;
1050 APInt NewUpper
= getUpper() - Other
.getLower();
1051 if (NewLower
== NewUpper
)
1054 ConstantRange X
= ConstantRange(std::move(NewLower
), std::move(NewUpper
));
1055 if (X
.isSizeStrictlySmallerThan(*this) ||
1056 X
.isSizeStrictlySmallerThan(Other
))
1057 // We've wrapped, therefore, full set.
1062 ConstantRange
ConstantRange::subWithNoWrap(const ConstantRange
&Other
,
1063 unsigned NoWrapKind
,
1064 PreferredRangeType RangeType
) const {
1065 // Calculate the range for "X - Y" which is guaranteed not to wrap(overflow).
1066 // (X is from this, and Y is from Other)
1067 if (isEmptySet() || Other
.isEmptySet())
1069 if (isFullSet() && Other
.isFullSet())
1072 using OBO
= OverflowingBinaryOperator
;
1073 ConstantRange Result
= sub(Other
);
1075 // If an overflow happens for every value pair in these two constant ranges,
1076 // we must return Empty set. In signed case, we get that for free, because we
1077 // get lucky that intersection of sub() with ssub_sat() results in an
1078 // empty set. But for unsigned we must perform the overflow check manually.
1080 if (NoWrapKind
& OBO::NoSignedWrap
)
1081 Result
= Result
.intersectWith(ssub_sat(Other
), RangeType
);
1083 if (NoWrapKind
& OBO::NoUnsignedWrap
) {
1084 if (getUnsignedMax().ult(Other
.getUnsignedMin()))
1085 return getEmpty(); // Always overflows.
1086 Result
= Result
.intersectWith(usub_sat(Other
), RangeType
);
1093 ConstantRange::multiply(const ConstantRange
&Other
) const {
1094 // TODO: If either operand is a single element and the multiply is known to
1095 // be non-wrapping, round the result min and max value to the appropriate
1096 // multiple of that element. If wrapping is possible, at least adjust the
1097 // range according to the greatest power-of-two factor of the single element.
1099 if (isEmptySet() || Other
.isEmptySet())
1102 if (const APInt
*C
= getSingleElement()) {
1106 return ConstantRange(APInt::getZero(getBitWidth())).sub(Other
);
1109 if (const APInt
*C
= Other
.getSingleElement()) {
1113 return ConstantRange(APInt::getZero(getBitWidth())).sub(*this);
1116 // Multiplication is signedness-independent. However different ranges can be
1117 // obtained depending on how the input ranges are treated. These different
1118 // ranges are all conservatively correct, but one might be better than the
1119 // other. We calculate two ranges; one treating the inputs as unsigned
1120 // and the other signed, then return the smallest of these ranges.
1122 // Unsigned range first.
1123 APInt this_min
= getUnsignedMin().zext(getBitWidth() * 2);
1124 APInt this_max
= getUnsignedMax().zext(getBitWidth() * 2);
1125 APInt Other_min
= Other
.getUnsignedMin().zext(getBitWidth() * 2);
1126 APInt Other_max
= Other
.getUnsignedMax().zext(getBitWidth() * 2);
1128 ConstantRange Result_zext
= ConstantRange(this_min
* Other_min
,
1129 this_max
* Other_max
+ 1);
1130 ConstantRange UR
= Result_zext
.truncate(getBitWidth());
1132 // If the unsigned range doesn't wrap, and isn't negative then it's a range
1133 // from one positive number to another which is as good as we can generate.
1134 // In this case, skip the extra work of generating signed ranges which aren't
1135 // going to be better than this range.
1136 if (!UR
.isUpperWrapped() &&
1137 (UR
.getUpper().isNonNegative() || UR
.getUpper().isMinSignedValue()))
1140 // Now the signed range. Because we could be dealing with negative numbers
1141 // here, the lower bound is the smallest of the cartesian product of the
1142 // lower and upper ranges; for example:
1143 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1144 // Similarly for the upper bound, swapping min for max.
1146 this_min
= getSignedMin().sext(getBitWidth() * 2);
1147 this_max
= getSignedMax().sext(getBitWidth() * 2);
1148 Other_min
= Other
.getSignedMin().sext(getBitWidth() * 2);
1149 Other_max
= Other
.getSignedMax().sext(getBitWidth() * 2);
1151 auto L
= {this_min
* Other_min
, this_min
* Other_max
,
1152 this_max
* Other_min
, this_max
* Other_max
};
1153 auto Compare
= [](const APInt
&A
, const APInt
&B
) { return A
.slt(B
); };
1154 ConstantRange
Result_sext(std::min(L
, Compare
), std::max(L
, Compare
) + 1);
1155 ConstantRange SR
= Result_sext
.truncate(getBitWidth());
1157 return UR
.isSizeStrictlySmallerThan(SR
) ? UR
: SR
;
1160 ConstantRange
ConstantRange::smul_fast(const ConstantRange
&Other
) const {
1161 if (isEmptySet() || Other
.isEmptySet())
1164 APInt Min
= getSignedMin();
1165 APInt Max
= getSignedMax();
1166 APInt OtherMin
= Other
.getSignedMin();
1167 APInt OtherMax
= Other
.getSignedMax();
1169 bool O1
, O2
, O3
, O4
;
1170 auto Muls
= {Min
.smul_ov(OtherMin
, O1
), Min
.smul_ov(OtherMax
, O2
),
1171 Max
.smul_ov(OtherMin
, O3
), Max
.smul_ov(OtherMax
, O4
)};
1172 if (O1
|| O2
|| O3
|| O4
)
1175 auto Compare
= [](const APInt
&A
, const APInt
&B
) { return A
.slt(B
); };
1176 return getNonEmpty(std::min(Muls
, Compare
), std::max(Muls
, Compare
) + 1);
1180 ConstantRange::smax(const ConstantRange
&Other
) const {
1181 // X smax Y is: range(smax(X_smin, Y_smin),
1182 // smax(X_smax, Y_smax))
1183 if (isEmptySet() || Other
.isEmptySet())
1185 APInt NewL
= APIntOps::smax(getSignedMin(), Other
.getSignedMin());
1186 APInt NewU
= APIntOps::smax(getSignedMax(), Other
.getSignedMax()) + 1;
1187 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1188 if (isSignWrappedSet() || Other
.isSignWrappedSet())
1189 return Res
.intersectWith(unionWith(Other
, Signed
), Signed
);
1194 ConstantRange::umax(const ConstantRange
&Other
) const {
1195 // X umax Y is: range(umax(X_umin, Y_umin),
1196 // umax(X_umax, Y_umax))
1197 if (isEmptySet() || Other
.isEmptySet())
1199 APInt NewL
= APIntOps::umax(getUnsignedMin(), Other
.getUnsignedMin());
1200 APInt NewU
= APIntOps::umax(getUnsignedMax(), Other
.getUnsignedMax()) + 1;
1201 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1202 if (isWrappedSet() || Other
.isWrappedSet())
1203 return Res
.intersectWith(unionWith(Other
, Unsigned
), Unsigned
);
1208 ConstantRange::smin(const ConstantRange
&Other
) const {
1209 // X smin Y is: range(smin(X_smin, Y_smin),
1210 // smin(X_smax, Y_smax))
1211 if (isEmptySet() || Other
.isEmptySet())
1213 APInt NewL
= APIntOps::smin(getSignedMin(), Other
.getSignedMin());
1214 APInt NewU
= APIntOps::smin(getSignedMax(), Other
.getSignedMax()) + 1;
1215 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1216 if (isSignWrappedSet() || Other
.isSignWrappedSet())
1217 return Res
.intersectWith(unionWith(Other
, Signed
), Signed
);
1222 ConstantRange::umin(const ConstantRange
&Other
) const {
1223 // X umin Y is: range(umin(X_umin, Y_umin),
1224 // umin(X_umax, Y_umax))
1225 if (isEmptySet() || Other
.isEmptySet())
1227 APInt NewL
= APIntOps::umin(getUnsignedMin(), Other
.getUnsignedMin());
1228 APInt NewU
= APIntOps::umin(getUnsignedMax(), Other
.getUnsignedMax()) + 1;
1229 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1230 if (isWrappedSet() || Other
.isWrappedSet())
1231 return Res
.intersectWith(unionWith(Other
, Unsigned
), Unsigned
);
1236 ConstantRange::udiv(const ConstantRange
&RHS
) const {
1237 if (isEmptySet() || RHS
.isEmptySet() || RHS
.getUnsignedMax().isZero())
1240 APInt Lower
= getUnsignedMin().udiv(RHS
.getUnsignedMax());
1242 APInt RHS_umin
= RHS
.getUnsignedMin();
1243 if (RHS_umin
.isZero()) {
1244 // We want the lowest value in RHS excluding zero. Usually that would be 1
1245 // except for a range in the form of [X, 1) in which case it would be X.
1246 if (RHS
.getUpper() == 1)
1247 RHS_umin
= RHS
.getLower();
1252 APInt Upper
= getUnsignedMax().udiv(RHS_umin
) + 1;
1253 return getNonEmpty(std::move(Lower
), std::move(Upper
));
1256 ConstantRange
ConstantRange::sdiv(const ConstantRange
&RHS
) const {
1257 // We split up the LHS and RHS into positive and negative components
1258 // and then also compute the positive and negative components of the result
1259 // separately by combining division results with the appropriate signs.
1260 APInt Zero
= APInt::getZero(getBitWidth());
1261 APInt SignedMin
= APInt::getSignedMinValue(getBitWidth());
1262 // There are no positive 1-bit values. The 1 would get interpreted as -1.
1263 ConstantRange PosFilter
=
1264 getBitWidth() == 1 ? getEmpty()
1265 : ConstantRange(APInt(getBitWidth(), 1), SignedMin
);
1266 ConstantRange
NegFilter(SignedMin
, Zero
);
1267 ConstantRange PosL
= intersectWith(PosFilter
);
1268 ConstantRange NegL
= intersectWith(NegFilter
);
1269 ConstantRange PosR
= RHS
.intersectWith(PosFilter
);
1270 ConstantRange NegR
= RHS
.intersectWith(NegFilter
);
1272 ConstantRange PosRes
= getEmpty();
1273 if (!PosL
.isEmptySet() && !PosR
.isEmptySet())
1275 PosRes
= ConstantRange(PosL
.Lower
.sdiv(PosR
.Upper
- 1),
1276 (PosL
.Upper
- 1).sdiv(PosR
.Lower
) + 1);
1278 if (!NegL
.isEmptySet() && !NegR
.isEmptySet()) {
1281 // We need to deal with one tricky case here: SignedMin / -1 is UB on the
1282 // IR level, so we'll want to exclude this case when calculating bounds.
1283 // (For APInts the operation is well-defined and yields SignedMin.) We
1284 // handle this by dropping either SignedMin from the LHS or -1 from the RHS.
1285 APInt Lo
= (NegL
.Upper
- 1).sdiv(NegR
.Lower
);
1286 if (NegL
.Lower
.isMinSignedValue() && NegR
.Upper
.isZero()) {
1287 // Remove -1 from the LHS. Skip if it's the only element, as this would
1288 // leave us with an empty set.
1289 if (!NegR
.Lower
.isAllOnes()) {
1291 if (RHS
.Lower
.isAllOnes())
1292 // Negative part of [-1, X] without -1 is [SignedMin, X].
1293 AdjNegRUpper
= RHS
.Upper
;
1295 // [X, -1] without -1 is [X, -2].
1296 AdjNegRUpper
= NegR
.Upper
- 1;
1298 PosRes
= PosRes
.unionWith(
1299 ConstantRange(Lo
, NegL
.Lower
.sdiv(AdjNegRUpper
- 1) + 1));
1302 // Remove SignedMin from the RHS. Skip if it's the only element, as this
1303 // would leave us with an empty set.
1304 if (NegL
.Upper
!= SignedMin
+ 1) {
1306 if (Upper
== SignedMin
+ 1)
1307 // Negative part of [X, SignedMin] without SignedMin is [X, -1].
1308 AdjNegLLower
= Lower
;
1310 // [SignedMin, X] without SignedMin is [SignedMin + 1, X].
1311 AdjNegLLower
= NegL
.Lower
+ 1;
1313 PosRes
= PosRes
.unionWith(
1314 ConstantRange(std::move(Lo
),
1315 AdjNegLLower
.sdiv(NegR
.Upper
- 1) + 1));
1318 PosRes
= PosRes
.unionWith(
1319 ConstantRange(std::move(Lo
), NegL
.Lower
.sdiv(NegR
.Upper
- 1) + 1));
1323 ConstantRange NegRes
= getEmpty();
1324 if (!PosL
.isEmptySet() && !NegR
.isEmptySet())
1326 NegRes
= ConstantRange((PosL
.Upper
- 1).sdiv(NegR
.Upper
- 1),
1327 PosL
.Lower
.sdiv(NegR
.Lower
) + 1);
1329 if (!NegL
.isEmptySet() && !PosR
.isEmptySet())
1331 NegRes
= NegRes
.unionWith(
1332 ConstantRange(NegL
.Lower
.sdiv(PosR
.Lower
),
1333 (NegL
.Upper
- 1).sdiv(PosR
.Upper
- 1) + 1));
1335 // Prefer a non-wrapping signed range here.
1336 ConstantRange Res
= NegRes
.unionWith(PosRes
, PreferredRangeType::Signed
);
1338 // Preserve the zero that we dropped when splitting the LHS by sign.
1339 if (contains(Zero
) && (!PosR
.isEmptySet() || !NegR
.isEmptySet()))
1340 Res
= Res
.unionWith(ConstantRange(Zero
));
1344 ConstantRange
ConstantRange::urem(const ConstantRange
&RHS
) const {
1345 if (isEmptySet() || RHS
.isEmptySet() || RHS
.getUnsignedMax().isZero())
1348 if (const APInt
*RHSInt
= RHS
.getSingleElement()) {
1349 // UREM by null is UB.
1350 if (RHSInt
->isZero())
1352 // Use APInt's implementation of UREM for single element ranges.
1353 if (const APInt
*LHSInt
= getSingleElement())
1354 return {LHSInt
->urem(*RHSInt
)};
1357 // L % R for L < R is L.
1358 if (getUnsignedMax().ult(RHS
.getUnsignedMin()))
1361 // L % R is <= L and < R.
1362 APInt Upper
= APIntOps::umin(getUnsignedMax(), RHS
.getUnsignedMax() - 1) + 1;
1363 return getNonEmpty(APInt::getZero(getBitWidth()), std::move(Upper
));
1366 ConstantRange
ConstantRange::srem(const ConstantRange
&RHS
) const {
1367 if (isEmptySet() || RHS
.isEmptySet())
1370 if (const APInt
*RHSInt
= RHS
.getSingleElement()) {
1371 // SREM by null is UB.
1372 if (RHSInt
->isZero())
1374 // Use APInt's implementation of SREM for single element ranges.
1375 if (const APInt
*LHSInt
= getSingleElement())
1376 return {LHSInt
->srem(*RHSInt
)};
1379 ConstantRange AbsRHS
= RHS
.abs();
1380 APInt MinAbsRHS
= AbsRHS
.getUnsignedMin();
1381 APInt MaxAbsRHS
= AbsRHS
.getUnsignedMax();
1383 // Modulus by zero is UB.
1384 if (MaxAbsRHS
.isZero())
1387 if (MinAbsRHS
.isZero())
1390 APInt MinLHS
= getSignedMin(), MaxLHS
= getSignedMax();
1392 if (MinLHS
.isNonNegative()) {
1393 // L % R for L < R is L.
1394 if (MaxLHS
.ult(MinAbsRHS
))
1397 // L % R is <= L and < R.
1398 APInt Upper
= APIntOps::umin(MaxLHS
, MaxAbsRHS
- 1) + 1;
1399 return ConstantRange(APInt::getZero(getBitWidth()), std::move(Upper
));
1402 // Same basic logic as above, but the result is negative.
1403 if (MaxLHS
.isNegative()) {
1404 if (MinLHS
.ugt(-MinAbsRHS
))
1407 APInt Lower
= APIntOps::umax(MinLHS
, -MaxAbsRHS
+ 1);
1408 return ConstantRange(std::move(Lower
), APInt(getBitWidth(), 1));
1411 // LHS range crosses zero.
1412 APInt Lower
= APIntOps::umax(MinLHS
, -MaxAbsRHS
+ 1);
1413 APInt Upper
= APIntOps::umin(MaxLHS
, MaxAbsRHS
- 1) + 1;
1414 return ConstantRange(std::move(Lower
), std::move(Upper
));
1417 ConstantRange
ConstantRange::binaryNot() const {
1418 return ConstantRange(APInt::getAllOnes(getBitWidth())).sub(*this);
1421 ConstantRange
ConstantRange::binaryAnd(const ConstantRange
&Other
) const {
1422 if (isEmptySet() || Other
.isEmptySet())
1425 ConstantRange KnownBitsRange
=
1426 fromKnownBits(toKnownBits() & Other
.toKnownBits(), false);
1427 ConstantRange UMinUMaxRange
=
1428 getNonEmpty(APInt::getZero(getBitWidth()),
1429 APIntOps::umin(Other
.getUnsignedMax(), getUnsignedMax()) + 1);
1430 return KnownBitsRange
.intersectWith(UMinUMaxRange
);
1433 ConstantRange
ConstantRange::binaryOr(const ConstantRange
&Other
) const {
1434 if (isEmptySet() || Other
.isEmptySet())
1437 ConstantRange KnownBitsRange
=
1438 fromKnownBits(toKnownBits() | Other
.toKnownBits(), false);
1439 // Upper wrapped range.
1440 ConstantRange UMaxUMinRange
=
1441 getNonEmpty(APIntOps::umax(getUnsignedMin(), Other
.getUnsignedMin()),
1442 APInt::getZero(getBitWidth()));
1443 return KnownBitsRange
.intersectWith(UMaxUMinRange
);
1446 ConstantRange
ConstantRange::binaryXor(const ConstantRange
&Other
) const {
1447 if (isEmptySet() || Other
.isEmptySet())
1450 // Use APInt's implementation of XOR for single element ranges.
1451 if (isSingleElement() && Other
.isSingleElement())
1452 return {*getSingleElement() ^ *Other
.getSingleElement()};
1454 // Special-case binary complement, since we can give a precise answer.
1455 if (Other
.isSingleElement() && Other
.getSingleElement()->isAllOnes())
1457 if (isSingleElement() && getSingleElement()->isAllOnes())
1458 return Other
.binaryNot();
1460 return fromKnownBits(toKnownBits() ^ Other
.toKnownBits(), /*IsSigned*/false);
1464 ConstantRange::shl(const ConstantRange
&Other
) const {
1465 if (isEmptySet() || Other
.isEmptySet())
1468 APInt Min
= getUnsignedMin();
1469 APInt Max
= getUnsignedMax();
1470 if (const APInt
*RHS
= Other
.getSingleElement()) {
1471 unsigned BW
= getBitWidth();
1475 unsigned EqualLeadingBits
= (Min
^ Max
).countl_zero();
1476 if (RHS
->ule(EqualLeadingBits
))
1477 return getNonEmpty(Min
<< *RHS
, (Max
<< *RHS
) + 1);
1479 return getNonEmpty(APInt::getZero(BW
),
1480 APInt::getBitsSetFrom(BW
, RHS
->getZExtValue()) + 1);
1483 APInt OtherMax
= Other
.getUnsignedMax();
1484 if (isAllNegative() && OtherMax
.ule(Min
.countl_one())) {
1485 // For negative numbers, if the shift does not overflow in a signed sense,
1486 // a larger shift will make the number smaller.
1487 Max
<<= Other
.getUnsignedMin();
1489 return ConstantRange::getNonEmpty(std::move(Min
), std::move(Max
) + 1);
1492 // There's overflow!
1493 if (OtherMax
.ugt(Max
.countl_zero()))
1496 // FIXME: implement the other tricky cases
1498 Min
<<= Other
.getUnsignedMin();
1501 return ConstantRange::getNonEmpty(std::move(Min
), std::move(Max
) + 1);
1505 ConstantRange::lshr(const ConstantRange
&Other
) const {
1506 if (isEmptySet() || Other
.isEmptySet())
1509 APInt max
= getUnsignedMax().lshr(Other
.getUnsignedMin()) + 1;
1510 APInt min
= getUnsignedMin().lshr(Other
.getUnsignedMax());
1511 return getNonEmpty(std::move(min
), std::move(max
));
1515 ConstantRange::ashr(const ConstantRange
&Other
) const {
1516 if (isEmptySet() || Other
.isEmptySet())
1519 // May straddle zero, so handle both positive and negative cases.
1520 // 'PosMax' is the upper bound of the result of the ashr
1521 // operation, when Upper of the LHS of ashr is a non-negative.
1522 // number. Since ashr of a non-negative number will result in a
1523 // smaller number, the Upper value of LHS is shifted right with
1524 // the minimum value of 'Other' instead of the maximum value.
1525 APInt PosMax
= getSignedMax().ashr(Other
.getUnsignedMin()) + 1;
1527 // 'PosMin' is the lower bound of the result of the ashr
1528 // operation, when Lower of the LHS is a non-negative number.
1529 // Since ashr of a non-negative number will result in a smaller
1530 // number, the Lower value of LHS is shifted right with the
1531 // maximum value of 'Other'.
1532 APInt PosMin
= getSignedMin().ashr(Other
.getUnsignedMax());
1534 // 'NegMax' is the upper bound of the result of the ashr
1535 // operation, when Upper of the LHS of ashr is a negative number.
1536 // Since 'ashr' of a negative number will result in a bigger
1537 // number, the Upper value of LHS is shifted right with the
1538 // maximum value of 'Other'.
1539 APInt NegMax
= getSignedMax().ashr(Other
.getUnsignedMax()) + 1;
1541 // 'NegMin' is the lower bound of the result of the ashr
1542 // operation, when Lower of the LHS of ashr is a negative number.
1543 // Since 'ashr' of a negative number will result in a bigger
1544 // number, the Lower value of LHS is shifted right with the
1545 // minimum value of 'Other'.
1546 APInt NegMin
= getSignedMin().ashr(Other
.getUnsignedMin());
1549 if (getSignedMin().isNonNegative()) {
1550 // Upper and Lower of LHS are non-negative.
1553 } else if (getSignedMax().isNegative()) {
1554 // Upper and Lower of LHS are negative.
1558 // Upper is non-negative and Lower is negative.
1562 return getNonEmpty(std::move(min
), std::move(max
));
1565 ConstantRange
ConstantRange::uadd_sat(const ConstantRange
&Other
) const {
1566 if (isEmptySet() || Other
.isEmptySet())
1569 APInt NewL
= getUnsignedMin().uadd_sat(Other
.getUnsignedMin());
1570 APInt NewU
= getUnsignedMax().uadd_sat(Other
.getUnsignedMax()) + 1;
1571 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1574 ConstantRange
ConstantRange::sadd_sat(const ConstantRange
&Other
) const {
1575 if (isEmptySet() || Other
.isEmptySet())
1578 APInt NewL
= getSignedMin().sadd_sat(Other
.getSignedMin());
1579 APInt NewU
= getSignedMax().sadd_sat(Other
.getSignedMax()) + 1;
1580 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1583 ConstantRange
ConstantRange::usub_sat(const ConstantRange
&Other
) const {
1584 if (isEmptySet() || Other
.isEmptySet())
1587 APInt NewL
= getUnsignedMin().usub_sat(Other
.getUnsignedMax());
1588 APInt NewU
= getUnsignedMax().usub_sat(Other
.getUnsignedMin()) + 1;
1589 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1592 ConstantRange
ConstantRange::ssub_sat(const ConstantRange
&Other
) const {
1593 if (isEmptySet() || Other
.isEmptySet())
1596 APInt NewL
= getSignedMin().ssub_sat(Other
.getSignedMax());
1597 APInt NewU
= getSignedMax().ssub_sat(Other
.getSignedMin()) + 1;
1598 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1601 ConstantRange
ConstantRange::umul_sat(const ConstantRange
&Other
) const {
1602 if (isEmptySet() || Other
.isEmptySet())
1605 APInt NewL
= getUnsignedMin().umul_sat(Other
.getUnsignedMin());
1606 APInt NewU
= getUnsignedMax().umul_sat(Other
.getUnsignedMax()) + 1;
1607 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1610 ConstantRange
ConstantRange::smul_sat(const ConstantRange
&Other
) const {
1611 if (isEmptySet() || Other
.isEmptySet())
1614 // Because we could be dealing with negative numbers here, the lower bound is
1615 // the smallest of the cartesian product of the lower and upper ranges;
1617 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1618 // Similarly for the upper bound, swapping min for max.
1620 APInt Min
= getSignedMin();
1621 APInt Max
= getSignedMax();
1622 APInt OtherMin
= Other
.getSignedMin();
1623 APInt OtherMax
= Other
.getSignedMax();
1625 auto L
= {Min
.smul_sat(OtherMin
), Min
.smul_sat(OtherMax
),
1626 Max
.smul_sat(OtherMin
), Max
.smul_sat(OtherMax
)};
1627 auto Compare
= [](const APInt
&A
, const APInt
&B
) { return A
.slt(B
); };
1628 return getNonEmpty(std::min(L
, Compare
), std::max(L
, Compare
) + 1);
1631 ConstantRange
ConstantRange::ushl_sat(const ConstantRange
&Other
) const {
1632 if (isEmptySet() || Other
.isEmptySet())
1635 APInt NewL
= getUnsignedMin().ushl_sat(Other
.getUnsignedMin());
1636 APInt NewU
= getUnsignedMax().ushl_sat(Other
.getUnsignedMax()) + 1;
1637 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1640 ConstantRange
ConstantRange::sshl_sat(const ConstantRange
&Other
) const {
1641 if (isEmptySet() || Other
.isEmptySet())
1644 APInt Min
= getSignedMin(), Max
= getSignedMax();
1645 APInt ShAmtMin
= Other
.getUnsignedMin(), ShAmtMax
= Other
.getUnsignedMax();
1646 APInt NewL
= Min
.sshl_sat(Min
.isNonNegative() ? ShAmtMin
: ShAmtMax
);
1647 APInt NewU
= Max
.sshl_sat(Max
.isNegative() ? ShAmtMin
: ShAmtMax
) + 1;
1648 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1651 ConstantRange
ConstantRange::inverse() const {
1656 return ConstantRange(Upper
, Lower
);
1659 ConstantRange
ConstantRange::abs(bool IntMinIsPoison
) const {
1663 if (isSignWrappedSet()) {
1665 // Check whether the range crosses zero.
1666 if (Upper
.isStrictlyPositive() || !Lower
.isStrictlyPositive())
1667 Lo
= APInt::getZero(getBitWidth());
1669 Lo
= APIntOps::umin(Lower
, -Upper
+ 1);
1671 // If SignedMin is not poison, then it is included in the result range.
1673 return ConstantRange(Lo
, APInt::getSignedMinValue(getBitWidth()));
1675 return ConstantRange(Lo
, APInt::getSignedMinValue(getBitWidth()) + 1);
1678 APInt SMin
= getSignedMin(), SMax
= getSignedMax();
1680 // Skip SignedMin if it is poison.
1681 if (IntMinIsPoison
&& SMin
.isMinSignedValue()) {
1682 // The range may become empty if it *only* contains SignedMin.
1683 if (SMax
.isMinSignedValue())
1688 // All non-negative.
1689 if (SMin
.isNonNegative())
1690 return ConstantRange(SMin
, SMax
+ 1);
1693 if (SMax
.isNegative())
1694 return ConstantRange(-SMax
, -SMin
+ 1);
1696 // Range crosses zero.
1697 return ConstantRange::getNonEmpty(APInt::getZero(getBitWidth()),
1698 APIntOps::umax(-SMin
, SMax
) + 1);
1701 ConstantRange
ConstantRange::ctlz(bool ZeroIsPoison
) const {
1705 APInt Zero
= APInt::getZero(getBitWidth());
1706 if (ZeroIsPoison
&& contains(Zero
)) {
1707 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1708 // which a zero can appear:
1709 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1710 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1711 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1713 if (getLower().isZero()) {
1714 if ((getUpper() - 1).isZero()) {
1715 // We have in input interval of kind [0, 1). In this case we cannot
1716 // really help but return empty-set.
1720 // Compute the resulting range by excluding zero from Lower.
1721 return ConstantRange(
1722 APInt(getBitWidth(), (getUpper() - 1).countl_zero()),
1723 APInt(getBitWidth(), (getLower() + 1).countl_zero() + 1));
1724 } else if ((getUpper() - 1).isZero()) {
1725 // Compute the resulting range by excluding zero from Upper.
1726 return ConstantRange(Zero
,
1727 APInt(getBitWidth(), getLower().countl_zero() + 1));
1729 return ConstantRange(Zero
, APInt(getBitWidth(), getBitWidth()));
1733 // Zero is either safe or not in the range. The output range is composed by
1734 // the result of countLeadingZero of the two extremes.
1735 return getNonEmpty(APInt(getBitWidth(), getUnsignedMax().countl_zero()),
1736 APInt(getBitWidth(), getUnsignedMin().countl_zero() + 1));
1739 ConstantRange::OverflowResult
ConstantRange::unsignedAddMayOverflow(
1740 const ConstantRange
&Other
) const {
1741 if (isEmptySet() || Other
.isEmptySet())
1742 return OverflowResult::MayOverflow
;
1744 APInt Min
= getUnsignedMin(), Max
= getUnsignedMax();
1745 APInt OtherMin
= Other
.getUnsignedMin(), OtherMax
= Other
.getUnsignedMax();
1747 // a u+ b overflows high iff a u> ~b.
1748 if (Min
.ugt(~OtherMin
))
1749 return OverflowResult::AlwaysOverflowsHigh
;
1750 if (Max
.ugt(~OtherMax
))
1751 return OverflowResult::MayOverflow
;
1752 return OverflowResult::NeverOverflows
;
1755 ConstantRange::OverflowResult
ConstantRange::signedAddMayOverflow(
1756 const ConstantRange
&Other
) const {
1757 if (isEmptySet() || Other
.isEmptySet())
1758 return OverflowResult::MayOverflow
;
1760 APInt Min
= getSignedMin(), Max
= getSignedMax();
1761 APInt OtherMin
= Other
.getSignedMin(), OtherMax
= Other
.getSignedMax();
1763 APInt SignedMin
= APInt::getSignedMinValue(getBitWidth());
1764 APInt SignedMax
= APInt::getSignedMaxValue(getBitWidth());
1766 // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b.
1767 // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b.
1768 if (Min
.isNonNegative() && OtherMin
.isNonNegative() &&
1769 Min
.sgt(SignedMax
- OtherMin
))
1770 return OverflowResult::AlwaysOverflowsHigh
;
1771 if (Max
.isNegative() && OtherMax
.isNegative() &&
1772 Max
.slt(SignedMin
- OtherMax
))
1773 return OverflowResult::AlwaysOverflowsLow
;
1775 if (Max
.isNonNegative() && OtherMax
.isNonNegative() &&
1776 Max
.sgt(SignedMax
- OtherMax
))
1777 return OverflowResult::MayOverflow
;
1778 if (Min
.isNegative() && OtherMin
.isNegative() &&
1779 Min
.slt(SignedMin
- OtherMin
))
1780 return OverflowResult::MayOverflow
;
1782 return OverflowResult::NeverOverflows
;
1785 ConstantRange::OverflowResult
ConstantRange::unsignedSubMayOverflow(
1786 const ConstantRange
&Other
) const {
1787 if (isEmptySet() || Other
.isEmptySet())
1788 return OverflowResult::MayOverflow
;
1790 APInt Min
= getUnsignedMin(), Max
= getUnsignedMax();
1791 APInt OtherMin
= Other
.getUnsignedMin(), OtherMax
= Other
.getUnsignedMax();
1793 // a u- b overflows low iff a u< b.
1794 if (Max
.ult(OtherMin
))
1795 return OverflowResult::AlwaysOverflowsLow
;
1796 if (Min
.ult(OtherMax
))
1797 return OverflowResult::MayOverflow
;
1798 return OverflowResult::NeverOverflows
;
1801 ConstantRange::OverflowResult
ConstantRange::signedSubMayOverflow(
1802 const ConstantRange
&Other
) const {
1803 if (isEmptySet() || Other
.isEmptySet())
1804 return OverflowResult::MayOverflow
;
1806 APInt Min
= getSignedMin(), Max
= getSignedMax();
1807 APInt OtherMin
= Other
.getSignedMin(), OtherMax
= Other
.getSignedMax();
1809 APInt SignedMin
= APInt::getSignedMinValue(getBitWidth());
1810 APInt SignedMax
= APInt::getSignedMaxValue(getBitWidth());
1812 // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b.
1813 // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b.
1814 if (Min
.isNonNegative() && OtherMax
.isNegative() &&
1815 Min
.sgt(SignedMax
+ OtherMax
))
1816 return OverflowResult::AlwaysOverflowsHigh
;
1817 if (Max
.isNegative() && OtherMin
.isNonNegative() &&
1818 Max
.slt(SignedMin
+ OtherMin
))
1819 return OverflowResult::AlwaysOverflowsLow
;
1821 if (Max
.isNonNegative() && OtherMin
.isNegative() &&
1822 Max
.sgt(SignedMax
+ OtherMin
))
1823 return OverflowResult::MayOverflow
;
1824 if (Min
.isNegative() && OtherMax
.isNonNegative() &&
1825 Min
.slt(SignedMin
+ OtherMax
))
1826 return OverflowResult::MayOverflow
;
1828 return OverflowResult::NeverOverflows
;
1831 ConstantRange::OverflowResult
ConstantRange::unsignedMulMayOverflow(
1832 const ConstantRange
&Other
) const {
1833 if (isEmptySet() || Other
.isEmptySet())
1834 return OverflowResult::MayOverflow
;
1836 APInt Min
= getUnsignedMin(), Max
= getUnsignedMax();
1837 APInt OtherMin
= Other
.getUnsignedMin(), OtherMax
= Other
.getUnsignedMax();
1840 (void) Min
.umul_ov(OtherMin
, Overflow
);
1842 return OverflowResult::AlwaysOverflowsHigh
;
1844 (void) Max
.umul_ov(OtherMax
, Overflow
);
1846 return OverflowResult::MayOverflow
;
1848 return OverflowResult::NeverOverflows
;
1851 void ConstantRange::print(raw_ostream
&OS
) const {
1854 else if (isEmptySet())
1857 OS
<< "[" << Lower
<< "," << Upper
<< ")";
1860 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1861 LLVM_DUMP_METHOD
void ConstantRange::dump() const {
1866 ConstantRange
llvm::getConstantRangeFromMetadata(const MDNode
&Ranges
) {
1867 const unsigned NumRanges
= Ranges
.getNumOperands() / 2;
1868 assert(NumRanges
>= 1 && "Must have at least one range!");
1869 assert(Ranges
.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
1871 auto *FirstLow
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(0));
1872 auto *FirstHigh
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(1));
1874 ConstantRange
CR(FirstLow
->getValue(), FirstHigh
->getValue());
1876 for (unsigned i
= 1; i
< NumRanges
; ++i
) {
1877 auto *Low
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(2 * i
+ 0));
1878 auto *High
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(2 * i
+ 1));
1880 // Note: unionWith will potentially create a range that contains values not
1881 // contained in any of the original N ranges.
1882 CR
= CR
.unionWith(ConstantRange(Low
->getValue(), High
->getValue()));