ProcessImplicitDefs.cpp 5.4 KB

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