Initial implementation of ConstantRange support
[oota-llvm.git] / include / llvm / Support / ConstantRange.h
1 //===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
2 //
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):
9 //
10 //  [F, F) = {}     = Empty set
11 //  [T, F) = {T}
12 //  [F, T) = {F}
13 //  [T, T) = {F, T} = Full set
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
18 #define LLVM_SUPPORT_CONSTANT_RANGE_H
19
20 #include "Support/DataTypes.h"
21 class ConstantIntegral;
22 class Type;
23
24 class ConstantRange {
25   ConstantIntegral *Lower, *Upper;
26  public:
27   /// Initialize a full (the default) or empty set for the specified type.
28   ///
29   ConstantRange(const Type *Ty, bool isFullSet = true);
30   
31   /// Initialize a range of values explicitly... this will assert out if
32   /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
33   /// have different types)
34   ///
35   ConstantRange(ConstantIntegral *Lower, ConstantIntegral *Upper);
36   
37   /// Initialize a set of values that all satisfy the condition with C.
38   ///
39   ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C);
40   
41   /// getLower - Return the lower value for this range...
42   ///
43   ConstantIntegral *getLower() const { return Lower; }
44
45   /// getUpper - Return the upper value for this range...
46   ///
47   ConstantIntegral *getUpper() const { return Upper; }
48
49   /// getType - Return the LLVM data type of this range.
50   ///
51   const Type *getType() const;
52   
53   /// isFullSet - Return true if this set contains all of the elements possible
54   /// for this data-type
55   ///
56   bool isFullSet() const;
57   
58   /// isEmptySet - Return true if this set contains no members.
59   ///
60   bool isEmptySet() const;
61
62   /// isWrappedSet - Return true if this set wraps around the top of the range,
63   /// for example: [100, 8)
64   ///
65   bool isWrappedSet() const;
66   
67   /// getSingleElement - If this set contains a single element, return it,
68   /// otherwise return null.
69   ///
70   ConstantIntegral *getSingleElement() const;
71   
72   /// isSingleElement - Return true if this set contains exactly one member.
73   ///
74   bool isSingleElement() const { return getSingleElement() != 0; }
75
76   /// getSetSize - Return the number of elements in this set.
77   ///
78   uint64_t getSetSize() const;
79
80   /// intersect - Return the range that results from the intersection of this
81   /// range with another range.  The resultant range is pruned as much as
82   /// possible, but there may be cases where elements are included that are in
83   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
84   /// 120) yields [3, 120)
85   ///
86   ConstantRange intersectWith(const ConstantRange &CR) const;
87
88   /// union - Return the range that results from the union of this range with
89   /// another range.  The resultant range is guaranteed to include the elements
90   /// of both sets, but may contain more.  For example, [3, 9) union [12,15) is
91   /// [3, 15), which includes 9, 10, and 11, which were not included in either
92   /// set before.
93   ///
94   ConstantRange unionWith(const ConstantRange &CR) const;
95 };
96
97 #endif