LLVM API Documentation

ScheduleDAGSDNodes.h
Go to the documentation of this file.
00001 //===---- ScheduleDAGSDNodes.h - SDNode Scheduling --------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the ScheduleDAGSDNodes class, which implements
00011 // scheduling for an SDNode-based dependency graph.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H
00016 #define LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H
00017 
00018 #include "llvm/CodeGen/MachineBasicBlock.h"
00019 #include "llvm/CodeGen/ScheduleDAG.h"
00020 
00021 namespace llvm {
00022   /// ScheduleDAGSDNodes - A ScheduleDAG for scheduling SDNode-based DAGs.
00023   ///
00024   /// Edges between SUnits are initially based on edges in the SelectionDAG,
00025   /// and additional edges can be added by the schedulers as heuristics.
00026   /// SDNodes such as Constants, Registers, and a few others that are not
00027   /// interesting to schedulers are not allocated SUnits.
00028   ///
00029   /// SDNodes with MVT::Glue operands are grouped along with the flagged
00030   /// nodes into a single SUnit so that they are scheduled together.
00031   ///
00032   /// SDNode-based scheduling graphs do not use SDep::Anti or SDep::Output
00033   /// edges.  Physical register dependence information is not carried in
00034   /// the DAG and must be handled explicitly by schedulers.
00035   ///
00036   class ScheduleDAGSDNodes : public ScheduleDAG {
00037   public:
00038     MachineBasicBlock *BB;
00039     SelectionDAG *DAG;                    // DAG of the current basic block
00040     const InstrItineraryData *InstrItins;
00041 
00042     /// The schedule. Null SUnit*'s represent noop instructions.
00043     std::vector<SUnit*> Sequence;
00044 
00045     explicit ScheduleDAGSDNodes(MachineFunction &mf);
00046 
00047     virtual ~ScheduleDAGSDNodes() {}
00048 
00049     /// Run - perform scheduling.
00050     ///
00051     void Run(SelectionDAG *dag, MachineBasicBlock *bb);
00052 
00053     /// isPassiveNode - Return true if the node is a non-scheduled leaf.
00054     ///
00055     static bool isPassiveNode(SDNode *Node) {
00056       if (isa<ConstantSDNode>(Node))       return true;
00057       if (isa<ConstantFPSDNode>(Node))     return true;
00058       if (isa<RegisterSDNode>(Node))       return true;
00059       if (isa<RegisterMaskSDNode>(Node))   return true;
00060       if (isa<GlobalAddressSDNode>(Node))  return true;
00061       if (isa<BasicBlockSDNode>(Node))     return true;
00062       if (isa<FrameIndexSDNode>(Node))     return true;
00063       if (isa<ConstantPoolSDNode>(Node))   return true;
00064       if (isa<TargetIndexSDNode>(Node))    return true;
00065       if (isa<JumpTableSDNode>(Node))      return true;
00066       if (isa<ExternalSymbolSDNode>(Node)) return true;
00067       if (isa<BlockAddressSDNode>(Node))   return true;
00068       if (Node->getOpcode() == ISD::EntryToken ||
00069           isa<MDNodeSDNode>(Node)) return true;
00070       return false;
00071     }
00072 
00073     /// NewSUnit - Creates a new SUnit and return a ptr to it.
00074     ///
00075     SUnit *newSUnit(SDNode *N);
00076 
00077     /// Clone - Creates a clone of the specified SUnit. It does not copy the
00078     /// predecessors / successors info nor the temporary scheduling states.
00079     ///
00080     SUnit *Clone(SUnit *N);
00081 
00082     /// BuildSchedGraph - Build the SUnit graph from the selection dag that we
00083     /// are input.  This SUnit graph is similar to the SelectionDAG, but
00084     /// excludes nodes that aren't interesting to scheduling, and represents
00085     /// flagged together nodes with a single SUnit.
00086     void BuildSchedGraph(AliasAnalysis *AA);
00087 
00088     /// InitVRegCycleFlag - Set isVRegCycle if this node's single use is
00089     /// CopyToReg and its only active data operands are CopyFromReg within a
00090     /// single block loop.
00091     ///
00092     void InitVRegCycleFlag(SUnit *SU);
00093 
00094     /// InitNumRegDefsLeft - Determine the # of regs defined by this node.
00095     ///
00096     void InitNumRegDefsLeft(SUnit *SU);
00097 
00098     /// computeLatency - Compute node latency.
00099     ///
00100     virtual void computeLatency(SUnit *SU);
00101 
00102     virtual void computeOperandLatency(SDNode *Def, SDNode *Use,
00103                                        unsigned OpIdx, SDep& dep) const;
00104 
00105     /// Schedule - Order nodes according to selected style, filling
00106     /// in the Sequence member.
00107     ///
00108     virtual void Schedule() = 0;
00109 
00110     /// VerifyScheduledSequence - Verify that all SUnits are scheduled and
00111     /// consistent with the Sequence of scheduled instructions.
00112     void VerifyScheduledSequence(bool isBottomUp);
00113 
00114     /// EmitSchedule - Insert MachineInstrs into the MachineBasicBlock
00115     /// according to the order specified in Sequence.
00116     ///
00117     virtual MachineBasicBlock*
00118     EmitSchedule(MachineBasicBlock::iterator &InsertPos);
00119 
00120     void dumpNode(const SUnit *SU) const override;
00121 
00122     void dumpSchedule() const;
00123 
00124     std::string getGraphNodeLabel(const SUnit *SU) const override;
00125 
00126     std::string getDAGName() const override;
00127 
00128     virtual void getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const;
00129 
00130     /// RegDefIter - In place iteration over the values defined by an
00131     /// SUnit. This does not need copies of the iterator or any other STLisms.
00132     /// The iterator creates itself, rather than being provided by the SchedDAG.
00133     class RegDefIter {
00134       const ScheduleDAGSDNodes *SchedDAG;
00135       const SDNode *Node;
00136       unsigned DefIdx;
00137       unsigned NodeNumDefs;
00138       MVT ValueType;
00139     public:
00140       RegDefIter(const SUnit *SU, const ScheduleDAGSDNodes *SD);
00141 
00142       bool IsValid() const { return Node != nullptr; }
00143 
00144       MVT GetValue() const {
00145         assert(IsValid() && "bad iterator");
00146         return ValueType;
00147       }
00148 
00149       const SDNode *GetNode() const {
00150         return Node;
00151       }
00152 
00153       unsigned GetIdx() const {
00154         return DefIdx-1;
00155       }
00156 
00157       void Advance();
00158     private:
00159       void InitNodeNumDefs();
00160     };
00161 
00162   protected:
00163     /// ForceUnitLatencies - Return true if all scheduling edges should be given
00164     /// a latency value of one.  The default is to return false; schedulers may
00165     /// override this as needed.
00166     virtual bool forceUnitLatencies() const { return false; }
00167 
00168   private:
00169     /// ClusterNeighboringLoads - Cluster loads from "near" addresses into
00170     /// combined SUnits.
00171     void ClusterNeighboringLoads(SDNode *Node);
00172     /// ClusterNodes - Cluster certain nodes which should be scheduled together.
00173     ///
00174     void ClusterNodes();
00175 
00176     /// BuildSchedUnits, AddSchedEdges - Helper functions for BuildSchedGraph.
00177     void BuildSchedUnits();
00178     void AddSchedEdges();
00179 
00180     void EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap,
00181                          MachineBasicBlock::iterator InsertPos);
00182   };
00183 }
00184 
00185 #endif