Teach DAG scheduling how to properly emit subreg insert/extract machine instructions...
[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 "pre-RA-sched"
17 #include "llvm/Type.h"
18 #include "llvm/CodeGen/ScheduleDAG.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFunction.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/Target/TargetLowering.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 using namespace llvm;
29
30 /// BuildSchedUnits - Build SUnits from the selection dag that we are input.
31 /// This SUnit graph is similar to the SelectionDAG, but represents flagged
32 /// together nodes with a single SUnit.
33 void ScheduleDAG::BuildSchedUnits() {
34   // Reserve entries in the vector for each of the SUnits we are creating.  This
35   // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
36   // invalidated.
37   SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
38   
39   const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
40   
41   for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
42        E = DAG.allnodes_end(); NI != E; ++NI) {
43     if (isPassiveNode(NI))  // Leaf node, e.g. a TargetImmediate.
44       continue;
45     
46     // If this node has already been processed, stop now.
47     if (SUnitMap[NI]) continue;
48     
49     SUnit *NodeSUnit = NewSUnit(NI);
50     
51     // See if anything is flagged to this node, if so, add them to flagged
52     // nodes.  Nodes can have at most one flag input and one flag output.  Flags
53     // are required the be the last operand and result of a node.
54     
55     // Scan up, adding flagged preds to FlaggedNodes.
56     SDNode *N = NI;
57     if (N->getNumOperands() &&
58         N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
59       do {
60         N = N->getOperand(N->getNumOperands()-1).Val;
61         NodeSUnit->FlaggedNodes.push_back(N);
62         SUnitMap[N] = NodeSUnit;
63       } while (N->getNumOperands() &&
64                N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
65       std::reverse(NodeSUnit->FlaggedNodes.begin(),
66                    NodeSUnit->FlaggedNodes.end());
67     }
68     
69     // Scan down, adding this node and any flagged succs to FlaggedNodes if they
70     // have a user of the flag operand.
71     N = NI;
72     while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
73       SDOperand FlagVal(N, N->getNumValues()-1);
74       
75       // There are either zero or one users of the Flag result.
76       bool HasFlagUse = false;
77       for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); 
78            UI != E; ++UI)
79         if (FlagVal.isOperand(*UI)) {
80           HasFlagUse = true;
81           NodeSUnit->FlaggedNodes.push_back(N);
82           SUnitMap[N] = NodeSUnit;
83           N = *UI;
84           break;
85         }
86       if (!HasFlagUse) break;
87     }
88     
89     // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
90     // Update the SUnit
91     NodeSUnit->Node = N;
92     SUnitMap[N] = NodeSUnit;
93     
94     // Compute the latency for the node.  We use the sum of the latencies for
95     // all nodes flagged together into this SUnit.
96     if (InstrItins.isEmpty()) {
97       // No latency information.
98       NodeSUnit->Latency = 1;
99     } else {
100       NodeSUnit->Latency = 0;
101       if (N->isTargetOpcode()) {
102         unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
103         InstrStage *S = InstrItins.begin(SchedClass);
104         InstrStage *E = InstrItins.end(SchedClass);
105         for (; S != E; ++S)
106           NodeSUnit->Latency += S->Cycles;
107       }
108       for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
109         SDNode *FNode = NodeSUnit->FlaggedNodes[i];
110         if (FNode->isTargetOpcode()) {
111           unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
112           InstrStage *S = InstrItins.begin(SchedClass);
113           InstrStage *E = InstrItins.end(SchedClass);
114           for (; S != E; ++S)
115             NodeSUnit->Latency += S->Cycles;
116         }
117       }
118     }
119   }
120   
121   // Pass 2: add the preds, succs, etc.
122   for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
123     SUnit *SU = &SUnits[su];
124     SDNode *MainNode = SU->Node;
125     
126     if (MainNode->isTargetOpcode()) {
127       unsigned Opc = MainNode->getTargetOpcode();
128       for (unsigned i = 0, ee = TII->getNumOperands(Opc); i != ee; ++i) {
129         if (TII->getOperandConstraint(Opc, i, TOI::TIED_TO) != -1) {
130           SU->isTwoAddress = true;
131           break;
132         }
133       }
134       if (TII->isCommutableInstr(Opc))
135         SU->isCommutable = true;
136     }
137     
138     // Find all predecessors and successors of the group.
139     // Temporarily add N to make code simpler.
140     SU->FlaggedNodes.push_back(MainNode);
141     
142     for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
143       SDNode *N = SU->FlaggedNodes[n];
144       
145       for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
146         SDNode *OpN = N->getOperand(i).Val;
147         if (isPassiveNode(OpN)) continue;   // Not scheduled.
148         SUnit *OpSU = SUnitMap[OpN];
149         assert(OpSU && "Node has no SUnit!");
150         if (OpSU == SU) continue;           // In the same group.
151
152         MVT::ValueType OpVT = N->getOperand(i).getValueType();
153         assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
154         bool isChain = OpVT == MVT::Other;
155         
156         if (SU->addPred(OpSU, isChain)) {
157           if (!isChain) {
158             SU->NumPreds++;
159             SU->NumPredsLeft++;
160           } else {
161             SU->NumChainPredsLeft++;
162           }
163         }
164         if (OpSU->addSucc(SU, isChain)) {
165           if (!isChain) {
166             OpSU->NumSuccs++;
167             OpSU->NumSuccsLeft++;
168           } else {
169             OpSU->NumChainSuccsLeft++;
170           }
171         }
172       }
173     }
174     
175     // Remove MainNode from FlaggedNodes again.
176     SU->FlaggedNodes.pop_back();
177   }
178   
179   return;
180 }
181
182 void ScheduleDAG::CalculateDepths() {
183   std::vector<std::pair<SUnit*, unsigned> > WorkList;
184   for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
185     if (SUnits[i].Preds.size() == 0/* && &SUnits[i] != Entry*/)
186       WorkList.push_back(std::make_pair(&SUnits[i], 0U));
187
188   while (!WorkList.empty()) {
189     SUnit *SU = WorkList.back().first;
190     unsigned Depth = WorkList.back().second;
191     WorkList.pop_back();
192     if (SU->Depth == 0 || Depth > SU->Depth) {
193       SU->Depth = Depth;
194       for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
195            I != E; ++I)
196         WorkList.push_back(std::make_pair(I->first, Depth+1));
197     }
198   }
199 }
200
201 void ScheduleDAG::CalculateHeights() {
202   std::vector<std::pair<SUnit*, unsigned> > WorkList;
203   SUnit *Root = SUnitMap[DAG.getRoot().Val];
204   WorkList.push_back(std::make_pair(Root, 0U));
205
206   while (!WorkList.empty()) {
207     SUnit *SU = WorkList.back().first;
208     unsigned Height = WorkList.back().second;
209     WorkList.pop_back();
210     if (SU->Height == 0 || Height > SU->Height) {
211       SU->Height = Height;
212       for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
213            I != E; ++I)
214         WorkList.push_back(std::make_pair(I->first, Height+1));
215     }
216   }
217 }
218
219 /// CountResults - The results of target nodes have register or immediate
220 /// operands first, then an optional chain, and optional flag operands (which do
221 /// not go into the machine instrs.)
222 unsigned ScheduleDAG::CountResults(SDNode *Node) {
223   unsigned N = Node->getNumValues();
224   while (N && Node->getValueType(N - 1) == MVT::Flag)
225     --N;
226   if (N && Node->getValueType(N - 1) == MVT::Other)
227     --N;    // Skip over chain result.
228   return N;
229 }
230
231 /// CountOperands  The inputs to target nodes have any actual inputs first,
232 /// followed by an optional chain operand, then flag operands.  Compute the
233 /// number of actual operands that  will go into the machine instr.
234 unsigned ScheduleDAG::CountOperands(SDNode *Node) {
235   unsigned N = Node->getNumOperands();
236   while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
237     --N;
238   if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
239     --N; // Ignore chain if it exists.
240   return N;
241 }
242
243 static const TargetRegisterClass *getInstrOperandRegClass(
244         const MRegisterInfo *MRI, 
245         const TargetInstrInfo *TII,
246         const TargetInstrDescriptor *II,
247         unsigned Op) {
248   if (Op >= II->numOperands) {
249     assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
250     return NULL;
251   }
252   const TargetOperandInfo &toi = II->OpInfo[Op];
253   return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
254          ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
255 }
256
257 static void CreateVirtualRegisters(SDNode *Node,
258                                    unsigned NumResults, 
259                                    const MRegisterInfo *MRI,
260                                    MachineInstr *MI,
261                                    SSARegMap *RegMap,
262                                    const TargetInstrInfo *TII,
263                                    const TargetInstrDescriptor &II,
264                                    DenseMap<SDOperand, unsigned> &VRBaseMap) {
265   for (unsigned i = 0; i < NumResults; ++i) {
266     // If the specific node value is only used by a CopyToReg and the dest reg
267     // is a vreg, use the CopyToReg'd destination register instead of creating
268     // a new vreg.
269     unsigned VRBase = 0;
270     for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
271          UI != E; ++UI) {
272       SDNode *Use = *UI;
273       if (Use->getOpcode() == ISD::CopyToReg && 
274           Use->getOperand(2).Val == Node &&
275           Use->getOperand(2).ResNo == i) {
276         unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
277         if (MRegisterInfo::isVirtualRegister(Reg)) {
278           VRBase = Reg;
279           MI->addRegOperand(Reg, true);
280           break;
281         }
282       }
283     }
284
285     if (VRBase == 0) {
286       // Create the result registers for this node and add the result regs to
287       // the machine instruction.
288       const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
289       assert(RC && "Isn't a register operand!");
290       VRBase = RegMap->createVirtualRegister(RC);
291       MI->addRegOperand(VRBase, true);
292     }
293
294     bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
295     assert(isNew && "Node emitted out of order - early");
296   }
297 }
298
299 /// getVR - Return the virtual register corresponding to the specified result
300 /// of the specified node.
301 static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
302   DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
303   assert(I != VRBaseMap.end() && "Node emitted out of order - late");
304   return I->second;
305 }
306
307
308 /// AddOperand - Add the specified operand to the specified machine instr.  II
309 /// specifies the instruction information for the node, and IIOpNum is the
310 /// operand number (in the II) that we are adding. IIOpNum and II are used for 
311 /// assertions only.
312 void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
313                              unsigned IIOpNum,
314                              const TargetInstrDescriptor *II,
315                              DenseMap<SDOperand, unsigned> &VRBaseMap) {
316   if (Op.isTargetOpcode()) {
317     // Note that this case is redundant with the final else block, but we
318     // include it because it is the most common and it makes the logic
319     // simpler here.
320     assert(Op.getValueType() != MVT::Other &&
321            Op.getValueType() != MVT::Flag &&
322            "Chain and flag operands should occur at end of operand list!");
323     
324     // Get/emit the operand.
325     unsigned VReg = getVR(Op, VRBaseMap);
326     const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
327     bool isOptDef = (IIOpNum < TID->numOperands)
328       ? (TID->OpInfo[IIOpNum].Flags & M_OPTIONAL_DEF_OPERAND) : false;
329     MI->addRegOperand(VReg, isOptDef);
330     
331     // Verify that it is right.
332     assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
333     if (II) {
334       const TargetRegisterClass *RC =
335                           getInstrOperandRegClass(MRI, TII, II, IIOpNum);
336       assert(RC && "Don't have operand info for this instruction!");
337       const TargetRegisterClass *VRC = RegMap->getRegClass(VReg);
338       if (VRC != RC) {
339         cerr << "Register class of operand and regclass of use don't agree!\n";
340 #ifndef NDEBUG
341         cerr << "Operand = " << IIOpNum << "\n";
342         cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
343         cerr << "MI = "; MI->print(cerr);
344         cerr << "VReg = " << VReg << "\n";
345         cerr << "VReg RegClass     size = " << VRC->getSize()
346              << ", align = " << VRC->getAlignment() << "\n";
347         cerr << "Expected RegClass size = " << RC->getSize()
348              << ", align = " << RC->getAlignment() << "\n";
349 #endif
350         cerr << "Fatal error, aborting.\n";
351         abort();
352       }
353     }
354   } else if (ConstantSDNode *C =
355              dyn_cast<ConstantSDNode>(Op)) {
356     MI->addImmOperand(C->getValue());
357   } else if (RegisterSDNode *R =
358              dyn_cast<RegisterSDNode>(Op)) {
359     MI->addRegOperand(R->getReg(), false);
360   } else if (GlobalAddressSDNode *TGA =
361              dyn_cast<GlobalAddressSDNode>(Op)) {
362     MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
363   } else if (BasicBlockSDNode *BB =
364              dyn_cast<BasicBlockSDNode>(Op)) {
365     MI->addMachineBasicBlockOperand(BB->getBasicBlock());
366   } else if (FrameIndexSDNode *FI =
367              dyn_cast<FrameIndexSDNode>(Op)) {
368     MI->addFrameIndexOperand(FI->getIndex());
369   } else if (JumpTableSDNode *JT =
370              dyn_cast<JumpTableSDNode>(Op)) {
371     MI->addJumpTableIndexOperand(JT->getIndex());
372   } else if (ConstantPoolSDNode *CP = 
373              dyn_cast<ConstantPoolSDNode>(Op)) {
374     int Offset = CP->getOffset();
375     unsigned Align = CP->getAlignment();
376     const Type *Type = CP->getType();
377     // MachineConstantPool wants an explicit alignment.
378     if (Align == 0) {
379       Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
380       if (Align == 0) {
381         // Alignment of vector types.  FIXME!
382         Align = TM.getTargetData()->getTypeSize(Type);
383         Align = Log2_64(Align);
384       }
385     }
386     
387     unsigned Idx;
388     if (CP->isMachineConstantPoolEntry())
389       Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
390     else
391       Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
392     MI->addConstantPoolIndexOperand(Idx, Offset);
393   } else if (ExternalSymbolSDNode *ES = 
394              dyn_cast<ExternalSymbolSDNode>(Op)) {
395     MI->addExternalSymbolOperand(ES->getSymbol());
396   } else {
397     assert(Op.getValueType() != MVT::Other &&
398            Op.getValueType() != MVT::Flag &&
399            "Chain and flag operands should occur at end of operand list!");
400     unsigned VReg = getVR(Op, VRBaseMap);
401     MI->addRegOperand(VReg, false);
402     
403     // Verify that it is right.
404     assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
405     if (II) {
406       const TargetRegisterClass *RC =
407                             getInstrOperandRegClass(MRI, TII, II, IIOpNum);
408       assert(RC && "Don't have operand info for this instruction!");
409       assert(RegMap->getRegClass(VReg) == RC &&
410              "Register class of operand and regclass of use don't agree!");
411     }
412   }
413   
414 }
415
416 // Returns the Register Class of a physical register
417 static const TargetRegisterClass *getPhysicalRegisterRegClass(
418         const MRegisterInfo *MRI,
419         MVT::ValueType VT,
420         unsigned reg) {
421   assert(MRegisterInfo::isPhysicalRegister(reg) &&
422          "reg must be a physical register");
423   // Pick the register class of the right type that contains this physreg.
424   for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
425          E = MRI->regclass_end(); I != E; ++I)
426     if ((*I)->hasType(VT) && (*I)->contains(reg))
427       return *I;
428   assert(false && "Couldn't find the register class");
429   return 0;
430 }
431
432 // Returns the Register Class of a subregister
433 static const TargetRegisterClass *getSubRegisterRegClass(
434         const TargetRegisterClass *TRC,
435         unsigned SubIdx) {
436   // Pick the register class of the subregister
437   MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1;
438   assert(I < TRC->subregclasses_end() && 
439          "Invalid subregister index for register class");
440   return *I;
441 }
442
443 static const TargetRegisterClass *getSuperregRegisterClass(
444         const TargetRegisterClass *TRC,
445         unsigned SubIdx,
446         MVT::ValueType VT) {
447   // Pick the register class of the superegister for this type
448   for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
449          E = TRC->superregclasses_end(); I != E; ++I)
450     if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
451       return *I;
452   assert(false && "Couldn't find the register class");
453   return 0;
454 }
455
456 /// EmitSubregNode - Generate machine code for subreg nodes.
457 ///
458 void ScheduleDAG::EmitSubregNode(SDNode *Node, 
459                            DenseMap<SDOperand, unsigned> &VRBaseMap) {
460   unsigned VRBase = 0;
461   unsigned Opc = Node->getTargetOpcode();
462   if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
463     // If the node is only used by a CopyToReg and the dest reg is a vreg, use
464     // the CopyToReg'd destination register instead of creating a new vreg.
465     for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
466          UI != E; ++UI) {
467       SDNode *Use = *UI;
468       if (Use->getOpcode() == ISD::CopyToReg && 
469           Use->getOperand(2).Val == Node) {
470         unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
471         if (MRegisterInfo::isVirtualRegister(DestReg)) {
472           VRBase = DestReg;
473           break;
474         }
475       }
476     }
477     
478     unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
479     
480     // TODO: If the node is a use of a CopyFromReg from a physical register
481     // fold the extract into the copy now
482
483     // TODO: Add tracking info to SSARegMap of which vregs are subregs
484     // to allow coalescing in the allocator
485     
486     // Create the extract_subreg machine instruction.
487     MachineInstr *MI =
488       new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
489
490     // Figure out the register class to create for the destreg.
491     unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
492     const TargetRegisterClass *TRC = RegMap->getRegClass(VReg);
493     const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
494
495     if (VRBase) {
496       // Grab the destination register
497       const TargetRegisterClass *DRC = 0;
498       DRC = RegMap->getRegClass(VRBase);
499       assert(SRC == DRC && 
500              "Source subregister and destination must have the same class");
501     } else {
502       // Create the reg
503       VRBase = RegMap->createVirtualRegister(SRC);
504     }
505     
506     // Add def, source, and subreg index
507     MI->addRegOperand(VRBase, true);
508     AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
509     MI->addImmOperand(SubIdx);
510     
511   } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
512     assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
513             "Malformed insert_subreg node");
514     bool isUndefInput = (Node->getNumOperands() == 2);
515     unsigned SubReg = 0;
516     unsigned SubIdx = 0;
517     
518     if (isUndefInput) {
519       SubReg = getVR(Node->getOperand(0), VRBaseMap);
520       SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
521     } else {
522       SubReg = getVR(Node->getOperand(1), VRBaseMap);
523       SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
524     }
525     
526     // TODO: Add tracking info to SSARegMap of which vregs are subregs
527     // to allow coalescing in the allocator
528           
529     // If the node is only used by a CopyToReg and the dest reg is a vreg, use
530     // the CopyToReg'd destination register instead of creating a new vreg.
531     // If the CopyToReg'd destination register is physical, then fold the
532     // insert into the copy
533     for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
534          UI != E; ++UI) {
535       SDNode *Use = *UI;
536       if (Use->getOpcode() == ISD::CopyToReg && 
537           Use->getOperand(2).Val == Node) {
538         unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
539         if (MRegisterInfo::isVirtualRegister(DestReg)) {
540           VRBase = DestReg;
541           break;
542         }
543       }
544     }
545     
546     // Create the insert_subreg machine instruction.
547     MachineInstr *MI =
548       new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
549       
550     // Figure out the register class to create for the destreg.
551     const TargetRegisterClass *TRC = 0;
552     if (VRBase) {
553       TRC = RegMap->getRegClass(VRBase);
554     } else {
555       TRC = getSuperregRegisterClass(RegMap->getRegClass(SubReg), 
556                                      SubIdx, 
557                                      Node->getValueType(0));
558       assert(TRC && "Couldn't determine register class for insert_subreg");
559       VRBase = RegMap->createVirtualRegister(TRC); // Create the reg
560     }
561     
562     MI->addRegOperand(VRBase, true);
563     AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
564     if (!isUndefInput)
565       AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
566     MI->addImmOperand(SubIdx);
567   } else
568     assert(0 && "Node is not a subreg insert or extract");
569      
570   bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
571   assert(isNew && "Node emitted out of order - early");
572 }
573
574 /// EmitNode - Generate machine code for an node and needed dependencies.
575 ///
576 void ScheduleDAG::EmitNode(SDNode *Node, 
577                            DenseMap<SDOperand, unsigned> &VRBaseMap) {
578   // If machine instruction
579   if (Node->isTargetOpcode()) {
580     unsigned Opc = Node->getTargetOpcode();
581     
582     // Handle subreg insert/extract specially
583     if (Opc == TargetInstrInfo::EXTRACT_SUBREG || 
584         Opc == TargetInstrInfo::INSERT_SUBREG) {
585       EmitSubregNode(Node, VRBaseMap);
586       return;
587     }
588     
589     const TargetInstrDescriptor &II = TII->get(Opc);
590
591     unsigned NumResults = CountResults(Node);
592     unsigned NodeOperands = CountOperands(Node);
593     unsigned NumMIOperands = NodeOperands + NumResults;
594 #ifndef NDEBUG
595     assert((unsigned(II.numOperands) == NumMIOperands ||
596             (II.Flags & M_VARIABLE_OPS)) &&
597            "#operands for dag node doesn't match .td file!"); 
598 #endif
599
600     // Create the new machine instruction.
601     MachineInstr *MI = new MachineInstr(II);
602     
603     // Add result register values for things that are defined by this
604     // instruction.
605     if (NumResults)
606       CreateVirtualRegisters(Node, NumResults, MRI, MI, RegMap,
607                              TII, II, VRBaseMap);
608     
609     // Emit all of the actual operands of this instruction, adding them to the
610     // instruction as appropriate.
611     for (unsigned i = 0; i != NodeOperands; ++i)
612       AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
613
614     // Commute node if it has been determined to be profitable.
615     if (CommuteSet.count(Node)) {
616       MachineInstr *NewMI = TII->commuteInstruction(MI);
617       if (NewMI == 0)
618         DOUT << "Sched: COMMUTING FAILED!\n";
619       else {
620         DOUT << "Sched: COMMUTED TO: " << *NewMI;
621         if (MI != NewMI) {
622           delete MI;
623           MI = NewMI;
624         }
625       }
626     }
627
628     // Now that we have emitted all operands, emit this instruction itself.
629     if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
630       BB->insert(BB->end(), MI);
631     } else {
632       // Insert this instruction into the end of the basic block, potentially
633       // taking some custom action.
634       BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
635     }
636   } else {
637     switch (Node->getOpcode()) {
638     default:
639 #ifndef NDEBUG
640       Node->dump(&DAG);
641 #endif
642       assert(0 && "This target-independent node should have been selected!");
643     case ISD::EntryToken: // fall thru
644     case ISD::TokenFactor:
645     case ISD::LABEL:
646       break;
647     case ISD::CopyToReg: {
648       unsigned InReg;
649       if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
650         InReg = R->getReg();
651       else
652         InReg = getVR(Node->getOperand(2), VRBaseMap);
653       unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
654       if (InReg != DestReg)  {// Coalesced away the copy?
655         const TargetRegisterClass *TRC = 0;
656         // Get the target register class
657         if (MRegisterInfo::isVirtualRegister(InReg))
658           TRC = RegMap->getRegClass(InReg);
659         else
660           TRC = getPhysicalRegisterRegClass(MRI,
661                                             Node->getOperand(2).getValueType(),
662                                             InReg);
663         MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC);
664       }
665       break;
666     }
667     case ISD::CopyFromReg: {
668       unsigned VRBase = 0;
669       unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
670       if (MRegisterInfo::isVirtualRegister(SrcReg)) {
671         // Just use the input register directly!
672         bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0),SrcReg));
673         assert(isNew && "Node emitted out of order - early");
674         break;
675       }
676
677       // If the node is only used by a CopyToReg and the dest reg is a vreg, use
678       // the CopyToReg'd destination register instead of creating a new vreg.
679       for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
680            UI != E; ++UI) {
681         SDNode *Use = *UI;
682         if (Use->getOpcode() == ISD::CopyToReg && 
683             Use->getOperand(2).Val == Node) {
684           unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
685           if (MRegisterInfo::isVirtualRegister(DestReg)) {
686             VRBase = DestReg;
687             break;
688           }
689         }
690       }
691
692       // Figure out the register class to create for the destreg.
693       const TargetRegisterClass *TRC = 0;
694       if (VRBase) {
695         TRC = RegMap->getRegClass(VRBase);
696       } else {
697         TRC = getPhysicalRegisterRegClass(MRI, Node->getValueType(0), SrcReg);
698
699         // Create the reg, emit the copy.
700         VRBase = RegMap->createVirtualRegister(TRC);
701       }
702       MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
703
704       bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
705       assert(isNew && "Node emitted out of order - early");
706       break;
707     }
708     case ISD::INLINEASM: {
709       unsigned NumOps = Node->getNumOperands();
710       if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
711         --NumOps;  // Ignore the flag operand.
712       
713       // Create the inline asm machine instruction.
714       MachineInstr *MI =
715         new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
716
717       // Add the asm string as an external symbol operand.
718       const char *AsmStr =
719         cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
720       MI->addExternalSymbolOperand(AsmStr);
721       
722       // Add all of the operand registers to the instruction.
723       for (unsigned i = 2; i != NumOps;) {
724         unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
725         unsigned NumVals = Flags >> 3;
726         
727         MI->addImmOperand(Flags);
728         ++i;  // Skip the ID value.
729         
730         switch (Flags & 7) {
731         default: assert(0 && "Bad flags!");
732         case 1:  // Use of register.
733           for (; NumVals; --NumVals, ++i) {
734             unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
735             MI->addRegOperand(Reg, false);
736           }
737           break;
738         case 2:   // Def of register.
739           for (; NumVals; --NumVals, ++i) {
740             unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
741             MI->addRegOperand(Reg, true);
742           }
743           break;
744         case 3: { // Immediate.
745           assert(NumVals == 1 && "Unknown immediate value!");
746           if (ConstantSDNode *CS=dyn_cast<ConstantSDNode>(Node->getOperand(i))){
747             MI->addImmOperand(CS->getValue());
748           } else {
749             GlobalAddressSDNode *GA = 
750               cast<GlobalAddressSDNode>(Node->getOperand(i));
751             MI->addGlobalAddressOperand(GA->getGlobal(), GA->getOffset());
752           }
753           ++i;
754           break;
755         }
756         case 4:  // Addressing mode.
757           // The addressing mode has been selected, just add all of the
758           // operands to the machine instruction.
759           for (; NumVals; --NumVals, ++i)
760             AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
761           break;
762         }
763       }
764       break;
765     }
766     }
767   }
768 }
769
770 void ScheduleDAG::EmitNoop() {
771   TII->insertNoop(*BB, BB->end());
772 }
773
774 /// EmitSchedule - Emit the machine code in scheduled order.
775 void ScheduleDAG::EmitSchedule() {
776   // If this is the first basic block in the function, and if it has live ins
777   // that need to be copied into vregs, emit the copies into the top of the
778   // block before emitting the code for the block.
779   MachineFunction &MF = DAG.getMachineFunction();
780   if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
781     for (MachineFunction::livein_iterator LI = MF.livein_begin(),
782          E = MF.livein_end(); LI != E; ++LI)
783       if (LI->second)
784         MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
785                           LI->first, RegMap->getRegClass(LI->second));
786   }
787   
788   
789   // Finally, emit the code for all of the scheduled instructions.
790   DenseMap<SDOperand, unsigned> VRBaseMap;
791   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
792     if (SUnit *SU = Sequence[i]) {
793       for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++)
794         EmitNode(SU->FlaggedNodes[j], VRBaseMap);
795       EmitNode(SU->Node, VRBaseMap);
796     } else {
797       // Null SUnit* is a noop.
798       EmitNoop();
799     }
800   }
801 }
802
803 /// dump - dump the schedule.
804 void ScheduleDAG::dumpSchedule() const {
805   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
806     if (SUnit *SU = Sequence[i])
807       SU->dump(&DAG);
808     else
809       cerr << "**** NOOP ****\n";
810   }
811 }
812
813
814 /// Run - perform scheduling.
815 ///
816 MachineBasicBlock *ScheduleDAG::Run() {
817   TII = TM.getInstrInfo();
818   MRI = TM.getRegisterInfo();
819   RegMap = BB->getParent()->getSSARegMap();
820   ConstPool = BB->getParent()->getConstantPool();
821
822   Schedule();
823   return BB;
824 }
825
826 /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
827 /// a group of nodes flagged together.
828 void SUnit::dump(const SelectionDAG *G) const {
829   cerr << "SU(" << NodeNum << "): ";
830   Node->dump(G);
831   cerr << "\n";
832   if (FlaggedNodes.size() != 0) {
833     for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
834       cerr << "    ";
835       FlaggedNodes[i]->dump(G);
836       cerr << "\n";
837     }
838   }
839 }
840
841 void SUnit::dumpAll(const SelectionDAG *G) const {
842   dump(G);
843
844   cerr << "  # preds left       : " << NumPredsLeft << "\n";
845   cerr << "  # succs left       : " << NumSuccsLeft << "\n";
846   cerr << "  # chain preds left : " << NumChainPredsLeft << "\n";
847   cerr << "  # chain succs left : " << NumChainSuccsLeft << "\n";
848   cerr << "  Latency            : " << Latency << "\n";
849   cerr << "  Depth              : " << Depth << "\n";
850   cerr << "  Height             : " << Height << "\n";
851
852   if (Preds.size() != 0) {
853     cerr << "  Predecessors:\n";
854     for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
855          I != E; ++I) {
856       if (I->second)
857         cerr << "   ch  #";
858       else
859         cerr << "   val #";
860       cerr << I->first << " - SU(" << I->first->NodeNum << ")\n";
861     }
862   }
863   if (Succs.size() != 0) {
864     cerr << "  Successors:\n";
865     for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
866          I != E; ++I) {
867       if (I->second)
868         cerr << "   ch  #";
869       else
870         cerr << "   val #";
871       cerr << I->first << " - SU(" << I->first->NodeNum << ")\n";
872     }
873   }
874   cerr << "\n";
875 }