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/IR/ConstantRange.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/Metadata.h"
32 #include "llvm/IR/Operator.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/KnownBits.h"
37 #include "llvm/Support/raw_ostream.h"
45 ConstantRange::ConstantRange(uint32_t BitWidth
, bool Full
)
46 : Lower(Full
? APInt::getMaxValue(BitWidth
) : APInt::getMinValue(BitWidth
)),
49 ConstantRange::ConstantRange(APInt V
)
50 : Lower(std::move(V
)), Upper(Lower
+ 1) {}
52 ConstantRange::ConstantRange(APInt L
, APInt U
)
53 : Lower(std::move(L
)), Upper(std::move(U
)) {
54 assert(Lower
.getBitWidth() == Upper
.getBitWidth() &&
55 "ConstantRange with unequal bit widths");
56 assert((Lower
!= Upper
|| (Lower
.isMaxValue() || Lower
.isMinValue())) &&
57 "Lower == Upper, but they aren't min or max value!");
60 ConstantRange
ConstantRange::fromKnownBits(const KnownBits
&Known
,
62 if (Known
.hasConflict())
63 return getEmpty(Known
.getBitWidth());
64 if (Known
.isUnknown())
65 return getFull(Known
.getBitWidth());
67 // For unsigned ranges, or signed ranges with known sign bit, create a simple
68 // range between the smallest and largest possible value.
69 if (!IsSigned
|| Known
.isNegative() || Known
.isNonNegative())
70 return ConstantRange(Known
.getMinValue(), Known
.getMaxValue() + 1);
72 // If we don't know the sign bit, pick the lower bound as a negative number
73 // and the upper bound as a non-negative one.
74 APInt Lower
= Known
.getMinValue(), Upper
= Known
.getMaxValue();
77 return ConstantRange(Lower
, Upper
+ 1);
80 KnownBits
ConstantRange::toKnownBits() const {
81 // TODO: We could return conflicting known bits here, but consumers are
82 // likely not prepared for that.
84 return KnownBits(getBitWidth());
86 // We can only retain the top bits that are the same between min and max.
87 APInt Min
= getUnsignedMin();
88 APInt Max
= getUnsignedMax();
89 KnownBits Known
= KnownBits::makeConstant(Min
);
90 if (std::optional
<unsigned> DifferentBit
=
91 APIntOps::GetMostSignificantDifferentBit(Min
, Max
)) {
92 Known
.Zero
.clearLowBits(*DifferentBit
+ 1);
93 Known
.One
.clearLowBits(*DifferentBit
+ 1);
98 ConstantRange
ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred
,
99 const ConstantRange
&CR
) {
103 uint32_t W
= CR
.getBitWidth();
106 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
107 case CmpInst::ICMP_EQ
:
109 case CmpInst::ICMP_NE
:
110 if (CR
.isSingleElement())
111 return ConstantRange(CR
.getUpper(), CR
.getLower());
113 case CmpInst::ICMP_ULT
: {
114 APInt
UMax(CR
.getUnsignedMax());
115 if (UMax
.isMinValue())
117 return ConstantRange(APInt::getMinValue(W
), std::move(UMax
));
119 case CmpInst::ICMP_SLT
: {
120 APInt
SMax(CR
.getSignedMax());
121 if (SMax
.isMinSignedValue())
123 return ConstantRange(APInt::getSignedMinValue(W
), std::move(SMax
));
125 case CmpInst::ICMP_ULE
:
126 return getNonEmpty(APInt::getMinValue(W
), CR
.getUnsignedMax() + 1);
127 case CmpInst::ICMP_SLE
:
128 return getNonEmpty(APInt::getSignedMinValue(W
), CR
.getSignedMax() + 1);
129 case CmpInst::ICMP_UGT
: {
130 APInt
UMin(CR
.getUnsignedMin());
131 if (UMin
.isMaxValue())
133 return ConstantRange(std::move(UMin
) + 1, APInt::getZero(W
));
135 case CmpInst::ICMP_SGT
: {
136 APInt
SMin(CR
.getSignedMin());
137 if (SMin
.isMaxSignedValue())
139 return ConstantRange(std::move(SMin
) + 1, APInt::getSignedMinValue(W
));
141 case CmpInst::ICMP_UGE
:
142 return getNonEmpty(CR
.getUnsignedMin(), APInt::getZero(W
));
143 case CmpInst::ICMP_SGE
:
144 return getNonEmpty(CR
.getSignedMin(), APInt::getSignedMinValue(W
));
148 ConstantRange
ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred
,
149 const ConstantRange
&CR
) {
150 // Follows from De-Morgan's laws:
152 // ~(~A union ~B) == A intersect B.
154 return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred
), CR
)
158 ConstantRange
ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred
,
160 // Computes the exact range that is equal to both the constant ranges returned
161 // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true
162 // when RHS is a singleton such as an APInt and so the assert is valid.
163 // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion
164 // returns [0,4) but makeSatisfyICmpRegion returns [0,2).
166 assert(makeAllowedICmpRegion(Pred
, C
) == makeSatisfyingICmpRegion(Pred
, C
));
167 return makeAllowedICmpRegion(Pred
, C
);
170 bool ConstantRange::areInsensitiveToSignednessOfICmpPredicate(
171 const ConstantRange
&CR1
, const ConstantRange
&CR2
) {
172 if (CR1
.isEmptySet() || CR2
.isEmptySet())
175 return (CR1
.isAllNonNegative() && CR2
.isAllNonNegative()) ||
176 (CR1
.isAllNegative() && CR2
.isAllNegative());
179 bool ConstantRange::areInsensitiveToSignednessOfInvertedICmpPredicate(
180 const ConstantRange
&CR1
, const ConstantRange
&CR2
) {
181 if (CR1
.isEmptySet() || CR2
.isEmptySet())
184 return (CR1
.isAllNonNegative() && CR2
.isAllNegative()) ||
185 (CR1
.isAllNegative() && CR2
.isAllNonNegative());
188 CmpInst::Predicate
ConstantRange::getEquivalentPredWithFlippedSignedness(
189 CmpInst::Predicate Pred
, const ConstantRange
&CR1
,
190 const ConstantRange
&CR2
) {
191 assert(CmpInst::isIntPredicate(Pred
) && CmpInst::isRelational(Pred
) &&
192 "Only for relational integer predicates!");
194 CmpInst::Predicate FlippedSignednessPred
=
195 ICmpInst::getFlippedSignednessPredicate(Pred
);
197 if (areInsensitiveToSignednessOfICmpPredicate(CR1
, CR2
))
198 return FlippedSignednessPred
;
200 if (areInsensitiveToSignednessOfInvertedICmpPredicate(CR1
, CR2
))
201 return CmpInst::getInversePredicate(FlippedSignednessPred
);
203 return CmpInst::Predicate::BAD_ICMP_PREDICATE
;
206 void ConstantRange::getEquivalentICmp(CmpInst::Predicate
&Pred
,
207 APInt
&RHS
, APInt
&Offset
) const {
208 Offset
= APInt(getBitWidth(), 0);
209 if (isFullSet() || isEmptySet()) {
210 Pred
= isEmptySet() ? CmpInst::ICMP_ULT
: CmpInst::ICMP_UGE
;
211 RHS
= APInt(getBitWidth(), 0);
212 } else if (auto *OnlyElt
= getSingleElement()) {
213 Pred
= CmpInst::ICMP_EQ
;
215 } else if (auto *OnlyMissingElt
= getSingleMissingElement()) {
216 Pred
= CmpInst::ICMP_NE
;
217 RHS
= *OnlyMissingElt
;
218 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
220 getLower().isMinSignedValue() ? CmpInst::ICMP_SLT
: CmpInst::ICMP_ULT
;
222 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
224 getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE
: CmpInst::ICMP_UGE
;
227 Pred
= CmpInst::ICMP_ULT
;
228 RHS
= getUpper() - getLower();
229 Offset
= -getLower();
232 assert(ConstantRange::makeExactICmpRegion(Pred
, RHS
) == add(Offset
) &&
236 bool ConstantRange::getEquivalentICmp(CmpInst::Predicate
&Pred
,
239 getEquivalentICmp(Pred
, RHS
, Offset
);
240 return Offset
.isZero();
243 bool ConstantRange::icmp(CmpInst::Predicate Pred
,
244 const ConstantRange
&Other
) const {
245 if (isEmptySet() || Other
.isEmptySet())
249 case CmpInst::ICMP_EQ
:
250 if (const APInt
*L
= getSingleElement())
251 if (const APInt
*R
= Other
.getSingleElement())
254 case CmpInst::ICMP_NE
:
255 return inverse().contains(Other
);
256 case CmpInst::ICMP_ULT
:
257 return getUnsignedMax().ult(Other
.getUnsignedMin());
258 case CmpInst::ICMP_ULE
:
259 return getUnsignedMax().ule(Other
.getUnsignedMin());
260 case CmpInst::ICMP_UGT
:
261 return getUnsignedMin().ugt(Other
.getUnsignedMax());
262 case CmpInst::ICMP_UGE
:
263 return getUnsignedMin().uge(Other
.getUnsignedMax());
264 case CmpInst::ICMP_SLT
:
265 return getSignedMax().slt(Other
.getSignedMin());
266 case CmpInst::ICMP_SLE
:
267 return getSignedMax().sle(Other
.getSignedMin());
268 case CmpInst::ICMP_SGT
:
269 return getSignedMin().sgt(Other
.getSignedMax());
270 case CmpInst::ICMP_SGE
:
271 return getSignedMin().sge(Other
.getSignedMax());
273 llvm_unreachable("Invalid ICmp predicate");
277 /// Exact mul nuw region for single element RHS.
278 static ConstantRange
makeExactMulNUWRegion(const APInt
&V
) {
279 unsigned BitWidth
= V
.getBitWidth();
281 return ConstantRange::getFull(V
.getBitWidth());
283 return ConstantRange::getNonEmpty(
284 APIntOps::RoundingUDiv(APInt::getMinValue(BitWidth
), V
,
285 APInt::Rounding::UP
),
286 APIntOps::RoundingUDiv(APInt::getMaxValue(BitWidth
), V
,
287 APInt::Rounding::DOWN
) + 1);
290 /// Exact mul nsw region for single element RHS.
291 static ConstantRange
makeExactMulNSWRegion(const APInt
&V
) {
292 // Handle 0 and -1 separately to avoid division by zero or overflow.
293 unsigned BitWidth
= V
.getBitWidth();
295 return ConstantRange::getFull(BitWidth
);
297 APInt MinValue
= APInt::getSignedMinValue(BitWidth
);
298 APInt MaxValue
= APInt::getSignedMaxValue(BitWidth
);
299 // e.g. Returning [-127, 127], represented as [-127, -128).
301 return ConstantRange(-MaxValue
, MinValue
);
304 if (V
.isNegative()) {
305 Lower
= APIntOps::RoundingSDiv(MaxValue
, V
, APInt::Rounding::UP
);
306 Upper
= APIntOps::RoundingSDiv(MinValue
, V
, APInt::Rounding::DOWN
);
308 Lower
= APIntOps::RoundingSDiv(MinValue
, V
, APInt::Rounding::UP
);
309 Upper
= APIntOps::RoundingSDiv(MaxValue
, V
, APInt::Rounding::DOWN
);
311 return ConstantRange::getNonEmpty(Lower
, Upper
+ 1);
315 ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp
,
316 const ConstantRange
&Other
,
317 unsigned NoWrapKind
) {
318 using OBO
= OverflowingBinaryOperator
;
320 assert(Instruction::isBinaryOp(BinOp
) && "Binary operators only!");
322 assert((NoWrapKind
== OBO::NoSignedWrap
||
323 NoWrapKind
== OBO::NoUnsignedWrap
) &&
324 "NoWrapKind invalid!");
326 bool Unsigned
= NoWrapKind
== OBO::NoUnsignedWrap
;
327 unsigned BitWidth
= Other
.getBitWidth();
331 llvm_unreachable("Unsupported binary op");
333 case Instruction::Add
: {
335 return getNonEmpty(APInt::getZero(BitWidth
), -Other
.getUnsignedMax());
337 APInt SignedMinVal
= APInt::getSignedMinValue(BitWidth
);
338 APInt SMin
= Other
.getSignedMin(), SMax
= Other
.getSignedMax();
340 SMin
.isNegative() ? SignedMinVal
- SMin
: SignedMinVal
,
341 SMax
.isStrictlyPositive() ? SignedMinVal
- SMax
: SignedMinVal
);
344 case Instruction::Sub
: {
346 return getNonEmpty(Other
.getUnsignedMax(), APInt::getMinValue(BitWidth
));
348 APInt SignedMinVal
= APInt::getSignedMinValue(BitWidth
);
349 APInt SMin
= Other
.getSignedMin(), SMax
= Other
.getSignedMax();
351 SMax
.isStrictlyPositive() ? SignedMinVal
+ SMax
: SignedMinVal
,
352 SMin
.isNegative() ? SignedMinVal
+ SMin
: SignedMinVal
);
355 case Instruction::Mul
:
357 return makeExactMulNUWRegion(Other
.getUnsignedMax());
359 // Avoid one makeExactMulNSWRegion() call for the common case of constants.
360 if (const APInt
*C
= Other
.getSingleElement())
361 return makeExactMulNSWRegion(*C
);
363 return makeExactMulNSWRegion(Other
.getSignedMin())
364 .intersectWith(makeExactMulNSWRegion(Other
.getSignedMax()));
366 case Instruction::Shl
: {
367 // For given range of shift amounts, if we ignore all illegal shift amounts
368 // (that always produce poison), what shift amount range is left?
369 ConstantRange ShAmt
= Other
.intersectWith(
370 ConstantRange(APInt(BitWidth
, 0), APInt(BitWidth
, (BitWidth
- 1) + 1)));
371 if (ShAmt
.isEmptySet()) {
372 // If the entire range of shift amounts is already poison-producing,
373 // then we can freely add more poison-producing flags ontop of that.
374 return getFull(BitWidth
);
376 // There are some legal shift amounts, we can compute conservatively-correct
377 // range of no-wrap inputs. Note that by now we have clamped the ShAmtUMax
378 // to be at most bitwidth-1, which results in most conservative range.
379 APInt ShAmtUMax
= ShAmt
.getUnsignedMax();
381 return getNonEmpty(APInt::getZero(BitWidth
),
382 APInt::getMaxValue(BitWidth
).lshr(ShAmtUMax
) + 1);
383 return getNonEmpty(APInt::getSignedMinValue(BitWidth
).ashr(ShAmtUMax
),
384 APInt::getSignedMaxValue(BitWidth
).ashr(ShAmtUMax
) + 1);
389 ConstantRange
ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp
,
391 unsigned NoWrapKind
) {
392 // makeGuaranteedNoWrapRegion() is exact for single-element ranges, as
393 // "for all" and "for any" coincide in this case.
394 return makeGuaranteedNoWrapRegion(BinOp
, ConstantRange(Other
), NoWrapKind
);
397 ConstantRange
ConstantRange::makeMaskNotEqualRange(const APInt
&Mask
,
399 unsigned BitWidth
= Mask
.getBitWidth();
402 return getFull(BitWidth
);
405 return getEmpty(BitWidth
);
407 // If (Val & Mask) != C, constrained to the non-equality being
408 // satisfiable, then the value must be larger than the lowest set bit of
409 // Mask, offset by constant C.
410 return ConstantRange::getNonEmpty(
411 APInt::getOneBitSet(BitWidth
, Mask
.countr_zero()) + C
, C
);
414 bool ConstantRange::isFullSet() const {
415 return Lower
== Upper
&& Lower
.isMaxValue();
418 bool ConstantRange::isEmptySet() const {
419 return Lower
== Upper
&& Lower
.isMinValue();
422 bool ConstantRange::isWrappedSet() const {
423 return Lower
.ugt(Upper
) && !Upper
.isZero();
426 bool ConstantRange::isUpperWrapped() const {
427 return Lower
.ugt(Upper
);
430 bool ConstantRange::isSignWrappedSet() const {
431 return Lower
.sgt(Upper
) && !Upper
.isMinSignedValue();
434 bool ConstantRange::isUpperSignWrapped() const {
435 return Lower
.sgt(Upper
);
439 ConstantRange::isSizeStrictlySmallerThan(const ConstantRange
&Other
) const {
440 assert(getBitWidth() == Other
.getBitWidth());
443 if (Other
.isFullSet())
445 return (Upper
- Lower
).ult(Other
.Upper
- Other
.Lower
);
449 ConstantRange::isSizeLargerThan(uint64_t MaxSize
) const {
450 // If this a full set, we need special handling to avoid needing an extra bit
451 // to represent the size.
453 return MaxSize
== 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize
- 1);
455 return (Upper
- Lower
).ugt(MaxSize
);
458 bool ConstantRange::isAllNegative() const {
459 // Empty set is all negative, full set is not.
465 return !isUpperSignWrapped() && !Upper
.isStrictlyPositive();
468 bool ConstantRange::isAllNonNegative() const {
469 // Empty and full set are automatically treated correctly.
470 return !isSignWrappedSet() && Lower
.isNonNegative();
473 bool ConstantRange::isAllPositive() const {
474 // Empty set is all positive, full set is not.
480 return !isSignWrappedSet() && Lower
.isStrictlyPositive();
483 APInt
ConstantRange::getUnsignedMax() const {
484 if (isFullSet() || isUpperWrapped())
485 return APInt::getMaxValue(getBitWidth());
486 return getUpper() - 1;
489 APInt
ConstantRange::getUnsignedMin() const {
490 if (isFullSet() || isWrappedSet())
491 return APInt::getMinValue(getBitWidth());
495 APInt
ConstantRange::getSignedMax() const {
496 if (isFullSet() || isUpperSignWrapped())
497 return APInt::getSignedMaxValue(getBitWidth());
498 return getUpper() - 1;
501 APInt
ConstantRange::getSignedMin() const {
502 if (isFullSet() || isSignWrappedSet())
503 return APInt::getSignedMinValue(getBitWidth());
507 bool ConstantRange::contains(const APInt
&V
) const {
511 if (!isUpperWrapped())
512 return Lower
.ule(V
) && V
.ult(Upper
);
513 return Lower
.ule(V
) || V
.ult(Upper
);
516 bool ConstantRange::contains(const ConstantRange
&Other
) const {
517 if (isFullSet() || Other
.isEmptySet()) return true;
518 if (isEmptySet() || Other
.isFullSet()) return false;
520 if (!isUpperWrapped()) {
521 if (Other
.isUpperWrapped())
524 return Lower
.ule(Other
.getLower()) && Other
.getUpper().ule(Upper
);
527 if (!Other
.isUpperWrapped())
528 return Other
.getUpper().ule(Upper
) ||
529 Lower
.ule(Other
.getLower());
531 return Other
.getUpper().ule(Upper
) && Lower
.ule(Other
.getLower());
534 unsigned ConstantRange::getActiveBits() const {
538 return getUnsignedMax().getActiveBits();
541 unsigned ConstantRange::getMinSignedBits() const {
545 return std::max(getSignedMin().getSignificantBits(),
546 getSignedMax().getSignificantBits());
549 ConstantRange
ConstantRange::subtract(const APInt
&Val
) const {
550 assert(Val
.getBitWidth() == getBitWidth() && "Wrong bit width");
551 // If the set is empty or full, don't modify the endpoints.
554 return ConstantRange(Lower
- Val
, Upper
- Val
);
557 ConstantRange
ConstantRange::difference(const ConstantRange
&CR
) const {
558 return intersectWith(CR
.inverse());
561 static ConstantRange
getPreferredRange(
562 const ConstantRange
&CR1
, const ConstantRange
&CR2
,
563 ConstantRange::PreferredRangeType Type
) {
564 if (Type
== ConstantRange::Unsigned
) {
565 if (!CR1
.isWrappedSet() && CR2
.isWrappedSet())
567 if (CR1
.isWrappedSet() && !CR2
.isWrappedSet())
569 } else if (Type
== ConstantRange::Signed
) {
570 if (!CR1
.isSignWrappedSet() && CR2
.isSignWrappedSet())
572 if (CR1
.isSignWrappedSet() && !CR2
.isSignWrappedSet())
576 if (CR1
.isSizeStrictlySmallerThan(CR2
))
581 ConstantRange
ConstantRange::intersectWith(const ConstantRange
&CR
,
582 PreferredRangeType Type
) const {
583 assert(getBitWidth() == CR
.getBitWidth() &&
584 "ConstantRange types don't agree!");
586 // Handle common cases.
587 if ( isEmptySet() || CR
.isFullSet()) return *this;
588 if (CR
.isEmptySet() || isFullSet()) return CR
;
590 if (!isUpperWrapped() && CR
.isUpperWrapped())
591 return CR
.intersectWith(*this, Type
);
593 if (!isUpperWrapped() && !CR
.isUpperWrapped()) {
594 if (Lower
.ult(CR
.Lower
)) {
597 if (Upper
.ule(CR
.Lower
))
602 if (Upper
.ult(CR
.Upper
))
603 return ConstantRange(CR
.Lower
, Upper
);
611 if (Upper
.ult(CR
.Upper
))
616 if (Lower
.ult(CR
.Upper
))
617 return ConstantRange(Lower
, CR
.Upper
);
624 if (isUpperWrapped() && !CR
.isUpperWrapped()) {
625 if (CR
.Lower
.ult(Upper
)) {
626 // ------U L--- : this
628 if (CR
.Upper
.ult(Upper
))
631 // ------U L--- : this
633 if (CR
.Upper
.ule(Lower
))
634 return ConstantRange(CR
.Lower
, Upper
);
636 // ------U L--- : this
638 return getPreferredRange(*this, CR
, Type
);
640 if (CR
.Lower
.ult(Lower
)) {
643 if (CR
.Upper
.ule(Lower
))
648 return ConstantRange(Lower
, CR
.Upper
);
651 // --U L------ : this
656 if (CR
.Upper
.ult(Upper
)) {
657 // ------U L-- : this
659 if (CR
.Lower
.ult(Upper
))
660 return getPreferredRange(*this, CR
, Type
);
664 if (CR
.Lower
.ult(Lower
))
665 return ConstantRange(Lower
, CR
.Upper
);
667 // ----U L---- : this
671 if (CR
.Upper
.ule(Lower
)) {
674 if (CR
.Lower
.ult(Lower
))
679 return ConstantRange(CR
.Lower
, Upper
);
682 // --U L------ : this
684 return getPreferredRange(*this, CR
, Type
);
687 ConstantRange
ConstantRange::unionWith(const ConstantRange
&CR
,
688 PreferredRangeType Type
) const {
689 assert(getBitWidth() == CR
.getBitWidth() &&
690 "ConstantRange types don't agree!");
692 if ( isFullSet() || CR
.isEmptySet()) return *this;
693 if (CR
.isFullSet() || isEmptySet()) return CR
;
695 if (!isUpperWrapped() && CR
.isUpperWrapped())
696 return CR
.unionWith(*this, Type
);
698 if (!isUpperWrapped() && !CR
.isUpperWrapped()) {
699 // L---U and L---U : this
704 if (CR
.Upper
.ult(Lower
) || Upper
.ult(CR
.Lower
))
705 return getPreferredRange(
706 ConstantRange(Lower
, CR
.Upper
), ConstantRange(CR
.Lower
, Upper
), Type
);
708 APInt L
= CR
.Lower
.ult(Lower
) ? CR
.Lower
: Lower
;
709 APInt U
= (CR
.Upper
- 1).ugt(Upper
- 1) ? CR
.Upper
: Upper
;
711 if (L
.isZero() && U
.isZero())
714 return ConstantRange(std::move(L
), std::move(U
));
717 if (!CR
.isUpperWrapped()) {
718 // ------U L----- and ------U L----- : this
720 if (CR
.Upper
.ule(Upper
) || CR
.Lower
.uge(Lower
))
723 // ------U L----- : this
725 if (CR
.Lower
.ule(Upper
) && Lower
.ule(CR
.Upper
))
728 // ----U L---- : this
733 if (Upper
.ult(CR
.Lower
) && CR
.Upper
.ult(Lower
))
734 return getPreferredRange(
735 ConstantRange(Lower
, CR
.Upper
), ConstantRange(CR
.Lower
, Upper
), Type
);
737 // ----U L----- : this
739 if (Upper
.ult(CR
.Lower
) && Lower
.ule(CR
.Upper
))
740 return ConstantRange(CR
.Lower
, Upper
);
742 // ------U L---- : this
744 assert(CR
.Lower
.ule(Upper
) && CR
.Upper
.ult(Lower
) &&
745 "ConstantRange::unionWith missed a case with one range wrapped");
746 return ConstantRange(Lower
, CR
.Upper
);
749 // ------U L---- and ------U L---- : this
750 // -U L----------- and ------------U L : CR
751 if (CR
.Lower
.ule(Upper
) || Lower
.ule(CR
.Upper
))
754 APInt L
= CR
.Lower
.ult(Lower
) ? CR
.Lower
: Lower
;
755 APInt U
= CR
.Upper
.ugt(Upper
) ? CR
.Upper
: Upper
;
757 return ConstantRange(std::move(L
), std::move(U
));
760 std::optional
<ConstantRange
>
761 ConstantRange::exactIntersectWith(const ConstantRange
&CR
) const {
762 // TODO: This can be implemented more efficiently.
763 ConstantRange Result
= intersectWith(CR
);
764 if (Result
== inverse().unionWith(CR
.inverse()).inverse())
769 std::optional
<ConstantRange
>
770 ConstantRange::exactUnionWith(const ConstantRange
&CR
) const {
771 // TODO: This can be implemented more efficiently.
772 ConstantRange Result
= unionWith(CR
);
773 if (Result
== inverse().intersectWith(CR
.inverse()).inverse())
778 ConstantRange
ConstantRange::castOp(Instruction::CastOps CastOp
,
779 uint32_t ResultBitWidth
) const {
782 llvm_unreachable("unsupported cast type");
783 case Instruction::Trunc
:
784 return truncate(ResultBitWidth
);
785 case Instruction::SExt
:
786 return signExtend(ResultBitWidth
);
787 case Instruction::ZExt
:
788 return zeroExtend(ResultBitWidth
);
789 case Instruction::BitCast
:
791 case Instruction::FPToUI
:
792 case Instruction::FPToSI
:
793 if (getBitWidth() == ResultBitWidth
)
796 return getFull(ResultBitWidth
);
797 case Instruction::UIToFP
: {
798 // TODO: use input range if available
799 auto BW
= getBitWidth();
800 APInt Min
= APInt::getMinValue(BW
);
801 APInt Max
= APInt::getMaxValue(BW
);
802 if (ResultBitWidth
> BW
) {
803 Min
= Min
.zext(ResultBitWidth
);
804 Max
= Max
.zext(ResultBitWidth
);
806 return getNonEmpty(std::move(Min
), std::move(Max
) + 1);
808 case Instruction::SIToFP
: {
809 // TODO: use input range if available
810 auto BW
= getBitWidth();
811 APInt SMin
= APInt::getSignedMinValue(BW
);
812 APInt SMax
= APInt::getSignedMaxValue(BW
);
813 if (ResultBitWidth
> BW
) {
814 SMin
= SMin
.sext(ResultBitWidth
);
815 SMax
= SMax
.sext(ResultBitWidth
);
817 return getNonEmpty(std::move(SMin
), std::move(SMax
) + 1);
819 case Instruction::FPTrunc
:
820 case Instruction::FPExt
:
821 case Instruction::IntToPtr
:
822 case Instruction::PtrToInt
:
823 case Instruction::AddrSpaceCast
:
824 // Conservatively return getFull set.
825 return getFull(ResultBitWidth
);
829 ConstantRange
ConstantRange::zeroExtend(uint32_t DstTySize
) const {
830 if (isEmptySet()) return getEmpty(DstTySize
);
832 unsigned SrcTySize
= getBitWidth();
833 assert(SrcTySize
< DstTySize
&& "Not a value extension");
834 if (isFullSet() || isUpperWrapped()) {
835 // Change into [0, 1 << src bit width)
836 APInt
LowerExt(DstTySize
, 0);
837 if (!Upper
) // special case: [X, 0) -- not really wrapping around
838 LowerExt
= Lower
.zext(DstTySize
);
839 return ConstantRange(std::move(LowerExt
),
840 APInt::getOneBitSet(DstTySize
, SrcTySize
));
843 return ConstantRange(Lower
.zext(DstTySize
), Upper
.zext(DstTySize
));
846 ConstantRange
ConstantRange::signExtend(uint32_t DstTySize
) const {
847 if (isEmptySet()) return getEmpty(DstTySize
);
849 unsigned SrcTySize
= getBitWidth();
850 assert(SrcTySize
< DstTySize
&& "Not a value extension");
852 // special case: [X, INT_MIN) -- not really wrapping around
853 if (Upper
.isMinSignedValue())
854 return ConstantRange(Lower
.sext(DstTySize
), Upper
.zext(DstTySize
));
856 if (isFullSet() || isSignWrappedSet()) {
857 return ConstantRange(APInt::getHighBitsSet(DstTySize
,DstTySize
-SrcTySize
+1),
858 APInt::getLowBitsSet(DstTySize
, SrcTySize
-1) + 1);
861 return ConstantRange(Lower
.sext(DstTySize
), Upper
.sext(DstTySize
));
864 ConstantRange
ConstantRange::truncate(uint32_t DstTySize
) const {
865 assert(getBitWidth() > DstTySize
&& "Not a value truncation");
867 return getEmpty(DstTySize
);
869 return getFull(DstTySize
);
871 APInt
LowerDiv(Lower
), UpperDiv(Upper
);
872 ConstantRange
Union(DstTySize
, /*isFullSet=*/false);
874 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
875 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
876 // then we do the union with [MaxValue, Upper)
877 if (isUpperWrapped()) {
878 // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
880 if (Upper
.getActiveBits() > DstTySize
|| Upper
.countr_one() == DstTySize
)
881 return getFull(DstTySize
);
883 Union
= ConstantRange(APInt::getMaxValue(DstTySize
),Upper
.trunc(DstTySize
));
884 UpperDiv
.setAllBits();
886 // Union covers the MaxValue case, so return if the remaining range is just
888 if (LowerDiv
== UpperDiv
)
892 // Chop off the most significant bits that are past the destination bitwidth.
893 if (LowerDiv
.getActiveBits() > DstTySize
) {
894 // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv.
895 APInt Adjust
= LowerDiv
& APInt::getBitsSetFrom(getBitWidth(), DstTySize
);
900 unsigned UpperDivWidth
= UpperDiv
.getActiveBits();
901 if (UpperDivWidth
<= DstTySize
)
902 return ConstantRange(LowerDiv
.trunc(DstTySize
),
903 UpperDiv
.trunc(DstTySize
)).unionWith(Union
);
905 // The truncated value wraps around. Check if we can do better than fullset.
906 if (UpperDivWidth
== DstTySize
+ 1) {
907 // Clear the MSB so that UpperDiv wraps around.
908 UpperDiv
.clearBit(DstTySize
);
909 if (UpperDiv
.ult(LowerDiv
))
910 return ConstantRange(LowerDiv
.trunc(DstTySize
),
911 UpperDiv
.trunc(DstTySize
)).unionWith(Union
);
914 return getFull(DstTySize
);
917 ConstantRange
ConstantRange::zextOrTrunc(uint32_t DstTySize
) const {
918 unsigned SrcTySize
= getBitWidth();
919 if (SrcTySize
> DstTySize
)
920 return truncate(DstTySize
);
921 if (SrcTySize
< DstTySize
)
922 return zeroExtend(DstTySize
);
926 ConstantRange
ConstantRange::sextOrTrunc(uint32_t DstTySize
) const {
927 unsigned SrcTySize
= getBitWidth();
928 if (SrcTySize
> DstTySize
)
929 return truncate(DstTySize
);
930 if (SrcTySize
< DstTySize
)
931 return signExtend(DstTySize
);
935 ConstantRange
ConstantRange::binaryOp(Instruction::BinaryOps BinOp
,
936 const ConstantRange
&Other
) const {
937 assert(Instruction::isBinaryOp(BinOp
) && "Binary operators only!");
940 case Instruction::Add
:
942 case Instruction::Sub
:
944 case Instruction::Mul
:
945 return multiply(Other
);
946 case Instruction::UDiv
:
948 case Instruction::SDiv
:
950 case Instruction::URem
:
952 case Instruction::SRem
:
954 case Instruction::Shl
:
956 case Instruction::LShr
:
958 case Instruction::AShr
:
960 case Instruction::And
:
961 return binaryAnd(Other
);
962 case Instruction::Or
:
963 return binaryOr(Other
);
964 case Instruction::Xor
:
965 return binaryXor(Other
);
966 // Note: floating point operations applied to abstract ranges are just
967 // ideal integer operations with a lossy representation
968 case Instruction::FAdd
:
970 case Instruction::FSub
:
972 case Instruction::FMul
:
973 return multiply(Other
);
975 // Conservatively return getFull set.
980 ConstantRange
ConstantRange::overflowingBinaryOp(Instruction::BinaryOps BinOp
,
981 const ConstantRange
&Other
,
982 unsigned NoWrapKind
) const {
983 assert(Instruction::isBinaryOp(BinOp
) && "Binary operators only!");
986 case Instruction::Add
:
987 return addWithNoWrap(Other
, NoWrapKind
);
988 case Instruction::Sub
:
989 return subWithNoWrap(Other
, NoWrapKind
);
990 case Instruction::Mul
:
991 return multiplyWithNoWrap(Other
, NoWrapKind
);
992 case Instruction::Shl
:
993 return shlWithNoWrap(Other
, NoWrapKind
);
995 // Don't know about this Overflowing Binary Operation.
996 // Conservatively fallback to plain binop handling.
997 return binaryOp(BinOp
, Other
);
1001 bool ConstantRange::isIntrinsicSupported(Intrinsic::ID IntrinsicID
) {
1002 switch (IntrinsicID
) {
1003 case Intrinsic::uadd_sat
:
1004 case Intrinsic::usub_sat
:
1005 case Intrinsic::sadd_sat
:
1006 case Intrinsic::ssub_sat
:
1007 case Intrinsic::umin
:
1008 case Intrinsic::umax
:
1009 case Intrinsic::smin
:
1010 case Intrinsic::smax
:
1011 case Intrinsic::abs
:
1012 case Intrinsic::ctlz
:
1013 case Intrinsic::cttz
:
1014 case Intrinsic::ctpop
:
1021 ConstantRange
ConstantRange::intrinsic(Intrinsic::ID IntrinsicID
,
1022 ArrayRef
<ConstantRange
> Ops
) {
1023 switch (IntrinsicID
) {
1024 case Intrinsic::uadd_sat
:
1025 return Ops
[0].uadd_sat(Ops
[1]);
1026 case Intrinsic::usub_sat
:
1027 return Ops
[0].usub_sat(Ops
[1]);
1028 case Intrinsic::sadd_sat
:
1029 return Ops
[0].sadd_sat(Ops
[1]);
1030 case Intrinsic::ssub_sat
:
1031 return Ops
[0].ssub_sat(Ops
[1]);
1032 case Intrinsic::umin
:
1033 return Ops
[0].umin(Ops
[1]);
1034 case Intrinsic::umax
:
1035 return Ops
[0].umax(Ops
[1]);
1036 case Intrinsic::smin
:
1037 return Ops
[0].smin(Ops
[1]);
1038 case Intrinsic::smax
:
1039 return Ops
[0].smax(Ops
[1]);
1040 case Intrinsic::abs
: {
1041 const APInt
*IntMinIsPoison
= Ops
[1].getSingleElement();
1042 assert(IntMinIsPoison
&& "Must be known (immarg)");
1043 assert(IntMinIsPoison
->getBitWidth() == 1 && "Must be boolean");
1044 return Ops
[0].abs(IntMinIsPoison
->getBoolValue());
1046 case Intrinsic::ctlz
: {
1047 const APInt
*ZeroIsPoison
= Ops
[1].getSingleElement();
1048 assert(ZeroIsPoison
&& "Must be known (immarg)");
1049 assert(ZeroIsPoison
->getBitWidth() == 1 && "Must be boolean");
1050 return Ops
[0].ctlz(ZeroIsPoison
->getBoolValue());
1052 case Intrinsic::cttz
: {
1053 const APInt
*ZeroIsPoison
= Ops
[1].getSingleElement();
1054 assert(ZeroIsPoison
&& "Must be known (immarg)");
1055 assert(ZeroIsPoison
->getBitWidth() == 1 && "Must be boolean");
1056 return Ops
[0].cttz(ZeroIsPoison
->getBoolValue());
1058 case Intrinsic::ctpop
:
1059 return Ops
[0].ctpop();
1061 assert(!isIntrinsicSupported(IntrinsicID
) && "Shouldn't be supported");
1062 llvm_unreachable("Unsupported intrinsic");
1067 ConstantRange::add(const ConstantRange
&Other
) const {
1068 if (isEmptySet() || Other
.isEmptySet())
1070 if (isFullSet() || Other
.isFullSet())
1073 APInt NewLower
= getLower() + Other
.getLower();
1074 APInt NewUpper
= getUpper() + Other
.getUpper() - 1;
1075 if (NewLower
== NewUpper
)
1078 ConstantRange X
= ConstantRange(std::move(NewLower
), std::move(NewUpper
));
1079 if (X
.isSizeStrictlySmallerThan(*this) ||
1080 X
.isSizeStrictlySmallerThan(Other
))
1081 // We've wrapped, therefore, full set.
1086 ConstantRange
ConstantRange::addWithNoWrap(const ConstantRange
&Other
,
1087 unsigned NoWrapKind
,
1088 PreferredRangeType RangeType
) const {
1089 // Calculate the range for "X + Y" which is guaranteed not to wrap(overflow).
1090 // (X is from this, and Y is from Other)
1091 if (isEmptySet() || Other
.isEmptySet())
1093 if (isFullSet() && Other
.isFullSet())
1096 using OBO
= OverflowingBinaryOperator
;
1097 ConstantRange Result
= add(Other
);
1099 // If an overflow happens for every value pair in these two constant ranges,
1100 // we must return Empty set. In this case, we get that for free, because we
1101 // get lucky that intersection of add() with uadd_sat()/sadd_sat() results
1104 if (NoWrapKind
& OBO::NoSignedWrap
)
1105 Result
= Result
.intersectWith(sadd_sat(Other
), RangeType
);
1107 if (NoWrapKind
& OBO::NoUnsignedWrap
)
1108 Result
= Result
.intersectWith(uadd_sat(Other
), RangeType
);
1114 ConstantRange::sub(const ConstantRange
&Other
) const {
1115 if (isEmptySet() || Other
.isEmptySet())
1117 if (isFullSet() || Other
.isFullSet())
1120 APInt NewLower
= getLower() - Other
.getUpper() + 1;
1121 APInt NewUpper
= getUpper() - Other
.getLower();
1122 if (NewLower
== NewUpper
)
1125 ConstantRange X
= ConstantRange(std::move(NewLower
), std::move(NewUpper
));
1126 if (X
.isSizeStrictlySmallerThan(*this) ||
1127 X
.isSizeStrictlySmallerThan(Other
))
1128 // We've wrapped, therefore, full set.
1133 ConstantRange
ConstantRange::subWithNoWrap(const ConstantRange
&Other
,
1134 unsigned NoWrapKind
,
1135 PreferredRangeType RangeType
) const {
1136 // Calculate the range for "X - Y" which is guaranteed not to wrap(overflow).
1137 // (X is from this, and Y is from Other)
1138 if (isEmptySet() || Other
.isEmptySet())
1140 if (isFullSet() && Other
.isFullSet())
1143 using OBO
= OverflowingBinaryOperator
;
1144 ConstantRange Result
= sub(Other
);
1146 // If an overflow happens for every value pair in these two constant ranges,
1147 // we must return Empty set. In signed case, we get that for free, because we
1148 // get lucky that intersection of sub() with ssub_sat() results in an
1149 // empty set. But for unsigned we must perform the overflow check manually.
1151 if (NoWrapKind
& OBO::NoSignedWrap
)
1152 Result
= Result
.intersectWith(ssub_sat(Other
), RangeType
);
1154 if (NoWrapKind
& OBO::NoUnsignedWrap
) {
1155 if (getUnsignedMax().ult(Other
.getUnsignedMin()))
1156 return getEmpty(); // Always overflows.
1157 Result
= Result
.intersectWith(usub_sat(Other
), RangeType
);
1164 ConstantRange::multiply(const ConstantRange
&Other
) const {
1165 // TODO: If either operand is a single element and the multiply is known to
1166 // be non-wrapping, round the result min and max value to the appropriate
1167 // multiple of that element. If wrapping is possible, at least adjust the
1168 // range according to the greatest power-of-two factor of the single element.
1170 if (isEmptySet() || Other
.isEmptySet())
1173 if (const APInt
*C
= getSingleElement()) {
1177 return ConstantRange(APInt::getZero(getBitWidth())).sub(Other
);
1180 if (const APInt
*C
= Other
.getSingleElement()) {
1184 return ConstantRange(APInt::getZero(getBitWidth())).sub(*this);
1187 // Multiplication is signedness-independent. However different ranges can be
1188 // obtained depending on how the input ranges are treated. These different
1189 // ranges are all conservatively correct, but one might be better than the
1190 // other. We calculate two ranges; one treating the inputs as unsigned
1191 // and the other signed, then return the smallest of these ranges.
1193 // Unsigned range first.
1194 APInt this_min
= getUnsignedMin().zext(getBitWidth() * 2);
1195 APInt this_max
= getUnsignedMax().zext(getBitWidth() * 2);
1196 APInt Other_min
= Other
.getUnsignedMin().zext(getBitWidth() * 2);
1197 APInt Other_max
= Other
.getUnsignedMax().zext(getBitWidth() * 2);
1199 ConstantRange Result_zext
= ConstantRange(this_min
* Other_min
,
1200 this_max
* Other_max
+ 1);
1201 ConstantRange UR
= Result_zext
.truncate(getBitWidth());
1203 // If the unsigned range doesn't wrap, and isn't negative then it's a range
1204 // from one positive number to another which is as good as we can generate.
1205 // In this case, skip the extra work of generating signed ranges which aren't
1206 // going to be better than this range.
1207 if (!UR
.isUpperWrapped() &&
1208 (UR
.getUpper().isNonNegative() || UR
.getUpper().isMinSignedValue()))
1211 // Now the signed range. Because we could be dealing with negative numbers
1212 // here, the lower bound is the smallest of the cartesian product of the
1213 // lower and upper ranges; for example:
1214 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1215 // Similarly for the upper bound, swapping min for max.
1217 this_min
= getSignedMin().sext(getBitWidth() * 2);
1218 this_max
= getSignedMax().sext(getBitWidth() * 2);
1219 Other_min
= Other
.getSignedMin().sext(getBitWidth() * 2);
1220 Other_max
= Other
.getSignedMax().sext(getBitWidth() * 2);
1222 auto L
= {this_min
* Other_min
, this_min
* Other_max
,
1223 this_max
* Other_min
, this_max
* Other_max
};
1224 auto Compare
= [](const APInt
&A
, const APInt
&B
) { return A
.slt(B
); };
1225 ConstantRange
Result_sext(std::min(L
, Compare
), std::max(L
, Compare
) + 1);
1226 ConstantRange SR
= Result_sext
.truncate(getBitWidth());
1228 return UR
.isSizeStrictlySmallerThan(SR
) ? UR
: SR
;
1232 ConstantRange::multiplyWithNoWrap(const ConstantRange
&Other
,
1233 unsigned NoWrapKind
,
1234 PreferredRangeType RangeType
) const {
1235 if (isEmptySet() || Other
.isEmptySet())
1237 if (isFullSet() && Other
.isFullSet())
1240 ConstantRange Result
= multiply(Other
);
1242 if (NoWrapKind
& OverflowingBinaryOperator::NoSignedWrap
)
1243 Result
= Result
.intersectWith(smul_sat(Other
), RangeType
);
1245 if (NoWrapKind
& OverflowingBinaryOperator::NoUnsignedWrap
)
1246 Result
= Result
.intersectWith(umul_sat(Other
), RangeType
);
1248 // mul nsw nuw X, Y s>= 0 if X s> 1 or Y s> 1
1249 if ((NoWrapKind
== (OverflowingBinaryOperator::NoSignedWrap
|
1250 OverflowingBinaryOperator::NoUnsignedWrap
)) &&
1251 !Result
.isAllNonNegative()) {
1252 if (getSignedMin().sgt(1) || Other
.getSignedMin().sgt(1))
1253 Result
= Result
.intersectWith(
1254 getNonEmpty(APInt::getZero(getBitWidth()),
1255 APInt::getSignedMinValue(getBitWidth())),
1262 ConstantRange
ConstantRange::smul_fast(const ConstantRange
&Other
) const {
1263 if (isEmptySet() || Other
.isEmptySet())
1266 APInt Min
= getSignedMin();
1267 APInt Max
= getSignedMax();
1268 APInt OtherMin
= Other
.getSignedMin();
1269 APInt OtherMax
= Other
.getSignedMax();
1271 bool O1
, O2
, O3
, O4
;
1272 auto Muls
= {Min
.smul_ov(OtherMin
, O1
), Min
.smul_ov(OtherMax
, O2
),
1273 Max
.smul_ov(OtherMin
, O3
), Max
.smul_ov(OtherMax
, O4
)};
1274 if (O1
|| O2
|| O3
|| O4
)
1277 auto Compare
= [](const APInt
&A
, const APInt
&B
) { return A
.slt(B
); };
1278 return getNonEmpty(std::min(Muls
, Compare
), std::max(Muls
, Compare
) + 1);
1282 ConstantRange::smax(const ConstantRange
&Other
) const {
1283 // X smax Y is: range(smax(X_smin, Y_smin),
1284 // smax(X_smax, Y_smax))
1285 if (isEmptySet() || Other
.isEmptySet())
1287 APInt NewL
= APIntOps::smax(getSignedMin(), Other
.getSignedMin());
1288 APInt NewU
= APIntOps::smax(getSignedMax(), Other
.getSignedMax()) + 1;
1289 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1290 if (isSignWrappedSet() || Other
.isSignWrappedSet())
1291 return Res
.intersectWith(unionWith(Other
, Signed
), Signed
);
1296 ConstantRange::umax(const ConstantRange
&Other
) const {
1297 // X umax Y is: range(umax(X_umin, Y_umin),
1298 // umax(X_umax, Y_umax))
1299 if (isEmptySet() || Other
.isEmptySet())
1301 APInt NewL
= APIntOps::umax(getUnsignedMin(), Other
.getUnsignedMin());
1302 APInt NewU
= APIntOps::umax(getUnsignedMax(), Other
.getUnsignedMax()) + 1;
1303 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1304 if (isWrappedSet() || Other
.isWrappedSet())
1305 return Res
.intersectWith(unionWith(Other
, Unsigned
), Unsigned
);
1310 ConstantRange::smin(const ConstantRange
&Other
) const {
1311 // X smin Y is: range(smin(X_smin, Y_smin),
1312 // smin(X_smax, Y_smax))
1313 if (isEmptySet() || Other
.isEmptySet())
1315 APInt NewL
= APIntOps::smin(getSignedMin(), Other
.getSignedMin());
1316 APInt NewU
= APIntOps::smin(getSignedMax(), Other
.getSignedMax()) + 1;
1317 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1318 if (isSignWrappedSet() || Other
.isSignWrappedSet())
1319 return Res
.intersectWith(unionWith(Other
, Signed
), Signed
);
1324 ConstantRange::umin(const ConstantRange
&Other
) const {
1325 // X umin Y is: range(umin(X_umin, Y_umin),
1326 // umin(X_umax, Y_umax))
1327 if (isEmptySet() || Other
.isEmptySet())
1329 APInt NewL
= APIntOps::umin(getUnsignedMin(), Other
.getUnsignedMin());
1330 APInt NewU
= APIntOps::umin(getUnsignedMax(), Other
.getUnsignedMax()) + 1;
1331 ConstantRange Res
= getNonEmpty(std::move(NewL
), std::move(NewU
));
1332 if (isWrappedSet() || Other
.isWrappedSet())
1333 return Res
.intersectWith(unionWith(Other
, Unsigned
), Unsigned
);
1338 ConstantRange::udiv(const ConstantRange
&RHS
) const {
1339 if (isEmptySet() || RHS
.isEmptySet() || RHS
.getUnsignedMax().isZero())
1342 APInt Lower
= getUnsignedMin().udiv(RHS
.getUnsignedMax());
1344 APInt RHS_umin
= RHS
.getUnsignedMin();
1345 if (RHS_umin
.isZero()) {
1346 // We want the lowest value in RHS excluding zero. Usually that would be 1
1347 // except for a range in the form of [X, 1) in which case it would be X.
1348 if (RHS
.getUpper() == 1)
1349 RHS_umin
= RHS
.getLower();
1354 APInt Upper
= getUnsignedMax().udiv(RHS_umin
) + 1;
1355 return getNonEmpty(std::move(Lower
), std::move(Upper
));
1358 ConstantRange
ConstantRange::sdiv(const ConstantRange
&RHS
) const {
1359 // We split up the LHS and RHS into positive and negative components
1360 // and then also compute the positive and negative components of the result
1361 // separately by combining division results with the appropriate signs.
1362 APInt Zero
= APInt::getZero(getBitWidth());
1363 APInt SignedMin
= APInt::getSignedMinValue(getBitWidth());
1364 // There are no positive 1-bit values. The 1 would get interpreted as -1.
1365 ConstantRange PosFilter
=
1366 getBitWidth() == 1 ? getEmpty()
1367 : ConstantRange(APInt(getBitWidth(), 1), SignedMin
);
1368 ConstantRange
NegFilter(SignedMin
, Zero
);
1369 ConstantRange PosL
= intersectWith(PosFilter
);
1370 ConstantRange NegL
= intersectWith(NegFilter
);
1371 ConstantRange PosR
= RHS
.intersectWith(PosFilter
);
1372 ConstantRange NegR
= RHS
.intersectWith(NegFilter
);
1374 ConstantRange PosRes
= getEmpty();
1375 if (!PosL
.isEmptySet() && !PosR
.isEmptySet())
1377 PosRes
= ConstantRange(PosL
.Lower
.sdiv(PosR
.Upper
- 1),
1378 (PosL
.Upper
- 1).sdiv(PosR
.Lower
) + 1);
1380 if (!NegL
.isEmptySet() && !NegR
.isEmptySet()) {
1383 // We need to deal with one tricky case here: SignedMin / -1 is UB on the
1384 // IR level, so we'll want to exclude this case when calculating bounds.
1385 // (For APInts the operation is well-defined and yields SignedMin.) We
1386 // handle this by dropping either SignedMin from the LHS or -1 from the RHS.
1387 APInt Lo
= (NegL
.Upper
- 1).sdiv(NegR
.Lower
);
1388 if (NegL
.Lower
.isMinSignedValue() && NegR
.Upper
.isZero()) {
1389 // Remove -1 from the LHS. Skip if it's the only element, as this would
1390 // leave us with an empty set.
1391 if (!NegR
.Lower
.isAllOnes()) {
1393 if (RHS
.Lower
.isAllOnes())
1394 // Negative part of [-1, X] without -1 is [SignedMin, X].
1395 AdjNegRUpper
= RHS
.Upper
;
1397 // [X, -1] without -1 is [X, -2].
1398 AdjNegRUpper
= NegR
.Upper
- 1;
1400 PosRes
= PosRes
.unionWith(
1401 ConstantRange(Lo
, NegL
.Lower
.sdiv(AdjNegRUpper
- 1) + 1));
1404 // Remove SignedMin from the RHS. Skip if it's the only element, as this
1405 // would leave us with an empty set.
1406 if (NegL
.Upper
!= SignedMin
+ 1) {
1408 if (Upper
== SignedMin
+ 1)
1409 // Negative part of [X, SignedMin] without SignedMin is [X, -1].
1410 AdjNegLLower
= Lower
;
1412 // [SignedMin, X] without SignedMin is [SignedMin + 1, X].
1413 AdjNegLLower
= NegL
.Lower
+ 1;
1415 PosRes
= PosRes
.unionWith(
1416 ConstantRange(std::move(Lo
),
1417 AdjNegLLower
.sdiv(NegR
.Upper
- 1) + 1));
1420 PosRes
= PosRes
.unionWith(
1421 ConstantRange(std::move(Lo
), NegL
.Lower
.sdiv(NegR
.Upper
- 1) + 1));
1425 ConstantRange NegRes
= getEmpty();
1426 if (!PosL
.isEmptySet() && !NegR
.isEmptySet())
1428 NegRes
= ConstantRange((PosL
.Upper
- 1).sdiv(NegR
.Upper
- 1),
1429 PosL
.Lower
.sdiv(NegR
.Lower
) + 1);
1431 if (!NegL
.isEmptySet() && !PosR
.isEmptySet())
1433 NegRes
= NegRes
.unionWith(
1434 ConstantRange(NegL
.Lower
.sdiv(PosR
.Lower
),
1435 (NegL
.Upper
- 1).sdiv(PosR
.Upper
- 1) + 1));
1437 // Prefer a non-wrapping signed range here.
1438 ConstantRange Res
= NegRes
.unionWith(PosRes
, PreferredRangeType::Signed
);
1440 // Preserve the zero that we dropped when splitting the LHS by sign.
1441 if (contains(Zero
) && (!PosR
.isEmptySet() || !NegR
.isEmptySet()))
1442 Res
= Res
.unionWith(ConstantRange(Zero
));
1446 ConstantRange
ConstantRange::urem(const ConstantRange
&RHS
) const {
1447 if (isEmptySet() || RHS
.isEmptySet() || RHS
.getUnsignedMax().isZero())
1450 if (const APInt
*RHSInt
= RHS
.getSingleElement()) {
1451 // UREM by null is UB.
1452 if (RHSInt
->isZero())
1454 // Use APInt's implementation of UREM for single element ranges.
1455 if (const APInt
*LHSInt
= getSingleElement())
1456 return {LHSInt
->urem(*RHSInt
)};
1459 // L % R for L < R is L.
1460 if (getUnsignedMax().ult(RHS
.getUnsignedMin()))
1463 // L % R is <= L and < R.
1464 APInt Upper
= APIntOps::umin(getUnsignedMax(), RHS
.getUnsignedMax() - 1) + 1;
1465 return getNonEmpty(APInt::getZero(getBitWidth()), std::move(Upper
));
1468 ConstantRange
ConstantRange::srem(const ConstantRange
&RHS
) const {
1469 if (isEmptySet() || RHS
.isEmptySet())
1472 if (const APInt
*RHSInt
= RHS
.getSingleElement()) {
1473 // SREM by null is UB.
1474 if (RHSInt
->isZero())
1476 // Use APInt's implementation of SREM for single element ranges.
1477 if (const APInt
*LHSInt
= getSingleElement())
1478 return {LHSInt
->srem(*RHSInt
)};
1481 ConstantRange AbsRHS
= RHS
.abs();
1482 APInt MinAbsRHS
= AbsRHS
.getUnsignedMin();
1483 APInt MaxAbsRHS
= AbsRHS
.getUnsignedMax();
1485 // Modulus by zero is UB.
1486 if (MaxAbsRHS
.isZero())
1489 if (MinAbsRHS
.isZero())
1492 APInt MinLHS
= getSignedMin(), MaxLHS
= getSignedMax();
1494 if (MinLHS
.isNonNegative()) {
1495 // L % R for L < R is L.
1496 if (MaxLHS
.ult(MinAbsRHS
))
1499 // L % R is <= L and < R.
1500 APInt Upper
= APIntOps::umin(MaxLHS
, MaxAbsRHS
- 1) + 1;
1501 return ConstantRange(APInt::getZero(getBitWidth()), std::move(Upper
));
1504 // Same basic logic as above, but the result is negative.
1505 if (MaxLHS
.isNegative()) {
1506 if (MinLHS
.ugt(-MinAbsRHS
))
1509 APInt Lower
= APIntOps::umax(MinLHS
, -MaxAbsRHS
+ 1);
1510 return ConstantRange(std::move(Lower
), APInt(getBitWidth(), 1));
1513 // LHS range crosses zero.
1514 APInt Lower
= APIntOps::umax(MinLHS
, -MaxAbsRHS
+ 1);
1515 APInt Upper
= APIntOps::umin(MaxLHS
, MaxAbsRHS
- 1) + 1;
1516 return ConstantRange(std::move(Lower
), std::move(Upper
));
1519 ConstantRange
ConstantRange::binaryNot() const {
1520 return ConstantRange(APInt::getAllOnes(getBitWidth())).sub(*this);
1523 ConstantRange
ConstantRange::binaryAnd(const ConstantRange
&Other
) const {
1524 if (isEmptySet() || Other
.isEmptySet())
1527 ConstantRange KnownBitsRange
=
1528 fromKnownBits(toKnownBits() & Other
.toKnownBits(), false);
1529 ConstantRange UMinUMaxRange
=
1530 getNonEmpty(APInt::getZero(getBitWidth()),
1531 APIntOps::umin(Other
.getUnsignedMax(), getUnsignedMax()) + 1);
1532 return KnownBitsRange
.intersectWith(UMinUMaxRange
);
1535 ConstantRange
ConstantRange::binaryOr(const ConstantRange
&Other
) const {
1536 if (isEmptySet() || Other
.isEmptySet())
1539 ConstantRange KnownBitsRange
=
1540 fromKnownBits(toKnownBits() | Other
.toKnownBits(), false);
1541 // Upper wrapped range.
1542 ConstantRange UMaxUMinRange
=
1543 getNonEmpty(APIntOps::umax(getUnsignedMin(), Other
.getUnsignedMin()),
1544 APInt::getZero(getBitWidth()));
1545 return KnownBitsRange
.intersectWith(UMaxUMinRange
);
1548 ConstantRange
ConstantRange::binaryXor(const ConstantRange
&Other
) const {
1549 if (isEmptySet() || Other
.isEmptySet())
1552 // Use APInt's implementation of XOR for single element ranges.
1553 if (isSingleElement() && Other
.isSingleElement())
1554 return {*getSingleElement() ^ *Other
.getSingleElement()};
1556 // Special-case binary complement, since we can give a precise answer.
1557 if (Other
.isSingleElement() && Other
.getSingleElement()->isAllOnes())
1559 if (isSingleElement() && getSingleElement()->isAllOnes())
1560 return Other
.binaryNot();
1562 KnownBits LHSKnown
= toKnownBits();
1563 KnownBits RHSKnown
= Other
.toKnownBits();
1564 KnownBits Known
= LHSKnown
^ RHSKnown
;
1565 ConstantRange CR
= fromKnownBits(Known
, /*IsSigned*/ false);
1566 // Typically the following code doesn't improve the result if BW = 1.
1567 if (getBitWidth() == 1)
1570 // If LHS is known to be the subset of RHS, treat LHS ^ RHS as RHS -nuw/nsw
1571 // LHS. If RHS is known to be the subset of LHS, treat LHS ^ RHS as LHS
1573 if ((~LHSKnown
.Zero
).isSubsetOf(RHSKnown
.One
))
1574 CR
= CR
.intersectWith(Other
.sub(*this), PreferredRangeType::Unsigned
);
1575 else if ((~RHSKnown
.Zero
).isSubsetOf(LHSKnown
.One
))
1576 CR
= CR
.intersectWith(this->sub(Other
), PreferredRangeType::Unsigned
);
1581 ConstantRange::shl(const ConstantRange
&Other
) const {
1582 if (isEmptySet() || Other
.isEmptySet())
1585 APInt Min
= getUnsignedMin();
1586 APInt Max
= getUnsignedMax();
1587 if (const APInt
*RHS
= Other
.getSingleElement()) {
1588 unsigned BW
= getBitWidth();
1592 unsigned EqualLeadingBits
= (Min
^ Max
).countl_zero();
1593 if (RHS
->ule(EqualLeadingBits
))
1594 return getNonEmpty(Min
<< *RHS
, (Max
<< *RHS
) + 1);
1596 return getNonEmpty(APInt::getZero(BW
),
1597 APInt::getBitsSetFrom(BW
, RHS
->getZExtValue()) + 1);
1600 APInt OtherMax
= Other
.getUnsignedMax();
1601 if (isAllNegative() && OtherMax
.ule(Min
.countl_one())) {
1602 // For negative numbers, if the shift does not overflow in a signed sense,
1603 // a larger shift will make the number smaller.
1604 Max
<<= Other
.getUnsignedMin();
1606 return ConstantRange::getNonEmpty(std::move(Min
), std::move(Max
) + 1);
1609 // There's overflow!
1610 if (OtherMax
.ugt(Max
.countl_zero()))
1613 // FIXME: implement the other tricky cases
1615 Min
<<= Other
.getUnsignedMin();
1618 return ConstantRange::getNonEmpty(std::move(Min
), std::move(Max
) + 1);
1621 static ConstantRange
computeShlNUW(const ConstantRange
&LHS
,
1622 const ConstantRange
&RHS
) {
1623 unsigned BitWidth
= LHS
.getBitWidth();
1625 APInt LHSMin
= LHS
.getUnsignedMin();
1626 unsigned RHSMin
= RHS
.getUnsignedMin().getLimitedValue(BitWidth
);
1627 APInt MinShl
= LHSMin
.ushl_ov(RHSMin
, Overflow
);
1629 return ConstantRange::getEmpty(BitWidth
);
1630 APInt LHSMax
= LHS
.getUnsignedMax();
1631 unsigned RHSMax
= RHS
.getUnsignedMax().getLimitedValue(BitWidth
);
1632 APInt MaxShl
= MinShl
;
1633 unsigned MaxShAmt
= LHSMax
.countLeadingZeros();
1634 if (RHSMin
<= MaxShAmt
)
1635 MaxShl
= LHSMax
<< std::min(RHSMax
, MaxShAmt
);
1636 RHSMin
= std::max(RHSMin
, MaxShAmt
+ 1);
1637 RHSMax
= std::min(RHSMax
, LHSMin
.countLeadingZeros());
1638 if (RHSMin
<= RHSMax
)
1639 MaxShl
= APIntOps::umax(MaxShl
,
1640 APInt::getHighBitsSet(BitWidth
, BitWidth
- RHSMin
));
1641 return ConstantRange::getNonEmpty(MinShl
, MaxShl
+ 1);
1644 static ConstantRange
computeShlNSWWithNNegLHS(const APInt
&LHSMin
,
1645 const APInt
&LHSMax
,
1648 unsigned BitWidth
= LHSMin
.getBitWidth();
1650 APInt MinShl
= LHSMin
.sshl_ov(RHSMin
, Overflow
);
1652 return ConstantRange::getEmpty(BitWidth
);
1653 APInt MaxShl
= MinShl
;
1654 unsigned MaxShAmt
= LHSMax
.countLeadingZeros() - 1;
1655 if (RHSMin
<= MaxShAmt
)
1656 MaxShl
= LHSMax
<< std::min(RHSMax
, MaxShAmt
);
1657 RHSMin
= std::max(RHSMin
, MaxShAmt
+ 1);
1658 RHSMax
= std::min(RHSMax
, LHSMin
.countLeadingZeros() - 1);
1659 if (RHSMin
<= RHSMax
)
1660 MaxShl
= APIntOps::umax(MaxShl
,
1661 APInt::getBitsSet(BitWidth
, RHSMin
, BitWidth
- 1));
1662 return ConstantRange::getNonEmpty(MinShl
, MaxShl
+ 1);
1665 static ConstantRange
computeShlNSWWithNegLHS(const APInt
&LHSMin
,
1666 const APInt
&LHSMax
,
1667 unsigned RHSMin
, unsigned RHSMax
) {
1668 unsigned BitWidth
= LHSMin
.getBitWidth();
1670 APInt MaxShl
= LHSMax
.sshl_ov(RHSMin
, Overflow
);
1672 return ConstantRange::getEmpty(BitWidth
);
1673 APInt MinShl
= MaxShl
;
1674 unsigned MaxShAmt
= LHSMin
.countLeadingOnes() - 1;
1675 if (RHSMin
<= MaxShAmt
)
1676 MinShl
= LHSMin
.shl(std::min(RHSMax
, MaxShAmt
));
1677 RHSMin
= std::max(RHSMin
, MaxShAmt
+ 1);
1678 RHSMax
= std::min(RHSMax
, LHSMax
.countLeadingOnes() - 1);
1679 if (RHSMin
<= RHSMax
)
1680 MinShl
= APInt::getSignMask(BitWidth
);
1681 return ConstantRange::getNonEmpty(MinShl
, MaxShl
+ 1);
1684 static ConstantRange
computeShlNSW(const ConstantRange
&LHS
,
1685 const ConstantRange
&RHS
) {
1686 unsigned BitWidth
= LHS
.getBitWidth();
1687 unsigned RHSMin
= RHS
.getUnsignedMin().getLimitedValue(BitWidth
);
1688 unsigned RHSMax
= RHS
.getUnsignedMax().getLimitedValue(BitWidth
);
1689 APInt LHSMin
= LHS
.getSignedMin();
1690 APInt LHSMax
= LHS
.getSignedMax();
1691 if (LHSMin
.isNonNegative())
1692 return computeShlNSWWithNNegLHS(LHSMin
, LHSMax
, RHSMin
, RHSMax
);
1693 else if (LHSMax
.isNegative())
1694 return computeShlNSWWithNegLHS(LHSMin
, LHSMax
, RHSMin
, RHSMax
);
1695 return computeShlNSWWithNNegLHS(APInt::getZero(BitWidth
), LHSMax
, RHSMin
,
1697 .unionWith(computeShlNSWWithNegLHS(LHSMin
, APInt::getAllOnes(BitWidth
),
1699 ConstantRange::Signed
);
1702 ConstantRange
ConstantRange::shlWithNoWrap(const ConstantRange
&Other
,
1703 unsigned NoWrapKind
,
1704 PreferredRangeType RangeType
) const {
1705 if (isEmptySet() || Other
.isEmptySet())
1708 switch (NoWrapKind
) {
1711 case OverflowingBinaryOperator::NoSignedWrap
:
1712 return computeShlNSW(*this, Other
);
1713 case OverflowingBinaryOperator::NoUnsignedWrap
:
1714 return computeShlNUW(*this, Other
);
1715 case OverflowingBinaryOperator::NoSignedWrap
|
1716 OverflowingBinaryOperator::NoUnsignedWrap
:
1717 return computeShlNSW(*this, Other
)
1718 .intersectWith(computeShlNUW(*this, Other
), RangeType
);
1720 llvm_unreachable("Invalid NoWrapKind");
1725 ConstantRange::lshr(const ConstantRange
&Other
) const {
1726 if (isEmptySet() || Other
.isEmptySet())
1729 APInt max
= getUnsignedMax().lshr(Other
.getUnsignedMin()) + 1;
1730 APInt min
= getUnsignedMin().lshr(Other
.getUnsignedMax());
1731 return getNonEmpty(std::move(min
), std::move(max
));
1735 ConstantRange::ashr(const ConstantRange
&Other
) const {
1736 if (isEmptySet() || Other
.isEmptySet())
1739 // May straddle zero, so handle both positive and negative cases.
1740 // 'PosMax' is the upper bound of the result of the ashr
1741 // operation, when Upper of the LHS of ashr is a non-negative.
1742 // number. Since ashr of a non-negative number will result in a
1743 // smaller number, the Upper value of LHS is shifted right with
1744 // the minimum value of 'Other' instead of the maximum value.
1745 APInt PosMax
= getSignedMax().ashr(Other
.getUnsignedMin()) + 1;
1747 // 'PosMin' is the lower bound of the result of the ashr
1748 // operation, when Lower of the LHS is a non-negative number.
1749 // Since ashr of a non-negative number will result in a smaller
1750 // number, the Lower value of LHS is shifted right with the
1751 // maximum value of 'Other'.
1752 APInt PosMin
= getSignedMin().ashr(Other
.getUnsignedMax());
1754 // 'NegMax' is the upper bound of the result of the ashr
1755 // operation, when Upper of the LHS of ashr is a negative number.
1756 // Since 'ashr' of a negative number will result in a bigger
1757 // number, the Upper value of LHS is shifted right with the
1758 // maximum value of 'Other'.
1759 APInt NegMax
= getSignedMax().ashr(Other
.getUnsignedMax()) + 1;
1761 // 'NegMin' is the lower bound of the result of the ashr
1762 // operation, when Lower of the LHS of ashr is a negative number.
1763 // Since 'ashr' of a negative number will result in a bigger
1764 // number, the Lower value of LHS is shifted right with the
1765 // minimum value of 'Other'.
1766 APInt NegMin
= getSignedMin().ashr(Other
.getUnsignedMin());
1769 if (getSignedMin().isNonNegative()) {
1770 // Upper and Lower of LHS are non-negative.
1773 } else if (getSignedMax().isNegative()) {
1774 // Upper and Lower of LHS are negative.
1778 // Upper is non-negative and Lower is negative.
1782 return getNonEmpty(std::move(min
), std::move(max
));
1785 ConstantRange
ConstantRange::uadd_sat(const ConstantRange
&Other
) const {
1786 if (isEmptySet() || Other
.isEmptySet())
1789 APInt NewL
= getUnsignedMin().uadd_sat(Other
.getUnsignedMin());
1790 APInt NewU
= getUnsignedMax().uadd_sat(Other
.getUnsignedMax()) + 1;
1791 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1794 ConstantRange
ConstantRange::sadd_sat(const ConstantRange
&Other
) const {
1795 if (isEmptySet() || Other
.isEmptySet())
1798 APInt NewL
= getSignedMin().sadd_sat(Other
.getSignedMin());
1799 APInt NewU
= getSignedMax().sadd_sat(Other
.getSignedMax()) + 1;
1800 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1803 ConstantRange
ConstantRange::usub_sat(const ConstantRange
&Other
) const {
1804 if (isEmptySet() || Other
.isEmptySet())
1807 APInt NewL
= getUnsignedMin().usub_sat(Other
.getUnsignedMax());
1808 APInt NewU
= getUnsignedMax().usub_sat(Other
.getUnsignedMin()) + 1;
1809 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1812 ConstantRange
ConstantRange::ssub_sat(const ConstantRange
&Other
) const {
1813 if (isEmptySet() || Other
.isEmptySet())
1816 APInt NewL
= getSignedMin().ssub_sat(Other
.getSignedMax());
1817 APInt NewU
= getSignedMax().ssub_sat(Other
.getSignedMin()) + 1;
1818 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1821 ConstantRange
ConstantRange::umul_sat(const ConstantRange
&Other
) const {
1822 if (isEmptySet() || Other
.isEmptySet())
1825 APInt NewL
= getUnsignedMin().umul_sat(Other
.getUnsignedMin());
1826 APInt NewU
= getUnsignedMax().umul_sat(Other
.getUnsignedMax()) + 1;
1827 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1830 ConstantRange
ConstantRange::smul_sat(const ConstantRange
&Other
) const {
1831 if (isEmptySet() || Other
.isEmptySet())
1834 // Because we could be dealing with negative numbers here, the lower bound is
1835 // the smallest of the cartesian product of the lower and upper ranges;
1837 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1838 // Similarly for the upper bound, swapping min for max.
1840 APInt Min
= getSignedMin();
1841 APInt Max
= getSignedMax();
1842 APInt OtherMin
= Other
.getSignedMin();
1843 APInt OtherMax
= Other
.getSignedMax();
1845 auto L
= {Min
.smul_sat(OtherMin
), Min
.smul_sat(OtherMax
),
1846 Max
.smul_sat(OtherMin
), Max
.smul_sat(OtherMax
)};
1847 auto Compare
= [](const APInt
&A
, const APInt
&B
) { return A
.slt(B
); };
1848 return getNonEmpty(std::min(L
, Compare
), std::max(L
, Compare
) + 1);
1851 ConstantRange
ConstantRange::ushl_sat(const ConstantRange
&Other
) const {
1852 if (isEmptySet() || Other
.isEmptySet())
1855 APInt NewL
= getUnsignedMin().ushl_sat(Other
.getUnsignedMin());
1856 APInt NewU
= getUnsignedMax().ushl_sat(Other
.getUnsignedMax()) + 1;
1857 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1860 ConstantRange
ConstantRange::sshl_sat(const ConstantRange
&Other
) const {
1861 if (isEmptySet() || Other
.isEmptySet())
1864 APInt Min
= getSignedMin(), Max
= getSignedMax();
1865 APInt ShAmtMin
= Other
.getUnsignedMin(), ShAmtMax
= Other
.getUnsignedMax();
1866 APInt NewL
= Min
.sshl_sat(Min
.isNonNegative() ? ShAmtMin
: ShAmtMax
);
1867 APInt NewU
= Max
.sshl_sat(Max
.isNegative() ? ShAmtMin
: ShAmtMax
) + 1;
1868 return getNonEmpty(std::move(NewL
), std::move(NewU
));
1871 ConstantRange
ConstantRange::inverse() const {
1876 return ConstantRange(Upper
, Lower
);
1879 ConstantRange
ConstantRange::abs(bool IntMinIsPoison
) const {
1883 if (isSignWrappedSet()) {
1885 // Check whether the range crosses zero.
1886 if (Upper
.isStrictlyPositive() || !Lower
.isStrictlyPositive())
1887 Lo
= APInt::getZero(getBitWidth());
1889 Lo
= APIntOps::umin(Lower
, -Upper
+ 1);
1891 // If SignedMin is not poison, then it is included in the result range.
1893 return ConstantRange(Lo
, APInt::getSignedMinValue(getBitWidth()));
1895 return ConstantRange(Lo
, APInt::getSignedMinValue(getBitWidth()) + 1);
1898 APInt SMin
= getSignedMin(), SMax
= getSignedMax();
1900 // Skip SignedMin if it is poison.
1901 if (IntMinIsPoison
&& SMin
.isMinSignedValue()) {
1902 // The range may become empty if it *only* contains SignedMin.
1903 if (SMax
.isMinSignedValue())
1908 // All non-negative.
1909 if (SMin
.isNonNegative())
1910 return ConstantRange(SMin
, SMax
+ 1);
1913 if (SMax
.isNegative())
1914 return ConstantRange(-SMax
, -SMin
+ 1);
1916 // Range crosses zero.
1917 return ConstantRange::getNonEmpty(APInt::getZero(getBitWidth()),
1918 APIntOps::umax(-SMin
, SMax
) + 1);
1921 ConstantRange
ConstantRange::ctlz(bool ZeroIsPoison
) const {
1925 APInt Zero
= APInt::getZero(getBitWidth());
1926 if (ZeroIsPoison
&& contains(Zero
)) {
1927 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1928 // which a zero can appear:
1929 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1930 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1931 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1933 if (getLower().isZero()) {
1934 if ((getUpper() - 1).isZero()) {
1935 // We have in input interval of kind [0, 1). In this case we cannot
1936 // really help but return empty-set.
1940 // Compute the resulting range by excluding zero from Lower.
1941 return ConstantRange(
1942 APInt(getBitWidth(), (getUpper() - 1).countl_zero()),
1943 APInt(getBitWidth(), (getLower() + 1).countl_zero() + 1));
1944 } else if ((getUpper() - 1).isZero()) {
1945 // Compute the resulting range by excluding zero from Upper.
1946 return ConstantRange(Zero
,
1947 APInt(getBitWidth(), getLower().countl_zero() + 1));
1949 return ConstantRange(Zero
, APInt(getBitWidth(), getBitWidth()));
1953 // Zero is either safe or not in the range. The output range is composed by
1954 // the result of countLeadingZero of the two extremes.
1955 return getNonEmpty(APInt(getBitWidth(), getUnsignedMax().countl_zero()),
1956 APInt(getBitWidth(), getUnsignedMin().countl_zero()) + 1);
1959 static ConstantRange
getUnsignedCountTrailingZerosRange(const APInt
&Lower
,
1960 const APInt
&Upper
) {
1961 assert(!ConstantRange(Lower
, Upper
).isWrappedSet() &&
1962 "Unexpected wrapped set.");
1963 assert(Lower
!= Upper
&& "Unexpected empty set.");
1964 unsigned BitWidth
= Lower
.getBitWidth();
1965 if (Lower
+ 1 == Upper
)
1966 return ConstantRange(APInt(BitWidth
, Lower
.countr_zero()));
1968 return ConstantRange(APInt::getZero(BitWidth
),
1969 APInt(BitWidth
, BitWidth
+ 1));
1971 // Calculate longest common prefix.
1972 unsigned LCPLength
= (Lower
^ (Upper
- 1)).countl_zero();
1973 // If Lower is {LCP, 000...}, the maximum is Lower.countr_zero().
1974 // Otherwise, the maximum is BitWidth - LCPLength - 1 ({LCP, 100...}).
1975 return ConstantRange(
1976 APInt::getZero(BitWidth
),
1978 std::max(BitWidth
- LCPLength
- 1, Lower
.countr_zero()) + 1));
1981 ConstantRange
ConstantRange::cttz(bool ZeroIsPoison
) const {
1985 unsigned BitWidth
= getBitWidth();
1986 APInt Zero
= APInt::getZero(BitWidth
);
1987 if (ZeroIsPoison
&& contains(Zero
)) {
1988 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1989 // which a zero can appear:
1990 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1991 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1992 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1994 if (Lower
.isZero()) {
1996 // We have in input interval of kind [0, 1). In this case we cannot
1997 // really help but return empty-set.
2001 // Compute the resulting range by excluding zero from Lower.
2002 return getUnsignedCountTrailingZerosRange(APInt(BitWidth
, 1), Upper
);
2003 } else if (Upper
== 1) {
2004 // Compute the resulting range by excluding zero from Upper.
2005 return getUnsignedCountTrailingZerosRange(Lower
, Zero
);
2007 ConstantRange CR1
= getUnsignedCountTrailingZerosRange(Lower
, Zero
);
2009 getUnsignedCountTrailingZerosRange(APInt(BitWidth
, 1), Upper
);
2010 return CR1
.unionWith(CR2
);
2015 return getNonEmpty(Zero
, APInt(BitWidth
, BitWidth
) + 1);
2016 if (!isWrappedSet())
2017 return getUnsignedCountTrailingZerosRange(Lower
, Upper
);
2018 // The range is wrapped. We decompose it into two ranges, [0, Upper) and
2020 // Handle [Lower, 0)
2021 ConstantRange CR1
= getUnsignedCountTrailingZerosRange(Lower
, Zero
);
2022 // Handle [0, Upper)
2023 ConstantRange CR2
= getUnsignedCountTrailingZerosRange(Zero
, Upper
);
2024 return CR1
.unionWith(CR2
);
2027 static ConstantRange
getUnsignedPopCountRange(const APInt
&Lower
,
2028 const APInt
&Upper
) {
2029 assert(!ConstantRange(Lower
, Upper
).isWrappedSet() &&
2030 "Unexpected wrapped set.");
2031 assert(Lower
!= Upper
&& "Unexpected empty set.");
2032 unsigned BitWidth
= Lower
.getBitWidth();
2033 if (Lower
+ 1 == Upper
)
2034 return ConstantRange(APInt(BitWidth
, Lower
.popcount()));
2036 APInt Max
= Upper
- 1;
2037 // Calculate longest common prefix.
2038 unsigned LCPLength
= (Lower
^ Max
).countl_zero();
2039 unsigned LCPPopCount
= Lower
.getHiBits(LCPLength
).popcount();
2040 // If Lower is {LCP, 000...}, the minimum is the popcount of LCP.
2041 // Otherwise, the minimum is the popcount of LCP + 1.
2043 LCPPopCount
+ (Lower
.countr_zero() < BitWidth
- LCPLength
? 1 : 0);
2044 // If Max is {LCP, 111...}, the maximum is the popcount of LCP + (BitWidth -
2046 // Otherwise, the minimum is the popcount of LCP + (BitWidth -
2047 // length of LCP - 1).
2048 unsigned MaxBits
= LCPPopCount
+ (BitWidth
- LCPLength
) -
2049 (Max
.countr_one() < BitWidth
- LCPLength
? 1 : 0);
2050 return ConstantRange(APInt(BitWidth
, MinBits
), APInt(BitWidth
, MaxBits
+ 1));
2053 ConstantRange
ConstantRange::ctpop() const {
2057 unsigned BitWidth
= getBitWidth();
2058 APInt Zero
= APInt::getZero(BitWidth
);
2060 return getNonEmpty(Zero
, APInt(BitWidth
, BitWidth
) + 1);
2061 if (!isWrappedSet())
2062 return getUnsignedPopCountRange(Lower
, Upper
);
2063 // The range is wrapped. We decompose it into two ranges, [0, Upper) and
2065 // Handle [Lower, 0) == [Lower, Max]
2066 ConstantRange CR1
= ConstantRange(APInt(BitWidth
, Lower
.countl_one()),
2067 APInt(BitWidth
, BitWidth
+ 1));
2068 // Handle [0, Upper)
2069 ConstantRange CR2
= getUnsignedPopCountRange(Zero
, Upper
);
2070 return CR1
.unionWith(CR2
);
2073 ConstantRange::OverflowResult
ConstantRange::unsignedAddMayOverflow(
2074 const ConstantRange
&Other
) const {
2075 if (isEmptySet() || Other
.isEmptySet())
2076 return OverflowResult::MayOverflow
;
2078 APInt Min
= getUnsignedMin(), Max
= getUnsignedMax();
2079 APInt OtherMin
= Other
.getUnsignedMin(), OtherMax
= Other
.getUnsignedMax();
2081 // a u+ b overflows high iff a u> ~b.
2082 if (Min
.ugt(~OtherMin
))
2083 return OverflowResult::AlwaysOverflowsHigh
;
2084 if (Max
.ugt(~OtherMax
))
2085 return OverflowResult::MayOverflow
;
2086 return OverflowResult::NeverOverflows
;
2089 ConstantRange::OverflowResult
ConstantRange::signedAddMayOverflow(
2090 const ConstantRange
&Other
) const {
2091 if (isEmptySet() || Other
.isEmptySet())
2092 return OverflowResult::MayOverflow
;
2094 APInt Min
= getSignedMin(), Max
= getSignedMax();
2095 APInt OtherMin
= Other
.getSignedMin(), OtherMax
= Other
.getSignedMax();
2097 APInt SignedMin
= APInt::getSignedMinValue(getBitWidth());
2098 APInt SignedMax
= APInt::getSignedMaxValue(getBitWidth());
2100 // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b.
2101 // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b.
2102 if (Min
.isNonNegative() && OtherMin
.isNonNegative() &&
2103 Min
.sgt(SignedMax
- OtherMin
))
2104 return OverflowResult::AlwaysOverflowsHigh
;
2105 if (Max
.isNegative() && OtherMax
.isNegative() &&
2106 Max
.slt(SignedMin
- OtherMax
))
2107 return OverflowResult::AlwaysOverflowsLow
;
2109 if (Max
.isNonNegative() && OtherMax
.isNonNegative() &&
2110 Max
.sgt(SignedMax
- OtherMax
))
2111 return OverflowResult::MayOverflow
;
2112 if (Min
.isNegative() && OtherMin
.isNegative() &&
2113 Min
.slt(SignedMin
- OtherMin
))
2114 return OverflowResult::MayOverflow
;
2116 return OverflowResult::NeverOverflows
;
2119 ConstantRange::OverflowResult
ConstantRange::unsignedSubMayOverflow(
2120 const ConstantRange
&Other
) const {
2121 if (isEmptySet() || Other
.isEmptySet())
2122 return OverflowResult::MayOverflow
;
2124 APInt Min
= getUnsignedMin(), Max
= getUnsignedMax();
2125 APInt OtherMin
= Other
.getUnsignedMin(), OtherMax
= Other
.getUnsignedMax();
2127 // a u- b overflows low iff a u< b.
2128 if (Max
.ult(OtherMin
))
2129 return OverflowResult::AlwaysOverflowsLow
;
2130 if (Min
.ult(OtherMax
))
2131 return OverflowResult::MayOverflow
;
2132 return OverflowResult::NeverOverflows
;
2135 ConstantRange::OverflowResult
ConstantRange::signedSubMayOverflow(
2136 const ConstantRange
&Other
) const {
2137 if (isEmptySet() || Other
.isEmptySet())
2138 return OverflowResult::MayOverflow
;
2140 APInt Min
= getSignedMin(), Max
= getSignedMax();
2141 APInt OtherMin
= Other
.getSignedMin(), OtherMax
= Other
.getSignedMax();
2143 APInt SignedMin
= APInt::getSignedMinValue(getBitWidth());
2144 APInt SignedMax
= APInt::getSignedMaxValue(getBitWidth());
2146 // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b.
2147 // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b.
2148 if (Min
.isNonNegative() && OtherMax
.isNegative() &&
2149 Min
.sgt(SignedMax
+ OtherMax
))
2150 return OverflowResult::AlwaysOverflowsHigh
;
2151 if (Max
.isNegative() && OtherMin
.isNonNegative() &&
2152 Max
.slt(SignedMin
+ OtherMin
))
2153 return OverflowResult::AlwaysOverflowsLow
;
2155 if (Max
.isNonNegative() && OtherMin
.isNegative() &&
2156 Max
.sgt(SignedMax
+ OtherMin
))
2157 return OverflowResult::MayOverflow
;
2158 if (Min
.isNegative() && OtherMax
.isNonNegative() &&
2159 Min
.slt(SignedMin
+ OtherMax
))
2160 return OverflowResult::MayOverflow
;
2162 return OverflowResult::NeverOverflows
;
2165 ConstantRange::OverflowResult
ConstantRange::unsignedMulMayOverflow(
2166 const ConstantRange
&Other
) const {
2167 if (isEmptySet() || Other
.isEmptySet())
2168 return OverflowResult::MayOverflow
;
2170 APInt Min
= getUnsignedMin(), Max
= getUnsignedMax();
2171 APInt OtherMin
= Other
.getUnsignedMin(), OtherMax
= Other
.getUnsignedMax();
2174 (void) Min
.umul_ov(OtherMin
, Overflow
);
2176 return OverflowResult::AlwaysOverflowsHigh
;
2178 (void) Max
.umul_ov(OtherMax
, Overflow
);
2180 return OverflowResult::MayOverflow
;
2182 return OverflowResult::NeverOverflows
;
2185 void ConstantRange::print(raw_ostream
&OS
) const {
2188 else if (isEmptySet())
2191 OS
<< "[" << Lower
<< "," << Upper
<< ")";
2194 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2195 LLVM_DUMP_METHOD
void ConstantRange::dump() const {
2200 ConstantRange
llvm::getConstantRangeFromMetadata(const MDNode
&Ranges
) {
2201 const unsigned NumRanges
= Ranges
.getNumOperands() / 2;
2202 assert(NumRanges
>= 1 && "Must have at least one range!");
2203 assert(Ranges
.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
2205 auto *FirstLow
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(0));
2206 auto *FirstHigh
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(1));
2208 ConstantRange
CR(FirstLow
->getValue(), FirstHigh
->getValue());
2210 for (unsigned i
= 1; i
< NumRanges
; ++i
) {
2211 auto *Low
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(2 * i
+ 0));
2212 auto *High
= mdconst::extract
<ConstantInt
>(Ranges
.getOperand(2 * i
+ 1));
2214 // Note: unionWith will potentially create a range that contains values not
2215 // contained in any of the original N ranges.
2216 CR
= CR
.unionWith(ConstantRange(Low
->getValue(), High
->getValue()));