LLVM API Documentation

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