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