Turn on -sched-commute-nodes by default.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / ScheduleDAGRRList.cpp
1 //===----- ScheduleDAGList.cpp - Reg pressure reduction list scheduler ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Evan Cheng and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements bottom-up and top-down register pressure reduction list
11 // schedulers, using standard algorithms.  The basic approach uses a priority
12 // queue of available nodes to schedule.  One at a time, nodes are taken from
13 // the priority queue (thus in priority order), checked for legality to
14 // schedule, and emitted if legal.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #define DEBUG_TYPE "sched"
19 #include "llvm/CodeGen/ScheduleDAG.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Target/MRegisterInfo.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/ADT/Statistic.h"
27 #include <climits>
28 #include <iostream>
29 #include <queue>
30 #include "llvm/Support/CommandLine.h"
31 using namespace llvm;
32
33 namespace {
34 //===----------------------------------------------------------------------===//
35 /// ScheduleDAGRRList - The actual register reduction list scheduler
36 /// implementation.  This supports both top-down and bottom-up scheduling.
37 ///
38
39 class ScheduleDAGRRList : public ScheduleDAG {
40 private:
41   /// isBottomUp - This is true if the scheduling problem is bottom-up, false if
42   /// it is top-down.
43   bool isBottomUp;
44   
45   /// AvailableQueue - The priority queue to use for the available SUnits.
46   ///
47   SchedulingPriorityQueue *AvailableQueue;
48
49 public:
50   ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb,
51                   const TargetMachine &tm, bool isbottomup,
52                   SchedulingPriorityQueue *availqueue)
53     : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup),
54       AvailableQueue(availqueue) {
55     }
56
57   ~ScheduleDAGRRList() {
58     delete AvailableQueue;
59   }
60
61   void Schedule();
62
63 private:
64   void ReleasePred(SUnit *PredSU, bool isChain, unsigned CurCycle);
65   void ReleaseSucc(SUnit *SuccSU, bool isChain, unsigned CurCycle);
66   void ScheduleNodeBottomUp(SUnit *SU, unsigned& CurCycle);
67   void ScheduleNodeTopDown(SUnit *SU, unsigned& CurCycle);
68   void ListScheduleTopDown();
69   void ListScheduleBottomUp();
70   void CommuteNodesToReducePressure();
71 };
72 }  // end anonymous namespace
73
74
75 /// Schedule - Schedule the DAG using list scheduling.
76 void ScheduleDAGRRList::Schedule() {
77   DEBUG(std::cerr << "********** List Scheduling **********\n");
78   
79   // Build scheduling units.
80   BuildSchedUnits();
81   DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
82         SUnits[su].dumpAll(&DAG));
83
84   CalculateDepths();
85   CalculateHeights();
86   DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
87         SUnits[su].dumpAll(&DAG));
88
89   AvailableQueue->initNodes(SUnits);
90
91   // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate.
92   if (isBottomUp)
93     ListScheduleBottomUp();
94   else
95     ListScheduleTopDown();
96   
97   AvailableQueue->releaseState();
98
99   CommuteNodesToReducePressure();
100   
101   DEBUG(std::cerr << "*** Final schedule ***\n");
102   DEBUG(dumpSchedule());
103   DEBUG(std::cerr << "\n");
104   
105   // Emit in scheduled order
106   EmitSchedule();
107 }
108
109 /// CommuteNodesToReducePressure - Is a node is two-address and commutable, and
110 /// it is not the last use of its first operand, add it to the CommuteSet if
111 /// possible. It will be commuted when it is translated to a MI.
112 void ScheduleDAGRRList::CommuteNodesToReducePressure() {
113   std::set<SUnit *> OperandSeen;
114   for (unsigned i = Sequence.size()-1; i != 0; --i) {  // Ignore first node.
115     SUnit *SU = Sequence[i];
116     if (!SU) continue;
117     if (SU->isTwoAddress && SU->isCommutable) {
118       SDNode *OpN = SU->Node->getOperand(0).Val;
119       SUnit *OpSU = SUnitMap[OpN];
120       if (OpSU && OperandSeen.count(OpSU) == 1) {
121         // Ok, so SU is not the last use of OpSU, but SU is two-address so
122         // it will clobber OpSU. Try to commute it if possible.
123         bool DoCommute = true;
124         for (unsigned j = 1, e = SU->Node->getNumOperands(); j != e; ++j) {
125           OpN = SU->Node->getOperand(j).Val;
126           OpSU = SUnitMap[OpN];
127           if (OpSU && OperandSeen.count(OpSU) == 1) {
128             DoCommute = false;
129             break;
130           }
131         }
132         if (DoCommute)
133           CommuteSet.insert(SU->Node);
134       }
135     }
136
137     for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
138            E = SU->Preds.end(); I != E; ++I) {
139       if (!I->second)
140         OperandSeen.insert(I->first);
141     }
142   }
143 }
144
145 //===----------------------------------------------------------------------===//
146 //  Bottom-Up Scheduling
147 //===----------------------------------------------------------------------===//
148
149 static const TargetRegisterClass *getRegClass(SUnit *SU,
150                                               const TargetInstrInfo *TII,
151                                               const MRegisterInfo *MRI,
152                                               SSARegMap *RegMap) {
153   if (SU->Node->isTargetOpcode()) {
154     unsigned Opc = SU->Node->getTargetOpcode();
155     const TargetInstrDescriptor &II = TII->get(Opc);
156     return II.OpInfo->RegClass;
157   } else {
158     assert(SU->Node->getOpcode() == ISD::CopyFromReg);
159     unsigned SrcReg = cast<RegisterSDNode>(SU->Node->getOperand(1))->getReg();
160     if (MRegisterInfo::isVirtualRegister(SrcReg))
161       return RegMap->getRegClass(SrcReg);
162     else {
163       for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
164              E = MRI->regclass_end(); I != E; ++I)
165         if ((*I)->hasType(SU->Node->getValueType(0)) &&
166             (*I)->contains(SrcReg))
167           return *I;
168       assert(false && "Couldn't find register class for reg copy!");
169     }
170     return NULL;
171   }
172 }
173
174 static unsigned getNumResults(SUnit *SU) {
175   unsigned NumResults = 0;
176   for (unsigned i = 0, e = SU->Node->getNumValues(); i != e; ++i) {
177     MVT::ValueType VT = SU->Node->getValueType(i);
178     if (VT != MVT::Other && VT != MVT::Flag)
179       NumResults++;
180   }
181   return NumResults;
182 }
183
184 /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
185 /// the Available queue is the count reaches zero. Also update its cycle bound.
186 void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain, 
187                                     unsigned CurCycle) {
188   // FIXME: the distance between two nodes is not always == the predecessor's
189   // latency. For example, the reader can very well read the register written
190   // by the predecessor later than the issue cycle. It also depends on the
191   // interrupt model (drain vs. freeze).
192   PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency);
193
194   if (!isChain)
195     PredSU->NumSuccsLeft--;
196   else
197     PredSU->NumChainSuccsLeft--;
198   
199 #ifndef NDEBUG
200   if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) {
201     std::cerr << "*** List scheduling failed! ***\n";
202     PredSU->dump(&DAG);
203     std::cerr << " has been released too many times!\n";
204     assert(0);
205   }
206 #endif
207   
208   if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) {
209     // EntryToken has to go last!  Special case it here.
210     if (PredSU->Node->getOpcode() != ISD::EntryToken) {
211       PredSU->isAvailable = true;
212       AvailableQueue->push(PredSU);
213     }
214   }
215 }
216
217 /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
218 /// count of its predecessors. If a predecessor pending count is zero, add it to
219 /// the Available queue.
220 void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned& CurCycle) {
221   DEBUG(std::cerr << "*** Scheduling [" << CurCycle << "]: ");
222   DEBUG(SU->dump(&DAG));
223   SU->Cycle = CurCycle;
224
225   AvailableQueue->ScheduledNode(SU);
226   Sequence.push_back(SU);
227
228   // Bottom up: release predecessors
229   for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
230          E = SU->Preds.end(); I != E; ++I)
231     ReleasePred(I->first, I->second, CurCycle);
232   SU->isScheduled = true;
233   CurCycle++;
234 }
235
236 /// isReady - True if node's lower cycle bound is less or equal to the current
237 /// scheduling cycle. Always true if all nodes have uniform latency 1.
238 static inline bool isReady(SUnit *SU, unsigned CurCycle) {
239   return SU->CycleBound <= CurCycle;
240 }
241
242 /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
243 /// schedulers.
244 void ScheduleDAGRRList::ListScheduleBottomUp() {
245   unsigned CurCycle = 0;
246   // Add root to Available queue.
247   AvailableQueue->push(SUnitMap[DAG.getRoot().Val]);
248
249   // While Available queue is not empty, grab the node with the highest
250   // priority. If it is not ready put it back. Schedule the node.
251   std::vector<SUnit*> NotReady;
252   SUnit *CurNode = NULL;
253   while (!AvailableQueue->empty()) {
254     SUnit *CurNode = AvailableQueue->pop();
255     while (!isReady(CurNode, CurCycle)) {
256       NotReady.push_back(CurNode);
257       CurNode = AvailableQueue->pop();
258     }
259     
260     // Add the nodes that aren't ready back onto the available list.
261     AvailableQueue->push_all(NotReady);
262     NotReady.clear();
263
264     ScheduleNodeBottomUp(CurNode, CurCycle);
265   }
266
267   // Add entry node last
268   if (DAG.getEntryNode().Val != DAG.getRoot().Val) {
269     SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
270     Sequence.push_back(Entry);
271   }
272
273   // Reverse the order if it is bottom up.
274   std::reverse(Sequence.begin(), Sequence.end());
275   
276   
277 #ifndef NDEBUG
278   // Verify that all SUnits were scheduled.
279   bool AnyNotSched = false;
280   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
281     if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) {
282       if (!AnyNotSched)
283         std::cerr << "*** List scheduling failed! ***\n";
284       SUnits[i].dump(&DAG);
285       std::cerr << "has not been scheduled!\n";
286       AnyNotSched = true;
287     }
288   }
289   assert(!AnyNotSched);
290 #endif
291 }
292
293 //===----------------------------------------------------------------------===//
294 //  Top-Down Scheduling
295 //===----------------------------------------------------------------------===//
296
297 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
298 /// the PendingQueue if the count reaches zero.
299 void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain, 
300                                     unsigned CurCycle) {
301   // FIXME: the distance between two nodes is not always == the predecessor's
302   // latency. For example, the reader can very well read the register written
303   // by the predecessor later than the issue cycle. It also depends on the
304   // interrupt model (drain vs. freeze).
305   SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency);
306
307   if (!isChain)
308     SuccSU->NumPredsLeft--;
309   else
310     SuccSU->NumChainPredsLeft--;
311   
312 #ifndef NDEBUG
313   if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) {
314     std::cerr << "*** List scheduling failed! ***\n";
315     SuccSU->dump(&DAG);
316     std::cerr << " has been released too many times!\n";
317     assert(0);
318   }
319 #endif
320   
321   if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) {
322     SuccSU->isAvailable = true;
323     AvailableQueue->push(SuccSU);
324   }
325 }
326
327
328 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
329 /// count of its successors. If a successor pending count is zero, add it to
330 /// the Available queue.
331 void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned& CurCycle) {
332   DEBUG(std::cerr << "*** Scheduling [" << CurCycle << "]: ");
333   DEBUG(SU->dump(&DAG));
334   SU->Cycle = CurCycle;
335
336   AvailableQueue->ScheduledNode(SU);
337   Sequence.push_back(SU);
338
339   // Top down: release successors
340   for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(),
341          E = SU->Succs.end(); I != E; ++I)
342     ReleaseSucc(I->first, I->second, CurCycle);
343   SU->isScheduled = true;
344   CurCycle++;
345 }
346
347 void ScheduleDAGRRList::ListScheduleTopDown() {
348   unsigned CurCycle = 0;
349   SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
350
351   // All leaves to Available queue.
352   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
353     // It is available if it has no predecessors.
354     if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
355       AvailableQueue->push(&SUnits[i]);
356       SUnits[i].isAvailable = true;
357     }
358   }
359   
360   // Emit the entry node first.
361   ScheduleNodeTopDown(Entry, CurCycle);
362
363   // While Available queue is not empty, grab the node with the highest
364   // priority. If it is not ready put it back. Schedule the node.
365   std::vector<SUnit*> NotReady;
366   SUnit *CurNode = NULL;
367   while (!AvailableQueue->empty()) {
368     SUnit *CurNode = AvailableQueue->pop();
369     while (!isReady(CurNode, CurCycle)) {
370       NotReady.push_back(CurNode);
371       CurNode = AvailableQueue->pop();
372     }
373     
374     // Add the nodes that aren't ready back onto the available list.
375     AvailableQueue->push_all(NotReady);
376     NotReady.clear();
377
378     ScheduleNodeTopDown(CurNode, CurCycle);
379   }
380   
381   
382 #ifndef NDEBUG
383   // Verify that all SUnits were scheduled.
384   bool AnyNotSched = false;
385   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
386     if (!SUnits[i].isScheduled) {
387       if (!AnyNotSched)
388         std::cerr << "*** List scheduling failed! ***\n";
389       SUnits[i].dump(&DAG);
390       std::cerr << "has not been scheduled!\n";
391       AnyNotSched = true;
392     }
393   }
394   assert(!AnyNotSched);
395 #endif
396 }
397
398
399
400 //===----------------------------------------------------------------------===//
401 //                RegReductionPriorityQueue Implementation
402 //===----------------------------------------------------------------------===//
403 //
404 // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers
405 // to reduce register pressure.
406 // 
407 namespace {
408   template<class SF>
409   class RegReductionPriorityQueue;
410   
411   /// Sorting functions for the Available queue.
412   struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
413     RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ;
414     bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {}
415     bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
416     
417     bool operator()(const SUnit* left, const SUnit* right) const;
418   };
419
420   struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> {
421     RegReductionPriorityQueue<td_ls_rr_sort> *SPQ;
422     td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {}
423     td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {}
424     
425     bool operator()(const SUnit* left, const SUnit* right) const;
426   };
427 }  // end anonymous namespace
428
429 namespace {
430   template<class SF>
431   class RegReductionPriorityQueue : public SchedulingPriorityQueue {
432     std::priority_queue<SUnit*, std::vector<SUnit*>, SF> Queue;
433
434   public:
435     RegReductionPriorityQueue() :
436     Queue(SF(this)) {}
437     
438     virtual void initNodes(const std::vector<SUnit> &sunits) {}
439     virtual void releaseState() {}
440     
441     virtual int getSethiUllmanNumber(unsigned NodeNum) const {
442       return 0;
443     }
444     
445     bool empty() const { return Queue.empty(); }
446     
447     void push(SUnit *U) {
448       Queue.push(U);
449     }
450     void push_all(const std::vector<SUnit *> &Nodes) {
451       for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
452         Queue.push(Nodes[i]);
453     }
454     
455     SUnit *pop() {
456       SUnit *V = Queue.top();
457       Queue.pop();
458       return V;
459     }
460   };
461
462   template<class SF>
463   class BURegReductionPriorityQueue : public RegReductionPriorityQueue<SF> {
464     // SUnits - The SUnits for the current graph.
465     const std::vector<SUnit> *SUnits;
466     
467     // SethiUllmanNumbers - The SethiUllman number for each node.
468     std::vector<int> SethiUllmanNumbers;
469
470   public:
471     BURegReductionPriorityQueue() {}
472
473     void initNodes(const std::vector<SUnit> &sunits) {
474       SUnits = &sunits;
475       // Add pseudo dependency edges for two-address nodes.
476       AddPseudoTwoAddrDeps();
477       // Calculate node priorities.
478       CalculatePriorities();
479     }
480
481     void releaseState() {
482       SUnits = 0;
483       SethiUllmanNumbers.clear();
484     }
485
486     int getSethiUllmanNumber(unsigned NodeNum) const {
487       assert(NodeNum < SethiUllmanNumbers.size());
488       return SethiUllmanNumbers[NodeNum];
489     }
490
491   private:
492     void AddPseudoTwoAddrDeps();
493     void CalculatePriorities();
494     int CalcNodePriority(const SUnit *SU);
495   };
496
497
498   template<class SF>
499   class TDRegReductionPriorityQueue : public RegReductionPriorityQueue<SF> {
500     // SUnits - The SUnits for the current graph.
501     const std::vector<SUnit> *SUnits;
502     
503     // SethiUllmanNumbers - The SethiUllman number for each node.
504     std::vector<int> SethiUllmanNumbers;
505
506   public:
507     TDRegReductionPriorityQueue() {}
508
509     void initNodes(const std::vector<SUnit> &sunits) {
510       SUnits = &sunits;
511       // Calculate node priorities.
512       CalculatePriorities();
513     }
514
515     void releaseState() {
516       SUnits = 0;
517       SethiUllmanNumbers.clear();
518     }
519
520     int getSethiUllmanNumber(unsigned NodeNum) const {
521       assert(NodeNum < SethiUllmanNumbers.size());
522       return SethiUllmanNumbers[NodeNum];
523     }
524
525   private:
526     void CalculatePriorities();
527     int CalcNodePriority(const SUnit *SU);
528   };
529 }
530
531 static bool isFloater(const SUnit *SU) {
532   if (SU->Node->isTargetOpcode()) {
533     if (SU->NumPreds == 0)
534       return true;
535     if (SU->NumPreds == 1) {
536       for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
537              E = SU->Preds.end(); I != E; ++I) {
538         if (I->second) continue;
539
540         SUnit *PredSU = I->first;
541         unsigned Opc = PredSU->Node->getOpcode();
542         if (Opc != ISD::EntryToken && Opc != ISD::TokenFactor &&
543             Opc != ISD::CopyFromReg && Opc != ISD::CopyToReg)
544           return false;
545       }
546       return true;
547     }
548   }
549   return false;
550 }
551
552 static bool isSimpleFloaterUse(const SUnit *SU) {
553   unsigned NumOps = 0;
554   for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
555          E = SU->Preds.end(); I != E; ++I) {
556     if (I->second) continue;
557     if (++NumOps > 1)
558       return false;
559     if (!isFloater(I->first))
560       return false;
561   }
562   return true;
563 }
564
565 // Bottom up
566 bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
567   unsigned LeftNum  = left->NodeNum;
568   unsigned RightNum = right->NodeNum;
569   bool LIsTarget = left->Node->isTargetOpcode();
570   bool RIsTarget = right->Node->isTargetOpcode();
571   int LPriority = SPQ->getSethiUllmanNumber(LeftNum);
572   int RPriority = SPQ->getSethiUllmanNumber(RightNum);
573   int LBonus = 0;
574   int RBonus = 0;
575
576   // Schedule floaters (e.g. load from some constant address) and those nodes
577   // with a single predecessor each first. They maintain / reduce register
578   // pressure.
579   if (isFloater(left) || isSimpleFloaterUse(left))
580     LBonus += 2;
581   if (isFloater(right) || isSimpleFloaterUse(right))
582     RBonus += 2;
583
584   // Special tie breaker: if two nodes share a operand, the one that use it
585   // as a def&use operand is preferred.
586   if (LIsTarget && RIsTarget) {
587     if (left->isTwoAddress && !right->isTwoAddress) {
588       SDNode *DUNode = left->Node->getOperand(0).Val;
589       if (DUNode->isOperand(right->Node))
590         LBonus += 2;
591     }
592     if (!left->isTwoAddress && right->isTwoAddress) {
593       SDNode *DUNode = right->Node->getOperand(0).Val;
594       if (DUNode->isOperand(left->Node))
595         RBonus += 2;
596     }
597   }
598
599   if (LPriority+LBonus < RPriority+RBonus)
600     return true;
601   else if (LPriority+LBonus == RPriority+RBonus)
602     if (left->Height > right->Height)
603       return true;
604     else if (left->Height == right->Height)
605       if (left->Depth < right->Depth)
606         return true;
607       else if (left->Depth == right->Depth)
608         if (left->CycleBound > right->CycleBound) 
609           return true;
610   return false;
611 }
612
613 static inline bool isCopyFromLiveIn(const SUnit *SU) {
614   SDNode *N = SU->Node;
615   return N->getOpcode() == ISD::CopyFromReg &&
616     N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag;
617 }
618
619 // FIXME: This is probably too slow!
620 static void isReachable(SUnit *SU, SUnit *TargetSU,
621                         std::set<SUnit *> &Visited, bool &Reached) {
622   if (Reached) return;
623   if (SU == TargetSU) {
624     Reached = true;
625     return;
626   }
627   if (!Visited.insert(SU).second) return;
628
629   for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
630          E = SU->Preds.end(); I != E; ++I)
631     isReachable(I->first, TargetSU, Visited, Reached);
632 }
633
634 static bool isReachable(SUnit *SU, SUnit *TargetSU) {
635   std::set<SUnit *> Visited;
636   bool Reached = false;
637   isReachable(SU, TargetSU, Visited, Reached);
638   return Reached;
639 }
640
641 static SUnit *getDefUsePredecessor(SUnit *SU) {
642   SDNode *DU = SU->Node->getOperand(0).Val;
643   for (std::set<std::pair<SUnit*, bool> >::iterator
644          I = SU->Preds.begin(), E = SU->Preds.end(); I != E; ++I) {
645     if (I->second) continue;  // ignore chain preds
646     SUnit *PredSU = I->first;
647     if (PredSU->Node == DU)
648       return PredSU;
649   }
650
651   // Must be flagged.
652   return NULL;
653 }
654
655 static bool canClobber(SUnit *SU, SUnit *Op) {
656   if (SU->isTwoAddress)
657     return Op == getDefUsePredecessor(SU);
658   return false;
659 }
660
661 /// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses
662 /// it as a def&use operand. Add a pseudo control edge from it to the other
663 /// node (if it won't create a cycle) so the two-address one will be scheduled
664 /// first (lower in the schedule).
665 template<class SF>
666 void BURegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() {
667   for (unsigned i = 0, e = SUnits->size(); i != e; ++i) {
668     SUnit *SU = (SUnit *)&((*SUnits)[i]);
669     SDNode *Node = SU->Node;
670     if (!Node->isTargetOpcode())
671       continue;
672
673     if (SU->isTwoAddress) {
674       SUnit *DUSU = getDefUsePredecessor(SU);
675       if (!DUSU) continue;
676
677       for (std::set<std::pair<SUnit*, bool> >::iterator I = DUSU->Succs.begin(),
678              E = DUSU->Succs.end(); I != E; ++I) {
679         if (I->second) continue;
680         SUnit *SuccSU = I->first;
681         if (SuccSU != SU &&
682             (!canClobber(SuccSU, DUSU) ||
683              (!SU->isCommutable && SuccSU->isCommutable))){
684           if (SuccSU->Depth == SU->Depth && !isReachable(SuccSU, SU)) {
685             DEBUG(std::cerr << "Adding an edge from SU # " << SU->NodeNum
686                   << " to SU #" << SuccSU->NodeNum << "\n");
687             if (SU->Preds.insert(std::make_pair(SuccSU, true)).second)
688               SU->NumChainPredsLeft++;
689             if (SuccSU->Succs.insert(std::make_pair(SU, true)).second)
690               SuccSU->NumChainSuccsLeft++;
691           }
692         }
693       }
694     }
695   }
696 }
697
698 /// CalcNodePriority - Priority is the Sethi Ullman number. 
699 /// Smaller number is the higher priority.
700 template<class SF>
701 int BURegReductionPriorityQueue<SF>::CalcNodePriority(const SUnit *SU) {
702   int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
703   if (SethiUllmanNumber != 0)
704     return SethiUllmanNumber;
705
706   unsigned Opc = SU->Node->getOpcode();
707   if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
708     SethiUllmanNumber = INT_MAX - 10;
709   else if (SU->NumSuccsLeft == 0)
710     // If SU does not have a use, i.e. it doesn't produce a value that would
711     // be consumed (e.g. store), then it terminates a chain of computation.
712     // Give it a small SethiUllman number so it will be scheduled right before its
713     // predecessors that it doesn't lengthen their live ranges.
714     SethiUllmanNumber = INT_MIN + 10;
715   // FIXME: remove this else if? It seems to reduce register spills but often
716   // ends up increasing runtime. Need to investigate.
717   else if (SU->NumPredsLeft == 0 &&
718            (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
719     SethiUllmanNumber = INT_MAX - 10;
720   else {
721     int Extra = 0;
722     for (std::set<std::pair<SUnit*, bool> >::const_iterator
723          I = SU->Preds.begin(), E = SU->Preds.end(); I != E; ++I) {
724       if (I->second) continue;  // ignore chain preds
725       SUnit *PredSU = I->first;
726       int PredSethiUllman = CalcNodePriority(PredSU);
727       if (PredSethiUllman > SethiUllmanNumber) {
728         SethiUllmanNumber = PredSethiUllman;
729         Extra = 0;
730       } else if (PredSethiUllman == SethiUllmanNumber && !I->second)
731         Extra++;
732     }
733
734     SethiUllmanNumber += Extra;
735   }
736   
737   return SethiUllmanNumber;
738 }
739
740 /// CalculatePriorities - Calculate priorities of all scheduling units.
741 template<class SF>
742 void BURegReductionPriorityQueue<SF>::CalculatePriorities() {
743   SethiUllmanNumbers.assign(SUnits->size(), 0);
744   
745   for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
746     CalcNodePriority(&(*SUnits)[i]);
747 }
748
749 static unsigned SumOfUnscheduledPredsOfSuccs(const SUnit *SU) {
750   unsigned Sum = 0;
751   for (std::set<std::pair<SUnit*, bool> >::const_iterator
752          I = SU->Succs.begin(), E = SU->Succs.end(); I != E; ++I) {
753     SUnit *SuccSU = I->first;
754     for (std::set<std::pair<SUnit*, bool> >::const_iterator
755          II = SuccSU->Preds.begin(), EE = SuccSU->Preds.end(); II != EE; ++II) {
756       SUnit *PredSU = II->first;
757       if (!PredSU->isScheduled)
758         Sum++;
759     }
760   }
761
762   return Sum;
763 }
764
765
766 // Top down
767 bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const {
768   unsigned LeftNum  = left->NodeNum;
769   unsigned RightNum = right->NodeNum;
770   int LPriority = SPQ->getSethiUllmanNumber(LeftNum);
771   int RPriority = SPQ->getSethiUllmanNumber(RightNum);
772   bool LIsTarget = left->Node->isTargetOpcode();
773   bool RIsTarget = right->Node->isTargetOpcode();
774   bool LIsFloater = LIsTarget && left->NumPreds == 0;
775   bool RIsFloater = RIsTarget && right->NumPreds == 0;
776   unsigned LBonus = (SumOfUnscheduledPredsOfSuccs(left) == 1) ? 2 : 0;
777   unsigned RBonus = (SumOfUnscheduledPredsOfSuccs(right) == 1) ? 2 : 0;
778
779   if (left->NumSuccs == 0 && right->NumSuccs != 0)
780     return false;
781   else if (left->NumSuccs != 0 && right->NumSuccs == 0)
782     return true;
783
784   // Special tie breaker: if two nodes share a operand, the one that use it
785   // as a def&use operand is preferred.
786   if (LIsTarget && RIsTarget) {
787     if (left->isTwoAddress && !right->isTwoAddress) {
788       SDNode *DUNode = left->Node->getOperand(0).Val;
789       if (DUNode->isOperand(right->Node))
790         RBonus += 2;
791     }
792     if (!left->isTwoAddress && right->isTwoAddress) {
793       SDNode *DUNode = right->Node->getOperand(0).Val;
794       if (DUNode->isOperand(left->Node))
795         LBonus += 2;
796     }
797   }
798   if (LIsFloater)
799     LBonus -= 2;
800   if (RIsFloater)
801     RBonus -= 2;
802   if (left->NumSuccs == 1)
803     LBonus += 2;
804   if (right->NumSuccs == 1)
805     RBonus += 2;
806
807   if (LPriority+LBonus < RPriority+RBonus)
808     return true;
809   else if (LPriority == RPriority)
810     if (left->Depth < right->Depth)
811       return true;
812     else if (left->Depth == right->Depth)
813       if (left->NumSuccsLeft > right->NumSuccsLeft)
814         return true;
815       else if (left->NumSuccsLeft == right->NumSuccsLeft)
816         if (left->CycleBound > right->CycleBound) 
817           return true;
818   return false;
819 }
820
821 /// CalcNodePriority - Priority is the Sethi Ullman number. 
822 /// Smaller number is the higher priority.
823 template<class SF>
824 int TDRegReductionPriorityQueue<SF>::CalcNodePriority(const SUnit *SU) {
825   int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum];
826   if (SethiUllmanNumber != 0)
827     return SethiUllmanNumber;
828
829   unsigned Opc = SU->Node->getOpcode();
830   if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg)
831     SethiUllmanNumber = INT_MAX - 10;
832   else if (SU->NumSuccsLeft == 0)
833     // If SU does not have a use, i.e. it doesn't produce a value that would
834     // be consumed (e.g. store), then it terminates a chain of computation.
835     // Give it a small SethiUllman number so it will be scheduled right before its
836     // predecessors that it doesn't lengthen their live ranges.
837     SethiUllmanNumber = INT_MIN + 10;
838   else if (SU->NumPredsLeft == 0 &&
839            (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU)))
840     SethiUllmanNumber = 1;
841   else {
842     int Extra = 0;
843     for (std::set<std::pair<SUnit*, bool> >::const_iterator
844          I = SU->Preds.begin(), E = SU->Preds.end(); I != E; ++I) {
845       if (I->second) continue;  // ignore chain preds
846       SUnit *PredSU = I->first;
847       int PredSethiUllman = CalcNodePriority(PredSU);
848       if (PredSethiUllman > SethiUllmanNumber) {
849         SethiUllmanNumber = PredSethiUllman;
850         Extra = 0;
851       } else if (PredSethiUllman == SethiUllmanNumber && !I->second)
852         Extra++;
853     }
854
855     SethiUllmanNumber += Extra;
856   }
857   
858   return SethiUllmanNumber;
859 }
860
861 /// CalculatePriorities - Calculate priorities of all scheduling units.
862 template<class SF>
863 void TDRegReductionPriorityQueue<SF>::CalculatePriorities() {
864   SethiUllmanNumbers.assign(SUnits->size(), 0);
865   
866   for (unsigned i = 0, e = SUnits->size(); i != e; ++i)
867     CalcNodePriority(&(*SUnits)[i]);
868 }
869
870 //===----------------------------------------------------------------------===//
871 //                         Public Constructor Functions
872 //===----------------------------------------------------------------------===//
873
874 llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG,
875                                                     MachineBasicBlock *BB) {
876   return new ScheduleDAGRRList(DAG, BB, DAG.getTarget(), true,
877                                new BURegReductionPriorityQueue<bu_ls_rr_sort>());
878 }
879
880 llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAG &DAG,
881                                                     MachineBasicBlock *BB) {
882   return new ScheduleDAGRRList(DAG, BB, DAG.getTarget(), false,
883                                new TDRegReductionPriorityQueue<td_ls_rr_sort>());
884 }
885