LLVM API Documentation

ScheduleDAGSDNodesEmit.cpp

Go to the documentation of this file.
00001 //===---- ScheduleDAGEmit.cpp - Emit routines for the ScheduleDAG class ---===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This implements the Emit routines for the ScheduleDAG class, which creates
00011 // MachineInstrs according to the computed schedule.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "pre-RA-sched"
00016 #include "llvm/CodeGen/ScheduleDAGSDNodes.h"
00017 #include "llvm/CodeGen/MachineConstantPool.h"
00018 #include "llvm/CodeGen/MachineFunction.h"
00019 #include "llvm/CodeGen/MachineInstrBuilder.h"
00020 #include "llvm/CodeGen/MachineRegisterInfo.h"
00021 #include "llvm/Target/TargetData.h"
00022 #include "llvm/Target/TargetMachine.h"
00023 #include "llvm/Target/TargetInstrInfo.h"
00024 #include "llvm/Target/TargetLowering.h"
00025 #include "llvm/ADT/Statistic.h"
00026 #include "llvm/Support/CommandLine.h"
00027 #include "llvm/Support/Debug.h"
00028 #include "llvm/Support/MathExtras.h"
00029 using namespace llvm;
00030 
00031 /// getInstrOperandRegClass - Return register class of the operand of an
00032 /// instruction of the specified TargetInstrDesc.
00033 static const TargetRegisterClass*
00034 getInstrOperandRegClass(const TargetRegisterInfo *TRI, 
00035                         const TargetInstrInfo *TII, const TargetInstrDesc &II,
00036                         unsigned Op) {
00037   if (Op >= II.getNumOperands()) {
00038     assert(II.isVariadic() && "Invalid operand # of instruction");
00039     return NULL;
00040   }
00041   if (II.OpInfo[Op].isLookupPtrRegClass())
00042     return TII->getPointerRegClass();
00043   return TRI->getRegClass(II.OpInfo[Op].RegClass);
00044 }
00045 
00046 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
00047 /// implicit physical register output.
00048 void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
00049                                          bool IsClone, bool IsCloned,
00050                                          unsigned SrcReg,
00051                                          DenseMap<SDValue, unsigned> &VRBaseMap) {
00052   unsigned VRBase = 0;
00053   if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
00054     // Just use the input register directly!
00055     SDValue Op(Node, ResNo);
00056     if (IsClone)
00057       VRBaseMap.erase(Op);
00058     bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
00059     isNew = isNew; // Silence compiler warning.
00060     assert(isNew && "Node emitted out of order - early");
00061     return;
00062   }
00063 
00064   // If the node is only used by a CopyToReg and the dest reg is a vreg, use
00065   // the CopyToReg'd destination register instead of creating a new vreg.
00066   bool MatchReg = true;
00067   const TargetRegisterClass *UseRC = NULL;
00068   if (!IsClone && !IsCloned)
00069     for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
00070          UI != E; ++UI) {
00071       SDNode *User = *UI;
00072       bool Match = true;
00073       if (User->getOpcode() == ISD::CopyToReg && 
00074           User->getOperand(2).getNode() == Node &&
00075           User->getOperand(2).getResNo() == ResNo) {
00076         unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00077         if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
00078           VRBase = DestReg;
00079           Match = false;
00080         } else if (DestReg != SrcReg)
00081           Match = false;
00082       } else {
00083         for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
00084           SDValue Op = User->getOperand(i);
00085           if (Op.getNode() != Node || Op.getResNo() != ResNo)
00086             continue;
00087           MVT VT = Node->getValueType(Op.getResNo());
00088           if (VT == MVT::Other || VT == MVT::Flag)
00089             continue;
00090           Match = false;
00091           if (User->isMachineOpcode()) {
00092             const TargetInstrDesc &II = TII->get(User->getMachineOpcode());
00093             const TargetRegisterClass *RC =
00094               getInstrOperandRegClass(TRI,TII,II,i+II.getNumDefs());
00095             if (!UseRC)
00096               UseRC = RC;
00097             else if (RC)
00098               assert(UseRC == RC &&
00099                      "Multiple uses expecting different register classes!");
00100           }
00101         }
00102       }
00103       MatchReg &= Match;
00104       if (VRBase)
00105         break;
00106     }
00107 
00108   MVT VT = Node->getValueType(ResNo);
00109   const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
00110   SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT);
00111   
00112   // Figure out the register class to create for the destreg.
00113   if (VRBase) {
00114     DstRC = MRI.getRegClass(VRBase);
00115   } else if (UseRC) {
00116     assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
00117     DstRC = UseRC;
00118   } else {
00119     DstRC = TLI->getRegClassFor(VT);
00120   }
00121     
00122   // If all uses are reading from the src physical register and copying the
00123   // register is either impossible or very expensive, then don't create a copy.
00124   if (MatchReg && SrcRC->getCopyCost() < 0) {
00125     VRBase = SrcReg;
00126   } else {
00127     // Create the reg, emit the copy.
00128     VRBase = MRI.createVirtualRegister(DstRC);
00129     bool Emitted =
00130       TII->copyRegToReg(*BB, End, VRBase, SrcReg, DstRC, SrcRC);
00131     Emitted = Emitted; // Silence compiler warning.
00132     assert(Emitted && "Unable to issue a copy instruction!");
00133   }
00134 
00135   SDValue Op(Node, ResNo);
00136   if (IsClone)
00137     VRBaseMap.erase(Op);
00138   bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
00139   isNew = isNew; // Silence compiler warning.
00140   assert(isNew && "Node emitted out of order - early");
00141 }
00142 
00143 /// getDstOfCopyToRegUse - If the only use of the specified result number of
00144 /// node is a CopyToReg, return its destination register. Return 0 otherwise.
00145 unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
00146                                                       unsigned ResNo) const {
00147   if (!Node->hasOneUse())
00148     return 0;
00149 
00150   SDNode *User = *Node->use_begin();
00151   if (User->getOpcode() == ISD::CopyToReg && 
00152       User->getOperand(2).getNode() == Node &&
00153       User->getOperand(2).getResNo() == ResNo) {
00154     unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00155     if (TargetRegisterInfo::isVirtualRegister(Reg))
00156       return Reg;
00157   }
00158   return 0;
00159 }
00160 
00161 void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
00162                                        const TargetInstrDesc &II,
00163                                        bool IsClone, bool IsCloned,
00164                                        DenseMap<SDValue, unsigned> &VRBaseMap) {
00165   assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
00166          "IMPLICIT_DEF should have been handled as a special case elsewhere!");
00167 
00168   for (unsigned i = 0; i < II.getNumDefs(); ++i) {
00169     // If the specific node value is only used by a CopyToReg and the dest reg
00170     // is a vreg, use the CopyToReg'd destination register instead of creating
00171     // a new vreg.
00172     unsigned VRBase = 0;
00173 
00174     if (!IsClone && !IsCloned)
00175       for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
00176            UI != E; ++UI) {
00177         SDNode *User = *UI;
00178         if (User->getOpcode() == ISD::CopyToReg && 
00179             User->getOperand(2).getNode() == Node &&
00180             User->getOperand(2).getResNo() == i) {
00181           unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00182           if (TargetRegisterInfo::isVirtualRegister(Reg)) {
00183             VRBase = Reg;
00184             MI->addOperand(MachineOperand::CreateReg(Reg, true));
00185             break;
00186           }
00187         }
00188       }
00189 
00190     // Create the result registers for this node and add the result regs to
00191     // the machine instruction.
00192     if (VRBase == 0) {
00193       const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
00194       assert(RC && "Isn't a register operand!");
00195       VRBase = MRI.createVirtualRegister(RC);
00196       MI->addOperand(MachineOperand::CreateReg(VRBase, true));
00197     }
00198 
00199     SDValue Op(Node, i);
00200     if (IsClone)
00201       VRBaseMap.erase(Op);
00202     bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
00203     isNew = isNew; // Silence compiler warning.
00204     assert(isNew && "Node emitted out of order - early");
00205   }
00206 }
00207 
00208 /// getVR - Return the virtual register corresponding to the specified result
00209 /// of the specified node.
00210 unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
00211                                    DenseMap<SDValue, unsigned> &VRBaseMap) {
00212   if (Op.isMachineOpcode() &&
00213       Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
00214     // Add an IMPLICIT_DEF instruction before every use.
00215     unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
00216     // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
00217     // does not include operand register class info.
00218     if (!VReg) {
00219       const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
00220       VReg = MRI.createVirtualRegister(RC);
00221     }
00222     BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg);
00223     return VReg;
00224   }
00225 
00226   DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
00227   assert(I != VRBaseMap.end() && "Node emitted out of order - late");
00228   return I->second;
00229 }
00230 
00231 
00232 /// AddOperand - Add the specified operand to the specified machine instr.  II
00233 /// specifies the instruction information for the node, and IIOpNum is the
00234 /// operand number (in the II) that we are adding. IIOpNum and II are used for 
00235 /// assertions only.
00236 void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
00237                                     unsigned IIOpNum,
00238                                     const TargetInstrDesc *II,
00239                                     DenseMap<SDValue, unsigned> &VRBaseMap) {
00240   if (Op.isMachineOpcode()) {
00241     // Note that this case is redundant with the final else block, but we
00242     // include it because it is the most common and it makes the logic
00243     // simpler here.
00244     assert(Op.getValueType() != MVT::Other &&
00245            Op.getValueType() != MVT::Flag &&
00246            "Chain and flag operands should occur at end of operand list!");
00247     // Get/emit the operand.
00248     unsigned VReg = getVR(Op, VRBaseMap);
00249     const TargetInstrDesc &TID = MI->getDesc();
00250     bool isOptDef = IIOpNum < TID.getNumOperands() &&
00251       TID.OpInfo[IIOpNum].isOptionalDef();
00252     MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
00253     
00254     // Verify that it is right.
00255     assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
00256 #ifndef NDEBUG
00257     if (II) {
00258       // There may be no register class for this operand if it is a variadic
00259       // argument (RC will be NULL in this case).  In this case, we just assume
00260       // the regclass is ok.
00261       const TargetRegisterClass *RC =
00262                           getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
00263       assert((RC || II->isVariadic()) && "Expected reg class info!");
00264       const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
00265       if (RC && VRC != RC) {
00266         cerr << "Register class of operand and regclass of use don't agree!\n";
00267         cerr << "Operand = " << IIOpNum << "\n";
00268         cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n";
00269         cerr << "MI = "; MI->print(cerr);
00270         cerr << "VReg = " << VReg << "\n";
00271         cerr << "VReg RegClass     size = " << VRC->getSize()
00272              << ", align = " << VRC->getAlignment() << "\n";
00273         cerr << "Expected RegClass size = " << RC->getSize()
00274              << ", align = " << RC->getAlignment() << "\n";
00275         cerr << "Fatal error, aborting.\n";
00276         abort();
00277       }
00278     }
00279 #endif
00280   } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
00281     MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
00282   } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
00283     const ConstantFP *CFP = F->getConstantFPValue();
00284     MI->addOperand(MachineOperand::CreateFPImm(CFP));
00285   } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
00286     MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
00287   } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
00288     MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
00289   } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
00290     MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
00291   } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
00292     MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
00293   } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
00294     MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
00295   } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
00296     int Offset = CP->getOffset();
00297     unsigned Align = CP->getAlignment();
00298     const Type *Type = CP->getType();
00299     // MachineConstantPool wants an explicit alignment.
00300     if (Align == 0) {
00301       Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
00302       if (Align == 0) {
00303         // Alignment of vector types.  FIXME!
00304         Align = TM.getTargetData()->getTypePaddedSize(Type);
00305         Align = Log2_64(Align);
00306       }
00307     }
00308     
00309     unsigned Idx;
00310     if (CP->isMachineConstantPoolEntry())
00311       Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
00312     else
00313       Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
00314     MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
00315   } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
00316     MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
00317   } else {
00318     assert(Op.getValueType() != MVT::Other &&
00319            Op.getValueType() != MVT::Flag &&
00320            "Chain and flag operands should occur at end of operand list!");
00321     unsigned VReg = getVR(Op, VRBaseMap);
00322     MI->addOperand(MachineOperand::CreateReg(VReg, false));
00323     
00324     // Verify that it is right.  Note that the reg class of the physreg and the
00325     // vreg don't necessarily need to match, but the target copy insertion has
00326     // to be able to handle it.  This handles things like copies from ST(0) to
00327     // an FP vreg on x86.
00328     assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
00329     if (II && !II->isVariadic()) {
00330       assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
00331              "Don't have operand info for this instruction!");
00332     }
00333   }  
00334 }
00335 
00336 /// EmitSubregNode - Generate machine code for subreg nodes.
00337 ///
00338 void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node, 
00339                                         DenseMap<SDValue, unsigned> &VRBaseMap) {
00340   unsigned VRBase = 0;
00341   unsigned Opc = Node->getMachineOpcode();
00342   
00343   // If the node is only used by a CopyToReg and the dest reg is a vreg, use
00344   // the CopyToReg'd destination register instead of creating a new vreg.
00345   for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
00346        UI != E; ++UI) {
00347     SDNode *User = *UI;
00348     if (User->getOpcode() == ISD::CopyToReg && 
00349         User->getOperand(2).getNode() == Node) {
00350       unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00351       if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
00352         VRBase = DestReg;
00353         break;
00354       }
00355     }
00356   }
00357   
00358   if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
00359     unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
00360 
00361     // Create the extract_subreg machine instruction.
00362     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
00363                                TII->get(TargetInstrInfo::EXTRACT_SUBREG));
00364 
00365     // Figure out the register class to create for the destreg.
00366     const TargetRegisterClass *SRC = TLI->getRegClassFor(Node->getValueType(0));
00367 
00368     if (VRBase) {
00369       // Grab the destination register
00370 #ifndef NDEBUG
00371       const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
00372       assert(SRC && DRC && SRC == DRC && 
00373              "Source subregister and destination must have the same class");
00374 #endif
00375     } else {
00376       // Create the reg
00377       assert(SRC && "Couldn't find source register class");
00378       VRBase = MRI.createVirtualRegister(SRC);
00379     }
00380     
00381     // Add def, source, and subreg index
00382     MI->addOperand(MachineOperand::CreateReg(VRBase, true));
00383     AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
00384     MI->addOperand(MachineOperand::CreateImm(SubIdx));
00385     BB->insert(End, MI);
00386   } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
00387              Opc == TargetInstrInfo::SUBREG_TO_REG) {
00388     SDValue N0 = Node->getOperand(0);
00389     SDValue N1 = Node->getOperand(1);
00390     SDValue N2 = Node->getOperand(2);
00391     unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
00392     
00393       
00394     // Figure out the register class to create for the destreg.
00395     const TargetRegisterClass *TRC = 0;
00396     if (VRBase) {
00397       TRC = MRI.getRegClass(VRBase);
00398     } else {
00399       TRC = TLI->getRegClassFor(Node->getValueType(0));
00400       assert(TRC && "Couldn't determine register class for insert_subreg");
00401       VRBase = MRI.createVirtualRegister(TRC); // Create the reg
00402     }
00403     
00404     // Create the insert_subreg or subreg_to_reg machine instruction.
00405     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
00406     MI->addOperand(MachineOperand::CreateReg(VRBase, true));
00407     
00408     // If creating a subreg_to_reg, then the first input operand
00409     // is an implicit value immediate, otherwise it's a register
00410     if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
00411       const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
00412       MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
00413     } else
00414       AddOperand(MI, N0, 0, 0, VRBaseMap);
00415     // Add the subregster being inserted
00416     AddOperand(MI, N1, 0, 0, VRBaseMap);
00417     MI->addOperand(MachineOperand::CreateImm(SubIdx));
00418     BB->insert(End, MI);
00419   } else
00420     assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
00421      
00422   SDValue Op(Node, 0);
00423   bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
00424   isNew = isNew; // Silence compiler warning.
00425   assert(isNew && "Node emitted out of order - early");
00426 }
00427 
00428 /// EmitNode - Generate machine code for an node and needed dependencies.
00429 ///
00430 void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
00431                                   DenseMap<SDValue, unsigned> &VRBaseMap) {
00432   // If machine instruction
00433   if (Node->isMachineOpcode()) {
00434     unsigned Opc = Node->getMachineOpcode();
00435     
00436     // Handle subreg insert/extract specially
00437     if (Opc == TargetInstrInfo::EXTRACT_SUBREG || 
00438         Opc == TargetInstrInfo::INSERT_SUBREG ||
00439         Opc == TargetInstrInfo::SUBREG_TO_REG) {
00440       EmitSubregNode(Node, VRBaseMap);
00441       return;
00442     }
00443 
00444     if (Opc == TargetInstrInfo::IMPLICIT_DEF)
00445       // We want a unique VR for each IMPLICIT_DEF use.
00446       return;
00447     
00448     const TargetInstrDesc &II = TII->get(Opc);
00449     unsigned NumResults = CountResults(Node);
00450     unsigned NodeOperands = CountOperands(Node);
00451     unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
00452     bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
00453                           II.getImplicitDefs() != 0;
00454 #ifndef NDEBUG
00455     unsigned NumMIOperands = NodeOperands + NumResults;
00456     assert((II.getNumOperands() == NumMIOperands ||
00457             HasPhysRegOuts || II.isVariadic()) &&
00458            "#operands for dag node doesn't match .td file!"); 
00459 #endif
00460 
00461     // Create the new machine instruction.
00462     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
00463     
00464     // Add result register values for things that are defined by this
00465     // instruction.
00466     if (NumResults)
00467       CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
00468     
00469     // Emit all of the actual operands of this instruction, adding them to the
00470     // instruction as appropriate.
00471     for (unsigned i = 0; i != NodeOperands; ++i)
00472       AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
00473 
00474     // Emit all of the memory operands of this instruction
00475     for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
00476       AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
00477 
00478     if (II.usesCustomDAGSchedInsertionHook()) {
00479       // Insert this instruction into the basic block using a target
00480       // specific inserter which may returns a new basic block.
00481       BB = TLI->EmitInstrWithCustomInserter(MI, BB);
00482       Begin = End = BB->end();
00483     } else {
00484       BB->insert(End, MI);
00485     }
00486 
00487     // Additional results must be an physical register def.
00488     if (HasPhysRegOuts) {
00489       for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
00490         unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
00491         if (Node->hasAnyUseOfValue(i))
00492           EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
00493       }
00494     }
00495     return;
00496   }
00497 
00498   switch (Node->getOpcode()) {
00499   default:
00500 #ifndef NDEBUG
00501     Node->dump(DAG);
00502 #endif
00503     assert(0 && "This target-independent node should have been selected!");
00504     break;
00505   case ISD::EntryToken:
00506     assert(0 && "EntryToken should have been excluded from the schedule!");
00507     break;
00508   case ISD::TokenFactor: // fall thru
00509     break;
00510   case ISD::CopyToReg: {
00511     unsigned SrcReg;
00512     SDValue SrcVal = Node->getOperand(2);
00513     if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
00514       SrcReg = R->getReg();
00515     else
00516       SrcReg = getVR(SrcVal, VRBaseMap);
00517       
00518     unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
00519     if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
00520       break;
00521       
00522     const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
00523     // Get the register classes of the src/dst.
00524     if (TargetRegisterInfo::isVirtualRegister(SrcReg))
00525       SrcTRC = MRI.getRegClass(SrcReg);
00526     else
00527       SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
00528 
00529     if (TargetRegisterInfo::isVirtualRegister(DestReg))
00530       DstTRC = MRI.getRegClass(DestReg);
00531     else
00532       DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
00533                                             Node->getOperand(1).getValueType());
00534     TII->copyRegToReg(*BB, End, DestReg, SrcReg, DstTRC, SrcTRC);
00535     break;
00536   }
00537   case ISD::CopyFromReg: {
00538     unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
00539     EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
00540     break;
00541   }
00542   case ISD::INLINEASM: {
00543     unsigned NumOps = Node->getNumOperands();
00544     if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
00545       --NumOps;  // Ignore the flag operand.
00546       
00547     // Create the inline asm machine instruction.
00548     MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
00549                                TII->get(TargetInstrInfo::INLINEASM));
00550 
00551     // Add the asm string as an external symbol operand.
00552     const char *AsmStr =
00553       cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
00554     MI->addOperand(MachineOperand::CreateES(AsmStr));
00555       
00556     // Add all of the operand registers to the instruction.
00557     for (unsigned i = 2; i != NumOps;) {
00558       unsigned Flags =
00559         cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
00560       unsigned NumVals = Flags >> 3;
00561         
00562       MI->addOperand(MachineOperand::CreateImm(Flags));
00563       ++i;  // Skip the ID value.
00564         
00565       switch (Flags & 7) {
00566       default: assert(0 && "Bad flags!");
00567       case 2:   // Def of register.
00568         for (; NumVals; --NumVals, ++i) {
00569           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
00570           MI->addOperand(MachineOperand::CreateReg(Reg, true));
00571         }
00572         break;
00573       case 6:   // Def of earlyclobber register.
00574         for (; NumVals; --NumVals, ++i) {
00575           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
00576           MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false, 
00577                                                    false, 0, true));
00578         }
00579         break;
00580       case 1:  // Use of register.
00581       case 3:  // Immediate.
00582       case 4:  // Addressing mode.
00583         // The addressing mode has been selected, just add all of the
00584         // operands to the machine instruction.
00585         for (; NumVals; --NumVals, ++i)
00586           AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
00587         break;
00588       }
00589     }
00590     BB->insert(End, MI);
00591     break;
00592   }
00593   }
00594 }
00595 
00596 /// EmitSchedule - Emit the machine code in scheduled order.
00597 MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
00598   DenseMap<SDValue, unsigned> VRBaseMap;
00599   DenseMap<SUnit*, unsigned> CopyVRBaseMap;
00600   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
00601     SUnit *SU = Sequence[i];
00602     if (!SU) {
00603       // Null SUnit* is a noop.
00604       EmitNoop();
00605       continue;
00606     }
00607 
00608     // For pre-regalloc scheduling, create instructions corresponding to the
00609     // SDNode and any flagged SDNodes and append them to the block.
00610     if (!SU->getNode()) {
00611       // Emit a copy.
00612       EmitPhysRegCopy(SU, CopyVRBaseMap);
00613       continue;
00614     }
00615 
00616     SmallVector<SDNode *, 4> FlaggedNodes;
00617     for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
00618          N = N->getFlaggedNode())
00619       FlaggedNodes.push_back(N);
00620     while (!FlaggedNodes.empty()) {
00621       EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
00622       FlaggedNodes.pop_back();
00623     }
00624     EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
00625   }
00626 
00627   return BB;
00628 }