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