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"
29 class ConstantIntegral;
33 ConstantIntegral *Lower, *Upper;
35 /// Initialize a full (the default) or empty set for the specified type.
37 ConstantRange(const Type *Ty, bool isFullSet = true);
39 /// Initialize a range of values explicitly... this will assert out if
40 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
41 /// have different types)
43 ConstantRange(ConstantIntegral *Lower, ConstantIntegral *Upper);
45 /// Initialize a set of values that all satisfy the condition with C.
47 ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C);
49 /// getLower - Return the lower value for this range...
51 ConstantIntegral *getLower() const { return Lower; }
53 /// getUpper - Return the upper value for this range...
55 ConstantIntegral *getUpper() const { return Upper; }
57 /// getType - Return the LLVM data type of this range.
59 const Type *getType() const;
61 /// isFullSet - Return true if this set contains all of the elements possible
62 /// for this data-type
64 bool isFullSet() const;
66 /// isEmptySet - Return true if this set contains no members.
68 bool isEmptySet() const;
70 /// isWrappedSet - Return true if this set wraps around the top of the range,
71 /// for example: [100, 8)
73 bool isWrappedSet() const;
75 /// getSingleElement - If this set contains a single element, return it,
76 /// otherwise return null.
78 ConstantIntegral *getSingleElement() const;
80 /// isSingleElement - Return true if this set contains exactly one member.
82 bool isSingleElement() const { return getSingleElement() != 0; }
84 /// getSetSize - Return the number of elements in this set.
86 uint64_t getSetSize() const;
88 /// operator== - Return true if this range is equal to another range.
90 bool operator==(const ConstantRange &CR) const {
91 return Lower == CR.Lower && Upper == CR.Upper;
93 bool operator!=(const ConstantRange &CR) const {
94 return !operator==(CR);
97 /// intersect - Return the range that results from the intersection of this
98 /// range with another range. The resultant range is pruned as much as
99 /// possible, but there may be cases where elements are included that are in
100 /// one of the sets but not the other. For example: [100, 8) intersect [3,
101 /// 120) yields [3, 120)
103 ConstantRange intersectWith(const ConstantRange &CR) const;
105 /// union - Return the range that results from the union of this range with
106 /// another range. The resultant range is guaranteed to include the elements
107 /// of both sets, but may contain more. For example, [3, 9) union [12,15) is
108 /// [3, 15), which includes 9, 10, and 11, which were not included in either
111 ConstantRange unionWith(const ConstantRange &CR) const;
113 /// print - Print out the bounds to a stream...
115 void print(std::ostream &OS) const;
117 /// dump - Allow printing from a debugger easily...
122 inline std::ostream &operator<<(std::ostream &OS, const ConstantRange &CR) {