LLVM 17.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 assert(!Known.hasConflict() && "Expected valid KnownBits");
62
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 return makeExactMulNSWRegion(Other.getSignedMin())
330 .intersectWith(makeExactMulNSWRegion(Other.getSignedMax()));
331
332 case Instruction::Shl: {
333 // For given range of shift amounts, if we ignore all illegal shift amounts
334 // (that always produce poison), what shift amount range is left?
335 ConstantRange ShAmt = Other.intersectWith(
337 if (ShAmt.isEmptySet()) {
338 // If the entire range of shift amounts is already poison-producing,
339 // then we can freely add more poison-producing flags ontop of that.
340 return getFull(BitWidth);
341 }
342 // There are some legal shift amounts, we can compute conservatively-correct
343 // range of no-wrap inputs. Note that by now we have clamped the ShAmtUMax
344 // to be at most bitwidth-1, which results in most conservative range.
345 APInt ShAmtUMax = ShAmt.getUnsignedMax();
346 if (Unsigned)
348 APInt::getMaxValue(BitWidth).lshr(ShAmtUMax) + 1);
350 APInt::getSignedMaxValue(BitWidth).ashr(ShAmtUMax) + 1);
351 }
352 }
353}
354
356 const APInt &Other,
357 unsigned NoWrapKind) {
358 // makeGuaranteedNoWrapRegion() is exact for single-element ranges, as
359 // "for all" and "for any" coincide in this case.
360 return makeGuaranteedNoWrapRegion(BinOp, ConstantRange(Other), NoWrapKind);
361}
362
364 return Lower == Upper && Lower.isMaxValue();
365}
366
368 return Lower == Upper && Lower.isMinValue();
369}
370
372 return Lower.ugt(Upper) && !Upper.isZero();
373}
374
376 return Lower.ugt(Upper);
377}
378
380 return Lower.sgt(Upper) && !Upper.isMinSignedValue();
381}
382
384 return Lower.sgt(Upper);
385}
386
387bool
389 assert(getBitWidth() == Other.getBitWidth());
390 if (isFullSet())
391 return false;
392 if (Other.isFullSet())
393 return true;
394 return (Upper - Lower).ult(Other.Upper - Other.Lower);
395}
396
397bool
399 // If this a full set, we need special handling to avoid needing an extra bit
400 // to represent the size.
401 if (isFullSet())
402 return MaxSize == 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
403
404 return (Upper - Lower).ugt(MaxSize);
405}
406
408 // Empty set is all negative, full set is not.
409 if (isEmptySet())
410 return true;
411 if (isFullSet())
412 return false;
413
414 return !isUpperSignWrapped() && !Upper.isStrictlyPositive();
415}
416
418 // Empty and full set are automatically treated correctly.
419 return !isSignWrappedSet() && Lower.isNonNegative();
420}
421
423 if (isFullSet() || isUpperWrapped())
425 return getUpper() - 1;
426}
427
429 if (isFullSet() || isWrappedSet())
431 return getLower();
432}
433
435 if (isFullSet() || isUpperSignWrapped())
437 return getUpper() - 1;
438}
439
441 if (isFullSet() || isSignWrappedSet())
443 return getLower();
444}
445
446bool ConstantRange::contains(const APInt &V) const {
447 if (Lower == Upper)
448 return isFullSet();
449
450 if (!isUpperWrapped())
451 return Lower.ule(V) && V.ult(Upper);
452 return Lower.ule(V) || V.ult(Upper);
453}
454
456 if (isFullSet() || Other.isEmptySet()) return true;
457 if (isEmptySet() || Other.isFullSet()) return false;
458
459 if (!isUpperWrapped()) {
460 if (Other.isUpperWrapped())
461 return false;
462
463 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
464 }
465
466 if (!Other.isUpperWrapped())
467 return Other.getUpper().ule(Upper) ||
468 Lower.ule(Other.getLower());
469
470 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
471}
472
474 if (isEmptySet())
475 return 0;
476
477 return getUnsignedMax().getActiveBits();
478}
479
481 if (isEmptySet())
482 return 0;
483
484 return std::max(getSignedMin().getSignificantBits(),
485 getSignedMax().getSignificantBits());
486}
487
489 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
490 // If the set is empty or full, don't modify the endpoints.
491 if (Lower == Upper)
492 return *this;
493 return ConstantRange(Lower - Val, Upper - Val);
494}
495
497 return intersectWith(CR.inverse());
498}
499
501 const ConstantRange &CR1, const ConstantRange &CR2,
504 if (!CR1.isWrappedSet() && CR2.isWrappedSet())
505 return CR1;
506 if (CR1.isWrappedSet() && !CR2.isWrappedSet())
507 return CR2;
508 } else if (Type == ConstantRange::Signed) {
509 if (!CR1.isSignWrappedSet() && CR2.isSignWrappedSet())
510 return CR1;
511 if (CR1.isSignWrappedSet() && !CR2.isSignWrappedSet())
512 return CR2;
513 }
514
515 if (CR1.isSizeStrictlySmallerThan(CR2))
516 return CR1;
517 return CR2;
518}
519
521 PreferredRangeType Type) const {
522 assert(getBitWidth() == CR.getBitWidth() &&
523 "ConstantRange types don't agree!");
524
525 // Handle common cases.
526 if ( isEmptySet() || CR.isFullSet()) return *this;
527 if (CR.isEmptySet() || isFullSet()) return CR;
528
529 if (!isUpperWrapped() && CR.isUpperWrapped())
530 return CR.intersectWith(*this, Type);
531
532 if (!isUpperWrapped() && !CR.isUpperWrapped()) {
533 if (Lower.ult(CR.Lower)) {
534 // L---U : this
535 // L---U : CR
536 if (Upper.ule(CR.Lower))
537 return getEmpty();
538
539 // L---U : this
540 // L---U : CR
541 if (Upper.ult(CR.Upper))
542 return ConstantRange(CR.Lower, Upper);
543
544 // L-------U : this
545 // L---U : CR
546 return CR;
547 }
548 // L---U : this
549 // L-------U : CR
550 if (Upper.ult(CR.Upper))
551 return *this;
552
553 // L-----U : this
554 // L-----U : CR
555 if (Lower.ult(CR.Upper))
556 return ConstantRange(Lower, CR.Upper);
557
558 // L---U : this
559 // L---U : CR
560 return getEmpty();
561 }
562
563 if (isUpperWrapped() && !CR.isUpperWrapped()) {
564 if (CR.Lower.ult(Upper)) {
565 // ------U L--- : this
566 // L--U : CR
567 if (CR.Upper.ult(Upper))
568 return CR;
569
570 // ------U L--- : this
571 // L------U : CR
572 if (CR.Upper.ule(Lower))
573 return ConstantRange(CR.Lower, Upper);
574
575 // ------U L--- : this
576 // L----------U : CR
577 return getPreferredRange(*this, CR, Type);
578 }
579 if (CR.Lower.ult(Lower)) {
580 // --U L---- : this
581 // L--U : CR
582 if (CR.Upper.ule(Lower))
583 return getEmpty();
584
585 // --U L---- : this
586 // L------U : CR
587 return ConstantRange(Lower, CR.Upper);
588 }
589
590 // --U L------ : this
591 // L--U : CR
592 return CR;
593 }
594
595 if (CR.Upper.ult(Upper)) {
596 // ------U L-- : this
597 // --U L------ : CR
598 if (CR.Lower.ult(Upper))
599 return getPreferredRange(*this, CR, Type);
600
601 // ----U L-- : this
602 // --U L---- : CR
603 if (CR.Lower.ult(Lower))
604 return ConstantRange(Lower, CR.Upper);
605
606 // ----U L---- : this
607 // --U L-- : CR
608 return CR;
609 }
610 if (CR.Upper.ule(Lower)) {
611 // --U L-- : this
612 // ----U L---- : CR
613 if (CR.Lower.ult(Lower))
614 return *this;
615
616 // --U L---- : this
617 // ----U L-- : CR
618 return ConstantRange(CR.Lower, Upper);
619 }
620
621 // --U L------ : this
622 // ------U L-- : CR
623 return getPreferredRange(*this, CR, Type);
624}
625
627 PreferredRangeType Type) const {
628 assert(getBitWidth() == CR.getBitWidth() &&
629 "ConstantRange types don't agree!");
630
631 if ( isFullSet() || CR.isEmptySet()) return *this;
632 if (CR.isFullSet() || isEmptySet()) return CR;
633
634 if (!isUpperWrapped() && CR.isUpperWrapped())
635 return CR.unionWith(*this, Type);
636
637 if (!isUpperWrapped() && !CR.isUpperWrapped()) {
638 // L---U and L---U : this
639 // L---U L---U : CR
640 // result in one of
641 // L---------U
642 // -----U L-----
643 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower))
644 return getPreferredRange(
645 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
646
647 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
648 APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
649
650 if (L.isZero() && U.isZero())
651 return getFull();
652
653 return ConstantRange(std::move(L), std::move(U));
654 }
655
656 if (!CR.isUpperWrapped()) {
657 // ------U L----- and ------U L----- : this
658 // L--U L--U : CR
659 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
660 return *this;
661
662 // ------U L----- : this
663 // L---------U : CR
664 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
665 return getFull();
666
667 // ----U L---- : this
668 // L---U : CR
669 // results in one of
670 // ----------U L----
671 // ----U L----------
672 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower))
673 return getPreferredRange(
674 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
675
676 // ----U L----- : this
677 // L----U : CR
678 if (Upper.ult(CR.Lower) && Lower.ule(CR.Upper))
679 return ConstantRange(CR.Lower, Upper);
680
681 // ------U L---- : this
682 // L-----U : CR
683 assert(CR.Lower.ule(Upper) && CR.Upper.ult(Lower) &&
684 "ConstantRange::unionWith missed a case with one range wrapped");
685 return ConstantRange(Lower, CR.Upper);
686 }
687
688 // ------U L---- and ------U L---- : this
689 // -U L----------- and ------------U L : CR
690 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
691 return getFull();
692
693 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
694 APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
695
696 return ConstantRange(std::move(L), std::move(U));
697}
698
699std::optional<ConstantRange>
701 // TODO: This can be implemented more efficiently.
702 ConstantRange Result = intersectWith(CR);
703 if (Result == inverse().unionWith(CR.inverse()).inverse())
704 return Result;
705 return std::nullopt;
706}
707
708std::optional<ConstantRange>
710 // TODO: This can be implemented more efficiently.
711 ConstantRange Result = unionWith(CR);
712 if (Result == inverse().intersectWith(CR.inverse()).inverse())
713 return Result;
714 return std::nullopt;
715}
716
718 uint32_t ResultBitWidth) const {
719 switch (CastOp) {
720 default:
721 llvm_unreachable("unsupported cast type");
722 case Instruction::Trunc:
723 return truncate(ResultBitWidth);
724 case Instruction::SExt:
725 return signExtend(ResultBitWidth);
726 case Instruction::ZExt:
727 return zeroExtend(ResultBitWidth);
728 case Instruction::BitCast:
729 return *this;
730 case Instruction::FPToUI:
731 case Instruction::FPToSI:
732 if (getBitWidth() == ResultBitWidth)
733 return *this;
734 else
735 return getFull(ResultBitWidth);
736 case Instruction::UIToFP: {
737 // TODO: use input range if available
738 auto BW = getBitWidth();
739 APInt Min = APInt::getMinValue(BW);
740 APInt Max = APInt::getMaxValue(BW);
741 if (ResultBitWidth > BW) {
742 Min = Min.zext(ResultBitWidth);
743 Max = Max.zext(ResultBitWidth);
744 }
745 return ConstantRange(std::move(Min), std::move(Max));
746 }
747 case Instruction::SIToFP: {
748 // TODO: use input range if available
749 auto BW = getBitWidth();
752 if (ResultBitWidth > BW) {
753 SMin = SMin.sext(ResultBitWidth);
754 SMax = SMax.sext(ResultBitWidth);
755 }
756 return ConstantRange(std::move(SMin), std::move(SMax));
757 }
758 case Instruction::FPTrunc:
759 case Instruction::FPExt:
760 case Instruction::IntToPtr:
761 case Instruction::PtrToInt:
762 case Instruction::AddrSpaceCast:
763 // Conservatively return getFull set.
764 return getFull(ResultBitWidth);
765 };
766}
767
769 if (isEmptySet()) return getEmpty(DstTySize);
770
771 unsigned SrcTySize = getBitWidth();
772 assert(SrcTySize < DstTySize && "Not a value extension");
773 if (isFullSet() || isUpperWrapped()) {
774 // Change into [0, 1 << src bit width)
775 APInt LowerExt(DstTySize, 0);
776 if (!Upper) // special case: [X, 0) -- not really wrapping around
777 LowerExt = Lower.zext(DstTySize);
778 return ConstantRange(std::move(LowerExt),
779 APInt::getOneBitSet(DstTySize, SrcTySize));
780 }
781
782 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
783}
784
786 if (isEmptySet()) return getEmpty(DstTySize);
787
788 unsigned SrcTySize = getBitWidth();
789 assert(SrcTySize < DstTySize && "Not a value extension");
790
791 // special case: [X, INT_MIN) -- not really wrapping around
792 if (Upper.isMinSignedValue())
793 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
794
795 if (isFullSet() || isSignWrappedSet()) {
796 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
797 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
798 }
799
800 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
801}
802
804 assert(getBitWidth() > DstTySize && "Not a value truncation");
805 if (isEmptySet())
806 return getEmpty(DstTySize);
807 if (isFullSet())
808 return getFull(DstTySize);
809
810 APInt LowerDiv(Lower), UpperDiv(Upper);
811 ConstantRange Union(DstTySize, /*isFullSet=*/false);
812
813 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
814 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
815 // then we do the union with [MaxValue, Upper)
816 if (isUpperWrapped()) {
817 // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
818 // truncated range.
819 if (Upper.getActiveBits() > DstTySize || Upper.countr_one() == DstTySize)
820 return getFull(DstTySize);
821
822 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
823 UpperDiv.setAllBits();
824
825 // Union covers the MaxValue case, so return if the remaining range is just
826 // MaxValue(DstTy).
827 if (LowerDiv == UpperDiv)
828 return Union;
829 }
830
831 // Chop off the most significant bits that are past the destination bitwidth.
832 if (LowerDiv.getActiveBits() > DstTySize) {
833 // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv.
834 APInt Adjust = LowerDiv & APInt::getBitsSetFrom(getBitWidth(), DstTySize);
835 LowerDiv -= Adjust;
836 UpperDiv -= Adjust;
837 }
838
839 unsigned UpperDivWidth = UpperDiv.getActiveBits();
840 if (UpperDivWidth <= DstTySize)
841 return ConstantRange(LowerDiv.trunc(DstTySize),
842 UpperDiv.trunc(DstTySize)).unionWith(Union);
843
844 // The truncated value wraps around. Check if we can do better than fullset.
845 if (UpperDivWidth == DstTySize + 1) {
846 // Clear the MSB so that UpperDiv wraps around.
847 UpperDiv.clearBit(DstTySize);
848 if (UpperDiv.ult(LowerDiv))
849 return ConstantRange(LowerDiv.trunc(DstTySize),
850 UpperDiv.trunc(DstTySize)).unionWith(Union);
851 }
852
853 return getFull(DstTySize);
854}
855
857 unsigned SrcTySize = getBitWidth();
858 if (SrcTySize > DstTySize)
859 return truncate(DstTySize);
860 if (SrcTySize < DstTySize)
861 return zeroExtend(DstTySize);
862 return *this;
863}
864
866 unsigned SrcTySize = getBitWidth();
867 if (SrcTySize > DstTySize)
868 return truncate(DstTySize);
869 if (SrcTySize < DstTySize)
870 return signExtend(DstTySize);
871 return *this;
872}
873
875 const ConstantRange &Other) const {
876 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
877
878 switch (BinOp) {
879 case Instruction::Add:
880 return add(Other);
881 case Instruction::Sub:
882 return sub(Other);
883 case Instruction::Mul:
884 return multiply(Other);
885 case Instruction::UDiv:
886 return udiv(Other);
887 case Instruction::SDiv:
888 return sdiv(Other);
889 case Instruction::URem:
890 return urem(Other);
891 case Instruction::SRem:
892 return srem(Other);
893 case Instruction::Shl:
894 return shl(Other);
895 case Instruction::LShr:
896 return lshr(Other);
897 case Instruction::AShr:
898 return ashr(Other);
899 case Instruction::And:
900 return binaryAnd(Other);
901 case Instruction::Or:
902 return binaryOr(Other);
903 case Instruction::Xor:
904 return binaryXor(Other);
905 // Note: floating point operations applied to abstract ranges are just
906 // ideal integer operations with a lossy representation
907 case Instruction::FAdd:
908 return add(Other);
909 case Instruction::FSub:
910 return sub(Other);
911 case Instruction::FMul:
912 return multiply(Other);
913 default:
914 // Conservatively return getFull set.
915 return getFull();
916 }
917}
918
920 const ConstantRange &Other,
921 unsigned NoWrapKind) const {
922 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
923
924 switch (BinOp) {
925 case Instruction::Add:
926 return addWithNoWrap(Other, NoWrapKind);
927 case Instruction::Sub:
928 return subWithNoWrap(Other, NoWrapKind);
929 default:
930 // Don't know about this Overflowing Binary Operation.
931 // Conservatively fallback to plain binop handling.
932 return binaryOp(BinOp, Other);
933 }
934}
935
937 switch (IntrinsicID) {
938 case Intrinsic::uadd_sat:
939 case Intrinsic::usub_sat:
940 case Intrinsic::sadd_sat:
941 case Intrinsic::ssub_sat:
942 case Intrinsic::umin:
943 case Intrinsic::umax:
944 case Intrinsic::smin:
945 case Intrinsic::smax:
946 case Intrinsic::abs:
947 case Intrinsic::ctlz:
948 return true;
949 default:
950 return false;
951 }
952}
953
956 switch (IntrinsicID) {
957 case Intrinsic::uadd_sat:
958 return Ops[0].uadd_sat(Ops[1]);
959 case Intrinsic::usub_sat:
960 return Ops[0].usub_sat(Ops[1]);
961 case Intrinsic::sadd_sat:
962 return Ops[0].sadd_sat(Ops[1]);
963 case Intrinsic::ssub_sat:
964 return Ops[0].ssub_sat(Ops[1]);
965 case Intrinsic::umin:
966 return Ops[0].umin(Ops[1]);
967 case Intrinsic::umax:
968 return Ops[0].umax(Ops[1]);
969 case Intrinsic::smin:
970 return Ops[0].smin(Ops[1]);
971 case Intrinsic::smax:
972 return Ops[0].smax(Ops[1]);
973 case Intrinsic::abs: {
974 const APInt *IntMinIsPoison = Ops[1].getSingleElement();
975 assert(IntMinIsPoison && "Must be known (immarg)");
976 assert(IntMinIsPoison->getBitWidth() == 1 && "Must be boolean");
977 return Ops[0].abs(IntMinIsPoison->getBoolValue());
978 }
979 case Intrinsic::ctlz: {
980 const APInt *ZeroIsPoison = Ops[1].getSingleElement();
981 assert(ZeroIsPoison && "Must be known (immarg)");
982 assert(ZeroIsPoison->getBitWidth() == 1 && "Must be boolean");
983 return Ops[0].ctlz(ZeroIsPoison->getBoolValue());
984 }
985 default:
986 assert(!isIntrinsicSupported(IntrinsicID) && "Shouldn't be supported");
987 llvm_unreachable("Unsupported intrinsic");
988 }
989}
990
993 if (isEmptySet() || Other.isEmptySet())
994 return getEmpty();
995 if (isFullSet() || Other.isFullSet())
996 return getFull();
997
998 APInt NewLower = getLower() + Other.getLower();
999 APInt NewUpper = getUpper() + Other.getUpper() - 1;
1000 if (NewLower == NewUpper)
1001 return getFull();
1002
1003 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1004 if (X.isSizeStrictlySmallerThan(*this) ||
1005 X.isSizeStrictlySmallerThan(Other))
1006 // We've wrapped, therefore, full set.
1007 return getFull();
1008 return X;
1009}
1010
1012 unsigned NoWrapKind,
1013 PreferredRangeType RangeType) const {
1014 // Calculate the range for "X + Y" which is guaranteed not to wrap(overflow).
1015 // (X is from this, and Y is from Other)
1016 if (isEmptySet() || Other.isEmptySet())
1017 return getEmpty();
1018 if (isFullSet() && Other.isFullSet())
1019 return getFull();
1020
1021 using OBO = OverflowingBinaryOperator;
1022 ConstantRange Result = add(Other);
1023
1024 // If an overflow happens for every value pair in these two constant ranges,
1025 // we must return Empty set. In this case, we get that for free, because we
1026 // get lucky that intersection of add() with uadd_sat()/sadd_sat() results
1027 // in an empty set.
1028
1029 if (NoWrapKind & OBO::NoSignedWrap)
1030 Result = Result.intersectWith(sadd_sat(Other), RangeType);
1031
1032 if (NoWrapKind & OBO::NoUnsignedWrap)
1033 Result = Result.intersectWith(uadd_sat(Other), RangeType);
1034
1035 return Result;
1036}
1037
1040 if (isEmptySet() || Other.isEmptySet())
1041 return getEmpty();
1042 if (isFullSet() || Other.isFullSet())
1043 return getFull();
1044
1045 APInt NewLower = getLower() - Other.getUpper() + 1;
1046 APInt NewUpper = getUpper() - Other.getLower();
1047 if (NewLower == NewUpper)
1048 return getFull();
1049
1050 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1051 if (X.isSizeStrictlySmallerThan(*this) ||
1052 X.isSizeStrictlySmallerThan(Other))
1053 // We've wrapped, therefore, full set.
1054 return getFull();
1055 return X;
1056}
1057
1059 unsigned NoWrapKind,
1060 PreferredRangeType RangeType) const {
1061 // Calculate the range for "X - Y" which is guaranteed not to wrap(overflow).
1062 // (X is from this, and Y is from Other)
1063 if (isEmptySet() || Other.isEmptySet())
1064 return getEmpty();
1065 if (isFullSet() && Other.isFullSet())
1066 return getFull();
1067
1068 using OBO = OverflowingBinaryOperator;
1069 ConstantRange Result = sub(Other);
1070
1071 // If an overflow happens for every value pair in these two constant ranges,
1072 // we must return Empty set. In signed case, we get that for free, because we
1073 // get lucky that intersection of sub() with ssub_sat() results in an
1074 // empty set. But for unsigned we must perform the overflow check manually.
1075
1076 if (NoWrapKind & OBO::NoSignedWrap)
1077 Result = Result.intersectWith(ssub_sat(Other), RangeType);
1078
1079 if (NoWrapKind & OBO::NoUnsignedWrap) {
1080 if (getUnsignedMax().ult(Other.getUnsignedMin()))
1081 return getEmpty(); // Always overflows.
1082 Result = Result.intersectWith(usub_sat(Other), RangeType);
1083 }
1084
1085 return Result;
1086}
1087
1090 // TODO: If either operand is a single element and the multiply is known to
1091 // be non-wrapping, round the result min and max value to the appropriate
1092 // multiple of that element. If wrapping is possible, at least adjust the
1093 // range according to the greatest power-of-two factor of the single element.
1094
1095 if (isEmptySet() || Other.isEmptySet())
1096 return getEmpty();
1097
1098 // Multiplication is signedness-independent. However different ranges can be
1099 // obtained depending on how the input ranges are treated. These different
1100 // ranges are all conservatively correct, but one might be better than the
1101 // other. We calculate two ranges; one treating the inputs as unsigned
1102 // and the other signed, then return the smallest of these ranges.
1103
1104 // Unsigned range first.
1105 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
1106 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
1107 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
1108 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
1109
1110 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
1111 this_max * Other_max + 1);
1112 ConstantRange UR = Result_zext.truncate(getBitWidth());
1113
1114 // If the unsigned range doesn't wrap, and isn't negative then it's a range
1115 // from one positive number to another which is as good as we can generate.
1116 // In this case, skip the extra work of generating signed ranges which aren't
1117 // going to be better than this range.
1118 if (!UR.isUpperWrapped() &&
1120 return UR;
1121
1122 // Now the signed range. Because we could be dealing with negative numbers
1123 // here, the lower bound is the smallest of the cartesian product of the
1124 // lower and upper ranges; for example:
1125 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1126 // Similarly for the upper bound, swapping min for max.
1127
1128 this_min = getSignedMin().sext(getBitWidth() * 2);
1129 this_max = getSignedMax().sext(getBitWidth() * 2);
1130 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
1131 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
1132
1133 auto L = {this_min * Other_min, this_min * Other_max,
1134 this_max * Other_min, this_max * Other_max};
1135 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1136 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
1137 ConstantRange SR = Result_sext.truncate(getBitWidth());
1138
1139 return UR.isSizeStrictlySmallerThan(SR) ? UR : SR;
1140}
1141
1143 if (isEmptySet() || Other.isEmptySet())
1144 return getEmpty();
1145
1146 APInt Min = getSignedMin();
1147 APInt Max = getSignedMax();
1148 APInt OtherMin = Other.getSignedMin();
1149 APInt OtherMax = Other.getSignedMax();
1150
1151 bool O1, O2, O3, O4;
1152 auto Muls = {Min.smul_ov(OtherMin, O1), Min.smul_ov(OtherMax, O2),
1153 Max.smul_ov(OtherMin, O3), Max.smul_ov(OtherMax, O4)};
1154 if (O1 || O2 || O3 || O4)
1155 return getFull();
1156
1157 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1158 return getNonEmpty(std::min(Muls, Compare), std::max(Muls, Compare) + 1);
1159}
1160
1163 // X smax Y is: range(smax(X_smin, Y_smin),
1164 // smax(X_smax, Y_smax))
1165 if (isEmptySet() || Other.isEmptySet())
1166 return getEmpty();
1167 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
1168 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
1169 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1170 if (isSignWrappedSet() || Other.isSignWrappedSet())
1171 return Res.intersectWith(unionWith(Other, Signed), Signed);
1172 return Res;
1173}
1174
1177 // X umax Y is: range(umax(X_umin, Y_umin),
1178 // umax(X_umax, Y_umax))
1179 if (isEmptySet() || Other.isEmptySet())
1180 return getEmpty();
1181 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
1182 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
1183 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1184 if (isWrappedSet() || Other.isWrappedSet())
1186 return Res;
1187}
1188
1191 // X smin Y is: range(smin(X_smin, Y_smin),
1192 // smin(X_smax, Y_smax))
1193 if (isEmptySet() || Other.isEmptySet())
1194 return getEmpty();
1195 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
1196 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
1197 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1198 if (isSignWrappedSet() || Other.isSignWrappedSet())
1199 return Res.intersectWith(unionWith(Other, Signed), Signed);
1200 return Res;
1201}
1202
1205 // X umin Y is: range(umin(X_umin, Y_umin),
1206 // umin(X_umax, Y_umax))
1207 if (isEmptySet() || Other.isEmptySet())
1208 return getEmpty();
1209 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
1210 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
1211 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1212 if (isWrappedSet() || Other.isWrappedSet())
1214 return Res;
1215}
1216
1219 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
1220 return getEmpty();
1221
1222 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
1223
1224 APInt RHS_umin = RHS.getUnsignedMin();
1225 if (RHS_umin.isZero()) {
1226 // We want the lowest value in RHS excluding zero. Usually that would be 1
1227 // except for a range in the form of [X, 1) in which case it would be X.
1228 if (RHS.getUpper() == 1)
1229 RHS_umin = RHS.getLower();
1230 else
1231 RHS_umin = 1;
1232 }
1233
1234 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
1235 return getNonEmpty(std::move(Lower), std::move(Upper));
1236}
1237
1239 // We split up the LHS and RHS into positive and negative components
1240 // and then also compute the positive and negative components of the result
1241 // separately by combining division results with the appropriate signs.
1244 // There are no positive 1-bit values. The 1 would get interpreted as -1.
1245 ConstantRange PosFilter =
1246 getBitWidth() == 1 ? getEmpty()
1247 : ConstantRange(APInt(getBitWidth(), 1), SignedMin);
1248 ConstantRange NegFilter(SignedMin, Zero);
1249 ConstantRange PosL = intersectWith(PosFilter);
1250 ConstantRange NegL = intersectWith(NegFilter);
1251 ConstantRange PosR = RHS.intersectWith(PosFilter);
1252 ConstantRange NegR = RHS.intersectWith(NegFilter);
1253
1254 ConstantRange PosRes = getEmpty();
1255 if (!PosL.isEmptySet() && !PosR.isEmptySet())
1256 // pos / pos = pos.
1257 PosRes = ConstantRange(PosL.Lower.sdiv(PosR.Upper - 1),
1258 (PosL.Upper - 1).sdiv(PosR.Lower) + 1);
1259
1260 if (!NegL.isEmptySet() && !NegR.isEmptySet()) {
1261 // neg / neg = pos.
1262 //
1263 // We need to deal with one tricky case here: SignedMin / -1 is UB on the
1264 // IR level, so we'll want to exclude this case when calculating bounds.
1265 // (For APInts the operation is well-defined and yields SignedMin.) We
1266 // handle this by dropping either SignedMin from the LHS or -1 from the RHS.
1267 APInt Lo = (NegL.Upper - 1).sdiv(NegR.Lower);
1268 if (NegL.Lower.isMinSignedValue() && NegR.Upper.isZero()) {
1269 // Remove -1 from the LHS. Skip if it's the only element, as this would
1270 // leave us with an empty set.
1271 if (!NegR.Lower.isAllOnes()) {
1272 APInt AdjNegRUpper;
1273 if (RHS.Lower.isAllOnes())
1274 // Negative part of [-1, X] without -1 is [SignedMin, X].
1275 AdjNegRUpper = RHS.Upper;
1276 else
1277 // [X, -1] without -1 is [X, -2].
1278 AdjNegRUpper = NegR.Upper - 1;
1279
1280 PosRes = PosRes.unionWith(
1281 ConstantRange(Lo, NegL.Lower.sdiv(AdjNegRUpper - 1) + 1));
1282 }
1283
1284 // Remove SignedMin from the RHS. Skip if it's the only element, as this
1285 // would leave us with an empty set.
1286 if (NegL.Upper != SignedMin + 1) {
1287 APInt AdjNegLLower;
1288 if (Upper == SignedMin + 1)
1289 // Negative part of [X, SignedMin] without SignedMin is [X, -1].
1290 AdjNegLLower = Lower;
1291 else
1292 // [SignedMin, X] without SignedMin is [SignedMin + 1, X].
1293 AdjNegLLower = NegL.Lower + 1;
1294
1295 PosRes = PosRes.unionWith(
1296 ConstantRange(std::move(Lo),
1297 AdjNegLLower.sdiv(NegR.Upper - 1) + 1));
1298 }
1299 } else {
1300 PosRes = PosRes.unionWith(
1301 ConstantRange(std::move(Lo), NegL.Lower.sdiv(NegR.Upper - 1) + 1));
1302 }
1303 }
1304
1305 ConstantRange NegRes = getEmpty();
1306 if (!PosL.isEmptySet() && !NegR.isEmptySet())
1307 // pos / neg = neg.
1308 NegRes = ConstantRange((PosL.Upper - 1).sdiv(NegR.Upper - 1),
1309 PosL.Lower.sdiv(NegR.Lower) + 1);
1310
1311 if (!NegL.isEmptySet() && !PosR.isEmptySet())
1312 // neg / pos = neg.
1313 NegRes = NegRes.unionWith(
1314 ConstantRange(NegL.Lower.sdiv(PosR.Lower),
1315 (NegL.Upper - 1).sdiv(PosR.Upper - 1) + 1));
1316
1317 // Prefer a non-wrapping signed range here.
1319
1320 // Preserve the zero that we dropped when splitting the LHS by sign.
1321 if (contains(Zero) && (!PosR.isEmptySet() || !NegR.isEmptySet()))
1322 Res = Res.unionWith(ConstantRange(Zero));
1323 return Res;
1324}
1325
1327 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
1328 return getEmpty();
1329
1330 if (const APInt *RHSInt = RHS.getSingleElement()) {
1331 // UREM by null is UB.
1332 if (RHSInt->isZero())
1333 return getEmpty();
1334 // Use APInt's implementation of UREM for single element ranges.
1335 if (const APInt *LHSInt = getSingleElement())
1336 return {LHSInt->urem(*RHSInt)};
1337 }
1338
1339 // L % R for L < R is L.
1340 if (getUnsignedMax().ult(RHS.getUnsignedMin()))
1341 return *this;
1342
1343 // L % R is <= L and < R.
1344 APInt Upper = APIntOps::umin(getUnsignedMax(), RHS.getUnsignedMax() - 1) + 1;
1345 return getNonEmpty(APInt::getZero(getBitWidth()), std::move(Upper));
1346}
1347
1349 if (isEmptySet() || RHS.isEmptySet())
1350 return getEmpty();
1351
1352 if (const APInt *RHSInt = RHS.getSingleElement()) {
1353 // SREM by null is UB.
1354 if (RHSInt->isZero())
1355 return getEmpty();
1356 // Use APInt's implementation of SREM for single element ranges.
1357 if (const APInt *LHSInt = getSingleElement())
1358 return {LHSInt->srem(*RHSInt)};
1359 }
1360
1361 ConstantRange AbsRHS = RHS.abs();
1362 APInt MinAbsRHS = AbsRHS.getUnsignedMin();
1363 APInt MaxAbsRHS = AbsRHS.getUnsignedMax();
1364
1365 // Modulus by zero is UB.
1366 if (MaxAbsRHS.isZero())
1367 return getEmpty();
1368
1369 if (MinAbsRHS.isZero())
1370 ++MinAbsRHS;
1371
1372 APInt MinLHS = getSignedMin(), MaxLHS = getSignedMax();
1373
1374 if (MinLHS.isNonNegative()) {
1375 // L % R for L < R is L.
1376 if (MaxLHS.ult(MinAbsRHS))
1377 return *this;
1378
1379 // L % R is <= L and < R.
1380 APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1381 return ConstantRange(APInt::getZero(getBitWidth()), std::move(Upper));
1382 }
1383
1384 // Same basic logic as above, but the result is negative.
1385 if (MaxLHS.isNegative()) {
1386 if (MinLHS.ugt(-MinAbsRHS))
1387 return *this;
1388
1389 APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1390 return ConstantRange(std::move(Lower), APInt(getBitWidth(), 1));
1391 }
1392
1393 // LHS range crosses zero.
1394 APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1395 APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1396 return ConstantRange(std::move(Lower), std::move(Upper));
1397}
1398
1401}
1402
1404 if (isEmptySet() || Other.isEmptySet())
1405 return getEmpty();
1406
1407 ConstantRange KnownBitsRange =
1408 fromKnownBits(toKnownBits() & Other.toKnownBits(), false);
1409 ConstantRange UMinUMaxRange =
1411 APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()) + 1);
1412 return KnownBitsRange.intersectWith(UMinUMaxRange);
1413}
1414
1416 if (isEmptySet() || Other.isEmptySet())
1417 return getEmpty();
1418
1419 ConstantRange KnownBitsRange =
1420 fromKnownBits(toKnownBits() | Other.toKnownBits(), false);
1421 // Upper wrapped range.
1422 ConstantRange UMaxUMinRange =
1423 getNonEmpty(APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()),
1425 return KnownBitsRange.intersectWith(UMaxUMinRange);
1426}
1427
1429 if (isEmptySet() || Other.isEmptySet())
1430 return getEmpty();
1431
1432 // Use APInt's implementation of XOR for single element ranges.
1433 if (isSingleElement() && Other.isSingleElement())
1434 return {*getSingleElement() ^ *Other.getSingleElement()};
1435
1436 // Special-case binary complement, since we can give a precise answer.
1437 if (Other.isSingleElement() && Other.getSingleElement()->isAllOnes())
1438 return binaryNot();
1439 if (isSingleElement() && getSingleElement()->isAllOnes())
1440 return Other.binaryNot();
1441
1442 return fromKnownBits(toKnownBits() ^ Other.toKnownBits(), /*IsSigned*/false);
1443}
1444
1447 if (isEmptySet() || Other.isEmptySet())
1448 return getEmpty();
1449
1450 APInt Min = getUnsignedMin();
1451 APInt Max = getUnsignedMax();
1452 if (const APInt *RHS = Other.getSingleElement()) {
1453 unsigned BW = getBitWidth();
1454 if (RHS->uge(BW))
1455 return getEmpty();
1456
1457 unsigned EqualLeadingBits = (Min ^ Max).countl_zero();
1458 if (RHS->ule(EqualLeadingBits))
1459 return getNonEmpty(Min << *RHS, (Max << *RHS) + 1);
1460
1461 return getNonEmpty(APInt::getZero(BW),
1462 APInt::getBitsSetFrom(BW, RHS->getZExtValue()) + 1);
1463 }
1464
1465 APInt OtherMax = Other.getUnsignedMax();
1466
1467 // There's overflow!
1468 if (OtherMax.ugt(Max.countl_zero()))
1469 return getFull();
1470
1471 // FIXME: implement the other tricky cases
1472
1473 Min <<= Other.getUnsignedMin();
1474 Max <<= OtherMax;
1475
1476 return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1477}
1478
1481 if (isEmptySet() || Other.isEmptySet())
1482 return getEmpty();
1483
1484 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1;
1485 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
1486 return getNonEmpty(std::move(min), std::move(max));
1487}
1488
1491 if (isEmptySet() || Other.isEmptySet())
1492 return getEmpty();
1493
1494 // May straddle zero, so handle both positive and negative cases.
1495 // 'PosMax' is the upper bound of the result of the ashr
1496 // operation, when Upper of the LHS of ashr is a non-negative.
1497 // number. Since ashr of a non-negative number will result in a
1498 // smaller number, the Upper value of LHS is shifted right with
1499 // the minimum value of 'Other' instead of the maximum value.
1500 APInt PosMax = getSignedMax().ashr(Other.getUnsignedMin()) + 1;
1501
1502 // 'PosMin' is the lower bound of the result of the ashr
1503 // operation, when Lower of the LHS is a non-negative number.
1504 // Since ashr of a non-negative number will result in a smaller
1505 // number, the Lower value of LHS is shifted right with the
1506 // maximum value of 'Other'.
1507 APInt PosMin = getSignedMin().ashr(Other.getUnsignedMax());
1508
1509 // 'NegMax' is the upper bound of the result of the ashr
1510 // operation, when Upper of the LHS of ashr is a negative number.
1511 // Since 'ashr' of a negative number will result in a bigger
1512 // number, the Upper value of LHS is shifted right with the
1513 // maximum value of 'Other'.
1514 APInt NegMax = getSignedMax().ashr(Other.getUnsignedMax()) + 1;
1515
1516 // 'NegMin' is the lower bound of the result of the ashr
1517 // operation, when Lower of the LHS of ashr is a negative number.
1518 // Since 'ashr' of a negative number will result in a bigger
1519 // number, the Lower value of LHS is shifted right with the
1520 // minimum value of 'Other'.
1521 APInt NegMin = getSignedMin().ashr(Other.getUnsignedMin());
1522
1523 APInt max, min;
1524 if (getSignedMin().isNonNegative()) {
1525 // Upper and Lower of LHS are non-negative.
1526 min = PosMin;
1527 max = PosMax;
1528 } else if (getSignedMax().isNegative()) {
1529 // Upper and Lower of LHS are negative.
1530 min = NegMin;
1531 max = NegMax;
1532 } else {
1533 // Upper is non-negative and Lower is negative.
1534 min = NegMin;
1535 max = PosMax;
1536 }
1537 return getNonEmpty(std::move(min), std::move(max));
1538}
1539
1541 if (isEmptySet() || Other.isEmptySet())
1542 return getEmpty();
1543
1544 APInt NewL = getUnsignedMin().uadd_sat(Other.getUnsignedMin());
1545 APInt NewU = getUnsignedMax().uadd_sat(Other.getUnsignedMax()) + 1;
1546 return getNonEmpty(std::move(NewL), std::move(NewU));
1547}
1548
1550 if (isEmptySet() || Other.isEmptySet())
1551 return getEmpty();
1552
1553 APInt NewL = getSignedMin().sadd_sat(Other.getSignedMin());
1554 APInt NewU = getSignedMax().sadd_sat(Other.getSignedMax()) + 1;
1555 return getNonEmpty(std::move(NewL), std::move(NewU));
1556}
1557
1559 if (isEmptySet() || Other.isEmptySet())
1560 return getEmpty();
1561
1562 APInt NewL = getUnsignedMin().usub_sat(Other.getUnsignedMax());
1563 APInt NewU = getUnsignedMax().usub_sat(Other.getUnsignedMin()) + 1;
1564 return getNonEmpty(std::move(NewL), std::move(NewU));
1565}
1566
1568 if (isEmptySet() || Other.isEmptySet())
1569 return getEmpty();
1570
1571 APInt NewL = getSignedMin().ssub_sat(Other.getSignedMax());
1572 APInt NewU = getSignedMax().ssub_sat(Other.getSignedMin()) + 1;
1573 return getNonEmpty(std::move(NewL), std::move(NewU));
1574}
1575
1577 if (isEmptySet() || Other.isEmptySet())
1578 return getEmpty();
1579
1580 APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
1581 APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
1582 return getNonEmpty(std::move(NewL), std::move(NewU));
1583}
1584
1586 if (isEmptySet() || Other.isEmptySet())
1587 return getEmpty();
1588
1589 // Because we could be dealing with negative numbers here, the lower bound is
1590 // the smallest of the cartesian product of the lower and upper ranges;
1591 // for example:
1592 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1593 // Similarly for the upper bound, swapping min for max.
1594
1595 APInt Min = getSignedMin();
1596 APInt Max = getSignedMax();
1597 APInt OtherMin = Other.getSignedMin();
1598 APInt OtherMax = Other.getSignedMax();
1599
1600 auto L = {Min.smul_sat(OtherMin), Min.smul_sat(OtherMax),
1601 Max.smul_sat(OtherMin), Max.smul_sat(OtherMax)};
1602 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1603 return getNonEmpty(std::min(L, Compare), std::max(L, Compare) + 1);
1604}
1605
1607 if (isEmptySet() || Other.isEmptySet())
1608 return getEmpty();
1609
1610 APInt NewL = getUnsignedMin().ushl_sat(Other.getUnsignedMin());
1611 APInt NewU = getUnsignedMax().ushl_sat(Other.getUnsignedMax()) + 1;
1612 return getNonEmpty(std::move(NewL), std::move(NewU));
1613}
1614
1616 if (isEmptySet() || Other.isEmptySet())
1617 return getEmpty();
1618
1619 APInt Min = getSignedMin(), Max = getSignedMax();
1620 APInt ShAmtMin = Other.getUnsignedMin(), ShAmtMax = Other.getUnsignedMax();
1621 APInt NewL = Min.sshl_sat(Min.isNonNegative() ? ShAmtMin : ShAmtMax);
1622 APInt NewU = Max.sshl_sat(Max.isNegative() ? ShAmtMin : ShAmtMax) + 1;
1623 return getNonEmpty(std::move(NewL), std::move(NewU));
1624}
1625
1627 if (isFullSet())
1628 return getEmpty();
1629 if (isEmptySet())
1630 return getFull();
1631 return ConstantRange(Upper, Lower);
1632}
1633
1634ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
1635 if (isEmptySet())
1636 return getEmpty();
1637
1638 if (isSignWrappedSet()) {
1639 APInt Lo;
1640 // Check whether the range crosses zero.
1641 if (Upper.isStrictlyPositive() || !Lower.isStrictlyPositive())
1643 else
1644 Lo = APIntOps::umin(Lower, -Upper + 1);
1645
1646 // If SignedMin is not poison, then it is included in the result range.
1647 if (IntMinIsPoison)
1649 else
1651 }
1652
1654
1655 // Skip SignedMin if it is poison.
1656 if (IntMinIsPoison && SMin.isMinSignedValue()) {
1657 // The range may become empty if it *only* contains SignedMin.
1658 if (SMax.isMinSignedValue())
1659 return getEmpty();
1660 ++SMin;
1661 }
1662
1663 // All non-negative.
1664 if (SMin.isNonNegative())
1665 return ConstantRange(SMin, SMax + 1);
1666
1667 // All negative.
1668 if (SMax.isNegative())
1669 return ConstantRange(-SMax, -SMin + 1);
1670
1671 // Range crosses zero.
1673 APIntOps::umax(-SMin, SMax) + 1);
1674}
1675
1676ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const {
1677 if (isEmptySet())
1678 return getEmpty();
1679
1681 if (ZeroIsPoison && contains(Zero)) {
1682 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1683 // which a zero can appear:
1684 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1685 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1686 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1687
1688 if (getLower().isZero()) {
1689 if ((getUpper() - 1).isZero()) {
1690 // We have in input interval of kind [0, 1). In this case we cannot
1691 // really help but return empty-set.
1692 return getEmpty();
1693 }
1694
1695 // Compute the resulting range by excluding zero from Lower.
1696 return ConstantRange(
1697 APInt(getBitWidth(), (getUpper() - 1).countl_zero()),
1698 APInt(getBitWidth(), (getLower() + 1).countl_zero() + 1));
1699 } else if ((getUpper() - 1).isZero()) {
1700 // Compute the resulting range by excluding zero from Upper.
1701 return ConstantRange(Zero,
1703 } else {
1704 return ConstantRange(Zero, APInt(getBitWidth(), getBitWidth()));
1705 }
1706 }
1707
1708 // Zero is either safe or not in the range. The output range is composed by
1709 // the result of countLeadingZero of the two extremes.
1712}
1713
1715 const ConstantRange &Other) const {
1716 if (isEmptySet() || Other.isEmptySet())
1718
1719 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1720 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1721
1722 // a u+ b overflows high iff a u> ~b.
1723 if (Min.ugt(~OtherMin))
1725 if (Max.ugt(~OtherMax))
1728}
1729
1731 const ConstantRange &Other) const {
1732 if (isEmptySet() || Other.isEmptySet())
1734
1735 APInt Min = getSignedMin(), Max = getSignedMax();
1736 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1737
1740
1741 // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b.
1742 // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b.
1743 if (Min.isNonNegative() && OtherMin.isNonNegative() &&
1744 Min.sgt(SignedMax - OtherMin))
1746 if (Max.isNegative() && OtherMax.isNegative() &&
1747 Max.slt(SignedMin - OtherMax))
1749
1750 if (Max.isNonNegative() && OtherMax.isNonNegative() &&
1751 Max.sgt(SignedMax - OtherMax))
1753 if (Min.isNegative() && OtherMin.isNegative() &&
1754 Min.slt(SignedMin - OtherMin))
1756
1758}
1759
1761 const ConstantRange &Other) const {
1762 if (isEmptySet() || Other.isEmptySet())
1764
1765 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1766 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1767
1768 // a u- b overflows low iff a u< b.
1769 if (Max.ult(OtherMin))
1771 if (Min.ult(OtherMax))
1774}
1775
1777 const ConstantRange &Other) const {
1778 if (isEmptySet() || Other.isEmptySet())
1780
1781 APInt Min = getSignedMin(), Max = getSignedMax();
1782 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1783
1786
1787 // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b.
1788 // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b.
1789 if (Min.isNonNegative() && OtherMax.isNegative() &&
1790 Min.sgt(SignedMax + OtherMax))
1792 if (Max.isNegative() && OtherMin.isNonNegative() &&
1793 Max.slt(SignedMin + OtherMin))
1795
1796 if (Max.isNonNegative() && OtherMin.isNegative() &&
1797 Max.sgt(SignedMax + OtherMin))
1799 if (Min.isNegative() && OtherMax.isNonNegative() &&
1800 Min.slt(SignedMin + OtherMax))
1802
1804}
1805
1807 const ConstantRange &Other) const {
1808 if (isEmptySet() || Other.isEmptySet())
1810
1811 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1812 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1813 bool Overflow;
1814
1815 (void) Min.umul_ov(OtherMin, Overflow);
1816 if (Overflow)
1818
1819 (void) Max.umul_ov(OtherMax, Overflow);
1820 if (Overflow)
1822
1824}
1825
1827 if (isFullSet())
1828 OS << "full-set";
1829 else if (isEmptySet())
1830 OS << "empty-set";
1831 else
1832 OS << "[" << Lower << "," << Upper << ")";
1833}
1834
1835#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1837 print(dbgs());
1838}
1839#endif
1840
1842 const unsigned NumRanges = Ranges.getNumOperands() / 2;
1843 assert(NumRanges >= 1 && "Must have at least one range!");
1844 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
1845
1846 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
1847 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
1848
1849 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
1850
1851 for (unsigned i = 1; i < NumRanges; ++i) {
1852 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
1853 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
1854
1855 // Note: unionWith will potentially create a range that contains values not
1856 // contained in any of the original N ranges.
1857 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
1858 }
1859
1860 return CR;
1861}
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:492
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)
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:522
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:75
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1968
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2045
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1570
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:214
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1389
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:972
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:415
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1467
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:897
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:186
APInt smul_sat(const APInt &RHS) const
Definition: APInt.cpp:2054
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2016
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1183
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:354
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1164
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:366
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1443
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1093
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:189
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:409
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:196
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:312
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1641
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:199
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2076
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2090
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:339
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1399
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2026
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:815
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1301
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:459
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1957
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:317
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1132
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:945
APInt umul_sat(const APInt &RHS) const
Definition: APInt.cpp:2067
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:289
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1112
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:279
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:177
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:269
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:222
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:839
unsigned countr_one() const
Count the number of trailing one bits.
Definition: APInt.h:1613
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1203
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1406
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2035
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition: APInt.h:391
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:711
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:740
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:741
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:735
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:734
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:738
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:736
@ ICMP_EQ
equal
Definition: InstrTypes.h:732
@ ICMP_NE
not equal
Definition: InstrTypes.h:733
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:739
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:737
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:825
Predicate getFlippedSignednessPredicate()
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert.
Definition: InstrTypes.h:1004
bool isIntPredicate() const
Definition: InstrTypes.h:819
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition: InstrTypes.h:957
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 bool areInsensitiveToSignednessOfICmpPredicate(const ConstantRange &CR1, const ConstantRange &CR2)
Return true iff CR1 ult CR2 is equivalent to CR1 slt CR2.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition: ConstantRange.h:84
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.
bool isBinaryOp() const
Definition: Instruction.h:173
Metadata node.
Definition: Metadata.h:950
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:75
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:2970
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:2731
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:2749
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition: APInt.h:2187
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition: APInt.h:2192
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition: APInt.h:2197
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition: APInt.h:2202
@ 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
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
@ Offset
Definition: DWP.cpp:440
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:354
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:245
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ UMin
Unisgned 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()).
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:334
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:184
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:1946
Definition: BitVector.h:858
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:292
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:99
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:63
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:136
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:120
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:96