1 //===-- SchedGraphCommon.h - Scheduling Base Graph ---------------*- C++ -*---=//
3 // A common graph class that is based on the SSA graph. It includes
4 // extra dependencies that are caused by machine resources.
6 //===-------------------------------------------------------------------------=//
8 #ifndef LLVM_CODEGEN_SCHEDGRAPHCOMMON_H
9 #define LLVM_CODEGEN_SCHEDGRAPHCOMMON_H
11 #include "llvm/Value.h"
16 /******************** Exported Data Types and Constants ********************/
18 typedef int ResourceId;
19 const ResourceId InvalidRID = -1;
20 const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
21 const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
22 const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
25 //*********************** Public Class Declarations ************************/
26 class SchedGraphNodeCommon {
29 std::vector<SchedGraphEdge*> inEdges;
30 std::vector<SchedGraphEdge*> outEdges;
34 typedef std::vector<SchedGraphEdge*>::iterator iterator;
35 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
36 typedef std::vector<SchedGraphEdge*>::reverse_iterator reverse_iterator;
37 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
40 unsigned getNodeId() const { return ID; }
41 int getLatency() const { return latency; }
42 unsigned getNumInEdges() const { return inEdges.size(); }
43 unsigned getNumOutEdges() const { return outEdges.size(); }
47 iterator beginInEdges() { return inEdges.begin(); }
48 iterator endInEdges() { return inEdges.end(); }
49 iterator beginOutEdges() { return outEdges.begin(); }
50 iterator endOutEdges() { return outEdges.end(); }
52 const_iterator beginInEdges() const { return inEdges.begin(); }
53 const_iterator endInEdges() const { return inEdges.end(); }
54 const_iterator beginOutEdges() const { return outEdges.begin(); }
55 const_iterator endOutEdges() const { return outEdges.end(); }
57 void dump(int indent=0) const;
60 virtual void print(std::ostream &os) const = 0;
63 friend class SchedGraph;
64 friend class SchedGraphCommon;
65 friend class SchedGraphEdge; // give access for adding edges
68 // disable default constructor and provide a ctor for single-block graphs
69 SchedGraphNodeCommon(); // DO NOT IMPLEMENT
71 inline SchedGraphNodeCommon(unsigned Id) : ID(Id), latency(0) {}
72 virtual ~SchedGraphNodeCommon();
74 //Functions to add and remove edges
75 inline void addInEdge(SchedGraphEdge* edge) { inEdges.push_back(edge); }
76 inline void addOutEdge(SchedGraphEdge* edge) { outEdges.push_back(edge); }
77 void removeInEdge(const SchedGraphEdge* edge);
78 void removeOutEdge(const SchedGraphEdge* edge);
81 // ostream << operator for SchedGraphNode class
82 inline std::ostream &operator<<(std::ostream &os,
83 const SchedGraphNodeCommon &node) {
92 // SchedGraphEdge - Edge class to represent dependencies
94 class SchedGraphEdge {
96 enum SchedGraphEdgeDepType {
97 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
99 enum DataDepOrderType {
100 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
104 SchedGraphNodeCommon* src;
105 SchedGraphNodeCommon* sink;
106 SchedGraphEdgeDepType depType;
107 unsigned int depOrderType;
108 int minDelay; // cached latency (assumes fixed target arch)
114 ResourceId resourceId;
118 // For all constructors, if minDelay is unspecified, minDelay is
119 // set to _src->getLatency().
121 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
122 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
123 SchedGraphEdgeDepType _depType, unsigned int _depOrderType,
126 // constructor for explicit value dependence (may be true/anti/output)
127 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
128 const Value* _val, unsigned int _depOrderType,
131 // constructor for machine register dependence
132 SchedGraphEdge(SchedGraphNodeCommon* _src,SchedGraphNodeCommon* _sink,
133 unsigned int _regNum, unsigned int _depOrderType,
136 // constructor for any other machine resource dependences.
137 // DataDepOrderType is always NonDataDep. It it not an argument to
138 // avoid overloading ambiguity with previous constructor.
139 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
140 ResourceId _resourceId, int _minDelay = -1);
144 SchedGraphNodeCommon* getSrc() const { return src; }
145 SchedGraphNodeCommon* getSink() const { return sink; }
146 int getMinDelay() const { return minDelay; }
147 SchedGraphEdgeDepType getDepType() const { return depType; }
149 const Value* getValue() const {
150 assert(depType == ValueDep); return val;
153 int getMachineReg() const {
154 assert(depType == MachineRegister); return machineRegNum;
157 int getResourceId() const {
158 assert(depType == MachineResource); return resourceId;
161 void setIteDiff(int _iteDiff) {
171 void print(std::ostream &os) const;
172 void dump(int indent=0) const;
175 // disable default ctor
176 SchedGraphEdge(); // DO NOT IMPLEMENT
179 // ostream << operator for SchedGraphNode class
180 inline std::ostream &operator<<(std::ostream &os, const SchedGraphEdge &edge) {
185 class SchedGraphCommon {
188 SchedGraphNodeCommon* graphRoot; // the root and leaf are not inserted
189 SchedGraphNodeCommon* graphLeaf; // in the hash_map (see getNumNodes())
195 SchedGraphNodeCommon* getRoot() const { return graphRoot; }
196 SchedGraphNodeCommon* getLeaf() const { return graphLeaf; }
199 // Delete nodes or edges from the graph.
201 void eraseNode(SchedGraphNodeCommon* node);
202 void eraseIncomingEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
203 void eraseOutgoingEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
204 void eraseIncidentEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
206 SchedGraphCommon() {}