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