Use getDebugLoc forwarder instead of getNode()->getDebugLoc.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / ScheduleDAGSDNodesEmit.cpp
1 //===---- ScheduleDAGEmit.cpp - Emit routines for the ScheduleDAG class ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the Emit routines for the ScheduleDAG class, which creates
11 // MachineInstrs according to the computed schedule.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "pre-RA-sched"
16 #include "ScheduleDAGSDNodes.h"
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.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/ADT/Statistic.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/MathExtras.h"
29 using namespace llvm;
30
31 /// getInstrOperandRegClass - Return register class of the operand of an
32 /// instruction of the specified TargetInstrDesc.
33 static const TargetRegisterClass*
34 getInstrOperandRegClass(const TargetRegisterInfo *TRI, 
35                         const TargetInstrDesc &II, unsigned Op) {
36   if (Op >= II.getNumOperands()) {
37     assert(II.isVariadic() && "Invalid operand # of instruction");
38     return NULL;
39   }
40   if (II.OpInfo[Op].isLookupPtrRegClass())
41     return TRI->getPointerRegClass();
42   return TRI->getRegClass(II.OpInfo[Op].RegClass);
43 }
44
45 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
46 /// implicit physical register output.
47 void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
48                                          bool IsClone, bool IsCloned,
49                                          unsigned SrcReg,
50                                          DenseMap<SDValue, unsigned> &VRBaseMap) {
51   unsigned VRBase = 0;
52   if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
53     // Just use the input register directly!
54     SDValue Op(Node, ResNo);
55     if (IsClone)
56       VRBaseMap.erase(Op);
57     bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
58     isNew = isNew; // Silence compiler warning.
59     assert(isNew && "Node emitted out of order - early");
60     return;
61   }
62
63   // If the node is only used by a CopyToReg and the dest reg is a vreg, use
64   // the CopyToReg'd destination register instead of creating a new vreg.
65   bool MatchReg = true;
66   const TargetRegisterClass *UseRC = NULL;
67   if (!IsClone && !IsCloned)
68     for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
69          UI != E; ++UI) {
70       SDNode *User = *UI;
71       bool Match = true;
72       if (User->getOpcode() == ISD::CopyToReg && 
73           User->getOperand(2).getNode() == Node &&
74           User->getOperand(2).getResNo() == ResNo) {
75         unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
76         if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
77           VRBase = DestReg;
78           Match = false;
79         } else if (DestReg != SrcReg)
80           Match = false;
81       } else {
82         for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
83           SDValue Op = User->getOperand(i);
84           if (Op.getNode() != Node || Op.getResNo() != ResNo)
85             continue;
86           MVT VT = Node->getValueType(Op.getResNo());
87           if (VT == MVT::Other || VT == MVT::Flag)
88             continue;
89           Match = false;
90           if (User->isMachineOpcode()) {
91             const TargetInstrDesc &II = TII->get(User->getMachineOpcode());
92             const TargetRegisterClass *RC =
93               getInstrOperandRegClass(TRI, II, i+II.getNumDefs());
94             if (!UseRC)
95               UseRC = RC;
96             else if (RC)
97               assert(UseRC == RC &&
98                      "Multiple uses expecting different register classes!");
99           }
100         }
101       }
102       MatchReg &= Match;
103       if (VRBase)
104         break;
105     }
106
107   MVT VT = Node->getValueType(ResNo);
108   const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
109   SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT);
110   
111   // Figure out the register class to create for the destreg.
112   if (VRBase) {
113     DstRC = MRI.getRegClass(VRBase);
114   } else if (UseRC) {
115     assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
116     DstRC = UseRC;
117   } else {
118     DstRC = TLI->getRegClassFor(VT);
119   }
120     
121   // If all uses are reading from the src physical register and copying the
122   // register is either impossible or very expensive, then don't create a copy.
123   if (MatchReg && SrcRC->getCopyCost() < 0) {
124     VRBase = SrcReg;
125   } else {
126     // Create the reg, emit the copy.
127     VRBase = MRI.createVirtualRegister(DstRC);
128     bool Emitted =
129       TII->copyRegToReg(*BB, End, VRBase, SrcReg, DstRC, SrcRC);
130     Emitted = Emitted; // Silence compiler warning.
131     assert(Emitted && "Unable to issue a copy instruction!");
132   }
133
134   SDValue Op(Node, ResNo);
135   if (IsClone)
136     VRBaseMap.erase(Op);
137   bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
138   isNew = isNew; // Silence compiler warning.
139   assert(isNew && "Node emitted out of order - early");
140 }
141
142 /// getDstOfCopyToRegUse - If the only use of the specified result number of
143 /// node is a CopyToReg, return its destination register. Return 0 otherwise.
144 unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
145                                                       unsigned ResNo) const {
146   if (!Node->hasOneUse())
147     return 0;
148
149   SDNode *User = *Node->use_begin();
150   if (User->getOpcode() == ISD::CopyToReg && 
151       User->getOperand(2).getNode() == Node &&
152       User->getOperand(2).getResNo() == ResNo) {
153     unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
154     if (TargetRegisterInfo::isVirtualRegister(Reg))
155       return Reg;
156   }
157   return 0;
158 }
159
160 void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
161                                        const TargetInstrDesc &II,
162                                        bool IsClone, bool IsCloned,
163                                        DenseMap<SDValue, unsigned> &VRBaseMap) {
164   assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
165          "IMPLICIT_DEF should have been handled as a special case elsewhere!");
166
167   for (unsigned i = 0; i < II.getNumDefs(); ++i) {
168     // If the specific node value is only used by a CopyToReg and the dest reg
169     // is a vreg, use the CopyToReg'd destination register instead of creating
170     // a new vreg.
171     unsigned VRBase = 0;
172
173     if (!IsClone && !IsCloned)
174       for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
175            UI != E; ++UI) {
176         SDNode *User = *UI;
177         if (User->getOpcode() == ISD::CopyToReg && 
178             User->getOperand(2).getNode() == Node &&
179             User->getOperand(2).getResNo() == i) {
180           unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
181           if (TargetRegisterInfo::isVirtualRegister(Reg)) {
182             VRBase = Reg;
183             MI->addOperand(MachineOperand::CreateReg(Reg, true));
184             break;
185           }
186         }
187       }
188
189     // Create the result registers for this node and add the result regs to
190     // the machine instruction.
191     if (VRBase == 0) {
192       const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, II, i);
193       assert(RC && "Isn't a register operand!");
194       VRBase = MRI.createVirtualRegister(RC);
195       MI->addOperand(MachineOperand::CreateReg(VRBase, true));
196     }
197
198     SDValue Op(Node, i);
199     if (IsClone)
200       VRBaseMap.erase(Op);
201     bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
202     isNew = isNew; // Silence compiler warning.
203     assert(isNew && "Node emitted out of order - early");
204   }
205 }
206
207 /// getVR - Return the virtual register corresponding to the specified result
208 /// of the specified node.
209 unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
210                                    DenseMap<SDValue, unsigned> &VRBaseMap) {
211   if (Op.isMachineOpcode() &&
212       Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
213     // Add an IMPLICIT_DEF instruction before every use.
214     unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
215     // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
216     // does not include operand register class info.
217     if (!VReg) {
218       const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
219       VReg = MRI.createVirtualRegister(RC);
220     }
221     BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg);
222     return VReg;
223   }
224
225   DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
226   assert(I != VRBaseMap.end() && "Node emitted out of order - late");
227   return I->second;
228 }
229
230
231 /// AddOperand - Add the specified operand to the specified machine instr.  II
232 /// specifies the instruction information for the node, and IIOpNum is the
233 /// operand number (in the II) that we are adding. IIOpNum and II are used for 
234 /// assertions only.
235 void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
236                                     unsigned IIOpNum,
237                                     const TargetInstrDesc *II,
238                                     DenseMap<SDValue, unsigned> &VRBaseMap) {
239   if (Op.isMachineOpcode()) {
240     // Note that this case is redundant with the final else block, but we
241     // include it because it is the most common and it makes the logic
242     // simpler here.
243     assert(Op.getValueType() != MVT::Other &&
244            Op.getValueType() != MVT::Flag &&
245            "Chain and flag operands should occur at end of operand list!");
246     // Get/emit the operand.
247     unsigned VReg = getVR(Op, VRBaseMap);
248     const TargetInstrDesc &TID = MI->getDesc();
249     bool isOptDef = IIOpNum < TID.getNumOperands() &&
250       TID.OpInfo[IIOpNum].isOptionalDef();
251     MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
252     
253     // Verify that it is right.
254     assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
255 #ifndef NDEBUG
256     if (II) {
257       // There may be no register class for this operand if it is a variadic
258       // argument (RC will be NULL in this case).  In this case, we just assume
259       // the regclass is ok.
260       const TargetRegisterClass *RC= getInstrOperandRegClass(TRI, *II, IIOpNum);
261       assert((RC || II->isVariadic()) && "Expected reg class info!");
262       const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
263       if (RC && VRC != RC) {
264         cerr << "Register class of operand and regclass of use don't agree!\n";
265         cerr << "Operand = " << IIOpNum << "\n";
266         cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n";
267         cerr << "MI = "; MI->print(cerr);
268         cerr << "VReg = " << VReg << "\n";
269         cerr << "VReg RegClass     size = " << VRC->getSize()
270              << ", align = " << VRC->getAlignment() << "\n";
271         cerr << "Expected RegClass size = " << RC->getSize()
272              << ", align = " << RC->getAlignment() << "\n";
273         cerr << "Fatal error, aborting.\n";
274         abort();
275       }
276     }
277 #endif
278   } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
279     MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
280   } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
281     const ConstantFP *CFP = F->getConstantFPValue();
282     MI->addOperand(MachineOperand::CreateFPImm(CFP));
283   } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
284     MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
285   } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
286     MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
287   } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
288     MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
289   } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
290     MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
291   } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
292     MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
293   } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
294     int Offset = CP->getOffset();
295     unsigned Align = CP->getAlignment();
296     const Type *Type = CP->getType();
297     // MachineConstantPool wants an explicit alignment.
298     if (Align == 0) {
299       Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
300       if (Align == 0) {
301         // Alignment of vector types.  FIXME!
302         Align = TM.getTargetData()->getTypePaddedSize(Type);
303         Align = Log2_64(Align);
304       }
305     }
306     
307     unsigned Idx;
308     if (CP->isMachineConstantPoolEntry())
309       Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
310     else
311       Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
312     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
313   } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
314     MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
315   } else {
316     assert(Op.getValueType() != MVT::Other &&
317            Op.getValueType() != MVT::Flag &&
318            "Chain and flag operands should occur at end of operand list!");
319     unsigned VReg = getVR(Op, VRBaseMap);
320     MI->addOperand(MachineOperand::CreateReg(VReg, false));
321     
322     // Verify that it is right.  Note that the reg class of the physreg and the
323     // vreg don't necessarily need to match, but the target copy insertion has
324     // to be able to handle it.  This handles things like copies from ST(0) to
325     // an FP vreg on x86.
326     assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
327     if (II && !II->isVariadic()) {
328       assert(getInstrOperandRegClass(TRI, *II, IIOpNum) &&
329              "Don't have operand info for this instruction!");
330     }
331   }  
332 }
333
334 /// EmitSubregNode - Generate machine code for subreg nodes.
335 ///
336 void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node, 
337                                         DenseMap<SDValue, unsigned> &VRBaseMap) {
338   unsigned VRBase = 0;
339   unsigned Opc = Node->getMachineOpcode();
340   
341   // If the node is only used by a CopyToReg and the dest reg is a vreg, use
342   // the CopyToReg'd destination register instead of creating a new vreg.
343   for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
344        UI != E; ++UI) {
345     SDNode *User = *UI;
346     if (User->getOpcode() == ISD::CopyToReg && 
347         User->getOperand(2).getNode() == Node) {
348       unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
349       if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
350         VRBase = DestReg;
351         break;
352       }
353     }
354   }
355   
356   if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
357     unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
358
359     // Create the extract_subreg machine instruction.
360     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
361                                TII->get(TargetInstrInfo::EXTRACT_SUBREG));
362
363     // Figure out the register class to create for the destreg.
364     const TargetRegisterClass *SRC = TLI->getRegClassFor(Node->getValueType(0));
365
366     if (VRBase) {
367       // Grab the destination register
368 #ifndef NDEBUG
369       const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
370       assert(SRC && DRC && SRC == DRC && 
371              "Source subregister and destination must have the same class");
372 #endif
373     } else {
374       // Create the reg
375       assert(SRC && "Couldn't find source register class");
376       VRBase = MRI.createVirtualRegister(SRC);
377     }
378     
379     // Add def, source, and subreg index
380     MI->addOperand(MachineOperand::CreateReg(VRBase, true));
381     AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
382     MI->addOperand(MachineOperand::CreateImm(SubIdx));
383     BB->insert(End, MI);
384   } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
385              Opc == TargetInstrInfo::SUBREG_TO_REG) {
386     SDValue N0 = Node->getOperand(0);
387     SDValue N1 = Node->getOperand(1);
388     SDValue N2 = Node->getOperand(2);
389     unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
390     
391       
392     // Figure out the register class to create for the destreg.
393     const TargetRegisterClass *TRC = 0;
394     if (VRBase) {
395       TRC = MRI.getRegClass(VRBase);
396     } else {
397       TRC = TLI->getRegClassFor(Node->getValueType(0));
398       assert(TRC && "Couldn't determine register class for insert_subreg");
399       VRBase = MRI.createVirtualRegister(TRC); // Create the reg
400     }
401     
402     // Create the insert_subreg or subreg_to_reg machine instruction.
403     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
404     MI->addOperand(MachineOperand::CreateReg(VRBase, true));
405     
406     // If creating a subreg_to_reg, then the first input operand
407     // is an implicit value immediate, otherwise it's a register
408     if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
409       const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
410       MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
411     } else
412       AddOperand(MI, N0, 0, 0, VRBaseMap);
413     // Add the subregster being inserted
414     AddOperand(MI, N1, 0, 0, VRBaseMap);
415     MI->addOperand(MachineOperand::CreateImm(SubIdx));
416     BB->insert(End, MI);
417   } else
418     assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
419      
420   SDValue Op(Node, 0);
421   bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
422   isNew = isNew; // Silence compiler warning.
423   assert(isNew && "Node emitted out of order - early");
424 }
425
426 /// EmitNode - Generate machine code for an node and needed dependencies.
427 ///
428 void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
429                                   DenseMap<SDValue, unsigned> &VRBaseMap) {
430   // If machine instruction
431   if (Node->isMachineOpcode()) {
432     unsigned Opc = Node->getMachineOpcode();
433     
434     // Handle subreg insert/extract specially
435     if (Opc == TargetInstrInfo::EXTRACT_SUBREG || 
436         Opc == TargetInstrInfo::INSERT_SUBREG ||
437         Opc == TargetInstrInfo::SUBREG_TO_REG) {
438       EmitSubregNode(Node, VRBaseMap);
439       return;
440     }
441
442     if (Opc == TargetInstrInfo::IMPLICIT_DEF)
443       // We want a unique VR for each IMPLICIT_DEF use.
444       return;
445     
446     const TargetInstrDesc &II = TII->get(Opc);
447     unsigned NumResults = CountResults(Node);
448     unsigned NodeOperands = CountOperands(Node);
449     unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
450     bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
451                           II.getImplicitDefs() != 0;
452 #ifndef NDEBUG
453     unsigned NumMIOperands = NodeOperands + NumResults;
454     assert((II.getNumOperands() == NumMIOperands ||
455             HasPhysRegOuts || II.isVariadic()) &&
456            "#operands for dag node doesn't match .td file!"); 
457 #endif
458
459     // Create the new machine instruction.
460     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
461     
462     // Add result register values for things that are defined by this
463     // instruction.
464     if (NumResults)
465       CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
466     
467     // Emit all of the actual operands of this instruction, adding them to the
468     // instruction as appropriate.
469     for (unsigned i = 0; i != NodeOperands; ++i)
470       AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
471
472     // Emit all of the memory operands of this instruction
473     for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
474       AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
475
476     if (II.usesCustomDAGSchedInsertionHook()) {
477       // Insert this instruction into the basic block using a target
478       // specific inserter which may returns a new basic block.
479       BB = TLI->EmitInstrWithCustomInserter(MI, BB);
480       Begin = End = BB->end();
481     } else {
482       BB->insert(End, MI);
483     }
484
485     // Additional results must be an physical register def.
486     if (HasPhysRegOuts) {
487       for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
488         unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
489         if (Node->hasAnyUseOfValue(i))
490           EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
491       }
492     }
493     return;
494   }
495
496   switch (Node->getOpcode()) {
497   default:
498 #ifndef NDEBUG
499     Node->dump(DAG);
500 #endif
501     assert(0 && "This target-independent node should have been selected!");
502     break;
503   case ISD::EntryToken:
504     assert(0 && "EntryToken should have been excluded from the schedule!");
505     break;
506   case ISD::TokenFactor: // fall thru
507     break;
508   case ISD::CopyToReg: {
509     unsigned SrcReg;
510     SDValue SrcVal = Node->getOperand(2);
511     if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
512       SrcReg = R->getReg();
513     else
514       SrcReg = getVR(SrcVal, VRBaseMap);
515       
516     unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
517     if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
518       break;
519       
520     const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
521     // Get the register classes of the src/dst.
522     if (TargetRegisterInfo::isVirtualRegister(SrcReg))
523       SrcTRC = MRI.getRegClass(SrcReg);
524     else
525       SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
526
527     if (TargetRegisterInfo::isVirtualRegister(DestReg))
528       DstTRC = MRI.getRegClass(DestReg);
529     else
530       DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
531                                             Node->getOperand(1).getValueType());
532     TII->copyRegToReg(*BB, End, DestReg, SrcReg, DstTRC, SrcTRC);
533     break;
534   }
535   case ISD::CopyFromReg: {
536     unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
537     EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
538     break;
539   }
540   case ISD::INLINEASM: {
541     unsigned NumOps = Node->getNumOperands();
542     if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
543       --NumOps;  // Ignore the flag operand.
544       
545     // Create the inline asm machine instruction.
546     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
547                                TII->get(TargetInstrInfo::INLINEASM));
548
549     // Add the asm string as an external symbol operand.
550     const char *AsmStr =
551       cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
552     MI->addOperand(MachineOperand::CreateES(AsmStr));
553       
554     // Add all of the operand registers to the instruction.
555     for (unsigned i = 2; i != NumOps;) {
556       unsigned Flags =
557         cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
558       unsigned NumVals = Flags >> 3;
559         
560       MI->addOperand(MachineOperand::CreateImm(Flags));
561       ++i;  // Skip the ID value.
562         
563       switch (Flags & 7) {
564       default: assert(0 && "Bad flags!");
565       case 2:   // Def of register.
566         for (; NumVals; --NumVals, ++i) {
567           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
568           MI->addOperand(MachineOperand::CreateReg(Reg, true));
569         }
570         break;
571       case 6:   // Def of earlyclobber register.
572         for (; NumVals; --NumVals, ++i) {
573           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
574           MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false, 
575                                                    false, 0, true));
576         }
577         break;
578       case 1:  // Use of register.
579       case 3:  // Immediate.
580       case 4:  // Addressing mode.
581         // The addressing mode has been selected, just add all of the
582         // operands to the machine instruction.
583         for (; NumVals; --NumVals, ++i)
584           AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
585         break;
586       }
587     }
588     BB->insert(End, MI);
589     break;
590   }
591   }
592 }
593
594 /// EmitSchedule - Emit the machine code in scheduled order.
595 MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
596   DenseMap<SDValue, unsigned> VRBaseMap;
597   DenseMap<SUnit*, unsigned> CopyVRBaseMap;
598   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
599     SUnit *SU = Sequence[i];
600     if (!SU) {
601       // Null SUnit* is a noop.
602       EmitNoop();
603       continue;
604     }
605
606     // For pre-regalloc scheduling, create instructions corresponding to the
607     // SDNode and any flagged SDNodes and append them to the block.
608     if (!SU->getNode()) {
609       // Emit a copy.
610       EmitPhysRegCopy(SU, CopyVRBaseMap);
611       continue;
612     }
613
614     SmallVector<SDNode *, 4> FlaggedNodes;
615     for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
616          N = N->getFlaggedNode())
617       FlaggedNodes.push_back(N);
618     while (!FlaggedNodes.empty()) {
619       EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
620       FlaggedNodes.pop_back();
621     }
622     EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
623   }
624
625   return BB;
626 }