1 //==--- InstrEmitter.cpp - Emit MachineInstrs for the SelectionDAG class ---==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This implements the Emit routines for the SelectionDAG class, which creates 11 // MachineInstrs based on the decisions of the SelectionDAG instruction 12 // selection. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "InstrEmitter.h" 17 #include "SDNodeDbgValue.h" 18 #include "llvm/ADT/Statistic.h" 19 #include "llvm/CodeGen/MachineConstantPool.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/StackMaps.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/DebugInfo.h" 26 #include "llvm/Support/Debug.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/MathExtras.h" 29 #include "llvm/Target/TargetInstrInfo.h" 30 #include "llvm/Target/TargetLowering.h" 31 #include "llvm/Target/TargetSubtargetInfo.h" 32 using namespace llvm; 33 34 #define DEBUG_TYPE "instr-emitter" 35 36 /// MinRCSize - Smallest register class we allow when constraining virtual 37 /// registers. If satisfying all register class constraints would require 38 /// using a smaller register class, emit a COPY to a new virtual register 39 /// instead. 40 const unsigned MinRCSize = 4; 41 42 /// CountResults - The results of target nodes have register or immediate 43 /// operands first, then an optional chain, and optional glue operands (which do 44 /// not go into the resulting MachineInstr). 45 unsigned InstrEmitter::CountResults(SDNode *Node) { 46 unsigned N = Node->getNumValues(); 47 while (N && Node->getValueType(N - 1) == MVT::Glue) 48 --N; 49 if (N && Node->getValueType(N - 1) == MVT::Other) 50 --N; // Skip over chain result. 51 return N; 52 } 53 54 /// countOperands - The inputs to target nodes have any actual inputs first, 55 /// followed by an optional chain operand, then an optional glue operand. 56 /// Compute the number of actual operands that will go into the resulting 57 /// MachineInstr. 58 /// 59 /// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding 60 /// the chain and glue. These operands may be implicit on the machine instr. 61 static unsigned countOperands(SDNode *Node, unsigned NumExpUses, 62 unsigned &NumImpUses) { 63 unsigned N = Node->getNumOperands(); 64 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue) 65 --N; 66 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other) 67 --N; // Ignore chain if it exists. 68 69 // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses. 70 NumImpUses = N - NumExpUses; 71 for (unsigned I = N; I > NumExpUses; --I) { 72 if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1))) 73 continue; 74 if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1))) 75 if (TargetRegisterInfo::isPhysicalRegister(RN->getReg())) 76 continue; 77 NumImpUses = N - I; 78 break; 79 } 80 81 return N; 82 } 83 84 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an 85 /// implicit physical register output. 86 void InstrEmitter:: 87 EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned, 88 unsigned SrcReg, DenseMap<SDValue, unsigned> &VRBaseMap) { 89 unsigned VRBase = 0; 90 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) { 91 // Just use the input register directly! 92 SDValue Op(Node, ResNo); 93 if (IsClone) 94 VRBaseMap.erase(Op); 95 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second; 96 (void)isNew; // Silence compiler warning. 97 assert(isNew && "Node emitted out of order - early"); 98 return; 99 } 100 101 // If the node is only used by a CopyToReg and the dest reg is a vreg, use 102 // the CopyToReg'd destination register instead of creating a new vreg. 103 bool MatchReg = true; 104 const TargetRegisterClass *UseRC = nullptr; 105 MVT VT = Node->getSimpleValueType(ResNo); 106 107 // Stick to the preferred register classes for legal types. 108 if (TLI->isTypeLegal(VT)) 109 UseRC = TLI->getRegClassFor(VT); 110 111 if (!IsClone && !IsCloned) 112 for (SDNode *User : Node->uses()) { 113 bool Match = true; 114 if (User->getOpcode() == ISD::CopyToReg && 115 User->getOperand(2).getNode() == Node && 116 User->getOperand(2).getResNo() == ResNo) { 117 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 118 if (TargetRegisterInfo::isVirtualRegister(DestReg)) { 119 VRBase = DestReg; 120 Match = false; 121 } else if (DestReg != SrcReg) 122 Match = false; 123 } else { 124 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { 125 SDValue Op = User->getOperand(i); 126 if (Op.getNode() != Node || Op.getResNo() != ResNo) 127 continue; 128 MVT VT = Node->getSimpleValueType(Op.getResNo()); 129 if (VT == MVT::Other || VT == MVT::Glue) 130 continue; 131 Match = false; 132 if (User->isMachineOpcode()) { 133 const MCInstrDesc &II = TII->get(User->getMachineOpcode()); 134 const TargetRegisterClass *RC = nullptr; 135 if (i+II.getNumDefs() < II.getNumOperands()) { 136 RC = TRI->getAllocatableClass( 137 TII->getRegClass(II, i+II.getNumDefs(), TRI, *MF)); 138 } 139 if (!UseRC) 140 UseRC = RC; 141 else if (RC) { 142 const TargetRegisterClass *ComRC = 143 TRI->getCommonSubClass(UseRC, RC, VT.SimpleTy); 144 // If multiple uses expect disjoint register classes, we emit 145 // copies in AddRegisterOperand. 146 if (ComRC) 147 UseRC = ComRC; 148 } 149 } 150 } 151 } 152 MatchReg &= Match; 153 if (VRBase) 154 break; 155 } 156 157 const TargetRegisterClass *SrcRC = nullptr, *DstRC = nullptr; 158 SrcRC = TRI->getMinimalPhysRegClass(SrcReg, VT); 159 160 // Figure out the register class to create for the destreg. 161 if (VRBase) { 162 DstRC = MRI->getRegClass(VRBase); 163 } else if (UseRC) { 164 assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!"); 165 DstRC = UseRC; 166 } else { 167 DstRC = TLI->getRegClassFor(VT); 168 } 169 170 // If all uses are reading from the src physical register and copying the 171 // register is either impossible or very expensive, then don't create a copy. 172 if (MatchReg && SrcRC->getCopyCost() < 0) { 173 VRBase = SrcReg; 174 } else { 175 // Create the reg, emit the copy. 176 VRBase = MRI->createVirtualRegister(DstRC); 177 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 178 VRBase).addReg(SrcReg); 179 } 180 181 SDValue Op(Node, ResNo); 182 if (IsClone) 183 VRBaseMap.erase(Op); 184 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 185 (void)isNew; // Silence compiler warning. 186 assert(isNew && "Node emitted out of order - early"); 187 } 188 189 /// getDstOfCopyToRegUse - If the only use of the specified result number of 190 /// node is a CopyToReg, return its destination register. Return 0 otherwise. 191 unsigned InstrEmitter::getDstOfOnlyCopyToRegUse(SDNode *Node, 192 unsigned ResNo) const { 193 if (!Node->hasOneUse()) 194 return 0; 195 196 SDNode *User = *Node->use_begin(); 197 if (User->getOpcode() == ISD::CopyToReg && 198 User->getOperand(2).getNode() == Node && 199 User->getOperand(2).getResNo() == ResNo) { 200 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 201 if (TargetRegisterInfo::isVirtualRegister(Reg)) 202 return Reg; 203 } 204 return 0; 205 } 206 207 void InstrEmitter::CreateVirtualRegisters(SDNode *Node, 208 MachineInstrBuilder &MIB, 209 const MCInstrDesc &II, 210 bool IsClone, bool IsCloned, 211 DenseMap<SDValue, unsigned> &VRBaseMap) { 212 assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF && 213 "IMPLICIT_DEF should have been handled as a special case elsewhere!"); 214 215 unsigned NumResults = CountResults(Node); 216 for (unsigned i = 0; i < II.getNumDefs(); ++i) { 217 // If the specific node value is only used by a CopyToReg and the dest reg 218 // is a vreg in the same register class, use the CopyToReg'd destination 219 // register instead of creating a new vreg. 220 unsigned VRBase = 0; 221 const TargetRegisterClass *RC = 222 TRI->getAllocatableClass(TII->getRegClass(II, i, TRI, *MF)); 223 // Always let the value type influence the used register class. The 224 // constraints on the instruction may be too lax to represent the value 225 // type correctly. For example, a 64-bit float (X86::FR64) can't live in 226 // the 32-bit float super-class (X86::FR32). 227 if (i < NumResults && TLI->isTypeLegal(Node->getSimpleValueType(i))) { 228 const TargetRegisterClass *VTRC = 229 TLI->getRegClassFor(Node->getSimpleValueType(i)); 230 if (RC) 231 VTRC = TRI->getCommonSubClass(RC, VTRC); 232 if (VTRC) 233 RC = VTRC; 234 } 235 236 if (II.OpInfo[i].isOptionalDef()) { 237 // Optional def must be a physical register. 238 VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg(); 239 assert(TargetRegisterInfo::isPhysicalRegister(VRBase)); 240 MIB.addReg(VRBase, RegState::Define); 241 } 242 243 if (!VRBase && !IsClone && !IsCloned) 244 for (SDNode *User : Node->uses()) { 245 if (User->getOpcode() == ISD::CopyToReg && 246 User->getOperand(2).getNode() == Node && 247 User->getOperand(2).getResNo() == i) { 248 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 249 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 250 const TargetRegisterClass *RegRC = MRI->getRegClass(Reg); 251 if (RegRC == RC) { 252 VRBase = Reg; 253 MIB.addReg(VRBase, RegState::Define); 254 break; 255 } 256 } 257 } 258 } 259 260 // Create the result registers for this node and add the result regs to 261 // the machine instruction. 262 if (VRBase == 0) { 263 assert(RC && "Isn't a register operand!"); 264 VRBase = MRI->createVirtualRegister(RC); 265 MIB.addReg(VRBase, RegState::Define); 266 } 267 268 // If this def corresponds to a result of the SDNode insert the VRBase into 269 // the lookup map. 270 if (i < NumResults) { 271 SDValue Op(Node, i); 272 if (IsClone) 273 VRBaseMap.erase(Op); 274 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 275 (void)isNew; // Silence compiler warning. 276 assert(isNew && "Node emitted out of order - early"); 277 } 278 } 279 } 280 281 /// getVR - Return the virtual register corresponding to the specified result 282 /// of the specified node. 283 unsigned InstrEmitter::getVR(SDValue Op, 284 DenseMap<SDValue, unsigned> &VRBaseMap) { 285 if (Op.isMachineOpcode() && 286 Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) { 287 // Add an IMPLICIT_DEF instruction before every use. 288 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo()); 289 // IMPLICIT_DEF can produce any type of result so its MCInstrDesc 290 // does not include operand register class info. 291 if (!VReg) { 292 const TargetRegisterClass *RC = 293 TLI->getRegClassFor(Op.getSimpleValueType()); 294 VReg = MRI->createVirtualRegister(RC); 295 } 296 BuildMI(*MBB, InsertPos, Op.getDebugLoc(), 297 TII->get(TargetOpcode::IMPLICIT_DEF), VReg); 298 return VReg; 299 } 300 301 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op); 302 assert(I != VRBaseMap.end() && "Node emitted out of order - late"); 303 return I->second; 304 } 305 306 307 /// AddRegisterOperand - Add the specified register as an operand to the 308 /// specified machine instr. Insert register copies if the register is 309 /// not in the required register class. 310 void 311 InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB, 312 SDValue Op, 313 unsigned IIOpNum, 314 const MCInstrDesc *II, 315 DenseMap<SDValue, unsigned> &VRBaseMap, 316 bool IsDebug, bool IsClone, bool IsCloned) { 317 assert(Op.getValueType() != MVT::Other && 318 Op.getValueType() != MVT::Glue && 319 "Chain and glue operands should occur at end of operand list!"); 320 // Get/emit the operand. 321 unsigned VReg = getVR(Op, VRBaseMap); 322 323 const MCInstrDesc &MCID = MIB->getDesc(); 324 bool isOptDef = IIOpNum < MCID.getNumOperands() && 325 MCID.OpInfo[IIOpNum].isOptionalDef(); 326 327 // If the instruction requires a register in a different class, create 328 // a new virtual register and copy the value into it, but first attempt to 329 // shrink VReg's register class within reason. For example, if VReg == GR32 330 // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP. 331 if (II) { 332 const TargetRegisterClass *OpRC = nullptr; 333 if (IIOpNum < II->getNumOperands()) 334 OpRC = TII->getRegClass(*II, IIOpNum, TRI, *MF); 335 336 if (OpRC) { 337 const TargetRegisterClass *ConstrainedRC 338 = MRI->constrainRegClass(VReg, OpRC, MinRCSize); 339 if (!ConstrainedRC) { 340 OpRC = TRI->getAllocatableClass(OpRC); 341 assert(OpRC && "Constraints cannot be fulfilled for allocation"); 342 unsigned NewVReg = MRI->createVirtualRegister(OpRC); 343 BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(), 344 TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg); 345 VReg = NewVReg; 346 } else { 347 assert(ConstrainedRC->isAllocatable() && 348 "Constraining an allocatable VReg produced an unallocatable class?"); 349 } 350 } 351 } 352 353 // If this value has only one use, that use is a kill. This is a 354 // conservative approximation. InstrEmitter does trivial coalescing 355 // with CopyFromReg nodes, so don't emit kill flags for them. 356 // Avoid kill flags on Schedule cloned nodes, since there will be 357 // multiple uses. 358 // Tied operands are never killed, so we need to check that. And that 359 // means we need to determine the index of the operand. 360 bool isKill = Op.hasOneUse() && 361 Op.getNode()->getOpcode() != ISD::CopyFromReg && 362 !IsDebug && 363 !(IsClone || IsCloned); 364 if (isKill) { 365 unsigned Idx = MIB->getNumOperands(); 366 while (Idx > 0 && 367 MIB->getOperand(Idx-1).isReg() && 368 MIB->getOperand(Idx-1).isImplicit()) 369 --Idx; 370 bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1; 371 if (isTied) 372 isKill = false; 373 } 374 375 MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) | 376 getDebugRegState(IsDebug)); 377 } 378 379 /// AddOperand - Add the specified operand to the specified machine instr. II 380 /// specifies the instruction information for the node, and IIOpNum is the 381 /// operand number (in the II) that we are adding. 382 void InstrEmitter::AddOperand(MachineInstrBuilder &MIB, 383 SDValue Op, 384 unsigned IIOpNum, 385 const MCInstrDesc *II, 386 DenseMap<SDValue, unsigned> &VRBaseMap, 387 bool IsDebug, bool IsClone, bool IsCloned) { 388 if (Op.isMachineOpcode()) { 389 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap, 390 IsDebug, IsClone, IsCloned); 391 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 392 MIB.addImm(C->getSExtValue()); 393 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) { 394 MIB.addFPImm(F->getConstantFPValue()); 395 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) { 396 // Turn additional physreg operands into implicit uses on non-variadic 397 // instructions. This is used by call and return instructions passing 398 // arguments in registers. 399 bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic()); 400 MIB.addReg(R->getReg(), getImplRegState(Imp)); 401 } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) { 402 MIB.addRegMask(RM->getRegMask()); 403 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) { 404 MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(), 405 TGA->getTargetFlags()); 406 } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) { 407 MIB.addMBB(BBNode->getBasicBlock()); 408 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) { 409 MIB.addFrameIndex(FI->getIndex()); 410 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) { 411 MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags()); 412 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) { 413 int Offset = CP->getOffset(); 414 unsigned Align = CP->getAlignment(); 415 Type *Type = CP->getType(); 416 // MachineConstantPool wants an explicit alignment. 417 if (Align == 0) { 418 Align = MF->getDataLayout().getPrefTypeAlignment(Type); 419 if (Align == 0) { 420 // Alignment of vector types. FIXME! 421 Align = MF->getDataLayout().getTypeAllocSize(Type); 422 } 423 } 424 425 unsigned Idx; 426 MachineConstantPool *MCP = MF->getConstantPool(); 427 if (CP->isMachineConstantPoolEntry()) 428 Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Align); 429 else 430 Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Align); 431 MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags()); 432 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) { 433 MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags()); 434 } else if (auto *SymNode = dyn_cast<MCSymbolSDNode>(Op)) { 435 MIB.addSym(SymNode->getMCSymbol()); 436 } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) { 437 MIB.addBlockAddress(BA->getBlockAddress(), 438 BA->getOffset(), 439 BA->getTargetFlags()); 440 } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) { 441 MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags()); 442 } else { 443 assert(Op.getValueType() != MVT::Other && 444 Op.getValueType() != MVT::Glue && 445 "Chain and glue operands should occur at end of operand list!"); 446 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap, 447 IsDebug, IsClone, IsCloned); 448 } 449 } 450 451 unsigned InstrEmitter::ConstrainForSubReg(unsigned VReg, unsigned SubIdx, 452 MVT VT, const DebugLoc &DL) { 453 const TargetRegisterClass *VRC = MRI->getRegClass(VReg); 454 const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx); 455 456 // RC is a sub-class of VRC that supports SubIdx. Try to constrain VReg 457 // within reason. 458 if (RC && RC != VRC) 459 RC = MRI->constrainRegClass(VReg, RC, MinRCSize); 460 461 // VReg has been adjusted. It can be used with SubIdx operands now. 462 if (RC) 463 return VReg; 464 465 // VReg couldn't be reasonably constrained. Emit a COPY to a new virtual 466 // register instead. 467 RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT), SubIdx); 468 assert(RC && "No legal register class for VT supports that SubIdx"); 469 unsigned NewReg = MRI->createVirtualRegister(RC); 470 BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg) 471 .addReg(VReg); 472 return NewReg; 473 } 474 475 /// EmitSubregNode - Generate machine code for subreg nodes. 476 /// 477 void InstrEmitter::EmitSubregNode(SDNode *Node, 478 DenseMap<SDValue, unsigned> &VRBaseMap, 479 bool IsClone, bool IsCloned) { 480 unsigned VRBase = 0; 481 unsigned Opc = Node->getMachineOpcode(); 482 483 // If the node is only used by a CopyToReg and the dest reg is a vreg, use 484 // the CopyToReg'd destination register instead of creating a new vreg. 485 for (SDNode *User : Node->uses()) { 486 if (User->getOpcode() == ISD::CopyToReg && 487 User->getOperand(2).getNode() == Node) { 488 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 489 if (TargetRegisterInfo::isVirtualRegister(DestReg)) { 490 VRBase = DestReg; 491 break; 492 } 493 } 494 } 495 496 if (Opc == TargetOpcode::EXTRACT_SUBREG) { 497 // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub. There are no 498 // constraints on the %dst register, COPY can target all legal register 499 // classes. 500 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 501 const TargetRegisterClass *TRC = 502 TLI->getRegClassFor(Node->getSimpleValueType(0)); 503 504 unsigned Reg; 505 MachineInstr *DefMI; 506 RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(0)); 507 if (R && TargetRegisterInfo::isPhysicalRegister(R->getReg())) { 508 Reg = R->getReg(); 509 DefMI = nullptr; 510 } else { 511 Reg = getVR(Node->getOperand(0), VRBaseMap); 512 DefMI = MRI->getVRegDef(Reg); 513 } 514 515 unsigned SrcReg, DstReg, DefSubIdx; 516 if (DefMI && 517 TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) && 518 SubIdx == DefSubIdx && 519 TRC == MRI->getRegClass(SrcReg)) { 520 // Optimize these: 521 // r1025 = s/zext r1024, 4 522 // r1026 = extract_subreg r1025, 4 523 // to a copy 524 // r1026 = copy r1024 525 VRBase = MRI->createVirtualRegister(TRC); 526 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 527 TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg); 528 MRI->clearKillFlags(SrcReg); 529 } else { 530 // Reg may not support a SubIdx sub-register, and we may need to 531 // constrain its register class or issue a COPY to a compatible register 532 // class. 533 if (TargetRegisterInfo::isVirtualRegister(Reg)) 534 Reg = ConstrainForSubReg(Reg, SubIdx, 535 Node->getOperand(0).getSimpleValueType(), 536 Node->getDebugLoc()); 537 538 // Create the destreg if it is missing. 539 if (VRBase == 0) 540 VRBase = MRI->createVirtualRegister(TRC); 541 542 // Create the extract_subreg machine instruction. 543 MachineInstrBuilder CopyMI = 544 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 545 TII->get(TargetOpcode::COPY), VRBase); 546 if (TargetRegisterInfo::isVirtualRegister(Reg)) 547 CopyMI.addReg(Reg, 0, SubIdx); 548 else 549 CopyMI.addReg(TRI->getSubReg(Reg, SubIdx)); 550 } 551 } else if (Opc == TargetOpcode::INSERT_SUBREG || 552 Opc == TargetOpcode::SUBREG_TO_REG) { 553 SDValue N0 = Node->getOperand(0); 554 SDValue N1 = Node->getOperand(1); 555 SDValue N2 = Node->getOperand(2); 556 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue(); 557 558 // Figure out the register class to create for the destreg. It should be 559 // the largest legal register class supporting SubIdx sub-registers. 560 // RegisterCoalescer will constrain it further if it decides to eliminate 561 // the INSERT_SUBREG instruction. 562 // 563 // %dst = INSERT_SUBREG %src, %sub, SubIdx 564 // 565 // is lowered by TwoAddressInstructionPass to: 566 // 567 // %dst = COPY %src 568 // %dst:SubIdx = COPY %sub 569 // 570 // There is no constraint on the %src register class. 571 // 572 const TargetRegisterClass *SRC = TLI->getRegClassFor(Node->getSimpleValueType(0)); 573 SRC = TRI->getSubClassWithSubReg(SRC, SubIdx); 574 assert(SRC && "No register class supports VT and SubIdx for INSERT_SUBREG"); 575 576 if (VRBase == 0 || !SRC->hasSubClassEq(MRI->getRegClass(VRBase))) 577 VRBase = MRI->createVirtualRegister(SRC); 578 579 // Create the insert_subreg or subreg_to_reg machine instruction. 580 MachineInstrBuilder MIB = 581 BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc), VRBase); 582 583 // If creating a subreg_to_reg, then the first input operand 584 // is an implicit value immediate, otherwise it's a register 585 if (Opc == TargetOpcode::SUBREG_TO_REG) { 586 const ConstantSDNode *SD = cast<ConstantSDNode>(N0); 587 MIB.addImm(SD->getZExtValue()); 588 } else 589 AddOperand(MIB, N0, 0, nullptr, VRBaseMap, /*IsDebug=*/false, 590 IsClone, IsCloned); 591 // Add the subregster being inserted 592 AddOperand(MIB, N1, 0, nullptr, VRBaseMap, /*IsDebug=*/false, 593 IsClone, IsCloned); 594 MIB.addImm(SubIdx); 595 MBB->insert(InsertPos, MIB); 596 } else 597 llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg"); 598 599 SDValue Op(Node, 0); 600 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 601 (void)isNew; // Silence compiler warning. 602 assert(isNew && "Node emitted out of order - early"); 603 } 604 605 /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes. 606 /// COPY_TO_REGCLASS is just a normal copy, except that the destination 607 /// register is constrained to be in a particular register class. 608 /// 609 void 610 InstrEmitter::EmitCopyToRegClassNode(SDNode *Node, 611 DenseMap<SDValue, unsigned> &VRBaseMap) { 612 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap); 613 614 // Create the new VReg in the destination class and emit a copy. 615 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 616 const TargetRegisterClass *DstRC = 617 TRI->getAllocatableClass(TRI->getRegClass(DstRCIdx)); 618 unsigned NewVReg = MRI->createVirtualRegister(DstRC); 619 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 620 NewVReg).addReg(VReg); 621 622 SDValue Op(Node, 0); 623 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second; 624 (void)isNew; // Silence compiler warning. 625 assert(isNew && "Node emitted out of order - early"); 626 } 627 628 /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes. 629 /// 630 void InstrEmitter::EmitRegSequence(SDNode *Node, 631 DenseMap<SDValue, unsigned> &VRBaseMap, 632 bool IsClone, bool IsCloned) { 633 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue(); 634 const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx); 635 unsigned NewVReg = MRI->createVirtualRegister(TRI->getAllocatableClass(RC)); 636 const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE); 637 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg); 638 unsigned NumOps = Node->getNumOperands(); 639 assert((NumOps & 1) == 1 && 640 "REG_SEQUENCE must have an odd number of operands!"); 641 for (unsigned i = 1; i != NumOps; ++i) { 642 SDValue Op = Node->getOperand(i); 643 if ((i & 1) == 0) { 644 RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(i-1)); 645 // Skip physical registers as they don't have a vreg to get and we'll 646 // insert copies for them in TwoAddressInstructionPass anyway. 647 if (!R || !TargetRegisterInfo::isPhysicalRegister(R->getReg())) { 648 unsigned SubIdx = cast<ConstantSDNode>(Op)->getZExtValue(); 649 unsigned SubReg = getVR(Node->getOperand(i-1), VRBaseMap); 650 const TargetRegisterClass *TRC = MRI->getRegClass(SubReg); 651 const TargetRegisterClass *SRC = 652 TRI->getMatchingSuperRegClass(RC, TRC, SubIdx); 653 if (SRC && SRC != RC) { 654 MRI->setRegClass(NewVReg, SRC); 655 RC = SRC; 656 } 657 } 658 } 659 AddOperand(MIB, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false, 660 IsClone, IsCloned); 661 } 662 663 MBB->insert(InsertPos, MIB); 664 SDValue Op(Node, 0); 665 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second; 666 (void)isNew; // Silence compiler warning. 667 assert(isNew && "Node emitted out of order - early"); 668 } 669 670 /// EmitDbgValue - Generate machine instruction for a dbg_value node. 671 /// 672 MachineInstr * 673 InstrEmitter::EmitDbgValue(SDDbgValue *SD, 674 DenseMap<SDValue, unsigned> &VRBaseMap) { 675 uint64_t Offset = SD->getOffset(); 676 MDNode *Var = SD->getVariable(); 677 MDNode *Expr = SD->getExpression(); 678 DebugLoc DL = SD->getDebugLoc(); 679 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) && 680 "Expected inlined-at fields to agree"); 681 682 if (SD->getKind() == SDDbgValue::FRAMEIX) { 683 // Stack address; this needs to be lowered in target-dependent fashion. 684 // EmitTargetCodeForFrameDebugValue is responsible for allocation. 685 return BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE)) 686 .addFrameIndex(SD->getFrameIx()) 687 .addImm(Offset) 688 .addMetadata(Var) 689 .addMetadata(Expr); 690 } 691 // Otherwise, we're going to create an instruction here. 692 const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE); 693 MachineInstrBuilder MIB = BuildMI(*MF, DL, II); 694 if (SD->getKind() == SDDbgValue::SDNODE) { 695 SDNode *Node = SD->getSDNode(); 696 SDValue Op = SDValue(Node, SD->getResNo()); 697 // It's possible we replaced this SDNode with other(s) and therefore 698 // didn't generate code for it. It's better to catch these cases where 699 // they happen and transfer the debug info, but trying to guarantee that 700 // in all cases would be very fragile; this is a safeguard for any 701 // that were missed. 702 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op); 703 if (I==VRBaseMap.end()) 704 MIB.addReg(0U); // undef 705 else 706 AddOperand(MIB, Op, (*MIB).getNumOperands(), &II, VRBaseMap, 707 /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false); 708 } else if (SD->getKind() == SDDbgValue::CONST) { 709 const Value *V = SD->getConst(); 710 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 711 if (CI->getBitWidth() > 64) 712 MIB.addCImm(CI); 713 else 714 MIB.addImm(CI->getSExtValue()); 715 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { 716 MIB.addFPImm(CF); 717 } else { 718 // Could be an Undef. In any case insert an Undef so we can see what we 719 // dropped. 720 MIB.addReg(0U); 721 } 722 } else { 723 // Insert an Undef so we can see what we dropped. 724 MIB.addReg(0U); 725 } 726 727 // Indirect addressing is indicated by an Imm as the second parameter. 728 if (SD->isIndirect()) 729 MIB.addImm(Offset); 730 else { 731 assert(Offset == 0 && "direct value cannot have an offset"); 732 MIB.addReg(0U, RegState::Debug); 733 } 734 735 MIB.addMetadata(Var); 736 MIB.addMetadata(Expr); 737 738 return &*MIB; 739 } 740 741 /// EmitMachineNode - Generate machine code for a target-specific node and 742 /// needed dependencies. 743 /// 744 void InstrEmitter:: 745 EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned, 746 DenseMap<SDValue, unsigned> &VRBaseMap) { 747 unsigned Opc = Node->getMachineOpcode(); 748 749 // Handle subreg insert/extract specially 750 if (Opc == TargetOpcode::EXTRACT_SUBREG || 751 Opc == TargetOpcode::INSERT_SUBREG || 752 Opc == TargetOpcode::SUBREG_TO_REG) { 753 EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned); 754 return; 755 } 756 757 // Handle COPY_TO_REGCLASS specially. 758 if (Opc == TargetOpcode::COPY_TO_REGCLASS) { 759 EmitCopyToRegClassNode(Node, VRBaseMap); 760 return; 761 } 762 763 // Handle REG_SEQUENCE specially. 764 if (Opc == TargetOpcode::REG_SEQUENCE) { 765 EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned); 766 return; 767 } 768 769 if (Opc == TargetOpcode::IMPLICIT_DEF) 770 // We want a unique VR for each IMPLICIT_DEF use. 771 return; 772 773 const MCInstrDesc &II = TII->get(Opc); 774 unsigned NumResults = CountResults(Node); 775 unsigned NumDefs = II.getNumDefs(); 776 const MCPhysReg *ScratchRegs = nullptr; 777 778 // Handle STACKMAP and PATCHPOINT specially and then use the generic code. 779 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) { 780 // Stackmaps do not have arguments and do not preserve their calling 781 // convention. However, to simplify runtime support, they clobber the same 782 // scratch registers as AnyRegCC. 783 unsigned CC = CallingConv::AnyReg; 784 if (Opc == TargetOpcode::PATCHPOINT) { 785 CC = Node->getConstantOperandVal(PatchPointOpers::CCPos); 786 NumDefs = NumResults; 787 } 788 ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC); 789 } 790 791 unsigned NumImpUses = 0; 792 unsigned NodeOperands = 793 countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses); 794 bool HasPhysRegOuts = NumResults > NumDefs && II.getImplicitDefs()!=nullptr; 795 #ifndef NDEBUG 796 unsigned NumMIOperands = NodeOperands + NumResults; 797 if (II.isVariadic()) 798 assert(NumMIOperands >= II.getNumOperands() && 799 "Too few operands for a variadic node!"); 800 else 801 assert(NumMIOperands >= II.getNumOperands() && 802 NumMIOperands <= II.getNumOperands() + II.getNumImplicitDefs() + 803 NumImpUses && 804 "#operands for dag node doesn't match .td file!"); 805 #endif 806 807 // Create the new machine instruction. 808 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II); 809 810 // Add result register values for things that are defined by this 811 // instruction. 812 if (NumResults) 813 CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap); 814 815 // Emit all of the actual operands of this instruction, adding them to the 816 // instruction as appropriate. 817 bool HasOptPRefs = NumDefs > NumResults; 818 assert((!HasOptPRefs || !HasPhysRegOuts) && 819 "Unable to cope with optional defs and phys regs defs!"); 820 unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0; 821 for (unsigned i = NumSkip; i != NodeOperands; ++i) 822 AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II, 823 VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned); 824 825 // Add scratch registers as implicit def and early clobber 826 if (ScratchRegs) 827 for (unsigned i = 0; ScratchRegs[i]; ++i) 828 MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine | 829 RegState::EarlyClobber); 830 831 // Transfer all of the memory reference descriptions of this instruction. 832 MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands_begin(), 833 cast<MachineSDNode>(Node)->memoperands_end()); 834 835 // Insert the instruction into position in the block. This needs to 836 // happen before any custom inserter hook is called so that the 837 // hook knows where in the block to insert the replacement code. 838 MBB->insert(InsertPos, MIB); 839 840 // The MachineInstr may also define physregs instead of virtregs. These 841 // physreg values can reach other instructions in different ways: 842 // 843 // 1. When there is a use of a Node value beyond the explicitly defined 844 // virtual registers, we emit a CopyFromReg for one of the implicitly 845 // defined physregs. This only happens when HasPhysRegOuts is true. 846 // 847 // 2. A CopyFromReg reading a physreg may be glued to this instruction. 848 // 849 // 3. A glued instruction may implicitly use a physreg. 850 // 851 // 4. A glued instruction may use a RegisterSDNode operand. 852 // 853 // Collect all the used physreg defs, and make sure that any unused physreg 854 // defs are marked as dead. 855 SmallVector<unsigned, 8> UsedRegs; 856 857 // Additional results must be physical register defs. 858 if (HasPhysRegOuts) { 859 for (unsigned i = NumDefs; i < NumResults; ++i) { 860 unsigned Reg = II.getImplicitDefs()[i - NumDefs]; 861 if (!Node->hasAnyUseOfValue(i)) 862 continue; 863 // This implicitly defined physreg has a use. 864 UsedRegs.push_back(Reg); 865 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap); 866 } 867 } 868 869 // Scan the glue chain for any used physregs. 870 if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) { 871 for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) { 872 if (F->getOpcode() == ISD::CopyFromReg) { 873 UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg()); 874 continue; 875 } else if (F->getOpcode() == ISD::CopyToReg) { 876 // Skip CopyToReg nodes that are internal to the glue chain. 877 continue; 878 } 879 // Collect declared implicit uses. 880 const MCInstrDesc &MCID = TII->get(F->getMachineOpcode()); 881 UsedRegs.append(MCID.getImplicitUses(), 882 MCID.getImplicitUses() + MCID.getNumImplicitUses()); 883 // In addition to declared implicit uses, we must also check for 884 // direct RegisterSDNode operands. 885 for (unsigned i = 0, e = F->getNumOperands(); i != e; ++i) 886 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(F->getOperand(i))) { 887 unsigned Reg = R->getReg(); 888 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 889 UsedRegs.push_back(Reg); 890 } 891 } 892 } 893 894 // Finally mark unused registers as dead. 895 if (!UsedRegs.empty() || II.getImplicitDefs()) 896 MIB->setPhysRegsDeadExcept(UsedRegs, *TRI); 897 898 // Run post-isel target hook to adjust this instruction if needed. 899 if (II.hasPostISelHook()) 900 TLI->AdjustInstrPostInstrSelection(*MIB, Node); 901 } 902 903 /// EmitSpecialNode - Generate machine code for a target-independent node and 904 /// needed dependencies. 905 void InstrEmitter:: 906 EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned, 907 DenseMap<SDValue, unsigned> &VRBaseMap) { 908 switch (Node->getOpcode()) { 909 default: 910 #ifndef NDEBUG 911 Node->dump(); 912 #endif 913 llvm_unreachable("This target-independent node should have been selected!"); 914 case ISD::EntryToken: 915 llvm_unreachable("EntryToken should have been excluded from the schedule!"); 916 case ISD::MERGE_VALUES: 917 case ISD::TokenFactor: // fall thru 918 break; 919 case ISD::CopyToReg: { 920 unsigned SrcReg; 921 SDValue SrcVal = Node->getOperand(2); 922 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal)) 923 SrcReg = R->getReg(); 924 else 925 SrcReg = getVR(SrcVal, VRBaseMap); 926 927 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 928 if (SrcReg == DestReg) // Coalesced away the copy? Ignore. 929 break; 930 931 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 932 DestReg).addReg(SrcReg); 933 break; 934 } 935 case ISD::CopyFromReg: { 936 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 937 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap); 938 break; 939 } 940 case ISD::EH_LABEL: { 941 MCSymbol *S = cast<EHLabelSDNode>(Node)->getLabel(); 942 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 943 TII->get(TargetOpcode::EH_LABEL)).addSym(S); 944 break; 945 } 946 947 case ISD::LIFETIME_START: 948 case ISD::LIFETIME_END: { 949 unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START) ? 950 TargetOpcode::LIFETIME_START : TargetOpcode::LIFETIME_END; 951 952 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Node->getOperand(1)); 953 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp)) 954 .addFrameIndex(FI->getIndex()); 955 break; 956 } 957 958 case ISD::INLINEASM: { 959 unsigned NumOps = Node->getNumOperands(); 960 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue) 961 --NumOps; // Ignore the glue operand. 962 963 // Create the inline asm machine instruction. 964 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), 965 TII->get(TargetOpcode::INLINEASM)); 966 967 // Add the asm string as an external symbol operand. 968 SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString); 969 const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol(); 970 MIB.addExternalSymbol(AsmStr); 971 972 // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore 973 // bits. 974 int64_t ExtraInfo = 975 cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))-> 976 getZExtValue(); 977 MIB.addImm(ExtraInfo); 978 979 // Remember to operand index of the group flags. 980 SmallVector<unsigned, 8> GroupIdx; 981 982 // Remember registers that are part of early-clobber defs. 983 SmallVector<unsigned, 8> ECRegs; 984 985 // Add all of the operand registers to the instruction. 986 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) { 987 unsigned Flags = 988 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); 989 const unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 990 991 GroupIdx.push_back(MIB->getNumOperands()); 992 MIB.addImm(Flags); 993 ++i; // Skip the ID value. 994 995 switch (InlineAsm::getKind(Flags)) { 996 default: llvm_unreachable("Bad flags!"); 997 case InlineAsm::Kind_RegDef: 998 for (unsigned j = 0; j != NumVals; ++j, ++i) { 999 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 1000 // FIXME: Add dead flags for physical and virtual registers defined. 1001 // For now, mark physical register defs as implicit to help fast 1002 // regalloc. This makes inline asm look a lot like calls. 1003 MIB.addReg(Reg, RegState::Define | 1004 getImplRegState(TargetRegisterInfo::isPhysicalRegister(Reg))); 1005 } 1006 break; 1007 case InlineAsm::Kind_RegDefEarlyClobber: 1008 case InlineAsm::Kind_Clobber: 1009 for (unsigned j = 0; j != NumVals; ++j, ++i) { 1010 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 1011 MIB.addReg(Reg, RegState::Define | RegState::EarlyClobber | 1012 getImplRegState(TargetRegisterInfo::isPhysicalRegister(Reg))); 1013 ECRegs.push_back(Reg); 1014 } 1015 break; 1016 case InlineAsm::Kind_RegUse: // Use of register. 1017 case InlineAsm::Kind_Imm: // Immediate. 1018 case InlineAsm::Kind_Mem: // Addressing mode. 1019 // The addressing mode has been selected, just add all of the 1020 // operands to the machine instruction. 1021 for (unsigned j = 0; j != NumVals; ++j, ++i) 1022 AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap, 1023 /*IsDebug=*/false, IsClone, IsCloned); 1024 1025 // Manually set isTied bits. 1026 if (InlineAsm::getKind(Flags) == InlineAsm::Kind_RegUse) { 1027 unsigned DefGroup = 0; 1028 if (InlineAsm::isUseOperandTiedToDef(Flags, DefGroup)) { 1029 unsigned DefIdx = GroupIdx[DefGroup] + 1; 1030 unsigned UseIdx = GroupIdx.back() + 1; 1031 for (unsigned j = 0; j != NumVals; ++j) 1032 MIB->tieOperands(DefIdx + j, UseIdx + j); 1033 } 1034 } 1035 break; 1036 } 1037 } 1038 1039 // GCC inline assembly allows input operands to also be early-clobber 1040 // output operands (so long as the operand is written only after it's 1041 // used), but this does not match the semantics of our early-clobber flag. 1042 // If an early-clobber operand register is also an input operand register, 1043 // then remove the early-clobber flag. 1044 for (unsigned Reg : ECRegs) { 1045 if (MIB->readsRegister(Reg, TRI)) { 1046 MachineOperand *MO = MIB->findRegisterDefOperand(Reg, false, TRI); 1047 assert(MO && "No def operand for clobbered register?"); 1048 MO->setIsEarlyClobber(false); 1049 } 1050 } 1051 1052 // Get the mdnode from the asm if it exists and add it to the instruction. 1053 SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode); 1054 const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD(); 1055 if (MD) 1056 MIB.addMetadata(MD); 1057 1058 MBB->insert(InsertPos, MIB); 1059 break; 1060 } 1061 } 1062 } 1063 1064 /// InstrEmitter - Construct an InstrEmitter and set it to start inserting 1065 /// at the given position in the given block. 1066 InstrEmitter::InstrEmitter(MachineBasicBlock *mbb, 1067 MachineBasicBlock::iterator insertpos) 1068 : MF(mbb->getParent()), MRI(&MF->getRegInfo()), 1069 TII(MF->getSubtarget().getInstrInfo()), 1070 TRI(MF->getSubtarget().getRegisterInfo()), 1071 TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb), 1072 InsertPos(insertpos) {} 1073