blob: 7b45d20b5331d32408177ae4936c57d3ea1dbfa3 [file] [log] [blame]
Chris Lattner645e00d2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner645e00d2002-09-01 23:53:36 +00009//
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):
16//
17// [F, F) = {} = Empty set
18// [T, F) = {T}
19// [F, T) = {F}
20// [T, T) = {F, T} = Full set
21//
22//===----------------------------------------------------------------------===//
23
24#include "llvm/Support/ConstantRange.h"
25#include "llvm/Type.h"
26#include "llvm/Instruction.h"
27#include "llvm/ConstantHandling.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner645e00d2002-09-01 23:53:36 +000030/// Initialize a full (the default) or empty set for the specified type.
31///
32ConstantRange::ConstantRange(const Type *Ty, bool Full) {
33 assert(Ty->isIntegral() &&
34 "Cannot make constant range of non-integral type!");
35 if (Full)
36 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
37 else
38 Lower = Upper = ConstantIntegral::getMinValue(Ty);
39}
40
41/// Initialize a range of values explicitly... this will assert out if
42/// Lower==Upper and Lower != Min or Max for its type (or if the two constants
43/// have different types)
44///
45ConstantRange::ConstantRange(ConstantIntegral *L,
46 ConstantIntegral *U) : Lower(L), Upper(U) {
47 assert(Lower->getType() == Upper->getType() &&
48 "Incompatible types for ConstantRange!");
49
50 // Make sure that if L & U are equal that they are either Min or Max...
51 assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
52 L == ConstantIntegral::getMinValue(L->getType()))) &&
53 "Lower == Upper, but they aren't min or max for type!");
54}
55
56static ConstantIntegral *Next(ConstantIntegral *CI) {
57 if (CI->getType() == Type::BoolTy)
58 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
59
60 // Otherwise use operator+ in the ConstantHandling Library.
61 Constant *Result = *ConstantInt::get(CI->getType(), 1) + *CI;
62 assert(Result && "ConstantHandling not implemented for integral plus!?");
63 return cast<ConstantIntegral>(Result);
64}
65
66/// Initialize a set of values that all satisfy the condition with C.
67///
68ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
69 switch (SetCCOpcode) {
70 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
71 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
72 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
73 case Instruction::SetLT:
74 Lower = ConstantIntegral::getMinValue(C->getType());
75 Upper = C;
76 return;
77 case Instruction::SetGT:
Chris Lattner645e00d2002-09-01 23:53:36 +000078 Lower = Next(C);
Chris Lattner20d41292002-09-03 23:12:40 +000079 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +000080 return;
81 case Instruction::SetLE:
82 Lower = ConstantIntegral::getMinValue(C->getType());
83 Upper = Next(C);
84 return;
85 case Instruction::SetGE:
Chris Lattner645e00d2002-09-01 23:53:36 +000086 Lower = C;
Chris Lattner20d41292002-09-03 23:12:40 +000087 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +000088 return;
89 }
90}
91
92/// getType - Return the LLVM data type of this range.
93///
94const Type *ConstantRange::getType() const { return Lower->getType(); }
95
96/// isFullSet - Return true if this set contains all of the elements possible
97/// for this data-type
98bool ConstantRange::isFullSet() const {
99 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
100}
101
102/// isEmptySet - Return true if this set contains no members.
103///
104bool ConstantRange::isEmptySet() const {
105 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
106}
107
108/// isWrappedSet - Return true if this set wraps around the top of the range,
109/// for example: [100, 8)
110///
111bool ConstantRange::isWrappedSet() const {
112 return (*(Constant*)Lower > *(Constant*)Upper)->getValue();
113}
114
115
116/// getSingleElement - If this set contains a single element, return it,
117/// otherwise return null.
118ConstantIntegral *ConstantRange::getSingleElement() const {
119 if (Upper == Next(Lower)) // Is it a single element range?
120 return Lower;
121 return 0;
122}
123
124/// getSetSize - Return the number of elements in this set.
125///
126uint64_t ConstantRange::getSetSize() const {
127 if (isEmptySet()) return 0;
128 if (getType() == Type::BoolTy) {
129 if (Lower != Upper) // One of T or F in the set...
130 return 1;
131 return 2; // Must be full set...
132 }
133
134 // Simply subtract the bounds...
135 Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
136 assert(Result && "Subtraction of constant integers not implemented?");
Chris Lattnerc07736a2003-07-23 15:22:26 +0000137 return cast<ConstantInt>(Result)->getRawValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000138}
139
140
141
142
143// intersect1Wrapped - This helper function is used to intersect two ranges when
144// it is known that LHS is wrapped and RHS isn't.
145//
146static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
147 const ConstantRange &RHS) {
148 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
149
Chris Lattner645e00d2002-09-01 23:53:36 +0000150 // Check to see if we overlap on the Left side of RHS...
151 //
152 if ((*(Constant*)RHS.getLower() < *(Constant*)LHS.getUpper())->getValue()) {
153 // We do overlap on the left side of RHS, see if we overlap on the right of
154 // RHS...
155 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
156 // Ok, the result overlaps on both the left and right sides. See if the
157 // resultant interval will be smaller if we wrap or not...
158 //
159 if (LHS.getSetSize() < RHS.getSetSize())
160 return LHS;
161 else
162 return RHS;
163
164 } else {
165 // No overlap on the right, just on the left.
166 return ConstantRange(RHS.getLower(), LHS.getUpper());
167 }
168
169 } else {
170 // We don't overlap on the left side of RHS, see if we overlap on the right
171 // of RHS...
172 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
173 // Simple overlap...
174 return ConstantRange(LHS.getLower(), RHS.getUpper());
175 } else {
176 // No overlap...
177 return ConstantRange(LHS.getType(), false);
178 }
179 }
180}
181
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000182static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
183 if ((*(Constant*)A < *(Constant*)B)->getValue())
184 return A;
185 return B;
186}
187static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
188 if ((*(Constant*)A > *(Constant*)B)->getValue())
189 return A;
190 return B;
191}
192
Chris Lattner645e00d2002-09-01 23:53:36 +0000193
194/// intersect - Return the range that results from the intersection of this
195/// range with another range.
196///
197ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
198 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000199 // Handle common special cases
200 if (isEmptySet() || CR.isFullSet()) return *this;
201 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000202
203 if (!isWrappedSet()) {
204 if (!CR.isWrappedSet()) {
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000205 ConstantIntegral *L = Max(Lower, CR.Lower);
206 ConstantIntegral *U = Min(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000207
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000208 if ((*L < *U)->getValue()) // If range isn't empty...
209 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000210 else
211 return ConstantRange(getType(), false); // Otherwise, return empty set
212 } else
213 return intersect1Wrapped(CR, *this);
214 } else { // We know "this" is wrapped...
215 if (!CR.isWrappedSet())
216 return intersect1Wrapped(*this, CR);
217 else {
218 // Both ranges are wrapped...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000219 ConstantIntegral *L = Max(Lower, CR.Lower);
220 ConstantIntegral *U = Min(Upper, CR.Upper);
221 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000222 }
223 }
224 return *this;
225}
226
227/// union - Return the range that results from the union of this range with
228/// another range. The resultant range is guaranteed to include the elements of
229/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
230/// 15), which includes 9, 10, and 11, which were not included in either set
231/// before.
232///
233ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
234 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
235
236 assert(0 && "Range union not implemented yet!");
237
238 return *this;
239}
Chris Lattner96f9d722002-09-02 00:18:22 +0000240
241/// print - Print out the bounds to a stream...
242///
243void ConstantRange::print(std::ostream &OS) const {
244 OS << "[" << Lower << "," << Upper << " )";
245}
246
247/// dump - Allow printing from a debugger easily...
248///
249void ConstantRange::dump() const {
250 print(std::cerr);
251}