1 //===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
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
27 // Note that ConstantRange always keeps unsigned values.
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
31 #define LLVM_SUPPORT_CONSTANT_RANGE_H
33 #include "llvm/Support/DataTypes.h"
34 #include "llvm/Support/Streams.h"
39 class ConstantIntegral;
44 ConstantIntegral *Lower, *Upper;
46 /// Initialize a full (the default) or empty set for the specified type.
48 ConstantRange(const Type *Ty, bool isFullSet = true);
50 /// Initialize a range to hold the single specified value.
52 ConstantRange(Constant *Value);
54 /// Initialize a range of values explicitly... this will assert out if
55 /// Lower==Upper and Lower != Min or Max for its type, if the two constants
56 /// have different types, or if the constant are not integral values.
58 ConstantRange(Constant *Lower, Constant *Upper);
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
63 /// @brief Get a range for a relation with a constant integral.
64 ConstantRange(unsigned short predicate, ConstantIntegral *C);
66 /// getLower - Return the lower value for this range...
68 ConstantIntegral *getLower() const { return Lower; }
70 /// getUpper - Return the upper value for this range...
72 ConstantIntegral *getUpper() const { return Upper; }
74 /// getType - Return the LLVM data type of this range.
76 const Type *getType() const;
78 /// isFullSet - Return true if this set contains all of the elements possible
79 /// for this data-type
81 bool isFullSet() const;
83 /// isEmptySet - Return true if this set contains no members.
85 bool isEmptySet() const;
87 /// isWrappedSet - Return true if this set wraps around the top of the range,
88 /// for example: [100, 8)
90 bool isWrappedSet(bool isSigned) const;
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.
96 bool contains(ConstantInt *Val, bool isSigned) const;
98 /// getSingleElement - If this set contains a single element, return it,
99 /// otherwise return null.
101 ConstantIntegral *getSingleElement() const;
103 /// isSingleElement - Return true if this set contains exactly one member.
105 bool isSingleElement() const { return getSingleElement() != 0; }
107 /// getSetSize - Return the number of elements in this set.
109 uint64_t getSetSize() const;
111 /// operator== - Return true if this range is equal to another range.
113 bool operator==(const ConstantRange &CR) const {
114 return Lower == CR.Lower && Upper == CR.Upper;
116 bool operator!=(const ConstantRange &CR) const {
117 return !operator==(CR);
120 /// subtract - Subtract the specified constant from the endpoints of this
122 ConstantRange subtract(ConstantInt *CI) const;
124 /// intersect - Return the range that results from the intersection of this
125 /// range with another range. The resultant range is pruned as much as
126 /// possible, but there may be cases where elements are included that are in
127 /// one of the sets but not the other. For example: [100, 8) intersect [3,
128 /// 120) yields [3, 120)
130 ConstantRange intersectWith(const ConstantRange &CR, bool isSigned) const;
132 /// union - Return the range that results from the union of this range with
133 /// another range. The resultant range is guaranteed to include the elements
134 /// of both sets, but may contain more. For example, [3, 9) union [12,15) is
135 /// [3, 15), which includes 9, 10, and 11, which were not included in either
138 ConstantRange unionWith(const ConstantRange &CR, bool isSigned) const;
140 /// zeroExtend - Return a new range in the specified integer type, which must
141 /// be strictly larger than the current type. The returned range will
142 /// correspond to the possible range of values if the source range had been
144 ConstantRange zeroExtend(const Type *Ty) const;
146 /// truncate - Return a new range in the specified integer type, which must be
147 /// strictly smaller than the current type. The returned range will
148 /// correspond to the possible range of values if the source range had been
149 /// truncated to the specified type.
150 ConstantRange truncate(const Type *Ty) const;
152 /// print - Print out the bounds to a stream...
154 void print(std::ostream &OS) const;
155 void print(std::ostream *OS) const { if (OS) print(*OS); }
157 /// dump - Allow printing from a debugger easily...
162 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {
167 } // End llvm namespace