LLVM  9.0.0svn
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 bool fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU,
40  SUnit &SecondSU) {
41  // 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 ScheduleDAGInstrs::addEdge(), since such edges are valid,
52  // the extra computation cost makes it less interesting in general cases.
53 
54  // 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.
56  if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
57  return false;
58 
59  // Adjust the latency between both instrs.
60  for (SDep &SI : FirstSU.Succs)
61  if (SI.getSUnit() == &SecondSU)
62  SI.setLatency(0);
63 
64  for (SDep &SI : SecondSU.Preds)
65  if (SI.getSUnit() == &FirstSU)
66  SI.setLatency(0);
67 
68  LLVM_DEBUG(
69  dbgs() << "Macro fuse: "; DAG.dumpNodeName(FirstSU); dbgs() << " - ";
70  DAG.dumpNodeName(SecondSU); dbgs() << " / ";
71  dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - "
72  << DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n';);
73 
74  // Make data dependencies from the FirstSU also dependent on the SecondSU to
75  // prevent them from being scheduled between the FirstSU and the SecondSU.
76  if (&SecondSU != &DAG.ExitSU)
77  for (const SDep &SI : FirstSU.Succs) {
78  SUnit *SU = SI.getSUnit();
79  if (SI.isWeak() || isHazard(SI) ||
80  SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
81  continue;
82  LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(SecondSU);
83  dbgs() << " - "; DAG.dumpNodeName(*SU); dbgs() << '\n';);
84  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.
89  if (&FirstSU != &DAG.EntrySU) {
90  for (const SDep &SI : SecondSU.Preds) {
91  SUnit *SU = SI.getSUnit();
92  if (SI.isWeak() || isHazard(SI) || &FirstSU == SU || FirstSU.isSucc(SU))
93  continue;
94  LLVM_DEBUG(dbgs() << " Bind "; DAG.dumpNodeName(*SU); dbgs() << " - ";
95  DAG.dumpNodeName(FirstSU); dbgs() << '\n';);
96  DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
97  }
98  // 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  }
108 
109  ++NumFused;
110  return true;
111 }
112 
113 namespace {
114 
115 /// Post-process the DAG to create cluster edges between instrs that may
116 /// be fused by the processor into a single operation.
117 class MacroFusion : public ScheduleDAGMutation {
119  bool FuseBlock;
120  bool scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU);
121 
122 public:
123  MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
124  : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
125 
126  void apply(ScheduleDAGInstrs *DAGInstrs) override;
127 };
128 
129 } // end anonymous namespace
130 
132  if (FuseBlock)
133  // For each of the SUnits in the scheduling block, try to fuse the instr in
134  // it with one in its predecessors.
135  for (SUnit &ISU : DAG->SUnits)
136  scheduleAdjacentImpl(*DAG, ISU);
137 
138  if (DAG->ExitSU.getInstr())
139  // Try to fuse the instr in the ExitSU with one in its predecessors.
140  scheduleAdjacentImpl(*DAG, DAG->ExitSU);
141 }
142 
143 /// Implement the fusion of instr pairs in the scheduling DAG,
144 /// anchored at the instr in AnchorSU..
145 bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU) {
146  const MachineInstr &AnchorMI = *AnchorSU.getInstr();
147  const TargetInstrInfo &TII = *DAG.TII;
148  const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
149 
150  // Check if the anchor instr may be fused.
151  if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
152  return false;
153 
154  // Explorer for fusion candidates among the dependencies of the anchor instr.
155  for (SDep &Dep : AnchorSU.Preds) {
156  // Ignore dependencies other than data or strong ordering.
157  if (Dep.isWeak() || isHazard(Dep))
158  continue;
159 
160  SUnit &DepSU = *Dep.getSUnit();
161  if (DepSU.isBoundaryNode())
162  continue;
163 
164  const MachineInstr *DepMI = DepSU.getInstr();
165  if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
166  continue;
167 
168  if (fuseInstructionPair(DAG, DepSU, AnchorSU))
169  return true;
170  }
171 
172  return false;
173 }
174 
175 std::unique_ptr<ScheduleDAGMutation>
179  return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
180  return nullptr;
181 }
182 
183 std::unique_ptr<ScheduleDAGMutation>
187  return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
188  return nullptr;
189 }
Weak DAG edge linking a chain of clustered instrs.
Definition: ScheduleDAG.h:74
This class represents lattice values for constants.
Definition: AllocatorList.h:23
STATISTIC(NumFused, "Number of instr pairs fused")
Mutate the DAG as a postpass after normal DAG building.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
Definition: ScheduleDAG.h:256
A register anti-dependence (aka WAR).
Definition: ScheduleDAG.h:54
MachineFunction & MF
Machine function.
Definition: ScheduleDAG.h:560
const HexagonInstrInfo * TII
void apply(Opt *O, const Mod &M, const Mods &... Ms)
Definition: CommandLine.h:1217
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:408
std::unique_ptr< ScheduleDAGMutation > createMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)
Create a DAG scheduling mutation to pair instructions back to back for instructions that benefit acco...
A register output-dependence (aka WAW).
Definition: ScheduleDAG.h:55
SUnit * getSUnit() const
Definition: ScheduleDAG.h:480
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
Definition: ScheduleDAG.h:344
TargetInstrInfo - Interface to description of machine instruction set.
Scheduling dependency.
Definition: ScheduleDAG.h:49
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
Definition: ScheduleDAG.h:373
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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
void dumpNodeName(const SUnit &SU) const
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:50
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.
bool addEdge(SUnit *SuccSU, const SDep &PredDep)
Add a DAG edge to the given SU with the given predecessor dependence data.
std::unique_ptr< ScheduleDAGMutation > createBranchMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent)
Create a DAG scheduling mutation to pair branch instructions with one of their predecessors back to b...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static bool isHazard(const SDep &Dep)
Definition: MacroFusion.cpp:35
static cl::opt< bool > EnableMacroFusion("misched-fusion", cl::Hidden, cl::desc("Enable scheduling for macro fusion."), cl::init(true))
void setLatency(unsigned Lat)
Sets the latency for this edge.
Definition: ScheduleDAG.h:147
TargetSubtargetInfo - Generic base class for all target subtargets.
SUnit EntrySU
Special node for the region entry.
Definition: ScheduleDAG.h:563
A ScheduleDAG for scheduling lists of MachineInstr.
Representation of each machine instruction.
Definition: MachineInstr.h:63
SUnit ExitSU
Special node for the region exit.
Definition: ScheduleDAG.h:564
const TargetInstrInfo * TII
Target instruction information.
Definition: ScheduleDAG.h:558
Kind getKind() const
Returns an enum value representing the kind of the dependence.
Definition: ScheduleDAG.h:486
SmallVector< SDep, 4 > Succs
All sunit successors.
Definition: ScheduleDAG.h:257
bool isCluster() const
Tests if this is an Order dependence that is marked as "cluster", meaning it is artificial and wants ...
Definition: ScheduleDAG.h:206
Arbitrary strong DAG edge (no real dependence).
Definition: ScheduleDAG.h:72
bool isWeak() const
Tests if this a weak dependence.
Definition: ScheduleDAG.h:194
static bool fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU, SUnit &SecondSU)
Definition: MacroFusion.cpp:39
std::vector< SUnit > SUnits
The scheduling units.
Definition: ScheduleDAG.h:562
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool isSucc(const SUnit *N) const
Tests if node N is a successor of this node.
Definition: ScheduleDAG.h:439
Scheduling unit. This is a node in the scheduling DAG.
Definition: ScheduleDAG.h:242
bool isPred(const SUnit *N) const
Tests if node N is a predecessor of this node.
Definition: ScheduleDAG.h:431