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