LLVM  10.0.0
MachineCopyPropagation.cpp
Go to the documentation of this file.
1 //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is an extremely simple MachineInstr-level copy propagation pass.
10 //
11 // This pass forwards the source of COPYs to the users of their destinations
12 // when doing so is legal. For example:
13 //
14 // %reg1 = COPY %reg0
15 // ...
16 // ... = OP %reg1
17 //
18 // If
19 // - %reg0 has not been clobbered by the time of the use of %reg1
20 // - the register class constraints are satisfied
21 // - the COPY def is the only value that reaches OP
22 // then this pass replaces the above with:
23 //
24 // %reg1 = COPY %reg0
25 // ...
26 // ... = OP %reg0
27 //
28 // This pass also removes some redundant COPYs. For example:
29 //
30 // %R1 = COPY %R0
31 // ... // No clobber of %R1
32 // %R0 = COPY %R1 <<< Removed
33 //
34 // or
35 //
36 // %R1 = COPY %R0
37 // ... // No clobber of %R0
38 // %R1 = COPY %R0 <<< Removed
39 //
40 // or
41 //
42 // $R0 = OP ...
43 // ... // No read/clobber of $R0 and $R1
44 // $R1 = COPY $R0 // $R0 is killed
45 // Replace $R0 with $R1 and remove the COPY
46 // $R1 = OP ...
47 // ...
48 //
49 //===----------------------------------------------------------------------===//
50 
51 #include "llvm/ADT/DenseMap.h"
52 #include "llvm/ADT/STLExtras.h"
53 #include "llvm/ADT/SetVector.h"
54 #include "llvm/ADT/SmallVector.h"
55 #include "llvm/ADT/Statistic.h"
66 #include "llvm/InitializePasses.h"
67 #include "llvm/MC/MCRegisterInfo.h"
68 #include "llvm/Pass.h"
69 #include "llvm/Support/Debug.h"
72 #include <cassert>
73 #include <iterator>
74 
75 using namespace llvm;
76 
77 #define DEBUG_TYPE "machine-cp"
78 
79 STATISTIC(NumDeletes, "Number of dead copies deleted");
80 STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
81 STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated");
82 DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
83  "Controls which register COPYs are forwarded");
84 
85 namespace {
86 
87 class CopyTracker {
88  struct CopyInfo {
91  bool Avail;
92  };
93 
95 
96 public:
97  /// Mark all of the given registers and their subregisters as unavailable for
98  /// copying.
99  void markRegsUnavailable(ArrayRef<unsigned> Regs,
100  const TargetRegisterInfo &TRI) {
101  for (unsigned Reg : Regs) {
102  // Source of copy is no longer available for propagation.
103  for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
104  auto CI = Copies.find(*RUI);
105  if (CI != Copies.end())
106  CI->second.Avail = false;
107  }
108  }
109  }
110 
111  /// Remove register from copy maps.
112  void invalidateRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
113  // Since Reg might be a subreg of some registers, only invalidate Reg is not
114  // enough. We have to find the COPY defines Reg or registers defined by Reg
115  // and invalidate all of them.
116  DenseSet<unsigned> RegsToInvalidate{Reg};
117  for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
118  auto I = Copies.find(*RUI);
119  if (I != Copies.end()) {
120  if (MachineInstr *MI = I->second.MI) {
121  RegsToInvalidate.insert(MI->getOperand(0).getReg());
122  RegsToInvalidate.insert(MI->getOperand(1).getReg());
123  }
124  RegsToInvalidate.insert(I->second.DefRegs.begin(),
125  I->second.DefRegs.end());
126  }
127  }
128  for (unsigned InvalidReg : RegsToInvalidate)
129  for (MCRegUnitIterator RUI(InvalidReg, &TRI); RUI.isValid(); ++RUI)
130  Copies.erase(*RUI);
131  }
132 
133  /// Clobber a single register, removing it from the tracker's copy maps.
134  void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) {
135  for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
136  auto I = Copies.find(*RUI);
137  if (I != Copies.end()) {
138  // When we clobber the source of a copy, we need to clobber everything
139  // it defined.
140  markRegsUnavailable(I->second.DefRegs, TRI);
141  // When we clobber the destination of a copy, we need to clobber the
142  // whole register it defined.
143  if (MachineInstr *MI = I->second.MI)
144  markRegsUnavailable({MI->getOperand(0).getReg()}, TRI);
145  // Now we can erase the copy.
146  Copies.erase(I);
147  }
148  }
149  }
150 
151  /// Add this copy's registers into the tracker's copy maps.
152  void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) {
153  assert(MI->isCopy() && "Tracking non-copy?");
154 
155  Register Def = MI->getOperand(0).getReg();
156  Register Src = MI->getOperand(1).getReg();
157 
158  // Remember Def is defined by the copy.
159  for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
160  Copies[*RUI] = {MI, {}, true};
161 
162  // Remember source that's copied to Def. Once it's clobbered, then
163  // it's no longer available for copy propagation.
164  for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
165  auto I = Copies.insert({*RUI, {nullptr, {}, false}});
166  auto &Copy = I.first->second;
167  if (!is_contained(Copy.DefRegs, Def))
168  Copy.DefRegs.push_back(Def);
169  }
170  }
171 
172  bool hasAnyCopies() {
173  return !Copies.empty();
174  }
175 
176  MachineInstr *findCopyForUnit(unsigned RegUnit, const TargetRegisterInfo &TRI,
177  bool MustBeAvailable = false) {
178  auto CI = Copies.find(RegUnit);
179  if (CI == Copies.end())
180  return nullptr;
181  if (MustBeAvailable && !CI->second.Avail)
182  return nullptr;
183  return CI->second.MI;
184  }
185 
186  MachineInstr *findCopyDefViaUnit(unsigned RegUnit,
187  const TargetRegisterInfo &TRI) {
188  auto CI = Copies.find(RegUnit);
189  if (CI == Copies.end())
190  return nullptr;
191  if (CI->second.DefRegs.size() != 1)
192  return nullptr;
193  MCRegUnitIterator RUI(CI->second.DefRegs[0], &TRI);
194  return findCopyForUnit(*RUI, TRI, true);
195  }
196 
197  MachineInstr *findAvailBackwardCopy(MachineInstr &I, unsigned Reg,
198  const TargetRegisterInfo &TRI) {
199  MCRegUnitIterator RUI(Reg, &TRI);
200  MachineInstr *AvailCopy = findCopyDefViaUnit(*RUI, TRI);
201  if (!AvailCopy ||
202  !TRI.isSubRegisterEq(AvailCopy->getOperand(1).getReg(), Reg))
203  return nullptr;
204 
205  Register AvailSrc = AvailCopy->getOperand(1).getReg();
206  Register AvailDef = AvailCopy->getOperand(0).getReg();
207  for (const MachineInstr &MI :
209  for (const MachineOperand &MO : MI.operands())
210  if (MO.isRegMask())
211  // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef?
212  if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
213  return nullptr;
214 
215  return AvailCopy;
216  }
217 
218  MachineInstr *findAvailCopy(MachineInstr &DestCopy, unsigned Reg,
219  const TargetRegisterInfo &TRI) {
220  // We check the first RegUnit here, since we'll only be interested in the
221  // copy if it copies the entire register anyway.
222  MCRegUnitIterator RUI(Reg, &TRI);
223  MachineInstr *AvailCopy =
224  findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true);
225  if (!AvailCopy ||
226  !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg))
227  return nullptr;
228 
229  // Check that the available copy isn't clobbered by any regmasks between
230  // itself and the destination.
231  Register AvailSrc = AvailCopy->getOperand(1).getReg();
232  Register AvailDef = AvailCopy->getOperand(0).getReg();
233  for (const MachineInstr &MI :
234  make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
235  for (const MachineOperand &MO : MI.operands())
236  if (MO.isRegMask())
237  if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
238  return nullptr;
239 
240  return AvailCopy;
241  }
242 
243  void clear() {
244  Copies.clear();
245  }
246 };
247 
248 class MachineCopyPropagation : public MachineFunctionPass {
249  const TargetRegisterInfo *TRI;
250  const TargetInstrInfo *TII;
251  const MachineRegisterInfo *MRI;
252 
253 public:
254  static char ID; // Pass identification, replacement for typeid
255 
256  MachineCopyPropagation() : MachineFunctionPass(ID) {
258  }
259 
260  void getAnalysisUsage(AnalysisUsage &AU) const override {
261  AU.setPreservesCFG();
263  }
264 
265  bool runOnMachineFunction(MachineFunction &MF) override;
266 
267  MachineFunctionProperties getRequiredProperties() const override {
270  }
271 
272 private:
273  typedef enum { DebugUse = false, RegularUse = true } DebugType;
274 
275  void ClobberRegister(unsigned Reg);
276  void ReadRegister(unsigned Reg, MachineInstr &Reader,
277  DebugType DT);
278  void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
279  void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
280  bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
281  void forwardUses(MachineInstr &MI);
282  void propagateDefs(MachineInstr &MI);
283  bool isForwardableRegClassCopy(const MachineInstr &Copy,
284  const MachineInstr &UseI, unsigned UseIdx);
285  bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
286  const MachineInstr &UseI,
287  unsigned UseIdx);
288  bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
289 
290  /// Candidates for deletion.
291  SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
292 
293  /// Multimap tracking debug users in current BB
295 
296  CopyTracker Tracker;
297 
298  bool Changed;
299 };
300 
301 } // end anonymous namespace
302 
304 
306 
307 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
308  "Machine Copy Propagation Pass", false, false)
309 
310 void MachineCopyPropagation::ReadRegister(unsigned Reg, MachineInstr &Reader,
311  DebugType DT) {
312  // If 'Reg' is defined by a copy, the copy is no longer a candidate
313  // for elimination. If a copy is "read" by a debug user, record the user
314  // for propagation.
315  for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
316  if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
317  if (DT == RegularUse) {
318  LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
319  MaybeDeadCopies.remove(Copy);
320  } else {
321  CopyDbgUsers[Copy].push_back(&Reader);
322  }
323  }
324  }
325 }
326 
327 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
328 /// This fact may have been obscured by sub register usage or may not be true at
329 /// all even though Src and Def are subregisters of the registers used in
330 /// PreviousCopy. e.g.
331 /// isNopCopy("ecx = COPY eax", AX, CX) == true
332 /// isNopCopy("ecx = COPY eax", AH, CL) == false
333 static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
334  unsigned Def, const TargetRegisterInfo *TRI) {
335  Register PreviousSrc = PreviousCopy.getOperand(1).getReg();
336  Register PreviousDef = PreviousCopy.getOperand(0).getReg();
337  if (Src == PreviousSrc) {
338  assert(Def == PreviousDef);
339  return true;
340  }
341  if (!TRI->isSubRegister(PreviousSrc, Src))
342  return false;
343  unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
344  return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
345 }
346 
347 /// Remove instruction \p Copy if there exists a previous copy that copies the
348 /// register \p Src to the register \p Def; This may happen indirectly by
349 /// copying the super registers.
350 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
351  unsigned Def) {
352  // Avoid eliminating a copy from/to a reserved registers as we cannot predict
353  // the value (Example: The sparc zero register is writable but stays zero).
354  if (MRI->isReserved(Src) || MRI->isReserved(Def))
355  return false;
356 
357  // Search for an existing copy.
358  MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
359  if (!PrevCopy)
360  return false;
361 
362  // Check that the existing copy uses the correct sub registers.
363  if (PrevCopy->getOperand(0).isDead())
364  return false;
365  if (!isNopCopy(*PrevCopy, Src, Def, TRI))
366  return false;
367 
368  LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
369 
370  // Copy was redundantly redefining either Src or Def. Remove earlier kill
371  // flags between Copy and PrevCopy because the value will be reused now.
372  assert(Copy.isCopy());
373  Register CopyDef = Copy.getOperand(0).getReg();
374  assert(CopyDef == Src || CopyDef == Def);
375  for (MachineInstr &MI :
376  make_range(PrevCopy->getIterator(), Copy.getIterator()))
377  MI.clearRegisterKills(CopyDef, TRI);
378 
379  Copy.eraseFromParent();
380  Changed = true;
381  ++NumDeletes;
382  return true;
383 }
384 
385 bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
386  const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
387  Register Def = Copy.getOperand(0).getReg();
388 
389  if (const TargetRegisterClass *URC =
390  UseI.getRegClassConstraint(UseIdx, TII, TRI))
391  return URC->contains(Def);
392 
393  // We don't process further if UseI is a COPY, since forward copy propagation
394  // should handle that.
395  return false;
396 }
397 
398 /// Decide whether we should forward the source of \param Copy to its use in
399 /// \param UseI based on the physical register class constraints of the opcode
400 /// and avoiding introducing more cross-class COPYs.
401 bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
402  const MachineInstr &UseI,
403  unsigned UseIdx) {
404 
405  Register CopySrcReg = Copy.getOperand(1).getReg();
406 
407  // If the new register meets the opcode register constraints, then allow
408  // forwarding.
409  if (const TargetRegisterClass *URC =
410  UseI.getRegClassConstraint(UseIdx, TII, TRI))
411  return URC->contains(CopySrcReg);
412 
413  if (!UseI.isCopy())
414  return false;
415 
416  /// COPYs don't have register class constraints, so if the user instruction
417  /// is a COPY, we just try to avoid introducing additional cross-class
418  /// COPYs. For example:
419  ///
420  /// RegClassA = COPY RegClassB // Copy parameter
421  /// ...
422  /// RegClassB = COPY RegClassA // UseI parameter
423  ///
424  /// which after forwarding becomes
425  ///
426  /// RegClassA = COPY RegClassB
427  /// ...
428  /// RegClassB = COPY RegClassB
429  ///
430  /// so we have reduced the number of cross-class COPYs and potentially
431  /// introduced a nop COPY that can be removed.
432  const TargetRegisterClass *UseDstRC =
433  TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
434 
435  const TargetRegisterClass *SuperRC = UseDstRC;
436  for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
437  SuperRC; SuperRC = *SuperRCI++)
438  if (SuperRC->contains(CopySrcReg))
439  return true;
440 
441  return false;
442 }
443 
444 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
445 /// operand (the register being replaced), since these can sometimes be
446 /// implicitly tied to other operands. For example, on AMDGPU:
447 ///
448 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
449 ///
450 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
451 /// way of knowing we need to update the latter when updating the former.
452 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
453  const MachineOperand &Use) {
454  for (const MachineOperand &MIUse : MI.uses())
455  if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
456  MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
457  return true;
458 
459  return false;
460 }
461 
462 /// Look for available copies whose destination register is used by \p MI and
463 /// replace the use in \p MI with the copy's source register.
464 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
465  if (!Tracker.hasAnyCopies())
466  return;
467 
468  // Look for non-tied explicit vreg uses that have an active COPY
469  // instruction that defines the physical register allocated to them.
470  // Replace the vreg with the source of the active COPY.
471  for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
472  ++OpIdx) {
473  MachineOperand &MOUse = MI.getOperand(OpIdx);
474  // Don't forward into undef use operands since doing so can cause problems
475  // with the machine verifier, since it doesn't treat undef reads as reads,
476  // so we can end up with a live range that ends on an undef read, leading to
477  // an error that the live range doesn't end on a read of the live range
478  // register.
479  if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
480  MOUse.isImplicit())
481  continue;
482 
483  if (!MOUse.getReg())
484  continue;
485 
486  // Check that the register is marked 'renamable' so we know it is safe to
487  // rename it without violating any constraints that aren't expressed in the
488  // IR (e.g. ABI or opcode requirements).
489  if (!MOUse.isRenamable())
490  continue;
491 
492  MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg(), *TRI);
493  if (!Copy)
494  continue;
495 
496  Register CopyDstReg = Copy->getOperand(0).getReg();
497  const MachineOperand &CopySrc = Copy->getOperand(1);
498  Register CopySrcReg = CopySrc.getReg();
499 
500  // FIXME: Don't handle partial uses of wider COPYs yet.
501  if (MOUse.getReg() != CopyDstReg) {
502  LLVM_DEBUG(
503  dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n "
504  << MI);
505  continue;
506  }
507 
508  // Don't forward COPYs of reserved regs unless they are constant.
509  if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
510  continue;
511 
512  if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
513  continue;
514 
515  if (hasImplicitOverlap(MI, MOUse))
516  continue;
517 
518  // Check that the instruction is not a copy that partially overwrites the
519  // original copy source that we are about to use. The tracker mechanism
520  // cannot cope with that.
521  if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) &&
522  !MI.definesRegister(CopySrcReg)) {
523  LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
524  continue;
525  }
526 
527  if (!DebugCounter::shouldExecute(FwdCounter)) {
528  LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n "
529  << MI);
530  continue;
531  }
532 
533  LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
534  << "\n with " << printReg(CopySrcReg, TRI)
535  << "\n in " << MI << " from " << *Copy);
536 
537  MOUse.setReg(CopySrcReg);
538  if (!CopySrc.isRenamable())
539  MOUse.setIsRenamable(false);
540 
541  LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
542 
543  // Clear kill markers that may have been invalidated.
544  for (MachineInstr &KMI :
545  make_range(Copy->getIterator(), std::next(MI.getIterator())))
546  KMI.clearRegisterKills(CopySrcReg, TRI);
547 
548  ++NumCopyForwards;
549  Changed = true;
550  }
551 }
552 
553 void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
554  LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
555  << "\n");
556 
557  for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
558  MachineInstr *MI = &*I;
559  ++I;
560 
561  // Analyze copies (which don't overlap themselves).
562  if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
563  MI->getOperand(1).getReg())) {
564  Register Def = MI->getOperand(0).getReg();
565  Register Src = MI->getOperand(1).getReg();
566 
569  "MachineCopyPropagation should be run after register allocation!");
570 
571  // The two copies cancel out and the source of the first copy
572  // hasn't been overridden, eliminate the second one. e.g.
573  // %ecx = COPY %eax
574  // ... nothing clobbered eax.
575  // %eax = COPY %ecx
576  // =>
577  // %ecx = COPY %eax
578  //
579  // or
580  //
581  // %ecx = COPY %eax
582  // ... nothing clobbered eax.
583  // %ecx = COPY %eax
584  // =>
585  // %ecx = COPY %eax
586  if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
587  continue;
588 
589  forwardUses(*MI);
590 
591  // Src may have been changed by forwardUses()
592  Src = MI->getOperand(1).getReg();
593 
594  // If Src is defined by a previous copy, the previous copy cannot be
595  // eliminated.
596  ReadRegister(Src, *MI, RegularUse);
597  for (const MachineOperand &MO : MI->implicit_operands()) {
598  if (!MO.isReg() || !MO.readsReg())
599  continue;
600  Register Reg = MO.getReg();
601  if (!Reg)
602  continue;
603  ReadRegister(Reg, *MI, RegularUse);
604  }
605 
606  LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
607 
608  // Copy is now a candidate for deletion.
609  if (!MRI->isReserved(Def))
610  MaybeDeadCopies.insert(MI);
611 
612  // If 'Def' is previously source of another copy, then this earlier copy's
613  // source is no longer available. e.g.
614  // %xmm9 = copy %xmm2
615  // ...
616  // %xmm2 = copy %xmm0
617  // ...
618  // %xmm2 = copy %xmm9
619  Tracker.clobberRegister(Def, *TRI);
620  for (const MachineOperand &MO : MI->implicit_operands()) {
621  if (!MO.isReg() || !MO.isDef())
622  continue;
623  Register Reg = MO.getReg();
624  if (!Reg)
625  continue;
626  Tracker.clobberRegister(Reg, *TRI);
627  }
628 
629  Tracker.trackCopy(MI, *TRI);
630 
631  continue;
632  }
633 
634  // Clobber any earlyclobber regs first.
635  for (const MachineOperand &MO : MI->operands())
636  if (MO.isReg() && MO.isEarlyClobber()) {
637  Register Reg = MO.getReg();
638  // If we have a tied earlyclobber, that means it is also read by this
639  // instruction, so we need to make sure we don't remove it as dead
640  // later.
641  if (MO.isTied())
642  ReadRegister(Reg, *MI, RegularUse);
643  Tracker.clobberRegister(Reg, *TRI);
644  }
645 
646  forwardUses(*MI);
647 
648  // Not a copy.
650  const MachineOperand *RegMask = nullptr;
651  for (const MachineOperand &MO : MI->operands()) {
652  if (MO.isRegMask())
653  RegMask = &MO;
654  if (!MO.isReg())
655  continue;
656  Register Reg = MO.getReg();
657  if (!Reg)
658  continue;
659 
661  "MachineCopyPropagation should be run after register allocation!");
662 
663  if (MO.isDef() && !MO.isEarlyClobber()) {
664  Defs.push_back(Reg);
665  continue;
666  } else if (MO.readsReg())
667  ReadRegister(Reg, *MI, MO.isDebug() ? DebugUse : RegularUse);
668  }
669 
670  // The instruction has a register mask operand which means that it clobbers
671  // a large set of registers. Treat clobbered registers the same way as
672  // defined registers.
673  if (RegMask) {
674  // Erase any MaybeDeadCopies whose destination register is clobbered.
676  MaybeDeadCopies.begin();
677  DI != MaybeDeadCopies.end();) {
678  MachineInstr *MaybeDead = *DI;
679  Register Reg = MaybeDead->getOperand(0).getReg();
680  assert(!MRI->isReserved(Reg));
681 
682  if (!RegMask->clobbersPhysReg(Reg)) {
683  ++DI;
684  continue;
685  }
686 
687  LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
688  MaybeDead->dump());
689 
690  // Make sure we invalidate any entries in the copy maps before erasing
691  // the instruction.
692  Tracker.clobberRegister(Reg, *TRI);
693 
694  // erase() will return the next valid iterator pointing to the next
695  // element after the erased one.
696  DI = MaybeDeadCopies.erase(DI);
697  MaybeDead->eraseFromParent();
698  Changed = true;
699  ++NumDeletes;
700  }
701  }
702 
703  // Any previous copy definition or reading the Defs is no longer available.
704  for (unsigned Reg : Defs)
705  Tracker.clobberRegister(Reg, *TRI);
706  }
707 
708  // If MBB doesn't have successors, delete the copies whose defs are not used.
709  // If MBB does have successors, then conservative assume the defs are live-out
710  // since we don't want to trust live-in lists.
711  if (MBB.succ_empty()) {
712  for (MachineInstr *MaybeDead : MaybeDeadCopies) {
713  LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
714  MaybeDead->dump());
715  assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
716 
717  // Update matching debug values, if any.
718  assert(MaybeDead->isCopy());
719  unsigned SrcReg = MaybeDead->getOperand(1).getReg();
720  MRI->updateDbgUsersToReg(SrcReg, CopyDbgUsers[MaybeDead]);
721 
722  MaybeDead->eraseFromParent();
723  Changed = true;
724  ++NumDeletes;
725  }
726  }
727 
728  MaybeDeadCopies.clear();
729  CopyDbgUsers.clear();
730  Tracker.clear();
731 }
732 
734  const MachineRegisterInfo &MRI) {
735  assert(MI.isCopy() && "MI is expected to be a COPY");
736  Register Def = MI.getOperand(0).getReg();
737  Register Src = MI.getOperand(1).getReg();
738 
739  if (!Def || !Src)
740  return false;
741 
742  if (MRI.isReserved(Def) || MRI.isReserved(Src))
743  return false;
744 
745  return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill();
746 }
747 
748 void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
749  if (!Tracker.hasAnyCopies())
750  return;
751 
752  for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
753  ++OpIdx) {
754  MachineOperand &MODef = MI.getOperand(OpIdx);
755 
756  if (!MODef.isReg() || MODef.isUse())
757  continue;
758 
759  // Ignore non-trivial cases.
760  if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
761  continue;
762 
763  if (!MODef.getReg())
764  continue;
765 
766  // We only handle if the register comes from a vreg.
767  if (!MODef.isRenamable())
768  continue;
769 
770  MachineInstr *Copy =
771  Tracker.findAvailBackwardCopy(MI, MODef.getReg(), *TRI);
772  if (!Copy)
773  continue;
774 
775  Register Def = Copy->getOperand(0).getReg();
776  Register Src = Copy->getOperand(1).getReg();
777 
778  if (MODef.getReg() != Src)
779  continue;
780 
781  if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
782  continue;
783 
784  if (hasImplicitOverlap(MI, MODef))
785  continue;
786 
787  LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
788  << "\n with " << printReg(Def, TRI) << "\n in "
789  << MI << " from " << *Copy);
790 
791  MODef.setReg(Def);
792  MODef.setIsRenamable(Copy->getOperand(0).isRenamable());
793 
794  LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
795  MaybeDeadCopies.insert(Copy);
796  Changed = true;
797  ++NumCopyBackwardPropagated;
798  }
799 }
800 
801 void MachineCopyPropagation::BackwardCopyPropagateBlock(
802  MachineBasicBlock &MBB) {
803  LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
804  << "\n");
805 
806  for (MachineBasicBlock::reverse_iterator I = MBB.rbegin(), E = MBB.rend();
807  I != E;) {
808  MachineInstr *MI = &*I;
809  ++I;
810 
811  // Ignore non-trivial COPYs.
812  if (MI->isCopy() && MI->getNumOperands() == 2 &&
813  !TRI->regsOverlap(MI->getOperand(0).getReg(),
814  MI->getOperand(1).getReg())) {
815 
816  Register Def = MI->getOperand(0).getReg();
817  Register Src = MI->getOperand(1).getReg();
818 
819  // Unlike forward cp, we don't invoke propagateDefs here,
820  // just let forward cp do COPY-to-COPY propagation.
821  if (isBackwardPropagatableCopy(*MI, *MRI)) {
822  Tracker.invalidateRegister(Src, *TRI);
823  Tracker.invalidateRegister(Def, *TRI);
824  Tracker.trackCopy(MI, *TRI);
825  continue;
826  }
827  }
828 
829  // Invalidate any earlyclobber regs first.
830  for (const MachineOperand &MO : MI->operands())
831  if (MO.isReg() && MO.isEarlyClobber()) {
832  Register Reg = MO.getReg();
833  if (!Reg)
834  continue;
835  Tracker.invalidateRegister(Reg, *TRI);
836  }
837 
838  propagateDefs(*MI);
839  for (const MachineOperand &MO : MI->operands()) {
840  if (!MO.isReg())
841  continue;
842 
843  if (!MO.getReg())
844  continue;
845 
846  if (MO.isDef())
847  Tracker.invalidateRegister(MO.getReg(), *TRI);
848 
849  if (MO.readsReg())
850  Tracker.invalidateRegister(MO.getReg(), *TRI);
851  }
852  }
853 
854  for (auto *Copy : MaybeDeadCopies) {
855  Copy->eraseFromParent();
856  ++NumDeletes;
857  }
858 
859  MaybeDeadCopies.clear();
860  CopyDbgUsers.clear();
861  Tracker.clear();
862 }
863 
864 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
865  if (skipFunction(MF.getFunction()))
866  return false;
867 
868  Changed = false;
869 
870  TRI = MF.getSubtarget().getRegisterInfo();
871  TII = MF.getSubtarget().getInstrInfo();
872  MRI = &MF.getRegInfo();
873 
874  for (MachineBasicBlock &MBB : MF) {
875  BackwardCopyPropagateBlock(MBB);
876  ForwardCopyPropagateBlock(MBB);
877  }
878 
879  return Changed;
880 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool contains(unsigned Reg) const
Return true if the specified register is included in this register class.
bool isReserved(Register PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
Definition: MachineInstr.h:525
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
void push_back(const T &Elt)
Definition: SmallVector.h:211
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
unsigned Reg
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:492
void setIsRenamable(bool Val=true)
INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, "Machine Copy Propagation Pass", false, false) void MachineCopyPropagation
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:195
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
const TargetRegisterClass * getRegClassConstraint(unsigned OpIdx, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
Compute the static register class constraint for operand OpIdx.
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:429
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
void eraseFromParent()
Unlink &#39;this&#39; from the containing basic block and delete it.
This file provides an implementation of debug counters.
const TargetRegisterClass *const * sc_iterator
DEBUG_COUNTER(FwdCounter, "machine-cp-fwd", "Controls which register COPYs are forwarded")
bool regsOverlap(Register regA, Register regB) const
Returns true if the two registers are equal or alias each other.
unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:82
void setReg(Register Reg)
Change the register this operand corresponds to.
virtual const TargetInstrInfo * getInstrInfo() const
reverse_iterator rend()
reverse_iterator rbegin()
TargetInstrInfo - Interface to description of machine instruction set.
#define DEBUG_TYPE
sc_iterator getSuperClasses() const
Returns a NULL-terminated list of super-classes.
unsigned const MachineRegisterInfo * MRI
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
bool definesRegister(Register Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
self_iterator getIterator()
Definition: ilist_node.h:81
static bool shouldExecute(unsigned CounterName)
Definition: DebugCounter.h:73
char & MachineCopyPropagationID
MachineCopyPropagation - This pass performs copy propagation on machine instructions.
bool isCopy() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a sub-register of RegA or if RegB == RegA.
bool isConstantPhysReg(unsigned PhysReg) const
Returns true if PhysReg is unallocatable and constant throughout the function.
void initializeMachineCopyPropagationPass(PassRegistry &)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:297
MachineOperand class - Representation of each machine instruction operand.
bool modifiesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register...
SI Lower i1 Copies
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
bool isSubRegister(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a sub-register of RegA.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
bool isRenamable() const
isRenamable - Returns true if this register may be renamed, i.e.
iterator_range< mop_iterator > implicit_operands()
Definition: MachineInstr.h:506
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:226
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
static bool isBackwardPropagatableCopy(MachineInstr &MI, const MachineRegisterInfo &MRI)
Representation of each machine instruction.
Definition: MachineInstr.h:63
reverse_self_iterator getReverseIterator()
Definition: ilist_node.h:84
void updateDbgUsersToReg(unsigned Reg, ArrayRef< MachineInstr *> Users) const
updateDbgUsersToReg - Update a collection of DBG_VALUE instructions to refer to the designated regist...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
#define I(x, y, z)
Definition: MD5.cpp:58
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
DebugType
Definition: COFF.h:645
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:69
static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src, unsigned Def, const TargetRegisterInfo *TRI)
Return true if PreviousCopy did copy register Src to register Def.
IRTranslator LLVM IR MI
Register getReg() const
getReg - Returns the register number.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:431
void clearRegisterKills(Register Reg, const TargetRegisterInfo *RegInfo)
Clear all kill flags affecting Reg.
Properties which a MachineFunction may have at a given point in time.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
bool isImplicit() const
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1241