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 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 // 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 return Lower == Upper && Lower.isMaxValue();
369}
370
372 return Lower == Upper && Lower.isMinValue();
373}
374
376 return Lower.ugt(Upper) && !Upper.isZero();
377}
378
380 return Lower.ugt(Upper);
381}
382
384 return Lower.sgt(Upper) && !Upper.isMinSignedValue();
385}
386
388 return Lower.sgt(Upper);
389}
390
391bool
393 assert(getBitWidth() == Other.getBitWidth());
394 if (isFullSet())
395 return false;
396 if (Other.isFullSet())
397 return true;
398 return (Upper - Lower).ult(Other.Upper - Other.Lower);
399}
400
401bool
403 // If this a full set, we need special handling to avoid needing an extra bit
404 // to represent the size.
405 if (isFullSet())
406 return MaxSize == 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
407
408 return (Upper - Lower).ugt(MaxSize);
409}
410
412 // Empty set is all negative, full set is not.
413 if (isEmptySet())
414 return true;
415 if (isFullSet())
416 return false;
417
418 return !isUpperSignWrapped() && !Upper.isStrictlyPositive();
419}
420
422 // Empty and full set are automatically treated correctly.
423 return !isSignWrappedSet() && Lower.isNonNegative();
424}
425
427 if (isFullSet() || isUpperWrapped())
429 return getUpper() - 1;
430}
431
433 if (isFullSet() || isWrappedSet())
435 return getLower();
436}
437
439 if (isFullSet() || isUpperSignWrapped())
441 return getUpper() - 1;
442}
443
445 if (isFullSet() || isSignWrappedSet())
447 return getLower();
448}
449
450bool ConstantRange::contains(const APInt &V) const {
451 if (Lower == Upper)
452 return isFullSet();
453
454 if (!isUpperWrapped())
455 return Lower.ule(V) && V.ult(Upper);
456 return Lower.ule(V) || V.ult(Upper);
457}
458
460 if (isFullSet() || Other.isEmptySet()) return true;
461 if (isEmptySet() || Other.isFullSet()) return false;
462
463 if (!isUpperWrapped()) {
464 if (Other.isUpperWrapped())
465 return false;
466
467 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
468 }
469
470 if (!Other.isUpperWrapped())
471 return Other.getUpper().ule(Upper) ||
472 Lower.ule(Other.getLower());
473
474 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
475}
476
478 if (isEmptySet())
479 return 0;
480
481 return getUnsignedMax().getActiveBits();
482}
483
485 if (isEmptySet())
486 return 0;
487
488 return std::max(getSignedMin().getSignificantBits(),
489 getSignedMax().getSignificantBits());
490}
491
493 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
494 // If the set is empty or full, don't modify the endpoints.
495 if (Lower == Upper)
496 return *this;
497 return ConstantRange(Lower - Val, Upper - Val);
498}
499
501 return intersectWith(CR.inverse());
502}
503
505 const ConstantRange &CR1, const ConstantRange &CR2,
508 if (!CR1.isWrappedSet() && CR2.isWrappedSet())
509 return CR1;
510 if (CR1.isWrappedSet() && !CR2.isWrappedSet())
511 return CR2;
512 } else if (Type == ConstantRange::Signed) {
513 if (!CR1.isSignWrappedSet() && CR2.isSignWrappedSet())
514 return CR1;
515 if (CR1.isSignWrappedSet() && !CR2.isSignWrappedSet())
516 return CR2;
517 }
518
519 if (CR1.isSizeStrictlySmallerThan(CR2))
520 return CR1;
521 return CR2;
522}
523
525 PreferredRangeType Type) const {
526 assert(getBitWidth() == CR.getBitWidth() &&
527 "ConstantRange types don't agree!");
528
529 // Handle common cases.
530 if ( isEmptySet() || CR.isFullSet()) return *this;
531 if (CR.isEmptySet() || isFullSet()) return CR;
532
533 if (!isUpperWrapped() && CR.isUpperWrapped())
534 return CR.intersectWith(*this, Type);
535
536 if (!isUpperWrapped() && !CR.isUpperWrapped()) {
537 if (Lower.ult(CR.Lower)) {
538 // L---U : this
539 // L---U : CR
540 if (Upper.ule(CR.Lower))
541 return getEmpty();
542
543 // L---U : this
544 // L---U : CR
545 if (Upper.ult(CR.Upper))
546 return ConstantRange(CR.Lower, Upper);
547
548 // L-------U : this
549 // L---U : CR
550 return CR;
551 }
552 // L---U : this
553 // L-------U : CR
554 if (Upper.ult(CR.Upper))
555 return *this;
556
557 // L-----U : this
558 // L-----U : CR
559 if (Lower.ult(CR.Upper))
560 return ConstantRange(Lower, CR.Upper);
561
562 // L---U : this
563 // L---U : CR
564 return getEmpty();
565 }
566
567 if (isUpperWrapped() && !CR.isUpperWrapped()) {
568 if (CR.Lower.ult(Upper)) {
569 // ------U L--- : this
570 // L--U : CR
571 if (CR.Upper.ult(Upper))
572 return CR;
573
574 // ------U L--- : this
575 // L------U : CR
576 if (CR.Upper.ule(Lower))
577 return ConstantRange(CR.Lower, Upper);
578
579 // ------U L--- : this
580 // L----------U : CR
581 return getPreferredRange(*this, CR, Type);
582 }
583 if (CR.Lower.ult(Lower)) {
584 // --U L---- : this
585 // L--U : CR
586 if (CR.Upper.ule(Lower))
587 return getEmpty();
588
589 // --U L---- : this
590 // L------U : CR
591 return ConstantRange(Lower, CR.Upper);
592 }
593
594 // --U L------ : this
595 // L--U : CR
596 return CR;
597 }
598
599 if (CR.Upper.ult(Upper)) {
600 // ------U L-- : this
601 // --U L------ : CR
602 if (CR.Lower.ult(Upper))
603 return getPreferredRange(*this, CR, Type);
604
605 // ----U L-- : this
606 // --U L---- : CR
607 if (CR.Lower.ult(Lower))
608 return ConstantRange(Lower, CR.Upper);
609
610 // ----U L---- : this
611 // --U L-- : CR
612 return CR;
613 }
614 if (CR.Upper.ule(Lower)) {
615 // --U L-- : this
616 // ----U L---- : CR
617 if (CR.Lower.ult(Lower))
618 return *this;
619
620 // --U L---- : this
621 // ----U L-- : CR
622 return ConstantRange(CR.Lower, Upper);
623 }
624
625 // --U L------ : this
626 // ------U L-- : CR
627 return getPreferredRange(*this, CR, Type);
628}
629
631 PreferredRangeType Type) const {
632 assert(getBitWidth() == CR.getBitWidth() &&
633 "ConstantRange types don't agree!");
634
635 if ( isFullSet() || CR.isEmptySet()) return *this;
636 if (CR.isFullSet() || isEmptySet()) return CR;
637
638 if (!isUpperWrapped() && CR.isUpperWrapped())
639 return CR.unionWith(*this, Type);
640
641 if (!isUpperWrapped() && !CR.isUpperWrapped()) {
642 // L---U and L---U : this
643 // L---U L---U : CR
644 // result in one of
645 // L---------U
646 // -----U L-----
647 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower))
648 return getPreferredRange(
649 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
650
651 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
652 APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
653
654 if (L.isZero() && U.isZero())
655 return getFull();
656
657 return ConstantRange(std::move(L), std::move(U));
658 }
659
660 if (!CR.isUpperWrapped()) {
661 // ------U L----- and ------U L----- : this
662 // L--U L--U : CR
663 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
664 return *this;
665
666 // ------U L----- : this
667 // L---------U : CR
668 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
669 return getFull();
670
671 // ----U L---- : this
672 // L---U : CR
673 // results in one of
674 // ----------U L----
675 // ----U L----------
676 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower))
677 return getPreferredRange(
678 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
679
680 // ----U L----- : this
681 // L----U : CR
682 if (Upper.ult(CR.Lower) && Lower.ule(CR.Upper))
683 return ConstantRange(CR.Lower, Upper);
684
685 // ------U L---- : this
686 // L-----U : CR
687 assert(CR.Lower.ule(Upper) && CR.Upper.ult(Lower) &&
688 "ConstantRange::unionWith missed a case with one range wrapped");
689 return ConstantRange(Lower, CR.Upper);
690 }
691
692 // ------U L---- and ------U L---- : this
693 // -U L----------- and ------------U L : CR
694 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
695 return getFull();
696
697 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
698 APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
699
700 return ConstantRange(std::move(L), std::move(U));
701}
702
703std::optional<ConstantRange>
705 // TODO: This can be implemented more efficiently.
706 ConstantRange Result = intersectWith(CR);
707 if (Result == inverse().unionWith(CR.inverse()).inverse())
708 return Result;
709 return std::nullopt;
710}
711
712std::optional<ConstantRange>
714 // TODO: This can be implemented more efficiently.
715 ConstantRange Result = unionWith(CR);
716 if (Result == inverse().intersectWith(CR.inverse()).inverse())
717 return Result;
718 return std::nullopt;
719}
720
722 uint32_t ResultBitWidth) const {
723 switch (CastOp) {
724 default:
725 llvm_unreachable("unsupported cast type");
726 case Instruction::Trunc:
727 return truncate(ResultBitWidth);
728 case Instruction::SExt:
729 return signExtend(ResultBitWidth);
730 case Instruction::ZExt:
731 return zeroExtend(ResultBitWidth);
732 case Instruction::BitCast:
733 return *this;
734 case Instruction::FPToUI:
735 case Instruction::FPToSI:
736 if (getBitWidth() == ResultBitWidth)
737 return *this;
738 else
739 return getFull(ResultBitWidth);
740 case Instruction::UIToFP: {
741 // TODO: use input range if available
742 auto BW = getBitWidth();
743 APInt Min = APInt::getMinValue(BW);
744 APInt Max = APInt::getMaxValue(BW);
745 if (ResultBitWidth > BW) {
746 Min = Min.zext(ResultBitWidth);
747 Max = Max.zext(ResultBitWidth);
748 }
749 return getNonEmpty(std::move(Min), std::move(Max) + 1);
750 }
751 case Instruction::SIToFP: {
752 // TODO: use input range if available
753 auto BW = getBitWidth();
756 if (ResultBitWidth > BW) {
757 SMin = SMin.sext(ResultBitWidth);
758 SMax = SMax.sext(ResultBitWidth);
759 }
760 return getNonEmpty(std::move(SMin), std::move(SMax) + 1);
761 }
762 case Instruction::FPTrunc:
763 case Instruction::FPExt:
764 case Instruction::IntToPtr:
765 case Instruction::PtrToInt:
766 case Instruction::AddrSpaceCast:
767 // Conservatively return getFull set.
768 return getFull(ResultBitWidth);
769 };
770}
771
773 if (isEmptySet()) return getEmpty(DstTySize);
774
775 unsigned SrcTySize = getBitWidth();
776 assert(SrcTySize < DstTySize && "Not a value extension");
777 if (isFullSet() || isUpperWrapped()) {
778 // Change into [0, 1 << src bit width)
779 APInt LowerExt(DstTySize, 0);
780 if (!Upper) // special case: [X, 0) -- not really wrapping around
781 LowerExt = Lower.zext(DstTySize);
782 return ConstantRange(std::move(LowerExt),
783 APInt::getOneBitSet(DstTySize, SrcTySize));
784 }
785
786 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
787}
788
790 if (isEmptySet()) return getEmpty(DstTySize);
791
792 unsigned SrcTySize = getBitWidth();
793 assert(SrcTySize < DstTySize && "Not a value extension");
794
795 // special case: [X, INT_MIN) -- not really wrapping around
796 if (Upper.isMinSignedValue())
797 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
798
799 if (isFullSet() || isSignWrappedSet()) {
800 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
801 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
802 }
803
804 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
805}
806
808 assert(getBitWidth() > DstTySize && "Not a value truncation");
809 if (isEmptySet())
810 return getEmpty(DstTySize);
811 if (isFullSet())
812 return getFull(DstTySize);
813
814 APInt LowerDiv(Lower), UpperDiv(Upper);
815 ConstantRange Union(DstTySize, /*isFullSet=*/false);
816
817 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
818 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
819 // then we do the union with [MaxValue, Upper)
820 if (isUpperWrapped()) {
821 // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
822 // truncated range.
823 if (Upper.getActiveBits() > DstTySize || Upper.countr_one() == DstTySize)
824 return getFull(DstTySize);
825
826 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
827 UpperDiv.setAllBits();
828
829 // Union covers the MaxValue case, so return if the remaining range is just
830 // MaxValue(DstTy).
831 if (LowerDiv == UpperDiv)
832 return Union;
833 }
834
835 // Chop off the most significant bits that are past the destination bitwidth.
836 if (LowerDiv.getActiveBits() > DstTySize) {
837 // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv.
838 APInt Adjust = LowerDiv & APInt::getBitsSetFrom(getBitWidth(), DstTySize);
839 LowerDiv -= Adjust;
840 UpperDiv -= Adjust;
841 }
842
843 unsigned UpperDivWidth = UpperDiv.getActiveBits();
844 if (UpperDivWidth <= DstTySize)
845 return ConstantRange(LowerDiv.trunc(DstTySize),
846 UpperDiv.trunc(DstTySize)).unionWith(Union);
847
848 // The truncated value wraps around. Check if we can do better than fullset.
849 if (UpperDivWidth == DstTySize + 1) {
850 // Clear the MSB so that UpperDiv wraps around.
851 UpperDiv.clearBit(DstTySize);
852 if (UpperDiv.ult(LowerDiv))
853 return ConstantRange(LowerDiv.trunc(DstTySize),
854 UpperDiv.trunc(DstTySize)).unionWith(Union);
855 }
856
857 return getFull(DstTySize);
858}
859
861 unsigned SrcTySize = getBitWidth();
862 if (SrcTySize > DstTySize)
863 return truncate(DstTySize);
864 if (SrcTySize < DstTySize)
865 return zeroExtend(DstTySize);
866 return *this;
867}
868
870 unsigned SrcTySize = getBitWidth();
871 if (SrcTySize > DstTySize)
872 return truncate(DstTySize);
873 if (SrcTySize < DstTySize)
874 return signExtend(DstTySize);
875 return *this;
876}
877
879 const ConstantRange &Other) const {
880 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
881
882 switch (BinOp) {
883 case Instruction::Add:
884 return add(Other);
885 case Instruction::Sub:
886 return sub(Other);
887 case Instruction::Mul:
888 return multiply(Other);
889 case Instruction::UDiv:
890 return udiv(Other);
891 case Instruction::SDiv:
892 return sdiv(Other);
893 case Instruction::URem:
894 return urem(Other);
895 case Instruction::SRem:
896 return srem(Other);
897 case Instruction::Shl:
898 return shl(Other);
899 case Instruction::LShr:
900 return lshr(Other);
901 case Instruction::AShr:
902 return ashr(Other);
903 case Instruction::And:
904 return binaryAnd(Other);
905 case Instruction::Or:
906 return binaryOr(Other);
907 case Instruction::Xor:
908 return binaryXor(Other);
909 // Note: floating point operations applied to abstract ranges are just
910 // ideal integer operations with a lossy representation
911 case Instruction::FAdd:
912 return add(Other);
913 case Instruction::FSub:
914 return sub(Other);
915 case Instruction::FMul:
916 return multiply(Other);
917 default:
918 // Conservatively return getFull set.
919 return getFull();
920 }
921}
922
924 const ConstantRange &Other,
925 unsigned NoWrapKind) const {
926 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
927
928 switch (BinOp) {
929 case Instruction::Add:
930 return addWithNoWrap(Other, NoWrapKind);
931 case Instruction::Sub:
932 return subWithNoWrap(Other, NoWrapKind);
933 default:
934 // Don't know about this Overflowing Binary Operation.
935 // Conservatively fallback to plain binop handling.
936 return binaryOp(BinOp, Other);
937 }
938}
939
941 switch (IntrinsicID) {
942 case Intrinsic::uadd_sat:
943 case Intrinsic::usub_sat:
944 case Intrinsic::sadd_sat:
945 case Intrinsic::ssub_sat:
946 case Intrinsic::umin:
947 case Intrinsic::umax:
948 case Intrinsic::smin:
949 case Intrinsic::smax:
950 case Intrinsic::abs:
951 case Intrinsic::ctlz:
952 case Intrinsic::cttz:
953 case Intrinsic::ctpop:
954 return true;
955 default:
956 return false;
957 }
958}
959
962 switch (IntrinsicID) {
963 case Intrinsic::uadd_sat:
964 return Ops[0].uadd_sat(Ops[1]);
965 case Intrinsic::usub_sat:
966 return Ops[0].usub_sat(Ops[1]);
967 case Intrinsic::sadd_sat:
968 return Ops[0].sadd_sat(Ops[1]);
969 case Intrinsic::ssub_sat:
970 return Ops[0].ssub_sat(Ops[1]);
971 case Intrinsic::umin:
972 return Ops[0].umin(Ops[1]);
973 case Intrinsic::umax:
974 return Ops[0].umax(Ops[1]);
975 case Intrinsic::smin:
976 return Ops[0].smin(Ops[1]);
977 case Intrinsic::smax:
978 return Ops[0].smax(Ops[1]);
979 case Intrinsic::abs: {
980 const APInt *IntMinIsPoison = Ops[1].getSingleElement();
981 assert(IntMinIsPoison && "Must be known (immarg)");
982 assert(IntMinIsPoison->getBitWidth() == 1 && "Must be boolean");
983 return Ops[0].abs(IntMinIsPoison->getBoolValue());
984 }
985 case Intrinsic::ctlz: {
986 const APInt *ZeroIsPoison = Ops[1].getSingleElement();
987 assert(ZeroIsPoison && "Must be known (immarg)");
988 assert(ZeroIsPoison->getBitWidth() == 1 && "Must be boolean");
989 return Ops[0].ctlz(ZeroIsPoison->getBoolValue());
990 }
991 case Intrinsic::cttz: {
992 const APInt *ZeroIsPoison = Ops[1].getSingleElement();
993 assert(ZeroIsPoison && "Must be known (immarg)");
994 assert(ZeroIsPoison->getBitWidth() == 1 && "Must be boolean");
995 return Ops[0].cttz(ZeroIsPoison->getBoolValue());
996 }
997 case Intrinsic::ctpop:
998 return Ops[0].ctpop();
999 default:
1000 assert(!isIntrinsicSupported(IntrinsicID) && "Shouldn't be supported");
1001 llvm_unreachable("Unsupported intrinsic");
1002 }
1003}
1004
1007 if (isEmptySet() || Other.isEmptySet())
1008 return getEmpty();
1009 if (isFullSet() || Other.isFullSet())
1010 return getFull();
1011
1012 APInt NewLower = getLower() + Other.getLower();
1013 APInt NewUpper = getUpper() + Other.getUpper() - 1;
1014 if (NewLower == NewUpper)
1015 return getFull();
1016
1017 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1018 if (X.isSizeStrictlySmallerThan(*this) ||
1019 X.isSizeStrictlySmallerThan(Other))
1020 // We've wrapped, therefore, full set.
1021 return getFull();
1022 return X;
1023}
1024
1026 unsigned NoWrapKind,
1027 PreferredRangeType RangeType) const {
1028 // Calculate the range for "X + Y" which is guaranteed not to wrap(overflow).
1029 // (X is from this, and Y is from Other)
1030 if (isEmptySet() || Other.isEmptySet())
1031 return getEmpty();
1032 if (isFullSet() && Other.isFullSet())
1033 return getFull();
1034
1035 using OBO = OverflowingBinaryOperator;
1036 ConstantRange Result = add(Other);
1037
1038 // If an overflow happens for every value pair in these two constant ranges,
1039 // we must return Empty set. In this case, we get that for free, because we
1040 // get lucky that intersection of add() with uadd_sat()/sadd_sat() results
1041 // in an empty set.
1042
1043 if (NoWrapKind & OBO::NoSignedWrap)
1044 Result = Result.intersectWith(sadd_sat(Other), RangeType);
1045
1046 if (NoWrapKind & OBO::NoUnsignedWrap)
1047 Result = Result.intersectWith(uadd_sat(Other), RangeType);
1048
1049 return Result;
1050}
1051
1054 if (isEmptySet() || Other.isEmptySet())
1055 return getEmpty();
1056 if (isFullSet() || Other.isFullSet())
1057 return getFull();
1058
1059 APInt NewLower = getLower() - Other.getUpper() + 1;
1060 APInt NewUpper = getUpper() - Other.getLower();
1061 if (NewLower == NewUpper)
1062 return getFull();
1063
1064 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1065 if (X.isSizeStrictlySmallerThan(*this) ||
1066 X.isSizeStrictlySmallerThan(Other))
1067 // We've wrapped, therefore, full set.
1068 return getFull();
1069 return X;
1070}
1071
1073 unsigned NoWrapKind,
1074 PreferredRangeType RangeType) const {
1075 // Calculate the range for "X - Y" which is guaranteed not to wrap(overflow).
1076 // (X is from this, and Y is from Other)
1077 if (isEmptySet() || Other.isEmptySet())
1078 return getEmpty();
1079 if (isFullSet() && Other.isFullSet())
1080 return getFull();
1081
1082 using OBO = OverflowingBinaryOperator;
1083 ConstantRange Result = sub(Other);
1084
1085 // If an overflow happens for every value pair in these two constant ranges,
1086 // we must return Empty set. In signed case, we get that for free, because we
1087 // get lucky that intersection of sub() with ssub_sat() results in an
1088 // empty set. But for unsigned we must perform the overflow check manually.
1089
1090 if (NoWrapKind & OBO::NoSignedWrap)
1091 Result = Result.intersectWith(ssub_sat(Other), RangeType);
1092
1093 if (NoWrapKind & OBO::NoUnsignedWrap) {
1094 if (getUnsignedMax().ult(Other.getUnsignedMin()))
1095 return getEmpty(); // Always overflows.
1096 Result = Result.intersectWith(usub_sat(Other), RangeType);
1097 }
1098
1099 return Result;
1100}
1101
1104 // TODO: If either operand is a single element and the multiply is known to
1105 // be non-wrapping, round the result min and max value to the appropriate
1106 // multiple of that element. If wrapping is possible, at least adjust the
1107 // range according to the greatest power-of-two factor of the single element.
1108
1109 if (isEmptySet() || Other.isEmptySet())
1110 return getEmpty();
1111
1112 if (const APInt *C = getSingleElement()) {
1113 if (C->isOne())
1114 return Other;
1115 if (C->isAllOnes())
1117 }
1118
1119 if (const APInt *C = Other.getSingleElement()) {
1120 if (C->isOne())
1121 return *this;
1122 if (C->isAllOnes())
1123 return ConstantRange(APInt::getZero(getBitWidth())).sub(*this);
1124 }
1125
1126 // Multiplication is signedness-independent. However different ranges can be
1127 // obtained depending on how the input ranges are treated. These different
1128 // ranges are all conservatively correct, but one might be better than the
1129 // other. We calculate two ranges; one treating the inputs as unsigned
1130 // and the other signed, then return the smallest of these ranges.
1131
1132 // Unsigned range first.
1133 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
1134 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
1135 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
1136 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
1137
1138 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
1139 this_max * Other_max + 1);
1140 ConstantRange UR = Result_zext.truncate(getBitWidth());
1141
1142 // If the unsigned range doesn't wrap, and isn't negative then it's a range
1143 // from one positive number to another which is as good as we can generate.
1144 // In this case, skip the extra work of generating signed ranges which aren't
1145 // going to be better than this range.
1146 if (!UR.isUpperWrapped() &&
1148 return UR;
1149
1150 // Now the signed range. Because we could be dealing with negative numbers
1151 // here, the lower bound is the smallest of the cartesian product of the
1152 // lower and upper ranges; for example:
1153 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1154 // Similarly for the upper bound, swapping min for max.
1155
1156 this_min = getSignedMin().sext(getBitWidth() * 2);
1157 this_max = getSignedMax().sext(getBitWidth() * 2);
1158 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
1159 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
1160
1161 auto L = {this_min * Other_min, this_min * Other_max,
1162 this_max * Other_min, this_max * Other_max};
1163 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1164 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
1165 ConstantRange SR = Result_sext.truncate(getBitWidth());
1166
1167 return UR.isSizeStrictlySmallerThan(SR) ? UR : SR;
1168}
1169
1171 if (isEmptySet() || Other.isEmptySet())
1172 return getEmpty();
1173
1174 APInt Min = getSignedMin();
1175 APInt Max = getSignedMax();
1176 APInt OtherMin = Other.getSignedMin();
1177 APInt OtherMax = Other.getSignedMax();
1178
1179 bool O1, O2, O3, O4;
1180 auto Muls = {Min.smul_ov(OtherMin, O1), Min.smul_ov(OtherMax, O2),
1181 Max.smul_ov(OtherMin, O3), Max.smul_ov(OtherMax, O4)};
1182 if (O1 || O2 || O3 || O4)
1183 return getFull();
1184
1185 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1186 return getNonEmpty(std::min(Muls, Compare), std::max(Muls, Compare) + 1);
1187}
1188
1191 // X smax Y is: range(smax(X_smin, Y_smin),
1192 // smax(X_smax, Y_smax))
1193 if (isEmptySet() || Other.isEmptySet())
1194 return getEmpty();
1195 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
1196 APInt NewU = APIntOps::smax(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 umax Y is: range(umax(X_umin, Y_umin),
1206 // umax(X_umax, Y_umax))
1207 if (isEmptySet() || Other.isEmptySet())
1208 return getEmpty();
1209 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
1210 APInt NewU = APIntOps::umax(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 // X smin Y is: range(smin(X_smin, Y_smin),
1220 // smin(X_smax, Y_smax))
1221 if (isEmptySet() || Other.isEmptySet())
1222 return getEmpty();
1223 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
1224 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
1225 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1226 if (isSignWrappedSet() || Other.isSignWrappedSet())
1227 return Res.intersectWith(unionWith(Other, Signed), Signed);
1228 return Res;
1229}
1230
1233 // X umin Y is: range(umin(X_umin, Y_umin),
1234 // umin(X_umax, Y_umax))
1235 if (isEmptySet() || Other.isEmptySet())
1236 return getEmpty();
1237 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
1238 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
1239 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1240 if (isWrappedSet() || Other.isWrappedSet())
1242 return Res;
1243}
1244
1247 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
1248 return getEmpty();
1249
1250 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
1251
1252 APInt RHS_umin = RHS.getUnsignedMin();
1253 if (RHS_umin.isZero()) {
1254 // We want the lowest value in RHS excluding zero. Usually that would be 1
1255 // except for a range in the form of [X, 1) in which case it would be X.
1256 if (RHS.getUpper() == 1)
1257 RHS_umin = RHS.getLower();
1258 else
1259 RHS_umin = 1;
1260 }
1261
1262 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
1263 return getNonEmpty(std::move(Lower), std::move(Upper));
1264}
1265
1267 // We split up the LHS and RHS into positive and negative components
1268 // and then also compute the positive and negative components of the result
1269 // separately by combining division results with the appropriate signs.
1272 // There are no positive 1-bit values. The 1 would get interpreted as -1.
1273 ConstantRange PosFilter =
1274 getBitWidth() == 1 ? getEmpty()
1275 : ConstantRange(APInt(getBitWidth(), 1), SignedMin);
1276 ConstantRange NegFilter(SignedMin, Zero);
1277 ConstantRange PosL = intersectWith(PosFilter);
1278 ConstantRange NegL = intersectWith(NegFilter);
1279 ConstantRange PosR = RHS.intersectWith(PosFilter);
1280 ConstantRange NegR = RHS.intersectWith(NegFilter);
1281
1282 ConstantRange PosRes = getEmpty();
1283 if (!PosL.isEmptySet() && !PosR.isEmptySet())
1284 // pos / pos = pos.
1285 PosRes = ConstantRange(PosL.Lower.sdiv(PosR.Upper - 1),
1286 (PosL.Upper - 1).sdiv(PosR.Lower) + 1);
1287
1288 if (!NegL.isEmptySet() && !NegR.isEmptySet()) {
1289 // neg / neg = pos.
1290 //
1291 // We need to deal with one tricky case here: SignedMin / -1 is UB on the
1292 // IR level, so we'll want to exclude this case when calculating bounds.
1293 // (For APInts the operation is well-defined and yields SignedMin.) We
1294 // handle this by dropping either SignedMin from the LHS or -1 from the RHS.
1295 APInt Lo = (NegL.Upper - 1).sdiv(NegR.Lower);
1296 if (NegL.Lower.isMinSignedValue() && NegR.Upper.isZero()) {
1297 // Remove -1 from the LHS. Skip if it's the only element, as this would
1298 // leave us with an empty set.
1299 if (!NegR.Lower.isAllOnes()) {
1300 APInt AdjNegRUpper;
1301 if (RHS.Lower.isAllOnes())
1302 // Negative part of [-1, X] without -1 is [SignedMin, X].
1303 AdjNegRUpper = RHS.Upper;
1304 else
1305 // [X, -1] without -1 is [X, -2].
1306 AdjNegRUpper = NegR.Upper - 1;
1307
1308 PosRes = PosRes.unionWith(
1309 ConstantRange(Lo, NegL.Lower.sdiv(AdjNegRUpper - 1) + 1));
1310 }
1311
1312 // Remove SignedMin from the RHS. Skip if it's the only element, as this
1313 // would leave us with an empty set.
1314 if (NegL.Upper != SignedMin + 1) {
1315 APInt AdjNegLLower;
1316 if (Upper == SignedMin + 1)
1317 // Negative part of [X, SignedMin] without SignedMin is [X, -1].
1318 AdjNegLLower = Lower;
1319 else
1320 // [SignedMin, X] without SignedMin is [SignedMin + 1, X].
1321 AdjNegLLower = NegL.Lower + 1;
1322
1323 PosRes = PosRes.unionWith(
1324 ConstantRange(std::move(Lo),
1325 AdjNegLLower.sdiv(NegR.Upper - 1) + 1));
1326 }
1327 } else {
1328 PosRes = PosRes.unionWith(
1329 ConstantRange(std::move(Lo), NegL.Lower.sdiv(NegR.Upper - 1) + 1));
1330 }
1331 }
1332
1333 ConstantRange NegRes = getEmpty();
1334 if (!PosL.isEmptySet() && !NegR.isEmptySet())
1335 // pos / neg = neg.
1336 NegRes = ConstantRange((PosL.Upper - 1).sdiv(NegR.Upper - 1),
1337 PosL.Lower.sdiv(NegR.Lower) + 1);
1338
1339 if (!NegL.isEmptySet() && !PosR.isEmptySet())
1340 // neg / pos = neg.
1341 NegRes = NegRes.unionWith(
1342 ConstantRange(NegL.Lower.sdiv(PosR.Lower),
1343 (NegL.Upper - 1).sdiv(PosR.Upper - 1) + 1));
1344
1345 // Prefer a non-wrapping signed range here.
1347
1348 // Preserve the zero that we dropped when splitting the LHS by sign.
1349 if (contains(Zero) && (!PosR.isEmptySet() || !NegR.isEmptySet()))
1350 Res = Res.unionWith(ConstantRange(Zero));
1351 return Res;
1352}
1353
1355 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
1356 return getEmpty();
1357
1358 if (const APInt *RHSInt = RHS.getSingleElement()) {
1359 // UREM by null is UB.
1360 if (RHSInt->isZero())
1361 return getEmpty();
1362 // Use APInt's implementation of UREM for single element ranges.
1363 if (const APInt *LHSInt = getSingleElement())
1364 return {LHSInt->urem(*RHSInt)};
1365 }
1366
1367 // L % R for L < R is L.
1368 if (getUnsignedMax().ult(RHS.getUnsignedMin()))
1369 return *this;
1370
1371 // L % R is <= L and < R.
1372 APInt Upper = APIntOps::umin(getUnsignedMax(), RHS.getUnsignedMax() - 1) + 1;
1373 return getNonEmpty(APInt::getZero(getBitWidth()), std::move(Upper));
1374}
1375
1377 if (isEmptySet() || RHS.isEmptySet())
1378 return getEmpty();
1379
1380 if (const APInt *RHSInt = RHS.getSingleElement()) {
1381 // SREM by null is UB.
1382 if (RHSInt->isZero())
1383 return getEmpty();
1384 // Use APInt's implementation of SREM for single element ranges.
1385 if (const APInt *LHSInt = getSingleElement())
1386 return {LHSInt->srem(*RHSInt)};
1387 }
1388
1389 ConstantRange AbsRHS = RHS.abs();
1390 APInt MinAbsRHS = AbsRHS.getUnsignedMin();
1391 APInt MaxAbsRHS = AbsRHS.getUnsignedMax();
1392
1393 // Modulus by zero is UB.
1394 if (MaxAbsRHS.isZero())
1395 return getEmpty();
1396
1397 if (MinAbsRHS.isZero())
1398 ++MinAbsRHS;
1399
1400 APInt MinLHS = getSignedMin(), MaxLHS = getSignedMax();
1401
1402 if (MinLHS.isNonNegative()) {
1403 // L % R for L < R is L.
1404 if (MaxLHS.ult(MinAbsRHS))
1405 return *this;
1406
1407 // L % R is <= L and < R.
1408 APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1409 return ConstantRange(APInt::getZero(getBitWidth()), std::move(Upper));
1410 }
1411
1412 // Same basic logic as above, but the result is negative.
1413 if (MaxLHS.isNegative()) {
1414 if (MinLHS.ugt(-MinAbsRHS))
1415 return *this;
1416
1417 APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1418 return ConstantRange(std::move(Lower), APInt(getBitWidth(), 1));
1419 }
1420
1421 // LHS range crosses zero.
1422 APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1423 APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1424 return ConstantRange(std::move(Lower), std::move(Upper));
1425}
1426
1429}
1430
1432 if (isEmptySet() || Other.isEmptySet())
1433 return getEmpty();
1434
1435 ConstantRange KnownBitsRange =
1436 fromKnownBits(toKnownBits() & Other.toKnownBits(), false);
1437 ConstantRange UMinUMaxRange =
1439 APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()) + 1);
1440 return KnownBitsRange.intersectWith(UMinUMaxRange);
1441}
1442
1444 if (isEmptySet() || Other.isEmptySet())
1445 return getEmpty();
1446
1447 ConstantRange KnownBitsRange =
1448 fromKnownBits(toKnownBits() | Other.toKnownBits(), false);
1449 // Upper wrapped range.
1450 ConstantRange UMaxUMinRange =
1451 getNonEmpty(APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()),
1453 return KnownBitsRange.intersectWith(UMaxUMinRange);
1454}
1455
1457 if (isEmptySet() || Other.isEmptySet())
1458 return getEmpty();
1459
1460 // Use APInt's implementation of XOR for single element ranges.
1461 if (isSingleElement() && Other.isSingleElement())
1462 return {*getSingleElement() ^ *Other.getSingleElement()};
1463
1464 // Special-case binary complement, since we can give a precise answer.
1465 if (Other.isSingleElement() && Other.getSingleElement()->isAllOnes())
1466 return binaryNot();
1467 if (isSingleElement() && getSingleElement()->isAllOnes())
1468 return Other.binaryNot();
1469
1470 KnownBits LHSKnown = toKnownBits();
1471 KnownBits RHSKnown = Other.toKnownBits();
1472 KnownBits Known = LHSKnown ^ RHSKnown;
1473 ConstantRange CR = fromKnownBits(Known, /*IsSigned*/ false);
1474 // Typically the following code doesn't improve the result if BW = 1.
1475 if (getBitWidth() == 1)
1476 return CR;
1477
1478 // If LHS is known to be the subset of RHS, treat LHS ^ RHS as RHS -nuw/nsw
1479 // LHS. If RHS is known to be the subset of LHS, treat LHS ^ RHS as LHS
1480 // -nuw/nsw RHS.
1481 if ((~LHSKnown.Zero).isSubsetOf(RHSKnown.One))
1483 else if ((~RHSKnown.Zero).isSubsetOf(LHSKnown.One))
1484 CR = CR.intersectWith(this->sub(Other), PreferredRangeType::Unsigned);
1485 return CR;
1486}
1487
1490 if (isEmptySet() || Other.isEmptySet())
1491 return getEmpty();
1492
1493 APInt Min = getUnsignedMin();
1494 APInt Max = getUnsignedMax();
1495 if (const APInt *RHS = Other.getSingleElement()) {
1496 unsigned BW = getBitWidth();
1497 if (RHS->uge(BW))
1498 return getEmpty();
1499
1500 unsigned EqualLeadingBits = (Min ^ Max).countl_zero();
1501 if (RHS->ule(EqualLeadingBits))
1502 return getNonEmpty(Min << *RHS, (Max << *RHS) + 1);
1503
1504 return getNonEmpty(APInt::getZero(BW),
1505 APInt::getBitsSetFrom(BW, RHS->getZExtValue()) + 1);
1506 }
1507
1508 APInt OtherMax = Other.getUnsignedMax();
1509 if (isAllNegative() && OtherMax.ule(Min.countl_one())) {
1510 // For negative numbers, if the shift does not overflow in a signed sense,
1511 // a larger shift will make the number smaller.
1512 Max <<= Other.getUnsignedMin();
1513 Min <<= OtherMax;
1514 return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1515 }
1516
1517 // There's overflow!
1518 if (OtherMax.ugt(Max.countl_zero()))
1519 return getFull();
1520
1521 // FIXME: implement the other tricky cases
1522
1523 Min <<= Other.getUnsignedMin();
1524 Max <<= OtherMax;
1525
1526 return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1527}
1528
1531 if (isEmptySet() || Other.isEmptySet())
1532 return getEmpty();
1533
1534 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1;
1535 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
1536 return getNonEmpty(std::move(min), std::move(max));
1537}
1538
1541 if (isEmptySet() || Other.isEmptySet())
1542 return getEmpty();
1543
1544 // May straddle zero, so handle both positive and negative cases.
1545 // 'PosMax' is the upper bound of the result of the ashr
1546 // operation, when Upper of the LHS of ashr is a non-negative.
1547 // number. Since ashr of a non-negative number will result in a
1548 // smaller number, the Upper value of LHS is shifted right with
1549 // the minimum value of 'Other' instead of the maximum value.
1550 APInt PosMax = getSignedMax().ashr(Other.getUnsignedMin()) + 1;
1551
1552 // 'PosMin' is the lower bound of the result of the ashr
1553 // operation, when Lower of the LHS is a non-negative number.
1554 // Since ashr of a non-negative number will result in a smaller
1555 // number, the Lower value of LHS is shifted right with the
1556 // maximum value of 'Other'.
1557 APInt PosMin = getSignedMin().ashr(Other.getUnsignedMax());
1558
1559 // 'NegMax' is the upper bound of the result of the ashr
1560 // operation, when Upper of the LHS of ashr is a negative number.
1561 // Since 'ashr' of a negative number will result in a bigger
1562 // number, the Upper value of LHS is shifted right with the
1563 // maximum value of 'Other'.
1564 APInt NegMax = getSignedMax().ashr(Other.getUnsignedMax()) + 1;
1565
1566 // 'NegMin' is the lower bound of the result of the ashr
1567 // operation, when Lower of the LHS of ashr is a negative number.
1568 // Since 'ashr' of a negative number will result in a bigger
1569 // number, the Lower value of LHS is shifted right with the
1570 // minimum value of 'Other'.
1571 APInt NegMin = getSignedMin().ashr(Other.getUnsignedMin());
1572
1573 APInt max, min;
1574 if (getSignedMin().isNonNegative()) {
1575 // Upper and Lower of LHS are non-negative.
1576 min = PosMin;
1577 max = PosMax;
1578 } else if (getSignedMax().isNegative()) {
1579 // Upper and Lower of LHS are negative.
1580 min = NegMin;
1581 max = NegMax;
1582 } else {
1583 // Upper is non-negative and Lower is negative.
1584 min = NegMin;
1585 max = PosMax;
1586 }
1587 return getNonEmpty(std::move(min), std::move(max));
1588}
1589
1591 if (isEmptySet() || Other.isEmptySet())
1592 return getEmpty();
1593
1594 APInt NewL = getUnsignedMin().uadd_sat(Other.getUnsignedMin());
1595 APInt NewU = getUnsignedMax().uadd_sat(Other.getUnsignedMax()) + 1;
1596 return getNonEmpty(std::move(NewL), std::move(NewU));
1597}
1598
1600 if (isEmptySet() || Other.isEmptySet())
1601 return getEmpty();
1602
1603 APInt NewL = getSignedMin().sadd_sat(Other.getSignedMin());
1604 APInt NewU = getSignedMax().sadd_sat(Other.getSignedMax()) + 1;
1605 return getNonEmpty(std::move(NewL), std::move(NewU));
1606}
1607
1609 if (isEmptySet() || Other.isEmptySet())
1610 return getEmpty();
1611
1612 APInt NewL = getUnsignedMin().usub_sat(Other.getUnsignedMax());
1613 APInt NewU = getUnsignedMax().usub_sat(Other.getUnsignedMin()) + 1;
1614 return getNonEmpty(std::move(NewL), std::move(NewU));
1615}
1616
1618 if (isEmptySet() || Other.isEmptySet())
1619 return getEmpty();
1620
1621 APInt NewL = getSignedMin().ssub_sat(Other.getSignedMax());
1622 APInt NewU = getSignedMax().ssub_sat(Other.getSignedMin()) + 1;
1623 return getNonEmpty(std::move(NewL), std::move(NewU));
1624}
1625
1627 if (isEmptySet() || Other.isEmptySet())
1628 return getEmpty();
1629
1630 APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
1631 APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
1632 return getNonEmpty(std::move(NewL), std::move(NewU));
1633}
1634
1636 if (isEmptySet() || Other.isEmptySet())
1637 return getEmpty();
1638
1639 // Because we could be dealing with negative numbers here, the lower bound is
1640 // the smallest of the cartesian product of the lower and upper ranges;
1641 // for example:
1642 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1643 // Similarly for the upper bound, swapping min for max.
1644
1645 APInt Min = getSignedMin();
1646 APInt Max = getSignedMax();
1647 APInt OtherMin = Other.getSignedMin();
1648 APInt OtherMax = Other.getSignedMax();
1649
1650 auto L = {Min.smul_sat(OtherMin), Min.smul_sat(OtherMax),
1651 Max.smul_sat(OtherMin), Max.smul_sat(OtherMax)};
1652 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1653 return getNonEmpty(std::min(L, Compare), std::max(L, Compare) + 1);
1654}
1655
1657 if (isEmptySet() || Other.isEmptySet())
1658 return getEmpty();
1659
1660 APInt NewL = getUnsignedMin().ushl_sat(Other.getUnsignedMin());
1661 APInt NewU = getUnsignedMax().ushl_sat(Other.getUnsignedMax()) + 1;
1662 return getNonEmpty(std::move(NewL), std::move(NewU));
1663}
1664
1666 if (isEmptySet() || Other.isEmptySet())
1667 return getEmpty();
1668
1669 APInt Min = getSignedMin(), Max = getSignedMax();
1670 APInt ShAmtMin = Other.getUnsignedMin(), ShAmtMax = Other.getUnsignedMax();
1671 APInt NewL = Min.sshl_sat(Min.isNonNegative() ? ShAmtMin : ShAmtMax);
1672 APInt NewU = Max.sshl_sat(Max.isNegative() ? ShAmtMin : ShAmtMax) + 1;
1673 return getNonEmpty(std::move(NewL), std::move(NewU));
1674}
1675
1677 if (isFullSet())
1678 return getEmpty();
1679 if (isEmptySet())
1680 return getFull();
1681 return ConstantRange(Upper, Lower);
1682}
1683
1684ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
1685 if (isEmptySet())
1686 return getEmpty();
1687
1688 if (isSignWrappedSet()) {
1689 APInt Lo;
1690 // Check whether the range crosses zero.
1691 if (Upper.isStrictlyPositive() || !Lower.isStrictlyPositive())
1693 else
1694 Lo = APIntOps::umin(Lower, -Upper + 1);
1695
1696 // If SignedMin is not poison, then it is included in the result range.
1697 if (IntMinIsPoison)
1699 else
1701 }
1702
1704
1705 // Skip SignedMin if it is poison.
1706 if (IntMinIsPoison && SMin.isMinSignedValue()) {
1707 // The range may become empty if it *only* contains SignedMin.
1708 if (SMax.isMinSignedValue())
1709 return getEmpty();
1710 ++SMin;
1711 }
1712
1713 // All non-negative.
1714 if (SMin.isNonNegative())
1715 return ConstantRange(SMin, SMax + 1);
1716
1717 // All negative.
1718 if (SMax.isNegative())
1719 return ConstantRange(-SMax, -SMin + 1);
1720
1721 // Range crosses zero.
1723 APIntOps::umax(-SMin, SMax) + 1);
1724}
1725
1726ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const {
1727 if (isEmptySet())
1728 return getEmpty();
1729
1731 if (ZeroIsPoison && contains(Zero)) {
1732 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1733 // which a zero can appear:
1734 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1735 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1736 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1737
1738 if (getLower().isZero()) {
1739 if ((getUpper() - 1).isZero()) {
1740 // We have in input interval of kind [0, 1). In this case we cannot
1741 // really help but return empty-set.
1742 return getEmpty();
1743 }
1744
1745 // Compute the resulting range by excluding zero from Lower.
1746 return ConstantRange(
1747 APInt(getBitWidth(), (getUpper() - 1).countl_zero()),
1748 APInt(getBitWidth(), (getLower() + 1).countl_zero() + 1));
1749 } else if ((getUpper() - 1).isZero()) {
1750 // Compute the resulting range by excluding zero from Upper.
1751 return ConstantRange(Zero,
1753 } else {
1754 return ConstantRange(Zero, APInt(getBitWidth(), getBitWidth()));
1755 }
1756 }
1757
1758 // Zero is either safe or not in the range. The output range is composed by
1759 // the result of countLeadingZero of the two extremes.
1762}
1763
1765 const APInt &Upper) {
1766 assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
1767 "Unexpected wrapped set.");
1768 assert(Lower != Upper && "Unexpected empty set.");
1769 unsigned BitWidth = Lower.getBitWidth();
1770 if (Lower + 1 == Upper)
1771 return ConstantRange(APInt(BitWidth, Lower.countr_zero()));
1772 if (Lower.isZero())
1774 APInt(BitWidth, BitWidth + 1));
1775
1776 // Calculate longest common prefix.
1777 unsigned LCPLength = (Lower ^ (Upper - 1)).countl_zero();
1778 // If Lower is {LCP, 000...}, the maximum is Lower.countr_zero().
1779 // Otherwise, the maximum is BitWidth - LCPLength - 1 ({LCP, 100...}).
1780 return ConstantRange(
1783 std::max(BitWidth - LCPLength - 1, Lower.countr_zero()) + 1));
1784}
1785
1786ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const {
1787 if (isEmptySet())
1788 return getEmpty();
1789
1790 unsigned BitWidth = getBitWidth();
1792 if (ZeroIsPoison && contains(Zero)) {
1793 // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1794 // which a zero can appear:
1795 // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1796 // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1797 // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1798
1799 if (Lower.isZero()) {
1800 if (Upper == 1) {
1801 // We have in input interval of kind [0, 1). In this case we cannot
1802 // really help but return empty-set.
1803 return getEmpty();
1804 }
1805
1806 // Compute the resulting range by excluding zero from Lower.
1808 } else if (Upper == 1) {
1809 // Compute the resulting range by excluding zero from Upper.
1810 return getUnsignedCountTrailingZerosRange(Lower, Zero);
1811 } else {
1813 ConstantRange CR2 =
1815 return CR1.unionWith(CR2);
1816 }
1817 }
1818
1819 if (isFullSet())
1820 return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1821 if (!isWrappedSet())
1822 return getUnsignedCountTrailingZerosRange(Lower, Upper);
1823 // The range is wrapped. We decompose it into two ranges, [0, Upper) and
1824 // [Lower, 0).
1825 // Handle [Lower, 0)
1827 // Handle [0, Upper)
1829 return CR1.unionWith(CR2);
1830}
1831
1833 const APInt &Upper) {
1834 assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
1835 "Unexpected wrapped set.");
1836 assert(Lower != Upper && "Unexpected empty set.");
1837 unsigned BitWidth = Lower.getBitWidth();
1838 if (Lower + 1 == Upper)
1839 return ConstantRange(APInt(BitWidth, Lower.popcount()));
1840
1841 APInt Max = Upper - 1;
1842 // Calculate longest common prefix.
1843 unsigned LCPLength = (Lower ^ Max).countl_zero();
1844 unsigned LCPPopCount = Lower.getHiBits(LCPLength).popcount();
1845 // If Lower is {LCP, 000...}, the minimum is the popcount of LCP.
1846 // Otherwise, the minimum is the popcount of LCP + 1.
1847 unsigned MinBits =
1848 LCPPopCount + (Lower.countr_zero() < BitWidth - LCPLength ? 1 : 0);
1849 // If Max is {LCP, 111...}, the maximum is the popcount of LCP + (BitWidth -
1850 // length of LCP).
1851 // Otherwise, the minimum is the popcount of LCP + (BitWidth -
1852 // length of LCP - 1).
1853 unsigned MaxBits = LCPPopCount + (BitWidth - LCPLength) -
1854 (Max.countr_one() < BitWidth - LCPLength ? 1 : 0);
1855 return ConstantRange(APInt(BitWidth, MinBits), APInt(BitWidth, MaxBits + 1));
1856}
1857
1859 if (isEmptySet())
1860 return getEmpty();
1861
1862 unsigned BitWidth = getBitWidth();
1864 if (isFullSet())
1865 return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1866 if (!isWrappedSet())
1867 return getUnsignedPopCountRange(Lower, Upper);
1868 // The range is wrapped. We decompose it into two ranges, [0, Upper) and
1869 // [Lower, 0).
1870 // Handle [Lower, 0) == [Lower, Max]
1872 APInt(BitWidth, BitWidth + 1));
1873 // Handle [0, Upper)
1874 ConstantRange CR2 = getUnsignedPopCountRange(Zero, Upper);
1875 return CR1.unionWith(CR2);
1876}
1877
1879 const ConstantRange &Other) const {
1880 if (isEmptySet() || Other.isEmptySet())
1882
1883 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1884 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1885
1886 // a u+ b overflows high iff a u> ~b.
1887 if (Min.ugt(~OtherMin))
1889 if (Max.ugt(~OtherMax))
1892}
1893
1895 const ConstantRange &Other) const {
1896 if (isEmptySet() || Other.isEmptySet())
1898
1899 APInt Min = getSignedMin(), Max = getSignedMax();
1900 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1901
1904
1905 // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b.
1906 // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b.
1907 if (Min.isNonNegative() && OtherMin.isNonNegative() &&
1908 Min.sgt(SignedMax - OtherMin))
1910 if (Max.isNegative() && OtherMax.isNegative() &&
1911 Max.slt(SignedMin - OtherMax))
1913
1914 if (Max.isNonNegative() && OtherMax.isNonNegative() &&
1915 Max.sgt(SignedMax - OtherMax))
1917 if (Min.isNegative() && OtherMin.isNegative() &&
1918 Min.slt(SignedMin - OtherMin))
1920
1922}
1923
1925 const ConstantRange &Other) const {
1926 if (isEmptySet() || Other.isEmptySet())
1928
1929 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1930 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1931
1932 // a u- b overflows low iff a u< b.
1933 if (Max.ult(OtherMin))
1935 if (Min.ult(OtherMax))
1938}
1939
1941 const ConstantRange &Other) const {
1942 if (isEmptySet() || Other.isEmptySet())
1944
1945 APInt Min = getSignedMin(), Max = getSignedMax();
1946 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1947
1950
1951 // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b.
1952 // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b.
1953 if (Min.isNonNegative() && OtherMax.isNegative() &&
1954 Min.sgt(SignedMax + OtherMax))
1956 if (Max.isNegative() && OtherMin.isNonNegative() &&
1957 Max.slt(SignedMin + OtherMin))
1959
1960 if (Max.isNonNegative() && OtherMin.isNegative() &&
1961 Max.sgt(SignedMax + OtherMin))
1963 if (Min.isNegative() && OtherMax.isNonNegative() &&
1964 Min.slt(SignedMin + OtherMax))
1966
1968}
1969
1971 const ConstantRange &Other) const {
1972 if (isEmptySet() || Other.isEmptySet())
1974
1975 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1976 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1977 bool Overflow;
1978
1979 (void) Min.umul_ov(OtherMin, Overflow);
1980 if (Overflow)
1982
1983 (void) Max.umul_ov(OtherMax, Overflow);
1984 if (Overflow)
1986
1988}
1989
1991 if (isFullSet())
1992 OS << "full-set";
1993 else if (isEmptySet())
1994 OS << "empty-set";
1995 else
1996 OS << "[" << Lower << "," << Upper << ")";
1997}
1998
1999#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2001 print(dbgs());
2002}
2003#endif
2004
2006 const unsigned NumRanges = Ranges.getNumOperands() / 2;
2007 assert(NumRanges >= 1 && "Must have at least one range!");
2008 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
2009
2010 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
2011 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
2012
2013 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
2014
2015 for (unsigned i = 1; i < NumRanges; ++i) {
2016 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
2017 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
2018
2019 // Note: unionWith will potentially create a range that contains values not
2020 // contained in any of the original N ranges.
2021 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
2022 }
2023
2024 return CR;
2025}
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:529
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:76
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:212
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1385
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:401
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1463
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:184
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:1179
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:395
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:194
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
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:197
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:334
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1565
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1395
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1297
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:449
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:312
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1128
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:284
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1108
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:274
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
unsigned countr_one() const
Count the number of trailing one bits.
Definition: APInt.h:1606
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1402
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:377
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:993
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_NE
not equal
Definition: InstrTypes.h:1015
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:1129
Predicate getFlippedSignednessPredicate()
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert.
Definition: InstrTypes.h:1308
bool isIntPredicate() const
Definition: InstrTypes.h:1123
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition: InstrTypes.h:1261
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.
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.
bool isBinaryOp() const
Definition: Instruction.h:257
Metadata node.
Definition: Metadata.h:1067
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:76
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:2178
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition: APInt.h:2183
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition: APInt.h:2188
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition: APInt.h:2193
@ 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:456
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:297
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:104
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:141
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:125
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:101