LLVM API Documentation

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_SUPPORT_CONSTANTRANGE_H
00033 #define LLVM_SUPPORT_CONSTANTRANGE_H
00034 
00035 #include "llvm/ADT/APInt.h"
00036 #include "llvm/Support/DataTypes.h"
00037 
00038 namespace llvm {
00039 
00040 /// ConstantRange - This class represents an range of values.
00041 ///
00042 class ConstantRange {
00043   APInt Lower, Upper;
00044 
00045   // If we have move semantics, pass APInts by value and move them into place.
00046   typedef APInt APIntMoveTy;
00047 
00048 public:
00049   /// Initialize a full (the default) or empty set for the specified bit width.
00050   ///
00051   explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
00052 
00053   /// Initialize a range to hold the single specified value.
00054   ///
00055   ConstantRange(APIntMoveTy Value);
00056 
00057   /// @brief Initialize a range of values explicitly. This will assert out if
00058   /// Lower==Upper and Lower != Min or Max value for its type. It will also
00059   /// assert out if the two APInt's are not the same bit width.
00060   ConstantRange(APIntMoveTy Lower, APIntMoveTy Upper);
00061 
00062   /// makeICmpRegion - Produce the smallest range that contains all values that
00063   /// might satisfy the comparison specified by Pred when compared to any value
00064   /// contained within Other.
00065   ///
00066   /// Solves for range X in 'for all x in X, there exists a y in Y such that
00067   /// icmp op x, y is true'. Every value that might make the comparison true
00068   /// is included in the resulting range.
00069   static ConstantRange makeICmpRegion(unsigned Pred,
00070                                       const ConstantRange &Other);
00071 
00072   /// getLower - Return the lower value for this range...
00073   ///
00074   const APInt &getLower() const { return Lower; }
00075 
00076   /// getUpper - Return the upper value for this range...
00077   ///
00078   const APInt &getUpper() const { return Upper; }
00079 
00080   /// getBitWidth - get the bit width of this ConstantRange
00081   ///
00082   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
00083 
00084   /// isFullSet - Return true if this set contains all of the elements possible
00085   /// for this data-type
00086   ///
00087   bool isFullSet() const;
00088 
00089   /// isEmptySet - Return true if this set contains no members.
00090   ///
00091   bool isEmptySet() const;
00092 
00093   /// isWrappedSet - Return true if this set wraps around the top of the range,
00094   /// for example: [100, 8)
00095   ///
00096   bool isWrappedSet() const;
00097 
00098   /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
00099   /// its bitwidth, for example: i8 [120, 140).
00100   ///
00101   bool isSignWrappedSet() const;
00102 
00103   /// contains - Return true if the specified value is in the set.
00104   ///
00105   bool contains(const APInt &Val) const;
00106 
00107   /// contains - Return true if the other range is a subset of this one.
00108   ///
00109   bool contains(const ConstantRange &CR) const;
00110 
00111   /// getSingleElement - If this set contains a single element, return it,
00112   /// otherwise return null.
00113   ///
00114   const APInt *getSingleElement() const {
00115     if (Upper == Lower + 1)
00116       return &Lower;
00117     return nullptr;
00118   }
00119 
00120   /// isSingleElement - Return true if this set contains exactly one member.
00121   ///
00122   bool isSingleElement() const { return getSingleElement() != nullptr; }
00123 
00124   /// getSetSize - Return the number of elements in this set.
00125   ///
00126   APInt getSetSize() const;
00127 
00128   /// getUnsignedMax - Return the largest unsigned value contained in the
00129   /// ConstantRange.
00130   ///
00131   APInt getUnsignedMax() const;
00132 
00133   /// getUnsignedMin - Return the smallest unsigned value contained in the
00134   /// ConstantRange.
00135   ///
00136   APInt getUnsignedMin() const;
00137 
00138   /// getSignedMax - Return the largest signed value contained in the
00139   /// ConstantRange.
00140   ///
00141   APInt getSignedMax() const;
00142 
00143   /// getSignedMin - Return the smallest signed value contained in the
00144   /// ConstantRange.
00145   ///
00146   APInt getSignedMin() const;
00147 
00148   /// operator== - Return true if this range is equal to another range.
00149   ///
00150   bool operator==(const ConstantRange &CR) const {
00151     return Lower == CR.Lower && Upper == CR.Upper;
00152   }
00153   bool operator!=(const ConstantRange &CR) const {
00154     return !operator==(CR);
00155   }
00156 
00157   /// subtract - Subtract the specified constant from the endpoints of this
00158   /// constant range.
00159   ConstantRange subtract(const APInt &CI) const;
00160 
00161   /// \brief Subtract the specified range from this range (aka relative
00162   /// complement of the sets).
00163   ConstantRange difference(const ConstantRange &CR) const;
00164 
00165   /// intersectWith - Return the range that results from the intersection of
00166   /// this range with another range.  The resultant range is guaranteed to
00167   /// include all elements contained in both input ranges, and to have the
00168   /// smallest possible set size that does so.  Because there may be two
00169   /// intersections with the same set size, A.intersectWith(B) might not
00170   /// be equal to B.intersectWith(A).
00171   ///
00172   ConstantRange intersectWith(const ConstantRange &CR) const;
00173 
00174   /// unionWith - Return the range that results from the union of this range
00175   /// with another range.  The resultant range is guaranteed to include the
00176   /// elements of both sets, but may contain more.  For example, [3, 9) union
00177   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
00178   /// in either set before.
00179   ///
00180   ConstantRange unionWith(const ConstantRange &CR) const;
00181 
00182   /// zeroExtend - Return a new range in the specified integer type, which must
00183   /// be strictly larger than the current type.  The returned range will
00184   /// correspond to the possible range of values if the source range had been
00185   /// zero extended to BitWidth.
00186   ConstantRange zeroExtend(uint32_t BitWidth) const;
00187 
00188   /// signExtend - Return a new range in the specified integer type, which must
00189   /// be strictly larger than the current type.  The returned range will
00190   /// correspond to the possible range of values if the source range had been
00191   /// sign extended to BitWidth.
00192   ConstantRange signExtend(uint32_t BitWidth) const;
00193 
00194   /// truncate - Return a new range in the specified integer type, which must be
00195   /// strictly smaller than the current type.  The returned range will
00196   /// correspond to the possible range of values if the source range had been
00197   /// truncated to the specified type.
00198   ConstantRange truncate(uint32_t BitWidth) const;
00199 
00200   /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The
00201   /// value is zero extended, truncated, or left alone to make it that width.
00202   ConstantRange zextOrTrunc(uint32_t BitWidth) const;
00203   
00204   /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The
00205   /// value is sign extended, truncated, or left alone to make it that width.
00206   ConstantRange sextOrTrunc(uint32_t BitWidth) const;
00207 
00208   /// add - Return a new range representing the possible values resulting
00209   /// from an addition of a value in this range and a value in \p Other.
00210   ConstantRange add(const ConstantRange &Other) const;
00211 
00212   /// sub - Return a new range representing the possible values resulting
00213   /// from a subtraction of a value in this range and a value in \p Other.
00214   ConstantRange sub(const ConstantRange &Other) const;
00215 
00216   /// multiply - Return a new range representing the possible values resulting
00217   /// from a multiplication of a value in this range and a value in \p Other.
00218   /// TODO: This isn't fully implemented yet.
00219   ConstantRange multiply(const ConstantRange &Other) const;
00220 
00221   /// smax - Return a new range representing the possible values resulting
00222   /// from a signed maximum of a value in this range and a value in \p Other.
00223   ConstantRange smax(const ConstantRange &Other) const;
00224 
00225   /// umax - Return a new range representing the possible values resulting
00226   /// from an unsigned maximum of a value in this range and a value in \p Other.
00227   ConstantRange umax(const ConstantRange &Other) const;
00228 
00229   /// udiv - Return a new range representing the possible values resulting
00230   /// from an unsigned division of a value in this range and a value in
00231   /// \p Other.
00232   ConstantRange udiv(const ConstantRange &Other) const;
00233 
00234   /// binaryAnd - return a new range representing the possible values resulting
00235   /// from a binary-and of a value in this range by a value in \p Other.
00236   ConstantRange binaryAnd(const ConstantRange &Other) const;
00237 
00238   /// binaryOr - return a new range representing the possible values resulting
00239   /// from a binary-or of a value in this range by a value in \p Other.
00240   ConstantRange binaryOr(const ConstantRange &Other) const;
00241 
00242   /// shl - Return a new range representing the possible values resulting
00243   /// from a left shift of a value in this range by a value in \p Other.
00244   /// TODO: This isn't fully implemented yet.
00245   ConstantRange shl(const ConstantRange &Other) const;
00246 
00247   /// lshr - Return a new range representing the possible values resulting
00248   /// from a logical right shift of a value in this range and a value in
00249   /// \p Other.
00250   ConstantRange lshr(const ConstantRange &Other) const;
00251 
00252   /// inverse - Return a new range that is the logical not of the current set.
00253   ///
00254   ConstantRange inverse() const;
00255   
00256   /// print - Print out the bounds to a stream...
00257   ///
00258   void print(raw_ostream &OS) const;
00259 
00260   /// dump - Allow printing from a debugger easily...
00261   ///
00262   void dump() const;
00263 };
00264 
00265 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
00266   CR.print(OS);
00267   return OS;
00268 }
00269 
00270 } // End llvm namespace
00271 
00272 #endif