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"
1781ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.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"
220b57cec5SDimitry Andric #include "llvm/CodeGen/StackMaps.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
2681ad6265SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
27e8d8bef9SDimitry Andric #include "llvm/IR/PseudoProbe.h"
280b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
295ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric #define DEBUG_TYPE "instr-emitter"
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric /// MinRCSize - Smallest register class we allow when constraining virtual
350b57cec5SDimitry Andric /// registers. If satisfying all register class constraints would require
360b57cec5SDimitry Andric /// using a smaller register class, emit a COPY to a new virtual register
370b57cec5SDimitry Andric /// instead.
380b57cec5SDimitry Andric const unsigned MinRCSize = 4;
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric /// CountResults - The results of target nodes have register or immediate
410b57cec5SDimitry Andric /// operands first, then an optional chain, and optional glue operands (which do
420b57cec5SDimitry Andric /// not go into the resulting MachineInstr).
CountResults(SDNode * Node)430b57cec5SDimitry Andric unsigned InstrEmitter::CountResults(SDNode *Node) {
440b57cec5SDimitry Andric unsigned N = Node->getNumValues();
450b57cec5SDimitry Andric while (N && Node->getValueType(N - 1) == MVT::Glue)
460b57cec5SDimitry Andric --N;
470b57cec5SDimitry Andric if (N && Node->getValueType(N - 1) == MVT::Other)
480b57cec5SDimitry Andric --N; // Skip over chain result.
490b57cec5SDimitry Andric return N;
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric /// countOperands - The inputs to target nodes have any actual inputs first,
530b57cec5SDimitry Andric /// followed by an optional chain operand, then an optional glue operand.
540b57cec5SDimitry Andric /// Compute the number of actual operands that will go into the resulting
550b57cec5SDimitry Andric /// MachineInstr.
560b57cec5SDimitry Andric ///
570b57cec5SDimitry Andric /// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding
580b57cec5SDimitry Andric /// the chain and glue. These operands may be implicit on the machine instr.
countOperands(SDNode * Node,unsigned NumExpUses,unsigned & NumImpUses)590b57cec5SDimitry Andric static unsigned countOperands(SDNode *Node, unsigned NumExpUses,
600b57cec5SDimitry Andric unsigned &NumImpUses) {
610b57cec5SDimitry Andric unsigned N = Node->getNumOperands();
620b57cec5SDimitry Andric while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
630b57cec5SDimitry Andric --N;
640b57cec5SDimitry Andric if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
650b57cec5SDimitry Andric --N; // Ignore chain if it exists.
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses.
680b57cec5SDimitry Andric NumImpUses = N - NumExpUses;
690b57cec5SDimitry Andric for (unsigned I = N; I > NumExpUses; --I) {
700b57cec5SDimitry Andric if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1)))
710b57cec5SDimitry Andric continue;
720b57cec5SDimitry Andric if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1)))
73bdd1243dSDimitry Andric if (RN->getReg().isPhysical())
740b57cec5SDimitry Andric continue;
750b57cec5SDimitry Andric NumImpUses = N - I;
760b57cec5SDimitry Andric break;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric return N;
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
830b57cec5SDimitry Andric /// implicit physical register output.
EmitCopyFromReg(SDNode * Node,unsigned ResNo,bool IsClone,Register SrcReg,DenseMap<SDValue,Register> & VRBaseMap)84bdd1243dSDimitry Andric void InstrEmitter::EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone,
85bdd1243dSDimitry Andric Register SrcReg,
86bdd1243dSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
875ffd83dbSDimitry Andric Register VRBase;
885ffd83dbSDimitry Andric if (SrcReg.isVirtual()) {
890b57cec5SDimitry Andric // Just use the input register directly!
900b57cec5SDimitry Andric SDValue Op(Node, ResNo);
910b57cec5SDimitry Andric if (IsClone)
920b57cec5SDimitry Andric VRBaseMap.erase(Op);
930b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
940b57cec5SDimitry Andric (void)isNew; // Silence compiler warning.
950b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early");
960b57cec5SDimitry Andric return;
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric // If the node is only used by a CopyToReg and the dest reg is a vreg, use
1000b57cec5SDimitry Andric // the CopyToReg'd destination register instead of creating a new vreg.
1010b57cec5SDimitry Andric bool MatchReg = true;
1020b57cec5SDimitry Andric const TargetRegisterClass *UseRC = nullptr;
1030b57cec5SDimitry Andric MVT VT = Node->getSimpleValueType(ResNo);
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric // Stick to the preferred register classes for legal types.
1060b57cec5SDimitry Andric if (TLI->isTypeLegal(VT))
1070b57cec5SDimitry Andric UseRC = TLI->getRegClassFor(VT, Node->isDivergent());
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric for (SDNode *User : Node->uses()) {
1100b57cec5SDimitry Andric bool Match = true;
1110b57cec5SDimitry Andric if (User->getOpcode() == ISD::CopyToReg &&
1120b57cec5SDimitry Andric User->getOperand(2).getNode() == Node &&
1130b57cec5SDimitry Andric User->getOperand(2).getResNo() == ResNo) {
1145ffd83dbSDimitry Andric Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
1155ffd83dbSDimitry Andric if (DestReg.isVirtual()) {
1160b57cec5SDimitry Andric VRBase = DestReg;
1170b57cec5SDimitry Andric Match = false;
1180b57cec5SDimitry Andric } else if (DestReg != SrcReg)
1190b57cec5SDimitry Andric Match = false;
1200b57cec5SDimitry Andric } else {
1210b57cec5SDimitry Andric for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
1220b57cec5SDimitry Andric SDValue Op = User->getOperand(i);
1230b57cec5SDimitry Andric if (Op.getNode() != Node || Op.getResNo() != ResNo)
1240b57cec5SDimitry Andric continue;
1250b57cec5SDimitry Andric MVT VT = Node->getSimpleValueType(Op.getResNo());
1260b57cec5SDimitry Andric if (VT == MVT::Other || VT == MVT::Glue)
1270b57cec5SDimitry Andric continue;
1280b57cec5SDimitry Andric Match = false;
1290b57cec5SDimitry Andric if (User->isMachineOpcode()) {
1300b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(User->getMachineOpcode());
1310b57cec5SDimitry Andric const TargetRegisterClass *RC = nullptr;
1320b57cec5SDimitry Andric if (i + II.getNumDefs() < II.getNumOperands()) {
1330b57cec5SDimitry Andric RC = TRI->getAllocatableClass(
1340b57cec5SDimitry Andric TII->getRegClass(II, i + II.getNumDefs(), TRI, *MF));
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric if (!UseRC)
1370b57cec5SDimitry Andric UseRC = RC;
1380b57cec5SDimitry Andric else if (RC) {
1390b57cec5SDimitry Andric const TargetRegisterClass *ComRC =
1408bcb0991SDimitry Andric TRI->getCommonSubClass(UseRC, RC);
1410b57cec5SDimitry Andric // If multiple uses expect disjoint register classes, we emit
1420b57cec5SDimitry Andric // copies in AddRegisterOperand.
1430b57cec5SDimitry Andric if (ComRC)
1440b57cec5SDimitry Andric UseRC = ComRC;
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric MatchReg &= Match;
1500b57cec5SDimitry Andric if (VRBase)
1510b57cec5SDimitry Andric break;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric const TargetRegisterClass *SrcRC = nullptr, *DstRC = nullptr;
1550b57cec5SDimitry Andric SrcRC = TRI->getMinimalPhysRegClass(SrcReg, VT);
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric // Figure out the register class to create for the destreg.
1580b57cec5SDimitry Andric if (VRBase) {
1590b57cec5SDimitry Andric DstRC = MRI->getRegClass(VRBase);
1600b57cec5SDimitry Andric } else if (UseRC) {
1610b57cec5SDimitry Andric assert(TRI->isTypeLegalForClass(*UseRC, VT) &&
1620b57cec5SDimitry Andric "Incompatible phys register def and uses!");
1630b57cec5SDimitry Andric DstRC = UseRC;
164fe6060f1SDimitry Andric } else
165fe6060f1SDimitry Andric DstRC = SrcRC;
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric // If all uses are reading from the src physical register and copying the
1680b57cec5SDimitry Andric // register is either impossible or very expensive, then don't create a copy.
1690b57cec5SDimitry Andric if (MatchReg && SrcRC->getCopyCost() < 0) {
1700b57cec5SDimitry Andric VRBase = SrcReg;
1710b57cec5SDimitry Andric } else {
1720b57cec5SDimitry Andric // Create the reg, emit the copy.
1730b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(DstRC);
1740b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
1750b57cec5SDimitry Andric VRBase).addReg(SrcReg);
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric SDValue Op(Node, ResNo);
1790b57cec5SDimitry Andric if (IsClone)
1800b57cec5SDimitry Andric VRBaseMap.erase(Op);
1810b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
1820b57cec5SDimitry Andric (void)isNew; // Silence compiler warning.
1830b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early");
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric
CreateVirtualRegisters(SDNode * Node,MachineInstrBuilder & MIB,const MCInstrDesc & II,bool IsClone,bool IsCloned,DenseMap<SDValue,Register> & VRBaseMap)1860b57cec5SDimitry Andric void InstrEmitter::CreateVirtualRegisters(SDNode *Node,
1870b57cec5SDimitry Andric MachineInstrBuilder &MIB,
1880b57cec5SDimitry Andric const MCInstrDesc &II,
1890b57cec5SDimitry Andric bool IsClone, bool IsCloned,
1905ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
1910b57cec5SDimitry Andric assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF &&
1920b57cec5SDimitry Andric "IMPLICIT_DEF should have been handled as a special case elsewhere!");
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric unsigned NumResults = CountResults(Node);
1955ffd83dbSDimitry Andric bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() &&
1965ffd83dbSDimitry Andric II.isVariadic() && II.variadicOpsAreDefs();
1975ffd83dbSDimitry Andric unsigned NumVRegs = HasVRegVariadicDefs ? NumResults : II.getNumDefs();
198e8d8bef9SDimitry Andric if (Node->getMachineOpcode() == TargetOpcode::STATEPOINT)
199e8d8bef9SDimitry Andric NumVRegs = NumResults;
2005ffd83dbSDimitry Andric for (unsigned i = 0; i < NumVRegs; ++i) {
2010b57cec5SDimitry Andric // If the specific node value is only used by a CopyToReg and the dest reg
2020b57cec5SDimitry Andric // is a vreg in the same register class, use the CopyToReg'd destination
2030b57cec5SDimitry Andric // register instead of creating a new vreg.
2045ffd83dbSDimitry Andric Register VRBase;
2050b57cec5SDimitry Andric const TargetRegisterClass *RC =
2060b57cec5SDimitry Andric TRI->getAllocatableClass(TII->getRegClass(II, i, TRI, *MF));
2070b57cec5SDimitry Andric // Always let the value type influence the used register class. The
2080b57cec5SDimitry Andric // constraints on the instruction may be too lax to represent the value
2090b57cec5SDimitry Andric // type correctly. For example, a 64-bit float (X86::FR64) can't live in
2100b57cec5SDimitry Andric // the 32-bit float super-class (X86::FR32).
2110b57cec5SDimitry Andric if (i < NumResults && TLI->isTypeLegal(Node->getSimpleValueType(i))) {
2120b57cec5SDimitry Andric const TargetRegisterClass *VTRC = TLI->getRegClassFor(
2130b57cec5SDimitry Andric Node->getSimpleValueType(i),
2140b57cec5SDimitry Andric (Node->isDivergent() || (RC && TRI->isDivergentRegClass(RC))));
2150b57cec5SDimitry Andric if (RC)
2160b57cec5SDimitry Andric VTRC = TRI->getCommonSubClass(RC, VTRC);
2170b57cec5SDimitry Andric if (VTRC)
2180b57cec5SDimitry Andric RC = VTRC;
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric
221bdd1243dSDimitry Andric if (!II.operands().empty() && II.operands()[i].isOptionalDef()) {
2220b57cec5SDimitry Andric // Optional def must be a physical register.
2230b57cec5SDimitry Andric VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg();
2245ffd83dbSDimitry Andric assert(VRBase.isPhysical());
2250b57cec5SDimitry Andric MIB.addReg(VRBase, RegState::Define);
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric if (!VRBase && !IsClone && !IsCloned)
2290b57cec5SDimitry Andric for (SDNode *User : Node->uses()) {
2300b57cec5SDimitry Andric if (User->getOpcode() == ISD::CopyToReg &&
2310b57cec5SDimitry Andric User->getOperand(2).getNode() == Node &&
2320b57cec5SDimitry Andric User->getOperand(2).getResNo() == i) {
233bdd1243dSDimitry Andric Register Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
234bdd1243dSDimitry Andric if (Reg.isVirtual()) {
2350b57cec5SDimitry Andric const TargetRegisterClass *RegRC = MRI->getRegClass(Reg);
2360b57cec5SDimitry Andric if (RegRC == RC) {
2370b57cec5SDimitry Andric VRBase = Reg;
2380b57cec5SDimitry Andric MIB.addReg(VRBase, RegState::Define);
2390b57cec5SDimitry Andric break;
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric // Create the result registers for this node and add the result regs to
2460b57cec5SDimitry Andric // the machine instruction.
2470b57cec5SDimitry Andric if (VRBase == 0) {
2480b57cec5SDimitry Andric assert(RC && "Isn't a register operand!");
2490b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(RC);
2500b57cec5SDimitry Andric MIB.addReg(VRBase, RegState::Define);
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric // If this def corresponds to a result of the SDNode insert the VRBase into
2540b57cec5SDimitry Andric // the lookup map.
2550b57cec5SDimitry Andric if (i < NumResults) {
2560b57cec5SDimitry Andric SDValue Op(Node, i);
2570b57cec5SDimitry Andric if (IsClone)
2580b57cec5SDimitry Andric VRBaseMap.erase(Op);
2590b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
2600b57cec5SDimitry Andric (void)isNew; // Silence compiler warning.
2610b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early");
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric /// getVR - Return the virtual register corresponding to the specified result
2670b57cec5SDimitry Andric /// of the specified node.
getVR(SDValue Op,DenseMap<SDValue,Register> & VRBaseMap)2685ffd83dbSDimitry Andric Register InstrEmitter::getVR(SDValue Op,
2695ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
2700b57cec5SDimitry Andric if (Op.isMachineOpcode() &&
2710b57cec5SDimitry Andric Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
2720b57cec5SDimitry Andric // Add an IMPLICIT_DEF instruction before every use.
2730b57cec5SDimitry Andric // IMPLICIT_DEF can produce any type of result so its MCInstrDesc
2740b57cec5SDimitry Andric // does not include operand register class info.
2750b57cec5SDimitry Andric const TargetRegisterClass *RC = TLI->getRegClassFor(
2760b57cec5SDimitry Andric Op.getSimpleValueType(), Op.getNode()->isDivergent());
2778bcb0991SDimitry Andric Register VReg = MRI->createVirtualRegister(RC);
2780b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Op.getDebugLoc(),
2790b57cec5SDimitry Andric TII->get(TargetOpcode::IMPLICIT_DEF), VReg);
2800b57cec5SDimitry Andric return VReg;
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric
2835ffd83dbSDimitry Andric DenseMap<SDValue, Register>::iterator I = VRBaseMap.find(Op);
2840b57cec5SDimitry Andric assert(I != VRBaseMap.end() && "Node emitted out of order - late");
2850b57cec5SDimitry Andric return I->second;
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric
isConvergenceCtrlMachineOp(SDValue Op)288*0fca6ea1SDimitry Andric static bool isConvergenceCtrlMachineOp(SDValue Op) {
289*0fca6ea1SDimitry Andric if (Op->isMachineOpcode()) {
290*0fca6ea1SDimitry Andric switch (Op->getMachineOpcode()) {
291*0fca6ea1SDimitry Andric case TargetOpcode::CONVERGENCECTRL_ANCHOR:
292*0fca6ea1SDimitry Andric case TargetOpcode::CONVERGENCECTRL_ENTRY:
293*0fca6ea1SDimitry Andric case TargetOpcode::CONVERGENCECTRL_LOOP:
294*0fca6ea1SDimitry Andric case TargetOpcode::CONVERGENCECTRL_GLUE:
295*0fca6ea1SDimitry Andric return true;
296*0fca6ea1SDimitry Andric }
297*0fca6ea1SDimitry Andric return false;
298*0fca6ea1SDimitry Andric }
299*0fca6ea1SDimitry Andric
300*0fca6ea1SDimitry Andric // We can reach here when CopyFromReg is encountered. But rather than making a
301*0fca6ea1SDimitry Andric // special case for that, we just make sure we don't reach here in some
302*0fca6ea1SDimitry Andric // surprising way.
303*0fca6ea1SDimitry Andric switch (Op->getOpcode()) {
304*0fca6ea1SDimitry Andric case ISD::CONVERGENCECTRL_ANCHOR:
305*0fca6ea1SDimitry Andric case ISD::CONVERGENCECTRL_ENTRY:
306*0fca6ea1SDimitry Andric case ISD::CONVERGENCECTRL_LOOP:
307*0fca6ea1SDimitry Andric case ISD::CONVERGENCECTRL_GLUE:
308*0fca6ea1SDimitry Andric llvm_unreachable("Convergence control should have been selected by now.");
309*0fca6ea1SDimitry Andric }
310*0fca6ea1SDimitry Andric return false;
311*0fca6ea1SDimitry Andric }
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric /// AddRegisterOperand - Add the specified register as an operand to the
3140b57cec5SDimitry Andric /// specified machine instr. Insert register copies if the register is
3150b57cec5SDimitry Andric /// not in the required register class.
3160b57cec5SDimitry Andric void
AddRegisterOperand(MachineInstrBuilder & MIB,SDValue Op,unsigned IIOpNum,const MCInstrDesc * II,DenseMap<SDValue,Register> & VRBaseMap,bool IsDebug,bool IsClone,bool IsCloned)3170b57cec5SDimitry Andric InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB,
3180b57cec5SDimitry Andric SDValue Op,
3190b57cec5SDimitry Andric unsigned IIOpNum,
3200b57cec5SDimitry Andric const MCInstrDesc *II,
3215ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap,
3220b57cec5SDimitry Andric bool IsDebug, bool IsClone, bool IsCloned) {
3230b57cec5SDimitry Andric assert(Op.getValueType() != MVT::Other &&
3240b57cec5SDimitry Andric Op.getValueType() != MVT::Glue &&
3250b57cec5SDimitry Andric "Chain and glue operands should occur at end of operand list!");
3260b57cec5SDimitry Andric // Get/emit the operand.
3275ffd83dbSDimitry Andric Register VReg = getVR(Op, VRBaseMap);
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andric const MCInstrDesc &MCID = MIB->getDesc();
3300b57cec5SDimitry Andric bool isOptDef = IIOpNum < MCID.getNumOperands() &&
331bdd1243dSDimitry Andric MCID.operands()[IIOpNum].isOptionalDef();
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric // If the instruction requires a register in a different class, create
3340b57cec5SDimitry Andric // a new virtual register and copy the value into it, but first attempt to
3350b57cec5SDimitry Andric // shrink VReg's register class within reason. For example, if VReg == GR32
3360b57cec5SDimitry Andric // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP.
3370b57cec5SDimitry Andric if (II) {
3380b57cec5SDimitry Andric const TargetRegisterClass *OpRC = nullptr;
3390b57cec5SDimitry Andric if (IIOpNum < II->getNumOperands())
3400b57cec5SDimitry Andric OpRC = TII->getRegClass(*II, IIOpNum, TRI, *MF);
3410b57cec5SDimitry Andric
3420b57cec5SDimitry Andric if (OpRC) {
34381ad6265SDimitry Andric unsigned MinNumRegs = MinRCSize;
34481ad6265SDimitry Andric // Don't apply any RC size limit for IMPLICIT_DEF. Each use has a unique
34581ad6265SDimitry Andric // virtual register.
34681ad6265SDimitry Andric if (Op.isMachineOpcode() &&
34781ad6265SDimitry Andric Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF)
34881ad6265SDimitry Andric MinNumRegs = 0;
34981ad6265SDimitry Andric
3500b57cec5SDimitry Andric const TargetRegisterClass *ConstrainedRC
35181ad6265SDimitry Andric = MRI->constrainRegClass(VReg, OpRC, MinNumRegs);
3520b57cec5SDimitry Andric if (!ConstrainedRC) {
3530b57cec5SDimitry Andric OpRC = TRI->getAllocatableClass(OpRC);
3540b57cec5SDimitry Andric assert(OpRC && "Constraints cannot be fulfilled for allocation");
3558bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(OpRC);
3560b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
3570b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
3580b57cec5SDimitry Andric VReg = NewVReg;
3590b57cec5SDimitry Andric } else {
3600b57cec5SDimitry Andric assert(ConstrainedRC->isAllocatable() &&
3610b57cec5SDimitry Andric "Constraining an allocatable VReg produced an unallocatable class?");
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric // If this value has only one use, that use is a kill. This is a
3670b57cec5SDimitry Andric // conservative approximation. InstrEmitter does trivial coalescing
3680b57cec5SDimitry Andric // with CopyFromReg nodes, so don't emit kill flags for them.
3690b57cec5SDimitry Andric // Avoid kill flags on Schedule cloned nodes, since there will be
3700b57cec5SDimitry Andric // multiple uses.
3710b57cec5SDimitry Andric // Tied operands are never killed, so we need to check that. And that
3720b57cec5SDimitry Andric // means we need to determine the index of the operand.
373*0fca6ea1SDimitry Andric // Don't kill convergence control tokens. Initially they are only used in glue
374*0fca6ea1SDimitry Andric // nodes, and the InstrEmitter later adds implicit uses on the users of the
375*0fca6ea1SDimitry Andric // glue node. This can sometimes make it seem like there is only one use,
376*0fca6ea1SDimitry Andric // which is the glue node itself.
377*0fca6ea1SDimitry Andric bool isKill = Op.hasOneUse() && !isConvergenceCtrlMachineOp(Op) &&
378*0fca6ea1SDimitry Andric Op.getNode()->getOpcode() != ISD::CopyFromReg && !IsDebug &&
3790b57cec5SDimitry Andric !(IsClone || IsCloned);
3800b57cec5SDimitry Andric if (isKill) {
3810b57cec5SDimitry Andric unsigned Idx = MIB->getNumOperands();
3820b57cec5SDimitry Andric while (Idx > 0 &&
3830b57cec5SDimitry Andric MIB->getOperand(Idx-1).isReg() &&
3840b57cec5SDimitry Andric MIB->getOperand(Idx-1).isImplicit())
3850b57cec5SDimitry Andric --Idx;
3860b57cec5SDimitry Andric bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1;
3870b57cec5SDimitry Andric if (isTied)
3880b57cec5SDimitry Andric isKill = false;
3890b57cec5SDimitry Andric }
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) |
3920b57cec5SDimitry Andric getDebugRegState(IsDebug));
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andric /// AddOperand - Add the specified operand to the specified machine instr. II
3960b57cec5SDimitry Andric /// specifies the instruction information for the node, and IIOpNum is the
3970b57cec5SDimitry Andric /// operand number (in the II) that we are adding.
AddOperand(MachineInstrBuilder & MIB,SDValue Op,unsigned IIOpNum,const MCInstrDesc * II,DenseMap<SDValue,Register> & VRBaseMap,bool IsDebug,bool IsClone,bool IsCloned)3980b57cec5SDimitry Andric void InstrEmitter::AddOperand(MachineInstrBuilder &MIB,
3990b57cec5SDimitry Andric SDValue Op,
4000b57cec5SDimitry Andric unsigned IIOpNum,
4010b57cec5SDimitry Andric const MCInstrDesc *II,
4025ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap,
4030b57cec5SDimitry Andric bool IsDebug, bool IsClone, bool IsCloned) {
4040b57cec5SDimitry Andric if (Op.isMachineOpcode()) {
4050b57cec5SDimitry Andric AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
4060b57cec5SDimitry Andric IsDebug, IsClone, IsCloned);
4070b57cec5SDimitry Andric } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4080b57cec5SDimitry Andric MIB.addImm(C->getSExtValue());
4090b57cec5SDimitry Andric } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
4100b57cec5SDimitry Andric MIB.addFPImm(F->getConstantFPValue());
4110b57cec5SDimitry Andric } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
4125ffd83dbSDimitry Andric Register VReg = R->getReg();
4130b57cec5SDimitry Andric MVT OpVT = Op.getSimpleValueType();
4140b57cec5SDimitry Andric const TargetRegisterClass *IIRC =
4150b57cec5SDimitry Andric II ? TRI->getAllocatableClass(TII->getRegClass(*II, IIOpNum, TRI, *MF))
4160b57cec5SDimitry Andric : nullptr;
4170b57cec5SDimitry Andric const TargetRegisterClass *OpRC =
4180b57cec5SDimitry Andric TLI->isTypeLegal(OpVT)
4190b57cec5SDimitry Andric ? TLI->getRegClassFor(OpVT,
4200b57cec5SDimitry Andric Op.getNode()->isDivergent() ||
4210b57cec5SDimitry Andric (IIRC && TRI->isDivergentRegClass(IIRC)))
4220b57cec5SDimitry Andric : nullptr;
4230b57cec5SDimitry Andric
424bdd1243dSDimitry Andric if (OpRC && IIRC && OpRC != IIRC && VReg.isVirtual()) {
4258bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(IIRC);
4260b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
4270b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
4280b57cec5SDimitry Andric VReg = NewVReg;
4290b57cec5SDimitry Andric }
4300b57cec5SDimitry Andric // Turn additional physreg operands into implicit uses on non-variadic
4310b57cec5SDimitry Andric // instructions. This is used by call and return instructions passing
4320b57cec5SDimitry Andric // arguments in registers.
4330b57cec5SDimitry Andric bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic());
4340b57cec5SDimitry Andric MIB.addReg(VReg, getImplRegState(Imp));
4350b57cec5SDimitry Andric } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) {
4360b57cec5SDimitry Andric MIB.addRegMask(RM->getRegMask());
4370b57cec5SDimitry Andric } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
4380b57cec5SDimitry Andric MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(),
4390b57cec5SDimitry Andric TGA->getTargetFlags());
4400b57cec5SDimitry Andric } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
4410b57cec5SDimitry Andric MIB.addMBB(BBNode->getBasicBlock());
4420b57cec5SDimitry Andric } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
4430b57cec5SDimitry Andric MIB.addFrameIndex(FI->getIndex());
4440b57cec5SDimitry Andric } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
4450b57cec5SDimitry Andric MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags());
4460b57cec5SDimitry Andric } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
4470b57cec5SDimitry Andric int Offset = CP->getOffset();
4485ffd83dbSDimitry Andric Align Alignment = CP->getAlign();
4490b57cec5SDimitry Andric
4500b57cec5SDimitry Andric unsigned Idx;
4510b57cec5SDimitry Andric MachineConstantPool *MCP = MF->getConstantPool();
4520b57cec5SDimitry Andric if (CP->isMachineConstantPoolEntry())
4535ffd83dbSDimitry Andric Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Alignment);
4540b57cec5SDimitry Andric else
4555ffd83dbSDimitry Andric Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Alignment);
4560b57cec5SDimitry Andric MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags());
4570b57cec5SDimitry Andric } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
4580b57cec5SDimitry Andric MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags());
4590b57cec5SDimitry Andric } else if (auto *SymNode = dyn_cast<MCSymbolSDNode>(Op)) {
4600b57cec5SDimitry Andric MIB.addSym(SymNode->getMCSymbol());
4610b57cec5SDimitry Andric } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) {
4620b57cec5SDimitry Andric MIB.addBlockAddress(BA->getBlockAddress(),
4630b57cec5SDimitry Andric BA->getOffset(),
4640b57cec5SDimitry Andric BA->getTargetFlags());
4650b57cec5SDimitry Andric } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) {
4660b57cec5SDimitry Andric MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags());
4670b57cec5SDimitry Andric } else {
4680b57cec5SDimitry Andric assert(Op.getValueType() != MVT::Other &&
4690b57cec5SDimitry Andric Op.getValueType() != MVT::Glue &&
4700b57cec5SDimitry Andric "Chain and glue operands should occur at end of operand list!");
4710b57cec5SDimitry Andric AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
4720b57cec5SDimitry Andric IsDebug, IsClone, IsCloned);
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric
ConstrainForSubReg(Register VReg,unsigned SubIdx,MVT VT,bool isDivergent,const DebugLoc & DL)4765ffd83dbSDimitry Andric Register InstrEmitter::ConstrainForSubReg(Register VReg, unsigned SubIdx,
4770b57cec5SDimitry Andric MVT VT, bool isDivergent, const DebugLoc &DL) {
4780b57cec5SDimitry Andric const TargetRegisterClass *VRC = MRI->getRegClass(VReg);
4790b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx);
4800b57cec5SDimitry Andric
4810b57cec5SDimitry Andric // RC is a sub-class of VRC that supports SubIdx. Try to constrain VReg
4820b57cec5SDimitry Andric // within reason.
4830b57cec5SDimitry Andric if (RC && RC != VRC)
4840b57cec5SDimitry Andric RC = MRI->constrainRegClass(VReg, RC, MinRCSize);
4850b57cec5SDimitry Andric
4860b57cec5SDimitry Andric // VReg has been adjusted. It can be used with SubIdx operands now.
4870b57cec5SDimitry Andric if (RC)
4880b57cec5SDimitry Andric return VReg;
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andric // VReg couldn't be reasonably constrained. Emit a COPY to a new virtual
4910b57cec5SDimitry Andric // register instead.
4920b57cec5SDimitry Andric RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT, isDivergent), SubIdx);
4930b57cec5SDimitry Andric assert(RC && "No legal register class for VT supports that SubIdx");
4948bcb0991SDimitry Andric Register NewReg = MRI->createVirtualRegister(RC);
4950b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg)
4960b57cec5SDimitry Andric .addReg(VReg);
4970b57cec5SDimitry Andric return NewReg;
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric
5000b57cec5SDimitry Andric /// EmitSubregNode - Generate machine code for subreg nodes.
5010b57cec5SDimitry Andric ///
EmitSubregNode(SDNode * Node,DenseMap<SDValue,Register> & VRBaseMap,bool IsClone,bool IsCloned)5020b57cec5SDimitry Andric void InstrEmitter::EmitSubregNode(SDNode *Node,
5035ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap,
5040b57cec5SDimitry Andric bool IsClone, bool IsCloned) {
5055ffd83dbSDimitry Andric Register VRBase;
5060b57cec5SDimitry Andric unsigned Opc = Node->getMachineOpcode();
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andric // If the node is only used by a CopyToReg and the dest reg is a vreg, use
5090b57cec5SDimitry Andric // the CopyToReg'd destination register instead of creating a new vreg.
5100b57cec5SDimitry Andric for (SDNode *User : Node->uses()) {
5110b57cec5SDimitry Andric if (User->getOpcode() == ISD::CopyToReg &&
5120b57cec5SDimitry Andric User->getOperand(2).getNode() == Node) {
5135ffd83dbSDimitry Andric Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
5145ffd83dbSDimitry Andric if (DestReg.isVirtual()) {
5150b57cec5SDimitry Andric VRBase = DestReg;
5160b57cec5SDimitry Andric break;
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric }
5190b57cec5SDimitry Andric }
5200b57cec5SDimitry Andric
5210b57cec5SDimitry Andric if (Opc == TargetOpcode::EXTRACT_SUBREG) {
5220b57cec5SDimitry Andric // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub. There are no
5230b57cec5SDimitry Andric // constraints on the %dst register, COPY can target all legal register
5240b57cec5SDimitry Andric // classes.
525647cbc5dSDimitry Andric unsigned SubIdx = Node->getConstantOperandVal(1);
5260b57cec5SDimitry Andric const TargetRegisterClass *TRC =
5270b57cec5SDimitry Andric TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
5280b57cec5SDimitry Andric
5295ffd83dbSDimitry Andric Register Reg;
5300b57cec5SDimitry Andric MachineInstr *DefMI;
5310b57cec5SDimitry Andric RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(0));
532bdd1243dSDimitry Andric if (R && R->getReg().isPhysical()) {
5330b57cec5SDimitry Andric Reg = R->getReg();
5340b57cec5SDimitry Andric DefMI = nullptr;
5350b57cec5SDimitry Andric } else {
5360b57cec5SDimitry Andric Reg = R ? R->getReg() : getVR(Node->getOperand(0), VRBaseMap);
5370b57cec5SDimitry Andric DefMI = MRI->getVRegDef(Reg);
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric
5405ffd83dbSDimitry Andric Register SrcReg, DstReg;
5415ffd83dbSDimitry Andric unsigned DefSubIdx;
5420b57cec5SDimitry Andric if (DefMI &&
5430b57cec5SDimitry Andric TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) &&
5440b57cec5SDimitry Andric SubIdx == DefSubIdx &&
5450b57cec5SDimitry Andric TRC == MRI->getRegClass(SrcReg)) {
5460b57cec5SDimitry Andric // Optimize these:
5470b57cec5SDimitry Andric // r1025 = s/zext r1024, 4
5480b57cec5SDimitry Andric // r1026 = extract_subreg r1025, 4
5490b57cec5SDimitry Andric // to a copy
5500b57cec5SDimitry Andric // r1026 = copy r1024
5510b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(TRC);
5520b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
5530b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg);
5540b57cec5SDimitry Andric MRI->clearKillFlags(SrcReg);
5550b57cec5SDimitry Andric } else {
5560b57cec5SDimitry Andric // Reg may not support a SubIdx sub-register, and we may need to
5570b57cec5SDimitry Andric // constrain its register class or issue a COPY to a compatible register
5580b57cec5SDimitry Andric // class.
5595ffd83dbSDimitry Andric if (Reg.isVirtual())
5600b57cec5SDimitry Andric Reg = ConstrainForSubReg(Reg, SubIdx,
5610b57cec5SDimitry Andric Node->getOperand(0).getSimpleValueType(),
5620b57cec5SDimitry Andric Node->isDivergent(), Node->getDebugLoc());
5630b57cec5SDimitry Andric // Create the destreg if it is missing.
5645ffd83dbSDimitry Andric if (!VRBase)
5650b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(TRC);
5660b57cec5SDimitry Andric
5670b57cec5SDimitry Andric // Create the extract_subreg machine instruction.
5680b57cec5SDimitry Andric MachineInstrBuilder CopyMI =
5690b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
5700b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), VRBase);
5715ffd83dbSDimitry Andric if (Reg.isVirtual())
5720b57cec5SDimitry Andric CopyMI.addReg(Reg, 0, SubIdx);
5730b57cec5SDimitry Andric else
5740b57cec5SDimitry Andric CopyMI.addReg(TRI->getSubReg(Reg, SubIdx));
5750b57cec5SDimitry Andric }
5760b57cec5SDimitry Andric } else if (Opc == TargetOpcode::INSERT_SUBREG ||
5770b57cec5SDimitry Andric Opc == TargetOpcode::SUBREG_TO_REG) {
5780b57cec5SDimitry Andric SDValue N0 = Node->getOperand(0);
5790b57cec5SDimitry Andric SDValue N1 = Node->getOperand(1);
5800b57cec5SDimitry Andric SDValue N2 = Node->getOperand(2);
5811db9f3b2SDimitry Andric unsigned SubIdx = N2->getAsZExtVal();
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric // Figure out the register class to create for the destreg. It should be
5840b57cec5SDimitry Andric // the largest legal register class supporting SubIdx sub-registers.
5850b57cec5SDimitry Andric // RegisterCoalescer will constrain it further if it decides to eliminate
5860b57cec5SDimitry Andric // the INSERT_SUBREG instruction.
5870b57cec5SDimitry Andric //
5880b57cec5SDimitry Andric // %dst = INSERT_SUBREG %src, %sub, SubIdx
5890b57cec5SDimitry Andric //
5900b57cec5SDimitry Andric // is lowered by TwoAddressInstructionPass to:
5910b57cec5SDimitry Andric //
5920b57cec5SDimitry Andric // %dst = COPY %src
5930b57cec5SDimitry Andric // %dst:SubIdx = COPY %sub
5940b57cec5SDimitry Andric //
5950b57cec5SDimitry Andric // There is no constraint on the %src register class.
5960b57cec5SDimitry Andric //
5970b57cec5SDimitry Andric const TargetRegisterClass *SRC =
5980b57cec5SDimitry Andric TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
5990b57cec5SDimitry Andric SRC = TRI->getSubClassWithSubReg(SRC, SubIdx);
6000b57cec5SDimitry Andric assert(SRC && "No register class supports VT and SubIdx for INSERT_SUBREG");
6010b57cec5SDimitry Andric
6020b57cec5SDimitry Andric if (VRBase == 0 || !SRC->hasSubClassEq(MRI->getRegClass(VRBase)))
6030b57cec5SDimitry Andric VRBase = MRI->createVirtualRegister(SRC);
6040b57cec5SDimitry Andric
6050b57cec5SDimitry Andric // Create the insert_subreg or subreg_to_reg machine instruction.
6060b57cec5SDimitry Andric MachineInstrBuilder MIB =
6070b57cec5SDimitry Andric BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc), VRBase);
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric // If creating a subreg_to_reg, then the first input operand
6100b57cec5SDimitry Andric // is an implicit value immediate, otherwise it's a register
6110b57cec5SDimitry Andric if (Opc == TargetOpcode::SUBREG_TO_REG) {
6120b57cec5SDimitry Andric const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
6130b57cec5SDimitry Andric MIB.addImm(SD->getZExtValue());
6140b57cec5SDimitry Andric } else
6150b57cec5SDimitry Andric AddOperand(MIB, N0, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
6160b57cec5SDimitry Andric IsClone, IsCloned);
6170b57cec5SDimitry Andric // Add the subregister being inserted
6180b57cec5SDimitry Andric AddOperand(MIB, N1, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
6190b57cec5SDimitry Andric IsClone, IsCloned);
6200b57cec5SDimitry Andric MIB.addImm(SubIdx);
6210b57cec5SDimitry Andric MBB->insert(InsertPos, MIB);
6220b57cec5SDimitry Andric } else
6230b57cec5SDimitry Andric llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg");
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andric SDValue Op(Node, 0);
6260b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
6270b57cec5SDimitry Andric (void)isNew; // Silence compiler warning.
6280b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early");
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric
6310b57cec5SDimitry Andric /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
6320b57cec5SDimitry Andric /// COPY_TO_REGCLASS is just a normal copy, except that the destination
6330b57cec5SDimitry Andric /// register is constrained to be in a particular register class.
6340b57cec5SDimitry Andric ///
6350b57cec5SDimitry Andric void
EmitCopyToRegClassNode(SDNode * Node,DenseMap<SDValue,Register> & VRBaseMap)6360b57cec5SDimitry Andric InstrEmitter::EmitCopyToRegClassNode(SDNode *Node,
6375ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
6380b57cec5SDimitry Andric unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
6390b57cec5SDimitry Andric
6400b57cec5SDimitry Andric // Create the new VReg in the destination class and emit a copy.
641647cbc5dSDimitry Andric unsigned DstRCIdx = Node->getConstantOperandVal(1);
6420b57cec5SDimitry Andric const TargetRegisterClass *DstRC =
6430b57cec5SDimitry Andric TRI->getAllocatableClass(TRI->getRegClass(DstRCIdx));
6448bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(DstRC);
6450b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
6460b57cec5SDimitry Andric NewVReg).addReg(VReg);
6470b57cec5SDimitry Andric
6480b57cec5SDimitry Andric SDValue Op(Node, 0);
6490b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
6500b57cec5SDimitry Andric (void)isNew; // Silence compiler warning.
6510b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early");
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric
6540b57cec5SDimitry Andric /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
6550b57cec5SDimitry Andric ///
EmitRegSequence(SDNode * Node,DenseMap<SDValue,Register> & VRBaseMap,bool IsClone,bool IsCloned)6560b57cec5SDimitry Andric void InstrEmitter::EmitRegSequence(SDNode *Node,
6575ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap,
6580b57cec5SDimitry Andric bool IsClone, bool IsCloned) {
659647cbc5dSDimitry Andric unsigned DstRCIdx = Node->getConstantOperandVal(0);
6600b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
6618bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(TRI->getAllocatableClass(RC));
6620b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE);
6630b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg);
6640b57cec5SDimitry Andric unsigned NumOps = Node->getNumOperands();
6650b57cec5SDimitry Andric // If the input pattern has a chain, then the root of the corresponding
6660b57cec5SDimitry Andric // output pattern will get a chain as well. This can happen to be a
6670b57cec5SDimitry Andric // REG_SEQUENCE (which is not "guarded" by countOperands/CountResults).
6680b57cec5SDimitry Andric if (NumOps && Node->getOperand(NumOps-1).getValueType() == MVT::Other)
6690b57cec5SDimitry Andric --NumOps; // Ignore chain if it exists.
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andric assert((NumOps & 1) == 1 &&
6720b57cec5SDimitry Andric "REG_SEQUENCE must have an odd number of operands!");
6730b57cec5SDimitry Andric for (unsigned i = 1; i != NumOps; ++i) {
6740b57cec5SDimitry Andric SDValue Op = Node->getOperand(i);
6750b57cec5SDimitry Andric if ((i & 1) == 0) {
6760b57cec5SDimitry Andric RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(i-1));
6770b57cec5SDimitry Andric // Skip physical registers as they don't have a vreg to get and we'll
6780b57cec5SDimitry Andric // insert copies for them in TwoAddressInstructionPass anyway.
679bdd1243dSDimitry Andric if (!R || !R->getReg().isPhysical()) {
6801db9f3b2SDimitry Andric unsigned SubIdx = Op->getAsZExtVal();
6810b57cec5SDimitry Andric unsigned SubReg = getVR(Node->getOperand(i-1), VRBaseMap);
6820b57cec5SDimitry Andric const TargetRegisterClass *TRC = MRI->getRegClass(SubReg);
6830b57cec5SDimitry Andric const TargetRegisterClass *SRC =
6840b57cec5SDimitry Andric TRI->getMatchingSuperRegClass(RC, TRC, SubIdx);
6850b57cec5SDimitry Andric if (SRC && SRC != RC) {
6860b57cec5SDimitry Andric MRI->setRegClass(NewVReg, SRC);
6870b57cec5SDimitry Andric RC = SRC;
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric }
6900b57cec5SDimitry Andric }
6910b57cec5SDimitry Andric AddOperand(MIB, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false,
6920b57cec5SDimitry Andric IsClone, IsCloned);
6930b57cec5SDimitry Andric }
6940b57cec5SDimitry Andric
6950b57cec5SDimitry Andric MBB->insert(InsertPos, MIB);
6960b57cec5SDimitry Andric SDValue Op(Node, 0);
6970b57cec5SDimitry Andric bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
6980b57cec5SDimitry Andric (void)isNew; // Silence compiler warning.
6990b57cec5SDimitry Andric assert(isNew && "Node emitted out of order - early");
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric
7020b57cec5SDimitry Andric /// EmitDbgValue - Generate machine instruction for a dbg_value node.
7030b57cec5SDimitry Andric ///
7040b57cec5SDimitry Andric MachineInstr *
EmitDbgValue(SDDbgValue * SD,DenseMap<SDValue,Register> & VRBaseMap)7050b57cec5SDimitry Andric InstrEmitter::EmitDbgValue(SDDbgValue *SD,
7065ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
7070b57cec5SDimitry Andric DebugLoc DL = SD->getDebugLoc();
708bdd1243dSDimitry Andric assert(cast<DILocalVariable>(SD->getVariable())
709bdd1243dSDimitry Andric ->isValidLocationForIntrinsic(DL) &&
7100b57cec5SDimitry Andric "Expected inlined-at fields to agree");
7110b57cec5SDimitry Andric
7120b57cec5SDimitry Andric SD->setIsEmitted();
7130b57cec5SDimitry Andric
714bdd1243dSDimitry Andric assert(!SD->getLocationOps().empty() &&
715bdd1243dSDimitry Andric "dbg_value with no location operands?");
716fe6060f1SDimitry Andric
717fe6060f1SDimitry Andric if (SD->isInvalidated())
718fe6060f1SDimitry Andric return EmitDbgNoLocation(SD);
719fe6060f1SDimitry Andric
720e8d8bef9SDimitry Andric // Attempt to produce a DBG_INSTR_REF if we've been asked to.
721e8d8bef9SDimitry Andric if (EmitDebugInstrRefs)
722e8d8bef9SDimitry Andric if (auto *InstrRef = EmitDbgInstrRef(SD, VRBaseMap))
723e8d8bef9SDimitry Andric return InstrRef;
724e8d8bef9SDimitry Andric
725bdd1243dSDimitry Andric // Emit variadic dbg_value nodes as DBG_VALUE_LIST if they have not been
726bdd1243dSDimitry Andric // emitted as instruction references.
727bdd1243dSDimitry Andric if (SD->isVariadic())
728bdd1243dSDimitry Andric return EmitDbgValueList(SD, VRBaseMap);
729bdd1243dSDimitry Andric
730bdd1243dSDimitry Andric // Emit single-location dbg_value nodes as DBG_VALUE if they have not been
731bdd1243dSDimitry Andric // emitted as instruction references.
732fe6060f1SDimitry Andric return EmitDbgValueFromSingleOp(SD, VRBaseMap);
7330b57cec5SDimitry Andric }
734fe6060f1SDimitry Andric
GetMOForConstDbgOp(const SDDbgOperand & Op)735bdd1243dSDimitry Andric MachineOperand GetMOForConstDbgOp(const SDDbgOperand &Op) {
736bdd1243dSDimitry Andric const Value *V = Op.getConst();
737bdd1243dSDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
738bdd1243dSDimitry Andric if (CI->getBitWidth() > 64)
739bdd1243dSDimitry Andric return MachineOperand::CreateCImm(CI);
740bdd1243dSDimitry Andric return MachineOperand::CreateImm(CI->getSExtValue());
741bdd1243dSDimitry Andric }
742bdd1243dSDimitry Andric if (const ConstantFP *CF = dyn_cast<ConstantFP>(V))
743bdd1243dSDimitry Andric return MachineOperand::CreateFPImm(CF);
744bdd1243dSDimitry Andric // Note: This assumes that all nullptr constants are zero-valued.
745bdd1243dSDimitry Andric if (isa<ConstantPointerNull>(V))
746bdd1243dSDimitry Andric return MachineOperand::CreateImm(0);
747bdd1243dSDimitry Andric // Undef or unhandled value type, so return an undef operand.
748bdd1243dSDimitry Andric return MachineOperand::CreateReg(
749bdd1243dSDimitry Andric /* Reg */ 0U, /* isDef */ false, /* isImp */ false,
750bdd1243dSDimitry Andric /* isKill */ false, /* isDead */ false,
751bdd1243dSDimitry Andric /* isUndef */ false, /* isEarlyClobber */ false,
752bdd1243dSDimitry Andric /* SubReg */ 0, /* isDebug */ true);
753bdd1243dSDimitry Andric }
754bdd1243dSDimitry Andric
AddDbgValueLocationOps(MachineInstrBuilder & MIB,const MCInstrDesc & DbgValDesc,ArrayRef<SDDbgOperand> LocationOps,DenseMap<SDValue,Register> & VRBaseMap)755fe6060f1SDimitry Andric void InstrEmitter::AddDbgValueLocationOps(
756fe6060f1SDimitry Andric MachineInstrBuilder &MIB, const MCInstrDesc &DbgValDesc,
757fe6060f1SDimitry Andric ArrayRef<SDDbgOperand> LocationOps,
758fe6060f1SDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
759fe6060f1SDimitry Andric for (const SDDbgOperand &Op : LocationOps) {
760fe6060f1SDimitry Andric switch (Op.getKind()) {
761fe6060f1SDimitry Andric case SDDbgOperand::FRAMEIX:
762fe6060f1SDimitry Andric MIB.addFrameIndex(Op.getFrameIx());
763fe6060f1SDimitry Andric break;
764fe6060f1SDimitry Andric case SDDbgOperand::VREG:
765349cc55cSDimitry Andric MIB.addReg(Op.getVReg());
766fe6060f1SDimitry Andric break;
767fe6060f1SDimitry Andric case SDDbgOperand::SDNODE: {
768fe6060f1SDimitry Andric SDValue V = SDValue(Op.getSDNode(), Op.getResNo());
7690b57cec5SDimitry Andric // It's possible we replaced this SDNode with other(s) and therefore
7700b57cec5SDimitry Andric // didn't generate code for it. It's better to catch these cases where
7710b57cec5SDimitry Andric // they happen and transfer the debug info, but trying to guarantee that
7720b57cec5SDimitry Andric // in all cases would be very fragile; this is a safeguard for any
7730b57cec5SDimitry Andric // that were missed.
774fe6060f1SDimitry Andric if (VRBaseMap.count(V) == 0)
7750b57cec5SDimitry Andric MIB.addReg(0U); // undef
7760b57cec5SDimitry Andric else
777fe6060f1SDimitry Andric AddOperand(MIB, V, (*MIB).getNumOperands(), &DbgValDesc, VRBaseMap,
7780b57cec5SDimitry Andric /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false);
779fe6060f1SDimitry Andric } break;
780bdd1243dSDimitry Andric case SDDbgOperand::CONST:
781bdd1243dSDimitry Andric MIB.add(GetMOForConstDbgOp(Op));
782bdd1243dSDimitry Andric break;
7830b57cec5SDimitry Andric }
784fe6060f1SDimitry Andric }
7850b57cec5SDimitry Andric }
7860b57cec5SDimitry Andric
7870b57cec5SDimitry Andric MachineInstr *
EmitDbgInstrRef(SDDbgValue * SD,DenseMap<SDValue,Register> & VRBaseMap)788e8d8bef9SDimitry Andric InstrEmitter::EmitDbgInstrRef(SDDbgValue *SD,
789e8d8bef9SDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
790e8d8bef9SDimitry Andric MDNode *Var = SD->getVariable();
791bdd1243dSDimitry Andric const DIExpression *Expr = (DIExpression *)SD->getExpression();
792e8d8bef9SDimitry Andric DebugLoc DL = SD->getDebugLoc();
793fe6060f1SDimitry Andric const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_INSTR_REF);
794fe6060f1SDimitry Andric
795bdd1243dSDimitry Andric // Returns true if the given operand is not a legal debug operand for a
796bdd1243dSDimitry Andric // DBG_INSTR_REF.
797bdd1243dSDimitry Andric auto IsInvalidOp = [](SDDbgOperand DbgOp) {
798bdd1243dSDimitry Andric return DbgOp.getKind() == SDDbgOperand::FRAMEIX;
799bdd1243dSDimitry Andric };
800bdd1243dSDimitry Andric // Returns true if the given operand is not itself an instruction reference
801bdd1243dSDimitry Andric // but is a legal debug operand for a DBG_INSTR_REF.
802bdd1243dSDimitry Andric auto IsNonInstrRefOp = [](SDDbgOperand DbgOp) {
803bdd1243dSDimitry Andric return DbgOp.getKind() == SDDbgOperand::CONST;
804bdd1243dSDimitry Andric };
805bdd1243dSDimitry Andric
806bdd1243dSDimitry Andric // If this variable location does not depend on any instructions or contains
807bdd1243dSDimitry Andric // any stack locations, produce it as a standard debug value instead.
808bdd1243dSDimitry Andric if (any_of(SD->getLocationOps(), IsInvalidOp) ||
809bdd1243dSDimitry Andric all_of(SD->getLocationOps(), IsNonInstrRefOp)) {
810bdd1243dSDimitry Andric if (SD->isVariadic())
811bdd1243dSDimitry Andric return EmitDbgValueList(SD, VRBaseMap);
812fe6060f1SDimitry Andric return EmitDbgValueFromSingleOp(SD, VRBaseMap);
813bdd1243dSDimitry Andric }
814fe6060f1SDimitry Andric
8154824e7fdSDimitry Andric // Immediately fold any indirectness from the LLVM-IR intrinsic into the
8164824e7fdSDimitry Andric // expression:
817bdd1243dSDimitry Andric if (SD->isIndirect())
818bdd1243dSDimitry Andric Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);
819bdd1243dSDimitry Andric // If this is not already a variadic expression, it must be modified to become
820bdd1243dSDimitry Andric // one.
821bdd1243dSDimitry Andric if (!SD->isVariadic())
822bdd1243dSDimitry Andric Expr = DIExpression::convertToVariadicExpression(Expr);
823bdd1243dSDimitry Andric
824bdd1243dSDimitry Andric SmallVector<MachineOperand> MOs;
8254824e7fdSDimitry Andric
826fe6060f1SDimitry Andric // It may not be immediately possible to identify the MachineInstr that
827fe6060f1SDimitry Andric // defines a VReg, it can depend for example on the order blocks are
828fe6060f1SDimitry Andric // emitted in. When this happens, or when further analysis is needed later,
829fe6060f1SDimitry Andric // produce an instruction like this:
830fe6060f1SDimitry Andric //
831bdd1243dSDimitry Andric // DBG_INSTR_REF !123, !456, %0:gr64
832fe6060f1SDimitry Andric //
833fe6060f1SDimitry Andric // i.e., point the instruction at the vreg, and patch it up later in
834fe6060f1SDimitry Andric // MachineFunction::finalizeDebugInstrRefs.
835bdd1243dSDimitry Andric auto AddVRegOp = [&](unsigned VReg) {
836bdd1243dSDimitry Andric MOs.push_back(MachineOperand::CreateReg(
837bdd1243dSDimitry Andric /* Reg */ VReg, /* isDef */ false, /* isImp */ false,
838bdd1243dSDimitry Andric /* isKill */ false, /* isDead */ false,
839bdd1243dSDimitry Andric /* isUndef */ false, /* isEarlyClobber */ false,
840bdd1243dSDimitry Andric /* SubReg */ 0, /* isDebug */ true));
841fe6060f1SDimitry Andric };
842bdd1243dSDimitry Andric unsigned OpCount = SD->getLocationOps().size();
843bdd1243dSDimitry Andric for (unsigned OpIdx = 0; OpIdx < OpCount; ++OpIdx) {
844bdd1243dSDimitry Andric SDDbgOperand DbgOperand = SD->getLocationOps()[OpIdx];
845fe6060f1SDimitry Andric
846fe6060f1SDimitry Andric // Try to find both the defined register and the instruction defining it.
847fe6060f1SDimitry Andric MachineInstr *DefMI = nullptr;
848fe6060f1SDimitry Andric unsigned VReg;
849fe6060f1SDimitry Andric
850fe6060f1SDimitry Andric if (DbgOperand.getKind() == SDDbgOperand::VREG) {
851fe6060f1SDimitry Andric VReg = DbgOperand.getVReg();
852fe6060f1SDimitry Andric
853fe6060f1SDimitry Andric // No definition means that block hasn't been emitted yet. Leave a vreg
854fe6060f1SDimitry Andric // reference to be fixed later.
855bdd1243dSDimitry Andric if (!MRI->hasOneDef(VReg)) {
856bdd1243dSDimitry Andric AddVRegOp(VReg);
857bdd1243dSDimitry Andric continue;
858bdd1243dSDimitry Andric }
859fe6060f1SDimitry Andric
860fe6060f1SDimitry Andric DefMI = &*MRI->def_instr_begin(VReg);
861bdd1243dSDimitry Andric } else if (DbgOperand.getKind() == SDDbgOperand::SDNODE) {
862fe6060f1SDimitry Andric // Look up the corresponding VReg for the given SDNode, if any.
863fe6060f1SDimitry Andric SDNode *Node = DbgOperand.getSDNode();
864fe6060f1SDimitry Andric SDValue Op = SDValue(Node, DbgOperand.getResNo());
865fe6060f1SDimitry Andric DenseMap<SDValue, Register>::iterator I = VRBaseMap.find(Op);
866fe6060f1SDimitry Andric // No VReg -> produce a DBG_VALUE $noreg instead.
867fe6060f1SDimitry Andric if (I == VRBaseMap.end())
868bdd1243dSDimitry Andric break;
869e8d8bef9SDimitry Andric
870e8d8bef9SDimitry Andric // Try to pick out a defining instruction at this point.
871fe6060f1SDimitry Andric VReg = getVR(Op, VRBaseMap);
872e8d8bef9SDimitry Andric
873fe6060f1SDimitry Andric // Again, if there's no instruction defining the VReg right now, fix it up
874fe6060f1SDimitry Andric // later.
875bdd1243dSDimitry Andric if (!MRI->hasOneDef(VReg)) {
876bdd1243dSDimitry Andric AddVRegOp(VReg);
877bdd1243dSDimitry Andric continue;
878bdd1243dSDimitry Andric }
879e8d8bef9SDimitry Andric
880fe6060f1SDimitry Andric DefMI = &*MRI->def_instr_begin(VReg);
881bdd1243dSDimitry Andric } else {
882bdd1243dSDimitry Andric assert(DbgOperand.getKind() == SDDbgOperand::CONST);
883bdd1243dSDimitry Andric MOs.push_back(GetMOForConstDbgOp(DbgOperand));
884bdd1243dSDimitry Andric continue;
885fe6060f1SDimitry Andric }
886e8d8bef9SDimitry Andric
887fe6060f1SDimitry Andric // Avoid copy like instructions: they don't define values, only move them.
888bdd1243dSDimitry Andric // Leave a virtual-register reference until it can be fixed up later, to
889bdd1243dSDimitry Andric // find the underlying value definition.
890bdd1243dSDimitry Andric if (DefMI->isCopyLike() || TII->isCopyInstr(*DefMI)) {
891bdd1243dSDimitry Andric AddVRegOp(VReg);
892bdd1243dSDimitry Andric continue;
893bdd1243dSDimitry Andric }
894e8d8bef9SDimitry Andric
895fe6060f1SDimitry Andric // Find the operand number which defines the specified VReg.
896e8d8bef9SDimitry Andric unsigned OperandIdx = 0;
897fe6060f1SDimitry Andric for (const auto &MO : DefMI->operands()) {
898e8d8bef9SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() == VReg)
899e8d8bef9SDimitry Andric break;
900e8d8bef9SDimitry Andric ++OperandIdx;
901e8d8bef9SDimitry Andric }
902fe6060f1SDimitry Andric assert(OperandIdx < DefMI->getNumOperands());
903e8d8bef9SDimitry Andric
904e8d8bef9SDimitry Andric // Make the DBG_INSTR_REF refer to that instruction, and that operand.
905fe6060f1SDimitry Andric unsigned InstrNum = DefMI->getDebugInstrNum();
906bdd1243dSDimitry Andric MOs.push_back(MachineOperand::CreateDbgInstrRef(InstrNum, OperandIdx));
907bdd1243dSDimitry Andric }
908bdd1243dSDimitry Andric
909bdd1243dSDimitry Andric // If we haven't created a valid MachineOperand for every DbgOp, abort and
910bdd1243dSDimitry Andric // produce an undef DBG_VALUE.
911bdd1243dSDimitry Andric if (MOs.size() != OpCount)
912bdd1243dSDimitry Andric return EmitDbgNoLocation(SD);
913bdd1243dSDimitry Andric
914bdd1243dSDimitry Andric return BuildMI(*MF, DL, RefII, false, MOs, Var, Expr);
915fe6060f1SDimitry Andric }
916fe6060f1SDimitry Andric
EmitDbgNoLocation(SDDbgValue * SD)917fe6060f1SDimitry Andric MachineInstr *InstrEmitter::EmitDbgNoLocation(SDDbgValue *SD) {
918fe6060f1SDimitry Andric // An invalidated SDNode must generate an undef DBG_VALUE: although the
919fe6060f1SDimitry Andric // original value is no longer computed, earlier DBG_VALUEs live ranges
920fe6060f1SDimitry Andric // must not leak into later code.
921bdd1243dSDimitry Andric DIVariable *Var = SD->getVariable();
922bdd1243dSDimitry Andric const DIExpression *Expr =
923bdd1243dSDimitry Andric DIExpression::convertToUndefExpression(SD->getExpression());
924fe6060f1SDimitry Andric DebugLoc DL = SD->getDebugLoc();
925bdd1243dSDimitry Andric const MCInstrDesc &Desc = TII->get(TargetOpcode::DBG_VALUE);
926bdd1243dSDimitry Andric return BuildMI(*MF, DL, Desc, false, 0U, Var, Expr);
927bdd1243dSDimitry Andric }
928bdd1243dSDimitry Andric
929bdd1243dSDimitry Andric MachineInstr *
EmitDbgValueList(SDDbgValue * SD,DenseMap<SDValue,Register> & VRBaseMap)930bdd1243dSDimitry Andric InstrEmitter::EmitDbgValueList(SDDbgValue *SD,
931bdd1243dSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
932bdd1243dSDimitry Andric MDNode *Var = SD->getVariable();
933bdd1243dSDimitry Andric DIExpression *Expr = SD->getExpression();
934bdd1243dSDimitry Andric DebugLoc DL = SD->getDebugLoc();
935bdd1243dSDimitry Andric // DBG_VALUE_LIST := "DBG_VALUE_LIST" var, expression, loc (, loc)*
936bdd1243dSDimitry Andric const MCInstrDesc &DbgValDesc = TII->get(TargetOpcode::DBG_VALUE_LIST);
937bdd1243dSDimitry Andric // Build the DBG_VALUE_LIST instruction base.
938bdd1243dSDimitry Andric auto MIB = BuildMI(*MF, DL, DbgValDesc);
939fe6060f1SDimitry Andric MIB.addMetadata(Var);
940fe6060f1SDimitry Andric MIB.addMetadata(Expr);
941bdd1243dSDimitry Andric AddDbgValueLocationOps(MIB, DbgValDesc, SD->getLocationOps(), VRBaseMap);
942fe6060f1SDimitry Andric return &*MIB;
943fe6060f1SDimitry Andric }
944fe6060f1SDimitry Andric
945fe6060f1SDimitry Andric MachineInstr *
EmitDbgValueFromSingleOp(SDDbgValue * SD,DenseMap<SDValue,Register> & VRBaseMap)946fe6060f1SDimitry Andric InstrEmitter::EmitDbgValueFromSingleOp(SDDbgValue *SD,
947fe6060f1SDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
948fe6060f1SDimitry Andric MDNode *Var = SD->getVariable();
949349cc55cSDimitry Andric DIExpression *Expr = SD->getExpression();
950fe6060f1SDimitry Andric DebugLoc DL = SD->getDebugLoc();
951fe6060f1SDimitry Andric const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE);
952fe6060f1SDimitry Andric
953fe6060f1SDimitry Andric assert(SD->getLocationOps().size() == 1 &&
954fe6060f1SDimitry Andric "Non variadic dbg_value should have only one location op");
955fe6060f1SDimitry Andric
956349cc55cSDimitry Andric // See about constant-folding the expression.
957349cc55cSDimitry Andric // Copy the location operand in case we replace it.
958349cc55cSDimitry Andric SmallVector<SDDbgOperand, 1> LocationOps(1, SD->getLocationOps()[0]);
959349cc55cSDimitry Andric if (Expr && LocationOps[0].getKind() == SDDbgOperand::CONST) {
960349cc55cSDimitry Andric const Value *V = LocationOps[0].getConst();
961349cc55cSDimitry Andric if (auto *C = dyn_cast<ConstantInt>(V)) {
962349cc55cSDimitry Andric std::tie(Expr, C) = Expr->constantFold(C);
963349cc55cSDimitry Andric LocationOps[0] = SDDbgOperand::fromConst(C);
964349cc55cSDimitry Andric }
965349cc55cSDimitry Andric }
966349cc55cSDimitry Andric
967fe6060f1SDimitry Andric // Emit non-variadic dbg_value nodes as DBG_VALUE.
968fe6060f1SDimitry Andric // DBG_VALUE := "DBG_VALUE" loc, isIndirect, var, expr
969fe6060f1SDimitry Andric auto MIB = BuildMI(*MF, DL, II);
970349cc55cSDimitry Andric AddDbgValueLocationOps(MIB, II, LocationOps, VRBaseMap);
971fe6060f1SDimitry Andric
972fe6060f1SDimitry Andric if (SD->isIndirect())
973fe6060f1SDimitry Andric MIB.addImm(0U);
974fe6060f1SDimitry Andric else
975349cc55cSDimitry Andric MIB.addReg(0U);
976fe6060f1SDimitry Andric
977fe6060f1SDimitry Andric return MIB.addMetadata(Var).addMetadata(Expr);
978e8d8bef9SDimitry Andric }
979e8d8bef9SDimitry Andric
980e8d8bef9SDimitry Andric MachineInstr *
EmitDbgLabel(SDDbgLabel * SD)9810b57cec5SDimitry Andric InstrEmitter::EmitDbgLabel(SDDbgLabel *SD) {
9820b57cec5SDimitry Andric MDNode *Label = SD->getLabel();
9830b57cec5SDimitry Andric DebugLoc DL = SD->getDebugLoc();
9840b57cec5SDimitry Andric assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
9850b57cec5SDimitry Andric "Expected inlined-at fields to agree");
9860b57cec5SDimitry Andric
9870b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(TargetOpcode::DBG_LABEL);
9880b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
9890b57cec5SDimitry Andric MIB.addMetadata(Label);
9900b57cec5SDimitry Andric
9910b57cec5SDimitry Andric return &*MIB;
9920b57cec5SDimitry Andric }
9930b57cec5SDimitry Andric
9940b57cec5SDimitry Andric /// EmitMachineNode - Generate machine code for a target-specific node and
9950b57cec5SDimitry Andric /// needed dependencies.
9960b57cec5SDimitry Andric ///
9970b57cec5SDimitry Andric void InstrEmitter::
EmitMachineNode(SDNode * Node,bool IsClone,bool IsCloned,DenseMap<SDValue,Register> & VRBaseMap)9980b57cec5SDimitry Andric EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
9995ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
10000b57cec5SDimitry Andric unsigned Opc = Node->getMachineOpcode();
10010b57cec5SDimitry Andric
10020b57cec5SDimitry Andric // Handle subreg insert/extract specially
10030b57cec5SDimitry Andric if (Opc == TargetOpcode::EXTRACT_SUBREG ||
10040b57cec5SDimitry Andric Opc == TargetOpcode::INSERT_SUBREG ||
10050b57cec5SDimitry Andric Opc == TargetOpcode::SUBREG_TO_REG) {
10060b57cec5SDimitry Andric EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned);
10070b57cec5SDimitry Andric return;
10080b57cec5SDimitry Andric }
10090b57cec5SDimitry Andric
10100b57cec5SDimitry Andric // Handle COPY_TO_REGCLASS specially.
10110b57cec5SDimitry Andric if (Opc == TargetOpcode::COPY_TO_REGCLASS) {
10120b57cec5SDimitry Andric EmitCopyToRegClassNode(Node, VRBaseMap);
10130b57cec5SDimitry Andric return;
10140b57cec5SDimitry Andric }
10150b57cec5SDimitry Andric
10160b57cec5SDimitry Andric // Handle REG_SEQUENCE specially.
10170b57cec5SDimitry Andric if (Opc == TargetOpcode::REG_SEQUENCE) {
10180b57cec5SDimitry Andric EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned);
10190b57cec5SDimitry Andric return;
10200b57cec5SDimitry Andric }
10210b57cec5SDimitry Andric
10220b57cec5SDimitry Andric if (Opc == TargetOpcode::IMPLICIT_DEF)
10230b57cec5SDimitry Andric // We want a unique VR for each IMPLICIT_DEF use.
10240b57cec5SDimitry Andric return;
10250b57cec5SDimitry Andric
10260b57cec5SDimitry Andric const MCInstrDesc &II = TII->get(Opc);
10270b57cec5SDimitry Andric unsigned NumResults = CountResults(Node);
10280b57cec5SDimitry Andric unsigned NumDefs = II.getNumDefs();
10290b57cec5SDimitry Andric const MCPhysReg *ScratchRegs = nullptr;
10300b57cec5SDimitry Andric
10310b57cec5SDimitry Andric // Handle STACKMAP and PATCHPOINT specially and then use the generic code.
10320b57cec5SDimitry Andric if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
10330b57cec5SDimitry Andric // Stackmaps do not have arguments and do not preserve their calling
10340b57cec5SDimitry Andric // convention. However, to simplify runtime support, they clobber the same
10350b57cec5SDimitry Andric // scratch registers as AnyRegCC.
10360b57cec5SDimitry Andric unsigned CC = CallingConv::AnyReg;
10370b57cec5SDimitry Andric if (Opc == TargetOpcode::PATCHPOINT) {
10380b57cec5SDimitry Andric CC = Node->getConstantOperandVal(PatchPointOpers::CCPos);
10390b57cec5SDimitry Andric NumDefs = NumResults;
10400b57cec5SDimitry Andric }
10410b57cec5SDimitry Andric ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC);
1042e8d8bef9SDimitry Andric } else if (Opc == TargetOpcode::STATEPOINT) {
1043e8d8bef9SDimitry Andric NumDefs = NumResults;
10440b57cec5SDimitry Andric }
10450b57cec5SDimitry Andric
10460b57cec5SDimitry Andric unsigned NumImpUses = 0;
10470b57cec5SDimitry Andric unsigned NodeOperands =
10480b57cec5SDimitry Andric countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses);
10495ffd83dbSDimitry Andric bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() &&
10505ffd83dbSDimitry Andric II.isVariadic() && II.variadicOpsAreDefs();
1051bdd1243dSDimitry Andric bool HasPhysRegOuts = NumResults > NumDefs && !II.implicit_defs().empty() &&
1052bdd1243dSDimitry Andric !HasVRegVariadicDefs;
10530b57cec5SDimitry Andric #ifndef NDEBUG
10540b57cec5SDimitry Andric unsigned NumMIOperands = NodeOperands + NumResults;
10550b57cec5SDimitry Andric if (II.isVariadic())
10560b57cec5SDimitry Andric assert(NumMIOperands >= II.getNumOperands() &&
10570b57cec5SDimitry Andric "Too few operands for a variadic node!");
10580b57cec5SDimitry Andric else
10590b57cec5SDimitry Andric assert(NumMIOperands >= II.getNumOperands() &&
1060bdd1243dSDimitry Andric NumMIOperands <=
1061bdd1243dSDimitry Andric II.getNumOperands() + II.implicit_defs().size() + NumImpUses &&
10620b57cec5SDimitry Andric "#operands for dag node doesn't match .td file!");
10630b57cec5SDimitry Andric #endif
10640b57cec5SDimitry Andric
10650b57cec5SDimitry Andric // Create the new machine instruction.
10660b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II);
10670b57cec5SDimitry Andric
10680b57cec5SDimitry Andric // Add result register values for things that are defined by this
10690b57cec5SDimitry Andric // instruction.
10700b57cec5SDimitry Andric if (NumResults) {
10710b57cec5SDimitry Andric CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap);
10720b57cec5SDimitry Andric
10730b57cec5SDimitry Andric // Transfer any IR flags from the SDNode to the MachineInstr
10740b57cec5SDimitry Andric MachineInstr *MI = MIB.getInstr();
10750b57cec5SDimitry Andric const SDNodeFlags Flags = Node->getFlags();
10760b57cec5SDimitry Andric if (Flags.hasNoSignedZeros())
10770b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmNsz);
10780b57cec5SDimitry Andric
10790b57cec5SDimitry Andric if (Flags.hasAllowReciprocal())
10800b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmArcp);
10810b57cec5SDimitry Andric
10820b57cec5SDimitry Andric if (Flags.hasNoNaNs())
10830b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmNoNans);
10840b57cec5SDimitry Andric
10850b57cec5SDimitry Andric if (Flags.hasNoInfs())
10860b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmNoInfs);
10870b57cec5SDimitry Andric
10880b57cec5SDimitry Andric if (Flags.hasAllowContract())
10890b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmContract);
10900b57cec5SDimitry Andric
10910b57cec5SDimitry Andric if (Flags.hasApproximateFuncs())
10920b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmAfn);
10930b57cec5SDimitry Andric
10940b57cec5SDimitry Andric if (Flags.hasAllowReassociation())
10950b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::FmReassoc);
10960b57cec5SDimitry Andric
10970b57cec5SDimitry Andric if (Flags.hasNoUnsignedWrap())
10980b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::NoUWrap);
10990b57cec5SDimitry Andric
11000b57cec5SDimitry Andric if (Flags.hasNoSignedWrap())
11010b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::NoSWrap);
11020b57cec5SDimitry Andric
11030b57cec5SDimitry Andric if (Flags.hasExact())
11040b57cec5SDimitry Andric MI->setFlag(MachineInstr::MIFlag::IsExact);
11050b57cec5SDimitry Andric
1106480093f4SDimitry Andric if (Flags.hasNoFPExcept())
1107480093f4SDimitry Andric MI->setFlag(MachineInstr::MIFlag::NoFPExcept);
110806c3fb27SDimitry Andric
110906c3fb27SDimitry Andric if (Flags.hasUnpredictable())
111006c3fb27SDimitry Andric MI->setFlag(MachineInstr::MIFlag::Unpredictable);
11110b57cec5SDimitry Andric }
11120b57cec5SDimitry Andric
11130b57cec5SDimitry Andric // Emit all of the actual operands of this instruction, adding them to the
11140b57cec5SDimitry Andric // instruction as appropriate.
11150b57cec5SDimitry Andric bool HasOptPRefs = NumDefs > NumResults;
11160b57cec5SDimitry Andric assert((!HasOptPRefs || !HasPhysRegOuts) &&
11170b57cec5SDimitry Andric "Unable to cope with optional defs and phys regs defs!");
11180b57cec5SDimitry Andric unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0;
11190b57cec5SDimitry Andric for (unsigned i = NumSkip; i != NodeOperands; ++i)
11200b57cec5SDimitry Andric AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II,
11210b57cec5SDimitry Andric VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned);
11220b57cec5SDimitry Andric
11230b57cec5SDimitry Andric // Add scratch registers as implicit def and early clobber
11240b57cec5SDimitry Andric if (ScratchRegs)
11250b57cec5SDimitry Andric for (unsigned i = 0; ScratchRegs[i]; ++i)
11260b57cec5SDimitry Andric MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine |
11270b57cec5SDimitry Andric RegState::EarlyClobber);
11280b57cec5SDimitry Andric
11290b57cec5SDimitry Andric // Set the memory reference descriptions of this instruction now that it is
11300b57cec5SDimitry Andric // part of the function.
11310b57cec5SDimitry Andric MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands());
11320b57cec5SDimitry Andric
1133bdd1243dSDimitry Andric // Set the CFI type.
1134bdd1243dSDimitry Andric MIB->setCFIType(*MF, Node->getCFIType());
1135bdd1243dSDimitry Andric
11360b57cec5SDimitry Andric // Insert the instruction into position in the block. This needs to
11370b57cec5SDimitry Andric // happen before any custom inserter hook is called so that the
11380b57cec5SDimitry Andric // hook knows where in the block to insert the replacement code.
11390b57cec5SDimitry Andric MBB->insert(InsertPos, MIB);
11400b57cec5SDimitry Andric
11410b57cec5SDimitry Andric // The MachineInstr may also define physregs instead of virtregs. These
11420b57cec5SDimitry Andric // physreg values can reach other instructions in different ways:
11430b57cec5SDimitry Andric //
11440b57cec5SDimitry Andric // 1. When there is a use of a Node value beyond the explicitly defined
11450b57cec5SDimitry Andric // virtual registers, we emit a CopyFromReg for one of the implicitly
11460b57cec5SDimitry Andric // defined physregs. This only happens when HasPhysRegOuts is true.
11470b57cec5SDimitry Andric //
11480b57cec5SDimitry Andric // 2. A CopyFromReg reading a physreg may be glued to this instruction.
11490b57cec5SDimitry Andric //
11500b57cec5SDimitry Andric // 3. A glued instruction may implicitly use a physreg.
11510b57cec5SDimitry Andric //
11520b57cec5SDimitry Andric // 4. A glued instruction may use a RegisterSDNode operand.
11530b57cec5SDimitry Andric //
11540b57cec5SDimitry Andric // Collect all the used physreg defs, and make sure that any unused physreg
11550b57cec5SDimitry Andric // defs are marked as dead.
11568bcb0991SDimitry Andric SmallVector<Register, 8> UsedRegs;
11570b57cec5SDimitry Andric
11580b57cec5SDimitry Andric // Additional results must be physical register defs.
11590b57cec5SDimitry Andric if (HasPhysRegOuts) {
11600b57cec5SDimitry Andric for (unsigned i = NumDefs; i < NumResults; ++i) {
1161bdd1243dSDimitry Andric Register Reg = II.implicit_defs()[i - NumDefs];
11620b57cec5SDimitry Andric if (!Node->hasAnyUseOfValue(i))
11630b57cec5SDimitry Andric continue;
11640b57cec5SDimitry Andric // This implicitly defined physreg has a use.
11650b57cec5SDimitry Andric UsedRegs.push_back(Reg);
1166bdd1243dSDimitry Andric EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
11670b57cec5SDimitry Andric }
11680b57cec5SDimitry Andric }
11690b57cec5SDimitry Andric
11700b57cec5SDimitry Andric // Scan the glue chain for any used physregs.
11710b57cec5SDimitry Andric if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) {
11720b57cec5SDimitry Andric for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) {
11730b57cec5SDimitry Andric if (F->getOpcode() == ISD::CopyFromReg) {
11740b57cec5SDimitry Andric UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg());
11750b57cec5SDimitry Andric continue;
11760b57cec5SDimitry Andric } else if (F->getOpcode() == ISD::CopyToReg) {
11770b57cec5SDimitry Andric // Skip CopyToReg nodes that are internal to the glue chain.
11780b57cec5SDimitry Andric continue;
11790b57cec5SDimitry Andric }
11800b57cec5SDimitry Andric // Collect declared implicit uses.
11810b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(F->getMachineOpcode());
1182bdd1243dSDimitry Andric append_range(UsedRegs, MCID.implicit_uses());
11830b57cec5SDimitry Andric // In addition to declared implicit uses, we must also check for
11840b57cec5SDimitry Andric // direct RegisterSDNode operands.
1185*0fca6ea1SDimitry Andric for (const SDValue &Op : F->op_values())
1186*0fca6ea1SDimitry Andric if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
11878bcb0991SDimitry Andric Register Reg = R->getReg();
11888bcb0991SDimitry Andric if (Reg.isPhysical())
11890b57cec5SDimitry Andric UsedRegs.push_back(Reg);
11900b57cec5SDimitry Andric }
11910b57cec5SDimitry Andric }
11920b57cec5SDimitry Andric }
11930b57cec5SDimitry Andric
119406c3fb27SDimitry Andric // Add rounding control registers as implicit def for function call.
119506c3fb27SDimitry Andric if (II.isCall() && MF->getFunction().hasFnAttribute(Attribute::StrictFP)) {
119606c3fb27SDimitry Andric ArrayRef<MCPhysReg> RCRegs = TLI->getRoundingControlRegisters();
119706c3fb27SDimitry Andric for (MCPhysReg Reg : RCRegs)
119806c3fb27SDimitry Andric UsedRegs.push_back(Reg);
119906c3fb27SDimitry Andric }
120006c3fb27SDimitry Andric
12010b57cec5SDimitry Andric // Finally mark unused registers as dead.
1202bdd1243dSDimitry Andric if (!UsedRegs.empty() || !II.implicit_defs().empty() || II.hasOptionalDef())
12030b57cec5SDimitry Andric MIB->setPhysRegsDeadExcept(UsedRegs, *TRI);
12040b57cec5SDimitry Andric
1205e8d8bef9SDimitry Andric // STATEPOINT is too 'dynamic' to have meaningful machine description.
1206e8d8bef9SDimitry Andric // We have to manually tie operands.
1207e8d8bef9SDimitry Andric if (Opc == TargetOpcode::STATEPOINT && NumDefs > 0) {
1208e8d8bef9SDimitry Andric assert(!HasPhysRegOuts && "STATEPOINT mishandled");
1209e8d8bef9SDimitry Andric MachineInstr *MI = MIB;
1210e8d8bef9SDimitry Andric unsigned Def = 0;
1211e8d8bef9SDimitry Andric int First = StatepointOpers(MI).getFirstGCPtrIdx();
1212e8d8bef9SDimitry Andric assert(First > 0 && "Statepoint has Defs but no GC ptr list");
1213e8d8bef9SDimitry Andric unsigned Use = (unsigned)First;
1214e8d8bef9SDimitry Andric while (Def < NumDefs) {
1215e8d8bef9SDimitry Andric if (MI->getOperand(Use).isReg())
1216e8d8bef9SDimitry Andric MI->tieOperands(Def++, Use);
1217e8d8bef9SDimitry Andric Use = StackMaps::getNextMetaArgIdx(MI, Use);
1218e8d8bef9SDimitry Andric }
1219e8d8bef9SDimitry Andric }
1220e8d8bef9SDimitry Andric
1221*0fca6ea1SDimitry Andric if (SDNode *GluedNode = Node->getGluedNode()) {
1222*0fca6ea1SDimitry Andric // FIXME: Possibly iterate over multiple glue nodes?
1223*0fca6ea1SDimitry Andric if (GluedNode->getOpcode() ==
1224*0fca6ea1SDimitry Andric ~(unsigned)TargetOpcode::CONVERGENCECTRL_GLUE) {
1225*0fca6ea1SDimitry Andric Register VReg = getVR(GluedNode->getOperand(0), VRBaseMap);
1226*0fca6ea1SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(VReg, /*isDef=*/false,
1227*0fca6ea1SDimitry Andric /*isImp=*/true);
1228*0fca6ea1SDimitry Andric MIB->addOperand(MO);
1229*0fca6ea1SDimitry Andric }
1230*0fca6ea1SDimitry Andric }
1231*0fca6ea1SDimitry Andric
12320b57cec5SDimitry Andric // Run post-isel target hook to adjust this instruction if needed.
12330b57cec5SDimitry Andric if (II.hasPostISelHook())
12340b57cec5SDimitry Andric TLI->AdjustInstrPostInstrSelection(*MIB, Node);
12350b57cec5SDimitry Andric }
12360b57cec5SDimitry Andric
12370b57cec5SDimitry Andric /// EmitSpecialNode - Generate machine code for a target-independent node and
12380b57cec5SDimitry Andric /// needed dependencies.
12390b57cec5SDimitry Andric void InstrEmitter::
EmitSpecialNode(SDNode * Node,bool IsClone,bool IsCloned,DenseMap<SDValue,Register> & VRBaseMap)12400b57cec5SDimitry Andric EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
12415ffd83dbSDimitry Andric DenseMap<SDValue, Register> &VRBaseMap) {
12420b57cec5SDimitry Andric switch (Node->getOpcode()) {
12430b57cec5SDimitry Andric default:
12440b57cec5SDimitry Andric #ifndef NDEBUG
12450b57cec5SDimitry Andric Node->dump();
12460b57cec5SDimitry Andric #endif
12470b57cec5SDimitry Andric llvm_unreachable("This target-independent node should have been selected!");
12480b57cec5SDimitry Andric case ISD::EntryToken:
12490b57cec5SDimitry Andric case ISD::MERGE_VALUES:
12500b57cec5SDimitry Andric case ISD::TokenFactor: // fall thru
12510b57cec5SDimitry Andric break;
12520b57cec5SDimitry Andric case ISD::CopyToReg: {
12535ffd83dbSDimitry Andric Register DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
12540b57cec5SDimitry Andric SDValue SrcVal = Node->getOperand(2);
1255bdd1243dSDimitry Andric if (DestReg.isVirtual() && SrcVal.isMachineOpcode() &&
12560b57cec5SDimitry Andric SrcVal.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
12570b57cec5SDimitry Andric // Instead building a COPY to that vreg destination, build an
12580b57cec5SDimitry Andric // IMPLICIT_DEF instruction instead.
12590b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
12600b57cec5SDimitry Andric TII->get(TargetOpcode::IMPLICIT_DEF), DestReg);
12610b57cec5SDimitry Andric break;
12620b57cec5SDimitry Andric }
12635ffd83dbSDimitry Andric Register SrcReg;
12640b57cec5SDimitry Andric if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
12650b57cec5SDimitry Andric SrcReg = R->getReg();
12660b57cec5SDimitry Andric else
12670b57cec5SDimitry Andric SrcReg = getVR(SrcVal, VRBaseMap);
12680b57cec5SDimitry Andric
12690b57cec5SDimitry Andric if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
12700b57cec5SDimitry Andric break;
12710b57cec5SDimitry Andric
12720b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
12730b57cec5SDimitry Andric DestReg).addReg(SrcReg);
12740b57cec5SDimitry Andric break;
12750b57cec5SDimitry Andric }
12760b57cec5SDimitry Andric case ISD::CopyFromReg: {
12770b57cec5SDimitry Andric unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1278bdd1243dSDimitry Andric EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
12790b57cec5SDimitry Andric break;
12800b57cec5SDimitry Andric }
12810b57cec5SDimitry Andric case ISD::EH_LABEL:
12820b57cec5SDimitry Andric case ISD::ANNOTATION_LABEL: {
12830b57cec5SDimitry Andric unsigned Opc = (Node->getOpcode() == ISD::EH_LABEL)
12840b57cec5SDimitry Andric ? TargetOpcode::EH_LABEL
12850b57cec5SDimitry Andric : TargetOpcode::ANNOTATION_LABEL;
12860b57cec5SDimitry Andric MCSymbol *S = cast<LabelSDNode>(Node)->getLabel();
12870b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
12880b57cec5SDimitry Andric TII->get(Opc)).addSym(S);
12890b57cec5SDimitry Andric break;
12900b57cec5SDimitry Andric }
12910b57cec5SDimitry Andric
12920b57cec5SDimitry Andric case ISD::LIFETIME_START:
12930b57cec5SDimitry Andric case ISD::LIFETIME_END: {
1294fe6060f1SDimitry Andric unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START)
1295fe6060f1SDimitry Andric ? TargetOpcode::LIFETIME_START
1296fe6060f1SDimitry Andric : TargetOpcode::LIFETIME_END;
1297fe6060f1SDimitry Andric auto *FI = cast<FrameIndexSDNode>(Node->getOperand(1));
12980b57cec5SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
12990b57cec5SDimitry Andric .addFrameIndex(FI->getIndex());
13000b57cec5SDimitry Andric break;
13010b57cec5SDimitry Andric }
13020b57cec5SDimitry Andric
1303e8d8bef9SDimitry Andric case ISD::PSEUDO_PROBE: {
1304e8d8bef9SDimitry Andric unsigned TarOp = TargetOpcode::PSEUDO_PROBE;
1305e8d8bef9SDimitry Andric auto Guid = cast<PseudoProbeSDNode>(Node)->getGuid();
1306e8d8bef9SDimitry Andric auto Index = cast<PseudoProbeSDNode>(Node)->getIndex();
1307e8d8bef9SDimitry Andric auto Attr = cast<PseudoProbeSDNode>(Node)->getAttributes();
1308e8d8bef9SDimitry Andric
1309e8d8bef9SDimitry Andric BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
1310e8d8bef9SDimitry Andric .addImm(Guid)
1311e8d8bef9SDimitry Andric .addImm(Index)
1312e8d8bef9SDimitry Andric .addImm((uint8_t)PseudoProbeType::Block)
1313e8d8bef9SDimitry Andric .addImm(Attr);
1314e8d8bef9SDimitry Andric break;
1315e8d8bef9SDimitry Andric }
1316e8d8bef9SDimitry Andric
13170b57cec5SDimitry Andric case ISD::INLINEASM:
13180b57cec5SDimitry Andric case ISD::INLINEASM_BR: {
13190b57cec5SDimitry Andric unsigned NumOps = Node->getNumOperands();
13200b57cec5SDimitry Andric if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
13210b57cec5SDimitry Andric --NumOps; // Ignore the glue operand.
13220b57cec5SDimitry Andric
13230b57cec5SDimitry Andric // Create the inline asm machine instruction.
13240b57cec5SDimitry Andric unsigned TgtOpc = Node->getOpcode() == ISD::INLINEASM_BR
13250b57cec5SDimitry Andric ? TargetOpcode::INLINEASM_BR
13260b57cec5SDimitry Andric : TargetOpcode::INLINEASM;
13270b57cec5SDimitry Andric MachineInstrBuilder MIB =
13280b57cec5SDimitry Andric BuildMI(*MF, Node->getDebugLoc(), TII->get(TgtOpc));
13290b57cec5SDimitry Andric
13300b57cec5SDimitry Andric // Add the asm string as an external symbol operand.
13310b57cec5SDimitry Andric SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString);
13320b57cec5SDimitry Andric const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol();
13330b57cec5SDimitry Andric MIB.addExternalSymbol(AsmStr);
13340b57cec5SDimitry Andric
13350b57cec5SDimitry Andric // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore
13360b57cec5SDimitry Andric // bits.
13370b57cec5SDimitry Andric int64_t ExtraInfo =
13380b57cec5SDimitry Andric cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))->
13390b57cec5SDimitry Andric getZExtValue();
13400b57cec5SDimitry Andric MIB.addImm(ExtraInfo);
13410b57cec5SDimitry Andric
13420b57cec5SDimitry Andric // Remember to operand index of the group flags.
13430b57cec5SDimitry Andric SmallVector<unsigned, 8> GroupIdx;
13440b57cec5SDimitry Andric
13450b57cec5SDimitry Andric // Remember registers that are part of early-clobber defs.
13460b57cec5SDimitry Andric SmallVector<unsigned, 8> ECRegs;
13470b57cec5SDimitry Andric
13480b57cec5SDimitry Andric // Add all of the operand registers to the instruction.
13490b57cec5SDimitry Andric for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
1350647cbc5dSDimitry Andric unsigned Flags = Node->getConstantOperandVal(i);
13515f757f3fSDimitry Andric const InlineAsm::Flag F(Flags);
13525f757f3fSDimitry Andric const unsigned NumVals = F.getNumOperandRegisters();
13530b57cec5SDimitry Andric
13540b57cec5SDimitry Andric GroupIdx.push_back(MIB->getNumOperands());
13550b57cec5SDimitry Andric MIB.addImm(Flags);
13560b57cec5SDimitry Andric ++i; // Skip the ID value.
13570b57cec5SDimitry Andric
13585f757f3fSDimitry Andric switch (F.getKind()) {
13595f757f3fSDimitry Andric case InlineAsm::Kind::RegDef:
13600b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j, ++i) {
1361bdd1243dSDimitry Andric Register Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
13620b57cec5SDimitry Andric // FIXME: Add dead flags for physical and virtual registers defined.
13630b57cec5SDimitry Andric // For now, mark physical register defs as implicit to help fast
13640b57cec5SDimitry Andric // regalloc. This makes inline asm look a lot like calls.
1365bdd1243dSDimitry Andric MIB.addReg(Reg, RegState::Define | getImplRegState(Reg.isPhysical()));
13660b57cec5SDimitry Andric }
13670b57cec5SDimitry Andric break;
13685f757f3fSDimitry Andric case InlineAsm::Kind::RegDefEarlyClobber:
13695f757f3fSDimitry Andric case InlineAsm::Kind::Clobber:
13700b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j, ++i) {
1371bdd1243dSDimitry Andric Register Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1372bdd1243dSDimitry Andric MIB.addReg(Reg, RegState::Define | RegState::EarlyClobber |
1373bdd1243dSDimitry Andric getImplRegState(Reg.isPhysical()));
13740b57cec5SDimitry Andric ECRegs.push_back(Reg);
13750b57cec5SDimitry Andric }
13760b57cec5SDimitry Andric break;
13775f757f3fSDimitry Andric case InlineAsm::Kind::RegUse: // Use of register.
13785f757f3fSDimitry Andric case InlineAsm::Kind::Imm: // Immediate.
13795f757f3fSDimitry Andric case InlineAsm::Kind::Mem: // Non-function addressing mode.
13800b57cec5SDimitry Andric // The addressing mode has been selected, just add all of the
13810b57cec5SDimitry Andric // operands to the machine instruction.
13820b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j, ++i)
13830b57cec5SDimitry Andric AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap,
13840b57cec5SDimitry Andric /*IsDebug=*/false, IsClone, IsCloned);
13850b57cec5SDimitry Andric
13860b57cec5SDimitry Andric // Manually set isTied bits.
13875f757f3fSDimitry Andric if (F.isRegUseKind()) {
13885f757f3fSDimitry Andric unsigned DefGroup;
13895f757f3fSDimitry Andric if (F.isUseOperandTiedToDef(DefGroup)) {
13900b57cec5SDimitry Andric unsigned DefIdx = GroupIdx[DefGroup] + 1;
13910b57cec5SDimitry Andric unsigned UseIdx = GroupIdx.back() + 1;
13920b57cec5SDimitry Andric for (unsigned j = 0; j != NumVals; ++j)
13930b57cec5SDimitry Andric MIB->tieOperands(DefIdx + j, UseIdx + j);
13940b57cec5SDimitry Andric }
13950b57cec5SDimitry Andric }
13960b57cec5SDimitry Andric break;
13975f757f3fSDimitry Andric case InlineAsm::Kind::Func: // Function addressing mode.
1398bdd1243dSDimitry Andric for (unsigned j = 0; j != NumVals; ++j, ++i) {
1399bdd1243dSDimitry Andric SDValue Op = Node->getOperand(i);
1400bdd1243dSDimitry Andric AddOperand(MIB, Op, 0, nullptr, VRBaseMap,
1401bdd1243dSDimitry Andric /*IsDebug=*/false, IsClone, IsCloned);
1402bdd1243dSDimitry Andric
1403bdd1243dSDimitry Andric // Adjust Target Flags for function reference.
1404bdd1243dSDimitry Andric if (auto *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
1405bdd1243dSDimitry Andric unsigned NewFlags =
1406bdd1243dSDimitry Andric MF->getSubtarget().classifyGlobalFunctionReference(
1407bdd1243dSDimitry Andric TGA->getGlobal());
1408bdd1243dSDimitry Andric unsigned LastIdx = MIB.getInstr()->getNumOperands() - 1;
1409bdd1243dSDimitry Andric MIB.getInstr()->getOperand(LastIdx).setTargetFlags(NewFlags);
1410bdd1243dSDimitry Andric }
1411bdd1243dSDimitry Andric }
14120b57cec5SDimitry Andric }
14130b57cec5SDimitry Andric }
14140b57cec5SDimitry Andric
1415*0fca6ea1SDimitry Andric // Add rounding control registers as implicit def for inline asm.
1416*0fca6ea1SDimitry Andric if (MF->getFunction().hasFnAttribute(Attribute::StrictFP)) {
1417*0fca6ea1SDimitry Andric ArrayRef<MCPhysReg> RCRegs = TLI->getRoundingControlRegisters();
1418*0fca6ea1SDimitry Andric for (MCPhysReg Reg : RCRegs)
1419*0fca6ea1SDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine);
1420*0fca6ea1SDimitry Andric }
1421*0fca6ea1SDimitry Andric
14220b57cec5SDimitry Andric // GCC inline assembly allows input operands to also be early-clobber
14230b57cec5SDimitry Andric // output operands (so long as the operand is written only after it's
14240b57cec5SDimitry Andric // used), but this does not match the semantics of our early-clobber flag.
14250b57cec5SDimitry Andric // If an early-clobber operand register is also an input operand register,
14260b57cec5SDimitry Andric // then remove the early-clobber flag.
14270b57cec5SDimitry Andric for (unsigned Reg : ECRegs) {
14280b57cec5SDimitry Andric if (MIB->readsRegister(Reg, TRI)) {
14290b57cec5SDimitry Andric MachineOperand *MO =
1430*0fca6ea1SDimitry Andric MIB->findRegisterDefOperand(Reg, TRI, false, false);
14310b57cec5SDimitry Andric assert(MO && "No def operand for clobbered register?");
14320b57cec5SDimitry Andric MO->setIsEarlyClobber(false);
14330b57cec5SDimitry Andric }
14340b57cec5SDimitry Andric }
14350b57cec5SDimitry Andric
14360b57cec5SDimitry Andric // Get the mdnode from the asm if it exists and add it to the instruction.
14370b57cec5SDimitry Andric SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode);
14380b57cec5SDimitry Andric const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD();
14390b57cec5SDimitry Andric if (MD)
14400b57cec5SDimitry Andric MIB.addMetadata(MD);
14410b57cec5SDimitry Andric
14420b57cec5SDimitry Andric MBB->insert(InsertPos, MIB);
14430b57cec5SDimitry Andric break;
14440b57cec5SDimitry Andric }
14450b57cec5SDimitry Andric }
14460b57cec5SDimitry Andric }
14470b57cec5SDimitry Andric
14480b57cec5SDimitry Andric /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
14490b57cec5SDimitry Andric /// at the given position in the given block.
InstrEmitter(const TargetMachine & TM,MachineBasicBlock * mbb,MachineBasicBlock::iterator insertpos)1450e8d8bef9SDimitry Andric InstrEmitter::InstrEmitter(const TargetMachine &TM, MachineBasicBlock *mbb,
1451bdd1243dSDimitry Andric MachineBasicBlock::iterator insertpos)
14520b57cec5SDimitry Andric : MF(mbb->getParent()), MRI(&MF->getRegInfo()),
14530b57cec5SDimitry Andric TII(MF->getSubtarget().getInstrInfo()),
14540b57cec5SDimitry Andric TRI(MF->getSubtarget().getRegisterInfo()),
14550b57cec5SDimitry Andric TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb),
1456e8d8bef9SDimitry Andric InsertPos(insertpos) {
1457bdd1243dSDimitry Andric EmitDebugInstrRefs = mbb->getParent()->useDebugInstrRef();
1458e8d8bef9SDimitry Andric }
1459