It seems the llvm::OStream class does not handle stream manipulators.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / ScheduleDAGSimple.cpp
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"
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>
28 using namespace llvm;
29
30 namespace {
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                      
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
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 ///
410 class VISIBILITY_HIDDEN ScheduleDAGSimple : public ScheduleDAG {
411 private:
412   bool NoSched;                         // Just do a BFS schedule, nothing fancy
413   bool NoItins;                         // Don't use itineraries?
414   ResourceTally<unsigned> Tally;        // Resource usage tally
415   unsigned NSlots;                      // Total latency
416   static const unsigned NotFound = ~0U; // Search marker
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
424   
425 public:
426
427   // Ctor.
428   ScheduleDAGSimple(bool noSched, bool noItins, SelectionDAG &dag,
429                     MachineBasicBlock *bb, const TargetMachine &tm)
430     : ScheduleDAG(dag, bb, tm), NoSched(noSched), NoItins(noItins), NSlots(0),
431     NodeCount(0), HasGroups(false), Info(NULL), HeadNG(NULL), TailNG(NULL) {
432     assert(&TII && "Target doesn't provide instr info?");
433     assert(&MRI && "Target doesn't provide register info?");
434   }
435
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   }
447
448   void Schedule();
449
450   /// getNI - Returns the node info for the specified node.
451   ///
452   NodeInfo *getNI(SDNode *Node) { return Map[Node]; }
453   
454 private:
455   static bool isDefiner(NodeInfo *A, NodeInfo *B);
456   void IncludeNode(NodeInfo *NI);
457   void VisitAll();
458   void GatherSchedulingInfo();
459   void FakeGroupDominators(); 
460   bool isStrongDependency(NodeInfo *A, NodeInfo *B);
461   bool isWeakDependency(NodeInfo *A, NodeInfo *B);
462   void ScheduleBackward();
463   void ScheduleForward();
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   ///
476   void print(OStream &O) const;
477   
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   ///
488   void printNI(OStream &O, NodeInfo *NI) const;
489   
490   /// printChanges - Hilight changes in order caused by scheduling.
491   ///
492   void printChanges(unsigned Index) const;
493 };
494
495 //===----------------------------------------------------------------------===//
496 /// Special case itineraries.
497 ///
498 enum {
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 };
506 static InstrStage LoadStage  = { 5, RSLoadStore };
507 static InstrStage StoreStage = { 2, RSLoadStore };
508 static InstrStage IntStage   = { 2, RSInteger };
509 static InstrStage FloatStage = { 3, RSFloat };
510 //===----------------------------------------------------------------------===//
511
512 } // namespace
513
514 //===----------------------------------------------------------------------===//
515
516 /// PrepareNodeInfo - Set up the basic minimum node info for scheduling.
517 /// 
518 void 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 ///
538 void 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 ///
559 static 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 ///
572 void 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 ///
639 void ScheduleDAGSimple::print(OStream &O) const {
640 #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
659 void ScheduleDAGSimple::dump(const char *tag) const {
660   cerr << tag; dump();
661 }
662
663 void ScheduleDAGSimple::dump() const {
664   print(cerr);
665 }
666
667
668 /// EmitAll - Emit all nodes in schedule sorted order.
669 ///
670 void ScheduleDAGSimple::EmitAll() {
671   // 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   
683   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.
699 static 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 ///
706 static 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 ///
713 void ScheduleDAGSimple::printNI(OStream &O, NodeInfo *NI) const {
714 #ifndef NDEBUG
715   SDNode *Node = NI->Node;
716   *(O.stream()) << " "
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 ///
730 void 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) {
742     cerr << Index << ". New Ordering\n";
743     
744     for (i = 0; i < N; i++) {
745       NodeInfo *NI = Ordering[i];
746       cerr << "  " << NI->Preorder << ". ";
747       printNI(cerr, NI);
748       cerr << "\n";
749       if (NI->isGroupDominator()) {
750         NodeGroup *Group = NI->Group;
751         for (NIIterator NII = Group->group_begin(), E = Group->group_end();
752              NII != E; NII++) {
753           cerr << "          ";
754           printNI(cerr, *NII);
755           cerr << "\n";
756         }
757       }
758     }
759   } else {
760     cerr << Index << ". No Changes\n";
761   }
762 #endif
763 }
764
765 //===----------------------------------------------------------------------===//
766 /// isDefiner - Return true if node A is a definer for B.
767 ///
768 bool 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
785 /// IncludeNode - Add node to NodeInfo vector.
786 ///
787 void 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
813 /// GatherSchedulingInfo - Get latency and resource information about each node.
814 ///
815 void ScheduleDAGSimple::GatherSchedulingInfo() {
816   // Get instruction itineraries for the target
817   const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
818   
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
826     if (InstrItins.isEmpty() || NoItins) {
827       // 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
897 /// VisitAll - Visit each node breadth-wise to produce an initial ordering.
898 /// Note that the ordering in the Nodes vector is reversed.
899 void 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
932 /// FakeGroupDominators - Set dominators for non-scheduling.
933 /// 
934 void 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
948 /// isStrongDependency - Return true if node A has results used by node B. 
949 /// I.E., B must wait for latency of A.
950 bool 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.
959 bool 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.
972 void 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 ///
1029 void 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
1082 /// Schedule - Order nodes according to selected style.
1083 ///
1084 void ScheduleDAGSimple::Schedule() {
1085   // Number the nodes
1086   NodeCount = std::distance(DAG.allnodes_begin(), DAG.allnodes_end());
1087
1088   // Set up minimum info for scheduling
1089   PrepareNodeInfo();
1090   // Construct node groups for flagged nodes
1091   IdentifyGroups();
1092   
1093   // Test to see if scheduling should occur
1094   bool ShouldSchedule = NodeCount > 3 && !NoSched;
1095   // 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
1131
1132 /// createSimpleDAGScheduler - This creates a simple two pass instruction
1133 /// scheduler using instruction itinerary.
1134 llvm::ScheduleDAG* llvm::createSimpleDAGScheduler(SelectionDAGISel *IS,
1135                                                   SelectionDAG *DAG,
1136                                                   MachineBasicBlock *BB) {
1137   return new ScheduleDAGSimple(false, false, *DAG, BB, DAG->getTarget());
1138 }
1139
1140 /// createNoItinsDAGScheduler - This creates a simple two pass instruction
1141 /// scheduler without using instruction itinerary.
1142 llvm::ScheduleDAG* llvm::createNoItinsDAGScheduler(SelectionDAGISel *IS,
1143                                                    SelectionDAG *DAG,
1144                                                    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.
1150 llvm::ScheduleDAG* llvm::createBFS_DAGScheduler(SelectionDAGISel *IS,
1151                                                 SelectionDAG *DAG,
1152                                                 MachineBasicBlock *BB) {
1153   return new ScheduleDAGSimple(true, false, *DAG, BB,  DAG->getTarget());
1154 }