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