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/TargetSubtargetInfo.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 = MF->getSubtarget().getDataLayout()->getPrefTypeAlignment(Type);
00410       if (Align == 0) {
00411         // Alignment of vector types.  FIXME!
00412         Align = MF->getSubtarget().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 *Var = SD->getVariable();
00651   MDNode *Expr = SD->getExpression();
00652   DebugLoc DL = SD->getDebugLoc();
00653 
00654   if (SD->getKind() == SDDbgValue::FRAMEIX) {
00655     // Stack address; this needs to be lowered in target-dependent fashion.
00656     // EmitTargetCodeForFrameDebugValue is responsible for allocation.
00657     return BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE))
00658         .addFrameIndex(SD->getFrameIx())
00659         .addImm(Offset)
00660         .addMetadata(Var)
00661         .addMetadata(Expr);
00662   }
00663   // Otherwise, we're going to create an instruction here.
00664   const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE);
00665   MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
00666   if (SD->getKind() == SDDbgValue::SDNODE) {
00667     SDNode *Node = SD->getSDNode();
00668     SDValue Op = SDValue(Node, SD->getResNo());
00669     // It's possible we replaced this SDNode with other(s) and therefore
00670     // didn't generate code for it.  It's better to catch these cases where
00671     // they happen and transfer the debug info, but trying to guarantee that
00672     // in all cases would be very fragile; this is a safeguard for any
00673     // that were missed.
00674     DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
00675     if (I==VRBaseMap.end())
00676       MIB.addReg(0U);       // undef
00677     else
00678       AddOperand(MIB, Op, (*MIB).getNumOperands(), &II, VRBaseMap,
00679                  /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false);
00680   } else if (SD->getKind() == SDDbgValue::CONST) {
00681     const Value *V = SD->getConst();
00682     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00683       if (CI->getBitWidth() > 64)
00684         MIB.addCImm(CI);
00685       else
00686         MIB.addImm(CI->getSExtValue());
00687     } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
00688       MIB.addFPImm(CF);
00689     } else {
00690       // Could be an Undef.  In any case insert an Undef so we can see what we
00691       // dropped.
00692       MIB.addReg(0U);
00693     }
00694   } else {
00695     // Insert an Undef so we can see what we dropped.
00696     MIB.addReg(0U);
00697   }
00698 
00699   // Indirect addressing is indicated by an Imm as the second parameter.
00700   if (SD->isIndirect())
00701     MIB.addImm(Offset);
00702   else {
00703     assert(Offset == 0 && "direct value cannot have an offset");
00704     MIB.addReg(0U, RegState::Debug);
00705   }
00706 
00707   MIB.addMetadata(Var);
00708   MIB.addMetadata(Expr);
00709 
00710   return &*MIB;
00711 }
00712 
00713 /// EmitMachineNode - Generate machine code for a target-specific node and
00714 /// needed dependencies.
00715 ///
00716 void InstrEmitter::
00717 EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
00718                 DenseMap<SDValue, unsigned> &VRBaseMap) {
00719   unsigned Opc = Node->getMachineOpcode();
00720 
00721   // Handle subreg insert/extract specially
00722   if (Opc == TargetOpcode::EXTRACT_SUBREG ||
00723       Opc == TargetOpcode::INSERT_SUBREG ||
00724       Opc == TargetOpcode::SUBREG_TO_REG) {
00725     EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned);
00726     return;
00727   }
00728 
00729   // Handle COPY_TO_REGCLASS specially.
00730   if (Opc == TargetOpcode::COPY_TO_REGCLASS) {
00731     EmitCopyToRegClassNode(Node, VRBaseMap);
00732     return;
00733   }
00734 
00735   // Handle REG_SEQUENCE specially.
00736   if (Opc == TargetOpcode::REG_SEQUENCE) {
00737     EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned);
00738     return;
00739   }
00740 
00741   if (Opc == TargetOpcode::IMPLICIT_DEF)
00742     // We want a unique VR for each IMPLICIT_DEF use.
00743     return;
00744 
00745   const MCInstrDesc &II = TII->get(Opc);
00746   unsigned NumResults = CountResults(Node);
00747   unsigned NumDefs = II.getNumDefs();
00748   const MCPhysReg *ScratchRegs = nullptr;
00749 
00750   // Handle STACKMAP and PATCHPOINT specially and then use the generic code.
00751   if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
00752     // Stackmaps do not have arguments and do not preserve their calling
00753     // convention. However, to simplify runtime support, they clobber the same
00754     // scratch registers as AnyRegCC.
00755     unsigned CC = CallingConv::AnyReg;
00756     if (Opc == TargetOpcode::PATCHPOINT) {
00757       CC = Node->getConstantOperandVal(PatchPointOpers::CCPos);
00758       NumDefs = NumResults;
00759     }
00760     ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC);
00761   }
00762 
00763   unsigned NumImpUses = 0;
00764   unsigned NodeOperands =
00765     countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses);
00766   bool HasPhysRegOuts = NumResults > NumDefs && II.getImplicitDefs()!=nullptr;
00767 #ifndef NDEBUG
00768   unsigned NumMIOperands = NodeOperands + NumResults;
00769   if (II.isVariadic())
00770     assert(NumMIOperands >= II.getNumOperands() &&
00771            "Too few operands for a variadic node!");
00772   else
00773     assert(NumMIOperands >= II.getNumOperands() &&
00774            NumMIOperands <= II.getNumOperands() + II.getNumImplicitDefs() +
00775                             NumImpUses &&
00776            "#operands for dag node doesn't match .td file!");
00777 #endif
00778 
00779   // Create the new machine instruction.
00780   MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II);
00781 
00782   // Add result register values for things that are defined by this
00783   // instruction.
00784   if (NumResults)
00785     CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap);
00786 
00787   // Emit all of the actual operands of this instruction, adding them to the
00788   // instruction as appropriate.
00789   bool HasOptPRefs = NumDefs > NumResults;
00790   assert((!HasOptPRefs || !HasPhysRegOuts) &&
00791          "Unable to cope with optional defs and phys regs defs!");
00792   unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0;
00793   for (unsigned i = NumSkip; i != NodeOperands; ++i)
00794     AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II,
00795                VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned);
00796 
00797   // Add scratch registers as implicit def and early clobber
00798   if (ScratchRegs)
00799     for (unsigned i = 0; ScratchRegs[i]; ++i)
00800       MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine |
00801                                  RegState::EarlyClobber);
00802 
00803   // Transfer all of the memory reference descriptions of this instruction.
00804   MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands_begin(),
00805                  cast<MachineSDNode>(Node)->memoperands_end());
00806 
00807   // Insert the instruction into position in the block. This needs to
00808   // happen before any custom inserter hook is called so that the
00809   // hook knows where in the block to insert the replacement code.
00810   MBB->insert(InsertPos, MIB);
00811 
00812   // The MachineInstr may also define physregs instead of virtregs.  These
00813   // physreg values can reach other instructions in different ways:
00814   //
00815   // 1. When there is a use of a Node value beyond the explicitly defined
00816   //    virtual registers, we emit a CopyFromReg for one of the implicitly
00817   //    defined physregs.  This only happens when HasPhysRegOuts is true.
00818   //
00819   // 2. A CopyFromReg reading a physreg may be glued to this instruction.
00820   //
00821   // 3. A glued instruction may implicitly use a physreg.
00822   //
00823   // 4. A glued instruction may use a RegisterSDNode operand.
00824   //
00825   // Collect all the used physreg defs, and make sure that any unused physreg
00826   // defs are marked as dead.
00827   SmallVector<unsigned, 8> UsedRegs;
00828 
00829   // Additional results must be physical register defs.
00830   if (HasPhysRegOuts) {
00831     for (unsigned i = NumDefs; i < NumResults; ++i) {
00832       unsigned Reg = II.getImplicitDefs()[i - NumDefs];
00833       if (!Node->hasAnyUseOfValue(i))
00834         continue;
00835       // This implicitly defined physreg has a use.
00836       UsedRegs.push_back(Reg);
00837       EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
00838     }
00839   }
00840 
00841   // Scan the glue chain for any used physregs.
00842   if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) {
00843     for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) {
00844       if (F->getOpcode() == ISD::CopyFromReg) {
00845         UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg());
00846         continue;
00847       } else if (F->getOpcode() == ISD::CopyToReg) {
00848         // Skip CopyToReg nodes that are internal to the glue chain.
00849         continue;
00850       }
00851       // Collect declared implicit uses.
00852       const MCInstrDesc &MCID = TII->get(F->getMachineOpcode());
00853       UsedRegs.append(MCID.getImplicitUses(),
00854                       MCID.getImplicitUses() + MCID.getNumImplicitUses());
00855       // In addition to declared implicit uses, we must also check for
00856       // direct RegisterSDNode operands.
00857       for (unsigned i = 0, e = F->getNumOperands(); i != e; ++i)
00858         if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(F->getOperand(i))) {
00859           unsigned Reg = R->getReg();
00860           if (TargetRegisterInfo::isPhysicalRegister(Reg))
00861             UsedRegs.push_back(Reg);
00862         }
00863     }
00864   }
00865 
00866   // Finally mark unused registers as dead.
00867   if (!UsedRegs.empty() || II.getImplicitDefs())
00868     MIB->setPhysRegsDeadExcept(UsedRegs, *TRI);
00869 
00870   // Run post-isel target hook to adjust this instruction if needed.
00871   if (II.hasPostISelHook())
00872     TLI->AdjustInstrPostInstrSelection(MIB, Node);
00873 }
00874 
00875 /// EmitSpecialNode - Generate machine code for a target-independent node and
00876 /// needed dependencies.
00877 void InstrEmitter::
00878 EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
00879                 DenseMap<SDValue, unsigned> &VRBaseMap) {
00880   switch (Node->getOpcode()) {
00881   default:
00882 #ifndef NDEBUG
00883     Node->dump();
00884 #endif
00885     llvm_unreachable("This target-independent node should have been selected!");
00886   case ISD::EntryToken:
00887     llvm_unreachable("EntryToken should have been excluded from the schedule!");
00888   case ISD::MERGE_VALUES:
00889   case ISD::TokenFactor: // fall thru
00890     break;
00891   case ISD::CopyToReg: {
00892     unsigned SrcReg;
00893     SDValue SrcVal = Node->getOperand(2);
00894     if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
00895       SrcReg = R->getReg();
00896     else
00897       SrcReg = getVR(SrcVal, VRBaseMap);
00898 
00899     unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
00900     if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
00901       break;
00902 
00903     BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
00904             DestReg).addReg(SrcReg);
00905     break;
00906   }
00907   case ISD::CopyFromReg: {
00908     unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
00909     EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
00910     break;
00911   }
00912   case ISD::EH_LABEL: {
00913     MCSymbol *S = cast<EHLabelSDNode>(Node)->getLabel();
00914     BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
00915             TII->get(TargetOpcode::EH_LABEL)).addSym(S);
00916     break;
00917   }
00918 
00919   case ISD::LIFETIME_START:
00920   case ISD::LIFETIME_END: {
00921     unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START) ?
00922     TargetOpcode::LIFETIME_START : TargetOpcode::LIFETIME_END;
00923 
00924     FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Node->getOperand(1));
00925     BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
00926     .addFrameIndex(FI->getIndex());
00927     break;
00928   }
00929 
00930   case ISD::INLINEASM: {
00931     unsigned NumOps = Node->getNumOperands();
00932     if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
00933       --NumOps;  // Ignore the glue operand.
00934 
00935     // Create the inline asm machine instruction.
00936     MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(),
00937                                       TII->get(TargetOpcode::INLINEASM));
00938 
00939     // Add the asm string as an external symbol operand.
00940     SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString);
00941     const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol();
00942     MIB.addExternalSymbol(AsmStr);
00943 
00944     // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore
00945     // bits.
00946     int64_t ExtraInfo =
00947       cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))->
00948                           getZExtValue();
00949     MIB.addImm(ExtraInfo);
00950 
00951     // Remember to operand index of the group flags.
00952     SmallVector<unsigned, 8> GroupIdx;
00953 
00954     // Add all of the operand registers to the instruction.
00955     for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
00956       unsigned Flags =
00957         cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
00958       const unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
00959 
00960       GroupIdx.push_back(MIB->getNumOperands());
00961       MIB.addImm(Flags);
00962       ++i;  // Skip the ID value.
00963 
00964       switch (InlineAsm::getKind(Flags)) {
00965       default: llvm_unreachable("Bad flags!");
00966         case InlineAsm::Kind_RegDef:
00967         for (unsigned j = 0; j != NumVals; ++j, ++i) {
00968           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
00969           // FIXME: Add dead flags for physical and virtual registers defined.
00970           // For now, mark physical register defs as implicit to help fast
00971           // regalloc. This makes inline asm look a lot like calls.
00972           MIB.addReg(Reg, RegState::Define |
00973                   getImplRegState(TargetRegisterInfo::isPhysicalRegister(Reg)));
00974         }
00975         break;
00976       case InlineAsm::Kind_RegDefEarlyClobber:
00977       case InlineAsm::Kind_Clobber:
00978         for (unsigned j = 0; j != NumVals; ++j, ++i) {
00979           unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
00980           MIB.addReg(Reg, RegState::Define | RegState::EarlyClobber |
00981                   getImplRegState(TargetRegisterInfo::isPhysicalRegister(Reg)));
00982         }
00983         break;
00984       case InlineAsm::Kind_RegUse:  // Use of register.
00985       case InlineAsm::Kind_Imm:  // Immediate.
00986       case InlineAsm::Kind_Mem:  // Addressing mode.
00987         // The addressing mode has been selected, just add all of the
00988         // operands to the machine instruction.
00989         for (unsigned j = 0; j != NumVals; ++j, ++i)
00990           AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap,
00991                      /*IsDebug=*/false, IsClone, IsCloned);
00992 
00993         // Manually set isTied bits.
00994         if (InlineAsm::getKind(Flags) == InlineAsm::Kind_RegUse) {
00995           unsigned DefGroup = 0;
00996           if (InlineAsm::isUseOperandTiedToDef(Flags, DefGroup)) {
00997             unsigned DefIdx = GroupIdx[DefGroup] + 1;
00998             unsigned UseIdx = GroupIdx.back() + 1;
00999             for (unsigned j = 0; j != NumVals; ++j)
01000               MIB->tieOperands(DefIdx + j, UseIdx + j);
01001           }
01002         }
01003         break;
01004       }
01005     }
01006 
01007     // Get the mdnode from the asm if it exists and add it to the instruction.
01008     SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode);
01009     const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD();
01010     if (MD)
01011       MIB.addMetadata(MD);
01012 
01013     MBB->insert(InsertPos, MIB);
01014     break;
01015   }
01016   }
01017 }
01018 
01019 /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
01020 /// at the given position in the given block.
01021 InstrEmitter::InstrEmitter(MachineBasicBlock *mbb,
01022                            MachineBasicBlock::iterator insertpos)
01023     : MF(mbb->getParent()), MRI(&MF->getRegInfo()),
01024       TII(MF->getSubtarget().getInstrInfo()),
01025       TRI(MF->getSubtarget().getRegisterInfo()),
01026       TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb),
01027       InsertPos(insertpos) {}