LCOV - code coverage report
Current view: top level - lib/CodeGen - MacroFusion.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 51 52 98.1 %
Date: 2018-06-17 00:07:59 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/CodeGen/TargetInstrInfo.h"
      23             : #include "llvm/Support/CommandLine.h"
      24             : #include "llvm/Support/Debug.h"
      25             : #include "llvm/Support/raw_ostream.h"
      26             : 
      27             : #define DEBUG_TYPE "machine-scheduler"
      28             : 
      29             : STATISTIC(NumFused, "Number of instr pairs fused");
      30             : 
      31             : using namespace llvm;
      32             : 
      33      101169 : static cl::opt<bool> EnableMacroFusion("misched-fusion", cl::Hidden,
      34      202338 :   cl::desc("Enable scheduling for macro fusion."), cl::init(true));
      35             : 
      36             : static bool isHazard(const SDep &Dep) {
      37       93796 :   return Dep.getKind() == SDep::Anti || Dep.getKind() == SDep::Output;
      38             : }
      39             : 
      40       22867 : static bool fuseInstructionPair(ScheduleDAGMI &DAG, SUnit &FirstSU,
      41             :                                 SUnit &SecondSU) {
      42             :   // Check that neither instr is already paired with another along the edge
      43             :   // between them.
      44       89437 :   for (SDep &SI : FirstSU.Succs)
      45             :     if (SI.isCluster())
      46             :       return false;
      47             : 
      48      115211 :   for (SDep &SI : SecondSU.Preds)
      49             :     if (SI.isCluster())
      50             :       return false;
      51             :   // Though the reachability checks above could be made more generic,
      52             :   // perhaps as part of ScheduleDAGMI::addEdge(), since such edges are valid,
      53             :   // the extra computation cost makes it less interesting in general cases.
      54             : 
      55             :   // Create a single weak edge between the adjacent instrs. The only effect is
      56             :   // to cause bottom-up scheduling to heavily prioritize the clustered instrs.
      57       21635 :   if (!DAG.addEdge(&SecondSU, SDep(&FirstSU, SDep::Cluster)))
      58             :     return false;
      59             : 
      60             :   // Adjust the latency between both instrs.
      61      123193 :   for (SDep &SI : FirstSU.Succs)
      62       50779 :     if (SI.getSUnit() == &SecondSU)
      63             :       SI.setLatency(0);
      64             : 
      65      158481 :   for (SDep &SI : SecondSU.Preds)
      66       68423 :     if (SI.getSUnit() == &FirstSU)
      67             :       SI.setLatency(0);
      68             : 
      69             :   LLVM_DEBUG(
      70             :       dbgs() << "Macro fuse: "; FirstSU.print(dbgs(), &DAG); dbgs() << " - ";
      71             :       SecondSU.print(dbgs(), &DAG); dbgs() << " /  ";
      72             :       dbgs() << DAG.TII->getName(FirstSU.getInstr()->getOpcode()) << " - "
      73             :              << DAG.TII->getName(SecondSU.getInstr()->getOpcode()) << '\n';);
      74             : 
      75             :   // Make data dependencies from the FirstSU also dependent on the SecondSU to
      76             :   // prevent them from being scheduled between the FirstSU and the SecondSU.
      77       21635 :   if (&SecondSU != &DAG.ExitSU)
      78       53880 :     for (const SDep &SI : FirstSU.Succs) {
      79             :       SUnit *SU = SI.getSUnit();
      80       34605 :       if (SI.isWeak() || isHazard(SI) ||
      81       21584 :           SU == &DAG.ExitSU || SU == &SecondSU || SU->isPred(&SecondSU))
      82       19886 :         continue;
      83             :       LLVM_DEBUG(dbgs() << "  Bind "; SecondSU.print(dbgs(), &DAG);
      84             :                  dbgs() << " - "; SU->print(dbgs(), &DAG); dbgs() << '\n';);
      85        3127 :       DAG.addEdge(SU, SDep(&SecondSU, SDep::Artificial));
      86             :     }
      87             : 
      88             :   // Make the FirstSU also dependent on the dependencies of the SecondSU to
      89             :   // prevent them from being scheduled between the FirstSU and the SecondSU.
      90       21635 :   if (&FirstSU != &DAG.EntrySU)
      91      158481 :     for (const SDep &SI : SecondSU.Preds) {
      92             :       SUnit *SU = SI.getSUnit();
      93      114794 :       if (SI.isWeak() || isHazard(SI) || &FirstSU == SU || FirstSU.isSucc(SU))
      94       43687 :         continue;
      95             :       LLVM_DEBUG(dbgs() << "  Bind "; SU->print(dbgs(), &DAG); dbgs() << " - ";
      96             :                  FirstSU.print(dbgs(), &DAG); dbgs() << '\n';);
      97       24736 :       DAG.addEdge(&FirstSU, SDep(SU, SDep::Artificial));
      98             :     }
      99             : 
     100             :   ++NumFused;
     101             :   return true;
     102             : }
     103             : 
     104             : namespace {
     105             : 
     106             : /// Post-process the DAG to create cluster edges between instrs that may
     107             : /// be fused by the processor into a single operation.
     108      282128 : class MacroFusion : public ScheduleDAGMutation {
     109             :   ShouldSchedulePredTy shouldScheduleAdjacent;
     110             :   bool FuseBlock;
     111             :   bool scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU);
     112             : 
     113             : public:
     114             :   MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
     115      282128 :     : shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
     116             : 
     117             :   void apply(ScheduleDAGInstrs *DAGInstrs) override;
     118             : };
     119             : 
     120             : } // end anonymous namespace
     121             : 
     122      350787 : void MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) {
     123             :   ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
     124             : 
     125      350787 :   if (FuseBlock)
     126             :     // For each of the SUnits in the scheduling block, try to fuse the instr in
     127             :     // it with one in its predecessors.
     128      440256 :     for (SUnit &ISU : DAG->SUnits)
     129      400255 :         scheduleAdjacentImpl(*DAG, ISU);
     130             : 
     131      350787 :   if (DAG->ExitSU.getInstr())
     132             :     // Try to fuse the instr in the ExitSU with one in its predecessors.
     133      340737 :     scheduleAdjacentImpl(*DAG, DAG->ExitSU);
     134      350787 : }
     135             : 
     136             : /// Implement the fusion of instr pairs in the scheduling DAG,
     137             : /// anchored at the instr in AnchorSU..
     138      740992 : bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGMI &DAG, SUnit &AnchorSU) {
     139      740992 :   const MachineInstr &AnchorMI = *AnchorSU.getInstr();
     140      740992 :   const TargetInstrInfo &TII = *DAG.TII;
     141      740992 :   const TargetSubtargetInfo &ST = DAG.MF.getSubtarget();
     142             : 
     143             :   // Check if the anchor instr may be fused.
     144      740992 :   if (!shouldScheduleAdjacent(TII, ST, nullptr, AnchorMI))
     145             :     return false;
     146             : 
     147             :   // Explorer for fusion candidates among the dependencies of the anchor instr.
     148       43884 :   for (SDep &Dep : AnchorSU.Preds) {
     149             :     // Ignore dependencies other than data or strong ordering.
     150         243 :     if (Dep.isWeak() || isHazard(Dep))
     151         243 :       continue;
     152             : 
     153             :     SUnit &DepSU = *Dep.getSUnit();
     154       31623 :     if (DepSU.isBoundaryNode())
     155           0 :       continue;
     156             : 
     157       31623 :     const MachineInstr *DepMI = DepSU.getInstr();
     158       31623 :     if (!shouldScheduleAdjacent(TII, ST, DepMI, AnchorMI))
     159        8756 :       continue;
     160             : 
     161       22867 :     if (fuseInstructionPair(DAG, DepSU, AnchorSU))
     162             :       return true;
     163             :   }
     164             : 
     165             :   return false;
     166             : }
     167             : 
     168             : std::unique_ptr<ScheduleDAGMutation>
     169       39898 : llvm::createMacroFusionDAGMutation(
     170             :      ShouldSchedulePredTy shouldScheduleAdjacent) {
     171       39898 :   if(EnableMacroFusion)
     172       79796 :     return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
     173             :   return nullptr;
     174             : }
     175             : 
     176             : std::unique_ptr<ScheduleDAGMutation>
     177      101166 : llvm::createBranchMacroFusionDAGMutation(
     178             :      ShouldSchedulePredTy shouldScheduleAdjacent) {
     179      101166 :   if(EnableMacroFusion)
     180      202332 :     return llvm::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
     181             :   return nullptr;
     182      303507 : }

Generated by: LCOV version 1.13