LLVM 20.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"
27#include "llvm/IR/PseudoProbe.h"
30using namespace llvm;
31
32#define DEBUG_TYPE "instr-emitter"
33
34/// MinRCSize - Smallest register class we allow when constraining virtual
35/// registers. If satisfying all register class constraints would require
36/// using a smaller register class, emit a COPY to a new virtual register
37/// instead.
38const unsigned MinRCSize = 4;
39
40/// CountResults - The results of target nodes have register or immediate
41/// operands first, then an optional chain, and optional glue operands (which do
42/// not go into the resulting MachineInstr).
44 unsigned N = Node->getNumValues();
45 while (N && Node->getValueType(N - 1) == MVT::Glue)
46 --N;
47 if (N && Node->getValueType(N - 1) == MVT::Other)
48 --N; // Skip over chain result.
49 return N;
50}
51
52/// countOperands - The inputs to target nodes have any actual inputs first,
53/// followed by an optional chain operand, then an optional glue operand.
54/// Compute the number of actual operands that will go into the resulting
55/// MachineInstr.
56///
57/// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding
58/// the chain and glue. These operands may be implicit on the machine instr.
59static unsigned countOperands(SDNode *Node, unsigned NumExpUses,
60 unsigned &NumImpUses) {
61 unsigned N = Node->getNumOperands();
62 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
63 --N;
64 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
65 --N; // Ignore chain if it exists.
66
67 // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses.
68 NumImpUses = N - NumExpUses;
69 for (unsigned I = N; I > NumExpUses; --I) {
70 if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1)))
71 continue;
72 if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1)))
73 if (RN->getReg().isPhysical())
74 continue;
75 NumImpUses = N - I;
76 break;
77 }
78
79 return N;
80}
81
82/// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
83/// implicit physical register output.
84void InstrEmitter::EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone,
85 Register SrcReg, VRBaseMapType &VRBaseMap) {
86 Register VRBase;
87 if (SrcReg.isVirtual()) {
88 // Just use the input register directly!
89 SDValue Op(Node, ResNo);
90 if (IsClone)
91 VRBaseMap.erase(Op);
92 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
93 (void)isNew; // Silence compiler warning.
94 assert(isNew && "Node emitted out of order - early");
95 return;
96 }
97
98 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
99 // the CopyToReg'd destination register instead of creating a new vreg.
100 bool MatchReg = true;
101 const TargetRegisterClass *UseRC = nullptr;
102 MVT VT = Node->getSimpleValueType(ResNo);
103
104 // Stick to the preferred register classes for legal types.
105 if (TLI->isTypeLegal(VT))
106 UseRC = TLI->getRegClassFor(VT, Node->isDivergent());
107
108 for (SDNode *User : Node->users()) {
109 bool Match = true;
110 if (User->getOpcode() == ISD::CopyToReg &&
111 User->getOperand(2).getNode() == Node &&
112 User->getOperand(2).getResNo() == ResNo) {
113 Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
114 if (DestReg.isVirtual()) {
115 VRBase = DestReg;
116 Match = false;
117 } else if (DestReg != SrcReg)
118 Match = false;
119 } else {
120 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
122 if (Op.getNode() != Node || Op.getResNo() != ResNo)
123 continue;
124 MVT VT = Node->getSimpleValueType(Op.getResNo());
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(), TRI, *MF));
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, VT);
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->getCopyCost() < 0) {
169 VRBase = SrcReg;
170 } else {
171 // Create the reg, emit the copy.
172 VRBase = MRI->createVirtualRegister(DstRC);
173 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
174 VRBase).addReg(SrcReg);
175 }
176
177 SDValue Op(Node, ResNo);
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, TRI, *MF));
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 == 0) {
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, TRI, *MF);
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, Op.getNode()->getDebugLoc(),
355 TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
356 VReg = NewVReg;
357 } else {
358 assert(ConstrainedRC->isAllocatable() &&
359 "Constraining an allocatable VReg produced an unallocatable class?");
360 }
361 }
362 }
363
364 // If this value has only one use, that use is a kill. This is a
365 // conservative approximation. InstrEmitter does trivial coalescing
366 // with CopyFromReg nodes, so don't emit kill flags for them.
367 // Avoid kill flags on Schedule cloned nodes, since there will be
368 // multiple uses.
369 // Tied operands are never killed, so we need to check that. And that
370 // means we need to determine the index of the operand.
371 // Don't kill convergence control tokens. Initially they are only used in glue
372 // nodes, and the InstrEmitter later adds implicit uses on the users of the
373 // glue node. This can sometimes make it seem like there is only one use,
374 // which is the glue node itself.
375 bool isKill = Op.hasOneUse() && !isConvergenceCtrlMachineOp(Op) &&
376 Op.getNode()->getOpcode() != ISD::CopyFromReg && !IsDebug &&
377 !(IsClone || IsCloned);
378 if (isKill) {
379 unsigned Idx = MIB->getNumOperands();
380 while (Idx > 0 &&
381 MIB->getOperand(Idx-1).isReg() &&
382 MIB->getOperand(Idx-1).isImplicit())
383 --Idx;
384 bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1;
385 if (isTied)
386 isKill = false;
387 }
388
389 MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) |
390 getDebugRegState(IsDebug));
391}
392
393/// AddOperand - Add the specified operand to the specified machine instr. II
394/// specifies the instruction information for the node, and IIOpNum is the
395/// operand number (in the II) that we are adding.
396void InstrEmitter::AddOperand(MachineInstrBuilder &MIB, SDValue Op,
397 unsigned IIOpNum, const MCInstrDesc *II,
398 VRBaseMapType &VRBaseMap, bool IsDebug,
399 bool IsClone, bool IsCloned) {
400 if (Op.isMachineOpcode()) {
401 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
402 IsDebug, IsClone, IsCloned);
403 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
404 MIB.addImm(C->getSExtValue());
405 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
406 MIB.addFPImm(F->getConstantFPValue());
407 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
408 Register VReg = R->getReg();
409 MVT OpVT = Op.getSimpleValueType();
410 const TargetRegisterClass *IIRC =
411 II ? TRI->getAllocatableClass(TII->getRegClass(*II, IIOpNum, TRI, *MF))
412 : nullptr;
413 const TargetRegisterClass *OpRC =
414 TLI->isTypeLegal(OpVT)
415 ? TLI->getRegClassFor(OpVT,
416 Op.getNode()->isDivergent() ||
417 (IIRC && TRI->isDivergentRegClass(IIRC)))
418 : nullptr;
419
420 if (OpRC && IIRC && OpRC != IIRC && VReg.isVirtual()) {
421 Register NewVReg = MRI->createVirtualRegister(IIRC);
422 BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
423 TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
424 VReg = NewVReg;
425 }
426 // Turn additional physreg operands into implicit uses on non-variadic
427 // instructions. This is used by call and return instructions passing
428 // arguments in registers.
429 bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic());
430 MIB.addReg(VReg, getImplRegState(Imp));
431 } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) {
432 MIB.addRegMask(RM->getRegMask());
433 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
434 MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(),
435 TGA->getTargetFlags());
436 } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
437 MIB.addMBB(BBNode->getBasicBlock());
438 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
439 MIB.addFrameIndex(FI->getIndex());
440 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
441 MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags());
442 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
443 int Offset = CP->getOffset();
444 Align Alignment = CP->getAlign();
445
446 unsigned Idx;
448 if (CP->isMachineConstantPoolEntry())
449 Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Alignment);
450 else
451 Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Alignment);
452 MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags());
453 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
454 MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags());
455 } else if (auto *SymNode = dyn_cast<MCSymbolSDNode>(Op)) {
456 MIB.addSym(SymNode->getMCSymbol());
457 } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) {
458 MIB.addBlockAddress(BA->getBlockAddress(),
459 BA->getOffset(),
460 BA->getTargetFlags());
461 } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) {
462 MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags());
463 } else {
464 assert(Op.getValueType() != MVT::Other &&
465 Op.getValueType() != MVT::Glue &&
466 "Chain and glue operands should occur at end of operand list!");
467 AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
468 IsDebug, IsClone, IsCloned);
469 }
470}
471
472Register InstrEmitter::ConstrainForSubReg(Register VReg, unsigned SubIdx,
473 MVT VT, bool isDivergent, const DebugLoc &DL) {
474 const TargetRegisterClass *VRC = MRI->getRegClass(VReg);
475 const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx);
476
477 // RC is a sub-class of VRC that supports SubIdx. Try to constrain VReg
478 // within reason.
479 if (RC && RC != VRC)
480 RC = MRI->constrainRegClass(VReg, RC, MinRCSize);
481
482 // VReg has been adjusted. It can be used with SubIdx operands now.
483 if (RC)
484 return VReg;
485
486 // VReg couldn't be reasonably constrained. Emit a COPY to a new virtual
487 // register instead.
488 RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT, isDivergent), SubIdx);
489 assert(RC && "No legal register class for VT supports that SubIdx");
490 Register NewReg = MRI->createVirtualRegister(RC);
491 BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg)
492 .addReg(VReg);
493 return NewReg;
494}
495
496/// EmitSubregNode - Generate machine code for subreg nodes.
497///
498void InstrEmitter::EmitSubregNode(SDNode *Node, VRBaseMapType &VRBaseMap,
499 bool IsClone, bool IsCloned) {
500 Register VRBase;
501 unsigned Opc = Node->getMachineOpcode();
502
503 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
504 // the CopyToReg'd destination register instead of creating a new vreg.
505 for (SDNode *User : Node->users()) {
506 if (User->getOpcode() == ISD::CopyToReg &&
507 User->getOperand(2).getNode() == Node) {
508 Register DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
509 if (DestReg.isVirtual()) {
510 VRBase = DestReg;
511 break;
512 }
513 }
514 }
515
516 if (Opc == TargetOpcode::EXTRACT_SUBREG) {
517 // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub. There are no
518 // constraints on the %dst register, COPY can target all legal register
519 // classes.
520 unsigned SubIdx = Node->getConstantOperandVal(1);
521 const TargetRegisterClass *TRC =
522 TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
523
526 RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(0));
527 if (R && R->getReg().isPhysical()) {
528 Reg = R->getReg();
529 DefMI = nullptr;
530 } else {
531 Reg = R ? R->getReg() : getVR(Node->getOperand(0), VRBaseMap);
532 DefMI = MRI->getVRegDef(Reg);
533 }
534
535 Register SrcReg, DstReg;
536 unsigned DefSubIdx;
537 if (DefMI &&
538 TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) &&
539 SubIdx == DefSubIdx &&
540 TRC == MRI->getRegClass(SrcReg)) {
541 // Optimize these:
542 // r1025 = s/zext r1024, 4
543 // r1026 = extract_subreg r1025, 4
544 // to a copy
545 // r1026 = copy r1024
546 VRBase = MRI->createVirtualRegister(TRC);
547 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
548 TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg);
549 MRI->clearKillFlags(SrcReg);
550 } else {
551 // Reg may not support a SubIdx sub-register, and we may need to
552 // constrain its register class or issue a COPY to a compatible register
553 // class.
554 if (Reg.isVirtual())
555 Reg = ConstrainForSubReg(Reg, SubIdx,
556 Node->getOperand(0).getSimpleValueType(),
557 Node->isDivergent(), Node->getDebugLoc());
558 // Create the destreg if it is missing.
559 if (!VRBase)
560 VRBase = MRI->createVirtualRegister(TRC);
561
562 // Create the extract_subreg machine instruction.
563 MachineInstrBuilder CopyMI =
564 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
565 TII->get(TargetOpcode::COPY), VRBase);
566 if (Reg.isVirtual())
567 CopyMI.addReg(Reg, 0, SubIdx);
568 else
569 CopyMI.addReg(TRI->getSubReg(Reg, SubIdx));
570 }
571 } else if (Opc == TargetOpcode::INSERT_SUBREG ||
572 Opc == TargetOpcode::SUBREG_TO_REG) {
573 SDValue N0 = Node->getOperand(0);
574 SDValue N1 = Node->getOperand(1);
575 SDValue N2 = Node->getOperand(2);
576 unsigned SubIdx = N2->getAsZExtVal();
577
578 // Figure out the register class to create for the destreg. It should be
579 // the largest legal register class supporting SubIdx sub-registers.
580 // RegisterCoalescer will constrain it further if it decides to eliminate
581 // the INSERT_SUBREG instruction.
582 //
583 // %dst = INSERT_SUBREG %src, %sub, SubIdx
584 //
585 // is lowered by TwoAddressInstructionPass to:
586 //
587 // %dst = COPY %src
588 // %dst:SubIdx = COPY %sub
589 //
590 // There is no constraint on the %src register class.
591 //
592 const TargetRegisterClass *SRC =
593 TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
594 SRC = TRI->getSubClassWithSubReg(SRC, SubIdx);
595 assert(SRC && "No register class supports VT and SubIdx for INSERT_SUBREG");
596
597 if (VRBase == 0 || !SRC->hasSubClassEq(MRI->getRegClass(VRBase)))
598 VRBase = MRI->createVirtualRegister(SRC);
599
600 // Create the insert_subreg or subreg_to_reg machine instruction.
602 BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc), VRBase);
603
604 // If creating a subreg_to_reg, then the first input operand
605 // is an implicit value immediate, otherwise it's a register
606 if (Opc == TargetOpcode::SUBREG_TO_REG) {
607 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
608 MIB.addImm(SD->getZExtValue());
609 } else
610 AddOperand(MIB, N0, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
611 IsClone, IsCloned);
612 // Add the subregister being inserted
613 AddOperand(MIB, N1, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
614 IsClone, IsCloned);
615 MIB.addImm(SubIdx);
616 MBB->insert(InsertPos, MIB);
617 } else
618 llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg");
619
620 SDValue Op(Node, 0);
621 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
622 (void)isNew; // Silence compiler warning.
623 assert(isNew && "Node emitted out of order - early");
624}
625
626/// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
627/// COPY_TO_REGCLASS is just a normal copy, except that the destination
628/// register is constrained to be in a particular register class.
629///
630void
631InstrEmitter::EmitCopyToRegClassNode(SDNode *Node,
632 VRBaseMapType &VRBaseMap) {
633 Register VReg = getVR(Node->getOperand(0), VRBaseMap);
634
635 // Create the new VReg in the destination class and emit a copy.
636 unsigned DstRCIdx = Node->getConstantOperandVal(1);
637 const TargetRegisterClass *DstRC =
638 TRI->getAllocatableClass(TRI->getRegClass(DstRCIdx));
639 Register NewVReg = MRI->createVirtualRegister(DstRC);
640 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
641 NewVReg).addReg(VReg);
642
643 SDValue Op(Node, 0);
644 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
645 (void)isNew; // Silence compiler warning.
646 assert(isNew && "Node emitted out of order - early");
647}
648
649/// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
650///
651void InstrEmitter::EmitRegSequence(SDNode *Node, VRBaseMapType &VRBaseMap,
652 bool IsClone, bool IsCloned) {
653 unsigned DstRCIdx = Node->getConstantOperandVal(0);
654 const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
655 Register NewVReg = MRI->createVirtualRegister(TRI->getAllocatableClass(RC));
656 const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE);
657 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg);
658 unsigned NumOps = Node->getNumOperands();
659 // If the input pattern has a chain, then the root of the corresponding
660 // output pattern will get a chain as well. This can happen to be a
661 // REG_SEQUENCE (which is not "guarded" by countOperands/CountResults).
662 if (NumOps && Node->getOperand(NumOps-1).getValueType() == MVT::Other)
663 --NumOps; // Ignore chain if it exists.
664
665 assert((NumOps & 1) == 1 &&
666 "REG_SEQUENCE must have an odd number of operands!");
667 for (unsigned i = 1; i != NumOps; ++i) {
668 SDValue Op = Node->getOperand(i);
669 if ((i & 1) == 0) {
670 RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(i-1));
671 // Skip physical registers as they don't have a vreg to get and we'll
672 // insert copies for them in TwoAddressInstructionPass anyway.
673 if (!R || !R->getReg().isPhysical()) {
674 unsigned SubIdx = Op->getAsZExtVal();
675 Register SubReg = getVR(Node->getOperand(i - 1), VRBaseMap);
676 const TargetRegisterClass *TRC = MRI->getRegClass(SubReg);
677 const TargetRegisterClass *SRC =
678 TRI->getMatchingSuperRegClass(RC, TRC, SubIdx);
679 if (SRC && SRC != RC) {
680 MRI->setRegClass(NewVReg, SRC);
681 RC = SRC;
682 }
683 }
684 }
685 AddOperand(MIB, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false,
686 IsClone, IsCloned);
687 }
688
689 MBB->insert(InsertPos, MIB);
690 SDValue Op(Node, 0);
691 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
692 (void)isNew; // Silence compiler warning.
693 assert(isNew && "Node emitted out of order - early");
694}
695
696/// EmitDbgValue - Generate machine instruction for a dbg_value node.
697///
700 VRBaseMapType &VRBaseMap) {
701 DebugLoc DL = SD->getDebugLoc();
702 assert(cast<DILocalVariable>(SD->getVariable())
703 ->isValidLocationForIntrinsic(DL) &&
704 "Expected inlined-at fields to agree");
705
706 SD->setIsEmitted();
707
708 assert(!SD->getLocationOps().empty() &&
709 "dbg_value with no location operands?");
710
711 if (SD->isInvalidated())
712 return EmitDbgNoLocation(SD);
713
714 // Attempt to produce a DBG_INSTR_REF if we've been asked to.
715 if (EmitDebugInstrRefs)
716 if (auto *InstrRef = EmitDbgInstrRef(SD, VRBaseMap))
717 return InstrRef;
718
719 // Emit variadic dbg_value nodes as DBG_VALUE_LIST if they have not been
720 // emitted as instruction references.
721 if (SD->isVariadic())
722 return EmitDbgValueList(SD, VRBaseMap);
723
724 // Emit single-location dbg_value nodes as DBG_VALUE if they have not been
725 // emitted as instruction references.
726 return EmitDbgValueFromSingleOp(SD, VRBaseMap);
727}
728
730 const Value *V = Op.getConst();
731 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
732 if (CI->getBitWidth() > 64)
734 return MachineOperand::CreateImm(CI->getSExtValue());
735 }
736 if (const ConstantFP *CF = dyn_cast<ConstantFP>(V))
738 // Note: This assumes that all nullptr constants are zero-valued.
739 if (isa<ConstantPointerNull>(V))
741 // Undef or unhandled value type, so return an undef operand.
743 /* Reg */ 0U, /* isDef */ false, /* isImp */ false,
744 /* isKill */ false, /* isDead */ false,
745 /* isUndef */ false, /* isEarlyClobber */ false,
746 /* SubReg */ 0, /* isDebug */ true);
747}
748
750 MachineInstrBuilder &MIB, const MCInstrDesc &DbgValDesc,
751 ArrayRef<SDDbgOperand> LocationOps,
752 VRBaseMapType &VRBaseMap) {
753 for (const SDDbgOperand &Op : LocationOps) {
754 switch (Op.getKind()) {
756 MIB.addFrameIndex(Op.getFrameIx());
757 break;
759 MIB.addReg(Op.getVReg());
760 break;
762 SDValue V = SDValue(Op.getSDNode(), Op.getResNo());
763 // It's possible we replaced this SDNode with other(s) and therefore
764 // didn't generate code for it. It's better to catch these cases where
765 // they happen and transfer the debug info, but trying to guarantee that
766 // in all cases would be very fragile; this is a safeguard for any
767 // that were missed.
768 if (VRBaseMap.count(V) == 0)
769 MIB.addReg(0U); // undef
770 else
771 AddOperand(MIB, V, (*MIB).getNumOperands(), &DbgValDesc, VRBaseMap,
772 /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false);
773 } break;
776 break;
777 }
778 }
779}
780
783 VRBaseMapType &VRBaseMap) {
784 MDNode *Var = SD->getVariable();
785 const DIExpression *Expr = (DIExpression *)SD->getExpression();
786 DebugLoc DL = SD->getDebugLoc();
787 const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_INSTR_REF);
788
789 // Returns true if the given operand is not a legal debug operand for a
790 // DBG_INSTR_REF.
791 auto IsInvalidOp = [](SDDbgOperand DbgOp) {
792 return DbgOp.getKind() == SDDbgOperand::FRAMEIX;
793 };
794 // Returns true if the given operand is not itself an instruction reference
795 // but is a legal debug operand for a DBG_INSTR_REF.
796 auto IsNonInstrRefOp = [](SDDbgOperand DbgOp) {
797 return DbgOp.getKind() == SDDbgOperand::CONST;
798 };
799
800 // If this variable location does not depend on any instructions or contains
801 // any stack locations, produce it as a standard debug value instead.
802 if (any_of(SD->getLocationOps(), IsInvalidOp) ||
803 all_of(SD->getLocationOps(), IsNonInstrRefOp)) {
804 if (SD->isVariadic())
805 return EmitDbgValueList(SD, VRBaseMap);
806 return EmitDbgValueFromSingleOp(SD, VRBaseMap);
807 }
808
809 // Immediately fold any indirectness from the LLVM-IR intrinsic into the
810 // expression:
811 if (SD->isIndirect())
812 Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);
813 // If this is not already a variadic expression, it must be modified to become
814 // one.
815 if (!SD->isVariadic())
817
819
820 // It may not be immediately possible to identify the MachineInstr that
821 // defines a VReg, it can depend for example on the order blocks are
822 // emitted in. When this happens, or when further analysis is needed later,
823 // produce an instruction like this:
824 //
825 // DBG_INSTR_REF !123, !456, %0:gr64
826 //
827 // i.e., point the instruction at the vreg, and patch it up later in
828 // MachineFunction::finalizeDebugInstrRefs.
829 auto AddVRegOp = [&](unsigned VReg) {
831 /* Reg */ VReg, /* isDef */ false, /* isImp */ false,
832 /* isKill */ false, /* isDead */ false,
833 /* isUndef */ false, /* isEarlyClobber */ false,
834 /* SubReg */ 0, /* isDebug */ true));
835 };
836 unsigned OpCount = SD->getLocationOps().size();
837 for (unsigned OpIdx = 0; OpIdx < OpCount; ++OpIdx) {
838 SDDbgOperand DbgOperand = SD->getLocationOps()[OpIdx];
839
840 // Try to find both the defined register and the instruction defining it.
841 MachineInstr *DefMI = nullptr;
842 unsigned VReg;
843
844 if (DbgOperand.getKind() == SDDbgOperand::VREG) {
845 VReg = DbgOperand.getVReg();
846
847 // No definition means that block hasn't been emitted yet. Leave a vreg
848 // reference to be fixed later.
849 if (!MRI->hasOneDef(VReg)) {
850 AddVRegOp(VReg);
851 continue;
852 }
853
854 DefMI = &*MRI->def_instr_begin(VReg);
855 } else if (DbgOperand.getKind() == SDDbgOperand::SDNODE) {
856 // Look up the corresponding VReg for the given SDNode, if any.
857 SDNode *Node = DbgOperand.getSDNode();
858 SDValue Op = SDValue(Node, DbgOperand.getResNo());
859 VRBaseMapType::iterator I = VRBaseMap.find(Op);
860 // No VReg -> produce a DBG_VALUE $noreg instead.
861 if (I == VRBaseMap.end())
862 break;
863
864 // Try to pick out a defining instruction at this point.
865 VReg = getVR(Op, VRBaseMap);
866
867 // Again, if there's no instruction defining the VReg right now, fix it up
868 // later.
869 if (!MRI->hasOneDef(VReg)) {
870 AddVRegOp(VReg);
871 continue;
872 }
873
874 DefMI = &*MRI->def_instr_begin(VReg);
875 } else {
876 assert(DbgOperand.getKind() == SDDbgOperand::CONST);
877 MOs.push_back(GetMOForConstDbgOp(DbgOperand));
878 continue;
879 }
880
881 // Avoid copy like instructions: they don't define values, only move them.
882 // Leave a virtual-register reference until it can be fixed up later, to
883 // find the underlying value definition.
884 if (DefMI->isCopyLike() || TII->isCopyInstr(*DefMI)) {
885 AddVRegOp(VReg);
886 continue;
887 }
888
889 // Find the operand number which defines the specified VReg.
890 unsigned OperandIdx = 0;
891 for (const auto &MO : DefMI->operands()) {
892 if (MO.isReg() && MO.isDef() && MO.getReg() == VReg)
893 break;
894 ++OperandIdx;
895 }
896 assert(OperandIdx < DefMI->getNumOperands());
897
898 // Make the DBG_INSTR_REF refer to that instruction, and that operand.
899 unsigned InstrNum = DefMI->getDebugInstrNum();
900 MOs.push_back(MachineOperand::CreateDbgInstrRef(InstrNum, OperandIdx));
901 }
902
903 // If we haven't created a valid MachineOperand for every DbgOp, abort and
904 // produce an undef DBG_VALUE.
905 if (MOs.size() != OpCount)
906 return EmitDbgNoLocation(SD);
907
908 return BuildMI(*MF, DL, RefII, false, MOs, Var, Expr);
909}
910
912 // An invalidated SDNode must generate an undef DBG_VALUE: although the
913 // original value is no longer computed, earlier DBG_VALUEs live ranges
914 // must not leak into later code.
915 DIVariable *Var = SD->getVariable();
916 const DIExpression *Expr =
918 DebugLoc DL = SD->getDebugLoc();
919 const MCInstrDesc &Desc = TII->get(TargetOpcode::DBG_VALUE);
920 return BuildMI(*MF, DL, Desc, false, 0U, Var, Expr);
921}
922
925 VRBaseMapType &VRBaseMap) {
926 MDNode *Var = SD->getVariable();
927 DIExpression *Expr = SD->getExpression();
928 DebugLoc DL = SD->getDebugLoc();
929 // DBG_VALUE_LIST := "DBG_VALUE_LIST" var, expression, loc (, loc)*
930 const MCInstrDesc &DbgValDesc = TII->get(TargetOpcode::DBG_VALUE_LIST);
931 // Build the DBG_VALUE_LIST instruction base.
932 auto MIB = BuildMI(*MF, DL, DbgValDesc);
933 MIB.addMetadata(Var);
934 MIB.addMetadata(Expr);
935 AddDbgValueLocationOps(MIB, DbgValDesc, SD->getLocationOps(), VRBaseMap);
936 return &*MIB;
937}
938
941 VRBaseMapType &VRBaseMap) {
942 MDNode *Var = SD->getVariable();
943 DIExpression *Expr = SD->getExpression();
944 DebugLoc DL = SD->getDebugLoc();
945 const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE);
946
947 assert(SD->getLocationOps().size() == 1 &&
948 "Non variadic dbg_value should have only one location op");
949
950 // See about constant-folding the expression.
951 // Copy the location operand in case we replace it.
952 SmallVector<SDDbgOperand, 1> LocationOps(1, SD->getLocationOps()[0]);
953 if (Expr && LocationOps[0].getKind() == SDDbgOperand::CONST) {
954 const Value *V = LocationOps[0].getConst();
955 if (auto *C = dyn_cast<ConstantInt>(V)) {
956 std::tie(Expr, C) = Expr->constantFold(C);
957 LocationOps[0] = SDDbgOperand::fromConst(C);
958 }
959 }
960
961 // Emit non-variadic dbg_value nodes as DBG_VALUE.
962 // DBG_VALUE := "DBG_VALUE" loc, isIndirect, var, expr
963 auto MIB = BuildMI(*MF, DL, II);
964 AddDbgValueLocationOps(MIB, II, LocationOps, VRBaseMap);
965
966 if (SD->isIndirect())
967 MIB.addImm(0U);
968 else
969 MIB.addReg(0U);
970
971 return MIB.addMetadata(Var).addMetadata(Expr);
972}
973
976 MDNode *Label = SD->getLabel();
977 DebugLoc DL = SD->getDebugLoc();
978 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
979 "Expected inlined-at fields to agree");
980
981 const MCInstrDesc &II = TII->get(TargetOpcode::DBG_LABEL);
982 MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
983 MIB.addMetadata(Label);
984
985 return &*MIB;
986}
987
988/// EmitMachineNode - Generate machine code for a target-specific node and
989/// needed dependencies.
990///
991void InstrEmitter::
992EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
993 VRBaseMapType &VRBaseMap) {
994 unsigned Opc = Node->getMachineOpcode();
995
996 // Handle subreg insert/extract specially
997 if (Opc == TargetOpcode::EXTRACT_SUBREG ||
998 Opc == TargetOpcode::INSERT_SUBREG ||
999 Opc == TargetOpcode::SUBREG_TO_REG) {
1000 EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned);
1001 return;
1002 }
1003
1004 // Handle COPY_TO_REGCLASS specially.
1005 if (Opc == TargetOpcode::COPY_TO_REGCLASS) {
1006 EmitCopyToRegClassNode(Node, VRBaseMap);
1007 return;
1008 }
1009
1010 // Handle REG_SEQUENCE specially.
1011 if (Opc == TargetOpcode::REG_SEQUENCE) {
1012 EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned);
1013 return;
1014 }
1015
1016 if (Opc == TargetOpcode::IMPLICIT_DEF)
1017 // We want a unique VR for each IMPLICIT_DEF use.
1018 return;
1019
1020 const MCInstrDesc &II = TII->get(Opc);
1021 unsigned NumResults = CountResults(Node);
1022 unsigned NumDefs = II.getNumDefs();
1023 const MCPhysReg *ScratchRegs = nullptr;
1024
1025 // Handle STACKMAP and PATCHPOINT specially and then use the generic code.
1026 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
1027 // Stackmaps do not have arguments and do not preserve their calling
1028 // convention. However, to simplify runtime support, they clobber the same
1029 // scratch registers as AnyRegCC.
1030 unsigned CC = CallingConv::AnyReg;
1031 if (Opc == TargetOpcode::PATCHPOINT) {
1032 CC = Node->getConstantOperandVal(PatchPointOpers::CCPos);
1033 NumDefs = NumResults;
1034 }
1035 ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC);
1036 } else if (Opc == TargetOpcode::STATEPOINT) {
1037 NumDefs = NumResults;
1038 }
1039
1040 unsigned NumImpUses = 0;
1041 unsigned NodeOperands =
1042 countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses);
1043 bool HasVRegVariadicDefs = !MF->getTarget().usesPhysRegsForValues() &&
1044 II.isVariadic() && II.variadicOpsAreDefs();
1045 bool HasPhysRegOuts = NumResults > NumDefs && !II.implicit_defs().empty() &&
1046 !HasVRegVariadicDefs;
1047#ifndef NDEBUG
1048 unsigned NumMIOperands = NodeOperands + NumResults;
1049 if (II.isVariadic())
1050 assert(NumMIOperands >= II.getNumOperands() &&
1051 "Too few operands for a variadic node!");
1052 else
1053 assert(NumMIOperands >= II.getNumOperands() &&
1054 NumMIOperands <=
1055 II.getNumOperands() + II.implicit_defs().size() + NumImpUses &&
1056 "#operands for dag node doesn't match .td file!");
1057#endif
1058
1059 // Create the new machine instruction.
1060 MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II);
1061
1062 // Transfer IR flags from the SDNode to the MachineInstr
1063 MachineInstr *MI = MIB.getInstr();
1064 const SDNodeFlags Flags = Node->getFlags();
1065 if (Flags.hasUnpredictable())
1067
1068 // Add result register values for things that are defined by this
1069 // instruction.
1070 if (NumResults) {
1071 CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap);
1072
1073 if (Flags.hasNoSignedZeros())
1075
1076 if (Flags.hasAllowReciprocal())
1078
1079 if (Flags.hasNoNaNs())
1081
1082 if (Flags.hasNoInfs())
1084
1085 if (Flags.hasAllowContract())
1087
1088 if (Flags.hasApproximateFuncs())
1090
1091 if (Flags.hasAllowReassociation())
1093
1094 if (Flags.hasNoUnsignedWrap())
1096
1097 if (Flags.hasNoSignedWrap())
1099
1100 if (Flags.hasExact())
1102
1103 if (Flags.hasNoFPExcept())
1105
1106 if (Flags.hasDisjoint())
1108
1109 if (Flags.hasSameSign())
1111 }
1112
1113 // Emit all of the actual operands of this instruction, adding them to the
1114 // instruction as appropriate.
1115 bool HasOptPRefs = NumDefs > NumResults;
1116 assert((!HasOptPRefs || !HasPhysRegOuts) &&
1117 "Unable to cope with optional defs and phys regs defs!");
1118 unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0;
1119 for (unsigned i = NumSkip; i != NodeOperands; ++i)
1120 AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II,
1121 VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned);
1122
1123 // Add scratch registers as implicit def and early clobber
1124 if (ScratchRegs)
1125 for (unsigned i = 0; ScratchRegs[i]; ++i)
1126 MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine |
1128
1129 // Set the memory reference descriptions of this instruction now that it is
1130 // part of the function.
1131 MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands());
1132
1133 // Set the CFI type.
1134 MIB->setCFIType(*MF, Node->getCFIType());
1135
1136 // Insert the instruction into position in the block. This needs to
1137 // happen before any custom inserter hook is called so that the
1138 // hook knows where in the block to insert the replacement code.
1139 MBB->insert(InsertPos, MIB);
1140
1141 // The MachineInstr may also define physregs instead of virtregs. These
1142 // physreg values can reach other instructions in different ways:
1143 //
1144 // 1. When there is a use of a Node value beyond the explicitly defined
1145 // virtual registers, we emit a CopyFromReg for one of the implicitly
1146 // defined physregs. This only happens when HasPhysRegOuts is true.
1147 //
1148 // 2. A CopyFromReg reading a physreg may be glued to this instruction.
1149 //
1150 // 3. A glued instruction may implicitly use a physreg.
1151 //
1152 // 4. A glued instruction may use a RegisterSDNode operand.
1153 //
1154 // Collect all the used physreg defs, and make sure that any unused physreg
1155 // defs are marked as dead.
1156 SmallVector<Register, 8> UsedRegs;
1157
1158 // Additional results must be physical register defs.
1159 if (HasPhysRegOuts) {
1160 for (unsigned i = NumDefs; i < NumResults; ++i) {
1161 Register Reg = II.implicit_defs()[i - NumDefs];
1162 if (!Node->hasAnyUseOfValue(i))
1163 continue;
1164 // This implicitly defined physreg has a use.
1165 UsedRegs.push_back(Reg);
1166 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
1167 }
1168 }
1169
1170 // Scan the glue chain for any used physregs.
1171 if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) {
1172 for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) {
1173 if (F->getOpcode() == ISD::CopyFromReg) {
1174 UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg());
1175 continue;
1176 } else if (F->getOpcode() == ISD::CopyToReg) {
1177 // Skip CopyToReg nodes that are internal to the glue chain.
1178 continue;
1179 }
1180 // Collect declared implicit uses.
1181 const MCInstrDesc &MCID = TII->get(F->getMachineOpcode());
1182 append_range(UsedRegs, MCID.implicit_uses());
1183 // In addition to declared implicit uses, we must also check for
1184 // direct RegisterSDNode operands.
1185 for (const SDValue &Op : F->op_values())
1186 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
1187 Register Reg = R->getReg();
1188 if (Reg.isPhysical())
1189 UsedRegs.push_back(Reg);
1190 }
1191 }
1192 }
1193
1194 // Add rounding control registers as implicit def for function call.
1195 if (II.isCall() && MF->getFunction().hasFnAttribute(Attribute::StrictFP)) {
1197 for (MCPhysReg Reg : RCRegs)
1198 UsedRegs.push_back(Reg);
1199 }
1200
1201 // Finally mark unused registers as dead.
1202 if (!UsedRegs.empty() || !II.implicit_defs().empty() || II.hasOptionalDef())
1203 MIB->setPhysRegsDeadExcept(UsedRegs, *TRI);
1204
1205 // STATEPOINT is too 'dynamic' to have meaningful machine description.
1206 // We have to manually tie operands.
1207 if (Opc == TargetOpcode::STATEPOINT && NumDefs > 0) {
1208 assert(!HasPhysRegOuts && "STATEPOINT mishandled");
1209 MachineInstr *MI = MIB;
1210 unsigned Def = 0;
1212 assert(First > 0 && "Statepoint has Defs but no GC ptr list");
1213 unsigned Use = (unsigned)First;
1214 while (Def < NumDefs) {
1215 if (MI->getOperand(Use).isReg())
1216 MI->tieOperands(Def++, Use);
1218 }
1219 }
1220
1221 if (SDNode *GluedNode = Node->getGluedNode()) {
1222 // FIXME: Possibly iterate over multiple glue nodes?
1223 if (GluedNode->getOpcode() ==
1224 ~(unsigned)TargetOpcode::CONVERGENCECTRL_GLUE) {
1225 Register VReg = getVR(GluedNode->getOperand(0), VRBaseMap);
1226 MachineOperand MO = MachineOperand::CreateReg(VReg, /*isDef=*/false,
1227 /*isImp=*/true);
1228 MIB->addOperand(MO);
1229 }
1230 }
1231
1232 // Run post-isel target hook to adjust this instruction if needed.
1233 if (II.hasPostISelHook())
1235}
1236
1237/// EmitSpecialNode - Generate machine code for a target-independent node and
1238/// needed dependencies.
1239void InstrEmitter::
1240EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
1241 VRBaseMapType &VRBaseMap) {
1242 switch (Node->getOpcode()) {
1243 default:
1244#ifndef NDEBUG
1245 Node->dump();
1246#endif
1247 llvm_unreachable("This target-independent node should have been selected!");
1248 case ISD::EntryToken:
1249 case ISD::MERGE_VALUES:
1250 case ISD::TokenFactor: // fall thru
1251 break;
1252 case ISD::CopyToReg: {
1253 Register DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1254 SDValue SrcVal = Node->getOperand(2);
1255 if (DestReg.isVirtual() && SrcVal.isMachineOpcode() &&
1256 SrcVal.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
1257 // Instead building a COPY to that vreg destination, build an
1258 // IMPLICIT_DEF instruction instead.
1259 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
1260 TII->get(TargetOpcode::IMPLICIT_DEF), DestReg);
1261 break;
1262 }
1263 Register SrcReg;
1264 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
1265 SrcReg = R->getReg();
1266 else
1267 SrcReg = getVR(SrcVal, VRBaseMap);
1268
1269 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
1270 break;
1271
1272 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
1273 DestReg).addReg(SrcReg);
1274 break;
1275 }
1276 case ISD::CopyFromReg: {
1277 Register SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1278 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
1279 break;
1280 }
1281 case ISD::EH_LABEL:
1282 case ISD::ANNOTATION_LABEL: {
1283 unsigned Opc = (Node->getOpcode() == ISD::EH_LABEL)
1284 ? TargetOpcode::EH_LABEL
1285 : TargetOpcode::ANNOTATION_LABEL;
1286 MCSymbol *S = cast<LabelSDNode>(Node)->getLabel();
1287 BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
1288 TII->get(Opc)).addSym(S);
1289 break;
1290 }
1291
1293 case ISD::LIFETIME_END: {
1294 unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START)
1295 ? TargetOpcode::LIFETIME_START
1296 : TargetOpcode::LIFETIME_END;
1297 auto *FI = cast<FrameIndexSDNode>(Node->getOperand(1));
1298 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
1299 .addFrameIndex(FI->getIndex());
1300 break;
1301 }
1302
1303 case ISD::PSEUDO_PROBE: {
1304 unsigned TarOp = TargetOpcode::PSEUDO_PROBE;
1305 auto Guid = cast<PseudoProbeSDNode>(Node)->getGuid();
1306 auto Index = cast<PseudoProbeSDNode>(Node)->getIndex();
1307 auto Attr = cast<PseudoProbeSDNode>(Node)->getAttributes();
1308
1309 BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
1310 .addImm(Guid)
1311 .addImm(Index)
1313 .addImm(Attr);
1314 break;
1315 }
1316
1317 case ISD::INLINEASM:
1318 case ISD::INLINEASM_BR: {
1319 unsigned NumOps = Node->getNumOperands();
1320 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
1321 --NumOps; // Ignore the glue operand.
1322
1323 // Create the inline asm machine instruction.
1324 unsigned TgtOpc = Node->getOpcode() == ISD::INLINEASM_BR
1325 ? TargetOpcode::INLINEASM_BR
1326 : TargetOpcode::INLINEASM;
1328 BuildMI(*MF, Node->getDebugLoc(), TII->get(TgtOpc));
1329
1330 // Add the asm string as an external symbol operand.
1331 SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString);
1332 const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol();
1333 MIB.addExternalSymbol(AsmStr);
1334
1335 // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore
1336 // bits.
1337 int64_t ExtraInfo =
1338 cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))->
1339 getZExtValue();
1340 MIB.addImm(ExtraInfo);
1341
1342 // Remember to operand index of the group flags.
1343 SmallVector<unsigned, 8> GroupIdx;
1344
1345 // Remember registers that are part of early-clobber defs.
1347
1348 // Add all of the operand registers to the instruction.
1349 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
1350 unsigned Flags = Node->getConstantOperandVal(i);
1351 const InlineAsm::Flag F(Flags);
1352 const unsigned NumVals = F.getNumOperandRegisters();
1353
1354 GroupIdx.push_back(MIB->getNumOperands());
1355 MIB.addImm(Flags);
1356 ++i; // Skip the ID value.
1357
1358 switch (F.getKind()) {
1360 for (unsigned j = 0; j != NumVals; ++j, ++i) {
1361 Register Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1362 // FIXME: Add dead flags for physical and virtual registers defined.
1363 // For now, mark physical register defs as implicit to help fast
1364 // regalloc. This makes inline asm look a lot like calls.
1365 MIB.addReg(Reg, RegState::Define | getImplRegState(Reg.isPhysical()));
1366 }
1367 break;
1370 for (unsigned j = 0; j != NumVals; ++j, ++i) {
1371 Register Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
1373 getImplRegState(Reg.isPhysical()));
1374 ECRegs.push_back(Reg);
1375 }
1376 break;
1377 case InlineAsm::Kind::RegUse: // Use of register.
1378 case InlineAsm::Kind::Imm: // Immediate.
1379 case InlineAsm::Kind::Mem: // Non-function addressing mode.
1380 // The addressing mode has been selected, just add all of the
1381 // operands to the machine instruction.
1382 for (unsigned j = 0; j != NumVals; ++j, ++i)
1383 AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap,
1384 /*IsDebug=*/false, IsClone, IsCloned);
1385
1386 // Manually set isTied bits.
1387 if (F.isRegUseKind()) {
1388 unsigned DefGroup;
1389 if (F.isUseOperandTiedToDef(DefGroup)) {
1390 unsigned DefIdx = GroupIdx[DefGroup] + 1;
1391 unsigned UseIdx = GroupIdx.back() + 1;
1392 for (unsigned j = 0; j != NumVals; ++j)
1393 MIB->tieOperands(DefIdx + j, UseIdx + j);
1394 }
1395 }
1396 break;
1397 case InlineAsm::Kind::Func: // Function addressing mode.
1398 for (unsigned j = 0; j != NumVals; ++j, ++i) {
1399 SDValue Op = Node->getOperand(i);
1400 AddOperand(MIB, Op, 0, nullptr, VRBaseMap,
1401 /*IsDebug=*/false, IsClone, IsCloned);
1402
1403 // Adjust Target Flags for function reference.
1404 if (auto *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
1405 unsigned NewFlags =
1407 TGA->getGlobal());
1408 unsigned LastIdx = MIB.getInstr()->getNumOperands() - 1;
1409 MIB.getInstr()->getOperand(LastIdx).setTargetFlags(NewFlags);
1410 }
1411 }
1412 }
1413 }
1414
1415 // Add rounding control registers as implicit def for inline asm.
1416 if (MF->getFunction().hasFnAttribute(Attribute::StrictFP)) {
1418 for (MCPhysReg Reg : RCRegs)
1420 }
1421
1422 // GCC inline assembly allows input operands to also be early-clobber
1423 // output operands (so long as the operand is written only after it's
1424 // used), but this does not match the semantics of our early-clobber flag.
1425 // If an early-clobber operand register is also an input operand register,
1426 // then remove the early-clobber flag.
1427 for (Register Reg : ECRegs) {
1428 if (MIB->readsRegister(Reg, TRI)) {
1429 MachineOperand *MO =
1430 MIB->findRegisterDefOperand(Reg, TRI, false, false);
1431 assert(MO && "No def operand for clobbered register?");
1432 MO->setIsEarlyClobber(false);
1433 }
1434 }
1435
1436 // Get the mdnode from the asm if it exists and add it to the instruction.
1437 SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode);
1438 const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD();
1439 if (MD)
1440 MIB.addMetadata(MD);
1441
1442 MBB->insert(InsertPos, MIB);
1443 break;
1444 }
1445 }
1446}
1447
1448/// InstrEmitter - Construct an InstrEmitter and set it to start inserting
1449/// at the given position in the given block.
1452 : MF(mbb->getParent()), MRI(&MF->getRegInfo()),
1453 TII(MF->getSubtarget().getInstrInfo()),
1454 TRI(MF->getSubtarget().getRegisterInfo()),
1455 TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb),
1456 InsertPos(insertpos) {
1457 EmitDebugInstrRefs = mbb->getParent()->useDebugInstrRef();
1458}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file contains constants used for implementing Dwarf debug support.
const HexagonInstrInfo * TII
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...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned const TargetRegisterInfo * TRI
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file describes how to lower LLVM code to machine code.
DEMANGLE_DUMP_METHOD void dump() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:271
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
uint64_t getZExtValue() const
DWARF expression.
static DIExpression * append(const DIExpression *Expr, ArrayRef< uint64_t > Ops)
Append the opcodes Ops to DIExpr.
std::pair< DIExpression *, const ConstantInt * > constantFold(const ConstantInt *CI)
Try to shorten an expression with an initial constant operand.
static const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static 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.
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition: DenseMap.h:71
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:152
iterator end()
Definition: DenseMap.h:84
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:731
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.
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.
Definition: MCInstrDesc.h:198
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:237
ArrayRef< MCOperandInfo > operands() const
Definition: MCInstrDesc.h:239
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specified operand constraint if it is present.
Definition: MCInstrDesc.h:219
ArrayRef< MCPhysReg > implicit_uses() const
Return a list of registers that are potentially read by any instance of this machine instruction.
Definition: MCInstrDesc.h:565
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition: MCInstrInfo.h:63
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Metadata node.
Definition: Metadata.h:1069
Machine Value Type.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
The MachineConstantPool class keeps track of constants referenced by a function which must be spilled...
unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
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 & 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 & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
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.
Definition: MachineInstr.h:69
void setCFIType(MachineFunction &MF, uint32_t Type)
Set the CFI type for the instruction.
bool isCopyLike() const
Return true if the instruction behaves like a copy.
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.
Definition: MachineInstr.h:578
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.
Definition: MachineInstr.h:572
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:691
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.
void setPhysRegsDeadExcept(ArrayRef< Register > UsedRegs, const TargetRegisterInfo &TRI)
Mark every physreg used by this instruction as dead except those in the UsedRegs list.
unsigned getDebugInstrNum()
Fetch the instruction number of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:585
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 isImplicit() const
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)
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)
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
void clearKillFlags(Register Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
def_instr_iterator def_instr_begin(Register RegNo) const
bool hasOneDef(Register RegNo) const
Return true if there is exactly one operand defining the specified register.
void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:91
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:95
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,...
unsigned getResNo() const
Returns the ResNo for a register ref.
unsigned getVReg() const
Returns the Virtual Register for a VReg.
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.
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.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isMachineOpcode() const
unsigned getMachineOpcode() const
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
static unsigned getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx)
Get index of next meta operand.
Definition: StackMaps.cpp:170
MI-level Statepoint operands.
Definition: StackMaps.h:158
int getFirstGCPtrIdx()
Get index of first GC pointer operand of -1 if there are none.
Definition: StackMaps.cpp:124
Completely target-dependent object reference.
virtual bool isCoalescableExtInstr(const MachineInstr &MI, Register &SrcReg, Register &DstReg, unsigned &SubIdx) const
Return true if the instruction is a "coalescable" extension instruction.
std::optional< DestSourcePair > isCopyInstr(const MachineInstr &MI) const
If the specific machine instruction is a instruction that moves/copies value from one register to ano...
virtual const TargetRegisterClass * getRegClass(const MCInstrDesc &MCID, unsigned OpNum, const TargetRegisterInfo *TRI, const MachineFunction &MF) const
Given a machine instruction descriptor, returns the register class constraint for OpNum,...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual void AdjustInstrPostInstrSelection(MachineInstr &MI, SDNode *Node) const
This method should be implemented by targets that mark instructions with the 'hasPostISelHook' flag.
virtual ArrayRef< MCPhysReg > getRoundingControlRegisters() const
Returns a 0 terminated array of rounding control registers that can be attached into strict FP call.
virtual const MCPhysReg * getScratchRegisters(CallingConv::ID CC) const
Returns a 0 terminated array of registers that can be safely used as scratch registers.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
virtual bool usesPhysRegsForValues() const
True if the target uses physical regs (as nearly all targets do).
bool isAllocatable() const
Return true if this register class may be used to create virtual registers.
bool hasSubClassEq(const TargetRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
int getCopyCost() const
Return the cost of copying a value between two registers in this class.
const TargetRegisterClass * getMinimalPhysRegClass(MCRegister Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
virtual const TargetRegisterClass * getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const
Returns the largest legal sub-class of RC that supports the sub-register index Idx.
const TargetRegisterClass * getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
const TargetRegisterClass * getCommonSubClass(const TargetRegisterClass *A, const TargetRegisterClass *B) const
Find the largest common subclass of A and B.
const TargetRegisterClass * getAllocatableClass(const TargetRegisterClass *RC) const
Return the maximal subclass of the given register class that is allocatable or NULL.
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo.
virtual bool isDivergentRegClass(const TargetRegisterClass *RC) const
Returns true if the register class is considered divergent.
bool isTypeLegalForClass(const TargetRegisterClass &RC, MVT T) const
Return true if the given TargetRegisterClass has the ValueType T.
virtual const TargetRegisterClass * getMatchingSuperRegClass(const TargetRegisterClass *A, const TargetRegisterClass *B, unsigned Idx) const
Return a subclass of the specified register class A so that each register in it has a sub-register of...
virtual unsigned char classifyGlobalFunctionReference(const GlobalValue *GV) const
Classify a global function reference.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
Value * getOperand(unsigned i) const
Definition: User.h:228
unsigned getNumOperands() const
Definition: User.h:250
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition: CallingConv.h:60
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:243
@ CONVERGENCECTRL_ANCHOR
Definition: ISDOpcodes.h:1470
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1173
@ ANNOTATION_LABEL
ANNOTATION_LABEL - Represents a mid basic block label used by annotations.
Definition: ISDOpcodes.h:1179
@ CONVERGENCECTRL_GLUE
Definition: ISDOpcodes.h:1476
@ CONVERGENCECTRL_ENTRY
Definition: ISDOpcodes.h:1471
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition: ISDOpcodes.h:215
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition: ISDOpcodes.h:47
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition: ISDOpcodes.h:209
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1377
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
Definition: ISDOpcodes.h:1168
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1402
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ CONVERGENCECTRL_LOOP
Definition: ISDOpcodes.h:1472
@ INLINEASM
INLINEASM - Represents an inline asm block.
Definition: ISDOpcodes.h:1165
@ Define
Register definition.
@ EarlyClobber
Register definition happens before uses.
Reg
All possible values of the reg field in the ModR/M byte.
NodeAddr< DefNode * > Def
Definition: RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
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:1739
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
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:1746
unsigned getImplRegState(bool B)
unsigned getDebugRegState(bool B)
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
unsigned getDefRegState(bool B)
unsigned getKillRegState(bool B)
DWARFExpression::Operation Op
#define N
TODO: Might pack better if we changed this to a Struct of Arrays, since MachineOperand is width 32,...
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Description of the encoding of one expression Op.
These are IR-level optimization flags that may be propagated to SDNodes.