blob: f64e65eafa18cd5f3e4ebd44d7db9a29f342dcb4 [file] [log] [blame]
Evan Chenga9c20912006-01-21 02:32:06 +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 "sched"
Jim Laskeyf4923912006-08-03 20:51:06 +000017#include "llvm/CodeGen/MachineFunction.h"
Evan Chenga9c20912006-01-21 02:32:06 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000019#include "llvm/CodeGen/SchedulerRegistry.h"
Evan Chenga9c20912006-01-21 02:32:06 +000020#include "llvm/CodeGen/SelectionDAG.h"
Jim Laskeyf4923912006-08-03 20:51:06 +000021#include "llvm/CodeGen/SSARegMap.h"
Owen Anderson07000c62006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Evan Chenga9c20912006-01-21 02:32:06 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
Evan Chenga9c20912006-01-21 02:32:06 +000025#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Jeff Cohen2aa750a2006-01-24 04:43:17 +000027#include <algorithm>
Evan Chenga9c20912006-01-21 02:32:06 +000028using namespace llvm;
29
30namespace {
Jim Laskey13ec7022006-08-01 14:21:23 +000031
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
Chris Lattnera34b6f82006-03-10 07:51:18 +000046class 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
167
168public:
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///
205class NodeGroupOpIterator {
206private:
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
229public:
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 Chenga9c20912006-01-21 02:32:06 +0000248//===----------------------------------------------------------------------===//
249///
250/// BitsIterator - Provides iteration through individual bits in a bit vector.
251///
252template<class T>
253class BitsIterator {
254private:
255 T Bits; // Bits left to iterate through
256
257public:
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///
283template<class T>
284class ResourceTally {
285private:
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
384public:
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 Lattnerf8c68f62006-06-28 22:17:39 +0000410class VISIBILITY_HIDDEN ScheduleDAGSimple : public ScheduleDAG {
Evan Chenga9c20912006-01-21 02:32:06 +0000411private:
Chris Lattner20a49212006-03-10 07:49:12 +0000412 bool NoSched; // Just do a BFS schedule, nothing fancy
413 bool NoItins; // Don't use itineraries?
Evan Chenga9c20912006-01-21 02:32:06 +0000414 ResourceTally<unsigned> Tally; // Resource usage tally
415 unsigned NSlots; // Total latency
416 static const unsigned NotFound = ~0U; // Search marker
Chris Lattner2f5806c2006-03-10 07:42:02 +0000417
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 Chenga9c20912006-01-21 02:32:06 +0000424
425public:
426
427 // Ctor.
Chris Lattner20a49212006-03-10 07:49:12 +0000428 ScheduleDAGSimple(bool noSched, bool noItins, SelectionDAG &dag,
Evan Cheng4ef10862006-01-23 07:01:07 +0000429 MachineBasicBlock *bb, const TargetMachine &tm)
Chris Lattner20a49212006-03-10 07:49:12 +0000430 : ScheduleDAG(dag, bb, tm), NoSched(noSched), NoItins(noItins), NSlots(0),
Chris Lattner2f5806c2006-03-10 07:42:02 +0000431 NodeCount(0), HasGroups(false), Info(NULL), HeadNG(NULL), TailNG(NULL) {
Evan Chenga9c20912006-01-21 02:32:06 +0000432 assert(&TII && "Target doesn't provide instr info?");
433 assert(&MRI && "Target doesn't provide register info?");
434 }
435
Chris Lattner2f5806c2006-03-10 07:42:02 +0000436 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 Chenga9c20912006-01-21 02:32:06 +0000447
Evan Cheng41484292006-01-23 08:25:34 +0000448 void Schedule();
449
Chris Lattner2f5806c2006-03-10 07:42:02 +0000450 /// getNI - Returns the node info for the specified node.
451 ///
452 NodeInfo *getNI(SDNode *Node) { return Map[Node]; }
453
Evan Chenga9c20912006-01-21 02:32:06 +0000454private:
Evan Chenga9c20912006-01-21 02:32:06 +0000455 static bool isDefiner(NodeInfo *A, NodeInfo *B);
Evan Chenga9c20912006-01-21 02:32:06 +0000456 void IncludeNode(NodeInfo *NI);
457 void VisitAll();
Evan Chenga9c20912006-01-21 02:32:06 +0000458 void GatherSchedulingInfo();
459 void FakeGroupDominators();
Evan Chenga9c20912006-01-21 02:32:06 +0000460 bool isStrongDependency(NodeInfo *A, NodeInfo *B);
461 bool isWeakDependency(NodeInfo *A, NodeInfo *B);
462 void ScheduleBackward();
463 void ScheduleForward();
Chris Lattnere76074a2006-03-10 07:35:21 +0000464
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 Wendling832171c2006-12-07 20:04:42 +0000476 void print(OStream &O) const;
Chris Lattnere76074a2006-03-10 07:35:21 +0000477
478 void dump(const char *tag) const;
479
480 virtual void dump() const;
481
482 /// EmitAll - Emit all nodes in schedule sorted order.
483 ///
484 void EmitAll();
485
486 /// printNI - Print node info.
487 ///
Bill Wendling832171c2006-12-07 20:04:42 +0000488 void printNI(OStream &O, NodeInfo *NI) const;
Chris Lattnere76074a2006-03-10 07:35:21 +0000489
490 /// printChanges - Hilight changes in order caused by scheduling.
491 ///
492 void printChanges(unsigned Index) const;
Evan Chenga9c20912006-01-21 02:32:06 +0000493};
494
Evan Chenga9c20912006-01-21 02:32:06 +0000495//===----------------------------------------------------------------------===//
496/// Special case itineraries.
497///
498enum {
499 CallLatency = 40, // To push calls back in time
500
501 RSInteger = 0xC0000000, // Two integer units
502 RSFloat = 0x30000000, // Two float units
503 RSLoadStore = 0x0C000000, // Two load store units
504 RSBranch = 0x02000000 // One branch unit
505};
Evan Chenga9c20912006-01-21 02:32:06 +0000506static InstrStage LoadStage = { 5, RSLoadStore };
507static InstrStage StoreStage = { 2, RSLoadStore };
508static InstrStage IntStage = { 2, RSInteger };
509static InstrStage FloatStage = { 3, RSFloat };
510//===----------------------------------------------------------------------===//
511
Evan Chenga9c20912006-01-21 02:32:06 +0000512} // namespace
513
514//===----------------------------------------------------------------------===//
515
Chris Lattnere76074a2006-03-10 07:35:21 +0000516/// PrepareNodeInfo - Set up the basic minimum node info for scheduling.
517///
518void ScheduleDAGSimple::PrepareNodeInfo() {
519 // Allocate node information
520 Info = new NodeInfo[NodeCount];
521
522 unsigned i = 0;
523 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
524 E = DAG.allnodes_end(); I != E; ++I, ++i) {
525 // Fast reference to node schedule info
526 NodeInfo* NI = &Info[i];
527 // Set up map
528 Map[I] = NI;
529 // Set node
530 NI->Node = I;
531 // Set pending visit count
532 NI->setPending(I->use_size());
533 }
534}
535
536/// IdentifyGroups - Put flagged nodes into groups.
537///
538void ScheduleDAGSimple::IdentifyGroups() {
539 for (unsigned i = 0, N = NodeCount; i < N; i++) {
540 NodeInfo* NI = &Info[i];
541 SDNode *Node = NI->Node;
542
543 // For each operand (in reverse to only look at flags)
544 for (unsigned N = Node->getNumOperands(); 0 < N--;) {
545 // Get operand
546 SDOperand Op = Node->getOperand(N);
547 // No more flags to walk
548 if (Op.getValueType() != MVT::Flag) break;
549 // Add to node group
550 AddToGroup(getNI(Op.Val), NI);
551 // Let everyone else know
552 HasGroups = true;
553 }
554 }
555}
556
557/// CountInternalUses - Returns the number of edges between the two nodes.
558///
559static unsigned CountInternalUses(NodeInfo *D, NodeInfo *U) {
560 unsigned N = 0;
561 for (unsigned M = U->Node->getNumOperands(); 0 < M--;) {
562 SDOperand Op = U->Node->getOperand(M);
563 if (Op.Val == D->Node) N++;
564 }
565
566 return N;
567}
568
569//===----------------------------------------------------------------------===//
570/// Add - Adds a definer and user pair to a node group.
571///
572void ScheduleDAGSimple::AddToGroup(NodeInfo *D, NodeInfo *U) {
573 // Get current groups
574 NodeGroup *DGroup = D->Group;
575 NodeGroup *UGroup = U->Group;
576 // If both are members of groups
577 if (DGroup && UGroup) {
578 // There may have been another edge connecting
579 if (DGroup == UGroup) return;
580 // Add the pending users count
581 DGroup->addPending(UGroup->getPending());
582 // For each member of the users group
583 NodeGroupIterator UNGI(U);
584 while (NodeInfo *UNI = UNGI.next() ) {
585 // Change the group
586 UNI->Group = DGroup;
587 // For each member of the definers group
588 NodeGroupIterator DNGI(D);
589 while (NodeInfo *DNI = DNGI.next() ) {
590 // Remove internal edges
591 DGroup->addPending(-CountInternalUses(DNI, UNI));
592 }
593 }
594 // Merge the two lists
595 DGroup->group_insert(DGroup->group_end(),
596 UGroup->group_begin(), UGroup->group_end());
597 } else if (DGroup) {
598 // Make user member of definers group
599 U->Group = DGroup;
600 // Add users uses to definers group pending
601 DGroup->addPending(U->Node->use_size());
602 // For each member of the definers group
603 NodeGroupIterator DNGI(D);
604 while (NodeInfo *DNI = DNGI.next() ) {
605 // Remove internal edges
606 DGroup->addPending(-CountInternalUses(DNI, U));
607 }
608 DGroup->group_push_back(U);
609 } else if (UGroup) {
610 // Make definer member of users group
611 D->Group = UGroup;
612 // Add definers uses to users group pending
613 UGroup->addPending(D->Node->use_size());
614 // For each member of the users group
615 NodeGroupIterator UNGI(U);
616 while (NodeInfo *UNI = UNGI.next() ) {
617 // Remove internal edges
618 UGroup->addPending(-CountInternalUses(D, UNI));
619 }
620 UGroup->group_insert(UGroup->group_begin(), D);
621 } else {
622 D->Group = U->Group = DGroup = new NodeGroup();
623 DGroup->addPending(D->Node->use_size() + U->Node->use_size() -
624 CountInternalUses(D, U));
625 DGroup->group_push_back(D);
626 DGroup->group_push_back(U);
627
628 if (HeadNG == NULL)
629 HeadNG = DGroup;
630 if (TailNG != NULL)
631 TailNG->Next = DGroup;
632 TailNG = DGroup;
633 }
634}
635
636
637/// print - Print ordering to specified output stream.
638///
Bill Wendling832171c2006-12-07 20:04:42 +0000639void ScheduleDAGSimple::print(OStream &O) const {
Chris Lattnere76074a2006-03-10 07:35:21 +0000640#ifndef NDEBUG
641 O << "Ordering\n";
642 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
643 NodeInfo *NI = Ordering[i];
644 printNI(O, NI);
645 O << "\n";
646 if (NI->isGroupDominator()) {
647 NodeGroup *Group = NI->Group;
648 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
649 NII != E; NII++) {
650 O << " ";
651 printNI(O, *NII);
652 O << "\n";
653 }
654 }
655 }
656#endif
657}
658
659void ScheduleDAGSimple::dump(const char *tag) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000660 cerr << tag; dump();
Chris Lattnere76074a2006-03-10 07:35:21 +0000661}
662
663void ScheduleDAGSimple::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +0000664 print(cerr);
Chris Lattnere76074a2006-03-10 07:35:21 +0000665}
666
667
668/// EmitAll - Emit all nodes in schedule sorted order.
669///
670void ScheduleDAGSimple::EmitAll() {
Jim Laskeyf4923912006-08-03 20:51:06 +0000671 // If this is the first basic block in the function, and if it has live ins
672 // that need to be copied into vregs, emit the copies into the top of the
673 // block before emitting the code for the block.
674 MachineFunction &MF = DAG.getMachineFunction();
675 if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
676 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
677 E = MF.livein_end(); LI != E; ++LI)
678 if (LI->second)
679 MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
680 LI->first, RegMap->getRegClass(LI->second));
681 }
682
Chris Lattnere76074a2006-03-10 07:35:21 +0000683 std::map<SDNode*, unsigned> VRBaseMap;
684
685 // For each node in the ordering
686 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
687 // Get the scheduling info
688 NodeInfo *NI = Ordering[i];
689 if (NI->isInGroup()) {
690 NodeGroupIterator NGI(Ordering[i]);
691 while (NodeInfo *NI = NGI.next()) EmitNode(NI->Node, VRBaseMap);
692 } else {
693 EmitNode(NI->Node, VRBaseMap);
694 }
695 }
696}
697
698/// isFlagDefiner - Returns true if the node defines a flag result.
699static bool isFlagDefiner(SDNode *A) {
700 unsigned N = A->getNumValues();
701 return N && A->getValueType(N - 1) == MVT::Flag;
702}
703
704/// isFlagUser - Returns true if the node uses a flag result.
705///
706static bool isFlagUser(SDNode *A) {
707 unsigned N = A->getNumOperands();
708 return N && A->getOperand(N - 1).getValueType() == MVT::Flag;
709}
710
711/// printNI - Print node info.
712///
Bill Wendling832171c2006-12-07 20:04:42 +0000713void ScheduleDAGSimple::printNI(OStream &O, NodeInfo *NI) const {
Chris Lattnere76074a2006-03-10 07:35:21 +0000714#ifndef NDEBUG
715 SDNode *Node = NI->Node;
716 O << " "
717 << std::hex << Node << std::dec
718 << ", Lat=" << NI->Latency
719 << ", Slot=" << NI->Slot
720 << ", ARITY=(" << Node->getNumOperands() << ","
721 << Node->getNumValues() << ")"
722 << " " << Node->getOperationName(&DAG);
723 if (isFlagDefiner(Node)) O << "<#";
724 if (isFlagUser(Node)) O << ">#";
725#endif
726}
727
728/// printChanges - Hilight changes in order caused by scheduling.
729///
730void ScheduleDAGSimple::printChanges(unsigned Index) const {
731#ifndef NDEBUG
732 // Get the ordered node count
733 unsigned N = Ordering.size();
734 // Determine if any changes
735 unsigned i = 0;
736 for (; i < N; i++) {
737 NodeInfo *NI = Ordering[i];
738 if (NI->Preorder != i) break;
739 }
740
741 if (i < N) {
Bill Wendling832171c2006-12-07 20:04:42 +0000742 cerr << Index << ". New Ordering\n";
Chris Lattnere76074a2006-03-10 07:35:21 +0000743
744 for (i = 0; i < N; i++) {
745 NodeInfo *NI = Ordering[i];
Bill Wendling832171c2006-12-07 20:04:42 +0000746 cerr << " " << NI->Preorder << ". ";
747 printNI(cerr, NI);
748 cerr << "\n";
Chris Lattnere76074a2006-03-10 07:35:21 +0000749 if (NI->isGroupDominator()) {
750 NodeGroup *Group = NI->Group;
751 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
752 NII != E; NII++) {
Bill Wendling832171c2006-12-07 20:04:42 +0000753 cerr << " ";
754 printNI(cerr, *NII);
755 cerr << "\n";
Chris Lattnere76074a2006-03-10 07:35:21 +0000756 }
757 }
758 }
759 } else {
Bill Wendling832171c2006-12-07 20:04:42 +0000760 cerr << Index << ". No Changes\n";
Chris Lattnere76074a2006-03-10 07:35:21 +0000761 }
762#endif
763}
Evan Chenga9c20912006-01-21 02:32:06 +0000764
765//===----------------------------------------------------------------------===//
Evan Chenga9c20912006-01-21 02:32:06 +0000766/// isDefiner - Return true if node A is a definer for B.
767///
768bool ScheduleDAGSimple::isDefiner(NodeInfo *A, NodeInfo *B) {
769 // While there are A nodes
770 NodeGroupIterator NII(A);
771 while (NodeInfo *NI = NII.next()) {
772 // Extract node
773 SDNode *Node = NI->Node;
774 // While there operands in nodes of B
775 NodeGroupOpIterator NGOI(B);
776 while (!NGOI.isEnd()) {
777 SDOperand Op = NGOI.next();
778 // If node from A defines a node in B
779 if (Node == Op.Val) return true;
780 }
781 }
782 return false;
783}
784
Evan Chenga9c20912006-01-21 02:32:06 +0000785/// IncludeNode - Add node to NodeInfo vector.
786///
787void ScheduleDAGSimple::IncludeNode(NodeInfo *NI) {
788 // Get node
789 SDNode *Node = NI->Node;
790 // Ignore entry node
791 if (Node->getOpcode() == ISD::EntryToken) return;
792 // Check current count for node
793 int Count = NI->getPending();
794 // If the node is already in list
795 if (Count < 0) return;
796 // Decrement count to indicate a visit
797 Count--;
798 // If count has gone to zero then add node to list
799 if (!Count) {
800 // Add node
801 if (NI->isInGroup()) {
802 Ordering.push_back(NI->Group->getDominator());
803 } else {
804 Ordering.push_back(NI);
805 }
806 // indicate node has been added
807 Count--;
808 }
809 // Mark as visited with new count
810 NI->setPending(Count);
811}
812
Evan Chenga9c20912006-01-21 02:32:06 +0000813/// GatherSchedulingInfo - Get latency and resource information about each node.
814///
815void ScheduleDAGSimple::GatherSchedulingInfo() {
816 // Get instruction itineraries for the target
Chris Lattnere70f6712006-03-09 07:13:00 +0000817 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
Evan Chenga9c20912006-01-21 02:32:06 +0000818
819 // For each node
820 for (unsigned i = 0, N = NodeCount; i < N; i++) {
821 // Get node info
822 NodeInfo* NI = &Info[i];
823 SDNode *Node = NI->Node;
824
825 // If there are itineraries and it is a machine instruction
Chris Lattner20a49212006-03-10 07:49:12 +0000826 if (InstrItins.isEmpty() || NoItins) {
Evan Chenga9c20912006-01-21 02:32:06 +0000827 // If machine opcode
828 if (Node->isTargetOpcode()) {
829 // Get return type to guess which processing unit
830 MVT::ValueType VT = Node->getValueType(0);
831 // Get machine opcode
832 MachineOpCode TOpc = Node->getTargetOpcode();
833 NI->IsCall = TII->isCall(TOpc);
834 NI->IsLoad = TII->isLoad(TOpc);
835 NI->IsStore = TII->isStore(TOpc);
836
837 if (TII->isLoad(TOpc)) NI->StageBegin = &LoadStage;
838 else if (TII->isStore(TOpc)) NI->StageBegin = &StoreStage;
839 else if (MVT::isInteger(VT)) NI->StageBegin = &IntStage;
840 else if (MVT::isFloatingPoint(VT)) NI->StageBegin = &FloatStage;
841 if (NI->StageBegin) NI->StageEnd = NI->StageBegin + 1;
842 }
843 } else if (Node->isTargetOpcode()) {
844 // get machine opcode
845 MachineOpCode TOpc = Node->getTargetOpcode();
846 // Check to see if it is a call
847 NI->IsCall = TII->isCall(TOpc);
848 // Get itinerary stages for instruction
849 unsigned II = TII->getSchedClass(TOpc);
850 NI->StageBegin = InstrItins.begin(II);
851 NI->StageEnd = InstrItins.end(II);
852 }
853
854 // One slot for the instruction itself
855 NI->Latency = 1;
856
857 // Add long latency for a call to push it back in time
858 if (NI->IsCall) NI->Latency += CallLatency;
859
860 // Sum up all the latencies
861 for (InstrStage *Stage = NI->StageBegin, *E = NI->StageEnd;
862 Stage != E; Stage++) {
863 NI->Latency += Stage->Cycles;
864 }
865
866 // Sum up all the latencies for max tally size
867 NSlots += NI->Latency;
868 }
869
870 // Unify metrics if in a group
871 if (HasGroups) {
872 for (unsigned i = 0, N = NodeCount; i < N; i++) {
873 NodeInfo* NI = &Info[i];
874
875 if (NI->isInGroup()) {
876 NodeGroup *Group = NI->Group;
877
878 if (!Group->getDominator()) {
879 NIIterator NGI = Group->group_begin(), NGE = Group->group_end();
880 NodeInfo *Dominator = *NGI;
881 unsigned Latency = 0;
882
883 for (NGI++; NGI != NGE; NGI++) {
884 NodeInfo* NGNI = *NGI;
885 Latency += NGNI->Latency;
886 if (Dominator->Latency < NGNI->Latency) Dominator = NGNI;
887 }
888
889 Dominator->Latency = Latency;
890 Group->setDominator(Dominator);
891 }
892 }
893 }
894 }
895}
896
Evan Cheng4ef10862006-01-23 07:01:07 +0000897/// VisitAll - Visit each node breadth-wise to produce an initial ordering.
898/// Note that the ordering in the Nodes vector is reversed.
899void ScheduleDAGSimple::VisitAll() {
900 // Add first element to list
901 NodeInfo *NI = getNI(DAG.getRoot().Val);
902 if (NI->isInGroup()) {
903 Ordering.push_back(NI->Group->getDominator());
904 } else {
905 Ordering.push_back(NI);
906 }
907
908 // Iterate through all nodes that have been added
909 for (unsigned i = 0; i < Ordering.size(); i++) { // note: size() varies
910 // Visit all operands
911 NodeGroupOpIterator NGI(Ordering[i]);
912 while (!NGI.isEnd()) {
913 // Get next operand
914 SDOperand Op = NGI.next();
915 // Get node
916 SDNode *Node = Op.Val;
917 // Ignore passive nodes
918 if (isPassiveNode(Node)) continue;
919 // Check out node
920 IncludeNode(getNI(Node));
921 }
922 }
923
924 // Add entry node last (IncludeNode filters entry nodes)
925 if (DAG.getEntryNode().Val != DAG.getRoot().Val)
926 Ordering.push_back(getNI(DAG.getEntryNode().Val));
927
928 // Reverse the order
929 std::reverse(Ordering.begin(), Ordering.end());
930}
931
Evan Chenga9c20912006-01-21 02:32:06 +0000932/// FakeGroupDominators - Set dominators for non-scheduling.
933///
934void ScheduleDAGSimple::FakeGroupDominators() {
935 for (unsigned i = 0, N = NodeCount; i < N; i++) {
936 NodeInfo* NI = &Info[i];
937
938 if (NI->isInGroup()) {
939 NodeGroup *Group = NI->Group;
940
941 if (!Group->getDominator()) {
942 Group->setDominator(NI);
943 }
944 }
945 }
946}
947
Evan Chenga9c20912006-01-21 02:32:06 +0000948/// isStrongDependency - Return true if node A has results used by node B.
949/// I.E., B must wait for latency of A.
950bool ScheduleDAGSimple::isStrongDependency(NodeInfo *A, NodeInfo *B) {
951 // If A defines for B then it's a strong dependency or
952 // if a load follows a store (may be dependent but why take a chance.)
953 return isDefiner(A, B) || (A->IsStore && B->IsLoad);
954}
955
956/// isWeakDependency Return true if node A produces a result that will
957/// conflict with operands of B. It is assumed that we have called
958/// isStrongDependency prior.
959bool ScheduleDAGSimple::isWeakDependency(NodeInfo *A, NodeInfo *B) {
960 // TODO check for conflicting real registers and aliases
961#if 0 // FIXME - Since we are in SSA form and not checking register aliasing
962 return A->Node->getOpcode() == ISD::EntryToken || isStrongDependency(B, A);
963#else
964 return A->Node->getOpcode() == ISD::EntryToken;
965#endif
966}
967
968/// ScheduleBackward - Schedule instructions so that any long latency
969/// instructions and the critical path get pushed back in time. Time is run in
970/// reverse to allow code reuse of the Tally and eliminate the overhead of
971/// biasing every slot indices against NSlots.
972void ScheduleDAGSimple::ScheduleBackward() {
973 // Size and clear the resource tally
974 Tally.Initialize(NSlots);
975 // Get number of nodes to schedule
976 unsigned N = Ordering.size();
977
978 // For each node being scheduled
979 for (unsigned i = N; 0 < i--;) {
980 NodeInfo *NI = Ordering[i];
981 // Track insertion
982 unsigned Slot = NotFound;
983
984 // Compare against those previously scheduled nodes
985 unsigned j = i + 1;
986 for (; j < N; j++) {
987 // Get following instruction
988 NodeInfo *Other = Ordering[j];
989
990 // Check dependency against previously inserted nodes
991 if (isStrongDependency(NI, Other)) {
992 Slot = Other->Slot + Other->Latency;
993 break;
994 } else if (isWeakDependency(NI, Other)) {
995 Slot = Other->Slot;
996 break;
997 }
998 }
999
1000 // If independent of others (or first entry)
1001 if (Slot == NotFound) Slot = 0;
1002
1003#if 0 // FIXME - measure later
1004 // Find a slot where the needed resources are available
1005 if (NI->StageBegin != NI->StageEnd)
1006 Slot = Tally.FindAndReserve(Slot, NI->StageBegin, NI->StageEnd);
1007#endif
1008
1009 // Set node slot
1010 NI->Slot = Slot;
1011
1012 // Insert sort based on slot
1013 j = i + 1;
1014 for (; j < N; j++) {
1015 // Get following instruction
1016 NodeInfo *Other = Ordering[j];
1017 // Should we look further (remember slots are in reverse time)
1018 if (Slot >= Other->Slot) break;
1019 // Shuffle other into ordering
1020 Ordering[j - 1] = Other;
1021 }
1022 // Insert node in proper slot
1023 if (j != i + 1) Ordering[j - 1] = NI;
1024 }
1025}
1026
1027/// ScheduleForward - Schedule instructions to maximize packing.
1028///
1029void ScheduleDAGSimple::ScheduleForward() {
1030 // Size and clear the resource tally
1031 Tally.Initialize(NSlots);
1032 // Get number of nodes to schedule
1033 unsigned N = Ordering.size();
1034
1035 // For each node being scheduled
1036 for (unsigned i = 0; i < N; i++) {
1037 NodeInfo *NI = Ordering[i];
1038 // Track insertion
1039 unsigned Slot = NotFound;
1040
1041 // Compare against those previously scheduled nodes
1042 unsigned j = i;
1043 for (; 0 < j--;) {
1044 // Get following instruction
1045 NodeInfo *Other = Ordering[j];
1046
1047 // Check dependency against previously inserted nodes
1048 if (isStrongDependency(Other, NI)) {
1049 Slot = Other->Slot + Other->Latency;
1050 break;
1051 } else if (Other->IsCall || isWeakDependency(Other, NI)) {
1052 Slot = Other->Slot;
1053 break;
1054 }
1055 }
1056
1057 // If independent of others (or first entry)
1058 if (Slot == NotFound) Slot = 0;
1059
1060 // Find a slot where the needed resources are available
1061 if (NI->StageBegin != NI->StageEnd)
1062 Slot = Tally.FindAndReserve(Slot, NI->StageBegin, NI->StageEnd);
1063
1064 // Set node slot
1065 NI->Slot = Slot;
1066
1067 // Insert sort based on slot
1068 j = i;
1069 for (; 0 < j--;) {
1070 // Get prior instruction
1071 NodeInfo *Other = Ordering[j];
1072 // Should we look further
1073 if (Slot >= Other->Slot) break;
1074 // Shuffle other into ordering
1075 Ordering[j + 1] = Other;
1076 }
1077 // Insert node in proper slot
1078 if (j != i) Ordering[j + 1] = NI;
1079 }
1080}
1081
Evan Chenga9c20912006-01-21 02:32:06 +00001082/// Schedule - Order nodes according to selected style.
1083///
1084void ScheduleDAGSimple::Schedule() {
Chris Lattner2f5806c2006-03-10 07:42:02 +00001085 // Number the nodes
1086 NodeCount = std::distance(DAG.allnodes_begin(), DAG.allnodes_end());
1087
Chris Lattnerbe24e592006-03-10 06:34:51 +00001088 // Set up minimum info for scheduling
1089 PrepareNodeInfo();
1090 // Construct node groups for flagged nodes
1091 IdentifyGroups();
1092
Evan Chenga9c20912006-01-21 02:32:06 +00001093 // Test to see if scheduling should occur
Chris Lattner20a49212006-03-10 07:49:12 +00001094 bool ShouldSchedule = NodeCount > 3 && !NoSched;
Evan Chenga9c20912006-01-21 02:32:06 +00001095 // Don't waste time if is only entry and return
1096 if (ShouldSchedule) {
1097 // Get latency and resource requirements
1098 GatherSchedulingInfo();
1099 } else if (HasGroups) {
1100 // Make sure all the groups have dominators
1101 FakeGroupDominators();
1102 }
1103
1104 // Breadth first walk of DAG
1105 VisitAll();
1106
1107#ifndef NDEBUG
1108 static unsigned Count = 0;
1109 Count++;
1110 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
1111 NodeInfo *NI = Ordering[i];
1112 NI->Preorder = i;
1113 }
1114#endif
1115
1116 // Don't waste time if is only entry and return
1117 if (ShouldSchedule) {
1118 // Push back long instructions and critical path
1119 ScheduleBackward();
1120
1121 // Pack instructions to maximize resource utilization
1122 ScheduleForward();
1123 }
1124
1125 DEBUG(printChanges(Count));
1126
1127 // Emit in scheduled order
1128 EmitAll();
1129}
1130
Evan Chenga9c20912006-01-21 02:32:06 +00001131
1132/// createSimpleDAGScheduler - This creates a simple two pass instruction
Jim Laskey13ec7022006-08-01 14:21:23 +00001133/// scheduler using instruction itinerary.
Jim Laskey9ff542f2006-08-01 18:29:48 +00001134llvm::ScheduleDAG* llvm::createSimpleDAGScheduler(SelectionDAGISel *IS,
1135 SelectionDAG *DAG,
Evan Chenga9c20912006-01-21 02:32:06 +00001136 MachineBasicBlock *BB) {
Jim Laskey13ec7022006-08-01 14:21:23 +00001137 return new ScheduleDAGSimple(false, false, *DAG, BB, DAG->getTarget());
Chris Lattner20a49212006-03-10 07:49:12 +00001138}
1139
Jim Laskey13ec7022006-08-01 14:21:23 +00001140/// createNoItinsDAGScheduler - This creates a simple two pass instruction
1141/// scheduler without using instruction itinerary.
Jim Laskey9ff542f2006-08-01 18:29:48 +00001142llvm::ScheduleDAG* llvm::createNoItinsDAGScheduler(SelectionDAGISel *IS,
1143 SelectionDAG *DAG,
Jim Laskey13ec7022006-08-01 14:21:23 +00001144 MachineBasicBlock *BB) {
1145 return new ScheduleDAGSimple(false, true, *DAG, BB, DAG->getTarget());
1146}
1147
1148/// createBFS_DAGScheduler - This creates a simple breadth first instruction
1149/// scheduler.
Jim Laskey9ff542f2006-08-01 18:29:48 +00001150llvm::ScheduleDAG* llvm::createBFS_DAGScheduler(SelectionDAGISel *IS,
1151 SelectionDAG *DAG,
Chris Lattner20a49212006-03-10 07:49:12 +00001152 MachineBasicBlock *BB) {
Jim Laskey13ec7022006-08-01 14:21:23 +00001153 return new ScheduleDAGSimple(true, false, *DAG, BB, DAG->getTarget());
Evan Chenga9c20912006-01-21 02:32:06 +00001154}