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