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/CodeGen/LiveInterval.h"
22 #include "llvm/ADT/SmallPtrSet.h"
28 class MachineLoopInfo;
29 class MachineRegisterInfo;
34 /// Callback methods for LiveRangeEdit owners.
36 /// Called immediately before erasing a dead machine instruction.
37 virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
39 /// Called when a virtual register is no longer used. Return false to defer
40 /// its deletion from LiveIntervals.
41 virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
43 /// Called before shrinking the live range of a virtual register.
44 virtual void LRE_WillShrinkVirtReg(unsigned) {}
46 /// Called after cloning a virtual register.
47 /// This is used for new registers representing connected components of Old.
48 virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {}
50 virtual ~Delegate() {}
54 LiveInterval &parent_;
55 SmallVectorImpl<LiveInterval*> &newRegs_;
56 Delegate *const delegate_;
57 const SmallVectorImpl<LiveInterval*> *uselessRegs_;
59 /// firstNew_ - Index of the first register added to newRegs_.
60 const unsigned firstNew_;
62 /// scannedRemattable_ - true when remattable values have been identified.
63 bool scannedRemattable_;
65 /// remattable_ - Values defined by remattable instructions as identified by
66 /// tii.isTriviallyReMaterializable().
67 SmallPtrSet<const VNInfo*,4> remattable_;
69 /// rematted_ - Values that were actually rematted, and so need to have their
70 /// live range trimmed or entirely removed.
71 SmallPtrSet<const VNInfo*,4> rematted_;
73 /// scanRemattable - Identify the parent_ values that may rematerialize.
74 void scanRemattable(LiveIntervals &lis,
75 const TargetInstrInfo &tii,
78 /// allUsesAvailableAt - Return true if all registers used by OrigMI at
79 /// OrigIdx are also available with the same value at UseIdx.
80 bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
81 SlotIndex UseIdx, LiveIntervals &lis);
84 /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
85 /// @param parent The register being spilled or split.
86 /// @param newRegs List to receive any new registers created. This needn't be
87 /// empty initially, any existing registers are ignored.
88 /// @param uselessRegs List of registers that can't be used when
89 /// rematerializing values because they are about to be removed.
90 LiveRangeEdit(LiveInterval &parent,
91 SmallVectorImpl<LiveInterval*> &newRegs,
92 Delegate *delegate = 0,
93 const SmallVectorImpl<LiveInterval*> *uselessRegs = 0)
94 : parent_(parent), newRegs_(newRegs),
96 uselessRegs_(uselessRegs),
97 firstNew_(newRegs.size()),
98 scannedRemattable_(false) {}
100 LiveInterval &getParent() const { return parent_; }
101 unsigned getReg() const { return parent_.reg; }
103 /// Iterator for accessing the new registers added by this edit.
104 typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
105 iterator begin() const { return newRegs_.begin()+firstNew_; }
106 iterator end() const { return newRegs_.end(); }
107 unsigned size() const { return newRegs_.size()-firstNew_; }
108 bool empty() const { return size() == 0; }
109 LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
111 /// FIXME: Temporary accessors until we can get rid of
112 /// LiveIntervals::AddIntervalsForSpills
113 SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; }
114 const SmallVectorImpl<LiveInterval*> *getUselessVRegs() {
118 /// createFrom - Create a new virtual register based on OldReg.
119 LiveInterval &createFrom(unsigned OldReg, LiveIntervals&, VirtRegMap&);
121 /// create - Create a new register with the same class and original slot as
123 LiveInterval &create(LiveIntervals &LIS, VirtRegMap &VRM) {
124 return createFrom(getReg(), LIS, VRM);
127 /// anyRematerializable - Return true if any parent values may be
128 /// rematerializable.
129 /// This function must be called before any rematerialization is attempted.
130 bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
133 /// checkRematerializable - Manually add VNI to the list of rematerializable
134 /// values if DefMI may be rematerializable.
135 void checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
136 const TargetInstrInfo&, AliasAnalysis*);
138 /// Remat - Information needed to rematerialize at a specific location.
140 VNInfo *ParentVNI; // parent_'s value at the remat location.
141 MachineInstr *OrigMI; // Instruction defining ParentVNI.
142 explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
145 /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
146 /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
147 /// When cheapAsAMove is set, only cheap remats are allowed.
148 bool canRematerializeAt(Remat &RM,
153 /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
154 /// instruction into MBB before MI. The new instruction is mapped, but
155 /// liveness is not updated.
156 /// Return the SlotIndex of the new instruction.
157 SlotIndex rematerializeAt(MachineBasicBlock &MBB,
158 MachineBasicBlock::iterator MI,
162 const TargetInstrInfo&,
163 const TargetRegisterInfo&);
165 /// markRematerialized - explicitly mark a value as rematerialized after doing
167 void markRematerialized(const VNInfo *ParentVNI) {
168 rematted_.insert(ParentVNI);
171 /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
172 bool didRematerialize(const VNInfo *ParentVNI) const {
173 return rematted_.count(ParentVNI);
176 /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
177 /// to erase it from LIS.
178 void eraseVirtReg(unsigned Reg, LiveIntervals &LIS);
180 /// eliminateDeadDefs - Try to delete machine instructions that are now dead
181 /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
182 /// and further dead efs to be eliminated.
183 void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
184 LiveIntervals&, VirtRegMap&,
185 const TargetInstrInfo&);
187 /// calculateRegClassAndHint - Recompute register class and hint for each new
189 void calculateRegClassAndHint(MachineFunction&, LiveIntervals&,
190 const MachineLoopInfo&);