blob: 3fdf16fb630d6633eb2dd62249bf4877251ea04f [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
Derek Schuffad154c82016-03-28 17:05:30 +000052 MachineFunctionProperties getRequiredProperties() const override {
53 return MachineFunctionProperties().set(
54 MachineFunctionProperties::Property::AllVRegsAllocated);
55 }
56
Evan Cheng00b1a3c2012-01-07 03:02:36 +000057 private:
Matthias Braune39ff702016-02-26 03:18:50 +000058 void ClobberRegister(unsigned Reg);
Matthias Braunbd18d752016-02-20 03:56:39 +000059 void CopyPropagateBlock(MachineBasicBlock &MBB);
Matthias Braun9dcd65f2016-02-26 03:18:55 +000060 bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
Matthias Braunbd18d752016-02-20 03:56:39 +000061
62 /// Candidates for deletion.
63 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
64 /// Def -> available copies map.
Matthias Braunc65e9042016-02-20 03:56:41 +000065 Reg2MIMap AvailCopyMap;
Matthias Braunbd18d752016-02-20 03:56:39 +000066 /// Def -> copies map.
Matthias Braunc65e9042016-02-20 03:56:41 +000067 Reg2MIMap CopyMap;
Matthias Braunbd18d752016-02-20 03:56:39 +000068 /// Src -> Def map
69 SourceMap SrcMap;
70 bool Changed;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000071 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000072}
Evan Cheng00b1a3c2012-01-07 03:02:36 +000073char MachineCopyPropagation::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +000074char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
Evan Cheng00b1a3c2012-01-07 03:02:36 +000075
76INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
77 "Machine Copy Propagation Pass", false, false)
78
Matthias Braune39ff702016-02-26 03:18:50 +000079/// Remove any entry in \p Map where the register is a subregister or equal to
80/// a register contained in \p Regs.
81static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
82 const TargetRegisterInfo &TRI) {
83 for (unsigned Reg : Regs) {
84 // Source of copy is no longer available for propagation.
85 for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
86 Map.erase(*SR);
Evan Cheng00b1a3c2012-01-07 03:02:36 +000087 }
88}
89
Matthias Braune39ff702016-02-26 03:18:50 +000090/// Remove any entry in \p Map that is marked clobbered in \p RegMask.
91/// The map will typically have a lot fewer entries than the regmask clobbers,
92/// so this is more efficient than iterating the clobbered registers and calling
93/// ClobberRegister() on them.
94static void removeClobberedRegsFromMap(Reg2MIMap &Map,
95 const MachineOperand &RegMask) {
96 for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
97 I = Next) {
98 Next = std::next(I);
99 unsigned Reg = I->first;
100 if (RegMask.clobbersPhysReg(Reg))
101 Map.erase(I);
Evan Cheng520730f2012-01-08 19:52:28 +0000102 }
Matthias Braune39ff702016-02-26 03:18:50 +0000103}
104
105void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
106 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
107 CopyMap.erase(*AI);
108 AvailCopyMap.erase(*AI);
109
110 SourceMap::iterator SI = SrcMap.find(*AI);
111 if (SI != SrcMap.end()) {
112 removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
113 SrcMap.erase(SI);
114 }
115 }
Evan Cheng520730f2012-01-08 19:52:28 +0000116}
117
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000118/// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
119/// This fact may have been obscured by sub register usage or may not be true at
120/// all even though Src and Def are subregisters of the registers used in
121/// PreviousCopy. e.g.
122/// isNopCopy("ecx = COPY eax", AX, CX) == true
123/// isNopCopy("ecx = COPY eax", AH, CL) == false
124static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
125 unsigned Def, const TargetRegisterInfo *TRI) {
126 unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
127 unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
128 if (Src == PreviousSrc) {
129 assert(Def == PreviousDef);
Evan Cheng63618f92012-02-20 23:28:17 +0000130 return true;
Evan Cheng63618f92012-02-20 23:28:17 +0000131 }
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000132 if (!TRI->isSubRegister(PreviousSrc, Src))
133 return false;
134 unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
135 return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
136}
Evan Cheng63618f92012-02-20 23:28:17 +0000137
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000138/// Remove instruction \p Copy if there exists a previous copy that copies the
139/// register \p Src to the register \p Def; This may happen indirectly by
140/// copying the super registers.
141bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
142 unsigned Def) {
143 // Avoid eliminating a copy from/to a reserved registers as we cannot predict
144 // the value (Example: The sparc zero register is writable but stays zero).
145 if (MRI->isReserved(Src) || MRI->isReserved(Def))
146 return false;
147
148 // Search for an existing copy.
149 Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
150 if (CI == AvailCopyMap.end())
151 return false;
152
153 // Check that the existing copy uses the correct sub registers.
154 MachineInstr &PrevCopy = *CI->second;
155 if (!isNopCopy(PrevCopy, Src, Def, TRI))
156 return false;
157
158 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
159
160 // Copy was redundantly redefining either Src or Def. Remove earlier kill
161 // flags between Copy and PrevCopy because the value will be reused now.
162 assert(Copy.isCopy());
163 unsigned CopyDef = Copy.getOperand(0).getReg();
164 assert(CopyDef == Src || CopyDef == Def);
165 for (MachineInstr &MI :
166 make_range(PrevCopy.getIterator(), Copy.getIterator()))
167 MI.clearRegisterKills(CopyDef, TRI);
168
169 Copy.eraseFromParent();
170 Changed = true;
171 ++NumDeletes;
172 return true;
Evan Cheng63618f92012-02-20 23:28:17 +0000173}
174
Matthias Braunbd18d752016-02-20 03:56:39 +0000175void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
James Molloyd787d3e2014-01-22 09:12:27 +0000176 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
177
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000178 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
179 MachineInstr *MI = &*I;
180 ++I;
181
182 if (MI->isCopy()) {
183 unsigned Def = MI->getOperand(0).getReg();
184 unsigned Src = MI->getOperand(1).getReg();
185
Matthias Braun57b5f112016-02-20 03:56:33 +0000186 assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
187 !TargetRegisterInfo::isVirtualRegister(Src) &&
188 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000189
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000190 // The two copies cancel out and the source of the first copy
191 // hasn't been overridden, eliminate the second one. e.g.
192 // %ECX<def> = COPY %EAX
193 // ... nothing clobbered EAX.
194 // %EAX<def> = COPY %ECX
195 // =>
196 // %ECX<def> = COPY %EAX
197 //
198 // or
199 //
200 // %ECX<def> = COPY %EAX
201 // ... nothing clobbered EAX.
202 // %ECX<def> = COPY %EAX
203 // =>
204 // %ECX<def> = COPY %EAX
205 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
206 continue;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000207
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000208 // If Src is defined by a previous copy, the previous copy cannot be
209 // eliminated.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000210 for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
Matthias Braunc65e9042016-02-20 03:56:41 +0000211 Reg2MIMap::iterator CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000212 if (CI != CopyMap.end()) {
213 DEBUG(dbgs() << "MCP: Copy is no longer dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000214 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000215 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000216 }
217
James Molloyd787d3e2014-01-22 09:12:27 +0000218 DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
219
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000220 // Copy is now a candidate for deletion.
Matthias Braun273575d2016-02-20 03:56:36 +0000221 if (!MRI->isReserved(Def))
222 MaybeDeadCopies.insert(MI);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000223
Jun Bum Lim59df5e82016-02-03 15:56:27 +0000224 // If 'Def' is previously source of another copy, then this earlier copy's
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000225 // source is no longer available. e.g.
226 // %xmm9<def> = copy %xmm2
227 // ...
228 // %xmm2<def> = copy %xmm0
229 // ...
230 // %xmm2<def> = copy %xmm9
Matthias Braune39ff702016-02-26 03:18:50 +0000231 ClobberRegister(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000232
233 // Remember Def is defined by the copy.
Chad Rosierabdb1d62013-05-22 23:17:36 +0000234 for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
235 ++SR) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000236 CopyMap[*SR] = MI;
237 AvailCopyMap[*SR] = MI;
238 }
239
240 // Remember source that's copied to Def. Once it's clobbered, then
241 // it's no longer available for copy propagation.
Matthias Braune39ff702016-02-26 03:18:50 +0000242 RegList &DestList = SrcMap[Src];
Matthias Braun273575d2016-02-20 03:56:36 +0000243 if (std::find(DestList.begin(), DestList.end(), Def) == DestList.end())
244 DestList.push_back(Def);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000245
246 continue;
247 }
248
249 // Not a copy.
250 SmallVector<unsigned, 2> Defs;
Matthias Braun273575d2016-02-20 03:56:36 +0000251 const MachineOperand *RegMask = nullptr;
252 for (const MachineOperand &MO : MI->operands()) {
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000253 if (MO.isRegMask())
Matthias Braun273575d2016-02-20 03:56:36 +0000254 RegMask = &MO;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000255 if (!MO.isReg())
256 continue;
257 unsigned Reg = MO.getReg();
258 if (!Reg)
259 continue;
260
Matthias Braun57b5f112016-02-20 03:56:33 +0000261 assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
262 "MachineCopyPropagation should be run after register allocation!");
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000263
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000264 if (MO.isDef()) {
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000265 Defs.push_back(Reg);
266 continue;
267 }
268
269 // If 'Reg' is defined by a copy, the copy is no longer a candidate
270 // for elimination.
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +0000271 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Matthias Braunc65e9042016-02-20 03:56:41 +0000272 Reg2MIMap::iterator CI = CopyMap.find(*AI);
James Molloyd787d3e2014-01-22 09:12:27 +0000273 if (CI != CopyMap.end()) {
274 DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000275 MaybeDeadCopies.remove(CI->second);
James Molloyd787d3e2014-01-22 09:12:27 +0000276 }
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000277 }
Quentin Colombet75afbfd2015-05-28 22:38:40 +0000278 // Treat undef use like defs for copy propagation but not for
279 // dead copy. We would need to do a liveness check to be sure the copy
280 // is dead for undef uses.
281 // The backends are allowed to do whatever they want with undef value
282 // and we cannot be sure this register will not be rewritten to break
283 // some false dependencies for the hardware for instance.
284 if (MO.isUndef())
285 Defs.push_back(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000286 }
287
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000288 // The instruction has a register mask operand which means that it clobbers
Matthias Braune39ff702016-02-26 03:18:50 +0000289 // a large set of registers. Treat clobbered registers the same way as
290 // defined registers.
Matthias Braun273575d2016-02-20 03:56:36 +0000291 if (RegMask) {
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000292 // Erase any MaybeDeadCopies whose destination register is clobbered.
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000293 for (SmallSetVector<MachineInstr *, 8>::iterator DI =
294 MaybeDeadCopies.begin();
295 DI != MaybeDeadCopies.end();) {
296 MachineInstr *MaybeDead = *DI;
Matthias Braun273575d2016-02-20 03:56:36 +0000297 unsigned Reg = MaybeDead->getOperand(0).getReg();
298 assert(!MRI->isReserved(Reg));
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000299
300 if (!RegMask->clobbersPhysReg(Reg)) {
301 ++DI;
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000302 continue;
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000303 }
304
James Molloyd787d3e2014-01-22 09:12:27 +0000305 DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
Matthias Braun273575d2016-02-20 03:56:36 +0000306 MaybeDead->dump());
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000307
308 // erase() will return the next valid iterator pointing to the next
309 // element after the erased one.
310 DI = MaybeDeadCopies.erase(DI);
Matthias Braun273575d2016-02-20 03:56:36 +0000311 MaybeDead->eraseFromParent();
Jakob Stoklund Olesen938b4d22012-02-09 00:19:08 +0000312 Changed = true;
313 ++NumDeletes;
314 }
Matthias Braune39ff702016-02-26 03:18:50 +0000315
316 removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
317 removeClobberedRegsFromMap(CopyMap, *RegMask);
318 for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
319 I != E; I = Next) {
320 Next = std::next(I);
321 if (RegMask->clobbersPhysReg(I->first)) {
322 removeRegsFromMap(AvailCopyMap, I->second, *TRI);
323 SrcMap.erase(I);
324 }
325 }
Jakob Stoklund Olesen8610a592012-02-08 22:37:35 +0000326 }
327
Matthias Braune39ff702016-02-26 03:18:50 +0000328 // Any previous copy definition or reading the Defs is no longer available.
Matthias Braun9dcd65f2016-02-26 03:18:55 +0000329 for (unsigned Reg : Defs)
Matthias Braune39ff702016-02-26 03:18:50 +0000330 ClobberRegister(Reg);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000331 }
332
333 // If MBB doesn't have successors, delete the copies whose defs are not used.
334 // If MBB does have successors, then conservative assume the defs are live-out
335 // since we don't want to trust live-in lists.
336 if (MBB.succ_empty()) {
Matthias Braun273575d2016-02-20 03:56:36 +0000337 for (MachineInstr *MaybeDead : MaybeDeadCopies) {
338 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
339 MaybeDead->eraseFromParent();
340 Changed = true;
341 ++NumDeletes;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000342 }
343 }
344
Matthias Braunbd18d752016-02-20 03:56:39 +0000345 MaybeDeadCopies.clear();
346 AvailCopyMap.clear();
347 CopyMap.clear();
348 SrcMap.clear();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000349}
350
351bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
Paul Robinson7c99ec52014-03-31 17:43:35 +0000352 if (skipOptnoneFunction(*MF.getFunction()))
353 return false;
354
Matthias Braunbd18d752016-02-20 03:56:39 +0000355 Changed = false;
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000356
Eric Christopherfc6de422014-08-05 02:39:49 +0000357 TRI = MF.getSubtarget().getRegisterInfo();
358 TII = MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesenc30a9af2012-10-15 21:57:41 +0000359 MRI = &MF.getRegInfo();
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000360
Matthias Braun273575d2016-02-20 03:56:36 +0000361 for (MachineBasicBlock &MBB : MF)
Matthias Braunbd18d752016-02-20 03:56:39 +0000362 CopyPropagateBlock(MBB);
Evan Cheng00b1a3c2012-01-07 03:02:36 +0000363
364 return Changed;
365}
Jun Bum Lim36c53fe2016-03-25 21:15:35 +0000366