Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Thu Jul 17 2025 12:26:56 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp