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