cfe5e6b0767d91711e2bf0e261ecc5ffb3a1dcbd
[oota-llvm.git] / lib / CodeGen / SelectionDAG / ScheduleDAG.cpp
1 //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
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/ScheduleDAG.h"
18 #include "llvm/CodeGen/MachineConstantPool.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetLowering.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/MathExtras.h"
27 #include <iostream>
28 using namespace llvm;
29
30
31 /// BuildSchedUnits - Build SUnits from the selection dag that we are input.
32 /// This SUnit graph is similar to the SelectionDAG, but represents flagged
33 /// together nodes with a single SUnit.
34 void ScheduleDAG::BuildSchedUnits() {
35   // Reserve entries in the vector for each of the SUnits we are creating.  This
36   // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
37   // invalidated.
38   SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
39   
40   const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
41   
42   for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
43        E = DAG.allnodes_end(); NI != E; ++NI) {
44     if (isPassiveNode(NI))  // Leaf node, e.g. a TargetImmediate.
45       continue;
46     
47     // If this node has already been processed, stop now.
48     if (SUnitMap[NI]) continue;
49     
50     SUnit *NodeSUnit = NewSUnit(NI);
51     
52     // See if anything is flagged to this node, if so, add them to flagged
53     // nodes.  Nodes can have at most one flag input and one flag output.  Flags
54     // are required the be the last operand and result of a node.
55     
56     // Scan up, adding flagged preds to FlaggedNodes.
57     SDNode *N = NI;
58     while (N->getNumOperands() &&
59            N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
60       N = N->getOperand(N->getNumOperands()-1).Val;
61       NodeSUnit->FlaggedNodes.push_back(N);
62       SUnitMap[N] = NodeSUnit;
63     }
64     
65     // Scan down, adding this node and any flagged succs to FlaggedNodes if they
66     // have a user of the flag operand.
67     N = NI;
68     while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
69       SDOperand FlagVal(N, N->getNumValues()-1);
70       
71       // There are either zero or one users of the Flag result.
72       bool HasFlagUse = false;
73       for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); 
74            UI != E; ++UI)
75         if (FlagVal.isOperand(*UI)) {
76           HasFlagUse = true;
77           NodeSUnit->FlaggedNodes.push_back(N);
78           SUnitMap[N] = NodeSUnit;
79           N = *UI;
80           break;
81         }
82           if (!HasFlagUse) break;
83     }
84     
85     // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
86     // Update the SUnit
87     NodeSUnit->Node = N;
88     SUnitMap[N] = NodeSUnit;
89     
90     // Compute the latency for the node.  We use the sum of the latencies for
91     // all nodes flagged together into this SUnit.
92     if (InstrItins.isEmpty()) {
93       // No latency information.
94       NodeSUnit->Latency = 1;
95     } else {
96       NodeSUnit->Latency = 0;
97       if (N->isTargetOpcode()) {
98         unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
99         InstrStage *S = InstrItins.begin(SchedClass);
100         InstrStage *E = InstrItins.end(SchedClass);
101         for (; S != E; ++S)
102           NodeSUnit->Latency += S->Cycles;
103       }
104       for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
105         SDNode *FNode = NodeSUnit->FlaggedNodes[i];
106         if (FNode->isTargetOpcode()) {
107           unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
108           InstrStage *S = InstrItins.begin(SchedClass);
109           InstrStage *E = InstrItins.end(SchedClass);
110           for (; S != E; ++S)
111             NodeSUnit->Latency += S->Cycles;
112         }
113       }
114     }
115   }
116   
117   // Pass 2: add the preds, succs, etc.
118   for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
119     SUnit *SU = &SUnits[su];
120     SDNode *MainNode = SU->Node;
121     
122     if (MainNode->isTargetOpcode()) {
123       unsigned Opc = MainNode->getTargetOpcode();
124       if (TII->isTwoAddrInstr(Opc))
125         SU->isTwoAddress = true;
126       if (TII->isCommutableInstr(Opc))
127         SU->isCommutable = true;
128     }
129     
130     // Find all predecessors and successors of the group.
131     // Temporarily add N to make code simpler.
132     SU->FlaggedNodes.push_back(MainNode);
133     
134     for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
135       SDNode *N = SU->FlaggedNodes[n];
136       
137       for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
138         SDNode *OpN = N->getOperand(i).Val;
139         if (isPassiveNode(OpN)) continue;   // Not scheduled.
140         SUnit *OpSU = SUnitMap[OpN];
141         assert(OpSU && "Node has no SUnit!");
142         if (OpSU == SU) continue;           // In the same group.
143
144         MVT::ValueType OpVT = N->getOperand(i).getValueType();
145         assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
146         bool isChain = OpVT == MVT::Other;
147         
148         if (SU->Preds.insert(std::make_pair(OpSU, isChain)).second) {
149           if (!isChain) {
150             SU->NumPreds++;
151             SU->NumPredsLeft++;
152           } else {
153             SU->NumChainPredsLeft++;
154           }
155         }
156         if (OpSU->Succs.insert(std::make_pair(SU, isChain)).second) {
157           if (!isChain) {
158             OpSU->NumSuccs++;
159             OpSU->NumSuccsLeft++;
160           } else {
161             OpSU->NumChainSuccsLeft++;
162           }
163         }
164       }
165     }
166     
167     // Remove MainNode from FlaggedNodes again.
168     SU->FlaggedNodes.pop_back();
169   }
170   
171   return;
172 }
173
174 static void CalculateDepths(SUnit *SU, unsigned Depth) {
175   if (SU->Depth == 0 || Depth > SU->Depth) {
176     SU->Depth = Depth;
177     for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(),
178            E = SU->Succs.end(); I != E; ++I)
179       CalculateDepths(I->first, Depth+1);
180   }
181 }
182
183 void ScheduleDAG::CalculateDepths() {
184   SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
185   ::CalculateDepths(Entry, 0U);
186   for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
187     if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
188       ::CalculateDepths(&SUnits[i], 0U);
189     }
190 }
191
192 static void CalculateHeights(SUnit *SU, unsigned Height) {
193   if (SU->Height == 0 || Height > SU->Height) {
194     SU->Height = Height;
195     for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
196            E = SU->Preds.end(); I != E; ++I)
197       CalculateHeights(I->first, Height+1);
198   }
199 }
200 void ScheduleDAG::CalculateHeights() {
201   SUnit *Root = SUnitMap[DAG.getRoot().Val];
202   ::CalculateHeights(Root, 0U);
203 }
204
205 /// CountResults - The results of target nodes have register or immediate
206 /// operands first, then an optional chain, and optional flag operands (which do
207 /// not go into the machine instrs.)
208 static unsigned CountResults(SDNode *Node) {
209   unsigned N = Node->getNumValues();
210   while (N && Node->getValueType(N - 1) == MVT::Flag)
211     --N;
212   if (N && Node->getValueType(N - 1) == MVT::Other)
213     --N;    // Skip over chain result.
214   return N;
215 }
216
217 /// CountOperands  The inputs to target nodes have any actual inputs first,
218 /// followed by an optional chain operand, then flag operands.  Compute the
219 /// number of actual operands that  will go into the machine instr.
220 static unsigned CountOperands(SDNode *Node) {
221   unsigned N = Node->getNumOperands();
222   while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
223     --N;
224   if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
225     --N; // Ignore chain if it exists.
226   return N;
227 }
228
229 static unsigned CreateVirtualRegisters(MachineInstr *MI,
230                                        unsigned NumResults,
231                                        SSARegMap *RegMap,
232                                        const TargetInstrInfo *TII,
233                                        const TargetInstrDescriptor &II) {
234   // Create the result registers for this node and add the result regs to
235   // the machine instruction.
236   unsigned ResultReg =
237     RegMap->createVirtualRegister(TII->getInstrOperandRegClass(&II, 0));
238   MI->addRegOperand(ResultReg, MachineOperand::Def);
239   for (unsigned i = 1; i != NumResults; ++i) {
240     const TargetRegisterClass *RC = TII->getInstrOperandRegClass(&II, i);
241     assert(RC && "Isn't a register operand!");
242     MI->addRegOperand(RegMap->createVirtualRegister(RC), MachineOperand::Def);
243   }
244   return ResultReg;
245 }
246
247 /// getVR - Return the virtual register corresponding to the specified result
248 /// of the specified node.
249 static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) {
250   std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
251   assert(I != VRBaseMap.end() && "Node emitted out of order - late");
252   return I->second + Op.ResNo;
253 }
254
255
256 /// AddOperand - Add the specified operand to the specified machine instr.  II
257 /// specifies the instruction information for the node, and IIOpNum is the
258 /// operand number (in the II) that we are adding. IIOpNum and II are used for 
259 /// assertions only.
260 void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
261                              unsigned IIOpNum,
262                              const TargetInstrDescriptor *II,
263                              std::map<SDNode*, unsigned> &VRBaseMap) {
264   if (Op.isTargetOpcode()) {
265     // Note that this case is redundant with the final else block, but we
266     // include it because it is the most common and it makes the logic
267     // simpler here.
268     assert(Op.getValueType() != MVT::Other &&
269            Op.getValueType() != MVT::Flag &&
270            "Chain and flag operands should occur at end of operand list!");
271     
272     // Get/emit the operand.
273     unsigned VReg = getVR(Op, VRBaseMap);
274     MI->addRegOperand(VReg, MachineOperand::Use);
275     
276     // Verify that it is right.
277     assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
278     if (II) {
279       const TargetRegisterClass *RC = TII->getInstrOperandRegClass(II, IIOpNum);
280       assert(RC && "Don't have operand info for this instruction!");
281       assert(RegMap->getRegClass(VReg) == RC &&
282              "Register class of operand and regclass of use don't agree!");
283     }
284   } else if (ConstantSDNode *C =
285              dyn_cast<ConstantSDNode>(Op)) {
286     MI->addImmOperand(C->getValue());
287   } else if (RegisterSDNode*R =
288              dyn_cast<RegisterSDNode>(Op)) {
289     MI->addRegOperand(R->getReg(), MachineOperand::Use);
290   } else if (GlobalAddressSDNode *TGA =
291              dyn_cast<GlobalAddressSDNode>(Op)) {
292     MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
293   } else if (BasicBlockSDNode *BB =
294              dyn_cast<BasicBlockSDNode>(Op)) {
295     MI->addMachineBasicBlockOperand(BB->getBasicBlock());
296   } else if (FrameIndexSDNode *FI =
297              dyn_cast<FrameIndexSDNode>(Op)) {
298     MI->addFrameIndexOperand(FI->getIndex());
299   } else if (JumpTableSDNode *JT =
300              dyn_cast<JumpTableSDNode>(Op)) {
301     MI->addJumpTableIndexOperand(JT->getIndex());
302   } else if (ConstantPoolSDNode *CP = 
303              dyn_cast<ConstantPoolSDNode>(Op)) {
304     int Offset = CP->getOffset();
305     unsigned Align = CP->getAlignment();
306     // MachineConstantPool wants an explicit alignment.
307     if (Align == 0) {
308       if (CP->get()->getType() == Type::DoubleTy)
309         Align = 3;  // always 8-byte align doubles.
310       else {
311         Align = TM.getTargetData()
312           ->getTypeAlignmentShift(CP->get()->getType());
313         if (Align == 0) {
314           // Alignment of packed types.  FIXME!
315           Align = TM.getTargetData()->getTypeSize(CP->get()->getType());
316           Align = Log2_64(Align);
317         }
318       }
319     }
320     
321     unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
322     MI->addConstantPoolIndexOperand(Idx, Offset);
323   } else if (ExternalSymbolSDNode *ES = 
324              dyn_cast<ExternalSymbolSDNode>(Op)) {
325     MI->addExternalSymbolOperand(ES->getSymbol());
326   } else {
327     assert(Op.getValueType() != MVT::Other &&
328            Op.getValueType() != MVT::Flag &&
329            "Chain and flag operands should occur at end of operand list!");
330     unsigned VReg = getVR(Op, VRBaseMap);
331     MI->addRegOperand(VReg, MachineOperand::Use);
332     
333     // Verify that it is right.
334     assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
335     if (II) {
336       const TargetRegisterClass *RC = TII->getInstrOperandRegClass(II, IIOpNum);
337       assert(RC && "Don't have operand info for this instruction!");
338       assert(RegMap->getRegClass(VReg) == RC &&
339              "Register class of operand and regclass of use don't agree!");
340     }
341   }
342   
343 }
344
345
346 /// EmitNode - Generate machine code for an node and needed dependencies.
347 ///
348 void ScheduleDAG::EmitNode(SDNode *Node, 
349                            std::map<SDNode*, unsigned> &VRBaseMap) {
350   unsigned VRBase = 0;                 // First virtual register for node
351   
352   // If machine instruction
353   if (Node->isTargetOpcode()) {
354     unsigned Opc = Node->getTargetOpcode();
355     const TargetInstrDescriptor &II = TII->get(Opc);
356
357     unsigned NumResults = CountResults(Node);
358     unsigned NodeOperands = CountOperands(Node);
359     unsigned NumMIOperands = NodeOperands + NumResults;
360 #ifndef NDEBUG
361     assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
362            "#operands for dag node doesn't match .td file!"); 
363 #endif
364
365     // Create the new machine instruction.
366     MachineInstr *MI = new MachineInstr(Opc, NumMIOperands);
367     
368     // Add result register values for things that are defined by this
369     // instruction.
370     
371     // If the node is only used by a CopyToReg and the dest reg is a vreg, use
372     // the CopyToReg'd destination register instead of creating a new vreg.
373     if (NumResults == 1) {
374       for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
375            UI != E; ++UI) {
376         SDNode *Use = *UI;
377         if (Use->getOpcode() == ISD::CopyToReg && 
378             Use->getOperand(2).Val == Node) {
379           unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
380           if (MRegisterInfo::isVirtualRegister(Reg)) {
381             VRBase = Reg;
382             MI->addRegOperand(Reg, MachineOperand::Def);
383             break;
384           }
385         }
386       }
387     }
388     
389     // Otherwise, create new virtual registers.
390     if (NumResults && VRBase == 0)
391       VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, TII, II);
392     
393     // Emit all of the actual operands of this instruction, adding them to the
394     // instruction as appropriate.
395     for (unsigned i = 0; i != NodeOperands; ++i)
396       AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
397
398     // Commute node if it has been determined to be profitable.
399     if (CommuteSet.count(Node)) {
400       MachineInstr *NewMI = TII->commuteInstruction(MI);
401       if (NewMI == 0)
402         DEBUG(std::cerr << "Sched: COMMUTING FAILED!\n");
403       else {
404         DEBUG(std::cerr << "Sched: COMMUTED TO: " << *NewMI);
405         MI = NewMI;
406       }
407     }
408
409     // Now that we have emitted all operands, emit this instruction itself.
410     if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
411       BB->insert(BB->end(), MI);
412     } else {
413       // Insert this instruction into the end of the basic block, potentially
414       // taking some custom action.
415       BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
416     }
417   } else {
418     switch (Node->getOpcode()) {
419     default:
420       Node->dump(); 
421       assert(0 && "This target-independent node should have been selected!");
422     case ISD::EntryToken: // fall thru
423     case ISD::TokenFactor:
424       break;
425     case ISD::CopyToReg: {
426       unsigned InReg = getVR(Node->getOperand(2), VRBaseMap);
427       unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
428       if (InReg != DestReg)   // Coalesced away the copy?
429         MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
430                           RegMap->getRegClass(InReg));
431       break;
432     }
433     case ISD::CopyFromReg: {
434       unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
435       if (MRegisterInfo::isVirtualRegister(SrcReg)) {
436         VRBase = SrcReg;  // Just use the input register directly!
437         break;
438       }
439
440       // If the node is only used by a CopyToReg and the dest reg is a vreg, use
441       // the CopyToReg'd destination register instead of creating a new vreg.
442       for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
443            UI != E; ++UI) {
444         SDNode *Use = *UI;
445         if (Use->getOpcode() == ISD::CopyToReg && 
446             Use->getOperand(2).Val == Node) {
447           unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
448           if (MRegisterInfo::isVirtualRegister(DestReg)) {
449             VRBase = DestReg;
450             break;
451           }
452         }
453       }
454
455       // Figure out the register class to create for the destreg.
456       const TargetRegisterClass *TRC = 0;
457       if (VRBase) {
458         TRC = RegMap->getRegClass(VRBase);
459       } else {
460
461         // Pick the register class of the right type that contains this physreg.
462         for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
463              E = MRI->regclass_end(); I != E; ++I)
464           if ((*I)->hasType(Node->getValueType(0)) &&
465               (*I)->contains(SrcReg)) {
466             TRC = *I;
467             break;
468           }
469         assert(TRC && "Couldn't find register class for reg copy!");
470       
471         // Create the reg, emit the copy.
472         VRBase = RegMap->createVirtualRegister(TRC);
473       }
474       MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
475       break;
476     }
477     case ISD::INLINEASM: {
478       unsigned NumOps = Node->getNumOperands();
479       if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
480         --NumOps;  // Ignore the flag operand.
481       
482       // Create the inline asm machine instruction.
483       MachineInstr *MI =
484         new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
485
486       // Add the asm string as an external symbol operand.
487       const char *AsmStr =
488         cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
489       MI->addExternalSymbolOperand(AsmStr);
490       
491       // Add all of the operand registers to the instruction.
492       for (unsigned i = 2; i != NumOps;) {
493         unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
494         unsigned NumVals = Flags >> 3;
495         
496         MI->addImmOperand(Flags);
497         ++i;  // Skip the ID value.
498         
499         switch (Flags & 7) {
500         default: assert(0 && "Bad flags!");
501         case 1:  // Use of register.
502           for (; NumVals; --NumVals, ++i) {
503             unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
504             MI->addRegOperand(Reg, MachineOperand::Use);
505           }
506           break;
507         case 2:   // Def of register.
508           for (; NumVals; --NumVals, ++i) {
509             unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
510             MI->addRegOperand(Reg, MachineOperand::Def);
511           }
512           break;
513         case 3: { // Immediate.
514           assert(NumVals == 1 && "Unknown immediate value!");
515           uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
516           MI->addImmOperand(Val);
517           ++i;
518           break;
519         }
520         case 4:  // Addressing mode.
521           // The addressing mode has been selected, just add all of the
522           // operands to the machine instruction.
523           for (; NumVals; --NumVals, ++i)
524             AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
525           break;
526         }
527       }
528       break;
529     }
530     }
531   }
532
533   assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
534   VRBaseMap[Node] = VRBase;
535 }
536
537 void ScheduleDAG::EmitNoop() {
538   TII->insertNoop(*BB, BB->end());
539 }
540
541 /// EmitSchedule - Emit the machine code in scheduled order.
542 void ScheduleDAG::EmitSchedule() {
543   // If this is the first basic block in the function, and if it has live ins
544   // that need to be copied into vregs, emit the copies into the top of the
545   // block before emitting the code for the block.
546   MachineFunction &MF = DAG.getMachineFunction();
547   if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
548     for (MachineFunction::livein_iterator LI = MF.livein_begin(),
549          E = MF.livein_end(); LI != E; ++LI)
550       if (LI->second)
551         MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
552                           LI->first, RegMap->getRegClass(LI->second));
553   }
554   
555   
556   // Finally, emit the code for all of the scheduled instructions.
557   std::map<SDNode*, unsigned> VRBaseMap;
558   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
559     if (SUnit *SU = Sequence[i]) {
560       for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++)
561         EmitNode(SU->FlaggedNodes[j], VRBaseMap);
562       EmitNode(SU->Node, VRBaseMap);
563     } else {
564       // Null SUnit* is a noop.
565       EmitNoop();
566     }
567   }
568 }
569
570 /// dump - dump the schedule.
571 void ScheduleDAG::dumpSchedule() const {
572   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
573     if (SUnit *SU = Sequence[i])
574       SU->dump(&DAG);
575     else
576       std::cerr << "**** NOOP ****\n";
577   }
578 }
579
580
581 /// Run - perform scheduling.
582 ///
583 MachineBasicBlock *ScheduleDAG::Run() {
584   TII = TM.getInstrInfo();
585   MRI = TM.getRegisterInfo();
586   RegMap = BB->getParent()->getSSARegMap();
587   ConstPool = BB->getParent()->getConstantPool();
588
589   Schedule();
590   return BB;
591 }
592
593 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
594 /// a group of nodes flagged together.
595 void SUnit::dump(const SelectionDAG *G) const {
596   std::cerr << "SU(" << NodeNum << "): ";
597   Node->dump(G);
598   std::cerr << "\n";
599   if (FlaggedNodes.size() != 0) {
600     for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
601       std::cerr << "    ";
602       FlaggedNodes[i]->dump(G);
603       std::cerr << "\n";
604     }
605   }
606 }
607
608 void SUnit::dumpAll(const SelectionDAG *G) const {
609   dump(G);
610
611   std::cerr << "  # preds left       : " << NumPredsLeft << "\n";
612   std::cerr << "  # succs left       : " << NumSuccsLeft << "\n";
613   std::cerr << "  # chain preds left : " << NumChainPredsLeft << "\n";
614   std::cerr << "  # chain succs left : " << NumChainSuccsLeft << "\n";
615   std::cerr << "  Latency            : " << Latency << "\n";
616   std::cerr << "  Depth              : " << Depth << "\n";
617   std::cerr << "  Height             : " << Height << "\n";
618
619   if (Preds.size() != 0) {
620     std::cerr << "  Predecessors:\n";
621     for (std::set<std::pair<SUnit*,bool> >::const_iterator I = Preds.begin(),
622            E = Preds.end(); I != E; ++I) {
623       if (I->second)
624         std::cerr << "   ch  ";
625       else
626         std::cerr << "   val ";
627       I->first->dump(G);
628     }
629   }
630   if (Succs.size() != 0) {
631     std::cerr << "  Successors:\n";
632     for (std::set<std::pair<SUnit*, bool> >::const_iterator I = Succs.begin(),
633            E = Succs.end(); I != E; ++I) {
634       if (I->second)
635         std::cerr << "   ch  ";
636       else
637         std::cerr << "   val ";
638       I->first->dump(G);
639     }
640   }
641   std::cerr << "\n";
642 }