blob: 565efb3898c915bf49d7cba84dc1b1995bf30ed8 [file] [log] [blame]
Evan Cheng977679d2012-01-07 03:02:36 +00001//===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is an extremely simple MachineInstr-level copy propagation pass.
11//
12//===----------------------------------------------------------------------===//
13
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"
Lang Hamesd9eb1d72012-03-27 00:44:47 +000025#include "llvm/ADT/DenseSet.h"
Evan Cheng977679d2012-01-07 03:02:36 +000026#include "llvm/ADT/SetVector.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/Statistic.h"
29using namespace llvm;
30
31STATISTIC(NumDeletes, "Number of dead copies deleted");
32
33namespace {
34 class MachineCopyPropagation : public MachineFunctionPass {
35 const TargetRegisterInfo *TRI;
36 BitVector ReservedRegs;
Andrew Trick1df91b02012-02-08 21:22:43 +000037
Evan Cheng977679d2012-01-07 03:02:36 +000038 public:
39 static char ID; // Pass identification, replacement for typeid
40 MachineCopyPropagation() : MachineFunctionPass(ID) {
41 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
42 }
43
44 virtual bool runOnMachineFunction(MachineFunction &MF);
45
46 private:
47 void SourceNoLongerAvailable(unsigned Reg,
Lang Hamesd9eb1d72012-03-27 00:44:47 +000048 DenseMap<unsigned, DenseSet<unsigned> > &SrcMap,
Evan Cheng977679d2012-01-07 03:02:36 +000049 DenseMap<unsigned, MachineInstr*> &AvailCopyMap);
50 bool CopyPropagateBlock(MachineBasicBlock &MBB);
51 };
52}
53char MachineCopyPropagation::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000054char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
Evan Cheng977679d2012-01-07 03:02:36 +000055
56INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
57 "Machine Copy Propagation Pass", false, false)
58
Evan Cheng977679d2012-01-07 03:02:36 +000059void
60MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg,
Lang Hamesd9eb1d72012-03-27 00:44:47 +000061 DenseMap<unsigned, DenseSet<unsigned> > &SrcMap,
Evan Cheng977679d2012-01-07 03:02:36 +000062 DenseMap<unsigned, MachineInstr*> &AvailCopyMap) {
Lang Hamesd9eb1d72012-03-27 00:44:47 +000063 DenseMap<unsigned, DenseSet<unsigned> >::iterator SI = SrcMap.find(Reg);
Evan Cheng977679d2012-01-07 03:02:36 +000064 if (SI != SrcMap.end()) {
Lang Hamesd9eb1d72012-03-27 00:44:47 +000065 const DenseSet<unsigned>& Defs = SI->second;
66 for (DenseSet<unsigned>::const_iterator I = Defs.begin(), E = Defs.end();
67 I != E; ++I) {
68 unsigned MappedDef = *I;
69 // Source of copy is no longer available for propagation.
70 if (AvailCopyMap.erase(MappedDef)) {
71 for (const uint16_t *SR = TRI->getSubRegisters(MappedDef); *SR; ++SR)
72 AvailCopyMap.erase(*SR);
73 }
Evan Cheng977679d2012-01-07 03:02:36 +000074 }
75 }
Craig Toppere4fd9072012-03-04 10:43:23 +000076 for (const uint16_t *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
Evan Cheng977679d2012-01-07 03:02:36 +000077 SI = SrcMap.find(*AS);
78 if (SI != SrcMap.end()) {
Lang Hamesd9eb1d72012-03-27 00:44:47 +000079 const DenseSet<unsigned>& Defs = SI->second;
80 for (DenseSet<unsigned>::const_iterator I = Defs.begin(), E = Defs.end();
81 I != E; ++I) {
82 unsigned MappedDef = *I;
83 if (AvailCopyMap.erase(MappedDef)) {
84 for (const uint16_t *SR = TRI->getSubRegisters(MappedDef); *SR; ++SR)
85 AvailCopyMap.erase(*SR);
86 }
Evan Cheng977679d2012-01-07 03:02:36 +000087 }
88 }
89 }
90}
91
Evan Chenge811d0d2012-01-08 19:52:28 +000092static bool NoInterveningSideEffect(const MachineInstr *CopyMI,
93 const MachineInstr *MI) {
94 const MachineBasicBlock *MBB = CopyMI->getParent();
95 if (MI->getParent() != MBB)
96 return false;
97 MachineBasicBlock::const_iterator I = CopyMI;
98 MachineBasicBlock::const_iterator E = MBB->end();
99 MachineBasicBlock::const_iterator E2 = MI;
100
101 ++I;
102 while (I != E && I != E2) {
103 if (I->hasUnmodeledSideEffects() || I->isCall() ||
104 I->isTerminator())
105 return false;
106 ++I;
107 }
108 return true;
109}
110
Evan Cheng01b623c2012-02-20 23:28:17 +0000111/// isNopCopy - Return true if the specified copy is really a nop. That is
112/// if the source of the copy is the same of the definition of the copy that
113/// supplied the source. If the source of the copy is a sub-register than it
114/// must check the sub-indices match. e.g.
115/// ecx = mov eax
116/// al = mov cl
117/// But not
118/// ecx = mov eax
119/// al = mov ch
120static bool isNopCopy(MachineInstr *CopyMI, unsigned Def, unsigned Src,
121 const TargetRegisterInfo *TRI) {
122 unsigned SrcSrc = CopyMI->getOperand(1).getReg();
123 if (Def == SrcSrc)
124 return true;
125 if (TRI->isSubRegister(SrcSrc, Def)) {
126 unsigned SrcDef = CopyMI->getOperand(0).getReg();
127 unsigned SubIdx = TRI->getSubRegIndex(SrcSrc, Def);
128 if (!SubIdx)
129 return false;
130 return SubIdx == TRI->getSubRegIndex(SrcDef, Src);
131 }
132
133 return false;
134}
135
Evan Cheng977679d2012-01-07 03:02:36 +0000136bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
Lang Hamesd9eb1d72012-03-27 00:44:47 +0000137 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies; // Candidates for deletion
138 DenseMap<unsigned, MachineInstr*> AvailCopyMap; // Def -> available copies map
139 DenseMap<unsigned, MachineInstr*> CopyMap; // Def -> copies map
140 DenseMap<unsigned, DenseSet<unsigned> > SrcMap; // Src -> Def map
Evan Cheng977679d2012-01-07 03:02:36 +0000141
142 bool Changed = false;
143 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
144 MachineInstr *MI = &*I;
145 ++I;
146
147 if (MI->isCopy()) {
148 unsigned Def = MI->getOperand(0).getReg();
149 unsigned Src = MI->getOperand(1).getReg();
150
151 if (TargetRegisterInfo::isVirtualRegister(Def) ||
152 TargetRegisterInfo::isVirtualRegister(Src))
153 report_fatal_error("MachineCopyPropagation should be run after"
154 " register allocation!");
155
156 DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src);
157 if (CI != AvailCopyMap.end()) {
158 MachineInstr *CopyMI = CI->second;
Evan Cheng977679d2012-01-07 03:02:36 +0000159 if (!ReservedRegs.test(Def) &&
Evan Chenge811d0d2012-01-08 19:52:28 +0000160 (!ReservedRegs.test(Src) || NoInterveningSideEffect(CopyMI, MI)) &&
Evan Cheng01b623c2012-02-20 23:28:17 +0000161 isNopCopy(CopyMI, Def, Src, TRI)) {
Evan Cheng977679d2012-01-07 03:02:36 +0000162 // The two copies cancel out and the source of the first copy
163 // hasn't been overridden, eliminate the second one. e.g.
164 // %ECX<def> = COPY %EAX<kill>
165 // ... nothing clobbered EAX.
166 // %EAX<def> = COPY %ECX
167 // =>
168 // %ECX<def> = COPY %EAX
Evan Chenge811d0d2012-01-08 19:52:28 +0000169 //
170 // Also avoid eliminating a copy from reserved registers unless the
171 // definition is proven not clobbered. e.g.
172 // %RSP<def> = COPY %RAX
173 // CALL
174 // %RAX<def> = COPY %RSP
Jakob Stoklund Olesen1a96c912012-01-26 17:52:15 +0000175
176 // Clear any kills of Def between CopyMI and MI. This extends the
177 // live range.
178 for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I)
179 I->clearRegisterKills(Def, TRI);
180
Evan Cheng977679d2012-01-07 03:02:36 +0000181 MI->eraseFromParent();
182 Changed = true;
183 ++NumDeletes;
184 continue;
185 }
186 }
187
188 // If Src is defined by a previous copy, it cannot be eliminated.
189 CI = CopyMap.find(Src);
190 if (CI != CopyMap.end())
191 MaybeDeadCopies.remove(CI->second);
Craig Toppere4fd9072012-03-04 10:43:23 +0000192 for (const uint16_t *AS = TRI->getAliasSet(Src); *AS; ++AS) {
Evan Cheng977679d2012-01-07 03:02:36 +0000193 CI = CopyMap.find(*AS);
194 if (CI != CopyMap.end())
195 MaybeDeadCopies.remove(CI->second);
196 }
197
198 // Copy is now a candidate for deletion.
199 MaybeDeadCopies.insert(MI);
200
201 // If 'Src' is previously source of another copy, then this earlier copy's
202 // source is no longer available. e.g.
203 // %xmm9<def> = copy %xmm2
204 // ...
205 // %xmm2<def> = copy %xmm0
206 // ...
207 // %xmm2<def> = copy %xmm9
208 SourceNoLongerAvailable(Def, SrcMap, AvailCopyMap);
209
210 // Remember Def is defined by the copy.
Evan Chengb266cd02012-02-27 21:46:42 +0000211 // ... Make sure to clear the def maps of aliases first.
Craig Toppere4fd9072012-03-04 10:43:23 +0000212 for (const uint16_t *AS = TRI->getAliasSet(Def); *AS; ++AS) {
Evan Chengb266cd02012-02-27 21:46:42 +0000213 CopyMap.erase(*AS);
214 AvailCopyMap.erase(*AS);
215 }
Evan Cheng977679d2012-01-07 03:02:36 +0000216 CopyMap[Def] = MI;
217 AvailCopyMap[Def] = MI;
Craig Topper9ebfbf82012-03-05 05:37:41 +0000218 for (const uint16_t *SR = TRI->getSubRegisters(Def); *SR; ++SR) {
Evan Cheng977679d2012-01-07 03:02:36 +0000219 CopyMap[*SR] = MI;
220 AvailCopyMap[*SR] = MI;
221 }
222
223 // Remember source that's copied to Def. Once it's clobbered, then
224 // it's no longer available for copy propagation.
Lang Hamesd9eb1d72012-03-27 00:44:47 +0000225 SrcMap[Src].insert(Def);
Evan Cheng977679d2012-01-07 03:02:36 +0000226
227 continue;
228 }
229
230 // Not a copy.
231 SmallVector<unsigned, 2> Defs;
Jakob Stoklund Olesenf56ce532012-02-09 00:19:08 +0000232 int RegMaskOpNum = -1;
Evan Cheng977679d2012-01-07 03:02:36 +0000233 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
234 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesena8fc1712012-02-08 22:37:35 +0000235 if (MO.isRegMask())
Jakob Stoklund Olesenf56ce532012-02-09 00:19:08 +0000236 RegMaskOpNum = i;
Evan Cheng977679d2012-01-07 03:02:36 +0000237 if (!MO.isReg())
238 continue;
239 unsigned Reg = MO.getReg();
240 if (!Reg)
241 continue;
242
243 if (TargetRegisterInfo::isVirtualRegister(Reg))
244 report_fatal_error("MachineCopyPropagation should be run after"
245 " register allocation!");
246
247 if (MO.isDef()) {
248 Defs.push_back(Reg);
249 continue;
250 }
251
252 // If 'Reg' is defined by a copy, the copy is no longer a candidate
253 // for elimination.
254 DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(Reg);
255 if (CI != CopyMap.end())
256 MaybeDeadCopies.remove(CI->second);
Craig Toppere4fd9072012-03-04 10:43:23 +0000257 for (const uint16_t *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
Evan Cheng977679d2012-01-07 03:02:36 +0000258 CI = CopyMap.find(*AS);
259 if (CI != CopyMap.end())
260 MaybeDeadCopies.remove(CI->second);
261 }
262 }
263
Jakob Stoklund Olesena8fc1712012-02-08 22:37:35 +0000264 // The instruction has a register mask operand which means that it clobbers
265 // a large set of registers. It is possible to use the register mask to
266 // prune the available copies, but treat it like a basic block boundary for
267 // now.
Jakob Stoklund Olesenf56ce532012-02-09 00:19:08 +0000268 if (RegMaskOpNum >= 0) {
269 // Erase any MaybeDeadCopies whose destination register is clobbered.
270 const MachineOperand &MaskMO = MI->getOperand(RegMaskOpNum);
271 for (SmallSetVector<MachineInstr*, 8>::iterator
272 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
273 DI != DE; ++DI) {
274 unsigned Reg = (*DI)->getOperand(0).getReg();
275 if (ReservedRegs.test(Reg) || !MaskMO.clobbersPhysReg(Reg))
276 continue;
277 (*DI)->eraseFromParent();
278 Changed = true;
279 ++NumDeletes;
280 }
281
282 // Clear all data structures as if we were beginning a new basic block.
Jakob Stoklund Olesena8fc1712012-02-08 22:37:35 +0000283 MaybeDeadCopies.clear();
284 AvailCopyMap.clear();
285 CopyMap.clear();
286 SrcMap.clear();
287 continue;
288 }
289
Evan Cheng977679d2012-01-07 03:02:36 +0000290 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
291 unsigned Reg = Defs[i];
292
293 // No longer defined by a copy.
294 CopyMap.erase(Reg);
295 AvailCopyMap.erase(Reg);
Craig Toppere4fd9072012-03-04 10:43:23 +0000296 for (const uint16_t *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
Evan Cheng977679d2012-01-07 03:02:36 +0000297 CopyMap.erase(*AS);
298 AvailCopyMap.erase(*AS);
299 }
300
301 // If 'Reg' is previously source of a copy, it is no longer available for
302 // copy propagation.
303 SourceNoLongerAvailable(Reg, SrcMap, AvailCopyMap);
304 }
305 }
306
307 // If MBB doesn't have successors, delete the copies whose defs are not used.
308 // If MBB does have successors, then conservative assume the defs are live-out
309 // since we don't want to trust live-in lists.
310 if (MBB.succ_empty()) {
311 for (SmallSetVector<MachineInstr*, 8>::iterator
312 DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
313 DI != DE; ++DI) {
314 if (!ReservedRegs.test((*DI)->getOperand(0).getReg())) {
315 (*DI)->eraseFromParent();
316 Changed = true;
317 ++NumDeletes;
318 }
319 }
320 }
321
322 return Changed;
323}
324
325bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
326 bool Changed = false;
327
328 TRI = MF.getTarget().getRegisterInfo();
329 ReservedRegs = TRI->getReservedRegs(MF);
330
331 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
332 Changed |= CopyPropagateBlock(*I);
333
334 return Changed;
335}