LLVM 24.0.0git
InstrEmitter.cpp
Go to the documentation of this file.
1//==--- InstrEmitter.cpp - Emit MachineInstrs for the SelectionDAG class ---==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the Emit routines for the SelectionDAG class, which creates
10// MachineInstrs based on the decisions of the SelectionDAG instruction
11// selection.
12//
13//===----------------------------------------------------------------------===//
14
15#include "InstrEmitter.h"
16#include "SDNodeDbgValue.h"
29#include "llvm/IR/PseudoProbe.h"
32using namespace llvm;
33
34#define DEBUG_TYPE "instr-emitter"
35
36/// MinRCSize - Smallest register class we allow when constraining virtual
37/// registers. If satisfying all register class constraints would require
38/// using a smaller register class, emit a COPY to a new virtual register
39/// instead.
40const unsigned MinRCSize = 4;
41
42/// CountResults - The results of target nodes have register or immediate
43/// operands first, then an optional chain, and optional glue operands (which do
44/// not go into the resulting MachineInstr).
46 unsigned N = Node->getNumValues();
47 while (N && Node->getValueType(N - 1) == MVT::Glue)
48 --N;
49 if (N && Node->getValueType(N - 1) == MVT::Other)
50 --N; // Skip over chain result.
51 return N;
52}
53
54/// countOperands - The inputs to target nodes have any actual inputs first,
55/// followed by an optional chain operand, then an optional glue operand.
56/// Compute the number of actual operands that will go into the resulting
57/// MachineInstr.
58///
59/// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding
60/// the chain and glue. These operands may be implicit on the machine instr.
61static unsigned countOperands(SDNode *Node, unsigned NumExpUses,
62 unsigned &NumImpUses) {
63 unsigned N = Node->getNumOperands();
64 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
65 --N;
66 if (N && Node->getOperand(N - 1).getOpcode() == ISD::DEACTIVATION_SYMBOL)
67 --N; // Ignore deactivation symbol if it exists.
68 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
69 --N; // Ignore chain if it exists.
70
71 // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses.
72 NumImpUses = N - NumExpUses;
73 for (unsigned I = N; I > NumExpUses; --I) {
74 if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1)))
75 continue;
76 if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1)))
77 if (RN->getReg().isPhysical())
78 continue;
79 NumImpUses = N - I;
80 break;
81 }
82
83 return N;
84}
85
86/// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
87/// implicit physical register output.
88void InstrEmitter::EmitCopyFromReg(SDValue Op, bool IsClone, Register SrcReg,
89 VRBaseMapType &VRBaseMap) {
90 Register VRBase;
91 if (SrcReg.isVirtual()) {
92 // Just use the input register directly!
93 if (IsClone)
94 VRBaseMap.erase(Op);
95 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
96 (void)isNew; // Silence compiler warning.
97 assert(isNew && "Node emitted out of order - early");
98 return;
99 }
100
101 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
102 // the CopyToReg'd destination register instead of creating a new vreg.
103 bool MatchReg = true;
104
105 MVT VT = Op.getSimpleValueType();
106
107 // FIXME: The Untyped check is a workaround for SystemZ i128 inline assembly
108 // using i128, when it should probably be using v2i64.
109 const TargetRegisterClass *UseRC =
110 VT == MVT::Untyped ? nullptr : TLI->getRegClassFor(VT, Op->isDivergent());
111
112 for (SDNode *User : Op->users()) {
113 bool Match = true;
114 if (User->getOpcode() == ISD::CopyToReg && User->getOperand(2) == Op) {
115 Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
116 if (DestReg.isVirtual()) {
117 VRBase = DestReg;
118 Match = false;
119 } else if (DestReg != SrcReg)
120 Match = false;
121 } else {
122 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
123 if (User->getOperand(i) != Op)
124 continue;
125 if (VT == MVT::Other || VT == MVT::Glue)
126 continue;
127 Match = false;
128 if (User->isMachineOpcode()) {
129 const MCInstrDesc &II = TII->get(User->getMachineOpcode());
130 const TargetRegisterClass *RC = nullptr;
131 if (i + II.getNumDefs() < II.getNumOperands()) {
132 RC = TRI->getAllocatableClass(
133 TII->getRegClass(II, i + II.getNumDefs()));
134 }
135 if (!UseRC)
136 UseRC = RC;
137 else if (RC) {
138 const TargetRegisterClass *ComRC =
139 TRI->getCommonSubClass(UseRC, RC);
140 // If multiple uses expect disjoint register classes, we emit
141 // copies in AddRegisterOperand.
142 if (ComRC)
143 UseRC = ComRC;
144 }
145 }
146 }
147 }
148 MatchReg &= Match;
149 if (VRBase)
150 break;
151 }
152
153 const TargetRegisterClass *SrcRC = nullptr, *DstRC = nullptr;
154 SrcRC = TRI->getMinimalPhysRegClass(SrcReg);
155
156 // Figure out the register class to create for the destreg.
157 if (VRBase) {
158 DstRC = MRI->getRegClass(VRBase);
159 } else if (UseRC) {
160 assert(TRI->isTypeLegalForClass(*UseRC, VT) &&
161 "Incompatible phys register def and uses!");
162 DstRC = UseRC;
163 } else
164 DstRC = SrcRC;
165
166 // If all uses are reading from the src physical register and copying the
167 // register is either impossible or very expensive, then don't create a copy.
168 if (MatchReg && SrcRC->expensiveOrImpossibleToCopy()) {
169 VRBase = SrcReg;
170 } else {
171 // Create the reg, emit the copy.
172 VRBase = MRI->createVirtualRegister(DstRC);
173 BuildMI(*MBB, InsertPos, Op.getDebugLoc(), TII->get(TargetOpcode::COPY),
174 VRBase)
175 .addReg(SrcReg);
176 }
177
178 if (IsClone)
179 VRBaseMap.erase(Op);
180 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
181 (void)isNew; // Silence compiler warning.
182 assert(isNew && "Node emitted out of order - early");
183}
184
185void InstrEmitter::CreateVirtualRegisters(SDNode *Node,
187 const MCInstrDesc &II,
188 bool IsClone, bool IsCloned,
189 VRBaseMapType &VRBaseMap) {
190 assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF &&
191 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
192
193 unsigned NumResults = CountResults(Node);
194 bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() &&
195 II.isVariadic() && II.variadicOpsAreDefs();
196 unsigned NumVRegs = HasVRegVariadicDefs ? NumResults : II.getNumDefs();
197 if (Node->getMachineOpcode() == TargetOpcode::STATEPOINT)
198 NumVRegs = NumResults;
199 for (unsigned i = 0; i < NumVRegs; ++i) {
200 // If the specific node value is only used by a CopyToReg and the dest reg
201 // is a vreg in the same register class, use the CopyToReg'd destination
202 // register instead of creating a new vreg.
203 Register VRBase;
204 const TargetRegisterClass *RC =
205 TRI->getAllocatableClass(TII->getRegClass(II, i));
206 // Always let the value type influence the used register class. The
207 // constraints on the instruction may be too lax to represent the value
208 // type correctly. For example, a 64-bit float (X86::FR64) can't live in
209 // the 32-bit float super-class (X86::FR32).
210 if (i < NumResults && TLI->isTypeLegal(Node->getSimpleValueType(i))) {
211 const TargetRegisterClass *VTRC = TLI->getRegClassFor(
212 Node->getSimpleValueType(i),
213 (Node->isDivergent() || (RC && TRI->isDivergentRegClass(RC))));
214 if (RC)
215 VTRC = TRI->getCommonSubClass(RC, VTRC);
216 if (VTRC)
217 RC = VTRC;
218 }
219
220 if (!II.operands().empty() && II.operands()[i].isOptionalDef()) {
221 // Optional def must be a physical register.
222 VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg();
223 assert(VRBase.isPhysical());
224 MIB.addReg(VRBase, RegState::Define);
225 }
226
227 if (!VRBase && !IsClone && !IsCloned)
228 for (SDNode *User : Node->users()) {
229 if (User->getOpcode() == ISD::CopyToReg &&
230 User->getOperand(2).getNode() == Node &&
231 User->getOperand(2).getResNo() == i) {
232 Register Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
233 if (Reg.isVirtual()) {
234 const TargetRegisterClass *RegRC = MRI->getRegClass(Reg);
235 if (RegRC == RC) {
236 VRBase = Reg;
237 MIB.addReg(VRBase, RegState::Define);
238 break;
239 }
240 }
241 }
242 }
243
244 // Create the result registers for this node and add the result regs to
245 // the machine instruction.
246 if (!VRBase) {
247 assert(RC && "Isn't a register operand!");
248 VRBase = MRI->createVirtualRegister(RC);
249 MIB.addReg(VRBase, RegState::Define);
250 }
251
252 // If this def corresponds to a result of the SDNode insert the VRBase into
253 // the lookup map.
254 if (i < NumResults) {
255 SDValue Op(Node, i);
256 if (IsClone)
257 VRBaseMap.erase(Op);
258 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
259 (void)isNew; // Silence compiler warning.
260 assert(isNew && "Node emitted out of order - early");
261 }
262 }
263}
264
265/// getVR - Return the virtual register corresponding to the specified result
266/// of the specified node.
267Register InstrEmitter::getVR(SDValue Op, VRBaseMapType &VRBaseMap) {
268 if (Op.isMachineOpcode() &&
269 Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
270 // Add an IMPLICIT_DEF instruction before every use.
271 // IMPLICIT_DEF can produce any type of result so its MCInstrDesc
272 // does not include operand register class info.
273 const TargetRegisterClass *RC = TLI->getRegClassFor(
274 Op.getSimpleValueType(), Op.getNode()->isDivergent());
275 Register VReg = MRI->createVirtualRegister(RC);
276 BuildMI(*MBB, InsertPos, Op.getDebugLoc(),
277 TII->get(TargetOpcode::IMPLICIT_DEF), VReg);
278 return VReg;
279 }
280
281 VRBaseMapType::iterator I = VRBaseMap.find(Op);
282 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
283 return I->second;
284}
285
287 if (Op->isMachineOpcode()) {
288 switch (Op->getMachineOpcode()) {
289 case TargetOpcode::CONVERGENCECTRL_ANCHOR:
290 case TargetOpcode::CONVERGENCECTRL_ENTRY:
291 case TargetOpcode::CONVERGENCECTRL_LOOP:
292 case TargetOpcode::CONVERGENCECTRL_GLUE:
293 return true;
294 }
295 return false;
296 }
297
298 // We can reach here when CopyFromReg is encountered. But rather than making a
299 // special case for that, we just make sure we don't reach here in some
300 // surprising way.
301 switch (Op->getOpcode()) {
306 llvm_unreachable("Convergence control should have been selected by now.");
307 }
308 return false;
309}
310
311/// AddRegisterOperand - Add the specified register as an operand to the
312/// specified machine instr. Insert register copies if the register is
313/// not in the required register class.
314void
315InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB,
316 SDValue Op,
317 unsigned IIOpNum,
318 const MCInstrDesc *II,
319 VRBaseMapType &VRBaseMap,
320 bool IsDebug, bool IsClone, bool IsCloned) {
321 assert(Op.getValueType() != MVT::Other &&
322 Op.getValueType() != MVT::Glue &&
323 "Chain and glue operands should occur at end of operand list!");
324 // Get/emit the operand.
325 Register VReg = getVR(Op, VRBaseMap);
326
327 const MCInstrDesc &MCID = MIB->getDesc();
328 bool isOptDef = IIOpNum < MCID.getNumOperands() &&
329 MCID.operands()[IIOpNum].isOptionalDef();
330
331 // If the instruction requires a register in a different class, create
332 // a new virtual register and copy the value into it, but first attempt to
333 // shrink VReg's register class within reason. For example, if VReg == GR32
334 // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP.
335 if (II) {
336 const TargetRegisterClass *OpRC = nullptr;
337 if (IIOpNum < II->getNumOperands())
338 OpRC = TII->getRegClass(*II, IIOpNum);
339
340 if (OpRC) {
341 unsigned MinNumRegs = MinRCSize;
342 // Don't apply any RC size limit for IMPLICIT_DEF. Each use has a unique
343 // virtual register.
344 if (Op.isMachineOpcode() &&
345 Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF)
346 MinNumRegs = 0;
347
348 const TargetRegisterClass *ConstrainedRC
349 = MRI->constrainRegClass(VReg, OpRC, MinNumRegs);
350 if (!ConstrainedRC) {
351 OpRC = TRI->getAllocatableClass(OpRC);
352 assert(OpRC && "Constraints cannot be fulfilled for allocation");
353 Register NewVReg = MRI->createVirtualRegister(OpRC);
354 BuildMI(*MBB, InsertPos, MIB->getDebugLoc(),
355 TII->get(TargetOpcode::COPY), NewVReg)
356 .addReg(VReg);
357 VReg = NewVReg;
358 } else {
359 assert(ConstrainedRC->isAllocatable() &&
360 "Constraining an allocatable VReg produced an unallocatable class?");
361 }
362 }
363 }
364
365 // If this value has only one use, that use is a kill. This is a
366 // conservative approximation. InstrEmitter does trivial coalescing
367 // with CopyFromReg nodes, so don't emit kill flags for them.
368 // Avoid kill flags on Schedule cloned nodes, since there will be
369 // multiple uses.
370 // Tied operands are never killed, so we need to check that. And that
371 // means we need to determine the index of the operand.
372 // Don't kill convergence control tokens. Initially they are only used in glue
373 // nodes, and the InstrEmitter later adds implicit uses on the users of the
374 // glue node. This can sometimes make it seem like there is only one use,
375 // which is the glue node itself.
376 bool isKill = Op.hasOneUse() && !isConvergenceCtrlMachineOp(Op) &&
377 Op.getNode()->getOpcode() != ISD::CopyFromReg && !IsDebug &&
378 !(IsClone || IsCloned);
379 if (isKill) {
380 unsigned Idx = MIB->getNumOperands();
381 while (Idx > 0 &&
382 MIB->getOperand(Idx-1).isReg() &&
383 MIB->getOperand(Idx-1).isImplicit())
384 --Idx;
385 bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1;
386 if (isTied)
387 isKill = false;
388 }
389
390 MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) |
391 getDebugRegState(IsDebug));
392}
393
394/// AddOperand - Add the specified operand to the specified machine instr. II
395/// specifies the instruction information for the node, and IIOpNum is the
396/// operand number (in the II) that we are adding.
397void InstrEmitter::AddOperand(MachineInstrBuilder &MIB, SDValue Op,
398 unsigned IIOpNum, const MCInstrDesc *II,
399 VRBaseMapType &VRBaseMap, bool IsDebug,
400 bool IsClone, bool IsCloned) {
401 if (Op.isMachineOpcode()) {
402 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
403 IsDebug, IsClone, IsCloned);
404 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
405 if (C->getAPIntValue().getSignificantBits() <= 64) {
406 MIB.addImm(C->getSExtValue());
407 } else {
408 MIB.addCImm(
409 ConstantInt::get(MF->getFunction().getContext(), C->getAPIntValue()));
410 }
411 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
412 MIB.addFPImm(F->getConstantFPValue());
413 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
414 Register VReg = R->getReg();
415 MVT OpVT = Op.getSimpleValueType();
416 const TargetRegisterClass *IIRC =
417 II ? TRI->getAllocatableClass(TII->getRegClass(*II, IIOpNum)) : nullptr;
418 const TargetRegisterClass *OpRC =
419 TLI->isTypeLegal(OpVT)
420 ? TLI->getRegClassFor(OpVT,
421 Op.getNode()->isDivergent() ||
422 (IIRC && TRI->isDivergentRegClass(IIRC)))
423 : nullptr;
424
425 if (OpRC && IIRC && OpRC != IIRC && VReg.isVirtual()) {
426 Register NewVReg = MRI->createVirtualRegister(IIRC);
427 BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
428 TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
429 VReg = NewVReg;
430 }
431 // Turn additional physreg operands into implicit uses on non-variadic
432 // instructions. This is used by call and return instructions passing
433 // arguments in registers.
434 bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic());
435 MIB.addReg(VReg, getImplRegState(Imp));
436 } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) {
437 MIB.addRegMask(RM->getRegMask());
438 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
439 MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(),
440 TGA->getTargetFlags());
441 } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
442 MIB.addMBB(BBNode->getBasicBlock());
443 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
444 MIB.addFrameIndex(FI->getIndex());
445 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
446 MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags());
447 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
448 int Offset = CP->getOffset();
449 Align Alignment = CP->getAlign();
450
451 unsigned Idx;
452 MachineConstantPool *MCP = MF->getConstantPool();
453 if (CP->isMachineConstantPoolEntry())
454 Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Alignment);
455 else
456 Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Alignment);
457 MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags());
458 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
459 MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags());
460 } else if (auto *SymNode = dyn_cast<MCSymbolSDNode>(Op)) {
461 MIB.addSym(SymNode->getMCSymbol());
462 } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) {
463 MIB.addBlockAddress(BA->getBlockAddress(),
464 BA->getOffset(),
465 BA->getTargetFlags());
466 } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) {
467 MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags());
468 } else {
469 assert(Op.getValueType() != MVT::Other &&
470 Op.getValueType() != MVT::Glue &&
471 "Chain and glue operands should occur at end of operand list!");
472 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
473 IsDebug, IsClone, IsCloned);
474 }
475}
476
477Register InstrEmitter::ConstrainForSubReg(Register VReg, unsigned SubIdx,
478 MVT VT, bool isDivergent, const DebugLoc &DL) {
479 const TargetRegisterClass *VRC = MRI->getRegClass(VReg);
480 const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx);
481
482 // RC is a sub-class of VRC that supports SubIdx. Try to constrain VReg
483 // within reason.
484 if (RC && RC != VRC)
485 RC = MRI->constrainRegClass(VReg, RC, MinRCSize);
486
487 // VReg has been adjusted. It can be used with SubIdx operands now.
488 if (RC)
489 return VReg;
490
491 // VReg couldn't be reasonably constrained. Emit a COPY to a new virtual
492 // register instead.
493 RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT, isDivergent), SubIdx);
494 assert(RC && "No legal register class for VT supports that SubIdx");
495 Register NewReg = MRI->createVirtualRegister(RC);
496 BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg)
497 .addReg(VReg);
498 return NewReg;
499}
500
501/// EmitSubregNode - Generate machine code for subreg nodes.
502///
503void InstrEmitter::EmitSubregNode(SDNode *Node, VRBaseMapType &VRBaseMap,
504 bool IsClone, bool IsCloned) {
505 Register VRBase;
506 unsigned Opc = Node->getMachineOpcode();
507
508 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
509 // the CopyToReg'd destination register instead of creating a new vreg.
510 for (SDNode *User : Node->users()) {
511 if (User->getOpcode() == ISD::CopyToReg &&
512 User->getOperand(2).getNode() == Node) {
513 Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
514 if (DestReg.isVirtual()) {
515 VRBase = DestReg;
516 break;
517 }
518 }
519 }
520
521 if (Opc == TargetOpcode::EXTRACT_SUBREG) {
522 // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub. There are no
523 // constraints on the %dst register, COPY can target all legal register
524 // classes.
525 unsigned SubIdx = Node->getConstantOperandVal(1);
526 const TargetRegisterClass *TRC =
527 TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
528
530 MachineInstr *DefMI;
531 RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(0));
532 if (R && R->getReg().isPhysical()) {
533 Reg = R->getReg();
534 DefMI = nullptr;
535 } else {
536 Reg = R ? R->getReg() : getVR(Node->getOperand(0), VRBaseMap);
537 DefMI = MRI->getVRegDef(Reg);
538 }
539
540 Register SrcReg, DstReg;
541 unsigned DefSubIdx;
542 if (DefMI &&
543 TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) &&
544 SubIdx == DefSubIdx &&
545 TRC == MRI->getRegClass(SrcReg)) {
546 // Optimize these:
547 // r1025 = s/zext r1024, 4
548 // r1026 = extract_subreg r1025, 4
549 // to a copy
550 // r1026 = copy r1024
551 VRBase = MRI->createVirtualRegister(TRC);
552 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
553 TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg);
554 MRI->clearKillFlags(SrcReg);
555 } else {
556 // Reg may not support a SubIdx sub-register, and we may need to
557 // constrain its register class or issue a COPY to a compatible register
558 // class.
559 if (Reg.isVirtual())
560 Reg = ConstrainForSubReg(Reg, SubIdx,
561 Node->getOperand(0).getSimpleValueType(),
562 Node->isDivergent(), Node->getDebugLoc());
563 // Create the destreg if it is missing.
564 if (!VRBase)
565 VRBase = MRI->createVirtualRegister(TRC);
566
567 // Create the extract_subreg machine instruction.
568 MachineInstrBuilder CopyMI =
569 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
570 TII->get(TargetOpcode::COPY), VRBase);
571 if (Reg.isVirtual())
572 CopyMI.addReg(Reg, {}, SubIdx);
573 else
574 CopyMI.addReg(TRI->getSubReg(Reg, SubIdx));
575 }
576 } else if (Opc == TargetOpcode::INSERT_SUBREG ||
577 Opc == TargetOpcode::SUBREG_TO_REG) {
578 SDValue Reg;
579 SDValue SubReg;
580 unsigned SubIdx;
581 if (Opc == TargetOpcode::INSERT_SUBREG) {
582 Reg = Node->getOperand(0);
583 SubReg = Node->getOperand(1);
584 SubIdx = Node->getOperand(2)->getAsZExtVal();
585 } else {
586 SubReg = Node->getOperand(0);
587 SubIdx = Node->getOperand(1)->getAsZExtVal();
588 }
589
590 // Figure out the register class to create for the destreg. It should be
591 // the largest legal register class supporting SubIdx sub-registers.
592 // RegisterCoalescer will constrain it further if it decides to eliminate
593 // the INSERT_SUBREG instruction.
594 //
595 // %dst = INSERT_SUBREG %src, %sub, SubIdx
596 //
597 // is lowered by TwoAddressInstructionPass to:
598 //
599 // %dst = COPY %src
600 // %dst:SubIdx = COPY %sub
601 //
602 // There is no constraint on the %src register class.
603 //
604 const TargetRegisterClass *SRC =
605 TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
606 SRC = TRI->getSubClassWithSubReg(SRC, SubIdx);
607 assert(SRC && "No register class supports VT and SubIdx for INSERT_SUBREG");
608
609 if (VRBase == 0 || !SRC->hasSubClassEq(MRI->getRegClass(VRBase)))
610 VRBase = MRI->createVirtualRegister(SRC);
611
612 // Create the insert_subreg or subreg_to_reg machine instruction.
613 MachineInstrBuilder MIB =
614 BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc), VRBase);
615
616 // If creating an insert_subreg, then the first input operand
617 // is a register
618 if (Reg) {
619 AddOperand(MIB, Reg, 0, nullptr, VRBaseMap, /*IsDebug=*/false, IsClone,
620 IsCloned);
621 }
622 // Add the subregister being inserted
623 AddOperand(MIB, SubReg, 0, nullptr, VRBaseMap, /*IsDebug=*/false, IsClone,
624 IsCloned);
625 MIB.addImm(SubIdx);
626 MBB->insert(InsertPos, MIB);
627 } else
628 llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg");
629
630 SDValue Op(Node, 0);
631 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
632 (void)isNew; // Silence compiler warning.
633 assert(isNew && "Node emitted out of order - early");
634}
635
636/// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
637/// COPY_TO_REGCLASS is just a normal copy, except that the destination
638/// register is constrained to be in a particular register class.
639///
640void
641InstrEmitter::EmitCopyToRegClassNode(SDNode *Node,
642 VRBaseMapType &VRBaseMap) {
643 // Create the new VReg in the destination class and emit a copy.
644 unsigned DstRCIdx = Node->getConstantOperandVal(1);
645 const TargetRegisterClass *DstRC =
646 TRI->getAllocatableClass(TRI->getRegClass(DstRCIdx));
647 Register NewVReg = MRI->createVirtualRegister(DstRC);
648 const MCInstrDesc &II = TII->get(TargetOpcode::COPY);
649 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg);
650 AddOperand(MIB, Node->getOperand(0), 1, &II, VRBaseMap, /*IsDebug=*/false,
651 /*IsClone=*/false, /*IsCloned*/ false);
652
653 MBB->insert(InsertPos, MIB);
654 SDValue Op(Node, 0);
655 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
656 (void)isNew; // Silence compiler warning.
657 assert(isNew && "Node emitted out of order - early");
658}
659
660/// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
661///
662void InstrEmitter::EmitRegSequence(SDNode *Node, VRBaseMapType &VRBaseMap,
663 bool IsClone, bool IsCloned) {
664 unsigned DstRCIdx = Node->getConstantOperandVal(0);
665 const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
666 Register NewVReg = MRI->createVirtualRegister(TRI->getAllocatableClass(RC));
667 const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE);
668 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg);
669 unsigned NumOps = Node->getNumOperands();
670 // If the input pattern has a chain, then the root of the corresponding
671 // output pattern will get a chain as well. This can happen to be a
672 // REG_SEQUENCE (which is not "guarded" by countOperands/CountResults).
673 if (NumOps && Node->getOperand(NumOps-1).getValueType() == MVT::Other)
674 --NumOps; // Ignore chain if it exists.
675
676 assert((NumOps & 1) == 1 &&
677 "REG_SEQUENCE must have an odd number of operands!");
678 for (unsigned i = 1; i != NumOps; ++i) {
679 SDValue Op = Node->getOperand(i);
680 if ((i & 1) == 0) {
681 RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(i-1));
682 // Skip physical registers as they don't have a vreg to get and we'll
683 // insert copies for them in TwoAddressInstructionPass anyway.
684 if (!R || !R->getReg().isPhysical()) {
685 unsigned SubIdx = Op->getAsZExtVal();
686 Register SubReg = getVR(Node->getOperand(i - 1), VRBaseMap);
687 const TargetRegisterClass *TRC = MRI->getRegClass(SubReg);
688 const TargetRegisterClass *SRC =
689 TRI->getMatchingSuperRegClass(RC, TRC, SubIdx);
690 if (SRC && SRC != RC) {
691 MRI->setRegClass(NewVReg, SRC);
692 RC = SRC;
693 }
694 }
695 }
696 AddOperand(MIB, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false,
697 IsClone, IsCloned);
698 }
699
700 MBB->insert(InsertPos, MIB);
701 SDValue Op(Node, 0);
702 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
703 (void)isNew; // Silence compiler warning.
704 assert(isNew && "Node emitted out of order - early");
705}
706
707/// EmitDbgValue - Generate machine instruction for a dbg_value node.
708///
710 VRBaseMapType &VRBaseMap) {
712 ->isValidLocationForIntrinsic(SD->getDebugLoc()) &&
713 "Expected inlined-at fields to agree");
714
715 SD->setIsEmitted();
716
717 assert(!SD->getLocationOps().empty() &&
718 "dbg_value with no location operands?");
719
720 if (SD->isInvalidated())
721 return EmitDbgNoLocation(SD);
722
723 // Attempt to produce a DBG_INSTR_REF if we've been asked to.
724 if (EmitDebugInstrRefs)
725 if (auto *InstrRef = EmitDbgInstrRef(SD, VRBaseMap))
726 return InstrRef;
727
728 // Emit variadic dbg_value nodes as DBG_VALUE_LIST if they have not been
729 // emitted as instruction references.
730 if (SD->isVariadic())
731 return EmitDbgValueList(SD, VRBaseMap);
732
733 // Emit single-location dbg_value nodes as DBG_VALUE if they have not been
734 // emitted as instruction references.
735 return EmitDbgValueFromSingleOp(SD, VRBaseMap);
736}
737
739 const Value *V = Op.getConst();
740 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
741 if (CI->getBitWidth() > 64)
743 if (CI->getBitWidth() == 1)
744 return MachineOperand::CreateImm(CI->getZExtValue());
745 return MachineOperand::CreateImm(CI->getSExtValue());
746 }
747 if (const ConstantFP *CF = dyn_cast<ConstantFP>(V))
749 // Note: This assumes that all nullptr constants are zero-valued.
752 // Undef or unhandled value type, so return an undef operand.
754 /* Reg */ 0U, /* isDef */ false, /* isImp */ false,
755 /* isKill */ false, /* isDead */ false,
756 /* isUndef */ false, /* isEarlyClobber */ false,
757 /* SubReg */ 0, /* isDebug */ true);
758}
759
761 MachineInstrBuilder &MIB, const MCInstrDesc &DbgValDesc,
762 ArrayRef<SDDbgOperand> LocationOps,
763 VRBaseMapType &VRBaseMap) {
764 for (const SDDbgOperand &Op : LocationOps) {
765 switch (Op.getKind()) {
767 MIB.addFrameIndex(Op.getFrameIx());
768 break;
770 MIB.addReg(Op.getVReg());
771 break;
773 SDValue V = SDValue(Op.getSDNode(), Op.getResNo());
774 // It's possible we replaced this SDNode with other(s) and therefore
775 // didn't generate code for it. It's better to catch these cases where
776 // they happen and transfer the debug info, but trying to guarantee that
777 // in all cases would be very fragile; this is a safeguard for any
778 // that were missed.
779 if (VRBaseMap.count(V) == 0)
780 MIB.addReg(0U); // undef
781 else
782 AddOperand(MIB, V, (*MIB).getNumOperands(), &DbgValDesc, VRBaseMap,
783 /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false);
784 } break;
787 break;
789 MIB.addGlobalAddress(Op.getGlobal());
790 break;
791 }
792 }
793}
794
797 VRBaseMapType &VRBaseMap) {
798 MDNode *Var = SD->getVariable();
799 const DIExpression *Expr = SD->getExpression();
800 DebugLoc DL = SD->getDebugLoc();
801 const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_INSTR_REF);
802
803 // Returns true if the given operand is not a legal debug operand for a
804 // DBG_INSTR_REF.
805 auto IsInvalidOp = [](SDDbgOperand DbgOp) {
806 return DbgOp.getKind() == SDDbgOperand::FRAMEIX;
807 };
808 // Returns true if the given operand is not itself an instruction reference
809 // but is a legal debug operand for a DBG_INSTR_REF.
810 auto IsNonInstrRefOp = [](SDDbgOperand DbgOp) {
811 return DbgOp.getKind() == SDDbgOperand::CONST ||
812 DbgOp.getKind() == SDDbgOperand::GLOBALADDR;
813 };
814
815 // If this variable location does not depend on any instructions or contains
816 // any stack locations, produce it as a standard debug value instead.
817 if (any_of(SD->getLocationOps(), IsInvalidOp) ||
818 all_of(SD->getLocationOps(), IsNonInstrRefOp)) {
819 if (SD->isVariadic())
820 return EmitDbgValueList(SD, VRBaseMap);
821 return EmitDbgValueFromSingleOp(SD, VRBaseMap);
822 }
823
824 // Immediately fold any indirectness from the LLVM-IR intrinsic into the
825 // expression:
826 if (SD->isIndirect())
827 Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);
828 // If this is not already a variadic expression, it must be modified to become
829 // one.
830 if (!SD->isVariadic())
832
834
835 // It may not be immediately possible to identify the MachineInstr that
836 // defines a VReg, it can depend for example on the order blocks are
837 // emitted in. When this happens, or when further analysis is needed later,
838 // produce an instruction like this:
839 //
840 // DBG_INSTR_REF !123, !456, %0:gr64
841 //
842 // i.e., point the instruction at the vreg, and patch it up later in
843 // MachineFunction::finalizeDebugInstrRefs.
844 auto AddVRegOp = [&](Register VReg) {
846 /* Reg */ VReg, /* isDef */ false, /* isImp */ false,
847 /* isKill */ false, /* isDead */ false,
848 /* isUndef */ false, /* isEarlyClobber */ false,
849 /* SubReg */ 0, /* isDebug */ true));
850 };
851 unsigned OpCount = SD->getLocationOps().size();
852 for (unsigned OpIdx = 0; OpIdx < OpCount; ++OpIdx) {
853 SDDbgOperand DbgOperand = SD->getLocationOps()[OpIdx];
854
855 // Try to find both the defined register and the instruction defining it.
856 MachineInstr *DefMI = nullptr;
857 Register VReg;
858
859 if (DbgOperand.getKind() == SDDbgOperand::VREG) {
860 VReg = DbgOperand.getVReg();
861
862 // No definition means that block hasn't been emitted yet. Leave a vreg
863 // reference to be fixed later.
864 if (!MRI->hasOneDef(VReg)) {
865 AddVRegOp(VReg);
866 continue;
867 }
868
869 DefMI = &*MRI->def_instr_begin(VReg);
870 } else if (DbgOperand.getKind() == SDDbgOperand::SDNODE) {
871 // Look up the corresponding VReg for the given SDNode, if any.
872 SDNode *Node = DbgOperand.getSDNode();
873 SDValue Op = SDValue(Node, DbgOperand.getResNo());
874 VRBaseMapType::iterator I = VRBaseMap.find(Op);
875 // No VReg -> produce a DBG_VALUE $noreg instead.
876 if (I == VRBaseMap.end())
877 break;
878
879 // Try to pick out a defining instruction at this point.
880 VReg = getVR(Op, VRBaseMap);
881
882 // Again, if there's no instruction defining the VReg right now, fix it up
883 // later.
884 if (!MRI->hasOneDef(VReg)) {
885 AddVRegOp(VReg);
886 continue;
887 }
888
889 DefMI = &*MRI->def_instr_begin(VReg);
890 } else if (DbgOperand.getKind() == SDDbgOperand::GLOBALADDR) {
892 /*Offset=*/0));
893 continue;
894 } else {
895 assert(DbgOperand.getKind() == SDDbgOperand::CONST);
896 MOs.push_back(GetMOForConstDbgOp(DbgOperand));
897 continue;
898 }
899
900 // Avoid copy like instructions: they don't define values, only move them.
901 // Leave a virtual-register reference until it can be fixed up later, to
902 // find the underlying value definition.
903 if (DefMI->isCopyLike() || TII->isCopyInstr(*DefMI)) {
904 AddVRegOp(VReg);
905 continue;
906 }
907
908 // Find the operand number which defines the specified VReg.
909 unsigned OperandIdx = 0;
910 for (const auto &MO : DefMI->operands()) {
911 if (MO.isReg() && MO.isDef() && MO.getReg() == VReg)
912 break;
913 ++OperandIdx;
914 }
915 assert(OperandIdx < DefMI->getNumOperands());
916
917 // Make the DBG_INSTR_REF refer to that instruction, and that operand.
918 unsigned InstrNum = DefMI->getDebugInstrNum();
919 MOs.push_back(MachineOperand::CreateDbgInstrRef(InstrNum, OperandIdx));
920 }
921
922 // If we haven't created a valid MachineOperand for every DbgOp, abort and
923 // produce an undef DBG_VALUE.
924 if (MOs.size() != OpCount)
925 return EmitDbgNoLocation(SD);
926
927 return BuildMI(*MF, DL, RefII, false, MOs, Var, Expr);
928}
929
931 // An invalidated SDNode must generate an undef DBG_VALUE: although the
932 // original value is no longer computed, earlier DBG_VALUEs live ranges
933 // must not leak into later code.
934 DIVariable *Var = SD->getVariable();
935 const DIExpression *Expr =
937 DebugLoc DL = SD->getDebugLoc();
938 const MCInstrDesc &Desc = TII->get(TargetOpcode::DBG_VALUE);
939 return BuildMI(*MF, DL, Desc, false, 0U, Var, Expr);
940}
941
944 VRBaseMapType &VRBaseMap) {
945 MDNode *Var = SD->getVariable();
946 DIExpression *Expr = SD->getExpression();
947 DebugLoc DL = SD->getDebugLoc();
948 // DBG_VALUE_LIST := "DBG_VALUE_LIST" var, expression, loc (, loc)*
949 const MCInstrDesc &DbgValDesc = TII->get(TargetOpcode::DBG_VALUE_LIST);
950 // Build the DBG_VALUE_LIST instruction base.
951 auto MIB = BuildMI(*MF, DL, DbgValDesc);
952 MIB.addMetadata(Var);
953 MIB.addMetadata(Expr);
954 AddDbgValueLocationOps(MIB, DbgValDesc, SD->getLocationOps(), VRBaseMap);
955 return &*MIB;
956}
957
960 VRBaseMapType &VRBaseMap) {
961 MDNode *Var = SD->getVariable();
962 DIExpression *Expr = SD->getExpression();
963 DebugLoc DL = SD->getDebugLoc();
964 const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE);
965
966 assert(SD->getLocationOps().size() == 1 &&
967 "Non variadic dbg_value should have only one location op");
968
969 // See about constant-folding the expression.
970 // Copy the location operand in case we replace it.
971 SmallVector<SDDbgOperand, 1> LocationOps(1, SD->getLocationOps()[0]);
972 if (Expr && LocationOps[0].getKind() == SDDbgOperand::CONST) {
973 const Value *V = LocationOps[0].getConst();
974 if (auto *C = dyn_cast<ConstantInt>(V)) {
975 std::tie(Expr, C) = Expr->constantFold(C);
976 LocationOps[0] = SDDbgOperand::fromConst(C);
977 }
978 }
979
980 // Emit non-variadic dbg_value nodes as DBG_VALUE.
981 // DBG_VALUE := "DBG_VALUE" loc, isIndirect, var, expr
982 auto MIB = BuildMI(*MF, DL, II);
983 AddDbgValueLocationOps(MIB, II, LocationOps, VRBaseMap);
984
985 if (SD->isIndirect())
986 MIB.addImm(0U);
987 else
988 MIB.addReg(0U);
989
990 return MIB.addMetadata(Var).addMetadata(Expr);
991}
992
995 MDNode *Label = SD->getLabel();
996 DebugLoc DL = SD->getDebugLoc();
997 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
998 "Expected inlined-at fields to agree");
999
1000 const MCInstrDesc &II = TII->get(TargetOpcode::DBG_LABEL);
1001 MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
1002 MIB.addMetadata(Label);
1003
1004 return &*MIB;
1005}
1006
1007/// EmitMachineNode - Generate machine code for a target-specific node and
1008/// needed dependencies.
1009///
1010void InstrEmitter::
1011EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
1012 VRBaseMapType &VRBaseMap) {
1013 unsigned Opc = Node->getMachineOpcode();
1014
1015 // Handle subreg insert/extract specially
1016 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
1017 Opc == TargetOpcode::INSERT_SUBREG ||
1018 Opc == TargetOpcode::SUBREG_TO_REG) {
1019 EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned);
1020 return;
1021 }
1022
1023 // Handle COPY_TO_REGCLASS specially.
1024 if (Opc == TargetOpcode::COPY_TO_REGCLASS) {
1025 EmitCopyToRegClassNode(Node, VRBaseMap);
1026 return;
1027 }
1028
1029 // Handle REG_SEQUENCE specially.
1030 if (Opc == TargetOpcode::REG_SEQUENCE) {
1031 EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned);
1032 return;
1033 }
1034
1035 if (Opc == TargetOpcode::IMPLICIT_DEF)
1036 // We want a unique VR for each IMPLICIT_DEF use.
1037 return;
1038
1039 const MCInstrDesc &II = TII->get(Opc);
1040 unsigned NumResults = CountResults(Node);
1041 unsigned NumDefs = II.getNumDefs();
1042 const MCPhysReg *ScratchRegs = nullptr;
1043
1044 // Handle STACKMAP and PATCHPOINT specially and then use the generic code.
1045 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
1046 // Stackmaps do not have arguments and do not preserve their calling
1047 // convention. However, to simplify runtime support, they clobber the same
1048 // scratch registers as AnyRegCC.
1049 unsigned CC = CallingConv::AnyReg;
1050 if (Opc == TargetOpcode::PATCHPOINT) {
1051 CC = Node->getConstantOperandVal(PatchPointOpers::CCPos);
1052 NumDefs = NumResults;
1053 }
1054 ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC);
1055 } else if (Opc == TargetOpcode::STATEPOINT) {
1056 NumDefs = NumResults;
1057 }
1058
1059 unsigned NumImpUses = 0;
1060 unsigned NodeOperands =
1061 countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses);
1062 bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() &&
1063 II.isVariadic() && II.variadicOpsAreDefs();
1064 bool HasPhysRegOuts = NumResults > NumDefs && !II.implicit_defs().empty() &&
1065 !HasVRegVariadicDefs;
1066#ifndef NDEBUG
1067 unsigned NumMIOperands = NodeOperands + NumResults;
1068 if (II.isVariadic())
1069 assert(NumMIOperands >= II.getNumOperands() &&
1070 "Too few operands for a variadic node!");
1071 else
1072 assert(NumMIOperands >= II.getNumOperands() &&
1073 NumMIOperands <=
1074 II.getNumOperands() + II.implicit_defs().size() + NumImpUses &&
1075 "#operands for dag node doesn't match .td file!");
1076#endif
1077
1078 // Create the new machine instruction.
1079 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II);
1080
1081 // Transfer IR flags from the SDNode to the MachineInstr
1082 MachineInstr *MI = MIB.getInstr();
1083 const SDNodeFlags Flags = Node->getFlags();
1084 if (Flags.hasUnpredictable())
1086
1087 // Add result register values for things that are defined by this
1088 // instruction.
1089 if (NumResults) {
1090 CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap);
1091
1092 if (Flags.hasNoSignedZeros())
1094
1095 if (Flags.hasAllowReciprocal())
1097
1098 if (Flags.hasNoNaNs())
1100
1101 if (Flags.hasNoInfs())
1103
1104 if (Flags.hasAllowContract())
1106
1107 if (Flags.hasApproximateFuncs())
1109
1110 if (Flags.hasAllowReassociation())
1112
1113 if (Flags.hasNoUnsignedWrap())
1115
1116 if (Flags.hasNoSignedWrap())
1118
1119 if (Flags.hasExact())
1121
1122 if (Flags.hasNoFPExcept())
1124
1125 if (Flags.hasDisjoint())
1127
1128 if (Flags.hasSameSign())
1130
1131 if (Flags.hasNoConvergent())
1133 }
1134
1135 // Emit all of the actual operands of this instruction, adding them to the
1136 // instruction as appropriate.
1137 bool HasOptPRefs = NumDefs > NumResults;
1138 assert((!HasOptPRefs || !HasPhysRegOuts) &&
1139 "Unable to cope with optional defs and phys regs defs!");
1140 unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0;
1141 for (unsigned i = NumSkip; i != NodeOperands; ++i)
1142 AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II,
1143 VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned);
1144
1145 // Add scratch registers as implicit def and early clobber
1146 if (ScratchRegs)
1147 for (unsigned i = 0; ScratchRegs[i]; ++i)
1148 MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine |
1150
1151 // Set the memory reference descriptions of this instruction now that it is
1152 // part of the function.
1153 MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands());
1154
1155 // Set the CFI type.
1156 MIB->setCFIType(*MF, Node->getCFIType());
1157
1158 // Insert the instruction into position in the block. This needs to
1159 // happen before any custom inserter hook is called so that the
1160 // hook knows where in the block to insert the replacement code.
1161 MBB->insert(InsertPos, MIB);
1162
1163 // The MachineInstr may also define physregs instead of virtregs. These
1164 // physreg values can reach other instructions in different ways:
1165 //
1166 // 1. When there is a use of a Node value beyond the explicitly defined
1167 // virtual registers, we emit a CopyFromReg for one of the implicitly
1168 // defined physregs. This only happens when HasPhysRegOuts is true.
1169 //
1170 // 2. A CopyFromReg reading a physreg may be glued to this instruction.
1171 //
1172 // 3. A glued instruction may implicitly use a physreg.
1173 //
1174 // 4. A glued instruction may use a RegisterSDNode operand.
1175 //
1176 // Collect all the used physreg defs, and make sure that any unused physreg
1177 // defs are marked as dead.
1178 SmallVector<Register, 8> UsedRegs;
1179
1180 // Additional results must be physical register defs.
1181 if (HasPhysRegOuts) {
1182 for (unsigned i = NumDefs; i < NumResults; ++i) {
1183 Register Reg = II.implicit_defs()[i - NumDefs];
1184 if (!Node->hasAnyUseOfValue(i))
1185 continue;
1186 // This implicitly defined physreg has a use.
1187 UsedRegs.push_back(Reg);
1188 EmitCopyFromReg(SDValue(Node, i), IsClone, Reg, VRBaseMap);
1189 }
1190 }
1191
1192 // Scan the glue chain for any used physregs.
1193 if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) {
1194 for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) {
1195 if (F->getOpcode() == ISD::CopyFromReg) {
1196 Register Reg = cast<RegisterSDNode>(F->getOperand(1))->getReg();
1197 if (Reg.isPhysical())
1198 UsedRegs.push_back(Reg);
1199 continue;
1200 } else if (F->getOpcode() == ISD::CopyToReg) {
1201 // Skip CopyToReg nodes that are internal to the glue chain.
1202 continue;
1203 }
1204 // Collect declared implicit uses.
1205 const MCInstrDesc &MCID = TII->get(F->getMachineOpcode());
1206 append_range(UsedRegs, MCID.implicit_uses());
1207 // In addition to declared implicit uses, we must also check for
1208 // direct RegisterSDNode operands.
1209 for (const SDValue &Op : F->op_values())
1210 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
1211 Register Reg = R->getReg();
1212 if (Reg.isPhysical())
1213 UsedRegs.push_back(Reg);
1214 }
1215 }
1216 }
1217
1218 // Add rounding control registers as implicit def for function call.
1219 if (II.isCall() && MF->getFunction().hasFnAttribute(Attribute::StrictFP)) {
1220 ArrayRef<MCPhysReg> RCRegs = TLI->getRoundingControlRegisters();
1221 llvm::append_range(UsedRegs, RCRegs);
1222 }
1223
1224 // Finally mark unused registers as dead.
1225 if (!UsedRegs.empty() || !II.implicit_defs().empty() || II.hasOptionalDef())
1226 MIB->setPhysRegsDeadExcept(UsedRegs, *TRI);
1227
1228 // STATEPOINT is too 'dynamic' to have meaningful machine description.
1229 // We have to manually tie operands.
1230 if (Opc == TargetOpcode::STATEPOINT && NumDefs > 0) {
1231 assert(!HasPhysRegOuts && "STATEPOINT mishandled");
1232 MachineInstr *MI = MIB;
1233 unsigned Def = 0;
1234 int First = StatepointOpers(MI).getFirstGCPtrIdx();
1235 assert(First > 0 && "Statepoint has Defs but no GC ptr list");
1236 unsigned Use = (unsigned)First;
1237 while (Def < NumDefs) {
1238 if (MI->getOperand(Use).isReg())
1239 MI->tieOperands(Def++, Use);
1241 }
1242 }
1243
1244 unsigned Op = Node->getNumOperands();
1245 if (Op != 0 && Node->getOperand(Op - 1)->getOpcode() ==
1246 ~(unsigned)TargetOpcode::CONVERGENCECTRL_GLUE) {
1247 Register VReg = getVR(Node->getOperand(Op - 1)->getOperand(0), VRBaseMap);
1248 MachineOperand MO = MachineOperand::CreateReg(VReg, /*isDef=*/false,
1249 /*isImp=*/true);
1250 MIB->addOperand(MO);
1251 Op--;
1252 }
1253
1254 if (Op != 0 &&
1255 Node->getOperand(Op - 1)->getOpcode() == ISD::DEACTIVATION_SYMBOL) {
1256 MI->setDeactivationSymbol(
1257 *MF, const_cast<GlobalValue *>(
1258 cast<DeactivationSymbolSDNode>(Node->getOperand(Op - 1))
1259 ->getGlobal()));
1260 Op--;
1261 }
1262
1263 // Run post-isel target hook to adjust this instruction if needed.
1264 if (II.hasPostISelHook())
1265 TLI->AdjustInstrPostInstrSelection(*MIB, Node);
1266}
1267
1268/// EmitSpecialNode - Generate machine code for a target-independent node and
1269/// needed dependencies.
1270void InstrEmitter::
1271EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
1272 VRBaseMapType &VRBaseMap) {
1273 switch (Node->getOpcode()) {
1274 default:
1275#ifndef NDEBUG
1276 Node->dump();
1277#endif
1278 llvm_unreachable("This target-independent node should have been selected!");
1279 case ISD::EntryToken:
1280 case ISD::MERGE_VALUES:
1281 case ISD::TokenFactor:
1283 break;
1284 case ISD::CopyToReg: {
1285 Register DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1286 SDValue SrcVal = Node->getOperand(2);
1287 if (DestReg.isVirtual() && SrcVal.isMachineOpcode() &&
1288 SrcVal.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
1289 // Instead building a COPY to that vreg destination, build an
1290 // IMPLICIT_DEF instruction instead.
1291 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
1292 TII->get(TargetOpcode::IMPLICIT_DEF), DestReg);
1293 break;
1294 }
1295 Register SrcReg;
1296 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
1297 SrcReg = R->getReg();
1298 else
1299 SrcReg = getVR(SrcVal, VRBaseMap);
1300
1301 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
1302 break;
1303
1304 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
1305 DestReg).addReg(SrcReg);
1306 break;
1307 }
1308 case ISD::CopyFromReg: {
1309 Register SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1310 EmitCopyFromReg(SDValue(Node, 0), IsClone, SrcReg, VRBaseMap);
1311 break;
1312 }
1313 case ISD::EH_LABEL:
1314 case ISD::ANNOTATION_LABEL: {
1315 unsigned Opc = (Node->getOpcode() == ISD::EH_LABEL)
1316 ? TargetOpcode::EH_LABEL
1317 : TargetOpcode::ANNOTATION_LABEL;
1318 MCSymbol *S = cast<LabelSDNode>(Node)->getLabel();
1319 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
1320 TII->get(Opc)).addSym(S);
1321 break;
1322 }
1323
1325 case ISD::LIFETIME_END: {
1326 unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START)
1327 ? TargetOpcode::LIFETIME_START
1328 : TargetOpcode::LIFETIME_END;
1329 auto *FI = cast<FrameIndexSDNode>(Node->getOperand(1));
1330 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
1331 .addFrameIndex(FI->getIndex());
1332 break;
1333 }
1334
1335 case ISD::PSEUDO_PROBE: {
1336 unsigned TarOp = TargetOpcode::PSEUDO_PROBE;
1337 auto Guid = cast<PseudoProbeSDNode>(Node)->getGuid();
1338 auto Index = cast<PseudoProbeSDNode>(Node)->getIndex();
1339 auto Attr = cast<PseudoProbeSDNode>(Node)->getAttributes();
1340
1341 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
1342 .addImm(Guid)
1343 .addImm(Index)
1345 .addImm(Attr);
1346 break;
1347 }
1348
1349 case ISD::INLINEASM:
1350 case ISD::INLINEASM_BR: {
1351 unsigned NumOps = Node->getNumOperands();
1352 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
1353 --NumOps; // Ignore the glue operand.
1354
1355 // Create the inline asm machine instruction.
1356 unsigned TgtOpc = Node->getOpcode() == ISD::INLINEASM_BR
1357 ? TargetOpcode::INLINEASM_BR
1358 : TargetOpcode::INLINEASM;
1359 MachineInstrBuilder MIB =
1360 BuildMI(*MF, Node->getDebugLoc(), TII->get(TgtOpc));
1361
1362 // Add the asm string as an external symbol operand.
1363 SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString);
1364 const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol();
1365 MIB.addExternalSymbol(AsmStr);
1366
1367 // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore
1368 // bits.
1369 int64_t ExtraInfo =
1371 getZExtValue();
1372 MIB.addImm(ExtraInfo);
1373
1374 // Remember to operand index of the group flags.
1375 SmallVector<unsigned, 8> GroupIdx;
1376
1377 // Remember registers that are part of early-clobber defs.
1379
1380 // A glued CopyFromReg may read a clobbered register, e.g. a flag output
1381 // like X86 "={@ccz}" reads EFLAGS defined only by "~{flags}".
1382 SmallVector<Register, 2> GluedUses;
1383 if (Node->getValueType(Node->getNumValues() - 1) == MVT::Glue) {
1384 for (SDNode *G = Node->getGluedUser(); G; G = G->getGluedUser()) {
1385 if (G->getOpcode() != ISD::CopyFromReg)
1386 continue;
1387 Register Reg = cast<RegisterSDNode>(G->getOperand(1))->getReg();
1388 if (Reg.isPhysical())
1389 GluedUses.push_back(Reg);
1390 }
1391 }
1392
1393 // Add all of the operand registers to the instruction.
1394 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
1395 unsigned Flags = Node->getConstantOperandVal(i);
1396 const InlineAsm::Flag F(Flags);
1397 const unsigned NumVals = F.getNumOperandRegisters();
1398
1399 GroupIdx.push_back(MIB->getNumOperands());
1400 MIB.addImm(Flags);
1401 ++i; // Skip the ID value.
1402
1403 switch (F.getKind()) {
1405 for (unsigned j = 0; j != NumVals; ++j, ++i) {
1406 Register Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1407 // FIXME: Add dead flags for physical and virtual registers defined.
1408 // For now, mark physical register defs as implicit to help fast
1409 // regalloc. This makes inline asm look a lot like calls.
1411 }
1412 break;
1415 for (unsigned j = 0; j != NumVals; ++j, ++i) {
1416 Register Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1417 bool IsDead =
1418 F.isClobberKind() && none_of(GluedUses, [&](Register U) {
1419 return TRI->regsOverlap(U, Reg);
1420 });
1424 ECRegs.push_back(Reg);
1425 }
1426 break;
1427 case InlineAsm::Kind::RegUse: // Use of register.
1428 case InlineAsm::Kind::Imm: // Immediate.
1429 case InlineAsm::Kind::Mem: // Non-function addressing mode.
1430 // The addressing mode has been selected, just add all of the
1431 // operands to the machine instruction.
1432 for (unsigned j = 0; j != NumVals; ++j, ++i)
1433 AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap,
1434 /*IsDebug=*/false, IsClone, IsCloned);
1435
1436 // Manually set isTied bits.
1437 if (F.isRegUseKind()) {
1438 unsigned DefGroup;
1439 if (F.isUseOperandTiedToDef(DefGroup)) {
1440 unsigned DefIdx = GroupIdx[DefGroup] + 1;
1441 unsigned UseIdx = GroupIdx.back() + 1;
1442 for (unsigned j = 0; j != NumVals; ++j)
1443 MIB->tieOperands(DefIdx + j, UseIdx + j);
1444 }
1445 }
1446 break;
1447 case InlineAsm::Kind::Func: // Function addressing mode.
1448 for (unsigned j = 0; j != NumVals; ++j, ++i) {
1449 SDValue Op = Node->getOperand(i);
1450 AddOperand(MIB, Op, 0, nullptr, VRBaseMap,
1451 /*IsDebug=*/false, IsClone, IsCloned);
1452
1453 // Adjust Target Flags for function reference.
1454 if (auto *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
1455 unsigned NewFlags =
1456 MF->getSubtarget().classifyGlobalFunctionReference(
1457 TGA->getGlobal());
1458 unsigned LastIdx = MIB.getInstr()->getNumOperands() - 1;
1459 MIB.getInstr()->getOperand(LastIdx).setTargetFlags(NewFlags);
1460 }
1461 }
1462 }
1463 }
1464
1465 // GCC inline assembly allows input operands to also be early-clobber
1466 // output operands (so long as the operand is written only after it's
1467 // used), but this does not match the semantics of our early-clobber flag.
1468 // If an early-clobber operand register is also an input operand register,
1469 // then remove the early-clobber flag.
1470 for (Register Reg : ECRegs) {
1471 if (MIB->readsRegister(Reg, TRI)) {
1472 MachineOperand *MO =
1473 MIB->findRegisterDefOperand(Reg, TRI, false, false);
1474 assert(MO && "No def operand for clobbered register?");
1475 MO->setIsEarlyClobber(false);
1476 }
1477 }
1478
1479 // Get the mdnode from the asm if it exists and add it to the instruction.
1480 SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode);
1481 const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD();
1482 if (MD)
1483 MIB.addMetadata(MD);
1484
1485 // Add rounding control registers as implicit def for inline asm.
1486 if (MF->getFunction().hasFnAttribute(Attribute::StrictFP)) {
1487 ArrayRef<MCPhysReg> RCRegs = TLI->getRoundingControlRegisters();
1488 for (MCPhysReg Reg : RCRegs)
1490 }
1491
1492 MBB->insert(InsertPos, MIB);
1493 break;
1494 }
1495 }
1496}
1497
1498/// InstrEmitter - Construct an InstrEmitter and set it to start inserting
1499/// at the given position in the given block.
1502 : MF(mbb->getParent()), MRI(&MF->getRegInfo()),
1503 TII(MF->getSubtarget().getInstrInfo()),
1504 TRI(MF->getSubtarget().getRegisterInfo()),
1505 TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb),
1506 InsertPos(insertpos) {
1507 EmitDebugInstrRefs = mbb->getParent()->useDebugInstrRef();
1508}
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains constants used for implementing Dwarf debug support.
IRTranslator LLVM IR MI
static bool isConvergenceCtrlMachineOp(SDValue Op)
MachineOperand GetMOForConstDbgOp(const SDDbgOperand &Op)
const unsigned MinRCSize
MinRCSize - Smallest register class we allow when constraining virtual registers.
static unsigned countOperands(SDNode *Node, unsigned NumExpUses, unsigned &NumImpUses)
countOperands - The inputs to target nodes have any actual inputs first, followed by an optional chai...
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
uint64_t IntrinsicInst * II
bool IsDead
This file describes how to lower LLVM code to machine code.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
DWARF expression.
static LLVM_ABI DIExpression * append(const DIExpression *Expr, ArrayRef< uint64_t > Ops)
Append the opcodes Ops to DIExpr.
LLVM_ABI std::pair< DIExpression *, const ConstantInt * > constantFold(const ConstantInt *CI)
Try to shorten an expression with an initial constant operand.
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI const DIExpression * convertToUndefExpression(const DIExpression *Expr)
Removes all elements from Expr that do not apply to an undef debug value, which includes every operat...
Base class for variables.
A debug info location.
Definition DebugLoc.h:126
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:778
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:782
iterator end()
Definition DenseMap.h:702
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:694
MachineInstr * EmitDbgValue(SDDbgValue *SD, VRBaseMapType &VRBaseMap)
EmitDbgValue - Generate machine instruction for a dbg_value node.
MachineInstr * EmitDbgInstrRef(SDDbgValue *SD, VRBaseMapType &VRBaseMap)
Emit a dbg_value as a DBG_INSTR_REF.
SmallDenseMap< SDValue, Register, 16 > VRBaseMapType
MachineInstr * EmitDbgLabel(SDDbgLabel *SD)
Generate machine instruction for a dbg_label node.
MachineInstr * EmitDbgNoLocation(SDDbgValue *SD)
Emit a DBG_VALUE $noreg, indicating a variable has no location.
static unsigned CountResults(SDNode *Node)
CountResults - The results of target nodes have register or immediate operands first,...
MachineInstr * EmitDbgValueList(SDDbgValue *SD, VRBaseMapType &VRBaseMap)
Emit a DBG_VALUE_LIST from the operands to SDDbgValue.
InstrEmitter(const TargetMachine &TM, MachineBasicBlock *mbb, MachineBasicBlock::iterator insertpos)
InstrEmitter - Construct an InstrEmitter and set it to start inserting at the given position in the g...
void AddDbgValueLocationOps(MachineInstrBuilder &MIB, const MCInstrDesc &DbgValDesc, ArrayRef< SDDbgOperand > Locations, VRBaseMapType &VRBaseMap)
MachineInstr * EmitDbgValueFromSingleOp(SDDbgValue *SD, VRBaseMapType &VRBaseMap)
Emit a DBG_VALUE from the operands to SDDbgValue.
Describe properties that are true of each instruction in the target description file.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
ArrayRef< MCOperandInfo > operands() const
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specified operand constraint if it is present.
ArrayRef< MCPhysReg > implicit_uses() const
Return a list of registers that are potentially read by any instance of this machine instruction.
bool isAllocatable() const
isAllocatable - Return true if this register class may be used to create virtual registers.
bool expensiveOrImpossibleToCopy() const
bool hasSubClassEq(const MCRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
Metadata node.
Definition Metadata.h:1081
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
Machine Value Type.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
const MachineInstrBuilder & addTargetIndex(unsigned Idx, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addCImm(const ConstantInt *Val) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addBlockAddress(const BlockAddress *BA, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addMetadata(const MDNode *MD) const
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addFPImm(const ConstantFP *Val) const
const MachineInstrBuilder & addJumpTableIndex(unsigned Idx, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
LLVM_ABI void setCFIType(MachineFunction &MF, uint32_t Type)
Set the CFI type for the instruction.
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr reads the specified register.
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
LLVM_ABI void insert(mop_iterator InsertBefore, ArrayRef< MachineOperand > Ops)
Inserts Ops BEFORE It. Can untie/retie tied operands.
LLVM_ABI void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void setPhysRegsDeadExcept(ArrayRef< Register > UsedRegs, const TargetRegisterInfo &TRI)
Mark every physreg used by this instruction as dead except those in the UsedRegs list.
const MachineOperand & getOperand(unsigned i) const
MachineOperand * findRegisterDefOperand(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
MachineOperand class - Representation of each machine instruction operand.
static MachineOperand CreateFPImm(const ConstantFP *CFP)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static MachineOperand CreateCImm(const ConstantInt *CI)
void setIsEarlyClobber(bool Val=true)
static MachineOperand CreateImm(int64_t Val)
static MachineOperand CreateDbgInstrRef(unsigned InstrIdx, unsigned OpIdx)
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
void setTargetFlags(unsigned F)
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Holds the information from a dbg_label node through SDISel.
MDNode * getLabel() const
Returns the MDNode pointer for the label.
const DebugLoc & getDebugLoc() const
Returns the DebugLoc.
Holds the information for a single machine location through SDISel; either an SDNode,...
const GlobalValue * getGlobal() const
Returns the GlobalValue whose address describes the variable.
Register getVReg() const
Returns the Virtual Register for a VReg.
unsigned getResNo() const
Returns the ResNo for a register ref.
static SDDbgOperand fromConst(const Value *Const)
SDNode * getSDNode() const
Returns the SDNode* for a register ref.
@ VREG
Value is a virtual register.
@ FRAMEIX
Value is contents of a stack location.
@ SDNODE
Value is the result of an expression.
@ CONST
Value is a constant.
@ GLOBALADDR
Value is the address of a global.
Kind getKind() const
Holds the information from a dbg_value node through SDISel.
const DebugLoc & getDebugLoc() const
Returns the DebugLoc.
DIVariable * getVariable() const
Returns the DIVariable pointer for the variable.
bool isInvalidated() const
ArrayRef< SDDbgOperand > getLocationOps() const
DIExpression * getExpression() const
Returns the DIExpression pointer for the expression.
bool isIndirect() const
Returns whether this is an indirect value.
void setIsEmitted()
setIsEmitted / isEmitted - Getter/Setter for flag indicating that this SDDbgValue has been emitted to...
bool isVariadic() const
Represents one node in the SelectionDAG.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isMachineOpcode() const
unsigned getMachineOpcode() const
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static LLVM_ABI unsigned getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx)
Get index of next meta operand.
Primary interface to the complete machine description for the target machine.
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:263
@ CONVERGENCECTRL_ANCHOR
The llvm.experimental.convergence.* intrinsics.
@ DEACTIVATION_SYMBOL
Untyped node storing deactivation symbol reference (DeactivationSymbolSDNode).
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ANNOTATION_LABEL
ANNOTATION_LABEL - Represents a mid basic block label used by annotations.
@ CONVERGENCECTRL_GLUE
This does not correspond to any convergence control intrinsic.
@ CONVERGENCECTRL_ENTRY
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:232
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:50
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:226
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:55
@ CONVERGENCECTRL_LOOP
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ User
could "use" a pointer
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ EarlyClobber
Register definition happens before uses.
@ Define
Register definition.
constexpr RegState getImplRegState(bool B)
constexpr RegState getKillRegState(bool B)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
constexpr RegState getDeadRegState(bool B)
Op::Description Desc
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
constexpr RegState getDefRegState(bool B)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr RegState getDebugRegState(bool B)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
#define N
TODO: Might pack better if we changed this to a Struct of Arrays, since MachineOperand is width 32,...