Implement "general dynamic", "initial exec" and "local exec" TLS models for
[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 "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Streams.h"
26 #include <iosfwd>
27 #include <vector>
28 #include <cassert>
29
30 namespace llvm {
31   class MachineInstr;
32   class MRegisterInfo;
33
34   /// LiveRange structure - This represents a simple register range in the
35   /// program, with an inclusive start point and an exclusive end point.
36   /// These ranges are rendered as [start,end).
37   struct LiveRange {
38     unsigned start;  // Start point of the interval (inclusive)
39     unsigned end;    // End point of the interval (exclusive)
40     unsigned ValId;  // identifier for the value contained in this interval.
41
42     LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
43       assert(S < E && "Cannot create empty or backwards range");
44     }
45
46     /// contains - Return true if the index is covered by this range.
47     ///
48     bool contains(unsigned I) const {
49       return start <= I && I < end;
50     }
51
52     bool operator<(const LiveRange &LR) const {
53       return start < LR.start || (start == LR.start && end < LR.end);
54     }
55     bool operator==(const LiveRange &LR) const {
56       return start == LR.start && end == LR.end;
57     }
58
59     void dump() const;
60     void print(std::ostream &os) const;
61     void print(std::ostream *os) const { if (os) print(*os); }
62
63   private:
64     LiveRange(); // DO NOT IMPLEMENT
65   };
66
67   std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
68
69
70   inline bool operator<(unsigned V, const LiveRange &LR) {
71     return V < LR.start;
72   }
73
74   inline bool operator<(const LiveRange &LR, unsigned V) {
75     return LR.start < V;
76   }
77
78   /// LiveInterval - This class represents some number of live ranges for a
79   /// register or value.  This class also contains a bit of register allocator
80   /// state.
81   struct LiveInterval {
82     typedef SmallVector<LiveRange,4> Ranges;
83     unsigned reg;        // the register of this interval
84     unsigned preference; // preferred register to allocate for this interval
85     float weight;        // weight of this interval
86     MachineInstr* remat; // definition if the definition rematerializable
87     Ranges ranges;       // the ranges in which this register is live
88   private:
89     /// ValueNumberInfo - If this value number is not defined by a copy, this
90     /// holds ~0,x.  If the value number is not in use, it contains ~1,x to
91     /// indicate that the value # is not used.  If the val# is defined by a
92     /// copy, the first entry is the instruction # of the copy, and the second
93     /// is the register number copied from.
94     SmallVector<std::pair<unsigned,unsigned>, 4> ValueNumberInfo;
95   public:
96
97     LiveInterval(unsigned Reg, float Weight)
98       : reg(Reg), preference(0), weight(Weight), remat(NULL) {
99     }
100
101     typedef Ranges::iterator iterator;
102     iterator begin() { return ranges.begin(); }
103     iterator end()   { return ranges.end(); }
104
105     typedef Ranges::const_iterator const_iterator;
106     const_iterator begin() const { return ranges.begin(); }
107     const_iterator end() const  { return ranges.end(); }
108
109
110     /// advanceTo - Advance the specified iterator to point to the LiveRange
111     /// containing the specified position, or end() if the position is past the
112     /// end of the interval.  If no LiveRange contains this position, but the
113     /// position is in a hole, this method returns an iterator pointing the the
114     /// LiveRange immediately after the hole.
115     iterator advanceTo(iterator I, unsigned Pos) {
116       if (Pos >= endNumber())
117         return end();
118       while (I->end <= Pos) ++I;
119       return I;
120     }
121
122     void swap(LiveInterval& other) {
123       std::swap(reg, other.reg);
124       std::swap(weight, other.weight);
125       std::swap(remat, other.remat);
126       std::swap(ranges, other.ranges);
127       std::swap(ValueNumberInfo, other.ValueNumberInfo);
128     }
129
130     bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
131
132     unsigned getNumValNums() const { return ValueNumberInfo.size(); }
133     
134     /// getNextValue - Create a new value number and return it.  MIIdx specifies
135     /// the instruction that defines the value number.
136     unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
137       ValueNumberInfo.push_back(std::make_pair(MIIdx, SrcReg));
138       return ValueNumberInfo.size()-1;
139     }
140     
141     /// getInstForValNum - Return the machine instruction index that defines the
142     /// specified value number.
143     unsigned getInstForValNum(unsigned ValNo) const {
144       //assert(ValNo < ValueNumberInfo.size());
145       return ValueNumberInfo[ValNo].first;
146     }
147     
148     unsigned getSrcRegForValNum(unsigned ValNo) const {
149       //assert(ValNo < ValueNumberInfo.size());
150       if (ValueNumberInfo[ValNo].first < ~2U)
151         return ValueNumberInfo[ValNo].second;
152       return 0;
153     }
154     
155     std::pair<unsigned, unsigned> getValNumInfo(unsigned ValNo) const {
156       //assert(ValNo < ValueNumberInfo.size());
157       return ValueNumberInfo[ValNo];
158     }
159     
160     /// setValueNumberInfo - Change the value number info for the specified
161     /// value number.
162     void setValueNumberInfo(unsigned ValNo,
163                             const std::pair<unsigned, unsigned> &I){
164       ValueNumberInfo[ValNo] = I;
165     }
166     
167     /// MergeValueNumberInto - This method is called when two value nubmers
168     /// are found to be equivalent.  This eliminates V1, replacing all
169     /// LiveRanges with the V1 value number with the V2 value number.  This can
170     /// cause merging of V1/V2 values numbers and compaction of the value space.
171     void MergeValueNumberInto(unsigned V1, unsigned V2);
172
173     /// MergeInClobberRanges - For any live ranges that are not defined in the
174     /// current interval, but are defined in the Clobbers interval, mark them
175     /// used with an unknown definition value.
176     void MergeInClobberRanges(const LiveInterval &Clobbers);
177
178     
179     /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
180     /// interval as the specified value number.  The LiveRanges in RHS are
181     /// allowed to overlap with LiveRanges in the current interval, but only if
182     /// the overlapping LiveRanges have the specified value number.
183     void MergeRangesInAsValue(const LiveInterval &RHS, unsigned LHSValNo);
184     
185     bool empty() const { return ranges.empty(); }
186
187     /// beginNumber - Return the lowest numbered slot covered by interval.
188     unsigned beginNumber() const {
189       assert(!empty() && "empty interval for register");
190       return ranges.front().start;
191     }
192
193     /// endNumber - return the maximum point of the interval of the whole,
194     /// exclusive.
195     unsigned endNumber() const {
196       assert(!empty() && "empty interval for register");
197       return ranges.back().end;
198     }
199
200     bool expiredAt(unsigned index) const {
201       return index >= endNumber();
202     }
203
204     bool liveAt(unsigned index) const;
205
206     /// getLiveRangeContaining - Return the live range that contains the
207     /// specified index, or null if there is none.
208     const LiveRange *getLiveRangeContaining(unsigned Idx) const {
209       const_iterator I = FindLiveRangeContaining(Idx);
210       return I == end() ? 0 : &*I;
211     }
212
213     /// FindLiveRangeContaining - Return an iterator to the live range that
214     /// contains the specified index, or end() if there is none.
215     const_iterator FindLiveRangeContaining(unsigned Idx) const;
216
217     /// FindLiveRangeContaining - Return an iterator to the live range that
218     /// contains the specified index, or end() if there is none.
219     iterator FindLiveRangeContaining(unsigned Idx);
220     
221     /// getOverlapingRanges - Given another live interval which is defined as a
222     /// copy from this one, return a list of all of the live ranges where the
223     /// two overlap and have different value numbers.
224     void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
225                              std::vector<LiveRange*> &Ranges);
226
227     /// overlaps - Return true if the intersection of the two live intervals is
228     /// not empty.
229     bool overlaps(const LiveInterval& other) const {
230       return overlapsFrom(other, other.begin());
231     }
232
233     /// overlapsFrom - Return true if the intersection of the two live intervals
234     /// is not empty.  The specified iterator is a hint that we can begin
235     /// scanning the Other interval starting at I.
236     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
237
238     /// addRange - Add the specified LiveRange to this interval, merging
239     /// intervals as appropriate.  This returns an iterator to the inserted live
240     /// range (which may have grown since it was inserted.
241     void addRange(LiveRange LR) {
242       addRangeFrom(LR, ranges.begin());
243     }
244
245     /// join - Join two live intervals (this, and other) together.  This applies
246     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
247     /// the intervals are not joinable, this aborts.
248     void join(LiveInterval &Other, int *ValNoAssignments,
249               int *RHSValNoAssignments,
250               SmallVector<std::pair<unsigned,unsigned>,16> &NewValueNumberInfo);
251
252     /// removeRange - Remove the specified range from this interval.  Note that
253     /// the range must already be in this interval in its entirety.
254     void removeRange(unsigned Start, unsigned End);
255
256     void removeRange(LiveRange LR) {
257       removeRange(LR.start, LR.end);
258     }
259
260     /// getSize - Returns the sum of sizes of all the LiveRange's.
261     ///
262     unsigned getSize() const;
263
264     bool operator<(const LiveInterval& other) const {
265       return beginNumber() < other.beginNumber();
266     }
267
268     void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const;
269     void print(std::ostream *OS, const MRegisterInfo *MRI = 0) const {
270       if (OS) print(*OS, MRI);
271     }
272     void dump() const;
273
274   private:
275     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
276     void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
277     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
278     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
279   };
280
281   inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {
282     LI.print(OS);
283     return OS;
284   }
285 }
286
287 #endif