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 15 #include "llvm/CodeGen/MacroFusion.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/Statistic.h" 18 #include "llvm/CodeGen/MachineInstr.h" 19 #include "llvm/CodeGen/MachineScheduler.h" 20 #include "llvm/CodeGen/ScheduleDAG.h" 21 #include "llvm/CodeGen/ScheduleDAGMutation.h" 22 #include "llvm/Support/CommandLine.h" 23 #include "llvm/Support/Debug.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include "llvm/Target/TargetInstrInfo.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 { 79 ShouldSchedulePredTy shouldScheduleAdjacent; 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> 140 llvm::createMacroFusionDAGMutation( 141 ShouldSchedulePredTy shouldScheduleAdjacent) { 142 if(EnableMacroFusion) 143 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true); 144 return nullptr; 145 } 146 147 std::unique_ptr<ScheduleDAGMutation> 148 llvm::createBranchMacroFusionDAGMutation( 149 ShouldSchedulePredTy shouldScheduleAdjacent) { 150 if(EnableMacroFusion) 151 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false); 152 return nullptr; 153 } 154