blob: 5b3523be63510e9c7cfbf2de5bbf41f14d6877d2 [file] [log] [blame]
Eugene Zelenko76bf48d2017-06-26 22:44:03 +00001//===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
Florian Hahn5f746c82017-06-19 12:53:31 +00002//
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 Zelenko76bf48d2017-06-26 22:44:03 +000016#include "llvm/ADT/STLExtras.h"
Florian Hahn5f746c82017-06-19 12:53:31 +000017#include "llvm/ADT/Statistic.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000018#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineScheduler.h"
20#include "llvm/CodeGen/ScheduleDAG.h"
21#include "llvm/CodeGen/ScheduleDAGMutation.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetInstrInfo.h"
Florian Hahn5f746c82017-06-19 12:53:31 +000023#include "llvm/Support/CommandLine.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Florian Hahn5f746c82017-06-19 12:53:31 +000026
Evandro Menezes0cd23f562017-07-11 22:08:28 +000027#define DEBUG_TYPE "machine-scheduler"
Florian Hahn5f746c82017-06-19 12:53:31 +000028
29STATISTIC(NumFused, "Number of instr pairs fused");
30
31using namespace llvm;
32
33static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
34 cl::desc("Enable scheduling for macro fusion."), cl::init(true));
35
Evandro Menezes54be62d2017-12-11 21:09:27 +000036static bool isHazard(const SDep &Dep) {
37 return Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output;
38}
39
40static bool fuseInstructionPair(ScheduleDAGMI &DAG, SUnit &FirstSU,
Florian Hahn5f746c82017-06-19 12:53:31 +000041 SUnit &SecondSU) {
Evandro Menezes54be62d2017-12-11 21:09:27 +000042 // 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 Hahn5f746c82017-06-19 12:53:31 +000055 // 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 Menezes54be62d2017-12-11 21:09:27 +000057 if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
58 return false;
Florian Hahn5f746c82017-06-19 12:53:31 +000059
Evandro Menezes54be62d2017-12-11 21:09:27 +000060 // Adjust the latency between both instrs.
61 for (SDep &SI : FirstSU.Succs)
62 if (SI.getSUnit() == &SecondSU)
63 SI.setLatency(0);
Florian Hahn5f746c82017-06-19 12:53:31 +000064
Evandro Menezes54be62d2017-12-11 21:09:27 +000065 for (SDep &SI : SecondSU.Preds)
66 if (SI.getSUnit() == &FirstSU)
67 SI.setLatency(0);
Florian Hahn5f746c82017-06-19 12:53:31 +000068
Evandro Menezes54be62d2017-12-11 21:09:27 +000069 DEBUG(dbgs() << "Macro fuse: ";
Florian Hahn5f746c82017-06-19 12:53:31 +000070 FirstSU.print(dbgs(), &DAG); dbgs() << " - ";
71 SecondSU.print(dbgs(), &DAG); dbgs() << " / ";
72 dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - " <<
73 DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n'; );
74
Evandro Menezes54be62d2017-12-11 21:09:27 +000075 // 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 Hahn5f746c82017-06-19 12:53:31 +000077 if (&SecondSU != &DAG.ExitSU)
Florian Hahn5f746c82017-06-19 12:53:31 +000078 for (const SDep &SI : FirstSU.Succs) {
Evandro Menezes54be62d2017-12-11 21:09:27 +000079 SUnit *SU = SI.getSUnit();
80 if (SI.isWeak() || isHazard(SI) ||
81 SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
Florian Hahn5f746c82017-06-19 12:53:31 +000082 continue;
Evandro Menezes54be62d2017-12-11 21:09:27 +000083 DEBUG(dbgs() << " Bind ";
84 SecondSU.print(dbgs(), &DAG); dbgs() << " - ";
85 SU->print(dbgs(), &DAG); dbgs() << '\n';);
86 DAG.addEdge(SU, SDep(&SecondSU, SDep::Artificial));
87 }
88
89 // Make the FirstSU also dependent on the dependencies of the SecondSU to
90 // prevent them from being scheduled between the FirstSU and the SecondSU.
91 if (&FirstSU != &DAG.EntrySU)
92 for (const SDep &SI : SecondSU.Preds) {
93 SUnit *SU = SI.getSUnit();
94 if (SI.isWeak() || isHazard(SI) || &FirstSU == SU || FirstSU.isSucc(SU))
95 continue;
96 DEBUG(dbgs() << " Bind ";
97 SU->print(dbgs(), &DAG); dbgs() << " - ";
98 FirstSU.print(dbgs(), &DAG); dbgs() << '\n';);
99 DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
Florian Hahn5f746c82017-06-19 12:53:31 +0000100 }
101
102 ++NumFused;
Evandro Menezes54be62d2017-12-11 21:09:27 +0000103 return true;
Florian Hahn5f746c82017-06-19 12:53:31 +0000104}
105
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000106namespace {
Florian Hahn5f746c82017-06-19 12:53:31 +0000107
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000108/// Post-process the DAG to create cluster edges between instrs that may
Florian Hahn5f746c82017-06-19 12:53:31 +0000109/// be fused by the processor into a single operation.
110class MacroFusion : public ScheduleDAGMutation {
111 ShouldSchedulePredTy shouldScheduleAdjacent;
112 bool FuseBlock;
113 bool scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU);
114
115public:
116 MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
117 : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
118
119 void apply(ScheduleDAGInstrs *DAGInstrs) override;
120};
121
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000122} // end anonymous namespace
123
Florian Hahn5f746c82017-06-19 12:53:31 +0000124void MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) {
125 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
126
127 if (FuseBlock)
128 // For each of the SUnits in the scheduling block, try to fuse the instr in
129 // it with one in its predecessors.
130 for (SUnit &ISU : DAG->SUnits)
131 scheduleAdjacentImpl(*DAG, ISU);
132
133 if (DAG->ExitSU.getInstr())
134 // Try to fuse the instr in the ExitSU with one in its predecessors.
135 scheduleAdjacentImpl(*DAG, DAG->ExitSU);
136}
137
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000138/// Implement the fusion of instr pairs in the scheduling DAG,
Florian Hahn5f746c82017-06-19 12:53:31 +0000139/// anchored at the instr in AnchorSU..
140bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU) {
141 const MachineInstr &AnchorMI = *AnchorSU.getInstr();
142 const TargetInstrInfo &TII = *DAG.TII;
143 const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
144
145 // Check if the anchor instr may be fused.
146 if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
147 return false;
148
149 // Explorer for fusion candidates among the dependencies of the anchor instr.
150 for (SDep &Dep : AnchorSU.Preds) {
Evandro Menezes54be62d2017-12-11 21:09:27 +0000151 // Ignore dependencies other than data or strong ordering.
152 if (Dep.isWeak() || isHazard(Dep))
Florian Hahn5f746c82017-06-19 12:53:31 +0000153 continue;
154
155 SUnit &DepSU = *Dep.getSUnit();
156 if (DepSU.isBoundaryNode())
157 continue;
158
159 const MachineInstr *DepMI = DepSU.getInstr();
160 if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
161 continue;
162
Evandro Menezes54be62d2017-12-11 21:09:27 +0000163 if (fuseInstructionPair(DAG, DepSU, AnchorSU))
164 return true;
Florian Hahn5f746c82017-06-19 12:53:31 +0000165 }
166
167 return false;
168}
169
Florian Hahn5f746c82017-06-19 12:53:31 +0000170std::unique_ptr<ScheduleDAGMutation>
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000171llvm::createMacroFusionDAGMutation(
172 ShouldSchedulePredTy shouldScheduleAdjacent) {
Florian Hahn5f746c82017-06-19 12:53:31 +0000173 if(EnableMacroFusion)
174 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
175 return nullptr;
176}
177
178std::unique_ptr<ScheduleDAGMutation>
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000179llvm::createBranchMacroFusionDAGMutation(
180 ShouldSchedulePredTy shouldScheduleAdjacent) {
Florian Hahn5f746c82017-06-19 12:53:31 +0000181 if(EnableMacroFusion)
182 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
183 return nullptr;
184}