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