109467b48Spatrick //===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick /// \file This file contains the implementation of the DAG scheduling mutation
1009467b48Spatrick /// to pair instructions back to back.
1109467b48Spatrick //
1209467b48Spatrick //===----------------------------------------------------------------------===//
1309467b48Spatrick 
1409467b48Spatrick #include "llvm/CodeGen/MacroFusion.h"
1509467b48Spatrick #include "llvm/ADT/Statistic.h"
1609467b48Spatrick #include "llvm/CodeGen/MachineInstr.h"
1709467b48Spatrick #include "llvm/CodeGen/ScheduleDAG.h"
18*d415bd75Srobert #include "llvm/CodeGen/ScheduleDAGInstrs.h"
1909467b48Spatrick #include "llvm/CodeGen/ScheduleDAGMutation.h"
2009467b48Spatrick #include "llvm/CodeGen/TargetInstrInfo.h"
2109467b48Spatrick #include "llvm/Support/CommandLine.h"
2209467b48Spatrick #include "llvm/Support/Debug.h"
2309467b48Spatrick #include "llvm/Support/raw_ostream.h"
2409467b48Spatrick 
2509467b48Spatrick #define DEBUG_TYPE "machine-scheduler"
2609467b48Spatrick 
2709467b48Spatrick STATISTIC(NumFused, "Number of instr pairs fused");
2809467b48Spatrick 
2909467b48Spatrick using namespace llvm;
3009467b48Spatrick 
3109467b48Spatrick static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
3209467b48Spatrick   cl::desc("Enable scheduling for macro fusion."), cl::init(true));
3309467b48Spatrick 
isHazard(const SDep & Dep)3409467b48Spatrick static bool isHazard(const SDep &Dep) {
3509467b48Spatrick   return Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output;
3609467b48Spatrick }
3709467b48Spatrick 
getPredClusterSU(const SUnit & SU)3809467b48Spatrick static SUnit *getPredClusterSU(const SUnit &SU) {
3909467b48Spatrick   for (const SDep &SI : SU.Preds)
4009467b48Spatrick     if (SI.isCluster())
4109467b48Spatrick       return SI.getSUnit();
4209467b48Spatrick 
4309467b48Spatrick   return nullptr;
4409467b48Spatrick }
4509467b48Spatrick 
hasLessThanNumFused(const SUnit & SU,unsigned FuseLimit)46*d415bd75Srobert bool llvm::hasLessThanNumFused(const SUnit &SU, unsigned FuseLimit) {
4709467b48Spatrick   unsigned Num = 1;
4809467b48Spatrick   const SUnit *CurrentSU = &SU;
4909467b48Spatrick   while ((CurrentSU = getPredClusterSU(*CurrentSU)) && Num < FuseLimit) Num ++;
5009467b48Spatrick   return Num < FuseLimit;
5109467b48Spatrick }
5209467b48Spatrick 
fuseInstructionPair(ScheduleDAGInstrs & DAG,SUnit & FirstSU,SUnit & SecondSU)53*d415bd75Srobert bool llvm::fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU,
5409467b48Spatrick                                SUnit &SecondSU) {
5509467b48Spatrick   // Check that neither instr is already paired with another along the edge
5609467b48Spatrick   // between them.
5709467b48Spatrick   for (SDep &SI : FirstSU.Succs)
5809467b48Spatrick     if (SI.isCluster())
5909467b48Spatrick       return false;
6009467b48Spatrick 
6109467b48Spatrick   for (SDep &SI : SecondSU.Preds)
6209467b48Spatrick     if (SI.isCluster())
6309467b48Spatrick       return false;
6409467b48Spatrick   // Though the reachability checks above could be made more generic,
6509467b48Spatrick   // perhaps as part of ScheduleDAGInstrs::addEdge(), since such edges are valid,
6609467b48Spatrick   // the extra computation cost makes it less interesting in general cases.
6709467b48Spatrick 
6809467b48Spatrick   // Create a single weak edge between the adjacent instrs. The only effect is
6909467b48Spatrick   // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
7009467b48Spatrick   if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
7109467b48Spatrick     return false;
7209467b48Spatrick 
7309467b48Spatrick   // TODO - If we want to chain more than two instructions, we need to create
7409467b48Spatrick   // artifical edges to make dependencies from the FirstSU also dependent
7509467b48Spatrick   // on other chained instructions, and other chained instructions also
7609467b48Spatrick   // dependent on the dependencies of the SecondSU, to prevent them from being
7709467b48Spatrick   // scheduled into these chained instructions.
7809467b48Spatrick   assert(hasLessThanNumFused(FirstSU, 2) &&
7909467b48Spatrick          "Currently we only support chaining together two instructions");
8009467b48Spatrick 
8109467b48Spatrick   // Adjust the latency between both instrs.
8209467b48Spatrick   for (SDep &SI : FirstSU.Succs)
8309467b48Spatrick     if (SI.getSUnit() == &SecondSU)
8409467b48Spatrick       SI.setLatency(0);
8509467b48Spatrick 
8609467b48Spatrick   for (SDep &SI : SecondSU.Preds)
8709467b48Spatrick     if (SI.getSUnit() == &FirstSU)
8809467b48Spatrick       SI.setLatency(0);
8909467b48Spatrick 
9009467b48Spatrick   LLVM_DEBUG(
9109467b48Spatrick       dbgs() << "Macro fuse: "; DAG.dumpNodeName(FirstSU); dbgs() << " - ";
9209467b48Spatrick       DAG.dumpNodeName(SecondSU); dbgs() << " /  ";
9309467b48Spatrick       dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - "
9409467b48Spatrick              << DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n';);
9509467b48Spatrick 
9609467b48Spatrick   // Make data dependencies from the FirstSU also dependent on the SecondSU to
9709467b48Spatrick   // prevent them from being scheduled between the FirstSU and the SecondSU.
9809467b48Spatrick   if (&SecondSU != &DAG.ExitSU)
9909467b48Spatrick     for (const SDep &SI : FirstSU.Succs) {
10009467b48Spatrick       SUnit *SU = SI.getSUnit();
10109467b48Spatrick       if (SI.isWeak() || isHazard(SI) ||
10209467b48Spatrick           SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
10309467b48Spatrick         continue;
10409467b48Spatrick       LLVM_DEBUG(dbgs() << "  Bind "; DAG.dumpNodeName(SecondSU);
10509467b48Spatrick                  dbgs() << " - "; DAG.dumpNodeName(*SU); dbgs() << '\n';);
10609467b48Spatrick       DAG.addEdge(SU, SDep(&SecondSU, SDep::Artificial));
10709467b48Spatrick     }
10809467b48Spatrick 
10909467b48Spatrick   // Make the FirstSU also dependent on the dependencies of the SecondSU to
11009467b48Spatrick   // prevent them from being scheduled between the FirstSU and the SecondSU.
11109467b48Spatrick   if (&FirstSU != &DAG.EntrySU) {
11209467b48Spatrick     for (const SDep &SI : SecondSU.Preds) {
11309467b48Spatrick       SUnit *SU = SI.getSUnit();
11409467b48Spatrick       if (SI.isWeak() || isHazard(SI) || &FirstSU == SU || FirstSU.isSucc(SU))
11509467b48Spatrick         continue;
11609467b48Spatrick       LLVM_DEBUG(dbgs() << "  Bind "; DAG.dumpNodeName(*SU); dbgs() << " - ";
11709467b48Spatrick                  DAG.dumpNodeName(FirstSU); dbgs() << '\n';);
11809467b48Spatrick       DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
11909467b48Spatrick     }
12009467b48Spatrick     // ExitSU comes last by design, which acts like an implicit dependency
12109467b48Spatrick     // between ExitSU and any bottom root in the graph. We should transfer
12209467b48Spatrick     // this to FirstSU as well.
12309467b48Spatrick     if (&SecondSU == &DAG.ExitSU) {
12409467b48Spatrick       for (SUnit &SU : DAG.SUnits) {
12509467b48Spatrick         if (SU.Succs.empty())
12609467b48Spatrick           DAG.addEdge(&FirstSU, SDep(&SU, SDep::Artificial));
12709467b48Spatrick       }
12809467b48Spatrick     }
12909467b48Spatrick   }
13009467b48Spatrick 
13109467b48Spatrick   ++NumFused;
13209467b48Spatrick   return true;
13309467b48Spatrick }
13409467b48Spatrick 
13509467b48Spatrick namespace {
13609467b48Spatrick 
13709467b48Spatrick /// Post-process the DAG to create cluster edges between instrs that may
13809467b48Spatrick /// be fused by the processor into a single operation.
13909467b48Spatrick class MacroFusion : public ScheduleDAGMutation {
14009467b48Spatrick   ShouldSchedulePredTy shouldScheduleAdjacent;
14109467b48Spatrick   bool FuseBlock;
14209467b48Spatrick   bool scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU);
14309467b48Spatrick 
14409467b48Spatrick public:
MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent,bool FuseBlock)14509467b48Spatrick   MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
14609467b48Spatrick     : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
14709467b48Spatrick 
14809467b48Spatrick   void apply(ScheduleDAGInstrs *DAGInstrs) override;
14909467b48Spatrick };
15009467b48Spatrick 
15109467b48Spatrick } // end anonymous namespace
15209467b48Spatrick 
apply(ScheduleDAGInstrs * DAG)15309467b48Spatrick void MacroFusion::apply(ScheduleDAGInstrs *DAG) {
15409467b48Spatrick   if (FuseBlock)
15509467b48Spatrick     // For each of the SUnits in the scheduling block, try to fuse the instr in
15609467b48Spatrick     // it with one in its predecessors.
15709467b48Spatrick     for (SUnit &ISU : DAG->SUnits)
15809467b48Spatrick         scheduleAdjacentImpl(*DAG, ISU);
15909467b48Spatrick 
16009467b48Spatrick   if (DAG->ExitSU.getInstr())
16109467b48Spatrick     // Try to fuse the instr in the ExitSU with one in its predecessors.
16209467b48Spatrick     scheduleAdjacentImpl(*DAG, DAG->ExitSU);
16309467b48Spatrick }
16409467b48Spatrick 
16509467b48Spatrick /// Implement the fusion of instr pairs in the scheduling DAG,
16609467b48Spatrick /// anchored at the instr in AnchorSU..
scheduleAdjacentImpl(ScheduleDAGInstrs & DAG,SUnit & AnchorSU)16709467b48Spatrick bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU) {
16809467b48Spatrick   const MachineInstr &AnchorMI = *AnchorSU.getInstr();
16909467b48Spatrick   const TargetInstrInfo &TII = *DAG.TII;
17009467b48Spatrick   const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
17109467b48Spatrick 
17209467b48Spatrick   // Check if the anchor instr may be fused.
17309467b48Spatrick   if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
17409467b48Spatrick     return false;
17509467b48Spatrick 
17609467b48Spatrick   // Explorer for fusion candidates among the dependencies of the anchor instr.
17709467b48Spatrick   for (SDep &Dep : AnchorSU.Preds) {
17809467b48Spatrick     // Ignore dependencies other than data or strong ordering.
17909467b48Spatrick     if (Dep.isWeak() || isHazard(Dep))
18009467b48Spatrick       continue;
18109467b48Spatrick 
18209467b48Spatrick     SUnit &DepSU = *Dep.getSUnit();
18309467b48Spatrick     if (DepSU.isBoundaryNode())
18409467b48Spatrick       continue;
18509467b48Spatrick 
18609467b48Spatrick     // Only chain two instructions together at most.
18709467b48Spatrick     const MachineInstr *DepMI = DepSU.getInstr();
18809467b48Spatrick     if (!hasLessThanNumFused(DepSU, 2) ||
18909467b48Spatrick         !shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
19009467b48Spatrick       continue;
19109467b48Spatrick 
19209467b48Spatrick     if (fuseInstructionPair(DAG, DepSU, AnchorSU))
19309467b48Spatrick       return true;
19409467b48Spatrick   }
19509467b48Spatrick 
19609467b48Spatrick   return false;
19709467b48Spatrick }
19809467b48Spatrick 
19909467b48Spatrick std::unique_ptr<ScheduleDAGMutation>
createMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)20009467b48Spatrick llvm::createMacroFusionDAGMutation(
20109467b48Spatrick      ShouldSchedulePredTy shouldScheduleAdjacent) {
20209467b48Spatrick   if(EnableMacroFusion)
20309467b48Spatrick     return std::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
20409467b48Spatrick   return nullptr;
20509467b48Spatrick }
20609467b48Spatrick 
20709467b48Spatrick std::unique_ptr<ScheduleDAGMutation>
createBranchMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)20809467b48Spatrick llvm::createBranchMacroFusionDAGMutation(
20909467b48Spatrick      ShouldSchedulePredTy shouldScheduleAdjacent) {
21009467b48Spatrick   if(EnableMacroFusion)
21109467b48Spatrick     return std::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
21209467b48Spatrick   return nullptr;
21309467b48Spatrick }
214