Add printing support to ConstantRange class
[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   /// intersect - Return the range that results from the intersection of this
82   /// range with another range.  The resultant range is pruned as much as
83   /// possible, but there may be cases where elements are included that are in
84   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
85   /// 120) yields [3, 120)
86   ///
87   ConstantRange intersectWith(const ConstantRange &CR) const;
88
89   /// union - Return the range that results from the union of this range with
90   /// another range.  The resultant range is guaranteed to include the elements
91   /// of both sets, but may contain more.  For example, [3, 9) union [12,15) is
92   /// [3, 15), which includes 9, 10, and 11, which were not included in either
93   /// set before.
94   ///
95   ConstantRange unionWith(const ConstantRange &CR) const;
96
97   /// print - Print out the bounds to a stream...
98   ///
99   void print(std::ostream &OS) const;
100
101   /// dump - Allow printing from a debugger easily...
102   ///
103   void dump() const;
104 };
105
106 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {
107   CR.print(OS);
108   return OS;
109 }
110
111 #endif