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