1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Represent a range of possible values that may occur when the program is run
11 // for an integral value. This keeps track of a lower and upper bound for the
12 // constant, which MAY wrap around the end of the numeric range. To do this, it
13 // keeps track of a [lower, upper) bound, which specifies an interval just like
14 // STL iterators. When used with boolean values, the following are important
15 // ranges (other integral ranges use min/max values for special range values):
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Type.h"
26 #include "llvm/Instruction.h"
27 #include "llvm/ConstantHandling.h"
31 /// Initialize a full (the default) or empty set for the specified type.
33 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
34 assert(Ty->isIntegral() &&
35 "Cannot make constant range of non-integral type!");
37 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
39 Lower = Upper = ConstantIntegral::getMinValue(Ty);
42 /// Initialize a range of values explicitly... this will assert out if
43 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
44 /// have different types)
46 ConstantRange::ConstantRange(ConstantIntegral *L,
47 ConstantIntegral *U) : Lower(L), Upper(U) {
48 assert(Lower->getType() == Upper->getType() &&
49 "Incompatible types for ConstantRange!");
51 // Make sure that if L & U are equal that they are either Min or Max...
52 assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
53 L == ConstantIntegral::getMinValue(L->getType()))) &&
54 "Lower == Upper, but they aren't min or max for type!");
57 static ConstantIntegral *Next(ConstantIntegral *CI) {
58 if (CI->getType() == Type::BoolTy)
59 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
61 // Otherwise use operator+ in the ConstantHandling Library.
62 Constant *Result = *ConstantInt::get(CI->getType(), 1) + *CI;
63 assert(Result && "ConstantHandling not implemented for integral plus!?");
64 return cast<ConstantIntegral>(Result);
67 /// Initialize a set of values that all satisfy the condition with C.
69 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
70 switch (SetCCOpcode) {
71 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
72 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
73 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
74 case Instruction::SetLT:
75 Lower = ConstantIntegral::getMinValue(C->getType());
78 case Instruction::SetGT:
80 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
82 case Instruction::SetLE:
83 Lower = ConstantIntegral::getMinValue(C->getType());
86 case Instruction::SetGE:
88 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
93 /// getType - Return the LLVM data type of this range.
95 const Type *ConstantRange::getType() const { return Lower->getType(); }
97 /// isFullSet - Return true if this set contains all of the elements possible
98 /// for this data-type
99 bool ConstantRange::isFullSet() const {
100 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
103 /// isEmptySet - Return true if this set contains no members.
105 bool ConstantRange::isEmptySet() const {
106 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
109 /// isWrappedSet - Return true if this set wraps around the top of the range,
110 /// for example: [100, 8)
112 bool ConstantRange::isWrappedSet() const {
113 return (*(Constant*)Lower > *(Constant*)Upper)->getValue();
117 /// getSingleElement - If this set contains a single element, return it,
118 /// otherwise return null.
119 ConstantIntegral *ConstantRange::getSingleElement() const {
120 if (Upper == Next(Lower)) // Is it a single element range?
125 /// getSetSize - Return the number of elements in this set.
127 uint64_t ConstantRange::getSetSize() const {
128 if (isEmptySet()) return 0;
129 if (getType() == Type::BoolTy) {
130 if (Lower != Upper) // One of T or F in the set...
132 return 2; // Must be full set...
135 // Simply subtract the bounds...
136 Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
137 assert(Result && "Subtraction of constant integers not implemented?");
138 return cast<ConstantInt>(Result)->getRawValue();
144 // intersect1Wrapped - This helper function is used to intersect two ranges when
145 // it is known that LHS is wrapped and RHS isn't.
147 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
148 const ConstantRange &RHS) {
149 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
151 // Check to see if we overlap on the Left side of RHS...
153 if ((*(Constant*)RHS.getLower() < *(Constant*)LHS.getUpper())->getValue()) {
154 // We do overlap on the left side of RHS, see if we overlap on the right of
156 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
157 // Ok, the result overlaps on both the left and right sides. See if the
158 // resultant interval will be smaller if we wrap or not...
160 if (LHS.getSetSize() < RHS.getSetSize())
166 // No overlap on the right, just on the left.
167 return ConstantRange(RHS.getLower(), LHS.getUpper());
171 // We don't overlap on the left side of RHS, see if we overlap on the right
173 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
175 return ConstantRange(LHS.getLower(), RHS.getUpper());
178 return ConstantRange(LHS.getType(), false);
183 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
184 if ((*(Constant*)A < *(Constant*)B)->getValue())
188 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
189 if ((*(Constant*)A > *(Constant*)B)->getValue())
195 /// intersect - Return the range that results from the intersection of this
196 /// range with another range.
198 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
199 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
200 // Handle common special cases
201 if (isEmptySet() || CR.isFullSet()) return *this;
202 if (isFullSet() || CR.isEmptySet()) return CR;
204 if (!isWrappedSet()) {
205 if (!CR.isWrappedSet()) {
206 ConstantIntegral *L = Max(Lower, CR.Lower);
207 ConstantIntegral *U = Min(Upper, CR.Upper);
209 if ((*L < *U)->getValue()) // If range isn't empty...
210 return ConstantRange(L, U);
212 return ConstantRange(getType(), false); // Otherwise, return empty set
214 return intersect1Wrapped(CR, *this);
215 } else { // We know "this" is wrapped...
216 if (!CR.isWrappedSet())
217 return intersect1Wrapped(*this, CR);
219 // Both ranges are wrapped...
220 ConstantIntegral *L = Max(Lower, CR.Lower);
221 ConstantIntegral *U = Min(Upper, CR.Upper);
222 return ConstantRange(L, U);
228 /// union - Return the range that results from the union of this range with
229 /// another range. The resultant range is guaranteed to include the elements of
230 /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
231 /// 15), which includes 9, 10, and 11, which were not included in either set
234 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
235 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
237 assert(0 && "Range union not implemented yet!");
242 /// print - Print out the bounds to a stream...
244 void ConstantRange::print(std::ostream &OS) const {
245 OS << "[" << Lower << "," << Upper << " )";
248 /// dump - Allow printing from a debugger easily...
250 void ConstantRange::dump() const {
254 } // End llvm namespace