LLVM  5.0.0
MacroFusion.cpp
Go to the documentation of this file.
1 //===- MacroFusion.cpp - Macro Fusion -------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file This file contains the implementation of the DAG scheduling mutation
11 /// to pair instructions back to back.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/Debug.h"
26 
27 #define DEBUG_TYPE "machine-scheduler"
28 
29 STATISTIC(NumFused, "Number of instr pairs fused");
30 
31 using namespace llvm;
32 
33 static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
34  cl::desc("Enable scheduling for macro fusion."), cl::init(true));
35 
36 static void fuseInstructionPair(ScheduleDAGMI &DAG, SUnit &FirstSU,
37  SUnit &SecondSU) {
38  // Create a single weak edge between the adjacent instrs. The only effect is
39  // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
40  DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster));
41 
42  // Adjust the latency between the anchor instr and its
43  // predecessors.
44  for (SDep &IDep : SecondSU.Preds)
45  if (IDep.getSUnit() == &FirstSU)
46  IDep.setLatency(0);
47 
48  // Adjust the latency between the dependent instr and its
49  // predecessors.
50  for (SDep &IDep : FirstSU.Succs)
51  if (IDep.getSUnit() == &SecondSU)
52  IDep.setLatency(0);
53 
54  DEBUG(dbgs() << DAG.MF.getName() << "(): Macro fuse ";
55  FirstSU.print(dbgs(), &DAG); dbgs() << " - ";
56  SecondSU.print(dbgs(), &DAG); dbgs() << " / ";
57  dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - " <<
58  DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n'; );
59 
60  if (&SecondSU != &DAG.ExitSU)
61  // Make instructions dependent on FirstSU also dependent on SecondSU to
62  // prevent them from being scheduled between FirstSU and and SecondSU.
63  for (const SDep &SI : FirstSU.Succs) {
64  if (SI.getSUnit() == &SecondSU)
65  continue;
66  DEBUG(dbgs() << " Copy Succ ";
67  SI.getSUnit()->print(dbgs(), &DAG); dbgs() << '\n';);
68  DAG.addEdge(SI.getSUnit(), SDep(&SecondSU, SDep::Artificial));
69  }
70 
71  ++NumFused;
72 }
73 
74 namespace {
75 
76 /// \brief Post-process the DAG to create cluster edges between instrs that may
77 /// be fused by the processor into a single operation.
78 class MacroFusion : public ScheduleDAGMutation {
80  bool FuseBlock;
81  bool scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU);
82 
83 public:
84  MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
85  : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
86 
87  void apply(ScheduleDAGInstrs *DAGInstrs) override;
88 };
89 
90 } // end anonymous namespace
91 
92 void MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) {
93  ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
94 
95  if (FuseBlock)
96  // For each of the SUnits in the scheduling block, try to fuse the instr in
97  // it with one in its predecessors.
98  for (SUnit &ISU : DAG->SUnits)
99  scheduleAdjacentImpl(*DAG, ISU);
100 
101  if (DAG->ExitSU.getInstr())
102  // Try to fuse the instr in the ExitSU with one in its predecessors.
103  scheduleAdjacentImpl(*DAG, DAG->ExitSU);
104 }
105 
106 /// \brief Implement the fusion of instr pairs in the scheduling DAG,
107 /// anchored at the instr in AnchorSU..
108 bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU) {
109  const MachineInstr &AnchorMI = *AnchorSU.getInstr();
110  const TargetInstrInfo &TII = *DAG.TII;
111  const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
112 
113  // Check if the anchor instr may be fused.
114  if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
115  return false;
116 
117  // Explorer for fusion candidates among the dependencies of the anchor instr.
118  for (SDep &Dep : AnchorSU.Preds) {
119  // Ignore dependencies that don't enforce ordering.
120  if (Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output ||
121  Dep.isWeak())
122  continue;
123 
124  SUnit &DepSU = *Dep.getSUnit();
125  if (DepSU.isBoundaryNode())
126  continue;
127 
128  const MachineInstr *DepMI = DepSU.getInstr();
129  if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
130  continue;
131 
132  fuseInstructionPair(DAG, DepSU, AnchorSU);
133  return true;
134  }
135 
136  return false;
137 }
138 
139 std::unique_ptr<ScheduleDAGMutation>
143  return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
144  return nullptr;
145 }
146 
147 std::unique_ptr<ScheduleDAGMutation>
151  return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
152  return nullptr;
153 }
Weak DAG edge linking a chain of clustered instrs.
Definition: ScheduleDAG.h:75
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
Definition: ScheduleDAG.h:378
Mutate the DAG as a postpass after normal DAG building.
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
SmallVector< SDep, 4 > Preds
All sunit predecessors.
Definition: ScheduleDAG.h:261
A register anti-dependence (aka WAR).
Definition: ScheduleDAG.h:55
MachineFunction & MF
Machine function.
Definition: ScheduleDAG.h:570
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isWeak() const
Tests if this a weak dependence.
Definition: ScheduleDAG.h:195
const HexagonInstrInfo * TII
Kind getKind() const
Returns an enum value representing the kind of the dependence.
Definition: ScheduleDAG.h:496
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:51
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:56
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
TargetInstrInfo - Interface to description of machine instruction set.
Scheduling dependency.
Definition: ScheduleDAG.h:50
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
bool addEdge(SUnit *SuccSU, const SDep &PredDep)
Add a DAG edge to the given SU with the given predecessor dependence data.
raw_ostream & print(raw_ostream &O, const SUnit *N=nullptr, const SUnit *X=nullptr) const
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:34
void apply(Opt *O, const Mod &M, const Mods &...Ms)
Definition: CommandLine.h:1164
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.
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
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
Definition: ScheduleDAG.h:349
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:148
TargetSubtargetInfo - Generic base class for all target subtargets.
static void fuseInstructionPair(ScheduleDAGMI &DAG, SUnit &FirstSU, SUnit &SecondSU)
Definition: MacroFusion.cpp:36
A ScheduleDAG for scheduling lists of MachineInstr.
Representation of each machine instruction.
Definition: MachineInstr.h:59
SUnit ExitSU
Special node for the region exit.
Definition: ScheduleDAG.h:574
SUnit * getSUnit() const
Definition: ScheduleDAG.h:490
const TargetInstrInfo * TII
Target instruction information.
Definition: ScheduleDAG.h:568
SmallVector< SDep, 4 > Succs
All sunit successors.
Definition: ScheduleDAG.h:262
Arbitrary strong DAG edge (no real dependence).
Definition: ScheduleDAG.h:73
#define DEBUG(X)
Definition: Debug.h:118
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
std::vector< SUnit > SUnits
The scheduling units.
Definition: ScheduleDAG.h:572
Scheduling unit. This is a node in the scheduling DAG.
Definition: ScheduleDAG.h:247
STATISTIC(NumFused,"Number of instr pairs fused")