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/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetInstrInfo.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 void fuseInstructionPair(ScheduleDAGMI &DAG, SUnit &FirstSU,
38 // Create a single weak edge between the adjacent instrs. The only effect is
39 // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
40 DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster));
42 // Adjust the latency between the anchor instr and its
44 for (SDep &IDep : SecondSU.Preds)
45 if (IDep.getSUnit() == &FirstSU)
48 // Adjust the latency between the dependent instr and its
50 for (SDep &IDep : FirstSU.Succs)
51 if (IDep.getSUnit() == &SecondSU)
54 DEBUG(dbgs() << DAG.MF.getName() << "(): Macro fuse ";
55 FirstSU.print(dbgs(), &DAG); dbgs() << " - ";
56 SecondSU.print(dbgs(), &DAG); dbgs() << " / ";
57 dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - " <<
58 DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n'; );
60 if (&SecondSU != &DAG.ExitSU)
61 // Make instructions dependent on FirstSU also dependent on SecondSU to
62 // prevent them from being scheduled between FirstSU and and SecondSU.
63 for (const SDep &SI : FirstSU.Succs) {
64 if (SI.getSUnit() == &SecondSU)
66 DEBUG(dbgs() << " Copy Succ ";
67 SI.getSUnit()->print(dbgs(), &DAG); dbgs() << '\n';);
68 DAG.addEdge(SI.getSUnit(), SDep(&SecondSU, SDep::Artificial));
76 /// \brief Post-process the DAG to create cluster edges between instrs that may
77 /// be fused by the processor into a single operation.
78 class MacroFusion : public ScheduleDAGMutation {
79 ShouldSchedulePredTy shouldScheduleAdjacent;
81 bool scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU);
84 MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
85 : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
87 void apply(ScheduleDAGInstrs *DAGInstrs) override;
90 } // end anonymous namespace
92 void MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) {
93 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
96 // For each of the SUnits in the scheduling block, try to fuse the instr in
97 // it with one in its predecessors.
98 for (SUnit &ISU : DAG->SUnits)
99 scheduleAdjacentImpl(*DAG, ISU);
101 if (DAG->ExitSU.getInstr())
102 // Try to fuse the instr in the ExitSU with one in its predecessors.
103 scheduleAdjacentImpl(*DAG, DAG->ExitSU);
106 /// \brief Implement the fusion of instr pairs in the scheduling DAG,
107 /// anchored at the instr in AnchorSU..
108 bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU) {
109 const MachineInstr &AnchorMI = *AnchorSU.getInstr();
110 const TargetInstrInfo &TII = *DAG.TII;
111 const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
113 // Check if the anchor instr may be fused.
114 if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
117 // Explorer for fusion candidates among the dependencies of the anchor instr.
118 for (SDep &Dep : AnchorSU.Preds) {
119 // Ignore dependencies that don't enforce ordering.
120 if (Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output ||
124 SUnit &DepSU = *Dep.getSUnit();
125 if (DepSU.isBoundaryNode())
128 const MachineInstr *DepMI = DepSU.getInstr();
129 if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
132 fuseInstructionPair(DAG, DepSU, AnchorSU);
139 std::unique_ptr<ScheduleDAGMutation>
140 llvm::createMacroFusionDAGMutation(
141 ShouldSchedulePredTy shouldScheduleAdjacent) {
142 if(EnableMacroFusion)
143 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
147 std::unique_ptr<ScheduleDAGMutation>
148 llvm::createBranchMacroFusionDAGMutation(
149 ShouldSchedulePredTy shouldScheduleAdjacent) {
150 if(EnableMacroFusion)
151 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false);