ExpandPostRAPseudos.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion 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 file defines a pass that expands COPY and SUBREG_TO_REG pseudo
  10. // instructions after register allocation.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachineFunctionPass.h"
  14. #include "llvm/CodeGen/MachineInstr.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/CodeGen/TargetInstrInfo.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  19. #include "llvm/InitializePasses.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "postrapseudos"
  24. namespace {
  25. struct ExpandPostRA : public MachineFunctionPass {
  26. private:
  27. const TargetRegisterInfo *TRI;
  28. const TargetInstrInfo *TII;
  29. public:
  30. static char ID; // Pass identification, replacement for typeid
  31. ExpandPostRA() : MachineFunctionPass(ID) {}
  32. void getAnalysisUsage(AnalysisUsage &AU) const override {
  33. AU.setPreservesCFG();
  34. AU.addPreservedID(MachineLoopInfoID);
  35. AU.addPreservedID(MachineDominatorsID);
  36. MachineFunctionPass::getAnalysisUsage(AU);
  37. }
  38. /// runOnMachineFunction - pass entry point
  39. bool runOnMachineFunction(MachineFunction&) override;
  40. private:
  41. bool LowerSubregToReg(MachineInstr *MI);
  42. bool LowerCopy(MachineInstr *MI);
  43. void TransferImplicitOperands(MachineInstr *MI);
  44. };
  45. } // end anonymous namespace
  46. char ExpandPostRA::ID = 0;
  47. char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
  48. INITIALIZE_PASS(ExpandPostRA, DEBUG_TYPE,
  49. "Post-RA pseudo instruction expansion pass", false, false)
  50. /// TransferImplicitOperands - MI is a pseudo-instruction, and the lowered
  51. /// replacement instructions immediately precede it. Copy any implicit
  52. /// operands from MI to the replacement instruction.
  53. void ExpandPostRA::TransferImplicitOperands(MachineInstr *MI) {
  54. MachineBasicBlock::iterator CopyMI = MI;
  55. --CopyMI;
  56. Register DstReg = MI->getOperand(0).getReg();
  57. for (const MachineOperand &MO : MI->implicit_operands()) {
  58. CopyMI->addOperand(MO);
  59. // Be conservative about preserving kills when subregister defs are
  60. // involved. If there was implicit kill of a super-register overlapping the
  61. // copy result, we would kill the subregisters previous copies defined.
  62. if (MO.isKill() && TRI->regsOverlap(DstReg, MO.getReg()))
  63. CopyMI->getOperand(CopyMI->getNumOperands() - 1).setIsKill(false);
  64. }
  65. }
  66. bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
  67. MachineBasicBlock *MBB = MI->getParent();
  68. assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
  69. MI->getOperand(1).isImm() &&
  70. (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
  71. MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
  72. Register DstReg = MI->getOperand(0).getReg();
  73. Register InsReg = MI->getOperand(2).getReg();
  74. assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
  75. unsigned SubIdx = MI->getOperand(3).getImm();
  76. assert(SubIdx != 0 && "Invalid index for insert_subreg");
  77. Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
  78. assert(DstReg.isPhysical() &&
  79. "Insert destination must be in a physical register");
  80. assert(InsReg.isPhysical() &&
  81. "Inserted value must be in a physical register");
  82. LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
  83. if (MI->allDefsAreDead()) {
  84. MI->setDesc(TII->get(TargetOpcode::KILL));
  85. MI->removeOperand(3); // SubIdx
  86. MI->removeOperand(1); // Imm
  87. LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
  88. return true;
  89. }
  90. if (DstSubReg == InsReg) {
  91. // No need to insert an identity copy instruction.
  92. // Watch out for case like this:
  93. // %rax = SUBREG_TO_REG 0, killed %eax, 3
  94. // We must leave %rax live.
  95. if (DstReg != InsReg) {
  96. MI->setDesc(TII->get(TargetOpcode::KILL));
  97. MI->removeOperand(3); // SubIdx
  98. MI->removeOperand(1); // Imm
  99. LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
  100. return true;
  101. }
  102. LLVM_DEBUG(dbgs() << "subreg: eliminated!");
  103. } else {
  104. TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
  105. MI->getOperand(2).isKill());
  106. // Implicitly define DstReg for subsequent uses.
  107. MachineBasicBlock::iterator CopyMI = MI;
  108. --CopyMI;
  109. CopyMI->addRegisterDefined(DstReg);
  110. LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
  111. }
  112. LLVM_DEBUG(dbgs() << '\n');
  113. MBB->erase(MI);
  114. return true;
  115. }
  116. bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
  117. if (MI->allDefsAreDead()) {
  118. LLVM_DEBUG(dbgs() << "dead copy: " << *MI);
  119. MI->setDesc(TII->get(TargetOpcode::KILL));
  120. LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
  121. return true;
  122. }
  123. MachineOperand &DstMO = MI->getOperand(0);
  124. MachineOperand &SrcMO = MI->getOperand(1);
  125. bool IdentityCopy = (SrcMO.getReg() == DstMO.getReg());
  126. if (IdentityCopy || SrcMO.isUndef()) {
  127. LLVM_DEBUG(dbgs() << (IdentityCopy ? "identity copy: " : "undef copy: ")
  128. << *MI);
  129. // No need to insert an identity copy instruction, but replace with a KILL
  130. // if liveness is changed.
  131. if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
  132. // We must make sure the super-register gets killed. Replace the
  133. // instruction with KILL.
  134. MI->setDesc(TII->get(TargetOpcode::KILL));
  135. LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
  136. return true;
  137. }
  138. // Vanilla identity copy.
  139. MI->eraseFromParent();
  140. return true;
  141. }
  142. LLVM_DEBUG(dbgs() << "real copy: " << *MI);
  143. TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
  144. DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
  145. if (MI->getNumOperands() > 2)
  146. TransferImplicitOperands(MI);
  147. LLVM_DEBUG({
  148. MachineBasicBlock::iterator dMI = MI;
  149. dbgs() << "replaced by: " << *(--dMI);
  150. });
  151. MI->eraseFromParent();
  152. return true;
  153. }
  154. /// runOnMachineFunction - Reduce subregister inserts and extracts to register
  155. /// copies.
  156. ///
  157. bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
  158. LLVM_DEBUG(dbgs() << "Machine Function\n"
  159. << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
  160. << "********** Function: " << MF.getName() << '\n');
  161. TRI = MF.getSubtarget().getRegisterInfo();
  162. TII = MF.getSubtarget().getInstrInfo();
  163. bool MadeChange = false;
  164. for (MachineBasicBlock &MBB : MF) {
  165. for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
  166. // Only expand pseudos.
  167. if (!MI.isPseudo())
  168. continue;
  169. // Give targets a chance to expand even standard pseudos.
  170. if (TII->expandPostRAPseudo(MI)) {
  171. MadeChange = true;
  172. continue;
  173. }
  174. // Expand standard pseudos.
  175. switch (MI.getOpcode()) {
  176. case TargetOpcode::SUBREG_TO_REG:
  177. MadeChange |= LowerSubregToReg(&MI);
  178. break;
  179. case TargetOpcode::COPY:
  180. MadeChange |= LowerCopy(&MI);
  181. break;
  182. case TargetOpcode::DBG_VALUE:
  183. continue;
  184. case TargetOpcode::INSERT_SUBREG:
  185. case TargetOpcode::EXTRACT_SUBREG:
  186. llvm_unreachable("Sub-register pseudos should have been eliminated.");
  187. }
  188. }
  189. }
  190. return MadeChange;
  191. }