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