Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1 | //===-- ScheduleDAGSimple.cpp - Implement a trivial DAG scheduler ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by James M. Laskey and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements a simple two pass scheduler. The first pass attempts to push |
| 11 | // backward any lengthy instructions and critical paths. The second pass packs |
| 12 | // instructions into semi-optimal time slots. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "sched" |
Jim Laskey | f492391 | 2006-08-03 20:51:06 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ScheduleDAG.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SelectionDAG.h" |
Jim Laskey | f492391 | 2006-08-03 20:51:06 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SSARegMap.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Jeff Cohen | 2aa750a | 2006-01-24 04:43:17 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 31 | |
| 32 | static RegisterScheduler |
| 33 | bfsDAGScheduler("none", " No scheduling: breadth first sequencing", |
| 34 | createBFS_DAGScheduler); |
| 35 | static RegisterScheduler |
| 36 | simpleDAGScheduler("simple", |
| 37 | " Simple two pass scheduling: minimize critical path " |
| 38 | "and maximize processor utilization", |
| 39 | createSimpleDAGScheduler); |
| 40 | static RegisterScheduler |
| 41 | noitinDAGScheduler("simple-noitin", |
| 42 | " Simple two pass scheduling: Same as simple " |
| 43 | "except using generic latency", |
| 44 | createNoItinsDAGScheduler); |
| 45 | |
Chris Lattner | a34b6f8 | 2006-03-10 07:51:18 +0000 | [diff] [blame] | 46 | class NodeInfo; |
| 47 | typedef NodeInfo *NodeInfoPtr; |
| 48 | typedef std::vector<NodeInfoPtr> NIVector; |
| 49 | typedef std::vector<NodeInfoPtr>::iterator NIIterator; |
| 50 | |
| 51 | //===--------------------------------------------------------------------===// |
| 52 | /// |
| 53 | /// Node group - This struct is used to manage flagged node groups. |
| 54 | /// |
| 55 | class NodeGroup { |
| 56 | public: |
| 57 | NodeGroup *Next; |
| 58 | private: |
| 59 | NIVector Members; // Group member nodes |
| 60 | NodeInfo *Dominator; // Node with highest latency |
| 61 | unsigned Latency; // Total latency of the group |
| 62 | int Pending; // Number of visits pending before |
| 63 | // adding to order |
| 64 | |
| 65 | public: |
| 66 | // Ctor. |
| 67 | NodeGroup() : Next(NULL), Dominator(NULL), Pending(0) {} |
| 68 | |
| 69 | // Accessors |
| 70 | inline void setDominator(NodeInfo *D) { Dominator = D; } |
| 71 | inline NodeInfo *getTop() { return Members.front(); } |
| 72 | inline NodeInfo *getBottom() { return Members.back(); } |
| 73 | inline NodeInfo *getDominator() { return Dominator; } |
| 74 | inline void setLatency(unsigned L) { Latency = L; } |
| 75 | inline unsigned getLatency() { return Latency; } |
| 76 | inline int getPending() const { return Pending; } |
| 77 | inline void setPending(int P) { Pending = P; } |
| 78 | inline int addPending(int I) { return Pending += I; } |
| 79 | |
| 80 | // Pass thru |
| 81 | inline bool group_empty() { return Members.empty(); } |
| 82 | inline NIIterator group_begin() { return Members.begin(); } |
| 83 | inline NIIterator group_end() { return Members.end(); } |
| 84 | inline void group_push_back(const NodeInfoPtr &NI) { |
| 85 | Members.push_back(NI); |
| 86 | } |
| 87 | inline NIIterator group_insert(NIIterator Pos, const NodeInfoPtr &NI) { |
| 88 | return Members.insert(Pos, NI); |
| 89 | } |
| 90 | inline void group_insert(NIIterator Pos, NIIterator First, |
| 91 | NIIterator Last) { |
| 92 | Members.insert(Pos, First, Last); |
| 93 | } |
| 94 | |
| 95 | static void Add(NodeInfo *D, NodeInfo *U); |
| 96 | }; |
| 97 | |
| 98 | //===--------------------------------------------------------------------===// |
| 99 | /// |
| 100 | /// NodeInfo - This struct tracks information used to schedule the a node. |
| 101 | /// |
| 102 | class NodeInfo { |
| 103 | private: |
| 104 | int Pending; // Number of visits pending before |
| 105 | // adding to order |
| 106 | public: |
| 107 | SDNode *Node; // DAG node |
| 108 | InstrStage *StageBegin; // First stage in itinerary |
| 109 | InstrStage *StageEnd; // Last+1 stage in itinerary |
| 110 | unsigned Latency; // Total cycles to complete instr |
| 111 | bool IsCall : 1; // Is function call |
| 112 | bool IsLoad : 1; // Is memory load |
| 113 | bool IsStore : 1; // Is memory store |
| 114 | unsigned Slot; // Node's time slot |
| 115 | NodeGroup *Group; // Grouping information |
| 116 | #ifndef NDEBUG |
| 117 | unsigned Preorder; // Index before scheduling |
| 118 | #endif |
| 119 | |
| 120 | // Ctor. |
| 121 | NodeInfo(SDNode *N = NULL) |
| 122 | : Pending(0) |
| 123 | , Node(N) |
| 124 | , StageBegin(NULL) |
| 125 | , StageEnd(NULL) |
| 126 | , Latency(0) |
| 127 | , IsCall(false) |
| 128 | , Slot(0) |
| 129 | , Group(NULL) |
| 130 | #ifndef NDEBUG |
| 131 | , Preorder(0) |
| 132 | #endif |
| 133 | {} |
| 134 | |
| 135 | // Accessors |
| 136 | inline bool isInGroup() const { |
| 137 | assert(!Group || !Group->group_empty() && "Group with no members"); |
| 138 | return Group != NULL; |
| 139 | } |
| 140 | inline bool isGroupDominator() const { |
| 141 | return isInGroup() && Group->getDominator() == this; |
| 142 | } |
| 143 | inline int getPending() const { |
| 144 | return Group ? Group->getPending() : Pending; |
| 145 | } |
| 146 | inline void setPending(int P) { |
| 147 | if (Group) Group->setPending(P); |
| 148 | else Pending = P; |
| 149 | } |
| 150 | inline int addPending(int I) { |
| 151 | if (Group) return Group->addPending(I); |
| 152 | else return Pending += I; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | //===--------------------------------------------------------------------===// |
| 157 | /// |
| 158 | /// NodeGroupIterator - Iterates over all the nodes indicated by the node |
| 159 | /// info. If the node is in a group then iterate over the members of the |
| 160 | /// group, otherwise just the node info. |
| 161 | /// |
| 162 | class NodeGroupIterator { |
| 163 | private: |
| 164 | NodeInfo *NI; // Node info |
| 165 | NIIterator NGI; // Node group iterator |
| 166 | NIIterator NGE; // Node group iterator end |
| 167 | |
| 168 | public: |
| 169 | // Ctor. |
| 170 | NodeGroupIterator(NodeInfo *N) : NI(N) { |
| 171 | // If the node is in a group then set up the group iterator. Otherwise |
| 172 | // the group iterators will trip first time out. |
| 173 | if (N->isInGroup()) { |
| 174 | // get Group |
| 175 | NodeGroup *Group = NI->Group; |
| 176 | NGI = Group->group_begin(); |
| 177 | NGE = Group->group_end(); |
| 178 | // Prevent this node from being used (will be in members list |
| 179 | NI = NULL; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /// next - Return the next node info, otherwise NULL. |
| 184 | /// |
| 185 | NodeInfo *next() { |
| 186 | // If members list |
| 187 | if (NGI != NGE) return *NGI++; |
| 188 | // Use node as the result (may be NULL) |
| 189 | NodeInfo *Result = NI; |
| 190 | // Only use once |
| 191 | NI = NULL; |
| 192 | // Return node or NULL |
| 193 | return Result; |
| 194 | } |
| 195 | }; |
| 196 | //===--------------------------------------------------------------------===// |
| 197 | |
| 198 | |
| 199 | //===--------------------------------------------------------------------===// |
| 200 | /// |
| 201 | /// NodeGroupOpIterator - Iterates over all the operands of a node. If the |
| 202 | /// node is a member of a group, this iterates over all the operands of all |
| 203 | /// the members of the group. |
| 204 | /// |
| 205 | class NodeGroupOpIterator { |
| 206 | private: |
| 207 | NodeInfo *NI; // Node containing operands |
| 208 | NodeGroupIterator GI; // Node group iterator |
| 209 | SDNode::op_iterator OI; // Operand iterator |
| 210 | SDNode::op_iterator OE; // Operand iterator end |
| 211 | |
| 212 | /// CheckNode - Test if node has more operands. If not get the next node |
| 213 | /// skipping over nodes that have no operands. |
| 214 | void CheckNode() { |
| 215 | // Only if operands are exhausted first |
| 216 | while (OI == OE) { |
| 217 | // Get next node info |
| 218 | NodeInfo *NI = GI.next(); |
| 219 | // Exit if nodes are exhausted |
| 220 | if (!NI) return; |
| 221 | // Get node itself |
| 222 | SDNode *Node = NI->Node; |
| 223 | // Set up the operand iterators |
| 224 | OI = Node->op_begin(); |
| 225 | OE = Node->op_end(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public: |
| 230 | // Ctor. |
| 231 | NodeGroupOpIterator(NodeInfo *N) |
| 232 | : NI(N), GI(N), OI(SDNode::op_iterator()), OE(SDNode::op_iterator()) {} |
| 233 | |
| 234 | /// isEnd - Returns true when not more operands are available. |
| 235 | /// |
| 236 | inline bool isEnd() { CheckNode(); return OI == OE; } |
| 237 | |
| 238 | /// next - Returns the next available operand. |
| 239 | /// |
| 240 | inline SDOperand next() { |
| 241 | assert(OI != OE && |
| 242 | "Not checking for end of NodeGroupOpIterator correctly"); |
| 243 | return *OI++; |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 248 | //===----------------------------------------------------------------------===// |
| 249 | /// |
| 250 | /// BitsIterator - Provides iteration through individual bits in a bit vector. |
| 251 | /// |
| 252 | template<class T> |
| 253 | class BitsIterator { |
| 254 | private: |
| 255 | T Bits; // Bits left to iterate through |
| 256 | |
| 257 | public: |
| 258 | /// Ctor. |
| 259 | BitsIterator(T Initial) : Bits(Initial) {} |
| 260 | |
| 261 | /// Next - Returns the next bit set or zero if exhausted. |
| 262 | inline T Next() { |
| 263 | // Get the rightmost bit set |
| 264 | T Result = Bits & -Bits; |
| 265 | // Remove from rest |
| 266 | Bits &= ~Result; |
| 267 | // Return single bit or zero |
| 268 | return Result; |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
| 274 | |
| 275 | //===----------------------------------------------------------------------===// |
| 276 | /// |
| 277 | /// ResourceTally - Manages the use of resources over time intervals. Each |
| 278 | /// item (slot) in the tally vector represents the resources used at a given |
| 279 | /// moment. A bit set to 1 indicates that a resource is in use, otherwise |
| 280 | /// available. An assumption is made that the tally is large enough to schedule |
| 281 | /// all current instructions (asserts otherwise.) |
| 282 | /// |
| 283 | template<class T> |
| 284 | class ResourceTally { |
| 285 | private: |
| 286 | std::vector<T> Tally; // Resources used per slot |
| 287 | typedef typename std::vector<T>::iterator Iter; |
| 288 | // Tally iterator |
| 289 | |
| 290 | /// SlotsAvailable - Returns true if all units are available. |
| 291 | /// |
| 292 | bool SlotsAvailable(Iter Begin, unsigned N, unsigned ResourceSet, |
| 293 | unsigned &Resource) { |
| 294 | assert(N && "Must check availability with N != 0"); |
| 295 | // Determine end of interval |
| 296 | Iter End = Begin + N; |
| 297 | assert(End <= Tally.end() && "Tally is not large enough for schedule"); |
| 298 | |
| 299 | // Iterate thru each resource |
| 300 | BitsIterator<T> Resources(ResourceSet & ~*Begin); |
| 301 | while (unsigned Res = Resources.Next()) { |
| 302 | // Check if resource is available for next N slots |
| 303 | Iter Interval = End; |
| 304 | do { |
| 305 | Interval--; |
| 306 | if (*Interval & Res) break; |
| 307 | } while (Interval != Begin); |
| 308 | |
| 309 | // If available for N |
| 310 | if (Interval == Begin) { |
| 311 | // Success |
| 312 | Resource = Res; |
| 313 | return true; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // No luck |
| 318 | Resource = 0; |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | /// RetrySlot - Finds a good candidate slot to retry search. |
| 323 | Iter RetrySlot(Iter Begin, unsigned N, unsigned ResourceSet) { |
| 324 | assert(N && "Must check availability with N != 0"); |
| 325 | // Determine end of interval |
| 326 | Iter End = Begin + N; |
| 327 | assert(End <= Tally.end() && "Tally is not large enough for schedule"); |
| 328 | |
| 329 | while (Begin != End--) { |
| 330 | // Clear units in use |
| 331 | ResourceSet &= ~*End; |
| 332 | // If no units left then we should go no further |
| 333 | if (!ResourceSet) return End + 1; |
| 334 | } |
| 335 | // Made it all the way through |
| 336 | return Begin; |
| 337 | } |
| 338 | |
| 339 | /// FindAndReserveStages - Return true if the stages can be completed. If |
| 340 | /// so mark as busy. |
| 341 | bool FindAndReserveStages(Iter Begin, |
| 342 | InstrStage *Stage, InstrStage *StageEnd) { |
| 343 | // If at last stage then we're done |
| 344 | if (Stage == StageEnd) return true; |
| 345 | // Get number of cycles for current stage |
| 346 | unsigned N = Stage->Cycles; |
| 347 | // Check to see if N slots are available, if not fail |
| 348 | unsigned Resource; |
| 349 | if (!SlotsAvailable(Begin, N, Stage->Units, Resource)) return false; |
| 350 | // Check to see if remaining stages are available, if not fail |
| 351 | if (!FindAndReserveStages(Begin + N, Stage + 1, StageEnd)) return false; |
| 352 | // Reserve resource |
| 353 | Reserve(Begin, N, Resource); |
| 354 | // Success |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | /// Reserve - Mark busy (set) the specified N slots. |
| 359 | void Reserve(Iter Begin, unsigned N, unsigned Resource) { |
| 360 | // Determine end of interval |
| 361 | Iter End = Begin + N; |
| 362 | assert(End <= Tally.end() && "Tally is not large enough for schedule"); |
| 363 | |
| 364 | // Set resource bit in each slot |
| 365 | for (; Begin < End; Begin++) |
| 366 | *Begin |= Resource; |
| 367 | } |
| 368 | |
| 369 | /// FindSlots - Starting from Begin, locate consecutive slots where all stages |
| 370 | /// can be completed. Returns the address of first slot. |
| 371 | Iter FindSlots(Iter Begin, InstrStage *StageBegin, InstrStage *StageEnd) { |
| 372 | // Track position |
| 373 | Iter Cursor = Begin; |
| 374 | |
| 375 | // Try all possible slots forward |
| 376 | while (true) { |
| 377 | // Try at cursor, if successful return position. |
| 378 | if (FindAndReserveStages(Cursor, StageBegin, StageEnd)) return Cursor; |
| 379 | // Locate a better position |
| 380 | Cursor = RetrySlot(Cursor + 1, StageBegin->Cycles, StageBegin->Units); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | public: |
| 385 | /// Initialize - Resize and zero the tally to the specified number of time |
| 386 | /// slots. |
| 387 | inline void Initialize(unsigned N) { |
| 388 | Tally.assign(N, 0); // Initialize tally to all zeros. |
| 389 | } |
| 390 | |
| 391 | // FindAndReserve - Locate an ideal slot for the specified stages and mark |
| 392 | // as busy. |
| 393 | unsigned FindAndReserve(unsigned Slot, InstrStage *StageBegin, |
| 394 | InstrStage *StageEnd) { |
| 395 | // Where to begin |
| 396 | Iter Begin = Tally.begin() + Slot; |
| 397 | // Find a free slot |
| 398 | Iter Where = FindSlots(Begin, StageBegin, StageEnd); |
| 399 | // Distance is slot number |
| 400 | unsigned Final = Where - Tally.begin(); |
| 401 | return Final; |
| 402 | } |
| 403 | |
| 404 | }; |
| 405 | |
| 406 | //===----------------------------------------------------------------------===// |
| 407 | /// |
| 408 | /// ScheduleDAGSimple - Simple two pass scheduler. |
| 409 | /// |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 410 | class VISIBILITY_HIDDEN ScheduleDAGSimple : public ScheduleDAG { |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 411 | private: |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 412 | bool NoSched; // Just do a BFS schedule, nothing fancy |
| 413 | bool NoItins; // Don't use itineraries? |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 414 | ResourceTally<unsigned> Tally; // Resource usage tally |
| 415 | unsigned NSlots; // Total latency |
| 416 | static const unsigned NotFound = ~0U; // Search marker |
Chris Lattner | 2f5806c | 2006-03-10 07:42:02 +0000 | [diff] [blame] | 417 | |
| 418 | unsigned NodeCount; // Number of nodes in DAG |
| 419 | std::map<SDNode *, NodeInfo *> Map; // Map nodes to info |
| 420 | bool HasGroups; // True if there are any groups |
| 421 | NodeInfo *Info; // Info for nodes being scheduled |
| 422 | NIVector Ordering; // Emit ordering of nodes |
| 423 | NodeGroup *HeadNG, *TailNG; // Keep track of allocated NodeGroups |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 424 | |
| 425 | public: |
| 426 | |
| 427 | // Ctor. |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 428 | ScheduleDAGSimple(bool noSched, bool noItins, SelectionDAG &dag, |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 429 | MachineBasicBlock *bb, const TargetMachine &tm) |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 430 | : ScheduleDAG(dag, bb, tm), NoSched(noSched), NoItins(noItins), NSlots(0), |
Chris Lattner | 2f5806c | 2006-03-10 07:42:02 +0000 | [diff] [blame] | 431 | NodeCount(0), HasGroups(false), Info(NULL), HeadNG(NULL), TailNG(NULL) { |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 432 | assert(&TII && "Target doesn't provide instr info?"); |
| 433 | assert(&MRI && "Target doesn't provide register info?"); |
| 434 | } |
| 435 | |
Chris Lattner | 2f5806c | 2006-03-10 07:42:02 +0000 | [diff] [blame] | 436 | virtual ~ScheduleDAGSimple() { |
| 437 | if (Info) |
| 438 | delete[] Info; |
| 439 | |
| 440 | NodeGroup *NG = HeadNG; |
| 441 | while (NG) { |
| 442 | NodeGroup *NextSU = NG->Next; |
| 443 | delete NG; |
| 444 | NG = NextSU; |
| 445 | } |
| 446 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 447 | |
Evan Cheng | 4148429 | 2006-01-23 08:25:34 +0000 | [diff] [blame] | 448 | void Schedule(); |
| 449 | |
Chris Lattner | 2f5806c | 2006-03-10 07:42:02 +0000 | [diff] [blame] | 450 | /// getNI - Returns the node info for the specified node. |
| 451 | /// |
| 452 | NodeInfo *getNI(SDNode *Node) { return Map[Node]; } |
| 453 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 454 | private: |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 455 | static bool isDefiner(NodeInfo *A, NodeInfo *B); |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 456 | void IncludeNode(NodeInfo *NI); |
| 457 | void VisitAll(); |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 458 | void GatherSchedulingInfo(); |
| 459 | void FakeGroupDominators(); |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 460 | bool isStrongDependency(NodeInfo *A, NodeInfo *B); |
| 461 | bool isWeakDependency(NodeInfo *A, NodeInfo *B); |
| 462 | void ScheduleBackward(); |
| 463 | void ScheduleForward(); |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 464 | |
| 465 | void AddToGroup(NodeInfo *D, NodeInfo *U); |
| 466 | /// PrepareNodeInfo - Set up the basic minimum node info for scheduling. |
| 467 | /// |
| 468 | void PrepareNodeInfo(); |
| 469 | |
| 470 | /// IdentifyGroups - Put flagged nodes into groups. |
| 471 | /// |
| 472 | void IdentifyGroups(); |
| 473 | |
| 474 | /// print - Print ordering to specified output stream. |
| 475 | /// |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame^] | 476 | void print(std::ostream &O) const; |
| 477 | void print(std::ostream *O) const { if (O) print(*O); } |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 478 | |
| 479 | void dump(const char *tag) const; |
| 480 | |
| 481 | virtual void dump() const; |
| 482 | |
| 483 | /// EmitAll - Emit all nodes in schedule sorted order. |
| 484 | /// |
| 485 | void EmitAll(); |
| 486 | |
| 487 | /// printNI - Print node info. |
| 488 | /// |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame^] | 489 | void printNI(std::ostream &O, NodeInfo *NI) const; |
| 490 | void printNI(std::ostream *O, NodeInfo *NI) const { if (O) printNI(O, NI); } |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 491 | |
| 492 | /// printChanges - Hilight changes in order caused by scheduling. |
| 493 | /// |
| 494 | void printChanges(unsigned Index) const; |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 495 | }; |
| 496 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 497 | //===----------------------------------------------------------------------===// |
| 498 | /// Special case itineraries. |
| 499 | /// |
| 500 | enum { |
| 501 | CallLatency = 40, // To push calls back in time |
| 502 | |
| 503 | RSInteger = 0xC0000000, // Two integer units |
| 504 | RSFloat = 0x30000000, // Two float units |
| 505 | RSLoadStore = 0x0C000000, // Two load store units |
| 506 | RSBranch = 0x02000000 // One branch unit |
| 507 | }; |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 508 | static InstrStage LoadStage = { 5, RSLoadStore }; |
| 509 | static InstrStage StoreStage = { 2, RSLoadStore }; |
| 510 | static InstrStage IntStage = { 2, RSInteger }; |
| 511 | static InstrStage FloatStage = { 3, RSFloat }; |
| 512 | //===----------------------------------------------------------------------===// |
| 513 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 514 | } // namespace |
| 515 | |
| 516 | //===----------------------------------------------------------------------===// |
| 517 | |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 518 | /// PrepareNodeInfo - Set up the basic minimum node info for scheduling. |
| 519 | /// |
| 520 | void ScheduleDAGSimple::PrepareNodeInfo() { |
| 521 | // Allocate node information |
| 522 | Info = new NodeInfo[NodeCount]; |
| 523 | |
| 524 | unsigned i = 0; |
| 525 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 526 | E = DAG.allnodes_end(); I != E; ++I, ++i) { |
| 527 | // Fast reference to node schedule info |
| 528 | NodeInfo* NI = &Info[i]; |
| 529 | // Set up map |
| 530 | Map[I] = NI; |
| 531 | // Set node |
| 532 | NI->Node = I; |
| 533 | // Set pending visit count |
| 534 | NI->setPending(I->use_size()); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /// IdentifyGroups - Put flagged nodes into groups. |
| 539 | /// |
| 540 | void ScheduleDAGSimple::IdentifyGroups() { |
| 541 | for (unsigned i = 0, N = NodeCount; i < N; i++) { |
| 542 | NodeInfo* NI = &Info[i]; |
| 543 | SDNode *Node = NI->Node; |
| 544 | |
| 545 | // For each operand (in reverse to only look at flags) |
| 546 | for (unsigned N = Node->getNumOperands(); 0 < N--;) { |
| 547 | // Get operand |
| 548 | SDOperand Op = Node->getOperand(N); |
| 549 | // No more flags to walk |
| 550 | if (Op.getValueType() != MVT::Flag) break; |
| 551 | // Add to node group |
| 552 | AddToGroup(getNI(Op.Val), NI); |
| 553 | // Let everyone else know |
| 554 | HasGroups = true; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /// CountInternalUses - Returns the number of edges between the two nodes. |
| 560 | /// |
| 561 | static unsigned CountInternalUses(NodeInfo *D, NodeInfo *U) { |
| 562 | unsigned N = 0; |
| 563 | for (unsigned M = U->Node->getNumOperands(); 0 < M--;) { |
| 564 | SDOperand Op = U->Node->getOperand(M); |
| 565 | if (Op.Val == D->Node) N++; |
| 566 | } |
| 567 | |
| 568 | return N; |
| 569 | } |
| 570 | |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | /// Add - Adds a definer and user pair to a node group. |
| 573 | /// |
| 574 | void ScheduleDAGSimple::AddToGroup(NodeInfo *D, NodeInfo *U) { |
| 575 | // Get current groups |
| 576 | NodeGroup *DGroup = D->Group; |
| 577 | NodeGroup *UGroup = U->Group; |
| 578 | // If both are members of groups |
| 579 | if (DGroup && UGroup) { |
| 580 | // There may have been another edge connecting |
| 581 | if (DGroup == UGroup) return; |
| 582 | // Add the pending users count |
| 583 | DGroup->addPending(UGroup->getPending()); |
| 584 | // For each member of the users group |
| 585 | NodeGroupIterator UNGI(U); |
| 586 | while (NodeInfo *UNI = UNGI.next() ) { |
| 587 | // Change the group |
| 588 | UNI->Group = DGroup; |
| 589 | // For each member of the definers group |
| 590 | NodeGroupIterator DNGI(D); |
| 591 | while (NodeInfo *DNI = DNGI.next() ) { |
| 592 | // Remove internal edges |
| 593 | DGroup->addPending(-CountInternalUses(DNI, UNI)); |
| 594 | } |
| 595 | } |
| 596 | // Merge the two lists |
| 597 | DGroup->group_insert(DGroup->group_end(), |
| 598 | UGroup->group_begin(), UGroup->group_end()); |
| 599 | } else if (DGroup) { |
| 600 | // Make user member of definers group |
| 601 | U->Group = DGroup; |
| 602 | // Add users uses to definers group pending |
| 603 | DGroup->addPending(U->Node->use_size()); |
| 604 | // For each member of the definers group |
| 605 | NodeGroupIterator DNGI(D); |
| 606 | while (NodeInfo *DNI = DNGI.next() ) { |
| 607 | // Remove internal edges |
| 608 | DGroup->addPending(-CountInternalUses(DNI, U)); |
| 609 | } |
| 610 | DGroup->group_push_back(U); |
| 611 | } else if (UGroup) { |
| 612 | // Make definer member of users group |
| 613 | D->Group = UGroup; |
| 614 | // Add definers uses to users group pending |
| 615 | UGroup->addPending(D->Node->use_size()); |
| 616 | // For each member of the users group |
| 617 | NodeGroupIterator UNGI(U); |
| 618 | while (NodeInfo *UNI = UNGI.next() ) { |
| 619 | // Remove internal edges |
| 620 | UGroup->addPending(-CountInternalUses(D, UNI)); |
| 621 | } |
| 622 | UGroup->group_insert(UGroup->group_begin(), D); |
| 623 | } else { |
| 624 | D->Group = U->Group = DGroup = new NodeGroup(); |
| 625 | DGroup->addPending(D->Node->use_size() + U->Node->use_size() - |
| 626 | CountInternalUses(D, U)); |
| 627 | DGroup->group_push_back(D); |
| 628 | DGroup->group_push_back(U); |
| 629 | |
| 630 | if (HeadNG == NULL) |
| 631 | HeadNG = DGroup; |
| 632 | if (TailNG != NULL) |
| 633 | TailNG->Next = DGroup; |
| 634 | TailNG = DGroup; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | |
| 639 | /// print - Print ordering to specified output stream. |
| 640 | /// |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame^] | 641 | void ScheduleDAGSimple::print(std::ostream &O) const { |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 642 | #ifndef NDEBUG |
| 643 | O << "Ordering\n"; |
| 644 | for (unsigned i = 0, N = Ordering.size(); i < N; i++) { |
| 645 | NodeInfo *NI = Ordering[i]; |
| 646 | printNI(O, NI); |
| 647 | O << "\n"; |
| 648 | if (NI->isGroupDominator()) { |
| 649 | NodeGroup *Group = NI->Group; |
| 650 | for (NIIterator NII = Group->group_begin(), E = Group->group_end(); |
| 651 | NII != E; NII++) { |
| 652 | O << " "; |
| 653 | printNI(O, *NII); |
| 654 | O << "\n"; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | #endif |
| 659 | } |
| 660 | |
| 661 | void ScheduleDAGSimple::dump(const char *tag) const { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 662 | cerr << tag; dump(); |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | void ScheduleDAGSimple::dump() const { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 666 | print(cerr); |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | |
| 670 | /// EmitAll - Emit all nodes in schedule sorted order. |
| 671 | /// |
| 672 | void ScheduleDAGSimple::EmitAll() { |
Jim Laskey | f492391 | 2006-08-03 20:51:06 +0000 | [diff] [blame] | 673 | // If this is the first basic block in the function, and if it has live ins |
| 674 | // that need to be copied into vregs, emit the copies into the top of the |
| 675 | // block before emitting the code for the block. |
| 676 | MachineFunction &MF = DAG.getMachineFunction(); |
| 677 | if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) { |
| 678 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 679 | E = MF.livein_end(); LI != E; ++LI) |
| 680 | if (LI->second) |
| 681 | MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second, |
| 682 | LI->first, RegMap->getRegClass(LI->second)); |
| 683 | } |
| 684 | |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 685 | std::map<SDNode*, unsigned> VRBaseMap; |
| 686 | |
| 687 | // For each node in the ordering |
| 688 | for (unsigned i = 0, N = Ordering.size(); i < N; i++) { |
| 689 | // Get the scheduling info |
| 690 | NodeInfo *NI = Ordering[i]; |
| 691 | if (NI->isInGroup()) { |
| 692 | NodeGroupIterator NGI(Ordering[i]); |
| 693 | while (NodeInfo *NI = NGI.next()) EmitNode(NI->Node, VRBaseMap); |
| 694 | } else { |
| 695 | EmitNode(NI->Node, VRBaseMap); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /// isFlagDefiner - Returns true if the node defines a flag result. |
| 701 | static bool isFlagDefiner(SDNode *A) { |
| 702 | unsigned N = A->getNumValues(); |
| 703 | return N && A->getValueType(N - 1) == MVT::Flag; |
| 704 | } |
| 705 | |
| 706 | /// isFlagUser - Returns true if the node uses a flag result. |
| 707 | /// |
| 708 | static bool isFlagUser(SDNode *A) { |
| 709 | unsigned N = A->getNumOperands(); |
| 710 | return N && A->getOperand(N - 1).getValueType() == MVT::Flag; |
| 711 | } |
| 712 | |
| 713 | /// printNI - Print node info. |
| 714 | /// |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame^] | 715 | void ScheduleDAGSimple::printNI(std::ostream &O, NodeInfo *NI) const { |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 716 | #ifndef NDEBUG |
| 717 | SDNode *Node = NI->Node; |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame^] | 718 | O << " " |
| 719 | << std::hex << Node << std::dec |
| 720 | << ", Lat=" << NI->Latency |
| 721 | << ", Slot=" << NI->Slot |
| 722 | << ", ARITY=(" << Node->getNumOperands() << "," |
| 723 | << Node->getNumValues() << ")" |
| 724 | << " " << Node->getOperationName(&DAG); |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 725 | if (isFlagDefiner(Node)) O << "<#"; |
| 726 | if (isFlagUser(Node)) O << ">#"; |
| 727 | #endif |
| 728 | } |
| 729 | |
| 730 | /// printChanges - Hilight changes in order caused by scheduling. |
| 731 | /// |
| 732 | void ScheduleDAGSimple::printChanges(unsigned Index) const { |
| 733 | #ifndef NDEBUG |
| 734 | // Get the ordered node count |
| 735 | unsigned N = Ordering.size(); |
| 736 | // Determine if any changes |
| 737 | unsigned i = 0; |
| 738 | for (; i < N; i++) { |
| 739 | NodeInfo *NI = Ordering[i]; |
| 740 | if (NI->Preorder != i) break; |
| 741 | } |
| 742 | |
| 743 | if (i < N) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 744 | cerr << Index << ". New Ordering\n"; |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 745 | |
| 746 | for (i = 0; i < N; i++) { |
| 747 | NodeInfo *NI = Ordering[i]; |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 748 | cerr << " " << NI->Preorder << ". "; |
| 749 | printNI(cerr, NI); |
| 750 | cerr << "\n"; |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 751 | if (NI->isGroupDominator()) { |
| 752 | NodeGroup *Group = NI->Group; |
| 753 | for (NIIterator NII = Group->group_begin(), E = Group->group_end(); |
| 754 | NII != E; NII++) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 755 | cerr << " "; |
| 756 | printNI(cerr, *NII); |
| 757 | cerr << "\n"; |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | } |
| 761 | } else { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 762 | cerr << Index << ". No Changes\n"; |
Chris Lattner | e76074a | 2006-03-10 07:35:21 +0000 | [diff] [blame] | 763 | } |
| 764 | #endif |
| 765 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 766 | |
| 767 | //===----------------------------------------------------------------------===// |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 768 | /// isDefiner - Return true if node A is a definer for B. |
| 769 | /// |
| 770 | bool ScheduleDAGSimple::isDefiner(NodeInfo *A, NodeInfo *B) { |
| 771 | // While there are A nodes |
| 772 | NodeGroupIterator NII(A); |
| 773 | while (NodeInfo *NI = NII.next()) { |
| 774 | // Extract node |
| 775 | SDNode *Node = NI->Node; |
| 776 | // While there operands in nodes of B |
| 777 | NodeGroupOpIterator NGOI(B); |
| 778 | while (!NGOI.isEnd()) { |
| 779 | SDOperand Op = NGOI.next(); |
| 780 | // If node from A defines a node in B |
| 781 | if (Node == Op.Val) return true; |
| 782 | } |
| 783 | } |
| 784 | return false; |
| 785 | } |
| 786 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 787 | /// IncludeNode - Add node to NodeInfo vector. |
| 788 | /// |
| 789 | void ScheduleDAGSimple::IncludeNode(NodeInfo *NI) { |
| 790 | // Get node |
| 791 | SDNode *Node = NI->Node; |
| 792 | // Ignore entry node |
| 793 | if (Node->getOpcode() == ISD::EntryToken) return; |
| 794 | // Check current count for node |
| 795 | int Count = NI->getPending(); |
| 796 | // If the node is already in list |
| 797 | if (Count < 0) return; |
| 798 | // Decrement count to indicate a visit |
| 799 | Count--; |
| 800 | // If count has gone to zero then add node to list |
| 801 | if (!Count) { |
| 802 | // Add node |
| 803 | if (NI->isInGroup()) { |
| 804 | Ordering.push_back(NI->Group->getDominator()); |
| 805 | } else { |
| 806 | Ordering.push_back(NI); |
| 807 | } |
| 808 | // indicate node has been added |
| 809 | Count--; |
| 810 | } |
| 811 | // Mark as visited with new count |
| 812 | NI->setPending(Count); |
| 813 | } |
| 814 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 815 | /// GatherSchedulingInfo - Get latency and resource information about each node. |
| 816 | /// |
| 817 | void ScheduleDAGSimple::GatherSchedulingInfo() { |
| 818 | // Get instruction itineraries for the target |
Chris Lattner | e70f671 | 2006-03-09 07:13:00 +0000 | [diff] [blame] | 819 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 820 | |
| 821 | // For each node |
| 822 | for (unsigned i = 0, N = NodeCount; i < N; i++) { |
| 823 | // Get node info |
| 824 | NodeInfo* NI = &Info[i]; |
| 825 | SDNode *Node = NI->Node; |
| 826 | |
| 827 | // If there are itineraries and it is a machine instruction |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 828 | if (InstrItins.isEmpty() || NoItins) { |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 829 | // If machine opcode |
| 830 | if (Node->isTargetOpcode()) { |
| 831 | // Get return type to guess which processing unit |
| 832 | MVT::ValueType VT = Node->getValueType(0); |
| 833 | // Get machine opcode |
| 834 | MachineOpCode TOpc = Node->getTargetOpcode(); |
| 835 | NI->IsCall = TII->isCall(TOpc); |
| 836 | NI->IsLoad = TII->isLoad(TOpc); |
| 837 | NI->IsStore = TII->isStore(TOpc); |
| 838 | |
| 839 | if (TII->isLoad(TOpc)) NI->StageBegin = &LoadStage; |
| 840 | else if (TII->isStore(TOpc)) NI->StageBegin = &StoreStage; |
| 841 | else if (MVT::isInteger(VT)) NI->StageBegin = &IntStage; |
| 842 | else if (MVT::isFloatingPoint(VT)) NI->StageBegin = &FloatStage; |
| 843 | if (NI->StageBegin) NI->StageEnd = NI->StageBegin + 1; |
| 844 | } |
| 845 | } else if (Node->isTargetOpcode()) { |
| 846 | // get machine opcode |
| 847 | MachineOpCode TOpc = Node->getTargetOpcode(); |
| 848 | // Check to see if it is a call |
| 849 | NI->IsCall = TII->isCall(TOpc); |
| 850 | // Get itinerary stages for instruction |
| 851 | unsigned II = TII->getSchedClass(TOpc); |
| 852 | NI->StageBegin = InstrItins.begin(II); |
| 853 | NI->StageEnd = InstrItins.end(II); |
| 854 | } |
| 855 | |
| 856 | // One slot for the instruction itself |
| 857 | NI->Latency = 1; |
| 858 | |
| 859 | // Add long latency for a call to push it back in time |
| 860 | if (NI->IsCall) NI->Latency += CallLatency; |
| 861 | |
| 862 | // Sum up all the latencies |
| 863 | for (InstrStage *Stage = NI->StageBegin, *E = NI->StageEnd; |
| 864 | Stage != E; Stage++) { |
| 865 | NI->Latency += Stage->Cycles; |
| 866 | } |
| 867 | |
| 868 | // Sum up all the latencies for max tally size |
| 869 | NSlots += NI->Latency; |
| 870 | } |
| 871 | |
| 872 | // Unify metrics if in a group |
| 873 | if (HasGroups) { |
| 874 | for (unsigned i = 0, N = NodeCount; i < N; i++) { |
| 875 | NodeInfo* NI = &Info[i]; |
| 876 | |
| 877 | if (NI->isInGroup()) { |
| 878 | NodeGroup *Group = NI->Group; |
| 879 | |
| 880 | if (!Group->getDominator()) { |
| 881 | NIIterator NGI = Group->group_begin(), NGE = Group->group_end(); |
| 882 | NodeInfo *Dominator = *NGI; |
| 883 | unsigned Latency = 0; |
| 884 | |
| 885 | for (NGI++; NGI != NGE; NGI++) { |
| 886 | NodeInfo* NGNI = *NGI; |
| 887 | Latency += NGNI->Latency; |
| 888 | if (Dominator->Latency < NGNI->Latency) Dominator = NGNI; |
| 889 | } |
| 890 | |
| 891 | Dominator->Latency = Latency; |
| 892 | Group->setDominator(Dominator); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 899 | /// VisitAll - Visit each node breadth-wise to produce an initial ordering. |
| 900 | /// Note that the ordering in the Nodes vector is reversed. |
| 901 | void ScheduleDAGSimple::VisitAll() { |
| 902 | // Add first element to list |
| 903 | NodeInfo *NI = getNI(DAG.getRoot().Val); |
| 904 | if (NI->isInGroup()) { |
| 905 | Ordering.push_back(NI->Group->getDominator()); |
| 906 | } else { |
| 907 | Ordering.push_back(NI); |
| 908 | } |
| 909 | |
| 910 | // Iterate through all nodes that have been added |
| 911 | for (unsigned i = 0; i < Ordering.size(); i++) { // note: size() varies |
| 912 | // Visit all operands |
| 913 | NodeGroupOpIterator NGI(Ordering[i]); |
| 914 | while (!NGI.isEnd()) { |
| 915 | // Get next operand |
| 916 | SDOperand Op = NGI.next(); |
| 917 | // Get node |
| 918 | SDNode *Node = Op.Val; |
| 919 | // Ignore passive nodes |
| 920 | if (isPassiveNode(Node)) continue; |
| 921 | // Check out node |
| 922 | IncludeNode(getNI(Node)); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | // Add entry node last (IncludeNode filters entry nodes) |
| 927 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) |
| 928 | Ordering.push_back(getNI(DAG.getEntryNode().Val)); |
| 929 | |
| 930 | // Reverse the order |
| 931 | std::reverse(Ordering.begin(), Ordering.end()); |
| 932 | } |
| 933 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 934 | /// FakeGroupDominators - Set dominators for non-scheduling. |
| 935 | /// |
| 936 | void ScheduleDAGSimple::FakeGroupDominators() { |
| 937 | for (unsigned i = 0, N = NodeCount; i < N; i++) { |
| 938 | NodeInfo* NI = &Info[i]; |
| 939 | |
| 940 | if (NI->isInGroup()) { |
| 941 | NodeGroup *Group = NI->Group; |
| 942 | |
| 943 | if (!Group->getDominator()) { |
| 944 | Group->setDominator(NI); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 950 | /// isStrongDependency - Return true if node A has results used by node B. |
| 951 | /// I.E., B must wait for latency of A. |
| 952 | bool ScheduleDAGSimple::isStrongDependency(NodeInfo *A, NodeInfo *B) { |
| 953 | // If A defines for B then it's a strong dependency or |
| 954 | // if a load follows a store (may be dependent but why take a chance.) |
| 955 | return isDefiner(A, B) || (A->IsStore && B->IsLoad); |
| 956 | } |
| 957 | |
| 958 | /// isWeakDependency Return true if node A produces a result that will |
| 959 | /// conflict with operands of B. It is assumed that we have called |
| 960 | /// isStrongDependency prior. |
| 961 | bool ScheduleDAGSimple::isWeakDependency(NodeInfo *A, NodeInfo *B) { |
| 962 | // TODO check for conflicting real registers and aliases |
| 963 | #if 0 // FIXME - Since we are in SSA form and not checking register aliasing |
| 964 | return A->Node->getOpcode() == ISD::EntryToken || isStrongDependency(B, A); |
| 965 | #else |
| 966 | return A->Node->getOpcode() == ISD::EntryToken; |
| 967 | #endif |
| 968 | } |
| 969 | |
| 970 | /// ScheduleBackward - Schedule instructions so that any long latency |
| 971 | /// instructions and the critical path get pushed back in time. Time is run in |
| 972 | /// reverse to allow code reuse of the Tally and eliminate the overhead of |
| 973 | /// biasing every slot indices against NSlots. |
| 974 | void ScheduleDAGSimple::ScheduleBackward() { |
| 975 | // Size and clear the resource tally |
| 976 | Tally.Initialize(NSlots); |
| 977 | // Get number of nodes to schedule |
| 978 | unsigned N = Ordering.size(); |
| 979 | |
| 980 | // For each node being scheduled |
| 981 | for (unsigned i = N; 0 < i--;) { |
| 982 | NodeInfo *NI = Ordering[i]; |
| 983 | // Track insertion |
| 984 | unsigned Slot = NotFound; |
| 985 | |
| 986 | // Compare against those previously scheduled nodes |
| 987 | unsigned j = i + 1; |
| 988 | for (; j < N; j++) { |
| 989 | // Get following instruction |
| 990 | NodeInfo *Other = Ordering[j]; |
| 991 | |
| 992 | // Check dependency against previously inserted nodes |
| 993 | if (isStrongDependency(NI, Other)) { |
| 994 | Slot = Other->Slot + Other->Latency; |
| 995 | break; |
| 996 | } else if (isWeakDependency(NI, Other)) { |
| 997 | Slot = Other->Slot; |
| 998 | break; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | // If independent of others (or first entry) |
| 1003 | if (Slot == NotFound) Slot = 0; |
| 1004 | |
| 1005 | #if 0 // FIXME - measure later |
| 1006 | // Find a slot where the needed resources are available |
| 1007 | if (NI->StageBegin != NI->StageEnd) |
| 1008 | Slot = Tally.FindAndReserve(Slot, NI->StageBegin, NI->StageEnd); |
| 1009 | #endif |
| 1010 | |
| 1011 | // Set node slot |
| 1012 | NI->Slot = Slot; |
| 1013 | |
| 1014 | // Insert sort based on slot |
| 1015 | j = i + 1; |
| 1016 | for (; j < N; j++) { |
| 1017 | // Get following instruction |
| 1018 | NodeInfo *Other = Ordering[j]; |
| 1019 | // Should we look further (remember slots are in reverse time) |
| 1020 | if (Slot >= Other->Slot) break; |
| 1021 | // Shuffle other into ordering |
| 1022 | Ordering[j - 1] = Other; |
| 1023 | } |
| 1024 | // Insert node in proper slot |
| 1025 | if (j != i + 1) Ordering[j - 1] = NI; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /// ScheduleForward - Schedule instructions to maximize packing. |
| 1030 | /// |
| 1031 | void ScheduleDAGSimple::ScheduleForward() { |
| 1032 | // Size and clear the resource tally |
| 1033 | Tally.Initialize(NSlots); |
| 1034 | // Get number of nodes to schedule |
| 1035 | unsigned N = Ordering.size(); |
| 1036 | |
| 1037 | // For each node being scheduled |
| 1038 | for (unsigned i = 0; i < N; i++) { |
| 1039 | NodeInfo *NI = Ordering[i]; |
| 1040 | // Track insertion |
| 1041 | unsigned Slot = NotFound; |
| 1042 | |
| 1043 | // Compare against those previously scheduled nodes |
| 1044 | unsigned j = i; |
| 1045 | for (; 0 < j--;) { |
| 1046 | // Get following instruction |
| 1047 | NodeInfo *Other = Ordering[j]; |
| 1048 | |
| 1049 | // Check dependency against previously inserted nodes |
| 1050 | if (isStrongDependency(Other, NI)) { |
| 1051 | Slot = Other->Slot + Other->Latency; |
| 1052 | break; |
| 1053 | } else if (Other->IsCall || isWeakDependency(Other, NI)) { |
| 1054 | Slot = Other->Slot; |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | // If independent of others (or first entry) |
| 1060 | if (Slot == NotFound) Slot = 0; |
| 1061 | |
| 1062 | // Find a slot where the needed resources are available |
| 1063 | if (NI->StageBegin != NI->StageEnd) |
| 1064 | Slot = Tally.FindAndReserve(Slot, NI->StageBegin, NI->StageEnd); |
| 1065 | |
| 1066 | // Set node slot |
| 1067 | NI->Slot = Slot; |
| 1068 | |
| 1069 | // Insert sort based on slot |
| 1070 | j = i; |
| 1071 | for (; 0 < j--;) { |
| 1072 | // Get prior instruction |
| 1073 | NodeInfo *Other = Ordering[j]; |
| 1074 | // Should we look further |
| 1075 | if (Slot >= Other->Slot) break; |
| 1076 | // Shuffle other into ordering |
| 1077 | Ordering[j + 1] = Other; |
| 1078 | } |
| 1079 | // Insert node in proper slot |
| 1080 | if (j != i) Ordering[j + 1] = NI; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1084 | /// Schedule - Order nodes according to selected style. |
| 1085 | /// |
| 1086 | void ScheduleDAGSimple::Schedule() { |
Chris Lattner | 2f5806c | 2006-03-10 07:42:02 +0000 | [diff] [blame] | 1087 | // Number the nodes |
| 1088 | NodeCount = std::distance(DAG.allnodes_begin(), DAG.allnodes_end()); |
| 1089 | |
Chris Lattner | be24e59 | 2006-03-10 06:34:51 +0000 | [diff] [blame] | 1090 | // Set up minimum info for scheduling |
| 1091 | PrepareNodeInfo(); |
| 1092 | // Construct node groups for flagged nodes |
| 1093 | IdentifyGroups(); |
| 1094 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1095 | // Test to see if scheduling should occur |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 1096 | bool ShouldSchedule = NodeCount > 3 && !NoSched; |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1097 | // Don't waste time if is only entry and return |
| 1098 | if (ShouldSchedule) { |
| 1099 | // Get latency and resource requirements |
| 1100 | GatherSchedulingInfo(); |
| 1101 | } else if (HasGroups) { |
| 1102 | // Make sure all the groups have dominators |
| 1103 | FakeGroupDominators(); |
| 1104 | } |
| 1105 | |
| 1106 | // Breadth first walk of DAG |
| 1107 | VisitAll(); |
| 1108 | |
| 1109 | #ifndef NDEBUG |
| 1110 | static unsigned Count = 0; |
| 1111 | Count++; |
| 1112 | for (unsigned i = 0, N = Ordering.size(); i < N; i++) { |
| 1113 | NodeInfo *NI = Ordering[i]; |
| 1114 | NI->Preorder = i; |
| 1115 | } |
| 1116 | #endif |
| 1117 | |
| 1118 | // Don't waste time if is only entry and return |
| 1119 | if (ShouldSchedule) { |
| 1120 | // Push back long instructions and critical path |
| 1121 | ScheduleBackward(); |
| 1122 | |
| 1123 | // Pack instructions to maximize resource utilization |
| 1124 | ScheduleForward(); |
| 1125 | } |
| 1126 | |
| 1127 | DEBUG(printChanges(Count)); |
| 1128 | |
| 1129 | // Emit in scheduled order |
| 1130 | EmitAll(); |
| 1131 | } |
| 1132 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1133 | |
| 1134 | /// createSimpleDAGScheduler - This creates a simple two pass instruction |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1135 | /// scheduler using instruction itinerary. |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1136 | llvm::ScheduleDAG* llvm::createSimpleDAGScheduler(SelectionDAGISel *IS, |
| 1137 | SelectionDAG *DAG, |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1138 | MachineBasicBlock *BB) { |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1139 | return new ScheduleDAGSimple(false, false, *DAG, BB, DAG->getTarget()); |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1142 | /// createNoItinsDAGScheduler - This creates a simple two pass instruction |
| 1143 | /// scheduler without using instruction itinerary. |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1144 | llvm::ScheduleDAG* llvm::createNoItinsDAGScheduler(SelectionDAGISel *IS, |
| 1145 | SelectionDAG *DAG, |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1146 | MachineBasicBlock *BB) { |
| 1147 | return new ScheduleDAGSimple(false, true, *DAG, BB, DAG->getTarget()); |
| 1148 | } |
| 1149 | |
| 1150 | /// createBFS_DAGScheduler - This creates a simple breadth first instruction |
| 1151 | /// scheduler. |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1152 | llvm::ScheduleDAG* llvm::createBFS_DAGScheduler(SelectionDAGISel *IS, |
| 1153 | SelectionDAG *DAG, |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 1154 | MachineBasicBlock *BB) { |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1155 | return new ScheduleDAGSimple(true, false, *DAG, BB, DAG->getTarget()); |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1156 | } |