Switch spill weights from a basic loop depth estimation to BlockFrequencyInfo.
[oota-llvm.git] / include / llvm / 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/ADT/ArrayRef.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/CodeGen/LiveInterval.h"
24 #include "llvm/Target/TargetMachine.h"
25
26 namespace llvm {
27
28 class AliasAnalysis;
29 class LiveIntervals;
30 class MachineBlockFrequencyInfo;
31 class MachineLoopInfo;
32 class MachineRegisterInfo;
33 class VirtRegMap;
34
35 class LiveRangeEdit {
36 public:
37   /// Callback methods for LiveRangeEdit owners.
38   class Delegate {
39     virtual void anchor();
40   public:
41     /// Called immediately before erasing a dead machine instruction.
42     virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
43
44     /// Called when a virtual register is no longer used. Return false to defer
45     /// its deletion from LiveIntervals.
46     virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
47
48     /// Called before shrinking the live range of a virtual register.
49     virtual void LRE_WillShrinkVirtReg(unsigned) {}
50
51     /// Called after cloning a virtual register.
52     /// This is used for new registers representing connected components of Old.
53     virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {}
54
55     virtual ~Delegate() {}
56   };
57
58 private:
59   LiveInterval *Parent;
60   SmallVectorImpl<LiveInterval*> &NewRegs;
61   MachineRegisterInfo &MRI;
62   LiveIntervals &LIS;
63   VirtRegMap *VRM;
64   const TargetInstrInfo &TII;
65   Delegate *const TheDelegate;
66
67   /// FirstNew - Index of the first register added to NewRegs.
68   const unsigned FirstNew;
69
70   /// ScannedRemattable - true when remattable values have been identified.
71   bool ScannedRemattable;
72
73   /// Remattable - Values defined by remattable instructions as identified by
74   /// tii.isTriviallyReMaterializable().
75   SmallPtrSet<const VNInfo*,4> Remattable;
76
77   /// Rematted - Values that were actually rematted, and so need to have their
78   /// live range trimmed or entirely removed.
79   SmallPtrSet<const VNInfo*,4> Rematted;
80
81   /// scanRemattable - Identify the Parent values that may rematerialize.
82   void scanRemattable(AliasAnalysis *aa);
83
84   /// allUsesAvailableAt - Return true if all registers used by OrigMI at
85   /// OrigIdx are also available with the same value at UseIdx.
86   bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
87                           SlotIndex UseIdx) const;
88
89   /// foldAsLoad - If LI has a single use and a single def that can be folded as
90   /// a load, eliminate the register by folding the def into the use.
91   bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead);
92
93 public:
94   /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
95   /// @param parent The register being spilled or split.
96   /// @param newRegs List to receive any new registers created. This needn't be
97   ///                empty initially, any existing registers are ignored.
98   /// @param MF The MachineFunction the live range edit is taking place in.
99   /// @param lis The collection of all live intervals in this function.
100   /// @param vrm Map of virtual registers to physical registers for this
101   ///            function.  If NULL, no virtual register map updates will
102   ///            be done.  This could be the case if called before Regalloc.
103   LiveRangeEdit(LiveInterval *parent,
104                 SmallVectorImpl<LiveInterval*> &newRegs,
105                 MachineFunction &MF,
106                 LiveIntervals &lis,
107                 VirtRegMap *vrm,
108                 Delegate *delegate = 0)
109     : Parent(parent), NewRegs(newRegs),
110       MRI(MF.getRegInfo()), LIS(lis), VRM(vrm),
111       TII(*MF.getTarget().getInstrInfo()),
112       TheDelegate(delegate),
113       FirstNew(newRegs.size()),
114       ScannedRemattable(false) {}
115
116   LiveInterval &getParent() const {
117    assert(Parent && "No parent LiveInterval");
118    return *Parent;
119   }
120   unsigned getReg() const { return getParent().reg; }
121
122   /// Iterator for accessing the new registers added by this edit.
123   typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
124   iterator begin() const { return NewRegs.begin()+FirstNew; }
125   iterator end() const { return NewRegs.end(); }
126   unsigned size() const { return NewRegs.size()-FirstNew; }
127   bool empty() const { return size() == 0; }
128   LiveInterval *get(unsigned idx) const { return NewRegs[idx+FirstNew]; }
129
130   ArrayRef<LiveInterval*> regs() const {
131     return makeArrayRef(NewRegs).slice(FirstNew);
132   }
133
134   /// createFrom - Create a new virtual register based on OldReg.
135   LiveInterval &createFrom(unsigned OldReg);
136
137   /// create - Create a new register with the same class and original slot as
138   /// parent.
139   LiveInterval &create() {
140     return createFrom(getReg());
141   }
142
143   /// anyRematerializable - Return true if any parent values may be
144   /// rematerializable.
145   /// This function must be called before any rematerialization is attempted.
146   bool anyRematerializable(AliasAnalysis*);
147
148   /// checkRematerializable - Manually add VNI to the list of rematerializable
149   /// values if DefMI may be rematerializable.
150   bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
151                              AliasAnalysis*);
152
153   /// Remat - Information needed to rematerialize at a specific location.
154   struct Remat {
155     VNInfo *ParentVNI;      // parent_'s value at the remat location.
156     MachineInstr *OrigMI;   // Instruction defining ParentVNI.
157     explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
158   };
159
160   /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
161   /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
162   /// When cheapAsAMove is set, only cheap remats are allowed.
163   bool canRematerializeAt(Remat &RM,
164                           SlotIndex UseIdx,
165                           bool cheapAsAMove);
166
167   /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
168   /// instruction into MBB before MI. The new instruction is mapped, but
169   /// liveness is not updated.
170   /// Return the SlotIndex of the new instruction.
171   SlotIndex rematerializeAt(MachineBasicBlock &MBB,
172                             MachineBasicBlock::iterator MI,
173                             unsigned DestReg,
174                             const Remat &RM,
175                             const TargetRegisterInfo&,
176                             bool Late = false);
177
178   /// markRematerialized - explicitly mark a value as rematerialized after doing
179   /// it manually.
180   void markRematerialized(const VNInfo *ParentVNI) {
181     Rematted.insert(ParentVNI);
182   }
183
184   /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
185   bool didRematerialize(const VNInfo *ParentVNI) const {
186     return Rematted.count(ParentVNI);
187   }
188
189   /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
190   /// to erase it from LIS.
191   void eraseVirtReg(unsigned Reg);
192
193   /// eliminateDeadDefs - Try to delete machine instructions that are now dead
194   /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
195   /// and further dead efs to be eliminated.
196   /// RegsBeingSpilled lists registers currently being spilled by the register
197   /// allocator.  These registers should not be split into new intervals
198   /// as currently those new intervals are not guaranteed to spill.
199   void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
200                          ArrayRef<unsigned> RegsBeingSpilled = None);
201
202   /// calculateRegClassAndHint - Recompute register class and hint for each new
203   /// register.
204   void calculateRegClassAndHint(MachineFunction&,
205                                 const MachineLoopInfo&,
206                                 const MachineBlockFrequencyInfo&);
207 };
208
209 }
210
211 #endif