1 //===-- SchedGraphCommon.h - Scheduling Base Graph --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A common graph class that is based on the SSA graph. It includes
11 // extra dependencies that are caused by machine resources.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SCHEDGRAPHCOMMON_H
16 #define LLVM_CODEGEN_SCHEDGRAPHCOMMON_H
18 #include "llvm/Value.h"
19 #include "llvm/ADT/iterator"
20 #include "llvm/Support/Streams.h"
28 /******************** Exported Data Types and Constants ********************/
30 typedef int ResourceId;
31 const ResourceId InvalidRID = -1;
32 const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
33 const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
34 const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
37 //*********************** Public Class Declarations ************************/
38 class SchedGraphNodeCommon {
41 std::vector<SchedGraphEdge*> inEdges;
42 std::vector<SchedGraphEdge*> outEdges;
44 int origIndexInBB; // original position of instr in BB
47 typedef std::vector<SchedGraphEdge*>::iterator iterator;
48 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
49 typedef std::vector<SchedGraphEdge*>::reverse_iterator reverse_iterator;
50 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
53 unsigned getNodeId() const { return ID; }
54 int getLatency() const { return latency; }
55 unsigned getNumInEdges() const { return inEdges.size(); }
56 unsigned getNumOutEdges() const { return outEdges.size(); }
57 int getOrigIndexInBB() const { return origIndexInBB; }
60 iterator beginInEdges() { return inEdges.begin(); }
61 iterator endInEdges() { return inEdges.end(); }
62 iterator beginOutEdges() { return outEdges.begin(); }
63 iterator endOutEdges() { return outEdges.end(); }
65 const_iterator beginInEdges() const { return inEdges.begin(); }
66 const_iterator endInEdges() const { return inEdges.end(); }
67 const_iterator beginOutEdges() const { return outEdges.begin(); }
68 const_iterator endOutEdges() const { return outEdges.end(); }
70 void dump(int indent=0) const;
73 virtual void print(std::ostream &os) const = 0;
74 void print(std::ostream *os) const { if (os) print(*os); }
77 friend class SchedGraphCommon;
78 friend class SchedGraphEdge; // give access for adding edges
81 // disable default constructor and provide a ctor for single-block graphs
82 SchedGraphNodeCommon(); // DO NOT IMPLEMENT
84 inline SchedGraphNodeCommon(unsigned Id, int index, int late=0) : ID(Id), latency(late), origIndexInBB(index) {}
86 virtual ~SchedGraphNodeCommon();
88 //Functions to add and remove edges
89 inline void addInEdge(SchedGraphEdge* edge) { inEdges.push_back(edge); }
90 inline void addOutEdge(SchedGraphEdge* edge) { outEdges.push_back(edge); }
91 void removeInEdge(const SchedGraphEdge* edge);
92 void removeOutEdge(const SchedGraphEdge* edge);
96 // ostream << operator for SchedGraphNode class
97 inline std::ostream &operator<<(std::ostream &os,
98 const SchedGraphNodeCommon &node) {
104 // SchedGraphEdge - Edge class to represent dependencies
106 class SchedGraphEdge {
108 enum SchedGraphEdgeDepType {
109 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
111 enum DataDepOrderType {
112 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
116 SchedGraphNodeCommon* src;
117 SchedGraphNodeCommon* sink;
118 SchedGraphEdgeDepType depType;
119 unsigned int depOrderType;
120 int minDelay; // cached latency (assumes fixed target arch)
126 ResourceId resourceId;
130 // For all constructors, if minDelay is unspecified, minDelay is
131 // set to _src->getLatency().
133 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
134 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
135 SchedGraphEdgeDepType _depType, unsigned int _depOrderType,
138 // constructor for explicit value dependence (may be true/anti/output)
139 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
140 const Value* _val, unsigned int _depOrderType,
143 // constructor for machine register dependence
144 SchedGraphEdge(SchedGraphNodeCommon* _src,SchedGraphNodeCommon* _sink,
145 unsigned int _regNum, unsigned int _depOrderType,
148 // constructor for any other machine resource dependences.
149 // DataDepOrderType is always NonDataDep. It it not an argument to
150 // avoid overloading ambiguity with previous constructor.
151 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
152 ResourceId _resourceId, int _minDelay = -1);
156 SchedGraphNodeCommon* getSrc() const { return src; }
157 SchedGraphNodeCommon* getSink() const { return sink; }
158 int getMinDelay() const { return minDelay; }
159 SchedGraphEdgeDepType getDepType() const { return depType; }
160 unsigned int getDepOrderType() const { return depOrderType; }
162 const Value* getValue() const {
163 assert(depType == ValueDep); return val;
166 int getMachineReg() const {
167 assert(depType == MachineRegister); return machineRegNum;
170 int getResourceId() const {
171 assert(depType == MachineResource); return resourceId;
174 void setIteDiff(int _iteDiff) {
184 void print(std::ostream &os) const;
185 void print(std::ostream *os) const { if (os) print(*os); }
186 void dump(int indent=0) const;
189 // disable default ctor
190 SchedGraphEdge(); // DO NOT IMPLEMENT
193 // ostream << operator for SchedGraphNode class
194 inline std::ostream &operator<<(std::ostream &os, const SchedGraphEdge &edge) {
199 class SchedGraphCommon {
202 SchedGraphNodeCommon* graphRoot; // the root and leaf are not inserted
203 SchedGraphNodeCommon* graphLeaf; // in the hash_map (see getNumNodes())
209 SchedGraphNodeCommon* getRoot() const { return graphRoot; }
210 SchedGraphNodeCommon* getLeaf() const { return graphLeaf; }
213 // Delete nodes or edges from the graph.
215 void eraseNode(SchedGraphNodeCommon* node);
216 void eraseIncomingEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
217 void eraseOutgoingEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
218 void eraseIncidentEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
220 SchedGraphCommon() {}
225 //********************** Sched Graph Iterators *****************************/
227 // Ok to make it a template because it shd get instantiated at most twice:
228 // for <SchedGraphNode, SchedGraphNode::iterator> and
229 // for <const SchedGraphNode, SchedGraphNode::const_iterator>.
231 template <class _NodeType, class _EdgeType, class _EdgeIter>
232 class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
236 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
238 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
240 inline bool operator==(const _Self& x) const { return oi == x.oi; }
241 inline bool operator!=(const _Self& x) const { return !operator==(x); }
243 // operator*() differs for pred or succ iterator
244 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
245 inline _NodeType* operator->() const { return operator*(); }
247 inline _EdgeType* getEdge() const { return *(oi); }
249 inline _Self &operator++() { ++oi; return *this; } // Preincrement
250 inline _Self operator++(int) { // Postincrement
251 _Self tmp(*this); ++*this; return tmp;
254 inline _Self &operator--() { --oi; return *this; } // Predecrement
255 inline _Self operator--(int) { // Postdecrement
256 _Self tmp = *this; --*this; return tmp;
260 template <class _NodeType, class _EdgeType, class _EdgeIter>
261 class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
265 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
267 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
269 inline bool operator==(const _Self& x) const { return oi == x.oi; }
270 inline bool operator!=(const _Self& x) const { return !operator==(x); }
272 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
273 inline _NodeType* operator->() const { return operator*(); }
275 inline _EdgeType* getEdge() const { return *(oi); }
277 inline _Self &operator++() { ++oi; return *this; } // Preincrement
278 inline _Self operator++(int) { // Postincrement
279 _Self tmp(*this); ++*this; return tmp;
282 inline _Self &operator--() { --oi; return *this; } // Predecrement
283 inline _Self operator--(int) { // Postdecrement
284 _Self tmp = *this; --*this; return tmp;
287 } // End llvm namespace