LLVM  14.0.0git
MacroFusion.cpp
Go to the documentation of this file.
1 //===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file This file contains the implementation of the DAG scheduling mutation
10 /// to pair instructions back to back.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/Debug.h"
25 
26 #define DEBUG_TYPE "machine-scheduler"
27 
28 STATISTIC(NumFused, "Number of instr pairs fused");
29 
30 using namespace llvm;
31 
32 static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
33  cl::desc("Enable scheduling for macro fusion."), cl::init(true));
34 
35 static bool isHazard(const SDep &Dep) {
36  return Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output;
37 }
38 
39 static SUnit *getPredClusterSU(const SUnit &SU) {
40  for (const SDep &SI : SU.Preds)
41  if (SI.isCluster())
42  return SI.getSUnit();
43 
44  return nullptr;
45 }
46 
47 static bool hasLessThanNumFused(const SUnit &SU, unsigned FuseLimit) {
48  unsigned Num = 1;
49  const SUnit *CurrentSU = &SU;
50  while ((CurrentSU = getPredClusterSU(*CurrentSU)) && Num < FuseLimit) Num ++;
51  return Num < FuseLimit;
52 }
53 
54 static bool fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU,
55  SUnit &SecondSU) {
56  // Check that neither instr is already paired with another along the edge
57  // between them.
58  for (SDep &SI : FirstSU.Succs)
59  if (SI.isCluster())
60  return false;
61 
62  for (SDep &SI : SecondSU.Preds)
63  if (SI.isCluster())
64  return false;
65  // Though the reachability checks above could be made more generic,
66  // perhaps as part of ScheduleDAGInstrs::addEdge(), since such edges are valid,
67  // the extra computation cost makes it less interesting in general cases.
68 
69  // Create a single weak edge between the adjacent instrs. The only effect is
70  // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
71  if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
72  return false;
73 
74  // TODO - If we want to chain more than two instructions, we need to create
75  // artifical edges to make dependencies from the FirstSU also dependent
76  // on other chained instructions, and other chained instructions also
77  // dependent on the dependencies of the SecondSU, to prevent them from being
78  // scheduled into these chained instructions.
79  assert(hasLessThanNumFused(FirstSU, 2) &&
80  "Currently we only support chaining together two instructions");
81 
82  // Adjust the latency between both instrs.
83  for (SDep &SI : FirstSU.Succs)
84  if (SI.getSUnit() == &SecondSU)
85  SI.setLatency(0);
86 
87  for (SDep &SI : SecondSU.Preds)
88  if (SI.getSUnit() == &FirstSU)
89  SI.setLatency(0);
90 
91  LLVM_DEBUG(
92  dbgs() << "Macro fuse: "; DAG.dumpNodeName(FirstSU); dbgs() << " - ";
93  DAG.dumpNodeName(SecondSU); dbgs() << " / ";
94  dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - "
95  << DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n';);
96 
97  // Make data dependencies from the FirstSU also dependent on the SecondSU to
98  // prevent them from being scheduled between the FirstSU and the SecondSU.
99  if (&SecondSU != &DAG.ExitSU)
100  for (const SDep &SI : FirstSU.Succs) {
101  SUnit *SU = SI.getSUnit();
102  if (SI.isWeak() || isHazard(SI) ||
103  SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
104  continue;
105  LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(SecondSU);
106  dbgs() << " - "; DAG.dumpNodeName(*SU); dbgs() << '\n';);
107  DAG.addEdge(SU, SDep(&SecondSU, SDep::Artificial));
108  }
109 
110  // Make the FirstSU also dependent on the dependencies of the SecondSU to
111  // prevent them from being scheduled between the FirstSU and the SecondSU.
112  if (&FirstSU != &DAG.EntrySU) {
113  for (const SDep &SI : SecondSU.Preds) {
114  SUnit *SU = SI.getSUnit();
115  if (SI.isWeak() || isHazard(SI) || &FirstSU == SU || FirstSU.isSucc(SU))
116  continue;
117  LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(*SU); dbgs() << " - ";
118  DAG.dumpNodeName(FirstSU); dbgs() << '\n';);
119  DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
120  }
121  // ExitSU comes last by design, which acts like an implicit dependency
122  // between ExitSU and any bottom root in the graph. We should transfer
123  // this to FirstSU as well.
124  if (&SecondSU == &DAG.ExitSU) {
125  for (SUnit &SU : DAG.SUnits) {
126  if (SU.Succs.empty())
127  DAG.addEdge(&FirstSU, SDep(&SU, SDep::Artificial));
128  }
129  }
130  }
131 
132  ++NumFused;
133  return true;
134 }
135 
136 namespace {
137 
138 /// Post-process the DAG to create cluster edges between instrs that may
139 /// be fused by the processor into a single operation.
140 class MacroFusion : public ScheduleDAGMutation {
142  bool FuseBlock;
143  bool scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU);
144 
145 public:
146  MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
147  : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
148 
149  void apply(ScheduleDAGInstrs *DAGInstrs) override;
150 };
151 
152 } // end anonymous namespace
153 
155  if (FuseBlock)
156  // For each of the SUnits in the scheduling block, try to fuse the instr in
157  // it with one in its predecessors.
158  for (SUnit &ISU : DAG->SUnits)
159  scheduleAdjacentImpl(*DAG, ISU);
160 
161  if (DAG->ExitSU.getInstr())
162  // Try to fuse the instr in the ExitSU with one in its predecessors.
163  scheduleAdjacentImpl(*DAG, DAG->ExitSU);
164 }
165 
166 /// Implement the fusion of instr pairs in the scheduling DAG,
167 /// anchored at the instr in AnchorSU..
168 bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU) {
169  const MachineInstr &AnchorMI = *AnchorSU.getInstr();
170  const TargetInstrInfo &TII = *DAG.TII;
171  const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
172 
173  // Check if the anchor instr may be fused.
174  if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
175  return false;
176 
177  // Explorer for fusion candidates among the dependencies of the anchor instr.
178  for (SDep &Dep : AnchorSU.Preds) {
179  // Ignore dependencies other than data or strong ordering.
180  if (Dep.isWeak() || isHazard(Dep))
181  continue;
182 
183  SUnit &DepSU = *Dep.getSUnit();
184  if (DepSU.isBoundaryNode())
185  continue;
186 
187  // Only chain two instructions together at most.
188  const MachineInstr *DepMI = DepSU.getInstr();
189  if (!hasLessThanNumFused(DepSU, 2) ||
190  !shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
191  continue;
192 
193  if (fuseInstructionPair(DAG, DepSU, AnchorSU))
194  return true;
195  }
196 
197  return false;
198 }
199 
200 std::unique_ptr<ScheduleDAGMutation>
204  return std::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
205  return nullptr;
206 }
207 
208 std::unique_ptr<ScheduleDAGMutation>
212  return std::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
213  return nullptr;
214 }
ScheduleDAG.h
MachineInstr.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::SDep::Artificial
@ Artificial
Arbitrary strong DAG edge (no real dependence).
Definition: ScheduleDAG.h:72
Statistic.h
llvm::createBranchMacroFusionDAGMutation
std::unique_ptr< ScheduleDAGMutation > createBranchMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)
Create a DAG scheduling mutation to pair branch instructions with one of their predecessors back to b...
Definition: MacroFusion.cpp:209
llvm::SDep::Anti
@ Anti
A register anti-dependence (aka WAR).
Definition: ScheduleDAG.h:54
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::ScheduleDAGInstrs::addEdge
bool addEdge(SUnit *SuccSU, const SDep &PredDep)
Add a DAG edge to the given SU with the given predecessor dependence data.
Definition: ScheduleDAGInstrs.cpp:1205
TargetInstrInfo.h
llvm::SUnit::Succs
SmallVector< SDep, 4 > Succs
All sunit successors.
Definition: ScheduleDAG.h:257
STLExtras.h
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
llvm::SUnit::isBoundaryNode
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
Definition: ScheduleDAG.h:344
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:97
llvm::cl::apply
void apply(Opt *O, const Mod &M, const Mods &... Ms)
Definition: CommandLine.h:1320
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::SDep::Output
@ Output
A register output-dependence (aka WAW).
Definition: ScheduleDAG.h:55
llvm::createMacroFusionDAGMutation
std::unique_ptr< ScheduleDAGMutation > createMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)
Create a DAG scheduling mutation to pair instructions back to back for instructions that benefit acco...
Definition: MacroFusion.cpp:201
llvm::shouldScheduleAdjacent
static bool shouldScheduleAdjacent(const TargetInstrInfo &TII, const TargetSubtargetInfo &TSI, const MachineInstr *FirstMI, const MachineInstr &SecondMI)
Check if the instr pair, FirstMI and SecondMI, should be fused together.
Definition: ARMMacroFusion.cpp:51
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:626
llvm::cl::opt< bool >
llvm::SUnit::isPred
bool isPred(const SUnit *N) const
Tests if node N is a predecessor of this node.
Definition: ScheduleDAG.h:431
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
MacroFusion.h
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
llvm::SUnit::getInstr
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
Definition: ScheduleDAG.h:373
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SI
StandardInstrumentations SI(Debug, VerifyEach)
hasLessThanNumFused
static bool hasLessThanNumFused(const SUnit &SU, unsigned FuseLimit)
Definition: MacroFusion.cpp:47
llvm::MCInstrInfo::getName
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:68
ScheduleDAGMutation.h
llvm::SDep::getSUnit
SUnit * getSUnit() const
Definition: ScheduleDAG.h:480
llvm::MachineInstr::getOpcode
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:489
llvm::ScheduleDAG::MF
MachineFunction & MF
Machine function.
Definition: ScheduleDAG.h:560
llvm::ScheduleDAG::EntrySU
SUnit EntrySU
Special node for the region entry.
Definition: ScheduleDAG.h:563
EnableMacroFusion
static cl::opt< bool > EnableMacroFusion("misched-fusion", cl::Hidden, cl::desc("Enable scheduling for macro fusion."), cl::init(true))
llvm::TargetSubtargetInfo
TargetSubtargetInfo - Generic base class for all target subtargets.
Definition: TargetSubtargetInfo.h:59
llvm::SDep
Scheduling dependency.
Definition: ScheduleDAG.h:49
llvm::ScheduleDAG::SUnits
std::vector< SUnit > SUnits
The scheduling units.
Definition: ScheduleDAG.h:562
getPredClusterSU
static SUnit * getPredClusterSU(const SUnit &SU)
Definition: MacroFusion.cpp:39
llvm::ScheduleDAG::TII
const TargetInstrInfo * TII
Target instruction information.
Definition: ScheduleDAG.h:558
llvm::SDep::isWeak
bool isWeak() const
Tests if this a weak dependence.
Definition: ScheduleDAG.h:194
fuseInstructionPair
static bool fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU, SUnit &SecondSU)
Definition: MacroFusion.cpp:54
llvm::SDep::getKind
Kind getKind() const
Returns an enum value representing the kind of the dependence.
Definition: ScheduleDAG.h:486
llvm::SUnit::isSucc
bool isSucc(const SUnit *N) const
Tests if node N is a successor of this node.
Definition: ScheduleDAG.h:439
MachineScheduler.h
llvm::ScheduleDAG::dumpNodeName
void dumpNodeName(const SUnit &SU) const
Definition: ScheduleDAG.cpp:354
llvm::SDep::Cluster
@ Cluster
Weak DAG edge linking a chain of clustered instrs.
Definition: ScheduleDAG.h:74
llvm::ScheduleDAGMutation
Mutate the DAG as a postpass after normal DAG building.
Definition: ScheduleDAGMutation.h:22
isHazard
static bool isHazard(const SDep &Dep)
Definition: MacroFusion.cpp:35
llvm::SUnit::Preds
SmallVector< SDep, 4 > Preds
All sunit predecessors.
Definition: ScheduleDAG.h:256
llvm::SUnit
Scheduling unit. This is a node in the scheduling DAG.
Definition: ScheduleDAG.h:242
llvm::cl::desc
Definition: CommandLine.h:414
llvm::ScheduleDAGInstrs
A ScheduleDAG for scheduling lists of MachineInstr.
Definition: ScheduleDAGInstrs.h:119
raw_ostream.h
STATISTIC
STATISTIC(NumFused, "Number of instr pairs fused")
Debug.h
llvm::ScheduleDAG::ExitSU
SUnit ExitSU
Special node for the region exit.
Definition: ScheduleDAG.h:564
llvm::ShouldSchedulePredTy
std::function< bool(const TargetInstrInfo &TII, const TargetSubtargetInfo &TSI, const MachineInstr *FirstMI, const MachineInstr &SecondMI)> ShouldSchedulePredTy
Check if the instr pair, FirstMI and SecondMI, should be fused together.
Definition: MacroFusion.h:33