comparison lib/CodeGen/MachineCopyPropagation.cpp @ 134:3a76565eade5 LLVM5.0.1

update 5.0.1
author mir3636
date Sat, 17 Feb 2018 09:57:20 +0900
parents 803732b1fca8
children c2174574ed3a
comparison
equal deleted inserted replaced
133:c60214abe0e8 134:3a76565eade5
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This is an extremely simple MachineInstr-level copy propagation pass. 10 // This is an extremely simple MachineInstr-level copy propagation pass.
11 //
12 // This pass forwards the source of COPYs to the users of their destinations
13 // when doing so is legal. For example:
14 //
15 // %reg1 = COPY %reg0
16 // ...
17 // ... = OP %reg1
18 //
19 // If
20 // - %reg0 has not been clobbered by the time of the use of %reg1
21 // - the register class constraints are satisfied
22 // - the COPY def is the only value that reaches OP
23 // then this pass replaces the above with:
24 //
25 // %reg1 = COPY %reg0
26 // ...
27 // ... = OP %reg0
28 //
29 // This pass also removes some redundant COPYs. For example:
30 //
31 // %R1 = COPY %R0
32 // ... // No clobber of %R1
33 // %R0 = COPY %R1 <<< Removed
34 //
35 // or
36 //
37 // %R1 = COPY %R0
38 // ... // No clobber of %R0
39 // %R1 = COPY %R0 <<< Removed
11 // 40 //
12 //===----------------------------------------------------------------------===// 41 //===----------------------------------------------------------------------===//
13 42
14 #include "llvm/ADT/DenseMap.h" 43 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/STLExtras.h" 44 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/CodeGen/MachineFunction.h" 50 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h" 51 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h" 52 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineOperand.h" 53 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h" 54 #include "llvm/CodeGen/MachineRegisterInfo.h"
55 #include "llvm/CodeGen/TargetInstrInfo.h"
56 #include "llvm/CodeGen/TargetRegisterInfo.h"
57 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/MC/MCRegisterInfo.h" 58 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/Pass.h" 59 #include "llvm/Pass.h"
28 #include "llvm/Support/Debug.h" 60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/DebugCounter.h"
29 #include "llvm/Support/raw_ostream.h" 62 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetRegisterInfo.h"
32 #include "llvm/Target/TargetSubtargetInfo.h"
33 #include <cassert> 63 #include <cassert>
34 #include <iterator> 64 #include <iterator>
35 65
36 using namespace llvm; 66 using namespace llvm;
37 67
38 #define DEBUG_TYPE "machine-cp" 68 #define DEBUG_TYPE "machine-cp"
39 69
40 STATISTIC(NumDeletes, "Number of dead copies deleted"); 70 STATISTIC(NumDeletes, "Number of dead copies deleted");
71 STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
72 DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
73 "Controls which register COPYs are forwarded");
41 74
42 namespace { 75 namespace {
43 76
44 using RegList = SmallVector<unsigned, 4>; 77 using RegList = SmallVector<unsigned, 4>;
45 using SourceMap = DenseMap<unsigned, RegList>; 78 using SourceMap = DenseMap<unsigned, RegList>;
72 private: 105 private:
73 void ClobberRegister(unsigned Reg); 106 void ClobberRegister(unsigned Reg);
74 void ReadRegister(unsigned Reg); 107 void ReadRegister(unsigned Reg);
75 void CopyPropagateBlock(MachineBasicBlock &MBB); 108 void CopyPropagateBlock(MachineBasicBlock &MBB);
76 bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def); 109 bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
110 void forwardUses(MachineInstr &MI);
111 bool isForwardableRegClassCopy(const MachineInstr &Copy,
112 const MachineInstr &UseI, unsigned UseIdx);
113 bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
77 114
78 /// Candidates for deletion. 115 /// Candidates for deletion.
79 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies; 116 SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
80 117
81 /// Def -> available copies map. 118 /// Def -> available copies map.
185 if (CI == AvailCopyMap.end()) 222 if (CI == AvailCopyMap.end())
186 return false; 223 return false;
187 224
188 // Check that the existing copy uses the correct sub registers. 225 // Check that the existing copy uses the correct sub registers.
189 MachineInstr &PrevCopy = *CI->second; 226 MachineInstr &PrevCopy = *CI->second;
227 if (PrevCopy.getOperand(0).isDead())
228 return false;
190 if (!isNopCopy(PrevCopy, Src, Def, TRI)) 229 if (!isNopCopy(PrevCopy, Src, Def, TRI))
191 return false; 230 return false;
192 231
193 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump()); 232 DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
194 233
205 Changed = true; 244 Changed = true;
206 ++NumDeletes; 245 ++NumDeletes;
207 return true; 246 return true;
208 } 247 }
209 248
249 /// Decide whether we should forward the source of \param Copy to its use in
250 /// \param UseI based on the physical register class constraints of the opcode
251 /// and avoiding introducing more cross-class COPYs.
252 bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
253 const MachineInstr &UseI,
254 unsigned UseIdx) {
255
256 unsigned CopySrcReg = Copy.getOperand(1).getReg();
257
258 // If the new register meets the opcode register constraints, then allow
259 // forwarding.
260 if (const TargetRegisterClass *URC =
261 UseI.getRegClassConstraint(UseIdx, TII, TRI))
262 return URC->contains(CopySrcReg);
263
264 if (!UseI.isCopy())
265 return false;
266
267 /// COPYs don't have register class constraints, so if the user instruction
268 /// is a COPY, we just try to avoid introducing additional cross-class
269 /// COPYs. For example:
270 ///
271 /// RegClassA = COPY RegClassB // Copy parameter
272 /// ...
273 /// RegClassB = COPY RegClassA // UseI parameter
274 ///
275 /// which after forwarding becomes
276 ///
277 /// RegClassA = COPY RegClassB
278 /// ...
279 /// RegClassB = COPY RegClassB
280 ///
281 /// so we have reduced the number of cross-class COPYs and potentially
282 /// introduced a nop COPY that can be removed.
283 const TargetRegisterClass *UseDstRC =
284 TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
285
286 const TargetRegisterClass *SuperRC = UseDstRC;
287 for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
288 SuperRC; SuperRC = *SuperRCI++)
289 if (SuperRC->contains(CopySrcReg))
290 return true;
291
292 return false;
293 }
294
295 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
296 /// operand (the register being replaced), since these can sometimes be
297 /// implicitly tied to other operands. For example, on AMDGPU:
298 ///
299 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
300 ///
301 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
302 /// way of knowing we need to update the latter when updating the former.
303 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
304 const MachineOperand &Use) {
305 for (const MachineOperand &MIUse : MI.uses())
306 if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
307 MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
308 return true;
309
310 return false;
311 }
312
313 /// Look for available copies whose destination register is used by \p MI and
314 /// replace the use in \p MI with the copy's source register.
315 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
316 if (AvailCopyMap.empty())
317 return;
318
319 // Look for non-tied explicit vreg uses that have an active COPY
320 // instruction that defines the physical register allocated to them.
321 // Replace the vreg with the source of the active COPY.
322 for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
323 ++OpIdx) {
324 MachineOperand &MOUse = MI.getOperand(OpIdx);
325 // Don't forward into undef use operands since doing so can cause problems
326 // with the machine verifier, since it doesn't treat undef reads as reads,
327 // so we can end up with a live range that ends on an undef read, leading to
328 // an error that the live range doesn't end on a read of the live range
329 // register.
330 if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
331 MOUse.isImplicit())
332 continue;
333
334 if (!MOUse.getReg())
335 continue;
336
337 // Check that the register is marked 'renamable' so we know it is safe to
338 // rename it without violating any constraints that aren't expressed in the
339 // IR (e.g. ABI or opcode requirements).
340 if (!MOUse.isRenamable())
341 continue;
342
343 auto CI = AvailCopyMap.find(MOUse.getReg());
344 if (CI == AvailCopyMap.end())
345 continue;
346
347 MachineInstr &Copy = *CI->second;
348 unsigned CopyDstReg = Copy.getOperand(0).getReg();
349 const MachineOperand &CopySrc = Copy.getOperand(1);
350 unsigned CopySrcReg = CopySrc.getReg();
351
352 // FIXME: Don't handle partial uses of wider COPYs yet.
353 if (MOUse.getReg() != CopyDstReg) {
354 DEBUG(dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n "
355 << MI);
356 continue;
357 }
358
359 // Don't forward COPYs of reserved regs unless they are constant.
360 if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
361 continue;
362
363 if (!isForwardableRegClassCopy(Copy, MI, OpIdx))
364 continue;
365
366 if (hasImplicitOverlap(MI, MOUse))
367 continue;
368
369 if (!DebugCounter::shouldExecute(FwdCounter)) {
370 DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n "
371 << MI);
372 continue;
373 }
374
375 DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
376 << "\n with " << printReg(CopySrcReg, TRI) << "\n in "
377 << MI << " from " << Copy);
378
379 MOUse.setReg(CopySrcReg);
380 if (!CopySrc.isRenamable())
381 MOUse.setIsRenamable(false);
382
383 DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
384
385 // Clear kill markers that may have been invalidated.
386 for (MachineInstr &KMI :
387 make_range(Copy.getIterator(), std::next(MI.getIterator())))
388 KMI.clearRegisterKills(CopySrcReg, TRI);
389
390 ++NumCopyForwards;
391 Changed = true;
392 }
393 }
394
210 void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) { 395 void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
211 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n"); 396 DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
212 397
213 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) { 398 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
214 MachineInstr *MI = &*I; 399 MachineInstr *MI = &*I;
222 !TargetRegisterInfo::isVirtualRegister(Src) && 407 !TargetRegisterInfo::isVirtualRegister(Src) &&
223 "MachineCopyPropagation should be run after register allocation!"); 408 "MachineCopyPropagation should be run after register allocation!");
224 409
225 // The two copies cancel out and the source of the first copy 410 // The two copies cancel out and the source of the first copy
226 // hasn't been overridden, eliminate the second one. e.g. 411 // hasn't been overridden, eliminate the second one. e.g.
227 // %ECX<def> = COPY %EAX 412 // %ecx = COPY %eax
228 // ... nothing clobbered EAX. 413 // ... nothing clobbered eax.
229 // %EAX<def> = COPY %ECX 414 // %eax = COPY %ecx
230 // => 415 // =>
231 // %ECX<def> = COPY %EAX 416 // %ecx = COPY %eax
232 // 417 //
233 // or 418 // or
234 // 419 //
235 // %ECX<def> = COPY %EAX 420 // %ecx = COPY %eax
236 // ... nothing clobbered EAX. 421 // ... nothing clobbered eax.
237 // %ECX<def> = COPY %EAX 422 // %ecx = COPY %eax
238 // => 423 // =>
239 // %ECX<def> = COPY %EAX 424 // %ecx = COPY %eax
240 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def)) 425 if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
241 continue; 426 continue;
427
428 forwardUses(*MI);
429
430 // Src may have been changed by forwardUses()
431 Src = MI->getOperand(1).getReg();
242 432
243 // If Src is defined by a previous copy, the previous copy cannot be 433 // If Src is defined by a previous copy, the previous copy cannot be
244 // eliminated. 434 // eliminated.
245 ReadRegister(Src); 435 ReadRegister(Src);
246 for (const MachineOperand &MO : MI->implicit_operands()) { 436 for (const MachineOperand &MO : MI->implicit_operands()) {
258 if (!MRI->isReserved(Def)) 448 if (!MRI->isReserved(Def))
259 MaybeDeadCopies.insert(MI); 449 MaybeDeadCopies.insert(MI);
260 450
261 // If 'Def' is previously source of another copy, then this earlier copy's 451 // If 'Def' is previously source of another copy, then this earlier copy's
262 // source is no longer available. e.g. 452 // source is no longer available. e.g.
263 // %xmm9<def> = copy %xmm2 453 // %xmm9 = copy %xmm2
264 // ... 454 // ...
265 // %xmm2<def> = copy %xmm0 455 // %xmm2 = copy %xmm0
266 // ... 456 // ...
267 // %xmm2<def> = copy %xmm9 457 // %xmm2 = copy %xmm9
268 ClobberRegister(Def); 458 ClobberRegister(Def);
269 for (const MachineOperand &MO : MI->implicit_operands()) { 459 for (const MachineOperand &MO : MI->implicit_operands()) {
270 if (!MO.isReg() || !MO.isDef()) 460 if (!MO.isReg() || !MO.isDef())
271 continue; 461 continue;
272 unsigned Reg = MO.getReg(); 462 unsigned Reg = MO.getReg();
288 if (!is_contained(DestList, Def)) 478 if (!is_contained(DestList, Def))
289 DestList.push_back(Def); 479 DestList.push_back(Def);
290 480
291 continue; 481 continue;
292 } 482 }
483
484 // Clobber any earlyclobber regs first.
485 for (const MachineOperand &MO : MI->operands())
486 if (MO.isReg() && MO.isEarlyClobber()) {
487 unsigned Reg = MO.getReg();
488 // If we have a tied earlyclobber, that means it is also read by this
489 // instruction, so we need to make sure we don't remove it as dead
490 // later.
491 if (MO.isTied())
492 ReadRegister(Reg);
493 ClobberRegister(Reg);
494 }
495
496 forwardUses(*MI);
293 497
294 // Not a copy. 498 // Not a copy.
295 SmallVector<unsigned, 2> Defs; 499 SmallVector<unsigned, 2> Defs;
296 const MachineOperand *RegMask = nullptr; 500 const MachineOperand *RegMask = nullptr;
297 for (const MachineOperand &MO : MI->operands()) { 501 for (const MachineOperand &MO : MI->operands()) {
304 continue; 508 continue;
305 509
306 assert(!TargetRegisterInfo::isVirtualRegister(Reg) && 510 assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
307 "MachineCopyPropagation should be run after register allocation!"); 511 "MachineCopyPropagation should be run after register allocation!");
308 512
309 if (MO.isDef()) { 513 if (MO.isDef() && !MO.isEarlyClobber()) {
310 Defs.push_back(Reg); 514 Defs.push_back(Reg);
311 continue; 515 continue;
312 } else if (MO.readsReg()) 516 } else if (MO.readsReg())
313 ReadRegister(Reg); 517 ReadRegister(Reg);
314 } 518 }
361 // If MBB doesn't have successors, delete the copies whose defs are not used. 565 // If MBB doesn't have successors, delete the copies whose defs are not used.
362 // If MBB does have successors, then conservative assume the defs are live-out 566 // If MBB does have successors, then conservative assume the defs are live-out
363 // since we don't want to trust live-in lists. 567 // since we don't want to trust live-in lists.
364 if (MBB.succ_empty()) { 568 if (MBB.succ_empty()) {
365 for (MachineInstr *MaybeDead : MaybeDeadCopies) { 569 for (MachineInstr *MaybeDead : MaybeDeadCopies) {
570 DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
571 MaybeDead->dump());
366 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg())); 572 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
367 MaybeDead->eraseFromParent(); 573 MaybeDead->eraseFromParent();
368 Changed = true; 574 Changed = true;
369 ++NumDeletes; 575 ++NumDeletes;
370 } 576 }
375 CopyMap.clear(); 581 CopyMap.clear();
376 SrcMap.clear(); 582 SrcMap.clear();
377 } 583 }
378 584
379 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) { 585 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
380 if (skipFunction(*MF.getFunction())) 586 if (skipFunction(MF.getFunction()))
381 return false; 587 return false;
382 588
383 Changed = false; 589 Changed = false;
384 590
385 TRI = MF.getSubtarget().getRegisterInfo(); 591 TRI = MF.getSubtarget().getRegisterInfo();