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/Statistic.h" 17 #include "llvm/Support/CommandLine.h" 18 #include "llvm/Target/TargetInstrInfo.h" 19 20 #define DEBUG_TYPE "misched" 21 22 STATISTIC(NumFused, "Number of instr pairs fused"); 23 24 using namespace llvm; 25 26 static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden, 27 cl::desc("Enable scheduling for macro fusion."), cl::init(true)); 28 29 namespace { 30 31 static void fuseInstructionPair(ScheduleDAGMI &DAG, SUnit &FirstSU, 32 SUnit &SecondSU) { 33 // Create a single weak edge between the adjacent instrs. The only effect is 34 // to cause bottom-up scheduling to heavily prioritize the clustered instrs. 35 DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)); 36 37 // Adjust the latency between the anchor instr and its 38 // predecessors. 39 for (SDep &IDep : SecondSU.Preds) 40 if (IDep.getSUnit() == &FirstSU) 41 IDep.setLatency(0); 42 43 // Adjust the latency between the dependent instr and its 44 // predecessors. 45 for (SDep &IDep : FirstSU.Succs) 46 if (IDep.getSUnit() == &SecondSU) 47 IDep.setLatency(0); 48 49 DEBUG(dbgs() << DAG.MF.getName() << "(): Macro fuse "; 50 FirstSU.print(dbgs(), &DAG); dbgs() << " - "; 51 SecondSU.print(dbgs(), &DAG); dbgs() << " / "; 52 dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - " << 53 DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n'; ); 54 55 if (&SecondSU != &DAG.ExitSU) 56 // Make instructions dependent on FirstSU also dependent on SecondSU to 57 // prevent them from being scheduled between FirstSU and and SecondSU. 58 for (const SDep &SI : FirstSU.Succs) { 59 if (SI.getSUnit() == &SecondSU) 60 continue; 61 DEBUG(dbgs() << " Copy Succ "; 62 SI.getSUnit()->print(dbgs(), &DAG); dbgs() << '\n';); 63 DAG.addEdge(SI.getSUnit(), SDep(&SecondSU, SDep::Artificial)); 64 } 65 66 ++NumFused; 67 } 68 69 70 /// \brief Post-process the DAG to create cluster edges between instrs that may 71 /// be fused by the processor into a single operation. 72 class MacroFusion : public ScheduleDAGMutation { 73 ShouldSchedulePredTy shouldScheduleAdjacent; 74 bool FuseBlock; 75 bool scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU); 76 77 public: 78 MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock) 79 : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {} 80 81 void apply(ScheduleDAGInstrs *DAGInstrs) override; 82 }; 83 84 void MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) { 85 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs); 86 87 if (FuseBlock) 88 // For each of the SUnits in the scheduling block, try to fuse the instr in 89 // it with one in its predecessors. 90 for (SUnit &ISU : DAG->SUnits) 91 scheduleAdjacentImpl(*DAG, ISU); 92 93 if (DAG->ExitSU.getInstr()) 94 // Try to fuse the instr in the ExitSU with one in its predecessors. 95 scheduleAdjacentImpl(*DAG, DAG->ExitSU); 96 } 97 98 /// \brief Implement the fusion of instr pairs in the scheduling DAG, 99 /// anchored at the instr in AnchorSU.. 100 bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU) { 101 const MachineInstr &AnchorMI = *AnchorSU.getInstr(); 102 const TargetInstrInfo &TII = *DAG.TII; 103 const TargetSubtargetInfo &ST = DAG.MF.getSubtarget(); 104 105 // Check if the anchor instr may be fused. 106 if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI)) 107 return false; 108 109 // Explorer for fusion candidates among the dependencies of the anchor instr. 110 for (SDep &Dep : AnchorSU.Preds) { 111 // Ignore dependencies that don't enforce ordering. 112 if (Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output || 113 Dep.isWeak()) 114 continue; 115 116 SUnit &DepSU = *Dep.getSUnit(); 117 if (DepSU.isBoundaryNode()) 118 continue; 119 120 const MachineInstr *DepMI = DepSU.getInstr(); 121 if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI)) 122 continue; 123 124 fuseInstructionPair(DAG, DepSU, AnchorSU); 125 return true; 126 } 127 128 return false; 129 } 130 131 } // end anonymous namespace 132 133 134 namespace llvm { 135 136 std::unique_ptr<ScheduleDAGMutation> 137 createMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent) { 138 if(EnableMacroFusion) 139 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true); 140 return nullptr; 141 } 142 143 std::unique_ptr<ScheduleDAGMutation> 144 createBranchMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent) { 145 if(EnableMacroFusion) 146 return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false); 147 return nullptr; 148 } 149 150 } // end namespace llvm 151