xref: /freebsd/contrib/llvm-project/llvm/lib/IR/ConstantRange.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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 
41 using namespace llvm;
42 
43 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full)
44     : Lower(Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth)),
45       Upper(Lower) {}
46 
47 ConstantRange::ConstantRange(APInt V)
48     : Lower(std::move(V)), Upper(Lower + 1) {}
49 
50 ConstantRange::ConstantRange(APInt L, APInt U)
51     : Lower(std::move(L)), Upper(std::move(U)) {
52   assert(Lower.getBitWidth() == Upper.getBitWidth() &&
53          "ConstantRange with unequal bit widths");
54   assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
55          "Lower == Upper, but they aren't min or max value!");
56 }
57 
58 ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
59                                            bool IsSigned) {
60   assert(!Known.hasConflict() && "Expected valid KnownBits");
61 
62   if (Known.isUnknown())
63     return getFull(Known.getBitWidth());
64 
65   // For unsigned ranges, or signed ranges with known sign bit, create a simple
66   // range between the smallest and largest possible value.
67   if (!IsSigned || Known.isNegative() || Known.isNonNegative())
68     return ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1);
69 
70   // If we don't know the sign bit, pick the lower bound as a negative number
71   // and the upper bound as a non-negative one.
72   APInt Lower = Known.getMinValue(), Upper = Known.getMaxValue();
73   Lower.setSignBit();
74   Upper.clearSignBit();
75   return ConstantRange(Lower, Upper + 1);
76 }
77 
78 ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
79                                                    const ConstantRange &CR) {
80   if (CR.isEmptySet())
81     return CR;
82 
83   uint32_t W = CR.getBitWidth();
84   switch (Pred) {
85   default:
86     llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
87   case CmpInst::ICMP_EQ:
88     return CR;
89   case CmpInst::ICMP_NE:
90     if (CR.isSingleElement())
91       return ConstantRange(CR.getUpper(), CR.getLower());
92     return getFull(W);
93   case CmpInst::ICMP_ULT: {
94     APInt UMax(CR.getUnsignedMax());
95     if (UMax.isMinValue())
96       return getEmpty(W);
97     return ConstantRange(APInt::getMinValue(W), std::move(UMax));
98   }
99   case CmpInst::ICMP_SLT: {
100     APInt SMax(CR.getSignedMax());
101     if (SMax.isMinSignedValue())
102       return getEmpty(W);
103     return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax));
104   }
105   case CmpInst::ICMP_ULE:
106     return getNonEmpty(APInt::getMinValue(W), CR.getUnsignedMax() + 1);
107   case CmpInst::ICMP_SLE:
108     return getNonEmpty(APInt::getSignedMinValue(W), CR.getSignedMax() + 1);
109   case CmpInst::ICMP_UGT: {
110     APInt UMin(CR.getUnsignedMin());
111     if (UMin.isMaxValue())
112       return getEmpty(W);
113     return ConstantRange(std::move(UMin) + 1, APInt::getNullValue(W));
114   }
115   case CmpInst::ICMP_SGT: {
116     APInt SMin(CR.getSignedMin());
117     if (SMin.isMaxSignedValue())
118       return getEmpty(W);
119     return ConstantRange(std::move(SMin) + 1, APInt::getSignedMinValue(W));
120   }
121   case CmpInst::ICMP_UGE:
122     return getNonEmpty(CR.getUnsignedMin(), APInt::getNullValue(W));
123   case CmpInst::ICMP_SGE:
124     return getNonEmpty(CR.getSignedMin(), APInt::getSignedMinValue(W));
125   }
126 }
127 
128 ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
129                                                       const ConstantRange &CR) {
130   // Follows from De-Morgan's laws:
131   //
132   // ~(~A union ~B) == A intersect B.
133   //
134   return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
135       .inverse();
136 }
137 
138 ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
139                                                  const APInt &C) {
140   // Computes the exact range that is equal to both the constant ranges returned
141   // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true
142   // when RHS is a singleton such as an APInt and so the assert is valid.
143   // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion
144   // returns [0,4) but makeSatisfyICmpRegion returns [0,2).
145   //
146   assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C));
147   return makeAllowedICmpRegion(Pred, C);
148 }
149 
150 bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
151                                       APInt &RHS) const {
152   bool Success = false;
153 
154   if (isFullSet() || isEmptySet()) {
155     Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
156     RHS = APInt(getBitWidth(), 0);
157     Success = true;
158   } else if (auto *OnlyElt = getSingleElement()) {
159     Pred = CmpInst::ICMP_EQ;
160     RHS = *OnlyElt;
161     Success = true;
162   } else if (auto *OnlyMissingElt = getSingleMissingElement()) {
163     Pred = CmpInst::ICMP_NE;
164     RHS = *OnlyMissingElt;
165     Success = true;
166   } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
167     Pred =
168         getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
169     RHS = getUpper();
170     Success = true;
171   } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
172     Pred =
173         getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE;
174     RHS = getLower();
175     Success = true;
176   }
177 
178   assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) &&
179          "Bad result!");
180 
181   return Success;
182 }
183 
184 /// Exact mul nuw region for single element RHS.
185 static ConstantRange makeExactMulNUWRegion(const APInt &V) {
186   unsigned BitWidth = V.getBitWidth();
187   if (V == 0)
188     return ConstantRange::getFull(V.getBitWidth());
189 
190   return ConstantRange::getNonEmpty(
191       APIntOps::RoundingUDiv(APInt::getMinValue(BitWidth), V,
192                              APInt::Rounding::UP),
193       APIntOps::RoundingUDiv(APInt::getMaxValue(BitWidth), V,
194                              APInt::Rounding::DOWN) + 1);
195 }
196 
197 /// Exact mul nsw region for single element RHS.
198 static ConstantRange makeExactMulNSWRegion(const APInt &V) {
199   // Handle special case for 0, -1 and 1. See the last for reason why we
200   // specialize -1 and 1.
201   unsigned BitWidth = V.getBitWidth();
202   if (V == 0 || V.isOneValue())
203     return ConstantRange::getFull(BitWidth);
204 
205   APInt MinValue = APInt::getSignedMinValue(BitWidth);
206   APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
207   // e.g. Returning [-127, 127], represented as [-127, -128).
208   if (V.isAllOnesValue())
209     return ConstantRange(-MaxValue, MinValue);
210 
211   APInt Lower, Upper;
212   if (V.isNegative()) {
213     Lower = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::UP);
214     Upper = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::DOWN);
215   } else {
216     Lower = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::UP);
217     Upper = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::DOWN);
218   }
219   // ConstantRange ctor take a half inclusive interval [Lower, Upper + 1).
220   // Upper + 1 is guaranteed not to overflow, because |divisor| > 1. 0, -1,
221   // and 1 are already handled as special cases.
222   return ConstantRange(Lower, Upper + 1);
223 }
224 
225 ConstantRange
226 ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
227                                           const ConstantRange &Other,
228                                           unsigned NoWrapKind) {
229   using OBO = OverflowingBinaryOperator;
230 
231   assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
232 
233   assert((NoWrapKind == OBO::NoSignedWrap ||
234           NoWrapKind == OBO::NoUnsignedWrap) &&
235          "NoWrapKind invalid!");
236 
237   bool Unsigned = NoWrapKind == OBO::NoUnsignedWrap;
238   unsigned BitWidth = Other.getBitWidth();
239 
240   switch (BinOp) {
241   default:
242     llvm_unreachable("Unsupported binary op");
243 
244   case Instruction::Add: {
245     if (Unsigned)
246       return getNonEmpty(APInt::getNullValue(BitWidth),
247                          -Other.getUnsignedMax());
248 
249     APInt SignedMinVal = APInt::getSignedMinValue(BitWidth);
250     APInt SMin = Other.getSignedMin(), SMax = Other.getSignedMax();
251     return getNonEmpty(
252         SMin.isNegative() ? SignedMinVal - SMin : SignedMinVal,
253         SMax.isStrictlyPositive() ? SignedMinVal - SMax : SignedMinVal);
254   }
255 
256   case Instruction::Sub: {
257     if (Unsigned)
258       return getNonEmpty(Other.getUnsignedMax(), APInt::getMinValue(BitWidth));
259 
260     APInt SignedMinVal = APInt::getSignedMinValue(BitWidth);
261     APInt SMin = Other.getSignedMin(), SMax = Other.getSignedMax();
262     return getNonEmpty(
263         SMax.isStrictlyPositive() ? SignedMinVal + SMax : SignedMinVal,
264         SMin.isNegative() ? SignedMinVal + SMin : SignedMinVal);
265   }
266 
267   case Instruction::Mul:
268     if (Unsigned)
269       return makeExactMulNUWRegion(Other.getUnsignedMax());
270 
271     return makeExactMulNSWRegion(Other.getSignedMin())
272         .intersectWith(makeExactMulNSWRegion(Other.getSignedMax()));
273 
274   case Instruction::Shl: {
275     // For given range of shift amounts, if we ignore all illegal shift amounts
276     // (that always produce poison), what shift amount range is left?
277     ConstantRange ShAmt = Other.intersectWith(
278         ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, (BitWidth - 1) + 1)));
279     if (ShAmt.isEmptySet()) {
280       // If the entire range of shift amounts is already poison-producing,
281       // then we can freely add more poison-producing flags ontop of that.
282       return getFull(BitWidth);
283     }
284     // There are some legal shift amounts, we can compute conservatively-correct
285     // range of no-wrap inputs. Note that by now we have clamped the ShAmtUMax
286     // to be at most bitwidth-1, which results in most conservative range.
287     APInt ShAmtUMax = ShAmt.getUnsignedMax();
288     if (Unsigned)
289       return getNonEmpty(APInt::getNullValue(BitWidth),
290                          APInt::getMaxValue(BitWidth).lshr(ShAmtUMax) + 1);
291     return getNonEmpty(APInt::getSignedMinValue(BitWidth).ashr(ShAmtUMax),
292                        APInt::getSignedMaxValue(BitWidth).ashr(ShAmtUMax) + 1);
293   }
294   }
295 }
296 
297 ConstantRange ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
298                                                    const APInt &Other,
299                                                    unsigned NoWrapKind) {
300   // makeGuaranteedNoWrapRegion() is exact for single-element ranges, as
301   // "for all" and "for any" coincide in this case.
302   return makeGuaranteedNoWrapRegion(BinOp, ConstantRange(Other), NoWrapKind);
303 }
304 
305 bool ConstantRange::isFullSet() const {
306   return Lower == Upper && Lower.isMaxValue();
307 }
308 
309 bool ConstantRange::isEmptySet() const {
310   return Lower == Upper && Lower.isMinValue();
311 }
312 
313 bool ConstantRange::isWrappedSet() const {
314   return Lower.ugt(Upper) && !Upper.isNullValue();
315 }
316 
317 bool ConstantRange::isUpperWrapped() const {
318   return Lower.ugt(Upper);
319 }
320 
321 bool ConstantRange::isSignWrappedSet() const {
322   return Lower.sgt(Upper) && !Upper.isMinSignedValue();
323 }
324 
325 bool ConstantRange::isUpperSignWrapped() const {
326   return Lower.sgt(Upper);
327 }
328 
329 bool
330 ConstantRange::isSizeStrictlySmallerThan(const ConstantRange &Other) const {
331   assert(getBitWidth() == Other.getBitWidth());
332   if (isFullSet())
333     return false;
334   if (Other.isFullSet())
335     return true;
336   return (Upper - Lower).ult(Other.Upper - Other.Lower);
337 }
338 
339 bool
340 ConstantRange::isSizeLargerThan(uint64_t MaxSize) const {
341   assert(MaxSize && "MaxSize can't be 0.");
342   // If this a full set, we need special handling to avoid needing an extra bit
343   // to represent the size.
344   if (isFullSet())
345     return APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
346 
347   return (Upper - Lower).ugt(MaxSize);
348 }
349 
350 bool ConstantRange::isAllNegative() const {
351   // Empty set is all negative, full set is not.
352   if (isEmptySet())
353     return true;
354   if (isFullSet())
355     return false;
356 
357   return !isUpperSignWrapped() && !Upper.isStrictlyPositive();
358 }
359 
360 bool ConstantRange::isAllNonNegative() const {
361   // Empty and full set are automatically treated correctly.
362   return !isSignWrappedSet() && Lower.isNonNegative();
363 }
364 
365 APInt ConstantRange::getUnsignedMax() const {
366   if (isFullSet() || isUpperWrapped())
367     return APInt::getMaxValue(getBitWidth());
368   return getUpper() - 1;
369 }
370 
371 APInt ConstantRange::getUnsignedMin() const {
372   if (isFullSet() || isWrappedSet())
373     return APInt::getMinValue(getBitWidth());
374   return getLower();
375 }
376 
377 APInt ConstantRange::getSignedMax() const {
378   if (isFullSet() || isUpperSignWrapped())
379     return APInt::getSignedMaxValue(getBitWidth());
380   return getUpper() - 1;
381 }
382 
383 APInt ConstantRange::getSignedMin() const {
384   if (isFullSet() || isSignWrappedSet())
385     return APInt::getSignedMinValue(getBitWidth());
386   return getLower();
387 }
388 
389 bool ConstantRange::contains(const APInt &V) const {
390   if (Lower == Upper)
391     return isFullSet();
392 
393   if (!isUpperWrapped())
394     return Lower.ule(V) && V.ult(Upper);
395   return Lower.ule(V) || V.ult(Upper);
396 }
397 
398 bool ConstantRange::contains(const ConstantRange &Other) const {
399   if (isFullSet() || Other.isEmptySet()) return true;
400   if (isEmptySet() || Other.isFullSet()) return false;
401 
402   if (!isUpperWrapped()) {
403     if (Other.isUpperWrapped())
404       return false;
405 
406     return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
407   }
408 
409   if (!Other.isUpperWrapped())
410     return Other.getUpper().ule(Upper) ||
411            Lower.ule(Other.getLower());
412 
413   return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
414 }
415 
416 unsigned ConstantRange::getActiveBits() const {
417   if (isEmptySet())
418     return 0;
419 
420   return getUnsignedMax().getActiveBits();
421 }
422 
423 unsigned ConstantRange::getMinSignedBits() const {
424   if (isEmptySet())
425     return 0;
426 
427   return std::max(getSignedMin().getMinSignedBits(),
428                   getSignedMax().getMinSignedBits());
429 }
430 
431 ConstantRange ConstantRange::subtract(const APInt &Val) const {
432   assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
433   // If the set is empty or full, don't modify the endpoints.
434   if (Lower == Upper)
435     return *this;
436   return ConstantRange(Lower - Val, Upper - Val);
437 }
438 
439 ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
440   return intersectWith(CR.inverse());
441 }
442 
443 static ConstantRange getPreferredRange(
444     const ConstantRange &CR1, const ConstantRange &CR2,
445     ConstantRange::PreferredRangeType Type) {
446   if (Type == ConstantRange::Unsigned) {
447     if (!CR1.isWrappedSet() && CR2.isWrappedSet())
448       return CR1;
449     if (CR1.isWrappedSet() && !CR2.isWrappedSet())
450       return CR2;
451   } else if (Type == ConstantRange::Signed) {
452     if (!CR1.isSignWrappedSet() && CR2.isSignWrappedSet())
453       return CR1;
454     if (CR1.isSignWrappedSet() && !CR2.isSignWrappedSet())
455       return CR2;
456   }
457 
458   if (CR1.isSizeStrictlySmallerThan(CR2))
459     return CR1;
460   return CR2;
461 }
462 
463 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
464                                            PreferredRangeType Type) const {
465   assert(getBitWidth() == CR.getBitWidth() &&
466          "ConstantRange types don't agree!");
467 
468   // Handle common cases.
469   if (   isEmptySet() || CR.isFullSet()) return *this;
470   if (CR.isEmptySet() ||    isFullSet()) return CR;
471 
472   if (!isUpperWrapped() && CR.isUpperWrapped())
473     return CR.intersectWith(*this, Type);
474 
475   if (!isUpperWrapped() && !CR.isUpperWrapped()) {
476     if (Lower.ult(CR.Lower)) {
477       // L---U       : this
478       //       L---U : CR
479       if (Upper.ule(CR.Lower))
480         return getEmpty();
481 
482       // L---U       : this
483       //   L---U     : CR
484       if (Upper.ult(CR.Upper))
485         return ConstantRange(CR.Lower, Upper);
486 
487       // L-------U   : this
488       //   L---U     : CR
489       return CR;
490     }
491     //   L---U     : this
492     // L-------U   : CR
493     if (Upper.ult(CR.Upper))
494       return *this;
495 
496     //   L-----U   : this
497     // L-----U     : CR
498     if (Lower.ult(CR.Upper))
499       return ConstantRange(Lower, CR.Upper);
500 
501     //       L---U : this
502     // L---U       : CR
503     return getEmpty();
504   }
505 
506   if (isUpperWrapped() && !CR.isUpperWrapped()) {
507     if (CR.Lower.ult(Upper)) {
508       // ------U   L--- : this
509       //  L--U          : CR
510       if (CR.Upper.ult(Upper))
511         return CR;
512 
513       // ------U   L--- : this
514       //  L------U      : CR
515       if (CR.Upper.ule(Lower))
516         return ConstantRange(CR.Lower, Upper);
517 
518       // ------U   L--- : this
519       //  L----------U  : CR
520       return getPreferredRange(*this, CR, Type);
521     }
522     if (CR.Lower.ult(Lower)) {
523       // --U      L---- : this
524       //     L--U       : CR
525       if (CR.Upper.ule(Lower))
526         return getEmpty();
527 
528       // --U      L---- : this
529       //     L------U   : CR
530       return ConstantRange(Lower, CR.Upper);
531     }
532 
533     // --U  L------ : this
534     //        L--U  : CR
535     return CR;
536   }
537 
538   if (CR.Upper.ult(Upper)) {
539     // ------U L-- : this
540     // --U L------ : CR
541     if (CR.Lower.ult(Upper))
542       return getPreferredRange(*this, CR, Type);
543 
544     // ----U   L-- : this
545     // --U   L---- : CR
546     if (CR.Lower.ult(Lower))
547       return ConstantRange(Lower, CR.Upper);
548 
549     // ----U L---- : this
550     // --U     L-- : CR
551     return CR;
552   }
553   if (CR.Upper.ule(Lower)) {
554     // --U     L-- : this
555     // ----U L---- : CR
556     if (CR.Lower.ult(Lower))
557       return *this;
558 
559     // --U   L---- : this
560     // ----U   L-- : CR
561     return ConstantRange(CR.Lower, Upper);
562   }
563 
564   // --U L------ : this
565   // ------U L-- : CR
566   return getPreferredRange(*this, CR, Type);
567 }
568 
569 ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
570                                        PreferredRangeType Type) const {
571   assert(getBitWidth() == CR.getBitWidth() &&
572          "ConstantRange types don't agree!");
573 
574   if (   isFullSet() || CR.isEmptySet()) return *this;
575   if (CR.isFullSet() ||    isEmptySet()) return CR;
576 
577   if (!isUpperWrapped() && CR.isUpperWrapped())
578     return CR.unionWith(*this, Type);
579 
580   if (!isUpperWrapped() && !CR.isUpperWrapped()) {
581     //        L---U  and  L---U        : this
582     //  L---U                   L---U  : CR
583     // result in one of
584     //  L---------U
585     // -----U L-----
586     if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower))
587       return getPreferredRange(
588           ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
589 
590     APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
591     APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
592 
593     if (L.isNullValue() && U.isNullValue())
594       return getFull();
595 
596     return ConstantRange(std::move(L), std::move(U));
597   }
598 
599   if (!CR.isUpperWrapped()) {
600     // ------U   L-----  and  ------U   L----- : this
601     //   L--U                            L--U  : CR
602     if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
603       return *this;
604 
605     // ------U   L----- : this
606     //    L---------U   : CR
607     if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
608       return getFull();
609 
610     // ----U       L---- : this
611     //       L---U       : CR
612     // results in one of
613     // ----------U L----
614     // ----U L----------
615     if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower))
616       return getPreferredRange(
617           ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
618 
619     // ----U     L----- : this
620     //        L----U    : CR
621     if (Upper.ult(CR.Lower) && Lower.ule(CR.Upper))
622       return ConstantRange(CR.Lower, Upper);
623 
624     // ------U    L---- : this
625     //    L-----U       : CR
626     assert(CR.Lower.ule(Upper) && CR.Upper.ult(Lower) &&
627            "ConstantRange::unionWith missed a case with one range wrapped");
628     return ConstantRange(Lower, CR.Upper);
629   }
630 
631   // ------U    L----  and  ------U    L---- : this
632   // -U  L-----------  and  ------------U  L : CR
633   if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
634     return getFull();
635 
636   APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
637   APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
638 
639   return ConstantRange(std::move(L), std::move(U));
640 }
641 
642 ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
643                                     uint32_t ResultBitWidth) const {
644   switch (CastOp) {
645   default:
646     llvm_unreachable("unsupported cast type");
647   case Instruction::Trunc:
648     return truncate(ResultBitWidth);
649   case Instruction::SExt:
650     return signExtend(ResultBitWidth);
651   case Instruction::ZExt:
652     return zeroExtend(ResultBitWidth);
653   case Instruction::BitCast:
654     return *this;
655   case Instruction::FPToUI:
656   case Instruction::FPToSI:
657     if (getBitWidth() == ResultBitWidth)
658       return *this;
659     else
660       return getFull(ResultBitWidth);
661   case Instruction::UIToFP: {
662     // TODO: use input range if available
663     auto BW = getBitWidth();
664     APInt Min = APInt::getMinValue(BW).zextOrSelf(ResultBitWidth);
665     APInt Max = APInt::getMaxValue(BW).zextOrSelf(ResultBitWidth);
666     return ConstantRange(std::move(Min), std::move(Max));
667   }
668   case Instruction::SIToFP: {
669     // TODO: use input range if available
670     auto BW = getBitWidth();
671     APInt SMin = APInt::getSignedMinValue(BW).sextOrSelf(ResultBitWidth);
672     APInt SMax = APInt::getSignedMaxValue(BW).sextOrSelf(ResultBitWidth);
673     return ConstantRange(std::move(SMin), std::move(SMax));
674   }
675   case Instruction::FPTrunc:
676   case Instruction::FPExt:
677   case Instruction::IntToPtr:
678   case Instruction::PtrToInt:
679   case Instruction::AddrSpaceCast:
680     // Conservatively return getFull set.
681     return getFull(ResultBitWidth);
682   };
683 }
684 
685 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
686   if (isEmptySet()) return getEmpty(DstTySize);
687 
688   unsigned SrcTySize = getBitWidth();
689   assert(SrcTySize < DstTySize && "Not a value extension");
690   if (isFullSet() || isUpperWrapped()) {
691     // Change into [0, 1 << src bit width)
692     APInt LowerExt(DstTySize, 0);
693     if (!Upper) // special case: [X, 0) -- not really wrapping around
694       LowerExt = Lower.zext(DstTySize);
695     return ConstantRange(std::move(LowerExt),
696                          APInt::getOneBitSet(DstTySize, SrcTySize));
697   }
698 
699   return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
700 }
701 
702 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
703   if (isEmptySet()) return getEmpty(DstTySize);
704 
705   unsigned SrcTySize = getBitWidth();
706   assert(SrcTySize < DstTySize && "Not a value extension");
707 
708   // special case: [X, INT_MIN) -- not really wrapping around
709   if (Upper.isMinSignedValue())
710     return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
711 
712   if (isFullSet() || isSignWrappedSet()) {
713     return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
714                          APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
715   }
716 
717   return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
718 }
719 
720 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
721   assert(getBitWidth() > DstTySize && "Not a value truncation");
722   if (isEmptySet())
723     return getEmpty(DstTySize);
724   if (isFullSet())
725     return getFull(DstTySize);
726 
727   APInt LowerDiv(Lower), UpperDiv(Upper);
728   ConstantRange Union(DstTySize, /*isFullSet=*/false);
729 
730   // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
731   // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
732   // then we do the union with [MaxValue, Upper)
733   if (isUpperWrapped()) {
734     // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
735     // truncated range.
736     if (Upper.getActiveBits() > DstTySize ||
737         Upper.countTrailingOnes() == DstTySize)
738       return getFull(DstTySize);
739 
740     Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
741     UpperDiv.setAllBits();
742 
743     // Union covers the MaxValue case, so return if the remaining range is just
744     // MaxValue(DstTy).
745     if (LowerDiv == UpperDiv)
746       return Union;
747   }
748 
749   // Chop off the most significant bits that are past the destination bitwidth.
750   if (LowerDiv.getActiveBits() > DstTySize) {
751     // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv.
752     APInt Adjust = LowerDiv & APInt::getBitsSetFrom(getBitWidth(), DstTySize);
753     LowerDiv -= Adjust;
754     UpperDiv -= Adjust;
755   }
756 
757   unsigned UpperDivWidth = UpperDiv.getActiveBits();
758   if (UpperDivWidth <= DstTySize)
759     return ConstantRange(LowerDiv.trunc(DstTySize),
760                          UpperDiv.trunc(DstTySize)).unionWith(Union);
761 
762   // The truncated value wraps around. Check if we can do better than fullset.
763   if (UpperDivWidth == DstTySize + 1) {
764     // Clear the MSB so that UpperDiv wraps around.
765     UpperDiv.clearBit(DstTySize);
766     if (UpperDiv.ult(LowerDiv))
767       return ConstantRange(LowerDiv.trunc(DstTySize),
768                            UpperDiv.trunc(DstTySize)).unionWith(Union);
769   }
770 
771   return getFull(DstTySize);
772 }
773 
774 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
775   unsigned SrcTySize = getBitWidth();
776   if (SrcTySize > DstTySize)
777     return truncate(DstTySize);
778   if (SrcTySize < DstTySize)
779     return zeroExtend(DstTySize);
780   return *this;
781 }
782 
783 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
784   unsigned SrcTySize = getBitWidth();
785   if (SrcTySize > DstTySize)
786     return truncate(DstTySize);
787   if (SrcTySize < DstTySize)
788     return signExtend(DstTySize);
789   return *this;
790 }
791 
792 ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
793                                       const ConstantRange &Other) const {
794   assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
795 
796   switch (BinOp) {
797   case Instruction::Add:
798     return add(Other);
799   case Instruction::Sub:
800     return sub(Other);
801   case Instruction::Mul:
802     return multiply(Other);
803   case Instruction::UDiv:
804     return udiv(Other);
805   case Instruction::SDiv:
806     return sdiv(Other);
807   case Instruction::URem:
808     return urem(Other);
809   case Instruction::SRem:
810     return srem(Other);
811   case Instruction::Shl:
812     return shl(Other);
813   case Instruction::LShr:
814     return lshr(Other);
815   case Instruction::AShr:
816     return ashr(Other);
817   case Instruction::And:
818     return binaryAnd(Other);
819   case Instruction::Or:
820     return binaryOr(Other);
821   case Instruction::Xor:
822     return binaryXor(Other);
823   // Note: floating point operations applied to abstract ranges are just
824   // ideal integer operations with a lossy representation
825   case Instruction::FAdd:
826     return add(Other);
827   case Instruction::FSub:
828     return sub(Other);
829   case Instruction::FMul:
830     return multiply(Other);
831   default:
832     // Conservatively return getFull set.
833     return getFull();
834   }
835 }
836 
837 ConstantRange ConstantRange::overflowingBinaryOp(Instruction::BinaryOps BinOp,
838                                                  const ConstantRange &Other,
839                                                  unsigned NoWrapKind) const {
840   assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
841 
842   switch (BinOp) {
843   case Instruction::Add:
844     return addWithNoWrap(Other, NoWrapKind);
845   case Instruction::Sub:
846     return subWithNoWrap(Other, NoWrapKind);
847   default:
848     // Don't know about this Overflowing Binary Operation.
849     // Conservatively fallback to plain binop handling.
850     return binaryOp(BinOp, Other);
851   }
852 }
853 
854 bool ConstantRange::isIntrinsicSupported(Intrinsic::ID IntrinsicID) {
855   switch (IntrinsicID) {
856   case Intrinsic::uadd_sat:
857   case Intrinsic::usub_sat:
858   case Intrinsic::sadd_sat:
859   case Intrinsic::ssub_sat:
860   case Intrinsic::umin:
861   case Intrinsic::umax:
862   case Intrinsic::smin:
863   case Intrinsic::smax:
864   case Intrinsic::abs:
865     return true;
866   default:
867     return false;
868   }
869 }
870 
871 ConstantRange ConstantRange::intrinsic(Intrinsic::ID IntrinsicID,
872                                        ArrayRef<ConstantRange> Ops) {
873   switch (IntrinsicID) {
874   case Intrinsic::uadd_sat:
875     return Ops[0].uadd_sat(Ops[1]);
876   case Intrinsic::usub_sat:
877     return Ops[0].usub_sat(Ops[1]);
878   case Intrinsic::sadd_sat:
879     return Ops[0].sadd_sat(Ops[1]);
880   case Intrinsic::ssub_sat:
881     return Ops[0].ssub_sat(Ops[1]);
882   case Intrinsic::umin:
883     return Ops[0].umin(Ops[1]);
884   case Intrinsic::umax:
885     return Ops[0].umax(Ops[1]);
886   case Intrinsic::smin:
887     return Ops[0].smin(Ops[1]);
888   case Intrinsic::smax:
889     return Ops[0].smax(Ops[1]);
890   case Intrinsic::abs: {
891     const APInt *IntMinIsPoison = Ops[1].getSingleElement();
892     assert(IntMinIsPoison && "Must be known (immarg)");
893     assert(IntMinIsPoison->getBitWidth() == 1 && "Must be boolean");
894     return Ops[0].abs(IntMinIsPoison->getBoolValue());
895   }
896   default:
897     assert(!isIntrinsicSupported(IntrinsicID) && "Shouldn't be supported");
898     llvm_unreachable("Unsupported intrinsic");
899   }
900 }
901 
902 ConstantRange
903 ConstantRange::add(const ConstantRange &Other) const {
904   if (isEmptySet() || Other.isEmptySet())
905     return getEmpty();
906   if (isFullSet() || Other.isFullSet())
907     return getFull();
908 
909   APInt NewLower = getLower() + Other.getLower();
910   APInt NewUpper = getUpper() + Other.getUpper() - 1;
911   if (NewLower == NewUpper)
912     return getFull();
913 
914   ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
915   if (X.isSizeStrictlySmallerThan(*this) ||
916       X.isSizeStrictlySmallerThan(Other))
917     // We've wrapped, therefore, full set.
918     return getFull();
919   return X;
920 }
921 
922 ConstantRange ConstantRange::addWithNoWrap(const ConstantRange &Other,
923                                            unsigned NoWrapKind,
924                                            PreferredRangeType RangeType) const {
925   // Calculate the range for "X + Y" which is guaranteed not to wrap(overflow).
926   // (X is from this, and Y is from Other)
927   if (isEmptySet() || Other.isEmptySet())
928     return getEmpty();
929   if (isFullSet() && Other.isFullSet())
930     return getFull();
931 
932   using OBO = OverflowingBinaryOperator;
933   ConstantRange Result = add(Other);
934 
935   // If an overflow happens for every value pair in these two constant ranges,
936   // we must return Empty set. In this case, we get that for free, because we
937   // get lucky that intersection of add() with uadd_sat()/sadd_sat() results
938   // in an empty set.
939 
940   if (NoWrapKind & OBO::NoSignedWrap)
941     Result = Result.intersectWith(sadd_sat(Other), RangeType);
942 
943   if (NoWrapKind & OBO::NoUnsignedWrap)
944     Result = Result.intersectWith(uadd_sat(Other), RangeType);
945 
946   return Result;
947 }
948 
949 ConstantRange
950 ConstantRange::sub(const ConstantRange &Other) const {
951   if (isEmptySet() || Other.isEmptySet())
952     return getEmpty();
953   if (isFullSet() || Other.isFullSet())
954     return getFull();
955 
956   APInt NewLower = getLower() - Other.getUpper() + 1;
957   APInt NewUpper = getUpper() - Other.getLower();
958   if (NewLower == NewUpper)
959     return getFull();
960 
961   ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
962   if (X.isSizeStrictlySmallerThan(*this) ||
963       X.isSizeStrictlySmallerThan(Other))
964     // We've wrapped, therefore, full set.
965     return getFull();
966   return X;
967 }
968 
969 ConstantRange ConstantRange::subWithNoWrap(const ConstantRange &Other,
970                                            unsigned NoWrapKind,
971                                            PreferredRangeType RangeType) const {
972   // Calculate the range for "X - Y" which is guaranteed not to wrap(overflow).
973   // (X is from this, and Y is from Other)
974   if (isEmptySet() || Other.isEmptySet())
975     return getEmpty();
976   if (isFullSet() && Other.isFullSet())
977     return getFull();
978 
979   using OBO = OverflowingBinaryOperator;
980   ConstantRange Result = sub(Other);
981 
982   // If an overflow happens for every value pair in these two constant ranges,
983   // we must return Empty set. In signed case, we get that for free, because we
984   // get lucky that intersection of sub() with ssub_sat() results in an
985   // empty set. But for unsigned we must perform the overflow check manually.
986 
987   if (NoWrapKind & OBO::NoSignedWrap)
988     Result = Result.intersectWith(ssub_sat(Other), RangeType);
989 
990   if (NoWrapKind & OBO::NoUnsignedWrap) {
991     if (getUnsignedMax().ult(Other.getUnsignedMin()))
992       return getEmpty(); // Always overflows.
993     Result = Result.intersectWith(usub_sat(Other), RangeType);
994   }
995 
996   return Result;
997 }
998 
999 ConstantRange
1000 ConstantRange::multiply(const ConstantRange &Other) const {
1001   // TODO: If either operand is a single element and the multiply is known to
1002   // be non-wrapping, round the result min and max value to the appropriate
1003   // multiple of that element. If wrapping is possible, at least adjust the
1004   // range according to the greatest power-of-two factor of the single element.
1005 
1006   if (isEmptySet() || Other.isEmptySet())
1007     return getEmpty();
1008 
1009   // Multiplication is signedness-independent. However different ranges can be
1010   // obtained depending on how the input ranges are treated. These different
1011   // ranges are all conservatively correct, but one might be better than the
1012   // other. We calculate two ranges; one treating the inputs as unsigned
1013   // and the other signed, then return the smallest of these ranges.
1014 
1015   // Unsigned range first.
1016   APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
1017   APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
1018   APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
1019   APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
1020 
1021   ConstantRange Result_zext = ConstantRange(this_min * Other_min,
1022                                             this_max * Other_max + 1);
1023   ConstantRange UR = Result_zext.truncate(getBitWidth());
1024 
1025   // If the unsigned range doesn't wrap, and isn't negative then it's a range
1026   // from one positive number to another which is as good as we can generate.
1027   // In this case, skip the extra work of generating signed ranges which aren't
1028   // going to be better than this range.
1029   if (!UR.isUpperWrapped() &&
1030       (UR.getUpper().isNonNegative() || UR.getUpper().isMinSignedValue()))
1031     return UR;
1032 
1033   // Now the signed range. Because we could be dealing with negative numbers
1034   // here, the lower bound is the smallest of the cartesian product of the
1035   // lower and upper ranges; for example:
1036   //   [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1037   // Similarly for the upper bound, swapping min for max.
1038 
1039   this_min = getSignedMin().sext(getBitWidth() * 2);
1040   this_max = getSignedMax().sext(getBitWidth() * 2);
1041   Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
1042   Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
1043 
1044   auto L = {this_min * Other_min, this_min * Other_max,
1045             this_max * Other_min, this_max * Other_max};
1046   auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1047   ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
1048   ConstantRange SR = Result_sext.truncate(getBitWidth());
1049 
1050   return UR.isSizeStrictlySmallerThan(SR) ? UR : SR;
1051 }
1052 
1053 ConstantRange
1054 ConstantRange::smax(const ConstantRange &Other) const {
1055   // X smax Y is: range(smax(X_smin, Y_smin),
1056   //                    smax(X_smax, Y_smax))
1057   if (isEmptySet() || Other.isEmptySet())
1058     return getEmpty();
1059   APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
1060   APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
1061   return getNonEmpty(std::move(NewL), std::move(NewU));
1062 }
1063 
1064 ConstantRange
1065 ConstantRange::umax(const ConstantRange &Other) const {
1066   // X umax Y is: range(umax(X_umin, Y_umin),
1067   //                    umax(X_umax, Y_umax))
1068   if (isEmptySet() || Other.isEmptySet())
1069     return getEmpty();
1070   APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
1071   APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
1072   return getNonEmpty(std::move(NewL), std::move(NewU));
1073 }
1074 
1075 ConstantRange
1076 ConstantRange::smin(const ConstantRange &Other) const {
1077   // X smin Y is: range(smin(X_smin, Y_smin),
1078   //                    smin(X_smax, Y_smax))
1079   if (isEmptySet() || Other.isEmptySet())
1080     return getEmpty();
1081   APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
1082   APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
1083   return getNonEmpty(std::move(NewL), std::move(NewU));
1084 }
1085 
1086 ConstantRange
1087 ConstantRange::umin(const ConstantRange &Other) const {
1088   // X umin Y is: range(umin(X_umin, Y_umin),
1089   //                    umin(X_umax, Y_umax))
1090   if (isEmptySet() || Other.isEmptySet())
1091     return getEmpty();
1092   APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
1093   APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
1094   return getNonEmpty(std::move(NewL), std::move(NewU));
1095 }
1096 
1097 ConstantRange
1098 ConstantRange::udiv(const ConstantRange &RHS) const {
1099   if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isNullValue())
1100     return getEmpty();
1101 
1102   APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
1103 
1104   APInt RHS_umin = RHS.getUnsignedMin();
1105   if (RHS_umin.isNullValue()) {
1106     // We want the lowest value in RHS excluding zero. Usually that would be 1
1107     // except for a range in the form of [X, 1) in which case it would be X.
1108     if (RHS.getUpper() == 1)
1109       RHS_umin = RHS.getLower();
1110     else
1111       RHS_umin = 1;
1112   }
1113 
1114   APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
1115   return getNonEmpty(std::move(Lower), std::move(Upper));
1116 }
1117 
1118 ConstantRange ConstantRange::sdiv(const ConstantRange &RHS) const {
1119   // We split up the LHS and RHS into positive and negative components
1120   // and then also compute the positive and negative components of the result
1121   // separately by combining division results with the appropriate signs.
1122   APInt Zero = APInt::getNullValue(getBitWidth());
1123   APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
1124   ConstantRange PosFilter(APInt(getBitWidth(), 1), SignedMin);
1125   ConstantRange NegFilter(SignedMin, Zero);
1126   ConstantRange PosL = intersectWith(PosFilter);
1127   ConstantRange NegL = intersectWith(NegFilter);
1128   ConstantRange PosR = RHS.intersectWith(PosFilter);
1129   ConstantRange NegR = RHS.intersectWith(NegFilter);
1130 
1131   ConstantRange PosRes = getEmpty();
1132   if (!PosL.isEmptySet() && !PosR.isEmptySet())
1133     // pos / pos = pos.
1134     PosRes = ConstantRange(PosL.Lower.sdiv(PosR.Upper - 1),
1135                            (PosL.Upper - 1).sdiv(PosR.Lower) + 1);
1136 
1137   if (!NegL.isEmptySet() && !NegR.isEmptySet()) {
1138     // neg / neg = pos.
1139     //
1140     // We need to deal with one tricky case here: SignedMin / -1 is UB on the
1141     // IR level, so we'll want to exclude this case when calculating bounds.
1142     // (For APInts the operation is well-defined and yields SignedMin.) We
1143     // handle this by dropping either SignedMin from the LHS or -1 from the RHS.
1144     APInt Lo = (NegL.Upper - 1).sdiv(NegR.Lower);
1145     if (NegL.Lower.isMinSignedValue() && NegR.Upper.isNullValue()) {
1146       // Remove -1 from the LHS. Skip if it's the only element, as this would
1147       // leave us with an empty set.
1148       if (!NegR.Lower.isAllOnesValue()) {
1149         APInt AdjNegRUpper;
1150         if (RHS.Lower.isAllOnesValue())
1151           // Negative part of [-1, X] without -1 is [SignedMin, X].
1152           AdjNegRUpper = RHS.Upper;
1153         else
1154           // [X, -1] without -1 is [X, -2].
1155           AdjNegRUpper = NegR.Upper - 1;
1156 
1157         PosRes = PosRes.unionWith(
1158             ConstantRange(Lo, NegL.Lower.sdiv(AdjNegRUpper - 1) + 1));
1159       }
1160 
1161       // Remove SignedMin from the RHS. Skip if it's the only element, as this
1162       // would leave us with an empty set.
1163       if (NegL.Upper != SignedMin + 1) {
1164         APInt AdjNegLLower;
1165         if (Upper == SignedMin + 1)
1166           // Negative part of [X, SignedMin] without SignedMin is [X, -1].
1167           AdjNegLLower = Lower;
1168         else
1169           // [SignedMin, X] without SignedMin is [SignedMin + 1, X].
1170           AdjNegLLower = NegL.Lower + 1;
1171 
1172         PosRes = PosRes.unionWith(
1173             ConstantRange(std::move(Lo),
1174                           AdjNegLLower.sdiv(NegR.Upper - 1) + 1));
1175       }
1176     } else {
1177       PosRes = PosRes.unionWith(
1178           ConstantRange(std::move(Lo), NegL.Lower.sdiv(NegR.Upper - 1) + 1));
1179     }
1180   }
1181 
1182   ConstantRange NegRes = getEmpty();
1183   if (!PosL.isEmptySet() && !NegR.isEmptySet())
1184     // pos / neg = neg.
1185     NegRes = ConstantRange((PosL.Upper - 1).sdiv(NegR.Upper - 1),
1186                            PosL.Lower.sdiv(NegR.Lower) + 1);
1187 
1188   if (!NegL.isEmptySet() && !PosR.isEmptySet())
1189     // neg / pos = neg.
1190     NegRes = NegRes.unionWith(
1191         ConstantRange(NegL.Lower.sdiv(PosR.Lower),
1192                       (NegL.Upper - 1).sdiv(PosR.Upper - 1) + 1));
1193 
1194   // Prefer a non-wrapping signed range here.
1195   ConstantRange Res = NegRes.unionWith(PosRes, PreferredRangeType::Signed);
1196 
1197   // Preserve the zero that we dropped when splitting the LHS by sign.
1198   if (contains(Zero) && (!PosR.isEmptySet() || !NegR.isEmptySet()))
1199     Res = Res.unionWith(ConstantRange(Zero));
1200   return Res;
1201 }
1202 
1203 ConstantRange ConstantRange::urem(const ConstantRange &RHS) const {
1204   if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isNullValue())
1205     return getEmpty();
1206 
1207   // L % R for L < R is L.
1208   if (getUnsignedMax().ult(RHS.getUnsignedMin()))
1209     return *this;
1210 
1211   // L % R is <= L and < R.
1212   APInt Upper = APIntOps::umin(getUnsignedMax(), RHS.getUnsignedMax() - 1) + 1;
1213   return getNonEmpty(APInt::getNullValue(getBitWidth()), std::move(Upper));
1214 }
1215 
1216 ConstantRange ConstantRange::srem(const ConstantRange &RHS) const {
1217   if (isEmptySet() || RHS.isEmptySet())
1218     return getEmpty();
1219 
1220   ConstantRange AbsRHS = RHS.abs();
1221   APInt MinAbsRHS = AbsRHS.getUnsignedMin();
1222   APInt MaxAbsRHS = AbsRHS.getUnsignedMax();
1223 
1224   // Modulus by zero is UB.
1225   if (MaxAbsRHS.isNullValue())
1226     return getEmpty();
1227 
1228   if (MinAbsRHS.isNullValue())
1229     ++MinAbsRHS;
1230 
1231   APInt MinLHS = getSignedMin(), MaxLHS = getSignedMax();
1232 
1233   if (MinLHS.isNonNegative()) {
1234     // L % R for L < R is L.
1235     if (MaxLHS.ult(MinAbsRHS))
1236       return *this;
1237 
1238     // L % R is <= L and < R.
1239     APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1240     return ConstantRange(APInt::getNullValue(getBitWidth()), std::move(Upper));
1241   }
1242 
1243   // Same basic logic as above, but the result is negative.
1244   if (MaxLHS.isNegative()) {
1245     if (MinLHS.ugt(-MinAbsRHS))
1246       return *this;
1247 
1248     APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1249     return ConstantRange(std::move(Lower), APInt(getBitWidth(), 1));
1250   }
1251 
1252   // LHS range crosses zero.
1253   APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
1254   APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
1255   return ConstantRange(std::move(Lower), std::move(Upper));
1256 }
1257 
1258 ConstantRange ConstantRange::binaryNot() const {
1259   if (isEmptySet())
1260     return getEmpty();
1261 
1262   if (isWrappedSet())
1263     return getFull();
1264 
1265   return ConstantRange(APInt::getAllOnesValue(getBitWidth())).sub(*this);
1266 }
1267 
1268 ConstantRange
1269 ConstantRange::binaryAnd(const ConstantRange &Other) const {
1270   if (isEmptySet() || Other.isEmptySet())
1271     return getEmpty();
1272 
1273   // Use APInt's implementation of AND for single element ranges.
1274   if (isSingleElement() && Other.isSingleElement())
1275     return {*getSingleElement() & *Other.getSingleElement()};
1276 
1277   // TODO: replace this with something less conservative
1278 
1279   APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
1280   return getNonEmpty(APInt::getNullValue(getBitWidth()), std::move(umin) + 1);
1281 }
1282 
1283 ConstantRange
1284 ConstantRange::binaryOr(const ConstantRange &Other) const {
1285   if (isEmptySet() || Other.isEmptySet())
1286     return getEmpty();
1287 
1288   // Use APInt's implementation of OR for single element ranges.
1289   if (isSingleElement() && Other.isSingleElement())
1290     return {*getSingleElement() | *Other.getSingleElement()};
1291 
1292   // TODO: replace this with something less conservative
1293 
1294   APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
1295   return getNonEmpty(std::move(umax), APInt::getNullValue(getBitWidth()));
1296 }
1297 
1298 ConstantRange ConstantRange::binaryXor(const ConstantRange &Other) const {
1299   if (isEmptySet() || Other.isEmptySet())
1300     return getEmpty();
1301 
1302   // Use APInt's implementation of XOR for single element ranges.
1303   if (isSingleElement() && Other.isSingleElement())
1304     return {*getSingleElement() ^ *Other.getSingleElement()};
1305 
1306   // Special-case binary complement, since we can give a precise answer.
1307   if (Other.isSingleElement() && Other.getSingleElement()->isAllOnesValue())
1308     return binaryNot();
1309   if (isSingleElement() && getSingleElement()->isAllOnesValue())
1310     return Other.binaryNot();
1311 
1312   // TODO: replace this with something less conservative
1313   return getFull();
1314 }
1315 
1316 ConstantRange
1317 ConstantRange::shl(const ConstantRange &Other) const {
1318   if (isEmptySet() || Other.isEmptySet())
1319     return getEmpty();
1320 
1321   APInt max = getUnsignedMax();
1322   APInt Other_umax = Other.getUnsignedMax();
1323 
1324   // If we are shifting by maximum amount of
1325   // zero return return the original range.
1326   if (Other_umax.isNullValue())
1327     return *this;
1328   // there's overflow!
1329   if (Other_umax.ugt(max.countLeadingZeros()))
1330     return getFull();
1331 
1332   // FIXME: implement the other tricky cases
1333 
1334   APInt min = getUnsignedMin();
1335   min <<= Other.getUnsignedMin();
1336   max <<= Other_umax;
1337 
1338   return ConstantRange(std::move(min), std::move(max) + 1);
1339 }
1340 
1341 ConstantRange
1342 ConstantRange::lshr(const ConstantRange &Other) const {
1343   if (isEmptySet() || Other.isEmptySet())
1344     return getEmpty();
1345 
1346   APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1;
1347   APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
1348   return getNonEmpty(std::move(min), std::move(max));
1349 }
1350 
1351 ConstantRange
1352 ConstantRange::ashr(const ConstantRange &Other) const {
1353   if (isEmptySet() || Other.isEmptySet())
1354     return getEmpty();
1355 
1356   // May straddle zero, so handle both positive and negative cases.
1357   // 'PosMax' is the upper bound of the result of the ashr
1358   // operation, when Upper of the LHS of ashr is a non-negative.
1359   // number. Since ashr of a non-negative number will result in a
1360   // smaller number, the Upper value of LHS is shifted right with
1361   // the minimum value of 'Other' instead of the maximum value.
1362   APInt PosMax = getSignedMax().ashr(Other.getUnsignedMin()) + 1;
1363 
1364   // 'PosMin' is the lower bound of the result of the ashr
1365   // operation, when Lower of the LHS is a non-negative number.
1366   // Since ashr of a non-negative number will result in a smaller
1367   // number, the Lower value of LHS is shifted right with the
1368   // maximum value of 'Other'.
1369   APInt PosMin = getSignedMin().ashr(Other.getUnsignedMax());
1370 
1371   // 'NegMax' is the upper bound of the result of the ashr
1372   // operation, when Upper of the LHS of ashr is a negative number.
1373   // Since 'ashr' of a negative number will result in a bigger
1374   // number, the Upper value of LHS is shifted right with the
1375   // maximum value of 'Other'.
1376   APInt NegMax = getSignedMax().ashr(Other.getUnsignedMax()) + 1;
1377 
1378   // 'NegMin' is the lower bound of the result of the ashr
1379   // operation, when Lower of the LHS of ashr is a negative number.
1380   // Since 'ashr' of a negative number will result in a bigger
1381   // number, the Lower value of LHS is shifted right with the
1382   // minimum value of 'Other'.
1383   APInt NegMin = getSignedMin().ashr(Other.getUnsignedMin());
1384 
1385   APInt max, min;
1386   if (getSignedMin().isNonNegative()) {
1387     // Upper and Lower of LHS are non-negative.
1388     min = PosMin;
1389     max = PosMax;
1390   } else if (getSignedMax().isNegative()) {
1391     // Upper and Lower of LHS are negative.
1392     min = NegMin;
1393     max = NegMax;
1394   } else {
1395     // Upper is non-negative and Lower is negative.
1396     min = NegMin;
1397     max = PosMax;
1398   }
1399   return getNonEmpty(std::move(min), std::move(max));
1400 }
1401 
1402 ConstantRange ConstantRange::uadd_sat(const ConstantRange &Other) const {
1403   if (isEmptySet() || Other.isEmptySet())
1404     return getEmpty();
1405 
1406   APInt NewL = getUnsignedMin().uadd_sat(Other.getUnsignedMin());
1407   APInt NewU = getUnsignedMax().uadd_sat(Other.getUnsignedMax()) + 1;
1408   return getNonEmpty(std::move(NewL), std::move(NewU));
1409 }
1410 
1411 ConstantRange ConstantRange::sadd_sat(const ConstantRange &Other) const {
1412   if (isEmptySet() || Other.isEmptySet())
1413     return getEmpty();
1414 
1415   APInt NewL = getSignedMin().sadd_sat(Other.getSignedMin());
1416   APInt NewU = getSignedMax().sadd_sat(Other.getSignedMax()) + 1;
1417   return getNonEmpty(std::move(NewL), std::move(NewU));
1418 }
1419 
1420 ConstantRange ConstantRange::usub_sat(const ConstantRange &Other) const {
1421   if (isEmptySet() || Other.isEmptySet())
1422     return getEmpty();
1423 
1424   APInt NewL = getUnsignedMin().usub_sat(Other.getUnsignedMax());
1425   APInt NewU = getUnsignedMax().usub_sat(Other.getUnsignedMin()) + 1;
1426   return getNonEmpty(std::move(NewL), std::move(NewU));
1427 }
1428 
1429 ConstantRange ConstantRange::ssub_sat(const ConstantRange &Other) const {
1430   if (isEmptySet() || Other.isEmptySet())
1431     return getEmpty();
1432 
1433   APInt NewL = getSignedMin().ssub_sat(Other.getSignedMax());
1434   APInt NewU = getSignedMax().ssub_sat(Other.getSignedMin()) + 1;
1435   return getNonEmpty(std::move(NewL), std::move(NewU));
1436 }
1437 
1438 ConstantRange ConstantRange::umul_sat(const ConstantRange &Other) const {
1439   if (isEmptySet() || Other.isEmptySet())
1440     return getEmpty();
1441 
1442   APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
1443   APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
1444   return getNonEmpty(std::move(NewL), std::move(NewU));
1445 }
1446 
1447 ConstantRange ConstantRange::smul_sat(const ConstantRange &Other) const {
1448   if (isEmptySet() || Other.isEmptySet())
1449     return getEmpty();
1450 
1451   // Because we could be dealing with negative numbers here, the lower bound is
1452   // the smallest of the cartesian product of the lower and upper ranges;
1453   // for example:
1454   //   [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
1455   // Similarly for the upper bound, swapping min for max.
1456 
1457   APInt this_min = getSignedMin().sext(getBitWidth() * 2);
1458   APInt this_max = getSignedMax().sext(getBitWidth() * 2);
1459   APInt Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
1460   APInt Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
1461 
1462   auto L = {this_min * Other_min, this_min * Other_max, this_max * Other_min,
1463             this_max * Other_max};
1464   auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
1465 
1466   // Note that we wanted to perform signed saturating multiplication,
1467   // so since we performed plain multiplication in twice the bitwidth,
1468   // we need to perform signed saturating truncation.
1469   return getNonEmpty(std::min(L, Compare).truncSSat(getBitWidth()),
1470                      std::max(L, Compare).truncSSat(getBitWidth()) + 1);
1471 }
1472 
1473 ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const {
1474   if (isEmptySet() || Other.isEmptySet())
1475     return getEmpty();
1476 
1477   APInt NewL = getUnsignedMin().ushl_sat(Other.getUnsignedMin());
1478   APInt NewU = getUnsignedMax().ushl_sat(Other.getUnsignedMax()) + 1;
1479   return getNonEmpty(std::move(NewL), std::move(NewU));
1480 }
1481 
1482 ConstantRange ConstantRange::sshl_sat(const ConstantRange &Other) const {
1483   if (isEmptySet() || Other.isEmptySet())
1484     return getEmpty();
1485 
1486   APInt Min = getSignedMin(), Max = getSignedMax();
1487   APInt ShAmtMin = Other.getUnsignedMin(), ShAmtMax = Other.getUnsignedMax();
1488   APInt NewL = Min.sshl_sat(Min.isNonNegative() ? ShAmtMin : ShAmtMax);
1489   APInt NewU = Max.sshl_sat(Max.isNegative() ? ShAmtMin : ShAmtMax) + 1;
1490   return getNonEmpty(std::move(NewL), std::move(NewU));
1491 }
1492 
1493 ConstantRange ConstantRange::inverse() const {
1494   if (isFullSet())
1495     return getEmpty();
1496   if (isEmptySet())
1497     return getFull();
1498   return ConstantRange(Upper, Lower);
1499 }
1500 
1501 ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
1502   if (isEmptySet())
1503     return getEmpty();
1504 
1505   if (isSignWrappedSet()) {
1506     APInt Lo;
1507     // Check whether the range crosses zero.
1508     if (Upper.isStrictlyPositive() || !Lower.isStrictlyPositive())
1509       Lo = APInt::getNullValue(getBitWidth());
1510     else
1511       Lo = APIntOps::umin(Lower, -Upper + 1);
1512 
1513     // If SignedMin is not poison, then it is included in the result range.
1514     if (IntMinIsPoison)
1515       return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()));
1516     else
1517       return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()) + 1);
1518   }
1519 
1520   APInt SMin = getSignedMin(), SMax = getSignedMax();
1521 
1522   // Skip SignedMin if it is poison.
1523   if (IntMinIsPoison && SMin.isMinSignedValue()) {
1524     // The range may become empty if it *only* contains SignedMin.
1525     if (SMax.isMinSignedValue())
1526       return getEmpty();
1527     ++SMin;
1528   }
1529 
1530   // All non-negative.
1531   if (SMin.isNonNegative())
1532     return *this;
1533 
1534   // All negative.
1535   if (SMax.isNegative())
1536     return ConstantRange(-SMax, -SMin + 1);
1537 
1538   // Range crosses zero.
1539   return ConstantRange(APInt::getNullValue(getBitWidth()),
1540                        APIntOps::umax(-SMin, SMax) + 1);
1541 }
1542 
1543 ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow(
1544     const ConstantRange &Other) const {
1545   if (isEmptySet() || Other.isEmptySet())
1546     return OverflowResult::MayOverflow;
1547 
1548   APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1549   APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1550 
1551   // a u+ b overflows high iff a u> ~b.
1552   if (Min.ugt(~OtherMin))
1553     return OverflowResult::AlwaysOverflowsHigh;
1554   if (Max.ugt(~OtherMax))
1555     return OverflowResult::MayOverflow;
1556   return OverflowResult::NeverOverflows;
1557 }
1558 
1559 ConstantRange::OverflowResult ConstantRange::signedAddMayOverflow(
1560     const ConstantRange &Other) const {
1561   if (isEmptySet() || Other.isEmptySet())
1562     return OverflowResult::MayOverflow;
1563 
1564   APInt Min = getSignedMin(), Max = getSignedMax();
1565   APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1566 
1567   APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
1568   APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
1569 
1570   // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b.
1571   // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b.
1572   if (Min.isNonNegative() && OtherMin.isNonNegative() &&
1573       Min.sgt(SignedMax - OtherMin))
1574     return OverflowResult::AlwaysOverflowsHigh;
1575   if (Max.isNegative() && OtherMax.isNegative() &&
1576       Max.slt(SignedMin - OtherMax))
1577     return OverflowResult::AlwaysOverflowsLow;
1578 
1579   if (Max.isNonNegative() && OtherMax.isNonNegative() &&
1580       Max.sgt(SignedMax - OtherMax))
1581     return OverflowResult::MayOverflow;
1582   if (Min.isNegative() && OtherMin.isNegative() &&
1583       Min.slt(SignedMin - OtherMin))
1584     return OverflowResult::MayOverflow;
1585 
1586   return OverflowResult::NeverOverflows;
1587 }
1588 
1589 ConstantRange::OverflowResult ConstantRange::unsignedSubMayOverflow(
1590     const ConstantRange &Other) const {
1591   if (isEmptySet() || Other.isEmptySet())
1592     return OverflowResult::MayOverflow;
1593 
1594   APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1595   APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1596 
1597   // a u- b overflows low iff a u< b.
1598   if (Max.ult(OtherMin))
1599     return OverflowResult::AlwaysOverflowsLow;
1600   if (Min.ult(OtherMax))
1601     return OverflowResult::MayOverflow;
1602   return OverflowResult::NeverOverflows;
1603 }
1604 
1605 ConstantRange::OverflowResult ConstantRange::signedSubMayOverflow(
1606     const ConstantRange &Other) const {
1607   if (isEmptySet() || Other.isEmptySet())
1608     return OverflowResult::MayOverflow;
1609 
1610   APInt Min = getSignedMin(), Max = getSignedMax();
1611   APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
1612 
1613   APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
1614   APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
1615 
1616   // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b.
1617   // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b.
1618   if (Min.isNonNegative() && OtherMax.isNegative() &&
1619       Min.sgt(SignedMax + OtherMax))
1620     return OverflowResult::AlwaysOverflowsHigh;
1621   if (Max.isNegative() && OtherMin.isNonNegative() &&
1622       Max.slt(SignedMin + OtherMin))
1623     return OverflowResult::AlwaysOverflowsLow;
1624 
1625   if (Max.isNonNegative() && OtherMin.isNegative() &&
1626       Max.sgt(SignedMax + OtherMin))
1627     return OverflowResult::MayOverflow;
1628   if (Min.isNegative() && OtherMax.isNonNegative() &&
1629       Min.slt(SignedMin + OtherMax))
1630     return OverflowResult::MayOverflow;
1631 
1632   return OverflowResult::NeverOverflows;
1633 }
1634 
1635 ConstantRange::OverflowResult ConstantRange::unsignedMulMayOverflow(
1636     const ConstantRange &Other) const {
1637   if (isEmptySet() || Other.isEmptySet())
1638     return OverflowResult::MayOverflow;
1639 
1640   APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1641   APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1642   bool Overflow;
1643 
1644   (void) Min.umul_ov(OtherMin, Overflow);
1645   if (Overflow)
1646     return OverflowResult::AlwaysOverflowsHigh;
1647 
1648   (void) Max.umul_ov(OtherMax, Overflow);
1649   if (Overflow)
1650     return OverflowResult::MayOverflow;
1651 
1652   return OverflowResult::NeverOverflows;
1653 }
1654 
1655 void ConstantRange::print(raw_ostream &OS) const {
1656   if (isFullSet())
1657     OS << "full-set";
1658   else if (isEmptySet())
1659     OS << "empty-set";
1660   else
1661     OS << "[" << Lower << "," << Upper << ")";
1662 }
1663 
1664 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1665 LLVM_DUMP_METHOD void ConstantRange::dump() const {
1666   print(dbgs());
1667 }
1668 #endif
1669 
1670 ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
1671   const unsigned NumRanges = Ranges.getNumOperands() / 2;
1672   assert(NumRanges >= 1 && "Must have at least one range!");
1673   assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
1674 
1675   auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
1676   auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
1677 
1678   ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
1679 
1680   for (unsigned i = 1; i < NumRanges; ++i) {
1681     auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
1682     auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
1683 
1684     // Note: unionWith will potentially create a range that contains values not
1685     // contained in any of the original N ranges.
1686     CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
1687   }
1688 
1689   return CR;
1690 }
1691