LLVM 22.0.0git
SIInstrInfo.cpp
Go to the documentation of this file.
1//===- SIInstrInfo.cpp - SI Instruction Information ----------------------===//
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/// \file
10/// SI Implementation of TargetInstrInfo.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SIInstrInfo.h"
15#include "AMDGPU.h"
16#include "AMDGPUInstrInfo.h"
17#include "AMDGPULaneMaskUtils.h"
18#include "GCNHazardRecognizer.h"
19#include "GCNSubtarget.h"
22#include "llvm/ADT/STLExtras.h"
33#include "llvm/IR/IntrinsicsAMDGPU.h"
34#include "llvm/MC/MCContext.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "si-instr-info"
41
42#define GET_INSTRINFO_CTOR_DTOR
43#include "AMDGPUGenInstrInfo.inc"
44
45namespace llvm::AMDGPU {
46#define GET_D16ImageDimIntrinsics_IMPL
47#define GET_ImageDimIntrinsicTable_IMPL
48#define GET_RsrcIntrinsics_IMPL
49#include "AMDGPUGenSearchableTables.inc"
50} // namespace llvm::AMDGPU
51
52// Must be at least 4 to be able to branch over minimum unconditional branch
53// code. This is only for making it possible to write reasonably small tests for
54// long branches.
56BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16),
57 cl::desc("Restrict range of branch instructions (DEBUG)"));
58
60 "amdgpu-fix-16-bit-physreg-copies",
61 cl::desc("Fix copies between 32 and 16 bit registers by extending to 32 bit"),
62 cl::init(true),
64
66 : AMDGPUGenInstrInfo(ST, AMDGPU::ADJCALLSTACKUP, AMDGPU::ADJCALLSTACKDOWN),
67 RI(ST), ST(ST) {
68 SchedModel.init(&ST);
69}
70
71//===----------------------------------------------------------------------===//
72// TargetInstrInfo callbacks
73//===----------------------------------------------------------------------===//
74
75static unsigned getNumOperandsNoGlue(SDNode *Node) {
76 unsigned N = Node->getNumOperands();
77 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
78 --N;
79 return N;
80}
81
82/// Returns true if both nodes have the same value for the given
83/// operand \p Op, or if both nodes do not have this operand.
85 AMDGPU::OpName OpName) {
86 unsigned Opc0 = N0->getMachineOpcode();
87 unsigned Opc1 = N1->getMachineOpcode();
88
89 int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName);
90 int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName);
91
92 if (Op0Idx == -1 && Op1Idx == -1)
93 return true;
94
95
96 if ((Op0Idx == -1 && Op1Idx != -1) ||
97 (Op1Idx == -1 && Op0Idx != -1))
98 return false;
99
100 // getNamedOperandIdx returns the index for the MachineInstr's operands,
101 // which includes the result as the first operand. We are indexing into the
102 // MachineSDNode's operands, so we need to skip the result operand to get
103 // the real index.
104 --Op0Idx;
105 --Op1Idx;
106
107 return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx);
108}
109
110static bool canRemat(const MachineInstr &MI) {
111
115 return true;
116
117 if (SIInstrInfo::isSMRD(MI)) {
118 return !MI.memoperands_empty() &&
119 llvm::all_of(MI.memoperands(), [](const MachineMemOperand *MMO) {
120 return MMO->isLoad() && MMO->isInvariant();
121 });
122 }
123
124 return false;
125}
126
128 const MachineInstr &MI) const {
129
130 if (canRemat(MI)) {
131 // Normally VALU use of exec would block the rematerialization, but that
132 // is OK in this case to have an implicit exec read as all VALU do.
133 // We really want all of the generic logic for this except for this.
134
135 // Another potential implicit use is mode register. The core logic of
136 // the RA will not attempt rematerialization if mode is set anywhere
137 // in the function, otherwise it is safe since mode is not changed.
138
139 // There is difference to generic method which does not allow
140 // rematerialization if there are virtual register uses. We allow this,
141 // therefore this method includes SOP instructions as well.
142 if (!MI.hasImplicitDef() &&
143 MI.getNumImplicitOperands() == MI.getDesc().implicit_uses().size() &&
144 !MI.mayRaiseFPException())
145 return true;
146 }
147
149}
150
151// Returns true if the scalar result of a VALU instruction depends on exec.
152bool SIInstrInfo::resultDependsOnExec(const MachineInstr &MI) const {
153 // Ignore comparisons which are only used masked with exec.
154 // This allows some hoisting/sinking of VALU comparisons.
155 if (MI.isCompare()) {
156 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::sdst);
157 if (!Dst)
158 return true;
159
160 Register DstReg = Dst->getReg();
161 if (!DstReg.isVirtual())
162 return true;
163
164 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
165 for (MachineInstr &Use : MRI.use_nodbg_instructions(DstReg)) {
166 switch (Use.getOpcode()) {
167 case AMDGPU::S_AND_SAVEEXEC_B32:
168 case AMDGPU::S_AND_SAVEEXEC_B64:
169 break;
170 case AMDGPU::S_AND_B32:
171 case AMDGPU::S_AND_B64:
172 if (!Use.readsRegister(AMDGPU::EXEC, /*TRI=*/nullptr))
173 return true;
174 break;
175 default:
176 return true;
177 }
178 }
179 return false;
180 }
181
182 switch (MI.getOpcode()) {
183 default:
184 break;
185 case AMDGPU::V_READFIRSTLANE_B32:
186 return true;
187 }
188
189 return false;
190}
191
193 // Any implicit use of exec by VALU is not a real register read.
194 return MO.getReg() == AMDGPU::EXEC && MO.isImplicit() &&
195 isVALU(*MO.getParent()) && !resultDependsOnExec(*MO.getParent());
196}
197
199 MachineBasicBlock *SuccToSinkTo,
200 MachineCycleInfo *CI) const {
201 // Allow sinking if MI edits lane mask (divergent i1 in sgpr).
202 if (MI.getOpcode() == AMDGPU::SI_IF_BREAK)
203 return true;
204
205 MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
206 // Check if sinking of MI would create temporal divergent use.
207 for (auto Op : MI.uses()) {
208 if (Op.isReg() && Op.getReg().isVirtual() &&
209 RI.isSGPRClass(MRI.getRegClass(Op.getReg()))) {
210 MachineInstr *SgprDef = MRI.getVRegDef(Op.getReg());
211
212 // SgprDef defined inside cycle
213 MachineCycle *FromCycle = CI->getCycle(SgprDef->getParent());
214 if (FromCycle == nullptr)
215 continue;
216
217 MachineCycle *ToCycle = CI->getCycle(SuccToSinkTo);
218 // Check if there is a FromCycle that contains SgprDef's basic block but
219 // does not contain SuccToSinkTo and also has divergent exit condition.
220 while (FromCycle && !FromCycle->contains(ToCycle)) {
222 FromCycle->getExitingBlocks(ExitingBlocks);
223
224 // FromCycle has divergent exit condition.
225 for (MachineBasicBlock *ExitingBlock : ExitingBlocks) {
226 if (hasDivergentBranch(ExitingBlock))
227 return false;
228 }
229
230 FromCycle = FromCycle->getParentCycle();
231 }
232 }
233 }
234
235 return true;
236}
237
239 int64_t &Offset0,
240 int64_t &Offset1) const {
241 if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode())
242 return false;
243
244 unsigned Opc0 = Load0->getMachineOpcode();
245 unsigned Opc1 = Load1->getMachineOpcode();
246
247 // Make sure both are actually loads.
248 if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad())
249 return false;
250
251 // A mayLoad instruction without a def is not a load. Likely a prefetch.
252 if (!get(Opc0).getNumDefs() || !get(Opc1).getNumDefs())
253 return false;
254
255 if (isDS(Opc0) && isDS(Opc1)) {
256
257 // FIXME: Handle this case:
258 if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1))
259 return false;
260
261 // Check base reg.
262 if (Load0->getOperand(0) != Load1->getOperand(0))
263 return false;
264
265 // Skip read2 / write2 variants for simplicity.
266 // TODO: We should report true if the used offsets are adjacent (excluded
267 // st64 versions).
268 int Offset0Idx = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
269 int Offset1Idx = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
270 if (Offset0Idx == -1 || Offset1Idx == -1)
271 return false;
272
273 // XXX - be careful of dataless loads
274 // getNamedOperandIdx returns the index for MachineInstrs. Since they
275 // include the output in the operand list, but SDNodes don't, we need to
276 // subtract the index by one.
277 Offset0Idx -= get(Opc0).NumDefs;
278 Offset1Idx -= get(Opc1).NumDefs;
279 Offset0 = Load0->getConstantOperandVal(Offset0Idx);
280 Offset1 = Load1->getConstantOperandVal(Offset1Idx);
281 return true;
282 }
283
284 if (isSMRD(Opc0) && isSMRD(Opc1)) {
285 // Skip time and cache invalidation instructions.
286 if (!AMDGPU::hasNamedOperand(Opc0, AMDGPU::OpName::sbase) ||
287 !AMDGPU::hasNamedOperand(Opc1, AMDGPU::OpName::sbase))
288 return false;
289
290 unsigned NumOps = getNumOperandsNoGlue(Load0);
291 if (NumOps != getNumOperandsNoGlue(Load1))
292 return false;
293
294 // Check base reg.
295 if (Load0->getOperand(0) != Load1->getOperand(0))
296 return false;
297
298 // Match register offsets, if both register and immediate offsets present.
299 assert(NumOps == 4 || NumOps == 5);
300 if (NumOps == 5 && Load0->getOperand(1) != Load1->getOperand(1))
301 return false;
302
303 const ConstantSDNode *Load0Offset =
305 const ConstantSDNode *Load1Offset =
307
308 if (!Load0Offset || !Load1Offset)
309 return false;
310
311 Offset0 = Load0Offset->getZExtValue();
312 Offset1 = Load1Offset->getZExtValue();
313 return true;
314 }
315
316 // MUBUF and MTBUF can access the same addresses.
317 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) {
318
319 // MUBUF and MTBUF have vaddr at different indices.
320 if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) ||
321 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) ||
322 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc))
323 return false;
324
325 int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
326 int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
327
328 if (OffIdx0 == -1 || OffIdx1 == -1)
329 return false;
330
331 // getNamedOperandIdx returns the index for MachineInstrs. Since they
332 // include the output in the operand list, but SDNodes don't, we need to
333 // subtract the index by one.
334 OffIdx0 -= get(Opc0).NumDefs;
335 OffIdx1 -= get(Opc1).NumDefs;
336
337 SDValue Off0 = Load0->getOperand(OffIdx0);
338 SDValue Off1 = Load1->getOperand(OffIdx1);
339
340 // The offset might be a FrameIndexSDNode.
341 if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1))
342 return false;
343
344 Offset0 = Off0->getAsZExtVal();
345 Offset1 = Off1->getAsZExtVal();
346 return true;
347 }
348
349 return false;
350}
351
352static bool isStride64(unsigned Opc) {
353 switch (Opc) {
354 case AMDGPU::DS_READ2ST64_B32:
355 case AMDGPU::DS_READ2ST64_B64:
356 case AMDGPU::DS_WRITE2ST64_B32:
357 case AMDGPU::DS_WRITE2ST64_B64:
358 return true;
359 default:
360 return false;
361 }
362}
363
366 int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width,
367 const TargetRegisterInfo *TRI) const {
368 if (!LdSt.mayLoadOrStore())
369 return false;
370
371 unsigned Opc = LdSt.getOpcode();
372 OffsetIsScalable = false;
373 const MachineOperand *BaseOp, *OffsetOp;
374 int DataOpIdx;
375
376 if (isDS(LdSt)) {
377 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr);
378 OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset);
379 if (OffsetOp) {
380 // Normal, single offset LDS instruction.
381 if (!BaseOp) {
382 // DS_CONSUME/DS_APPEND use M0 for the base address.
383 // TODO: find the implicit use operand for M0 and use that as BaseOp?
384 return false;
385 }
386 BaseOps.push_back(BaseOp);
387 Offset = OffsetOp->getImm();
388 // Get appropriate operand, and compute width accordingly.
389 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
390 if (DataOpIdx == -1)
391 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
392 if (Opc == AMDGPU::DS_ATOMIC_ASYNC_BARRIER_ARRIVE_B64)
393 Width = LocationSize::precise(64);
394 else
395 Width = LocationSize::precise(getOpSize(LdSt, DataOpIdx));
396 } else {
397 // The 2 offset instructions use offset0 and offset1 instead. We can treat
398 // these as a load with a single offset if the 2 offsets are consecutive.
399 // We will use this for some partially aligned loads.
400 const MachineOperand *Offset0Op =
401 getNamedOperand(LdSt, AMDGPU::OpName::offset0);
402 const MachineOperand *Offset1Op =
403 getNamedOperand(LdSt, AMDGPU::OpName::offset1);
404
405 unsigned Offset0 = Offset0Op->getImm() & 0xff;
406 unsigned Offset1 = Offset1Op->getImm() & 0xff;
407 if (Offset0 + 1 != Offset1)
408 return false;
409
410 // Each of these offsets is in element sized units, so we need to convert
411 // to bytes of the individual reads.
412
413 unsigned EltSize;
414 if (LdSt.mayLoad())
415 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, 0)) / 16;
416 else {
417 assert(LdSt.mayStore());
418 int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
419 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, Data0Idx)) / 8;
420 }
421
422 if (isStride64(Opc))
423 EltSize *= 64;
424
425 BaseOps.push_back(BaseOp);
426 Offset = EltSize * Offset0;
427 // Get appropriate operand(s), and compute width accordingly.
428 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
429 if (DataOpIdx == -1) {
430 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
431 Width = LocationSize::precise(getOpSize(LdSt, DataOpIdx));
432 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data1);
433 Width = LocationSize::precise(
434 Width.getValue() + TypeSize::getFixed(getOpSize(LdSt, DataOpIdx)));
435 } else {
436 Width = LocationSize::precise(getOpSize(LdSt, DataOpIdx));
437 }
438 }
439 return true;
440 }
441
442 if (isMUBUF(LdSt) || isMTBUF(LdSt)) {
443 const MachineOperand *RSrc = getNamedOperand(LdSt, AMDGPU::OpName::srsrc);
444 if (!RSrc) // e.g. BUFFER_WBINVL1_VOL
445 return false;
446 BaseOps.push_back(RSrc);
447 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr);
448 if (BaseOp && !BaseOp->isFI())
449 BaseOps.push_back(BaseOp);
450 const MachineOperand *OffsetImm =
451 getNamedOperand(LdSt, AMDGPU::OpName::offset);
452 Offset = OffsetImm->getImm();
453 const MachineOperand *SOffset =
454 getNamedOperand(LdSt, AMDGPU::OpName::soffset);
455 if (SOffset) {
456 if (SOffset->isReg())
457 BaseOps.push_back(SOffset);
458 else
459 Offset += SOffset->getImm();
460 }
461 // Get appropriate operand, and compute width accordingly.
462 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
463 if (DataOpIdx == -1)
464 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata);
465 if (DataOpIdx == -1) // LDS DMA
466 return false;
467 Width = LocationSize::precise(getOpSize(LdSt, DataOpIdx));
468 return true;
469 }
470
471 if (isImage(LdSt)) {
472 auto RsrcOpName =
473 isMIMG(LdSt) ? AMDGPU::OpName::srsrc : AMDGPU::OpName::rsrc;
474 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opc, RsrcOpName);
475 BaseOps.push_back(&LdSt.getOperand(SRsrcIdx));
476 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0);
477 if (VAddr0Idx >= 0) {
478 // GFX10 possible NSA encoding.
479 for (int I = VAddr0Idx; I < SRsrcIdx; ++I)
480 BaseOps.push_back(&LdSt.getOperand(I));
481 } else {
482 BaseOps.push_back(getNamedOperand(LdSt, AMDGPU::OpName::vaddr));
483 }
484 Offset = 0;
485 // Get appropriate operand, and compute width accordingly.
486 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata);
487 if (DataOpIdx == -1)
488 return false; // no return sampler
489 Width = LocationSize::precise(getOpSize(LdSt, DataOpIdx));
490 return true;
491 }
492
493 if (isSMRD(LdSt)) {
494 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::sbase);
495 if (!BaseOp) // e.g. S_MEMTIME
496 return false;
497 BaseOps.push_back(BaseOp);
498 OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset);
499 Offset = OffsetOp ? OffsetOp->getImm() : 0;
500 // Get appropriate operand, and compute width accordingly.
501 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::sdst);
502 if (DataOpIdx == -1)
503 return false;
504 Width = LocationSize::precise(getOpSize(LdSt, DataOpIdx));
505 return true;
506 }
507
508 if (isFLAT(LdSt)) {
509 // Instructions have either vaddr or saddr or both or none.
510 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr);
511 if (BaseOp)
512 BaseOps.push_back(BaseOp);
513 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::saddr);
514 if (BaseOp)
515 BaseOps.push_back(BaseOp);
516 Offset = getNamedOperand(LdSt, AMDGPU::OpName::offset)->getImm();
517 // Get appropriate operand, and compute width accordingly.
518 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
519 if (DataOpIdx == -1)
520 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata);
521 if (DataOpIdx == -1) // LDS DMA
522 return false;
523 Width = LocationSize::precise(getOpSize(LdSt, DataOpIdx));
524 return true;
525 }
526
527 return false;
528}
529
530static bool memOpsHaveSameBasePtr(const MachineInstr &MI1,
532 const MachineInstr &MI2,
534 // Only examine the first "base" operand of each instruction, on the
535 // assumption that it represents the real base address of the memory access.
536 // Other operands are typically offsets or indices from this base address.
537 if (BaseOps1.front()->isIdenticalTo(*BaseOps2.front()))
538 return true;
539
540 if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand())
541 return false;
542
543 auto *MO1 = *MI1.memoperands_begin();
544 auto *MO2 = *MI2.memoperands_begin();
545 if (MO1->getAddrSpace() != MO2->getAddrSpace())
546 return false;
547
548 const auto *Base1 = MO1->getValue();
549 const auto *Base2 = MO2->getValue();
550 if (!Base1 || !Base2)
551 return false;
552 Base1 = getUnderlyingObject(Base1);
553 Base2 = getUnderlyingObject(Base2);
554
555 if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2))
556 return false;
557
558 return Base1 == Base2;
559}
560
562 int64_t Offset1, bool OffsetIsScalable1,
564 int64_t Offset2, bool OffsetIsScalable2,
565 unsigned ClusterSize,
566 unsigned NumBytes) const {
567 // If the mem ops (to be clustered) do not have the same base ptr, then they
568 // should not be clustered
569 unsigned MaxMemoryClusterDWords = DefaultMemoryClusterDWordsLimit;
570 if (!BaseOps1.empty() && !BaseOps2.empty()) {
571 const MachineInstr &FirstLdSt = *BaseOps1.front()->getParent();
572 const MachineInstr &SecondLdSt = *BaseOps2.front()->getParent();
573 if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOps1, SecondLdSt, BaseOps2))
574 return false;
575
576 const SIMachineFunctionInfo *MFI =
577 FirstLdSt.getMF()->getInfo<SIMachineFunctionInfo>();
578 MaxMemoryClusterDWords = MFI->getMaxMemoryClusterDWords();
579 } else if (!BaseOps1.empty() || !BaseOps2.empty()) {
580 // If only one base op is empty, they do not have the same base ptr
581 return false;
582 }
583
584 // In order to avoid register pressure, on an average, the number of DWORDS
585 // loaded together by all clustered mem ops should not exceed
586 // MaxMemoryClusterDWords. This is an empirical value based on certain
587 // observations and performance related experiments.
588 // The good thing about this heuristic is - it avoids clustering of too many
589 // sub-word loads, and also avoids clustering of wide loads. Below is the
590 // brief summary of how the heuristic behaves for various `LoadSize` when
591 // MaxMemoryClusterDWords is 8.
592 //
593 // (1) 1 <= LoadSize <= 4: cluster at max 8 mem ops
594 // (2) 5 <= LoadSize <= 8: cluster at max 4 mem ops
595 // (3) 9 <= LoadSize <= 12: cluster at max 2 mem ops
596 // (4) 13 <= LoadSize <= 16: cluster at max 2 mem ops
597 // (5) LoadSize >= 17: do not cluster
598 const unsigned LoadSize = NumBytes / ClusterSize;
599 const unsigned NumDWords = ((LoadSize + 3) / 4) * ClusterSize;
600 return NumDWords <= MaxMemoryClusterDWords;
601}
602
603// FIXME: This behaves strangely. If, for example, you have 32 load + stores,
604// the first 16 loads will be interleaved with the stores, and the next 16 will
605// be clustered as expected. It should really split into 2 16 store batches.
606//
607// Loads are clustered until this returns false, rather than trying to schedule
608// groups of stores. This also means we have to deal with saying different
609// address space loads should be clustered, and ones which might cause bank
610// conflicts.
611//
612// This might be deprecated so it might not be worth that much effort to fix.
614 int64_t Offset0, int64_t Offset1,
615 unsigned NumLoads) const {
616 assert(Offset1 > Offset0 &&
617 "Second offset should be larger than first offset!");
618 // If we have less than 16 loads in a row, and the offsets are within 64
619 // bytes, then schedule together.
620
621 // A cacheline is 64 bytes (for global memory).
622 return (NumLoads <= 16 && (Offset1 - Offset0) < 64);
623}
624
627 const DebugLoc &DL, MCRegister DestReg,
628 MCRegister SrcReg, bool KillSrc,
629 const char *Msg = "illegal VGPR to SGPR copy") {
630 MachineFunction *MF = MBB.getParent();
631
633 C.diagnose(DiagnosticInfoUnsupported(MF->getFunction(), Msg, DL, DS_Error));
634
635 BuildMI(MBB, MI, DL, TII->get(AMDGPU::SI_ILLEGAL_COPY), DestReg)
636 .addReg(SrcReg, getKillRegState(KillSrc));
637}
638
639/// Handle copying from SGPR to AGPR, or from AGPR to AGPR on GFX908. It is not
640/// possible to have a direct copy in these cases on GFX908, so an intermediate
641/// VGPR copy is required.
645 const DebugLoc &DL, MCRegister DestReg,
646 MCRegister SrcReg, bool KillSrc,
647 RegScavenger &RS, bool RegsOverlap,
648 Register ImpDefSuperReg = Register(),
649 Register ImpUseSuperReg = Register()) {
650 assert((TII.getSubtarget().hasMAIInsts() &&
651 !TII.getSubtarget().hasGFX90AInsts()) &&
652 "Expected GFX908 subtarget.");
653
654 assert((AMDGPU::SReg_32RegClass.contains(SrcReg) ||
655 AMDGPU::AGPR_32RegClass.contains(SrcReg)) &&
656 "Source register of the copy should be either an SGPR or an AGPR.");
657
658 assert(AMDGPU::AGPR_32RegClass.contains(DestReg) &&
659 "Destination register of the copy should be an AGPR.");
660
661 const SIRegisterInfo &RI = TII.getRegisterInfo();
662
663 // First try to find defining accvgpr_write to avoid temporary registers.
664 // In the case of copies of overlapping AGPRs, we conservatively do not
665 // reuse previous accvgpr_writes. Otherwise, we may incorrectly pick up
666 // an accvgpr_write used for this same copy due to implicit-defs
667 if (!RegsOverlap) {
668 for (auto Def = MI, E = MBB.begin(); Def != E; ) {
669 --Def;
670
671 if (!Def->modifiesRegister(SrcReg, &RI))
672 continue;
673
674 if (Def->getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64 ||
675 Def->getOperand(0).getReg() != SrcReg)
676 break;
677
678 MachineOperand &DefOp = Def->getOperand(1);
679 assert(DefOp.isReg() || DefOp.isImm());
680
681 if (DefOp.isReg()) {
682 bool SafeToPropagate = true;
683 // Check that register source operand is not clobbered before MI.
684 // Immediate operands are always safe to propagate.
685 for (auto I = Def; I != MI && SafeToPropagate; ++I)
686 if (I->modifiesRegister(DefOp.getReg(), &RI))
687 SafeToPropagate = false;
688
689 if (!SafeToPropagate)
690 break;
691
692 for (auto I = Def; I != MI; ++I)
693 I->clearRegisterKills(DefOp.getReg(), &RI);
694 }
695
696 MachineInstrBuilder Builder =
697 BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg)
698 .add(DefOp);
699 if (ImpDefSuperReg)
700 Builder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit);
701
702 if (ImpUseSuperReg) {
703 Builder.addReg(ImpUseSuperReg,
705 }
706
707 return;
708 }
709 }
710
712 RS.backward(std::next(MI));
713
714 // Ideally we want to have three registers for a long reg_sequence copy
715 // to hide 2 waitstates between v_mov_b32 and accvgpr_write.
716 unsigned MaxVGPRs = RI.getRegPressureLimit(&AMDGPU::VGPR_32RegClass,
717 *MBB.getParent());
718
719 // Registers in the sequence are allocated contiguously so we can just
720 // use register number to pick one of three round-robin temps.
721 unsigned RegNo = (DestReg - AMDGPU::AGPR0) % 3;
722 Register Tmp =
723 MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getVGPRForAGPRCopy();
724 assert(MBB.getParent()->getRegInfo().isReserved(Tmp) &&
725 "VGPR used for an intermediate copy should have been reserved.");
726
727 // Only loop through if there are any free registers left. We don't want to
728 // spill.
729 while (RegNo--) {
730 Register Tmp2 = RS.scavengeRegisterBackwards(AMDGPU::VGPR_32RegClass, MI,
731 /* RestoreAfter */ false, 0,
732 /* AllowSpill */ false);
733 if (!Tmp2 || RI.getHWRegIndex(Tmp2) >= MaxVGPRs)
734 break;
735 Tmp = Tmp2;
736 RS.setRegUsed(Tmp);
737 }
738
739 // Insert copy to temporary VGPR.
740 unsigned TmpCopyOp = AMDGPU::V_MOV_B32_e32;
741 if (AMDGPU::AGPR_32RegClass.contains(SrcReg)) {
742 TmpCopyOp = AMDGPU::V_ACCVGPR_READ_B32_e64;
743 } else {
744 assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
745 }
746
747 MachineInstrBuilder UseBuilder = BuildMI(MBB, MI, DL, TII.get(TmpCopyOp), Tmp)
748 .addReg(SrcReg, getKillRegState(KillSrc));
749 if (ImpUseSuperReg) {
750 UseBuilder.addReg(ImpUseSuperReg,
752 }
753
754 MachineInstrBuilder DefBuilder
755 = BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg)
756 .addReg(Tmp, RegState::Kill);
757
758 if (ImpDefSuperReg)
759 DefBuilder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit);
760}
761
764 MCRegister DestReg, MCRegister SrcReg, bool KillSrc,
765 const TargetRegisterClass *RC, bool Forward) {
766 const SIRegisterInfo &RI = TII.getRegisterInfo();
767 ArrayRef<int16_t> BaseIndices = RI.getRegSplitParts(RC, 4);
769 MachineInstr *FirstMI = nullptr, *LastMI = nullptr;
770
771 for (unsigned Idx = 0; Idx < BaseIndices.size(); ++Idx) {
772 int16_t SubIdx = BaseIndices[Idx];
773 Register DestSubReg = RI.getSubReg(DestReg, SubIdx);
774 Register SrcSubReg = RI.getSubReg(SrcReg, SubIdx);
775 assert(DestSubReg && SrcSubReg && "Failed to find subregs!");
776 unsigned Opcode = AMDGPU::S_MOV_B32;
777
778 // Is SGPR aligned? If so try to combine with next.
779 bool AlignedDest = ((DestSubReg - AMDGPU::SGPR0) % 2) == 0;
780 bool AlignedSrc = ((SrcSubReg - AMDGPU::SGPR0) % 2) == 0;
781 if (AlignedDest && AlignedSrc && (Idx + 1 < BaseIndices.size())) {
782 // Can use SGPR64 copy
783 unsigned Channel = RI.getChannelFromSubReg(SubIdx);
784 SubIdx = RI.getSubRegFromChannel(Channel, 2);
785 DestSubReg = RI.getSubReg(DestReg, SubIdx);
786 SrcSubReg = RI.getSubReg(SrcReg, SubIdx);
787 assert(DestSubReg && SrcSubReg && "Failed to find subregs!");
788 Opcode = AMDGPU::S_MOV_B64;
789 Idx++;
790 }
791
792 LastMI = BuildMI(MBB, I, DL, TII.get(Opcode), DestSubReg)
793 .addReg(SrcSubReg)
794 .addReg(SrcReg, RegState::Implicit);
795
796 if (!FirstMI)
797 FirstMI = LastMI;
798
799 if (!Forward)
800 I--;
801 }
802
803 assert(FirstMI && LastMI);
804 if (!Forward)
805 std::swap(FirstMI, LastMI);
806
807 FirstMI->addOperand(
808 MachineOperand::CreateReg(DestReg, true /*IsDef*/, true /*IsImp*/));
809
810 if (KillSrc)
811 LastMI->addRegisterKilled(SrcReg, &RI);
812}
813
816 const DebugLoc &DL, Register DestReg,
817 Register SrcReg, bool KillSrc, bool RenamableDest,
818 bool RenamableSrc) const {
819 const TargetRegisterClass *RC = RI.getPhysRegBaseClass(DestReg);
820 unsigned Size = RI.getRegSizeInBits(*RC);
821 const TargetRegisterClass *SrcRC = RI.getPhysRegBaseClass(SrcReg);
822 unsigned SrcSize = RI.getRegSizeInBits(*SrcRC);
823
824 // The rest of copyPhysReg assumes Src and Dst size are the same size.
825 // TODO-GFX11_16BIT If all true 16 bit instruction patterns are completed can
826 // we remove Fix16BitCopies and this code block?
827 if (Fix16BitCopies) {
828 if (((Size == 16) != (SrcSize == 16))) {
829 // Non-VGPR Src and Dst will later be expanded back to 32 bits.
830 assert(ST.useRealTrue16Insts());
831 Register &RegToFix = (Size == 32) ? DestReg : SrcReg;
832 MCRegister SubReg = RI.getSubReg(RegToFix, AMDGPU::lo16);
833 RegToFix = SubReg;
834
835 if (DestReg == SrcReg) {
836 // Identity copy. Insert empty bundle since ExpandPostRA expects an
837 // instruction here.
838 BuildMI(MBB, MI, DL, get(AMDGPU::BUNDLE));
839 return;
840 }
841 RC = RI.getPhysRegBaseClass(DestReg);
842 Size = RI.getRegSizeInBits(*RC);
843 SrcRC = RI.getPhysRegBaseClass(SrcReg);
844 SrcSize = RI.getRegSizeInBits(*SrcRC);
845 }
846 }
847
848 if (RC == &AMDGPU::VGPR_32RegClass) {
849 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
850 AMDGPU::SReg_32RegClass.contains(SrcReg) ||
851 AMDGPU::AGPR_32RegClass.contains(SrcReg));
852 unsigned Opc = AMDGPU::AGPR_32RegClass.contains(SrcReg) ?
853 AMDGPU::V_ACCVGPR_READ_B32_e64 : AMDGPU::V_MOV_B32_e32;
854 BuildMI(MBB, MI, DL, get(Opc), DestReg)
855 .addReg(SrcReg, getKillRegState(KillSrc));
856 return;
857 }
858
859 if (RC == &AMDGPU::SReg_32_XM0RegClass ||
860 RC == &AMDGPU::SReg_32RegClass) {
861 if (SrcReg == AMDGPU::SCC) {
862 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B32), DestReg)
863 .addImm(1)
864 .addImm(0);
865 return;
866 }
867
868 if (DestReg == AMDGPU::VCC_LO) {
869 if (AMDGPU::SReg_32RegClass.contains(SrcReg)) {
870 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), AMDGPU::VCC_LO)
871 .addReg(SrcReg, getKillRegState(KillSrc));
872 } else {
873 // FIXME: Hack until VReg_1 removed.
874 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
875 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32))
876 .addImm(0)
877 .addReg(SrcReg, getKillRegState(KillSrc));
878 }
879
880 return;
881 }
882
883 if (!AMDGPU::SReg_32RegClass.contains(SrcReg)) {
884 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
885 return;
886 }
887
888 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
889 .addReg(SrcReg, getKillRegState(KillSrc));
890 return;
891 }
892
893 if (RC == &AMDGPU::SReg_64RegClass) {
894 if (SrcReg == AMDGPU::SCC) {
895 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B64), DestReg)
896 .addImm(1)
897 .addImm(0);
898 return;
899 }
900
901 if (DestReg == AMDGPU::VCC) {
902 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) {
903 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC)
904 .addReg(SrcReg, getKillRegState(KillSrc));
905 } else {
906 // FIXME: Hack until VReg_1 removed.
907 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
908 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32))
909 .addImm(0)
910 .addReg(SrcReg, getKillRegState(KillSrc));
911 }
912
913 return;
914 }
915
916 if (!AMDGPU::SReg_64_EncodableRegClass.contains(SrcReg)) {
917 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
918 return;
919 }
920
921 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
922 .addReg(SrcReg, getKillRegState(KillSrc));
923 return;
924 }
925
926 if (DestReg == AMDGPU::SCC) {
927 // Copying 64-bit or 32-bit sources to SCC barely makes sense,
928 // but SelectionDAG emits such copies for i1 sources.
929 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) {
930 // This copy can only be produced by patterns
931 // with explicit SCC, which are known to be enabled
932 // only for subtargets with S_CMP_LG_U64 present.
933 assert(ST.hasScalarCompareEq64());
934 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U64))
935 .addReg(SrcReg, getKillRegState(KillSrc))
936 .addImm(0);
937 } else {
938 assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
939 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U32))
940 .addReg(SrcReg, getKillRegState(KillSrc))
941 .addImm(0);
942 }
943
944 return;
945 }
946
947 if (RC == &AMDGPU::AGPR_32RegClass) {
948 if (AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
949 (ST.hasGFX90AInsts() && AMDGPU::SReg_32RegClass.contains(SrcReg))) {
950 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg)
951 .addReg(SrcReg, getKillRegState(KillSrc));
952 return;
953 }
954
955 if (AMDGPU::AGPR_32RegClass.contains(SrcReg) && ST.hasGFX90AInsts()) {
956 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_MOV_B32), DestReg)
957 .addReg(SrcReg, getKillRegState(KillSrc));
958 return;
959 }
960
961 // FIXME: Pass should maintain scavenger to avoid scan through the block on
962 // every AGPR spill.
963 RegScavenger RS;
964 const bool Overlap = RI.regsOverlap(SrcReg, DestReg);
965 indirectCopyToAGPR(*this, MBB, MI, DL, DestReg, SrcReg, KillSrc, RS, Overlap);
966 return;
967 }
968
969 if (Size == 16) {
970 assert(AMDGPU::VGPR_16RegClass.contains(SrcReg) ||
971 AMDGPU::SReg_LO16RegClass.contains(SrcReg) ||
972 AMDGPU::AGPR_LO16RegClass.contains(SrcReg));
973
974 bool IsSGPRDst = AMDGPU::SReg_LO16RegClass.contains(DestReg);
975 bool IsSGPRSrc = AMDGPU::SReg_LO16RegClass.contains(SrcReg);
976 bool IsAGPRDst = AMDGPU::AGPR_LO16RegClass.contains(DestReg);
977 bool IsAGPRSrc = AMDGPU::AGPR_LO16RegClass.contains(SrcReg);
978 bool DstLow = !AMDGPU::isHi16Reg(DestReg, RI);
979 bool SrcLow = !AMDGPU::isHi16Reg(SrcReg, RI);
980 MCRegister NewDestReg = RI.get32BitRegister(DestReg);
981 MCRegister NewSrcReg = RI.get32BitRegister(SrcReg);
982
983 if (IsSGPRDst) {
984 if (!IsSGPRSrc) {
985 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
986 return;
987 }
988
989 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), NewDestReg)
990 .addReg(NewSrcReg, getKillRegState(KillSrc));
991 return;
992 }
993
994 if (IsAGPRDst || IsAGPRSrc) {
995 if (!DstLow || !SrcLow) {
996 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc,
997 "Cannot use hi16 subreg with an AGPR!");
998 }
999
1000 copyPhysReg(MBB, MI, DL, NewDestReg, NewSrcReg, KillSrc);
1001 return;
1002 }
1003
1004 if (ST.useRealTrue16Insts()) {
1005 if (IsSGPRSrc) {
1006 assert(SrcLow);
1007 SrcReg = NewSrcReg;
1008 }
1009 // Use the smaller instruction encoding if possible.
1010 if (AMDGPU::VGPR_16_Lo128RegClass.contains(DestReg) &&
1011 (IsSGPRSrc || AMDGPU::VGPR_16_Lo128RegClass.contains(SrcReg))) {
1012 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B16_t16_e32), DestReg)
1013 .addReg(SrcReg);
1014 } else {
1015 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B16_t16_e64), DestReg)
1016 .addImm(0) // src0_modifiers
1017 .addReg(SrcReg)
1018 .addImm(0); // op_sel
1019 }
1020 return;
1021 }
1022
1023 if (IsSGPRSrc && !ST.hasSDWAScalar()) {
1024 if (!DstLow || !SrcLow) {
1025 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc,
1026 "Cannot use hi16 subreg on VI!");
1027 }
1028
1029 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), NewDestReg)
1030 .addReg(NewSrcReg, getKillRegState(KillSrc));
1031 return;
1032 }
1033
1034 auto MIB = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_sdwa), NewDestReg)
1035 .addImm(0) // src0_modifiers
1036 .addReg(NewSrcReg)
1037 .addImm(0) // clamp
1044 // First implicit operand is $exec.
1045 MIB->tieOperands(0, MIB->getNumOperands() - 1);
1046 return;
1047 }
1048
1049 if (RC == RI.getVGPR64Class() && (SrcRC == RC || RI.isSGPRClass(SrcRC))) {
1050 if (ST.hasMovB64()) {
1051 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_e32), DestReg)
1052 .addReg(SrcReg, getKillRegState(KillSrc));
1053 return;
1054 }
1055 if (ST.hasPkMovB32()) {
1056 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DestReg)
1058 .addReg(SrcReg)
1060 .addReg(SrcReg)
1061 .addImm(0) // op_sel_lo
1062 .addImm(0) // op_sel_hi
1063 .addImm(0) // neg_lo
1064 .addImm(0) // neg_hi
1065 .addImm(0) // clamp
1066 .addReg(SrcReg, getKillRegState(KillSrc) | RegState::Implicit);
1067 return;
1068 }
1069 }
1070
1071 const bool Forward = RI.getHWRegIndex(DestReg) <= RI.getHWRegIndex(SrcReg);
1072 if (RI.isSGPRClass(RC)) {
1073 if (!RI.isSGPRClass(SrcRC)) {
1074 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
1075 return;
1076 }
1077 const bool CanKillSuperReg = KillSrc && !RI.regsOverlap(SrcReg, DestReg);
1078 expandSGPRCopy(*this, MBB, MI, DL, DestReg, SrcReg, CanKillSuperReg, RC,
1079 Forward);
1080 return;
1081 }
1082
1083 unsigned EltSize = 4;
1084 unsigned Opcode = AMDGPU::V_MOV_B32_e32;
1085 if (RI.isAGPRClass(RC)) {
1086 if (ST.hasGFX90AInsts() && RI.isAGPRClass(SrcRC))
1087 Opcode = AMDGPU::V_ACCVGPR_MOV_B32;
1088 else if (RI.hasVGPRs(SrcRC) ||
1089 (ST.hasGFX90AInsts() && RI.isSGPRClass(SrcRC)))
1090 Opcode = AMDGPU::V_ACCVGPR_WRITE_B32_e64;
1091 else
1092 Opcode = AMDGPU::INSTRUCTION_LIST_END;
1093 } else if (RI.hasVGPRs(RC) && RI.isAGPRClass(SrcRC)) {
1094 Opcode = AMDGPU::V_ACCVGPR_READ_B32_e64;
1095 } else if ((Size % 64 == 0) && RI.hasVGPRs(RC) &&
1096 (RI.isProperlyAlignedRC(*RC) &&
1097 (SrcRC == RC || RI.isSGPRClass(SrcRC)))) {
1098 // TODO: In 96-bit case, could do a 64-bit mov and then a 32-bit mov.
1099 if (ST.hasMovB64()) {
1100 Opcode = AMDGPU::V_MOV_B64_e32;
1101 EltSize = 8;
1102 } else if (ST.hasPkMovB32()) {
1103 Opcode = AMDGPU::V_PK_MOV_B32;
1104 EltSize = 8;
1105 }
1106 }
1107
1108 // For the cases where we need an intermediate instruction/temporary register
1109 // (destination is an AGPR), we need a scavenger.
1110 //
1111 // FIXME: The pass should maintain this for us so we don't have to re-scan the
1112 // whole block for every handled copy.
1113 std::unique_ptr<RegScavenger> RS;
1114 if (Opcode == AMDGPU::INSTRUCTION_LIST_END)
1115 RS = std::make_unique<RegScavenger>();
1116
1117 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RC, EltSize);
1118
1119 // If there is an overlap, we can't kill the super-register on the last
1120 // instruction, since it will also kill the components made live by this def.
1121 const bool Overlap = RI.regsOverlap(SrcReg, DestReg);
1122 const bool CanKillSuperReg = KillSrc && !Overlap;
1123
1124 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) {
1125 unsigned SubIdx;
1126 if (Forward)
1127 SubIdx = SubIndices[Idx];
1128 else
1129 SubIdx = SubIndices[SubIndices.size() - Idx - 1];
1130 Register DestSubReg = RI.getSubReg(DestReg, SubIdx);
1131 Register SrcSubReg = RI.getSubReg(SrcReg, SubIdx);
1132 assert(DestSubReg && SrcSubReg && "Failed to find subregs!");
1133
1134 bool IsFirstSubreg = Idx == 0;
1135 bool UseKill = CanKillSuperReg && Idx == SubIndices.size() - 1;
1136
1137 if (Opcode == AMDGPU::INSTRUCTION_LIST_END) {
1138 Register ImpDefSuper = IsFirstSubreg ? Register(DestReg) : Register();
1139 Register ImpUseSuper = SrcReg;
1140 indirectCopyToAGPR(*this, MBB, MI, DL, DestSubReg, SrcSubReg, UseKill,
1141 *RS, Overlap, ImpDefSuper, ImpUseSuper);
1142 } else if (Opcode == AMDGPU::V_PK_MOV_B32) {
1144 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DestSubReg)
1146 .addReg(SrcSubReg)
1148 .addReg(SrcSubReg)
1149 .addImm(0) // op_sel_lo
1150 .addImm(0) // op_sel_hi
1151 .addImm(0) // neg_lo
1152 .addImm(0) // neg_hi
1153 .addImm(0) // clamp
1154 .addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit);
1155 if (IsFirstSubreg)
1157 } else {
1158 MachineInstrBuilder Builder =
1159 BuildMI(MBB, MI, DL, get(Opcode), DestSubReg).addReg(SrcSubReg);
1160 if (IsFirstSubreg)
1161 Builder.addReg(DestReg, RegState::Define | RegState::Implicit);
1162
1163 Builder.addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit);
1164 }
1165 }
1166}
1167
1168int SIInstrInfo::commuteOpcode(unsigned Opcode) const {
1169 int NewOpc;
1170
1171 // Try to map original to commuted opcode
1172 NewOpc = AMDGPU::getCommuteRev(Opcode);
1173 if (NewOpc != -1)
1174 // Check if the commuted (REV) opcode exists on the target.
1175 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
1176
1177 // Try to map commuted to original opcode
1178 NewOpc = AMDGPU::getCommuteOrig(Opcode);
1179 if (NewOpc != -1)
1180 // Check if the original (non-REV) opcode exists on the target.
1181 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
1182
1183 return Opcode;
1184}
1185
1186const TargetRegisterClass *
1188 return &AMDGPU::VGPR_32RegClass;
1189}
1190
1193 const DebugLoc &DL, Register DstReg,
1195 Register TrueReg,
1196 Register FalseReg) const {
1197 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1198 const TargetRegisterClass *BoolXExecRC = RI.getWaveMaskRegClass();
1200 assert(MRI.getRegClass(DstReg) == &AMDGPU::VGPR_32RegClass &&
1201 "Not a VGPR32 reg");
1202
1203 if (Cond.size() == 1) {
1204 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1205 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
1206 .add(Cond[0]);
1207 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1208 .addImm(0)
1209 .addReg(FalseReg)
1210 .addImm(0)
1211 .addReg(TrueReg)
1212 .addReg(SReg);
1213 } else if (Cond.size() == 2) {
1214 assert(Cond[0].isImm() && "Cond[0] is not an immediate");
1215 switch (Cond[0].getImm()) {
1216 case SIInstrInfo::SCC_TRUE: {
1217 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1218 BuildMI(MBB, I, DL, get(LMC.CSelectOpc), SReg).addImm(1).addImm(0);
1219 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1220 .addImm(0)
1221 .addReg(FalseReg)
1222 .addImm(0)
1223 .addReg(TrueReg)
1224 .addReg(SReg);
1225 break;
1226 }
1227 case SIInstrInfo::SCC_FALSE: {
1228 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1229 BuildMI(MBB, I, DL, get(LMC.CSelectOpc), SReg).addImm(0).addImm(1);
1230 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1231 .addImm(0)
1232 .addReg(FalseReg)
1233 .addImm(0)
1234 .addReg(TrueReg)
1235 .addReg(SReg);
1236 break;
1237 }
1238 case SIInstrInfo::VCCNZ: {
1239 MachineOperand RegOp = Cond[1];
1240 RegOp.setImplicit(false);
1241 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1242 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
1243 .add(RegOp);
1244 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1245 .addImm(0)
1246 .addReg(FalseReg)
1247 .addImm(0)
1248 .addReg(TrueReg)
1249 .addReg(SReg);
1250 break;
1251 }
1252 case SIInstrInfo::VCCZ: {
1253 MachineOperand RegOp = Cond[1];
1254 RegOp.setImplicit(false);
1255 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1256 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
1257 .add(RegOp);
1258 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1259 .addImm(0)
1260 .addReg(TrueReg)
1261 .addImm(0)
1262 .addReg(FalseReg)
1263 .addReg(SReg);
1264 break;
1265 }
1266 case SIInstrInfo::EXECNZ: {
1267 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1268 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC());
1269 BuildMI(MBB, I, DL, get(LMC.OrSaveExecOpc), SReg2).addImm(0);
1270 BuildMI(MBB, I, DL, get(LMC.CSelectOpc), SReg).addImm(1).addImm(0);
1271 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1272 .addImm(0)
1273 .addReg(FalseReg)
1274 .addImm(0)
1275 .addReg(TrueReg)
1276 .addReg(SReg);
1277 break;
1278 }
1279 case SIInstrInfo::EXECZ: {
1280 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1281 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC());
1282 BuildMI(MBB, I, DL, get(LMC.OrSaveExecOpc), SReg2).addImm(0);
1283 BuildMI(MBB, I, DL, get(LMC.CSelectOpc), SReg).addImm(0).addImm(1);
1284 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1285 .addImm(0)
1286 .addReg(FalseReg)
1287 .addImm(0)
1288 .addReg(TrueReg)
1289 .addReg(SReg);
1290 llvm_unreachable("Unhandled branch predicate EXECZ");
1291 break;
1292 }
1293 default:
1294 llvm_unreachable("invalid branch predicate");
1295 }
1296 } else {
1297 llvm_unreachable("Can only handle Cond size 1 or 2");
1298 }
1299}
1300
1303 const DebugLoc &DL,
1304 Register SrcReg, int Value) const {
1305 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1306 Register Reg = MRI.createVirtualRegister(RI.getBoolRC());
1307 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_EQ_I32_e64), Reg)
1308 .addImm(Value)
1309 .addReg(SrcReg);
1310
1311 return Reg;
1312}
1313
1316 const DebugLoc &DL,
1317 Register SrcReg, int Value) const {
1318 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1319 Register Reg = MRI.createVirtualRegister(RI.getBoolRC());
1320 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_NE_I32_e64), Reg)
1321 .addImm(Value)
1322 .addReg(SrcReg);
1323
1324 return Reg;
1325}
1326
1328 const Register Reg,
1329 int64_t &ImmVal) const {
1330 switch (MI.getOpcode()) {
1331 case AMDGPU::V_MOV_B32_e32:
1332 case AMDGPU::S_MOV_B32:
1333 case AMDGPU::S_MOVK_I32:
1334 case AMDGPU::S_MOV_B64:
1335 case AMDGPU::V_MOV_B64_e32:
1336 case AMDGPU::V_ACCVGPR_WRITE_B32_e64:
1337 case AMDGPU::AV_MOV_B32_IMM_PSEUDO:
1338 case AMDGPU::AV_MOV_B64_IMM_PSEUDO:
1339 case AMDGPU::S_MOV_B64_IMM_PSEUDO:
1340 case AMDGPU::V_MOV_B64_PSEUDO: {
1341 const MachineOperand &Src0 = MI.getOperand(1);
1342 if (Src0.isImm()) {
1343 ImmVal = Src0.getImm();
1344 return MI.getOperand(0).getReg() == Reg;
1345 }
1346
1347 return false;
1348 }
1349 case AMDGPU::S_BREV_B32:
1350 case AMDGPU::V_BFREV_B32_e32:
1351 case AMDGPU::V_BFREV_B32_e64: {
1352 const MachineOperand &Src0 = MI.getOperand(1);
1353 if (Src0.isImm()) {
1354 ImmVal = static_cast<int64_t>(reverseBits<int32_t>(Src0.getImm()));
1355 return MI.getOperand(0).getReg() == Reg;
1356 }
1357
1358 return false;
1359 }
1360 case AMDGPU::S_NOT_B32:
1361 case AMDGPU::V_NOT_B32_e32:
1362 case AMDGPU::V_NOT_B32_e64: {
1363 const MachineOperand &Src0 = MI.getOperand(1);
1364 if (Src0.isImm()) {
1365 ImmVal = static_cast<int64_t>(~static_cast<int32_t>(Src0.getImm()));
1366 return MI.getOperand(0).getReg() == Reg;
1367 }
1368
1369 return false;
1370 }
1371 default:
1372 return false;
1373 }
1374}
1375
1377
1378 if (RI.isAGPRClass(DstRC))
1379 return AMDGPU::COPY;
1380 if (RI.getRegSizeInBits(*DstRC) == 16) {
1381 // Assume hi bits are unneeded. Only _e64 true16 instructions are legal
1382 // before RA.
1383 return RI.isSGPRClass(DstRC) ? AMDGPU::COPY : AMDGPU::V_MOV_B16_t16_e64;
1384 }
1385 if (RI.getRegSizeInBits(*DstRC) == 32)
1386 return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
1387 if (RI.getRegSizeInBits(*DstRC) == 64 && RI.isSGPRClass(DstRC))
1388 return AMDGPU::S_MOV_B64;
1389 if (RI.getRegSizeInBits(*DstRC) == 64 && !RI.isSGPRClass(DstRC))
1390 return AMDGPU::V_MOV_B64_PSEUDO;
1391 return AMDGPU::COPY;
1392}
1393
1394const MCInstrDesc &
1396 bool IsIndirectSrc) const {
1397 if (IsIndirectSrc) {
1398 if (VecSize <= 32) // 4 bytes
1399 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1);
1400 if (VecSize <= 64) // 8 bytes
1401 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2);
1402 if (VecSize <= 96) // 12 bytes
1403 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3);
1404 if (VecSize <= 128) // 16 bytes
1405 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4);
1406 if (VecSize <= 160) // 20 bytes
1407 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5);
1408 if (VecSize <= 256) // 32 bytes
1409 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8);
1410 if (VecSize <= 288) // 36 bytes
1411 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V9);
1412 if (VecSize <= 320) // 40 bytes
1413 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V10);
1414 if (VecSize <= 352) // 44 bytes
1415 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V11);
1416 if (VecSize <= 384) // 48 bytes
1417 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V12);
1418 if (VecSize <= 512) // 64 bytes
1419 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16);
1420 if (VecSize <= 1024) // 128 bytes
1421 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32);
1422
1423 llvm_unreachable("unsupported size for IndirectRegReadGPRIDX pseudos");
1424 }
1425
1426 if (VecSize <= 32) // 4 bytes
1427 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1);
1428 if (VecSize <= 64) // 8 bytes
1429 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2);
1430 if (VecSize <= 96) // 12 bytes
1431 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3);
1432 if (VecSize <= 128) // 16 bytes
1433 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4);
1434 if (VecSize <= 160) // 20 bytes
1435 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5);
1436 if (VecSize <= 256) // 32 bytes
1437 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8);
1438 if (VecSize <= 288) // 36 bytes
1439 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V9);
1440 if (VecSize <= 320) // 40 bytes
1441 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V10);
1442 if (VecSize <= 352) // 44 bytes
1443 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V11);
1444 if (VecSize <= 384) // 48 bytes
1445 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V12);
1446 if (VecSize <= 512) // 64 bytes
1447 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16);
1448 if (VecSize <= 1024) // 128 bytes
1449 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32);
1450
1451 llvm_unreachable("unsupported size for IndirectRegWriteGPRIDX pseudos");
1452}
1453
1454static unsigned getIndirectVGPRWriteMovRelPseudoOpc(unsigned VecSize) {
1455 if (VecSize <= 32) // 4 bytes
1456 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1;
1457 if (VecSize <= 64) // 8 bytes
1458 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2;
1459 if (VecSize <= 96) // 12 bytes
1460 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3;
1461 if (VecSize <= 128) // 16 bytes
1462 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4;
1463 if (VecSize <= 160) // 20 bytes
1464 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5;
1465 if (VecSize <= 256) // 32 bytes
1466 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8;
1467 if (VecSize <= 288) // 36 bytes
1468 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V9;
1469 if (VecSize <= 320) // 40 bytes
1470 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V10;
1471 if (VecSize <= 352) // 44 bytes
1472 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V11;
1473 if (VecSize <= 384) // 48 bytes
1474 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V12;
1475 if (VecSize <= 512) // 64 bytes
1476 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16;
1477 if (VecSize <= 1024) // 128 bytes
1478 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32;
1479
1480 llvm_unreachable("unsupported size for IndirectRegWrite pseudos");
1481}
1482
1483static unsigned getIndirectSGPRWriteMovRelPseudo32(unsigned VecSize) {
1484 if (VecSize <= 32) // 4 bytes
1485 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1;
1486 if (VecSize <= 64) // 8 bytes
1487 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2;
1488 if (VecSize <= 96) // 12 bytes
1489 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3;
1490 if (VecSize <= 128) // 16 bytes
1491 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4;
1492 if (VecSize <= 160) // 20 bytes
1493 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5;
1494 if (VecSize <= 256) // 32 bytes
1495 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8;
1496 if (VecSize <= 288) // 36 bytes
1497 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V9;
1498 if (VecSize <= 320) // 40 bytes
1499 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V10;
1500 if (VecSize <= 352) // 44 bytes
1501 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V11;
1502 if (VecSize <= 384) // 48 bytes
1503 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V12;
1504 if (VecSize <= 512) // 64 bytes
1505 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16;
1506 if (VecSize <= 1024) // 128 bytes
1507 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32;
1508
1509 llvm_unreachable("unsupported size for IndirectRegWrite pseudos");
1510}
1511
1512static unsigned getIndirectSGPRWriteMovRelPseudo64(unsigned VecSize) {
1513 if (VecSize <= 64) // 8 bytes
1514 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1;
1515 if (VecSize <= 128) // 16 bytes
1516 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2;
1517 if (VecSize <= 256) // 32 bytes
1518 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4;
1519 if (VecSize <= 512) // 64 bytes
1520 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8;
1521 if (VecSize <= 1024) // 128 bytes
1522 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16;
1523
1524 llvm_unreachable("unsupported size for IndirectRegWrite pseudos");
1525}
1526
1527const MCInstrDesc &
1528SIInstrInfo::getIndirectRegWriteMovRelPseudo(unsigned VecSize, unsigned EltSize,
1529 bool IsSGPR) const {
1530 if (IsSGPR) {
1531 switch (EltSize) {
1532 case 32:
1533 return get(getIndirectSGPRWriteMovRelPseudo32(VecSize));
1534 case 64:
1535 return get(getIndirectSGPRWriteMovRelPseudo64(VecSize));
1536 default:
1537 llvm_unreachable("invalid reg indexing elt size");
1538 }
1539 }
1540
1541 assert(EltSize == 32 && "invalid reg indexing elt size");
1543}
1544
1545static unsigned getSGPRSpillSaveOpcode(unsigned Size) {
1546 switch (Size) {
1547 case 4:
1548 return AMDGPU::SI_SPILL_S32_SAVE;
1549 case 8:
1550 return AMDGPU::SI_SPILL_S64_SAVE;
1551 case 12:
1552 return AMDGPU::SI_SPILL_S96_SAVE;
1553 case 16:
1554 return AMDGPU::SI_SPILL_S128_SAVE;
1555 case 20:
1556 return AMDGPU::SI_SPILL_S160_SAVE;
1557 case 24:
1558 return AMDGPU::SI_SPILL_S192_SAVE;
1559 case 28:
1560 return AMDGPU::SI_SPILL_S224_SAVE;
1561 case 32:
1562 return AMDGPU::SI_SPILL_S256_SAVE;
1563 case 36:
1564 return AMDGPU::SI_SPILL_S288_SAVE;
1565 case 40:
1566 return AMDGPU::SI_SPILL_S320_SAVE;
1567 case 44:
1568 return AMDGPU::SI_SPILL_S352_SAVE;
1569 case 48:
1570 return AMDGPU::SI_SPILL_S384_SAVE;
1571 case 64:
1572 return AMDGPU::SI_SPILL_S512_SAVE;
1573 case 128:
1574 return AMDGPU::SI_SPILL_S1024_SAVE;
1575 default:
1576 llvm_unreachable("unknown register size");
1577 }
1578}
1579
1580static unsigned getVGPRSpillSaveOpcode(unsigned Size) {
1581 switch (Size) {
1582 case 2:
1583 return AMDGPU::SI_SPILL_V16_SAVE;
1584 case 4:
1585 return AMDGPU::SI_SPILL_V32_SAVE;
1586 case 8:
1587 return AMDGPU::SI_SPILL_V64_SAVE;
1588 case 12:
1589 return AMDGPU::SI_SPILL_V96_SAVE;
1590 case 16:
1591 return AMDGPU::SI_SPILL_V128_SAVE;
1592 case 20:
1593 return AMDGPU::SI_SPILL_V160_SAVE;
1594 case 24:
1595 return AMDGPU::SI_SPILL_V192_SAVE;
1596 case 28:
1597 return AMDGPU::SI_SPILL_V224_SAVE;
1598 case 32:
1599 return AMDGPU::SI_SPILL_V256_SAVE;
1600 case 36:
1601 return AMDGPU::SI_SPILL_V288_SAVE;
1602 case 40:
1603 return AMDGPU::SI_SPILL_V320_SAVE;
1604 case 44:
1605 return AMDGPU::SI_SPILL_V352_SAVE;
1606 case 48:
1607 return AMDGPU::SI_SPILL_V384_SAVE;
1608 case 64:
1609 return AMDGPU::SI_SPILL_V512_SAVE;
1610 case 128:
1611 return AMDGPU::SI_SPILL_V1024_SAVE;
1612 default:
1613 llvm_unreachable("unknown register size");
1614 }
1615}
1616
1617static unsigned getAVSpillSaveOpcode(unsigned Size) {
1618 switch (Size) {
1619 case 4:
1620 return AMDGPU::SI_SPILL_AV32_SAVE;
1621 case 8:
1622 return AMDGPU::SI_SPILL_AV64_SAVE;
1623 case 12:
1624 return AMDGPU::SI_SPILL_AV96_SAVE;
1625 case 16:
1626 return AMDGPU::SI_SPILL_AV128_SAVE;
1627 case 20:
1628 return AMDGPU::SI_SPILL_AV160_SAVE;
1629 case 24:
1630 return AMDGPU::SI_SPILL_AV192_SAVE;
1631 case 28:
1632 return AMDGPU::SI_SPILL_AV224_SAVE;
1633 case 32:
1634 return AMDGPU::SI_SPILL_AV256_SAVE;
1635 case 36:
1636 return AMDGPU::SI_SPILL_AV288_SAVE;
1637 case 40:
1638 return AMDGPU::SI_SPILL_AV320_SAVE;
1639 case 44:
1640 return AMDGPU::SI_SPILL_AV352_SAVE;
1641 case 48:
1642 return AMDGPU::SI_SPILL_AV384_SAVE;
1643 case 64:
1644 return AMDGPU::SI_SPILL_AV512_SAVE;
1645 case 128:
1646 return AMDGPU::SI_SPILL_AV1024_SAVE;
1647 default:
1648 llvm_unreachable("unknown register size");
1649 }
1650}
1651
1652static unsigned getWWMRegSpillSaveOpcode(unsigned Size,
1653 bool IsVectorSuperClass) {
1654 // Currently, there is only 32-bit WWM register spills needed.
1655 if (Size != 4)
1656 llvm_unreachable("unknown wwm register spill size");
1657
1658 if (IsVectorSuperClass)
1659 return AMDGPU::SI_SPILL_WWM_AV32_SAVE;
1660
1661 return AMDGPU::SI_SPILL_WWM_V32_SAVE;
1662}
1663
1665 Register Reg, const TargetRegisterClass *RC, unsigned Size,
1666 const SIMachineFunctionInfo &MFI) const {
1667 bool IsVectorSuperClass = RI.isVectorSuperClass(RC);
1668
1669 // Choose the right opcode if spilling a WWM register.
1671 return getWWMRegSpillSaveOpcode(Size, IsVectorSuperClass);
1672
1673 // TODO: Check if AGPRs are available
1674 if (ST.hasMAIInsts())
1675 return getAVSpillSaveOpcode(Size);
1676
1678}
1679
1682 bool isKill, int FrameIndex, const TargetRegisterClass *RC,
1683 const TargetRegisterInfo *TRI, Register VReg,
1684 MachineInstr::MIFlag Flags) const {
1685 MachineFunction *MF = MBB.getParent();
1687 MachineFrameInfo &FrameInfo = MF->getFrameInfo();
1688 const DebugLoc &DL = MBB.findDebugLoc(MI);
1689
1690 MachinePointerInfo PtrInfo
1691 = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
1693 PtrInfo, MachineMemOperand::MOStore, FrameInfo.getObjectSize(FrameIndex),
1694 FrameInfo.getObjectAlign(FrameIndex));
1695 unsigned SpillSize = TRI->getSpillSize(*RC);
1696
1698 if (RI.isSGPRClass(RC)) {
1699 MFI->setHasSpilledSGPRs();
1700 assert(SrcReg != AMDGPU::M0 && "m0 should not be spilled");
1701 assert(SrcReg != AMDGPU::EXEC_LO && SrcReg != AMDGPU::EXEC_HI &&
1702 SrcReg != AMDGPU::EXEC && "exec should not be spilled");
1703
1704 // We are only allowed to create one new instruction when spilling
1705 // registers, so we need to use pseudo instruction for spilling SGPRs.
1706 const MCInstrDesc &OpDesc = get(getSGPRSpillSaveOpcode(SpillSize));
1707
1708 // The SGPR spill/restore instructions only work on number sgprs, so we need
1709 // to make sure we are using the correct register class.
1710 if (SrcReg.isVirtual() && SpillSize == 4) {
1711 MRI.constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0_XEXECRegClass);
1712 }
1713
1714 BuildMI(MBB, MI, DL, OpDesc)
1715 .addReg(SrcReg, getKillRegState(isKill)) // data
1716 .addFrameIndex(FrameIndex) // addr
1717 .addMemOperand(MMO)
1719
1720 if (RI.spillSGPRToVGPR())
1721 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill);
1722 return;
1723 }
1724
1725 unsigned Opcode =
1726 getVectorRegSpillSaveOpcode(VReg ? VReg : SrcReg, RC, SpillSize, *MFI);
1727 MFI->setHasSpilledVGPRs();
1728
1729 BuildMI(MBB, MI, DL, get(Opcode))
1730 .addReg(SrcReg, getKillRegState(isKill)) // data
1731 .addFrameIndex(FrameIndex) // addr
1732 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset
1733 .addImm(0) // offset
1734 .addMemOperand(MMO);
1735}
1736
1737static unsigned getSGPRSpillRestoreOpcode(unsigned Size) {
1738 switch (Size) {
1739 case 4:
1740 return AMDGPU::SI_SPILL_S32_RESTORE;
1741 case 8:
1742 return AMDGPU::SI_SPILL_S64_RESTORE;
1743 case 12:
1744 return AMDGPU::SI_SPILL_S96_RESTORE;
1745 case 16:
1746 return AMDGPU::SI_SPILL_S128_RESTORE;
1747 case 20:
1748 return AMDGPU::SI_SPILL_S160_RESTORE;
1749 case 24:
1750 return AMDGPU::SI_SPILL_S192_RESTORE;
1751 case 28:
1752 return AMDGPU::SI_SPILL_S224_RESTORE;
1753 case 32:
1754 return AMDGPU::SI_SPILL_S256_RESTORE;
1755 case 36:
1756 return AMDGPU::SI_SPILL_S288_RESTORE;
1757 case 40:
1758 return AMDGPU::SI_SPILL_S320_RESTORE;
1759 case 44:
1760 return AMDGPU::SI_SPILL_S352_RESTORE;
1761 case 48:
1762 return AMDGPU::SI_SPILL_S384_RESTORE;
1763 case 64:
1764 return AMDGPU::SI_SPILL_S512_RESTORE;
1765 case 128:
1766 return AMDGPU::SI_SPILL_S1024_RESTORE;
1767 default:
1768 llvm_unreachable("unknown register size");
1769 }
1770}
1771
1772static unsigned getVGPRSpillRestoreOpcode(unsigned Size) {
1773 switch (Size) {
1774 case 2:
1775 return AMDGPU::SI_SPILL_V16_RESTORE;
1776 case 4:
1777 return AMDGPU::SI_SPILL_V32_RESTORE;
1778 case 8:
1779 return AMDGPU::SI_SPILL_V64_RESTORE;
1780 case 12:
1781 return AMDGPU::SI_SPILL_V96_RESTORE;
1782 case 16:
1783 return AMDGPU::SI_SPILL_V128_RESTORE;
1784 case 20:
1785 return AMDGPU::SI_SPILL_V160_RESTORE;
1786 case 24:
1787 return AMDGPU::SI_SPILL_V192_RESTORE;
1788 case 28:
1789 return AMDGPU::SI_SPILL_V224_RESTORE;
1790 case 32:
1791 return AMDGPU::SI_SPILL_V256_RESTORE;
1792 case 36:
1793 return AMDGPU::SI_SPILL_V288_RESTORE;
1794 case 40:
1795 return AMDGPU::SI_SPILL_V320_RESTORE;
1796 case 44:
1797 return AMDGPU::SI_SPILL_V352_RESTORE;
1798 case 48:
1799 return AMDGPU::SI_SPILL_V384_RESTORE;
1800 case 64:
1801 return AMDGPU::SI_SPILL_V512_RESTORE;
1802 case 128:
1803 return AMDGPU::SI_SPILL_V1024_RESTORE;
1804 default:
1805 llvm_unreachable("unknown register size");
1806 }
1807}
1808
1809static unsigned getAVSpillRestoreOpcode(unsigned Size) {
1810 switch (Size) {
1811 case 4:
1812 return AMDGPU::SI_SPILL_AV32_RESTORE;
1813 case 8:
1814 return AMDGPU::SI_SPILL_AV64_RESTORE;
1815 case 12:
1816 return AMDGPU::SI_SPILL_AV96_RESTORE;
1817 case 16:
1818 return AMDGPU::SI_SPILL_AV128_RESTORE;
1819 case 20:
1820 return AMDGPU::SI_SPILL_AV160_RESTORE;
1821 case 24:
1822 return AMDGPU::SI_SPILL_AV192_RESTORE;
1823 case 28:
1824 return AMDGPU::SI_SPILL_AV224_RESTORE;
1825 case 32:
1826 return AMDGPU::SI_SPILL_AV256_RESTORE;
1827 case 36:
1828 return AMDGPU::SI_SPILL_AV288_RESTORE;
1829 case 40:
1830 return AMDGPU::SI_SPILL_AV320_RESTORE;
1831 case 44:
1832 return AMDGPU::SI_SPILL_AV352_RESTORE;
1833 case 48:
1834 return AMDGPU::SI_SPILL_AV384_RESTORE;
1835 case 64:
1836 return AMDGPU::SI_SPILL_AV512_RESTORE;
1837 case 128:
1838 return AMDGPU::SI_SPILL_AV1024_RESTORE;
1839 default:
1840 llvm_unreachable("unknown register size");
1841 }
1842}
1843
1844static unsigned getWWMRegSpillRestoreOpcode(unsigned Size,
1845 bool IsVectorSuperClass) {
1846 // Currently, there is only 32-bit WWM register spills needed.
1847 if (Size != 4)
1848 llvm_unreachable("unknown wwm register spill size");
1849
1850 if (IsVectorSuperClass) // TODO: Always use this if there are AGPRs
1851 return AMDGPU::SI_SPILL_WWM_AV32_RESTORE;
1852
1853 return AMDGPU::SI_SPILL_WWM_V32_RESTORE;
1854}
1855
1857 Register Reg, const TargetRegisterClass *RC, unsigned Size,
1858 const SIMachineFunctionInfo &MFI) const {
1859 bool IsVectorSuperClass = RI.isVectorSuperClass(RC);
1860
1861 // Choose the right opcode if restoring a WWM register.
1863 return getWWMRegSpillRestoreOpcode(Size, IsVectorSuperClass);
1864
1865 // TODO: Check if AGPRs are available
1866 if (ST.hasMAIInsts())
1868
1869 assert(!RI.isAGPRClass(RC));
1871}
1872
1875 Register DestReg, int FrameIndex,
1876 const TargetRegisterClass *RC,
1877 const TargetRegisterInfo *TRI,
1878 Register VReg,
1879 MachineInstr::MIFlag Flags) const {
1880 MachineFunction *MF = MBB.getParent();
1882 MachineFrameInfo &FrameInfo = MF->getFrameInfo();
1883 const DebugLoc &DL = MBB.findDebugLoc(MI);
1884 unsigned SpillSize = TRI->getSpillSize(*RC);
1885
1886 MachinePointerInfo PtrInfo
1887 = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
1888
1890 PtrInfo, MachineMemOperand::MOLoad, FrameInfo.getObjectSize(FrameIndex),
1891 FrameInfo.getObjectAlign(FrameIndex));
1892
1893 if (RI.isSGPRClass(RC)) {
1894 MFI->setHasSpilledSGPRs();
1895 assert(DestReg != AMDGPU::M0 && "m0 should not be reloaded into");
1896 assert(DestReg != AMDGPU::EXEC_LO && DestReg != AMDGPU::EXEC_HI &&
1897 DestReg != AMDGPU::EXEC && "exec should not be spilled");
1898
1899 // FIXME: Maybe this should not include a memoperand because it will be
1900 // lowered to non-memory instructions.
1901 const MCInstrDesc &OpDesc = get(getSGPRSpillRestoreOpcode(SpillSize));
1902 if (DestReg.isVirtual() && SpillSize == 4) {
1904 MRI.constrainRegClass(DestReg, &AMDGPU::SReg_32_XM0_XEXECRegClass);
1905 }
1906
1907 if (RI.spillSGPRToVGPR())
1908 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill);
1909 BuildMI(MBB, MI, DL, OpDesc, DestReg)
1910 .addFrameIndex(FrameIndex) // addr
1911 .addMemOperand(MMO)
1913
1914 return;
1915 }
1916
1917 unsigned Opcode = getVectorRegSpillRestoreOpcode(VReg ? VReg : DestReg, RC,
1918 SpillSize, *MFI);
1919 BuildMI(MBB, MI, DL, get(Opcode), DestReg)
1920 .addFrameIndex(FrameIndex) // vaddr
1921 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset
1922 .addImm(0) // offset
1923 .addMemOperand(MMO);
1924}
1925
1930
1933 unsigned Quantity) const {
1934 DebugLoc DL = MBB.findDebugLoc(MI);
1935 unsigned MaxSNopCount = 1u << ST.getSNopBits();
1936 while (Quantity > 0) {
1937 unsigned Arg = std::min(Quantity, MaxSNopCount);
1938 Quantity -= Arg;
1939 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOP)).addImm(Arg - 1);
1940 }
1941}
1942
1944 auto *MF = MBB.getParent();
1945 SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>();
1946
1947 assert(Info->isEntryFunction());
1948
1949 if (MBB.succ_empty()) {
1950 bool HasNoTerminator = MBB.getFirstTerminator() == MBB.end();
1951 if (HasNoTerminator) {
1952 if (Info->returnsVoid()) {
1953 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::S_ENDPGM)).addImm(0);
1954 } else {
1955 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::SI_RETURN_TO_EPILOG));
1956 }
1957 }
1958 }
1959}
1960
1964 const DebugLoc &DL) const {
1965 MachineFunction *MF = MBB.getParent();
1966 constexpr unsigned DoorbellIDMask = 0x3ff;
1967 constexpr unsigned ECQueueWaveAbort = 0x400;
1968
1969 MachineBasicBlock *TrapBB = &MBB;
1970 MachineBasicBlock *ContBB = &MBB;
1971 MachineBasicBlock *HaltLoopBB = MF->CreateMachineBasicBlock();
1972
1973 if (!MBB.succ_empty() || std::next(MI.getIterator()) != MBB.end()) {
1974 ContBB = MBB.splitAt(MI, /*UpdateLiveIns=*/false);
1975 TrapBB = MF->CreateMachineBasicBlock();
1976 BuildMI(MBB, MI, DL, get(AMDGPU::S_CBRANCH_EXECNZ)).addMBB(TrapBB);
1977 MF->push_back(TrapBB);
1978 MBB.addSuccessor(TrapBB);
1979 }
1980
1981 // Start with a `s_trap 2`, if we're in PRIV=1 and we need the workaround this
1982 // will be a nop.
1983 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_TRAP))
1984 .addImm(static_cast<unsigned>(GCNSubtarget::TrapID::LLVMAMDHSATrap));
1985 Register DoorbellReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
1986 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_SENDMSG_RTN_B32),
1987 DoorbellReg)
1989 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_MOV_B32), AMDGPU::TTMP2)
1990 .addUse(AMDGPU::M0);
1991 Register DoorbellRegMasked =
1992 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
1993 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_AND_B32), DoorbellRegMasked)
1994 .addUse(DoorbellReg)
1995 .addImm(DoorbellIDMask);
1996 Register SetWaveAbortBit =
1997 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
1998 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_OR_B32), SetWaveAbortBit)
1999 .addUse(DoorbellRegMasked)
2000 .addImm(ECQueueWaveAbort);
2001 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_MOV_B32), AMDGPU::M0)
2002 .addUse(SetWaveAbortBit);
2003 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_SENDMSG))
2005 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_MOV_B32), AMDGPU::M0)
2006 .addUse(AMDGPU::TTMP2);
2007 BuildMI(*TrapBB, TrapBB->end(), DL, get(AMDGPU::S_BRANCH)).addMBB(HaltLoopBB);
2008 TrapBB->addSuccessor(HaltLoopBB);
2009
2010 BuildMI(*HaltLoopBB, HaltLoopBB->end(), DL, get(AMDGPU::S_SETHALT)).addImm(5);
2011 BuildMI(*HaltLoopBB, HaltLoopBB->end(), DL, get(AMDGPU::S_BRANCH))
2012 .addMBB(HaltLoopBB);
2013 MF->push_back(HaltLoopBB);
2014 HaltLoopBB->addSuccessor(HaltLoopBB);
2015
2016 return ContBB;
2017}
2018
2020 switch (MI.getOpcode()) {
2021 default:
2022 if (MI.isMetaInstruction())
2023 return 0;
2024 return 1; // FIXME: Do wait states equal cycles?
2025
2026 case AMDGPU::S_NOP:
2027 return MI.getOperand(0).getImm() + 1;
2028 // SI_RETURN_TO_EPILOG is a fallthrough to code outside of the function. The
2029 // hazard, even if one exist, won't really be visible. Should we handle it?
2030 }
2031}
2032
2034 MachineBasicBlock &MBB = *MI.getParent();
2035 DebugLoc DL = MBB.findDebugLoc(MI);
2037 switch (MI.getOpcode()) {
2038 default: return TargetInstrInfo::expandPostRAPseudo(MI);
2039 case AMDGPU::S_MOV_B64_term:
2040 // This is only a terminator to get the correct spill code placement during
2041 // register allocation.
2042 MI.setDesc(get(AMDGPU::S_MOV_B64));
2043 break;
2044
2045 case AMDGPU::S_MOV_B32_term:
2046 // This is only a terminator to get the correct spill code placement during
2047 // register allocation.
2048 MI.setDesc(get(AMDGPU::S_MOV_B32));
2049 break;
2050
2051 case AMDGPU::S_XOR_B64_term:
2052 // This is only a terminator to get the correct spill code placement during
2053 // register allocation.
2054 MI.setDesc(get(AMDGPU::S_XOR_B64));
2055 break;
2056
2057 case AMDGPU::S_XOR_B32_term:
2058 // This is only a terminator to get the correct spill code placement during
2059 // register allocation.
2060 MI.setDesc(get(AMDGPU::S_XOR_B32));
2061 break;
2062 case AMDGPU::S_OR_B64_term:
2063 // This is only a terminator to get the correct spill code placement during
2064 // register allocation.
2065 MI.setDesc(get(AMDGPU::S_OR_B64));
2066 break;
2067 case AMDGPU::S_OR_B32_term:
2068 // This is only a terminator to get the correct spill code placement during
2069 // register allocation.
2070 MI.setDesc(get(AMDGPU::S_OR_B32));
2071 break;
2072
2073 case AMDGPU::S_ANDN2_B64_term:
2074 // This is only a terminator to get the correct spill code placement during
2075 // register allocation.
2076 MI.setDesc(get(AMDGPU::S_ANDN2_B64));
2077 break;
2078
2079 case AMDGPU::S_ANDN2_B32_term:
2080 // This is only a terminator to get the correct spill code placement during
2081 // register allocation.
2082 MI.setDesc(get(AMDGPU::S_ANDN2_B32));
2083 break;
2084
2085 case AMDGPU::S_AND_B64_term:
2086 // This is only a terminator to get the correct spill code placement during
2087 // register allocation.
2088 MI.setDesc(get(AMDGPU::S_AND_B64));
2089 break;
2090
2091 case AMDGPU::S_AND_B32_term:
2092 // This is only a terminator to get the correct spill code placement during
2093 // register allocation.
2094 MI.setDesc(get(AMDGPU::S_AND_B32));
2095 break;
2096
2097 case AMDGPU::S_AND_SAVEEXEC_B64_term:
2098 // This is only a terminator to get the correct spill code placement during
2099 // register allocation.
2100 MI.setDesc(get(AMDGPU::S_AND_SAVEEXEC_B64));
2101 break;
2102
2103 case AMDGPU::S_AND_SAVEEXEC_B32_term:
2104 // This is only a terminator to get the correct spill code placement during
2105 // register allocation.
2106 MI.setDesc(get(AMDGPU::S_AND_SAVEEXEC_B32));
2107 break;
2108
2109 case AMDGPU::SI_SPILL_S32_TO_VGPR:
2110 MI.setDesc(get(AMDGPU::V_WRITELANE_B32));
2111 break;
2112
2113 case AMDGPU::SI_RESTORE_S32_FROM_VGPR:
2114 MI.setDesc(get(AMDGPU::V_READLANE_B32));
2115 MI.getMF()->getRegInfo().constrainRegClass(MI.getOperand(0).getReg(),
2116 &AMDGPU::SReg_32_XM0RegClass);
2117 break;
2118 case AMDGPU::AV_MOV_B32_IMM_PSEUDO: {
2119 Register Dst = MI.getOperand(0).getReg();
2120 bool IsAGPR = SIRegisterInfo::isAGPRClass(RI.getPhysRegBaseClass(Dst));
2121 MI.setDesc(
2122 get(IsAGPR ? AMDGPU::V_ACCVGPR_WRITE_B32_e64 : AMDGPU::V_MOV_B32_e32));
2123 break;
2124 }
2125 case AMDGPU::AV_MOV_B64_IMM_PSEUDO: {
2126 Register Dst = MI.getOperand(0).getReg();
2127 if (SIRegisterInfo::isAGPRClass(RI.getPhysRegBaseClass(Dst))) {
2128 int64_t Imm = MI.getOperand(1).getImm();
2129
2130 Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
2131 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
2132 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DstLo)
2135 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DstHi)
2136 .addImm(SignExtend64<32>(Imm >> 32))
2138 MI.eraseFromParent();
2139 break;
2140 }
2141
2142 [[fallthrough]];
2143 }
2144 case AMDGPU::V_MOV_B64_PSEUDO: {
2145 Register Dst = MI.getOperand(0).getReg();
2146 Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
2147 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
2148
2149 const MachineOperand &SrcOp = MI.getOperand(1);
2150 // FIXME: Will this work for 64-bit floating point immediates?
2151 assert(!SrcOp.isFPImm());
2152 if (ST.hasMovB64()) {
2153 MI.setDesc(get(AMDGPU::V_MOV_B64_e32));
2154 if (SrcOp.isReg() || isInlineConstant(MI, 1) ||
2155 isUInt<32>(SrcOp.getImm()) || ST.has64BitLiterals())
2156 break;
2157 }
2158 if (SrcOp.isImm()) {
2159 APInt Imm(64, SrcOp.getImm());
2160 APInt Lo(32, Imm.getLoBits(32).getZExtValue());
2161 APInt Hi(32, Imm.getHiBits(32).getZExtValue());
2162 if (ST.hasPkMovB32() && Lo == Hi && isInlineConstant(Lo)) {
2163 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst)
2165 .addImm(Lo.getSExtValue())
2167 .addImm(Lo.getSExtValue())
2168 .addImm(0) // op_sel_lo
2169 .addImm(0) // op_sel_hi
2170 .addImm(0) // neg_lo
2171 .addImm(0) // neg_hi
2172 .addImm(0); // clamp
2173 } else {
2174 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
2175 .addImm(Lo.getSExtValue())
2177 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
2178 .addImm(Hi.getSExtValue())
2180 }
2181 } else {
2182 assert(SrcOp.isReg());
2183 if (ST.hasPkMovB32() &&
2184 !RI.isAGPR(MBB.getParent()->getRegInfo(), SrcOp.getReg())) {
2185 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst)
2186 .addImm(SISrcMods::OP_SEL_1) // src0_mod
2187 .addReg(SrcOp.getReg())
2189 .addReg(SrcOp.getReg())
2190 .addImm(0) // op_sel_lo
2191 .addImm(0) // op_sel_hi
2192 .addImm(0) // neg_lo
2193 .addImm(0) // neg_hi
2194 .addImm(0); // clamp
2195 } else {
2196 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
2197 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0))
2199 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
2200 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1))
2202 }
2203 }
2204 MI.eraseFromParent();
2205 break;
2206 }
2207 case AMDGPU::V_MOV_B64_DPP_PSEUDO: {
2209 break;
2210 }
2211 case AMDGPU::S_MOV_B64_IMM_PSEUDO: {
2212 const MachineOperand &SrcOp = MI.getOperand(1);
2213 assert(!SrcOp.isFPImm());
2214
2215 if (ST.has64BitLiterals()) {
2216 MI.setDesc(get(AMDGPU::S_MOV_B64));
2217 break;
2218 }
2219
2220 APInt Imm(64, SrcOp.getImm());
2221 if (Imm.isIntN(32) || isInlineConstant(Imm)) {
2222 MI.setDesc(get(AMDGPU::S_MOV_B64));
2223 break;
2224 }
2225
2226 Register Dst = MI.getOperand(0).getReg();
2227 Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
2228 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
2229
2230 APInt Lo(32, Imm.getLoBits(32).getZExtValue());
2231 APInt Hi(32, Imm.getHiBits(32).getZExtValue());
2232 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DstLo)
2233 .addImm(Lo.getSExtValue())
2235 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DstHi)
2236 .addImm(Hi.getSExtValue())
2238 MI.eraseFromParent();
2239 break;
2240 }
2241 case AMDGPU::V_SET_INACTIVE_B32: {
2242 // Lower V_SET_INACTIVE_B32 to V_CNDMASK_B32.
2243 Register DstReg = MI.getOperand(0).getReg();
2244 BuildMI(MBB, MI, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
2245 .add(MI.getOperand(3))
2246 .add(MI.getOperand(4))
2247 .add(MI.getOperand(1))
2248 .add(MI.getOperand(2))
2249 .add(MI.getOperand(5));
2250 MI.eraseFromParent();
2251 break;
2252 }
2253 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1:
2254 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2:
2255 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3:
2256 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4:
2257 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5:
2258 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8:
2259 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V9:
2260 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V10:
2261 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V11:
2262 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V12:
2263 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16:
2264 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32:
2265 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1:
2266 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2:
2267 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3:
2268 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4:
2269 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5:
2270 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8:
2271 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V9:
2272 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V10:
2273 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V11:
2274 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V12:
2275 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16:
2276 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32:
2277 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1:
2278 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2:
2279 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4:
2280 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8:
2281 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16: {
2282 const TargetRegisterClass *EltRC = getOpRegClass(MI, 2);
2283
2284 unsigned Opc;
2285 if (RI.hasVGPRs(EltRC)) {
2286 Opc = AMDGPU::V_MOVRELD_B32_e32;
2287 } else {
2288 Opc = RI.getRegSizeInBits(*EltRC) == 64 ? AMDGPU::S_MOVRELD_B64
2289 : AMDGPU::S_MOVRELD_B32;
2290 }
2291
2292 const MCInstrDesc &OpDesc = get(Opc);
2293 Register VecReg = MI.getOperand(0).getReg();
2294 bool IsUndef = MI.getOperand(1).isUndef();
2295 unsigned SubReg = MI.getOperand(3).getImm();
2296 assert(VecReg == MI.getOperand(1).getReg());
2297
2299 BuildMI(MBB, MI, DL, OpDesc)
2300 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef)
2301 .add(MI.getOperand(2))
2303 .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0));
2304
2305 const int ImpDefIdx =
2306 OpDesc.getNumOperands() + OpDesc.implicit_uses().size();
2307 const int ImpUseIdx = ImpDefIdx + 1;
2308 MIB->tieOperands(ImpDefIdx, ImpUseIdx);
2309 MI.eraseFromParent();
2310 break;
2311 }
2312 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1:
2313 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2:
2314 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3:
2315 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4:
2316 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5:
2317 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8:
2318 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V9:
2319 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V10:
2320 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V11:
2321 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V12:
2322 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16:
2323 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32: {
2324 assert(ST.useVGPRIndexMode());
2325 Register VecReg = MI.getOperand(0).getReg();
2326 bool IsUndef = MI.getOperand(1).isUndef();
2327 MachineOperand &Idx = MI.getOperand(3);
2328 Register SubReg = MI.getOperand(4).getImm();
2329
2330 MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON))
2331 .add(Idx)
2333 SetOn->getOperand(3).setIsUndef();
2334
2335 const MCInstrDesc &OpDesc = get(AMDGPU::V_MOV_B32_indirect_write);
2337 BuildMI(MBB, MI, DL, OpDesc)
2338 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef)
2339 .add(MI.getOperand(2))
2341 .addReg(VecReg,
2342 RegState::Implicit | (IsUndef ? RegState::Undef : 0));
2343
2344 const int ImpDefIdx =
2345 OpDesc.getNumOperands() + OpDesc.implicit_uses().size();
2346 const int ImpUseIdx = ImpDefIdx + 1;
2347 MIB->tieOperands(ImpDefIdx, ImpUseIdx);
2348
2349 MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF));
2350
2351 finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator()));
2352
2353 MI.eraseFromParent();
2354 break;
2355 }
2356 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1:
2357 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2:
2358 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3:
2359 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4:
2360 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5:
2361 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8:
2362 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V9:
2363 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V10:
2364 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V11:
2365 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V12:
2366 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16:
2367 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32: {
2368 assert(ST.useVGPRIndexMode());
2369 Register Dst = MI.getOperand(0).getReg();
2370 Register VecReg = MI.getOperand(1).getReg();
2371 bool IsUndef = MI.getOperand(1).isUndef();
2372 Register Idx = MI.getOperand(2).getReg();
2373 Register SubReg = MI.getOperand(3).getImm();
2374
2375 MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON))
2376 .addReg(Idx)
2378 SetOn->getOperand(3).setIsUndef();
2379
2380 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_indirect_read))
2381 .addDef(Dst)
2382 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef)
2383 .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0));
2384
2385 MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF));
2386
2387 finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator()));
2388
2389 MI.eraseFromParent();
2390 break;
2391 }
2392 case AMDGPU::SI_PC_ADD_REL_OFFSET: {
2393 MachineFunction &MF = *MBB.getParent();
2394 Register Reg = MI.getOperand(0).getReg();
2395 Register RegLo = RI.getSubReg(Reg, AMDGPU::sub0);
2396 Register RegHi = RI.getSubReg(Reg, AMDGPU::sub1);
2397 MachineOperand OpLo = MI.getOperand(1);
2398 MachineOperand OpHi = MI.getOperand(2);
2399
2400 // Create a bundle so these instructions won't be re-ordered by the
2401 // post-RA scheduler.
2402 MIBundleBuilder Bundler(MBB, MI);
2403 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg));
2404
2405 // What we want here is an offset from the value returned by s_getpc (which
2406 // is the address of the s_add_u32 instruction) to the global variable, but
2407 // since the encoding of $symbol starts 4 bytes after the start of the
2408 // s_add_u32 instruction, we end up with an offset that is 4 bytes too
2409 // small. This requires us to add 4 to the global variable offset in order
2410 // to compute the correct address. Similarly for the s_addc_u32 instruction,
2411 // the encoding of $symbol starts 12 bytes after the start of the s_add_u32
2412 // instruction.
2413
2414 int64_t Adjust = 0;
2415 if (ST.hasGetPCZeroExtension()) {
2416 // Fix up hardware that does not sign-extend the 48-bit PC value by
2417 // inserting: s_sext_i32_i16 reghi, reghi
2418 Bundler.append(
2419 BuildMI(MF, DL, get(AMDGPU::S_SEXT_I32_I16), RegHi).addReg(RegHi));
2420 Adjust += 4;
2421 }
2422
2423 if (OpLo.isGlobal())
2424 OpLo.setOffset(OpLo.getOffset() + Adjust + 4);
2425 Bundler.append(
2426 BuildMI(MF, DL, get(AMDGPU::S_ADD_U32), RegLo).addReg(RegLo).add(OpLo));
2427
2428 if (OpHi.isGlobal())
2429 OpHi.setOffset(OpHi.getOffset() + Adjust + 12);
2430 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_ADDC_U32), RegHi)
2431 .addReg(RegHi)
2432 .add(OpHi));
2433
2434 finalizeBundle(MBB, Bundler.begin());
2435
2436 MI.eraseFromParent();
2437 break;
2438 }
2439 case AMDGPU::SI_PC_ADD_REL_OFFSET64: {
2440 MachineFunction &MF = *MBB.getParent();
2441 Register Reg = MI.getOperand(0).getReg();
2442 MachineOperand Op = MI.getOperand(1);
2443
2444 // Create a bundle so these instructions won't be re-ordered by the
2445 // post-RA scheduler.
2446 MIBundleBuilder Bundler(MBB, MI);
2447 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg));
2448 if (Op.isGlobal())
2449 Op.setOffset(Op.getOffset() + 4);
2450 Bundler.append(
2451 BuildMI(MF, DL, get(AMDGPU::S_ADD_U64), Reg).addReg(Reg).add(Op));
2452
2453 finalizeBundle(MBB, Bundler.begin());
2454
2455 MI.eraseFromParent();
2456 break;
2457 }
2458 case AMDGPU::ENTER_STRICT_WWM: {
2459 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
2460 // Whole Wave Mode is entered.
2461 MI.setDesc(get(LMC.OrSaveExecOpc));
2462 break;
2463 }
2464 case AMDGPU::ENTER_STRICT_WQM: {
2465 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
2466 // STRICT_WQM is entered.
2467 BuildMI(MBB, MI, DL, get(LMC.MovOpc), MI.getOperand(0).getReg())
2468 .addReg(LMC.ExecReg);
2469 BuildMI(MBB, MI, DL, get(LMC.WQMOpc), LMC.ExecReg).addReg(LMC.ExecReg);
2470
2471 MI.eraseFromParent();
2472 break;
2473 }
2474 case AMDGPU::EXIT_STRICT_WWM:
2475 case AMDGPU::EXIT_STRICT_WQM: {
2476 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
2477 // WWM/STICT_WQM is exited.
2478 MI.setDesc(get(LMC.MovOpc));
2479 break;
2480 }
2481 case AMDGPU::SI_RETURN: {
2482 const MachineFunction *MF = MBB.getParent();
2483 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
2484 const SIRegisterInfo *TRI = ST.getRegisterInfo();
2485 // Hiding the return address use with SI_RETURN may lead to extra kills in
2486 // the function and missing live-ins. We are fine in practice because callee
2487 // saved register handling ensures the register value is restored before
2488 // RET, but we need the undef flag here to appease the MachineVerifier
2489 // liveness checks.
2491 BuildMI(MBB, MI, DL, get(AMDGPU::S_SETPC_B64_return))
2492 .addReg(TRI->getReturnAddressReg(*MF), RegState::Undef);
2493
2494 MIB.copyImplicitOps(MI);
2495 MI.eraseFromParent();
2496 break;
2497 }
2498
2499 case AMDGPU::S_MUL_U64_U32_PSEUDO:
2500 case AMDGPU::S_MUL_I64_I32_PSEUDO:
2501 MI.setDesc(get(AMDGPU::S_MUL_U64));
2502 break;
2503
2504 case AMDGPU::S_GETPC_B64_pseudo:
2505 MI.setDesc(get(AMDGPU::S_GETPC_B64));
2506 if (ST.hasGetPCZeroExtension()) {
2507 Register Dst = MI.getOperand(0).getReg();
2508 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
2509 // Fix up hardware that does not sign-extend the 48-bit PC value by
2510 // inserting: s_sext_i32_i16 dsthi, dsthi
2511 BuildMI(MBB, std::next(MI.getIterator()), DL, get(AMDGPU::S_SEXT_I32_I16),
2512 DstHi)
2513 .addReg(DstHi);
2514 }
2515 break;
2516
2517 case AMDGPU::V_MAX_BF16_PSEUDO_e64:
2518 assert(ST.hasBF16PackedInsts());
2519 MI.setDesc(get(AMDGPU::V_PK_MAX_NUM_BF16));
2520 MI.addOperand(MachineOperand::CreateImm(0)); // op_sel
2521 MI.addOperand(MachineOperand::CreateImm(0)); // neg_lo
2522 MI.addOperand(MachineOperand::CreateImm(0)); // neg_hi
2523 auto Op0 = getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
2524 Op0->setImm(Op0->getImm() | SISrcMods::OP_SEL_1);
2525 auto Op1 = getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
2526 Op1->setImm(Op1->getImm() | SISrcMods::OP_SEL_1);
2527 break;
2528 }
2529
2530 return true;
2531}
2532
2535 unsigned SubIdx, const MachineInstr &Orig,
2536 const TargetRegisterInfo &RI) const {
2537
2538 // Try shrinking the instruction to remat only the part needed for current
2539 // context.
2540 // TODO: Handle more cases.
2541 unsigned Opcode = Orig.getOpcode();
2542 switch (Opcode) {
2543 case AMDGPU::S_LOAD_DWORDX16_IMM:
2544 case AMDGPU::S_LOAD_DWORDX8_IMM: {
2545 if (SubIdx != 0)
2546 break;
2547
2548 if (I == MBB.end())
2549 break;
2550
2551 if (I->isBundled())
2552 break;
2553
2554 // Look for a single use of the register that is also a subreg.
2555 Register RegToFind = Orig.getOperand(0).getReg();
2556 MachineOperand *UseMO = nullptr;
2557 for (auto &CandMO : I->operands()) {
2558 if (!CandMO.isReg() || CandMO.getReg() != RegToFind || CandMO.isDef())
2559 continue;
2560 if (UseMO) {
2561 UseMO = nullptr;
2562 break;
2563 }
2564 UseMO = &CandMO;
2565 }
2566 if (!UseMO || UseMO->getSubReg() == AMDGPU::NoSubRegister)
2567 break;
2568
2569 unsigned Offset = RI.getSubRegIdxOffset(UseMO->getSubReg());
2570 unsigned SubregSize = RI.getSubRegIdxSize(UseMO->getSubReg());
2571
2572 MachineFunction *MF = MBB.getParent();
2574 assert(MRI.use_nodbg_empty(DestReg) && "DestReg should have no users yet.");
2575
2576 unsigned NewOpcode = -1;
2577 if (SubregSize == 256)
2578 NewOpcode = AMDGPU::S_LOAD_DWORDX8_IMM;
2579 else if (SubregSize == 128)
2580 NewOpcode = AMDGPU::S_LOAD_DWORDX4_IMM;
2581 else
2582 break;
2583
2584 const MCInstrDesc &TID = get(NewOpcode);
2585 const TargetRegisterClass *NewRC =
2586 RI.getAllocatableClass(getRegClass(TID, 0, &RI));
2587 MRI.setRegClass(DestReg, NewRC);
2588
2589 UseMO->setReg(DestReg);
2590 UseMO->setSubReg(AMDGPU::NoSubRegister);
2591
2592 // Use a smaller load with the desired size, possibly with updated offset.
2593 MachineInstr *MI = MF->CloneMachineInstr(&Orig);
2594 MI->setDesc(TID);
2595 MI->getOperand(0).setReg(DestReg);
2596 MI->getOperand(0).setSubReg(AMDGPU::NoSubRegister);
2597 if (Offset) {
2598 MachineOperand *OffsetMO = getNamedOperand(*MI, AMDGPU::OpName::offset);
2599 int64_t FinalOffset = OffsetMO->getImm() + Offset / 8;
2600 OffsetMO->setImm(FinalOffset);
2601 }
2603 for (const MachineMemOperand *MemOp : Orig.memoperands())
2604 NewMMOs.push_back(MF->getMachineMemOperand(MemOp, MemOp->getPointerInfo(),
2605 SubregSize / 8));
2606 MI->setMemRefs(*MF, NewMMOs);
2607
2608 MBB.insert(I, MI);
2609 return;
2610 }
2611
2612 default:
2613 break;
2614 }
2615
2616 TargetInstrInfo::reMaterialize(MBB, I, DestReg, SubIdx, Orig, RI);
2617}
2618
2619std::pair<MachineInstr*, MachineInstr*>
2621 assert (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
2622
2623 if (ST.hasMovB64() && ST.hasFeature(AMDGPU::FeatureDPALU_DPP) &&
2625 ST, getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl)->getImm())) {
2626 MI.setDesc(get(AMDGPU::V_MOV_B64_dpp));
2627 return std::pair(&MI, nullptr);
2628 }
2629
2630 MachineBasicBlock &MBB = *MI.getParent();
2631 DebugLoc DL = MBB.findDebugLoc(MI);
2632 MachineFunction *MF = MBB.getParent();
2634 Register Dst = MI.getOperand(0).getReg();
2635 unsigned Part = 0;
2636 MachineInstr *Split[2];
2637
2638 for (auto Sub : { AMDGPU::sub0, AMDGPU::sub1 }) {
2639 auto MovDPP = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_dpp));
2640 if (Dst.isPhysical()) {
2641 MovDPP.addDef(RI.getSubReg(Dst, Sub));
2642 } else {
2643 assert(MRI.isSSA());
2644 auto Tmp = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2645 MovDPP.addDef(Tmp);
2646 }
2647
2648 for (unsigned I = 1; I <= 2; ++I) { // old and src operands.
2649 const MachineOperand &SrcOp = MI.getOperand(I);
2650 assert(!SrcOp.isFPImm());
2651 if (SrcOp.isImm()) {
2652 APInt Imm(64, SrcOp.getImm());
2653 Imm.ashrInPlace(Part * 32);
2654 MovDPP.addImm(Imm.getLoBits(32).getZExtValue());
2655 } else {
2656 assert(SrcOp.isReg());
2657 Register Src = SrcOp.getReg();
2658 if (Src.isPhysical())
2659 MovDPP.addReg(RI.getSubReg(Src, Sub));
2660 else
2661 MovDPP.addReg(Src, SrcOp.isUndef() ? RegState::Undef : 0, Sub);
2662 }
2663 }
2664
2665 for (const MachineOperand &MO : llvm::drop_begin(MI.explicit_operands(), 3))
2666 MovDPP.addImm(MO.getImm());
2667
2668 Split[Part] = MovDPP;
2669 ++Part;
2670 }
2671
2672 if (Dst.isVirtual())
2673 BuildMI(MBB, MI, DL, get(AMDGPU::REG_SEQUENCE), Dst)
2674 .addReg(Split[0]->getOperand(0).getReg())
2675 .addImm(AMDGPU::sub0)
2676 .addReg(Split[1]->getOperand(0).getReg())
2677 .addImm(AMDGPU::sub1);
2678
2679 MI.eraseFromParent();
2680 return std::pair(Split[0], Split[1]);
2681}
2682
2683std::optional<DestSourcePair>
2685 if (MI.getOpcode() == AMDGPU::WWM_COPY)
2686 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
2687
2688 return std::nullopt;
2689}
2690
2692 AMDGPU::OpName Src0OpName,
2693 MachineOperand &Src1,
2694 AMDGPU::OpName Src1OpName) const {
2695 MachineOperand *Src0Mods = getNamedOperand(MI, Src0OpName);
2696 if (!Src0Mods)
2697 return false;
2698
2699 MachineOperand *Src1Mods = getNamedOperand(MI, Src1OpName);
2700 assert(Src1Mods &&
2701 "All commutable instructions have both src0 and src1 modifiers");
2702
2703 int Src0ModsVal = Src0Mods->getImm();
2704 int Src1ModsVal = Src1Mods->getImm();
2705
2706 Src1Mods->setImm(Src0ModsVal);
2707 Src0Mods->setImm(Src1ModsVal);
2708 return true;
2709}
2710
2712 MachineOperand &RegOp,
2713 MachineOperand &NonRegOp) {
2714 Register Reg = RegOp.getReg();
2715 unsigned SubReg = RegOp.getSubReg();
2716 bool IsKill = RegOp.isKill();
2717 bool IsDead = RegOp.isDead();
2718 bool IsUndef = RegOp.isUndef();
2719 bool IsDebug = RegOp.isDebug();
2720
2721 if (NonRegOp.isImm())
2722 RegOp.ChangeToImmediate(NonRegOp.getImm());
2723 else if (NonRegOp.isFI())
2724 RegOp.ChangeToFrameIndex(NonRegOp.getIndex());
2725 else if (NonRegOp.isGlobal()) {
2726 RegOp.ChangeToGA(NonRegOp.getGlobal(), NonRegOp.getOffset(),
2727 NonRegOp.getTargetFlags());
2728 } else
2729 return nullptr;
2730
2731 // Make sure we don't reinterpret a subreg index in the target flags.
2732 RegOp.setTargetFlags(NonRegOp.getTargetFlags());
2733
2734 NonRegOp.ChangeToRegister(Reg, false, false, IsKill, IsDead, IsUndef, IsDebug);
2735 NonRegOp.setSubReg(SubReg);
2736
2737 return &MI;
2738}
2739
2741 MachineOperand &NonRegOp1,
2742 MachineOperand &NonRegOp2) {
2743 unsigned TargetFlags = NonRegOp1.getTargetFlags();
2744 int64_t NonRegVal = NonRegOp1.getImm();
2745
2746 NonRegOp1.setImm(NonRegOp2.getImm());
2747 NonRegOp2.setImm(NonRegVal);
2748 NonRegOp1.setTargetFlags(NonRegOp2.getTargetFlags());
2749 NonRegOp2.setTargetFlags(TargetFlags);
2750 return &MI;
2751}
2752
2753bool SIInstrInfo::isLegalToSwap(const MachineInstr &MI, unsigned OpIdx0,
2754 unsigned OpIdx1) const {
2755 const MCInstrDesc &InstDesc = MI.getDesc();
2756 const MCOperandInfo &OpInfo0 = InstDesc.operands()[OpIdx0];
2757 const MCOperandInfo &OpInfo1 = InstDesc.operands()[OpIdx1];
2758
2759 unsigned Opc = MI.getOpcode();
2760 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
2761
2762 const MachineOperand &MO0 = MI.getOperand(OpIdx0);
2763 const MachineOperand &MO1 = MI.getOperand(OpIdx1);
2764
2765 // Swap doesn't breach constant bus or literal limits
2766 // It may move literal to position other than src0, this is not allowed
2767 // pre-gfx10 However, most test cases need literals in Src0 for VOP
2768 // FIXME: After gfx9, literal can be in place other than Src0
2769 if (isVALU(MI)) {
2770 if ((int)OpIdx0 == Src0Idx && !MO0.isReg() &&
2771 !isInlineConstant(MO0, OpInfo1))
2772 return false;
2773 if ((int)OpIdx1 == Src0Idx && !MO1.isReg() &&
2774 !isInlineConstant(MO1, OpInfo0))
2775 return false;
2776 }
2777
2778 if ((int)OpIdx1 != Src0Idx && MO0.isReg()) {
2779 if (OpInfo1.RegClass == -1)
2780 return OpInfo1.OperandType == MCOI::OPERAND_UNKNOWN;
2781 return isLegalRegOperand(MI, OpIdx1, MO0) &&
2782 (!MO1.isReg() || isLegalRegOperand(MI, OpIdx0, MO1));
2783 }
2784 if ((int)OpIdx0 != Src0Idx && MO1.isReg()) {
2785 if (OpInfo0.RegClass == -1)
2786 return OpInfo0.OperandType == MCOI::OPERAND_UNKNOWN;
2787 return (!MO0.isReg() || isLegalRegOperand(MI, OpIdx1, MO0)) &&
2788 isLegalRegOperand(MI, OpIdx0, MO1);
2789 }
2790
2791 // No need to check 64-bit literals since swapping does not bring new
2792 // 64-bit literals into current instruction to fold to 32-bit
2793
2794 return isImmOperandLegal(MI, OpIdx1, MO0);
2795}
2796
2798 unsigned Src0Idx,
2799 unsigned Src1Idx) const {
2800 assert(!NewMI && "this should never be used");
2801
2802 unsigned Opc = MI.getOpcode();
2803 int CommutedOpcode = commuteOpcode(Opc);
2804 if (CommutedOpcode == -1)
2805 return nullptr;
2806
2807 if (Src0Idx > Src1Idx)
2808 std::swap(Src0Idx, Src1Idx);
2809
2810 assert(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) ==
2811 static_cast<int>(Src0Idx) &&
2812 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) ==
2813 static_cast<int>(Src1Idx) &&
2814 "inconsistency with findCommutedOpIndices");
2815
2816 if (!isLegalToSwap(MI, Src0Idx, Src1Idx))
2817 return nullptr;
2818
2819 MachineInstr *CommutedMI = nullptr;
2820 MachineOperand &Src0 = MI.getOperand(Src0Idx);
2821 MachineOperand &Src1 = MI.getOperand(Src1Idx);
2822 if (Src0.isReg() && Src1.isReg()) {
2823 // Be sure to copy the source modifiers to the right place.
2824 CommutedMI =
2825 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, Src0Idx, Src1Idx);
2826 } else if (Src0.isReg() && !Src1.isReg()) {
2827 CommutedMI = swapRegAndNonRegOperand(MI, Src0, Src1);
2828 } else if (!Src0.isReg() && Src1.isReg()) {
2829 CommutedMI = swapRegAndNonRegOperand(MI, Src1, Src0);
2830 } else if (Src0.isImm() && Src1.isImm()) {
2831 CommutedMI = swapImmOperands(MI, Src0, Src1);
2832 } else {
2833 // FIXME: Found two non registers to commute. This does happen.
2834 return nullptr;
2835 }
2836
2837 if (CommutedMI) {
2838 swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_modifiers,
2839 Src1, AMDGPU::OpName::src1_modifiers);
2840
2841 swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_sel, Src1,
2842 AMDGPU::OpName::src1_sel);
2843
2844 CommutedMI->setDesc(get(CommutedOpcode));
2845 }
2846
2847 return CommutedMI;
2848}
2849
2850// This needs to be implemented because the source modifiers may be inserted
2851// between the true commutable operands, and the base
2852// TargetInstrInfo::commuteInstruction uses it.
2854 unsigned &SrcOpIdx0,
2855 unsigned &SrcOpIdx1) const {
2856 return findCommutedOpIndices(MI.getDesc(), SrcOpIdx0, SrcOpIdx1);
2857}
2858
2860 unsigned &SrcOpIdx0,
2861 unsigned &SrcOpIdx1) const {
2862 if (!Desc.isCommutable())
2863 return false;
2864
2865 unsigned Opc = Desc.getOpcode();
2866 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
2867 if (Src0Idx == -1)
2868 return false;
2869
2870 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
2871 if (Src1Idx == -1)
2872 return false;
2873
2874 return fixCommutedOpIndices(SrcOpIdx0, SrcOpIdx1, Src0Idx, Src1Idx);
2875}
2876
2878 int64_t BrOffset) const {
2879 // BranchRelaxation should never have to check s_setpc_b64 or s_add_pc_i64
2880 // because its dest block is unanalyzable.
2881 assert(isSOPP(BranchOp) || isSOPK(BranchOp));
2882
2883 // Convert to dwords.
2884 BrOffset /= 4;
2885
2886 // The branch instructions do PC += signext(SIMM16 * 4) + 4, so the offset is
2887 // from the next instruction.
2888 BrOffset -= 1;
2889
2890 return isIntN(BranchOffsetBits, BrOffset);
2891}
2892
2895 return MI.getOperand(0).getMBB();
2896}
2897
2899 for (const MachineInstr &MI : MBB->terminators()) {
2900 if (MI.getOpcode() == AMDGPU::SI_IF || MI.getOpcode() == AMDGPU::SI_ELSE ||
2901 MI.getOpcode() == AMDGPU::SI_LOOP)
2902 return true;
2903 }
2904 return false;
2905}
2906
2908 MachineBasicBlock &DestBB,
2909 MachineBasicBlock &RestoreBB,
2910 const DebugLoc &DL, int64_t BrOffset,
2911 RegScavenger *RS) const {
2912 assert(MBB.empty() &&
2913 "new block should be inserted for expanding unconditional branch");
2914 assert(MBB.pred_size() == 1);
2915 assert(RestoreBB.empty() &&
2916 "restore block should be inserted for restoring clobbered registers");
2917
2918 MachineFunction *MF = MBB.getParent();
2921 auto I = MBB.end();
2922 auto &MCCtx = MF->getContext();
2923
2924 if (ST.hasAddPC64Inst()) {
2925 MCSymbol *Offset =
2926 MCCtx.createTempSymbol("offset", /*AlwaysAddSuffix=*/true);
2927 auto AddPC = BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_PC_I64))
2929 MCSymbol *PostAddPCLabel =
2930 MCCtx.createTempSymbol("post_addpc", /*AlwaysAddSuffix=*/true);
2931 AddPC->setPostInstrSymbol(*MF, PostAddPCLabel);
2932 auto *OffsetExpr = MCBinaryExpr::createSub(
2933 MCSymbolRefExpr::create(DestBB.getSymbol(), MCCtx),
2934 MCSymbolRefExpr::create(PostAddPCLabel, MCCtx), MCCtx);
2935 Offset->setVariableValue(OffsetExpr);
2936 return;
2937 }
2938
2939 assert(RS && "RegScavenger required for long branching");
2940
2941 // FIXME: Virtual register workaround for RegScavenger not working with empty
2942 // blocks.
2943 Register PCReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
2944
2945 // Note: as this is used after hazard recognizer we need to apply some hazard
2946 // workarounds directly.
2947 const bool FlushSGPRWrites = (ST.isWave64() && ST.hasVALUMaskWriteHazard()) ||
2948 ST.hasVALUReadSGPRHazard();
2949 auto ApplyHazardWorkarounds = [this, &MBB, &I, &DL, FlushSGPRWrites]() {
2950 if (FlushSGPRWrites)
2951 BuildMI(MBB, I, DL, get(AMDGPU::S_WAITCNT_DEPCTR))
2953 };
2954
2955 // We need to compute the offset relative to the instruction immediately after
2956 // s_getpc_b64. Insert pc arithmetic code before last terminator.
2957 MachineInstr *GetPC = BuildMI(MBB, I, DL, get(AMDGPU::S_GETPC_B64), PCReg);
2958 ApplyHazardWorkarounds();
2959
2960 MCSymbol *PostGetPCLabel =
2961 MCCtx.createTempSymbol("post_getpc", /*AlwaysAddSuffix=*/true);
2962 GetPC->setPostInstrSymbol(*MF, PostGetPCLabel);
2963
2964 MCSymbol *OffsetLo =
2965 MCCtx.createTempSymbol("offset_lo", /*AlwaysAddSuffix=*/true);
2966 MCSymbol *OffsetHi =
2967 MCCtx.createTempSymbol("offset_hi", /*AlwaysAddSuffix=*/true);
2968 BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_U32))
2969 .addReg(PCReg, RegState::Define, AMDGPU::sub0)
2970 .addReg(PCReg, 0, AMDGPU::sub0)
2971 .addSym(OffsetLo, MO_FAR_BRANCH_OFFSET);
2972 BuildMI(MBB, I, DL, get(AMDGPU::S_ADDC_U32))
2973 .addReg(PCReg, RegState::Define, AMDGPU::sub1)
2974 .addReg(PCReg, 0, AMDGPU::sub1)
2975 .addSym(OffsetHi, MO_FAR_BRANCH_OFFSET);
2976 ApplyHazardWorkarounds();
2977
2978 // Insert the indirect branch after the other terminator.
2979 BuildMI(&MBB, DL, get(AMDGPU::S_SETPC_B64))
2980 .addReg(PCReg);
2981
2982 // If a spill is needed for the pc register pair, we need to insert a spill
2983 // restore block right before the destination block, and insert a short branch
2984 // into the old destination block's fallthrough predecessor.
2985 // e.g.:
2986 //
2987 // s_cbranch_scc0 skip_long_branch:
2988 //
2989 // long_branch_bb:
2990 // spill s[8:9]
2991 // s_getpc_b64 s[8:9]
2992 // s_add_u32 s8, s8, restore_bb
2993 // s_addc_u32 s9, s9, 0
2994 // s_setpc_b64 s[8:9]
2995 //
2996 // skip_long_branch:
2997 // foo;
2998 //
2999 // .....
3000 //
3001 // dest_bb_fallthrough_predecessor:
3002 // bar;
3003 // s_branch dest_bb
3004 //
3005 // restore_bb:
3006 // restore s[8:9]
3007 // fallthrough dest_bb
3008 ///
3009 // dest_bb:
3010 // buzz;
3011
3012 Register LongBranchReservedReg = MFI->getLongBranchReservedReg();
3013 Register Scav;
3014
3015 // If we've previously reserved a register for long branches
3016 // avoid running the scavenger and just use those registers
3017 if (LongBranchReservedReg) {
3018 RS->enterBasicBlock(MBB);
3019 Scav = LongBranchReservedReg;
3020 } else {
3022 Scav = RS->scavengeRegisterBackwards(
3023 AMDGPU::SReg_64RegClass, MachineBasicBlock::iterator(GetPC),
3024 /* RestoreAfter */ false, 0, /* AllowSpill */ false);
3025 }
3026 if (Scav) {
3027 RS->setRegUsed(Scav);
3028 MRI.replaceRegWith(PCReg, Scav);
3029 MRI.clearVirtRegs();
3030 } else {
3031 // As SGPR needs VGPR to be spilled, we reuse the slot of temporary VGPR for
3032 // SGPR spill.
3033 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
3034 const SIRegisterInfo *TRI = ST.getRegisterInfo();
3035 TRI->spillEmergencySGPR(GetPC, RestoreBB, AMDGPU::SGPR0_SGPR1, RS);
3036 MRI.replaceRegWith(PCReg, AMDGPU::SGPR0_SGPR1);
3037 MRI.clearVirtRegs();
3038 }
3039
3040 MCSymbol *DestLabel = Scav ? DestBB.getSymbol() : RestoreBB.getSymbol();
3041 // Now, the distance could be defined.
3043 MCSymbolRefExpr::create(DestLabel, MCCtx),
3044 MCSymbolRefExpr::create(PostGetPCLabel, MCCtx), MCCtx);
3045 // Add offset assignments.
3046 auto *Mask = MCConstantExpr::create(0xFFFFFFFFULL, MCCtx);
3047 OffsetLo->setVariableValue(MCBinaryExpr::createAnd(Offset, Mask, MCCtx));
3048 auto *ShAmt = MCConstantExpr::create(32, MCCtx);
3049 OffsetHi->setVariableValue(MCBinaryExpr::createAShr(Offset, ShAmt, MCCtx));
3050}
3051
3052unsigned SIInstrInfo::getBranchOpcode(SIInstrInfo::BranchPredicate Cond) {
3053 switch (Cond) {
3054 case SIInstrInfo::SCC_TRUE:
3055 return AMDGPU::S_CBRANCH_SCC1;
3056 case SIInstrInfo::SCC_FALSE:
3057 return AMDGPU::S_CBRANCH_SCC0;
3058 case SIInstrInfo::VCCNZ:
3059 return AMDGPU::S_CBRANCH_VCCNZ;
3060 case SIInstrInfo::VCCZ:
3061 return AMDGPU::S_CBRANCH_VCCZ;
3062 case SIInstrInfo::EXECNZ:
3063 return AMDGPU::S_CBRANCH_EXECNZ;
3064 case SIInstrInfo::EXECZ:
3065 return AMDGPU::S_CBRANCH_EXECZ;
3066 default:
3067 llvm_unreachable("invalid branch predicate");
3068 }
3069}
3070
3071SIInstrInfo::BranchPredicate SIInstrInfo::getBranchPredicate(unsigned Opcode) {
3072 switch (Opcode) {
3073 case AMDGPU::S_CBRANCH_SCC0:
3074 return SCC_FALSE;
3075 case AMDGPU::S_CBRANCH_SCC1:
3076 return SCC_TRUE;
3077 case AMDGPU::S_CBRANCH_VCCNZ:
3078 return VCCNZ;
3079 case AMDGPU::S_CBRANCH_VCCZ:
3080 return VCCZ;
3081 case AMDGPU::S_CBRANCH_EXECNZ:
3082 return EXECNZ;
3083 case AMDGPU::S_CBRANCH_EXECZ:
3084 return EXECZ;
3085 default:
3086 return INVALID_BR;
3087 }
3088}
3089
3093 MachineBasicBlock *&FBB,
3095 bool AllowModify) const {
3096 if (I->getOpcode() == AMDGPU::S_BRANCH) {
3097 // Unconditional Branch
3098 TBB = I->getOperand(0).getMBB();
3099 return false;
3100 }
3101
3102 BranchPredicate Pred = getBranchPredicate(I->getOpcode());
3103 if (Pred == INVALID_BR)
3104 return true;
3105
3106 MachineBasicBlock *CondBB = I->getOperand(0).getMBB();
3107 Cond.push_back(MachineOperand::CreateImm(Pred));
3108 Cond.push_back(I->getOperand(1)); // Save the branch register.
3109
3110 ++I;
3111
3112 if (I == MBB.end()) {
3113 // Conditional branch followed by fall-through.
3114 TBB = CondBB;
3115 return false;
3116 }
3117
3118 if (I->getOpcode() == AMDGPU::S_BRANCH) {
3119 TBB = CondBB;
3120 FBB = I->getOperand(0).getMBB();
3121 return false;
3122 }
3123
3124 return true;
3125}
3126
3128 MachineBasicBlock *&FBB,
3130 bool AllowModify) const {
3131 MachineBasicBlock::iterator I = MBB.getFirstTerminator();
3132 auto E = MBB.end();
3133 if (I == E)
3134 return false;
3135
3136 // Skip over the instructions that are artificially terminators for special
3137 // exec management.
3138 while (I != E && !I->isBranch() && !I->isReturn()) {
3139 switch (I->getOpcode()) {
3140 case AMDGPU::S_MOV_B64_term:
3141 case AMDGPU::S_XOR_B64_term:
3142 case AMDGPU::S_OR_B64_term:
3143 case AMDGPU::S_ANDN2_B64_term:
3144 case AMDGPU::S_AND_B64_term:
3145 case AMDGPU::S_AND_SAVEEXEC_B64_term:
3146 case AMDGPU::S_MOV_B32_term:
3147 case AMDGPU::S_XOR_B32_term:
3148 case AMDGPU::S_OR_B32_term:
3149 case AMDGPU::S_ANDN2_B32_term:
3150 case AMDGPU::S_AND_B32_term:
3151 case AMDGPU::S_AND_SAVEEXEC_B32_term:
3152 break;
3153 case AMDGPU::SI_IF:
3154 case AMDGPU::SI_ELSE:
3155 case AMDGPU::SI_KILL_I1_TERMINATOR:
3156 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
3157 // FIXME: It's messy that these need to be considered here at all.
3158 return true;
3159 default:
3160 llvm_unreachable("unexpected non-branch terminator inst");
3161 }
3162
3163 ++I;
3164 }
3165
3166 if (I == E)
3167 return false;
3168
3169 return analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify);
3170}
3171
3173 int *BytesRemoved) const {
3174 unsigned Count = 0;
3175 unsigned RemovedSize = 0;
3176 for (MachineInstr &MI : llvm::make_early_inc_range(MBB.terminators())) {
3177 // Skip over artificial terminators when removing instructions.
3178 if (MI.isBranch() || MI.isReturn()) {
3179 RemovedSize += getInstSizeInBytes(MI);
3180 MI.eraseFromParent();
3181 ++Count;
3182 }
3183 }
3184
3185 if (BytesRemoved)
3186 *BytesRemoved = RemovedSize;
3187
3188 return Count;
3189}
3190
3191// Copy the flags onto the implicit condition register operand.
3193 const MachineOperand &OrigCond) {
3194 CondReg.setIsUndef(OrigCond.isUndef());
3195 CondReg.setIsKill(OrigCond.isKill());
3196}
3197
3200 MachineBasicBlock *FBB,
3202 const DebugLoc &DL,
3203 int *BytesAdded) const {
3204 if (!FBB && Cond.empty()) {
3205 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH))
3206 .addMBB(TBB);
3207 if (BytesAdded)
3208 *BytesAdded = ST.hasOffset3fBug() ? 8 : 4;
3209 return 1;
3210 }
3211
3212 assert(TBB && Cond[0].isImm());
3213
3214 unsigned Opcode
3215 = getBranchOpcode(static_cast<BranchPredicate>(Cond[0].getImm()));
3216
3217 if (!FBB) {
3218 MachineInstr *CondBr =
3219 BuildMI(&MBB, DL, get(Opcode))
3220 .addMBB(TBB);
3221
3222 // Copy the flags onto the implicit condition register operand.
3223 preserveCondRegFlags(CondBr->getOperand(1), Cond[1]);
3224 fixImplicitOperands(*CondBr);
3225
3226 if (BytesAdded)
3227 *BytesAdded = ST.hasOffset3fBug() ? 8 : 4;
3228 return 1;
3229 }
3230
3231 assert(TBB && FBB);
3232
3233 MachineInstr *CondBr =
3234 BuildMI(&MBB, DL, get(Opcode))
3235 .addMBB(TBB);
3236 fixImplicitOperands(*CondBr);
3237 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH))
3238 .addMBB(FBB);
3239
3240 MachineOperand &CondReg = CondBr->getOperand(1);
3241 CondReg.setIsUndef(Cond[1].isUndef());
3242 CondReg.setIsKill(Cond[1].isKill());
3243
3244 if (BytesAdded)
3245 *BytesAdded = ST.hasOffset3fBug() ? 16 : 8;
3246
3247 return 2;
3248}
3249
3252 if (Cond.size() != 2) {
3253 return true;
3254 }
3255
3256 if (Cond[0].isImm()) {
3257 Cond[0].setImm(-Cond[0].getImm());
3258 return false;
3259 }
3260
3261 return true;
3262}
3263
3266 Register DstReg, Register TrueReg,
3267 Register FalseReg, int &CondCycles,
3268 int &TrueCycles, int &FalseCycles) const {
3269 switch (Cond[0].getImm()) {
3270 case VCCNZ:
3271 case VCCZ: {
3272 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3273 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg);
3274 if (MRI.getRegClass(FalseReg) != RC)
3275 return false;
3276
3277 int NumInsts = AMDGPU::getRegBitWidth(*RC) / 32;
3278 CondCycles = TrueCycles = FalseCycles = NumInsts; // ???
3279
3280 // Limit to equal cost for branch vs. N v_cndmask_b32s.
3281 return RI.hasVGPRs(RC) && NumInsts <= 6;
3282 }
3283 case SCC_TRUE:
3284 case SCC_FALSE: {
3285 // FIXME: We could insert for VGPRs if we could replace the original compare
3286 // with a vector one.
3287 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3288 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg);
3289 if (MRI.getRegClass(FalseReg) != RC)
3290 return false;
3291
3292 int NumInsts = AMDGPU::getRegBitWidth(*RC) / 32;
3293
3294 // Multiples of 8 can do s_cselect_b64
3295 if (NumInsts % 2 == 0)
3296 NumInsts /= 2;
3297
3298 CondCycles = TrueCycles = FalseCycles = NumInsts; // ???
3299 return RI.isSGPRClass(RC);
3300 }
3301 default:
3302 return false;
3303 }
3304}
3305
3309 Register TrueReg, Register FalseReg) const {
3310 BranchPredicate Pred = static_cast<BranchPredicate>(Cond[0].getImm());
3311 if (Pred == VCCZ || Pred == SCC_FALSE) {
3312 Pred = static_cast<BranchPredicate>(-Pred);
3313 std::swap(TrueReg, FalseReg);
3314 }
3315
3316 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3317 const TargetRegisterClass *DstRC = MRI.getRegClass(DstReg);
3318 unsigned DstSize = RI.getRegSizeInBits(*DstRC);
3319
3320 if (DstSize == 32) {
3322 if (Pred == SCC_TRUE) {
3323 Select = BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B32), DstReg)
3324 .addReg(TrueReg)
3325 .addReg(FalseReg);
3326 } else {
3327 // Instruction's operands are backwards from what is expected.
3328 Select = BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e32), DstReg)
3329 .addReg(FalseReg)
3330 .addReg(TrueReg);
3331 }
3332
3333 preserveCondRegFlags(Select->getOperand(3), Cond[1]);
3334 return;
3335 }
3336
3337 if (DstSize == 64 && Pred == SCC_TRUE) {
3339 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), DstReg)
3340 .addReg(TrueReg)
3341 .addReg(FalseReg);
3342
3343 preserveCondRegFlags(Select->getOperand(3), Cond[1]);
3344 return;
3345 }
3346
3347 static const int16_t Sub0_15[] = {
3348 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
3349 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
3350 AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11,
3351 AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15,
3352 };
3353
3354 static const int16_t Sub0_15_64[] = {
3355 AMDGPU::sub0_sub1, AMDGPU::sub2_sub3,
3356 AMDGPU::sub4_sub5, AMDGPU::sub6_sub7,
3357 AMDGPU::sub8_sub9, AMDGPU::sub10_sub11,
3358 AMDGPU::sub12_sub13, AMDGPU::sub14_sub15,
3359 };
3360
3361 unsigned SelOp = AMDGPU::V_CNDMASK_B32_e32;
3362 const TargetRegisterClass *EltRC = &AMDGPU::VGPR_32RegClass;
3363 const int16_t *SubIndices = Sub0_15;
3364 int NElts = DstSize / 32;
3365
3366 // 64-bit select is only available for SALU.
3367 // TODO: Split 96-bit into 64-bit and 32-bit, not 3x 32-bit.
3368 if (Pred == SCC_TRUE) {
3369 if (NElts % 2) {
3370 SelOp = AMDGPU::S_CSELECT_B32;
3371 EltRC = &AMDGPU::SGPR_32RegClass;
3372 } else {
3373 SelOp = AMDGPU::S_CSELECT_B64;
3374 EltRC = &AMDGPU::SGPR_64RegClass;
3375 SubIndices = Sub0_15_64;
3376 NElts /= 2;
3377 }
3378 }
3379
3381 MBB, I, DL, get(AMDGPU::REG_SEQUENCE), DstReg);
3382
3383 I = MIB->getIterator();
3384
3386 for (int Idx = 0; Idx != NElts; ++Idx) {
3387 Register DstElt = MRI.createVirtualRegister(EltRC);
3388 Regs.push_back(DstElt);
3389
3390 unsigned SubIdx = SubIndices[Idx];
3391
3393 if (SelOp == AMDGPU::V_CNDMASK_B32_e32) {
3394 Select =
3395 BuildMI(MBB, I, DL, get(SelOp), DstElt)
3396 .addReg(FalseReg, 0, SubIdx)
3397 .addReg(TrueReg, 0, SubIdx);
3398 } else {
3399 Select =
3400 BuildMI(MBB, I, DL, get(SelOp), DstElt)
3401 .addReg(TrueReg, 0, SubIdx)
3402 .addReg(FalseReg, 0, SubIdx);
3403 }
3404
3405 preserveCondRegFlags(Select->getOperand(3), Cond[1]);
3407
3408 MIB.addReg(DstElt)
3409 .addImm(SubIdx);
3410 }
3411}
3412
3414 switch (MI.getOpcode()) {
3415 case AMDGPU::V_MOV_B16_t16_e32:
3416 case AMDGPU::V_MOV_B16_t16_e64:
3417 case AMDGPU::V_MOV_B32_e32:
3418 case AMDGPU::V_MOV_B32_e64:
3419 case AMDGPU::V_MOV_B64_PSEUDO:
3420 case AMDGPU::V_MOV_B64_e32:
3421 case AMDGPU::V_MOV_B64_e64:
3422 case AMDGPU::S_MOV_B32:
3423 case AMDGPU::S_MOV_B64:
3424 case AMDGPU::S_MOV_B64_IMM_PSEUDO:
3425 case AMDGPU::COPY:
3426 case AMDGPU::WWM_COPY:
3427 case AMDGPU::V_ACCVGPR_WRITE_B32_e64:
3428 case AMDGPU::V_ACCVGPR_READ_B32_e64:
3429 case AMDGPU::V_ACCVGPR_MOV_B32:
3430 case AMDGPU::AV_MOV_B32_IMM_PSEUDO:
3431 case AMDGPU::AV_MOV_B64_IMM_PSEUDO:
3432 return true;
3433 default:
3434 return false;
3435 }
3436}
3437
3438static constexpr AMDGPU::OpName ModifierOpNames[] = {
3439 AMDGPU::OpName::src0_modifiers, AMDGPU::OpName::src1_modifiers,
3440 AMDGPU::OpName::src2_modifiers, AMDGPU::OpName::clamp,
3441 AMDGPU::OpName::omod, AMDGPU::OpName::op_sel};
3442
3444 unsigned Opc = MI.getOpcode();
3445 for (AMDGPU::OpName Name : reverse(ModifierOpNames)) {
3446 int Idx = AMDGPU::getNamedOperandIdx(Opc, Name);
3447 if (Idx >= 0)
3448 MI.removeOperand(Idx);
3449 }
3450}
3451
3452std::optional<int64_t> SIInstrInfo::extractSubregFromImm(int64_t Imm,
3453 unsigned SubRegIndex) {
3454 switch (SubRegIndex) {
3455 case AMDGPU::NoSubRegister:
3456 return Imm;
3457 case AMDGPU::sub0:
3458 return SignExtend64<32>(Imm);
3459 case AMDGPU::sub1:
3460 return SignExtend64<32>(Imm >> 32);
3461 case AMDGPU::lo16:
3462 return SignExtend64<16>(Imm);
3463 case AMDGPU::hi16:
3464 return SignExtend64<16>(Imm >> 16);
3465 case AMDGPU::sub1_lo16:
3466 return SignExtend64<16>(Imm >> 32);
3467 case AMDGPU::sub1_hi16:
3468 return SignExtend64<16>(Imm >> 48);
3469 default:
3470 return std::nullopt;
3471 }
3472
3473 llvm_unreachable("covered subregister switch");
3474}
3475
3476static unsigned getNewFMAAKInst(const GCNSubtarget &ST, unsigned Opc) {
3477 switch (Opc) {
3478 case AMDGPU::V_MAC_F16_e32:
3479 case AMDGPU::V_MAC_F16_e64:
3480 case AMDGPU::V_MAD_F16_e64:
3481 return AMDGPU::V_MADAK_F16;
3482 case AMDGPU::V_MAC_F32_e32:
3483 case AMDGPU::V_MAC_F32_e64:
3484 case AMDGPU::V_MAD_F32_e64:
3485 return AMDGPU::V_MADAK_F32;
3486 case AMDGPU::V_FMAC_F32_e32:
3487 case AMDGPU::V_FMAC_F32_e64:
3488 case AMDGPU::V_FMA_F32_e64:
3489 return AMDGPU::V_FMAAK_F32;
3490 case AMDGPU::V_FMAC_F16_e32:
3491 case AMDGPU::V_FMAC_F16_e64:
3492 case AMDGPU::V_FMAC_F16_t16_e64:
3493 case AMDGPU::V_FMAC_F16_fake16_e64:
3494 case AMDGPU::V_FMA_F16_e64:
3495 return ST.hasTrue16BitInsts() ? ST.useRealTrue16Insts()
3496 ? AMDGPU::V_FMAAK_F16_t16
3497 : AMDGPU::V_FMAAK_F16_fake16
3498 : AMDGPU::V_FMAAK_F16;
3499 case AMDGPU::V_FMAC_F64_e32:
3500 case AMDGPU::V_FMAC_F64_e64:
3501 case AMDGPU::V_FMA_F64_e64:
3502 return AMDGPU::V_FMAAK_F64;
3503 default:
3504 llvm_unreachable("invalid instruction");
3505 }
3506}
3507
3508static unsigned getNewFMAMKInst(const GCNSubtarget &ST, unsigned Opc) {
3509 switch (Opc) {
3510 case AMDGPU::V_MAC_F16_e32:
3511 case AMDGPU::V_MAC_F16_e64:
3512 case AMDGPU::V_MAD_F16_e64:
3513 return AMDGPU::V_MADMK_F16;
3514 case AMDGPU::V_MAC_F32_e32:
3515 case AMDGPU::V_MAC_F32_e64:
3516 case AMDGPU::V_MAD_F32_e64:
3517 return AMDGPU::V_MADMK_F32;
3518 case AMDGPU::V_FMAC_F32_e32:
3519 case AMDGPU::V_FMAC_F32_e64:
3520 case AMDGPU::V_FMA_F32_e64:
3521 return AMDGPU::V_FMAMK_F32;
3522 case AMDGPU::V_FMAC_F16_e32:
3523 case AMDGPU::V_FMAC_F16_e64:
3524 case AMDGPU::V_FMAC_F16_t16_e64:
3525 case AMDGPU::V_FMAC_F16_fake16_e64:
3526 case AMDGPU::V_FMA_F16_e64:
3527 return ST.hasTrue16BitInsts() ? ST.useRealTrue16Insts()
3528 ? AMDGPU::V_FMAMK_F16_t16
3529 : AMDGPU::V_FMAMK_F16_fake16
3530 : AMDGPU::V_FMAMK_F16;
3531 case AMDGPU::V_FMAC_F64_e32:
3532 case AMDGPU::V_FMAC_F64_e64:
3533 case AMDGPU::V_FMA_F64_e64:
3534 return AMDGPU::V_FMAMK_F64;
3535 default:
3536 llvm_unreachable("invalid instruction");
3537 }
3538}
3539
3541 Register Reg, MachineRegisterInfo *MRI) const {
3542 int64_t Imm;
3543 if (!getConstValDefinedInReg(DefMI, Reg, Imm))
3544 return false;
3545
3546 const bool HasMultipleUses = !MRI->hasOneNonDBGUse(Reg);
3547
3548 assert(!DefMI.getOperand(0).getSubReg() && "Expected SSA form");
3549
3550 unsigned Opc = UseMI.getOpcode();
3551 if (Opc == AMDGPU::COPY) {
3552 assert(!UseMI.getOperand(0).getSubReg() && "Expected SSA form");
3553
3554 Register DstReg = UseMI.getOperand(0).getReg();
3555 Register UseSubReg = UseMI.getOperand(1).getSubReg();
3556
3557 const TargetRegisterClass *DstRC = RI.getRegClassForReg(*MRI, DstReg);
3558
3559 if (HasMultipleUses) {
3560 // TODO: This should fold in more cases with multiple use, but we need to
3561 // more carefully consider what those uses are.
3562 unsigned ImmDefSize = RI.getRegSizeInBits(*MRI->getRegClass(Reg));
3563
3564 // Avoid breaking up a 64-bit inline immediate into a subregister extract.
3565 if (UseSubReg != AMDGPU::NoSubRegister && ImmDefSize == 64)
3566 return false;
3567
3568 // Most of the time folding a 32-bit inline constant is free (though this
3569 // might not be true if we can't later fold it into a real user).
3570 //
3571 // FIXME: This isInlineConstant check is imprecise if
3572 // getConstValDefinedInReg handled the tricky non-mov cases.
3573 if (ImmDefSize == 32 &&
3575 return false;
3576 }
3577
3578 bool Is16Bit = UseSubReg != AMDGPU::NoSubRegister &&
3579 RI.getSubRegIdxSize(UseSubReg) == 16;
3580
3581 if (Is16Bit) {
3582 if (RI.hasVGPRs(DstRC))
3583 return false; // Do not clobber vgpr_hi16
3584
3585 if (DstReg.isVirtual() && UseSubReg != AMDGPU::lo16)
3586 return false;
3587 }
3588
3589 MachineFunction *MF = UseMI.getMF();
3590
3591 unsigned NewOpc = AMDGPU::INSTRUCTION_LIST_END;
3592 MCRegister MovDstPhysReg =
3593 DstReg.isPhysical() ? DstReg.asMCReg() : MCRegister();
3594
3595 std::optional<int64_t> SubRegImm = extractSubregFromImm(Imm, UseSubReg);
3596
3597 // TODO: Try to fold with AMDGPU::V_MOV_B16_t16_e64
3598 for (unsigned MovOp :
3599 {AMDGPU::S_MOV_B32, AMDGPU::V_MOV_B32_e32, AMDGPU::S_MOV_B64,
3600 AMDGPU::V_MOV_B64_PSEUDO, AMDGPU::V_ACCVGPR_WRITE_B32_e64}) {
3601 const MCInstrDesc &MovDesc = get(MovOp);
3602
3603 const TargetRegisterClass *MovDstRC = getRegClass(MovDesc, 0, &RI);
3604 if (Is16Bit) {
3605 // We just need to find a correctly sized register class, so the
3606 // subregister index compatibility doesn't matter since we're statically
3607 // extracting the immediate value.
3608 MovDstRC = RI.getMatchingSuperRegClass(MovDstRC, DstRC, AMDGPU::lo16);
3609 if (!MovDstRC)
3610 continue;
3611
3612 if (MovDstPhysReg) {
3613 // FIXME: We probably should not do this. If there is a live value in
3614 // the high half of the register, it will be corrupted.
3615 MovDstPhysReg =
3616 RI.getMatchingSuperReg(MovDstPhysReg, AMDGPU::lo16, MovDstRC);
3617 if (!MovDstPhysReg)
3618 continue;
3619 }
3620 }
3621
3622 // Result class isn't the right size, try the next instruction.
3623 if (MovDstPhysReg) {
3624 if (!MovDstRC->contains(MovDstPhysReg))
3625 return false;
3626 } else if (!MRI->constrainRegClass(DstReg, MovDstRC)) {
3627 // TODO: This will be overly conservative in the case of 16-bit virtual
3628 // SGPRs. We could hack up the virtual register uses to use a compatible
3629 // 32-bit class.
3630 continue;
3631 }
3632
3633 const MCOperandInfo &OpInfo = MovDesc.operands()[1];
3634
3635 // Ensure the interpreted immediate value is a valid operand in the new
3636 // mov.
3637 //
3638 // FIXME: isImmOperandLegal should have form that doesn't require existing
3639 // MachineInstr or MachineOperand
3640 if (!RI.opCanUseLiteralConstant(OpInfo.OperandType) &&
3641 !isInlineConstant(*SubRegImm, OpInfo.OperandType))
3642 break;
3643
3644 NewOpc = MovOp;
3645 break;
3646 }
3647
3648 if (NewOpc == AMDGPU::INSTRUCTION_LIST_END)
3649 return false;
3650
3651 if (Is16Bit) {
3652 UseMI.getOperand(0).setSubReg(AMDGPU::NoSubRegister);
3653 if (MovDstPhysReg)
3654 UseMI.getOperand(0).setReg(MovDstPhysReg);
3655 assert(UseMI.getOperand(1).getReg().isVirtual());
3656 }
3657
3658 const MCInstrDesc &NewMCID = get(NewOpc);
3659 UseMI.setDesc(NewMCID);
3660 UseMI.getOperand(1).ChangeToImmediate(*SubRegImm);
3661 UseMI.addImplicitDefUseOperands(*MF);
3662 return true;
3663 }
3664
3665 if (HasMultipleUses)
3666 return false;
3667
3668 if (Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 ||
3669 Opc == AMDGPU::V_MAD_F16_e64 || Opc == AMDGPU::V_MAC_F16_e64 ||
3670 Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64 ||
3671 Opc == AMDGPU::V_FMA_F16_e64 || Opc == AMDGPU::V_FMAC_F16_e64 ||
3672 Opc == AMDGPU::V_FMAC_F16_t16_e64 ||
3673 Opc == AMDGPU::V_FMAC_F16_fake16_e64 || Opc == AMDGPU::V_FMA_F64_e64 ||
3674 Opc == AMDGPU::V_FMAC_F64_e64) {
3675 // Don't fold if we are using source or output modifiers. The new VOP2
3676 // instructions don't have them.
3678 return false;
3679
3680 // If this is a free constant, there's no reason to do this.
3681 // TODO: We could fold this here instead of letting SIFoldOperands do it
3682 // later.
3683 int Src0Idx = getNamedOperandIdx(UseMI.getOpcode(), AMDGPU::OpName::src0);
3684
3685 // Any src operand can be used for the legality check.
3686 if (isInlineConstant(UseMI, Src0Idx, Imm))
3687 return false;
3688
3689 MachineOperand *Src0 = &UseMI.getOperand(Src0Idx);
3690
3691 MachineOperand *Src1 = getNamedOperand(UseMI, AMDGPU::OpName::src1);
3692 MachineOperand *Src2 = getNamedOperand(UseMI, AMDGPU::OpName::src2);
3693
3694 // Multiplied part is the constant: Use v_madmk_{f16, f32}.
3695 if ((Src0->isReg() && Src0->getReg() == Reg) ||
3696 (Src1->isReg() && Src1->getReg() == Reg)) {
3697 MachineOperand *RegSrc =
3698 Src1->isReg() && Src1->getReg() == Reg ? Src0 : Src1;
3699 if (!RegSrc->isReg())
3700 return false;
3701 if (RI.isSGPRClass(MRI->getRegClass(RegSrc->getReg())) &&
3702 ST.getConstantBusLimit(Opc) < 2)
3703 return false;
3704
3705 if (!Src2->isReg() || RI.isSGPRClass(MRI->getRegClass(Src2->getReg())))
3706 return false;
3707
3708 // If src2 is also a literal constant then we have to choose which one to
3709 // fold. In general it is better to choose madak so that the other literal
3710 // can be materialized in an sgpr instead of a vgpr:
3711 // s_mov_b32 s0, literal
3712 // v_madak_f32 v0, s0, v0, literal
3713 // Instead of:
3714 // v_mov_b32 v1, literal
3715 // v_madmk_f32 v0, v0, literal, v1
3716 MachineInstr *Def = MRI->getUniqueVRegDef(Src2->getReg());
3717 if (Def && Def->isMoveImmediate() &&
3718 !isInlineConstant(Def->getOperand(1)))
3719 return false;
3720
3721 unsigned NewOpc = getNewFMAMKInst(ST, Opc);
3722 if (pseudoToMCOpcode(NewOpc) == -1)
3723 return false;
3724
3725 // V_FMAMK_F16_t16 takes VGPR_16_Lo128 operands while V_FMAMK_F16_fake16
3726 // takes VGPR_32_Lo128 operands, so the rewrite would also require
3727 // restricting their register classes. For now just bail out.
3728 if (NewOpc == AMDGPU::V_FMAMK_F16_t16 ||
3729 NewOpc == AMDGPU::V_FMAMK_F16_fake16)
3730 return false;
3731
3732 const std::optional<int64_t> SubRegImm = extractSubregFromImm(
3733 Imm, RegSrc == Src1 ? Src0->getSubReg() : Src1->getSubReg());
3734
3735 // FIXME: This would be a lot easier if we could return a new instruction
3736 // instead of having to modify in place.
3737
3738 Register SrcReg = RegSrc->getReg();
3739 unsigned SrcSubReg = RegSrc->getSubReg();
3740 Src0->setReg(SrcReg);
3741 Src0->setSubReg(SrcSubReg);
3742 Src0->setIsKill(RegSrc->isKill());
3743
3744 if (Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64 ||
3745 Opc == AMDGPU::V_FMAC_F32_e64 || Opc == AMDGPU::V_FMAC_F16_t16_e64 ||
3746 Opc == AMDGPU::V_FMAC_F16_fake16_e64 ||
3747 Opc == AMDGPU::V_FMAC_F16_e64 || Opc == AMDGPU::V_FMAC_F64_e64)
3748 UseMI.untieRegOperand(
3749 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
3750
3751 Src1->ChangeToImmediate(*SubRegImm);
3752
3754 UseMI.setDesc(get(NewOpc));
3755
3756 bool DeleteDef = MRI->use_nodbg_empty(Reg);
3757 if (DeleteDef)
3758 DefMI.eraseFromParent();
3759
3760 return true;
3761 }
3762
3763 // Added part is the constant: Use v_madak_{f16, f32}.
3764 if (Src2->isReg() && Src2->getReg() == Reg) {
3765 if (ST.getConstantBusLimit(Opc) < 2) {
3766 // Not allowed to use constant bus for another operand.
3767 // We can however allow an inline immediate as src0.
3768 bool Src0Inlined = false;
3769 if (Src0->isReg()) {
3770 // Try to inline constant if possible.
3771 // If the Def moves immediate and the use is single
3772 // We are saving VGPR here.
3773 MachineInstr *Def = MRI->getUniqueVRegDef(Src0->getReg());
3774 if (Def && Def->isMoveImmediate() &&
3775 isInlineConstant(Def->getOperand(1)) &&
3776 MRI->hasOneUse(Src0->getReg())) {
3777 Src0->ChangeToImmediate(Def->getOperand(1).getImm());
3778 Src0Inlined = true;
3779 } else if (ST.getConstantBusLimit(Opc) <= 1 &&
3780 RI.isSGPRReg(*MRI, Src0->getReg())) {
3781 return false;
3782 }
3783 // VGPR is okay as Src0 - fallthrough
3784 }
3785
3786 if (Src1->isReg() && !Src0Inlined) {
3787 // We have one slot for inlinable constant so far - try to fill it
3788 MachineInstr *Def = MRI->getUniqueVRegDef(Src1->getReg());
3789 if (Def && Def->isMoveImmediate() &&
3790 isInlineConstant(Def->getOperand(1)) &&
3791 MRI->hasOneUse(Src1->getReg()) && commuteInstruction(UseMI))
3792 Src0->ChangeToImmediate(Def->getOperand(1).getImm());
3793 else if (RI.isSGPRReg(*MRI, Src1->getReg()))
3794 return false;
3795 // VGPR is okay as Src1 - fallthrough
3796 }
3797 }
3798
3799 unsigned NewOpc = getNewFMAAKInst(ST, Opc);
3800 if (pseudoToMCOpcode(NewOpc) == -1)
3801 return false;
3802
3803 // V_FMAAK_F16_t16 takes VGPR_16_Lo128 operands while V_FMAAK_F16_fake16
3804 // takes VGPR_32_Lo128 operands, so the rewrite would also require
3805 // restricting their register classes. For now just bail out.
3806 if (NewOpc == AMDGPU::V_FMAAK_F16_t16 ||
3807 NewOpc == AMDGPU::V_FMAAK_F16_fake16)
3808 return false;
3809
3810 // FIXME: This would be a lot easier if we could return a new instruction
3811 // instead of having to modify in place.
3812
3813 if (Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64 ||
3814 Opc == AMDGPU::V_FMAC_F32_e64 || Opc == AMDGPU::V_FMAC_F16_t16_e64 ||
3815 Opc == AMDGPU::V_FMAC_F16_fake16_e64 ||
3816 Opc == AMDGPU::V_FMAC_F16_e64 || Opc == AMDGPU::V_FMAC_F64_e64)
3817 UseMI.untieRegOperand(
3818 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
3819
3820 const std::optional<int64_t> SubRegImm =
3821 extractSubregFromImm(Imm, Src2->getSubReg());
3822
3823 // ChangingToImmediate adds Src2 back to the instruction.
3824 Src2->ChangeToImmediate(*SubRegImm);
3825
3826 // These come before src2.
3828 UseMI.setDesc(get(NewOpc));
3829 // It might happen that UseMI was commuted
3830 // and we now have SGPR as SRC1. If so 2 inlined
3831 // constant and SGPR are illegal.
3833
3834 bool DeleteDef = MRI->use_nodbg_empty(Reg);
3835 if (DeleteDef)
3836 DefMI.eraseFromParent();
3837
3838 return true;
3839 }
3840 }
3841
3842 return false;
3843}
3844
3845static bool
3848 if (BaseOps1.size() != BaseOps2.size())
3849 return false;
3850 for (size_t I = 0, E = BaseOps1.size(); I < E; ++I) {
3851 if (!BaseOps1[I]->isIdenticalTo(*BaseOps2[I]))
3852 return false;
3853 }
3854 return true;
3855}
3856
3857static bool offsetsDoNotOverlap(LocationSize WidthA, int OffsetA,
3858 LocationSize WidthB, int OffsetB) {
3859 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
3860 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
3861 LocationSize LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
3862 return LowWidth.hasValue() &&
3863 LowOffset + (int)LowWidth.getValue() <= HighOffset;
3864}
3865
3866bool SIInstrInfo::checkInstOffsetsDoNotOverlap(const MachineInstr &MIa,
3867 const MachineInstr &MIb) const {
3868 SmallVector<const MachineOperand *, 4> BaseOps0, BaseOps1;
3869 int64_t Offset0, Offset1;
3870 LocationSize Dummy0 = LocationSize::precise(0);
3871 LocationSize Dummy1 = LocationSize::precise(0);
3872 bool Offset0IsScalable, Offset1IsScalable;
3873 if (!getMemOperandsWithOffsetWidth(MIa, BaseOps0, Offset0, Offset0IsScalable,
3874 Dummy0, &RI) ||
3875 !getMemOperandsWithOffsetWidth(MIb, BaseOps1, Offset1, Offset1IsScalable,
3876 Dummy1, &RI))
3877 return false;
3878
3879 if (!memOpsHaveSameBaseOperands(BaseOps0, BaseOps1))
3880 return false;
3881
3882 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) {
3883 // FIXME: Handle ds_read2 / ds_write2.
3884 return false;
3885 }
3886 LocationSize Width0 = MIa.memoperands().front()->getSize();
3887 LocationSize Width1 = MIb.memoperands().front()->getSize();
3888 return offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1);
3889}
3890
3892 const MachineInstr &MIb) const {
3893 assert(MIa.mayLoadOrStore() &&
3894 "MIa must load from or modify a memory location");
3895 assert(MIb.mayLoadOrStore() &&
3896 "MIb must load from or modify a memory location");
3897
3899 return false;
3900
3901 // XXX - Can we relax this between address spaces?
3902 if (MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
3903 return false;
3904
3905 if (isLDSDMA(MIa) || isLDSDMA(MIb))
3906 return false;
3907
3908 // TODO: Should we check the address space from the MachineMemOperand? That
3909 // would allow us to distinguish objects we know don't alias based on the
3910 // underlying address space, even if it was lowered to a different one,
3911 // e.g. private accesses lowered to use MUBUF instructions on a scratch
3912 // buffer.
3913 if (isDS(MIa)) {
3914 if (isDS(MIb))
3915 return checkInstOffsetsDoNotOverlap(MIa, MIb);
3916
3917 return !isFLAT(MIb) || isSegmentSpecificFLAT(MIb);
3918 }
3919
3920 if (isMUBUF(MIa) || isMTBUF(MIa)) {
3921 if (isMUBUF(MIb) || isMTBUF(MIb))
3922 return checkInstOffsetsDoNotOverlap(MIa, MIb);
3923
3924 if (isFLAT(MIb))
3925 return isFLATScratch(MIb);
3926
3927 return !isSMRD(MIb);
3928 }
3929
3930 if (isSMRD(MIa)) {
3931 if (isSMRD(MIb))
3932 return checkInstOffsetsDoNotOverlap(MIa, MIb);
3933
3934 if (isFLAT(MIb))
3935 return isFLATScratch(MIb);
3936
3937 return !isMUBUF(MIb) && !isMTBUF(MIb);
3938 }
3939
3940 if (isFLAT(MIa)) {
3941 if (isFLAT(MIb)) {
3942 if ((isFLATScratch(MIa) && isFLATGlobal(MIb)) ||
3943 (isFLATGlobal(MIa) && isFLATScratch(MIb)))
3944 return true;
3945
3946 return checkInstOffsetsDoNotOverlap(MIa, MIb);
3947 }
3948
3949 return false;
3950 }
3951
3952 return false;
3953}
3954
3956 int64_t &Imm, MachineInstr **DefMI = nullptr) {
3957 if (Reg.isPhysical())
3958 return false;
3959 auto *Def = MRI.getUniqueVRegDef(Reg);
3960 if (Def && SIInstrInfo::isFoldableCopy(*Def) && Def->getOperand(1).isImm()) {
3961 Imm = Def->getOperand(1).getImm();
3962 if (DefMI)
3963 *DefMI = Def;
3964 return true;
3965 }
3966 return false;
3967}
3968
3969static bool getFoldableImm(const MachineOperand *MO, int64_t &Imm,
3970 MachineInstr **DefMI = nullptr) {
3971 if (!MO->isReg())
3972 return false;
3973 const MachineFunction *MF = MO->getParent()->getParent()->getParent();
3974 const MachineRegisterInfo &MRI = MF->getRegInfo();
3975 return getFoldableImm(MO->getReg(), MRI, Imm, DefMI);
3976}
3977
3979 MachineInstr &NewMI) {
3980 if (LV) {
3981 unsigned NumOps = MI.getNumOperands();
3982 for (unsigned I = 1; I < NumOps; ++I) {
3983 MachineOperand &Op = MI.getOperand(I);
3984 if (Op.isReg() && Op.isKill())
3985 LV->replaceKillInstruction(Op.getReg(), MI, NewMI);
3986 }
3987 }
3988}
3989
3990static unsigned getNewFMAInst(const GCNSubtarget &ST, unsigned Opc) {
3991 switch (Opc) {
3992 case AMDGPU::V_MAC_F16_e32:
3993 case AMDGPU::V_MAC_F16_e64:
3994 return AMDGPU::V_MAD_F16_e64;
3995 case AMDGPU::V_MAC_F32_e32:
3996 case AMDGPU::V_MAC_F32_e64:
3997 return AMDGPU::V_MAD_F32_e64;
3998 case AMDGPU::V_MAC_LEGACY_F32_e32:
3999 case AMDGPU::V_MAC_LEGACY_F32_e64:
4000 return AMDGPU::V_MAD_LEGACY_F32_e64;
4001 case AMDGPU::V_FMAC_LEGACY_F32_e32:
4002 case AMDGPU::V_FMAC_LEGACY_F32_e64:
4003 return AMDGPU::V_FMA_LEGACY_F32_e64;
4004 case AMDGPU::V_FMAC_F16_e32:
4005 case AMDGPU::V_FMAC_F16_e64:
4006 case AMDGPU::V_FMAC_F16_t16_e64:
4007 case AMDGPU::V_FMAC_F16_fake16_e64:
4008 return ST.hasTrue16BitInsts() ? ST.useRealTrue16Insts()
4009 ? AMDGPU::V_FMA_F16_gfx9_t16_e64
4010 : AMDGPU::V_FMA_F16_gfx9_fake16_e64
4011 : AMDGPU::V_FMA_F16_gfx9_e64;
4012 case AMDGPU::V_FMAC_F32_e32:
4013 case AMDGPU::V_FMAC_F32_e64:
4014 return AMDGPU::V_FMA_F32_e64;
4015 case AMDGPU::V_FMAC_F64_e32:
4016 case AMDGPU::V_FMAC_F64_e64:
4017 return AMDGPU::V_FMA_F64_e64;
4018 default:
4019 llvm_unreachable("invalid instruction");
4020 }
4021}
4022
4024 LiveVariables *LV,
4025 LiveIntervals *LIS) const {
4026 MachineBasicBlock &MBB = *MI.getParent();
4027 unsigned Opc = MI.getOpcode();
4028
4029 // Handle MFMA.
4030 int NewMFMAOpc = AMDGPU::getMFMAEarlyClobberOp(Opc);
4031 if (NewMFMAOpc != -1) {
4033 BuildMI(MBB, MI, MI.getDebugLoc(), get(NewMFMAOpc));
4034 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I)
4035 MIB.add(MI.getOperand(I));
4036 updateLiveVariables(LV, MI, *MIB);
4037 if (LIS) {
4038 LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4039 // SlotIndex of defs needs to be updated when converting to early-clobber
4040 MachineOperand &Def = MIB->getOperand(0);
4041 if (Def.isEarlyClobber() && Def.isReg() &&
4042 LIS->hasInterval(Def.getReg())) {
4043 SlotIndex OldIndex = LIS->getInstructionIndex(*MIB).getRegSlot(false);
4044 SlotIndex NewIndex = LIS->getInstructionIndex(*MIB).getRegSlot(true);
4045 auto &LI = LIS->getInterval(Def.getReg());
4046 auto UpdateDefIndex = [&](LiveRange &LR) {
4047 auto *S = LR.find(OldIndex);
4048 if (S != LR.end() && S->start == OldIndex) {
4049 assert(S->valno && S->valno->def == OldIndex);
4050 S->start = NewIndex;
4051 S->valno->def = NewIndex;
4052 }
4053 };
4054 UpdateDefIndex(LI);
4055 for (auto &SR : LI.subranges())
4056 UpdateDefIndex(SR);
4057 }
4058 }
4059 return MIB;
4060 }
4061
4062 if (SIInstrInfo::isWMMA(MI)) {
4063 unsigned NewOpc = AMDGPU::mapWMMA2AddrTo3AddrOpcode(MI.getOpcode());
4064 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4065 .setMIFlags(MI.getFlags());
4066 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I)
4067 MIB->addOperand(MI.getOperand(I));
4068
4069 updateLiveVariables(LV, MI, *MIB);
4070 if (LIS)
4071 LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4072
4073 return MIB;
4074 }
4075
4076 assert(Opc != AMDGPU::V_FMAC_F16_t16_e32 &&
4077 Opc != AMDGPU::V_FMAC_F16_fake16_e32 &&
4078 "V_FMAC_F16_t16/fake16_e32 is not supported and not expected to be "
4079 "present pre-RA");
4080
4081 // Handle MAC/FMAC.
4082 bool IsF64 = Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64;
4083 bool IsLegacy = Opc == AMDGPU::V_MAC_LEGACY_F32_e32 ||
4084 Opc == AMDGPU::V_MAC_LEGACY_F32_e64 ||
4085 Opc == AMDGPU::V_FMAC_LEGACY_F32_e32 ||
4086 Opc == AMDGPU::V_FMAC_LEGACY_F32_e64;
4087 bool Src0Literal = false;
4088
4089 switch (Opc) {
4090 default:
4091 return nullptr;
4092 case AMDGPU::V_MAC_F16_e64:
4093 case AMDGPU::V_FMAC_F16_e64:
4094 case AMDGPU::V_FMAC_F16_t16_e64:
4095 case AMDGPU::V_FMAC_F16_fake16_e64:
4096 case AMDGPU::V_MAC_F32_e64:
4097 case AMDGPU::V_MAC_LEGACY_F32_e64:
4098 case AMDGPU::V_FMAC_F32_e64:
4099 case AMDGPU::V_FMAC_LEGACY_F32_e64:
4100 case AMDGPU::V_FMAC_F64_e64:
4101 break;
4102 case AMDGPU::V_MAC_F16_e32:
4103 case AMDGPU::V_FMAC_F16_e32:
4104 case AMDGPU::V_MAC_F32_e32:
4105 case AMDGPU::V_MAC_LEGACY_F32_e32:
4106 case AMDGPU::V_FMAC_F32_e32:
4107 case AMDGPU::V_FMAC_LEGACY_F32_e32:
4108 case AMDGPU::V_FMAC_F64_e32: {
4109 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
4110 AMDGPU::OpName::src0);
4111 const MachineOperand *Src0 = &MI.getOperand(Src0Idx);
4112 if (!Src0->isReg() && !Src0->isImm())
4113 return nullptr;
4114
4115 if (Src0->isImm() && !isInlineConstant(MI, Src0Idx, *Src0))
4116 Src0Literal = true;
4117
4118 break;
4119 }
4120 }
4121
4123 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
4124 const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0);
4125 const MachineOperand *Src0Mods =
4126 getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
4127 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
4128 const MachineOperand *Src1Mods =
4129 getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
4130 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
4131 const MachineOperand *Src2Mods =
4132 getNamedOperand(MI, AMDGPU::OpName::src2_modifiers);
4133 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp);
4134 const MachineOperand *Omod = getNamedOperand(MI, AMDGPU::OpName::omod);
4135 const MachineOperand *OpSel = getNamedOperand(MI, AMDGPU::OpName::op_sel);
4136
4137 if (!Src0Mods && !Src1Mods && !Src2Mods && !Clamp && !Omod && !IsLegacy &&
4138 (!IsF64 || ST.hasFmaakFmamkF64Insts()) &&
4139 // If we have an SGPR input, we will violate the constant bus restriction.
4140 (ST.getConstantBusLimit(Opc) > 1 || !Src0->isReg() ||
4141 !RI.isSGPRReg(MBB.getParent()->getRegInfo(), Src0->getReg()))) {
4143 const auto killDef = [&]() -> void {
4144 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
4145 // The only user is the instruction which will be killed.
4146 Register DefReg = DefMI->getOperand(0).getReg();
4147
4148 if (MRI.hasOneNonDBGUse(DefReg)) {
4149 // We cannot just remove the DefMI here, calling pass will crash.
4150 DefMI->setDesc(get(AMDGPU::IMPLICIT_DEF));
4151 DefMI->getOperand(0).setIsDead(true);
4152 for (unsigned I = DefMI->getNumOperands() - 1; I != 0; --I)
4153 DefMI->removeOperand(I);
4154 if (LV)
4155 LV->getVarInfo(DefReg).AliveBlocks.clear();
4156 }
4157
4158 if (LIS) {
4159 LiveInterval &DefLI = LIS->getInterval(DefReg);
4160
4161 // We cannot delete the original instruction here, so hack out the use
4162 // in the original instruction with a dummy register so we can use
4163 // shrinkToUses to deal with any multi-use edge cases. Other targets do
4164 // not have the complexity of deleting a use to consider here.
4165 Register DummyReg = MRI.cloneVirtualRegister(DefReg);
4166 for (MachineOperand &MIOp : MI.uses()) {
4167 if (MIOp.isReg() && MIOp.getReg() == DefReg) {
4168 MIOp.setIsUndef(true);
4169 MIOp.setReg(DummyReg);
4170 }
4171 }
4172
4173 LIS->shrinkToUses(&DefLI);
4174 }
4175 };
4176
4177 int64_t Imm;
4178 if (!Src0Literal && getFoldableImm(Src2, Imm, &DefMI)) {
4179 unsigned NewOpc = getNewFMAAKInst(ST, Opc);
4180 if (pseudoToMCOpcode(NewOpc) != -1) {
4181 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4182 .add(*Dst)
4183 .add(*Src0)
4184 .add(*Src1)
4185 .addImm(Imm)
4186 .setMIFlags(MI.getFlags());
4187 updateLiveVariables(LV, MI, *MIB);
4188 if (LIS)
4189 LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4190 killDef();
4191 return MIB;
4192 }
4193 }
4194 unsigned NewOpc = getNewFMAMKInst(ST, Opc);
4195 if (!Src0Literal && getFoldableImm(Src1, Imm, &DefMI)) {
4196 if (pseudoToMCOpcode(NewOpc) != -1) {
4197 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4198 .add(*Dst)
4199 .add(*Src0)
4200 .addImm(Imm)
4201 .add(*Src2)
4202 .setMIFlags(MI.getFlags());
4203 updateLiveVariables(LV, MI, *MIB);
4204
4205 if (LIS)
4206 LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4207 killDef();
4208 return MIB;
4209 }
4210 }
4211 if (Src0Literal || getFoldableImm(Src0, Imm, &DefMI)) {
4212 if (Src0Literal) {
4213 Imm = Src0->getImm();
4214 DefMI = nullptr;
4215 }
4216 if (pseudoToMCOpcode(NewOpc) != -1 &&
4218 MI, AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::src0),
4219 Src1)) {
4220 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4221 .add(*Dst)
4222 .add(*Src1)
4223 .addImm(Imm)
4224 .add(*Src2)
4225 .setMIFlags(MI.getFlags());
4226 updateLiveVariables(LV, MI, *MIB);
4227
4228 if (LIS)
4229 LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4230 if (DefMI)
4231 killDef();
4232 return MIB;
4233 }
4234 }
4235 }
4236
4237 // VOP2 mac/fmac with a literal operand cannot be converted to VOP3 mad/fma
4238 // if VOP3 does not allow a literal operand.
4239 if (Src0Literal && !ST.hasVOP3Literal())
4240 return nullptr;
4241
4242 unsigned NewOpc = getNewFMAInst(ST, Opc);
4243
4244 if (pseudoToMCOpcode(NewOpc) == -1)
4245 return nullptr;
4246
4247 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4248 .add(*Dst)
4249 .addImm(Src0Mods ? Src0Mods->getImm() : 0)
4250 .add(*Src0)
4251 .addImm(Src1Mods ? Src1Mods->getImm() : 0)
4252 .add(*Src1)
4253 .addImm(Src2Mods ? Src2Mods->getImm() : 0)
4254 .add(*Src2)
4255 .addImm(Clamp ? Clamp->getImm() : 0)
4256 .addImm(Omod ? Omod->getImm() : 0)
4257 .setMIFlags(MI.getFlags());
4258 if (AMDGPU::hasNamedOperand(NewOpc, AMDGPU::OpName::op_sel))
4259 MIB.addImm(OpSel ? OpSel->getImm() : 0);
4260 updateLiveVariables(LV, MI, *MIB);
4261 if (LIS)
4262 LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4263 return MIB;
4264}
4265
4266// It's not generally safe to move VALU instructions across these since it will
4267// start using the register as a base index rather than directly.
4268// XXX - Why isn't hasSideEffects sufficient for these?
4270 switch (MI.getOpcode()) {
4271 case AMDGPU::S_SET_GPR_IDX_ON:
4272 case AMDGPU::S_SET_GPR_IDX_MODE:
4273 case AMDGPU::S_SET_GPR_IDX_OFF:
4274 return true;
4275 default:
4276 return false;
4277 }
4278}
4279
4281 const MachineBasicBlock *MBB,
4282 const MachineFunction &MF) const {
4283 // Skipping the check for SP writes in the base implementation. The reason it
4284 // was added was apparently due to compile time concerns.
4285 //
4286 // TODO: Do we really want this barrier? It triggers unnecessary hazard nops
4287 // but is probably avoidable.
4288
4289 // Copied from base implementation.
4290 // Terminators and labels can't be scheduled around.
4291 if (MI.isTerminator() || MI.isPosition())
4292 return true;
4293
4294 // INLINEASM_BR can jump to another block
4295 if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
4296 return true;
4297
4298 if (MI.getOpcode() == AMDGPU::SCHED_BARRIER && MI.getOperand(0).getImm() == 0)
4299 return true;
4300
4301 // Target-independent instructions do not have an implicit-use of EXEC, even
4302 // when they operate on VGPRs. Treating EXEC modifications as scheduling
4303 // boundaries prevents incorrect movements of such instructions.
4304 return MI.modifiesRegister(AMDGPU::EXEC, &RI) ||
4305 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
4306 MI.getOpcode() == AMDGPU::S_SETREG_B32 ||
4307 MI.getOpcode() == AMDGPU::S_SETPRIO ||
4308 MI.getOpcode() == AMDGPU::S_SETPRIO_INC_WG ||
4310}
4311
4313 return Opcode == AMDGPU::DS_ORDERED_COUNT ||
4314 Opcode == AMDGPU::DS_ADD_GS_REG_RTN ||
4315 Opcode == AMDGPU::DS_SUB_GS_REG_RTN || isGWS(Opcode);
4316}
4317
4319 if (!isFLAT(MI) || isFLATGlobal(MI))
4320 return false;
4321
4322 // If scratch is not initialized, we can never access it.
4323 if (MI.getMF()->getFunction().hasFnAttribute("amdgpu-no-flat-scratch-init"))
4324 return false;
4325
4326 // SCRATCH instructions always access scratch.
4327 if (isFLATScratch(MI))
4328 return true;
4329
4330 // If there are no memory operands then conservatively assume the flat
4331 // operation may access scratch.
4332 if (MI.memoperands_empty())
4333 return true;
4334
4335 // See if any memory operand specifies an address space that involves scratch.
4336 return any_of(MI.memoperands(), [](const MachineMemOperand *Memop) {
4337 unsigned AS = Memop->getAddrSpace();
4338 if (AS == AMDGPUAS::FLAT_ADDRESS) {
4339 const MDNode *MD = Memop->getAAInfo().NoAliasAddrSpace;
4340 return !MD || !AMDGPU::hasValueInRangeLikeMetadata(
4341 *MD, AMDGPUAS::PRIVATE_ADDRESS);
4342 }
4343 return AS == AMDGPUAS::PRIVATE_ADDRESS;
4344 });
4345}
4346
4348 // Skip the full operand and register alias search modifiesRegister
4349 // does. There's only a handful of instructions that touch this, it's only an
4350 // implicit def, and doesn't alias any other registers.
4351 return is_contained(MI.getDesc().implicit_defs(), AMDGPU::MODE);
4352}
4353
4355 unsigned Opcode = MI.getOpcode();
4356
4357 if (MI.mayStore() && isSMRD(MI))
4358 return true; // scalar store or atomic
4359
4360 // This will terminate the function when other lanes may need to continue.
4361 if (MI.isReturn())
4362 return true;
4363
4364 // These instructions cause shader I/O that may cause hardware lockups
4365 // when executed with an empty EXEC mask.
4366 //
4367 // Note: exp with VM = DONE = 0 is automatically skipped by hardware when
4368 // EXEC = 0, but checking for that case here seems not worth it
4369 // given the typical code patterns.
4370 if (Opcode == AMDGPU::S_SENDMSG || Opcode == AMDGPU::S_SENDMSGHALT ||
4371 isEXP(Opcode) || Opcode == AMDGPU::DS_ORDERED_COUNT ||
4372 Opcode == AMDGPU::S_TRAP || Opcode == AMDGPU::S_WAIT_EVENT)
4373 return true;
4374
4375 if (MI.isCall() || MI.isInlineAsm())
4376 return true; // conservative assumption
4377
4378 // Assume that barrier interactions are only intended with active lanes.
4379 if (isBarrier(Opcode))
4380 return true;
4381
4382 // A mode change is a scalar operation that influences vector instructions.
4384 return true;
4385
4386 // These are like SALU instructions in terms of effects, so it's questionable
4387 // whether we should return true for those.
4388 //
4389 // However, executing them with EXEC = 0 causes them to operate on undefined
4390 // data, which we avoid by returning true here.
4391 if (Opcode == AMDGPU::V_READFIRSTLANE_B32 ||
4392 Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32 ||
4393 Opcode == AMDGPU::SI_RESTORE_S32_FROM_VGPR ||
4394 Opcode == AMDGPU::SI_SPILL_S32_TO_VGPR)
4395 return true;
4396
4397 return false;
4398}
4399
4401 const MachineInstr &MI) const {
4402 if (MI.isMetaInstruction())
4403 return false;
4404
4405 // This won't read exec if this is an SGPR->SGPR copy.
4406 if (MI.isCopyLike()) {
4407 if (!RI.isSGPRReg(MRI, MI.getOperand(0).getReg()))
4408 return true;
4409
4410 // Make sure this isn't copying exec as a normal operand
4411 return MI.readsRegister(AMDGPU::EXEC, &RI);
4412 }
4413
4414 // Make a conservative assumption about the callee.
4415 if (MI.isCall())
4416 return true;
4417
4418 // Be conservative with any unhandled generic opcodes.
4419 if (!isTargetSpecificOpcode(MI.getOpcode()))
4420 return true;
4421
4422 return !isSALU(MI) || MI.readsRegister(AMDGPU::EXEC, &RI);
4423}
4424
4425bool SIInstrInfo::isInlineConstant(const APInt &Imm) const {
4426 switch (Imm.getBitWidth()) {
4427 case 1: // This likely will be a condition code mask.
4428 return true;
4429
4430 case 32:
4431 return AMDGPU::isInlinableLiteral32(Imm.getSExtValue(),
4432 ST.hasInv2PiInlineImm());
4433 case 64:
4434 return AMDGPU::isInlinableLiteral64(Imm.getSExtValue(),
4435 ST.hasInv2PiInlineImm());
4436 case 16:
4437 return ST.has16BitInsts() &&
4438 AMDGPU::isInlinableLiteralI16(Imm.getSExtValue(),
4439 ST.hasInv2PiInlineImm());
4440 default:
4441 llvm_unreachable("invalid bitwidth");
4442 }
4443}
4444
4446 APInt IntImm = Imm.bitcastToAPInt();
4447 int64_t IntImmVal = IntImm.getSExtValue();
4448 bool HasInv2Pi = ST.hasInv2PiInlineImm();
4449 switch (APFloat::SemanticsToEnum(Imm.getSemantics())) {
4450 default:
4451 llvm_unreachable("invalid fltSemantics");
4454 return isInlineConstant(IntImm);
4456 return ST.has16BitInsts() &&
4457 AMDGPU::isInlinableLiteralBF16(IntImmVal, HasInv2Pi);
4459 return ST.has16BitInsts() &&
4460 AMDGPU::isInlinableLiteralFP16(IntImmVal, HasInv2Pi);
4461 }
4462}
4463
4464bool SIInstrInfo::isInlineConstant(int64_t Imm, uint8_t OperandType) const {
4465 // MachineOperand provides no way to tell the true operand size, since it only
4466 // records a 64-bit value. We need to know the size to determine if a 32-bit
4467 // floating point immediate bit pattern is legal for an integer immediate. It
4468 // would be for any 32-bit integer operand, but would not be for a 64-bit one.
4469 switch (OperandType) {
4479 int32_t Trunc = static_cast<int32_t>(Imm);
4480 return AMDGPU::isInlinableLiteral32(Trunc, ST.hasInv2PiInlineImm());
4481 }
4487 return AMDGPU::isInlinableLiteral64(Imm, ST.hasInv2PiInlineImm());
4490 // We would expect inline immediates to not be concerned with an integer/fp
4491 // distinction. However, in the case of 16-bit integer operations, the
4492 // "floating point" values appear to not work. It seems read the low 16-bits
4493 // of 32-bit immediates, which happens to always work for the integer
4494 // values.
4495 //
4496 // See llvm bugzilla 46302.
4497 //
4498 // TODO: Theoretically we could use op-sel to use the high bits of the
4499 // 32-bit FP values.
4511 return false;
4514 if (isInt<16>(Imm) || isUInt<16>(Imm)) {
4515 // A few special case instructions have 16-bit operands on subtargets
4516 // where 16-bit instructions are not legal.
4517 // TODO: Do the 32-bit immediates work? We shouldn't really need to handle
4518 // constants in these cases
4519 int16_t Trunc = static_cast<int16_t>(Imm);
4520 return ST.has16BitInsts() &&
4521 AMDGPU::isInlinableLiteralFP16(Trunc, ST.hasInv2PiInlineImm());
4522 }
4523
4524 return false;
4525 }
4528 if (isInt<16>(Imm) || isUInt<16>(Imm)) {
4529 int16_t Trunc = static_cast<int16_t>(Imm);
4530 return ST.has16BitInsts() &&
4531 AMDGPU::isInlinableLiteralBF16(Trunc, ST.hasInv2PiInlineImm());
4532 }
4533 return false;
4534 }
4538 return false;
4540 return isLegalAV64PseudoImm(Imm);
4543 // Always embedded in the instruction for free.
4544 return true;
4554 // Just ignore anything else.
4555 return true;
4556 default:
4557 llvm_unreachable("invalid operand type");
4558 }
4559}
4560
4561static bool compareMachineOp(const MachineOperand &Op0,
4562 const MachineOperand &Op1) {
4563 if (Op0.getType() != Op1.getType())
4564 return false;
4565
4566 switch (Op0.getType()) {
4568 return Op0.getReg() == Op1.getReg();
4570 return Op0.getImm() == Op1.getImm();
4571 default:
4572 llvm_unreachable("Didn't expect to be comparing these operand types");
4573 }
4574}
4575
4577 const MCOperandInfo &OpInfo) const {
4578 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE)
4579 return true;
4580
4581 if (!RI.opCanUseLiteralConstant(OpInfo.OperandType))
4582 return false;
4583
4584 if (!isVOP3(InstDesc) || !AMDGPU::isSISrcOperand(OpInfo))
4585 return true;
4586
4587 return ST.hasVOP3Literal();
4588}
4589
4590bool SIInstrInfo::isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo,
4591 int64_t ImmVal) const {
4592 const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo];
4593 if (isInlineConstant(ImmVal, OpInfo.OperandType)) {
4594 if (isMAI(InstDesc) && ST.hasMFMAInlineLiteralBug() &&
4595 OpNo == (unsigned)AMDGPU::getNamedOperandIdx(InstDesc.getOpcode(),
4596 AMDGPU::OpName::src2))
4597 return false;
4598 return RI.opCanUseInlineConstant(OpInfo.OperandType);
4599 }
4600
4601 return isLiteralOperandLegal(InstDesc, OpInfo);
4602}
4603
4604bool SIInstrInfo::isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo,
4605 const MachineOperand &MO) const {
4606 if (MO.isImm())
4607 return isImmOperandLegal(InstDesc, OpNo, MO.getImm());
4608
4609 assert((MO.isTargetIndex() || MO.isFI() || MO.isGlobal()) &&
4610 "unexpected imm-like operand kind");
4611 const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo];
4612 return isLiteralOperandLegal(InstDesc, OpInfo);
4613}
4614
4616 // 2 32-bit inline constants packed into one.
4617 return AMDGPU::isInlinableLiteral32(Lo_32(Imm), ST.hasInv2PiInlineImm()) &&
4618 AMDGPU::isInlinableLiteral32(Hi_32(Imm), ST.hasInv2PiInlineImm());
4619}
4620
4621bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const {
4622 // GFX90A does not have V_MUL_LEGACY_F32_e32.
4623 if (Opcode == AMDGPU::V_MUL_LEGACY_F32_e64 && ST.hasGFX90AInsts())
4624 return false;
4625
4626 int Op32 = AMDGPU::getVOPe32(Opcode);
4627 if (Op32 == -1)
4628 return false;
4629
4630 return pseudoToMCOpcode(Op32) != -1;
4631}
4632
4633bool SIInstrInfo::hasModifiers(unsigned Opcode) const {
4634 // The src0_modifier operand is present on all instructions
4635 // that have modifiers.
4636
4637 return AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src0_modifiers);
4638}
4639
4641 AMDGPU::OpName OpName) const {
4642 const MachineOperand *Mods = getNamedOperand(MI, OpName);
4643 return Mods && Mods->getImm();
4644}
4645
4647 return any_of(ModifierOpNames,
4648 [&](AMDGPU::OpName Name) { return hasModifiersSet(MI, Name); });
4649}
4650
4652 const MachineRegisterInfo &MRI) const {
4653 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
4654 // Can't shrink instruction with three operands.
4655 if (Src2) {
4656 switch (MI.getOpcode()) {
4657 default: return false;
4658
4659 case AMDGPU::V_ADDC_U32_e64:
4660 case AMDGPU::V_SUBB_U32_e64:
4661 case AMDGPU::V_SUBBREV_U32_e64: {
4662 const MachineOperand *Src1
4663 = getNamedOperand(MI, AMDGPU::OpName::src1);
4664 if (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()))
4665 return false;
4666 // Additional verification is needed for sdst/src2.
4667 return true;
4668 }
4669 case AMDGPU::V_MAC_F16_e64:
4670 case AMDGPU::V_MAC_F32_e64:
4671 case AMDGPU::V_MAC_LEGACY_F32_e64:
4672 case AMDGPU::V_FMAC_F16_e64:
4673 case AMDGPU::V_FMAC_F16_t16_e64:
4674 case AMDGPU::V_FMAC_F16_fake16_e64:
4675 case AMDGPU::V_FMAC_F32_e64:
4676 case AMDGPU::V_FMAC_F64_e64:
4677 case AMDGPU::V_FMAC_LEGACY_F32_e64:
4678 if (!Src2->isReg() || !RI.isVGPR(MRI, Src2->getReg()) ||
4679 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers))
4680 return false;
4681 break;
4682
4683 case AMDGPU::V_CNDMASK_B32_e64:
4684 break;
4685 }
4686 }
4687
4688 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
4689 if (Src1 && (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()) ||
4690 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers)))
4691 return false;
4692
4693 // We don't need to check src0, all input types are legal, so just make sure
4694 // src0 isn't using any modifiers.
4695 if (hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers))
4696 return false;
4697
4698 // Can it be shrunk to a valid 32 bit opcode?
4699 if (!hasVALU32BitEncoding(MI.getOpcode()))
4700 return false;
4701
4702 // Check output modifiers
4703 return !hasModifiersSet(MI, AMDGPU::OpName::omod) &&
4704 !hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
4705 !hasModifiersSet(MI, AMDGPU::OpName::byte_sel) &&
4706 // TODO: Can we avoid checking bound_ctrl/fi here?
4707 // They are only used by permlane*_swap special case.
4708 !hasModifiersSet(MI, AMDGPU::OpName::bound_ctrl) &&
4709 !hasModifiersSet(MI, AMDGPU::OpName::fi);
4710}
4711
4712// Set VCC operand with all flags from \p Orig, except for setting it as
4713// implicit.
4715 const MachineOperand &Orig) {
4716
4717 for (MachineOperand &Use : MI.implicit_operands()) {
4718 if (Use.isUse() &&
4719 (Use.getReg() == AMDGPU::VCC || Use.getReg() == AMDGPU::VCC_LO)) {
4720 Use.setIsUndef(Orig.isUndef());
4721 Use.setIsKill(Orig.isKill());
4722 return;
4723 }
4724 }
4725}
4726
4728 unsigned Op32) const {
4729 MachineBasicBlock *MBB = MI.getParent();
4730
4731 const MCInstrDesc &Op32Desc = get(Op32);
4732 MachineInstrBuilder Inst32 =
4733 BuildMI(*MBB, MI, MI.getDebugLoc(), Op32Desc)
4734 .setMIFlags(MI.getFlags());
4735
4736 // Add the dst operand if the 32-bit encoding also has an explicit $vdst.
4737 // For VOPC instructions, this is replaced by an implicit def of vcc.
4738
4739 // We assume the defs of the shrunk opcode are in the same order, and the
4740 // shrunk opcode loses the last def (SGPR def, in the VOP3->VOPC case).
4741 for (int I = 0, E = Op32Desc.getNumDefs(); I != E; ++I)
4742 Inst32.add(MI.getOperand(I));
4743
4744 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
4745
4746 int Idx = MI.getNumExplicitDefs();
4747 for (const MachineOperand &Use : MI.explicit_uses()) {
4748 int OpTy = MI.getDesc().operands()[Idx++].OperandType;
4750 continue;
4751
4752 if (&Use == Src2) {
4753 if (AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2) == -1) {
4754 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
4755 // replaced with an implicit read of vcc or vcc_lo. The implicit read
4756 // of vcc was already added during the initial BuildMI, but we
4757 // 1) may need to change vcc to vcc_lo to preserve the original register
4758 // 2) have to preserve the original flags.
4759 copyFlagsToImplicitVCC(*Inst32, *Src2);
4760 continue;
4761 }
4762 }
4763
4764 Inst32.add(Use);
4765 }
4766
4767 // FIXME: Losing implicit operands
4768 fixImplicitOperands(*Inst32);
4769 return Inst32;
4770}
4771
4773 // Null is free
4774 Register Reg = RegOp.getReg();
4775 if (Reg == AMDGPU::SGPR_NULL || Reg == AMDGPU::SGPR_NULL64)
4776 return false;
4777
4778 // SGPRs use the constant bus
4779
4780 // FIXME: implicit registers that are not part of the MCInstrDesc's implicit
4781 // physical register operands should also count, except for exec.
4782 if (RegOp.isImplicit())
4783 return Reg == AMDGPU::VCC || Reg == AMDGPU::VCC_LO || Reg == AMDGPU::M0;
4784
4785 // SGPRs use the constant bus
4786 return AMDGPU::SReg_32RegClass.contains(Reg) ||
4787 AMDGPU::SReg_64RegClass.contains(Reg);
4788}
4789
4791 const MachineRegisterInfo &MRI) const {
4792 Register Reg = RegOp.getReg();
4793 return Reg.isVirtual() ? RI.isSGPRClass(MRI.getRegClass(Reg))
4794 : physRegUsesConstantBus(RegOp);
4795}
4796
4798 const MachineOperand &MO,
4799 const MCOperandInfo &OpInfo) const {
4800 // Literal constants use the constant bus.
4801 if (!MO.isReg())
4802 return !isInlineConstant(MO, OpInfo);
4803
4804 Register Reg = MO.getReg();
4805 return Reg.isVirtual() ? RI.isSGPRClass(MRI.getRegClass(Reg))
4807}
4808
4810 for (const MachineOperand &MO : MI.implicit_operands()) {
4811 // We only care about reads.
4812 if (MO.isDef())
4813 continue;
4814
4815 switch (MO.getReg()) {
4816 case AMDGPU::VCC:
4817 case AMDGPU::VCC_LO:
4818 case AMDGPU::VCC_HI:
4819 case AMDGPU::M0:
4820 case AMDGPU::FLAT_SCR:
4821 return MO.getReg();
4822
4823 default:
4824 break;
4825 }
4826 }
4827
4828 return Register();
4829}
4830
4831static bool shouldReadExec(const MachineInstr &MI) {
4832 if (SIInstrInfo::isVALU(MI)) {
4833 switch (MI.getOpcode()) {
4834 case AMDGPU::V_READLANE_B32:
4835 case AMDGPU::SI_RESTORE_S32_FROM_VGPR:
4836 case AMDGPU::V_WRITELANE_B32:
4837 case AMDGPU::SI_SPILL_S32_TO_VGPR:
4838 return false;
4839 }
4840
4841 return true;
4842 }
4843
4844 if (MI.isPreISelOpcode() ||
4845 SIInstrInfo::isGenericOpcode(MI.getOpcode()) ||
4848 return false;
4849
4850 return true;
4851}
4852
4853static bool isRegOrFI(const MachineOperand &MO) {
4854 return MO.isReg() || MO.isFI();
4855}
4856
4857static bool isSubRegOf(const SIRegisterInfo &TRI,
4858 const MachineOperand &SuperVec,
4859 const MachineOperand &SubReg) {
4860 if (SubReg.getReg().isPhysical())
4861 return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg());
4862
4863 return SubReg.getSubReg() != AMDGPU::NoSubRegister &&
4864 SubReg.getReg() == SuperVec.getReg();
4865}
4866
4867// Verify the illegal copy from vector register to SGPR for generic opcode COPY
4868bool SIInstrInfo::verifyCopy(const MachineInstr &MI,
4869 const MachineRegisterInfo &MRI,
4870 StringRef &ErrInfo) const {
4871 Register DstReg = MI.getOperand(0).getReg();
4872 Register SrcReg = MI.getOperand(1).getReg();
4873 // This is a check for copy from vector register to SGPR
4874 if (RI.isVectorRegister(MRI, SrcReg) && RI.isSGPRReg(MRI, DstReg)) {
4875 ErrInfo = "illegal copy from vector register to SGPR";
4876 return false;
4877 }
4878 return true;
4879}
4880
4882 StringRef &ErrInfo) const {
4883 uint16_t Opcode = MI.getOpcode();
4884 const MachineFunction *MF = MI.getParent()->getParent();
4885 const MachineRegisterInfo &MRI = MF->getRegInfo();
4886
4887 // FIXME: At this point the COPY verify is done only for non-ssa forms.
4888 // Find a better property to recognize the point where instruction selection
4889 // is just done.
4890 // We can only enforce this check after SIFixSGPRCopies pass so that the
4891 // illegal copies are legalized and thereafter we don't expect a pass
4892 // inserting similar copies.
4893 if (!MRI.isSSA() && MI.isCopy())
4894 return verifyCopy(MI, MRI, ErrInfo);
4895
4896 if (SIInstrInfo::isGenericOpcode(Opcode))
4897 return true;
4898
4899 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
4900 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
4901 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
4902 int Src3Idx = -1;
4903 if (Src0Idx == -1) {
4904 // VOPD V_DUAL_* instructions use different operand names.
4905 Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0X);
4906 Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vsrc1X);
4907 Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0Y);
4908 Src3Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vsrc1Y);
4909 }
4910
4911 // Make sure the number of operands is correct.
4912 const MCInstrDesc &Desc = get(Opcode);
4913 if (!Desc.isVariadic() &&
4914 Desc.getNumOperands() != MI.getNumExplicitOperands()) {
4915 ErrInfo = "Instruction has wrong number of operands.";
4916 return false;
4917 }
4918
4919 if (MI.isInlineAsm()) {
4920 // Verify register classes for inlineasm constraints.
4921 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands();
4922 I != E; ++I) {
4923 const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI);
4924 if (!RC)
4925 continue;
4926
4927 const MachineOperand &Op = MI.getOperand(I);
4928 if (!Op.isReg())
4929 continue;
4930
4931 Register Reg = Op.getReg();
4932 if (!Reg.isVirtual() && !RC->contains(Reg)) {
4933 ErrInfo = "inlineasm operand has incorrect register class.";
4934 return false;
4935 }
4936 }
4937
4938 return true;
4939 }
4940
4941 if (isImage(MI) && MI.memoperands_empty() && MI.mayLoadOrStore()) {
4942 ErrInfo = "missing memory operand from image instruction.";
4943 return false;
4944 }
4945
4946 // Make sure the register classes are correct.
4947 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) {
4948 const MachineOperand &MO = MI.getOperand(i);
4949 if (MO.isFPImm()) {
4950 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast "
4951 "all fp values to integers.";
4952 return false;
4953 }
4954
4955 int RegClass = Desc.operands()[i].RegClass;
4956
4957 const MCOperandInfo &OpInfo = Desc.operands()[i];
4958 switch (OpInfo.OperandType) {
4960 if (MI.getOperand(i).isImm() || MI.getOperand(i).isGlobal()) {
4961 ErrInfo = "Illegal immediate value for operand.";
4962 return false;
4963 }
4964 break;
4977 break;
4979 break;
4980 break;
4994 if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) {
4995 ErrInfo = "Illegal immediate value for operand.";
4996 return false;
4997 }
4998 break;
4999 }
5001 if (!MI.getOperand(i).isImm() || !isInlineConstant(MI, i)) {
5002 ErrInfo = "Expected inline constant for operand.";
5003 return false;
5004 }
5005 break;
5009 break;
5014 // Check if this operand is an immediate.
5015 // FrameIndex operands will be replaced by immediates, so they are
5016 // allowed.
5017 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) {
5018 ErrInfo = "Expected immediate, but got non-immediate";
5019 return false;
5020 }
5021 break;
5025 break;
5026 default:
5027 if (OpInfo.isGenericType())
5028 continue;
5029 break;
5030 }
5031
5032 if (!MO.isReg())
5033 continue;
5034 Register Reg = MO.getReg();
5035 if (!Reg)
5036 continue;
5037
5038 // FIXME: Ideally we would have separate instruction definitions with the
5039 // aligned register constraint.
5040 // FIXME: We do not verify inline asm operands, but custom inline asm
5041 // verification is broken anyway
5042 if (ST.needsAlignedVGPRs() && Opcode != AMDGPU::AV_MOV_B64_IMM_PSEUDO) {
5043 const TargetRegisterClass *RC = RI.getRegClassForReg(MRI, Reg);
5044 if (RI.hasVectorRegisters(RC) && MO.getSubReg()) {
5045 if (const TargetRegisterClass *SubRC =
5046 RI.getSubRegisterClass(RC, MO.getSubReg())) {
5047 RC = RI.getCompatibleSubRegClass(RC, SubRC, MO.getSubReg());
5048 if (RC)
5049 RC = SubRC;
5050 }
5051 }
5052
5053 // Check that this is the aligned version of the class.
5054 if (!RC || !RI.isProperlyAlignedRC(*RC)) {
5055 ErrInfo = "Subtarget requires even aligned vector registers";
5056 return false;
5057 }
5058 }
5059
5060 if (RegClass != -1) {
5061 if (Reg.isVirtual())
5062 continue;
5063
5064 const TargetRegisterClass *RC = RI.getRegClass(RegClass);
5065 if (!RC->contains(Reg)) {
5066 ErrInfo = "Operand has incorrect register class.";
5067 return false;
5068 }
5069 }
5070 }
5071
5072 // Verify SDWA
5073 if (isSDWA(MI)) {
5074 if (!ST.hasSDWA()) {
5075 ErrInfo = "SDWA is not supported on this target";
5076 return false;
5077 }
5078
5079 for (auto Op : {AMDGPU::OpName::src0_sel, AMDGPU::OpName::src1_sel,
5080 AMDGPU::OpName::dst_sel}) {
5081 const MachineOperand *MO = getNamedOperand(MI, Op);
5082 if (!MO)
5083 continue;
5084 int64_t Imm = MO->getImm();
5085 if (Imm < 0 || Imm > AMDGPU::SDWA::SdwaSel::DWORD) {
5086 ErrInfo = "Invalid SDWA selection";
5087 return false;
5088 }
5089 }
5090
5091 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst);
5092
5093 for (int OpIdx : {DstIdx, Src0Idx, Src1Idx, Src2Idx}) {
5094 if (OpIdx == -1)
5095 continue;
5096 const MachineOperand &MO = MI.getOperand(OpIdx);
5097
5098 if (!ST.hasSDWAScalar()) {
5099 // Only VGPRS on VI
5100 if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) {
5101 ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI";
5102 return false;
5103 }
5104 } else {
5105 // No immediates on GFX9
5106 if (!MO.isReg()) {
5107 ErrInfo =
5108 "Only reg allowed as operands in SDWA instructions on GFX9+";
5109 return false;
5110 }
5111 }
5112 }
5113
5114 if (!ST.hasSDWAOmod()) {
5115 // No omod allowed on VI
5116 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
5117 if (OMod != nullptr &&
5118 (!OMod->isImm() || OMod->getImm() != 0)) {
5119 ErrInfo = "OMod not allowed in SDWA instructions on VI";
5120 return false;
5121 }
5122 }
5123
5124 if (Opcode == AMDGPU::V_CVT_F32_FP8_sdwa ||
5125 Opcode == AMDGPU::V_CVT_F32_BF8_sdwa ||
5126 Opcode == AMDGPU::V_CVT_PK_F32_FP8_sdwa ||
5127 Opcode == AMDGPU::V_CVT_PK_F32_BF8_sdwa) {
5128 const MachineOperand *Src0ModsMO =
5129 getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
5130 unsigned Mods = Src0ModsMO->getImm();
5131 if (Mods & SISrcMods::ABS || Mods & SISrcMods::NEG ||
5132 Mods & SISrcMods::SEXT) {
5133 ErrInfo = "sext, abs and neg are not allowed on this instruction";
5134 return false;
5135 }
5136 }
5137
5138 uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode);
5139 if (isVOPC(BasicOpcode)) {
5140 if (!ST.hasSDWASdst() && DstIdx != -1) {
5141 // Only vcc allowed as dst on VI for VOPC
5142 const MachineOperand &Dst = MI.getOperand(DstIdx);
5143 if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) {
5144 ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI";
5145 return false;
5146 }
5147 } else if (!ST.hasSDWAOutModsVOPC()) {
5148 // No clamp allowed on GFX9 for VOPC
5149 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp);
5150 if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) {
5151 ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI";
5152 return false;
5153 }
5154
5155 // No omod allowed on GFX9 for VOPC
5156 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
5157 if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) {
5158 ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI";
5159 return false;
5160 }
5161 }
5162 }
5163
5164 const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused);
5165 if (DstUnused && DstUnused->isImm() &&
5166 DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) {
5167 const MachineOperand &Dst = MI.getOperand(DstIdx);
5168 if (!Dst.isReg() || !Dst.isTied()) {
5169 ErrInfo = "Dst register should have tied register";
5170 return false;
5171 }
5172
5173 const MachineOperand &TiedMO =
5174 MI.getOperand(MI.findTiedOperandIdx(DstIdx));
5175 if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) {
5176 ErrInfo =
5177 "Dst register should be tied to implicit use of preserved register";
5178 return false;
5179 }
5180 if (TiedMO.getReg().isPhysical() && Dst.getReg() != TiedMO.getReg()) {
5181 ErrInfo = "Dst register should use same physical register as preserved";
5182 return false;
5183 }
5184 }
5185 }
5186
5187 // Verify MIMG / VIMAGE / VSAMPLE
5188 if (isImage(Opcode) && !MI.mayStore()) {
5189 // Ensure that the return type used is large enough for all the options
5190 // being used TFE/LWE require an extra result register.
5191 const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask);
5192 if (DMask) {
5193 uint64_t DMaskImm = DMask->getImm();
5194 uint32_t RegCount = isGather4(Opcode) ? 4 : llvm::popcount(DMaskImm);
5195 const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe);
5196 const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe);
5197 const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16);
5198
5199 // Adjust for packed 16 bit values
5200 if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem())
5201 RegCount = divideCeil(RegCount, 2);
5202
5203 // Adjust if using LWE or TFE
5204 if ((LWE && LWE->getImm()) || (TFE && TFE->getImm()))
5205 RegCount += 1;
5206
5207 const uint32_t DstIdx =
5208 AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
5209 const MachineOperand &Dst = MI.getOperand(DstIdx);
5210 if (Dst.isReg()) {
5211 const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx);
5212 uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32;
5213 if (RegCount > DstSize) {
5214 ErrInfo = "Image instruction returns too many registers for dst "
5215 "register class";
5216 return false;
5217 }
5218 }
5219 }
5220 }
5221
5222 // Verify VOP*. Ignore multiple sgpr operands on writelane.
5223 if (isVALU(MI) && Desc.getOpcode() != AMDGPU::V_WRITELANE_B32) {
5224 unsigned ConstantBusCount = 0;
5225 bool UsesLiteral = false;
5226 const MachineOperand *LiteralVal = nullptr;
5227
5228 int ImmIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm);
5229 if (ImmIdx != -1) {
5230 ++ConstantBusCount;
5231 UsesLiteral = true;
5232 LiteralVal = &MI.getOperand(ImmIdx);
5233 }
5234
5235 SmallVector<Register, 2> SGPRsUsed;
5236 Register SGPRUsed;
5237
5238 // Only look at the true operands. Only a real operand can use the constant
5239 // bus, and we don't want to check pseudo-operands like the source modifier
5240 // flags.
5241 for (int OpIdx : {Src0Idx, Src1Idx, Src2Idx, Src3Idx}) {
5242 if (OpIdx == -1)
5243 continue;
5244 const MachineOperand &MO = MI.getOperand(OpIdx);
5245 if (usesConstantBus(MRI, MO, MI.getDesc().operands()[OpIdx])) {
5246 if (MO.isReg()) {
5247 SGPRUsed = MO.getReg();
5248 if (!llvm::is_contained(SGPRsUsed, SGPRUsed)) {
5249 ++ConstantBusCount;
5250 SGPRsUsed.push_back(SGPRUsed);
5251 }
5252 } else if (!MO.isFI()) { // Treat FI like a register.
5253 if (!UsesLiteral) {
5254 ++ConstantBusCount;
5255 UsesLiteral = true;
5256 LiteralVal = &MO;
5257 } else if (!MO.isIdenticalTo(*LiteralVal)) {
5258 assert(isVOP2(MI) || isVOP3(MI));
5259 ErrInfo = "VOP2/VOP3 instruction uses more than one literal";
5260 return false;
5261 }
5262 }
5263 }
5264 }
5265
5266 SGPRUsed = findImplicitSGPRRead(MI);
5267 if (SGPRUsed) {
5268 // Implicit uses may safely overlap true operands
5269 if (llvm::all_of(SGPRsUsed, [this, SGPRUsed](unsigned SGPR) {
5270 return !RI.regsOverlap(SGPRUsed, SGPR);
5271 })) {
5272 ++ConstantBusCount;
5273 SGPRsUsed.push_back(SGPRUsed);
5274 }
5275 }
5276
5277 // v_writelane_b32 is an exception from constant bus restriction:
5278 // vsrc0 can be sgpr, const or m0 and lane select sgpr, m0 or inline-const
5279 if (ConstantBusCount > ST.getConstantBusLimit(Opcode) &&
5280 Opcode != AMDGPU::V_WRITELANE_B32) {
5281 ErrInfo = "VOP* instruction violates constant bus restriction";
5282 return false;
5283 }
5284
5285 if (isVOP3(MI) && UsesLiteral && !ST.hasVOP3Literal()) {
5286 ErrInfo = "VOP3 instruction uses literal";
5287 return false;
5288 }
5289 }
5290
5291 // Special case for writelane - this can break the multiple constant bus rule,
5292 // but still can't use more than one SGPR register
5293 if (Desc.getOpcode() == AMDGPU::V_WRITELANE_B32) {
5294 unsigned SGPRCount = 0;
5295 Register SGPRUsed;
5296
5297 for (int OpIdx : {Src0Idx, Src1Idx}) {
5298 if (OpIdx == -1)
5299 break;
5300
5301 const MachineOperand &MO = MI.getOperand(OpIdx);
5302
5303 if (usesConstantBus(MRI, MO, MI.getDesc().operands()[OpIdx])) {
5304 if (MO.isReg() && MO.getReg() != AMDGPU::M0) {
5305 if (MO.getReg() != SGPRUsed)
5306 ++SGPRCount;
5307 SGPRUsed = MO.getReg();
5308 }
5309 }
5310 if (SGPRCount > ST.getConstantBusLimit(Opcode)) {
5311 ErrInfo = "WRITELANE instruction violates constant bus restriction";
5312 return false;
5313 }
5314 }
5315 }
5316
5317 // Verify misc. restrictions on specific instructions.
5318 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32_e64 ||
5319 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64_e64) {
5320 const MachineOperand &Src0 = MI.getOperand(Src0Idx);
5321 const MachineOperand &Src1 = MI.getOperand(Src1Idx);
5322 const MachineOperand &Src2 = MI.getOperand(Src2Idx);
5323 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) {
5324 if (!compareMachineOp(Src0, Src1) &&
5325 !compareMachineOp(Src0, Src2)) {
5326 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2";
5327 return false;
5328 }
5329 }
5330 if ((getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm() &
5331 SISrcMods::ABS) ||
5332 (getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm() &
5333 SISrcMods::ABS) ||
5334 (getNamedOperand(MI, AMDGPU::OpName::src2_modifiers)->getImm() &
5335 SISrcMods::ABS)) {
5336 ErrInfo = "ABS not allowed in VOP3B instructions";
5337 return false;
5338 }
5339 }
5340
5341 if (isSOP2(MI) || isSOPC(MI)) {
5342 const MachineOperand &Src0 = MI.getOperand(Src0Idx);
5343 const MachineOperand &Src1 = MI.getOperand(Src1Idx);
5344
5345 if (!isRegOrFI(Src0) && !isRegOrFI(Src1) &&
5346 !isInlineConstant(Src0, Desc.operands()[Src0Idx]) &&
5347 !isInlineConstant(Src1, Desc.operands()[Src1Idx]) &&
5348 !Src0.isIdenticalTo(Src1)) {
5349 ErrInfo = "SOP2/SOPC instruction requires too many immediate constants";
5350 return false;
5351 }
5352 }
5353
5354 if (isSOPK(MI)) {
5355 const auto *Op = getNamedOperand(MI, AMDGPU::OpName::simm16);
5356 if (Desc.isBranch()) {
5357 if (!Op->isMBB()) {
5358 ErrInfo = "invalid branch target for SOPK instruction";
5359 return false;
5360 }
5361 } else {
5362 uint64_t Imm = Op->getImm();
5363 if (sopkIsZext(Opcode)) {
5364 if (!isUInt<16>(Imm)) {
5365 ErrInfo = "invalid immediate for SOPK instruction";
5366 return false;
5367 }
5368 } else {
5369 if (!isInt<16>(Imm)) {
5370 ErrInfo = "invalid immediate for SOPK instruction";
5371 return false;
5372 }
5373 }
5374 }
5375 }
5376
5377 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 ||
5378 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 ||
5379 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
5380 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) {
5381 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
5382 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64;
5383
5384 const unsigned StaticNumOps =
5385 Desc.getNumOperands() + Desc.implicit_uses().size();
5386 const unsigned NumImplicitOps = IsDst ? 2 : 1;
5387
5388 // Allow additional implicit operands. This allows a fixup done by the post
5389 // RA scheduler where the main implicit operand is killed and implicit-defs
5390 // are added for sub-registers that remain live after this instruction.
5391 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) {
5392 ErrInfo = "missing implicit register operands";
5393 return false;
5394 }
5395
5396 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
5397 if (IsDst) {
5398 if (!Dst->isUse()) {
5399 ErrInfo = "v_movreld_b32 vdst should be a use operand";
5400 return false;
5401 }
5402
5403 unsigned UseOpIdx;
5404 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) ||
5405 UseOpIdx != StaticNumOps + 1) {
5406 ErrInfo = "movrel implicit operands should be tied";
5407 return false;
5408 }
5409 }
5410
5411 const MachineOperand &Src0 = MI.getOperand(Src0Idx);
5412 const MachineOperand &ImpUse
5413 = MI.getOperand(StaticNumOps + NumImplicitOps - 1);
5414 if (!ImpUse.isReg() || !ImpUse.isUse() ||
5415 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) {
5416 ErrInfo = "src0 should be subreg of implicit vector use";
5417 return false;
5418 }
5419 }
5420
5421 // Make sure we aren't losing exec uses in the td files. This mostly requires
5422 // being careful when using let Uses to try to add other use registers.
5423 if (shouldReadExec(MI)) {
5424 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) {
5425 ErrInfo = "VALU instruction does not implicitly read exec mask";
5426 return false;
5427 }
5428 }
5429
5430 if (isSMRD(MI)) {
5431 if (MI.mayStore() &&
5432 ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
5433 // The register offset form of scalar stores may only use m0 as the
5434 // soffset register.
5435 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soffset);
5436 if (Soff && Soff->getReg() != AMDGPU::M0) {
5437 ErrInfo = "scalar stores must use m0 as offset register";
5438 return false;
5439 }
5440 }
5441 }
5442
5443 if (isFLAT(MI) && !ST.hasFlatInstOffsets()) {
5444 const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
5445 if (Offset->getImm() != 0) {
5446 ErrInfo = "subtarget does not support offsets in flat instructions";
5447 return false;
5448 }
5449 }
5450
5451 if (isDS(MI) && !ST.hasGDS()) {
5452 const MachineOperand *GDSOp = getNamedOperand(MI, AMDGPU::OpName::gds);
5453 if (GDSOp && GDSOp->getImm() != 0) {
5454 ErrInfo = "GDS is not supported on this subtarget";
5455 return false;
5456 }
5457 }
5458
5459 if (isImage(MI)) {
5460 const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim);
5461 if (DimOp) {
5462 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode,
5463 AMDGPU::OpName::vaddr0);
5464 AMDGPU::OpName RSrcOpName =
5465 isMIMG(MI) ? AMDGPU::OpName::srsrc : AMDGPU::OpName::rsrc;
5466 int RsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, RSrcOpName);
5467 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode);
5468 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
5469 AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode);
5470 const AMDGPU::MIMGDimInfo *Dim =
5472
5473 if (!Dim) {
5474 ErrInfo = "dim is out of range";
5475 return false;
5476 }
5477
5478 bool IsA16 = false;
5479 if (ST.hasR128A16()) {
5480 const MachineOperand *R128A16 = getNamedOperand(MI, AMDGPU::OpName::r128);
5481 IsA16 = R128A16->getImm() != 0;
5482 } else if (ST.hasA16()) {
5483 const MachineOperand *A16 = getNamedOperand(MI, AMDGPU::OpName::a16);
5484 IsA16 = A16->getImm() != 0;
5485 }
5486
5487 bool IsNSA = RsrcIdx - VAddr0Idx > 1;
5488
5489 unsigned AddrWords =
5490 AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, ST.hasG16());
5491
5492 unsigned VAddrWords;
5493 if (IsNSA) {
5494 VAddrWords = RsrcIdx - VAddr0Idx;
5495 if (ST.hasPartialNSAEncoding() &&
5496 AddrWords > ST.getNSAMaxSize(isVSAMPLE(MI))) {
5497 unsigned LastVAddrIdx = RsrcIdx - 1;
5498 VAddrWords += getOpSize(MI, LastVAddrIdx) / 4 - 1;
5499 }
5500 } else {
5501 VAddrWords = getOpSize(MI, VAddr0Idx) / 4;
5502 if (AddrWords > 12)
5503 AddrWords = 16;
5504 }
5505
5506 if (VAddrWords != AddrWords) {
5507 LLVM_DEBUG(dbgs() << "bad vaddr size, expected " << AddrWords
5508 << " but got " << VAddrWords << "\n");
5509 ErrInfo = "bad vaddr size";
5510 return false;
5511 }
5512 }
5513 }
5514
5515 const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl);
5516 if (DppCt) {
5517 using namespace AMDGPU::DPP;
5518
5519 unsigned DC = DppCt->getImm();
5520 if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 ||
5521 DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST ||
5522 (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) ||
5523 (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) ||
5524 (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) ||
5525 (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) ||
5526 (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) {
5527 ErrInfo = "Invalid dpp_ctrl value";
5528 return false;
5529 }
5530 if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 &&
5531 ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
5532 ErrInfo = "Invalid dpp_ctrl value: "
5533 "wavefront shifts are not supported on GFX10+";
5534 return false;
5535 }
5536 if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 &&
5537 ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
5538 ErrInfo = "Invalid dpp_ctrl value: "
5539 "broadcasts are not supported on GFX10+";
5540 return false;
5541 }
5542 if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST &&
5543 ST.getGeneration() < AMDGPUSubtarget::GFX10) {
5544 if (DC >= DppCtrl::ROW_NEWBCAST_FIRST &&
5545 DC <= DppCtrl::ROW_NEWBCAST_LAST &&
5546 !ST.hasGFX90AInsts()) {
5547 ErrInfo = "Invalid dpp_ctrl value: "
5548 "row_newbroadcast/row_share is not supported before "
5549 "GFX90A/GFX10";
5550 return false;
5551 }
5552 if (DC > DppCtrl::ROW_NEWBCAST_LAST || !ST.hasGFX90AInsts()) {
5553 ErrInfo = "Invalid dpp_ctrl value: "
5554 "row_share and row_xmask are not supported before GFX10";
5555 return false;
5556 }
5557 }
5558
5559 if (Opcode != AMDGPU::V_MOV_B64_DPP_PSEUDO &&
5562 ErrInfo = "Invalid dpp_ctrl value: "
5563 "DP ALU dpp only support row_newbcast";
5564 return false;
5565 }
5566 }
5567
5568 if ((MI.mayStore() || MI.mayLoad()) && !isVGPRSpill(MI)) {
5569 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
5570 AMDGPU::OpName DataName =
5571 isDS(Opcode) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata;
5572 const MachineOperand *Data = getNamedOperand(MI, DataName);
5573 const MachineOperand *Data2 = getNamedOperand(MI, AMDGPU::OpName::data1);
5574 if (Data && !Data->isReg())
5575 Data = nullptr;
5576
5577 if (ST.hasGFX90AInsts()) {
5578 if (Dst && Data && !Dst->isTied() && !Data->isTied() &&
5579 (RI.isAGPR(MRI, Dst->getReg()) != RI.isAGPR(MRI, Data->getReg()))) {
5580 ErrInfo = "Invalid register class: "
5581 "vdata and vdst should be both VGPR or AGPR";
5582 return false;
5583 }
5584 if (Data && Data2 &&
5585 (RI.isAGPR(MRI, Data->getReg()) != RI.isAGPR(MRI, Data2->getReg()))) {
5586 ErrInfo = "Invalid register class: "
5587 "both data operands should be VGPR or AGPR";
5588 return false;
5589 }
5590 } else {
5591 if ((Dst && RI.isAGPR(MRI, Dst->getReg())) ||
5592 (Data && RI.isAGPR(MRI, Data->getReg())) ||
5593 (Data2 && RI.isAGPR(MRI, Data2->getReg()))) {
5594 ErrInfo = "Invalid register class: "
5595 "agpr loads and stores not supported on this GPU";
5596 return false;
5597 }
5598 }
5599 }
5600
5601 if (ST.needsAlignedVGPRs()) {
5602 const auto isAlignedReg = [&MI, &MRI, this](AMDGPU::OpName OpName) -> bool {
5604 if (!Op)
5605 return true;
5606 Register Reg = Op->getReg();
5607 if (Reg.isPhysical())
5608 return !(RI.getHWRegIndex(Reg) & 1);
5609 const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
5610 return RI.getRegSizeInBits(RC) > 32 && RI.isProperlyAlignedRC(RC) &&
5611 !(RI.getChannelFromSubReg(Op->getSubReg()) & 1);
5612 };
5613
5614 if (Opcode == AMDGPU::DS_GWS_INIT || Opcode == AMDGPU::DS_GWS_SEMA_BR ||
5615 Opcode == AMDGPU::DS_GWS_BARRIER) {
5616
5617 if (!isAlignedReg(AMDGPU::OpName::data0)) {
5618 ErrInfo = "Subtarget requires even aligned vector registers "
5619 "for DS_GWS instructions";
5620 return false;
5621 }
5622 }
5623
5624 if (isMIMG(MI)) {
5625 if (!isAlignedReg(AMDGPU::OpName::vaddr)) {
5626 ErrInfo = "Subtarget requires even aligned vector registers "
5627 "for vaddr operand of image instructions";
5628 return false;
5629 }
5630 }
5631 }
5632
5633 if (Opcode == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && !ST.hasGFX90AInsts()) {
5634 const MachineOperand *Src = getNamedOperand(MI, AMDGPU::OpName::src0);
5635 if (Src->isReg() && RI.isSGPRReg(MRI, Src->getReg())) {
5636 ErrInfo = "Invalid register class: "
5637 "v_accvgpr_write with an SGPR is not supported on this GPU";
5638 return false;
5639 }
5640 }
5641
5642 if (Desc.getOpcode() == AMDGPU::G_AMDGPU_WAVE_ADDRESS) {
5643 const MachineOperand &SrcOp = MI.getOperand(1);
5644 if (!SrcOp.isReg() || SrcOp.getReg().isVirtual()) {
5645 ErrInfo = "pseudo expects only physical SGPRs";
5646 return false;
5647 }
5648 }
5649
5650 if (const MachineOperand *CPol = getNamedOperand(MI, AMDGPU::OpName::cpol)) {
5651 if (CPol->getImm() & AMDGPU::CPol::SCAL) {
5652 if (!ST.hasScaleOffset()) {
5653 ErrInfo = "Subtarget does not support offset scaling";
5654 return false;
5655 }
5656 if (!AMDGPU::supportsScaleOffset(*this, MI.getOpcode())) {
5657 ErrInfo = "Instruction does not support offset scaling";
5658 return false;
5659 }
5660 }
5661 }
5662
5663 // See SIInstrInfo::isLegalGFX12PlusPackedMathFP32Operand for more
5664 // information.
5665 if (AMDGPU::isPackedFP32Inst(Opcode) && AMDGPU::isGFX12Plus(ST)) {
5666 for (unsigned I = 0; I < 3; ++I) {
5668 return false;
5669 }
5670 }
5671
5672 return true;
5673}
5674
5675// It is more readable to list mapped opcodes on the same line.
5676// clang-format off
5677
5679 switch (MI.getOpcode()) {
5680 default: return AMDGPU::INSTRUCTION_LIST_END;
5681 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE;
5682 case AMDGPU::COPY: return AMDGPU::COPY;
5683 case AMDGPU::PHI: return AMDGPU::PHI;
5684 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG;
5685 case AMDGPU::WQM: return AMDGPU::WQM;
5686 case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM;
5687 case AMDGPU::STRICT_WWM: return AMDGPU::STRICT_WWM;
5688 case AMDGPU::STRICT_WQM: return AMDGPU::STRICT_WQM;
5689 case AMDGPU::S_MOV_B32: {
5690 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
5691 return MI.getOperand(1).isReg() ||
5692 RI.isAGPR(MRI, MI.getOperand(0).getReg()) ?
5693 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32;
5694 }
5695 case AMDGPU::S_ADD_I32:
5696 return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32;
5697 case AMDGPU::S_ADDC_U32:
5698 return AMDGPU::V_ADDC_U32_e32;
5699 case AMDGPU::S_SUB_I32:
5700 return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_CO_U32_e32;
5701 // FIXME: These are not consistently handled, and selected when the carry is
5702 // used.
5703 case AMDGPU::S_ADD_U32:
5704 return AMDGPU::V_ADD_CO_U32_e32;
5705 case AMDGPU::S_SUB_U32:
5706 return AMDGPU::V_SUB_CO_U32_e32;
5707 case AMDGPU::S_ADD_U64_PSEUDO:
5708 return AMDGPU::V_ADD_U64_PSEUDO;
5709 case AMDGPU::S_SUB_U64_PSEUDO:
5710 return AMDGPU::V_SUB_U64_PSEUDO;
5711 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32;
5712 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32_e64;
5713 case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32_e64;
5714 case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32_e64;
5715 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64;
5716 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64;
5717 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64;
5718 case AMDGPU::S_XNOR_B32:
5719 return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END;
5720 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64;
5721 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64;
5722 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64;
5723 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64;
5724 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32;
5725 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64_e64;
5726 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32;
5727 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64_e64;
5728 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32;
5729 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64_e64;
5730 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32_e64;
5731 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32_e64;
5732 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32_e64;
5733 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32_e64;
5734 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64;
5735 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32;
5736 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32;
5737 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32;
5738 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e64;
5739 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e64;
5740 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e64;
5741 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e64;
5742 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e64;
5743 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e64;
5744 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e64;
5745 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e64;
5746 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e64;
5747 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e64;
5748 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e64;
5749 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e64;
5750 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e64;
5751 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e64;
5752 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64;
5753 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32;
5754 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32;
5755 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64;
5756 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ;
5757 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ;
5758 case AMDGPU::S_CVT_F32_I32: return AMDGPU::V_CVT_F32_I32_e64;
5759 case AMDGPU::S_CVT_F32_U32: return AMDGPU::V_CVT_F32_U32_e64;
5760 case AMDGPU::S_CVT_I32_F32: return AMDGPU::V_CVT_I32_F32_e64;
5761 case AMDGPU::S_CVT_U32_F32: return AMDGPU::V_CVT_U32_F32_e64;
5762 case AMDGPU::S_CVT_F32_F16:
5763 case AMDGPU::S_CVT_HI_F32_F16:
5764 return ST.useRealTrue16Insts() ? AMDGPU::V_CVT_F32_F16_t16_e64
5765 : AMDGPU::V_CVT_F32_F16_fake16_e64;
5766 case AMDGPU::S_CVT_F16_F32:
5767 return ST.useRealTrue16Insts() ? AMDGPU::V_CVT_F16_F32_t16_e64
5768 : AMDGPU::V_CVT_F16_F32_fake16_e64;
5769 case AMDGPU::S_CEIL_F32: return AMDGPU::V_CEIL_F32_e64;
5770 case AMDGPU::S_FLOOR_F32: return AMDGPU::V_FLOOR_F32_e64;
5771 case AMDGPU::S_TRUNC_F32: return AMDGPU::V_TRUNC_F32_e64;
5772 case AMDGPU::S_RNDNE_F32: return AMDGPU::V_RNDNE_F32_e64;
5773 case AMDGPU::S_CEIL_F16:
5774 return ST.useRealTrue16Insts() ? AMDGPU::V_CEIL_F16_t16_e64
5775 : AMDGPU::V_CEIL_F16_fake16_e64;
5776 case AMDGPU::S_FLOOR_F16:
5777 return ST.useRealTrue16Insts() ? AMDGPU::V_FLOOR_F16_t16_e64
5778 : AMDGPU::V_FLOOR_F16_fake16_e64;
5779 case AMDGPU::S_TRUNC_F16:
5780 return ST.useRealTrue16Insts() ? AMDGPU::V_TRUNC_F16_t16_e64
5781 : AMDGPU::V_TRUNC_F16_fake16_e64;
5782 case AMDGPU::S_RNDNE_F16:
5783 return ST.useRealTrue16Insts() ? AMDGPU::V_RNDNE_F16_t16_e64
5784 : AMDGPU::V_RNDNE_F16_fake16_e64;
5785 case AMDGPU::S_ADD_F32: return AMDGPU::V_ADD_F32_e64;
5786 case AMDGPU::S_SUB_F32: return AMDGPU::V_SUB_F32_e64;
5787 case AMDGPU::S_MIN_F32: return AMDGPU::V_MIN_F32_e64;
5788 case AMDGPU::S_MAX_F32: return AMDGPU::V_MAX_F32_e64;
5789 case AMDGPU::S_MINIMUM_F32: return AMDGPU::V_MINIMUM_F32_e64;
5790 case AMDGPU::S_MAXIMUM_F32: return AMDGPU::V_MAXIMUM_F32_e64;
5791 case AMDGPU::S_MUL_F32: return AMDGPU::V_MUL_F32_e64;
5792 case AMDGPU::S_ADD_F16:
5793 return ST.useRealTrue16Insts() ? AMDGPU::V_ADD_F16_t16_e64
5794 : AMDGPU::V_ADD_F16_fake16_e64;
5795 case AMDGPU::S_SUB_F16:
5796 return ST.useRealTrue16Insts() ? AMDGPU::V_SUB_F16_t16_e64
5797 : AMDGPU::V_SUB_F16_fake16_e64;
5798 case AMDGPU::S_MIN_F16:
5799 return ST.useRealTrue16Insts() ? AMDGPU::V_MIN_F16_t16_e64
5800 : AMDGPU::V_MIN_F16_fake16_e64;
5801 case AMDGPU::S_MAX_F16:
5802 return ST.useRealTrue16Insts() ? AMDGPU::V_MAX_F16_t16_e64
5803 : AMDGPU::V_MAX_F16_fake16_e64;
5804 case AMDGPU::S_MINIMUM_F16:
5805 return ST.useRealTrue16Insts() ? AMDGPU::V_MINIMUM_F16_t16_e64
5806 : AMDGPU::V_MINIMUM_F16_fake16_e64;
5807 case AMDGPU::S_MAXIMUM_F16:
5808 return ST.useRealTrue16Insts() ? AMDGPU::V_MAXIMUM_F16_t16_e64
5809 : AMDGPU::V_MAXIMUM_F16_fake16_e64;
5810 case AMDGPU::S_MUL_F16:
5811 return ST.useRealTrue16Insts() ? AMDGPU::V_MUL_F16_t16_e64
5812 : AMDGPU::V_MUL_F16_fake16_e64;
5813 case AMDGPU::S_CVT_PK_RTZ_F16_F32: return AMDGPU::V_CVT_PKRTZ_F16_F32_e64;
5814 case AMDGPU::S_FMAC_F32: return AMDGPU::V_FMAC_F32_e64;
5815 case AMDGPU::S_FMAC_F16:
5816 return ST.useRealTrue16Insts() ? AMDGPU::V_FMAC_F16_t16_e64
5817 : AMDGPU::V_FMAC_F16_fake16_e64;
5818 case AMDGPU::S_FMAMK_F32: return AMDGPU::V_FMAMK_F32;
5819 case AMDGPU::S_FMAAK_F32: return AMDGPU::V_FMAAK_F32;
5820 case AMDGPU::S_CMP_LT_F32: return AMDGPU::V_CMP_LT_F32_e64;
5821 case AMDGPU::S_CMP_EQ_F32: return AMDGPU::V_CMP_EQ_F32_e64;
5822 case AMDGPU::S_CMP_LE_F32: return AMDGPU::V_CMP_LE_F32_e64;
5823 case AMDGPU::S_CMP_GT_F32: return AMDGPU::V_CMP_GT_F32_e64;
5824 case AMDGPU::S_CMP_LG_F32: return AMDGPU::V_CMP_LG_F32_e64;
5825 case AMDGPU::S_CMP_GE_F32: return AMDGPU::V_CMP_GE_F32_e64;
5826 case AMDGPU::S_CMP_O_F32: return AMDGPU::V_CMP_O_F32_e64;
5827 case AMDGPU::S_CMP_U_F32: return AMDGPU::V_CMP_U_F32_e64;
5828 case AMDGPU::S_CMP_NGE_F32: return AMDGPU::V_CMP_NGE_F32_e64;
5829 case AMDGPU::S_CMP_NLG_F32: return AMDGPU::V_CMP_NLG_F32_e64;
5830 case AMDGPU::S_CMP_NGT_F32: return AMDGPU::V_CMP_NGT_F32_e64;
5831 case AMDGPU::S_CMP_NLE_F32: return AMDGPU::V_CMP_NLE_F32_e64;
5832 case AMDGPU::S_CMP_NEQ_F32: return AMDGPU::V_CMP_NEQ_F32_e64;
5833 case AMDGPU::S_CMP_NLT_F32: return AMDGPU::V_CMP_NLT_F32_e64;
5834 case AMDGPU::S_CMP_LT_F16:
5835 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_LT_F16_t16_e64
5836 : AMDGPU::V_CMP_LT_F16_fake16_e64;
5837 case AMDGPU::S_CMP_EQ_F16:
5838 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_EQ_F16_t16_e64
5839 : AMDGPU::V_CMP_EQ_F16_fake16_e64;
5840 case AMDGPU::S_CMP_LE_F16:
5841 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_LE_F16_t16_e64
5842 : AMDGPU::V_CMP_LE_F16_fake16_e64;
5843 case AMDGPU::S_CMP_GT_F16:
5844 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_GT_F16_t16_e64
5845 : AMDGPU::V_CMP_GT_F16_fake16_e64;
5846 case AMDGPU::S_CMP_LG_F16:
5847 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_LG_F16_t16_e64
5848 : AMDGPU::V_CMP_LG_F16_fake16_e64;
5849 case AMDGPU::S_CMP_GE_F16:
5850 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_GE_F16_t16_e64
5851 : AMDGPU::V_CMP_GE_F16_fake16_e64;
5852 case AMDGPU::S_CMP_O_F16:
5853 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_O_F16_t16_e64
5854 : AMDGPU::V_CMP_O_F16_fake16_e64;
5855 case AMDGPU::S_CMP_U_F16:
5856 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_U_F16_t16_e64
5857 : AMDGPU::V_CMP_U_F16_fake16_e64;
5858 case AMDGPU::S_CMP_NGE_F16:
5859 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_NGE_F16_t16_e64
5860 : AMDGPU::V_CMP_NGE_F16_fake16_e64;
5861 case AMDGPU::S_CMP_NLG_F16:
5862 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_NLG_F16_t16_e64
5863 : AMDGPU::V_CMP_NLG_F16_fake16_e64;
5864 case AMDGPU::S_CMP_NGT_F16:
5865 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_NGT_F16_t16_e64
5866 : AMDGPU::V_CMP_NGT_F16_fake16_e64;
5867 case AMDGPU::S_CMP_NLE_F16:
5868 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_NLE_F16_t16_e64
5869 : AMDGPU::V_CMP_NLE_F16_fake16_e64;
5870 case AMDGPU::S_CMP_NEQ_F16:
5871 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_NEQ_F16_t16_e64
5872 : AMDGPU::V_CMP_NEQ_F16_fake16_e64;
5873 case AMDGPU::S_CMP_NLT_F16:
5874 return ST.useRealTrue16Insts() ? AMDGPU::V_CMP_NLT_F16_t16_e64
5875 : AMDGPU::V_CMP_NLT_F16_fake16_e64;
5876 case AMDGPU::V_S_EXP_F32_e64: return AMDGPU::V_EXP_F32_e64;
5877 case AMDGPU::V_S_EXP_F16_e64:
5878 return ST.useRealTrue16Insts() ? AMDGPU::V_EXP_F16_t16_e64
5879 : AMDGPU::V_EXP_F16_fake16_e64;
5880 case AMDGPU::V_S_LOG_F32_e64: return AMDGPU::V_LOG_F32_e64;
5881 case AMDGPU::V_S_LOG_F16_e64:
5882 return ST.useRealTrue16Insts() ? AMDGPU::V_LOG_F16_t16_e64
5883 : AMDGPU::V_LOG_F16_fake16_e64;
5884 case AMDGPU::V_S_RCP_F32_e64: return AMDGPU::V_RCP_F32_e64;
5885 case AMDGPU::V_S_RCP_F16_e64:
5886 return ST.useRealTrue16Insts() ? AMDGPU::V_RCP_F16_t16_e64
5887 : AMDGPU::V_RCP_F16_fake16_e64;
5888 case AMDGPU::V_S_RSQ_F32_e64: return AMDGPU::V_RSQ_F32_e64;
5889 case AMDGPU::V_S_RSQ_F16_e64:
5890 return ST.useRealTrue16Insts() ? AMDGPU::V_RSQ_F16_t16_e64
5891 : AMDGPU::V_RSQ_F16_fake16_e64;
5892 case AMDGPU::V_S_SQRT_F32_e64: return AMDGPU::V_SQRT_F32_e64;
5893 case AMDGPU::V_S_SQRT_F16_e64:
5894 return ST.useRealTrue16Insts() ? AMDGPU::V_SQRT_F16_t16_e64
5895 : AMDGPU::V_SQRT_F16_fake16_e64;
5896 }
5898 "Unexpected scalar opcode without corresponding vector one!");
5899}
5900
5901// clang-format on
5902
5906 const DebugLoc &DL, Register Reg,
5907 bool IsSCCLive,
5908 SlotIndexes *Indexes) const {
5909 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
5910 const SIInstrInfo *TII = ST.getInstrInfo();
5912 if (IsSCCLive) {
5913 // Insert two move instructions, one to save the original value of EXEC and
5914 // the other to turn on all bits in EXEC. This is required as we can't use
5915 // the single instruction S_OR_SAVEEXEC that clobbers SCC.
5916 auto StoreExecMI = BuildMI(MBB, MBBI, DL, TII->get(LMC.MovOpc), Reg)
5918 auto FlipExecMI =
5919 BuildMI(MBB, MBBI, DL, TII->get(LMC.MovOpc), LMC.ExecReg).addImm(-1);
5920 if (Indexes) {
5921 Indexes->insertMachineInstrInMaps(*StoreExecMI);
5922 Indexes->insertMachineInstrInMaps(*FlipExecMI);
5923 }
5924 } else {
5925 auto SaveExec =
5926 BuildMI(MBB, MBBI, DL, TII->get(LMC.OrSaveExecOpc), Reg).addImm(-1);
5927 SaveExec->getOperand(3).setIsDead(); // Mark SCC as dead.
5928 if (Indexes)
5929 Indexes->insertMachineInstrInMaps(*SaveExec);
5930 }
5931}
5932
5935 const DebugLoc &DL, Register Reg,
5936 SlotIndexes *Indexes) const {
5938 auto ExecRestoreMI = BuildMI(MBB, MBBI, DL, get(LMC.MovOpc), LMC.ExecReg)
5939 .addReg(Reg, RegState::Kill);
5940 if (Indexes)
5941 Indexes->insertMachineInstrInMaps(*ExecRestoreMI);
5942}
5943
5947 "Not a whole wave func");
5948 MachineBasicBlock &MBB = *MF.begin();
5949 for (MachineInstr &MI : MBB)
5950 if (MI.getOpcode() == AMDGPU::SI_WHOLE_WAVE_FUNC_SETUP ||
5951 MI.getOpcode() == AMDGPU::G_AMDGPU_WHOLE_WAVE_FUNC_SETUP)
5952 return &MI;
5953
5954 llvm_unreachable("Couldn't find SI_SETUP_WHOLE_WAVE_FUNC instruction");
5955}
5956
5957static const TargetRegisterClass *
5959 const MCInstrDesc &TID, unsigned RCID) {
5960 if (!ST.hasGFX90AInsts() && (TID.mayLoad() || TID.mayStore())) {
5961 switch (RCID) {
5962 case AMDGPU::AV_32RegClassID:
5963 RCID = AMDGPU::VGPR_32RegClassID;
5964 break;
5965 case AMDGPU::AV_64RegClassID:
5966 RCID = AMDGPU::VReg_64RegClassID;
5967 break;
5968 case AMDGPU::AV_96RegClassID:
5969 RCID = AMDGPU::VReg_96RegClassID;
5970 break;
5971 case AMDGPU::AV_128RegClassID:
5972 RCID = AMDGPU::VReg_128RegClassID;
5973 break;
5974 case AMDGPU::AV_160RegClassID:
5975 RCID = AMDGPU::VReg_160RegClassID;
5976 break;
5977 case AMDGPU::AV_512RegClassID:
5978 RCID = AMDGPU::VReg_512RegClassID;
5979 break;
5980 default:
5981 break;
5982 }
5983 }
5984
5985 return RI.getProperlyAlignedRC(RI.getRegClass(RCID));
5986}
5987
5988const TargetRegisterClass *
5989SIInstrInfo::getRegClass(const MCInstrDesc &TID, unsigned OpNum,
5990 const TargetRegisterInfo *TRI) const {
5991 if (OpNum >= TID.getNumOperands())
5992 return nullptr;
5993 auto RegClass = TID.operands()[OpNum].RegClass;
5994 // Special pseudos have no alignment requirement.
5995 if (TID.getOpcode() == AMDGPU::AV_MOV_B64_IMM_PSEUDO || isSpill(TID))
5996 return RI.getRegClass(RegClass);
5997
5998 return adjustAllocatableRegClass(ST, RI, TID, RegClass);
5999}
6000
6002 unsigned OpNo) const {
6003 const MCInstrDesc &Desc = get(MI.getOpcode());
6004 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
6005 Desc.operands()[OpNo].RegClass == -1) {
6006 Register Reg = MI.getOperand(OpNo).getReg();
6007
6008 if (Reg.isVirtual()) {
6009 const MachineRegisterInfo &MRI =
6010 MI.getParent()->getParent()->getRegInfo();
6011 return MRI.getRegClass(Reg);
6012 }
6013 return RI.getPhysRegBaseClass(Reg);
6014 }
6015
6016 unsigned RCID = Desc.operands()[OpNo].RegClass;
6017 return adjustAllocatableRegClass(ST, RI, Desc, RCID);
6018}
6019
6022 MachineBasicBlock *MBB = MI.getParent();
6023 MachineOperand &MO = MI.getOperand(OpIdx);
6024 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
6025 unsigned RCID = get(MI.getOpcode()).operands()[OpIdx].RegClass;
6026 const TargetRegisterClass *RC = RI.getRegClass(RCID);
6027 unsigned Size = RI.getRegSizeInBits(*RC);
6028 unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO
6029 : Size == 16 ? AMDGPU::V_MOV_B16_t16_e64
6030 : AMDGPU::V_MOV_B32_e32;
6031 if (MO.isReg())
6032 Opcode = AMDGPU::COPY;
6033 else if (RI.isSGPRClass(RC))
6034 Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32;
6035
6036 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
6037 Register Reg = MRI.createVirtualRegister(VRC);
6038 DebugLoc DL = MBB->findDebugLoc(I);
6039 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO);
6040 MO.ChangeToRegister(Reg, false);
6041}
6042
6045 const MachineOperand &SuperReg, const TargetRegisterClass *SuperRC,
6046 unsigned SubIdx, const TargetRegisterClass *SubRC) const {
6047 if (!SuperReg.getReg().isVirtual())
6048 return RI.getSubReg(SuperReg.getReg(), SubIdx);
6049
6050 MachineBasicBlock *MBB = MI->getParent();
6051 const DebugLoc &DL = MI->getDebugLoc();
6052 Register SubReg = MRI.createVirtualRegister(SubRC);
6053
6054 unsigned NewSubIdx = RI.composeSubRegIndices(SuperReg.getSubReg(), SubIdx);
6055 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
6056 .addReg(SuperReg.getReg(), 0, NewSubIdx);
6057 return SubReg;
6058}
6059
6062 const MachineOperand &Op, const TargetRegisterClass *SuperRC,
6063 unsigned SubIdx, const TargetRegisterClass *SubRC) const {
6064 if (Op.isImm()) {
6065 if (SubIdx == AMDGPU::sub0)
6066 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm()));
6067 if (SubIdx == AMDGPU::sub1)
6068 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32));
6069
6070 llvm_unreachable("Unhandled register index for immediate");
6071 }
6072
6073 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
6074 SubIdx, SubRC);
6075 return MachineOperand::CreateReg(SubReg, false);
6076}
6077
6078// Change the order of operands from (0, 1, 2) to (0, 2, 1)
6079void SIInstrInfo::swapOperands(MachineInstr &Inst) const {
6080 assert(Inst.getNumExplicitOperands() == 3);
6081 MachineOperand Op1 = Inst.getOperand(1);
6082 Inst.removeOperand(1);
6083 Inst.addOperand(Op1);
6084}
6085
6087 const MCOperandInfo &OpInfo,
6088 const MachineOperand &MO) const {
6089 if (!MO.isReg())
6090 return false;
6091
6092 Register Reg = MO.getReg();
6093
6094 const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass);
6095 if (Reg.isPhysical())
6096 return DRC->contains(Reg);
6097
6098 const TargetRegisterClass *RC = MRI.getRegClass(Reg);
6099
6100 if (MO.getSubReg()) {
6101 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
6102 const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF);
6103 if (!SuperRC)
6104 return false;
6105 return RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg()) != nullptr;
6106 }
6107
6108 return RI.getCommonSubClass(DRC, RC) != nullptr;
6109}
6110
6112 const MachineOperand &MO) const {
6113 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
6114 const MCOperandInfo OpInfo = MI.getDesc().operands()[OpIdx];
6115 unsigned Opc = MI.getOpcode();
6116
6117 // See SIInstrInfo::isLegalGFX12PlusPackedMathFP32Operand for more
6118 // information.
6119 if (AMDGPU::isPackedFP32Inst(MI.getOpcode()) && AMDGPU::isGFX12Plus(ST) &&
6120 MO.isReg() && RI.isSGPRReg(MRI, MO.getReg())) {
6121 constexpr const AMDGPU::OpName OpNames[] = {
6122 AMDGPU::OpName::src0, AMDGPU::OpName::src1, AMDGPU::OpName::src2};
6123
6124 for (auto [I, OpName] : enumerate(OpNames)) {
6125 int SrcIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OpNames[I]);
6126 if (static_cast<unsigned>(SrcIdx) == OpIdx &&
6128 return false;
6129 }
6130 }
6131
6132 if (!isLegalRegOperand(MRI, OpInfo, MO))
6133 return false;
6134
6135 // check Accumulate GPR operand
6136 bool IsAGPR = RI.isAGPR(MRI, MO.getReg());
6137 if (IsAGPR && !ST.hasMAIInsts())
6138 return false;
6139 if (IsAGPR && (!ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) &&
6140 (MI.mayLoad() || MI.mayStore() || isDS(Opc) || isMIMG(Opc)))
6141 return false;
6142 // Atomics should have both vdst and vdata either vgpr or agpr.
6143 const int VDstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
6144 const int DataIdx = AMDGPU::getNamedOperandIdx(
6145 Opc, isDS(Opc) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata);
6146 if ((int)OpIdx == VDstIdx && DataIdx != -1 &&
6147 MI.getOperand(DataIdx).isReg() &&
6148 RI.isAGPR(MRI, MI.getOperand(DataIdx).getReg()) != IsAGPR)
6149 return false;
6150 if ((int)OpIdx == DataIdx) {
6151 if (VDstIdx != -1 &&
6152 RI.isAGPR(MRI, MI.getOperand(VDstIdx).getReg()) != IsAGPR)
6153 return false;
6154 // DS instructions with 2 src operands also must have tied RC.
6155 const int Data1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data1);
6156 if (Data1Idx != -1 && MI.getOperand(Data1Idx).isReg() &&
6157 RI.isAGPR(MRI, MI.getOperand(Data1Idx).getReg()) != IsAGPR)
6158 return false;
6159 }
6160
6161 // Check V_ACCVGPR_WRITE_B32_e64
6162 if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && !ST.hasGFX90AInsts() &&
6163 (int)OpIdx == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) &&
6164 RI.isSGPRReg(MRI, MO.getReg()))
6165 return false;
6166 return true;
6167}
6168
6170 const MCOperandInfo &OpInfo,
6171 const MachineOperand &MO) const {
6172 if (MO.isReg())
6173 return isLegalRegOperand(MRI, OpInfo, MO);
6174
6175 // Handle non-register types that are treated like immediates.
6176 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal());
6177 return true;
6178}
6179
6181 const MachineRegisterInfo &MRI, const MachineInstr &MI, unsigned SrcN,
6182 const MachineOperand *MO) const {
6183 constexpr const unsigned NumOps = 3;
6184 constexpr const AMDGPU::OpName OpNames[NumOps * 2] = {
6185 AMDGPU::OpName::src0, AMDGPU::OpName::src1,
6186 AMDGPU::OpName::src2, AMDGPU::OpName::src0_modifiers,
6187 AMDGPU::OpName::src1_modifiers, AMDGPU::OpName::src2_modifiers};
6188
6189 assert(SrcN < NumOps);
6190
6191 if (!MO) {
6192 int SrcIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OpNames[SrcN]);
6193 if (SrcIdx == -1)
6194 return true;
6195 MO = &MI.getOperand(SrcIdx);
6196 }
6197
6198 if (!MO->isReg() || !RI.isSGPRReg(MRI, MO->getReg()))
6199 return true;
6200
6201 int ModsIdx =
6202 AMDGPU::getNamedOperandIdx(MI.getOpcode(), OpNames[NumOps + SrcN]);
6203 if (ModsIdx == -1)
6204 return true;
6205
6206 unsigned Mods = MI.getOperand(ModsIdx).getImm();
6207 bool OpSel = Mods & SISrcMods::OP_SEL_0;
6208 bool OpSelHi = Mods & SISrcMods::OP_SEL_1;
6209
6210 return !OpSel && !OpSelHi;
6211}
6212
6214 const MachineOperand *MO) const {
6215 const MachineFunction &MF = *MI.getParent()->getParent();
6216 const MachineRegisterInfo &MRI = MF.getRegInfo();
6217 const MCInstrDesc &InstDesc = MI.getDesc();
6218 const MCOperandInfo &OpInfo = InstDesc.operands()[OpIdx];
6219 const TargetRegisterClass *DefinedRC =
6220 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr;
6221 if (!MO)
6222 MO = &MI.getOperand(OpIdx);
6223
6224 const bool IsInlineConst = !MO->isReg() && isInlineConstant(*MO, OpInfo);
6225
6226 if (isVALU(MI) && !IsInlineConst && usesConstantBus(MRI, *MO, OpInfo)) {
6227 const MachineOperand *UsedLiteral = nullptr;
6228
6229 int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode());
6230 int LiteralLimit = !isVOP3(MI) || ST.hasVOP3Literal() ? 1 : 0;
6231
6232 // TODO: Be more permissive with frame indexes.
6233 if (!MO->isReg() && !isInlineConstant(*MO, OpInfo)) {
6234 if (!LiteralLimit--)
6235 return false;
6236
6237 UsedLiteral = MO;
6238 }
6239
6241 if (MO->isReg())
6242 SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg()));
6243
6244 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
6245 if (i == OpIdx)
6246 continue;
6247 const MachineOperand &Op = MI.getOperand(i);
6248 if (Op.isReg()) {
6249 if (Op.isUse()) {
6250 RegSubRegPair SGPR(Op.getReg(), Op.getSubReg());
6251 if (regUsesConstantBus(Op, MRI) && SGPRsUsed.insert(SGPR).second) {
6252 if (--ConstantBusLimit <= 0)
6253 return false;
6254 }
6255 }
6256 } else if (AMDGPU::isSISrcOperand(InstDesc.operands()[i]) &&
6257 !isInlineConstant(Op, InstDesc.operands()[i])) {
6258 // The same literal may be used multiple times.
6259 if (!UsedLiteral)
6260 UsedLiteral = &Op;
6261 else if (UsedLiteral->isIdenticalTo(Op))
6262 continue;
6263
6264 if (!LiteralLimit--)
6265 return false;
6266 if (--ConstantBusLimit <= 0)
6267 return false;
6268 }
6269 }
6270 } else if (!IsInlineConst && !MO->isReg() && isSALU(MI)) {
6271 // There can be at most one literal operand, but it can be repeated.
6272 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
6273 if (i == OpIdx)
6274 continue;
6275 const MachineOperand &Op = MI.getOperand(i);
6276 if (!Op.isReg() && !Op.isFI() && !Op.isRegMask() &&
6277 !isInlineConstant(Op, InstDesc.operands()[i]) &&
6278 !Op.isIdenticalTo(*MO))
6279 return false;
6280
6281 // Do not fold a non-inlineable and non-register operand into an
6282 // instruction that already has a frame index. The frame index handling
6283 // code could not handle well when a frame index co-exists with another
6284 // non-register operand, unless that operand is an inlineable immediate.
6285 if (Op.isFI())
6286 return false;
6287 }
6288 } else if (IsInlineConst && ST.hasNoF16PseudoScalarTransInlineConstants() &&
6289 isF16PseudoScalarTrans(MI.getOpcode())) {
6290 return false;
6291 }
6292
6293 if (MO->isReg()) {
6294 if (!DefinedRC)
6295 return OpInfo.OperandType == MCOI::OPERAND_UNKNOWN;
6296 return isLegalRegOperand(MI, OpIdx, *MO);
6297 }
6298
6299 if (MO->isImm()) {
6300 uint64_t Imm = MO->getImm();
6301 bool Is64BitFPOp = OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_FP64;
6302 bool Is64BitOp = Is64BitFPOp ||
6303 OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_INT64 ||
6304 OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_V2INT32 ||
6305 OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_V2FP32;
6306 if (Is64BitOp &&
6307 !AMDGPU::isInlinableLiteral64(Imm, ST.hasInv2PiInlineImm())) {
6308 if (!AMDGPU::isValid32BitLiteral(Imm, Is64BitFPOp) &&
6309 (!ST.has64BitLiterals() || InstDesc.getSize() != 4))
6310 return false;
6311
6312 // FIXME: We can use sign extended 64-bit literals, but only for signed
6313 // operands. At the moment we do not know if an operand is signed.
6314 // Such operand will be encoded as its low 32 bits and then either
6315 // correctly sign extended or incorrectly zero extended by HW.
6316 // If 64-bit literals are supported and the literal will be encoded
6317 // as full 64 bit we still can use it.
6318 if (!Is64BitFPOp && (int32_t)Imm < 0 &&
6319 (!ST.has64BitLiterals() || AMDGPU::isValid32BitLiteral(Imm, false)))
6320 return false;
6321 }
6322 }
6323
6324 // Handle non-register types that are treated like immediates.
6325 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal());
6326
6327 if (!DefinedRC) {
6328 // This operand expects an immediate.
6329 return true;
6330 }
6331
6332 return isImmOperandLegal(MI, OpIdx, *MO);
6333}
6334
6336 MachineInstr &MI) const {
6337 unsigned Opc = MI.getOpcode();
6338 const MCInstrDesc &InstrDesc = get(Opc);
6339
6340 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
6341 MachineOperand &Src0 = MI.getOperand(Src0Idx);
6342
6343 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
6344 MachineOperand &Src1 = MI.getOperand(Src1Idx);
6345
6346 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32
6347 // we need to only have one constant bus use before GFX10.
6348 bool HasImplicitSGPR = findImplicitSGPRRead(MI);
6349 if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 && Src0.isReg() &&
6350 RI.isSGPRReg(MRI, Src0.getReg()))
6351 legalizeOpWithMove(MI, Src0Idx);
6352
6353 // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for
6354 // both the value to write (src0) and lane select (src1). Fix up non-SGPR
6355 // src0/src1 with V_READFIRSTLANE.
6356 if (Opc == AMDGPU::V_WRITELANE_B32) {
6357 const DebugLoc &DL = MI.getDebugLoc();
6358 if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) {
6359 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6360 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
6361 .add(Src0);
6362 Src0.ChangeToRegister(Reg, false);
6363 }
6364 if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) {
6365 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6366 const DebugLoc &DL = MI.getDebugLoc();
6367 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
6368 .add(Src1);
6369 Src1.ChangeToRegister(Reg, false);
6370 }
6371 return;
6372 }
6373
6374 // No VOP2 instructions support AGPRs.
6375 if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg()))
6376 legalizeOpWithMove(MI, Src0Idx);
6377
6378 if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg()))
6379 legalizeOpWithMove(MI, Src1Idx);
6380
6381 // Special case: V_FMAC_F32 and V_FMAC_F16 have src2.
6382 if (Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F16_e32) {
6383 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
6384 if (!RI.isVGPR(MRI, MI.getOperand(Src2Idx).getReg()))
6385 legalizeOpWithMove(MI, Src2Idx);
6386 }
6387
6388 // VOP2 src0 instructions support all operand types, so we don't need to check
6389 // their legality. If src1 is already legal, we don't need to do anything.
6390 if (isLegalRegOperand(MRI, InstrDesc.operands()[Src1Idx], Src1))
6391 return;
6392
6393 // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for
6394 // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane
6395 // select is uniform.
6396 if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() &&
6397 RI.isVGPR(MRI, Src1.getReg())) {
6398 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6399 const DebugLoc &DL = MI.getDebugLoc();
6400 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
6401 .add(Src1);
6402 Src1.ChangeToRegister(Reg, false);
6403 return;
6404 }
6405
6406 // We do not use commuteInstruction here because it is too aggressive and will
6407 // commute if it is possible. We only want to commute here if it improves
6408 // legality. This can be called a fairly large number of times so don't waste
6409 // compile time pointlessly swapping and checking legality again.
6410 if (HasImplicitSGPR || !MI.isCommutable()) {
6411 legalizeOpWithMove(MI, Src1Idx);
6412 return;
6413 }
6414
6415 // If src0 can be used as src1, commuting will make the operands legal.
6416 // Otherwise we have to give up and insert a move.
6417 //
6418 // TODO: Other immediate-like operand kinds could be commuted if there was a
6419 // MachineOperand::ChangeTo* for them.
6420 if ((!Src1.isImm() && !Src1.isReg()) ||
6421 !isLegalRegOperand(MRI, InstrDesc.operands()[Src1Idx], Src0)) {
6422 legalizeOpWithMove(MI, Src1Idx);
6423 return;
6424 }
6425
6426 int CommutedOpc = commuteOpcode(MI);
6427 if (CommutedOpc == -1) {
6428 legalizeOpWithMove(MI, Src1Idx);
6429 return;
6430 }
6431
6432 MI.setDesc(get(CommutedOpc));
6433
6434 Register Src0Reg = Src0.getReg();
6435 unsigned Src0SubReg = Src0.getSubReg();
6436 bool Src0Kill = Src0.isKill();
6437
6438 if (Src1.isImm())
6439 Src0.ChangeToImmediate(Src1.getImm());
6440 else if (Src1.isReg()) {
6441 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill());
6442 Src0.setSubReg(Src1.getSubReg());
6443 } else
6444 llvm_unreachable("Should only have register or immediate operands");
6445
6446 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill);
6447 Src1.setSubReg(Src0SubReg);
6449}
6450
6451// Legalize VOP3 operands. All operand types are supported for any operand
6452// but only one literal constant and only starting from GFX10.
6454 MachineInstr &MI) const {
6455 unsigned Opc = MI.getOpcode();
6456
6457 int VOP3Idx[3] = {
6458 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0),
6459 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1),
6460 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)
6461 };
6462
6463 if (Opc == AMDGPU::V_PERMLANE16_B32_e64 ||
6464 Opc == AMDGPU::V_PERMLANEX16_B32_e64 ||
6465 Opc == AMDGPU::V_PERMLANE_BCAST_B32_e64 ||
6466 Opc == AMDGPU::V_PERMLANE_UP_B32_e64 ||
6467 Opc == AMDGPU::V_PERMLANE_DOWN_B32_e64 ||
6468 Opc == AMDGPU::V_PERMLANE_XOR_B32_e64 ||
6469 Opc == AMDGPU::V_PERMLANE_IDX_GEN_B32_e64) {
6470 // src1 and src2 must be scalar
6471 MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]);
6472 const DebugLoc &DL = MI.getDebugLoc();
6473 if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) {
6474 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6475 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
6476 .add(Src1);
6477 Src1.ChangeToRegister(Reg, false);
6478 }
6479 if (VOP3Idx[2] != -1) {
6480 MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]);
6481 if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) {
6482 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6483 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
6484 .add(Src2);
6485 Src2.ChangeToRegister(Reg, false);
6486 }
6487 }
6488 }
6489
6490 // Find the one SGPR operand we are allowed to use.
6491 int ConstantBusLimit = ST.getConstantBusLimit(Opc);
6492 int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0;
6493 SmallDenseSet<unsigned> SGPRsUsed;
6494 Register SGPRReg = findUsedSGPR(MI, VOP3Idx);
6495 if (SGPRReg) {
6496 SGPRsUsed.insert(SGPRReg);
6497 --ConstantBusLimit;
6498 }
6499
6500 for (int Idx : VOP3Idx) {
6501 if (Idx == -1)
6502 break;
6503 MachineOperand &MO = MI.getOperand(Idx);
6504
6505 if (!MO.isReg()) {
6506 if (isInlineConstant(MO, get(Opc).operands()[Idx]))
6507 continue;
6508
6509 if (LiteralLimit > 0 && ConstantBusLimit > 0) {
6510 --LiteralLimit;
6511 --ConstantBusLimit;
6512 continue;
6513 }
6514
6515 --LiteralLimit;
6516 --ConstantBusLimit;
6517 legalizeOpWithMove(MI, Idx);
6518 continue;
6519 }
6520
6521 if (RI.hasAGPRs(RI.getRegClassForReg(MRI, MO.getReg())) &&
6522 !isOperandLegal(MI, Idx, &MO)) {
6523 legalizeOpWithMove(MI, Idx);
6524 continue;
6525 }
6526
6527 if (!RI.isSGPRClass(RI.getRegClassForReg(MRI, MO.getReg())))
6528 continue; // VGPRs are legal
6529
6530 // We can use one SGPR in each VOP3 instruction prior to GFX10
6531 // and two starting from GFX10.
6532 if (SGPRsUsed.count(MO.getReg()))
6533 continue;
6534 if (ConstantBusLimit > 0) {
6535 SGPRsUsed.insert(MO.getReg());
6536 --ConstantBusLimit;
6537 continue;
6538 }
6539
6540 // If we make it this far, then the operand is not legal and we must
6541 // legalize it.
6542 legalizeOpWithMove(MI, Idx);
6543 }
6544
6545 // Special case: V_FMAC_F32 and V_FMAC_F16 have src2 tied to vdst.
6546 if ((Opc == AMDGPU::V_FMAC_F32_e64 || Opc == AMDGPU::V_FMAC_F16_e64) &&
6547 !RI.isVGPR(MRI, MI.getOperand(VOP3Idx[2]).getReg()))
6548 legalizeOpWithMove(MI, VOP3Idx[2]);
6549
6550 // Fix the register class of packed FP32 instructions on gfx12+. See
6551 // SIInstrInfo::isLegalGFX12PlusPackedMathFP32Operand for more information.
6553 for (unsigned I = 0; I < 3; ++I) {
6555 legalizeOpWithMove(MI, VOP3Idx[I]);
6556 }
6557 }
6558}
6559
6562 const TargetRegisterClass *DstRC /*=nullptr*/) const {
6563 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg);
6564 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC);
6565 if (DstRC)
6566 SRC = RI.getCommonSubClass(SRC, DstRC);
6567
6568 Register DstReg = MRI.createVirtualRegister(SRC);
6569 unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32;
6570
6571 if (RI.hasAGPRs(VRC)) {
6572 VRC = RI.getEquivalentVGPRClass(VRC);
6573 Register NewSrcReg = MRI.createVirtualRegister(VRC);
6574 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
6575 get(TargetOpcode::COPY), NewSrcReg)
6576 .addReg(SrcReg);
6577 SrcReg = NewSrcReg;
6578 }
6579
6580 if (SubRegs == 1) {
6581 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
6582 get(AMDGPU::V_READFIRSTLANE_B32), DstReg)
6583 .addReg(SrcReg);
6584 return DstReg;
6585 }
6586
6588 for (unsigned i = 0; i < SubRegs; ++i) {
6589 Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
6590 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
6591 get(AMDGPU::V_READFIRSTLANE_B32), SGPR)
6592 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i));
6593 SRegs.push_back(SGPR);
6594 }
6595
6597 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
6598 get(AMDGPU::REG_SEQUENCE), DstReg);
6599 for (unsigned i = 0; i < SubRegs; ++i) {
6600 MIB.addReg(SRegs[i]);
6601 MIB.addImm(RI.getSubRegFromChannel(i));
6602 }
6603 return DstReg;
6604}
6605
6607 MachineInstr &MI) const {
6608
6609 // If the pointer is store in VGPRs, then we need to move them to
6610 // SGPRs using v_readfirstlane. This is safe because we only select
6611 // loads with uniform pointers to SMRD instruction so we know the
6612 // pointer value is uniform.
6613 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase);
6614 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) {
6615 Register SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI);
6616 SBase->setReg(SGPR);
6617 }
6618 MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soffset);
6619 if (SOff && !RI.isSGPRReg(MRI, SOff->getReg())) {
6620 Register SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI);
6621 SOff->setReg(SGPR);
6622 }
6623}
6624
6626 unsigned Opc = Inst.getOpcode();
6627 int OldSAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::saddr);
6628 if (OldSAddrIdx < 0)
6629 return false;
6630
6631 assert(isSegmentSpecificFLAT(Inst) || (isFLAT(Inst) && ST.hasFlatGVSMode()));
6632
6633 int NewOpc = AMDGPU::getGlobalVaddrOp(Opc);
6634 if (NewOpc < 0)
6636 if (NewOpc < 0)
6637 return false;
6638
6640 MachineOperand &SAddr = Inst.getOperand(OldSAddrIdx);
6641 if (RI.isSGPRReg(MRI, SAddr.getReg()))
6642 return false;
6643
6644 int NewVAddrIdx = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vaddr);
6645 if (NewVAddrIdx < 0)
6646 return false;
6647
6648 int OldVAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr);
6649
6650 // Check vaddr, it shall be zero or absent.
6651 MachineInstr *VAddrDef = nullptr;
6652 if (OldVAddrIdx >= 0) {
6653 MachineOperand &VAddr = Inst.getOperand(OldVAddrIdx);
6654 VAddrDef = MRI.getUniqueVRegDef(VAddr.getReg());
6655 if (!VAddrDef || !VAddrDef->isMoveImmediate() ||
6656 !VAddrDef->getOperand(1).isImm() ||
6657 VAddrDef->getOperand(1).getImm() != 0)
6658 return false;
6659 }
6660
6661 const MCInstrDesc &NewDesc = get(NewOpc);
6662 Inst.setDesc(NewDesc);
6663
6664 // Callers expect iterator to be valid after this call, so modify the
6665 // instruction in place.
6666 if (OldVAddrIdx == NewVAddrIdx) {
6667 MachineOperand &NewVAddr = Inst.getOperand(NewVAddrIdx);
6668 // Clear use list from the old vaddr holding a zero register.
6669 MRI.removeRegOperandFromUseList(&NewVAddr);
6670 MRI.moveOperands(&NewVAddr, &SAddr, 1);
6671 Inst.removeOperand(OldSAddrIdx);
6672 // Update the use list with the pointer we have just moved from vaddr to
6673 // saddr position. Otherwise new vaddr will be missing from the use list.
6674 MRI.removeRegOperandFromUseList(&NewVAddr);
6675 MRI.addRegOperandToUseList(&NewVAddr);
6676 } else {
6677 assert(OldSAddrIdx == NewVAddrIdx);
6678
6679 if (OldVAddrIdx >= 0) {
6680 int NewVDstIn = AMDGPU::getNamedOperandIdx(NewOpc,
6681 AMDGPU::OpName::vdst_in);
6682
6683 // removeOperand doesn't try to fixup tied operand indexes at it goes, so
6684 // it asserts. Untie the operands for now and retie them afterwards.
6685 if (NewVDstIn != -1) {
6686 int OldVDstIn = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst_in);
6687 Inst.untieRegOperand(OldVDstIn);
6688 }
6689
6690 Inst.removeOperand(OldVAddrIdx);
6691
6692 if (NewVDstIn != -1) {
6693 int NewVDst = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vdst);
6694 Inst.tieOperands(NewVDst, NewVDstIn);
6695 }
6696 }
6697 }
6698
6699 if (VAddrDef && MRI.use_nodbg_empty(VAddrDef->getOperand(0).getReg()))
6700 VAddrDef->eraseFromParent();
6701
6702 return true;
6703}
6704
6705// FIXME: Remove this when SelectionDAG is obsoleted.
6707 MachineInstr &MI) const {
6708 if (!isSegmentSpecificFLAT(MI) && !ST.hasFlatGVSMode())
6709 return;
6710
6711 // Fixup SGPR operands in VGPRs. We only select these when the DAG divergence
6712 // thinks they are uniform, so a readfirstlane should be valid.
6713 MachineOperand *SAddr = getNamedOperand(MI, AMDGPU::OpName::saddr);
6714 if (!SAddr || RI.isSGPRClass(MRI.getRegClass(SAddr->getReg())))
6715 return;
6716
6718 return;
6719
6720 const TargetRegisterClass *DeclaredRC =
6721 getRegClass(MI.getDesc(), SAddr->getOperandNo(), &RI);
6722
6723 Register ToSGPR = readlaneVGPRToSGPR(SAddr->getReg(), MI, MRI, DeclaredRC);
6724 SAddr->setReg(ToSGPR);
6725}
6726
6729 const TargetRegisterClass *DstRC,
6732 const DebugLoc &DL) const {
6733 Register OpReg = Op.getReg();
6734 unsigned OpSubReg = Op.getSubReg();
6735
6736 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg(
6737 RI.getRegClassForReg(MRI, OpReg), OpSubReg);
6738
6739 // Check if operand is already the correct register class.
6740 if (DstRC == OpRC)
6741 return;
6742
6743 Register DstReg = MRI.createVirtualRegister(DstRC);
6744 auto Copy =
6745 BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).addReg(OpReg);
6746 Op.setReg(DstReg);
6747
6748 MachineInstr *Def = MRI.getVRegDef(OpReg);
6749 if (!Def)
6750 return;
6751
6752 // Try to eliminate the copy if it is copying an immediate value.
6753 if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass)
6754 foldImmediate(*Copy, *Def, OpReg, &MRI);
6755
6756 bool ImpDef = Def->isImplicitDef();
6757 while (!ImpDef && Def && Def->isCopy()) {
6758 if (Def->getOperand(1).getReg().isPhysical())
6759 break;
6760 Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg());
6761 ImpDef = Def && Def->isImplicitDef();
6762 }
6763 if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) &&
6764 !ImpDef)
6765 Copy.addReg(AMDGPU::EXEC, RegState::Implicit);
6766}
6767
6768// Emit the actual waterfall loop, executing the wrapped instruction for each
6769// unique value of \p ScalarOps across all lanes. In the best case we execute 1
6770// iteration, in the worst case we execute 64 (once per lane).
6771static void
6774 MachineBasicBlock &LoopBB,
6775 MachineBasicBlock &BodyBB,
6776 const DebugLoc &DL,
6777 ArrayRef<MachineOperand *> ScalarOps) {
6778 MachineFunction &MF = *LoopBB.getParent();
6779 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
6780 const SIRegisterInfo *TRI = ST.getRegisterInfo();
6782 const auto *BoolXExecRC = TRI->getWaveMaskRegClass();
6783
6785 Register CondReg;
6786
6787 for (MachineOperand *ScalarOp : ScalarOps) {
6788 unsigned RegSize = TRI->getRegSizeInBits(ScalarOp->getReg(), MRI);
6789 unsigned NumSubRegs = RegSize / 32;
6790 Register VScalarOp = ScalarOp->getReg();
6791
6792 if (NumSubRegs == 1) {
6793 Register CurReg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6794
6795 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurReg)
6796 .addReg(VScalarOp);
6797
6798 Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC);
6799
6800 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U32_e64), NewCondReg)
6801 .addReg(CurReg)
6802 .addReg(VScalarOp);
6803
6804 // Combine the comparison results with AND.
6805 if (!CondReg) // First.
6806 CondReg = NewCondReg;
6807 else { // If not the first, we create an AND.
6808 Register AndReg = MRI.createVirtualRegister(BoolXExecRC);
6809 BuildMI(LoopBB, I, DL, TII.get(LMC.AndOpc), AndReg)
6810 .addReg(CondReg)
6811 .addReg(NewCondReg);
6812 CondReg = AndReg;
6813 }
6814
6815 // Update ScalarOp operand to use the SGPR ScalarOp.
6816 ScalarOp->setReg(CurReg);
6817 ScalarOp->setIsKill();
6818 } else {
6819 SmallVector<Register, 8> ReadlanePieces;
6820 unsigned VScalarOpUndef = getUndefRegState(ScalarOp->isUndef());
6821 assert(NumSubRegs % 2 == 0 && NumSubRegs <= 32 &&
6822 "Unhandled register size");
6823
6824 for (unsigned Idx = 0; Idx < NumSubRegs; Idx += 2) {
6825 Register CurRegLo =
6826 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6827 Register CurRegHi =
6828 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6829
6830 // Read the next variant <- also loop target.
6831 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegLo)
6832 .addReg(VScalarOp, VScalarOpUndef, TRI->getSubRegFromChannel(Idx));
6833
6834 // Read the next variant <- also loop target.
6835 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegHi)
6836 .addReg(VScalarOp, VScalarOpUndef,
6837 TRI->getSubRegFromChannel(Idx + 1));
6838
6839 ReadlanePieces.push_back(CurRegLo);
6840 ReadlanePieces.push_back(CurRegHi);
6841
6842 // Comparison is to be done as 64-bit.
6843 Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
6844 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), CurReg)
6845 .addReg(CurRegLo)
6846 .addImm(AMDGPU::sub0)
6847 .addReg(CurRegHi)
6848 .addImm(AMDGPU::sub1);
6849
6850 Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC);
6851 auto Cmp = BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64),
6852 NewCondReg)
6853 .addReg(CurReg);
6854 if (NumSubRegs <= 2)
6855 Cmp.addReg(VScalarOp);
6856 else
6857 Cmp.addReg(VScalarOp, VScalarOpUndef,
6858 TRI->getSubRegFromChannel(Idx, 2));
6859
6860 // Combine the comparison results with AND.
6861 if (!CondReg) // First.
6862 CondReg = NewCondReg;
6863 else { // If not the first, we create an AND.
6864 Register AndReg = MRI.createVirtualRegister(BoolXExecRC);
6865 BuildMI(LoopBB, I, DL, TII.get(LMC.AndOpc), AndReg)
6866 .addReg(CondReg)
6867 .addReg(NewCondReg);
6868 CondReg = AndReg;
6869 }
6870 } // End for loop.
6871
6872 const auto *SScalarOpRC =
6873 TRI->getEquivalentSGPRClass(MRI.getRegClass(VScalarOp));
6874 Register SScalarOp = MRI.createVirtualRegister(SScalarOpRC);
6875
6876 // Build scalar ScalarOp.
6877 auto Merge =
6878 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SScalarOp);
6879 unsigned Channel = 0;
6880 for (Register Piece : ReadlanePieces) {
6881 Merge.addReg(Piece).addImm(TRI->getSubRegFromChannel(Channel++));
6882 }
6883
6884 // Update ScalarOp operand to use the SGPR ScalarOp.
6885 ScalarOp->setReg(SScalarOp);
6886 ScalarOp->setIsKill();
6887 }
6888 }
6889
6890 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
6891 MRI.setSimpleHint(SaveExec, CondReg);
6892
6893 // Update EXEC to matching lanes, saving original to SaveExec.
6894 BuildMI(LoopBB, I, DL, TII.get(LMC.AndSaveExecOpc), SaveExec)
6895 .addReg(CondReg, RegState::Kill);
6896
6897 // The original instruction is here; we insert the terminators after it.
6898 I = BodyBB.end();
6899
6900 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
6901 BuildMI(BodyBB, I, DL, TII.get(LMC.XorTermOpc), LMC.ExecReg)
6902 .addReg(LMC.ExecReg)
6903 .addReg(SaveExec);
6904
6905 BuildMI(BodyBB, I, DL, TII.get(AMDGPU::SI_WATERFALL_LOOP)).addMBB(&LoopBB);
6906}
6907
6908// Build a waterfall loop around \p MI, replacing the VGPR \p ScalarOp register
6909// with SGPRs by iterating over all unique values across all lanes.
6910// Returns the loop basic block that now contains \p MI.
6911static MachineBasicBlock *
6915 MachineBasicBlock::iterator Begin = nullptr,
6916 MachineBasicBlock::iterator End = nullptr) {
6917 MachineBasicBlock &MBB = *MI.getParent();
6918 MachineFunction &MF = *MBB.getParent();
6919 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
6920 const SIRegisterInfo *TRI = ST.getRegisterInfo();
6922 if (!Begin.isValid())
6923 Begin = &MI;
6924 if (!End.isValid()) {
6925 End = &MI;
6926 ++End;
6927 }
6928 const DebugLoc &DL = MI.getDebugLoc();
6930 const auto *BoolXExecRC = TRI->getWaveMaskRegClass();
6931
6932 // Save SCC. Waterfall Loop may overwrite SCC.
6933 Register SaveSCCReg;
6934
6935 // FIXME: We should maintain SCC liveness while doing the FixSGPRCopies walk
6936 // rather than unlimited scan everywhere
6937 bool SCCNotDead =
6938 MBB.computeRegisterLiveness(TRI, AMDGPU::SCC, MI,
6939 std::numeric_limits<unsigned>::max()) !=
6941 if (SCCNotDead) {
6942 SaveSCCReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6943 BuildMI(MBB, Begin, DL, TII.get(AMDGPU::S_CSELECT_B32), SaveSCCReg)
6944 .addImm(1)
6945 .addImm(0);
6946 }
6947
6948 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
6949
6950 // Save the EXEC mask
6951 BuildMI(MBB, Begin, DL, TII.get(LMC.MovOpc), SaveExec).addReg(LMC.ExecReg);
6952
6953 // Killed uses in the instruction we are waterfalling around will be
6954 // incorrect due to the added control-flow.
6956 ++AfterMI;
6957 for (auto I = Begin; I != AfterMI; I++) {
6958 for (auto &MO : I->all_uses())
6959 MRI.clearKillFlags(MO.getReg());
6960 }
6961
6962 // To insert the loop we need to split the block. Move everything after this
6963 // point to a new block, and insert a new empty block between the two.
6966 MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock();
6968 ++MBBI;
6969
6970 MF.insert(MBBI, LoopBB);
6971 MF.insert(MBBI, BodyBB);
6972 MF.insert(MBBI, RemainderBB);
6973
6974 LoopBB->addSuccessor(BodyBB);
6975 BodyBB->addSuccessor(LoopBB);
6976 BodyBB->addSuccessor(RemainderBB);
6977
6978 // Move Begin to MI to the BodyBB, and the remainder of the block to
6979 // RemainderBB.
6980 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
6981 RemainderBB->splice(RemainderBB->begin(), &MBB, End, MBB.end());
6982 BodyBB->splice(BodyBB->begin(), &MBB, Begin, MBB.end());
6983
6984 MBB.addSuccessor(LoopBB);
6985
6986 // Update dominators. We know that MBB immediately dominates LoopBB, that
6987 // LoopBB immediately dominates BodyBB, and BodyBB immediately dominates
6988 // RemainderBB. RemainderBB immediately dominates all of the successors
6989 // transferred to it from MBB that MBB used to properly dominate.
6990 if (MDT) {
6991 MDT->addNewBlock(LoopBB, &MBB);
6992 MDT->addNewBlock(BodyBB, LoopBB);
6993 MDT->addNewBlock(RemainderBB, BodyBB);
6994 for (auto &Succ : RemainderBB->successors()) {
6995 if (MDT->properlyDominates(&MBB, Succ)) {
6996 MDT->changeImmediateDominator(Succ, RemainderBB);
6997 }
6998 }
6999 }
7000
7001 emitLoadScalarOpsFromVGPRLoop(TII, MRI, *LoopBB, *BodyBB, DL, ScalarOps);
7002
7003 MachineBasicBlock::iterator First = RemainderBB->begin();
7004 // Restore SCC
7005 if (SCCNotDead) {
7006 BuildMI(*RemainderBB, First, DL, TII.get(AMDGPU::S_CMP_LG_U32))
7007 .addReg(SaveSCCReg, RegState::Kill)
7008 .addImm(0);
7009 }
7010
7011 // Restore the EXEC mask
7012 BuildMI(*RemainderBB, First, DL, TII.get(LMC.MovOpc), LMC.ExecReg)
7013 .addReg(SaveExec);
7014 return BodyBB;
7015}
7016
7017// Extract pointer from Rsrc and return a zero-value Rsrc replacement.
7018static std::tuple<unsigned, unsigned>
7020 MachineBasicBlock &MBB = *MI.getParent();
7021 MachineFunction &MF = *MBB.getParent();
7023
7024 // Extract the ptr from the resource descriptor.
7025 unsigned RsrcPtr =
7026 TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass,
7027 AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass);
7028
7029 // Create an empty resource descriptor
7030 Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
7031 Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
7032 Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
7033 Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass);
7034 uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat();
7035
7036 // Zero64 = 0
7037 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64)
7038 .addImm(0);
7039
7040 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
7041 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo)
7042 .addImm(Lo_32(RsrcDataFormat));
7043
7044 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
7045 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi)
7046 .addImm(Hi_32(RsrcDataFormat));
7047
7048 // NewSRsrc = {Zero64, SRsrcFormat}
7049 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc)
7050 .addReg(Zero64)
7051 .addImm(AMDGPU::sub0_sub1)
7052 .addReg(SRsrcFormatLo)
7053 .addImm(AMDGPU::sub2)
7054 .addReg(SRsrcFormatHi)
7055 .addImm(AMDGPU::sub3);
7056
7057 return std::tuple(RsrcPtr, NewSRsrc);
7058}
7059
7062 MachineDominatorTree *MDT) const {
7063 MachineFunction &MF = *MI.getParent()->getParent();
7065 MachineBasicBlock *CreatedBB = nullptr;
7066
7067 // Legalize VOP2
7068 if (isVOP2(MI) || isVOPC(MI)) {
7070 return CreatedBB;
7071 }
7072
7073 // Legalize VOP3
7074 if (isVOP3(MI)) {
7076 return CreatedBB;
7077 }
7078
7079 // Legalize SMRD
7080 if (isSMRD(MI)) {
7082 return CreatedBB;
7083 }
7084
7085 // Legalize FLAT
7086 if (isFLAT(MI)) {
7088 return CreatedBB;
7089 }
7090
7091 // Legalize REG_SEQUENCE and PHI
7092 // The register class of the operands much be the same type as the register
7093 // class of the output.
7094 if (MI.getOpcode() == AMDGPU::PHI) {
7095 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr;
7096 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
7097 if (!MI.getOperand(i).isReg() || !MI.getOperand(i).getReg().isVirtual())
7098 continue;
7099 const TargetRegisterClass *OpRC =
7100 MRI.getRegClass(MI.getOperand(i).getReg());
7101 if (RI.hasVectorRegisters(OpRC)) {
7102 VRC = OpRC;
7103 } else {
7104 SRC = OpRC;
7105 }
7106 }
7107
7108 // If any of the operands are VGPR registers, then they all most be
7109 // otherwise we will create illegal VGPR->SGPR copies when legalizing
7110 // them.
7111 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) {
7112 if (!VRC) {
7113 assert(SRC);
7114 if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) {
7115 VRC = &AMDGPU::VReg_1RegClass;
7116 } else
7117 VRC = RI.isAGPRClass(getOpRegClass(MI, 0))
7118 ? RI.getEquivalentAGPRClass(SRC)
7119 : RI.getEquivalentVGPRClass(SRC);
7120 } else {
7121 VRC = RI.isAGPRClass(getOpRegClass(MI, 0))
7122 ? RI.getEquivalentAGPRClass(VRC)
7123 : RI.getEquivalentVGPRClass(VRC);
7124 }
7125 RC = VRC;
7126 } else {
7127 RC = SRC;
7128 }
7129
7130 // Update all the operands so they have the same type.
7131 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
7132 MachineOperand &Op = MI.getOperand(I);
7133 if (!Op.isReg() || !Op.getReg().isVirtual())
7134 continue;
7135
7136 // MI is a PHI instruction.
7137 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB();
7139
7140 // Avoid creating no-op copies with the same src and dst reg class. These
7141 // confuse some of the machine passes.
7142 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc());
7143 }
7144 }
7145
7146 // REG_SEQUENCE doesn't really require operand legalization, but if one has a
7147 // VGPR dest type and SGPR sources, insert copies so all operands are
7148 // VGPRs. This seems to help operand folding / the register coalescer.
7149 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) {
7150 MachineBasicBlock *MBB = MI.getParent();
7151 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0);
7152 if (RI.hasVGPRs(DstRC)) {
7153 // Update all the operands so they are VGPR register classes. These may
7154 // not be the same register class because REG_SEQUENCE supports mixing
7155 // subregister index types e.g. sub0_sub1 + sub2 + sub3
7156 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
7157 MachineOperand &Op = MI.getOperand(I);
7158 if (!Op.isReg() || !Op.getReg().isVirtual())
7159 continue;
7160
7161 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg());
7162 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC);
7163 if (VRC == OpRC)
7164 continue;
7165
7166 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc());
7167 Op.setIsKill();
7168 }
7169 }
7170
7171 return CreatedBB;
7172 }
7173
7174 // Legalize INSERT_SUBREG
7175 // src0 must have the same register class as dst
7176 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) {
7177 Register Dst = MI.getOperand(0).getReg();
7178 Register Src0 = MI.getOperand(1).getReg();
7179 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
7180 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0);
7181 if (DstRC != Src0RC) {
7182 MachineBasicBlock *MBB = MI.getParent();
7183 MachineOperand &Op = MI.getOperand(1);
7184 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc());
7185 }
7186 return CreatedBB;
7187 }
7188
7189 // Legalize SI_INIT_M0
7190 if (MI.getOpcode() == AMDGPU::SI_INIT_M0) {
7191 MachineOperand &Src = MI.getOperand(0);
7192 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg())))
7193 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI));
7194 return CreatedBB;
7195 }
7196
7197 // Legalize S_BITREPLICATE, S_QUADMASK and S_WQM
7198 if (MI.getOpcode() == AMDGPU::S_BITREPLICATE_B64_B32 ||
7199 MI.getOpcode() == AMDGPU::S_QUADMASK_B32 ||
7200 MI.getOpcode() == AMDGPU::S_QUADMASK_B64 ||
7201 MI.getOpcode() == AMDGPU::S_WQM_B32 ||
7202 MI.getOpcode() == AMDGPU::S_WQM_B64 ||
7203 MI.getOpcode() == AMDGPU::S_INVERSE_BALLOT_U32 ||
7204 MI.getOpcode() == AMDGPU::S_INVERSE_BALLOT_U64) {
7205 MachineOperand &Src = MI.getOperand(1);
7206 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg())))
7207 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI));
7208 return CreatedBB;
7209 }
7210
7211 // Legalize MIMG/VIMAGE/VSAMPLE and MUBUF/MTBUF for shaders.
7212 //
7213 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via
7214 // scratch memory access. In both cases, the legalization never involves
7215 // conversion to the addr64 form.
7217 (isMUBUF(MI) || isMTBUF(MI)))) {
7218 AMDGPU::OpName RSrcOpName = (isVIMAGE(MI) || isVSAMPLE(MI))
7219 ? AMDGPU::OpName::rsrc
7220 : AMDGPU::OpName::srsrc;
7221 MachineOperand *SRsrc = getNamedOperand(MI, RSrcOpName);
7222 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg())))
7223 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {SRsrc}, MDT);
7224
7225 AMDGPU::OpName SampOpName =
7226 isMIMG(MI) ? AMDGPU::OpName::ssamp : AMDGPU::OpName::samp;
7227 MachineOperand *SSamp = getNamedOperand(MI, SampOpName);
7228 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg())))
7229 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {SSamp}, MDT);
7230
7231 return CreatedBB;
7232 }
7233
7234 // Legalize SI_CALL
7235 if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) {
7236 MachineOperand *Dest = &MI.getOperand(0);
7237 if (!RI.isSGPRClass(MRI.getRegClass(Dest->getReg()))) {
7238 // Move everything between ADJCALLSTACKUP and ADJCALLSTACKDOWN and
7239 // following copies, we also need to move copies from and to physical
7240 // registers into the loop block.
7241 unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
7242 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
7243
7244 // Also move the copies to physical registers into the loop block
7245 MachineBasicBlock &MBB = *MI.getParent();
7247 while (Start->getOpcode() != FrameSetupOpcode)
7248 --Start;
7250 while (End->getOpcode() != FrameDestroyOpcode)
7251 ++End;
7252 // Also include following copies of the return value
7253 ++End;
7254 while (End != MBB.end() && End->isCopy() && End->getOperand(1).isReg() &&
7255 MI.definesRegister(End->getOperand(1).getReg(), /*TRI=*/nullptr))
7256 ++End;
7257 CreatedBB =
7258 loadMBUFScalarOperandsFromVGPR(*this, MI, {Dest}, MDT, Start, End);
7259 }
7260 }
7261
7262 // Legalize s_sleep_var.
7263 if (MI.getOpcode() == AMDGPU::S_SLEEP_VAR) {
7264 const DebugLoc &DL = MI.getDebugLoc();
7265 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
7266 int Src0Idx =
7267 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
7268 MachineOperand &Src0 = MI.getOperand(Src0Idx);
7269 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
7270 .add(Src0);
7271 Src0.ChangeToRegister(Reg, false);
7272 return nullptr;
7273 }
7274
7275 // Legalize TENSOR_LOAD_TO_LDS, TENSOR_LOAD_TO_LDS_D2, TENSOR_STORE_FROM_LDS,
7276 // TENSOR_STORE_FROM_LDS_D2. All their operands are scalar.
7277 if (MI.getOpcode() == AMDGPU::TENSOR_LOAD_TO_LDS ||
7278 MI.getOpcode() == AMDGPU::TENSOR_LOAD_TO_LDS_D2 ||
7279 MI.getOpcode() == AMDGPU::TENSOR_STORE_FROM_LDS ||
7280 MI.getOpcode() == AMDGPU::TENSOR_STORE_FROM_LDS_D2) {
7281 for (MachineOperand &Src : MI.explicit_operands()) {
7282 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg())))
7283 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI));
7284 }
7285 return CreatedBB;
7286 }
7287
7288 // Legalize MUBUF instructions.
7289 bool isSoffsetLegal = true;
7290 int SoffsetIdx =
7291 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::soffset);
7292 if (SoffsetIdx != -1) {
7293 MachineOperand *Soffset = &MI.getOperand(SoffsetIdx);
7294 if (Soffset->isReg() && Soffset->getReg().isVirtual() &&
7295 !RI.isSGPRClass(MRI.getRegClass(Soffset->getReg()))) {
7296 isSoffsetLegal = false;
7297 }
7298 }
7299
7300 bool isRsrcLegal = true;
7301 int RsrcIdx =
7302 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc);
7303 if (RsrcIdx != -1) {
7304 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx);
7305 if (Rsrc->isReg() && !RI.isSGPRReg(MRI, Rsrc->getReg()))
7306 isRsrcLegal = false;
7307 }
7308
7309 // The operands are legal.
7310 if (isRsrcLegal && isSoffsetLegal)
7311 return CreatedBB;
7312
7313 if (!isRsrcLegal) {
7314 // Legalize a VGPR Rsrc
7315 //
7316 // If the instruction is _ADDR64, we can avoid a waterfall by extracting
7317 // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using
7318 // a zero-value SRsrc.
7319 //
7320 // If the instruction is _OFFSET (both idxen and offen disabled), and we
7321 // support ADDR64 instructions, we can convert to ADDR64 and do the same as
7322 // above.
7323 //
7324 // Otherwise we are on non-ADDR64 hardware, and/or we have
7325 // idxen/offen/bothen and we fall back to a waterfall loop.
7326
7327 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx);
7328 MachineBasicBlock &MBB = *MI.getParent();
7329
7330 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
7331 if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) {
7332 // This is already an ADDR64 instruction so we need to add the pointer
7333 // extracted from the resource descriptor to the current value of VAddr.
7334 Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7335 Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7336 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
7337
7338 const auto *BoolXExecRC = RI.getWaveMaskRegClass();
7339 Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC);
7340 Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC);
7341
7342 unsigned RsrcPtr, NewSRsrc;
7343 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
7344
7345 // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0
7346 const DebugLoc &DL = MI.getDebugLoc();
7347 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_CO_U32_e64), NewVAddrLo)
7348 .addDef(CondReg0)
7349 .addReg(RsrcPtr, 0, AMDGPU::sub0)
7350 .addReg(VAddr->getReg(), 0, AMDGPU::sub0)
7351 .addImm(0);
7352
7353 // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1
7354 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi)
7355 .addDef(CondReg1, RegState::Dead)
7356 .addReg(RsrcPtr, 0, AMDGPU::sub1)
7357 .addReg(VAddr->getReg(), 0, AMDGPU::sub1)
7358 .addReg(CondReg0, RegState::Kill)
7359 .addImm(0);
7360
7361 // NewVaddr = {NewVaddrHi, NewVaddrLo}
7362 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr)
7363 .addReg(NewVAddrLo)
7364 .addImm(AMDGPU::sub0)
7365 .addReg(NewVAddrHi)
7366 .addImm(AMDGPU::sub1);
7367
7368 VAddr->setReg(NewVAddr);
7369 Rsrc->setReg(NewSRsrc);
7370 } else if (!VAddr && ST.hasAddr64()) {
7371 // This instructions is the _OFFSET variant, so we need to convert it to
7372 // ADDR64.
7373 assert(ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS &&
7374 "FIXME: Need to emit flat atomics here");
7375
7376 unsigned RsrcPtr, NewSRsrc;
7377 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
7378
7379 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
7380 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata);
7381 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
7382 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset);
7383 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode());
7384
7385 // Atomics with return have an additional tied operand and are
7386 // missing some of the special bits.
7387 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in);
7388 MachineInstr *Addr64;
7389
7390 if (!VDataIn) {
7391 // Regular buffer load / store.
7393 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
7394 .add(*VData)
7395 .addReg(NewVAddr)
7396 .addReg(NewSRsrc)
7397 .add(*SOffset)
7398 .add(*Offset);
7399
7400 if (const MachineOperand *CPol =
7401 getNamedOperand(MI, AMDGPU::OpName::cpol)) {
7402 MIB.addImm(CPol->getImm());
7403 }
7404
7405 if (const MachineOperand *TFE =
7406 getNamedOperand(MI, AMDGPU::OpName::tfe)) {
7407 MIB.addImm(TFE->getImm());
7408 }
7409
7410 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz));
7411
7412 MIB.cloneMemRefs(MI);
7413 Addr64 = MIB;
7414 } else {
7415 // Atomics with return.
7416 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
7417 .add(*VData)
7418 .add(*VDataIn)
7419 .addReg(NewVAddr)
7420 .addReg(NewSRsrc)
7421 .add(*SOffset)
7422 .add(*Offset)
7423 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::cpol))
7424 .cloneMemRefs(MI);
7425 }
7426
7427 MI.removeFromParent();
7428
7429 // NewVaddr = {NewVaddrHi, NewVaddrLo}
7430 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
7431 NewVAddr)
7432 .addReg(RsrcPtr, 0, AMDGPU::sub0)
7433 .addImm(AMDGPU::sub0)
7434 .addReg(RsrcPtr, 0, AMDGPU::sub1)
7435 .addImm(AMDGPU::sub1);
7436 } else {
7437 // Legalize a VGPR Rsrc and soffset together.
7438 if (!isSoffsetLegal) {
7439 MachineOperand *Soffset = getNamedOperand(MI, AMDGPU::OpName::soffset);
7440 CreatedBB =
7441 loadMBUFScalarOperandsFromVGPR(*this, MI, {Rsrc, Soffset}, MDT);
7442 return CreatedBB;
7443 }
7444 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {Rsrc}, MDT);
7445 return CreatedBB;
7446 }
7447 }
7448
7449 // Legalize a VGPR soffset.
7450 if (!isSoffsetLegal) {
7451 MachineOperand *Soffset = getNamedOperand(MI, AMDGPU::OpName::soffset);
7452 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {Soffset}, MDT);
7453 return CreatedBB;
7454 }
7455 return CreatedBB;
7456}
7457
7459 InstrList.insert(MI);
7460 // Add MBUF instructiosn to deferred list.
7461 int RsrcIdx =
7462 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::srsrc);
7463 if (RsrcIdx != -1) {
7464 DeferredList.insert(MI);
7465 }
7466}
7467
7469 return DeferredList.contains(MI);
7470}
7471
7472// Legalize size mismatches between 16bit and 32bit registers in v2s copy
7473// lowering (change spgr to vgpr).
7474// This is mainly caused by 16bit SALU and 16bit VALU using reg with different
7475// size. Need to legalize the size of the operands during the vgpr lowering
7476// chain. This can be removed after we have sgpr16 in place
7478 MachineRegisterInfo &MRI) const {
7479 if (!ST.useRealTrue16Insts())
7480 return;
7481
7482 unsigned Opcode = MI.getOpcode();
7483 MachineBasicBlock *MBB = MI.getParent();
7484 // Legalize operands and check for size mismatch
7485 if (!OpIdx || OpIdx >= MI.getNumExplicitOperands() ||
7486 OpIdx >= get(Opcode).getNumOperands() ||
7487 get(Opcode).operands()[OpIdx].RegClass == -1)
7488 return;
7489
7490 MachineOperand &Op = MI.getOperand(OpIdx);
7491 if (!Op.isReg() || !Op.getReg().isVirtual())
7492 return;
7493
7494 const TargetRegisterClass *CurrRC = MRI.getRegClass(Op.getReg());
7495 if (!RI.isVGPRClass(CurrRC))
7496 return;
7497
7498 unsigned RCID = get(Opcode).operands()[OpIdx].RegClass;
7499 const TargetRegisterClass *ExpectedRC = RI.getRegClass(RCID);
7500 if (RI.getMatchingSuperRegClass(CurrRC, ExpectedRC, AMDGPU::lo16)) {
7501 Op.setSubReg(AMDGPU::lo16);
7502 } else if (RI.getMatchingSuperRegClass(ExpectedRC, CurrRC, AMDGPU::lo16)) {
7503 const DebugLoc &DL = MI.getDebugLoc();
7504 Register NewDstReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7505 Register Undef = MRI.createVirtualRegister(&AMDGPU::VGPR_16RegClass);
7506 BuildMI(*MBB, MI, DL, get(AMDGPU::IMPLICIT_DEF), Undef);
7507 BuildMI(*MBB, MI, DL, get(AMDGPU::REG_SEQUENCE), NewDstReg)
7508 .addReg(Op.getReg())
7509 .addImm(AMDGPU::lo16)
7510 .addReg(Undef)
7511 .addImm(AMDGPU::hi16);
7512 Op.setReg(NewDstReg);
7513 }
7514}
7516 MachineRegisterInfo &MRI) const {
7517 for (unsigned OpIdx = 1; OpIdx < MI.getNumExplicitOperands(); OpIdx++)
7519}
7520
7522 MachineDominatorTree *MDT) const {
7523
7524 while (!Worklist.empty()) {
7525 MachineInstr &Inst = *Worklist.top();
7526 Worklist.erase_top();
7527 // Skip MachineInstr in the deferred list.
7528 if (Worklist.isDeferred(&Inst))
7529 continue;
7530 moveToVALUImpl(Worklist, MDT, Inst);
7531 }
7532
7533 // Deferred list of instructions will be processed once
7534 // all the MachineInstr in the worklist are done.
7535 for (MachineInstr *Inst : Worklist.getDeferredList()) {
7536 moveToVALUImpl(Worklist, MDT, *Inst);
7537 assert(Worklist.empty() &&
7538 "Deferred MachineInstr are not supposed to re-populate worklist");
7539 }
7540}
7541
7544 MachineInstr &Inst) const {
7545
7547 if (!MBB)
7548 return;
7549 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7550 unsigned Opcode = Inst.getOpcode();
7551 unsigned NewOpcode = getVALUOp(Inst);
7552 // Handle some special cases
7553 switch (Opcode) {
7554 default:
7555 break;
7556 case AMDGPU::S_ADD_I32:
7557 case AMDGPU::S_SUB_I32: {
7558 // FIXME: The u32 versions currently selected use the carry.
7559 bool Changed;
7560 MachineBasicBlock *CreatedBBTmp = nullptr;
7561 std::tie(Changed, CreatedBBTmp) = moveScalarAddSub(Worklist, Inst, MDT);
7562 if (Changed)
7563 return;
7564
7565 // Default handling
7566 break;
7567 }
7568
7569 case AMDGPU::S_MUL_U64:
7570 if (ST.hasVectorMulU64()) {
7571 NewOpcode = AMDGPU::V_MUL_U64_e64;
7572 break;
7573 }
7574 // Split s_mul_u64 in 32-bit vector multiplications.
7575 splitScalarSMulU64(Worklist, Inst, MDT);
7576 Inst.eraseFromParent();
7577 return;
7578
7579 case AMDGPU::S_MUL_U64_U32_PSEUDO:
7580 case AMDGPU::S_MUL_I64_I32_PSEUDO:
7581 // This is a special case of s_mul_u64 where all the operands are either
7582 // zero extended or sign extended.
7583 splitScalarSMulPseudo(Worklist, Inst, MDT);
7584 Inst.eraseFromParent();
7585 return;
7586
7587 case AMDGPU::S_AND_B64:
7588 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT);
7589 Inst.eraseFromParent();
7590 return;
7591
7592 case AMDGPU::S_OR_B64:
7593 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT);
7594 Inst.eraseFromParent();
7595 return;
7596
7597 case AMDGPU::S_XOR_B64:
7598 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT);
7599 Inst.eraseFromParent();
7600 return;
7601
7602 case AMDGPU::S_NAND_B64:
7603 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT);
7604 Inst.eraseFromParent();
7605 return;
7606
7607 case AMDGPU::S_NOR_B64:
7608 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT);
7609 Inst.eraseFromParent();
7610 return;
7611
7612 case AMDGPU::S_XNOR_B64:
7613 if (ST.hasDLInsts())
7614 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT);
7615 else
7616 splitScalar64BitXnor(Worklist, Inst, MDT);
7617 Inst.eraseFromParent();
7618 return;
7619
7620 case AMDGPU::S_ANDN2_B64:
7621 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT);
7622 Inst.eraseFromParent();
7623 return;
7624
7625 case AMDGPU::S_ORN2_B64:
7626 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT);
7627 Inst.eraseFromParent();
7628 return;
7629
7630 case AMDGPU::S_BREV_B64:
7631 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_BREV_B32, true);
7632 Inst.eraseFromParent();
7633 return;
7634
7635 case AMDGPU::S_NOT_B64:
7636 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32);
7637 Inst.eraseFromParent();
7638 return;
7639
7640 case AMDGPU::S_BCNT1_I32_B64:
7641 splitScalar64BitBCNT(Worklist, Inst);
7642 Inst.eraseFromParent();
7643 return;
7644
7645 case AMDGPU::S_BFE_I64:
7646 splitScalar64BitBFE(Worklist, Inst);
7647 Inst.eraseFromParent();
7648 return;
7649
7650 case AMDGPU::S_FLBIT_I32_B64:
7651 splitScalar64BitCountOp(Worklist, Inst, AMDGPU::V_FFBH_U32_e32);
7652 Inst.eraseFromParent();
7653 return;
7654 case AMDGPU::S_FF1_I32_B64:
7655 splitScalar64BitCountOp(Worklist, Inst, AMDGPU::V_FFBL_B32_e32);
7656 Inst.eraseFromParent();
7657 return;
7658
7659 case AMDGPU::S_LSHL_B32:
7660 if (ST.hasOnlyRevVALUShifts()) {
7661 NewOpcode = AMDGPU::V_LSHLREV_B32_e64;
7662 swapOperands(Inst);
7663 }
7664 break;
7665 case AMDGPU::S_ASHR_I32:
7666 if (ST.hasOnlyRevVALUShifts()) {
7667 NewOpcode = AMDGPU::V_ASHRREV_I32_e64;
7668 swapOperands(Inst);
7669 }
7670 break;
7671 case AMDGPU::S_LSHR_B32:
7672 if (ST.hasOnlyRevVALUShifts()) {
7673 NewOpcode = AMDGPU::V_LSHRREV_B32_e64;
7674 swapOperands(Inst);
7675 }
7676 break;
7677 case AMDGPU::S_LSHL_B64:
7678 if (ST.hasOnlyRevVALUShifts()) {
7679 NewOpcode = ST.getGeneration() >= AMDGPUSubtarget::GFX12
7680 ? AMDGPU::V_LSHLREV_B64_pseudo_e64
7681 : AMDGPU::V_LSHLREV_B64_e64;
7682 swapOperands(Inst);
7683 }
7684 break;
7685 case AMDGPU::S_ASHR_I64:
7686 if (ST.hasOnlyRevVALUShifts()) {
7687 NewOpcode = AMDGPU::V_ASHRREV_I64_e64;
7688 swapOperands(Inst);
7689 }
7690 break;
7691 case AMDGPU::S_LSHR_B64:
7692 if (ST.hasOnlyRevVALUShifts()) {
7693 NewOpcode = AMDGPU::V_LSHRREV_B64_e64;
7694 swapOperands(Inst);
7695 }
7696 break;
7697
7698 case AMDGPU::S_ABS_I32:
7699 lowerScalarAbs(Worklist, Inst);
7700 Inst.eraseFromParent();
7701 return;
7702
7703 case AMDGPU::S_CBRANCH_SCC0:
7704 case AMDGPU::S_CBRANCH_SCC1: {
7705 // Clear unused bits of vcc
7706 Register CondReg = Inst.getOperand(1).getReg();
7707 bool IsSCC = CondReg == AMDGPU::SCC;
7709 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(LMC.AndOpc), LMC.VccReg)
7710 .addReg(LMC.ExecReg)
7711 .addReg(IsSCC ? LMC.VccReg : CondReg);
7712 Inst.removeOperand(1);
7713 } break;
7714
7715 case AMDGPU::S_BFE_U64:
7716 case AMDGPU::S_BFM_B64:
7717 llvm_unreachable("Moving this op to VALU not implemented");
7718
7719 case AMDGPU::S_PACK_LL_B32_B16:
7720 case AMDGPU::S_PACK_LH_B32_B16:
7721 case AMDGPU::S_PACK_HL_B32_B16:
7722 case AMDGPU::S_PACK_HH_B32_B16:
7723 movePackToVALU(Worklist, MRI, Inst);
7724 Inst.eraseFromParent();
7725 return;
7726
7727 case AMDGPU::S_XNOR_B32:
7728 lowerScalarXnor(Worklist, Inst);
7729 Inst.eraseFromParent();
7730 return;
7731
7732 case AMDGPU::S_NAND_B32:
7733 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32);
7734 Inst.eraseFromParent();
7735 return;
7736
7737 case AMDGPU::S_NOR_B32:
7738 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32);
7739 Inst.eraseFromParent();
7740 return;
7741
7742 case AMDGPU::S_ANDN2_B32:
7743 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32);
7744 Inst.eraseFromParent();
7745 return;
7746
7747 case AMDGPU::S_ORN2_B32:
7748 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32);
7749 Inst.eraseFromParent();
7750 return;
7751
7752 // TODO: remove as soon as everything is ready
7753 // to replace VGPR to SGPR copy with V_READFIRSTLANEs.
7754 // S_ADD/SUB_CO_PSEUDO as well as S_UADDO/USUBO_PSEUDO
7755 // can only be selected from the uniform SDNode.
7756 case AMDGPU::S_ADD_CO_PSEUDO:
7757 case AMDGPU::S_SUB_CO_PSEUDO: {
7758 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO)
7759 ? AMDGPU::V_ADDC_U32_e64
7760 : AMDGPU::V_SUBB_U32_e64;
7761 const auto *CarryRC = RI.getWaveMaskRegClass();
7762
7763 Register CarryInReg = Inst.getOperand(4).getReg();
7764 if (!MRI.constrainRegClass(CarryInReg, CarryRC)) {
7765 Register NewCarryReg = MRI.createVirtualRegister(CarryRC);
7766 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::COPY), NewCarryReg)
7767 .addReg(CarryInReg);
7768 }
7769
7770 Register CarryOutReg = Inst.getOperand(1).getReg();
7771
7772 Register DestReg = MRI.createVirtualRegister(RI.getEquivalentVGPRClass(
7773 MRI.getRegClass(Inst.getOperand(0).getReg())));
7774 MachineInstr *CarryOp =
7775 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(Opc), DestReg)
7776 .addReg(CarryOutReg, RegState::Define)
7777 .add(Inst.getOperand(2))
7778 .add(Inst.getOperand(3))
7779 .addReg(CarryInReg)
7780 .addImm(0);
7781 legalizeOperands(*CarryOp);
7782 MRI.replaceRegWith(Inst.getOperand(0).getReg(), DestReg);
7783 addUsersToMoveToVALUWorklist(DestReg, MRI, Worklist);
7784 Inst.eraseFromParent();
7785 }
7786 return;
7787 case AMDGPU::S_UADDO_PSEUDO:
7788 case AMDGPU::S_USUBO_PSEUDO: {
7789 const DebugLoc &DL = Inst.getDebugLoc();
7790 MachineOperand &Dest0 = Inst.getOperand(0);
7791 MachineOperand &Dest1 = Inst.getOperand(1);
7792 MachineOperand &Src0 = Inst.getOperand(2);
7793 MachineOperand &Src1 = Inst.getOperand(3);
7794
7795 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_UADDO_PSEUDO)
7796 ? AMDGPU::V_ADD_CO_U32_e64
7797 : AMDGPU::V_SUB_CO_U32_e64;
7798 const TargetRegisterClass *NewRC =
7799 RI.getEquivalentVGPRClass(MRI.getRegClass(Dest0.getReg()));
7800 Register DestReg = MRI.createVirtualRegister(NewRC);
7801 MachineInstr *NewInstr = BuildMI(*MBB, &Inst, DL, get(Opc), DestReg)
7802 .addReg(Dest1.getReg(), RegState::Define)
7803 .add(Src0)
7804 .add(Src1)
7805 .addImm(0); // clamp bit
7806
7807 legalizeOperands(*NewInstr, MDT);
7808 MRI.replaceRegWith(Dest0.getReg(), DestReg);
7809 addUsersToMoveToVALUWorklist(NewInstr->getOperand(0).getReg(), MRI,
7810 Worklist);
7811 Inst.eraseFromParent();
7812 }
7813 return;
7814
7815 case AMDGPU::S_CSELECT_B32:
7816 case AMDGPU::S_CSELECT_B64:
7817 lowerSelect(Worklist, Inst, MDT);
7818 Inst.eraseFromParent();
7819 return;
7820 case AMDGPU::S_CMP_EQ_I32:
7821 case AMDGPU::S_CMP_LG_I32:
7822 case AMDGPU::S_CMP_GT_I32:
7823 case AMDGPU::S_CMP_GE_I32:
7824 case AMDGPU::S_CMP_LT_I32:
7825 case AMDGPU::S_CMP_LE_I32:
7826 case AMDGPU::S_CMP_EQ_U32:
7827 case AMDGPU::S_CMP_LG_U32:
7828 case AMDGPU::S_CMP_GT_U32:
7829 case AMDGPU::S_CMP_GE_U32:
7830 case AMDGPU::S_CMP_LT_U32:
7831 case AMDGPU::S_CMP_LE_U32:
7832 case AMDGPU::S_CMP_EQ_U64:
7833 case AMDGPU::S_CMP_LG_U64:
7834 case AMDGPU::S_CMP_LT_F32:
7835 case AMDGPU::S_CMP_EQ_F32:
7836 case AMDGPU::S_CMP_LE_F32:
7837 case AMDGPU::S_CMP_GT_F32:
7838 case AMDGPU::S_CMP_LG_F32:
7839 case AMDGPU::S_CMP_GE_F32:
7840 case AMDGPU::S_CMP_O_F32:
7841 case AMDGPU::S_CMP_U_F32:
7842 case AMDGPU::S_CMP_NGE_F32:
7843 case AMDGPU::S_CMP_NLG_F32:
7844 case AMDGPU::S_CMP_NGT_F32:
7845 case AMDGPU::S_CMP_NLE_F32:
7846 case AMDGPU::S_CMP_NEQ_F32:
7847 case AMDGPU::S_CMP_NLT_F32: {
7848 Register CondReg = MRI.createVirtualRegister(RI.getWaveMaskRegClass());
7849 auto NewInstr =
7850 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(NewOpcode), CondReg)
7851 .setMIFlags(Inst.getFlags());
7852 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::src0_modifiers) >=
7853 0) {
7854 NewInstr
7855 .addImm(0) // src0_modifiers
7856 .add(Inst.getOperand(0)) // src0
7857 .addImm(0) // src1_modifiers
7858 .add(Inst.getOperand(1)) // src1
7859 .addImm(0); // clamp
7860 } else {
7861 NewInstr.add(Inst.getOperand(0)).add(Inst.getOperand(1));
7862 }
7863 legalizeOperands(*NewInstr, MDT);
7864 int SCCIdx = Inst.findRegisterDefOperandIdx(AMDGPU::SCC, /*TRI=*/nullptr);
7865 MachineOperand SCCOp = Inst.getOperand(SCCIdx);
7866 addSCCDefUsersToVALUWorklist(SCCOp, Inst, Worklist, CondReg);
7867 Inst.eraseFromParent();
7868 return;
7869 }
7870 case AMDGPU::S_CMP_LT_F16:
7871 case AMDGPU::S_CMP_EQ_F16:
7872 case AMDGPU::S_CMP_LE_F16:
7873 case AMDGPU::S_CMP_GT_F16:
7874 case AMDGPU::S_CMP_LG_F16:
7875 case AMDGPU::S_CMP_GE_F16:
7876 case AMDGPU::S_CMP_O_F16:
7877 case AMDGPU::S_CMP_U_F16:
7878 case AMDGPU::S_CMP_NGE_F16:
7879 case AMDGPU::S_CMP_NLG_F16:
7880 case AMDGPU::S_CMP_NGT_F16:
7881 case AMDGPU::S_CMP_NLE_F16:
7882 case AMDGPU::S_CMP_NEQ_F16:
7883 case AMDGPU::S_CMP_NLT_F16: {
7884 Register CondReg = MRI.createVirtualRegister(RI.getWaveMaskRegClass());
7885 auto NewInstr =
7886 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(NewOpcode), CondReg)
7887 .setMIFlags(Inst.getFlags());
7888 if (AMDGPU::hasNamedOperand(NewOpcode, AMDGPU::OpName::src0_modifiers)) {
7889 NewInstr
7890 .addImm(0) // src0_modifiers
7891 .add(Inst.getOperand(0)) // src0
7892 .addImm(0) // src1_modifiers
7893 .add(Inst.getOperand(1)) // src1
7894 .addImm(0); // clamp
7895 if (AMDGPU::hasNamedOperand(NewOpcode, AMDGPU::OpName::op_sel))
7896 NewInstr.addImm(0); // op_sel0
7897 } else {
7898 NewInstr
7899 .add(Inst.getOperand(0))
7900 .add(Inst.getOperand(1));
7901 }
7902 legalizeOperandsVALUt16(*NewInstr, MRI);
7903 legalizeOperands(*NewInstr, MDT);
7904 int SCCIdx = Inst.findRegisterDefOperandIdx(AMDGPU::SCC, /*TRI=*/nullptr);
7905 MachineOperand SCCOp = Inst.getOperand(SCCIdx);
7906 addSCCDefUsersToVALUWorklist(SCCOp, Inst, Worklist, CondReg);
7907 Inst.eraseFromParent();
7908 return;
7909 }
7910 case AMDGPU::S_CVT_HI_F32_F16: {
7911 const DebugLoc &DL = Inst.getDebugLoc();
7912 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7913 Register NewDst = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7914 if (ST.useRealTrue16Insts()) {
7915 BuildMI(*MBB, Inst, DL, get(AMDGPU::COPY), TmpReg)
7916 .add(Inst.getOperand(1));
7917 BuildMI(*MBB, Inst, DL, get(NewOpcode), NewDst)
7918 .addImm(0) // src0_modifiers
7919 .addReg(TmpReg, 0, AMDGPU::hi16)
7920 .addImm(0) // clamp
7921 .addImm(0) // omod
7922 .addImm(0); // op_sel0
7923 } else {
7924 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg)
7925 .addImm(16)
7926 .add(Inst.getOperand(1));
7927 BuildMI(*MBB, Inst, DL, get(NewOpcode), NewDst)
7928 .addImm(0) // src0_modifiers
7929 .addReg(TmpReg)
7930 .addImm(0) // clamp
7931 .addImm(0); // omod
7932 }
7933
7934 MRI.replaceRegWith(Inst.getOperand(0).getReg(), NewDst);
7935 addUsersToMoveToVALUWorklist(NewDst, MRI, Worklist);
7936 Inst.eraseFromParent();
7937 return;
7938 }
7939 case AMDGPU::S_MINIMUM_F32:
7940 case AMDGPU::S_MAXIMUM_F32: {
7941 const DebugLoc &DL = Inst.getDebugLoc();
7942 Register NewDst = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7943 MachineInstr *NewInstr = BuildMI(*MBB, Inst, DL, get(NewOpcode), NewDst)
7944 .addImm(0) // src0_modifiers
7945 .add(Inst.getOperand(1))
7946 .addImm(0) // src1_modifiers
7947 .add(Inst.getOperand(2))
7948 .addImm(0) // clamp
7949 .addImm(0); // omod
7950 MRI.replaceRegWith(Inst.getOperand(0).getReg(), NewDst);
7951
7952 legalizeOperands(*NewInstr, MDT);
7953 addUsersToMoveToVALUWorklist(NewDst, MRI, Worklist);
7954 Inst.eraseFromParent();
7955 return;
7956 }
7957 case AMDGPU::S_MINIMUM_F16:
7958 case AMDGPU::S_MAXIMUM_F16: {
7959 const DebugLoc &DL = Inst.getDebugLoc();
7960 Register NewDst = MRI.createVirtualRegister(ST.useRealTrue16Insts()
7961 ? &AMDGPU::VGPR_16RegClass
7962 : &AMDGPU::VGPR_32RegClass);
7963 MachineInstr *NewInstr = BuildMI(*MBB, Inst, DL, get(NewOpcode), NewDst)
7964 .addImm(0) // src0_modifiers
7965 .add(Inst.getOperand(1))
7966 .addImm(0) // src1_modifiers
7967 .add(Inst.getOperand(2))
7968 .addImm(0) // clamp
7969 .addImm(0) // omod
7970 .addImm(0); // opsel0
7971 MRI.replaceRegWith(Inst.getOperand(0).getReg(), NewDst);
7972 legalizeOperandsVALUt16(*NewInstr, MRI);
7973 legalizeOperands(*NewInstr, MDT);
7974 addUsersToMoveToVALUWorklist(NewDst, MRI, Worklist);
7975 Inst.eraseFromParent();
7976 return;
7977 }
7978 case AMDGPU::V_S_EXP_F16_e64:
7979 case AMDGPU::V_S_LOG_F16_e64:
7980 case AMDGPU::V_S_RCP_F16_e64:
7981 case AMDGPU::V_S_RSQ_F16_e64:
7982 case AMDGPU::V_S_SQRT_F16_e64: {
7983 const DebugLoc &DL = Inst.getDebugLoc();
7984 Register NewDst = MRI.createVirtualRegister(ST.useRealTrue16Insts()
7985 ? &AMDGPU::VGPR_16RegClass
7986 : &AMDGPU::VGPR_32RegClass);
7987 auto NewInstr = BuildMI(*MBB, Inst, DL, get(NewOpcode), NewDst)
7988 .add(Inst.getOperand(1)) // src0_modifiers
7989 .add(Inst.getOperand(2))
7990 .add(Inst.getOperand(3)) // clamp
7991 .add(Inst.getOperand(4)) // omod
7992 .setMIFlags(Inst.getFlags());
7993 if (AMDGPU::hasNamedOperand(NewOpcode, AMDGPU::OpName::op_sel))
7994 NewInstr.addImm(0); // opsel0
7995 MRI.replaceRegWith(Inst.getOperand(0).getReg(), NewDst);
7996 legalizeOperandsVALUt16(*NewInstr, MRI);
7997 legalizeOperands(*NewInstr, MDT);
7998 addUsersToMoveToVALUWorklist(NewDst, MRI, Worklist);
7999 Inst.eraseFromParent();
8000 return;
8001 }
8002 }
8003
8004 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
8005 // We cannot move this instruction to the VALU, so we should try to
8006 // legalize its operands instead.
8007 legalizeOperands(Inst, MDT);
8008 return;
8009 }
8010 // Handle converting generic instructions like COPY-to-SGPR into
8011 // COPY-to-VGPR.
8012 if (NewOpcode == Opcode) {
8013 Register DstReg = Inst.getOperand(0).getReg();
8014 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst);
8015
8016 // If it's a copy of a VGPR to a physical SGPR, insert a V_READFIRSTLANE and
8017 // hope for the best.
8018 if (Inst.isCopy() && DstReg.isPhysical() &&
8019 RI.isVGPR(MRI, Inst.getOperand(1).getReg())) {
8020 // TODO: Only works for 32 bit registers.
8021 if (MRI.constrainRegClass(DstReg, &AMDGPU::SReg_32_XM0RegClass)) {
8022 BuildMI(*Inst.getParent(), &Inst, Inst.getDebugLoc(),
8023 get(AMDGPU::V_READFIRSTLANE_B32), DstReg)
8024 .add(Inst.getOperand(1));
8025 } else {
8026 Register NewDst =
8027 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
8028 BuildMI(*Inst.getParent(), &Inst, Inst.getDebugLoc(),
8029 get(AMDGPU::V_READFIRSTLANE_B32), NewDst)
8030 .add(Inst.getOperand(1));
8031 BuildMI(*Inst.getParent(), &Inst, Inst.getDebugLoc(), get(AMDGPU::COPY),
8032 DstReg)
8033 .addReg(NewDst);
8034 }
8035 Inst.eraseFromParent();
8036 return;
8037 }
8038
8039 if (Inst.isCopy() && Inst.getOperand(1).getReg().isVirtual() &&
8040 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) {
8041 // Instead of creating a copy where src and dst are the same register
8042 // class, we just replace all uses of dst with src. These kinds of
8043 // copies interfere with the heuristics MachineSink uses to decide
8044 // whether or not to split a critical edge. Since the pass assumes
8045 // that copies will end up as machine instructions and not be
8046 // eliminated.
8047 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist);
8048 Register NewDstReg = Inst.getOperand(1).getReg();
8049 MRI.replaceRegWith(DstReg, NewDstReg);
8050 MRI.clearKillFlags(NewDstReg);
8051 Inst.getOperand(0).setReg(DstReg);
8052 Inst.eraseFromParent();
8053 // Legalize t16 operand since replaceReg is called after addUsersToVALU
8054 for (MachineOperand &MO :
8055 make_early_inc_range(MRI.use_operands(NewDstReg))) {
8056 legalizeOperandsVALUt16(*MO.getParent(), MRI);
8057 }
8058 return;
8059 }
8060
8061 // If this is a v2s copy between 16bit and 32bit reg,
8062 // replace vgpr copy to reg_sequence/extract_subreg
8063 // This can be remove after we have sgpr16 in place
8064 if (ST.useRealTrue16Insts() && Inst.isCopy() &&
8065 Inst.getOperand(1).getReg().isVirtual() &&
8066 RI.isVGPR(MRI, Inst.getOperand(1).getReg())) {
8067 const TargetRegisterClass *SrcRegRC = getOpRegClass(Inst, 1);
8068 if (RI.getMatchingSuperRegClass(NewDstRC, SrcRegRC, AMDGPU::lo16)) {
8069 Register NewDstReg = MRI.createVirtualRegister(NewDstRC);
8070 Register Undef = MRI.createVirtualRegister(&AMDGPU::VGPR_16RegClass);
8071 BuildMI(*Inst.getParent(), &Inst, Inst.getDebugLoc(),
8072 get(AMDGPU::IMPLICIT_DEF), Undef);
8073 BuildMI(*Inst.getParent(), &Inst, Inst.getDebugLoc(),
8074 get(AMDGPU::REG_SEQUENCE), NewDstReg)
8075 .addReg(Inst.getOperand(1).getReg())
8076 .addImm(AMDGPU::lo16)
8077 .addReg(Undef)
8078 .addImm(AMDGPU::hi16);
8079 Inst.eraseFromParent();
8080 MRI.replaceRegWith(DstReg, NewDstReg);
8081 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
8082 return;
8083 } else if (RI.getMatchingSuperRegClass(SrcRegRC, NewDstRC,
8084 AMDGPU::lo16)) {
8085 Inst.getOperand(1).setSubReg(AMDGPU::lo16);
8086 Register NewDstReg = MRI.createVirtualRegister(NewDstRC);
8087 MRI.replaceRegWith(DstReg, NewDstReg);
8088 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
8089 return;
8090 }
8091 }
8092
8093 Register NewDstReg = MRI.createVirtualRegister(NewDstRC);
8094 MRI.replaceRegWith(DstReg, NewDstReg);
8095 legalizeOperands(Inst, MDT);
8096 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
8097 return;
8098 }
8099
8100 // Use the new VALU Opcode.
8101 auto NewInstr = BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(NewOpcode))
8102 .setMIFlags(Inst.getFlags());
8103 if (isVOP3(NewOpcode) && !isVOP3(Opcode)) {
8104 // Intersperse VOP3 modifiers among the SALU operands.
8105 NewInstr->addOperand(Inst.getOperand(0));
8106 if (AMDGPU::getNamedOperandIdx(NewOpcode,
8107 AMDGPU::OpName::src0_modifiers) >= 0)
8108 NewInstr.addImm(0);
8109 if (AMDGPU::hasNamedOperand(NewOpcode, AMDGPU::OpName::src0)) {
8110 MachineOperand Src = Inst.getOperand(1);
8111 NewInstr->addOperand(Src);
8112 }
8113
8114 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) {
8115 // We are converting these to a BFE, so we need to add the missing
8116 // operands for the size and offset.
8117 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16;
8118 NewInstr.addImm(0);
8119 NewInstr.addImm(Size);
8120 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) {
8121 // The VALU version adds the second operand to the result, so insert an
8122 // extra 0 operand.
8123 NewInstr.addImm(0);
8124 } else if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) {
8125 const MachineOperand &OffsetWidthOp = Inst.getOperand(2);
8126 // If we need to move this to VGPRs, we need to unpack the second
8127 // operand back into the 2 separate ones for bit offset and width.
8128 assert(OffsetWidthOp.isImm() &&
8129 "Scalar BFE is only implemented for constant width and offset");
8130 uint32_t Imm = OffsetWidthOp.getImm();
8131
8132 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
8133 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
8134 NewInstr.addImm(Offset);
8135 NewInstr.addImm(BitWidth);
8136 } else {
8137 if (AMDGPU::getNamedOperandIdx(NewOpcode,
8138 AMDGPU::OpName::src1_modifiers) >= 0)
8139 NewInstr.addImm(0);
8140 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::src1) >= 0)
8141 NewInstr->addOperand(Inst.getOperand(2));
8142 if (AMDGPU::getNamedOperandIdx(NewOpcode,
8143 AMDGPU::OpName::src2_modifiers) >= 0)
8144 NewInstr.addImm(0);
8145 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::src2) >= 0)
8146 NewInstr->addOperand(Inst.getOperand(3));
8147 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::clamp) >= 0)
8148 NewInstr.addImm(0);
8149 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::omod) >= 0)
8150 NewInstr.addImm(0);
8151 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::op_sel) >= 0)
8152 NewInstr.addImm(0);
8153 }
8154 } else {
8155 // Just copy the SALU operands.
8156 for (const MachineOperand &Op : Inst.explicit_operands())
8157 NewInstr->addOperand(Op);
8158 }
8159
8160 // Remove any references to SCC. Vector instructions can't read from it, and
8161 // We're just about to add the implicit use / defs of VCC, and we don't want
8162 // both.
8163 for (MachineOperand &Op : Inst.implicit_operands()) {
8164 if (Op.getReg() == AMDGPU::SCC) {
8165 // Only propagate through live-def of SCC.
8166 if (Op.isDef() && !Op.isDead())
8167 addSCCDefUsersToVALUWorklist(Op, Inst, Worklist);
8168 if (Op.isUse())
8169 addSCCDefsToVALUWorklist(NewInstr, Worklist);
8170 }
8171 }
8172 Inst.eraseFromParent();
8173 Register NewDstReg;
8174 if (NewInstr->getOperand(0).isReg() && NewInstr->getOperand(0).isDef()) {
8175 Register DstReg = NewInstr->getOperand(0).getReg();
8176 assert(DstReg.isVirtual());
8177 // Update the destination register class.
8178 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(*NewInstr);
8179 assert(NewDstRC);
8180 NewDstReg = MRI.createVirtualRegister(NewDstRC);
8181 MRI.replaceRegWith(DstReg, NewDstReg);
8182 }
8183 fixImplicitOperands(*NewInstr);
8184
8185 legalizeOperandsVALUt16(*NewInstr, MRI);
8186
8187 // Legalize the operands
8188 legalizeOperands(*NewInstr, MDT);
8189 if (NewDstReg)
8190 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
8191}
8192
8193// Add/sub require special handling to deal with carry outs.
8194std::pair<bool, MachineBasicBlock *>
8195SIInstrInfo::moveScalarAddSub(SIInstrWorklist &Worklist, MachineInstr &Inst,
8196 MachineDominatorTree *MDT) const {
8197 if (ST.hasAddNoCarry()) {
8198 // Assume there is no user of scc since we don't select this in that case.
8199 // Since scc isn't used, it doesn't really matter if the i32 or u32 variant
8200 // is used.
8201
8202 MachineBasicBlock &MBB = *Inst.getParent();
8203 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8204
8205 Register OldDstReg = Inst.getOperand(0).getReg();
8206 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8207
8208 unsigned Opc = Inst.getOpcode();
8209 assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32);
8210
8211 unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ?
8212 AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64;
8213
8214 assert(Inst.getOperand(3).getReg() == AMDGPU::SCC);
8215 Inst.removeOperand(3);
8216
8217 Inst.setDesc(get(NewOpc));
8218 Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit
8219 Inst.addImplicitDefUseOperands(*MBB.getParent());
8220 MRI.replaceRegWith(OldDstReg, ResultReg);
8221 MachineBasicBlock *NewBB = legalizeOperands(Inst, MDT);
8222
8223 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
8224 return std::pair(true, NewBB);
8225 }
8226
8227 return std::pair(false, nullptr);
8228}
8229
8230void SIInstrInfo::lowerSelect(SIInstrWorklist &Worklist, MachineInstr &Inst,
8231 MachineDominatorTree *MDT) const {
8232
8233 MachineBasicBlock &MBB = *Inst.getParent();
8234 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8235 MachineBasicBlock::iterator MII = Inst;
8236 DebugLoc DL = Inst.getDebugLoc();
8237
8238 MachineOperand &Dest = Inst.getOperand(0);
8239 MachineOperand &Src0 = Inst.getOperand(1);
8240 MachineOperand &Src1 = Inst.getOperand(2);
8241 MachineOperand &Cond = Inst.getOperand(3);
8242
8243 Register CondReg = Cond.getReg();
8244 bool IsSCC = (CondReg == AMDGPU::SCC);
8245
8246 // If this is a trivial select where the condition is effectively not SCC
8247 // (CondReg is a source of copy to SCC), then the select is semantically
8248 // equivalent to copying CondReg. Hence, there is no need to create
8249 // V_CNDMASK, we can just use that and bail out.
8250 if (!IsSCC && Src0.isImm() && (Src0.getImm() == -1) && Src1.isImm() &&
8251 (Src1.getImm() == 0)) {
8252 MRI.replaceRegWith(Dest.getReg(), CondReg);
8253 return;
8254 }
8255
8256 Register NewCondReg = CondReg;
8257 if (IsSCC) {
8258 const TargetRegisterClass *TC = RI.getWaveMaskRegClass();
8259 NewCondReg = MRI.createVirtualRegister(TC);
8260
8261 // Now look for the closest SCC def if it is a copy
8262 // replacing the CondReg with the COPY source register
8263 bool CopyFound = false;
8264 for (MachineInstr &CandI :
8266 Inst.getParent()->rend())) {
8267 if (CandI.findRegisterDefOperandIdx(AMDGPU::SCC, &RI, false, false) !=
8268 -1) {
8269 if (CandI.isCopy() && CandI.getOperand(0).getReg() == AMDGPU::SCC) {
8270 BuildMI(MBB, MII, DL, get(AMDGPU::COPY), NewCondReg)
8271 .addReg(CandI.getOperand(1).getReg());
8272 CopyFound = true;
8273 }
8274 break;
8275 }
8276 }
8277 if (!CopyFound) {
8278 // SCC def is not a copy
8279 // Insert a trivial select instead of creating a copy, because a copy from
8280 // SCC would semantically mean just copying a single bit, but we may need
8281 // the result to be a vector condition mask that needs preserving.
8282 unsigned Opcode =
8283 ST.isWave64() ? AMDGPU::S_CSELECT_B64 : AMDGPU::S_CSELECT_B32;
8284 auto NewSelect =
8285 BuildMI(MBB, MII, DL, get(Opcode), NewCondReg).addImm(-1).addImm(0);
8286 NewSelect->getOperand(3).setIsUndef(Cond.isUndef());
8287 }
8288 }
8289
8290 Register NewDestReg = MRI.createVirtualRegister(
8291 RI.getEquivalentVGPRClass(MRI.getRegClass(Dest.getReg())));
8292 MachineInstr *NewInst;
8293 if (Inst.getOpcode() == AMDGPU::S_CSELECT_B32) {
8294 NewInst = BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B32_e64), NewDestReg)
8295 .addImm(0)
8296 .add(Src1) // False
8297 .addImm(0)
8298 .add(Src0) // True
8299 .addReg(NewCondReg);
8300 } else {
8301 NewInst =
8302 BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B64_PSEUDO), NewDestReg)
8303 .add(Src1) // False
8304 .add(Src0) // True
8305 .addReg(NewCondReg);
8306 }
8307 MRI.replaceRegWith(Dest.getReg(), NewDestReg);
8308 legalizeOperands(*NewInst, MDT);
8309 addUsersToMoveToVALUWorklist(NewDestReg, MRI, Worklist);
8310}
8311
8312void SIInstrInfo::lowerScalarAbs(SIInstrWorklist &Worklist,
8313 MachineInstr &Inst) const {
8314 MachineBasicBlock &MBB = *Inst.getParent();
8315 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8316 MachineBasicBlock::iterator MII = Inst;
8317 DebugLoc DL = Inst.getDebugLoc();
8318
8319 MachineOperand &Dest = Inst.getOperand(0);
8320 MachineOperand &Src = Inst.getOperand(1);
8321 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8322 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8323
8324 unsigned SubOp = ST.hasAddNoCarry() ?
8325 AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_CO_U32_e32;
8326
8327 BuildMI(MBB, MII, DL, get(SubOp), TmpReg)
8328 .addImm(0)
8329 .addReg(Src.getReg());
8330
8331 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg)
8332 .addReg(Src.getReg())
8333 .addReg(TmpReg);
8334
8335 MRI.replaceRegWith(Dest.getReg(), ResultReg);
8336 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
8337}
8338
8339void SIInstrInfo::lowerScalarXnor(SIInstrWorklist &Worklist,
8340 MachineInstr &Inst) const {
8341 MachineBasicBlock &MBB = *Inst.getParent();
8342 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8343 MachineBasicBlock::iterator MII = Inst;
8344 const DebugLoc &DL = Inst.getDebugLoc();
8345
8346 MachineOperand &Dest = Inst.getOperand(0);
8347 MachineOperand &Src0 = Inst.getOperand(1);
8348 MachineOperand &Src1 = Inst.getOperand(2);
8349
8350 if (ST.hasDLInsts()) {
8351 Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8352 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL);
8353 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL);
8354
8355 BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest)
8356 .add(Src0)
8357 .add(Src1);
8358
8359 MRI.replaceRegWith(Dest.getReg(), NewDest);
8360 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
8361 } else {
8362 // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can
8363 // invert either source and then perform the XOR. If either source is a
8364 // scalar register, then we can leave the inversion on the scalar unit to
8365 // achieve a better distribution of scalar and vector instructions.
8366 bool Src0IsSGPR = Src0.isReg() &&
8367 RI.isSGPRClass(MRI.getRegClass(Src0.getReg()));
8368 bool Src1IsSGPR = Src1.isReg() &&
8369 RI.isSGPRClass(MRI.getRegClass(Src1.getReg()));
8370 MachineInstr *Xor;
8371 Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
8372 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
8373
8374 // Build a pair of scalar instructions and add them to the work list.
8375 // The next iteration over the work list will lower these to the vector
8376 // unit as necessary.
8377 if (Src0IsSGPR) {
8378 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0);
8379 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
8380 .addReg(Temp)
8381 .add(Src1);
8382 } else if (Src1IsSGPR) {
8383 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1);
8384 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
8385 .add(Src0)
8386 .addReg(Temp);
8387 } else {
8388 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp)
8389 .add(Src0)
8390 .add(Src1);
8391 MachineInstr *Not =
8392 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp);
8393 Worklist.insert(Not);
8394 }
8395
8396 MRI.replaceRegWith(Dest.getReg(), NewDest);
8397
8398 Worklist.insert(Xor);
8399
8400 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
8401 }
8402}
8403
8404void SIInstrInfo::splitScalarNotBinop(SIInstrWorklist &Worklist,
8405 MachineInstr &Inst,
8406 unsigned Opcode) const {
8407 MachineBasicBlock &MBB = *Inst.getParent();
8408 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8409 MachineBasicBlock::iterator MII = Inst;
8410 const DebugLoc &DL = Inst.getDebugLoc();
8411
8412 MachineOperand &Dest = Inst.getOperand(0);
8413 MachineOperand &Src0 = Inst.getOperand(1);
8414 MachineOperand &Src1 = Inst.getOperand(2);
8415
8416 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
8417 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
8418
8419 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm)
8420 .add(Src0)
8421 .add(Src1);
8422
8423 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest)
8424 .addReg(Interm);
8425
8426 Worklist.insert(&Op);
8427 Worklist.insert(&Not);
8428
8429 MRI.replaceRegWith(Dest.getReg(), NewDest);
8430 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
8431}
8432
8433void SIInstrInfo::splitScalarBinOpN2(SIInstrWorklist &Worklist,
8434 MachineInstr &Inst,
8435 unsigned Opcode) const {
8436 MachineBasicBlock &MBB = *Inst.getParent();
8437 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8438 MachineBasicBlock::iterator MII = Inst;
8439 const DebugLoc &DL = Inst.getDebugLoc();
8440
8441 MachineOperand &Dest = Inst.getOperand(0);
8442 MachineOperand &Src0 = Inst.getOperand(1);
8443 MachineOperand &Src1 = Inst.getOperand(2);
8444
8445 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
8446 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
8447
8448 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm)
8449 .add(Src1);
8450
8451 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest)
8452 .add(Src0)
8453 .addReg(Interm);
8454
8455 Worklist.insert(&Not);
8456 Worklist.insert(&Op);
8457
8458 MRI.replaceRegWith(Dest.getReg(), NewDest);
8459 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
8460}
8461
8462void SIInstrInfo::splitScalar64BitUnaryOp(SIInstrWorklist &Worklist,
8463 MachineInstr &Inst, unsigned Opcode,
8464 bool Swap) const {
8465 MachineBasicBlock &MBB = *Inst.getParent();
8466 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8467
8468 MachineOperand &Dest = Inst.getOperand(0);
8469 MachineOperand &Src0 = Inst.getOperand(1);
8470 DebugLoc DL = Inst.getDebugLoc();
8471
8472 MachineBasicBlock::iterator MII = Inst;
8473
8474 const MCInstrDesc &InstDesc = get(Opcode);
8475 const TargetRegisterClass *Src0RC = Src0.isReg() ?
8476 MRI.getRegClass(Src0.getReg()) :
8477 &AMDGPU::SGPR_32RegClass;
8478
8479 const TargetRegisterClass *Src0SubRC =
8480 RI.getSubRegisterClass(Src0RC, AMDGPU::sub0);
8481
8482 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
8483 AMDGPU::sub0, Src0SubRC);
8484
8485 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
8486 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
8487 const TargetRegisterClass *NewDestSubRC =
8488 RI.getSubRegisterClass(NewDestRC, AMDGPU::sub0);
8489
8490 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
8491 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0);
8492
8493 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
8494 AMDGPU::sub1, Src0SubRC);
8495
8496 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
8497 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1);
8498
8499 if (Swap)
8500 std::swap(DestSub0, DestSub1);
8501
8502 Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
8503 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
8504 .addReg(DestSub0)
8505 .addImm(AMDGPU::sub0)
8506 .addReg(DestSub1)
8507 .addImm(AMDGPU::sub1);
8508
8509 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
8510
8511 Worklist.insert(&LoHalf);
8512 Worklist.insert(&HiHalf);
8513
8514 // We don't need to legalizeOperands here because for a single operand, src0
8515 // will support any kind of input.
8516
8517 // Move all users of this moved value.
8518 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
8519}
8520
8521// There is not a vector equivalent of s_mul_u64. For this reason, we need to
8522// split the s_mul_u64 in 32-bit vector multiplications.
8523void SIInstrInfo::splitScalarSMulU64(SIInstrWorklist &Worklist,
8524 MachineInstr &Inst,
8525 MachineDominatorTree *MDT) const {
8526 MachineBasicBlock &MBB = *Inst.getParent();
8527 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8528
8529 Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
8530 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8531 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8532
8533 MachineOperand &Dest = Inst.getOperand(0);
8534 MachineOperand &Src0 = Inst.getOperand(1);
8535 MachineOperand &Src1 = Inst.getOperand(2);
8536 const DebugLoc &DL = Inst.getDebugLoc();
8537 MachineBasicBlock::iterator MII = Inst;
8538
8539 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg());
8540 const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg());
8541 const TargetRegisterClass *Src0SubRC =
8542 RI.getSubRegisterClass(Src0RC, AMDGPU::sub0);
8543 if (RI.isSGPRClass(Src0SubRC))
8544 Src0SubRC = RI.getEquivalentVGPRClass(Src0SubRC);
8545 const TargetRegisterClass *Src1SubRC =
8546 RI.getSubRegisterClass(Src1RC, AMDGPU::sub0);
8547 if (RI.isSGPRClass(Src1SubRC))
8548 Src1SubRC = RI.getEquivalentVGPRClass(Src1SubRC);
8549
8550 // First, we extract the low 32-bit and high 32-bit values from each of the
8551 // operands.
8552 MachineOperand Op0L =
8553 buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, AMDGPU::sub0, Src0SubRC);
8554 MachineOperand Op1L =
8555 buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, AMDGPU::sub0, Src1SubRC);
8556 MachineOperand Op0H =
8557 buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, AMDGPU::sub1, Src0SubRC);
8558 MachineOperand Op1H =
8559 buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, AMDGPU::sub1, Src1SubRC);
8560
8561 // The multilication is done as follows:
8562 //
8563 // Op1H Op1L
8564 // * Op0H Op0L
8565 // --------------------
8566 // Op1H*Op0L Op1L*Op0L
8567 // + Op1H*Op0H Op1L*Op0H
8568 // -----------------------------------------
8569 // (Op1H*Op0L + Op1L*Op0H + carry) Op1L*Op0L
8570 //
8571 // We drop Op1H*Op0H because the result of the multiplication is a 64-bit
8572 // value and that would overflow.
8573 // The low 32-bit value is Op1L*Op0L.
8574 // The high 32-bit value is Op1H*Op0L + Op1L*Op0H + carry (from Op1L*Op0L).
8575
8576 Register Op1L_Op0H_Reg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8577 MachineInstr *Op1L_Op0H =
8578 BuildMI(MBB, MII, DL, get(AMDGPU::V_MUL_LO_U32_e64), Op1L_Op0H_Reg)
8579 .add(Op1L)
8580 .add(Op0H);
8581
8582 Register Op1H_Op0L_Reg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8583 MachineInstr *Op1H_Op0L =
8584 BuildMI(MBB, MII, DL, get(AMDGPU::V_MUL_LO_U32_e64), Op1H_Op0L_Reg)
8585 .add(Op1H)
8586 .add(Op0L);
8587
8588 Register CarryReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8589 MachineInstr *Carry =
8590 BuildMI(MBB, MII, DL, get(AMDGPU::V_MUL_HI_U32_e64), CarryReg)
8591 .add(Op1L)
8592 .add(Op0L);
8593
8594 MachineInstr *LoHalf =
8595 BuildMI(MBB, MII, DL, get(AMDGPU::V_MUL_LO_U32_e64), DestSub0)
8596 .add(Op1L)
8597 .add(Op0L);
8598
8599 Register AddReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8600 MachineInstr *Add = BuildMI(MBB, MII, DL, get(AMDGPU::V_ADD_U32_e32), AddReg)
8601 .addReg(Op1L_Op0H_Reg)
8602 .addReg(Op1H_Op0L_Reg);
8603
8604 MachineInstr *HiHalf =
8605 BuildMI(MBB, MII, DL, get(AMDGPU::V_ADD_U32_e32), DestSub1)
8606 .addReg(AddReg)
8607 .addReg(CarryReg);
8608
8609 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
8610 .addReg(DestSub0)
8611 .addImm(AMDGPU::sub0)
8612 .addReg(DestSub1)
8613 .addImm(AMDGPU::sub1);
8614
8615 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
8616
8617 // Try to legalize the operands in case we need to swap the order to keep it
8618 // valid.
8619 legalizeOperands(*Op1L_Op0H, MDT);
8620 legalizeOperands(*Op1H_Op0L, MDT);
8621 legalizeOperands(*Carry, MDT);
8622 legalizeOperands(*LoHalf, MDT);
8623 legalizeOperands(*Add, MDT);
8624 legalizeOperands(*HiHalf, MDT);
8625
8626 // Move all users of this moved value.
8627 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
8628}
8629
8630// Lower S_MUL_U64_U32_PSEUDO/S_MUL_I64_I32_PSEUDO in two 32-bit vector
8631// multiplications.
8632void SIInstrInfo::splitScalarSMulPseudo(SIInstrWorklist &Worklist,
8633 MachineInstr &Inst,
8634 MachineDominatorTree *MDT) const {
8635 MachineBasicBlock &MBB = *Inst.getParent();
8636 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8637
8638 Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
8639 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8640 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8641
8642 MachineOperand &Dest = Inst.getOperand(0);
8643 MachineOperand &Src0 = Inst.getOperand(1);
8644 MachineOperand &Src1 = Inst.getOperand(2);
8645 const DebugLoc &DL = Inst.getDebugLoc();
8646 MachineBasicBlock::iterator MII = Inst;
8647
8648 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg());
8649 const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg());
8650 const TargetRegisterClass *Src0SubRC =
8651 RI.getSubRegisterClass(Src0RC, AMDGPU::sub0);
8652 if (RI.isSGPRClass(Src0SubRC))
8653 Src0SubRC = RI.getEquivalentVGPRClass(Src0SubRC);
8654 const TargetRegisterClass *Src1SubRC =
8655 RI.getSubRegisterClass(Src1RC, AMDGPU::sub0);
8656 if (RI.isSGPRClass(Src1SubRC))
8657 Src1SubRC = RI.getEquivalentVGPRClass(Src1SubRC);
8658
8659 // First, we extract the low 32-bit and high 32-bit values from each of the
8660 // operands.
8661 MachineOperand Op0L =
8662 buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, AMDGPU::sub0, Src0SubRC);
8663 MachineOperand Op1L =
8664 buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, AMDGPU::sub0, Src1SubRC);
8665
8666 unsigned Opc = Inst.getOpcode();
8667 unsigned NewOpc = Opc == AMDGPU::S_MUL_U64_U32_PSEUDO
8668 ? AMDGPU::V_MUL_HI_U32_e64
8669 : AMDGPU::V_MUL_HI_I32_e64;
8670 MachineInstr *HiHalf =
8671 BuildMI(MBB, MII, DL, get(NewOpc), DestSub1).add(Op1L).add(Op0L);
8672
8673 MachineInstr *LoHalf =
8674 BuildMI(MBB, MII, DL, get(AMDGPU::V_MUL_LO_U32_e64), DestSub0)
8675 .add(Op1L)
8676 .add(Op0L);
8677
8678 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
8679 .addReg(DestSub0)
8680 .addImm(AMDGPU::sub0)
8681 .addReg(DestSub1)
8682 .addImm(AMDGPU::sub1);
8683
8684 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
8685
8686 // Try to legalize the operands in case we need to swap the order to keep it
8687 // valid.
8688 legalizeOperands(*HiHalf, MDT);
8689 legalizeOperands(*LoHalf, MDT);
8690
8691 // Move all users of this moved value.
8692 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
8693}
8694
8695void SIInstrInfo::splitScalar64BitBinaryOp(SIInstrWorklist &Worklist,
8696 MachineInstr &Inst, unsigned Opcode,
8697 MachineDominatorTree *MDT) const {
8698 MachineBasicBlock &MBB = *Inst.getParent();
8699 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8700
8701 MachineOperand &Dest = Inst.getOperand(0);
8702 MachineOperand &Src0 = Inst.getOperand(1);
8703 MachineOperand &Src1 = Inst.getOperand(2);
8704 DebugLoc DL = Inst.getDebugLoc();
8705
8706 MachineBasicBlock::iterator MII = Inst;
8707
8708 const MCInstrDesc &InstDesc = get(Opcode);
8709 const TargetRegisterClass *Src0RC = Src0.isReg() ?
8710 MRI.getRegClass(Src0.getReg()) :
8711 &AMDGPU::SGPR_32RegClass;
8712
8713 const TargetRegisterClass *Src0SubRC =
8714 RI.getSubRegisterClass(Src0RC, AMDGPU::sub0);
8715 const TargetRegisterClass *Src1RC = Src1.isReg() ?
8716 MRI.getRegClass(Src1.getReg()) :
8717 &AMDGPU::SGPR_32RegClass;
8718
8719 const TargetRegisterClass *Src1SubRC =
8720 RI.getSubRegisterClass(Src1RC, AMDGPU::sub0);
8721
8722 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
8723 AMDGPU::sub0, Src0SubRC);
8724 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
8725 AMDGPU::sub0, Src1SubRC);
8726 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
8727 AMDGPU::sub1, Src0SubRC);
8728 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
8729 AMDGPU::sub1, Src1SubRC);
8730
8731 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
8732 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
8733 const TargetRegisterClass *NewDestSubRC =
8734 RI.getSubRegisterClass(NewDestRC, AMDGPU::sub0);
8735
8736 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
8737 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0)
8738 .add(SrcReg0Sub0)
8739 .add(SrcReg1Sub0);
8740
8741 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
8742 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1)
8743 .add(SrcReg0Sub1)
8744 .add(SrcReg1Sub1);
8745
8746 Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
8747 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
8748 .addReg(DestSub0)
8749 .addImm(AMDGPU::sub0)
8750 .addReg(DestSub1)
8751 .addImm(AMDGPU::sub1);
8752
8753 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
8754
8755 Worklist.insert(&LoHalf);
8756 Worklist.insert(&HiHalf);
8757
8758 // Move all users of this moved value.
8759 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
8760}
8761
8762void SIInstrInfo::splitScalar64BitXnor(SIInstrWorklist &Worklist,
8763 MachineInstr &Inst,
8764 MachineDominatorTree *MDT) const {
8765 MachineBasicBlock &MBB = *Inst.getParent();
8766 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8767
8768 MachineOperand &Dest = Inst.getOperand(0);
8769 MachineOperand &Src0 = Inst.getOperand(1);
8770 MachineOperand &Src1 = Inst.getOperand(2);
8771 const DebugLoc &DL = Inst.getDebugLoc();
8772
8773 MachineBasicBlock::iterator MII = Inst;
8774
8775 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
8776
8777 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
8778
8779 MachineOperand* Op0;
8780 MachineOperand* Op1;
8781
8782 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) {
8783 Op0 = &Src0;
8784 Op1 = &Src1;
8785 } else {
8786 Op0 = &Src1;
8787 Op1 = &Src0;
8788 }
8789
8790 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm)
8791 .add(*Op0);
8792
8793 Register NewDest = MRI.createVirtualRegister(DestRC);
8794
8795 MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest)
8796 .addReg(Interm)
8797 .add(*Op1);
8798
8799 MRI.replaceRegWith(Dest.getReg(), NewDest);
8800
8801 Worklist.insert(&Xor);
8802}
8803
8804void SIInstrInfo::splitScalar64BitBCNT(SIInstrWorklist &Worklist,
8805 MachineInstr &Inst) const {
8806 MachineBasicBlock &MBB = *Inst.getParent();
8807 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8808
8809 MachineBasicBlock::iterator MII = Inst;
8810 const DebugLoc &DL = Inst.getDebugLoc();
8811
8812 MachineOperand &Dest = Inst.getOperand(0);
8813 MachineOperand &Src = Inst.getOperand(1);
8814
8815 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64);
8816 const TargetRegisterClass *SrcRC = Src.isReg() ?
8817 MRI.getRegClass(Src.getReg()) :
8818 &AMDGPU::SGPR_32RegClass;
8819
8820 Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8821 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8822
8823 const TargetRegisterClass *SrcSubRC =
8824 RI.getSubRegisterClass(SrcRC, AMDGPU::sub0);
8825
8826 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
8827 AMDGPU::sub0, SrcSubRC);
8828 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
8829 AMDGPU::sub1, SrcSubRC);
8830
8831 BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0);
8832
8833 BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg);
8834
8835 MRI.replaceRegWith(Dest.getReg(), ResultReg);
8836
8837 // We don't need to legalize operands here. src0 for either instruction can be
8838 // an SGPR, and the second input is unused or determined here.
8839 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
8840}
8841
8842void SIInstrInfo::splitScalar64BitBFE(SIInstrWorklist &Worklist,
8843 MachineInstr &Inst) const {
8844 MachineBasicBlock &MBB = *Inst.getParent();
8845 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8846 MachineBasicBlock::iterator MII = Inst;
8847 const DebugLoc &DL = Inst.getDebugLoc();
8848
8849 MachineOperand &Dest = Inst.getOperand(0);
8850 uint32_t Imm = Inst.getOperand(2).getImm();
8851 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
8852 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
8853
8854 (void) Offset;
8855
8856 // Only sext_inreg cases handled.
8857 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 &&
8858 Offset == 0 && "Not implemented");
8859
8860 if (BitWidth < 32) {
8861 Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8862 Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8863 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
8864
8865 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32_e64), MidRegLo)
8866 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0)
8867 .addImm(0)
8868 .addImm(BitWidth);
8869
8870 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi)
8871 .addImm(31)
8872 .addReg(MidRegLo);
8873
8874 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
8875 .addReg(MidRegLo)
8876 .addImm(AMDGPU::sub0)
8877 .addReg(MidRegHi)
8878 .addImm(AMDGPU::sub1);
8879
8880 MRI.replaceRegWith(Dest.getReg(), ResultReg);
8881 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
8882 return;
8883 }
8884
8885 MachineOperand &Src = Inst.getOperand(1);
8886 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8887 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
8888
8889 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg)
8890 .addImm(31)
8891 .addReg(Src.getReg(), 0, AMDGPU::sub0);
8892
8893 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
8894 .addReg(Src.getReg(), 0, AMDGPU::sub0)
8895 .addImm(AMDGPU::sub0)
8896 .addReg(TmpReg)
8897 .addImm(AMDGPU::sub1);
8898
8899 MRI.replaceRegWith(Dest.getReg(), ResultReg);
8900 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
8901}
8902
8903void SIInstrInfo::splitScalar64BitCountOp(SIInstrWorklist &Worklist,
8904 MachineInstr &Inst, unsigned Opcode,
8905 MachineDominatorTree *MDT) const {
8906 // (S_FLBIT_I32_B64 hi:lo) ->
8907 // -> (umin (V_FFBH_U32_e32 hi), (uaddsat (V_FFBH_U32_e32 lo), 32))
8908 // (S_FF1_I32_B64 hi:lo) ->
8909 // ->(umin (uaddsat (V_FFBL_B32_e32 hi), 32) (V_FFBL_B32_e32 lo))
8910
8911 MachineBasicBlock &MBB = *Inst.getParent();
8912 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
8913 MachineBasicBlock::iterator MII = Inst;
8914 const DebugLoc &DL = Inst.getDebugLoc();
8915
8916 MachineOperand &Dest = Inst.getOperand(0);
8917 MachineOperand &Src = Inst.getOperand(1);
8918
8919 const MCInstrDesc &InstDesc = get(Opcode);
8920
8921 bool IsCtlz = Opcode == AMDGPU::V_FFBH_U32_e32;
8922 unsigned OpcodeAdd =
8923 ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32;
8924
8925 const TargetRegisterClass *SrcRC =
8926 Src.isReg() ? MRI.getRegClass(Src.getReg()) : &AMDGPU::SGPR_32RegClass;
8927 const TargetRegisterClass *SrcSubRC =
8928 RI.getSubRegisterClass(SrcRC, AMDGPU::sub0);
8929
8930 MachineOperand SrcRegSub0 =
8931 buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, AMDGPU::sub0, SrcSubRC);
8932 MachineOperand SrcRegSub1 =
8933 buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, AMDGPU::sub1, SrcSubRC);
8934
8935 Register MidReg1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8936 Register MidReg2 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8937 Register MidReg3 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8938 Register MidReg4 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8939
8940 BuildMI(MBB, MII, DL, InstDesc, MidReg1).add(SrcRegSub0);
8941
8942 BuildMI(MBB, MII, DL, InstDesc, MidReg2).add(SrcRegSub1);
8943
8944 BuildMI(MBB, MII, DL, get(OpcodeAdd), MidReg3)
8945 .addReg(IsCtlz ? MidReg1 : MidReg2)
8946 .addImm(32)
8947 .addImm(1); // enable clamp
8948
8949 BuildMI(MBB, MII, DL, get(AMDGPU::V_MIN_U32_e64), MidReg4)
8950 .addReg(MidReg3)
8951 .addReg(IsCtlz ? MidReg2 : MidReg1);
8952
8953 MRI.replaceRegWith(Dest.getReg(), MidReg4);
8954
8955 addUsersToMoveToVALUWorklist(MidReg4, MRI, Worklist);
8956}
8957
8958void SIInstrInfo::addUsersToMoveToVALUWorklist(
8960 SIInstrWorklist &Worklist) const {
8961 for (MachineOperand &MO : make_early_inc_range(MRI.use_operands(DstReg))) {
8962 MachineInstr &UseMI = *MO.getParent();
8963
8964 unsigned OpNo = 0;
8965
8966 switch (UseMI.getOpcode()) {
8967 case AMDGPU::COPY:
8968 case AMDGPU::WQM:
8969 case AMDGPU::SOFT_WQM:
8970 case AMDGPU::STRICT_WWM:
8971 case AMDGPU::STRICT_WQM:
8972 case AMDGPU::REG_SEQUENCE:
8973 case AMDGPU::PHI:
8974 case AMDGPU::INSERT_SUBREG:
8975 break;
8976 default:
8977 OpNo = MO.getOperandNo();
8978 break;
8979 }
8980
8981 const TargetRegisterClass *OpRC = getOpRegClass(UseMI, OpNo);
8982 MRI.constrainRegClass(DstReg, OpRC);
8983
8984 if (!RI.hasVectorRegisters(OpRC))
8985 Worklist.insert(&UseMI);
8986 else
8987 // Legalization could change user list.
8989 }
8990}
8991
8992void SIInstrInfo::movePackToVALU(SIInstrWorklist &Worklist,
8994 MachineInstr &Inst) const {
8995 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
8996 MachineBasicBlock *MBB = Inst.getParent();
8997 MachineOperand &Src0 = Inst.getOperand(1);
8998 MachineOperand &Src1 = Inst.getOperand(2);
8999 const DebugLoc &DL = Inst.getDebugLoc();
9000
9001 switch (Inst.getOpcode()) {
9002 case AMDGPU::S_PACK_LL_B32_B16: {
9003 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
9004 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
9005
9006 // FIXME: Can do a lot better if we know the high bits of src0 or src1 are
9007 // 0.
9008 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
9009 .addImm(0xffff);
9010
9011 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg)
9012 .addReg(ImmReg, RegState::Kill)
9013 .add(Src0);
9014
9015 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg)
9016 .add(Src1)
9017 .addImm(16)
9018 .addReg(TmpReg, RegState::Kill);
9019 break;
9020 }
9021 case AMDGPU::S_PACK_LH_B32_B16: {
9022 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
9023 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
9024 .addImm(0xffff);
9025 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32_e64), ResultReg)
9026 .addReg(ImmReg, RegState::Kill)
9027 .add(Src0)
9028 .add(Src1);
9029 break;
9030 }
9031 case AMDGPU::S_PACK_HL_B32_B16: {
9032 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
9033 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg)
9034 .addImm(16)
9035 .add(Src0);
9036 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg)
9037 .add(Src1)
9038 .addImm(16)
9039 .addReg(TmpReg, RegState::Kill);
9040 break;
9041 }
9042 case AMDGPU::S_PACK_HH_B32_B16: {
9043 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
9044 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
9045 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg)
9046 .addImm(16)
9047 .add(Src0);
9048 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
9049 .addImm(0xffff0000);
9050 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32_e64), ResultReg)
9051 .add(Src1)
9052 .addReg(ImmReg, RegState::Kill)
9053 .addReg(TmpReg, RegState::Kill);
9054 break;
9055 }
9056 default:
9057 llvm_unreachable("unhandled s_pack_* instruction");
9058 }
9059
9060 MachineOperand &Dest = Inst.getOperand(0);
9061 MRI.replaceRegWith(Dest.getReg(), ResultReg);
9062 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
9063}
9064
9065void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op,
9066 MachineInstr &SCCDefInst,
9067 SIInstrWorklist &Worklist,
9068 Register NewCond) const {
9069
9070 // Ensure that def inst defines SCC, which is still live.
9071 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() &&
9072 !Op.isDead() && Op.getParent() == &SCCDefInst);
9073 SmallVector<MachineInstr *, 4> CopyToDelete;
9074 // This assumes that all the users of SCC are in the same block
9075 // as the SCC def.
9076 for (MachineInstr &MI : // Skip the def inst itself.
9077 make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)),
9078 SCCDefInst.getParent()->end())) {
9079 // Check if SCC is used first.
9080 int SCCIdx = MI.findRegisterUseOperandIdx(AMDGPU::SCC, &RI, false);
9081 if (SCCIdx != -1) {
9082 if (MI.isCopy()) {
9083 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
9084 Register DestReg = MI.getOperand(0).getReg();
9085
9086 MRI.replaceRegWith(DestReg, NewCond);
9087 CopyToDelete.push_back(&MI);
9088 } else {
9089
9090 if (NewCond.isValid())
9091 MI.getOperand(SCCIdx).setReg(NewCond);
9092
9093 Worklist.insert(&MI);
9094 }
9095 }
9096 // Exit if we find another SCC def.
9097 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, &RI, false, false) != -1)
9098 break;
9099 }
9100 for (auto &Copy : CopyToDelete)
9101 Copy->eraseFromParent();
9102}
9103
9104// Instructions that use SCC may be converted to VALU instructions. When that
9105// happens, the SCC register is changed to VCC_LO. The instruction that defines
9106// SCC must be changed to an instruction that defines VCC. This function makes
9107// sure that the instruction that defines SCC is added to the moveToVALU
9108// worklist.
9109void SIInstrInfo::addSCCDefsToVALUWorklist(MachineInstr *SCCUseInst,
9110 SIInstrWorklist &Worklist) const {
9111 // Look for a preceding instruction that either defines VCC or SCC. If VCC
9112 // then there is nothing to do because the defining instruction has been
9113 // converted to a VALU already. If SCC then that instruction needs to be
9114 // converted to a VALU.
9115 for (MachineInstr &MI :
9116 make_range(std::next(MachineBasicBlock::reverse_iterator(SCCUseInst)),
9117 SCCUseInst->getParent()->rend())) {
9118 if (MI.modifiesRegister(AMDGPU::VCC, &RI))
9119 break;
9120 if (MI.definesRegister(AMDGPU::SCC, &RI)) {
9121 Worklist.insert(&MI);
9122 break;
9123 }
9124 }
9125}
9126
9127const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass(
9128 const MachineInstr &Inst) const {
9129 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0);
9130
9131 switch (Inst.getOpcode()) {
9132 // For target instructions, getOpRegClass just returns the virtual register
9133 // class associated with the operand, so we need to find an equivalent VGPR
9134 // register class in order to move the instruction to the VALU.
9135 case AMDGPU::COPY:
9136 case AMDGPU::PHI:
9137 case AMDGPU::REG_SEQUENCE:
9138 case AMDGPU::INSERT_SUBREG:
9139 case AMDGPU::WQM:
9140 case AMDGPU::SOFT_WQM:
9141 case AMDGPU::STRICT_WWM:
9142 case AMDGPU::STRICT_WQM: {
9143 const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1);
9144 if (RI.isAGPRClass(SrcRC)) {
9145 if (RI.isAGPRClass(NewDstRC))
9146 return nullptr;
9147
9148 switch (Inst.getOpcode()) {
9149 case AMDGPU::PHI:
9150 case AMDGPU::REG_SEQUENCE:
9151 case AMDGPU::INSERT_SUBREG:
9152 NewDstRC = RI.getEquivalentAGPRClass(NewDstRC);
9153 break;
9154 default:
9155 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
9156 }
9157
9158 if (!NewDstRC)
9159 return nullptr;
9160 } else {
9161 if (RI.isVGPRClass(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass)
9162 return nullptr;
9163
9164 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
9165 if (!NewDstRC)
9166 return nullptr;
9167 }
9168
9169 return NewDstRC;
9170 }
9171 default:
9172 return NewDstRC;
9173 }
9174}
9175
9176// Find the one SGPR operand we are allowed to use.
9177Register SIInstrInfo::findUsedSGPR(const MachineInstr &MI,
9178 int OpIndices[3]) const {
9179 const MCInstrDesc &Desc = MI.getDesc();
9180
9181 // Find the one SGPR operand we are allowed to use.
9182 //
9183 // First we need to consider the instruction's operand requirements before
9184 // legalizing. Some operands are required to be SGPRs, such as implicit uses
9185 // of VCC, but we are still bound by the constant bus requirement to only use
9186 // one.
9187 //
9188 // If the operand's class is an SGPR, we can never move it.
9189
9190 Register SGPRReg = findImplicitSGPRRead(MI);
9191 if (SGPRReg)
9192 return SGPRReg;
9193
9194 Register UsedSGPRs[3] = {Register()};
9195 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
9196
9197 for (unsigned i = 0; i < 3; ++i) {
9198 int Idx = OpIndices[i];
9199 if (Idx == -1)
9200 break;
9201
9202 const MachineOperand &MO = MI.getOperand(Idx);
9203 if (!MO.isReg())
9204 continue;
9205
9206 // Is this operand statically required to be an SGPR based on the operand
9207 // constraints?
9208 const TargetRegisterClass *OpRC =
9209 RI.getRegClass(Desc.operands()[Idx].RegClass);
9210 bool IsRequiredSGPR = RI.isSGPRClass(OpRC);
9211 if (IsRequiredSGPR)
9212 return MO.getReg();
9213
9214 // If this could be a VGPR or an SGPR, Check the dynamic register class.
9215 Register Reg = MO.getReg();
9216 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg);
9217 if (RI.isSGPRClass(RegRC))
9218 UsedSGPRs[i] = Reg;
9219 }
9220
9221 // We don't have a required SGPR operand, so we have a bit more freedom in
9222 // selecting operands to move.
9223
9224 // Try to select the most used SGPR. If an SGPR is equal to one of the
9225 // others, we choose that.
9226 //
9227 // e.g.
9228 // V_FMA_F32 v0, s0, s0, s0 -> No moves
9229 // V_FMA_F32 v0, s0, s1, s0 -> Move s1
9230
9231 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should
9232 // prefer those.
9233
9234 if (UsedSGPRs[0]) {
9235 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2])
9236 SGPRReg = UsedSGPRs[0];
9237 }
9238
9239 if (!SGPRReg && UsedSGPRs[1]) {
9240 if (UsedSGPRs[1] == UsedSGPRs[2])
9241 SGPRReg = UsedSGPRs[1];
9242 }
9243
9244 return SGPRReg;
9245}
9246
9248 AMDGPU::OpName OperandName) const {
9249 if (OperandName == AMDGPU::OpName::NUM_OPERAND_NAMES)
9250 return nullptr;
9251
9252 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName);
9253 if (Idx == -1)
9254 return nullptr;
9255
9256 return &MI.getOperand(Idx);
9257}
9258
9260 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
9261 int64_t Format = ST.getGeneration() >= AMDGPUSubtarget::GFX11
9264 return (Format << 44) |
9265 (1ULL << 56) | // RESOURCE_LEVEL = 1
9266 (3ULL << 60); // OOB_SELECT = 3
9267 }
9268
9269 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT;
9270 if (ST.isAmdHsaOS()) {
9271 // Set ATC = 1. GFX9 doesn't have this bit.
9272 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS)
9273 RsrcDataFormat |= (1ULL << 56);
9274
9275 // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this.
9276 // BTW, it disables TC L2 and therefore decreases performance.
9277 if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS)
9278 RsrcDataFormat |= (2ULL << 59);
9279 }
9280
9281 return RsrcDataFormat;
9282}
9283
9287 0xffffffff; // Size;
9288
9289 // GFX9 doesn't have ELEMENT_SIZE.
9290 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
9291 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize(true)) - 1;
9292 Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT;
9293 }
9294
9295 // IndexStride = 64 / 32.
9296 uint64_t IndexStride = ST.isWave64() ? 3 : 2;
9297 Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT;
9298
9299 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17].
9300 // Clear them unless we want a huge stride.
9301 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
9302 ST.getGeneration() <= AMDGPUSubtarget::GFX9)
9303 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT;
9304
9305 return Rsrc23;
9306}
9307
9309 unsigned Opc = MI.getOpcode();
9310
9311 return isSMRD(Opc);
9312}
9313
9315 return get(Opc).mayLoad() &&
9316 (isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc) || isFLAT(Opc));
9317}
9318
9320 int &FrameIndex) const {
9321 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
9322 if (!Addr || !Addr->isFI())
9323 return Register();
9324
9325 assert(!MI.memoperands_empty() &&
9326 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS);
9327
9328 FrameIndex = Addr->getIndex();
9329 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg();
9330}
9331
9333 int &FrameIndex) const {
9334 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr);
9335 assert(Addr && Addr->isFI());
9336 FrameIndex = Addr->getIndex();
9337 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg();
9338}
9339
9341 int &FrameIndex) const {
9342 if (!MI.mayLoad())
9343 return Register();
9344
9345 if (isMUBUF(MI) || isVGPRSpill(MI))
9346 return isStackAccess(MI, FrameIndex);
9347
9348 if (isSGPRSpill(MI))
9349 return isSGPRStackAccess(MI, FrameIndex);
9350
9351 return Register();
9352}
9353
9355 int &FrameIndex) const {
9356 if (!MI.mayStore())
9357 return Register();
9358
9359 if (isMUBUF(MI) || isVGPRSpill(MI))
9360 return isStackAccess(MI, FrameIndex);
9361
9362 if (isSGPRSpill(MI))
9363 return isSGPRStackAccess(MI, FrameIndex);
9364
9365 return Register();
9366}
9367
9369 unsigned Size = 0;
9371 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
9372 while (++I != E && I->isInsideBundle()) {
9373 assert(!I->isBundle() && "No nested bundle!");
9375 }
9376
9377 return Size;
9378}
9379
9381 unsigned Opc = MI.getOpcode();
9383 unsigned DescSize = Desc.getSize();
9384
9385 // If we have a definitive size, we can use it. Otherwise we need to inspect
9386 // the operands to know the size.
9387 if (isFixedSize(MI)) {
9388 unsigned Size = DescSize;
9389
9390 // If we hit the buggy offset, an extra nop will be inserted in MC so
9391 // estimate the worst case.
9392 if (MI.isBranch() && ST.hasOffset3fBug())
9393 Size += 4;
9394
9395 return Size;
9396 }
9397
9398 // Instructions may have a 32-bit literal encoded after them. Check
9399 // operands that could ever be literals.
9400 if (isVALU(MI) || isSALU(MI)) {
9401 if (isDPP(MI))
9402 return DescSize;
9403 bool HasLiteral = false;
9404 unsigned LiteralSize = 4;
9405 for (int I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) {
9406 const MachineOperand &Op = MI.getOperand(I);
9407 const MCOperandInfo &OpInfo = Desc.operands()[I];
9408 if (!Op.isReg() && !isInlineConstant(Op, OpInfo)) {
9409 HasLiteral = true;
9410 if (ST.has64BitLiterals()) {
9411 switch (OpInfo.OperandType) {
9412 default:
9413 break;
9415 if (!AMDGPU::isValid32BitLiteral(Op.getImm(), true))
9416 LiteralSize = 8;
9417 break;
9419 if (!Op.isImm() || !AMDGPU::isValid32BitLiteral(Op.getImm(), false))
9420 LiteralSize = 8;
9421 break;
9422 }
9423 }
9424 break;
9425 }
9426 }
9427 return HasLiteral ? DescSize + LiteralSize : DescSize;
9428 }
9429
9430 // Check whether we have extra NSA words.
9431 if (isMIMG(MI)) {
9432 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0);
9433 if (VAddr0Idx < 0)
9434 return 8;
9435
9436 int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc);
9437 return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4);
9438 }
9439
9440 switch (Opc) {
9441 case TargetOpcode::BUNDLE:
9442 return getInstBundleSize(MI);
9443 case TargetOpcode::INLINEASM:
9444 case TargetOpcode::INLINEASM_BR: {
9445 const MachineFunction *MF = MI.getParent()->getParent();
9446 const char *AsmStr = MI.getOperand(0).getSymbolName();
9447 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(), &ST);
9448 }
9449 default:
9450 if (MI.isMetaInstruction())
9451 return 0;
9452
9453 // If D16 Pseudo inst, get correct MC code size
9454 const auto *D16Info = AMDGPU::getT16D16Helper(Opc);
9455 if (D16Info) {
9456 // Assume d16_lo/hi inst are always in same size
9457 unsigned LoInstOpcode = D16Info->LoOp;
9458 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(LoInstOpcode);
9459 DescSize = Desc.getSize();
9460 }
9461
9462 return DescSize;
9463 }
9464}
9465
9467 if (!isFLAT(MI))
9468 return false;
9469
9470 if (MI.memoperands_empty())
9471 return true;
9472
9473 for (const MachineMemOperand *MMO : MI.memoperands()) {
9474 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS)
9475 return true;
9476 }
9477 return false;
9478}
9479
9482 static const std::pair<int, const char *> TargetIndices[] = {
9483 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"},
9484 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"},
9485 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"},
9486 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"},
9487 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}};
9488 return ArrayRef(TargetIndices);
9489}
9490
9491/// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The
9492/// post-RA version of misched uses CreateTargetMIHazardRecognizer.
9498
9499/// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer
9500/// pass.
9505
9506// Called during:
9507// - pre-RA scheduling and post-RA scheduling
9510 const ScheduleDAGMI *DAG) const {
9511 // Borrowed from Arm Target
9512 // We would like to restrict this hazard recognizer to only
9513 // post-RA scheduling; we can tell that we're post-RA because we don't
9514 // track VRegLiveness.
9515 if (!DAG->hasVRegLiveness())
9516 return new GCNHazardRecognizer(DAG->MF);
9518}
9519
9520std::pair<unsigned, unsigned>
9522 return std::pair(TF & MO_MASK, TF & ~MO_MASK);
9523}
9524
9527 static const std::pair<unsigned, const char *> TargetFlags[] = {
9528 {MO_GOTPCREL, "amdgpu-gotprel"},
9529 {MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo"},
9530 {MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi"},
9531 {MO_GOTPCREL64, "amdgpu-gotprel64"},
9532 {MO_REL32_LO, "amdgpu-rel32-lo"},
9533 {MO_REL32_HI, "amdgpu-rel32-hi"},
9534 {MO_REL64, "amdgpu-rel64"},
9535 {MO_ABS32_LO, "amdgpu-abs32-lo"},
9536 {MO_ABS32_HI, "amdgpu-abs32-hi"},
9537 {MO_ABS64, "amdgpu-abs64"},
9538 };
9539
9540 return ArrayRef(TargetFlags);
9541}
9542
9545 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
9546 {
9547 {MONoClobber, "amdgpu-noclobber"},
9548 {MOLastUse, "amdgpu-last-use"},
9549 {MOCooperative, "amdgpu-cooperative"},
9550 };
9551
9552 return ArrayRef(TargetFlags);
9553}
9554
9556 const MachineFunction &MF) const {
9558 assert(SrcReg.isVirtual());
9559 if (MFI->checkFlag(SrcReg, AMDGPU::VirtRegFlag::WWM_REG))
9560 return AMDGPU::WWM_COPY;
9561
9562 return AMDGPU::COPY;
9563}
9564
9566 Register Reg) const {
9567 // We need to handle instructions which may be inserted during register
9568 // allocation to handle the prolog. The initial prolog instruction may have
9569 // been separated from the start of the block by spills and copies inserted
9570 // needed by the prolog. However, the insertions for scalar registers can
9571 // always be placed at the BB top as they are independent of the exec mask
9572 // value.
9573 const MachineFunction *MF = MI.getParent()->getParent();
9574 bool IsNullOrVectorRegister = true;
9575 if (Reg) {
9576 const MachineRegisterInfo &MRI = MF->getRegInfo();
9577 IsNullOrVectorRegister = !RI.isSGPRClass(RI.getRegClassForReg(MRI, Reg));
9578 }
9579
9580 uint16_t Opcode = MI.getOpcode();
9582 return IsNullOrVectorRegister &&
9583 (isSGPRSpill(Opcode) || isWWMRegSpillOpcode(Opcode) ||
9584 (Opcode == AMDGPU::IMPLICIT_DEF &&
9585 MFI->isWWMReg(MI.getOperand(0).getReg())) ||
9586 (!MI.isTerminator() && Opcode != AMDGPU::COPY &&
9587 MI.modifiesRegister(AMDGPU::EXEC, &RI)));
9588}
9589
9593 const DebugLoc &DL,
9594 Register DestReg) const {
9595 if (ST.hasAddNoCarry())
9596 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg);
9597
9598 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
9599 Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC());
9600 MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC());
9601
9602 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg)
9603 .addReg(UnusedCarry, RegState::Define | RegState::Dead);
9604}
9605
9608 const DebugLoc &DL,
9609 Register DestReg,
9610 RegScavenger &RS) const {
9611 if (ST.hasAddNoCarry())
9612 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg);
9613
9614 // If available, prefer to use vcc.
9615 Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC)
9616 ? Register(RI.getVCC())
9618 *RI.getBoolRC(), I, /* RestoreAfter */ false,
9619 0, /* AllowSpill */ false);
9620
9621 // TODO: Users need to deal with this.
9622 if (!UnusedCarry.isValid())
9623 return MachineInstrBuilder();
9624
9625 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg)
9626 .addReg(UnusedCarry, RegState::Define | RegState::Dead);
9627}
9628
9629bool SIInstrInfo::isKillTerminator(unsigned Opcode) {
9630 switch (Opcode) {
9631 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
9632 case AMDGPU::SI_KILL_I1_TERMINATOR:
9633 return true;
9634 default:
9635 return false;
9636 }
9637}
9638
9640 switch (Opcode) {
9641 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO:
9642 return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR);
9643 case AMDGPU::SI_KILL_I1_PSEUDO:
9644 return get(AMDGPU::SI_KILL_I1_TERMINATOR);
9645 default:
9646 llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO");
9647 }
9648}
9649
9650bool SIInstrInfo::isLegalMUBUFImmOffset(unsigned Imm) const {
9651 return Imm <= getMaxMUBUFImmOffset(ST);
9652}
9653
9655 // GFX12 field is non-negative 24-bit signed byte offset.
9656 const unsigned OffsetBits =
9657 ST.getGeneration() >= AMDGPUSubtarget::GFX12 ? 23 : 12;
9658 return (1 << OffsetBits) - 1;
9659}
9660
9662 if (!ST.isWave32())
9663 return;
9664
9665 if (MI.isInlineAsm())
9666 return;
9667
9668 for (auto &Op : MI.implicit_operands()) {
9669 if (Op.isReg() && Op.getReg() == AMDGPU::VCC)
9670 Op.setReg(AMDGPU::VCC_LO);
9671 }
9672}
9673
9675 if (!isSMRD(MI))
9676 return false;
9677
9678 // Check that it is using a buffer resource.
9679 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase);
9680 if (Idx == -1) // e.g. s_memtime
9681 return false;
9682
9683 const auto RCID = MI.getDesc().operands()[Idx].RegClass;
9684 return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass);
9685}
9686
9687// Given Imm, split it into the values to put into the SOffset and ImmOffset
9688// fields in an MUBUF instruction. Return false if it is not possible (due to a
9689// hardware bug needing a workaround).
9690//
9691// The required alignment ensures that individual address components remain
9692// aligned if they are aligned to begin with. It also ensures that additional
9693// offsets within the given alignment can be added to the resulting ImmOffset.
9695 uint32_t &ImmOffset, Align Alignment) const {
9696 const uint32_t MaxOffset = SIInstrInfo::getMaxMUBUFImmOffset(ST);
9697 const uint32_t MaxImm = alignDown(MaxOffset, Alignment.value());
9698 uint32_t Overflow = 0;
9699
9700 if (Imm > MaxImm) {
9701 if (Imm <= MaxImm + 64) {
9702 // Use an SOffset inline constant for 4..64
9703 Overflow = Imm - MaxImm;
9704 Imm = MaxImm;
9705 } else {
9706 // Try to keep the same value in SOffset for adjacent loads, so that
9707 // the corresponding register contents can be re-used.
9708 //
9709 // Load values with all low-bits (except for alignment bits) set into
9710 // SOffset, so that a larger range of values can be covered using
9711 // s_movk_i32.
9712 //
9713 // Atomic operations fail to work correctly when individual address
9714 // components are unaligned, even if their sum is aligned.
9715 uint32_t High = (Imm + Alignment.value()) & ~MaxOffset;
9716 uint32_t Low = (Imm + Alignment.value()) & MaxOffset;
9717 Imm = Low;
9718 Overflow = High - Alignment.value();
9719 }
9720 }
9721
9722 if (Overflow > 0) {
9723 // There is a hardware bug in SI and CI which prevents address clamping in
9724 // MUBUF instructions from working correctly with SOffsets. The immediate
9725 // offset is unaffected.
9726 if (ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
9727 return false;
9728
9729 // It is not possible to set immediate in SOffset field on some targets.
9730 if (ST.hasRestrictedSOffset())
9731 return false;
9732 }
9733
9734 ImmOffset = Imm;
9735 SOffset = Overflow;
9736 return true;
9737}
9738
9739// Depending on the used address space and instructions, some immediate offsets
9740// are allowed and some are not.
9741// Pre-GFX12, flat instruction offsets can only be non-negative, global and
9742// scratch instruction offsets can also be negative. On GFX12, offsets can be
9743// negative for all variants.
9744//
9745// There are several bugs related to these offsets:
9746// On gfx10.1, flat instructions that go into the global address space cannot
9747// use an offset.
9748//
9749// For scratch instructions, the address can be either an SGPR or a VGPR.
9750// The following offsets can be used, depending on the architecture (x means
9751// cannot be used):
9752// +----------------------------+------+------+
9753// | Address-Mode | SGPR | VGPR |
9754// +----------------------------+------+------+
9755// | gfx9 | | |
9756// | negative, 4-aligned offset | x | ok |
9757// | negative, unaligned offset | x | ok |
9758// +----------------------------+------+------+
9759// | gfx10 | | |
9760// | negative, 4-aligned offset | ok | ok |
9761// | negative, unaligned offset | ok | x |
9762// +----------------------------+------+------+
9763// | gfx10.3 | | |
9764// | negative, 4-aligned offset | ok | ok |
9765// | negative, unaligned offset | ok | ok |
9766// +----------------------------+------+------+
9767//
9768// This function ignores the addressing mode, so if an offset cannot be used in
9769// one addressing mode, it is considered illegal.
9770bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace,
9771 uint64_t FlatVariant) const {
9772 // TODO: Should 0 be special cased?
9773 if (!ST.hasFlatInstOffsets())
9774 return false;
9775
9776 if (ST.hasFlatSegmentOffsetBug() && FlatVariant == SIInstrFlags::FLAT &&
9777 (AddrSpace == AMDGPUAS::FLAT_ADDRESS ||
9778 AddrSpace == AMDGPUAS::GLOBAL_ADDRESS))
9779 return false;
9780
9781 if (ST.hasNegativeUnalignedScratchOffsetBug() &&
9782 FlatVariant == SIInstrFlags::FlatScratch && Offset < 0 &&
9783 (Offset % 4) != 0) {
9784 return false;
9785 }
9786
9787 bool AllowNegative = allowNegativeFlatOffset(FlatVariant);
9788 unsigned N = AMDGPU::getNumFlatOffsetBits(ST);
9789 return isIntN(N, Offset) && (AllowNegative || Offset >= 0);
9790}
9791
9792// See comment on SIInstrInfo::isLegalFLATOffset for what is legal and what not.
9793std::pair<int64_t, int64_t>
9794SIInstrInfo::splitFlatOffset(int64_t COffsetVal, unsigned AddrSpace,
9795 uint64_t FlatVariant) const {
9796 int64_t RemainderOffset = COffsetVal;
9797 int64_t ImmField = 0;
9798
9799 bool AllowNegative = allowNegativeFlatOffset(FlatVariant);
9800 const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(ST) - 1;
9801
9802 if (AllowNegative) {
9803 // Use signed division by a power of two to truncate towards 0.
9804 int64_t D = 1LL << NumBits;
9805 RemainderOffset = (COffsetVal / D) * D;
9806 ImmField = COffsetVal - RemainderOffset;
9807
9808 if (ST.hasNegativeUnalignedScratchOffsetBug() &&
9809 FlatVariant == SIInstrFlags::FlatScratch && ImmField < 0 &&
9810 (ImmField % 4) != 0) {
9811 // Make ImmField a multiple of 4
9812 RemainderOffset += ImmField % 4;
9813 ImmField -= ImmField % 4;
9814 }
9815 } else if (COffsetVal >= 0) {
9816 ImmField = COffsetVal & maskTrailingOnes<uint64_t>(NumBits);
9817 RemainderOffset = COffsetVal - ImmField;
9818 }
9819
9820 assert(isLegalFLATOffset(ImmField, AddrSpace, FlatVariant));
9821 assert(RemainderOffset + ImmField == COffsetVal);
9822 return {ImmField, RemainderOffset};
9823}
9824
9826 if (ST.hasNegativeScratchOffsetBug() &&
9827 FlatVariant == SIInstrFlags::FlatScratch)
9828 return false;
9829
9830 return FlatVariant != SIInstrFlags::FLAT || AMDGPU::isGFX12Plus(ST);
9831}
9832
9833static unsigned subtargetEncodingFamily(const GCNSubtarget &ST) {
9834 switch (ST.getGeneration()) {
9835 default:
9836 break;
9839 return SIEncodingFamily::SI;
9842 return SIEncodingFamily::VI;
9848 return ST.hasGFX1250Insts() ? SIEncodingFamily::GFX1250
9850 }
9851 llvm_unreachable("Unknown subtarget generation!");
9852}
9853
9854bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const {
9855 switch(MCOp) {
9856 // These opcodes use indirect register addressing so
9857 // they need special handling by codegen (currently missing).
9858 // Therefore it is too risky to allow these opcodes
9859 // to be selected by dpp combiner or sdwa peepholer.
9860 case AMDGPU::V_MOVRELS_B32_dpp_gfx10:
9861 case AMDGPU::V_MOVRELS_B32_sdwa_gfx10:
9862 case AMDGPU::V_MOVRELD_B32_dpp_gfx10:
9863 case AMDGPU::V_MOVRELD_B32_sdwa_gfx10:
9864 case AMDGPU::V_MOVRELSD_B32_dpp_gfx10:
9865 case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10:
9866 case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10:
9867 case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10:
9868 return true;
9869 default:
9870 return false;
9871 }
9872}
9873
9874#define GENERATE_RENAMED_GFX9_CASES(OPCODE) \
9875 case OPCODE##_dpp: \
9876 case OPCODE##_e32: \
9877 case OPCODE##_e64: \
9878 case OPCODE##_e64_dpp: \
9879 case OPCODE##_sdwa:
9880
9881static bool isRenamedInGFX9(int Opcode) {
9882 switch (Opcode) {
9883 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_ADDC_U32)
9884 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_ADD_CO_U32)
9885 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_ADD_U32)
9886 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_SUBBREV_U32)
9887 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_SUBB_U32)
9888 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_SUBREV_CO_U32)
9889 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_SUBREV_U32)
9890 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_SUB_CO_U32)
9891 GENERATE_RENAMED_GFX9_CASES(AMDGPU::V_SUB_U32)
9892 //
9893 case AMDGPU::V_DIV_FIXUP_F16_gfx9_e64:
9894 case AMDGPU::V_DIV_FIXUP_F16_gfx9_fake16_e64:
9895 case AMDGPU::V_FMA_F16_gfx9_e64:
9896 case AMDGPU::V_FMA_F16_gfx9_fake16_e64:
9897 case AMDGPU::V_INTERP_P2_F16:
9898 case AMDGPU::V_MAD_F16_e64:
9899 case AMDGPU::V_MAD_U16_e64:
9900 case AMDGPU::V_MAD_I16_e64:
9901 return true;
9902 default:
9903 return false;
9904 }
9905}
9906
9907int SIInstrInfo::pseudoToMCOpcode(int Opcode) const {
9908 Opcode = SIInstrInfo::getNonSoftWaitcntOpcode(Opcode);
9909
9910 unsigned Gen = subtargetEncodingFamily(ST);
9911
9912 if (ST.getGeneration() == AMDGPUSubtarget::GFX9 && isRenamedInGFX9(Opcode))
9914
9915 // Adjust the encoding family to GFX80 for D16 buffer instructions when the
9916 // subtarget has UnpackedD16VMem feature.
9917 // TODO: remove this when we discard GFX80 encoding.
9918 if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf))
9920
9921 if (get(Opcode).TSFlags & SIInstrFlags::SDWA) {
9922 switch (ST.getGeneration()) {
9923 default:
9925 break;
9928 break;
9931 break;
9932 }
9933 }
9934
9935 if (isMAI(Opcode)) {
9936 int MFMAOp = AMDGPU::getMFMAEarlyClobberOp(Opcode);
9937 if (MFMAOp != -1)
9938 Opcode = MFMAOp;
9939 }
9940
9941 int MCOp = AMDGPU::getMCOpcode(Opcode, Gen);
9942
9943 if (MCOp == (uint16_t)-1 && ST.hasGFX1250Insts())
9945
9946 // -1 means that Opcode is already a native instruction.
9947 if (MCOp == -1)
9948 return Opcode;
9949
9950 if (ST.hasGFX90AInsts()) {
9951 uint16_t NMCOp = (uint16_t)-1;
9952 if (ST.hasGFX940Insts())
9954 if (NMCOp == (uint16_t)-1)
9956 if (NMCOp == (uint16_t)-1)
9958 if (NMCOp != (uint16_t)-1)
9959 MCOp = NMCOp;
9960 }
9961
9962 // (uint16_t)-1 means that Opcode is a pseudo instruction that has
9963 // no encoding in the given subtarget generation.
9964 if (MCOp == (uint16_t)-1)
9965 return -1;
9966
9967 if (isAsmOnlyOpcode(MCOp))
9968 return -1;
9969
9970 return MCOp;
9971}
9972
9973static
9975 assert(RegOpnd.isReg());
9976 return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() :
9977 getRegSubRegPair(RegOpnd);
9978}
9979
9982 assert(MI.isRegSequence());
9983 for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I)
9984 if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) {
9985 auto &RegOp = MI.getOperand(1 + 2 * I);
9986 return getRegOrUndef(RegOp);
9987 }
9989}
9990
9991// Try to find the definition of reg:subreg in subreg-manipulation pseudos
9992// Following a subreg of reg:subreg isn't supported
9995 if (!RSR.SubReg)
9996 return false;
9997 switch (MI.getOpcode()) {
9998 default: break;
9999 case AMDGPU::REG_SEQUENCE:
10000 RSR = getRegSequenceSubReg(MI, RSR.SubReg);
10001 return true;
10002 // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg
10003 case AMDGPU::INSERT_SUBREG:
10004 if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm())
10005 // inserted the subreg we're looking for
10006 RSR = getRegOrUndef(MI.getOperand(2));
10007 else { // the subreg in the rest of the reg
10008 auto R1 = getRegOrUndef(MI.getOperand(1));
10009 if (R1.SubReg) // subreg of subreg isn't supported
10010 return false;
10011 RSR.Reg = R1.Reg;
10012 }
10013 return true;
10014 }
10015 return false;
10016}
10017
10020 assert(MRI.isSSA());
10021 if (!P.Reg.isVirtual())
10022 return nullptr;
10023
10024 auto RSR = P;
10025 auto *DefInst = MRI.getVRegDef(RSR.Reg);
10026 while (auto *MI = DefInst) {
10027 DefInst = nullptr;
10028 switch (MI->getOpcode()) {
10029 case AMDGPU::COPY:
10030 case AMDGPU::V_MOV_B32_e32: {
10031 auto &Op1 = MI->getOperand(1);
10032 if (Op1.isReg() && Op1.getReg().isVirtual()) {
10033 if (Op1.isUndef())
10034 return nullptr;
10035 RSR = getRegSubRegPair(Op1);
10036 DefInst = MRI.getVRegDef(RSR.Reg);
10037 }
10038 break;
10039 }
10040 default:
10041 if (followSubRegDef(*MI, RSR)) {
10042 if (!RSR.Reg)
10043 return nullptr;
10044 DefInst = MRI.getVRegDef(RSR.Reg);
10045 }
10046 }
10047 if (!DefInst)
10048 return MI;
10049 }
10050 return nullptr;
10051}
10052
10054 Register VReg,
10055 const MachineInstr &DefMI,
10056 const MachineInstr &UseMI) {
10057 assert(MRI.isSSA() && "Must be run on SSA");
10058
10059 auto *TRI = MRI.getTargetRegisterInfo();
10060 auto *DefBB = DefMI.getParent();
10061
10062 // Don't bother searching between blocks, although it is possible this block
10063 // doesn't modify exec.
10064 if (UseMI.getParent() != DefBB)
10065 return true;
10066
10067 const int MaxInstScan = 20;
10068 int NumInst = 0;
10069
10070 // Stop scan at the use.
10071 auto E = UseMI.getIterator();
10072 for (auto I = std::next(DefMI.getIterator()); I != E; ++I) {
10073 if (I->isDebugInstr())
10074 continue;
10075
10076 if (++NumInst > MaxInstScan)
10077 return true;
10078
10079 if (I->modifiesRegister(AMDGPU::EXEC, TRI))
10080 return true;
10081 }
10082
10083 return false;
10084}
10085
10087 Register VReg,
10088 const MachineInstr &DefMI) {
10089 assert(MRI.isSSA() && "Must be run on SSA");
10090
10091 auto *TRI = MRI.getTargetRegisterInfo();
10092 auto *DefBB = DefMI.getParent();
10093
10094 const int MaxUseScan = 10;
10095 int NumUse = 0;
10096
10097 for (auto &Use : MRI.use_nodbg_operands(VReg)) {
10098 auto &UseInst = *Use.getParent();
10099 // Don't bother searching between blocks, although it is possible this block
10100 // doesn't modify exec.
10101 if (UseInst.getParent() != DefBB || UseInst.isPHI())
10102 return true;
10103
10104 if (++NumUse > MaxUseScan)
10105 return true;
10106 }
10107
10108 if (NumUse == 0)
10109 return false;
10110
10111 const int MaxInstScan = 20;
10112 int NumInst = 0;
10113
10114 // Stop scan when we have seen all the uses.
10115 for (auto I = std::next(DefMI.getIterator()); ; ++I) {
10116 assert(I != DefBB->end());
10117
10118 if (I->isDebugInstr())
10119 continue;
10120
10121 if (++NumInst > MaxInstScan)
10122 return true;
10123
10124 for (const MachineOperand &Op : I->operands()) {
10125 // We don't check reg masks here as they're used only on calls:
10126 // 1. EXEC is only considered const within one BB
10127 // 2. Call should be a terminator instruction if present in a BB
10128
10129 if (!Op.isReg())
10130 continue;
10131
10132 Register Reg = Op.getReg();
10133 if (Op.isUse()) {
10134 if (Reg == VReg && --NumUse == 0)
10135 return false;
10136 } else if (TRI->regsOverlap(Reg, AMDGPU::EXEC))
10137 return true;
10138 }
10139 }
10140}
10141
10144 const DebugLoc &DL, Register Src, Register Dst) const {
10145 auto Cur = MBB.begin();
10146 if (Cur != MBB.end())
10147 do {
10148 if (!Cur->isPHI() && Cur->readsRegister(Dst, /*TRI=*/nullptr))
10149 return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src);
10150 ++Cur;
10151 } while (Cur != MBB.end() && Cur != LastPHIIt);
10152
10153 return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src,
10154 Dst);
10155}
10156
10159 const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const {
10160 if (InsPt != MBB.end() &&
10161 (InsPt->getOpcode() == AMDGPU::SI_IF ||
10162 InsPt->getOpcode() == AMDGPU::SI_ELSE ||
10163 InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) &&
10164 InsPt->definesRegister(Src, /*TRI=*/nullptr)) {
10165 InsPt++;
10166 return BuildMI(MBB, InsPt, DL,
10167 get(AMDGPU::LaneMaskConstants::get(ST).MovTermOpc), Dst)
10168 .addReg(Src, 0, SrcSubReg)
10169 .addReg(AMDGPU::EXEC, RegState::Implicit);
10170 }
10171 return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg,
10172 Dst);
10173}
10174
10175bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); }
10176
10179 MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
10180 VirtRegMap *VRM) const {
10181 // This is a bit of a hack (copied from AArch64). Consider this instruction:
10182 //
10183 // %0:sreg_32 = COPY $m0
10184 //
10185 // We explicitly chose SReg_32 for the virtual register so such a copy might
10186 // be eliminated by RegisterCoalescer. However, that may not be possible, and
10187 // %0 may even spill. We can't spill $m0 normally (it would require copying to
10188 // a numbered SGPR anyway), and since it is in the SReg_32 register class,
10189 // TargetInstrInfo::foldMemoryOperand() is going to try.
10190 // A similar issue also exists with spilling and reloading $exec registers.
10191 //
10192 // To prevent that, constrain the %0 register class here.
10193 if (isFullCopyInstr(MI)) {
10194 Register DstReg = MI.getOperand(0).getReg();
10195 Register SrcReg = MI.getOperand(1).getReg();
10196 if ((DstReg.isVirtual() || SrcReg.isVirtual()) &&
10197 (DstReg.isVirtual() != SrcReg.isVirtual())) {
10199 Register VirtReg = DstReg.isVirtual() ? DstReg : SrcReg;
10200 const TargetRegisterClass *RC = MRI.getRegClass(VirtReg);
10201 if (RC->hasSuperClassEq(&AMDGPU::SReg_32RegClass)) {
10202 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_32_XM0_XEXECRegClass);
10203 return nullptr;
10204 }
10205 if (RC->hasSuperClassEq(&AMDGPU::SReg_64RegClass)) {
10206 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_64_XEXECRegClass);
10207 return nullptr;
10208 }
10209 }
10210 }
10211
10212 return nullptr;
10213}
10214
10216 const MachineInstr &MI,
10217 unsigned *PredCost) const {
10218 if (MI.isBundle()) {
10220 MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end());
10221 unsigned Lat = 0, Count = 0;
10222 for (++I; I != E && I->isBundledWithPred(); ++I) {
10223 ++Count;
10224 Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I));
10225 }
10226 return Lat + Count - 1;
10227 }
10228
10229 return SchedModel.computeInstrLatency(&MI);
10230}
10231
10234 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
10235 unsigned Opcode = MI.getOpcode();
10236
10237 auto HandleAddrSpaceCast = [this, &MRI](const MachineInstr &MI) {
10238 Register Dst = MI.getOperand(0).getReg();
10239 Register Src = isa<GIntrinsic>(MI) ? MI.getOperand(2).getReg()
10240 : MI.getOperand(1).getReg();
10241 LLT DstTy = MRI.getType(Dst);
10242 LLT SrcTy = MRI.getType(Src);
10243 unsigned DstAS = DstTy.getAddressSpace();
10244 unsigned SrcAS = SrcTy.getAddressSpace();
10245 return SrcAS == AMDGPUAS::PRIVATE_ADDRESS &&
10246 DstAS == AMDGPUAS::FLAT_ADDRESS &&
10247 ST.hasGloballyAddressableScratch()
10250 };
10251
10252 // If the target supports globally addressable scratch, the mapping from
10253 // scratch memory to the flat aperture changes therefore an address space cast
10254 // is no longer uniform.
10255 if (Opcode == TargetOpcode::G_ADDRSPACE_CAST)
10256 return HandleAddrSpaceCast(MI);
10257
10258 if (auto *GI = dyn_cast<GIntrinsic>(&MI)) {
10259 auto IID = GI->getIntrinsicID();
10264
10265 switch (IID) {
10266 case Intrinsic::amdgcn_addrspacecast_nonnull:
10267 return HandleAddrSpaceCast(MI);
10268 case Intrinsic::amdgcn_if:
10269 case Intrinsic::amdgcn_else:
10270 // FIXME: Uniform if second result
10271 break;
10272 }
10273
10275 }
10276
10277 // Loads from the private and flat address spaces are divergent, because
10278 // threads can execute the load instruction with the same inputs and get
10279 // different results.
10280 //
10281 // All other loads are not divergent, because if threads issue loads with the
10282 // same arguments, they will always get the same result.
10283 if (Opcode == AMDGPU::G_LOAD || Opcode == AMDGPU::G_ZEXTLOAD ||
10284 Opcode == AMDGPU::G_SEXTLOAD) {
10285 if (MI.memoperands_empty())
10286 return InstructionUniformity::NeverUniform; // conservative assumption
10287
10288 if (llvm::any_of(MI.memoperands(), [](const MachineMemOperand *mmo) {
10289 return mmo->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
10290 mmo->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS;
10291 })) {
10292 // At least one MMO in a non-global address space.
10294 }
10296 }
10297
10298 if (SIInstrInfo::isGenericAtomicRMWOpcode(Opcode) ||
10299 Opcode == AMDGPU::G_ATOMIC_CMPXCHG ||
10300 Opcode == AMDGPU::G_ATOMIC_CMPXCHG_WITH_SUCCESS ||
10301 AMDGPU::isGenericAtomic(Opcode)) {
10303 }
10305}
10306
10309
10310 if (isNeverUniform(MI))
10312
10313 unsigned opcode = MI.getOpcode();
10314 if (opcode == AMDGPU::V_READLANE_B32 ||
10315 opcode == AMDGPU::V_READFIRSTLANE_B32 ||
10316 opcode == AMDGPU::SI_RESTORE_S32_FROM_VGPR)
10318
10319 if (isCopyInstr(MI)) {
10320 const MachineOperand &srcOp = MI.getOperand(1);
10321 if (srcOp.isReg() && srcOp.getReg().isPhysical()) {
10322 const TargetRegisterClass *regClass =
10323 RI.getPhysRegBaseClass(srcOp.getReg());
10324 return RI.isSGPRClass(regClass) ? InstructionUniformity::AlwaysUniform
10326 }
10328 }
10329
10330 // GMIR handling
10331 if (MI.isPreISelOpcode())
10333
10334 // Atomics are divergent because they are executed sequentially: when an
10335 // atomic operation refers to the same address in each thread, then each
10336 // thread after the first sees the value written by the previous thread as
10337 // original value.
10338
10339 if (isAtomic(MI))
10341
10342 // Loads from the private and flat address spaces are divergent, because
10343 // threads can execute the load instruction with the same inputs and get
10344 // different results.
10345 if (isFLAT(MI) && MI.mayLoad()) {
10346 if (MI.memoperands_empty())
10347 return InstructionUniformity::NeverUniform; // conservative assumption
10348
10349 if (llvm::any_of(MI.memoperands(), [](const MachineMemOperand *mmo) {
10350 return mmo->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
10351 mmo->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS;
10352 })) {
10353 // At least one MMO in a non-global address space.
10355 }
10356
10358 }
10359
10360 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
10361 const AMDGPURegisterBankInfo *RBI = ST.getRegBankInfo();
10362
10363 // FIXME: It's conceptually broken to report this for an instruction, and not
10364 // a specific def operand. For inline asm in particular, there could be mixed
10365 // uniform and divergent results.
10366 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
10367 const MachineOperand &SrcOp = MI.getOperand(I);
10368 if (!SrcOp.isReg())
10369 continue;
10370
10371 Register Reg = SrcOp.getReg();
10372 if (!Reg || !SrcOp.readsReg())
10373 continue;
10374
10375 // If RegBank is null, this is unassigned or an unallocatable special
10376 // register, which are all scalars.
10377 const RegisterBank *RegBank = RBI->getRegBank(Reg, MRI, RI);
10378 if (RegBank && RegBank->getID() != AMDGPU::SGPRRegBankID)
10380 }
10381
10382 // TODO: Uniformity check condtions above can be rearranged for more
10383 // redability
10384
10385 // TODO: amdgcn.{ballot, [if]cmp} should be AlwaysUniform, but they are
10386 // currently turned into no-op COPYs by SelectionDAG ISel and are
10387 // therefore no longer recognizable.
10388
10390}
10391
10393 switch (MF.getFunction().getCallingConv()) {
10395 return 1;
10397 return 2;
10399 return 3;
10403 const Function &F = MF.getFunction();
10404 F.getContext().diagnose(DiagnosticInfoUnsupported(
10405 F, "ds_ordered_count unsupported for this calling conv"));
10406 [[fallthrough]];
10407 }
10410 case CallingConv::C:
10411 case CallingConv::Fast:
10412 default:
10413 // Assume other calling conventions are various compute callable functions
10414 return 0;
10415 }
10416}
10417
10419 Register &SrcReg2, int64_t &CmpMask,
10420 int64_t &CmpValue) const {
10421 if (!MI.getOperand(0).isReg() || MI.getOperand(0).getSubReg())
10422 return false;
10423
10424 switch (MI.getOpcode()) {
10425 default:
10426 break;
10427 case AMDGPU::S_CMP_EQ_U32:
10428 case AMDGPU::S_CMP_EQ_I32:
10429 case AMDGPU::S_CMP_LG_U32:
10430 case AMDGPU::S_CMP_LG_I32:
10431 case AMDGPU::S_CMP_LT_U32:
10432 case AMDGPU::S_CMP_LT_I32:
10433 case AMDGPU::S_CMP_GT_U32:
10434 case AMDGPU::S_CMP_GT_I32:
10435 case AMDGPU::S_CMP_LE_U32:
10436 case AMDGPU::S_CMP_LE_I32:
10437 case AMDGPU::S_CMP_GE_U32:
10438 case AMDGPU::S_CMP_GE_I32:
10439 case AMDGPU::S_CMP_EQ_U64:
10440 case AMDGPU::S_CMP_LG_U64:
10441 SrcReg = MI.getOperand(0).getReg();
10442 if (MI.getOperand(1).isReg()) {
10443 if (MI.getOperand(1).getSubReg())
10444 return false;
10445 SrcReg2 = MI.getOperand(1).getReg();
10446 CmpValue = 0;
10447 } else if (MI.getOperand(1).isImm()) {
10448 SrcReg2 = Register();
10449 CmpValue = MI.getOperand(1).getImm();
10450 } else {
10451 return false;
10452 }
10453 CmpMask = ~0;
10454 return true;
10455 case AMDGPU::S_CMPK_EQ_U32:
10456 case AMDGPU::S_CMPK_EQ_I32:
10457 case AMDGPU::S_CMPK_LG_U32:
10458 case AMDGPU::S_CMPK_LG_I32:
10459 case AMDGPU::S_CMPK_LT_U32:
10460 case AMDGPU::S_CMPK_LT_I32:
10461 case AMDGPU::S_CMPK_GT_U32:
10462 case AMDGPU::S_CMPK_GT_I32:
10463 case AMDGPU::S_CMPK_LE_U32:
10464 case AMDGPU::S_CMPK_LE_I32:
10465 case AMDGPU::S_CMPK_GE_U32:
10466 case AMDGPU::S_CMPK_GE_I32:
10467 SrcReg = MI.getOperand(0).getReg();
10468 SrcReg2 = Register();
10469 CmpValue = MI.getOperand(1).getImm();
10470 CmpMask = ~0;
10471 return true;
10472 }
10473
10474 return false;
10475}
10476
10478 Register SrcReg2, int64_t CmpMask,
10479 int64_t CmpValue,
10480 const MachineRegisterInfo *MRI) const {
10481 if (!SrcReg || SrcReg.isPhysical())
10482 return false;
10483
10484 if (SrcReg2 && !getFoldableImm(SrcReg2, *MRI, CmpValue))
10485 return false;
10486
10487 const auto optimizeCmpAnd = [&CmpInstr, SrcReg, CmpValue, MRI,
10488 this](int64_t ExpectedValue, unsigned SrcSize,
10489 bool IsReversible, bool IsSigned) -> bool {
10490 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
10491 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
10492 // s_cmp_ge_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
10493 // s_cmp_ge_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
10494 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 1 << n => s_and_b64 $src, 1 << n
10495 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
10496 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
10497 // s_cmp_gt_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
10498 // s_cmp_gt_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
10499 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 0 => s_and_b64 $src, 1 << n
10500 //
10501 // Signed ge/gt are not used for the sign bit.
10502 //
10503 // If result of the AND is unused except in the compare:
10504 // s_and_b(32|64) $src, 1 << n => s_bitcmp1_b(32|64) $src, n
10505 //
10506 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n
10507 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n
10508 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 0 => s_bitcmp0_b64 $src, n
10509 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n
10510 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n
10511 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 1 << n => s_bitcmp0_b64 $src, n
10512
10513 MachineInstr *Def = MRI->getUniqueVRegDef(SrcReg);
10514 if (!Def || Def->getParent() != CmpInstr.getParent())
10515 return false;
10516
10517 if (Def->getOpcode() != AMDGPU::S_AND_B32 &&
10518 Def->getOpcode() != AMDGPU::S_AND_B64)
10519 return false;
10520
10521 int64_t Mask;
10522 const auto isMask = [&Mask, SrcSize](const MachineOperand *MO) -> bool {
10523 if (MO->isImm())
10524 Mask = MO->getImm();
10525 else if (!getFoldableImm(MO, Mask))
10526 return false;
10527 Mask &= maxUIntN(SrcSize);
10528 return isPowerOf2_64(Mask);
10529 };
10530
10531 MachineOperand *SrcOp = &Def->getOperand(1);
10532 if (isMask(SrcOp))
10533 SrcOp = &Def->getOperand(2);
10534 else if (isMask(&Def->getOperand(2)))
10535 SrcOp = &Def->getOperand(1);
10536 else
10537 return false;
10538
10539 // A valid Mask is required to have a single bit set, hence a non-zero and
10540 // power-of-two value. This verifies that we will not do 64-bit shift below.
10541 assert(llvm::has_single_bit<uint64_t>(Mask) && "Invalid mask.");
10542 unsigned BitNo = llvm::countr_zero((uint64_t)Mask);
10543 if (IsSigned && BitNo == SrcSize - 1)
10544 return false;
10545
10546 ExpectedValue <<= BitNo;
10547
10548 bool IsReversedCC = false;
10549 if (CmpValue != ExpectedValue) {
10550 if (!IsReversible)
10551 return false;
10552 IsReversedCC = CmpValue == (ExpectedValue ^ Mask);
10553 if (!IsReversedCC)
10554 return false;
10555 }
10556
10557 Register DefReg = Def->getOperand(0).getReg();
10558 if (IsReversedCC && !MRI->hasOneNonDBGUse(DefReg))
10559 return false;
10560
10561 for (auto I = std::next(Def->getIterator()), E = CmpInstr.getIterator();
10562 I != E; ++I) {
10563 if (I->modifiesRegister(AMDGPU::SCC, &RI) ||
10564 I->killsRegister(AMDGPU::SCC, &RI))
10565 return false;
10566 }
10567
10568 MachineOperand *SccDef =
10569 Def->findRegisterDefOperand(AMDGPU::SCC, /*TRI=*/nullptr);
10570 SccDef->setIsDead(false);
10571 CmpInstr.eraseFromParent();
10572
10573 if (!MRI->use_nodbg_empty(DefReg)) {
10574 assert(!IsReversedCC);
10575 return true;
10576 }
10577
10578 // Replace AND with unused result with a S_BITCMP.
10579 MachineBasicBlock *MBB = Def->getParent();
10580
10581 unsigned NewOpc = (SrcSize == 32) ? IsReversedCC ? AMDGPU::S_BITCMP0_B32
10582 : AMDGPU::S_BITCMP1_B32
10583 : IsReversedCC ? AMDGPU::S_BITCMP0_B64
10584 : AMDGPU::S_BITCMP1_B64;
10585
10586 BuildMI(*MBB, Def, Def->getDebugLoc(), get(NewOpc))
10587 .add(*SrcOp)
10588 .addImm(BitNo);
10589 Def->eraseFromParent();
10590
10591 return true;
10592 };
10593
10594 switch (CmpInstr.getOpcode()) {
10595 default:
10596 break;
10597 case AMDGPU::S_CMP_EQ_U32:
10598 case AMDGPU::S_CMP_EQ_I32:
10599 case AMDGPU::S_CMPK_EQ_U32:
10600 case AMDGPU::S_CMPK_EQ_I32:
10601 return optimizeCmpAnd(1, 32, true, false);
10602 case AMDGPU::S_CMP_GE_U32:
10603 case AMDGPU::S_CMPK_GE_U32:
10604 return optimizeCmpAnd(1, 32, false, false);
10605 case AMDGPU::S_CMP_GE_I32:
10606 case AMDGPU::S_CMPK_GE_I32:
10607 return optimizeCmpAnd(1, 32, false, true);
10608 case AMDGPU::S_CMP_EQ_U64:
10609 return optimizeCmpAnd(1, 64, true, false);
10610 case AMDGPU::S_CMP_LG_U32:
10611 case AMDGPU::S_CMP_LG_I32:
10612 case AMDGPU::S_CMPK_LG_U32:
10613 case AMDGPU::S_CMPK_LG_I32:
10614 return optimizeCmpAnd(0, 32, true, false);
10615 case AMDGPU::S_CMP_GT_U32:
10616 case AMDGPU::S_CMPK_GT_U32:
10617 return optimizeCmpAnd(0, 32, false, false);
10618 case AMDGPU::S_CMP_GT_I32:
10619 case AMDGPU::S_CMPK_GT_I32:
10620 return optimizeCmpAnd(0, 32, false, true);
10621 case AMDGPU::S_CMP_LG_U64:
10622 return optimizeCmpAnd(0, 64, true, false);
10623 }
10624
10625 return false;
10626}
10627
10629 AMDGPU::OpName OpName) const {
10630 if (!ST.needsAlignedVGPRs())
10631 return;
10632
10633 int OpNo = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OpName);
10634 if (OpNo < 0)
10635 return;
10636 MachineOperand &Op = MI.getOperand(OpNo);
10637 if (getOpSize(MI, OpNo) > 4)
10638 return;
10639
10640 // Add implicit aligned super-reg to force alignment on the data operand.
10641 const DebugLoc &DL = MI.getDebugLoc();
10642 MachineBasicBlock *BB = MI.getParent();
10644 Register DataReg = Op.getReg();
10645 bool IsAGPR = RI.isAGPR(MRI, DataReg);
10646 Register Undef = MRI.createVirtualRegister(
10647 IsAGPR ? &AMDGPU::AGPR_32RegClass : &AMDGPU::VGPR_32RegClass);
10648 BuildMI(*BB, MI, DL, get(AMDGPU::IMPLICIT_DEF), Undef);
10649 Register NewVR =
10650 MRI.createVirtualRegister(IsAGPR ? &AMDGPU::AReg_64_Align2RegClass
10651 : &AMDGPU::VReg_64_Align2RegClass);
10652 BuildMI(*BB, MI, DL, get(AMDGPU::REG_SEQUENCE), NewVR)
10653 .addReg(DataReg, 0, Op.getSubReg())
10654 .addImm(AMDGPU::sub0)
10655 .addReg(Undef)
10656 .addImm(AMDGPU::sub1);
10657 Op.setReg(NewVR);
10658 Op.setSubReg(AMDGPU::sub0);
10659 MI.addOperand(MachineOperand::CreateReg(NewVR, false, true));
10660}
10661
10663 if (isIGLP(*MI))
10664 return false;
10665
10667}
10668
10670 if (!isWMMA(MI) && !isSWMMAC(MI))
10671 return false;
10672
10673 if (AMDGPU::isGFX1250(ST))
10674 return AMDGPU::getWMMAIsXDL(MI.getOpcode());
10675
10676 return true;
10677}
10678
10680 unsigned Opcode = MI.getOpcode();
10681
10682 if (AMDGPU::isGFX12Plus(ST))
10683 return isDOT(MI) || isXDLWMMA(MI);
10684
10685 if (!isMAI(MI) || isDGEMM(Opcode) ||
10686 Opcode == AMDGPU::V_ACCVGPR_WRITE_B32_e64 ||
10687 Opcode == AMDGPU::V_ACCVGPR_READ_B32_e64)
10688 return false;
10689
10690 if (!ST.hasGFX940Insts())
10691 return true;
10692
10693 return AMDGPU::getMAIIsGFX940XDL(Opcode);
10694}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Contains the definition of a TargetInstrInfo class that is common to all AMD GPUs.
AMDGPU Register Bank Select
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
static bool isUndef(const MachineInstr &MI)
TargetInstrInfo::RegSubRegPair RegSubRegPair
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
MachineInstr unsigned OpIdx
uint64_t High
uint64_t IntrinsicInst * II
#define P(N)
R600 Clause Merge
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
This file declares the machine register scavenger class.
static cl::opt< bool > Fix16BitCopies("amdgpu-fix-16-bit-physreg-copies", cl::desc("Fix copies between 32 and 16 bit registers by extending to 32 bit"), cl::init(true), cl::ReallyHidden)
static void expandSGPRCopy(const SIInstrInfo &TII, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg, bool KillSrc, const TargetRegisterClass *RC, bool Forward)
static unsigned getNewFMAInst(const GCNSubtarget &ST, unsigned Opc)
static void indirectCopyToAGPR(const SIInstrInfo &TII, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg, bool KillSrc, RegScavenger &RS, bool RegsOverlap, Register ImpDefSuperReg=Register(), Register ImpUseSuperReg=Register())
Handle copying from SGPR to AGPR, or from AGPR to AGPR on GFX908.
static unsigned getIndirectSGPRWriteMovRelPseudo32(unsigned VecSize)
static bool compareMachineOp(const MachineOperand &Op0, const MachineOperand &Op1)
static bool isStride64(unsigned Opc)
#define GENERATE_RENAMED_GFX9_CASES(OPCODE)
static std::tuple< unsigned, unsigned > extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc)
static bool followSubRegDef(MachineInstr &MI, TargetInstrInfo::RegSubRegPair &RSR)
static unsigned getIndirectSGPRWriteMovRelPseudo64(unsigned VecSize)
static MachineInstr * swapImmOperands(MachineInstr &MI, MachineOperand &NonRegOp1, MachineOperand &NonRegOp2)
static void copyFlagsToImplicitVCC(MachineInstr &MI, const MachineOperand &Orig)
static void emitLoadScalarOpsFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI, MachineBasicBlock &LoopBB, MachineBasicBlock &BodyBB, const DebugLoc &DL, ArrayRef< MachineOperand * > ScalarOps)
static bool offsetsDoNotOverlap(LocationSize WidthA, int OffsetA, LocationSize WidthB, int OffsetB)
static unsigned getWWMRegSpillSaveOpcode(unsigned Size, bool IsVectorSuperClass)
static bool memOpsHaveSameBaseOperands(ArrayRef< const MachineOperand * > BaseOps1, ArrayRef< const MachineOperand * > BaseOps2)
static unsigned getWWMRegSpillRestoreOpcode(unsigned Size, bool IsVectorSuperClass)
static bool getFoldableImm(Register Reg, const MachineRegisterInfo &MRI, int64_t &Imm, MachineInstr **DefMI=nullptr)
static unsigned getIndirectVGPRWriteMovRelPseudoOpc(unsigned VecSize)
static unsigned subtargetEncodingFamily(const GCNSubtarget &ST)
static void preserveCondRegFlags(MachineOperand &CondReg, const MachineOperand &OrigCond)
static Register findImplicitSGPRRead(const MachineInstr &MI)
static unsigned getNewFMAAKInst(const GCNSubtarget &ST, unsigned Opc)
static cl::opt< unsigned > BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16), cl::desc("Restrict range of branch instructions (DEBUG)"))
static void updateLiveVariables(LiveVariables *LV, MachineInstr &MI, MachineInstr &NewMI)
static bool memOpsHaveSameBasePtr(const MachineInstr &MI1, ArrayRef< const MachineOperand * > BaseOps1, const MachineInstr &MI2, ArrayRef< const MachineOperand * > BaseOps2)
static unsigned getSGPRSpillRestoreOpcode(unsigned Size)
static bool isRegOrFI(const MachineOperand &MO)
static unsigned getSGPRSpillSaveOpcode(unsigned Size)
static constexpr AMDGPU::OpName ModifierOpNames[]
static unsigned getVGPRSpillSaveOpcode(unsigned Size)
static void reportIllegalCopy(const SIInstrInfo *TII, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg, bool KillSrc, const char *Msg="illegal VGPR to SGPR copy")
static MachineInstr * swapRegAndNonRegOperand(MachineInstr &MI, MachineOperand &RegOp, MachineOperand &NonRegOp)
static const TargetRegisterClass * adjustAllocatableRegClass(const GCNSubtarget &ST, const SIRegisterInfo &RI, const MCInstrDesc &TID, unsigned RCID)
static bool shouldReadExec(const MachineInstr &MI)
static unsigned getNewFMAMKInst(const GCNSubtarget &ST, unsigned Opc)
static bool isRenamedInGFX9(int Opcode)
static TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd)
static bool changesVGPRIndexingMode(const MachineInstr &MI)
static bool isSubRegOf(const SIRegisterInfo &TRI, const MachineOperand &SuperVec, const MachineOperand &SubReg)
static bool nodesHaveSameOperandValue(SDNode *N0, SDNode *N1, AMDGPU::OpName OpName)
Returns true if both nodes have the same value for the given operand Op, or if both nodes do not have...
static unsigned getAVSpillSaveOpcode(unsigned Size)
static unsigned getNumOperandsNoGlue(SDNode *Node)
static bool canRemat(const MachineInstr &MI)
static MachineBasicBlock * loadMBUFScalarOperandsFromVGPR(const SIInstrInfo &TII, MachineInstr &MI, ArrayRef< MachineOperand * > ScalarOps, MachineDominatorTree *MDT, MachineBasicBlock::iterator Begin=nullptr, MachineBasicBlock::iterator End=nullptr)
static unsigned getAVSpillRestoreOpcode(unsigned Size)
static unsigned getVGPRSpillRestoreOpcode(unsigned Size)
Interface definition for SIInstrInfo.
bool IsDead
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
#define LLVM_DEBUG(...)
Definition Debug.h:114
static const LaneMaskConstants & get(const GCNSubtarget &ST)
Class for arbitrary precision integers.
Definition APInt.h:78
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1562
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition ArrayRef.h:150
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
uint64_t getZExtValue() const
A debug info location.
Definition DebugLoc.h:124
Diagnostic information for unsupported feature in backend.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:359
bool hasAddNoCarry() const
CycleT * getCycle(const BlockT *Block) const
Find the innermost cycle containing a given block.
void getExitingBlocks(SmallVectorImpl< BlockT * > &TmpStorage) const
Return all blocks of this cycle that have successor outside of this cycle.
bool contains(const BlockT *Block) const
Return whether Block is contained in the cycle.
const GenericCycle * getParentCycle() const
Itinerary data supplied by a subtarget to be used by a target.
constexpr unsigned getAddressSpace() const
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LiveInterval - This class represents the liveness of a register, or stack slot.
bool hasInterval(Register Reg) const
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
LiveInterval & getInterval(Register Reg)
LLVM_ABI bool shrinkToUses(LiveInterval *li, SmallVectorImpl< MachineInstr * > *dead=nullptr)
After removing some uses of a register, shrink its live range to just the remaining uses.
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
This class represents the liveness of a register, stack slot, etc.
LLVM_ABI void replaceKillInstruction(Register Reg, MachineInstr &OldMI, MachineInstr &NewMI)
replaceKillInstruction - Update register kill info by replacing a kill instruction with a new one.
LLVM_ABI VarInfo & getVarInfo(Register Reg)
getVarInfo - Return the VarInfo structure for the specified VIRTUAL register.
bool hasValue() const
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
static const MCBinaryExpr * createAnd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:348
static const MCBinaryExpr * createAShr(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:418
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:428
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
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
bool mayStore() const
Return true if this instruction could possibly modify memory.
bool mayLoad() const
Return true if this instruction could possibly read memory.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
unsigned getSize() const
Return the number of bytes in the encoding of this instruction, or zero if the encoding size cannot b...
ArrayRef< MCPhysReg > implicit_uses() const
Return a list of registers that are potentially read by any instance of this machine instruction.
unsigned getOpcode() const
Return the opcode number for this descriptor.
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:86
uint8_t OperandType
Information about the type of the operand.
Definition MCInstrDesc.h:98
int16_t RegClass
This specifies the register class enumeration of the operand if the operand is a register.
Definition MCInstrDesc.h:92
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI void setVariableValue(const MCExpr *Value)
Definition MCSymbol.cpp:50
Helper class for constructing bundles of MachineInstrs.
MachineBasicBlock::instr_iterator begin() const
Return an iterator to the first bundled instruction.
MIBundleBuilder & append(MachineInstr *MI)
Insert MI into MBB by appending it to the instructions in the bundle.
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
Instructions::const_iterator const_instr_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
@ LQR_Dead
Register is known to be fully dead.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
void push_back(MachineBasicBlock *MBB)
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const MachineInstrBuilder & copyImplicitOps(const MachineInstr &OtherMI) const
Copy all the implicit operands from OtherMI onto this one.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
bool isCopy() const
const MachineBasicBlock * getParent() const
LLVM_ABI void addImplicitDefUseOperands(MachineFunction &MF)
Add all implicit def and use operands to this instruction.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
mop_range implicit_operands()
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
LLVM_ABI bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore,...
void untieRegOperand(unsigned OpIdx)
Break any tie involving OpIdx.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
mop_range explicit_operands()
LLVM_ABI void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
LLVM_ABI bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
ArrayRef< MachineMemOperand * > memoperands() const
Access to memory operands of the instruction.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
bool isMoveImmediate(QueryType Type=IgnoreBundle) const
Return true if this instruction is a move immediate (including conditional moves) instruction.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
LLVM_ABI void setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just after the instruction itself.
const MachineOperand & getOperand(unsigned i) const
uint32_t getFlags() const
Return the MI flags bitvector.
LLVM_ABI int findRegisterDefOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false) const
Returns the operand index that is a def of the specified register or -1 if it is not found.
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
LLVM_ABI unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
const GlobalValue * getGlobal() const
void setImplicit(bool Val=true)
LLVM_ABI void ChangeToFrameIndex(int Idx, unsigned TargetFlags=0)
Replace this operand with a frame index.
void setImm(int64_t immVal)
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
LLVM_ABI void ChangeToGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
ChangeToGA - Replace this operand with a new global address operand.
void setIsKill(bool Val=true)
LLVM_ABI void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
void setOffset(int64_t Offset)
unsigned getTargetFlags() const
static MachineOperand CreateImm(int64_t Val)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
void setIsUndef(bool Val=true)
Register getReg() const
getReg - Returns the register number.
bool isTargetIndex() const
isTargetIndex - Tests if this is a MO_TargetIndex operand.
void setTargetFlags(unsigned F)
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
@ MO_Immediate
Immediate operand.
@ MO_Register
Register operand.
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)
int64_t getOffset() const
Return the offset from the symbol in this operand.
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
void enterBasicBlockEnd(MachineBasicBlock &MBB)
Start tracking liveness from the end of basic block MBB.
bool isRegUsed(Register Reg, bool includeReserved=true) const
Return if a specific register is currently used.
void setRegUsed(Register Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Tell the scavenger a register is used.
void backward()
Update internal register state and move MBB iterator backwards.
void enterBasicBlock(MachineBasicBlock &MBB)
Start tracking liveness from the begin of basic block MBB.
Register scavengeRegisterBackwards(const TargetRegisterClass &RC, MachineBasicBlock::iterator To, bool RestoreAfter, int SPAdj, bool AllowSpill=true)
Make a register of the specific register class available from the current position backwards to the p...
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
This class implements the register bank concept.
unsigned getID() const
Get the identifier of this register bank.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:102
constexpr bool isValid() const
Definition Register.h:107
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:78
Represents one node in the SelectionDAG.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
const SDValue & getOperand(unsigned Num) const
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isLegalMUBUFImmOffset(unsigned Imm) const
bool isInlineConstant(const APInt &Imm) const
void legalizeOperandsVOP3(MachineRegisterInfo &MRI, MachineInstr &MI) const
Fix operands in MI to satisfy constant bus requirements.
static bool isDS(const MachineInstr &MI)
MachineBasicBlock * legalizeOperands(MachineInstr &MI, MachineDominatorTree *MDT=nullptr) const
Legalize all operands in this instruction.
bool areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1, int64_t &Offset0, int64_t &Offset1) const override
unsigned getLiveRangeSplitOpcode(Register Reg, const MachineFunction &MF) const override
bool getMemOperandsWithOffsetWidth(const MachineInstr &LdSt, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width, const TargetRegisterInfo *TRI) const final
Register isSGPRStackAccess(const MachineInstr &MI, int &FrameIndex) const
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
static bool isNeverUniform(const MachineInstr &MI)
unsigned getOpSize(uint16_t Opcode, unsigned OpNo) const
Return the size in bytes of the operand OpNo on the given.
bool isXDLWMMA(const MachineInstr &MI) const
bool isBasicBlockPrologue(const MachineInstr &MI, Register Reg=Register()) const override
uint64_t getDefaultRsrcDataFormat() const
static bool isSOPP(const MachineInstr &MI)
InstructionUniformity getGenericInstructionUniformity(const MachineInstr &MI) const
bool isIGLP(unsigned Opcode) const
static bool isFLATScratch(const MachineInstr &MI)
const MCInstrDesc & getIndirectRegWriteMovRelPseudo(unsigned VecSize, unsigned EltSize, bool IsSGPR) const
MachineInstrBuilder getAddNoCarry(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg) const
Return a partially built integer add instruction without carry.
bool mayAccessFlatAddressSpace(const MachineInstr &MI) const
bool shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1, int64_t Offset0, int64_t Offset1, unsigned NumLoads) const override
bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset, Align Alignment=Align(4)) const
ArrayRef< std::pair< unsigned, const char * > > getSerializableDirectMachineOperandTargetFlags() const override
void moveToVALU(SIInstrWorklist &Worklist, MachineDominatorTree *MDT) const
Replace the instructions opcode with the equivalent VALU opcode.
static bool isSMRD(const MachineInstr &MI)
void restoreExec(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register Reg, SlotIndexes *Indexes=nullptr) const
bool usesConstantBus(const MachineRegisterInfo &MRI, const MachineOperand &MO, const MCOperandInfo &OpInfo) const
Returns true if this operand uses the constant bus.
static unsigned getMaxMUBUFImmOffset(const GCNSubtarget &ST)
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
bool mayAccessScratchThroughFlat(const MachineInstr &MI) const
void legalizeOperandsFLAT(MachineRegisterInfo &MRI, MachineInstr &MI) const
bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask, int64_t CmpValue, const MachineRegisterInfo *MRI) const override
static std::optional< int64_t > extractSubregFromImm(int64_t ImmVal, unsigned SubRegIndex)
Return the extracted immediate value in a subregister use from a constant materialized in a super reg...
Register isStackAccess(const MachineInstr &MI, int &FrameIndex) const
static bool isMTBUF(const MachineInstr &MI)
const MCInstrDesc & getIndirectGPRIDXPseudo(unsigned VecSize, bool IsIndirectSrc) const
void insertReturn(MachineBasicBlock &MBB) const
static bool isDGEMM(unsigned Opcode)
static bool isEXP(const MachineInstr &MI)
static bool isSALU(const MachineInstr &MI)
void legalizeGenericOperand(MachineBasicBlock &InsertMBB, MachineBasicBlock::iterator I, const TargetRegisterClass *DstRC, MachineOperand &Op, MachineRegisterInfo &MRI, const DebugLoc &DL) const
MachineInstr * buildShrunkInst(MachineInstr &MI, unsigned NewOpcode) const
unsigned getInstBundleSize(const MachineInstr &MI) const
static bool isVOP2(const MachineInstr &MI)
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify=false) const override
static bool isSDWA(const MachineInstr &MI)
const MCInstrDesc & getKillTerminatorFromPseudo(unsigned Opcode) const
void insertNoops(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned Quantity) const override
static bool isGather4(const MachineInstr &MI)
MachineInstr * getWholeWaveFunctionSetup(MachineFunction &MF) const
bool isLegalVSrcOperand(const MachineRegisterInfo &MRI, const MCOperandInfo &OpInfo, const MachineOperand &MO) const
Check if MO would be a valid operand for the given operand definition OpInfo.
static bool isDOT(const MachineInstr &MI)
MachineInstr * createPHISourceCopy(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const override
bool hasModifiers(unsigned Opcode) const
Return true if this instruction has any modifiers.
bool shouldClusterMemOps(ArrayRef< const MachineOperand * > BaseOps1, int64_t Offset1, bool OffsetIsScalable1, ArrayRef< const MachineOperand * > BaseOps2, int64_t Offset2, bool OffsetIsScalable2, unsigned ClusterSize, unsigned NumBytes) const override
static bool isSWMMAC(const MachineInstr &MI)
ScheduleHazardRecognizer * CreateTargetMIHazardRecognizer(const InstrItineraryData *II, const ScheduleDAGMI *DAG) const override
bool isWave32() const
bool isHighLatencyDef(int Opc) const override
void legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const
Legalize the OpIndex operand of this instruction by inserting a MOV.
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
static bool isVOPC(const MachineInstr &MI)
void removeModOperands(MachineInstr &MI) const
std::pair< int64_t, int64_t > splitFlatOffset(int64_t COffsetVal, unsigned AddrSpace, uint64_t FlatVariant) const
Split COffsetVal into {immediate offset field, remainder offset} values.
bool isSpill(uint16_t Opcode) const
unsigned getVectorRegSpillRestoreOpcode(Register Reg, const TargetRegisterClass *RC, unsigned Size, const SIMachineFunctionInfo &MFI) const
bool isXDL(const MachineInstr &MI) const
static bool isVIMAGE(const MachineInstr &MI)
void enforceOperandRCAlignment(MachineInstr &MI, AMDGPU::OpName OpName) const
static bool isSOP2(const MachineInstr &MI)
static bool isGWS(const MachineInstr &MI)
bool isLegalAV64PseudoImm(uint64_t Imm) const
Check if this immediate value can be used for AV_MOV_B64_IMM_PSEUDO.
bool hasModifiersSet(const MachineInstr &MI, AMDGPU::OpName OpName) const
const TargetRegisterClass * getPreferredSelectRegClass(unsigned Size) const
bool isLegalToSwap(const MachineInstr &MI, unsigned fromIdx, unsigned toIdx) const
bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override
static bool isFLATGlobal(const MachineInstr &MI)
bool isGlobalMemoryObject(const MachineInstr *MI) const override
static bool isVSAMPLE(const MachineInstr &MI)
bool isBufferSMRD(const MachineInstr &MI) const
static bool isKillTerminator(unsigned Opcode)
bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx0, unsigned &SrcOpIdx1) const override
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
void insertScratchExecCopy(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register Reg, bool IsSCCLive, SlotIndexes *Indexes=nullptr) const
bool hasVALU32BitEncoding(unsigned Opcode) const
Return true if this 64-bit VALU instruction has a 32-bit encoding.
unsigned getMovOpcode(const TargetRegisterClass *DstRC) const
void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg, unsigned SubIdx, const MachineInstr &Orig, const TargetRegisterInfo &TRI) const override
unsigned buildExtractSubReg(MachineBasicBlock::iterator MI, MachineRegisterInfo &MRI, const MachineOperand &SuperReg, const TargetRegisterClass *SuperRC, unsigned SubIdx, const TargetRegisterClass *SubRC) const
void legalizeOperandsVOP2(MachineRegisterInfo &MRI, MachineInstr &MI) const
Legalize operands in MI by either commuting it or inserting a copy of src1.
bool foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg, MachineRegisterInfo *MRI) const final
static bool isImage(const MachineInstr &MI)
static bool isSOPK(const MachineInstr &MI)
const TargetRegisterClass * getOpRegClass(const MachineInstr &MI, unsigned OpNo) const
Return the correct register class for OpNo.
MachineBasicBlock * insertSimulatedTrap(MachineRegisterInfo &MRI, MachineBasicBlock &MBB, MachineInstr &MI, const DebugLoc &DL) const
Build instructions that simulate the behavior of a s_trap 2 instructions for hardware (namely,...
static unsigned getNonSoftWaitcntOpcode(unsigned Opcode)
static unsigned getDSShaderTypeValue(const MachineFunction &MF)
static bool isFoldableCopy(const MachineInstr &MI)
bool isIgnorableUse(const MachineOperand &MO) const override
static bool isMUBUF(const MachineInstr &MI)
bool expandPostRAPseudo(MachineInstr &MI) const override
bool analyzeCompare(const MachineInstr &MI, Register &SrcReg, Register &SrcReg2, int64_t &CmpMask, int64_t &CmpValue) const override
const TargetRegisterClass * getRegClass(const MCInstrDesc &TID, unsigned OpNum, const TargetRegisterInfo *TRI) const override
InstructionUniformity getInstructionUniformity(const MachineInstr &MI) const override final
static bool isSegmentSpecificFLAT(const MachineInstr &MI)
static bool isVOP3(const MCInstrDesc &Desc)
bool physRegUsesConstantBus(const MachineOperand &Reg) const
static bool isF16PseudoScalarTrans(unsigned Opcode)
void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DstReg, ArrayRef< MachineOperand > Cond, Register TrueReg, Register FalseReg) const override
static bool isDPP(const MachineInstr &MI)
bool analyzeBranchImpl(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const
bool isLowLatencyInstruction(const MachineInstr &MI) const
std::optional< DestSourcePair > isCopyInstrImpl(const MachineInstr &MI) const override
If the specific machine instruction is a instruction that moves/copies value from one register to ano...
bool isAlwaysGDS(uint16_t Opcode) const
static bool isMAI(const MCInstrDesc &Desc)
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
void legalizeOperandsVALUt16(MachineInstr &Inst, MachineRegisterInfo &MRI) const
Fix operands in Inst to fix 16bit SALU to VALU lowering.
void moveToVALUImpl(SIInstrWorklist &Worklist, MachineDominatorTree *MDT, MachineInstr &Inst) const
bool isImmOperandLegal(const MCInstrDesc &InstDesc, unsigned OpNo, const MachineOperand &MO) const
bool canShrink(const MachineInstr &MI, const MachineRegisterInfo &MRI) const
bool isAsmOnlyOpcode(int MCOp) const
Check if this instruction should only be used by assembler.
static bool isVGPRSpill(const MachineInstr &MI)
ScheduleHazardRecognizer * CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, const ScheduleDAG *DAG) const override
This is used by the post-RA scheduler (SchedulePostRAList.cpp).
bool verifyInstruction(const MachineInstr &MI, StringRef &ErrInfo) const override
bool isLegalFLATOffset(int64_t Offset, unsigned AddrSpace, uint64_t FlatVariant) const
Returns if Offset is legal for the subtarget as the offset to a FLAT encoded instruction with the giv...
static bool isWWMRegSpillOpcode(uint16_t Opcode)
unsigned getInstrLatency(const InstrItineraryData *ItinData, const MachineInstr &MI, unsigned *PredCost=nullptr) const override
MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS=nullptr, VirtRegMap *VRM=nullptr) const override
int64_t getNamedImmOperand(const MachineInstr &MI, AMDGPU::OpName OperandName) const
Get required immediate operand.
ArrayRef< std::pair< int, const char * > > getSerializableTargetIndices() const override
bool regUsesConstantBus(const MachineOperand &Reg, const MachineRegisterInfo &MRI) const
static bool isMIMG(const MachineInstr &MI)
MachineOperand buildExtractSubRegOrImm(MachineBasicBlock::iterator MI, MachineRegisterInfo &MRI, const MachineOperand &SuperReg, const TargetRegisterClass *SuperRC, unsigned SubIdx, const TargetRegisterClass *SubRC) const
bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const override
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
bool isLegalRegOperand(const MachineRegisterInfo &MRI, const MCOperandInfo &OpInfo, const MachineOperand &MO) const
Check if MO (a register operand) is a legal register for the given operand description or operand ind...
bool allowNegativeFlatOffset(uint64_t FlatVariant) const
Returns true if negative offsets are allowed for the given FlatVariant.
static unsigned getNumWaitStates(const MachineInstr &MI)
Return the number of wait states that result from executing this instruction.
unsigned getVectorRegSpillSaveOpcode(Register Reg, const TargetRegisterClass *RC, unsigned Size, const SIMachineFunctionInfo &MFI) const
unsigned getVALUOp(const MachineInstr &MI) const
static bool modifiesModeRegister(const MachineInstr &MI)
Return true if the instruction modifies the mode register.q.
Register readlaneVGPRToSGPR(Register SrcReg, MachineInstr &UseMI, MachineRegisterInfo &MRI, const TargetRegisterClass *DstRC=nullptr) const
Copy a value from a VGPR (SrcReg) to SGPR.
bool hasDivergentBranch(const MachineBasicBlock *MBB) const
Return whether the block terminate with divergent branch.
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
void fixImplicitOperands(MachineInstr &MI) const
bool moveFlatAddrToVGPR(MachineInstr &Inst) const
Change SADDR form of a FLAT Inst to its VADDR form if saddr operand was moved to VGPR.
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
bool swapSourceModifiers(MachineInstr &MI, MachineOperand &Src0, AMDGPU::OpName Src0OpName, MachineOperand &Src1, AMDGPU::OpName Src1OpName) const
Register insertNE(MachineBasicBlock *MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register SrcReg, int Value) const
MachineBasicBlock * getBranchDestBlock(const MachineInstr &MI) const override
bool hasUnwantedEffectsWhenEXECEmpty(const MachineInstr &MI) const
This function is used to determine if an instruction can be safely executed under EXEC = 0 without ha...
bool getConstValDefinedInReg(const MachineInstr &MI, const Register Reg, int64_t &ImmVal) const override
static bool isAtomic(const MachineInstr &MI)
bool canInsertSelect(const MachineBasicBlock &MBB, ArrayRef< MachineOperand > Cond, Register DstReg, Register TrueReg, Register FalseReg, int &CondCycles, int &TrueCycles, int &FalseCycles) const override
bool isLiteralOperandLegal(const MCInstrDesc &InstDesc, const MCOperandInfo &OpInfo) const
static bool sopkIsZext(unsigned Opcode)
static bool isSGPRSpill(const MachineInstr &MI)
static bool isWMMA(const MachineInstr &MI)
ArrayRef< std::pair< MachineMemOperand::Flags, const char * > > getSerializableMachineMemOperandTargetFlags() const override
MachineInstr * convertToThreeAddress(MachineInstr &MI, LiveVariables *LV, LiveIntervals *LIS) const override
bool mayReadEXEC(const MachineRegisterInfo &MRI, const MachineInstr &MI) const
Returns true if the instruction could potentially depend on the value of exec.
void legalizeOperandsSMRD(MachineRegisterInfo &MRI, MachineInstr &MI) const
bool isBranchOffsetInRange(unsigned BranchOpc, int64_t BrOffset) const override
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
void insertVectorSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DstReg, ArrayRef< MachineOperand > Cond, Register TrueReg, Register FalseReg) const
void insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
std::pair< MachineInstr *, MachineInstr * > expandMovDPP64(MachineInstr &MI) const
Register insertEQ(MachineBasicBlock *MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register SrcReg, int Value) const
static bool isSOPC(const MachineInstr &MI)
static bool isFLAT(const MachineInstr &MI)
static bool isVALU(const MachineInstr &MI)
bool isBarrier(unsigned Opcode) const
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx0, unsigned OpIdx1) const override
int pseudoToMCOpcode(int Opcode) const
Return a target-specific opcode if Opcode is a pseudo instruction.
const MCInstrDesc & getMCOpcodeFromPseudo(unsigned Opcode) const
Return the descriptor of the target-specific machine instruction that corresponds to the specified ps...
bool isLegalGFX12PlusPackedMathFP32Operand(const MachineRegisterInfo &MRI, const MachineInstr &MI, unsigned SrcN, const MachineOperand *MO=nullptr) const
Check if MO would be a legal operand for gfx12+ packed math FP32 instructions.
MachineInstr * createPHIDestinationCopy(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, const DebugLoc &DL, Register Src, Register Dst) const override
static bool isFixedSize(const MachineInstr &MI)
bool isSafeToSink(MachineInstr &MI, MachineBasicBlock *SuccToSinkTo, MachineCycleInfo *CI) const override
LLVM_READONLY int commuteOpcode(unsigned Opc) const
uint64_t getScratchRsrcWords23() const
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, AMDGPU::OpName OperandName) const
Returns the operand named Op.
std::pair< unsigned, unsigned > decomposeMachineOperandsTargetFlags(unsigned TF) const override
bool areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, const MachineInstr &MIb) const override
bool isOperandLegal(const MachineInstr &MI, unsigned OpIdx, const MachineOperand *MO=nullptr) const
Check if MO is a legal operand if it was the OpIdx Operand for MI.
static bool isLDSDMA(const MachineInstr &MI)
static bool isVOP1(const MachineInstr &MI)
SIInstrInfo(const GCNSubtarget &ST)
void insertIndirectBranch(MachineBasicBlock &MBB, MachineBasicBlock &NewDestBB, MachineBasicBlock &RestoreBB, const DebugLoc &DL, int64_t BrOffset, RegScavenger *RS) const override
bool hasAnyModifiersSet(const MachineInstr &MI) const
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
void setHasSpilledVGPRs(bool Spill=true)
bool isWWMReg(Register Reg) const
bool checkFlag(Register Reg, uint8_t Flag) const
void setHasSpilledSGPRs(bool Spill=true)
const TargetRegisterClass * getRegClass(unsigned RCID) const
static unsigned getSubRegFromChannel(unsigned Channel, unsigned NumRegs=1)
const TargetRegisterClass * getProperlyAlignedRC(const TargetRegisterClass *RC) const
ArrayRef< int16_t > getRegSplitParts(const TargetRegisterClass *RC, unsigned EltSize) const
unsigned getHWRegIndex(MCRegister Reg) const
unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const override
unsigned getChannelFromSubReg(unsigned SubReg) const
static bool isAGPRClass(const TargetRegisterClass *RC)
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
virtual bool hasVRegLiveness() const
Return true if this DAG supports VReg liveness and RegPressure.
MachineFunction & MF
Machine function.
HazardRecognizer - This determines whether or not an instruction can be issued this cycle,...
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
SlotIndexes pass.
SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late=false)
Insert the given machine instruction into the mapping.
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:281
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
int64_t getImm() const
Register getReg() const
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
virtual ScheduleHazardRecognizer * CreateTargetMIHazardRecognizer(const InstrItineraryData *, const ScheduleDAGMI *DAG) const
Allocate and return a hazard recognizer to use for this target when scheduling the machine instructio...
virtual MachineInstr * createPHIDestinationCopy(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, const DebugLoc &DL, Register Src, Register Dst) const
During PHI eleimination lets target to make necessary checks and insert the copy to the PHI destinati...
virtual void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg, unsigned SubIdx, const MachineInstr &Orig, const TargetRegisterInfo &TRI) const
Re-issue the specified 'original' instruction at the specific location targeting a new destination re...
virtual MachineInstr * createPHISourceCopy(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const
During PHI eleimination lets target to make necessary checks and insert the copy to the PHI destinati...
virtual bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const
For instructions with opcodes for which the M_REMATERIALIZABLE flag is set, this hook lets the target...
virtual MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const
This method commutes the operands of the given machine instruction MI.
virtual bool isGlobalMemoryObject(const MachineInstr *MI) const
Returns true if MI is an instruction we are unable to reason about (like a call or something with unm...
virtual bool expandPostRAPseudo(MachineInstr &MI) const
This function is called for all pseudo instructions that remain after register allocation.
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
bool hasSuperClassEq(const TargetRegisterClass *RC) const
Returns true if RC is a super-class of or equal to this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:194
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:174
self_iterator getIterator()
Definition ilist_node.h:130
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ FLAT_ADDRESS
Address space for flat memory.
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
@ PRIVATE_ADDRESS
Address space for private memory.
unsigned encodeFieldSaSdst(unsigned Encoded, unsigned SaSdst)
bool isPackedFP32Inst(unsigned Opc)
bool isInlinableLiteralBF16(int16_t Literal, bool HasInv2Pi)
const uint64_t RSRC_DATA_FORMAT
LLVM_READONLY int getBasicFromSDWAOp(uint16_t Opcode)
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
bool isInlinableLiteralFP16(int16_t Literal, bool HasInv2Pi)
LLVM_READONLY int getVOPe32(uint16_t Opcode)
bool getWMMAIsXDL(unsigned Opc)
unsigned mapWMMA2AddrTo3AddrOpcode(unsigned Opc)
bool isInlinableLiteralV2I16(uint32_t Literal)
bool isHi16Reg(MCRegister Reg, const MCRegisterInfo &MRI)
bool isInlinableLiteralV2BF16(uint32_t Literal)
LLVM_READONLY int getFlatScratchInstSVfromSS(uint16_t Opcode)
unsigned getNumFlatOffsetBits(const MCSubtargetInfo &ST)
For pre-GFX12 FLAT instructions the offset must be positive; MSB is ignored and forced to zero.
bool isGFX12Plus(const MCSubtargetInfo &STI)
bool isInlinableLiteralV2F16(uint32_t Literal)
LLVM_READONLY int getGlobalVaddrOp(uint16_t Opcode)
bool isValid32BitLiteral(uint64_t Val, bool IsFP64)
LLVM_READNONE bool isLegalDPALU_DPPControl(const MCSubtargetInfo &ST, unsigned DC)
bool getMAIIsGFX940XDL(unsigned Opc)
const uint64_t RSRC_ELEMENT_SIZE_SHIFT
LLVM_READONLY int getAddr64Inst(uint16_t Opcode)
bool isIntrinsicAlwaysUniform(unsigned IntrID)
LLVM_READONLY int getMFMAEarlyClobberOp(uint16_t Opcode)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
LLVM_READONLY const MIMGDimInfo * getMIMGDimInfoByEncoding(uint8_t DimEnc)
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
const uint64_t RSRC_TID_ENABLE
bool isIntrinsicSourceOfDivergence(unsigned IntrID)
constexpr bool isSISrcOperand(const MCOperandInfo &OpInfo)
Is this an AMDGPU specific source operand?
bool isGenericAtomic(unsigned Opc)
LLVM_READNONE bool isInlinableIntLiteral(int64_t Literal)
Is this literal inlinable, and not one of the values intended for floating point values.
bool isDPALU_DPP(const MCInstrDesc &OpDesc, const MCSubtargetInfo &ST)
LLVM_READONLY int getCommuteRev(uint16_t Opcode)
unsigned getAddrSizeMIMGOp(const MIMGBaseOpcodeInfo *BaseOpcode, const MIMGDimInfo *Dim, bool IsA16, bool IsG16Supported)
@ OPERAND_KIMM32
Operand with 32-bit immediate that uses the constant bus.
Definition SIDefines.h:231
@ OPERAND_REG_IMM_INT64
Definition SIDefines.h:202
@ OPERAND_REG_IMM_V2FP16
Definition SIDefines.h:209
@ OPERAND_REG_INLINE_C_FP64
Definition SIDefines.h:222
@ OPERAND_REG_INLINE_C_BF16
Definition SIDefines.h:219
@ OPERAND_REG_INLINE_C_V2BF16
Definition SIDefines.h:224
@ OPERAND_REG_IMM_V2INT16
Definition SIDefines.h:210
@ OPERAND_REG_IMM_BF16
Definition SIDefines.h:206
@ OPERAND_REG_IMM_INT32
Operands with register, 32-bit, or 64-bit immediate.
Definition SIDefines.h:201
@ OPERAND_REG_IMM_V2BF16
Definition SIDefines.h:208
@ OPERAND_REG_IMM_FP16
Definition SIDefines.h:207
@ OPERAND_REG_INLINE_C_INT64
Definition SIDefines.h:218
@ OPERAND_REG_INLINE_C_INT16
Operands with register or inline constant.
Definition SIDefines.h:216
@ OPERAND_REG_IMM_NOINLINE_V2FP16
Definition SIDefines.h:211
@ OPERAND_REG_IMM_FP64
Definition SIDefines.h:205
@ OPERAND_REG_INLINE_C_V2FP16
Definition SIDefines.h:225
@ OPERAND_REG_INLINE_AC_INT32
Operands with an AccVGPR register or inline constant.
Definition SIDefines.h:236
@ OPERAND_REG_INLINE_AC_FP32
Definition SIDefines.h:237
@ OPERAND_REG_IMM_V2INT32
Definition SIDefines.h:212
@ OPERAND_SDWA_VOPC_DST
Definition SIDefines.h:248
@ OPERAND_REG_IMM_FP32
Definition SIDefines.h:204
@ OPERAND_REG_INLINE_C_FP32
Definition SIDefines.h:221
@ OPERAND_REG_INLINE_C_INT32
Definition SIDefines.h:217
@ OPERAND_REG_INLINE_C_V2INT16
Definition SIDefines.h:223
@ OPERAND_INLINE_C_AV64_PSEUDO
Definition SIDefines.h:242
@ OPERAND_REG_IMM_V2FP32
Definition SIDefines.h:213
@ OPERAND_REG_INLINE_AC_FP64
Definition SIDefines.h:238
@ OPERAND_REG_INLINE_C_FP16
Definition SIDefines.h:220
@ OPERAND_REG_IMM_INT16
Definition SIDefines.h:203
@ OPERAND_INLINE_SPLIT_BARRIER_INT32
Definition SIDefines.h:228
@ TI_SCRATCH_RSRC_DWORD1
Definition AMDGPU.h:569
@ TI_SCRATCH_RSRC_DWORD3
Definition AMDGPU.h:571
@ TI_SCRATCH_RSRC_DWORD0
Definition AMDGPU.h:568
@ TI_SCRATCH_RSRC_DWORD2
Definition AMDGPU.h:570
@ TI_CONSTDATA_START
Definition AMDGPU.h:567
LLVM_READONLY int getCommuteOrig(uint16_t Opcode)
unsigned getRegBitWidth(const TargetRegisterClass &RC)
Get the size in bits of a register from the register class RC.
bool isGFX1250(const MCSubtargetInfo &STI)
int getMCOpcode(uint16_t Opcode, unsigned Gen)
bool supportsScaleOffset(const MCInstrInfo &MII, unsigned Opcode)
const uint64_t RSRC_INDEX_STRIDE_SHIFT
LLVM_READONLY const MIMGBaseOpcodeInfo * getMIMGBaseOpcodeInfo(unsigned BaseOpcode)
bool isInlinableLiteralI16(int32_t Literal, bool HasInv2Pi)
LLVM_READNONE constexpr bool isGraphics(CallingConv::ID CC)
bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi)
Is this literal inlinable.
LLVM_READONLY int getIfAddr64Inst(uint16_t Opcode)
Check if Opcode is an Addr64 opcode.
@ AMDGPU_CS
Used for Mesa/AMDPAL compute shaders.
@ AMDGPU_VS
Used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (vertex shader if tess...
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
@ AMDGPU_HS
Used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
@ AMDGPU_GS
Used for Mesa/AMDPAL geometry shaders.
@ AMDGPU_PS
Used for Mesa/AMDPAL pixel shaders.
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ AMDGPU_ES
Used for AMDPAL shader stage before geometry shader if geometry is in use.
@ AMDGPU_LS
Used for AMDPAL vertex shader if tessellation is in use.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ OPERAND_GENERIC_4
Definition MCInstrDesc.h:71
@ OPERAND_GENERIC_2
Definition MCInstrDesc.h:69
@ OPERAND_GENERIC_1
Definition MCInstrDesc.h:68
@ OPERAND_GENERIC_3
Definition MCInstrDesc.h:70
@ OPERAND_IMMEDIATE
Definition MCInstrDesc.h:61
@ OPERAND_GENERIC_0
Definition MCInstrDesc.h:67
@ OPERAND_GENERIC_5
Definition MCInstrDesc.h:72
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Define
Register definition.
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
Not(const Pred &P) -> Not< Pred >
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:318
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:262
@ Offset
Definition DWP.cpp:477
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
TargetInstrInfo::RegSubRegPair getRegSubRegPair(const MachineOperand &O)
Create RegSubRegPair from a register MachineOperand.
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:1705
constexpr uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition MathExtras.h:216
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:174
bool execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI, Register VReg, const MachineInstr &DefMI, const MachineInstr &UseMI)
Return false if EXEC is not changed between the def of VReg at DefMI and the use at UseMI.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2452
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:557
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:293
Op::Description Desc
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:186
TargetInstrInfo::RegSubRegPair getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg)
Return the SubReg component from REG_SEQUENCE.
static const MachineMemOperand::Flags MONoClobber
Mark the MMO of a uniform load if there are no potentially clobbering stores on any path from the sta...
Definition SIInstrInfo.h:44
constexpr bool has_single_bit(T Value) noexcept
Definition bit.h:147
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:1712
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:342
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition MathExtras.h:159
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
MachineInstr * getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, MachineRegisterInfo &MRI)
Return the defining instruction for a given reg:subreg pair skipping copy like instructions and subre...
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition MathExtras.h:164
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:548
static const MachineMemOperand::Flags MOCooperative
Mark the MMO of cooperative load/store atomics.
Definition SIInstrInfo.h:52
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:405
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
unsigned getUndefRegState(bool B)
@ Xor
Bitwise or logical XOR of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
unsigned getKillRegState(bool B)
bool isTargetSpecificOpcode(unsigned Opcode)
Check whether the given Opcode is a target-specific opcode.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned DefaultMemoryClusterDWordsLimit
Definition SIInstrInfo.h:40
constexpr unsigned BitWidth
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:257
static const MachineMemOperand::Flags MOLastUse
Mark the MMO of a load as the last use.
Definition SIInstrInfo.h:48
constexpr T reverseBits(T Val)
Reverse the bits in Val.
Definition MathExtras.h:127
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1877
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:583
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:86
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
InstructionUniformity
Enum describing how instructions behave with respect to uniformity and divergence,...
Definition Uniformity.h:18
@ AlwaysUniform
The result values are always uniform.
Definition Uniformity.h:23
@ NeverUniform
The result values can never be assumed to be uniform.
Definition Uniformity.h:26
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
GenericCycleInfo< MachineSSAContext > MachineCycleInfo
MachineCycleInfo::CycleT MachineCycle
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
bool execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI, Register VReg, const MachineInstr &DefMI)
Return false if EXEC is not changed between the def of VReg at DefMI and all its uses.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
#define N
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:219
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:85
SparseBitVector AliveBlocks
AliveBlocks - Set of blocks in which this value is alive completely through.
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
Utility to store machine instructions worklist.
Definition SIInstrInfo.h:56
MachineInstr * top() const
Definition SIInstrInfo.h:61
bool isDeferred(MachineInstr *MI)
SetVector< MachineInstr * > & getDeferredList()
Definition SIInstrInfo.h:80
void insert(MachineInstr *MI)
A pair composed of a register and a sub-register index.