blob: a9e1204de52db2999ff1cb2422e5d2dde56cebaf [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"
28
29/// Initialize a full (the default) or empty set for the specified type.
30///
31ConstantRange::ConstantRange(const Type *Ty, bool Full) {
32 assert(Ty->isIntegral() &&
33 "Cannot make constant range of non-integral type!");
34 if (Full)
35 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
36 else
37 Lower = Upper = ConstantIntegral::getMinValue(Ty);
38}
39
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)
43///
44ConstantRange::ConstantRange(ConstantIntegral *L,
45 ConstantIntegral *U) : Lower(L), Upper(U) {
46 assert(Lower->getType() == Upper->getType() &&
47 "Incompatible types for ConstantRange!");
48
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!");
53}
54
55static ConstantIntegral *Next(ConstantIntegral *CI) {
56 if (CI->getType() == Type::BoolTy)
57 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
58
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);
63}
64
65/// Initialize a set of values that all satisfy the condition with C.
66///
67ConstantRange::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());
74 Upper = C;
75 return;
76 case Instruction::SetGT:
Chris Lattner645e00d2002-09-01 23:53:36 +000077 Lower = Next(C);
Chris Lattner20d41292002-09-03 23:12:40 +000078 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +000079 return;
80 case Instruction::SetLE:
81 Lower = ConstantIntegral::getMinValue(C->getType());
82 Upper = Next(C);
83 return;
84 case Instruction::SetGE:
Chris Lattner645e00d2002-09-01 23:53:36 +000085 Lower = C;
Chris Lattner20d41292002-09-03 23:12:40 +000086 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +000087 return;
88 }
89}
90
91/// getType - Return the LLVM data type of this range.
92///
93const Type *ConstantRange::getType() const { return Lower->getType(); }
94
95/// isFullSet - Return true if this set contains all of the elements possible
96/// for this data-type
97bool ConstantRange::isFullSet() const {
98 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
99}
100
101/// isEmptySet - Return true if this set contains no members.
102///
103bool ConstantRange::isEmptySet() const {
104 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
105}
106
107/// isWrappedSet - Return true if this set wraps around the top of the range,
108/// for example: [100, 8)
109///
110bool ConstantRange::isWrappedSet() const {
111 return (*(Constant*)Lower > *(Constant*)Upper)->getValue();
112}
113
114
115/// getSingleElement - If this set contains a single element, return it,
116/// otherwise return null.
117ConstantIntegral *ConstantRange::getSingleElement() const {
118 if (Upper == Next(Lower)) // Is it a single element range?
119 return Lower;
120 return 0;
121}
122
123/// getSetSize - Return the number of elements in this set.
124///
125uint64_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...
129 return 1;
130 return 2; // Must be full set...
131 }
132
133 // Simply subtract the bounds...
134 Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
135 assert(Result && "Subtraction of constant integers not implemented?");
Chris Lattnerc07736a2003-07-23 15:22:26 +0000136 return cast<ConstantInt>(Result)->getRawValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000137}
138
139
140
141
142// intersect1Wrapped - This helper function is used to intersect two ranges when
143// it is known that LHS is wrapped and RHS isn't.
144//
145static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
146 const ConstantRange &RHS) {
147 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
148
Chris Lattner645e00d2002-09-01 23:53:36 +0000149 // Check to see if we overlap on the Left side of RHS...
150 //
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
153 // RHS...
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...
157 //
158 if (LHS.getSetSize() < RHS.getSetSize())
159 return LHS;
160 else
161 return RHS;
162
163 } else {
164 // No overlap on the right, just on the left.
165 return ConstantRange(RHS.getLower(), LHS.getUpper());
166 }
167
168 } else {
169 // We don't overlap on the left side of RHS, see if we overlap on the right
170 // of RHS...
171 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
172 // Simple overlap...
173 return ConstantRange(LHS.getLower(), RHS.getUpper());
174 } else {
175 // No overlap...
176 return ConstantRange(LHS.getType(), false);
177 }
178 }
179}
180
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000181static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
182 if ((*(Constant*)A < *(Constant*)B)->getValue())
183 return A;
184 return B;
185}
186static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
187 if ((*(Constant*)A > *(Constant*)B)->getValue())
188 return A;
189 return B;
190}
191
Chris Lattner645e00d2002-09-01 23:53:36 +0000192
193/// intersect - Return the range that results from the intersection of this
194/// range with another range.
195///
196ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
197 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000198 // Handle common special cases
199 if (isEmptySet() || CR.isFullSet()) return *this;
200 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000201
202 if (!isWrappedSet()) {
203 if (!CR.isWrappedSet()) {
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000204 ConstantIntegral *L = Max(Lower, CR.Lower);
205 ConstantIntegral *U = Min(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000206
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000207 if ((*L < *U)->getValue()) // If range isn't empty...
208 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000209 else
210 return ConstantRange(getType(), false); // Otherwise, return empty set
211 } else
212 return intersect1Wrapped(CR, *this);
213 } else { // We know "this" is wrapped...
214 if (!CR.isWrappedSet())
215 return intersect1Wrapped(*this, CR);
216 else {
217 // Both ranges are wrapped...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000218 ConstantIntegral *L = Max(Lower, CR.Lower);
219 ConstantIntegral *U = Min(Upper, CR.Upper);
220 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000221 }
222 }
223 return *this;
224}
225
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
230/// before.
231///
232ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
233 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
234
235 assert(0 && "Range union not implemented yet!");
236
237 return *this;
238}
Chris Lattner96f9d722002-09-02 00:18:22 +0000239
240/// print - Print out the bounds to a stream...
241///
242void ConstantRange::print(std::ostream &OS) const {
243 OS << "[" << Lower << "," << Upper << " )";
244}
245
246/// dump - Allow printing from a debugger easily...
247///
248void ConstantRange::dump() const {
249 print(std::cerr);
250}