MacroFusion.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
  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. /// \file This file contains the implementation of the DAG scheduling mutation
  10. /// to pair instructions back to back.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MacroFusion.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/CodeGen/MachineInstr.h"
  17. #include "llvm/CodeGen/MachineScheduler.h"
  18. #include "llvm/CodeGen/ScheduleDAG.h"
  19. #include "llvm/CodeGen/ScheduleDAGMutation.h"
  20. #include "llvm/CodeGen/TargetInstrInfo.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #define DEBUG_TYPE "machine-scheduler"
  25. STATISTIC(NumFused, "Number of instr pairs fused");
  26. using namespace llvm;
  27. static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
  28. cl::desc("Enable scheduling for macro fusion."), cl::init(true));
  29. static bool isHazard(const SDep &Dep) {
  30. return Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output;
  31. }
  32. static bool fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU,
  33. SUnit &SecondSU) {
  34. // Check that neither instr is already paired with another along the edge
  35. // between them.
  36. for (SDep &SI : FirstSU.Succs)
  37. if (SI.isCluster())
  38. return false;
  39. for (SDep &SI : SecondSU.Preds)
  40. if (SI.isCluster())
  41. return false;
  42. // Though the reachability checks above could be made more generic,
  43. // perhaps as part of ScheduleDAGInstrs::addEdge(), since such edges are valid,
  44. // the extra computation cost makes it less interesting in general cases.
  45. // Create a single weak edge between the adjacent instrs. The only effect is
  46. // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
  47. if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
  48. return false;
  49. // Adjust the latency between both instrs.
  50. for (SDep &SI : FirstSU.Succs)
  51. if (SI.getSUnit() == &SecondSU)
  52. SI.setLatency(0);
  53. for (SDep &SI : SecondSU.Preds)
  54. if (SI.getSUnit() == &FirstSU)
  55. SI.setLatency(0);
  56. LLVM_DEBUG(
  57. dbgs() << "Macro fuse: "; DAG.dumpNodeName(FirstSU); dbgs() << " - ";
  58. DAG.dumpNodeName(SecondSU); dbgs() << " / ";
  59. dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - "
  60. << DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n';);
  61. // Make data dependencies from the FirstSU also dependent on the SecondSU to
  62. // prevent them from being scheduled between the FirstSU and the SecondSU.
  63. if (&SecondSU != &DAG.ExitSU)
  64. for (const SDep &SI : FirstSU.Succs) {
  65. SUnit *SU = SI.getSUnit();
  66. if (SI.isWeak() || isHazard(SI) ||
  67. SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
  68. continue;
  69. LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(SecondSU);
  70. dbgs() << " - "; DAG.dumpNodeName(*SU); dbgs() << '\n';);
  71. DAG.addEdge(SU, SDep(&SecondSU, SDep::Artificial));
  72. }
  73. // Make the FirstSU also dependent on the dependencies of the SecondSU to
  74. // prevent them from being scheduled between the FirstSU and the SecondSU.
  75. if (&FirstSU != &DAG.EntrySU) {
  76. for (const SDep &SI : SecondSU.Preds) {
  77. SUnit *SU = SI.getSUnit();
  78. if (SI.isWeak() || isHazard(SI) || &FirstSU == SU || FirstSU.isSucc(SU))
  79. continue;
  80. LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(*SU); dbgs() << " - ";
  81. DAG.dumpNodeName(FirstSU); dbgs() << '\n';);
  82. DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
  83. }
  84. // ExitSU comes last by design, which acts like an implicit dependency
  85. // between ExitSU and any bottom root in the graph. We should transfer
  86. // this to FirstSU as well.
  87. if (&SecondSU == &DAG.ExitSU) {
  88. for (SUnit &SU : DAG.SUnits) {
  89. if (SU.Succs.empty())
  90. DAG.addEdge(&FirstSU, SDep(&SU, SDep::Artificial));
  91. }
  92. }
  93. }
  94. ++NumFused;
  95. return true;
  96. }
  97. namespace {
  98. /// Post-process the DAG to create cluster edges between instrs that may
  99. /// be fused by the processor into a single operation.
  100. class MacroFusion : public ScheduleDAGMutation {
  101. ShouldSchedulePredTy shouldScheduleAdjacent;
  102. bool FuseBlock;
  103. bool scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU);
  104. public:
  105. MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
  106. : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
  107. void apply(ScheduleDAGInstrs *DAGInstrs) override;
  108. };
  109. } // end anonymous namespace
  110. void MacroFusion::apply(ScheduleDAGInstrs *DAG) {
  111. if (FuseBlock)
  112. // For each of the SUnits in the scheduling block, try to fuse the instr in
  113. // it with one in its predecessors.
  114. for (SUnit &ISU : DAG->SUnits)
  115. scheduleAdjacentImpl(*DAG, ISU);
  116. if (DAG->ExitSU.getInstr())
  117. // Try to fuse the instr in the ExitSU with one in its predecessors.
  118. scheduleAdjacentImpl(*DAG, DAG->ExitSU);
  119. }
  120. /// Implement the fusion of instr pairs in the scheduling DAG,
  121. /// anchored at the instr in AnchorSU..
  122. bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU) {
  123. const MachineInstr &AnchorMI = *AnchorSU.getInstr();
  124. const TargetInstrInfo &TII = *DAG.TII;
  125. const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
  126. // Check if the anchor instr may be fused.
  127. if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
  128. return false;
  129. // Explorer for fusion candidates among the dependencies of the anchor instr.
  130. for (SDep &Dep : AnchorSU.Preds) {
  131. // Ignore dependencies other than data or strong ordering.
  132. if (Dep.isWeak() || isHazard(Dep))
  133. continue;
  134. SUnit &DepSU = *Dep.getSUnit();
  135. if (DepSU.isBoundaryNode())
  136. continue;
  137. const MachineInstr *DepMI = DepSU.getInstr();
  138. if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
  139. continue;
  140. if (fuseInstructionPair(DAG, DepSU, AnchorSU))
  141. return true;
  142. }
  143. return false;
  144. }
  145. std::unique_ptr<ScheduleDAGMutation>
  146. llvm::createMacroFusionDAGMutation(
  147. ShouldSchedulePredTy shouldScheduleAdjacent) {
  148. if(EnableMacroFusion)
  149. return std::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
  150. return nullptr;
  151. }
  152. std::unique_ptr<ScheduleDAGMutation>
  153. llvm::createBranchMacroFusionDAGMutation(
  154. ShouldSchedulePredTy shouldScheduleAdjacent) {
  155. if(EnableMacroFusion)
  156. return std::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
  157. return nullptr;
  158. }