LLVM API Documentation

InstrEmitter.cpp
Go to the documentation of this file.
00001 //==--- InstrEmitter.cpp - Emit MachineInstrs for the SelectionDAG 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 SelectionDAG class, which creates
00011 // MachineInstrs based on the decisions of the SelectionDAG instruction
00012 // selection.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "InstrEmitter.h"
00017 #include "SDNodeDbgValue.h"
00018 #include "llvm/ADT/Statistic.h"
00019 #include "llvm/CodeGen/MachineConstantPool.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineInstrBuilder.h"
00022 #include "llvm/CodeGen/MachineRegisterInfo.h"
00023 #include "llvm/CodeGen/StackMaps.h"
00024 #include "llvm/IR/DataLayout.h"
00025 #include "llvm/Support/Debug.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include "llvm/Support/MathExtras.h"
00028 #include "llvm/Target/TargetInstrInfo.h"
00029 #include "llvm/Target/TargetLowering.h"
00030 #include "llvm/Target/TargetMachine.h"
00031 using namespace llvm;
00032 
00033 #define DEBUG_TYPE "instr-emitter"
00034 
00035 /// MinRCSize - Smallest register class we allow when constraining virtual
00036 /// registers.  If satisfying all register class constraints would require
00037 /// using a smaller register class, emit a COPY to a new virtual register
00038 /// instead.
00039 const unsigned MinRCSize = 4;
00040 
00041 /// CountResults - The results of target nodes have register or immediate
00042 /// operands first, then an optional chain, and optional glue operands (which do
00043 /// not go into the resulting MachineInstr).
00044 unsigned InstrEmitter::CountResults(SDNode *Node) {
00045   unsigned N = Node->getNumValues();
00046   while (N && Node->getValueType(N - 1) == MVT::Glue)
00047     --N;
00048   if (N && Node->getValueType(N - 1) == MVT::Other)
00049     --N;    // Skip over chain result.
00050   return N;
00051 }
00052 
00053 /// countOperands - The inputs to target nodes have any actual inputs first,
00054 /// followed by an optional chain operand, then an optional glue operand.
00055 /// Compute the number of actual operands that will go into the resulting
00056 /// MachineInstr.
00057 ///
00058 /// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding
00059 /// the chain and glue. These operands may be implicit on the machine instr.
00060 static unsigned countOperands(SDNode *Node, unsigned NumExpUses,
00061                               unsigned &NumImpUses) {
00062   unsigned N = Node->getNumOperands();
00063   while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
00064     --N;
00065   if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
00066     --N; // Ignore chain if it exists.
00067 
00068   // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses.
00069   NumImpUses = N - NumExpUses;
00070   for (unsigned I = N; I > NumExpUses; --I) {
00071     if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1)))
00072       continue;
00073     if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1)))
00074       if (TargetRegisterInfo::isPhysicalRegister(RN->getReg()))
00075         continue;
00076     NumImpUses = N - I;
00077     break;
00078   }
00079 
00080   return N;
00081 }
00082 
00083 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
00084 /// implicit physical register output.
00085 void InstrEmitter::
00086 EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned,
00087                 unsigned SrcReg, DenseMap<SDValue, unsigned> &VRBaseMap) {
00088   unsigned VRBase = 0;
00089   if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
00090     // Just use the input register directly!
00091     SDValue Op(Node, ResNo);
00092     if (IsClone)
00093       VRBaseMap.erase(Op);
00094     bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
00095     (void)isNew; // Silence compiler warning.
00096     assert(isNew && "Node emitted out of order - early");
00097     return;
00098   }
00099 
00100   // If the node is only used by a CopyToReg and the dest reg is a vreg, use
00101   // the CopyToReg'd destination register instead of creating a new vreg.
00102   bool MatchReg = true;
00103   const TargetRegisterClass *UseRC = nullptr;
00104   MVT VT = Node->getSimpleValueType(ResNo);
00105 
00106   // Stick to the preferred register classes for legal types.
00107   if (TLI->isTypeLegal(VT))
00108     UseRC = TLI->getRegClassFor(VT);
00109 
00110   if (!IsClone && !IsCloned)
00111     for (SDNode *User : Node->uses()) {
00112       bool Match = true;
00113       if (User->getOpcode() == ISD::CopyToReg &&
00114           User->getOperand(2).getNode() == Node &&
00115           User->getOperand(2).getResNo() == ResNo) {
00116         unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00117         if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
00118           VRBase = DestReg;
00119           Match = false;
00120         } else if (DestReg != SrcReg)
00121           Match = false;
00122       } else {
00123         for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
00124           SDValue Op = User->getOperand(i);
00125           if (Op.getNode() != Node || Op.getResNo() != ResNo)
00126             continue;
00127           MVT VT = Node->getSimpleValueType(Op.getResNo());
00128           if (VT == MVT::Other || VT == MVT::Glue)
00129             continue;
00130           Match = false;
00131           if (User->isMachineOpcode()) {
00132             const MCInstrDesc &II = TII->get(User->getMachineOpcode());
00133             const TargetRegisterClass *RC = nullptr;
00134             if (i+II.getNumDefs() < II.getNumOperands()) {
00135               RC = TRI->getAllocatableClass(
00136                 TII->getRegClass(II, i+II.getNumDefs(), TRI, *MF));
00137             }
00138             if (!UseRC)
00139               UseRC = RC;
00140             else if (RC) {
00141               const TargetRegisterClass *ComRC =
00142                 TRI->getCommonSubClass(UseRC, RC);
00143               // If multiple uses expect disjoint register classes, we emit
00144               // copies in AddRegisterOperand.
00145               if (ComRC)
00146                 UseRC = ComRC;
00147             }
00148           }
00149         }
00150       }
00151       MatchReg &= Match;
00152       if (VRBase)
00153         break;
00154     }
00155 
00156   const TargetRegisterClass *SrcRC = nullptr, *DstRC = nullptr;
00157   SrcRC = TRI->getMinimalPhysRegClass(SrcReg, VT);
00158 
00159   // Figure out the register class to create for the destreg.
00160   if (VRBase) {
00161     DstRC = MRI->getRegClass(VRBase);
00162   } else if (UseRC) {
00163     assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
00164     DstRC = UseRC;
00165   } else {
00166     DstRC = TLI->getRegClassFor(VT);
00167   }
00168 
00169   // If all uses are reading from the src physical register and copying the
00170   // register is either impossible or very expensive, then don't create a copy.
00171   if (MatchReg && SrcRC->getCopyCost() < 0) {
00172     VRBase = SrcReg;
00173   } else {
00174     // Create the reg, emit the copy.
00175     VRBase = MRI->createVirtualRegister(DstRC);
00176     BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
00177             VRBase).addReg(SrcReg);
00178   }
00179 
00180   SDValue Op(Node, ResNo);
00181   if (IsClone)
00182     VRBaseMap.erase(Op);
00183   bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
00184   (void)isNew; // Silence compiler warning.
00185   assert(isNew && "Node emitted out of order - early");
00186 }
00187 
00188 /// getDstOfCopyToRegUse - If the only use of the specified result number of
00189 /// node is a CopyToReg, return its destination register. Return 0 otherwise.
00190 unsigned InstrEmitter::getDstOfOnlyCopyToRegUse(SDNode *Node,
00191                                                 unsigned ResNo) const {
00192   if (!Node->hasOneUse())
00193     return 0;
00194 
00195   SDNode *User = *Node->use_begin();
00196   if (User->getOpcode() == ISD::CopyToReg &&
00197       User->getOperand(2).getNode() == Node &&
00198       User->getOperand(2).getResNo() == ResNo) {
00199     unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00200     if (TargetRegisterInfo::isVirtualRegister(Reg))
00201       return Reg;
00202   }
00203   return 0;
00204 }
00205 
00206 void InstrEmitter::CreateVirtualRegisters(SDNode *Node,
00207                                        MachineInstrBuilder &MIB,
00208                                        const MCInstrDesc &II,
00209                                        bool IsClone, bool IsCloned,
00210                                        DenseMap<SDValue, unsigned> &VRBaseMap) {
00211   assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF &&
00212          "IMPLICIT_DEF should have been handled as a special case elsewhere!");
00213 
00214   unsigned NumResults = CountResults(Node);
00215   for (unsigned i = 0; i < II.getNumDefs(); ++i) {
00216     // If the specific node value is only used by a CopyToReg and the dest reg
00217     // is a vreg in the same register class, use the CopyToReg'd destination
00218     // register instead of creating a new vreg.
00219     unsigned VRBase = 0;
00220     const TargetRegisterClass *RC =
00221       TRI->getAllocatableClass(TII->getRegClass(II, i, TRI, *MF));
00222     // Always let the value type influence the used register class. The
00223     // constraints on the instruction may be too lax to represent the value
00224     // type correctly. For example, a 64-bit float (X86::FR64) can't live in
00225     // the 32-bit float super-class (X86::FR32).
00226     if (i < NumResults && TLI->isTypeLegal(Node->getSimpleValueType(i))) {
00227       const TargetRegisterClass *VTRC =
00228         TLI->getRegClassFor(Node->getSimpleValueType(i));
00229       if (RC)
00230         VTRC = TRI->getCommonSubClass(RC, VTRC);
00231       if (VTRC)
00232         RC = VTRC;
00233     }
00234 
00235     if (II.OpInfo[i].isOptionalDef()) {
00236       // Optional def must be a physical register.
00237       unsigned NumResults = CountResults(Node);
00238       VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg();
00239       assert(TargetRegisterInfo::isPhysicalRegister(VRBase));
00240       MIB.addReg(VRBase, RegState::Define);
00241     }
00242 
00243     if (!VRBase && !IsClone && !IsCloned)
00244       for (SDNode *User : Node->uses()) {
00245         if (User->getOpcode() == ISD::CopyToReg &&
00246             User->getOperand(2).getNode() == Node &&
00247             User->getOperand(2).getResNo() == i) {
00248           unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00249           if (TargetRegisterInfo::isVirtualRegister(Reg)) {
00250             const TargetRegisterClass *RegRC = MRI->getRegClass(Reg);
00251             if (RegRC == RC) {
00252               VRBase = Reg;
00253               MIB.addReg(VRBase, RegState::Define);
00254               break;
00255             }
00256           }
00257         }
00258       }
00259 
00260     // Create the result registers for this node and add the result regs to
00261     // the machine instruction.
00262     if (VRBase == 0) {
00263       assert(RC && "Isn't a register operand!");
00264       VRBase = MRI->createVirtualRegister(RC);
00265       MIB.addReg(VRBase, RegState::Define);
00266     }
00267 
00268     // If this def corresponds to a result of the SDNode insert the VRBase into
00269     // the lookup map.
00270     if (i < NumResults) {
00271       SDValue Op(Node, i);
00272       if (IsClone)
00273         VRBaseMap.erase(Op);
00274       bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
00275       (void)isNew; // Silence compiler warning.
00276       assert(isNew && "Node emitted out of order - early");
00277     }
00278   }
00279 }
00280 
00281 /// getVR - Return the virtual register corresponding to the specified result
00282 /// of the specified node.
00283 unsigned InstrEmitter::getVR(SDValue Op,
00284                              DenseMap<SDValue, unsigned> &VRBaseMap) {
00285   if (Op.isMachineOpcode() &&
00286       Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
00287     // Add an IMPLICIT_DEF instruction before every use.
00288     unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
00289     // IMPLICIT_DEF can produce any type of result so its MCInstrDesc
00290     // does not include operand register class info.
00291     if (!VReg) {
00292       const TargetRegisterClass *RC =
00293         TLI->getRegClassFor(Op.getSimpleValueType());
00294       VReg = MRI->createVirtualRegister(RC);
00295     }
00296     BuildMI(*MBB, InsertPos, Op.getDebugLoc(),
00297             TII->get(TargetOpcode::IMPLICIT_DEF), VReg);
00298     return VReg;
00299   }
00300 
00301   DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
00302   assert(I != VRBaseMap.end() && "Node emitted out of order - late");
00303   return I->second;
00304 }
00305 
00306 
00307 /// AddRegisterOperand - Add the specified register as an operand to the
00308 /// specified machine instr. Insert register copies if the register is
00309 /// not in the required register class.
00310 void
00311 InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB,
00312                                  SDValue Op,
00313                                  unsigned IIOpNum,
00314                                  const MCInstrDesc *II,
00315                                  DenseMap<SDValue, unsigned> &VRBaseMap,
00316                                  bool IsDebug, bool IsClone, bool IsCloned) {
00317   assert(Op.getValueType() != MVT::Other &&
00318          Op.getValueType() != MVT::Glue &&
00319          "Chain and glue operands should occur at end of operand list!");
00320   // Get/emit the operand.
00321   unsigned VReg = getVR(Op, VRBaseMap);
00322   assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
00323 
00324   const MCInstrDesc &MCID = MIB->getDesc();
00325   bool isOptDef = IIOpNum < MCID.getNumOperands() &&
00326     MCID.OpInfo[IIOpNum].isOptionalDef();
00327 
00328   // If the instruction requires a register in a different class, create
00329   // a new virtual register and copy the value into it, but first attempt to
00330   // shrink VReg's register class within reason.  For example, if VReg == GR32
00331   // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP.
00332   if (II) {
00333     const TargetRegisterClass *DstRC = nullptr;
00334     if (IIOpNum < II->getNumOperands())
00335       DstRC = TRI->getAllocatableClass(TII->getRegClass(*II,IIOpNum,TRI,*MF));
00336     if (DstRC && !MRI->constrainRegClass(VReg, DstRC, MinRCSize)) {
00337       unsigned NewVReg = MRI->createVirtualRegister(DstRC);
00338       BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
00339               TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
00340       VReg = NewVReg;
00341     }
00342   }
00343 
00344   // If this value has only one use, that use is a kill. This is a
00345   // conservative approximation. InstrEmitter does trivial coalescing
00346   // with CopyFromReg nodes, so don't emit kill flags for them.
00347   // Avoid kill flags on Schedule cloned nodes, since there will be
00348   // multiple uses.
00349   // Tied operands are never killed, so we need to check that. And that
00350   // means we need to determine the index of the operand.
00351   bool isKill = Op.hasOneUse() &&
00352                 Op.getNode()->getOpcode() != ISD::CopyFromReg &&
00353                 !IsDebug &&
00354                 !(IsClone || IsCloned);
00355   if (isKill) {
00356     unsigned Idx = MIB->getNumOperands();
00357     while (Idx > 0 &&
00358            MIB->getOperand(Idx-1).isReg() &&
00359            MIB->getOperand(Idx-1).isImplicit())
00360       --Idx;
00361     bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1;
00362     if (isTied)
00363       isKill = false;
00364   }
00365 
00366   MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) |
00367              getDebugRegState(IsDebug));
00368 }
00369 
00370 /// AddOperand - Add the specified operand to the specified machine instr.  II
00371 /// specifies the instruction information for the node, and IIOpNum is the
00372 /// operand number (in the II) that we are adding.
00373 void InstrEmitter::AddOperand(MachineInstrBuilder &MIB,
00374                               SDValue Op,
00375                               unsigned IIOpNum,
00376                               const MCInstrDesc *II,
00377                               DenseMap<SDValue, unsigned> &VRBaseMap,
00378                               bool IsDebug, bool IsClone, bool IsCloned) {
00379   if (Op.isMachineOpcode()) {
00380     AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
00381                        IsDebug, IsClone, IsCloned);
00382   } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
00383     MIB.addImm(C->getSExtValue());
00384   } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
00385     MIB.addFPImm(F->getConstantFPValue());
00386   } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
00387     // Turn additional physreg operands into implicit uses on non-variadic
00388     // instructions. This is used by call and return instructions passing
00389     // arguments in registers.
00390     bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic());
00391     MIB.addReg(R->getReg(), getImplRegState(Imp));
00392   } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) {
00393     MIB.addRegMask(RM->getRegMask());
00394   } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
00395     MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(),
00396                          TGA->getTargetFlags());
00397   } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
00398     MIB.addMBB(BBNode->getBasicBlock());
00399   } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
00400     MIB.addFrameIndex(FI->getIndex());
00401   } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
00402     MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags());
00403   } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
00404     int Offset = CP->getOffset();
00405     unsigned Align = CP->getAlignment();
00406     Type *Type = CP->getType();
00407     // MachineConstantPool wants an explicit alignment.
00408     if (Align == 0) {
00409       Align = TM->getDataLayout()->getPrefTypeAlignment(Type);
00410       if (Align == 0) {
00411         // Alignment of vector types.  FIXME!
00412         Align = TM->getDataLayout()->getTypeAllocSize(Type);
00413       }
00414     }
00415 
00416     unsigned Idx;
00417     MachineConstantPool *MCP = MF->getConstantPool();
00418     if (CP->isMachineConstantPoolEntry())
00419       Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Align);
00420     else
00421       Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Align);
00422     MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags());
00423   } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
00424     MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags());
00425   } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) {
00426     MIB.addBlockAddress(BA->getBlockAddress(),
00427                         BA->getOffset(),
00428                         BA->getTargetFlags());
00429   } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) {
00430     MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags());
00431   } else {
00432     assert(Op.getValueType() != MVT::Other &&
00433            Op.getValueType() != MVT::Glue &&
00434            "Chain and glue operands should occur at end of operand list!");
00435     AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
00436                        IsDebug, IsClone, IsCloned);
00437   }
00438 }
00439 
00440 unsigned InstrEmitter::ConstrainForSubReg(unsigned VReg, unsigned SubIdx,
00441                                           MVT VT, DebugLoc DL) {
00442   const TargetRegisterClass *VRC = MRI->getRegClass(VReg);
00443   const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx);
00444 
00445   // RC is a sub-class of VRC that supports SubIdx.  Try to constrain VReg
00446   // within reason.
00447   if (RC && RC != VRC)
00448     RC = MRI->constrainRegClass(VReg, RC, MinRCSize);
00449 
00450   // VReg has been adjusted.  It can be used with SubIdx operands now.
00451   if (RC)
00452     return VReg;
00453 
00454   // VReg couldn't be reasonably constrained.  Emit a COPY to a new virtual
00455   // register instead.
00456   RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT), SubIdx);
00457   assert(RC && "No legal register class for VT supports that SubIdx");
00458   unsigned NewReg = MRI->createVirtualRegister(RC);
00459   BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg)
00460     .addReg(VReg);
00461   return NewReg;
00462 }
00463 
00464 /// EmitSubregNode - Generate machine code for subreg nodes.
00465 ///
00466 void InstrEmitter::EmitSubregNode(SDNode *Node,
00467                                   DenseMap<SDValue, unsigned> &VRBaseMap,
00468                                   bool IsClone, bool IsCloned) {
00469   unsigned VRBase = 0;
00470   unsigned Opc = Node->getMachineOpcode();
00471 
00472   // If the node is only used by a CopyToReg and the dest reg is a vreg, use
00473   // the CopyToReg'd destination register instead of creating a new vreg.
00474   for (SDNode *User : Node->uses()) {
00475     if (User->getOpcode() == ISD::CopyToReg &&
00476         User->getOperand(2).getNode() == Node) {
00477       unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
00478       if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
00479         VRBase = DestReg;
00480         break;
00481       }
00482     }
00483   }
00484 
00485   if (Opc == TargetOpcode::EXTRACT_SUBREG) {
00486     // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub.  There are no
00487     // constraints on the %dst register, COPY can target all legal register
00488     // classes.
00489     unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
00490     const TargetRegisterClass *TRC =
00491       TLI->getRegClassFor(Node->getSimpleValueType(0));
00492 
00493     unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
00494     MachineInstr *DefMI = MRI->getVRegDef(VReg);
00495     unsigned SrcReg, DstReg, DefSubIdx;
00496     if (DefMI &&
00497         TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) &&
00498         SubIdx == DefSubIdx &&
00499         TRC == MRI->getRegClass(SrcReg)) {
00500       // Optimize these:
00501       // r1025 = s/zext r1024, 4
00502       // r1026 = extract_subreg r1025, 4
00503       // to a copy
00504       // r1026 = copy r1024
00505       VRBase = MRI->createVirtualRegister(TRC);
00506       BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
00507               TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg);
00508       MRI->clearKillFlags(SrcReg);
00509     } else {
00510       // VReg may not support a SubIdx sub-register, and we may need to
00511       // constrain its register class or issue a COPY to a compatible register
00512       // class.
00513       VReg = ConstrainForSubReg(VReg, SubIdx,
00514                                 Node->getOperand(0).getSimpleValueType(),
00515                                 Node->getDebugLoc());
00516 
00517       // Create the destreg if it is missing.
00518       if (VRBase == 0)
00519         VRBase = MRI->createVirtualRegister(TRC);
00520 
00521       // Create the extract_subreg machine instruction.
00522       BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
00523               TII->get(TargetOpcode::COPY), VRBase).addReg(VReg, 0, SubIdx);
00524     }
00525   } else if (Opc == TargetOpcode::INSERT_SUBREG ||
00526              Opc == TargetOpcode::SUBREG_TO_REG) {
00527     SDValue N0 = Node->getOperand(0);
00528     SDValue N1 = Node->getOperand(1);
00529     SDValue N2 = Node->getOperand(2);
00530     unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
00531 
00532     // Figure out the register class to create for the destreg.  It should be
00533     // the largest legal register class supporting SubIdx sub-registers.
00534     // RegisterCoalescer will constrain it further if it decides to eliminate
00535     // the INSERT_SUBREG instruction.
00536     //
00537     //   %dst = INSERT_SUBREG %src, %sub, SubIdx
00538     //
00539     // is lowered by TwoAddressInstructionPass to:
00540     //
00541     //   %dst = COPY %src
00542     //   %dst:SubIdx = COPY %sub
00543     //
00544     // There is no constraint on the %src register class.
00545     //
00546     const TargetRegisterClass *SRC = TLI->getRegClassFor(Node->getSimpleValueType(0));
00547     SRC = TRI->getSubClassWithSubReg(SRC, SubIdx);
00548     assert(SRC && "No register class supports VT and SubIdx for INSERT_SUBREG");
00549 
00550     if (VRBase == 0 || !SRC->hasSubClassEq(MRI->getRegClass(VRBase)))
00551       VRBase = MRI->createVirtualRegister(SRC);
00552 
00553     // Create the insert_subreg or subreg_to_reg machine instruction.
00554     MachineInstrBuilder MIB =
00555       BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc), VRBase);
00556 
00557     // If creating a subreg_to_reg, then the first input operand
00558     // is an implicit value immediate, otherwise it's a register
00559     if (Opc == TargetOpcode::SUBREG_TO_REG) {
00560       const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
00561       MIB.addImm(SD->getZExtValue());
00562     } else
00563       AddOperand(MIB, N0, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
00564                  IsClone, IsCloned);
00565     // Add the subregster being inserted
00566     AddOperand(MIB, N1, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
00567                IsClone, IsCloned);
00568     MIB.addImm(SubIdx);
00569     MBB->insert(InsertPos, MIB);
00570   } else
00571     llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg");
00572 
00573   SDValue Op(Node, 0);
00574   bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
00575   (void)isNew; // Silence compiler warning.
00576   assert(isNew && "Node emitted out of order - early");
00577 }
00578 
00579 /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
00580 /// COPY_TO_REGCLASS is just a normal copy, except that the destination
00581 /// register is constrained to be in a particular register class.
00582 ///
00583 void
00584 InstrEmitter::EmitCopyToRegClassNode(SDNode *Node,
00585                                      DenseMap<SDValue, unsigned> &VRBaseMap) {
00586   unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
00587 
00588   // Create the new VReg in the destination class and emit a copy.
00589   unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
00590   const TargetRegisterClass *DstRC =
00591     TRI->getAllocatableClass(TRI->getRegClass(DstRCIdx));
00592   unsigned NewVReg = MRI->createVirtualRegister(DstRC);
00593   BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
00594     NewVReg).addReg(VReg);
00595 
00596   SDValue Op(Node, 0);
00597   bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
00598   (void)isNew; // Silence compiler warning.
00599   assert(isNew && "Node emitted out of order - early");
00600 }
00601 
00602 /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
00603 ///
00604 void InstrEmitter::EmitRegSequence(SDNode *Node,
00605                                   DenseMap<SDValue, unsigned> &VRBaseMap,
00606                                   bool IsClone, bool IsCloned) {
00607   unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
00608   const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
00609   unsigned NewVReg = MRI->createVirtualRegister(TRI->getAllocatableClass(RC));
00610   const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE);
00611   MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg);
00612   unsigned NumOps = Node->getNumOperands();
00613   assert((NumOps & 1) == 1 &&
00614          "REG_SEQUENCE must have an odd number of operands!");
00615   for (unsigned i = 1; i != NumOps; ++i) {
00616     SDValue Op = Node->getOperand(i);
00617     if ((i & 1) == 0) {
00618       RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(i-1));
00619       // Skip physical registers as they don't have a vreg to get and we'll
00620       // insert copies for them in TwoAddressInstructionPass anyway.
00621       if (!R || !TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
00622         unsigned SubIdx = cast<ConstantSDNode>(Op)->getZExtValue();
00623         unsigned SubReg = getVR(Node->getOperand(i-1), VRBaseMap);
00624         const TargetRegisterClass *TRC = MRI->getRegClass(SubReg);
00625         const TargetRegisterClass *SRC =
00626         TRI->getMatchingSuperRegClass(RC, TRC, SubIdx);
00627         if (SRC && SRC != RC) {
00628           MRI->setRegClass(NewVReg, SRC);
00629           RC = SRC;
00630         }
00631       }
00632     }
00633     AddOperand(MIB, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false,
00634                IsClone, IsCloned);
00635   }
00636 
00637   MBB->insert(InsertPos, MIB);
00638   SDValue Op(Node, 0);
00639   bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
00640   (void)isNew; // Silence compiler warning.
00641   assert(isNew && "Node emitted out of order - early");
00642 }
00643 
00644 /// EmitDbgValue - Generate machine instruction for a dbg_value node.
00645 ///
00646 MachineInstr *
00647 InstrEmitter::EmitDbgValue(SDDbgValue *SD,
00648                            DenseMap<SDValue, unsigned> &VRBaseMap) {
00649   uint64_t Offset = SD->getOffset();
00650   MDNode* MDPtr = SD->getMDPtr();
00651   DebugLoc DL = SD->getDebugLoc();
00652 
00653   if (SD->getKind() == SDDbgValue::FRAMEIX) {
00654     // Stack address; this needs to be lowered in target-dependent fashion.
00655     // EmitTargetCodeForFrameDebugValue is responsible for allocation.
00656     return BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE))
00657         .addFrameIndex(SD->getFrameIx()).addImm(Offset).addMetadata(MDPtr);
00658   }
00659   // Otherwise, we're going to create an instruction here.
00660   const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE);
00661   MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
00662   if (SD->getKind() == SDDbgValue::SDNODE) {
00663     SDNode *Node = SD->getSDNode();
00664     SDValue Op = SDValue(Node, SD->getResNo());
00665     // It's possible we replaced this SDNode with other(s) and therefore
00666     // didn't generate code for it.  It's better to catch these cases where
00667     // they happen and transfer the debug info, but trying to guarantee that
00668     // in all cases would be very fragile; this is a safeguard for any
00669     // that were missed.
00670     DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
00671     if (I==VRBaseMap.end())
00672       MIB.addReg(0U);       // undef
00673     else
00674       AddOperand(MIB, Op, (*MIB).getNumOperands(), &II, VRBaseMap,
00675                  /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false);
00676   } else if (SD->getKind() == SDDbgValue::CONST) {
00677     const Value *V = SD->getConst();
00678     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00679       if (CI->getBitWidth() > 64)
00680         MIB.addCImm(CI);
00681       else
00682         MIB.addImm(CI->getSExtValue());
00683     } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
00684       MIB.addFPImm(CF);
00685     } else {
00686       // Could be an Undef.  In any case insert an Undef so we can see what we
00687       // dropped.
00688       MIB.addReg(0U);
00689     }
00690   } else {
00691     // Insert an Undef so we can see what we dropped.
00692     MIB.addReg(0U);
00693   }
00694 
00695   // Indirect addressing is indicated by an Imm as the second parameter.
00696   if (SD->isIndirect())
00697     MIB.addImm(Offset);
00698   else {
00699     assert(Offset == 0 && "direct value cannot have an offset");
00700     MIB.addReg(0U, RegState::Debug);
00701   }
00702 
00703   MIB.addMetadata(MDPtr);
00704 
00705   return &*MIB;
00706 }
00707 
00708 /// EmitMachineNode - Generate machine code for a target-specific node and
00709 /// needed dependencies.
00710 ///
00711 void InstrEmitter::
00712 EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
00713                 DenseMap<SDValue, unsigned> &VRBaseMap) {
00714   unsigned Opc = Node->getMachineOpcode();
00715 
00716   // Handle subreg insert/extract specially
00717   if (Opc == TargetOpcode::EXTRACT_SUBREG ||
00718       Opc == TargetOpcode::INSERT_SUBREG ||
00719       Opc == TargetOpcode::SUBREG_TO_REG) {
00720     EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned);
00721     return;
00722   }
00723 
00724   // Handle COPY_TO_REGCLASS specially.
00725   if (Opc == TargetOpcode::COPY_TO_REGCLASS) {
00726     EmitCopyToRegClassNode(Node, VRBaseMap);
00727     return;
00728   }
00729 
00730   // Handle REG_SEQUENCE specially.
00731   if (Opc == TargetOpcode::REG_SEQUENCE) {
00732     EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned);
00733     return;
00734   }
00735 
00736   if (Opc == TargetOpcode::IMPLICIT_DEF)
00737     // We want a unique VR for each IMPLICIT_DEF use.
00738     return;
00739 
00740   const MCInstrDesc &II = TII->get(Opc);
00741   unsigned NumResults = CountResults(Node);
00742   unsigned NumDefs = II.getNumDefs();
00743   const MCPhysReg *ScratchRegs = nullptr;
00744 
00745   // Handle STACKMAP and PATCHPOINT specially and then use the generic code.
00746   if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
00747     // Stackmaps do not have arguments and do not preserve their calling
00748     // convention. However, to simplify runtime support, they clobber the same
00749     // scratch registers as AnyRegCC.
00750     unsigned CC = CallingConv::AnyReg;
00751     if (Opc == TargetOpcode::PATCHPOINT) {
00752       CC = Node->getConstantOperandVal(PatchPointOpers::CCPos);
00753       NumDefs = NumResults;
00754     }
00755     ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC);
00756   }
00757 
00758   unsigned NumImpUses = 0;
00759   unsigned NodeOperands =
00760     countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses);
00761   bool HasPhysRegOuts = NumResults > NumDefs && II.getImplicitDefs()!=nullptr;
00762 #ifndef NDEBUG
00763   unsigned NumMIOperands = NodeOperands + NumResults;
00764   if (II.isVariadic())
00765     assert(NumMIOperands >= II.getNumOperands() &&
00766            "Too few operands for a variadic node!");
00767   else
00768     assert(NumMIOperands >= II.getNumOperands() &&
00769            NumMIOperands <= II.getNumOperands() + II.getNumImplicitDefs() +
00770                             NumImpUses &&
00771            "#operands for dag node doesn't match .td file!");
00772 #endif
00773 
00774   // Create the new machine instruction.
00775   MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II);
00776 
00777   // Add result register values for things that are defined by this
00778   // instruction.
00779   if (NumResults)
00780     CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap);
00781 
00782   // Emit all of the actual operands of this instruction, adding them to the
00783   // instruction as appropriate.
00784   bool HasOptPRefs = NumDefs > NumResults;
00785   assert((!HasOptPRefs || !HasPhysRegOuts) &&
00786          "Unable to cope with optional defs and phys regs defs!");
00787   unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0;
00788   for (unsigned i = NumSkip; i != NodeOperands; ++i)
00789     AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II,
00790                VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned);
00791 
00792   // Add scratch registers as implicit def and early clobber
00793   if (ScratchRegs)
00794     for (unsigned i = 0; ScratchRegs[i]; ++i)
00795       MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine |
00796                                  RegState::EarlyClobber);
00797 
00798   // Transfer all of the memory reference descriptions of this instruction.
00799   MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands_begin(),
00800                  cast<MachineSDNode>(Node)->memoperands_end());
00801 
00802   // Insert the instruction into position in the block. This needs to
00803   // happen before any custom inserter hook is called so that the
00804   // hook knows where in the block to insert the replacement code.
00805   MBB->insert(InsertPos, MIB);
00806 
00807   // The MachineInstr may also define physregs instead of virtregs.  These
00808   // physreg values can reach other instructions in different ways:
00809   //
00810   // 1. When there is a use of a Node value beyond the explicitly defined
00811   //    virtual registers, we emit a CopyFromReg for one of the implicitly
00812   //    defined physregs.  This only happens when HasPhysRegOuts is true.
00813   //
00814   // 2. A CopyFromReg reading a physreg may be glued to this instruction.
00815   //
00816   // 3. A glued instruction may implicitly use a physreg.
00817   //
00818   // 4. A glued instruction may use a RegisterSDNode operand.
00819   //
00820   // Collect all the used physreg defs, and make sure that any unused physreg
00821   // defs are marked as dead.
00822   SmallVector<unsigned, 8> UsedRegs;
00823 
00824   // Additional results must be physical register defs.
00825   if (HasPhysRegOuts) {
00826     for (unsigned i = NumDefs; i < NumResults; ++i) {
00827       unsigned Reg = II.getImplicitDefs()[i - NumDefs];
00828       if (!Node->hasAnyUseOfValue(i))
00829         continue;
00830       // This implicitly defined physreg has a use.
00831       UsedRegs.push_back(Reg);
00832       EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
00833     }
00834   }
00835 
00836   // Scan the glue chain for any used physregs.
00837   if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) {
00838     for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) {
00839       if (F->getOpcode() == ISD::CopyFromReg) {
00840         UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg());
00841         continue;
00842       } else if (F->getOpcode() == ISD::CopyToReg) {
00843         // Skip CopyToReg nodes that are internal to the glue chain.
00844         continue;
00845       }
00846       // Collect declared implicit uses.
00847       const MCInstrDesc &MCID = TII->get(F->getMachineOpcode());
00848       UsedRegs.append(MCID.getImplicitUses(),
00849                       MCID.getImplicitUses() + MCID.getNumImplicitUses());
00850       // In addition to declared implicit uses, we must also check for
00851       // direct RegisterSDNode operands.
00852       for (unsigned i = 0, e = F->getNumOperands(); i != e; ++i)
00853         if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(F->getOperand(i))) {
00854           unsigned Reg = R->getReg();
00855           if (TargetRegisterInfo::isPhysicalRegister(Reg))
00856             UsedRegs.push_back(Reg);
00857         }
00858     }
00859   }
00860 
00861   // Finally mark unused registers as dead.
00862   if (!UsedRegs.empty() || II.getImplicitDefs())
00863     MIB->setPhysRegsDeadExcept(UsedRegs, *TRI);
00864 
00865   // Run post-isel target hook to adjust this instruction if needed.
00866 #ifdef NDEBUG
00867   if (II.hasPostISelHook())
00868 #endif
00869     TLI->AdjustInstrPostInstrSelection(MIB, Node);
00870 }
00871 
00872 /// EmitSpecialNode - Generate machine code for a target-independent node and
00873 /// needed dependencies.
00874 void InstrEmitter::
00875 EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
00876                 DenseMap<SDValue, unsigned> &VRBaseMap) {
00877   switch (Node->getOpcode()) {
00878   default:
00879 #ifndef NDEBUG
00880     Node->dump();
00881 #endif
00882     llvm_unreachable("This target-independent node should have been selected!");
00883   case ISD::EntryToken:
00884     llvm_unreachable("EntryToken should have been excluded from the schedule!");
00885   case ISD::MERGE_VALUES:
00886   case ISD::TokenFactor: // fall thru
00887     break;
00888   case ISD::CopyToReg: {
00889     unsigned SrcReg;
00890     SDValue SrcVal = Node->getOperand(2);
00891     if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
00892       SrcReg = R->getReg();
00893     else
00894       SrcReg = getVR(SrcVal, VRBaseMap);
00895 
00896     unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
00897     if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
00898       break;
00899 
00900     BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
00901             DestReg).addReg(SrcReg);
00902     break;
00903   }
00904   case ISD::CopyFromReg: {
00905     unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
00906     EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
00907     break;
00908   }
00909   case ISD::EH_LABEL: {
00910     MCSymbol *S = cast<EHLabelSDNode>(Node)->getLabel();
00911     BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
00912             TII->get(TargetOpcode::EH_LABEL)).addSym(S);
00913     break;
00914   }
00915 
00916   case ISD::LIFETIME_START:
00917   case ISD::LIFETIME_END: {
00918     unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START) ?
00919     TargetOpcode::LIFETIME_START : TargetOpcode::LIFETIME_END;
00920 
00921     FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Node->getOperand(1));
00922     BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
00923     .addFrameIndex(FI->getIndex());
00924     break;
00925   }
00926 
00927   case ISD::INLINEASM: {
00928     unsigned NumOps = Node->getNumOperands();
00929     if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
00930       --NumOps;  // Ignore the glue operand.
00931 
00932     // Create the inline asm machine instruction.
00933     MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(),
00934                                       TII->get(TargetOpcode::INLINEASM));
00935 
00936     // Add the asm string as an external symbol operand.
00937     SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString);
00938     const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol();
00939     MIB.addExternalSymbol(AsmStr);
00940 
00941     // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore
00942     // bits.
00943     int64_t ExtraInfo =
00944       cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))->
00945                           getZExtValue();
00946     MIB.addImm(ExtraInfo);
00947 
00948     // Remember to operand index of the group flags.
00949     SmallVector<unsigned, 8> GroupIdx;
00950 
00951     // Add all of the operand registers to the instruction.
00952     for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
00953       unsigned Flags =
00954         cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
00955       const unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
00956 
00957       GroupIdx.push_back(MIB->getNumOperands());
00958       MIB.addImm(Flags);
00959       ++i;  // Skip the ID value.
00960 
00961       switch (InlineAsm::getKind(Flags)) {
00962       default: llvm_unreachable("Bad flags!");
00963         case InlineAsm::Kind_RegDef:
00964         for (unsigned j = 0; j != NumVals; ++j, ++i) {
00965           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
00966           // FIXME: Add dead flags for physical and virtual registers defined.
00967           // For now, mark physical register defs as implicit to help fast
00968           // regalloc. This makes inline asm look a lot like calls.
00969           MIB.addReg(Reg, RegState::Define |
00970                   getImplRegState(TargetRegisterInfo::isPhysicalRegister(Reg)));
00971         }
00972         break;
00973       case InlineAsm::Kind_RegDefEarlyClobber:
00974       case InlineAsm::Kind_Clobber:
00975         for (unsigned j = 0; j != NumVals; ++j, ++i) {
00976           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
00977           MIB.addReg(Reg, RegState::Define | RegState::EarlyClobber |
00978                   getImplRegState(TargetRegisterInfo::isPhysicalRegister(Reg)));
00979         }
00980         break;
00981       case InlineAsm::Kind_RegUse:  // Use of register.
00982       case InlineAsm::Kind_Imm:  // Immediate.
00983       case InlineAsm::Kind_Mem:  // Addressing mode.
00984         // The addressing mode has been selected, just add all of the
00985         // operands to the machine instruction.
00986         for (unsigned j = 0; j != NumVals; ++j, ++i)
00987           AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap,
00988                      /*IsDebug=*/false, IsClone, IsCloned);
00989 
00990         // Manually set isTied bits.
00991         if (InlineAsm::getKind(Flags) == InlineAsm::Kind_RegUse) {
00992           unsigned DefGroup = 0;
00993           if (InlineAsm::isUseOperandTiedToDef(Flags, DefGroup)) {
00994             unsigned DefIdx = GroupIdx[DefGroup] + 1;
00995             unsigned UseIdx = GroupIdx.back() + 1;
00996             for (unsigned j = 0; j != NumVals; ++j)
00997               MIB->tieOperands(DefIdx + j, UseIdx + j);
00998           }
00999         }
01000         break;
01001       }
01002     }
01003 
01004     // Get the mdnode from the asm if it exists and add it to the instruction.
01005     SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode);
01006     const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD();
01007     if (MD)
01008       MIB.addMetadata(MD);
01009 
01010     MBB->insert(InsertPos, MIB);
01011     break;
01012   }
01013   }
01014 }
01015 
01016 /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
01017 /// at the given position in the given block.
01018 InstrEmitter::InstrEmitter(MachineBasicBlock *mbb,
01019                            MachineBasicBlock::iterator insertpos)
01020   : MF(mbb->getParent()),
01021     MRI(&MF->getRegInfo()),
01022     TM(&MF->getTarget()),
01023     TII(TM->getInstrInfo()),
01024     TRI(TM->getRegisterInfo()),
01025     TLI(TM->getTargetLowering()),
01026     MBB(mbb), InsertPos(insertpos) {
01027 }