MachineCopyPropagation.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. //===- 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. // 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
  40. //
  41. //===----------------------------------------------------------------------===//
  42. #include "llvm/ADT/DenseMap.h"
  43. #include "llvm/ADT/STLExtras.h"
  44. #include "llvm/ADT/SetVector.h"
  45. #include "llvm/ADT/SmallVector.h"
  46. #include "llvm/ADT/Statistic.h"
  47. #include "llvm/ADT/iterator_range.h"
  48. #include "llvm/CodeGen/MachineBasicBlock.h"
  49. #include "llvm/CodeGen/MachineFunction.h"
  50. #include "llvm/CodeGen/MachineFunctionPass.h"
  51. #include "llvm/CodeGen/MachineInstr.h"
  52. #include "llvm/CodeGen/MachineOperand.h"
  53. #include "llvm/CodeGen/MachineRegisterInfo.h"
  54. #include "llvm/CodeGen/TargetInstrInfo.h"
  55. #include "llvm/CodeGen/TargetRegisterInfo.h"
  56. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  57. #include "llvm/MC/MCRegisterInfo.h"
  58. #include "llvm/Pass.h"
  59. #include "llvm/Support/Debug.h"
  60. #include "llvm/Support/DebugCounter.h"
  61. #include "llvm/Support/raw_ostream.h"
  62. #include <cassert>
  63. #include <iterator>
  64. using namespace llvm;
  65. #define DEBUG_TYPE "machine-cp"
  66. STATISTIC(NumDeletes, "Number of dead copies deleted");
  67. STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
  68. DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
  69. "Controls which register COPYs are forwarded");
  70. namespace {
  71. using RegList = SmallVector<unsigned, 4>;
  72. using SourceMap = DenseMap<unsigned, RegList>;
  73. using Reg2MIMap = DenseMap<unsigned, MachineInstr *>;
  74. class MachineCopyPropagation : public MachineFunctionPass {
  75. const TargetRegisterInfo *TRI;
  76. const TargetInstrInfo *TII;
  77. const MachineRegisterInfo *MRI;
  78. public:
  79. static char ID; // Pass identification, replacement for typeid
  80. MachineCopyPropagation() : MachineFunctionPass(ID) {
  81. initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
  82. }
  83. void getAnalysisUsage(AnalysisUsage &AU) const override {
  84. AU.setPreservesCFG();
  85. MachineFunctionPass::getAnalysisUsage(AU);
  86. }
  87. bool runOnMachineFunction(MachineFunction &MF) override;
  88. MachineFunctionProperties getRequiredProperties() const override {
  89. return MachineFunctionProperties().set(
  90. MachineFunctionProperties::Property::NoVRegs);
  91. }
  92. private:
  93. void ClobberRegister(unsigned Reg);
  94. void ReadRegister(unsigned Reg);
  95. void CopyPropagateBlock(MachineBasicBlock &MBB);
  96. bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
  97. void forwardUses(MachineInstr &MI);
  98. bool isForwardableRegClassCopy(const MachineInstr &Copy,
  99. const MachineInstr &UseI, unsigned UseIdx);
  100. bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
  101. /// Candidates for deletion.
  102. SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
  103. /// Def -> available copies map.
  104. Reg2MIMap AvailCopyMap;
  105. /// Def -> copies map.
  106. Reg2MIMap CopyMap;
  107. /// Src -> Def map
  108. SourceMap SrcMap;
  109. bool Changed;
  110. };
  111. } // end anonymous namespace
  112. char MachineCopyPropagation::ID = 0;
  113. char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
  114. INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
  115. "Machine Copy Propagation Pass", false, false)
  116. /// Remove any entry in \p Map where the register is a subregister or equal to
  117. /// a register contained in \p Regs.
  118. static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
  119. const TargetRegisterInfo &TRI) {
  120. for (unsigned Reg : Regs) {
  121. // Source of copy is no longer available for propagation.
  122. for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
  123. Map.erase(*SR);
  124. }
  125. }
  126. /// Remove any entry in \p Map that is marked clobbered in \p RegMask.
  127. /// The map will typically have a lot fewer entries than the regmask clobbers,
  128. /// so this is more efficient than iterating the clobbered registers and calling
  129. /// ClobberRegister() on them.
  130. static void removeClobberedRegsFromMap(Reg2MIMap &Map,
  131. const MachineOperand &RegMask) {
  132. for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
  133. I = Next) {
  134. Next = std::next(I);
  135. unsigned Reg = I->first;
  136. if (RegMask.clobbersPhysReg(Reg))
  137. Map.erase(I);
  138. }
  139. }
  140. void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
  141. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  142. CopyMap.erase(*AI);
  143. AvailCopyMap.erase(*AI);
  144. SourceMap::iterator SI = SrcMap.find(*AI);
  145. if (SI != SrcMap.end()) {
  146. removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
  147. SrcMap.erase(SI);
  148. }
  149. }
  150. }
  151. void MachineCopyPropagation::ReadRegister(unsigned Reg) {
  152. // If 'Reg' is defined by a copy, the copy is no longer a candidate
  153. // for elimination.
  154. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  155. Reg2MIMap::iterator CI = CopyMap.find(*AI);
  156. if (CI != CopyMap.end()) {
  157. LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: ";
  158. CI->second->dump());
  159. MaybeDeadCopies.remove(CI->second);
  160. }
  161. }
  162. }
  163. /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
  164. /// This fact may have been obscured by sub register usage or may not be true at
  165. /// all even though Src and Def are subregisters of the registers used in
  166. /// PreviousCopy. e.g.
  167. /// isNopCopy("ecx = COPY eax", AX, CX) == true
  168. /// isNopCopy("ecx = COPY eax", AH, CL) == false
  169. static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
  170. unsigned Def, const TargetRegisterInfo *TRI) {
  171. unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
  172. unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
  173. if (Src == PreviousSrc) {
  174. assert(Def == PreviousDef);
  175. return true;
  176. }
  177. if (!TRI->isSubRegister(PreviousSrc, Src))
  178. return false;
  179. unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
  180. return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
  181. }
  182. /// Remove instruction \p Copy if there exists a previous copy that copies the
  183. /// register \p Src to the register \p Def; This may happen indirectly by
  184. /// copying the super registers.
  185. bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
  186. unsigned Def) {
  187. // Avoid eliminating a copy from/to a reserved registers as we cannot predict
  188. // the value (Example: The sparc zero register is writable but stays zero).
  189. if (MRI->isReserved(Src) || MRI->isReserved(Def))
  190. return false;
  191. // Search for an existing copy.
  192. Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
  193. if (CI == AvailCopyMap.end())
  194. return false;
  195. // Check that the existing copy uses the correct sub registers.
  196. MachineInstr &PrevCopy = *CI->second;
  197. if (PrevCopy.getOperand(0).isDead())
  198. return false;
  199. if (!isNopCopy(PrevCopy, Src, Def, TRI))
  200. return false;
  201. LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
  202. // Copy was redundantly redefining either Src or Def. Remove earlier kill
  203. // flags between Copy and PrevCopy because the value will be reused now.
  204. assert(Copy.isCopy());
  205. unsigned CopyDef = Copy.getOperand(0).getReg();
  206. assert(CopyDef == Src || CopyDef == Def);
  207. for (MachineInstr &MI :
  208. make_range(PrevCopy.getIterator(), Copy.getIterator()))
  209. MI.clearRegisterKills(CopyDef, TRI);
  210. Copy.eraseFromParent();
  211. Changed = true;
  212. ++NumDeletes;
  213. return true;
  214. }
  215. /// Decide whether we should forward the source of \param Copy to its use in
  216. /// \param UseI based on the physical register class constraints of the opcode
  217. /// and avoiding introducing more cross-class COPYs.
  218. bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
  219. const MachineInstr &UseI,
  220. unsigned UseIdx) {
  221. unsigned CopySrcReg = Copy.getOperand(1).getReg();
  222. // If the new register meets the opcode register constraints, then allow
  223. // forwarding.
  224. if (const TargetRegisterClass *URC =
  225. UseI.getRegClassConstraint(UseIdx, TII, TRI))
  226. return URC->contains(CopySrcReg);
  227. if (!UseI.isCopy())
  228. return false;
  229. /// COPYs don't have register class constraints, so if the user instruction
  230. /// is a COPY, we just try to avoid introducing additional cross-class
  231. /// COPYs. For example:
  232. ///
  233. /// RegClassA = COPY RegClassB // Copy parameter
  234. /// ...
  235. /// RegClassB = COPY RegClassA // UseI parameter
  236. ///
  237. /// which after forwarding becomes
  238. ///
  239. /// RegClassA = COPY RegClassB
  240. /// ...
  241. /// RegClassB = COPY RegClassB
  242. ///
  243. /// so we have reduced the number of cross-class COPYs and potentially
  244. /// introduced a nop COPY that can be removed.
  245. const TargetRegisterClass *UseDstRC =
  246. TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
  247. const TargetRegisterClass *SuperRC = UseDstRC;
  248. for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
  249. SuperRC; SuperRC = *SuperRCI++)
  250. if (SuperRC->contains(CopySrcReg))
  251. return true;
  252. return false;
  253. }
  254. /// Check that \p MI does not have implicit uses that overlap with it's \p Use
  255. /// operand (the register being replaced), since these can sometimes be
  256. /// implicitly tied to other operands. For example, on AMDGPU:
  257. ///
  258. /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
  259. ///
  260. /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
  261. /// way of knowing we need to update the latter when updating the former.
  262. bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
  263. const MachineOperand &Use) {
  264. for (const MachineOperand &MIUse : MI.uses())
  265. if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
  266. MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
  267. return true;
  268. return false;
  269. }
  270. /// Look for available copies whose destination register is used by \p MI and
  271. /// replace the use in \p MI with the copy's source register.
  272. void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
  273. if (AvailCopyMap.empty())
  274. return;
  275. // Look for non-tied explicit vreg uses that have an active COPY
  276. // instruction that defines the physical register allocated to them.
  277. // Replace the vreg with the source of the active COPY.
  278. for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
  279. ++OpIdx) {
  280. MachineOperand &MOUse = MI.getOperand(OpIdx);
  281. // Don't forward into undef use operands since doing so can cause problems
  282. // with the machine verifier, since it doesn't treat undef reads as reads,
  283. // so we can end up with a live range that ends on an undef read, leading to
  284. // an error that the live range doesn't end on a read of the live range
  285. // register.
  286. if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
  287. MOUse.isImplicit())
  288. continue;
  289. if (!MOUse.getReg())
  290. continue;
  291. // Check that the register is marked 'renamable' so we know it is safe to
  292. // rename it without violating any constraints that aren't expressed in the
  293. // IR (e.g. ABI or opcode requirements).
  294. if (!MOUse.isRenamable())
  295. continue;
  296. auto CI = AvailCopyMap.find(MOUse.getReg());
  297. if (CI == AvailCopyMap.end())
  298. continue;
  299. MachineInstr &Copy = *CI->second;
  300. unsigned CopyDstReg = Copy.getOperand(0).getReg();
  301. const MachineOperand &CopySrc = Copy.getOperand(1);
  302. unsigned CopySrcReg = CopySrc.getReg();
  303. // FIXME: Don't handle partial uses of wider COPYs yet.
  304. if (MOUse.getReg() != CopyDstReg) {
  305. LLVM_DEBUG(
  306. dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n "
  307. << MI);
  308. continue;
  309. }
  310. // Don't forward COPYs of reserved regs unless they are constant.
  311. if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
  312. continue;
  313. if (!isForwardableRegClassCopy(Copy, MI, OpIdx))
  314. continue;
  315. if (hasImplicitOverlap(MI, MOUse))
  316. continue;
  317. if (!DebugCounter::shouldExecute(FwdCounter)) {
  318. LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n "
  319. << MI);
  320. continue;
  321. }
  322. LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
  323. << "\n with " << printReg(CopySrcReg, TRI)
  324. << "\n in " << MI << " from " << Copy);
  325. MOUse.setReg(CopySrcReg);
  326. if (!CopySrc.isRenamable())
  327. MOUse.setIsRenamable(false);
  328. LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
  329. // Clear kill markers that may have been invalidated.
  330. for (MachineInstr &KMI :
  331. make_range(Copy.getIterator(), std::next(MI.getIterator())))
  332. KMI.clearRegisterKills(CopySrcReg, TRI);
  333. ++NumCopyForwards;
  334. Changed = true;
  335. }
  336. }
  337. void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
  338. LLVM_DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
  339. for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
  340. MachineInstr *MI = &*I;
  341. ++I;
  342. // Analyze copies (which don't overlap themselves).
  343. if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(),
  344. MI->getOperand(1).getReg())) {
  345. unsigned Def = MI->getOperand(0).getReg();
  346. unsigned Src = MI->getOperand(1).getReg();
  347. assert(!TargetRegisterInfo::isVirtualRegister(Def) &&
  348. !TargetRegisterInfo::isVirtualRegister(Src) &&
  349. "MachineCopyPropagation should be run after register allocation!");
  350. // The two copies cancel out and the source of the first copy
  351. // hasn't been overridden, eliminate the second one. e.g.
  352. // %ecx = COPY %eax
  353. // ... nothing clobbered eax.
  354. // %eax = COPY %ecx
  355. // =>
  356. // %ecx = COPY %eax
  357. //
  358. // or
  359. //
  360. // %ecx = COPY %eax
  361. // ... nothing clobbered eax.
  362. // %ecx = COPY %eax
  363. // =>
  364. // %ecx = COPY %eax
  365. if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
  366. continue;
  367. forwardUses(*MI);
  368. // Src may have been changed by forwardUses()
  369. Src = MI->getOperand(1).getReg();
  370. // If Src is defined by a previous copy, the previous copy cannot be
  371. // eliminated.
  372. ReadRegister(Src);
  373. for (const MachineOperand &MO : MI->implicit_operands()) {
  374. if (!MO.isReg() || !MO.readsReg())
  375. continue;
  376. unsigned Reg = MO.getReg();
  377. if (!Reg)
  378. continue;
  379. ReadRegister(Reg);
  380. }
  381. LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
  382. // Copy is now a candidate for deletion.
  383. if (!MRI->isReserved(Def))
  384. MaybeDeadCopies.insert(MI);
  385. // If 'Def' is previously source of another copy, then this earlier copy's
  386. // source is no longer available. e.g.
  387. // %xmm9 = copy %xmm2
  388. // ...
  389. // %xmm2 = copy %xmm0
  390. // ...
  391. // %xmm2 = copy %xmm9
  392. ClobberRegister(Def);
  393. for (const MachineOperand &MO : MI->implicit_operands()) {
  394. if (!MO.isReg() || !MO.isDef())
  395. continue;
  396. unsigned Reg = MO.getReg();
  397. if (!Reg)
  398. continue;
  399. ClobberRegister(Reg);
  400. }
  401. // Remember Def is defined by the copy.
  402. for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
  403. ++SR) {
  404. CopyMap[*SR] = MI;
  405. AvailCopyMap[*SR] = MI;
  406. }
  407. // Remember source that's copied to Def. Once it's clobbered, then
  408. // it's no longer available for copy propagation.
  409. RegList &DestList = SrcMap[Src];
  410. if (!is_contained(DestList, Def))
  411. DestList.push_back(Def);
  412. continue;
  413. }
  414. // Clobber any earlyclobber regs first.
  415. for (const MachineOperand &MO : MI->operands())
  416. if (MO.isReg() && MO.isEarlyClobber()) {
  417. unsigned Reg = MO.getReg();
  418. // If we have a tied earlyclobber, that means it is also read by this
  419. // instruction, so we need to make sure we don't remove it as dead
  420. // later.
  421. if (MO.isTied())
  422. ReadRegister(Reg);
  423. ClobberRegister(Reg);
  424. }
  425. forwardUses(*MI);
  426. // Not a copy.
  427. SmallVector<unsigned, 2> Defs;
  428. const MachineOperand *RegMask = nullptr;
  429. for (const MachineOperand &MO : MI->operands()) {
  430. if (MO.isRegMask())
  431. RegMask = &MO;
  432. if (!MO.isReg())
  433. continue;
  434. unsigned Reg = MO.getReg();
  435. if (!Reg)
  436. continue;
  437. assert(!TargetRegisterInfo::isVirtualRegister(Reg) &&
  438. "MachineCopyPropagation should be run after register allocation!");
  439. if (MO.isDef() && !MO.isEarlyClobber()) {
  440. Defs.push_back(Reg);
  441. continue;
  442. } else if (!MO.isDebug() && MO.readsReg())
  443. ReadRegister(Reg);
  444. }
  445. // The instruction has a register mask operand which means that it clobbers
  446. // a large set of registers. Treat clobbered registers the same way as
  447. // defined registers.
  448. if (RegMask) {
  449. // Erase any MaybeDeadCopies whose destination register is clobbered.
  450. for (SmallSetVector<MachineInstr *, 8>::iterator DI =
  451. MaybeDeadCopies.begin();
  452. DI != MaybeDeadCopies.end();) {
  453. MachineInstr *MaybeDead = *DI;
  454. unsigned Reg = MaybeDead->getOperand(0).getReg();
  455. assert(!MRI->isReserved(Reg));
  456. if (!RegMask->clobbersPhysReg(Reg)) {
  457. ++DI;
  458. continue;
  459. }
  460. LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
  461. MaybeDead->dump());
  462. // erase() will return the next valid iterator pointing to the next
  463. // element after the erased one.
  464. DI = MaybeDeadCopies.erase(DI);
  465. MaybeDead->eraseFromParent();
  466. Changed = true;
  467. ++NumDeletes;
  468. }
  469. removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
  470. removeClobberedRegsFromMap(CopyMap, *RegMask);
  471. for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
  472. I != E; I = Next) {
  473. Next = std::next(I);
  474. if (RegMask->clobbersPhysReg(I->first)) {
  475. removeRegsFromMap(AvailCopyMap, I->second, *TRI);
  476. SrcMap.erase(I);
  477. }
  478. }
  479. }
  480. // Any previous copy definition or reading the Defs is no longer available.
  481. for (unsigned Reg : Defs)
  482. ClobberRegister(Reg);
  483. }
  484. // If MBB doesn't have successors, delete the copies whose defs are not used.
  485. // If MBB does have successors, then conservative assume the defs are live-out
  486. // since we don't want to trust live-in lists.
  487. if (MBB.succ_empty()) {
  488. for (MachineInstr *MaybeDead : MaybeDeadCopies) {
  489. LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
  490. MaybeDead->dump());
  491. assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
  492. MaybeDead->eraseFromParent();
  493. Changed = true;
  494. ++NumDeletes;
  495. }
  496. }
  497. MaybeDeadCopies.clear();
  498. AvailCopyMap.clear();
  499. CopyMap.clear();
  500. SrcMap.clear();
  501. }
  502. bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
  503. if (skipFunction(MF.getFunction()))
  504. return false;
  505. Changed = false;
  506. TRI = MF.getSubtarget().getRegisterInfo();
  507. TII = MF.getSubtarget().getInstrInfo();
  508. MRI = &MF.getRegInfo();
  509. for (MachineBasicBlock &MBB : MF)
  510. CopyPropagateBlock(MBB);
  511. return Changed;
  512. }