LLVM  mainline
ConstantRange.h
Go to the documentation of this file.
00001 //===- ConstantRange.h - Represent a range ----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Represent a range of possible values that may occur when the program is run
00011 // for an integral value.  This keeps track of a lower and upper bound for the
00012 // constant, which MAY wrap around the end of the numeric range.  To do this, it
00013 // keeps track of a [lower, upper) bound, which specifies an interval just like
00014 // STL iterators.  When used with boolean values, the following are important
00015 // ranges: :
00016 //
00017 //  [F, F) = {}     = Empty set
00018 //  [T, F) = {T}
00019 //  [F, T) = {F}
00020 //  [T, T) = {F, T} = Full set
00021 //
00022 // The other integral ranges use min/max values for special range values. For
00023 // example, for 8-bit types, it uses:
00024 // [0, 0)     = {}       = Empty set
00025 // [255, 255) = {0..255} = Full Set
00026 //
00027 // Note that ConstantRange can be used to represent either signed or
00028 // unsigned ranges.
00029 //
00030 //===----------------------------------------------------------------------===//
00031 
00032 #ifndef LLVM_IR_CONSTANTRANGE_H
00033 #define LLVM_IR_CONSTANTRANGE_H
00034 
00035 #include "llvm/ADT/APInt.h"
00036 #include "llvm/IR/InstrTypes.h"
00037 #include "llvm/Support/DataTypes.h"
00038 
00039 namespace llvm {
00040 
00041 /// This class represents a range of values.
00042 ///
00043 class ConstantRange {
00044   APInt Lower, Upper;
00045 
00046   // If we have move semantics, pass APInts by value and move them into place.
00047   typedef APInt APIntMoveTy;
00048 
00049 public:
00050   /// Initialize a full (the default) or empty set for the specified bit width.
00051   ///
00052   explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
00053 
00054   /// Initialize a range to hold the single specified value.
00055   ///
00056   ConstantRange(APIntMoveTy Value);
00057 
00058   /// @brief Initialize a range of values explicitly. This will assert out if
00059   /// Lower==Upper and Lower != Min or Max value for its type. It will also
00060   /// assert out if the two APInt's are not the same bit width.
00061   ConstantRange(APIntMoveTy Lower, APIntMoveTy Upper);
00062 
00063   /// Produce the smallest range such that all values that may satisfy the given
00064   /// predicate with any value contained within Other is contained in the
00065   /// returned range.  Formally, this returns a superset of
00066   /// 'union over all y in Other . { x : icmp op x y is true }'.  If the exact
00067   /// answer is not representable as a ConstantRange, the return value will be a
00068   /// proper superset of the above.
00069   ///
00070   /// Example: Pred = ult and Other = i8 [2, 5) returns Result = [0, 4)
00071   static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred,
00072                                              const ConstantRange &Other);
00073 
00074   /// Produce the largest range such that all values in the returned range
00075   /// satisfy the given predicate with all values contained within Other.
00076   /// Formally, this returns a subset of
00077   /// 'intersection over all y in Other . { x : icmp op x y is true }'.  If the
00078   /// exact answer is not representable as a ConstantRange, the return value
00079   /// will be a proper subset of the above.
00080   ///
00081   /// Example: Pred = ult and Other = i8 [2, 5) returns [0, 2)
00082   static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
00083                                                 const ConstantRange &Other);
00084 
00085   /// Return the largest range containing all X such that "X BinOpC C" does not
00086   /// wrap (overflow).
00087   ///
00088   /// Example:
00089   ///  typedef OverflowingBinaryOperator OBO;
00090   ///  makeNoWrapRegion(Add, i8 1, OBO::NoSignedWrap) == [-128, 127)
00091   ///  makeNoWrapRegion(Add, i8 1, OBO::NoUnsignedWrap) == [0, -1)
00092   ///  makeNoWrapRegion(Add, i8 0, OBO::NoUnsignedWrap) == Full Set
00093   static ConstantRange makeNoWrapRegion(Instruction::BinaryOps BinOp,
00094                                         const APInt &C, unsigned NoWrapKind);
00095 
00096   /// Return the lower value for this range.
00097   ///
00098   const APInt &getLower() const { return Lower; }
00099 
00100   /// Return the upper value for this range.
00101   ///
00102   const APInt &getUpper() const { return Upper; }
00103 
00104   /// Get the bit width of this ConstantRange.
00105   ///
00106   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
00107 
00108   /// Return true if this set contains all of the elements possible
00109   /// for this data-type.
00110   ///
00111   bool isFullSet() const;
00112 
00113   /// Return true if this set contains no members.
00114   ///
00115   bool isEmptySet() const;
00116 
00117   /// Return true if this set wraps around the top of the range.
00118   /// For example: [100, 8).
00119   ///
00120   bool isWrappedSet() const;
00121 
00122   /// Return true if this set wraps around the INT_MIN of
00123   /// its bitwidth. For example: i8 [120, 140).
00124   ///
00125   bool isSignWrappedSet() const;
00126 
00127   /// Return true if the specified value is in the set.
00128   ///
00129   bool contains(const APInt &Val) const;
00130 
00131   /// Return true if the other range is a subset of this one.
00132   ///
00133   bool contains(const ConstantRange &CR) const;
00134 
00135   /// If this set contains a single element, return it, otherwise return null.
00136   ///
00137   const APInt *getSingleElement() const {
00138     if (Upper == Lower + 1)
00139       return &Lower;
00140     return nullptr;
00141   }
00142 
00143   /// Return true if this set contains exactly one member.
00144   ///
00145   bool isSingleElement() const { return getSingleElement() != nullptr; }
00146 
00147   /// Return the number of elements in this set.
00148   ///
00149   APInt getSetSize() const;
00150 
00151   /// Return the largest unsigned value contained in the ConstantRange.
00152   ///
00153   APInt getUnsignedMax() const;
00154 
00155   /// Return the smallest unsigned value contained in the ConstantRange.
00156   ///
00157   APInt getUnsignedMin() const;
00158 
00159   /// Return the largest signed value contained in the ConstantRange.
00160   ///
00161   APInt getSignedMax() const;
00162 
00163   /// Return the smallest signed value contained in the ConstantRange.
00164   ///
00165   APInt getSignedMin() const;
00166 
00167   /// Return true if this range is equal to another range.
00168   ///
00169   bool operator==(const ConstantRange &CR) const {
00170     return Lower == CR.Lower && Upper == CR.Upper;
00171   }
00172   bool operator!=(const ConstantRange &CR) const {
00173     return !operator==(CR);
00174   }
00175 
00176   /// Subtract the specified constant from the endpoints of this constant range.
00177   ConstantRange subtract(const APInt &CI) const;
00178 
00179   /// \brief Subtract the specified range from this range (aka relative
00180   /// complement of the sets).
00181   ConstantRange difference(const ConstantRange &CR) const;
00182 
00183   /// Return the range that results from the intersection of
00184   /// this range with another range.  The resultant range is guaranteed to
00185   /// include all elements contained in both input ranges, and to have the
00186   /// smallest possible set size that does so.  Because there may be two
00187   /// intersections with the same set size, A.intersectWith(B) might not
00188   /// be equal to B.intersectWith(A).
00189   ///
00190   ConstantRange intersectWith(const ConstantRange &CR) const;
00191 
00192   /// Return the range that results from the union of this range
00193   /// with another range.  The resultant range is guaranteed to include the
00194   /// elements of both sets, but may contain more.  For example, [3, 9) union
00195   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
00196   /// in either set before.
00197   ///
00198   ConstantRange unionWith(const ConstantRange &CR) const;
00199 
00200   /// Return a new range in the specified integer type, which must
00201   /// be strictly larger than the current type.  The returned range will
00202   /// correspond to the possible range of values if the source range had been
00203   /// zero extended to BitWidth.
00204   ConstantRange zeroExtend(uint32_t BitWidth) const;
00205 
00206   /// Return a new range in the specified integer type, which must
00207   /// be strictly larger than the current type.  The returned range will
00208   /// correspond to the possible range of values if the source range had been
00209   /// sign extended to BitWidth.
00210   ConstantRange signExtend(uint32_t BitWidth) const;
00211 
00212   /// Return a new range in the specified integer type, which must be
00213   /// strictly smaller than the current type.  The returned range will
00214   /// correspond to the possible range of values if the source range had been
00215   /// truncated to the specified type.
00216   ConstantRange truncate(uint32_t BitWidth) const;
00217 
00218   /// Make this range have the bit width given by \p BitWidth. The
00219   /// value is zero extended, truncated, or left alone to make it that width.
00220   ConstantRange zextOrTrunc(uint32_t BitWidth) const;
00221 
00222   /// Make this range have the bit width given by \p BitWidth. The
00223   /// value is sign extended, truncated, or left alone to make it that width.
00224   ConstantRange sextOrTrunc(uint32_t BitWidth) const;
00225 
00226   /// Return a new range representing the possible values resulting
00227   /// from an addition of a value in this range and a value in \p Other.
00228   ConstantRange add(const ConstantRange &Other) const;
00229 
00230   /// Return a new range representing the possible values resulting
00231   /// from a subtraction of a value in this range and a value in \p Other.
00232   ConstantRange sub(const ConstantRange &Other) const;
00233 
00234   /// Return a new range representing the possible values resulting
00235   /// from a multiplication of a value in this range and a value in \p Other,
00236   /// treating both this and \p Other as unsigned ranges.
00237   ConstantRange multiply(const ConstantRange &Other) const;
00238 
00239   /// Return a new range representing the possible values resulting
00240   /// from a signed maximum of a value in this range and a value in \p Other.
00241   ConstantRange smax(const ConstantRange &Other) const;
00242 
00243   /// Return a new range representing the possible values resulting
00244   /// from an unsigned maximum of a value in this range and a value in \p Other.
00245   ConstantRange umax(const ConstantRange &Other) const;
00246 
00247   /// Return a new range representing the possible values resulting
00248   /// from an unsigned division of a value in this range and a value in
00249   /// \p Other.
00250   ConstantRange udiv(const ConstantRange &Other) const;
00251 
00252   /// Return a new range representing the possible values resulting
00253   /// from a binary-and of a value in this range by a value in \p Other.
00254   ConstantRange binaryAnd(const ConstantRange &Other) const;
00255 
00256   /// Return a new range representing the possible values resulting
00257   /// from a binary-or of a value in this range by a value in \p Other.
00258   ConstantRange binaryOr(const ConstantRange &Other) const;
00259 
00260   /// Return a new range representing the possible values resulting
00261   /// from a left shift of a value in this range by a value in \p Other.
00262   /// TODO: This isn't fully implemented yet.
00263   ConstantRange shl(const ConstantRange &Other) const;
00264 
00265   /// Return a new range representing the possible values resulting from a
00266   /// logical right shift of a value in this range and a value in \p Other.
00267   ConstantRange lshr(const ConstantRange &Other) const;
00268 
00269   /// Return a new range that is the logical not of the current set.
00270   ///
00271   ConstantRange inverse() const;
00272 
00273   /// Print out the bounds to a stream.
00274   ///
00275   void print(raw_ostream &OS) const;
00276 
00277   /// Allow printing from a debugger easily.
00278   ///
00279   void dump() const;
00280 };
00281 
00282 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
00283   CR.print(OS);
00284   return OS;
00285 }
00286 
00287 } // End llvm namespace
00288 
00289 #endif