1 //===---- LiveRangeEdit.h - Basic tools for split and spill -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The LiveRangeEdit class represents changes done to a virtual register when it
11 // is spilled or split.
13 // The parent register is never changed. Instead, a number of new virtual
14 // registers are created and added to the newRegs vector.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
19 #define LLVM_CODEGEN_LIVERANGEEDIT_H
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/CodeGen/LiveInterval.h"
24 #include "llvm/Target/TargetMachine.h"
30 class MachineBlockFrequencyInfo;
31 class MachineLoopInfo;
32 class MachineRegisterInfo;
37 /// Callback methods for LiveRangeEdit owners.
39 virtual void anchor();
41 /// Called immediately before erasing a dead machine instruction.
42 virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
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; }
48 /// Called before shrinking the live range of a virtual register.
49 virtual void LRE_WillShrinkVirtReg(unsigned) {}
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) {}
55 virtual ~Delegate() {}
60 SmallVectorImpl<LiveInterval*> &NewRegs;
61 MachineRegisterInfo &MRI;
64 const TargetInstrInfo &TII;
65 Delegate *const TheDelegate;
67 /// FirstNew - Index of the first register added to NewRegs.
68 const unsigned FirstNew;
70 /// ScannedRemattable - true when remattable values have been identified.
71 bool ScannedRemattable;
73 /// Remattable - Values defined by remattable instructions as identified by
74 /// tii.isTriviallyReMaterializable().
75 SmallPtrSet<const VNInfo*,4> Remattable;
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;
81 /// scanRemattable - Identify the Parent values that may rematerialize.
82 void scanRemattable(AliasAnalysis *aa);
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;
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);
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,
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) {}
116 LiveInterval &getParent() const {
117 assert(Parent && "No parent LiveInterval");
120 unsigned getReg() const { return getParent().reg; }
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]; }
130 ArrayRef<LiveInterval*> regs() const {
131 return makeArrayRef(NewRegs).slice(FirstNew);
134 /// createFrom - Create a new virtual register based on OldReg.
135 LiveInterval &createFrom(unsigned OldReg);
137 /// create - Create a new register with the same class and original slot as
139 LiveInterval &create() {
140 return createFrom(getReg());
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*);
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,
153 /// Remat - Information needed to rematerialize at a specific location.
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) {}
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,
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,
175 const TargetRegisterInfo&,
178 /// markRematerialized - explicitly mark a value as rematerialized after doing
180 void markRematerialized(const VNInfo *ParentVNI) {
181 Rematted.insert(ParentVNI);
184 /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
185 bool didRematerialize(const VNInfo *ParentVNI) const {
186 return Rematted.count(ParentVNI);
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);
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);
202 /// calculateRegClassAndHint - Recompute register class and hint for each new
204 void calculateRegClassAndHint(MachineFunction&,
205 const MachineLoopInfo&,
206 const MachineBlockFrequencyInfo&);