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