f37c54c7076da6cfc49dfb8c52074665e5c650f7
[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: :
16 //
17 //  [F, F) = {}     = Empty set
18 //  [T, F) = {T}
19 //  [F, T) = {F}
20 //  [T, T) = {F, T} = Full set
21 //
22 // The other integral ranges use min/max values for special range values. For
23 // example, for 8-bit types, it uses:
24 // [0, 0)     = {}       = Empty set
25 // [255, 255) = {0..255} = Full Set
26 //
27 // Note that ConstantRange always keeps unsigned values.
28 //===----------------------------------------------------------------------===//
29
30 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
31 #define LLVM_SUPPORT_CONSTANT_RANGE_H
32
33 #include "llvm/Support/DataTypes.h"
34 #include "llvm/Support/Streams.h"
35 #include <iosfwd>
36
37 namespace llvm {
38 class Constant;
39 class ConstantInt;
40 class Type;
41
42 class ConstantRange {
43   ConstantInt *Lower, *Upper;
44  public:
45   /// Initialize a full (the default) or empty set for the specified type.
46   ///
47   ConstantRange(const Type *Ty, bool isFullSet = true);
48
49   /// Initialize a range to hold the single specified value.
50   ///
51   ConstantRange(Constant *Value);
52
53   /// Initialize a range of values explicitly... this will assert out if
54   /// Lower==Upper and Lower != Min or Max for its type, if the two constants
55   /// have different types, or if the constant are not integral values.
56   ///
57   ConstantRange(Constant *Lower, Constant *Upper);
58
59   /// Initialize a set of values that all satisfy the predicate with C. The
60   /// predicate should be either an ICmpInst::Predicate or FCmpInst::Predicate
61   /// value.
62   /// @brief Get a range for a relation with a constant integral.
63   ConstantRange(unsigned short predicate, ConstantInt *C);
64
65   /// getLower - Return the lower value for this range...
66   ///
67   ConstantInt *getLower() const { return Lower; }
68
69   /// getUpper - Return the upper value for this range...
70   ///
71   ConstantInt *getUpper() const { return Upper; }
72
73   /// getType - Return the LLVM data type of this range.
74   ///
75   const Type *getType() const;
76
77   /// isFullSet - Return true if this set contains all of the elements possible
78   /// for this data-type
79   ///
80   bool isFullSet() const;
81
82   /// isEmptySet - Return true if this set contains no members.
83   ///
84   bool isEmptySet() const;
85
86   /// isWrappedSet - Return true if this set wraps around the top of the range,
87   /// for example: [100, 8)
88   ///
89   bool isWrappedSet(bool isSigned) const;
90
91   /// contains - Return true if the specified value is in the set.
92   /// The isSigned parameter indicates whether the comparisons should be
93   /// performed as if the values are signed or not.
94   ///
95   bool contains(ConstantInt *Val, bool isSigned) const;
96
97   /// getSingleElement - If this set contains a single element, return it,
98   /// otherwise return null.
99   ///
100   ConstantInt *getSingleElement() const;
101
102   /// isSingleElement - Return true if this set contains exactly one member.
103   ///
104   bool isSingleElement() const { return getSingleElement() != 0; }
105
106   /// getSetSize - Return the number of elements in this set.
107   ///
108   uint64_t getSetSize() const;
109
110   /// operator== - Return true if this range is equal to another range.
111   ///
112   bool operator==(const ConstantRange &CR) const {
113     return Lower == CR.Lower && Upper == CR.Upper;
114   }
115   bool operator!=(const ConstantRange &CR) const {
116     return !operator==(CR);
117   }
118
119   /// subtract - Subtract the specified constant from the endpoints of this
120   /// constant range.
121   ConstantRange subtract(ConstantInt *CI) const;
122
123   /// intersect - Return the range that results from the intersection of this
124   /// range with another range.  The resultant range is pruned as much as
125   /// possible, but there may be cases where elements are included that are in
126   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
127   /// 120) yields [3, 120)
128   ///
129   ConstantRange intersectWith(const ConstantRange &CR, bool isSigned) const;
130
131   /// union - Return the range that results from the union of this range with
132   /// another range.  The resultant range is guaranteed to include the elements
133   /// of both sets, but may contain more.  For example, [3, 9) union [12,15) is
134   /// [3, 15), which includes 9, 10, and 11, which were not included in either
135   /// set before.
136   ///
137   ConstantRange unionWith(const ConstantRange &CR, bool isSigned) const;
138
139   /// zeroExtend - Return a new range in the specified integer type, which must
140   /// be strictly larger than the current type.  The returned range will
141   /// correspond to the possible range of values if the source range had been
142   /// zero extended.
143   ConstantRange zeroExtend(const Type *Ty) const;
144
145   /// truncate - Return a new range in the specified integer type, which must be
146   /// strictly smaller than the current type.  The returned range will
147   /// correspond to the possible range of values if the source range had been
148   /// truncated to the specified type.
149   ConstantRange truncate(const Type *Ty) const;
150
151   /// print - Print out the bounds to a stream...
152   ///
153   void print(std::ostream &OS) const;
154   void print(std::ostream *OS) const { if (OS) print(*OS); }
155
156   /// dump - Allow printing from a debugger easily...
157   ///
158   void dump() const;
159 };
160
161 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {
162   CR.print(OS);
163   return OS;
164 }
165
166 } // End llvm namespace
167
168 #endif