ExpandPostRAPseudos.cpp 7.5 KB

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