Change the Spiller interface to take a LiveRangeEdit reference.
[oota-llvm.git] / lib / CodeGen / LiveRangeEdit.h
1 //===---- LiveRangeEdit.h - Basic tools for split and spill -----*- 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 // The LiveRangeEdit class represents changes done to a virtual register when it
11 // is spilled or split.
12 //
13 // The parent register is never changed. Instead, a number of new virtual
14 // registers are created and added to the newRegs vector.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
19 #define LLVM_CODEGEN_LIVERANGEEDIT_H
20
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23
24 namespace llvm {
25
26 class AliasAnalysis;
27 class LiveIntervals;
28 class MachineRegisterInfo;
29 class VirtRegMap;
30
31 class LiveRangeEdit {
32 public:
33   /// Callback methods for LiveRangeEdit owners.
34   struct Delegate {
35     /// Called immediately before erasing a dead machine instruction.
36     virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
37     virtual ~Delegate() {}
38   };
39
40 private:
41   LiveInterval &parent_;
42   SmallVectorImpl<LiveInterval*> &newRegs_;
43   Delegate *const delegate_;
44   const SmallVectorImpl<LiveInterval*> *uselessRegs_;
45
46   /// firstNew_ - Index of the first register added to newRegs_.
47   const unsigned firstNew_;
48
49   /// scannedRemattable_ - true when remattable values have been identified.
50   bool scannedRemattable_;
51
52   /// remattable_ - Values defined by remattable instructions as identified by
53   /// tii.isTriviallyReMaterializable().
54   SmallPtrSet<const VNInfo*,4> remattable_;
55
56   /// rematted_ - Values that were actually rematted, and so need to have their
57   /// live range trimmed or entirely removed.
58   SmallPtrSet<const VNInfo*,4> rematted_;
59
60   /// scanRemattable - Identify the parent_ values that may rematerialize.
61   void scanRemattable(LiveIntervals &lis,
62                       const TargetInstrInfo &tii,
63                       AliasAnalysis *aa);
64
65   /// allUsesAvailableAt - Return true if all registers used by OrigMI at
66   /// OrigIdx are also available with the same value at UseIdx.
67   bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
68                           SlotIndex UseIdx, LiveIntervals &lis);
69
70 public:
71   /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
72   /// @param parent The register being spilled or split.
73   /// @param newRegs List to receive any new registers created. This needn't be
74   ///                empty initially, any existing registers are ignored.
75   /// @param uselessRegs List of registers that can't be used when
76   ///        rematerializing values because they are about to be removed.
77   LiveRangeEdit(LiveInterval &parent,
78                 SmallVectorImpl<LiveInterval*> &newRegs,
79                 Delegate *delegate = 0,
80                 const SmallVectorImpl<LiveInterval*> *uselessRegs = 0)
81     : parent_(parent), newRegs_(newRegs),
82       delegate_(delegate),
83       uselessRegs_(uselessRegs),
84       firstNew_(newRegs.size()),
85       scannedRemattable_(false) {}
86
87   LiveInterval &getParent() const { return parent_; }
88   unsigned getReg() const { return parent_.reg; }
89
90   /// Iterator for accessing the new registers added by this edit.
91   typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
92   iterator begin() const { return newRegs_.begin()+firstNew_; }
93   iterator end() const { return newRegs_.end(); }
94   unsigned size() const { return newRegs_.size()-firstNew_; }
95   bool empty() const { return size() == 0; }
96   LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
97
98   /// FIXME: Temporary accessors until we can get rid of
99   /// LiveIntervals::AddIntervalsForSpills
100   SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; }
101   const SmallVectorImpl<LiveInterval*> *getUselessVRegs() {
102     return uselessRegs_;
103   }
104
105   /// create - Create a new register with the same class and stack slot as
106   /// parent.
107   LiveInterval &create(MachineRegisterInfo&, LiveIntervals&, VirtRegMap&);
108
109   /// anyRematerializable - Return true if any parent values may be
110   /// rematerializable.
111   /// This function must be called before any rematerialization is attempted.
112   bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
113                            AliasAnalysis*);
114
115   /// Remat - Information needed to rematerialize at a specific location.
116   struct Remat {
117     VNInfo *ParentVNI;      // parent_'s value at the remat location.
118     MachineInstr *OrigMI;   // Instruction defining ParentVNI.
119     explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
120   };
121
122   /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
123   /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
124   /// When cheapAsAMove is set, only cheap remats are allowed.
125   bool canRematerializeAt(Remat &RM,
126                           SlotIndex UseIdx,
127                           bool cheapAsAMove,
128                           LiveIntervals &lis);
129
130   /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
131   /// instruction into MBB before MI. The new instruction is mapped, but
132   /// liveness is not updated.
133   /// Return the SlotIndex of the new instruction.
134   SlotIndex rematerializeAt(MachineBasicBlock &MBB,
135                             MachineBasicBlock::iterator MI,
136                             unsigned DestReg,
137                             const Remat &RM,
138                             LiveIntervals&,
139                             const TargetInstrInfo&,
140                             const TargetRegisterInfo&);
141
142   /// markRematerialized - explicitly mark a value as rematerialized after doing
143   /// it manually.
144   void markRematerialized(const VNInfo *ParentVNI) {
145     rematted_.insert(ParentVNI);
146   }
147
148   /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
149   bool didRematerialize(const VNInfo *ParentVNI) const {
150     return rematted_.count(ParentVNI);
151   }
152
153   /// eliminateDeadDefs - Try to delete machine instructions that are now dead
154   /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
155   /// and further dead efs to be eliminated.
156   void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
157                          LiveIntervals&,
158                          const TargetInstrInfo&);
159
160 };
161
162 }
163
164 #endif