ScheduleDAGSDNodes.h revision 221345
1178825Sdfr//===---- ScheduleDAGSDNodes.h - SDNode Scheduling --------------*- C++ -*-===//
2178825Sdfr//
3178825Sdfr//                     The LLVM Compiler Infrastructure
4178825Sdfr//
5178825Sdfr// This file is distributed under the University of Illinois Open Source
6178825Sdfr// License. See LICENSE.TXT for details.
7178825Sdfr//
8178825Sdfr//===----------------------------------------------------------------------===//
9178825Sdfr//
10178825Sdfr// This file implements the ScheduleDAGSDNodes class, which implements
11178825Sdfr// scheduling for an SDNode-based dependency graph.
12178825Sdfr//
13178825Sdfr//===----------------------------------------------------------------------===//
14178825Sdfr
15178825Sdfr#ifndef SCHEDULEDAGSDNODES_H
16178825Sdfr#define SCHEDULEDAGSDNODES_H
17178825Sdfr
18178825Sdfr#include "llvm/CodeGen/ScheduleDAG.h"
19178825Sdfr#include "llvm/CodeGen/SelectionDAG.h"
20178825Sdfr
21178825Sdfrnamespace llvm {
22178825Sdfr  /// ScheduleDAGSDNodes - A ScheduleDAG for scheduling SDNode-based DAGs.
23178825Sdfr  ///
24178825Sdfr  /// Edges between SUnits are initially based on edges in the SelectionDAG,
25178825Sdfr  /// and additional edges can be added by the schedulers as heuristics.
26178825Sdfr  /// SDNodes such as Constants, Registers, and a few others that are not
27178825Sdfr  /// interesting to schedulers are not allocated SUnits.
28178825Sdfr  ///
29178825Sdfr  /// SDNodes with MVT::Glue operands are grouped along with the flagged
30178825Sdfr  /// nodes into a single SUnit so that they are scheduled together.
31178825Sdfr  ///
32178825Sdfr  /// SDNode-based scheduling graphs do not use SDep::Anti or SDep::Output
33178825Sdfr  /// edges.  Physical register dependence information is not carried in
34178825Sdfr  /// the DAG and must be handled explicitly by schedulers.
35178825Sdfr  ///
36178825Sdfr  class ScheduleDAGSDNodes : public ScheduleDAG {
37178825Sdfr  public:
38178825Sdfr    SelectionDAG *DAG;                    // DAG of the current basic block
39178825Sdfr    const InstrItineraryData *InstrItins;
40178825Sdfr
41178825Sdfr    explicit ScheduleDAGSDNodes(MachineFunction &mf);
42178825Sdfr
43178825Sdfr    virtual ~ScheduleDAGSDNodes() {}
44178825Sdfr
45178825Sdfr    /// Run - perform scheduling.
46178825Sdfr    ///
47178825Sdfr    void Run(SelectionDAG *dag, MachineBasicBlock *bb,
48178825Sdfr             MachineBasicBlock::iterator insertPos);
49178825Sdfr
50178825Sdfr    /// isPassiveNode - Return true if the node is a non-scheduled leaf.
51178825Sdfr    ///
52178825Sdfr    static bool isPassiveNode(SDNode *Node) {
53178825Sdfr      if (isa<ConstantSDNode>(Node))       return true;
54178825Sdfr      if (isa<ConstantFPSDNode>(Node))     return true;
55178825Sdfr      if (isa<RegisterSDNode>(Node))       return true;
56178825Sdfr      if (isa<GlobalAddressSDNode>(Node))  return true;
57178825Sdfr      if (isa<BasicBlockSDNode>(Node))     return true;
58178825Sdfr      if (isa<FrameIndexSDNode>(Node))     return true;
59178825Sdfr      if (isa<ConstantPoolSDNode>(Node))   return true;
60178825Sdfr      if (isa<JumpTableSDNode>(Node))      return true;
61178825Sdfr      if (isa<ExternalSymbolSDNode>(Node)) return true;
62178825Sdfr      if (isa<BlockAddressSDNode>(Node))   return true;
63178825Sdfr      if (Node->getOpcode() == ISD::EntryToken ||
64178825Sdfr          isa<MDNodeSDNode>(Node)) return true;
65178825Sdfr      return false;
66178825Sdfr    }
67178825Sdfr
68178825Sdfr    /// NewSUnit - Creates a new SUnit and return a ptr to it.
69178825Sdfr    ///
70178825Sdfr    SUnit *NewSUnit(SDNode *N);
71178825Sdfr
72178825Sdfr    /// Clone - Creates a clone of the specified SUnit. It does not copy the
73178825Sdfr    /// predecessors / successors info nor the temporary scheduling states.
74178825Sdfr    ///
75178825Sdfr    SUnit *Clone(SUnit *N);
76178825Sdfr
77178825Sdfr    /// BuildSchedGraph - Build the SUnit graph from the selection dag that we
78178825Sdfr    /// are input.  This SUnit graph is similar to the SelectionDAG, but
79178825Sdfr    /// excludes nodes that aren't interesting to scheduling, and represents
80178825Sdfr    /// flagged together nodes with a single SUnit.
81178825Sdfr    virtual void BuildSchedGraph(AliasAnalysis *AA);
82178825Sdfr
83178825Sdfr    /// InitVRegCycleFlag - Set isVRegCycle if this node's single use is
84178825Sdfr    /// CopyToReg and its only active data operands are CopyFromReg within a
85178825Sdfr    /// single block loop.
86178825Sdfr    ///
87178825Sdfr    void InitVRegCycleFlag(SUnit *SU);
88178825Sdfr
89178825Sdfr    /// InitNumRegDefsLeft - Determine the # of regs defined by this node.
90178825Sdfr    ///
91178825Sdfr    void InitNumRegDefsLeft(SUnit *SU);
92178825Sdfr
93178825Sdfr    /// ComputeLatency - Compute node latency.
94178825Sdfr    ///
95178825Sdfr    virtual void ComputeLatency(SUnit *SU);
96178825Sdfr
97178825Sdfr    /// ComputeOperandLatency - Override dependence edge latency using
98178825Sdfr    /// operand use/def information
99178825Sdfr    ///
100178825Sdfr    virtual void ComputeOperandLatency(SUnit *Def, SUnit *Use,
101178825Sdfr                                       SDep& dep) const { }
102178825Sdfr
103178825Sdfr    virtual void ComputeOperandLatency(SDNode *Def, SDNode *Use,
104178825Sdfr                                       unsigned OpIdx, SDep& dep) const;
105178825Sdfr
106178825Sdfr    virtual MachineBasicBlock *EmitSchedule();
107178825Sdfr
108178825Sdfr    /// Schedule - Order nodes according to selected style, filling
109178825Sdfr    /// in the Sequence member.
110178825Sdfr    ///
111178825Sdfr    virtual void Schedule() = 0;
112178825Sdfr
113178825Sdfr    virtual void dumpNode(const SUnit *SU) const;
114178825Sdfr
115178825Sdfr    virtual std::string getGraphNodeLabel(const SUnit *SU) const;
116178825Sdfr
117178825Sdfr    virtual void getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const;
118178825Sdfr
119178825Sdfr    /// RegDefIter - In place iteration over the values defined by an
120178825Sdfr    /// SUnit. This does not need copies of the iterator or any other STLisms.
121178825Sdfr    /// The iterator creates itself, rather than being provided by the SchedDAG.
122178825Sdfr    class RegDefIter {
123178825Sdfr      const ScheduleDAGSDNodes *SchedDAG;
124178825Sdfr      const SDNode *Node;
125178825Sdfr      unsigned DefIdx;
126178825Sdfr      unsigned NodeNumDefs;
127178825Sdfr      EVT ValueType;
128178825Sdfr    public:
129178825Sdfr      RegDefIter(const SUnit *SU, const ScheduleDAGSDNodes *SD);
130178825Sdfr
131178825Sdfr      bool IsValid() const { return Node != NULL; }
132178825Sdfr
133178825Sdfr      EVT GetValue() const {
134178825Sdfr        assert(IsValid() && "bad iterator");
135178825Sdfr        return ValueType;
136178825Sdfr      }
137178825Sdfr
138178825Sdfr      void Advance();
139178825Sdfr    private:
140178825Sdfr      void InitNodeNumDefs();
141178825Sdfr    };
142178825Sdfr
143178825Sdfr  private:
144178825Sdfr    /// ClusterNeighboringLoads - Cluster loads from "near" addresses into
145178825Sdfr    /// combined SUnits.
146178825Sdfr    void ClusterNeighboringLoads(SDNode *Node);
147178825Sdfr    /// ClusterNodes - Cluster certain nodes which should be scheduled together.
148178825Sdfr    ///
149178825Sdfr    void ClusterNodes();
150178825Sdfr
151178825Sdfr    /// BuildSchedUnits, AddSchedEdges - Helper functions for BuildSchedGraph.
152178825Sdfr    void BuildSchedUnits();
153178825Sdfr    void AddSchedEdges();
154178825Sdfr  };
155178825Sdfr}
156178825Sdfr
157178825Sdfr#endif
158178825Sdfr