ProcessImplicitDefs.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
  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. #include "llvm/ADT/SetVector.h"
  9. #include "llvm/Analysis/AliasAnalysis.h"
  10. #include "llvm/CodeGen/MachineFunctionPass.h"
  11. #include "llvm/CodeGen/MachineInstr.h"
  12. #include "llvm/CodeGen/MachineRegisterInfo.h"
  13. #include "llvm/CodeGen/Passes.h"
  14. #include "llvm/CodeGen/TargetInstrInfo.h"
  15. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "processimpdefs"
  21. namespace {
  22. /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
  23. /// for each use. Add isUndef marker to implicit_def defs and their uses.
  24. class ProcessImplicitDefs : public MachineFunctionPass {
  25. const TargetInstrInfo *TII;
  26. const TargetRegisterInfo *TRI;
  27. MachineRegisterInfo *MRI;
  28. SmallSetVector<MachineInstr*, 16> WorkList;
  29. void processImplicitDef(MachineInstr *MI);
  30. bool canTurnIntoImplicitDef(MachineInstr *MI);
  31. public:
  32. static char ID;
  33. ProcessImplicitDefs() : MachineFunctionPass(ID) {
  34. initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
  35. }
  36. void getAnalysisUsage(AnalysisUsage &au) const override;
  37. bool runOnMachineFunction(MachineFunction &MF) override;
  38. };
  39. } // end anonymous namespace
  40. char ProcessImplicitDefs::ID = 0;
  41. char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
  42. INITIALIZE_PASS(ProcessImplicitDefs, DEBUG_TYPE,
  43. "Process Implicit Definitions", false, false)
  44. void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
  45. AU.setPreservesCFG();
  46. AU.addPreserved<AAResultsWrapperPass>();
  47. MachineFunctionPass::getAnalysisUsage(AU);
  48. }
  49. bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
  50. if (!MI->isCopyLike() &&
  51. !MI->isInsertSubreg() &&
  52. !MI->isRegSequence() &&
  53. !MI->isPHI())
  54. return false;
  55. for (const MachineOperand &MO : MI->operands())
  56. if (MO.isReg() && MO.isUse() && MO.readsReg())
  57. return false;
  58. return true;
  59. }
  60. void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
  61. LLVM_DEBUG(dbgs() << "Processing " << *MI);
  62. Register Reg = MI->getOperand(0).getReg();
  63. if (Register::isVirtualRegister(Reg)) {
  64. // For virtual registers, mark all uses as <undef>, and convert users to
  65. // implicit-def when possible.
  66. for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
  67. MO.setIsUndef();
  68. MachineInstr *UserMI = MO.getParent();
  69. if (!canTurnIntoImplicitDef(UserMI))
  70. continue;
  71. LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
  72. UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  73. WorkList.insert(UserMI);
  74. }
  75. MI->eraseFromParent();
  76. return;
  77. }
  78. // This is a physreg implicit-def.
  79. // Look for the first instruction to use or define an alias.
  80. MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
  81. MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
  82. bool Found = false;
  83. for (++UserMI; UserMI != UserE; ++UserMI) {
  84. for (MachineOperand &MO : UserMI->operands()) {
  85. if (!MO.isReg())
  86. continue;
  87. Register UserReg = MO.getReg();
  88. if (!Register::isPhysicalRegister(UserReg) ||
  89. !TRI->regsOverlap(Reg, UserReg))
  90. continue;
  91. // UserMI uses or redefines Reg. Set <undef> flags on all uses.
  92. Found = true;
  93. if (MO.isUse())
  94. MO.setIsUndef();
  95. }
  96. if (Found)
  97. break;
  98. }
  99. // If we found the using MI, we can erase the IMPLICIT_DEF.
  100. if (Found) {
  101. LLVM_DEBUG(dbgs() << "Physreg user: " << *UserMI);
  102. MI->eraseFromParent();
  103. return;
  104. }
  105. // Using instr wasn't found, it could be in another block.
  106. // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
  107. for (unsigned i = MI->getNumOperands() - 1; i; --i)
  108. MI->RemoveOperand(i);
  109. LLVM_DEBUG(dbgs() << "Keeping physreg: " << *MI);
  110. }
  111. /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
  112. /// <undef> operands.
  113. bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
  114. LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
  115. << "********** Function: " << MF.getName() << '\n');
  116. bool Changed = false;
  117. TII = MF.getSubtarget().getInstrInfo();
  118. TRI = MF.getSubtarget().getRegisterInfo();
  119. MRI = &MF.getRegInfo();
  120. assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
  121. assert(WorkList.empty() && "Inconsistent worklist state");
  122. for (MachineBasicBlock &MBB : MF) {
  123. // Scan the basic block for implicit defs.
  124. for (MachineInstr &MI : MBB)
  125. if (MI.isImplicitDef())
  126. WorkList.insert(&MI);
  127. if (WorkList.empty())
  128. continue;
  129. LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
  130. << " implicit defs.\n");
  131. Changed = true;
  132. // Drain the WorkList to recursively process any new implicit defs.
  133. do processImplicitDef(WorkList.pop_back_val());
  134. while (!WorkList.empty());
  135. }
  136. return Changed;
  137. }