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 "AMDGPUSubtarget.h"
17 #include "GCNHazardRecognizer.h"
18 #include "SIDefines.h"
19 #include "SIMachineFunctionInfo.h"
20 #include "SIRegisterInfo.h"
21 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
22 #include "Utils/AMDGPUBaseInfo.h"
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Analysis/AliasAnalysis.h"
29 #include "llvm/Analysis/MemoryLocation.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/CodeGen/MachineBasicBlock.h"
32 #include "llvm/CodeGen/MachineDominators.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineFunction.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineInstrBundle.h"
38 #include "llvm/CodeGen/MachineMemOperand.h"
39 #include "llvm/CodeGen/MachineOperand.h"
40 #include "llvm/CodeGen/MachineRegisterInfo.h"
41 #include "llvm/CodeGen/RegisterScavenging.h"
42 #include "llvm/CodeGen/ScheduleDAG.h"
43 #include "llvm/CodeGen/SelectionDAGNodes.h"
44 #include "llvm/CodeGen/TargetOpcodes.h"
45 #include "llvm/CodeGen/TargetRegisterInfo.h"
46 #include "llvm/IR/DebugLoc.h"
47 #include "llvm/IR/DiagnosticInfo.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/InlineAsm.h"
50 #include "llvm/IR/LLVMContext.h"
51 #include "llvm/MC/MCInstrDesc.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/CommandLine.h"
54 #include "llvm/Support/Compiler.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/MachineValueType.h"
57 #include "llvm/Support/MathExtras.h"
58 #include "llvm/Target/TargetMachine.h"
59 #include <cassert>
60 #include <cstdint>
61 #include <iterator>
62 #include <utility>
63
64 using namespace llvm;
65
66 #define GET_INSTRINFO_CTOR_DTOR
67 #include "AMDGPUGenInstrInfo.inc"
68
69 namespace llvm {
70 namespace AMDGPU {
71 #define GET_D16ImageDimIntrinsics_IMPL
72 #define GET_ImageDimIntrinsicTable_IMPL
73 #define GET_RsrcIntrinsics_IMPL
74 #include "AMDGPUGenSearchableTables.inc"
75 }
76 }
77
78
79 // Must be at least 4 to be able to branch over minimum unconditional branch
80 // code. This is only for making it possible to write reasonably small tests for
81 // long branches.
82 static cl::opt<unsigned>
83 BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16),
84 cl::desc("Restrict range of branch instructions (DEBUG)"));
85
SIInstrInfo(const GCNSubtarget & ST)86 SIInstrInfo::SIInstrInfo(const GCNSubtarget &ST)
87 : AMDGPUGenInstrInfo(AMDGPU::ADJCALLSTACKUP, AMDGPU::ADJCALLSTACKDOWN),
88 RI(ST), ST(ST) {
89 SchedModel.init(&ST);
90 }
91
92 //===----------------------------------------------------------------------===//
93 // TargetInstrInfo callbacks
94 //===----------------------------------------------------------------------===//
95
getNumOperandsNoGlue(SDNode * Node)96 static unsigned getNumOperandsNoGlue(SDNode *Node) {
97 unsigned N = Node->getNumOperands();
98 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
99 --N;
100 return N;
101 }
102
103 /// Returns true if both nodes have the same value for the given
104 /// operand \p Op, or if both nodes do not have this operand.
nodesHaveSameOperandValue(SDNode * N0,SDNode * N1,unsigned OpName)105 static bool nodesHaveSameOperandValue(SDNode *N0, SDNode* N1, unsigned OpName) {
106 unsigned Opc0 = N0->getMachineOpcode();
107 unsigned Opc1 = N1->getMachineOpcode();
108
109 int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName);
110 int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName);
111
112 if (Op0Idx == -1 && Op1Idx == -1)
113 return true;
114
115
116 if ((Op0Idx == -1 && Op1Idx != -1) ||
117 (Op1Idx == -1 && Op0Idx != -1))
118 return false;
119
120 // getNamedOperandIdx returns the index for the MachineInstr's operands,
121 // which includes the result as the first operand. We are indexing into the
122 // MachineSDNode's operands, so we need to skip the result operand to get
123 // the real index.
124 --Op0Idx;
125 --Op1Idx;
126
127 return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx);
128 }
129
isReallyTriviallyReMaterializable(const MachineInstr & MI,AliasAnalysis * AA) const130 bool SIInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
131 AliasAnalysis *AA) const {
132 // TODO: The generic check fails for VALU instructions that should be
133 // rematerializable due to implicit reads of exec. We really want all of the
134 // generic logic for this except for this.
135 switch (MI.getOpcode()) {
136 case AMDGPU::V_MOV_B32_e32:
137 case AMDGPU::V_MOV_B32_e64:
138 case AMDGPU::V_MOV_B64_PSEUDO:
139 // No implicit operands.
140 return MI.getNumOperands() == MI.getDesc().getNumOperands();
141 default:
142 return false;
143 }
144 }
145
areLoadsFromSameBasePtr(SDNode * Load0,SDNode * Load1,int64_t & Offset0,int64_t & Offset1) const146 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1,
147 int64_t &Offset0,
148 int64_t &Offset1) const {
149 if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode())
150 return false;
151
152 unsigned Opc0 = Load0->getMachineOpcode();
153 unsigned Opc1 = Load1->getMachineOpcode();
154
155 // Make sure both are actually loads.
156 if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad())
157 return false;
158
159 if (isDS(Opc0) && isDS(Opc1)) {
160
161 // FIXME: Handle this case:
162 if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1))
163 return false;
164
165 // Check base reg.
166 if (Load0->getOperand(0) != Load1->getOperand(0))
167 return false;
168
169 // Skip read2 / write2 variants for simplicity.
170 // TODO: We should report true if the used offsets are adjacent (excluded
171 // st64 versions).
172 int Offset0Idx = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
173 int Offset1Idx = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
174 if (Offset0Idx == -1 || Offset1Idx == -1)
175 return false;
176
177 // XXX - be careful of datalesss loads
178 // getNamedOperandIdx returns the index for MachineInstrs. Since they
179 // include the output in the operand list, but SDNodes don't, we need to
180 // subtract the index by one.
181 Offset0Idx -= get(Opc0).NumDefs;
182 Offset1Idx -= get(Opc1).NumDefs;
183 Offset0 = cast<ConstantSDNode>(Load0->getOperand(Offset0Idx))->getZExtValue();
184 Offset1 = cast<ConstantSDNode>(Load1->getOperand(Offset1Idx))->getZExtValue();
185 return true;
186 }
187
188 if (isSMRD(Opc0) && isSMRD(Opc1)) {
189 // Skip time and cache invalidation instructions.
190 if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::sbase) == -1 ||
191 AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::sbase) == -1)
192 return false;
193
194 assert(getNumOperandsNoGlue(Load0) == getNumOperandsNoGlue(Load1));
195
196 // Check base reg.
197 if (Load0->getOperand(0) != Load1->getOperand(0))
198 return false;
199
200 const ConstantSDNode *Load0Offset =
201 dyn_cast<ConstantSDNode>(Load0->getOperand(1));
202 const ConstantSDNode *Load1Offset =
203 dyn_cast<ConstantSDNode>(Load1->getOperand(1));
204
205 if (!Load0Offset || !Load1Offset)
206 return false;
207
208 Offset0 = Load0Offset->getZExtValue();
209 Offset1 = Load1Offset->getZExtValue();
210 return true;
211 }
212
213 // MUBUF and MTBUF can access the same addresses.
214 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) {
215
216 // MUBUF and MTBUF have vaddr at different indices.
217 if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) ||
218 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) ||
219 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc))
220 return false;
221
222 int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
223 int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
224
225 if (OffIdx0 == -1 || OffIdx1 == -1)
226 return false;
227
228 // getNamedOperandIdx returns the index for MachineInstrs. Since they
229 // include the output in the operand list, but SDNodes don't, we need to
230 // subtract the index by one.
231 OffIdx0 -= get(Opc0).NumDefs;
232 OffIdx1 -= get(Opc1).NumDefs;
233
234 SDValue Off0 = Load0->getOperand(OffIdx0);
235 SDValue Off1 = Load1->getOperand(OffIdx1);
236
237 // The offset might be a FrameIndexSDNode.
238 if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1))
239 return false;
240
241 Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue();
242 Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue();
243 return true;
244 }
245
246 return false;
247 }
248
isStride64(unsigned Opc)249 static bool isStride64(unsigned Opc) {
250 switch (Opc) {
251 case AMDGPU::DS_READ2ST64_B32:
252 case AMDGPU::DS_READ2ST64_B64:
253 case AMDGPU::DS_WRITE2ST64_B32:
254 case AMDGPU::DS_WRITE2ST64_B64:
255 return true;
256 default:
257 return false;
258 }
259 }
260
getMemOperandWithOffset(const MachineInstr & LdSt,const MachineOperand * & BaseOp,int64_t & Offset,const TargetRegisterInfo * TRI) const261 bool SIInstrInfo::getMemOperandWithOffset(const MachineInstr &LdSt,
262 const MachineOperand *&BaseOp,
263 int64_t &Offset,
264 const TargetRegisterInfo *TRI) const {
265 if (!LdSt.mayLoadOrStore())
266 return false;
267
268 unsigned Opc = LdSt.getOpcode();
269
270 if (isDS(LdSt)) {
271 const MachineOperand *OffsetImm =
272 getNamedOperand(LdSt, AMDGPU::OpName::offset);
273 if (OffsetImm) {
274 // Normal, single offset LDS instruction.
275 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr);
276 // TODO: ds_consume/ds_append use M0 for the base address. Is it safe to
277 // report that here?
278 if (!BaseOp || !BaseOp->isReg())
279 return false;
280
281 Offset = OffsetImm->getImm();
282
283 return true;
284 }
285
286 // The 2 offset instructions use offset0 and offset1 instead. We can treat
287 // these as a load with a single offset if the 2 offsets are consecutive. We
288 // will use this for some partially aligned loads.
289 const MachineOperand *Offset0Imm =
290 getNamedOperand(LdSt, AMDGPU::OpName::offset0);
291 const MachineOperand *Offset1Imm =
292 getNamedOperand(LdSt, AMDGPU::OpName::offset1);
293
294 uint8_t Offset0 = Offset0Imm->getImm();
295 uint8_t Offset1 = Offset1Imm->getImm();
296
297 if (Offset1 > Offset0 && Offset1 - Offset0 == 1) {
298 // Each of these offsets is in element sized units, so we need to convert
299 // to bytes of the individual reads.
300
301 unsigned EltSize;
302 if (LdSt.mayLoad())
303 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, 0)) / 16;
304 else {
305 assert(LdSt.mayStore());
306 int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
307 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, Data0Idx)) / 8;
308 }
309
310 if (isStride64(Opc))
311 EltSize *= 64;
312
313 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr);
314 if (!BaseOp->isReg())
315 return false;
316
317 Offset = EltSize * Offset0;
318
319 return true;
320 }
321
322 return false;
323 }
324
325 if (isMUBUF(LdSt) || isMTBUF(LdSt)) {
326 const MachineOperand *SOffset = getNamedOperand(LdSt, AMDGPU::OpName::soffset);
327 if (SOffset && SOffset->isReg()) {
328 // We can only handle this if it's a stack access, as any other resource
329 // would require reporting multiple base registers.
330 const MachineOperand *AddrReg = getNamedOperand(LdSt, AMDGPU::OpName::vaddr);
331 if (AddrReg && !AddrReg->isFI())
332 return false;
333
334 const MachineOperand *RSrc = getNamedOperand(LdSt, AMDGPU::OpName::srsrc);
335 const SIMachineFunctionInfo *MFI
336 = LdSt.getParent()->getParent()->getInfo<SIMachineFunctionInfo>();
337 if (RSrc->getReg() != MFI->getScratchRSrcReg())
338 return false;
339
340 const MachineOperand *OffsetImm =
341 getNamedOperand(LdSt, AMDGPU::OpName::offset);
342 BaseOp = SOffset;
343 Offset = OffsetImm->getImm();
344 return true;
345 }
346
347 const MachineOperand *AddrReg = getNamedOperand(LdSt, AMDGPU::OpName::vaddr);
348 if (!AddrReg)
349 return false;
350
351 const MachineOperand *OffsetImm =
352 getNamedOperand(LdSt, AMDGPU::OpName::offset);
353 BaseOp = AddrReg;
354 Offset = OffsetImm->getImm();
355 if (SOffset) // soffset can be an inline immediate.
356 Offset += SOffset->getImm();
357
358 if (!BaseOp->isReg())
359 return false;
360
361 return true;
362 }
363
364 if (isSMRD(LdSt)) {
365 const MachineOperand *OffsetImm =
366 getNamedOperand(LdSt, AMDGPU::OpName::offset);
367 if (!OffsetImm)
368 return false;
369
370 const MachineOperand *SBaseReg = getNamedOperand(LdSt, AMDGPU::OpName::sbase);
371 BaseOp = SBaseReg;
372 Offset = OffsetImm->getImm();
373 if (!BaseOp->isReg())
374 return false;
375
376 return true;
377 }
378
379 if (isFLAT(LdSt)) {
380 const MachineOperand *VAddr = getNamedOperand(LdSt, AMDGPU::OpName::vaddr);
381 if (VAddr) {
382 // Can't analyze 2 offsets.
383 if (getNamedOperand(LdSt, AMDGPU::OpName::saddr))
384 return false;
385
386 BaseOp = VAddr;
387 } else {
388 // scratch instructions have either vaddr or saddr.
389 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::saddr);
390 }
391
392 Offset = getNamedOperand(LdSt, AMDGPU::OpName::offset)->getImm();
393 if (!BaseOp->isReg())
394 return false;
395 return true;
396 }
397
398 return false;
399 }
400
memOpsHaveSameBasePtr(const MachineInstr & MI1,const MachineOperand & BaseOp1,const MachineInstr & MI2,const MachineOperand & BaseOp2)401 static bool memOpsHaveSameBasePtr(const MachineInstr &MI1,
402 const MachineOperand &BaseOp1,
403 const MachineInstr &MI2,
404 const MachineOperand &BaseOp2) {
405 // Support only base operands with base registers.
406 // Note: this could be extended to support FI operands.
407 if (!BaseOp1.isReg() || !BaseOp2.isReg())
408 return false;
409
410 if (BaseOp1.isIdenticalTo(BaseOp2))
411 return true;
412
413 if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand())
414 return false;
415
416 auto MO1 = *MI1.memoperands_begin();
417 auto MO2 = *MI2.memoperands_begin();
418 if (MO1->getAddrSpace() != MO2->getAddrSpace())
419 return false;
420
421 auto Base1 = MO1->getValue();
422 auto Base2 = MO2->getValue();
423 if (!Base1 || !Base2)
424 return false;
425 const MachineFunction &MF = *MI1.getParent()->getParent();
426 const DataLayout &DL = MF.getFunction().getParent()->getDataLayout();
427 Base1 = GetUnderlyingObject(Base1, DL);
428 Base2 = GetUnderlyingObject(Base2, DL);
429
430 if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2))
431 return false;
432
433 return Base1 == Base2;
434 }
435
shouldClusterMemOps(const MachineOperand & BaseOp1,const MachineOperand & BaseOp2,unsigned NumLoads) const436 bool SIInstrInfo::shouldClusterMemOps(const MachineOperand &BaseOp1,
437 const MachineOperand &BaseOp2,
438 unsigned NumLoads) const {
439 const MachineInstr &FirstLdSt = *BaseOp1.getParent();
440 const MachineInstr &SecondLdSt = *BaseOp2.getParent();
441
442 if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOp1, SecondLdSt, BaseOp2))
443 return false;
444
445 const MachineOperand *FirstDst = nullptr;
446 const MachineOperand *SecondDst = nullptr;
447
448 if ((isMUBUF(FirstLdSt) && isMUBUF(SecondLdSt)) ||
449 (isMTBUF(FirstLdSt) && isMTBUF(SecondLdSt)) ||
450 (isFLAT(FirstLdSt) && isFLAT(SecondLdSt))) {
451 const unsigned MaxGlobalLoadCluster = 6;
452 if (NumLoads > MaxGlobalLoadCluster)
453 return false;
454
455 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdata);
456 if (!FirstDst)
457 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdst);
458 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdata);
459 if (!SecondDst)
460 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdst);
461 } else if (isSMRD(FirstLdSt) && isSMRD(SecondLdSt)) {
462 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::sdst);
463 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::sdst);
464 } else if (isDS(FirstLdSt) && isDS(SecondLdSt)) {
465 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdst);
466 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdst);
467 }
468
469 if (!FirstDst || !SecondDst)
470 return false;
471
472 // Try to limit clustering based on the total number of bytes loaded
473 // rather than the number of instructions. This is done to help reduce
474 // register pressure. The method used is somewhat inexact, though,
475 // because it assumes that all loads in the cluster will load the
476 // same number of bytes as FirstLdSt.
477
478 // The unit of this value is bytes.
479 // FIXME: This needs finer tuning.
480 unsigned LoadClusterThreshold = 16;
481
482 const MachineRegisterInfo &MRI =
483 FirstLdSt.getParent()->getParent()->getRegInfo();
484
485 const Register Reg = FirstDst->getReg();
486
487 const TargetRegisterClass *DstRC = Register::isVirtualRegister(Reg)
488 ? MRI.getRegClass(Reg)
489 : RI.getPhysRegClass(Reg);
490
491 return (NumLoads * (RI.getRegSizeInBits(*DstRC) / 8)) <= LoadClusterThreshold;
492 }
493
494 // FIXME: This behaves strangely. If, for example, you have 32 load + stores,
495 // the first 16 loads will be interleaved with the stores, and the next 16 will
496 // be clustered as expected. It should really split into 2 16 store batches.
497 //
498 // Loads are clustered until this returns false, rather than trying to schedule
499 // groups of stores. This also means we have to deal with saying different
500 // address space loads should be clustered, and ones which might cause bank
501 // conflicts.
502 //
503 // This might be deprecated so it might not be worth that much effort to fix.
shouldScheduleLoadsNear(SDNode * Load0,SDNode * Load1,int64_t Offset0,int64_t Offset1,unsigned NumLoads) const504 bool SIInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1,
505 int64_t Offset0, int64_t Offset1,
506 unsigned NumLoads) const {
507 assert(Offset1 > Offset0 &&
508 "Second offset should be larger than first offset!");
509 // If we have less than 16 loads in a row, and the offsets are within 64
510 // bytes, then schedule together.
511
512 // A cacheline is 64 bytes (for global memory).
513 return (NumLoads <= 16 && (Offset1 - Offset0) < 64);
514 }
515
reportIllegalCopy(const SIInstrInfo * TII,MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const DebugLoc & DL,MCRegister DestReg,MCRegister SrcReg,bool KillSrc)516 static void reportIllegalCopy(const SIInstrInfo *TII, MachineBasicBlock &MBB,
517 MachineBasicBlock::iterator MI,
518 const DebugLoc &DL, MCRegister DestReg,
519 MCRegister SrcReg, bool KillSrc) {
520 MachineFunction *MF = MBB.getParent();
521 DiagnosticInfoUnsupported IllegalCopy(MF->getFunction(),
522 "illegal SGPR to VGPR copy",
523 DL, DS_Error);
524 LLVMContext &C = MF->getFunction().getContext();
525 C.diagnose(IllegalCopy);
526
527 BuildMI(MBB, MI, DL, TII->get(AMDGPU::SI_ILLEGAL_COPY), DestReg)
528 .addReg(SrcReg, getKillRegState(KillSrc));
529 }
530
copyPhysReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const DebugLoc & DL,MCRegister DestReg,MCRegister SrcReg,bool KillSrc) const531 void SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
532 MachineBasicBlock::iterator MI,
533 const DebugLoc &DL, MCRegister DestReg,
534 MCRegister SrcReg, bool KillSrc) const {
535 const TargetRegisterClass *RC = RI.getPhysRegClass(DestReg);
536
537 if (RC == &AMDGPU::VGPR_32RegClass) {
538 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
539 AMDGPU::SReg_32RegClass.contains(SrcReg) ||
540 AMDGPU::AGPR_32RegClass.contains(SrcReg));
541 unsigned Opc = AMDGPU::AGPR_32RegClass.contains(SrcReg) ?
542 AMDGPU::V_ACCVGPR_READ_B32 : AMDGPU::V_MOV_B32_e32;
543 BuildMI(MBB, MI, DL, get(Opc), DestReg)
544 .addReg(SrcReg, getKillRegState(KillSrc));
545 return;
546 }
547
548 if (RC == &AMDGPU::SReg_32_XM0RegClass ||
549 RC == &AMDGPU::SReg_32RegClass) {
550 if (SrcReg == AMDGPU::SCC) {
551 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B32), DestReg)
552 .addImm(1)
553 .addImm(0);
554 return;
555 }
556
557 if (DestReg == AMDGPU::VCC_LO) {
558 if (AMDGPU::SReg_32RegClass.contains(SrcReg)) {
559 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), AMDGPU::VCC_LO)
560 .addReg(SrcReg, getKillRegState(KillSrc));
561 } else {
562 // FIXME: Hack until VReg_1 removed.
563 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
564 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32))
565 .addImm(0)
566 .addReg(SrcReg, getKillRegState(KillSrc));
567 }
568
569 return;
570 }
571
572 if (!AMDGPU::SReg_32RegClass.contains(SrcReg)) {
573 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
574 return;
575 }
576
577 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
578 .addReg(SrcReg, getKillRegState(KillSrc));
579 return;
580 }
581
582 if (RC == &AMDGPU::SReg_64RegClass) {
583 if (DestReg == AMDGPU::VCC) {
584 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) {
585 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC)
586 .addReg(SrcReg, getKillRegState(KillSrc));
587 } else {
588 // FIXME: Hack until VReg_1 removed.
589 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
590 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32))
591 .addImm(0)
592 .addReg(SrcReg, getKillRegState(KillSrc));
593 }
594
595 return;
596 }
597
598 if (!AMDGPU::SReg_64RegClass.contains(SrcReg)) {
599 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
600 return;
601 }
602
603 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
604 .addReg(SrcReg, getKillRegState(KillSrc));
605 return;
606 }
607
608 if (DestReg == AMDGPU::SCC) {
609 assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
610 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U32))
611 .addReg(SrcReg, getKillRegState(KillSrc))
612 .addImm(0);
613 return;
614 }
615
616 if (RC == &AMDGPU::AGPR_32RegClass) {
617 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
618 AMDGPU::SReg_32RegClass.contains(SrcReg) ||
619 AMDGPU::AGPR_32RegClass.contains(SrcReg));
620 if (!AMDGPU::VGPR_32RegClass.contains(SrcReg)) {
621 // First try to find defining accvgpr_write to avoid temporary registers.
622 for (auto Def = MI, E = MBB.begin(); Def != E; ) {
623 --Def;
624 if (!Def->definesRegister(SrcReg, &RI))
625 continue;
626 if (Def->getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32)
627 break;
628
629 MachineOperand &DefOp = Def->getOperand(1);
630 assert(DefOp.isReg() || DefOp.isImm());
631
632 if (DefOp.isReg()) {
633 // Check that register source operand if not clobbered before MI.
634 // Immediate operands are always safe to propagate.
635 bool SafeToPropagate = true;
636 for (auto I = Def; I != MI && SafeToPropagate; ++I)
637 if (I->modifiesRegister(DefOp.getReg(), &RI))
638 SafeToPropagate = false;
639
640 if (!SafeToPropagate)
641 break;
642
643 DefOp.setIsKill(false);
644 }
645
646 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32), DestReg)
647 .add(DefOp);
648 return;
649 }
650
651 RegScavenger RS;
652 RS.enterBasicBlock(MBB);
653 RS.forward(MI);
654
655 // Ideally we want to have three registers for a long reg_sequence copy
656 // to hide 2 waitstates between v_mov_b32 and accvgpr_write.
657 unsigned MaxVGPRs = RI.getRegPressureLimit(&AMDGPU::VGPR_32RegClass,
658 *MBB.getParent());
659
660 // Registers in the sequence are allocated contiguously so we can just
661 // use register number to pick one of three round-robin temps.
662 unsigned RegNo = DestReg % 3;
663 unsigned Tmp = RS.scavengeRegister(&AMDGPU::VGPR_32RegClass, 0);
664 if (!Tmp)
665 report_fatal_error("Cannot scavenge VGPR to copy to AGPR");
666 RS.setRegUsed(Tmp);
667 // Only loop through if there are any free registers left, otherwise
668 // scavenger may report a fatal error without emergency spill slot
669 // or spill with the slot.
670 while (RegNo-- && RS.FindUnusedReg(&AMDGPU::VGPR_32RegClass)) {
671 unsigned Tmp2 = RS.scavengeRegister(&AMDGPU::VGPR_32RegClass, 0);
672 if (!Tmp2 || RI.getHWRegIndex(Tmp2) >= MaxVGPRs)
673 break;
674 Tmp = Tmp2;
675 RS.setRegUsed(Tmp);
676 }
677 copyPhysReg(MBB, MI, DL, Tmp, SrcReg, KillSrc);
678 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32), DestReg)
679 .addReg(Tmp, RegState::Kill);
680 return;
681 }
682
683 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32), DestReg)
684 .addReg(SrcReg, getKillRegState(KillSrc));
685 return;
686 }
687
688 unsigned EltSize = 4;
689 unsigned Opcode = AMDGPU::V_MOV_B32_e32;
690 if (RI.isSGPRClass(RC)) {
691 // TODO: Copy vec3/vec5 with s_mov_b64s then final s_mov_b32.
692 if (!(RI.getRegSizeInBits(*RC) % 64)) {
693 Opcode = AMDGPU::S_MOV_B64;
694 EltSize = 8;
695 } else {
696 Opcode = AMDGPU::S_MOV_B32;
697 EltSize = 4;
698 }
699
700 if (!RI.isSGPRClass(RI.getPhysRegClass(SrcReg))) {
701 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
702 return;
703 }
704 } else if (RI.hasAGPRs(RC)) {
705 Opcode = RI.hasVGPRs(RI.getPhysRegClass(SrcReg)) ?
706 AMDGPU::V_ACCVGPR_WRITE_B32 : AMDGPU::COPY;
707 } else if (RI.hasVGPRs(RC) && RI.hasAGPRs(RI.getPhysRegClass(SrcReg))) {
708 Opcode = AMDGPU::V_ACCVGPR_READ_B32;
709 }
710
711 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RC, EltSize);
712 bool Forward = RI.getHWRegIndex(DestReg) <= RI.getHWRegIndex(SrcReg);
713
714 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) {
715 unsigned SubIdx;
716 if (Forward)
717 SubIdx = SubIndices[Idx];
718 else
719 SubIdx = SubIndices[SubIndices.size() - Idx - 1];
720
721 if (Opcode == TargetOpcode::COPY) {
722 copyPhysReg(MBB, MI, DL, RI.getSubReg(DestReg, SubIdx),
723 RI.getSubReg(SrcReg, SubIdx), KillSrc);
724 continue;
725 }
726
727 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL,
728 get(Opcode), RI.getSubReg(DestReg, SubIdx));
729
730 Builder.addReg(RI.getSubReg(SrcReg, SubIdx));
731
732 if (Idx == 0)
733 Builder.addReg(DestReg, RegState::Define | RegState::Implicit);
734
735 bool UseKill = KillSrc && Idx == SubIndices.size() - 1;
736 Builder.addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit);
737 }
738 }
739
commuteOpcode(unsigned Opcode) const740 int SIInstrInfo::commuteOpcode(unsigned Opcode) const {
741 int NewOpc;
742
743 // Try to map original to commuted opcode
744 NewOpc = AMDGPU::getCommuteRev(Opcode);
745 if (NewOpc != -1)
746 // Check if the commuted (REV) opcode exists on the target.
747 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
748
749 // Try to map commuted to original opcode
750 NewOpc = AMDGPU::getCommuteOrig(Opcode);
751 if (NewOpc != -1)
752 // Check if the original (non-REV) opcode exists on the target.
753 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
754
755 return Opcode;
756 }
757
materializeImmediate(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const DebugLoc & DL,unsigned DestReg,int64_t Value) const758 void SIInstrInfo::materializeImmediate(MachineBasicBlock &MBB,
759 MachineBasicBlock::iterator MI,
760 const DebugLoc &DL, unsigned DestReg,
761 int64_t Value) const {
762 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
763 const TargetRegisterClass *RegClass = MRI.getRegClass(DestReg);
764 if (RegClass == &AMDGPU::SReg_32RegClass ||
765 RegClass == &AMDGPU::SGPR_32RegClass ||
766 RegClass == &AMDGPU::SReg_32_XM0RegClass ||
767 RegClass == &AMDGPU::SReg_32_XM0_XEXECRegClass) {
768 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
769 .addImm(Value);
770 return;
771 }
772
773 if (RegClass == &AMDGPU::SReg_64RegClass ||
774 RegClass == &AMDGPU::SGPR_64RegClass ||
775 RegClass == &AMDGPU::SReg_64_XEXECRegClass) {
776 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
777 .addImm(Value);
778 return;
779 }
780
781 if (RegClass == &AMDGPU::VGPR_32RegClass) {
782 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg)
783 .addImm(Value);
784 return;
785 }
786 if (RegClass == &AMDGPU::VReg_64RegClass) {
787 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), DestReg)
788 .addImm(Value);
789 return;
790 }
791
792 unsigned EltSize = 4;
793 unsigned Opcode = AMDGPU::V_MOV_B32_e32;
794 if (RI.isSGPRClass(RegClass)) {
795 if (RI.getRegSizeInBits(*RegClass) > 32) {
796 Opcode = AMDGPU::S_MOV_B64;
797 EltSize = 8;
798 } else {
799 Opcode = AMDGPU::S_MOV_B32;
800 EltSize = 4;
801 }
802 }
803
804 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RegClass, EltSize);
805 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) {
806 int64_t IdxValue = Idx == 0 ? Value : 0;
807
808 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL,
809 get(Opcode), RI.getSubReg(DestReg, Idx));
810 Builder.addImm(IdxValue);
811 }
812 }
813
814 const TargetRegisterClass *
getPreferredSelectRegClass(unsigned Size) const815 SIInstrInfo::getPreferredSelectRegClass(unsigned Size) const {
816 return &AMDGPU::VGPR_32RegClass;
817 }
818
insertVectorSelect(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,unsigned DstReg,ArrayRef<MachineOperand> Cond,unsigned TrueReg,unsigned FalseReg) const819 void SIInstrInfo::insertVectorSelect(MachineBasicBlock &MBB,
820 MachineBasicBlock::iterator I,
821 const DebugLoc &DL, unsigned DstReg,
822 ArrayRef<MachineOperand> Cond,
823 unsigned TrueReg,
824 unsigned FalseReg) const {
825 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
826 MachineFunction *MF = MBB.getParent();
827 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
828 const TargetRegisterClass *BoolXExecRC =
829 RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
830 assert(MRI.getRegClass(DstReg) == &AMDGPU::VGPR_32RegClass &&
831 "Not a VGPR32 reg");
832
833 if (Cond.size() == 1) {
834 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
835 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
836 .add(Cond[0]);
837 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
838 .addImm(0)
839 .addReg(FalseReg)
840 .addImm(0)
841 .addReg(TrueReg)
842 .addReg(SReg);
843 } else if (Cond.size() == 2) {
844 assert(Cond[0].isImm() && "Cond[0] is not an immediate");
845 switch (Cond[0].getImm()) {
846 case SIInstrInfo::SCC_TRUE: {
847 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
848 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
849 : AMDGPU::S_CSELECT_B64), SReg)
850 .addImm(1)
851 .addImm(0);
852 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
853 .addImm(0)
854 .addReg(FalseReg)
855 .addImm(0)
856 .addReg(TrueReg)
857 .addReg(SReg);
858 break;
859 }
860 case SIInstrInfo::SCC_FALSE: {
861 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
862 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
863 : AMDGPU::S_CSELECT_B64), SReg)
864 .addImm(0)
865 .addImm(1);
866 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
867 .addImm(0)
868 .addReg(FalseReg)
869 .addImm(0)
870 .addReg(TrueReg)
871 .addReg(SReg);
872 break;
873 }
874 case SIInstrInfo::VCCNZ: {
875 MachineOperand RegOp = Cond[1];
876 RegOp.setImplicit(false);
877 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
878 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
879 .add(RegOp);
880 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
881 .addImm(0)
882 .addReg(FalseReg)
883 .addImm(0)
884 .addReg(TrueReg)
885 .addReg(SReg);
886 break;
887 }
888 case SIInstrInfo::VCCZ: {
889 MachineOperand RegOp = Cond[1];
890 RegOp.setImplicit(false);
891 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
892 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
893 .add(RegOp);
894 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
895 .addImm(0)
896 .addReg(TrueReg)
897 .addImm(0)
898 .addReg(FalseReg)
899 .addReg(SReg);
900 break;
901 }
902 case SIInstrInfo::EXECNZ: {
903 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
904 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC());
905 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32
906 : AMDGPU::S_OR_SAVEEXEC_B64), SReg2)
907 .addImm(0);
908 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
909 : AMDGPU::S_CSELECT_B64), SReg)
910 .addImm(1)
911 .addImm(0);
912 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
913 .addImm(0)
914 .addReg(FalseReg)
915 .addImm(0)
916 .addReg(TrueReg)
917 .addReg(SReg);
918 break;
919 }
920 case SIInstrInfo::EXECZ: {
921 Register SReg = MRI.createVirtualRegister(BoolXExecRC);
922 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC());
923 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32
924 : AMDGPU::S_OR_SAVEEXEC_B64), SReg2)
925 .addImm(0);
926 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
927 : AMDGPU::S_CSELECT_B64), SReg)
928 .addImm(0)
929 .addImm(1);
930 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
931 .addImm(0)
932 .addReg(FalseReg)
933 .addImm(0)
934 .addReg(TrueReg)
935 .addReg(SReg);
936 llvm_unreachable("Unhandled branch predicate EXECZ");
937 break;
938 }
939 default:
940 llvm_unreachable("invalid branch predicate");
941 }
942 } else {
943 llvm_unreachable("Can only handle Cond size 1 or 2");
944 }
945 }
946
insertEQ(MachineBasicBlock * MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,unsigned SrcReg,int Value) const947 unsigned SIInstrInfo::insertEQ(MachineBasicBlock *MBB,
948 MachineBasicBlock::iterator I,
949 const DebugLoc &DL,
950 unsigned SrcReg, int Value) const {
951 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
952 Register Reg = MRI.createVirtualRegister(RI.getBoolRC());
953 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_EQ_I32_e64), Reg)
954 .addImm(Value)
955 .addReg(SrcReg);
956
957 return Reg;
958 }
959
insertNE(MachineBasicBlock * MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,unsigned SrcReg,int Value) const960 unsigned SIInstrInfo::insertNE(MachineBasicBlock *MBB,
961 MachineBasicBlock::iterator I,
962 const DebugLoc &DL,
963 unsigned SrcReg, int Value) const {
964 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
965 Register Reg = MRI.createVirtualRegister(RI.getBoolRC());
966 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_NE_I32_e64), Reg)
967 .addImm(Value)
968 .addReg(SrcReg);
969
970 return Reg;
971 }
972
getMovOpcode(const TargetRegisterClass * DstRC) const973 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const {
974
975 if (RI.hasAGPRs(DstRC))
976 return AMDGPU::COPY;
977 if (RI.getRegSizeInBits(*DstRC) == 32) {
978 return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
979 } else if (RI.getRegSizeInBits(*DstRC) == 64 && RI.isSGPRClass(DstRC)) {
980 return AMDGPU::S_MOV_B64;
981 } else if (RI.getRegSizeInBits(*DstRC) == 64 && !RI.isSGPRClass(DstRC)) {
982 return AMDGPU::V_MOV_B64_PSEUDO;
983 }
984 return AMDGPU::COPY;
985 }
986
getSGPRSpillSaveOpcode(unsigned Size)987 static unsigned getSGPRSpillSaveOpcode(unsigned Size) {
988 switch (Size) {
989 case 4:
990 return AMDGPU::SI_SPILL_S32_SAVE;
991 case 8:
992 return AMDGPU::SI_SPILL_S64_SAVE;
993 case 12:
994 return AMDGPU::SI_SPILL_S96_SAVE;
995 case 16:
996 return AMDGPU::SI_SPILL_S128_SAVE;
997 case 20:
998 return AMDGPU::SI_SPILL_S160_SAVE;
999 case 32:
1000 return AMDGPU::SI_SPILL_S256_SAVE;
1001 case 64:
1002 return AMDGPU::SI_SPILL_S512_SAVE;
1003 case 128:
1004 return AMDGPU::SI_SPILL_S1024_SAVE;
1005 default:
1006 llvm_unreachable("unknown register size");
1007 }
1008 }
1009
getVGPRSpillSaveOpcode(unsigned Size)1010 static unsigned getVGPRSpillSaveOpcode(unsigned Size) {
1011 switch (Size) {
1012 case 4:
1013 return AMDGPU::SI_SPILL_V32_SAVE;
1014 case 8:
1015 return AMDGPU::SI_SPILL_V64_SAVE;
1016 case 12:
1017 return AMDGPU::SI_SPILL_V96_SAVE;
1018 case 16:
1019 return AMDGPU::SI_SPILL_V128_SAVE;
1020 case 20:
1021 return AMDGPU::SI_SPILL_V160_SAVE;
1022 case 32:
1023 return AMDGPU::SI_SPILL_V256_SAVE;
1024 case 64:
1025 return AMDGPU::SI_SPILL_V512_SAVE;
1026 case 128:
1027 return AMDGPU::SI_SPILL_V1024_SAVE;
1028 default:
1029 llvm_unreachable("unknown register size");
1030 }
1031 }
1032
getAGPRSpillSaveOpcode(unsigned Size)1033 static unsigned getAGPRSpillSaveOpcode(unsigned Size) {
1034 switch (Size) {
1035 case 4:
1036 return AMDGPU::SI_SPILL_A32_SAVE;
1037 case 8:
1038 return AMDGPU::SI_SPILL_A64_SAVE;
1039 case 16:
1040 return AMDGPU::SI_SPILL_A128_SAVE;
1041 case 64:
1042 return AMDGPU::SI_SPILL_A512_SAVE;
1043 case 128:
1044 return AMDGPU::SI_SPILL_A1024_SAVE;
1045 default:
1046 llvm_unreachable("unknown register size");
1047 }
1048 }
1049
storeRegToStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned SrcReg,bool isKill,int FrameIndex,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const1050 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
1051 MachineBasicBlock::iterator MI,
1052 unsigned SrcReg, bool isKill,
1053 int FrameIndex,
1054 const TargetRegisterClass *RC,
1055 const TargetRegisterInfo *TRI) const {
1056 MachineFunction *MF = MBB.getParent();
1057 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1058 MachineFrameInfo &FrameInfo = MF->getFrameInfo();
1059 const DebugLoc &DL = MBB.findDebugLoc(MI);
1060
1061 unsigned Size = FrameInfo.getObjectSize(FrameIndex);
1062 unsigned Align = FrameInfo.getObjectAlignment(FrameIndex);
1063 MachinePointerInfo PtrInfo
1064 = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
1065 MachineMemOperand *MMO
1066 = MF->getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore,
1067 Size, Align);
1068 unsigned SpillSize = TRI->getSpillSize(*RC);
1069
1070 if (RI.isSGPRClass(RC)) {
1071 MFI->setHasSpilledSGPRs();
1072 assert(SrcReg != AMDGPU::M0 && "m0 should not be spilled");
1073
1074 // We are only allowed to create one new instruction when spilling
1075 // registers, so we need to use pseudo instruction for spilling SGPRs.
1076 const MCInstrDesc &OpDesc = get(getSGPRSpillSaveOpcode(SpillSize));
1077
1078 // The SGPR spill/restore instructions only work on number sgprs, so we need
1079 // to make sure we are using the correct register class.
1080 if (Register::isVirtualRegister(SrcReg) && SpillSize == 4) {
1081 MachineRegisterInfo &MRI = MF->getRegInfo();
1082 MRI.constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0RegClass);
1083 }
1084
1085 BuildMI(MBB, MI, DL, OpDesc)
1086 .addReg(SrcReg, getKillRegState(isKill)) // data
1087 .addFrameIndex(FrameIndex) // addr
1088 .addMemOperand(MMO)
1089 .addReg(MFI->getScratchRSrcReg(), RegState::Implicit)
1090 .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit);
1091 // Add the scratch resource registers as implicit uses because we may end up
1092 // needing them, and need to ensure that the reserved registers are
1093 // correctly handled.
1094 if (RI.spillSGPRToVGPR())
1095 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill);
1096 return;
1097 }
1098
1099 unsigned Opcode = RI.hasAGPRs(RC) ? getAGPRSpillSaveOpcode(SpillSize)
1100 : getVGPRSpillSaveOpcode(SpillSize);
1101 MFI->setHasSpilledVGPRs();
1102
1103 auto MIB = BuildMI(MBB, MI, DL, get(Opcode));
1104 if (RI.hasAGPRs(RC)) {
1105 MachineRegisterInfo &MRI = MF->getRegInfo();
1106 Register Tmp = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1107 MIB.addReg(Tmp, RegState::Define);
1108 }
1109 MIB.addReg(SrcReg, getKillRegState(isKill)) // data
1110 .addFrameIndex(FrameIndex) // addr
1111 .addReg(MFI->getScratchRSrcReg()) // scratch_rsrc
1112 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset
1113 .addImm(0) // offset
1114 .addMemOperand(MMO);
1115 }
1116
getSGPRSpillRestoreOpcode(unsigned Size)1117 static unsigned getSGPRSpillRestoreOpcode(unsigned Size) {
1118 switch (Size) {
1119 case 4:
1120 return AMDGPU::SI_SPILL_S32_RESTORE;
1121 case 8:
1122 return AMDGPU::SI_SPILL_S64_RESTORE;
1123 case 12:
1124 return AMDGPU::SI_SPILL_S96_RESTORE;
1125 case 16:
1126 return AMDGPU::SI_SPILL_S128_RESTORE;
1127 case 20:
1128 return AMDGPU::SI_SPILL_S160_RESTORE;
1129 case 32:
1130 return AMDGPU::SI_SPILL_S256_RESTORE;
1131 case 64:
1132 return AMDGPU::SI_SPILL_S512_RESTORE;
1133 case 128:
1134 return AMDGPU::SI_SPILL_S1024_RESTORE;
1135 default:
1136 llvm_unreachable("unknown register size");
1137 }
1138 }
1139
getVGPRSpillRestoreOpcode(unsigned Size)1140 static unsigned getVGPRSpillRestoreOpcode(unsigned Size) {
1141 switch (Size) {
1142 case 4:
1143 return AMDGPU::SI_SPILL_V32_RESTORE;
1144 case 8:
1145 return AMDGPU::SI_SPILL_V64_RESTORE;
1146 case 12:
1147 return AMDGPU::SI_SPILL_V96_RESTORE;
1148 case 16:
1149 return AMDGPU::SI_SPILL_V128_RESTORE;
1150 case 20:
1151 return AMDGPU::SI_SPILL_V160_RESTORE;
1152 case 32:
1153 return AMDGPU::SI_SPILL_V256_RESTORE;
1154 case 64:
1155 return AMDGPU::SI_SPILL_V512_RESTORE;
1156 case 128:
1157 return AMDGPU::SI_SPILL_V1024_RESTORE;
1158 default:
1159 llvm_unreachable("unknown register size");
1160 }
1161 }
1162
getAGPRSpillRestoreOpcode(unsigned Size)1163 static unsigned getAGPRSpillRestoreOpcode(unsigned Size) {
1164 switch (Size) {
1165 case 4:
1166 return AMDGPU::SI_SPILL_A32_RESTORE;
1167 case 8:
1168 return AMDGPU::SI_SPILL_A64_RESTORE;
1169 case 16:
1170 return AMDGPU::SI_SPILL_A128_RESTORE;
1171 case 64:
1172 return AMDGPU::SI_SPILL_A512_RESTORE;
1173 case 128:
1174 return AMDGPU::SI_SPILL_A1024_RESTORE;
1175 default:
1176 llvm_unreachable("unknown register size");
1177 }
1178 }
1179
loadRegFromStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned DestReg,int FrameIndex,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const1180 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
1181 MachineBasicBlock::iterator MI,
1182 unsigned DestReg, int FrameIndex,
1183 const TargetRegisterClass *RC,
1184 const TargetRegisterInfo *TRI) const {
1185 MachineFunction *MF = MBB.getParent();
1186 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1187 MachineFrameInfo &FrameInfo = MF->getFrameInfo();
1188 const DebugLoc &DL = MBB.findDebugLoc(MI);
1189 unsigned Align = FrameInfo.getObjectAlignment(FrameIndex);
1190 unsigned Size = FrameInfo.getObjectSize(FrameIndex);
1191 unsigned SpillSize = TRI->getSpillSize(*RC);
1192
1193 MachinePointerInfo PtrInfo
1194 = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
1195
1196 MachineMemOperand *MMO = MF->getMachineMemOperand(
1197 PtrInfo, MachineMemOperand::MOLoad, Size, Align);
1198
1199 if (RI.isSGPRClass(RC)) {
1200 MFI->setHasSpilledSGPRs();
1201 assert(DestReg != AMDGPU::M0 && "m0 should not be reloaded into");
1202
1203 // FIXME: Maybe this should not include a memoperand because it will be
1204 // lowered to non-memory instructions.
1205 const MCInstrDesc &OpDesc = get(getSGPRSpillRestoreOpcode(SpillSize));
1206 if (Register::isVirtualRegister(DestReg) && SpillSize == 4) {
1207 MachineRegisterInfo &MRI = MF->getRegInfo();
1208 MRI.constrainRegClass(DestReg, &AMDGPU::SReg_32_XM0RegClass);
1209 }
1210
1211 if (RI.spillSGPRToVGPR())
1212 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill);
1213 BuildMI(MBB, MI, DL, OpDesc, DestReg)
1214 .addFrameIndex(FrameIndex) // addr
1215 .addMemOperand(MMO)
1216 .addReg(MFI->getScratchRSrcReg(), RegState::Implicit)
1217 .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit);
1218 return;
1219 }
1220
1221 unsigned Opcode = RI.hasAGPRs(RC) ? getAGPRSpillRestoreOpcode(SpillSize)
1222 : getVGPRSpillRestoreOpcode(SpillSize);
1223 auto MIB = BuildMI(MBB, MI, DL, get(Opcode), DestReg);
1224 if (RI.hasAGPRs(RC)) {
1225 MachineRegisterInfo &MRI = MF->getRegInfo();
1226 Register Tmp = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1227 MIB.addReg(Tmp, RegState::Define);
1228 }
1229 MIB.addFrameIndex(FrameIndex) // vaddr
1230 .addReg(MFI->getScratchRSrcReg()) // scratch_rsrc
1231 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset
1232 .addImm(0) // offset
1233 .addMemOperand(MMO);
1234 }
1235
1236 /// \param @Offset Offset in bytes of the FrameIndex being spilled
calculateLDSSpillAddress(MachineBasicBlock & MBB,MachineInstr & MI,RegScavenger * RS,unsigned TmpReg,unsigned FrameOffset,unsigned Size) const1237 unsigned SIInstrInfo::calculateLDSSpillAddress(
1238 MachineBasicBlock &MBB, MachineInstr &MI, RegScavenger *RS, unsigned TmpReg,
1239 unsigned FrameOffset, unsigned Size) const {
1240 MachineFunction *MF = MBB.getParent();
1241 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1242 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
1243 const DebugLoc &DL = MBB.findDebugLoc(MI);
1244 unsigned WorkGroupSize = MFI->getMaxFlatWorkGroupSize();
1245 unsigned WavefrontSize = ST.getWavefrontSize();
1246
1247 unsigned TIDReg = MFI->getTIDReg();
1248 if (!MFI->hasCalculatedTID()) {
1249 MachineBasicBlock &Entry = MBB.getParent()->front();
1250 MachineBasicBlock::iterator Insert = Entry.front();
1251 const DebugLoc &DL = Insert->getDebugLoc();
1252
1253 TIDReg = RI.findUnusedRegister(MF->getRegInfo(), &AMDGPU::VGPR_32RegClass,
1254 *MF);
1255 if (TIDReg == AMDGPU::NoRegister)
1256 return TIDReg;
1257
1258 if (!AMDGPU::isShader(MF->getFunction().getCallingConv()) &&
1259 WorkGroupSize > WavefrontSize) {
1260 Register TIDIGXReg =
1261 MFI->getPreloadedReg(AMDGPUFunctionArgInfo::WORKGROUP_ID_X);
1262 Register TIDIGYReg =
1263 MFI->getPreloadedReg(AMDGPUFunctionArgInfo::WORKGROUP_ID_Y);
1264 Register TIDIGZReg =
1265 MFI->getPreloadedReg(AMDGPUFunctionArgInfo::WORKGROUP_ID_Z);
1266 Register InputPtrReg =
1267 MFI->getPreloadedReg(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR);
1268 for (unsigned Reg : {TIDIGXReg, TIDIGYReg, TIDIGZReg}) {
1269 if (!Entry.isLiveIn(Reg))
1270 Entry.addLiveIn(Reg);
1271 }
1272
1273 RS->enterBasicBlock(Entry);
1274 // FIXME: Can we scavenge an SReg_64 and access the subregs?
1275 unsigned STmp0 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0);
1276 unsigned STmp1 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0);
1277 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp0)
1278 .addReg(InputPtrReg)
1279 .addImm(SI::KernelInputOffsets::NGROUPS_Z);
1280 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp1)
1281 .addReg(InputPtrReg)
1282 .addImm(SI::KernelInputOffsets::NGROUPS_Y);
1283
1284 // NGROUPS.X * NGROUPS.Y
1285 BuildMI(Entry, Insert, DL, get(AMDGPU::S_MUL_I32), STmp1)
1286 .addReg(STmp1)
1287 .addReg(STmp0);
1288 // (NGROUPS.X * NGROUPS.Y) * TIDIG.X
1289 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MUL_U32_U24_e32), TIDReg)
1290 .addReg(STmp1)
1291 .addReg(TIDIGXReg);
1292 // NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)
1293 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MAD_U32_U24), TIDReg)
1294 .addReg(STmp0)
1295 .addReg(TIDIGYReg)
1296 .addReg(TIDReg);
1297 // (NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)) + TIDIG.Z
1298 getAddNoCarry(Entry, Insert, DL, TIDReg)
1299 .addReg(TIDReg)
1300 .addReg(TIDIGZReg)
1301 .addImm(0); // clamp bit
1302 } else {
1303 // Get the wave id
1304 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_LO_U32_B32_e64),
1305 TIDReg)
1306 .addImm(-1)
1307 .addImm(0);
1308
1309 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_HI_U32_B32_e64),
1310 TIDReg)
1311 .addImm(-1)
1312 .addReg(TIDReg);
1313 }
1314
1315 BuildMI(Entry, Insert, DL, get(AMDGPU::V_LSHLREV_B32_e32),
1316 TIDReg)
1317 .addImm(2)
1318 .addReg(TIDReg);
1319 MFI->setTIDReg(TIDReg);
1320 }
1321
1322 // Add FrameIndex to LDS offset
1323 unsigned LDSOffset = MFI->getLDSSize() + (FrameOffset * WorkGroupSize);
1324 getAddNoCarry(MBB, MI, DL, TmpReg)
1325 .addImm(LDSOffset)
1326 .addReg(TIDReg)
1327 .addImm(0); // clamp bit
1328
1329 return TmpReg;
1330 }
1331
insertWaitStates(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,int Count) const1332 void SIInstrInfo::insertWaitStates(MachineBasicBlock &MBB,
1333 MachineBasicBlock::iterator MI,
1334 int Count) const {
1335 DebugLoc DL = MBB.findDebugLoc(MI);
1336 while (Count > 0) {
1337 int Arg;
1338 if (Count >= 8)
1339 Arg = 7;
1340 else
1341 Arg = Count - 1;
1342 Count -= 8;
1343 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOP))
1344 .addImm(Arg);
1345 }
1346 }
1347
insertNoop(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI) const1348 void SIInstrInfo::insertNoop(MachineBasicBlock &MBB,
1349 MachineBasicBlock::iterator MI) const {
1350 insertWaitStates(MBB, MI, 1);
1351 }
1352
insertReturn(MachineBasicBlock & MBB) const1353 void SIInstrInfo::insertReturn(MachineBasicBlock &MBB) const {
1354 auto MF = MBB.getParent();
1355 SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>();
1356
1357 assert(Info->isEntryFunction());
1358
1359 if (MBB.succ_empty()) {
1360 bool HasNoTerminator = MBB.getFirstTerminator() == MBB.end();
1361 if (HasNoTerminator) {
1362 if (Info->returnsVoid()) {
1363 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::S_ENDPGM)).addImm(0);
1364 } else {
1365 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::SI_RETURN_TO_EPILOG));
1366 }
1367 }
1368 }
1369 }
1370
getNumWaitStates(const MachineInstr & MI)1371 unsigned SIInstrInfo::getNumWaitStates(const MachineInstr &MI) {
1372 switch (MI.getOpcode()) {
1373 default: return 1; // FIXME: Do wait states equal cycles?
1374
1375 case AMDGPU::S_NOP:
1376 return MI.getOperand(0).getImm() + 1;
1377 }
1378 }
1379
expandPostRAPseudo(MachineInstr & MI) const1380 bool SIInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1381 MachineBasicBlock &MBB = *MI.getParent();
1382 DebugLoc DL = MBB.findDebugLoc(MI);
1383 switch (MI.getOpcode()) {
1384 default: return TargetInstrInfo::expandPostRAPseudo(MI);
1385 case AMDGPU::S_MOV_B64_term:
1386 // This is only a terminator to get the correct spill code placement during
1387 // register allocation.
1388 MI.setDesc(get(AMDGPU::S_MOV_B64));
1389 break;
1390
1391 case AMDGPU::S_MOV_B32_term:
1392 // This is only a terminator to get the correct spill code placement during
1393 // register allocation.
1394 MI.setDesc(get(AMDGPU::S_MOV_B32));
1395 break;
1396
1397 case AMDGPU::S_XOR_B64_term:
1398 // This is only a terminator to get the correct spill code placement during
1399 // register allocation.
1400 MI.setDesc(get(AMDGPU::S_XOR_B64));
1401 break;
1402
1403 case AMDGPU::S_XOR_B32_term:
1404 // This is only a terminator to get the correct spill code placement during
1405 // register allocation.
1406 MI.setDesc(get(AMDGPU::S_XOR_B32));
1407 break;
1408
1409 case AMDGPU::S_OR_B32_term:
1410 // This is only a terminator to get the correct spill code placement during
1411 // register allocation.
1412 MI.setDesc(get(AMDGPU::S_OR_B32));
1413 break;
1414
1415 case AMDGPU::S_ANDN2_B64_term:
1416 // This is only a terminator to get the correct spill code placement during
1417 // register allocation.
1418 MI.setDesc(get(AMDGPU::S_ANDN2_B64));
1419 break;
1420
1421 case AMDGPU::S_ANDN2_B32_term:
1422 // This is only a terminator to get the correct spill code placement during
1423 // register allocation.
1424 MI.setDesc(get(AMDGPU::S_ANDN2_B32));
1425 break;
1426
1427 case AMDGPU::V_MOV_B64_PSEUDO: {
1428 Register Dst = MI.getOperand(0).getReg();
1429 Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
1430 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
1431
1432 const MachineOperand &SrcOp = MI.getOperand(1);
1433 // FIXME: Will this work for 64-bit floating point immediates?
1434 assert(!SrcOp.isFPImm());
1435 if (SrcOp.isImm()) {
1436 APInt Imm(64, SrcOp.getImm());
1437 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
1438 .addImm(Imm.getLoBits(32).getZExtValue())
1439 .addReg(Dst, RegState::Implicit | RegState::Define);
1440 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
1441 .addImm(Imm.getHiBits(32).getZExtValue())
1442 .addReg(Dst, RegState::Implicit | RegState::Define);
1443 } else {
1444 assert(SrcOp.isReg());
1445 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
1446 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0))
1447 .addReg(Dst, RegState::Implicit | RegState::Define);
1448 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
1449 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1))
1450 .addReg(Dst, RegState::Implicit | RegState::Define);
1451 }
1452 MI.eraseFromParent();
1453 break;
1454 }
1455 case AMDGPU::V_MOV_B64_DPP_PSEUDO: {
1456 expandMovDPP64(MI);
1457 break;
1458 }
1459 case AMDGPU::V_SET_INACTIVE_B32: {
1460 unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64;
1461 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
1462 BuildMI(MBB, MI, DL, get(NotOpc), Exec)
1463 .addReg(Exec);
1464 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), MI.getOperand(0).getReg())
1465 .add(MI.getOperand(2));
1466 BuildMI(MBB, MI, DL, get(NotOpc), Exec)
1467 .addReg(Exec);
1468 MI.eraseFromParent();
1469 break;
1470 }
1471 case AMDGPU::V_SET_INACTIVE_B64: {
1472 unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64;
1473 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
1474 BuildMI(MBB, MI, DL, get(NotOpc), Exec)
1475 .addReg(Exec);
1476 MachineInstr *Copy = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO),
1477 MI.getOperand(0).getReg())
1478 .add(MI.getOperand(2));
1479 expandPostRAPseudo(*Copy);
1480 BuildMI(MBB, MI, DL, get(NotOpc), Exec)
1481 .addReg(Exec);
1482 MI.eraseFromParent();
1483 break;
1484 }
1485 case AMDGPU::V_MOVRELD_B32_V1:
1486 case AMDGPU::V_MOVRELD_B32_V2:
1487 case AMDGPU::V_MOVRELD_B32_V4:
1488 case AMDGPU::V_MOVRELD_B32_V8:
1489 case AMDGPU::V_MOVRELD_B32_V16: {
1490 const MCInstrDesc &MovRelDesc = get(AMDGPU::V_MOVRELD_B32_e32);
1491 Register VecReg = MI.getOperand(0).getReg();
1492 bool IsUndef = MI.getOperand(1).isUndef();
1493 unsigned SubReg = AMDGPU::sub0 + MI.getOperand(3).getImm();
1494 assert(VecReg == MI.getOperand(1).getReg());
1495
1496 MachineInstr *MovRel =
1497 BuildMI(MBB, MI, DL, MovRelDesc)
1498 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef)
1499 .add(MI.getOperand(2))
1500 .addReg(VecReg, RegState::ImplicitDefine)
1501 .addReg(VecReg,
1502 RegState::Implicit | (IsUndef ? RegState::Undef : 0));
1503
1504 const int ImpDefIdx =
1505 MovRelDesc.getNumOperands() + MovRelDesc.getNumImplicitUses();
1506 const int ImpUseIdx = ImpDefIdx + 1;
1507 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1508
1509 MI.eraseFromParent();
1510 break;
1511 }
1512 case AMDGPU::SI_PC_ADD_REL_OFFSET: {
1513 MachineFunction &MF = *MBB.getParent();
1514 Register Reg = MI.getOperand(0).getReg();
1515 Register RegLo = RI.getSubReg(Reg, AMDGPU::sub0);
1516 Register RegHi = RI.getSubReg(Reg, AMDGPU::sub1);
1517
1518 // Create a bundle so these instructions won't be re-ordered by the
1519 // post-RA scheduler.
1520 MIBundleBuilder Bundler(MBB, MI);
1521 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg));
1522
1523 // Add 32-bit offset from this instruction to the start of the
1524 // constant data.
1525 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_ADD_U32), RegLo)
1526 .addReg(RegLo)
1527 .add(MI.getOperand(1)));
1528
1529 MachineInstrBuilder MIB = BuildMI(MF, DL, get(AMDGPU::S_ADDC_U32), RegHi)
1530 .addReg(RegHi);
1531 MIB.add(MI.getOperand(2));
1532
1533 Bundler.append(MIB);
1534 finalizeBundle(MBB, Bundler.begin());
1535
1536 MI.eraseFromParent();
1537 break;
1538 }
1539 case AMDGPU::ENTER_WWM: {
1540 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
1541 // WWM is entered.
1542 MI.setDesc(get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32
1543 : AMDGPU::S_OR_SAVEEXEC_B64));
1544 break;
1545 }
1546 case AMDGPU::EXIT_WWM: {
1547 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
1548 // WWM is exited.
1549 MI.setDesc(get(ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64));
1550 break;
1551 }
1552 case TargetOpcode::BUNDLE: {
1553 if (!MI.mayLoad() || MI.hasUnmodeledSideEffects())
1554 return false;
1555
1556 // If it is a load it must be a memory clause
1557 for (MachineBasicBlock::instr_iterator I = MI.getIterator();
1558 I->isBundledWithSucc(); ++I) {
1559 I->unbundleFromSucc();
1560 for (MachineOperand &MO : I->operands())
1561 if (MO.isReg())
1562 MO.setIsInternalRead(false);
1563 }
1564
1565 MI.eraseFromParent();
1566 break;
1567 }
1568 }
1569 return true;
1570 }
1571
1572 std::pair<MachineInstr*, MachineInstr*>
expandMovDPP64(MachineInstr & MI) const1573 SIInstrInfo::expandMovDPP64(MachineInstr &MI) const {
1574 assert (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
1575
1576 MachineBasicBlock &MBB = *MI.getParent();
1577 DebugLoc DL = MBB.findDebugLoc(MI);
1578 MachineFunction *MF = MBB.getParent();
1579 MachineRegisterInfo &MRI = MF->getRegInfo();
1580 Register Dst = MI.getOperand(0).getReg();
1581 unsigned Part = 0;
1582 MachineInstr *Split[2];
1583
1584
1585 for (auto Sub : { AMDGPU::sub0, AMDGPU::sub1 }) {
1586 auto MovDPP = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_dpp));
1587 if (Dst.isPhysical()) {
1588 MovDPP.addDef(RI.getSubReg(Dst, Sub));
1589 } else {
1590 assert(MRI.isSSA());
1591 auto Tmp = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1592 MovDPP.addDef(Tmp);
1593 }
1594
1595 for (unsigned I = 1; I <= 2; ++I) { // old and src operands.
1596 const MachineOperand &SrcOp = MI.getOperand(I);
1597 assert(!SrcOp.isFPImm());
1598 if (SrcOp.isImm()) {
1599 APInt Imm(64, SrcOp.getImm());
1600 Imm.ashrInPlace(Part * 32);
1601 MovDPP.addImm(Imm.getLoBits(32).getZExtValue());
1602 } else {
1603 assert(SrcOp.isReg());
1604 Register Src = SrcOp.getReg();
1605 if (Src.isPhysical())
1606 MovDPP.addReg(RI.getSubReg(Src, Sub));
1607 else
1608 MovDPP.addReg(Src, SrcOp.isUndef() ? RegState::Undef : 0, Sub);
1609 }
1610 }
1611
1612 for (unsigned I = 3; I < MI.getNumExplicitOperands(); ++I)
1613 MovDPP.addImm(MI.getOperand(I).getImm());
1614
1615 Split[Part] = MovDPP;
1616 ++Part;
1617 }
1618
1619 if (Dst.isVirtual())
1620 BuildMI(MBB, MI, DL, get(AMDGPU::REG_SEQUENCE), Dst)
1621 .addReg(Split[0]->getOperand(0).getReg())
1622 .addImm(AMDGPU::sub0)
1623 .addReg(Split[1]->getOperand(0).getReg())
1624 .addImm(AMDGPU::sub1);
1625
1626 MI.eraseFromParent();
1627 return std::make_pair(Split[0], Split[1]);
1628 }
1629
swapSourceModifiers(MachineInstr & MI,MachineOperand & Src0,unsigned Src0OpName,MachineOperand & Src1,unsigned Src1OpName) const1630 bool SIInstrInfo::swapSourceModifiers(MachineInstr &MI,
1631 MachineOperand &Src0,
1632 unsigned Src0OpName,
1633 MachineOperand &Src1,
1634 unsigned Src1OpName) const {
1635 MachineOperand *Src0Mods = getNamedOperand(MI, Src0OpName);
1636 if (!Src0Mods)
1637 return false;
1638
1639 MachineOperand *Src1Mods = getNamedOperand(MI, Src1OpName);
1640 assert(Src1Mods &&
1641 "All commutable instructions have both src0 and src1 modifiers");
1642
1643 int Src0ModsVal = Src0Mods->getImm();
1644 int Src1ModsVal = Src1Mods->getImm();
1645
1646 Src1Mods->setImm(Src0ModsVal);
1647 Src0Mods->setImm(Src1ModsVal);
1648 return true;
1649 }
1650
swapRegAndNonRegOperand(MachineInstr & MI,MachineOperand & RegOp,MachineOperand & NonRegOp)1651 static MachineInstr *swapRegAndNonRegOperand(MachineInstr &MI,
1652 MachineOperand &RegOp,
1653 MachineOperand &NonRegOp) {
1654 Register Reg = RegOp.getReg();
1655 unsigned SubReg = RegOp.getSubReg();
1656 bool IsKill = RegOp.isKill();
1657 bool IsDead = RegOp.isDead();
1658 bool IsUndef = RegOp.isUndef();
1659 bool IsDebug = RegOp.isDebug();
1660
1661 if (NonRegOp.isImm())
1662 RegOp.ChangeToImmediate(NonRegOp.getImm());
1663 else if (NonRegOp.isFI())
1664 RegOp.ChangeToFrameIndex(NonRegOp.getIndex());
1665 else
1666 return nullptr;
1667
1668 NonRegOp.ChangeToRegister(Reg, false, false, IsKill, IsDead, IsUndef, IsDebug);
1669 NonRegOp.setSubReg(SubReg);
1670
1671 return &MI;
1672 }
1673
commuteInstructionImpl(MachineInstr & MI,bool NewMI,unsigned Src0Idx,unsigned Src1Idx) const1674 MachineInstr *SIInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
1675 unsigned Src0Idx,
1676 unsigned Src1Idx) const {
1677 assert(!NewMI && "this should never be used");
1678
1679 unsigned Opc = MI.getOpcode();
1680 int CommutedOpcode = commuteOpcode(Opc);
1681 if (CommutedOpcode == -1)
1682 return nullptr;
1683
1684 assert(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) ==
1685 static_cast<int>(Src0Idx) &&
1686 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) ==
1687 static_cast<int>(Src1Idx) &&
1688 "inconsistency with findCommutedOpIndices");
1689
1690 MachineOperand &Src0 = MI.getOperand(Src0Idx);
1691 MachineOperand &Src1 = MI.getOperand(Src1Idx);
1692
1693 MachineInstr *CommutedMI = nullptr;
1694 if (Src0.isReg() && Src1.isReg()) {
1695 if (isOperandLegal(MI, Src1Idx, &Src0)) {
1696 // Be sure to copy the source modifiers to the right place.
1697 CommutedMI
1698 = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, Src0Idx, Src1Idx);
1699 }
1700
1701 } else if (Src0.isReg() && !Src1.isReg()) {
1702 // src0 should always be able to support any operand type, so no need to
1703 // check operand legality.
1704 CommutedMI = swapRegAndNonRegOperand(MI, Src0, Src1);
1705 } else if (!Src0.isReg() && Src1.isReg()) {
1706 if (isOperandLegal(MI, Src1Idx, &Src0))
1707 CommutedMI = swapRegAndNonRegOperand(MI, Src1, Src0);
1708 } else {
1709 // FIXME: Found two non registers to commute. This does happen.
1710 return nullptr;
1711 }
1712
1713 if (CommutedMI) {
1714 swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_modifiers,
1715 Src1, AMDGPU::OpName::src1_modifiers);
1716
1717 CommutedMI->setDesc(get(CommutedOpcode));
1718 }
1719
1720 return CommutedMI;
1721 }
1722
1723 // This needs to be implemented because the source modifiers may be inserted
1724 // between the true commutable operands, and the base
1725 // TargetInstrInfo::commuteInstruction uses it.
findCommutedOpIndices(const MachineInstr & MI,unsigned & SrcOpIdx0,unsigned & SrcOpIdx1) const1726 bool SIInstrInfo::findCommutedOpIndices(const MachineInstr &MI,
1727 unsigned &SrcOpIdx0,
1728 unsigned &SrcOpIdx1) const {
1729 return findCommutedOpIndices(MI.getDesc(), SrcOpIdx0, SrcOpIdx1);
1730 }
1731
findCommutedOpIndices(MCInstrDesc Desc,unsigned & SrcOpIdx0,unsigned & SrcOpIdx1) const1732 bool SIInstrInfo::findCommutedOpIndices(MCInstrDesc Desc, unsigned &SrcOpIdx0,
1733 unsigned &SrcOpIdx1) const {
1734 if (!Desc.isCommutable())
1735 return false;
1736
1737 unsigned Opc = Desc.getOpcode();
1738 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
1739 if (Src0Idx == -1)
1740 return false;
1741
1742 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
1743 if (Src1Idx == -1)
1744 return false;
1745
1746 return fixCommutedOpIndices(SrcOpIdx0, SrcOpIdx1, Src0Idx, Src1Idx);
1747 }
1748
isBranchOffsetInRange(unsigned BranchOp,int64_t BrOffset) const1749 bool SIInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
1750 int64_t BrOffset) const {
1751 // BranchRelaxation should never have to check s_setpc_b64 because its dest
1752 // block is unanalyzable.
1753 assert(BranchOp != AMDGPU::S_SETPC_B64);
1754
1755 // Convert to dwords.
1756 BrOffset /= 4;
1757
1758 // The branch instructions do PC += signext(SIMM16 * 4) + 4, so the offset is
1759 // from the next instruction.
1760 BrOffset -= 1;
1761
1762 return isIntN(BranchOffsetBits, BrOffset);
1763 }
1764
getBranchDestBlock(const MachineInstr & MI) const1765 MachineBasicBlock *SIInstrInfo::getBranchDestBlock(
1766 const MachineInstr &MI) const {
1767 if (MI.getOpcode() == AMDGPU::S_SETPC_B64) {
1768 // This would be a difficult analysis to perform, but can always be legal so
1769 // there's no need to analyze it.
1770 return nullptr;
1771 }
1772
1773 return MI.getOperand(0).getMBB();
1774 }
1775
insertIndirectBranch(MachineBasicBlock & MBB,MachineBasicBlock & DestBB,const DebugLoc & DL,int64_t BrOffset,RegScavenger * RS) const1776 unsigned SIInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
1777 MachineBasicBlock &DestBB,
1778 const DebugLoc &DL,
1779 int64_t BrOffset,
1780 RegScavenger *RS) const {
1781 assert(RS && "RegScavenger required for long branching");
1782 assert(MBB.empty() &&
1783 "new block should be inserted for expanding unconditional branch");
1784 assert(MBB.pred_size() == 1);
1785
1786 MachineFunction *MF = MBB.getParent();
1787 MachineRegisterInfo &MRI = MF->getRegInfo();
1788
1789 // FIXME: Virtual register workaround for RegScavenger not working with empty
1790 // blocks.
1791 Register PCReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1792
1793 auto I = MBB.end();
1794
1795 // We need to compute the offset relative to the instruction immediately after
1796 // s_getpc_b64. Insert pc arithmetic code before last terminator.
1797 MachineInstr *GetPC = BuildMI(MBB, I, DL, get(AMDGPU::S_GETPC_B64), PCReg);
1798
1799 // TODO: Handle > 32-bit block address.
1800 if (BrOffset >= 0) {
1801 BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_U32))
1802 .addReg(PCReg, RegState::Define, AMDGPU::sub0)
1803 .addReg(PCReg, 0, AMDGPU::sub0)
1804 .addMBB(&DestBB, MO_LONG_BRANCH_FORWARD);
1805 BuildMI(MBB, I, DL, get(AMDGPU::S_ADDC_U32))
1806 .addReg(PCReg, RegState::Define, AMDGPU::sub1)
1807 .addReg(PCReg, 0, AMDGPU::sub1)
1808 .addImm(0);
1809 } else {
1810 // Backwards branch.
1811 BuildMI(MBB, I, DL, get(AMDGPU::S_SUB_U32))
1812 .addReg(PCReg, RegState::Define, AMDGPU::sub0)
1813 .addReg(PCReg, 0, AMDGPU::sub0)
1814 .addMBB(&DestBB, MO_LONG_BRANCH_BACKWARD);
1815 BuildMI(MBB, I, DL, get(AMDGPU::S_SUBB_U32))
1816 .addReg(PCReg, RegState::Define, AMDGPU::sub1)
1817 .addReg(PCReg, 0, AMDGPU::sub1)
1818 .addImm(0);
1819 }
1820
1821 // Insert the indirect branch after the other terminator.
1822 BuildMI(&MBB, DL, get(AMDGPU::S_SETPC_B64))
1823 .addReg(PCReg);
1824
1825 // FIXME: If spilling is necessary, this will fail because this scavenger has
1826 // no emergency stack slots. It is non-trivial to spill in this situation,
1827 // because the restore code needs to be specially placed after the
1828 // jump. BranchRelaxation then needs to be made aware of the newly inserted
1829 // block.
1830 //
1831 // If a spill is needed for the pc register pair, we need to insert a spill
1832 // restore block right before the destination block, and insert a short branch
1833 // into the old destination block's fallthrough predecessor.
1834 // e.g.:
1835 //
1836 // s_cbranch_scc0 skip_long_branch:
1837 //
1838 // long_branch_bb:
1839 // spill s[8:9]
1840 // s_getpc_b64 s[8:9]
1841 // s_add_u32 s8, s8, restore_bb
1842 // s_addc_u32 s9, s9, 0
1843 // s_setpc_b64 s[8:9]
1844 //
1845 // skip_long_branch:
1846 // foo;
1847 //
1848 // .....
1849 //
1850 // dest_bb_fallthrough_predecessor:
1851 // bar;
1852 // s_branch dest_bb
1853 //
1854 // restore_bb:
1855 // restore s[8:9]
1856 // fallthrough dest_bb
1857 ///
1858 // dest_bb:
1859 // buzz;
1860
1861 RS->enterBasicBlockEnd(MBB);
1862 unsigned Scav = RS->scavengeRegisterBackwards(
1863 AMDGPU::SReg_64RegClass,
1864 MachineBasicBlock::iterator(GetPC), false, 0);
1865 MRI.replaceRegWith(PCReg, Scav);
1866 MRI.clearVirtRegs();
1867 RS->setRegUsed(Scav);
1868
1869 return 4 + 8 + 4 + 4;
1870 }
1871
getBranchOpcode(SIInstrInfo::BranchPredicate Cond)1872 unsigned SIInstrInfo::getBranchOpcode(SIInstrInfo::BranchPredicate Cond) {
1873 switch (Cond) {
1874 case SIInstrInfo::SCC_TRUE:
1875 return AMDGPU::S_CBRANCH_SCC1;
1876 case SIInstrInfo::SCC_FALSE:
1877 return AMDGPU::S_CBRANCH_SCC0;
1878 case SIInstrInfo::VCCNZ:
1879 return AMDGPU::S_CBRANCH_VCCNZ;
1880 case SIInstrInfo::VCCZ:
1881 return AMDGPU::S_CBRANCH_VCCZ;
1882 case SIInstrInfo::EXECNZ:
1883 return AMDGPU::S_CBRANCH_EXECNZ;
1884 case SIInstrInfo::EXECZ:
1885 return AMDGPU::S_CBRANCH_EXECZ;
1886 default:
1887 llvm_unreachable("invalid branch predicate");
1888 }
1889 }
1890
getBranchPredicate(unsigned Opcode)1891 SIInstrInfo::BranchPredicate SIInstrInfo::getBranchPredicate(unsigned Opcode) {
1892 switch (Opcode) {
1893 case AMDGPU::S_CBRANCH_SCC0:
1894 return SCC_FALSE;
1895 case AMDGPU::S_CBRANCH_SCC1:
1896 return SCC_TRUE;
1897 case AMDGPU::S_CBRANCH_VCCNZ:
1898 return VCCNZ;
1899 case AMDGPU::S_CBRANCH_VCCZ:
1900 return VCCZ;
1901 case AMDGPU::S_CBRANCH_EXECNZ:
1902 return EXECNZ;
1903 case AMDGPU::S_CBRANCH_EXECZ:
1904 return EXECZ;
1905 default:
1906 return INVALID_BR;
1907 }
1908 }
1909
analyzeBranchImpl(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,MachineBasicBlock * & TBB,MachineBasicBlock * & FBB,SmallVectorImpl<MachineOperand> & Cond,bool AllowModify) const1910 bool SIInstrInfo::analyzeBranchImpl(MachineBasicBlock &MBB,
1911 MachineBasicBlock::iterator I,
1912 MachineBasicBlock *&TBB,
1913 MachineBasicBlock *&FBB,
1914 SmallVectorImpl<MachineOperand> &Cond,
1915 bool AllowModify) const {
1916 if (I->getOpcode() == AMDGPU::S_BRANCH) {
1917 // Unconditional Branch
1918 TBB = I->getOperand(0).getMBB();
1919 return false;
1920 }
1921
1922 MachineBasicBlock *CondBB = nullptr;
1923
1924 if (I->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
1925 CondBB = I->getOperand(1).getMBB();
1926 Cond.push_back(I->getOperand(0));
1927 } else {
1928 BranchPredicate Pred = getBranchPredicate(I->getOpcode());
1929 if (Pred == INVALID_BR)
1930 return true;
1931
1932 CondBB = I->getOperand(0).getMBB();
1933 Cond.push_back(MachineOperand::CreateImm(Pred));
1934 Cond.push_back(I->getOperand(1)); // Save the branch register.
1935 }
1936 ++I;
1937
1938 if (I == MBB.end()) {
1939 // Conditional branch followed by fall-through.
1940 TBB = CondBB;
1941 return false;
1942 }
1943
1944 if (I->getOpcode() == AMDGPU::S_BRANCH) {
1945 TBB = CondBB;
1946 FBB = I->getOperand(0).getMBB();
1947 return false;
1948 }
1949
1950 return true;
1951 }
1952
analyzeBranch(MachineBasicBlock & MBB,MachineBasicBlock * & TBB,MachineBasicBlock * & FBB,SmallVectorImpl<MachineOperand> & Cond,bool AllowModify) const1953 bool SIInstrInfo::analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
1954 MachineBasicBlock *&FBB,
1955 SmallVectorImpl<MachineOperand> &Cond,
1956 bool AllowModify) const {
1957 MachineBasicBlock::iterator I = MBB.getFirstTerminator();
1958 auto E = MBB.end();
1959 if (I == E)
1960 return false;
1961
1962 // Skip over the instructions that are artificially terminators for special
1963 // exec management.
1964 while (I != E && !I->isBranch() && !I->isReturn() &&
1965 I->getOpcode() != AMDGPU::SI_MASK_BRANCH) {
1966 switch (I->getOpcode()) {
1967 case AMDGPU::SI_MASK_BRANCH:
1968 case AMDGPU::S_MOV_B64_term:
1969 case AMDGPU::S_XOR_B64_term:
1970 case AMDGPU::S_ANDN2_B64_term:
1971 case AMDGPU::S_MOV_B32_term:
1972 case AMDGPU::S_XOR_B32_term:
1973 case AMDGPU::S_OR_B32_term:
1974 case AMDGPU::S_ANDN2_B32_term:
1975 break;
1976 case AMDGPU::SI_IF:
1977 case AMDGPU::SI_ELSE:
1978 case AMDGPU::SI_KILL_I1_TERMINATOR:
1979 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
1980 // FIXME: It's messy that these need to be considered here at all.
1981 return true;
1982 default:
1983 llvm_unreachable("unexpected non-branch terminator inst");
1984 }
1985
1986 ++I;
1987 }
1988
1989 if (I == E)
1990 return false;
1991
1992 if (I->getOpcode() != AMDGPU::SI_MASK_BRANCH)
1993 return analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify);
1994
1995 ++I;
1996
1997 // TODO: Should be able to treat as fallthrough?
1998 if (I == MBB.end())
1999 return true;
2000
2001 if (analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify))
2002 return true;
2003
2004 MachineBasicBlock *MaskBrDest = I->getOperand(0).getMBB();
2005
2006 // Specifically handle the case where the conditional branch is to the same
2007 // destination as the mask branch. e.g.
2008 //
2009 // si_mask_branch BB8
2010 // s_cbranch_execz BB8
2011 // s_cbranch BB9
2012 //
2013 // This is required to understand divergent loops which may need the branches
2014 // to be relaxed.
2015 if (TBB != MaskBrDest || Cond.empty())
2016 return true;
2017
2018 auto Pred = Cond[0].getImm();
2019 return (Pred != EXECZ && Pred != EXECNZ);
2020 }
2021
removeBranch(MachineBasicBlock & MBB,int * BytesRemoved) const2022 unsigned SIInstrInfo::removeBranch(MachineBasicBlock &MBB,
2023 int *BytesRemoved) const {
2024 MachineBasicBlock::iterator I = MBB.getFirstTerminator();
2025
2026 unsigned Count = 0;
2027 unsigned RemovedSize = 0;
2028 while (I != MBB.end()) {
2029 MachineBasicBlock::iterator Next = std::next(I);
2030 if (I->getOpcode() == AMDGPU::SI_MASK_BRANCH) {
2031 I = Next;
2032 continue;
2033 }
2034
2035 RemovedSize += getInstSizeInBytes(*I);
2036 I->eraseFromParent();
2037 ++Count;
2038 I = Next;
2039 }
2040
2041 if (BytesRemoved)
2042 *BytesRemoved = RemovedSize;
2043
2044 return Count;
2045 }
2046
2047 // Copy the flags onto the implicit condition register operand.
preserveCondRegFlags(MachineOperand & CondReg,const MachineOperand & OrigCond)2048 static void preserveCondRegFlags(MachineOperand &CondReg,
2049 const MachineOperand &OrigCond) {
2050 CondReg.setIsUndef(OrigCond.isUndef());
2051 CondReg.setIsKill(OrigCond.isKill());
2052 }
2053
insertBranch(MachineBasicBlock & MBB,MachineBasicBlock * TBB,MachineBasicBlock * FBB,ArrayRef<MachineOperand> Cond,const DebugLoc & DL,int * BytesAdded) const2054 unsigned SIInstrInfo::insertBranch(MachineBasicBlock &MBB,
2055 MachineBasicBlock *TBB,
2056 MachineBasicBlock *FBB,
2057 ArrayRef<MachineOperand> Cond,
2058 const DebugLoc &DL,
2059 int *BytesAdded) const {
2060 if (!FBB && Cond.empty()) {
2061 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH))
2062 .addMBB(TBB);
2063 if (BytesAdded)
2064 *BytesAdded = 4;
2065 return 1;
2066 }
2067
2068 if(Cond.size() == 1 && Cond[0].isReg()) {
2069 BuildMI(&MBB, DL, get(AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO))
2070 .add(Cond[0])
2071 .addMBB(TBB);
2072 return 1;
2073 }
2074
2075 assert(TBB && Cond[0].isImm());
2076
2077 unsigned Opcode
2078 = getBranchOpcode(static_cast<BranchPredicate>(Cond[0].getImm()));
2079
2080 if (!FBB) {
2081 Cond[1].isUndef();
2082 MachineInstr *CondBr =
2083 BuildMI(&MBB, DL, get(Opcode))
2084 .addMBB(TBB);
2085
2086 // Copy the flags onto the implicit condition register operand.
2087 preserveCondRegFlags(CondBr->getOperand(1), Cond[1]);
2088
2089 if (BytesAdded)
2090 *BytesAdded = 4;
2091 return 1;
2092 }
2093
2094 assert(TBB && FBB);
2095
2096 MachineInstr *CondBr =
2097 BuildMI(&MBB, DL, get(Opcode))
2098 .addMBB(TBB);
2099 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH))
2100 .addMBB(FBB);
2101
2102 MachineOperand &CondReg = CondBr->getOperand(1);
2103 CondReg.setIsUndef(Cond[1].isUndef());
2104 CondReg.setIsKill(Cond[1].isKill());
2105
2106 if (BytesAdded)
2107 *BytesAdded = 8;
2108
2109 return 2;
2110 }
2111
reverseBranchCondition(SmallVectorImpl<MachineOperand> & Cond) const2112 bool SIInstrInfo::reverseBranchCondition(
2113 SmallVectorImpl<MachineOperand> &Cond) const {
2114 if (Cond.size() != 2) {
2115 return true;
2116 }
2117
2118 if (Cond[0].isImm()) {
2119 Cond[0].setImm(-Cond[0].getImm());
2120 return false;
2121 }
2122
2123 return true;
2124 }
2125
canInsertSelect(const MachineBasicBlock & MBB,ArrayRef<MachineOperand> Cond,unsigned TrueReg,unsigned FalseReg,int & CondCycles,int & TrueCycles,int & FalseCycles) const2126 bool SIInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
2127 ArrayRef<MachineOperand> Cond,
2128 unsigned TrueReg, unsigned FalseReg,
2129 int &CondCycles,
2130 int &TrueCycles, int &FalseCycles) const {
2131 switch (Cond[0].getImm()) {
2132 case VCCNZ:
2133 case VCCZ: {
2134 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2135 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg);
2136 assert(MRI.getRegClass(FalseReg) == RC);
2137
2138 int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32;
2139 CondCycles = TrueCycles = FalseCycles = NumInsts; // ???
2140
2141 // Limit to equal cost for branch vs. N v_cndmask_b32s.
2142 return RI.hasVGPRs(RC) && NumInsts <= 6;
2143 }
2144 case SCC_TRUE:
2145 case SCC_FALSE: {
2146 // FIXME: We could insert for VGPRs if we could replace the original compare
2147 // with a vector one.
2148 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2149 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg);
2150 assert(MRI.getRegClass(FalseReg) == RC);
2151
2152 int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32;
2153
2154 // Multiples of 8 can do s_cselect_b64
2155 if (NumInsts % 2 == 0)
2156 NumInsts /= 2;
2157
2158 CondCycles = TrueCycles = FalseCycles = NumInsts; // ???
2159 return RI.isSGPRClass(RC);
2160 }
2161 default:
2162 return false;
2163 }
2164 }
2165
insertSelect(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,unsigned DstReg,ArrayRef<MachineOperand> Cond,unsigned TrueReg,unsigned FalseReg) const2166 void SIInstrInfo::insertSelect(MachineBasicBlock &MBB,
2167 MachineBasicBlock::iterator I, const DebugLoc &DL,
2168 unsigned DstReg, ArrayRef<MachineOperand> Cond,
2169 unsigned TrueReg, unsigned FalseReg) const {
2170 BranchPredicate Pred = static_cast<BranchPredicate>(Cond[0].getImm());
2171 if (Pred == VCCZ || Pred == SCC_FALSE) {
2172 Pred = static_cast<BranchPredicate>(-Pred);
2173 std::swap(TrueReg, FalseReg);
2174 }
2175
2176 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2177 const TargetRegisterClass *DstRC = MRI.getRegClass(DstReg);
2178 unsigned DstSize = RI.getRegSizeInBits(*DstRC);
2179
2180 if (DstSize == 32) {
2181 unsigned SelOp = Pred == SCC_TRUE ?
2182 AMDGPU::S_CSELECT_B32 : AMDGPU::V_CNDMASK_B32_e32;
2183
2184 // Instruction's operands are backwards from what is expected.
2185 MachineInstr *Select =
2186 BuildMI(MBB, I, DL, get(SelOp), DstReg)
2187 .addReg(FalseReg)
2188 .addReg(TrueReg);
2189
2190 preserveCondRegFlags(Select->getOperand(3), Cond[1]);
2191 return;
2192 }
2193
2194 if (DstSize == 64 && Pred == SCC_TRUE) {
2195 MachineInstr *Select =
2196 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), DstReg)
2197 .addReg(FalseReg)
2198 .addReg(TrueReg);
2199
2200 preserveCondRegFlags(Select->getOperand(3), Cond[1]);
2201 return;
2202 }
2203
2204 static const int16_t Sub0_15[] = {
2205 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
2206 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
2207 AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11,
2208 AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15,
2209 };
2210
2211 static const int16_t Sub0_15_64[] = {
2212 AMDGPU::sub0_sub1, AMDGPU::sub2_sub3,
2213 AMDGPU::sub4_sub5, AMDGPU::sub6_sub7,
2214 AMDGPU::sub8_sub9, AMDGPU::sub10_sub11,
2215 AMDGPU::sub12_sub13, AMDGPU::sub14_sub15,
2216 };
2217
2218 unsigned SelOp = AMDGPU::V_CNDMASK_B32_e32;
2219 const TargetRegisterClass *EltRC = &AMDGPU::VGPR_32RegClass;
2220 const int16_t *SubIndices = Sub0_15;
2221 int NElts = DstSize / 32;
2222
2223 // 64-bit select is only available for SALU.
2224 // TODO: Split 96-bit into 64-bit and 32-bit, not 3x 32-bit.
2225 if (Pred == SCC_TRUE) {
2226 if (NElts % 2) {
2227 SelOp = AMDGPU::S_CSELECT_B32;
2228 EltRC = &AMDGPU::SGPR_32RegClass;
2229 } else {
2230 SelOp = AMDGPU::S_CSELECT_B64;
2231 EltRC = &AMDGPU::SGPR_64RegClass;
2232 SubIndices = Sub0_15_64;
2233 NElts /= 2;
2234 }
2235 }
2236
2237 MachineInstrBuilder MIB = BuildMI(
2238 MBB, I, DL, get(AMDGPU::REG_SEQUENCE), DstReg);
2239
2240 I = MIB->getIterator();
2241
2242 SmallVector<unsigned, 8> Regs;
2243 for (int Idx = 0; Idx != NElts; ++Idx) {
2244 Register DstElt = MRI.createVirtualRegister(EltRC);
2245 Regs.push_back(DstElt);
2246
2247 unsigned SubIdx = SubIndices[Idx];
2248
2249 MachineInstr *Select =
2250 BuildMI(MBB, I, DL, get(SelOp), DstElt)
2251 .addReg(FalseReg, 0, SubIdx)
2252 .addReg(TrueReg, 0, SubIdx);
2253 preserveCondRegFlags(Select->getOperand(3), Cond[1]);
2254 fixImplicitOperands(*Select);
2255
2256 MIB.addReg(DstElt)
2257 .addImm(SubIdx);
2258 }
2259 }
2260
isFoldableCopy(const MachineInstr & MI) const2261 bool SIInstrInfo::isFoldableCopy(const MachineInstr &MI) const {
2262 switch (MI.getOpcode()) {
2263 case AMDGPU::V_MOV_B32_e32:
2264 case AMDGPU::V_MOV_B32_e64:
2265 case AMDGPU::V_MOV_B64_PSEUDO: {
2266 // If there are additional implicit register operands, this may be used for
2267 // register indexing so the source register operand isn't simply copied.
2268 unsigned NumOps = MI.getDesc().getNumOperands() +
2269 MI.getDesc().getNumImplicitUses();
2270
2271 return MI.getNumOperands() == NumOps;
2272 }
2273 case AMDGPU::S_MOV_B32:
2274 case AMDGPU::S_MOV_B64:
2275 case AMDGPU::COPY:
2276 case AMDGPU::V_ACCVGPR_WRITE_B32:
2277 case AMDGPU::V_ACCVGPR_READ_B32:
2278 return true;
2279 default:
2280 return false;
2281 }
2282 }
2283
getAddressSpaceForPseudoSourceKind(unsigned Kind) const2284 unsigned SIInstrInfo::getAddressSpaceForPseudoSourceKind(
2285 unsigned Kind) const {
2286 switch(Kind) {
2287 case PseudoSourceValue::Stack:
2288 case PseudoSourceValue::FixedStack:
2289 return AMDGPUAS::PRIVATE_ADDRESS;
2290 case PseudoSourceValue::ConstantPool:
2291 case PseudoSourceValue::GOT:
2292 case PseudoSourceValue::JumpTable:
2293 case PseudoSourceValue::GlobalValueCallEntry:
2294 case PseudoSourceValue::ExternalSymbolCallEntry:
2295 case PseudoSourceValue::TargetCustom:
2296 return AMDGPUAS::CONSTANT_ADDRESS;
2297 }
2298 return AMDGPUAS::FLAT_ADDRESS;
2299 }
2300
removeModOperands(MachineInstr & MI)2301 static void removeModOperands(MachineInstr &MI) {
2302 unsigned Opc = MI.getOpcode();
2303 int Src0ModIdx = AMDGPU::getNamedOperandIdx(Opc,
2304 AMDGPU::OpName::src0_modifiers);
2305 int Src1ModIdx = AMDGPU::getNamedOperandIdx(Opc,
2306 AMDGPU::OpName::src1_modifiers);
2307 int Src2ModIdx = AMDGPU::getNamedOperandIdx(Opc,
2308 AMDGPU::OpName::src2_modifiers);
2309
2310 MI.RemoveOperand(Src2ModIdx);
2311 MI.RemoveOperand(Src1ModIdx);
2312 MI.RemoveOperand(Src0ModIdx);
2313 }
2314
FoldImmediate(MachineInstr & UseMI,MachineInstr & DefMI,unsigned Reg,MachineRegisterInfo * MRI) const2315 bool SIInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2316 unsigned Reg, MachineRegisterInfo *MRI) const {
2317 if (!MRI->hasOneNonDBGUse(Reg))
2318 return false;
2319
2320 switch (DefMI.getOpcode()) {
2321 default:
2322 return false;
2323 case AMDGPU::S_MOV_B64:
2324 // TODO: We could fold 64-bit immediates, but this get compilicated
2325 // when there are sub-registers.
2326 return false;
2327
2328 case AMDGPU::V_MOV_B32_e32:
2329 case AMDGPU::S_MOV_B32:
2330 case AMDGPU::V_ACCVGPR_WRITE_B32:
2331 break;
2332 }
2333
2334 const MachineOperand *ImmOp = getNamedOperand(DefMI, AMDGPU::OpName::src0);
2335 assert(ImmOp);
2336 // FIXME: We could handle FrameIndex values here.
2337 if (!ImmOp->isImm())
2338 return false;
2339
2340 unsigned Opc = UseMI.getOpcode();
2341 if (Opc == AMDGPU::COPY) {
2342 bool isVGPRCopy = RI.isVGPR(*MRI, UseMI.getOperand(0).getReg());
2343 unsigned NewOpc = isVGPRCopy ? AMDGPU::V_MOV_B32_e32 : AMDGPU::S_MOV_B32;
2344 if (RI.isAGPR(*MRI, UseMI.getOperand(0).getReg())) {
2345 if (!isInlineConstant(*ImmOp, AMDGPU::OPERAND_REG_INLINE_AC_INT32))
2346 return false;
2347 NewOpc = AMDGPU::V_ACCVGPR_WRITE_B32;
2348 }
2349 UseMI.setDesc(get(NewOpc));
2350 UseMI.getOperand(1).ChangeToImmediate(ImmOp->getImm());
2351 UseMI.addImplicitDefUseOperands(*UseMI.getParent()->getParent());
2352 return true;
2353 }
2354
2355 if (Opc == AMDGPU::V_MAD_F32 || Opc == AMDGPU::V_MAC_F32_e64 ||
2356 Opc == AMDGPU::V_MAD_F16 || Opc == AMDGPU::V_MAC_F16_e64 ||
2357 Opc == AMDGPU::V_FMA_F32 || Opc == AMDGPU::V_FMAC_F32_e64 ||
2358 Opc == AMDGPU::V_FMA_F16 || Opc == AMDGPU::V_FMAC_F16_e64) {
2359 // Don't fold if we are using source or output modifiers. The new VOP2
2360 // instructions don't have them.
2361 if (hasAnyModifiersSet(UseMI))
2362 return false;
2363
2364 // If this is a free constant, there's no reason to do this.
2365 // TODO: We could fold this here instead of letting SIFoldOperands do it
2366 // later.
2367 MachineOperand *Src0 = getNamedOperand(UseMI, AMDGPU::OpName::src0);
2368
2369 // Any src operand can be used for the legality check.
2370 if (isInlineConstant(UseMI, *Src0, *ImmOp))
2371 return false;
2372
2373 bool IsF32 = Opc == AMDGPU::V_MAD_F32 || Opc == AMDGPU::V_MAC_F32_e64 ||
2374 Opc == AMDGPU::V_FMA_F32 || Opc == AMDGPU::V_FMAC_F32_e64;
2375 bool IsFMA = Opc == AMDGPU::V_FMA_F32 || Opc == AMDGPU::V_FMAC_F32_e64 ||
2376 Opc == AMDGPU::V_FMA_F16 || Opc == AMDGPU::V_FMAC_F16_e64;
2377 MachineOperand *Src1 = getNamedOperand(UseMI, AMDGPU::OpName::src1);
2378 MachineOperand *Src2 = getNamedOperand(UseMI, AMDGPU::OpName::src2);
2379
2380 // Multiplied part is the constant: Use v_madmk_{f16, f32}.
2381 // We should only expect these to be on src0 due to canonicalizations.
2382 if (Src0->isReg() && Src0->getReg() == Reg) {
2383 if (!Src1->isReg() || RI.isSGPRClass(MRI->getRegClass(Src1->getReg())))
2384 return false;
2385
2386 if (!Src2->isReg() || RI.isSGPRClass(MRI->getRegClass(Src2->getReg())))
2387 return false;
2388
2389 unsigned NewOpc =
2390 IsFMA ? (IsF32 ? AMDGPU::V_FMAMK_F32 : AMDGPU::V_FMAMK_F16)
2391 : (IsF32 ? AMDGPU::V_MADMK_F32 : AMDGPU::V_MADMK_F16);
2392 if (pseudoToMCOpcode(NewOpc) == -1)
2393 return false;
2394
2395 // We need to swap operands 0 and 1 since madmk constant is at operand 1.
2396
2397 const int64_t Imm = ImmOp->getImm();
2398
2399 // FIXME: This would be a lot easier if we could return a new instruction
2400 // instead of having to modify in place.
2401
2402 // Remove these first since they are at the end.
2403 UseMI.RemoveOperand(
2404 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod));
2405 UseMI.RemoveOperand(
2406 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp));
2407
2408 Register Src1Reg = Src1->getReg();
2409 unsigned Src1SubReg = Src1->getSubReg();
2410 Src0->setReg(Src1Reg);
2411 Src0->setSubReg(Src1SubReg);
2412 Src0->setIsKill(Src1->isKill());
2413
2414 if (Opc == AMDGPU::V_MAC_F32_e64 ||
2415 Opc == AMDGPU::V_MAC_F16_e64 ||
2416 Opc == AMDGPU::V_FMAC_F32_e64 ||
2417 Opc == AMDGPU::V_FMAC_F16_e64)
2418 UseMI.untieRegOperand(
2419 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
2420
2421 Src1->ChangeToImmediate(Imm);
2422
2423 removeModOperands(UseMI);
2424 UseMI.setDesc(get(NewOpc));
2425
2426 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
2427 if (DeleteDef)
2428 DefMI.eraseFromParent();
2429
2430 return true;
2431 }
2432
2433 // Added part is the constant: Use v_madak_{f16, f32}.
2434 if (Src2->isReg() && Src2->getReg() == Reg) {
2435 // Not allowed to use constant bus for another operand.
2436 // We can however allow an inline immediate as src0.
2437 bool Src0Inlined = false;
2438 if (Src0->isReg()) {
2439 // Try to inline constant if possible.
2440 // If the Def moves immediate and the use is single
2441 // We are saving VGPR here.
2442 MachineInstr *Def = MRI->getUniqueVRegDef(Src0->getReg());
2443 if (Def && Def->isMoveImmediate() &&
2444 isInlineConstant(Def->getOperand(1)) &&
2445 MRI->hasOneUse(Src0->getReg())) {
2446 Src0->ChangeToImmediate(Def->getOperand(1).getImm());
2447 Src0Inlined = true;
2448 } else if ((Register::isPhysicalRegister(Src0->getReg()) &&
2449 (ST.getConstantBusLimit(Opc) <= 1 &&
2450 RI.isSGPRClass(RI.getPhysRegClass(Src0->getReg())))) ||
2451 (Register::isVirtualRegister(Src0->getReg()) &&
2452 (ST.getConstantBusLimit(Opc) <= 1 &&
2453 RI.isSGPRClass(MRI->getRegClass(Src0->getReg())))))
2454 return false;
2455 // VGPR is okay as Src0 - fallthrough
2456 }
2457
2458 if (Src1->isReg() && !Src0Inlined ) {
2459 // We have one slot for inlinable constant so far - try to fill it
2460 MachineInstr *Def = MRI->getUniqueVRegDef(Src1->getReg());
2461 if (Def && Def->isMoveImmediate() &&
2462 isInlineConstant(Def->getOperand(1)) &&
2463 MRI->hasOneUse(Src1->getReg()) &&
2464 commuteInstruction(UseMI)) {
2465 Src0->ChangeToImmediate(Def->getOperand(1).getImm());
2466 } else if ((Register::isPhysicalRegister(Src1->getReg()) &&
2467 RI.isSGPRClass(RI.getPhysRegClass(Src1->getReg()))) ||
2468 (Register::isVirtualRegister(Src1->getReg()) &&
2469 RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))))
2470 return false;
2471 // VGPR is okay as Src1 - fallthrough
2472 }
2473
2474 unsigned NewOpc =
2475 IsFMA ? (IsF32 ? AMDGPU::V_FMAAK_F32 : AMDGPU::V_FMAAK_F16)
2476 : (IsF32 ? AMDGPU::V_MADAK_F32 : AMDGPU::V_MADAK_F16);
2477 if (pseudoToMCOpcode(NewOpc) == -1)
2478 return false;
2479
2480 const int64_t Imm = ImmOp->getImm();
2481
2482 // FIXME: This would be a lot easier if we could return a new instruction
2483 // instead of having to modify in place.
2484
2485 // Remove these first since they are at the end.
2486 UseMI.RemoveOperand(
2487 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod));
2488 UseMI.RemoveOperand(
2489 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp));
2490
2491 if (Opc == AMDGPU::V_MAC_F32_e64 ||
2492 Opc == AMDGPU::V_MAC_F16_e64 ||
2493 Opc == AMDGPU::V_FMAC_F32_e64 ||
2494 Opc == AMDGPU::V_FMAC_F16_e64)
2495 UseMI.untieRegOperand(
2496 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
2497
2498 // ChangingToImmediate adds Src2 back to the instruction.
2499 Src2->ChangeToImmediate(Imm);
2500
2501 // These come before src2.
2502 removeModOperands(UseMI);
2503 UseMI.setDesc(get(NewOpc));
2504 // It might happen that UseMI was commuted
2505 // and we now have SGPR as SRC1. If so 2 inlined
2506 // constant and SGPR are illegal.
2507 legalizeOperands(UseMI);
2508
2509 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
2510 if (DeleteDef)
2511 DefMI.eraseFromParent();
2512
2513 return true;
2514 }
2515 }
2516
2517 return false;
2518 }
2519
offsetsDoNotOverlap(int WidthA,int OffsetA,int WidthB,int OffsetB)2520 static bool offsetsDoNotOverlap(int WidthA, int OffsetA,
2521 int WidthB, int OffsetB) {
2522 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
2523 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
2524 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
2525 return LowOffset + LowWidth <= HighOffset;
2526 }
2527
checkInstOffsetsDoNotOverlap(const MachineInstr & MIa,const MachineInstr & MIb) const2528 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(const MachineInstr &MIa,
2529 const MachineInstr &MIb) const {
2530 const MachineOperand *BaseOp0, *BaseOp1;
2531 int64_t Offset0, Offset1;
2532
2533 if (getMemOperandWithOffset(MIa, BaseOp0, Offset0, &RI) &&
2534 getMemOperandWithOffset(MIb, BaseOp1, Offset1, &RI)) {
2535 if (!BaseOp0->isIdenticalTo(*BaseOp1))
2536 return false;
2537
2538 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) {
2539 // FIXME: Handle ds_read2 / ds_write2.
2540 return false;
2541 }
2542 unsigned Width0 = (*MIa.memoperands_begin())->getSize();
2543 unsigned Width1 = (*MIb.memoperands_begin())->getSize();
2544 if (offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1)) {
2545 return true;
2546 }
2547 }
2548
2549 return false;
2550 }
2551
areMemAccessesTriviallyDisjoint(const MachineInstr & MIa,const MachineInstr & MIb) const2552 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
2553 const MachineInstr &MIb) const {
2554 assert(MIa.mayLoadOrStore() &&
2555 "MIa must load from or modify a memory location");
2556 assert(MIb.mayLoadOrStore() &&
2557 "MIb must load from or modify a memory location");
2558
2559 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects())
2560 return false;
2561
2562 // XXX - Can we relax this between address spaces?
2563 if (MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
2564 return false;
2565
2566 // TODO: Should we check the address space from the MachineMemOperand? That
2567 // would allow us to distinguish objects we know don't alias based on the
2568 // underlying address space, even if it was lowered to a different one,
2569 // e.g. private accesses lowered to use MUBUF instructions on a scratch
2570 // buffer.
2571 if (isDS(MIa)) {
2572 if (isDS(MIb))
2573 return checkInstOffsetsDoNotOverlap(MIa, MIb);
2574
2575 return !isFLAT(MIb) || isSegmentSpecificFLAT(MIb);
2576 }
2577
2578 if (isMUBUF(MIa) || isMTBUF(MIa)) {
2579 if (isMUBUF(MIb) || isMTBUF(MIb))
2580 return checkInstOffsetsDoNotOverlap(MIa, MIb);
2581
2582 return !isFLAT(MIb) && !isSMRD(MIb);
2583 }
2584
2585 if (isSMRD(MIa)) {
2586 if (isSMRD(MIb))
2587 return checkInstOffsetsDoNotOverlap(MIa, MIb);
2588
2589 return !isFLAT(MIb) && !isMUBUF(MIa) && !isMTBUF(MIa);
2590 }
2591
2592 if (isFLAT(MIa)) {
2593 if (isFLAT(MIb))
2594 return checkInstOffsetsDoNotOverlap(MIa, MIb);
2595
2596 return false;
2597 }
2598
2599 return false;
2600 }
2601
getFoldableImm(const MachineOperand * MO)2602 static int64_t getFoldableImm(const MachineOperand* MO) {
2603 if (!MO->isReg())
2604 return false;
2605 const MachineFunction *MF = MO->getParent()->getParent()->getParent();
2606 const MachineRegisterInfo &MRI = MF->getRegInfo();
2607 auto Def = MRI.getUniqueVRegDef(MO->getReg());
2608 if (Def && Def->getOpcode() == AMDGPU::V_MOV_B32_e32 &&
2609 Def->getOperand(1).isImm())
2610 return Def->getOperand(1).getImm();
2611 return AMDGPU::NoRegister;
2612 }
2613
convertToThreeAddress(MachineFunction::iterator & MBB,MachineInstr & MI,LiveVariables * LV) const2614 MachineInstr *SIInstrInfo::convertToThreeAddress(MachineFunction::iterator &MBB,
2615 MachineInstr &MI,
2616 LiveVariables *LV) const {
2617 unsigned Opc = MI.getOpcode();
2618 bool IsF16 = false;
2619 bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F32_e64 ||
2620 Opc == AMDGPU::V_FMAC_F16_e32 || Opc == AMDGPU::V_FMAC_F16_e64;
2621
2622 switch (Opc) {
2623 default:
2624 return nullptr;
2625 case AMDGPU::V_MAC_F16_e64:
2626 case AMDGPU::V_FMAC_F16_e64:
2627 IsF16 = true;
2628 LLVM_FALLTHROUGH;
2629 case AMDGPU::V_MAC_F32_e64:
2630 case AMDGPU::V_FMAC_F32_e64:
2631 break;
2632 case AMDGPU::V_MAC_F16_e32:
2633 case AMDGPU::V_FMAC_F16_e32:
2634 IsF16 = true;
2635 LLVM_FALLTHROUGH;
2636 case AMDGPU::V_MAC_F32_e32:
2637 case AMDGPU::V_FMAC_F32_e32: {
2638 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
2639 AMDGPU::OpName::src0);
2640 const MachineOperand *Src0 = &MI.getOperand(Src0Idx);
2641 if (!Src0->isReg() && !Src0->isImm())
2642 return nullptr;
2643
2644 if (Src0->isImm() && !isInlineConstant(MI, Src0Idx, *Src0))
2645 return nullptr;
2646
2647 break;
2648 }
2649 }
2650
2651 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
2652 const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0);
2653 const MachineOperand *Src0Mods =
2654 getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
2655 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
2656 const MachineOperand *Src1Mods =
2657 getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
2658 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
2659 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp);
2660 const MachineOperand *Omod = getNamedOperand(MI, AMDGPU::OpName::omod);
2661
2662 if (!Src0Mods && !Src1Mods && !Clamp && !Omod &&
2663 // If we have an SGPR input, we will violate the constant bus restriction.
2664 (ST.getConstantBusLimit(Opc) > 1 ||
2665 !Src0->isReg() ||
2666 !RI.isSGPRReg(MBB->getParent()->getRegInfo(), Src0->getReg()))) {
2667 if (auto Imm = getFoldableImm(Src2)) {
2668 unsigned NewOpc =
2669 IsFMA ? (IsF16 ? AMDGPU::V_FMAAK_F16 : AMDGPU::V_FMAAK_F32)
2670 : (IsF16 ? AMDGPU::V_MADAK_F16 : AMDGPU::V_MADAK_F32);
2671 if (pseudoToMCOpcode(NewOpc) != -1)
2672 return BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
2673 .add(*Dst)
2674 .add(*Src0)
2675 .add(*Src1)
2676 .addImm(Imm);
2677 }
2678 unsigned NewOpc =
2679 IsFMA ? (IsF16 ? AMDGPU::V_FMAMK_F16 : AMDGPU::V_FMAMK_F32)
2680 : (IsF16 ? AMDGPU::V_MADMK_F16 : AMDGPU::V_MADMK_F32);
2681 if (auto Imm = getFoldableImm(Src1)) {
2682 if (pseudoToMCOpcode(NewOpc) != -1)
2683 return BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
2684 .add(*Dst)
2685 .add(*Src0)
2686 .addImm(Imm)
2687 .add(*Src2);
2688 }
2689 if (auto Imm = getFoldableImm(Src0)) {
2690 if (pseudoToMCOpcode(NewOpc) != -1 &&
2691 isOperandLegal(MI, AMDGPU::getNamedOperandIdx(NewOpc,
2692 AMDGPU::OpName::src0), Src1))
2693 return BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
2694 .add(*Dst)
2695 .add(*Src1)
2696 .addImm(Imm)
2697 .add(*Src2);
2698 }
2699 }
2700
2701 unsigned NewOpc = IsFMA ? (IsF16 ? AMDGPU::V_FMA_F16 : AMDGPU::V_FMA_F32)
2702 : (IsF16 ? AMDGPU::V_MAD_F16 : AMDGPU::V_MAD_F32);
2703 if (pseudoToMCOpcode(NewOpc) == -1)
2704 return nullptr;
2705
2706 return BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
2707 .add(*Dst)
2708 .addImm(Src0Mods ? Src0Mods->getImm() : 0)
2709 .add(*Src0)
2710 .addImm(Src1Mods ? Src1Mods->getImm() : 0)
2711 .add(*Src1)
2712 .addImm(0) // Src mods
2713 .add(*Src2)
2714 .addImm(Clamp ? Clamp->getImm() : 0)
2715 .addImm(Omod ? Omod->getImm() : 0);
2716 }
2717
2718 // It's not generally safe to move VALU instructions across these since it will
2719 // start using the register as a base index rather than directly.
2720 // XXX - Why isn't hasSideEffects sufficient for these?
changesVGPRIndexingMode(const MachineInstr & MI)2721 static bool changesVGPRIndexingMode(const MachineInstr &MI) {
2722 switch (MI.getOpcode()) {
2723 case AMDGPU::S_SET_GPR_IDX_ON:
2724 case AMDGPU::S_SET_GPR_IDX_MODE:
2725 case AMDGPU::S_SET_GPR_IDX_OFF:
2726 return true;
2727 default:
2728 return false;
2729 }
2730 }
2731
isSchedulingBoundary(const MachineInstr & MI,const MachineBasicBlock * MBB,const MachineFunction & MF) const2732 bool SIInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
2733 const MachineBasicBlock *MBB,
2734 const MachineFunction &MF) const {
2735 // XXX - Do we want the SP check in the base implementation?
2736
2737 // Target-independent instructions do not have an implicit-use of EXEC, even
2738 // when they operate on VGPRs. Treating EXEC modifications as scheduling
2739 // boundaries prevents incorrect movements of such instructions.
2740 return TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF) ||
2741 MI.modifiesRegister(AMDGPU::EXEC, &RI) ||
2742 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
2743 MI.getOpcode() == AMDGPU::S_SETREG_B32 ||
2744 MI.getOpcode() == AMDGPU::S_DENORM_MODE ||
2745 changesVGPRIndexingMode(MI);
2746 }
2747
isAlwaysGDS(uint16_t Opcode) const2748 bool SIInstrInfo::isAlwaysGDS(uint16_t Opcode) const {
2749 return Opcode == AMDGPU::DS_ORDERED_COUNT ||
2750 Opcode == AMDGPU::DS_GWS_INIT ||
2751 Opcode == AMDGPU::DS_GWS_SEMA_V ||
2752 Opcode == AMDGPU::DS_GWS_SEMA_BR ||
2753 Opcode == AMDGPU::DS_GWS_SEMA_P ||
2754 Opcode == AMDGPU::DS_GWS_SEMA_RELEASE_ALL ||
2755 Opcode == AMDGPU::DS_GWS_BARRIER;
2756 }
2757
hasUnwantedEffectsWhenEXECEmpty(const MachineInstr & MI) const2758 bool SIInstrInfo::hasUnwantedEffectsWhenEXECEmpty(const MachineInstr &MI) const {
2759 unsigned Opcode = MI.getOpcode();
2760
2761 if (MI.mayStore() && isSMRD(MI))
2762 return true; // scalar store or atomic
2763
2764 // This will terminate the function when other lanes may need to continue.
2765 if (MI.isReturn())
2766 return true;
2767
2768 // These instructions cause shader I/O that may cause hardware lockups
2769 // when executed with an empty EXEC mask.
2770 //
2771 // Note: exp with VM = DONE = 0 is automatically skipped by hardware when
2772 // EXEC = 0, but checking for that case here seems not worth it
2773 // given the typical code patterns.
2774 if (Opcode == AMDGPU::S_SENDMSG || Opcode == AMDGPU::S_SENDMSGHALT ||
2775 Opcode == AMDGPU::EXP || Opcode == AMDGPU::EXP_DONE ||
2776 Opcode == AMDGPU::DS_ORDERED_COUNT || Opcode == AMDGPU::S_TRAP ||
2777 Opcode == AMDGPU::DS_GWS_INIT || Opcode == AMDGPU::DS_GWS_BARRIER)
2778 return true;
2779
2780 if (MI.isCall() || MI.isInlineAsm())
2781 return true; // conservative assumption
2782
2783 // These are like SALU instructions in terms of effects, so it's questionable
2784 // whether we should return true for those.
2785 //
2786 // However, executing them with EXEC = 0 causes them to operate on undefined
2787 // data, which we avoid by returning true here.
2788 if (Opcode == AMDGPU::V_READFIRSTLANE_B32 || Opcode == AMDGPU::V_READLANE_B32)
2789 return true;
2790
2791 return false;
2792 }
2793
mayReadEXEC(const MachineRegisterInfo & MRI,const MachineInstr & MI) const2794 bool SIInstrInfo::mayReadEXEC(const MachineRegisterInfo &MRI,
2795 const MachineInstr &MI) const {
2796 if (MI.isMetaInstruction())
2797 return false;
2798
2799 // This won't read exec if this is an SGPR->SGPR copy.
2800 if (MI.isCopyLike()) {
2801 if (!RI.isSGPRReg(MRI, MI.getOperand(0).getReg()))
2802 return true;
2803
2804 // Make sure this isn't copying exec as a normal operand
2805 return MI.readsRegister(AMDGPU::EXEC, &RI);
2806 }
2807
2808 // Make a conservative assumption about the callee.
2809 if (MI.isCall())
2810 return true;
2811
2812 // Be conservative with any unhandled generic opcodes.
2813 if (!isTargetSpecificOpcode(MI.getOpcode()))
2814 return true;
2815
2816 return !isSALU(MI) || MI.readsRegister(AMDGPU::EXEC, &RI);
2817 }
2818
isInlineConstant(const APInt & Imm) const2819 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const {
2820 switch (Imm.getBitWidth()) {
2821 case 1: // This likely will be a condition code mask.
2822 return true;
2823
2824 case 32:
2825 return AMDGPU::isInlinableLiteral32(Imm.getSExtValue(),
2826 ST.hasInv2PiInlineImm());
2827 case 64:
2828 return AMDGPU::isInlinableLiteral64(Imm.getSExtValue(),
2829 ST.hasInv2PiInlineImm());
2830 case 16:
2831 return ST.has16BitInsts() &&
2832 AMDGPU::isInlinableLiteral16(Imm.getSExtValue(),
2833 ST.hasInv2PiInlineImm());
2834 default:
2835 llvm_unreachable("invalid bitwidth");
2836 }
2837 }
2838
isInlineConstant(const MachineOperand & MO,uint8_t OperandType) const2839 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO,
2840 uint8_t OperandType) const {
2841 if (!MO.isImm() ||
2842 OperandType < AMDGPU::OPERAND_SRC_FIRST ||
2843 OperandType > AMDGPU::OPERAND_SRC_LAST)
2844 return false;
2845
2846 // MachineOperand provides no way to tell the true operand size, since it only
2847 // records a 64-bit value. We need to know the size to determine if a 32-bit
2848 // floating point immediate bit pattern is legal for an integer immediate. It
2849 // would be for any 32-bit integer operand, but would not be for a 64-bit one.
2850
2851 int64_t Imm = MO.getImm();
2852 switch (OperandType) {
2853 case AMDGPU::OPERAND_REG_IMM_INT32:
2854 case AMDGPU::OPERAND_REG_IMM_FP32:
2855 case AMDGPU::OPERAND_REG_INLINE_C_INT32:
2856 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
2857 case AMDGPU::OPERAND_REG_INLINE_AC_INT32:
2858 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: {
2859 int32_t Trunc = static_cast<int32_t>(Imm);
2860 return AMDGPU::isInlinableLiteral32(Trunc, ST.hasInv2PiInlineImm());
2861 }
2862 case AMDGPU::OPERAND_REG_IMM_INT64:
2863 case AMDGPU::OPERAND_REG_IMM_FP64:
2864 case AMDGPU::OPERAND_REG_INLINE_C_INT64:
2865 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
2866 return AMDGPU::isInlinableLiteral64(MO.getImm(),
2867 ST.hasInv2PiInlineImm());
2868 case AMDGPU::OPERAND_REG_IMM_INT16:
2869 case AMDGPU::OPERAND_REG_IMM_FP16:
2870 case AMDGPU::OPERAND_REG_INLINE_C_INT16:
2871 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
2872 case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
2873 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: {
2874 if (isInt<16>(Imm) || isUInt<16>(Imm)) {
2875 // A few special case instructions have 16-bit operands on subtargets
2876 // where 16-bit instructions are not legal.
2877 // TODO: Do the 32-bit immediates work? We shouldn't really need to handle
2878 // constants in these cases
2879 int16_t Trunc = static_cast<int16_t>(Imm);
2880 return ST.has16BitInsts() &&
2881 AMDGPU::isInlinableLiteral16(Trunc, ST.hasInv2PiInlineImm());
2882 }
2883
2884 return false;
2885 }
2886 case AMDGPU::OPERAND_REG_IMM_V2INT16:
2887 case AMDGPU::OPERAND_REG_IMM_V2FP16:
2888 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
2889 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
2890 case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16:
2891 case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16: {
2892 uint32_t Trunc = static_cast<uint32_t>(Imm);
2893 return AMDGPU::isInlinableLiteralV216(Trunc, ST.hasInv2PiInlineImm());
2894 }
2895 default:
2896 llvm_unreachable("invalid bitwidth");
2897 }
2898 }
2899
isLiteralConstantLike(const MachineOperand & MO,const MCOperandInfo & OpInfo) const2900 bool SIInstrInfo::isLiteralConstantLike(const MachineOperand &MO,
2901 const MCOperandInfo &OpInfo) const {
2902 switch (MO.getType()) {
2903 case MachineOperand::MO_Register:
2904 return false;
2905 case MachineOperand::MO_Immediate:
2906 return !isInlineConstant(MO, OpInfo);
2907 case MachineOperand::MO_FrameIndex:
2908 case MachineOperand::MO_MachineBasicBlock:
2909 case MachineOperand::MO_ExternalSymbol:
2910 case MachineOperand::MO_GlobalAddress:
2911 case MachineOperand::MO_MCSymbol:
2912 return true;
2913 default:
2914 llvm_unreachable("unexpected operand type");
2915 }
2916 }
2917
compareMachineOp(const MachineOperand & Op0,const MachineOperand & Op1)2918 static bool compareMachineOp(const MachineOperand &Op0,
2919 const MachineOperand &Op1) {
2920 if (Op0.getType() != Op1.getType())
2921 return false;
2922
2923 switch (Op0.getType()) {
2924 case MachineOperand::MO_Register:
2925 return Op0.getReg() == Op1.getReg();
2926 case MachineOperand::MO_Immediate:
2927 return Op0.getImm() == Op1.getImm();
2928 default:
2929 llvm_unreachable("Didn't expect to be comparing these operand types");
2930 }
2931 }
2932
isImmOperandLegal(const MachineInstr & MI,unsigned OpNo,const MachineOperand & MO) const2933 bool SIInstrInfo::isImmOperandLegal(const MachineInstr &MI, unsigned OpNo,
2934 const MachineOperand &MO) const {
2935 const MCInstrDesc &InstDesc = MI.getDesc();
2936 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpNo];
2937
2938 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal());
2939
2940 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE)
2941 return true;
2942
2943 if (OpInfo.RegClass < 0)
2944 return false;
2945
2946 const MachineFunction *MF = MI.getParent()->getParent();
2947 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
2948
2949 if (MO.isImm() && isInlineConstant(MO, OpInfo)) {
2950 if (isMAI(MI) && ST.hasMFMAInlineLiteralBug() &&
2951 OpNo ==(unsigned)AMDGPU::getNamedOperandIdx(MI.getOpcode(),
2952 AMDGPU::OpName::src2))
2953 return false;
2954 return RI.opCanUseInlineConstant(OpInfo.OperandType);
2955 }
2956
2957 if (!RI.opCanUseLiteralConstant(OpInfo.OperandType))
2958 return false;
2959
2960 if (!isVOP3(MI) || !AMDGPU::isSISrcOperand(InstDesc, OpNo))
2961 return true;
2962
2963 return ST.hasVOP3Literal();
2964 }
2965
hasVALU32BitEncoding(unsigned Opcode) const2966 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const {
2967 int Op32 = AMDGPU::getVOPe32(Opcode);
2968 if (Op32 == -1)
2969 return false;
2970
2971 return pseudoToMCOpcode(Op32) != -1;
2972 }
2973
hasModifiers(unsigned Opcode) const2974 bool SIInstrInfo::hasModifiers(unsigned Opcode) const {
2975 // The src0_modifier operand is present on all instructions
2976 // that have modifiers.
2977
2978 return AMDGPU::getNamedOperandIdx(Opcode,
2979 AMDGPU::OpName::src0_modifiers) != -1;
2980 }
2981
hasModifiersSet(const MachineInstr & MI,unsigned OpName) const2982 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI,
2983 unsigned OpName) const {
2984 const MachineOperand *Mods = getNamedOperand(MI, OpName);
2985 return Mods && Mods->getImm();
2986 }
2987
hasAnyModifiersSet(const MachineInstr & MI) const2988 bool SIInstrInfo::hasAnyModifiersSet(const MachineInstr &MI) const {
2989 return hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
2990 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
2991 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers) ||
2992 hasModifiersSet(MI, AMDGPU::OpName::clamp) ||
2993 hasModifiersSet(MI, AMDGPU::OpName::omod);
2994 }
2995
canShrink(const MachineInstr & MI,const MachineRegisterInfo & MRI) const2996 bool SIInstrInfo::canShrink(const MachineInstr &MI,
2997 const MachineRegisterInfo &MRI) const {
2998 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
2999 // Can't shrink instruction with three operands.
3000 // FIXME: v_cndmask_b32 has 3 operands and is shrinkable, but we need to add
3001 // a special case for it. It can only be shrunk if the third operand
3002 // is vcc, and src0_modifiers and src1_modifiers are not set.
3003 // We should handle this the same way we handle vopc, by addding
3004 // a register allocation hint pre-regalloc and then do the shrinking
3005 // post-regalloc.
3006 if (Src2) {
3007 switch (MI.getOpcode()) {
3008 default: return false;
3009
3010 case AMDGPU::V_ADDC_U32_e64:
3011 case AMDGPU::V_SUBB_U32_e64:
3012 case AMDGPU::V_SUBBREV_U32_e64: {
3013 const MachineOperand *Src1
3014 = getNamedOperand(MI, AMDGPU::OpName::src1);
3015 if (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()))
3016 return false;
3017 // Additional verification is needed for sdst/src2.
3018 return true;
3019 }
3020 case AMDGPU::V_MAC_F32_e64:
3021 case AMDGPU::V_MAC_F16_e64:
3022 case AMDGPU::V_FMAC_F32_e64:
3023 case AMDGPU::V_FMAC_F16_e64:
3024 if (!Src2->isReg() || !RI.isVGPR(MRI, Src2->getReg()) ||
3025 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers))
3026 return false;
3027 break;
3028
3029 case AMDGPU::V_CNDMASK_B32_e64:
3030 break;
3031 }
3032 }
3033
3034 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
3035 if (Src1 && (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()) ||
3036 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers)))
3037 return false;
3038
3039 // We don't need to check src0, all input types are legal, so just make sure
3040 // src0 isn't using any modifiers.
3041 if (hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers))
3042 return false;
3043
3044 // Can it be shrunk to a valid 32 bit opcode?
3045 if (!hasVALU32BitEncoding(MI.getOpcode()))
3046 return false;
3047
3048 // Check output modifiers
3049 return !hasModifiersSet(MI, AMDGPU::OpName::omod) &&
3050 !hasModifiersSet(MI, AMDGPU::OpName::clamp);
3051 }
3052
3053 // Set VCC operand with all flags from \p Orig, except for setting it as
3054 // implicit.
copyFlagsToImplicitVCC(MachineInstr & MI,const MachineOperand & Orig)3055 static void copyFlagsToImplicitVCC(MachineInstr &MI,
3056 const MachineOperand &Orig) {
3057
3058 for (MachineOperand &Use : MI.implicit_operands()) {
3059 if (Use.isUse() && Use.getReg() == AMDGPU::VCC) {
3060 Use.setIsUndef(Orig.isUndef());
3061 Use.setIsKill(Orig.isKill());
3062 return;
3063 }
3064 }
3065 }
3066
buildShrunkInst(MachineInstr & MI,unsigned Op32) const3067 MachineInstr *SIInstrInfo::buildShrunkInst(MachineInstr &MI,
3068 unsigned Op32) const {
3069 MachineBasicBlock *MBB = MI.getParent();;
3070 MachineInstrBuilder Inst32 =
3071 BuildMI(*MBB, MI, MI.getDebugLoc(), get(Op32));
3072
3073 // Add the dst operand if the 32-bit encoding also has an explicit $vdst.
3074 // For VOPC instructions, this is replaced by an implicit def of vcc.
3075 int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst);
3076 if (Op32DstIdx != -1) {
3077 // dst
3078 Inst32.add(MI.getOperand(0));
3079 } else {
3080 assert(((MI.getOperand(0).getReg() == AMDGPU::VCC) ||
3081 (MI.getOperand(0).getReg() == AMDGPU::VCC_LO)) &&
3082 "Unexpected case");
3083 }
3084
3085 Inst32.add(*getNamedOperand(MI, AMDGPU::OpName::src0));
3086
3087 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
3088 if (Src1)
3089 Inst32.add(*Src1);
3090
3091 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
3092
3093 if (Src2) {
3094 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2);
3095 if (Op32Src2Idx != -1) {
3096 Inst32.add(*Src2);
3097 } else {
3098 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
3099 // replaced with an implicit read of vcc. This was already added
3100 // during the initial BuildMI, so find it to preserve the flags.
3101 copyFlagsToImplicitVCC(*Inst32, *Src2);
3102 }
3103 }
3104
3105 return Inst32;
3106 }
3107
usesConstantBus(const MachineRegisterInfo & MRI,const MachineOperand & MO,const MCOperandInfo & OpInfo) const3108 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI,
3109 const MachineOperand &MO,
3110 const MCOperandInfo &OpInfo) const {
3111 // Literal constants use the constant bus.
3112 //if (isLiteralConstantLike(MO, OpInfo))
3113 // return true;
3114 if (MO.isImm())
3115 return !isInlineConstant(MO, OpInfo);
3116
3117 if (!MO.isReg())
3118 return true; // Misc other operands like FrameIndex
3119
3120 if (!MO.isUse())
3121 return false;
3122
3123 if (Register::isVirtualRegister(MO.getReg()))
3124 return RI.isSGPRClass(MRI.getRegClass(MO.getReg()));
3125
3126 // Null is free
3127 if (MO.getReg() == AMDGPU::SGPR_NULL)
3128 return false;
3129
3130 // SGPRs use the constant bus
3131 if (MO.isImplicit()) {
3132 return MO.getReg() == AMDGPU::M0 ||
3133 MO.getReg() == AMDGPU::VCC ||
3134 MO.getReg() == AMDGPU::VCC_LO;
3135 } else {
3136 return AMDGPU::SReg_32RegClass.contains(MO.getReg()) ||
3137 AMDGPU::SReg_64RegClass.contains(MO.getReg());
3138 }
3139 }
3140
findImplicitSGPRRead(const MachineInstr & MI)3141 static unsigned findImplicitSGPRRead(const MachineInstr &MI) {
3142 for (const MachineOperand &MO : MI.implicit_operands()) {
3143 // We only care about reads.
3144 if (MO.isDef())
3145 continue;
3146
3147 switch (MO.getReg()) {
3148 case AMDGPU::VCC:
3149 case AMDGPU::VCC_LO:
3150 case AMDGPU::VCC_HI:
3151 case AMDGPU::M0:
3152 case AMDGPU::FLAT_SCR:
3153 return MO.getReg();
3154
3155 default:
3156 break;
3157 }
3158 }
3159
3160 return AMDGPU::NoRegister;
3161 }
3162
shouldReadExec(const MachineInstr & MI)3163 static bool shouldReadExec(const MachineInstr &MI) {
3164 if (SIInstrInfo::isVALU(MI)) {
3165 switch (MI.getOpcode()) {
3166 case AMDGPU::V_READLANE_B32:
3167 case AMDGPU::V_READLANE_B32_gfx6_gfx7:
3168 case AMDGPU::V_READLANE_B32_gfx10:
3169 case AMDGPU::V_READLANE_B32_vi:
3170 case AMDGPU::V_WRITELANE_B32:
3171 case AMDGPU::V_WRITELANE_B32_gfx6_gfx7:
3172 case AMDGPU::V_WRITELANE_B32_gfx10:
3173 case AMDGPU::V_WRITELANE_B32_vi:
3174 return false;
3175 }
3176
3177 return true;
3178 }
3179
3180 if (MI.isPreISelOpcode() ||
3181 SIInstrInfo::isGenericOpcode(MI.getOpcode()) ||
3182 SIInstrInfo::isSALU(MI) ||
3183 SIInstrInfo::isSMRD(MI))
3184 return false;
3185
3186 return true;
3187 }
3188
isSubRegOf(const SIRegisterInfo & TRI,const MachineOperand & SuperVec,const MachineOperand & SubReg)3189 static bool isSubRegOf(const SIRegisterInfo &TRI,
3190 const MachineOperand &SuperVec,
3191 const MachineOperand &SubReg) {
3192 if (Register::isPhysicalRegister(SubReg.getReg()))
3193 return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg());
3194
3195 return SubReg.getSubReg() != AMDGPU::NoSubRegister &&
3196 SubReg.getReg() == SuperVec.getReg();
3197 }
3198
verifyInstruction(const MachineInstr & MI,StringRef & ErrInfo) const3199 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI,
3200 StringRef &ErrInfo) const {
3201 uint16_t Opcode = MI.getOpcode();
3202 if (SIInstrInfo::isGenericOpcode(MI.getOpcode()))
3203 return true;
3204
3205 const MachineFunction *MF = MI.getParent()->getParent();
3206 const MachineRegisterInfo &MRI = MF->getRegInfo();
3207
3208 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
3209 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
3210 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
3211
3212 // Make sure the number of operands is correct.
3213 const MCInstrDesc &Desc = get(Opcode);
3214 if (!Desc.isVariadic() &&
3215 Desc.getNumOperands() != MI.getNumExplicitOperands()) {
3216 ErrInfo = "Instruction has wrong number of operands.";
3217 return false;
3218 }
3219
3220 if (MI.isInlineAsm()) {
3221 // Verify register classes for inlineasm constraints.
3222 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands();
3223 I != E; ++I) {
3224 const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI);
3225 if (!RC)
3226 continue;
3227
3228 const MachineOperand &Op = MI.getOperand(I);
3229 if (!Op.isReg())
3230 continue;
3231
3232 Register Reg = Op.getReg();
3233 if (!Register::isVirtualRegister(Reg) && !RC->contains(Reg)) {
3234 ErrInfo = "inlineasm operand has incorrect register class.";
3235 return false;
3236 }
3237 }
3238
3239 return true;
3240 }
3241
3242 // Make sure the register classes are correct.
3243 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) {
3244 if (MI.getOperand(i).isFPImm()) {
3245 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast "
3246 "all fp values to integers.";
3247 return false;
3248 }
3249
3250 int RegClass = Desc.OpInfo[i].RegClass;
3251
3252 switch (Desc.OpInfo[i].OperandType) {
3253 case MCOI::OPERAND_REGISTER:
3254 if (MI.getOperand(i).isImm() || MI.getOperand(i).isGlobal()) {
3255 ErrInfo = "Illegal immediate value for operand.";
3256 return false;
3257 }
3258 break;
3259 case AMDGPU::OPERAND_REG_IMM_INT32:
3260 case AMDGPU::OPERAND_REG_IMM_FP32:
3261 break;
3262 case AMDGPU::OPERAND_REG_INLINE_C_INT32:
3263 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
3264 case AMDGPU::OPERAND_REG_INLINE_C_INT64:
3265 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
3266 case AMDGPU::OPERAND_REG_INLINE_C_INT16:
3267 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
3268 case AMDGPU::OPERAND_REG_INLINE_AC_INT32:
3269 case AMDGPU::OPERAND_REG_INLINE_AC_FP32:
3270 case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
3271 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: {
3272 const MachineOperand &MO = MI.getOperand(i);
3273 if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) {
3274 ErrInfo = "Illegal immediate value for operand.";
3275 return false;
3276 }
3277 break;
3278 }
3279 case MCOI::OPERAND_IMMEDIATE:
3280 case AMDGPU::OPERAND_KIMM32:
3281 // Check if this operand is an immediate.
3282 // FrameIndex operands will be replaced by immediates, so they are
3283 // allowed.
3284 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) {
3285 ErrInfo = "Expected immediate, but got non-immediate";
3286 return false;
3287 }
3288 LLVM_FALLTHROUGH;
3289 default:
3290 continue;
3291 }
3292
3293 if (!MI.getOperand(i).isReg())
3294 continue;
3295
3296 if (RegClass != -1) {
3297 Register Reg = MI.getOperand(i).getReg();
3298 if (Reg == AMDGPU::NoRegister || Register::isVirtualRegister(Reg))
3299 continue;
3300
3301 const TargetRegisterClass *RC = RI.getRegClass(RegClass);
3302 if (!RC->contains(Reg)) {
3303 ErrInfo = "Operand has incorrect register class.";
3304 return false;
3305 }
3306 }
3307 }
3308
3309 // Verify SDWA
3310 if (isSDWA(MI)) {
3311 if (!ST.hasSDWA()) {
3312 ErrInfo = "SDWA is not supported on this target";
3313 return false;
3314 }
3315
3316 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst);
3317
3318 const int OpIndicies[] = { DstIdx, Src0Idx, Src1Idx, Src2Idx };
3319
3320 for (int OpIdx: OpIndicies) {
3321 if (OpIdx == -1)
3322 continue;
3323 const MachineOperand &MO = MI.getOperand(OpIdx);
3324
3325 if (!ST.hasSDWAScalar()) {
3326 // Only VGPRS on VI
3327 if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) {
3328 ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI";
3329 return false;
3330 }
3331 } else {
3332 // No immediates on GFX9
3333 if (!MO.isReg()) {
3334 ErrInfo = "Only reg allowed as operands in SDWA instructions on GFX9";
3335 return false;
3336 }
3337 }
3338 }
3339
3340 if (!ST.hasSDWAOmod()) {
3341 // No omod allowed on VI
3342 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
3343 if (OMod != nullptr &&
3344 (!OMod->isImm() || OMod->getImm() != 0)) {
3345 ErrInfo = "OMod not allowed in SDWA instructions on VI";
3346 return false;
3347 }
3348 }
3349
3350 uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode);
3351 if (isVOPC(BasicOpcode)) {
3352 if (!ST.hasSDWASdst() && DstIdx != -1) {
3353 // Only vcc allowed as dst on VI for VOPC
3354 const MachineOperand &Dst = MI.getOperand(DstIdx);
3355 if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) {
3356 ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI";
3357 return false;
3358 }
3359 } else if (!ST.hasSDWAOutModsVOPC()) {
3360 // No clamp allowed on GFX9 for VOPC
3361 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp);
3362 if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) {
3363 ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI";
3364 return false;
3365 }
3366
3367 // No omod allowed on GFX9 for VOPC
3368 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
3369 if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) {
3370 ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI";
3371 return false;
3372 }
3373 }
3374 }
3375
3376 const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused);
3377 if (DstUnused && DstUnused->isImm() &&
3378 DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) {
3379 const MachineOperand &Dst = MI.getOperand(DstIdx);
3380 if (!Dst.isReg() || !Dst.isTied()) {
3381 ErrInfo = "Dst register should have tied register";
3382 return false;
3383 }
3384
3385 const MachineOperand &TiedMO =
3386 MI.getOperand(MI.findTiedOperandIdx(DstIdx));
3387 if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) {
3388 ErrInfo =
3389 "Dst register should be tied to implicit use of preserved register";
3390 return false;
3391 } else if (Register::isPhysicalRegister(TiedMO.getReg()) &&
3392 Dst.getReg() != TiedMO.getReg()) {
3393 ErrInfo = "Dst register should use same physical register as preserved";
3394 return false;
3395 }
3396 }
3397 }
3398
3399 // Verify MIMG
3400 if (isMIMG(MI.getOpcode()) && !MI.mayStore()) {
3401 // Ensure that the return type used is large enough for all the options
3402 // being used TFE/LWE require an extra result register.
3403 const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask);
3404 if (DMask) {
3405 uint64_t DMaskImm = DMask->getImm();
3406 uint32_t RegCount =
3407 isGather4(MI.getOpcode()) ? 4 : countPopulation(DMaskImm);
3408 const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe);
3409 const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe);
3410 const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16);
3411
3412 // Adjust for packed 16 bit values
3413 if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem())
3414 RegCount >>= 1;
3415
3416 // Adjust if using LWE or TFE
3417 if ((LWE && LWE->getImm()) || (TFE && TFE->getImm()))
3418 RegCount += 1;
3419
3420 const uint32_t DstIdx =
3421 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata);
3422 const MachineOperand &Dst = MI.getOperand(DstIdx);
3423 if (Dst.isReg()) {
3424 const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx);
3425 uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32;
3426 if (RegCount > DstSize) {
3427 ErrInfo = "MIMG instruction returns too many registers for dst "
3428 "register class";
3429 return false;
3430 }
3431 }
3432 }
3433 }
3434
3435 // Verify VOP*. Ignore multiple sgpr operands on writelane.
3436 if (Desc.getOpcode() != AMDGPU::V_WRITELANE_B32
3437 && (isVOP1(MI) || isVOP2(MI) || isVOP3(MI) || isVOPC(MI) || isSDWA(MI))) {
3438 // Only look at the true operands. Only a real operand can use the constant
3439 // bus, and we don't want to check pseudo-operands like the source modifier
3440 // flags.
3441 const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx };
3442
3443 unsigned ConstantBusCount = 0;
3444 unsigned LiteralCount = 0;
3445
3446 if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1)
3447 ++ConstantBusCount;
3448
3449 SmallVector<unsigned, 2> SGPRsUsed;
3450 unsigned SGPRUsed = findImplicitSGPRRead(MI);
3451 if (SGPRUsed != AMDGPU::NoRegister) {
3452 ++ConstantBusCount;
3453 SGPRsUsed.push_back(SGPRUsed);
3454 }
3455
3456 for (int OpIdx : OpIndices) {
3457 if (OpIdx == -1)
3458 break;
3459 const MachineOperand &MO = MI.getOperand(OpIdx);
3460 if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) {
3461 if (MO.isReg()) {
3462 SGPRUsed = MO.getReg();
3463 if (llvm::all_of(SGPRsUsed, [this, SGPRUsed](unsigned SGPR) {
3464 return !RI.regsOverlap(SGPRUsed, SGPR);
3465 })) {
3466 ++ConstantBusCount;
3467 SGPRsUsed.push_back(SGPRUsed);
3468 }
3469 } else {
3470 ++ConstantBusCount;
3471 ++LiteralCount;
3472 }
3473 }
3474 }
3475 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
3476 // v_writelane_b32 is an exception from constant bus restriction:
3477 // vsrc0 can be sgpr, const or m0 and lane select sgpr, m0 or inline-const
3478 if (ConstantBusCount > ST.getConstantBusLimit(Opcode) &&
3479 Opcode != AMDGPU::V_WRITELANE_B32) {
3480 ErrInfo = "VOP* instruction violates constant bus restriction";
3481 return false;
3482 }
3483
3484 if (isVOP3(MI) && LiteralCount) {
3485 if (LiteralCount && !ST.hasVOP3Literal()) {
3486 ErrInfo = "VOP3 instruction uses literal";
3487 return false;
3488 }
3489 if (LiteralCount > 1) {
3490 ErrInfo = "VOP3 instruction uses more than one literal";
3491 return false;
3492 }
3493 }
3494 }
3495
3496 // Special case for writelane - this can break the multiple constant bus rule,
3497 // but still can't use more than one SGPR register
3498 if (Desc.getOpcode() == AMDGPU::V_WRITELANE_B32) {
3499 unsigned SGPRCount = 0;
3500 Register SGPRUsed = AMDGPU::NoRegister;
3501
3502 for (int OpIdx : {Src0Idx, Src1Idx, Src2Idx}) {
3503 if (OpIdx == -1)
3504 break;
3505
3506 const MachineOperand &MO = MI.getOperand(OpIdx);
3507
3508 if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) {
3509 if (MO.isReg() && MO.getReg() != AMDGPU::M0) {
3510 if (MO.getReg() != SGPRUsed)
3511 ++SGPRCount;
3512 SGPRUsed = MO.getReg();
3513 }
3514 }
3515 if (SGPRCount > ST.getConstantBusLimit(Opcode)) {
3516 ErrInfo = "WRITELANE instruction violates constant bus restriction";
3517 return false;
3518 }
3519 }
3520 }
3521
3522 // Verify misc. restrictions on specific instructions.
3523 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32 ||
3524 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64) {
3525 const MachineOperand &Src0 = MI.getOperand(Src0Idx);
3526 const MachineOperand &Src1 = MI.getOperand(Src1Idx);
3527 const MachineOperand &Src2 = MI.getOperand(Src2Idx);
3528 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) {
3529 if (!compareMachineOp(Src0, Src1) &&
3530 !compareMachineOp(Src0, Src2)) {
3531 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2";
3532 return false;
3533 }
3534 }
3535 }
3536
3537 if (isSOP2(MI) || isSOPC(MI)) {
3538 const MachineOperand &Src0 = MI.getOperand(Src0Idx);
3539 const MachineOperand &Src1 = MI.getOperand(Src1Idx);
3540 unsigned Immediates = 0;
3541
3542 if (!Src0.isReg() &&
3543 !isInlineConstant(Src0, Desc.OpInfo[Src0Idx].OperandType))
3544 Immediates++;
3545 if (!Src1.isReg() &&
3546 !isInlineConstant(Src1, Desc.OpInfo[Src1Idx].OperandType))
3547 Immediates++;
3548
3549 if (Immediates > 1) {
3550 ErrInfo = "SOP2/SOPC instruction requires too many immediate constants";
3551 return false;
3552 }
3553 }
3554
3555 if (isSOPK(MI)) {
3556 auto Op = getNamedOperand(MI, AMDGPU::OpName::simm16);
3557 if (Desc.isBranch()) {
3558 if (!Op->isMBB()) {
3559 ErrInfo = "invalid branch target for SOPK instruction";
3560 return false;
3561 }
3562 } else {
3563 uint64_t Imm = Op->getImm();
3564 if (sopkIsZext(MI)) {
3565 if (!isUInt<16>(Imm)) {
3566 ErrInfo = "invalid immediate for SOPK instruction";
3567 return false;
3568 }
3569 } else {
3570 if (!isInt<16>(Imm)) {
3571 ErrInfo = "invalid immediate for SOPK instruction";
3572 return false;
3573 }
3574 }
3575 }
3576 }
3577
3578 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 ||
3579 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 ||
3580 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
3581 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) {
3582 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
3583 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64;
3584
3585 const unsigned StaticNumOps = Desc.getNumOperands() +
3586 Desc.getNumImplicitUses();
3587 const unsigned NumImplicitOps = IsDst ? 2 : 1;
3588
3589 // Allow additional implicit operands. This allows a fixup done by the post
3590 // RA scheduler where the main implicit operand is killed and implicit-defs
3591 // are added for sub-registers that remain live after this instruction.
3592 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) {
3593 ErrInfo = "missing implicit register operands";
3594 return false;
3595 }
3596
3597 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
3598 if (IsDst) {
3599 if (!Dst->isUse()) {
3600 ErrInfo = "v_movreld_b32 vdst should be a use operand";
3601 return false;
3602 }
3603
3604 unsigned UseOpIdx;
3605 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) ||
3606 UseOpIdx != StaticNumOps + 1) {
3607 ErrInfo = "movrel implicit operands should be tied";
3608 return false;
3609 }
3610 }
3611
3612 const MachineOperand &Src0 = MI.getOperand(Src0Idx);
3613 const MachineOperand &ImpUse
3614 = MI.getOperand(StaticNumOps + NumImplicitOps - 1);
3615 if (!ImpUse.isReg() || !ImpUse.isUse() ||
3616 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) {
3617 ErrInfo = "src0 should be subreg of implicit vector use";
3618 return false;
3619 }
3620 }
3621
3622 // Make sure we aren't losing exec uses in the td files. This mostly requires
3623 // being careful when using let Uses to try to add other use registers.
3624 if (shouldReadExec(MI)) {
3625 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) {
3626 ErrInfo = "VALU instruction does not implicitly read exec mask";
3627 return false;
3628 }
3629 }
3630
3631 if (isSMRD(MI)) {
3632 if (MI.mayStore()) {
3633 // The register offset form of scalar stores may only use m0 as the
3634 // soffset register.
3635 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soff);
3636 if (Soff && Soff->getReg() != AMDGPU::M0) {
3637 ErrInfo = "scalar stores must use m0 as offset register";
3638 return false;
3639 }
3640 }
3641 }
3642
3643 if (isFLAT(MI) && !MF->getSubtarget<GCNSubtarget>().hasFlatInstOffsets()) {
3644 const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
3645 if (Offset->getImm() != 0) {
3646 ErrInfo = "subtarget does not support offsets in flat instructions";
3647 return false;
3648 }
3649 }
3650
3651 if (isMIMG(MI)) {
3652 const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim);
3653 if (DimOp) {
3654 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode,
3655 AMDGPU::OpName::vaddr0);
3656 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
3657 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode);
3658 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
3659 AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode);
3660 const AMDGPU::MIMGDimInfo *Dim =
3661 AMDGPU::getMIMGDimInfoByEncoding(DimOp->getImm());
3662
3663 if (!Dim) {
3664 ErrInfo = "dim is out of range";
3665 return false;
3666 }
3667
3668 bool IsNSA = SRsrcIdx - VAddr0Idx > 1;
3669 unsigned AddrWords = BaseOpcode->NumExtraArgs +
3670 (BaseOpcode->Gradients ? Dim->NumGradients : 0) +
3671 (BaseOpcode->Coordinates ? Dim->NumCoords : 0) +
3672 (BaseOpcode->LodOrClampOrMip ? 1 : 0);
3673
3674 unsigned VAddrWords;
3675 if (IsNSA) {
3676 VAddrWords = SRsrcIdx - VAddr0Idx;
3677 } else {
3678 const TargetRegisterClass *RC = getOpRegClass(MI, VAddr0Idx);
3679 VAddrWords = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 32;
3680 if (AddrWords > 8)
3681 AddrWords = 16;
3682 else if (AddrWords > 4)
3683 AddrWords = 8;
3684 else if (AddrWords == 3 && VAddrWords == 4) {
3685 // CodeGen uses the V4 variant of instructions for three addresses,
3686 // because the selection DAG does not support non-power-of-two types.
3687 AddrWords = 4;
3688 }
3689 }
3690
3691 if (VAddrWords != AddrWords) {
3692 ErrInfo = "bad vaddr size";
3693 return false;
3694 }
3695 }
3696 }
3697
3698 const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl);
3699 if (DppCt) {
3700 using namespace AMDGPU::DPP;
3701
3702 unsigned DC = DppCt->getImm();
3703 if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 ||
3704 DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST ||
3705 (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) ||
3706 (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) ||
3707 (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) ||
3708 (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) ||
3709 (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) {
3710 ErrInfo = "Invalid dpp_ctrl value";
3711 return false;
3712 }
3713 if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 &&
3714 ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
3715 ErrInfo = "Invalid dpp_ctrl value: "
3716 "wavefront shifts are not supported on GFX10+";
3717 return false;
3718 }
3719 if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 &&
3720 ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
3721 ErrInfo = "Invalid dpp_ctrl value: "
3722 "broadcasts are not supported on GFX10+";
3723 return false;
3724 }
3725 if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST &&
3726 ST.getGeneration() < AMDGPUSubtarget::GFX10) {
3727 ErrInfo = "Invalid dpp_ctrl value: "
3728 "row_share and row_xmask are not supported before GFX10";
3729 return false;
3730 }
3731 }
3732
3733 return true;
3734 }
3735
getVALUOp(const MachineInstr & MI) const3736 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const {
3737 switch (MI.getOpcode()) {
3738 default: return AMDGPU::INSTRUCTION_LIST_END;
3739 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE;
3740 case AMDGPU::COPY: return AMDGPU::COPY;
3741 case AMDGPU::PHI: return AMDGPU::PHI;
3742 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG;
3743 case AMDGPU::WQM: return AMDGPU::WQM;
3744 case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM;
3745 case AMDGPU::WWM: return AMDGPU::WWM;
3746 case AMDGPU::S_MOV_B32: {
3747 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3748 return MI.getOperand(1).isReg() ||
3749 RI.isAGPR(MRI, MI.getOperand(0).getReg()) ?
3750 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32;
3751 }
3752 case AMDGPU::S_ADD_I32:
3753 return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_I32_e32;
3754 case AMDGPU::S_ADDC_U32:
3755 return AMDGPU::V_ADDC_U32_e32;
3756 case AMDGPU::S_SUB_I32:
3757 return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32;
3758 // FIXME: These are not consistently handled, and selected when the carry is
3759 // used.
3760 case AMDGPU::S_ADD_U32:
3761 return AMDGPU::V_ADD_I32_e32;
3762 case AMDGPU::S_SUB_U32:
3763 return AMDGPU::V_SUB_I32_e32;
3764 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32;
3765 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32;
3766 case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32;
3767 case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32;
3768 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64;
3769 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64;
3770 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64;
3771 case AMDGPU::S_XNOR_B32:
3772 return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END;
3773 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64;
3774 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64;
3775 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64;
3776 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64;
3777 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32;
3778 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64;
3779 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32;
3780 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64;
3781 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32;
3782 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64;
3783 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32;
3784 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32;
3785 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32;
3786 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32;
3787 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64;
3788 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32;
3789 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32;
3790 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32;
3791 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32;
3792 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32;
3793 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32;
3794 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32;
3795 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32;
3796 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32;
3797 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e32;
3798 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e32;
3799 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e32;
3800 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e32;
3801 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e32;
3802 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e32;
3803 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e32;
3804 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e32;
3805 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64;
3806 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32;
3807 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32;
3808 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64;
3809 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ;
3810 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ;
3811 }
3812 llvm_unreachable(
3813 "Unexpected scalar opcode without corresponding vector one!");
3814 }
3815
getOpRegClass(const MachineInstr & MI,unsigned OpNo) const3816 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI,
3817 unsigned OpNo) const {
3818 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3819 const MCInstrDesc &Desc = get(MI.getOpcode());
3820 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
3821 Desc.OpInfo[OpNo].RegClass == -1) {
3822 Register Reg = MI.getOperand(OpNo).getReg();
3823
3824 if (Register::isVirtualRegister(Reg))
3825 return MRI.getRegClass(Reg);
3826 return RI.getPhysRegClass(Reg);
3827 }
3828
3829 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
3830 return RI.getRegClass(RCID);
3831 }
3832
legalizeOpWithMove(MachineInstr & MI,unsigned OpIdx) const3833 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const {
3834 MachineBasicBlock::iterator I = MI;
3835 MachineBasicBlock *MBB = MI.getParent();
3836 MachineOperand &MO = MI.getOperand(OpIdx);
3837 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
3838 const SIRegisterInfo *TRI =
3839 static_cast<const SIRegisterInfo*>(MRI.getTargetRegisterInfo());
3840 unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass;
3841 const TargetRegisterClass *RC = RI.getRegClass(RCID);
3842 unsigned Size = TRI->getRegSizeInBits(*RC);
3843 unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO : AMDGPU::V_MOV_B32_e32;
3844 if (MO.isReg())
3845 Opcode = AMDGPU::COPY;
3846 else if (RI.isSGPRClass(RC))
3847 Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32;
3848
3849 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
3850 if (RI.getCommonSubClass(&AMDGPU::VReg_64RegClass, VRC))
3851 VRC = &AMDGPU::VReg_64RegClass;
3852 else
3853 VRC = &AMDGPU::VGPR_32RegClass;
3854
3855 Register Reg = MRI.createVirtualRegister(VRC);
3856 DebugLoc DL = MBB->findDebugLoc(I);
3857 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO);
3858 MO.ChangeToRegister(Reg, false);
3859 }
3860
buildExtractSubReg(MachineBasicBlock::iterator MI,MachineRegisterInfo & MRI,MachineOperand & SuperReg,const TargetRegisterClass * SuperRC,unsigned SubIdx,const TargetRegisterClass * SubRC) const3861 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI,
3862 MachineRegisterInfo &MRI,
3863 MachineOperand &SuperReg,
3864 const TargetRegisterClass *SuperRC,
3865 unsigned SubIdx,
3866 const TargetRegisterClass *SubRC)
3867 const {
3868 MachineBasicBlock *MBB = MI->getParent();
3869 DebugLoc DL = MI->getDebugLoc();
3870 Register SubReg = MRI.createVirtualRegister(SubRC);
3871
3872 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) {
3873 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
3874 .addReg(SuperReg.getReg(), 0, SubIdx);
3875 return SubReg;
3876 }
3877
3878 // Just in case the super register is itself a sub-register, copy it to a new
3879 // value so we don't need to worry about merging its subreg index with the
3880 // SubIdx passed to this function. The register coalescer should be able to
3881 // eliminate this extra copy.
3882 Register NewSuperReg = MRI.createVirtualRegister(SuperRC);
3883
3884 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg)
3885 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg());
3886
3887 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
3888 .addReg(NewSuperReg, 0, SubIdx);
3889
3890 return SubReg;
3891 }
3892
buildExtractSubRegOrImm(MachineBasicBlock::iterator MII,MachineRegisterInfo & MRI,MachineOperand & Op,const TargetRegisterClass * SuperRC,unsigned SubIdx,const TargetRegisterClass * SubRC) const3893 MachineOperand SIInstrInfo::buildExtractSubRegOrImm(
3894 MachineBasicBlock::iterator MII,
3895 MachineRegisterInfo &MRI,
3896 MachineOperand &Op,
3897 const TargetRegisterClass *SuperRC,
3898 unsigned SubIdx,
3899 const TargetRegisterClass *SubRC) const {
3900 if (Op.isImm()) {
3901 if (SubIdx == AMDGPU::sub0)
3902 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm()));
3903 if (SubIdx == AMDGPU::sub1)
3904 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32));
3905
3906 llvm_unreachable("Unhandled register index for immediate");
3907 }
3908
3909 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
3910 SubIdx, SubRC);
3911 return MachineOperand::CreateReg(SubReg, false);
3912 }
3913
3914 // Change the order of operands from (0, 1, 2) to (0, 2, 1)
swapOperands(MachineInstr & Inst) const3915 void SIInstrInfo::swapOperands(MachineInstr &Inst) const {
3916 assert(Inst.getNumExplicitOperands() == 3);
3917 MachineOperand Op1 = Inst.getOperand(1);
3918 Inst.RemoveOperand(1);
3919 Inst.addOperand(Op1);
3920 }
3921
isLegalRegOperand(const MachineRegisterInfo & MRI,const MCOperandInfo & OpInfo,const MachineOperand & MO) const3922 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI,
3923 const MCOperandInfo &OpInfo,
3924 const MachineOperand &MO) const {
3925 if (!MO.isReg())
3926 return false;
3927
3928 Register Reg = MO.getReg();
3929 const TargetRegisterClass *RC = Register::isVirtualRegister(Reg)
3930 ? MRI.getRegClass(Reg)
3931 : RI.getPhysRegClass(Reg);
3932
3933 const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass);
3934 if (MO.getSubReg()) {
3935 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
3936 const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF);
3937 if (!SuperRC)
3938 return false;
3939
3940 DRC = RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg());
3941 if (!DRC)
3942 return false;
3943 }
3944 return RC->hasSuperClassEq(DRC);
3945 }
3946
isLegalVSrcOperand(const MachineRegisterInfo & MRI,const MCOperandInfo & OpInfo,const MachineOperand & MO) const3947 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI,
3948 const MCOperandInfo &OpInfo,
3949 const MachineOperand &MO) const {
3950 if (MO.isReg())
3951 return isLegalRegOperand(MRI, OpInfo, MO);
3952
3953 // Handle non-register types that are treated like immediates.
3954 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal());
3955 return true;
3956 }
3957
isOperandLegal(const MachineInstr & MI,unsigned OpIdx,const MachineOperand * MO) const3958 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx,
3959 const MachineOperand *MO) const {
3960 const MachineFunction &MF = *MI.getParent()->getParent();
3961 const MachineRegisterInfo &MRI = MF.getRegInfo();
3962 const MCInstrDesc &InstDesc = MI.getDesc();
3963 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx];
3964 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
3965 const TargetRegisterClass *DefinedRC =
3966 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr;
3967 if (!MO)
3968 MO = &MI.getOperand(OpIdx);
3969
3970 int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode());
3971 int VOP3LiteralLimit = ST.hasVOP3Literal() ? 1 : 0;
3972 if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) {
3973 if (isVOP3(MI) && isLiteralConstantLike(*MO, OpInfo) && !VOP3LiteralLimit--)
3974 return false;
3975
3976 SmallDenseSet<RegSubRegPair> SGPRsUsed;
3977 if (MO->isReg())
3978 SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg()));
3979
3980 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
3981 if (i == OpIdx)
3982 continue;
3983 const MachineOperand &Op = MI.getOperand(i);
3984 if (Op.isReg()) {
3985 RegSubRegPair SGPR(Op.getReg(), Op.getSubReg());
3986 if (!SGPRsUsed.count(SGPR) &&
3987 usesConstantBus(MRI, Op, InstDesc.OpInfo[i])) {
3988 if (--ConstantBusLimit <= 0)
3989 return false;
3990 SGPRsUsed.insert(SGPR);
3991 }
3992 } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) {
3993 if (--ConstantBusLimit <= 0)
3994 return false;
3995 } else if (isVOP3(MI) && AMDGPU::isSISrcOperand(InstDesc, i) &&
3996 isLiteralConstantLike(Op, InstDesc.OpInfo[i])) {
3997 if (!VOP3LiteralLimit--)
3998 return false;
3999 if (--ConstantBusLimit <= 0)
4000 return false;
4001 }
4002 }
4003 }
4004
4005 if (MO->isReg()) {
4006 assert(DefinedRC);
4007 return isLegalRegOperand(MRI, OpInfo, *MO);
4008 }
4009
4010 // Handle non-register types that are treated like immediates.
4011 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal());
4012
4013 if (!DefinedRC) {
4014 // This operand expects an immediate.
4015 return true;
4016 }
4017
4018 return isImmOperandLegal(MI, OpIdx, *MO);
4019 }
4020
legalizeOperandsVOP2(MachineRegisterInfo & MRI,MachineInstr & MI) const4021 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI,
4022 MachineInstr &MI) const {
4023 unsigned Opc = MI.getOpcode();
4024 const MCInstrDesc &InstrDesc = get(Opc);
4025
4026 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
4027 MachineOperand &Src0 = MI.getOperand(Src0Idx);
4028
4029 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
4030 MachineOperand &Src1 = MI.getOperand(Src1Idx);
4031
4032 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32
4033 // we need to only have one constant bus use before GFX10.
4034 bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister;
4035 if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 &&
4036 Src0.isReg() && (RI.isSGPRReg(MRI, Src0.getReg()) ||
4037 isLiteralConstantLike(Src0, InstrDesc.OpInfo[Src0Idx])))
4038 legalizeOpWithMove(MI, Src0Idx);
4039
4040 // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for
4041 // both the value to write (src0) and lane select (src1). Fix up non-SGPR
4042 // src0/src1 with V_READFIRSTLANE.
4043 if (Opc == AMDGPU::V_WRITELANE_B32) {
4044 const DebugLoc &DL = MI.getDebugLoc();
4045 if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) {
4046 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
4047 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
4048 .add(Src0);
4049 Src0.ChangeToRegister(Reg, false);
4050 }
4051 if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) {
4052 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
4053 const DebugLoc &DL = MI.getDebugLoc();
4054 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
4055 .add(Src1);
4056 Src1.ChangeToRegister(Reg, false);
4057 }
4058 return;
4059 }
4060
4061 // No VOP2 instructions support AGPRs.
4062 if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg()))
4063 legalizeOpWithMove(MI, Src0Idx);
4064
4065 if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg()))
4066 legalizeOpWithMove(MI, Src1Idx);
4067
4068 // VOP2 src0 instructions support all operand types, so we don't need to check
4069 // their legality. If src1 is already legal, we don't need to do anything.
4070 if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1))
4071 return;
4072
4073 // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for
4074 // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane
4075 // select is uniform.
4076 if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() &&
4077 RI.isVGPR(MRI, Src1.getReg())) {
4078 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
4079 const DebugLoc &DL = MI.getDebugLoc();
4080 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
4081 .add(Src1);
4082 Src1.ChangeToRegister(Reg, false);
4083 return;
4084 }
4085
4086 // We do not use commuteInstruction here because it is too aggressive and will
4087 // commute if it is possible. We only want to commute here if it improves
4088 // legality. This can be called a fairly large number of times so don't waste
4089 // compile time pointlessly swapping and checking legality again.
4090 if (HasImplicitSGPR || !MI.isCommutable()) {
4091 legalizeOpWithMove(MI, Src1Idx);
4092 return;
4093 }
4094
4095 // If src0 can be used as src1, commuting will make the operands legal.
4096 // Otherwise we have to give up and insert a move.
4097 //
4098 // TODO: Other immediate-like operand kinds could be commuted if there was a
4099 // MachineOperand::ChangeTo* for them.
4100 if ((!Src1.isImm() && !Src1.isReg()) ||
4101 !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) {
4102 legalizeOpWithMove(MI, Src1Idx);
4103 return;
4104 }
4105
4106 int CommutedOpc = commuteOpcode(MI);
4107 if (CommutedOpc == -1) {
4108 legalizeOpWithMove(MI, Src1Idx);
4109 return;
4110 }
4111
4112 MI.setDesc(get(CommutedOpc));
4113
4114 Register Src0Reg = Src0.getReg();
4115 unsigned Src0SubReg = Src0.getSubReg();
4116 bool Src0Kill = Src0.isKill();
4117
4118 if (Src1.isImm())
4119 Src0.ChangeToImmediate(Src1.getImm());
4120 else if (Src1.isReg()) {
4121 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill());
4122 Src0.setSubReg(Src1.getSubReg());
4123 } else
4124 llvm_unreachable("Should only have register or immediate operands");
4125
4126 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill);
4127 Src1.setSubReg(Src0SubReg);
4128 fixImplicitOperands(MI);
4129 }
4130
4131 // Legalize VOP3 operands. All operand types are supported for any operand
4132 // but only one literal constant and only starting from GFX10.
legalizeOperandsVOP3(MachineRegisterInfo & MRI,MachineInstr & MI) const4133 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI,
4134 MachineInstr &MI) const {
4135 unsigned Opc = MI.getOpcode();
4136
4137 int VOP3Idx[3] = {
4138 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0),
4139 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1),
4140 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)
4141 };
4142
4143 if (Opc == AMDGPU::V_PERMLANE16_B32 ||
4144 Opc == AMDGPU::V_PERMLANEX16_B32) {
4145 // src1 and src2 must be scalar
4146 MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]);
4147 MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]);
4148 const DebugLoc &DL = MI.getDebugLoc();
4149 if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) {
4150 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
4151 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
4152 .add(Src1);
4153 Src1.ChangeToRegister(Reg, false);
4154 }
4155 if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) {
4156 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
4157 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
4158 .add(Src2);
4159 Src2.ChangeToRegister(Reg, false);
4160 }
4161 }
4162
4163 // Find the one SGPR operand we are allowed to use.
4164 int ConstantBusLimit = ST.getConstantBusLimit(Opc);
4165 int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0;
4166 SmallDenseSet<unsigned> SGPRsUsed;
4167 unsigned SGPRReg = findUsedSGPR(MI, VOP3Idx);
4168 if (SGPRReg != AMDGPU::NoRegister) {
4169 SGPRsUsed.insert(SGPRReg);
4170 --ConstantBusLimit;
4171 }
4172
4173 for (unsigned i = 0; i < 3; ++i) {
4174 int Idx = VOP3Idx[i];
4175 if (Idx == -1)
4176 break;
4177 MachineOperand &MO = MI.getOperand(Idx);
4178
4179 if (!MO.isReg()) {
4180 if (!isLiteralConstantLike(MO, get(Opc).OpInfo[Idx]))
4181 continue;
4182
4183 if (LiteralLimit > 0 && ConstantBusLimit > 0) {
4184 --LiteralLimit;
4185 --ConstantBusLimit;
4186 continue;
4187 }
4188
4189 --LiteralLimit;
4190 --ConstantBusLimit;
4191 legalizeOpWithMove(MI, Idx);
4192 continue;
4193 }
4194
4195 if (RI.hasAGPRs(MRI.getRegClass(MO.getReg())) &&
4196 !isOperandLegal(MI, Idx, &MO)) {
4197 legalizeOpWithMove(MI, Idx);
4198 continue;
4199 }
4200
4201 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg())))
4202 continue; // VGPRs are legal
4203
4204 // We can use one SGPR in each VOP3 instruction prior to GFX10
4205 // and two starting from GFX10.
4206 if (SGPRsUsed.count(MO.getReg()))
4207 continue;
4208 if (ConstantBusLimit > 0) {
4209 SGPRsUsed.insert(MO.getReg());
4210 --ConstantBusLimit;
4211 continue;
4212 }
4213
4214 // If we make it this far, then the operand is not legal and we must
4215 // legalize it.
4216 legalizeOpWithMove(MI, Idx);
4217 }
4218 }
4219
readlaneVGPRToSGPR(unsigned SrcReg,MachineInstr & UseMI,MachineRegisterInfo & MRI) const4220 unsigned SIInstrInfo::readlaneVGPRToSGPR(unsigned SrcReg, MachineInstr &UseMI,
4221 MachineRegisterInfo &MRI) const {
4222 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg);
4223 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC);
4224 Register DstReg = MRI.createVirtualRegister(SRC);
4225 unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32;
4226
4227 if (RI.hasAGPRs(VRC)) {
4228 VRC = RI.getEquivalentVGPRClass(VRC);
4229 Register NewSrcReg = MRI.createVirtualRegister(VRC);
4230 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
4231 get(TargetOpcode::COPY), NewSrcReg)
4232 .addReg(SrcReg);
4233 SrcReg = NewSrcReg;
4234 }
4235
4236 if (SubRegs == 1) {
4237 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
4238 get(AMDGPU::V_READFIRSTLANE_B32), DstReg)
4239 .addReg(SrcReg);
4240 return DstReg;
4241 }
4242
4243 SmallVector<unsigned, 8> SRegs;
4244 for (unsigned i = 0; i < SubRegs; ++i) {
4245 Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
4246 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
4247 get(AMDGPU::V_READFIRSTLANE_B32), SGPR)
4248 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i));
4249 SRegs.push_back(SGPR);
4250 }
4251
4252 MachineInstrBuilder MIB =
4253 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
4254 get(AMDGPU::REG_SEQUENCE), DstReg);
4255 for (unsigned i = 0; i < SubRegs; ++i) {
4256 MIB.addReg(SRegs[i]);
4257 MIB.addImm(RI.getSubRegFromChannel(i));
4258 }
4259 return DstReg;
4260 }
4261
legalizeOperandsSMRD(MachineRegisterInfo & MRI,MachineInstr & MI) const4262 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI,
4263 MachineInstr &MI) const {
4264
4265 // If the pointer is store in VGPRs, then we need to move them to
4266 // SGPRs using v_readfirstlane. This is safe because we only select
4267 // loads with uniform pointers to SMRD instruction so we know the
4268 // pointer value is uniform.
4269 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase);
4270 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) {
4271 unsigned SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI);
4272 SBase->setReg(SGPR);
4273 }
4274 MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soff);
4275 if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) {
4276 unsigned SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI);
4277 SOff->setReg(SGPR);
4278 }
4279 }
4280
legalizeGenericOperand(MachineBasicBlock & InsertMBB,MachineBasicBlock::iterator I,const TargetRegisterClass * DstRC,MachineOperand & Op,MachineRegisterInfo & MRI,const DebugLoc & DL) const4281 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB,
4282 MachineBasicBlock::iterator I,
4283 const TargetRegisterClass *DstRC,
4284 MachineOperand &Op,
4285 MachineRegisterInfo &MRI,
4286 const DebugLoc &DL) const {
4287 Register OpReg = Op.getReg();
4288 unsigned OpSubReg = Op.getSubReg();
4289
4290 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg(
4291 RI.getRegClassForReg(MRI, OpReg), OpSubReg);
4292
4293 // Check if operand is already the correct register class.
4294 if (DstRC == OpRC)
4295 return;
4296
4297 Register DstReg = MRI.createVirtualRegister(DstRC);
4298 MachineInstr *Copy =
4299 BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op);
4300
4301 Op.setReg(DstReg);
4302 Op.setSubReg(0);
4303
4304 MachineInstr *Def = MRI.getVRegDef(OpReg);
4305 if (!Def)
4306 return;
4307
4308 // Try to eliminate the copy if it is copying an immediate value.
4309 if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass)
4310 FoldImmediate(*Copy, *Def, OpReg, &MRI);
4311
4312 bool ImpDef = Def->isImplicitDef();
4313 while (!ImpDef && Def && Def->isCopy()) {
4314 if (Def->getOperand(1).getReg().isPhysical())
4315 break;
4316 Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg());
4317 ImpDef = Def && Def->isImplicitDef();
4318 }
4319 if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) &&
4320 !ImpDef)
4321 Copy->addOperand(MachineOperand::CreateReg(AMDGPU::EXEC, false, true));
4322 }
4323
4324 // Emit the actual waterfall loop, executing the wrapped instruction for each
4325 // unique value of \p Rsrc across all lanes. In the best case we execute 1
4326 // iteration, in the worst case we execute 64 (once per lane).
4327 static void
emitLoadSRsrcFromVGPRLoop(const SIInstrInfo & TII,MachineRegisterInfo & MRI,MachineBasicBlock & OrigBB,MachineBasicBlock & LoopBB,const DebugLoc & DL,MachineOperand & Rsrc)4328 emitLoadSRsrcFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI,
4329 MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB,
4330 const DebugLoc &DL, MachineOperand &Rsrc) {
4331 MachineFunction &MF = *OrigBB.getParent();
4332 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
4333 const SIRegisterInfo *TRI = ST.getRegisterInfo();
4334 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
4335 unsigned SaveExecOpc =
4336 ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64;
4337 unsigned XorTermOpc =
4338 ST.isWave32() ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term;
4339 unsigned AndOpc =
4340 ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
4341 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
4342
4343 MachineBasicBlock::iterator I = LoopBB.begin();
4344
4345 Register VRsrc = Rsrc.getReg();
4346 unsigned VRsrcUndef = getUndefRegState(Rsrc.isUndef());
4347
4348 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
4349 Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC);
4350 Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC);
4351 Register AndCond = MRI.createVirtualRegister(BoolXExecRC);
4352 Register SRsrcSub0 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
4353 Register SRsrcSub1 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
4354 Register SRsrcSub2 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
4355 Register SRsrcSub3 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
4356 Register SRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass);
4357
4358 // Beginning of the loop, read the next Rsrc variant.
4359 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub0)
4360 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub0);
4361 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub1)
4362 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub1);
4363 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub2)
4364 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub2);
4365 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub3)
4366 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub3);
4367
4368 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SRsrc)
4369 .addReg(SRsrcSub0)
4370 .addImm(AMDGPU::sub0)
4371 .addReg(SRsrcSub1)
4372 .addImm(AMDGPU::sub1)
4373 .addReg(SRsrcSub2)
4374 .addImm(AMDGPU::sub2)
4375 .addReg(SRsrcSub3)
4376 .addImm(AMDGPU::sub3);
4377
4378 // Update Rsrc operand to use the SGPR Rsrc.
4379 Rsrc.setReg(SRsrc);
4380 Rsrc.setIsKill(true);
4381
4382 // Identify all lanes with identical Rsrc operands in their VGPRs.
4383 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), CondReg0)
4384 .addReg(SRsrc, 0, AMDGPU::sub0_sub1)
4385 .addReg(VRsrc, 0, AMDGPU::sub0_sub1);
4386 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), CondReg1)
4387 .addReg(SRsrc, 0, AMDGPU::sub2_sub3)
4388 .addReg(VRsrc, 0, AMDGPU::sub2_sub3);
4389 BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndCond)
4390 .addReg(CondReg0)
4391 .addReg(CondReg1);
4392
4393 MRI.setSimpleHint(SaveExec, AndCond);
4394
4395 // Update EXEC to matching lanes, saving original to SaveExec.
4396 BuildMI(LoopBB, I, DL, TII.get(SaveExecOpc), SaveExec)
4397 .addReg(AndCond, RegState::Kill);
4398
4399 // The original instruction is here; we insert the terminators after it.
4400 I = LoopBB.end();
4401
4402 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
4403 BuildMI(LoopBB, I, DL, TII.get(XorTermOpc), Exec)
4404 .addReg(Exec)
4405 .addReg(SaveExec);
4406 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::S_CBRANCH_EXECNZ)).addMBB(&LoopBB);
4407 }
4408
4409 // Build a waterfall loop around \p MI, replacing the VGPR \p Rsrc register
4410 // with SGPRs by iterating over all unique values across all lanes.
loadSRsrcFromVGPR(const SIInstrInfo & TII,MachineInstr & MI,MachineOperand & Rsrc,MachineDominatorTree * MDT)4411 static void loadSRsrcFromVGPR(const SIInstrInfo &TII, MachineInstr &MI,
4412 MachineOperand &Rsrc, MachineDominatorTree *MDT) {
4413 MachineBasicBlock &MBB = *MI.getParent();
4414 MachineFunction &MF = *MBB.getParent();
4415 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
4416 const SIRegisterInfo *TRI = ST.getRegisterInfo();
4417 MachineRegisterInfo &MRI = MF.getRegInfo();
4418 MachineBasicBlock::iterator I(&MI);
4419 const DebugLoc &DL = MI.getDebugLoc();
4420 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
4421 unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
4422 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
4423
4424 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
4425
4426 // Save the EXEC mask
4427 BuildMI(MBB, I, DL, TII.get(MovExecOpc), SaveExec).addReg(Exec);
4428
4429 // Killed uses in the instruction we are waterfalling around will be
4430 // incorrect due to the added control-flow.
4431 for (auto &MO : MI.uses()) {
4432 if (MO.isReg() && MO.isUse()) {
4433 MRI.clearKillFlags(MO.getReg());
4434 }
4435 }
4436
4437 // To insert the loop we need to split the block. Move everything after this
4438 // point to a new block, and insert a new empty block between the two.
4439 MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock();
4440 MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock();
4441 MachineFunction::iterator MBBI(MBB);
4442 ++MBBI;
4443
4444 MF.insert(MBBI, LoopBB);
4445 MF.insert(MBBI, RemainderBB);
4446
4447 LoopBB->addSuccessor(LoopBB);
4448 LoopBB->addSuccessor(RemainderBB);
4449
4450 // Move MI to the LoopBB, and the remainder of the block to RemainderBB.
4451 MachineBasicBlock::iterator J = I++;
4452 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
4453 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
4454 LoopBB->splice(LoopBB->begin(), &MBB, J);
4455
4456 MBB.addSuccessor(LoopBB);
4457
4458 // Update dominators. We know that MBB immediately dominates LoopBB, that
4459 // LoopBB immediately dominates RemainderBB, and that RemainderBB immediately
4460 // dominates all of the successors transferred to it from MBB that MBB used
4461 // to properly dominate.
4462 if (MDT) {
4463 MDT->addNewBlock(LoopBB, &MBB);
4464 MDT->addNewBlock(RemainderBB, LoopBB);
4465 for (auto &Succ : RemainderBB->successors()) {
4466 if (MDT->properlyDominates(&MBB, Succ)) {
4467 MDT->changeImmediateDominator(Succ, RemainderBB);
4468 }
4469 }
4470 }
4471
4472 emitLoadSRsrcFromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, Rsrc);
4473
4474 // Restore the EXEC mask
4475 MachineBasicBlock::iterator First = RemainderBB->begin();
4476 BuildMI(*RemainderBB, First, DL, TII.get(MovExecOpc), Exec).addReg(SaveExec);
4477 }
4478
4479 // Extract pointer from Rsrc and return a zero-value Rsrc replacement.
4480 static std::tuple<unsigned, unsigned>
extractRsrcPtr(const SIInstrInfo & TII,MachineInstr & MI,MachineOperand & Rsrc)4481 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) {
4482 MachineBasicBlock &MBB = *MI.getParent();
4483 MachineFunction &MF = *MBB.getParent();
4484 MachineRegisterInfo &MRI = MF.getRegInfo();
4485
4486 // Extract the ptr from the resource descriptor.
4487 unsigned RsrcPtr =
4488 TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass,
4489 AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass);
4490
4491 // Create an empty resource descriptor
4492 Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
4493 Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
4494 Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
4495 Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass);
4496 uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat();
4497
4498 // Zero64 = 0
4499 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64)
4500 .addImm(0);
4501
4502 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
4503 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo)
4504 .addImm(RsrcDataFormat & 0xFFFFFFFF);
4505
4506 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
4507 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi)
4508 .addImm(RsrcDataFormat >> 32);
4509
4510 // NewSRsrc = {Zero64, SRsrcFormat}
4511 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc)
4512 .addReg(Zero64)
4513 .addImm(AMDGPU::sub0_sub1)
4514 .addReg(SRsrcFormatLo)
4515 .addImm(AMDGPU::sub2)
4516 .addReg(SRsrcFormatHi)
4517 .addImm(AMDGPU::sub3);
4518
4519 return std::make_tuple(RsrcPtr, NewSRsrc);
4520 }
4521
legalizeOperands(MachineInstr & MI,MachineDominatorTree * MDT) const4522 void SIInstrInfo::legalizeOperands(MachineInstr &MI,
4523 MachineDominatorTree *MDT) const {
4524 MachineFunction &MF = *MI.getParent()->getParent();
4525 MachineRegisterInfo &MRI = MF.getRegInfo();
4526
4527 // Legalize VOP2
4528 if (isVOP2(MI) || isVOPC(MI)) {
4529 legalizeOperandsVOP2(MRI, MI);
4530 return;
4531 }
4532
4533 // Legalize VOP3
4534 if (isVOP3(MI)) {
4535 legalizeOperandsVOP3(MRI, MI);
4536 return;
4537 }
4538
4539 // Legalize SMRD
4540 if (isSMRD(MI)) {
4541 legalizeOperandsSMRD(MRI, MI);
4542 return;
4543 }
4544
4545 // Legalize REG_SEQUENCE and PHI
4546 // The register class of the operands much be the same type as the register
4547 // class of the output.
4548 if (MI.getOpcode() == AMDGPU::PHI) {
4549 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr;
4550 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
4551 if (!MI.getOperand(i).isReg() ||
4552 !Register::isVirtualRegister(MI.getOperand(i).getReg()))
4553 continue;
4554 const TargetRegisterClass *OpRC =
4555 MRI.getRegClass(MI.getOperand(i).getReg());
4556 if (RI.hasVectorRegisters(OpRC)) {
4557 VRC = OpRC;
4558 } else {
4559 SRC = OpRC;
4560 }
4561 }
4562
4563 // If any of the operands are VGPR registers, then they all most be
4564 // otherwise we will create illegal VGPR->SGPR copies when legalizing
4565 // them.
4566 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) {
4567 if (!VRC) {
4568 assert(SRC);
4569 if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) {
4570 VRC = &AMDGPU::VReg_1RegClass;
4571 } else
4572 VRC = RI.hasAGPRs(getOpRegClass(MI, 0))
4573 ? RI.getEquivalentAGPRClass(SRC)
4574 : RI.getEquivalentVGPRClass(SRC);
4575 } else {
4576 VRC = RI.hasAGPRs(getOpRegClass(MI, 0))
4577 ? RI.getEquivalentAGPRClass(VRC)
4578 : RI.getEquivalentVGPRClass(VRC);
4579 }
4580 RC = VRC;
4581 } else {
4582 RC = SRC;
4583 }
4584
4585 // Update all the operands so they have the same type.
4586 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
4587 MachineOperand &Op = MI.getOperand(I);
4588 if (!Op.isReg() || !Register::isVirtualRegister(Op.getReg()))
4589 continue;
4590
4591 // MI is a PHI instruction.
4592 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB();
4593 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator();
4594
4595 // Avoid creating no-op copies with the same src and dst reg class. These
4596 // confuse some of the machine passes.
4597 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc());
4598 }
4599 }
4600
4601 // REG_SEQUENCE doesn't really require operand legalization, but if one has a
4602 // VGPR dest type and SGPR sources, insert copies so all operands are
4603 // VGPRs. This seems to help operand folding / the register coalescer.
4604 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) {
4605 MachineBasicBlock *MBB = MI.getParent();
4606 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0);
4607 if (RI.hasVGPRs(DstRC)) {
4608 // Update all the operands so they are VGPR register classes. These may
4609 // not be the same register class because REG_SEQUENCE supports mixing
4610 // subregister index types e.g. sub0_sub1 + sub2 + sub3
4611 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
4612 MachineOperand &Op = MI.getOperand(I);
4613 if (!Op.isReg() || !Register::isVirtualRegister(Op.getReg()))
4614 continue;
4615
4616 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg());
4617 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC);
4618 if (VRC == OpRC)
4619 continue;
4620
4621 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc());
4622 Op.setIsKill();
4623 }
4624 }
4625
4626 return;
4627 }
4628
4629 // Legalize INSERT_SUBREG
4630 // src0 must have the same register class as dst
4631 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) {
4632 Register Dst = MI.getOperand(0).getReg();
4633 Register Src0 = MI.getOperand(1).getReg();
4634 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
4635 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0);
4636 if (DstRC != Src0RC) {
4637 MachineBasicBlock *MBB = MI.getParent();
4638 MachineOperand &Op = MI.getOperand(1);
4639 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc());
4640 }
4641 return;
4642 }
4643
4644 // Legalize SI_INIT_M0
4645 if (MI.getOpcode() == AMDGPU::SI_INIT_M0) {
4646 MachineOperand &Src = MI.getOperand(0);
4647 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg())))
4648 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI));
4649 return;
4650 }
4651
4652 // Legalize MIMG and MUBUF/MTBUF for shaders.
4653 //
4654 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via
4655 // scratch memory access. In both cases, the legalization never involves
4656 // conversion to the addr64 form.
4657 if (isMIMG(MI) ||
4658 (AMDGPU::isShader(MF.getFunction().getCallingConv()) &&
4659 (isMUBUF(MI) || isMTBUF(MI)))) {
4660 MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc);
4661 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) {
4662 unsigned SGPR = readlaneVGPRToSGPR(SRsrc->getReg(), MI, MRI);
4663 SRsrc->setReg(SGPR);
4664 }
4665
4666 MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp);
4667 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) {
4668 unsigned SGPR = readlaneVGPRToSGPR(SSamp->getReg(), MI, MRI);
4669 SSamp->setReg(SGPR);
4670 }
4671 return;
4672 }
4673
4674 // Legalize MUBUF* instructions.
4675 int RsrcIdx =
4676 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc);
4677 if (RsrcIdx != -1) {
4678 // We have an MUBUF instruction
4679 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx);
4680 unsigned RsrcRC = get(MI.getOpcode()).OpInfo[RsrcIdx].RegClass;
4681 if (RI.getCommonSubClass(MRI.getRegClass(Rsrc->getReg()),
4682 RI.getRegClass(RsrcRC))) {
4683 // The operands are legal.
4684 // FIXME: We may need to legalize operands besided srsrc.
4685 return;
4686 }
4687
4688 // Legalize a VGPR Rsrc.
4689 //
4690 // If the instruction is _ADDR64, we can avoid a waterfall by extracting
4691 // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using
4692 // a zero-value SRsrc.
4693 //
4694 // If the instruction is _OFFSET (both idxen and offen disabled), and we
4695 // support ADDR64 instructions, we can convert to ADDR64 and do the same as
4696 // above.
4697 //
4698 // Otherwise we are on non-ADDR64 hardware, and/or we have
4699 // idxen/offen/bothen and we fall back to a waterfall loop.
4700
4701 MachineBasicBlock &MBB = *MI.getParent();
4702
4703 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
4704 if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) {
4705 // This is already an ADDR64 instruction so we need to add the pointer
4706 // extracted from the resource descriptor to the current value of VAddr.
4707 Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
4708 Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
4709 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
4710
4711 const auto *BoolXExecRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
4712 Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC);
4713 Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC);
4714
4715 unsigned RsrcPtr, NewSRsrc;
4716 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
4717
4718 // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0
4719 const DebugLoc &DL = MI.getDebugLoc();
4720 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e64), NewVAddrLo)
4721 .addDef(CondReg0)
4722 .addReg(RsrcPtr, 0, AMDGPU::sub0)
4723 .addReg(VAddr->getReg(), 0, AMDGPU::sub0)
4724 .addImm(0);
4725
4726 // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1
4727 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi)
4728 .addDef(CondReg1, RegState::Dead)
4729 .addReg(RsrcPtr, 0, AMDGPU::sub1)
4730 .addReg(VAddr->getReg(), 0, AMDGPU::sub1)
4731 .addReg(CondReg0, RegState::Kill)
4732 .addImm(0);
4733
4734 // NewVaddr = {NewVaddrHi, NewVaddrLo}
4735 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr)
4736 .addReg(NewVAddrLo)
4737 .addImm(AMDGPU::sub0)
4738 .addReg(NewVAddrHi)
4739 .addImm(AMDGPU::sub1);
4740
4741 VAddr->setReg(NewVAddr);
4742 Rsrc->setReg(NewSRsrc);
4743 } else if (!VAddr && ST.hasAddr64()) {
4744 // This instructions is the _OFFSET variant, so we need to convert it to
4745 // ADDR64.
4746 assert(MBB.getParent()->getSubtarget<GCNSubtarget>().getGeneration()
4747 < AMDGPUSubtarget::VOLCANIC_ISLANDS &&
4748 "FIXME: Need to emit flat atomics here");
4749
4750 unsigned RsrcPtr, NewSRsrc;
4751 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
4752
4753 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
4754 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata);
4755 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
4756 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset);
4757 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode());
4758
4759 // Atomics rith return have have an additional tied operand and are
4760 // missing some of the special bits.
4761 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in);
4762 MachineInstr *Addr64;
4763
4764 if (!VDataIn) {
4765 // Regular buffer load / store.
4766 MachineInstrBuilder MIB =
4767 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
4768 .add(*VData)
4769 .addReg(NewVAddr)
4770 .addReg(NewSRsrc)
4771 .add(*SOffset)
4772 .add(*Offset);
4773
4774 // Atomics do not have this operand.
4775 if (const MachineOperand *GLC =
4776 getNamedOperand(MI, AMDGPU::OpName::glc)) {
4777 MIB.addImm(GLC->getImm());
4778 }
4779 if (const MachineOperand *DLC =
4780 getNamedOperand(MI, AMDGPU::OpName::dlc)) {
4781 MIB.addImm(DLC->getImm());
4782 }
4783
4784 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc));
4785
4786 if (const MachineOperand *TFE =
4787 getNamedOperand(MI, AMDGPU::OpName::tfe)) {
4788 MIB.addImm(TFE->getImm());
4789 }
4790
4791 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz));
4792
4793 MIB.cloneMemRefs(MI);
4794 Addr64 = MIB;
4795 } else {
4796 // Atomics with return.
4797 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
4798 .add(*VData)
4799 .add(*VDataIn)
4800 .addReg(NewVAddr)
4801 .addReg(NewSRsrc)
4802 .add(*SOffset)
4803 .add(*Offset)
4804 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc))
4805 .cloneMemRefs(MI);
4806 }
4807
4808 MI.removeFromParent();
4809
4810 // NewVaddr = {NewVaddrHi, NewVaddrLo}
4811 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
4812 NewVAddr)
4813 .addReg(RsrcPtr, 0, AMDGPU::sub0)
4814 .addImm(AMDGPU::sub0)
4815 .addReg(RsrcPtr, 0, AMDGPU::sub1)
4816 .addImm(AMDGPU::sub1);
4817 } else {
4818 // This is another variant; legalize Rsrc with waterfall loop from VGPRs
4819 // to SGPRs.
4820 loadSRsrcFromVGPR(*this, MI, *Rsrc, MDT);
4821 }
4822 }
4823 }
4824
moveToVALU(MachineInstr & TopInst,MachineDominatorTree * MDT) const4825 void SIInstrInfo::moveToVALU(MachineInstr &TopInst,
4826 MachineDominatorTree *MDT) const {
4827 SetVectorType Worklist;
4828 Worklist.insert(&TopInst);
4829
4830 while (!Worklist.empty()) {
4831 MachineInstr &Inst = *Worklist.pop_back_val();
4832 MachineBasicBlock *MBB = Inst.getParent();
4833 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
4834
4835 unsigned Opcode = Inst.getOpcode();
4836 unsigned NewOpcode = getVALUOp(Inst);
4837
4838 // Handle some special cases
4839 switch (Opcode) {
4840 default:
4841 break;
4842 case AMDGPU::S_ADD_U64_PSEUDO:
4843 case AMDGPU::S_SUB_U64_PSEUDO:
4844 splitScalar64BitAddSub(Worklist, Inst, MDT);
4845 Inst.eraseFromParent();
4846 continue;
4847 case AMDGPU::S_ADD_I32:
4848 case AMDGPU::S_SUB_I32:
4849 // FIXME: The u32 versions currently selected use the carry.
4850 if (moveScalarAddSub(Worklist, Inst, MDT))
4851 continue;
4852
4853 // Default handling
4854 break;
4855 case AMDGPU::S_AND_B64:
4856 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT);
4857 Inst.eraseFromParent();
4858 continue;
4859
4860 case AMDGPU::S_OR_B64:
4861 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT);
4862 Inst.eraseFromParent();
4863 continue;
4864
4865 case AMDGPU::S_XOR_B64:
4866 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT);
4867 Inst.eraseFromParent();
4868 continue;
4869
4870 case AMDGPU::S_NAND_B64:
4871 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT);
4872 Inst.eraseFromParent();
4873 continue;
4874
4875 case AMDGPU::S_NOR_B64:
4876 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT);
4877 Inst.eraseFromParent();
4878 continue;
4879
4880 case AMDGPU::S_XNOR_B64:
4881 if (ST.hasDLInsts())
4882 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT);
4883 else
4884 splitScalar64BitXnor(Worklist, Inst, MDT);
4885 Inst.eraseFromParent();
4886 continue;
4887
4888 case AMDGPU::S_ANDN2_B64:
4889 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT);
4890 Inst.eraseFromParent();
4891 continue;
4892
4893 case AMDGPU::S_ORN2_B64:
4894 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT);
4895 Inst.eraseFromParent();
4896 continue;
4897
4898 case AMDGPU::S_NOT_B64:
4899 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32);
4900 Inst.eraseFromParent();
4901 continue;
4902
4903 case AMDGPU::S_BCNT1_I32_B64:
4904 splitScalar64BitBCNT(Worklist, Inst);
4905 Inst.eraseFromParent();
4906 continue;
4907
4908 case AMDGPU::S_BFE_I64:
4909 splitScalar64BitBFE(Worklist, Inst);
4910 Inst.eraseFromParent();
4911 continue;
4912
4913 case AMDGPU::S_LSHL_B32:
4914 if (ST.hasOnlyRevVALUShifts()) {
4915 NewOpcode = AMDGPU::V_LSHLREV_B32_e64;
4916 swapOperands(Inst);
4917 }
4918 break;
4919 case AMDGPU::S_ASHR_I32:
4920 if (ST.hasOnlyRevVALUShifts()) {
4921 NewOpcode = AMDGPU::V_ASHRREV_I32_e64;
4922 swapOperands(Inst);
4923 }
4924 break;
4925 case AMDGPU::S_LSHR_B32:
4926 if (ST.hasOnlyRevVALUShifts()) {
4927 NewOpcode = AMDGPU::V_LSHRREV_B32_e64;
4928 swapOperands(Inst);
4929 }
4930 break;
4931 case AMDGPU::S_LSHL_B64:
4932 if (ST.hasOnlyRevVALUShifts()) {
4933 NewOpcode = AMDGPU::V_LSHLREV_B64;
4934 swapOperands(Inst);
4935 }
4936 break;
4937 case AMDGPU::S_ASHR_I64:
4938 if (ST.hasOnlyRevVALUShifts()) {
4939 NewOpcode = AMDGPU::V_ASHRREV_I64;
4940 swapOperands(Inst);
4941 }
4942 break;
4943 case AMDGPU::S_LSHR_B64:
4944 if (ST.hasOnlyRevVALUShifts()) {
4945 NewOpcode = AMDGPU::V_LSHRREV_B64;
4946 swapOperands(Inst);
4947 }
4948 break;
4949
4950 case AMDGPU::S_ABS_I32:
4951 lowerScalarAbs(Worklist, Inst);
4952 Inst.eraseFromParent();
4953 continue;
4954
4955 case AMDGPU::S_CBRANCH_SCC0:
4956 case AMDGPU::S_CBRANCH_SCC1:
4957 // Clear unused bits of vcc
4958 if (ST.isWave32())
4959 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::S_AND_B32),
4960 AMDGPU::VCC_LO)
4961 .addReg(AMDGPU::EXEC_LO)
4962 .addReg(AMDGPU::VCC_LO);
4963 else
4964 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::S_AND_B64),
4965 AMDGPU::VCC)
4966 .addReg(AMDGPU::EXEC)
4967 .addReg(AMDGPU::VCC);
4968 break;
4969
4970 case AMDGPU::S_BFE_U64:
4971 case AMDGPU::S_BFM_B64:
4972 llvm_unreachable("Moving this op to VALU not implemented");
4973
4974 case AMDGPU::S_PACK_LL_B32_B16:
4975 case AMDGPU::S_PACK_LH_B32_B16:
4976 case AMDGPU::S_PACK_HH_B32_B16:
4977 movePackToVALU(Worklist, MRI, Inst);
4978 Inst.eraseFromParent();
4979 continue;
4980
4981 case AMDGPU::S_XNOR_B32:
4982 lowerScalarXnor(Worklist, Inst);
4983 Inst.eraseFromParent();
4984 continue;
4985
4986 case AMDGPU::S_NAND_B32:
4987 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32);
4988 Inst.eraseFromParent();
4989 continue;
4990
4991 case AMDGPU::S_NOR_B32:
4992 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32);
4993 Inst.eraseFromParent();
4994 continue;
4995
4996 case AMDGPU::S_ANDN2_B32:
4997 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32);
4998 Inst.eraseFromParent();
4999 continue;
5000
5001 case AMDGPU::S_ORN2_B32:
5002 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32);
5003 Inst.eraseFromParent();
5004 continue;
5005 }
5006
5007 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
5008 // We cannot move this instruction to the VALU, so we should try to
5009 // legalize its operands instead.
5010 legalizeOperands(Inst, MDT);
5011 continue;
5012 }
5013
5014 // Use the new VALU Opcode.
5015 const MCInstrDesc &NewDesc = get(NewOpcode);
5016 Inst.setDesc(NewDesc);
5017
5018 // Remove any references to SCC. Vector instructions can't read from it, and
5019 // We're just about to add the implicit use / defs of VCC, and we don't want
5020 // both.
5021 for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) {
5022 MachineOperand &Op = Inst.getOperand(i);
5023 if (Op.isReg() && Op.getReg() == AMDGPU::SCC) {
5024 // Only propagate through live-def of SCC.
5025 if (Op.isDef() && !Op.isDead())
5026 addSCCDefUsersToVALUWorklist(Op, Inst, Worklist);
5027 Inst.RemoveOperand(i);
5028 }
5029 }
5030
5031 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) {
5032 // We are converting these to a BFE, so we need to add the missing
5033 // operands for the size and offset.
5034 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16;
5035 Inst.addOperand(MachineOperand::CreateImm(0));
5036 Inst.addOperand(MachineOperand::CreateImm(Size));
5037
5038 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) {
5039 // The VALU version adds the second operand to the result, so insert an
5040 // extra 0 operand.
5041 Inst.addOperand(MachineOperand::CreateImm(0));
5042 }
5043
5044 Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent());
5045 fixImplicitOperands(Inst);
5046
5047 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) {
5048 const MachineOperand &OffsetWidthOp = Inst.getOperand(2);
5049 // If we need to move this to VGPRs, we need to unpack the second operand
5050 // back into the 2 separate ones for bit offset and width.
5051 assert(OffsetWidthOp.isImm() &&
5052 "Scalar BFE is only implemented for constant width and offset");
5053 uint32_t Imm = OffsetWidthOp.getImm();
5054
5055 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
5056 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
5057 Inst.RemoveOperand(2); // Remove old immediate.
5058 Inst.addOperand(MachineOperand::CreateImm(Offset));
5059 Inst.addOperand(MachineOperand::CreateImm(BitWidth));
5060 }
5061
5062 bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef();
5063 unsigned NewDstReg = AMDGPU::NoRegister;
5064 if (HasDst) {
5065 Register DstReg = Inst.getOperand(0).getReg();
5066 if (Register::isPhysicalRegister(DstReg))
5067 continue;
5068
5069 // Update the destination register class.
5070 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst);
5071 if (!NewDstRC)
5072 continue;
5073
5074 if (Inst.isCopy() &&
5075 Register::isVirtualRegister(Inst.getOperand(1).getReg()) &&
5076 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) {
5077 // Instead of creating a copy where src and dst are the same register
5078 // class, we just replace all uses of dst with src. These kinds of
5079 // copies interfere with the heuristics MachineSink uses to decide
5080 // whether or not to split a critical edge. Since the pass assumes
5081 // that copies will end up as machine instructions and not be
5082 // eliminated.
5083 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist);
5084 MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg());
5085 MRI.clearKillFlags(Inst.getOperand(1).getReg());
5086 Inst.getOperand(0).setReg(DstReg);
5087
5088 // Make sure we don't leave around a dead VGPR->SGPR copy. Normally
5089 // these are deleted later, but at -O0 it would leave a suspicious
5090 // looking illegal copy of an undef register.
5091 for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I)
5092 Inst.RemoveOperand(I);
5093 Inst.setDesc(get(AMDGPU::IMPLICIT_DEF));
5094 continue;
5095 }
5096
5097 NewDstReg = MRI.createVirtualRegister(NewDstRC);
5098 MRI.replaceRegWith(DstReg, NewDstReg);
5099 }
5100
5101 // Legalize the operands
5102 legalizeOperands(Inst, MDT);
5103
5104 if (HasDst)
5105 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
5106 }
5107 }
5108
5109 // Add/sub require special handling to deal with carry outs.
moveScalarAddSub(SetVectorType & Worklist,MachineInstr & Inst,MachineDominatorTree * MDT) const5110 bool SIInstrInfo::moveScalarAddSub(SetVectorType &Worklist, MachineInstr &Inst,
5111 MachineDominatorTree *MDT) const {
5112 if (ST.hasAddNoCarry()) {
5113 // Assume there is no user of scc since we don't select this in that case.
5114 // Since scc isn't used, it doesn't really matter if the i32 or u32 variant
5115 // is used.
5116
5117 MachineBasicBlock &MBB = *Inst.getParent();
5118 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5119
5120 Register OldDstReg = Inst.getOperand(0).getReg();
5121 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5122
5123 unsigned Opc = Inst.getOpcode();
5124 assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32);
5125
5126 unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ?
5127 AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64;
5128
5129 assert(Inst.getOperand(3).getReg() == AMDGPU::SCC);
5130 Inst.RemoveOperand(3);
5131
5132 Inst.setDesc(get(NewOpc));
5133 Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit
5134 Inst.addImplicitDefUseOperands(*MBB.getParent());
5135 MRI.replaceRegWith(OldDstReg, ResultReg);
5136 legalizeOperands(Inst, MDT);
5137
5138 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
5139 return true;
5140 }
5141
5142 return false;
5143 }
5144
lowerScalarAbs(SetVectorType & Worklist,MachineInstr & Inst) const5145 void SIInstrInfo::lowerScalarAbs(SetVectorType &Worklist,
5146 MachineInstr &Inst) const {
5147 MachineBasicBlock &MBB = *Inst.getParent();
5148 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5149 MachineBasicBlock::iterator MII = Inst;
5150 DebugLoc DL = Inst.getDebugLoc();
5151
5152 MachineOperand &Dest = Inst.getOperand(0);
5153 MachineOperand &Src = Inst.getOperand(1);
5154 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5155 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5156
5157 unsigned SubOp = ST.hasAddNoCarry() ?
5158 AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_I32_e32;
5159
5160 BuildMI(MBB, MII, DL, get(SubOp), TmpReg)
5161 .addImm(0)
5162 .addReg(Src.getReg());
5163
5164 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg)
5165 .addReg(Src.getReg())
5166 .addReg(TmpReg);
5167
5168 MRI.replaceRegWith(Dest.getReg(), ResultReg);
5169 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
5170 }
5171
lowerScalarXnor(SetVectorType & Worklist,MachineInstr & Inst) const5172 void SIInstrInfo::lowerScalarXnor(SetVectorType &Worklist,
5173 MachineInstr &Inst) const {
5174 MachineBasicBlock &MBB = *Inst.getParent();
5175 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5176 MachineBasicBlock::iterator MII = Inst;
5177 const DebugLoc &DL = Inst.getDebugLoc();
5178
5179 MachineOperand &Dest = Inst.getOperand(0);
5180 MachineOperand &Src0 = Inst.getOperand(1);
5181 MachineOperand &Src1 = Inst.getOperand(2);
5182
5183 if (ST.hasDLInsts()) {
5184 Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5185 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL);
5186 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL);
5187
5188 BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest)
5189 .add(Src0)
5190 .add(Src1);
5191
5192 MRI.replaceRegWith(Dest.getReg(), NewDest);
5193 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
5194 } else {
5195 // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can
5196 // invert either source and then perform the XOR. If either source is a
5197 // scalar register, then we can leave the inversion on the scalar unit to
5198 // acheive a better distrubution of scalar and vector instructions.
5199 bool Src0IsSGPR = Src0.isReg() &&
5200 RI.isSGPRClass(MRI.getRegClass(Src0.getReg()));
5201 bool Src1IsSGPR = Src1.isReg() &&
5202 RI.isSGPRClass(MRI.getRegClass(Src1.getReg()));
5203 MachineInstr *Xor;
5204 Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5205 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5206
5207 // Build a pair of scalar instructions and add them to the work list.
5208 // The next iteration over the work list will lower these to the vector
5209 // unit as necessary.
5210 if (Src0IsSGPR) {
5211 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0);
5212 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
5213 .addReg(Temp)
5214 .add(Src1);
5215 } else if (Src1IsSGPR) {
5216 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1);
5217 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
5218 .add(Src0)
5219 .addReg(Temp);
5220 } else {
5221 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp)
5222 .add(Src0)
5223 .add(Src1);
5224 MachineInstr *Not =
5225 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp);
5226 Worklist.insert(Not);
5227 }
5228
5229 MRI.replaceRegWith(Dest.getReg(), NewDest);
5230
5231 Worklist.insert(Xor);
5232
5233 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
5234 }
5235 }
5236
splitScalarNotBinop(SetVectorType & Worklist,MachineInstr & Inst,unsigned Opcode) const5237 void SIInstrInfo::splitScalarNotBinop(SetVectorType &Worklist,
5238 MachineInstr &Inst,
5239 unsigned Opcode) const {
5240 MachineBasicBlock &MBB = *Inst.getParent();
5241 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5242 MachineBasicBlock::iterator MII = Inst;
5243 const DebugLoc &DL = Inst.getDebugLoc();
5244
5245 MachineOperand &Dest = Inst.getOperand(0);
5246 MachineOperand &Src0 = Inst.getOperand(1);
5247 MachineOperand &Src1 = Inst.getOperand(2);
5248
5249 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5250 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5251
5252 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm)
5253 .add(Src0)
5254 .add(Src1);
5255
5256 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest)
5257 .addReg(Interm);
5258
5259 Worklist.insert(&Op);
5260 Worklist.insert(&Not);
5261
5262 MRI.replaceRegWith(Dest.getReg(), NewDest);
5263 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
5264 }
5265
splitScalarBinOpN2(SetVectorType & Worklist,MachineInstr & Inst,unsigned Opcode) const5266 void SIInstrInfo::splitScalarBinOpN2(SetVectorType& Worklist,
5267 MachineInstr &Inst,
5268 unsigned Opcode) const {
5269 MachineBasicBlock &MBB = *Inst.getParent();
5270 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5271 MachineBasicBlock::iterator MII = Inst;
5272 const DebugLoc &DL = Inst.getDebugLoc();
5273
5274 MachineOperand &Dest = Inst.getOperand(0);
5275 MachineOperand &Src0 = Inst.getOperand(1);
5276 MachineOperand &Src1 = Inst.getOperand(2);
5277
5278 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5279 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5280
5281 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm)
5282 .add(Src1);
5283
5284 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest)
5285 .add(Src0)
5286 .addReg(Interm);
5287
5288 Worklist.insert(&Not);
5289 Worklist.insert(&Op);
5290
5291 MRI.replaceRegWith(Dest.getReg(), NewDest);
5292 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
5293 }
5294
splitScalar64BitUnaryOp(SetVectorType & Worklist,MachineInstr & Inst,unsigned Opcode) const5295 void SIInstrInfo::splitScalar64BitUnaryOp(
5296 SetVectorType &Worklist, MachineInstr &Inst,
5297 unsigned Opcode) const {
5298 MachineBasicBlock &MBB = *Inst.getParent();
5299 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5300
5301 MachineOperand &Dest = Inst.getOperand(0);
5302 MachineOperand &Src0 = Inst.getOperand(1);
5303 DebugLoc DL = Inst.getDebugLoc();
5304
5305 MachineBasicBlock::iterator MII = Inst;
5306
5307 const MCInstrDesc &InstDesc = get(Opcode);
5308 const TargetRegisterClass *Src0RC = Src0.isReg() ?
5309 MRI.getRegClass(Src0.getReg()) :
5310 &AMDGPU::SGPR_32RegClass;
5311
5312 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
5313
5314 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
5315 AMDGPU::sub0, Src0SubRC);
5316
5317 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
5318 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
5319 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
5320
5321 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
5322 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0);
5323
5324 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
5325 AMDGPU::sub1, Src0SubRC);
5326
5327 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
5328 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1);
5329
5330 Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
5331 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
5332 .addReg(DestSub0)
5333 .addImm(AMDGPU::sub0)
5334 .addReg(DestSub1)
5335 .addImm(AMDGPU::sub1);
5336
5337 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
5338
5339 Worklist.insert(&LoHalf);
5340 Worklist.insert(&HiHalf);
5341
5342 // We don't need to legalizeOperands here because for a single operand, src0
5343 // will support any kind of input.
5344
5345 // Move all users of this moved value.
5346 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
5347 }
5348
splitScalar64BitAddSub(SetVectorType & Worklist,MachineInstr & Inst,MachineDominatorTree * MDT) const5349 void SIInstrInfo::splitScalar64BitAddSub(SetVectorType &Worklist,
5350 MachineInstr &Inst,
5351 MachineDominatorTree *MDT) const {
5352 bool IsAdd = (Inst.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO);
5353
5354 MachineBasicBlock &MBB = *Inst.getParent();
5355 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5356 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
5357
5358 Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
5359 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5360 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5361
5362 Register CarryReg = MRI.createVirtualRegister(CarryRC);
5363 Register DeadCarryReg = MRI.createVirtualRegister(CarryRC);
5364
5365 MachineOperand &Dest = Inst.getOperand(0);
5366 MachineOperand &Src0 = Inst.getOperand(1);
5367 MachineOperand &Src1 = Inst.getOperand(2);
5368 const DebugLoc &DL = Inst.getDebugLoc();
5369 MachineBasicBlock::iterator MII = Inst;
5370
5371 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg());
5372 const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg());
5373 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
5374 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
5375
5376 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
5377 AMDGPU::sub0, Src0SubRC);
5378 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
5379 AMDGPU::sub0, Src1SubRC);
5380
5381
5382 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
5383 AMDGPU::sub1, Src0SubRC);
5384 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
5385 AMDGPU::sub1, Src1SubRC);
5386
5387 unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64;
5388 MachineInstr *LoHalf =
5389 BuildMI(MBB, MII, DL, get(LoOpc), DestSub0)
5390 .addReg(CarryReg, RegState::Define)
5391 .add(SrcReg0Sub0)
5392 .add(SrcReg1Sub0)
5393 .addImm(0); // clamp bit
5394
5395 unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64;
5396 MachineInstr *HiHalf =
5397 BuildMI(MBB, MII, DL, get(HiOpc), DestSub1)
5398 .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
5399 .add(SrcReg0Sub1)
5400 .add(SrcReg1Sub1)
5401 .addReg(CarryReg, RegState::Kill)
5402 .addImm(0); // clamp bit
5403
5404 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
5405 .addReg(DestSub0)
5406 .addImm(AMDGPU::sub0)
5407 .addReg(DestSub1)
5408 .addImm(AMDGPU::sub1);
5409
5410 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
5411
5412 // Try to legalize the operands in case we need to swap the order to keep it
5413 // valid.
5414 legalizeOperands(*LoHalf, MDT);
5415 legalizeOperands(*HiHalf, MDT);
5416
5417 // Move all users of this moved vlaue.
5418 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
5419 }
5420
splitScalar64BitBinaryOp(SetVectorType & Worklist,MachineInstr & Inst,unsigned Opcode,MachineDominatorTree * MDT) const5421 void SIInstrInfo::splitScalar64BitBinaryOp(SetVectorType &Worklist,
5422 MachineInstr &Inst, unsigned Opcode,
5423 MachineDominatorTree *MDT) const {
5424 MachineBasicBlock &MBB = *Inst.getParent();
5425 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5426
5427 MachineOperand &Dest = Inst.getOperand(0);
5428 MachineOperand &Src0 = Inst.getOperand(1);
5429 MachineOperand &Src1 = Inst.getOperand(2);
5430 DebugLoc DL = Inst.getDebugLoc();
5431
5432 MachineBasicBlock::iterator MII = Inst;
5433
5434 const MCInstrDesc &InstDesc = get(Opcode);
5435 const TargetRegisterClass *Src0RC = Src0.isReg() ?
5436 MRI.getRegClass(Src0.getReg()) :
5437 &AMDGPU::SGPR_32RegClass;
5438
5439 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
5440 const TargetRegisterClass *Src1RC = Src1.isReg() ?
5441 MRI.getRegClass(Src1.getReg()) :
5442 &AMDGPU::SGPR_32RegClass;
5443
5444 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
5445
5446 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
5447 AMDGPU::sub0, Src0SubRC);
5448 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
5449 AMDGPU::sub0, Src1SubRC);
5450 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
5451 AMDGPU::sub1, Src0SubRC);
5452 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
5453 AMDGPU::sub1, Src1SubRC);
5454
5455 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
5456 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
5457 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
5458
5459 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
5460 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0)
5461 .add(SrcReg0Sub0)
5462 .add(SrcReg1Sub0);
5463
5464 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
5465 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1)
5466 .add(SrcReg0Sub1)
5467 .add(SrcReg1Sub1);
5468
5469 Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
5470 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
5471 .addReg(DestSub0)
5472 .addImm(AMDGPU::sub0)
5473 .addReg(DestSub1)
5474 .addImm(AMDGPU::sub1);
5475
5476 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
5477
5478 Worklist.insert(&LoHalf);
5479 Worklist.insert(&HiHalf);
5480
5481 // Move all users of this moved vlaue.
5482 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
5483 }
5484
splitScalar64BitXnor(SetVectorType & Worklist,MachineInstr & Inst,MachineDominatorTree * MDT) const5485 void SIInstrInfo::splitScalar64BitXnor(SetVectorType &Worklist,
5486 MachineInstr &Inst,
5487 MachineDominatorTree *MDT) const {
5488 MachineBasicBlock &MBB = *Inst.getParent();
5489 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5490
5491 MachineOperand &Dest = Inst.getOperand(0);
5492 MachineOperand &Src0 = Inst.getOperand(1);
5493 MachineOperand &Src1 = Inst.getOperand(2);
5494 const DebugLoc &DL = Inst.getDebugLoc();
5495
5496 MachineBasicBlock::iterator MII = Inst;
5497
5498 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
5499
5500 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
5501
5502 MachineOperand* Op0;
5503 MachineOperand* Op1;
5504
5505 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) {
5506 Op0 = &Src0;
5507 Op1 = &Src1;
5508 } else {
5509 Op0 = &Src1;
5510 Op1 = &Src0;
5511 }
5512
5513 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm)
5514 .add(*Op0);
5515
5516 Register NewDest = MRI.createVirtualRegister(DestRC);
5517
5518 MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest)
5519 .addReg(Interm)
5520 .add(*Op1);
5521
5522 MRI.replaceRegWith(Dest.getReg(), NewDest);
5523
5524 Worklist.insert(&Xor);
5525 }
5526
splitScalar64BitBCNT(SetVectorType & Worklist,MachineInstr & Inst) const5527 void SIInstrInfo::splitScalar64BitBCNT(
5528 SetVectorType &Worklist, MachineInstr &Inst) const {
5529 MachineBasicBlock &MBB = *Inst.getParent();
5530 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5531
5532 MachineBasicBlock::iterator MII = Inst;
5533 const DebugLoc &DL = Inst.getDebugLoc();
5534
5535 MachineOperand &Dest = Inst.getOperand(0);
5536 MachineOperand &Src = Inst.getOperand(1);
5537
5538 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64);
5539 const TargetRegisterClass *SrcRC = Src.isReg() ?
5540 MRI.getRegClass(Src.getReg()) :
5541 &AMDGPU::SGPR_32RegClass;
5542
5543 Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5544 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5545
5546 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0);
5547
5548 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
5549 AMDGPU::sub0, SrcSubRC);
5550 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
5551 AMDGPU::sub1, SrcSubRC);
5552
5553 BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0);
5554
5555 BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg);
5556
5557 MRI.replaceRegWith(Dest.getReg(), ResultReg);
5558
5559 // We don't need to legalize operands here. src0 for etiher instruction can be
5560 // an SGPR, and the second input is unused or determined here.
5561 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
5562 }
5563
splitScalar64BitBFE(SetVectorType & Worklist,MachineInstr & Inst) const5564 void SIInstrInfo::splitScalar64BitBFE(SetVectorType &Worklist,
5565 MachineInstr &Inst) const {
5566 MachineBasicBlock &MBB = *Inst.getParent();
5567 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
5568 MachineBasicBlock::iterator MII = Inst;
5569 const DebugLoc &DL = Inst.getDebugLoc();
5570
5571 MachineOperand &Dest = Inst.getOperand(0);
5572 uint32_t Imm = Inst.getOperand(2).getImm();
5573 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
5574 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
5575
5576 (void) Offset;
5577
5578 // Only sext_inreg cases handled.
5579 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 &&
5580 Offset == 0 && "Not implemented");
5581
5582 if (BitWidth < 32) {
5583 Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5584 Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5585 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
5586
5587 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32), MidRegLo)
5588 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0)
5589 .addImm(0)
5590 .addImm(BitWidth);
5591
5592 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi)
5593 .addImm(31)
5594 .addReg(MidRegLo);
5595
5596 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
5597 .addReg(MidRegLo)
5598 .addImm(AMDGPU::sub0)
5599 .addReg(MidRegHi)
5600 .addImm(AMDGPU::sub1);
5601
5602 MRI.replaceRegWith(Dest.getReg(), ResultReg);
5603 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
5604 return;
5605 }
5606
5607 MachineOperand &Src = Inst.getOperand(1);
5608 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5609 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
5610
5611 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg)
5612 .addImm(31)
5613 .addReg(Src.getReg(), 0, AMDGPU::sub0);
5614
5615 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
5616 .addReg(Src.getReg(), 0, AMDGPU::sub0)
5617 .addImm(AMDGPU::sub0)
5618 .addReg(TmpReg)
5619 .addImm(AMDGPU::sub1);
5620
5621 MRI.replaceRegWith(Dest.getReg(), ResultReg);
5622 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
5623 }
5624
addUsersToMoveToVALUWorklist(unsigned DstReg,MachineRegisterInfo & MRI,SetVectorType & Worklist) const5625 void SIInstrInfo::addUsersToMoveToVALUWorklist(
5626 unsigned DstReg,
5627 MachineRegisterInfo &MRI,
5628 SetVectorType &Worklist) const {
5629 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg),
5630 E = MRI.use_end(); I != E;) {
5631 MachineInstr &UseMI = *I->getParent();
5632
5633 unsigned OpNo = 0;
5634
5635 switch (UseMI.getOpcode()) {
5636 case AMDGPU::COPY:
5637 case AMDGPU::WQM:
5638 case AMDGPU::SOFT_WQM:
5639 case AMDGPU::WWM:
5640 case AMDGPU::REG_SEQUENCE:
5641 case AMDGPU::PHI:
5642 case AMDGPU::INSERT_SUBREG:
5643 break;
5644 default:
5645 OpNo = I.getOperandNo();
5646 break;
5647 }
5648
5649 if (!RI.hasVectorRegisters(getOpRegClass(UseMI, OpNo))) {
5650 Worklist.insert(&UseMI);
5651
5652 do {
5653 ++I;
5654 } while (I != E && I->getParent() == &UseMI);
5655 } else {
5656 ++I;
5657 }
5658 }
5659 }
5660
movePackToVALU(SetVectorType & Worklist,MachineRegisterInfo & MRI,MachineInstr & Inst) const5661 void SIInstrInfo::movePackToVALU(SetVectorType &Worklist,
5662 MachineRegisterInfo &MRI,
5663 MachineInstr &Inst) const {
5664 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5665 MachineBasicBlock *MBB = Inst.getParent();
5666 MachineOperand &Src0 = Inst.getOperand(1);
5667 MachineOperand &Src1 = Inst.getOperand(2);
5668 const DebugLoc &DL = Inst.getDebugLoc();
5669
5670 switch (Inst.getOpcode()) {
5671 case AMDGPU::S_PACK_LL_B32_B16: {
5672 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5673 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5674
5675 // FIXME: Can do a lot better if we know the high bits of src0 or src1 are
5676 // 0.
5677 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
5678 .addImm(0xffff);
5679
5680 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg)
5681 .addReg(ImmReg, RegState::Kill)
5682 .add(Src0);
5683
5684 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32), ResultReg)
5685 .add(Src1)
5686 .addImm(16)
5687 .addReg(TmpReg, RegState::Kill);
5688 break;
5689 }
5690 case AMDGPU::S_PACK_LH_B32_B16: {
5691 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5692 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
5693 .addImm(0xffff);
5694 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32), ResultReg)
5695 .addReg(ImmReg, RegState::Kill)
5696 .add(Src0)
5697 .add(Src1);
5698 break;
5699 }
5700 case AMDGPU::S_PACK_HH_B32_B16: {
5701 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5702 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5703 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg)
5704 .addImm(16)
5705 .add(Src0);
5706 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
5707 .addImm(0xffff0000);
5708 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32), ResultReg)
5709 .add(Src1)
5710 .addReg(ImmReg, RegState::Kill)
5711 .addReg(TmpReg, RegState::Kill);
5712 break;
5713 }
5714 default:
5715 llvm_unreachable("unhandled s_pack_* instruction");
5716 }
5717
5718 MachineOperand &Dest = Inst.getOperand(0);
5719 MRI.replaceRegWith(Dest.getReg(), ResultReg);
5720 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
5721 }
5722
addSCCDefUsersToVALUWorklist(MachineOperand & Op,MachineInstr & SCCDefInst,SetVectorType & Worklist) const5723 void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op,
5724 MachineInstr &SCCDefInst,
5725 SetVectorType &Worklist) const {
5726 // Ensure that def inst defines SCC, which is still live.
5727 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() &&
5728 !Op.isDead() && Op.getParent() == &SCCDefInst);
5729 // This assumes that all the users of SCC are in the same block
5730 // as the SCC def.
5731 for (MachineInstr &MI : // Skip the def inst itself.
5732 make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)),
5733 SCCDefInst.getParent()->end())) {
5734 // Check if SCC is used first.
5735 if (MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI) != -1)
5736 Worklist.insert(&MI);
5737 // Exit if we find another SCC def.
5738 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1)
5739 return;
5740 }
5741 }
5742
getDestEquivalentVGPRClass(const MachineInstr & Inst) const5743 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass(
5744 const MachineInstr &Inst) const {
5745 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0);
5746
5747 switch (Inst.getOpcode()) {
5748 // For target instructions, getOpRegClass just returns the virtual register
5749 // class associated with the operand, so we need to find an equivalent VGPR
5750 // register class in order to move the instruction to the VALU.
5751 case AMDGPU::COPY:
5752 case AMDGPU::PHI:
5753 case AMDGPU::REG_SEQUENCE:
5754 case AMDGPU::INSERT_SUBREG:
5755 case AMDGPU::WQM:
5756 case AMDGPU::SOFT_WQM:
5757 case AMDGPU::WWM: {
5758 const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1);
5759 if (RI.hasAGPRs(SrcRC)) {
5760 if (RI.hasAGPRs(NewDstRC))
5761 return nullptr;
5762
5763 switch (Inst.getOpcode()) {
5764 case AMDGPU::PHI:
5765 case AMDGPU::REG_SEQUENCE:
5766 case AMDGPU::INSERT_SUBREG:
5767 NewDstRC = RI.getEquivalentAGPRClass(NewDstRC);
5768 break;
5769 default:
5770 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
5771 }
5772
5773 if (!NewDstRC)
5774 return nullptr;
5775 } else {
5776 if (RI.hasVGPRs(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass)
5777 return nullptr;
5778
5779 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
5780 if (!NewDstRC)
5781 return nullptr;
5782 }
5783
5784 return NewDstRC;
5785 }
5786 default:
5787 return NewDstRC;
5788 }
5789 }
5790
5791 // Find the one SGPR operand we are allowed to use.
findUsedSGPR(const MachineInstr & MI,int OpIndices[3]) const5792 unsigned SIInstrInfo::findUsedSGPR(const MachineInstr &MI,
5793 int OpIndices[3]) const {
5794 const MCInstrDesc &Desc = MI.getDesc();
5795
5796 // Find the one SGPR operand we are allowed to use.
5797 //
5798 // First we need to consider the instruction's operand requirements before
5799 // legalizing. Some operands are required to be SGPRs, such as implicit uses
5800 // of VCC, but we are still bound by the constant bus requirement to only use
5801 // one.
5802 //
5803 // If the operand's class is an SGPR, we can never move it.
5804
5805 unsigned SGPRReg = findImplicitSGPRRead(MI);
5806 if (SGPRReg != AMDGPU::NoRegister)
5807 return SGPRReg;
5808
5809 unsigned UsedSGPRs[3] = { AMDGPU::NoRegister };
5810 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
5811
5812 for (unsigned i = 0; i < 3; ++i) {
5813 int Idx = OpIndices[i];
5814 if (Idx == -1)
5815 break;
5816
5817 const MachineOperand &MO = MI.getOperand(Idx);
5818 if (!MO.isReg())
5819 continue;
5820
5821 // Is this operand statically required to be an SGPR based on the operand
5822 // constraints?
5823 const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass);
5824 bool IsRequiredSGPR = RI.isSGPRClass(OpRC);
5825 if (IsRequiredSGPR)
5826 return MO.getReg();
5827
5828 // If this could be a VGPR or an SGPR, Check the dynamic register class.
5829 Register Reg = MO.getReg();
5830 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg);
5831 if (RI.isSGPRClass(RegRC))
5832 UsedSGPRs[i] = Reg;
5833 }
5834
5835 // We don't have a required SGPR operand, so we have a bit more freedom in
5836 // selecting operands to move.
5837
5838 // Try to select the most used SGPR. If an SGPR is equal to one of the
5839 // others, we choose that.
5840 //
5841 // e.g.
5842 // V_FMA_F32 v0, s0, s0, s0 -> No moves
5843 // V_FMA_F32 v0, s0, s1, s0 -> Move s1
5844
5845 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should
5846 // prefer those.
5847
5848 if (UsedSGPRs[0] != AMDGPU::NoRegister) {
5849 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2])
5850 SGPRReg = UsedSGPRs[0];
5851 }
5852
5853 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) {
5854 if (UsedSGPRs[1] == UsedSGPRs[2])
5855 SGPRReg = UsedSGPRs[1];
5856 }
5857
5858 return SGPRReg;
5859 }
5860
getNamedOperand(MachineInstr & MI,unsigned OperandName) const5861 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI,
5862 unsigned OperandName) const {
5863 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName);
5864 if (Idx == -1)
5865 return nullptr;
5866
5867 return &MI.getOperand(Idx);
5868 }
5869
getDefaultRsrcDataFormat() const5870 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const {
5871 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
5872 return (22ULL << 44) | // IMG_FORMAT_32_FLOAT
5873 (1ULL << 56) | // RESOURCE_LEVEL = 1
5874 (3ULL << 60); // OOB_SELECT = 3
5875 }
5876
5877 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT;
5878 if (ST.isAmdHsaOS()) {
5879 // Set ATC = 1. GFX9 doesn't have this bit.
5880 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS)
5881 RsrcDataFormat |= (1ULL << 56);
5882
5883 // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this.
5884 // BTW, it disables TC L2 and therefore decreases performance.
5885 if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS)
5886 RsrcDataFormat |= (2ULL << 59);
5887 }
5888
5889 return RsrcDataFormat;
5890 }
5891
getScratchRsrcWords23() const5892 uint64_t SIInstrInfo::getScratchRsrcWords23() const {
5893 uint64_t Rsrc23 = getDefaultRsrcDataFormat() |
5894 AMDGPU::RSRC_TID_ENABLE |
5895 0xffffffff; // Size;
5896
5897 // GFX9 doesn't have ELEMENT_SIZE.
5898 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
5899 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize()) - 1;
5900 Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT;
5901 }
5902
5903 // IndexStride = 64 / 32.
5904 uint64_t IndexStride = ST.getWavefrontSize() == 64 ? 3 : 2;
5905 Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT;
5906
5907 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17].
5908 // Clear them unless we want a huge stride.
5909 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
5910 ST.getGeneration() <= AMDGPUSubtarget::GFX9)
5911 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT;
5912
5913 return Rsrc23;
5914 }
5915
isLowLatencyInstruction(const MachineInstr & MI) const5916 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const {
5917 unsigned Opc = MI.getOpcode();
5918
5919 return isSMRD(Opc);
5920 }
5921
isHighLatencyInstruction(const MachineInstr & MI) const5922 bool SIInstrInfo::isHighLatencyInstruction(const MachineInstr &MI) const {
5923 unsigned Opc = MI.getOpcode();
5924
5925 return isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc);
5926 }
5927
isStackAccess(const MachineInstr & MI,int & FrameIndex) const5928 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI,
5929 int &FrameIndex) const {
5930 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
5931 if (!Addr || !Addr->isFI())
5932 return AMDGPU::NoRegister;
5933
5934 assert(!MI.memoperands_empty() &&
5935 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS);
5936
5937 FrameIndex = Addr->getIndex();
5938 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg();
5939 }
5940
isSGPRStackAccess(const MachineInstr & MI,int & FrameIndex) const5941 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI,
5942 int &FrameIndex) const {
5943 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr);
5944 assert(Addr && Addr->isFI());
5945 FrameIndex = Addr->getIndex();
5946 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg();
5947 }
5948
isLoadFromStackSlot(const MachineInstr & MI,int & FrameIndex) const5949 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
5950 int &FrameIndex) const {
5951 if (!MI.mayLoad())
5952 return AMDGPU::NoRegister;
5953
5954 if (isMUBUF(MI) || isVGPRSpill(MI))
5955 return isStackAccess(MI, FrameIndex);
5956
5957 if (isSGPRSpill(MI))
5958 return isSGPRStackAccess(MI, FrameIndex);
5959
5960 return AMDGPU::NoRegister;
5961 }
5962
isStoreToStackSlot(const MachineInstr & MI,int & FrameIndex) const5963 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
5964 int &FrameIndex) const {
5965 if (!MI.mayStore())
5966 return AMDGPU::NoRegister;
5967
5968 if (isMUBUF(MI) || isVGPRSpill(MI))
5969 return isStackAccess(MI, FrameIndex);
5970
5971 if (isSGPRSpill(MI))
5972 return isSGPRStackAccess(MI, FrameIndex);
5973
5974 return AMDGPU::NoRegister;
5975 }
5976
getInstBundleSize(const MachineInstr & MI) const5977 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const {
5978 unsigned Size = 0;
5979 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
5980 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
5981 while (++I != E && I->isInsideBundle()) {
5982 assert(!I->isBundle() && "No nested bundle!");
5983 Size += getInstSizeInBytes(*I);
5984 }
5985
5986 return Size;
5987 }
5988
getInstSizeInBytes(const MachineInstr & MI) const5989 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
5990 unsigned Opc = MI.getOpcode();
5991 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc);
5992 unsigned DescSize = Desc.getSize();
5993
5994 // If we have a definitive size, we can use it. Otherwise we need to inspect
5995 // the operands to know the size.
5996 if (isFixedSize(MI))
5997 return DescSize;
5998
5999 // 4-byte instructions may have a 32-bit literal encoded after them. Check
6000 // operands that coud ever be literals.
6001 if (isVALU(MI) || isSALU(MI)) {
6002 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
6003 if (Src0Idx == -1)
6004 return DescSize; // No operands.
6005
6006 if (isLiteralConstantLike(MI.getOperand(Src0Idx), Desc.OpInfo[Src0Idx]))
6007 return isVOP3(MI) ? 12 : (DescSize + 4);
6008
6009 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
6010 if (Src1Idx == -1)
6011 return DescSize;
6012
6013 if (isLiteralConstantLike(MI.getOperand(Src1Idx), Desc.OpInfo[Src1Idx]))
6014 return isVOP3(MI) ? 12 : (DescSize + 4);
6015
6016 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
6017 if (Src2Idx == -1)
6018 return DescSize;
6019
6020 if (isLiteralConstantLike(MI.getOperand(Src2Idx), Desc.OpInfo[Src2Idx]))
6021 return isVOP3(MI) ? 12 : (DescSize + 4);
6022
6023 return DescSize;
6024 }
6025
6026 // Check whether we have extra NSA words.
6027 if (isMIMG(MI)) {
6028 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0);
6029 if (VAddr0Idx < 0)
6030 return 8;
6031
6032 int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc);
6033 return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4);
6034 }
6035
6036 switch (Opc) {
6037 case TargetOpcode::IMPLICIT_DEF:
6038 case TargetOpcode::KILL:
6039 case TargetOpcode::DBG_VALUE:
6040 case TargetOpcode::EH_LABEL:
6041 return 0;
6042 case TargetOpcode::BUNDLE:
6043 return getInstBundleSize(MI);
6044 case TargetOpcode::INLINEASM:
6045 case TargetOpcode::INLINEASM_BR: {
6046 const MachineFunction *MF = MI.getParent()->getParent();
6047 const char *AsmStr = MI.getOperand(0).getSymbolName();
6048 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(),
6049 &MF->getSubtarget());
6050 }
6051 default:
6052 return DescSize;
6053 }
6054 }
6055
mayAccessFlatAddressSpace(const MachineInstr & MI) const6056 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const {
6057 if (!isFLAT(MI))
6058 return false;
6059
6060 if (MI.memoperands_empty())
6061 return true;
6062
6063 for (const MachineMemOperand *MMO : MI.memoperands()) {
6064 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS)
6065 return true;
6066 }
6067 return false;
6068 }
6069
isNonUniformBranchInstr(MachineInstr & Branch) const6070 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const {
6071 return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO;
6072 }
6073
convertNonUniformIfRegion(MachineBasicBlock * IfEntry,MachineBasicBlock * IfEnd) const6074 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry,
6075 MachineBasicBlock *IfEnd) const {
6076 MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator();
6077 assert(TI != IfEntry->end());
6078
6079 MachineInstr *Branch = &(*TI);
6080 MachineFunction *MF = IfEntry->getParent();
6081 MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo();
6082
6083 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
6084 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC());
6085 MachineInstr *SIIF =
6086 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg)
6087 .add(Branch->getOperand(0))
6088 .add(Branch->getOperand(1));
6089 MachineInstr *SIEND =
6090 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF))
6091 .addReg(DstReg);
6092
6093 IfEntry->erase(TI);
6094 IfEntry->insert(IfEntry->end(), SIIF);
6095 IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND);
6096 }
6097 }
6098
convertNonUniformLoopRegion(MachineBasicBlock * LoopEntry,MachineBasicBlock * LoopEnd) const6099 void SIInstrInfo::convertNonUniformLoopRegion(
6100 MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const {
6101 MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator();
6102 // We expect 2 terminators, one conditional and one unconditional.
6103 assert(TI != LoopEnd->end());
6104
6105 MachineInstr *Branch = &(*TI);
6106 MachineFunction *MF = LoopEnd->getParent();
6107 MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo();
6108
6109 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
6110
6111 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC());
6112 Register BackEdgeReg = MRI.createVirtualRegister(RI.getBoolRC());
6113 MachineInstrBuilder HeaderPHIBuilder =
6114 BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg);
6115 for (MachineBasicBlock::pred_iterator PI = LoopEntry->pred_begin(),
6116 E = LoopEntry->pred_end();
6117 PI != E; ++PI) {
6118 if (*PI == LoopEnd) {
6119 HeaderPHIBuilder.addReg(BackEdgeReg);
6120 } else {
6121 MachineBasicBlock *PMBB = *PI;
6122 Register ZeroReg = MRI.createVirtualRegister(RI.getBoolRC());
6123 materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(),
6124 ZeroReg, 0);
6125 HeaderPHIBuilder.addReg(ZeroReg);
6126 }
6127 HeaderPHIBuilder.addMBB(*PI);
6128 }
6129 MachineInstr *HeaderPhi = HeaderPHIBuilder;
6130 MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(),
6131 get(AMDGPU::SI_IF_BREAK), BackEdgeReg)
6132 .addReg(DstReg)
6133 .add(Branch->getOperand(0));
6134 MachineInstr *SILOOP =
6135 BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP))
6136 .addReg(BackEdgeReg)
6137 .addMBB(LoopEntry);
6138
6139 LoopEntry->insert(LoopEntry->begin(), HeaderPhi);
6140 LoopEnd->erase(TI);
6141 LoopEnd->insert(LoopEnd->end(), SIIFBREAK);
6142 LoopEnd->insert(LoopEnd->end(), SILOOP);
6143 }
6144 }
6145
6146 ArrayRef<std::pair<int, const char *>>
getSerializableTargetIndices() const6147 SIInstrInfo::getSerializableTargetIndices() const {
6148 static const std::pair<int, const char *> TargetIndices[] = {
6149 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"},
6150 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"},
6151 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"},
6152 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"},
6153 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}};
6154 return makeArrayRef(TargetIndices);
6155 }
6156
6157 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The
6158 /// post-RA version of misched uses CreateTargetMIHazardRecognizer.
6159 ScheduleHazardRecognizer *
CreateTargetPostRAHazardRecognizer(const InstrItineraryData * II,const ScheduleDAG * DAG) const6160 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
6161 const ScheduleDAG *DAG) const {
6162 return new GCNHazardRecognizer(DAG->MF);
6163 }
6164
6165 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer
6166 /// pass.
6167 ScheduleHazardRecognizer *
CreateTargetPostRAHazardRecognizer(const MachineFunction & MF) const6168 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const {
6169 return new GCNHazardRecognizer(MF);
6170 }
6171
6172 std::pair<unsigned, unsigned>
decomposeMachineOperandsTargetFlags(unsigned TF) const6173 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
6174 return std::make_pair(TF & MO_MASK, TF & ~MO_MASK);
6175 }
6176
6177 ArrayRef<std::pair<unsigned, const char *>>
getSerializableDirectMachineOperandTargetFlags() const6178 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
6179 static const std::pair<unsigned, const char *> TargetFlags[] = {
6180 { MO_GOTPCREL, "amdgpu-gotprel" },
6181 { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" },
6182 { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" },
6183 { MO_REL32_LO, "amdgpu-rel32-lo" },
6184 { MO_REL32_HI, "amdgpu-rel32-hi" },
6185 { MO_ABS32_LO, "amdgpu-abs32-lo" },
6186 { MO_ABS32_HI, "amdgpu-abs32-hi" },
6187 };
6188
6189 return makeArrayRef(TargetFlags);
6190 }
6191
isBasicBlockPrologue(const MachineInstr & MI) const6192 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI) const {
6193 return !MI.isTerminator() && MI.getOpcode() != AMDGPU::COPY &&
6194 MI.modifiesRegister(AMDGPU::EXEC, &RI);
6195 }
6196
6197 MachineInstrBuilder
getAddNoCarry(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,unsigned DestReg) const6198 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB,
6199 MachineBasicBlock::iterator I,
6200 const DebugLoc &DL,
6201 unsigned DestReg) const {
6202 if (ST.hasAddNoCarry())
6203 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg);
6204
6205 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6206 Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC());
6207 MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC());
6208
6209 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_I32_e64), DestReg)
6210 .addReg(UnusedCarry, RegState::Define | RegState::Dead);
6211 }
6212
getAddNoCarry(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,Register DestReg,RegScavenger & RS) const6213 MachineInstrBuilder SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB,
6214 MachineBasicBlock::iterator I,
6215 const DebugLoc &DL,
6216 Register DestReg,
6217 RegScavenger &RS) const {
6218 if (ST.hasAddNoCarry())
6219 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg);
6220
6221 // If available, prefer to use vcc.
6222 Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC)
6223 ? Register(RI.getVCC())
6224 : RS.scavengeRegister(RI.getBoolRC(), I, 0, false);
6225
6226 // TODO: Users need to deal with this.
6227 if (!UnusedCarry.isValid())
6228 return MachineInstrBuilder();
6229
6230 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_I32_e64), DestReg)
6231 .addReg(UnusedCarry, RegState::Define | RegState::Dead);
6232 }
6233
isKillTerminator(unsigned Opcode)6234 bool SIInstrInfo::isKillTerminator(unsigned Opcode) {
6235 switch (Opcode) {
6236 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
6237 case AMDGPU::SI_KILL_I1_TERMINATOR:
6238 return true;
6239 default:
6240 return false;
6241 }
6242 }
6243
getKillTerminatorFromPseudo(unsigned Opcode) const6244 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const {
6245 switch (Opcode) {
6246 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO:
6247 return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR);
6248 case AMDGPU::SI_KILL_I1_PSEUDO:
6249 return get(AMDGPU::SI_KILL_I1_TERMINATOR);
6250 default:
6251 llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO");
6252 }
6253 }
6254
fixImplicitOperands(MachineInstr & MI) const6255 void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const {
6256 MachineBasicBlock *MBB = MI.getParent();
6257 MachineFunction *MF = MBB->getParent();
6258 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
6259
6260 if (!ST.isWave32())
6261 return;
6262
6263 for (auto &Op : MI.implicit_operands()) {
6264 if (Op.isReg() && Op.getReg() == AMDGPU::VCC)
6265 Op.setReg(AMDGPU::VCC_LO);
6266 }
6267 }
6268
isBufferSMRD(const MachineInstr & MI) const6269 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const {
6270 if (!isSMRD(MI))
6271 return false;
6272
6273 // Check that it is using a buffer resource.
6274 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase);
6275 if (Idx == -1) // e.g. s_memtime
6276 return false;
6277
6278 const auto RCID = MI.getDesc().OpInfo[Idx].RegClass;
6279 return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass);
6280 }
6281
getNumFlatOffsetBits(unsigned AddrSpace,bool Signed) const6282 unsigned SIInstrInfo::getNumFlatOffsetBits(unsigned AddrSpace,
6283 bool Signed) const {
6284 if (!ST.hasFlatInstOffsets())
6285 return 0;
6286
6287 if (ST.hasFlatSegmentOffsetBug() && AddrSpace == AMDGPUAS::FLAT_ADDRESS)
6288 return 0;
6289
6290 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10)
6291 return Signed ? 12 : 11;
6292
6293 return Signed ? 13 : 12;
6294 }
6295
isLegalFLATOffset(int64_t Offset,unsigned AddrSpace,bool Signed) const6296 bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace,
6297 bool Signed) const {
6298 // TODO: Should 0 be special cased?
6299 if (!ST.hasFlatInstOffsets())
6300 return false;
6301
6302 if (ST.hasFlatSegmentOffsetBug() && AddrSpace == AMDGPUAS::FLAT_ADDRESS)
6303 return false;
6304
6305 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
6306 return (Signed && isInt<12>(Offset)) ||
6307 (!Signed && isUInt<11>(Offset));
6308 }
6309
6310 return (Signed && isInt<13>(Offset)) ||
6311 (!Signed && isUInt<12>(Offset));
6312 }
6313
6314
6315 // This must be kept in sync with the SIEncodingFamily class in SIInstrInfo.td
6316 enum SIEncodingFamily {
6317 SI = 0,
6318 VI = 1,
6319 SDWA = 2,
6320 SDWA9 = 3,
6321 GFX80 = 4,
6322 GFX9 = 5,
6323 GFX10 = 6,
6324 SDWA10 = 7
6325 };
6326
subtargetEncodingFamily(const GCNSubtarget & ST)6327 static SIEncodingFamily subtargetEncodingFamily(const GCNSubtarget &ST) {
6328 switch (ST.getGeneration()) {
6329 default:
6330 break;
6331 case AMDGPUSubtarget::SOUTHERN_ISLANDS:
6332 case AMDGPUSubtarget::SEA_ISLANDS:
6333 return SIEncodingFamily::SI;
6334 case AMDGPUSubtarget::VOLCANIC_ISLANDS:
6335 case AMDGPUSubtarget::GFX9:
6336 return SIEncodingFamily::VI;
6337 case AMDGPUSubtarget::GFX10:
6338 return SIEncodingFamily::GFX10;
6339 }
6340 llvm_unreachable("Unknown subtarget generation!");
6341 }
6342
isAsmOnlyOpcode(int MCOp) const6343 bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const {
6344 switch(MCOp) {
6345 // These opcodes use indirect register addressing so
6346 // they need special handling by codegen (currently missing).
6347 // Therefore it is too risky to allow these opcodes
6348 // to be selected by dpp combiner or sdwa peepholer.
6349 case AMDGPU::V_MOVRELS_B32_dpp_gfx10:
6350 case AMDGPU::V_MOVRELS_B32_sdwa_gfx10:
6351 case AMDGPU::V_MOVRELD_B32_dpp_gfx10:
6352 case AMDGPU::V_MOVRELD_B32_sdwa_gfx10:
6353 case AMDGPU::V_MOVRELSD_B32_dpp_gfx10:
6354 case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10:
6355 case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10:
6356 case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10:
6357 return true;
6358 default:
6359 return false;
6360 }
6361 }
6362
pseudoToMCOpcode(int Opcode) const6363 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const {
6364 SIEncodingFamily Gen = subtargetEncodingFamily(ST);
6365
6366 if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 &&
6367 ST.getGeneration() == AMDGPUSubtarget::GFX9)
6368 Gen = SIEncodingFamily::GFX9;
6369
6370 // Adjust the encoding family to GFX80 for D16 buffer instructions when the
6371 // subtarget has UnpackedD16VMem feature.
6372 // TODO: remove this when we discard GFX80 encoding.
6373 if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf))
6374 Gen = SIEncodingFamily::GFX80;
6375
6376 if (get(Opcode).TSFlags & SIInstrFlags::SDWA) {
6377 switch (ST.getGeneration()) {
6378 default:
6379 Gen = SIEncodingFamily::SDWA;
6380 break;
6381 case AMDGPUSubtarget::GFX9:
6382 Gen = SIEncodingFamily::SDWA9;
6383 break;
6384 case AMDGPUSubtarget::GFX10:
6385 Gen = SIEncodingFamily::SDWA10;
6386 break;
6387 }
6388 }
6389
6390 int MCOp = AMDGPU::getMCOpcode(Opcode, Gen);
6391
6392 // -1 means that Opcode is already a native instruction.
6393 if (MCOp == -1)
6394 return Opcode;
6395
6396 // (uint16_t)-1 means that Opcode is a pseudo instruction that has
6397 // no encoding in the given subtarget generation.
6398 if (MCOp == (uint16_t)-1)
6399 return -1;
6400
6401 if (isAsmOnlyOpcode(MCOp))
6402 return -1;
6403
6404 return MCOp;
6405 }
6406
6407 static
getRegOrUndef(const MachineOperand & RegOpnd)6408 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) {
6409 assert(RegOpnd.isReg());
6410 return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() :
6411 getRegSubRegPair(RegOpnd);
6412 }
6413
6414 TargetInstrInfo::RegSubRegPair
getRegSequenceSubReg(MachineInstr & MI,unsigned SubReg)6415 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) {
6416 assert(MI.isRegSequence());
6417 for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I)
6418 if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) {
6419 auto &RegOp = MI.getOperand(1 + 2 * I);
6420 return getRegOrUndef(RegOp);
6421 }
6422 return TargetInstrInfo::RegSubRegPair();
6423 }
6424
6425 // Try to find the definition of reg:subreg in subreg-manipulation pseudos
6426 // Following a subreg of reg:subreg isn't supported
followSubRegDef(MachineInstr & MI,TargetInstrInfo::RegSubRegPair & RSR)6427 static bool followSubRegDef(MachineInstr &MI,
6428 TargetInstrInfo::RegSubRegPair &RSR) {
6429 if (!RSR.SubReg)
6430 return false;
6431 switch (MI.getOpcode()) {
6432 default: break;
6433 case AMDGPU::REG_SEQUENCE:
6434 RSR = getRegSequenceSubReg(MI, RSR.SubReg);
6435 return true;
6436 // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg
6437 case AMDGPU::INSERT_SUBREG:
6438 if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm())
6439 // inserted the subreg we're looking for
6440 RSR = getRegOrUndef(MI.getOperand(2));
6441 else { // the subreg in the rest of the reg
6442 auto R1 = getRegOrUndef(MI.getOperand(1));
6443 if (R1.SubReg) // subreg of subreg isn't supported
6444 return false;
6445 RSR.Reg = R1.Reg;
6446 }
6447 return true;
6448 }
6449 return false;
6450 }
6451
getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair & P,MachineRegisterInfo & MRI)6452 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P,
6453 MachineRegisterInfo &MRI) {
6454 assert(MRI.isSSA());
6455 if (!Register::isVirtualRegister(P.Reg))
6456 return nullptr;
6457
6458 auto RSR = P;
6459 auto *DefInst = MRI.getVRegDef(RSR.Reg);
6460 while (auto *MI = DefInst) {
6461 DefInst = nullptr;
6462 switch (MI->getOpcode()) {
6463 case AMDGPU::COPY:
6464 case AMDGPU::V_MOV_B32_e32: {
6465 auto &Op1 = MI->getOperand(1);
6466 if (Op1.isReg() && Register::isVirtualRegister(Op1.getReg())) {
6467 if (Op1.isUndef())
6468 return nullptr;
6469 RSR = getRegSubRegPair(Op1);
6470 DefInst = MRI.getVRegDef(RSR.Reg);
6471 }
6472 break;
6473 }
6474 default:
6475 if (followSubRegDef(*MI, RSR)) {
6476 if (!RSR.Reg)
6477 return nullptr;
6478 DefInst = MRI.getVRegDef(RSR.Reg);
6479 }
6480 }
6481 if (!DefInst)
6482 return MI;
6483 }
6484 return nullptr;
6485 }
6486
execMayBeModifiedBeforeUse(const MachineRegisterInfo & MRI,Register VReg,const MachineInstr & DefMI,const MachineInstr & UseMI)6487 bool llvm::execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI,
6488 Register VReg,
6489 const MachineInstr &DefMI,
6490 const MachineInstr &UseMI) {
6491 assert(MRI.isSSA() && "Must be run on SSA");
6492
6493 auto *TRI = MRI.getTargetRegisterInfo();
6494 auto *DefBB = DefMI.getParent();
6495
6496 // Don't bother searching between blocks, although it is possible this block
6497 // doesn't modify exec.
6498 if (UseMI.getParent() != DefBB)
6499 return true;
6500
6501 const int MaxInstScan = 20;
6502 int NumInst = 0;
6503
6504 // Stop scan at the use.
6505 auto E = UseMI.getIterator();
6506 for (auto I = std::next(DefMI.getIterator()); I != E; ++I) {
6507 if (I->isDebugInstr())
6508 continue;
6509
6510 if (++NumInst > MaxInstScan)
6511 return true;
6512
6513 if (I->modifiesRegister(AMDGPU::EXEC, TRI))
6514 return true;
6515 }
6516
6517 return false;
6518 }
6519
execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo & MRI,Register VReg,const MachineInstr & DefMI)6520 bool llvm::execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI,
6521 Register VReg,
6522 const MachineInstr &DefMI) {
6523 assert(MRI.isSSA() && "Must be run on SSA");
6524
6525 auto *TRI = MRI.getTargetRegisterInfo();
6526 auto *DefBB = DefMI.getParent();
6527
6528 const int MaxUseInstScan = 10;
6529 int NumUseInst = 0;
6530
6531 for (auto &UseInst : MRI.use_nodbg_instructions(VReg)) {
6532 // Don't bother searching between blocks, although it is possible this block
6533 // doesn't modify exec.
6534 if (UseInst.getParent() != DefBB)
6535 return true;
6536
6537 if (++NumUseInst > MaxUseInstScan)
6538 return true;
6539 }
6540
6541 const int MaxInstScan = 20;
6542 int NumInst = 0;
6543
6544 // Stop scan when we have seen all the uses.
6545 for (auto I = std::next(DefMI.getIterator()); ; ++I) {
6546 if (I->isDebugInstr())
6547 continue;
6548
6549 if (++NumInst > MaxInstScan)
6550 return true;
6551
6552 if (I->readsRegister(VReg))
6553 if (--NumUseInst == 0)
6554 return false;
6555
6556 if (I->modifiesRegister(AMDGPU::EXEC, TRI))
6557 return true;
6558 }
6559 }
6560
createPHIDestinationCopy(MachineBasicBlock & MBB,MachineBasicBlock::iterator LastPHIIt,const DebugLoc & DL,Register Src,Register Dst) const6561 MachineInstr *SIInstrInfo::createPHIDestinationCopy(
6562 MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt,
6563 const DebugLoc &DL, Register Src, Register Dst) const {
6564 auto Cur = MBB.begin();
6565 if (Cur != MBB.end())
6566 do {
6567 if (!Cur->isPHI() && Cur->readsRegister(Dst))
6568 return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src);
6569 ++Cur;
6570 } while (Cur != MBB.end() && Cur != LastPHIIt);
6571
6572 return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src,
6573 Dst);
6574 }
6575
createPHISourceCopy(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsPt,const DebugLoc & DL,Register Src,unsigned SrcSubReg,Register Dst) const6576 MachineInstr *SIInstrInfo::createPHISourceCopy(
6577 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt,
6578 const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const {
6579 if (InsPt != MBB.end() &&
6580 (InsPt->getOpcode() == AMDGPU::SI_IF ||
6581 InsPt->getOpcode() == AMDGPU::SI_ELSE ||
6582 InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) &&
6583 InsPt->definesRegister(Src)) {
6584 InsPt++;
6585 return BuildMI(MBB, InsPt, DL,
6586 get(ST.isWave32() ? AMDGPU::S_MOV_B32_term
6587 : AMDGPU::S_MOV_B64_term),
6588 Dst)
6589 .addReg(Src, 0, SrcSubReg)
6590 .addReg(AMDGPU::EXEC, RegState::Implicit);
6591 }
6592 return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg,
6593 Dst);
6594 }
6595
isWave32() const6596 bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); }
6597
foldMemoryOperandImpl(MachineFunction & MF,MachineInstr & MI,ArrayRef<unsigned> Ops,MachineBasicBlock::iterator InsertPt,int FrameIndex,LiveIntervals * LIS,VirtRegMap * VRM) const6598 MachineInstr *SIInstrInfo::foldMemoryOperandImpl(
6599 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
6600 MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
6601 VirtRegMap *VRM) const {
6602 // This is a bit of a hack (copied from AArch64). Consider this instruction:
6603 //
6604 // %0:sreg_32 = COPY $m0
6605 //
6606 // We explicitly chose SReg_32 for the virtual register so such a copy might
6607 // be eliminated by RegisterCoalescer. However, that may not be possible, and
6608 // %0 may even spill. We can't spill $m0 normally (it would require copying to
6609 // a numbered SGPR anyway), and since it is in the SReg_32 register class,
6610 // TargetInstrInfo::foldMemoryOperand() is going to try.
6611 //
6612 // To prevent that, constrain the %0 register class here.
6613 if (MI.isFullCopy()) {
6614 Register DstReg = MI.getOperand(0).getReg();
6615 Register SrcReg = MI.getOperand(1).getReg();
6616
6617 if (DstReg == AMDGPU::M0 && SrcReg.isVirtual()) {
6618 MF.getRegInfo().constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0RegClass);
6619 return nullptr;
6620 }
6621
6622 if (SrcReg == AMDGPU::M0 && DstReg.isVirtual()) {
6623 MF.getRegInfo().constrainRegClass(DstReg, &AMDGPU::SReg_32_XM0RegClass);
6624 return nullptr;
6625 }
6626 }
6627
6628 return nullptr;
6629 }
6630
getInstrLatency(const InstrItineraryData * ItinData,const MachineInstr & MI,unsigned * PredCost) const6631 unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
6632 const MachineInstr &MI,
6633 unsigned *PredCost) const {
6634 if (MI.isBundle()) {
6635 MachineBasicBlock::const_instr_iterator I(MI.getIterator());
6636 MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end());
6637 unsigned Lat = 0, Count = 0;
6638 for (++I; I != E && I->isBundledWithPred(); ++I) {
6639 ++Count;
6640 Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I));
6641 }
6642 return Lat + Count - 1;
6643 }
6644
6645 return SchedModel.computeInstrLatency(&MI);
6646 }
6647