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"
29 /// Initialize a full (the default) or empty set for the specified type.
31 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
32 assert(Ty->isIntegral() &&
33 "Cannot make constant range of non-integral type!");
35 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
37 Lower = Upper = ConstantIntegral::getMinValue(Ty);
40 /// Initialize a range of values explicitly... this will assert out if
41 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
42 /// have different types)
44 ConstantRange::ConstantRange(ConstantIntegral *L,
45 ConstantIntegral *U) : Lower(L), Upper(U) {
46 assert(Lower->getType() == Upper->getType() &&
47 "Incompatible types for ConstantRange!");
49 // Make sure that if L & U are equal that they are either Min or Max...
50 assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
51 L == ConstantIntegral::getMinValue(L->getType()))) &&
52 "Lower == Upper, but they aren't min or max for type!");
55 static ConstantIntegral *Next(ConstantIntegral *CI) {
56 if (CI->getType() == Type::BoolTy)
57 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
59 // Otherwise use operator+ in the ConstantHandling Library.
60 Constant *Result = *ConstantInt::get(CI->getType(), 1) + *CI;
61 assert(Result && "ConstantHandling not implemented for integral plus!?");
62 return cast<ConstantIntegral>(Result);
65 /// Initialize a set of values that all satisfy the condition with C.
67 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
68 switch (SetCCOpcode) {
69 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
70 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
71 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
72 case Instruction::SetLT:
73 Lower = ConstantIntegral::getMinValue(C->getType());
76 case Instruction::SetGT:
78 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
80 case Instruction::SetLE:
81 Lower = ConstantIntegral::getMinValue(C->getType());
84 case Instruction::SetGE:
86 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
91 /// getType - Return the LLVM data type of this range.
93 const Type *ConstantRange::getType() const { return Lower->getType(); }
95 /// isFullSet - Return true if this set contains all of the elements possible
96 /// for this data-type
97 bool ConstantRange::isFullSet() const {
98 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
101 /// isEmptySet - Return true if this set contains no members.
103 bool ConstantRange::isEmptySet() const {
104 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
107 /// isWrappedSet - Return true if this set wraps around the top of the range,
108 /// for example: [100, 8)
110 bool ConstantRange::isWrappedSet() const {
111 return (*(Constant*)Lower > *(Constant*)Upper)->getValue();
115 /// getSingleElement - If this set contains a single element, return it,
116 /// otherwise return null.
117 ConstantIntegral *ConstantRange::getSingleElement() const {
118 if (Upper == Next(Lower)) // Is it a single element range?
123 /// getSetSize - Return the number of elements in this set.
125 uint64_t ConstantRange::getSetSize() const {
126 if (isEmptySet()) return 0;
127 if (getType() == Type::BoolTy) {
128 if (Lower != Upper) // One of T or F in the set...
130 return 2; // Must be full set...
133 // Simply subtract the bounds...
134 Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
135 assert(Result && "Subtraction of constant integers not implemented?");
136 return cast<ConstantInt>(Result)->getRawValue();
142 // intersect1Wrapped - This helper function is used to intersect two ranges when
143 // it is known that LHS is wrapped and RHS isn't.
145 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
146 const ConstantRange &RHS) {
147 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
149 // Check to see if we overlap on the Left side of RHS...
151 if ((*(Constant*)RHS.getLower() < *(Constant*)LHS.getUpper())->getValue()) {
152 // We do overlap on the left side of RHS, see if we overlap on the right of
154 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
155 // Ok, the result overlaps on both the left and right sides. See if the
156 // resultant interval will be smaller if we wrap or not...
158 if (LHS.getSetSize() < RHS.getSetSize())
164 // No overlap on the right, just on the left.
165 return ConstantRange(RHS.getLower(), LHS.getUpper());
169 // We don't overlap on the left side of RHS, see if we overlap on the right
171 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
173 return ConstantRange(LHS.getLower(), RHS.getUpper());
176 return ConstantRange(LHS.getType(), false);
181 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
182 if ((*(Constant*)A < *(Constant*)B)->getValue())
186 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
187 if ((*(Constant*)A > *(Constant*)B)->getValue())
193 /// intersect - Return the range that results from the intersection of this
194 /// range with another range.
196 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
197 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
198 // Handle common special cases
199 if (isEmptySet() || CR.isFullSet()) return *this;
200 if (isFullSet() || CR.isEmptySet()) return CR;
202 if (!isWrappedSet()) {
203 if (!CR.isWrappedSet()) {
204 ConstantIntegral *L = Max(Lower, CR.Lower);
205 ConstantIntegral *U = Min(Upper, CR.Upper);
207 if ((*L < *U)->getValue()) // If range isn't empty...
208 return ConstantRange(L, U);
210 return ConstantRange(getType(), false); // Otherwise, return empty set
212 return intersect1Wrapped(CR, *this);
213 } else { // We know "this" is wrapped...
214 if (!CR.isWrappedSet())
215 return intersect1Wrapped(*this, CR);
217 // Both ranges are wrapped...
218 ConstantIntegral *L = Max(Lower, CR.Lower);
219 ConstantIntegral *U = Min(Upper, CR.Upper);
220 return ConstantRange(L, U);
226 /// union - Return the range that results from the union of this range with
227 /// another range. The resultant range is guaranteed to include the elements of
228 /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
229 /// 15), which includes 9, 10, and 11, which were not included in either set
232 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
233 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
235 assert(0 && "Range union not implemented yet!");
240 /// print - Print out the bounds to a stream...
242 void ConstantRange::print(std::ostream &OS) const {
243 OS << "[" << Lower << "," << Upper << " )";
246 /// dump - Allow printing from a debugger easily...
248 void ConstantRange::dump() const {