ProcessImplicitDefs.cpp 5.3 KB

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