MacroFusion.cpp 7.5 KB

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