Defer SplitKit value mapping until all defs are available.
[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/IntEqClasses.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/AlignOf.h"
27 #include "llvm/CodeGen/SlotIndexes.h"
28 #include <cassert>
29 #include <climits>
30
31 namespace llvm {
32   class LiveIntervals;
33   class MachineInstr;
34   class MachineRegisterInfo;
35   class TargetRegisterInfo;
36   class raw_ostream;
37
38   /// VNInfo - Value Number Information.
39   /// This class holds information about a machine level values, including
40   /// definition and use points.
41   ///
42   class VNInfo {
43   private:
44     enum {
45       HAS_PHI_KILL    = 1,
46       REDEF_BY_EC     = 1 << 1,
47       IS_PHI_DEF      = 1 << 2,
48       IS_UNUSED       = 1 << 3
49     };
50
51     MachineInstr *copy;
52     unsigned char flags;
53
54   public:
55     typedef BumpPtrAllocator Allocator;
56
57     /// The ID number of this value.
58     unsigned id;
59
60     /// The index of the defining instruction (if isDefAccurate() returns true).
61     SlotIndex def;
62
63     /// VNInfo constructor.
64     VNInfo(unsigned i, SlotIndex d, MachineInstr *c)
65       : copy(c), flags(0), id(i), def(d)
66     { }
67
68     /// VNInfo construtor, copies values from orig, except for the value number.
69     VNInfo(unsigned i, const VNInfo &orig)
70       : copy(orig.copy), flags(orig.flags), id(i), def(orig.def)
71     { }
72
73     /// Copy from the parameter into this VNInfo.
74     void copyFrom(VNInfo &src) {
75       flags = src.flags;
76       copy = src.copy;
77       def = src.def;
78     }
79
80     /// Used for copying value number info.
81     unsigned getFlags() const { return flags; }
82     void setFlags(unsigned flags) { this->flags = flags; }
83
84     /// Merge flags from another VNInfo
85     void mergeFlags(const VNInfo *VNI) {
86       flags = (flags | VNI->flags) & ~IS_UNUSED;
87     }
88
89     /// For a register interval, if this VN was definied by a copy instr
90     /// getCopy() returns a pointer to it, otherwise returns 0.
91     /// For a stack interval the behaviour of this method is undefined.
92     MachineInstr* getCopy() const { return copy; }
93     /// For a register interval, set the copy member.
94     /// This method should not be called on stack intervals as it may lead to
95     /// undefined behavior.
96     void setCopy(MachineInstr *c) { copy = c; }
97
98     /// isDefByCopy - Return true when this value was defined by a copy-like
99     /// instruction as determined by MachineInstr::isCopyLike.
100     bool isDefByCopy() const { return copy != 0; }
101
102     /// Returns true if one or more kills are PHI nodes.
103     bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
104     /// Set the PHI kill flag on this value.
105     void setHasPHIKill(bool hasKill) {
106       if (hasKill)
107         flags |= HAS_PHI_KILL;
108       else
109         flags &= ~HAS_PHI_KILL;
110     }
111
112     /// Returns true if this value is re-defined by an early clobber somewhere
113     /// during the live range.
114     bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
115     /// Set the "redef by early clobber" flag on this value.
116     void setHasRedefByEC(bool hasRedef) {
117       if (hasRedef)
118         flags |= REDEF_BY_EC;
119       else
120         flags &= ~REDEF_BY_EC;
121     }
122
123     /// Returns true if this value is defined by a PHI instruction (or was,
124     /// PHI instrucions may have been eliminated).
125     bool isPHIDef() const { return flags & IS_PHI_DEF; }
126     /// Set the "phi def" flag on this value.
127     void setIsPHIDef(bool phiDef) {
128       if (phiDef)
129         flags |= IS_PHI_DEF;
130       else
131         flags &= ~IS_PHI_DEF;
132     }
133
134     /// Returns true if this value is unused.
135     bool isUnused() const { return flags & IS_UNUSED; }
136     /// Set the "is unused" flag on this value.
137     void setIsUnused(bool unused) {
138       if (unused)
139         flags |= IS_UNUSED;
140       else
141         flags &= ~IS_UNUSED;
142     }
143   };
144
145   /// LiveRange structure - This represents a simple register range in the
146   /// program, with an inclusive start point and an exclusive end point.
147   /// These ranges are rendered as [start,end).
148   struct LiveRange {
149     SlotIndex start;  // Start point of the interval (inclusive)
150     SlotIndex end;    // End point of the interval (exclusive)
151     VNInfo *valno;   // identifier for the value contained in this interval.
152
153     LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
154       : start(S), end(E), valno(V) {
155
156       assert(S < E && "Cannot create empty or backwards range");
157     }
158
159     /// contains - Return true if the index is covered by this range.
160     ///
161     bool contains(SlotIndex I) const {
162       return start <= I && I < end;
163     }
164
165     /// containsRange - Return true if the given range, [S, E), is covered by
166     /// this range.
167     bool containsRange(SlotIndex S, SlotIndex E) const {
168       assert((S < E) && "Backwards interval?");
169       return (start <= S && S < end) && (start < E && E <= end);
170     }
171
172     bool operator<(const LiveRange &LR) const {
173       return start < LR.start || (start == LR.start && end < LR.end);
174     }
175     bool operator==(const LiveRange &LR) const {
176       return start == LR.start && end == LR.end;
177     }
178
179     void dump() const;
180     void print(raw_ostream &os) const;
181
182   private:
183     LiveRange(); // DO NOT IMPLEMENT
184   };
185
186   template <> struct isPodLike<LiveRange> { static const bool value = true; };
187
188   raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
189
190
191   inline bool operator<(SlotIndex V, const LiveRange &LR) {
192     return V < LR.start;
193   }
194
195   inline bool operator<(const LiveRange &LR, SlotIndex V) {
196     return LR.start < V;
197   }
198
199   /// LiveInterval - This class represents some number of live ranges for a
200   /// register or value.  This class also contains a bit of register allocator
201   /// state.
202   class LiveInterval {
203   public:
204
205     typedef SmallVector<LiveRange,4> Ranges;
206     typedef SmallVector<VNInfo*,4> VNInfoList;
207
208     const unsigned reg;  // the register or stack slot of this interval.
209     float weight;        // weight of this interval
210     Ranges ranges;       // the ranges in which this register is live
211     VNInfoList valnos;   // value#'s
212
213     struct InstrSlots {
214       enum {
215         LOAD  = 0,
216         USE   = 1,
217         DEF   = 2,
218         STORE = 3,
219         NUM   = 4
220       };
221
222     };
223
224     LiveInterval(unsigned Reg, float Weight)
225       : reg(Reg), weight(Weight) {}
226
227     typedef Ranges::iterator iterator;
228     iterator begin() { return ranges.begin(); }
229     iterator end()   { return ranges.end(); }
230
231     typedef Ranges::const_iterator const_iterator;
232     const_iterator begin() const { return ranges.begin(); }
233     const_iterator end() const  { return ranges.end(); }
234
235     typedef VNInfoList::iterator vni_iterator;
236     vni_iterator vni_begin() { return valnos.begin(); }
237     vni_iterator vni_end() { return valnos.end(); }
238
239     typedef VNInfoList::const_iterator const_vni_iterator;
240     const_vni_iterator vni_begin() const { return valnos.begin(); }
241     const_vni_iterator vni_end() const { return valnos.end(); }
242
243     /// advanceTo - Advance the specified iterator to point to the LiveRange
244     /// containing the specified position, or end() if the position is past the
245     /// end of the interval.  If no LiveRange contains this position, but the
246     /// position is in a hole, this method returns an iterator pointing to the
247     /// LiveRange immediately after the hole.
248     iterator advanceTo(iterator I, SlotIndex Pos) {
249       assert(I != end());
250       if (Pos >= endIndex())
251         return end();
252       while (I->end <= Pos) ++I;
253       return I;
254     }
255
256     /// find - Return an iterator pointing to the first range that ends after
257     /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
258     /// when searching large intervals.
259     ///
260     /// If Pos is contained in a LiveRange, that range is returned.
261     /// If Pos is in a hole, the following LiveRange is returned.
262     /// If Pos is beyond endIndex, end() is returned.
263     iterator find(SlotIndex Pos);
264
265     const_iterator find(SlotIndex Pos) const {
266       return const_cast<LiveInterval*>(this)->find(Pos);
267     }
268
269     void clear() {
270       valnos.clear();
271       ranges.clear();
272     }
273
274     bool hasAtLeastOneValue() const { return !valnos.empty(); }
275
276     bool containsOneValue() const { return valnos.size() == 1; }
277
278     unsigned getNumValNums() const { return (unsigned)valnos.size(); }
279
280     /// getValNumInfo - Returns pointer to the specified val#.
281     ///
282     inline VNInfo *getValNumInfo(unsigned ValNo) {
283       return valnos[ValNo];
284     }
285     inline const VNInfo *getValNumInfo(unsigned ValNo) const {
286       return valnos[ValNo];
287     }
288
289     /// getNextValue - Create a new value number and return it.  MIIdx specifies
290     /// the instruction that defines the value number.
291     VNInfo *getNextValue(SlotIndex def, MachineInstr *CopyMI,
292                          VNInfo::Allocator &VNInfoAllocator) {
293       VNInfo *VNI =
294         new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def, CopyMI);
295       valnos.push_back(VNI);
296       return VNI;
297     }
298
299     /// Create a copy of the given value. The new value will be identical except
300     /// for the Value number.
301     VNInfo *createValueCopy(const VNInfo *orig,
302                             VNInfo::Allocator &VNInfoAllocator) {
303       VNInfo *VNI =
304         new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
305       valnos.push_back(VNI);
306       return VNI;
307     }
308
309     /// RenumberValues - Renumber all values in order of appearance and remove
310     /// unused values.
311     /// Recalculate phi-kill flags in case any phi-def values were removed.
312     void RenumberValues(LiveIntervals &lis);
313
314     /// isOnlyLROfValNo - Return true if the specified live range is the only
315     /// one defined by the its val#.
316     bool isOnlyLROfValNo(const LiveRange *LR) {
317       for (const_iterator I = begin(), E = end(); I != E; ++I) {
318         const LiveRange *Tmp = I;
319         if (Tmp != LR && Tmp->valno == LR->valno)
320           return false;
321       }
322       return true;
323     }
324
325     /// MergeValueNumberInto - This method is called when two value nubmers
326     /// are found to be equivalent.  This eliminates V1, replacing all
327     /// LiveRanges with the V1 value number with the V2 value number.  This can
328     /// cause merging of V1/V2 values numbers and compaction of the value space.
329     VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
330
331     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
332     /// in RHS into this live interval as the specified value number.
333     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
334     /// current interval, it will replace the value numbers of the overlaped
335     /// live ranges with the specified value number.
336     void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
337
338     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
339     /// in RHS into this live interval as the specified value number.
340     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
341     /// current interval, but only if the overlapping LiveRanges have the
342     /// specified value number.
343     void MergeValueInAsValue(const LiveInterval &RHS,
344                              const VNInfo *RHSValNo, VNInfo *LHSValNo);
345
346     /// Copy - Copy the specified live interval. This copies all the fields
347     /// except for the register of the interval.
348     void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
349               VNInfo::Allocator &VNInfoAllocator);
350
351     bool empty() const { return ranges.empty(); }
352
353     /// beginIndex - Return the lowest numbered slot covered by interval.
354     SlotIndex beginIndex() const {
355       assert(!empty() && "Call to beginIndex() on empty interval.");
356       return ranges.front().start;
357     }
358
359     /// endNumber - return the maximum point of the interval of the whole,
360     /// exclusive.
361     SlotIndex endIndex() const {
362       assert(!empty() && "Call to endIndex() on empty interval.");
363       return ranges.back().end;
364     }
365
366     bool expiredAt(SlotIndex index) const {
367       return index >= endIndex();
368     }
369
370     bool liveAt(SlotIndex index) const {
371       const_iterator r = find(index);
372       return r != end() && r->start <= index;
373     }
374
375     /// killedAt - Return true if a live range ends at index. Note that the kill
376     /// point is not contained in the half-open live range. It is usually the
377     /// getDefIndex() slot following its last use.
378     bool killedAt(SlotIndex index) const {
379       const_iterator r = find(index.getUseIndex());
380       return r != end() && r->end == index;
381     }
382
383     /// killedInRange - Return true if the interval has kills in [Start,End).
384     /// Note that the kill point is considered the end of a live range, so it is
385     /// not contained in the live range. If a live range ends at End, it won't
386     /// be counted as a kill by this method.
387     bool killedInRange(SlotIndex Start, SlotIndex End) const;
388
389     /// getLiveRangeContaining - Return the live range that contains the
390     /// specified index, or null if there is none.
391     const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
392       const_iterator I = FindLiveRangeContaining(Idx);
393       return I == end() ? 0 : &*I;
394     }
395
396     /// getLiveRangeContaining - Return the live range that contains the
397     /// specified index, or null if there is none.
398     LiveRange *getLiveRangeContaining(SlotIndex Idx) {
399       iterator I = FindLiveRangeContaining(Idx);
400       return I == end() ? 0 : &*I;
401     }
402
403     /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
404     VNInfo *getVNInfoAt(SlotIndex Idx) const {
405       const_iterator I = FindLiveRangeContaining(Idx);
406       return I == end() ? 0 : I->valno;
407     }
408
409     /// FindLiveRangeContaining - Return an iterator to the live range that
410     /// contains the specified index, or end() if there is none.
411     iterator FindLiveRangeContaining(SlotIndex Idx) {
412       iterator I = find(Idx);
413       return I != end() && I->start <= Idx ? I : end();
414     }
415
416     const_iterator FindLiveRangeContaining(SlotIndex Idx) const {
417       const_iterator I = find(Idx);
418       return I != end() && I->start <= Idx ? I : end();
419     }
420
421     /// findDefinedVNInfo - Find the by the specified
422     /// index (register interval) or defined
423     VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
424
425
426     /// overlaps - Return true if the intersection of the two live intervals is
427     /// not empty.
428     bool overlaps(const LiveInterval& other) const {
429       if (other.empty())
430         return false;
431       return overlapsFrom(other, other.begin());
432     }
433
434     /// overlaps - Return true if the live interval overlaps a range specified
435     /// by [Start, End).
436     bool overlaps(SlotIndex Start, SlotIndex End) const;
437
438     /// overlapsFrom - Return true if the intersection of the two live intervals
439     /// is not empty.  The specified iterator is a hint that we can begin
440     /// scanning the Other interval starting at I.
441     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
442
443     /// addRange - Add the specified LiveRange to this interval, merging
444     /// intervals as appropriate.  This returns an iterator to the inserted live
445     /// range (which may have grown since it was inserted.
446     void addRange(LiveRange LR) {
447       addRangeFrom(LR, ranges.begin());
448     }
449
450     /// join - Join two live intervals (this, and other) together.  This applies
451     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
452     /// the intervals are not joinable, this aborts.
453     void join(LiveInterval &Other,
454               const int *ValNoAssignments,
455               const int *RHSValNoAssignments,
456               SmallVector<VNInfo*, 16> &NewVNInfo,
457               MachineRegisterInfo *MRI);
458
459     /// isInOneLiveRange - Return true if the range specified is entirely in the
460     /// a single LiveRange of the live interval.
461     bool isInOneLiveRange(SlotIndex Start, SlotIndex End) const {
462       const_iterator r = find(Start);
463       return r != end() && r->containsRange(Start, End);
464     }
465
466     /// removeRange - Remove the specified range from this interval.  Note that
467     /// the range must be a single LiveRange in its entirety.
468     void removeRange(SlotIndex Start, SlotIndex End,
469                      bool RemoveDeadValNo = false);
470
471     void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
472       removeRange(LR.start, LR.end, RemoveDeadValNo);
473     }
474
475     /// removeValNo - Remove all the ranges defined by the specified value#.
476     /// Also remove the value# from value# list.
477     void removeValNo(VNInfo *ValNo);
478
479     /// getSize - Returns the sum of sizes of all the LiveRange's.
480     ///
481     unsigned getSize() const;
482
483     /// Returns true if the live interval is zero length, i.e. no live ranges
484     /// span instructions. It doesn't pay to spill such an interval.
485     bool isZeroLength() const {
486       for (const_iterator i = begin(), e = end(); i != e; ++i)
487         if (i->end.getPrevIndex() > i->start)
488           return false;
489       return true;
490     }
491
492     /// isSpillable - Can this interval be spilled?
493     bool isSpillable() const {
494       return weight != HUGE_VALF;
495     }
496
497     /// markNotSpillable - Mark interval as not spillable
498     void markNotSpillable() {
499       weight = HUGE_VALF;
500     }
501
502     /// ComputeJoinedWeight - Set the weight of a live interval after
503     /// Other has been merged into it.
504     void ComputeJoinedWeight(const LiveInterval &Other);
505
506     bool operator<(const LiveInterval& other) const {
507       const SlotIndex &thisIndex = beginIndex();
508       const SlotIndex &otherIndex = other.beginIndex();
509       return (thisIndex < otherIndex ||
510               (thisIndex == otherIndex && reg < other.reg));
511     }
512
513     void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
514     void dump() const;
515
516   private:
517
518     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
519     void extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd);
520     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStr);
521     void markValNoForDeletion(VNInfo *V);
522
523     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
524
525   };
526
527   inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
528     LI.print(OS);
529     return OS;
530   }
531
532   /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
533   /// LiveInterval into equivalence clases of connected components. A
534   /// LiveInterval that has multiple connected components can be broken into
535   /// multiple LiveIntervals.
536   ///
537   /// Given a LiveInterval that may have multiple connected components, run:
538   ///
539   ///   unsigned numComps = ConEQ.Classify(LI);
540   ///   if (numComps > 1) {
541   ///     // allocate numComps-1 new LiveIntervals into LIS[1..]
542   ///     ConEQ.Distribute(LIS);
543   /// }
544
545   class ConnectedVNInfoEqClasses {
546     LiveIntervals &lis_;
547     IntEqClasses eqClass_;
548
549     // Note that values a and b are connected.
550     void Connect(unsigned a, unsigned b);
551
552     unsigned Renumber();
553
554   public:
555     explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : lis_(lis) {}
556
557     /// Classify - Classify the values in LI into connected components.
558     /// Return the number of connected components.
559     unsigned Classify(const LiveInterval *LI);
560
561     /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
562     /// the equivalence class assigned the VNI.
563     unsigned getEqClass(const VNInfo *VNI) const { return eqClass_[VNI->id]; }
564
565     /// Distribute - Distribute values in LIV[0] into a separate LiveInterval
566     /// for each connected component. LIV must have a LiveInterval for each
567     /// connected component. The LiveIntervals in Liv[1..] must be empty.
568     void Distribute(LiveInterval *LIV[]);
569
570   };
571
572 }
573 #endif