Debian Codegraph @

suite/synctools/tablegen/include/llvm/CodeGen/LiveRangeEdit.h

raw symbols SCIP
1 //===- LiveRangeEdit.h - Basic tools for split and spill --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The LiveRangeEdit class represents changes done to a virtual register when it
10 // is spilled or split.
11 //
12 // The parent register is never changed. Instead, a number of new virtual
13 // registers are created and added to the newRegs vector.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
18 #define LLVM_CODEGEN_LIVERANGEEDIT_H
19
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/CodeGen/LiveInterval.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/SlotIndexes.h"
30 #include "llvm/CodeGen/TargetSubtargetInfo.h"
31 #include <cassert>
32
33 namespace llvm {
34
35 class AAResults;
36 class LiveIntervals;
37 class MachineInstr;
38 class MachineOperand;
39 class TargetInstrInfo;
40 class TargetRegisterInfo;
41 class VirtRegMap;
42 class VirtRegAuxInfo;
43
44 class LiveRangeEdit : private MachineRegisterInfo::Delegate {
45 public:
46 /// Callback methods for LiveRangeEdit owners.
47 class Delegate {
48 virtual void anchor();
49
50 public:
51 virtual ~Delegate() = default;
52
53 /// Called immediately before erasing a dead machine instruction.
54 virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
55
56 /// Called when a virtual register is no longer used. Return false to defer
57 /// its deletion from LiveIntervals.
58 virtual bool LRE_CanEraseVirtReg(Register) { return true; }
59
60 /// Called before shrinking the live range of a virtual register.
61 virtual void LRE_WillShrinkVirtReg(Register) {}
62
63 /// Called after cloning a virtual register.
64 /// This is used for new registers representing connected components of Old.
65 virtual void LRE_DidCloneVirtReg(Register New, Register Old) {}
66 };
67
68 private:
69 LiveInterval *Parent;
70 SmallVectorImpl<Register> &NewRegs;
71 MachineRegisterInfo &MRI;
72 LiveIntervals &LIS;
73 VirtRegMap *VRM;
74 const TargetInstrInfo &TII;
75 Delegate *const TheDelegate;
76
77 /// FirstNew - Index of the first register added to NewRegs.
78 const unsigned FirstNew;
79
80 /// ScannedRemattable - true when remattable values have been identified.
81 bool ScannedRemattable = false;
82
83 /// DeadRemats - The saved instructions which have already been dead after
84 /// rematerialization but not deleted yet -- to be done in postOptimization.
85 SmallPtrSet<MachineInstr *, 32> *DeadRemats;
86
87 /// Remattable - Values defined by remattable instructions as identified by
88 /// tii.isTriviallyReMaterializable().
89 SmallPtrSet<const VNInfo *, 4> Remattable;
90
91 /// Rematted - Values that were actually rematted, and so need to have their
92 /// live range trimmed or entirely removed.
93 SmallPtrSet<const VNInfo *, 4> Rematted;
94
95 /// scanRemattable - Identify the Parent values that may rematerialize.
96 void scanRemattable(AAResults *aa);
97
98 /// foldAsLoad - If LI has a single use and a single def that can be folded as
99 /// a load, eliminate the register by folding the def into the use.
100 bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr *> &Dead);
101
102 using ToShrinkSet = SetVector<LiveInterval *, SmallVector<LiveInterval *, 8>,
103 SmallPtrSet<LiveInterval *, 8>>;
104
105 /// Helper for eliminateDeadDefs.
106 void eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
107 AAResults *AA);
108
109 /// MachineRegisterInfo callback to notify when new virtual
110 /// registers are created.
111 void MRI_NoteNewVirtualRegister(Register VReg) override;
112
113 /// Check if MachineOperand \p MO is a last use/kill either in the
114 /// main live range of \p LI or in one of the matching subregister ranges.
115 bool useIsKill(const LiveInterval &LI, const MachineOperand &MO) const;
116
117 /// Create a new empty interval based on OldReg.
118 LiveInterval &createEmptyIntervalFrom(Register OldReg, bool createSubRanges);
119
120 public:
121 /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
122 /// @param parent The register being spilled or split.
123 /// @param newRegs List to receive any new registers created. This needn't be
124 /// empty initially, any existing registers are ignored.
125 /// @param MF The MachineFunction the live range edit is taking place in.
126 /// @param lis The collection of all live intervals in this function.
127 /// @param vrm Map of virtual registers to physical registers for this
128 /// function. If NULL, no virtual register map updates will
129 /// be done. This could be the case if called before Regalloc.
130 /// @param deadRemats The collection of all the instructions defining an
131 /// original reg and are dead after remat.
132 LiveRangeEdit(LiveInterval *parent, SmallVectorImpl<Register> &newRegs,
133 MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm,
134 Delegate *delegate = nullptr,
135 SmallPtrSet<MachineInstr *, 32> *deadRemats = nullptr)
136 : Parent(parent), NewRegs(newRegs), MRI(MF.getRegInfo()), LIS(lis),
137 VRM(vrm), TII(*MF.getSubtarget().getInstrInfo()), TheDelegate(delegate),
138 FirstNew(newRegs.size()), DeadRemats(deadRemats) {
139 MRI.setDelegate(this);
140 }
141
142 ~LiveRangeEdit() override { MRI.resetDelegate(this); }
143
144 LiveInterval &getParent() const {
145 assert(Parent && "No parent LiveInterval");
146 return *Parent;
147 }
148
149 Register getReg() const { return getParent().reg(); }
150
151 /// Iterator for accessing the new registers added by this edit.
152 using iterator = SmallVectorImpl<Register>::const_iterator;
153 iterator begin() const { return NewRegs.begin() + FirstNew; }
154 iterator end() const { return NewRegs.end(); }
155 unsigned size() const { return NewRegs.size() - FirstNew; }
156 bool empty() const { return size() == 0; }
157 Register get(unsigned idx) const { return NewRegs[idx + FirstNew]; }
158
159 /// pop_back - It allows LiveRangeEdit users to drop new registers.
160 /// The context is when an original def instruction of a register is
161 /// dead after rematerialization, we still want to keep it for following
162 /// rematerializations. We save the def instruction in DeadRemats,
163 /// and replace the original dst register with a new dummy register so
164 /// the live range of original dst register can be shrinked normally.
165 /// We don't want to allocate phys register for the dummy register, so
166 /// we want to drop it from the NewRegs set.
167 void pop_back() { NewRegs.pop_back(); }
168
169 ArrayRef<Register> regs() const {
170 return makeArrayRef(NewRegs).slice(FirstNew);
171 }
172
173 /// createFrom - Create a new virtual register based on OldReg.
174 Register createFrom(Register OldReg);
175
176 /// create - Create a new register with the same class and original slot as
177 /// parent.
178 LiveInterval &createEmptyInterval() {
179 return createEmptyIntervalFrom(getReg(), true);
180 }
181
182 Register create() { return createFrom(getReg()); }
183
184 /// anyRematerializable - Return true if any parent values may be
185 /// rematerializable.
186 /// This function must be called before any rematerialization is attempted.
187 bool anyRematerializable(AAResults *);
188
189 /// checkRematerializable - Manually add VNI to the list of rematerializable
190 /// values if DefMI may be rematerializable.
191 bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
192 AAResults *);
193
194 /// Remat - Information needed to rematerialize at a specific location.
195 struct Remat {
196 VNInfo *ParentVNI; // parent_'s value at the remat location.
197 MachineInstr *OrigMI = nullptr; // Instruction defining OrigVNI. It contains
198 // the real expr for remat.
199
200 explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI) {}
201 };
202
203 /// allUsesAvailableAt - Return true if all registers used by OrigMI at
204 /// OrigIdx are also available with the same value at UseIdx.
205 bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
206 SlotIndex UseIdx) const;
207
208 /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
209 /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
210 /// When cheapAsAMove is set, only cheap remats are allowed.
211 bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx,
212 bool cheapAsAMove);
213
214 /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
215 /// instruction into MBB before MI. The new instruction is mapped, but
216 /// liveness is not updated.
217 /// Return the SlotIndex of the new instruction.
218 SlotIndex rematerializeAt(MachineBasicBlock &MBB,
219 MachineBasicBlock::iterator MI, unsigned DestReg,
220 const Remat &RM, const TargetRegisterInfo &,
221 bool Late = false);
222
223 /// markRematerialized - explicitly mark a value as rematerialized after doing
224 /// it manually.
225 void markRematerialized(const VNInfo *ParentVNI) {
226 Rematted.insert(ParentVNI);
227 }
228
229 /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
230 bool didRematerialize(const VNInfo *ParentVNI) const {
231 return Rematted.count(ParentVNI);
232 }
233
234 /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
235 /// to erase it from LIS.
236 void eraseVirtReg(Register Reg);
237
238 /// eliminateDeadDefs - Try to delete machine instructions that are now dead
239 /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
240 /// and further dead efs to be eliminated.
241 /// RegsBeingSpilled lists registers currently being spilled by the register
242 /// allocator. These registers should not be split into new intervals
243 /// as currently those new intervals are not guaranteed to spill.
244 void eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
245 ArrayRef<Register> RegsBeingSpilled = None,
246 AAResults *AA = nullptr);
247
248 /// calculateRegClassAndHint - Recompute register class and hint for each new
249 /// register.
250 void calculateRegClassAndHint(MachineFunction &, VirtRegAuxInfo &);
251 };
252
253 } // end namespace llvm
254
255 #endif // LLVM_CODEGEN_LIVERANGEEDIT_H