MachineCopyPropagation.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. #include "llvm/ADT/DenseMap.h"
  51. #include "llvm/ADT/STLExtras.h"
  52. #include "llvm/ADT/SetVector.h"
  53. #include "llvm/ADT/SmallSet.h"
  54. #include "llvm/ADT/SmallVector.h"
  55. #include "llvm/ADT/Statistic.h"
  56. #include "llvm/ADT/iterator_range.h"
  57. #include "llvm/CodeGen/MachineBasicBlock.h"
  58. #include "llvm/CodeGen/MachineFunction.h"
  59. #include "llvm/CodeGen/MachineFunctionPass.h"
  60. #include "llvm/CodeGen/MachineInstr.h"
  61. #include "llvm/CodeGen/MachineOperand.h"
  62. #include "llvm/CodeGen/MachineRegisterInfo.h"
  63. #include "llvm/CodeGen/TargetInstrInfo.h"
  64. #include "llvm/CodeGen/TargetRegisterInfo.h"
  65. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  66. #include "llvm/InitializePasses.h"
  67. #include "llvm/MC/MCRegisterInfo.h"
  68. #include "llvm/Pass.h"
  69. #include "llvm/Support/Debug.h"
  70. #include "llvm/Support/DebugCounter.h"
  71. #include "llvm/Support/raw_ostream.h"
  72. #include <cassert>
  73. #include <iterator>
  74. using namespace llvm;
  75. #define DEBUG_TYPE "machine-cp"
  76. STATISTIC(NumDeletes, "Number of dead copies deleted");
  77. STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
  78. STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated");
  79. DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
  80. "Controls which register COPYs are forwarded");
  81. namespace {
  82. class CopyTracker {
  83. struct CopyInfo {
  84. MachineInstr *MI;
  85. SmallVector<MCRegister, 4> DefRegs;
  86. bool Avail;
  87. };
  88. DenseMap<MCRegister, CopyInfo> Copies;
  89. public:
  90. /// Mark all of the given registers and their subregisters as unavailable for
  91. /// copying.
  92. void markRegsUnavailable(ArrayRef<MCRegister> Regs,
  93. const TargetRegisterInfo &TRI) {
  94. for (MCRegister Reg : Regs) {
  95. // Source of copy is no longer available for propagation.
  96. for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
  97. auto CI = Copies.find(*RUI);
  98. if (CI != Copies.end())
  99. CI->second.Avail = false;
  100. }
  101. }
  102. }
  103. /// Remove register from copy maps.
  104. void invalidateRegister(MCRegister Reg, const TargetRegisterInfo &TRI) {
  105. // Since Reg might be a subreg of some registers, only invalidate Reg is not
  106. // enough. We have to find the COPY defines Reg or registers defined by Reg
  107. // and invalidate all of them.
  108. SmallSet<MCRegister, 8> RegsToInvalidate;
  109. RegsToInvalidate.insert(Reg);
  110. for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
  111. auto I = Copies.find(*RUI);
  112. if (I != Copies.end()) {
  113. if (MachineInstr *MI = I->second.MI) {
  114. RegsToInvalidate.insert(MI->getOperand(0).getReg().asMCReg());
  115. RegsToInvalidate.insert(MI->getOperand(1).getReg().asMCReg());
  116. }
  117. RegsToInvalidate.insert(I->second.DefRegs.begin(),
  118. I->second.DefRegs.end());
  119. }
  120. }
  121. for (MCRegister InvalidReg : RegsToInvalidate)
  122. for (MCRegUnitIterator RUI(InvalidReg, &TRI); RUI.isValid(); ++RUI)
  123. Copies.erase(*RUI);
  124. }
  125. /// Clobber a single register, removing it from the tracker's copy maps.
  126. void clobberRegister(MCRegister Reg, const TargetRegisterInfo &TRI) {
  127. for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
  128. auto I = Copies.find(*RUI);
  129. if (I != Copies.end()) {
  130. // When we clobber the source of a copy, we need to clobber everything
  131. // it defined.
  132. markRegsUnavailable(I->second.DefRegs, TRI);
  133. // When we clobber the destination of a copy, we need to clobber the
  134. // whole register it defined.
  135. if (MachineInstr *MI = I->second.MI)
  136. markRegsUnavailable({MI->getOperand(0).getReg().asMCReg()}, TRI);
  137. // Now we can erase the copy.
  138. Copies.erase(I);
  139. }
  140. }
  141. }
  142. /// Add this copy's registers into the tracker's copy maps.
  143. void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) {
  144. assert(MI->isCopy() && "Tracking non-copy?");
  145. MCRegister Def = MI->getOperand(0).getReg().asMCReg();
  146. MCRegister Src = MI->getOperand(1).getReg().asMCReg();
  147. // Remember Def is defined by the copy.
  148. for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
  149. Copies[*RUI] = {MI, {}, true};
  150. // Remember source that's copied to Def. Once it's clobbered, then
  151. // it's no longer available for copy propagation.
  152. for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
  153. auto I = Copies.insert({*RUI, {nullptr, {}, false}});
  154. auto &Copy = I.first->second;
  155. if (!is_contained(Copy.DefRegs, Def))
  156. Copy.DefRegs.push_back(Def);
  157. }
  158. }
  159. bool hasAnyCopies() {
  160. return !Copies.empty();
  161. }
  162. MachineInstr *findCopyForUnit(MCRegister RegUnit,
  163. const TargetRegisterInfo &TRI,
  164. bool MustBeAvailable = false) {
  165. auto CI = Copies.find(RegUnit);
  166. if (CI == Copies.end())
  167. return nullptr;
  168. if (MustBeAvailable && !CI->second.Avail)
  169. return nullptr;
  170. return CI->second.MI;
  171. }
  172. MachineInstr *findCopyDefViaUnit(MCRegister RegUnit,
  173. const TargetRegisterInfo &TRI) {
  174. auto CI = Copies.find(RegUnit);
  175. if (CI == Copies.end())
  176. return nullptr;
  177. if (CI->second.DefRegs.size() != 1)
  178. return nullptr;
  179. MCRegUnitIterator RUI(CI->second.DefRegs[0], &TRI);
  180. return findCopyForUnit(*RUI, TRI, true);
  181. }
  182. MachineInstr *findAvailBackwardCopy(MachineInstr &I, MCRegister Reg,
  183. const TargetRegisterInfo &TRI) {
  184. MCRegUnitIterator RUI(Reg, &TRI);
  185. MachineInstr *AvailCopy = findCopyDefViaUnit(*RUI, TRI);
  186. if (!AvailCopy ||
  187. !TRI.isSubRegisterEq(AvailCopy->getOperand(1).getReg(), Reg))
  188. return nullptr;
  189. Register AvailSrc = AvailCopy->getOperand(1).getReg();
  190. Register AvailDef = AvailCopy->getOperand(0).getReg();
  191. for (const MachineInstr &MI :
  192. make_range(AvailCopy->getReverseIterator(), I.getReverseIterator()))
  193. for (const MachineOperand &MO : MI.operands())
  194. if (MO.isRegMask())
  195. // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef?
  196. if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
  197. return nullptr;
  198. return AvailCopy;
  199. }
  200. MachineInstr *findAvailCopy(MachineInstr &DestCopy, MCRegister Reg,
  201. const TargetRegisterInfo &TRI) {
  202. // We check the first RegUnit here, since we'll only be interested in the
  203. // copy if it copies the entire register anyway.
  204. MCRegUnitIterator RUI(Reg, &TRI);
  205. MachineInstr *AvailCopy =
  206. findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true);
  207. if (!AvailCopy ||
  208. !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg))
  209. return nullptr;
  210. // Check that the available copy isn't clobbered by any regmasks between
  211. // itself and the destination.
  212. Register AvailSrc = AvailCopy->getOperand(1).getReg();
  213. Register AvailDef = AvailCopy->getOperand(0).getReg();
  214. for (const MachineInstr &MI :
  215. make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
  216. for (const MachineOperand &MO : MI.operands())
  217. if (MO.isRegMask())
  218. if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
  219. return nullptr;
  220. return AvailCopy;
  221. }
  222. void clear() {
  223. Copies.clear();
  224. }
  225. };
  226. class MachineCopyPropagation : public MachineFunctionPass {
  227. const TargetRegisterInfo *TRI;
  228. const TargetInstrInfo *TII;
  229. const MachineRegisterInfo *MRI;
  230. public:
  231. static char ID; // Pass identification, replacement for typeid
  232. MachineCopyPropagation() : MachineFunctionPass(ID) {
  233. initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
  234. }
  235. void getAnalysisUsage(AnalysisUsage &AU) const override {
  236. AU.setPreservesCFG();
  237. MachineFunctionPass::getAnalysisUsage(AU);
  238. }
  239. bool runOnMachineFunction(MachineFunction &MF) override;
  240. MachineFunctionProperties getRequiredProperties() const override {
  241. return MachineFunctionProperties().set(
  242. MachineFunctionProperties::Property::NoVRegs);
  243. }
  244. private:
  245. typedef enum { DebugUse = false, RegularUse = true } DebugType;
  246. void ReadRegister(MCRegister Reg, MachineInstr &Reader, DebugType DT);
  247. void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
  248. void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
  249. bool eraseIfRedundant(MachineInstr &Copy, MCRegister Src, MCRegister Def);
  250. void forwardUses(MachineInstr &MI);
  251. void propagateDefs(MachineInstr &MI);
  252. bool isForwardableRegClassCopy(const MachineInstr &Copy,
  253. const MachineInstr &UseI, unsigned UseIdx);
  254. bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
  255. const MachineInstr &UseI,
  256. unsigned UseIdx);
  257. bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
  258. bool hasOverlappingMultipleDef(const MachineInstr &MI,
  259. const MachineOperand &MODef, Register Def);
  260. /// Candidates for deletion.
  261. SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
  262. /// Multimap tracking debug users in current BB
  263. DenseMap<MachineInstr *, SmallSet<MachineInstr *, 2>> CopyDbgUsers;
  264. CopyTracker Tracker;
  265. bool Changed;
  266. };
  267. } // end anonymous namespace
  268. char MachineCopyPropagation::ID = 0;
  269. char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
  270. INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
  271. "Machine Copy Propagation Pass", false, false)
  272. void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader,
  273. DebugType DT) {
  274. // If 'Reg' is defined by a copy, the copy is no longer a candidate
  275. // for elimination. If a copy is "read" by a debug user, record the user
  276. // for propagation.
  277. for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
  278. if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
  279. if (DT == RegularUse) {
  280. LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
  281. MaybeDeadCopies.remove(Copy);
  282. } else {
  283. CopyDbgUsers[Copy].insert(&Reader);
  284. }
  285. }
  286. }
  287. }
  288. /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
  289. /// This fact may have been obscured by sub register usage or may not be true at
  290. /// all even though Src and Def are subregisters of the registers used in
  291. /// PreviousCopy. e.g.
  292. /// isNopCopy("ecx = COPY eax", AX, CX) == true
  293. /// isNopCopy("ecx = COPY eax", AH, CL) == false
  294. static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src,
  295. MCRegister Def, const TargetRegisterInfo *TRI) {
  296. MCRegister PreviousSrc = PreviousCopy.getOperand(1).getReg().asMCReg();
  297. MCRegister PreviousDef = PreviousCopy.getOperand(0).getReg().asMCReg();
  298. if (Src == PreviousSrc && Def == PreviousDef)
  299. return true;
  300. if (!TRI->isSubRegister(PreviousSrc, Src))
  301. return false;
  302. unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
  303. return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
  304. }
  305. /// Remove instruction \p Copy if there exists a previous copy that copies the
  306. /// register \p Src to the register \p Def; This may happen indirectly by
  307. /// copying the super registers.
  308. bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy,
  309. MCRegister Src, MCRegister Def) {
  310. // Avoid eliminating a copy from/to a reserved registers as we cannot predict
  311. // the value (Example: The sparc zero register is writable but stays zero).
  312. if (MRI->isReserved(Src) || MRI->isReserved(Def))
  313. return false;
  314. // Search for an existing copy.
  315. MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
  316. if (!PrevCopy)
  317. return false;
  318. // Check that the existing copy uses the correct sub registers.
  319. if (PrevCopy->getOperand(0).isDead())
  320. return false;
  321. if (!isNopCopy(*PrevCopy, Src, Def, TRI))
  322. return false;
  323. LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
  324. // Copy was redundantly redefining either Src or Def. Remove earlier kill
  325. // flags between Copy and PrevCopy because the value will be reused now.
  326. assert(Copy.isCopy());
  327. Register CopyDef = Copy.getOperand(0).getReg();
  328. assert(CopyDef == Src || CopyDef == Def);
  329. for (MachineInstr &MI :
  330. make_range(PrevCopy->getIterator(), Copy.getIterator()))
  331. MI.clearRegisterKills(CopyDef, TRI);
  332. Copy.eraseFromParent();
  333. Changed = true;
  334. ++NumDeletes;
  335. return true;
  336. }
  337. bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
  338. const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
  339. Register Def = Copy.getOperand(0).getReg();
  340. if (const TargetRegisterClass *URC =
  341. UseI.getRegClassConstraint(UseIdx, TII, TRI))
  342. return URC->contains(Def);
  343. // We don't process further if UseI is a COPY, since forward copy propagation
  344. // should handle that.
  345. return false;
  346. }
  347. /// Decide whether we should forward the source of \param Copy to its use in
  348. /// \param UseI based on the physical register class constraints of the opcode
  349. /// and avoiding introducing more cross-class COPYs.
  350. bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
  351. const MachineInstr &UseI,
  352. unsigned UseIdx) {
  353. Register CopySrcReg = Copy.getOperand(1).getReg();
  354. // If the new register meets the opcode register constraints, then allow
  355. // forwarding.
  356. if (const TargetRegisterClass *URC =
  357. UseI.getRegClassConstraint(UseIdx, TII, TRI))
  358. return URC->contains(CopySrcReg);
  359. if (!UseI.isCopy())
  360. return false;
  361. const TargetRegisterClass *CopySrcRC =
  362. TRI->getMinimalPhysRegClass(CopySrcReg);
  363. const TargetRegisterClass *UseDstRC =
  364. TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
  365. const TargetRegisterClass *CrossCopyRC = TRI->getCrossCopyRegClass(CopySrcRC);
  366. // If cross copy register class is not the same as copy source register class
  367. // then it is not possible to copy the register directly and requires a cross
  368. // register class copy. Fowarding this copy without checking register class of
  369. // UseDst may create additional cross register copies when expanding the copy
  370. // instruction in later passes.
  371. if (CopySrcRC != CrossCopyRC) {
  372. const TargetRegisterClass *CopyDstRC =
  373. TRI->getMinimalPhysRegClass(Copy.getOperand(0).getReg());
  374. // Check if UseDstRC matches the necessary register class to copy from
  375. // CopySrc's register class. If so then forwarding the copy will not
  376. // introduce any cross-class copys. Else if CopyDstRC matches then keep the
  377. // copy and do not forward. If neither UseDstRC or CopyDstRC matches then
  378. // we may need a cross register copy later but we do not worry about it
  379. // here.
  380. if (UseDstRC != CrossCopyRC && CopyDstRC == CrossCopyRC)
  381. return false;
  382. }
  383. /// COPYs don't have register class constraints, so if the user instruction
  384. /// is a COPY, we just try to avoid introducing additional cross-class
  385. /// COPYs. For example:
  386. ///
  387. /// RegClassA = COPY RegClassB // Copy parameter
  388. /// ...
  389. /// RegClassB = COPY RegClassA // UseI parameter
  390. ///
  391. /// which after forwarding becomes
  392. ///
  393. /// RegClassA = COPY RegClassB
  394. /// ...
  395. /// RegClassB = COPY RegClassB
  396. ///
  397. /// so we have reduced the number of cross-class COPYs and potentially
  398. /// introduced a nop COPY that can be removed.
  399. const TargetRegisterClass *SuperRC = UseDstRC;
  400. for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
  401. SuperRC; SuperRC = *SuperRCI++)
  402. if (SuperRC->contains(CopySrcReg))
  403. return true;
  404. return false;
  405. }
  406. /// Check that \p MI does not have implicit uses that overlap with it's \p Use
  407. /// operand (the register being replaced), since these can sometimes be
  408. /// implicitly tied to other operands. For example, on AMDGPU:
  409. ///
  410. /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
  411. ///
  412. /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
  413. /// way of knowing we need to update the latter when updating the former.
  414. bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
  415. const MachineOperand &Use) {
  416. for (const MachineOperand &MIUse : MI.uses())
  417. if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
  418. MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
  419. return true;
  420. return false;
  421. }
  422. /// For an MI that has multiple definitions, check whether \p MI has
  423. /// a definition that overlaps with another of its definitions.
  424. /// For example, on ARM: umull r9, r9, lr, r0
  425. /// The umull instruction is unpredictable unless RdHi and RdLo are different.
  426. bool MachineCopyPropagation::hasOverlappingMultipleDef(
  427. const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
  428. for (const MachineOperand &MIDef : MI.defs()) {
  429. if ((&MIDef != &MODef) && MIDef.isReg() &&
  430. TRI->regsOverlap(Def, MIDef.getReg()))
  431. return true;
  432. }
  433. return false;
  434. }
  435. /// Look for available copies whose destination register is used by \p MI and
  436. /// replace the use in \p MI with the copy's source register.
  437. void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
  438. if (!Tracker.hasAnyCopies())
  439. return;
  440. // Look for non-tied explicit vreg uses that have an active COPY
  441. // instruction that defines the physical register allocated to them.
  442. // Replace the vreg with the source of the active COPY.
  443. for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
  444. ++OpIdx) {
  445. MachineOperand &MOUse = MI.getOperand(OpIdx);
  446. // Don't forward into undef use operands since doing so can cause problems
  447. // with the machine verifier, since it doesn't treat undef reads as reads,
  448. // so we can end up with a live range that ends on an undef read, leading to
  449. // an error that the live range doesn't end on a read of the live range
  450. // register.
  451. if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
  452. MOUse.isImplicit())
  453. continue;
  454. if (!MOUse.getReg())
  455. continue;
  456. // Check that the register is marked 'renamable' so we know it is safe to
  457. // rename it without violating any constraints that aren't expressed in the
  458. // IR (e.g. ABI or opcode requirements).
  459. if (!MOUse.isRenamable())
  460. continue;
  461. MachineInstr *Copy =
  462. Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(), *TRI);
  463. if (!Copy)
  464. continue;
  465. Register CopyDstReg = Copy->getOperand(0).getReg();
  466. const MachineOperand &CopySrc = Copy->getOperand(1);
  467. Register CopySrcReg = CopySrc.getReg();
  468. // FIXME: Don't handle partial uses of wider COPYs yet.
  469. if (MOUse.getReg() != CopyDstReg) {
  470. LLVM_DEBUG(
  471. dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n "
  472. << MI);
  473. continue;
  474. }
  475. // Don't forward COPYs of reserved regs unless they are constant.
  476. if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
  477. continue;
  478. if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
  479. continue;
  480. if (hasImplicitOverlap(MI, MOUse))
  481. continue;
  482. // Check that the instruction is not a copy that partially overwrites the
  483. // original copy source that we are about to use. The tracker mechanism
  484. // cannot cope with that.
  485. if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) &&
  486. !MI.definesRegister(CopySrcReg)) {
  487. LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
  488. continue;
  489. }
  490. if (!DebugCounter::shouldExecute(FwdCounter)) {
  491. LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n "
  492. << MI);
  493. continue;
  494. }
  495. LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
  496. << "\n with " << printReg(CopySrcReg, TRI)
  497. << "\n in " << MI << " from " << *Copy);
  498. MOUse.setReg(CopySrcReg);
  499. if (!CopySrc.isRenamable())
  500. MOUse.setIsRenamable(false);
  501. MOUse.setIsUndef(CopySrc.isUndef());
  502. LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
  503. // Clear kill markers that may have been invalidated.
  504. for (MachineInstr &KMI :
  505. make_range(Copy->getIterator(), std::next(MI.getIterator())))
  506. KMI.clearRegisterKills(CopySrcReg, TRI);
  507. ++NumCopyForwards;
  508. Changed = true;
  509. }
  510. }
  511. void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
  512. LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
  513. << "\n");
  514. for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
  515. // Analyze copies (which don't overlap themselves).
  516. if (MI.isCopy() && !TRI->regsOverlap(MI.getOperand(0).getReg(),
  517. MI.getOperand(1).getReg())) {
  518. assert(MI.getOperand(0).getReg().isPhysical() &&
  519. MI.getOperand(1).getReg().isPhysical() &&
  520. "MachineCopyPropagation should be run after register allocation!");
  521. MCRegister Def = MI.getOperand(0).getReg().asMCReg();
  522. MCRegister Src = MI.getOperand(1).getReg().asMCReg();
  523. // The two copies cancel out and the source of the first copy
  524. // hasn't been overridden, eliminate the second one. e.g.
  525. // %ecx = COPY %eax
  526. // ... nothing clobbered eax.
  527. // %eax = COPY %ecx
  528. // =>
  529. // %ecx = COPY %eax
  530. //
  531. // or
  532. //
  533. // %ecx = COPY %eax
  534. // ... nothing clobbered eax.
  535. // %ecx = COPY %eax
  536. // =>
  537. // %ecx = COPY %eax
  538. if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def))
  539. continue;
  540. forwardUses(MI);
  541. // Src may have been changed by forwardUses()
  542. Src = MI.getOperand(1).getReg().asMCReg();
  543. // If Src is defined by a previous copy, the previous copy cannot be
  544. // eliminated.
  545. ReadRegister(Src, MI, RegularUse);
  546. for (const MachineOperand &MO : MI.implicit_operands()) {
  547. if (!MO.isReg() || !MO.readsReg())
  548. continue;
  549. MCRegister Reg = MO.getReg().asMCReg();
  550. if (!Reg)
  551. continue;
  552. ReadRegister(Reg, MI, RegularUse);
  553. }
  554. LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump());
  555. // Copy is now a candidate for deletion.
  556. if (!MRI->isReserved(Def))
  557. MaybeDeadCopies.insert(&MI);
  558. // If 'Def' is previously source of another copy, then this earlier copy's
  559. // source is no longer available. e.g.
  560. // %xmm9 = copy %xmm2
  561. // ...
  562. // %xmm2 = copy %xmm0
  563. // ...
  564. // %xmm2 = copy %xmm9
  565. Tracker.clobberRegister(Def, *TRI);
  566. for (const MachineOperand &MO : MI.implicit_operands()) {
  567. if (!MO.isReg() || !MO.isDef())
  568. continue;
  569. MCRegister Reg = MO.getReg().asMCReg();
  570. if (!Reg)
  571. continue;
  572. Tracker.clobberRegister(Reg, *TRI);
  573. }
  574. Tracker.trackCopy(&MI, *TRI);
  575. continue;
  576. }
  577. // Clobber any earlyclobber regs first.
  578. for (const MachineOperand &MO : MI.operands())
  579. if (MO.isReg() && MO.isEarlyClobber()) {
  580. MCRegister Reg = MO.getReg().asMCReg();
  581. // If we have a tied earlyclobber, that means it is also read by this
  582. // instruction, so we need to make sure we don't remove it as dead
  583. // later.
  584. if (MO.isTied())
  585. ReadRegister(Reg, MI, RegularUse);
  586. Tracker.clobberRegister(Reg, *TRI);
  587. }
  588. forwardUses(MI);
  589. // Not a copy.
  590. SmallVector<Register, 2> Defs;
  591. const MachineOperand *RegMask = nullptr;
  592. for (const MachineOperand &MO : MI.operands()) {
  593. if (MO.isRegMask())
  594. RegMask = &MO;
  595. if (!MO.isReg())
  596. continue;
  597. Register Reg = MO.getReg();
  598. if (!Reg)
  599. continue;
  600. assert(!Reg.isVirtual() &&
  601. "MachineCopyPropagation should be run after register allocation!");
  602. if (MO.isDef() && !MO.isEarlyClobber()) {
  603. Defs.push_back(Reg.asMCReg());
  604. continue;
  605. } else if (MO.readsReg())
  606. ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse);
  607. }
  608. // The instruction has a register mask operand which means that it clobbers
  609. // a large set of registers. Treat clobbered registers the same way as
  610. // defined registers.
  611. if (RegMask) {
  612. // Erase any MaybeDeadCopies whose destination register is clobbered.
  613. for (SmallSetVector<MachineInstr *, 8>::iterator DI =
  614. MaybeDeadCopies.begin();
  615. DI != MaybeDeadCopies.end();) {
  616. MachineInstr *MaybeDead = *DI;
  617. MCRegister Reg = MaybeDead->getOperand(0).getReg().asMCReg();
  618. assert(!MRI->isReserved(Reg));
  619. if (!RegMask->clobbersPhysReg(Reg)) {
  620. ++DI;
  621. continue;
  622. }
  623. LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
  624. MaybeDead->dump());
  625. // Make sure we invalidate any entries in the copy maps before erasing
  626. // the instruction.
  627. Tracker.clobberRegister(Reg, *TRI);
  628. // erase() will return the next valid iterator pointing to the next
  629. // element after the erased one.
  630. DI = MaybeDeadCopies.erase(DI);
  631. MaybeDead->eraseFromParent();
  632. Changed = true;
  633. ++NumDeletes;
  634. }
  635. }
  636. // Any previous copy definition or reading the Defs is no longer available.
  637. for (MCRegister Reg : Defs)
  638. Tracker.clobberRegister(Reg, *TRI);
  639. }
  640. // If MBB doesn't have successors, delete the copies whose defs are not used.
  641. // If MBB does have successors, then conservative assume the defs are live-out
  642. // since we don't want to trust live-in lists.
  643. if (MBB.succ_empty()) {
  644. for (MachineInstr *MaybeDead : MaybeDeadCopies) {
  645. LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
  646. MaybeDead->dump());
  647. assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
  648. // Update matching debug values, if any.
  649. assert(MaybeDead->isCopy());
  650. Register SrcReg = MaybeDead->getOperand(1).getReg();
  651. Register DestReg = MaybeDead->getOperand(0).getReg();
  652. SmallVector<MachineInstr *> MaybeDeadDbgUsers(
  653. CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end());
  654. MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(),
  655. MaybeDeadDbgUsers);
  656. MaybeDead->eraseFromParent();
  657. Changed = true;
  658. ++NumDeletes;
  659. }
  660. }
  661. MaybeDeadCopies.clear();
  662. CopyDbgUsers.clear();
  663. Tracker.clear();
  664. }
  665. static bool isBackwardPropagatableCopy(MachineInstr &MI,
  666. const MachineRegisterInfo &MRI) {
  667. assert(MI.isCopy() && "MI is expected to be a COPY");
  668. Register Def = MI.getOperand(0).getReg();
  669. Register Src = MI.getOperand(1).getReg();
  670. if (!Def || !Src)
  671. return false;
  672. if (MRI.isReserved(Def) || MRI.isReserved(Src))
  673. return false;
  674. return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill();
  675. }
  676. void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
  677. if (!Tracker.hasAnyCopies())
  678. return;
  679. for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
  680. ++OpIdx) {
  681. MachineOperand &MODef = MI.getOperand(OpIdx);
  682. if (!MODef.isReg() || MODef.isUse())
  683. continue;
  684. // Ignore non-trivial cases.
  685. if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
  686. continue;
  687. if (!MODef.getReg())
  688. continue;
  689. // We only handle if the register comes from a vreg.
  690. if (!MODef.isRenamable())
  691. continue;
  692. MachineInstr *Copy =
  693. Tracker.findAvailBackwardCopy(MI, MODef.getReg().asMCReg(), *TRI);
  694. if (!Copy)
  695. continue;
  696. Register Def = Copy->getOperand(0).getReg();
  697. Register Src = Copy->getOperand(1).getReg();
  698. if (MODef.getReg() != Src)
  699. continue;
  700. if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
  701. continue;
  702. if (hasImplicitOverlap(MI, MODef))
  703. continue;
  704. if (hasOverlappingMultipleDef(MI, MODef, Def))
  705. continue;
  706. LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
  707. << "\n with " << printReg(Def, TRI) << "\n in "
  708. << MI << " from " << *Copy);
  709. MODef.setReg(Def);
  710. MODef.setIsRenamable(Copy->getOperand(0).isRenamable());
  711. LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
  712. MaybeDeadCopies.insert(Copy);
  713. Changed = true;
  714. ++NumCopyBackwardPropagated;
  715. }
  716. }
  717. void MachineCopyPropagation::BackwardCopyPropagateBlock(
  718. MachineBasicBlock &MBB) {
  719. LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
  720. << "\n");
  721. for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) {
  722. // Ignore non-trivial COPYs.
  723. if (MI.isCopy() && MI.getNumOperands() == 2 &&
  724. !TRI->regsOverlap(MI.getOperand(0).getReg(),
  725. MI.getOperand(1).getReg())) {
  726. MCRegister Def = MI.getOperand(0).getReg().asMCReg();
  727. MCRegister Src = MI.getOperand(1).getReg().asMCReg();
  728. // Unlike forward cp, we don't invoke propagateDefs here,
  729. // just let forward cp do COPY-to-COPY propagation.
  730. if (isBackwardPropagatableCopy(MI, *MRI)) {
  731. Tracker.invalidateRegister(Src, *TRI);
  732. Tracker.invalidateRegister(Def, *TRI);
  733. Tracker.trackCopy(&MI, *TRI);
  734. continue;
  735. }
  736. }
  737. // Invalidate any earlyclobber regs first.
  738. for (const MachineOperand &MO : MI.operands())
  739. if (MO.isReg() && MO.isEarlyClobber()) {
  740. MCRegister Reg = MO.getReg().asMCReg();
  741. if (!Reg)
  742. continue;
  743. Tracker.invalidateRegister(Reg, *TRI);
  744. }
  745. propagateDefs(MI);
  746. for (const MachineOperand &MO : MI.operands()) {
  747. if (!MO.isReg())
  748. continue;
  749. if (!MO.getReg())
  750. continue;
  751. if (MO.isDef())
  752. Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
  753. if (MO.readsReg()) {
  754. if (MO.isDebug()) {
  755. // Check if the register in the debug instruction is utilized
  756. // in a copy instruction, so we can update the debug info if the
  757. // register is changed.
  758. for (MCRegUnitIterator RUI(MO.getReg().asMCReg(), TRI); RUI.isValid();
  759. ++RUI) {
  760. if (auto *Copy = Tracker.findCopyDefViaUnit(*RUI, *TRI)) {
  761. CopyDbgUsers[Copy].insert(&MI);
  762. }
  763. }
  764. } else {
  765. Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
  766. }
  767. }
  768. }
  769. }
  770. for (auto *Copy : MaybeDeadCopies) {
  771. Register Src = Copy->getOperand(1).getReg();
  772. Register Def = Copy->getOperand(0).getReg();
  773. SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(),
  774. CopyDbgUsers[Copy].end());
  775. MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers);
  776. Copy->eraseFromParent();
  777. ++NumDeletes;
  778. }
  779. MaybeDeadCopies.clear();
  780. CopyDbgUsers.clear();
  781. Tracker.clear();
  782. }
  783. bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
  784. if (skipFunction(MF.getFunction()))
  785. return false;
  786. Changed = false;
  787. TRI = MF.getSubtarget().getRegisterInfo();
  788. TII = MF.getSubtarget().getInstrInfo();
  789. MRI = &MF.getRegInfo();
  790. for (MachineBasicBlock &MBB : MF) {
  791. BackwardCopyPropagateBlock(MBB);
  792. ForwardCopyPropagateBlock(MBB);
  793. }
  794. return Changed;
  795. }