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 "Support/iterator"
27 /******************** Exported Data Types and Constants ********************/
29 typedef int ResourceId;
30 const ResourceId InvalidRID = -1;
31 const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
32 const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
33 const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
36 //*********************** Public Class Declarations ************************/
37 class SchedGraphNodeCommon {
40 std::vector<SchedGraphEdge*> inEdges;
41 std::vector<SchedGraphEdge*> outEdges;
43 int origIndexInBB; // original position of instr in BB
46 typedef std::vector<SchedGraphEdge*>::iterator iterator;
47 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
48 typedef std::vector<SchedGraphEdge*>::reverse_iterator reverse_iterator;
49 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
52 unsigned getNodeId() const { return ID; }
53 int getLatency() const { return latency; }
54 unsigned getNumInEdges() const { return inEdges.size(); }
55 unsigned getNumOutEdges() const { return outEdges.size(); }
56 int getOrigIndexInBB() const { return origIndexInBB; }
59 iterator beginInEdges() { return inEdges.begin(); }
60 iterator endInEdges() { return inEdges.end(); }
61 iterator beginOutEdges() { return outEdges.begin(); }
62 iterator endOutEdges() { return outEdges.end(); }
64 const_iterator beginInEdges() const { return inEdges.begin(); }
65 const_iterator endInEdges() const { return inEdges.end(); }
66 const_iterator beginOutEdges() const { return outEdges.begin(); }
67 const_iterator endOutEdges() const { return outEdges.end(); }
69 void dump(int indent=0) const;
72 virtual void print(std::ostream &os) const = 0;
75 friend class SchedGraphCommon;
76 friend class SchedGraphEdge; // give access for adding edges
79 // disable default constructor and provide a ctor for single-block graphs
80 SchedGraphNodeCommon(); // DO NOT IMPLEMENT
82 inline SchedGraphNodeCommon(unsigned Id, int index) : ID(Id), latency(0),
83 origIndexInBB(index) {}
84 inline SchedGraphNodeCommon(unsigned Id, int late, int index) : 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) {
107 // SchedGraphEdge - Edge class to represent dependencies
109 class SchedGraphEdge {
111 enum SchedGraphEdgeDepType {
112 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
114 enum DataDepOrderType {
115 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
119 SchedGraphNodeCommon* src;
120 SchedGraphNodeCommon* sink;
121 SchedGraphEdgeDepType depType;
122 unsigned int depOrderType;
123 int minDelay; // cached latency (assumes fixed target arch)
129 ResourceId resourceId;
133 // For all constructors, if minDelay is unspecified, minDelay is
134 // set to _src->getLatency().
136 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
137 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
138 SchedGraphEdgeDepType _depType, unsigned int _depOrderType,
141 // constructor for explicit value dependence (may be true/anti/output)
142 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
143 const Value* _val, unsigned int _depOrderType,
146 // constructor for machine register dependence
147 SchedGraphEdge(SchedGraphNodeCommon* _src,SchedGraphNodeCommon* _sink,
148 unsigned int _regNum, unsigned int _depOrderType,
151 // constructor for any other machine resource dependences.
152 // DataDepOrderType is always NonDataDep. It it not an argument to
153 // avoid overloading ambiguity with previous constructor.
154 SchedGraphEdge(SchedGraphNodeCommon* _src, SchedGraphNodeCommon* _sink,
155 ResourceId _resourceId, int _minDelay = -1);
159 SchedGraphNodeCommon* getSrc() const { return src; }
160 SchedGraphNodeCommon* getSink() const { return sink; }
161 int getMinDelay() const { return minDelay; }
162 SchedGraphEdgeDepType getDepType() const { return depType; }
164 const Value* getValue() const {
165 assert(depType == ValueDep); return val;
168 int getMachineReg() const {
169 assert(depType == MachineRegister); return machineRegNum;
172 int getResourceId() const {
173 assert(depType == MachineResource); return resourceId;
176 void setIteDiff(int _iteDiff) {
186 void print(std::ostream &os) const;
187 void dump(int indent=0) const;
190 // disable default ctor
191 SchedGraphEdge(); // DO NOT IMPLEMENT
194 // ostream << operator for SchedGraphNode class
195 inline std::ostream &operator<<(std::ostream &os, const SchedGraphEdge &edge) {
200 class SchedGraphCommon {
203 SchedGraphNodeCommon* graphRoot; // the root and leaf are not inserted
204 SchedGraphNodeCommon* graphLeaf; // in the hash_map (see getNumNodes())
210 SchedGraphNodeCommon* getRoot() const { return graphRoot; }
211 SchedGraphNodeCommon* getLeaf() const { return graphLeaf; }
214 // Delete nodes or edges from the graph.
216 void eraseNode(SchedGraphNodeCommon* node);
217 void eraseIncomingEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
218 void eraseOutgoingEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
219 void eraseIncidentEdges(SchedGraphNodeCommon* node, bool addDummyEdges = true);
221 SchedGraphCommon() {}
226 //********************** Sched Graph Iterators *****************************/
228 // Ok to make it a template because it shd get instantiated at most twice:
229 // for <SchedGraphNode, SchedGraphNode::iterator> and
230 // for <const SchedGraphNode, SchedGraphNode::const_iterator>.
232 template <class _NodeType, class _EdgeType, class _EdgeIter>
233 class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
237 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
239 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
241 inline bool operator==(const _Self& x) const { return oi == x.oi; }
242 inline bool operator!=(const _Self& x) const { return !operator==(x); }
244 // operator*() differs for pred or succ iterator
245 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
246 inline _NodeType* operator->() const { return operator*(); }
248 inline _EdgeType* getEdge() const { return *(oi); }
250 inline _Self &operator++() { ++oi; return *this; } // Preincrement
251 inline _Self operator++(int) { // Postincrement
252 _Self tmp(*this); ++*this; return tmp;
255 inline _Self &operator--() { --oi; return *this; } // Predecrement
256 inline _Self operator--(int) { // Postdecrement
257 _Self tmp = *this; --*this; return tmp;
261 template <class _NodeType, class _EdgeType, class _EdgeIter>
262 class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
266 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
268 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
270 inline bool operator==(const _Self& x) const { return oi == x.oi; }
271 inline bool operator!=(const _Self& x) const { return !operator==(x); }
273 inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
274 inline _NodeType* operator->() const { return operator*(); }
276 inline _EdgeType* getEdge() const { return *(oi); }
278 inline _Self &operator++() { ++oi; return *this; } // Preincrement
279 inline _Self operator++(int) { // Postincrement
280 _Self tmp(*this); ++*this; return tmp;
283 inline _Self &operator--() { --oi; return *this; } // Predecrement
284 inline _Self operator--(int) { // Postdecrement
285 _Self tmp = *this; --*this; return tmp;
288 } // End llvm namespace