1 //===- SIInstrInfo.cpp - SI Instruction Information  ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// SI Implementation of TargetInstrInfo.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "SIInstrInfo.h"
15 #include "AMDGPU.h"
16 #include "AMDGPUInstrInfo.h"
17 #include "GCNHazardRecognizer.h"
18 #include "GCNSubtarget.h"
19 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
20 #include "SIMachineFunctionInfo.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/CodeGen/LiveIntervals.h"
23 #include "llvm/CodeGen/LiveVariables.h"
24 #include "llvm/CodeGen/MachineDominators.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineScheduler.h"
27 #include "llvm/CodeGen/RegisterScavenging.h"
28 #include "llvm/CodeGen/ScheduleDAG.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/IntrinsicsAMDGPU.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Target/TargetMachine.h"
34 
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "si-instr-info"
38 
39 #define GET_INSTRINFO_CTOR_DTOR
40 #include "AMDGPUGenInstrInfo.inc"
41 
42 namespace llvm {
43 
44 class AAResults;
45 
46 namespace AMDGPU {
47 #define GET_D16ImageDimIntrinsics_IMPL
48 #define GET_ImageDimIntrinsicTable_IMPL
49 #define GET_RsrcIntrinsics_IMPL
50 #include "AMDGPUGenSearchableTables.inc"
51 }
52 }
53 
54 
55 // Must be at least 4 to be able to branch over minimum unconditional branch
56 // code. This is only for making it possible to write reasonably small tests for
57 // long branches.
58 static cl::opt<unsigned>
59 BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16),
60                  cl::desc("Restrict range of branch instructions (DEBUG)"));
61 
62 static cl::opt<bool> Fix16BitCopies(
63   "amdgpu-fix-16-bit-physreg-copies",
64   cl::desc("Fix copies between 32 and 16 bit registers by extending to 32 bit"),
65   cl::init(true),
66   cl::ReallyHidden);
67 
68 SIInstrInfo::SIInstrInfo(const GCNSubtarget &ST)
69   : AMDGPUGenInstrInfo(AMDGPU::ADJCALLSTACKUP, AMDGPU::ADJCALLSTACKDOWN),
70     RI(ST), ST(ST) {
71   SchedModel.init(&ST);
72 }
73 
74 //===----------------------------------------------------------------------===//
75 // TargetInstrInfo callbacks
76 //===----------------------------------------------------------------------===//
77 
78 static unsigned getNumOperandsNoGlue(SDNode *Node) {
79   unsigned N = Node->getNumOperands();
80   while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
81     --N;
82   return N;
83 }
84 
85 /// Returns true if both nodes have the same value for the given
86 ///        operand \p Op, or if both nodes do not have this operand.
87 static bool nodesHaveSameOperandValue(SDNode *N0, SDNode* N1, unsigned OpName) {
88   unsigned Opc0 = N0->getMachineOpcode();
89   unsigned Opc1 = N1->getMachineOpcode();
90 
91   int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName);
92   int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName);
93 
94   if (Op0Idx == -1 && Op1Idx == -1)
95     return true;
96 
97 
98   if ((Op0Idx == -1 && Op1Idx != -1) ||
99       (Op1Idx == -1 && Op0Idx != -1))
100     return false;
101 
102   // getNamedOperandIdx returns the index for the MachineInstr's operands,
103   // which includes the result as the first operand. We are indexing into the
104   // MachineSDNode's operands, so we need to skip the result operand to get
105   // the real index.
106   --Op0Idx;
107   --Op1Idx;
108 
109   return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx);
110 }
111 
112 bool SIInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
113                                                     AAResults *AA) const {
114   if (isVOP1(MI) || isVOP2(MI) || isVOP3(MI) || isSDWA(MI) || isSALU(MI)) {
115     // Normally VALU use of exec would block the rematerialization, but that
116     // is OK in this case to have an implicit exec read as all VALU do.
117     // We really want all of the generic logic for this except for this.
118 
119     // Another potential implicit use is mode register. The core logic of
120     // the RA will not attempt rematerialization if mode is set anywhere
121     // in the function, otherwise it is safe since mode is not changed.
122 
123     // There is difference to generic method which does not allow
124     // rematerialization if there are virtual register uses. We allow this,
125     // therefore this method includes SOP instructions as well.
126     return !MI.hasImplicitDef() &&
127            MI.getNumImplicitOperands() == MI.getDesc().getNumImplicitUses() &&
128            !MI.mayRaiseFPException();
129   }
130 
131   return false;
132 }
133 
134 // Returns true if the scalar result of a VALU instruction depends on exec.
135 static bool resultDependsOnExec(const MachineInstr &MI) {
136   // Ignore comparisons which are only used masked with exec.
137   // This allows some hoisting/sinking of VALU comparisons.
138   if (MI.isCompare()) {
139     const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
140     Register DstReg = MI.getOperand(0).getReg();
141     if (!DstReg.isVirtual())
142       return true;
143     for (MachineInstr &Use : MRI.use_nodbg_instructions(DstReg)) {
144       switch (Use.getOpcode()) {
145       case AMDGPU::S_AND_SAVEEXEC_B32:
146       case AMDGPU::S_AND_SAVEEXEC_B64:
147         break;
148       case AMDGPU::S_AND_B32:
149       case AMDGPU::S_AND_B64:
150         if (!Use.readsRegister(AMDGPU::EXEC))
151           return true;
152         break;
153       default:
154         return true;
155       }
156     }
157     return false;
158   }
159 
160   switch (MI.getOpcode()) {
161   default:
162     break;
163   case AMDGPU::V_READFIRSTLANE_B32:
164     return true;
165   }
166 
167   return false;
168 }
169 
170 bool SIInstrInfo::isIgnorableUse(const MachineOperand &MO) const {
171   // Any implicit use of exec by VALU is not a real register read.
172   return MO.getReg() == AMDGPU::EXEC && MO.isImplicit() &&
173          isVALU(*MO.getParent()) && !resultDependsOnExec(*MO.getParent());
174 }
175 
176 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1,
177                                           int64_t &Offset0,
178                                           int64_t &Offset1) const {
179   if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode())
180     return false;
181 
182   unsigned Opc0 = Load0->getMachineOpcode();
183   unsigned Opc1 = Load1->getMachineOpcode();
184 
185   // Make sure both are actually loads.
186   if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad())
187     return false;
188 
189   if (isDS(Opc0) && isDS(Opc1)) {
190 
191     // FIXME: Handle this case:
192     if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1))
193       return false;
194 
195     // Check base reg.
196     if (Load0->getOperand(0) != Load1->getOperand(0))
197       return false;
198 
199     // Skip read2 / write2 variants for simplicity.
200     // TODO: We should report true if the used offsets are adjacent (excluded
201     // st64 versions).
202     int Offset0Idx = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
203     int Offset1Idx = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
204     if (Offset0Idx == -1 || Offset1Idx == -1)
205       return false;
206 
207     // XXX - be careful of dataless loads
208     // getNamedOperandIdx returns the index for MachineInstrs.  Since they
209     // include the output in the operand list, but SDNodes don't, we need to
210     // subtract the index by one.
211     Offset0Idx -= get(Opc0).NumDefs;
212     Offset1Idx -= get(Opc1).NumDefs;
213     Offset0 = cast<ConstantSDNode>(Load0->getOperand(Offset0Idx))->getZExtValue();
214     Offset1 = cast<ConstantSDNode>(Load1->getOperand(Offset1Idx))->getZExtValue();
215     return true;
216   }
217 
218   if (isSMRD(Opc0) && isSMRD(Opc1)) {
219     // Skip time and cache invalidation instructions.
220     if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::sbase) == -1 ||
221         AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::sbase) == -1)
222       return false;
223 
224     assert(getNumOperandsNoGlue(Load0) == getNumOperandsNoGlue(Load1));
225 
226     // Check base reg.
227     if (Load0->getOperand(0) != Load1->getOperand(0))
228       return false;
229 
230     const ConstantSDNode *Load0Offset =
231         dyn_cast<ConstantSDNode>(Load0->getOperand(1));
232     const ConstantSDNode *Load1Offset =
233         dyn_cast<ConstantSDNode>(Load1->getOperand(1));
234 
235     if (!Load0Offset || !Load1Offset)
236       return false;
237 
238     Offset0 = Load0Offset->getZExtValue();
239     Offset1 = Load1Offset->getZExtValue();
240     return true;
241   }
242 
243   // MUBUF and MTBUF can access the same addresses.
244   if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) {
245 
246     // MUBUF and MTBUF have vaddr at different indices.
247     if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) ||
248         !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) ||
249         !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc))
250       return false;
251 
252     int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
253     int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
254 
255     if (OffIdx0 == -1 || OffIdx1 == -1)
256       return false;
257 
258     // getNamedOperandIdx returns the index for MachineInstrs.  Since they
259     // include the output in the operand list, but SDNodes don't, we need to
260     // subtract the index by one.
261     OffIdx0 -= get(Opc0).NumDefs;
262     OffIdx1 -= get(Opc1).NumDefs;
263 
264     SDValue Off0 = Load0->getOperand(OffIdx0);
265     SDValue Off1 = Load1->getOperand(OffIdx1);
266 
267     // The offset might be a FrameIndexSDNode.
268     if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1))
269       return false;
270 
271     Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue();
272     Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue();
273     return true;
274   }
275 
276   return false;
277 }
278 
279 static bool isStride64(unsigned Opc) {
280   switch (Opc) {
281   case AMDGPU::DS_READ2ST64_B32:
282   case AMDGPU::DS_READ2ST64_B64:
283   case AMDGPU::DS_WRITE2ST64_B32:
284   case AMDGPU::DS_WRITE2ST64_B64:
285     return true;
286   default:
287     return false;
288   }
289 }
290 
291 bool SIInstrInfo::getMemOperandsWithOffsetWidth(
292     const MachineInstr &LdSt, SmallVectorImpl<const MachineOperand *> &BaseOps,
293     int64_t &Offset, bool &OffsetIsScalable, unsigned &Width,
294     const TargetRegisterInfo *TRI) const {
295   if (!LdSt.mayLoadOrStore())
296     return false;
297 
298   unsigned Opc = LdSt.getOpcode();
299   OffsetIsScalable = false;
300   const MachineOperand *BaseOp, *OffsetOp;
301   int DataOpIdx;
302 
303   if (isDS(LdSt)) {
304     BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr);
305     OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset);
306     if (OffsetOp) {
307       // Normal, single offset LDS instruction.
308       if (!BaseOp) {
309         // DS_CONSUME/DS_APPEND use M0 for the base address.
310         // TODO: find the implicit use operand for M0 and use that as BaseOp?
311         return false;
312       }
313       BaseOps.push_back(BaseOp);
314       Offset = OffsetOp->getImm();
315       // Get appropriate operand, and compute width accordingly.
316       DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
317       if (DataOpIdx == -1)
318         DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
319       Width = getOpSize(LdSt, DataOpIdx);
320     } else {
321       // The 2 offset instructions use offset0 and offset1 instead. We can treat
322       // these as a load with a single offset if the 2 offsets are consecutive.
323       // We will use this for some partially aligned loads.
324       const MachineOperand *Offset0Op =
325           getNamedOperand(LdSt, AMDGPU::OpName::offset0);
326       const MachineOperand *Offset1Op =
327           getNamedOperand(LdSt, AMDGPU::OpName::offset1);
328 
329       unsigned Offset0 = Offset0Op->getImm();
330       unsigned Offset1 = Offset1Op->getImm();
331       if (Offset0 + 1 != Offset1)
332         return false;
333 
334       // Each of these offsets is in element sized units, so we need to convert
335       // to bytes of the individual reads.
336 
337       unsigned EltSize;
338       if (LdSt.mayLoad())
339         EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, 0)) / 16;
340       else {
341         assert(LdSt.mayStore());
342         int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
343         EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, Data0Idx)) / 8;
344       }
345 
346       if (isStride64(Opc))
347         EltSize *= 64;
348 
349       BaseOps.push_back(BaseOp);
350       Offset = EltSize * Offset0;
351       // Get appropriate operand(s), and compute width accordingly.
352       DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
353       if (DataOpIdx == -1) {
354         DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
355         Width = getOpSize(LdSt, DataOpIdx);
356         DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data1);
357         Width += getOpSize(LdSt, DataOpIdx);
358       } else {
359         Width = getOpSize(LdSt, DataOpIdx);
360       }
361     }
362     return true;
363   }
364 
365   if (isMUBUF(LdSt) || isMTBUF(LdSt)) {
366     const MachineOperand *RSrc = getNamedOperand(LdSt, AMDGPU::OpName::srsrc);
367     if (!RSrc) // e.g. BUFFER_WBINVL1_VOL
368       return false;
369     BaseOps.push_back(RSrc);
370     BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr);
371     if (BaseOp && !BaseOp->isFI())
372       BaseOps.push_back(BaseOp);
373     const MachineOperand *OffsetImm =
374         getNamedOperand(LdSt, AMDGPU::OpName::offset);
375     Offset = OffsetImm->getImm();
376     const MachineOperand *SOffset =
377         getNamedOperand(LdSt, AMDGPU::OpName::soffset);
378     if (SOffset) {
379       if (SOffset->isReg())
380         BaseOps.push_back(SOffset);
381       else
382         Offset += SOffset->getImm();
383     }
384     // Get appropriate operand, and compute width accordingly.
385     DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
386     if (DataOpIdx == -1)
387       DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata);
388     Width = getOpSize(LdSt, DataOpIdx);
389     return true;
390   }
391 
392   if (isMIMG(LdSt)) {
393     int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc);
394     BaseOps.push_back(&LdSt.getOperand(SRsrcIdx));
395     int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0);
396     if (VAddr0Idx >= 0) {
397       // GFX10 possible NSA encoding.
398       for (int I = VAddr0Idx; I < SRsrcIdx; ++I)
399         BaseOps.push_back(&LdSt.getOperand(I));
400     } else {
401       BaseOps.push_back(getNamedOperand(LdSt, AMDGPU::OpName::vaddr));
402     }
403     Offset = 0;
404     // Get appropriate operand, and compute width accordingly.
405     DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata);
406     Width = getOpSize(LdSt, DataOpIdx);
407     return true;
408   }
409 
410   if (isSMRD(LdSt)) {
411     BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::sbase);
412     if (!BaseOp) // e.g. S_MEMTIME
413       return false;
414     BaseOps.push_back(BaseOp);
415     OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset);
416     Offset = OffsetOp ? OffsetOp->getImm() : 0;
417     // Get appropriate operand, and compute width accordingly.
418     DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::sdst);
419     Width = getOpSize(LdSt, DataOpIdx);
420     return true;
421   }
422 
423   if (isFLAT(LdSt)) {
424     // Instructions have either vaddr or saddr or both or none.
425     BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr);
426     if (BaseOp)
427       BaseOps.push_back(BaseOp);
428     BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::saddr);
429     if (BaseOp)
430       BaseOps.push_back(BaseOp);
431     Offset = getNamedOperand(LdSt, AMDGPU::OpName::offset)->getImm();
432     // Get appropriate operand, and compute width accordingly.
433     DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
434     if (DataOpIdx == -1)
435       DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata);
436     Width = getOpSize(LdSt, DataOpIdx);
437     return true;
438   }
439 
440   return false;
441 }
442 
443 static bool memOpsHaveSameBasePtr(const MachineInstr &MI1,
444                                   ArrayRef<const MachineOperand *> BaseOps1,
445                                   const MachineInstr &MI2,
446                                   ArrayRef<const MachineOperand *> BaseOps2) {
447   // Only examine the first "base" operand of each instruction, on the
448   // assumption that it represents the real base address of the memory access.
449   // Other operands are typically offsets or indices from this base address.
450   if (BaseOps1.front()->isIdenticalTo(*BaseOps2.front()))
451     return true;
452 
453   if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand())
454     return false;
455 
456   auto MO1 = *MI1.memoperands_begin();
457   auto MO2 = *MI2.memoperands_begin();
458   if (MO1->getAddrSpace() != MO2->getAddrSpace())
459     return false;
460 
461   auto Base1 = MO1->getValue();
462   auto Base2 = MO2->getValue();
463   if (!Base1 || !Base2)
464     return false;
465   Base1 = getUnderlyingObject(Base1);
466   Base2 = getUnderlyingObject(Base2);
467 
468   if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2))
469     return false;
470 
471   return Base1 == Base2;
472 }
473 
474 bool SIInstrInfo::shouldClusterMemOps(ArrayRef<const MachineOperand *> BaseOps1,
475                                       ArrayRef<const MachineOperand *> BaseOps2,
476                                       unsigned NumLoads,
477                                       unsigned NumBytes) const {
478   // If the mem ops (to be clustered) do not have the same base ptr, then they
479   // should not be clustered
480   if (!BaseOps1.empty() && !BaseOps2.empty()) {
481     const MachineInstr &FirstLdSt = *BaseOps1.front()->getParent();
482     const MachineInstr &SecondLdSt = *BaseOps2.front()->getParent();
483     if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOps1, SecondLdSt, BaseOps2))
484       return false;
485   } else if (!BaseOps1.empty() || !BaseOps2.empty()) {
486     // If only one base op is empty, they do not have the same base ptr
487     return false;
488   }
489 
490   // In order to avoid register pressure, on an average, the number of DWORDS
491   // loaded together by all clustered mem ops should not exceed 8. This is an
492   // empirical value based on certain observations and performance related
493   // experiments.
494   // The good thing about this heuristic is - it avoids clustering of too many
495   // sub-word loads, and also avoids clustering of wide loads. Below is the
496   // brief summary of how the heuristic behaves for various `LoadSize`.
497   // (1) 1 <= LoadSize <= 4: cluster at max 8 mem ops
498   // (2) 5 <= LoadSize <= 8: cluster at max 4 mem ops
499   // (3) 9 <= LoadSize <= 12: cluster at max 2 mem ops
500   // (4) 13 <= LoadSize <= 16: cluster at max 2 mem ops
501   // (5) LoadSize >= 17: do not cluster
502   const unsigned LoadSize = NumBytes / NumLoads;
503   const unsigned NumDWORDs = ((LoadSize + 3) / 4) * NumLoads;
504   return NumDWORDs <= 8;
505 }
506 
507 // FIXME: This behaves strangely. If, for example, you have 32 load + stores,
508 // the first 16 loads will be interleaved with the stores, and the next 16 will
509 // be clustered as expected. It should really split into 2 16 store batches.
510 //
511 // Loads are clustered until this returns false, rather than trying to schedule
512 // groups of stores. This also means we have to deal with saying different
513 // address space loads should be clustered, and ones which might cause bank
514 // conflicts.
515 //
516 // This might be deprecated so it might not be worth that much effort to fix.
517 bool SIInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1,
518                                           int64_t Offset0, int64_t Offset1,
519                                           unsigned NumLoads) const {
520   assert(Offset1 > Offset0 &&
521          "Second offset should be larger than first offset!");
522   // If we have less than 16 loads in a row, and the offsets are within 64
523   // bytes, then schedule together.
524 
525   // A cacheline is 64 bytes (for global memory).
526   return (NumLoads <= 16 && (Offset1 - Offset0) < 64);
527 }
528 
529 static void reportIllegalCopy(const SIInstrInfo *TII, MachineBasicBlock &MBB,
530                               MachineBasicBlock::iterator MI,
531                               const DebugLoc &DL, MCRegister DestReg,
532                               MCRegister SrcReg, bool KillSrc,
533                               const char *Msg = "illegal SGPR to VGPR copy") {
534   MachineFunction *MF = MBB.getParent();
535   DiagnosticInfoUnsupported IllegalCopy(MF->getFunction(), Msg, DL, DS_Error);
536   LLVMContext &C = MF->getFunction().getContext();
537   C.diagnose(IllegalCopy);
538 
539   BuildMI(MBB, MI, DL, TII->get(AMDGPU::SI_ILLEGAL_COPY), DestReg)
540     .addReg(SrcReg, getKillRegState(KillSrc));
541 }
542 
543 /// Handle copying from SGPR to AGPR, or from AGPR to AGPR on GFX908. It is not
544 /// possible to have a direct copy in these cases on GFX908, so an intermediate
545 /// VGPR copy is required.
546 static void indirectCopyToAGPR(const SIInstrInfo &TII,
547                                MachineBasicBlock &MBB,
548                                MachineBasicBlock::iterator MI,
549                                const DebugLoc &DL, MCRegister DestReg,
550                                MCRegister SrcReg, bool KillSrc,
551                                RegScavenger &RS,
552                                Register ImpDefSuperReg = Register(),
553                                Register ImpUseSuperReg = Register()) {
554   assert((TII.getSubtarget().hasMAIInsts() &&
555           !TII.getSubtarget().hasGFX90AInsts()) &&
556          "Expected GFX908 subtarget.");
557 
558   assert((AMDGPU::SReg_32RegClass.contains(SrcReg) ||
559           AMDGPU::AGPR_32RegClass.contains(SrcReg)) &&
560          "Source register of the copy should be either an SGPR or an AGPR.");
561 
562   assert(AMDGPU::AGPR_32RegClass.contains(DestReg) &&
563          "Destination register of the copy should be an AGPR.");
564 
565   const SIRegisterInfo &RI = TII.getRegisterInfo();
566 
567   // First try to find defining accvgpr_write to avoid temporary registers.
568   for (auto Def = MI, E = MBB.begin(); Def != E; ) {
569     --Def;
570     if (!Def->definesRegister(SrcReg, &RI))
571       continue;
572     if (Def->getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64)
573       break;
574 
575     MachineOperand &DefOp = Def->getOperand(1);
576     assert(DefOp.isReg() || DefOp.isImm());
577 
578     if (DefOp.isReg()) {
579       // Check that register source operand if not clobbered before MI.
580       // Immediate operands are always safe to propagate.
581       bool SafeToPropagate = true;
582       for (auto I = Def; I != MI && SafeToPropagate; ++I)
583         if (I->modifiesRegister(DefOp.getReg(), &RI))
584           SafeToPropagate = false;
585 
586       if (!SafeToPropagate)
587         break;
588 
589       DefOp.setIsKill(false);
590     }
591 
592     MachineInstrBuilder Builder =
593       BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg)
594       .add(DefOp);
595     if (ImpDefSuperReg)
596       Builder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit);
597 
598     if (ImpUseSuperReg) {
599       Builder.addReg(ImpUseSuperReg,
600                      getKillRegState(KillSrc) | RegState::Implicit);
601     }
602 
603     return;
604   }
605 
606   RS.enterBasicBlock(MBB);
607   RS.forward(MI);
608 
609   // Ideally we want to have three registers for a long reg_sequence copy
610   // to hide 2 waitstates between v_mov_b32 and accvgpr_write.
611   unsigned MaxVGPRs = RI.getRegPressureLimit(&AMDGPU::VGPR_32RegClass,
612                                              *MBB.getParent());
613 
614   // Registers in the sequence are allocated contiguously so we can just
615   // use register number to pick one of three round-robin temps.
616   unsigned RegNo = DestReg % 3;
617   Register Tmp = AMDGPU::VGPR32;
618   assert(MBB.getParent()->getRegInfo().isReserved(Tmp) &&
619          "VGPR used for an intermediate copy should have been reserved.");
620 
621   // Only loop through if there are any free registers left, otherwise
622   // scavenger may report a fatal error without emergency spill slot
623   // or spill with the slot.
624   while (RegNo-- && RS.FindUnusedReg(&AMDGPU::VGPR_32RegClass)) {
625     Register Tmp2 = RS.scavengeRegister(&AMDGPU::VGPR_32RegClass, 0);
626     if (!Tmp2 || RI.getHWRegIndex(Tmp2) >= MaxVGPRs)
627       break;
628     Tmp = Tmp2;
629     RS.setRegUsed(Tmp);
630   }
631 
632   // Insert copy to temporary VGPR.
633   unsigned TmpCopyOp = AMDGPU::V_MOV_B32_e32;
634   if (AMDGPU::AGPR_32RegClass.contains(SrcReg)) {
635     TmpCopyOp = AMDGPU::V_ACCVGPR_READ_B32_e64;
636   } else {
637     assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
638   }
639 
640   MachineInstrBuilder UseBuilder = BuildMI(MBB, MI, DL, TII.get(TmpCopyOp), Tmp)
641     .addReg(SrcReg, getKillRegState(KillSrc));
642   if (ImpUseSuperReg) {
643     UseBuilder.addReg(ImpUseSuperReg,
644                       getKillRegState(KillSrc) | RegState::Implicit);
645   }
646 
647   MachineInstrBuilder DefBuilder
648     = BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg)
649     .addReg(Tmp, RegState::Kill);
650 
651   if (ImpDefSuperReg)
652     DefBuilder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit);
653 }
654 
655 static void expandSGPRCopy(const SIInstrInfo &TII, MachineBasicBlock &MBB,
656                            MachineBasicBlock::iterator MI, const DebugLoc &DL,
657                            MCRegister DestReg, MCRegister SrcReg, bool KillSrc,
658                            const TargetRegisterClass *RC, bool Forward) {
659   const SIRegisterInfo &RI = TII.getRegisterInfo();
660   ArrayRef<int16_t> BaseIndices = RI.getRegSplitParts(RC, 4);
661   MachineBasicBlock::iterator I = MI;
662   MachineInstr *FirstMI = nullptr, *LastMI = nullptr;
663 
664   for (unsigned Idx = 0; Idx < BaseIndices.size(); ++Idx) {
665     int16_t SubIdx = BaseIndices[Idx];
666     Register Reg = RI.getSubReg(DestReg, SubIdx);
667     unsigned Opcode = AMDGPU::S_MOV_B32;
668 
669     // Is SGPR aligned? If so try to combine with next.
670     Register Src = RI.getSubReg(SrcReg, SubIdx);
671     bool AlignedDest = ((Reg - AMDGPU::SGPR0) % 2) == 0;
672     bool AlignedSrc = ((Src - AMDGPU::SGPR0) % 2) == 0;
673     if (AlignedDest && AlignedSrc && (Idx + 1 < BaseIndices.size())) {
674       // Can use SGPR64 copy
675       unsigned Channel = RI.getChannelFromSubReg(SubIdx);
676       SubIdx = RI.getSubRegFromChannel(Channel, 2);
677       Opcode = AMDGPU::S_MOV_B64;
678       Idx++;
679     }
680 
681     LastMI = BuildMI(MBB, I, DL, TII.get(Opcode), RI.getSubReg(DestReg, SubIdx))
682                  .addReg(RI.getSubReg(SrcReg, SubIdx))
683                  .addReg(SrcReg, RegState::Implicit);
684 
685     if (!FirstMI)
686       FirstMI = LastMI;
687 
688     if (!Forward)
689       I--;
690   }
691 
692   assert(FirstMI && LastMI);
693   if (!Forward)
694     std::swap(FirstMI, LastMI);
695 
696   FirstMI->addOperand(
697       MachineOperand::CreateReg(DestReg, true /*IsDef*/, true /*IsImp*/));
698 
699   if (KillSrc)
700     LastMI->addRegisterKilled(SrcReg, &RI);
701 }
702 
703 void SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
704                               MachineBasicBlock::iterator MI,
705                               const DebugLoc &DL, MCRegister DestReg,
706                               MCRegister SrcReg, bool KillSrc) const {
707   const TargetRegisterClass *RC = RI.getPhysRegClass(DestReg);
708 
709   // FIXME: This is hack to resolve copies between 16 bit and 32 bit
710   // registers until all patterns are fixed.
711   if (Fix16BitCopies &&
712       ((RI.getRegSizeInBits(*RC) == 16) ^
713        (RI.getRegSizeInBits(*RI.getPhysRegClass(SrcReg)) == 16))) {
714     MCRegister &RegToFix = (RI.getRegSizeInBits(*RC) == 16) ? DestReg : SrcReg;
715     MCRegister Super = RI.get32BitRegister(RegToFix);
716     assert(RI.getSubReg(Super, AMDGPU::lo16) == RegToFix);
717     RegToFix = Super;
718 
719     if (DestReg == SrcReg) {
720       // Insert empty bundle since ExpandPostRA expects an instruction here.
721       BuildMI(MBB, MI, DL, get(AMDGPU::BUNDLE));
722       return;
723     }
724 
725     RC = RI.getPhysRegClass(DestReg);
726   }
727 
728   if (RC == &AMDGPU::VGPR_32RegClass) {
729     assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
730            AMDGPU::SReg_32RegClass.contains(SrcReg) ||
731            AMDGPU::AGPR_32RegClass.contains(SrcReg));
732     unsigned Opc = AMDGPU::AGPR_32RegClass.contains(SrcReg) ?
733                      AMDGPU::V_ACCVGPR_READ_B32_e64 : AMDGPU::V_MOV_B32_e32;
734     BuildMI(MBB, MI, DL, get(Opc), DestReg)
735       .addReg(SrcReg, getKillRegState(KillSrc));
736     return;
737   }
738 
739   if (RC == &AMDGPU::SReg_32_XM0RegClass ||
740       RC == &AMDGPU::SReg_32RegClass) {
741     if (SrcReg == AMDGPU::SCC) {
742       BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B32), DestReg)
743           .addImm(1)
744           .addImm(0);
745       return;
746     }
747 
748     if (DestReg == AMDGPU::VCC_LO) {
749       if (AMDGPU::SReg_32RegClass.contains(SrcReg)) {
750         BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), AMDGPU::VCC_LO)
751           .addReg(SrcReg, getKillRegState(KillSrc));
752       } else {
753         // FIXME: Hack until VReg_1 removed.
754         assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
755         BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32))
756           .addImm(0)
757           .addReg(SrcReg, getKillRegState(KillSrc));
758       }
759 
760       return;
761     }
762 
763     if (!AMDGPU::SReg_32RegClass.contains(SrcReg)) {
764       reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
765       return;
766     }
767 
768     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
769             .addReg(SrcReg, getKillRegState(KillSrc));
770     return;
771   }
772 
773   if (RC == &AMDGPU::SReg_64RegClass) {
774     if (SrcReg == AMDGPU::SCC) {
775       BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B64), DestReg)
776           .addImm(1)
777           .addImm(0);
778       return;
779     }
780 
781     if (DestReg == AMDGPU::VCC) {
782       if (AMDGPU::SReg_64RegClass.contains(SrcReg)) {
783         BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC)
784           .addReg(SrcReg, getKillRegState(KillSrc));
785       } else {
786         // FIXME: Hack until VReg_1 removed.
787         assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
788         BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32))
789           .addImm(0)
790           .addReg(SrcReg, getKillRegState(KillSrc));
791       }
792 
793       return;
794     }
795 
796     if (!AMDGPU::SReg_64RegClass.contains(SrcReg)) {
797       reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
798       return;
799     }
800 
801     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
802             .addReg(SrcReg, getKillRegState(KillSrc));
803     return;
804   }
805 
806   if (DestReg == AMDGPU::SCC) {
807     // Copying 64-bit or 32-bit sources to SCC barely makes sense,
808     // but SelectionDAG emits such copies for i1 sources.
809     if (AMDGPU::SReg_64RegClass.contains(SrcReg)) {
810       // This copy can only be produced by patterns
811       // with explicit SCC, which are known to be enabled
812       // only for subtargets with S_CMP_LG_U64 present.
813       assert(ST.hasScalarCompareEq64());
814       BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U64))
815           .addReg(SrcReg, getKillRegState(KillSrc))
816           .addImm(0);
817     } else {
818       assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
819       BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U32))
820           .addReg(SrcReg, getKillRegState(KillSrc))
821           .addImm(0);
822     }
823 
824     return;
825   }
826 
827   if (RC == &AMDGPU::AGPR_32RegClass) {
828     if (AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
829         (ST.hasGFX90AInsts() && AMDGPU::SReg_32RegClass.contains(SrcReg))) {
830       BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg)
831         .addReg(SrcReg, getKillRegState(KillSrc));
832       return;
833     }
834 
835     if (AMDGPU::AGPR_32RegClass.contains(SrcReg) && ST.hasGFX90AInsts()) {
836       BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_MOV_B32), DestReg)
837         .addReg(SrcReg, getKillRegState(KillSrc));
838       return;
839     }
840 
841     // FIXME: Pass should maintain scavenger to avoid scan through the block on
842     // every AGPR spill.
843     RegScavenger RS;
844     indirectCopyToAGPR(*this, MBB, MI, DL, DestReg, SrcReg, KillSrc, RS);
845     return;
846   }
847 
848   const unsigned Size = RI.getRegSizeInBits(*RC);
849   if (Size == 16) {
850     assert(AMDGPU::VGPR_LO16RegClass.contains(SrcReg) ||
851            AMDGPU::VGPR_HI16RegClass.contains(SrcReg) ||
852            AMDGPU::SReg_LO16RegClass.contains(SrcReg) ||
853            AMDGPU::AGPR_LO16RegClass.contains(SrcReg));
854 
855     bool IsSGPRDst = AMDGPU::SReg_LO16RegClass.contains(DestReg);
856     bool IsSGPRSrc = AMDGPU::SReg_LO16RegClass.contains(SrcReg);
857     bool IsAGPRDst = AMDGPU::AGPR_LO16RegClass.contains(DestReg);
858     bool IsAGPRSrc = AMDGPU::AGPR_LO16RegClass.contains(SrcReg);
859     bool DstLow = AMDGPU::VGPR_LO16RegClass.contains(DestReg) ||
860                   AMDGPU::SReg_LO16RegClass.contains(DestReg) ||
861                   AMDGPU::AGPR_LO16RegClass.contains(DestReg);
862     bool SrcLow = AMDGPU::VGPR_LO16RegClass.contains(SrcReg) ||
863                   AMDGPU::SReg_LO16RegClass.contains(SrcReg) ||
864                   AMDGPU::AGPR_LO16RegClass.contains(SrcReg);
865     MCRegister NewDestReg = RI.get32BitRegister(DestReg);
866     MCRegister NewSrcReg = RI.get32BitRegister(SrcReg);
867 
868     if (IsSGPRDst) {
869       if (!IsSGPRSrc) {
870         reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
871         return;
872       }
873 
874       BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), NewDestReg)
875         .addReg(NewSrcReg, getKillRegState(KillSrc));
876       return;
877     }
878 
879     if (IsAGPRDst || IsAGPRSrc) {
880       if (!DstLow || !SrcLow) {
881         reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc,
882                           "Cannot use hi16 subreg with an AGPR!");
883       }
884 
885       copyPhysReg(MBB, MI, DL, NewDestReg, NewSrcReg, KillSrc);
886       return;
887     }
888 
889     if (IsSGPRSrc && !ST.hasSDWAScalar()) {
890       if (!DstLow || !SrcLow) {
891         reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc,
892                           "Cannot use hi16 subreg on VI!");
893       }
894 
895       BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), NewDestReg)
896         .addReg(NewSrcReg, getKillRegState(KillSrc));
897       return;
898     }
899 
900     auto MIB = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_sdwa), NewDestReg)
901       .addImm(0) // src0_modifiers
902       .addReg(NewSrcReg)
903       .addImm(0) // clamp
904       .addImm(DstLow ? AMDGPU::SDWA::SdwaSel::WORD_0
905                      : AMDGPU::SDWA::SdwaSel::WORD_1)
906       .addImm(AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE)
907       .addImm(SrcLow ? AMDGPU::SDWA::SdwaSel::WORD_0
908                      : AMDGPU::SDWA::SdwaSel::WORD_1)
909       .addReg(NewDestReg, RegState::Implicit | RegState::Undef);
910     // First implicit operand is $exec.
911     MIB->tieOperands(0, MIB->getNumOperands() - 1);
912     return;
913   }
914 
915   const TargetRegisterClass *SrcRC = RI.getPhysRegClass(SrcReg);
916   if (RC == RI.getVGPR64Class() && (SrcRC == RC || RI.isSGPRClass(SrcRC))) {
917     if (ST.hasMovB64()) {
918       BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_e32), DestReg)
919         .addReg(SrcReg, getKillRegState(KillSrc));
920       return;
921     }
922     if (ST.hasPackedFP32Ops()) {
923       BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DestReg)
924         .addImm(SISrcMods::OP_SEL_1)
925         .addReg(SrcReg)
926         .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1)
927         .addReg(SrcReg)
928         .addImm(0) // op_sel_lo
929         .addImm(0) // op_sel_hi
930         .addImm(0) // neg_lo
931         .addImm(0) // neg_hi
932         .addImm(0) // clamp
933         .addReg(SrcReg, getKillRegState(KillSrc) | RegState::Implicit);
934       return;
935     }
936   }
937 
938   const bool Forward = RI.getHWRegIndex(DestReg) <= RI.getHWRegIndex(SrcReg);
939   if (RI.isSGPRClass(RC)) {
940     if (!RI.isSGPRClass(SrcRC)) {
941       reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc);
942       return;
943     }
944     const bool CanKillSuperReg = KillSrc && !RI.regsOverlap(SrcReg, DestReg);
945     expandSGPRCopy(*this, MBB, MI, DL, DestReg, SrcReg, CanKillSuperReg, RC,
946                    Forward);
947     return;
948   }
949 
950   unsigned EltSize = 4;
951   unsigned Opcode = AMDGPU::V_MOV_B32_e32;
952   if (RI.isAGPRClass(RC)) {
953     if (ST.hasGFX90AInsts() && RI.isAGPRClass(SrcRC))
954       Opcode = AMDGPU::V_ACCVGPR_MOV_B32;
955     else if (RI.hasVGPRs(SrcRC) ||
956              (ST.hasGFX90AInsts() && RI.isSGPRClass(SrcRC)))
957       Opcode = AMDGPU::V_ACCVGPR_WRITE_B32_e64;
958     else
959       Opcode = AMDGPU::INSTRUCTION_LIST_END;
960   } else if (RI.hasVGPRs(RC) && RI.isAGPRClass(SrcRC)) {
961     Opcode = AMDGPU::V_ACCVGPR_READ_B32_e64;
962   } else if ((Size % 64 == 0) && RI.hasVGPRs(RC) &&
963              (RI.isProperlyAlignedRC(*RC) &&
964               (SrcRC == RC || RI.isSGPRClass(SrcRC)))) {
965     // TODO: In 96-bit case, could do a 64-bit mov and then a 32-bit mov.
966     if (ST.hasMovB64()) {
967       Opcode = AMDGPU::V_MOV_B64_e32;
968       EltSize = 8;
969     } else if (ST.hasPackedFP32Ops()) {
970       Opcode = AMDGPU::V_PK_MOV_B32;
971       EltSize = 8;
972     }
973   }
974 
975   // For the cases where we need an intermediate instruction/temporary register
976   // (destination is an AGPR), we need a scavenger.
977   //
978   // FIXME: The pass should maintain this for us so we don't have to re-scan the
979   // whole block for every handled copy.
980   std::unique_ptr<RegScavenger> RS;
981   if (Opcode == AMDGPU::INSTRUCTION_LIST_END)
982     RS.reset(new RegScavenger());
983 
984   ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RC, EltSize);
985 
986   // If there is an overlap, we can't kill the super-register on the last
987   // instruction, since it will also kill the components made live by this def.
988   const bool CanKillSuperReg = KillSrc && !RI.regsOverlap(SrcReg, DestReg);
989 
990   for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) {
991     unsigned SubIdx;
992     if (Forward)
993       SubIdx = SubIndices[Idx];
994     else
995       SubIdx = SubIndices[SubIndices.size() - Idx - 1];
996 
997     bool UseKill = CanKillSuperReg && Idx == SubIndices.size() - 1;
998 
999     if (Opcode == AMDGPU::INSTRUCTION_LIST_END) {
1000       Register ImpDefSuper = Idx == 0 ? Register(DestReg) : Register();
1001       Register ImpUseSuper = SrcReg;
1002       indirectCopyToAGPR(*this, MBB, MI, DL, RI.getSubReg(DestReg, SubIdx),
1003                          RI.getSubReg(SrcReg, SubIdx), UseKill, *RS,
1004                          ImpDefSuper, ImpUseSuper);
1005     } else if (Opcode == AMDGPU::V_PK_MOV_B32) {
1006       Register DstSubReg = RI.getSubReg(DestReg, SubIdx);
1007       Register SrcSubReg = RI.getSubReg(SrcReg, SubIdx);
1008       MachineInstrBuilder MIB =
1009         BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DstSubReg)
1010         .addImm(SISrcMods::OP_SEL_1)
1011         .addReg(SrcSubReg)
1012         .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1)
1013         .addReg(SrcSubReg)
1014         .addImm(0) // op_sel_lo
1015         .addImm(0) // op_sel_hi
1016         .addImm(0) // neg_lo
1017         .addImm(0) // neg_hi
1018         .addImm(0) // clamp
1019         .addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit);
1020       if (Idx == 0)
1021         MIB.addReg(DestReg, RegState::Define | RegState::Implicit);
1022     } else {
1023       MachineInstrBuilder Builder =
1024         BuildMI(MBB, MI, DL, get(Opcode), RI.getSubReg(DestReg, SubIdx))
1025         .addReg(RI.getSubReg(SrcReg, SubIdx));
1026       if (Idx == 0)
1027         Builder.addReg(DestReg, RegState::Define | RegState::Implicit);
1028 
1029       Builder.addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit);
1030     }
1031   }
1032 }
1033 
1034 int SIInstrInfo::commuteOpcode(unsigned Opcode) const {
1035   int NewOpc;
1036 
1037   // Try to map original to commuted opcode
1038   NewOpc = AMDGPU::getCommuteRev(Opcode);
1039   if (NewOpc != -1)
1040     // Check if the commuted (REV) opcode exists on the target.
1041     return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
1042 
1043   // Try to map commuted to original opcode
1044   NewOpc = AMDGPU::getCommuteOrig(Opcode);
1045   if (NewOpc != -1)
1046     // Check if the original (non-REV) opcode exists on the target.
1047     return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
1048 
1049   return Opcode;
1050 }
1051 
1052 void SIInstrInfo::materializeImmediate(MachineBasicBlock &MBB,
1053                                        MachineBasicBlock::iterator MI,
1054                                        const DebugLoc &DL, unsigned DestReg,
1055                                        int64_t Value) const {
1056   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1057   const TargetRegisterClass *RegClass = MRI.getRegClass(DestReg);
1058   if (RegClass == &AMDGPU::SReg_32RegClass ||
1059       RegClass == &AMDGPU::SGPR_32RegClass ||
1060       RegClass == &AMDGPU::SReg_32_XM0RegClass ||
1061       RegClass == &AMDGPU::SReg_32_XM0_XEXECRegClass) {
1062     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
1063       .addImm(Value);
1064     return;
1065   }
1066 
1067   if (RegClass == &AMDGPU::SReg_64RegClass ||
1068       RegClass == &AMDGPU::SGPR_64RegClass ||
1069       RegClass == &AMDGPU::SReg_64_XEXECRegClass) {
1070     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
1071       .addImm(Value);
1072     return;
1073   }
1074 
1075   if (RegClass == &AMDGPU::VGPR_32RegClass) {
1076     BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg)
1077       .addImm(Value);
1078     return;
1079   }
1080   if (RegClass->hasSuperClassEq(&AMDGPU::VReg_64RegClass)) {
1081     BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), DestReg)
1082       .addImm(Value);
1083     return;
1084   }
1085 
1086   unsigned EltSize = 4;
1087   unsigned Opcode = AMDGPU::V_MOV_B32_e32;
1088   if (RI.isSGPRClass(RegClass)) {
1089     if (RI.getRegSizeInBits(*RegClass) > 32) {
1090       Opcode =  AMDGPU::S_MOV_B64;
1091       EltSize = 8;
1092     } else {
1093       Opcode = AMDGPU::S_MOV_B32;
1094       EltSize = 4;
1095     }
1096   }
1097 
1098   ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RegClass, EltSize);
1099   for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) {
1100     int64_t IdxValue = Idx == 0 ? Value : 0;
1101 
1102     MachineInstrBuilder Builder = BuildMI(MBB, MI, DL,
1103       get(Opcode), RI.getSubReg(DestReg, SubIndices[Idx]));
1104     Builder.addImm(IdxValue);
1105   }
1106 }
1107 
1108 const TargetRegisterClass *
1109 SIInstrInfo::getPreferredSelectRegClass(unsigned Size) const {
1110   return &AMDGPU::VGPR_32RegClass;
1111 }
1112 
1113 void SIInstrInfo::insertVectorSelect(MachineBasicBlock &MBB,
1114                                      MachineBasicBlock::iterator I,
1115                                      const DebugLoc &DL, Register DstReg,
1116                                      ArrayRef<MachineOperand> Cond,
1117                                      Register TrueReg,
1118                                      Register FalseReg) const {
1119   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1120   const TargetRegisterClass *BoolXExecRC =
1121     RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
1122   assert(MRI.getRegClass(DstReg) == &AMDGPU::VGPR_32RegClass &&
1123          "Not a VGPR32 reg");
1124 
1125   if (Cond.size() == 1) {
1126     Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1127     BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
1128       .add(Cond[0]);
1129     BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1130       .addImm(0)
1131       .addReg(FalseReg)
1132       .addImm(0)
1133       .addReg(TrueReg)
1134       .addReg(SReg);
1135   } else if (Cond.size() == 2) {
1136     assert(Cond[0].isImm() && "Cond[0] is not an immediate");
1137     switch (Cond[0].getImm()) {
1138     case SIInstrInfo::SCC_TRUE: {
1139       Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1140       BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
1141                                             : AMDGPU::S_CSELECT_B64), SReg)
1142         .addImm(1)
1143         .addImm(0);
1144       BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1145         .addImm(0)
1146         .addReg(FalseReg)
1147         .addImm(0)
1148         .addReg(TrueReg)
1149         .addReg(SReg);
1150       break;
1151     }
1152     case SIInstrInfo::SCC_FALSE: {
1153       Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1154       BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
1155                                             : AMDGPU::S_CSELECT_B64), SReg)
1156         .addImm(0)
1157         .addImm(1);
1158       BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1159         .addImm(0)
1160         .addReg(FalseReg)
1161         .addImm(0)
1162         .addReg(TrueReg)
1163         .addReg(SReg);
1164       break;
1165     }
1166     case SIInstrInfo::VCCNZ: {
1167       MachineOperand RegOp = Cond[1];
1168       RegOp.setImplicit(false);
1169       Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1170       BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
1171         .add(RegOp);
1172       BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1173           .addImm(0)
1174           .addReg(FalseReg)
1175           .addImm(0)
1176           .addReg(TrueReg)
1177           .addReg(SReg);
1178       break;
1179     }
1180     case SIInstrInfo::VCCZ: {
1181       MachineOperand RegOp = Cond[1];
1182       RegOp.setImplicit(false);
1183       Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1184       BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg)
1185         .add(RegOp);
1186       BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1187           .addImm(0)
1188           .addReg(TrueReg)
1189           .addImm(0)
1190           .addReg(FalseReg)
1191           .addReg(SReg);
1192       break;
1193     }
1194     case SIInstrInfo::EXECNZ: {
1195       Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1196       Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC());
1197       BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32
1198                                             : AMDGPU::S_OR_SAVEEXEC_B64), SReg2)
1199         .addImm(0);
1200       BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
1201                                             : AMDGPU::S_CSELECT_B64), SReg)
1202         .addImm(1)
1203         .addImm(0);
1204       BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1205         .addImm(0)
1206         .addReg(FalseReg)
1207         .addImm(0)
1208         .addReg(TrueReg)
1209         .addReg(SReg);
1210       break;
1211     }
1212     case SIInstrInfo::EXECZ: {
1213       Register SReg = MRI.createVirtualRegister(BoolXExecRC);
1214       Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC());
1215       BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32
1216                                             : AMDGPU::S_OR_SAVEEXEC_B64), SReg2)
1217         .addImm(0);
1218       BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32
1219                                             : AMDGPU::S_CSELECT_B64), SReg)
1220         .addImm(0)
1221         .addImm(1);
1222       BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg)
1223         .addImm(0)
1224         .addReg(FalseReg)
1225         .addImm(0)
1226         .addReg(TrueReg)
1227         .addReg(SReg);
1228       llvm_unreachable("Unhandled branch predicate EXECZ");
1229       break;
1230     }
1231     default:
1232       llvm_unreachable("invalid branch predicate");
1233     }
1234   } else {
1235     llvm_unreachable("Can only handle Cond size 1 or 2");
1236   }
1237 }
1238 
1239 Register SIInstrInfo::insertEQ(MachineBasicBlock *MBB,
1240                                MachineBasicBlock::iterator I,
1241                                const DebugLoc &DL,
1242                                Register SrcReg, int Value) const {
1243   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1244   Register Reg = MRI.createVirtualRegister(RI.getBoolRC());
1245   BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_EQ_I32_e64), Reg)
1246     .addImm(Value)
1247     .addReg(SrcReg);
1248 
1249   return Reg;
1250 }
1251 
1252 Register SIInstrInfo::insertNE(MachineBasicBlock *MBB,
1253                                MachineBasicBlock::iterator I,
1254                                const DebugLoc &DL,
1255                                Register SrcReg, int Value) const {
1256   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1257   Register Reg = MRI.createVirtualRegister(RI.getBoolRC());
1258   BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_NE_I32_e64), Reg)
1259     .addImm(Value)
1260     .addReg(SrcReg);
1261 
1262   return Reg;
1263 }
1264 
1265 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const {
1266 
1267   if (RI.isAGPRClass(DstRC))
1268     return AMDGPU::COPY;
1269   if (RI.getRegSizeInBits(*DstRC) == 32) {
1270     return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
1271   } else if (RI.getRegSizeInBits(*DstRC) == 64 && RI.isSGPRClass(DstRC)) {
1272     return AMDGPU::S_MOV_B64;
1273   } else if (RI.getRegSizeInBits(*DstRC) == 64 && !RI.isSGPRClass(DstRC)) {
1274     return  AMDGPU::V_MOV_B64_PSEUDO;
1275   }
1276   return AMDGPU::COPY;
1277 }
1278 
1279 const MCInstrDesc &
1280 SIInstrInfo::getIndirectGPRIDXPseudo(unsigned VecSize,
1281                                      bool IsIndirectSrc) const {
1282   if (IsIndirectSrc) {
1283     if (VecSize <= 32) // 4 bytes
1284       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1);
1285     if (VecSize <= 64) // 8 bytes
1286       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2);
1287     if (VecSize <= 96) // 12 bytes
1288       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3);
1289     if (VecSize <= 128) // 16 bytes
1290       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4);
1291     if (VecSize <= 160) // 20 bytes
1292       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5);
1293     if (VecSize <= 256) // 32 bytes
1294       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8);
1295     if (VecSize <= 512) // 64 bytes
1296       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16);
1297     if (VecSize <= 1024) // 128 bytes
1298       return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32);
1299 
1300     llvm_unreachable("unsupported size for IndirectRegReadGPRIDX pseudos");
1301   }
1302 
1303   if (VecSize <= 32) // 4 bytes
1304     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1);
1305   if (VecSize <= 64) // 8 bytes
1306     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2);
1307   if (VecSize <= 96) // 12 bytes
1308     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3);
1309   if (VecSize <= 128) // 16 bytes
1310     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4);
1311   if (VecSize <= 160) // 20 bytes
1312     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5);
1313   if (VecSize <= 256) // 32 bytes
1314     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8);
1315   if (VecSize <= 512) // 64 bytes
1316     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16);
1317   if (VecSize <= 1024) // 128 bytes
1318     return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32);
1319 
1320   llvm_unreachable("unsupported size for IndirectRegWriteGPRIDX pseudos");
1321 }
1322 
1323 static unsigned getIndirectVGPRWriteMovRelPseudoOpc(unsigned VecSize) {
1324   if (VecSize <= 32) // 4 bytes
1325     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1;
1326   if (VecSize <= 64) // 8 bytes
1327     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2;
1328   if (VecSize <= 96) // 12 bytes
1329     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3;
1330   if (VecSize <= 128) // 16 bytes
1331     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4;
1332   if (VecSize <= 160) // 20 bytes
1333     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5;
1334   if (VecSize <= 256) // 32 bytes
1335     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8;
1336   if (VecSize <= 512) // 64 bytes
1337     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16;
1338   if (VecSize <= 1024) // 128 bytes
1339     return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32;
1340 
1341   llvm_unreachable("unsupported size for IndirectRegWrite pseudos");
1342 }
1343 
1344 static unsigned getIndirectSGPRWriteMovRelPseudo32(unsigned VecSize) {
1345   if (VecSize <= 32) // 4 bytes
1346     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1;
1347   if (VecSize <= 64) // 8 bytes
1348     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2;
1349   if (VecSize <= 96) // 12 bytes
1350     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3;
1351   if (VecSize <= 128) // 16 bytes
1352     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4;
1353   if (VecSize <= 160) // 20 bytes
1354     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5;
1355   if (VecSize <= 256) // 32 bytes
1356     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8;
1357   if (VecSize <= 512) // 64 bytes
1358     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16;
1359   if (VecSize <= 1024) // 128 bytes
1360     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32;
1361 
1362   llvm_unreachable("unsupported size for IndirectRegWrite pseudos");
1363 }
1364 
1365 static unsigned getIndirectSGPRWriteMovRelPseudo64(unsigned VecSize) {
1366   if (VecSize <= 64) // 8 bytes
1367     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1;
1368   if (VecSize <= 128) // 16 bytes
1369     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2;
1370   if (VecSize <= 256) // 32 bytes
1371     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4;
1372   if (VecSize <= 512) // 64 bytes
1373     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8;
1374   if (VecSize <= 1024) // 128 bytes
1375     return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16;
1376 
1377   llvm_unreachable("unsupported size for IndirectRegWrite pseudos");
1378 }
1379 
1380 const MCInstrDesc &
1381 SIInstrInfo::getIndirectRegWriteMovRelPseudo(unsigned VecSize, unsigned EltSize,
1382                                              bool IsSGPR) const {
1383   if (IsSGPR) {
1384     switch (EltSize) {
1385     case 32:
1386       return get(getIndirectSGPRWriteMovRelPseudo32(VecSize));
1387     case 64:
1388       return get(getIndirectSGPRWriteMovRelPseudo64(VecSize));
1389     default:
1390       llvm_unreachable("invalid reg indexing elt size");
1391     }
1392   }
1393 
1394   assert(EltSize == 32 && "invalid reg indexing elt size");
1395   return get(getIndirectVGPRWriteMovRelPseudoOpc(VecSize));
1396 }
1397 
1398 static unsigned getSGPRSpillSaveOpcode(unsigned Size) {
1399   switch (Size) {
1400   case 4:
1401     return AMDGPU::SI_SPILL_S32_SAVE;
1402   case 8:
1403     return AMDGPU::SI_SPILL_S64_SAVE;
1404   case 12:
1405     return AMDGPU::SI_SPILL_S96_SAVE;
1406   case 16:
1407     return AMDGPU::SI_SPILL_S128_SAVE;
1408   case 20:
1409     return AMDGPU::SI_SPILL_S160_SAVE;
1410   case 24:
1411     return AMDGPU::SI_SPILL_S192_SAVE;
1412   case 28:
1413     return AMDGPU::SI_SPILL_S224_SAVE;
1414   case 32:
1415     return AMDGPU::SI_SPILL_S256_SAVE;
1416   case 64:
1417     return AMDGPU::SI_SPILL_S512_SAVE;
1418   case 128:
1419     return AMDGPU::SI_SPILL_S1024_SAVE;
1420   default:
1421     llvm_unreachable("unknown register size");
1422   }
1423 }
1424 
1425 static unsigned getVGPRSpillSaveOpcode(unsigned Size) {
1426   switch (Size) {
1427   case 4:
1428     return AMDGPU::SI_SPILL_V32_SAVE;
1429   case 8:
1430     return AMDGPU::SI_SPILL_V64_SAVE;
1431   case 12:
1432     return AMDGPU::SI_SPILL_V96_SAVE;
1433   case 16:
1434     return AMDGPU::SI_SPILL_V128_SAVE;
1435   case 20:
1436     return AMDGPU::SI_SPILL_V160_SAVE;
1437   case 24:
1438     return AMDGPU::SI_SPILL_V192_SAVE;
1439   case 28:
1440     return AMDGPU::SI_SPILL_V224_SAVE;
1441   case 32:
1442     return AMDGPU::SI_SPILL_V256_SAVE;
1443   case 64:
1444     return AMDGPU::SI_SPILL_V512_SAVE;
1445   case 128:
1446     return AMDGPU::SI_SPILL_V1024_SAVE;
1447   default:
1448     llvm_unreachable("unknown register size");
1449   }
1450 }
1451 
1452 static unsigned getAGPRSpillSaveOpcode(unsigned Size) {
1453   switch (Size) {
1454   case 4:
1455     return AMDGPU::SI_SPILL_A32_SAVE;
1456   case 8:
1457     return AMDGPU::SI_SPILL_A64_SAVE;
1458   case 12:
1459     return AMDGPU::SI_SPILL_A96_SAVE;
1460   case 16:
1461     return AMDGPU::SI_SPILL_A128_SAVE;
1462   case 20:
1463     return AMDGPU::SI_SPILL_A160_SAVE;
1464   case 24:
1465     return AMDGPU::SI_SPILL_A192_SAVE;
1466   case 28:
1467     return AMDGPU::SI_SPILL_A224_SAVE;
1468   case 32:
1469     return AMDGPU::SI_SPILL_A256_SAVE;
1470   case 64:
1471     return AMDGPU::SI_SPILL_A512_SAVE;
1472   case 128:
1473     return AMDGPU::SI_SPILL_A1024_SAVE;
1474   default:
1475     llvm_unreachable("unknown register size");
1476   }
1477 }
1478 
1479 static unsigned getAVSpillSaveOpcode(unsigned Size) {
1480   switch (Size) {
1481   case 4:
1482     return AMDGPU::SI_SPILL_AV32_SAVE;
1483   case 8:
1484     return AMDGPU::SI_SPILL_AV64_SAVE;
1485   case 12:
1486     return AMDGPU::SI_SPILL_AV96_SAVE;
1487   case 16:
1488     return AMDGPU::SI_SPILL_AV128_SAVE;
1489   case 20:
1490     return AMDGPU::SI_SPILL_AV160_SAVE;
1491   case 24:
1492     return AMDGPU::SI_SPILL_AV192_SAVE;
1493   case 28:
1494     return AMDGPU::SI_SPILL_AV224_SAVE;
1495   case 32:
1496     return AMDGPU::SI_SPILL_AV256_SAVE;
1497   case 64:
1498     return AMDGPU::SI_SPILL_AV512_SAVE;
1499   case 128:
1500     return AMDGPU::SI_SPILL_AV1024_SAVE;
1501   default:
1502     llvm_unreachable("unknown register size");
1503   }
1504 }
1505 
1506 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
1507                                       MachineBasicBlock::iterator MI,
1508                                       Register SrcReg, bool isKill,
1509                                       int FrameIndex,
1510                                       const TargetRegisterClass *RC,
1511                                       const TargetRegisterInfo *TRI) const {
1512   MachineFunction *MF = MBB.getParent();
1513   SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1514   MachineFrameInfo &FrameInfo = MF->getFrameInfo();
1515   const DebugLoc &DL = MBB.findDebugLoc(MI);
1516 
1517   MachinePointerInfo PtrInfo
1518     = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
1519   MachineMemOperand *MMO = MF->getMachineMemOperand(
1520       PtrInfo, MachineMemOperand::MOStore, FrameInfo.getObjectSize(FrameIndex),
1521       FrameInfo.getObjectAlign(FrameIndex));
1522   unsigned SpillSize = TRI->getSpillSize(*RC);
1523 
1524   MachineRegisterInfo &MRI = MF->getRegInfo();
1525   if (RI.isSGPRClass(RC)) {
1526     MFI->setHasSpilledSGPRs();
1527     assert(SrcReg != AMDGPU::M0 && "m0 should not be spilled");
1528     assert(SrcReg != AMDGPU::EXEC_LO && SrcReg != AMDGPU::EXEC_HI &&
1529            SrcReg != AMDGPU::EXEC && "exec should not be spilled");
1530 
1531     // We are only allowed to create one new instruction when spilling
1532     // registers, so we need to use pseudo instruction for spilling SGPRs.
1533     const MCInstrDesc &OpDesc = get(getSGPRSpillSaveOpcode(SpillSize));
1534 
1535     // The SGPR spill/restore instructions only work on number sgprs, so we need
1536     // to make sure we are using the correct register class.
1537     if (SrcReg.isVirtual() && SpillSize == 4) {
1538       MRI.constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0_XEXECRegClass);
1539     }
1540 
1541     BuildMI(MBB, MI, DL, OpDesc)
1542       .addReg(SrcReg, getKillRegState(isKill)) // data
1543       .addFrameIndex(FrameIndex)               // addr
1544       .addMemOperand(MMO)
1545       .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit);
1546 
1547     if (RI.spillSGPRToVGPR())
1548       FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill);
1549     return;
1550   }
1551 
1552   unsigned Opcode = RI.isVectorSuperClass(RC) ? getAVSpillSaveOpcode(SpillSize)
1553                     : RI.isAGPRClass(RC) ? getAGPRSpillSaveOpcode(SpillSize)
1554                                          : getVGPRSpillSaveOpcode(SpillSize);
1555   MFI->setHasSpilledVGPRs();
1556 
1557   BuildMI(MBB, MI, DL, get(Opcode))
1558     .addReg(SrcReg, getKillRegState(isKill)) // data
1559     .addFrameIndex(FrameIndex)               // addr
1560     .addReg(MFI->getStackPtrOffsetReg())     // scratch_offset
1561     .addImm(0)                               // offset
1562     .addMemOperand(MMO);
1563 }
1564 
1565 static unsigned getSGPRSpillRestoreOpcode(unsigned Size) {
1566   switch (Size) {
1567   case 4:
1568     return AMDGPU::SI_SPILL_S32_RESTORE;
1569   case 8:
1570     return AMDGPU::SI_SPILL_S64_RESTORE;
1571   case 12:
1572     return AMDGPU::SI_SPILL_S96_RESTORE;
1573   case 16:
1574     return AMDGPU::SI_SPILL_S128_RESTORE;
1575   case 20:
1576     return AMDGPU::SI_SPILL_S160_RESTORE;
1577   case 24:
1578     return AMDGPU::SI_SPILL_S192_RESTORE;
1579   case 28:
1580     return AMDGPU::SI_SPILL_S224_RESTORE;
1581   case 32:
1582     return AMDGPU::SI_SPILL_S256_RESTORE;
1583   case 64:
1584     return AMDGPU::SI_SPILL_S512_RESTORE;
1585   case 128:
1586     return AMDGPU::SI_SPILL_S1024_RESTORE;
1587   default:
1588     llvm_unreachable("unknown register size");
1589   }
1590 }
1591 
1592 static unsigned getVGPRSpillRestoreOpcode(unsigned Size) {
1593   switch (Size) {
1594   case 4:
1595     return AMDGPU::SI_SPILL_V32_RESTORE;
1596   case 8:
1597     return AMDGPU::SI_SPILL_V64_RESTORE;
1598   case 12:
1599     return AMDGPU::SI_SPILL_V96_RESTORE;
1600   case 16:
1601     return AMDGPU::SI_SPILL_V128_RESTORE;
1602   case 20:
1603     return AMDGPU::SI_SPILL_V160_RESTORE;
1604   case 24:
1605     return AMDGPU::SI_SPILL_V192_RESTORE;
1606   case 28:
1607     return AMDGPU::SI_SPILL_V224_RESTORE;
1608   case 32:
1609     return AMDGPU::SI_SPILL_V256_RESTORE;
1610   case 64:
1611     return AMDGPU::SI_SPILL_V512_RESTORE;
1612   case 128:
1613     return AMDGPU::SI_SPILL_V1024_RESTORE;
1614   default:
1615     llvm_unreachable("unknown register size");
1616   }
1617 }
1618 
1619 static unsigned getAGPRSpillRestoreOpcode(unsigned Size) {
1620   switch (Size) {
1621   case 4:
1622     return AMDGPU::SI_SPILL_A32_RESTORE;
1623   case 8:
1624     return AMDGPU::SI_SPILL_A64_RESTORE;
1625   case 12:
1626     return AMDGPU::SI_SPILL_A96_RESTORE;
1627   case 16:
1628     return AMDGPU::SI_SPILL_A128_RESTORE;
1629   case 20:
1630     return AMDGPU::SI_SPILL_A160_RESTORE;
1631   case 24:
1632     return AMDGPU::SI_SPILL_A192_RESTORE;
1633   case 28:
1634     return AMDGPU::SI_SPILL_A224_RESTORE;
1635   case 32:
1636     return AMDGPU::SI_SPILL_A256_RESTORE;
1637   case 64:
1638     return AMDGPU::SI_SPILL_A512_RESTORE;
1639   case 128:
1640     return AMDGPU::SI_SPILL_A1024_RESTORE;
1641   default:
1642     llvm_unreachable("unknown register size");
1643   }
1644 }
1645 
1646 static unsigned getAVSpillRestoreOpcode(unsigned Size) {
1647   switch (Size) {
1648   case 4:
1649     return AMDGPU::SI_SPILL_AV32_RESTORE;
1650   case 8:
1651     return AMDGPU::SI_SPILL_AV64_RESTORE;
1652   case 12:
1653     return AMDGPU::SI_SPILL_AV96_RESTORE;
1654   case 16:
1655     return AMDGPU::SI_SPILL_AV128_RESTORE;
1656   case 20:
1657     return AMDGPU::SI_SPILL_AV160_RESTORE;
1658   case 24:
1659     return AMDGPU::SI_SPILL_AV192_RESTORE;
1660   case 28:
1661     return AMDGPU::SI_SPILL_AV224_RESTORE;
1662   case 32:
1663     return AMDGPU::SI_SPILL_AV256_RESTORE;
1664   case 64:
1665     return AMDGPU::SI_SPILL_AV512_RESTORE;
1666   case 128:
1667     return AMDGPU::SI_SPILL_AV1024_RESTORE;
1668   default:
1669     llvm_unreachable("unknown register size");
1670   }
1671 }
1672 
1673 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
1674                                        MachineBasicBlock::iterator MI,
1675                                        Register DestReg, int FrameIndex,
1676                                        const TargetRegisterClass *RC,
1677                                        const TargetRegisterInfo *TRI) const {
1678   MachineFunction *MF = MBB.getParent();
1679   SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1680   MachineFrameInfo &FrameInfo = MF->getFrameInfo();
1681   const DebugLoc &DL = MBB.findDebugLoc(MI);
1682   unsigned SpillSize = TRI->getSpillSize(*RC);
1683 
1684   MachinePointerInfo PtrInfo
1685     = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
1686 
1687   MachineMemOperand *MMO = MF->getMachineMemOperand(
1688       PtrInfo, MachineMemOperand::MOLoad, FrameInfo.getObjectSize(FrameIndex),
1689       FrameInfo.getObjectAlign(FrameIndex));
1690 
1691   if (RI.isSGPRClass(RC)) {
1692     MFI->setHasSpilledSGPRs();
1693     assert(DestReg != AMDGPU::M0 && "m0 should not be reloaded into");
1694     assert(DestReg != AMDGPU::EXEC_LO && DestReg != AMDGPU::EXEC_HI &&
1695            DestReg != AMDGPU::EXEC && "exec should not be spilled");
1696 
1697     // FIXME: Maybe this should not include a memoperand because it will be
1698     // lowered to non-memory instructions.
1699     const MCInstrDesc &OpDesc = get(getSGPRSpillRestoreOpcode(SpillSize));
1700     if (DestReg.isVirtual() && SpillSize == 4) {
1701       MachineRegisterInfo &MRI = MF->getRegInfo();
1702       MRI.constrainRegClass(DestReg, &AMDGPU::SReg_32_XM0_XEXECRegClass);
1703     }
1704 
1705     if (RI.spillSGPRToVGPR())
1706       FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill);
1707     BuildMI(MBB, MI, DL, OpDesc, DestReg)
1708       .addFrameIndex(FrameIndex) // addr
1709       .addMemOperand(MMO)
1710       .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit);
1711 
1712     return;
1713   }
1714 
1715   unsigned Opcode = RI.isVectorSuperClass(RC)
1716                         ? getAVSpillRestoreOpcode(SpillSize)
1717                     : RI.isAGPRClass(RC) ? getAGPRSpillRestoreOpcode(SpillSize)
1718                                          : getVGPRSpillRestoreOpcode(SpillSize);
1719   BuildMI(MBB, MI, DL, get(Opcode), DestReg)
1720       .addFrameIndex(FrameIndex)           // vaddr
1721       .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset
1722       .addImm(0)                           // offset
1723       .addMemOperand(MMO);
1724 }
1725 
1726 void SIInstrInfo::insertNoop(MachineBasicBlock &MBB,
1727                              MachineBasicBlock::iterator MI) const {
1728   insertNoops(MBB, MI, 1);
1729 }
1730 
1731 void SIInstrInfo::insertNoops(MachineBasicBlock &MBB,
1732                               MachineBasicBlock::iterator MI,
1733                               unsigned Quantity) const {
1734   DebugLoc DL = MBB.findDebugLoc(MI);
1735   while (Quantity > 0) {
1736     unsigned Arg = std::min(Quantity, 8u);
1737     Quantity -= Arg;
1738     BuildMI(MBB, MI, DL, get(AMDGPU::S_NOP)).addImm(Arg - 1);
1739   }
1740 }
1741 
1742 void SIInstrInfo::insertReturn(MachineBasicBlock &MBB) const {
1743   auto MF = MBB.getParent();
1744   SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>();
1745 
1746   assert(Info->isEntryFunction());
1747 
1748   if (MBB.succ_empty()) {
1749     bool HasNoTerminator = MBB.getFirstTerminator() == MBB.end();
1750     if (HasNoTerminator) {
1751       if (Info->returnsVoid()) {
1752         BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::S_ENDPGM)).addImm(0);
1753       } else {
1754         BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::SI_RETURN_TO_EPILOG));
1755       }
1756     }
1757   }
1758 }
1759 
1760 unsigned SIInstrInfo::getNumWaitStates(const MachineInstr &MI) {
1761   switch (MI.getOpcode()) {
1762   default:
1763     if (MI.isMetaInstruction())
1764       return 0;
1765     return 1; // FIXME: Do wait states equal cycles?
1766 
1767   case AMDGPU::S_NOP:
1768     return MI.getOperand(0).getImm() + 1;
1769 
1770   // FIXME: Any other pseudo instruction?
1771   // SI_RETURN_TO_EPILOG is a fallthrough to code outside of the function. The
1772   // hazard, even if one exist, won't really be visible. Should we handle it?
1773   case AMDGPU::SI_MASKED_UNREACHABLE:
1774   case AMDGPU::WAVE_BARRIER:
1775     return 0;
1776   }
1777 }
1778 
1779 bool SIInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1780   const SIRegisterInfo *TRI = ST.getRegisterInfo();
1781   MachineBasicBlock &MBB = *MI.getParent();
1782   DebugLoc DL = MBB.findDebugLoc(MI);
1783   switch (MI.getOpcode()) {
1784   default: return TargetInstrInfo::expandPostRAPseudo(MI);
1785   case AMDGPU::S_MOV_B64_term:
1786     // This is only a terminator to get the correct spill code placement during
1787     // register allocation.
1788     MI.setDesc(get(AMDGPU::S_MOV_B64));
1789     break;
1790 
1791   case AMDGPU::S_MOV_B32_term:
1792     // This is only a terminator to get the correct spill code placement during
1793     // register allocation.
1794     MI.setDesc(get(AMDGPU::S_MOV_B32));
1795     break;
1796 
1797   case AMDGPU::S_XOR_B64_term:
1798     // This is only a terminator to get the correct spill code placement during
1799     // register allocation.
1800     MI.setDesc(get(AMDGPU::S_XOR_B64));
1801     break;
1802 
1803   case AMDGPU::S_XOR_B32_term:
1804     // This is only a terminator to get the correct spill code placement during
1805     // register allocation.
1806     MI.setDesc(get(AMDGPU::S_XOR_B32));
1807     break;
1808   case AMDGPU::S_OR_B64_term:
1809     // This is only a terminator to get the correct spill code placement during
1810     // register allocation.
1811     MI.setDesc(get(AMDGPU::S_OR_B64));
1812     break;
1813   case AMDGPU::S_OR_B32_term:
1814     // This is only a terminator to get the correct spill code placement during
1815     // register allocation.
1816     MI.setDesc(get(AMDGPU::S_OR_B32));
1817     break;
1818 
1819   case AMDGPU::S_ANDN2_B64_term:
1820     // This is only a terminator to get the correct spill code placement during
1821     // register allocation.
1822     MI.setDesc(get(AMDGPU::S_ANDN2_B64));
1823     break;
1824 
1825   case AMDGPU::S_ANDN2_B32_term:
1826     // This is only a terminator to get the correct spill code placement during
1827     // register allocation.
1828     MI.setDesc(get(AMDGPU::S_ANDN2_B32));
1829     break;
1830 
1831   case AMDGPU::S_AND_B64_term:
1832     // This is only a terminator to get the correct spill code placement during
1833     // register allocation.
1834     MI.setDesc(get(AMDGPU::S_AND_B64));
1835     break;
1836 
1837   case AMDGPU::S_AND_B32_term:
1838     // This is only a terminator to get the correct spill code placement during
1839     // register allocation.
1840     MI.setDesc(get(AMDGPU::S_AND_B32));
1841     break;
1842 
1843   case AMDGPU::V_MOV_B64_PSEUDO: {
1844     Register Dst = MI.getOperand(0).getReg();
1845     Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
1846     Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
1847 
1848     const MachineOperand &SrcOp = MI.getOperand(1);
1849     // FIXME: Will this work for 64-bit floating point immediates?
1850     assert(!SrcOp.isFPImm());
1851     if (ST.hasMovB64()) {
1852       MI.setDesc(get(AMDGPU::V_MOV_B64_e32));
1853       if (!isLiteralConstant(MI, 1) || isUInt<32>(SrcOp.getImm()))
1854         break;
1855     }
1856     if (SrcOp.isImm()) {
1857       APInt Imm(64, SrcOp.getImm());
1858       APInt Lo(32, Imm.getLoBits(32).getZExtValue());
1859       APInt Hi(32, Imm.getHiBits(32).getZExtValue());
1860       if (ST.hasPackedFP32Ops() && Lo == Hi && isInlineConstant(Lo)) {
1861         BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst)
1862           .addImm(SISrcMods::OP_SEL_1)
1863           .addImm(Lo.getSExtValue())
1864           .addImm(SISrcMods::OP_SEL_1)
1865           .addImm(Lo.getSExtValue())
1866           .addImm(0)  // op_sel_lo
1867           .addImm(0)  // op_sel_hi
1868           .addImm(0)  // neg_lo
1869           .addImm(0)  // neg_hi
1870           .addImm(0); // clamp
1871       } else {
1872         BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
1873           .addImm(Lo.getSExtValue())
1874           .addReg(Dst, RegState::Implicit | RegState::Define);
1875         BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
1876           .addImm(Hi.getSExtValue())
1877           .addReg(Dst, RegState::Implicit | RegState::Define);
1878       }
1879     } else {
1880       assert(SrcOp.isReg());
1881       if (ST.hasPackedFP32Ops() &&
1882           !RI.isAGPR(MBB.getParent()->getRegInfo(), SrcOp.getReg())) {
1883         BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst)
1884           .addImm(SISrcMods::OP_SEL_1) // src0_mod
1885           .addReg(SrcOp.getReg())
1886           .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1) // src1_mod
1887           .addReg(SrcOp.getReg())
1888           .addImm(0)  // op_sel_lo
1889           .addImm(0)  // op_sel_hi
1890           .addImm(0)  // neg_lo
1891           .addImm(0)  // neg_hi
1892           .addImm(0); // clamp
1893       } else {
1894         BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
1895           .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0))
1896           .addReg(Dst, RegState::Implicit | RegState::Define);
1897         BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
1898           .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1))
1899           .addReg(Dst, RegState::Implicit | RegState::Define);
1900       }
1901     }
1902     MI.eraseFromParent();
1903     break;
1904   }
1905   case AMDGPU::V_MOV_B64_DPP_PSEUDO: {
1906     expandMovDPP64(MI);
1907     break;
1908   }
1909   case AMDGPU::S_MOV_B64_IMM_PSEUDO: {
1910     const MachineOperand &SrcOp = MI.getOperand(1);
1911     assert(!SrcOp.isFPImm());
1912     APInt Imm(64, SrcOp.getImm());
1913     if (Imm.isIntN(32) || isInlineConstant(Imm)) {
1914       MI.setDesc(get(AMDGPU::S_MOV_B64));
1915       break;
1916     }
1917 
1918     Register Dst = MI.getOperand(0).getReg();
1919     Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
1920     Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
1921 
1922     APInt Lo(32, Imm.getLoBits(32).getZExtValue());
1923     APInt Hi(32, Imm.getHiBits(32).getZExtValue());
1924     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DstLo)
1925       .addImm(Lo.getSExtValue())
1926       .addReg(Dst, RegState::Implicit | RegState::Define);
1927     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DstHi)
1928       .addImm(Hi.getSExtValue())
1929       .addReg(Dst, RegState::Implicit | RegState::Define);
1930     MI.eraseFromParent();
1931     break;
1932   }
1933   case AMDGPU::V_SET_INACTIVE_B32: {
1934     unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64;
1935     unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
1936     // FIXME: We may possibly optimize the COPY once we find ways to make LLVM
1937     // optimizations (mainly Register Coalescer) aware of WWM register liveness.
1938     BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), MI.getOperand(0).getReg())
1939         .add(MI.getOperand(1));
1940     auto FirstNot = BuildMI(MBB, MI, DL, get(NotOpc), Exec).addReg(Exec);
1941     FirstNot->addRegisterDead(AMDGPU::SCC, TRI); // SCC is overwritten
1942     BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), MI.getOperand(0).getReg())
1943       .add(MI.getOperand(2));
1944     BuildMI(MBB, MI, DL, get(NotOpc), Exec)
1945       .addReg(Exec);
1946     MI.eraseFromParent();
1947     break;
1948   }
1949   case AMDGPU::V_SET_INACTIVE_B64: {
1950     unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64;
1951     unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
1952     MachineInstr *Copy = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO),
1953                                  MI.getOperand(0).getReg())
1954                              .add(MI.getOperand(1));
1955     expandPostRAPseudo(*Copy);
1956     auto FirstNot = BuildMI(MBB, MI, DL, get(NotOpc), Exec).addReg(Exec);
1957     FirstNot->addRegisterDead(AMDGPU::SCC, TRI); // SCC is overwritten
1958     Copy = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO),
1959                    MI.getOperand(0).getReg())
1960                .add(MI.getOperand(2));
1961     expandPostRAPseudo(*Copy);
1962     BuildMI(MBB, MI, DL, get(NotOpc), Exec)
1963       .addReg(Exec);
1964     MI.eraseFromParent();
1965     break;
1966   }
1967   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1:
1968   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2:
1969   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3:
1970   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4:
1971   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5:
1972   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8:
1973   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16:
1974   case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32:
1975   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1:
1976   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2:
1977   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3:
1978   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4:
1979   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5:
1980   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8:
1981   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16:
1982   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32:
1983   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1:
1984   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2:
1985   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4:
1986   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8:
1987   case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16: {
1988     const TargetRegisterClass *EltRC = getOpRegClass(MI, 2);
1989 
1990     unsigned Opc;
1991     if (RI.hasVGPRs(EltRC)) {
1992       Opc = AMDGPU::V_MOVRELD_B32_e32;
1993     } else {
1994       Opc = RI.getRegSizeInBits(*EltRC) == 64 ? AMDGPU::S_MOVRELD_B64
1995                                               : AMDGPU::S_MOVRELD_B32;
1996     }
1997 
1998     const MCInstrDesc &OpDesc = get(Opc);
1999     Register VecReg = MI.getOperand(0).getReg();
2000     bool IsUndef = MI.getOperand(1).isUndef();
2001     unsigned SubReg = MI.getOperand(3).getImm();
2002     assert(VecReg == MI.getOperand(1).getReg());
2003 
2004     MachineInstrBuilder MIB =
2005       BuildMI(MBB, MI, DL, OpDesc)
2006         .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef)
2007         .add(MI.getOperand(2))
2008         .addReg(VecReg, RegState::ImplicitDefine)
2009         .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0));
2010 
2011     const int ImpDefIdx =
2012       OpDesc.getNumOperands() + OpDesc.getNumImplicitUses();
2013     const int ImpUseIdx = ImpDefIdx + 1;
2014     MIB->tieOperands(ImpDefIdx, ImpUseIdx);
2015     MI.eraseFromParent();
2016     break;
2017   }
2018   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1:
2019   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2:
2020   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3:
2021   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4:
2022   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5:
2023   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8:
2024   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16:
2025   case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32: {
2026     assert(ST.useVGPRIndexMode());
2027     Register VecReg = MI.getOperand(0).getReg();
2028     bool IsUndef = MI.getOperand(1).isUndef();
2029     Register Idx = MI.getOperand(3).getReg();
2030     Register SubReg = MI.getOperand(4).getImm();
2031 
2032     MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON))
2033                               .addReg(Idx)
2034                               .addImm(AMDGPU::VGPRIndexMode::DST_ENABLE);
2035     SetOn->getOperand(3).setIsUndef();
2036 
2037     const MCInstrDesc &OpDesc = get(AMDGPU::V_MOV_B32_indirect_write);
2038     MachineInstrBuilder MIB =
2039         BuildMI(MBB, MI, DL, OpDesc)
2040             .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef)
2041             .add(MI.getOperand(2))
2042             .addReg(VecReg, RegState::ImplicitDefine)
2043             .addReg(VecReg,
2044                     RegState::Implicit | (IsUndef ? RegState::Undef : 0));
2045 
2046     const int ImpDefIdx = OpDesc.getNumOperands() + OpDesc.getNumImplicitUses();
2047     const int ImpUseIdx = ImpDefIdx + 1;
2048     MIB->tieOperands(ImpDefIdx, ImpUseIdx);
2049 
2050     MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF));
2051 
2052     finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator()));
2053 
2054     MI.eraseFromParent();
2055     break;
2056   }
2057   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1:
2058   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2:
2059   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3:
2060   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4:
2061   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5:
2062   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8:
2063   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16:
2064   case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32: {
2065     assert(ST.useVGPRIndexMode());
2066     Register Dst = MI.getOperand(0).getReg();
2067     Register VecReg = MI.getOperand(1).getReg();
2068     bool IsUndef = MI.getOperand(1).isUndef();
2069     Register Idx = MI.getOperand(2).getReg();
2070     Register SubReg = MI.getOperand(3).getImm();
2071 
2072     MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON))
2073                               .addReg(Idx)
2074                               .addImm(AMDGPU::VGPRIndexMode::SRC0_ENABLE);
2075     SetOn->getOperand(3).setIsUndef();
2076 
2077     BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_indirect_read))
2078         .addDef(Dst)
2079         .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef)
2080         .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0));
2081 
2082     MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF));
2083 
2084     finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator()));
2085 
2086     MI.eraseFromParent();
2087     break;
2088   }
2089   case AMDGPU::SI_PC_ADD_REL_OFFSET: {
2090     MachineFunction &MF = *MBB.getParent();
2091     Register Reg = MI.getOperand(0).getReg();
2092     Register RegLo = RI.getSubReg(Reg, AMDGPU::sub0);
2093     Register RegHi = RI.getSubReg(Reg, AMDGPU::sub1);
2094 
2095     // Create a bundle so these instructions won't be re-ordered by the
2096     // post-RA scheduler.
2097     MIBundleBuilder Bundler(MBB, MI);
2098     Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg));
2099 
2100     // Add 32-bit offset from this instruction to the start of the
2101     // constant data.
2102     Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_ADD_U32), RegLo)
2103                        .addReg(RegLo)
2104                        .add(MI.getOperand(1)));
2105 
2106     MachineInstrBuilder MIB = BuildMI(MF, DL, get(AMDGPU::S_ADDC_U32), RegHi)
2107                                   .addReg(RegHi);
2108     MIB.add(MI.getOperand(2));
2109 
2110     Bundler.append(MIB);
2111     finalizeBundle(MBB, Bundler.begin());
2112 
2113     MI.eraseFromParent();
2114     break;
2115   }
2116   case AMDGPU::ENTER_STRICT_WWM: {
2117     // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
2118     // Whole Wave Mode is entered.
2119     MI.setDesc(get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32
2120                                  : AMDGPU::S_OR_SAVEEXEC_B64));
2121     break;
2122   }
2123   case AMDGPU::ENTER_STRICT_WQM: {
2124     // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
2125     // STRICT_WQM is entered.
2126     const unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
2127     const unsigned WQMOp = ST.isWave32() ? AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64;
2128     const unsigned MovOp = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
2129     BuildMI(MBB, MI, DL, get(MovOp), MI.getOperand(0).getReg()).addReg(Exec);
2130     BuildMI(MBB, MI, DL, get(WQMOp), Exec).addReg(Exec);
2131 
2132     MI.eraseFromParent();
2133     break;
2134   }
2135   case AMDGPU::EXIT_STRICT_WWM:
2136   case AMDGPU::EXIT_STRICT_WQM: {
2137     // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when
2138     // WWM/STICT_WQM is exited.
2139     MI.setDesc(get(ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64));
2140     break;
2141   }
2142   case AMDGPU::SI_RETURN: {
2143     const MachineFunction *MF = MBB.getParent();
2144     const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
2145     const SIRegisterInfo *TRI = ST.getRegisterInfo();
2146     // Hiding the return address use with SI_RETURN may lead to extra kills in
2147     // the function and missing live-ins. We are fine in practice because callee
2148     // saved register handling ensures the register value is restored before
2149     // RET, but we need the undef flag here to appease the MachineVerifier
2150     // liveness checks.
2151     MachineInstrBuilder MIB =
2152         BuildMI(MBB, MI, DL, get(AMDGPU::S_SETPC_B64_return))
2153             .addReg(TRI->getReturnAddressReg(*MF), RegState::Undef);
2154 
2155     MIB.copyImplicitOps(MI);
2156     MI.eraseFromParent();
2157     break;
2158   }
2159   }
2160   return true;
2161 }
2162 
2163 std::pair<MachineInstr*, MachineInstr*>
2164 SIInstrInfo::expandMovDPP64(MachineInstr &MI) const {
2165   assert (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO);
2166 
2167   if (ST.hasMovB64() &&
2168       AMDGPU::isLegal64BitDPPControl(
2169         getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl)->getImm())) {
2170     MI.setDesc(get(AMDGPU::V_MOV_B64_dpp));
2171     return std::make_pair(&MI, nullptr);
2172   }
2173 
2174   MachineBasicBlock &MBB = *MI.getParent();
2175   DebugLoc DL = MBB.findDebugLoc(MI);
2176   MachineFunction *MF = MBB.getParent();
2177   MachineRegisterInfo &MRI = MF->getRegInfo();
2178   Register Dst = MI.getOperand(0).getReg();
2179   unsigned Part = 0;
2180   MachineInstr *Split[2];
2181 
2182   for (auto Sub : { AMDGPU::sub0, AMDGPU::sub1 }) {
2183     auto MovDPP = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_dpp));
2184     if (Dst.isPhysical()) {
2185       MovDPP.addDef(RI.getSubReg(Dst, Sub));
2186     } else {
2187       assert(MRI.isSSA());
2188       auto Tmp = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2189       MovDPP.addDef(Tmp);
2190     }
2191 
2192     for (unsigned I = 1; I <= 2; ++I) { // old and src operands.
2193       const MachineOperand &SrcOp = MI.getOperand(I);
2194       assert(!SrcOp.isFPImm());
2195       if (SrcOp.isImm()) {
2196         APInt Imm(64, SrcOp.getImm());
2197         Imm.ashrInPlace(Part * 32);
2198         MovDPP.addImm(Imm.getLoBits(32).getZExtValue());
2199       } else {
2200         assert(SrcOp.isReg());
2201         Register Src = SrcOp.getReg();
2202         if (Src.isPhysical())
2203           MovDPP.addReg(RI.getSubReg(Src, Sub));
2204         else
2205           MovDPP.addReg(Src, SrcOp.isUndef() ? RegState::Undef : 0, Sub);
2206       }
2207     }
2208 
2209     for (unsigned I = 3; I < MI.getNumExplicitOperands(); ++I)
2210       MovDPP.addImm(MI.getOperand(I).getImm());
2211 
2212     Split[Part] = MovDPP;
2213     ++Part;
2214   }
2215 
2216   if (Dst.isVirtual())
2217     BuildMI(MBB, MI, DL, get(AMDGPU::REG_SEQUENCE), Dst)
2218       .addReg(Split[0]->getOperand(0).getReg())
2219       .addImm(AMDGPU::sub0)
2220       .addReg(Split[1]->getOperand(0).getReg())
2221       .addImm(AMDGPU::sub1);
2222 
2223   MI.eraseFromParent();
2224   return std::make_pair(Split[0], Split[1]);
2225 }
2226 
2227 bool SIInstrInfo::swapSourceModifiers(MachineInstr &MI,
2228                                       MachineOperand &Src0,
2229                                       unsigned Src0OpName,
2230                                       MachineOperand &Src1,
2231                                       unsigned Src1OpName) const {
2232   MachineOperand *Src0Mods = getNamedOperand(MI, Src0OpName);
2233   if (!Src0Mods)
2234     return false;
2235 
2236   MachineOperand *Src1Mods = getNamedOperand(MI, Src1OpName);
2237   assert(Src1Mods &&
2238          "All commutable instructions have both src0 and src1 modifiers");
2239 
2240   int Src0ModsVal = Src0Mods->getImm();
2241   int Src1ModsVal = Src1Mods->getImm();
2242 
2243   Src1Mods->setImm(Src0ModsVal);
2244   Src0Mods->setImm(Src1ModsVal);
2245   return true;
2246 }
2247 
2248 static MachineInstr *swapRegAndNonRegOperand(MachineInstr &MI,
2249                                              MachineOperand &RegOp,
2250                                              MachineOperand &NonRegOp) {
2251   Register Reg = RegOp.getReg();
2252   unsigned SubReg = RegOp.getSubReg();
2253   bool IsKill = RegOp.isKill();
2254   bool IsDead = RegOp.isDead();
2255   bool IsUndef = RegOp.isUndef();
2256   bool IsDebug = RegOp.isDebug();
2257 
2258   if (NonRegOp.isImm())
2259     RegOp.ChangeToImmediate(NonRegOp.getImm());
2260   else if (NonRegOp.isFI())
2261     RegOp.ChangeToFrameIndex(NonRegOp.getIndex());
2262   else if (NonRegOp.isGlobal()) {
2263     RegOp.ChangeToGA(NonRegOp.getGlobal(), NonRegOp.getOffset(),
2264                      NonRegOp.getTargetFlags());
2265   } else
2266     return nullptr;
2267 
2268   // Make sure we don't reinterpret a subreg index in the target flags.
2269   RegOp.setTargetFlags(NonRegOp.getTargetFlags());
2270 
2271   NonRegOp.ChangeToRegister(Reg, false, false, IsKill, IsDead, IsUndef, IsDebug);
2272   NonRegOp.setSubReg(SubReg);
2273 
2274   return &MI;
2275 }
2276 
2277 MachineInstr *SIInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
2278                                                   unsigned Src0Idx,
2279                                                   unsigned Src1Idx) const {
2280   assert(!NewMI && "this should never be used");
2281 
2282   unsigned Opc = MI.getOpcode();
2283   int CommutedOpcode = commuteOpcode(Opc);
2284   if (CommutedOpcode == -1)
2285     return nullptr;
2286 
2287   assert(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) ==
2288            static_cast<int>(Src0Idx) &&
2289          AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) ==
2290            static_cast<int>(Src1Idx) &&
2291          "inconsistency with findCommutedOpIndices");
2292 
2293   MachineOperand &Src0 = MI.getOperand(Src0Idx);
2294   MachineOperand &Src1 = MI.getOperand(Src1Idx);
2295 
2296   MachineInstr *CommutedMI = nullptr;
2297   if (Src0.isReg() && Src1.isReg()) {
2298     if (isOperandLegal(MI, Src1Idx, &Src0)) {
2299       // Be sure to copy the source modifiers to the right place.
2300       CommutedMI
2301         = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, Src0Idx, Src1Idx);
2302     }
2303 
2304   } else if (Src0.isReg() && !Src1.isReg()) {
2305     // src0 should always be able to support any operand type, so no need to
2306     // check operand legality.
2307     CommutedMI = swapRegAndNonRegOperand(MI, Src0, Src1);
2308   } else if (!Src0.isReg() && Src1.isReg()) {
2309     if (isOperandLegal(MI, Src1Idx, &Src0))
2310       CommutedMI = swapRegAndNonRegOperand(MI, Src1, Src0);
2311   } else {
2312     // FIXME: Found two non registers to commute. This does happen.
2313     return nullptr;
2314   }
2315 
2316   if (CommutedMI) {
2317     swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_modifiers,
2318                         Src1, AMDGPU::OpName::src1_modifiers);
2319 
2320     CommutedMI->setDesc(get(CommutedOpcode));
2321   }
2322 
2323   return CommutedMI;
2324 }
2325 
2326 // This needs to be implemented because the source modifiers may be inserted
2327 // between the true commutable operands, and the base
2328 // TargetInstrInfo::commuteInstruction uses it.
2329 bool SIInstrInfo::findCommutedOpIndices(const MachineInstr &MI,
2330                                         unsigned &SrcOpIdx0,
2331                                         unsigned &SrcOpIdx1) const {
2332   return findCommutedOpIndices(MI.getDesc(), SrcOpIdx0, SrcOpIdx1);
2333 }
2334 
2335 bool SIInstrInfo::findCommutedOpIndices(MCInstrDesc Desc, unsigned &SrcOpIdx0,
2336                                         unsigned &SrcOpIdx1) const {
2337   if (!Desc.isCommutable())
2338     return false;
2339 
2340   unsigned Opc = Desc.getOpcode();
2341   int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
2342   if (Src0Idx == -1)
2343     return false;
2344 
2345   int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
2346   if (Src1Idx == -1)
2347     return false;
2348 
2349   return fixCommutedOpIndices(SrcOpIdx0, SrcOpIdx1, Src0Idx, Src1Idx);
2350 }
2351 
2352 bool SIInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
2353                                         int64_t BrOffset) const {
2354   // BranchRelaxation should never have to check s_setpc_b64 because its dest
2355   // block is unanalyzable.
2356   assert(BranchOp != AMDGPU::S_SETPC_B64);
2357 
2358   // Convert to dwords.
2359   BrOffset /= 4;
2360 
2361   // The branch instructions do PC += signext(SIMM16 * 4) + 4, so the offset is
2362   // from the next instruction.
2363   BrOffset -= 1;
2364 
2365   return isIntN(BranchOffsetBits, BrOffset);
2366 }
2367 
2368 MachineBasicBlock *SIInstrInfo::getBranchDestBlock(
2369   const MachineInstr &MI) const {
2370   if (MI.getOpcode() == AMDGPU::S_SETPC_B64) {
2371     // This would be a difficult analysis to perform, but can always be legal so
2372     // there's no need to analyze it.
2373     return nullptr;
2374   }
2375 
2376   return MI.getOperand(0).getMBB();
2377 }
2378 
2379 void SIInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
2380                                        MachineBasicBlock &DestBB,
2381                                        MachineBasicBlock &RestoreBB,
2382                                        const DebugLoc &DL, int64_t BrOffset,
2383                                        RegScavenger *RS) const {
2384   assert(RS && "RegScavenger required for long branching");
2385   assert(MBB.empty() &&
2386          "new block should be inserted for expanding unconditional branch");
2387   assert(MBB.pred_size() == 1);
2388   assert(RestoreBB.empty() &&
2389          "restore block should be inserted for restoring clobbered registers");
2390 
2391   MachineFunction *MF = MBB.getParent();
2392   MachineRegisterInfo &MRI = MF->getRegInfo();
2393 
2394   // FIXME: Virtual register workaround for RegScavenger not working with empty
2395   // blocks.
2396   Register PCReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
2397 
2398   auto I = MBB.end();
2399 
2400   // We need to compute the offset relative to the instruction immediately after
2401   // s_getpc_b64. Insert pc arithmetic code before last terminator.
2402   MachineInstr *GetPC = BuildMI(MBB, I, DL, get(AMDGPU::S_GETPC_B64), PCReg);
2403 
2404   auto &MCCtx = MF->getContext();
2405   MCSymbol *PostGetPCLabel =
2406       MCCtx.createTempSymbol("post_getpc", /*AlwaysAddSuffix=*/true);
2407   GetPC->setPostInstrSymbol(*MF, PostGetPCLabel);
2408 
2409   MCSymbol *OffsetLo =
2410       MCCtx.createTempSymbol("offset_lo", /*AlwaysAddSuffix=*/true);
2411   MCSymbol *OffsetHi =
2412       MCCtx.createTempSymbol("offset_hi", /*AlwaysAddSuffix=*/true);
2413   BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_U32))
2414       .addReg(PCReg, RegState::Define, AMDGPU::sub0)
2415       .addReg(PCReg, 0, AMDGPU::sub0)
2416       .addSym(OffsetLo, MO_FAR_BRANCH_OFFSET);
2417   BuildMI(MBB, I, DL, get(AMDGPU::S_ADDC_U32))
2418       .addReg(PCReg, RegState::Define, AMDGPU::sub1)
2419       .addReg(PCReg, 0, AMDGPU::sub1)
2420       .addSym(OffsetHi, MO_FAR_BRANCH_OFFSET);
2421 
2422   // Insert the indirect branch after the other terminator.
2423   BuildMI(&MBB, DL, get(AMDGPU::S_SETPC_B64))
2424     .addReg(PCReg);
2425 
2426   // FIXME: If spilling is necessary, this will fail because this scavenger has
2427   // no emergency stack slots. It is non-trivial to spill in this situation,
2428   // because the restore code needs to be specially placed after the
2429   // jump. BranchRelaxation then needs to be made aware of the newly inserted
2430   // block.
2431   //
2432   // If a spill is needed for the pc register pair, we need to insert a spill
2433   // restore block right before the destination block, and insert a short branch
2434   // into the old destination block's fallthrough predecessor.
2435   // e.g.:
2436   //
2437   // s_cbranch_scc0 skip_long_branch:
2438   //
2439   // long_branch_bb:
2440   //   spill s[8:9]
2441   //   s_getpc_b64 s[8:9]
2442   //   s_add_u32 s8, s8, restore_bb
2443   //   s_addc_u32 s9, s9, 0
2444   //   s_setpc_b64 s[8:9]
2445   //
2446   // skip_long_branch:
2447   //   foo;
2448   //
2449   // .....
2450   //
2451   // dest_bb_fallthrough_predecessor:
2452   // bar;
2453   // s_branch dest_bb
2454   //
2455   // restore_bb:
2456   //  restore s[8:9]
2457   //  fallthrough dest_bb
2458   ///
2459   // dest_bb:
2460   //   buzz;
2461 
2462   RS->enterBasicBlockEnd(MBB);
2463   Register Scav = RS->scavengeRegisterBackwards(
2464       AMDGPU::SReg_64RegClass, MachineBasicBlock::iterator(GetPC),
2465       /* RestoreAfter */ false, 0, /* AllowSpill */ false);
2466   if (Scav) {
2467     RS->setRegUsed(Scav);
2468     MRI.replaceRegWith(PCReg, Scav);
2469     MRI.clearVirtRegs();
2470   } else {
2471     // As SGPR needs VGPR to be spilled, we reuse the slot of temporary VGPR for
2472     // SGPR spill.
2473     const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
2474     const SIRegisterInfo *TRI = ST.getRegisterInfo();
2475     TRI->spillEmergencySGPR(GetPC, RestoreBB, AMDGPU::SGPR0_SGPR1, RS);
2476     MRI.replaceRegWith(PCReg, AMDGPU::SGPR0_SGPR1);
2477     MRI.clearVirtRegs();
2478   }
2479 
2480   MCSymbol *DestLabel = Scav ? DestBB.getSymbol() : RestoreBB.getSymbol();
2481   // Now, the distance could be defined.
2482   auto *Offset = MCBinaryExpr::createSub(
2483       MCSymbolRefExpr::create(DestLabel, MCCtx),
2484       MCSymbolRefExpr::create(PostGetPCLabel, MCCtx), MCCtx);
2485   // Add offset assignments.
2486   auto *Mask = MCConstantExpr::create(0xFFFFFFFFULL, MCCtx);
2487   OffsetLo->setVariableValue(MCBinaryExpr::createAnd(Offset, Mask, MCCtx));
2488   auto *ShAmt = MCConstantExpr::create(32, MCCtx);
2489   OffsetHi->setVariableValue(MCBinaryExpr::createAShr(Offset, ShAmt, MCCtx));
2490 }
2491 
2492 unsigned SIInstrInfo::getBranchOpcode(SIInstrInfo::BranchPredicate Cond) {
2493   switch (Cond) {
2494   case SIInstrInfo::SCC_TRUE:
2495     return AMDGPU::S_CBRANCH_SCC1;
2496   case SIInstrInfo::SCC_FALSE:
2497     return AMDGPU::S_CBRANCH_SCC0;
2498   case SIInstrInfo::VCCNZ:
2499     return AMDGPU::S_CBRANCH_VCCNZ;
2500   case SIInstrInfo::VCCZ:
2501     return AMDGPU::S_CBRANCH_VCCZ;
2502   case SIInstrInfo::EXECNZ:
2503     return AMDGPU::S_CBRANCH_EXECNZ;
2504   case SIInstrInfo::EXECZ:
2505     return AMDGPU::S_CBRANCH_EXECZ;
2506   default:
2507     llvm_unreachable("invalid branch predicate");
2508   }
2509 }
2510 
2511 SIInstrInfo::BranchPredicate SIInstrInfo::getBranchPredicate(unsigned Opcode) {
2512   switch (Opcode) {
2513   case AMDGPU::S_CBRANCH_SCC0:
2514     return SCC_FALSE;
2515   case AMDGPU::S_CBRANCH_SCC1:
2516     return SCC_TRUE;
2517   case AMDGPU::S_CBRANCH_VCCNZ:
2518     return VCCNZ;
2519   case AMDGPU::S_CBRANCH_VCCZ:
2520     return VCCZ;
2521   case AMDGPU::S_CBRANCH_EXECNZ:
2522     return EXECNZ;
2523   case AMDGPU::S_CBRANCH_EXECZ:
2524     return EXECZ;
2525   default:
2526     return INVALID_BR;
2527   }
2528 }
2529 
2530 bool SIInstrInfo::analyzeBranchImpl(MachineBasicBlock &MBB,
2531                                     MachineBasicBlock::iterator I,
2532                                     MachineBasicBlock *&TBB,
2533                                     MachineBasicBlock *&FBB,
2534                                     SmallVectorImpl<MachineOperand> &Cond,
2535                                     bool AllowModify) const {
2536   if (I->getOpcode() == AMDGPU::S_BRANCH) {
2537     // Unconditional Branch
2538     TBB = I->getOperand(0).getMBB();
2539     return false;
2540   }
2541 
2542   MachineBasicBlock *CondBB = nullptr;
2543 
2544   if (I->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
2545     CondBB = I->getOperand(1).getMBB();
2546     Cond.push_back(I->getOperand(0));
2547   } else {
2548     BranchPredicate Pred = getBranchPredicate(I->getOpcode());
2549     if (Pred == INVALID_BR)
2550       return true;
2551 
2552     CondBB = I->getOperand(0).getMBB();
2553     Cond.push_back(MachineOperand::CreateImm(Pred));
2554     Cond.push_back(I->getOperand(1)); // Save the branch register.
2555   }
2556   ++I;
2557 
2558   if (I == MBB.end()) {
2559     // Conditional branch followed by fall-through.
2560     TBB = CondBB;
2561     return false;
2562   }
2563 
2564   if (I->getOpcode() == AMDGPU::S_BRANCH) {
2565     TBB = CondBB;
2566     FBB = I->getOperand(0).getMBB();
2567     return false;
2568   }
2569 
2570   return true;
2571 }
2572 
2573 bool SIInstrInfo::analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
2574                                 MachineBasicBlock *&FBB,
2575                                 SmallVectorImpl<MachineOperand> &Cond,
2576                                 bool AllowModify) const {
2577   MachineBasicBlock::iterator I = MBB.getFirstTerminator();
2578   auto E = MBB.end();
2579   if (I == E)
2580     return false;
2581 
2582   // Skip over the instructions that are artificially terminators for special
2583   // exec management.
2584   while (I != E && !I->isBranch() && !I->isReturn()) {
2585     switch (I->getOpcode()) {
2586     case AMDGPU::S_MOV_B64_term:
2587     case AMDGPU::S_XOR_B64_term:
2588     case AMDGPU::S_OR_B64_term:
2589     case AMDGPU::S_ANDN2_B64_term:
2590     case AMDGPU::S_AND_B64_term:
2591     case AMDGPU::S_MOV_B32_term:
2592     case AMDGPU::S_XOR_B32_term:
2593     case AMDGPU::S_OR_B32_term:
2594     case AMDGPU::S_ANDN2_B32_term:
2595     case AMDGPU::S_AND_B32_term:
2596       break;
2597     case AMDGPU::SI_IF:
2598     case AMDGPU::SI_ELSE:
2599     case AMDGPU::SI_KILL_I1_TERMINATOR:
2600     case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
2601       // FIXME: It's messy that these need to be considered here at all.
2602       return true;
2603     default:
2604       llvm_unreachable("unexpected non-branch terminator inst");
2605     }
2606 
2607     ++I;
2608   }
2609 
2610   if (I == E)
2611     return false;
2612 
2613   return analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify);
2614 }
2615 
2616 unsigned SIInstrInfo::removeBranch(MachineBasicBlock &MBB,
2617                                    int *BytesRemoved) const {
2618   unsigned Count = 0;
2619   unsigned RemovedSize = 0;
2620   for (MachineInstr &MI : llvm::make_early_inc_range(MBB.terminators())) {
2621     // Skip over artificial terminators when removing instructions.
2622     if (MI.isBranch() || MI.isReturn()) {
2623       RemovedSize += getInstSizeInBytes(MI);
2624       MI.eraseFromParent();
2625       ++Count;
2626     }
2627   }
2628 
2629   if (BytesRemoved)
2630     *BytesRemoved = RemovedSize;
2631 
2632   return Count;
2633 }
2634 
2635 // Copy the flags onto the implicit condition register operand.
2636 static void preserveCondRegFlags(MachineOperand &CondReg,
2637                                  const MachineOperand &OrigCond) {
2638   CondReg.setIsUndef(OrigCond.isUndef());
2639   CondReg.setIsKill(OrigCond.isKill());
2640 }
2641 
2642 unsigned SIInstrInfo::insertBranch(MachineBasicBlock &MBB,
2643                                    MachineBasicBlock *TBB,
2644                                    MachineBasicBlock *FBB,
2645                                    ArrayRef<MachineOperand> Cond,
2646                                    const DebugLoc &DL,
2647                                    int *BytesAdded) const {
2648   if (!FBB && Cond.empty()) {
2649     BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH))
2650       .addMBB(TBB);
2651     if (BytesAdded)
2652       *BytesAdded = ST.hasOffset3fBug() ? 8 : 4;
2653     return 1;
2654   }
2655 
2656   if(Cond.size() == 1 && Cond[0].isReg()) {
2657      BuildMI(&MBB, DL, get(AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO))
2658        .add(Cond[0])
2659        .addMBB(TBB);
2660      return 1;
2661   }
2662 
2663   assert(TBB && Cond[0].isImm());
2664 
2665   unsigned Opcode
2666     = getBranchOpcode(static_cast<BranchPredicate>(Cond[0].getImm()));
2667 
2668   if (!FBB) {
2669     Cond[1].isUndef();
2670     MachineInstr *CondBr =
2671       BuildMI(&MBB, DL, get(Opcode))
2672       .addMBB(TBB);
2673 
2674     // Copy the flags onto the implicit condition register operand.
2675     preserveCondRegFlags(CondBr->getOperand(1), Cond[1]);
2676     fixImplicitOperands(*CondBr);
2677 
2678     if (BytesAdded)
2679       *BytesAdded = ST.hasOffset3fBug() ? 8 : 4;
2680     return 1;
2681   }
2682 
2683   assert(TBB && FBB);
2684 
2685   MachineInstr *CondBr =
2686     BuildMI(&MBB, DL, get(Opcode))
2687     .addMBB(TBB);
2688   fixImplicitOperands(*CondBr);
2689   BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH))
2690     .addMBB(FBB);
2691 
2692   MachineOperand &CondReg = CondBr->getOperand(1);
2693   CondReg.setIsUndef(Cond[1].isUndef());
2694   CondReg.setIsKill(Cond[1].isKill());
2695 
2696   if (BytesAdded)
2697     *BytesAdded = ST.hasOffset3fBug() ? 16 : 8;
2698 
2699   return 2;
2700 }
2701 
2702 bool SIInstrInfo::reverseBranchCondition(
2703   SmallVectorImpl<MachineOperand> &Cond) const {
2704   if (Cond.size() != 2) {
2705     return true;
2706   }
2707 
2708   if (Cond[0].isImm()) {
2709     Cond[0].setImm(-Cond[0].getImm());
2710     return false;
2711   }
2712 
2713   return true;
2714 }
2715 
2716 bool SIInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
2717                                   ArrayRef<MachineOperand> Cond,
2718                                   Register DstReg, Register TrueReg,
2719                                   Register FalseReg, int &CondCycles,
2720                                   int &TrueCycles, int &FalseCycles) const {
2721   switch (Cond[0].getImm()) {
2722   case VCCNZ:
2723   case VCCZ: {
2724     const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2725     const TargetRegisterClass *RC = MRI.getRegClass(TrueReg);
2726     if (MRI.getRegClass(FalseReg) != RC)
2727       return false;
2728 
2729     int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32;
2730     CondCycles = TrueCycles = FalseCycles = NumInsts; // ???
2731 
2732     // Limit to equal cost for branch vs. N v_cndmask_b32s.
2733     return RI.hasVGPRs(RC) && NumInsts <= 6;
2734   }
2735   case SCC_TRUE:
2736   case SCC_FALSE: {
2737     // FIXME: We could insert for VGPRs if we could replace the original compare
2738     // with a vector one.
2739     const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2740     const TargetRegisterClass *RC = MRI.getRegClass(TrueReg);
2741     if (MRI.getRegClass(FalseReg) != RC)
2742       return false;
2743 
2744     int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32;
2745 
2746     // Multiples of 8 can do s_cselect_b64
2747     if (NumInsts % 2 == 0)
2748       NumInsts /= 2;
2749 
2750     CondCycles = TrueCycles = FalseCycles = NumInsts; // ???
2751     return RI.isSGPRClass(RC);
2752   }
2753   default:
2754     return false;
2755   }
2756 }
2757 
2758 void SIInstrInfo::insertSelect(MachineBasicBlock &MBB,
2759                                MachineBasicBlock::iterator I, const DebugLoc &DL,
2760                                Register DstReg, ArrayRef<MachineOperand> Cond,
2761                                Register TrueReg, Register FalseReg) const {
2762   BranchPredicate Pred = static_cast<BranchPredicate>(Cond[0].getImm());
2763   if (Pred == VCCZ || Pred == SCC_FALSE) {
2764     Pred = static_cast<BranchPredicate>(-Pred);
2765     std::swap(TrueReg, FalseReg);
2766   }
2767 
2768   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2769   const TargetRegisterClass *DstRC = MRI.getRegClass(DstReg);
2770   unsigned DstSize = RI.getRegSizeInBits(*DstRC);
2771 
2772   if (DstSize == 32) {
2773     MachineInstr *Select;
2774     if (Pred == SCC_TRUE) {
2775       Select = BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B32), DstReg)
2776         .addReg(TrueReg)
2777         .addReg(FalseReg);
2778     } else {
2779       // Instruction's operands are backwards from what is expected.
2780       Select = BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e32), DstReg)
2781         .addReg(FalseReg)
2782         .addReg(TrueReg);
2783     }
2784 
2785     preserveCondRegFlags(Select->getOperand(3), Cond[1]);
2786     return;
2787   }
2788 
2789   if (DstSize == 64 && Pred == SCC_TRUE) {
2790     MachineInstr *Select =
2791       BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), DstReg)
2792       .addReg(TrueReg)
2793       .addReg(FalseReg);
2794 
2795     preserveCondRegFlags(Select->getOperand(3), Cond[1]);
2796     return;
2797   }
2798 
2799   static const int16_t Sub0_15[] = {
2800     AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
2801     AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
2802     AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11,
2803     AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15,
2804   };
2805 
2806   static const int16_t Sub0_15_64[] = {
2807     AMDGPU::sub0_sub1, AMDGPU::sub2_sub3,
2808     AMDGPU::sub4_sub5, AMDGPU::sub6_sub7,
2809     AMDGPU::sub8_sub9, AMDGPU::sub10_sub11,
2810     AMDGPU::sub12_sub13, AMDGPU::sub14_sub15,
2811   };
2812 
2813   unsigned SelOp = AMDGPU::V_CNDMASK_B32_e32;
2814   const TargetRegisterClass *EltRC = &AMDGPU::VGPR_32RegClass;
2815   const int16_t *SubIndices = Sub0_15;
2816   int NElts = DstSize / 32;
2817 
2818   // 64-bit select is only available for SALU.
2819   // TODO: Split 96-bit into 64-bit and 32-bit, not 3x 32-bit.
2820   if (Pred == SCC_TRUE) {
2821     if (NElts % 2) {
2822       SelOp = AMDGPU::S_CSELECT_B32;
2823       EltRC = &AMDGPU::SGPR_32RegClass;
2824     } else {
2825       SelOp = AMDGPU::S_CSELECT_B64;
2826       EltRC = &AMDGPU::SGPR_64RegClass;
2827       SubIndices = Sub0_15_64;
2828       NElts /= 2;
2829     }
2830   }
2831 
2832   MachineInstrBuilder MIB = BuildMI(
2833     MBB, I, DL, get(AMDGPU::REG_SEQUENCE), DstReg);
2834 
2835   I = MIB->getIterator();
2836 
2837   SmallVector<Register, 8> Regs;
2838   for (int Idx = 0; Idx != NElts; ++Idx) {
2839     Register DstElt = MRI.createVirtualRegister(EltRC);
2840     Regs.push_back(DstElt);
2841 
2842     unsigned SubIdx = SubIndices[Idx];
2843 
2844     MachineInstr *Select;
2845     if (SelOp == AMDGPU::V_CNDMASK_B32_e32) {
2846       Select =
2847         BuildMI(MBB, I, DL, get(SelOp), DstElt)
2848         .addReg(FalseReg, 0, SubIdx)
2849         .addReg(TrueReg, 0, SubIdx);
2850     } else {
2851       Select =
2852         BuildMI(MBB, I, DL, get(SelOp), DstElt)
2853         .addReg(TrueReg, 0, SubIdx)
2854         .addReg(FalseReg, 0, SubIdx);
2855     }
2856 
2857     preserveCondRegFlags(Select->getOperand(3), Cond[1]);
2858     fixImplicitOperands(*Select);
2859 
2860     MIB.addReg(DstElt)
2861        .addImm(SubIdx);
2862   }
2863 }
2864 
2865 bool SIInstrInfo::isFoldableCopy(const MachineInstr &MI) {
2866   switch (MI.getOpcode()) {
2867   case AMDGPU::V_MOV_B32_e32:
2868   case AMDGPU::V_MOV_B32_e64:
2869   case AMDGPU::V_MOV_B64_PSEUDO:
2870   case AMDGPU::V_MOV_B64_e32:
2871   case AMDGPU::V_MOV_B64_e64:
2872   case AMDGPU::S_MOV_B32:
2873   case AMDGPU::S_MOV_B64:
2874   case AMDGPU::COPY:
2875   case AMDGPU::V_ACCVGPR_WRITE_B32_e64:
2876   case AMDGPU::V_ACCVGPR_READ_B32_e64:
2877   case AMDGPU::V_ACCVGPR_MOV_B32:
2878     return true;
2879   default:
2880     return false;
2881   }
2882 }
2883 
2884 unsigned SIInstrInfo::getAddressSpaceForPseudoSourceKind(
2885     unsigned Kind) const {
2886   switch(Kind) {
2887   case PseudoSourceValue::Stack:
2888   case PseudoSourceValue::FixedStack:
2889     return AMDGPUAS::PRIVATE_ADDRESS;
2890   case PseudoSourceValue::ConstantPool:
2891   case PseudoSourceValue::GOT:
2892   case PseudoSourceValue::JumpTable:
2893   case PseudoSourceValue::GlobalValueCallEntry:
2894   case PseudoSourceValue::ExternalSymbolCallEntry:
2895   case PseudoSourceValue::TargetCustom:
2896     return AMDGPUAS::CONSTANT_ADDRESS;
2897   }
2898   return AMDGPUAS::FLAT_ADDRESS;
2899 }
2900 
2901 static void removeModOperands(MachineInstr &MI) {
2902   unsigned Opc = MI.getOpcode();
2903   int Src0ModIdx = AMDGPU::getNamedOperandIdx(Opc,
2904                                               AMDGPU::OpName::src0_modifiers);
2905   int Src1ModIdx = AMDGPU::getNamedOperandIdx(Opc,
2906                                               AMDGPU::OpName::src1_modifiers);
2907   int Src2ModIdx = AMDGPU::getNamedOperandIdx(Opc,
2908                                               AMDGPU::OpName::src2_modifiers);
2909 
2910   MI.removeOperand(Src2ModIdx);
2911   MI.removeOperand(Src1ModIdx);
2912   MI.removeOperand(Src0ModIdx);
2913 }
2914 
2915 bool SIInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2916                                 Register Reg, MachineRegisterInfo *MRI) const {
2917   if (!MRI->hasOneNonDBGUse(Reg))
2918     return false;
2919 
2920   switch (DefMI.getOpcode()) {
2921   default:
2922     return false;
2923   case AMDGPU::S_MOV_B64:
2924     // TODO: We could fold 64-bit immediates, but this get complicated
2925     // when there are sub-registers.
2926     return false;
2927 
2928   case AMDGPU::V_MOV_B32_e32:
2929   case AMDGPU::S_MOV_B32:
2930   case AMDGPU::V_ACCVGPR_WRITE_B32_e64:
2931     break;
2932   }
2933 
2934   const MachineOperand *ImmOp = getNamedOperand(DefMI, AMDGPU::OpName::src0);
2935   assert(ImmOp);
2936   // FIXME: We could handle FrameIndex values here.
2937   if (!ImmOp->isImm())
2938     return false;
2939 
2940   unsigned Opc = UseMI.getOpcode();
2941   if (Opc == AMDGPU::COPY) {
2942     Register DstReg = UseMI.getOperand(0).getReg();
2943     bool Is16Bit = getOpSize(UseMI, 0) == 2;
2944     bool isVGPRCopy = RI.isVGPR(*MRI, DstReg);
2945     unsigned NewOpc = isVGPRCopy ? AMDGPU::V_MOV_B32_e32 : AMDGPU::S_MOV_B32;
2946     APInt Imm(32, ImmOp->getImm());
2947 
2948     if (UseMI.getOperand(1).getSubReg() == AMDGPU::hi16)
2949       Imm = Imm.ashr(16);
2950 
2951     if (RI.isAGPR(*MRI, DstReg)) {
2952       if (!isInlineConstant(Imm))
2953         return false;
2954       NewOpc = AMDGPU::V_ACCVGPR_WRITE_B32_e64;
2955     }
2956 
2957     if (Is16Bit) {
2958       if (isVGPRCopy)
2959         return false; // Do not clobber vgpr_hi16
2960 
2961       if (DstReg.isVirtual() && UseMI.getOperand(0).getSubReg() != AMDGPU::lo16)
2962         return false;
2963 
2964       UseMI.getOperand(0).setSubReg(0);
2965       if (DstReg.isPhysical()) {
2966         DstReg = RI.get32BitRegister(DstReg);
2967         UseMI.getOperand(0).setReg(DstReg);
2968       }
2969       assert(UseMI.getOperand(1).getReg().isVirtual());
2970     }
2971 
2972     UseMI.setDesc(get(NewOpc));
2973     UseMI.getOperand(1).ChangeToImmediate(Imm.getSExtValue());
2974     UseMI.addImplicitDefUseOperands(*UseMI.getParent()->getParent());
2975     return true;
2976   }
2977 
2978   if (Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 ||
2979       Opc == AMDGPU::V_MAD_F16_e64 || Opc == AMDGPU::V_MAC_F16_e64 ||
2980       Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64 ||
2981       Opc == AMDGPU::V_FMA_F16_e64 || Opc == AMDGPU::V_FMAC_F16_e64) {
2982     // Don't fold if we are using source or output modifiers. The new VOP2
2983     // instructions don't have them.
2984     if (hasAnyModifiersSet(UseMI))
2985       return false;
2986 
2987     // If this is a free constant, there's no reason to do this.
2988     // TODO: We could fold this here instead of letting SIFoldOperands do it
2989     // later.
2990     MachineOperand *Src0 = getNamedOperand(UseMI, AMDGPU::OpName::src0);
2991 
2992     // Any src operand can be used for the legality check.
2993     if (isInlineConstant(UseMI, *Src0, *ImmOp))
2994       return false;
2995 
2996     bool IsF32 = Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 ||
2997                  Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64;
2998     bool IsFMA = Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64 ||
2999                  Opc == AMDGPU::V_FMA_F16_e64 || Opc == AMDGPU::V_FMAC_F16_e64;
3000     MachineOperand *Src1 = getNamedOperand(UseMI, AMDGPU::OpName::src1);
3001     MachineOperand *Src2 = getNamedOperand(UseMI, AMDGPU::OpName::src2);
3002 
3003     // Multiplied part is the constant: Use v_madmk_{f16, f32}.
3004     // We should only expect these to be on src0 due to canonicalization.
3005     if (Src0->isReg() && Src0->getReg() == Reg) {
3006       if (!Src1->isReg() || RI.isSGPRClass(MRI->getRegClass(Src1->getReg())))
3007         return false;
3008 
3009       if (!Src2->isReg() || RI.isSGPRClass(MRI->getRegClass(Src2->getReg())))
3010         return false;
3011 
3012       unsigned NewOpc =
3013         IsFMA ? (IsF32 ? AMDGPU::V_FMAMK_F32 : AMDGPU::V_FMAMK_F16)
3014               : (IsF32 ? AMDGPU::V_MADMK_F32 : AMDGPU::V_MADMK_F16);
3015       if (pseudoToMCOpcode(NewOpc) == -1)
3016         return false;
3017 
3018       // We need to swap operands 0 and 1 since madmk constant is at operand 1.
3019 
3020       const int64_t Imm = ImmOp->getImm();
3021 
3022       // FIXME: This would be a lot easier if we could return a new instruction
3023       // instead of having to modify in place.
3024 
3025       // Remove these first since they are at the end.
3026       UseMI.removeOperand(
3027           AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod));
3028       UseMI.removeOperand(
3029           AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp));
3030 
3031       Register Src1Reg = Src1->getReg();
3032       unsigned Src1SubReg = Src1->getSubReg();
3033       Src0->setReg(Src1Reg);
3034       Src0->setSubReg(Src1SubReg);
3035       Src0->setIsKill(Src1->isKill());
3036 
3037       if (Opc == AMDGPU::V_MAC_F32_e64 ||
3038           Opc == AMDGPU::V_MAC_F16_e64 ||
3039           Opc == AMDGPU::V_FMAC_F32_e64 ||
3040           Opc == AMDGPU::V_FMAC_F16_e64)
3041         UseMI.untieRegOperand(
3042             AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
3043 
3044       Src1->ChangeToImmediate(Imm);
3045 
3046       removeModOperands(UseMI);
3047       UseMI.setDesc(get(NewOpc));
3048 
3049       bool DeleteDef = MRI->use_nodbg_empty(Reg);
3050       if (DeleteDef)
3051         DefMI.eraseFromParent();
3052 
3053       return true;
3054     }
3055 
3056     // Added part is the constant: Use v_madak_{f16, f32}.
3057     if (Src2->isReg() && Src2->getReg() == Reg) {
3058       // Not allowed to use constant bus for another operand.
3059       // We can however allow an inline immediate as src0.
3060       bool Src0Inlined = false;
3061       if (Src0->isReg()) {
3062         // Try to inline constant if possible.
3063         // If the Def moves immediate and the use is single
3064         // We are saving VGPR here.
3065         MachineInstr *Def = MRI->getUniqueVRegDef(Src0->getReg());
3066         if (Def && Def->isMoveImmediate() &&
3067           isInlineConstant(Def->getOperand(1)) &&
3068           MRI->hasOneUse(Src0->getReg())) {
3069           Src0->ChangeToImmediate(Def->getOperand(1).getImm());
3070           Src0Inlined = true;
3071         } else if ((Src0->getReg().isPhysical() &&
3072                     (ST.getConstantBusLimit(Opc) <= 1 &&
3073                      RI.isSGPRClass(RI.getPhysRegClass(Src0->getReg())))) ||
3074                    (Src0->getReg().isVirtual() &&
3075                     (ST.getConstantBusLimit(Opc) <= 1 &&
3076                      RI.isSGPRClass(MRI->getRegClass(Src0->getReg())))))
3077           return false;
3078           // VGPR is okay as Src0 - fallthrough
3079       }
3080 
3081       if (Src1->isReg() && !Src0Inlined ) {
3082         // We have one slot for inlinable constant so far - try to fill it
3083         MachineInstr *Def = MRI->getUniqueVRegDef(Src1->getReg());
3084         if (Def && Def->isMoveImmediate() &&
3085             isInlineConstant(Def->getOperand(1)) &&
3086             MRI->hasOneUse(Src1->getReg()) &&
3087             commuteInstruction(UseMI)) {
3088             Src0->ChangeToImmediate(Def->getOperand(1).getImm());
3089         } else if ((Src1->getReg().isPhysical() &&
3090                     RI.isSGPRClass(RI.getPhysRegClass(Src1->getReg()))) ||
3091                    (Src1->getReg().isVirtual() &&
3092                     RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))))
3093           return false;
3094           // VGPR is okay as Src1 - fallthrough
3095       }
3096 
3097       unsigned NewOpc =
3098         IsFMA ? (IsF32 ? AMDGPU::V_FMAAK_F32 : AMDGPU::V_FMAAK_F16)
3099               : (IsF32 ? AMDGPU::V_MADAK_F32 : AMDGPU::V_MADAK_F16);
3100       if (pseudoToMCOpcode(NewOpc) == -1)
3101         return false;
3102 
3103       const int64_t Imm = ImmOp->getImm();
3104 
3105       // FIXME: This would be a lot easier if we could return a new instruction
3106       // instead of having to modify in place.
3107 
3108       // Remove these first since they are at the end.
3109       UseMI.removeOperand(
3110           AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod));
3111       UseMI.removeOperand(
3112           AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp));
3113 
3114       if (Opc == AMDGPU::V_MAC_F32_e64 ||
3115           Opc == AMDGPU::V_MAC_F16_e64 ||
3116           Opc == AMDGPU::V_FMAC_F32_e64 ||
3117           Opc == AMDGPU::V_FMAC_F16_e64)
3118         UseMI.untieRegOperand(
3119             AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
3120 
3121       // ChangingToImmediate adds Src2 back to the instruction.
3122       Src2->ChangeToImmediate(Imm);
3123 
3124       // These come before src2.
3125       removeModOperands(UseMI);
3126       UseMI.setDesc(get(NewOpc));
3127       // It might happen that UseMI was commuted
3128       // and we now have SGPR as SRC1. If so 2 inlined
3129       // constant and SGPR are illegal.
3130       legalizeOperands(UseMI);
3131 
3132       bool DeleteDef = MRI->use_nodbg_empty(Reg);
3133       if (DeleteDef)
3134         DefMI.eraseFromParent();
3135 
3136       return true;
3137     }
3138   }
3139 
3140   return false;
3141 }
3142 
3143 static bool
3144 memOpsHaveSameBaseOperands(ArrayRef<const MachineOperand *> BaseOps1,
3145                            ArrayRef<const MachineOperand *> BaseOps2) {
3146   if (BaseOps1.size() != BaseOps2.size())
3147     return false;
3148   for (size_t I = 0, E = BaseOps1.size(); I < E; ++I) {
3149     if (!BaseOps1[I]->isIdenticalTo(*BaseOps2[I]))
3150       return false;
3151   }
3152   return true;
3153 }
3154 
3155 static bool offsetsDoNotOverlap(int WidthA, int OffsetA,
3156                                 int WidthB, int OffsetB) {
3157   int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
3158   int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
3159   int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
3160   return LowOffset + LowWidth <= HighOffset;
3161 }
3162 
3163 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(const MachineInstr &MIa,
3164                                                const MachineInstr &MIb) const {
3165   SmallVector<const MachineOperand *, 4> BaseOps0, BaseOps1;
3166   int64_t Offset0, Offset1;
3167   unsigned Dummy0, Dummy1;
3168   bool Offset0IsScalable, Offset1IsScalable;
3169   if (!getMemOperandsWithOffsetWidth(MIa, BaseOps0, Offset0, Offset0IsScalable,
3170                                      Dummy0, &RI) ||
3171       !getMemOperandsWithOffsetWidth(MIb, BaseOps1, Offset1, Offset1IsScalable,
3172                                      Dummy1, &RI))
3173     return false;
3174 
3175   if (!memOpsHaveSameBaseOperands(BaseOps0, BaseOps1))
3176     return false;
3177 
3178   if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) {
3179     // FIXME: Handle ds_read2 / ds_write2.
3180     return false;
3181   }
3182   unsigned Width0 = MIa.memoperands().front()->getSize();
3183   unsigned Width1 = MIb.memoperands().front()->getSize();
3184   return offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1);
3185 }
3186 
3187 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
3188                                                   const MachineInstr &MIb) const {
3189   assert(MIa.mayLoadOrStore() &&
3190          "MIa must load from or modify a memory location");
3191   assert(MIb.mayLoadOrStore() &&
3192          "MIb must load from or modify a memory location");
3193 
3194   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects())
3195     return false;
3196 
3197   // XXX - Can we relax this between address spaces?
3198   if (MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
3199     return false;
3200 
3201   // TODO: Should we check the address space from the MachineMemOperand? That
3202   // would allow us to distinguish objects we know don't alias based on the
3203   // underlying address space, even if it was lowered to a different one,
3204   // e.g. private accesses lowered to use MUBUF instructions on a scratch
3205   // buffer.
3206   if (isDS(MIa)) {
3207     if (isDS(MIb))
3208       return checkInstOffsetsDoNotOverlap(MIa, MIb);
3209 
3210     return !isFLAT(MIb) || isSegmentSpecificFLAT(MIb);
3211   }
3212 
3213   if (isMUBUF(MIa) || isMTBUF(MIa)) {
3214     if (isMUBUF(MIb) || isMTBUF(MIb))
3215       return checkInstOffsetsDoNotOverlap(MIa, MIb);
3216 
3217     return !isFLAT(MIb) && !isSMRD(MIb);
3218   }
3219 
3220   if (isSMRD(MIa)) {
3221     if (isSMRD(MIb))
3222       return checkInstOffsetsDoNotOverlap(MIa, MIb);
3223 
3224     return !isFLAT(MIb) && !isMUBUF(MIb) && !isMTBUF(MIb);
3225   }
3226 
3227   if (isFLAT(MIa)) {
3228     if (isFLAT(MIb))
3229       return checkInstOffsetsDoNotOverlap(MIa, MIb);
3230 
3231     return false;
3232   }
3233 
3234   return false;
3235 }
3236 
3237 static bool getFoldableImm(Register Reg, const MachineRegisterInfo &MRI,
3238                            int64_t &Imm, MachineInstr **DefMI = nullptr) {
3239   if (Reg.isPhysical())
3240     return false;
3241   auto *Def = MRI.getUniqueVRegDef(Reg);
3242   if (Def && SIInstrInfo::isFoldableCopy(*Def) && Def->getOperand(1).isImm()) {
3243     Imm = Def->getOperand(1).getImm();
3244     if (DefMI)
3245       *DefMI = Def;
3246     return true;
3247   }
3248   return false;
3249 }
3250 
3251 static bool getFoldableImm(const MachineOperand *MO, int64_t &Imm,
3252                            MachineInstr **DefMI = nullptr) {
3253   if (!MO->isReg())
3254     return false;
3255   const MachineFunction *MF = MO->getParent()->getParent()->getParent();
3256   const MachineRegisterInfo &MRI = MF->getRegInfo();
3257   return getFoldableImm(MO->getReg(), MRI, Imm, DefMI);
3258 }
3259 
3260 static void updateLiveVariables(LiveVariables *LV, MachineInstr &MI,
3261                                 MachineInstr &NewMI) {
3262   if (LV) {
3263     unsigned NumOps = MI.getNumOperands();
3264     for (unsigned I = 1; I < NumOps; ++I) {
3265       MachineOperand &Op = MI.getOperand(I);
3266       if (Op.isReg() && Op.isKill())
3267         LV->replaceKillInstruction(Op.getReg(), MI, NewMI);
3268     }
3269   }
3270 }
3271 
3272 MachineInstr *SIInstrInfo::convertToThreeAddress(MachineInstr &MI,
3273                                                  LiveVariables *LV,
3274                                                  LiveIntervals *LIS) const {
3275   MachineBasicBlock &MBB = *MI.getParent();
3276   unsigned Opc = MI.getOpcode();
3277 
3278   // Handle MFMA.
3279   int NewMFMAOpc = AMDGPU::getMFMAEarlyClobberOp(Opc);
3280   if (NewMFMAOpc != -1) {
3281     MachineInstrBuilder MIB =
3282         BuildMI(MBB, MI, MI.getDebugLoc(), get(NewMFMAOpc));
3283     for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I)
3284       MIB.add(MI.getOperand(I));
3285     updateLiveVariables(LV, MI, *MIB);
3286     if (LIS)
3287       LIS->ReplaceMachineInstrInMaps(MI, *MIB);
3288     return MIB;
3289   }
3290 
3291   // Handle MAC/FMAC.
3292   bool IsF16 = Opc == AMDGPU::V_MAC_F16_e32 || Opc == AMDGPU::V_MAC_F16_e64 ||
3293                Opc == AMDGPU::V_FMAC_F16_e32 || Opc == AMDGPU::V_FMAC_F16_e64;
3294   bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F32_e64 ||
3295                Opc == AMDGPU::V_FMAC_LEGACY_F32_e32 ||
3296                Opc == AMDGPU::V_FMAC_LEGACY_F32_e64 ||
3297                Opc == AMDGPU::V_FMAC_F16_e32 || Opc == AMDGPU::V_FMAC_F16_e64 ||
3298                Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64;
3299   bool IsF64 = Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64;
3300   bool IsLegacy = Opc == AMDGPU::V_MAC_LEGACY_F32_e32 ||
3301                   Opc == AMDGPU::V_MAC_LEGACY_F32_e64 ||
3302                   Opc == AMDGPU::V_FMAC_LEGACY_F32_e32 ||
3303                   Opc == AMDGPU::V_FMAC_LEGACY_F32_e64;
3304   bool Src0Literal = false;
3305 
3306   switch (Opc) {
3307   default:
3308     return nullptr;
3309   case AMDGPU::V_MAC_F16_e64:
3310   case AMDGPU::V_FMAC_F16_e64:
3311   case AMDGPU::V_MAC_F32_e64:
3312   case AMDGPU::V_MAC_LEGACY_F32_e64:
3313   case AMDGPU::V_FMAC_F32_e64:
3314   case AMDGPU::V_FMAC_LEGACY_F32_e64:
3315   case AMDGPU::V_FMAC_F64_e64:
3316     break;
3317   case AMDGPU::V_MAC_F16_e32:
3318   case AMDGPU::V_FMAC_F16_e32:
3319   case AMDGPU::V_MAC_F32_e32:
3320   case AMDGPU::V_MAC_LEGACY_F32_e32:
3321   case AMDGPU::V_FMAC_F32_e32:
3322   case AMDGPU::V_FMAC_LEGACY_F32_e32:
3323   case AMDGPU::V_FMAC_F64_e32: {
3324     int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
3325                                              AMDGPU::OpName::src0);
3326     const MachineOperand *Src0 = &MI.getOperand(Src0Idx);
3327     if (!Src0->isReg() && !Src0->isImm())
3328       return nullptr;
3329 
3330     if (Src0->isImm() && !isInlineConstant(MI, Src0Idx, *Src0))
3331       Src0Literal = true;
3332 
3333     break;
3334   }
3335   }
3336 
3337   MachineInstrBuilder MIB;
3338   const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
3339   const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0);
3340   const MachineOperand *Src0Mods =
3341     getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
3342   const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
3343   const MachineOperand *Src1Mods =
3344     getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
3345   const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
3346   const MachineOperand *Src2Mods =
3347       getNamedOperand(MI, AMDGPU::OpName::src2_modifiers);
3348   const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp);
3349   const MachineOperand *Omod = getNamedOperand(MI, AMDGPU::OpName::omod);
3350 
3351   if (!Src0Mods && !Src1Mods && !Src2Mods && !Clamp && !Omod && !IsF64 &&
3352       !IsLegacy &&
3353       // If we have an SGPR input, we will violate the constant bus restriction.
3354       (ST.getConstantBusLimit(Opc) > 1 || !Src0->isReg() ||
3355        !RI.isSGPRReg(MBB.getParent()->getRegInfo(), Src0->getReg()))) {
3356     MachineInstr *DefMI;
3357     const auto killDef = [&DefMI, &MBB, this]() -> void {
3358       const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3359       // The only user is the instruction which will be killed.
3360       if (!MRI.hasOneNonDBGUse(DefMI->getOperand(0).getReg()))
3361         return;
3362       // We cannot just remove the DefMI here, calling pass will crash.
3363       DefMI->setDesc(get(AMDGPU::IMPLICIT_DEF));
3364       for (unsigned I = DefMI->getNumOperands() - 1; I != 0; --I)
3365         DefMI->removeOperand(I);
3366     };
3367 
3368     int64_t Imm;
3369     if (!Src0Literal && getFoldableImm(Src2, Imm, &DefMI)) {
3370       unsigned NewOpc =
3371           IsFMA ? (IsF16 ? AMDGPU::V_FMAAK_F16 : AMDGPU::V_FMAAK_F32)
3372                 : (IsF16 ? AMDGPU::V_MADAK_F16 : AMDGPU::V_MADAK_F32);
3373       if (pseudoToMCOpcode(NewOpc) != -1) {
3374         MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
3375                   .add(*Dst)
3376                   .add(*Src0)
3377                   .add(*Src1)
3378                   .addImm(Imm);
3379         updateLiveVariables(LV, MI, *MIB);
3380         if (LIS)
3381           LIS->ReplaceMachineInstrInMaps(MI, *MIB);
3382         killDef();
3383         return MIB;
3384       }
3385     }
3386     unsigned NewOpc = IsFMA
3387                           ? (IsF16 ? AMDGPU::V_FMAMK_F16 : AMDGPU::V_FMAMK_F32)
3388                           : (IsF16 ? AMDGPU::V_MADMK_F16 : AMDGPU::V_MADMK_F32);
3389     if (!Src0Literal && getFoldableImm(Src1, Imm, &DefMI)) {
3390       if (pseudoToMCOpcode(NewOpc) != -1) {
3391         MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
3392                   .add(*Dst)
3393                   .add(*Src0)
3394                   .addImm(Imm)
3395                   .add(*Src2);
3396         updateLiveVariables(LV, MI, *MIB);
3397         if (LIS)
3398           LIS->ReplaceMachineInstrInMaps(MI, *MIB);
3399         killDef();
3400         return MIB;
3401       }
3402     }
3403     if (Src0Literal || getFoldableImm(Src0, Imm, &DefMI)) {
3404       if (Src0Literal) {
3405         Imm = Src0->getImm();
3406         DefMI = nullptr;
3407       }
3408       if (pseudoToMCOpcode(NewOpc) != -1 &&
3409           isOperandLegal(
3410               MI, AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::src0),
3411               Src1)) {
3412         MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
3413                   .add(*Dst)
3414                   .add(*Src1)
3415                   .addImm(Imm)
3416                   .add(*Src2);
3417         updateLiveVariables(LV, MI, *MIB);
3418         if (LIS)
3419           LIS->ReplaceMachineInstrInMaps(MI, *MIB);
3420         if (DefMI)
3421           killDef();
3422         return MIB;
3423       }
3424     }
3425   }
3426 
3427   // VOP2 mac/fmac with a literal operand cannot be converted to VOP3 mad/fma
3428   // because VOP3 does not allow a literal operand.
3429   // TODO: Remove this restriction for GFX10.
3430   if (Src0Literal)
3431     return nullptr;
3432 
3433   unsigned NewOpc = IsFMA ? IsF16 ? AMDGPU::V_FMA_F16_gfx9_e64
3434                                   : IsF64 ? AMDGPU::V_FMA_F64_e64
3435                                           : IsLegacy
3436                                                 ? AMDGPU::V_FMA_LEGACY_F32_e64
3437                                                 : AMDGPU::V_FMA_F32_e64
3438                           : IsF16 ? AMDGPU::V_MAD_F16_e64
3439                                   : IsLegacy ? AMDGPU::V_MAD_LEGACY_F32_e64
3440                                              : AMDGPU::V_MAD_F32_e64;
3441   if (pseudoToMCOpcode(NewOpc) == -1)
3442     return nullptr;
3443 
3444   MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
3445             .add(*Dst)
3446             .addImm(Src0Mods ? Src0Mods->getImm() : 0)
3447             .add(*Src0)
3448             .addImm(Src1Mods ? Src1Mods->getImm() : 0)
3449             .add(*Src1)
3450             .addImm(Src2Mods ? Src2Mods->getImm() : 0)
3451             .add(*Src2)
3452             .addImm(Clamp ? Clamp->getImm() : 0)
3453             .addImm(Omod ? Omod->getImm() : 0);
3454   updateLiveVariables(LV, MI, *MIB);
3455   if (LIS)
3456     LIS->ReplaceMachineInstrInMaps(MI, *MIB);
3457   return MIB;
3458 }
3459 
3460 // It's not generally safe to move VALU instructions across these since it will
3461 // start using the register as a base index rather than directly.
3462 // XXX - Why isn't hasSideEffects sufficient for these?
3463 static bool changesVGPRIndexingMode(const MachineInstr &MI) {
3464   switch (MI.getOpcode()) {
3465   case AMDGPU::S_SET_GPR_IDX_ON:
3466   case AMDGPU::S_SET_GPR_IDX_MODE:
3467   case AMDGPU::S_SET_GPR_IDX_OFF:
3468     return true;
3469   default:
3470     return false;
3471   }
3472 }
3473 
3474 bool SIInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
3475                                        const MachineBasicBlock *MBB,
3476                                        const MachineFunction &MF) const {
3477   // Skipping the check for SP writes in the base implementation. The reason it
3478   // was added was apparently due to compile time concerns.
3479   //
3480   // TODO: Do we really want this barrier? It triggers unnecessary hazard nops
3481   // but is probably avoidable.
3482 
3483   // Copied from base implementation.
3484   // Terminators and labels can't be scheduled around.
3485   if (MI.isTerminator() || MI.isPosition())
3486     return true;
3487 
3488   // INLINEASM_BR can jump to another block
3489   if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
3490     return true;
3491 
3492   // Target-independent instructions do not have an implicit-use of EXEC, even
3493   // when they operate on VGPRs. Treating EXEC modifications as scheduling
3494   // boundaries prevents incorrect movements of such instructions.
3495   return MI.modifiesRegister(AMDGPU::EXEC, &RI) ||
3496          MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
3497          MI.getOpcode() == AMDGPU::S_SETREG_B32 ||
3498          changesVGPRIndexingMode(MI);
3499 }
3500 
3501 bool SIInstrInfo::isAlwaysGDS(uint16_t Opcode) const {
3502   return Opcode == AMDGPU::DS_ORDERED_COUNT ||
3503          Opcode == AMDGPU::DS_GWS_INIT ||
3504          Opcode == AMDGPU::DS_GWS_SEMA_V ||
3505          Opcode == AMDGPU::DS_GWS_SEMA_BR ||
3506          Opcode == AMDGPU::DS_GWS_SEMA_P ||
3507          Opcode == AMDGPU::DS_GWS_SEMA_RELEASE_ALL ||
3508          Opcode == AMDGPU::DS_GWS_BARRIER;
3509 }
3510 
3511 bool SIInstrInfo::modifiesModeRegister(const MachineInstr &MI) {
3512   // Skip the full operand and register alias search modifiesRegister
3513   // does. There's only a handful of instructions that touch this, it's only an
3514   // implicit def, and doesn't alias any other registers.
3515   if (const MCPhysReg *ImpDef = MI.getDesc().getImplicitDefs()) {
3516     for (; ImpDef && *ImpDef; ++ImpDef) {
3517       if (*ImpDef == AMDGPU::MODE)
3518         return true;
3519     }
3520   }
3521 
3522   return false;
3523 }
3524 
3525 bool SIInstrInfo::hasUnwantedEffectsWhenEXECEmpty(const MachineInstr &MI) const {
3526   unsigned Opcode = MI.getOpcode();
3527 
3528   if (MI.mayStore() && isSMRD(MI))
3529     return true; // scalar store or atomic
3530 
3531   // This will terminate the function when other lanes may need to continue.
3532   if (MI.isReturn())
3533     return true;
3534 
3535   // These instructions cause shader I/O that may cause hardware lockups
3536   // when executed with an empty EXEC mask.
3537   //
3538   // Note: exp with VM = DONE = 0 is automatically skipped by hardware when
3539   //       EXEC = 0, but checking for that case here seems not worth it
3540   //       given the typical code patterns.
3541   if (Opcode == AMDGPU::S_SENDMSG || Opcode == AMDGPU::S_SENDMSGHALT ||
3542       isEXP(Opcode) ||
3543       Opcode == AMDGPU::DS_ORDERED_COUNT || Opcode == AMDGPU::S_TRAP ||
3544       Opcode == AMDGPU::DS_GWS_INIT || Opcode == AMDGPU::DS_GWS_BARRIER)
3545     return true;
3546 
3547   if (MI.isCall() || MI.isInlineAsm())
3548     return true; // conservative assumption
3549 
3550   // A mode change is a scalar operation that influences vector instructions.
3551   if (modifiesModeRegister(MI))
3552     return true;
3553 
3554   // These are like SALU instructions in terms of effects, so it's questionable
3555   // whether we should return true for those.
3556   //
3557   // However, executing them with EXEC = 0 causes them to operate on undefined
3558   // data, which we avoid by returning true here.
3559   if (Opcode == AMDGPU::V_READFIRSTLANE_B32 ||
3560       Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32)
3561     return true;
3562 
3563   return false;
3564 }
3565 
3566 bool SIInstrInfo::mayReadEXEC(const MachineRegisterInfo &MRI,
3567                               const MachineInstr &MI) const {
3568   if (MI.isMetaInstruction())
3569     return false;
3570 
3571   // This won't read exec if this is an SGPR->SGPR copy.
3572   if (MI.isCopyLike()) {
3573     if (!RI.isSGPRReg(MRI, MI.getOperand(0).getReg()))
3574       return true;
3575 
3576     // Make sure this isn't copying exec as a normal operand
3577     return MI.readsRegister(AMDGPU::EXEC, &RI);
3578   }
3579 
3580   // Make a conservative assumption about the callee.
3581   if (MI.isCall())
3582     return true;
3583 
3584   // Be conservative with any unhandled generic opcodes.
3585   if (!isTargetSpecificOpcode(MI.getOpcode()))
3586     return true;
3587 
3588   return !isSALU(MI) || MI.readsRegister(AMDGPU::EXEC, &RI);
3589 }
3590 
3591 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const {
3592   switch (Imm.getBitWidth()) {
3593   case 1: // This likely will be a condition code mask.
3594     return true;
3595 
3596   case 32:
3597     return AMDGPU::isInlinableLiteral32(Imm.getSExtValue(),
3598                                         ST.hasInv2PiInlineImm());
3599   case 64:
3600     return AMDGPU::isInlinableLiteral64(Imm.getSExtValue(),
3601                                         ST.hasInv2PiInlineImm());
3602   case 16:
3603     return ST.has16BitInsts() &&
3604            AMDGPU::isInlinableLiteral16(Imm.getSExtValue(),
3605                                         ST.hasInv2PiInlineImm());
3606   default:
3607     llvm_unreachable("invalid bitwidth");
3608   }
3609 }
3610 
3611 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO,
3612                                    uint8_t OperandType) const {
3613   if (!MO.isImm() ||
3614       OperandType < AMDGPU::OPERAND_SRC_FIRST ||
3615       OperandType > AMDGPU::OPERAND_SRC_LAST)
3616     return false;
3617 
3618   // MachineOperand provides no way to tell the true operand size, since it only
3619   // records a 64-bit value. We need to know the size to determine if a 32-bit
3620   // floating point immediate bit pattern is legal for an integer immediate. It
3621   // would be for any 32-bit integer operand, but would not be for a 64-bit one.
3622 
3623   int64_t Imm = MO.getImm();
3624   switch (OperandType) {
3625   case AMDGPU::OPERAND_REG_IMM_INT32:
3626   case AMDGPU::OPERAND_REG_IMM_FP32:
3627   case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED:
3628   case AMDGPU::OPERAND_REG_INLINE_C_INT32:
3629   case AMDGPU::OPERAND_REG_INLINE_C_FP32:
3630   case AMDGPU::OPERAND_REG_IMM_V2FP32:
3631   case AMDGPU::OPERAND_REG_INLINE_C_V2FP32:
3632   case AMDGPU::OPERAND_REG_IMM_V2INT32:
3633   case AMDGPU::OPERAND_REG_INLINE_C_V2INT32:
3634   case AMDGPU::OPERAND_REG_INLINE_AC_INT32:
3635   case AMDGPU::OPERAND_REG_INLINE_AC_FP32: {
3636     int32_t Trunc = static_cast<int32_t>(Imm);
3637     return AMDGPU::isInlinableLiteral32(Trunc, ST.hasInv2PiInlineImm());
3638   }
3639   case AMDGPU::OPERAND_REG_IMM_INT64:
3640   case AMDGPU::OPERAND_REG_IMM_FP64:
3641   case AMDGPU::OPERAND_REG_INLINE_C_INT64:
3642   case AMDGPU::OPERAND_REG_INLINE_C_FP64:
3643   case AMDGPU::OPERAND_REG_INLINE_AC_FP64:
3644     return AMDGPU::isInlinableLiteral64(MO.getImm(),
3645                                         ST.hasInv2PiInlineImm());
3646   case AMDGPU::OPERAND_REG_IMM_INT16:
3647   case AMDGPU::OPERAND_REG_INLINE_C_INT16:
3648   case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
3649     // We would expect inline immediates to not be concerned with an integer/fp
3650     // distinction. However, in the case of 16-bit integer operations, the
3651     // "floating point" values appear to not work. It seems read the low 16-bits
3652     // of 32-bit immediates, which happens to always work for the integer
3653     // values.
3654     //
3655     // See llvm bugzilla 46302.
3656     //
3657     // TODO: Theoretically we could use op-sel to use the high bits of the
3658     // 32-bit FP values.
3659     return AMDGPU::isInlinableIntLiteral(Imm);
3660   case AMDGPU::OPERAND_REG_IMM_V2INT16:
3661   case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
3662   case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16:
3663     // This suffers the same problem as the scalar 16-bit cases.
3664     return AMDGPU::isInlinableIntLiteralV216(Imm);
3665   case AMDGPU::OPERAND_REG_IMM_FP16:
3666   case AMDGPU::OPERAND_REG_IMM_FP16_DEFERRED:
3667   case AMDGPU::OPERAND_REG_INLINE_C_FP16:
3668   case AMDGPU::OPERAND_REG_INLINE_AC_FP16: {
3669     if (isInt<16>(Imm) || isUInt<16>(Imm)) {
3670       // A few special case instructions have 16-bit operands on subtargets
3671       // where 16-bit instructions are not legal.
3672       // TODO: Do the 32-bit immediates work? We shouldn't really need to handle
3673       // constants in these cases
3674       int16_t Trunc = static_cast<int16_t>(Imm);
3675       return ST.has16BitInsts() &&
3676              AMDGPU::isInlinableLiteral16(Trunc, ST.hasInv2PiInlineImm());
3677     }
3678 
3679     return false;
3680   }
3681   case AMDGPU::OPERAND_REG_IMM_V2FP16:
3682   case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
3683   case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16: {
3684     uint32_t Trunc = static_cast<uint32_t>(Imm);
3685     return AMDGPU::isInlinableLiteralV216(Trunc, ST.hasInv2PiInlineImm());
3686   }
3687   case AMDGPU::OPERAND_KIMM32:
3688   case AMDGPU::OPERAND_KIMM16:
3689     return false;
3690   default:
3691     llvm_unreachable("invalid bitwidth");
3692   }
3693 }
3694 
3695 bool SIInstrInfo::isLiteralConstantLike(const MachineOperand &MO,
3696                                         const MCOperandInfo &OpInfo) const {
3697   switch (MO.getType()) {
3698   case MachineOperand::MO_Register:
3699     return false;
3700   case MachineOperand::MO_Immediate:
3701     return !isInlineConstant(MO, OpInfo);
3702   case MachineOperand::MO_FrameIndex:
3703   case MachineOperand::MO_MachineBasicBlock:
3704   case MachineOperand::MO_ExternalSymbol:
3705   case MachineOperand::MO_GlobalAddress:
3706   case MachineOperand::MO_MCSymbol:
3707     return true;
3708   default:
3709     llvm_unreachable("unexpected operand type");
3710   }
3711 }
3712 
3713 static bool compareMachineOp(const MachineOperand &Op0,
3714                              const MachineOperand &Op1) {
3715   if (Op0.getType() != Op1.getType())
3716     return false;
3717 
3718   switch (Op0.getType()) {
3719   case MachineOperand::MO_Register:
3720     return Op0.getReg() == Op1.getReg();
3721   case MachineOperand::MO_Immediate:
3722     return Op0.getImm() == Op1.getImm();
3723   default:
3724     llvm_unreachable("Didn't expect to be comparing these operand types");
3725   }
3726 }
3727 
3728 bool SIInstrInfo::isImmOperandLegal(const MachineInstr &MI, unsigned OpNo,
3729                                     const MachineOperand &MO) const {
3730   const MCInstrDesc &InstDesc = MI.getDesc();
3731   const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpNo];
3732 
3733   assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal());
3734 
3735   if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE)
3736     return true;
3737 
3738   if (OpInfo.RegClass < 0)
3739     return false;
3740 
3741   if (MO.isImm() && isInlineConstant(MO, OpInfo)) {
3742     if (isMAI(MI) && ST.hasMFMAInlineLiteralBug() &&
3743         OpNo ==(unsigned)AMDGPU::getNamedOperandIdx(MI.getOpcode(),
3744                                                     AMDGPU::OpName::src2))
3745       return false;
3746     return RI.opCanUseInlineConstant(OpInfo.OperandType);
3747   }
3748 
3749   if (!RI.opCanUseLiteralConstant(OpInfo.OperandType))
3750     return false;
3751 
3752   if (!isVOP3(MI) || !AMDGPU::isSISrcOperand(InstDesc, OpNo))
3753     return true;
3754 
3755   return ST.hasVOP3Literal();
3756 }
3757 
3758 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const {
3759   // GFX90A does not have V_MUL_LEGACY_F32_e32.
3760   if (Opcode == AMDGPU::V_MUL_LEGACY_F32_e64 && ST.hasGFX90AInsts())
3761     return false;
3762 
3763   int Op32 = AMDGPU::getVOPe32(Opcode);
3764   if (Op32 == -1)
3765     return false;
3766 
3767   return pseudoToMCOpcode(Op32) != -1;
3768 }
3769 
3770 bool SIInstrInfo::hasModifiers(unsigned Opcode) const {
3771   // The src0_modifier operand is present on all instructions
3772   // that have modifiers.
3773 
3774   return AMDGPU::getNamedOperandIdx(Opcode,
3775                                     AMDGPU::OpName::src0_modifiers) != -1;
3776 }
3777 
3778 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI,
3779                                   unsigned OpName) const {
3780   const MachineOperand *Mods = getNamedOperand(MI, OpName);
3781   return Mods && Mods->getImm();
3782 }
3783 
3784 bool SIInstrInfo::hasAnyModifiersSet(const MachineInstr &MI) const {
3785   return hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
3786          hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
3787          hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers) ||
3788          hasModifiersSet(MI, AMDGPU::OpName::clamp) ||
3789          hasModifiersSet(MI, AMDGPU::OpName::omod);
3790 }
3791 
3792 bool SIInstrInfo::canShrink(const MachineInstr &MI,
3793                             const MachineRegisterInfo &MRI) const {
3794   const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
3795   // Can't shrink instruction with three operands.
3796   if (Src2) {
3797     switch (MI.getOpcode()) {
3798       default: return false;
3799 
3800       case AMDGPU::V_ADDC_U32_e64:
3801       case AMDGPU::V_SUBB_U32_e64:
3802       case AMDGPU::V_SUBBREV_U32_e64: {
3803         const MachineOperand *Src1
3804           = getNamedOperand(MI, AMDGPU::OpName::src1);
3805         if (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()))
3806           return false;
3807         // Additional verification is needed for sdst/src2.
3808         return true;
3809       }
3810       case AMDGPU::V_MAC_F16_e64:
3811       case AMDGPU::V_MAC_F32_e64:
3812       case AMDGPU::V_MAC_LEGACY_F32_e64:
3813       case AMDGPU::V_FMAC_F16_e64:
3814       case AMDGPU::V_FMAC_F32_e64:
3815       case AMDGPU::V_FMAC_F64_e64:
3816       case AMDGPU::V_FMAC_LEGACY_F32_e64:
3817         if (!Src2->isReg() || !RI.isVGPR(MRI, Src2->getReg()) ||
3818             hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers))
3819           return false;
3820         break;
3821 
3822       case AMDGPU::V_CNDMASK_B32_e64:
3823         break;
3824     }
3825   }
3826 
3827   const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
3828   if (Src1 && (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()) ||
3829                hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers)))
3830     return false;
3831 
3832   // We don't need to check src0, all input types are legal, so just make sure
3833   // src0 isn't using any modifiers.
3834   if (hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers))
3835     return false;
3836 
3837   // Can it be shrunk to a valid 32 bit opcode?
3838   if (!hasVALU32BitEncoding(MI.getOpcode()))
3839     return false;
3840 
3841   // Check output modifiers
3842   return !hasModifiersSet(MI, AMDGPU::OpName::omod) &&
3843          !hasModifiersSet(MI, AMDGPU::OpName::clamp);
3844 }
3845 
3846 // Set VCC operand with all flags from \p Orig, except for setting it as
3847 // implicit.
3848 static void copyFlagsToImplicitVCC(MachineInstr &MI,
3849                                    const MachineOperand &Orig) {
3850 
3851   for (MachineOperand &Use : MI.implicit_operands()) {
3852     if (Use.isUse() &&
3853         (Use.getReg() == AMDGPU::VCC || Use.getReg() == AMDGPU::VCC_LO)) {
3854       Use.setIsUndef(Orig.isUndef());
3855       Use.setIsKill(Orig.isKill());
3856       return;
3857     }
3858   }
3859 }
3860 
3861 MachineInstr *SIInstrInfo::buildShrunkInst(MachineInstr &MI,
3862                                            unsigned Op32) const {
3863   MachineBasicBlock *MBB = MI.getParent();;
3864   MachineInstrBuilder Inst32 =
3865     BuildMI(*MBB, MI, MI.getDebugLoc(), get(Op32))
3866     .setMIFlags(MI.getFlags());
3867 
3868   // Add the dst operand if the 32-bit encoding also has an explicit $vdst.
3869   // For VOPC instructions, this is replaced by an implicit def of vcc.
3870   int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst);
3871   if (Op32DstIdx != -1) {
3872     // dst
3873     Inst32.add(MI.getOperand(0));
3874   } else {
3875     assert(((MI.getOperand(0).getReg() == AMDGPU::VCC) ||
3876             (MI.getOperand(0).getReg() == AMDGPU::VCC_LO)) &&
3877            "Unexpected case");
3878   }
3879 
3880   Inst32.add(*getNamedOperand(MI, AMDGPU::OpName::src0));
3881 
3882   const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
3883   if (Src1)
3884     Inst32.add(*Src1);
3885 
3886   const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
3887 
3888   if (Src2) {
3889     int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2);
3890     if (Op32Src2Idx != -1) {
3891       Inst32.add(*Src2);
3892     } else {
3893       // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
3894       // replaced with an implicit read of vcc or vcc_lo. The implicit read
3895       // of vcc was already added during the initial BuildMI, but we
3896       // 1) may need to change vcc to vcc_lo to preserve the original register
3897       // 2) have to preserve the original flags.
3898       fixImplicitOperands(*Inst32);
3899       copyFlagsToImplicitVCC(*Inst32, *Src2);
3900     }
3901   }
3902 
3903   return Inst32;
3904 }
3905 
3906 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI,
3907                                   const MachineOperand &MO,
3908                                   const MCOperandInfo &OpInfo) const {
3909   // Literal constants use the constant bus.
3910   //if (isLiteralConstantLike(MO, OpInfo))
3911   // return true;
3912   if (MO.isImm())
3913     return !isInlineConstant(MO, OpInfo);
3914 
3915   if (!MO.isReg())
3916     return true; // Misc other operands like FrameIndex
3917 
3918   if (!MO.isUse())
3919     return false;
3920 
3921   if (MO.getReg().isVirtual())
3922     return RI.isSGPRClass(MRI.getRegClass(MO.getReg()));
3923 
3924   // Null is free
3925   if (MO.getReg() == AMDGPU::SGPR_NULL)
3926     return false;
3927 
3928   // SGPRs use the constant bus
3929   if (MO.isImplicit()) {
3930     return MO.getReg() == AMDGPU::M0 ||
3931            MO.getReg() == AMDGPU::VCC ||
3932            MO.getReg() == AMDGPU::VCC_LO;
3933   } else {
3934     return AMDGPU::SReg_32RegClass.contains(MO.getReg()) ||
3935            AMDGPU::SReg_64RegClass.contains(MO.getReg());
3936   }
3937 }
3938 
3939 static Register findImplicitSGPRRead(const MachineInstr &MI) {
3940   for (const MachineOperand &MO : MI.implicit_operands()) {
3941     // We only care about reads.
3942     if (MO.isDef())
3943       continue;
3944 
3945     switch (MO.getReg()) {
3946     case AMDGPU::VCC:
3947     case AMDGPU::VCC_LO:
3948     case AMDGPU::VCC_HI:
3949     case AMDGPU::M0:
3950     case AMDGPU::FLAT_SCR:
3951       return MO.getReg();
3952 
3953     default:
3954       break;
3955     }
3956   }
3957 
3958   return AMDGPU::NoRegister;
3959 }
3960 
3961 static bool shouldReadExec(const MachineInstr &MI) {
3962   if (SIInstrInfo::isVALU(MI)) {
3963     switch (MI.getOpcode()) {
3964     case AMDGPU::V_READLANE_B32:
3965     case AMDGPU::V_WRITELANE_B32:
3966       return false;
3967     }
3968 
3969     return true;
3970   }
3971 
3972   if (MI.isPreISelOpcode() ||
3973       SIInstrInfo::isGenericOpcode(MI.getOpcode()) ||
3974       SIInstrInfo::isSALU(MI) ||
3975       SIInstrInfo::isSMRD(MI))
3976     return false;
3977 
3978   return true;
3979 }
3980 
3981 static bool isSubRegOf(const SIRegisterInfo &TRI,
3982                        const MachineOperand &SuperVec,
3983                        const MachineOperand &SubReg) {
3984   if (SubReg.getReg().isPhysical())
3985     return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg());
3986 
3987   return SubReg.getSubReg() != AMDGPU::NoSubRegister &&
3988          SubReg.getReg() == SuperVec.getReg();
3989 }
3990 
3991 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI,
3992                                     StringRef &ErrInfo) const {
3993   uint16_t Opcode = MI.getOpcode();
3994   if (SIInstrInfo::isGenericOpcode(MI.getOpcode()))
3995     return true;
3996 
3997   const MachineFunction *MF = MI.getParent()->getParent();
3998   const MachineRegisterInfo &MRI = MF->getRegInfo();
3999 
4000   int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
4001   int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
4002   int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
4003 
4004   // Make sure the number of operands is correct.
4005   const MCInstrDesc &Desc = get(Opcode);
4006   if (!Desc.isVariadic() &&
4007       Desc.getNumOperands() != MI.getNumExplicitOperands()) {
4008     ErrInfo = "Instruction has wrong number of operands.";
4009     return false;
4010   }
4011 
4012   if (MI.isInlineAsm()) {
4013     // Verify register classes for inlineasm constraints.
4014     for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands();
4015          I != E; ++I) {
4016       const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI);
4017       if (!RC)
4018         continue;
4019 
4020       const MachineOperand &Op = MI.getOperand(I);
4021       if (!Op.isReg())
4022         continue;
4023 
4024       Register Reg = Op.getReg();
4025       if (!Reg.isVirtual() && !RC->contains(Reg)) {
4026         ErrInfo = "inlineasm operand has incorrect register class.";
4027         return false;
4028       }
4029     }
4030 
4031     return true;
4032   }
4033 
4034   if (isMIMG(MI) && MI.memoperands_empty() && MI.mayLoadOrStore()) {
4035     ErrInfo = "missing memory operand from MIMG instruction.";
4036     return false;
4037   }
4038 
4039   // Make sure the register classes are correct.
4040   for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) {
4041     const MachineOperand &MO = MI.getOperand(i);
4042     if (MO.isFPImm()) {
4043       ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast "
4044                 "all fp values to integers.";
4045       return false;
4046     }
4047 
4048     int RegClass = Desc.OpInfo[i].RegClass;
4049 
4050     switch (Desc.OpInfo[i].OperandType) {
4051     case MCOI::OPERAND_REGISTER:
4052       if (MI.getOperand(i).isImm() || MI.getOperand(i).isGlobal()) {
4053         ErrInfo = "Illegal immediate value for operand.";
4054         return false;
4055       }
4056       break;
4057     case AMDGPU::OPERAND_REG_IMM_INT32:
4058     case AMDGPU::OPERAND_REG_IMM_FP32:
4059     case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED:
4060     case AMDGPU::OPERAND_REG_IMM_V2FP32:
4061       break;
4062     case AMDGPU::OPERAND_REG_INLINE_C_INT32:
4063     case AMDGPU::OPERAND_REG_INLINE_C_FP32:
4064     case AMDGPU::OPERAND_REG_INLINE_C_INT64:
4065     case AMDGPU::OPERAND_REG_INLINE_C_FP64:
4066     case AMDGPU::OPERAND_REG_INLINE_C_INT16:
4067     case AMDGPU::OPERAND_REG_INLINE_C_FP16:
4068     case AMDGPU::OPERAND_REG_INLINE_AC_INT32:
4069     case AMDGPU::OPERAND_REG_INLINE_AC_FP32:
4070     case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
4071     case AMDGPU::OPERAND_REG_INLINE_AC_FP16:
4072     case AMDGPU::OPERAND_REG_INLINE_AC_FP64: {
4073       if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) {
4074         ErrInfo = "Illegal immediate value for operand.";
4075         return false;
4076       }
4077       break;
4078     }
4079     case MCOI::OPERAND_IMMEDIATE:
4080     case AMDGPU::OPERAND_KIMM32:
4081       // Check if this operand is an immediate.
4082       // FrameIndex operands will be replaced by immediates, so they are
4083       // allowed.
4084       if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) {
4085         ErrInfo = "Expected immediate, but got non-immediate";
4086         return false;
4087       }
4088       LLVM_FALLTHROUGH;
4089     default:
4090       continue;
4091     }
4092 
4093     if (!MO.isReg())
4094       continue;
4095     Register Reg = MO.getReg();
4096     if (!Reg)
4097       continue;
4098 
4099     // FIXME: Ideally we would have separate instruction definitions with the
4100     // aligned register constraint.
4101     // FIXME: We do not verify inline asm operands, but custom inline asm
4102     // verification is broken anyway
4103     if (ST.needsAlignedVGPRs()) {
4104       const TargetRegisterClass *RC = RI.getRegClassForReg(MRI, Reg);
4105       if (RI.hasVectorRegisters(RC) && MO.getSubReg()) {
4106         const TargetRegisterClass *SubRC =
4107             RI.getSubRegClass(RC, MO.getSubReg());
4108         RC = RI.getCompatibleSubRegClass(RC, SubRC, MO.getSubReg());
4109         if (RC)
4110           RC = SubRC;
4111       }
4112 
4113       // Check that this is the aligned version of the class.
4114       if (!RC || !RI.isProperlyAlignedRC(*RC)) {
4115         ErrInfo = "Subtarget requires even aligned vector registers";
4116         return false;
4117       }
4118     }
4119 
4120     if (RegClass != -1) {
4121       if (Reg.isVirtual())
4122         continue;
4123 
4124       const TargetRegisterClass *RC = RI.getRegClass(RegClass);
4125       if (!RC->contains(Reg)) {
4126         ErrInfo = "Operand has incorrect register class.";
4127         return false;
4128       }
4129     }
4130   }
4131 
4132   // Verify SDWA
4133   if (isSDWA(MI)) {
4134     if (!ST.hasSDWA()) {
4135       ErrInfo = "SDWA is not supported on this target";
4136       return false;
4137     }
4138 
4139     int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst);
4140 
4141     for (int OpIdx : {DstIdx, Src0Idx, Src1Idx, Src2Idx}) {
4142       if (OpIdx == -1)
4143         continue;
4144       const MachineOperand &MO = MI.getOperand(OpIdx);
4145 
4146       if (!ST.hasSDWAScalar()) {
4147         // Only VGPRS on VI
4148         if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) {
4149           ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI";
4150           return false;
4151         }
4152       } else {
4153         // No immediates on GFX9
4154         if (!MO.isReg()) {
4155           ErrInfo =
4156             "Only reg allowed as operands in SDWA instructions on GFX9+";
4157           return false;
4158         }
4159       }
4160     }
4161 
4162     if (!ST.hasSDWAOmod()) {
4163       // No omod allowed on VI
4164       const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
4165       if (OMod != nullptr &&
4166         (!OMod->isImm() || OMod->getImm() != 0)) {
4167         ErrInfo = "OMod not allowed in SDWA instructions on VI";
4168         return false;
4169       }
4170     }
4171 
4172     uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode);
4173     if (isVOPC(BasicOpcode)) {
4174       if (!ST.hasSDWASdst() && DstIdx != -1) {
4175         // Only vcc allowed as dst on VI for VOPC
4176         const MachineOperand &Dst = MI.getOperand(DstIdx);
4177         if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) {
4178           ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI";
4179           return false;
4180         }
4181       } else if (!ST.hasSDWAOutModsVOPC()) {
4182         // No clamp allowed on GFX9 for VOPC
4183         const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp);
4184         if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) {
4185           ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI";
4186           return false;
4187         }
4188 
4189         // No omod allowed on GFX9 for VOPC
4190         const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
4191         if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) {
4192           ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI";
4193           return false;
4194         }
4195       }
4196     }
4197 
4198     const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused);
4199     if (DstUnused && DstUnused->isImm() &&
4200         DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) {
4201       const MachineOperand &Dst = MI.getOperand(DstIdx);
4202       if (!Dst.isReg() || !Dst.isTied()) {
4203         ErrInfo = "Dst register should have tied register";
4204         return false;
4205       }
4206 
4207       const MachineOperand &TiedMO =
4208           MI.getOperand(MI.findTiedOperandIdx(DstIdx));
4209       if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) {
4210         ErrInfo =
4211             "Dst register should be tied to implicit use of preserved register";
4212         return false;
4213       } else if (TiedMO.getReg().isPhysical() &&
4214                  Dst.getReg() != TiedMO.getReg()) {
4215         ErrInfo = "Dst register should use same physical register as preserved";
4216         return false;
4217       }
4218     }
4219   }
4220 
4221   // Verify MIMG
4222   if (isMIMG(MI.getOpcode()) && !MI.mayStore()) {
4223     // Ensure that the return type used is large enough for all the options
4224     // being used TFE/LWE require an extra result register.
4225     const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask);
4226     if (DMask) {
4227       uint64_t DMaskImm = DMask->getImm();
4228       uint32_t RegCount =
4229           isGather4(MI.getOpcode()) ? 4 : countPopulation(DMaskImm);
4230       const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe);
4231       const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe);
4232       const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16);
4233 
4234       // Adjust for packed 16 bit values
4235       if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem())
4236         RegCount >>= 1;
4237 
4238       // Adjust if using LWE or TFE
4239       if ((LWE && LWE->getImm()) || (TFE && TFE->getImm()))
4240         RegCount += 1;
4241 
4242       const uint32_t DstIdx =
4243           AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata);
4244       const MachineOperand &Dst = MI.getOperand(DstIdx);
4245       if (Dst.isReg()) {
4246         const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx);
4247         uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32;
4248         if (RegCount > DstSize) {
4249           ErrInfo = "MIMG instruction returns too many registers for dst "
4250                     "register class";
4251           return false;
4252         }
4253       }
4254     }
4255   }
4256 
4257   // Verify VOP*. Ignore multiple sgpr operands on writelane.
4258   if (isVALU(MI) && Desc.getOpcode() != AMDGPU::V_WRITELANE_B32) {
4259     unsigned ConstantBusCount = 0;
4260     bool UsesLiteral = false;
4261     const MachineOperand *LiteralVal = nullptr;
4262 
4263     if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1)
4264       ++ConstantBusCount;
4265 
4266     SmallVector<Register, 2> SGPRsUsed;
4267     Register SGPRUsed;
4268 
4269     // Only look at the true operands. Only a real operand can use the constant
4270     // bus, and we don't want to check pseudo-operands like the source modifier
4271     // flags.
4272     for (int OpIdx : {Src0Idx, Src1Idx, Src2Idx}) {
4273       if (OpIdx == -1)
4274         break;
4275       const MachineOperand &MO = MI.getOperand(OpIdx);
4276       if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) {
4277         if (MO.isReg()) {
4278           SGPRUsed = MO.getReg();
4279           if (llvm::all_of(SGPRsUsed, [SGPRUsed](unsigned SGPR) {
4280                 return SGPRUsed != SGPR;
4281               })) {
4282             ++ConstantBusCount;
4283             SGPRsUsed.push_back(SGPRUsed);
4284           }
4285         } else {
4286           if (!UsesLiteral) {
4287             ++ConstantBusCount;
4288             UsesLiteral = true;
4289             LiteralVal = &MO;
4290           } else if (!MO.isIdenticalTo(*LiteralVal)) {
4291             assert(isVOP3(MI));
4292             ErrInfo = "VOP3 instruction uses more than one literal";
4293             return false;
4294           }
4295         }
4296       }
4297     }
4298 
4299     SGPRUsed = findImplicitSGPRRead(MI);
4300     if (SGPRUsed != AMDGPU::NoRegister) {
4301       // Implicit uses may safely overlap true operands
4302       if (llvm::all_of(SGPRsUsed, [this, SGPRUsed](unsigned SGPR) {
4303             return !RI.regsOverlap(SGPRUsed, SGPR);
4304           })) {
4305         ++ConstantBusCount;
4306         SGPRsUsed.push_back(SGPRUsed);
4307       }
4308     }
4309 
4310     // v_writelane_b32 is an exception from constant bus restriction:
4311     // vsrc0 can be sgpr, const or m0 and lane select sgpr, m0 or inline-const
4312     if (ConstantBusCount > ST.getConstantBusLimit(Opcode) &&
4313         Opcode != AMDGPU::V_WRITELANE_B32) {
4314       ErrInfo = "VOP* instruction violates constant bus restriction";
4315       return false;
4316     }
4317 
4318     if (isVOP3(MI) && UsesLiteral && !ST.hasVOP3Literal()) {
4319       ErrInfo = "VOP3 instruction uses literal";
4320       return false;
4321     }
4322   }
4323 
4324   // Special case for writelane - this can break the multiple constant bus rule,
4325   // but still can't use more than one SGPR register
4326   if (Desc.getOpcode() == AMDGPU::V_WRITELANE_B32) {
4327     unsigned SGPRCount = 0;
4328     Register SGPRUsed = AMDGPU::NoRegister;
4329 
4330     for (int OpIdx : {Src0Idx, Src1Idx}) {
4331       if (OpIdx == -1)
4332         break;
4333 
4334       const MachineOperand &MO = MI.getOperand(OpIdx);
4335 
4336       if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) {
4337         if (MO.isReg() && MO.getReg() != AMDGPU::M0) {
4338           if (MO.getReg() != SGPRUsed)
4339             ++SGPRCount;
4340           SGPRUsed = MO.getReg();
4341         }
4342       }
4343       if (SGPRCount > ST.getConstantBusLimit(Opcode)) {
4344         ErrInfo = "WRITELANE instruction violates constant bus restriction";
4345         return false;
4346       }
4347     }
4348   }
4349 
4350   // Verify misc. restrictions on specific instructions.
4351   if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32_e64 ||
4352       Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64_e64) {
4353     const MachineOperand &Src0 = MI.getOperand(Src0Idx);
4354     const MachineOperand &Src1 = MI.getOperand(Src1Idx);
4355     const MachineOperand &Src2 = MI.getOperand(Src2Idx);
4356     if (Src0.isReg() && Src1.isReg() && Src2.isReg()) {
4357       if (!compareMachineOp(Src0, Src1) &&
4358           !compareMachineOp(Src0, Src2)) {
4359         ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2";
4360         return false;
4361       }
4362     }
4363     if ((getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm() &
4364          SISrcMods::ABS) ||
4365         (getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm() &
4366          SISrcMods::ABS) ||
4367         (getNamedOperand(MI, AMDGPU::OpName::src2_modifiers)->getImm() &
4368          SISrcMods::ABS)) {
4369       ErrInfo = "ABS not allowed in VOP3B instructions";
4370       return false;
4371     }
4372   }
4373 
4374   if (isSOP2(MI) || isSOPC(MI)) {
4375     const MachineOperand &Src0 = MI.getOperand(Src0Idx);
4376     const MachineOperand &Src1 = MI.getOperand(Src1Idx);
4377     unsigned Immediates = 0;
4378 
4379     if (!Src0.isReg() &&
4380         !isInlineConstant(Src0, Desc.OpInfo[Src0Idx].OperandType))
4381       Immediates++;
4382     if (!Src1.isReg() &&
4383         !isInlineConstant(Src1, Desc.OpInfo[Src1Idx].OperandType))
4384       Immediates++;
4385 
4386     if (Immediates > 1) {
4387       ErrInfo = "SOP2/SOPC instruction requires too many immediate constants";
4388       return false;
4389     }
4390   }
4391 
4392   if (isSOPK(MI)) {
4393     auto Op = getNamedOperand(MI, AMDGPU::OpName::simm16);
4394     if (Desc.isBranch()) {
4395       if (!Op->isMBB()) {
4396         ErrInfo = "invalid branch target for SOPK instruction";
4397         return false;
4398       }
4399     } else {
4400       uint64_t Imm = Op->getImm();
4401       if (sopkIsZext(MI)) {
4402         if (!isUInt<16>(Imm)) {
4403           ErrInfo = "invalid immediate for SOPK instruction";
4404           return false;
4405         }
4406       } else {
4407         if (!isInt<16>(Imm)) {
4408           ErrInfo = "invalid immediate for SOPK instruction";
4409           return false;
4410         }
4411       }
4412     }
4413   }
4414 
4415   if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 ||
4416       Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 ||
4417       Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
4418       Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) {
4419     const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
4420                        Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64;
4421 
4422     const unsigned StaticNumOps = Desc.getNumOperands() +
4423       Desc.getNumImplicitUses();
4424     const unsigned NumImplicitOps = IsDst ? 2 : 1;
4425 
4426     // Allow additional implicit operands. This allows a fixup done by the post
4427     // RA scheduler where the main implicit operand is killed and implicit-defs
4428     // are added for sub-registers that remain live after this instruction.
4429     if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) {
4430       ErrInfo = "missing implicit register operands";
4431       return false;
4432     }
4433 
4434     const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
4435     if (IsDst) {
4436       if (!Dst->isUse()) {
4437         ErrInfo = "v_movreld_b32 vdst should be a use operand";
4438         return false;
4439       }
4440 
4441       unsigned UseOpIdx;
4442       if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) ||
4443           UseOpIdx != StaticNumOps + 1) {
4444         ErrInfo = "movrel implicit operands should be tied";
4445         return false;
4446       }
4447     }
4448 
4449     const MachineOperand &Src0 = MI.getOperand(Src0Idx);
4450     const MachineOperand &ImpUse
4451       = MI.getOperand(StaticNumOps + NumImplicitOps - 1);
4452     if (!ImpUse.isReg() || !ImpUse.isUse() ||
4453         !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) {
4454       ErrInfo = "src0 should be subreg of implicit vector use";
4455       return false;
4456     }
4457   }
4458 
4459   // Make sure we aren't losing exec uses in the td files. This mostly requires
4460   // being careful when using let Uses to try to add other use registers.
4461   if (shouldReadExec(MI)) {
4462     if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) {
4463       ErrInfo = "VALU instruction does not implicitly read exec mask";
4464       return false;
4465     }
4466   }
4467 
4468   if (isSMRD(MI)) {
4469     if (MI.mayStore()) {
4470       // The register offset form of scalar stores may only use m0 as the
4471       // soffset register.
4472       const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soff);
4473       if (Soff && Soff->getReg() != AMDGPU::M0) {
4474         ErrInfo = "scalar stores must use m0 as offset register";
4475         return false;
4476       }
4477     }
4478   }
4479 
4480   if (isFLAT(MI) && !ST.hasFlatInstOffsets()) {
4481     const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
4482     if (Offset->getImm() != 0) {
4483       ErrInfo = "subtarget does not support offsets in flat instructions";
4484       return false;
4485     }
4486   }
4487 
4488   if (isMIMG(MI)) {
4489     const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim);
4490     if (DimOp) {
4491       int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode,
4492                                                  AMDGPU::OpName::vaddr0);
4493       int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
4494       const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode);
4495       const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
4496           AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode);
4497       const AMDGPU::MIMGDimInfo *Dim =
4498           AMDGPU::getMIMGDimInfoByEncoding(DimOp->getImm());
4499 
4500       if (!Dim) {
4501         ErrInfo = "dim is out of range";
4502         return false;
4503       }
4504 
4505       bool IsA16 = false;
4506       if (ST.hasR128A16()) {
4507         const MachineOperand *R128A16 = getNamedOperand(MI, AMDGPU::OpName::r128);
4508         IsA16 = R128A16->getImm() != 0;
4509       } else if (ST.hasGFX10A16()) {
4510         const MachineOperand *A16 = getNamedOperand(MI, AMDGPU::OpName::a16);
4511         IsA16 = A16->getImm() != 0;
4512       }
4513 
4514       bool IsNSA = SRsrcIdx - VAddr0Idx > 1;
4515 
4516       unsigned AddrWords =
4517           AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, ST.hasG16());
4518 
4519       unsigned VAddrWords;
4520       if (IsNSA) {
4521         VAddrWords = SRsrcIdx - VAddr0Idx;
4522       } else {
4523         const TargetRegisterClass *RC = getOpRegClass(MI, VAddr0Idx);
4524         VAddrWords = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 32;
4525         if (AddrWords > 8)
4526           AddrWords = 16;
4527       }
4528 
4529       if (VAddrWords != AddrWords) {
4530         LLVM_DEBUG(dbgs() << "bad vaddr size, expected " << AddrWords
4531                           << " but got " << VAddrWords << "\n");
4532         ErrInfo = "bad vaddr size";
4533         return false;
4534       }
4535     }
4536   }
4537 
4538   const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl);
4539   if (DppCt) {
4540     using namespace AMDGPU::DPP;
4541 
4542     unsigned DC = DppCt->getImm();
4543     if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 ||
4544         DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST ||
4545         (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) ||
4546         (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) ||
4547         (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) ||
4548         (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) ||
4549         (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) {
4550       ErrInfo = "Invalid dpp_ctrl value";
4551       return false;
4552     }
4553     if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 &&
4554         ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
4555       ErrInfo = "Invalid dpp_ctrl value: "
4556                 "wavefront shifts are not supported on GFX10+";
4557       return false;
4558     }
4559     if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 &&
4560         ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
4561       ErrInfo = "Invalid dpp_ctrl value: "
4562                 "broadcasts are not supported on GFX10+";
4563       return false;
4564     }
4565     if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST &&
4566         ST.getGeneration() < AMDGPUSubtarget::GFX10) {
4567       if (DC >= DppCtrl::ROW_NEWBCAST_FIRST &&
4568           DC <= DppCtrl::ROW_NEWBCAST_LAST &&
4569           !ST.hasGFX90AInsts()) {
4570         ErrInfo = "Invalid dpp_ctrl value: "
4571                   "row_newbroadcast/row_share is not supported before "
4572                   "GFX90A/GFX10";
4573         return false;
4574       } else if (DC > DppCtrl::ROW_NEWBCAST_LAST || !ST.hasGFX90AInsts()) {
4575         ErrInfo = "Invalid dpp_ctrl value: "
4576                   "row_share and row_xmask are not supported before GFX10";
4577         return false;
4578       }
4579     }
4580 
4581     int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst);
4582 
4583     if (Opcode != AMDGPU::V_MOV_B64_DPP_PSEUDO &&
4584         ((DstIdx >= 0 &&
4585           (Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64RegClassID ||
4586            Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64_Align2RegClassID)) ||
4587          ((Src0Idx >= 0 &&
4588            (Desc.OpInfo[Src0Idx].RegClass == AMDGPU::VReg_64RegClassID ||
4589             Desc.OpInfo[Src0Idx].RegClass ==
4590                 AMDGPU::VReg_64_Align2RegClassID)))) &&
4591         !AMDGPU::isLegal64BitDPPControl(DC)) {
4592       ErrInfo = "Invalid dpp_ctrl value: "
4593                 "64 bit dpp only support row_newbcast";
4594       return false;
4595     }
4596   }
4597 
4598   if ((MI.mayStore() || MI.mayLoad()) && !isVGPRSpill(MI)) {
4599     const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
4600     uint16_t DataNameIdx = isDS(Opcode) ? AMDGPU::OpName::data0
4601                                         : AMDGPU::OpName::vdata;
4602     const MachineOperand *Data = getNamedOperand(MI, DataNameIdx);
4603     const MachineOperand *Data2 = getNamedOperand(MI, AMDGPU::OpName::data1);
4604     if (Data && !Data->isReg())
4605       Data = nullptr;
4606 
4607     if (ST.hasGFX90AInsts()) {
4608       if (Dst && Data &&
4609           (RI.isAGPR(MRI, Dst->getReg()) != RI.isAGPR(MRI, Data->getReg()))) {
4610         ErrInfo = "Invalid register class: "
4611                   "vdata and vdst should be both VGPR or AGPR";
4612         return false;
4613       }
4614       if (Data && Data2 &&
4615           (RI.isAGPR(MRI, Data->getReg()) != RI.isAGPR(MRI, Data2->getReg()))) {
4616         ErrInfo = "Invalid register class: "
4617                   "both data operands should be VGPR or AGPR";
4618         return false;
4619       }
4620     } else {
4621       if ((Dst && RI.isAGPR(MRI, Dst->getReg())) ||
4622           (Data && RI.isAGPR(MRI, Data->getReg())) ||
4623           (Data2 && RI.isAGPR(MRI, Data2->getReg()))) {
4624         ErrInfo = "Invalid register class: "
4625                   "agpr loads and stores not supported on this GPU";
4626         return false;
4627       }
4628     }
4629   }
4630 
4631   if (ST.needsAlignedVGPRs() &&
4632       (MI.getOpcode() == AMDGPU::DS_GWS_INIT ||
4633        MI.getOpcode() == AMDGPU::DS_GWS_SEMA_BR ||
4634        MI.getOpcode() == AMDGPU::DS_GWS_BARRIER)) {
4635     const MachineOperand *Op = getNamedOperand(MI, AMDGPU::OpName::data0);
4636     Register Reg = Op->getReg();
4637     bool Aligned = true;
4638     if (Reg.isPhysical()) {
4639       Aligned = !(RI.getHWRegIndex(Reg) & 1);
4640     } else {
4641       const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
4642       Aligned = RI.getRegSizeInBits(RC) > 32 && RI.isProperlyAlignedRC(RC) &&
4643                 !(RI.getChannelFromSubReg(Op->getSubReg()) & 1);
4644     }
4645 
4646     if (!Aligned) {
4647       ErrInfo = "Subtarget requires even aligned vector registers "
4648                 "for DS_GWS instructions";
4649       return false;
4650     }
4651   }
4652 
4653   if (MI.getOpcode() == AMDGPU::V_ACCVGPR_WRITE_B32_e64 &&
4654       !ST.hasGFX90AInsts()) {
4655     const MachineOperand *Src = getNamedOperand(MI, AMDGPU::OpName::src0);
4656     if (Src->isReg() && RI.isSGPRReg(MRI, Src->getReg())) {
4657       ErrInfo = "Invalid register class: "
4658                 "v_accvgpr_write with an SGPR is not supported on this GPU";
4659       return false;
4660     }
4661   }
4662 
4663   if (Desc.getOpcode() == AMDGPU::G_AMDGPU_WAVE_ADDRESS) {
4664     const MachineOperand &SrcOp = MI.getOperand(1);
4665     if (!SrcOp.isReg() || SrcOp.getReg().isVirtual()) {
4666       ErrInfo = "pseudo expects only physical SGPRs";
4667       return false;
4668     }
4669   }
4670 
4671   return true;
4672 }
4673 
4674 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const {
4675   switch (MI.getOpcode()) {
4676   default: return AMDGPU::INSTRUCTION_LIST_END;
4677   case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE;
4678   case AMDGPU::COPY: return AMDGPU::COPY;
4679   case AMDGPU::PHI: return AMDGPU::PHI;
4680   case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG;
4681   case AMDGPU::WQM: return AMDGPU::WQM;
4682   case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM;
4683   case AMDGPU::STRICT_WWM: return AMDGPU::STRICT_WWM;
4684   case AMDGPU::STRICT_WQM: return AMDGPU::STRICT_WQM;
4685   case AMDGPU::S_MOV_B32: {
4686     const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
4687     return MI.getOperand(1).isReg() ||
4688            RI.isAGPR(MRI, MI.getOperand(0).getReg()) ?
4689            AMDGPU::COPY : AMDGPU::V_MOV_B32_e32;
4690   }
4691   case AMDGPU::S_ADD_I32:
4692     return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32;
4693   case AMDGPU::S_ADDC_U32:
4694     return AMDGPU::V_ADDC_U32_e32;
4695   case AMDGPU::S_SUB_I32:
4696     return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_CO_U32_e32;
4697     // FIXME: These are not consistently handled, and selected when the carry is
4698     // used.
4699   case AMDGPU::S_ADD_U32:
4700     return AMDGPU::V_ADD_CO_U32_e32;
4701   case AMDGPU::S_SUB_U32:
4702     return AMDGPU::V_SUB_CO_U32_e32;
4703   case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32;
4704   case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32_e64;
4705   case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32_e64;
4706   case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32_e64;
4707   case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64;
4708   case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64;
4709   case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64;
4710   case AMDGPU::S_XNOR_B32:
4711     return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END;
4712   case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64;
4713   case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64;
4714   case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64;
4715   case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64;
4716   case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32;
4717   case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64_e64;
4718   case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32;
4719   case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64_e64;
4720   case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32;
4721   case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64_e64;
4722   case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32_e64;
4723   case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32_e64;
4724   case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32_e64;
4725   case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32_e64;
4726   case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64;
4727   case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32;
4728   case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32;
4729   case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32;
4730   case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e64;
4731   case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e64;
4732   case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e64;
4733   case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e64;
4734   case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e64;
4735   case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e64;
4736   case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e64;
4737   case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e64;
4738   case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e64;
4739   case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e64;
4740   case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e64;
4741   case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e64;
4742   case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e64;
4743   case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e64;
4744   case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64;
4745   case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32;
4746   case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32;
4747   case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64;
4748   case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ;
4749   case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ;
4750   }
4751   llvm_unreachable(
4752       "Unexpected scalar opcode without corresponding vector one!");
4753 }
4754 
4755 static const TargetRegisterClass *
4756 adjustAllocatableRegClass(const GCNSubtarget &ST, const SIRegisterInfo &RI,
4757                           const MachineRegisterInfo &MRI,
4758                           const MCInstrDesc &TID, unsigned RCID,
4759                           bool IsAllocatable) {
4760   if ((IsAllocatable || !ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) &&
4761       (((TID.mayLoad() || TID.mayStore()) &&
4762         !(TID.TSFlags & SIInstrFlags::VGPRSpill)) ||
4763        (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::MIMG)))) {
4764     switch (RCID) {
4765     case AMDGPU::AV_32RegClassID:
4766       RCID = AMDGPU::VGPR_32RegClassID;
4767       break;
4768     case AMDGPU::AV_64RegClassID:
4769       RCID = AMDGPU::VReg_64RegClassID;
4770       break;
4771     case AMDGPU::AV_96RegClassID:
4772       RCID = AMDGPU::VReg_96RegClassID;
4773       break;
4774     case AMDGPU::AV_128RegClassID:
4775       RCID = AMDGPU::VReg_128RegClassID;
4776       break;
4777     case AMDGPU::AV_160RegClassID:
4778       RCID = AMDGPU::VReg_160RegClassID;
4779       break;
4780     case AMDGPU::AV_512RegClassID:
4781       RCID = AMDGPU::VReg_512RegClassID;
4782       break;
4783     default:
4784       break;
4785     }
4786   }
4787 
4788   return RI.getProperlyAlignedRC(RI.getRegClass(RCID));
4789 }
4790 
4791 const TargetRegisterClass *SIInstrInfo::getRegClass(const MCInstrDesc &TID,
4792     unsigned OpNum, const TargetRegisterInfo *TRI,
4793     const MachineFunction &MF)
4794   const {
4795   if (OpNum >= TID.getNumOperands())
4796     return nullptr;
4797   auto RegClass = TID.OpInfo[OpNum].RegClass;
4798   bool IsAllocatable = false;
4799   if (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::FLAT)) {
4800     // vdst and vdata should be both VGPR or AGPR, same for the DS instructions
4801     // with two data operands. Request register class constrained to VGPR only
4802     // of both operands present as Machine Copy Propagation can not check this
4803     // constraint and possibly other passes too.
4804     //
4805     // The check is limited to FLAT and DS because atomics in non-flat encoding
4806     // have their vdst and vdata tied to be the same register.
4807     const int VDstIdx = AMDGPU::getNamedOperandIdx(TID.Opcode,
4808                                                    AMDGPU::OpName::vdst);
4809     const int DataIdx = AMDGPU::getNamedOperandIdx(TID.Opcode,
4810         (TID.TSFlags & SIInstrFlags::DS) ? AMDGPU::OpName::data0
4811                                          : AMDGPU::OpName::vdata);
4812     if (DataIdx != -1) {
4813       IsAllocatable = VDstIdx != -1 ||
4814                       AMDGPU::getNamedOperandIdx(TID.Opcode,
4815                                                  AMDGPU::OpName::data1) != -1;
4816     }
4817   }
4818   return adjustAllocatableRegClass(ST, RI, MF.getRegInfo(), TID, RegClass,
4819                                    IsAllocatable);
4820 }
4821 
4822 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI,
4823                                                       unsigned OpNo) const {
4824   const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
4825   const MCInstrDesc &Desc = get(MI.getOpcode());
4826   if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
4827       Desc.OpInfo[OpNo].RegClass == -1) {
4828     Register Reg = MI.getOperand(OpNo).getReg();
4829 
4830     if (Reg.isVirtual())
4831       return MRI.getRegClass(Reg);
4832     return RI.getPhysRegClass(Reg);
4833   }
4834 
4835   unsigned RCID = Desc.OpInfo[OpNo].RegClass;
4836   return adjustAllocatableRegClass(ST, RI, MRI, Desc, RCID, true);
4837 }
4838 
4839 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const {
4840   MachineBasicBlock::iterator I = MI;
4841   MachineBasicBlock *MBB = MI.getParent();
4842   MachineOperand &MO = MI.getOperand(OpIdx);
4843   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
4844   unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass;
4845   const TargetRegisterClass *RC = RI.getRegClass(RCID);
4846   unsigned Size = RI.getRegSizeInBits(*RC);
4847   unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO : AMDGPU::V_MOV_B32_e32;
4848   if (MO.isReg())
4849     Opcode = AMDGPU::COPY;
4850   else if (RI.isSGPRClass(RC))
4851     Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32;
4852 
4853   const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
4854   const TargetRegisterClass *VRC64 = RI.getVGPR64Class();
4855   if (RI.getCommonSubClass(VRC64, VRC))
4856     VRC = VRC64;
4857   else
4858     VRC = &AMDGPU::VGPR_32RegClass;
4859 
4860   Register Reg = MRI.createVirtualRegister(VRC);
4861   DebugLoc DL = MBB->findDebugLoc(I);
4862   BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO);
4863   MO.ChangeToRegister(Reg, false);
4864 }
4865 
4866 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI,
4867                                          MachineRegisterInfo &MRI,
4868                                          MachineOperand &SuperReg,
4869                                          const TargetRegisterClass *SuperRC,
4870                                          unsigned SubIdx,
4871                                          const TargetRegisterClass *SubRC)
4872                                          const {
4873   MachineBasicBlock *MBB = MI->getParent();
4874   DebugLoc DL = MI->getDebugLoc();
4875   Register SubReg = MRI.createVirtualRegister(SubRC);
4876 
4877   if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) {
4878     BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
4879       .addReg(SuperReg.getReg(), 0, SubIdx);
4880     return SubReg;
4881   }
4882 
4883   // Just in case the super register is itself a sub-register, copy it to a new
4884   // value so we don't need to worry about merging its subreg index with the
4885   // SubIdx passed to this function. The register coalescer should be able to
4886   // eliminate this extra copy.
4887   Register NewSuperReg = MRI.createVirtualRegister(SuperRC);
4888 
4889   BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg)
4890     .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg());
4891 
4892   BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
4893     .addReg(NewSuperReg, 0, SubIdx);
4894 
4895   return SubReg;
4896 }
4897 
4898 MachineOperand SIInstrInfo::buildExtractSubRegOrImm(
4899   MachineBasicBlock::iterator MII,
4900   MachineRegisterInfo &MRI,
4901   MachineOperand &Op,
4902   const TargetRegisterClass *SuperRC,
4903   unsigned SubIdx,
4904   const TargetRegisterClass *SubRC) const {
4905   if (Op.isImm()) {
4906     if (SubIdx == AMDGPU::sub0)
4907       return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm()));
4908     if (SubIdx == AMDGPU::sub1)
4909       return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32));
4910 
4911     llvm_unreachable("Unhandled register index for immediate");
4912   }
4913 
4914   unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
4915                                        SubIdx, SubRC);
4916   return MachineOperand::CreateReg(SubReg, false);
4917 }
4918 
4919 // Change the order of operands from (0, 1, 2) to (0, 2, 1)
4920 void SIInstrInfo::swapOperands(MachineInstr &Inst) const {
4921   assert(Inst.getNumExplicitOperands() == 3);
4922   MachineOperand Op1 = Inst.getOperand(1);
4923   Inst.removeOperand(1);
4924   Inst.addOperand(Op1);
4925 }
4926 
4927 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI,
4928                                     const MCOperandInfo &OpInfo,
4929                                     const MachineOperand &MO) const {
4930   if (!MO.isReg())
4931     return false;
4932 
4933   Register Reg = MO.getReg();
4934 
4935   const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass);
4936   if (Reg.isPhysical())
4937     return DRC->contains(Reg);
4938 
4939   const TargetRegisterClass *RC = MRI.getRegClass(Reg);
4940 
4941   if (MO.getSubReg()) {
4942     const MachineFunction *MF = MO.getParent()->getParent()->getParent();
4943     const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF);
4944     if (!SuperRC)
4945       return false;
4946 
4947     DRC = RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg());
4948     if (!DRC)
4949       return false;
4950   }
4951   return RC->hasSuperClassEq(DRC);
4952 }
4953 
4954 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI,
4955                                      const MCOperandInfo &OpInfo,
4956                                      const MachineOperand &MO) const {
4957   if (MO.isReg())
4958     return isLegalRegOperand(MRI, OpInfo, MO);
4959 
4960   // Handle non-register types that are treated like immediates.
4961   assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal());
4962   return true;
4963 }
4964 
4965 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx,
4966                                  const MachineOperand *MO) const {
4967   const MachineFunction &MF = *MI.getParent()->getParent();
4968   const MachineRegisterInfo &MRI = MF.getRegInfo();
4969   const MCInstrDesc &InstDesc = MI.getDesc();
4970   const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx];
4971   const TargetRegisterClass *DefinedRC =
4972       OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr;
4973   if (!MO)
4974     MO = &MI.getOperand(OpIdx);
4975 
4976   int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode());
4977   int VOP3LiteralLimit = ST.hasVOP3Literal() ? 1 : 0;
4978   if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) {
4979     if (isVOP3(MI) && isLiteralConstantLike(*MO, OpInfo) && !VOP3LiteralLimit--)
4980       return false;
4981 
4982     SmallDenseSet<RegSubRegPair> SGPRsUsed;
4983     if (MO->isReg())
4984       SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg()));
4985 
4986     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
4987       if (i == OpIdx)
4988         continue;
4989       const MachineOperand &Op = MI.getOperand(i);
4990       if (Op.isReg()) {
4991         RegSubRegPair SGPR(Op.getReg(), Op.getSubReg());
4992         if (!SGPRsUsed.count(SGPR) &&
4993             usesConstantBus(MRI, Op, InstDesc.OpInfo[i])) {
4994           if (--ConstantBusLimit <= 0)
4995             return false;
4996           SGPRsUsed.insert(SGPR);
4997         }
4998       } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) {
4999         if (--ConstantBusLimit <= 0)
5000           return false;
5001       } else if (isVOP3(MI) && AMDGPU::isSISrcOperand(InstDesc, i) &&
5002                  isLiteralConstantLike(Op, InstDesc.OpInfo[i])) {
5003         if (!VOP3LiteralLimit--)
5004           return false;
5005         if (--ConstantBusLimit <= 0)
5006           return false;
5007       }
5008     }
5009   }
5010 
5011   if (MO->isReg()) {
5012     assert(DefinedRC);
5013     if (!isLegalRegOperand(MRI, OpInfo, *MO))
5014       return false;
5015     bool IsAGPR = RI.isAGPR(MRI, MO->getReg());
5016     if (IsAGPR && !ST.hasMAIInsts())
5017       return false;
5018     unsigned Opc = MI.getOpcode();
5019     if (IsAGPR &&
5020         (!ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) &&
5021         (MI.mayLoad() || MI.mayStore() || isDS(Opc) || isMIMG(Opc)))
5022       return false;
5023     // Atomics should have both vdst and vdata either vgpr or agpr.
5024     const int VDstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
5025     const int DataIdx = AMDGPU::getNamedOperandIdx(Opc,
5026         isDS(Opc) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata);
5027     if ((int)OpIdx == VDstIdx && DataIdx != -1 &&
5028         MI.getOperand(DataIdx).isReg() &&
5029         RI.isAGPR(MRI, MI.getOperand(DataIdx).getReg()) != IsAGPR)
5030       return false;
5031     if ((int)OpIdx == DataIdx) {
5032       if (VDstIdx != -1 &&
5033           RI.isAGPR(MRI, MI.getOperand(VDstIdx).getReg()) != IsAGPR)
5034         return false;
5035       // DS instructions with 2 src operands also must have tied RC.
5036       const int Data1Idx = AMDGPU::getNamedOperandIdx(Opc,
5037                                                       AMDGPU::OpName::data1);
5038       if (Data1Idx != -1 && MI.getOperand(Data1Idx).isReg() &&
5039           RI.isAGPR(MRI, MI.getOperand(Data1Idx).getReg()) != IsAGPR)
5040         return false;
5041     }
5042     if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && !ST.hasGFX90AInsts() &&
5043         (int)OpIdx == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) &&
5044         RI.isSGPRReg(MRI, MO->getReg()))
5045       return false;
5046     return true;
5047   }
5048 
5049   // Handle non-register types that are treated like immediates.
5050   assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal());
5051 
5052   if (!DefinedRC) {
5053     // This operand expects an immediate.
5054     return true;
5055   }
5056 
5057   return isImmOperandLegal(MI, OpIdx, *MO);
5058 }
5059 
5060 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI,
5061                                        MachineInstr &MI) const {
5062   unsigned Opc = MI.getOpcode();
5063   const MCInstrDesc &InstrDesc = get(Opc);
5064 
5065   int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
5066   MachineOperand &Src0 = MI.getOperand(Src0Idx);
5067 
5068   int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
5069   MachineOperand &Src1 = MI.getOperand(Src1Idx);
5070 
5071   // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32
5072   // we need to only have one constant bus use before GFX10.
5073   bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister;
5074   if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 &&
5075       Src0.isReg() && (RI.isSGPRReg(MRI, Src0.getReg()) ||
5076        isLiteralConstantLike(Src0, InstrDesc.OpInfo[Src0Idx])))
5077     legalizeOpWithMove(MI, Src0Idx);
5078 
5079   // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for
5080   // both the value to write (src0) and lane select (src1).  Fix up non-SGPR
5081   // src0/src1 with V_READFIRSTLANE.
5082   if (Opc == AMDGPU::V_WRITELANE_B32) {
5083     const DebugLoc &DL = MI.getDebugLoc();
5084     if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) {
5085       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5086       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5087           .add(Src0);
5088       Src0.ChangeToRegister(Reg, false);
5089     }
5090     if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) {
5091       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5092       const DebugLoc &DL = MI.getDebugLoc();
5093       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5094           .add(Src1);
5095       Src1.ChangeToRegister(Reg, false);
5096     }
5097     return;
5098   }
5099 
5100   // No VOP2 instructions support AGPRs.
5101   if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg()))
5102     legalizeOpWithMove(MI, Src0Idx);
5103 
5104   if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg()))
5105     legalizeOpWithMove(MI, Src1Idx);
5106 
5107   // VOP2 src0 instructions support all operand types, so we don't need to check
5108   // their legality. If src1 is already legal, we don't need to do anything.
5109   if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1))
5110     return;
5111 
5112   // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for
5113   // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane
5114   // select is uniform.
5115   if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() &&
5116       RI.isVGPR(MRI, Src1.getReg())) {
5117     Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5118     const DebugLoc &DL = MI.getDebugLoc();
5119     BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5120         .add(Src1);
5121     Src1.ChangeToRegister(Reg, false);
5122     return;
5123   }
5124 
5125   // We do not use commuteInstruction here because it is too aggressive and will
5126   // commute if it is possible. We only want to commute here if it improves
5127   // legality. This can be called a fairly large number of times so don't waste
5128   // compile time pointlessly swapping and checking legality again.
5129   if (HasImplicitSGPR || !MI.isCommutable()) {
5130     legalizeOpWithMove(MI, Src1Idx);
5131     return;
5132   }
5133 
5134   // If src0 can be used as src1, commuting will make the operands legal.
5135   // Otherwise we have to give up and insert a move.
5136   //
5137   // TODO: Other immediate-like operand kinds could be commuted if there was a
5138   // MachineOperand::ChangeTo* for them.
5139   if ((!Src1.isImm() && !Src1.isReg()) ||
5140       !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) {
5141     legalizeOpWithMove(MI, Src1Idx);
5142     return;
5143   }
5144 
5145   int CommutedOpc = commuteOpcode(MI);
5146   if (CommutedOpc == -1) {
5147     legalizeOpWithMove(MI, Src1Idx);
5148     return;
5149   }
5150 
5151   MI.setDesc(get(CommutedOpc));
5152 
5153   Register Src0Reg = Src0.getReg();
5154   unsigned Src0SubReg = Src0.getSubReg();
5155   bool Src0Kill = Src0.isKill();
5156 
5157   if (Src1.isImm())
5158     Src0.ChangeToImmediate(Src1.getImm());
5159   else if (Src1.isReg()) {
5160     Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill());
5161     Src0.setSubReg(Src1.getSubReg());
5162   } else
5163     llvm_unreachable("Should only have register or immediate operands");
5164 
5165   Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill);
5166   Src1.setSubReg(Src0SubReg);
5167   fixImplicitOperands(MI);
5168 }
5169 
5170 // Legalize VOP3 operands. All operand types are supported for any operand
5171 // but only one literal constant and only starting from GFX10.
5172 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI,
5173                                        MachineInstr &MI) const {
5174   unsigned Opc = MI.getOpcode();
5175 
5176   int VOP3Idx[3] = {
5177     AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0),
5178     AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1),
5179     AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)
5180   };
5181 
5182   if (Opc == AMDGPU::V_PERMLANE16_B32_e64 ||
5183       Opc == AMDGPU::V_PERMLANEX16_B32_e64) {
5184     // src1 and src2 must be scalar
5185     MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]);
5186     MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]);
5187     const DebugLoc &DL = MI.getDebugLoc();
5188     if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) {
5189       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5190       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5191         .add(Src1);
5192       Src1.ChangeToRegister(Reg, false);
5193     }
5194     if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) {
5195       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5196       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5197         .add(Src2);
5198       Src2.ChangeToRegister(Reg, false);
5199     }
5200   }
5201 
5202   // Find the one SGPR operand we are allowed to use.
5203   int ConstantBusLimit = ST.getConstantBusLimit(Opc);
5204   int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0;
5205   SmallDenseSet<unsigned> SGPRsUsed;
5206   Register SGPRReg = findUsedSGPR(MI, VOP3Idx);
5207   if (SGPRReg != AMDGPU::NoRegister) {
5208     SGPRsUsed.insert(SGPRReg);
5209     --ConstantBusLimit;
5210   }
5211 
5212   for (int Idx : VOP3Idx) {
5213     if (Idx == -1)
5214       break;
5215     MachineOperand &MO = MI.getOperand(Idx);
5216 
5217     if (!MO.isReg()) {
5218       if (!isLiteralConstantLike(MO, get(Opc).OpInfo[Idx]))
5219         continue;
5220 
5221       if (LiteralLimit > 0 && ConstantBusLimit > 0) {
5222         --LiteralLimit;
5223         --ConstantBusLimit;
5224         continue;
5225       }
5226 
5227       --LiteralLimit;
5228       --ConstantBusLimit;
5229       legalizeOpWithMove(MI, Idx);
5230       continue;
5231     }
5232 
5233     if (RI.hasAGPRs(RI.getRegClassForReg(MRI, MO.getReg())) &&
5234         !isOperandLegal(MI, Idx, &MO)) {
5235       legalizeOpWithMove(MI, Idx);
5236       continue;
5237     }
5238 
5239     if (!RI.isSGPRClass(RI.getRegClassForReg(MRI, MO.getReg())))
5240       continue; // VGPRs are legal
5241 
5242     // We can use one SGPR in each VOP3 instruction prior to GFX10
5243     // and two starting from GFX10.
5244     if (SGPRsUsed.count(MO.getReg()))
5245       continue;
5246     if (ConstantBusLimit > 0) {
5247       SGPRsUsed.insert(MO.getReg());
5248       --ConstantBusLimit;
5249       continue;
5250     }
5251 
5252     // If we make it this far, then the operand is not legal and we must
5253     // legalize it.
5254     legalizeOpWithMove(MI, Idx);
5255   }
5256 }
5257 
5258 Register SIInstrInfo::readlaneVGPRToSGPR(Register SrcReg, MachineInstr &UseMI,
5259                                          MachineRegisterInfo &MRI) const {
5260   const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg);
5261   const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC);
5262   Register DstReg = MRI.createVirtualRegister(SRC);
5263   unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32;
5264 
5265   if (RI.hasAGPRs(VRC)) {
5266     VRC = RI.getEquivalentVGPRClass(VRC);
5267     Register NewSrcReg = MRI.createVirtualRegister(VRC);
5268     BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5269             get(TargetOpcode::COPY), NewSrcReg)
5270         .addReg(SrcReg);
5271     SrcReg = NewSrcReg;
5272   }
5273 
5274   if (SubRegs == 1) {
5275     BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5276             get(AMDGPU::V_READFIRSTLANE_B32), DstReg)
5277         .addReg(SrcReg);
5278     return DstReg;
5279   }
5280 
5281   SmallVector<unsigned, 8> SRegs;
5282   for (unsigned i = 0; i < SubRegs; ++i) {
5283     Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5284     BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5285             get(AMDGPU::V_READFIRSTLANE_B32), SGPR)
5286         .addReg(SrcReg, 0, RI.getSubRegFromChannel(i));
5287     SRegs.push_back(SGPR);
5288   }
5289 
5290   MachineInstrBuilder MIB =
5291       BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5292               get(AMDGPU::REG_SEQUENCE), DstReg);
5293   for (unsigned i = 0; i < SubRegs; ++i) {
5294     MIB.addReg(SRegs[i]);
5295     MIB.addImm(RI.getSubRegFromChannel(i));
5296   }
5297   return DstReg;
5298 }
5299 
5300 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI,
5301                                        MachineInstr &MI) const {
5302 
5303   // If the pointer is store in VGPRs, then we need to move them to
5304   // SGPRs using v_readfirstlane.  This is safe because we only select
5305   // loads with uniform pointers to SMRD instruction so we know the
5306   // pointer value is uniform.
5307   MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase);
5308   if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) {
5309     Register SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI);
5310     SBase->setReg(SGPR);
5311   }
5312   MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soff);
5313   if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) {
5314     Register SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI);
5315     SOff->setReg(SGPR);
5316   }
5317 }
5318 
5319 bool SIInstrInfo::moveFlatAddrToVGPR(MachineInstr &Inst) const {
5320   unsigned Opc = Inst.getOpcode();
5321   int OldSAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::saddr);
5322   if (OldSAddrIdx < 0)
5323     return false;
5324 
5325   assert(isSegmentSpecificFLAT(Inst));
5326 
5327   int NewOpc = AMDGPU::getGlobalVaddrOp(Opc);
5328   if (NewOpc < 0)
5329     NewOpc = AMDGPU::getFlatScratchInstSVfromSS(Opc);
5330   if (NewOpc < 0)
5331     return false;
5332 
5333   MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo();
5334   MachineOperand &SAddr = Inst.getOperand(OldSAddrIdx);
5335   if (RI.isSGPRReg(MRI, SAddr.getReg()))
5336     return false;
5337 
5338   int NewVAddrIdx = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vaddr);
5339   if (NewVAddrIdx < 0)
5340     return false;
5341 
5342   int OldVAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr);
5343 
5344   // Check vaddr, it shall be zero or absent.
5345   MachineInstr *VAddrDef = nullptr;
5346   if (OldVAddrIdx >= 0) {
5347     MachineOperand &VAddr = Inst.getOperand(OldVAddrIdx);
5348     VAddrDef = MRI.getUniqueVRegDef(VAddr.getReg());
5349     if (!VAddrDef || VAddrDef->getOpcode() != AMDGPU::V_MOV_B32_e32 ||
5350         !VAddrDef->getOperand(1).isImm() ||
5351         VAddrDef->getOperand(1).getImm() != 0)
5352       return false;
5353   }
5354 
5355   const MCInstrDesc &NewDesc = get(NewOpc);
5356   Inst.setDesc(NewDesc);
5357 
5358   // Callers expect iterator to be valid after this call, so modify the
5359   // instruction in place.
5360   if (OldVAddrIdx == NewVAddrIdx) {
5361     MachineOperand &NewVAddr = Inst.getOperand(NewVAddrIdx);
5362     // Clear use list from the old vaddr holding a zero register.
5363     MRI.removeRegOperandFromUseList(&NewVAddr);
5364     MRI.moveOperands(&NewVAddr, &SAddr, 1);
5365     Inst.removeOperand(OldSAddrIdx);
5366     // Update the use list with the pointer we have just moved from vaddr to
5367     // saddr position. Otherwise new vaddr will be missing from the use list.
5368     MRI.removeRegOperandFromUseList(&NewVAddr);
5369     MRI.addRegOperandToUseList(&NewVAddr);
5370   } else {
5371     assert(OldSAddrIdx == NewVAddrIdx);
5372 
5373     if (OldVAddrIdx >= 0) {
5374       int NewVDstIn = AMDGPU::getNamedOperandIdx(NewOpc,
5375                                                  AMDGPU::OpName::vdst_in);
5376 
5377       // removeOperand doesn't try to fixup tied operand indexes at it goes, so
5378       // it asserts. Untie the operands for now and retie them afterwards.
5379       if (NewVDstIn != -1) {
5380         int OldVDstIn = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst_in);
5381         Inst.untieRegOperand(OldVDstIn);
5382       }
5383 
5384       Inst.removeOperand(OldVAddrIdx);
5385 
5386       if (NewVDstIn != -1) {
5387         int NewVDst = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vdst);
5388         Inst.tieOperands(NewVDst, NewVDstIn);
5389       }
5390     }
5391   }
5392 
5393   if (VAddrDef && MRI.use_nodbg_empty(VAddrDef->getOperand(0).getReg()))
5394     VAddrDef->eraseFromParent();
5395 
5396   return true;
5397 }
5398 
5399 // FIXME: Remove this when SelectionDAG is obsoleted.
5400 void SIInstrInfo::legalizeOperandsFLAT(MachineRegisterInfo &MRI,
5401                                        MachineInstr &MI) const {
5402   if (!isSegmentSpecificFLAT(MI))
5403     return;
5404 
5405   // Fixup SGPR operands in VGPRs. We only select these when the DAG divergence
5406   // thinks they are uniform, so a readfirstlane should be valid.
5407   MachineOperand *SAddr = getNamedOperand(MI, AMDGPU::OpName::saddr);
5408   if (!SAddr || RI.isSGPRClass(MRI.getRegClass(SAddr->getReg())))
5409     return;
5410 
5411   if (moveFlatAddrToVGPR(MI))
5412     return;
5413 
5414   Register ToSGPR = readlaneVGPRToSGPR(SAddr->getReg(), MI, MRI);
5415   SAddr->setReg(ToSGPR);
5416 }
5417 
5418 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB,
5419                                          MachineBasicBlock::iterator I,
5420                                          const TargetRegisterClass *DstRC,
5421                                          MachineOperand &Op,
5422                                          MachineRegisterInfo &MRI,
5423                                          const DebugLoc &DL) const {
5424   Register OpReg = Op.getReg();
5425   unsigned OpSubReg = Op.getSubReg();
5426 
5427   const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg(
5428       RI.getRegClassForReg(MRI, OpReg), OpSubReg);
5429 
5430   // Check if operand is already the correct register class.
5431   if (DstRC == OpRC)
5432     return;
5433 
5434   Register DstReg = MRI.createVirtualRegister(DstRC);
5435   auto Copy = BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op);
5436 
5437   Op.setReg(DstReg);
5438   Op.setSubReg(0);
5439 
5440   MachineInstr *Def = MRI.getVRegDef(OpReg);
5441   if (!Def)
5442     return;
5443 
5444   // Try to eliminate the copy if it is copying an immediate value.
5445   if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass)
5446     FoldImmediate(*Copy, *Def, OpReg, &MRI);
5447 
5448   bool ImpDef = Def->isImplicitDef();
5449   while (!ImpDef && Def && Def->isCopy()) {
5450     if (Def->getOperand(1).getReg().isPhysical())
5451       break;
5452     Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg());
5453     ImpDef = Def && Def->isImplicitDef();
5454   }
5455   if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) &&
5456       !ImpDef)
5457     Copy.addReg(AMDGPU::EXEC, RegState::Implicit);
5458 }
5459 
5460 // Emit the actual waterfall loop, executing the wrapped instruction for each
5461 // unique value of \p Rsrc across all lanes. In the best case we execute 1
5462 // iteration, in the worst case we execute 64 (once per lane).
5463 static void
5464 emitLoadSRsrcFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI,
5465                           MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB,
5466                           const DebugLoc &DL, MachineOperand &Rsrc) {
5467   MachineFunction &MF = *OrigBB.getParent();
5468   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
5469   const SIRegisterInfo *TRI = ST.getRegisterInfo();
5470   unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5471   unsigned SaveExecOpc =
5472       ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64;
5473   unsigned XorTermOpc =
5474       ST.isWave32() ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term;
5475   unsigned AndOpc =
5476       ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
5477   const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
5478 
5479   MachineBasicBlock::iterator I = LoopBB.begin();
5480 
5481   SmallVector<Register, 8> ReadlanePieces;
5482   Register CondReg = AMDGPU::NoRegister;
5483 
5484   Register VRsrc = Rsrc.getReg();
5485   unsigned VRsrcUndef = getUndefRegState(Rsrc.isUndef());
5486 
5487   unsigned RegSize = TRI->getRegSizeInBits(Rsrc.getReg(), MRI);
5488   unsigned NumSubRegs =  RegSize / 32;
5489   assert(NumSubRegs % 2 == 0 && NumSubRegs <= 32 && "Unhandled register size");
5490 
5491   for (unsigned Idx = 0; Idx < NumSubRegs; Idx += 2) {
5492 
5493     Register CurRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5494     Register CurRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5495 
5496     // Read the next variant <- also loop target.
5497     BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegLo)
5498             .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx));
5499 
5500     // Read the next variant <- also loop target.
5501     BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegHi)
5502             .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx + 1));
5503 
5504     ReadlanePieces.push_back(CurRegLo);
5505     ReadlanePieces.push_back(CurRegHi);
5506 
5507     // Comparison is to be done as 64-bit.
5508     Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
5509     BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), CurReg)
5510             .addReg(CurRegLo)
5511             .addImm(AMDGPU::sub0)
5512             .addReg(CurRegHi)
5513             .addImm(AMDGPU::sub1);
5514 
5515     Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC);
5516     auto Cmp =
5517         BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), NewCondReg)
5518             .addReg(CurReg);
5519     if (NumSubRegs <= 2)
5520       Cmp.addReg(VRsrc);
5521     else
5522       Cmp.addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx, 2));
5523 
5524     // Combine the comparison results with AND.
5525     if (CondReg == AMDGPU::NoRegister) // First.
5526       CondReg = NewCondReg;
5527     else { // If not the first, we create an AND.
5528       Register AndReg = MRI.createVirtualRegister(BoolXExecRC);
5529       BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndReg)
5530               .addReg(CondReg)
5531               .addReg(NewCondReg);
5532       CondReg = AndReg;
5533     }
5534   } // End for loop.
5535 
5536   auto SRsrcRC = TRI->getEquivalentSGPRClass(MRI.getRegClass(VRsrc));
5537   Register SRsrc = MRI.createVirtualRegister(SRsrcRC);
5538 
5539   // Build scalar Rsrc.
5540   auto Merge = BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SRsrc);
5541   unsigned Channel = 0;
5542   for (Register Piece : ReadlanePieces) {
5543     Merge.addReg(Piece)
5544          .addImm(TRI->getSubRegFromChannel(Channel++));
5545   }
5546 
5547   // Update Rsrc operand to use the SGPR Rsrc.
5548   Rsrc.setReg(SRsrc);
5549   Rsrc.setIsKill(true);
5550 
5551   Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
5552   MRI.setSimpleHint(SaveExec, CondReg);
5553 
5554   // Update EXEC to matching lanes, saving original to SaveExec.
5555   BuildMI(LoopBB, I, DL, TII.get(SaveExecOpc), SaveExec)
5556       .addReg(CondReg, RegState::Kill);
5557 
5558   // The original instruction is here; we insert the terminators after it.
5559   I = LoopBB.end();
5560 
5561   // Update EXEC, switch all done bits to 0 and all todo bits to 1.
5562   BuildMI(LoopBB, I, DL, TII.get(XorTermOpc), Exec)
5563       .addReg(Exec)
5564       .addReg(SaveExec);
5565 
5566   BuildMI(LoopBB, I, DL, TII.get(AMDGPU::SI_WATERFALL_LOOP)).addMBB(&LoopBB);
5567 }
5568 
5569 // Build a waterfall loop around \p MI, replacing the VGPR \p Rsrc register
5570 // with SGPRs by iterating over all unique values across all lanes.
5571 // Returns the loop basic block that now contains \p MI.
5572 static MachineBasicBlock *
5573 loadSRsrcFromVGPR(const SIInstrInfo &TII, MachineInstr &MI,
5574                   MachineOperand &Rsrc, MachineDominatorTree *MDT,
5575                   MachineBasicBlock::iterator Begin = nullptr,
5576                   MachineBasicBlock::iterator End = nullptr) {
5577   MachineBasicBlock &MBB = *MI.getParent();
5578   MachineFunction &MF = *MBB.getParent();
5579   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
5580   const SIRegisterInfo *TRI = ST.getRegisterInfo();
5581   MachineRegisterInfo &MRI = MF.getRegInfo();
5582   if (!Begin.isValid())
5583     Begin = &MI;
5584   if (!End.isValid()) {
5585     End = &MI;
5586     ++End;
5587   }
5588   const DebugLoc &DL = MI.getDebugLoc();
5589   unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5590   unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
5591   const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
5592 
5593   Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
5594 
5595   // Save the EXEC mask
5596   BuildMI(MBB, Begin, DL, TII.get(MovExecOpc), SaveExec).addReg(Exec);
5597 
5598   // Killed uses in the instruction we are waterfalling around will be
5599   // incorrect due to the added control-flow.
5600   MachineBasicBlock::iterator AfterMI = MI;
5601   ++AfterMI;
5602   for (auto I = Begin; I != AfterMI; I++) {
5603     for (auto &MO : I->uses()) {
5604       if (MO.isReg() && MO.isUse()) {
5605         MRI.clearKillFlags(MO.getReg());
5606       }
5607     }
5608   }
5609 
5610   // To insert the loop we need to split the block. Move everything after this
5611   // point to a new block, and insert a new empty block between the two.
5612   MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock();
5613   MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock();
5614   MachineFunction::iterator MBBI(MBB);
5615   ++MBBI;
5616 
5617   MF.insert(MBBI, LoopBB);
5618   MF.insert(MBBI, RemainderBB);
5619 
5620   LoopBB->addSuccessor(LoopBB);
5621   LoopBB->addSuccessor(RemainderBB);
5622 
5623   // Move Begin to MI to the LoopBB, and the remainder of the block to
5624   // RemainderBB.
5625   RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
5626   RemainderBB->splice(RemainderBB->begin(), &MBB, End, MBB.end());
5627   LoopBB->splice(LoopBB->begin(), &MBB, Begin, MBB.end());
5628 
5629   MBB.addSuccessor(LoopBB);
5630 
5631   // Update dominators. We know that MBB immediately dominates LoopBB, that
5632   // LoopBB immediately dominates RemainderBB, and that RemainderBB immediately
5633   // dominates all of the successors transferred to it from MBB that MBB used
5634   // to properly dominate.
5635   if (MDT) {
5636     MDT->addNewBlock(LoopBB, &MBB);
5637     MDT->addNewBlock(RemainderBB, LoopBB);
5638     for (auto &Succ : RemainderBB->successors()) {
5639       if (MDT->properlyDominates(&MBB, Succ)) {
5640         MDT->changeImmediateDominator(Succ, RemainderBB);
5641       }
5642     }
5643   }
5644 
5645   emitLoadSRsrcFromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, Rsrc);
5646 
5647   // Restore the EXEC mask
5648   MachineBasicBlock::iterator First = RemainderBB->begin();
5649   BuildMI(*RemainderBB, First, DL, TII.get(MovExecOpc), Exec).addReg(SaveExec);
5650   return LoopBB;
5651 }
5652 
5653 // Extract pointer from Rsrc and return a zero-value Rsrc replacement.
5654 static std::tuple<unsigned, unsigned>
5655 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) {
5656   MachineBasicBlock &MBB = *MI.getParent();
5657   MachineFunction &MF = *MBB.getParent();
5658   MachineRegisterInfo &MRI = MF.getRegInfo();
5659 
5660   // Extract the ptr from the resource descriptor.
5661   unsigned RsrcPtr =
5662       TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass,
5663                              AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass);
5664 
5665   // Create an empty resource descriptor
5666   Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
5667   Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5668   Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5669   Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass);
5670   uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat();
5671 
5672   // Zero64 = 0
5673   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64)
5674       .addImm(0);
5675 
5676   // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
5677   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo)
5678       .addImm(RsrcDataFormat & 0xFFFFFFFF);
5679 
5680   // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
5681   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi)
5682       .addImm(RsrcDataFormat >> 32);
5683 
5684   // NewSRsrc = {Zero64, SRsrcFormat}
5685   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc)
5686       .addReg(Zero64)
5687       .addImm(AMDGPU::sub0_sub1)
5688       .addReg(SRsrcFormatLo)
5689       .addImm(AMDGPU::sub2)
5690       .addReg(SRsrcFormatHi)
5691       .addImm(AMDGPU::sub3);
5692 
5693   return std::make_tuple(RsrcPtr, NewSRsrc);
5694 }
5695 
5696 MachineBasicBlock *
5697 SIInstrInfo::legalizeOperands(MachineInstr &MI,
5698                               MachineDominatorTree *MDT) const {
5699   MachineFunction &MF = *MI.getParent()->getParent();
5700   MachineRegisterInfo &MRI = MF.getRegInfo();
5701   MachineBasicBlock *CreatedBB = nullptr;
5702 
5703   // Legalize VOP2
5704   if (isVOP2(MI) || isVOPC(MI)) {
5705     legalizeOperandsVOP2(MRI, MI);
5706     return CreatedBB;
5707   }
5708 
5709   // Legalize VOP3
5710   if (isVOP3(MI)) {
5711     legalizeOperandsVOP3(MRI, MI);
5712     return CreatedBB;
5713   }
5714 
5715   // Legalize SMRD
5716   if (isSMRD(MI)) {
5717     legalizeOperandsSMRD(MRI, MI);
5718     return CreatedBB;
5719   }
5720 
5721   // Legalize FLAT
5722   if (isFLAT(MI)) {
5723     legalizeOperandsFLAT(MRI, MI);
5724     return CreatedBB;
5725   }
5726 
5727   // Legalize REG_SEQUENCE and PHI
5728   // The register class of the operands much be the same type as the register
5729   // class of the output.
5730   if (MI.getOpcode() == AMDGPU::PHI) {
5731     const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr;
5732     for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
5733       if (!MI.getOperand(i).isReg() || !MI.getOperand(i).getReg().isVirtual())
5734         continue;
5735       const TargetRegisterClass *OpRC =
5736           MRI.getRegClass(MI.getOperand(i).getReg());
5737       if (RI.hasVectorRegisters(OpRC)) {
5738         VRC = OpRC;
5739       } else {
5740         SRC = OpRC;
5741       }
5742     }
5743 
5744     // If any of the operands are VGPR registers, then they all most be
5745     // otherwise we will create illegal VGPR->SGPR copies when legalizing
5746     // them.
5747     if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) {
5748       if (!VRC) {
5749         assert(SRC);
5750         if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) {
5751           VRC = &AMDGPU::VReg_1RegClass;
5752         } else
5753           VRC = RI.isAGPRClass(getOpRegClass(MI, 0))
5754                     ? RI.getEquivalentAGPRClass(SRC)
5755                     : RI.getEquivalentVGPRClass(SRC);
5756       } else {
5757         VRC = RI.isAGPRClass(getOpRegClass(MI, 0))
5758                   ? RI.getEquivalentAGPRClass(VRC)
5759                   : RI.getEquivalentVGPRClass(VRC);
5760       }
5761       RC = VRC;
5762     } else {
5763       RC = SRC;
5764     }
5765 
5766     // Update all the operands so they have the same type.
5767     for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
5768       MachineOperand &Op = MI.getOperand(I);
5769       if (!Op.isReg() || !Op.getReg().isVirtual())
5770         continue;
5771 
5772       // MI is a PHI instruction.
5773       MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB();
5774       MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator();
5775 
5776       // Avoid creating no-op copies with the same src and dst reg class.  These
5777       // confuse some of the machine passes.
5778       legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc());
5779     }
5780   }
5781 
5782   // REG_SEQUENCE doesn't really require operand legalization, but if one has a
5783   // VGPR dest type and SGPR sources, insert copies so all operands are
5784   // VGPRs. This seems to help operand folding / the register coalescer.
5785   if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) {
5786     MachineBasicBlock *MBB = MI.getParent();
5787     const TargetRegisterClass *DstRC = getOpRegClass(MI, 0);
5788     if (RI.hasVGPRs(DstRC)) {
5789       // Update all the operands so they are VGPR register classes. These may
5790       // not be the same register class because REG_SEQUENCE supports mixing
5791       // subregister index types e.g. sub0_sub1 + sub2 + sub3
5792       for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
5793         MachineOperand &Op = MI.getOperand(I);
5794         if (!Op.isReg() || !Op.getReg().isVirtual())
5795           continue;
5796 
5797         const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg());
5798         const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC);
5799         if (VRC == OpRC)
5800           continue;
5801 
5802         legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc());
5803         Op.setIsKill();
5804       }
5805     }
5806 
5807     return CreatedBB;
5808   }
5809 
5810   // Legalize INSERT_SUBREG
5811   // src0 must have the same register class as dst
5812   if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) {
5813     Register Dst = MI.getOperand(0).getReg();
5814     Register Src0 = MI.getOperand(1).getReg();
5815     const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
5816     const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0);
5817     if (DstRC != Src0RC) {
5818       MachineBasicBlock *MBB = MI.getParent();
5819       MachineOperand &Op = MI.getOperand(1);
5820       legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc());
5821     }
5822     return CreatedBB;
5823   }
5824 
5825   // Legalize SI_INIT_M0
5826   if (MI.getOpcode() == AMDGPU::SI_INIT_M0) {
5827     MachineOperand &Src = MI.getOperand(0);
5828     if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg())))
5829       Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI));
5830     return CreatedBB;
5831   }
5832 
5833   // Legalize MIMG and MUBUF/MTBUF for shaders.
5834   //
5835   // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via
5836   // scratch memory access. In both cases, the legalization never involves
5837   // conversion to the addr64 form.
5838   if (isMIMG(MI) || (AMDGPU::isGraphics(MF.getFunction().getCallingConv()) &&
5839                      (isMUBUF(MI) || isMTBUF(MI)))) {
5840     MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc);
5841     if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg())))
5842       CreatedBB = loadSRsrcFromVGPR(*this, MI, *SRsrc, MDT);
5843 
5844     MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp);
5845     if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg())))
5846       CreatedBB = loadSRsrcFromVGPR(*this, MI, *SSamp, MDT);
5847 
5848     return CreatedBB;
5849   }
5850 
5851   // Legalize SI_CALL
5852   if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) {
5853     MachineOperand *Dest = &MI.getOperand(0);
5854     if (!RI.isSGPRClass(MRI.getRegClass(Dest->getReg()))) {
5855       // Move everything between ADJCALLSTACKUP and ADJCALLSTACKDOWN and
5856       // following copies, we also need to move copies from and to physical
5857       // registers into the loop block.
5858       unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
5859       unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
5860 
5861       // Also move the copies to physical registers into the loop block
5862       MachineBasicBlock &MBB = *MI.getParent();
5863       MachineBasicBlock::iterator Start(&MI);
5864       while (Start->getOpcode() != FrameSetupOpcode)
5865         --Start;
5866       MachineBasicBlock::iterator End(&MI);
5867       while (End->getOpcode() != FrameDestroyOpcode)
5868         ++End;
5869       // Also include following copies of the return value
5870       ++End;
5871       while (End != MBB.end() && End->isCopy() && End->getOperand(1).isReg() &&
5872              MI.definesRegister(End->getOperand(1).getReg()))
5873         ++End;
5874       CreatedBB = loadSRsrcFromVGPR(*this, MI, *Dest, MDT, Start, End);
5875     }
5876   }
5877 
5878   // Legalize MUBUF* instructions.
5879   int RsrcIdx =
5880       AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc);
5881   if (RsrcIdx != -1) {
5882     // We have an MUBUF instruction
5883     MachineOperand *Rsrc = &MI.getOperand(RsrcIdx);
5884     unsigned RsrcRC = get(MI.getOpcode()).OpInfo[RsrcIdx].RegClass;
5885     if (RI.getCommonSubClass(MRI.getRegClass(Rsrc->getReg()),
5886                              RI.getRegClass(RsrcRC))) {
5887       // The operands are legal.
5888       // FIXME: We may need to legalize operands besides srsrc.
5889       return CreatedBB;
5890     }
5891 
5892     // Legalize a VGPR Rsrc.
5893     //
5894     // If the instruction is _ADDR64, we can avoid a waterfall by extracting
5895     // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using
5896     // a zero-value SRsrc.
5897     //
5898     // If the instruction is _OFFSET (both idxen and offen disabled), and we
5899     // support ADDR64 instructions, we can convert to ADDR64 and do the same as
5900     // above.
5901     //
5902     // Otherwise we are on non-ADDR64 hardware, and/or we have
5903     // idxen/offen/bothen and we fall back to a waterfall loop.
5904 
5905     MachineBasicBlock &MBB = *MI.getParent();
5906 
5907     MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
5908     if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) {
5909       // This is already an ADDR64 instruction so we need to add the pointer
5910       // extracted from the resource descriptor to the current value of VAddr.
5911       Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5912       Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5913       Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
5914 
5915       const auto *BoolXExecRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
5916       Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC);
5917       Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC);
5918 
5919       unsigned RsrcPtr, NewSRsrc;
5920       std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
5921 
5922       // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0
5923       const DebugLoc &DL = MI.getDebugLoc();
5924       BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_CO_U32_e64), NewVAddrLo)
5925         .addDef(CondReg0)
5926         .addReg(RsrcPtr, 0, AMDGPU::sub0)
5927         .addReg(VAddr->getReg(), 0, AMDGPU::sub0)
5928         .addImm(0);
5929 
5930       // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1
5931       BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi)
5932         .addDef(CondReg1, RegState::Dead)
5933         .addReg(RsrcPtr, 0, AMDGPU::sub1)
5934         .addReg(VAddr->getReg(), 0, AMDGPU::sub1)
5935         .addReg(CondReg0, RegState::Kill)
5936         .addImm(0);
5937 
5938       // NewVaddr = {NewVaddrHi, NewVaddrLo}
5939       BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr)
5940           .addReg(NewVAddrLo)
5941           .addImm(AMDGPU::sub0)
5942           .addReg(NewVAddrHi)
5943           .addImm(AMDGPU::sub1);
5944 
5945       VAddr->setReg(NewVAddr);
5946       Rsrc->setReg(NewSRsrc);
5947     } else if (!VAddr && ST.hasAddr64()) {
5948       // This instructions is the _OFFSET variant, so we need to convert it to
5949       // ADDR64.
5950       assert(ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS &&
5951              "FIXME: Need to emit flat atomics here");
5952 
5953       unsigned RsrcPtr, NewSRsrc;
5954       std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
5955 
5956       Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
5957       MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata);
5958       MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
5959       MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset);
5960       unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode());
5961 
5962       // Atomics with return have an additional tied operand and are
5963       // missing some of the special bits.
5964       MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in);
5965       MachineInstr *Addr64;
5966 
5967       if (!VDataIn) {
5968         // Regular buffer load / store.
5969         MachineInstrBuilder MIB =
5970             BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
5971                 .add(*VData)
5972                 .addReg(NewVAddr)
5973                 .addReg(NewSRsrc)
5974                 .add(*SOffset)
5975                 .add(*Offset);
5976 
5977         if (const MachineOperand *CPol =
5978                 getNamedOperand(MI, AMDGPU::OpName::cpol)) {
5979           MIB.addImm(CPol->getImm());
5980         }
5981 
5982         if (const MachineOperand *TFE =
5983                 getNamedOperand(MI, AMDGPU::OpName::tfe)) {
5984           MIB.addImm(TFE->getImm());
5985         }
5986 
5987         MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz));
5988 
5989         MIB.cloneMemRefs(MI);
5990         Addr64 = MIB;
5991       } else {
5992         // Atomics with return.
5993         Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
5994                      .add(*VData)
5995                      .add(*VDataIn)
5996                      .addReg(NewVAddr)
5997                      .addReg(NewSRsrc)
5998                      .add(*SOffset)
5999                      .add(*Offset)
6000                      .addImm(getNamedImmOperand(MI, AMDGPU::OpName::cpol))
6001                      .cloneMemRefs(MI);
6002       }
6003 
6004       MI.removeFromParent();
6005 
6006       // NewVaddr = {NewVaddrHi, NewVaddrLo}
6007       BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
6008               NewVAddr)
6009           .addReg(RsrcPtr, 0, AMDGPU::sub0)
6010           .addImm(AMDGPU::sub0)
6011           .addReg(RsrcPtr, 0, AMDGPU::sub1)
6012           .addImm(AMDGPU::sub1);
6013     } else {
6014       // This is another variant; legalize Rsrc with waterfall loop from VGPRs
6015       // to SGPRs.
6016       CreatedBB = loadSRsrcFromVGPR(*this, MI, *Rsrc, MDT);
6017       return CreatedBB;
6018     }
6019   }
6020   return CreatedBB;
6021 }
6022 
6023 MachineBasicBlock *SIInstrInfo::moveToVALU(MachineInstr &TopInst,
6024                                            MachineDominatorTree *MDT) const {
6025   SetVectorType Worklist;
6026   Worklist.insert(&TopInst);
6027   MachineBasicBlock *CreatedBB = nullptr;
6028   MachineBasicBlock *CreatedBBTmp = nullptr;
6029 
6030   while (!Worklist.empty()) {
6031     MachineInstr &Inst = *Worklist.pop_back_val();
6032     MachineBasicBlock *MBB = Inst.getParent();
6033     MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
6034 
6035     unsigned Opcode = Inst.getOpcode();
6036     unsigned NewOpcode = getVALUOp(Inst);
6037 
6038     // Handle some special cases
6039     switch (Opcode) {
6040     default:
6041       break;
6042     case AMDGPU::S_ADD_U64_PSEUDO:
6043     case AMDGPU::S_SUB_U64_PSEUDO:
6044       splitScalar64BitAddSub(Worklist, Inst, MDT);
6045       Inst.eraseFromParent();
6046       continue;
6047     case AMDGPU::S_ADD_I32:
6048     case AMDGPU::S_SUB_I32: {
6049       // FIXME: The u32 versions currently selected use the carry.
6050       bool Changed;
6051       std::tie(Changed, CreatedBBTmp) = moveScalarAddSub(Worklist, Inst, MDT);
6052       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6053         CreatedBB = CreatedBBTmp;
6054       if (Changed)
6055         continue;
6056 
6057       // Default handling
6058       break;
6059     }
6060     case AMDGPU::S_AND_B64:
6061       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT);
6062       Inst.eraseFromParent();
6063       continue;
6064 
6065     case AMDGPU::S_OR_B64:
6066       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT);
6067       Inst.eraseFromParent();
6068       continue;
6069 
6070     case AMDGPU::S_XOR_B64:
6071       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT);
6072       Inst.eraseFromParent();
6073       continue;
6074 
6075     case AMDGPU::S_NAND_B64:
6076       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT);
6077       Inst.eraseFromParent();
6078       continue;
6079 
6080     case AMDGPU::S_NOR_B64:
6081       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT);
6082       Inst.eraseFromParent();
6083       continue;
6084 
6085     case AMDGPU::S_XNOR_B64:
6086       if (ST.hasDLInsts())
6087         splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT);
6088       else
6089         splitScalar64BitXnor(Worklist, Inst, MDT);
6090       Inst.eraseFromParent();
6091       continue;
6092 
6093     case AMDGPU::S_ANDN2_B64:
6094       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT);
6095       Inst.eraseFromParent();
6096       continue;
6097 
6098     case AMDGPU::S_ORN2_B64:
6099       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT);
6100       Inst.eraseFromParent();
6101       continue;
6102 
6103     case AMDGPU::S_BREV_B64:
6104       splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_BREV_B32, true);
6105       Inst.eraseFromParent();
6106       continue;
6107 
6108     case AMDGPU::S_NOT_B64:
6109       splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32);
6110       Inst.eraseFromParent();
6111       continue;
6112 
6113     case AMDGPU::S_BCNT1_I32_B64:
6114       splitScalar64BitBCNT(Worklist, Inst);
6115       Inst.eraseFromParent();
6116       continue;
6117 
6118     case AMDGPU::S_BFE_I64:
6119       splitScalar64BitBFE(Worklist, Inst);
6120       Inst.eraseFromParent();
6121       continue;
6122 
6123     case AMDGPU::S_LSHL_B32:
6124       if (ST.hasOnlyRevVALUShifts()) {
6125         NewOpcode = AMDGPU::V_LSHLREV_B32_e64;
6126         swapOperands(Inst);
6127       }
6128       break;
6129     case AMDGPU::S_ASHR_I32:
6130       if (ST.hasOnlyRevVALUShifts()) {
6131         NewOpcode = AMDGPU::V_ASHRREV_I32_e64;
6132         swapOperands(Inst);
6133       }
6134       break;
6135     case AMDGPU::S_LSHR_B32:
6136       if (ST.hasOnlyRevVALUShifts()) {
6137         NewOpcode = AMDGPU::V_LSHRREV_B32_e64;
6138         swapOperands(Inst);
6139       }
6140       break;
6141     case AMDGPU::S_LSHL_B64:
6142       if (ST.hasOnlyRevVALUShifts()) {
6143         NewOpcode = AMDGPU::V_LSHLREV_B64_e64;
6144         swapOperands(Inst);
6145       }
6146       break;
6147     case AMDGPU::S_ASHR_I64:
6148       if (ST.hasOnlyRevVALUShifts()) {
6149         NewOpcode = AMDGPU::V_ASHRREV_I64_e64;
6150         swapOperands(Inst);
6151       }
6152       break;
6153     case AMDGPU::S_LSHR_B64:
6154       if (ST.hasOnlyRevVALUShifts()) {
6155         NewOpcode = AMDGPU::V_LSHRREV_B64_e64;
6156         swapOperands(Inst);
6157       }
6158       break;
6159 
6160     case AMDGPU::S_ABS_I32:
6161       lowerScalarAbs(Worklist, Inst);
6162       Inst.eraseFromParent();
6163       continue;
6164 
6165     case AMDGPU::S_CBRANCH_SCC0:
6166     case AMDGPU::S_CBRANCH_SCC1: {
6167         // Clear unused bits of vcc
6168         Register CondReg = Inst.getOperand(1).getReg();
6169         bool IsSCC = CondReg == AMDGPU::SCC;
6170         Register VCC = RI.getVCC();
6171         Register EXEC = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
6172         unsigned Opc = ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
6173         BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(Opc), VCC)
6174             .addReg(EXEC)
6175             .addReg(IsSCC ? VCC : CondReg);
6176         Inst.removeOperand(1);
6177       }
6178       break;
6179 
6180     case AMDGPU::S_BFE_U64:
6181     case AMDGPU::S_BFM_B64:
6182       llvm_unreachable("Moving this op to VALU not implemented");
6183 
6184     case AMDGPU::S_PACK_LL_B32_B16:
6185     case AMDGPU::S_PACK_LH_B32_B16:
6186     case AMDGPU::S_PACK_HH_B32_B16:
6187       movePackToVALU(Worklist, MRI, Inst);
6188       Inst.eraseFromParent();
6189       continue;
6190 
6191     case AMDGPU::S_XNOR_B32:
6192       lowerScalarXnor(Worklist, Inst);
6193       Inst.eraseFromParent();
6194       continue;
6195 
6196     case AMDGPU::S_NAND_B32:
6197       splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32);
6198       Inst.eraseFromParent();
6199       continue;
6200 
6201     case AMDGPU::S_NOR_B32:
6202       splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32);
6203       Inst.eraseFromParent();
6204       continue;
6205 
6206     case AMDGPU::S_ANDN2_B32:
6207       splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32);
6208       Inst.eraseFromParent();
6209       continue;
6210 
6211     case AMDGPU::S_ORN2_B32:
6212       splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32);
6213       Inst.eraseFromParent();
6214       continue;
6215 
6216     // TODO: remove as soon as everything is ready
6217     // to replace VGPR to SGPR copy with V_READFIRSTLANEs.
6218     // S_ADD/SUB_CO_PSEUDO as well as S_UADDO/USUBO_PSEUDO
6219     // can only be selected from the uniform SDNode.
6220     case AMDGPU::S_ADD_CO_PSEUDO:
6221     case AMDGPU::S_SUB_CO_PSEUDO: {
6222       unsigned Opc = (Inst.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO)
6223                          ? AMDGPU::V_ADDC_U32_e64
6224                          : AMDGPU::V_SUBB_U32_e64;
6225       const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
6226 
6227       Register CarryInReg = Inst.getOperand(4).getReg();
6228       if (!MRI.constrainRegClass(CarryInReg, CarryRC)) {
6229         Register NewCarryReg = MRI.createVirtualRegister(CarryRC);
6230         BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(AMDGPU::COPY), NewCarryReg)
6231             .addReg(CarryInReg);
6232       }
6233 
6234       Register CarryOutReg = Inst.getOperand(1).getReg();
6235 
6236       Register DestReg = MRI.createVirtualRegister(RI.getEquivalentVGPRClass(
6237           MRI.getRegClass(Inst.getOperand(0).getReg())));
6238       MachineInstr *CarryOp =
6239           BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(Opc), DestReg)
6240               .addReg(CarryOutReg, RegState::Define)
6241               .add(Inst.getOperand(2))
6242               .add(Inst.getOperand(3))
6243               .addReg(CarryInReg)
6244               .addImm(0);
6245       CreatedBBTmp = legalizeOperands(*CarryOp);
6246       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6247         CreatedBB = CreatedBBTmp;
6248       MRI.replaceRegWith(Inst.getOperand(0).getReg(), DestReg);
6249       addUsersToMoveToVALUWorklist(DestReg, MRI, Worklist);
6250       Inst.eraseFromParent();
6251     }
6252       continue;
6253     case AMDGPU::S_UADDO_PSEUDO:
6254     case AMDGPU::S_USUBO_PSEUDO: {
6255       const DebugLoc &DL = Inst.getDebugLoc();
6256       MachineOperand &Dest0 = Inst.getOperand(0);
6257       MachineOperand &Dest1 = Inst.getOperand(1);
6258       MachineOperand &Src0 = Inst.getOperand(2);
6259       MachineOperand &Src1 = Inst.getOperand(3);
6260 
6261       unsigned Opc = (Inst.getOpcode() == AMDGPU::S_UADDO_PSEUDO)
6262                          ? AMDGPU::V_ADD_CO_U32_e64
6263                          : AMDGPU::V_SUB_CO_U32_e64;
6264       const TargetRegisterClass *NewRC =
6265           RI.getEquivalentVGPRClass(MRI.getRegClass(Dest0.getReg()));
6266       Register DestReg = MRI.createVirtualRegister(NewRC);
6267       MachineInstr *NewInstr = BuildMI(*MBB, &Inst, DL, get(Opc), DestReg)
6268                                    .addReg(Dest1.getReg(), RegState::Define)
6269                                    .add(Src0)
6270                                    .add(Src1)
6271                                    .addImm(0); // clamp bit
6272 
6273       CreatedBBTmp = legalizeOperands(*NewInstr, MDT);
6274       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6275         CreatedBB = CreatedBBTmp;
6276 
6277       MRI.replaceRegWith(Dest0.getReg(), DestReg);
6278       addUsersToMoveToVALUWorklist(NewInstr->getOperand(0).getReg(), MRI,
6279                                    Worklist);
6280       Inst.eraseFromParent();
6281     }
6282       continue;
6283 
6284     case AMDGPU::S_CSELECT_B32:
6285     case AMDGPU::S_CSELECT_B64:
6286       lowerSelect(Worklist, Inst, MDT);
6287       Inst.eraseFromParent();
6288       continue;
6289     case AMDGPU::S_CMP_EQ_I32:
6290     case AMDGPU::S_CMP_LG_I32:
6291     case AMDGPU::S_CMP_GT_I32:
6292     case AMDGPU::S_CMP_GE_I32:
6293     case AMDGPU::S_CMP_LT_I32:
6294     case AMDGPU::S_CMP_LE_I32:
6295     case AMDGPU::S_CMP_EQ_U32:
6296     case AMDGPU::S_CMP_LG_U32:
6297     case AMDGPU::S_CMP_GT_U32:
6298     case AMDGPU::S_CMP_GE_U32:
6299     case AMDGPU::S_CMP_LT_U32:
6300     case AMDGPU::S_CMP_LE_U32:
6301     case AMDGPU::S_CMP_EQ_U64:
6302     case AMDGPU::S_CMP_LG_U64: {
6303         const MCInstrDesc &NewDesc = get(NewOpcode);
6304         Register CondReg = MRI.createVirtualRegister(RI.getWaveMaskRegClass());
6305         MachineInstr *NewInstr =
6306             BuildMI(*MBB, Inst, Inst.getDebugLoc(), NewDesc, CondReg)
6307                 .add(Inst.getOperand(0))
6308                 .add(Inst.getOperand(1));
6309         legalizeOperands(*NewInstr, MDT);
6310         int SCCIdx = Inst.findRegisterDefOperandIdx(AMDGPU::SCC);
6311         MachineOperand SCCOp = Inst.getOperand(SCCIdx);
6312         addSCCDefUsersToVALUWorklist(SCCOp, Inst, Worklist, CondReg);
6313         Inst.eraseFromParent();
6314       }
6315       continue;
6316     }
6317 
6318 
6319     if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
6320       // We cannot move this instruction to the VALU, so we should try to
6321       // legalize its operands instead.
6322       CreatedBBTmp = legalizeOperands(Inst, MDT);
6323       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6324         CreatedBB = CreatedBBTmp;
6325       continue;
6326     }
6327 
6328     // Use the new VALU Opcode.
6329     const MCInstrDesc &NewDesc = get(NewOpcode);
6330     Inst.setDesc(NewDesc);
6331 
6332     // Remove any references to SCC. Vector instructions can't read from it, and
6333     // We're just about to add the implicit use / defs of VCC, and we don't want
6334     // both.
6335     for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) {
6336       MachineOperand &Op = Inst.getOperand(i);
6337       if (Op.isReg() && Op.getReg() == AMDGPU::SCC) {
6338         // Only propagate through live-def of SCC.
6339         if (Op.isDef() && !Op.isDead())
6340           addSCCDefUsersToVALUWorklist(Op, Inst, Worklist);
6341         if (Op.isUse())
6342           addSCCDefsToVALUWorklist(Op, Worklist);
6343         Inst.removeOperand(i);
6344       }
6345     }
6346 
6347     if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) {
6348       // We are converting these to a BFE, so we need to add the missing
6349       // operands for the size and offset.
6350       unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16;
6351       Inst.addOperand(MachineOperand::CreateImm(0));
6352       Inst.addOperand(MachineOperand::CreateImm(Size));
6353 
6354     } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) {
6355       // The VALU version adds the second operand to the result, so insert an
6356       // extra 0 operand.
6357       Inst.addOperand(MachineOperand::CreateImm(0));
6358     }
6359 
6360     Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent());
6361     fixImplicitOperands(Inst);
6362 
6363     if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) {
6364       const MachineOperand &OffsetWidthOp = Inst.getOperand(2);
6365       // If we need to move this to VGPRs, we need to unpack the second operand
6366       // back into the 2 separate ones for bit offset and width.
6367       assert(OffsetWidthOp.isImm() &&
6368              "Scalar BFE is only implemented for constant width and offset");
6369       uint32_t Imm = OffsetWidthOp.getImm();
6370 
6371       uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
6372       uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
6373       Inst.removeOperand(2);                     // Remove old immediate.
6374       Inst.addOperand(MachineOperand::CreateImm(Offset));
6375       Inst.addOperand(MachineOperand::CreateImm(BitWidth));
6376     }
6377 
6378     bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef();
6379     unsigned NewDstReg = AMDGPU::NoRegister;
6380     if (HasDst) {
6381       Register DstReg = Inst.getOperand(0).getReg();
6382       if (DstReg.isPhysical())
6383         continue;
6384 
6385       // Update the destination register class.
6386       const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst);
6387       if (!NewDstRC)
6388         continue;
6389 
6390       if (Inst.isCopy() && Inst.getOperand(1).getReg().isVirtual() &&
6391           NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) {
6392         // Instead of creating a copy where src and dst are the same register
6393         // class, we just replace all uses of dst with src.  These kinds of
6394         // copies interfere with the heuristics MachineSink uses to decide
6395         // whether or not to split a critical edge.  Since the pass assumes
6396         // that copies will end up as machine instructions and not be
6397         // eliminated.
6398         addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist);
6399         MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg());
6400         MRI.clearKillFlags(Inst.getOperand(1).getReg());
6401         Inst.getOperand(0).setReg(DstReg);
6402 
6403         // Make sure we don't leave around a dead VGPR->SGPR copy. Normally
6404         // these are deleted later, but at -O0 it would leave a suspicious
6405         // looking illegal copy of an undef register.
6406         for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I)
6407           Inst.removeOperand(I);
6408         Inst.setDesc(get(AMDGPU::IMPLICIT_DEF));
6409         continue;
6410       }
6411 
6412       NewDstReg = MRI.createVirtualRegister(NewDstRC);
6413       MRI.replaceRegWith(DstReg, NewDstReg);
6414     }
6415 
6416     // Legalize the operands
6417     CreatedBBTmp = legalizeOperands(Inst, MDT);
6418     if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6419       CreatedBB = CreatedBBTmp;
6420 
6421     if (HasDst)
6422      addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
6423   }
6424   return CreatedBB;
6425 }
6426 
6427 // Add/sub require special handling to deal with carry outs.
6428 std::pair<bool, MachineBasicBlock *>
6429 SIInstrInfo::moveScalarAddSub(SetVectorType &Worklist, MachineInstr &Inst,
6430                               MachineDominatorTree *MDT) const {
6431   if (ST.hasAddNoCarry()) {
6432     // Assume there is no user of scc since we don't select this in that case.
6433     // Since scc isn't used, it doesn't really matter if the i32 or u32 variant
6434     // is used.
6435 
6436     MachineBasicBlock &MBB = *Inst.getParent();
6437     MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6438 
6439     Register OldDstReg = Inst.getOperand(0).getReg();
6440     Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6441 
6442     unsigned Opc = Inst.getOpcode();
6443     assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32);
6444 
6445     unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ?
6446       AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64;
6447 
6448     assert(Inst.getOperand(3).getReg() == AMDGPU::SCC);
6449     Inst.removeOperand(3);
6450 
6451     Inst.setDesc(get(NewOpc));
6452     Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit
6453     Inst.addImplicitDefUseOperands(*MBB.getParent());
6454     MRI.replaceRegWith(OldDstReg, ResultReg);
6455     MachineBasicBlock *NewBB = legalizeOperands(Inst, MDT);
6456 
6457     addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6458     return std::make_pair(true, NewBB);
6459   }
6460 
6461   return std::make_pair(false, nullptr);
6462 }
6463 
6464 void SIInstrInfo::lowerSelect(SetVectorType &Worklist, MachineInstr &Inst,
6465                               MachineDominatorTree *MDT) const {
6466 
6467   MachineBasicBlock &MBB = *Inst.getParent();
6468   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6469   MachineBasicBlock::iterator MII = Inst;
6470   DebugLoc DL = Inst.getDebugLoc();
6471 
6472   MachineOperand &Dest = Inst.getOperand(0);
6473   MachineOperand &Src0 = Inst.getOperand(1);
6474   MachineOperand &Src1 = Inst.getOperand(2);
6475   MachineOperand &Cond = Inst.getOperand(3);
6476 
6477   Register SCCSource = Cond.getReg();
6478   bool IsSCC = (SCCSource == AMDGPU::SCC);
6479 
6480   // If this is a trivial select where the condition is effectively not SCC
6481   // (SCCSource is a source of copy to SCC), then the select is semantically
6482   // equivalent to copying SCCSource. Hence, there is no need to create
6483   // V_CNDMASK, we can just use that and bail out.
6484   if (!IsSCC && Src0.isImm() && (Src0.getImm() == -1) && Src1.isImm() &&
6485       (Src1.getImm() == 0)) {
6486     MRI.replaceRegWith(Dest.getReg(), SCCSource);
6487     return;
6488   }
6489 
6490   const TargetRegisterClass *TC =
6491       RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
6492 
6493   Register CopySCC = MRI.createVirtualRegister(TC);
6494 
6495   if (IsSCC) {
6496     // Now look for the closest SCC def if it is a copy
6497     // replacing the SCCSource with the COPY source register
6498     bool CopyFound = false;
6499     for (MachineInstr &CandI :
6500          make_range(std::next(MachineBasicBlock::reverse_iterator(Inst)),
6501                     Inst.getParent()->rend())) {
6502       if (CandI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) !=
6503           -1) {
6504         if (CandI.isCopy() && CandI.getOperand(0).getReg() == AMDGPU::SCC) {
6505           BuildMI(MBB, MII, DL, get(AMDGPU::COPY), CopySCC)
6506               .addReg(CandI.getOperand(1).getReg());
6507           CopyFound = true;
6508         }
6509         break;
6510       }
6511     }
6512     if (!CopyFound) {
6513       // SCC def is not a copy
6514       // Insert a trivial select instead of creating a copy, because a copy from
6515       // SCC would semantically mean just copying a single bit, but we may need
6516       // the result to be a vector condition mask that needs preserving.
6517       unsigned Opcode = (ST.getWavefrontSize() == 64) ? AMDGPU::S_CSELECT_B64
6518                                                       : AMDGPU::S_CSELECT_B32;
6519       auto NewSelect =
6520           BuildMI(MBB, MII, DL, get(Opcode), CopySCC).addImm(-1).addImm(0);
6521       NewSelect->getOperand(3).setIsUndef(Cond.isUndef());
6522     }
6523   }
6524 
6525   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6526 
6527   auto UpdatedInst =
6528       BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B32_e64), ResultReg)
6529           .addImm(0)
6530           .add(Src1) // False
6531           .addImm(0)
6532           .add(Src0) // True
6533           .addReg(IsSCC ? CopySCC : SCCSource);
6534 
6535   MRI.replaceRegWith(Dest.getReg(), ResultReg);
6536   legalizeOperands(*UpdatedInst, MDT);
6537   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6538 }
6539 
6540 void SIInstrInfo::lowerScalarAbs(SetVectorType &Worklist,
6541                                  MachineInstr &Inst) const {
6542   MachineBasicBlock &MBB = *Inst.getParent();
6543   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6544   MachineBasicBlock::iterator MII = Inst;
6545   DebugLoc DL = Inst.getDebugLoc();
6546 
6547   MachineOperand &Dest = Inst.getOperand(0);
6548   MachineOperand &Src = Inst.getOperand(1);
6549   Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6550   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6551 
6552   unsigned SubOp = ST.hasAddNoCarry() ?
6553     AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_CO_U32_e32;
6554 
6555   BuildMI(MBB, MII, DL, get(SubOp), TmpReg)
6556     .addImm(0)
6557     .addReg(Src.getReg());
6558 
6559   BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg)
6560     .addReg(Src.getReg())
6561     .addReg(TmpReg);
6562 
6563   MRI.replaceRegWith(Dest.getReg(), ResultReg);
6564   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6565 }
6566 
6567 void SIInstrInfo::lowerScalarXnor(SetVectorType &Worklist,
6568                                   MachineInstr &Inst) const {
6569   MachineBasicBlock &MBB = *Inst.getParent();
6570   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6571   MachineBasicBlock::iterator MII = Inst;
6572   const DebugLoc &DL = Inst.getDebugLoc();
6573 
6574   MachineOperand &Dest = Inst.getOperand(0);
6575   MachineOperand &Src0 = Inst.getOperand(1);
6576   MachineOperand &Src1 = Inst.getOperand(2);
6577 
6578   if (ST.hasDLInsts()) {
6579     Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6580     legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL);
6581     legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL);
6582 
6583     BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest)
6584       .add(Src0)
6585       .add(Src1);
6586 
6587     MRI.replaceRegWith(Dest.getReg(), NewDest);
6588     addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6589   } else {
6590     // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can
6591     // invert either source and then perform the XOR. If either source is a
6592     // scalar register, then we can leave the inversion on the scalar unit to
6593     // achieve a better distribution of scalar and vector instructions.
6594     bool Src0IsSGPR = Src0.isReg() &&
6595                       RI.isSGPRClass(MRI.getRegClass(Src0.getReg()));
6596     bool Src1IsSGPR = Src1.isReg() &&
6597                       RI.isSGPRClass(MRI.getRegClass(Src1.getReg()));
6598     MachineInstr *Xor;
6599     Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6600     Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6601 
6602     // Build a pair of scalar instructions and add them to the work list.
6603     // The next iteration over the work list will lower these to the vector
6604     // unit as necessary.
6605     if (Src0IsSGPR) {
6606       BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0);
6607       Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
6608       .addReg(Temp)
6609       .add(Src1);
6610     } else if (Src1IsSGPR) {
6611       BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1);
6612       Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
6613       .add(Src0)
6614       .addReg(Temp);
6615     } else {
6616       Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp)
6617         .add(Src0)
6618         .add(Src1);
6619       MachineInstr *Not =
6620           BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp);
6621       Worklist.insert(Not);
6622     }
6623 
6624     MRI.replaceRegWith(Dest.getReg(), NewDest);
6625 
6626     Worklist.insert(Xor);
6627 
6628     addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6629   }
6630 }
6631 
6632 void SIInstrInfo::splitScalarNotBinop(SetVectorType &Worklist,
6633                                       MachineInstr &Inst,
6634                                       unsigned Opcode) const {
6635   MachineBasicBlock &MBB = *Inst.getParent();
6636   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6637   MachineBasicBlock::iterator MII = Inst;
6638   const DebugLoc &DL = Inst.getDebugLoc();
6639 
6640   MachineOperand &Dest = Inst.getOperand(0);
6641   MachineOperand &Src0 = Inst.getOperand(1);
6642   MachineOperand &Src1 = Inst.getOperand(2);
6643 
6644   Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6645   Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6646 
6647   MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm)
6648     .add(Src0)
6649     .add(Src1);
6650 
6651   MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest)
6652     .addReg(Interm);
6653 
6654   Worklist.insert(&Op);
6655   Worklist.insert(&Not);
6656 
6657   MRI.replaceRegWith(Dest.getReg(), NewDest);
6658   addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6659 }
6660 
6661 void SIInstrInfo::splitScalarBinOpN2(SetVectorType& Worklist,
6662                                      MachineInstr &Inst,
6663                                      unsigned Opcode) const {
6664   MachineBasicBlock &MBB = *Inst.getParent();
6665   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6666   MachineBasicBlock::iterator MII = Inst;
6667   const DebugLoc &DL = Inst.getDebugLoc();
6668 
6669   MachineOperand &Dest = Inst.getOperand(0);
6670   MachineOperand &Src0 = Inst.getOperand(1);
6671   MachineOperand &Src1 = Inst.getOperand(2);
6672 
6673   Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6674   Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6675 
6676   MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm)
6677     .add(Src1);
6678 
6679   MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest)
6680     .add(Src0)
6681     .addReg(Interm);
6682 
6683   Worklist.insert(&Not);
6684   Worklist.insert(&Op);
6685 
6686   MRI.replaceRegWith(Dest.getReg(), NewDest);
6687   addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6688 }
6689 
6690 void SIInstrInfo::splitScalar64BitUnaryOp(
6691     SetVectorType &Worklist, MachineInstr &Inst,
6692     unsigned Opcode, bool Swap) const {
6693   MachineBasicBlock &MBB = *Inst.getParent();
6694   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6695 
6696   MachineOperand &Dest = Inst.getOperand(0);
6697   MachineOperand &Src0 = Inst.getOperand(1);
6698   DebugLoc DL = Inst.getDebugLoc();
6699 
6700   MachineBasicBlock::iterator MII = Inst;
6701 
6702   const MCInstrDesc &InstDesc = get(Opcode);
6703   const TargetRegisterClass *Src0RC = Src0.isReg() ?
6704     MRI.getRegClass(Src0.getReg()) :
6705     &AMDGPU::SGPR_32RegClass;
6706 
6707   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
6708 
6709   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6710                                                        AMDGPU::sub0, Src0SubRC);
6711 
6712   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
6713   const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
6714   const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
6715 
6716   Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
6717   MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0);
6718 
6719   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6720                                                        AMDGPU::sub1, Src0SubRC);
6721 
6722   Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
6723   MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1);
6724 
6725   if (Swap)
6726     std::swap(DestSub0, DestSub1);
6727 
6728   Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
6729   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
6730     .addReg(DestSub0)
6731     .addImm(AMDGPU::sub0)
6732     .addReg(DestSub1)
6733     .addImm(AMDGPU::sub1);
6734 
6735   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
6736 
6737   Worklist.insert(&LoHalf);
6738   Worklist.insert(&HiHalf);
6739 
6740   // We don't need to legalizeOperands here because for a single operand, src0
6741   // will support any kind of input.
6742 
6743   // Move all users of this moved value.
6744   addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
6745 }
6746 
6747 void SIInstrInfo::splitScalar64BitAddSub(SetVectorType &Worklist,
6748                                          MachineInstr &Inst,
6749                                          MachineDominatorTree *MDT) const {
6750   bool IsAdd = (Inst.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO);
6751 
6752   MachineBasicBlock &MBB = *Inst.getParent();
6753   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6754   const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
6755 
6756   Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
6757   Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6758   Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6759 
6760   Register CarryReg = MRI.createVirtualRegister(CarryRC);
6761   Register DeadCarryReg = MRI.createVirtualRegister(CarryRC);
6762 
6763   MachineOperand &Dest = Inst.getOperand(0);
6764   MachineOperand &Src0 = Inst.getOperand(1);
6765   MachineOperand &Src1 = Inst.getOperand(2);
6766   const DebugLoc &DL = Inst.getDebugLoc();
6767   MachineBasicBlock::iterator MII = Inst;
6768 
6769   const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg());
6770   const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg());
6771   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
6772   const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
6773 
6774   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6775                                                        AMDGPU::sub0, Src0SubRC);
6776   MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6777                                                        AMDGPU::sub0, Src1SubRC);
6778 
6779 
6780   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6781                                                        AMDGPU::sub1, Src0SubRC);
6782   MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6783                                                        AMDGPU::sub1, Src1SubRC);
6784 
6785   unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64;
6786   MachineInstr *LoHalf =
6787     BuildMI(MBB, MII, DL, get(LoOpc), DestSub0)
6788     .addReg(CarryReg, RegState::Define)
6789     .add(SrcReg0Sub0)
6790     .add(SrcReg1Sub0)
6791     .addImm(0); // clamp bit
6792 
6793   unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64;
6794   MachineInstr *HiHalf =
6795     BuildMI(MBB, MII, DL, get(HiOpc), DestSub1)
6796     .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
6797     .add(SrcReg0Sub1)
6798     .add(SrcReg1Sub1)
6799     .addReg(CarryReg, RegState::Kill)
6800     .addImm(0); // clamp bit
6801 
6802   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
6803     .addReg(DestSub0)
6804     .addImm(AMDGPU::sub0)
6805     .addReg(DestSub1)
6806     .addImm(AMDGPU::sub1);
6807 
6808   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
6809 
6810   // Try to legalize the operands in case we need to swap the order to keep it
6811   // valid.
6812   legalizeOperands(*LoHalf, MDT);
6813   legalizeOperands(*HiHalf, MDT);
6814 
6815   // Move all users of this moved value.
6816   addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
6817 }
6818 
6819 void SIInstrInfo::splitScalar64BitBinaryOp(SetVectorType &Worklist,
6820                                            MachineInstr &Inst, unsigned Opcode,
6821                                            MachineDominatorTree *MDT) const {
6822   MachineBasicBlock &MBB = *Inst.getParent();
6823   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6824 
6825   MachineOperand &Dest = Inst.getOperand(0);
6826   MachineOperand &Src0 = Inst.getOperand(1);
6827   MachineOperand &Src1 = Inst.getOperand(2);
6828   DebugLoc DL = Inst.getDebugLoc();
6829 
6830   MachineBasicBlock::iterator MII = Inst;
6831 
6832   const MCInstrDesc &InstDesc = get(Opcode);
6833   const TargetRegisterClass *Src0RC = Src0.isReg() ?
6834     MRI.getRegClass(Src0.getReg()) :
6835     &AMDGPU::SGPR_32RegClass;
6836 
6837   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
6838   const TargetRegisterClass *Src1RC = Src1.isReg() ?
6839     MRI.getRegClass(Src1.getReg()) :
6840     &AMDGPU::SGPR_32RegClass;
6841 
6842   const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
6843 
6844   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6845                                                        AMDGPU::sub0, Src0SubRC);
6846   MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6847                                                        AMDGPU::sub0, Src1SubRC);
6848   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6849                                                        AMDGPU::sub1, Src0SubRC);
6850   MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6851                                                        AMDGPU::sub1, Src1SubRC);
6852 
6853   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
6854   const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
6855   const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
6856 
6857   Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
6858   MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0)
6859                               .add(SrcReg0Sub0)
6860                               .add(SrcReg1Sub0);
6861 
6862   Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
6863   MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1)
6864                               .add(SrcReg0Sub1)
6865                               .add(SrcReg1Sub1);
6866 
6867   Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
6868   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
6869     .addReg(DestSub0)
6870     .addImm(AMDGPU::sub0)
6871     .addReg(DestSub1)
6872     .addImm(AMDGPU::sub1);
6873 
6874   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
6875 
6876   Worklist.insert(&LoHalf);
6877   Worklist.insert(&HiHalf);
6878 
6879   // Move all users of this moved value.
6880   addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
6881 }
6882 
6883 void SIInstrInfo::splitScalar64BitXnor(SetVectorType &Worklist,
6884                                        MachineInstr &Inst,
6885                                        MachineDominatorTree *MDT) const {
6886   MachineBasicBlock &MBB = *Inst.getParent();
6887   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6888 
6889   MachineOperand &Dest = Inst.getOperand(0);
6890   MachineOperand &Src0 = Inst.getOperand(1);
6891   MachineOperand &Src1 = Inst.getOperand(2);
6892   const DebugLoc &DL = Inst.getDebugLoc();
6893 
6894   MachineBasicBlock::iterator MII = Inst;
6895 
6896   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
6897 
6898   Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
6899 
6900   MachineOperand* Op0;
6901   MachineOperand* Op1;
6902 
6903   if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) {
6904     Op0 = &Src0;
6905     Op1 = &Src1;
6906   } else {
6907     Op0 = &Src1;
6908     Op1 = &Src0;
6909   }
6910 
6911   BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm)
6912     .add(*Op0);
6913 
6914   Register NewDest = MRI.createVirtualRegister(DestRC);
6915 
6916   MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest)
6917     .addReg(Interm)
6918     .add(*Op1);
6919 
6920   MRI.replaceRegWith(Dest.getReg(), NewDest);
6921 
6922   Worklist.insert(&Xor);
6923 }
6924 
6925 void SIInstrInfo::splitScalar64BitBCNT(
6926     SetVectorType &Worklist, MachineInstr &Inst) const {
6927   MachineBasicBlock &MBB = *Inst.getParent();
6928   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6929 
6930   MachineBasicBlock::iterator MII = Inst;
6931   const DebugLoc &DL = Inst.getDebugLoc();
6932 
6933   MachineOperand &Dest = Inst.getOperand(0);
6934   MachineOperand &Src = Inst.getOperand(1);
6935 
6936   const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64);
6937   const TargetRegisterClass *SrcRC = Src.isReg() ?
6938     MRI.getRegClass(Src.getReg()) :
6939     &AMDGPU::SGPR_32RegClass;
6940 
6941   Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6942   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6943 
6944   const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0);
6945 
6946   MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
6947                                                       AMDGPU::sub0, SrcSubRC);
6948   MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
6949                                                       AMDGPU::sub1, SrcSubRC);
6950 
6951   BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0);
6952 
6953   BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg);
6954 
6955   MRI.replaceRegWith(Dest.getReg(), ResultReg);
6956 
6957   // We don't need to legalize operands here. src0 for either instruction can be
6958   // an SGPR, and the second input is unused or determined here.
6959   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6960 }
6961 
6962 void SIInstrInfo::splitScalar64BitBFE(SetVectorType &Worklist,
6963                                       MachineInstr &Inst) const {
6964   MachineBasicBlock &MBB = *Inst.getParent();
6965   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6966   MachineBasicBlock::iterator MII = Inst;
6967   const DebugLoc &DL = Inst.getDebugLoc();
6968 
6969   MachineOperand &Dest = Inst.getOperand(0);
6970   uint32_t Imm = Inst.getOperand(2).getImm();
6971   uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
6972   uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
6973 
6974   (void) Offset;
6975 
6976   // Only sext_inreg cases handled.
6977   assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 &&
6978          Offset == 0 && "Not implemented");
6979 
6980   if (BitWidth < 32) {
6981     Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6982     Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6983     Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
6984 
6985     BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32_e64), MidRegLo)
6986         .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0)
6987         .addImm(0)
6988         .addImm(BitWidth);
6989 
6990     BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi)
6991       .addImm(31)
6992       .addReg(MidRegLo);
6993 
6994     BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
6995       .addReg(MidRegLo)
6996       .addImm(AMDGPU::sub0)
6997       .addReg(MidRegHi)
6998       .addImm(AMDGPU::sub1);
6999 
7000     MRI.replaceRegWith(Dest.getReg(), ResultReg);
7001     addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
7002     return;
7003   }
7004 
7005   MachineOperand &Src = Inst.getOperand(1);
7006   Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7007   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
7008 
7009   BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg)
7010     .addImm(31)
7011     .addReg(Src.getReg(), 0, AMDGPU::sub0);
7012 
7013   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
7014     .addReg(Src.getReg(), 0, AMDGPU::sub0)
7015     .addImm(AMDGPU::sub0)
7016     .addReg(TmpReg)
7017     .addImm(AMDGPU::sub1);
7018 
7019   MRI.replaceRegWith(Dest.getReg(), ResultReg);
7020   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
7021 }
7022 
7023 void SIInstrInfo::addUsersToMoveToVALUWorklist(
7024   Register DstReg,
7025   MachineRegisterInfo &MRI,
7026   SetVectorType &Worklist) const {
7027   for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg),
7028          E = MRI.use_end(); I != E;) {
7029     MachineInstr &UseMI = *I->getParent();
7030 
7031     unsigned OpNo = 0;
7032 
7033     switch (UseMI.getOpcode()) {
7034     case AMDGPU::COPY:
7035     case AMDGPU::WQM:
7036     case AMDGPU::SOFT_WQM:
7037     case AMDGPU::STRICT_WWM:
7038     case AMDGPU::STRICT_WQM:
7039     case AMDGPU::REG_SEQUENCE:
7040     case AMDGPU::PHI:
7041     case AMDGPU::INSERT_SUBREG:
7042       break;
7043     default:
7044       OpNo = I.getOperandNo();
7045       break;
7046     }
7047 
7048     if (!RI.hasVectorRegisters(getOpRegClass(UseMI, OpNo))) {
7049       Worklist.insert(&UseMI);
7050 
7051       do {
7052         ++I;
7053       } while (I != E && I->getParent() == &UseMI);
7054     } else {
7055       ++I;
7056     }
7057   }
7058 }
7059 
7060 void SIInstrInfo::movePackToVALU(SetVectorType &Worklist,
7061                                  MachineRegisterInfo &MRI,
7062                                  MachineInstr &Inst) const {
7063   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7064   MachineBasicBlock *MBB = Inst.getParent();
7065   MachineOperand &Src0 = Inst.getOperand(1);
7066   MachineOperand &Src1 = Inst.getOperand(2);
7067   const DebugLoc &DL = Inst.getDebugLoc();
7068 
7069   switch (Inst.getOpcode()) {
7070   case AMDGPU::S_PACK_LL_B32_B16: {
7071     Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7072     Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7073 
7074     // FIXME: Can do a lot better if we know the high bits of src0 or src1 are
7075     // 0.
7076     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
7077       .addImm(0xffff);
7078 
7079     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg)
7080       .addReg(ImmReg, RegState::Kill)
7081       .add(Src0);
7082 
7083     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg)
7084       .add(Src1)
7085       .addImm(16)
7086       .addReg(TmpReg, RegState::Kill);
7087     break;
7088   }
7089   case AMDGPU::S_PACK_LH_B32_B16: {
7090     Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7091     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
7092       .addImm(0xffff);
7093     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32_e64), ResultReg)
7094       .addReg(ImmReg, RegState::Kill)
7095       .add(Src0)
7096       .add(Src1);
7097     break;
7098   }
7099   case AMDGPU::S_PACK_HH_B32_B16: {
7100     Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7101     Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7102     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg)
7103       .addImm(16)
7104       .add(Src0);
7105     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
7106       .addImm(0xffff0000);
7107     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32_e64), ResultReg)
7108       .add(Src1)
7109       .addReg(ImmReg, RegState::Kill)
7110       .addReg(TmpReg, RegState::Kill);
7111     break;
7112   }
7113   default:
7114     llvm_unreachable("unhandled s_pack_* instruction");
7115   }
7116 
7117   MachineOperand &Dest = Inst.getOperand(0);
7118   MRI.replaceRegWith(Dest.getReg(), ResultReg);
7119   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
7120 }
7121 
7122 void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op,
7123                                                MachineInstr &SCCDefInst,
7124                                                SetVectorType &Worklist,
7125                                                Register NewCond) const {
7126 
7127   // Ensure that def inst defines SCC, which is still live.
7128   assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() &&
7129          !Op.isDead() && Op.getParent() == &SCCDefInst);
7130   SmallVector<MachineInstr *, 4> CopyToDelete;
7131   // This assumes that all the users of SCC are in the same block
7132   // as the SCC def.
7133   for (MachineInstr &MI : // Skip the def inst itself.
7134        make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)),
7135                   SCCDefInst.getParent()->end())) {
7136     // Check if SCC is used first.
7137     int SCCIdx = MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI);
7138     if (SCCIdx != -1) {
7139       if (MI.isCopy()) {
7140         MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
7141         Register DestReg = MI.getOperand(0).getReg();
7142 
7143         MRI.replaceRegWith(DestReg, NewCond);
7144         CopyToDelete.push_back(&MI);
7145       } else {
7146 
7147         if (NewCond.isValid())
7148           MI.getOperand(SCCIdx).setReg(NewCond);
7149 
7150         Worklist.insert(&MI);
7151       }
7152     }
7153     // Exit if we find another SCC def.
7154     if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1)
7155       break;
7156   }
7157   for (auto &Copy : CopyToDelete)
7158     Copy->eraseFromParent();
7159 }
7160 
7161 // Instructions that use SCC may be converted to VALU instructions. When that
7162 // happens, the SCC register is changed to VCC_LO. The instruction that defines
7163 // SCC must be changed to an instruction that defines VCC. This function makes
7164 // sure that the instruction that defines SCC is added to the moveToVALU
7165 // worklist.
7166 void SIInstrInfo::addSCCDefsToVALUWorklist(MachineOperand &Op,
7167                                            SetVectorType &Worklist) const {
7168   assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isUse());
7169 
7170   MachineInstr *SCCUseInst = Op.getParent();
7171   // Look for a preceding instruction that either defines VCC or SCC. If VCC
7172   // then there is nothing to do because the defining instruction has been
7173   // converted to a VALU already. If SCC then that instruction needs to be
7174   // converted to a VALU.
7175   for (MachineInstr &MI :
7176        make_range(std::next(MachineBasicBlock::reverse_iterator(SCCUseInst)),
7177                   SCCUseInst->getParent()->rend())) {
7178     if (MI.modifiesRegister(AMDGPU::VCC, &RI))
7179       break;
7180     if (MI.definesRegister(AMDGPU::SCC, &RI)) {
7181       Worklist.insert(&MI);
7182       break;
7183     }
7184   }
7185 }
7186 
7187 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass(
7188   const MachineInstr &Inst) const {
7189   const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0);
7190 
7191   switch (Inst.getOpcode()) {
7192   // For target instructions, getOpRegClass just returns the virtual register
7193   // class associated with the operand, so we need to find an equivalent VGPR
7194   // register class in order to move the instruction to the VALU.
7195   case AMDGPU::COPY:
7196   case AMDGPU::PHI:
7197   case AMDGPU::REG_SEQUENCE:
7198   case AMDGPU::INSERT_SUBREG:
7199   case AMDGPU::WQM:
7200   case AMDGPU::SOFT_WQM:
7201   case AMDGPU::STRICT_WWM:
7202   case AMDGPU::STRICT_WQM: {
7203     const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1);
7204     if (RI.isAGPRClass(SrcRC)) {
7205       if (RI.isAGPRClass(NewDstRC))
7206         return nullptr;
7207 
7208       switch (Inst.getOpcode()) {
7209       case AMDGPU::PHI:
7210       case AMDGPU::REG_SEQUENCE:
7211       case AMDGPU::INSERT_SUBREG:
7212         NewDstRC = RI.getEquivalentAGPRClass(NewDstRC);
7213         break;
7214       default:
7215         NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
7216       }
7217 
7218       if (!NewDstRC)
7219         return nullptr;
7220     } else {
7221       if (RI.isVGPRClass(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass)
7222         return nullptr;
7223 
7224       NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
7225       if (!NewDstRC)
7226         return nullptr;
7227     }
7228 
7229     return NewDstRC;
7230   }
7231   default:
7232     return NewDstRC;
7233   }
7234 }
7235 
7236 // Find the one SGPR operand we are allowed to use.
7237 Register SIInstrInfo::findUsedSGPR(const MachineInstr &MI,
7238                                    int OpIndices[3]) const {
7239   const MCInstrDesc &Desc = MI.getDesc();
7240 
7241   // Find the one SGPR operand we are allowed to use.
7242   //
7243   // First we need to consider the instruction's operand requirements before
7244   // legalizing. Some operands are required to be SGPRs, such as implicit uses
7245   // of VCC, but we are still bound by the constant bus requirement to only use
7246   // one.
7247   //
7248   // If the operand's class is an SGPR, we can never move it.
7249 
7250   Register SGPRReg = findImplicitSGPRRead(MI);
7251   if (SGPRReg != AMDGPU::NoRegister)
7252     return SGPRReg;
7253 
7254   Register UsedSGPRs[3] = { AMDGPU::NoRegister };
7255   const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
7256 
7257   for (unsigned i = 0; i < 3; ++i) {
7258     int Idx = OpIndices[i];
7259     if (Idx == -1)
7260       break;
7261 
7262     const MachineOperand &MO = MI.getOperand(Idx);
7263     if (!MO.isReg())
7264       continue;
7265 
7266     // Is this operand statically required to be an SGPR based on the operand
7267     // constraints?
7268     const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass);
7269     bool IsRequiredSGPR = RI.isSGPRClass(OpRC);
7270     if (IsRequiredSGPR)
7271       return MO.getReg();
7272 
7273     // If this could be a VGPR or an SGPR, Check the dynamic register class.
7274     Register Reg = MO.getReg();
7275     const TargetRegisterClass *RegRC = MRI.getRegClass(Reg);
7276     if (RI.isSGPRClass(RegRC))
7277       UsedSGPRs[i] = Reg;
7278   }
7279 
7280   // We don't have a required SGPR operand, so we have a bit more freedom in
7281   // selecting operands to move.
7282 
7283   // Try to select the most used SGPR. If an SGPR is equal to one of the
7284   // others, we choose that.
7285   //
7286   // e.g.
7287   // V_FMA_F32 v0, s0, s0, s0 -> No moves
7288   // V_FMA_F32 v0, s0, s1, s0 -> Move s1
7289 
7290   // TODO: If some of the operands are 64-bit SGPRs and some 32, we should
7291   // prefer those.
7292 
7293   if (UsedSGPRs[0] != AMDGPU::NoRegister) {
7294     if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2])
7295       SGPRReg = UsedSGPRs[0];
7296   }
7297 
7298   if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) {
7299     if (UsedSGPRs[1] == UsedSGPRs[2])
7300       SGPRReg = UsedSGPRs[1];
7301   }
7302 
7303   return SGPRReg;
7304 }
7305 
7306 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI,
7307                                              unsigned OperandName) const {
7308   int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName);
7309   if (Idx == -1)
7310     return nullptr;
7311 
7312   return &MI.getOperand(Idx);
7313 }
7314 
7315 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const {
7316   if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
7317     return (AMDGPU::MTBUFFormat::UFMT_32_FLOAT << 44) |
7318            (1ULL << 56) | // RESOURCE_LEVEL = 1
7319            (3ULL << 60); // OOB_SELECT = 3
7320   }
7321 
7322   uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT;
7323   if (ST.isAmdHsaOS()) {
7324     // Set ATC = 1. GFX9 doesn't have this bit.
7325     if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS)
7326       RsrcDataFormat |= (1ULL << 56);
7327 
7328     // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this.
7329     // BTW, it disables TC L2 and therefore decreases performance.
7330     if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS)
7331       RsrcDataFormat |= (2ULL << 59);
7332   }
7333 
7334   return RsrcDataFormat;
7335 }
7336 
7337 uint64_t SIInstrInfo::getScratchRsrcWords23() const {
7338   uint64_t Rsrc23 = getDefaultRsrcDataFormat() |
7339                     AMDGPU::RSRC_TID_ENABLE |
7340                     0xffffffff; // Size;
7341 
7342   // GFX9 doesn't have ELEMENT_SIZE.
7343   if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
7344     uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize(true)) - 1;
7345     Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT;
7346   }
7347 
7348   // IndexStride = 64 / 32.
7349   uint64_t IndexStride = ST.getWavefrontSize() == 64 ? 3 : 2;
7350   Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT;
7351 
7352   // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17].
7353   // Clear them unless we want a huge stride.
7354   if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
7355       ST.getGeneration() <= AMDGPUSubtarget::GFX9)
7356     Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT;
7357 
7358   return Rsrc23;
7359 }
7360 
7361 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const {
7362   unsigned Opc = MI.getOpcode();
7363 
7364   return isSMRD(Opc);
7365 }
7366 
7367 bool SIInstrInfo::isHighLatencyDef(int Opc) const {
7368   return get(Opc).mayLoad() &&
7369          (isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc) || isFLAT(Opc));
7370 }
7371 
7372 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI,
7373                                     int &FrameIndex) const {
7374   const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
7375   if (!Addr || !Addr->isFI())
7376     return AMDGPU::NoRegister;
7377 
7378   assert(!MI.memoperands_empty() &&
7379          (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS);
7380 
7381   FrameIndex = Addr->getIndex();
7382   return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg();
7383 }
7384 
7385 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI,
7386                                         int &FrameIndex) const {
7387   const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr);
7388   assert(Addr && Addr->isFI());
7389   FrameIndex = Addr->getIndex();
7390   return getNamedOperand(MI, AMDGPU::OpName::data)->getReg();
7391 }
7392 
7393 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
7394                                           int &FrameIndex) const {
7395   if (!MI.mayLoad())
7396     return AMDGPU::NoRegister;
7397 
7398   if (isMUBUF(MI) || isVGPRSpill(MI))
7399     return isStackAccess(MI, FrameIndex);
7400 
7401   if (isSGPRSpill(MI))
7402     return isSGPRStackAccess(MI, FrameIndex);
7403 
7404   return AMDGPU::NoRegister;
7405 }
7406 
7407 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
7408                                          int &FrameIndex) const {
7409   if (!MI.mayStore())
7410     return AMDGPU::NoRegister;
7411 
7412   if (isMUBUF(MI) || isVGPRSpill(MI))
7413     return isStackAccess(MI, FrameIndex);
7414 
7415   if (isSGPRSpill(MI))
7416     return isSGPRStackAccess(MI, FrameIndex);
7417 
7418   return AMDGPU::NoRegister;
7419 }
7420 
7421 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const {
7422   unsigned Size = 0;
7423   MachineBasicBlock::const_instr_iterator I = MI.getIterator();
7424   MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
7425   while (++I != E && I->isInsideBundle()) {
7426     assert(!I->isBundle() && "No nested bundle!");
7427     Size += getInstSizeInBytes(*I);
7428   }
7429 
7430   return Size;
7431 }
7432 
7433 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
7434   unsigned Opc = MI.getOpcode();
7435   const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc);
7436   unsigned DescSize = Desc.getSize();
7437 
7438   // If we have a definitive size, we can use it. Otherwise we need to inspect
7439   // the operands to know the size.
7440   if (isFixedSize(MI)) {
7441     unsigned Size = DescSize;
7442 
7443     // If we hit the buggy offset, an extra nop will be inserted in MC so
7444     // estimate the worst case.
7445     if (MI.isBranch() && ST.hasOffset3fBug())
7446       Size += 4;
7447 
7448     return Size;
7449   }
7450 
7451   // Instructions may have a 32-bit literal encoded after them. Check
7452   // operands that could ever be literals.
7453   if (isVALU(MI) || isSALU(MI)) {
7454     if (isDPP(MI))
7455       return DescSize;
7456     bool HasLiteral = false;
7457     for (int I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) {
7458       if (isLiteralConstant(MI, I)) {
7459         HasLiteral = true;
7460         break;
7461       }
7462     }
7463     return HasLiteral ? DescSize + 4 : DescSize;
7464   }
7465 
7466   // Check whether we have extra NSA words.
7467   if (isMIMG(MI)) {
7468     int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0);
7469     if (VAddr0Idx < 0)
7470       return 8;
7471 
7472     int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc);
7473     return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4);
7474   }
7475 
7476   switch (Opc) {
7477   case TargetOpcode::BUNDLE:
7478     return getInstBundleSize(MI);
7479   case TargetOpcode::INLINEASM:
7480   case TargetOpcode::INLINEASM_BR: {
7481     const MachineFunction *MF = MI.getParent()->getParent();
7482     const char *AsmStr = MI.getOperand(0).getSymbolName();
7483     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(), &ST);
7484   }
7485   default:
7486     if (MI.isMetaInstruction())
7487       return 0;
7488     return DescSize;
7489   }
7490 }
7491 
7492 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const {
7493   if (!isFLAT(MI))
7494     return false;
7495 
7496   if (MI.memoperands_empty())
7497     return true;
7498 
7499   for (const MachineMemOperand *MMO : MI.memoperands()) {
7500     if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS)
7501       return true;
7502   }
7503   return false;
7504 }
7505 
7506 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const {
7507   return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO;
7508 }
7509 
7510 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry,
7511                                             MachineBasicBlock *IfEnd) const {
7512   MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator();
7513   assert(TI != IfEntry->end());
7514 
7515   MachineInstr *Branch = &(*TI);
7516   MachineFunction *MF = IfEntry->getParent();
7517   MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo();
7518 
7519   if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
7520     Register DstReg = MRI.createVirtualRegister(RI.getBoolRC());
7521     MachineInstr *SIIF =
7522         BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg)
7523             .add(Branch->getOperand(0))
7524             .add(Branch->getOperand(1));
7525     MachineInstr *SIEND =
7526         BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF))
7527             .addReg(DstReg);
7528 
7529     IfEntry->erase(TI);
7530     IfEntry->insert(IfEntry->end(), SIIF);
7531     IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND);
7532   }
7533 }
7534 
7535 void SIInstrInfo::convertNonUniformLoopRegion(
7536     MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const {
7537   MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator();
7538   // We expect 2 terminators, one conditional and one unconditional.
7539   assert(TI != LoopEnd->end());
7540 
7541   MachineInstr *Branch = &(*TI);
7542   MachineFunction *MF = LoopEnd->getParent();
7543   MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo();
7544 
7545   if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
7546 
7547     Register DstReg = MRI.createVirtualRegister(RI.getBoolRC());
7548     Register BackEdgeReg = MRI.createVirtualRegister(RI.getBoolRC());
7549     MachineInstrBuilder HeaderPHIBuilder =
7550         BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg);
7551     for (MachineBasicBlock *PMBB : LoopEntry->predecessors()) {
7552       if (PMBB == LoopEnd) {
7553         HeaderPHIBuilder.addReg(BackEdgeReg);
7554       } else {
7555         Register ZeroReg = MRI.createVirtualRegister(RI.getBoolRC());
7556         materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(),
7557                              ZeroReg, 0);
7558         HeaderPHIBuilder.addReg(ZeroReg);
7559       }
7560       HeaderPHIBuilder.addMBB(PMBB);
7561     }
7562     MachineInstr *HeaderPhi = HeaderPHIBuilder;
7563     MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(),
7564                                       get(AMDGPU::SI_IF_BREAK), BackEdgeReg)
7565                                   .addReg(DstReg)
7566                                   .add(Branch->getOperand(0));
7567     MachineInstr *SILOOP =
7568         BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP))
7569             .addReg(BackEdgeReg)
7570             .addMBB(LoopEntry);
7571 
7572     LoopEntry->insert(LoopEntry->begin(), HeaderPhi);
7573     LoopEnd->erase(TI);
7574     LoopEnd->insert(LoopEnd->end(), SIIFBREAK);
7575     LoopEnd->insert(LoopEnd->end(), SILOOP);
7576   }
7577 }
7578 
7579 ArrayRef<std::pair<int, const char *>>
7580 SIInstrInfo::getSerializableTargetIndices() const {
7581   static const std::pair<int, const char *> TargetIndices[] = {
7582       {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"},
7583       {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"},
7584       {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"},
7585       {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"},
7586       {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}};
7587   return makeArrayRef(TargetIndices);
7588 }
7589 
7590 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp).  The
7591 /// post-RA version of misched uses CreateTargetMIHazardRecognizer.
7592 ScheduleHazardRecognizer *
7593 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
7594                                             const ScheduleDAG *DAG) const {
7595   return new GCNHazardRecognizer(DAG->MF);
7596 }
7597 
7598 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer
7599 /// pass.
7600 ScheduleHazardRecognizer *
7601 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const {
7602   return new GCNHazardRecognizer(MF);
7603 }
7604 
7605 // Called during:
7606 // - pre-RA scheduling and post-RA scheduling
7607 ScheduleHazardRecognizer *
7608 SIInstrInfo::CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
7609                                             const ScheduleDAGMI *DAG) const {
7610   // Borrowed from Arm Target
7611   // We would like to restrict this hazard recognizer to only
7612   // post-RA scheduling; we can tell that we're post-RA because we don't
7613   // track VRegLiveness.
7614   if (!DAG->hasVRegLiveness())
7615     return new GCNHazardRecognizer(DAG->MF);
7616   return TargetInstrInfo::CreateTargetMIHazardRecognizer(II, DAG);
7617 }
7618 
7619 std::pair<unsigned, unsigned>
7620 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
7621   return std::make_pair(TF & MO_MASK, TF & ~MO_MASK);
7622 }
7623 
7624 ArrayRef<std::pair<unsigned, const char *>>
7625 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
7626   static const std::pair<unsigned, const char *> TargetFlags[] = {
7627     { MO_GOTPCREL, "amdgpu-gotprel" },
7628     { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" },
7629     { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" },
7630     { MO_REL32_LO, "amdgpu-rel32-lo" },
7631     { MO_REL32_HI, "amdgpu-rel32-hi" },
7632     { MO_ABS32_LO, "amdgpu-abs32-lo" },
7633     { MO_ABS32_HI, "amdgpu-abs32-hi" },
7634   };
7635 
7636   return makeArrayRef(TargetFlags);
7637 }
7638 
7639 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
7640 SIInstrInfo::getSerializableMachineMemOperandTargetFlags() const {
7641   static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
7642       {
7643           {MONoClobber, "amdgpu-noclobber"},
7644       };
7645 
7646   return makeArrayRef(TargetFlags);
7647 }
7648 
7649 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI) const {
7650   return !MI.isTerminator() && MI.getOpcode() != AMDGPU::COPY &&
7651          MI.modifiesRegister(AMDGPU::EXEC, &RI);
7652 }
7653 
7654 MachineInstrBuilder
7655 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB,
7656                            MachineBasicBlock::iterator I,
7657                            const DebugLoc &DL,
7658                            Register DestReg) const {
7659   if (ST.hasAddNoCarry())
7660     return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg);
7661 
7662   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
7663   Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC());
7664   MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC());
7665 
7666   return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg)
7667            .addReg(UnusedCarry, RegState::Define | RegState::Dead);
7668 }
7669 
7670 MachineInstrBuilder SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB,
7671                                                MachineBasicBlock::iterator I,
7672                                                const DebugLoc &DL,
7673                                                Register DestReg,
7674                                                RegScavenger &RS) const {
7675   if (ST.hasAddNoCarry())
7676     return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg);
7677 
7678   // If available, prefer to use vcc.
7679   Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC)
7680                              ? Register(RI.getVCC())
7681                              : RS.scavengeRegister(RI.getBoolRC(), I, 0, false);
7682 
7683   // TODO: Users need to deal with this.
7684   if (!UnusedCarry.isValid())
7685     return MachineInstrBuilder();
7686 
7687   return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg)
7688            .addReg(UnusedCarry, RegState::Define | RegState::Dead);
7689 }
7690 
7691 bool SIInstrInfo::isKillTerminator(unsigned Opcode) {
7692   switch (Opcode) {
7693   case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
7694   case AMDGPU::SI_KILL_I1_TERMINATOR:
7695     return true;
7696   default:
7697     return false;
7698   }
7699 }
7700 
7701 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const {
7702   switch (Opcode) {
7703   case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO:
7704     return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR);
7705   case AMDGPU::SI_KILL_I1_PSEUDO:
7706     return get(AMDGPU::SI_KILL_I1_TERMINATOR);
7707   default:
7708     llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO");
7709   }
7710 }
7711 
7712 void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const {
7713   if (!ST.isWave32())
7714     return;
7715 
7716   for (auto &Op : MI.implicit_operands()) {
7717     if (Op.isReg() && Op.getReg() == AMDGPU::VCC)
7718       Op.setReg(AMDGPU::VCC_LO);
7719   }
7720 }
7721 
7722 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const {
7723   if (!isSMRD(MI))
7724     return false;
7725 
7726   // Check that it is using a buffer resource.
7727   int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase);
7728   if (Idx == -1) // e.g. s_memtime
7729     return false;
7730 
7731   const auto RCID = MI.getDesc().OpInfo[Idx].RegClass;
7732   return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass);
7733 }
7734 
7735 // Depending on the used address space and instructions, some immediate offsets
7736 // are allowed and some are not.
7737 // In general, flat instruction offsets can only be non-negative, global and
7738 // scratch instruction offsets can also be negative.
7739 //
7740 // There are several bugs related to these offsets:
7741 // On gfx10.1, flat instructions that go into the global address space cannot
7742 // use an offset.
7743 //
7744 // For scratch instructions, the address can be either an SGPR or a VGPR.
7745 // The following offsets can be used, depending on the architecture (x means
7746 // cannot be used):
7747 // +----------------------------+------+------+
7748 // | Address-Mode               | SGPR | VGPR |
7749 // +----------------------------+------+------+
7750 // | gfx9                       |      |      |
7751 // | negative, 4-aligned offset | x    | ok   |
7752 // | negative, unaligned offset | x    | ok   |
7753 // +----------------------------+------+------+
7754 // | gfx10                      |      |      |
7755 // | negative, 4-aligned offset | ok   | ok   |
7756 // | negative, unaligned offset | ok   | x    |
7757 // +----------------------------+------+------+
7758 // | gfx10.3                    |      |      |
7759 // | negative, 4-aligned offset | ok   | ok   |
7760 // | negative, unaligned offset | ok   | ok   |
7761 // +----------------------------+------+------+
7762 //
7763 // This function ignores the addressing mode, so if an offset cannot be used in
7764 // one addressing mode, it is considered illegal.
7765 bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace,
7766                                     uint64_t FlatVariant) const {
7767   // TODO: Should 0 be special cased?
7768   if (!ST.hasFlatInstOffsets())
7769     return false;
7770 
7771   if (ST.hasFlatSegmentOffsetBug() && FlatVariant == SIInstrFlags::FLAT &&
7772       (AddrSpace == AMDGPUAS::FLAT_ADDRESS ||
7773        AddrSpace == AMDGPUAS::GLOBAL_ADDRESS))
7774     return false;
7775 
7776   bool Signed = FlatVariant != SIInstrFlags::FLAT;
7777   if (ST.hasNegativeScratchOffsetBug() &&
7778       FlatVariant == SIInstrFlags::FlatScratch)
7779     Signed = false;
7780   if (ST.hasNegativeUnalignedScratchOffsetBug() &&
7781       FlatVariant == SIInstrFlags::FlatScratch && Offset < 0 &&
7782       (Offset % 4) != 0) {
7783     return false;
7784   }
7785 
7786   unsigned N = AMDGPU::getNumFlatOffsetBits(ST, Signed);
7787   return Signed ? isIntN(N, Offset) : isUIntN(N, Offset);
7788 }
7789 
7790 // See comment on SIInstrInfo::isLegalFLATOffset for what is legal and what not.
7791 std::pair<int64_t, int64_t>
7792 SIInstrInfo::splitFlatOffset(int64_t COffsetVal, unsigned AddrSpace,
7793                              uint64_t FlatVariant) const {
7794   int64_t RemainderOffset = COffsetVal;
7795   int64_t ImmField = 0;
7796   bool Signed = FlatVariant != SIInstrFlags::FLAT;
7797   if (ST.hasNegativeScratchOffsetBug() &&
7798       FlatVariant == SIInstrFlags::FlatScratch)
7799     Signed = false;
7800 
7801   const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(ST, Signed);
7802   if (Signed) {
7803     // Use signed division by a power of two to truncate towards 0.
7804     int64_t D = 1LL << (NumBits - 1);
7805     RemainderOffset = (COffsetVal / D) * D;
7806     ImmField = COffsetVal - RemainderOffset;
7807 
7808     if (ST.hasNegativeUnalignedScratchOffsetBug() &&
7809         FlatVariant == SIInstrFlags::FlatScratch && ImmField < 0 &&
7810         (ImmField % 4) != 0) {
7811       // Make ImmField a multiple of 4
7812       RemainderOffset += ImmField % 4;
7813       ImmField -= ImmField % 4;
7814     }
7815   } else if (COffsetVal >= 0) {
7816     ImmField = COffsetVal & maskTrailingOnes<uint64_t>(NumBits);
7817     RemainderOffset = COffsetVal - ImmField;
7818   }
7819 
7820   assert(isLegalFLATOffset(ImmField, AddrSpace, FlatVariant));
7821   assert(RemainderOffset + ImmField == COffsetVal);
7822   return {ImmField, RemainderOffset};
7823 }
7824 
7825 // This must be kept in sync with the SIEncodingFamily class in SIInstrInfo.td
7826 enum SIEncodingFamily {
7827   SI = 0,
7828   VI = 1,
7829   SDWA = 2,
7830   SDWA9 = 3,
7831   GFX80 = 4,
7832   GFX9 = 5,
7833   GFX10 = 6,
7834   SDWA10 = 7,
7835   GFX90A = 8,
7836   GFX940 = 9
7837 };
7838 
7839 static SIEncodingFamily subtargetEncodingFamily(const GCNSubtarget &ST) {
7840   switch (ST.getGeneration()) {
7841   default:
7842     break;
7843   case AMDGPUSubtarget::SOUTHERN_ISLANDS:
7844   case AMDGPUSubtarget::SEA_ISLANDS:
7845     return SIEncodingFamily::SI;
7846   case AMDGPUSubtarget::VOLCANIC_ISLANDS:
7847   case AMDGPUSubtarget::GFX9:
7848     return SIEncodingFamily::VI;
7849   case AMDGPUSubtarget::GFX10:
7850     return SIEncodingFamily::GFX10;
7851   }
7852   llvm_unreachable("Unknown subtarget generation!");
7853 }
7854 
7855 bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const {
7856   switch(MCOp) {
7857   // These opcodes use indirect register addressing so
7858   // they need special handling by codegen (currently missing).
7859   // Therefore it is too risky to allow these opcodes
7860   // to be selected by dpp combiner or sdwa peepholer.
7861   case AMDGPU::V_MOVRELS_B32_dpp_gfx10:
7862   case AMDGPU::V_MOVRELS_B32_sdwa_gfx10:
7863   case AMDGPU::V_MOVRELD_B32_dpp_gfx10:
7864   case AMDGPU::V_MOVRELD_B32_sdwa_gfx10:
7865   case AMDGPU::V_MOVRELSD_B32_dpp_gfx10:
7866   case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10:
7867   case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10:
7868   case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10:
7869     return true;
7870   default:
7871     return false;
7872   }
7873 }
7874 
7875 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const {
7876   SIEncodingFamily Gen = subtargetEncodingFamily(ST);
7877 
7878   if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 &&
7879     ST.getGeneration() == AMDGPUSubtarget::GFX9)
7880     Gen = SIEncodingFamily::GFX9;
7881 
7882   // Adjust the encoding family to GFX80 for D16 buffer instructions when the
7883   // subtarget has UnpackedD16VMem feature.
7884   // TODO: remove this when we discard GFX80 encoding.
7885   if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf))
7886     Gen = SIEncodingFamily::GFX80;
7887 
7888   if (get(Opcode).TSFlags & SIInstrFlags::SDWA) {
7889     switch (ST.getGeneration()) {
7890     default:
7891       Gen = SIEncodingFamily::SDWA;
7892       break;
7893     case AMDGPUSubtarget::GFX9:
7894       Gen = SIEncodingFamily::SDWA9;
7895       break;
7896     case AMDGPUSubtarget::GFX10:
7897       Gen = SIEncodingFamily::SDWA10;
7898       break;
7899     }
7900   }
7901 
7902   if (isMAI(Opcode)) {
7903     int MFMAOp = AMDGPU::getMFMAEarlyClobberOp(Opcode);
7904     if (MFMAOp != -1)
7905       Opcode = MFMAOp;
7906   }
7907 
7908   int MCOp = AMDGPU::getMCOpcode(Opcode, Gen);
7909 
7910   // -1 means that Opcode is already a native instruction.
7911   if (MCOp == -1)
7912     return Opcode;
7913 
7914   if (ST.hasGFX90AInsts()) {
7915     uint16_t NMCOp = (uint16_t)-1;
7916     if (ST.hasGFX940Insts())
7917       NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX940);
7918     if (NMCOp == (uint16_t)-1)
7919       NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX90A);
7920     if (NMCOp == (uint16_t)-1)
7921       NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX9);
7922     if (NMCOp != (uint16_t)-1)
7923       MCOp = NMCOp;
7924   }
7925 
7926   // (uint16_t)-1 means that Opcode is a pseudo instruction that has
7927   // no encoding in the given subtarget generation.
7928   if (MCOp == (uint16_t)-1)
7929     return -1;
7930 
7931   if (isAsmOnlyOpcode(MCOp))
7932     return -1;
7933 
7934   return MCOp;
7935 }
7936 
7937 static
7938 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) {
7939   assert(RegOpnd.isReg());
7940   return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() :
7941                              getRegSubRegPair(RegOpnd);
7942 }
7943 
7944 TargetInstrInfo::RegSubRegPair
7945 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) {
7946   assert(MI.isRegSequence());
7947   for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I)
7948     if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) {
7949       auto &RegOp = MI.getOperand(1 + 2 * I);
7950       return getRegOrUndef(RegOp);
7951     }
7952   return TargetInstrInfo::RegSubRegPair();
7953 }
7954 
7955 // Try to find the definition of reg:subreg in subreg-manipulation pseudos
7956 // Following a subreg of reg:subreg isn't supported
7957 static bool followSubRegDef(MachineInstr &MI,
7958                             TargetInstrInfo::RegSubRegPair &RSR) {
7959   if (!RSR.SubReg)
7960     return false;
7961   switch (MI.getOpcode()) {
7962   default: break;
7963   case AMDGPU::REG_SEQUENCE:
7964     RSR = getRegSequenceSubReg(MI, RSR.SubReg);
7965     return true;
7966   // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg
7967   case AMDGPU::INSERT_SUBREG:
7968     if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm())
7969       // inserted the subreg we're looking for
7970       RSR = getRegOrUndef(MI.getOperand(2));
7971     else { // the subreg in the rest of the reg
7972       auto R1 = getRegOrUndef(MI.getOperand(1));
7973       if (R1.SubReg) // subreg of subreg isn't supported
7974         return false;
7975       RSR.Reg = R1.Reg;
7976     }
7977     return true;
7978   }
7979   return false;
7980 }
7981 
7982 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P,
7983                                      MachineRegisterInfo &MRI) {
7984   assert(MRI.isSSA());
7985   if (!P.Reg.isVirtual())
7986     return nullptr;
7987 
7988   auto RSR = P;
7989   auto *DefInst = MRI.getVRegDef(RSR.Reg);
7990   while (auto *MI = DefInst) {
7991     DefInst = nullptr;
7992     switch (MI->getOpcode()) {
7993     case AMDGPU::COPY:
7994     case AMDGPU::V_MOV_B32_e32: {
7995       auto &Op1 = MI->getOperand(1);
7996       if (Op1.isReg() && Op1.getReg().isVirtual()) {
7997         if (Op1.isUndef())
7998           return nullptr;
7999         RSR = getRegSubRegPair(Op1);
8000         DefInst = MRI.getVRegDef(RSR.Reg);
8001       }
8002       break;
8003     }
8004     default:
8005       if (followSubRegDef(*MI, RSR)) {
8006         if (!RSR.Reg)
8007           return nullptr;
8008         DefInst = MRI.getVRegDef(RSR.Reg);
8009       }
8010     }
8011     if (!DefInst)
8012       return MI;
8013   }
8014   return nullptr;
8015 }
8016 
8017 bool llvm::execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI,
8018                                       Register VReg,
8019                                       const MachineInstr &DefMI,
8020                                       const MachineInstr &UseMI) {
8021   assert(MRI.isSSA() && "Must be run on SSA");
8022 
8023   auto *TRI = MRI.getTargetRegisterInfo();
8024   auto *DefBB = DefMI.getParent();
8025 
8026   // Don't bother searching between blocks, although it is possible this block
8027   // doesn't modify exec.
8028   if (UseMI.getParent() != DefBB)
8029     return true;
8030 
8031   const int MaxInstScan = 20;
8032   int NumInst = 0;
8033 
8034   // Stop scan at the use.
8035   auto E = UseMI.getIterator();
8036   for (auto I = std::next(DefMI.getIterator()); I != E; ++I) {
8037     if (I->isDebugInstr())
8038       continue;
8039 
8040     if (++NumInst > MaxInstScan)
8041       return true;
8042 
8043     if (I->modifiesRegister(AMDGPU::EXEC, TRI))
8044       return true;
8045   }
8046 
8047   return false;
8048 }
8049 
8050 bool llvm::execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI,
8051                                          Register VReg,
8052                                          const MachineInstr &DefMI) {
8053   assert(MRI.isSSA() && "Must be run on SSA");
8054 
8055   auto *TRI = MRI.getTargetRegisterInfo();
8056   auto *DefBB = DefMI.getParent();
8057 
8058   const int MaxUseScan = 10;
8059   int NumUse = 0;
8060 
8061   for (auto &Use : MRI.use_nodbg_operands(VReg)) {
8062     auto &UseInst = *Use.getParent();
8063     // Don't bother searching between blocks, although it is possible this block
8064     // doesn't modify exec.
8065     if (UseInst.getParent() != DefBB)
8066       return true;
8067 
8068     if (++NumUse > MaxUseScan)
8069       return true;
8070   }
8071 
8072   if (NumUse == 0)
8073     return false;
8074 
8075   const int MaxInstScan = 20;
8076   int NumInst = 0;
8077 
8078   // Stop scan when we have seen all the uses.
8079   for (auto I = std::next(DefMI.getIterator()); ; ++I) {
8080     assert(I != DefBB->end());
8081 
8082     if (I->isDebugInstr())
8083       continue;
8084 
8085     if (++NumInst > MaxInstScan)
8086       return true;
8087 
8088     for (const MachineOperand &Op : I->operands()) {
8089       // We don't check reg masks here as they're used only on calls:
8090       // 1. EXEC is only considered const within one BB
8091       // 2. Call should be a terminator instruction if present in a BB
8092 
8093       if (!Op.isReg())
8094         continue;
8095 
8096       Register Reg = Op.getReg();
8097       if (Op.isUse()) {
8098         if (Reg == VReg && --NumUse == 0)
8099           return false;
8100       } else if (TRI->regsOverlap(Reg, AMDGPU::EXEC))
8101         return true;
8102     }
8103   }
8104 }
8105 
8106 MachineInstr *SIInstrInfo::createPHIDestinationCopy(
8107     MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt,
8108     const DebugLoc &DL, Register Src, Register Dst) const {
8109   auto Cur = MBB.begin();
8110   if (Cur != MBB.end())
8111     do {
8112       if (!Cur->isPHI() && Cur->readsRegister(Dst))
8113         return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src);
8114       ++Cur;
8115     } while (Cur != MBB.end() && Cur != LastPHIIt);
8116 
8117   return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src,
8118                                                    Dst);
8119 }
8120 
8121 MachineInstr *SIInstrInfo::createPHISourceCopy(
8122     MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt,
8123     const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const {
8124   if (InsPt != MBB.end() &&
8125       (InsPt->getOpcode() == AMDGPU::SI_IF ||
8126        InsPt->getOpcode() == AMDGPU::SI_ELSE ||
8127        InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) &&
8128       InsPt->definesRegister(Src)) {
8129     InsPt++;
8130     return BuildMI(MBB, InsPt, DL,
8131                    get(ST.isWave32() ? AMDGPU::S_MOV_B32_term
8132                                      : AMDGPU::S_MOV_B64_term),
8133                    Dst)
8134         .addReg(Src, 0, SrcSubReg)
8135         .addReg(AMDGPU::EXEC, RegState::Implicit);
8136   }
8137   return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg,
8138                                               Dst);
8139 }
8140 
8141 bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); }
8142 
8143 MachineInstr *SIInstrInfo::foldMemoryOperandImpl(
8144     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
8145     MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
8146     VirtRegMap *VRM) const {
8147   // This is a bit of a hack (copied from AArch64). Consider this instruction:
8148   //
8149   //   %0:sreg_32 = COPY $m0
8150   //
8151   // We explicitly chose SReg_32 for the virtual register so such a copy might
8152   // be eliminated by RegisterCoalescer. However, that may not be possible, and
8153   // %0 may even spill. We can't spill $m0 normally (it would require copying to
8154   // a numbered SGPR anyway), and since it is in the SReg_32 register class,
8155   // TargetInstrInfo::foldMemoryOperand() is going to try.
8156   // A similar issue also exists with spilling and reloading $exec registers.
8157   //
8158   // To prevent that, constrain the %0 register class here.
8159   if (MI.isFullCopy()) {
8160     Register DstReg = MI.getOperand(0).getReg();
8161     Register SrcReg = MI.getOperand(1).getReg();
8162     if ((DstReg.isVirtual() || SrcReg.isVirtual()) &&
8163         (DstReg.isVirtual() != SrcReg.isVirtual())) {
8164       MachineRegisterInfo &MRI = MF.getRegInfo();
8165       Register VirtReg = DstReg.isVirtual() ? DstReg : SrcReg;
8166       const TargetRegisterClass *RC = MRI.getRegClass(VirtReg);
8167       if (RC->hasSuperClassEq(&AMDGPU::SReg_32RegClass)) {
8168         MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_32_XM0_XEXECRegClass);
8169         return nullptr;
8170       } else if (RC->hasSuperClassEq(&AMDGPU::SReg_64RegClass)) {
8171         MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_64_XEXECRegClass);
8172         return nullptr;
8173       }
8174     }
8175   }
8176 
8177   return nullptr;
8178 }
8179 
8180 unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
8181                                       const MachineInstr &MI,
8182                                       unsigned *PredCost) const {
8183   if (MI.isBundle()) {
8184     MachineBasicBlock::const_instr_iterator I(MI.getIterator());
8185     MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end());
8186     unsigned Lat = 0, Count = 0;
8187     for (++I; I != E && I->isBundledWithPred(); ++I) {
8188       ++Count;
8189       Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I));
8190     }
8191     return Lat + Count - 1;
8192   }
8193 
8194   return SchedModel.computeInstrLatency(&MI);
8195 }
8196 
8197 unsigned SIInstrInfo::getDSShaderTypeValue(const MachineFunction &MF) {
8198   switch (MF.getFunction().getCallingConv()) {
8199   case CallingConv::AMDGPU_PS:
8200     return 1;
8201   case CallingConv::AMDGPU_VS:
8202     return 2;
8203   case CallingConv::AMDGPU_GS:
8204     return 3;
8205   case CallingConv::AMDGPU_HS:
8206   case CallingConv::AMDGPU_LS:
8207   case CallingConv::AMDGPU_ES:
8208     report_fatal_error("ds_ordered_count unsupported for this calling conv");
8209   case CallingConv::AMDGPU_CS:
8210   case CallingConv::AMDGPU_KERNEL:
8211   case CallingConv::C:
8212   case CallingConv::Fast:
8213   default:
8214     // Assume other calling conventions are various compute callable functions
8215     return 0;
8216   }
8217 }
8218 
8219 bool SIInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
8220                                  Register &SrcReg2, int64_t &CmpMask,
8221                                  int64_t &CmpValue) const {
8222   if (!MI.getOperand(0).isReg() || MI.getOperand(0).getSubReg())
8223     return false;
8224 
8225   switch (MI.getOpcode()) {
8226   default:
8227     break;
8228   case AMDGPU::S_CMP_EQ_U32:
8229   case AMDGPU::S_CMP_EQ_I32:
8230   case AMDGPU::S_CMP_LG_U32:
8231   case AMDGPU::S_CMP_LG_I32:
8232   case AMDGPU::S_CMP_LT_U32:
8233   case AMDGPU::S_CMP_LT_I32:
8234   case AMDGPU::S_CMP_GT_U32:
8235   case AMDGPU::S_CMP_GT_I32:
8236   case AMDGPU::S_CMP_LE_U32:
8237   case AMDGPU::S_CMP_LE_I32:
8238   case AMDGPU::S_CMP_GE_U32:
8239   case AMDGPU::S_CMP_GE_I32:
8240   case AMDGPU::S_CMP_EQ_U64:
8241   case AMDGPU::S_CMP_LG_U64:
8242     SrcReg = MI.getOperand(0).getReg();
8243     if (MI.getOperand(1).isReg()) {
8244       if (MI.getOperand(1).getSubReg())
8245         return false;
8246       SrcReg2 = MI.getOperand(1).getReg();
8247       CmpValue = 0;
8248     } else if (MI.getOperand(1).isImm()) {
8249       SrcReg2 = Register();
8250       CmpValue = MI.getOperand(1).getImm();
8251     } else {
8252       return false;
8253     }
8254     CmpMask = ~0;
8255     return true;
8256   case AMDGPU::S_CMPK_EQ_U32:
8257   case AMDGPU::S_CMPK_EQ_I32:
8258   case AMDGPU::S_CMPK_LG_U32:
8259   case AMDGPU::S_CMPK_LG_I32:
8260   case AMDGPU::S_CMPK_LT_U32:
8261   case AMDGPU::S_CMPK_LT_I32:
8262   case AMDGPU::S_CMPK_GT_U32:
8263   case AMDGPU::S_CMPK_GT_I32:
8264   case AMDGPU::S_CMPK_LE_U32:
8265   case AMDGPU::S_CMPK_LE_I32:
8266   case AMDGPU::S_CMPK_GE_U32:
8267   case AMDGPU::S_CMPK_GE_I32:
8268     SrcReg = MI.getOperand(0).getReg();
8269     SrcReg2 = Register();
8270     CmpValue = MI.getOperand(1).getImm();
8271     CmpMask = ~0;
8272     return true;
8273   }
8274 
8275   return false;
8276 }
8277 
8278 bool SIInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
8279                                        Register SrcReg2, int64_t CmpMask,
8280                                        int64_t CmpValue,
8281                                        const MachineRegisterInfo *MRI) const {
8282   if (!SrcReg || SrcReg.isPhysical())
8283     return false;
8284 
8285   if (SrcReg2 && !getFoldableImm(SrcReg2, *MRI, CmpValue))
8286     return false;
8287 
8288   const auto optimizeCmpAnd = [&CmpInstr, SrcReg, CmpValue, MRI,
8289                                this](int64_t ExpectedValue, unsigned SrcSize,
8290                                      bool IsReversible, bool IsSigned) -> bool {
8291     // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8292     // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8293     // s_cmp_ge_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8294     // s_cmp_ge_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8295     // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 1 << n => s_and_b64 $src, 1 << n
8296     // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8297     // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8298     // s_cmp_gt_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8299     // s_cmp_gt_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8300     // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 0 => s_and_b64 $src, 1 << n
8301     //
8302     // Signed ge/gt are not used for the sign bit.
8303     //
8304     // If result of the AND is unused except in the compare:
8305     // s_and_b(32|64) $src, 1 << n => s_bitcmp1_b(32|64) $src, n
8306     //
8307     // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n
8308     // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n
8309     // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 0 => s_bitcmp0_b64 $src, n
8310     // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n
8311     // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n
8312     // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 1 << n => s_bitcmp0_b64 $src, n
8313 
8314     MachineInstr *Def = MRI->getUniqueVRegDef(SrcReg);
8315     if (!Def || Def->getParent() != CmpInstr.getParent())
8316       return false;
8317 
8318     if (Def->getOpcode() != AMDGPU::S_AND_B32 &&
8319         Def->getOpcode() != AMDGPU::S_AND_B64)
8320       return false;
8321 
8322     int64_t Mask;
8323     const auto isMask = [&Mask, SrcSize](const MachineOperand *MO) -> bool {
8324       if (MO->isImm())
8325         Mask = MO->getImm();
8326       else if (!getFoldableImm(MO, Mask))
8327         return false;
8328       Mask &= maxUIntN(SrcSize);
8329       return isPowerOf2_64(Mask);
8330     };
8331 
8332     MachineOperand *SrcOp = &Def->getOperand(1);
8333     if (isMask(SrcOp))
8334       SrcOp = &Def->getOperand(2);
8335     else if (isMask(&Def->getOperand(2)))
8336       SrcOp = &Def->getOperand(1);
8337     else
8338       return false;
8339 
8340     unsigned BitNo = countTrailingZeros((uint64_t)Mask);
8341     if (IsSigned && BitNo == SrcSize - 1)
8342       return false;
8343 
8344     ExpectedValue <<= BitNo;
8345 
8346     bool IsReversedCC = false;
8347     if (CmpValue != ExpectedValue) {
8348       if (!IsReversible)
8349         return false;
8350       IsReversedCC = CmpValue == (ExpectedValue ^ Mask);
8351       if (!IsReversedCC)
8352         return false;
8353     }
8354 
8355     Register DefReg = Def->getOperand(0).getReg();
8356     if (IsReversedCC && !MRI->hasOneNonDBGUse(DefReg))
8357       return false;
8358 
8359     for (auto I = std::next(Def->getIterator()), E = CmpInstr.getIterator();
8360          I != E; ++I) {
8361       if (I->modifiesRegister(AMDGPU::SCC, &RI) ||
8362           I->killsRegister(AMDGPU::SCC, &RI))
8363         return false;
8364     }
8365 
8366     MachineOperand *SccDef = Def->findRegisterDefOperand(AMDGPU::SCC);
8367     SccDef->setIsDead(false);
8368     CmpInstr.eraseFromParent();
8369 
8370     if (!MRI->use_nodbg_empty(DefReg)) {
8371       assert(!IsReversedCC);
8372       return true;
8373     }
8374 
8375     // Replace AND with unused result with a S_BITCMP.
8376     MachineBasicBlock *MBB = Def->getParent();
8377 
8378     unsigned NewOpc = (SrcSize == 32) ? IsReversedCC ? AMDGPU::S_BITCMP0_B32
8379                                                      : AMDGPU::S_BITCMP1_B32
8380                                       : IsReversedCC ? AMDGPU::S_BITCMP0_B64
8381                                                      : AMDGPU::S_BITCMP1_B64;
8382 
8383     BuildMI(*MBB, Def, Def->getDebugLoc(), get(NewOpc))
8384       .add(*SrcOp)
8385       .addImm(BitNo);
8386     Def->eraseFromParent();
8387 
8388     return true;
8389   };
8390 
8391   switch (CmpInstr.getOpcode()) {
8392   default:
8393     break;
8394   case AMDGPU::S_CMP_EQ_U32:
8395   case AMDGPU::S_CMP_EQ_I32:
8396   case AMDGPU::S_CMPK_EQ_U32:
8397   case AMDGPU::S_CMPK_EQ_I32:
8398     return optimizeCmpAnd(1, 32, true, false);
8399   case AMDGPU::S_CMP_GE_U32:
8400   case AMDGPU::S_CMPK_GE_U32:
8401     return optimizeCmpAnd(1, 32, false, false);
8402   case AMDGPU::S_CMP_GE_I32:
8403   case AMDGPU::S_CMPK_GE_I32:
8404     return optimizeCmpAnd(1, 32, false, true);
8405   case AMDGPU::S_CMP_EQ_U64:
8406     return optimizeCmpAnd(1, 64, true, false);
8407   case AMDGPU::S_CMP_LG_U32:
8408   case AMDGPU::S_CMP_LG_I32:
8409   case AMDGPU::S_CMPK_LG_U32:
8410   case AMDGPU::S_CMPK_LG_I32:
8411     return optimizeCmpAnd(0, 32, true, false);
8412   case AMDGPU::S_CMP_GT_U32:
8413   case AMDGPU::S_CMPK_GT_U32:
8414     return optimizeCmpAnd(0, 32, false, false);
8415   case AMDGPU::S_CMP_GT_I32:
8416   case AMDGPU::S_CMPK_GT_I32:
8417     return optimizeCmpAnd(0, 32, false, true);
8418   case AMDGPU::S_CMP_LG_U64:
8419     return optimizeCmpAnd(0, 64, true, false);
8420   }
8421 
8422   return false;
8423 }
8424