Make sure to consider alignment of variable sized objects.
[oota-llvm.git] / include / llvm / CodeGen / LiveInterval.h
1 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LiveRange and LiveInterval classes.  Given some
11 // numbering of each the machine instructions an interval [i, j) is said to be a
12 // live interval for register v if there is no instruction with number j' > j
13 // such that v is live at j' and there is no instruction with number i' < i such
14 // that v is live at i'. In this implementation intervals can have holes,
15 // i.e. an interval might look like [1,20), [50,65), [1000,1001).  Each
16 // individual range is represented as an instance of LiveRange, and the whole
17 // interval is represented as an instance of LiveInterval.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22 #define LLVM_CODEGEN_LIVEINTERVAL_H
23
24 #include <iosfwd>
25 #include <vector>
26 #include <cassert>
27
28 namespace llvm {
29   class MRegisterInfo;
30
31   /// LiveRange structure - This represents a simple register range in the
32   /// program, with an inclusive start point and an exclusive end point.
33   /// These ranges are rendered as [start,end).
34   struct LiveRange {
35     unsigned start;  // Start point of the interval (inclusive)
36     unsigned end;    // End point of the interval (exclusive)
37     unsigned ValId;  // identifier for the value contained in this interval.
38
39     LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
40       assert(S < E && "Cannot create empty or backwards range");
41     }
42
43     /// contains - Return true if the index is covered by this range.
44     ///
45     bool contains(unsigned I) const {
46       return start <= I && I < end;
47     }
48
49     bool operator<(const LiveRange &LR) const {
50       return start < LR.start || (start == LR.start && end < LR.end);
51     }
52     bool operator==(const LiveRange &LR) const {
53       return start == LR.start && end == LR.end;
54     }
55
56     void dump() const;
57
58   private:
59     LiveRange(); // DO NOT IMPLEMENT
60   };
61   std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
62
63   inline bool operator<(unsigned V, const LiveRange &LR) {
64     return V < LR.start;
65   }
66
67   inline bool operator<(const LiveRange &LR, unsigned V) {
68     return LR.start < V;
69   }
70
71   /// LiveInterval - This class represents some number of live ranges for a
72   /// register or value.  This class also contains a bit of register allocator
73   /// state.
74   struct LiveInterval {
75     typedef std::vector<LiveRange> Ranges;
76     unsigned reg;        // the register of this interval
77     float weight;        // weight of this interval
78     Ranges ranges;       // the ranges in which this register is live
79
80     LiveInterval(unsigned Reg, float Weight)
81       : reg(Reg), weight(Weight), NumValues(0) {
82     }
83
84     typedef Ranges::iterator iterator;
85     iterator begin() { return ranges.begin(); }
86     iterator end()   { return ranges.end(); }
87
88     typedef Ranges::const_iterator const_iterator;
89     const_iterator begin() const { return ranges.begin(); }
90     const_iterator end() const  { return ranges.end(); }
91
92
93     /// advanceTo - Advance the specified iterator to point to the LiveRange
94     /// containing the specified position, or end() if the position is past the
95     /// end of the interval.  If no LiveRange contains this position, but the
96     /// position is in a hole, this method returns an iterator pointing the the
97     /// LiveRange immediately after the hole.
98     iterator advanceTo(iterator I, unsigned Pos) {
99       if (Pos >= endNumber())
100         return end();
101       while (I->end <= Pos) ++I;
102       return I;
103     }
104
105     void swap(LiveInterval& other) {
106       std::swap(reg, other.reg);
107       std::swap(weight, other.weight);
108       ranges.swap(other.ranges);
109       std::swap(NumValues, other.NumValues);
110     }
111
112     bool containsOneValue() const { return NumValues == 1; }
113
114     unsigned getNextValue() {
115       return NumValues++;
116     }
117
118     bool empty() const { return ranges.empty(); }
119
120     /// beginNumber - Return the lowest numbered slot covered by interval.
121     unsigned beginNumber() const {
122       assert(!empty() && "empty interval for register");
123       return ranges.front().start;
124     }
125
126     /// endNumber - return the maximum point of the interval of the whole,
127     /// exclusive.
128     unsigned endNumber() const {
129       assert(!empty() && "empty interval for register");
130       return ranges.back().end;
131     }
132
133     bool expiredAt(unsigned index) const {
134       return index >= endNumber();
135     }
136
137     bool liveAt(unsigned index) const;
138
139     /// getLiveRangeContaining - Return the live range that contains the
140     /// specified index, or null if there is none.
141     const LiveRange *getLiveRangeContaining(unsigned Idx) const;
142
143
144     /// joinable - Two intervals are joinable if the either don't overlap at all
145     /// or if the destination of the copy is a single assignment value, and it
146     /// only overlaps with one value in the source interval.
147     bool joinable(const LiveInterval& other, unsigned CopyIdx) const;
148
149     /// getOverlapingRanges - Given another live interval which is defined as a
150     /// copy from this one, return a list of all of the live ranges where the
151     /// two overlap and have different value numbers.
152     void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
153                              std::vector<LiveRange*> &Ranges);
154
155     /// overlaps - Return true if the intersection of the two live intervals is
156     /// not empty.
157     bool overlaps(const LiveInterval& other) const {
158       return overlapsFrom(other, other.begin());
159     }
160
161     /// overlapsFrom - Return true if the intersection of the two live intervals
162     /// is not empty.  The specified iterator is a hint that we can begin
163     /// scanning the Other interval starting at I.
164     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
165
166     /// addRange - Add the specified LiveRange to this interval, merging
167     /// intervals as appropriate.  This returns an iterator to the inserted live
168     /// range (which may have grown since it was inserted.
169     void addRange(LiveRange LR) {
170       addRangeFrom(LR, ranges.begin());
171     }
172
173     /// join - Join two live intervals (this, and other) together.  This
174     /// operation is the result of a copy instruction in the source program,
175     /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'.  This
176     /// destroys 'other'.
177     void join(LiveInterval& other, unsigned CopyIdx);
178
179
180     /// removeRange - Remove the specified range from this interval.  Note that
181     /// the range must already be in this interval in its entirety.
182     void removeRange(unsigned Start, unsigned End);
183
184     bool operator<(const LiveInterval& other) const {
185       return beginNumber() < other.beginNumber();
186     }
187
188     void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const;
189     void dump() const;
190
191   private:
192     unsigned NumValues;  // the number of distinct values in this interval.
193     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
194     void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
195     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
196     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
197   };
198
199   inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {
200     LI.print(OS);
201     return OS;
202   }
203 }
204
205 #endif