LLVM 19.0.0git
ConstantRange.cpp
Go to the documentation of this file.
1//===- ConstantRange.cpp - ConstantRange implementation -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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):
15//
16// [F, F) = {} = Empty set
17// [T, F) = {T}
18// [F, T) = {F}
19// [T, T) = {F, T} = Full set
20//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/ADT/APInt.h"
24#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/Intrinsics.h"
30#include "llvm/IR/Metadata.h"
31#include "llvm/IR/Operator.h"
33#include "llvm/Support/Debug.h"
37#include <algorithm>
38#include <cassert>
39#include <cstdint>
40#include <optional>
41
42using namespace llvm;
43
45 : Lower(Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth)),
46 Upper(Lower) {}
47
49 : Lower(std::move(V)), Upper(Lower + 1) {}
50
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!");
57}
58
60 bool IsSigned) {
61 if (Known.hasConflict())
62 return getEmpty(Known.getBitWidth());
63 if (Known.isUnknown())
64 return getFull(Known.getBitWidth());
65
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);
70
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();
74 Lower.setSignBit();
75 Upper.clearSignBit();
76 return ConstantRange(Lower, Upper + 1);
77}
78
80 // TODO: We could return conflicting known bits here, but consumers are
81 // likely not prepared for that.
82 if (isEmptySet())
83 return KnownBits(getBitWidth());
84
85 // We can only retain the top bits that are the same between min and max.
86 APInt Min = getUnsignedMin();
87 APInt Max = getUnsignedMax();
89 if (std::optional<unsigned> DifferentBit =
91 Known.Zero.clearLowBits(*DifferentBit + 1);
92 Known.One.clearLowBits(*DifferentBit + 1);
93 }
94 return Known;
95}
96
98 const ConstantRange &CR) {
99 if (CR.isEmptySet())
100 return CR;
101
102 uint32_t W = CR.getBitWidth();
103 switch (Pred) {
104 default:
105 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
106 case CmpInst::ICMP_EQ:
107 return CR;
108 case CmpInst::ICMP_NE:
109 if (CR.isSingleElement())
110 return ConstantRange(CR.getUpper(), CR.getLower());
111 return getFull(W);
112 case CmpInst::ICMP_ULT: {
114 if (UMax.isMinValue())
115 return getEmpty(W);
116 return ConstantRange(APInt::getMinValue(W), std::move(UMax));
117 }
118 case CmpInst::ICMP_SLT: {
119 APInt SMax(CR.getSignedMax());
120 if (SMax.isMinSignedValue())
121 return getEmpty(W);
122 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax));
123 }
125 return getNonEmpty(APInt::getMinValue(W), CR.getUnsignedMax() + 1);
128 case CmpInst::ICMP_UGT: {
130 if (UMin.isMaxValue())
131 return getEmpty(W);
132 return ConstantRange(std::move(UMin) + 1, APInt::getZero(W));
133 }
134 case CmpInst::ICMP_SGT: {
135 APInt SMin(CR.getSignedMin());
136 if (SMin.isMaxSignedValue())
137 return getEmpty(W);
138 return ConstantRange(std::move(SMin) + 1, APInt::getSignedMinValue(W));
139 }
144 }
145}
146
148 const ConstantRange &CR) {
149 // Follows from De-Morgan's laws:
150 //
151 // ~(~A union ~B) == A intersect B.
152 //
154 .inverse();
155}
156
158 const APInt &C) {
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).
164 //
166 return makeAllowedICmpRegion(Pred, C);
167}
168
170 const ConstantRange &CR1, const ConstantRange &CR2) {
171 if (CR1.isEmptySet() || CR2.isEmptySet())
172 return true;
173
174 return (CR1.isAllNonNegative() && CR2.isAllNonNegative()) ||
175 (CR1.isAllNegative() && CR2.isAllNegative());
176}
177
179 const ConstantRange &CR1, const ConstantRange &CR2) {
180 if (CR1.isEmptySet() || CR2.isEmptySet())
181 return true;
182
183 return (CR1.isAllNonNegative() && CR2.isAllNegative()) ||
184 (CR1.isAllNegative() && CR2.isAllNonNegative());
185}
186
188 CmpInst::Predicate Pred, const ConstantRange &CR1,
189 const ConstantRange &CR2) {
191 "Only for relational integer predicates!");
192
193 CmpInst::Predicate FlippedSignednessPred =
195
197 return FlippedSignednessPred;
198
200 return CmpInst::getInversePredicate(FlippedSignednessPred);
201
203}
204
206 APInt &RHS, APInt &Offset) const {
207 Offset = APInt(getBitWidth(), 0);
208 if (isFullSet() || isEmptySet()) {
210 RHS = APInt(getBitWidth(), 0);
211 } else if (auto *OnlyElt = getSingleElement()) {
212 Pred = CmpInst::ICMP_EQ;
213 RHS = *OnlyElt;
214 } else if (auto *OnlyMissingElt = getSingleMissingElement()) {
215 Pred = CmpInst::ICMP_NE;
216 RHS = *OnlyMissingElt;
217 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
218 Pred =
220 RHS = getUpper();
221 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
222 Pred =
224 RHS = getLower();
225 } else {
226 Pred = CmpInst::ICMP_ULT;
227 RHS = getUpper() - getLower();
228 Offset = -getLower();
229 }
230
232 "Bad result!");
233}
234
236 APInt &RHS) const {
239 return Offset.isZero();
240}
241
243 const ConstantRange &Other) const {
244 return makeSatisfyingICmpRegion(Pred, Other).contains(*this);
245}
246
247/// Exact mul nuw region for single element RHS.
249 unsigned BitWidth = V.getBitWidth();
250 if (V == 0)
251 return ConstantRange::getFull(V.getBitWidth());
252
258}
259
260/// Exact mul nsw region for single element RHS.
262 // Handle 0 and -1 separately to avoid division by zero or overflow.
263 unsigned BitWidth = V.getBitWidth();
264 if (V == 0)
265 return ConstantRange::getFull(BitWidth);
266
269 // e.g. Returning [-127, 127], represented as [-127, -128).
270 if (V.isAllOnes())
271 return ConstantRange(-MaxValue, MinValue);
272
274 if (V.isNegative()) {
277 } else {
280 }
282}
283
286 const ConstantRange &Other,
287 unsigned NoWrapKind) {
288 using OBO = OverflowingBinaryOperator;
289
290 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
291
292 assert((NoWrapKind == OBO::NoSignedWrap ||
293 NoWrapKind == OBO::NoUnsignedWrap) &&
294 "NoWrapKind invalid!");
295
296 bool Unsigned = NoWrapKind == OBO::NoUnsignedWrap;
297 unsigned BitWidth = Other.getBitWidth();
298
299 switch (BinOp) {
300 default:
301 llvm_unreachable("Unsupported binary op");
302
303 case Instruction::Add: {
304 if (Unsigned)
305 return getNonEmpty(APInt::getZero(BitWidth), -Other.getUnsignedMax());
306
308 APInt SMin = Other.getSignedMin(), SMax = Other.getSignedMax();
309 return getNonEmpty(
310 SMin.isNegative() ? SignedMinVal - SMin : SignedMinVal,
311 SMax.isStrictlyPositive() ? SignedMinVal - SMax : SignedMinVal);
312 }
313
314 case Instruction::Sub: {
315 if (Unsigned)
316 return getNonEmpty(Other.getUnsignedMax(), APInt::getMinValue(BitWidth));
317
319 APInt SMin = Other.getSignedMin(), SMax = Other.getSignedMax();
320 return getNonEmpty(
321 SMax.isStrictlyPositive() ? SignedMinVal + SMax : SignedMinVal,
322 SMin.isNegative() ? SignedMinVal + SMin : SignedMinVal);
323 }
324
325 case Instruction::Mul:
326 if (Unsigned)
327 return makeExactMulNUWRegion(Other.getUnsignedMax());
328
329 // Avoid one makeExactMulNSWRegion() call for the common case of constants.
330 if (const APInt *C = Other.getSingleElement())
331 return makeExactMulNSWRegion(*C);
332
333 return makeExactMulNSWRegion(Other.getSignedMin())
334 .intersectWith(makeExactMulNSWRegion(Other.getSignedMax()));
335
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(
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);
345 }
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();
350 if (Unsigned)
352 APInt::getMaxValue(BitWidth).lshr(ShAmtUMax) + 1);
354 APInt::getSignedMaxValue(BitWidth).ashr(ShAmtUMax) + 1);
355 }
356 }
357}
358
360 const APInt &Other,
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);
365}
366
368 const APInt &C) {
369 unsigned BitWidth = Mask.getBitWidth();
370
371 if ((Mask & C) != C)
372 return getFull(BitWidth);
373
374 if (Mask.isZero())
375 return getEmpty(BitWidth);
376
377 // If (Val & Mask) != C, constrained to the non-equality being
378 // satisfiable, then the value must be larger than the lowest set bit of
379 // Mask, offset by constant C.
381 APInt::getOneBitSet(BitWidth, Mask.countr_zero()) + C, C);
382}
383
385 return Lower == Upper && Lower.isMaxValue();
386}
387
389 return Lower == Upper && Lower.isMinValue();
390}
391
393 return Lower.ugt(Upper) && !Upper.isZero();
394}
395
397 return Lower.ugt(Upper);
398}
399
401 return Lower.sgt(Upper) && !Upper.isMinSignedValue();
402}
403
405 return Lower.sgt(Upper);
406}
407
408bool
410 assert(getBitWidth() == Other.getBitWidth());
411 if (isFullSet())
412 return false;
413 if (Other.isFullSet())
414 return true;
415 return (Upper - Lower).ult(Other.Upper - Other.Lower);
416}
417
418bool
420 // If this a full set, we need special handling to avoid needing an extra bit
421 // to represent the size.
422 if (isFullSet())
423 return MaxSize == 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
424
425 return (Upper - Lower).ugt(MaxSize);
426}
427
429 // Empty set is all negative, full set is not.
430 if (isEmptySet())
431 return true;
432 if (isFullSet())
433 return false;
434
435 return !isUpperSignWrapped() && !Upper.isStrictlyPositive();
436}
437
439 // Empty and full set are automatically treated correctly.
440 return !isSignWrappedSet() && Lower.isNonNegative();
441}
442
444 if (isFullSet() || isUpperWrapped())
446 return getUpper() - 1;
447}
448
450 if (isFullSet() || isWrappedSet())
452 return getLower();
453}
454
456 if (isFullSet() || isUpperSignWrapped())
458 return getUpper() - 1;
459}
460
462 if (isFullSet() || isSignWrappedSet())
464 return getLower();
465}
466
467bool ConstantRange::contains(const APInt &V) const {
468 if (Lower == Upper)
469 return isFullSet();
470
471 if (!isUpperWrapped())
472 return Lower.ule(V) && V.ult(Upper);
473 return Lower.ule(V) || V.ult(Upper);
474}
475
477 if (isFullSet() || Other.isEmptySet()) return true;
478 if (isEmptySet() || Other.isFullSet()) return false;
479
480 if (!isUpperWrapped()) {
481 if (Other.isUpperWrapped())
482 return false;
483
484 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
485 }
486
487 if (!Other.isUpperWrapped())
488 return Other.getUpper().ule(Upper) ||
489 Lower.ule(Other.getLower());
490
491 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
492}
493
495 if (isEmptySet())
496 return 0;
497
498 return getUnsignedMax().getActiveBits();
499}
500
502 if (isEmptySet())
503 return 0;
504
505 return std::max(getSignedMin().getSignificantBits(),
506 getSignedMax().getSignificantBits());
507}
508
510 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
511 // If the set is empty or full, don't modify the endpoints.
512 if (Lower == Upper)
513 return *this;
514 return ConstantRange(Lower - Val, Upper - Val);
515}
516
518 return intersectWith(CR.inverse());
519}
520
522 const ConstantRange &CR1, const ConstantRange &CR2,
525 if (!CR1.isWrappedSet() && CR2.isWrappedSet())
526 return CR1;
527 if (CR1.isWrappedSet() && !CR2.isWrappedSet())
528 return CR2;
529 } else if (Type == ConstantRange::Signed) {
530 if (!CR1.isSignWrappedSet() && CR2.isSignWrappedSet())
531 return CR1;
532 if (CR1.isSignWrappedSet() && !CR2.isSignWrappedSet())
533 return CR2;
534 }
535
536 if (CR1.isSizeStrictlySmallerThan(CR2))
537 return CR1;
538 return CR2;
539}
540
542 PreferredRangeType Type) const {
543 assert(getBitWidth() == CR.getBitWidth() &&
544 "ConstantRange types don't agree!");
545
546 // Handle common cases.
547 if ( isEmptySet() || CR.isFullSet()) return *this;
548 if (CR.isEmptySet() || isFullSet()) return CR;
549
550 if (!isUpperWrapped() && CR.isUpperWrapped())
551 return CR.intersectWith(*this, Type);
552
553 if (!isUpperWrapped() && !CR.isUpperWrapped()) {
554 if (Lower.ult(CR.Lower)) {
555 // L---U : this
556 // L---U : CR
557 if (Upper.ule(CR.Lower))
558 return getEmpty();
559
560 // L---U : this
561 // L---U : CR
562 if (Upper.ult(CR.Upper))
563 return ConstantRange(CR.Lower, Upper);
564
565 // L-------U : this
566 // L---U : CR
567 return CR;
568 }
569 // L---U : this
570 // L-------U : CR
571 if (Upper.ult(CR.Upper))
572 return *this;
573
574 // L-----U : this
575 // L-----U : CR
576 if (Lower.ult(CR.Upper))
577 return ConstantRange(Lower, CR.Upper);
578
579 // L---U : this
580 // L---U : CR
581 return getEmpty();
582 }
583
584 if (isUpperWrapped() && !CR.isUpperWrapped()) {
585 if (CR.Lower.ult(Upper)) {
586 // ------U L--- : this
587 // L--U : CR
588 if (CR.Upper.ult(Upper))
589 return CR;
590
591 // ------U L--- : this
592 // L------U : CR
593 if (CR.Upper.ule(Lower))
594 return ConstantRange(CR.Lower, Upper);
595
596 // ------U L--- : this
597 // L----------U : CR
598 return getPreferredRange(*this, CR, Type);
599 }
600 if (CR.Lower.ult(Lower)) {
601 // --U L---- : this
602 // L--U : CR
603 if (CR.Upper.ule(Lower))
604 return getEmpty();
605
606 // --U L---- : this
607 // L------U : CR
608 return ConstantRange(Lower, CR.Upper);
609 }
610
611 // --U L------ : this
612 // L--U : CR
613 return CR;
614 }
615
616 if (CR.Upper.ult(Upper)) {
617 // ------U L-- : this
618 // --U L------ : CR
619 if (CR.Lower.ult(Upper))
620 return getPreferredRange(*this, CR, Type);
621
622 // ----U L-- : this
623 // --U L---- : CR
624 if (CR.Lower.ult(Lower))
625 return ConstantRange(Lower, CR.Upper);
626
627 // ----U L---- : this
628 // --U L-- : CR
629 return CR;
630 }
631 if (CR.Upper.ule(Lower)) {
632 // --U L-- : this
633 // ----U L---- : CR
634 if (CR.Lower.ult(Lower))
635 return *this;
636
637 // --U L---- : this
638 // ----U L-- : CR
639 return ConstantRange(CR.Lower, Upper);
640 }
641
642 // --U L------ : this
643 // ------U L-- : CR
644 return getPreferredRange(*this, CR, Type);
645}
646
648 PreferredRangeType Type) const {
649 assert(getBitWidth() == CR.getBitWidth() &&
650 "ConstantRange types don't agree!");
651
652 if ( isFullSet() || CR.isEmptySet()) return *this;
653 if (CR.isFullSet() || isEmptySet()) return CR;
654
655 if (!isUpperWrapped() && CR.isUpperWrapped())
656 return CR.unionWith(*this, Type);
657
658 if (!isUpperWrapped() && !CR.isUpperWrapped()) {
659 // L---U and L---U : this
660 // L---U L---U : CR
661 // result in one of
662 // L---------U
663 // -----U L-----
664 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower))
665 return getPreferredRange(
666 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
667
668 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
669 APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
670
671 if (L.isZero() && U.isZero())
672 return getFull();
673
674 return ConstantRange(std::move(L), std::move(U));
675 }
676
677 if (!CR.isUpperWrapped()) {
678 // ------U L----- and ------U L----- : this
679 // L--U L--U : CR
680 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
681 return *this;
682
683 // ------U L----- : this
684 // L---------U : CR
685 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
686 return getFull();
687
688 // ----U L---- : this
689 // L---U : CR
690 // results in one of
691 // ----------U L----
692 // ----U L----------
693 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower))
694 return getPreferredRange(
695 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
696
697 // ----U L----- : this
698 // L----U : CR
699 if (Upper.ult(CR.Lower) && Lower.ule(CR.Upper))
700 return ConstantRange(CR.Lower, Upper);
701
702 // ------U L---- : this
703 // L-----U : CR
704 assert(CR.Lower.ule(Upper) && CR.Upper.ult(Lower) &&
705 "ConstantRange::unionWith missed a case with one range wrapped");
706 return ConstantRange(Lower, CR.Upper);
707 }
708
709 // ------U L---- and ------U L---- : this
710 // -U L----------- and ------------U L : CR
711 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
712 return getFull();
713
714 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
715 APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
716
717 return ConstantRange(std::move(L), std::move(U));
718}
719
720std::optional<ConstantRange>
722 // TODO: This can be implemented more efficiently.
723 ConstantRange Result = intersectWith(CR);
724 if (Result == inverse().unionWith(CR.inverse()).inverse())
725 return Result;
726 return std::nullopt;
727}
728
729std::optional<ConstantRange>
731 // TODO: This can be implemented more efficiently.
732 ConstantRange Result = unionWith(CR);
733 if (Result == inverse().intersectWith(CR.inverse()).inverse())
734 return Result;
735 return std::nullopt;
736}
737
739 uint32_t ResultBitWidth) const {
740 switch (CastOp) {
741 default:
742 llvm_unreachable("unsupported cast type");
743 case Instruction::Trunc:
744 return truncate(ResultBitWidth);
745 case Instruction::SExt:
746 return signExtend(ResultBitWidth);
747 case Instruction::ZExt:
748 return zeroExtend(ResultBitWidth);
749 case Instruction::BitCast:
750 return *this;
751 case Instruction::FPToUI:
752 case Instruction::FPToSI:
753 if (getBitWidth() == ResultBitWidth)
754 return *this;
755 else
756 return getFull(ResultBitWidth);
757 case Instruction::UIToFP: {
758 // TODO: use input range if available
759 auto BW = getBitWidth();
760 APInt Min = APInt::getMinValue(BW);
761 APInt Max = APInt::getMaxValue(BW);
762 if (ResultBitWidth > BW) {
763 Min = Min.zext(ResultBitWidth);
764 Max = Max.zext(ResultBitWidth);
765 }
766 return getNonEmpty(std::move(Min), std::move(Max) + 1);
767 }
768 case Instruction::SIToFP: {
769 // TODO: use input range if available
770 auto BW = getBitWidth();
773 if (ResultBitWidth > BW) {
774 SMin = SMin.sext(ResultBitWidth);
775 SMax = SMax.sext(ResultBitWidth);
776 }
777 return getNonEmpty(std::move(SMin), std::move(SMax) + 1);
778 }
779 case Instruction::FPTrunc:
780 case Instruction::FPExt:
781 case Instruction::IntToPtr:
782 case Instruction::PtrToInt:
783 case Instruction::AddrSpaceCast:
784 // Conservatively return getFull set.
785 return getFull(ResultBitWidth);
786 };
787}
788
790 if (isEmptySet()) return getEmpty(DstTySize);
791
792 unsigned SrcTySize = getBitWidth();
793 assert(SrcTySize < DstTySize && "Not a value extension");
794 if (isFullSet() || isUpperWrapped()) {
795 // Change into [0, 1 << src bit width)
796 APInt LowerExt(DstTySize, 0);
797 if (!Upper) // special case: [X, 0) -- not really wrapping around
798 LowerExt = Lower.zext(DstTySize);
799 return ConstantRange(std::move(LowerExt),
800 APInt::getOneBitSet(DstTySize, SrcTySize));
801 }
802
803 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
804}
805
807 if (isEmptySet()) return getEmpty(DstTySize);
808
809 unsigned SrcTySize = getBitWidth();
810 assert(SrcTySize < DstTySize && "Not a value extension");
811
812 // special case: [X, INT_MIN) -- not really wrapping around
813 if (Upper.isMinSignedValue())
814 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
815
816 if (isFullSet() || isSignWrappedSet()) {
817 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
818 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
819 }
820
821 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
822}
823
825 assert(getBitWidth() > DstTySize && "Not a value truncation");
826 if (isEmptySet())
827 return getEmpty(DstTySize);
828 if (isFullSet())
829 return getFull(DstTySize);
830
831 APInt LowerDiv(Lower), UpperDiv(Upper);
832 ConstantRange Union(DstTySize, /*isFullSet=*/false);
833
834 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
835 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
836 // then we do the union with [MaxValue, Upper)
837 if (isUpperWrapped()) {
838 // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
839 // truncated range.
840 if (Upper.getActiveBits() > DstTySize || Upper.countr_one() == DstTySize)
841 return getFull(DstTySize);
842
843 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
844 UpperDiv.setAllBits();
845
846 // Union covers the MaxValue case, so return if the remaining range is just
847 // MaxValue(DstTy).
848 if (LowerDiv == UpperDiv)
849 return Union;
850 }
851
852 // Chop off the most significant bits that are past the destination bitwidth.
853 if (LowerDiv.getActiveBits() > DstTySize) {
854 // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv.
855 APInt Adjust = LowerDiv & APInt::getBitsSetFrom(getBitWidth(), DstTySize);
856 LowerDiv -= Adjust;
857 UpperDiv -= Adjust;
858 }
859
860 unsigned UpperDivWidth = UpperDiv.getActiveBits();
861 if (UpperDivWidth <= DstTySize)
862 return ConstantRange(LowerDiv.trunc(DstTySize),
863 UpperDiv.trunc(DstTySize)).unionWith(Union);
864
865 // The truncated value wraps around. Check if we can do better than fullset.
866 if (UpperDivWidth == DstTySize + 1) {
867 // Clear the MSB so that UpperDiv wraps around.
868 UpperDiv.clearBit(DstTySize);
869 if (UpperDiv.ult(LowerDiv))
870 return ConstantRange(LowerDiv.trunc(DstTySize),
871 UpperDiv.trunc(DstTySize)).unionWith(Union);
872 }
873
874 return getFull(DstTySize);
875}
876
878 unsigned SrcTySize = getBitWidth();
879 if (SrcTySize > DstTySize)
880 return truncate(DstTySize);
881 if (SrcTySize < DstTySize)
882 return zeroExtend(DstTySize);
883 return *this;
884}
885
887 unsigned SrcTySize = getBitWidth();
888 if (SrcTySize > DstTySize)
889 return truncate(DstTySize);
890 if (SrcTySize < DstTySize)
891 return signExtend(DstTySize);
892 return *this;
893}
894
896 const ConstantRange &Other) const {
897 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
898
899 switch (BinOp) {
900 case Instruction::Add:
901 return add(Other);
902 case Instruction::Sub:
903 return sub(Other);
904 case Instruction::Mul:
905 return multiply(Other);
906 case Instruction::UDiv:
907 return udiv(Other);
908 case Instruction::SDiv:
909 return sdiv(Other);
910 case Instruction::URem:
911 return urem(Other);
912 case Instruction::SRem:
913 return srem(Other);
914 case Instruction::Shl:
915 return shl(Other);
916 case Instruction::LShr:
917 return lshr(Other);
918 case Instruction::AShr:
919 return ashr(Other);
920 case Instruction::And:
921 return binaryAnd(Other);
922 case Instruction::Or:
923 return binaryOr(Other);
924 case Instruction::Xor:
925 return binaryXor(Other);
926 // Note: floating point operations applied to abstract ranges are just
927 // ideal integer operations with a lossy representation
928 case Instruction::FAdd:
929 return add(Other);
930 case Instruction::FSub:
931 return sub(Other);
932 case Instruction::FMul:
933 return multiply(Other);
934 default:
935 // Conservatively return getFull set.
936 return getFull();
937 }
938}
939
941 const ConstantRange &Other,
942 unsigned NoWrapKind) const {
943 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
944
945 switch (BinOp) {
946 case Instruction::Add:
947 return addWithNoWrap(Other, NoWrapKind);
948 case Instruction::Sub:
949 return subWithNoWrap(Other, NoWrapKind);
950 case Instruction::Mul:
951 return multiplyWithNoWrap(Other, NoWrapKind);
952 default:
953 // Don't know about this Overflowing Binary Operation.
954 // Conservatively fallback to plain binop handling.
955 return binaryOp(BinOp, Other);
956 }
957}
958
960 switch (IntrinsicID) {
961 case Intrinsic::uadd_sat:
962 case Intrinsic::usub_sat:
963 case Intrinsic::sadd_sat:
964 case Intrinsic::ssub_sat:
965 case Intrinsic::umin:
966 case Intrinsic::umax:
967 case Intrinsic::smin:
968 case Intrinsic::smax:
969 case Intrinsic::abs:
970 case Intrinsic::ctlz:
971 case Intrinsic::cttz:
972 case Intrinsic::ctpop:
973 return true;
974 default:
975 return false;
976 }
977}
978
981 switch (IntrinsicID) {
982 case Intrinsic::uadd_sat:
983 return Ops[0].uadd_sat(Ops[1]);
984 case Intrinsic::usub_sat:
985 return Ops[0].usub_sat(Ops[1]);
986 case Intrinsic::sadd_sat:
987 return Ops[0].sadd_sat(Ops[1]);
988 case Intrinsic::ssub_sat:
989 return Ops[0].ssub_sat(Ops[1]);
990 case Intrinsic::umin:
991 return Ops[0].umin(Ops[1]);
992 case Intrinsic::umax:
993 return Ops[0].umax(Ops[1]);
994 case Intrinsic::smin:
995 return Ops[0].smin(Ops[1]);
996 case Intrinsic::smax:
997 return Ops[0].smax(Ops[1]);
998 case Intrinsic::abs: {
999 const APInt *IntMinIsPoison = Ops[1].getSingleElement();
1000 assert(IntMinIsPoison && "Must be known (immarg)");
1001 assert(IntMinIsPoison->getBitWidth() == 1 && "Must be boolean");
1002 return Ops[0].abs(IntMinIsPoison->getBoolValue());
1003 }
1004 case Intrinsic::ctlz: {
1005 const APInt *ZeroIsPoison = Ops[1].getSingleElement();
1006 assert(ZeroIsPoison && "Must be known (immarg)");
1007 assert(ZeroIsPoison->getBitWidth() == 1 && "Must be boolean");
1008 return Ops[0].ctlz(ZeroIsPoison->getBoolValue());
1009 }
1010 case Intrinsic::cttz: {
1011 const APInt *ZeroIsPoison = Ops[1].getSingleElement();
1012 assert(ZeroIsPoison && "Must be known (immarg)");
1013 assert(ZeroIsPoison->getBitWidth() == 1 && "Must be boolean");
1014 return Ops[0].cttz(ZeroIsPoison->getBoolValue());
1015 }
1016 case Intrinsic::ctpop:
1017 return Ops[0].ctpop();
1018 default:
1019 assert(!isIntrinsicSupported(IntrinsicID) && "Shouldn't be supported");
1020 llvm_unreachable("Unsupported intrinsic");
1021 }
1022}
1023
1026 if (isEmptySet() || Other.isEmptySet())
1027 return getEmpty();
1028 if (isFullSet() || Other.isFullSet())
1029 return getFull();
1030
1031 APInt NewLower = getLower() + Other.getLower();
1032 APInt NewUpper = getUpper() + Other.getUpper() - 1;
1033 if (NewLower == NewUpper)
1034 return getFull();
1035
1036 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1037 if (X.isSizeStrictlySmallerThan(*this) ||
1038 X.isSizeStrictlySmallerThan(Other))
1039 // We've wrapped, therefore, full set.
1040 return getFull();
1041 return X;
1042}
1043
1045 unsigned NoWrapKind,
1046 PreferredRangeType RangeType) const {
1047 // Calculate the range for "X + Y" which is guaranteed not to wrap(overflow).
1048 // (X is from this, and Y is from Other)
1049 if (isEmptySet() || Other.isEmptySet())
1050 return getEmpty();
1051 if (isFullSet() && Other.isFullSet())
1052 return getFull();
1053
1054 using OBO = OverflowingBinaryOperator;
1055 ConstantRange Result = add(Other);
1056
1057 // If an overflow happens for every value pair in these two constant ranges,
1058 // we must return Empty set. In this case, we get that for free, because we
1059 // get lucky that intersection of add() with uadd_sat()/sadd_sat() results
1060 // in an empty set.
1061
1062 if (NoWrapKind & OBO::NoSignedWrap)
1063 Result = Result.intersectWith(sadd_sat(Other), RangeType);
1064
1065 if (NoWrapKind & OBO::NoUnsignedWrap)
1066 Result = Result.intersectWith(uadd_sat(Other), RangeType);
1067
1068 return Result;
1069}
1070
1073 if (isEmptySet() || Other.isEmptySet())
1074 return getEmpty();
1075 if (isFullSet() || Other.isFullSet())
1076 return getFull();
1077
1078 APInt NewLower = getLower() - Other.getUpper() + 1;
1079 APInt NewUpper = getUpper() - Other.getLower();
1080 if (NewLower == NewUpper)
1081 return getFull();
1082
1083 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1084 if (X.isSizeStrictlySmallerThan(*this) ||
1085 X.isSizeStrictlySmallerThan(Other))
1086 // We've wrapped, therefore, full set.
1087 return getFull();
1088 return X;
1089}
1090
1092 unsigned NoWrapKind,
1093 PreferredRangeType RangeType) const {
1094 // Calculate the range for "X - Y" which is guaranteed not to wrap(overflow).
1095 // (X is from this, and Y is from Other)
1096 if (isEmptySet() || Other.isEmptySet())
1097 return getEmpty();
1098 if (isFullSet() && Other.isFullSet())
1099 return getFull();
1100
1101 using OBO = OverflowingBinaryOperator;
1102 ConstantRange Result = sub(Other);
1103
1104 // If an overflow happens for every value pair in these two constant ranges,
1105 // we must return Empty set. In signed case, we get that for free, because we
1106 // get lucky that intersection of sub() with ssub_sat() results in an
1107 // empty set. But for unsigned we must perform the overflow check manually.
1108
1109 if (NoWrapKind & OBO::NoSignedWrap)
1110 Result = Result.intersectWith(ssub_sat(Other), RangeType);
1111
1112 if (NoWrapKind & OBO::NoUnsignedWrap) {
1113 if (getUnsignedMax().ult(Other.getUnsignedMin()))
1114 return getEmpty(); // Always overflows.
1115 Result = Result.intersectWith(usub_sat(Other), RangeType);
1116 }
1117
1118 return Result;
1119}
1120
1123 // TODO: If either operand is a single element and the multiply is known to
1124 // be non-wrapping, round the result min and max value to the appropriate
1125 // multiple of that element. If wrapping is possible, at least adjust the
1126 // range according to the greatest power-of-two factor of the single element.
1127
1128 if (isEmptySet() || Other.isEmptySet())
1129 return getEmpty();
1130
1131 if (const APInt *C = getSingleElement()) {
1132 if (C->isOne())
1133 return Other;
1134 if (C->isAllOnes())
1136 }
1137
1138 if (const APInt *C = Other.getSingleElement()) {
1139 if (C->isOne())
1140 return *this;
1141 if (C->isAllOnes())
1142 return ConstantRange(APInt::getZero(getBitWidth())).sub(*this);
1143 }
1144
1145 // Multiplication is signedness-independent. However different ranges can be
1146 // obtained depending on how the input ranges are treated. These different
1147 // ranges are all conservatively correct, but one might be better than the
1148 // other. We calculate two ranges; one treating the inputs as unsigned
1149 // and the other signed, then return the smallest of these ranges.
1150
1151 // Unsigned range first.
1152 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
1153 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
1154 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
1155 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
1156
1157 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
1158 this_max * Other_max + 1);
1159 ConstantRange UR = Result_zext.truncate(getBitWidth());
1160
1161 // If the unsigned range doesn't wrap, and isn't negative then it's a range
1162 // from one positive number to another which is as good as we can generate.
1163 // In this case, skip the extra work of generating signed ranges which aren't
1164 // going to be better than this range.
1165 if (!UR.isUpperWrapped() &&
1167 return UR;
1168
1169 // Now the signed range. Because we could be dealing with negative numbers
1170 // here, the lower bound is the smallest of the cartesian product of the
1171 // lower and upper ranges; for example:
1172 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1173 // Similarly for the upper bound, swapping min for max.
1174
1175 this_min = getSignedMin().sext(getBitWidth() * 2);
1176 this_max = getSignedMax().sext(getBitWidth() * 2);
1177 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
1178 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
1179
1180 auto L = {this_min * Other_min, this_min * Other_max,
1181 this_max * Other_min, this_max * Other_max};
1182 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1183 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
1184 ConstantRange SR = Result_sext.truncate(getBitWidth());
1185
1186 return UR.isSizeStrictlySmallerThan(SR) ? UR : SR;
1187}
1188
1191 unsigned NoWrapKind,
1192 PreferredRangeType RangeType) const {
1193 if (isEmptySet() || Other.isEmptySet())
1194 return getEmpty();
1195 if (isFullSet() && Other.isFullSet())
1196 return getFull();
1197
1198 ConstantRange Result = multiply(Other);
1199
1201 Result = Result.intersectWith(smul_sat(Other), RangeType);
1202
1204 Result = Result.intersectWith(umul_sat(Other), RangeType);
1205
1206 return Result;
1207}
1208
1210 if (isEmptySet() || Other.isEmptySet())
1211 return getEmpty();
1212
1213 APInt Min = getSignedMin();
1214 APInt Max = getSignedMax();
1215 APInt OtherMin = Other.getSignedMin();
1216 APInt OtherMax = Other.getSignedMax();
1217
1218 bool O1, O2, O3, O4;
1219 auto Muls = {Min.smul_ov(OtherMin, O1), Min.smul_ov(OtherMax, O2),
1220 Max.smul_ov(OtherMin, O3), Max.smul_ov(OtherMax, O4)};
1221 if (O1 || O2 || O3 || O4)
1222 return getFull();
1223
1224 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1225 return getNonEmpty(std::min(Muls, Compare), std::max(Muls, Compare) + 1);
1226}
1227
1230 // X smax Y is: range(smax(X_smin, Y_smin),
1231 // smax(X_smax, Y_smax))
1232 if (isEmptySet() || Other.isEmptySet())
1233 return getEmpty();
1234 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
1235 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
1236 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1237 if (isSignWrappedSet() || Other.isSignWrappedSet())
1238 return Res.intersectWith(unionWith(Other, Signed), Signed);
1239 return Res;
1240}
1241
1244 // X umax Y is: range(umax(X_umin, Y_umin),
1245 // umax(X_umax, Y_umax))
1246 if (isEmptySet() || Other.isEmptySet())
1247 return getEmpty();
1248 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
1249 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
1250 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1251 if (isWrappedSet() || Other.isWrappedSet())
1253 return Res;
1254}
1255
1258 // X smin Y is: range(smin(X_smin, Y_smin),
1259 // smin(X_smax, Y_smax))
1260 if (isEmptySet() || Other.isEmptySet())
1261 return getEmpty();
1262 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
1263 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
1264 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1265 if (isSignWrappedSet() || Other.isSignWrappedSet())
1266 return Res.intersectWith(unionWith(Other, Signed), Signed);
1267 return Res;
1268}
1269
1272 // X umin Y is: range(umin(X_umin, Y_umin),
1273 // umin(X_umax, Y_umax))
1274 if (isEmptySet() || Other.isEmptySet())
1275 return getEmpty();
1276 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
1277 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
1278 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1279 if (isWrappedSet() || Other.isWrappedSet())
1281 return Res;
1282}
1283
1286 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
1287 return getEmpty();
1288
1289 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
1290
1291 APInt RHS_umin = RHS.getUnsignedMin();
1292 if (RHS_umin.isZero()) {
1293 // We want the lowest value in RHS excluding zero. Usually that would be 1
1294 // except for a range in the form of [X, 1) in which case it would be X.
1295 if (RHS.getUpper() == 1)
1296 RHS_umin = RHS.getLower();
1297 else
1298 RHS_umin = 1;
1299 }
1300
1301 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
1302 return getNonEmpty(std::move(Lower), std::move(Upper));
1303}
1304
1306 // We split up the LHS and RHS into positive and negative components
1307 // and then also compute the positive and negative components of the result
1308 // separately by combining division results with the appropriate signs.
1311 // There are no positive 1-bit values. The 1 would get interpreted as -1.
1312 ConstantRange PosFilter =
1313 getBitWidth() == 1 ? getEmpty()
1314 : ConstantRange(APInt(getBitWidth(), 1), SignedMin);
1315 ConstantRange NegFilter(SignedMin, Zero);
1316 ConstantRange PosL = intersectWith(PosFilter);
1317 ConstantRange NegL = intersectWith(NegFilter);
1318 ConstantRange PosR = RHS.intersectWith(PosFilter);
1319 ConstantRange NegR = RHS.intersectWith(NegFilter);
1320
1321 ConstantRange PosRes = getEmpty();
1322 if (!PosL.isEmptySet() && !PosR.isEmptySet())
1323 // pos / pos = pos.
1324 PosRes = ConstantRange(PosL.Lower.sdiv(PosR.Upper - 1),
1325 (PosL.Upper - 1).sdiv(PosR.Lower) + 1);
1326
1327 if (!NegL.isEmptySet() && !NegR.isEmptySet()) {
1328 // neg / neg = pos.
1329 //
1330 // We need to deal with one tricky case here: SignedMin / -1 is UB on the
1331 // IR level, so we'll want to exclude this case when calculating bounds.
1332 // (For APInts the operation is well-defined and yields SignedMin.) We
1333 // handle this by dropping either SignedMin from the LHS or -1 from the RHS.
1334 APInt Lo = (NegL.Upper - 1).sdiv(NegR.Lower);
1335 if (NegL.Lower.isMinSignedValue() && NegR.Upper.isZero()) {
1336 // Remove -1 from the LHS. Skip if it's the only element, as this would
1337 // leave us with an empty set.
1338 if (!NegR.Lower.isAllOnes()) {
1339 APInt AdjNegRUpper;
1340 if (RHS.Lower.isAllOnes())
1341 // Negative part of [-1, X] without -1 is [SignedMin, X].
1342 AdjNegRUpper = RHS.Upper;
1343 else
1344 // [X, -1] without -1 is [X, -2].
1345 AdjNegRUpper = NegR.Upper - 1;
1346
1347 PosRes = PosRes.unionWith(
1348 ConstantRange(Lo, NegL.Lower.sdiv(AdjNegRUpper - 1) + 1));
1349 }
1350
1351 // Remove SignedMin from the RHS. Skip if it's the only element, as this
1352 // would leave us with an empty set.
1353 if (NegL.Upper != SignedMin + 1) {
1354 APInt AdjNegLLower;
1355 if (Upper == SignedMin + 1)
1356 // Negative part of [X, SignedMin] without SignedMin is [X, -1].
1357 AdjNegLLower = Lower;
1358 else
1359 // [SignedMin, X] without SignedMin is [SignedMin + 1, X].
1360 AdjNegLLower = NegL.Lower + 1;
1361
1362 PosRes = PosRes.unionWith(
1363 ConstantRange(std::move(Lo),
1364 AdjNegLLower.sdiv(NegR.Upper - 1) + 1));
1365 }
1366 } else {
1367 PosRes = PosRes.unionWith(
1368 ConstantRange(std::move(Lo), NegL.Lower.sdiv(NegR.Upper - 1) + 1));
1369 }
1370 }
1371
1372 ConstantRange NegRes = getEmpty();
1373 if (!PosL.isEmptySet() && !NegR.isEmptySet())
1374 // pos / neg = neg.
1375 NegRes = ConstantRange((PosL.Upper - 1).sdiv(NegR.Upper - 1),
1376 PosL.Lower.sdiv(NegR.Lower) + 1);
1377
1378 if (!NegL.isEmptySet() && !PosR.isEmptySet())
1379 // neg / pos = neg.
1380 NegRes = NegRes.unionWith(
1381 ConstantRange(NegL.Lower.sdiv(PosR.Lower),
1382 (NegL.Upper - 1).sdiv(PosR.Upper - 1) + 1));
1383
1384 // Prefer a non-wrapping signed range here.
1386
1387 // Preserve the zero that we dropped when splitting the LHS by sign.
1388 if (contains(Zero) && (!PosR.isEmptySet() || !NegR.isEmptySet()))
1389 Res = Res.unionWith(ConstantRange(Zero));
1390 return Res;
1391}
1392
1394 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
1395 return getEmpty();
1396
1397 if (const APInt *RHSInt = RHS.getSingleElement()) {
1398 // UREM by null is UB.
1399 if (RHSInt->isZero())
1400 return getEmpty();
1401 // Use APInt's implementation of UREM for single element ranges.
1402 if (const APInt *LHSInt = getSingleElement())
1403 return {LHSInt->urem(*RHSInt)};
1404 }
1405
1406 // L % R for L < R is L.
1407 if (getUnsignedMax().ult(RHS.getUnsignedMin()))
1408 return *this;
1409
1410 // L % R is <= L and < R.
1411 APInt Upper = APIntOps::umin(getUnsignedMax(), RHS.getUnsignedMax() - 1) + 1;
1412 return getNonEmpty(APInt::getZero(getBitWidth()), std::move(Upper));
1413}
1414
1416 if (isEmptySet() || RHS.isEmptySet())
1417 return getEmpty();
1418
1419 if (const APInt *RHSInt = RHS.getSingleElement()) {
1420 // SREM by null is UB.
1421 if (RHSInt->isZero())
1422 return getEmpty();
1423 // Use APInt's implementation of SREM for single element ranges.
1424 if (const APInt *LHSInt = getSingleElement())
1425 return {LHSInt->srem(*RHSInt)};
1426 }
1427
1428 ConstantRange AbsRHS = RHS.abs();
1429 APInt MinAbsRHS = AbsRHS.getUnsignedMin();
1430 APInt MaxAbsRHS = AbsRHS.getUnsignedMax();
1431
1432 // Modulus by zero is UB.
1433 if (MaxAbsRHS.isZero())
1434 return getEmpty();
1435
1436 if (MinAbsRHS.isZero())
1437 ++MinAbsRHS;
1438
1439 APInt MinLHS = getSignedMin(), MaxLHS = getSignedMax();
1440
1441 if (MinLHS.isNonNegative()) {
1442 // L % R for L < R is L.
1443 if (MaxLHS.ult(MinAbsRHS))
1444 return *this;
1445
1446 // L % R is <= L and < R.
1447 APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1448 return ConstantRange(APInt::getZero(getBitWidth()), std::move(Upper));
1449 }
1450
1451 // Same basic logic as above, but the result is negative.
1452 if (MaxLHS.isNegative()) {
1453 if (MinLHS.ugt(-MinAbsRHS))
1454 return *this;
1455
1456 APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1457 return ConstantRange(std::move(Lower), APInt(getBitWidth(), 1));
1458 }
1459
1460 // LHS range crosses zero.
1461 APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1462 APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1463 return ConstantRange(std::move(Lower), std::move(Upper));
1464}
1465
1468}
1469
1471 if (isEmptySet() || Other.isEmptySet())
1472 return getEmpty();
1473
1474 ConstantRange KnownBitsRange =
1475 fromKnownBits(toKnownBits() & Other.toKnownBits(), false);
1476 ConstantRange UMinUMaxRange =
1478 APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()) + 1);
1479 return KnownBitsRange.intersectWith(UMinUMaxRange);
1480}
1481
1483 if (isEmptySet() || Other.isEmptySet())
1484 return getEmpty();
1485
1486 ConstantRange KnownBitsRange =
1487 fromKnownBits(toKnownBits() | Other.toKnownBits(), false);
1488 // Upper wrapped range.
1489 ConstantRange UMaxUMinRange =
1490 getNonEmpty(APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()),
1492 return KnownBitsRange.intersectWith(UMaxUMinRange);
1493}
1494
1496 if (isEmptySet() || Other.isEmptySet())
1497 return getEmpty();
1498
1499 // Use APInt's implementation of XOR for single element ranges.
1500 if (isSingleElement() && Other.isSingleElement())
1501 return {*getSingleElement() ^ *Other.getSingleElement()};
1502
1503 // Special-case binary complement, since we can give a precise answer.
1504 if (Other.isSingleElement() && Other.getSingleElement()->isAllOnes())
1505 return binaryNot();
1506 if (isSingleElement() && getSingleElement()->isAllOnes())
1507 return Other.binaryNot();
1508
1509 KnownBits LHSKnown = toKnownBits();
1510 KnownBits RHSKnown = Other.toKnownBits();
1511 KnownBits Known = LHSKnown ^ RHSKnown;
1512 ConstantRange CR = fromKnownBits(Known, /*IsSigned*/ false);
1513 // Typically the following code doesn't improve the result if BW = 1.
1514 if (getBitWidth() == 1)
1515 return CR;
1516
1517 // If LHS is known to be the subset of RHS, treat LHS ^ RHS as RHS -nuw/nsw
1518 // LHS. If RHS is known to be the subset of LHS, treat LHS ^ RHS as LHS
1519 // -nuw/nsw RHS.
1520 if ((~LHSKnown.Zero).isSubsetOf(RHSKnown.One))
1522 else if ((~RHSKnown.Zero).isSubsetOf(LHSKnown.One))
1523 CR = CR.intersectWith(this->sub(Other), PreferredRangeType::Unsigned);
1524 return CR;
1525}
1526
1529 if (isEmptySet() || Other.isEmptySet())
1530 return getEmpty();
1531
1532 APInt Min = getUnsignedMin();
1533 APInt Max = getUnsignedMax();
1534 if (const APInt *RHS = Other.getSingleElement()) {
1535 unsigned BW = getBitWidth();
1536 if (RHS->uge(BW))
1537 return getEmpty();
1538
1539 unsigned EqualLeadingBits = (Min ^ Max).countl_zero();
1540 if (RHS->ule(EqualLeadingBits))
1541 return getNonEmpty(Min << *RHS, (Max << *RHS) + 1);
1542
1543 return getNonEmpty(APInt::getZero(BW),
1544 APInt::getBitsSetFrom(BW, RHS->getZExtValue()) + 1);
1545 }
1546
1547 APInt OtherMax = Other.getUnsignedMax();
1548 if (isAllNegative() && OtherMax.ule(Min.countl_one())) {
1549 // For negative numbers, if the shift does not overflow in a signed sense,
1550 // a larger shift will make the number smaller.
1551 Max <<= Other.getUnsignedMin();
1552 Min <<= OtherMax;
1553 return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1554 }
1555
1556 // There's overflow!
1557 if (OtherMax.ugt(Max.countl_zero()))
1558 return getFull();
1559
1560 // FIXME: implement the other tricky cases
1561
1562 Min <<= Other.getUnsignedMin();
1563 Max <<= OtherMax;
1564
1565 return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1566}
1567
1570 if (isEmptySet() || Other.isEmptySet())
1571 return getEmpty();
1572
1573 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1;
1574 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
1575 return getNonEmpty(std::move(min), std::move(max));
1576}
1577
1580 if (isEmptySet() || Other.isEmptySet())
1581 return getEmpty();
1582
1583 // May straddle zero, so handle both positive and negative cases.
1584 // 'PosMax' is the upper bound of the result of the ashr
1585 // operation, when Upper of the LHS of ashr is a non-negative.
1586 // number. Since ashr of a non-negative number will result in a
1587 // smaller number, the Upper value of LHS is shifted right with
1588 // the minimum value of 'Other' instead of the maximum value.
1589 APInt PosMax = getSignedMax().ashr(Other.getUnsignedMin()) + 1;
1590
1591 // 'PosMin' is the lower bound of the result of the ashr
1592 // operation, when Lower of the LHS is a non-negative number.
1593 // Since ashr of a non-negative number will result in a smaller
1594 // number, the Lower value of LHS is shifted right with the
1595 // maximum value of 'Other'.
1596 APInt PosMin = getSignedMin().ashr(Other.getUnsignedMax());
1597
1598 // 'NegMax' is the upper bound of the result of the ashr
1599 // operation, when Upper of the LHS of ashr is a negative number.
1600 // Since 'ashr' of a negative number will result in a bigger
1601 // number, the Upper value of LHS is shifted right with the
1602 // maximum value of 'Other'.
1603 APInt NegMax = getSignedMax().ashr(Other.getUnsignedMax()) + 1;
1604
1605 // 'NegMin' is the lower bound of the result of the ashr
1606 // operation, when Lower of the LHS of ashr is a negative number.
1607 // Since 'ashr' of a negative number will result in a bigger
1608 // number, the Lower value of LHS is shifted right with the
1609 // minimum value of 'Other'.
1610 APInt NegMin = getSignedMin().ashr(Other.getUnsignedMin());
1611
1612 APInt max, min;
1613 if (getSignedMin().isNonNegative()) {
1614 // Upper and Lower of LHS are non-negative.
1615 min = PosMin;
1616 max = PosMax;
1617 } else if (getSignedMax().isNegative()) {
1618 // Upper and Lower of LHS are negative.
1619 min = NegMin;
1620 max = NegMax;
1621 } else {
1622 // Upper is non-negative and Lower is negative.
1623 min = NegMin;
1624 max = PosMax;
1625 }
1626 return getNonEmpty(std::move(min), std::move(max));
1627}
1628
1630 if (isEmptySet() || Other.isEmptySet())
1631 return getEmpty();
1632
1633 APInt NewL = getUnsignedMin().uadd_sat(Other.getUnsignedMin());
1634 APInt NewU = getUnsignedMax().uadd_sat(Other.getUnsignedMax()) + 1;
1635 return getNonEmpty(std::move(NewL), std::move(NewU));
1636}
1637
1639 if (isEmptySet() || Other.isEmptySet())
1640 return getEmpty();
1641
1642 APInt NewL = getSignedMin().sadd_sat(Other.getSignedMin());
1643 APInt NewU = getSignedMax().sadd_sat(Other.getSignedMax()) + 1;
1644 return getNonEmpty(std::move(NewL), std::move(NewU));
1645}
1646
1648 if (isEmptySet() || Other.isEmptySet())
1649 return getEmpty();
1650
1651 APInt NewL = getUnsignedMin().usub_sat(Other.getUnsignedMax());
1652 APInt NewU = getUnsignedMax().usub_sat(Other.getUnsignedMin()) + 1;
1653 return getNonEmpty(std::move(NewL), std::move(NewU));
1654}
1655
1657 if (isEmptySet() || Other.isEmptySet())
1658 return getEmpty();
1659
1660 APInt NewL = getSignedMin().ssub_sat(Other.getSignedMax());
1661 APInt NewU = getSignedMax().ssub_sat(Other.getSignedMin()) + 1;
1662 return getNonEmpty(std::move(NewL), std::move(NewU));
1663}
1664
1666 if (isEmptySet() || Other.isEmptySet())
1667 return getEmpty();
1668
1669 APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
1670 APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
1671 return getNonEmpty(std::move(NewL), std::move(NewU));
1672}
1673
1675 if (isEmptySet() || Other.isEmptySet())
1676 return getEmpty();
1677
1678 // Because we could be dealing with negative numbers here, the lower bound is
1679 // the smallest of the cartesian product of the lower and upper ranges;
1680 // for example:
1681 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1682 // Similarly for the upper bound, swapping min for max.
1683
1684 APInt Min = getSignedMin();
1685 APInt Max = getSignedMax();
1686 APInt OtherMin = Other.getSignedMin();
1687 APInt OtherMax = Other.getSignedMax();
1688
1689 auto L = {Min.smul_sat(OtherMin), Min.smul_sat(OtherMax),
1690 Max.smul_sat(OtherMin), Max.smul_sat(OtherMax)};
1691 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1692 return getNonEmpty(std::min(L, Compare), std::max(L, Compare) + 1);
1693}
1694
1696 if (isEmptySet() || Other.isEmptySet())
1697 return getEmpty();
1698
1699 APInt NewL = getUnsignedMin().ushl_sat(Other.getUnsignedMin());
1700 APInt NewU = getUnsignedMax().ushl_sat(Other.getUnsignedMax()) + 1;
1701 return getNonEmpty(std::move(NewL), std::move(NewU));
1702}
1703
1705 if (isEmptySet() || Other.isEmptySet())
1706 return getEmpty();
1707
1708 APInt Min = getSignedMin(), Max = getSignedMax();
1709 APInt ShAmtMin = Other.getUnsignedMin(), ShAmtMax = Other.getUnsignedMax();
1710 APInt NewL = Min.sshl_sat(Min.isNonNegative() ? ShAmtMin : ShAmtMax);
1711 APInt NewU = Max.sshl_sat(Max.isNegative() ? ShAmtMin : ShAmtMax) + 1;
1712 return getNonEmpty(std::move(NewL), std::move(NewU));
1713}
1714
1716 if (isFullSet())
1717 return getEmpty();
1718 if (isEmptySet())
1719 return getFull();
1720 return ConstantRange(Upper, Lower);
1721}
1722
1723ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
1724 if (isEmptySet())
1725 return getEmpty();
1726
1727 if (isSignWrappedSet()) {
1728 APInt Lo;
1729 // Check whether the range crosses zero.
1730 if (Upper.isStrictlyPositive() || !Lower.isStrictlyPositive())
1732 else
1733 Lo = APIntOps::umin(Lower, -Upper + 1);
1734
1735 // If SignedMin is not poison, then it is included in the result range.
1736 if (IntMinIsPoison)
1738 else
1740 }
1741
1743
1744 // Skip SignedMin if it is poison.
1745 if (IntMinIsPoison && SMin.isMinSignedValue()) {
1746 // The range may become empty if it *only* contains SignedMin.
1747 if (SMax.isMinSignedValue())
1748 return getEmpty();
1749 ++SMin;
1750 }
1751
1752 // All non-negative.
1753 if (SMin.isNonNegative())
1754 return ConstantRange(SMin, SMax + 1);
1755
1756 // All negative.
1757 if (SMax.isNegative())
1758 return ConstantRange(-SMax, -SMin + 1);
1759
1760 // Range crosses zero.
1762 APIntOps::umax(-SMin, SMax) + 1);
1763}
1764
1765ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const {
1766 if (isEmptySet())
1767 return getEmpty();
1768
1770 if (ZeroIsPoison && contains(Zero)) {
1771 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1772 // which a zero can appear:
1773 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1774 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1775 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1776
1777 if (getLower().isZero()) {
1778 if ((getUpper() - 1).isZero()) {
1779 // We have in input interval of kind [0, 1). In this case we cannot
1780 // really help but return empty-set.
1781 return getEmpty();
1782 }
1783
1784 // Compute the resulting range by excluding zero from Lower.
1785 return ConstantRange(
1786 APInt(getBitWidth(), (getUpper() - 1).countl_zero()),
1787 APInt(getBitWidth(), (getLower() + 1).countl_zero() + 1));
1788 } else if ((getUpper() - 1).isZero()) {
1789 // Compute the resulting range by excluding zero from Upper.
1790 return ConstantRange(Zero,
1792 } else {
1793 return ConstantRange(Zero, APInt(getBitWidth(), getBitWidth()));
1794 }
1795 }
1796
1797 // Zero is either safe or not in the range. The output range is composed by
1798 // the result of countLeadingZero of the two extremes.
1801}
1802
1804 const APInt &Upper) {
1805 assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
1806 "Unexpected wrapped set.");
1807 assert(Lower != Upper && "Unexpected empty set.");
1808 unsigned BitWidth = Lower.getBitWidth();
1809 if (Lower + 1 == Upper)
1810 return ConstantRange(APInt(BitWidth, Lower.countr_zero()));
1811 if (Lower.isZero())
1813 APInt(BitWidth, BitWidth + 1));
1814
1815 // Calculate longest common prefix.
1816 unsigned LCPLength = (Lower ^ (Upper - 1)).countl_zero();
1817 // If Lower is {LCP, 000...}, the maximum is Lower.countr_zero().
1818 // Otherwise, the maximum is BitWidth - LCPLength - 1 ({LCP, 100...}).
1819 return ConstantRange(
1822 std::max(BitWidth - LCPLength - 1, Lower.countr_zero()) + 1));
1823}
1824
1825ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const {
1826 if (isEmptySet())
1827 return getEmpty();
1828
1829 unsigned BitWidth = getBitWidth();
1831 if (ZeroIsPoison && contains(Zero)) {
1832 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1833 // which a zero can appear:
1834 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1835 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1836 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1837
1838 if (Lower.isZero()) {
1839 if (Upper == 1) {
1840 // We have in input interval of kind [0, 1). In this case we cannot
1841 // really help but return empty-set.
1842 return getEmpty();
1843 }
1844
1845 // Compute the resulting range by excluding zero from Lower.
1847 } else if (Upper == 1) {
1848 // Compute the resulting range by excluding zero from Upper.
1849 return getUnsignedCountTrailingZerosRange(Lower, Zero);
1850 } else {
1852 ConstantRange CR2 =
1854 return CR1.unionWith(CR2);
1855 }
1856 }
1857
1858 if (isFullSet())
1859 return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1860 if (!isWrappedSet())
1861 return getUnsignedCountTrailingZerosRange(Lower, Upper);
1862 // The range is wrapped. We decompose it into two ranges, [0, Upper) and
1863 // [Lower, 0).
1864 // Handle [Lower, 0)
1866 // Handle [0, Upper)
1868 return CR1.unionWith(CR2);
1869}
1870
1872 const APInt &Upper) {
1873 assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
1874 "Unexpected wrapped set.");
1875 assert(Lower != Upper && "Unexpected empty set.");
1876 unsigned BitWidth = Lower.getBitWidth();
1877 if (Lower + 1 == Upper)
1878 return ConstantRange(APInt(BitWidth, Lower.popcount()));
1879
1880 APInt Max = Upper - 1;
1881 // Calculate longest common prefix.
1882 unsigned LCPLength = (Lower ^ Max).countl_zero();
1883 unsigned LCPPopCount = Lower.getHiBits(LCPLength).popcount();
1884 // If Lower is {LCP, 000...}, the minimum is the popcount of LCP.
1885 // Otherwise, the minimum is the popcount of LCP + 1.
1886 unsigned MinBits =
1887 LCPPopCount + (Lower.countr_zero() < BitWidth - LCPLength ? 1 : 0);
1888 // If Max is {LCP, 111...}, the maximum is the popcount of LCP + (BitWidth -
1889 // length of LCP).
1890 // Otherwise, the minimum is the popcount of LCP + (BitWidth -
1891 // length of LCP - 1).
1892 unsigned MaxBits = LCPPopCount + (BitWidth - LCPLength) -
1893 (Max.countr_one() < BitWidth - LCPLength ? 1 : 0);
1894 return ConstantRange(APInt(BitWidth, MinBits), APInt(BitWidth, MaxBits + 1));
1895}
1896
1898 if (isEmptySet())
1899 return getEmpty();
1900
1901 unsigned BitWidth = getBitWidth();
1903 if (isFullSet())
1904 return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1905 if (!isWrappedSet())
1906 return getUnsignedPopCountRange(Lower, Upper);
1907 // The range is wrapped. We decompose it into two ranges, [0, Upper) and
1908 // [Lower, 0).
1909 // Handle [Lower, 0) == [Lower, Max]
1911 APInt(BitWidth, BitWidth + 1));
1912 // Handle [0, Upper)
1913 ConstantRange CR2 = getUnsignedPopCountRange(Zero, Upper);
1914 return CR1.unionWith(CR2);
1915}
1916
1918 const ConstantRange &Other) const {
1919 if (isEmptySet() || Other.isEmptySet())
1921
1922 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1923 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1924
1925 // a u+ b overflows high iff a u> ~b.
1926 if (Min.ugt(~OtherMin))
1928 if (Max.ugt(~OtherMax))
1931}
1932
1934 const ConstantRange &Other) const {
1935 if (isEmptySet() || Other.isEmptySet())
1937
1938 APInt Min = getSignedMin(), Max = getSignedMax();
1939 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1940
1943
1944 // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b.
1945 // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b.
1946 if (Min.isNonNegative() && OtherMin.isNonNegative() &&
1947 Min.sgt(SignedMax - OtherMin))
1949 if (Max.isNegative() && OtherMax.isNegative() &&
1950 Max.slt(SignedMin - OtherMax))
1952
1953 if (Max.isNonNegative() && OtherMax.isNonNegative() &&
1954 Max.sgt(SignedMax - OtherMax))
1956 if (Min.isNegative() && OtherMin.isNegative() &&
1957 Min.slt(SignedMin - OtherMin))
1959
1961}
1962
1964 const ConstantRange &Other) const {
1965 if (isEmptySet() || Other.isEmptySet())
1967
1968 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1969 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1970
1971 // a u- b overflows low iff a u< b.
1972 if (Max.ult(OtherMin))
1974 if (Min.ult(OtherMax))
1977}
1978
1980 const ConstantRange &Other) const {
1981 if (isEmptySet() || Other.isEmptySet())
1983
1984 APInt Min = getSignedMin(), Max = getSignedMax();
1985 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1986
1989
1990 // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b.
1991 // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b.
1992 if (Min.isNonNegative() && OtherMax.isNegative() &&
1993 Min.sgt(SignedMax + OtherMax))
1995 if (Max.isNegative() && OtherMin.isNonNegative() &&
1996 Max.slt(SignedMin + OtherMin))
1998
1999 if (Max.isNonNegative() && OtherMin.isNegative() &&
2000 Max.sgt(SignedMax + OtherMin))
2002 if (Min.isNegative() && OtherMax.isNonNegative() &&
2003 Min.slt(SignedMin + OtherMax))
2005
2007}
2008
2010 const ConstantRange &Other) const {
2011 if (isEmptySet() || Other.isEmptySet())
2013
2014 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
2015 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
2016 bool Overflow;
2017
2018 (void) Min.umul_ov(OtherMin, Overflow);
2019 if (Overflow)
2021
2022 (void) Max.umul_ov(OtherMax, Overflow);
2023 if (Overflow)
2025
2027}
2028
2030 if (isFullSet())
2031 OS << "full-set";
2032 else if (isEmptySet())
2033 OS << "empty-set";
2034 else
2035 OS << "[" << Lower << "," << Upper << ")";
2036}
2037
2038#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2040 print(dbgs());
2041}
2042#endif
2043
2045 const unsigned NumRanges = Ranges.getNumOperands() / 2;
2046 assert(NumRanges >= 1 && "Must have at least one range!");
2047 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
2048
2049 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
2050 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
2051
2052 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
2053
2054 for (unsigned i = 1; i < NumRanges; ++i) {
2055 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
2056 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
2057
2058 // Note: unionWith will potentially create a range that contains values not
2059 // contained in any of the original N ranges.
2060 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
2061 }
2062
2063 return CR;
2064}
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:537
static ConstantRange getUnsignedPopCountRange(const APInt &Lower, const APInt &Upper)
static ConstantRange makeExactMulNUWRegion(const APInt &V)
Exact mul nuw region for single element RHS.
static ConstantRange makeExactMulNSWRegion(const APInt &V)
Exact mul nsw region for single element RHS.
static ConstantRange getPreferredRange(const ConstantRange &CR1, const ConstantRange &CR2, ConstantRange::PreferredRangeType Type)
static ConstantRange getUnsignedCountTrailingZerosRange(const APInt &Lower, const APInt &Upper)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:528
This file contains the declarations for metadata subclasses.
uint64_t High
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
Class for arbitrary precision integers.
Definition: APInt.h:77
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2025
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:213
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1386
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:402
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1471
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:185
APInt smul_sat(const APInt &RHS) const
Definition: APInt.cpp:2034
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:1996
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1180
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:350
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1161
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:359
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1447
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1090
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:188
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:396
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:195
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:308
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:198
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2056
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2070
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:335
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1573
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1396
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:806
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1298
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:450
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1930
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:313
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1129
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
APInt umul_sat(const APInt &RHS) const
Definition: APInt.cpp:2047
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:285
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1109
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:275
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:179
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:265
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:218
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:830
unsigned countr_one() const
Count the number of trailing one bits.
Definition: APInt.h:1614
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1200
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1410
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2015
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition: APInt.h:378
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:786
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:787
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:781
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:780
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:784
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:782
@ ICMP_EQ
equal
Definition: InstrTypes.h:778
@ ICMP_NE
not equal
Definition: InstrTypes.h:779
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:785
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:783
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:871
Predicate getFlippedSignednessPredicate()
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert.
Definition: InstrTypes.h:1050
bool isIntPredicate() const
Definition: InstrTypes.h:865
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition: InstrTypes.h:1003
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
bool isUpperSignWrapped() const
Return true if the (exclusive) upper bound wraps around the signed domain.
unsigned getActiveBits() const
Compute the maximal number of active bits needed to represent every value in this range.
ConstantRange zextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
std::optional< ConstantRange > exactUnionWith(const ConstantRange &CR) const
Union the two ranges and return the result if it can be represented exactly, otherwise return std::nu...
bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
ConstantRange umul_sat(const ConstantRange &Other) const
Perform an unsigned saturating multiplication of two constant ranges.
static CmpInst::Predicate getEquivalentPredWithFlippedSignedness(CmpInst::Predicate Pred, const ConstantRange &CR1, const ConstantRange &CR2)
If the comparison between constant ranges this and Other is insensitive to the signedness of the comp...
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
ConstantRange binaryXor(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a binary-xor of a value in this ra...
const APInt * getSingleMissingElement() const
If this set contains all but a single element, return it, otherwise return null.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
const APInt & getLower() const
Return the lower value for this range.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
bool isAllNegative() const
Return true if all values in this range are negative.
ConstantRange truncate(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
ConstantRange urem(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned remainder operation of...
ConstantRange sshl_sat(const ConstantRange &Other) const
Perform a signed saturating left shift of this constant range by a value in Other.
ConstantRange smul_fast(const ConstantRange &Other) const
Return range of possible values for a signed multiplication of this and Other.
ConstantRange lshr(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a logical right shift of a value i...
KnownBits toKnownBits() const
Return known bits for values in this range.
ConstantRange castOp(Instruction::CastOps CastOp, uint32_t BitWidth) const
Return a new range representing the possible values resulting from an application of the specified ca...
ConstantRange umin(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned minimum of a value in ...
APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
ConstantRange difference(const ConstantRange &CR) const
Subtract the specified range from this range (aka relative complement of the sets).
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
ConstantRange srem(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a signed remainder operation of a ...
bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
ConstantRange sadd_sat(const ConstantRange &Other) const
Perform a signed saturating addition of two constant ranges.
ConstantRange ushl_sat(const ConstantRange &Other) const
Perform an unsigned saturating left shift of this constant range by a value in Other.
static ConstantRange intrinsic(Intrinsic::ID IntrinsicID, ArrayRef< ConstantRange > Ops)
Compute range of intrinsic result for the given operand ranges.
void dump() const
Allow printing from a debugger easily.
bool isEmptySet() const
Return true if this set contains no members.
ConstantRange smul_sat(const ConstantRange &Other) const
Perform a signed saturating multiplication of two constant ranges.
ConstantRange shl(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a left shift of a value in this ra...
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
bool isSignWrappedSet() const
Return true if this set wraps around the signed domain.
bool isSizeLargerThan(uint64_t MaxSize) const
Compare set size of this range with Value.
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
ConstantRange abs(bool IntMinIsPoison=false) const
Calculate absolute value range.
static bool isIntrinsicSupported(Intrinsic::ID IntrinsicID)
Returns true if ConstantRange calculations are supported for intrinsic with IntrinsicID.
static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the largest range such that all values in the returned range satisfy the given predicate with...
bool isWrappedSet() const
Return true if this set wraps around the unsigned domain.
ConstantRange usub_sat(const ConstantRange &Other) const
Perform an unsigned saturating subtraction of two constant ranges.
ConstantRange uadd_sat(const ConstantRange &Other) const
Perform an unsigned saturating addition of two constant ranges.
ConstantRange overflowingBinaryOp(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind) const
Return a new range representing the possible values resulting from an application of the specified ov...
void print(raw_ostream &OS) const
Print out the bounds to a stream.
ConstantRange(uint32_t BitWidth, bool isFullSet)
Initialize a full or empty set for the specified bit width.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
ConstantRange subWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind, PreferredRangeType RangeType=Smallest) const
Return a new range representing the possible values resulting from an subtraction with wrap type NoWr...
bool isSingleElement() const
Return true if this set contains exactly one member.
ConstantRange ssub_sat(const ConstantRange &Other) const
Perform a signed saturating subtraction of two constant ranges.
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
ConstantRange umax(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned maximum of a value in ...
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
ConstantRange sdiv(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a signed division of a value in th...
const APInt & getUpper() const
Return the upper value for this range.
bool isUpperWrapped() const
Return true if the exclusive upper bound wraps around the unsigned domain.
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
std::optional< ConstantRange > exactIntersectWith(const ConstantRange &CR) const
Intersect the two ranges and return the result if it can be represented exactly, otherwise return std...
ConstantRange ashr(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a arithmetic right shift of a valu...
ConstantRange binaryAnd(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a binary-and of a value in this ra...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
static bool areInsensitiveToSignednessOfInvertedICmpPredicate(const ConstantRange &CR1, const ConstantRange &CR2)
Return true iff CR1 ult CR2 is equivalent to CR1 sge CR2.
OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
ConstantRange addWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind, PreferredRangeType RangeType=Smallest) const
Return a new range representing the possible values resulting from an addition with wrap type NoWrapK...
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange makeMaskNotEqualRange(const APInt &Mask, const APInt &C)
Initialize a range containing all values X that satisfy (X & Mask) != C.
static bool areInsensitiveToSignednessOfICmpPredicate(const ConstantRange &CR1, const ConstantRange &CR2)
Return true iff CR1 ult CR2 is equivalent to CR1 slt CR2.
ConstantRange cttz(bool ZeroIsPoison=false) const
Calculate cttz range.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition: ConstantRange.h:84
ConstantRange ctpop() const
Calculate ctpop range.
static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind)
Produce the largest range containing all X such that "X BinOp Y" is guaranteed not to wrap (overflow)...
ConstantRange smin(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a signed minimum of a value in thi...
ConstantRange udiv(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned division of a value in...
unsigned getMinSignedBits() const
Compute the maximal number of bits needed to represent every value in this signed range.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
ConstantRange binaryNot() const
Return a new range representing the possible values resulting from a binary-xor of a value in this ra...
ConstantRange smax(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a signed maximum of a value in thi...
ConstantRange binaryOp(Instruction::BinaryOps BinOp, const ConstantRange &Other) const
Return a new range representing the possible values resulting from an application of the specified bi...
ConstantRange binaryOr(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a binary-or of a value in this ran...
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
ConstantRange ctlz(bool ZeroIsPoison=false) const
Calculate ctlz range.
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
ConstantRange sextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
bool isSizeStrictlySmallerThan(const ConstantRange &CR) const
Compare set size of this range with the range CR.
ConstantRange multiplyWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind, PreferredRangeType RangeType=Smallest) const
Return a new range representing the possible values resulting from a multiplication with wrap type No...
bool isBinaryOp() const
Definition: Instruction.h:279
Metadata node.
Definition: Metadata.h:1067
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:77
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::optional< unsigned > GetMostSignificantDifferentBit(const APInt &A, const APInt &B)
Compare two values, and if they are different, return the position of the most significant bit that i...
Definition: APInt.cpp:2971
APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A unsign-divided by B, rounded by the given rounding mode.
Definition: APInt.cpp:2732
APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A sign-divided by B, rounded by the given rounding mode.
Definition: APInt.cpp:2750
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition: APInt.h:2193
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition: APInt.h:2198
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition: APInt.h:2203
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition: APInt.h:2208
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
@ Offset
Definition: DWP.cpp:480
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: bit.h:281
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:290
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:97
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:62
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:134
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:118
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:94