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 #define DEBUG_TYPE "codegen-cp"
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/Pass.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/ADT/BitVector.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/Statistic.h"
30 STATISTIC(NumDeletes, "Number of dead copies deleted");
33 class MachineCopyPropagation : public MachineFunctionPass {
34 const TargetRegisterInfo *TRI;
35 BitVector ReservedRegs;
38 static char ID; // Pass identification, replacement for typeid
39 MachineCopyPropagation() : MachineFunctionPass(ID) {
40 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
43 virtual bool runOnMachineFunction(MachineFunction &MF);
46 typedef SmallVector<unsigned, 4> DestList;
47 typedef DenseMap<unsigned, DestList> SourceMap;
49 void SourceNoLongerAvailable(unsigned Reg,
51 DenseMap<unsigned, MachineInstr*> &AvailCopyMap);
52 bool CopyPropagateBlock(MachineBasicBlock &MBB);
55 char MachineCopyPropagation::ID = 0;
56 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
58 INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
59 "Machine Copy Propagation Pass", false, false)
62 MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg,
64 DenseMap<unsigned, MachineInstr*> &AvailCopyMap) {
65 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
66 SourceMap::iterator SI = SrcMap.find(*AI);
67 if (SI != SrcMap.end()) {
68 const DestList& Defs = SI->second;
69 for (DestList::const_iterator I = Defs.begin(), E = Defs.end();
71 unsigned MappedDef = *I;
72 // Source of copy is no longer available for propagation.
73 if (AvailCopyMap.erase(MappedDef)) {
74 for (MCSubRegIterator SR(MappedDef, TRI); SR.isValid(); ++SR)
75 AvailCopyMap.erase(*SR);
82 static bool NoInterveningSideEffect(const MachineInstr *CopyMI,
83 const MachineInstr *MI) {
84 const MachineBasicBlock *MBB = CopyMI->getParent();
85 if (MI->getParent() != MBB)
87 MachineBasicBlock::const_iterator I = CopyMI;
88 MachineBasicBlock::const_iterator E = MBB->end();
89 MachineBasicBlock::const_iterator E2 = MI;
92 while (I != E && I != E2) {
93 if (I->hasUnmodeledSideEffects() || I->isCall() ||
101 /// isNopCopy - Return true if the specified copy is really a nop. That is
102 /// if the source of the copy is the same of the definition of the copy that
103 /// supplied the source. If the source of the copy is a sub-register than it
104 /// must check the sub-indices match. e.g.
110 static bool isNopCopy(MachineInstr *CopyMI, unsigned Def, unsigned Src,
111 const TargetRegisterInfo *TRI) {
112 unsigned SrcSrc = CopyMI->getOperand(1).getReg();
115 if (TRI->isSubRegister(SrcSrc, Def)) {
116 unsigned SrcDef = CopyMI->getOperand(0).getReg();
117 unsigned SubIdx = TRI->getSubRegIndex(SrcSrc, Def);
120 return SubIdx == TRI->getSubRegIndex(SrcDef, Src);
126 bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
127 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies; // Candidates for deletion
128 DenseMap<unsigned, MachineInstr*> AvailCopyMap; // Def -> available copies map
129 DenseMap<unsigned, MachineInstr*> CopyMap; // Def -> copies map
130 SourceMap SrcMap; // Src -> Def map
132 bool Changed = false;
133 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
134 MachineInstr *MI = &*I;
138 unsigned Def = MI->getOperand(0).getReg();
139 unsigned Src = MI->getOperand(1).getReg();
141 if (TargetRegisterInfo::isVirtualRegister(Def) ||
142 TargetRegisterInfo::isVirtualRegister(Src))
143 report_fatal_error("MachineCopyPropagation should be run after"
144 " register allocation!");
146 DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src);
147 if (CI != AvailCopyMap.end()) {
148 MachineInstr *CopyMI = CI->second;
149 if (!ReservedRegs.test(Def) &&
150 (!ReservedRegs.test(Src) || NoInterveningSideEffect(CopyMI, MI)) &&
151 isNopCopy(CopyMI, Def, Src, TRI)) {
152 // The two copies cancel out and the source of the first copy
153 // hasn't been overridden, eliminate the second one. e.g.
154 // %ECX<def> = COPY %EAX<kill>
155 // ... nothing clobbered EAX.
156 // %EAX<def> = COPY %ECX
158 // %ECX<def> = COPY %EAX
160 // Also avoid eliminating a copy from reserved registers unless the
161 // definition is proven not clobbered. e.g.
162 // %RSP<def> = COPY %RAX
164 // %RAX<def> = COPY %RSP
166 // Clear any kills of Def between CopyMI and MI. This extends the
168 for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I)
169 I->clearRegisterKills(Def, TRI);
171 MI->eraseFromParent();
178 // If Src is defined by a previous copy, it cannot be eliminated.
179 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
180 CI = CopyMap.find(*AI);
181 if (CI != CopyMap.end())
182 MaybeDeadCopies.remove(CI->second);
185 // Copy is now a candidate for deletion.
186 MaybeDeadCopies.insert(MI);
188 // If 'Src' is previously source of another copy, then this earlier copy's
189 // source is no longer available. e.g.
190 // %xmm9<def> = copy %xmm2
192 // %xmm2<def> = copy %xmm0
194 // %xmm2<def> = copy %xmm9
195 SourceNoLongerAvailable(Def, SrcMap, AvailCopyMap);
197 // Remember Def is defined by the copy.
198 // ... Make sure to clear the def maps of aliases first.
199 for (MCRegAliasIterator AI(Def, TRI, false); AI.isValid(); ++AI) {
201 AvailCopyMap.erase(*AI);
204 AvailCopyMap[Def] = MI;
205 for (MCSubRegIterator SR(Def, TRI); SR.isValid(); ++SR) {
207 AvailCopyMap[*SR] = MI;
210 // Remember source that's copied to Def. Once it's clobbered, then
211 // it's no longer available for copy propagation.
212 if (std::find(SrcMap[Src].begin(), SrcMap[Src].end(), Def) ==
214 SrcMap[Src].push_back(Def);
221 SmallVector<unsigned, 2> Defs;
222 int RegMaskOpNum = -1;
223 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
224 MachineOperand &MO = MI->getOperand(i);
229 unsigned Reg = MO.getReg();
233 if (TargetRegisterInfo::isVirtualRegister(Reg))
234 report_fatal_error("MachineCopyPropagation should be run after"
235 " register allocation!");
242 // If 'Reg' is defined by a copy, the copy is no longer a candidate
244 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
245 DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(*AI);
246 if (CI != CopyMap.end())
247 MaybeDeadCopies.remove(CI->second);
251 // The instruction has a register mask operand which means that it clobbers
252 // a large set of registers. It is possible to use the register mask to
253 // prune the available copies, but treat it like a basic block boundary for
255 if (RegMaskOpNum >= 0) {
256 // Erase any MaybeDeadCopies whose destination register is clobbered.
257 const MachineOperand &MaskMO = MI->getOperand(RegMaskOpNum);
258 for (SmallSetVector<MachineInstr*, 8>::iterator
259 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
261 unsigned Reg = (*DI)->getOperand(0).getReg();
262 if (ReservedRegs.test(Reg) || !MaskMO.clobbersPhysReg(Reg))
264 (*DI)->eraseFromParent();
269 // Clear all data structures as if we were beginning a new basic block.
270 MaybeDeadCopies.clear();
271 AvailCopyMap.clear();
277 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
278 unsigned Reg = Defs[i];
280 // No longer defined by a copy.
281 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
283 AvailCopyMap.erase(*AI);
286 // If 'Reg' is previously source of a copy, it is no longer available for
288 SourceNoLongerAvailable(Reg, SrcMap, AvailCopyMap);
292 // If MBB doesn't have successors, delete the copies whose defs are not used.
293 // If MBB does have successors, then conservative assume the defs are live-out
294 // since we don't want to trust live-in lists.
295 if (MBB.succ_empty()) {
296 for (SmallSetVector<MachineInstr*, 8>::iterator
297 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
299 if (!ReservedRegs.test((*DI)->getOperand(0).getReg())) {
300 (*DI)->eraseFromParent();
310 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
311 bool Changed = false;
313 TRI = MF.getTarget().getRegisterInfo();
314 ReservedRegs = TRI->getReservedRegs(MF);
316 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
317 Changed |= CopyPropagateBlock(*I);