blob: 0067e21f757ab860efefcf0d7e3839a469314bdb [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"
Chris Lattner67bb7602004-01-12 20:13:04 +000025#include "llvm/Constants.h"
Chris Lattner645e00d2002-09-01 23:53:36 +000026#include "llvm/Instruction.h"
Chris Lattner67bb7602004-01-12 20:13:04 +000027#include "llvm/Type.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner67bb7602004-01-12 20:13:04 +000030static 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();
34}
35
36static 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();
40}
41
42static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
43 return LT(A, B) ? A : B;
44}
45static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
46 return GT(A, B) ? A : B;
47}
48
49
Chris Lattner645e00d2002-09-01 23:53:36 +000050/// Initialize a full (the default) or empty set for the specified type.
51///
52ConstantRange::ConstantRange(const Type *Ty, bool Full) {
53 assert(Ty->isIntegral() &&
54 "Cannot make constant range of non-integral type!");
55 if (Full)
56 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
57 else
58 Lower = Upper = ConstantIntegral::getMinValue(Ty);
59}
60
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)
64///
65ConstantRange::ConstantRange(ConstantIntegral *L,
66 ConstantIntegral *U) : Lower(L), Upper(U) {
67 assert(Lower->getType() == Upper->getType() &&
68 "Incompatible types for ConstantRange!");
69
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!");
74}
75
76static ConstantIntegral *Next(ConstantIntegral *CI) {
77 if (CI->getType() == Type::BoolTy)
78 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
79
Chris Lattner67bb7602004-01-12 20:13:04 +000080 Constant *Result = ConstantExpr::get(Instruction::Add, CI,
81 ConstantInt::get(CI->getType(), 1));
Chris Lattner645e00d2002-09-01 23:53:36 +000082 return cast<ConstantIntegral>(Result);
83}
84
85/// Initialize a set of values that all satisfy the condition with C.
86///
87ConstantRange::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());
94 Upper = C;
95 return;
96 case Instruction::SetGT:
Chris Lattner645e00d2002-09-01 23:53:36 +000097 Lower = Next(C);
Chris Lattner20d41292002-09-03 23:12:40 +000098 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +000099 return;
100 case Instruction::SetLE:
101 Lower = ConstantIntegral::getMinValue(C->getType());
102 Upper = Next(C);
103 return;
104 case Instruction::SetGE:
Chris Lattner645e00d2002-09-01 23:53:36 +0000105 Lower = C;
Chris Lattner20d41292002-09-03 23:12:40 +0000106 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000107 return;
108 }
109}
110
111/// getType - Return the LLVM data type of this range.
112///
113const Type *ConstantRange::getType() const { return Lower->getType(); }
114
115/// isFullSet - Return true if this set contains all of the elements possible
116/// for this data-type
117bool ConstantRange::isFullSet() const {
118 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
119}
120
121/// isEmptySet - Return true if this set contains no members.
122///
123bool ConstantRange::isEmptySet() const {
124 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
125}
126
127/// isWrappedSet - Return true if this set wraps around the top of the range,
128/// for example: [100, 8)
129///
130bool ConstantRange::isWrappedSet() const {
Chris Lattner67bb7602004-01-12 20:13:04 +0000131 return GT(Lower, Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000132}
133
134
135/// getSingleElement - If this set contains a single element, return it,
136/// otherwise return null.
137ConstantIntegral *ConstantRange::getSingleElement() const {
138 if (Upper == Next(Lower)) // Is it a single element range?
139 return Lower;
140 return 0;
141}
142
143/// getSetSize - Return the number of elements in this set.
144///
145uint64_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...
149 return 1;
150 return 2; // Must be full set...
151 }
152
153 // Simply subtract the bounds...
Chris Lattner67bb7602004-01-12 20:13:04 +0000154 Constant *Result =
155 ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower);
Chris Lattnerc07736a2003-07-23 15:22:26 +0000156 return cast<ConstantInt>(Result)->getRawValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000157}
158
159
160
161
162// intersect1Wrapped - This helper function is used to intersect two ranges when
163// it is known that LHS is wrapped and RHS isn't.
164//
165static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
166 const ConstantRange &RHS) {
167 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
168
Chris Lattner645e00d2002-09-01 23:53:36 +0000169 // Check to see if we overlap on the Left side of RHS...
170 //
Chris Lattner67bb7602004-01-12 20:13:04 +0000171 if (LT(RHS.getLower(), LHS.getUpper())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000172 // We do overlap on the left side of RHS, see if we overlap on the right of
173 // RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000174 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000175 // 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...
177 //
178 if (LHS.getSetSize() < RHS.getSetSize())
179 return LHS;
180 else
181 return RHS;
182
183 } else {
184 // No overlap on the right, just on the left.
185 return ConstantRange(RHS.getLower(), LHS.getUpper());
186 }
187
188 } else {
189 // We don't overlap on the left side of RHS, see if we overlap on the right
190 // of RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000191 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000192 // Simple overlap...
193 return ConstantRange(LHS.getLower(), RHS.getUpper());
194 } else {
195 // No overlap...
196 return ConstantRange(LHS.getType(), false);
197 }
198 }
199}
200
Chris Lattner645e00d2002-09-01 23:53:36 +0000201/// intersect - Return the range that results from the intersection of this
202/// range with another range.
203///
204ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
205 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000206 // Handle common special cases
207 if (isEmptySet() || CR.isFullSet()) return *this;
208 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000209
210 if (!isWrappedSet()) {
211 if (!CR.isWrappedSet()) {
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000212 ConstantIntegral *L = Max(Lower, CR.Lower);
213 ConstantIntegral *U = Min(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000214
Chris Lattner67bb7602004-01-12 20:13:04 +0000215 if (LT(L, U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000216 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000217 else
218 return ConstantRange(getType(), false); // Otherwise, return empty set
219 } else
220 return intersect1Wrapped(CR, *this);
221 } else { // We know "this" is wrapped...
222 if (!CR.isWrappedSet())
223 return intersect1Wrapped(*this, CR);
224 else {
225 // Both ranges are wrapped...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000226 ConstantIntegral *L = Max(Lower, CR.Lower);
227 ConstantIntegral *U = Min(Upper, CR.Upper);
228 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000229 }
230 }
231 return *this;
232}
233
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
238/// before.
239///
240ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
241 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
242
243 assert(0 && "Range union not implemented yet!");
244
245 return *this;
246}
Chris Lattner96f9d722002-09-02 00:18:22 +0000247
248/// print - Print out the bounds to a stream...
249///
250void ConstantRange::print(std::ostream &OS) const {
251 OS << "[" << Lower << "," << Upper << " )";
252}
253
254/// dump - Allow printing from a debugger easily...
255///
256void ConstantRange::dump() const {
257 print(std::cerr);
258}