Chris Lattner | 179cdfb | 2002-08-09 20:08:03 +0000 | [diff] [blame] | 1 | //===-- SchedGraph.h - Scheduling Graph --------------------------*- C++ -*--=// |
| 2 | // |
| 3 | // Purpose: |
| 4 | // Scheduling graph based on SSA graph plus extra dependence edges |
| 5 | // capturing dependences due to machine resources (machine registers, |
| 6 | // CC registers, and any others). |
| 7 | // |
| 8 | // Strategy: |
| 9 | // This graph tries to leverage the SSA graph as much as possible, |
| 10 | // but captures the extra dependences through a common interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 13 | |
| 14 | #ifndef LLVM_CODEGEN_SCHEDGRAPH_H |
| 15 | #define LLVM_CODEGEN_SCHEDGRAPH_H |
| 16 | |
Vikram S. Adve | e64574c | 2001-11-08 05:20:23 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 18 | #include "Support/HashExtras.h" |
| 19 | #include "Support/GraphTraits.h" |
Chris Lattner | 747a044 | 2003-06-02 22:05:13 +0000 | [diff] [blame] | 20 | #include "Support/NonCopyable.h" |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 21 | |
| 22 | class Value; |
| 23 | class Instruction; |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 24 | class TerminatorInst; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 25 | class BasicBlock; |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 26 | class Function; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 27 | class TargetMachine; |
| 28 | class SchedGraphEdge; |
| 29 | class SchedGraphNode; |
| 30 | class SchedGraph; |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 31 | class RegToRefVecMap; |
Vikram S. Adve | c352d2c | 2001-11-05 04:04:23 +0000 | [diff] [blame] | 32 | class ValueToDefVecMap; |
| 33 | class RefVec; |
Chris Lattner | c0c7708 | 2001-09-14 17:55:51 +0000 | [diff] [blame] | 34 | class MachineInstr; |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 35 | class MachineBasicBlock; |
Vikram S. Adve | d0d79c0 | 2001-10-28 21:45:02 +0000 | [diff] [blame] | 36 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 37 | |
| 38 | /******************** Exported Data Types and Constants ********************/ |
| 39 | |
| 40 | typedef int ResourceId; |
Vikram S. Adve | d0d79c0 | 2001-10-28 21:45:02 +0000 | [diff] [blame] | 41 | const ResourceId InvalidRID = -1; |
| 42 | const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs |
| 43 | const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs |
| 44 | const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | //*********************** Public Class Declarations ************************/ |
| 48 | |
| 49 | class SchedGraphEdge: public NonCopyable { |
| 50 | public: |
| 51 | enum SchedGraphEdgeDepType { |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 52 | CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 53 | }; |
| 54 | enum DataDepOrderType { |
Vikram S. Adve | d0d79c0 | 2001-10-28 21:45:02 +0000 | [diff] [blame] | 55 | TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8 |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | protected: |
| 59 | SchedGraphNode* src; |
| 60 | SchedGraphNode* sink; |
| 61 | SchedGraphEdgeDepType depType; |
Vikram S. Adve | d0d79c0 | 2001-10-28 21:45:02 +0000 | [diff] [blame] | 62 | unsigned int depOrderType; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 63 | int minDelay; // cached latency (assumes fixed target arch) |
| 64 | |
| 65 | union { |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 66 | const Value* val; |
| 67 | int machineRegNum; |
| 68 | ResourceId resourceId; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | public: |
| 72 | // For all constructors, if minDelay is unspecified, minDelay is |
| 73 | // set to _src->getLatency(). |
| 74 | // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument |
| 75 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 76 | SchedGraphNode* _sink, |
| 77 | SchedGraphEdgeDepType _depType, |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 78 | unsigned int _depOrderType, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 79 | int _minDelay = -1); |
| 80 | |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 81 | // constructor for explicit value dependence (may be true/anti/output) |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 82 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 83 | SchedGraphNode* _sink, |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 84 | const Value* _val, |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 85 | unsigned int _depOrderType, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 86 | int _minDelay = -1); |
| 87 | |
| 88 | // constructor for machine register dependence |
| 89 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 90 | SchedGraphNode* _sink, |
| 91 | unsigned int _regNum, |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 92 | unsigned int _depOrderType, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 93 | int _minDelay = -1); |
| 94 | |
| 95 | // constructor for any other machine resource dependences. |
| 96 | // DataDepOrderType is always NonDataDep. It it not an argument to |
| 97 | // avoid overloading ambiguity with previous constructor. |
| 98 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 99 | SchedGraphNode* _sink, |
| 100 | ResourceId _resourceId, |
| 101 | int _minDelay = -1); |
| 102 | |
Vikram S. Adve | f0b6d79 | 2001-09-18 12:49:26 +0000 | [diff] [blame] | 103 | /*dtor*/ ~SchedGraphEdge(); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 104 | |
| 105 | SchedGraphNode* getSrc () const { return src; } |
| 106 | SchedGraphNode* getSink () const { return sink; } |
| 107 | int getMinDelay () const { return minDelay; } |
| 108 | SchedGraphEdgeDepType getDepType () const { return depType; } |
| 109 | |
| 110 | const Value* getValue () const { |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 111 | assert(depType == ValueDep); return val; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 112 | } |
| 113 | int getMachineReg () const { |
| 114 | assert(depType == MachineRegister); return machineRegNum; |
| 115 | } |
| 116 | int getResourceId () const { |
| 117 | assert(depType == MachineResource); return resourceId; |
| 118 | } |
| 119 | |
| 120 | public: |
| 121 | // |
| 122 | // Debugging support |
| 123 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 124 | friend std::ostream& operator<<(std::ostream& os, const SchedGraphEdge& edge); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 125 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 126 | void dump (int indent=0) const; |
| 127 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 128 | private: |
| 129 | // disable default ctor |
| 130 | /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | |
| 135 | class SchedGraphNode: public NonCopyable { |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 136 | unsigned nodeId; |
| 137 | MachineBasicBlock *MBB; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 138 | const MachineInstr* minstr; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 139 | std::vector<SchedGraphEdge*> inEdges; |
| 140 | std::vector<SchedGraphEdge*> outEdges; |
Vikram S. Adve | 5b43af9 | 2001-11-11 01:23:27 +0000 | [diff] [blame] | 141 | int origIndexInBB; // original position of machine instr in BB |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 142 | int latency; |
| 143 | |
| 144 | public: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 145 | typedef std::vector<SchedGraphEdge*>:: iterator iterator; |
| 146 | typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator; |
| 147 | typedef std::vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator; |
| 148 | typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 149 | |
| 150 | public: |
| 151 | // |
| 152 | // Accessor methods |
| 153 | // |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 154 | unsigned getNodeId () const { return nodeId; } |
| 155 | const MachineInstr* getMachineInstr () const { return minstr; } |
| 156 | const MachineOpCode getOpCode () const { return minstr->getOpCode(); } |
| 157 | int getLatency () const { return latency; } |
| 158 | unsigned getNumInEdges () const { return inEdges.size(); } |
| 159 | unsigned getNumOutEdges () const { return outEdges.size(); } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 160 | bool isDummyNode () const { return (minstr == NULL); } |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 161 | MachineBasicBlock &getMachineBasicBlock() const { return *MBB; } |
Vikram S. Adve | 5b43af9 | 2001-11-11 01:23:27 +0000 | [diff] [blame] | 162 | int getOrigIndexInBB() const { return origIndexInBB; } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 163 | |
| 164 | // |
| 165 | // Iterators |
| 166 | // |
| 167 | iterator beginInEdges () { return inEdges.begin(); } |
| 168 | iterator endInEdges () { return inEdges.end(); } |
| 169 | iterator beginOutEdges () { return outEdges.begin(); } |
| 170 | iterator endOutEdges () { return outEdges.end(); } |
| 171 | |
| 172 | const_iterator beginInEdges () const { return inEdges.begin(); } |
| 173 | const_iterator endInEdges () const { return inEdges.end(); } |
| 174 | const_iterator beginOutEdges () const { return outEdges.begin(); } |
| 175 | const_iterator endOutEdges () const { return outEdges.end(); } |
| 176 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 177 | public: |
| 178 | // |
| 179 | // Debugging support |
| 180 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 181 | friend std::ostream& operator<<(std::ostream& os, const SchedGraphNode& node); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 182 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 183 | void dump (int indent=0) const; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | friend class SchedGraph; // give access for ctor and dtor |
| 187 | friend class SchedGraphEdge; // give access for adding edges |
| 188 | |
| 189 | void addInEdge (SchedGraphEdge* edge); |
| 190 | void addOutEdge (SchedGraphEdge* edge); |
| 191 | |
| 192 | void removeInEdge (const SchedGraphEdge* edge); |
| 193 | void removeOutEdge (const SchedGraphEdge* edge); |
| 194 | |
| 195 | // disable default constructor and provide a ctor for single-block graphs |
| 196 | /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 197 | /*ctor*/ SchedGraphNode (unsigned nodeId, |
| 198 | MachineBasicBlock *mbb, |
Vikram S. Adve | 5b43af9 | 2001-11-11 01:23:27 +0000 | [diff] [blame] | 199 | int indexInBB, |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 200 | const TargetMachine& Target); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 201 | /*dtor*/ ~SchedGraphNode (); |
| 202 | }; |
| 203 | |
| 204 | |
| 205 | |
| 206 | class SchedGraph : |
| 207 | public NonCopyable, |
Chris Lattner | 09ff112 | 2002-07-24 21:21:32 +0000 | [diff] [blame] | 208 | private hash_map<const MachineInstr*, SchedGraphNode*> |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 209 | { |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 210 | MachineBasicBlock &MBB; // basic blocks for this graph |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 211 | SchedGraphNode* graphRoot; // the root and leaf are not inserted |
| 212 | SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes()) |
| 213 | |
Chris Lattner | 09ff112 | 2002-07-24 21:21:32 +0000 | [diff] [blame] | 214 | typedef hash_map<const MachineInstr*, SchedGraphNode*> map_base; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 215 | public: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 216 | using map_base::iterator; |
| 217 | using map_base::const_iterator; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 218 | |
| 219 | public: |
| 220 | // |
| 221 | // Accessor methods |
| 222 | // |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 223 | MachineBasicBlock &getBasicBlock() const { return MBB; } |
| 224 | unsigned getNumNodes() const { return size()+2; } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 225 | SchedGraphNode* getRoot() const { return graphRoot; } |
| 226 | SchedGraphNode* getLeaf() const { return graphLeaf; } |
| 227 | |
| 228 | SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const { |
| 229 | const_iterator onePair = this->find(minstr); |
| 230 | return (onePair != this->end())? (*onePair).second : NULL; |
| 231 | } |
| 232 | |
| 233 | // |
Vikram S. Adve | f0b6d79 | 2001-09-18 12:49:26 +0000 | [diff] [blame] | 234 | // Delete nodes or edges from the graph. |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 235 | // |
Vikram S. Adve | f0b6d79 | 2001-09-18 12:49:26 +0000 | [diff] [blame] | 236 | void eraseNode (SchedGraphNode* node); |
| 237 | |
| 238 | void eraseIncomingEdges (SchedGraphNode* node, |
| 239 | bool addDummyEdges = true); |
| 240 | |
| 241 | void eraseOutgoingEdges (SchedGraphNode* node, |
| 242 | bool addDummyEdges = true); |
| 243 | |
| 244 | void eraseIncidentEdges (SchedGraphNode* node, |
| 245 | bool addDummyEdges = true); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 246 | |
| 247 | // |
| 248 | // Unordered iterators. |
| 249 | // Return values is pair<const MachineIntr*,SchedGraphNode*>. |
| 250 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 251 | using map_base::begin; |
| 252 | using map_base::end; |
| 253 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 254 | // |
| 255 | // Ordered iterators. |
| 256 | // Return values is pair<const MachineIntr*,SchedGraphNode*>. |
| 257 | // |
| 258 | // void postord_init(); |
| 259 | // postorder_iterator postord_begin(); |
| 260 | // postorder_iterator postord_end(); |
| 261 | // const_postorder_iterator postord_begin() const; |
| 262 | // const_postorder_iterator postord_end() const; |
| 263 | |
| 264 | // |
| 265 | // Debugging support |
| 266 | // |
| 267 | void dump () const; |
| 268 | |
| 269 | private: |
| 270 | friend class SchedGraphSet; // give access to ctor |
| 271 | |
| 272 | // disable default constructor and provide a ctor for single-block graphs |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 273 | /*ctor*/ SchedGraph (MachineBasicBlock &bb, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 274 | const TargetMachine& target); |
| 275 | /*dtor*/ ~SchedGraph (); |
| 276 | |
| 277 | inline void noteGraphNodeForInstr (const MachineInstr* minstr, |
| 278 | SchedGraphNode* node) |
| 279 | { |
| 280 | assert((*this)[minstr] == NULL); |
| 281 | (*this)[minstr] = node; |
| 282 | } |
| 283 | |
| 284 | // |
| 285 | // Graph builder |
| 286 | // |
Vikram S. Adve | d0d79c0 | 2001-10-28 21:45:02 +0000 | [diff] [blame] | 287 | void buildGraph (const TargetMachine& target); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 288 | |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 289 | void buildNodesForBB (const TargetMachine& target, |
| 290 | MachineBasicBlock &MBB, |
Vikram S. Adve | 7952d60 | 2003-05-31 07:37:05 +0000 | [diff] [blame] | 291 | std::vector<SchedGraphNode*>& memNV, |
| 292 | std::vector<SchedGraphNode*>& callNV, |
Vikram S. Adve | c352d2c | 2001-11-05 04:04:23 +0000 | [diff] [blame] | 293 | RegToRefVecMap& regToRefVecMap, |
| 294 | ValueToDefVecMap& valueToDefVecMap); |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 295 | |
Vikram S. Adve | c352d2c | 2001-11-05 04:04:23 +0000 | [diff] [blame] | 296 | void findDefUseInfoAtInstr (const TargetMachine& target, |
| 297 | SchedGraphNode* node, |
Vikram S. Adve | 7952d60 | 2003-05-31 07:37:05 +0000 | [diff] [blame] | 298 | std::vector<SchedGraphNode*>& memNV, |
| 299 | std::vector<SchedGraphNode*>& callNV, |
Vikram S. Adve | c352d2c | 2001-11-05 04:04:23 +0000 | [diff] [blame] | 300 | RegToRefVecMap& regToRefVecMap, |
| 301 | ValueToDefVecMap& valueToDefVecMap); |
| 302 | |
Vikram S. Adve | e64574c | 2001-11-08 05:20:23 +0000 | [diff] [blame] | 303 | void addEdgesForInstruction(const MachineInstr& minstr, |
Vikram S. Adve | c352d2c | 2001-11-05 04:04:23 +0000 | [diff] [blame] | 304 | const ValueToDefVecMap& valueToDefVecMap, |
| 305 | const TargetMachine& target); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 306 | |
| 307 | void addCDEdges (const TerminatorInst* term, |
| 308 | const TargetMachine& target); |
| 309 | |
Vikram S. Adve | 7952d60 | 2003-05-31 07:37:05 +0000 | [diff] [blame] | 310 | void addMemEdges (const std::vector<SchedGraphNode*>& memNV, |
Vikram S. Adve | e64574c | 2001-11-08 05:20:23 +0000 | [diff] [blame] | 311 | const TargetMachine& target); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 312 | |
Vikram S. Adve | 7952d60 | 2003-05-31 07:37:05 +0000 | [diff] [blame] | 313 | void addCallDepEdges (const std::vector<SchedGraphNode*>& callNV, |
Vikram S. Adve | e64574c | 2001-11-08 05:20:23 +0000 | [diff] [blame] | 314 | const TargetMachine& target); |
Vikram S. Adve | d0d79c0 | 2001-10-28 21:45:02 +0000 | [diff] [blame] | 315 | |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 316 | void addMachineRegEdges (RegToRefVecMap& regToRefVecMap, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 317 | const TargetMachine& target); |
| 318 | |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 319 | void addEdgesForValue (SchedGraphNode* refNode, |
Vikram S. Adve | c352d2c | 2001-11-05 04:04:23 +0000 | [diff] [blame] | 320 | const RefVec& defVec, |
Vikram S. Adve | f43e336 | 2001-10-17 23:55:16 +0000 | [diff] [blame] | 321 | const Value* defValue, |
Vikram S. Adve | 200a435 | 2001-11-12 18:53:43 +0000 | [diff] [blame] | 322 | bool refNodeIsDef, |
Vikram S. Adve | 0baf1c0 | 2002-07-08 22:59:23 +0000 | [diff] [blame] | 323 | bool refNodeIsDefAndUse, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 324 | const TargetMachine& target); |
| 325 | |
| 326 | void addDummyEdges (); |
| 327 | }; |
| 328 | |
| 329 | |
| 330 | class SchedGraphSet : |
| 331 | public NonCopyable, |
Vikram S. Adve | 97fb99b | 2002-03-24 03:53:03 +0000 | [diff] [blame] | 332 | private std::vector<SchedGraph*> |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 333 | { |
| 334 | private: |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 335 | const Function* method; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 336 | |
| 337 | public: |
Vikram S. Adve | 97fb99b | 2002-03-24 03:53:03 +0000 | [diff] [blame] | 338 | typedef std::vector<SchedGraph*> baseVector; |
| 339 | using baseVector::iterator; |
| 340 | using baseVector::const_iterator; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 341 | |
| 342 | public: |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 343 | /*ctor*/ SchedGraphSet (const Function * function, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 344 | const TargetMachine& target); |
| 345 | /*dtor*/ ~SchedGraphSet (); |
| 346 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 347 | // Iterators |
Vikram S. Adve | 97fb99b | 2002-03-24 03:53:03 +0000 | [diff] [blame] | 348 | using baseVector::begin; |
| 349 | using baseVector::end; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 350 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 351 | // Debugging support |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 352 | void dump () const; |
| 353 | |
| 354 | private: |
Vikram S. Adve | 97fb99b | 2002-03-24 03:53:03 +0000 | [diff] [blame] | 355 | inline void addGraph(SchedGraph* graph) { |
| 356 | assert(graph != NULL); |
| 357 | this->push_back(graph); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 358 | } |
Vikram S. Adve | 97fb99b | 2002-03-24 03:53:03 +0000 | [diff] [blame] | 359 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 360 | // Graph builder |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 361 | void buildGraphsForMethod (const Function *F, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 362 | const TargetMachine& target); |
| 363 | }; |
| 364 | |
| 365 | |
| 366 | //********************** Sched Graph Iterators *****************************/ |
| 367 | |
| 368 | // Ok to make it a template because it shd get instantiated at most twice: |
| 369 | // for <SchedGraphNode, SchedGraphNode::iterator> and |
| 370 | // for <const SchedGraphNode, SchedGraphNode::const_iterator>. |
| 371 | // |
| 372 | template <class _NodeType, class _EdgeType, class _EdgeIter> |
Chris Lattner | 0c0edf8 | 2002-07-25 06:17:51 +0000 | [diff] [blame] | 373 | class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> { |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 374 | protected: |
| 375 | _EdgeIter oi; |
| 376 | public: |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 377 | typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 379 | inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {} |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 380 | |
| 381 | inline bool operator==(const _Self& x) const { return oi == x.oi; } |
| 382 | inline bool operator!=(const _Self& x) const { return !operator==(x); } |
| 383 | |
| 384 | // operator*() differs for pred or succ iterator |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 385 | inline _NodeType* operator*() const { return (*oi)->getSrc(); } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 386 | inline _NodeType* operator->() const { return operator*(); } |
| 387 | |
| 388 | inline _EdgeType* getEdge() const { return *(oi); } |
| 389 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 390 | inline _Self &operator++() { ++oi; return *this; } // Preincrement |
| 391 | inline _Self operator++(int) { // Postincrement |
| 392 | _Self tmp(*this); ++*this; return tmp; |
| 393 | } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 394 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 395 | inline _Self &operator--() { --oi; return *this; } // Predecrement |
| 396 | inline _Self operator--(int) { // Postdecrement |
| 397 | _Self tmp = *this; --*this; return tmp; |
| 398 | } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | template <class _NodeType, class _EdgeType, class _EdgeIter> |
Chris Lattner | 0c0edf8 | 2002-07-25 06:17:51 +0000 | [diff] [blame] | 402 | class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> { |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 403 | protected: |
| 404 | _EdgeIter oi; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 405 | public: |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 406 | typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 408 | inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {} |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 409 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 410 | inline bool operator==(const _Self& x) const { return oi == x.oi; } |
| 411 | inline bool operator!=(const _Self& x) const { return !operator==(x); } |
| 412 | |
| 413 | inline _NodeType* operator*() const { return (*oi)->getSink(); } |
| 414 | inline _NodeType* operator->() const { return operator*(); } |
| 415 | |
| 416 | inline _EdgeType* getEdge() const { return *(oi); } |
| 417 | |
| 418 | inline _Self &operator++() { ++oi; return *this; } // Preincrement |
| 419 | inline _Self operator++(int) { // Postincrement |
| 420 | _Self tmp(*this); ++*this; return tmp; |
| 421 | } |
| 422 | |
| 423 | inline _Self &operator--() { --oi; return *this; } // Predecrement |
| 424 | inline _Self operator--(int) { // Postdecrement |
| 425 | _Self tmp = *this; --*this; return tmp; |
| 426 | } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 429 | // |
| 430 | // sg_pred_iterator |
| 431 | // sg_pred_const_iterator |
| 432 | // |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 433 | typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator> |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 434 | sg_pred_iterator; |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 435 | typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator> |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 436 | sg_pred_const_iterator; |
| 437 | |
| 438 | inline sg_pred_iterator pred_begin( SchedGraphNode *N) { |
| 439 | return sg_pred_iterator(N->beginInEdges()); |
| 440 | } |
| 441 | inline sg_pred_iterator pred_end( SchedGraphNode *N) { |
| 442 | return sg_pred_iterator(N->endInEdges()); |
| 443 | } |
| 444 | inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) { |
| 445 | return sg_pred_const_iterator(N->beginInEdges()); |
| 446 | } |
| 447 | inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) { |
| 448 | return sg_pred_const_iterator(N->endInEdges()); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | // |
| 453 | // sg_succ_iterator |
| 454 | // sg_succ_const_iterator |
| 455 | // |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 456 | typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator> |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 457 | sg_succ_iterator; |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 458 | typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator> |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 459 | sg_succ_const_iterator; |
| 460 | |
| 461 | inline sg_succ_iterator succ_begin( SchedGraphNode *N) { |
| 462 | return sg_succ_iterator(N->beginOutEdges()); |
| 463 | } |
| 464 | inline sg_succ_iterator succ_end( SchedGraphNode *N) { |
| 465 | return sg_succ_iterator(N->endOutEdges()); |
| 466 | } |
| 467 | inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) { |
| 468 | return sg_succ_const_iterator(N->beginOutEdges()); |
| 469 | } |
| 470 | inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) { |
| 471 | return sg_succ_const_iterator(N->endOutEdges()); |
| 472 | } |
| 473 | |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 474 | // Provide specializations of GraphTraits to be able to use graph iterators on |
| 475 | // the scheduling graph! |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 476 | // |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 477 | template <> struct GraphTraits<SchedGraph*> { |
| 478 | typedef SchedGraphNode NodeType; |
| 479 | typedef sg_succ_iterator ChildIteratorType; |
| 480 | |
| 481 | static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); } |
| 482 | static inline ChildIteratorType child_begin(NodeType *N) { |
| 483 | return succ_begin(N); |
| 484 | } |
| 485 | static inline ChildIteratorType child_end(NodeType *N) { |
| 486 | return succ_end(N); |
| 487 | } |
| 488 | }; |
| 489 | |
| 490 | template <> struct GraphTraits<const SchedGraph*> { |
| 491 | typedef const SchedGraphNode NodeType; |
| 492 | typedef sg_succ_const_iterator ChildIteratorType; |
| 493 | |
| 494 | static inline NodeType *getEntryNode(const SchedGraph *SG) { |
| 495 | return SG->getRoot(); |
| 496 | } |
| 497 | static inline ChildIteratorType child_begin(NodeType *N) { |
| 498 | return succ_begin(N); |
| 499 | } |
| 500 | static inline ChildIteratorType child_end(NodeType *N) { |
| 501 | return succ_end(N); |
| 502 | } |
| 503 | }; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 504 | |
| 505 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 506 | std::ostream &operator<<(std::ostream& os, const SchedGraphEdge& edge); |
| 507 | std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 508 | |
| 509 | #endif |