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