ReduceInstructionsMIR.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===- ReduceInstructionsMIR.cpp - Specialized Delta Pass -----------------===//
  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. //
  9. // This file implements a function which calls the Generic Delta pass in order
  10. // to reduce uninteresting MachineInstr from the MachineFunction.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ReduceInstructionsMIR.h"
  14. #include "llvm/CodeGen/MachineDominators.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/MachineRegisterInfo.h"
  18. #include "llvm/CodeGen/TargetInstrInfo.h"
  19. using namespace llvm;
  20. static Register getPrevDefOfRCInMBB(MachineBasicBlock &MBB,
  21. MachineBasicBlock::reverse_iterator &RI,
  22. const TargetRegisterClass *RC,
  23. SetVector<MachineInstr *> &ExcludeMIs) {
  24. auto MRI = &MBB.getParent()->getRegInfo();
  25. for (MachineBasicBlock::reverse_instr_iterator E = MBB.instr_rend(); RI != E;
  26. ++RI) {
  27. auto &MI = *RI;
  28. // All Def operands explicit and implicit.
  29. for (auto &MO : MI.operands()) {
  30. if (!MO.isReg() || !MO.isDef())
  31. continue;
  32. auto Reg = MO.getReg();
  33. if (Register::isPhysicalRegister(Reg))
  34. continue;
  35. if (MRI->getRegClass(Reg) == RC && !ExcludeMIs.count(MO.getParent()))
  36. return Reg;
  37. }
  38. }
  39. return 0;
  40. }
  41. static void extractInstrFromModule(Oracle &O, MachineFunction &MF) {
  42. MachineDominatorTree MDT;
  43. MDT.runOnMachineFunction(MF);
  44. auto MRI = &MF.getRegInfo();
  45. SetVector<MachineInstr *> ToDelete;
  46. MachineInstr *TopMI = nullptr;
  47. // Mark MIs for deletion according to some criteria.
  48. for (auto &MBB : MF) {
  49. for (auto &MI : MBB) {
  50. if (MI.isTerminator())
  51. continue;
  52. if (MBB.isEntryBlock() && !TopMI) {
  53. TopMI = &MI;
  54. continue;
  55. }
  56. if (!O.shouldKeep())
  57. ToDelete.insert(&MI);
  58. }
  59. }
  60. // For each MI to be deleted update users of regs defined by that MI to use
  61. // some other dominating definition (that is not to be deleted).
  62. for (auto *MI : ToDelete) {
  63. for (auto &MO : MI->operands()) {
  64. if (!MO.isReg() || !MO.isDef())
  65. continue;
  66. auto Reg = MO.getReg();
  67. if (Register::isPhysicalRegister(Reg))
  68. continue;
  69. auto UI = MRI->use_begin(Reg);
  70. auto UE = MRI->use_end();
  71. auto RegRC = MRI->getRegClass(Reg);
  72. Register NewReg = 0;
  73. // If this is not a physical register and there are some uses.
  74. if (UI != UE) {
  75. MachineBasicBlock::reverse_iterator RI(*MI);
  76. MachineBasicBlock *BB = MI->getParent();
  77. ++RI;
  78. while (NewReg == 0 && BB) {
  79. NewReg = getPrevDefOfRCInMBB(*BB, RI, RegRC, ToDelete);
  80. // Prepare for idom(BB).
  81. if (auto *IDM = MDT.getNode(BB)->getIDom()) {
  82. BB = IDM->getBlock();
  83. RI = BB->rbegin();
  84. } else {
  85. BB = nullptr;
  86. }
  87. }
  88. }
  89. // If no dominating definition was found then add an implicit one to the
  90. // first instruction in the entry block.
  91. if (!NewReg && TopMI) {
  92. NewReg = MRI->createVirtualRegister(RegRC);
  93. TopMI->addOperand(MachineOperand::CreateReg(
  94. NewReg, true /*IsDef*/, true /*IsImp*/, false /*IsKill*/));
  95. }
  96. // Update all uses.
  97. while (UI != UE) {
  98. auto &UMO = *UI++;
  99. UMO.setReg(NewReg);
  100. }
  101. }
  102. }
  103. // Finally delete the MIs.
  104. for (auto *MI : ToDelete)
  105. MI->eraseFromParent();
  106. }
  107. void llvm::reduceInstructionsMIRDeltaPass(TestRunner &Test) {
  108. outs() << "*** Reducing Instructions...\n";
  109. runDeltaPass(Test, extractInstrFromModule);
  110. }