1 //===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
3 // Represent a range of possible values that may occur when the program is run
4 // for an integral value. This keeps track of a lower and upper bound for the
5 // constant, which MAY wrap around the end of the numeric range. To do this, it
6 // keeps track of a [lower, upper) bound, which specifies an interval just like
7 // STL iterators. When used with boolean values, the following are important
8 // ranges (other integral ranges use min/max values for special range values):
10 // [F, F) = {} = Empty set
13 // [T, T) = {F, T} = Full set
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
18 #define LLVM_SUPPORT_CONSTANT_RANGE_H
20 #include "Support/DataTypes.h"
22 class ConstantIntegral;
26 ConstantIntegral *Lower, *Upper;
28 /// Initialize a full (the default) or empty set for the specified type.
30 ConstantRange(const Type *Ty, bool isFullSet = true);
32 /// Initialize a range of values explicitly... this will assert out if
33 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
34 /// have different types)
36 ConstantRange(ConstantIntegral *Lower, ConstantIntegral *Upper);
38 /// Initialize a set of values that all satisfy the condition with C.
40 ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C);
42 /// getLower - Return the lower value for this range...
44 ConstantIntegral *getLower() const { return Lower; }
46 /// getUpper - Return the upper value for this range...
48 ConstantIntegral *getUpper() const { return Upper; }
50 /// getType - Return the LLVM data type of this range.
52 const Type *getType() const;
54 /// isFullSet - Return true if this set contains all of the elements possible
55 /// for this data-type
57 bool isFullSet() const;
59 /// isEmptySet - Return true if this set contains no members.
61 bool isEmptySet() const;
63 /// isWrappedSet - Return true if this set wraps around the top of the range,
64 /// for example: [100, 8)
66 bool isWrappedSet() const;
68 /// getSingleElement - If this set contains a single element, return it,
69 /// otherwise return null.
71 ConstantIntegral *getSingleElement() const;
73 /// isSingleElement - Return true if this set contains exactly one member.
75 bool isSingleElement() const { return getSingleElement() != 0; }
77 /// getSetSize - Return the number of elements in this set.
79 uint64_t getSetSize() const;
81 /// operator== - Return true if this range is equal to another range.
83 bool operator==(const ConstantRange &CR) const {
84 return Lower == CR.Lower && Upper == CR.Upper;
86 bool operator!=(const ConstantRange &CR) const {
87 return !operator==(CR);
90 /// intersect - Return the range that results from the intersection of this
91 /// range with another range. The resultant range is pruned as much as
92 /// possible, but there may be cases where elements are included that are in
93 /// one of the sets but not the other. For example: [100, 8) intersect [3,
94 /// 120) yields [3, 120)
96 ConstantRange intersectWith(const ConstantRange &CR) const;
98 /// union - Return the range that results from the union of this range with
99 /// another range. The resultant range is guaranteed to include the elements
100 /// of both sets, but may contain more. For example, [3, 9) union [12,15) is
101 /// [3, 15), which includes 9, 10, and 11, which were not included in either
104 ConstantRange unionWith(const ConstantRange &CR) const;
106 /// print - Print out the bounds to a stream...
108 void print(std::ostream &OS) const;
110 /// dump - Allow printing from a debugger easily...
115 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {