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