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 always keeps unsigned values.
00028 //===----------------------------------------------------------------------===//
00029 
00030 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
00031 #define LLVM_SUPPORT_CONSTANT_RANGE_H
00032 
00033 #include "llvm/ADT/APInt.h"
00034 #include "llvm/Support/DataTypes.h"
00035 
00036 namespace llvm {
00037 
00038 class ConstantRange {
00039   APInt Lower, Upper;
00040   static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
00041                                          const ConstantRange &RHS);
00042  public:
00043   /// Initialize a full (the default) or empty set for the specified bit width.
00044   ///
00045   explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
00046 
00047   /// Initialize a range to hold the single specified value.
00048   ///
00049   ConstantRange(const APInt &Value);
00050 
00051   /// @brief Initialize a range of values explicitly. This will assert out if
00052   /// Lower==Upper and Lower != Min or Max value for its type. It will also
00053   /// assert out if the two APInt's are not the same bit width.
00054   ConstantRange(const APInt& Lower, const APInt& Upper);
00055 
00056   /// getLower - Return the lower value for this range...
00057   ///
00058   const APInt &getLower() const { return Lower; }
00059 
00060   /// getUpper - Return the upper value for this range...
00061   ///
00062   const APInt &getUpper() const { return Upper; } 
00063 
00064   /// getBitWidth - get the bit width of this ConstantRange
00065   ///
00066   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
00067 
00068   /// isFullSet - Return true if this set contains all of the elements possible
00069   /// for this data-type
00070   ///
00071   bool isFullSet() const;
00072 
00073   /// isEmptySet - Return true if this set contains no members.
00074   ///
00075   bool isEmptySet() const;
00076 
00077   /// isWrappedSet - Return true if this set wraps around the top of the range,
00078   /// for example: [100, 8)
00079   ///
00080   bool isWrappedSet() const;
00081 
00082   /// contains - Return true if the specified value is in the set.
00083   ///
00084   bool contains(const APInt &Val) const;
00085 
00086   /// getSingleElement - If this set contains a single element, return it,
00087   /// otherwise return null.
00088   ///
00089   const APInt *getSingleElement() const {
00090     if (Upper == Lower + 1)
00091       return &Lower;
00092     return 0;
00093   }
00094 
00095   /// isSingleElement - Return true if this set contains exactly one member.
00096   ///
00097   bool isSingleElement() const { return getSingleElement() != 0; }
00098 
00099   /// getSetSize - Return the number of elements in this set.
00100   ///
00101   APInt getSetSize() const;
00102 
00103   /// getUnsignedMax - Return the largest unsigned value contained in the
00104   /// ConstantRange.
00105   ///
00106   APInt getUnsignedMax() const;
00107 
00108   /// getUnsignedMin - Return the smallest unsigned value contained in the
00109   /// ConstantRange.
00110   ///
00111   APInt getUnsignedMin() const;
00112 
00113   /// getSignedMax - Return the largest signed value contained in the
00114   /// ConstantRange.
00115   ///
00116   APInt getSignedMax() const;
00117 
00118   /// getSignedMin - Return the smallest signed value contained in the
00119   /// ConstantRange.
00120   ///
00121   APInt getSignedMin() const;
00122 
00123   /// operator== - Return true if this range is equal to another range.
00124   ///
00125   bool operator==(const ConstantRange &CR) const {
00126     return Lower == CR.Lower && Upper == CR.Upper;
00127   }
00128   bool operator!=(const ConstantRange &CR) const {
00129     return !operator==(CR);
00130   }
00131 
00132   /// subtract - Subtract the specified constant from the endpoints of this
00133   /// constant range.
00134   ConstantRange subtract(const APInt &CI) const;
00135 
00136   /// intersectWith - Return the range that results from the intersection of
00137   /// this range with another range.  The resultant range is pruned as much as
00138   /// possible, but there may be cases where elements are included that are in
00139   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
00140   /// 120) yields [3, 120)
00141   ///
00142   ConstantRange intersectWith(const ConstantRange &CR) const;
00143 
00144   /// maximalIntersectWith - Return the range that results from the intersection
00145   /// of this range with another range.  The resultant range is guaranteed to
00146   /// include all elements contained in both input ranges, and to have the
00147   /// smallest possible set size that does so.  Because there may be two
00148   /// intersections with the same set size, A.maximalIntersectWith(B) might not
00149   /// be equal to B.maximalIntersectWith(A).
00150   ///
00151   ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
00152 
00153   /// unionWith - Return the range that results from the union of this range
00154   /// with another range.  The resultant range is guaranteed to include the
00155   /// elements of both sets, but may contain more.  For example, [3, 9) union
00156   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
00157   /// in either set before.
00158   ///
00159   ConstantRange unionWith(const ConstantRange &CR) const;
00160 
00161   /// zeroExtend - Return a new range in the specified integer type, which must
00162   /// be strictly larger than the current type.  The returned range will
00163   /// correspond to the possible range of values if the source range had been
00164   /// zero extended to BitWidth.
00165   ConstantRange zeroExtend(uint32_t BitWidth) const;
00166 
00167   /// signExtend - Return a new range in the specified integer type, which must
00168   /// be strictly larger than the current type.  The returned range will
00169   /// correspond to the possible range of values if the source range had been
00170   /// sign extended to BitWidth.
00171   ConstantRange signExtend(uint32_t BitWidth) const;
00172 
00173   /// truncate - Return a new range in the specified integer type, which must be
00174   /// strictly smaller than the current type.  The returned range will
00175   /// correspond to the possible range of values if the source range had been
00176   /// truncated to the specified type.
00177   ConstantRange truncate(uint32_t BitWidth) const;
00178 
00179   /// print - Print out the bounds to a stream...
00180   ///
00181   void print(raw_ostream &OS) const;
00182 
00183   /// dump - Allow printing from a debugger easily...
00184   ///
00185   void dump() const;
00186 };
00187 
00188 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
00189   CR.print(OS);
00190   return OS;
00191 }
00192 
00193 } // End llvm namespace
00194 
00195 #endif