Combiner.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //===-- lib/CodeGen/GlobalISel/Combiner.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. //
  9. // This file constains common code to combine machine functions at generic
  10. // level.
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/GlobalISel/Combiner.h"
  13. #include "llvm/ADT/PostOrderIterator.h"
  14. #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
  15. #include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
  16. #include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h"
  17. #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
  18. #include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
  19. #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
  20. #include "llvm/CodeGen/GlobalISel/Utils.h"
  21. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/Support/Debug.h"
  24. #define DEBUG_TYPE "gi-combiner"
  25. using namespace llvm;
  26. namespace llvm {
  27. cl::OptionCategory GICombinerOptionCategory(
  28. "GlobalISel Combiner",
  29. "Control the rules which are enabled. These options all take a comma "
  30. "separated list of rules to disable and may be specified by number "
  31. "or number range (e.g. 1-10)."
  32. #ifndef NDEBUG
  33. " They may also be specified by name."
  34. #endif
  35. );
  36. } // end namespace llvm
  37. namespace {
  38. /// This class acts as the glue the joins the CombinerHelper to the overall
  39. /// Combine algorithm. The CombinerHelper is intended to report the
  40. /// modifications it makes to the MIR to the GISelChangeObserver and the
  41. /// observer subclass will act on these events. In this case, instruction
  42. /// erasure will cancel any future visits to the erased instruction and
  43. /// instruction creation will schedule that instruction for a future visit.
  44. /// Other Combiner implementations may require more complex behaviour from
  45. /// their GISelChangeObserver subclass.
  46. class WorkListMaintainer : public GISelChangeObserver {
  47. using WorkListTy = GISelWorkList<512>;
  48. WorkListTy &WorkList;
  49. /// The instructions that have been created but we want to report once they
  50. /// have their operands. This is only maintained if debug output is requested.
  51. SmallPtrSet<const MachineInstr *, 4> CreatedInstrs;
  52. public:
  53. WorkListMaintainer(WorkListTy &WorkList) : WorkList(WorkList) {}
  54. virtual ~WorkListMaintainer() {
  55. }
  56. void erasingInstr(MachineInstr &MI) override {
  57. LLVM_DEBUG(dbgs() << "Erasing: " << MI << "\n");
  58. WorkList.remove(&MI);
  59. }
  60. void createdInstr(MachineInstr &MI) override {
  61. LLVM_DEBUG(dbgs() << "Creating: " << MI << "\n");
  62. WorkList.insert(&MI);
  63. LLVM_DEBUG(CreatedInstrs.insert(&MI));
  64. }
  65. void changingInstr(MachineInstr &MI) override {
  66. LLVM_DEBUG(dbgs() << "Changing: " << MI << "\n");
  67. WorkList.insert(&MI);
  68. }
  69. void changedInstr(MachineInstr &MI) override {
  70. LLVM_DEBUG(dbgs() << "Changed: " << MI << "\n");
  71. WorkList.insert(&MI);
  72. }
  73. void reportFullyCreatedInstrs() {
  74. LLVM_DEBUG(for (const auto *MI
  75. : CreatedInstrs) {
  76. dbgs() << "Created: ";
  77. MI->print(dbgs());
  78. });
  79. LLVM_DEBUG(CreatedInstrs.clear());
  80. }
  81. };
  82. }
  83. Combiner::Combiner(CombinerInfo &Info, const TargetPassConfig *TPC)
  84. : CInfo(Info), TPC(TPC) {
  85. (void)this->TPC; // FIXME: Remove when used.
  86. }
  87. bool Combiner::combineMachineInstrs(MachineFunction &MF,
  88. GISelCSEInfo *CSEInfo) {
  89. // If the ISel pipeline failed, do not bother running this pass.
  90. // FIXME: Should this be here or in individual combiner passes.
  91. if (MF.getProperties().hasProperty(
  92. MachineFunctionProperties::Property::FailedISel))
  93. return false;
  94. Builder =
  95. CSEInfo ? std::make_unique<CSEMIRBuilder>() : std::make_unique<MachineIRBuilder>();
  96. MRI = &MF.getRegInfo();
  97. Builder->setMF(MF);
  98. if (CSEInfo)
  99. Builder->setCSEInfo(CSEInfo);
  100. LLVM_DEBUG(dbgs() << "Generic MI Combiner for: " << MF.getName() << '\n');
  101. MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
  102. bool MFChanged = false;
  103. bool Changed;
  104. MachineIRBuilder &B = *Builder.get();
  105. do {
  106. // Collect all instructions. Do a post order traversal for basic blocks and
  107. // insert with list bottom up, so while we pop_back_val, we'll traverse top
  108. // down RPOT.
  109. Changed = false;
  110. GISelWorkList<512> WorkList;
  111. WorkListMaintainer Observer(WorkList);
  112. GISelObserverWrapper WrapperObserver(&Observer);
  113. if (CSEInfo)
  114. WrapperObserver.addObserver(CSEInfo);
  115. RAIIDelegateInstaller DelInstall(MF, &WrapperObserver);
  116. for (MachineBasicBlock *MBB : post_order(&MF)) {
  117. for (MachineInstr &CurMI :
  118. llvm::make_early_inc_range(llvm::reverse(*MBB))) {
  119. // Erase dead insts before even adding to the list.
  120. if (isTriviallyDead(CurMI, *MRI)) {
  121. LLVM_DEBUG(dbgs() << CurMI << "Is dead; erasing.\n");
  122. CurMI.eraseFromParent();
  123. continue;
  124. }
  125. WorkList.deferred_insert(&CurMI);
  126. }
  127. }
  128. WorkList.finalize();
  129. // Main Loop. Process the instructions here.
  130. while (!WorkList.empty()) {
  131. MachineInstr *CurrInst = WorkList.pop_back_val();
  132. LLVM_DEBUG(dbgs() << "\nTry combining " << *CurrInst;);
  133. Changed |= CInfo.combine(WrapperObserver, *CurrInst, B);
  134. Observer.reportFullyCreatedInstrs();
  135. }
  136. MFChanged |= Changed;
  137. } while (Changed);
  138. #ifndef NDEBUG
  139. if (CSEInfo) {
  140. if (auto E = CSEInfo->verify()) {
  141. errs() << E << '\n';
  142. assert(false && "CSEInfo is not consistent. Likely missing calls to "
  143. "observer on mutations.");
  144. }
  145. }
  146. #endif
  147. return MFChanged;
  148. }