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/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
30 #define DEBUG_TYPE "codegen-cp"
32 STATISTIC(NumDeletes, "Number of dead copies deleted");
35 class MachineCopyPropagation : public MachineFunctionPass {
36 const TargetRegisterInfo *TRI;
37 const TargetInstrInfo *TII;
38 MachineRegisterInfo *MRI;
41 static char ID; // Pass identification, replacement for typeid
42 MachineCopyPropagation() : MachineFunctionPass(ID) {
43 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
46 bool runOnMachineFunction(MachineFunction &MF) override;
49 typedef SmallVector<unsigned, 4> DestList;
50 typedef DenseMap<unsigned, DestList> SourceMap;
52 void SourceNoLongerAvailable(unsigned Reg,
54 DenseMap<unsigned, MachineInstr*> &AvailCopyMap);
55 bool CopyPropagateBlock(MachineBasicBlock &MBB);
56 void removeCopy(MachineInstr *MI);
59 char MachineCopyPropagation::ID = 0;
60 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
62 INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
63 "Machine Copy Propagation Pass", false, false)
66 MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg,
68 DenseMap<unsigned, MachineInstr*> &AvailCopyMap) {
69 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
70 SourceMap::iterator SI = SrcMap.find(*AI);
71 if (SI != SrcMap.end()) {
72 const DestList& Defs = SI->second;
73 for (DestList::const_iterator I = Defs.begin(), E = Defs.end();
75 unsigned MappedDef = *I;
76 // Source of copy is no longer available for propagation.
77 if (AvailCopyMap.erase(MappedDef)) {
78 for (MCSubRegIterator SR(MappedDef, TRI); SR.isValid(); ++SR)
79 AvailCopyMap.erase(*SR);
86 static bool NoInterveningSideEffect(const MachineInstr *CopyMI,
87 const MachineInstr *MI) {
88 const MachineBasicBlock *MBB = CopyMI->getParent();
89 if (MI->getParent() != MBB)
91 MachineBasicBlock::const_iterator I = CopyMI;
92 MachineBasicBlock::const_iterator E = MBB->end();
93 MachineBasicBlock::const_iterator E2 = MI;
96 while (I != E && I != E2) {
97 if (I->hasUnmodeledSideEffects() || I->isCall() ||
105 /// isNopCopy - Return true if the specified copy is really a nop. That is
106 /// if the source of the copy is the same of the definition of the copy that
107 /// supplied the source. If the source of the copy is a sub-register than it
108 /// must check the sub-indices match. e.g.
114 static bool isNopCopy(MachineInstr *CopyMI, unsigned Def, unsigned Src,
115 const TargetRegisterInfo *TRI) {
116 unsigned SrcSrc = CopyMI->getOperand(1).getReg();
119 if (TRI->isSubRegister(SrcSrc, Def)) {
120 unsigned SrcDef = CopyMI->getOperand(0).getReg();
121 unsigned SubIdx = TRI->getSubRegIndex(SrcSrc, Def);
124 return SubIdx == TRI->getSubRegIndex(SrcDef, Src);
130 // Remove MI from the function because it has been determined it is dead.
131 // Turn it into a noop KILL instruction as opposed to removing it to
132 // maintain imp-use/imp-def chains.
133 void MachineCopyPropagation::removeCopy(MachineInstr *MI) {
134 MI->setDesc(TII->get(TargetOpcode::KILL));
137 bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
138 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies; // Candidates for deletion
139 DenseMap<unsigned, MachineInstr*> AvailCopyMap; // Def -> available copies map
140 DenseMap<unsigned, MachineInstr*> CopyMap; // Def -> copies map
141 SourceMap SrcMap; // Src -> Def map
143 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
145 bool Changed = false;
146 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
147 MachineInstr *MI = &*I;
151 unsigned Def = MI->getOperand(0).getReg();
152 unsigned Src = MI->getOperand(1).getReg();
154 if (TargetRegisterInfo::isVirtualRegister(Def) ||
155 TargetRegisterInfo::isVirtualRegister(Src))
156 report_fatal_error("MachineCopyPropagation should be run after"
157 " register allocation!");
159 DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src);
160 if (CI != AvailCopyMap.end()) {
161 MachineInstr *CopyMI = CI->second;
162 if (!MRI->isReserved(Def) &&
163 (!MRI->isReserved(Src) || NoInterveningSideEffect(CopyMI, MI)) &&
164 isNopCopy(CopyMI, Def, Src, TRI)) {
165 // The two copies cancel out and the source of the first copy
166 // hasn't been overridden, eliminate the second one. e.g.
167 // %ECX<def> = COPY %EAX<kill>
168 // ... nothing clobbered EAX.
169 // %EAX<def> = COPY %ECX
171 // %ECX<def> = COPY %EAX
173 // Also avoid eliminating a copy from reserved registers unless the
174 // definition is proven not clobbered. e.g.
175 // %RSP<def> = COPY %RAX
177 // %RAX<def> = COPY %RSP
179 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; MI->dump());
181 // Clear any kills of Def between CopyMI and MI. This extends the
183 for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I)
184 I->clearRegisterKills(Def, TRI);
193 // If Src is defined by a previous copy, it cannot be eliminated.
194 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
195 CI = CopyMap.find(*AI);
196 if (CI != CopyMap.end()) {
197 DEBUG(dbgs() << "MCP: Copy is no longer dead: "; CI->second->dump());
198 MaybeDeadCopies.remove(CI->second);
202 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
204 // Copy is now a candidate for deletion.
205 MaybeDeadCopies.insert(MI);
207 // If 'Src' is previously source of another copy, then this earlier copy's
208 // source is no longer available. e.g.
209 // %xmm9<def> = copy %xmm2
211 // %xmm2<def> = copy %xmm0
213 // %xmm2<def> = copy %xmm9
214 SourceNoLongerAvailable(Def, SrcMap, AvailCopyMap);
216 // Remember Def is defined by the copy.
217 // ... Make sure to clear the def maps of aliases first.
218 for (MCRegAliasIterator AI(Def, TRI, false); AI.isValid(); ++AI) {
220 AvailCopyMap.erase(*AI);
222 for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
225 AvailCopyMap[*SR] = MI;
228 // Remember source that's copied to Def. Once it's clobbered, then
229 // it's no longer available for copy propagation.
230 if (std::find(SrcMap[Src].begin(), SrcMap[Src].end(), Def) ==
232 SrcMap[Src].push_back(Def);
239 SmallVector<unsigned, 2> Defs;
240 int RegMaskOpNum = -1;
241 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
242 MachineOperand &MO = MI->getOperand(i);
247 unsigned Reg = MO.getReg();
251 if (TargetRegisterInfo::isVirtualRegister(Reg))
252 report_fatal_error("MachineCopyPropagation should be run after"
253 " register allocation!");
260 // If 'Reg' is defined by a copy, the copy is no longer a candidate
262 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
263 DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(*AI);
264 if (CI != CopyMap.end()) {
265 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
266 MaybeDeadCopies.remove(CI->second);
271 // The instruction has a register mask operand which means that it clobbers
272 // a large set of registers. It is possible to use the register mask to
273 // prune the available copies, but treat it like a basic block boundary for
275 if (RegMaskOpNum >= 0) {
276 // Erase any MaybeDeadCopies whose destination register is clobbered.
277 const MachineOperand &MaskMO = MI->getOperand(RegMaskOpNum);
278 for (SmallSetVector<MachineInstr*, 8>::iterator
279 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
281 unsigned Reg = (*DI)->getOperand(0).getReg();
282 if (MRI->isReserved(Reg) || !MaskMO.clobbersPhysReg(Reg))
284 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
291 // Clear all data structures as if we were beginning a new basic block.
292 MaybeDeadCopies.clear();
293 AvailCopyMap.clear();
299 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
300 unsigned Reg = Defs[i];
302 // No longer defined by a copy.
303 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
305 AvailCopyMap.erase(*AI);
308 // If 'Reg' is previously source of a copy, it is no longer available for
310 SourceNoLongerAvailable(Reg, SrcMap, AvailCopyMap);
314 // If MBB doesn't have successors, delete the copies whose defs are not used.
315 // If MBB does have successors, then conservative assume the defs are live-out
316 // since we don't want to trust live-in lists.
317 if (MBB.succ_empty()) {
318 for (SmallSetVector<MachineInstr*, 8>::iterator
319 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
321 if (!MRI->isReserved((*DI)->getOperand(0).getReg())) {
332 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
333 if (skipOptnoneFunction(*MF.getFunction()))
336 bool Changed = false;
338 TRI = MF.getTarget().getRegisterInfo();
339 TII = MF.getTarget().getInstrInfo();
340 MRI = &MF.getRegInfo();
342 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
343 Changed |= CopyPropagateBlock(*I);