ConstantRange.h revision 9850a288664087875c7c35bec8c92b72a72ac142
1//===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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: : 16// 17// [F, F) = {} = Empty set 18// [T, F) = {T} 19// [F, T) = {F} 20// [T, T) = {F, T} = Full set 21// 22// The other integral ranges use min/max values for special range values. For 23// example, for 8-bit types, it uses: 24// [0, 0) = {} = Empty set 25// [255, 255) = {0..255} = Full Set 26// 27// Note that ConstantRange can be used to represent either signed or 28// unsigned ranges. 29// 30//===----------------------------------------------------------------------===// 31 32#ifndef LLVM_SUPPORT_CONSTANT_RANGE_H 33#define LLVM_SUPPORT_CONSTANT_RANGE_H 34 35#include "llvm/ADT/APInt.h" 36#include "llvm/System/DataTypes.h" 37 38namespace llvm { 39 40/// ConstantRange - This class represents an range of values. 41/// 42class ConstantRange { 43 APInt Lower, Upper; 44 static ConstantRange intersect1Wrapped(const ConstantRange &LHS, 45 const ConstantRange &RHS); 46 47public: 48 /// Initialize a full (the default) or empty set for the specified bit width. 49 /// 50 explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true); 51 52 /// Initialize a range to hold the single specified value. 53 /// 54 ConstantRange(const APInt &Value); 55 56 /// @brief Initialize a range of values explicitly. This will assert out if 57 /// Lower==Upper and Lower != Min or Max value for its type. It will also 58 /// assert out if the two APInt's are not the same bit width. 59 ConstantRange(const APInt& Lower, const APInt& Upper); 60 61 /// makeICmpRegion - Produce the smallest range that contains all values that 62 /// might satisfy the comparison specified by Pred when compared to any value 63 /// contained within Other. 64 /// 65 /// Solves for range X in 'for all x in X, there exists a y in Y such that 66 /// icmp op x, y is true'. Every value that might make the comparison true 67 /// is included in the resulting range. 68 static ConstantRange makeICmpRegion(unsigned Pred, 69 const ConstantRange &Other); 70 71 /// getLower - Return the lower value for this range... 72 /// 73 const APInt &getLower() const { return Lower; } 74 75 /// getUpper - Return the upper value for this range... 76 /// 77 const APInt &getUpper() const { return Upper; } 78 79 /// getBitWidth - get the bit width of this ConstantRange 80 /// 81 uint32_t getBitWidth() const { return Lower.getBitWidth(); } 82 83 /// isFullSet - Return true if this set contains all of the elements possible 84 /// for this data-type 85 /// 86 bool isFullSet() const; 87 88 /// isEmptySet - Return true if this set contains no members. 89 /// 90 bool isEmptySet() const; 91 92 /// isWrappedSet - Return true if this set wraps around the top of the range, 93 /// for example: [100, 8) 94 /// 95 bool isWrappedSet() const; 96 97 /// contains - Return true if the specified value is in the set. 98 /// 99 bool contains(const APInt &Val) const; 100 101 /// contains - Return true if the other range is a subset of this one. 102 /// 103 bool contains(const ConstantRange &CR) const; 104 105 /// getSingleElement - If this set contains a single element, return it, 106 /// otherwise return null. 107 /// 108 const APInt *getSingleElement() const { 109 if (Upper == Lower + 1) 110 return &Lower; 111 return 0; 112 } 113 114 /// isSingleElement - Return true if this set contains exactly one member. 115 /// 116 bool isSingleElement() const { return getSingleElement() != 0; } 117 118 /// getSetSize - Return the number of elements in this set. 119 /// 120 APInt getSetSize() const; 121 122 /// getUnsignedMax - Return the largest unsigned value contained in the 123 /// ConstantRange. 124 /// 125 APInt getUnsignedMax() const; 126 127 /// getUnsignedMin - Return the smallest unsigned value contained in the 128 /// ConstantRange. 129 /// 130 APInt getUnsignedMin() const; 131 132 /// getSignedMax - Return the largest signed value contained in the 133 /// ConstantRange. 134 /// 135 APInt getSignedMax() const; 136 137 /// getSignedMin - Return the smallest signed value contained in the 138 /// ConstantRange. 139 /// 140 APInt getSignedMin() const; 141 142 /// operator= - Copy one ConstantRange over another. 143 void operator=(const ConstantRange &CR) { 144 Lower = CR.Lower; 145 Upper = CR.Upper; 146 } 147 148 /// operator== - Return true if this range is equal to another range. 149 /// 150 bool operator==(const ConstantRange &CR) const { 151 return Lower == CR.Lower && Upper == CR.Upper; 152 } 153 bool operator!=(const ConstantRange &CR) const { 154 return !operator==(CR); 155 } 156 157 /// subtract - Subtract the specified constant from the endpoints of this 158 /// constant range. 159 ConstantRange subtract(const APInt &CI) const; 160 161 /// intersectWith - Return the range that results from the intersection of 162 /// this range with another range. The resultant range is guaranteed to 163 /// include all elements contained in both input ranges, and to have the 164 /// smallest possible set size that does so. Because there may be two 165 /// intersections with the same set size, A.intersectWith(B) might not 166 /// be equal to B.intersectWith(A). 167 /// 168 ConstantRange intersectWith(const ConstantRange &CR) const; 169 170 /// unionWith - Return the range that results from the union of this range 171 /// with another range. The resultant range is guaranteed to include the 172 /// elements of both sets, but may contain more. For example, [3, 9) union 173 /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included 174 /// in either set before. 175 /// 176 ConstantRange unionWith(const ConstantRange &CR) const; 177 178 /// zeroExtend - Return a new range in the specified integer type, which must 179 /// be strictly larger than the current type. The returned range will 180 /// correspond to the possible range of values if the source range had been 181 /// zero extended to BitWidth. 182 ConstantRange zeroExtend(uint32_t BitWidth) const; 183 184 /// signExtend - Return a new range in the specified integer type, which must 185 /// be strictly larger than the current type. The returned range will 186 /// correspond to the possible range of values if the source range had been 187 /// sign extended to BitWidth. 188 ConstantRange signExtend(uint32_t BitWidth) const; 189 190 /// truncate - Return a new range in the specified integer type, which must be 191 /// strictly smaller than the current type. The returned range will 192 /// correspond to the possible range of values if the source range had been 193 /// truncated to the specified type. 194 ConstantRange truncate(uint32_t BitWidth) const; 195 196 /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The 197 /// value is zero extended, truncated, or left alone to make it that width. 198 ConstantRange zextOrTrunc(uint32_t BitWidth) const; 199 200 /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The 201 /// value is sign extended, truncated, or left alone to make it that width. 202 ConstantRange sextOrTrunc(uint32_t BitWidth) const; 203 204 /// add - Return a new range representing the possible values resulting 205 /// from an addition of a value in this range and a value in Other. 206 ConstantRange add(const ConstantRange &Other) const; 207 208 /// multiply - Return a new range representing the possible values resulting 209 /// from a multiplication of a value in this range and a value in Other. 210 /// TODO: This isn't fully implemented yet. 211 ConstantRange multiply(const ConstantRange &Other) const; 212 213 /// smax - Return a new range representing the possible values resulting 214 /// from a signed maximum of a value in this range and a value in Other. 215 ConstantRange smax(const ConstantRange &Other) const; 216 217 /// umax - Return a new range representing the possible values resulting 218 /// from an unsigned maximum of a value in this range and a value in Other. 219 ConstantRange umax(const ConstantRange &Other) const; 220 221 /// udiv - Return a new range representing the possible values resulting 222 /// from an unsigned division of a value in this range and a value in Other. 223 /// TODO: This isn't fully implemented yet. 224 ConstantRange udiv(const ConstantRange &Other) const; 225 226 /// shl - Return a new range representing the possible values resulting 227 /// from a left shift of a value in this range by the Amount value. 228 ConstantRange shl(const ConstantRange &Amount) const; 229 230 /// ashr - Return a new range representing the possible values resulting from 231 /// an arithmetic right shift of a value in this range by the Amount value. 232 ConstantRange ashr(const ConstantRange &Amount) const; 233 234 /// shr - Return a new range representing the possible values resulting 235 /// from a logical right shift of a value in this range by the Amount value. 236 ConstantRange lshr(const ConstantRange &Amount) const; 237 238 /// print - Print out the bounds to a stream... 239 /// 240 void print(raw_ostream &OS) const; 241 242 /// dump - Allow printing from a debugger easily... 243 /// 244 void dump() const; 245}; 246 247inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) { 248 CR.print(OS); 249 return OS; 250} 251 252} // End llvm namespace 253 254#endif 255