Remove AsmThatEarlyClobber etc. from LiveIntervalAnalysis
[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 is distributed under the University of Illinois Open Source
6 // 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/Allocator.h"
26 #include <iosfwd>
27 #include <cassert>
28
29 namespace llvm {
30   class MachineInstr;
31   class TargetRegisterInfo;
32   struct LiveInterval;
33
34   /// VNInfo - If the value number definition is undefined (e.g. phi
35   /// merge point), it contains ~0u,x. If the value number is not in use, it
36   /// contains ~1u,x to indicate that the value # is not used. 
37   ///   def   - Instruction # of the definition.
38   ///         - or reg # of the definition if it's a stack slot liveinterval.
39   ///   copy  - Copy iff val# is defined by a copy; zero otherwise.
40   ///   hasPHIKill - One or more of the kills are PHI nodes.
41   ///   kills - Instruction # of the kills.
42   struct VNInfo {
43     unsigned id;
44     unsigned def;
45     MachineInstr *copy;
46     bool hasPHIKill;
47     SmallVector<unsigned, 4> kills;
48     VNInfo() : id(~1U), def(~1U), copy(0), hasPHIKill(false) {}
49     VNInfo(unsigned i, unsigned d, MachineInstr *c)
50       : id(i), def(d), copy(c), hasPHIKill(false) {}
51   };
52
53   /// LiveRange structure - This represents a simple register range in the
54   /// program, with an inclusive start point and an exclusive end point.
55   /// These ranges are rendered as [start,end).
56   struct LiveRange {
57     unsigned start;  // Start point of the interval (inclusive)
58     unsigned end;    // End point of the interval (exclusive)
59     VNInfo *valno;   // identifier for the value contained in this interval.
60
61     LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
62       assert(S < E && "Cannot create empty or backwards range");
63     }
64
65     /// contains - Return true if the index is covered by this range.
66     ///
67     bool contains(unsigned I) const {
68       return start <= I && I < end;
69     }
70
71     bool operator<(const LiveRange &LR) const {
72       return start < LR.start || (start == LR.start && end < LR.end);
73     }
74     bool operator==(const LiveRange &LR) const {
75       return start == LR.start && end == LR.end;
76     }
77
78     void dump() const;
79     void print(std::ostream &os) const;
80     void print(std::ostream *os) const { if (os) print(*os); }
81
82   private:
83     LiveRange(); // DO NOT IMPLEMENT
84   };
85
86   std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
87
88
89   inline bool operator<(unsigned V, const LiveRange &LR) {
90     return V < LR.start;
91   }
92
93   inline bool operator<(const LiveRange &LR, unsigned V) {
94     return LR.start < V;
95   }
96
97   /// LiveInterval - This class represents some number of live ranges for a
98   /// register or value.  This class also contains a bit of register allocator
99   /// state.
100   struct LiveInterval {
101     typedef SmallVector<LiveRange,4> Ranges;
102     typedef SmallVector<VNInfo*,4> VNInfoList;
103
104     unsigned reg;        // the register or stack slot of this interval
105                          // if the top bits is set, it represents a stack slot.
106     unsigned preference; // preferred register to allocate for this interval
107     float weight;        // weight of this interval
108     bool isEarlyClobber;
109     bool overlapsEarlyClobber;
110     Ranges ranges;       // the ranges in which this register is live
111     VNInfoList valnos;   // value#'s
112
113   public:
114     LiveInterval(unsigned Reg, float Weight, bool IsSS = false, 
115                  bool IsEarlyClobber = false, bool OverlapsEarlyClobber = false)
116       : reg(Reg), preference(0), weight(Weight), 
117         isEarlyClobber(IsEarlyClobber), 
118         overlapsEarlyClobber(OverlapsEarlyClobber) {
119       if (IsSS)
120         reg = reg | (1U << (sizeof(unsigned)*8-1));
121     }
122
123     typedef Ranges::iterator iterator;
124     iterator begin() { return ranges.begin(); }
125     iterator end()   { return ranges.end(); }
126
127     typedef Ranges::const_iterator const_iterator;
128     const_iterator begin() const { return ranges.begin(); }
129     const_iterator end() const  { return ranges.end(); }
130
131     typedef VNInfoList::iterator vni_iterator;
132     vni_iterator vni_begin() { return valnos.begin(); }
133     vni_iterator vni_end() { return valnos.end(); }
134
135     typedef VNInfoList::const_iterator const_vni_iterator;
136     const_vni_iterator vni_begin() const { return valnos.begin(); }
137     const_vni_iterator vni_end() const { return valnos.end(); }
138
139     /// advanceTo - Advance the specified iterator to point to the LiveRange
140     /// containing the specified position, or end() if the position is past the
141     /// end of the interval.  If no LiveRange contains this position, but the
142     /// position is in a hole, this method returns an iterator pointing the the
143     /// LiveRange immediately after the hole.
144     iterator advanceTo(iterator I, unsigned Pos) {
145       if (Pos >= endNumber())
146         return end();
147       while (I->end <= Pos) ++I;
148       return I;
149     }
150
151     /// isStackSlot - Return true if this is a stack slot interval.
152     ///
153     bool isStackSlot() const {
154       return reg & (1U << (sizeof(unsigned)*8-1));
155     }
156
157     /// getStackSlotIndex - Return stack slot index if this is a stack slot
158     /// interval.
159     int getStackSlotIndex() const {
160       assert(isStackSlot() && "Interval is not a stack slot interval!");
161       return reg & ~(1U << (sizeof(unsigned)*8-1));
162     }
163
164     bool containsOneValue() const { return valnos.size() == 1; }
165
166     unsigned getNumValNums() const { return (unsigned)valnos.size(); }
167     
168     /// getValNumInfo - Returns pointer to the specified val#.
169     ///
170     inline VNInfo *getValNumInfo(unsigned ValNo) {
171       return valnos[ValNo];
172     }
173     inline const VNInfo *getValNumInfo(unsigned ValNo) const {
174       return valnos[ValNo];
175     }
176     
177     /// copyValNumInfo - Copy the value number info for one value number to
178     /// another.
179     void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
180       DstValNo->def = SrcValNo->def;
181       DstValNo->copy = SrcValNo->copy;
182       DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
183       DstValNo->kills = SrcValNo->kills;
184     }
185
186     /// getNextValue - Create a new value number and return it.  MIIdx specifies
187     /// the instruction that defines the value number.
188     VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
189                          BumpPtrAllocator &VNInfoAllocator) {
190 #ifdef __GNUC__
191       unsigned Alignment = (unsigned)__alignof__(VNInfo);
192 #else
193       // FIXME: ugly.
194       unsigned Alignment = 8;
195 #endif
196       VNInfo *VNI =
197         static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
198                                                       Alignment));
199       new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
200       valnos.push_back(VNI);
201       return VNI;
202     }
203
204     /// addKillForValNum - Add a kill instruction index to the specified value
205     /// number.
206     static void addKill(VNInfo *VNI, unsigned KillIdx) {
207       SmallVector<unsigned, 4> &kills = VNI->kills;
208       if (kills.empty()) {
209         kills.push_back(KillIdx);
210       } else {
211         SmallVector<unsigned, 4>::iterator
212           I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
213         kills.insert(I, KillIdx);
214       }
215     }
216
217     /// addKills - Add a number of kills into the VNInfo kill vector. If this
218     /// interval is live at a kill point, then the kill is not added.
219     void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
220       for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
221            i != e; ++i) {
222         unsigned KillIdx = kills[i];
223         if (!liveBeforeAndAt(KillIdx)) {
224           SmallVector<unsigned, 4>::iterator
225             I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx);
226           VNI->kills.insert(I, KillIdx);
227         }
228       }
229     }
230
231     /// removeKill - Remove the specified kill from the list of kills of
232     /// the specified val#.
233     static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
234       SmallVector<unsigned, 4> &kills = VNI->kills;
235       SmallVector<unsigned, 4>::iterator
236         I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
237       if (I != kills.end() && *I == KillIdx) {
238         kills.erase(I);
239         return true;
240       }
241       return false;
242     }
243
244     /// removeKills - Remove all the kills in specified range
245     /// [Start, End] of the specified val#.
246     void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
247       SmallVector<unsigned, 4> &kills = VNI->kills;
248       SmallVector<unsigned, 4>::iterator
249         I = std::lower_bound(kills.begin(), kills.end(), Start);
250       SmallVector<unsigned, 4>::iterator
251         E = std::upper_bound(kills.begin(), kills.end(), End);
252       kills.erase(I, E);
253     }
254
255     /// isKill - Return true if the specified index is a kill of the
256     /// specified val#.
257     bool isKill(const VNInfo *VNI, unsigned KillIdx) const {
258       const SmallVector<unsigned, 4> &kills = VNI->kills;
259       SmallVector<unsigned, 4>::const_iterator
260         I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
261       return I != kills.end() && *I == KillIdx;
262     }
263     
264     /// MergeValueNumberInto - This method is called when two value nubmers
265     /// are found to be equivalent.  This eliminates V1, replacing all
266     /// LiveRanges with the V1 value number with the V2 value number.  This can
267     /// cause merging of V1/V2 values numbers and compaction of the value space.
268     void MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
269
270     /// MergeInClobberRanges - For any live ranges that are not defined in the
271     /// current interval, but are defined in the Clobbers interval, mark them
272     /// used with an unknown definition value. Caller must pass in reference to
273     /// VNInfoAllocator since it will create a new val#.
274     void MergeInClobberRanges(const LiveInterval &Clobbers,
275                               BumpPtrAllocator &VNInfoAllocator);
276
277     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
278     /// in RHS into this live interval as the specified value number.
279     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
280     /// current interval, it will replace the value numbers of the overlaped
281     /// live ranges with the specified value number.
282     void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
283
284     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
285     /// in RHS into this live interval as the specified value number.
286     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
287     /// current interval, but only if the overlapping LiveRanges have the
288     /// specified value number.
289     void MergeValueInAsValue(const LiveInterval &RHS,
290                              const VNInfo *RHSValNo, VNInfo *LHSValNo);
291
292     /// Copy - Copy the specified live interval. This copies all the fields
293     /// except for the register of the interval.
294     void Copy(const LiveInterval &RHS, BumpPtrAllocator &VNInfoAllocator);
295     
296     bool empty() const { return ranges.empty(); }
297
298     /// beginNumber - Return the lowest numbered slot covered by interval.
299     unsigned beginNumber() const {
300       if (empty())
301         return 0;
302       return ranges.front().start;
303     }
304
305     /// endNumber - return the maximum point of the interval of the whole,
306     /// exclusive.
307     unsigned endNumber() const {
308       if (empty())
309         return 0;
310       return ranges.back().end;
311     }
312
313     bool expiredAt(unsigned index) const {
314       return index >= endNumber();
315     }
316
317     bool liveAt(unsigned index) const;
318
319     // liveBeforeAndAt - Check if the interval is live at the index and the
320     // index just before it. If index is liveAt, check if it starts a new live
321     // range.If it does, then check if the previous live range ends at index-1.
322     bool liveBeforeAndAt(unsigned index) const;
323
324     /// getLiveRangeContaining - Return the live range that contains the
325     /// specified index, or null if there is none.
326     const LiveRange *getLiveRangeContaining(unsigned Idx) const {
327       const_iterator I = FindLiveRangeContaining(Idx);
328       return I == end() ? 0 : &*I;
329     }
330
331     /// FindLiveRangeContaining - Return an iterator to the live range that
332     /// contains the specified index, or end() if there is none.
333     const_iterator FindLiveRangeContaining(unsigned Idx) const;
334
335     /// FindLiveRangeContaining - Return an iterator to the live range that
336     /// contains the specified index, or end() if there is none.
337     iterator FindLiveRangeContaining(unsigned Idx);
338
339     /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
340     /// index (register interval) or defined by the specified register (stack
341     /// inteval).
342     VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
343     
344     /// overlaps - Return true if the intersection of the two live intervals is
345     /// not empty.
346     bool overlaps(const LiveInterval& other) const {
347       return overlapsFrom(other, other.begin());
348     }
349
350     /// overlapsFrom - Return true if the intersection of the two live intervals
351     /// is not empty.  The specified iterator is a hint that we can begin
352     /// scanning the Other interval starting at I.
353     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
354
355     /// addRange - Add the specified LiveRange to this interval, merging
356     /// intervals as appropriate.  This returns an iterator to the inserted live
357     /// range (which may have grown since it was inserted.
358     void addRange(LiveRange LR) {
359       addRangeFrom(LR, ranges.begin());
360     }
361
362     /// join - Join two live intervals (this, and other) together.  This applies
363     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
364     /// the intervals are not joinable, this aborts.
365     void join(LiveInterval &Other, const int *ValNoAssignments,
366               const int *RHSValNoAssignments,
367               SmallVector<VNInfo*, 16> &NewVNInfo);
368
369     /// removeRange - Remove the specified range from this interval.  Note that
370     /// the range must already be in this interval in its entirety.
371     void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
372
373     void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
374       removeRange(LR.start, LR.end, RemoveDeadValNo);
375     }
376
377     /// removeValNo - Remove all the ranges defined by the specified value#.
378     /// Also remove the value# from value# list.
379     void removeValNo(VNInfo *ValNo);
380
381     /// getSize - Returns the sum of sizes of all the LiveRange's.
382     ///
383     unsigned getSize() const;
384
385     bool operator<(const LiveInterval& other) const {
386       return beginNumber() < other.beginNumber();
387     }
388
389     void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
390     void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
391       if (OS) print(*OS, TRI);
392     }
393     void dump() const;
394
395   private:
396     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
397     void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
398     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
399     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
400   };
401
402   inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {
403     LI.print(OS);
404     return OS;
405   }
406 }
407
408 #endif