Implement operator== and != for ranges
[oota-llvm.git] / include / llvm / Support / ConstantRange.h
1 //===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
2 //
3 // Represent a range of possible values that may occur when the program is run
4 // for an integral value.  This keeps track of a lower and upper bound for the
5 // constant, which MAY wrap around the end of the numeric range.  To do this, it
6 // keeps track of a [lower, upper) bound, which specifies an interval just like
7 // STL iterators.  When used with boolean values, the following are important
8 // ranges (other integral ranges use min/max values for special range values):
9 //
10 //  [F, F) = {}     = Empty set
11 //  [T, F) = {T}
12 //  [F, T) = {F}
13 //  [T, T) = {F, T} = Full set
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
18 #define LLVM_SUPPORT_CONSTANT_RANGE_H
19
20 #include "Support/DataTypes.h"
21 #include <iosfwd>
22 class ConstantIntegral;
23 class Type;
24
25 class ConstantRange {
26   ConstantIntegral *Lower, *Upper;
27  public:
28   /// Initialize a full (the default) or empty set for the specified type.
29   ///
30   ConstantRange(const Type *Ty, bool isFullSet = true);
31   
32   /// Initialize a range of values explicitly... this will assert out if
33   /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
34   /// have different types)
35   ///
36   ConstantRange(ConstantIntegral *Lower, ConstantIntegral *Upper);
37   
38   /// Initialize a set of values that all satisfy the condition with C.
39   ///
40   ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C);
41   
42   /// getLower - Return the lower value for this range...
43   ///
44   ConstantIntegral *getLower() const { return Lower; }
45
46   /// getUpper - Return the upper value for this range...
47   ///
48   ConstantIntegral *getUpper() const { return Upper; }
49
50   /// getType - Return the LLVM data type of this range.
51   ///
52   const Type *getType() const;
53   
54   /// isFullSet - Return true if this set contains all of the elements possible
55   /// for this data-type
56   ///
57   bool isFullSet() const;
58   
59   /// isEmptySet - Return true if this set contains no members.
60   ///
61   bool isEmptySet() const;
62
63   /// isWrappedSet - Return true if this set wraps around the top of the range,
64   /// for example: [100, 8)
65   ///
66   bool isWrappedSet() const;
67   
68   /// getSingleElement - If this set contains a single element, return it,
69   /// otherwise return null.
70   ///
71   ConstantIntegral *getSingleElement() const;
72   
73   /// isSingleElement - Return true if this set contains exactly one member.
74   ///
75   bool isSingleElement() const { return getSingleElement() != 0; }
76
77   /// getSetSize - Return the number of elements in this set.
78   ///
79   uint64_t getSetSize() const;
80
81   /// operator== - Return true if this range is equal to another range.
82   ///
83   bool operator==(const ConstantRange &CR) const {
84     return Lower == CR.Lower && Upper == CR.Upper;
85   }
86   bool operator!=(const ConstantRange &CR) const {
87     return !operator==(CR);
88   }
89
90   /// intersect - Return the range that results from the intersection of this
91   /// range with another range.  The resultant range is pruned as much as
92   /// possible, but there may be cases where elements are included that are in
93   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
94   /// 120) yields [3, 120)
95   ///
96   ConstantRange intersectWith(const ConstantRange &CR) const;
97
98   /// union - Return the range that results from the union of this range with
99   /// another range.  The resultant range is guaranteed to include the elements
100   /// of both sets, but may contain more.  For example, [3, 9) union [12,15) is
101   /// [3, 15), which includes 9, 10, and 11, which were not included in either
102   /// set before.
103   ///
104   ConstantRange unionWith(const ConstantRange &CR) const;
105
106   /// print - Print out the bounds to a stream...
107   ///
108   void print(std::ostream &OS) const;
109
110   /// dump - Allow printing from a debugger easily...
111   ///
112   void dump() const;
113 };
114
115 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {
116   CR.print(OS);
117   return OS;
118 }
119
120 #endif