blob: 3a6f2b5330eef8d328c61259686350fa84b5db0c [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
Craig Topper4584cd52014-03-07 09:26:03 +000050 bool runOnMachineFunction(MachineFunction &MF) override;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000051
52 private:
Matthias Braune39ff702016-02-26 03:18:50 +000053 void ClobberRegister(unsigned Reg);
Matthias Braunbd18d752016-02-20 03:56:39 +000054 void CopyPropagateBlock(MachineBasicBlock &MBB);
Matthias Braun9dcd65f2016-02-26 03:18:55 +000055 bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
Matthias Braunbd18d752016-02-20 03:56:39 +000056
57 /// Candidates for deletion.
58 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
59 /// Def -> available copies map.
Matthias Braunc65e9042016-02-20 03:56:41 +000060 Reg2MIMap AvailCopyMap;
Matthias Braunbd18d752016-02-20 03:56:39 +000061 /// Def -> copies map.
Matthias Braunc65e9042016-02-20 03:56:41 +000062 Reg2MIMap CopyMap;
Matthias Braunbd18d752016-02-20 03:56:39 +000063 /// Src -> Def map
64 SourceMap SrcMap;
65 bool Changed;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000066 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000067}
Evan Cheng00b1a3c2012-01-07 03:02:36 +000068char MachineCopyPropagation::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000069char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000070
71INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
72 "Machine Copy Propagation Pass", false, false)
73
Matthias Braune39ff702016-02-26 03:18:50 +000074/// Remove any entry in \p Map where the register is a subregister or equal to
75/// a register contained in \p Regs.
76static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
77 const TargetRegisterInfo &TRI) {
78 for (unsigned Reg : Regs) {
79 // Source of copy is no longer available for propagation.
80 for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
81 Map.erase(*SR);
Evan Cheng00b1a3c2012-01-07 03:02:36 +000082 }
83}
84
Matthias Braune39ff702016-02-26 03:18:50 +000085/// Remove any entry in \p Map that is marked clobbered in \p RegMask.
86/// The map will typically have a lot fewer entries than the regmask clobbers,
87/// so this is more efficient than iterating the clobbered registers and calling
88/// ClobberRegister() on them.
89static void removeClobberedRegsFromMap(Reg2MIMap &Map,
90 const MachineOperand &RegMask) {
91 for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
92 I = Next) {
93 Next = std::next(I);
94 unsigned Reg = I->first;
95 if (RegMask.clobbersPhysReg(Reg))
96 Map.erase(I);
Evan Cheng520730f2012-01-08 19:52:28 +000097 }
Matthias Braune39ff702016-02-26 03:18:50 +000098}
99
100void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
101 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
102 CopyMap.erase(*AI);
103 AvailCopyMap.erase(*AI);
104
105 SourceMap::iterator SI = SrcMap.find(*AI);
106 if (SI != SrcMap.end()) {
107 removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
108 SrcMap.erase(SI);
109 }
110 }
Evan Cheng520730f2012-01-08 19:52:28 +0000111}
112
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000113/// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
114/// This fact may have been obscured by sub register usage or may not be true at
115/// all even though Src and Def are subregisters of the registers used in
116/// PreviousCopy. e.g.
117/// isNopCopy("ecx = COPY eax", AX, CX) == true
118/// isNopCopy("ecx = COPY eax", AH, CL) == false
119static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
120 unsigned Def, const TargetRegisterInfo *TRI) {
121 unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
122 unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
123 if (Src == PreviousSrc) {
124 assert(Def == PreviousDef);
Evan Cheng63618f92012-02-20 23:28:17 +0000125 return true;
Evan Cheng63618f92012-02-20 23:28:17 +0000126 }
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000127 if (!TRI->isSubRegister(PreviousSrc, Src))
128 return false;
129 unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
130 return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
131}
Evan Cheng63618f92012-02-20 23:28:17 +0000132
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000133/// Remove instruction \p Copy if there exists a previous copy that copies the
134/// register \p Src to the register \p Def; This may happen indirectly by
135/// copying the super registers.
136bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
137 unsigned Def) {
138 // Avoid eliminating a copy from/to a reserved registers as we cannot predict
139 // the value (Example: The sparc zero register is writable but stays zero).
140 if (MRI->isReserved(Src) || MRI->isReserved(Def))
141 return false;
142
143 // Search for an existing copy.
144 Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
145 if (CI == AvailCopyMap.end())
146 return false;
147
148 // Check that the existing copy uses the correct sub registers.
149 MachineInstr &PrevCopy = *CI->second;
150 if (!isNopCopy(PrevCopy, Src, Def, TRI))
151 return false;
152
153 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
154
155 // Copy was redundantly redefining either Src or Def. Remove earlier kill
156 // flags between Copy and PrevCopy because the value will be reused now.
157 assert(Copy.isCopy());
158 unsigned CopyDef = Copy.getOperand(0).getReg();
159 assert(CopyDef == Src || CopyDef == Def);
160 for (MachineInstr &MI :
161 make_range(PrevCopy.getIterator(), Copy.getIterator()))
162 MI.clearRegisterKills(CopyDef, TRI);
163
164 Copy.eraseFromParent();
165 Changed = true;
166 ++NumDeletes;
167 return true;
Evan Cheng63618f92012-02-20 23:28:17 +0000168}
169
Matthias Braunbd18d752016-02-20 03:56:39 +0000170void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
James Molloyd787d3e2014-01-22 09:12:27 +0000171 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
172
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000173 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
174 MachineInstr *MI = &*I;
175 ++I;
176
177 if (MI->isCopy()) {
178 unsigned Def = MI->getOperand(0).getReg();
179 unsigned Src = MI->getOperand(1).getReg();
180
Matthias Braun57b5f112016-02-20 03:56:33 +0000181 assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
182 !TargetRegisterInfo::isVirtualRegister(Src) &&
183 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000184
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000185 // The two copies cancel out and the source of the first copy
186 // hasn't been overridden, eliminate the second one. e.g.
187 // %ECX<def> = COPY %EAX
188 // ... nothing clobbered EAX.
189 // %EAX<def> = COPY %ECX
190 // =>
191 // %ECX<def> = COPY %EAX
192 //
193 // or
194 //
195 // %ECX<def> = COPY %EAX
196 // ... nothing clobbered EAX.
197 // %ECX<def> = COPY %EAX
198 // =>
199 // %ECX<def> = COPY %EAX
200 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
201 continue;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000202
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000203 // If Src is defined by a previous copy, the previous copy cannot be
204 // eliminated.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000205 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
Matthias Braunc65e9042016-02-20 03:56:41 +0000206 Reg2MIMap::iterator CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000207 if (CI != CopyMap.end()) {
208 DEBUG(dbgs() << "MCP: Copy is no longer dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000209 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000210 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000211 }
212
James Molloyd787d3e2014-01-22 09:12:27 +0000213 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
214
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000215 // Copy is now a candidate for deletion.
Matthias Braun273575d2016-02-20 03:56:36 +0000216 if (!MRI->isReserved(Def))
217 MaybeDeadCopies.insert(MI);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000218
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000219 // If 'Def' is previously source of another copy, then this earlier copy's
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000220 // source is no longer available. e.g.
221 // %xmm9<def> = copy %xmm2
222 // ...
223 // %xmm2<def> = copy %xmm0
224 // ...
225 // %xmm2<def> = copy %xmm9
Matthias Braune39ff702016-02-26 03:18:50 +0000226 ClobberRegister(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000227
228 // Remember Def is defined by the copy.
Chad Rosierabdb1d62013-05-22 23:17:36 +0000229 for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
230 ++SR) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000231 CopyMap[*SR] = MI;
232 AvailCopyMap[*SR] = MI;
233 }
234
235 // Remember source that's copied to Def. Once it's clobbered, then
236 // it's no longer available for copy propagation.
Matthias Braune39ff702016-02-26 03:18:50 +0000237 RegList &DestList = SrcMap[Src];
Matthias Braun273575d2016-02-20 03:56:36 +0000238 if (std::find(DestList.begin(), DestList.end(), Def) == DestList.end())
239 DestList.push_back(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000240
241 continue;
242 }
243
244 // Not a copy.
245 SmallVector<unsigned, 2> Defs;
Matthias Braun273575d2016-02-20 03:56:36 +0000246 const MachineOperand *RegMask = nullptr;
247 for (const MachineOperand &MO : MI->operands()) {
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000248 if (MO.isRegMask())
Matthias Braun273575d2016-02-20 03:56:36 +0000249 RegMask = &MO;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000250 if (!MO.isReg())
251 continue;
252 unsigned Reg = MO.getReg();
253 if (!Reg)
254 continue;
255
Matthias Braun57b5f112016-02-20 03:56:33 +0000256 assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
257 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000258
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000259 if (MO.isDef()) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000260 Defs.push_back(Reg);
261 continue;
262 }
263
264 // If 'Reg' is defined by a copy, the copy is no longer a candidate
265 // for elimination.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000266 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Matthias Braunc65e9042016-02-20 03:56:41 +0000267 Reg2MIMap::iterator CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000268 if (CI != CopyMap.end()) {
269 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000270 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000271 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000272 }
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000273 // Treat undef use like defs for copy propagation but not for
274 // dead copy. We would need to do a liveness check to be sure the copy
275 // is dead for undef uses.
276 // The backends are allowed to do whatever they want with undef value
277 // and we cannot be sure this register will not be rewritten to break
278 // some false dependencies for the hardware for instance.
279 if (MO.isUndef())
280 Defs.push_back(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000281 }
282
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000283 // The instruction has a register mask operand which means that it clobbers
Matthias Braune39ff702016-02-26 03:18:50 +0000284 // a large set of registers. Treat clobbered registers the same way as
285 // defined registers.
Matthias Braun273575d2016-02-20 03:56:36 +0000286 if (RegMask) {
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000287 // Erase any MaybeDeadCopies whose destination register is clobbered.
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000288 for (SmallSetVector<MachineInstr *, 8>::iterator DI =
289 MaybeDeadCopies.begin();
290 DI != MaybeDeadCopies.end();) {
291 MachineInstr *MaybeDead = *DI;
Matthias Braun273575d2016-02-20 03:56:36 +0000292 unsigned Reg = MaybeDead->getOperand(0).getReg();
293 assert(!MRI->isReserved(Reg));
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000294
295 if (!RegMask->clobbersPhysReg(Reg)) {
296 ++DI;
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000297 continue;
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000298 }
299
James Molloyd787d3e2014-01-22 09:12:27 +0000300 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
Matthias Braun273575d2016-02-20 03:56:36 +0000301 MaybeDead->dump());
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000302
303 // erase() will return the next valid iterator pointing to the next
304 // element after the erased one.
305 DI = MaybeDeadCopies.erase(DI);
Matthias Braun273575d2016-02-20 03:56:36 +0000306 MaybeDead->eraseFromParent();
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000307 Changed = true;
308 ++NumDeletes;
309 }
Matthias Braune39ff702016-02-26 03:18:50 +0000310
311 removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
312 removeClobberedRegsFromMap(CopyMap, *RegMask);
313 for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
314 I != E; I = Next) {
315 Next = std::next(I);
316 if (RegMask->clobbersPhysReg(I->first)) {
317 removeRegsFromMap(AvailCopyMap, I->second, *TRI);
318 SrcMap.erase(I);
319 }
320 }
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000321 }
322
Matthias Braune39ff702016-02-26 03:18:50 +0000323 // Any previous copy definition or reading the Defs is no longer available.
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000324 for (unsigned Reg : Defs)
Matthias Braune39ff702016-02-26 03:18:50 +0000325 ClobberRegister(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000326 }
327
328 // If MBB doesn't have successors, delete the copies whose defs are not used.
329 // If MBB does have successors, then conservative assume the defs are live-out
330 // since we don't want to trust live-in lists.
331 if (MBB.succ_empty()) {
Matthias Braun273575d2016-02-20 03:56:36 +0000332 for (MachineInstr *MaybeDead : MaybeDeadCopies) {
333 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
334 MaybeDead->eraseFromParent();
335 Changed = true;
336 ++NumDeletes;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000337 }
338 }
339
Matthias Braunbd18d752016-02-20 03:56:39 +0000340 MaybeDeadCopies.clear();
341 AvailCopyMap.clear();
342 CopyMap.clear();
343 SrcMap.clear();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000344}
345
346bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000347 if (skipOptnoneFunction(*MF.getFunction()))
348 return false;
349
Matthias Braunbd18d752016-02-20 03:56:39 +0000350 Changed = false;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000351
Eric Christopherfc6de422014-08-05 02:39:49 +0000352 TRI = MF.getSubtarget().getRegisterInfo();
353 TII = MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000354 MRI = &MF.getRegInfo();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000355
Matthias Braun273575d2016-02-20 03:56:36 +0000356 for (MachineBasicBlock &MBB : MF)
Matthias Braunbd18d752016-02-20 03:56:39 +0000357 CopyPropagateBlock(MBB);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000358
359 return Changed;
360}
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000361