blob: d2af95cdd9b464d58aa9e77de37087f03699a81b [file] [log] [blame]
Evan Cheng00b1a3c2012-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
Evan Cheng00b1a3c2012-01-07 03:02:36 +000014#include "llvm/CodeGen/Passes.h"
Evan Cheng00b1a3c2012-01-07 03:02:36 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/raw_ostream.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000027#include "llvm/Target/TargetSubtargetInfo.h"
Evan Cheng00b1a3c2012-01-07 03:02:36 +000028using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "codegen-cp"
31
Evan Cheng00b1a3c2012-01-07 03:02:36 +000032STATISTIC(NumDeletes, "Number of dead copies deleted");
33
34namespace {
35 class MachineCopyPropagation : public MachineFunctionPass {
36 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesenbb1e9832012-11-30 23:53:00 +000037 const TargetInstrInfo *TII;
Matthias Braun273575d2016-02-20 03:56:36 +000038 const MachineRegisterInfo *MRI;
Andrew Trick9e761992012-02-08 21:22:43 +000039
Evan Cheng00b1a3c2012-01-07 03:02:36 +000040 public:
41 static char ID; // Pass identification, replacement for typeid
42 MachineCopyPropagation() : MachineFunctionPass(ID) {
Matthias Braun273575d2016-02-20 03:56:36 +000043 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
Evan Cheng00b1a3c2012-01-07 03:02:36 +000044 }
45
Craig Topper4584cd52014-03-07 09:26:03 +000046 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000047
48 private:
Lang Hames5544bf12012-03-27 19:10:45 +000049 typedef SmallVector<unsigned, 4> DestList;
50 typedef DenseMap<unsigned, DestList> SourceMap;
51
Matthias Braunbd18d752016-02-20 03:56:39 +000052 void SourceNoLongerAvailable(unsigned Reg);
53 void CopyPropagateBlock(MachineBasicBlock &MBB);
54
55 /// Candidates for deletion.
56 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
57 /// Def -> available copies map.
58 DenseMap<unsigned, MachineInstr*> AvailCopyMap;
59 /// Def -> copies map.
60 DenseMap<unsigned, MachineInstr*> CopyMap;
61 /// Src -> Def map
62 SourceMap SrcMap;
63 bool Changed;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000064 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000065}
Evan Cheng00b1a3c2012-01-07 03:02:36 +000066char MachineCopyPropagation::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000067char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000068
69INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
70 "Machine Copy Propagation Pass", false, false)
71
Matthias Braunbd18d752016-02-20 03:56:39 +000072void MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +000073 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
74 SourceMap::iterator SI = SrcMap.find(*AI);
Evan Cheng00b1a3c2012-01-07 03:02:36 +000075 if (SI != SrcMap.end()) {
Lang Hames5544bf12012-03-27 19:10:45 +000076 const DestList& Defs = SI->second;
Matthias Braun273575d2016-02-20 03:56:36 +000077 for (unsigned MappedDef : Defs) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +000078 // Source of copy is no longer available for propagation.
Matthias Braun273575d2016-02-20 03:56:36 +000079 for (MCSubRegIterator SR(MappedDef, TRI, true); SR.isValid(); ++SR)
Hao Liu04183242015-03-13 05:15:23 +000080 AvailCopyMap.erase(*SR);
Evan Cheng00b1a3c2012-01-07 03:02:36 +000081 }
82 }
83 }
84}
85
Evan Cheng520730f2012-01-08 19:52:28 +000086static bool NoInterveningSideEffect(const MachineInstr *CopyMI,
87 const MachineInstr *MI) {
88 const MachineBasicBlock *MBB = CopyMI->getParent();
89 if (MI->getParent() != MBB)
90 return false;
Evan Cheng520730f2012-01-08 19:52:28 +000091
Matthias Braun273575d2016-02-20 03:56:36 +000092 for (MachineBasicBlock::const_iterator I = std::next(CopyMI->getIterator()),
93 E = MBB->end(), E2 = MI->getIterator(); I != E && I != E2; ++I) {
Evan Cheng520730f2012-01-08 19:52:28 +000094 if (I->hasUnmodeledSideEffects() || I->isCall() ||
95 I->isTerminator())
96 return false;
Evan Cheng520730f2012-01-08 19:52:28 +000097 }
98 return true;
99}
100
Evan Cheng63618f92012-02-20 23:28:17 +0000101/// 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.
105/// ecx = mov eax
106/// al = mov cl
107/// But not
108/// ecx = mov eax
109/// al = mov ch
Matthias Braun273575d2016-02-20 03:56:36 +0000110static bool isNopCopy(const MachineInstr *CopyMI, unsigned Def, unsigned Src,
Evan Cheng63618f92012-02-20 23:28:17 +0000111 const TargetRegisterInfo *TRI) {
112 unsigned SrcSrc = CopyMI->getOperand(1).getReg();
113 if (Def == SrcSrc)
114 return true;
115 if (TRI->isSubRegister(SrcSrc, Def)) {
116 unsigned SrcDef = CopyMI->getOperand(0).getReg();
117 unsigned SubIdx = TRI->getSubRegIndex(SrcSrc, Def);
118 if (!SubIdx)
119 return false;
120 return SubIdx == TRI->getSubRegIndex(SrcDef, Src);
121 }
122
123 return false;
124}
125
Matthias Braunbd18d752016-02-20 03:56:39 +0000126void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
James Molloyd787d3e2014-01-22 09:12:27 +0000127 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
128
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000129 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
130 MachineInstr *MI = &*I;
131 ++I;
132
133 if (MI->isCopy()) {
134 unsigned Def = MI->getOperand(0).getReg();
135 unsigned Src = MI->getOperand(1).getReg();
136
Matthias Braun57b5f112016-02-20 03:56:33 +0000137 assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
138 !TargetRegisterInfo::isVirtualRegister(Src) &&
139 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000140
141 DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src);
142 if (CI != AvailCopyMap.end()) {
143 MachineInstr *CopyMI = CI->second;
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000144 if (!MRI->isReserved(Def) &&
145 (!MRI->isReserved(Src) || NoInterveningSideEffect(CopyMI, MI)) &&
Evan Cheng63618f92012-02-20 23:28:17 +0000146 isNopCopy(CopyMI, Def, Src, TRI)) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000147 // The two copies cancel out and the source of the first copy
148 // hasn't been overridden, eliminate the second one. e.g.
149 // %ECX<def> = COPY %EAX<kill>
150 // ... nothing clobbered EAX.
151 // %EAX<def> = COPY %ECX
152 // =>
153 // %ECX<def> = COPY %EAX
Evan Cheng520730f2012-01-08 19:52:28 +0000154 //
155 // Also avoid eliminating a copy from reserved registers unless the
156 // definition is proven not clobbered. e.g.
157 // %RSP<def> = COPY %RAX
158 // CALL
159 // %RAX<def> = COPY %RSP
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +0000160
James Molloyd787d3e2014-01-22 09:12:27 +0000161 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; MI->dump());
162
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +0000163 // Clear any kills of Def between CopyMI and MI. This extends the
164 // live range.
Matthias Braun273575d2016-02-20 03:56:36 +0000165 for (MachineInstr &MMI
166 : make_range(CopyMI->getIterator(), MI->getIterator()))
167 MMI.clearRegisterKills(Def, TRI);
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +0000168
Matthias Braun165d4672015-05-29 18:19:25 +0000169 MI->eraseFromParent();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000170 Changed = true;
171 ++NumDeletes;
172 continue;
173 }
174 }
175
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000176 // If Src is defined by a previous copy, the previous copy cannot be
177 // eliminated.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000178 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
179 CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000180 if (CI != CopyMap.end()) {
181 DEBUG(dbgs() << "MCP: Copy is no longer dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000182 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000183 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000184 }
185
James Molloyd787d3e2014-01-22 09:12:27 +0000186 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
187
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000188 // Copy is now a candidate for deletion.
Matthias Braun273575d2016-02-20 03:56:36 +0000189 if (!MRI->isReserved(Def))
190 MaybeDeadCopies.insert(MI);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000191
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000192 // If 'Def' is previously source of another copy, then this earlier copy's
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000193 // source is no longer available. e.g.
194 // %xmm9<def> = copy %xmm2
195 // ...
196 // %xmm2<def> = copy %xmm0
197 // ...
198 // %xmm2<def> = copy %xmm9
Matthias Braunbd18d752016-02-20 03:56:39 +0000199 SourceNoLongerAvailable(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000200
201 // Remember Def is defined by the copy.
Evan Chengddeb9d12012-02-27 21:46:42 +0000202 // ... Make sure to clear the def maps of aliases first.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000203 for (MCRegAliasIterator AI(Def, TRI, false); AI.isValid(); ++AI) {
204 CopyMap.erase(*AI);
205 AvailCopyMap.erase(*AI);
Evan Chengddeb9d12012-02-27 21:46:42 +0000206 }
Chad Rosierabdb1d62013-05-22 23:17:36 +0000207 for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
208 ++SR) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000209 CopyMap[*SR] = MI;
210 AvailCopyMap[*SR] = MI;
211 }
212
213 // Remember source that's copied to Def. Once it's clobbered, then
214 // it's no longer available for copy propagation.
Matthias Braun273575d2016-02-20 03:56:36 +0000215 SmallVectorImpl<unsigned> &DestList = SrcMap[Src];
216 if (std::find(DestList.begin(), DestList.end(), Def) == DestList.end())
217 DestList.push_back(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000218
219 continue;
220 }
221
222 // Not a copy.
223 SmallVector<unsigned, 2> Defs;
Matthias Braun273575d2016-02-20 03:56:36 +0000224 const MachineOperand *RegMask = nullptr;
225 for (const MachineOperand &MO : MI->operands()) {
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000226 if (MO.isRegMask())
Matthias Braun273575d2016-02-20 03:56:36 +0000227 RegMask = &MO;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000228 if (!MO.isReg())
229 continue;
230 unsigned Reg = MO.getReg();
231 if (!Reg)
232 continue;
233
Matthias Braun57b5f112016-02-20 03:56:33 +0000234 assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
235 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000236
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000237 if (MO.isDef()) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000238 Defs.push_back(Reg);
239 continue;
240 }
241
242 // If 'Reg' is defined by a copy, the copy is no longer a candidate
243 // for elimination.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000244 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
245 DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000246 if (CI != CopyMap.end()) {
247 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000248 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000249 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000250 }
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000251 // Treat undef use like defs for copy propagation but not for
252 // dead copy. We would need to do a liveness check to be sure the copy
253 // is dead for undef uses.
254 // The backends are allowed to do whatever they want with undef value
255 // and we cannot be sure this register will not be rewritten to break
256 // some false dependencies for the hardware for instance.
257 if (MO.isUndef())
258 Defs.push_back(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000259 }
260
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000261 // The instruction has a register mask operand which means that it clobbers
262 // a large set of registers. It is possible to use the register mask to
263 // prune the available copies, but treat it like a basic block boundary for
264 // now.
Matthias Braun273575d2016-02-20 03:56:36 +0000265 if (RegMask) {
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000266 // Erase any MaybeDeadCopies whose destination register is clobbered.
Matthias Braun273575d2016-02-20 03:56:36 +0000267 for (MachineInstr *MaybeDead : MaybeDeadCopies) {
268 unsigned Reg = MaybeDead->getOperand(0).getReg();
269 assert(!MRI->isReserved(Reg));
270 if (!RegMask->clobbersPhysReg(Reg))
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000271 continue;
James Molloyd787d3e2014-01-22 09:12:27 +0000272 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
Matthias Braun273575d2016-02-20 03:56:36 +0000273 MaybeDead->dump());
274 MaybeDead->eraseFromParent();
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000275 Changed = true;
276 ++NumDeletes;
277 }
278
279 // Clear all data structures as if we were beginning a new basic block.
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000280 MaybeDeadCopies.clear();
281 AvailCopyMap.clear();
282 CopyMap.clear();
283 SrcMap.clear();
284 continue;
285 }
286
Matthias Braun273575d2016-02-20 03:56:36 +0000287 for (unsigned Reg : Defs) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000288 // No longer defined by a copy.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000289 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
290 CopyMap.erase(*AI);
291 AvailCopyMap.erase(*AI);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000292 }
293
294 // If 'Reg' is previously source of a copy, it is no longer available for
295 // copy propagation.
Matthias Braunbd18d752016-02-20 03:56:39 +0000296 SourceNoLongerAvailable(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000297 }
298 }
299
300 // If MBB doesn't have successors, delete the copies whose defs are not used.
301 // If MBB does have successors, then conservative assume the defs are live-out
302 // since we don't want to trust live-in lists.
303 if (MBB.succ_empty()) {
Matthias Braun273575d2016-02-20 03:56:36 +0000304 for (MachineInstr *MaybeDead : MaybeDeadCopies) {
305 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
306 MaybeDead->eraseFromParent();
307 Changed = true;
308 ++NumDeletes;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000309 }
310 }
311
Matthias Braunbd18d752016-02-20 03:56:39 +0000312 MaybeDeadCopies.clear();
313 AvailCopyMap.clear();
314 CopyMap.clear();
315 SrcMap.clear();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000316}
317
318bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000319 if (skipOptnoneFunction(*MF.getFunction()))
320 return false;
321
Matthias Braunbd18d752016-02-20 03:56:39 +0000322 Changed = false;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000323
Eric Christopherfc6de422014-08-05 02:39:49 +0000324 TRI = MF.getSubtarget().getRegisterInfo();
325 TII = MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000326 MRI = &MF.getRegInfo();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000327
Matthias Braun273575d2016-02-20 03:56:36 +0000328 for (MachineBasicBlock &MBB : MF)
Matthias Braunbd18d752016-02-20 03:56:39 +0000329 CopyPropagateBlock(MBB);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000330
331 return Changed;
332}