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   if (AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst) != -1) {
3871     // dst
3872     Inst32.add(MI.getOperand(0));
3873   } else if (AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::sdst) != -1) {
3874     // VOPCX instructions won't be writing to an explicit dst, so this should
3875     // not fail for these instructions.
3876     assert(((MI.getOperand(0).getReg() == AMDGPU::VCC) ||
3877             (MI.getOperand(0).getReg() == AMDGPU::VCC_LO)) &&
3878            "Unexpected case");
3879   }
3880 
3881   Inst32.add(*getNamedOperand(MI, AMDGPU::OpName::src0));
3882 
3883   const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1);
3884   if (Src1)
3885     Inst32.add(*Src1);
3886 
3887   const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2);
3888 
3889   if (Src2) {
3890     int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2);
3891     if (Op32Src2Idx != -1) {
3892       Inst32.add(*Src2);
3893     } else {
3894       // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is
3895       // replaced with an implicit read of vcc or vcc_lo. The implicit read
3896       // of vcc was already added during the initial BuildMI, but we
3897       // 1) may need to change vcc to vcc_lo to preserve the original register
3898       // 2) have to preserve the original flags.
3899       fixImplicitOperands(*Inst32);
3900       copyFlagsToImplicitVCC(*Inst32, *Src2);
3901     }
3902   }
3903 
3904   return Inst32;
3905 }
3906 
3907 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI,
3908                                   const MachineOperand &MO,
3909                                   const MCOperandInfo &OpInfo) const {
3910   // Literal constants use the constant bus.
3911   //if (isLiteralConstantLike(MO, OpInfo))
3912   // return true;
3913   if (MO.isImm())
3914     return !isInlineConstant(MO, OpInfo);
3915 
3916   if (!MO.isReg())
3917     return true; // Misc other operands like FrameIndex
3918 
3919   if (!MO.isUse())
3920     return false;
3921 
3922   if (MO.getReg().isVirtual())
3923     return RI.isSGPRClass(MRI.getRegClass(MO.getReg()));
3924 
3925   // Null is free
3926   if (MO.getReg() == AMDGPU::SGPR_NULL)
3927     return false;
3928 
3929   // SGPRs use the constant bus
3930   if (MO.isImplicit()) {
3931     return MO.getReg() == AMDGPU::M0 ||
3932            MO.getReg() == AMDGPU::VCC ||
3933            MO.getReg() == AMDGPU::VCC_LO;
3934   } else {
3935     return AMDGPU::SReg_32RegClass.contains(MO.getReg()) ||
3936            AMDGPU::SReg_64RegClass.contains(MO.getReg());
3937   }
3938 }
3939 
3940 static Register findImplicitSGPRRead(const MachineInstr &MI) {
3941   for (const MachineOperand &MO : MI.implicit_operands()) {
3942     // We only care about reads.
3943     if (MO.isDef())
3944       continue;
3945 
3946     switch (MO.getReg()) {
3947     case AMDGPU::VCC:
3948     case AMDGPU::VCC_LO:
3949     case AMDGPU::VCC_HI:
3950     case AMDGPU::M0:
3951     case AMDGPU::FLAT_SCR:
3952       return MO.getReg();
3953 
3954     default:
3955       break;
3956     }
3957   }
3958 
3959   return AMDGPU::NoRegister;
3960 }
3961 
3962 static bool shouldReadExec(const MachineInstr &MI) {
3963   if (SIInstrInfo::isVALU(MI)) {
3964     switch (MI.getOpcode()) {
3965     case AMDGPU::V_READLANE_B32:
3966     case AMDGPU::V_WRITELANE_B32:
3967       return false;
3968     }
3969 
3970     return true;
3971   }
3972 
3973   if (MI.isPreISelOpcode() ||
3974       SIInstrInfo::isGenericOpcode(MI.getOpcode()) ||
3975       SIInstrInfo::isSALU(MI) ||
3976       SIInstrInfo::isSMRD(MI))
3977     return false;
3978 
3979   return true;
3980 }
3981 
3982 static bool isSubRegOf(const SIRegisterInfo &TRI,
3983                        const MachineOperand &SuperVec,
3984                        const MachineOperand &SubReg) {
3985   if (SubReg.getReg().isPhysical())
3986     return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg());
3987 
3988   return SubReg.getSubReg() != AMDGPU::NoSubRegister &&
3989          SubReg.getReg() == SuperVec.getReg();
3990 }
3991 
3992 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI,
3993                                     StringRef &ErrInfo) const {
3994   uint16_t Opcode = MI.getOpcode();
3995   if (SIInstrInfo::isGenericOpcode(MI.getOpcode()))
3996     return true;
3997 
3998   const MachineFunction *MF = MI.getParent()->getParent();
3999   const MachineRegisterInfo &MRI = MF->getRegInfo();
4000 
4001   int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
4002   int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
4003   int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
4004 
4005   // Make sure the number of operands is correct.
4006   const MCInstrDesc &Desc = get(Opcode);
4007   if (!Desc.isVariadic() &&
4008       Desc.getNumOperands() != MI.getNumExplicitOperands()) {
4009     ErrInfo = "Instruction has wrong number of operands.";
4010     return false;
4011   }
4012 
4013   if (MI.isInlineAsm()) {
4014     // Verify register classes for inlineasm constraints.
4015     for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands();
4016          I != E; ++I) {
4017       const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI);
4018       if (!RC)
4019         continue;
4020 
4021       const MachineOperand &Op = MI.getOperand(I);
4022       if (!Op.isReg())
4023         continue;
4024 
4025       Register Reg = Op.getReg();
4026       if (!Reg.isVirtual() && !RC->contains(Reg)) {
4027         ErrInfo = "inlineasm operand has incorrect register class.";
4028         return false;
4029       }
4030     }
4031 
4032     return true;
4033   }
4034 
4035   if (isMIMG(MI) && MI.memoperands_empty() && MI.mayLoadOrStore()) {
4036     ErrInfo = "missing memory operand from MIMG instruction.";
4037     return false;
4038   }
4039 
4040   // Make sure the register classes are correct.
4041   for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) {
4042     const MachineOperand &MO = MI.getOperand(i);
4043     if (MO.isFPImm()) {
4044       ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast "
4045                 "all fp values to integers.";
4046       return false;
4047     }
4048 
4049     int RegClass = Desc.OpInfo[i].RegClass;
4050 
4051     switch (Desc.OpInfo[i].OperandType) {
4052     case MCOI::OPERAND_REGISTER:
4053       if (MI.getOperand(i).isImm() || MI.getOperand(i).isGlobal()) {
4054         ErrInfo = "Illegal immediate value for operand.";
4055         return false;
4056       }
4057       break;
4058     case AMDGPU::OPERAND_REG_IMM_INT32:
4059     case AMDGPU::OPERAND_REG_IMM_FP32:
4060     case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED:
4061     case AMDGPU::OPERAND_REG_IMM_V2FP32:
4062       break;
4063     case AMDGPU::OPERAND_REG_INLINE_C_INT32:
4064     case AMDGPU::OPERAND_REG_INLINE_C_FP32:
4065     case AMDGPU::OPERAND_REG_INLINE_C_INT64:
4066     case AMDGPU::OPERAND_REG_INLINE_C_FP64:
4067     case AMDGPU::OPERAND_REG_INLINE_C_INT16:
4068     case AMDGPU::OPERAND_REG_INLINE_C_FP16:
4069     case AMDGPU::OPERAND_REG_INLINE_AC_INT32:
4070     case AMDGPU::OPERAND_REG_INLINE_AC_FP32:
4071     case AMDGPU::OPERAND_REG_INLINE_AC_INT16:
4072     case AMDGPU::OPERAND_REG_INLINE_AC_FP16:
4073     case AMDGPU::OPERAND_REG_INLINE_AC_FP64: {
4074       if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) {
4075         ErrInfo = "Illegal immediate value for operand.";
4076         return false;
4077       }
4078       break;
4079     }
4080     case MCOI::OPERAND_IMMEDIATE:
4081     case AMDGPU::OPERAND_KIMM32:
4082       // Check if this operand is an immediate.
4083       // FrameIndex operands will be replaced by immediates, so they are
4084       // allowed.
4085       if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) {
4086         ErrInfo = "Expected immediate, but got non-immediate";
4087         return false;
4088       }
4089       LLVM_FALLTHROUGH;
4090     default:
4091       continue;
4092     }
4093 
4094     if (!MO.isReg())
4095       continue;
4096     Register Reg = MO.getReg();
4097     if (!Reg)
4098       continue;
4099 
4100     // FIXME: Ideally we would have separate instruction definitions with the
4101     // aligned register constraint.
4102     // FIXME: We do not verify inline asm operands, but custom inline asm
4103     // verification is broken anyway
4104     if (ST.needsAlignedVGPRs()) {
4105       const TargetRegisterClass *RC = RI.getRegClassForReg(MRI, Reg);
4106       if (RI.hasVectorRegisters(RC) && MO.getSubReg()) {
4107         const TargetRegisterClass *SubRC =
4108             RI.getSubRegClass(RC, MO.getSubReg());
4109         RC = RI.getCompatibleSubRegClass(RC, SubRC, MO.getSubReg());
4110         if (RC)
4111           RC = SubRC;
4112       }
4113 
4114       // Check that this is the aligned version of the class.
4115       if (!RC || !RI.isProperlyAlignedRC(*RC)) {
4116         ErrInfo = "Subtarget requires even aligned vector registers";
4117         return false;
4118       }
4119     }
4120 
4121     if (RegClass != -1) {
4122       if (Reg.isVirtual())
4123         continue;
4124 
4125       const TargetRegisterClass *RC = RI.getRegClass(RegClass);
4126       if (!RC->contains(Reg)) {
4127         ErrInfo = "Operand has incorrect register class.";
4128         return false;
4129       }
4130     }
4131   }
4132 
4133   // Verify SDWA
4134   if (isSDWA(MI)) {
4135     if (!ST.hasSDWA()) {
4136       ErrInfo = "SDWA is not supported on this target";
4137       return false;
4138     }
4139 
4140     int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst);
4141 
4142     for (int OpIdx : {DstIdx, Src0Idx, Src1Idx, Src2Idx}) {
4143       if (OpIdx == -1)
4144         continue;
4145       const MachineOperand &MO = MI.getOperand(OpIdx);
4146 
4147       if (!ST.hasSDWAScalar()) {
4148         // Only VGPRS on VI
4149         if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) {
4150           ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI";
4151           return false;
4152         }
4153       } else {
4154         // No immediates on GFX9
4155         if (!MO.isReg()) {
4156           ErrInfo =
4157             "Only reg allowed as operands in SDWA instructions on GFX9+";
4158           return false;
4159         }
4160       }
4161     }
4162 
4163     if (!ST.hasSDWAOmod()) {
4164       // No omod allowed on VI
4165       const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
4166       if (OMod != nullptr &&
4167         (!OMod->isImm() || OMod->getImm() != 0)) {
4168         ErrInfo = "OMod not allowed in SDWA instructions on VI";
4169         return false;
4170       }
4171     }
4172 
4173     uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode);
4174     if (isVOPC(BasicOpcode)) {
4175       if (!ST.hasSDWASdst() && DstIdx != -1) {
4176         // Only vcc allowed as dst on VI for VOPC
4177         const MachineOperand &Dst = MI.getOperand(DstIdx);
4178         if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) {
4179           ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI";
4180           return false;
4181         }
4182       } else if (!ST.hasSDWAOutModsVOPC()) {
4183         // No clamp allowed on GFX9 for VOPC
4184         const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp);
4185         if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) {
4186           ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI";
4187           return false;
4188         }
4189 
4190         // No omod allowed on GFX9 for VOPC
4191         const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod);
4192         if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) {
4193           ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI";
4194           return false;
4195         }
4196       }
4197     }
4198 
4199     const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused);
4200     if (DstUnused && DstUnused->isImm() &&
4201         DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) {
4202       const MachineOperand &Dst = MI.getOperand(DstIdx);
4203       if (!Dst.isReg() || !Dst.isTied()) {
4204         ErrInfo = "Dst register should have tied register";
4205         return false;
4206       }
4207 
4208       const MachineOperand &TiedMO =
4209           MI.getOperand(MI.findTiedOperandIdx(DstIdx));
4210       if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) {
4211         ErrInfo =
4212             "Dst register should be tied to implicit use of preserved register";
4213         return false;
4214       } else if (TiedMO.getReg().isPhysical() &&
4215                  Dst.getReg() != TiedMO.getReg()) {
4216         ErrInfo = "Dst register should use same physical register as preserved";
4217         return false;
4218       }
4219     }
4220   }
4221 
4222   // Verify MIMG
4223   if (isMIMG(MI.getOpcode()) && !MI.mayStore()) {
4224     // Ensure that the return type used is large enough for all the options
4225     // being used TFE/LWE require an extra result register.
4226     const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask);
4227     if (DMask) {
4228       uint64_t DMaskImm = DMask->getImm();
4229       uint32_t RegCount =
4230           isGather4(MI.getOpcode()) ? 4 : countPopulation(DMaskImm);
4231       const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe);
4232       const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe);
4233       const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16);
4234 
4235       // Adjust for packed 16 bit values
4236       if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem())
4237         RegCount >>= 1;
4238 
4239       // Adjust if using LWE or TFE
4240       if ((LWE && LWE->getImm()) || (TFE && TFE->getImm()))
4241         RegCount += 1;
4242 
4243       const uint32_t DstIdx =
4244           AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata);
4245       const MachineOperand &Dst = MI.getOperand(DstIdx);
4246       if (Dst.isReg()) {
4247         const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx);
4248         uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32;
4249         if (RegCount > DstSize) {
4250           ErrInfo = "MIMG instruction returns too many registers for dst "
4251                     "register class";
4252           return false;
4253         }
4254       }
4255     }
4256   }
4257 
4258   // Verify VOP*. Ignore multiple sgpr operands on writelane.
4259   if (isVALU(MI) && Desc.getOpcode() != AMDGPU::V_WRITELANE_B32) {
4260     unsigned ConstantBusCount = 0;
4261     bool UsesLiteral = false;
4262     const MachineOperand *LiteralVal = nullptr;
4263 
4264     if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1)
4265       ++ConstantBusCount;
4266 
4267     SmallVector<Register, 2> SGPRsUsed;
4268     Register SGPRUsed;
4269 
4270     // Only look at the true operands. Only a real operand can use the constant
4271     // bus, and we don't want to check pseudo-operands like the source modifier
4272     // flags.
4273     for (int OpIdx : {Src0Idx, Src1Idx, Src2Idx}) {
4274       if (OpIdx == -1)
4275         break;
4276       const MachineOperand &MO = MI.getOperand(OpIdx);
4277       if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) {
4278         if (MO.isReg()) {
4279           SGPRUsed = MO.getReg();
4280           if (llvm::all_of(SGPRsUsed, [SGPRUsed](unsigned SGPR) {
4281                 return SGPRUsed != SGPR;
4282               })) {
4283             ++ConstantBusCount;
4284             SGPRsUsed.push_back(SGPRUsed);
4285           }
4286         } else {
4287           if (!UsesLiteral) {
4288             ++ConstantBusCount;
4289             UsesLiteral = true;
4290             LiteralVal = &MO;
4291           } else if (!MO.isIdenticalTo(*LiteralVal)) {
4292             assert(isVOP3(MI));
4293             ErrInfo = "VOP3 instruction uses more than one literal";
4294             return false;
4295           }
4296         }
4297       }
4298     }
4299 
4300     SGPRUsed = findImplicitSGPRRead(MI);
4301     if (SGPRUsed != AMDGPU::NoRegister) {
4302       // Implicit uses may safely overlap true operands
4303       if (llvm::all_of(SGPRsUsed, [this, SGPRUsed](unsigned SGPR) {
4304             return !RI.regsOverlap(SGPRUsed, SGPR);
4305           })) {
4306         ++ConstantBusCount;
4307         SGPRsUsed.push_back(SGPRUsed);
4308       }
4309     }
4310 
4311     // v_writelane_b32 is an exception from constant bus restriction:
4312     // vsrc0 can be sgpr, const or m0 and lane select sgpr, m0 or inline-const
4313     if (ConstantBusCount > ST.getConstantBusLimit(Opcode) &&
4314         Opcode != AMDGPU::V_WRITELANE_B32) {
4315       ErrInfo = "VOP* instruction violates constant bus restriction";
4316       return false;
4317     }
4318 
4319     if (isVOP3(MI) && UsesLiteral && !ST.hasVOP3Literal()) {
4320       ErrInfo = "VOP3 instruction uses literal";
4321       return false;
4322     }
4323   }
4324 
4325   // Special case for writelane - this can break the multiple constant bus rule,
4326   // but still can't use more than one SGPR register
4327   if (Desc.getOpcode() == AMDGPU::V_WRITELANE_B32) {
4328     unsigned SGPRCount = 0;
4329     Register SGPRUsed = AMDGPU::NoRegister;
4330 
4331     for (int OpIdx : {Src0Idx, Src1Idx}) {
4332       if (OpIdx == -1)
4333         break;
4334 
4335       const MachineOperand &MO = MI.getOperand(OpIdx);
4336 
4337       if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) {
4338         if (MO.isReg() && MO.getReg() != AMDGPU::M0) {
4339           if (MO.getReg() != SGPRUsed)
4340             ++SGPRCount;
4341           SGPRUsed = MO.getReg();
4342         }
4343       }
4344       if (SGPRCount > ST.getConstantBusLimit(Opcode)) {
4345         ErrInfo = "WRITELANE instruction violates constant bus restriction";
4346         return false;
4347       }
4348     }
4349   }
4350 
4351   // Verify misc. restrictions on specific instructions.
4352   if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32_e64 ||
4353       Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64_e64) {
4354     const MachineOperand &Src0 = MI.getOperand(Src0Idx);
4355     const MachineOperand &Src1 = MI.getOperand(Src1Idx);
4356     const MachineOperand &Src2 = MI.getOperand(Src2Idx);
4357     if (Src0.isReg() && Src1.isReg() && Src2.isReg()) {
4358       if (!compareMachineOp(Src0, Src1) &&
4359           !compareMachineOp(Src0, Src2)) {
4360         ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2";
4361         return false;
4362       }
4363     }
4364     if ((getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm() &
4365          SISrcMods::ABS) ||
4366         (getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm() &
4367          SISrcMods::ABS) ||
4368         (getNamedOperand(MI, AMDGPU::OpName::src2_modifiers)->getImm() &
4369          SISrcMods::ABS)) {
4370       ErrInfo = "ABS not allowed in VOP3B instructions";
4371       return false;
4372     }
4373   }
4374 
4375   if (isSOP2(MI) || isSOPC(MI)) {
4376     const MachineOperand &Src0 = MI.getOperand(Src0Idx);
4377     const MachineOperand &Src1 = MI.getOperand(Src1Idx);
4378     unsigned Immediates = 0;
4379 
4380     if (!Src0.isReg() &&
4381         !isInlineConstant(Src0, Desc.OpInfo[Src0Idx].OperandType))
4382       Immediates++;
4383     if (!Src1.isReg() &&
4384         !isInlineConstant(Src1, Desc.OpInfo[Src1Idx].OperandType))
4385       Immediates++;
4386 
4387     if (Immediates > 1) {
4388       ErrInfo = "SOP2/SOPC instruction requires too many immediate constants";
4389       return false;
4390     }
4391   }
4392 
4393   if (isSOPK(MI)) {
4394     auto Op = getNamedOperand(MI, AMDGPU::OpName::simm16);
4395     if (Desc.isBranch()) {
4396       if (!Op->isMBB()) {
4397         ErrInfo = "invalid branch target for SOPK instruction";
4398         return false;
4399       }
4400     } else {
4401       uint64_t Imm = Op->getImm();
4402       if (sopkIsZext(MI)) {
4403         if (!isUInt<16>(Imm)) {
4404           ErrInfo = "invalid immediate for SOPK instruction";
4405           return false;
4406         }
4407       } else {
4408         if (!isInt<16>(Imm)) {
4409           ErrInfo = "invalid immediate for SOPK instruction";
4410           return false;
4411         }
4412       }
4413     }
4414   }
4415 
4416   if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 ||
4417       Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 ||
4418       Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
4419       Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) {
4420     const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 ||
4421                        Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64;
4422 
4423     const unsigned StaticNumOps = Desc.getNumOperands() +
4424       Desc.getNumImplicitUses();
4425     const unsigned NumImplicitOps = IsDst ? 2 : 1;
4426 
4427     // Allow additional implicit operands. This allows a fixup done by the post
4428     // RA scheduler where the main implicit operand is killed and implicit-defs
4429     // are added for sub-registers that remain live after this instruction.
4430     if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) {
4431       ErrInfo = "missing implicit register operands";
4432       return false;
4433     }
4434 
4435     const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
4436     if (IsDst) {
4437       if (!Dst->isUse()) {
4438         ErrInfo = "v_movreld_b32 vdst should be a use operand";
4439         return false;
4440       }
4441 
4442       unsigned UseOpIdx;
4443       if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) ||
4444           UseOpIdx != StaticNumOps + 1) {
4445         ErrInfo = "movrel implicit operands should be tied";
4446         return false;
4447       }
4448     }
4449 
4450     const MachineOperand &Src0 = MI.getOperand(Src0Idx);
4451     const MachineOperand &ImpUse
4452       = MI.getOperand(StaticNumOps + NumImplicitOps - 1);
4453     if (!ImpUse.isReg() || !ImpUse.isUse() ||
4454         !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) {
4455       ErrInfo = "src0 should be subreg of implicit vector use";
4456       return false;
4457     }
4458   }
4459 
4460   // Make sure we aren't losing exec uses in the td files. This mostly requires
4461   // being careful when using let Uses to try to add other use registers.
4462   if (shouldReadExec(MI)) {
4463     if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) {
4464       ErrInfo = "VALU instruction does not implicitly read exec mask";
4465       return false;
4466     }
4467   }
4468 
4469   if (isSMRD(MI)) {
4470     if (MI.mayStore()) {
4471       // The register offset form of scalar stores may only use m0 as the
4472       // soffset register.
4473       const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soff);
4474       if (Soff && Soff->getReg() != AMDGPU::M0) {
4475         ErrInfo = "scalar stores must use m0 as offset register";
4476         return false;
4477       }
4478     }
4479   }
4480 
4481   if (isFLAT(MI) && !ST.hasFlatInstOffsets()) {
4482     const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
4483     if (Offset->getImm() != 0) {
4484       ErrInfo = "subtarget does not support offsets in flat instructions";
4485       return false;
4486     }
4487   }
4488 
4489   if (isMIMG(MI)) {
4490     const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim);
4491     if (DimOp) {
4492       int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode,
4493                                                  AMDGPU::OpName::vaddr0);
4494       int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
4495       const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode);
4496       const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
4497           AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode);
4498       const AMDGPU::MIMGDimInfo *Dim =
4499           AMDGPU::getMIMGDimInfoByEncoding(DimOp->getImm());
4500 
4501       if (!Dim) {
4502         ErrInfo = "dim is out of range";
4503         return false;
4504       }
4505 
4506       bool IsA16 = false;
4507       if (ST.hasR128A16()) {
4508         const MachineOperand *R128A16 = getNamedOperand(MI, AMDGPU::OpName::r128);
4509         IsA16 = R128A16->getImm() != 0;
4510       } else if (ST.hasGFX10A16()) {
4511         const MachineOperand *A16 = getNamedOperand(MI, AMDGPU::OpName::a16);
4512         IsA16 = A16->getImm() != 0;
4513       }
4514 
4515       bool IsNSA = SRsrcIdx - VAddr0Idx > 1;
4516 
4517       unsigned AddrWords =
4518           AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, ST.hasG16());
4519 
4520       unsigned VAddrWords;
4521       if (IsNSA) {
4522         VAddrWords = SRsrcIdx - VAddr0Idx;
4523       } else {
4524         const TargetRegisterClass *RC = getOpRegClass(MI, VAddr0Idx);
4525         VAddrWords = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 32;
4526         if (AddrWords > 8)
4527           AddrWords = 16;
4528       }
4529 
4530       if (VAddrWords != AddrWords) {
4531         LLVM_DEBUG(dbgs() << "bad vaddr size, expected " << AddrWords
4532                           << " but got " << VAddrWords << "\n");
4533         ErrInfo = "bad vaddr size";
4534         return false;
4535       }
4536     }
4537   }
4538 
4539   const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl);
4540   if (DppCt) {
4541     using namespace AMDGPU::DPP;
4542 
4543     unsigned DC = DppCt->getImm();
4544     if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 ||
4545         DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST ||
4546         (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) ||
4547         (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) ||
4548         (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) ||
4549         (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) ||
4550         (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) {
4551       ErrInfo = "Invalid dpp_ctrl value";
4552       return false;
4553     }
4554     if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 &&
4555         ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
4556       ErrInfo = "Invalid dpp_ctrl value: "
4557                 "wavefront shifts are not supported on GFX10+";
4558       return false;
4559     }
4560     if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 &&
4561         ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
4562       ErrInfo = "Invalid dpp_ctrl value: "
4563                 "broadcasts are not supported on GFX10+";
4564       return false;
4565     }
4566     if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST &&
4567         ST.getGeneration() < AMDGPUSubtarget::GFX10) {
4568       if (DC >= DppCtrl::ROW_NEWBCAST_FIRST &&
4569           DC <= DppCtrl::ROW_NEWBCAST_LAST &&
4570           !ST.hasGFX90AInsts()) {
4571         ErrInfo = "Invalid dpp_ctrl value: "
4572                   "row_newbroadcast/row_share is not supported before "
4573                   "GFX90A/GFX10";
4574         return false;
4575       } else if (DC > DppCtrl::ROW_NEWBCAST_LAST || !ST.hasGFX90AInsts()) {
4576         ErrInfo = "Invalid dpp_ctrl value: "
4577                   "row_share and row_xmask are not supported before GFX10";
4578         return false;
4579       }
4580     }
4581 
4582     int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst);
4583 
4584     if (Opcode != AMDGPU::V_MOV_B64_DPP_PSEUDO &&
4585         ((DstIdx >= 0 &&
4586           (Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64RegClassID ||
4587            Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64_Align2RegClassID)) ||
4588          ((Src0Idx >= 0 &&
4589            (Desc.OpInfo[Src0Idx].RegClass == AMDGPU::VReg_64RegClassID ||
4590             Desc.OpInfo[Src0Idx].RegClass ==
4591                 AMDGPU::VReg_64_Align2RegClassID)))) &&
4592         !AMDGPU::isLegal64BitDPPControl(DC)) {
4593       ErrInfo = "Invalid dpp_ctrl value: "
4594                 "64 bit dpp only support row_newbcast";
4595       return false;
4596     }
4597   }
4598 
4599   if ((MI.mayStore() || MI.mayLoad()) && !isVGPRSpill(MI)) {
4600     const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst);
4601     uint16_t DataNameIdx = isDS(Opcode) ? AMDGPU::OpName::data0
4602                                         : AMDGPU::OpName::vdata;
4603     const MachineOperand *Data = getNamedOperand(MI, DataNameIdx);
4604     const MachineOperand *Data2 = getNamedOperand(MI, AMDGPU::OpName::data1);
4605     if (Data && !Data->isReg())
4606       Data = nullptr;
4607 
4608     if (ST.hasGFX90AInsts()) {
4609       if (Dst && Data &&
4610           (RI.isAGPR(MRI, Dst->getReg()) != RI.isAGPR(MRI, Data->getReg()))) {
4611         ErrInfo = "Invalid register class: "
4612                   "vdata and vdst should be both VGPR or AGPR";
4613         return false;
4614       }
4615       if (Data && Data2 &&
4616           (RI.isAGPR(MRI, Data->getReg()) != RI.isAGPR(MRI, Data2->getReg()))) {
4617         ErrInfo = "Invalid register class: "
4618                   "both data operands should be VGPR or AGPR";
4619         return false;
4620       }
4621     } else {
4622       if ((Dst && RI.isAGPR(MRI, Dst->getReg())) ||
4623           (Data && RI.isAGPR(MRI, Data->getReg())) ||
4624           (Data2 && RI.isAGPR(MRI, Data2->getReg()))) {
4625         ErrInfo = "Invalid register class: "
4626                   "agpr loads and stores not supported on this GPU";
4627         return false;
4628       }
4629     }
4630   }
4631 
4632   if (ST.needsAlignedVGPRs() &&
4633       (MI.getOpcode() == AMDGPU::DS_GWS_INIT ||
4634        MI.getOpcode() == AMDGPU::DS_GWS_SEMA_BR ||
4635        MI.getOpcode() == AMDGPU::DS_GWS_BARRIER)) {
4636     const MachineOperand *Op = getNamedOperand(MI, AMDGPU::OpName::data0);
4637     Register Reg = Op->getReg();
4638     bool Aligned = true;
4639     if (Reg.isPhysical()) {
4640       Aligned = !(RI.getHWRegIndex(Reg) & 1);
4641     } else {
4642       const TargetRegisterClass &RC = *MRI.getRegClass(Reg);
4643       Aligned = RI.getRegSizeInBits(RC) > 32 && RI.isProperlyAlignedRC(RC) &&
4644                 !(RI.getChannelFromSubReg(Op->getSubReg()) & 1);
4645     }
4646 
4647     if (!Aligned) {
4648       ErrInfo = "Subtarget requires even aligned vector registers "
4649                 "for DS_GWS instructions";
4650       return false;
4651     }
4652   }
4653 
4654   if (MI.getOpcode() == AMDGPU::V_ACCVGPR_WRITE_B32_e64 &&
4655       !ST.hasGFX90AInsts()) {
4656     const MachineOperand *Src = getNamedOperand(MI, AMDGPU::OpName::src0);
4657     if (Src->isReg() && RI.isSGPRReg(MRI, Src->getReg())) {
4658       ErrInfo = "Invalid register class: "
4659                 "v_accvgpr_write with an SGPR is not supported on this GPU";
4660       return false;
4661     }
4662   }
4663 
4664   if (Desc.getOpcode() == AMDGPU::G_AMDGPU_WAVE_ADDRESS) {
4665     const MachineOperand &SrcOp = MI.getOperand(1);
4666     if (!SrcOp.isReg() || SrcOp.getReg().isVirtual()) {
4667       ErrInfo = "pseudo expects only physical SGPRs";
4668       return false;
4669     }
4670   }
4671 
4672   return true;
4673 }
4674 
4675 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const {
4676   switch (MI.getOpcode()) {
4677   default: return AMDGPU::INSTRUCTION_LIST_END;
4678   case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE;
4679   case AMDGPU::COPY: return AMDGPU::COPY;
4680   case AMDGPU::PHI: return AMDGPU::PHI;
4681   case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG;
4682   case AMDGPU::WQM: return AMDGPU::WQM;
4683   case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM;
4684   case AMDGPU::STRICT_WWM: return AMDGPU::STRICT_WWM;
4685   case AMDGPU::STRICT_WQM: return AMDGPU::STRICT_WQM;
4686   case AMDGPU::S_MOV_B32: {
4687     const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
4688     return MI.getOperand(1).isReg() ||
4689            RI.isAGPR(MRI, MI.getOperand(0).getReg()) ?
4690            AMDGPU::COPY : AMDGPU::V_MOV_B32_e32;
4691   }
4692   case AMDGPU::S_ADD_I32:
4693     return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32;
4694   case AMDGPU::S_ADDC_U32:
4695     return AMDGPU::V_ADDC_U32_e32;
4696   case AMDGPU::S_SUB_I32:
4697     return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_CO_U32_e32;
4698     // FIXME: These are not consistently handled, and selected when the carry is
4699     // used.
4700   case AMDGPU::S_ADD_U32:
4701     return AMDGPU::V_ADD_CO_U32_e32;
4702   case AMDGPU::S_SUB_U32:
4703     return AMDGPU::V_SUB_CO_U32_e32;
4704   case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32;
4705   case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32_e64;
4706   case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32_e64;
4707   case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32_e64;
4708   case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64;
4709   case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64;
4710   case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64;
4711   case AMDGPU::S_XNOR_B32:
4712     return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END;
4713   case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64;
4714   case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64;
4715   case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64;
4716   case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64;
4717   case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32;
4718   case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64_e64;
4719   case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32;
4720   case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64_e64;
4721   case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32;
4722   case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64_e64;
4723   case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32_e64;
4724   case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32_e64;
4725   case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32_e64;
4726   case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32_e64;
4727   case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64;
4728   case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32;
4729   case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32;
4730   case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32;
4731   case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e64;
4732   case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e64;
4733   case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e64;
4734   case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e64;
4735   case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e64;
4736   case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e64;
4737   case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e64;
4738   case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e64;
4739   case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e64;
4740   case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e64;
4741   case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e64;
4742   case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e64;
4743   case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e64;
4744   case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e64;
4745   case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64;
4746   case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32;
4747   case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32;
4748   case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64;
4749   case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ;
4750   case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ;
4751   }
4752   llvm_unreachable(
4753       "Unexpected scalar opcode without corresponding vector one!");
4754 }
4755 
4756 static const TargetRegisterClass *
4757 adjustAllocatableRegClass(const GCNSubtarget &ST, const SIRegisterInfo &RI,
4758                           const MachineRegisterInfo &MRI,
4759                           const MCInstrDesc &TID, unsigned RCID,
4760                           bool IsAllocatable) {
4761   if ((IsAllocatable || !ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) &&
4762       (((TID.mayLoad() || TID.mayStore()) &&
4763         !(TID.TSFlags & SIInstrFlags::VGPRSpill)) ||
4764        (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::MIMG)))) {
4765     switch (RCID) {
4766     case AMDGPU::AV_32RegClassID:
4767       RCID = AMDGPU::VGPR_32RegClassID;
4768       break;
4769     case AMDGPU::AV_64RegClassID:
4770       RCID = AMDGPU::VReg_64RegClassID;
4771       break;
4772     case AMDGPU::AV_96RegClassID:
4773       RCID = AMDGPU::VReg_96RegClassID;
4774       break;
4775     case AMDGPU::AV_128RegClassID:
4776       RCID = AMDGPU::VReg_128RegClassID;
4777       break;
4778     case AMDGPU::AV_160RegClassID:
4779       RCID = AMDGPU::VReg_160RegClassID;
4780       break;
4781     case AMDGPU::AV_512RegClassID:
4782       RCID = AMDGPU::VReg_512RegClassID;
4783       break;
4784     default:
4785       break;
4786     }
4787   }
4788 
4789   return RI.getProperlyAlignedRC(RI.getRegClass(RCID));
4790 }
4791 
4792 const TargetRegisterClass *SIInstrInfo::getRegClass(const MCInstrDesc &TID,
4793     unsigned OpNum, const TargetRegisterInfo *TRI,
4794     const MachineFunction &MF)
4795   const {
4796   if (OpNum >= TID.getNumOperands())
4797     return nullptr;
4798   auto RegClass = TID.OpInfo[OpNum].RegClass;
4799   bool IsAllocatable = false;
4800   if (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::FLAT)) {
4801     // vdst and vdata should be both VGPR or AGPR, same for the DS instructions
4802     // with two data operands. Request register class constrained to VGPR only
4803     // of both operands present as Machine Copy Propagation can not check this
4804     // constraint and possibly other passes too.
4805     //
4806     // The check is limited to FLAT and DS because atomics in non-flat encoding
4807     // have their vdst and vdata tied to be the same register.
4808     const int VDstIdx = AMDGPU::getNamedOperandIdx(TID.Opcode,
4809                                                    AMDGPU::OpName::vdst);
4810     const int DataIdx = AMDGPU::getNamedOperandIdx(TID.Opcode,
4811         (TID.TSFlags & SIInstrFlags::DS) ? AMDGPU::OpName::data0
4812                                          : AMDGPU::OpName::vdata);
4813     if (DataIdx != -1) {
4814       IsAllocatable = VDstIdx != -1 ||
4815                       AMDGPU::getNamedOperandIdx(TID.Opcode,
4816                                                  AMDGPU::OpName::data1) != -1;
4817     }
4818   }
4819   return adjustAllocatableRegClass(ST, RI, MF.getRegInfo(), TID, RegClass,
4820                                    IsAllocatable);
4821 }
4822 
4823 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI,
4824                                                       unsigned OpNo) const {
4825   const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
4826   const MCInstrDesc &Desc = get(MI.getOpcode());
4827   if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
4828       Desc.OpInfo[OpNo].RegClass == -1) {
4829     Register Reg = MI.getOperand(OpNo).getReg();
4830 
4831     if (Reg.isVirtual())
4832       return MRI.getRegClass(Reg);
4833     return RI.getPhysRegClass(Reg);
4834   }
4835 
4836   unsigned RCID = Desc.OpInfo[OpNo].RegClass;
4837   return adjustAllocatableRegClass(ST, RI, MRI, Desc, RCID, true);
4838 }
4839 
4840 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const {
4841   MachineBasicBlock::iterator I = MI;
4842   MachineBasicBlock *MBB = MI.getParent();
4843   MachineOperand &MO = MI.getOperand(OpIdx);
4844   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
4845   unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass;
4846   const TargetRegisterClass *RC = RI.getRegClass(RCID);
4847   unsigned Size = RI.getRegSizeInBits(*RC);
4848   unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO : AMDGPU::V_MOV_B32_e32;
4849   if (MO.isReg())
4850     Opcode = AMDGPU::COPY;
4851   else if (RI.isSGPRClass(RC))
4852     Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32;
4853 
4854   const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
4855   const TargetRegisterClass *VRC64 = RI.getVGPR64Class();
4856   if (RI.getCommonSubClass(VRC64, VRC))
4857     VRC = VRC64;
4858   else
4859     VRC = &AMDGPU::VGPR_32RegClass;
4860 
4861   Register Reg = MRI.createVirtualRegister(VRC);
4862   DebugLoc DL = MBB->findDebugLoc(I);
4863   BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO);
4864   MO.ChangeToRegister(Reg, false);
4865 }
4866 
4867 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI,
4868                                          MachineRegisterInfo &MRI,
4869                                          MachineOperand &SuperReg,
4870                                          const TargetRegisterClass *SuperRC,
4871                                          unsigned SubIdx,
4872                                          const TargetRegisterClass *SubRC)
4873                                          const {
4874   MachineBasicBlock *MBB = MI->getParent();
4875   DebugLoc DL = MI->getDebugLoc();
4876   Register SubReg = MRI.createVirtualRegister(SubRC);
4877 
4878   if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) {
4879     BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
4880       .addReg(SuperReg.getReg(), 0, SubIdx);
4881     return SubReg;
4882   }
4883 
4884   // Just in case the super register is itself a sub-register, copy it to a new
4885   // value so we don't need to worry about merging its subreg index with the
4886   // SubIdx passed to this function. The register coalescer should be able to
4887   // eliminate this extra copy.
4888   Register NewSuperReg = MRI.createVirtualRegister(SuperRC);
4889 
4890   BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg)
4891     .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg());
4892 
4893   BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
4894     .addReg(NewSuperReg, 0, SubIdx);
4895 
4896   return SubReg;
4897 }
4898 
4899 MachineOperand SIInstrInfo::buildExtractSubRegOrImm(
4900   MachineBasicBlock::iterator MII,
4901   MachineRegisterInfo &MRI,
4902   MachineOperand &Op,
4903   const TargetRegisterClass *SuperRC,
4904   unsigned SubIdx,
4905   const TargetRegisterClass *SubRC) const {
4906   if (Op.isImm()) {
4907     if (SubIdx == AMDGPU::sub0)
4908       return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm()));
4909     if (SubIdx == AMDGPU::sub1)
4910       return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32));
4911 
4912     llvm_unreachable("Unhandled register index for immediate");
4913   }
4914 
4915   unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
4916                                        SubIdx, SubRC);
4917   return MachineOperand::CreateReg(SubReg, false);
4918 }
4919 
4920 // Change the order of operands from (0, 1, 2) to (0, 2, 1)
4921 void SIInstrInfo::swapOperands(MachineInstr &Inst) const {
4922   assert(Inst.getNumExplicitOperands() == 3);
4923   MachineOperand Op1 = Inst.getOperand(1);
4924   Inst.removeOperand(1);
4925   Inst.addOperand(Op1);
4926 }
4927 
4928 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI,
4929                                     const MCOperandInfo &OpInfo,
4930                                     const MachineOperand &MO) const {
4931   if (!MO.isReg())
4932     return false;
4933 
4934   Register Reg = MO.getReg();
4935 
4936   const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass);
4937   if (Reg.isPhysical())
4938     return DRC->contains(Reg);
4939 
4940   const TargetRegisterClass *RC = MRI.getRegClass(Reg);
4941 
4942   if (MO.getSubReg()) {
4943     const MachineFunction *MF = MO.getParent()->getParent()->getParent();
4944     const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF);
4945     if (!SuperRC)
4946       return false;
4947 
4948     DRC = RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg());
4949     if (!DRC)
4950       return false;
4951   }
4952   return RC->hasSuperClassEq(DRC);
4953 }
4954 
4955 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI,
4956                                      const MCOperandInfo &OpInfo,
4957                                      const MachineOperand &MO) const {
4958   if (MO.isReg())
4959     return isLegalRegOperand(MRI, OpInfo, MO);
4960 
4961   // Handle non-register types that are treated like immediates.
4962   assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal());
4963   return true;
4964 }
4965 
4966 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx,
4967                                  const MachineOperand *MO) const {
4968   const MachineFunction &MF = *MI.getParent()->getParent();
4969   const MachineRegisterInfo &MRI = MF.getRegInfo();
4970   const MCInstrDesc &InstDesc = MI.getDesc();
4971   const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx];
4972   const TargetRegisterClass *DefinedRC =
4973       OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr;
4974   if (!MO)
4975     MO = &MI.getOperand(OpIdx);
4976 
4977   int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode());
4978   int VOP3LiteralLimit = ST.hasVOP3Literal() ? 1 : 0;
4979   if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) {
4980     if (isVOP3(MI) && isLiteralConstantLike(*MO, OpInfo) && !VOP3LiteralLimit--)
4981       return false;
4982 
4983     SmallDenseSet<RegSubRegPair> SGPRsUsed;
4984     if (MO->isReg())
4985       SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg()));
4986 
4987     for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
4988       if (i == OpIdx)
4989         continue;
4990       const MachineOperand &Op = MI.getOperand(i);
4991       if (Op.isReg()) {
4992         RegSubRegPair SGPR(Op.getReg(), Op.getSubReg());
4993         if (!SGPRsUsed.count(SGPR) &&
4994             usesConstantBus(MRI, Op, InstDesc.OpInfo[i])) {
4995           if (--ConstantBusLimit <= 0)
4996             return false;
4997           SGPRsUsed.insert(SGPR);
4998         }
4999       } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) {
5000         if (--ConstantBusLimit <= 0)
5001           return false;
5002       } else if (isVOP3(MI) && AMDGPU::isSISrcOperand(InstDesc, i) &&
5003                  isLiteralConstantLike(Op, InstDesc.OpInfo[i])) {
5004         if (!VOP3LiteralLimit--)
5005           return false;
5006         if (--ConstantBusLimit <= 0)
5007           return false;
5008       }
5009     }
5010   }
5011 
5012   if (MO->isReg()) {
5013     assert(DefinedRC);
5014     if (!isLegalRegOperand(MRI, OpInfo, *MO))
5015       return false;
5016     bool IsAGPR = RI.isAGPR(MRI, MO->getReg());
5017     if (IsAGPR && !ST.hasMAIInsts())
5018       return false;
5019     unsigned Opc = MI.getOpcode();
5020     if (IsAGPR &&
5021         (!ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) &&
5022         (MI.mayLoad() || MI.mayStore() || isDS(Opc) || isMIMG(Opc)))
5023       return false;
5024     // Atomics should have both vdst and vdata either vgpr or agpr.
5025     const int VDstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
5026     const int DataIdx = AMDGPU::getNamedOperandIdx(Opc,
5027         isDS(Opc) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata);
5028     if ((int)OpIdx == VDstIdx && DataIdx != -1 &&
5029         MI.getOperand(DataIdx).isReg() &&
5030         RI.isAGPR(MRI, MI.getOperand(DataIdx).getReg()) != IsAGPR)
5031       return false;
5032     if ((int)OpIdx == DataIdx) {
5033       if (VDstIdx != -1 &&
5034           RI.isAGPR(MRI, MI.getOperand(VDstIdx).getReg()) != IsAGPR)
5035         return false;
5036       // DS instructions with 2 src operands also must have tied RC.
5037       const int Data1Idx = AMDGPU::getNamedOperandIdx(Opc,
5038                                                       AMDGPU::OpName::data1);
5039       if (Data1Idx != -1 && MI.getOperand(Data1Idx).isReg() &&
5040           RI.isAGPR(MRI, MI.getOperand(Data1Idx).getReg()) != IsAGPR)
5041         return false;
5042     }
5043     if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && !ST.hasGFX90AInsts() &&
5044         (int)OpIdx == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) &&
5045         RI.isSGPRReg(MRI, MO->getReg()))
5046       return false;
5047     return true;
5048   }
5049 
5050   // Handle non-register types that are treated like immediates.
5051   assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal());
5052 
5053   if (!DefinedRC) {
5054     // This operand expects an immediate.
5055     return true;
5056   }
5057 
5058   return isImmOperandLegal(MI, OpIdx, *MO);
5059 }
5060 
5061 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI,
5062                                        MachineInstr &MI) const {
5063   unsigned Opc = MI.getOpcode();
5064   const MCInstrDesc &InstrDesc = get(Opc);
5065 
5066   int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
5067   MachineOperand &Src0 = MI.getOperand(Src0Idx);
5068 
5069   int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
5070   MachineOperand &Src1 = MI.getOperand(Src1Idx);
5071 
5072   // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32
5073   // we need to only have one constant bus use before GFX10.
5074   bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister;
5075   if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 &&
5076       Src0.isReg() && (RI.isSGPRReg(MRI, Src0.getReg()) ||
5077        isLiteralConstantLike(Src0, InstrDesc.OpInfo[Src0Idx])))
5078     legalizeOpWithMove(MI, Src0Idx);
5079 
5080   // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for
5081   // both the value to write (src0) and lane select (src1).  Fix up non-SGPR
5082   // src0/src1 with V_READFIRSTLANE.
5083   if (Opc == AMDGPU::V_WRITELANE_B32) {
5084     const DebugLoc &DL = MI.getDebugLoc();
5085     if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) {
5086       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5087       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5088           .add(Src0);
5089       Src0.ChangeToRegister(Reg, false);
5090     }
5091     if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) {
5092       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5093       const DebugLoc &DL = MI.getDebugLoc();
5094       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5095           .add(Src1);
5096       Src1.ChangeToRegister(Reg, false);
5097     }
5098     return;
5099   }
5100 
5101   // No VOP2 instructions support AGPRs.
5102   if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg()))
5103     legalizeOpWithMove(MI, Src0Idx);
5104 
5105   if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg()))
5106     legalizeOpWithMove(MI, Src1Idx);
5107 
5108   // VOP2 src0 instructions support all operand types, so we don't need to check
5109   // their legality. If src1 is already legal, we don't need to do anything.
5110   if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1))
5111     return;
5112 
5113   // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for
5114   // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane
5115   // select is uniform.
5116   if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() &&
5117       RI.isVGPR(MRI, Src1.getReg())) {
5118     Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5119     const DebugLoc &DL = MI.getDebugLoc();
5120     BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5121         .add(Src1);
5122     Src1.ChangeToRegister(Reg, false);
5123     return;
5124   }
5125 
5126   // We do not use commuteInstruction here because it is too aggressive and will
5127   // commute if it is possible. We only want to commute here if it improves
5128   // legality. This can be called a fairly large number of times so don't waste
5129   // compile time pointlessly swapping and checking legality again.
5130   if (HasImplicitSGPR || !MI.isCommutable()) {
5131     legalizeOpWithMove(MI, Src1Idx);
5132     return;
5133   }
5134 
5135   // If src0 can be used as src1, commuting will make the operands legal.
5136   // Otherwise we have to give up and insert a move.
5137   //
5138   // TODO: Other immediate-like operand kinds could be commuted if there was a
5139   // MachineOperand::ChangeTo* for them.
5140   if ((!Src1.isImm() && !Src1.isReg()) ||
5141       !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) {
5142     legalizeOpWithMove(MI, Src1Idx);
5143     return;
5144   }
5145 
5146   int CommutedOpc = commuteOpcode(MI);
5147   if (CommutedOpc == -1) {
5148     legalizeOpWithMove(MI, Src1Idx);
5149     return;
5150   }
5151 
5152   MI.setDesc(get(CommutedOpc));
5153 
5154   Register Src0Reg = Src0.getReg();
5155   unsigned Src0SubReg = Src0.getSubReg();
5156   bool Src0Kill = Src0.isKill();
5157 
5158   if (Src1.isImm())
5159     Src0.ChangeToImmediate(Src1.getImm());
5160   else if (Src1.isReg()) {
5161     Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill());
5162     Src0.setSubReg(Src1.getSubReg());
5163   } else
5164     llvm_unreachable("Should only have register or immediate operands");
5165 
5166   Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill);
5167   Src1.setSubReg(Src0SubReg);
5168   fixImplicitOperands(MI);
5169 }
5170 
5171 // Legalize VOP3 operands. All operand types are supported for any operand
5172 // but only one literal constant and only starting from GFX10.
5173 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI,
5174                                        MachineInstr &MI) const {
5175   unsigned Opc = MI.getOpcode();
5176 
5177   int VOP3Idx[3] = {
5178     AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0),
5179     AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1),
5180     AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)
5181   };
5182 
5183   if (Opc == AMDGPU::V_PERMLANE16_B32_e64 ||
5184       Opc == AMDGPU::V_PERMLANEX16_B32_e64) {
5185     // src1 and src2 must be scalar
5186     MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]);
5187     MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]);
5188     const DebugLoc &DL = MI.getDebugLoc();
5189     if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) {
5190       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5191       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5192         .add(Src1);
5193       Src1.ChangeToRegister(Reg, false);
5194     }
5195     if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) {
5196       Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5197       BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg)
5198         .add(Src2);
5199       Src2.ChangeToRegister(Reg, false);
5200     }
5201   }
5202 
5203   // Find the one SGPR operand we are allowed to use.
5204   int ConstantBusLimit = ST.getConstantBusLimit(Opc);
5205   int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0;
5206   SmallDenseSet<unsigned> SGPRsUsed;
5207   Register SGPRReg = findUsedSGPR(MI, VOP3Idx);
5208   if (SGPRReg != AMDGPU::NoRegister) {
5209     SGPRsUsed.insert(SGPRReg);
5210     --ConstantBusLimit;
5211   }
5212 
5213   for (int Idx : VOP3Idx) {
5214     if (Idx == -1)
5215       break;
5216     MachineOperand &MO = MI.getOperand(Idx);
5217 
5218     if (!MO.isReg()) {
5219       if (!isLiteralConstantLike(MO, get(Opc).OpInfo[Idx]))
5220         continue;
5221 
5222       if (LiteralLimit > 0 && ConstantBusLimit > 0) {
5223         --LiteralLimit;
5224         --ConstantBusLimit;
5225         continue;
5226       }
5227 
5228       --LiteralLimit;
5229       --ConstantBusLimit;
5230       legalizeOpWithMove(MI, Idx);
5231       continue;
5232     }
5233 
5234     if (RI.hasAGPRs(RI.getRegClassForReg(MRI, MO.getReg())) &&
5235         !isOperandLegal(MI, Idx, &MO)) {
5236       legalizeOpWithMove(MI, Idx);
5237       continue;
5238     }
5239 
5240     if (!RI.isSGPRClass(RI.getRegClassForReg(MRI, MO.getReg())))
5241       continue; // VGPRs are legal
5242 
5243     // We can use one SGPR in each VOP3 instruction prior to GFX10
5244     // and two starting from GFX10.
5245     if (SGPRsUsed.count(MO.getReg()))
5246       continue;
5247     if (ConstantBusLimit > 0) {
5248       SGPRsUsed.insert(MO.getReg());
5249       --ConstantBusLimit;
5250       continue;
5251     }
5252 
5253     // If we make it this far, then the operand is not legal and we must
5254     // legalize it.
5255     legalizeOpWithMove(MI, Idx);
5256   }
5257 }
5258 
5259 Register SIInstrInfo::readlaneVGPRToSGPR(Register SrcReg, MachineInstr &UseMI,
5260                                          MachineRegisterInfo &MRI) const {
5261   const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg);
5262   const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC);
5263   Register DstReg = MRI.createVirtualRegister(SRC);
5264   unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32;
5265 
5266   if (RI.hasAGPRs(VRC)) {
5267     VRC = RI.getEquivalentVGPRClass(VRC);
5268     Register NewSrcReg = MRI.createVirtualRegister(VRC);
5269     BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5270             get(TargetOpcode::COPY), NewSrcReg)
5271         .addReg(SrcReg);
5272     SrcReg = NewSrcReg;
5273   }
5274 
5275   if (SubRegs == 1) {
5276     BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5277             get(AMDGPU::V_READFIRSTLANE_B32), DstReg)
5278         .addReg(SrcReg);
5279     return DstReg;
5280   }
5281 
5282   SmallVector<unsigned, 8> SRegs;
5283   for (unsigned i = 0; i < SubRegs; ++i) {
5284     Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5285     BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5286             get(AMDGPU::V_READFIRSTLANE_B32), SGPR)
5287         .addReg(SrcReg, 0, RI.getSubRegFromChannel(i));
5288     SRegs.push_back(SGPR);
5289   }
5290 
5291   MachineInstrBuilder MIB =
5292       BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
5293               get(AMDGPU::REG_SEQUENCE), DstReg);
5294   for (unsigned i = 0; i < SubRegs; ++i) {
5295     MIB.addReg(SRegs[i]);
5296     MIB.addImm(RI.getSubRegFromChannel(i));
5297   }
5298   return DstReg;
5299 }
5300 
5301 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI,
5302                                        MachineInstr &MI) const {
5303 
5304   // If the pointer is store in VGPRs, then we need to move them to
5305   // SGPRs using v_readfirstlane.  This is safe because we only select
5306   // loads with uniform pointers to SMRD instruction so we know the
5307   // pointer value is uniform.
5308   MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase);
5309   if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) {
5310     Register SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI);
5311     SBase->setReg(SGPR);
5312   }
5313   MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soff);
5314   if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) {
5315     Register SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI);
5316     SOff->setReg(SGPR);
5317   }
5318 }
5319 
5320 bool SIInstrInfo::moveFlatAddrToVGPR(MachineInstr &Inst) const {
5321   unsigned Opc = Inst.getOpcode();
5322   int OldSAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::saddr);
5323   if (OldSAddrIdx < 0)
5324     return false;
5325 
5326   assert(isSegmentSpecificFLAT(Inst));
5327 
5328   int NewOpc = AMDGPU::getGlobalVaddrOp(Opc);
5329   if (NewOpc < 0)
5330     NewOpc = AMDGPU::getFlatScratchInstSVfromSS(Opc);
5331   if (NewOpc < 0)
5332     return false;
5333 
5334   MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo();
5335   MachineOperand &SAddr = Inst.getOperand(OldSAddrIdx);
5336   if (RI.isSGPRReg(MRI, SAddr.getReg()))
5337     return false;
5338 
5339   int NewVAddrIdx = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vaddr);
5340   if (NewVAddrIdx < 0)
5341     return false;
5342 
5343   int OldVAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr);
5344 
5345   // Check vaddr, it shall be zero or absent.
5346   MachineInstr *VAddrDef = nullptr;
5347   if (OldVAddrIdx >= 0) {
5348     MachineOperand &VAddr = Inst.getOperand(OldVAddrIdx);
5349     VAddrDef = MRI.getUniqueVRegDef(VAddr.getReg());
5350     if (!VAddrDef || VAddrDef->getOpcode() != AMDGPU::V_MOV_B32_e32 ||
5351         !VAddrDef->getOperand(1).isImm() ||
5352         VAddrDef->getOperand(1).getImm() != 0)
5353       return false;
5354   }
5355 
5356   const MCInstrDesc &NewDesc = get(NewOpc);
5357   Inst.setDesc(NewDesc);
5358 
5359   // Callers expect iterator to be valid after this call, so modify the
5360   // instruction in place.
5361   if (OldVAddrIdx == NewVAddrIdx) {
5362     MachineOperand &NewVAddr = Inst.getOperand(NewVAddrIdx);
5363     // Clear use list from the old vaddr holding a zero register.
5364     MRI.removeRegOperandFromUseList(&NewVAddr);
5365     MRI.moveOperands(&NewVAddr, &SAddr, 1);
5366     Inst.removeOperand(OldSAddrIdx);
5367     // Update the use list with the pointer we have just moved from vaddr to
5368     // saddr position. Otherwise new vaddr will be missing from the use list.
5369     MRI.removeRegOperandFromUseList(&NewVAddr);
5370     MRI.addRegOperandToUseList(&NewVAddr);
5371   } else {
5372     assert(OldSAddrIdx == NewVAddrIdx);
5373 
5374     if (OldVAddrIdx >= 0) {
5375       int NewVDstIn = AMDGPU::getNamedOperandIdx(NewOpc,
5376                                                  AMDGPU::OpName::vdst_in);
5377 
5378       // removeOperand doesn't try to fixup tied operand indexes at it goes, so
5379       // it asserts. Untie the operands for now and retie them afterwards.
5380       if (NewVDstIn != -1) {
5381         int OldVDstIn = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst_in);
5382         Inst.untieRegOperand(OldVDstIn);
5383       }
5384 
5385       Inst.removeOperand(OldVAddrIdx);
5386 
5387       if (NewVDstIn != -1) {
5388         int NewVDst = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vdst);
5389         Inst.tieOperands(NewVDst, NewVDstIn);
5390       }
5391     }
5392   }
5393 
5394   if (VAddrDef && MRI.use_nodbg_empty(VAddrDef->getOperand(0).getReg()))
5395     VAddrDef->eraseFromParent();
5396 
5397   return true;
5398 }
5399 
5400 // FIXME: Remove this when SelectionDAG is obsoleted.
5401 void SIInstrInfo::legalizeOperandsFLAT(MachineRegisterInfo &MRI,
5402                                        MachineInstr &MI) const {
5403   if (!isSegmentSpecificFLAT(MI))
5404     return;
5405 
5406   // Fixup SGPR operands in VGPRs. We only select these when the DAG divergence
5407   // thinks they are uniform, so a readfirstlane should be valid.
5408   MachineOperand *SAddr = getNamedOperand(MI, AMDGPU::OpName::saddr);
5409   if (!SAddr || RI.isSGPRClass(MRI.getRegClass(SAddr->getReg())))
5410     return;
5411 
5412   if (moveFlatAddrToVGPR(MI))
5413     return;
5414 
5415   Register ToSGPR = readlaneVGPRToSGPR(SAddr->getReg(), MI, MRI);
5416   SAddr->setReg(ToSGPR);
5417 }
5418 
5419 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB,
5420                                          MachineBasicBlock::iterator I,
5421                                          const TargetRegisterClass *DstRC,
5422                                          MachineOperand &Op,
5423                                          MachineRegisterInfo &MRI,
5424                                          const DebugLoc &DL) const {
5425   Register OpReg = Op.getReg();
5426   unsigned OpSubReg = Op.getSubReg();
5427 
5428   const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg(
5429       RI.getRegClassForReg(MRI, OpReg), OpSubReg);
5430 
5431   // Check if operand is already the correct register class.
5432   if (DstRC == OpRC)
5433     return;
5434 
5435   Register DstReg = MRI.createVirtualRegister(DstRC);
5436   auto Copy = BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op);
5437 
5438   Op.setReg(DstReg);
5439   Op.setSubReg(0);
5440 
5441   MachineInstr *Def = MRI.getVRegDef(OpReg);
5442   if (!Def)
5443     return;
5444 
5445   // Try to eliminate the copy if it is copying an immediate value.
5446   if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass)
5447     FoldImmediate(*Copy, *Def, OpReg, &MRI);
5448 
5449   bool ImpDef = Def->isImplicitDef();
5450   while (!ImpDef && Def && Def->isCopy()) {
5451     if (Def->getOperand(1).getReg().isPhysical())
5452       break;
5453     Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg());
5454     ImpDef = Def && Def->isImplicitDef();
5455   }
5456   if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) &&
5457       !ImpDef)
5458     Copy.addReg(AMDGPU::EXEC, RegState::Implicit);
5459 }
5460 
5461 // Emit the actual waterfall loop, executing the wrapped instruction for each
5462 // unique value of \p Rsrc across all lanes. In the best case we execute 1
5463 // iteration, in the worst case we execute 64 (once per lane).
5464 static void
5465 emitLoadSRsrcFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI,
5466                           MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB,
5467                           MachineBasicBlock &BodyBB, const DebugLoc &DL,
5468                           MachineOperand &Rsrc) {
5469   MachineFunction &MF = *OrigBB.getParent();
5470   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
5471   const SIRegisterInfo *TRI = ST.getRegisterInfo();
5472   unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5473   unsigned SaveExecOpc =
5474       ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64;
5475   unsigned XorTermOpc =
5476       ST.isWave32() ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term;
5477   unsigned AndOpc =
5478       ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
5479   const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
5480 
5481   MachineBasicBlock::iterator I = LoopBB.begin();
5482 
5483   SmallVector<Register, 8> ReadlanePieces;
5484   Register CondReg = AMDGPU::NoRegister;
5485 
5486   Register VRsrc = Rsrc.getReg();
5487   unsigned VRsrcUndef = getUndefRegState(Rsrc.isUndef());
5488 
5489   unsigned RegSize = TRI->getRegSizeInBits(Rsrc.getReg(), MRI);
5490   unsigned NumSubRegs =  RegSize / 32;
5491   assert(NumSubRegs % 2 == 0 && NumSubRegs <= 32 && "Unhandled register size");
5492 
5493   for (unsigned Idx = 0; Idx < NumSubRegs; Idx += 2) {
5494 
5495     Register CurRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5496     Register CurRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5497 
5498     // Read the next variant <- also loop target.
5499     BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegLo)
5500             .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx));
5501 
5502     // Read the next variant <- also loop target.
5503     BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegHi)
5504             .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx + 1));
5505 
5506     ReadlanePieces.push_back(CurRegLo);
5507     ReadlanePieces.push_back(CurRegHi);
5508 
5509     // Comparison is to be done as 64-bit.
5510     Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
5511     BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), CurReg)
5512             .addReg(CurRegLo)
5513             .addImm(AMDGPU::sub0)
5514             .addReg(CurRegHi)
5515             .addImm(AMDGPU::sub1);
5516 
5517     Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC);
5518     auto Cmp =
5519         BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), NewCondReg)
5520             .addReg(CurReg);
5521     if (NumSubRegs <= 2)
5522       Cmp.addReg(VRsrc);
5523     else
5524       Cmp.addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx, 2));
5525 
5526     // Combine the comparison results with AND.
5527     if (CondReg == AMDGPU::NoRegister) // First.
5528       CondReg = NewCondReg;
5529     else { // If not the first, we create an AND.
5530       Register AndReg = MRI.createVirtualRegister(BoolXExecRC);
5531       BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndReg)
5532               .addReg(CondReg)
5533               .addReg(NewCondReg);
5534       CondReg = AndReg;
5535     }
5536   } // End for loop.
5537 
5538   auto SRsrcRC = TRI->getEquivalentSGPRClass(MRI.getRegClass(VRsrc));
5539   Register SRsrc = MRI.createVirtualRegister(SRsrcRC);
5540 
5541   // Build scalar Rsrc.
5542   auto Merge = BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SRsrc);
5543   unsigned Channel = 0;
5544   for (Register Piece : ReadlanePieces) {
5545     Merge.addReg(Piece)
5546          .addImm(TRI->getSubRegFromChannel(Channel++));
5547   }
5548 
5549   // Update Rsrc operand to use the SGPR Rsrc.
5550   Rsrc.setReg(SRsrc);
5551   Rsrc.setIsKill(true);
5552 
5553   Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
5554   MRI.setSimpleHint(SaveExec, CondReg);
5555 
5556   // Update EXEC to matching lanes, saving original to SaveExec.
5557   BuildMI(LoopBB, I, DL, TII.get(SaveExecOpc), SaveExec)
5558       .addReg(CondReg, RegState::Kill);
5559 
5560   // The original instruction is here; we insert the terminators after it.
5561   I = BodyBB.end();
5562 
5563   // Update EXEC, switch all done bits to 0 and all todo bits to 1.
5564   BuildMI(BodyBB, I, DL, TII.get(XorTermOpc), Exec)
5565       .addReg(Exec)
5566       .addReg(SaveExec);
5567 
5568   BuildMI(BodyBB, I, DL, TII.get(AMDGPU::SI_WATERFALL_LOOP)).addMBB(&LoopBB);
5569 }
5570 
5571 // Build a waterfall loop around \p MI, replacing the VGPR \p Rsrc register
5572 // with SGPRs by iterating over all unique values across all lanes.
5573 // Returns the loop basic block that now contains \p MI.
5574 static MachineBasicBlock *
5575 loadSRsrcFromVGPR(const SIInstrInfo &TII, MachineInstr &MI,
5576                   MachineOperand &Rsrc, MachineDominatorTree *MDT,
5577                   MachineBasicBlock::iterator Begin = nullptr,
5578                   MachineBasicBlock::iterator End = nullptr) {
5579   MachineBasicBlock &MBB = *MI.getParent();
5580   MachineFunction &MF = *MBB.getParent();
5581   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
5582   const SIRegisterInfo *TRI = ST.getRegisterInfo();
5583   MachineRegisterInfo &MRI = MF.getRegInfo();
5584   if (!Begin.isValid())
5585     Begin = &MI;
5586   if (!End.isValid()) {
5587     End = &MI;
5588     ++End;
5589   }
5590   const DebugLoc &DL = MI.getDebugLoc();
5591   unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5592   unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
5593   const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
5594 
5595   Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
5596 
5597   // Save the EXEC mask
5598   BuildMI(MBB, Begin, DL, TII.get(MovExecOpc), SaveExec).addReg(Exec);
5599 
5600   // Killed uses in the instruction we are waterfalling around will be
5601   // incorrect due to the added control-flow.
5602   MachineBasicBlock::iterator AfterMI = MI;
5603   ++AfterMI;
5604   for (auto I = Begin; I != AfterMI; I++) {
5605     for (auto &MO : I->uses()) {
5606       if (MO.isReg() && MO.isUse()) {
5607         MRI.clearKillFlags(MO.getReg());
5608       }
5609     }
5610   }
5611 
5612   // To insert the loop we need to split the block. Move everything after this
5613   // point to a new block, and insert a new empty block between the two.
5614   MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock();
5615   MachineBasicBlock *BodyBB = MF.CreateMachineBasicBlock();
5616   MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock();
5617   MachineFunction::iterator MBBI(MBB);
5618   ++MBBI;
5619 
5620   MF.insert(MBBI, LoopBB);
5621   MF.insert(MBBI, BodyBB);
5622   MF.insert(MBBI, RemainderBB);
5623 
5624   LoopBB->addSuccessor(BodyBB);
5625   BodyBB->addSuccessor(LoopBB);
5626   BodyBB->addSuccessor(RemainderBB);
5627 
5628   // Move Begin to MI to the BodyBB, and the remainder of the block to
5629   // RemainderBB.
5630   RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
5631   RemainderBB->splice(RemainderBB->begin(), &MBB, End, MBB.end());
5632   BodyBB->splice(BodyBB->begin(), &MBB, Begin, MBB.end());
5633 
5634   MBB.addSuccessor(LoopBB);
5635 
5636   // Update dominators. We know that MBB immediately dominates LoopBB, that
5637   // LoopBB immediately dominates BodyBB, and BodyBB immediately dominates
5638   // RemainderBB. RemainderBB immediately dominates all of the successors
5639   // transferred to it from MBB that MBB used to properly dominate.
5640   if (MDT) {
5641     MDT->addNewBlock(LoopBB, &MBB);
5642     MDT->addNewBlock(BodyBB, LoopBB);
5643     MDT->addNewBlock(RemainderBB, BodyBB);
5644     for (auto &Succ : RemainderBB->successors()) {
5645       if (MDT->properlyDominates(&MBB, Succ)) {
5646         MDT->changeImmediateDominator(Succ, RemainderBB);
5647       }
5648     }
5649   }
5650 
5651   emitLoadSRsrcFromVGPRLoop(TII, MRI, MBB, *LoopBB, *BodyBB, DL, Rsrc);
5652 
5653   // Restore the EXEC mask
5654   MachineBasicBlock::iterator First = RemainderBB->begin();
5655   BuildMI(*RemainderBB, First, DL, TII.get(MovExecOpc), Exec).addReg(SaveExec);
5656   return BodyBB;
5657 }
5658 
5659 // Extract pointer from Rsrc and return a zero-value Rsrc replacement.
5660 static std::tuple<unsigned, unsigned>
5661 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) {
5662   MachineBasicBlock &MBB = *MI.getParent();
5663   MachineFunction &MF = *MBB.getParent();
5664   MachineRegisterInfo &MRI = MF.getRegInfo();
5665 
5666   // Extract the ptr from the resource descriptor.
5667   unsigned RsrcPtr =
5668       TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass,
5669                              AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass);
5670 
5671   // Create an empty resource descriptor
5672   Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
5673   Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5674   Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5675   Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass);
5676   uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat();
5677 
5678   // Zero64 = 0
5679   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64)
5680       .addImm(0);
5681 
5682   // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
5683   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo)
5684       .addImm(RsrcDataFormat & 0xFFFFFFFF);
5685 
5686   // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
5687   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi)
5688       .addImm(RsrcDataFormat >> 32);
5689 
5690   // NewSRsrc = {Zero64, SRsrcFormat}
5691   BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc)
5692       .addReg(Zero64)
5693       .addImm(AMDGPU::sub0_sub1)
5694       .addReg(SRsrcFormatLo)
5695       .addImm(AMDGPU::sub2)
5696       .addReg(SRsrcFormatHi)
5697       .addImm(AMDGPU::sub3);
5698 
5699   return std::make_tuple(RsrcPtr, NewSRsrc);
5700 }
5701 
5702 MachineBasicBlock *
5703 SIInstrInfo::legalizeOperands(MachineInstr &MI,
5704                               MachineDominatorTree *MDT) const {
5705   MachineFunction &MF = *MI.getParent()->getParent();
5706   MachineRegisterInfo &MRI = MF.getRegInfo();
5707   MachineBasicBlock *CreatedBB = nullptr;
5708 
5709   // Legalize VOP2
5710   if (isVOP2(MI) || isVOPC(MI)) {
5711     legalizeOperandsVOP2(MRI, MI);
5712     return CreatedBB;
5713   }
5714 
5715   // Legalize VOP3
5716   if (isVOP3(MI)) {
5717     legalizeOperandsVOP3(MRI, MI);
5718     return CreatedBB;
5719   }
5720 
5721   // Legalize SMRD
5722   if (isSMRD(MI)) {
5723     legalizeOperandsSMRD(MRI, MI);
5724     return CreatedBB;
5725   }
5726 
5727   // Legalize FLAT
5728   if (isFLAT(MI)) {
5729     legalizeOperandsFLAT(MRI, MI);
5730     return CreatedBB;
5731   }
5732 
5733   // Legalize REG_SEQUENCE and PHI
5734   // The register class of the operands much be the same type as the register
5735   // class of the output.
5736   if (MI.getOpcode() == AMDGPU::PHI) {
5737     const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr;
5738     for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
5739       if (!MI.getOperand(i).isReg() || !MI.getOperand(i).getReg().isVirtual())
5740         continue;
5741       const TargetRegisterClass *OpRC =
5742           MRI.getRegClass(MI.getOperand(i).getReg());
5743       if (RI.hasVectorRegisters(OpRC)) {
5744         VRC = OpRC;
5745       } else {
5746         SRC = OpRC;
5747       }
5748     }
5749 
5750     // If any of the operands are VGPR registers, then they all most be
5751     // otherwise we will create illegal VGPR->SGPR copies when legalizing
5752     // them.
5753     if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) {
5754       if (!VRC) {
5755         assert(SRC);
5756         if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) {
5757           VRC = &AMDGPU::VReg_1RegClass;
5758         } else
5759           VRC = RI.isAGPRClass(getOpRegClass(MI, 0))
5760                     ? RI.getEquivalentAGPRClass(SRC)
5761                     : RI.getEquivalentVGPRClass(SRC);
5762       } else {
5763         VRC = RI.isAGPRClass(getOpRegClass(MI, 0))
5764                   ? RI.getEquivalentAGPRClass(VRC)
5765                   : RI.getEquivalentVGPRClass(VRC);
5766       }
5767       RC = VRC;
5768     } else {
5769       RC = SRC;
5770     }
5771 
5772     // Update all the operands so they have the same type.
5773     for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
5774       MachineOperand &Op = MI.getOperand(I);
5775       if (!Op.isReg() || !Op.getReg().isVirtual())
5776         continue;
5777 
5778       // MI is a PHI instruction.
5779       MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB();
5780       MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator();
5781 
5782       // Avoid creating no-op copies with the same src and dst reg class.  These
5783       // confuse some of the machine passes.
5784       legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc());
5785     }
5786   }
5787 
5788   // REG_SEQUENCE doesn't really require operand legalization, but if one has a
5789   // VGPR dest type and SGPR sources, insert copies so all operands are
5790   // VGPRs. This seems to help operand folding / the register coalescer.
5791   if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) {
5792     MachineBasicBlock *MBB = MI.getParent();
5793     const TargetRegisterClass *DstRC = getOpRegClass(MI, 0);
5794     if (RI.hasVGPRs(DstRC)) {
5795       // Update all the operands so they are VGPR register classes. These may
5796       // not be the same register class because REG_SEQUENCE supports mixing
5797       // subregister index types e.g. sub0_sub1 + sub2 + sub3
5798       for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) {
5799         MachineOperand &Op = MI.getOperand(I);
5800         if (!Op.isReg() || !Op.getReg().isVirtual())
5801           continue;
5802 
5803         const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg());
5804         const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC);
5805         if (VRC == OpRC)
5806           continue;
5807 
5808         legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc());
5809         Op.setIsKill();
5810       }
5811     }
5812 
5813     return CreatedBB;
5814   }
5815 
5816   // Legalize INSERT_SUBREG
5817   // src0 must have the same register class as dst
5818   if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) {
5819     Register Dst = MI.getOperand(0).getReg();
5820     Register Src0 = MI.getOperand(1).getReg();
5821     const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
5822     const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0);
5823     if (DstRC != Src0RC) {
5824       MachineBasicBlock *MBB = MI.getParent();
5825       MachineOperand &Op = MI.getOperand(1);
5826       legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc());
5827     }
5828     return CreatedBB;
5829   }
5830 
5831   // Legalize SI_INIT_M0
5832   if (MI.getOpcode() == AMDGPU::SI_INIT_M0) {
5833     MachineOperand &Src = MI.getOperand(0);
5834     if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg())))
5835       Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI));
5836     return CreatedBB;
5837   }
5838 
5839   // Legalize MIMG and MUBUF/MTBUF for shaders.
5840   //
5841   // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via
5842   // scratch memory access. In both cases, the legalization never involves
5843   // conversion to the addr64 form.
5844   if (isMIMG(MI) || (AMDGPU::isGraphics(MF.getFunction().getCallingConv()) &&
5845                      (isMUBUF(MI) || isMTBUF(MI)))) {
5846     MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc);
5847     if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg())))
5848       CreatedBB = loadSRsrcFromVGPR(*this, MI, *SRsrc, MDT);
5849 
5850     MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp);
5851     if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg())))
5852       CreatedBB = loadSRsrcFromVGPR(*this, MI, *SSamp, MDT);
5853 
5854     return CreatedBB;
5855   }
5856 
5857   // Legalize SI_CALL
5858   if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) {
5859     MachineOperand *Dest = &MI.getOperand(0);
5860     if (!RI.isSGPRClass(MRI.getRegClass(Dest->getReg()))) {
5861       // Move everything between ADJCALLSTACKUP and ADJCALLSTACKDOWN and
5862       // following copies, we also need to move copies from and to physical
5863       // registers into the loop block.
5864       unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
5865       unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
5866 
5867       // Also move the copies to physical registers into the loop block
5868       MachineBasicBlock &MBB = *MI.getParent();
5869       MachineBasicBlock::iterator Start(&MI);
5870       while (Start->getOpcode() != FrameSetupOpcode)
5871         --Start;
5872       MachineBasicBlock::iterator End(&MI);
5873       while (End->getOpcode() != FrameDestroyOpcode)
5874         ++End;
5875       // Also include following copies of the return value
5876       ++End;
5877       while (End != MBB.end() && End->isCopy() && End->getOperand(1).isReg() &&
5878              MI.definesRegister(End->getOperand(1).getReg()))
5879         ++End;
5880       CreatedBB = loadSRsrcFromVGPR(*this, MI, *Dest, MDT, Start, End);
5881     }
5882   }
5883 
5884   // Legalize MUBUF* instructions.
5885   int RsrcIdx =
5886       AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc);
5887   if (RsrcIdx != -1) {
5888     // We have an MUBUF instruction
5889     MachineOperand *Rsrc = &MI.getOperand(RsrcIdx);
5890     unsigned RsrcRC = get(MI.getOpcode()).OpInfo[RsrcIdx].RegClass;
5891     if (RI.getCommonSubClass(MRI.getRegClass(Rsrc->getReg()),
5892                              RI.getRegClass(RsrcRC))) {
5893       // The operands are legal.
5894       // FIXME: We may need to legalize operands besides srsrc.
5895       return CreatedBB;
5896     }
5897 
5898     // Legalize a VGPR Rsrc.
5899     //
5900     // If the instruction is _ADDR64, we can avoid a waterfall by extracting
5901     // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using
5902     // a zero-value SRsrc.
5903     //
5904     // If the instruction is _OFFSET (both idxen and offen disabled), and we
5905     // support ADDR64 instructions, we can convert to ADDR64 and do the same as
5906     // above.
5907     //
5908     // Otherwise we are on non-ADDR64 hardware, and/or we have
5909     // idxen/offen/bothen and we fall back to a waterfall loop.
5910 
5911     MachineBasicBlock &MBB = *MI.getParent();
5912 
5913     MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
5914     if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) {
5915       // This is already an ADDR64 instruction so we need to add the pointer
5916       // extracted from the resource descriptor to the current value of VAddr.
5917       Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5918       Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5919       Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
5920 
5921       const auto *BoolXExecRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
5922       Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC);
5923       Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC);
5924 
5925       unsigned RsrcPtr, NewSRsrc;
5926       std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
5927 
5928       // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0
5929       const DebugLoc &DL = MI.getDebugLoc();
5930       BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_CO_U32_e64), NewVAddrLo)
5931         .addDef(CondReg0)
5932         .addReg(RsrcPtr, 0, AMDGPU::sub0)
5933         .addReg(VAddr->getReg(), 0, AMDGPU::sub0)
5934         .addImm(0);
5935 
5936       // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1
5937       BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi)
5938         .addDef(CondReg1, RegState::Dead)
5939         .addReg(RsrcPtr, 0, AMDGPU::sub1)
5940         .addReg(VAddr->getReg(), 0, AMDGPU::sub1)
5941         .addReg(CondReg0, RegState::Kill)
5942         .addImm(0);
5943 
5944       // NewVaddr = {NewVaddrHi, NewVaddrLo}
5945       BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr)
5946           .addReg(NewVAddrLo)
5947           .addImm(AMDGPU::sub0)
5948           .addReg(NewVAddrHi)
5949           .addImm(AMDGPU::sub1);
5950 
5951       VAddr->setReg(NewVAddr);
5952       Rsrc->setReg(NewSRsrc);
5953     } else if (!VAddr && ST.hasAddr64()) {
5954       // This instructions is the _OFFSET variant, so we need to convert it to
5955       // ADDR64.
5956       assert(ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS &&
5957              "FIXME: Need to emit flat atomics here");
5958 
5959       unsigned RsrcPtr, NewSRsrc;
5960       std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc);
5961 
5962       Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
5963       MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata);
5964       MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset);
5965       MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset);
5966       unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode());
5967 
5968       // Atomics with return have an additional tied operand and are
5969       // missing some of the special bits.
5970       MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in);
5971       MachineInstr *Addr64;
5972 
5973       if (!VDataIn) {
5974         // Regular buffer load / store.
5975         MachineInstrBuilder MIB =
5976             BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
5977                 .add(*VData)
5978                 .addReg(NewVAddr)
5979                 .addReg(NewSRsrc)
5980                 .add(*SOffset)
5981                 .add(*Offset);
5982 
5983         if (const MachineOperand *CPol =
5984                 getNamedOperand(MI, AMDGPU::OpName::cpol)) {
5985           MIB.addImm(CPol->getImm());
5986         }
5987 
5988         if (const MachineOperand *TFE =
5989                 getNamedOperand(MI, AMDGPU::OpName::tfe)) {
5990           MIB.addImm(TFE->getImm());
5991         }
5992 
5993         MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz));
5994 
5995         MIB.cloneMemRefs(MI);
5996         Addr64 = MIB;
5997       } else {
5998         // Atomics with return.
5999         Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode))
6000                      .add(*VData)
6001                      .add(*VDataIn)
6002                      .addReg(NewVAddr)
6003                      .addReg(NewSRsrc)
6004                      .add(*SOffset)
6005                      .add(*Offset)
6006                      .addImm(getNamedImmOperand(MI, AMDGPU::OpName::cpol))
6007                      .cloneMemRefs(MI);
6008       }
6009 
6010       MI.removeFromParent();
6011 
6012       // NewVaddr = {NewVaddrHi, NewVaddrLo}
6013       BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
6014               NewVAddr)
6015           .addReg(RsrcPtr, 0, AMDGPU::sub0)
6016           .addImm(AMDGPU::sub0)
6017           .addReg(RsrcPtr, 0, AMDGPU::sub1)
6018           .addImm(AMDGPU::sub1);
6019     } else {
6020       // This is another variant; legalize Rsrc with waterfall loop from VGPRs
6021       // to SGPRs.
6022       CreatedBB = loadSRsrcFromVGPR(*this, MI, *Rsrc, MDT);
6023       return CreatedBB;
6024     }
6025   }
6026   return CreatedBB;
6027 }
6028 
6029 MachineBasicBlock *SIInstrInfo::moveToVALU(MachineInstr &TopInst,
6030                                            MachineDominatorTree *MDT) const {
6031   SetVectorType Worklist;
6032   Worklist.insert(&TopInst);
6033   MachineBasicBlock *CreatedBB = nullptr;
6034   MachineBasicBlock *CreatedBBTmp = nullptr;
6035 
6036   while (!Worklist.empty()) {
6037     MachineInstr &Inst = *Worklist.pop_back_val();
6038     MachineBasicBlock *MBB = Inst.getParent();
6039     MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
6040 
6041     unsigned Opcode = Inst.getOpcode();
6042     unsigned NewOpcode = getVALUOp(Inst);
6043 
6044     // Handle some special cases
6045     switch (Opcode) {
6046     default:
6047       break;
6048     case AMDGPU::S_ADD_U64_PSEUDO:
6049     case AMDGPU::S_SUB_U64_PSEUDO:
6050       splitScalar64BitAddSub(Worklist, Inst, MDT);
6051       Inst.eraseFromParent();
6052       continue;
6053     case AMDGPU::S_ADD_I32:
6054     case AMDGPU::S_SUB_I32: {
6055       // FIXME: The u32 versions currently selected use the carry.
6056       bool Changed;
6057       std::tie(Changed, CreatedBBTmp) = moveScalarAddSub(Worklist, Inst, MDT);
6058       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6059         CreatedBB = CreatedBBTmp;
6060       if (Changed)
6061         continue;
6062 
6063       // Default handling
6064       break;
6065     }
6066     case AMDGPU::S_AND_B64:
6067       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT);
6068       Inst.eraseFromParent();
6069       continue;
6070 
6071     case AMDGPU::S_OR_B64:
6072       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT);
6073       Inst.eraseFromParent();
6074       continue;
6075 
6076     case AMDGPU::S_XOR_B64:
6077       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT);
6078       Inst.eraseFromParent();
6079       continue;
6080 
6081     case AMDGPU::S_NAND_B64:
6082       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT);
6083       Inst.eraseFromParent();
6084       continue;
6085 
6086     case AMDGPU::S_NOR_B64:
6087       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT);
6088       Inst.eraseFromParent();
6089       continue;
6090 
6091     case AMDGPU::S_XNOR_B64:
6092       if (ST.hasDLInsts())
6093         splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT);
6094       else
6095         splitScalar64BitXnor(Worklist, Inst, MDT);
6096       Inst.eraseFromParent();
6097       continue;
6098 
6099     case AMDGPU::S_ANDN2_B64:
6100       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT);
6101       Inst.eraseFromParent();
6102       continue;
6103 
6104     case AMDGPU::S_ORN2_B64:
6105       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT);
6106       Inst.eraseFromParent();
6107       continue;
6108 
6109     case AMDGPU::S_BREV_B64:
6110       splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_BREV_B32, true);
6111       Inst.eraseFromParent();
6112       continue;
6113 
6114     case AMDGPU::S_NOT_B64:
6115       splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32);
6116       Inst.eraseFromParent();
6117       continue;
6118 
6119     case AMDGPU::S_BCNT1_I32_B64:
6120       splitScalar64BitBCNT(Worklist, Inst);
6121       Inst.eraseFromParent();
6122       continue;
6123 
6124     case AMDGPU::S_BFE_I64:
6125       splitScalar64BitBFE(Worklist, Inst);
6126       Inst.eraseFromParent();
6127       continue;
6128 
6129     case AMDGPU::S_LSHL_B32:
6130       if (ST.hasOnlyRevVALUShifts()) {
6131         NewOpcode = AMDGPU::V_LSHLREV_B32_e64;
6132         swapOperands(Inst);
6133       }
6134       break;
6135     case AMDGPU::S_ASHR_I32:
6136       if (ST.hasOnlyRevVALUShifts()) {
6137         NewOpcode = AMDGPU::V_ASHRREV_I32_e64;
6138         swapOperands(Inst);
6139       }
6140       break;
6141     case AMDGPU::S_LSHR_B32:
6142       if (ST.hasOnlyRevVALUShifts()) {
6143         NewOpcode = AMDGPU::V_LSHRREV_B32_e64;
6144         swapOperands(Inst);
6145       }
6146       break;
6147     case AMDGPU::S_LSHL_B64:
6148       if (ST.hasOnlyRevVALUShifts()) {
6149         NewOpcode = AMDGPU::V_LSHLREV_B64_e64;
6150         swapOperands(Inst);
6151       }
6152       break;
6153     case AMDGPU::S_ASHR_I64:
6154       if (ST.hasOnlyRevVALUShifts()) {
6155         NewOpcode = AMDGPU::V_ASHRREV_I64_e64;
6156         swapOperands(Inst);
6157       }
6158       break;
6159     case AMDGPU::S_LSHR_B64:
6160       if (ST.hasOnlyRevVALUShifts()) {
6161         NewOpcode = AMDGPU::V_LSHRREV_B64_e64;
6162         swapOperands(Inst);
6163       }
6164       break;
6165 
6166     case AMDGPU::S_ABS_I32:
6167       lowerScalarAbs(Worklist, Inst);
6168       Inst.eraseFromParent();
6169       continue;
6170 
6171     case AMDGPU::S_CBRANCH_SCC0:
6172     case AMDGPU::S_CBRANCH_SCC1: {
6173         // Clear unused bits of vcc
6174         Register CondReg = Inst.getOperand(1).getReg();
6175         bool IsSCC = CondReg == AMDGPU::SCC;
6176         Register VCC = RI.getVCC();
6177         Register EXEC = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
6178         unsigned Opc = ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
6179         BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(Opc), VCC)
6180             .addReg(EXEC)
6181             .addReg(IsSCC ? VCC : CondReg);
6182         Inst.removeOperand(1);
6183       }
6184       break;
6185 
6186     case AMDGPU::S_BFE_U64:
6187     case AMDGPU::S_BFM_B64:
6188       llvm_unreachable("Moving this op to VALU not implemented");
6189 
6190     case AMDGPU::S_PACK_LL_B32_B16:
6191     case AMDGPU::S_PACK_LH_B32_B16:
6192     case AMDGPU::S_PACK_HH_B32_B16:
6193       movePackToVALU(Worklist, MRI, Inst);
6194       Inst.eraseFromParent();
6195       continue;
6196 
6197     case AMDGPU::S_XNOR_B32:
6198       lowerScalarXnor(Worklist, Inst);
6199       Inst.eraseFromParent();
6200       continue;
6201 
6202     case AMDGPU::S_NAND_B32:
6203       splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32);
6204       Inst.eraseFromParent();
6205       continue;
6206 
6207     case AMDGPU::S_NOR_B32:
6208       splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32);
6209       Inst.eraseFromParent();
6210       continue;
6211 
6212     case AMDGPU::S_ANDN2_B32:
6213       splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32);
6214       Inst.eraseFromParent();
6215       continue;
6216 
6217     case AMDGPU::S_ORN2_B32:
6218       splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32);
6219       Inst.eraseFromParent();
6220       continue;
6221 
6222     // TODO: remove as soon as everything is ready
6223     // to replace VGPR to SGPR copy with V_READFIRSTLANEs.
6224     // S_ADD/SUB_CO_PSEUDO as well as S_UADDO/USUBO_PSEUDO
6225     // can only be selected from the uniform SDNode.
6226     case AMDGPU::S_ADD_CO_PSEUDO:
6227     case AMDGPU::S_SUB_CO_PSEUDO: {
6228       unsigned Opc = (Inst.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO)
6229                          ? AMDGPU::V_ADDC_U32_e64
6230                          : AMDGPU::V_SUBB_U32_e64;
6231       const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
6232 
6233       Register CarryInReg = Inst.getOperand(4).getReg();
6234       if (!MRI.constrainRegClass(CarryInReg, CarryRC)) {
6235         Register NewCarryReg = MRI.createVirtualRegister(CarryRC);
6236         BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(AMDGPU::COPY), NewCarryReg)
6237             .addReg(CarryInReg);
6238       }
6239 
6240       Register CarryOutReg = Inst.getOperand(1).getReg();
6241 
6242       Register DestReg = MRI.createVirtualRegister(RI.getEquivalentVGPRClass(
6243           MRI.getRegClass(Inst.getOperand(0).getReg())));
6244       MachineInstr *CarryOp =
6245           BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(Opc), DestReg)
6246               .addReg(CarryOutReg, RegState::Define)
6247               .add(Inst.getOperand(2))
6248               .add(Inst.getOperand(3))
6249               .addReg(CarryInReg)
6250               .addImm(0);
6251       CreatedBBTmp = legalizeOperands(*CarryOp);
6252       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6253         CreatedBB = CreatedBBTmp;
6254       MRI.replaceRegWith(Inst.getOperand(0).getReg(), DestReg);
6255       addUsersToMoveToVALUWorklist(DestReg, MRI, Worklist);
6256       Inst.eraseFromParent();
6257     }
6258       continue;
6259     case AMDGPU::S_UADDO_PSEUDO:
6260     case AMDGPU::S_USUBO_PSEUDO: {
6261       const DebugLoc &DL = Inst.getDebugLoc();
6262       MachineOperand &Dest0 = Inst.getOperand(0);
6263       MachineOperand &Dest1 = Inst.getOperand(1);
6264       MachineOperand &Src0 = Inst.getOperand(2);
6265       MachineOperand &Src1 = Inst.getOperand(3);
6266 
6267       unsigned Opc = (Inst.getOpcode() == AMDGPU::S_UADDO_PSEUDO)
6268                          ? AMDGPU::V_ADD_CO_U32_e64
6269                          : AMDGPU::V_SUB_CO_U32_e64;
6270       const TargetRegisterClass *NewRC =
6271           RI.getEquivalentVGPRClass(MRI.getRegClass(Dest0.getReg()));
6272       Register DestReg = MRI.createVirtualRegister(NewRC);
6273       MachineInstr *NewInstr = BuildMI(*MBB, &Inst, DL, get(Opc), DestReg)
6274                                    .addReg(Dest1.getReg(), RegState::Define)
6275                                    .add(Src0)
6276                                    .add(Src1)
6277                                    .addImm(0); // clamp bit
6278 
6279       CreatedBBTmp = legalizeOperands(*NewInstr, MDT);
6280       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6281         CreatedBB = CreatedBBTmp;
6282 
6283       MRI.replaceRegWith(Dest0.getReg(), DestReg);
6284       addUsersToMoveToVALUWorklist(NewInstr->getOperand(0).getReg(), MRI,
6285                                    Worklist);
6286       Inst.eraseFromParent();
6287     }
6288       continue;
6289 
6290     case AMDGPU::S_CSELECT_B32:
6291     case AMDGPU::S_CSELECT_B64:
6292       lowerSelect(Worklist, Inst, MDT);
6293       Inst.eraseFromParent();
6294       continue;
6295     case AMDGPU::S_CMP_EQ_I32:
6296     case AMDGPU::S_CMP_LG_I32:
6297     case AMDGPU::S_CMP_GT_I32:
6298     case AMDGPU::S_CMP_GE_I32:
6299     case AMDGPU::S_CMP_LT_I32:
6300     case AMDGPU::S_CMP_LE_I32:
6301     case AMDGPU::S_CMP_EQ_U32:
6302     case AMDGPU::S_CMP_LG_U32:
6303     case AMDGPU::S_CMP_GT_U32:
6304     case AMDGPU::S_CMP_GE_U32:
6305     case AMDGPU::S_CMP_LT_U32:
6306     case AMDGPU::S_CMP_LE_U32:
6307     case AMDGPU::S_CMP_EQ_U64:
6308     case AMDGPU::S_CMP_LG_U64: {
6309         const MCInstrDesc &NewDesc = get(NewOpcode);
6310         Register CondReg = MRI.createVirtualRegister(RI.getWaveMaskRegClass());
6311         MachineInstr *NewInstr =
6312             BuildMI(*MBB, Inst, Inst.getDebugLoc(), NewDesc, CondReg)
6313                 .add(Inst.getOperand(0))
6314                 .add(Inst.getOperand(1));
6315         legalizeOperands(*NewInstr, MDT);
6316         int SCCIdx = Inst.findRegisterDefOperandIdx(AMDGPU::SCC);
6317         MachineOperand SCCOp = Inst.getOperand(SCCIdx);
6318         addSCCDefUsersToVALUWorklist(SCCOp, Inst, Worklist, CondReg);
6319         Inst.eraseFromParent();
6320       }
6321       continue;
6322     }
6323 
6324 
6325     if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
6326       // We cannot move this instruction to the VALU, so we should try to
6327       // legalize its operands instead.
6328       CreatedBBTmp = legalizeOperands(Inst, MDT);
6329       if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6330         CreatedBB = CreatedBBTmp;
6331       continue;
6332     }
6333 
6334     // Use the new VALU Opcode.
6335     const MCInstrDesc &NewDesc = get(NewOpcode);
6336     Inst.setDesc(NewDesc);
6337 
6338     // Remove any references to SCC. Vector instructions can't read from it, and
6339     // We're just about to add the implicit use / defs of VCC, and we don't want
6340     // both.
6341     for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) {
6342       MachineOperand &Op = Inst.getOperand(i);
6343       if (Op.isReg() && Op.getReg() == AMDGPU::SCC) {
6344         // Only propagate through live-def of SCC.
6345         if (Op.isDef() && !Op.isDead())
6346           addSCCDefUsersToVALUWorklist(Op, Inst, Worklist);
6347         if (Op.isUse())
6348           addSCCDefsToVALUWorklist(Op, Worklist);
6349         Inst.removeOperand(i);
6350       }
6351     }
6352 
6353     if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) {
6354       // We are converting these to a BFE, so we need to add the missing
6355       // operands for the size and offset.
6356       unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16;
6357       Inst.addOperand(MachineOperand::CreateImm(0));
6358       Inst.addOperand(MachineOperand::CreateImm(Size));
6359 
6360     } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) {
6361       // The VALU version adds the second operand to the result, so insert an
6362       // extra 0 operand.
6363       Inst.addOperand(MachineOperand::CreateImm(0));
6364     }
6365 
6366     Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent());
6367     fixImplicitOperands(Inst);
6368 
6369     if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) {
6370       const MachineOperand &OffsetWidthOp = Inst.getOperand(2);
6371       // If we need to move this to VGPRs, we need to unpack the second operand
6372       // back into the 2 separate ones for bit offset and width.
6373       assert(OffsetWidthOp.isImm() &&
6374              "Scalar BFE is only implemented for constant width and offset");
6375       uint32_t Imm = OffsetWidthOp.getImm();
6376 
6377       uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
6378       uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
6379       Inst.removeOperand(2);                     // Remove old immediate.
6380       Inst.addOperand(MachineOperand::CreateImm(Offset));
6381       Inst.addOperand(MachineOperand::CreateImm(BitWidth));
6382     }
6383 
6384     bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef();
6385     unsigned NewDstReg = AMDGPU::NoRegister;
6386     if (HasDst) {
6387       Register DstReg = Inst.getOperand(0).getReg();
6388       if (DstReg.isPhysical())
6389         continue;
6390 
6391       // Update the destination register class.
6392       const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst);
6393       if (!NewDstRC)
6394         continue;
6395 
6396       if (Inst.isCopy() && Inst.getOperand(1).getReg().isVirtual() &&
6397           NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) {
6398         // Instead of creating a copy where src and dst are the same register
6399         // class, we just replace all uses of dst with src.  These kinds of
6400         // copies interfere with the heuristics MachineSink uses to decide
6401         // whether or not to split a critical edge.  Since the pass assumes
6402         // that copies will end up as machine instructions and not be
6403         // eliminated.
6404         addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist);
6405         MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg());
6406         MRI.clearKillFlags(Inst.getOperand(1).getReg());
6407         Inst.getOperand(0).setReg(DstReg);
6408 
6409         // Make sure we don't leave around a dead VGPR->SGPR copy. Normally
6410         // these are deleted later, but at -O0 it would leave a suspicious
6411         // looking illegal copy of an undef register.
6412         for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I)
6413           Inst.removeOperand(I);
6414         Inst.setDesc(get(AMDGPU::IMPLICIT_DEF));
6415         continue;
6416       }
6417 
6418       NewDstReg = MRI.createVirtualRegister(NewDstRC);
6419       MRI.replaceRegWith(DstReg, NewDstReg);
6420     }
6421 
6422     // Legalize the operands
6423     CreatedBBTmp = legalizeOperands(Inst, MDT);
6424     if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp)
6425       CreatedBB = CreatedBBTmp;
6426 
6427     if (HasDst)
6428      addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
6429   }
6430   return CreatedBB;
6431 }
6432 
6433 // Add/sub require special handling to deal with carry outs.
6434 std::pair<bool, MachineBasicBlock *>
6435 SIInstrInfo::moveScalarAddSub(SetVectorType &Worklist, MachineInstr &Inst,
6436                               MachineDominatorTree *MDT) const {
6437   if (ST.hasAddNoCarry()) {
6438     // Assume there is no user of scc since we don't select this in that case.
6439     // Since scc isn't used, it doesn't really matter if the i32 or u32 variant
6440     // is used.
6441 
6442     MachineBasicBlock &MBB = *Inst.getParent();
6443     MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6444 
6445     Register OldDstReg = Inst.getOperand(0).getReg();
6446     Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6447 
6448     unsigned Opc = Inst.getOpcode();
6449     assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32);
6450 
6451     unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ?
6452       AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64;
6453 
6454     assert(Inst.getOperand(3).getReg() == AMDGPU::SCC);
6455     Inst.removeOperand(3);
6456 
6457     Inst.setDesc(get(NewOpc));
6458     Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit
6459     Inst.addImplicitDefUseOperands(*MBB.getParent());
6460     MRI.replaceRegWith(OldDstReg, ResultReg);
6461     MachineBasicBlock *NewBB = legalizeOperands(Inst, MDT);
6462 
6463     addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6464     return std::make_pair(true, NewBB);
6465   }
6466 
6467   return std::make_pair(false, nullptr);
6468 }
6469 
6470 void SIInstrInfo::lowerSelect(SetVectorType &Worklist, MachineInstr &Inst,
6471                               MachineDominatorTree *MDT) const {
6472 
6473   MachineBasicBlock &MBB = *Inst.getParent();
6474   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6475   MachineBasicBlock::iterator MII = Inst;
6476   DebugLoc DL = Inst.getDebugLoc();
6477 
6478   MachineOperand &Dest = Inst.getOperand(0);
6479   MachineOperand &Src0 = Inst.getOperand(1);
6480   MachineOperand &Src1 = Inst.getOperand(2);
6481   MachineOperand &Cond = Inst.getOperand(3);
6482 
6483   Register SCCSource = Cond.getReg();
6484   bool IsSCC = (SCCSource == AMDGPU::SCC);
6485 
6486   // If this is a trivial select where the condition is effectively not SCC
6487   // (SCCSource is a source of copy to SCC), then the select is semantically
6488   // equivalent to copying SCCSource. Hence, there is no need to create
6489   // V_CNDMASK, we can just use that and bail out.
6490   if (!IsSCC && Src0.isImm() && (Src0.getImm() == -1) && Src1.isImm() &&
6491       (Src1.getImm() == 0)) {
6492     MRI.replaceRegWith(Dest.getReg(), SCCSource);
6493     return;
6494   }
6495 
6496   const TargetRegisterClass *TC =
6497       RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
6498 
6499   Register CopySCC = MRI.createVirtualRegister(TC);
6500 
6501   if (IsSCC) {
6502     // Now look for the closest SCC def if it is a copy
6503     // replacing the SCCSource with the COPY source register
6504     bool CopyFound = false;
6505     for (MachineInstr &CandI :
6506          make_range(std::next(MachineBasicBlock::reverse_iterator(Inst)),
6507                     Inst.getParent()->rend())) {
6508       if (CandI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) !=
6509           -1) {
6510         if (CandI.isCopy() && CandI.getOperand(0).getReg() == AMDGPU::SCC) {
6511           BuildMI(MBB, MII, DL, get(AMDGPU::COPY), CopySCC)
6512               .addReg(CandI.getOperand(1).getReg());
6513           CopyFound = true;
6514         }
6515         break;
6516       }
6517     }
6518     if (!CopyFound) {
6519       // SCC def is not a copy
6520       // Insert a trivial select instead of creating a copy, because a copy from
6521       // SCC would semantically mean just copying a single bit, but we may need
6522       // the result to be a vector condition mask that needs preserving.
6523       unsigned Opcode = (ST.getWavefrontSize() == 64) ? AMDGPU::S_CSELECT_B64
6524                                                       : AMDGPU::S_CSELECT_B32;
6525       auto NewSelect =
6526           BuildMI(MBB, MII, DL, get(Opcode), CopySCC).addImm(-1).addImm(0);
6527       NewSelect->getOperand(3).setIsUndef(Cond.isUndef());
6528     }
6529   }
6530 
6531   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6532 
6533   auto UpdatedInst =
6534       BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B32_e64), ResultReg)
6535           .addImm(0)
6536           .add(Src1) // False
6537           .addImm(0)
6538           .add(Src0) // True
6539           .addReg(IsSCC ? CopySCC : SCCSource);
6540 
6541   MRI.replaceRegWith(Dest.getReg(), ResultReg);
6542   legalizeOperands(*UpdatedInst, MDT);
6543   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6544 }
6545 
6546 void SIInstrInfo::lowerScalarAbs(SetVectorType &Worklist,
6547                                  MachineInstr &Inst) const {
6548   MachineBasicBlock &MBB = *Inst.getParent();
6549   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6550   MachineBasicBlock::iterator MII = Inst;
6551   DebugLoc DL = Inst.getDebugLoc();
6552 
6553   MachineOperand &Dest = Inst.getOperand(0);
6554   MachineOperand &Src = Inst.getOperand(1);
6555   Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6556   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6557 
6558   unsigned SubOp = ST.hasAddNoCarry() ?
6559     AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_CO_U32_e32;
6560 
6561   BuildMI(MBB, MII, DL, get(SubOp), TmpReg)
6562     .addImm(0)
6563     .addReg(Src.getReg());
6564 
6565   BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg)
6566     .addReg(Src.getReg())
6567     .addReg(TmpReg);
6568 
6569   MRI.replaceRegWith(Dest.getReg(), ResultReg);
6570   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6571 }
6572 
6573 void SIInstrInfo::lowerScalarXnor(SetVectorType &Worklist,
6574                                   MachineInstr &Inst) const {
6575   MachineBasicBlock &MBB = *Inst.getParent();
6576   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6577   MachineBasicBlock::iterator MII = Inst;
6578   const DebugLoc &DL = Inst.getDebugLoc();
6579 
6580   MachineOperand &Dest = Inst.getOperand(0);
6581   MachineOperand &Src0 = Inst.getOperand(1);
6582   MachineOperand &Src1 = Inst.getOperand(2);
6583 
6584   if (ST.hasDLInsts()) {
6585     Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6586     legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL);
6587     legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL);
6588 
6589     BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest)
6590       .add(Src0)
6591       .add(Src1);
6592 
6593     MRI.replaceRegWith(Dest.getReg(), NewDest);
6594     addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6595   } else {
6596     // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can
6597     // invert either source and then perform the XOR. If either source is a
6598     // scalar register, then we can leave the inversion on the scalar unit to
6599     // achieve a better distribution of scalar and vector instructions.
6600     bool Src0IsSGPR = Src0.isReg() &&
6601                       RI.isSGPRClass(MRI.getRegClass(Src0.getReg()));
6602     bool Src1IsSGPR = Src1.isReg() &&
6603                       RI.isSGPRClass(MRI.getRegClass(Src1.getReg()));
6604     MachineInstr *Xor;
6605     Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6606     Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6607 
6608     // Build a pair of scalar instructions and add them to the work list.
6609     // The next iteration over the work list will lower these to the vector
6610     // unit as necessary.
6611     if (Src0IsSGPR) {
6612       BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0);
6613       Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
6614       .addReg(Temp)
6615       .add(Src1);
6616     } else if (Src1IsSGPR) {
6617       BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1);
6618       Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest)
6619       .add(Src0)
6620       .addReg(Temp);
6621     } else {
6622       Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp)
6623         .add(Src0)
6624         .add(Src1);
6625       MachineInstr *Not =
6626           BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp);
6627       Worklist.insert(Not);
6628     }
6629 
6630     MRI.replaceRegWith(Dest.getReg(), NewDest);
6631 
6632     Worklist.insert(Xor);
6633 
6634     addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6635   }
6636 }
6637 
6638 void SIInstrInfo::splitScalarNotBinop(SetVectorType &Worklist,
6639                                       MachineInstr &Inst,
6640                                       unsigned Opcode) const {
6641   MachineBasicBlock &MBB = *Inst.getParent();
6642   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6643   MachineBasicBlock::iterator MII = Inst;
6644   const DebugLoc &DL = Inst.getDebugLoc();
6645 
6646   MachineOperand &Dest = Inst.getOperand(0);
6647   MachineOperand &Src0 = Inst.getOperand(1);
6648   MachineOperand &Src1 = Inst.getOperand(2);
6649 
6650   Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6651   Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6652 
6653   MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm)
6654     .add(Src0)
6655     .add(Src1);
6656 
6657   MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest)
6658     .addReg(Interm);
6659 
6660   Worklist.insert(&Op);
6661   Worklist.insert(&Not);
6662 
6663   MRI.replaceRegWith(Dest.getReg(), NewDest);
6664   addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6665 }
6666 
6667 void SIInstrInfo::splitScalarBinOpN2(SetVectorType& Worklist,
6668                                      MachineInstr &Inst,
6669                                      unsigned Opcode) const {
6670   MachineBasicBlock &MBB = *Inst.getParent();
6671   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6672   MachineBasicBlock::iterator MII = Inst;
6673   const DebugLoc &DL = Inst.getDebugLoc();
6674 
6675   MachineOperand &Dest = Inst.getOperand(0);
6676   MachineOperand &Src0 = Inst.getOperand(1);
6677   MachineOperand &Src1 = Inst.getOperand(2);
6678 
6679   Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6680   Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6681 
6682   MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm)
6683     .add(Src1);
6684 
6685   MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest)
6686     .add(Src0)
6687     .addReg(Interm);
6688 
6689   Worklist.insert(&Not);
6690   Worklist.insert(&Op);
6691 
6692   MRI.replaceRegWith(Dest.getReg(), NewDest);
6693   addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist);
6694 }
6695 
6696 void SIInstrInfo::splitScalar64BitUnaryOp(
6697     SetVectorType &Worklist, MachineInstr &Inst,
6698     unsigned Opcode, bool Swap) const {
6699   MachineBasicBlock &MBB = *Inst.getParent();
6700   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6701 
6702   MachineOperand &Dest = Inst.getOperand(0);
6703   MachineOperand &Src0 = Inst.getOperand(1);
6704   DebugLoc DL = Inst.getDebugLoc();
6705 
6706   MachineBasicBlock::iterator MII = Inst;
6707 
6708   const MCInstrDesc &InstDesc = get(Opcode);
6709   const TargetRegisterClass *Src0RC = Src0.isReg() ?
6710     MRI.getRegClass(Src0.getReg()) :
6711     &AMDGPU::SGPR_32RegClass;
6712 
6713   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
6714 
6715   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6716                                                        AMDGPU::sub0, Src0SubRC);
6717 
6718   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
6719   const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
6720   const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
6721 
6722   Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
6723   MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0);
6724 
6725   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6726                                                        AMDGPU::sub1, Src0SubRC);
6727 
6728   Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
6729   MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1);
6730 
6731   if (Swap)
6732     std::swap(DestSub0, DestSub1);
6733 
6734   Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
6735   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
6736     .addReg(DestSub0)
6737     .addImm(AMDGPU::sub0)
6738     .addReg(DestSub1)
6739     .addImm(AMDGPU::sub1);
6740 
6741   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
6742 
6743   Worklist.insert(&LoHalf);
6744   Worklist.insert(&HiHalf);
6745 
6746   // We don't need to legalizeOperands here because for a single operand, src0
6747   // will support any kind of input.
6748 
6749   // Move all users of this moved value.
6750   addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
6751 }
6752 
6753 void SIInstrInfo::splitScalar64BitAddSub(SetVectorType &Worklist,
6754                                          MachineInstr &Inst,
6755                                          MachineDominatorTree *MDT) const {
6756   bool IsAdd = (Inst.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO);
6757 
6758   MachineBasicBlock &MBB = *Inst.getParent();
6759   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6760   const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
6761 
6762   Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
6763   Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6764   Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6765 
6766   Register CarryReg = MRI.createVirtualRegister(CarryRC);
6767   Register DeadCarryReg = MRI.createVirtualRegister(CarryRC);
6768 
6769   MachineOperand &Dest = Inst.getOperand(0);
6770   MachineOperand &Src0 = Inst.getOperand(1);
6771   MachineOperand &Src1 = Inst.getOperand(2);
6772   const DebugLoc &DL = Inst.getDebugLoc();
6773   MachineBasicBlock::iterator MII = Inst;
6774 
6775   const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg());
6776   const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg());
6777   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
6778   const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
6779 
6780   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6781                                                        AMDGPU::sub0, Src0SubRC);
6782   MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6783                                                        AMDGPU::sub0, Src1SubRC);
6784 
6785 
6786   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6787                                                        AMDGPU::sub1, Src0SubRC);
6788   MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6789                                                        AMDGPU::sub1, Src1SubRC);
6790 
6791   unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64;
6792   MachineInstr *LoHalf =
6793     BuildMI(MBB, MII, DL, get(LoOpc), DestSub0)
6794     .addReg(CarryReg, RegState::Define)
6795     .add(SrcReg0Sub0)
6796     .add(SrcReg1Sub0)
6797     .addImm(0); // clamp bit
6798 
6799   unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64;
6800   MachineInstr *HiHalf =
6801     BuildMI(MBB, MII, DL, get(HiOpc), DestSub1)
6802     .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
6803     .add(SrcReg0Sub1)
6804     .add(SrcReg1Sub1)
6805     .addReg(CarryReg, RegState::Kill)
6806     .addImm(0); // clamp bit
6807 
6808   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
6809     .addReg(DestSub0)
6810     .addImm(AMDGPU::sub0)
6811     .addReg(DestSub1)
6812     .addImm(AMDGPU::sub1);
6813 
6814   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
6815 
6816   // Try to legalize the operands in case we need to swap the order to keep it
6817   // valid.
6818   legalizeOperands(*LoHalf, MDT);
6819   legalizeOperands(*HiHalf, MDT);
6820 
6821   // Move all users of this moved value.
6822   addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
6823 }
6824 
6825 void SIInstrInfo::splitScalar64BitBinaryOp(SetVectorType &Worklist,
6826                                            MachineInstr &Inst, unsigned Opcode,
6827                                            MachineDominatorTree *MDT) const {
6828   MachineBasicBlock &MBB = *Inst.getParent();
6829   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6830 
6831   MachineOperand &Dest = Inst.getOperand(0);
6832   MachineOperand &Src0 = Inst.getOperand(1);
6833   MachineOperand &Src1 = Inst.getOperand(2);
6834   DebugLoc DL = Inst.getDebugLoc();
6835 
6836   MachineBasicBlock::iterator MII = Inst;
6837 
6838   const MCInstrDesc &InstDesc = get(Opcode);
6839   const TargetRegisterClass *Src0RC = Src0.isReg() ?
6840     MRI.getRegClass(Src0.getReg()) :
6841     &AMDGPU::SGPR_32RegClass;
6842 
6843   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
6844   const TargetRegisterClass *Src1RC = Src1.isReg() ?
6845     MRI.getRegClass(Src1.getReg()) :
6846     &AMDGPU::SGPR_32RegClass;
6847 
6848   const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
6849 
6850   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6851                                                        AMDGPU::sub0, Src0SubRC);
6852   MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6853                                                        AMDGPU::sub0, Src1SubRC);
6854   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
6855                                                        AMDGPU::sub1, Src0SubRC);
6856   MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
6857                                                        AMDGPU::sub1, Src1SubRC);
6858 
6859   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
6860   const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
6861   const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
6862 
6863   Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
6864   MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0)
6865                               .add(SrcReg0Sub0)
6866                               .add(SrcReg1Sub0);
6867 
6868   Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
6869   MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1)
6870                               .add(SrcReg0Sub1)
6871                               .add(SrcReg1Sub1);
6872 
6873   Register FullDestReg = MRI.createVirtualRegister(NewDestRC);
6874   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
6875     .addReg(DestSub0)
6876     .addImm(AMDGPU::sub0)
6877     .addReg(DestSub1)
6878     .addImm(AMDGPU::sub1);
6879 
6880   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
6881 
6882   Worklist.insert(&LoHalf);
6883   Worklist.insert(&HiHalf);
6884 
6885   // Move all users of this moved value.
6886   addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
6887 }
6888 
6889 void SIInstrInfo::splitScalar64BitXnor(SetVectorType &Worklist,
6890                                        MachineInstr &Inst,
6891                                        MachineDominatorTree *MDT) const {
6892   MachineBasicBlock &MBB = *Inst.getParent();
6893   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6894 
6895   MachineOperand &Dest = Inst.getOperand(0);
6896   MachineOperand &Src0 = Inst.getOperand(1);
6897   MachineOperand &Src1 = Inst.getOperand(2);
6898   const DebugLoc &DL = Inst.getDebugLoc();
6899 
6900   MachineBasicBlock::iterator MII = Inst;
6901 
6902   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
6903 
6904   Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
6905 
6906   MachineOperand* Op0;
6907   MachineOperand* Op1;
6908 
6909   if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) {
6910     Op0 = &Src0;
6911     Op1 = &Src1;
6912   } else {
6913     Op0 = &Src1;
6914     Op1 = &Src0;
6915   }
6916 
6917   BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm)
6918     .add(*Op0);
6919 
6920   Register NewDest = MRI.createVirtualRegister(DestRC);
6921 
6922   MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest)
6923     .addReg(Interm)
6924     .add(*Op1);
6925 
6926   MRI.replaceRegWith(Dest.getReg(), NewDest);
6927 
6928   Worklist.insert(&Xor);
6929 }
6930 
6931 void SIInstrInfo::splitScalar64BitBCNT(
6932     SetVectorType &Worklist, MachineInstr &Inst) const {
6933   MachineBasicBlock &MBB = *Inst.getParent();
6934   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6935 
6936   MachineBasicBlock::iterator MII = Inst;
6937   const DebugLoc &DL = Inst.getDebugLoc();
6938 
6939   MachineOperand &Dest = Inst.getOperand(0);
6940   MachineOperand &Src = Inst.getOperand(1);
6941 
6942   const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64);
6943   const TargetRegisterClass *SrcRC = Src.isReg() ?
6944     MRI.getRegClass(Src.getReg()) :
6945     &AMDGPU::SGPR_32RegClass;
6946 
6947   Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6948   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6949 
6950   const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0);
6951 
6952   MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
6953                                                       AMDGPU::sub0, SrcSubRC);
6954   MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
6955                                                       AMDGPU::sub1, SrcSubRC);
6956 
6957   BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0);
6958 
6959   BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg);
6960 
6961   MRI.replaceRegWith(Dest.getReg(), ResultReg);
6962 
6963   // We don't need to legalize operands here. src0 for either instruction can be
6964   // an SGPR, and the second input is unused or determined here.
6965   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
6966 }
6967 
6968 void SIInstrInfo::splitScalar64BitBFE(SetVectorType &Worklist,
6969                                       MachineInstr &Inst) const {
6970   MachineBasicBlock &MBB = *Inst.getParent();
6971   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
6972   MachineBasicBlock::iterator MII = Inst;
6973   const DebugLoc &DL = Inst.getDebugLoc();
6974 
6975   MachineOperand &Dest = Inst.getOperand(0);
6976   uint32_t Imm = Inst.getOperand(2).getImm();
6977   uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
6978   uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
6979 
6980   (void) Offset;
6981 
6982   // Only sext_inreg cases handled.
6983   assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 &&
6984          Offset == 0 && "Not implemented");
6985 
6986   if (BitWidth < 32) {
6987     Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6988     Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6989     Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
6990 
6991     BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32_e64), MidRegLo)
6992         .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0)
6993         .addImm(0)
6994         .addImm(BitWidth);
6995 
6996     BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi)
6997       .addImm(31)
6998       .addReg(MidRegLo);
6999 
7000     BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
7001       .addReg(MidRegLo)
7002       .addImm(AMDGPU::sub0)
7003       .addReg(MidRegHi)
7004       .addImm(AMDGPU::sub1);
7005 
7006     MRI.replaceRegWith(Dest.getReg(), ResultReg);
7007     addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
7008     return;
7009   }
7010 
7011   MachineOperand &Src = Inst.getOperand(1);
7012   Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7013   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
7014 
7015   BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg)
7016     .addImm(31)
7017     .addReg(Src.getReg(), 0, AMDGPU::sub0);
7018 
7019   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
7020     .addReg(Src.getReg(), 0, AMDGPU::sub0)
7021     .addImm(AMDGPU::sub0)
7022     .addReg(TmpReg)
7023     .addImm(AMDGPU::sub1);
7024 
7025   MRI.replaceRegWith(Dest.getReg(), ResultReg);
7026   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
7027 }
7028 
7029 void SIInstrInfo::addUsersToMoveToVALUWorklist(
7030   Register DstReg,
7031   MachineRegisterInfo &MRI,
7032   SetVectorType &Worklist) const {
7033   for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg),
7034          E = MRI.use_end(); I != E;) {
7035     MachineInstr &UseMI = *I->getParent();
7036 
7037     unsigned OpNo = 0;
7038 
7039     switch (UseMI.getOpcode()) {
7040     case AMDGPU::COPY:
7041     case AMDGPU::WQM:
7042     case AMDGPU::SOFT_WQM:
7043     case AMDGPU::STRICT_WWM:
7044     case AMDGPU::STRICT_WQM:
7045     case AMDGPU::REG_SEQUENCE:
7046     case AMDGPU::PHI:
7047     case AMDGPU::INSERT_SUBREG:
7048       break;
7049     default:
7050       OpNo = I.getOperandNo();
7051       break;
7052     }
7053 
7054     if (!RI.hasVectorRegisters(getOpRegClass(UseMI, OpNo))) {
7055       Worklist.insert(&UseMI);
7056 
7057       do {
7058         ++I;
7059       } while (I != E && I->getParent() == &UseMI);
7060     } else {
7061       ++I;
7062     }
7063   }
7064 }
7065 
7066 void SIInstrInfo::movePackToVALU(SetVectorType &Worklist,
7067                                  MachineRegisterInfo &MRI,
7068                                  MachineInstr &Inst) const {
7069   Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7070   MachineBasicBlock *MBB = Inst.getParent();
7071   MachineOperand &Src0 = Inst.getOperand(1);
7072   MachineOperand &Src1 = Inst.getOperand(2);
7073   const DebugLoc &DL = Inst.getDebugLoc();
7074 
7075   switch (Inst.getOpcode()) {
7076   case AMDGPU::S_PACK_LL_B32_B16: {
7077     Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7078     Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7079 
7080     // FIXME: Can do a lot better if we know the high bits of src0 or src1 are
7081     // 0.
7082     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
7083       .addImm(0xffff);
7084 
7085     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg)
7086       .addReg(ImmReg, RegState::Kill)
7087       .add(Src0);
7088 
7089     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg)
7090       .add(Src1)
7091       .addImm(16)
7092       .addReg(TmpReg, RegState::Kill);
7093     break;
7094   }
7095   case AMDGPU::S_PACK_LH_B32_B16: {
7096     Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7097     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
7098       .addImm(0xffff);
7099     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32_e64), ResultReg)
7100       .addReg(ImmReg, RegState::Kill)
7101       .add(Src0)
7102       .add(Src1);
7103     break;
7104   }
7105   case AMDGPU::S_PACK_HH_B32_B16: {
7106     Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7107     Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
7108     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg)
7109       .addImm(16)
7110       .add(Src0);
7111     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg)
7112       .addImm(0xffff0000);
7113     BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32_e64), ResultReg)
7114       .add(Src1)
7115       .addReg(ImmReg, RegState::Kill)
7116       .addReg(TmpReg, RegState::Kill);
7117     break;
7118   }
7119   default:
7120     llvm_unreachable("unhandled s_pack_* instruction");
7121   }
7122 
7123   MachineOperand &Dest = Inst.getOperand(0);
7124   MRI.replaceRegWith(Dest.getReg(), ResultReg);
7125   addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
7126 }
7127 
7128 void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op,
7129                                                MachineInstr &SCCDefInst,
7130                                                SetVectorType &Worklist,
7131                                                Register NewCond) const {
7132 
7133   // Ensure that def inst defines SCC, which is still live.
7134   assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() &&
7135          !Op.isDead() && Op.getParent() == &SCCDefInst);
7136   SmallVector<MachineInstr *, 4> CopyToDelete;
7137   // This assumes that all the users of SCC are in the same block
7138   // as the SCC def.
7139   for (MachineInstr &MI : // Skip the def inst itself.
7140        make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)),
7141                   SCCDefInst.getParent()->end())) {
7142     // Check if SCC is used first.
7143     int SCCIdx = MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI);
7144     if (SCCIdx != -1) {
7145       if (MI.isCopy()) {
7146         MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
7147         Register DestReg = MI.getOperand(0).getReg();
7148 
7149         MRI.replaceRegWith(DestReg, NewCond);
7150         CopyToDelete.push_back(&MI);
7151       } else {
7152 
7153         if (NewCond.isValid())
7154           MI.getOperand(SCCIdx).setReg(NewCond);
7155 
7156         Worklist.insert(&MI);
7157       }
7158     }
7159     // Exit if we find another SCC def.
7160     if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1)
7161       break;
7162   }
7163   for (auto &Copy : CopyToDelete)
7164     Copy->eraseFromParent();
7165 }
7166 
7167 // Instructions that use SCC may be converted to VALU instructions. When that
7168 // happens, the SCC register is changed to VCC_LO. The instruction that defines
7169 // SCC must be changed to an instruction that defines VCC. This function makes
7170 // sure that the instruction that defines SCC is added to the moveToVALU
7171 // worklist.
7172 void SIInstrInfo::addSCCDefsToVALUWorklist(MachineOperand &Op,
7173                                            SetVectorType &Worklist) const {
7174   assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isUse());
7175 
7176   MachineInstr *SCCUseInst = Op.getParent();
7177   // Look for a preceding instruction that either defines VCC or SCC. If VCC
7178   // then there is nothing to do because the defining instruction has been
7179   // converted to a VALU already. If SCC then that instruction needs to be
7180   // converted to a VALU.
7181   for (MachineInstr &MI :
7182        make_range(std::next(MachineBasicBlock::reverse_iterator(SCCUseInst)),
7183                   SCCUseInst->getParent()->rend())) {
7184     if (MI.modifiesRegister(AMDGPU::VCC, &RI))
7185       break;
7186     if (MI.definesRegister(AMDGPU::SCC, &RI)) {
7187       Worklist.insert(&MI);
7188       break;
7189     }
7190   }
7191 }
7192 
7193 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass(
7194   const MachineInstr &Inst) const {
7195   const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0);
7196 
7197   switch (Inst.getOpcode()) {
7198   // For target instructions, getOpRegClass just returns the virtual register
7199   // class associated with the operand, so we need to find an equivalent VGPR
7200   // register class in order to move the instruction to the VALU.
7201   case AMDGPU::COPY:
7202   case AMDGPU::PHI:
7203   case AMDGPU::REG_SEQUENCE:
7204   case AMDGPU::INSERT_SUBREG:
7205   case AMDGPU::WQM:
7206   case AMDGPU::SOFT_WQM:
7207   case AMDGPU::STRICT_WWM:
7208   case AMDGPU::STRICT_WQM: {
7209     const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1);
7210     if (RI.isAGPRClass(SrcRC)) {
7211       if (RI.isAGPRClass(NewDstRC))
7212         return nullptr;
7213 
7214       switch (Inst.getOpcode()) {
7215       case AMDGPU::PHI:
7216       case AMDGPU::REG_SEQUENCE:
7217       case AMDGPU::INSERT_SUBREG:
7218         NewDstRC = RI.getEquivalentAGPRClass(NewDstRC);
7219         break;
7220       default:
7221         NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
7222       }
7223 
7224       if (!NewDstRC)
7225         return nullptr;
7226     } else {
7227       if (RI.isVGPRClass(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass)
7228         return nullptr;
7229 
7230       NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
7231       if (!NewDstRC)
7232         return nullptr;
7233     }
7234 
7235     return NewDstRC;
7236   }
7237   default:
7238     return NewDstRC;
7239   }
7240 }
7241 
7242 // Find the one SGPR operand we are allowed to use.
7243 Register SIInstrInfo::findUsedSGPR(const MachineInstr &MI,
7244                                    int OpIndices[3]) const {
7245   const MCInstrDesc &Desc = MI.getDesc();
7246 
7247   // Find the one SGPR operand we are allowed to use.
7248   //
7249   // First we need to consider the instruction's operand requirements before
7250   // legalizing. Some operands are required to be SGPRs, such as implicit uses
7251   // of VCC, but we are still bound by the constant bus requirement to only use
7252   // one.
7253   //
7254   // If the operand's class is an SGPR, we can never move it.
7255 
7256   Register SGPRReg = findImplicitSGPRRead(MI);
7257   if (SGPRReg != AMDGPU::NoRegister)
7258     return SGPRReg;
7259 
7260   Register UsedSGPRs[3] = { AMDGPU::NoRegister };
7261   const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
7262 
7263   for (unsigned i = 0; i < 3; ++i) {
7264     int Idx = OpIndices[i];
7265     if (Idx == -1)
7266       break;
7267 
7268     const MachineOperand &MO = MI.getOperand(Idx);
7269     if (!MO.isReg())
7270       continue;
7271 
7272     // Is this operand statically required to be an SGPR based on the operand
7273     // constraints?
7274     const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass);
7275     bool IsRequiredSGPR = RI.isSGPRClass(OpRC);
7276     if (IsRequiredSGPR)
7277       return MO.getReg();
7278 
7279     // If this could be a VGPR or an SGPR, Check the dynamic register class.
7280     Register Reg = MO.getReg();
7281     const TargetRegisterClass *RegRC = MRI.getRegClass(Reg);
7282     if (RI.isSGPRClass(RegRC))
7283       UsedSGPRs[i] = Reg;
7284   }
7285 
7286   // We don't have a required SGPR operand, so we have a bit more freedom in
7287   // selecting operands to move.
7288 
7289   // Try to select the most used SGPR. If an SGPR is equal to one of the
7290   // others, we choose that.
7291   //
7292   // e.g.
7293   // V_FMA_F32 v0, s0, s0, s0 -> No moves
7294   // V_FMA_F32 v0, s0, s1, s0 -> Move s1
7295 
7296   // TODO: If some of the operands are 64-bit SGPRs and some 32, we should
7297   // prefer those.
7298 
7299   if (UsedSGPRs[0] != AMDGPU::NoRegister) {
7300     if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2])
7301       SGPRReg = UsedSGPRs[0];
7302   }
7303 
7304   if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) {
7305     if (UsedSGPRs[1] == UsedSGPRs[2])
7306       SGPRReg = UsedSGPRs[1];
7307   }
7308 
7309   return SGPRReg;
7310 }
7311 
7312 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI,
7313                                              unsigned OperandName) const {
7314   int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName);
7315   if (Idx == -1)
7316     return nullptr;
7317 
7318   return &MI.getOperand(Idx);
7319 }
7320 
7321 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const {
7322   if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) {
7323     return (AMDGPU::MTBUFFormat::UFMT_32_FLOAT << 44) |
7324            (1ULL << 56) | // RESOURCE_LEVEL = 1
7325            (3ULL << 60); // OOB_SELECT = 3
7326   }
7327 
7328   uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT;
7329   if (ST.isAmdHsaOS()) {
7330     // Set ATC = 1. GFX9 doesn't have this bit.
7331     if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS)
7332       RsrcDataFormat |= (1ULL << 56);
7333 
7334     // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this.
7335     // BTW, it disables TC L2 and therefore decreases performance.
7336     if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS)
7337       RsrcDataFormat |= (2ULL << 59);
7338   }
7339 
7340   return RsrcDataFormat;
7341 }
7342 
7343 uint64_t SIInstrInfo::getScratchRsrcWords23() const {
7344   uint64_t Rsrc23 = getDefaultRsrcDataFormat() |
7345                     AMDGPU::RSRC_TID_ENABLE |
7346                     0xffffffff; // Size;
7347 
7348   // GFX9 doesn't have ELEMENT_SIZE.
7349   if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
7350     uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize(true)) - 1;
7351     Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT;
7352   }
7353 
7354   // IndexStride = 64 / 32.
7355   uint64_t IndexStride = ST.getWavefrontSize() == 64 ? 3 : 2;
7356   Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT;
7357 
7358   // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17].
7359   // Clear them unless we want a huge stride.
7360   if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
7361       ST.getGeneration() <= AMDGPUSubtarget::GFX9)
7362     Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT;
7363 
7364   return Rsrc23;
7365 }
7366 
7367 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const {
7368   unsigned Opc = MI.getOpcode();
7369 
7370   return isSMRD(Opc);
7371 }
7372 
7373 bool SIInstrInfo::isHighLatencyDef(int Opc) const {
7374   return get(Opc).mayLoad() &&
7375          (isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc) || isFLAT(Opc));
7376 }
7377 
7378 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI,
7379                                     int &FrameIndex) const {
7380   const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr);
7381   if (!Addr || !Addr->isFI())
7382     return AMDGPU::NoRegister;
7383 
7384   assert(!MI.memoperands_empty() &&
7385          (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS);
7386 
7387   FrameIndex = Addr->getIndex();
7388   return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg();
7389 }
7390 
7391 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI,
7392                                         int &FrameIndex) const {
7393   const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr);
7394   assert(Addr && Addr->isFI());
7395   FrameIndex = Addr->getIndex();
7396   return getNamedOperand(MI, AMDGPU::OpName::data)->getReg();
7397 }
7398 
7399 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
7400                                           int &FrameIndex) const {
7401   if (!MI.mayLoad())
7402     return AMDGPU::NoRegister;
7403 
7404   if (isMUBUF(MI) || isVGPRSpill(MI))
7405     return isStackAccess(MI, FrameIndex);
7406 
7407   if (isSGPRSpill(MI))
7408     return isSGPRStackAccess(MI, FrameIndex);
7409 
7410   return AMDGPU::NoRegister;
7411 }
7412 
7413 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
7414                                          int &FrameIndex) const {
7415   if (!MI.mayStore())
7416     return AMDGPU::NoRegister;
7417 
7418   if (isMUBUF(MI) || isVGPRSpill(MI))
7419     return isStackAccess(MI, FrameIndex);
7420 
7421   if (isSGPRSpill(MI))
7422     return isSGPRStackAccess(MI, FrameIndex);
7423 
7424   return AMDGPU::NoRegister;
7425 }
7426 
7427 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const {
7428   unsigned Size = 0;
7429   MachineBasicBlock::const_instr_iterator I = MI.getIterator();
7430   MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
7431   while (++I != E && I->isInsideBundle()) {
7432     assert(!I->isBundle() && "No nested bundle!");
7433     Size += getInstSizeInBytes(*I);
7434   }
7435 
7436   return Size;
7437 }
7438 
7439 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
7440   unsigned Opc = MI.getOpcode();
7441   const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc);
7442   unsigned DescSize = Desc.getSize();
7443 
7444   // If we have a definitive size, we can use it. Otherwise we need to inspect
7445   // the operands to know the size.
7446   if (isFixedSize(MI)) {
7447     unsigned Size = DescSize;
7448 
7449     // If we hit the buggy offset, an extra nop will be inserted in MC so
7450     // estimate the worst case.
7451     if (MI.isBranch() && ST.hasOffset3fBug())
7452       Size += 4;
7453 
7454     return Size;
7455   }
7456 
7457   // Instructions may have a 32-bit literal encoded after them. Check
7458   // operands that could ever be literals.
7459   if (isVALU(MI) || isSALU(MI)) {
7460     if (isDPP(MI))
7461       return DescSize;
7462     bool HasLiteral = false;
7463     for (int I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) {
7464       const MachineOperand &Op = MI.getOperand(I);
7465       const MCOperandInfo &OpInfo = Desc.OpInfo[I];
7466       if (isLiteralConstantLike(Op, OpInfo)) {
7467         HasLiteral = true;
7468         break;
7469       }
7470     }
7471     return HasLiteral ? DescSize + 4 : DescSize;
7472   }
7473 
7474   // Check whether we have extra NSA words.
7475   if (isMIMG(MI)) {
7476     int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0);
7477     if (VAddr0Idx < 0)
7478       return 8;
7479 
7480     int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc);
7481     return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4);
7482   }
7483 
7484   switch (Opc) {
7485   case TargetOpcode::BUNDLE:
7486     return getInstBundleSize(MI);
7487   case TargetOpcode::INLINEASM:
7488   case TargetOpcode::INLINEASM_BR: {
7489     const MachineFunction *MF = MI.getParent()->getParent();
7490     const char *AsmStr = MI.getOperand(0).getSymbolName();
7491     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(), &ST);
7492   }
7493   default:
7494     if (MI.isMetaInstruction())
7495       return 0;
7496     return DescSize;
7497   }
7498 }
7499 
7500 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const {
7501   if (!isFLAT(MI))
7502     return false;
7503 
7504   if (MI.memoperands_empty())
7505     return true;
7506 
7507   for (const MachineMemOperand *MMO : MI.memoperands()) {
7508     if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS)
7509       return true;
7510   }
7511   return false;
7512 }
7513 
7514 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const {
7515   return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO;
7516 }
7517 
7518 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry,
7519                                             MachineBasicBlock *IfEnd) const {
7520   MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator();
7521   assert(TI != IfEntry->end());
7522 
7523   MachineInstr *Branch = &(*TI);
7524   MachineFunction *MF = IfEntry->getParent();
7525   MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo();
7526 
7527   if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
7528     Register DstReg = MRI.createVirtualRegister(RI.getBoolRC());
7529     MachineInstr *SIIF =
7530         BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg)
7531             .add(Branch->getOperand(0))
7532             .add(Branch->getOperand(1));
7533     MachineInstr *SIEND =
7534         BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF))
7535             .addReg(DstReg);
7536 
7537     IfEntry->erase(TI);
7538     IfEntry->insert(IfEntry->end(), SIIF);
7539     IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND);
7540   }
7541 }
7542 
7543 void SIInstrInfo::convertNonUniformLoopRegion(
7544     MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const {
7545   MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator();
7546   // We expect 2 terminators, one conditional and one unconditional.
7547   assert(TI != LoopEnd->end());
7548 
7549   MachineInstr *Branch = &(*TI);
7550   MachineFunction *MF = LoopEnd->getParent();
7551   MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo();
7552 
7553   if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) {
7554 
7555     Register DstReg = MRI.createVirtualRegister(RI.getBoolRC());
7556     Register BackEdgeReg = MRI.createVirtualRegister(RI.getBoolRC());
7557     MachineInstrBuilder HeaderPHIBuilder =
7558         BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg);
7559     for (MachineBasicBlock *PMBB : LoopEntry->predecessors()) {
7560       if (PMBB == LoopEnd) {
7561         HeaderPHIBuilder.addReg(BackEdgeReg);
7562       } else {
7563         Register ZeroReg = MRI.createVirtualRegister(RI.getBoolRC());
7564         materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(),
7565                              ZeroReg, 0);
7566         HeaderPHIBuilder.addReg(ZeroReg);
7567       }
7568       HeaderPHIBuilder.addMBB(PMBB);
7569     }
7570     MachineInstr *HeaderPhi = HeaderPHIBuilder;
7571     MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(),
7572                                       get(AMDGPU::SI_IF_BREAK), BackEdgeReg)
7573                                   .addReg(DstReg)
7574                                   .add(Branch->getOperand(0));
7575     MachineInstr *SILOOP =
7576         BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP))
7577             .addReg(BackEdgeReg)
7578             .addMBB(LoopEntry);
7579 
7580     LoopEntry->insert(LoopEntry->begin(), HeaderPhi);
7581     LoopEnd->erase(TI);
7582     LoopEnd->insert(LoopEnd->end(), SIIFBREAK);
7583     LoopEnd->insert(LoopEnd->end(), SILOOP);
7584   }
7585 }
7586 
7587 ArrayRef<std::pair<int, const char *>>
7588 SIInstrInfo::getSerializableTargetIndices() const {
7589   static const std::pair<int, const char *> TargetIndices[] = {
7590       {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"},
7591       {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"},
7592       {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"},
7593       {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"},
7594       {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}};
7595   return makeArrayRef(TargetIndices);
7596 }
7597 
7598 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp).  The
7599 /// post-RA version of misched uses CreateTargetMIHazardRecognizer.
7600 ScheduleHazardRecognizer *
7601 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
7602                                             const ScheduleDAG *DAG) const {
7603   return new GCNHazardRecognizer(DAG->MF);
7604 }
7605 
7606 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer
7607 /// pass.
7608 ScheduleHazardRecognizer *
7609 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const {
7610   return new GCNHazardRecognizer(MF);
7611 }
7612 
7613 // Called during:
7614 // - pre-RA scheduling and post-RA scheduling
7615 ScheduleHazardRecognizer *
7616 SIInstrInfo::CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
7617                                             const ScheduleDAGMI *DAG) const {
7618   // Borrowed from Arm Target
7619   // We would like to restrict this hazard recognizer to only
7620   // post-RA scheduling; we can tell that we're post-RA because we don't
7621   // track VRegLiveness.
7622   if (!DAG->hasVRegLiveness())
7623     return new GCNHazardRecognizer(DAG->MF);
7624   return TargetInstrInfo::CreateTargetMIHazardRecognizer(II, DAG);
7625 }
7626 
7627 std::pair<unsigned, unsigned>
7628 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
7629   return std::make_pair(TF & MO_MASK, TF & ~MO_MASK);
7630 }
7631 
7632 ArrayRef<std::pair<unsigned, const char *>>
7633 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
7634   static const std::pair<unsigned, const char *> TargetFlags[] = {
7635     { MO_GOTPCREL, "amdgpu-gotprel" },
7636     { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" },
7637     { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" },
7638     { MO_REL32_LO, "amdgpu-rel32-lo" },
7639     { MO_REL32_HI, "amdgpu-rel32-hi" },
7640     { MO_ABS32_LO, "amdgpu-abs32-lo" },
7641     { MO_ABS32_HI, "amdgpu-abs32-hi" },
7642   };
7643 
7644   return makeArrayRef(TargetFlags);
7645 }
7646 
7647 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
7648 SIInstrInfo::getSerializableMachineMemOperandTargetFlags() const {
7649   static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
7650       {
7651           {MONoClobber, "amdgpu-noclobber"},
7652       };
7653 
7654   return makeArrayRef(TargetFlags);
7655 }
7656 
7657 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI) const {
7658   return !MI.isTerminator() && MI.getOpcode() != AMDGPU::COPY &&
7659          MI.modifiesRegister(AMDGPU::EXEC, &RI);
7660 }
7661 
7662 MachineInstrBuilder
7663 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB,
7664                            MachineBasicBlock::iterator I,
7665                            const DebugLoc &DL,
7666                            Register DestReg) const {
7667   if (ST.hasAddNoCarry())
7668     return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg);
7669 
7670   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
7671   Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC());
7672   MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC());
7673 
7674   return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg)
7675            .addReg(UnusedCarry, RegState::Define | RegState::Dead);
7676 }
7677 
7678 MachineInstrBuilder SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB,
7679                                                MachineBasicBlock::iterator I,
7680                                                const DebugLoc &DL,
7681                                                Register DestReg,
7682                                                RegScavenger &RS) const {
7683   if (ST.hasAddNoCarry())
7684     return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg);
7685 
7686   // If available, prefer to use vcc.
7687   Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC)
7688                              ? Register(RI.getVCC())
7689                              : RS.scavengeRegister(RI.getBoolRC(), I, 0, false);
7690 
7691   // TODO: Users need to deal with this.
7692   if (!UnusedCarry.isValid())
7693     return MachineInstrBuilder();
7694 
7695   return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg)
7696            .addReg(UnusedCarry, RegState::Define | RegState::Dead);
7697 }
7698 
7699 bool SIInstrInfo::isKillTerminator(unsigned Opcode) {
7700   switch (Opcode) {
7701   case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
7702   case AMDGPU::SI_KILL_I1_TERMINATOR:
7703     return true;
7704   default:
7705     return false;
7706   }
7707 }
7708 
7709 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const {
7710   switch (Opcode) {
7711   case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO:
7712     return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR);
7713   case AMDGPU::SI_KILL_I1_PSEUDO:
7714     return get(AMDGPU::SI_KILL_I1_TERMINATOR);
7715   default:
7716     llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO");
7717   }
7718 }
7719 
7720 void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const {
7721   if (!ST.isWave32())
7722     return;
7723 
7724   for (auto &Op : MI.implicit_operands()) {
7725     if (Op.isReg() && Op.getReg() == AMDGPU::VCC)
7726       Op.setReg(AMDGPU::VCC_LO);
7727   }
7728 }
7729 
7730 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const {
7731   if (!isSMRD(MI))
7732     return false;
7733 
7734   // Check that it is using a buffer resource.
7735   int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase);
7736   if (Idx == -1) // e.g. s_memtime
7737     return false;
7738 
7739   const auto RCID = MI.getDesc().OpInfo[Idx].RegClass;
7740   return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass);
7741 }
7742 
7743 // Depending on the used address space and instructions, some immediate offsets
7744 // are allowed and some are not.
7745 // In general, flat instruction offsets can only be non-negative, global and
7746 // scratch instruction offsets can also be negative.
7747 //
7748 // There are several bugs related to these offsets:
7749 // On gfx10.1, flat instructions that go into the global address space cannot
7750 // use an offset.
7751 //
7752 // For scratch instructions, the address can be either an SGPR or a VGPR.
7753 // The following offsets can be used, depending on the architecture (x means
7754 // cannot be used):
7755 // +----------------------------+------+------+
7756 // | Address-Mode               | SGPR | VGPR |
7757 // +----------------------------+------+------+
7758 // | gfx9                       |      |      |
7759 // | negative, 4-aligned offset | x    | ok   |
7760 // | negative, unaligned offset | x    | ok   |
7761 // +----------------------------+------+------+
7762 // | gfx10                      |      |      |
7763 // | negative, 4-aligned offset | ok   | ok   |
7764 // | negative, unaligned offset | ok   | x    |
7765 // +----------------------------+------+------+
7766 // | gfx10.3                    |      |      |
7767 // | negative, 4-aligned offset | ok   | ok   |
7768 // | negative, unaligned offset | ok   | ok   |
7769 // +----------------------------+------+------+
7770 //
7771 // This function ignores the addressing mode, so if an offset cannot be used in
7772 // one addressing mode, it is considered illegal.
7773 bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace,
7774                                     uint64_t FlatVariant) const {
7775   // TODO: Should 0 be special cased?
7776   if (!ST.hasFlatInstOffsets())
7777     return false;
7778 
7779   if (ST.hasFlatSegmentOffsetBug() && FlatVariant == SIInstrFlags::FLAT &&
7780       (AddrSpace == AMDGPUAS::FLAT_ADDRESS ||
7781        AddrSpace == AMDGPUAS::GLOBAL_ADDRESS))
7782     return false;
7783 
7784   bool Signed = FlatVariant != SIInstrFlags::FLAT;
7785   if (ST.hasNegativeScratchOffsetBug() &&
7786       FlatVariant == SIInstrFlags::FlatScratch)
7787     Signed = false;
7788   if (ST.hasNegativeUnalignedScratchOffsetBug() &&
7789       FlatVariant == SIInstrFlags::FlatScratch && Offset < 0 &&
7790       (Offset % 4) != 0) {
7791     return false;
7792   }
7793 
7794   unsigned N = AMDGPU::getNumFlatOffsetBits(ST, Signed);
7795   return Signed ? isIntN(N, Offset) : isUIntN(N, Offset);
7796 }
7797 
7798 // See comment on SIInstrInfo::isLegalFLATOffset for what is legal and what not.
7799 std::pair<int64_t, int64_t>
7800 SIInstrInfo::splitFlatOffset(int64_t COffsetVal, unsigned AddrSpace,
7801                              uint64_t FlatVariant) const {
7802   int64_t RemainderOffset = COffsetVal;
7803   int64_t ImmField = 0;
7804   bool Signed = FlatVariant != SIInstrFlags::FLAT;
7805   if (ST.hasNegativeScratchOffsetBug() &&
7806       FlatVariant == SIInstrFlags::FlatScratch)
7807     Signed = false;
7808 
7809   const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(ST, Signed);
7810   if (Signed) {
7811     // Use signed division by a power of two to truncate towards 0.
7812     int64_t D = 1LL << (NumBits - 1);
7813     RemainderOffset = (COffsetVal / D) * D;
7814     ImmField = COffsetVal - RemainderOffset;
7815 
7816     if (ST.hasNegativeUnalignedScratchOffsetBug() &&
7817         FlatVariant == SIInstrFlags::FlatScratch && ImmField < 0 &&
7818         (ImmField % 4) != 0) {
7819       // Make ImmField a multiple of 4
7820       RemainderOffset += ImmField % 4;
7821       ImmField -= ImmField % 4;
7822     }
7823   } else if (COffsetVal >= 0) {
7824     ImmField = COffsetVal & maskTrailingOnes<uint64_t>(NumBits);
7825     RemainderOffset = COffsetVal - ImmField;
7826   }
7827 
7828   assert(isLegalFLATOffset(ImmField, AddrSpace, FlatVariant));
7829   assert(RemainderOffset + ImmField == COffsetVal);
7830   return {ImmField, RemainderOffset};
7831 }
7832 
7833 // This must be kept in sync with the SIEncodingFamily class in SIInstrInfo.td
7834 enum SIEncodingFamily {
7835   SI = 0,
7836   VI = 1,
7837   SDWA = 2,
7838   SDWA9 = 3,
7839   GFX80 = 4,
7840   GFX9 = 5,
7841   GFX10 = 6,
7842   SDWA10 = 7,
7843   GFX90A = 8,
7844   GFX940 = 9
7845 };
7846 
7847 static SIEncodingFamily subtargetEncodingFamily(const GCNSubtarget &ST) {
7848   switch (ST.getGeneration()) {
7849   default:
7850     break;
7851   case AMDGPUSubtarget::SOUTHERN_ISLANDS:
7852   case AMDGPUSubtarget::SEA_ISLANDS:
7853     return SIEncodingFamily::SI;
7854   case AMDGPUSubtarget::VOLCANIC_ISLANDS:
7855   case AMDGPUSubtarget::GFX9:
7856     return SIEncodingFamily::VI;
7857   case AMDGPUSubtarget::GFX10:
7858     return SIEncodingFamily::GFX10;
7859   }
7860   llvm_unreachable("Unknown subtarget generation!");
7861 }
7862 
7863 bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const {
7864   switch(MCOp) {
7865   // These opcodes use indirect register addressing so
7866   // they need special handling by codegen (currently missing).
7867   // Therefore it is too risky to allow these opcodes
7868   // to be selected by dpp combiner or sdwa peepholer.
7869   case AMDGPU::V_MOVRELS_B32_dpp_gfx10:
7870   case AMDGPU::V_MOVRELS_B32_sdwa_gfx10:
7871   case AMDGPU::V_MOVRELD_B32_dpp_gfx10:
7872   case AMDGPU::V_MOVRELD_B32_sdwa_gfx10:
7873   case AMDGPU::V_MOVRELSD_B32_dpp_gfx10:
7874   case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10:
7875   case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10:
7876   case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10:
7877     return true;
7878   default:
7879     return false;
7880   }
7881 }
7882 
7883 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const {
7884   SIEncodingFamily Gen = subtargetEncodingFamily(ST);
7885 
7886   if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 &&
7887     ST.getGeneration() == AMDGPUSubtarget::GFX9)
7888     Gen = SIEncodingFamily::GFX9;
7889 
7890   // Adjust the encoding family to GFX80 for D16 buffer instructions when the
7891   // subtarget has UnpackedD16VMem feature.
7892   // TODO: remove this when we discard GFX80 encoding.
7893   if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf))
7894     Gen = SIEncodingFamily::GFX80;
7895 
7896   if (get(Opcode).TSFlags & SIInstrFlags::SDWA) {
7897     switch (ST.getGeneration()) {
7898     default:
7899       Gen = SIEncodingFamily::SDWA;
7900       break;
7901     case AMDGPUSubtarget::GFX9:
7902       Gen = SIEncodingFamily::SDWA9;
7903       break;
7904     case AMDGPUSubtarget::GFX10:
7905       Gen = SIEncodingFamily::SDWA10;
7906       break;
7907     }
7908   }
7909 
7910   if (isMAI(Opcode)) {
7911     int MFMAOp = AMDGPU::getMFMAEarlyClobberOp(Opcode);
7912     if (MFMAOp != -1)
7913       Opcode = MFMAOp;
7914   }
7915 
7916   int MCOp = AMDGPU::getMCOpcode(Opcode, Gen);
7917 
7918   // -1 means that Opcode is already a native instruction.
7919   if (MCOp == -1)
7920     return Opcode;
7921 
7922   if (ST.hasGFX90AInsts()) {
7923     uint16_t NMCOp = (uint16_t)-1;
7924     if (ST.hasGFX940Insts())
7925       NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX940);
7926     if (NMCOp == (uint16_t)-1)
7927       NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX90A);
7928     if (NMCOp == (uint16_t)-1)
7929       NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX9);
7930     if (NMCOp != (uint16_t)-1)
7931       MCOp = NMCOp;
7932   }
7933 
7934   // (uint16_t)-1 means that Opcode is a pseudo instruction that has
7935   // no encoding in the given subtarget generation.
7936   if (MCOp == (uint16_t)-1)
7937     return -1;
7938 
7939   if (isAsmOnlyOpcode(MCOp))
7940     return -1;
7941 
7942   return MCOp;
7943 }
7944 
7945 static
7946 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) {
7947   assert(RegOpnd.isReg());
7948   return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() :
7949                              getRegSubRegPair(RegOpnd);
7950 }
7951 
7952 TargetInstrInfo::RegSubRegPair
7953 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) {
7954   assert(MI.isRegSequence());
7955   for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I)
7956     if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) {
7957       auto &RegOp = MI.getOperand(1 + 2 * I);
7958       return getRegOrUndef(RegOp);
7959     }
7960   return TargetInstrInfo::RegSubRegPair();
7961 }
7962 
7963 // Try to find the definition of reg:subreg in subreg-manipulation pseudos
7964 // Following a subreg of reg:subreg isn't supported
7965 static bool followSubRegDef(MachineInstr &MI,
7966                             TargetInstrInfo::RegSubRegPair &RSR) {
7967   if (!RSR.SubReg)
7968     return false;
7969   switch (MI.getOpcode()) {
7970   default: break;
7971   case AMDGPU::REG_SEQUENCE:
7972     RSR = getRegSequenceSubReg(MI, RSR.SubReg);
7973     return true;
7974   // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg
7975   case AMDGPU::INSERT_SUBREG:
7976     if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm())
7977       // inserted the subreg we're looking for
7978       RSR = getRegOrUndef(MI.getOperand(2));
7979     else { // the subreg in the rest of the reg
7980       auto R1 = getRegOrUndef(MI.getOperand(1));
7981       if (R1.SubReg) // subreg of subreg isn't supported
7982         return false;
7983       RSR.Reg = R1.Reg;
7984     }
7985     return true;
7986   }
7987   return false;
7988 }
7989 
7990 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P,
7991                                      MachineRegisterInfo &MRI) {
7992   assert(MRI.isSSA());
7993   if (!P.Reg.isVirtual())
7994     return nullptr;
7995 
7996   auto RSR = P;
7997   auto *DefInst = MRI.getVRegDef(RSR.Reg);
7998   while (auto *MI = DefInst) {
7999     DefInst = nullptr;
8000     switch (MI->getOpcode()) {
8001     case AMDGPU::COPY:
8002     case AMDGPU::V_MOV_B32_e32: {
8003       auto &Op1 = MI->getOperand(1);
8004       if (Op1.isReg() && Op1.getReg().isVirtual()) {
8005         if (Op1.isUndef())
8006           return nullptr;
8007         RSR = getRegSubRegPair(Op1);
8008         DefInst = MRI.getVRegDef(RSR.Reg);
8009       }
8010       break;
8011     }
8012     default:
8013       if (followSubRegDef(*MI, RSR)) {
8014         if (!RSR.Reg)
8015           return nullptr;
8016         DefInst = MRI.getVRegDef(RSR.Reg);
8017       }
8018     }
8019     if (!DefInst)
8020       return MI;
8021   }
8022   return nullptr;
8023 }
8024 
8025 bool llvm::execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI,
8026                                       Register VReg,
8027                                       const MachineInstr &DefMI,
8028                                       const MachineInstr &UseMI) {
8029   assert(MRI.isSSA() && "Must be run on SSA");
8030 
8031   auto *TRI = MRI.getTargetRegisterInfo();
8032   auto *DefBB = DefMI.getParent();
8033 
8034   // Don't bother searching between blocks, although it is possible this block
8035   // doesn't modify exec.
8036   if (UseMI.getParent() != DefBB)
8037     return true;
8038 
8039   const int MaxInstScan = 20;
8040   int NumInst = 0;
8041 
8042   // Stop scan at the use.
8043   auto E = UseMI.getIterator();
8044   for (auto I = std::next(DefMI.getIterator()); I != E; ++I) {
8045     if (I->isDebugInstr())
8046       continue;
8047 
8048     if (++NumInst > MaxInstScan)
8049       return true;
8050 
8051     if (I->modifiesRegister(AMDGPU::EXEC, TRI))
8052       return true;
8053   }
8054 
8055   return false;
8056 }
8057 
8058 bool llvm::execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI,
8059                                          Register VReg,
8060                                          const MachineInstr &DefMI) {
8061   assert(MRI.isSSA() && "Must be run on SSA");
8062 
8063   auto *TRI = MRI.getTargetRegisterInfo();
8064   auto *DefBB = DefMI.getParent();
8065 
8066   const int MaxUseScan = 10;
8067   int NumUse = 0;
8068 
8069   for (auto &Use : MRI.use_nodbg_operands(VReg)) {
8070     auto &UseInst = *Use.getParent();
8071     // Don't bother searching between blocks, although it is possible this block
8072     // doesn't modify exec.
8073     if (UseInst.getParent() != DefBB)
8074       return true;
8075 
8076     if (++NumUse > MaxUseScan)
8077       return true;
8078   }
8079 
8080   if (NumUse == 0)
8081     return false;
8082 
8083   const int MaxInstScan = 20;
8084   int NumInst = 0;
8085 
8086   // Stop scan when we have seen all the uses.
8087   for (auto I = std::next(DefMI.getIterator()); ; ++I) {
8088     assert(I != DefBB->end());
8089 
8090     if (I->isDebugInstr())
8091       continue;
8092 
8093     if (++NumInst > MaxInstScan)
8094       return true;
8095 
8096     for (const MachineOperand &Op : I->operands()) {
8097       // We don't check reg masks here as they're used only on calls:
8098       // 1. EXEC is only considered const within one BB
8099       // 2. Call should be a terminator instruction if present in a BB
8100 
8101       if (!Op.isReg())
8102         continue;
8103 
8104       Register Reg = Op.getReg();
8105       if (Op.isUse()) {
8106         if (Reg == VReg && --NumUse == 0)
8107           return false;
8108       } else if (TRI->regsOverlap(Reg, AMDGPU::EXEC))
8109         return true;
8110     }
8111   }
8112 }
8113 
8114 MachineInstr *SIInstrInfo::createPHIDestinationCopy(
8115     MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt,
8116     const DebugLoc &DL, Register Src, Register Dst) const {
8117   auto Cur = MBB.begin();
8118   if (Cur != MBB.end())
8119     do {
8120       if (!Cur->isPHI() && Cur->readsRegister(Dst))
8121         return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src);
8122       ++Cur;
8123     } while (Cur != MBB.end() && Cur != LastPHIIt);
8124 
8125   return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src,
8126                                                    Dst);
8127 }
8128 
8129 MachineInstr *SIInstrInfo::createPHISourceCopy(
8130     MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt,
8131     const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const {
8132   if (InsPt != MBB.end() &&
8133       (InsPt->getOpcode() == AMDGPU::SI_IF ||
8134        InsPt->getOpcode() == AMDGPU::SI_ELSE ||
8135        InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) &&
8136       InsPt->definesRegister(Src)) {
8137     InsPt++;
8138     return BuildMI(MBB, InsPt, DL,
8139                    get(ST.isWave32() ? AMDGPU::S_MOV_B32_term
8140                                      : AMDGPU::S_MOV_B64_term),
8141                    Dst)
8142         .addReg(Src, 0, SrcSubReg)
8143         .addReg(AMDGPU::EXEC, RegState::Implicit);
8144   }
8145   return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg,
8146                                               Dst);
8147 }
8148 
8149 bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); }
8150 
8151 MachineInstr *SIInstrInfo::foldMemoryOperandImpl(
8152     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
8153     MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
8154     VirtRegMap *VRM) const {
8155   // This is a bit of a hack (copied from AArch64). Consider this instruction:
8156   //
8157   //   %0:sreg_32 = COPY $m0
8158   //
8159   // We explicitly chose SReg_32 for the virtual register so such a copy might
8160   // be eliminated by RegisterCoalescer. However, that may not be possible, and
8161   // %0 may even spill. We can't spill $m0 normally (it would require copying to
8162   // a numbered SGPR anyway), and since it is in the SReg_32 register class,
8163   // TargetInstrInfo::foldMemoryOperand() is going to try.
8164   // A similar issue also exists with spilling and reloading $exec registers.
8165   //
8166   // To prevent that, constrain the %0 register class here.
8167   if (MI.isFullCopy()) {
8168     Register DstReg = MI.getOperand(0).getReg();
8169     Register SrcReg = MI.getOperand(1).getReg();
8170     if ((DstReg.isVirtual() || SrcReg.isVirtual()) &&
8171         (DstReg.isVirtual() != SrcReg.isVirtual())) {
8172       MachineRegisterInfo &MRI = MF.getRegInfo();
8173       Register VirtReg = DstReg.isVirtual() ? DstReg : SrcReg;
8174       const TargetRegisterClass *RC = MRI.getRegClass(VirtReg);
8175       if (RC->hasSuperClassEq(&AMDGPU::SReg_32RegClass)) {
8176         MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_32_XM0_XEXECRegClass);
8177         return nullptr;
8178       } else if (RC->hasSuperClassEq(&AMDGPU::SReg_64RegClass)) {
8179         MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_64_XEXECRegClass);
8180         return nullptr;
8181       }
8182     }
8183   }
8184 
8185   return nullptr;
8186 }
8187 
8188 unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
8189                                       const MachineInstr &MI,
8190                                       unsigned *PredCost) const {
8191   if (MI.isBundle()) {
8192     MachineBasicBlock::const_instr_iterator I(MI.getIterator());
8193     MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end());
8194     unsigned Lat = 0, Count = 0;
8195     for (++I; I != E && I->isBundledWithPred(); ++I) {
8196       ++Count;
8197       Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I));
8198     }
8199     return Lat + Count - 1;
8200   }
8201 
8202   return SchedModel.computeInstrLatency(&MI);
8203 }
8204 
8205 unsigned SIInstrInfo::getDSShaderTypeValue(const MachineFunction &MF) {
8206   switch (MF.getFunction().getCallingConv()) {
8207   case CallingConv::AMDGPU_PS:
8208     return 1;
8209   case CallingConv::AMDGPU_VS:
8210     return 2;
8211   case CallingConv::AMDGPU_GS:
8212     return 3;
8213   case CallingConv::AMDGPU_HS:
8214   case CallingConv::AMDGPU_LS:
8215   case CallingConv::AMDGPU_ES:
8216     report_fatal_error("ds_ordered_count unsupported for this calling conv");
8217   case CallingConv::AMDGPU_CS:
8218   case CallingConv::AMDGPU_KERNEL:
8219   case CallingConv::C:
8220   case CallingConv::Fast:
8221   default:
8222     // Assume other calling conventions are various compute callable functions
8223     return 0;
8224   }
8225 }
8226 
8227 bool SIInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
8228                                  Register &SrcReg2, int64_t &CmpMask,
8229                                  int64_t &CmpValue) const {
8230   if (!MI.getOperand(0).isReg() || MI.getOperand(0).getSubReg())
8231     return false;
8232 
8233   switch (MI.getOpcode()) {
8234   default:
8235     break;
8236   case AMDGPU::S_CMP_EQ_U32:
8237   case AMDGPU::S_CMP_EQ_I32:
8238   case AMDGPU::S_CMP_LG_U32:
8239   case AMDGPU::S_CMP_LG_I32:
8240   case AMDGPU::S_CMP_LT_U32:
8241   case AMDGPU::S_CMP_LT_I32:
8242   case AMDGPU::S_CMP_GT_U32:
8243   case AMDGPU::S_CMP_GT_I32:
8244   case AMDGPU::S_CMP_LE_U32:
8245   case AMDGPU::S_CMP_LE_I32:
8246   case AMDGPU::S_CMP_GE_U32:
8247   case AMDGPU::S_CMP_GE_I32:
8248   case AMDGPU::S_CMP_EQ_U64:
8249   case AMDGPU::S_CMP_LG_U64:
8250     SrcReg = MI.getOperand(0).getReg();
8251     if (MI.getOperand(1).isReg()) {
8252       if (MI.getOperand(1).getSubReg())
8253         return false;
8254       SrcReg2 = MI.getOperand(1).getReg();
8255       CmpValue = 0;
8256     } else if (MI.getOperand(1).isImm()) {
8257       SrcReg2 = Register();
8258       CmpValue = MI.getOperand(1).getImm();
8259     } else {
8260       return false;
8261     }
8262     CmpMask = ~0;
8263     return true;
8264   case AMDGPU::S_CMPK_EQ_U32:
8265   case AMDGPU::S_CMPK_EQ_I32:
8266   case AMDGPU::S_CMPK_LG_U32:
8267   case AMDGPU::S_CMPK_LG_I32:
8268   case AMDGPU::S_CMPK_LT_U32:
8269   case AMDGPU::S_CMPK_LT_I32:
8270   case AMDGPU::S_CMPK_GT_U32:
8271   case AMDGPU::S_CMPK_GT_I32:
8272   case AMDGPU::S_CMPK_LE_U32:
8273   case AMDGPU::S_CMPK_LE_I32:
8274   case AMDGPU::S_CMPK_GE_U32:
8275   case AMDGPU::S_CMPK_GE_I32:
8276     SrcReg = MI.getOperand(0).getReg();
8277     SrcReg2 = Register();
8278     CmpValue = MI.getOperand(1).getImm();
8279     CmpMask = ~0;
8280     return true;
8281   }
8282 
8283   return false;
8284 }
8285 
8286 bool SIInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
8287                                        Register SrcReg2, int64_t CmpMask,
8288                                        int64_t CmpValue,
8289                                        const MachineRegisterInfo *MRI) const {
8290   if (!SrcReg || SrcReg.isPhysical())
8291     return false;
8292 
8293   if (SrcReg2 && !getFoldableImm(SrcReg2, *MRI, CmpValue))
8294     return false;
8295 
8296   const auto optimizeCmpAnd = [&CmpInstr, SrcReg, CmpValue, MRI,
8297                                this](int64_t ExpectedValue, unsigned SrcSize,
8298                                      bool IsReversible, bool IsSigned) -> bool {
8299     // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8300     // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8301     // s_cmp_ge_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8302     // s_cmp_ge_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n
8303     // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 1 << n => s_and_b64 $src, 1 << n
8304     // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8305     // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8306     // s_cmp_gt_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8307     // s_cmp_gt_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n
8308     // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 0 => s_and_b64 $src, 1 << n
8309     //
8310     // Signed ge/gt are not used for the sign bit.
8311     //
8312     // If result of the AND is unused except in the compare:
8313     // s_and_b(32|64) $src, 1 << n => s_bitcmp1_b(32|64) $src, n
8314     //
8315     // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n
8316     // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n
8317     // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 0 => s_bitcmp0_b64 $src, n
8318     // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n
8319     // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n
8320     // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 1 << n => s_bitcmp0_b64 $src, n
8321 
8322     MachineInstr *Def = MRI->getUniqueVRegDef(SrcReg);
8323     if (!Def || Def->getParent() != CmpInstr.getParent())
8324       return false;
8325 
8326     if (Def->getOpcode() != AMDGPU::S_AND_B32 &&
8327         Def->getOpcode() != AMDGPU::S_AND_B64)
8328       return false;
8329 
8330     int64_t Mask;
8331     const auto isMask = [&Mask, SrcSize](const MachineOperand *MO) -> bool {
8332       if (MO->isImm())
8333         Mask = MO->getImm();
8334       else if (!getFoldableImm(MO, Mask))
8335         return false;
8336       Mask &= maxUIntN(SrcSize);
8337       return isPowerOf2_64(Mask);
8338     };
8339 
8340     MachineOperand *SrcOp = &Def->getOperand(1);
8341     if (isMask(SrcOp))
8342       SrcOp = &Def->getOperand(2);
8343     else if (isMask(&Def->getOperand(2)))
8344       SrcOp = &Def->getOperand(1);
8345     else
8346       return false;
8347 
8348     unsigned BitNo = countTrailingZeros((uint64_t)Mask);
8349     if (IsSigned && BitNo == SrcSize - 1)
8350       return false;
8351 
8352     ExpectedValue <<= BitNo;
8353 
8354     bool IsReversedCC = false;
8355     if (CmpValue != ExpectedValue) {
8356       if (!IsReversible)
8357         return false;
8358       IsReversedCC = CmpValue == (ExpectedValue ^ Mask);
8359       if (!IsReversedCC)
8360         return false;
8361     }
8362 
8363     Register DefReg = Def->getOperand(0).getReg();
8364     if (IsReversedCC && !MRI->hasOneNonDBGUse(DefReg))
8365       return false;
8366 
8367     for (auto I = std::next(Def->getIterator()), E = CmpInstr.getIterator();
8368          I != E; ++I) {
8369       if (I->modifiesRegister(AMDGPU::SCC, &RI) ||
8370           I->killsRegister(AMDGPU::SCC, &RI))
8371         return false;
8372     }
8373 
8374     MachineOperand *SccDef = Def->findRegisterDefOperand(AMDGPU::SCC);
8375     SccDef->setIsDead(false);
8376     CmpInstr.eraseFromParent();
8377 
8378     if (!MRI->use_nodbg_empty(DefReg)) {
8379       assert(!IsReversedCC);
8380       return true;
8381     }
8382 
8383     // Replace AND with unused result with a S_BITCMP.
8384     MachineBasicBlock *MBB = Def->getParent();
8385 
8386     unsigned NewOpc = (SrcSize == 32) ? IsReversedCC ? AMDGPU::S_BITCMP0_B32
8387                                                      : AMDGPU::S_BITCMP1_B32
8388                                       : IsReversedCC ? AMDGPU::S_BITCMP0_B64
8389                                                      : AMDGPU::S_BITCMP1_B64;
8390 
8391     BuildMI(*MBB, Def, Def->getDebugLoc(), get(NewOpc))
8392       .add(*SrcOp)
8393       .addImm(BitNo);
8394     Def->eraseFromParent();
8395 
8396     return true;
8397   };
8398 
8399   switch (CmpInstr.getOpcode()) {
8400   default:
8401     break;
8402   case AMDGPU::S_CMP_EQ_U32:
8403   case AMDGPU::S_CMP_EQ_I32:
8404   case AMDGPU::S_CMPK_EQ_U32:
8405   case AMDGPU::S_CMPK_EQ_I32:
8406     return optimizeCmpAnd(1, 32, true, false);
8407   case AMDGPU::S_CMP_GE_U32:
8408   case AMDGPU::S_CMPK_GE_U32:
8409     return optimizeCmpAnd(1, 32, false, false);
8410   case AMDGPU::S_CMP_GE_I32:
8411   case AMDGPU::S_CMPK_GE_I32:
8412     return optimizeCmpAnd(1, 32, false, true);
8413   case AMDGPU::S_CMP_EQ_U64:
8414     return optimizeCmpAnd(1, 64, true, false);
8415   case AMDGPU::S_CMP_LG_U32:
8416   case AMDGPU::S_CMP_LG_I32:
8417   case AMDGPU::S_CMPK_LG_U32:
8418   case AMDGPU::S_CMPK_LG_I32:
8419     return optimizeCmpAnd(0, 32, true, false);
8420   case AMDGPU::S_CMP_GT_U32:
8421   case AMDGPU::S_CMPK_GT_U32:
8422     return optimizeCmpAnd(0, 32, false, false);
8423   case AMDGPU::S_CMP_GT_I32:
8424   case AMDGPU::S_CMPK_GT_I32:
8425     return optimizeCmpAnd(0, 32, false, true);
8426   case AMDGPU::S_CMP_LG_U64:
8427     return optimizeCmpAnd(0, 64, true, false);
8428   }
8429 
8430   return false;
8431 }
8432