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/BinaryFormat/Dwarf.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/SelectionDAG.h" 24 #include "llvm/CodeGen/StackMaps.h" 25 #include "llvm/CodeGen/TargetInstrInfo.h" 26 #include "llvm/CodeGen/TargetLowering.h" 27 #include "llvm/CodeGen/TargetSubtargetInfo.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/DebugInfo.h" 30 #include "llvm/IR/PseudoProbe.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/MathExtras.h" 34 #include "llvm/Target/TargetMachine.h" 35 using namespace llvm; 36 37 #define DEBUG_TYPE "instr-emitter" 38 39 /// MinRCSize - Smallest register class we allow when constraining virtual 40 /// registers. If satisfying all register class constraints would require 41 /// using a smaller register class, emit a COPY to a new virtual register 42 /// instead. 43 const unsigned MinRCSize = 4; 44 45 /// CountResults - The results of target nodes have register or immediate 46 /// operands first, then an optional chain, and optional glue operands (which do 47 /// not go into the resulting MachineInstr). 48 unsigned InstrEmitter::CountResults(SDNode *Node) { 49 unsigned N = Node->getNumValues(); 50 while (N && Node->getValueType(N - 1) == MVT::Glue) 51 --N; 52 if (N && Node->getValueType(N - 1) == MVT::Other) 53 --N; // Skip over chain result. 54 return N; 55 } 56 57 /// countOperands - The inputs to target nodes have any actual inputs first, 58 /// followed by an optional chain operand, then an optional glue operand. 59 /// Compute the number of actual operands that will go into the resulting 60 /// MachineInstr. 61 /// 62 /// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding 63 /// the chain and glue. These operands may be implicit on the machine instr. 64 static unsigned countOperands(SDNode *Node, unsigned NumExpUses, 65 unsigned &NumImpUses) { 66 unsigned N = Node->getNumOperands(); 67 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue) 68 --N; 69 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other) 70 --N; // Ignore chain if it exists. 71 72 // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses. 73 NumImpUses = N - NumExpUses; 74 for (unsigned I = N; I > NumExpUses; --I) { 75 if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1))) 76 continue; 77 if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1))) 78 if (Register::isPhysicalRegister(RN->getReg())) 79 continue; 80 NumImpUses = N - I; 81 break; 82 } 83 84 return N; 85 } 86 87 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an 88 /// implicit physical register output. 89 void InstrEmitter:: 90 EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned, 91 Register SrcReg, DenseMap<SDValue, Register> &VRBaseMap) { 92 Register VRBase; 93 if (SrcReg.isVirtual()) { 94 // Just use the input register directly! 95 SDValue Op(Node, ResNo); 96 if (IsClone) 97 VRBaseMap.erase(Op); 98 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second; 99 (void)isNew; // Silence compiler warning. 100 assert(isNew && "Node emitted out of order - early"); 101 return; 102 } 103 104 // If the node is only used by a CopyToReg and the dest reg is a vreg, use 105 // the CopyToReg'd destination register instead of creating a new vreg. 106 bool MatchReg = true; 107 const TargetRegisterClass *UseRC = nullptr; 108 MVT VT = Node->getSimpleValueType(ResNo); 109 110 // Stick to the preferred register classes for legal types. 111 if (TLI->isTypeLegal(VT)) 112 UseRC = TLI->getRegClassFor(VT, Node->isDivergent()); 113 114 if (!IsClone && !IsCloned) 115 for (SDNode *User : Node->uses()) { 116 bool Match = true; 117 if (User->getOpcode() == ISD::CopyToReg && 118 User->getOperand(2).getNode() == Node && 119 User->getOperand(2).getResNo() == ResNo) { 120 Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 121 if (DestReg.isVirtual()) { 122 VRBase = DestReg; 123 Match = false; 124 } else if (DestReg != SrcReg) 125 Match = false; 126 } else { 127 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { 128 SDValue Op = User->getOperand(i); 129 if (Op.getNode() != Node || Op.getResNo() != ResNo) 130 continue; 131 MVT VT = Node->getSimpleValueType(Op.getResNo()); 132 if (VT == MVT::Other || VT == MVT::Glue) 133 continue; 134 Match = false; 135 if (User->isMachineOpcode()) { 136 const MCInstrDesc &II = TII->get(User->getMachineOpcode()); 137 const TargetRegisterClass *RC = nullptr; 138 if (i+II.getNumDefs() < II.getNumOperands()) { 139 RC = TRI->getAllocatableClass( 140 TII->getRegClass(II, i+II.getNumDefs(), TRI, *MF)); 141 } 142 if (!UseRC) 143 UseRC = RC; 144 else if (RC) { 145 const TargetRegisterClass *ComRC = 146 TRI->getCommonSubClass(UseRC, RC); 147 // If multiple uses expect disjoint register classes, we emit 148 // copies in AddRegisterOperand. 149 if (ComRC) 150 UseRC = ComRC; 151 } 152 } 153 } 154 } 155 MatchReg &= Match; 156 if (VRBase) 157 break; 158 } 159 160 const TargetRegisterClass *SrcRC = nullptr, *DstRC = nullptr; 161 SrcRC = TRI->getMinimalPhysRegClass(SrcReg, VT); 162 163 // Figure out the register class to create for the destreg. 164 if (VRBase) { 165 DstRC = MRI->getRegClass(VRBase); 166 } else if (UseRC) { 167 assert(TRI->isTypeLegalForClass(*UseRC, VT) && 168 "Incompatible phys register def and uses!"); 169 DstRC = UseRC; 170 } else 171 DstRC = SrcRC; 172 173 // If all uses are reading from the src physical register and copying the 174 // register is either impossible or very expensive, then don't create a copy. 175 if (MatchReg && SrcRC->getCopyCost() < 0) { 176 VRBase = SrcReg; 177 } else { 178 // Create the reg, emit the copy. 179 VRBase = MRI->createVirtualRegister(DstRC); 180 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 181 VRBase).addReg(SrcReg); 182 } 183 184 SDValue Op(Node, ResNo); 185 if (IsClone) 186 VRBaseMap.erase(Op); 187 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 188 (void)isNew; // Silence compiler warning. 189 assert(isNew && "Node emitted out of order - early"); 190 } 191 192 void InstrEmitter::CreateVirtualRegisters(SDNode *Node, 193 MachineInstrBuilder &MIB, 194 const MCInstrDesc &II, 195 bool IsClone, bool IsCloned, 196 DenseMap<SDValue, Register> &VRBaseMap) { 197 assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF && 198 "IMPLICIT_DEF should have been handled as a special case elsewhere!"); 199 200 unsigned NumResults = CountResults(Node); 201 bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() && 202 II.isVariadic() && II.variadicOpsAreDefs(); 203 unsigned NumVRegs = HasVRegVariadicDefs ? NumResults : II.getNumDefs(); 204 if (Node->getMachineOpcode() == TargetOpcode::STATEPOINT) 205 NumVRegs = NumResults; 206 for (unsigned i = 0; i < NumVRegs; ++i) { 207 // If the specific node value is only used by a CopyToReg and the dest reg 208 // is a vreg in the same register class, use the CopyToReg'd destination 209 // register instead of creating a new vreg. 210 Register VRBase; 211 const TargetRegisterClass *RC = 212 TRI->getAllocatableClass(TII->getRegClass(II, i, TRI, *MF)); 213 // Always let the value type influence the used register class. The 214 // constraints on the instruction may be too lax to represent the value 215 // type correctly. For example, a 64-bit float (X86::FR64) can't live in 216 // the 32-bit float super-class (X86::FR32). 217 if (i < NumResults && TLI->isTypeLegal(Node->getSimpleValueType(i))) { 218 const TargetRegisterClass *VTRC = TLI->getRegClassFor( 219 Node->getSimpleValueType(i), 220 (Node->isDivergent() || (RC && TRI->isDivergentRegClass(RC)))); 221 if (RC) 222 VTRC = TRI->getCommonSubClass(RC, VTRC); 223 if (VTRC) 224 RC = VTRC; 225 } 226 227 if (II.OpInfo != nullptr && II.OpInfo[i].isOptionalDef()) { 228 // Optional def must be a physical register. 229 VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg(); 230 assert(VRBase.isPhysical()); 231 MIB.addReg(VRBase, RegState::Define); 232 } 233 234 if (!VRBase && !IsClone && !IsCloned) 235 for (SDNode *User : Node->uses()) { 236 if (User->getOpcode() == ISD::CopyToReg && 237 User->getOperand(2).getNode() == Node && 238 User->getOperand(2).getResNo() == i) { 239 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 240 if (Register::isVirtualRegister(Reg)) { 241 const TargetRegisterClass *RegRC = MRI->getRegClass(Reg); 242 if (RegRC == RC) { 243 VRBase = Reg; 244 MIB.addReg(VRBase, RegState::Define); 245 break; 246 } 247 } 248 } 249 } 250 251 // Create the result registers for this node and add the result regs to 252 // the machine instruction. 253 if (VRBase == 0) { 254 assert(RC && "Isn't a register operand!"); 255 VRBase = MRI->createVirtualRegister(RC); 256 MIB.addReg(VRBase, RegState::Define); 257 } 258 259 // If this def corresponds to a result of the SDNode insert the VRBase into 260 // the lookup map. 261 if (i < NumResults) { 262 SDValue Op(Node, i); 263 if (IsClone) 264 VRBaseMap.erase(Op); 265 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 266 (void)isNew; // Silence compiler warning. 267 assert(isNew && "Node emitted out of order - early"); 268 } 269 } 270 } 271 272 /// getVR - Return the virtual register corresponding to the specified result 273 /// of the specified node. 274 Register InstrEmitter::getVR(SDValue Op, 275 DenseMap<SDValue, Register> &VRBaseMap) { 276 if (Op.isMachineOpcode() && 277 Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) { 278 // Add an IMPLICIT_DEF instruction before every use. 279 // IMPLICIT_DEF can produce any type of result so its MCInstrDesc 280 // does not include operand register class info. 281 const TargetRegisterClass *RC = TLI->getRegClassFor( 282 Op.getSimpleValueType(), Op.getNode()->isDivergent()); 283 Register VReg = MRI->createVirtualRegister(RC); 284 BuildMI(*MBB, InsertPos, Op.getDebugLoc(), 285 TII->get(TargetOpcode::IMPLICIT_DEF), VReg); 286 return VReg; 287 } 288 289 DenseMap<SDValue, Register>::iterator I = VRBaseMap.find(Op); 290 assert(I != VRBaseMap.end() && "Node emitted out of order - late"); 291 return I->second; 292 } 293 294 295 /// AddRegisterOperand - Add the specified register as an operand to the 296 /// specified machine instr. Insert register copies if the register is 297 /// not in the required register class. 298 void 299 InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB, 300 SDValue Op, 301 unsigned IIOpNum, 302 const MCInstrDesc *II, 303 DenseMap<SDValue, Register> &VRBaseMap, 304 bool IsDebug, bool IsClone, bool IsCloned) { 305 assert(Op.getValueType() != MVT::Other && 306 Op.getValueType() != MVT::Glue && 307 "Chain and glue operands should occur at end of operand list!"); 308 // Get/emit the operand. 309 Register VReg = getVR(Op, VRBaseMap); 310 311 const MCInstrDesc &MCID = MIB->getDesc(); 312 bool isOptDef = IIOpNum < MCID.getNumOperands() && 313 MCID.OpInfo[IIOpNum].isOptionalDef(); 314 315 // If the instruction requires a register in a different class, create 316 // a new virtual register and copy the value into it, but first attempt to 317 // shrink VReg's register class within reason. For example, if VReg == GR32 318 // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP. 319 if (II) { 320 const TargetRegisterClass *OpRC = nullptr; 321 if (IIOpNum < II->getNumOperands()) 322 OpRC = TII->getRegClass(*II, IIOpNum, TRI, *MF); 323 324 if (OpRC) { 325 const TargetRegisterClass *ConstrainedRC 326 = MRI->constrainRegClass(VReg, OpRC, MinRCSize); 327 if (!ConstrainedRC) { 328 OpRC = TRI->getAllocatableClass(OpRC); 329 assert(OpRC && "Constraints cannot be fulfilled for allocation"); 330 Register NewVReg = MRI->createVirtualRegister(OpRC); 331 BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(), 332 TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg); 333 VReg = NewVReg; 334 } else { 335 assert(ConstrainedRC->isAllocatable() && 336 "Constraining an allocatable VReg produced an unallocatable class?"); 337 } 338 } 339 } 340 341 // If this value has only one use, that use is a kill. This is a 342 // conservative approximation. InstrEmitter does trivial coalescing 343 // with CopyFromReg nodes, so don't emit kill flags for them. 344 // Avoid kill flags on Schedule cloned nodes, since there will be 345 // multiple uses. 346 // Tied operands are never killed, so we need to check that. And that 347 // means we need to determine the index of the operand. 348 bool isKill = Op.hasOneUse() && 349 Op.getNode()->getOpcode() != ISD::CopyFromReg && 350 !IsDebug && 351 !(IsClone || IsCloned); 352 if (isKill) { 353 unsigned Idx = MIB->getNumOperands(); 354 while (Idx > 0 && 355 MIB->getOperand(Idx-1).isReg() && 356 MIB->getOperand(Idx-1).isImplicit()) 357 --Idx; 358 bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1; 359 if (isTied) 360 isKill = false; 361 } 362 363 MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) | 364 getDebugRegState(IsDebug)); 365 } 366 367 /// AddOperand - Add the specified operand to the specified machine instr. II 368 /// specifies the instruction information for the node, and IIOpNum is the 369 /// operand number (in the II) that we are adding. 370 void InstrEmitter::AddOperand(MachineInstrBuilder &MIB, 371 SDValue Op, 372 unsigned IIOpNum, 373 const MCInstrDesc *II, 374 DenseMap<SDValue, Register> &VRBaseMap, 375 bool IsDebug, bool IsClone, bool IsCloned) { 376 if (Op.isMachineOpcode()) { 377 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap, 378 IsDebug, IsClone, IsCloned); 379 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 380 MIB.addImm(C->getSExtValue()); 381 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) { 382 MIB.addFPImm(F->getConstantFPValue()); 383 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) { 384 Register VReg = R->getReg(); 385 MVT OpVT = Op.getSimpleValueType(); 386 const TargetRegisterClass *IIRC = 387 II ? TRI->getAllocatableClass(TII->getRegClass(*II, IIOpNum, TRI, *MF)) 388 : nullptr; 389 const TargetRegisterClass *OpRC = 390 TLI->isTypeLegal(OpVT) 391 ? TLI->getRegClassFor(OpVT, 392 Op.getNode()->isDivergent() || 393 (IIRC && TRI->isDivergentRegClass(IIRC))) 394 : nullptr; 395 396 if (OpRC && IIRC && OpRC != IIRC && Register::isVirtualRegister(VReg)) { 397 Register NewVReg = MRI->createVirtualRegister(IIRC); 398 BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(), 399 TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg); 400 VReg = NewVReg; 401 } 402 // Turn additional physreg operands into implicit uses on non-variadic 403 // instructions. This is used by call and return instructions passing 404 // arguments in registers. 405 bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic()); 406 MIB.addReg(VReg, getImplRegState(Imp)); 407 } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) { 408 MIB.addRegMask(RM->getRegMask()); 409 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) { 410 MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(), 411 TGA->getTargetFlags()); 412 } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) { 413 MIB.addMBB(BBNode->getBasicBlock()); 414 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) { 415 MIB.addFrameIndex(FI->getIndex()); 416 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) { 417 MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags()); 418 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) { 419 int Offset = CP->getOffset(); 420 Align Alignment = CP->getAlign(); 421 422 unsigned Idx; 423 MachineConstantPool *MCP = MF->getConstantPool(); 424 if (CP->isMachineConstantPoolEntry()) 425 Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Alignment); 426 else 427 Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Alignment); 428 MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags()); 429 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) { 430 MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags()); 431 } else if (auto *SymNode = dyn_cast<MCSymbolSDNode>(Op)) { 432 MIB.addSym(SymNode->getMCSymbol()); 433 } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) { 434 MIB.addBlockAddress(BA->getBlockAddress(), 435 BA->getOffset(), 436 BA->getTargetFlags()); 437 } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) { 438 MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags()); 439 } else { 440 assert(Op.getValueType() != MVT::Other && 441 Op.getValueType() != MVT::Glue && 442 "Chain and glue operands should occur at end of operand list!"); 443 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap, 444 IsDebug, IsClone, IsCloned); 445 } 446 } 447 448 Register InstrEmitter::ConstrainForSubReg(Register VReg, unsigned SubIdx, 449 MVT VT, bool isDivergent, const DebugLoc &DL) { 450 const TargetRegisterClass *VRC = MRI->getRegClass(VReg); 451 const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx); 452 453 // RC is a sub-class of VRC that supports SubIdx. Try to constrain VReg 454 // within reason. 455 if (RC && RC != VRC) 456 RC = MRI->constrainRegClass(VReg, RC, MinRCSize); 457 458 // VReg has been adjusted. It can be used with SubIdx operands now. 459 if (RC) 460 return VReg; 461 462 // VReg couldn't be reasonably constrained. Emit a COPY to a new virtual 463 // register instead. 464 RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT, isDivergent), SubIdx); 465 assert(RC && "No legal register class for VT supports that SubIdx"); 466 Register NewReg = MRI->createVirtualRegister(RC); 467 BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg) 468 .addReg(VReg); 469 return NewReg; 470 } 471 472 /// EmitSubregNode - Generate machine code for subreg nodes. 473 /// 474 void InstrEmitter::EmitSubregNode(SDNode *Node, 475 DenseMap<SDValue, Register> &VRBaseMap, 476 bool IsClone, bool IsCloned) { 477 Register VRBase; 478 unsigned Opc = Node->getMachineOpcode(); 479 480 // If the node is only used by a CopyToReg and the dest reg is a vreg, use 481 // the CopyToReg'd destination register instead of creating a new vreg. 482 for (SDNode *User : Node->uses()) { 483 if (User->getOpcode() == ISD::CopyToReg && 484 User->getOperand(2).getNode() == Node) { 485 Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 486 if (DestReg.isVirtual()) { 487 VRBase = DestReg; 488 break; 489 } 490 } 491 } 492 493 if (Opc == TargetOpcode::EXTRACT_SUBREG) { 494 // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub. There are no 495 // constraints on the %dst register, COPY can target all legal register 496 // classes. 497 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 498 const TargetRegisterClass *TRC = 499 TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent()); 500 501 Register Reg; 502 MachineInstr *DefMI; 503 RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(0)); 504 if (R && Register::isPhysicalRegister(R->getReg())) { 505 Reg = R->getReg(); 506 DefMI = nullptr; 507 } else { 508 Reg = R ? R->getReg() : getVR(Node->getOperand(0), VRBaseMap); 509 DefMI = MRI->getVRegDef(Reg); 510 } 511 512 Register SrcReg, DstReg; 513 unsigned 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 (Reg.isVirtual()) 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) 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 (Reg.isVirtual()) 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, Register> &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, Register> &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, Register> &VRBaseMap) { 679 MDNode *Var = SD->getVariable(); 680 MDNode *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 ArrayRef<SDDbgOperand> LocationOps = SD->getLocationOps(); 688 assert(!LocationOps.empty() && "dbg_value with no location operands?"); 689 690 if (SD->isInvalidated()) 691 return EmitDbgNoLocation(SD); 692 693 // Emit variadic dbg_value nodes as DBG_VALUE_LIST. 694 if (SD->isVariadic()) { 695 // DBG_VALUE_LIST := "DBG_VALUE_LIST" var, expression, loc (, loc)* 696 const MCInstrDesc &DbgValDesc = TII->get(TargetOpcode::DBG_VALUE_LIST); 697 // Build the DBG_VALUE_LIST instruction base. 698 auto MIB = BuildMI(*MF, DL, DbgValDesc); 699 MIB.addMetadata(Var); 700 MIB.addMetadata(Expr); 701 AddDbgValueLocationOps(MIB, DbgValDesc, LocationOps, VRBaseMap); 702 return &*MIB; 703 } 704 705 // Attempt to produce a DBG_INSTR_REF if we've been asked to. 706 // We currently exclude the possibility of instruction references for 707 // variadic nodes; if at some point we enable them, this should be moved 708 // above the variadic block. 709 if (EmitDebugInstrRefs) 710 if (auto *InstrRef = EmitDbgInstrRef(SD, VRBaseMap)) 711 return InstrRef; 712 713 return EmitDbgValueFromSingleOp(SD, VRBaseMap); 714 } 715 716 void InstrEmitter::AddDbgValueLocationOps( 717 MachineInstrBuilder &MIB, const MCInstrDesc &DbgValDesc, 718 ArrayRef<SDDbgOperand> LocationOps, 719 DenseMap<SDValue, Register> &VRBaseMap) { 720 for (const SDDbgOperand &Op : LocationOps) { 721 switch (Op.getKind()) { 722 case SDDbgOperand::FRAMEIX: 723 MIB.addFrameIndex(Op.getFrameIx()); 724 break; 725 case SDDbgOperand::VREG: 726 MIB.addReg(Op.getVReg()); 727 break; 728 case SDDbgOperand::SDNODE: { 729 SDValue V = SDValue(Op.getSDNode(), Op.getResNo()); 730 // It's possible we replaced this SDNode with other(s) and therefore 731 // didn't generate code for it. It's better to catch these cases where 732 // they happen and transfer the debug info, but trying to guarantee that 733 // in all cases would be very fragile; this is a safeguard for any 734 // that were missed. 735 if (VRBaseMap.count(V) == 0) 736 MIB.addReg(0U); // undef 737 else 738 AddOperand(MIB, V, (*MIB).getNumOperands(), &DbgValDesc, VRBaseMap, 739 /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false); 740 } break; 741 case SDDbgOperand::CONST: { 742 const Value *V = Op.getConst(); 743 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 744 if (CI->getBitWidth() > 64) 745 MIB.addCImm(CI); 746 else 747 MIB.addImm(CI->getSExtValue()); 748 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { 749 MIB.addFPImm(CF); 750 } else if (isa<ConstantPointerNull>(V)) { 751 // Note: This assumes that all nullptr constants are zero-valued. 752 MIB.addImm(0); 753 } else { 754 // Could be an Undef. In any case insert an Undef so we can see what we 755 // dropped. 756 MIB.addReg(0U); 757 } 758 } break; 759 } 760 } 761 } 762 763 MachineInstr * 764 InstrEmitter::EmitDbgInstrRef(SDDbgValue *SD, 765 DenseMap<SDValue, Register> &VRBaseMap) { 766 assert(!SD->isVariadic()); 767 SDDbgOperand DbgOperand = SD->getLocationOps()[0]; 768 MDNode *Var = SD->getVariable(); 769 DIExpression *Expr = (DIExpression*)SD->getExpression(); 770 DebugLoc DL = SD->getDebugLoc(); 771 const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_INSTR_REF); 772 773 // Handle variable locations that don't actually depend on the instructions 774 // in the program: constants and stack locations. 775 if (DbgOperand.getKind() == SDDbgOperand::FRAMEIX || 776 DbgOperand.getKind() == SDDbgOperand::CONST) 777 return EmitDbgValueFromSingleOp(SD, VRBaseMap); 778 779 // Immediately fold any indirectness from the LLVM-IR intrinsic into the 780 // expression: 781 if (SD->isIndirect()) { 782 std::vector<uint64_t> Elts = {dwarf::DW_OP_deref}; 783 Expr = DIExpression::append(Expr, Elts); 784 } 785 786 // It may not be immediately possible to identify the MachineInstr that 787 // defines a VReg, it can depend for example on the order blocks are 788 // emitted in. When this happens, or when further analysis is needed later, 789 // produce an instruction like this: 790 // 791 // DBG_INSTR_REF %0:gr64, 0, !123, !456 792 // 793 // i.e., point the instruction at the vreg, and patch it up later in 794 // MachineFunction::finalizeDebugInstrRefs. 795 auto EmitHalfDoneInstrRef = [&](unsigned VReg) -> MachineInstr * { 796 auto MIB = BuildMI(*MF, DL, RefII); 797 MIB.addReg(VReg); 798 MIB.addImm(0); 799 MIB.addMetadata(Var); 800 MIB.addMetadata(Expr); 801 return MIB; 802 }; 803 804 // Try to find both the defined register and the instruction defining it. 805 MachineInstr *DefMI = nullptr; 806 unsigned VReg; 807 808 if (DbgOperand.getKind() == SDDbgOperand::VREG) { 809 VReg = DbgOperand.getVReg(); 810 811 // No definition means that block hasn't been emitted yet. Leave a vreg 812 // reference to be fixed later. 813 if (!MRI->hasOneDef(VReg)) 814 return EmitHalfDoneInstrRef(VReg); 815 816 DefMI = &*MRI->def_instr_begin(VReg); 817 } else { 818 assert(DbgOperand.getKind() == SDDbgOperand::SDNODE); 819 // Look up the corresponding VReg for the given SDNode, if any. 820 SDNode *Node = DbgOperand.getSDNode(); 821 SDValue Op = SDValue(Node, DbgOperand.getResNo()); 822 DenseMap<SDValue, Register>::iterator I = VRBaseMap.find(Op); 823 // No VReg -> produce a DBG_VALUE $noreg instead. 824 if (I==VRBaseMap.end()) 825 return EmitDbgNoLocation(SD); 826 827 // Try to pick out a defining instruction at this point. 828 VReg = getVR(Op, VRBaseMap); 829 830 // Again, if there's no instruction defining the VReg right now, fix it up 831 // later. 832 if (!MRI->hasOneDef(VReg)) 833 return EmitHalfDoneInstrRef(VReg); 834 835 DefMI = &*MRI->def_instr_begin(VReg); 836 } 837 838 // Avoid copy like instructions: they don't define values, only move them. 839 // Leave a virtual-register reference until it can be fixed up later, to find 840 // the underlying value definition. 841 if (DefMI->isCopyLike() || TII->isCopyInstr(*DefMI)) 842 return EmitHalfDoneInstrRef(VReg); 843 844 auto MIB = BuildMI(*MF, DL, RefII); 845 846 // Find the operand number which defines the specified VReg. 847 unsigned OperandIdx = 0; 848 for (const auto &MO : DefMI->operands()) { 849 if (MO.isReg() && MO.isDef() && MO.getReg() == VReg) 850 break; 851 ++OperandIdx; 852 } 853 assert(OperandIdx < DefMI->getNumOperands()); 854 855 // Make the DBG_INSTR_REF refer to that instruction, and that operand. 856 unsigned InstrNum = DefMI->getDebugInstrNum(); 857 MIB.addImm(InstrNum); 858 MIB.addImm(OperandIdx); 859 MIB.addMetadata(Var); 860 MIB.addMetadata(Expr); 861 return &*MIB; 862 } 863 864 MachineInstr *InstrEmitter::EmitDbgNoLocation(SDDbgValue *SD) { 865 // An invalidated SDNode must generate an undef DBG_VALUE: although the 866 // original value is no longer computed, earlier DBG_VALUEs live ranges 867 // must not leak into later code. 868 MDNode *Var = SD->getVariable(); 869 MDNode *Expr = SD->getExpression(); 870 DebugLoc DL = SD->getDebugLoc(); 871 auto MIB = BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE)); 872 MIB.addReg(0U); 873 MIB.addReg(0U); 874 MIB.addMetadata(Var); 875 MIB.addMetadata(Expr); 876 return &*MIB; 877 } 878 879 MachineInstr * 880 InstrEmitter::EmitDbgValueFromSingleOp(SDDbgValue *SD, 881 DenseMap<SDValue, Register> &VRBaseMap) { 882 MDNode *Var = SD->getVariable(); 883 DIExpression *Expr = SD->getExpression(); 884 DebugLoc DL = SD->getDebugLoc(); 885 const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE); 886 887 assert(SD->getLocationOps().size() == 1 && 888 "Non variadic dbg_value should have only one location op"); 889 890 // See about constant-folding the expression. 891 // Copy the location operand in case we replace it. 892 SmallVector<SDDbgOperand, 1> LocationOps(1, SD->getLocationOps()[0]); 893 if (Expr && LocationOps[0].getKind() == SDDbgOperand::CONST) { 894 const Value *V = LocationOps[0].getConst(); 895 if (auto *C = dyn_cast<ConstantInt>(V)) { 896 std::tie(Expr, C) = Expr->constantFold(C); 897 LocationOps[0] = SDDbgOperand::fromConst(C); 898 } 899 } 900 901 // Emit non-variadic dbg_value nodes as DBG_VALUE. 902 // DBG_VALUE := "DBG_VALUE" loc, isIndirect, var, expr 903 auto MIB = BuildMI(*MF, DL, II); 904 AddDbgValueLocationOps(MIB, II, LocationOps, VRBaseMap); 905 906 if (SD->isIndirect()) 907 MIB.addImm(0U); 908 else 909 MIB.addReg(0U); 910 911 return MIB.addMetadata(Var).addMetadata(Expr); 912 } 913 914 MachineInstr * 915 InstrEmitter::EmitDbgLabel(SDDbgLabel *SD) { 916 MDNode *Label = SD->getLabel(); 917 DebugLoc DL = SD->getDebugLoc(); 918 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) && 919 "Expected inlined-at fields to agree"); 920 921 const MCInstrDesc &II = TII->get(TargetOpcode::DBG_LABEL); 922 MachineInstrBuilder MIB = BuildMI(*MF, DL, II); 923 MIB.addMetadata(Label); 924 925 return &*MIB; 926 } 927 928 /// EmitMachineNode - Generate machine code for a target-specific node and 929 /// needed dependencies. 930 /// 931 void InstrEmitter:: 932 EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned, 933 DenseMap<SDValue, Register> &VRBaseMap) { 934 unsigned Opc = Node->getMachineOpcode(); 935 936 // Handle subreg insert/extract specially 937 if (Opc == TargetOpcode::EXTRACT_SUBREG || 938 Opc == TargetOpcode::INSERT_SUBREG || 939 Opc == TargetOpcode::SUBREG_TO_REG) { 940 EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned); 941 return; 942 } 943 944 // Handle COPY_TO_REGCLASS specially. 945 if (Opc == TargetOpcode::COPY_TO_REGCLASS) { 946 EmitCopyToRegClassNode(Node, VRBaseMap); 947 return; 948 } 949 950 // Handle REG_SEQUENCE specially. 951 if (Opc == TargetOpcode::REG_SEQUENCE) { 952 EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned); 953 return; 954 } 955 956 if (Opc == TargetOpcode::IMPLICIT_DEF) 957 // We want a unique VR for each IMPLICIT_DEF use. 958 return; 959 960 const MCInstrDesc &II = TII->get(Opc); 961 unsigned NumResults = CountResults(Node); 962 unsigned NumDefs = II.getNumDefs(); 963 const MCPhysReg *ScratchRegs = nullptr; 964 965 // Handle STACKMAP and PATCHPOINT specially and then use the generic code. 966 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) { 967 // Stackmaps do not have arguments and do not preserve their calling 968 // convention. However, to simplify runtime support, they clobber the same 969 // scratch registers as AnyRegCC. 970 unsigned CC = CallingConv::AnyReg; 971 if (Opc == TargetOpcode::PATCHPOINT) { 972 CC = Node->getConstantOperandVal(PatchPointOpers::CCPos); 973 NumDefs = NumResults; 974 } 975 ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC); 976 } else if (Opc == TargetOpcode::STATEPOINT) { 977 NumDefs = NumResults; 978 } 979 980 unsigned NumImpUses = 0; 981 unsigned NodeOperands = 982 countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses); 983 bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() && 984 II.isVariadic() && II.variadicOpsAreDefs(); 985 bool HasPhysRegOuts = NumResults > NumDefs && 986 II.getImplicitDefs() != nullptr && !HasVRegVariadicDefs; 987 #ifndef NDEBUG 988 unsigned NumMIOperands = NodeOperands + NumResults; 989 if (II.isVariadic()) 990 assert(NumMIOperands >= II.getNumOperands() && 991 "Too few operands for a variadic node!"); 992 else 993 assert(NumMIOperands >= II.getNumOperands() && 994 NumMIOperands <= II.getNumOperands() + II.getNumImplicitDefs() + 995 NumImpUses && 996 "#operands for dag node doesn't match .td file!"); 997 #endif 998 999 // Create the new machine instruction. 1000 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II); 1001 1002 // Add result register values for things that are defined by this 1003 // instruction. 1004 if (NumResults) { 1005 CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap); 1006 1007 // Transfer any IR flags from the SDNode to the MachineInstr 1008 MachineInstr *MI = MIB.getInstr(); 1009 const SDNodeFlags Flags = Node->getFlags(); 1010 if (Flags.hasNoSignedZeros()) 1011 MI->setFlag(MachineInstr::MIFlag::FmNsz); 1012 1013 if (Flags.hasAllowReciprocal()) 1014 MI->setFlag(MachineInstr::MIFlag::FmArcp); 1015 1016 if (Flags.hasNoNaNs()) 1017 MI->setFlag(MachineInstr::MIFlag::FmNoNans); 1018 1019 if (Flags.hasNoInfs()) 1020 MI->setFlag(MachineInstr::MIFlag::FmNoInfs); 1021 1022 if (Flags.hasAllowContract()) 1023 MI->setFlag(MachineInstr::MIFlag::FmContract); 1024 1025 if (Flags.hasApproximateFuncs()) 1026 MI->setFlag(MachineInstr::MIFlag::FmAfn); 1027 1028 if (Flags.hasAllowReassociation()) 1029 MI->setFlag(MachineInstr::MIFlag::FmReassoc); 1030 1031 if (Flags.hasNoUnsignedWrap()) 1032 MI->setFlag(MachineInstr::MIFlag::NoUWrap); 1033 1034 if (Flags.hasNoSignedWrap()) 1035 MI->setFlag(MachineInstr::MIFlag::NoSWrap); 1036 1037 if (Flags.hasExact()) 1038 MI->setFlag(MachineInstr::MIFlag::IsExact); 1039 1040 if (Flags.hasNoFPExcept()) 1041 MI->setFlag(MachineInstr::MIFlag::NoFPExcept); 1042 } 1043 1044 // Emit all of the actual operands of this instruction, adding them to the 1045 // instruction as appropriate. 1046 bool HasOptPRefs = NumDefs > NumResults; 1047 assert((!HasOptPRefs || !HasPhysRegOuts) && 1048 "Unable to cope with optional defs and phys regs defs!"); 1049 unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0; 1050 for (unsigned i = NumSkip; i != NodeOperands; ++i) 1051 AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II, 1052 VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned); 1053 1054 // Add scratch registers as implicit def and early clobber 1055 if (ScratchRegs) 1056 for (unsigned i = 0; ScratchRegs[i]; ++i) 1057 MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine | 1058 RegState::EarlyClobber); 1059 1060 // Set the memory reference descriptions of this instruction now that it is 1061 // part of the function. 1062 MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands()); 1063 1064 // Insert the instruction into position in the block. This needs to 1065 // happen before any custom inserter hook is called so that the 1066 // hook knows where in the block to insert the replacement code. 1067 MBB->insert(InsertPos, MIB); 1068 1069 // The MachineInstr may also define physregs instead of virtregs. These 1070 // physreg values can reach other instructions in different ways: 1071 // 1072 // 1. When there is a use of a Node value beyond the explicitly defined 1073 // virtual registers, we emit a CopyFromReg for one of the implicitly 1074 // defined physregs. This only happens when HasPhysRegOuts is true. 1075 // 1076 // 2. A CopyFromReg reading a physreg may be glued to this instruction. 1077 // 1078 // 3. A glued instruction may implicitly use a physreg. 1079 // 1080 // 4. A glued instruction may use a RegisterSDNode operand. 1081 // 1082 // Collect all the used physreg defs, and make sure that any unused physreg 1083 // defs are marked as dead. 1084 SmallVector<Register, 8> UsedRegs; 1085 1086 // Additional results must be physical register defs. 1087 if (HasPhysRegOuts) { 1088 for (unsigned i = NumDefs; i < NumResults; ++i) { 1089 Register Reg = II.getImplicitDefs()[i - NumDefs]; 1090 if (!Node->hasAnyUseOfValue(i)) 1091 continue; 1092 // This implicitly defined physreg has a use. 1093 UsedRegs.push_back(Reg); 1094 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap); 1095 } 1096 } 1097 1098 // Scan the glue chain for any used physregs. 1099 if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) { 1100 for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) { 1101 if (F->getOpcode() == ISD::CopyFromReg) { 1102 UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg()); 1103 continue; 1104 } else if (F->getOpcode() == ISD::CopyToReg) { 1105 // Skip CopyToReg nodes that are internal to the glue chain. 1106 continue; 1107 } 1108 // Collect declared implicit uses. 1109 const MCInstrDesc &MCID = TII->get(F->getMachineOpcode()); 1110 UsedRegs.append(MCID.getImplicitUses(), 1111 MCID.getImplicitUses() + MCID.getNumImplicitUses()); 1112 // In addition to declared implicit uses, we must also check for 1113 // direct RegisterSDNode operands. 1114 for (unsigned i = 0, e = F->getNumOperands(); i != e; ++i) 1115 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(F->getOperand(i))) { 1116 Register Reg = R->getReg(); 1117 if (Reg.isPhysical()) 1118 UsedRegs.push_back(Reg); 1119 } 1120 } 1121 } 1122 1123 // Finally mark unused registers as dead. 1124 if (!UsedRegs.empty() || II.getImplicitDefs() || II.hasOptionalDef()) 1125 MIB->setPhysRegsDeadExcept(UsedRegs, *TRI); 1126 1127 // STATEPOINT is too 'dynamic' to have meaningful machine description. 1128 // We have to manually tie operands. 1129 if (Opc == TargetOpcode::STATEPOINT && NumDefs > 0) { 1130 assert(!HasPhysRegOuts && "STATEPOINT mishandled"); 1131 MachineInstr *MI = MIB; 1132 unsigned Def = 0; 1133 int First = StatepointOpers(MI).getFirstGCPtrIdx(); 1134 assert(First > 0 && "Statepoint has Defs but no GC ptr list"); 1135 unsigned Use = (unsigned)First; 1136 while (Def < NumDefs) { 1137 if (MI->getOperand(Use).isReg()) 1138 MI->tieOperands(Def++, Use); 1139 Use = StackMaps::getNextMetaArgIdx(MI, Use); 1140 } 1141 } 1142 1143 // Run post-isel target hook to adjust this instruction if needed. 1144 if (II.hasPostISelHook()) 1145 TLI->AdjustInstrPostInstrSelection(*MIB, Node); 1146 } 1147 1148 /// EmitSpecialNode - Generate machine code for a target-independent node and 1149 /// needed dependencies. 1150 void InstrEmitter:: 1151 EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned, 1152 DenseMap<SDValue, Register> &VRBaseMap) { 1153 switch (Node->getOpcode()) { 1154 default: 1155 #ifndef NDEBUG 1156 Node->dump(); 1157 #endif 1158 llvm_unreachable("This target-independent node should have been selected!"); 1159 case ISD::EntryToken: 1160 llvm_unreachable("EntryToken should have been excluded from the schedule!"); 1161 case ISD::MERGE_VALUES: 1162 case ISD::TokenFactor: // fall thru 1163 break; 1164 case ISD::CopyToReg: { 1165 Register DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 1166 SDValue SrcVal = Node->getOperand(2); 1167 if (Register::isVirtualRegister(DestReg) && SrcVal.isMachineOpcode() && 1168 SrcVal.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) { 1169 // Instead building a COPY to that vreg destination, build an 1170 // IMPLICIT_DEF instruction instead. 1171 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 1172 TII->get(TargetOpcode::IMPLICIT_DEF), DestReg); 1173 break; 1174 } 1175 Register SrcReg; 1176 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal)) 1177 SrcReg = R->getReg(); 1178 else 1179 SrcReg = getVR(SrcVal, VRBaseMap); 1180 1181 if (SrcReg == DestReg) // Coalesced away the copy? Ignore. 1182 break; 1183 1184 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 1185 DestReg).addReg(SrcReg); 1186 break; 1187 } 1188 case ISD::CopyFromReg: { 1189 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 1190 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap); 1191 break; 1192 } 1193 case ISD::EH_LABEL: 1194 case ISD::ANNOTATION_LABEL: { 1195 unsigned Opc = (Node->getOpcode() == ISD::EH_LABEL) 1196 ? TargetOpcode::EH_LABEL 1197 : TargetOpcode::ANNOTATION_LABEL; 1198 MCSymbol *S = cast<LabelSDNode>(Node)->getLabel(); 1199 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 1200 TII->get(Opc)).addSym(S); 1201 break; 1202 } 1203 1204 case ISD::LIFETIME_START: 1205 case ISD::LIFETIME_END: { 1206 unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START) 1207 ? TargetOpcode::LIFETIME_START 1208 : TargetOpcode::LIFETIME_END; 1209 auto *FI = cast<FrameIndexSDNode>(Node->getOperand(1)); 1210 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp)) 1211 .addFrameIndex(FI->getIndex()); 1212 break; 1213 } 1214 1215 case ISD::PSEUDO_PROBE: { 1216 unsigned TarOp = TargetOpcode::PSEUDO_PROBE; 1217 auto Guid = cast<PseudoProbeSDNode>(Node)->getGuid(); 1218 auto Index = cast<PseudoProbeSDNode>(Node)->getIndex(); 1219 auto Attr = cast<PseudoProbeSDNode>(Node)->getAttributes(); 1220 1221 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp)) 1222 .addImm(Guid) 1223 .addImm(Index) 1224 .addImm((uint8_t)PseudoProbeType::Block) 1225 .addImm(Attr); 1226 break; 1227 } 1228 1229 case ISD::INLINEASM: 1230 case ISD::INLINEASM_BR: { 1231 unsigned NumOps = Node->getNumOperands(); 1232 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue) 1233 --NumOps; // Ignore the glue operand. 1234 1235 // Create the inline asm machine instruction. 1236 unsigned TgtOpc = Node->getOpcode() == ISD::INLINEASM_BR 1237 ? TargetOpcode::INLINEASM_BR 1238 : TargetOpcode::INLINEASM; 1239 MachineInstrBuilder MIB = 1240 BuildMI(*MF, Node->getDebugLoc(), TII->get(TgtOpc)); 1241 1242 // Add the asm string as an external symbol operand. 1243 SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString); 1244 const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol(); 1245 MIB.addExternalSymbol(AsmStr); 1246 1247 // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore 1248 // bits. 1249 int64_t ExtraInfo = 1250 cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))-> 1251 getZExtValue(); 1252 MIB.addImm(ExtraInfo); 1253 1254 // Remember to operand index of the group flags. 1255 SmallVector<unsigned, 8> GroupIdx; 1256 1257 // Remember registers that are part of early-clobber defs. 1258 SmallVector<unsigned, 8> ECRegs; 1259 1260 // Add all of the operand registers to the instruction. 1261 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) { 1262 unsigned Flags = 1263 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); 1264 const unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 1265 1266 GroupIdx.push_back(MIB->getNumOperands()); 1267 MIB.addImm(Flags); 1268 ++i; // Skip the ID value. 1269 1270 switch (InlineAsm::getKind(Flags)) { 1271 default: llvm_unreachable("Bad flags!"); 1272 case InlineAsm::Kind_RegDef: 1273 for (unsigned j = 0; j != NumVals; ++j, ++i) { 1274 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 1275 // FIXME: Add dead flags for physical and virtual registers defined. 1276 // For now, mark physical register defs as implicit to help fast 1277 // regalloc. This makes inline asm look a lot like calls. 1278 MIB.addReg(Reg, 1279 RegState::Define | 1280 getImplRegState(Register::isPhysicalRegister(Reg))); 1281 } 1282 break; 1283 case InlineAsm::Kind_RegDefEarlyClobber: 1284 case InlineAsm::Kind_Clobber: 1285 for (unsigned j = 0; j != NumVals; ++j, ++i) { 1286 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 1287 MIB.addReg(Reg, 1288 RegState::Define | RegState::EarlyClobber | 1289 getImplRegState(Register::isPhysicalRegister(Reg))); 1290 ECRegs.push_back(Reg); 1291 } 1292 break; 1293 case InlineAsm::Kind_RegUse: // Use of register. 1294 case InlineAsm::Kind_Imm: // Immediate. 1295 case InlineAsm::Kind_Mem: // Addressing mode. 1296 // The addressing mode has been selected, just add all of the 1297 // operands to the machine instruction. 1298 for (unsigned j = 0; j != NumVals; ++j, ++i) 1299 AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap, 1300 /*IsDebug=*/false, IsClone, IsCloned); 1301 1302 // Manually set isTied bits. 1303 if (InlineAsm::getKind(Flags) == InlineAsm::Kind_RegUse) { 1304 unsigned DefGroup = 0; 1305 if (InlineAsm::isUseOperandTiedToDef(Flags, DefGroup)) { 1306 unsigned DefIdx = GroupIdx[DefGroup] + 1; 1307 unsigned UseIdx = GroupIdx.back() + 1; 1308 for (unsigned j = 0; j != NumVals; ++j) 1309 MIB->tieOperands(DefIdx + j, UseIdx + j); 1310 } 1311 } 1312 break; 1313 } 1314 } 1315 1316 // GCC inline assembly allows input operands to also be early-clobber 1317 // output operands (so long as the operand is written only after it's 1318 // used), but this does not match the semantics of our early-clobber flag. 1319 // If an early-clobber operand register is also an input operand register, 1320 // then remove the early-clobber flag. 1321 for (unsigned Reg : ECRegs) { 1322 if (MIB->readsRegister(Reg, TRI)) { 1323 MachineOperand *MO = 1324 MIB->findRegisterDefOperand(Reg, false, false, TRI); 1325 assert(MO && "No def operand for clobbered register?"); 1326 MO->setIsEarlyClobber(false); 1327 } 1328 } 1329 1330 // Get the mdnode from the asm if it exists and add it to the instruction. 1331 SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode); 1332 const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD(); 1333 if (MD) 1334 MIB.addMetadata(MD); 1335 1336 MBB->insert(InsertPos, MIB); 1337 break; 1338 } 1339 } 1340 } 1341 1342 /// InstrEmitter - Construct an InstrEmitter and set it to start inserting 1343 /// at the given position in the given block. 1344 InstrEmitter::InstrEmitter(const TargetMachine &TM, MachineBasicBlock *mbb, 1345 MachineBasicBlock::iterator insertpos) 1346 : MF(mbb->getParent()), MRI(&MF->getRegInfo()), 1347 TII(MF->getSubtarget().getInstrInfo()), 1348 TRI(MF->getSubtarget().getRegisterInfo()), 1349 TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb), 1350 InsertPos(insertpos) { 1351 EmitDebugInstrRefs = MF->useDebugInstrRef(); 1352 } 1353