MacroFusion.cpp 6.3 KB

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