1 //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
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 // This is an extremely simple MachineInstr-level copy propagation pass.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetInstrInfo.h"
26 #include "llvm/Target/TargetRegisterInfo.h"
27 #include "llvm/Target/TargetSubtargetInfo.h"
30 #define DEBUG_TYPE "codegen-cp"
32 STATISTIC(NumDeletes, "Number of dead copies deleted");
35 typedef SmallVector<unsigned, 4> RegList;
36 typedef DenseMap<unsigned, RegList> SourceMap;
37 typedef DenseMap<unsigned, MachineInstr*> Reg2MIMap;
39 class MachineCopyPropagation : public MachineFunctionPass {
40 const TargetRegisterInfo *TRI;
41 const TargetInstrInfo *TII;
42 const MachineRegisterInfo *MRI;
45 static char ID; // Pass identification, replacement for typeid
46 MachineCopyPropagation() : MachineFunctionPass(ID) {
47 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 MachineFunctionPass::getAnalysisUsage(AU);
55 bool runOnMachineFunction(MachineFunction &MF) override;
57 MachineFunctionProperties getRequiredProperties() const override {
58 return MachineFunctionProperties().set(
59 MachineFunctionProperties::Property::NoVRegs);
63 void ClobberRegister(unsigned Reg);
64 void ReadRegister(unsigned Reg);
65 void CopyPropagateBlock(MachineBasicBlock &MBB);
66 bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
68 /// Candidates for deletion.
69 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
70 /// Def -> available copies map.
71 Reg2MIMap AvailCopyMap;
72 /// Def -> copies map.
79 char MachineCopyPropagation::ID = 0;
80 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
82 INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
83 "Machine Copy Propagation Pass", false, false)
85 /// Remove any entry in \p Map where the register is a subregister or equal to
86 /// a register contained in \p Regs.
87 static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
88 const TargetRegisterInfo &TRI) {
89 for (unsigned Reg : Regs) {
90 // Source of copy is no longer available for propagation.
91 for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
96 /// Remove any entry in \p Map that is marked clobbered in \p RegMask.
97 /// The map will typically have a lot fewer entries than the regmask clobbers,
98 /// so this is more efficient than iterating the clobbered registers and calling
99 /// ClobberRegister() on them.
100 static void removeClobberedRegsFromMap(Reg2MIMap &Map,
101 const MachineOperand &RegMask) {
102 for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
105 unsigned Reg = I->first;
106 if (RegMask.clobbersPhysReg(Reg))
111 void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
112 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
114 AvailCopyMap.erase(*AI);
116 SourceMap::iterator SI = SrcMap.find(*AI);
117 if (SI != SrcMap.end()) {
118 removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
124 void MachineCopyPropagation::ReadRegister(unsigned Reg) {
125 // If 'Reg' is defined by a copy, the copy is no longer a candidate
127 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
128 Reg2MIMap::iterator CI = CopyMap.find(*AI);
129 if (CI != CopyMap.end()) {
130 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
131 MaybeDeadCopies.remove(CI->second);
136 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
137 /// This fact may have been obscured by sub register usage or may not be true at
138 /// all even though Src and Def are subregisters of the registers used in
139 /// PreviousCopy. e.g.
140 /// isNopCopy("ecx = COPY eax", AX, CX) == true
141 /// isNopCopy("ecx = COPY eax", AH, CL) == false
142 static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
143 unsigned Def, const TargetRegisterInfo *TRI) {
144 unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
145 unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
146 if (Src == PreviousSrc) {
147 assert(Def == PreviousDef);
150 if (!TRI->isSubRegister(PreviousSrc, Src))
152 unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
153 return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
156 /// Remove instruction \p Copy if there exists a previous copy that copies the
157 /// register \p Src to the register \p Def; This may happen indirectly by
158 /// copying the super registers.
159 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
161 // Avoid eliminating a copy from/to a reserved registers as we cannot predict
162 // the value (Example: The sparc zero register is writable but stays zero).
163 if (MRI->isReserved(Src) || MRI->isReserved(Def))
166 // Search for an existing copy.
167 Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
168 if (CI == AvailCopyMap.end())
171 // Check that the existing copy uses the correct sub registers.
172 MachineInstr &PrevCopy = *CI->second;
173 if (!isNopCopy(PrevCopy, Src, Def, TRI))
176 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
178 // Copy was redundantly redefining either Src or Def. Remove earlier kill
179 // flags between Copy and PrevCopy because the value will be reused now.
180 assert(Copy.isCopy());
181 unsigned CopyDef = Copy.getOperand(0).getReg();
182 assert(CopyDef == Src || CopyDef == Def);
183 for (MachineInstr &MI :
184 make_range(PrevCopy.getIterator(), Copy.getIterator()))
185 MI.clearRegisterKills(CopyDef, TRI);
187 Copy.eraseFromParent();
193 void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
194 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
196 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
197 MachineInstr *MI = &*I;
201 unsigned Def = MI->getOperand(0).getReg();
202 unsigned Src = MI->getOperand(1).getReg();
204 assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
205 !TargetRegisterInfo::isVirtualRegister(Src) &&
206 "MachineCopyPropagation should be run after register allocation!");
208 // The two copies cancel out and the source of the first copy
209 // hasn't been overridden, eliminate the second one. e.g.
210 // %ECX<def> = COPY %EAX
211 // ... nothing clobbered EAX.
212 // %EAX<def> = COPY %ECX
214 // %ECX<def> = COPY %EAX
218 // %ECX<def> = COPY %EAX
219 // ... nothing clobbered EAX.
220 // %ECX<def> = COPY %EAX
222 // %ECX<def> = COPY %EAX
223 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
226 // If Src is defined by a previous copy, the previous copy cannot be
229 for (const MachineOperand &MO : MI->implicit_operands()) {
230 if (!MO.isReg() || !MO.readsReg())
232 unsigned Reg = MO.getReg();
238 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
240 // Copy is now a candidate for deletion.
241 if (!MRI->isReserved(Def))
242 MaybeDeadCopies.insert(MI);
244 // If 'Def' is previously source of another copy, then this earlier copy's
245 // source is no longer available. e.g.
246 // %xmm9<def> = copy %xmm2
248 // %xmm2<def> = copy %xmm0
250 // %xmm2<def> = copy %xmm9
251 ClobberRegister(Def);
252 for (const MachineOperand &MO : MI->implicit_operands()) {
253 if (!MO.isReg() || !MO.isDef())
255 unsigned Reg = MO.getReg();
258 ClobberRegister(Reg);
261 // Remember Def is defined by the copy.
262 for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
265 AvailCopyMap[*SR] = MI;
268 // Remember source that's copied to Def. Once it's clobbered, then
269 // it's no longer available for copy propagation.
270 RegList &DestList = SrcMap[Src];
271 if (!is_contained(DestList, Def))
272 DestList.push_back(Def);
278 SmallVector<unsigned, 2> Defs;
279 const MachineOperand *RegMask = nullptr;
280 for (const MachineOperand &MO : MI->operands()) {
285 unsigned Reg = MO.getReg();
289 assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
290 "MachineCopyPropagation should be run after register allocation!");
295 } else if (MO.readsReg())
299 // The instruction has a register mask operand which means that it clobbers
300 // a large set of registers. Treat clobbered registers the same way as
301 // defined registers.
303 // Erase any MaybeDeadCopies whose destination register is clobbered.
304 for (SmallSetVector<MachineInstr *, 8>::iterator DI =
305 MaybeDeadCopies.begin();
306 DI != MaybeDeadCopies.end();) {
307 MachineInstr *MaybeDead = *DI;
308 unsigned Reg = MaybeDead->getOperand(0).getReg();
309 assert(!MRI->isReserved(Reg));
311 if (!RegMask->clobbersPhysReg(Reg)) {
316 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
319 // erase() will return the next valid iterator pointing to the next
320 // element after the erased one.
321 DI = MaybeDeadCopies.erase(DI);
322 MaybeDead->eraseFromParent();
327 removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
328 removeClobberedRegsFromMap(CopyMap, *RegMask);
329 for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
332 if (RegMask->clobbersPhysReg(I->first)) {
333 removeRegsFromMap(AvailCopyMap, I->second, *TRI);
339 // Any previous copy definition or reading the Defs is no longer available.
340 for (unsigned Reg : Defs)
341 ClobberRegister(Reg);
344 // If MBB doesn't have successors, delete the copies whose defs are not used.
345 // If MBB does have successors, then conservative assume the defs are live-out
346 // since we don't want to trust live-in lists.
347 if (MBB.succ_empty()) {
348 for (MachineInstr *MaybeDead : MaybeDeadCopies) {
349 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
350 MaybeDead->eraseFromParent();
356 MaybeDeadCopies.clear();
357 AvailCopyMap.clear();
362 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
363 if (skipFunction(*MF.getFunction()))
368 TRI = MF.getSubtarget().getRegisterInfo();
369 TII = MF.getSubtarget().getInstrInfo();
370 MRI = &MF.getRegInfo();
372 for (MachineBasicBlock &MBB : MF)
373 CopyPropagateBlock(MBB);