xref: /freebsd/contrib/llvm-project/llvm/lib/IR/ConstantRange.cpp (revision a90b9d0159070121c221b966469c3e36d912bf82)
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"
25 #include "llvm/IR/ConstantRange.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/Metadata.h"
31 #include "llvm/IR/Operator.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/KnownBits.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <algorithm>
38 #include <cassert>
39 #include <cstdint>
40 #include <optional>
41 
42 using namespace llvm;
43 
44 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full)
45     : Lower(Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth)),
46       Upper(Lower) {}
47 
48 ConstantRange::ConstantRange(APInt V)
49     : Lower(std::move(V)), Upper(Lower + 1) {}
50 
51 ConstantRange::ConstantRange(APInt L, APInt U)
52     : Lower(std::move(L)), Upper(std::move(U)) {
53   assert(Lower.getBitWidth() == Upper.getBitWidth() &&
54          "ConstantRange with unequal bit widths");
55   assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
56          "Lower == Upper, but they aren't min or max value!");
57 }
58 
59 ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
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 
79 KnownBits ConstantRange::toKnownBits() const {
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();
88   KnownBits Known = KnownBits::makeConstant(Min);
89   if (std::optional<unsigned> DifferentBit =
90           APIntOps::GetMostSignificantDifferentBit(Min, Max)) {
91     Known.Zero.clearLowBits(*DifferentBit + 1);
92     Known.One.clearLowBits(*DifferentBit + 1);
93   }
94   return Known;
95 }
96 
97 ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
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: {
113     APInt UMax(CR.getUnsignedMax());
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   }
124   case CmpInst::ICMP_ULE:
125     return getNonEmpty(APInt::getMinValue(W), CR.getUnsignedMax() + 1);
126   case CmpInst::ICMP_SLE:
127     return getNonEmpty(APInt::getSignedMinValue(W), CR.getSignedMax() + 1);
128   case CmpInst::ICMP_UGT: {
129     APInt UMin(CR.getUnsignedMin());
130     if (UMin.isMaxValue())
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   }
140   case CmpInst::ICMP_UGE:
141     return getNonEmpty(CR.getUnsignedMin(), APInt::getZero(W));
142   case CmpInst::ICMP_SGE:
143     return getNonEmpty(CR.getSignedMin(), APInt::getSignedMinValue(W));
144   }
145 }
146 
147 ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
148                                                       const ConstantRange &CR) {
149   // Follows from De-Morgan's laws:
150   //
151   // ~(~A union ~B) == A intersect B.
152   //
153   return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
154       .inverse();
155 }
156 
157 ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
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   //
165   assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C));
166   return makeAllowedICmpRegion(Pred, C);
167 }
168 
169 bool ConstantRange::areInsensitiveToSignednessOfICmpPredicate(
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 
178 bool ConstantRange::areInsensitiveToSignednessOfInvertedICmpPredicate(
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 
187 CmpInst::Predicate ConstantRange::getEquivalentPredWithFlippedSignedness(
188     CmpInst::Predicate Pred, const ConstantRange &CR1,
189     const ConstantRange &CR2) {
190   assert(CmpInst::isIntPredicate(Pred) && CmpInst::isRelational(Pred) &&
191          "Only for relational integer predicates!");
192 
193   CmpInst::Predicate FlippedSignednessPred =
194       CmpInst::getFlippedSignednessPredicate(Pred);
195 
196   if (areInsensitiveToSignednessOfICmpPredicate(CR1, CR2))
197     return FlippedSignednessPred;
198 
199   if (areInsensitiveToSignednessOfInvertedICmpPredicate(CR1, CR2))
200     return CmpInst::getInversePredicate(FlippedSignednessPred);
201 
202   return CmpInst::Predicate::BAD_ICMP_PREDICATE;
203 }
204 
205 void ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
206                                       APInt &RHS, APInt &Offset) const {
207   Offset = APInt(getBitWidth(), 0);
208   if (isFullSet() || isEmptySet()) {
209     Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
210     RHS = APInt(getBitWidth(), 0);
211   } else if (auto *OnlyElt = getSingleElement()) {
212     Pred = CmpInst::ICMP_EQ;
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 =
219         getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
220     RHS = getUpper();
221   } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
222     Pred =
223         getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE;
224     RHS = getLower();
225   } else {
226     Pred = CmpInst::ICMP_ULT;
227     RHS = getUpper() - getLower();
228     Offset = -getLower();
229   }
230 
231   assert(ConstantRange::makeExactICmpRegion(Pred, RHS) == add(Offset) &&
232          "Bad result!");
233 }
234 
235 bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
236                                       APInt &RHS) const {
237   APInt Offset;
238   getEquivalentICmp(Pred, RHS, Offset);
239   return Offset.isZero();
240 }
241 
242 bool ConstantRange::icmp(CmpInst::Predicate Pred,
243                          const ConstantRange &Other) const {
244   return makeSatisfyingICmpRegion(Pred, Other).contains(*this);
245 }
246 
247 /// Exact mul nuw region for single element RHS.
248 static ConstantRange makeExactMulNUWRegion(const APInt &V) {
249   unsigned BitWidth = V.getBitWidth();
250   if (V == 0)
251     return ConstantRange::getFull(V.getBitWidth());
252 
253   return ConstantRange::getNonEmpty(
254       APIntOps::RoundingUDiv(APInt::getMinValue(BitWidth), V,
255                              APInt::Rounding::UP),
256       APIntOps::RoundingUDiv(APInt::getMaxValue(BitWidth), V,
257                              APInt::Rounding::DOWN) + 1);
258 }
259 
260 /// Exact mul nsw region for single element RHS.
261 static ConstantRange makeExactMulNSWRegion(const APInt &V) {
262   // Handle 0 and -1 separately to avoid division by zero or overflow.
263   unsigned BitWidth = V.getBitWidth();
264   if (V == 0)
265     return ConstantRange::getFull(BitWidth);
266 
267   APInt MinValue = APInt::getSignedMinValue(BitWidth);
268   APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
269   // e.g. Returning [-127, 127], represented as [-127, -128).
270   if (V.isAllOnes())
271     return ConstantRange(-MaxValue, MinValue);
272 
273   APInt Lower, Upper;
274   if (V.isNegative()) {
275     Lower = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::UP);
276     Upper = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::DOWN);
277   } else {
278     Lower = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::UP);
279     Upper = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::DOWN);
280   }
281   return ConstantRange::getNonEmpty(Lower, Upper + 1);
282 }
283 
284 ConstantRange
285 ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
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 
307     APInt SignedMinVal = APInt::getSignedMinValue(BitWidth);
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 
318     APInt SignedMinVal = APInt::getSignedMinValue(BitWidth);
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(
340         ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, (BitWidth - 1) + 1)));
341     if (ShAmt.isEmptySet()) {
342       // If the entire range of shift amounts is already poison-producing,
343       // then we can freely add more poison-producing flags ontop of that.
344       return getFull(BitWidth);
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)
351       return getNonEmpty(APInt::getZero(BitWidth),
352                          APInt::getMaxValue(BitWidth).lshr(ShAmtUMax) + 1);
353     return getNonEmpty(APInt::getSignedMinValue(BitWidth).ashr(ShAmtUMax),
354                        APInt::getSignedMaxValue(BitWidth).ashr(ShAmtUMax) + 1);
355   }
356   }
357 }
358 
359 ConstantRange ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
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 
367 bool ConstantRange::isFullSet() const {
368   return Lower == Upper && Lower.isMaxValue();
369 }
370 
371 bool ConstantRange::isEmptySet() const {
372   return Lower == Upper && Lower.isMinValue();
373 }
374 
375 bool ConstantRange::isWrappedSet() const {
376   return Lower.ugt(Upper) && !Upper.isZero();
377 }
378 
379 bool ConstantRange::isUpperWrapped() const {
380   return Lower.ugt(Upper);
381 }
382 
383 bool ConstantRange::isSignWrappedSet() const {
384   return Lower.sgt(Upper) && !Upper.isMinSignedValue();
385 }
386 
387 bool ConstantRange::isUpperSignWrapped() const {
388   return Lower.sgt(Upper);
389 }
390 
391 bool
392 ConstantRange::isSizeStrictlySmallerThan(const ConstantRange &Other) const {
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 
401 bool
402 ConstantRange::isSizeLargerThan(uint64_t MaxSize) const {
403   // If this a full set, we need special handling to avoid needing an extra bit
404   // to represent the size.
405   if (isFullSet())
406     return MaxSize == 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
407 
408   return (Upper - Lower).ugt(MaxSize);
409 }
410 
411 bool ConstantRange::isAllNegative() const {
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 
421 bool ConstantRange::isAllNonNegative() const {
422   // Empty and full set are automatically treated correctly.
423   return !isSignWrappedSet() && Lower.isNonNegative();
424 }
425 
426 APInt ConstantRange::getUnsignedMax() const {
427   if (isFullSet() || isUpperWrapped())
428     return APInt::getMaxValue(getBitWidth());
429   return getUpper() - 1;
430 }
431 
432 APInt ConstantRange::getUnsignedMin() const {
433   if (isFullSet() || isWrappedSet())
434     return APInt::getMinValue(getBitWidth());
435   return getLower();
436 }
437 
438 APInt ConstantRange::getSignedMax() const {
439   if (isFullSet() || isUpperSignWrapped())
440     return APInt::getSignedMaxValue(getBitWidth());
441   return getUpper() - 1;
442 }
443 
444 APInt ConstantRange::getSignedMin() const {
445   if (isFullSet() || isSignWrappedSet())
446     return APInt::getSignedMinValue(getBitWidth());
447   return getLower();
448 }
449 
450 bool 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 
459 bool ConstantRange::contains(const ConstantRange &Other) const {
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 
477 unsigned ConstantRange::getActiveBits() const {
478   if (isEmptySet())
479     return 0;
480 
481   return getUnsignedMax().getActiveBits();
482 }
483 
484 unsigned ConstantRange::getMinSignedBits() const {
485   if (isEmptySet())
486     return 0;
487 
488   return std::max(getSignedMin().getSignificantBits(),
489                   getSignedMax().getSignificantBits());
490 }
491 
492 ConstantRange ConstantRange::subtract(const APInt &Val) const {
493   assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
494   // If the set is empty or full, don't modify the endpoints.
495   if (Lower == Upper)
496     return *this;
497   return ConstantRange(Lower - Val, Upper - Val);
498 }
499 
500 ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
501   return intersectWith(CR.inverse());
502 }
503 
504 static ConstantRange getPreferredRange(
505     const ConstantRange &CR1, const ConstantRange &CR2,
506     ConstantRange::PreferredRangeType Type) {
507   if (Type == ConstantRange::Unsigned) {
508     if (!CR1.isWrappedSet() && CR2.isWrappedSet())
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 
524 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
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 
630 ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
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 
703 std::optional<ConstantRange>
704 ConstantRange::exactIntersectWith(const ConstantRange &CR) const {
705   // TODO: This can be implemented more efficiently.
706   ConstantRange Result = intersectWith(CR);
707   if (Result == inverse().unionWith(CR.inverse()).inverse())
708     return Result;
709   return std::nullopt;
710 }
711 
712 std::optional<ConstantRange>
713 ConstantRange::exactUnionWith(const ConstantRange &CR) const {
714   // TODO: This can be implemented more efficiently.
715   ConstantRange Result = unionWith(CR);
716   if (Result == inverse().intersectWith(CR.inverse()).inverse())
717     return Result;
718   return std::nullopt;
719 }
720 
721 ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
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();
754     APInt SMin = APInt::getSignedMinValue(BW);
755     APInt SMax = APInt::getSignedMaxValue(BW);
756     if (ResultBitWidth > BW) {
757       SMin = SMin.sext(ResultBitWidth);
758       SMax = SMax.sext(ResultBitWidth);
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 
772 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
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 
789 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
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 
807 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
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 
860 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
861   unsigned SrcTySize = getBitWidth();
862   if (SrcTySize > DstTySize)
863     return truncate(DstTySize);
864   if (SrcTySize < DstTySize)
865     return zeroExtend(DstTySize);
866   return *this;
867 }
868 
869 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
870   unsigned SrcTySize = getBitWidth();
871   if (SrcTySize > DstTySize)
872     return truncate(DstTySize);
873   if (SrcTySize < DstTySize)
874     return signExtend(DstTySize);
875   return *this;
876 }
877 
878 ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
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 
923 ConstantRange ConstantRange::overflowingBinaryOp(Instruction::BinaryOps BinOp,
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 
940 bool ConstantRange::isIntrinsicSupported(Intrinsic::ID IntrinsicID) {
941   switch (IntrinsicID) {
942   case Intrinsic::uadd_sat:
943   case Intrinsic::usub_sat:
944   case Intrinsic::sadd_sat:
945   case Intrinsic::ssub_sat:
946   case Intrinsic::umin:
947   case Intrinsic::umax:
948   case Intrinsic::smin:
949   case Intrinsic::smax:
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 
960 ConstantRange ConstantRange::intrinsic(Intrinsic::ID IntrinsicID,
961                                        ArrayRef<ConstantRange> Ops) {
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 
1005 ConstantRange
1006 ConstantRange::add(const ConstantRange &Other) const {
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 
1025 ConstantRange ConstantRange::addWithNoWrap(const ConstantRange &Other,
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 
1052 ConstantRange
1053 ConstantRange::sub(const ConstantRange &Other) const {
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 
1072 ConstantRange ConstantRange::subWithNoWrap(const ConstantRange &Other,
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 
1102 ConstantRange
1103 ConstantRange::multiply(const ConstantRange &Other) const {
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())
1116       return ConstantRange(APInt::getZero(getBitWidth())).sub(Other);
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() &&
1147       (UR.getUpper().isNonNegative() || UR.getUpper().isMinSignedValue()))
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 
1170 ConstantRange ConstantRange::smul_fast(const ConstantRange &Other) const {
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 
1189 ConstantRange
1190 ConstantRange::smax(const ConstantRange &Other) const {
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 
1203 ConstantRange
1204 ConstantRange::umax(const ConstantRange &Other) const {
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())
1213     return Res.intersectWith(unionWith(Other, Unsigned), Unsigned);
1214   return Res;
1215 }
1216 
1217 ConstantRange
1218 ConstantRange::smin(const ConstantRange &Other) const {
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 
1231 ConstantRange
1232 ConstantRange::umin(const ConstantRange &Other) const {
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())
1241     return Res.intersectWith(unionWith(Other, Unsigned), Unsigned);
1242   return Res;
1243 }
1244 
1245 ConstantRange
1246 ConstantRange::udiv(const ConstantRange &RHS) const {
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 
1266 ConstantRange ConstantRange::sdiv(const ConstantRange &RHS) const {
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.
1270   APInt Zero = APInt::getZero(getBitWidth());
1271   APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
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.
1346   ConstantRange Res = NegRes.unionWith(PosRes, PreferredRangeType::Signed);
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 
1354 ConstantRange ConstantRange::urem(const ConstantRange &RHS) const {
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 
1376 ConstantRange ConstantRange::srem(const ConstantRange &RHS) const {
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 
1427 ConstantRange ConstantRange::binaryNot() const {
1428   return ConstantRange(APInt::getAllOnes(getBitWidth())).sub(*this);
1429 }
1430 
1431 ConstantRange ConstantRange::binaryAnd(const ConstantRange &Other) const {
1432   if (isEmptySet() || Other.isEmptySet())
1433     return getEmpty();
1434 
1435   ConstantRange KnownBitsRange =
1436       fromKnownBits(toKnownBits() & Other.toKnownBits(), false);
1437   ConstantRange UMinUMaxRange =
1438       getNonEmpty(APInt::getZero(getBitWidth()),
1439                   APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()) + 1);
1440   return KnownBitsRange.intersectWith(UMinUMaxRange);
1441 }
1442 
1443 ConstantRange ConstantRange::binaryOr(const ConstantRange &Other) const {
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()),
1452                   APInt::getZero(getBitWidth()));
1453   return KnownBitsRange.intersectWith(UMaxUMinRange);
1454 }
1455 
1456 ConstantRange ConstantRange::binaryXor(const ConstantRange &Other) const {
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   return fromKnownBits(toKnownBits() ^ Other.toKnownBits(), /*IsSigned*/false);
1471 }
1472 
1473 ConstantRange
1474 ConstantRange::shl(const ConstantRange &Other) const {
1475   if (isEmptySet() || Other.isEmptySet())
1476     return getEmpty();
1477 
1478   APInt Min = getUnsignedMin();
1479   APInt Max = getUnsignedMax();
1480   if (const APInt *RHS = Other.getSingleElement()) {
1481     unsigned BW = getBitWidth();
1482     if (RHS->uge(BW))
1483       return getEmpty();
1484 
1485     unsigned EqualLeadingBits = (Min ^ Max).countl_zero();
1486     if (RHS->ule(EqualLeadingBits))
1487       return getNonEmpty(Min << *RHS, (Max << *RHS) + 1);
1488 
1489     return getNonEmpty(APInt::getZero(BW),
1490                        APInt::getBitsSetFrom(BW, RHS->getZExtValue()) + 1);
1491   }
1492 
1493   APInt OtherMax = Other.getUnsignedMax();
1494   if (isAllNegative() && OtherMax.ule(Min.countl_one())) {
1495     // For negative numbers, if the shift does not overflow in a signed sense,
1496     // a larger shift will make the number smaller.
1497     Max <<= Other.getUnsignedMin();
1498     Min <<= OtherMax;
1499     return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1500   }
1501 
1502   // There's overflow!
1503   if (OtherMax.ugt(Max.countl_zero()))
1504     return getFull();
1505 
1506   // FIXME: implement the other tricky cases
1507 
1508   Min <<= Other.getUnsignedMin();
1509   Max <<= OtherMax;
1510 
1511   return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1512 }
1513 
1514 ConstantRange
1515 ConstantRange::lshr(const ConstantRange &Other) const {
1516   if (isEmptySet() || Other.isEmptySet())
1517     return getEmpty();
1518 
1519   APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1;
1520   APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
1521   return getNonEmpty(std::move(min), std::move(max));
1522 }
1523 
1524 ConstantRange
1525 ConstantRange::ashr(const ConstantRange &Other) const {
1526   if (isEmptySet() || Other.isEmptySet())
1527     return getEmpty();
1528 
1529   // May straddle zero, so handle both positive and negative cases.
1530   // 'PosMax' is the upper bound of the result of the ashr
1531   // operation, when Upper of the LHS of ashr is a non-negative.
1532   // number. Since ashr of a non-negative number will result in a
1533   // smaller number, the Upper value of LHS is shifted right with
1534   // the minimum value of 'Other' instead of the maximum value.
1535   APInt PosMax = getSignedMax().ashr(Other.getUnsignedMin()) + 1;
1536 
1537   // 'PosMin' is the lower bound of the result of the ashr
1538   // operation, when Lower of the LHS is a non-negative number.
1539   // Since ashr of a non-negative number will result in a smaller
1540   // number, the Lower value of LHS is shifted right with the
1541   // maximum value of 'Other'.
1542   APInt PosMin = getSignedMin().ashr(Other.getUnsignedMax());
1543 
1544   // 'NegMax' is the upper bound of the result of the ashr
1545   // operation, when Upper of the LHS of ashr is a negative number.
1546   // Since 'ashr' of a negative number will result in a bigger
1547   // number, the Upper value of LHS is shifted right with the
1548   // maximum value of 'Other'.
1549   APInt NegMax = getSignedMax().ashr(Other.getUnsignedMax()) + 1;
1550 
1551   // 'NegMin' is the lower bound of the result of the ashr
1552   // operation, when Lower of the LHS of ashr is a negative number.
1553   // Since 'ashr' of a negative number will result in a bigger
1554   // number, the Lower value of LHS is shifted right with the
1555   // minimum value of 'Other'.
1556   APInt NegMin = getSignedMin().ashr(Other.getUnsignedMin());
1557 
1558   APInt max, min;
1559   if (getSignedMin().isNonNegative()) {
1560     // Upper and Lower of LHS are non-negative.
1561     min = PosMin;
1562     max = PosMax;
1563   } else if (getSignedMax().isNegative()) {
1564     // Upper and Lower of LHS are negative.
1565     min = NegMin;
1566     max = NegMax;
1567   } else {
1568     // Upper is non-negative and Lower is negative.
1569     min = NegMin;
1570     max = PosMax;
1571   }
1572   return getNonEmpty(std::move(min), std::move(max));
1573 }
1574 
1575 ConstantRange ConstantRange::uadd_sat(const ConstantRange &Other) const {
1576   if (isEmptySet() || Other.isEmptySet())
1577     return getEmpty();
1578 
1579   APInt NewL = getUnsignedMin().uadd_sat(Other.getUnsignedMin());
1580   APInt NewU = getUnsignedMax().uadd_sat(Other.getUnsignedMax()) + 1;
1581   return getNonEmpty(std::move(NewL), std::move(NewU));
1582 }
1583 
1584 ConstantRange ConstantRange::sadd_sat(const ConstantRange &Other) const {
1585   if (isEmptySet() || Other.isEmptySet())
1586     return getEmpty();
1587 
1588   APInt NewL = getSignedMin().sadd_sat(Other.getSignedMin());
1589   APInt NewU = getSignedMax().sadd_sat(Other.getSignedMax()) + 1;
1590   return getNonEmpty(std::move(NewL), std::move(NewU));
1591 }
1592 
1593 ConstantRange ConstantRange::usub_sat(const ConstantRange &Other) const {
1594   if (isEmptySet() || Other.isEmptySet())
1595     return getEmpty();
1596 
1597   APInt NewL = getUnsignedMin().usub_sat(Other.getUnsignedMax());
1598   APInt NewU = getUnsignedMax().usub_sat(Other.getUnsignedMin()) + 1;
1599   return getNonEmpty(std::move(NewL), std::move(NewU));
1600 }
1601 
1602 ConstantRange ConstantRange::ssub_sat(const ConstantRange &Other) const {
1603   if (isEmptySet() || Other.isEmptySet())
1604     return getEmpty();
1605 
1606   APInt NewL = getSignedMin().ssub_sat(Other.getSignedMax());
1607   APInt NewU = getSignedMax().ssub_sat(Other.getSignedMin()) + 1;
1608   return getNonEmpty(std::move(NewL), std::move(NewU));
1609 }
1610 
1611 ConstantRange ConstantRange::umul_sat(const ConstantRange &Other) const {
1612   if (isEmptySet() || Other.isEmptySet())
1613     return getEmpty();
1614 
1615   APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
1616   APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
1617   return getNonEmpty(std::move(NewL), std::move(NewU));
1618 }
1619 
1620 ConstantRange ConstantRange::smul_sat(const ConstantRange &Other) const {
1621   if (isEmptySet() || Other.isEmptySet())
1622     return getEmpty();
1623 
1624   // Because we could be dealing with negative numbers here, the lower bound is
1625   // the smallest of the cartesian product of the lower and upper ranges;
1626   // for example:
1627   //   [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1628   // Similarly for the upper bound, swapping min for max.
1629 
1630   APInt Min = getSignedMin();
1631   APInt Max = getSignedMax();
1632   APInt OtherMin = Other.getSignedMin();
1633   APInt OtherMax = Other.getSignedMax();
1634 
1635   auto L = {Min.smul_sat(OtherMin), Min.smul_sat(OtherMax),
1636             Max.smul_sat(OtherMin), Max.smul_sat(OtherMax)};
1637   auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1638   return getNonEmpty(std::min(L, Compare), std::max(L, Compare) + 1);
1639 }
1640 
1641 ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const {
1642   if (isEmptySet() || Other.isEmptySet())
1643     return getEmpty();
1644 
1645   APInt NewL = getUnsignedMin().ushl_sat(Other.getUnsignedMin());
1646   APInt NewU = getUnsignedMax().ushl_sat(Other.getUnsignedMax()) + 1;
1647   return getNonEmpty(std::move(NewL), std::move(NewU));
1648 }
1649 
1650 ConstantRange ConstantRange::sshl_sat(const ConstantRange &Other) const {
1651   if (isEmptySet() || Other.isEmptySet())
1652     return getEmpty();
1653 
1654   APInt Min = getSignedMin(), Max = getSignedMax();
1655   APInt ShAmtMin = Other.getUnsignedMin(), ShAmtMax = Other.getUnsignedMax();
1656   APInt NewL = Min.sshl_sat(Min.isNonNegative() ? ShAmtMin : ShAmtMax);
1657   APInt NewU = Max.sshl_sat(Max.isNegative() ? ShAmtMin : ShAmtMax) + 1;
1658   return getNonEmpty(std::move(NewL), std::move(NewU));
1659 }
1660 
1661 ConstantRange ConstantRange::inverse() const {
1662   if (isFullSet())
1663     return getEmpty();
1664   if (isEmptySet())
1665     return getFull();
1666   return ConstantRange(Upper, Lower);
1667 }
1668 
1669 ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
1670   if (isEmptySet())
1671     return getEmpty();
1672 
1673   if (isSignWrappedSet()) {
1674     APInt Lo;
1675     // Check whether the range crosses zero.
1676     if (Upper.isStrictlyPositive() || !Lower.isStrictlyPositive())
1677       Lo = APInt::getZero(getBitWidth());
1678     else
1679       Lo = APIntOps::umin(Lower, -Upper + 1);
1680 
1681     // If SignedMin is not poison, then it is included in the result range.
1682     if (IntMinIsPoison)
1683       return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()));
1684     else
1685       return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()) + 1);
1686   }
1687 
1688   APInt SMin = getSignedMin(), SMax = getSignedMax();
1689 
1690   // Skip SignedMin if it is poison.
1691   if (IntMinIsPoison && SMin.isMinSignedValue()) {
1692     // The range may become empty if it *only* contains SignedMin.
1693     if (SMax.isMinSignedValue())
1694       return getEmpty();
1695     ++SMin;
1696   }
1697 
1698   // All non-negative.
1699   if (SMin.isNonNegative())
1700     return ConstantRange(SMin, SMax + 1);
1701 
1702   // All negative.
1703   if (SMax.isNegative())
1704     return ConstantRange(-SMax, -SMin + 1);
1705 
1706   // Range crosses zero.
1707   return ConstantRange::getNonEmpty(APInt::getZero(getBitWidth()),
1708                                     APIntOps::umax(-SMin, SMax) + 1);
1709 }
1710 
1711 ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const {
1712   if (isEmptySet())
1713     return getEmpty();
1714 
1715   APInt Zero = APInt::getZero(getBitWidth());
1716   if (ZeroIsPoison && contains(Zero)) {
1717     // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1718     // which a zero can appear:
1719     // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1720     // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1721     // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1722 
1723     if (getLower().isZero()) {
1724       if ((getUpper() - 1).isZero()) {
1725         // We have in input interval of kind [0, 1). In this case we cannot
1726         // really help but return empty-set.
1727         return getEmpty();
1728       }
1729 
1730       // Compute the resulting range by excluding zero from Lower.
1731       return ConstantRange(
1732           APInt(getBitWidth(), (getUpper() - 1).countl_zero()),
1733           APInt(getBitWidth(), (getLower() + 1).countl_zero() + 1));
1734     } else if ((getUpper() - 1).isZero()) {
1735       // Compute the resulting range by excluding zero from Upper.
1736       return ConstantRange(Zero,
1737                            APInt(getBitWidth(), getLower().countl_zero() + 1));
1738     } else {
1739       return ConstantRange(Zero, APInt(getBitWidth(), getBitWidth()));
1740     }
1741   }
1742 
1743   // Zero is either safe or not in the range. The output range is composed by
1744   // the result of countLeadingZero of the two extremes.
1745   return getNonEmpty(APInt(getBitWidth(), getUnsignedMax().countl_zero()),
1746                      APInt(getBitWidth(), getUnsignedMin().countl_zero() + 1));
1747 }
1748 
1749 static ConstantRange getUnsignedCountTrailingZerosRange(const APInt &Lower,
1750                                                         const APInt &Upper) {
1751   assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
1752          "Unexpected wrapped set.");
1753   assert(Lower != Upper && "Unexpected empty set.");
1754   unsigned BitWidth = Lower.getBitWidth();
1755   if (Lower + 1 == Upper)
1756     return ConstantRange(APInt(BitWidth, Lower.countr_zero()));
1757   if (Lower.isZero())
1758     return ConstantRange(APInt::getZero(BitWidth),
1759                          APInt(BitWidth, BitWidth + 1));
1760 
1761   // Calculate longest common prefix.
1762   unsigned LCPLength = (Lower ^ (Upper - 1)).countl_zero();
1763   // If Lower is {LCP, 000...}, the maximum is Lower.countr_zero().
1764   // Otherwise, the maximum is BitWidth - LCPLength - 1 ({LCP, 100...}).
1765   return ConstantRange(
1766       APInt::getZero(BitWidth),
1767       APInt(BitWidth,
1768             std::max(BitWidth - LCPLength - 1, Lower.countr_zero()) + 1));
1769 }
1770 
1771 ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const {
1772   if (isEmptySet())
1773     return getEmpty();
1774 
1775   unsigned BitWidth = getBitWidth();
1776   APInt Zero = APInt::getZero(BitWidth);
1777   if (ZeroIsPoison && contains(Zero)) {
1778     // ZeroIsPoison is set, and zero is contained. We discern three cases, in
1779     // which a zero can appear:
1780     // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc.
1781     // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc.
1782     // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc.
1783 
1784     if (Lower.isZero()) {
1785       if (Upper == 1) {
1786         // We have in input interval of kind [0, 1). In this case we cannot
1787         // really help but return empty-set.
1788         return getEmpty();
1789       }
1790 
1791       // Compute the resulting range by excluding zero from Lower.
1792       return getUnsignedCountTrailingZerosRange(APInt(BitWidth, 1), Upper);
1793     } else if (Upper == 1) {
1794       // Compute the resulting range by excluding zero from Upper.
1795       return getUnsignedCountTrailingZerosRange(Lower, Zero);
1796     } else {
1797       ConstantRange CR1 = getUnsignedCountTrailingZerosRange(Lower, Zero);
1798       ConstantRange CR2 =
1799           getUnsignedCountTrailingZerosRange(APInt(BitWidth, 1), Upper);
1800       return CR1.unionWith(CR2);
1801     }
1802   }
1803 
1804   if (isFullSet())
1805     return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1806   if (!isWrappedSet())
1807     return getUnsignedCountTrailingZerosRange(Lower, Upper);
1808   // The range is wrapped. We decompose it into two ranges, [0, Upper) and
1809   // [Lower, 0).
1810   // Handle [Lower, 0)
1811   ConstantRange CR1 = getUnsignedCountTrailingZerosRange(Lower, Zero);
1812   // Handle [0, Upper)
1813   ConstantRange CR2 = getUnsignedCountTrailingZerosRange(Zero, Upper);
1814   return CR1.unionWith(CR2);
1815 }
1816 
1817 static ConstantRange getUnsignedPopCountRange(const APInt &Lower,
1818                                               const APInt &Upper) {
1819   assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
1820          "Unexpected wrapped set.");
1821   assert(Lower != Upper && "Unexpected empty set.");
1822   unsigned BitWidth = Lower.getBitWidth();
1823   if (Lower + 1 == Upper)
1824     return ConstantRange(APInt(BitWidth, Lower.popcount()));
1825 
1826   APInt Max = Upper - 1;
1827   // Calculate longest common prefix.
1828   unsigned LCPLength = (Lower ^ Max).countl_zero();
1829   unsigned LCPPopCount = Lower.getHiBits(LCPLength).popcount();
1830   // If Lower is {LCP, 000...}, the minimum is the popcount of LCP.
1831   // Otherwise, the minimum is the popcount of LCP + 1.
1832   unsigned MinBits =
1833       LCPPopCount + (Lower.countr_zero() < BitWidth - LCPLength ? 1 : 0);
1834   // If Max is {LCP, 111...}, the maximum is the popcount of LCP + (BitWidth -
1835   // length of LCP).
1836   // Otherwise, the minimum is the popcount of LCP + (BitWidth -
1837   // length of LCP - 1).
1838   unsigned MaxBits = LCPPopCount + (BitWidth - LCPLength) -
1839                      (Max.countr_one() < BitWidth - LCPLength ? 1 : 0);
1840   return ConstantRange(APInt(BitWidth, MinBits), APInt(BitWidth, MaxBits + 1));
1841 }
1842 
1843 ConstantRange ConstantRange::ctpop() const {
1844   if (isEmptySet())
1845     return getEmpty();
1846 
1847   unsigned BitWidth = getBitWidth();
1848   APInt Zero = APInt::getZero(BitWidth);
1849   if (isFullSet())
1850     return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1));
1851   if (!isWrappedSet())
1852     return getUnsignedPopCountRange(Lower, Upper);
1853   // The range is wrapped. We decompose it into two ranges, [0, Upper) and
1854   // [Lower, 0).
1855   // Handle [Lower, 0) == [Lower, Max]
1856   ConstantRange CR1 = ConstantRange(APInt(BitWidth, Lower.countl_one()),
1857                                     APInt(BitWidth, BitWidth + 1));
1858   // Handle [0, Upper)
1859   ConstantRange CR2 = getUnsignedPopCountRange(Zero, Upper);
1860   return CR1.unionWith(CR2);
1861 }
1862 
1863 ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow(
1864     const ConstantRange &Other) const {
1865   if (isEmptySet() || Other.isEmptySet())
1866     return OverflowResult::MayOverflow;
1867 
1868   APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1869   APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1870 
1871   // a u+ b overflows high iff a u> ~b.
1872   if (Min.ugt(~OtherMin))
1873     return OverflowResult::AlwaysOverflowsHigh;
1874   if (Max.ugt(~OtherMax))
1875     return OverflowResult::MayOverflow;
1876   return OverflowResult::NeverOverflows;
1877 }
1878 
1879 ConstantRange::OverflowResult ConstantRange::signedAddMayOverflow(
1880     const ConstantRange &Other) const {
1881   if (isEmptySet() || Other.isEmptySet())
1882     return OverflowResult::MayOverflow;
1883 
1884   APInt Min = getSignedMin(), Max = getSignedMax();
1885   APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1886 
1887   APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
1888   APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
1889 
1890   // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b.
1891   // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b.
1892   if (Min.isNonNegative() && OtherMin.isNonNegative() &&
1893       Min.sgt(SignedMax - OtherMin))
1894     return OverflowResult::AlwaysOverflowsHigh;
1895   if (Max.isNegative() && OtherMax.isNegative() &&
1896       Max.slt(SignedMin - OtherMax))
1897     return OverflowResult::AlwaysOverflowsLow;
1898 
1899   if (Max.isNonNegative() && OtherMax.isNonNegative() &&
1900       Max.sgt(SignedMax - OtherMax))
1901     return OverflowResult::MayOverflow;
1902   if (Min.isNegative() && OtherMin.isNegative() &&
1903       Min.slt(SignedMin - OtherMin))
1904     return OverflowResult::MayOverflow;
1905 
1906   return OverflowResult::NeverOverflows;
1907 }
1908 
1909 ConstantRange::OverflowResult ConstantRange::unsignedSubMayOverflow(
1910     const ConstantRange &Other) const {
1911   if (isEmptySet() || Other.isEmptySet())
1912     return OverflowResult::MayOverflow;
1913 
1914   APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1915   APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1916 
1917   // a u- b overflows low iff a u< b.
1918   if (Max.ult(OtherMin))
1919     return OverflowResult::AlwaysOverflowsLow;
1920   if (Min.ult(OtherMax))
1921     return OverflowResult::MayOverflow;
1922   return OverflowResult::NeverOverflows;
1923 }
1924 
1925 ConstantRange::OverflowResult ConstantRange::signedSubMayOverflow(
1926     const ConstantRange &Other) const {
1927   if (isEmptySet() || Other.isEmptySet())
1928     return OverflowResult::MayOverflow;
1929 
1930   APInt Min = getSignedMin(), Max = getSignedMax();
1931   APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1932 
1933   APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
1934   APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
1935 
1936   // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b.
1937   // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b.
1938   if (Min.isNonNegative() && OtherMax.isNegative() &&
1939       Min.sgt(SignedMax + OtherMax))
1940     return OverflowResult::AlwaysOverflowsHigh;
1941   if (Max.isNegative() && OtherMin.isNonNegative() &&
1942       Max.slt(SignedMin + OtherMin))
1943     return OverflowResult::AlwaysOverflowsLow;
1944 
1945   if (Max.isNonNegative() && OtherMin.isNegative() &&
1946       Max.sgt(SignedMax + OtherMin))
1947     return OverflowResult::MayOverflow;
1948   if (Min.isNegative() && OtherMax.isNonNegative() &&
1949       Min.slt(SignedMin + OtherMax))
1950     return OverflowResult::MayOverflow;
1951 
1952   return OverflowResult::NeverOverflows;
1953 }
1954 
1955 ConstantRange::OverflowResult ConstantRange::unsignedMulMayOverflow(
1956     const ConstantRange &Other) const {
1957   if (isEmptySet() || Other.isEmptySet())
1958     return OverflowResult::MayOverflow;
1959 
1960   APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1961   APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1962   bool Overflow;
1963 
1964   (void) Min.umul_ov(OtherMin, Overflow);
1965   if (Overflow)
1966     return OverflowResult::AlwaysOverflowsHigh;
1967 
1968   (void) Max.umul_ov(OtherMax, Overflow);
1969   if (Overflow)
1970     return OverflowResult::MayOverflow;
1971 
1972   return OverflowResult::NeverOverflows;
1973 }
1974 
1975 void ConstantRange::print(raw_ostream &OS) const {
1976   if (isFullSet())
1977     OS << "full-set";
1978   else if (isEmptySet())
1979     OS << "empty-set";
1980   else
1981     OS << "[" << Lower << "," << Upper << ")";
1982 }
1983 
1984 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1985 LLVM_DUMP_METHOD void ConstantRange::dump() const {
1986   print(dbgs());
1987 }
1988 #endif
1989 
1990 ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
1991   const unsigned NumRanges = Ranges.getNumOperands() / 2;
1992   assert(NumRanges >= 1 && "Must have at least one range!");
1993   assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
1994 
1995   auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
1996   auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
1997 
1998   ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
1999 
2000   for (unsigned i = 1; i < NumRanges; ++i) {
2001     auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
2002     auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
2003 
2004     // Note: unionWith will potentially create a range that contains values not
2005     // contained in any of the original N ranges.
2006     CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
2007   }
2008 
2009   return CR;
2010 }
2011