1 //===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// \file This file contains the implementation of the DAG scheduling mutation
10 /// to pair instructions back to back.
12 //===----------------------------------------------------------------------===//
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"
26 #define DEBUG_TYPE "machine-scheduler"
28 STATISTIC(NumFused
, "Number of instr pairs fused");
32 static cl::opt
<bool> EnableMacroFusion("misched-fusion", cl::Hidden
,
33 cl::desc("Enable scheduling for macro fusion."), cl::init(true));
35 static bool isHazard(const SDep
&Dep
) {
36 return Dep
.getKind() == SDep::Anti
|| Dep
.getKind() == SDep::Output
;
39 static SUnit
*getPredClusterSU(const SUnit
&SU
) {
40 for (const SDep
&SI
: SU
.Preds
)
47 static bool hasLessThanNumFused(const SUnit
&SU
, unsigned FuseLimit
) {
49 const SUnit
*CurrentSU
= &SU
;
50 while ((CurrentSU
= getPredClusterSU(*CurrentSU
)) && Num
< FuseLimit
) Num
++;
51 return Num
< FuseLimit
;
54 static bool fuseInstructionPair(ScheduleDAGInstrs
&DAG
, SUnit
&FirstSU
,
56 // Check that neither instr is already paired with another along the edge
58 for (SDep
&SI
: FirstSU
.Succs
)
62 for (SDep
&SI
: SecondSU
.Preds
)
65 // Though the reachability checks above could be made more generic,
66 // perhaps as part of ScheduleDAGInstrs::addEdge(), since such edges are valid,
67 // the extra computation cost makes it less interesting in general cases.
69 // Create a single weak edge between the adjacent instrs. The only effect is
70 // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
71 if (!DAG
.addEdge(&SecondSU
, SDep(&FirstSU
, SDep::Cluster
)))
74 // TODO - If we want to chain more than two instructions, we need to create
75 // artifical edges to make dependencies from the FirstSU also dependent
76 // on other chained instructions, and other chained instructions also
77 // dependent on the dependencies of the SecondSU, to prevent them from being
78 // scheduled into these chained instructions.
79 assert(hasLessThanNumFused(FirstSU
, 2) &&
80 "Currently we only support chaining together two instructions");
82 // Adjust the latency between both instrs.
83 for (SDep
&SI
: FirstSU
.Succs
)
84 if (SI
.getSUnit() == &SecondSU
)
87 for (SDep
&SI
: SecondSU
.Preds
)
88 if (SI
.getSUnit() == &FirstSU
)
92 dbgs() << "Macro fuse: "; DAG
.dumpNodeName(FirstSU
); dbgs() << " - ";
93 DAG
.dumpNodeName(SecondSU
); dbgs() << " / ";
94 dbgs() << DAG
.TII
->getName(FirstSU
.getInstr()->getOpcode()) << " - "
95 << DAG
.TII
->getName(SecondSU
.getInstr()->getOpcode()) << '\n';);
97 // Make data dependencies from the FirstSU also dependent on the SecondSU to
98 // prevent them from being scheduled between the FirstSU and the SecondSU.
99 if (&SecondSU
!= &DAG
.ExitSU
)
100 for (const SDep
&SI
: FirstSU
.Succs
) {
101 SUnit
*SU
= SI
.getSUnit();
102 if (SI
.isWeak() || isHazard(SI
) ||
103 SU
== &DAG
.ExitSU
|| SU
== &SecondSU
|| SU
->isPred(&SecondSU
))
105 LLVM_DEBUG(dbgs() << " Bind "; DAG
.dumpNodeName(SecondSU
);
106 dbgs() << " - "; DAG
.dumpNodeName(*SU
); dbgs() << '\n';);
107 DAG
.addEdge(SU
, SDep(&SecondSU
, SDep::Artificial
));
110 // Make the FirstSU also dependent on the dependencies of the SecondSU to
111 // prevent them from being scheduled between the FirstSU and the SecondSU.
112 if (&FirstSU
!= &DAG
.EntrySU
) {
113 for (const SDep
&SI
: SecondSU
.Preds
) {
114 SUnit
*SU
= SI
.getSUnit();
115 if (SI
.isWeak() || isHazard(SI
) || &FirstSU
== SU
|| FirstSU
.isSucc(SU
))
117 LLVM_DEBUG(dbgs() << " Bind "; DAG
.dumpNodeName(*SU
); dbgs() << " - ";
118 DAG
.dumpNodeName(FirstSU
); dbgs() << '\n';);
119 DAG
.addEdge(&FirstSU
, SDep(SU
, SDep::Artificial
));
121 // ExitSU comes last by design, which acts like an implicit dependency
122 // between ExitSU and any bottom root in the graph. We should transfer
123 // this to FirstSU as well.
124 if (&SecondSU
== &DAG
.ExitSU
) {
125 for (SUnit
&SU
: DAG
.SUnits
) {
126 if (SU
.Succs
.empty())
127 DAG
.addEdge(&FirstSU
, SDep(&SU
, SDep::Artificial
));
138 /// Post-process the DAG to create cluster edges between instrs that may
139 /// be fused by the processor into a single operation.
140 class MacroFusion
: public ScheduleDAGMutation
{
141 ShouldSchedulePredTy shouldScheduleAdjacent
;
143 bool scheduleAdjacentImpl(ScheduleDAGInstrs
&DAG
, SUnit
&AnchorSU
);
146 MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent
, bool FuseBlock
)
147 : shouldScheduleAdjacent(shouldScheduleAdjacent
), FuseBlock(FuseBlock
) {}
149 void apply(ScheduleDAGInstrs
*DAGInstrs
) override
;
152 } // end anonymous namespace
154 void MacroFusion::apply(ScheduleDAGInstrs
*DAG
) {
156 // For each of the SUnits in the scheduling block, try to fuse the instr in
157 // it with one in its predecessors.
158 for (SUnit
&ISU
: DAG
->SUnits
)
159 scheduleAdjacentImpl(*DAG
, ISU
);
161 if (DAG
->ExitSU
.getInstr())
162 // Try to fuse the instr in the ExitSU with one in its predecessors.
163 scheduleAdjacentImpl(*DAG
, DAG
->ExitSU
);
166 /// Implement the fusion of instr pairs in the scheduling DAG,
167 /// anchored at the instr in AnchorSU..
168 bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGInstrs
&DAG
, SUnit
&AnchorSU
) {
169 const MachineInstr
&AnchorMI
= *AnchorSU
.getInstr();
170 const TargetInstrInfo
&TII
= *DAG
.TII
;
171 const TargetSubtargetInfo
&ST
= DAG
.MF
.getSubtarget();
173 // Check if the anchor instr may be fused.
174 if (!shouldScheduleAdjacent(TII
, ST
, nullptr, AnchorMI
))
177 // Explorer for fusion candidates among the dependencies of the anchor instr.
178 for (SDep
&Dep
: AnchorSU
.Preds
) {
179 // Ignore dependencies other than data or strong ordering.
180 if (Dep
.isWeak() || isHazard(Dep
))
183 SUnit
&DepSU
= *Dep
.getSUnit();
184 if (DepSU
.isBoundaryNode())
187 // Only chain two instructions together at most.
188 const MachineInstr
*DepMI
= DepSU
.getInstr();
189 if (!hasLessThanNumFused(DepSU
, 2) ||
190 !shouldScheduleAdjacent(TII
, ST
, DepMI
, AnchorMI
))
193 if (fuseInstructionPair(DAG
, DepSU
, AnchorSU
))
200 std::unique_ptr
<ScheduleDAGMutation
>
201 llvm::createMacroFusionDAGMutation(
202 ShouldSchedulePredTy shouldScheduleAdjacent
) {
203 if(EnableMacroFusion
)
204 return std::make_unique
<MacroFusion
>(shouldScheduleAdjacent
, true);
208 std::unique_ptr
<ScheduleDAGMutation
>
209 llvm::createBranchMacroFusionDAGMutation(
210 ShouldSchedulePredTy shouldScheduleAdjacent
) {
211 if(EnableMacroFusion
)
212 return std::make_unique
<MacroFusion
>(shouldScheduleAdjacent
, false);