R600/SI: Remove explicit VOP operand checking
[oota-llvm.git] / lib / Target / R600 / SIInstrInfo.cpp
1 //===-- SIInstrInfo.cpp - SI Instruction Information  ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief SI Implementation of TargetInstrInfo.
12 //
13 //===----------------------------------------------------------------------===//
14
15
16 #include "SIInstrInfo.h"
17 #include "AMDGPUTargetMachine.h"
18 #include "SIDefines.h"
19 #include "SIMachineFunctionInfo.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/CodeGen/RegisterScavenging.h"
25 #include "llvm/MC/MCInstrDesc.h"
26 #include "llvm/Support/Debug.h"
27
28 using namespace llvm;
29
30 SIInstrInfo::SIInstrInfo(const AMDGPUSubtarget &st)
31     : AMDGPUInstrInfo(st), RI(st) {}
32
33 //===----------------------------------------------------------------------===//
34 // TargetInstrInfo callbacks
35 //===----------------------------------------------------------------------===//
36
37 static unsigned getNumOperandsNoGlue(SDNode *Node) {
38   unsigned N = Node->getNumOperands();
39   while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
40     --N;
41   return N;
42 }
43
44 static SDValue findChainOperand(SDNode *Load) {
45   SDValue LastOp = Load->getOperand(getNumOperandsNoGlue(Load) - 1);
46   assert(LastOp.getValueType() == MVT::Other && "Chain missing from load node");
47   return LastOp;
48 }
49
50 /// \brief Returns true if both nodes have the same value for the given
51 ///        operand \p Op, or if both nodes do not have this operand.
52 static bool nodesHaveSameOperandValue(SDNode *N0, SDNode* N1, unsigned OpName) {
53   unsigned Opc0 = N0->getMachineOpcode();
54   unsigned Opc1 = N1->getMachineOpcode();
55
56   int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName);
57   int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName);
58
59   if (Op0Idx == -1 && Op1Idx == -1)
60     return true;
61
62
63   if ((Op0Idx == -1 && Op1Idx != -1) ||
64       (Op1Idx == -1 && Op0Idx != -1))
65     return false;
66
67   // getNamedOperandIdx returns the index for the MachineInstr's operands,
68   // which includes the result as the first operand. We are indexing into the
69   // MachineSDNode's operands, so we need to skip the result operand to get
70   // the real index.
71   --Op0Idx;
72   --Op1Idx;
73
74   return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx);
75 }
76
77 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1,
78                                           int64_t &Offset0,
79                                           int64_t &Offset1) const {
80   if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode())
81     return false;
82
83   unsigned Opc0 = Load0->getMachineOpcode();
84   unsigned Opc1 = Load1->getMachineOpcode();
85
86   // Make sure both are actually loads.
87   if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad())
88     return false;
89
90   if (isDS(Opc0) && isDS(Opc1)) {
91
92     // FIXME: Handle this case:
93     if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1))
94       return false;
95
96     // Check base reg.
97     if (Load0->getOperand(1) != Load1->getOperand(1))
98       return false;
99
100     // Check chain.
101     if (findChainOperand(Load0) != findChainOperand(Load1))
102       return false;
103
104     // Skip read2 / write2 variants for simplicity.
105     // TODO: We should report true if the used offsets are adjacent (excluded
106     // st64 versions).
107     if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::data1) != -1 ||
108         AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::data1) != -1)
109       return false;
110
111     Offset0 = cast<ConstantSDNode>(Load0->getOperand(2))->getZExtValue();
112     Offset1 = cast<ConstantSDNode>(Load1->getOperand(2))->getZExtValue();
113     return true;
114   }
115
116   if (isSMRD(Opc0) && isSMRD(Opc1)) {
117     assert(getNumOperandsNoGlue(Load0) == getNumOperandsNoGlue(Load1));
118
119     // Check base reg.
120     if (Load0->getOperand(0) != Load1->getOperand(0))
121       return false;
122
123     // Check chain.
124     if (findChainOperand(Load0) != findChainOperand(Load1))
125       return false;
126
127     Offset0 = cast<ConstantSDNode>(Load0->getOperand(1))->getZExtValue();
128     Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getZExtValue();
129     return true;
130   }
131
132   // MUBUF and MTBUF can access the same addresses.
133   if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) {
134
135     // MUBUF and MTBUF have vaddr at different indices.
136     if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) ||
137         findChainOperand(Load0) != findChainOperand(Load1) ||
138         !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) ||
139         !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc))
140       return false;
141
142     int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
143     int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
144
145     if (OffIdx0 == -1 || OffIdx1 == -1)
146       return false;
147
148     // getNamedOperandIdx returns the index for MachineInstrs.  Since they
149     // inlcude the output in the operand list, but SDNodes don't, we need to
150     // subtract the index by one.
151     --OffIdx0;
152     --OffIdx1;
153
154     SDValue Off0 = Load0->getOperand(OffIdx0);
155     SDValue Off1 = Load1->getOperand(OffIdx1);
156
157     // The offset might be a FrameIndexSDNode.
158     if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1))
159       return false;
160
161     Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue();
162     Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue();
163     return true;
164   }
165
166   return false;
167 }
168
169 static bool isStride64(unsigned Opc) {
170   switch (Opc) {
171   case AMDGPU::DS_READ2ST64_B32:
172   case AMDGPU::DS_READ2ST64_B64:
173   case AMDGPU::DS_WRITE2ST64_B32:
174   case AMDGPU::DS_WRITE2ST64_B64:
175     return true;
176   default:
177     return false;
178   }
179 }
180
181 bool SIInstrInfo::getLdStBaseRegImmOfs(MachineInstr *LdSt,
182                                        unsigned &BaseReg, unsigned &Offset,
183                                        const TargetRegisterInfo *TRI) const {
184   unsigned Opc = LdSt->getOpcode();
185   if (isDS(Opc)) {
186     const MachineOperand *OffsetImm = getNamedOperand(*LdSt,
187                                                       AMDGPU::OpName::offset);
188     if (OffsetImm) {
189       // Normal, single offset LDS instruction.
190       const MachineOperand *AddrReg = getNamedOperand(*LdSt,
191                                                       AMDGPU::OpName::addr);
192
193       BaseReg = AddrReg->getReg();
194       Offset = OffsetImm->getImm();
195       return true;
196     }
197
198     // The 2 offset instructions use offset0 and offset1 instead. We can treat
199     // these as a load with a single offset if the 2 offsets are consecutive. We
200     // will use this for some partially aligned loads.
201     const MachineOperand *Offset0Imm = getNamedOperand(*LdSt,
202                                                        AMDGPU::OpName::offset0);
203     const MachineOperand *Offset1Imm = getNamedOperand(*LdSt,
204                                                        AMDGPU::OpName::offset1);
205
206     uint8_t Offset0 = Offset0Imm->getImm();
207     uint8_t Offset1 = Offset1Imm->getImm();
208     assert(Offset1 > Offset0);
209
210     if (Offset1 - Offset0 == 1) {
211       // Each of these offsets is in element sized units, so we need to convert
212       // to bytes of the individual reads.
213
214       unsigned EltSize;
215       if (LdSt->mayLoad())
216         EltSize = getOpRegClass(*LdSt, 0)->getSize() / 2;
217       else {
218         assert(LdSt->mayStore());
219         int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
220         EltSize = getOpRegClass(*LdSt, Data0Idx)->getSize();
221       }
222
223       if (isStride64(Opc))
224         EltSize *= 64;
225
226       const MachineOperand *AddrReg = getNamedOperand(*LdSt,
227                                                       AMDGPU::OpName::addr);
228       BaseReg = AddrReg->getReg();
229       Offset = EltSize * Offset0;
230       return true;
231     }
232
233     return false;
234   }
235
236   if (isMUBUF(Opc) || isMTBUF(Opc)) {
237     if (AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::soffset) != -1)
238       return false;
239
240     const MachineOperand *AddrReg = getNamedOperand(*LdSt,
241                                                     AMDGPU::OpName::vaddr);
242     if (!AddrReg)
243       return false;
244
245     const MachineOperand *OffsetImm = getNamedOperand(*LdSt,
246                                                       AMDGPU::OpName::offset);
247     BaseReg = AddrReg->getReg();
248     Offset = OffsetImm->getImm();
249     return true;
250   }
251
252   if (isSMRD(Opc)) {
253     const MachineOperand *OffsetImm = getNamedOperand(*LdSt,
254                                                       AMDGPU::OpName::offset);
255     if (!OffsetImm)
256       return false;
257
258     const MachineOperand *SBaseReg = getNamedOperand(*LdSt,
259                                                      AMDGPU::OpName::sbase);
260     BaseReg = SBaseReg->getReg();
261     Offset = OffsetImm->getImm();
262     return true;
263   }
264
265   return false;
266 }
267
268 bool SIInstrInfo::shouldClusterLoads(MachineInstr *FirstLdSt,
269                                      MachineInstr *SecondLdSt,
270                                      unsigned NumLoads) const {
271   unsigned Opc0 = FirstLdSt->getOpcode();
272   unsigned Opc1 = SecondLdSt->getOpcode();
273
274   // TODO: This needs finer tuning
275   if (NumLoads > 4)
276     return false;
277
278   if (isDS(Opc0) && isDS(Opc1))
279     return true;
280
281   if (isSMRD(Opc0) && isSMRD(Opc1))
282     return true;
283
284   if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1)))
285     return true;
286
287   return false;
288 }
289
290 void
291 SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
292                          MachineBasicBlock::iterator MI, DebugLoc DL,
293                          unsigned DestReg, unsigned SrcReg,
294                          bool KillSrc) const {
295
296   // If we are trying to copy to or from SCC, there is a bug somewhere else in
297   // the backend.  While it may be theoretically possible to do this, it should
298   // never be necessary.
299   assert(DestReg != AMDGPU::SCC && SrcReg != AMDGPU::SCC);
300
301   static const int16_t Sub0_15[] = {
302     AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
303     AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
304     AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11,
305     AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15, 0
306   };
307
308   static const int16_t Sub0_7[] = {
309     AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
310     AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, 0
311   };
312
313   static const int16_t Sub0_3[] = {
314     AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, 0
315   };
316
317   static const int16_t Sub0_2[] = {
318     AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, 0
319   };
320
321   static const int16_t Sub0_1[] = {
322     AMDGPU::sub0, AMDGPU::sub1, 0
323   };
324
325   unsigned Opcode;
326   const int16_t *SubIndices;
327
328   if (AMDGPU::SReg_32RegClass.contains(DestReg)) {
329     assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
330     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
331             .addReg(SrcReg, getKillRegState(KillSrc));
332     return;
333
334   } else if (AMDGPU::SReg_64RegClass.contains(DestReg)) {
335     if (DestReg == AMDGPU::VCC) {
336       if (AMDGPU::SReg_64RegClass.contains(SrcReg)) {
337         BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC)
338           .addReg(SrcReg, getKillRegState(KillSrc));
339       } else {
340         // FIXME: Hack until VReg_1 removed.
341         assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
342         BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_I32_e32), AMDGPU::VCC)
343           .addImm(0)
344           .addReg(SrcReg, getKillRegState(KillSrc));
345       }
346
347       return;
348     }
349
350     assert(AMDGPU::SReg_64RegClass.contains(SrcReg));
351     BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
352             .addReg(SrcReg, getKillRegState(KillSrc));
353     return;
354
355   } else if (AMDGPU::SReg_128RegClass.contains(DestReg)) {
356     assert(AMDGPU::SReg_128RegClass.contains(SrcReg));
357     Opcode = AMDGPU::S_MOV_B32;
358     SubIndices = Sub0_3;
359
360   } else if (AMDGPU::SReg_256RegClass.contains(DestReg)) {
361     assert(AMDGPU::SReg_256RegClass.contains(SrcReg));
362     Opcode = AMDGPU::S_MOV_B32;
363     SubIndices = Sub0_7;
364
365   } else if (AMDGPU::SReg_512RegClass.contains(DestReg)) {
366     assert(AMDGPU::SReg_512RegClass.contains(SrcReg));
367     Opcode = AMDGPU::S_MOV_B32;
368     SubIndices = Sub0_15;
369
370   } else if (AMDGPU::VGPR_32RegClass.contains(DestReg)) {
371     assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
372            AMDGPU::SReg_32RegClass.contains(SrcReg));
373     BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg)
374             .addReg(SrcReg, getKillRegState(KillSrc));
375     return;
376
377   } else if (AMDGPU::VReg_64RegClass.contains(DestReg)) {
378     assert(AMDGPU::VReg_64RegClass.contains(SrcReg) ||
379            AMDGPU::SReg_64RegClass.contains(SrcReg));
380     Opcode = AMDGPU::V_MOV_B32_e32;
381     SubIndices = Sub0_1;
382
383   } else if (AMDGPU::VReg_96RegClass.contains(DestReg)) {
384     assert(AMDGPU::VReg_96RegClass.contains(SrcReg));
385     Opcode = AMDGPU::V_MOV_B32_e32;
386     SubIndices = Sub0_2;
387
388   } else if (AMDGPU::VReg_128RegClass.contains(DestReg)) {
389     assert(AMDGPU::VReg_128RegClass.contains(SrcReg) ||
390            AMDGPU::SReg_128RegClass.contains(SrcReg));
391     Opcode = AMDGPU::V_MOV_B32_e32;
392     SubIndices = Sub0_3;
393
394   } else if (AMDGPU::VReg_256RegClass.contains(DestReg)) {
395     assert(AMDGPU::VReg_256RegClass.contains(SrcReg) ||
396            AMDGPU::SReg_256RegClass.contains(SrcReg));
397     Opcode = AMDGPU::V_MOV_B32_e32;
398     SubIndices = Sub0_7;
399
400   } else if (AMDGPU::VReg_512RegClass.contains(DestReg)) {
401     assert(AMDGPU::VReg_512RegClass.contains(SrcReg) ||
402            AMDGPU::SReg_512RegClass.contains(SrcReg));
403     Opcode = AMDGPU::V_MOV_B32_e32;
404     SubIndices = Sub0_15;
405
406   } else {
407     llvm_unreachable("Can't copy register!");
408   }
409
410   while (unsigned SubIdx = *SubIndices++) {
411     MachineInstrBuilder Builder = BuildMI(MBB, MI, DL,
412       get(Opcode), RI.getSubReg(DestReg, SubIdx));
413
414     Builder.addReg(RI.getSubReg(SrcReg, SubIdx), getKillRegState(KillSrc));
415
416     if (*SubIndices)
417       Builder.addReg(DestReg, RegState::Define | RegState::Implicit);
418   }
419 }
420
421 unsigned SIInstrInfo::commuteOpcode(unsigned Opcode) const {
422   int NewOpc;
423
424   // Try to map original to commuted opcode
425   NewOpc = AMDGPU::getCommuteRev(Opcode);
426   // Check if the commuted (REV) opcode exists on the target.
427   if (NewOpc != -1 && pseudoToMCOpcode(NewOpc) != -1)
428     return NewOpc;
429
430   // Try to map commuted to original opcode
431   NewOpc = AMDGPU::getCommuteOrig(Opcode);
432   // Check if the original (non-REV) opcode exists on the target.
433   if (NewOpc != -1 && pseudoToMCOpcode(NewOpc) != -1)
434     return NewOpc;
435
436   return Opcode;
437 }
438
439 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const {
440
441   if (DstRC->getSize() == 4) {
442     return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
443   } else if (DstRC->getSize() == 8 && RI.isSGPRClass(DstRC)) {
444     return AMDGPU::S_MOV_B64;
445   } else if (DstRC->getSize() == 8 && !RI.isSGPRClass(DstRC)) {
446     return  AMDGPU::V_MOV_B64_PSEUDO;
447   }
448   return AMDGPU::COPY;
449 }
450
451 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
452                                       MachineBasicBlock::iterator MI,
453                                       unsigned SrcReg, bool isKill,
454                                       int FrameIndex,
455                                       const TargetRegisterClass *RC,
456                                       const TargetRegisterInfo *TRI) const {
457   MachineFunction *MF = MBB.getParent();
458   SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
459   MachineFrameInfo *FrameInfo = MF->getFrameInfo();
460   DebugLoc DL = MBB.findDebugLoc(MI);
461   int Opcode = -1;
462
463   if (RI.isSGPRClass(RC)) {
464     // We are only allowed to create one new instruction when spilling
465     // registers, so we need to use pseudo instruction for spilling
466     // SGPRs.
467     switch (RC->getSize() * 8) {
468       case 32:  Opcode = AMDGPU::SI_SPILL_S32_SAVE;  break;
469       case 64:  Opcode = AMDGPU::SI_SPILL_S64_SAVE;  break;
470       case 128: Opcode = AMDGPU::SI_SPILL_S128_SAVE; break;
471       case 256: Opcode = AMDGPU::SI_SPILL_S256_SAVE; break;
472       case 512: Opcode = AMDGPU::SI_SPILL_S512_SAVE; break;
473     }
474   } else if(RI.hasVGPRs(RC) && ST.isVGPRSpillingEnabled(MFI)) {
475     MFI->setHasSpilledVGPRs();
476
477     switch(RC->getSize() * 8) {
478       case 32: Opcode = AMDGPU::SI_SPILL_V32_SAVE; break;
479       case 64: Opcode = AMDGPU::SI_SPILL_V64_SAVE; break;
480       case 96: Opcode = AMDGPU::SI_SPILL_V96_SAVE; break;
481       case 128: Opcode = AMDGPU::SI_SPILL_V128_SAVE; break;
482       case 256: Opcode = AMDGPU::SI_SPILL_V256_SAVE; break;
483       case 512: Opcode = AMDGPU::SI_SPILL_V512_SAVE; break;
484     }
485   }
486
487   if (Opcode != -1) {
488     FrameInfo->setObjectAlignment(FrameIndex, 4);
489     BuildMI(MBB, MI, DL, get(Opcode))
490             .addReg(SrcReg)
491             .addFrameIndex(FrameIndex)
492             // Place-holder registers, these will be filled in by
493             // SIPrepareScratchRegs.
494             .addReg(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, RegState::Undef)
495             .addReg(AMDGPU::SGPR0, RegState::Undef);
496   } else {
497     LLVMContext &Ctx = MF->getFunction()->getContext();
498     Ctx.emitError("SIInstrInfo::storeRegToStackSlot - Do not know how to"
499                   " spill register");
500     BuildMI(MBB, MI, DL, get(AMDGPU::KILL))
501             .addReg(SrcReg);
502   }
503 }
504
505 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
506                                        MachineBasicBlock::iterator MI,
507                                        unsigned DestReg, int FrameIndex,
508                                        const TargetRegisterClass *RC,
509                                        const TargetRegisterInfo *TRI) const {
510   MachineFunction *MF = MBB.getParent();
511   const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
512   MachineFrameInfo *FrameInfo = MF->getFrameInfo();
513   DebugLoc DL = MBB.findDebugLoc(MI);
514   int Opcode = -1;
515
516   if (RI.isSGPRClass(RC)){
517     switch(RC->getSize() * 8) {
518       case 32:  Opcode = AMDGPU::SI_SPILL_S32_RESTORE; break;
519       case 64:  Opcode = AMDGPU::SI_SPILL_S64_RESTORE;  break;
520       case 128: Opcode = AMDGPU::SI_SPILL_S128_RESTORE; break;
521       case 256: Opcode = AMDGPU::SI_SPILL_S256_RESTORE; break;
522       case 512: Opcode = AMDGPU::SI_SPILL_S512_RESTORE; break;
523     }
524   } else if(RI.hasVGPRs(RC) && ST.isVGPRSpillingEnabled(MFI)) {
525     switch(RC->getSize() * 8) {
526       case 32: Opcode = AMDGPU::SI_SPILL_V32_RESTORE; break;
527       case 64: Opcode = AMDGPU::SI_SPILL_V64_RESTORE; break;
528       case 96: Opcode = AMDGPU::SI_SPILL_V96_RESTORE; break;
529       case 128: Opcode = AMDGPU::SI_SPILL_V128_RESTORE; break;
530       case 256: Opcode = AMDGPU::SI_SPILL_V256_RESTORE; break;
531       case 512: Opcode = AMDGPU::SI_SPILL_V512_RESTORE; break;
532     }
533   }
534
535   if (Opcode != -1) {
536     FrameInfo->setObjectAlignment(FrameIndex, 4);
537     BuildMI(MBB, MI, DL, get(Opcode), DestReg)
538             .addFrameIndex(FrameIndex)
539             // Place-holder registers, these will be filled in by
540             // SIPrepareScratchRegs.
541             .addReg(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, RegState::Undef)
542             .addReg(AMDGPU::SGPR0, RegState::Undef);
543
544   } else {
545     LLVMContext &Ctx = MF->getFunction()->getContext();
546     Ctx.emitError("SIInstrInfo::loadRegFromStackSlot - Do not know how to"
547                   " restore register");
548     BuildMI(MBB, MI, DL, get(AMDGPU::IMPLICIT_DEF), DestReg);
549   }
550 }
551
552 /// \param @Offset Offset in bytes of the FrameIndex being spilled
553 unsigned SIInstrInfo::calculateLDSSpillAddress(MachineBasicBlock &MBB,
554                                                MachineBasicBlock::iterator MI,
555                                                RegScavenger *RS, unsigned TmpReg,
556                                                unsigned FrameOffset,
557                                                unsigned Size) const {
558   MachineFunction *MF = MBB.getParent();
559   SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
560   const AMDGPUSubtarget &ST = MF->getSubtarget<AMDGPUSubtarget>();
561   const SIRegisterInfo *TRI =
562       static_cast<const SIRegisterInfo*>(ST.getRegisterInfo());
563   DebugLoc DL = MBB.findDebugLoc(MI);
564   unsigned WorkGroupSize = MFI->getMaximumWorkGroupSize(*MF);
565   unsigned WavefrontSize = ST.getWavefrontSize();
566
567   unsigned TIDReg = MFI->getTIDReg();
568   if (!MFI->hasCalculatedTID()) {
569     MachineBasicBlock &Entry = MBB.getParent()->front();
570     MachineBasicBlock::iterator Insert = Entry.front();
571     DebugLoc DL = Insert->getDebugLoc();
572
573     TIDReg = RI.findUnusedRegister(MF->getRegInfo(), &AMDGPU::VGPR_32RegClass);
574     if (TIDReg == AMDGPU::NoRegister)
575       return TIDReg;
576
577
578     if (MFI->getShaderType() == ShaderType::COMPUTE &&
579         WorkGroupSize > WavefrontSize) {
580
581       unsigned TIDIGXReg = TRI->getPreloadedValue(*MF, SIRegisterInfo::TIDIG_X);
582       unsigned TIDIGYReg = TRI->getPreloadedValue(*MF, SIRegisterInfo::TIDIG_Y);
583       unsigned TIDIGZReg = TRI->getPreloadedValue(*MF, SIRegisterInfo::TIDIG_Z);
584       unsigned InputPtrReg =
585           TRI->getPreloadedValue(*MF, SIRegisterInfo::INPUT_PTR);
586       static const unsigned TIDIGRegs[3] = {
587         TIDIGXReg, TIDIGYReg, TIDIGZReg
588       };
589       for (unsigned Reg : TIDIGRegs) {
590         if (!Entry.isLiveIn(Reg))
591           Entry.addLiveIn(Reg);
592       }
593
594       RS->enterBasicBlock(&Entry);
595       unsigned STmp0 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0);
596       unsigned STmp1 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0);
597       BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp0)
598               .addReg(InputPtrReg)
599               .addImm(SI::KernelInputOffsets::NGROUPS_Z);
600       BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp1)
601               .addReg(InputPtrReg)
602               .addImm(SI::KernelInputOffsets::NGROUPS_Y);
603
604       // NGROUPS.X * NGROUPS.Y
605       BuildMI(Entry, Insert, DL, get(AMDGPU::S_MUL_I32), STmp1)
606               .addReg(STmp1)
607               .addReg(STmp0);
608       // (NGROUPS.X * NGROUPS.Y) * TIDIG.X
609       BuildMI(Entry, Insert, DL, get(AMDGPU::V_MUL_U32_U24_e32), TIDReg)
610               .addReg(STmp1)
611               .addReg(TIDIGXReg);
612       // NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)
613       BuildMI(Entry, Insert, DL, get(AMDGPU::V_MAD_U32_U24), TIDReg)
614               .addReg(STmp0)
615               .addReg(TIDIGYReg)
616               .addReg(TIDReg);
617       // (NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)) + TIDIG.Z
618       BuildMI(Entry, Insert, DL, get(AMDGPU::V_ADD_I32_e32), TIDReg)
619               .addReg(TIDReg)
620               .addReg(TIDIGZReg);
621     } else {
622       // Get the wave id
623       BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_LO_U32_B32_e64),
624               TIDReg)
625               .addImm(-1)
626               .addImm(0);
627
628       BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_HI_U32_B32_e64),
629               TIDReg)
630               .addImm(-1)
631               .addReg(TIDReg);
632     }
633
634     BuildMI(Entry, Insert, DL, get(AMDGPU::V_LSHLREV_B32_e32),
635             TIDReg)
636             .addImm(2)
637             .addReg(TIDReg);
638     MFI->setTIDReg(TIDReg);
639   }
640
641   // Add FrameIndex to LDS offset
642   unsigned LDSOffset = MFI->LDSSize + (FrameOffset * WorkGroupSize);
643   BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e32), TmpReg)
644           .addImm(LDSOffset)
645           .addReg(TIDReg);
646
647   return TmpReg;
648 }
649
650 void SIInstrInfo::insertNOPs(MachineBasicBlock::iterator MI,
651                              int Count) const {
652   while (Count > 0) {
653     int Arg;
654     if (Count >= 8)
655       Arg = 7;
656     else
657       Arg = Count - 1;
658     Count -= 8;
659     BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(AMDGPU::S_NOP))
660             .addImm(Arg);
661   }
662 }
663
664 bool SIInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
665   MachineBasicBlock &MBB = *MI->getParent();
666   DebugLoc DL = MBB.findDebugLoc(MI);
667   switch (MI->getOpcode()) {
668   default: return AMDGPUInstrInfo::expandPostRAPseudo(MI);
669
670   case AMDGPU::SI_CONSTDATA_PTR: {
671     unsigned Reg = MI->getOperand(0).getReg();
672     unsigned RegLo = RI.getSubReg(Reg, AMDGPU::sub0);
673     unsigned RegHi = RI.getSubReg(Reg, AMDGPU::sub1);
674
675     BuildMI(MBB, MI, DL, get(AMDGPU::S_GETPC_B64), Reg);
676
677     // Add 32-bit offset from this instruction to the start of the constant data.
678     BuildMI(MBB, MI, DL, get(AMDGPU::S_ADD_U32), RegLo)
679             .addReg(RegLo)
680             .addTargetIndex(AMDGPU::TI_CONSTDATA_START)
681             .addReg(AMDGPU::SCC, RegState::Define | RegState::Implicit);
682     BuildMI(MBB, MI, DL, get(AMDGPU::S_ADDC_U32), RegHi)
683             .addReg(RegHi)
684             .addImm(0)
685             .addReg(AMDGPU::SCC, RegState::Define | RegState::Implicit)
686             .addReg(AMDGPU::SCC, RegState::Implicit);
687     MI->eraseFromParent();
688     break;
689   }
690   case AMDGPU::SGPR_USE:
691     // This is just a placeholder for register allocation.
692     MI->eraseFromParent();
693     break;
694
695   case AMDGPU::V_MOV_B64_PSEUDO: {
696     unsigned Dst = MI->getOperand(0).getReg();
697     unsigned DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
698     unsigned DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
699
700     const MachineOperand &SrcOp = MI->getOperand(1);
701     // FIXME: Will this work for 64-bit floating point immediates?
702     assert(!SrcOp.isFPImm());
703     if (SrcOp.isImm()) {
704       APInt Imm(64, SrcOp.getImm());
705       BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
706               .addImm(Imm.getLoBits(32).getZExtValue())
707               .addReg(Dst, RegState::Implicit);
708       BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
709               .addImm(Imm.getHiBits(32).getZExtValue())
710               .addReg(Dst, RegState::Implicit);
711     } else {
712       assert(SrcOp.isReg());
713       BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
714               .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0))
715               .addReg(Dst, RegState::Implicit);
716       BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
717               .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1))
718               .addReg(Dst, RegState::Implicit);
719     }
720     MI->eraseFromParent();
721     break;
722   }
723   }
724   return true;
725 }
726
727 MachineInstr *SIInstrInfo::commuteInstruction(MachineInstr *MI,
728                                               bool NewMI) const {
729
730   if (MI->getNumOperands() < 3)
731     return nullptr;
732
733   int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
734                                            AMDGPU::OpName::src0);
735   assert(Src0Idx != -1 && "Should always have src0 operand");
736
737   MachineOperand &Src0 = MI->getOperand(Src0Idx);
738   if (!Src0.isReg())
739     return nullptr;
740
741   int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
742                                            AMDGPU::OpName::src1);
743   if (Src1Idx == -1)
744     return nullptr;
745
746   MachineOperand &Src1 = MI->getOperand(Src1Idx);
747
748   // Make sure it's legal to commute operands for VOP2.
749   if (isVOP2(MI->getOpcode()) &&
750       (!isOperandLegal(MI, Src0Idx, &Src1) ||
751        !isOperandLegal(MI, Src1Idx, &Src0))) {
752     return nullptr;
753   }
754
755   if (!Src1.isReg()) {
756     // Allow commuting instructions with Imm operands.
757     if (NewMI || !Src1.isImm() ||
758        (!isVOP2(MI->getOpcode()) && !isVOP3(MI->getOpcode()))) {
759       return nullptr;
760     }
761
762     // Be sure to copy the source modifiers to the right place.
763     if (MachineOperand *Src0Mods
764           = getNamedOperand(*MI, AMDGPU::OpName::src0_modifiers)) {
765       MachineOperand *Src1Mods
766         = getNamedOperand(*MI, AMDGPU::OpName::src1_modifiers);
767
768       int Src0ModsVal = Src0Mods->getImm();
769       if (!Src1Mods && Src0ModsVal != 0)
770         return nullptr;
771
772       // XXX - This assert might be a lie. It might be useful to have a neg
773       // modifier with 0.0.
774       int Src1ModsVal = Src1Mods->getImm();
775       assert((Src1ModsVal == 0) && "Not expecting modifiers with immediates");
776
777       Src1Mods->setImm(Src0ModsVal);
778       Src0Mods->setImm(Src1ModsVal);
779     }
780
781     unsigned Reg = Src0.getReg();
782     unsigned SubReg = Src0.getSubReg();
783     if (Src1.isImm())
784       Src0.ChangeToImmediate(Src1.getImm());
785     else
786       llvm_unreachable("Should only have immediates");
787
788     Src1.ChangeToRegister(Reg, false);
789     Src1.setSubReg(SubReg);
790   } else {
791     MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
792   }
793
794   if (MI)
795     MI->setDesc(get(commuteOpcode(MI->getOpcode())));
796
797   return MI;
798 }
799
800 // This needs to be implemented because the source modifiers may be inserted
801 // between the true commutable operands, and the base
802 // TargetInstrInfo::commuteInstruction uses it.
803 bool SIInstrInfo::findCommutedOpIndices(MachineInstr *MI,
804                                         unsigned &SrcOpIdx1,
805                                         unsigned &SrcOpIdx2) const {
806   const MCInstrDesc &MCID = MI->getDesc();
807   if (!MCID.isCommutable())
808     return false;
809
810   unsigned Opc = MI->getOpcode();
811   int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
812   if (Src0Idx == -1)
813     return false;
814
815   // FIXME: Workaround TargetInstrInfo::commuteInstruction asserting on
816   // immediate.
817   if (!MI->getOperand(Src0Idx).isReg())
818     return false;
819
820   int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
821   if (Src1Idx == -1)
822     return false;
823
824   if (!MI->getOperand(Src1Idx).isReg())
825     return false;
826
827   // If any source modifiers are set, the generic instruction commuting won't
828   // understand how to copy the source modifiers.
829   if (hasModifiersSet(*MI, AMDGPU::OpName::src0_modifiers) ||
830       hasModifiersSet(*MI, AMDGPU::OpName::src1_modifiers))
831     return false;
832
833   SrcOpIdx1 = Src0Idx;
834   SrcOpIdx2 = Src1Idx;
835   return true;
836 }
837
838 MachineInstr *SIInstrInfo::buildMovInstr(MachineBasicBlock *MBB,
839                                          MachineBasicBlock::iterator I,
840                                          unsigned DstReg,
841                                          unsigned SrcReg) const {
842   return BuildMI(*MBB, I, MBB->findDebugLoc(I), get(AMDGPU::V_MOV_B32_e32),
843                  DstReg) .addReg(SrcReg);
844 }
845
846 bool SIInstrInfo::isMov(unsigned Opcode) const {
847   switch(Opcode) {
848   default: return false;
849   case AMDGPU::S_MOV_B32:
850   case AMDGPU::S_MOV_B64:
851   case AMDGPU::V_MOV_B32_e32:
852   case AMDGPU::V_MOV_B32_e64:
853     return true;
854   }
855 }
856
857 bool
858 SIInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
859   return RC != &AMDGPU::EXECRegRegClass;
860 }
861
862 bool
863 SIInstrInfo::isTriviallyReMaterializable(const MachineInstr *MI,
864                                          AliasAnalysis *AA) const {
865   switch(MI->getOpcode()) {
866   default: return AMDGPUInstrInfo::isTriviallyReMaterializable(MI, AA);
867   case AMDGPU::S_MOV_B32:
868   case AMDGPU::S_MOV_B64:
869   case AMDGPU::V_MOV_B32_e32:
870     return MI->getOperand(1).isImm();
871   }
872 }
873
874 static bool offsetsDoNotOverlap(int WidthA, int OffsetA,
875                                 int WidthB, int OffsetB) {
876   int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
877   int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
878   int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
879   return LowOffset + LowWidth <= HighOffset;
880 }
881
882 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(MachineInstr *MIa,
883                                                MachineInstr *MIb) const {
884   unsigned BaseReg0, Offset0;
885   unsigned BaseReg1, Offset1;
886
887   if (getLdStBaseRegImmOfs(MIa, BaseReg0, Offset0, &RI) &&
888       getLdStBaseRegImmOfs(MIb, BaseReg1, Offset1, &RI)) {
889     assert(MIa->hasOneMemOperand() && MIb->hasOneMemOperand() &&
890            "read2 / write2 not expected here yet");
891     unsigned Width0 = (*MIa->memoperands_begin())->getSize();
892     unsigned Width1 = (*MIb->memoperands_begin())->getSize();
893     if (BaseReg0 == BaseReg1 &&
894         offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1)) {
895       return true;
896     }
897   }
898
899   return false;
900 }
901
902 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
903                                                   MachineInstr *MIb,
904                                                   AliasAnalysis *AA) const {
905   unsigned Opc0 = MIa->getOpcode();
906   unsigned Opc1 = MIb->getOpcode();
907
908   assert(MIa && (MIa->mayLoad() || MIa->mayStore()) &&
909          "MIa must load from or modify a memory location");
910   assert(MIb && (MIb->mayLoad() || MIb->mayStore()) &&
911          "MIb must load from or modify a memory location");
912
913   if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects())
914     return false;
915
916   // XXX - Can we relax this between address spaces?
917   if (MIa->hasOrderedMemoryRef() || MIb->hasOrderedMemoryRef())
918     return false;
919
920   // TODO: Should we check the address space from the MachineMemOperand? That
921   // would allow us to distinguish objects we know don't alias based on the
922   // underlying addres space, even if it was lowered to a different one,
923   // e.g. private accesses lowered to use MUBUF instructions on a scratch
924   // buffer.
925   if (isDS(Opc0)) {
926     if (isDS(Opc1))
927       return checkInstOffsetsDoNotOverlap(MIa, MIb);
928
929     return !isFLAT(Opc1);
930   }
931
932   if (isMUBUF(Opc0) || isMTBUF(Opc0)) {
933     if (isMUBUF(Opc1) || isMTBUF(Opc1))
934       return checkInstOffsetsDoNotOverlap(MIa, MIb);
935
936     return !isFLAT(Opc1) && !isSMRD(Opc1);
937   }
938
939   if (isSMRD(Opc0)) {
940     if (isSMRD(Opc1))
941       return checkInstOffsetsDoNotOverlap(MIa, MIb);
942
943     return !isFLAT(Opc1) && !isMUBUF(Opc0) && !isMTBUF(Opc0);
944   }
945
946   if (isFLAT(Opc0)) {
947     if (isFLAT(Opc1))
948       return checkInstOffsetsDoNotOverlap(MIa, MIb);
949
950     return false;
951   }
952
953   return false;
954 }
955
956 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const {
957   int64_t SVal = Imm.getSExtValue();
958   if (SVal >= -16 && SVal <= 64)
959     return true;
960
961   if (Imm.getBitWidth() == 64) {
962     uint64_t Val = Imm.getZExtValue();
963     return (DoubleToBits(0.0) == Val) ||
964            (DoubleToBits(1.0) == Val) ||
965            (DoubleToBits(-1.0) == Val) ||
966            (DoubleToBits(0.5) == Val) ||
967            (DoubleToBits(-0.5) == Val) ||
968            (DoubleToBits(2.0) == Val) ||
969            (DoubleToBits(-2.0) == Val) ||
970            (DoubleToBits(4.0) == Val) ||
971            (DoubleToBits(-4.0) == Val);
972   }
973
974   // The actual type of the operand does not seem to matter as long
975   // as the bits match one of the inline immediate values.  For example:
976   //
977   // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
978   // so it is a legal inline immediate.
979   //
980   // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
981   // floating-point, so it is a legal inline immediate.
982   uint32_t Val = Imm.getZExtValue();
983
984   return (FloatToBits(0.0f) == Val) ||
985          (FloatToBits(1.0f) == Val) ||
986          (FloatToBits(-1.0f) == Val) ||
987          (FloatToBits(0.5f) == Val) ||
988          (FloatToBits(-0.5f) == Val) ||
989          (FloatToBits(2.0f) == Val) ||
990          (FloatToBits(-2.0f) == Val) ||
991          (FloatToBits(4.0f) == Val) ||
992          (FloatToBits(-4.0f) == Val);
993 }
994
995 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO,
996                                    unsigned OpSize) const {
997   if (MO.isImm()) {
998     // MachineOperand provides no way to tell the true operand size, since it
999     // only records a 64-bit value. We need to know the size to determine if a
1000     // 32-bit floating point immediate bit pattern is legal for an integer
1001     // immediate. It would be for any 32-bit integer operand, but would not be
1002     // for a 64-bit one.
1003
1004     unsigned BitSize = 8 * OpSize;
1005     return isInlineConstant(APInt(BitSize, MO.getImm(), true));
1006   }
1007
1008   return false;
1009 }
1010
1011 bool SIInstrInfo::isLiteralConstant(const MachineOperand &MO,
1012                                     unsigned OpSize) const {
1013   return MO.isImm() && !isInlineConstant(MO, OpSize);
1014 }
1015
1016 static bool compareMachineOp(const MachineOperand &Op0,
1017                              const MachineOperand &Op1) {
1018   if (Op0.getType() != Op1.getType())
1019     return false;
1020
1021   switch (Op0.getType()) {
1022   case MachineOperand::MO_Register:
1023     return Op0.getReg() == Op1.getReg();
1024   case MachineOperand::MO_Immediate:
1025     return Op0.getImm() == Op1.getImm();
1026   default:
1027     llvm_unreachable("Didn't expect to be comparing these operand types");
1028   }
1029 }
1030
1031 bool SIInstrInfo::isImmOperandLegal(const MachineInstr *MI, unsigned OpNo,
1032                                  const MachineOperand &MO) const {
1033   const MCOperandInfo &OpInfo = get(MI->getOpcode()).OpInfo[OpNo];
1034
1035   assert(MO.isImm() || MO.isTargetIndex() || MO.isFI());
1036
1037   if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE)
1038     return true;
1039
1040   if (OpInfo.RegClass < 0)
1041     return false;
1042
1043   unsigned OpSize = RI.getRegClass(OpInfo.RegClass)->getSize();
1044   if (isLiteralConstant(MO, OpSize))
1045     return RI.opCanUseLiteralConstant(OpInfo.OperandType);
1046
1047   return RI.opCanUseInlineConstant(OpInfo.OperandType);
1048 }
1049
1050 bool SIInstrInfo::canFoldOffset(unsigned OffsetSize, unsigned AS) const {
1051   switch (AS) {
1052   case AMDGPUAS::GLOBAL_ADDRESS: {
1053     // MUBUF instructions a 12-bit offset in bytes.
1054     return isUInt<12>(OffsetSize);
1055   }
1056   case AMDGPUAS::CONSTANT_ADDRESS: {
1057     // SMRD instructions have an 8-bit offset in dwords on SI and
1058     // a 20-bit offset in bytes on VI.
1059     if (RI.ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1060       return isUInt<20>(OffsetSize);
1061     else
1062       return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1063   }
1064   case AMDGPUAS::LOCAL_ADDRESS:
1065   case AMDGPUAS::REGION_ADDRESS: {
1066     // The single offset versions have a 16-bit offset in bytes.
1067     return isUInt<16>(OffsetSize);
1068   }
1069   case AMDGPUAS::PRIVATE_ADDRESS:
1070     // Indirect register addressing does not use any offsets.
1071   default:
1072     return 0;
1073   }
1074 }
1075
1076 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const {
1077   int Op32 = AMDGPU::getVOPe32(Opcode);
1078   if (Op32 == -1)
1079     return false;
1080
1081   return pseudoToMCOpcode(Op32) != -1;
1082 }
1083
1084 bool SIInstrInfo::hasModifiers(unsigned Opcode) const {
1085   // The src0_modifier operand is present on all instructions
1086   // that have modifiers.
1087
1088   return AMDGPU::getNamedOperandIdx(Opcode,
1089                                     AMDGPU::OpName::src0_modifiers) != -1;
1090 }
1091
1092 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI,
1093                                   unsigned OpName) const {
1094   const MachineOperand *Mods = getNamedOperand(MI, OpName);
1095   return Mods && Mods->getImm();
1096 }
1097
1098 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI,
1099                                   const MachineOperand &MO,
1100                                   unsigned OpSize) const {
1101   // Literal constants use the constant bus.
1102   if (isLiteralConstant(MO, OpSize))
1103     return true;
1104
1105   if (!MO.isReg() || !MO.isUse())
1106     return false;
1107
1108   if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1109     return RI.isSGPRClass(MRI.getRegClass(MO.getReg()));
1110
1111   // FLAT_SCR is just an SGPR pair.
1112   if (!MO.isImplicit() && (MO.getReg() == AMDGPU::FLAT_SCR))
1113     return true;
1114
1115   // EXEC register uses the constant bus.
1116   if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC)
1117     return true;
1118
1119   // SGPRs use the constant bus
1120   if (MO.getReg() == AMDGPU::M0 || MO.getReg() == AMDGPU::VCC ||
1121       (!MO.isImplicit() &&
1122       (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) ||
1123        AMDGPU::SGPR_64RegClass.contains(MO.getReg())))) {
1124     return true;
1125   }
1126
1127   return false;
1128 }
1129
1130 bool SIInstrInfo::verifyInstruction(const MachineInstr *MI,
1131                                     StringRef &ErrInfo) const {
1132   uint16_t Opcode = MI->getOpcode();
1133   const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1134   int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
1135   int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
1136   int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
1137
1138   // Make sure the number of operands is correct.
1139   const MCInstrDesc &Desc = get(Opcode);
1140   if (!Desc.isVariadic() &&
1141       Desc.getNumOperands() != MI->getNumExplicitOperands()) {
1142      ErrInfo = "Instruction has wrong number of operands.";
1143      return false;
1144   }
1145
1146   // Make sure the register classes are correct
1147   for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) {
1148     if (MI->getOperand(i).isFPImm()) {
1149       ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast "
1150                 "all fp values to integers.";
1151       return false;
1152     }
1153
1154     switch (Desc.OpInfo[i].OperandType) {
1155     case MCOI::OPERAND_REGISTER:
1156       if (MI->getOperand(i).isImm()) {
1157         ErrInfo = "Illegal immediate value for operand.";
1158         return false;
1159       }
1160       break;
1161     case AMDGPU::OPERAND_REG_IMM32:
1162       break;
1163     case AMDGPU::OPERAND_REG_INLINE_C:
1164       if (MI->getOperand(i).isImm()) {
1165         int RegClass = Desc.OpInfo[i].RegClass;
1166         const TargetRegisterClass *RC = RI.getRegClass(RegClass);
1167         if (!isInlineConstant(MI->getOperand(i), RC->getSize())) {
1168           ErrInfo = "Illegal immediate value for operand.";
1169           return false;
1170         }
1171       }
1172       break;
1173     case MCOI::OPERAND_IMMEDIATE:
1174       // Check if this operand is an immediate.
1175       // FrameIndex operands will be replaced by immediates, so they are
1176       // allowed.
1177       if (!MI->getOperand(i).isImm() && !MI->getOperand(i).isFI()) {
1178         ErrInfo = "Expected immediate, but got non-immediate";
1179         return false;
1180       }
1181       // Fall-through
1182     default:
1183       continue;
1184     }
1185
1186     if (!MI->getOperand(i).isReg())
1187       continue;
1188
1189     int RegClass = Desc.OpInfo[i].RegClass;
1190     if (RegClass != -1) {
1191       unsigned Reg = MI->getOperand(i).getReg();
1192       if (TargetRegisterInfo::isVirtualRegister(Reg))
1193         continue;
1194
1195       const TargetRegisterClass *RC = RI.getRegClass(RegClass);
1196       if (!RC->contains(Reg)) {
1197         ErrInfo = "Operand has incorrect register class.";
1198         return false;
1199       }
1200     }
1201   }
1202
1203
1204   // Verify VOP*
1205   if (isVOP1(Opcode) || isVOP2(Opcode) || isVOP3(Opcode) || isVOPC(Opcode)) {
1206     // Only look at the true operands. Only a real operand can use the constant
1207     // bus, and we don't want to check pseudo-operands like the source modifier
1208     // flags.
1209     const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx };
1210
1211     unsigned ConstantBusCount = 0;
1212     unsigned SGPRUsed = AMDGPU::NoRegister;
1213     for (int OpIdx : OpIndices) {
1214       if (OpIdx == -1)
1215         break;
1216       const MachineOperand &MO = MI->getOperand(OpIdx);
1217       if (usesConstantBus(MRI, MO, getOpSize(Opcode, OpIdx))) {
1218         if (MO.isReg()) {
1219           if (MO.getReg() != SGPRUsed)
1220             ++ConstantBusCount;
1221           SGPRUsed = MO.getReg();
1222         } else {
1223           ++ConstantBusCount;
1224         }
1225       }
1226     }
1227     if (ConstantBusCount > 1) {
1228       ErrInfo = "VOP* instruction uses the constant bus more than once";
1229       return false;
1230     }
1231   }
1232
1233   // Verify misc. restrictions on specific instructions.
1234   if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32 ||
1235       Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64) {
1236     const MachineOperand &Src0 = MI->getOperand(Src0Idx);
1237     const MachineOperand &Src1 = MI->getOperand(Src1Idx);
1238     const MachineOperand &Src2 = MI->getOperand(Src2Idx);
1239     if (Src0.isReg() && Src1.isReg() && Src2.isReg()) {
1240       if (!compareMachineOp(Src0, Src1) &&
1241           !compareMachineOp(Src0, Src2)) {
1242         ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2";
1243         return false;
1244       }
1245     }
1246   }
1247
1248   return true;
1249 }
1250
1251 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) {
1252   switch (MI.getOpcode()) {
1253   default: return AMDGPU::INSTRUCTION_LIST_END;
1254   case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE;
1255   case AMDGPU::COPY: return AMDGPU::COPY;
1256   case AMDGPU::PHI: return AMDGPU::PHI;
1257   case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG;
1258   case AMDGPU::S_MOV_B32:
1259     return MI.getOperand(1).isReg() ?
1260            AMDGPU::COPY : AMDGPU::V_MOV_B32_e32;
1261   case AMDGPU::S_ADD_I32:
1262   case AMDGPU::S_ADD_U32: return AMDGPU::V_ADD_I32_e32;
1263   case AMDGPU::S_ADDC_U32: return AMDGPU::V_ADDC_U32_e32;
1264   case AMDGPU::S_SUB_I32:
1265   case AMDGPU::S_SUB_U32: return AMDGPU::V_SUB_I32_e32;
1266   case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32;
1267   case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_I32;
1268   case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e32;
1269   case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e32;
1270   case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e32;
1271   case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e32;
1272   case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e32;
1273   case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e32;
1274   case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e32;
1275   case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32;
1276   case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64;
1277   case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32;
1278   case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64;
1279   case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32;
1280   case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64;
1281   case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32;
1282   case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32;
1283   case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32;
1284   case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32;
1285   case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32;
1286   case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32;
1287   case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32;
1288   case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32;
1289   case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32;
1290   case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32;
1291   case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32;
1292   case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32;
1293   case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32;
1294   case AMDGPU::S_LOAD_DWORD_IMM:
1295   case AMDGPU::S_LOAD_DWORD_SGPR: return AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1296   case AMDGPU::S_LOAD_DWORDX2_IMM:
1297   case AMDGPU::S_LOAD_DWORDX2_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1298   case AMDGPU::S_LOAD_DWORDX4_IMM:
1299   case AMDGPU::S_LOAD_DWORDX4_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1300   case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64;
1301   case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32;
1302   case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32;
1303   }
1304 }
1305
1306 bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const {
1307   return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END;
1308 }
1309
1310 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI,
1311                                                       unsigned OpNo) const {
1312   const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1313   const MCInstrDesc &Desc = get(MI.getOpcode());
1314   if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
1315       Desc.OpInfo[OpNo].RegClass == -1) {
1316     unsigned Reg = MI.getOperand(OpNo).getReg();
1317
1318     if (TargetRegisterInfo::isVirtualRegister(Reg))
1319       return MRI.getRegClass(Reg);
1320     return RI.getPhysRegClass(Reg);
1321   }
1322
1323   unsigned RCID = Desc.OpInfo[OpNo].RegClass;
1324   return RI.getRegClass(RCID);
1325 }
1326
1327 bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const {
1328   switch (MI.getOpcode()) {
1329   case AMDGPU::COPY:
1330   case AMDGPU::REG_SEQUENCE:
1331   case AMDGPU::PHI:
1332   case AMDGPU::INSERT_SUBREG:
1333     return RI.hasVGPRs(getOpRegClass(MI, 0));
1334   default:
1335     return RI.hasVGPRs(getOpRegClass(MI, OpNo));
1336   }
1337 }
1338
1339 void SIInstrInfo::legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const {
1340   MachineBasicBlock::iterator I = MI;
1341   MachineBasicBlock *MBB = MI->getParent();
1342   MachineOperand &MO = MI->getOperand(OpIdx);
1343   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1344   unsigned RCID = get(MI->getOpcode()).OpInfo[OpIdx].RegClass;
1345   const TargetRegisterClass *RC = RI.getRegClass(RCID);
1346   unsigned Opcode = AMDGPU::V_MOV_B32_e32;
1347   if (MO.isReg())
1348     Opcode = AMDGPU::COPY;
1349   else if (RI.isSGPRClass(RC))
1350     Opcode = AMDGPU::S_MOV_B32;
1351
1352
1353   const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
1354   if (RI.getCommonSubClass(&AMDGPU::VReg_64RegClass, VRC))
1355     VRC = &AMDGPU::VReg_64RegClass;
1356   else
1357     VRC = &AMDGPU::VGPR_32RegClass;
1358
1359   unsigned Reg = MRI.createVirtualRegister(VRC);
1360   DebugLoc DL = MBB->findDebugLoc(I);
1361   BuildMI(*MI->getParent(), I, DL, get(Opcode), Reg)
1362     .addOperand(MO);
1363   MO.ChangeToRegister(Reg, false);
1364 }
1365
1366 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI,
1367                                          MachineRegisterInfo &MRI,
1368                                          MachineOperand &SuperReg,
1369                                          const TargetRegisterClass *SuperRC,
1370                                          unsigned SubIdx,
1371                                          const TargetRegisterClass *SubRC)
1372                                          const {
1373   assert(SuperReg.isReg());
1374
1375   unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC);
1376   unsigned SubReg = MRI.createVirtualRegister(SubRC);
1377
1378   // Just in case the super register is itself a sub-register, copy it to a new
1379   // value so we don't need to worry about merging its subreg index with the
1380   // SubIdx passed to this function. The register coalescer should be able to
1381   // eliminate this extra copy.
1382   MachineBasicBlock *MBB = MI->getParent();
1383   DebugLoc DL = MI->getDebugLoc();
1384
1385   BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg)
1386     .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg());
1387
1388   BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
1389     .addReg(NewSuperReg, 0, SubIdx);
1390
1391   return SubReg;
1392 }
1393
1394 MachineOperand SIInstrInfo::buildExtractSubRegOrImm(
1395   MachineBasicBlock::iterator MII,
1396   MachineRegisterInfo &MRI,
1397   MachineOperand &Op,
1398   const TargetRegisterClass *SuperRC,
1399   unsigned SubIdx,
1400   const TargetRegisterClass *SubRC) const {
1401   if (Op.isImm()) {
1402     // XXX - Is there a better way to do this?
1403     if (SubIdx == AMDGPU::sub0)
1404       return MachineOperand::CreateImm(Op.getImm() & 0xFFFFFFFF);
1405     if (SubIdx == AMDGPU::sub1)
1406       return MachineOperand::CreateImm(Op.getImm() >> 32);
1407
1408     llvm_unreachable("Unhandled register index for immediate");
1409   }
1410
1411   unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
1412                                        SubIdx, SubRC);
1413   return MachineOperand::CreateReg(SubReg, false);
1414 }
1415
1416 unsigned SIInstrInfo::split64BitImm(SmallVectorImpl<MachineInstr *> &Worklist,
1417                                     MachineBasicBlock::iterator MI,
1418                                     MachineRegisterInfo &MRI,
1419                                     const TargetRegisterClass *RC,
1420                                     const MachineOperand &Op) const {
1421   MachineBasicBlock *MBB = MI->getParent();
1422   DebugLoc DL = MI->getDebugLoc();
1423   unsigned LoDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1424   unsigned HiDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1425   unsigned Dst = MRI.createVirtualRegister(RC);
1426
1427   MachineInstr *Lo = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32),
1428                              LoDst)
1429     .addImm(Op.getImm() & 0xFFFFFFFF);
1430   MachineInstr *Hi = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32),
1431                              HiDst)
1432     .addImm(Op.getImm() >> 32);
1433
1434   BuildMI(*MBB, MI, DL, get(TargetOpcode::REG_SEQUENCE), Dst)
1435     .addReg(LoDst)
1436     .addImm(AMDGPU::sub0)
1437     .addReg(HiDst)
1438     .addImm(AMDGPU::sub1);
1439
1440   Worklist.push_back(Lo);
1441   Worklist.push_back(Hi);
1442
1443   return Dst;
1444 }
1445
1446 // Change the order of operands from (0, 1, 2) to (0, 2, 1)
1447 void SIInstrInfo::swapOperands(MachineBasicBlock::iterator Inst) const {
1448   assert(Inst->getNumExplicitOperands() == 3);
1449   MachineOperand Op1 = Inst->getOperand(1);
1450   Inst->RemoveOperand(1);
1451   Inst->addOperand(Op1);
1452 }
1453
1454 bool SIInstrInfo::isOperandLegal(const MachineInstr *MI, unsigned OpIdx,
1455                                  const MachineOperand *MO) const {
1456   const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1457   const MCInstrDesc &InstDesc = get(MI->getOpcode());
1458   const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx];
1459   const TargetRegisterClass *DefinedRC =
1460       OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr;
1461   if (!MO)
1462     MO = &MI->getOperand(OpIdx);
1463
1464   if (isVALU(InstDesc.Opcode) &&
1465       usesConstantBus(MRI, *MO, DefinedRC->getSize())) {
1466     unsigned SGPRUsed =
1467         MO->isReg() ? MO->getReg() : (unsigned)AMDGPU::NoRegister;
1468     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1469       if (i == OpIdx)
1470         continue;
1471       const MachineOperand &Op = MI->getOperand(i);
1472       if (Op.isReg() && Op.getReg() != SGPRUsed &&
1473           usesConstantBus(MRI, Op, getOpSize(*MI, i))) {
1474         return false;
1475       }
1476     }
1477   }
1478
1479   if (MO->isReg()) {
1480     assert(DefinedRC);
1481     const TargetRegisterClass *RC = MRI.getRegClass(MO->getReg());
1482
1483     // In order to be legal, the common sub-class must be equal to the
1484     // class of the current operand.  For example:
1485     //
1486     // v_mov_b32 s0 ; Operand defined as vsrc_32
1487     //              ; RI.getCommonSubClass(s0,vsrc_32) = sgpr ; LEGAL
1488     //
1489     // s_sendmsg 0, s0 ; Operand defined as m0reg
1490     //                 ; RI.getCommonSubClass(s0,m0reg) = m0reg ; NOT LEGAL
1491
1492     return RI.getCommonSubClass(RC, RI.getRegClass(OpInfo.RegClass)) == RC;
1493   }
1494
1495
1496   // Handle non-register types that are treated like immediates.
1497   assert(MO->isImm() || MO->isTargetIndex() || MO->isFI());
1498
1499   if (!DefinedRC) {
1500     // This operand expects an immediate.
1501     return true;
1502   }
1503
1504   return isImmOperandLegal(MI, OpIdx, *MO);
1505 }
1506
1507 void SIInstrInfo::legalizeOperands(MachineInstr *MI) const {
1508   MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1509
1510   int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
1511                                            AMDGPU::OpName::src0);
1512   int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
1513                                            AMDGPU::OpName::src1);
1514   int Src2Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
1515                                            AMDGPU::OpName::src2);
1516
1517   // Legalize VOP2
1518   if (isVOP2(MI->getOpcode()) && Src1Idx != -1) {
1519     // Legalize src0
1520     if (!isOperandLegal(MI, Src0Idx))
1521       legalizeOpWithMove(MI, Src0Idx);
1522
1523     // Legalize src1
1524     if (isOperandLegal(MI, Src1Idx))
1525       return;
1526
1527     // Usually src0 of VOP2 instructions allow more types of inputs
1528     // than src1, so try to commute the instruction to decrease our
1529     // chances of having to insert a MOV instruction to legalize src1.
1530     if (MI->isCommutable()) {
1531       if (commuteInstruction(MI))
1532         // If we are successful in commuting, then we know MI is legal, so
1533         // we are done.
1534         return;
1535     }
1536
1537     legalizeOpWithMove(MI, Src1Idx);
1538     return;
1539   }
1540
1541   // XXX - Do any VOP3 instructions read VCC?
1542   // Legalize VOP3
1543   if (isVOP3(MI->getOpcode())) {
1544     int VOP3Idx[3] = { Src0Idx, Src1Idx, Src2Idx };
1545
1546     // Find the one SGPR operand we are allowed to use.
1547     unsigned SGPRReg = findUsedSGPR(MI, VOP3Idx);
1548
1549     for (unsigned i = 0; i < 3; ++i) {
1550       int Idx = VOP3Idx[i];
1551       if (Idx == -1)
1552         break;
1553       MachineOperand &MO = MI->getOperand(Idx);
1554
1555       if (MO.isReg()) {
1556         if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg())))
1557           continue; // VGPRs are legal
1558
1559         assert(MO.getReg() != AMDGPU::SCC && "SCC operand to VOP3 instruction");
1560
1561         if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) {
1562           SGPRReg = MO.getReg();
1563           // We can use one SGPR in each VOP3 instruction.
1564           continue;
1565         }
1566       } else if (!isLiteralConstant(MO, getOpSize(MI->getOpcode(), Idx))) {
1567         // If it is not a register and not a literal constant, then it must be
1568         // an inline constant which is always legal.
1569         continue;
1570       }
1571       // If we make it this far, then the operand is not legal and we must
1572       // legalize it.
1573       legalizeOpWithMove(MI, Idx);
1574     }
1575   }
1576
1577   // Legalize REG_SEQUENCE and PHI
1578   // The register class of the operands much be the same type as the register
1579   // class of the output.
1580   if (MI->getOpcode() == AMDGPU::REG_SEQUENCE ||
1581       MI->getOpcode() == AMDGPU::PHI) {
1582     const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr;
1583     for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
1584       if (!MI->getOperand(i).isReg() ||
1585           !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
1586         continue;
1587       const TargetRegisterClass *OpRC =
1588               MRI.getRegClass(MI->getOperand(i).getReg());
1589       if (RI.hasVGPRs(OpRC)) {
1590         VRC = OpRC;
1591       } else {
1592         SRC = OpRC;
1593       }
1594     }
1595
1596     // If any of the operands are VGPR registers, then they all most be
1597     // otherwise we will create illegal VGPR->SGPR copies when legalizing
1598     // them.
1599     if (VRC || !RI.isSGPRClass(getOpRegClass(*MI, 0))) {
1600       if (!VRC) {
1601         assert(SRC);
1602         VRC = RI.getEquivalentVGPRClass(SRC);
1603       }
1604       RC = VRC;
1605     } else {
1606       RC = SRC;
1607     }
1608
1609     // Update all the operands so they have the same type.
1610     for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
1611       if (!MI->getOperand(i).isReg() ||
1612           !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
1613         continue;
1614       unsigned DstReg = MRI.createVirtualRegister(RC);
1615       MachineBasicBlock *InsertBB;
1616       MachineBasicBlock::iterator Insert;
1617       if (MI->getOpcode() == AMDGPU::REG_SEQUENCE) {
1618         InsertBB = MI->getParent();
1619         Insert = MI;
1620       } else {
1621         // MI is a PHI instruction.
1622         InsertBB = MI->getOperand(i + 1).getMBB();
1623         Insert = InsertBB->getFirstTerminator();
1624       }
1625       BuildMI(*InsertBB, Insert, MI->getDebugLoc(),
1626               get(AMDGPU::COPY), DstReg)
1627               .addOperand(MI->getOperand(i));
1628       MI->getOperand(i).setReg(DstReg);
1629     }
1630   }
1631
1632   // Legalize INSERT_SUBREG
1633   // src0 must have the same register class as dst
1634   if (MI->getOpcode() == AMDGPU::INSERT_SUBREG) {
1635     unsigned Dst = MI->getOperand(0).getReg();
1636     unsigned Src0 = MI->getOperand(1).getReg();
1637     const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
1638     const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0);
1639     if (DstRC != Src0RC) {
1640       MachineBasicBlock &MBB = *MI->getParent();
1641       unsigned NewSrc0 = MRI.createVirtualRegister(DstRC);
1642       BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::COPY), NewSrc0)
1643               .addReg(Src0);
1644       MI->getOperand(1).setReg(NewSrc0);
1645     }
1646     return;
1647   }
1648
1649   // Legalize MUBUF* instructions
1650   // FIXME: If we start using the non-addr64 instructions for compute, we
1651   // may need to legalize them here.
1652   int SRsrcIdx =
1653       AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::srsrc);
1654   if (SRsrcIdx != -1) {
1655     // We have an MUBUF instruction
1656     MachineOperand *SRsrc = &MI->getOperand(SRsrcIdx);
1657     unsigned SRsrcRC = get(MI->getOpcode()).OpInfo[SRsrcIdx].RegClass;
1658     if (RI.getCommonSubClass(MRI.getRegClass(SRsrc->getReg()),
1659                                              RI.getRegClass(SRsrcRC))) {
1660       // The operands are legal.
1661       // FIXME: We may need to legalize operands besided srsrc.
1662       return;
1663     }
1664
1665     MachineBasicBlock &MBB = *MI->getParent();
1666     // Extract the the ptr from the resource descriptor.
1667
1668     // SRsrcPtrLo = srsrc:sub0
1669     unsigned SRsrcPtrLo = buildExtractSubReg(MI, MRI, *SRsrc,
1670         &AMDGPU::VReg_128RegClass, AMDGPU::sub0, &AMDGPU::VGPR_32RegClass);
1671
1672     // SRsrcPtrHi = srsrc:sub1
1673     unsigned SRsrcPtrHi = buildExtractSubReg(MI, MRI, *SRsrc,
1674         &AMDGPU::VReg_128RegClass, AMDGPU::sub1, &AMDGPU::VGPR_32RegClass);
1675
1676     // Create an empty resource descriptor
1677     unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1678     unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1679     unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1680     unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass);
1681     uint64_t RsrcDataFormat = getDefaultRsrcDataFormat();
1682
1683     // Zero64 = 0
1684     BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B64),
1685             Zero64)
1686             .addImm(0);
1687
1688     // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
1689     BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
1690             SRsrcFormatLo)
1691             .addImm(RsrcDataFormat & 0xFFFFFFFF);
1692
1693     // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
1694     BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
1695             SRsrcFormatHi)
1696             .addImm(RsrcDataFormat >> 32);
1697
1698     // NewSRsrc = {Zero64, SRsrcFormat}
1699     BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
1700             NewSRsrc)
1701             .addReg(Zero64)
1702             .addImm(AMDGPU::sub0_sub1)
1703             .addReg(SRsrcFormatLo)
1704             .addImm(AMDGPU::sub2)
1705             .addReg(SRsrcFormatHi)
1706             .addImm(AMDGPU::sub3);
1707
1708     MachineOperand *VAddr = getNamedOperand(*MI, AMDGPU::OpName::vaddr);
1709     unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
1710     unsigned NewVAddrLo;
1711     unsigned NewVAddrHi;
1712     if (VAddr) {
1713       // This is already an ADDR64 instruction so we need to add the pointer
1714       // extracted from the resource descriptor to the current value of VAddr.
1715       NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1716       NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1717
1718       // NewVaddrLo = SRsrcPtrLo + VAddr:sub0
1719       BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADD_I32_e32),
1720               NewVAddrLo)
1721               .addReg(SRsrcPtrLo)
1722               .addReg(VAddr->getReg(), 0, AMDGPU::sub0)
1723               .addReg(AMDGPU::VCC, RegState::ImplicitDefine);
1724
1725       // NewVaddrHi = SRsrcPtrHi + VAddr:sub1
1726       BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADDC_U32_e32),
1727               NewVAddrHi)
1728               .addReg(SRsrcPtrHi)
1729               .addReg(VAddr->getReg(), 0, AMDGPU::sub1)
1730               .addReg(AMDGPU::VCC, RegState::ImplicitDefine)
1731               .addReg(AMDGPU::VCC, RegState::Implicit);
1732
1733     } else {
1734       // This instructions is the _OFFSET variant, so we need to convert it to
1735       // ADDR64.
1736       MachineOperand *VData = getNamedOperand(*MI, AMDGPU::OpName::vdata);
1737       MachineOperand *Offset = getNamedOperand(*MI, AMDGPU::OpName::offset);
1738       MachineOperand *SOffset = getNamedOperand(*MI, AMDGPU::OpName::soffset);
1739
1740       // Create the new instruction.
1741       unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI->getOpcode());
1742       MachineInstr *Addr64 =
1743           BuildMI(MBB, MI, MI->getDebugLoc(), get(Addr64Opcode))
1744                   .addOperand(*VData)
1745                   .addOperand(*SRsrc)
1746                   .addReg(AMDGPU::NoRegister) // Dummy value for vaddr.
1747                                               // This will be replaced later
1748                                               // with the new value of vaddr.
1749                   .addOperand(*SOffset)
1750                   .addOperand(*Offset);
1751
1752       MI->removeFromParent();
1753       MI = Addr64;
1754
1755       NewVAddrLo = SRsrcPtrLo;
1756       NewVAddrHi = SRsrcPtrHi;
1757       VAddr = getNamedOperand(*MI, AMDGPU::OpName::vaddr);
1758       SRsrc = getNamedOperand(*MI, AMDGPU::OpName::srsrc);
1759     }
1760
1761     // NewVaddr = {NewVaddrHi, NewVaddrLo}
1762     BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
1763             NewVAddr)
1764             .addReg(NewVAddrLo)
1765             .addImm(AMDGPU::sub0)
1766             .addReg(NewVAddrHi)
1767             .addImm(AMDGPU::sub1);
1768
1769
1770     // Update the instruction to use NewVaddr
1771     VAddr->setReg(NewVAddr);
1772     // Update the instruction to use NewSRsrc
1773     SRsrc->setReg(NewSRsrc);
1774   }
1775 }
1776
1777 void SIInstrInfo::splitSMRD(MachineInstr *MI,
1778                             const TargetRegisterClass *HalfRC,
1779                             unsigned HalfImmOp, unsigned HalfSGPROp,
1780                             MachineInstr *&Lo, MachineInstr *&Hi) const {
1781
1782   DebugLoc DL = MI->getDebugLoc();
1783   MachineBasicBlock *MBB = MI->getParent();
1784   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1785   unsigned RegLo = MRI.createVirtualRegister(HalfRC);
1786   unsigned RegHi = MRI.createVirtualRegister(HalfRC);
1787   unsigned HalfSize = HalfRC->getSize();
1788   const MachineOperand *OffOp =
1789       getNamedOperand(*MI, AMDGPU::OpName::offset);
1790   const MachineOperand *SBase = getNamedOperand(*MI, AMDGPU::OpName::sbase);
1791
1792   // The SMRD has an 8-bit offset in dwords on SI and a 20-bit offset in bytes
1793   // on VI.
1794   if (OffOp) {
1795     bool isVI = RI.ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS;
1796     unsigned OffScale = isVI ? 1 : 4;
1797     // Handle the _IMM variant
1798     unsigned LoOffset = OffOp->getImm() * OffScale;
1799     unsigned HiOffset = LoOffset + HalfSize;
1800     Lo = BuildMI(*MBB, MI, DL, get(HalfImmOp), RegLo)
1801                   .addOperand(*SBase)
1802                   .addImm(LoOffset / OffScale);
1803
1804     if (!isUInt<20>(HiOffset) || (!isVI && !isUInt<8>(HiOffset / OffScale))) {
1805       unsigned OffsetSGPR =
1806           MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
1807       BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32), OffsetSGPR)
1808               .addImm(HiOffset); // The offset in register is in bytes.
1809       Hi = BuildMI(*MBB, MI, DL, get(HalfSGPROp), RegHi)
1810                     .addOperand(*SBase)
1811                     .addReg(OffsetSGPR);
1812     } else {
1813       Hi = BuildMI(*MBB, MI, DL, get(HalfImmOp), RegHi)
1814                      .addOperand(*SBase)
1815                      .addImm(HiOffset / OffScale);
1816     }
1817   } else {
1818     // Handle the _SGPR variant
1819     MachineOperand *SOff = getNamedOperand(*MI, AMDGPU::OpName::soff);
1820     Lo = BuildMI(*MBB, MI, DL, get(HalfSGPROp), RegLo)
1821                   .addOperand(*SBase)
1822                   .addOperand(*SOff);
1823     unsigned OffsetSGPR = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
1824     BuildMI(*MBB, MI, DL, get(AMDGPU::S_ADD_I32), OffsetSGPR)
1825             .addOperand(*SOff)
1826             .addImm(HalfSize);
1827     Hi = BuildMI(*MBB, MI, DL, get(HalfSGPROp))
1828                   .addOperand(*SBase)
1829                   .addReg(OffsetSGPR);
1830   }
1831
1832   unsigned SubLo, SubHi;
1833   switch (HalfSize) {
1834     case 4:
1835       SubLo = AMDGPU::sub0;
1836       SubHi = AMDGPU::sub1;
1837       break;
1838     case 8:
1839       SubLo = AMDGPU::sub0_sub1;
1840       SubHi = AMDGPU::sub2_sub3;
1841       break;
1842     case 16:
1843       SubLo = AMDGPU::sub0_sub1_sub2_sub3;
1844       SubHi = AMDGPU::sub4_sub5_sub6_sub7;
1845       break;
1846     case 32:
1847       SubLo = AMDGPU::sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7;
1848       SubHi = AMDGPU::sub8_sub9_sub10_sub11_sub12_sub13_sub14_sub15;
1849       break;
1850     default:
1851       llvm_unreachable("Unhandled HalfSize");
1852   }
1853
1854   BuildMI(*MBB, MI, DL, get(AMDGPU::REG_SEQUENCE))
1855           .addOperand(MI->getOperand(0))
1856           .addReg(RegLo)
1857           .addImm(SubLo)
1858           .addReg(RegHi)
1859           .addImm(SubHi);
1860 }
1861
1862 void SIInstrInfo::moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI) const {
1863   MachineBasicBlock *MBB = MI->getParent();
1864   switch (MI->getOpcode()) {
1865     case AMDGPU::S_LOAD_DWORD_IMM:
1866     case AMDGPU::S_LOAD_DWORD_SGPR:
1867     case AMDGPU::S_LOAD_DWORDX2_IMM:
1868     case AMDGPU::S_LOAD_DWORDX2_SGPR:
1869     case AMDGPU::S_LOAD_DWORDX4_IMM:
1870     case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1871       unsigned NewOpcode = getVALUOp(*MI);
1872       unsigned RegOffset;
1873       unsigned ImmOffset;
1874
1875       if (MI->getOperand(2).isReg()) {
1876         RegOffset = MI->getOperand(2).getReg();
1877         ImmOffset = 0;
1878       } else {
1879         assert(MI->getOperand(2).isImm());
1880         // SMRD instructions take a dword offsets on SI and byte offset on VI
1881         // and MUBUF instructions always take a byte offset.
1882         ImmOffset = MI->getOperand(2).getImm();
1883         if (RI.ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
1884           ImmOffset <<= 2;
1885         RegOffset = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1886
1887         if (isUInt<12>(ImmOffset)) {
1888           BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
1889                   RegOffset)
1890                   .addImm(0);
1891         } else {
1892           BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
1893                   RegOffset)
1894                   .addImm(ImmOffset);
1895           ImmOffset = 0;
1896         }
1897       }
1898
1899       unsigned SRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass);
1900       unsigned DWord0 = RegOffset;
1901       unsigned DWord1 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1902       unsigned DWord2 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1903       unsigned DWord3 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1904       uint64_t RsrcDataFormat = getDefaultRsrcDataFormat();
1905
1906       BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord1)
1907               .addImm(0);
1908       BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord2)
1909               .addImm(RsrcDataFormat & 0xFFFFFFFF);
1910       BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord3)
1911               .addImm(RsrcDataFormat >> 32);
1912       BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), SRsrc)
1913               .addReg(DWord0)
1914               .addImm(AMDGPU::sub0)
1915               .addReg(DWord1)
1916               .addImm(AMDGPU::sub1)
1917               .addReg(DWord2)
1918               .addImm(AMDGPU::sub2)
1919               .addReg(DWord3)
1920               .addImm(AMDGPU::sub3);
1921       MI->setDesc(get(NewOpcode));
1922       if (MI->getOperand(2).isReg()) {
1923         MI->getOperand(2).setReg(MI->getOperand(1).getReg());
1924       } else {
1925         MI->getOperand(2).ChangeToRegister(MI->getOperand(1).getReg(), false);
1926       }
1927       MI->getOperand(1).setReg(SRsrc);
1928       MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(0));
1929       MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(ImmOffset));
1930
1931       const TargetRegisterClass *NewDstRC =
1932           RI.getRegClass(get(NewOpcode).OpInfo[0].RegClass);
1933
1934       unsigned DstReg = MI->getOperand(0).getReg();
1935       unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC);
1936       MRI.replaceRegWith(DstReg, NewDstReg);
1937       break;
1938     }
1939     case AMDGPU::S_LOAD_DWORDX8_IMM:
1940     case AMDGPU::S_LOAD_DWORDX8_SGPR: {
1941       MachineInstr *Lo, *Hi;
1942       splitSMRD(MI, &AMDGPU::SReg_128RegClass, AMDGPU::S_LOAD_DWORDX4_IMM,
1943                 AMDGPU::S_LOAD_DWORDX4_SGPR, Lo, Hi);
1944       MI->eraseFromParent();
1945       moveSMRDToVALU(Lo, MRI);
1946       moveSMRDToVALU(Hi, MRI);
1947       break;
1948     }
1949
1950     case AMDGPU::S_LOAD_DWORDX16_IMM:
1951     case AMDGPU::S_LOAD_DWORDX16_SGPR: {
1952       MachineInstr *Lo, *Hi;
1953       splitSMRD(MI, &AMDGPU::SReg_256RegClass, AMDGPU::S_LOAD_DWORDX8_IMM,
1954                 AMDGPU::S_LOAD_DWORDX8_SGPR, Lo, Hi);
1955       MI->eraseFromParent();
1956       moveSMRDToVALU(Lo, MRI);
1957       moveSMRDToVALU(Hi, MRI);
1958       break;
1959     }
1960   }
1961 }
1962
1963 void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const {
1964   SmallVector<MachineInstr *, 128> Worklist;
1965   Worklist.push_back(&TopInst);
1966
1967   while (!Worklist.empty()) {
1968     MachineInstr *Inst = Worklist.pop_back_val();
1969     MachineBasicBlock *MBB = Inst->getParent();
1970     MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1971
1972     unsigned Opcode = Inst->getOpcode();
1973     unsigned NewOpcode = getVALUOp(*Inst);
1974
1975     // Handle some special cases
1976     switch (Opcode) {
1977     default:
1978       if (isSMRD(Inst->getOpcode())) {
1979         moveSMRDToVALU(Inst, MRI);
1980       }
1981       break;
1982     case AMDGPU::S_MOV_B64: {
1983       DebugLoc DL = Inst->getDebugLoc();
1984
1985       // If the source operand is a register we can replace this with a
1986       // copy.
1987       if (Inst->getOperand(1).isReg()) {
1988         MachineInstr *Copy = BuildMI(*MBB, Inst, DL, get(TargetOpcode::COPY))
1989           .addOperand(Inst->getOperand(0))
1990           .addOperand(Inst->getOperand(1));
1991         Worklist.push_back(Copy);
1992       } else {
1993         // Otherwise, we need to split this into two movs, because there is
1994         // no 64-bit VALU move instruction.
1995         unsigned Reg = Inst->getOperand(0).getReg();
1996         unsigned Dst = split64BitImm(Worklist,
1997                                      Inst,
1998                                      MRI,
1999                                      MRI.getRegClass(Reg),
2000                                      Inst->getOperand(1));
2001         MRI.replaceRegWith(Reg, Dst);
2002       }
2003       Inst->eraseFromParent();
2004       continue;
2005     }
2006     case AMDGPU::S_AND_B64:
2007       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32);
2008       Inst->eraseFromParent();
2009       continue;
2010
2011     case AMDGPU::S_OR_B64:
2012       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32);
2013       Inst->eraseFromParent();
2014       continue;
2015
2016     case AMDGPU::S_XOR_B64:
2017       splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32);
2018       Inst->eraseFromParent();
2019       continue;
2020
2021     case AMDGPU::S_NOT_B64:
2022       splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32);
2023       Inst->eraseFromParent();
2024       continue;
2025
2026     case AMDGPU::S_BCNT1_I32_B64:
2027       splitScalar64BitBCNT(Worklist, Inst);
2028       Inst->eraseFromParent();
2029       continue;
2030
2031     case AMDGPU::S_BFE_I64: {
2032       splitScalar64BitBFE(Worklist, Inst);
2033       Inst->eraseFromParent();
2034       continue;
2035     }
2036
2037     case AMDGPU::S_LSHL_B32:
2038       if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2039         NewOpcode = AMDGPU::V_LSHLREV_B32_e64;
2040         swapOperands(Inst);
2041       }
2042       break;
2043     case AMDGPU::S_ASHR_I32:
2044       if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2045         NewOpcode = AMDGPU::V_ASHRREV_I32_e64;
2046         swapOperands(Inst);
2047       }
2048       break;
2049     case AMDGPU::S_LSHR_B32:
2050       if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2051         NewOpcode = AMDGPU::V_LSHRREV_B32_e64;
2052         swapOperands(Inst);
2053       }
2054       break;
2055     case AMDGPU::S_LSHL_B64:
2056       if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2057         NewOpcode = AMDGPU::V_LSHLREV_B64;
2058         swapOperands(Inst);
2059       }
2060       break;
2061     case AMDGPU::S_ASHR_I64:
2062       if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2063         NewOpcode = AMDGPU::V_ASHRREV_I64;
2064         swapOperands(Inst);
2065       }
2066       break;
2067     case AMDGPU::S_LSHR_B64:
2068       if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2069         NewOpcode = AMDGPU::V_LSHRREV_B64;
2070         swapOperands(Inst);
2071       }
2072       break;
2073
2074     case AMDGPU::S_BFE_U64:
2075     case AMDGPU::S_BFM_B64:
2076       llvm_unreachable("Moving this op to VALU not implemented");
2077     }
2078
2079     if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
2080       // We cannot move this instruction to the VALU, so we should try to
2081       // legalize its operands instead.
2082       legalizeOperands(Inst);
2083       continue;
2084     }
2085
2086     // Use the new VALU Opcode.
2087     const MCInstrDesc &NewDesc = get(NewOpcode);
2088     Inst->setDesc(NewDesc);
2089
2090     // Remove any references to SCC. Vector instructions can't read from it, and
2091     // We're just about to add the implicit use / defs of VCC, and we don't want
2092     // both.
2093     for (unsigned i = Inst->getNumOperands() - 1; i > 0; --i) {
2094       MachineOperand &Op = Inst->getOperand(i);
2095       if (Op.isReg() && Op.getReg() == AMDGPU::SCC)
2096         Inst->RemoveOperand(i);
2097     }
2098
2099     if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) {
2100       // We are converting these to a BFE, so we need to add the missing
2101       // operands for the size and offset.
2102       unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16;
2103       Inst->addOperand(MachineOperand::CreateImm(0));
2104       Inst->addOperand(MachineOperand::CreateImm(Size));
2105
2106     } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) {
2107       // The VALU version adds the second operand to the result, so insert an
2108       // extra 0 operand.
2109       Inst->addOperand(MachineOperand::CreateImm(0));
2110     }
2111
2112     addDescImplicitUseDef(NewDesc, Inst);
2113
2114     if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) {
2115       const MachineOperand &OffsetWidthOp = Inst->getOperand(2);
2116       // If we need to move this to VGPRs, we need to unpack the second operand
2117       // back into the 2 separate ones for bit offset and width.
2118       assert(OffsetWidthOp.isImm() &&
2119              "Scalar BFE is only implemented for constant width and offset");
2120       uint32_t Imm = OffsetWidthOp.getImm();
2121
2122       uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
2123       uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
2124       Inst->RemoveOperand(2); // Remove old immediate.
2125       Inst->addOperand(MachineOperand::CreateImm(Offset));
2126       Inst->addOperand(MachineOperand::CreateImm(BitWidth));
2127     }
2128
2129     // Update the destination register class.
2130
2131     const TargetRegisterClass *NewDstRC = getOpRegClass(*Inst, 0);
2132
2133     switch (Opcode) {
2134       // For target instructions, getOpRegClass just returns the virtual
2135       // register class associated with the operand, so we need to find an
2136       // equivalent VGPR register class in order to move the instruction to the
2137       // VALU.
2138     case AMDGPU::COPY:
2139     case AMDGPU::PHI:
2140     case AMDGPU::REG_SEQUENCE:
2141     case AMDGPU::INSERT_SUBREG:
2142       if (RI.hasVGPRs(NewDstRC))
2143         continue;
2144       NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
2145       if (!NewDstRC)
2146         continue;
2147       break;
2148     default:
2149       break;
2150     }
2151
2152     unsigned DstReg = Inst->getOperand(0).getReg();
2153     unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC);
2154     MRI.replaceRegWith(DstReg, NewDstReg);
2155
2156     // Legalize the operands
2157     legalizeOperands(Inst);
2158
2159     for (MachineRegisterInfo::use_iterator I = MRI.use_begin(NewDstReg),
2160            E = MRI.use_end(); I != E; ++I) {
2161       MachineInstr &UseMI = *I->getParent();
2162       if (!canReadVGPR(UseMI, I.getOperandNo())) {
2163         Worklist.push_back(&UseMI);
2164       }
2165     }
2166   }
2167 }
2168
2169 //===----------------------------------------------------------------------===//
2170 // Indirect addressing callbacks
2171 //===----------------------------------------------------------------------===//
2172
2173 unsigned SIInstrInfo::calculateIndirectAddress(unsigned RegIndex,
2174                                                  unsigned Channel) const {
2175   assert(Channel == 0);
2176   return RegIndex;
2177 }
2178
2179 const TargetRegisterClass *SIInstrInfo::getIndirectAddrRegClass() const {
2180   return &AMDGPU::VGPR_32RegClass;
2181 }
2182
2183 void SIInstrInfo::splitScalar64BitUnaryOp(
2184   SmallVectorImpl<MachineInstr *> &Worklist,
2185   MachineInstr *Inst,
2186   unsigned Opcode) const {
2187   MachineBasicBlock &MBB = *Inst->getParent();
2188   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2189
2190   MachineOperand &Dest = Inst->getOperand(0);
2191   MachineOperand &Src0 = Inst->getOperand(1);
2192   DebugLoc DL = Inst->getDebugLoc();
2193
2194   MachineBasicBlock::iterator MII = Inst;
2195
2196   const MCInstrDesc &InstDesc = get(Opcode);
2197   const TargetRegisterClass *Src0RC = Src0.isReg() ?
2198     MRI.getRegClass(Src0.getReg()) :
2199     &AMDGPU::SGPR_32RegClass;
2200
2201   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
2202
2203   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2204                                                        AMDGPU::sub0, Src0SubRC);
2205
2206   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
2207   const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0);
2208
2209   unsigned DestSub0 = MRI.createVirtualRegister(DestRC);
2210   MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0)
2211     .addOperand(SrcReg0Sub0);
2212
2213   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2214                                                        AMDGPU::sub1, Src0SubRC);
2215
2216   unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC);
2217   MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1)
2218     .addOperand(SrcReg0Sub1);
2219
2220   unsigned FullDestReg = MRI.createVirtualRegister(DestRC);
2221   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
2222     .addReg(DestSub0)
2223     .addImm(AMDGPU::sub0)
2224     .addReg(DestSub1)
2225     .addImm(AMDGPU::sub1);
2226
2227   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
2228
2229   // Try to legalize the operands in case we need to swap the order to keep it
2230   // valid.
2231   Worklist.push_back(LoHalf);
2232   Worklist.push_back(HiHalf);
2233 }
2234
2235 void SIInstrInfo::splitScalar64BitBinaryOp(
2236   SmallVectorImpl<MachineInstr *> &Worklist,
2237   MachineInstr *Inst,
2238   unsigned Opcode) const {
2239   MachineBasicBlock &MBB = *Inst->getParent();
2240   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2241
2242   MachineOperand &Dest = Inst->getOperand(0);
2243   MachineOperand &Src0 = Inst->getOperand(1);
2244   MachineOperand &Src1 = Inst->getOperand(2);
2245   DebugLoc DL = Inst->getDebugLoc();
2246
2247   MachineBasicBlock::iterator MII = Inst;
2248
2249   const MCInstrDesc &InstDesc = get(Opcode);
2250   const TargetRegisterClass *Src0RC = Src0.isReg() ?
2251     MRI.getRegClass(Src0.getReg()) :
2252     &AMDGPU::SGPR_32RegClass;
2253
2254   const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
2255   const TargetRegisterClass *Src1RC = Src1.isReg() ?
2256     MRI.getRegClass(Src1.getReg()) :
2257     &AMDGPU::SGPR_32RegClass;
2258
2259   const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
2260
2261   MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2262                                                        AMDGPU::sub0, Src0SubRC);
2263   MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
2264                                                        AMDGPU::sub0, Src1SubRC);
2265
2266   const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
2267   const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0);
2268
2269   unsigned DestSub0 = MRI.createVirtualRegister(DestRC);
2270   MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0)
2271     .addOperand(SrcReg0Sub0)
2272     .addOperand(SrcReg1Sub0);
2273
2274   MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2275                                                        AMDGPU::sub1, Src0SubRC);
2276   MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
2277                                                        AMDGPU::sub1, Src1SubRC);
2278
2279   unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC);
2280   MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1)
2281     .addOperand(SrcReg0Sub1)
2282     .addOperand(SrcReg1Sub1);
2283
2284   unsigned FullDestReg = MRI.createVirtualRegister(DestRC);
2285   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
2286     .addReg(DestSub0)
2287     .addImm(AMDGPU::sub0)
2288     .addReg(DestSub1)
2289     .addImm(AMDGPU::sub1);
2290
2291   MRI.replaceRegWith(Dest.getReg(), FullDestReg);
2292
2293   // Try to legalize the operands in case we need to swap the order to keep it
2294   // valid.
2295   Worklist.push_back(LoHalf);
2296   Worklist.push_back(HiHalf);
2297 }
2298
2299 void SIInstrInfo::splitScalar64BitBCNT(SmallVectorImpl<MachineInstr *> &Worklist,
2300                                        MachineInstr *Inst) const {
2301   MachineBasicBlock &MBB = *Inst->getParent();
2302   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2303
2304   MachineBasicBlock::iterator MII = Inst;
2305   DebugLoc DL = Inst->getDebugLoc();
2306
2307   MachineOperand &Dest = Inst->getOperand(0);
2308   MachineOperand &Src = Inst->getOperand(1);
2309
2310   const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64);
2311   const TargetRegisterClass *SrcRC = Src.isReg() ?
2312     MRI.getRegClass(Src.getReg()) :
2313     &AMDGPU::SGPR_32RegClass;
2314
2315   unsigned MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2316   unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2317
2318   const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0);
2319
2320   MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
2321                                                       AMDGPU::sub0, SrcSubRC);
2322   MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
2323                                                       AMDGPU::sub1, SrcSubRC);
2324
2325   MachineInstr *First = BuildMI(MBB, MII, DL, InstDesc, MidReg)
2326     .addOperand(SrcRegSub0)
2327     .addImm(0);
2328
2329   MachineInstr *Second = BuildMI(MBB, MII, DL, InstDesc, ResultReg)
2330     .addOperand(SrcRegSub1)
2331     .addReg(MidReg);
2332
2333   MRI.replaceRegWith(Dest.getReg(), ResultReg);
2334
2335   Worklist.push_back(First);
2336   Worklist.push_back(Second);
2337 }
2338
2339 void SIInstrInfo::splitScalar64BitBFE(SmallVectorImpl<MachineInstr *> &Worklist,
2340                                       MachineInstr *Inst) const {
2341   MachineBasicBlock &MBB = *Inst->getParent();
2342   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2343   MachineBasicBlock::iterator MII = Inst;
2344   DebugLoc DL = Inst->getDebugLoc();
2345
2346   MachineOperand &Dest = Inst->getOperand(0);
2347   uint32_t Imm = Inst->getOperand(2).getImm();
2348   uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
2349   uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
2350
2351   (void) Offset;
2352
2353   // Only sext_inreg cases handled.
2354   assert(Inst->getOpcode() == AMDGPU::S_BFE_I64 &&
2355          BitWidth <= 32 &&
2356          Offset == 0 &&
2357          "Not implemented");
2358
2359   if (BitWidth < 32) {
2360     unsigned MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2361     unsigned MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2362     unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
2363
2364     BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32), MidRegLo)
2365       .addReg(Inst->getOperand(1).getReg(), 0, AMDGPU::sub0)
2366       .addImm(0)
2367       .addImm(BitWidth);
2368
2369     BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi)
2370       .addImm(31)
2371       .addReg(MidRegLo);
2372
2373     BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
2374       .addReg(MidRegLo)
2375       .addImm(AMDGPU::sub0)
2376       .addReg(MidRegHi)
2377       .addImm(AMDGPU::sub1);
2378
2379     MRI.replaceRegWith(Dest.getReg(), ResultReg);
2380     return;
2381   }
2382
2383   MachineOperand &Src = Inst->getOperand(1);
2384   unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2385   unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
2386
2387   BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg)
2388     .addImm(31)
2389     .addReg(Src.getReg(), 0, AMDGPU::sub0);
2390
2391   BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
2392     .addReg(Src.getReg(), 0, AMDGPU::sub0)
2393     .addImm(AMDGPU::sub0)
2394     .addReg(TmpReg)
2395     .addImm(AMDGPU::sub1);
2396
2397   MRI.replaceRegWith(Dest.getReg(), ResultReg);
2398 }
2399
2400 void SIInstrInfo::addDescImplicitUseDef(const MCInstrDesc &NewDesc,
2401                                         MachineInstr *Inst) const {
2402   // Add the implict and explicit register definitions.
2403   if (NewDesc.ImplicitUses) {
2404     for (unsigned i = 0; NewDesc.ImplicitUses[i]; ++i) {
2405       unsigned Reg = NewDesc.ImplicitUses[i];
2406       Inst->addOperand(MachineOperand::CreateReg(Reg, false, true));
2407     }
2408   }
2409
2410   if (NewDesc.ImplicitDefs) {
2411     for (unsigned i = 0; NewDesc.ImplicitDefs[i]; ++i) {
2412       unsigned Reg = NewDesc.ImplicitDefs[i];
2413       Inst->addOperand(MachineOperand::CreateReg(Reg, true, true));
2414     }
2415   }
2416 }
2417
2418 unsigned SIInstrInfo::findUsedSGPR(const MachineInstr *MI,
2419                                    int OpIndices[3]) const {
2420   const MCInstrDesc &Desc = get(MI->getOpcode());
2421
2422   // Find the one SGPR operand we are allowed to use.
2423   unsigned SGPRReg = AMDGPU::NoRegister;
2424
2425   // First we need to consider the instruction's operand requirements before
2426   // legalizing. Some operands are required to be SGPRs, such as implicit uses
2427   // of VCC, but we are still bound by the constant bus requirement to only use
2428   // one.
2429   //
2430   // If the operand's class is an SGPR, we can never move it.
2431
2432   for (const MachineOperand &MO : MI->implicit_operands()) {
2433     // We only care about reads.
2434     if (MO.isDef())
2435       continue;
2436
2437     if (MO.getReg() == AMDGPU::VCC)
2438       return AMDGPU::VCC;
2439
2440     if (MO.getReg() == AMDGPU::FLAT_SCR)
2441       return AMDGPU::FLAT_SCR;
2442   }
2443
2444   unsigned UsedSGPRs[3] = { AMDGPU::NoRegister };
2445   const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
2446
2447   for (unsigned i = 0; i < 3; ++i) {
2448     int Idx = OpIndices[i];
2449     if (Idx == -1)
2450       break;
2451
2452     const MachineOperand &MO = MI->getOperand(Idx);
2453     if (RI.isSGPRClassID(Desc.OpInfo[Idx].RegClass))
2454       SGPRReg = MO.getReg();
2455
2456     if (MO.isReg() && RI.isSGPRClass(MRI.getRegClass(MO.getReg())))
2457       UsedSGPRs[i] = MO.getReg();
2458   }
2459
2460   if (SGPRReg != AMDGPU::NoRegister)
2461     return SGPRReg;
2462
2463   // We don't have a required SGPR operand, so we have a bit more freedom in
2464   // selecting operands to move.
2465
2466   // Try to select the most used SGPR. If an SGPR is equal to one of the
2467   // others, we choose that.
2468   //
2469   // e.g.
2470   // V_FMA_F32 v0, s0, s0, s0 -> No moves
2471   // V_FMA_F32 v0, s0, s1, s0 -> Move s1
2472
2473   if (UsedSGPRs[0] != AMDGPU::NoRegister) {
2474     if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2])
2475       SGPRReg = UsedSGPRs[0];
2476   }
2477
2478   if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) {
2479     if (UsedSGPRs[1] == UsedSGPRs[2])
2480       SGPRReg = UsedSGPRs[1];
2481   }
2482
2483   return SGPRReg;
2484 }
2485
2486 MachineInstrBuilder SIInstrInfo::buildIndirectWrite(
2487                                    MachineBasicBlock *MBB,
2488                                    MachineBasicBlock::iterator I,
2489                                    unsigned ValueReg,
2490                                    unsigned Address, unsigned OffsetReg) const {
2491   const DebugLoc &DL = MBB->findDebugLoc(I);
2492   unsigned IndirectBaseReg = AMDGPU::VGPR_32RegClass.getRegister(
2493                                       getIndirectIndexBegin(*MBB->getParent()));
2494
2495   return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_DST_V1))
2496           .addReg(IndirectBaseReg, RegState::Define)
2497           .addOperand(I->getOperand(0))
2498           .addReg(IndirectBaseReg)
2499           .addReg(OffsetReg)
2500           .addImm(0)
2501           .addReg(ValueReg);
2502 }
2503
2504 MachineInstrBuilder SIInstrInfo::buildIndirectRead(
2505                                    MachineBasicBlock *MBB,
2506                                    MachineBasicBlock::iterator I,
2507                                    unsigned ValueReg,
2508                                    unsigned Address, unsigned OffsetReg) const {
2509   const DebugLoc &DL = MBB->findDebugLoc(I);
2510   unsigned IndirectBaseReg = AMDGPU::VGPR_32RegClass.getRegister(
2511                                       getIndirectIndexBegin(*MBB->getParent()));
2512
2513   return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_SRC))
2514           .addOperand(I->getOperand(0))
2515           .addOperand(I->getOperand(1))
2516           .addReg(IndirectBaseReg)
2517           .addReg(OffsetReg)
2518           .addImm(0);
2519
2520 }
2521
2522 void SIInstrInfo::reserveIndirectRegisters(BitVector &Reserved,
2523                                             const MachineFunction &MF) const {
2524   int End = getIndirectIndexEnd(MF);
2525   int Begin = getIndirectIndexBegin(MF);
2526
2527   if (End == -1)
2528     return;
2529
2530
2531   for (int Index = Begin; Index <= End; ++Index)
2532     Reserved.set(AMDGPU::VGPR_32RegClass.getRegister(Index));
2533
2534   for (int Index = std::max(0, Begin - 1); Index <= End; ++Index)
2535     Reserved.set(AMDGPU::VReg_64RegClass.getRegister(Index));
2536
2537   for (int Index = std::max(0, Begin - 2); Index <= End; ++Index)
2538     Reserved.set(AMDGPU::VReg_96RegClass.getRegister(Index));
2539
2540   for (int Index = std::max(0, Begin - 3); Index <= End; ++Index)
2541     Reserved.set(AMDGPU::VReg_128RegClass.getRegister(Index));
2542
2543   for (int Index = std::max(0, Begin - 7); Index <= End; ++Index)
2544     Reserved.set(AMDGPU::VReg_256RegClass.getRegister(Index));
2545
2546   for (int Index = std::max(0, Begin - 15); Index <= End; ++Index)
2547     Reserved.set(AMDGPU::VReg_512RegClass.getRegister(Index));
2548 }
2549
2550 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI,
2551                                              unsigned OperandName) const {
2552   int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName);
2553   if (Idx == -1)
2554     return nullptr;
2555
2556   return &MI.getOperand(Idx);
2557 }
2558
2559 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const {
2560   uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT;
2561   if (ST.isAmdHsaOS())
2562     RsrcDataFormat |= (1ULL << 56);
2563
2564   return RsrcDataFormat;
2565 }