ExpandPostRAPseudos.cpp 7.2 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/MachineInstrBuilder.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/CodeGen/TargetInstrInfo.h"
  19. #include "llvm/CodeGen/TargetRegisterInfo.h"
  20. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace llvm;
  24. #define DEBUG_TYPE "postrapseudos"
  25. namespace {
  26. struct ExpandPostRA : public MachineFunctionPass {
  27. private:
  28. const TargetRegisterInfo *TRI;
  29. const TargetInstrInfo *TII;
  30. public:
  31. static char ID; // Pass identification, replacement for typeid
  32. ExpandPostRA() : MachineFunctionPass(ID) {}
  33. void getAnalysisUsage(AnalysisUsage &AU) const override {
  34. AU.setPreservesCFG();
  35. AU.addPreservedID(MachineLoopInfoID);
  36. AU.addPreservedID(MachineDominatorsID);
  37. MachineFunctionPass::getAnalysisUsage(AU);
  38. }
  39. /// runOnMachineFunction - pass entry point
  40. bool runOnMachineFunction(MachineFunction&) override;
  41. private:
  42. bool LowerSubregToReg(MachineInstr *MI);
  43. bool LowerCopy(MachineInstr *MI);
  44. void TransferImplicitOperands(MachineInstr *MI);
  45. };
  46. } // end anonymous namespace
  47. char ExpandPostRA::ID = 0;
  48. char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
  49. INITIALIZE_PASS(ExpandPostRA, DEBUG_TYPE,
  50. "Post-RA pseudo instruction expansion pass", false, false)
  51. /// TransferImplicitOperands - MI is a pseudo-instruction, and the lowered
  52. /// replacement instructions immediately precede it. Copy any implicit
  53. /// operands from MI to the replacement instruction.
  54. void ExpandPostRA::TransferImplicitOperands(MachineInstr *MI) {
  55. MachineBasicBlock::iterator CopyMI = MI;
  56. --CopyMI;
  57. for (const MachineOperand &MO : MI->implicit_operands())
  58. if (MO.isReg())
  59. CopyMI->addOperand(MO);
  60. }
  61. bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
  62. MachineBasicBlock *MBB = MI->getParent();
  63. assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
  64. MI->getOperand(1).isImm() &&
  65. (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
  66. MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
  67. Register DstReg = MI->getOperand(0).getReg();
  68. Register InsReg = MI->getOperand(2).getReg();
  69. assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
  70. unsigned SubIdx = MI->getOperand(3).getImm();
  71. assert(SubIdx != 0 && "Invalid index for insert_subreg");
  72. Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
  73. assert(Register::isPhysicalRegister(DstReg) &&
  74. "Insert destination must be in a physical register");
  75. assert(Register::isPhysicalRegister(InsReg) &&
  76. "Inserted value must be in a physical register");
  77. LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
  78. if (MI->allDefsAreDead()) {
  79. MI->setDesc(TII->get(TargetOpcode::KILL));
  80. MI->RemoveOperand(3); // SubIdx
  81. MI->RemoveOperand(1); // Imm
  82. LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
  83. return true;
  84. }
  85. if (DstSubReg == InsReg) {
  86. // No need to insert an identity copy instruction.
  87. // Watch out for case like this:
  88. // %rax = SUBREG_TO_REG 0, killed %eax, 3
  89. // We must leave %rax live.
  90. if (DstReg != InsReg) {
  91. MI->setDesc(TII->get(TargetOpcode::KILL));
  92. MI->RemoveOperand(3); // SubIdx
  93. MI->RemoveOperand(1); // Imm
  94. LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
  95. return true;
  96. }
  97. LLVM_DEBUG(dbgs() << "subreg: eliminated!");
  98. } else {
  99. TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
  100. MI->getOperand(2).isKill());
  101. // Implicitly define DstReg for subsequent uses.
  102. MachineBasicBlock::iterator CopyMI = MI;
  103. --CopyMI;
  104. CopyMI->addRegisterDefined(DstReg);
  105. LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
  106. }
  107. LLVM_DEBUG(dbgs() << '\n');
  108. MBB->erase(MI);
  109. return true;
  110. }
  111. bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
  112. if (MI->allDefsAreDead()) {
  113. LLVM_DEBUG(dbgs() << "dead copy: " << *MI);
  114. MI->setDesc(TII->get(TargetOpcode::KILL));
  115. LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
  116. return true;
  117. }
  118. MachineOperand &DstMO = MI->getOperand(0);
  119. MachineOperand &SrcMO = MI->getOperand(1);
  120. bool IdentityCopy = (SrcMO.getReg() == DstMO.getReg());
  121. if (IdentityCopy || SrcMO.isUndef()) {
  122. LLVM_DEBUG(dbgs() << (IdentityCopy ? "identity copy: " : "undef copy: ")
  123. << *MI);
  124. // No need to insert an identity copy instruction, but replace with a KILL
  125. // if liveness is changed.
  126. if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
  127. // We must make sure the super-register gets killed. Replace the
  128. // instruction with KILL.
  129. MI->setDesc(TII->get(TargetOpcode::KILL));
  130. LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
  131. return true;
  132. }
  133. // Vanilla identity copy.
  134. MI->eraseFromParent();
  135. return true;
  136. }
  137. LLVM_DEBUG(dbgs() << "real copy: " << *MI);
  138. TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
  139. DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
  140. if (MI->getNumOperands() > 2)
  141. TransferImplicitOperands(MI);
  142. LLVM_DEBUG({
  143. MachineBasicBlock::iterator dMI = MI;
  144. dbgs() << "replaced by: " << *(--dMI);
  145. });
  146. MI->eraseFromParent();
  147. return true;
  148. }
  149. /// runOnMachineFunction - Reduce subregister inserts and extracts to register
  150. /// copies.
  151. ///
  152. bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
  153. LLVM_DEBUG(dbgs() << "Machine Function\n"
  154. << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
  155. << "********** Function: " << MF.getName() << '\n');
  156. TRI = MF.getSubtarget().getRegisterInfo();
  157. TII = MF.getSubtarget().getInstrInfo();
  158. bool MadeChange = false;
  159. for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
  160. mbbi != mbbe; ++mbbi) {
  161. for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
  162. mi != me;) {
  163. MachineInstr &MI = *mi;
  164. // Advance iterator here because MI may be erased.
  165. ++mi;
  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. }