LCOV - code coverage report
Current view: top level - lib/CodeGen - MacroFusion.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 50 51 98.0 %
Date: 2017-09-14 15:23:50 Functions: 8 9 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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       72306 : static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
      34      216918 :   cl::desc("Enable scheduling for macro fusion."), cl::init(true));
      35             : 
      36       18648 : 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       18648 :   DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster));
      41             : 
      42             :   // Adjust the latency between the anchor instr and its
      43             :   // predecessors.
      44      121429 :   for (SDep &IDep : SecondSU.Preds)
      45       65485 :     if (IDep.getSUnit() == &FirstSU)
      46       37556 :       IDep.setLatency(0);
      47             : 
      48             :   // Adjust the latency between the dependent instr and its
      49             :   // predecessors.
      50      102624 :   for (SDep &IDep : FirstSU.Succs)
      51       46680 :     if (IDep.getSUnit() == &SecondSU)
      52       37556 :       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       18648 :   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       52642 :     for (const SDep &SI : FirstSU.Succs) {
      64       22884 :       if (SI.getSUnit() == &SecondSU)
      65       14008 :         continue;
      66             :       DEBUG(dbgs() << "  Copy Succ ";
      67             :             SI.getSUnit()->print(dbgs(), &DAG); dbgs() << '\n';);
      68       17752 :       DAG.addEdge(SI.getSUnit(), SDep(&SecondSU, SDep::Artificial));
      69             :     }
      70             : 
      71       18648 :   ++NumFused;
      72       18648 : }
      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      204936 : 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      204936 :     : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
      86             : 
      87             :   void apply(ScheduleDAGInstrs *DAGInstrs) override;
      88             : };
      89             : 
      90             : } // end anonymous namespace
      91             : 
      92      341037 : void MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) {
      93      341037 :   ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
      94             : 
      95      341037 :   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      485709 :     for (SUnit &ISU : DAG->SUnits)
      99      350465 :         scheduleAdjacentImpl(*DAG, ISU);
     100             : 
     101      341037 :   if (DAG->ExitSU.getInstr())
     102             :     // Try to fuse the instr in the ExitSU with one in its predecessors.
     103      331991 :     scheduleAdjacentImpl(*DAG, DAG->ExitSU);
     104      341037 : }
     105             : 
     106             : /// \brief Implement the fusion of instr pairs in the scheduling DAG,
     107             : /// anchored at the instr in AnchorSU..
     108      682456 : bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU) {
     109      682456 :   const MachineInstr &AnchorMI = *AnchorSU.getInstr();
     110      682456 :   const TargetInstrInfo &TII = *DAG.TII;
     111      682456 :   const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
     112             : 
     113             :   // Check if the anchor instr may be fused.
     114     1364912 :   if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
     115             :     return false;
     116             : 
     117             :   // Explorer for fusion candidates among the dependencies of the anchor instr.
     118       60863 :   for (SDep &Dep : AnchorSU.Preds) {
     119             :     // Ignore dependencies that don't enforce ordering.
     120       44455 :     if (Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output ||
     121       22027 :         Dep.isWeak())
     122         139 :       continue;
     123             : 
     124       22027 :     SUnit &DepSU = *Dep.getSUnit();
     125       22027 :     if (DepSU.isBoundaryNode())
     126           0 :       continue;
     127             : 
     128       22027 :     const MachineInstr *DepMI = DepSU.getInstr();
     129       44054 :     if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
     130        3379 :       continue;
     131             : 
     132       18648 :     fuseInstructionPair(DAG, DepSU, AnchorSU);
     133       18648 :     return true;
     134             :   }
     135             : 
     136             :   return false;
     137             : }
     138             : 
     139             : std::unique_ptr<ScheduleDAGMutation>
     140       33683 : llvm::createMacroFusionDAGMutation(
     141             :      ShouldSchedulePredTy shouldScheduleAdjacent) {
     142       33683 :   if(EnableMacroFusion)
     143      101049 :     return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
     144             :   return nullptr;
     145             : }
     146             : 
     147             : std::unique_ptr<ScheduleDAGMutation>
     148       68785 : llvm::createBranchMacroFusionDAGMutation(
     149             :      ShouldSchedulePredTy shouldScheduleAdjacent) {
     150       68785 :   if(EnableMacroFusion)
     151      206355 :     return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
     152             :   return nullptr;
     153      216918 : }

Generated by: LCOV version 1.13