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/Constants.h"
26 #include "llvm/Instruction.h"
27 #include "llvm/Type.h"
30 static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
31 Constant *C = ConstantExpr::get(Instruction::SetLT, A, B);
32 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
33 return cast<ConstantBool>(C)->getValue();
36 static bool GT(ConstantIntegral *A, ConstantIntegral *B) {
37 Constant *C = ConstantExpr::get(Instruction::SetGT, A, B);
38 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
39 return cast<ConstantBool>(C)->getValue();
42 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
43 return LT(A, B) ? A : B;
45 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
46 return GT(A, B) ? A : B;
50 /// Initialize a full (the default) or empty set for the specified type.
52 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
53 assert(Ty->isIntegral() &&
54 "Cannot make constant range of non-integral type!");
56 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
58 Lower = Upper = ConstantIntegral::getMinValue(Ty);
61 /// Initialize a range of values explicitly... this will assert out if
62 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
63 /// have different types)
65 ConstantRange::ConstantRange(ConstantIntegral *L,
66 ConstantIntegral *U) : Lower(L), Upper(U) {
67 assert(Lower->getType() == Upper->getType() &&
68 "Incompatible types for ConstantRange!");
70 // Make sure that if L & U are equal that they are either Min or Max...
71 assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
72 L == ConstantIntegral::getMinValue(L->getType()))) &&
73 "Lower == Upper, but they aren't min or max for type!");
76 static ConstantIntegral *Next(ConstantIntegral *CI) {
77 if (CI->getType() == Type::BoolTy)
78 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
80 Constant *Result = ConstantExpr::get(Instruction::Add, CI,
81 ConstantInt::get(CI->getType(), 1));
82 return cast<ConstantIntegral>(Result);
85 /// Initialize a set of values that all satisfy the condition with C.
87 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
88 switch (SetCCOpcode) {
89 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
90 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
91 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
92 case Instruction::SetLT:
93 Lower = ConstantIntegral::getMinValue(C->getType());
96 case Instruction::SetGT:
98 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
100 case Instruction::SetLE:
101 Lower = ConstantIntegral::getMinValue(C->getType());
104 case Instruction::SetGE:
106 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
111 /// getType - Return the LLVM data type of this range.
113 const Type *ConstantRange::getType() const { return Lower->getType(); }
115 /// isFullSet - Return true if this set contains all of the elements possible
116 /// for this data-type
117 bool ConstantRange::isFullSet() const {
118 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
121 /// isEmptySet - Return true if this set contains no members.
123 bool ConstantRange::isEmptySet() const {
124 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
127 /// isWrappedSet - Return true if this set wraps around the top of the range,
128 /// for example: [100, 8)
130 bool ConstantRange::isWrappedSet() const {
131 return GT(Lower, Upper);
135 /// getSingleElement - If this set contains a single element, return it,
136 /// otherwise return null.
137 ConstantIntegral *ConstantRange::getSingleElement() const {
138 if (Upper == Next(Lower)) // Is it a single element range?
143 /// getSetSize - Return the number of elements in this set.
145 uint64_t ConstantRange::getSetSize() const {
146 if (isEmptySet()) return 0;
147 if (getType() == Type::BoolTy) {
148 if (Lower != Upper) // One of T or F in the set...
150 return 2; // Must be full set...
153 // Simply subtract the bounds...
155 ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower);
156 return cast<ConstantInt>(Result)->getRawValue();
162 // intersect1Wrapped - This helper function is used to intersect two ranges when
163 // it is known that LHS is wrapped and RHS isn't.
165 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
166 const ConstantRange &RHS) {
167 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
169 // Check to see if we overlap on the Left side of RHS...
171 if (LT(RHS.getLower(), LHS.getUpper())) {
172 // We do overlap on the left side of RHS, see if we overlap on the right of
174 if (GT(RHS.getUpper(), LHS.getLower())) {
175 // Ok, the result overlaps on both the left and right sides. See if the
176 // resultant interval will be smaller if we wrap or not...
178 if (LHS.getSetSize() < RHS.getSetSize())
184 // No overlap on the right, just on the left.
185 return ConstantRange(RHS.getLower(), LHS.getUpper());
189 // We don't overlap on the left side of RHS, see if we overlap on the right
191 if (GT(RHS.getUpper(), LHS.getLower())) {
193 return ConstantRange(LHS.getLower(), RHS.getUpper());
196 return ConstantRange(LHS.getType(), false);
201 /// intersect - Return the range that results from the intersection of this
202 /// range with another range.
204 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
205 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
206 // Handle common special cases
207 if (isEmptySet() || CR.isFullSet()) return *this;
208 if (isFullSet() || CR.isEmptySet()) return CR;
210 if (!isWrappedSet()) {
211 if (!CR.isWrappedSet()) {
212 ConstantIntegral *L = Max(Lower, CR.Lower);
213 ConstantIntegral *U = Min(Upper, CR.Upper);
215 if (LT(L, U)) // If range isn't empty...
216 return ConstantRange(L, U);
218 return ConstantRange(getType(), false); // Otherwise, return empty set
220 return intersect1Wrapped(CR, *this);
221 } else { // We know "this" is wrapped...
222 if (!CR.isWrappedSet())
223 return intersect1Wrapped(*this, CR);
225 // Both ranges are wrapped...
226 ConstantIntegral *L = Max(Lower, CR.Lower);
227 ConstantIntegral *U = Min(Upper, CR.Upper);
228 return ConstantRange(L, U);
234 /// union - Return the range that results from the union of this range with
235 /// another range. The resultant range is guaranteed to include the elements of
236 /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
237 /// 15), which includes 9, 10, and 11, which were not included in either set
240 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
241 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
243 assert(0 && "Range union not implemented yet!");
248 /// print - Print out the bounds to a stream...
250 void ConstantRange::print(std::ostream &OS) const {
251 OS << "[" << Lower << "," << Upper << " )";
254 /// dump - Allow printing from a debugger easily...
256 void ConstantRange::dump() const {