MacroFusion.cpp 7.6 KB

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