blob: c7b8bf9fff68a7df7482c40c98af207b5ad87bba [file] [log] [blame]
Eugene Zelenko76bf48d2017-06-26 22:44:03 +00001//===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
Florian Hahn5f746c82017-06-19 12:53:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Hahn5f746c82017-06-19 12:53:31 +00006//
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 Zelenko76bf48d2017-06-26 22:44:03 +000015#include "llvm/ADT/STLExtras.h"
Florian Hahn5f746c82017-06-19 12:53:31 +000016#include "llvm/ADT/Statistic.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000017#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/MachineScheduler.h"
19#include "llvm/CodeGen/ScheduleDAG.h"
20#include "llvm/CodeGen/ScheduleDAGMutation.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000021#include "llvm/CodeGen/TargetInstrInfo.h"
Florian Hahn5f746c82017-06-19 12:53:31 +000022#include "llvm/Support/CommandLine.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/raw_ostream.h"
Florian Hahn5f746c82017-06-19 12:53:31 +000025
Evandro Menezes0cd23f562017-07-11 22:08:28 +000026#define DEBUG_TYPE "machine-scheduler"
Florian Hahn5f746c82017-06-19 12:53:31 +000027
28STATISTIC(NumFused, "Number of instr pairs fused");
29
30using namespace llvm;
31
32static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
33 cl::desc("Enable scheduling for macro fusion."), cl::init(true));
34
Evandro Menezes54be62d2017-12-11 21:09:27 +000035static bool isHazard(const SDep &Dep) {
36 return Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output;
37}
38
39static bool fuseInstructionPair(ScheduleDAGMI &DAG, SUnit &FirstSU,
Florian Hahn5f746c82017-06-19 12:53:31 +000040 SUnit &SecondSU) {
Evandro Menezes54be62d2017-12-11 21:09:27 +000041 // 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,
51 // perhaps as part of ScheduleDAGMI::addEdge(), since such edges are valid,
52 // the extra computation cost makes it less interesting in general cases.
53
Florian Hahn5f746c82017-06-19 12:53:31 +000054 // 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 Menezes54be62d2017-12-11 21:09:27 +000056 if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
57 return false;
Florian Hahn5f746c82017-06-19 12:53:31 +000058
Evandro Menezes54be62d2017-12-11 21:09:27 +000059 // Adjust the latency between both instrs.
60 for (SDep &SI : FirstSU.Succs)
61 if (SI.getSUnit() == &SecondSU)
62 SI.setLatency(0);
Florian Hahn5f746c82017-06-19 12:53:31 +000063
Evandro Menezes54be62d2017-12-11 21:09:27 +000064 for (SDep &SI : SecondSU.Preds)
65 if (SI.getSUnit() == &FirstSU)
66 SI.setLatency(0);
Florian Hahn5f746c82017-06-19 12:53:31 +000067
Nicola Zaghend34e60c2018-05-14 12:53:11 +000068 LLVM_DEBUG(
Matthias Braun726e12c2018-09-19 00:23:35 +000069 dbgs() << "Macro fuse: "; DAG.dumpNodeName(FirstSU); dbgs() << " - ";
70 DAG.dumpNodeName(SecondSU); dbgs() << " / ";
Nicola Zaghend34e60c2018-05-14 12:53:11 +000071 dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - "
72 << DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n';);
Florian Hahn5f746c82017-06-19 12:53:31 +000073
Evandro Menezes54be62d2017-12-11 21:09:27 +000074 // 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 Hahn5f746c82017-06-19 12:53:31 +000076 if (&SecondSU != &DAG.ExitSU)
Florian Hahn5f746c82017-06-19 12:53:31 +000077 for (const SDep &SI : FirstSU.Succs) {
Evandro Menezes54be62d2017-12-11 21:09:27 +000078 SUnit *SU = SI.getSUnit();
79 if (SI.isWeak() || isHazard(SI) ||
80 SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
Florian Hahn5f746c82017-06-19 12:53:31 +000081 continue;
Matthias Braun726e12c2018-09-19 00:23:35 +000082 LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(SecondSU);
83 dbgs() << " - "; DAG.dumpNodeName(*SU); dbgs() << '\n';);
Evandro Menezes54be62d2017-12-11 21:09:27 +000084 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 Braun09810c92018-07-26 17:43:56 +000089 if (&FirstSU != &DAG.EntrySU) {
Evandro Menezes54be62d2017-12-11 21:09:27 +000090 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 Braun726e12c2018-09-19 00:23:35 +000094 LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(*SU); dbgs() << " - ";
95 DAG.dumpNodeName(FirstSU); dbgs() << '\n';);
Evandro Menezes54be62d2017-12-11 21:09:27 +000096 DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
Florian Hahn5f746c82017-06-19 12:53:31 +000097 }
Matthias Braun09810c92018-07-26 17:43:56 +000098 // 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 Hahn5f746c82017-06-19 12:53:31 +0000108
109 ++NumFused;
Evandro Menezes54be62d2017-12-11 21:09:27 +0000110 return true;
Florian Hahn5f746c82017-06-19 12:53:31 +0000111}
112
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000113namespace {
Florian Hahn5f746c82017-06-19 12:53:31 +0000114
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000115/// Post-process the DAG to create cluster edges between instrs that may
Florian Hahn5f746c82017-06-19 12:53:31 +0000116/// be fused by the processor into a single operation.
117class MacroFusion : public ScheduleDAGMutation {
118 ShouldSchedulePredTy shouldScheduleAdjacent;
119 bool FuseBlock;
120 bool scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU);
121
122public:
123 MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
124 : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
125
126 void apply(ScheduleDAGInstrs *DAGInstrs) override;
127};
128
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000129} // end anonymous namespace
130
Florian Hahn5f746c82017-06-19 12:53:31 +0000131void MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) {
132 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
133
134 if (FuseBlock)
135 // For each of the SUnits in the scheduling block, try to fuse the instr in
136 // it with one in its predecessors.
137 for (SUnit &ISU : DAG->SUnits)
138 scheduleAdjacentImpl(*DAG, ISU);
139
140 if (DAG->ExitSU.getInstr())
141 // Try to fuse the instr in the ExitSU with one in its predecessors.
142 scheduleAdjacentImpl(*DAG, DAG->ExitSU);
143}
144
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000145/// Implement the fusion of instr pairs in the scheduling DAG,
Florian Hahn5f746c82017-06-19 12:53:31 +0000146/// anchored at the instr in AnchorSU..
147bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU) {
148 const MachineInstr &AnchorMI = *AnchorSU.getInstr();
149 const TargetInstrInfo &TII = *DAG.TII;
150 const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
151
152 // Check if the anchor instr may be fused.
153 if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
154 return false;
155
156 // Explorer for fusion candidates among the dependencies of the anchor instr.
157 for (SDep &Dep : AnchorSU.Preds) {
Evandro Menezes54be62d2017-12-11 21:09:27 +0000158 // Ignore dependencies other than data or strong ordering.
159 if (Dep.isWeak() || isHazard(Dep))
Florian Hahn5f746c82017-06-19 12:53:31 +0000160 continue;
161
162 SUnit &DepSU = *Dep.getSUnit();
163 if (DepSU.isBoundaryNode())
164 continue;
165
166 const MachineInstr *DepMI = DepSU.getInstr();
167 if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
168 continue;
169
Evandro Menezes54be62d2017-12-11 21:09:27 +0000170 if (fuseInstructionPair(DAG, DepSU, AnchorSU))
171 return true;
Florian Hahn5f746c82017-06-19 12:53:31 +0000172 }
173
174 return false;
175}
176
Florian Hahn5f746c82017-06-19 12:53:31 +0000177std::unique_ptr<ScheduleDAGMutation>
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000178llvm::createMacroFusionDAGMutation(
179 ShouldSchedulePredTy shouldScheduleAdjacent) {
Florian Hahn5f746c82017-06-19 12:53:31 +0000180 if(EnableMacroFusion)
181 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
182 return nullptr;
183}
184
185std::unique_ptr<ScheduleDAGMutation>
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000186llvm::createBranchMacroFusionDAGMutation(
187 ShouldSchedulePredTy shouldScheduleAdjacent) {
Florian Hahn5f746c82017-06-19 12:53:31 +0000188 if(EnableMacroFusion)
189 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
190 return nullptr;
191}