blob: 92d043df26b85601dace8e432aea12c59f414ff1 [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 {
Matthias Braune39ff702016-02-26 03:18:50 +000035 typedef SmallVector<unsigned, 4> RegList;
36 typedef DenseMap<unsigned, RegList> SourceMap;
37 typedef DenseMap<unsigned, MachineInstr*> Reg2MIMap;
38
Evan Cheng00b1a3c2012-01-07 03:02:36 +000039 class MachineCopyPropagation : public MachineFunctionPass {
40 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesenbb1e9832012-11-30 23:53:00 +000041 const TargetInstrInfo *TII;
Matthias Braun273575d2016-02-20 03:56:36 +000042 const MachineRegisterInfo *MRI;
Andrew Trick9e761992012-02-08 21:22:43 +000043
Evan Cheng00b1a3c2012-01-07 03:02:36 +000044 public:
45 static char ID; // Pass identification, replacement for typeid
46 MachineCopyPropagation() : MachineFunctionPass(ID) {
Matthias Braun273575d2016-02-20 03:56:36 +000047 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
Evan Cheng00b1a3c2012-01-07 03:02:36 +000048 }
49
Matt Arsenault8f4d43a2016-06-02 00:04:26 +000050 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesCFG();
52 MachineFunctionPass::getAnalysisUsage(AU);
53 }
54
Craig Topper4584cd52014-03-07 09:26:03 +000055 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000056
Derek Schuffad154c82016-03-28 17:05:30 +000057 MachineFunctionProperties getRequiredProperties() const override {
58 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000059 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +000060 }
61
Evan Cheng00b1a3c2012-01-07 03:02:36 +000062 private:
Matthias Braune39ff702016-02-26 03:18:50 +000063 void ClobberRegister(unsigned Reg);
Matthias Braunbd18d752016-02-20 03:56:39 +000064 void CopyPropagateBlock(MachineBasicBlock &MBB);
Matthias Braun9dcd65f2016-02-26 03:18:55 +000065 bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
Matthias Braunbd18d752016-02-20 03:56:39 +000066
67 /// Candidates for deletion.
68 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
69 /// Def -> available copies map.
Matthias Braunc65e9042016-02-20 03:56:41 +000070 Reg2MIMap AvailCopyMap;
Matthias Braunbd18d752016-02-20 03:56:39 +000071 /// Def -> copies map.
Matthias Braunc65e9042016-02-20 03:56:41 +000072 Reg2MIMap CopyMap;
Matthias Braunbd18d752016-02-20 03:56:39 +000073 /// Src -> Def map
74 SourceMap SrcMap;
75 bool Changed;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000076 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000077}
Evan Cheng00b1a3c2012-01-07 03:02:36 +000078char MachineCopyPropagation::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000079char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000080
81INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
82 "Machine Copy Propagation Pass", false, false)
83
Matthias Braune39ff702016-02-26 03:18:50 +000084/// Remove any entry in \p Map where the register is a subregister or equal to
85/// a register contained in \p Regs.
86static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
87 const TargetRegisterInfo &TRI) {
88 for (unsigned Reg : Regs) {
89 // Source of copy is no longer available for propagation.
90 for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
91 Map.erase(*SR);
Evan Cheng00b1a3c2012-01-07 03:02:36 +000092 }
93}
94
Matthias Braune39ff702016-02-26 03:18:50 +000095/// Remove any entry in \p Map that is marked clobbered in \p RegMask.
96/// The map will typically have a lot fewer entries than the regmask clobbers,
97/// so this is more efficient than iterating the clobbered registers and calling
98/// ClobberRegister() on them.
99static void removeClobberedRegsFromMap(Reg2MIMap &Map,
100 const MachineOperand &RegMask) {
101 for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
102 I = Next) {
103 Next = std::next(I);
104 unsigned Reg = I->first;
105 if (RegMask.clobbersPhysReg(Reg))
106 Map.erase(I);
Evan Cheng520730f2012-01-08 19:52:28 +0000107 }
Matthias Braune39ff702016-02-26 03:18:50 +0000108}
109
110void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
111 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
112 CopyMap.erase(*AI);
113 AvailCopyMap.erase(*AI);
114
115 SourceMap::iterator SI = SrcMap.find(*AI);
116 if (SI != SrcMap.end()) {
117 removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
118 SrcMap.erase(SI);
119 }
120 }
Evan Cheng520730f2012-01-08 19:52:28 +0000121}
122
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000123/// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
124/// This fact may have been obscured by sub register usage or may not be true at
125/// all even though Src and Def are subregisters of the registers used in
126/// PreviousCopy. e.g.
127/// isNopCopy("ecx = COPY eax", AX, CX) == true
128/// isNopCopy("ecx = COPY eax", AH, CL) == false
129static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
130 unsigned Def, const TargetRegisterInfo *TRI) {
131 unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
132 unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
133 if (Src == PreviousSrc) {
134 assert(Def == PreviousDef);
Evan Cheng63618f92012-02-20 23:28:17 +0000135 return true;
Evan Cheng63618f92012-02-20 23:28:17 +0000136 }
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000137 if (!TRI->isSubRegister(PreviousSrc, Src))
138 return false;
139 unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
140 return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
141}
Evan Cheng63618f92012-02-20 23:28:17 +0000142
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000143/// Remove instruction \p Copy if there exists a previous copy that copies the
144/// register \p Src to the register \p Def; This may happen indirectly by
145/// copying the super registers.
146bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
147 unsigned Def) {
148 // Avoid eliminating a copy from/to a reserved registers as we cannot predict
149 // the value (Example: The sparc zero register is writable but stays zero).
150 if (MRI->isReserved(Src) || MRI->isReserved(Def))
151 return false;
152
153 // Search for an existing copy.
154 Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
155 if (CI == AvailCopyMap.end())
156 return false;
157
158 // Check that the existing copy uses the correct sub registers.
159 MachineInstr &PrevCopy = *CI->second;
160 if (!isNopCopy(PrevCopy, Src, Def, TRI))
161 return false;
162
163 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
164
165 // Copy was redundantly redefining either Src or Def. Remove earlier kill
166 // flags between Copy and PrevCopy because the value will be reused now.
167 assert(Copy.isCopy());
168 unsigned CopyDef = Copy.getOperand(0).getReg();
169 assert(CopyDef == Src || CopyDef == Def);
170 for (MachineInstr &MI :
171 make_range(PrevCopy.getIterator(), Copy.getIterator()))
172 MI.clearRegisterKills(CopyDef, TRI);
173
174 Copy.eraseFromParent();
175 Changed = true;
176 ++NumDeletes;
177 return true;
Evan Cheng63618f92012-02-20 23:28:17 +0000178}
179
Matthias Braunbd18d752016-02-20 03:56:39 +0000180void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
James Molloyd787d3e2014-01-22 09:12:27 +0000181 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
182
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000183 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
184 MachineInstr *MI = &*I;
185 ++I;
186
187 if (MI->isCopy()) {
188 unsigned Def = MI->getOperand(0).getReg();
189 unsigned Src = MI->getOperand(1).getReg();
190
Matthias Braun57b5f112016-02-20 03:56:33 +0000191 assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
192 !TargetRegisterInfo::isVirtualRegister(Src) &&
193 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000194
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000195 // The two copies cancel out and the source of the first copy
196 // hasn't been overridden, eliminate the second one. e.g.
197 // %ECX<def> = COPY %EAX
198 // ... nothing clobbered EAX.
199 // %EAX<def> = COPY %ECX
200 // =>
201 // %ECX<def> = COPY %EAX
202 //
203 // or
204 //
205 // %ECX<def> = COPY %EAX
206 // ... nothing clobbered EAX.
207 // %ECX<def> = COPY %EAX
208 // =>
209 // %ECX<def> = COPY %EAX
210 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
211 continue;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000212
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000213 // If Src is defined by a previous copy, the previous copy cannot be
214 // eliminated.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000215 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
Matthias Braunc65e9042016-02-20 03:56:41 +0000216 Reg2MIMap::iterator CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000217 if (CI != CopyMap.end()) {
218 DEBUG(dbgs() << "MCP: Copy is no longer dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000219 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000220 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000221 }
222
James Molloyd787d3e2014-01-22 09:12:27 +0000223 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
224
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000225 // Copy is now a candidate for deletion.
Matthias Braun273575d2016-02-20 03:56:36 +0000226 if (!MRI->isReserved(Def))
227 MaybeDeadCopies.insert(MI);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000228
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000229 // If 'Def' is previously source of another copy, then this earlier copy's
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000230 // source is no longer available. e.g.
231 // %xmm9<def> = copy %xmm2
232 // ...
233 // %xmm2<def> = copy %xmm0
234 // ...
235 // %xmm2<def> = copy %xmm9
Matthias Braune39ff702016-02-26 03:18:50 +0000236 ClobberRegister(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000237
238 // Remember Def is defined by the copy.
Chad Rosierabdb1d62013-05-22 23:17:36 +0000239 for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
240 ++SR) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000241 CopyMap[*SR] = MI;
242 AvailCopyMap[*SR] = MI;
243 }
244
245 // Remember source that's copied to Def. Once it's clobbered, then
246 // it's no longer available for copy propagation.
Matthias Braune39ff702016-02-26 03:18:50 +0000247 RegList &DestList = SrcMap[Src];
David Majnemer0d955d02016-08-11 22:21:41 +0000248 if (!is_contained(DestList, Def))
Matthias Braun273575d2016-02-20 03:56:36 +0000249 DestList.push_back(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000250
251 continue;
252 }
253
254 // Not a copy.
255 SmallVector<unsigned, 2> Defs;
Matthias Braun273575d2016-02-20 03:56:36 +0000256 const MachineOperand *RegMask = nullptr;
257 for (const MachineOperand &MO : MI->operands()) {
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000258 if (MO.isRegMask())
Matthias Braun273575d2016-02-20 03:56:36 +0000259 RegMask = &MO;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000260 if (!MO.isReg())
261 continue;
262 unsigned Reg = MO.getReg();
263 if (!Reg)
264 continue;
265
Matthias Braun57b5f112016-02-20 03:56:33 +0000266 assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
267 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000268
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000269 if (MO.isDef()) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000270 Defs.push_back(Reg);
271 continue;
272 }
273
274 // If 'Reg' is defined by a copy, the copy is no longer a candidate
275 // for elimination.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000276 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Matthias Braunc65e9042016-02-20 03:56:41 +0000277 Reg2MIMap::iterator CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000278 if (CI != CopyMap.end()) {
279 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000280 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000281 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000282 }
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000283 // Treat undef use like defs for copy propagation but not for
284 // dead copy. We would need to do a liveness check to be sure the copy
285 // is dead for undef uses.
286 // The backends are allowed to do whatever they want with undef value
287 // and we cannot be sure this register will not be rewritten to break
288 // some false dependencies for the hardware for instance.
289 if (MO.isUndef())
290 Defs.push_back(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000291 }
292
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000293 // The instruction has a register mask operand which means that it clobbers
Matthias Braune39ff702016-02-26 03:18:50 +0000294 // a large set of registers. Treat clobbered registers the same way as
295 // defined registers.
Matthias Braun273575d2016-02-20 03:56:36 +0000296 if (RegMask) {
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000297 // Erase any MaybeDeadCopies whose destination register is clobbered.
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000298 for (SmallSetVector<MachineInstr *, 8>::iterator DI =
299 MaybeDeadCopies.begin();
300 DI != MaybeDeadCopies.end();) {
301 MachineInstr *MaybeDead = *DI;
Matthias Braun273575d2016-02-20 03:56:36 +0000302 unsigned Reg = MaybeDead->getOperand(0).getReg();
303 assert(!MRI->isReserved(Reg));
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000304
305 if (!RegMask->clobbersPhysReg(Reg)) {
306 ++DI;
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000307 continue;
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000308 }
309
James Molloyd787d3e2014-01-22 09:12:27 +0000310 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
Matthias Braun273575d2016-02-20 03:56:36 +0000311 MaybeDead->dump());
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000312
313 // erase() will return the next valid iterator pointing to the next
314 // element after the erased one.
315 DI = MaybeDeadCopies.erase(DI);
Matthias Braun273575d2016-02-20 03:56:36 +0000316 MaybeDead->eraseFromParent();
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000317 Changed = true;
318 ++NumDeletes;
319 }
Matthias Braune39ff702016-02-26 03:18:50 +0000320
321 removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
322 removeClobberedRegsFromMap(CopyMap, *RegMask);
323 for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
324 I != E; I = Next) {
325 Next = std::next(I);
326 if (RegMask->clobbersPhysReg(I->first)) {
327 removeRegsFromMap(AvailCopyMap, I->second, *TRI);
328 SrcMap.erase(I);
329 }
330 }
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000331 }
332
Matthias Braune39ff702016-02-26 03:18:50 +0000333 // Any previous copy definition or reading the Defs is no longer available.
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000334 for (unsigned Reg : Defs)
Matthias Braune39ff702016-02-26 03:18:50 +0000335 ClobberRegister(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000336 }
337
338 // If MBB doesn't have successors, delete the copies whose defs are not used.
339 // If MBB does have successors, then conservative assume the defs are live-out
340 // since we don't want to trust live-in lists.
341 if (MBB.succ_empty()) {
Matthias Braun273575d2016-02-20 03:56:36 +0000342 for (MachineInstr *MaybeDead : MaybeDeadCopies) {
343 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
344 MaybeDead->eraseFromParent();
345 Changed = true;
346 ++NumDeletes;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000347 }
348 }
349
Matthias Braunbd18d752016-02-20 03:56:39 +0000350 MaybeDeadCopies.clear();
351 AvailCopyMap.clear();
352 CopyMap.clear();
353 SrcMap.clear();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000354}
355
356bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000357 if (skipFunction(*MF.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000358 return false;
359
Matthias Braunbd18d752016-02-20 03:56:39 +0000360 Changed = false;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000361
Eric Christopherfc6de422014-08-05 02:39:49 +0000362 TRI = MF.getSubtarget().getRegisterInfo();
363 TII = MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000364 MRI = &MF.getRegInfo();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000365
Matthias Braun273575d2016-02-20 03:56:36 +0000366 for (MachineBasicBlock &MBB : MF)
Matthias Braunbd18d752016-02-20 03:56:39 +0000367 CopyPropagateBlock(MBB);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000368
369 return Changed;
370}
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000371