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