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
15 // ranges (other integral ranges use min/max values for special range values):
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
22 //===----------------------------------------------------------------------===//
24 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
25 #define LLVM_SUPPORT_CONSTANT_RANGE_H
27 #include "Support/DataTypes.h"
32 class ConstantIntegral;
36 ConstantIntegral *Lower, *Upper;
38 /// Initialize a full (the default) or empty set for the specified type.
40 ConstantRange(const Type *Ty, bool isFullSet = true);
42 /// Initialize a range of values explicitly... this will assert out if
43 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
44 /// have different types)
46 ConstantRange(ConstantIntegral *Lower, ConstantIntegral *Upper);
48 /// Initialize a set of values that all satisfy the condition with C.
50 ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C);
52 /// getLower - Return the lower value for this range...
54 ConstantIntegral *getLower() const { return Lower; }
56 /// getUpper - Return the upper value for this range...
58 ConstantIntegral *getUpper() const { return Upper; }
60 /// getType - Return the LLVM data type of this range.
62 const Type *getType() const;
64 /// isFullSet - Return true if this set contains all of the elements possible
65 /// for this data-type
67 bool isFullSet() const;
69 /// isEmptySet - Return true if this set contains no members.
71 bool isEmptySet() const;
73 /// isWrappedSet - Return true if this set wraps around the top of the range,
74 /// for example: [100, 8)
76 bool isWrappedSet() const;
78 /// getSingleElement - If this set contains a single element, return it,
79 /// otherwise return null.
81 ConstantIntegral *getSingleElement() const;
83 /// isSingleElement - Return true if this set contains exactly one member.
85 bool isSingleElement() const { return getSingleElement() != 0; }
87 /// getSetSize - Return the number of elements in this set.
89 uint64_t getSetSize() const;
91 /// operator== - Return true if this range is equal to another range.
93 bool operator==(const ConstantRange &CR) const {
94 return Lower == CR.Lower && Upper == CR.Upper;
96 bool operator!=(const ConstantRange &CR) const {
97 return !operator==(CR);
100 /// intersect - Return the range that results from the intersection of this
101 /// range with another range. The resultant range is pruned as much as
102 /// possible, but there may be cases where elements are included that are in
103 /// one of the sets but not the other. For example: [100, 8) intersect [3,
104 /// 120) yields [3, 120)
106 ConstantRange intersectWith(const ConstantRange &CR) const;
108 /// union - Return the range that results from the union of this range with
109 /// another range. The resultant range is guaranteed to include the elements
110 /// of both sets, but may contain more. For example, [3, 9) union [12,15) is
111 /// [3, 15), which includes 9, 10, and 11, which were not included in either
114 ConstantRange unionWith(const ConstantRange &CR) const;
116 /// print - Print out the bounds to a stream...
118 void print(std::ostream &OS) const;
120 /// dump - Allow printing from a debugger easily...
125 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {
130 } // End llvm namespace