Move more to raw_ostream, provide support for writing MachineBasicBlock,
[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 "llvm/Support/AlignOf.h"
27 #include <iosfwd>
28 #include <cassert>
29 #include <climits>
30
31 namespace llvm {
32   class MachineInstr;
33   class MachineRegisterInfo;
34   class TargetRegisterInfo;
35   class raw_ostream;
36
37   /// VNInfo - Value Number Information.
38   /// This class holds information about a machine level values, including
39   /// definition and use points.
40   ///
41   /// Care must be taken in interpreting the def index of the value. The 
42   /// following rules apply:
43   ///
44   /// If the isDefAccurate() method returns false then def does not contain the
45   /// index of the defining MachineInstr, or even (necessarily) to a
46   /// MachineInstr at all. In general such a def index is not meaningful
47   /// and should not be used. The exception is that, for values originally
48   /// defined by PHI instructions, after PHI elimination def will contain the
49   /// index of the MBB in which the PHI originally existed. This can be used
50   /// to insert code (spills or copies) which deals with the value, which will
51   /// be live in to the block.
52
53   class VNInfo {
54   private:
55     enum {
56       HAS_PHI_KILL    = 1,                         
57       REDEF_BY_EC     = 1 << 1,
58       IS_PHI_DEF      = 1 << 2,
59       IS_UNUSED       = 1 << 3,
60       IS_DEF_ACCURATE = 1 << 4
61     };
62
63     unsigned char flags;
64
65   public:
66     /// Holds information about individual kills.
67     struct KillInfo {
68       bool isPHIKill : 1;
69       unsigned killIdx : 31;
70
71       KillInfo(bool isPHIKill, unsigned killIdx)
72         : isPHIKill(isPHIKill), killIdx(killIdx) {
73
74         assert(killIdx != 0 && "Zero kill indices are no longer permitted.");
75       }
76
77     };
78
79     typedef SmallVector<KillInfo, 4> KillSet;
80
81     /// The ID number of this value.
82     unsigned id;
83     
84     /// The index of the defining instruction (if isDefAccurate() returns true).
85     unsigned def;
86     MachineInstr *copy;
87     KillSet kills;
88
89     VNInfo()
90       : flags(IS_UNUSED), id(~1U), def(0), copy(0) {}
91
92     /// VNInfo constructor.
93     /// d is presumed to point to the actual defining instr. If it doesn't
94     /// setIsDefAccurate(false) should be called after construction.
95     VNInfo(unsigned i, unsigned d, MachineInstr *c)
96       : flags(IS_DEF_ACCURATE), id(i), def(d), copy(c) {}
97
98     /// VNInfo construtor, copies values from orig, except for the value number.
99     VNInfo(unsigned i, const VNInfo &orig)
100       : flags(orig.flags), id(i), def(orig.def), copy(orig.copy),
101         kills(orig.kills) {}
102
103     /// Used for copying value number info.
104     unsigned getFlags() const { return flags; }
105     void setFlags(unsigned flags) { this->flags = flags; }
106
107     /// Returns true if one or more kills are PHI nodes.
108     bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
109     void setHasPHIKill(bool hasKill) {
110       if (hasKill)
111         flags |= HAS_PHI_KILL;
112       else
113         flags &= ~HAS_PHI_KILL;
114     }
115
116     /// Returns true if this value is re-defined by an early clobber somewhere
117     /// during the live range.
118     bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
119     void setHasRedefByEC(bool hasRedef) {
120       if (hasRedef)
121         flags |= REDEF_BY_EC;
122       else
123         flags &= ~REDEF_BY_EC;
124     }
125   
126     /// Returns true if this value is defined by a PHI instruction (or was,
127     /// PHI instrucions may have been eliminated).
128     bool isPHIDef() const { return flags & IS_PHI_DEF; }
129     void setIsPHIDef(bool phiDef) {
130       if (phiDef)
131         flags |= IS_PHI_DEF;
132       else
133         flags &= ~IS_PHI_DEF;
134     }
135
136     /// Returns true if this value is unused.
137     bool isUnused() const { return flags & IS_UNUSED; }
138     void setIsUnused(bool unused) {
139       if (unused)
140         flags |= IS_UNUSED;
141       else
142         flags &= ~IS_UNUSED;
143     }
144
145     /// Returns true if the def is accurate.
146     bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
147     void setIsDefAccurate(bool defAccurate) {
148       if (defAccurate)
149         flags |= IS_DEF_ACCURATE;
150       else 
151         flags &= ~IS_DEF_ACCURATE;
152     }
153
154   };
155
156   inline bool operator<(const VNInfo::KillInfo &k1, const VNInfo::KillInfo &k2) {
157     return k1.killIdx < k2.killIdx;
158   }
159   
160   inline bool operator<(const VNInfo::KillInfo &k, unsigned idx) {
161     return k.killIdx < idx;
162   }
163
164   inline bool operator<(unsigned idx, const VNInfo::KillInfo &k) {
165     return idx < k.killIdx;
166   }
167
168   /// LiveRange structure - This represents a simple register range in the
169   /// program, with an inclusive start point and an exclusive end point.
170   /// These ranges are rendered as [start,end).
171   struct LiveRange {
172     unsigned start;  // Start point of the interval (inclusive)
173     unsigned end;    // End point of the interval (exclusive)
174     VNInfo *valno;   // identifier for the value contained in this interval.
175
176     LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
177       assert(S < E && "Cannot create empty or backwards range");
178     }
179
180     /// contains - Return true if the index is covered by this range.
181     ///
182     bool contains(unsigned I) const {
183       return start <= I && I < end;
184     }
185
186     bool operator<(const LiveRange &LR) const {
187       return start < LR.start || (start == LR.start && end < LR.end);
188     }
189     bool operator==(const LiveRange &LR) const {
190       return start == LR.start && end == LR.end;
191     }
192
193     void dump() const;
194     void print(std::ostream &os) const;
195     void print(std::ostream *os) const { if (os) print(*os); }
196     void print(raw_ostream &os) const;
197     void print(raw_ostream *os) const { if (os) print(*os); }
198
199   private:
200     LiveRange(); // DO NOT IMPLEMENT
201   };
202
203   std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
204   raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
205
206
207   inline bool operator<(unsigned V, const LiveRange &LR) {
208     return V < LR.start;
209   }
210
211   inline bool operator<(const LiveRange &LR, unsigned V) {
212     return LR.start < V;
213   }
214
215   /// LiveInterval - This class represents some number of live ranges for a
216   /// register or value.  This class also contains a bit of register allocator
217   /// state.
218   class LiveInterval {
219   public:
220
221     typedef SmallVector<LiveRange,4> Ranges;
222     typedef SmallVector<VNInfo*,4> VNInfoList;
223
224     unsigned reg;        // the register or stack slot of this interval
225                          // if the top bits is set, it represents a stack slot.
226     float weight;        // weight of this interval
227     Ranges ranges;       // the ranges in which this register is live
228     VNInfoList valnos;   // value#'s
229     
230     struct InstrSlots {
231       enum {
232         LOAD  = 0,
233         USE   = 1,
234         DEF   = 2,
235         STORE = 3,
236         NUM   = 4
237       };
238
239       static unsigned scale(unsigned slot, unsigned factor) {
240         unsigned index = slot / NUM,
241                  offset = slot % NUM;
242         assert(index <= ~0U / (factor * NUM) &&
243                "Rescaled interval would overflow");
244         return index * NUM * factor + offset;
245       }
246
247     };
248
249     LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
250       : reg(Reg), weight(Weight) {
251       if (IsSS)
252         reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
253     }
254
255     typedef Ranges::iterator iterator;
256     iterator begin() { return ranges.begin(); }
257     iterator end()   { return ranges.end(); }
258
259     typedef Ranges::const_iterator const_iterator;
260     const_iterator begin() const { return ranges.begin(); }
261     const_iterator end() const  { return ranges.end(); }
262
263     typedef VNInfoList::iterator vni_iterator;
264     vni_iterator vni_begin() { return valnos.begin(); }
265     vni_iterator vni_end() { return valnos.end(); }
266
267     typedef VNInfoList::const_iterator const_vni_iterator;
268     const_vni_iterator vni_begin() const { return valnos.begin(); }
269     const_vni_iterator vni_end() const { return valnos.end(); }
270
271     /// advanceTo - Advance the specified iterator to point to the LiveRange
272     /// containing the specified position, or end() if the position is past the
273     /// end of the interval.  If no LiveRange contains this position, but the
274     /// position is in a hole, this method returns an iterator pointing the the
275     /// LiveRange immediately after the hole.
276     iterator advanceTo(iterator I, unsigned Pos) {
277       if (Pos >= endNumber())
278         return end();
279       while (I->end <= Pos) ++I;
280       return I;
281     }
282     
283     void clear() {
284       while (!valnos.empty()) {
285         VNInfo *VNI = valnos.back();
286         valnos.pop_back();
287         VNI->~VNInfo();
288       }
289       
290       ranges.clear();
291     }
292
293     /// isStackSlot - Return true if this is a stack slot interval.
294     ///
295     bool isStackSlot() const {
296       return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
297     }
298
299     /// getStackSlotIndex - Return stack slot index if this is a stack slot
300     /// interval.
301     int getStackSlotIndex() const {
302       assert(isStackSlot() && "Interval is not a stack slot interval!");
303       return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
304     }
305
306     bool hasAtLeastOneValue() const { return !valnos.empty(); }
307
308     bool containsOneValue() const { return valnos.size() == 1; }
309
310     unsigned getNumValNums() const { return (unsigned)valnos.size(); }
311     
312     /// getValNumInfo - Returns pointer to the specified val#.
313     ///
314     inline VNInfo *getValNumInfo(unsigned ValNo) {
315       return valnos[ValNo];
316     }
317     inline const VNInfo *getValNumInfo(unsigned ValNo) const {
318       return valnos[ValNo];
319     }
320     
321     /// copyValNumInfo - Copy the value number info for one value number to
322     /// another.
323     void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
324       DstValNo->def = SrcValNo->def;
325       DstValNo->copy = SrcValNo->copy;
326       DstValNo->setFlags(SrcValNo->getFlags());
327       DstValNo->kills = SrcValNo->kills;
328     }
329
330     /// getNextValue - Create a new value number and return it.  MIIdx specifies
331     /// the instruction that defines the value number.
332     VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
333                          bool isDefAccurate, BumpPtrAllocator &VNInfoAllocator) {
334
335       assert(MIIdx != ~0u && MIIdx != ~1u &&
336              "PHI def / unused flags should now be passed explicitly.");
337       VNInfo *VNI =
338         static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
339                                                       alignof<VNInfo>()));
340       new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
341       VNI->setIsDefAccurate(isDefAccurate);
342       valnos.push_back(VNI);
343       return VNI;
344     }
345
346     /// Create a copy of the given value. The new value will be identical except
347     /// for the Value number.
348     VNInfo *createValueCopy(const VNInfo *orig, BumpPtrAllocator &VNInfoAllocator) {
349
350       VNInfo *VNI =
351         static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
352                                                       alignof<VNInfo>()));
353     
354       new (VNI) VNInfo((unsigned)valnos.size(), *orig);
355       valnos.push_back(VNI);
356       return VNI;
357     }
358
359     /// addKill - Add a kill instruction index to the specified value
360     /// number.
361     static void addKill(VNInfo *VNI, unsigned KillIdx, bool phiKill) {
362       VNInfo::KillSet &kills = VNI->kills;
363       VNInfo::KillInfo newKill(phiKill, KillIdx);
364       if (kills.empty()) {
365         kills.push_back(newKill);
366       } else {
367         VNInfo::KillSet::iterator
368           I = std::lower_bound(kills.begin(), kills.end(), newKill);
369         kills.insert(I, newKill);
370       }
371     }
372
373     /// addKills - Add a number of kills into the VNInfo kill vector. If this
374     /// interval is live at a kill point, then the kill is not added.
375     void addKills(VNInfo *VNI, const VNInfo::KillSet &kills) {
376       for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
377            i != e; ++i) {
378         const VNInfo::KillInfo &Kill = kills[i];
379         if (!liveBeforeAndAt(Kill.killIdx)) {
380           VNInfo::KillSet::iterator
381             I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), Kill);
382           VNI->kills.insert(I, Kill);
383         }
384       }
385     }
386
387     /// removeKill - Remove the specified kill from the list of kills of
388     /// the specified val#.
389     static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
390       VNInfo::KillSet &kills = VNI->kills;
391       VNInfo::KillSet::iterator
392         I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
393       if (I != kills.end() && I->killIdx == KillIdx) {
394         kills.erase(I);
395         return true;
396       }
397       return false;
398     }
399
400     /// removeKills - Remove all the kills in specified range
401     /// [Start, End] of the specified val#.
402     static void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
403       VNInfo::KillSet &kills = VNI->kills;
404
405       VNInfo::KillSet::iterator
406         I = std::lower_bound(kills.begin(), kills.end(), Start);
407       VNInfo::KillSet::iterator
408         E = std::upper_bound(kills.begin(), kills.end(), End);
409       kills.erase(I, E);
410     }
411
412     /// isKill - Return true if the specified index is a kill of the
413     /// specified val#.
414     static bool isKill(const VNInfo *VNI, unsigned KillIdx) {
415       const VNInfo::KillSet &kills = VNI->kills;
416       VNInfo::KillSet::const_iterator
417         I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
418       return I != kills.end() && I->killIdx == KillIdx;
419     }
420
421     /// isOnlyLROfValNo - Return true if the specified live range is the only
422     /// one defined by the its val#.
423     bool isOnlyLROfValNo(const LiveRange *LR) {
424       for (const_iterator I = begin(), E = end(); I != E; ++I) {
425         const LiveRange *Tmp = I;
426         if (Tmp != LR && Tmp->valno == LR->valno)
427           return false;
428       }
429       return true;
430     }
431     
432     /// MergeValueNumberInto - This method is called when two value nubmers
433     /// are found to be equivalent.  This eliminates V1, replacing all
434     /// LiveRanges with the V1 value number with the V2 value number.  This can
435     /// cause merging of V1/V2 values numbers and compaction of the value space.
436     VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
437
438     /// MergeInClobberRanges - For any live ranges that are not defined in the
439     /// current interval, but are defined in the Clobbers interval, mark them
440     /// used with an unknown definition value. Caller must pass in reference to
441     /// VNInfoAllocator since it will create a new val#.
442     void MergeInClobberRanges(const LiveInterval &Clobbers,
443                               BumpPtrAllocator &VNInfoAllocator);
444
445     /// MergeInClobberRange - Same as MergeInClobberRanges except it merge in a
446     /// single LiveRange only.
447     void MergeInClobberRange(unsigned Start, unsigned End,
448                              BumpPtrAllocator &VNInfoAllocator);
449
450     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
451     /// in RHS into this live interval as the specified value number.
452     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
453     /// current interval, it will replace the value numbers of the overlaped
454     /// live ranges with the specified value number.
455     void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
456
457     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
458     /// in RHS into this live interval as the specified value number.
459     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
460     /// current interval, but only if the overlapping LiveRanges have the
461     /// specified value number.
462     void MergeValueInAsValue(const LiveInterval &RHS,
463                              const VNInfo *RHSValNo, VNInfo *LHSValNo);
464
465     /// Copy - Copy the specified live interval. This copies all the fields
466     /// except for the register of the interval.
467     void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
468               BumpPtrAllocator &VNInfoAllocator);
469     
470     bool empty() const { return ranges.empty(); }
471
472     /// beginNumber - Return the lowest numbered slot covered by interval.
473     unsigned beginNumber() const {
474       if (empty())
475         return 0;
476       return ranges.front().start;
477     }
478
479     /// endNumber - return the maximum point of the interval of the whole,
480     /// exclusive.
481     unsigned endNumber() const {
482       if (empty())
483         return 0;
484       return ranges.back().end;
485     }
486
487     bool expiredAt(unsigned index) const {
488       return index >= endNumber();
489     }
490
491     bool liveAt(unsigned index) const;
492
493     // liveBeforeAndAt - Check if the interval is live at the index and the
494     // index just before it. If index is liveAt, check if it starts a new live
495     // range.If it does, then check if the previous live range ends at index-1.
496     bool liveBeforeAndAt(unsigned index) const;
497
498     /// getLiveRangeContaining - Return the live range that contains the
499     /// specified index, or null if there is none.
500     const LiveRange *getLiveRangeContaining(unsigned Idx) const {
501       const_iterator I = FindLiveRangeContaining(Idx);
502       return I == end() ? 0 : &*I;
503     }
504
505     /// getLiveRangeContaining - Return the live range that contains the
506     /// specified index, or null if there is none.
507     LiveRange *getLiveRangeContaining(unsigned Idx) {
508       iterator I = FindLiveRangeContaining(Idx);
509       return I == end() ? 0 : &*I;
510     }
511
512     /// FindLiveRangeContaining - Return an iterator to the live range that
513     /// contains the specified index, or end() if there is none.
514     const_iterator FindLiveRangeContaining(unsigned Idx) const;
515
516     /// FindLiveRangeContaining - Return an iterator to the live range that
517     /// contains the specified index, or end() if there is none.
518     iterator FindLiveRangeContaining(unsigned Idx);
519
520     /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
521     /// index (register interval) or defined by the specified register (stack
522     /// inteval).
523     VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
524     
525     /// overlaps - Return true if the intersection of the two live intervals is
526     /// not empty.
527     bool overlaps(const LiveInterval& other) const {
528       return overlapsFrom(other, other.begin());
529     }
530
531     /// overlaps - Return true if the live interval overlaps a range specified
532     /// by [Start, End).
533     bool overlaps(unsigned Start, unsigned End) const;
534
535     /// overlapsFrom - Return true if the intersection of the two live intervals
536     /// is not empty.  The specified iterator is a hint that we can begin
537     /// scanning the Other interval starting at I.
538     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
539
540     /// addRange - Add the specified LiveRange to this interval, merging
541     /// intervals as appropriate.  This returns an iterator to the inserted live
542     /// range (which may have grown since it was inserted.
543     void addRange(LiveRange LR) {
544       addRangeFrom(LR, ranges.begin());
545     }
546
547     /// join - Join two live intervals (this, and other) together.  This applies
548     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
549     /// the intervals are not joinable, this aborts.
550     void join(LiveInterval &Other, const int *ValNoAssignments,
551               const int *RHSValNoAssignments,
552               SmallVector<VNInfo*, 16> &NewVNInfo,
553               MachineRegisterInfo *MRI);
554
555     /// isInOneLiveRange - Return true if the range specified is entirely in the
556     /// a single LiveRange of the live interval.
557     bool isInOneLiveRange(unsigned Start, unsigned End);
558
559     /// removeRange - Remove the specified range from this interval.  Note that
560     /// the range must be a single LiveRange in its entirety.
561     void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
562
563     void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
564       removeRange(LR.start, LR.end, RemoveDeadValNo);
565     }
566
567     /// removeValNo - Remove all the ranges defined by the specified value#.
568     /// Also remove the value# from value# list.
569     void removeValNo(VNInfo *ValNo);
570
571     /// scaleNumbering - Renumber VNI and ranges to provide gaps for new
572     /// instructions.
573     void scaleNumbering(unsigned factor);
574
575     /// getSize - Returns the sum of sizes of all the LiveRange's.
576     ///
577     unsigned getSize() const;
578
579     /// ComputeJoinedWeight - Set the weight of a live interval after
580     /// Other has been merged into it.
581     void ComputeJoinedWeight(const LiveInterval &Other);
582
583     bool operator<(const LiveInterval& other) const {
584       return beginNumber() < other.beginNumber();
585     }
586
587     void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
588     void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
589       if (OS) print(*OS, TRI);
590     }
591     void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
592     void print(raw_ostream *OS, const TargetRegisterInfo *TRI = 0) const {
593       if (OS) print(*OS, TRI);
594     }
595     void dump() const;
596
597   private:
598
599     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
600     void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
601     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
602     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
603
604   };
605
606   inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {
607     LI.print(OS);
608     return OS;
609   }
610   inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
611     LI.print(OS);
612     return OS;
613   }
614 }
615
616 #endif