176bf48d9SEugene Zelenko //===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
25f746c8eSFlorian Hahn //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f746c8eSFlorian Hahn //
75f746c8eSFlorian Hahn //===----------------------------------------------------------------------===//
85f746c8eSFlorian Hahn //
95f746c8eSFlorian Hahn /// \file This file contains the implementation of the DAG scheduling mutation
105f746c8eSFlorian Hahn /// to pair instructions back to back.
115f746c8eSFlorian Hahn //
125f746c8eSFlorian Hahn //===----------------------------------------------------------------------===//
135f746c8eSFlorian Hahn
145f746c8eSFlorian Hahn #include "llvm/CodeGen/MacroFusion.h"
155f746c8eSFlorian Hahn #include "llvm/ADT/Statistic.h"
1676bf48d9SEugene Zelenko #include "llvm/CodeGen/MachineInstr.h"
1776bf48d9SEugene Zelenko #include "llvm/CodeGen/ScheduleDAG.h"
18*989f1c72Sserge-sans-paille #include "llvm/CodeGen/ScheduleDAGInstrs.h"
1976bf48d9SEugene Zelenko #include "llvm/CodeGen/ScheduleDAGMutation.h"
203f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
215f746c8eSFlorian Hahn #include "llvm/Support/CommandLine.h"
2276bf48d9SEugene Zelenko #include "llvm/Support/Debug.h"
2376bf48d9SEugene Zelenko #include "llvm/Support/raw_ostream.h"
245f746c8eSFlorian Hahn
250cd23f56SEvandro Menezes #define DEBUG_TYPE "machine-scheduler"
265f746c8eSFlorian Hahn
275f746c8eSFlorian Hahn STATISTIC(NumFused, "Number of instr pairs fused");
285f746c8eSFlorian Hahn
295f746c8eSFlorian Hahn using namespace llvm;
305f746c8eSFlorian Hahn
315f746c8eSFlorian Hahn static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
325f746c8eSFlorian Hahn cl::desc("Enable scheduling for macro fusion."), cl::init(true));
335f746c8eSFlorian Hahn
isHazard(const SDep & Dep)3454be62dfSEvandro Menezes static bool isHazard(const SDep &Dep) {
3554be62dfSEvandro Menezes return Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output;
3654be62dfSEvandro Menezes }
3754be62dfSEvandro Menezes
getPredClusterSU(const SUnit & SU)38d84b320dSQingShan Zhang static SUnit *getPredClusterSU(const SUnit &SU) {
39d84b320dSQingShan Zhang for (const SDep &SI : SU.Preds)
40d84b320dSQingShan Zhang if (SI.isCluster())
41d84b320dSQingShan Zhang return SI.getSUnit();
42d84b320dSQingShan Zhang
43d84b320dSQingShan Zhang return nullptr;
44d84b320dSQingShan Zhang }
45d84b320dSQingShan Zhang
hasLessThanNumFused(const SUnit & SU,unsigned FuseLimit)468f55fdf2SJoe Nash bool llvm::hasLessThanNumFused(const SUnit &SU, unsigned FuseLimit) {
47d84b320dSQingShan Zhang unsigned Num = 1;
48d84b320dSQingShan Zhang const SUnit *CurrentSU = &SU;
49d84b320dSQingShan Zhang while ((CurrentSU = getPredClusterSU(*CurrentSU)) && Num < FuseLimit) Num ++;
50d84b320dSQingShan Zhang return Num < FuseLimit;
51d84b320dSQingShan Zhang }
52d84b320dSQingShan Zhang
fuseInstructionPair(ScheduleDAGInstrs & DAG,SUnit & FirstSU,SUnit & SecondSU)538f55fdf2SJoe Nash bool llvm::fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU,
545f746c8eSFlorian Hahn SUnit &SecondSU) {
5554be62dfSEvandro Menezes // Check that neither instr is already paired with another along the edge
5654be62dfSEvandro Menezes // between them.
5754be62dfSEvandro Menezes for (SDep &SI : FirstSU.Succs)
5854be62dfSEvandro Menezes if (SI.isCluster())
5954be62dfSEvandro Menezes return false;
6054be62dfSEvandro Menezes
6154be62dfSEvandro Menezes for (SDep &SI : SecondSU.Preds)
6254be62dfSEvandro Menezes if (SI.isCluster())
6354be62dfSEvandro Menezes return false;
6454be62dfSEvandro Menezes // Though the reachability checks above could be made more generic,
65b70355f0SClement Courbet // perhaps as part of ScheduleDAGInstrs::addEdge(), since such edges are valid,
6654be62dfSEvandro Menezes // the extra computation cost makes it less interesting in general cases.
6754be62dfSEvandro Menezes
685f746c8eSFlorian Hahn // Create a single weak edge between the adjacent instrs. The only effect is
695f746c8eSFlorian Hahn // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
7054be62dfSEvandro Menezes if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
7154be62dfSEvandro Menezes return false;
725f746c8eSFlorian Hahn
7305b0c76aSQingShan Zhang // TODO - If we want to chain more than two instructions, we need to create
7405b0c76aSQingShan Zhang // artifical edges to make dependencies from the FirstSU also dependent
7505b0c76aSQingShan Zhang // on other chained instructions, and other chained instructions also
7605b0c76aSQingShan Zhang // dependent on the dependencies of the SecondSU, to prevent them from being
7705b0c76aSQingShan Zhang // scheduled into these chained instructions.
7805b0c76aSQingShan Zhang assert(hasLessThanNumFused(FirstSU, 2) &&
7905b0c76aSQingShan Zhang "Currently we only support chaining together two instructions");
8005b0c76aSQingShan Zhang
8154be62dfSEvandro Menezes // Adjust the latency between both instrs.
8254be62dfSEvandro Menezes for (SDep &SI : FirstSU.Succs)
8354be62dfSEvandro Menezes if (SI.getSUnit() == &SecondSU)
8454be62dfSEvandro Menezes SI.setLatency(0);
855f746c8eSFlorian Hahn
8654be62dfSEvandro Menezes for (SDep &SI : SecondSU.Preds)
8754be62dfSEvandro Menezes if (SI.getSUnit() == &FirstSU)
8854be62dfSEvandro Menezes SI.setLatency(0);
895f746c8eSFlorian Hahn
90d34e60caSNicola Zaghen LLVM_DEBUG(
91726e12cfSMatthias Braun dbgs() << "Macro fuse: "; DAG.dumpNodeName(FirstSU); dbgs() << " - ";
92726e12cfSMatthias Braun DAG.dumpNodeName(SecondSU); dbgs() << " / ";
93d34e60caSNicola Zaghen dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - "
94d34e60caSNicola Zaghen << DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n';);
955f746c8eSFlorian Hahn
9654be62dfSEvandro Menezes // Make data dependencies from the FirstSU also dependent on the SecondSU to
9754be62dfSEvandro Menezes // prevent them from being scheduled between the FirstSU and the SecondSU.
985f746c8eSFlorian Hahn if (&SecondSU != &DAG.ExitSU)
995f746c8eSFlorian Hahn for (const SDep &SI : FirstSU.Succs) {
10054be62dfSEvandro Menezes SUnit *SU = SI.getSUnit();
10154be62dfSEvandro Menezes if (SI.isWeak() || isHazard(SI) ||
10254be62dfSEvandro Menezes SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
1035f746c8eSFlorian Hahn continue;
104726e12cfSMatthias Braun LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(SecondSU);
105726e12cfSMatthias Braun dbgs() << " - "; DAG.dumpNodeName(*SU); dbgs() << '\n';);
10654be62dfSEvandro Menezes DAG.addEdge(SU, SDep(&SecondSU, SDep::Artificial));
10754be62dfSEvandro Menezes }
10854be62dfSEvandro Menezes
10954be62dfSEvandro Menezes // Make the FirstSU also dependent on the dependencies of the SecondSU to
11054be62dfSEvandro Menezes // prevent them from being scheduled between the FirstSU and the SecondSU.
11117dc729bSAlexander Belyaev if (&FirstSU != &DAG.EntrySU) {
11254be62dfSEvandro Menezes for (const SDep &SI : SecondSU.Preds) {
11354be62dfSEvandro Menezes SUnit *SU = SI.getSUnit();
11454be62dfSEvandro Menezes if (SI.isWeak() || isHazard(SI) || &FirstSU == SU || FirstSU.isSucc(SU))
11554be62dfSEvandro Menezes continue;
116726e12cfSMatthias Braun LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(*SU); dbgs() << " - ";
117726e12cfSMatthias Braun DAG.dumpNodeName(FirstSU); dbgs() << '\n';);
11854be62dfSEvandro Menezes DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
1195f746c8eSFlorian Hahn }
12009810c92SMatthias Braun // ExitSU comes last by design, which acts like an implicit dependency
12109810c92SMatthias Braun // between ExitSU and any bottom root in the graph. We should transfer
12209810c92SMatthias Braun // this to FirstSU as well.
12309810c92SMatthias Braun if (&SecondSU == &DAG.ExitSU) {
12409810c92SMatthias Braun for (SUnit &SU : DAG.SUnits) {
12509810c92SMatthias Braun if (SU.Succs.empty())
12609810c92SMatthias Braun DAG.addEdge(&FirstSU, SDep(&SU, SDep::Artificial));
12709810c92SMatthias Braun }
12809810c92SMatthias Braun }
12917dc729bSAlexander Belyaev }
1305f746c8eSFlorian Hahn
1315f746c8eSFlorian Hahn ++NumFused;
13254be62dfSEvandro Menezes return true;
1335f746c8eSFlorian Hahn }
1345f746c8eSFlorian Hahn
13576bf48d9SEugene Zelenko namespace {
1365f746c8eSFlorian Hahn
1375f8f34e4SAdrian Prantl /// Post-process the DAG to create cluster edges between instrs that may
1385f746c8eSFlorian Hahn /// be fused by the processor into a single operation.
1395f746c8eSFlorian Hahn class MacroFusion : public ScheduleDAGMutation {
1405f746c8eSFlorian Hahn ShouldSchedulePredTy shouldScheduleAdjacent;
1415f746c8eSFlorian Hahn bool FuseBlock;
142b70355f0SClement Courbet bool scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU);
1435f746c8eSFlorian Hahn
1445f746c8eSFlorian Hahn public:
MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent,bool FuseBlock)1455f746c8eSFlorian Hahn MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
1465f746c8eSFlorian Hahn : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
1475f746c8eSFlorian Hahn
1485f746c8eSFlorian Hahn void apply(ScheduleDAGInstrs *DAGInstrs) override;
1495f746c8eSFlorian Hahn };
1505f746c8eSFlorian Hahn
15176bf48d9SEugene Zelenko } // end anonymous namespace
15276bf48d9SEugene Zelenko
apply(ScheduleDAGInstrs * DAG)153b70355f0SClement Courbet void MacroFusion::apply(ScheduleDAGInstrs *DAG) {
1545f746c8eSFlorian Hahn if (FuseBlock)
1555f746c8eSFlorian Hahn // For each of the SUnits in the scheduling block, try to fuse the instr in
1565f746c8eSFlorian Hahn // it with one in its predecessors.
1575f746c8eSFlorian Hahn for (SUnit &ISU : DAG->SUnits)
1585f746c8eSFlorian Hahn scheduleAdjacentImpl(*DAG, ISU);
1595f746c8eSFlorian Hahn
1605f746c8eSFlorian Hahn if (DAG->ExitSU.getInstr())
1615f746c8eSFlorian Hahn // Try to fuse the instr in the ExitSU with one in its predecessors.
1625f746c8eSFlorian Hahn scheduleAdjacentImpl(*DAG, DAG->ExitSU);
1635f746c8eSFlorian Hahn }
1645f746c8eSFlorian Hahn
1655f8f34e4SAdrian Prantl /// Implement the fusion of instr pairs in the scheduling DAG,
1665f746c8eSFlorian Hahn /// anchored at the instr in AnchorSU..
scheduleAdjacentImpl(ScheduleDAGInstrs & DAG,SUnit & AnchorSU)167b70355f0SClement Courbet bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU) {
1685f746c8eSFlorian Hahn const MachineInstr &AnchorMI = *AnchorSU.getInstr();
1695f746c8eSFlorian Hahn const TargetInstrInfo &TII = *DAG.TII;
1705f746c8eSFlorian Hahn const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
1715f746c8eSFlorian Hahn
1725f746c8eSFlorian Hahn // Check if the anchor instr may be fused.
1735f746c8eSFlorian Hahn if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
1745f746c8eSFlorian Hahn return false;
1755f746c8eSFlorian Hahn
1765f746c8eSFlorian Hahn // Explorer for fusion candidates among the dependencies of the anchor instr.
1775f746c8eSFlorian Hahn for (SDep &Dep : AnchorSU.Preds) {
17854be62dfSEvandro Menezes // Ignore dependencies other than data or strong ordering.
17954be62dfSEvandro Menezes if (Dep.isWeak() || isHazard(Dep))
1805f746c8eSFlorian Hahn continue;
1815f746c8eSFlorian Hahn
1825f746c8eSFlorian Hahn SUnit &DepSU = *Dep.getSUnit();
1835f746c8eSFlorian Hahn if (DepSU.isBoundaryNode())
1845f746c8eSFlorian Hahn continue;
1855f746c8eSFlorian Hahn
186d84b320dSQingShan Zhang // Only chain two instructions together at most.
1875f746c8eSFlorian Hahn const MachineInstr *DepMI = DepSU.getInstr();
188d84b320dSQingShan Zhang if (!hasLessThanNumFused(DepSU, 2) ||
189d84b320dSQingShan Zhang !shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
1905f746c8eSFlorian Hahn continue;
1915f746c8eSFlorian Hahn
19254be62dfSEvandro Menezes if (fuseInstructionPair(DAG, DepSU, AnchorSU))
1935f746c8eSFlorian Hahn return true;
1945f746c8eSFlorian Hahn }
1955f746c8eSFlorian Hahn
1965f746c8eSFlorian Hahn return false;
1975f746c8eSFlorian Hahn }
1985f746c8eSFlorian Hahn
1995f746c8eSFlorian Hahn std::unique_ptr<ScheduleDAGMutation>
createMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)20076bf48d9SEugene Zelenko llvm::createMacroFusionDAGMutation(
20176bf48d9SEugene Zelenko ShouldSchedulePredTy shouldScheduleAdjacent) {
2025f746c8eSFlorian Hahn if(EnableMacroFusion)
2030eaee545SJonas Devlieghere return std::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
2045f746c8eSFlorian Hahn return nullptr;
2055f746c8eSFlorian Hahn }
2065f746c8eSFlorian Hahn
2075f746c8eSFlorian Hahn std::unique_ptr<ScheduleDAGMutation>
createBranchMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)20876bf48d9SEugene Zelenko llvm::createBranchMacroFusionDAGMutation(
20976bf48d9SEugene Zelenko ShouldSchedulePredTy shouldScheduleAdjacent) {
2105f746c8eSFlorian Hahn if(EnableMacroFusion)
2110eaee545SJonas Devlieghere return std::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
2125f746c8eSFlorian Hahn return nullptr;
2135f746c8eSFlorian Hahn }
214