10b57cec5SDimitry Andric //==--- InstrEmitter.cpp - Emit MachineInstrs for the SelectionDAG class ---==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This implements the Emit routines for the SelectionDAG class, which creates 100b57cec5SDimitry Andric // MachineInstrs based on the decisions of the SelectionDAG instruction 110b57cec5SDimitry Andric // selection. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "InstrEmitter.h" 160b57cec5SDimitry Andric #include "SDNodeDbgValue.h" 170b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 225ffd83dbSDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/StackMaps.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 270b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 280b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h" 29*e8d8bef9SDimitry Andric #include "llvm/IR/PseudoProbe.h" 300b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 310b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 320b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 335ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h" 340b57cec5SDimitry Andric using namespace llvm; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #define DEBUG_TYPE "instr-emitter" 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric /// MinRCSize - Smallest register class we allow when constraining virtual 390b57cec5SDimitry Andric /// registers. If satisfying all register class constraints would require 400b57cec5SDimitry Andric /// using a smaller register class, emit a COPY to a new virtual register 410b57cec5SDimitry Andric /// instead. 420b57cec5SDimitry Andric const unsigned MinRCSize = 4; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric /// CountResults - The results of target nodes have register or immediate 450b57cec5SDimitry Andric /// operands first, then an optional chain, and optional glue operands (which do 460b57cec5SDimitry Andric /// not go into the resulting MachineInstr). 470b57cec5SDimitry Andric unsigned InstrEmitter::CountResults(SDNode *Node) { 480b57cec5SDimitry Andric unsigned N = Node->getNumValues(); 490b57cec5SDimitry Andric while (N && Node->getValueType(N - 1) == MVT::Glue) 500b57cec5SDimitry Andric --N; 510b57cec5SDimitry Andric if (N && Node->getValueType(N - 1) == MVT::Other) 520b57cec5SDimitry Andric --N; // Skip over chain result. 530b57cec5SDimitry Andric return N; 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric /// countOperands - The inputs to target nodes have any actual inputs first, 570b57cec5SDimitry Andric /// followed by an optional chain operand, then an optional glue operand. 580b57cec5SDimitry Andric /// Compute the number of actual operands that will go into the resulting 590b57cec5SDimitry Andric /// MachineInstr. 600b57cec5SDimitry Andric /// 610b57cec5SDimitry Andric /// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding 620b57cec5SDimitry Andric /// the chain and glue. These operands may be implicit on the machine instr. 630b57cec5SDimitry Andric static unsigned countOperands(SDNode *Node, unsigned NumExpUses, 640b57cec5SDimitry Andric unsigned &NumImpUses) { 650b57cec5SDimitry Andric unsigned N = Node->getNumOperands(); 660b57cec5SDimitry Andric while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue) 670b57cec5SDimitry Andric --N; 680b57cec5SDimitry Andric if (N && Node->getOperand(N - 1).getValueType() == MVT::Other) 690b57cec5SDimitry Andric --N; // Ignore chain if it exists. 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses. 720b57cec5SDimitry Andric NumImpUses = N - NumExpUses; 730b57cec5SDimitry Andric for (unsigned I = N; I > NumExpUses; --I) { 740b57cec5SDimitry Andric if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1))) 750b57cec5SDimitry Andric continue; 760b57cec5SDimitry Andric if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1))) 778bcb0991SDimitry Andric if (Register::isPhysicalRegister(RN->getReg())) 780b57cec5SDimitry Andric continue; 790b57cec5SDimitry Andric NumImpUses = N - I; 800b57cec5SDimitry Andric break; 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric return N; 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an 870b57cec5SDimitry Andric /// implicit physical register output. 880b57cec5SDimitry Andric void InstrEmitter:: 890b57cec5SDimitry Andric EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned, 905ffd83dbSDimitry Andric Register SrcReg, DenseMap<SDValue, Register> &VRBaseMap) { 915ffd83dbSDimitry Andric Register VRBase; 925ffd83dbSDimitry Andric if (SrcReg.isVirtual()) { 930b57cec5SDimitry Andric // Just use the input register directly! 940b57cec5SDimitry Andric SDValue Op(Node, ResNo); 950b57cec5SDimitry Andric if (IsClone) 960b57cec5SDimitry Andric VRBaseMap.erase(Op); 970b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second; 980b57cec5SDimitry Andric (void)isNew; // Silence compiler warning. 990b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early"); 1000b57cec5SDimitry Andric return; 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // If the node is only used by a CopyToReg and the dest reg is a vreg, use 1040b57cec5SDimitry Andric // the CopyToReg'd destination register instead of creating a new vreg. 1050b57cec5SDimitry Andric bool MatchReg = true; 1060b57cec5SDimitry Andric const TargetRegisterClass *UseRC = nullptr; 1070b57cec5SDimitry Andric MVT VT = Node->getSimpleValueType(ResNo); 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric // Stick to the preferred register classes for legal types. 1100b57cec5SDimitry Andric if (TLI->isTypeLegal(VT)) 1110b57cec5SDimitry Andric UseRC = TLI->getRegClassFor(VT, Node->isDivergent()); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric if (!IsClone && !IsCloned) 1140b57cec5SDimitry Andric for (SDNode *User : Node->uses()) { 1150b57cec5SDimitry Andric bool Match = true; 1160b57cec5SDimitry Andric if (User->getOpcode() == ISD::CopyToReg && 1170b57cec5SDimitry Andric User->getOperand(2).getNode() == Node && 1180b57cec5SDimitry Andric User->getOperand(2).getResNo() == ResNo) { 1195ffd83dbSDimitry Andric Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 1205ffd83dbSDimitry Andric if (DestReg.isVirtual()) { 1210b57cec5SDimitry Andric VRBase = DestReg; 1220b57cec5SDimitry Andric Match = false; 1230b57cec5SDimitry Andric } else if (DestReg != SrcReg) 1240b57cec5SDimitry Andric Match = false; 1250b57cec5SDimitry Andric } else { 1260b57cec5SDimitry Andric for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { 1270b57cec5SDimitry Andric SDValue Op = User->getOperand(i); 1280b57cec5SDimitry Andric if (Op.getNode() != Node || Op.getResNo() != ResNo) 1290b57cec5SDimitry Andric continue; 1300b57cec5SDimitry Andric MVT VT = Node->getSimpleValueType(Op.getResNo()); 1310b57cec5SDimitry Andric if (VT == MVT::Other || VT == MVT::Glue) 1320b57cec5SDimitry Andric continue; 1330b57cec5SDimitry Andric Match = false; 1340b57cec5SDimitry Andric if (User->isMachineOpcode()) { 1350b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(User->getMachineOpcode()); 1360b57cec5SDimitry Andric const TargetRegisterClass *RC = nullptr; 1370b57cec5SDimitry Andric if (i+II.getNumDefs() < II.getNumOperands()) { 1380b57cec5SDimitry Andric RC = TRI->getAllocatableClass( 1390b57cec5SDimitry Andric TII->getRegClass(II, i+II.getNumDefs(), TRI, *MF)); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric if (!UseRC) 1420b57cec5SDimitry Andric UseRC = RC; 1430b57cec5SDimitry Andric else if (RC) { 1440b57cec5SDimitry Andric const TargetRegisterClass *ComRC = 1458bcb0991SDimitry Andric TRI->getCommonSubClass(UseRC, RC); 1460b57cec5SDimitry Andric // If multiple uses expect disjoint register classes, we emit 1470b57cec5SDimitry Andric // copies in AddRegisterOperand. 1480b57cec5SDimitry Andric if (ComRC) 1490b57cec5SDimitry Andric UseRC = ComRC; 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric MatchReg &= Match; 1550b57cec5SDimitry Andric if (VRBase) 1560b57cec5SDimitry Andric break; 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric const TargetRegisterClass *SrcRC = nullptr, *DstRC = nullptr; 1600b57cec5SDimitry Andric SrcRC = TRI->getMinimalPhysRegClass(SrcReg, VT); 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric // Figure out the register class to create for the destreg. 1630b57cec5SDimitry Andric if (VRBase) { 1640b57cec5SDimitry Andric DstRC = MRI->getRegClass(VRBase); 1650b57cec5SDimitry Andric } else if (UseRC) { 1660b57cec5SDimitry Andric assert(TRI->isTypeLegalForClass(*UseRC, VT) && 1670b57cec5SDimitry Andric "Incompatible phys register def and uses!"); 1680b57cec5SDimitry Andric DstRC = UseRC; 1690b57cec5SDimitry Andric } else { 1700b57cec5SDimitry Andric DstRC = TLI->getRegClassFor(VT, Node->isDivergent()); 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // If all uses are reading from the src physical register and copying the 1740b57cec5SDimitry Andric // register is either impossible or very expensive, then don't create a copy. 1750b57cec5SDimitry Andric if (MatchReg && SrcRC->getCopyCost() < 0) { 1760b57cec5SDimitry Andric VRBase = SrcReg; 1770b57cec5SDimitry Andric } else { 1780b57cec5SDimitry Andric // Create the reg, emit the copy. 1790b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(DstRC); 1800b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 1810b57cec5SDimitry Andric VRBase).addReg(SrcReg); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric SDValue Op(Node, ResNo); 1850b57cec5SDimitry Andric if (IsClone) 1860b57cec5SDimitry Andric VRBaseMap.erase(Op); 1870b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 1880b57cec5SDimitry Andric (void)isNew; // Silence compiler warning. 1890b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early"); 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric void InstrEmitter::CreateVirtualRegisters(SDNode *Node, 1930b57cec5SDimitry Andric MachineInstrBuilder &MIB, 1940b57cec5SDimitry Andric const MCInstrDesc &II, 1950b57cec5SDimitry Andric bool IsClone, bool IsCloned, 1965ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) { 1970b57cec5SDimitry Andric assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF && 1980b57cec5SDimitry Andric "IMPLICIT_DEF should have been handled as a special case elsewhere!"); 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric unsigned NumResults = CountResults(Node); 2015ffd83dbSDimitry Andric bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() && 2025ffd83dbSDimitry Andric II.isVariadic() && II.variadicOpsAreDefs(); 2035ffd83dbSDimitry Andric unsigned NumVRegs = HasVRegVariadicDefs ? NumResults : II.getNumDefs(); 204*e8d8bef9SDimitry Andric if (Node->getMachineOpcode() == TargetOpcode::STATEPOINT) 205*e8d8bef9SDimitry Andric NumVRegs = NumResults; 2065ffd83dbSDimitry Andric for (unsigned i = 0; i < NumVRegs; ++i) { 2070b57cec5SDimitry Andric // If the specific node value is only used by a CopyToReg and the dest reg 2080b57cec5SDimitry Andric // is a vreg in the same register class, use the CopyToReg'd destination 2090b57cec5SDimitry Andric // register instead of creating a new vreg. 2105ffd83dbSDimitry Andric Register VRBase; 2110b57cec5SDimitry Andric const TargetRegisterClass *RC = 2120b57cec5SDimitry Andric TRI->getAllocatableClass(TII->getRegClass(II, i, TRI, *MF)); 2130b57cec5SDimitry Andric // Always let the value type influence the used register class. The 2140b57cec5SDimitry Andric // constraints on the instruction may be too lax to represent the value 2150b57cec5SDimitry Andric // type correctly. For example, a 64-bit float (X86::FR64) can't live in 2160b57cec5SDimitry Andric // the 32-bit float super-class (X86::FR32). 2170b57cec5SDimitry Andric if (i < NumResults && TLI->isTypeLegal(Node->getSimpleValueType(i))) { 2180b57cec5SDimitry Andric const TargetRegisterClass *VTRC = TLI->getRegClassFor( 2190b57cec5SDimitry Andric Node->getSimpleValueType(i), 2200b57cec5SDimitry Andric (Node->isDivergent() || (RC && TRI->isDivergentRegClass(RC)))); 2210b57cec5SDimitry Andric if (RC) 2220b57cec5SDimitry Andric VTRC = TRI->getCommonSubClass(RC, VTRC); 2230b57cec5SDimitry Andric if (VTRC) 2240b57cec5SDimitry Andric RC = VTRC; 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric 2275ffd83dbSDimitry Andric if (II.OpInfo != nullptr && II.OpInfo[i].isOptionalDef()) { 2280b57cec5SDimitry Andric // Optional def must be a physical register. 2290b57cec5SDimitry Andric VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg(); 2305ffd83dbSDimitry Andric assert(VRBase.isPhysical()); 2310b57cec5SDimitry Andric MIB.addReg(VRBase, RegState::Define); 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric if (!VRBase && !IsClone && !IsCloned) 2350b57cec5SDimitry Andric for (SDNode *User : Node->uses()) { 2360b57cec5SDimitry Andric if (User->getOpcode() == ISD::CopyToReg && 2370b57cec5SDimitry Andric User->getOperand(2).getNode() == Node && 2380b57cec5SDimitry Andric User->getOperand(2).getResNo() == i) { 2390b57cec5SDimitry Andric unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 2408bcb0991SDimitry Andric if (Register::isVirtualRegister(Reg)) { 2410b57cec5SDimitry Andric const TargetRegisterClass *RegRC = MRI->getRegClass(Reg); 2420b57cec5SDimitry Andric if (RegRC == RC) { 2430b57cec5SDimitry Andric VRBase = Reg; 2440b57cec5SDimitry Andric MIB.addReg(VRBase, RegState::Define); 2450b57cec5SDimitry Andric break; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // Create the result registers for this node and add the result regs to 2520b57cec5SDimitry Andric // the machine instruction. 2530b57cec5SDimitry Andric if (VRBase == 0) { 2540b57cec5SDimitry Andric assert(RC && "Isn't a register operand!"); 2550b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(RC); 2560b57cec5SDimitry Andric MIB.addReg(VRBase, RegState::Define); 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric // If this def corresponds to a result of the SDNode insert the VRBase into 2600b57cec5SDimitry Andric // the lookup map. 2610b57cec5SDimitry Andric if (i < NumResults) { 2620b57cec5SDimitry Andric SDValue Op(Node, i); 2630b57cec5SDimitry Andric if (IsClone) 2640b57cec5SDimitry Andric VRBaseMap.erase(Op); 2650b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 2660b57cec5SDimitry Andric (void)isNew; // Silence compiler warning. 2670b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early"); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric /// getVR - Return the virtual register corresponding to the specified result 2730b57cec5SDimitry Andric /// of the specified node. 2745ffd83dbSDimitry Andric Register InstrEmitter::getVR(SDValue Op, 2755ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) { 2760b57cec5SDimitry Andric if (Op.isMachineOpcode() && 2770b57cec5SDimitry Andric Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) { 2780b57cec5SDimitry Andric // Add an IMPLICIT_DEF instruction before every use. 2790b57cec5SDimitry Andric // IMPLICIT_DEF can produce any type of result so its MCInstrDesc 2800b57cec5SDimitry Andric // does not include operand register class info. 2810b57cec5SDimitry Andric const TargetRegisterClass *RC = TLI->getRegClassFor( 2820b57cec5SDimitry Andric Op.getSimpleValueType(), Op.getNode()->isDivergent()); 2838bcb0991SDimitry Andric Register VReg = MRI->createVirtualRegister(RC); 2840b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Op.getDebugLoc(), 2850b57cec5SDimitry Andric TII->get(TargetOpcode::IMPLICIT_DEF), VReg); 2860b57cec5SDimitry Andric return VReg; 2870b57cec5SDimitry Andric } 2880b57cec5SDimitry Andric 2895ffd83dbSDimitry Andric DenseMap<SDValue, Register>::iterator I = VRBaseMap.find(Op); 2900b57cec5SDimitry Andric assert(I != VRBaseMap.end() && "Node emitted out of order - late"); 2910b57cec5SDimitry Andric return I->second; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric /// AddRegisterOperand - Add the specified register as an operand to the 2960b57cec5SDimitry Andric /// specified machine instr. Insert register copies if the register is 2970b57cec5SDimitry Andric /// not in the required register class. 2980b57cec5SDimitry Andric void 2990b57cec5SDimitry Andric InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB, 3000b57cec5SDimitry Andric SDValue Op, 3010b57cec5SDimitry Andric unsigned IIOpNum, 3020b57cec5SDimitry Andric const MCInstrDesc *II, 3035ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap, 3040b57cec5SDimitry Andric bool IsDebug, bool IsClone, bool IsCloned) { 3050b57cec5SDimitry Andric assert(Op.getValueType() != MVT::Other && 3060b57cec5SDimitry Andric Op.getValueType() != MVT::Glue && 3070b57cec5SDimitry Andric "Chain and glue operands should occur at end of operand list!"); 3080b57cec5SDimitry Andric // Get/emit the operand. 3095ffd83dbSDimitry Andric Register VReg = getVR(Op, VRBaseMap); 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric const MCInstrDesc &MCID = MIB->getDesc(); 3120b57cec5SDimitry Andric bool isOptDef = IIOpNum < MCID.getNumOperands() && 3130b57cec5SDimitry Andric MCID.OpInfo[IIOpNum].isOptionalDef(); 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric // If the instruction requires a register in a different class, create 3160b57cec5SDimitry Andric // a new virtual register and copy the value into it, but first attempt to 3170b57cec5SDimitry Andric // shrink VReg's register class within reason. For example, if VReg == GR32 3180b57cec5SDimitry Andric // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP. 3190b57cec5SDimitry Andric if (II) { 3200b57cec5SDimitry Andric const TargetRegisterClass *OpRC = nullptr; 3210b57cec5SDimitry Andric if (IIOpNum < II->getNumOperands()) 3220b57cec5SDimitry Andric OpRC = TII->getRegClass(*II, IIOpNum, TRI, *MF); 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric if (OpRC) { 3250b57cec5SDimitry Andric const TargetRegisterClass *ConstrainedRC 3260b57cec5SDimitry Andric = MRI->constrainRegClass(VReg, OpRC, MinRCSize); 3270b57cec5SDimitry Andric if (!ConstrainedRC) { 3280b57cec5SDimitry Andric OpRC = TRI->getAllocatableClass(OpRC); 3290b57cec5SDimitry Andric assert(OpRC && "Constraints cannot be fulfilled for allocation"); 3308bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(OpRC); 3310b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(), 3320b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg); 3330b57cec5SDimitry Andric VReg = NewVReg; 3340b57cec5SDimitry Andric } else { 3350b57cec5SDimitry Andric assert(ConstrainedRC->isAllocatable() && 3360b57cec5SDimitry Andric "Constraining an allocatable VReg produced an unallocatable class?"); 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // If this value has only one use, that use is a kill. This is a 3420b57cec5SDimitry Andric // conservative approximation. InstrEmitter does trivial coalescing 3430b57cec5SDimitry Andric // with CopyFromReg nodes, so don't emit kill flags for them. 3440b57cec5SDimitry Andric // Avoid kill flags on Schedule cloned nodes, since there will be 3450b57cec5SDimitry Andric // multiple uses. 3460b57cec5SDimitry Andric // Tied operands are never killed, so we need to check that. And that 3470b57cec5SDimitry Andric // means we need to determine the index of the operand. 3480b57cec5SDimitry Andric bool isKill = Op.hasOneUse() && 3490b57cec5SDimitry Andric Op.getNode()->getOpcode() != ISD::CopyFromReg && 3500b57cec5SDimitry Andric !IsDebug && 3510b57cec5SDimitry Andric !(IsClone || IsCloned); 3520b57cec5SDimitry Andric if (isKill) { 3530b57cec5SDimitry Andric unsigned Idx = MIB->getNumOperands(); 3540b57cec5SDimitry Andric while (Idx > 0 && 3550b57cec5SDimitry Andric MIB->getOperand(Idx-1).isReg() && 3560b57cec5SDimitry Andric MIB->getOperand(Idx-1).isImplicit()) 3570b57cec5SDimitry Andric --Idx; 3580b57cec5SDimitry Andric bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1; 3590b57cec5SDimitry Andric if (isTied) 3600b57cec5SDimitry Andric isKill = false; 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) | 3640b57cec5SDimitry Andric getDebugRegState(IsDebug)); 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric /// AddOperand - Add the specified operand to the specified machine instr. II 3680b57cec5SDimitry Andric /// specifies the instruction information for the node, and IIOpNum is the 3690b57cec5SDimitry Andric /// operand number (in the II) that we are adding. 3700b57cec5SDimitry Andric void InstrEmitter::AddOperand(MachineInstrBuilder &MIB, 3710b57cec5SDimitry Andric SDValue Op, 3720b57cec5SDimitry Andric unsigned IIOpNum, 3730b57cec5SDimitry Andric const MCInstrDesc *II, 3745ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap, 3750b57cec5SDimitry Andric bool IsDebug, bool IsClone, bool IsCloned) { 3760b57cec5SDimitry Andric if (Op.isMachineOpcode()) { 3770b57cec5SDimitry Andric AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap, 3780b57cec5SDimitry Andric IsDebug, IsClone, IsCloned); 3790b57cec5SDimitry Andric } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { 3800b57cec5SDimitry Andric MIB.addImm(C->getSExtValue()); 3810b57cec5SDimitry Andric } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) { 3820b57cec5SDimitry Andric MIB.addFPImm(F->getConstantFPValue()); 3830b57cec5SDimitry Andric } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) { 3845ffd83dbSDimitry Andric Register VReg = R->getReg(); 3850b57cec5SDimitry Andric MVT OpVT = Op.getSimpleValueType(); 3860b57cec5SDimitry Andric const TargetRegisterClass *IIRC = 3870b57cec5SDimitry Andric II ? TRI->getAllocatableClass(TII->getRegClass(*II, IIOpNum, TRI, *MF)) 3880b57cec5SDimitry Andric : nullptr; 3890b57cec5SDimitry Andric const TargetRegisterClass *OpRC = 3900b57cec5SDimitry Andric TLI->isTypeLegal(OpVT) 3910b57cec5SDimitry Andric ? TLI->getRegClassFor(OpVT, 3920b57cec5SDimitry Andric Op.getNode()->isDivergent() || 3930b57cec5SDimitry Andric (IIRC && TRI->isDivergentRegClass(IIRC))) 3940b57cec5SDimitry Andric : nullptr; 3950b57cec5SDimitry Andric 3968bcb0991SDimitry Andric if (OpRC && IIRC && OpRC != IIRC && Register::isVirtualRegister(VReg)) { 3978bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(IIRC); 3980b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(), 3990b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg); 4000b57cec5SDimitry Andric VReg = NewVReg; 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric // Turn additional physreg operands into implicit uses on non-variadic 4030b57cec5SDimitry Andric // instructions. This is used by call and return instructions passing 4040b57cec5SDimitry Andric // arguments in registers. 4050b57cec5SDimitry Andric bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic()); 4060b57cec5SDimitry Andric MIB.addReg(VReg, getImplRegState(Imp)); 4070b57cec5SDimitry Andric } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) { 4080b57cec5SDimitry Andric MIB.addRegMask(RM->getRegMask()); 4090b57cec5SDimitry Andric } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) { 4100b57cec5SDimitry Andric MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(), 4110b57cec5SDimitry Andric TGA->getTargetFlags()); 4120b57cec5SDimitry Andric } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) { 4130b57cec5SDimitry Andric MIB.addMBB(BBNode->getBasicBlock()); 4140b57cec5SDimitry Andric } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) { 4150b57cec5SDimitry Andric MIB.addFrameIndex(FI->getIndex()); 4160b57cec5SDimitry Andric } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) { 4170b57cec5SDimitry Andric MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags()); 4180b57cec5SDimitry Andric } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) { 4190b57cec5SDimitry Andric int Offset = CP->getOffset(); 4205ffd83dbSDimitry Andric Align Alignment = CP->getAlign(); 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric unsigned Idx; 4230b57cec5SDimitry Andric MachineConstantPool *MCP = MF->getConstantPool(); 4240b57cec5SDimitry Andric if (CP->isMachineConstantPoolEntry()) 4255ffd83dbSDimitry Andric Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Alignment); 4260b57cec5SDimitry Andric else 4275ffd83dbSDimitry Andric Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Alignment); 4280b57cec5SDimitry Andric MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags()); 4290b57cec5SDimitry Andric } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) { 4300b57cec5SDimitry Andric MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags()); 4310b57cec5SDimitry Andric } else if (auto *SymNode = dyn_cast<MCSymbolSDNode>(Op)) { 4320b57cec5SDimitry Andric MIB.addSym(SymNode->getMCSymbol()); 4330b57cec5SDimitry Andric } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) { 4340b57cec5SDimitry Andric MIB.addBlockAddress(BA->getBlockAddress(), 4350b57cec5SDimitry Andric BA->getOffset(), 4360b57cec5SDimitry Andric BA->getTargetFlags()); 4370b57cec5SDimitry Andric } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) { 4380b57cec5SDimitry Andric MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags()); 4390b57cec5SDimitry Andric } else { 4400b57cec5SDimitry Andric assert(Op.getValueType() != MVT::Other && 4410b57cec5SDimitry Andric Op.getValueType() != MVT::Glue && 4420b57cec5SDimitry Andric "Chain and glue operands should occur at end of operand list!"); 4430b57cec5SDimitry Andric AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap, 4440b57cec5SDimitry Andric IsDebug, IsClone, IsCloned); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric 4485ffd83dbSDimitry Andric Register InstrEmitter::ConstrainForSubReg(Register VReg, unsigned SubIdx, 4490b57cec5SDimitry Andric MVT VT, bool isDivergent, const DebugLoc &DL) { 4500b57cec5SDimitry Andric const TargetRegisterClass *VRC = MRI->getRegClass(VReg); 4510b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric // RC is a sub-class of VRC that supports SubIdx. Try to constrain VReg 4540b57cec5SDimitry Andric // within reason. 4550b57cec5SDimitry Andric if (RC && RC != VRC) 4560b57cec5SDimitry Andric RC = MRI->constrainRegClass(VReg, RC, MinRCSize); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric // VReg has been adjusted. It can be used with SubIdx operands now. 4590b57cec5SDimitry Andric if (RC) 4600b57cec5SDimitry Andric return VReg; 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric // VReg couldn't be reasonably constrained. Emit a COPY to a new virtual 4630b57cec5SDimitry Andric // register instead. 4640b57cec5SDimitry Andric RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT, isDivergent), SubIdx); 4650b57cec5SDimitry Andric assert(RC && "No legal register class for VT supports that SubIdx"); 4668bcb0991SDimitry Andric Register NewReg = MRI->createVirtualRegister(RC); 4670b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg) 4680b57cec5SDimitry Andric .addReg(VReg); 4690b57cec5SDimitry Andric return NewReg; 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric /// EmitSubregNode - Generate machine code for subreg nodes. 4730b57cec5SDimitry Andric /// 4740b57cec5SDimitry Andric void InstrEmitter::EmitSubregNode(SDNode *Node, 4755ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap, 4760b57cec5SDimitry Andric bool IsClone, bool IsCloned) { 4775ffd83dbSDimitry Andric Register VRBase; 4780b57cec5SDimitry Andric unsigned Opc = Node->getMachineOpcode(); 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric // If the node is only used by a CopyToReg and the dest reg is a vreg, use 4810b57cec5SDimitry Andric // the CopyToReg'd destination register instead of creating a new vreg. 4820b57cec5SDimitry Andric for (SDNode *User : Node->uses()) { 4830b57cec5SDimitry Andric if (User->getOpcode() == ISD::CopyToReg && 4840b57cec5SDimitry Andric User->getOperand(2).getNode() == Node) { 4855ffd83dbSDimitry Andric Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 4865ffd83dbSDimitry Andric if (DestReg.isVirtual()) { 4870b57cec5SDimitry Andric VRBase = DestReg; 4880b57cec5SDimitry Andric break; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric if (Opc == TargetOpcode::EXTRACT_SUBREG) { 4940b57cec5SDimitry Andric // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub. There are no 4950b57cec5SDimitry Andric // constraints on the %dst register, COPY can target all legal register 4960b57cec5SDimitry Andric // classes. 4970b57cec5SDimitry Andric unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 4980b57cec5SDimitry Andric const TargetRegisterClass *TRC = 4990b57cec5SDimitry Andric TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent()); 5000b57cec5SDimitry Andric 5015ffd83dbSDimitry Andric Register Reg; 5020b57cec5SDimitry Andric MachineInstr *DefMI; 5030b57cec5SDimitry Andric RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(0)); 5048bcb0991SDimitry Andric if (R && Register::isPhysicalRegister(R->getReg())) { 5050b57cec5SDimitry Andric Reg = R->getReg(); 5060b57cec5SDimitry Andric DefMI = nullptr; 5070b57cec5SDimitry Andric } else { 5080b57cec5SDimitry Andric Reg = R ? R->getReg() : getVR(Node->getOperand(0), VRBaseMap); 5090b57cec5SDimitry Andric DefMI = MRI->getVRegDef(Reg); 5100b57cec5SDimitry Andric } 5110b57cec5SDimitry Andric 5125ffd83dbSDimitry Andric Register SrcReg, DstReg; 5135ffd83dbSDimitry Andric unsigned DefSubIdx; 5140b57cec5SDimitry Andric if (DefMI && 5150b57cec5SDimitry Andric TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) && 5160b57cec5SDimitry Andric SubIdx == DefSubIdx && 5170b57cec5SDimitry Andric TRC == MRI->getRegClass(SrcReg)) { 5180b57cec5SDimitry Andric // Optimize these: 5190b57cec5SDimitry Andric // r1025 = s/zext r1024, 4 5200b57cec5SDimitry Andric // r1026 = extract_subreg r1025, 4 5210b57cec5SDimitry Andric // to a copy 5220b57cec5SDimitry Andric // r1026 = copy r1024 5230b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(TRC); 5240b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 5250b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg); 5260b57cec5SDimitry Andric MRI->clearKillFlags(SrcReg); 5270b57cec5SDimitry Andric } else { 5280b57cec5SDimitry Andric // Reg may not support a SubIdx sub-register, and we may need to 5290b57cec5SDimitry Andric // constrain its register class or issue a COPY to a compatible register 5300b57cec5SDimitry Andric // class. 5315ffd83dbSDimitry Andric if (Reg.isVirtual()) 5320b57cec5SDimitry Andric Reg = ConstrainForSubReg(Reg, SubIdx, 5330b57cec5SDimitry Andric Node->getOperand(0).getSimpleValueType(), 5340b57cec5SDimitry Andric Node->isDivergent(), Node->getDebugLoc()); 5350b57cec5SDimitry Andric // Create the destreg if it is missing. 5365ffd83dbSDimitry Andric if (!VRBase) 5370b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(TRC); 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric // Create the extract_subreg machine instruction. 5400b57cec5SDimitry Andric MachineInstrBuilder CopyMI = 5410b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 5420b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), VRBase); 5435ffd83dbSDimitry Andric if (Reg.isVirtual()) 5440b57cec5SDimitry Andric CopyMI.addReg(Reg, 0, SubIdx); 5450b57cec5SDimitry Andric else 5460b57cec5SDimitry Andric CopyMI.addReg(TRI->getSubReg(Reg, SubIdx)); 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric } else if (Opc == TargetOpcode::INSERT_SUBREG || 5490b57cec5SDimitry Andric Opc == TargetOpcode::SUBREG_TO_REG) { 5500b57cec5SDimitry Andric SDValue N0 = Node->getOperand(0); 5510b57cec5SDimitry Andric SDValue N1 = Node->getOperand(1); 5520b57cec5SDimitry Andric SDValue N2 = Node->getOperand(2); 5530b57cec5SDimitry Andric unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue(); 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric // Figure out the register class to create for the destreg. It should be 5560b57cec5SDimitry Andric // the largest legal register class supporting SubIdx sub-registers. 5570b57cec5SDimitry Andric // RegisterCoalescer will constrain it further if it decides to eliminate 5580b57cec5SDimitry Andric // the INSERT_SUBREG instruction. 5590b57cec5SDimitry Andric // 5600b57cec5SDimitry Andric // %dst = INSERT_SUBREG %src, %sub, SubIdx 5610b57cec5SDimitry Andric // 5620b57cec5SDimitry Andric // is lowered by TwoAddressInstructionPass to: 5630b57cec5SDimitry Andric // 5640b57cec5SDimitry Andric // %dst = COPY %src 5650b57cec5SDimitry Andric // %dst:SubIdx = COPY %sub 5660b57cec5SDimitry Andric // 5670b57cec5SDimitry Andric // There is no constraint on the %src register class. 5680b57cec5SDimitry Andric // 5690b57cec5SDimitry Andric const TargetRegisterClass *SRC = 5700b57cec5SDimitry Andric TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent()); 5710b57cec5SDimitry Andric SRC = TRI->getSubClassWithSubReg(SRC, SubIdx); 5720b57cec5SDimitry Andric assert(SRC && "No register class supports VT and SubIdx for INSERT_SUBREG"); 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric if (VRBase == 0 || !SRC->hasSubClassEq(MRI->getRegClass(VRBase))) 5750b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(SRC); 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric // Create the insert_subreg or subreg_to_reg machine instruction. 5780b57cec5SDimitry Andric MachineInstrBuilder MIB = 5790b57cec5SDimitry Andric BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc), VRBase); 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric // If creating a subreg_to_reg, then the first input operand 5820b57cec5SDimitry Andric // is an implicit value immediate, otherwise it's a register 5830b57cec5SDimitry Andric if (Opc == TargetOpcode::SUBREG_TO_REG) { 5840b57cec5SDimitry Andric const ConstantSDNode *SD = cast<ConstantSDNode>(N0); 5850b57cec5SDimitry Andric MIB.addImm(SD->getZExtValue()); 5860b57cec5SDimitry Andric } else 5870b57cec5SDimitry Andric AddOperand(MIB, N0, 0, nullptr, VRBaseMap, /*IsDebug=*/false, 5880b57cec5SDimitry Andric IsClone, IsCloned); 5890b57cec5SDimitry Andric // Add the subregister being inserted 5900b57cec5SDimitry Andric AddOperand(MIB, N1, 0, nullptr, VRBaseMap, /*IsDebug=*/false, 5910b57cec5SDimitry Andric IsClone, IsCloned); 5920b57cec5SDimitry Andric MIB.addImm(SubIdx); 5930b57cec5SDimitry Andric MBB->insert(InsertPos, MIB); 5940b57cec5SDimitry Andric } else 5950b57cec5SDimitry Andric llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg"); 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric SDValue Op(Node, 0); 5980b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; 5990b57cec5SDimitry Andric (void)isNew; // Silence compiler warning. 6000b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early"); 6010b57cec5SDimitry Andric } 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes. 6040b57cec5SDimitry Andric /// COPY_TO_REGCLASS is just a normal copy, except that the destination 6050b57cec5SDimitry Andric /// register is constrained to be in a particular register class. 6060b57cec5SDimitry Andric /// 6070b57cec5SDimitry Andric void 6080b57cec5SDimitry Andric InstrEmitter::EmitCopyToRegClassNode(SDNode *Node, 6095ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) { 6100b57cec5SDimitry Andric unsigned VReg = getVR(Node->getOperand(0), VRBaseMap); 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric // Create the new VReg in the destination class and emit a copy. 6130b57cec5SDimitry Andric unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); 6140b57cec5SDimitry Andric const TargetRegisterClass *DstRC = 6150b57cec5SDimitry Andric TRI->getAllocatableClass(TRI->getRegClass(DstRCIdx)); 6168bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(DstRC); 6170b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 6180b57cec5SDimitry Andric NewVReg).addReg(VReg); 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric SDValue Op(Node, 0); 6210b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second; 6220b57cec5SDimitry Andric (void)isNew; // Silence compiler warning. 6230b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early"); 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes. 6270b57cec5SDimitry Andric /// 6280b57cec5SDimitry Andric void InstrEmitter::EmitRegSequence(SDNode *Node, 6295ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap, 6300b57cec5SDimitry Andric bool IsClone, bool IsCloned) { 6310b57cec5SDimitry Andric unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue(); 6320b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx); 6338bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(TRI->getAllocatableClass(RC)); 6340b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE); 6350b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg); 6360b57cec5SDimitry Andric unsigned NumOps = Node->getNumOperands(); 6370b57cec5SDimitry Andric // If the input pattern has a chain, then the root of the corresponding 6380b57cec5SDimitry Andric // output pattern will get a chain as well. This can happen to be a 6390b57cec5SDimitry Andric // REG_SEQUENCE (which is not "guarded" by countOperands/CountResults). 6400b57cec5SDimitry Andric if (NumOps && Node->getOperand(NumOps-1).getValueType() == MVT::Other) 6410b57cec5SDimitry Andric --NumOps; // Ignore chain if it exists. 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric assert((NumOps & 1) == 1 && 6440b57cec5SDimitry Andric "REG_SEQUENCE must have an odd number of operands!"); 6450b57cec5SDimitry Andric for (unsigned i = 1; i != NumOps; ++i) { 6460b57cec5SDimitry Andric SDValue Op = Node->getOperand(i); 6470b57cec5SDimitry Andric if ((i & 1) == 0) { 6480b57cec5SDimitry Andric RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(i-1)); 6490b57cec5SDimitry Andric // Skip physical registers as they don't have a vreg to get and we'll 6500b57cec5SDimitry Andric // insert copies for them in TwoAddressInstructionPass anyway. 6518bcb0991SDimitry Andric if (!R || !Register::isPhysicalRegister(R->getReg())) { 6520b57cec5SDimitry Andric unsigned SubIdx = cast<ConstantSDNode>(Op)->getZExtValue(); 6530b57cec5SDimitry Andric unsigned SubReg = getVR(Node->getOperand(i-1), VRBaseMap); 6540b57cec5SDimitry Andric const TargetRegisterClass *TRC = MRI->getRegClass(SubReg); 6550b57cec5SDimitry Andric const TargetRegisterClass *SRC = 6560b57cec5SDimitry Andric TRI->getMatchingSuperRegClass(RC, TRC, SubIdx); 6570b57cec5SDimitry Andric if (SRC && SRC != RC) { 6580b57cec5SDimitry Andric MRI->setRegClass(NewVReg, SRC); 6590b57cec5SDimitry Andric RC = SRC; 6600b57cec5SDimitry Andric } 6610b57cec5SDimitry Andric } 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric AddOperand(MIB, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false, 6640b57cec5SDimitry Andric IsClone, IsCloned); 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric MBB->insert(InsertPos, MIB); 6680b57cec5SDimitry Andric SDValue Op(Node, 0); 6690b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second; 6700b57cec5SDimitry Andric (void)isNew; // Silence compiler warning. 6710b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early"); 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric /// EmitDbgValue - Generate machine instruction for a dbg_value node. 6750b57cec5SDimitry Andric /// 6760b57cec5SDimitry Andric MachineInstr * 6770b57cec5SDimitry Andric InstrEmitter::EmitDbgValue(SDDbgValue *SD, 6785ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) { 6790b57cec5SDimitry Andric MDNode *Var = SD->getVariable(); 68013138422SDimitry Andric MDNode *Expr = SD->getExpression(); 6810b57cec5SDimitry Andric DebugLoc DL = SD->getDebugLoc(); 6820b57cec5SDimitry Andric assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) && 6830b57cec5SDimitry Andric "Expected inlined-at fields to agree"); 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric SD->setIsEmitted(); 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric if (SD->isInvalidated()) { 6880b57cec5SDimitry Andric // An invalidated SDNode must generate an undef DBG_VALUE: although the 6890b57cec5SDimitry Andric // original value is no longer computed, earlier DBG_VALUEs live ranges 6900b57cec5SDimitry Andric // must not leak into later code. 6910b57cec5SDimitry Andric auto MIB = BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE)); 6920b57cec5SDimitry Andric MIB.addReg(0U); 6930b57cec5SDimitry Andric MIB.addReg(0U, RegState::Debug); 6940b57cec5SDimitry Andric MIB.addMetadata(Var); 6950b57cec5SDimitry Andric MIB.addMetadata(Expr); 6960b57cec5SDimitry Andric return &*MIB; 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric 699*e8d8bef9SDimitry Andric // Attempt to produce a DBG_INSTR_REF if we've been asked to. 700*e8d8bef9SDimitry Andric if (EmitDebugInstrRefs) 701*e8d8bef9SDimitry Andric if (auto *InstrRef = EmitDbgInstrRef(SD, VRBaseMap)) 702*e8d8bef9SDimitry Andric return InstrRef; 703*e8d8bef9SDimitry Andric 7040b57cec5SDimitry Andric if (SD->getKind() == SDDbgValue::FRAMEIX) { 7050b57cec5SDimitry Andric // Stack address; this needs to be lowered in target-dependent fashion. 7060b57cec5SDimitry Andric // EmitTargetCodeForFrameDebugValue is responsible for allocation. 7070b57cec5SDimitry Andric auto FrameMI = BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE)) 7080b57cec5SDimitry Andric .addFrameIndex(SD->getFrameIx()); 7090b57cec5SDimitry Andric if (SD->isIndirect()) 71013138422SDimitry Andric // Push [fi + 0] onto the DIExpression stack. 71113138422SDimitry Andric FrameMI.addImm(0); 71213138422SDimitry Andric else 71313138422SDimitry Andric // Push fi onto the DIExpression stack. 7140b57cec5SDimitry Andric FrameMI.addReg(0); 7150b57cec5SDimitry Andric return FrameMI.addMetadata(Var).addMetadata(Expr); 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric // Otherwise, we're going to create an instruction here. 7180b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE); 7190b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MF, DL, II); 7200b57cec5SDimitry Andric if (SD->getKind() == SDDbgValue::SDNODE) { 7210b57cec5SDimitry Andric SDNode *Node = SD->getSDNode(); 7220b57cec5SDimitry Andric SDValue Op = SDValue(Node, SD->getResNo()); 7230b57cec5SDimitry Andric // It's possible we replaced this SDNode with other(s) and therefore 7240b57cec5SDimitry Andric // didn't generate code for it. It's better to catch these cases where 7250b57cec5SDimitry Andric // they happen and transfer the debug info, but trying to guarantee that 7260b57cec5SDimitry Andric // in all cases would be very fragile; this is a safeguard for any 7270b57cec5SDimitry Andric // that were missed. 7285ffd83dbSDimitry Andric DenseMap<SDValue, Register>::iterator I = VRBaseMap.find(Op); 7290b57cec5SDimitry Andric if (I==VRBaseMap.end()) 7300b57cec5SDimitry Andric MIB.addReg(0U); // undef 7310b57cec5SDimitry Andric else 7320b57cec5SDimitry Andric AddOperand(MIB, Op, (*MIB).getNumOperands(), &II, VRBaseMap, 7330b57cec5SDimitry Andric /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false); 7340b57cec5SDimitry Andric } else if (SD->getKind() == SDDbgValue::VREG) { 7350b57cec5SDimitry Andric MIB.addReg(SD->getVReg(), RegState::Debug); 7360b57cec5SDimitry Andric } else if (SD->getKind() == SDDbgValue::CONST) { 7370b57cec5SDimitry Andric const Value *V = SD->getConst(); 7380b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 7390b57cec5SDimitry Andric if (CI->getBitWidth() > 64) 7400b57cec5SDimitry Andric MIB.addCImm(CI); 7410b57cec5SDimitry Andric else 7420b57cec5SDimitry Andric MIB.addImm(CI->getSExtValue()); 7430b57cec5SDimitry Andric } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { 7440b57cec5SDimitry Andric MIB.addFPImm(CF); 7450b57cec5SDimitry Andric } else if (isa<ConstantPointerNull>(V)) { 7460b57cec5SDimitry Andric // Note: This assumes that all nullptr constants are zero-valued. 7470b57cec5SDimitry Andric MIB.addImm(0); 7480b57cec5SDimitry Andric } else { 7490b57cec5SDimitry Andric // Could be an Undef. In any case insert an Undef so we can see what we 7500b57cec5SDimitry Andric // dropped. 7510b57cec5SDimitry Andric MIB.addReg(0U); 7520b57cec5SDimitry Andric } 7530b57cec5SDimitry Andric } else { 7540b57cec5SDimitry Andric // Insert an Undef so we can see what we dropped. 7550b57cec5SDimitry Andric MIB.addReg(0U); 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric 7580b57cec5SDimitry Andric // Indirect addressing is indicated by an Imm as the second parameter. 7590b57cec5SDimitry Andric if (SD->isIndirect()) 76013138422SDimitry Andric MIB.addImm(0U); 76113138422SDimitry Andric else 7620b57cec5SDimitry Andric MIB.addReg(0U, RegState::Debug); 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric MIB.addMetadata(Var); 7650b57cec5SDimitry Andric MIB.addMetadata(Expr); 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric return &*MIB; 7680b57cec5SDimitry Andric } 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric MachineInstr * 771*e8d8bef9SDimitry Andric InstrEmitter::EmitDbgInstrRef(SDDbgValue *SD, 772*e8d8bef9SDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) { 773*e8d8bef9SDimitry Andric // Instruction referencing is still in a prototype state: for now we're only 774*e8d8bef9SDimitry Andric // going to support SDNodes within a block. Copies are not supported, they 775*e8d8bef9SDimitry Andric // don't actually define a value. 776*e8d8bef9SDimitry Andric if (SD->getKind() != SDDbgValue::SDNODE) 777*e8d8bef9SDimitry Andric return nullptr; 778*e8d8bef9SDimitry Andric 779*e8d8bef9SDimitry Andric SDNode *Node = SD->getSDNode(); 780*e8d8bef9SDimitry Andric SDValue Op = SDValue(Node, SD->getResNo()); 781*e8d8bef9SDimitry Andric DenseMap<SDValue, Register>::iterator I = VRBaseMap.find(Op); 782*e8d8bef9SDimitry Andric if (I==VRBaseMap.end()) 783*e8d8bef9SDimitry Andric return nullptr; // undef value: let EmitDbgValue produce a DBG_VALUE $noreg. 784*e8d8bef9SDimitry Andric 785*e8d8bef9SDimitry Andric MDNode *Var = SD->getVariable(); 786*e8d8bef9SDimitry Andric MDNode *Expr = SD->getExpression(); 787*e8d8bef9SDimitry Andric DebugLoc DL = SD->getDebugLoc(); 788*e8d8bef9SDimitry Andric 789*e8d8bef9SDimitry Andric // Try to pick out a defining instruction at this point. 790*e8d8bef9SDimitry Andric unsigned VReg = getVR(Op, VRBaseMap); 791*e8d8bef9SDimitry Andric MachineInstr *ResultInstr = nullptr; 792*e8d8bef9SDimitry Andric 793*e8d8bef9SDimitry Andric // No definition corresponds to scenarios where a vreg is live-in to a block, 794*e8d8bef9SDimitry Andric // and doesn't have a defining instruction (yet). This can be patched up 795*e8d8bef9SDimitry Andric // later; at this early stage of implementation, fall back to using DBG_VALUE. 796*e8d8bef9SDimitry Andric if (!MRI->hasOneDef(VReg)) 797*e8d8bef9SDimitry Andric return nullptr; 798*e8d8bef9SDimitry Andric 799*e8d8bef9SDimitry Andric MachineInstr &DefMI = *MRI->def_instr_begin(VReg); 800*e8d8bef9SDimitry Andric // Some target specific opcodes can become copies. As stated above, we're 801*e8d8bef9SDimitry Andric // ignoring those for now. 802*e8d8bef9SDimitry Andric if (DefMI.isCopy() || DefMI.getOpcode() == TargetOpcode::SUBREG_TO_REG) 803*e8d8bef9SDimitry Andric return nullptr; 804*e8d8bef9SDimitry Andric 805*e8d8bef9SDimitry Andric const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_INSTR_REF); 806*e8d8bef9SDimitry Andric auto MIB = BuildMI(*MF, DL, RefII); 807*e8d8bef9SDimitry Andric 808*e8d8bef9SDimitry Andric // Find the operand which defines the specified VReg. 809*e8d8bef9SDimitry Andric unsigned OperandIdx = 0; 810*e8d8bef9SDimitry Andric for (const auto &MO : DefMI.operands()) { 811*e8d8bef9SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() == VReg) 812*e8d8bef9SDimitry Andric break; 813*e8d8bef9SDimitry Andric ++OperandIdx; 814*e8d8bef9SDimitry Andric } 815*e8d8bef9SDimitry Andric assert(OperandIdx < DefMI.getNumOperands()); 816*e8d8bef9SDimitry Andric 817*e8d8bef9SDimitry Andric // Make the DBG_INSTR_REF refer to that instruction, and that operand. 818*e8d8bef9SDimitry Andric unsigned InstrNum = DefMI.getDebugInstrNum(); 819*e8d8bef9SDimitry Andric MIB.addImm(InstrNum); 820*e8d8bef9SDimitry Andric MIB.addImm(OperandIdx); 821*e8d8bef9SDimitry Andric MIB.addMetadata(Var); 822*e8d8bef9SDimitry Andric MIB.addMetadata(Expr); 823*e8d8bef9SDimitry Andric ResultInstr = &*MIB; 824*e8d8bef9SDimitry Andric return ResultInstr; 825*e8d8bef9SDimitry Andric } 826*e8d8bef9SDimitry Andric 827*e8d8bef9SDimitry Andric MachineInstr * 8280b57cec5SDimitry Andric InstrEmitter::EmitDbgLabel(SDDbgLabel *SD) { 8290b57cec5SDimitry Andric MDNode *Label = SD->getLabel(); 8300b57cec5SDimitry Andric DebugLoc DL = SD->getDebugLoc(); 8310b57cec5SDimitry Andric assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) && 8320b57cec5SDimitry Andric "Expected inlined-at fields to agree"); 8330b57cec5SDimitry Andric 8340b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(TargetOpcode::DBG_LABEL); 8350b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MF, DL, II); 8360b57cec5SDimitry Andric MIB.addMetadata(Label); 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric return &*MIB; 8390b57cec5SDimitry Andric } 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric /// EmitMachineNode - Generate machine code for a target-specific node and 8420b57cec5SDimitry Andric /// needed dependencies. 8430b57cec5SDimitry Andric /// 8440b57cec5SDimitry Andric void InstrEmitter:: 8450b57cec5SDimitry Andric EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned, 8465ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) { 8470b57cec5SDimitry Andric unsigned Opc = Node->getMachineOpcode(); 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric // Handle subreg insert/extract specially 8500b57cec5SDimitry Andric if (Opc == TargetOpcode::EXTRACT_SUBREG || 8510b57cec5SDimitry Andric Opc == TargetOpcode::INSERT_SUBREG || 8520b57cec5SDimitry Andric Opc == TargetOpcode::SUBREG_TO_REG) { 8530b57cec5SDimitry Andric EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned); 8540b57cec5SDimitry Andric return; 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric 8570b57cec5SDimitry Andric // Handle COPY_TO_REGCLASS specially. 8580b57cec5SDimitry Andric if (Opc == TargetOpcode::COPY_TO_REGCLASS) { 8590b57cec5SDimitry Andric EmitCopyToRegClassNode(Node, VRBaseMap); 8600b57cec5SDimitry Andric return; 8610b57cec5SDimitry Andric } 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric // Handle REG_SEQUENCE specially. 8640b57cec5SDimitry Andric if (Opc == TargetOpcode::REG_SEQUENCE) { 8650b57cec5SDimitry Andric EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned); 8660b57cec5SDimitry Andric return; 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric 8690b57cec5SDimitry Andric if (Opc == TargetOpcode::IMPLICIT_DEF) 8700b57cec5SDimitry Andric // We want a unique VR for each IMPLICIT_DEF use. 8710b57cec5SDimitry Andric return; 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(Opc); 8740b57cec5SDimitry Andric unsigned NumResults = CountResults(Node); 8750b57cec5SDimitry Andric unsigned NumDefs = II.getNumDefs(); 8760b57cec5SDimitry Andric const MCPhysReg *ScratchRegs = nullptr; 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric // Handle STACKMAP and PATCHPOINT specially and then use the generic code. 8790b57cec5SDimitry Andric if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) { 8800b57cec5SDimitry Andric // Stackmaps do not have arguments and do not preserve their calling 8810b57cec5SDimitry Andric // convention. However, to simplify runtime support, they clobber the same 8820b57cec5SDimitry Andric // scratch registers as AnyRegCC. 8830b57cec5SDimitry Andric unsigned CC = CallingConv::AnyReg; 8840b57cec5SDimitry Andric if (Opc == TargetOpcode::PATCHPOINT) { 8850b57cec5SDimitry Andric CC = Node->getConstantOperandVal(PatchPointOpers::CCPos); 8860b57cec5SDimitry Andric NumDefs = NumResults; 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC); 889*e8d8bef9SDimitry Andric } else if (Opc == TargetOpcode::STATEPOINT) { 890*e8d8bef9SDimitry Andric NumDefs = NumResults; 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric unsigned NumImpUses = 0; 8940b57cec5SDimitry Andric unsigned NodeOperands = 8950b57cec5SDimitry Andric countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses); 8965ffd83dbSDimitry Andric bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() && 8975ffd83dbSDimitry Andric II.isVariadic() && II.variadicOpsAreDefs(); 8985ffd83dbSDimitry Andric bool HasPhysRegOuts = NumResults > NumDefs && 8995ffd83dbSDimitry Andric II.getImplicitDefs() != nullptr && !HasVRegVariadicDefs; 9000b57cec5SDimitry Andric #ifndef NDEBUG 9010b57cec5SDimitry Andric unsigned NumMIOperands = NodeOperands + NumResults; 9020b57cec5SDimitry Andric if (II.isVariadic()) 9030b57cec5SDimitry Andric assert(NumMIOperands >= II.getNumOperands() && 9040b57cec5SDimitry Andric "Too few operands for a variadic node!"); 9050b57cec5SDimitry Andric else 9060b57cec5SDimitry Andric assert(NumMIOperands >= II.getNumOperands() && 9070b57cec5SDimitry Andric NumMIOperands <= II.getNumOperands() + II.getNumImplicitDefs() + 9080b57cec5SDimitry Andric NumImpUses && 9090b57cec5SDimitry Andric "#operands for dag node doesn't match .td file!"); 9100b57cec5SDimitry Andric #endif 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric // Create the new machine instruction. 9130b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II); 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric // Add result register values for things that are defined by this 9160b57cec5SDimitry Andric // instruction. 9170b57cec5SDimitry Andric if (NumResults) { 9180b57cec5SDimitry Andric CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap); 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric // Transfer any IR flags from the SDNode to the MachineInstr 9210b57cec5SDimitry Andric MachineInstr *MI = MIB.getInstr(); 9220b57cec5SDimitry Andric const SDNodeFlags Flags = Node->getFlags(); 9230b57cec5SDimitry Andric if (Flags.hasNoSignedZeros()) 9240b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmNsz); 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric if (Flags.hasAllowReciprocal()) 9270b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmArcp); 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric if (Flags.hasNoNaNs()) 9300b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmNoNans); 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric if (Flags.hasNoInfs()) 9330b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmNoInfs); 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric if (Flags.hasAllowContract()) 9360b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmContract); 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric if (Flags.hasApproximateFuncs()) 9390b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmAfn); 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric if (Flags.hasAllowReassociation()) 9420b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmReassoc); 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric if (Flags.hasNoUnsignedWrap()) 9450b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::NoUWrap); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric if (Flags.hasNoSignedWrap()) 9480b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::NoSWrap); 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric if (Flags.hasExact()) 9510b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::IsExact); 9520b57cec5SDimitry Andric 953480093f4SDimitry Andric if (Flags.hasNoFPExcept()) 954480093f4SDimitry Andric MI->setFlag(MachineInstr::MIFlag::NoFPExcept); 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric // Emit all of the actual operands of this instruction, adding them to the 9580b57cec5SDimitry Andric // instruction as appropriate. 9590b57cec5SDimitry Andric bool HasOptPRefs = NumDefs > NumResults; 9600b57cec5SDimitry Andric assert((!HasOptPRefs || !HasPhysRegOuts) && 9610b57cec5SDimitry Andric "Unable to cope with optional defs and phys regs defs!"); 9620b57cec5SDimitry Andric unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0; 9630b57cec5SDimitry Andric for (unsigned i = NumSkip; i != NodeOperands; ++i) 9640b57cec5SDimitry Andric AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II, 9650b57cec5SDimitry Andric VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned); 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric // Add scratch registers as implicit def and early clobber 9680b57cec5SDimitry Andric if (ScratchRegs) 9690b57cec5SDimitry Andric for (unsigned i = 0; ScratchRegs[i]; ++i) 9700b57cec5SDimitry Andric MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine | 9710b57cec5SDimitry Andric RegState::EarlyClobber); 9720b57cec5SDimitry Andric 9730b57cec5SDimitry Andric // Set the memory reference descriptions of this instruction now that it is 9740b57cec5SDimitry Andric // part of the function. 9750b57cec5SDimitry Andric MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands()); 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric // Insert the instruction into position in the block. This needs to 9780b57cec5SDimitry Andric // happen before any custom inserter hook is called so that the 9790b57cec5SDimitry Andric // hook knows where in the block to insert the replacement code. 9800b57cec5SDimitry Andric MBB->insert(InsertPos, MIB); 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric // The MachineInstr may also define physregs instead of virtregs. These 9830b57cec5SDimitry Andric // physreg values can reach other instructions in different ways: 9840b57cec5SDimitry Andric // 9850b57cec5SDimitry Andric // 1. When there is a use of a Node value beyond the explicitly defined 9860b57cec5SDimitry Andric // virtual registers, we emit a CopyFromReg for one of the implicitly 9870b57cec5SDimitry Andric // defined physregs. This only happens when HasPhysRegOuts is true. 9880b57cec5SDimitry Andric // 9890b57cec5SDimitry Andric // 2. A CopyFromReg reading a physreg may be glued to this instruction. 9900b57cec5SDimitry Andric // 9910b57cec5SDimitry Andric // 3. A glued instruction may implicitly use a physreg. 9920b57cec5SDimitry Andric // 9930b57cec5SDimitry Andric // 4. A glued instruction may use a RegisterSDNode operand. 9940b57cec5SDimitry Andric // 9950b57cec5SDimitry Andric // Collect all the used physreg defs, and make sure that any unused physreg 9960b57cec5SDimitry Andric // defs are marked as dead. 9978bcb0991SDimitry Andric SmallVector<Register, 8> UsedRegs; 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric // Additional results must be physical register defs. 10000b57cec5SDimitry Andric if (HasPhysRegOuts) { 10010b57cec5SDimitry Andric for (unsigned i = NumDefs; i < NumResults; ++i) { 10028bcb0991SDimitry Andric Register Reg = II.getImplicitDefs()[i - NumDefs]; 10030b57cec5SDimitry Andric if (!Node->hasAnyUseOfValue(i)) 10040b57cec5SDimitry Andric continue; 10050b57cec5SDimitry Andric // This implicitly defined physreg has a use. 10060b57cec5SDimitry Andric UsedRegs.push_back(Reg); 10070b57cec5SDimitry Andric EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap); 10080b57cec5SDimitry Andric } 10090b57cec5SDimitry Andric } 10100b57cec5SDimitry Andric 10110b57cec5SDimitry Andric // Scan the glue chain for any used physregs. 10120b57cec5SDimitry Andric if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) { 10130b57cec5SDimitry Andric for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) { 10140b57cec5SDimitry Andric if (F->getOpcode() == ISD::CopyFromReg) { 10150b57cec5SDimitry Andric UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg()); 10160b57cec5SDimitry Andric continue; 10170b57cec5SDimitry Andric } else if (F->getOpcode() == ISD::CopyToReg) { 10180b57cec5SDimitry Andric // Skip CopyToReg nodes that are internal to the glue chain. 10190b57cec5SDimitry Andric continue; 10200b57cec5SDimitry Andric } 10210b57cec5SDimitry Andric // Collect declared implicit uses. 10220b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(F->getMachineOpcode()); 10230b57cec5SDimitry Andric UsedRegs.append(MCID.getImplicitUses(), 10240b57cec5SDimitry Andric MCID.getImplicitUses() + MCID.getNumImplicitUses()); 10250b57cec5SDimitry Andric // In addition to declared implicit uses, we must also check for 10260b57cec5SDimitry Andric // direct RegisterSDNode operands. 10270b57cec5SDimitry Andric for (unsigned i = 0, e = F->getNumOperands(); i != e; ++i) 10280b57cec5SDimitry Andric if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(F->getOperand(i))) { 10298bcb0991SDimitry Andric Register Reg = R->getReg(); 10308bcb0991SDimitry Andric if (Reg.isPhysical()) 10310b57cec5SDimitry Andric UsedRegs.push_back(Reg); 10320b57cec5SDimitry Andric } 10330b57cec5SDimitry Andric } 10340b57cec5SDimitry Andric } 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric // Finally mark unused registers as dead. 10370b57cec5SDimitry Andric if (!UsedRegs.empty() || II.getImplicitDefs() || II.hasOptionalDef()) 10380b57cec5SDimitry Andric MIB->setPhysRegsDeadExcept(UsedRegs, *TRI); 10390b57cec5SDimitry Andric 1040*e8d8bef9SDimitry Andric // STATEPOINT is too 'dynamic' to have meaningful machine description. 1041*e8d8bef9SDimitry Andric // We have to manually tie operands. 1042*e8d8bef9SDimitry Andric if (Opc == TargetOpcode::STATEPOINT && NumDefs > 0) { 1043*e8d8bef9SDimitry Andric assert(!HasPhysRegOuts && "STATEPOINT mishandled"); 1044*e8d8bef9SDimitry Andric MachineInstr *MI = MIB; 1045*e8d8bef9SDimitry Andric unsigned Def = 0; 1046*e8d8bef9SDimitry Andric int First = StatepointOpers(MI).getFirstGCPtrIdx(); 1047*e8d8bef9SDimitry Andric assert(First > 0 && "Statepoint has Defs but no GC ptr list"); 1048*e8d8bef9SDimitry Andric unsigned Use = (unsigned)First; 1049*e8d8bef9SDimitry Andric while (Def < NumDefs) { 1050*e8d8bef9SDimitry Andric if (MI->getOperand(Use).isReg()) 1051*e8d8bef9SDimitry Andric MI->tieOperands(Def++, Use); 1052*e8d8bef9SDimitry Andric Use = StackMaps::getNextMetaArgIdx(MI, Use); 1053*e8d8bef9SDimitry Andric } 1054*e8d8bef9SDimitry Andric } 1055*e8d8bef9SDimitry Andric 10560b57cec5SDimitry Andric // Run post-isel target hook to adjust this instruction if needed. 10570b57cec5SDimitry Andric if (II.hasPostISelHook()) 10580b57cec5SDimitry Andric TLI->AdjustInstrPostInstrSelection(*MIB, Node); 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric /// EmitSpecialNode - Generate machine code for a target-independent node and 10620b57cec5SDimitry Andric /// needed dependencies. 10630b57cec5SDimitry Andric void InstrEmitter:: 10640b57cec5SDimitry Andric EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned, 10655ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) { 10660b57cec5SDimitry Andric switch (Node->getOpcode()) { 10670b57cec5SDimitry Andric default: 10680b57cec5SDimitry Andric #ifndef NDEBUG 10690b57cec5SDimitry Andric Node->dump(); 10700b57cec5SDimitry Andric #endif 10710b57cec5SDimitry Andric llvm_unreachable("This target-independent node should have been selected!"); 10720b57cec5SDimitry Andric case ISD::EntryToken: 10730b57cec5SDimitry Andric llvm_unreachable("EntryToken should have been excluded from the schedule!"); 10740b57cec5SDimitry Andric case ISD::MERGE_VALUES: 10750b57cec5SDimitry Andric case ISD::TokenFactor: // fall thru 10760b57cec5SDimitry Andric break; 10770b57cec5SDimitry Andric case ISD::CopyToReg: { 10785ffd83dbSDimitry Andric Register DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 10790b57cec5SDimitry Andric SDValue SrcVal = Node->getOperand(2); 10808bcb0991SDimitry Andric if (Register::isVirtualRegister(DestReg) && SrcVal.isMachineOpcode() && 10810b57cec5SDimitry Andric SrcVal.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) { 10820b57cec5SDimitry Andric // Instead building a COPY to that vreg destination, build an 10830b57cec5SDimitry Andric // IMPLICIT_DEF instruction instead. 10840b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 10850b57cec5SDimitry Andric TII->get(TargetOpcode::IMPLICIT_DEF), DestReg); 10860b57cec5SDimitry Andric break; 10870b57cec5SDimitry Andric } 10885ffd83dbSDimitry Andric Register SrcReg; 10890b57cec5SDimitry Andric if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal)) 10900b57cec5SDimitry Andric SrcReg = R->getReg(); 10910b57cec5SDimitry Andric else 10920b57cec5SDimitry Andric SrcReg = getVR(SrcVal, VRBaseMap); 10930b57cec5SDimitry Andric 10940b57cec5SDimitry Andric if (SrcReg == DestReg) // Coalesced away the copy? Ignore. 10950b57cec5SDimitry Andric break; 10960b57cec5SDimitry Andric 10970b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY), 10980b57cec5SDimitry Andric DestReg).addReg(SrcReg); 10990b57cec5SDimitry Andric break; 11000b57cec5SDimitry Andric } 11010b57cec5SDimitry Andric case ISD::CopyFromReg: { 11020b57cec5SDimitry Andric unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); 11030b57cec5SDimitry Andric EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap); 11040b57cec5SDimitry Andric break; 11050b57cec5SDimitry Andric } 11060b57cec5SDimitry Andric case ISD::EH_LABEL: 11070b57cec5SDimitry Andric case ISD::ANNOTATION_LABEL: { 11080b57cec5SDimitry Andric unsigned Opc = (Node->getOpcode() == ISD::EH_LABEL) 11090b57cec5SDimitry Andric ? TargetOpcode::EH_LABEL 11100b57cec5SDimitry Andric : TargetOpcode::ANNOTATION_LABEL; 11110b57cec5SDimitry Andric MCSymbol *S = cast<LabelSDNode>(Node)->getLabel(); 11120b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), 11130b57cec5SDimitry Andric TII->get(Opc)).addSym(S); 11140b57cec5SDimitry Andric break; 11150b57cec5SDimitry Andric } 11160b57cec5SDimitry Andric 11170b57cec5SDimitry Andric case ISD::LIFETIME_START: 11180b57cec5SDimitry Andric case ISD::LIFETIME_END: { 11190b57cec5SDimitry Andric unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START) ? 11200b57cec5SDimitry Andric TargetOpcode::LIFETIME_START : TargetOpcode::LIFETIME_END; 11210b57cec5SDimitry Andric 11220b57cec5SDimitry Andric FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Node->getOperand(1)); 11230b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp)) 11240b57cec5SDimitry Andric .addFrameIndex(FI->getIndex()); 11250b57cec5SDimitry Andric break; 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric 1128*e8d8bef9SDimitry Andric case ISD::PSEUDO_PROBE: { 1129*e8d8bef9SDimitry Andric unsigned TarOp = TargetOpcode::PSEUDO_PROBE; 1130*e8d8bef9SDimitry Andric auto Guid = cast<PseudoProbeSDNode>(Node)->getGuid(); 1131*e8d8bef9SDimitry Andric auto Index = cast<PseudoProbeSDNode>(Node)->getIndex(); 1132*e8d8bef9SDimitry Andric auto Attr = cast<PseudoProbeSDNode>(Node)->getAttributes(); 1133*e8d8bef9SDimitry Andric 1134*e8d8bef9SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp)) 1135*e8d8bef9SDimitry Andric .addImm(Guid) 1136*e8d8bef9SDimitry Andric .addImm(Index) 1137*e8d8bef9SDimitry Andric .addImm((uint8_t)PseudoProbeType::Block) 1138*e8d8bef9SDimitry Andric .addImm(Attr); 1139*e8d8bef9SDimitry Andric break; 1140*e8d8bef9SDimitry Andric } 1141*e8d8bef9SDimitry Andric 11420b57cec5SDimitry Andric case ISD::INLINEASM: 11430b57cec5SDimitry Andric case ISD::INLINEASM_BR: { 11440b57cec5SDimitry Andric unsigned NumOps = Node->getNumOperands(); 11450b57cec5SDimitry Andric if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue) 11460b57cec5SDimitry Andric --NumOps; // Ignore the glue operand. 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric // Create the inline asm machine instruction. 11490b57cec5SDimitry Andric unsigned TgtOpc = Node->getOpcode() == ISD::INLINEASM_BR 11500b57cec5SDimitry Andric ? TargetOpcode::INLINEASM_BR 11510b57cec5SDimitry Andric : TargetOpcode::INLINEASM; 11520b57cec5SDimitry Andric MachineInstrBuilder MIB = 11530b57cec5SDimitry Andric BuildMI(*MF, Node->getDebugLoc(), TII->get(TgtOpc)); 11540b57cec5SDimitry Andric 11550b57cec5SDimitry Andric // Add the asm string as an external symbol operand. 11560b57cec5SDimitry Andric SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString); 11570b57cec5SDimitry Andric const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol(); 11580b57cec5SDimitry Andric MIB.addExternalSymbol(AsmStr); 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore 11610b57cec5SDimitry Andric // bits. 11620b57cec5SDimitry Andric int64_t ExtraInfo = 11630b57cec5SDimitry Andric cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))-> 11640b57cec5SDimitry Andric getZExtValue(); 11650b57cec5SDimitry Andric MIB.addImm(ExtraInfo); 11660b57cec5SDimitry Andric 11670b57cec5SDimitry Andric // Remember to operand index of the group flags. 11680b57cec5SDimitry Andric SmallVector<unsigned, 8> GroupIdx; 11690b57cec5SDimitry Andric 11700b57cec5SDimitry Andric // Remember registers that are part of early-clobber defs. 11710b57cec5SDimitry Andric SmallVector<unsigned, 8> ECRegs; 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric // Add all of the operand registers to the instruction. 11740b57cec5SDimitry Andric for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) { 11750b57cec5SDimitry Andric unsigned Flags = 11760b57cec5SDimitry Andric cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); 11770b57cec5SDimitry Andric const unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 11780b57cec5SDimitry Andric 11790b57cec5SDimitry Andric GroupIdx.push_back(MIB->getNumOperands()); 11800b57cec5SDimitry Andric MIB.addImm(Flags); 11810b57cec5SDimitry Andric ++i; // Skip the ID value. 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andric switch (InlineAsm::getKind(Flags)) { 11840b57cec5SDimitry Andric default: llvm_unreachable("Bad flags!"); 11850b57cec5SDimitry Andric case InlineAsm::Kind_RegDef: 11860b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j, ++i) { 11870b57cec5SDimitry Andric unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 11880b57cec5SDimitry Andric // FIXME: Add dead flags for physical and virtual registers defined. 11890b57cec5SDimitry Andric // For now, mark physical register defs as implicit to help fast 11900b57cec5SDimitry Andric // regalloc. This makes inline asm look a lot like calls. 11918bcb0991SDimitry Andric MIB.addReg(Reg, 11928bcb0991SDimitry Andric RegState::Define | 11938bcb0991SDimitry Andric getImplRegState(Register::isPhysicalRegister(Reg))); 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric break; 11960b57cec5SDimitry Andric case InlineAsm::Kind_RegDefEarlyClobber: 11970b57cec5SDimitry Andric case InlineAsm::Kind_Clobber: 11980b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j, ++i) { 11990b57cec5SDimitry Andric unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); 12008bcb0991SDimitry Andric MIB.addReg(Reg, 12018bcb0991SDimitry Andric RegState::Define | RegState::EarlyClobber | 12028bcb0991SDimitry Andric getImplRegState(Register::isPhysicalRegister(Reg))); 12030b57cec5SDimitry Andric ECRegs.push_back(Reg); 12040b57cec5SDimitry Andric } 12050b57cec5SDimitry Andric break; 12060b57cec5SDimitry Andric case InlineAsm::Kind_RegUse: // Use of register. 12070b57cec5SDimitry Andric case InlineAsm::Kind_Imm: // Immediate. 12080b57cec5SDimitry Andric case InlineAsm::Kind_Mem: // Addressing mode. 12090b57cec5SDimitry Andric // The addressing mode has been selected, just add all of the 12100b57cec5SDimitry Andric // operands to the machine instruction. 12110b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j, ++i) 12120b57cec5SDimitry Andric AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap, 12130b57cec5SDimitry Andric /*IsDebug=*/false, IsClone, IsCloned); 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric // Manually set isTied bits. 12160b57cec5SDimitry Andric if (InlineAsm::getKind(Flags) == InlineAsm::Kind_RegUse) { 12170b57cec5SDimitry Andric unsigned DefGroup = 0; 12180b57cec5SDimitry Andric if (InlineAsm::isUseOperandTiedToDef(Flags, DefGroup)) { 12190b57cec5SDimitry Andric unsigned DefIdx = GroupIdx[DefGroup] + 1; 12200b57cec5SDimitry Andric unsigned UseIdx = GroupIdx.back() + 1; 12210b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j) 12220b57cec5SDimitry Andric MIB->tieOperands(DefIdx + j, UseIdx + j); 12230b57cec5SDimitry Andric } 12240b57cec5SDimitry Andric } 12250b57cec5SDimitry Andric break; 12260b57cec5SDimitry Andric } 12270b57cec5SDimitry Andric } 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric // GCC inline assembly allows input operands to also be early-clobber 12300b57cec5SDimitry Andric // output operands (so long as the operand is written only after it's 12310b57cec5SDimitry Andric // used), but this does not match the semantics of our early-clobber flag. 12320b57cec5SDimitry Andric // If an early-clobber operand register is also an input operand register, 12330b57cec5SDimitry Andric // then remove the early-clobber flag. 12340b57cec5SDimitry Andric for (unsigned Reg : ECRegs) { 12350b57cec5SDimitry Andric if (MIB->readsRegister(Reg, TRI)) { 12360b57cec5SDimitry Andric MachineOperand *MO = 12370b57cec5SDimitry Andric MIB->findRegisterDefOperand(Reg, false, false, TRI); 12380b57cec5SDimitry Andric assert(MO && "No def operand for clobbered register?"); 12390b57cec5SDimitry Andric MO->setIsEarlyClobber(false); 12400b57cec5SDimitry Andric } 12410b57cec5SDimitry Andric } 12420b57cec5SDimitry Andric 12430b57cec5SDimitry Andric // Get the mdnode from the asm if it exists and add it to the instruction. 12440b57cec5SDimitry Andric SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode); 12450b57cec5SDimitry Andric const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD(); 12460b57cec5SDimitry Andric if (MD) 12470b57cec5SDimitry Andric MIB.addMetadata(MD); 12480b57cec5SDimitry Andric 12490b57cec5SDimitry Andric MBB->insert(InsertPos, MIB); 12500b57cec5SDimitry Andric break; 12510b57cec5SDimitry Andric } 12520b57cec5SDimitry Andric } 12530b57cec5SDimitry Andric } 12540b57cec5SDimitry Andric 12550b57cec5SDimitry Andric /// InstrEmitter - Construct an InstrEmitter and set it to start inserting 12560b57cec5SDimitry Andric /// at the given position in the given block. 1257*e8d8bef9SDimitry Andric InstrEmitter::InstrEmitter(const TargetMachine &TM, MachineBasicBlock *mbb, 12580b57cec5SDimitry Andric MachineBasicBlock::iterator insertpos) 12590b57cec5SDimitry Andric : MF(mbb->getParent()), MRI(&MF->getRegInfo()), 12600b57cec5SDimitry Andric TII(MF->getSubtarget().getInstrInfo()), 12610b57cec5SDimitry Andric TRI(MF->getSubtarget().getRegisterInfo()), 12620b57cec5SDimitry Andric TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb), 1263*e8d8bef9SDimitry Andric InsertPos(insertpos) { 1264*e8d8bef9SDimitry Andric EmitDebugInstrRefs = TM.Options.ValueTrackingVariableLocations; 1265*e8d8bef9SDimitry Andric } 1266