Localizer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===- Localizer.cpp ---------------------- Localize some instrs -*- C++ -*-==//
  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. /// \file
  10. /// This file implements the Localizer class.
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/GlobalISel/Localizer.h"
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/CodeGen/MachineRegisterInfo.h"
  16. #include "llvm/Support/Debug.h"
  17. #define DEBUG_TYPE "localizer"
  18. using namespace llvm;
  19. char Localizer::ID = 0;
  20. INITIALIZE_PASS(Localizer, DEBUG_TYPE,
  21. "Move/duplicate certain instructions close to their use", false,
  22. false)
  23. Localizer::Localizer() : MachineFunctionPass(ID) {
  24. initializeLocalizerPass(*PassRegistry::getPassRegistry());
  25. }
  26. void Localizer::init(MachineFunction &MF) { MRI = &MF.getRegInfo(); }
  27. bool Localizer::shouldLocalize(const MachineInstr &MI) {
  28. switch (MI.getOpcode()) {
  29. default:
  30. return false;
  31. // Constants-like instructions should be close to their users.
  32. // We don't want long live-ranges for them.
  33. case TargetOpcode::G_CONSTANT:
  34. case TargetOpcode::G_FCONSTANT:
  35. case TargetOpcode::G_FRAME_INDEX:
  36. return true;
  37. }
  38. }
  39. void Localizer::getAnalysisUsage(AnalysisUsage &AU) const {
  40. getSelectionDAGFallbackAnalysisUsage(AU);
  41. MachineFunctionPass::getAnalysisUsage(AU);
  42. }
  43. bool Localizer::isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
  44. MachineBasicBlock *&InsertMBB) {
  45. MachineInstr &MIUse = *MOUse.getParent();
  46. InsertMBB = MIUse.getParent();
  47. if (MIUse.isPHI())
  48. InsertMBB = MIUse.getOperand(MIUse.getOperandNo(&MOUse) + 1).getMBB();
  49. return InsertMBB == Def.getParent();
  50. }
  51. bool Localizer::runOnMachineFunction(MachineFunction &MF) {
  52. // If the ISel pipeline failed, do not bother running that pass.
  53. if (MF.getProperties().hasProperty(
  54. MachineFunctionProperties::Property::FailedISel))
  55. return false;
  56. LLVM_DEBUG(dbgs() << "Localize instructions for: " << MF.getName() << '\n');
  57. init(MF);
  58. bool Changed = false;
  59. // Keep track of the instructions we localized.
  60. // We won't need to process them if we see them later in the CFG.
  61. SmallPtrSet<MachineInstr *, 16> LocalizedInstrs;
  62. DenseMap<std::pair<MachineBasicBlock *, unsigned>, unsigned> MBBWithLocalDef;
  63. // TODO: Do bottom up traversal.
  64. for (MachineBasicBlock &MBB : MF) {
  65. for (MachineInstr &MI : MBB) {
  66. if (LocalizedInstrs.count(&MI) || !shouldLocalize(MI))
  67. continue;
  68. LLVM_DEBUG(dbgs() << "Should localize: " << MI);
  69. assert(MI.getDesc().getNumDefs() == 1 &&
  70. "More than one definition not supported yet");
  71. unsigned Reg = MI.getOperand(0).getReg();
  72. // Check if all the users of MI are local.
  73. // We are going to invalidation the list of use operands, so we
  74. // can't use range iterator.
  75. for (auto MOIt = MRI->use_begin(Reg), MOItEnd = MRI->use_end();
  76. MOIt != MOItEnd;) {
  77. MachineOperand &MOUse = *MOIt++;
  78. // Check if the use is already local.
  79. MachineBasicBlock *InsertMBB;
  80. LLVM_DEBUG(MachineInstr &MIUse = *MOUse.getParent();
  81. dbgs() << "Checking use: " << MIUse
  82. << " #Opd: " << MIUse.getOperandNo(&MOUse) << '\n');
  83. if (isLocalUse(MOUse, MI, InsertMBB))
  84. continue;
  85. LLVM_DEBUG(dbgs() << "Fixing non-local use\n");
  86. Changed = true;
  87. auto MBBAndReg = std::make_pair(InsertMBB, Reg);
  88. auto NewVRegIt = MBBWithLocalDef.find(MBBAndReg);
  89. if (NewVRegIt == MBBWithLocalDef.end()) {
  90. // Create the localized instruction.
  91. MachineInstr *LocalizedMI = MF.CloneMachineInstr(&MI);
  92. LocalizedInstrs.insert(LocalizedMI);
  93. // Don't try to be smart for the insertion point.
  94. // There is no guarantee that the first seen use is the first
  95. // use in the block.
  96. InsertMBB->insert(InsertMBB->SkipPHIsAndLabels(InsertMBB->begin()),
  97. LocalizedMI);
  98. // Set a new register for the definition.
  99. unsigned NewReg =
  100. MRI->createGenericVirtualRegister(MRI->getType(Reg));
  101. MRI->setRegClassOrRegBank(NewReg, MRI->getRegClassOrRegBank(Reg));
  102. LocalizedMI->getOperand(0).setReg(NewReg);
  103. NewVRegIt =
  104. MBBWithLocalDef.insert(std::make_pair(MBBAndReg, NewReg)).first;
  105. LLVM_DEBUG(dbgs() << "Inserted: " << *LocalizedMI);
  106. }
  107. LLVM_DEBUG(dbgs() << "Update use with: " << printReg(NewVRegIt->second)
  108. << '\n');
  109. // Update the user reg.
  110. MOUse.setReg(NewVRegIt->second);
  111. }
  112. }
  113. }
  114. return Changed;
  115. }