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