1 //===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file This file contains the implementation of the DAG scheduling mutation
11 /// to pair instructions back to back.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/MacroFusion.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineScheduler.h"
20 #include "llvm/CodeGen/ScheduleDAG.h"
21 #include "llvm/CodeGen/ScheduleDAGMutation.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "machine-scheduler"
29 STATISTIC(NumFused
, "Number of instr pairs fused");
33 static cl::opt
<bool> EnableMacroFusion("misched-fusion", cl::Hidden
,
34 cl::desc("Enable scheduling for macro fusion."), cl::init(true));
36 static bool isHazard(const SDep
&Dep
) {
37 return Dep
.getKind() == SDep::Anti
|| Dep
.getKind() == SDep::Output
;
40 static bool fuseInstructionPair(ScheduleDAGMI
&DAG
, SUnit
&FirstSU
,
42 // Check that neither instr is already paired with another along the edge
44 for (SDep
&SI
: FirstSU
.Succs
)
48 for (SDep
&SI
: SecondSU
.Preds
)
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.
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.
57 if (!DAG
.addEdge(&SecondSU
, SDep(&FirstSU
, SDep::Cluster
)))
60 // Adjust the latency between both instrs.
61 for (SDep
&SI
: FirstSU
.Succs
)
62 if (SI
.getSUnit() == &SecondSU
)
65 for (SDep
&SI
: SecondSU
.Preds
)
66 if (SI
.getSUnit() == &FirstSU
)
70 dbgs() << "Macro fuse: "; DAG
.dumpNodeName(FirstSU
); dbgs() << " - ";
71 DAG
.dumpNodeName(SecondSU
); dbgs() << " / ";
72 dbgs() << DAG
.TII
->getName(FirstSU
.getInstr()->getOpcode()) << " - "
73 << DAG
.TII
->getName(SecondSU
.getInstr()->getOpcode()) << '\n';);
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.
77 if (&SecondSU
!= &DAG
.ExitSU
)
78 for (const SDep
&SI
: FirstSU
.Succs
) {
79 SUnit
*SU
= SI
.getSUnit();
80 if (SI
.isWeak() || isHazard(SI
) ||
81 SU
== &DAG
.ExitSU
|| SU
== &SecondSU
|| SU
->isPred(&SecondSU
))
83 LLVM_DEBUG(dbgs() << " Bind "; DAG
.dumpNodeName(SecondSU
);
84 dbgs() << " - "; DAG
.dumpNodeName(*SU
); dbgs() << '\n';);
85 DAG
.addEdge(SU
, SDep(&SecondSU
, SDep::Artificial
));
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.
90 if (&FirstSU
!= &DAG
.EntrySU
) {
91 for (const SDep
&SI
: SecondSU
.Preds
) {
92 SUnit
*SU
= SI
.getSUnit();
93 if (SI
.isWeak() || isHazard(SI
) || &FirstSU
== SU
|| FirstSU
.isSucc(SU
))
95 LLVM_DEBUG(dbgs() << " Bind "; DAG
.dumpNodeName(*SU
); dbgs() << " - ";
96 DAG
.dumpNodeName(FirstSU
); dbgs() << '\n';);
97 DAG
.addEdge(&FirstSU
, SDep(SU
, SDep::Artificial
));
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
));
116 /// Post-process the DAG to create cluster edges between instrs that may
117 /// be fused by the processor into a single operation.
118 class MacroFusion
: public ScheduleDAGMutation
{
119 ShouldSchedulePredTy shouldScheduleAdjacent
;
121 bool scheduleAdjacentImpl(ScheduleDAGMI
&DAG
, SUnit
&AnchorSU
);
124 MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent
, bool FuseBlock
)
125 : shouldScheduleAdjacent(shouldScheduleAdjacent
), FuseBlock(FuseBlock
) {}
127 void apply(ScheduleDAGInstrs
*DAGInstrs
) override
;
130 } // end anonymous namespace
132 void MacroFusion::apply(ScheduleDAGInstrs
*DAGInstrs
) {
133 ScheduleDAGMI
*DAG
= static_cast<ScheduleDAGMI
*>(DAGInstrs
);
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
);
141 if (DAG
->ExitSU
.getInstr())
142 // Try to fuse the instr in the ExitSU with one in its predecessors.
143 scheduleAdjacentImpl(*DAG
, DAG
->ExitSU
);
146 /// Implement the fusion of instr pairs in the scheduling DAG,
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();
153 // Check if the anchor instr may be fused.
154 if (!shouldScheduleAdjacent(TII
, ST
, nullptr, AnchorMI
))
157 // Explorer for fusion candidates among the dependencies of the anchor instr.
158 for (SDep
&Dep
: AnchorSU
.Preds
) {
159 // Ignore dependencies other than data or strong ordering.
160 if (Dep
.isWeak() || isHazard(Dep
))
163 SUnit
&DepSU
= *Dep
.getSUnit();
164 if (DepSU
.isBoundaryNode())
167 const MachineInstr
*DepMI
= DepSU
.getInstr();
168 if (!shouldScheduleAdjacent(TII
, ST
, DepMI
, AnchorMI
))
171 if (fuseInstructionPair(DAG
, DepSU
, AnchorSU
))
178 std::unique_ptr
<ScheduleDAGMutation
>
179 llvm::createMacroFusionDAGMutation(
180 ShouldSchedulePredTy shouldScheduleAdjacent
) {
181 if(EnableMacroFusion
)
182 return llvm::make_unique
<MacroFusion
>(shouldScheduleAdjacent
, true);
186 std::unique_ptr
<ScheduleDAGMutation
>
187 llvm::createBranchMacroFusionDAGMutation(
188 ShouldSchedulePredTy shouldScheduleAdjacent
) {
189 if(EnableMacroFusion
)
190 return llvm::make_unique
<MacroFusion
>(shouldScheduleAdjacent
, false);