2
0

ProcessImplicitDefs.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/ADT/SetVector.h"
  10. #include "llvm/Analysis/AliasAnalysis.h"
  11. #include "llvm/CodeGen/MachineFunctionPass.h"
  12. #include "llvm/CodeGen/MachineInstr.h"
  13. #include "llvm/CodeGen/MachineRegisterInfo.h"
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include "llvm/Target/TargetInstrInfo.h"
  18. #include "llvm/Target/TargetSubtargetInfo.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "processimplicitdefs"
  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 &fn) override;
  38. };
  39. } // end anonymous namespace
  40. char ProcessImplicitDefs::ID = 0;
  41. char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
  42. INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
  43. "Process Implicit Definitions", false, false)
  44. INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
  45. "Process Implicit Definitions", false, false)
  46. void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
  47. AU.setPreservesCFG();
  48. AU.addPreserved<AliasAnalysis>();
  49. MachineFunctionPass::getAnalysisUsage(AU);
  50. }
  51. bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
  52. if (!MI->isCopyLike() &&
  53. !MI->isInsertSubreg() &&
  54. !MI->isRegSequence() &&
  55. !MI->isPHI())
  56. return false;
  57. for (const MachineOperand &MO : MI->operands())
  58. if (MO.isReg() && MO.isUse() && MO.readsReg())
  59. return false;
  60. return true;
  61. }
  62. void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
  63. DEBUG(dbgs() << "Processing " << *MI);
  64. unsigned Reg = MI->getOperand(0).getReg();
  65. if (TargetRegisterInfo::isVirtualRegister(Reg)) {
  66. // For virtual registers, mark all uses as <undef>, and convert users to
  67. // implicit-def when possible.
  68. for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
  69. MO.setIsUndef();
  70. MachineInstr *UserMI = MO.getParent();
  71. if (!canTurnIntoImplicitDef(UserMI))
  72. continue;
  73. DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
  74. UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  75. WorkList.insert(UserMI);
  76. }
  77. MI->eraseFromParent();
  78. return;
  79. }
  80. // This is a physreg implicit-def.
  81. // Look for the first instruction to use or define an alias.
  82. MachineBasicBlock::instr_iterator UserMI = MI;
  83. MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
  84. bool Found = false;
  85. for (++UserMI; UserMI != UserE; ++UserMI) {
  86. for (MachineOperand &MO : UserMI->operands()) {
  87. if (!MO.isReg())
  88. continue;
  89. unsigned UserReg = MO.getReg();
  90. if (!TargetRegisterInfo::isPhysicalRegister(UserReg) ||
  91. !TRI->regsOverlap(Reg, UserReg))
  92. continue;
  93. // UserMI uses or redefines Reg. Set <undef> flags on all uses.
  94. Found = true;
  95. if (MO.isUse())
  96. MO.setIsUndef();
  97. }
  98. if (Found)
  99. break;
  100. }
  101. // If we found the using MI, we can erase the IMPLICIT_DEF.
  102. if (Found) {
  103. DEBUG(dbgs() << "Physreg user: " << *UserMI);
  104. MI->eraseFromParent();
  105. return;
  106. }
  107. // Using instr wasn't found, it could be in another block.
  108. // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
  109. for (unsigned i = MI->getNumOperands() - 1; i; --i)
  110. MI->RemoveOperand(i);
  111. DEBUG(dbgs() << "Keeping physreg: " << *MI);
  112. }
  113. /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
  114. /// <undef> operands.
  115. bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
  116. DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
  117. << "********** Function: " << MF.getName() << '\n');
  118. bool Changed = false;
  119. TII = MF.getSubtarget().getInstrInfo();
  120. TRI = MF.getSubtarget().getRegisterInfo();
  121. MRI = &MF.getRegInfo();
  122. assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
  123. assert(WorkList.empty() && "Inconsistent worklist state");
  124. for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end();
  125. MFI != MFE; ++MFI) {
  126. // Scan the basic block for implicit defs.
  127. for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(),
  128. MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI)
  129. if (MBBI->isImplicitDef())
  130. WorkList.insert(MBBI);
  131. if (WorkList.empty())
  132. continue;
  133. DEBUG(dbgs() << "BB#" << MFI->getNumber() << " has " << WorkList.size()
  134. << " implicit defs.\n");
  135. Changed = true;
  136. // Drain the WorkList to recursively process any new implicit defs.
  137. do processImplicitDef(WorkList.pop_back_val());
  138. while (!WorkList.empty());
  139. }
  140. return Changed;
  141. }