1 //===-- SIInstrInfo.cpp - SI Instruction Information ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief SI Implementation of TargetInstrInfo.
13 //===----------------------------------------------------------------------===//
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"
30 SIInstrInfo::SIInstrInfo(const AMDGPUSubtarget &st)
31 : AMDGPUInstrInfo(st), RI() {}
33 //===----------------------------------------------------------------------===//
34 // TargetInstrInfo callbacks
35 //===----------------------------------------------------------------------===//
37 static unsigned getNumOperandsNoGlue(SDNode *Node) {
38 unsigned N = Node->getNumOperands();
39 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
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");
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();
56 int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName);
57 int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName);
59 if (Op0Idx == -1 && Op1Idx == -1)
63 if ((Op0Idx == -1 && Op1Idx != -1) ||
64 (Op1Idx == -1 && Op0Idx != -1))
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
74 return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx);
77 bool SIInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr *MI,
78 AliasAnalysis *AA) const {
79 // TODO: The generic check fails for VALU instructions that should be
80 // rematerializable due to implicit reads of exec. We really want all of the
81 // generic logic for this except for this.
82 switch (MI->getOpcode()) {
83 case AMDGPU::V_MOV_B32_e32:
84 case AMDGPU::V_MOV_B32_e64:
91 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1,
93 int64_t &Offset1) const {
94 if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode())
97 unsigned Opc0 = Load0->getMachineOpcode();
98 unsigned Opc1 = Load1->getMachineOpcode();
100 // Make sure both are actually loads.
101 if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad())
104 if (isDS(Opc0) && isDS(Opc1)) {
106 // FIXME: Handle this case:
107 if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1))
111 if (Load0->getOperand(1) != Load1->getOperand(1))
115 if (findChainOperand(Load0) != findChainOperand(Load1))
118 // Skip read2 / write2 variants for simplicity.
119 // TODO: We should report true if the used offsets are adjacent (excluded
121 if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::data1) != -1 ||
122 AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::data1) != -1)
125 Offset0 = cast<ConstantSDNode>(Load0->getOperand(2))->getZExtValue();
126 Offset1 = cast<ConstantSDNode>(Load1->getOperand(2))->getZExtValue();
130 if (isSMRD(Opc0) && isSMRD(Opc1)) {
131 assert(getNumOperandsNoGlue(Load0) == getNumOperandsNoGlue(Load1));
134 if (Load0->getOperand(0) != Load1->getOperand(0))
137 const ConstantSDNode *Load0Offset =
138 dyn_cast<ConstantSDNode>(Load0->getOperand(1));
139 const ConstantSDNode *Load1Offset =
140 dyn_cast<ConstantSDNode>(Load1->getOperand(1));
142 if (!Load0Offset || !Load1Offset)
146 if (findChainOperand(Load0) != findChainOperand(Load1))
149 Offset0 = Load0Offset->getZExtValue();
150 Offset1 = Load1Offset->getZExtValue();
154 // MUBUF and MTBUF can access the same addresses.
155 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) {
157 // MUBUF and MTBUF have vaddr at different indices.
158 if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) ||
159 findChainOperand(Load0) != findChainOperand(Load1) ||
160 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) ||
161 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc))
164 int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset);
165 int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset);
167 if (OffIdx0 == -1 || OffIdx1 == -1)
170 // getNamedOperandIdx returns the index for MachineInstrs. Since they
171 // inlcude the output in the operand list, but SDNodes don't, we need to
172 // subtract the index by one.
176 SDValue Off0 = Load0->getOperand(OffIdx0);
177 SDValue Off1 = Load1->getOperand(OffIdx1);
179 // The offset might be a FrameIndexSDNode.
180 if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1))
183 Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue();
184 Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue();
191 static bool isStride64(unsigned Opc) {
193 case AMDGPU::DS_READ2ST64_B32:
194 case AMDGPU::DS_READ2ST64_B64:
195 case AMDGPU::DS_WRITE2ST64_B32:
196 case AMDGPU::DS_WRITE2ST64_B64:
203 bool SIInstrInfo::getMemOpBaseRegImmOfs(MachineInstr *LdSt, unsigned &BaseReg,
205 const TargetRegisterInfo *TRI) const {
206 unsigned Opc = LdSt->getOpcode();
208 const MachineOperand *OffsetImm = getNamedOperand(*LdSt,
209 AMDGPU::OpName::offset);
211 // Normal, single offset LDS instruction.
212 const MachineOperand *AddrReg = getNamedOperand(*LdSt,
213 AMDGPU::OpName::addr);
215 BaseReg = AddrReg->getReg();
216 Offset = OffsetImm->getImm();
220 // The 2 offset instructions use offset0 and offset1 instead. We can treat
221 // these as a load with a single offset if the 2 offsets are consecutive. We
222 // will use this for some partially aligned loads.
223 const MachineOperand *Offset0Imm = getNamedOperand(*LdSt,
224 AMDGPU::OpName::offset0);
225 const MachineOperand *Offset1Imm = getNamedOperand(*LdSt,
226 AMDGPU::OpName::offset1);
228 uint8_t Offset0 = Offset0Imm->getImm();
229 uint8_t Offset1 = Offset1Imm->getImm();
231 if (Offset1 > Offset0 && Offset1 - Offset0 == 1) {
232 // Each of these offsets is in element sized units, so we need to convert
233 // to bytes of the individual reads.
237 EltSize = getOpRegClass(*LdSt, 0)->getSize() / 2;
239 assert(LdSt->mayStore());
240 int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0);
241 EltSize = getOpRegClass(*LdSt, Data0Idx)->getSize();
247 const MachineOperand *AddrReg = getNamedOperand(*LdSt,
248 AMDGPU::OpName::addr);
249 BaseReg = AddrReg->getReg();
250 Offset = EltSize * Offset0;
257 if (isMUBUF(Opc) || isMTBUF(Opc)) {
258 if (AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::soffset) != -1)
261 const MachineOperand *AddrReg = getNamedOperand(*LdSt,
262 AMDGPU::OpName::vaddr);
266 const MachineOperand *OffsetImm = getNamedOperand(*LdSt,
267 AMDGPU::OpName::offset);
268 BaseReg = AddrReg->getReg();
269 Offset = OffsetImm->getImm();
274 const MachineOperand *OffsetImm = getNamedOperand(*LdSt,
275 AMDGPU::OpName::offset);
279 const MachineOperand *SBaseReg = getNamedOperand(*LdSt,
280 AMDGPU::OpName::sbase);
281 BaseReg = SBaseReg->getReg();
282 Offset = OffsetImm->getImm();
289 bool SIInstrInfo::shouldClusterLoads(MachineInstr *FirstLdSt,
290 MachineInstr *SecondLdSt,
291 unsigned NumLoads) const {
292 unsigned Opc0 = FirstLdSt->getOpcode();
293 unsigned Opc1 = SecondLdSt->getOpcode();
295 // TODO: This needs finer tuning
299 if (isDS(Opc0) && isDS(Opc1))
302 if (isSMRD(Opc0) && isSMRD(Opc1))
305 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1)))
312 SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
313 MachineBasicBlock::iterator MI, DebugLoc DL,
314 unsigned DestReg, unsigned SrcReg,
315 bool KillSrc) const {
317 // If we are trying to copy to or from SCC, there is a bug somewhere else in
318 // the backend. While it may be theoretically possible to do this, it should
319 // never be necessary.
320 assert(DestReg != AMDGPU::SCC && SrcReg != AMDGPU::SCC);
322 static const int16_t Sub0_15[] = {
323 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
324 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
325 AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11,
326 AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15, 0
329 static const int16_t Sub0_7[] = {
330 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
331 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, 0
334 static const int16_t Sub0_3[] = {
335 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, 0
338 static const int16_t Sub0_2[] = {
339 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, 0
342 static const int16_t Sub0_1[] = {
343 AMDGPU::sub0, AMDGPU::sub1, 0
347 const int16_t *SubIndices;
349 if (AMDGPU::SReg_32RegClass.contains(DestReg)) {
350 assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
351 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
352 .addReg(SrcReg, getKillRegState(KillSrc));
355 } else if (AMDGPU::SReg_64RegClass.contains(DestReg)) {
356 if (DestReg == AMDGPU::VCC) {
357 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) {
358 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC)
359 .addReg(SrcReg, getKillRegState(KillSrc));
361 // FIXME: Hack until VReg_1 removed.
362 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg));
363 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_I32_e32))
365 .addReg(SrcReg, getKillRegState(KillSrc));
371 assert(AMDGPU::SReg_64RegClass.contains(SrcReg));
372 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
373 .addReg(SrcReg, getKillRegState(KillSrc));
376 } else if (AMDGPU::SReg_128RegClass.contains(DestReg)) {
377 assert(AMDGPU::SReg_128RegClass.contains(SrcReg));
378 Opcode = AMDGPU::S_MOV_B32;
381 } else if (AMDGPU::SReg_256RegClass.contains(DestReg)) {
382 assert(AMDGPU::SReg_256RegClass.contains(SrcReg));
383 Opcode = AMDGPU::S_MOV_B32;
386 } else if (AMDGPU::SReg_512RegClass.contains(DestReg)) {
387 assert(AMDGPU::SReg_512RegClass.contains(SrcReg));
388 Opcode = AMDGPU::S_MOV_B32;
389 SubIndices = Sub0_15;
391 } else if (AMDGPU::VGPR_32RegClass.contains(DestReg)) {
392 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
393 AMDGPU::SReg_32RegClass.contains(SrcReg));
394 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg)
395 .addReg(SrcReg, getKillRegState(KillSrc));
398 } else if (AMDGPU::VReg_64RegClass.contains(DestReg)) {
399 assert(AMDGPU::VReg_64RegClass.contains(SrcReg) ||
400 AMDGPU::SReg_64RegClass.contains(SrcReg));
401 Opcode = AMDGPU::V_MOV_B32_e32;
404 } else if (AMDGPU::VReg_96RegClass.contains(DestReg)) {
405 assert(AMDGPU::VReg_96RegClass.contains(SrcReg));
406 Opcode = AMDGPU::V_MOV_B32_e32;
409 } else if (AMDGPU::VReg_128RegClass.contains(DestReg)) {
410 assert(AMDGPU::VReg_128RegClass.contains(SrcReg) ||
411 AMDGPU::SReg_128RegClass.contains(SrcReg));
412 Opcode = AMDGPU::V_MOV_B32_e32;
415 } else if (AMDGPU::VReg_256RegClass.contains(DestReg)) {
416 assert(AMDGPU::VReg_256RegClass.contains(SrcReg) ||
417 AMDGPU::SReg_256RegClass.contains(SrcReg));
418 Opcode = AMDGPU::V_MOV_B32_e32;
421 } else if (AMDGPU::VReg_512RegClass.contains(DestReg)) {
422 assert(AMDGPU::VReg_512RegClass.contains(SrcReg) ||
423 AMDGPU::SReg_512RegClass.contains(SrcReg));
424 Opcode = AMDGPU::V_MOV_B32_e32;
425 SubIndices = Sub0_15;
428 llvm_unreachable("Can't copy register!");
431 while (unsigned SubIdx = *SubIndices++) {
432 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL,
433 get(Opcode), RI.getSubReg(DestReg, SubIdx));
435 Builder.addReg(RI.getSubReg(SrcReg, SubIdx), getKillRegState(KillSrc));
438 Builder.addReg(DestReg, RegState::Define | RegState::Implicit);
442 int SIInstrInfo::commuteOpcode(const MachineInstr &MI) const {
443 const unsigned Opcode = MI.getOpcode();
447 // Try to map original to commuted opcode
448 NewOpc = AMDGPU::getCommuteRev(Opcode);
450 // Check if the commuted (REV) opcode exists on the target.
451 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
453 // Try to map commuted to original opcode
454 NewOpc = AMDGPU::getCommuteOrig(Opcode);
456 // Check if the original (non-REV) opcode exists on the target.
457 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1;
462 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const {
464 if (DstRC->getSize() == 4) {
465 return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
466 } else if (DstRC->getSize() == 8 && RI.isSGPRClass(DstRC)) {
467 return AMDGPU::S_MOV_B64;
468 } else if (DstRC->getSize() == 8 && !RI.isSGPRClass(DstRC)) {
469 return AMDGPU::V_MOV_B64_PSEUDO;
474 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
475 MachineBasicBlock::iterator MI,
476 unsigned SrcReg, bool isKill,
478 const TargetRegisterClass *RC,
479 const TargetRegisterInfo *TRI) const {
480 MachineFunction *MF = MBB.getParent();
481 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
482 MachineFrameInfo *FrameInfo = MF->getFrameInfo();
483 DebugLoc DL = MBB.findDebugLoc(MI);
486 if (RI.isSGPRClass(RC)) {
487 // We are only allowed to create one new instruction when spilling
488 // registers, so we need to use pseudo instruction for spilling
490 switch (RC->getSize() * 8) {
491 case 32: Opcode = AMDGPU::SI_SPILL_S32_SAVE; break;
492 case 64: Opcode = AMDGPU::SI_SPILL_S64_SAVE; break;
493 case 128: Opcode = AMDGPU::SI_SPILL_S128_SAVE; break;
494 case 256: Opcode = AMDGPU::SI_SPILL_S256_SAVE; break;
495 case 512: Opcode = AMDGPU::SI_SPILL_S512_SAVE; break;
497 } else if(RI.hasVGPRs(RC) && ST.isVGPRSpillingEnabled(MFI)) {
498 MFI->setHasSpilledVGPRs();
500 switch(RC->getSize() * 8) {
501 case 32: Opcode = AMDGPU::SI_SPILL_V32_SAVE; break;
502 case 64: Opcode = AMDGPU::SI_SPILL_V64_SAVE; break;
503 case 96: Opcode = AMDGPU::SI_SPILL_V96_SAVE; break;
504 case 128: Opcode = AMDGPU::SI_SPILL_V128_SAVE; break;
505 case 256: Opcode = AMDGPU::SI_SPILL_V256_SAVE; break;
506 case 512: Opcode = AMDGPU::SI_SPILL_V512_SAVE; break;
511 MachinePointerInfo PtrInfo
512 = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
513 unsigned Size = FrameInfo->getObjectSize(FrameIndex);
514 unsigned Align = FrameInfo->getObjectAlignment(FrameIndex);
515 MachineMemOperand *MMO
516 = MF->getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore,
519 FrameInfo->setObjectAlignment(FrameIndex, 4);
520 BuildMI(MBB, MI, DL, get(Opcode))
522 .addFrameIndex(FrameIndex)
523 // Place-holder registers, these will be filled in by
524 // SIPrepareScratchRegs.
525 .addReg(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, RegState::Undef)
526 .addReg(AMDGPU::SGPR0, RegState::Undef)
529 LLVMContext &Ctx = MF->getFunction()->getContext();
530 Ctx.emitError("SIInstrInfo::storeRegToStackSlot - Do not know how to"
532 BuildMI(MBB, MI, DL, get(AMDGPU::KILL))
537 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
538 MachineBasicBlock::iterator MI,
539 unsigned DestReg, int FrameIndex,
540 const TargetRegisterClass *RC,
541 const TargetRegisterInfo *TRI) const {
542 MachineFunction *MF = MBB.getParent();
543 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
544 MachineFrameInfo *FrameInfo = MF->getFrameInfo();
545 DebugLoc DL = MBB.findDebugLoc(MI);
548 if (RI.isSGPRClass(RC)){
549 switch(RC->getSize() * 8) {
550 case 32: Opcode = AMDGPU::SI_SPILL_S32_RESTORE; break;
551 case 64: Opcode = AMDGPU::SI_SPILL_S64_RESTORE; break;
552 case 128: Opcode = AMDGPU::SI_SPILL_S128_RESTORE; break;
553 case 256: Opcode = AMDGPU::SI_SPILL_S256_RESTORE; break;
554 case 512: Opcode = AMDGPU::SI_SPILL_S512_RESTORE; break;
556 } else if(RI.hasVGPRs(RC) && ST.isVGPRSpillingEnabled(MFI)) {
557 switch(RC->getSize() * 8) {
558 case 32: Opcode = AMDGPU::SI_SPILL_V32_RESTORE; break;
559 case 64: Opcode = AMDGPU::SI_SPILL_V64_RESTORE; break;
560 case 96: Opcode = AMDGPU::SI_SPILL_V96_RESTORE; break;
561 case 128: Opcode = AMDGPU::SI_SPILL_V128_RESTORE; break;
562 case 256: Opcode = AMDGPU::SI_SPILL_V256_RESTORE; break;
563 case 512: Opcode = AMDGPU::SI_SPILL_V512_RESTORE; break;
569 FrameInfo->setObjectAlignment(FrameIndex, Align);
570 unsigned Size = FrameInfo->getObjectSize(FrameIndex);
572 MachinePointerInfo PtrInfo
573 = MachinePointerInfo::getFixedStack(*MF, FrameIndex);
574 MachineMemOperand *MMO = MF->getMachineMemOperand(
575 PtrInfo, MachineMemOperand::MOLoad, Size, Align);
577 BuildMI(MBB, MI, DL, get(Opcode), DestReg)
578 .addFrameIndex(FrameIndex)
579 // Place-holder registers, these will be filled in by
580 // SIPrepareScratchRegs.
581 .addReg(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, RegState::Undef)
582 .addReg(AMDGPU::SGPR0, RegState::Undef)
585 LLVMContext &Ctx = MF->getFunction()->getContext();
586 Ctx.emitError("SIInstrInfo::loadRegFromStackSlot - Do not know how to"
587 " restore register");
588 BuildMI(MBB, MI, DL, get(AMDGPU::IMPLICIT_DEF), DestReg);
592 /// \param @Offset Offset in bytes of the FrameIndex being spilled
593 unsigned SIInstrInfo::calculateLDSSpillAddress(MachineBasicBlock &MBB,
594 MachineBasicBlock::iterator MI,
595 RegScavenger *RS, unsigned TmpReg,
596 unsigned FrameOffset,
597 unsigned Size) const {
598 MachineFunction *MF = MBB.getParent();
599 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
600 const AMDGPUSubtarget &ST = MF->getSubtarget<AMDGPUSubtarget>();
601 const SIRegisterInfo *TRI =
602 static_cast<const SIRegisterInfo*>(ST.getRegisterInfo());
603 DebugLoc DL = MBB.findDebugLoc(MI);
604 unsigned WorkGroupSize = MFI->getMaximumWorkGroupSize(*MF);
605 unsigned WavefrontSize = ST.getWavefrontSize();
607 unsigned TIDReg = MFI->getTIDReg();
608 if (!MFI->hasCalculatedTID()) {
609 MachineBasicBlock &Entry = MBB.getParent()->front();
610 MachineBasicBlock::iterator Insert = Entry.front();
611 DebugLoc DL = Insert->getDebugLoc();
613 TIDReg = RI.findUnusedRegister(MF->getRegInfo(), &AMDGPU::VGPR_32RegClass);
614 if (TIDReg == AMDGPU::NoRegister)
618 if (MFI->getShaderType() == ShaderType::COMPUTE &&
619 WorkGroupSize > WavefrontSize) {
621 unsigned TIDIGXReg = TRI->getPreloadedValue(*MF, SIRegisterInfo::TIDIG_X);
622 unsigned TIDIGYReg = TRI->getPreloadedValue(*MF, SIRegisterInfo::TIDIG_Y);
623 unsigned TIDIGZReg = TRI->getPreloadedValue(*MF, SIRegisterInfo::TIDIG_Z);
624 unsigned InputPtrReg =
625 TRI->getPreloadedValue(*MF, SIRegisterInfo::INPUT_PTR);
626 for (unsigned Reg : {TIDIGXReg, TIDIGYReg, TIDIGZReg}) {
627 if (!Entry.isLiveIn(Reg))
628 Entry.addLiveIn(Reg);
631 RS->enterBasicBlock(&Entry);
632 unsigned STmp0 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0);
633 unsigned STmp1 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0);
634 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp0)
636 .addImm(SI::KernelInputOffsets::NGROUPS_Z);
637 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp1)
639 .addImm(SI::KernelInputOffsets::NGROUPS_Y);
641 // NGROUPS.X * NGROUPS.Y
642 BuildMI(Entry, Insert, DL, get(AMDGPU::S_MUL_I32), STmp1)
645 // (NGROUPS.X * NGROUPS.Y) * TIDIG.X
646 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MUL_U32_U24_e32), TIDReg)
649 // NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)
650 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MAD_U32_U24), TIDReg)
654 // (NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)) + TIDIG.Z
655 BuildMI(Entry, Insert, DL, get(AMDGPU::V_ADD_I32_e32), TIDReg)
660 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_LO_U32_B32_e64),
665 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_HI_U32_B32_e64),
671 BuildMI(Entry, Insert, DL, get(AMDGPU::V_LSHLREV_B32_e32),
675 MFI->setTIDReg(TIDReg);
678 // Add FrameIndex to LDS offset
679 unsigned LDSOffset = MFI->LDSSize + (FrameOffset * WorkGroupSize);
680 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e32), TmpReg)
687 void SIInstrInfo::insertNOPs(MachineBasicBlock::iterator MI,
696 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(AMDGPU::S_NOP))
701 bool SIInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
702 MachineBasicBlock &MBB = *MI->getParent();
703 DebugLoc DL = MBB.findDebugLoc(MI);
704 switch (MI->getOpcode()) {
705 default: return AMDGPUInstrInfo::expandPostRAPseudo(MI);
707 case AMDGPU::SI_CONSTDATA_PTR: {
708 unsigned Reg = MI->getOperand(0).getReg();
709 unsigned RegLo = RI.getSubReg(Reg, AMDGPU::sub0);
710 unsigned RegHi = RI.getSubReg(Reg, AMDGPU::sub1);
712 BuildMI(MBB, MI, DL, get(AMDGPU::S_GETPC_B64), Reg);
714 // Add 32-bit offset from this instruction to the start of the constant data.
715 BuildMI(MBB, MI, DL, get(AMDGPU::S_ADD_U32), RegLo)
717 .addTargetIndex(AMDGPU::TI_CONSTDATA_START)
718 .addReg(AMDGPU::SCC, RegState::Define | RegState::Implicit);
719 BuildMI(MBB, MI, DL, get(AMDGPU::S_ADDC_U32), RegHi)
722 .addReg(AMDGPU::SCC, RegState::Define | RegState::Implicit)
723 .addReg(AMDGPU::SCC, RegState::Implicit);
724 MI->eraseFromParent();
727 case AMDGPU::SGPR_USE:
728 // This is just a placeholder for register allocation.
729 MI->eraseFromParent();
732 case AMDGPU::V_MOV_B64_PSEUDO: {
733 unsigned Dst = MI->getOperand(0).getReg();
734 unsigned DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
735 unsigned DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
737 const MachineOperand &SrcOp = MI->getOperand(1);
738 // FIXME: Will this work for 64-bit floating point immediates?
739 assert(!SrcOp.isFPImm());
741 APInt Imm(64, SrcOp.getImm());
742 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
743 .addImm(Imm.getLoBits(32).getZExtValue())
744 .addReg(Dst, RegState::Implicit);
745 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
746 .addImm(Imm.getHiBits(32).getZExtValue())
747 .addReg(Dst, RegState::Implicit);
749 assert(SrcOp.isReg());
750 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo)
751 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0))
752 .addReg(Dst, RegState::Implicit);
753 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi)
754 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1))
755 .addReg(Dst, RegState::Implicit);
757 MI->eraseFromParent();
761 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
762 unsigned Dst = MI->getOperand(0).getReg();
763 unsigned DstLo = RI.getSubReg(Dst, AMDGPU::sub0);
764 unsigned DstHi = RI.getSubReg(Dst, AMDGPU::sub1);
765 unsigned Src0 = MI->getOperand(1).getReg();
766 unsigned Src1 = MI->getOperand(2).getReg();
767 const MachineOperand &SrcCond = MI->getOperand(3);
769 BuildMI(MBB, MI, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
770 .addReg(RI.getSubReg(Src0, AMDGPU::sub0))
771 .addReg(RI.getSubReg(Src1, AMDGPU::sub0))
772 .addOperand(SrcCond);
773 BuildMI(MBB, MI, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
774 .addReg(RI.getSubReg(Src0, AMDGPU::sub1))
775 .addReg(RI.getSubReg(Src1, AMDGPU::sub1))
776 .addOperand(SrcCond);
777 MI->eraseFromParent();
784 MachineInstr *SIInstrInfo::commuteInstruction(MachineInstr *MI,
787 if (MI->getNumOperands() < 3)
790 int CommutedOpcode = commuteOpcode(*MI);
791 if (CommutedOpcode == -1)
794 int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
795 AMDGPU::OpName::src0);
796 assert(Src0Idx != -1 && "Should always have src0 operand");
798 MachineOperand &Src0 = MI->getOperand(Src0Idx);
802 int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
803 AMDGPU::OpName::src1);
807 MachineOperand &Src1 = MI->getOperand(Src1Idx);
809 // Make sure it's legal to commute operands for VOP2.
810 if (isVOP2(MI->getOpcode()) &&
811 (!isOperandLegal(MI, Src0Idx, &Src1) ||
812 !isOperandLegal(MI, Src1Idx, &Src0))) {
817 // Allow commuting instructions with Imm operands.
818 if (NewMI || !Src1.isImm() ||
819 (!isVOP2(MI->getOpcode()) && !isVOP3(MI->getOpcode()))) {
823 // Be sure to copy the source modifiers to the right place.
824 if (MachineOperand *Src0Mods
825 = getNamedOperand(*MI, AMDGPU::OpName::src0_modifiers)) {
826 MachineOperand *Src1Mods
827 = getNamedOperand(*MI, AMDGPU::OpName::src1_modifiers);
829 int Src0ModsVal = Src0Mods->getImm();
830 if (!Src1Mods && Src0ModsVal != 0)
833 // XXX - This assert might be a lie. It might be useful to have a neg
834 // modifier with 0.0.
835 int Src1ModsVal = Src1Mods->getImm();
836 assert((Src1ModsVal == 0) && "Not expecting modifiers with immediates");
838 Src1Mods->setImm(Src0ModsVal);
839 Src0Mods->setImm(Src1ModsVal);
842 unsigned Reg = Src0.getReg();
843 unsigned SubReg = Src0.getSubReg();
845 Src0.ChangeToImmediate(Src1.getImm());
847 llvm_unreachable("Should only have immediates");
849 Src1.ChangeToRegister(Reg, false);
850 Src1.setSubReg(SubReg);
852 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
856 MI->setDesc(get(CommutedOpcode));
861 // This needs to be implemented because the source modifiers may be inserted
862 // between the true commutable operands, and the base
863 // TargetInstrInfo::commuteInstruction uses it.
864 bool SIInstrInfo::findCommutedOpIndices(MachineInstr *MI,
866 unsigned &SrcOpIdx2) const {
867 const MCInstrDesc &MCID = MI->getDesc();
868 if (!MCID.isCommutable())
871 unsigned Opc = MI->getOpcode();
872 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
876 // FIXME: Workaround TargetInstrInfo::commuteInstruction asserting on
878 if (!MI->getOperand(Src0Idx).isReg())
881 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
885 if (!MI->getOperand(Src1Idx).isReg())
888 // If any source modifiers are set, the generic instruction commuting won't
889 // understand how to copy the source modifiers.
890 if (hasModifiersSet(*MI, AMDGPU::OpName::src0_modifiers) ||
891 hasModifiersSet(*MI, AMDGPU::OpName::src1_modifiers))
899 MachineInstr *SIInstrInfo::buildMovInstr(MachineBasicBlock *MBB,
900 MachineBasicBlock::iterator I,
902 unsigned SrcReg) const {
903 return BuildMI(*MBB, I, MBB->findDebugLoc(I), get(AMDGPU::V_MOV_B32_e32),
904 DstReg) .addReg(SrcReg);
907 bool SIInstrInfo::isMov(unsigned Opcode) const {
909 default: return false;
910 case AMDGPU::S_MOV_B32:
911 case AMDGPU::S_MOV_B64:
912 case AMDGPU::V_MOV_B32_e32:
913 case AMDGPU::V_MOV_B32_e64:
918 static void removeModOperands(MachineInstr &MI) {
919 unsigned Opc = MI.getOpcode();
920 int Src0ModIdx = AMDGPU::getNamedOperandIdx(Opc,
921 AMDGPU::OpName::src0_modifiers);
922 int Src1ModIdx = AMDGPU::getNamedOperandIdx(Opc,
923 AMDGPU::OpName::src1_modifiers);
924 int Src2ModIdx = AMDGPU::getNamedOperandIdx(Opc,
925 AMDGPU::OpName::src2_modifiers);
927 MI.RemoveOperand(Src2ModIdx);
928 MI.RemoveOperand(Src1ModIdx);
929 MI.RemoveOperand(Src0ModIdx);
932 bool SIInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
933 unsigned Reg, MachineRegisterInfo *MRI) const {
934 if (!MRI->hasOneNonDBGUse(Reg))
937 unsigned Opc = UseMI->getOpcode();
938 if (Opc == AMDGPU::V_MAD_F32 || Opc == AMDGPU::V_MAC_F32_e64) {
939 // Don't fold if we are using source modifiers. The new VOP2 instructions
941 if (hasModifiersSet(*UseMI, AMDGPU::OpName::src0_modifiers) ||
942 hasModifiersSet(*UseMI, AMDGPU::OpName::src1_modifiers) ||
943 hasModifiersSet(*UseMI, AMDGPU::OpName::src2_modifiers)) {
947 MachineOperand *Src0 = getNamedOperand(*UseMI, AMDGPU::OpName::src0);
948 MachineOperand *Src1 = getNamedOperand(*UseMI, AMDGPU::OpName::src1);
949 MachineOperand *Src2 = getNamedOperand(*UseMI, AMDGPU::OpName::src2);
951 // Multiplied part is the constant: Use v_madmk_f32
952 // We should only expect these to be on src0 due to canonicalizations.
953 if (Src0->isReg() && Src0->getReg() == Reg) {
954 if (!Src1->isReg() ||
955 (Src1->isReg() && RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))))
958 if (!Src2->isReg() ||
959 (Src2->isReg() && RI.isSGPRClass(MRI->getRegClass(Src2->getReg()))))
962 // We need to do some weird looking operand shuffling since the madmk
963 // operands are out of the normal expected order with the multiplied
964 // constant as the last operand.
966 // v_mad_f32 src0, src1, src2 -> v_madmk_f32 src0 * src2K + src1
971 const int64_t Imm = DefMI->getOperand(1).getImm();
973 // FIXME: This would be a lot easier if we could return a new instruction
974 // instead of having to modify in place.
976 // Remove these first since they are at the end.
977 UseMI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc,
978 AMDGPU::OpName::omod));
979 UseMI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc,
980 AMDGPU::OpName::clamp));
982 unsigned Src1Reg = Src1->getReg();
983 unsigned Src1SubReg = Src1->getSubReg();
984 unsigned Src2Reg = Src2->getReg();
985 unsigned Src2SubReg = Src2->getSubReg();
986 Src0->setReg(Src1Reg);
987 Src0->setSubReg(Src1SubReg);
988 Src0->setIsKill(Src1->isKill());
990 Src1->setReg(Src2Reg);
991 Src1->setSubReg(Src2SubReg);
992 Src1->setIsKill(Src2->isKill());
994 if (Opc == AMDGPU::V_MAC_F32_e64) {
995 UseMI->untieRegOperand(
996 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
999 UseMI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc,
1000 AMDGPU::OpName::src2));
1001 // ChangingToImmediate adds Src2 back to the instruction.
1002 Src2->ChangeToImmediate(Imm);
1004 removeModOperands(*UseMI);
1005 UseMI->setDesc(get(AMDGPU::V_MADMK_F32));
1007 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1009 DefMI->eraseFromParent();
1014 // Added part is the constant: Use v_madak_f32
1015 if (Src2->isReg() && Src2->getReg() == Reg) {
1016 // Not allowed to use constant bus for another operand.
1017 // We can however allow an inline immediate as src0.
1018 if (!Src0->isImm() &&
1019 (Src0->isReg() && RI.isSGPRClass(MRI->getRegClass(Src0->getReg()))))
1022 if (!Src1->isReg() ||
1023 (Src1->isReg() && RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))))
1026 const int64_t Imm = DefMI->getOperand(1).getImm();
1028 // FIXME: This would be a lot easier if we could return a new instruction
1029 // instead of having to modify in place.
1031 // Remove these first since they are at the end.
1032 UseMI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc,
1033 AMDGPU::OpName::omod));
1034 UseMI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc,
1035 AMDGPU::OpName::clamp));
1037 if (Opc == AMDGPU::V_MAC_F32_e64) {
1038 UseMI->untieRegOperand(
1039 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2));
1042 // ChangingToImmediate adds Src2 back to the instruction.
1043 Src2->ChangeToImmediate(Imm);
1045 // These come before src2.
1046 removeModOperands(*UseMI);
1047 UseMI->setDesc(get(AMDGPU::V_MADAK_F32));
1049 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1051 DefMI->eraseFromParent();
1060 static bool offsetsDoNotOverlap(int WidthA, int OffsetA,
1061 int WidthB, int OffsetB) {
1062 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
1063 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
1064 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1065 return LowOffset + LowWidth <= HighOffset;
1068 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(MachineInstr *MIa,
1069 MachineInstr *MIb) const {
1070 unsigned BaseReg0, Offset0;
1071 unsigned BaseReg1, Offset1;
1073 if (getMemOpBaseRegImmOfs(MIa, BaseReg0, Offset0, &RI) &&
1074 getMemOpBaseRegImmOfs(MIb, BaseReg1, Offset1, &RI)) {
1075 assert(MIa->hasOneMemOperand() && MIb->hasOneMemOperand() &&
1076 "read2 / write2 not expected here yet");
1077 unsigned Width0 = (*MIa->memoperands_begin())->getSize();
1078 unsigned Width1 = (*MIb->memoperands_begin())->getSize();
1079 if (BaseReg0 == BaseReg1 &&
1080 offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1)) {
1088 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
1090 AliasAnalysis *AA) const {
1091 unsigned Opc0 = MIa->getOpcode();
1092 unsigned Opc1 = MIb->getOpcode();
1094 assert(MIa && (MIa->mayLoad() || MIa->mayStore()) &&
1095 "MIa must load from or modify a memory location");
1096 assert(MIb && (MIb->mayLoad() || MIb->mayStore()) &&
1097 "MIb must load from or modify a memory location");
1099 if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects())
1102 // XXX - Can we relax this between address spaces?
1103 if (MIa->hasOrderedMemoryRef() || MIb->hasOrderedMemoryRef())
1106 // TODO: Should we check the address space from the MachineMemOperand? That
1107 // would allow us to distinguish objects we know don't alias based on the
1108 // underlying address space, even if it was lowered to a different one,
1109 // e.g. private accesses lowered to use MUBUF instructions on a scratch
1113 return checkInstOffsetsDoNotOverlap(MIa, MIb);
1115 return !isFLAT(Opc1);
1118 if (isMUBUF(Opc0) || isMTBUF(Opc0)) {
1119 if (isMUBUF(Opc1) || isMTBUF(Opc1))
1120 return checkInstOffsetsDoNotOverlap(MIa, MIb);
1122 return !isFLAT(Opc1) && !isSMRD(Opc1);
1127 return checkInstOffsetsDoNotOverlap(MIa, MIb);
1129 return !isFLAT(Opc1) && !isMUBUF(Opc0) && !isMTBUF(Opc0);
1134 return checkInstOffsetsDoNotOverlap(MIa, MIb);
1142 MachineInstr *SIInstrInfo::convertToThreeAddress(MachineFunction::iterator &MBB,
1143 MachineBasicBlock::iterator &MI,
1144 LiveVariables *LV) const {
1146 switch (MI->getOpcode()) {
1147 default: return nullptr;
1148 case AMDGPU::V_MAC_F32_e64: break;
1149 case AMDGPU::V_MAC_F32_e32: {
1150 const MachineOperand *Src0 = getNamedOperand(*MI, AMDGPU::OpName::src0);
1151 if (Src0->isImm() && !isInlineConstant(*Src0, 4))
1157 const MachineOperand *Dst = getNamedOperand(*MI, AMDGPU::OpName::dst);
1158 const MachineOperand *Src0 = getNamedOperand(*MI, AMDGPU::OpName::src0);
1159 const MachineOperand *Src1 = getNamedOperand(*MI, AMDGPU::OpName::src1);
1160 const MachineOperand *Src2 = getNamedOperand(*MI, AMDGPU::OpName::src2);
1162 return BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_MAD_F32))
1164 .addImm(0) // Src0 mods
1166 .addImm(0) // Src1 mods
1168 .addImm(0) // Src mods
1174 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const {
1175 int64_t SVal = Imm.getSExtValue();
1176 if (SVal >= -16 && SVal <= 64)
1179 if (Imm.getBitWidth() == 64) {
1180 uint64_t Val = Imm.getZExtValue();
1181 return (DoubleToBits(0.0) == Val) ||
1182 (DoubleToBits(1.0) == Val) ||
1183 (DoubleToBits(-1.0) == Val) ||
1184 (DoubleToBits(0.5) == Val) ||
1185 (DoubleToBits(-0.5) == Val) ||
1186 (DoubleToBits(2.0) == Val) ||
1187 (DoubleToBits(-2.0) == Val) ||
1188 (DoubleToBits(4.0) == Val) ||
1189 (DoubleToBits(-4.0) == Val);
1192 // The actual type of the operand does not seem to matter as long
1193 // as the bits match one of the inline immediate values. For example:
1195 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
1196 // so it is a legal inline immediate.
1198 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
1199 // floating-point, so it is a legal inline immediate.
1200 uint32_t Val = Imm.getZExtValue();
1202 return (FloatToBits(0.0f) == Val) ||
1203 (FloatToBits(1.0f) == Val) ||
1204 (FloatToBits(-1.0f) == Val) ||
1205 (FloatToBits(0.5f) == Val) ||
1206 (FloatToBits(-0.5f) == Val) ||
1207 (FloatToBits(2.0f) == Val) ||
1208 (FloatToBits(-2.0f) == Val) ||
1209 (FloatToBits(4.0f) == Val) ||
1210 (FloatToBits(-4.0f) == Val);
1213 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO,
1214 unsigned OpSize) const {
1216 // MachineOperand provides no way to tell the true operand size, since it
1217 // only records a 64-bit value. We need to know the size to determine if a
1218 // 32-bit floating point immediate bit pattern is legal for an integer
1219 // immediate. It would be for any 32-bit integer operand, but would not be
1220 // for a 64-bit one.
1222 unsigned BitSize = 8 * OpSize;
1223 return isInlineConstant(APInt(BitSize, MO.getImm(), true));
1229 bool SIInstrInfo::isLiteralConstant(const MachineOperand &MO,
1230 unsigned OpSize) const {
1231 return MO.isImm() && !isInlineConstant(MO, OpSize);
1234 static bool compareMachineOp(const MachineOperand &Op0,
1235 const MachineOperand &Op1) {
1236 if (Op0.getType() != Op1.getType())
1239 switch (Op0.getType()) {
1240 case MachineOperand::MO_Register:
1241 return Op0.getReg() == Op1.getReg();
1242 case MachineOperand::MO_Immediate:
1243 return Op0.getImm() == Op1.getImm();
1245 llvm_unreachable("Didn't expect to be comparing these operand types");
1249 bool SIInstrInfo::isImmOperandLegal(const MachineInstr *MI, unsigned OpNo,
1250 const MachineOperand &MO) const {
1251 const MCOperandInfo &OpInfo = get(MI->getOpcode()).OpInfo[OpNo];
1253 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI());
1255 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE)
1258 if (OpInfo.RegClass < 0)
1261 unsigned OpSize = RI.getRegClass(OpInfo.RegClass)->getSize();
1262 if (isLiteralConstant(MO, OpSize))
1263 return RI.opCanUseLiteralConstant(OpInfo.OperandType);
1265 return RI.opCanUseInlineConstant(OpInfo.OperandType);
1268 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const {
1269 int Op32 = AMDGPU::getVOPe32(Opcode);
1273 return pseudoToMCOpcode(Op32) != -1;
1276 bool SIInstrInfo::hasModifiers(unsigned Opcode) const {
1277 // The src0_modifier operand is present on all instructions
1278 // that have modifiers.
1280 return AMDGPU::getNamedOperandIdx(Opcode,
1281 AMDGPU::OpName::src0_modifiers) != -1;
1284 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI,
1285 unsigned OpName) const {
1286 const MachineOperand *Mods = getNamedOperand(MI, OpName);
1287 return Mods && Mods->getImm();
1290 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI,
1291 const MachineOperand &MO,
1292 unsigned OpSize) const {
1293 // Literal constants use the constant bus.
1294 if (isLiteralConstant(MO, OpSize))
1297 if (!MO.isReg() || !MO.isUse())
1300 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1301 return RI.isSGPRClass(MRI.getRegClass(MO.getReg()));
1303 // FLAT_SCR is just an SGPR pair.
1304 if (!MO.isImplicit() && (MO.getReg() == AMDGPU::FLAT_SCR))
1307 // EXEC register uses the constant bus.
1308 if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC)
1311 // SGPRs use the constant bus
1312 if (MO.getReg() == AMDGPU::M0 || MO.getReg() == AMDGPU::VCC ||
1313 (!MO.isImplicit() &&
1314 (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) ||
1315 AMDGPU::SGPR_64RegClass.contains(MO.getReg())))) {
1322 bool SIInstrInfo::verifyInstruction(const MachineInstr *MI,
1323 StringRef &ErrInfo) const {
1324 uint16_t Opcode = MI->getOpcode();
1325 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1326 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
1327 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
1328 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
1330 // Make sure the number of operands is correct.
1331 const MCInstrDesc &Desc = get(Opcode);
1332 if (!Desc.isVariadic() &&
1333 Desc.getNumOperands() != MI->getNumExplicitOperands()) {
1334 ErrInfo = "Instruction has wrong number of operands.";
1338 // Make sure the register classes are correct
1339 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) {
1340 if (MI->getOperand(i).isFPImm()) {
1341 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast "
1342 "all fp values to integers.";
1346 int RegClass = Desc.OpInfo[i].RegClass;
1348 switch (Desc.OpInfo[i].OperandType) {
1349 case MCOI::OPERAND_REGISTER:
1350 if (MI->getOperand(i).isImm()) {
1351 ErrInfo = "Illegal immediate value for operand.";
1355 case AMDGPU::OPERAND_REG_IMM32:
1357 case AMDGPU::OPERAND_REG_INLINE_C:
1358 if (isLiteralConstant(MI->getOperand(i),
1359 RI.getRegClass(RegClass)->getSize())) {
1360 ErrInfo = "Illegal immediate value for operand.";
1364 case MCOI::OPERAND_IMMEDIATE:
1365 // Check if this operand is an immediate.
1366 // FrameIndex operands will be replaced by immediates, so they are
1368 if (!MI->getOperand(i).isImm() && !MI->getOperand(i).isFI()) {
1369 ErrInfo = "Expected immediate, but got non-immediate";
1377 if (!MI->getOperand(i).isReg())
1380 if (RegClass != -1) {
1381 unsigned Reg = MI->getOperand(i).getReg();
1382 if (TargetRegisterInfo::isVirtualRegister(Reg))
1385 const TargetRegisterClass *RC = RI.getRegClass(RegClass);
1386 if (!RC->contains(Reg)) {
1387 ErrInfo = "Operand has incorrect register class.";
1395 if (isVOP1(Opcode) || isVOP2(Opcode) || isVOP3(Opcode) || isVOPC(Opcode)) {
1396 // Only look at the true operands. Only a real operand can use the constant
1397 // bus, and we don't want to check pseudo-operands like the source modifier
1399 const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx };
1401 unsigned ConstantBusCount = 0;
1402 unsigned SGPRUsed = AMDGPU::NoRegister;
1403 for (int OpIdx : OpIndices) {
1406 const MachineOperand &MO = MI->getOperand(OpIdx);
1407 if (usesConstantBus(MRI, MO, getOpSize(Opcode, OpIdx))) {
1409 if (MO.getReg() != SGPRUsed)
1411 SGPRUsed = MO.getReg();
1417 if (ConstantBusCount > 1) {
1418 ErrInfo = "VOP* instruction uses the constant bus more than once";
1423 // Verify misc. restrictions on specific instructions.
1424 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32 ||
1425 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64) {
1426 const MachineOperand &Src0 = MI->getOperand(Src0Idx);
1427 const MachineOperand &Src1 = MI->getOperand(Src1Idx);
1428 const MachineOperand &Src2 = MI->getOperand(Src2Idx);
1429 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) {
1430 if (!compareMachineOp(Src0, Src1) &&
1431 !compareMachineOp(Src0, Src2)) {
1432 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2";
1441 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) {
1442 switch (MI.getOpcode()) {
1443 default: return AMDGPU::INSTRUCTION_LIST_END;
1444 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE;
1445 case AMDGPU::COPY: return AMDGPU::COPY;
1446 case AMDGPU::PHI: return AMDGPU::PHI;
1447 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG;
1448 case AMDGPU::S_MOV_B32:
1449 return MI.getOperand(1).isReg() ?
1450 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32;
1451 case AMDGPU::S_ADD_I32:
1452 case AMDGPU::S_ADD_U32: return AMDGPU::V_ADD_I32_e32;
1453 case AMDGPU::S_ADDC_U32: return AMDGPU::V_ADDC_U32_e32;
1454 case AMDGPU::S_SUB_I32:
1455 case AMDGPU::S_SUB_U32: return AMDGPU::V_SUB_I32_e32;
1456 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32;
1457 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_I32;
1458 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e32;
1459 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e32;
1460 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e32;
1461 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e32;
1462 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e32;
1463 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e32;
1464 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e32;
1465 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32;
1466 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64;
1467 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32;
1468 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64;
1469 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32;
1470 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64;
1471 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32;
1472 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32;
1473 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32;
1474 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32;
1475 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64;
1476 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32;
1477 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32;
1478 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32;
1479 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32;
1480 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32;
1481 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32;
1482 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32;
1483 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32;
1484 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32;
1485 case AMDGPU::S_LOAD_DWORD_IMM:
1486 case AMDGPU::S_LOAD_DWORD_SGPR: return AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1487 case AMDGPU::S_LOAD_DWORDX2_IMM:
1488 case AMDGPU::S_LOAD_DWORDX2_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1489 case AMDGPU::S_LOAD_DWORDX4_IMM:
1490 case AMDGPU::S_LOAD_DWORDX4_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1491 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64;
1492 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32;
1493 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32;
1494 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64;
1498 bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const {
1499 return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END;
1502 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI,
1503 unsigned OpNo) const {
1504 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1505 const MCInstrDesc &Desc = get(MI.getOpcode());
1506 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
1507 Desc.OpInfo[OpNo].RegClass == -1) {
1508 unsigned Reg = MI.getOperand(OpNo).getReg();
1510 if (TargetRegisterInfo::isVirtualRegister(Reg))
1511 return MRI.getRegClass(Reg);
1512 return RI.getPhysRegClass(Reg);
1515 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
1516 return RI.getRegClass(RCID);
1519 bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const {
1520 switch (MI.getOpcode()) {
1522 case AMDGPU::REG_SEQUENCE:
1524 case AMDGPU::INSERT_SUBREG:
1525 return RI.hasVGPRs(getOpRegClass(MI, 0));
1527 return RI.hasVGPRs(getOpRegClass(MI, OpNo));
1531 void SIInstrInfo::legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const {
1532 MachineBasicBlock::iterator I = MI;
1533 MachineBasicBlock *MBB = MI->getParent();
1534 MachineOperand &MO = MI->getOperand(OpIdx);
1535 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1536 unsigned RCID = get(MI->getOpcode()).OpInfo[OpIdx].RegClass;
1537 const TargetRegisterClass *RC = RI.getRegClass(RCID);
1538 unsigned Opcode = AMDGPU::V_MOV_B32_e32;
1540 Opcode = AMDGPU::COPY;
1541 else if (RI.isSGPRClass(RC))
1542 Opcode = AMDGPU::S_MOV_B32;
1545 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
1546 if (RI.getCommonSubClass(&AMDGPU::VReg_64RegClass, VRC))
1547 VRC = &AMDGPU::VReg_64RegClass;
1549 VRC = &AMDGPU::VGPR_32RegClass;
1551 unsigned Reg = MRI.createVirtualRegister(VRC);
1552 DebugLoc DL = MBB->findDebugLoc(I);
1553 BuildMI(*MI->getParent(), I, DL, get(Opcode), Reg)
1555 MO.ChangeToRegister(Reg, false);
1558 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI,
1559 MachineRegisterInfo &MRI,
1560 MachineOperand &SuperReg,
1561 const TargetRegisterClass *SuperRC,
1563 const TargetRegisterClass *SubRC)
1565 assert(SuperReg.isReg());
1567 unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC);
1568 unsigned SubReg = MRI.createVirtualRegister(SubRC);
1570 // Just in case the super register is itself a sub-register, copy it to a new
1571 // value so we don't need to worry about merging its subreg index with the
1572 // SubIdx passed to this function. The register coalescer should be able to
1573 // eliminate this extra copy.
1574 MachineBasicBlock *MBB = MI->getParent();
1575 DebugLoc DL = MI->getDebugLoc();
1577 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg)
1578 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg());
1580 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg)
1581 .addReg(NewSuperReg, 0, SubIdx);
1586 MachineOperand SIInstrInfo::buildExtractSubRegOrImm(
1587 MachineBasicBlock::iterator MII,
1588 MachineRegisterInfo &MRI,
1590 const TargetRegisterClass *SuperRC,
1592 const TargetRegisterClass *SubRC) const {
1594 // XXX - Is there a better way to do this?
1595 if (SubIdx == AMDGPU::sub0)
1596 return MachineOperand::CreateImm(Op.getImm() & 0xFFFFFFFF);
1597 if (SubIdx == AMDGPU::sub1)
1598 return MachineOperand::CreateImm(Op.getImm() >> 32);
1600 llvm_unreachable("Unhandled register index for immediate");
1603 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
1605 return MachineOperand::CreateReg(SubReg, false);
1608 // Change the order of operands from (0, 1, 2) to (0, 2, 1)
1609 void SIInstrInfo::swapOperands(MachineBasicBlock::iterator Inst) const {
1610 assert(Inst->getNumExplicitOperands() == 3);
1611 MachineOperand Op1 = Inst->getOperand(1);
1612 Inst->RemoveOperand(1);
1613 Inst->addOperand(Op1);
1616 bool SIInstrInfo::isOperandLegal(const MachineInstr *MI, unsigned OpIdx,
1617 const MachineOperand *MO) const {
1618 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1619 const MCInstrDesc &InstDesc = get(MI->getOpcode());
1620 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx];
1621 const TargetRegisterClass *DefinedRC =
1622 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr;
1624 MO = &MI->getOperand(OpIdx);
1626 if (isVALU(InstDesc.Opcode) &&
1627 usesConstantBus(MRI, *MO, DefinedRC->getSize())) {
1629 MO->isReg() ? MO->getReg() : (unsigned)AMDGPU::NoRegister;
1630 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1633 const MachineOperand &Op = MI->getOperand(i);
1634 if (Op.isReg() && Op.getReg() != SGPRUsed &&
1635 usesConstantBus(MRI, Op, getOpSize(*MI, i))) {
1643 const TargetRegisterClass *RC =
1644 TargetRegisterInfo::isVirtualRegister(MO->getReg()) ?
1645 MRI.getRegClass(MO->getReg()) :
1646 RI.getPhysRegClass(MO->getReg());
1648 // In order to be legal, the common sub-class must be equal to the
1649 // class of the current operand. For example:
1651 // v_mov_b32 s0 ; Operand defined as vsrc_32
1652 // ; RI.getCommonSubClass(s0,vsrc_32) = sgpr ; LEGAL
1654 // s_sendmsg 0, s0 ; Operand defined as m0reg
1655 // ; RI.getCommonSubClass(s0,m0reg) = m0reg ; NOT LEGAL
1657 return RI.getCommonSubClass(RC, RI.getRegClass(OpInfo.RegClass)) == RC;
1661 // Handle non-register types that are treated like immediates.
1662 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI());
1665 // This operand expects an immediate.
1669 return isImmOperandLegal(MI, OpIdx, *MO);
1672 void SIInstrInfo::legalizeOperands(MachineInstr *MI) const {
1673 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1675 int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
1676 AMDGPU::OpName::src0);
1677 int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
1678 AMDGPU::OpName::src1);
1679 int Src2Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
1680 AMDGPU::OpName::src2);
1683 if (isVOP2(MI->getOpcode()) && Src1Idx != -1) {
1685 if (!isOperandLegal(MI, Src0Idx))
1686 legalizeOpWithMove(MI, Src0Idx);
1689 if (isOperandLegal(MI, Src1Idx))
1692 // Usually src0 of VOP2 instructions allow more types of inputs
1693 // than src1, so try to commute the instruction to decrease our
1694 // chances of having to insert a MOV instruction to legalize src1.
1695 if (MI->isCommutable()) {
1696 if (commuteInstruction(MI))
1697 // If we are successful in commuting, then we know MI is legal, so
1702 legalizeOpWithMove(MI, Src1Idx);
1706 // XXX - Do any VOP3 instructions read VCC?
1708 if (isVOP3(MI->getOpcode())) {
1709 int VOP3Idx[3] = { Src0Idx, Src1Idx, Src2Idx };
1711 // Find the one SGPR operand we are allowed to use.
1712 unsigned SGPRReg = findUsedSGPR(MI, VOP3Idx);
1714 for (unsigned i = 0; i < 3; ++i) {
1715 int Idx = VOP3Idx[i];
1718 MachineOperand &MO = MI->getOperand(Idx);
1721 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg())))
1722 continue; // VGPRs are legal
1724 assert(MO.getReg() != AMDGPU::SCC && "SCC operand to VOP3 instruction");
1726 if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) {
1727 SGPRReg = MO.getReg();
1728 // We can use one SGPR in each VOP3 instruction.
1731 } else if (!isLiteralConstant(MO, getOpSize(MI->getOpcode(), Idx))) {
1732 // If it is not a register and not a literal constant, then it must be
1733 // an inline constant which is always legal.
1736 // If we make it this far, then the operand is not legal and we must
1738 legalizeOpWithMove(MI, Idx);
1742 // Legalize REG_SEQUENCE and PHI
1743 // The register class of the operands much be the same type as the register
1744 // class of the output.
1745 if (MI->getOpcode() == AMDGPU::REG_SEQUENCE ||
1746 MI->getOpcode() == AMDGPU::PHI) {
1747 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr;
1748 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
1749 if (!MI->getOperand(i).isReg() ||
1750 !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
1752 const TargetRegisterClass *OpRC =
1753 MRI.getRegClass(MI->getOperand(i).getReg());
1754 if (RI.hasVGPRs(OpRC)) {
1761 // If any of the operands are VGPR registers, then they all most be
1762 // otherwise we will create illegal VGPR->SGPR copies when legalizing
1764 if (VRC || !RI.isSGPRClass(getOpRegClass(*MI, 0))) {
1767 VRC = RI.getEquivalentVGPRClass(SRC);
1774 // Update all the operands so they have the same type.
1775 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
1776 if (!MI->getOperand(i).isReg() ||
1777 !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
1779 unsigned DstReg = MRI.createVirtualRegister(RC);
1780 MachineBasicBlock *InsertBB;
1781 MachineBasicBlock::iterator Insert;
1782 if (MI->getOpcode() == AMDGPU::REG_SEQUENCE) {
1783 InsertBB = MI->getParent();
1786 // MI is a PHI instruction.
1787 InsertBB = MI->getOperand(i + 1).getMBB();
1788 Insert = InsertBB->getFirstTerminator();
1790 BuildMI(*InsertBB, Insert, MI->getDebugLoc(),
1791 get(AMDGPU::COPY), DstReg)
1792 .addOperand(MI->getOperand(i));
1793 MI->getOperand(i).setReg(DstReg);
1797 // Legalize INSERT_SUBREG
1798 // src0 must have the same register class as dst
1799 if (MI->getOpcode() == AMDGPU::INSERT_SUBREG) {
1800 unsigned Dst = MI->getOperand(0).getReg();
1801 unsigned Src0 = MI->getOperand(1).getReg();
1802 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst);
1803 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0);
1804 if (DstRC != Src0RC) {
1805 MachineBasicBlock &MBB = *MI->getParent();
1806 unsigned NewSrc0 = MRI.createVirtualRegister(DstRC);
1807 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::COPY), NewSrc0)
1809 MI->getOperand(1).setReg(NewSrc0);
1814 // Legalize MUBUF* instructions
1815 // FIXME: If we start using the non-addr64 instructions for compute, we
1816 // may need to legalize them here.
1818 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::srsrc);
1819 if (SRsrcIdx != -1) {
1820 // We have an MUBUF instruction
1821 MachineOperand *SRsrc = &MI->getOperand(SRsrcIdx);
1822 unsigned SRsrcRC = get(MI->getOpcode()).OpInfo[SRsrcIdx].RegClass;
1823 if (RI.getCommonSubClass(MRI.getRegClass(SRsrc->getReg()),
1824 RI.getRegClass(SRsrcRC))) {
1825 // The operands are legal.
1826 // FIXME: We may need to legalize operands besided srsrc.
1830 MachineBasicBlock &MBB = *MI->getParent();
1831 // Extract the ptr from the resource descriptor.
1833 // SRsrcPtrLo = srsrc:sub0
1834 unsigned SRsrcPtrLo = buildExtractSubReg(MI, MRI, *SRsrc,
1835 &AMDGPU::VReg_128RegClass, AMDGPU::sub0, &AMDGPU::VGPR_32RegClass);
1837 // SRsrcPtrHi = srsrc:sub1
1838 unsigned SRsrcPtrHi = buildExtractSubReg(MI, MRI, *SRsrc,
1839 &AMDGPU::VReg_128RegClass, AMDGPU::sub1, &AMDGPU::VGPR_32RegClass);
1841 // Create an empty resource descriptor
1842 unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1843 unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1844 unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1845 unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass);
1846 uint64_t RsrcDataFormat = getDefaultRsrcDataFormat();
1849 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B64),
1853 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
1854 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
1856 .addImm(RsrcDataFormat & 0xFFFFFFFF);
1858 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
1859 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
1861 .addImm(RsrcDataFormat >> 32);
1863 // NewSRsrc = {Zero64, SRsrcFormat}
1864 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
1867 .addImm(AMDGPU::sub0_sub1)
1868 .addReg(SRsrcFormatLo)
1869 .addImm(AMDGPU::sub2)
1870 .addReg(SRsrcFormatHi)
1871 .addImm(AMDGPU::sub3);
1873 MachineOperand *VAddr = getNamedOperand(*MI, AMDGPU::OpName::vaddr);
1874 unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
1875 unsigned NewVAddrLo;
1876 unsigned NewVAddrHi;
1878 // This is already an ADDR64 instruction so we need to add the pointer
1879 // extracted from the resource descriptor to the current value of VAddr.
1880 NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1881 NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1883 // NewVaddrLo = SRsrcPtrLo + VAddr:sub0
1884 DebugLoc DL = MI->getDebugLoc();
1885 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e32), NewVAddrLo)
1887 .addReg(VAddr->getReg(), 0, AMDGPU::sub0);
1889 // NewVaddrHi = SRsrcPtrHi + VAddr:sub1
1890 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e32), NewVAddrHi)
1892 .addReg(VAddr->getReg(), 0, AMDGPU::sub1);
1895 // This instructions is the _OFFSET variant, so we need to convert it to
1897 MachineOperand *VData = getNamedOperand(*MI, AMDGPU::OpName::vdata);
1898 MachineOperand *Offset = getNamedOperand(*MI, AMDGPU::OpName::offset);
1899 MachineOperand *SOffset = getNamedOperand(*MI, AMDGPU::OpName::soffset);
1901 // Create the new instruction.
1902 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI->getOpcode());
1903 MachineInstr *Addr64 =
1904 BuildMI(MBB, MI, MI->getDebugLoc(), get(Addr64Opcode))
1906 .addReg(AMDGPU::NoRegister) // Dummy value for vaddr.
1907 // This will be replaced later
1908 // with the new value of vaddr.
1910 .addOperand(*SOffset)
1911 .addOperand(*Offset)
1915 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1917 MI->removeFromParent();
1920 NewVAddrLo = SRsrcPtrLo;
1921 NewVAddrHi = SRsrcPtrHi;
1922 VAddr = getNamedOperand(*MI, AMDGPU::OpName::vaddr);
1923 SRsrc = getNamedOperand(*MI, AMDGPU::OpName::srsrc);
1926 // NewVaddr = {NewVaddrHi, NewVaddrLo}
1927 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
1930 .addImm(AMDGPU::sub0)
1932 .addImm(AMDGPU::sub1);
1935 // Update the instruction to use NewVaddr
1936 VAddr->setReg(NewVAddr);
1937 // Update the instruction to use NewSRsrc
1938 SRsrc->setReg(NewSRsrc);
1942 void SIInstrInfo::splitSMRD(MachineInstr *MI,
1943 const TargetRegisterClass *HalfRC,
1944 unsigned HalfImmOp, unsigned HalfSGPROp,
1945 MachineInstr *&Lo, MachineInstr *&Hi) const {
1947 DebugLoc DL = MI->getDebugLoc();
1948 MachineBasicBlock *MBB = MI->getParent();
1949 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1950 unsigned RegLo = MRI.createVirtualRegister(HalfRC);
1951 unsigned RegHi = MRI.createVirtualRegister(HalfRC);
1952 unsigned HalfSize = HalfRC->getSize();
1953 const MachineOperand *OffOp =
1954 getNamedOperand(*MI, AMDGPU::OpName::offset);
1955 const MachineOperand *SBase = getNamedOperand(*MI, AMDGPU::OpName::sbase);
1957 // The SMRD has an 8-bit offset in dwords on SI and a 20-bit offset in bytes
1960 bool IsKill = SBase->isKill();
1963 MBB->getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() >=
1964 AMDGPUSubtarget::VOLCANIC_ISLANDS;
1965 unsigned OffScale = isVI ? 1 : 4;
1966 // Handle the _IMM variant
1967 unsigned LoOffset = OffOp->getImm() * OffScale;
1968 unsigned HiOffset = LoOffset + HalfSize;
1969 Lo = BuildMI(*MBB, MI, DL, get(HalfImmOp), RegLo)
1970 // Use addReg instead of addOperand
1971 // to make sure kill flag is cleared.
1972 .addReg(SBase->getReg(), 0, SBase->getSubReg())
1973 .addImm(LoOffset / OffScale);
1975 if (!isUInt<20>(HiOffset) || (!isVI && !isUInt<8>(HiOffset / OffScale))) {
1976 unsigned OffsetSGPR =
1977 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
1978 BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32), OffsetSGPR)
1979 .addImm(HiOffset); // The offset in register is in bytes.
1980 Hi = BuildMI(*MBB, MI, DL, get(HalfSGPROp), RegHi)
1981 .addReg(SBase->getReg(), getKillRegState(IsKill),
1983 .addReg(OffsetSGPR);
1985 Hi = BuildMI(*MBB, MI, DL, get(HalfImmOp), RegHi)
1986 .addReg(SBase->getReg(), getKillRegState(IsKill),
1988 .addImm(HiOffset / OffScale);
1991 // Handle the _SGPR variant
1992 MachineOperand *SOff = getNamedOperand(*MI, AMDGPU::OpName::soff);
1993 Lo = BuildMI(*MBB, MI, DL, get(HalfSGPROp), RegLo)
1994 .addReg(SBase->getReg(), 0, SBase->getSubReg())
1996 unsigned OffsetSGPR = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
1997 BuildMI(*MBB, MI, DL, get(AMDGPU::S_ADD_I32), OffsetSGPR)
2000 Hi = BuildMI(*MBB, MI, DL, get(HalfSGPROp))
2001 .addReg(SBase->getReg(), getKillRegState(IsKill),
2003 .addReg(OffsetSGPR);
2006 unsigned SubLo, SubHi;
2009 SubLo = AMDGPU::sub0;
2010 SubHi = AMDGPU::sub1;
2013 SubLo = AMDGPU::sub0_sub1;
2014 SubHi = AMDGPU::sub2_sub3;
2017 SubLo = AMDGPU::sub0_sub1_sub2_sub3;
2018 SubHi = AMDGPU::sub4_sub5_sub6_sub7;
2021 SubLo = AMDGPU::sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7;
2022 SubHi = AMDGPU::sub8_sub9_sub10_sub11_sub12_sub13_sub14_sub15;
2025 llvm_unreachable("Unhandled HalfSize");
2028 BuildMI(*MBB, MI, DL, get(AMDGPU::REG_SEQUENCE))
2029 .addOperand(MI->getOperand(0))
2036 void SIInstrInfo::moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI) const {
2037 MachineBasicBlock *MBB = MI->getParent();
2038 int DstIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
2039 assert(DstIdx != -1);
2040 unsigned DstRCID = get(MI->getOpcode()).OpInfo[DstIdx].RegClass;
2041 switch(RI.getRegClass(DstRCID)->getSize()) {
2045 unsigned NewOpcode = getVALUOp(*MI);
2049 if (MI->getOperand(2).isReg()) {
2050 RegOffset = MI->getOperand(2).getReg();
2053 assert(MI->getOperand(2).isImm());
2054 // SMRD instructions take a dword offsets on SI and byte offset on VI
2055 // and MUBUF instructions always take a byte offset.
2056 ImmOffset = MI->getOperand(2).getImm();
2057 if (MBB->getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() <=
2058 AMDGPUSubtarget::SEA_ISLANDS)
2060 RegOffset = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
2062 if (isUInt<12>(ImmOffset)) {
2063 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
2067 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
2074 unsigned SRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass);
2075 unsigned DWord0 = RegOffset;
2076 unsigned DWord1 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
2077 unsigned DWord2 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
2078 unsigned DWord3 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
2079 uint64_t RsrcDataFormat = getDefaultRsrcDataFormat();
2081 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord1)
2083 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord2)
2084 .addImm(RsrcDataFormat & 0xFFFFFFFF);
2085 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord3)
2086 .addImm(RsrcDataFormat >> 32);
2087 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), SRsrc)
2089 .addImm(AMDGPU::sub0)
2091 .addImm(AMDGPU::sub1)
2093 .addImm(AMDGPU::sub2)
2095 .addImm(AMDGPU::sub3);
2096 MI->setDesc(get(NewOpcode));
2097 if (MI->getOperand(2).isReg()) {
2098 MI->getOperand(2).setReg(SRsrc);
2100 MI->getOperand(2).ChangeToRegister(SRsrc, false);
2102 MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(0));
2103 MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(ImmOffset));
2104 MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(0)); // glc
2105 MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(0)); // slc
2106 MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(0)); // tfe
2108 const TargetRegisterClass *NewDstRC =
2109 RI.getRegClass(get(NewOpcode).OpInfo[0].RegClass);
2111 unsigned DstReg = MI->getOperand(0).getReg();
2112 unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC);
2113 MRI.replaceRegWith(DstReg, NewDstReg);
2117 MachineInstr *Lo, *Hi;
2118 splitSMRD(MI, &AMDGPU::SReg_128RegClass, AMDGPU::S_LOAD_DWORDX4_IMM,
2119 AMDGPU::S_LOAD_DWORDX4_SGPR, Lo, Hi);
2120 MI->eraseFromParent();
2121 moveSMRDToVALU(Lo, MRI);
2122 moveSMRDToVALU(Hi, MRI);
2127 MachineInstr *Lo, *Hi;
2128 splitSMRD(MI, &AMDGPU::SReg_256RegClass, AMDGPU::S_LOAD_DWORDX8_IMM,
2129 AMDGPU::S_LOAD_DWORDX8_SGPR, Lo, Hi);
2130 MI->eraseFromParent();
2131 moveSMRDToVALU(Lo, MRI);
2132 moveSMRDToVALU(Hi, MRI);
2138 void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const {
2139 SmallVector<MachineInstr *, 128> Worklist;
2140 Worklist.push_back(&TopInst);
2142 while (!Worklist.empty()) {
2143 MachineInstr *Inst = Worklist.pop_back_val();
2144 MachineBasicBlock *MBB = Inst->getParent();
2145 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2147 unsigned Opcode = Inst->getOpcode();
2148 unsigned NewOpcode = getVALUOp(*Inst);
2150 // Handle some special cases
2153 if (isSMRD(Inst->getOpcode())) {
2154 moveSMRDToVALU(Inst, MRI);
2157 case AMDGPU::S_AND_B64:
2158 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_AND_B32_e64);
2159 Inst->eraseFromParent();
2162 case AMDGPU::S_OR_B64:
2163 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_OR_B32_e64);
2164 Inst->eraseFromParent();
2167 case AMDGPU::S_XOR_B64:
2168 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_XOR_B32_e64);
2169 Inst->eraseFromParent();
2172 case AMDGPU::S_NOT_B64:
2173 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::V_NOT_B32_e32);
2174 Inst->eraseFromParent();
2177 case AMDGPU::S_BCNT1_I32_B64:
2178 splitScalar64BitBCNT(Worklist, Inst);
2179 Inst->eraseFromParent();
2182 case AMDGPU::S_BFE_I64: {
2183 splitScalar64BitBFE(Worklist, Inst);
2184 Inst->eraseFromParent();
2188 case AMDGPU::S_LSHL_B32:
2189 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2190 NewOpcode = AMDGPU::V_LSHLREV_B32_e64;
2194 case AMDGPU::S_ASHR_I32:
2195 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2196 NewOpcode = AMDGPU::V_ASHRREV_I32_e64;
2200 case AMDGPU::S_LSHR_B32:
2201 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2202 NewOpcode = AMDGPU::V_LSHRREV_B32_e64;
2206 case AMDGPU::S_LSHL_B64:
2207 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2208 NewOpcode = AMDGPU::V_LSHLREV_B64;
2212 case AMDGPU::S_ASHR_I64:
2213 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2214 NewOpcode = AMDGPU::V_ASHRREV_I64;
2218 case AMDGPU::S_LSHR_B64:
2219 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
2220 NewOpcode = AMDGPU::V_LSHRREV_B64;
2225 case AMDGPU::S_BFE_U64:
2226 case AMDGPU::S_BFM_B64:
2227 llvm_unreachable("Moving this op to VALU not implemented");
2230 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
2231 // We cannot move this instruction to the VALU, so we should try to
2232 // legalize its operands instead.
2233 legalizeOperands(Inst);
2237 // Use the new VALU Opcode.
2238 const MCInstrDesc &NewDesc = get(NewOpcode);
2239 Inst->setDesc(NewDesc);
2241 // Remove any references to SCC. Vector instructions can't read from it, and
2242 // We're just about to add the implicit use / defs of VCC, and we don't want
2244 for (unsigned i = Inst->getNumOperands() - 1; i > 0; --i) {
2245 MachineOperand &Op = Inst->getOperand(i);
2246 if (Op.isReg() && Op.getReg() == AMDGPU::SCC)
2247 Inst->RemoveOperand(i);
2250 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) {
2251 // We are converting these to a BFE, so we need to add the missing
2252 // operands for the size and offset.
2253 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16;
2254 Inst->addOperand(MachineOperand::CreateImm(0));
2255 Inst->addOperand(MachineOperand::CreateImm(Size));
2257 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) {
2258 // The VALU version adds the second operand to the result, so insert an
2260 Inst->addOperand(MachineOperand::CreateImm(0));
2263 Inst->addImplicitDefUseOperands(*Inst->getParent()->getParent());
2265 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) {
2266 const MachineOperand &OffsetWidthOp = Inst->getOperand(2);
2267 // If we need to move this to VGPRs, we need to unpack the second operand
2268 // back into the 2 separate ones for bit offset and width.
2269 assert(OffsetWidthOp.isImm() &&
2270 "Scalar BFE is only implemented for constant width and offset");
2271 uint32_t Imm = OffsetWidthOp.getImm();
2273 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
2274 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
2275 Inst->RemoveOperand(2); // Remove old immediate.
2276 Inst->addOperand(MachineOperand::CreateImm(Offset));
2277 Inst->addOperand(MachineOperand::CreateImm(BitWidth));
2280 // Update the destination register class.
2282 const TargetRegisterClass *NewDstRC = getOpRegClass(*Inst, 0);
2285 // For target instructions, getOpRegClass just returns the virtual
2286 // register class associated with the operand, so we need to find an
2287 // equivalent VGPR register class in order to move the instruction to the
2291 case AMDGPU::REG_SEQUENCE:
2292 case AMDGPU::INSERT_SUBREG:
2293 if (RI.hasVGPRs(NewDstRC))
2295 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
2303 unsigned DstReg = Inst->getOperand(0).getReg();
2304 unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC);
2305 MRI.replaceRegWith(DstReg, NewDstReg);
2307 // Legalize the operands
2308 legalizeOperands(Inst);
2310 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist);
2314 //===----------------------------------------------------------------------===//
2315 // Indirect addressing callbacks
2316 //===----------------------------------------------------------------------===//
2318 unsigned SIInstrInfo::calculateIndirectAddress(unsigned RegIndex,
2319 unsigned Channel) const {
2320 assert(Channel == 0);
2324 const TargetRegisterClass *SIInstrInfo::getIndirectAddrRegClass() const {
2325 return &AMDGPU::VGPR_32RegClass;
2328 void SIInstrInfo::splitScalar64BitUnaryOp(
2329 SmallVectorImpl<MachineInstr *> &Worklist,
2331 unsigned Opcode) const {
2332 MachineBasicBlock &MBB = *Inst->getParent();
2333 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2335 MachineOperand &Dest = Inst->getOperand(0);
2336 MachineOperand &Src0 = Inst->getOperand(1);
2337 DebugLoc DL = Inst->getDebugLoc();
2339 MachineBasicBlock::iterator MII = Inst;
2341 const MCInstrDesc &InstDesc = get(Opcode);
2342 const TargetRegisterClass *Src0RC = Src0.isReg() ?
2343 MRI.getRegClass(Src0.getReg()) :
2344 &AMDGPU::SGPR_32RegClass;
2346 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
2348 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2349 AMDGPU::sub0, Src0SubRC);
2351 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
2352 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
2353 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
2355 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
2356 BuildMI(MBB, MII, DL, InstDesc, DestSub0)
2357 .addOperand(SrcReg0Sub0);
2359 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2360 AMDGPU::sub1, Src0SubRC);
2362 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
2363 BuildMI(MBB, MII, DL, InstDesc, DestSub1)
2364 .addOperand(SrcReg0Sub1);
2366 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC);
2367 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
2369 .addImm(AMDGPU::sub0)
2371 .addImm(AMDGPU::sub1);
2373 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
2375 // We don't need to legalizeOperands here because for a single operand, src0
2376 // will support any kind of input.
2378 // Move all users of this moved value.
2379 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
2382 void SIInstrInfo::splitScalar64BitBinaryOp(
2383 SmallVectorImpl<MachineInstr *> &Worklist,
2385 unsigned Opcode) const {
2386 MachineBasicBlock &MBB = *Inst->getParent();
2387 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2389 MachineOperand &Dest = Inst->getOperand(0);
2390 MachineOperand &Src0 = Inst->getOperand(1);
2391 MachineOperand &Src1 = Inst->getOperand(2);
2392 DebugLoc DL = Inst->getDebugLoc();
2394 MachineBasicBlock::iterator MII = Inst;
2396 const MCInstrDesc &InstDesc = get(Opcode);
2397 const TargetRegisterClass *Src0RC = Src0.isReg() ?
2398 MRI.getRegClass(Src0.getReg()) :
2399 &AMDGPU::SGPR_32RegClass;
2401 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
2402 const TargetRegisterClass *Src1RC = Src1.isReg() ?
2403 MRI.getRegClass(Src1.getReg()) :
2404 &AMDGPU::SGPR_32RegClass;
2406 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
2408 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2409 AMDGPU::sub0, Src0SubRC);
2410 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
2411 AMDGPU::sub0, Src1SubRC);
2413 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
2414 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC);
2415 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0);
2417 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC);
2418 MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0)
2419 .addOperand(SrcReg0Sub0)
2420 .addOperand(SrcReg1Sub0);
2422 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
2423 AMDGPU::sub1, Src0SubRC);
2424 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
2425 AMDGPU::sub1, Src1SubRC);
2427 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC);
2428 MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1)
2429 .addOperand(SrcReg0Sub1)
2430 .addOperand(SrcReg1Sub1);
2432 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC);
2433 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
2435 .addImm(AMDGPU::sub0)
2437 .addImm(AMDGPU::sub1);
2439 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
2441 // Try to legalize the operands in case we need to swap the order to keep it
2443 legalizeOperands(LoHalf);
2444 legalizeOperands(HiHalf);
2446 // Move all users of this moved vlaue.
2447 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist);
2450 void SIInstrInfo::splitScalar64BitBCNT(SmallVectorImpl<MachineInstr *> &Worklist,
2451 MachineInstr *Inst) const {
2452 MachineBasicBlock &MBB = *Inst->getParent();
2453 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2455 MachineBasicBlock::iterator MII = Inst;
2456 DebugLoc DL = Inst->getDebugLoc();
2458 MachineOperand &Dest = Inst->getOperand(0);
2459 MachineOperand &Src = Inst->getOperand(1);
2461 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64);
2462 const TargetRegisterClass *SrcRC = Src.isReg() ?
2463 MRI.getRegClass(Src.getReg()) :
2464 &AMDGPU::SGPR_32RegClass;
2466 unsigned MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2467 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2469 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0);
2471 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
2472 AMDGPU::sub0, SrcSubRC);
2473 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC,
2474 AMDGPU::sub1, SrcSubRC);
2476 BuildMI(MBB, MII, DL, InstDesc, MidReg)
2477 .addOperand(SrcRegSub0)
2480 BuildMI(MBB, MII, DL, InstDesc, ResultReg)
2481 .addOperand(SrcRegSub1)
2484 MRI.replaceRegWith(Dest.getReg(), ResultReg);
2486 // We don't need to legalize operands here. src0 for etiher instruction can be
2487 // an SGPR, and the second input is unused or determined here.
2488 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
2491 void SIInstrInfo::splitScalar64BitBFE(SmallVectorImpl<MachineInstr *> &Worklist,
2492 MachineInstr *Inst) const {
2493 MachineBasicBlock &MBB = *Inst->getParent();
2494 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2495 MachineBasicBlock::iterator MII = Inst;
2496 DebugLoc DL = Inst->getDebugLoc();
2498 MachineOperand &Dest = Inst->getOperand(0);
2499 uint32_t Imm = Inst->getOperand(2).getImm();
2500 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
2501 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
2505 // Only sext_inreg cases handled.
2506 assert(Inst->getOpcode() == AMDGPU::S_BFE_I64 &&
2511 if (BitWidth < 32) {
2512 unsigned MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2513 unsigned MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2514 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
2516 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32), MidRegLo)
2517 .addReg(Inst->getOperand(1).getReg(), 0, AMDGPU::sub0)
2521 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi)
2525 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
2527 .addImm(AMDGPU::sub0)
2529 .addImm(AMDGPU::sub1);
2531 MRI.replaceRegWith(Dest.getReg(), ResultReg);
2532 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
2536 MachineOperand &Src = Inst->getOperand(1);
2537 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
2538 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
2540 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg)
2542 .addReg(Src.getReg(), 0, AMDGPU::sub0);
2544 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg)
2545 .addReg(Src.getReg(), 0, AMDGPU::sub0)
2546 .addImm(AMDGPU::sub0)
2548 .addImm(AMDGPU::sub1);
2550 MRI.replaceRegWith(Dest.getReg(), ResultReg);
2551 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist);
2554 void SIInstrInfo::addUsersToMoveToVALUWorklist(
2556 MachineRegisterInfo &MRI,
2557 SmallVectorImpl<MachineInstr *> &Worklist) const {
2558 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg),
2559 E = MRI.use_end(); I != E; ++I) {
2560 MachineInstr &UseMI = *I->getParent();
2561 if (!canReadVGPR(UseMI, I.getOperandNo())) {
2562 Worklist.push_back(&UseMI);
2567 unsigned SIInstrInfo::findUsedSGPR(const MachineInstr *MI,
2568 int OpIndices[3]) const {
2569 const MCInstrDesc &Desc = get(MI->getOpcode());
2571 // Find the one SGPR operand we are allowed to use.
2572 unsigned SGPRReg = AMDGPU::NoRegister;
2574 // First we need to consider the instruction's operand requirements before
2575 // legalizing. Some operands are required to be SGPRs, such as implicit uses
2576 // of VCC, but we are still bound by the constant bus requirement to only use
2579 // If the operand's class is an SGPR, we can never move it.
2581 for (const MachineOperand &MO : MI->implicit_operands()) {
2582 // We only care about reads.
2586 if (MO.getReg() == AMDGPU::VCC)
2589 if (MO.getReg() == AMDGPU::FLAT_SCR)
2590 return AMDGPU::FLAT_SCR;
2593 unsigned UsedSGPRs[3] = { AMDGPU::NoRegister };
2594 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
2596 for (unsigned i = 0; i < 3; ++i) {
2597 int Idx = OpIndices[i];
2601 const MachineOperand &MO = MI->getOperand(Idx);
2602 if (RI.isSGPRClassID(Desc.OpInfo[Idx].RegClass))
2603 SGPRReg = MO.getReg();
2605 if (MO.isReg() && RI.isSGPRClass(MRI.getRegClass(MO.getReg())))
2606 UsedSGPRs[i] = MO.getReg();
2609 if (SGPRReg != AMDGPU::NoRegister)
2612 // We don't have a required SGPR operand, so we have a bit more freedom in
2613 // selecting operands to move.
2615 // Try to select the most used SGPR. If an SGPR is equal to one of the
2616 // others, we choose that.
2619 // V_FMA_F32 v0, s0, s0, s0 -> No moves
2620 // V_FMA_F32 v0, s0, s1, s0 -> Move s1
2622 if (UsedSGPRs[0] != AMDGPU::NoRegister) {
2623 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2])
2624 SGPRReg = UsedSGPRs[0];
2627 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) {
2628 if (UsedSGPRs[1] == UsedSGPRs[2])
2629 SGPRReg = UsedSGPRs[1];
2635 MachineInstrBuilder SIInstrInfo::buildIndirectWrite(
2636 MachineBasicBlock *MBB,
2637 MachineBasicBlock::iterator I,
2639 unsigned Address, unsigned OffsetReg) const {
2640 const DebugLoc &DL = MBB->findDebugLoc(I);
2641 unsigned IndirectBaseReg = AMDGPU::VGPR_32RegClass.getRegister(
2642 getIndirectIndexBegin(*MBB->getParent()));
2644 return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_DST_V1))
2645 .addReg(IndirectBaseReg, RegState::Define)
2646 .addOperand(I->getOperand(0))
2647 .addReg(IndirectBaseReg)
2653 MachineInstrBuilder SIInstrInfo::buildIndirectRead(
2654 MachineBasicBlock *MBB,
2655 MachineBasicBlock::iterator I,
2657 unsigned Address, unsigned OffsetReg) const {
2658 const DebugLoc &DL = MBB->findDebugLoc(I);
2659 unsigned IndirectBaseReg = AMDGPU::VGPR_32RegClass.getRegister(
2660 getIndirectIndexBegin(*MBB->getParent()));
2662 return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_SRC))
2663 .addOperand(I->getOperand(0))
2664 .addOperand(I->getOperand(1))
2665 .addReg(IndirectBaseReg)
2671 void SIInstrInfo::reserveIndirectRegisters(BitVector &Reserved,
2672 const MachineFunction &MF) const {
2673 int End = getIndirectIndexEnd(MF);
2674 int Begin = getIndirectIndexBegin(MF);
2680 for (int Index = Begin; Index <= End; ++Index)
2681 Reserved.set(AMDGPU::VGPR_32RegClass.getRegister(Index));
2683 for (int Index = std::max(0, Begin - 1); Index <= End; ++Index)
2684 Reserved.set(AMDGPU::VReg_64RegClass.getRegister(Index));
2686 for (int Index = std::max(0, Begin - 2); Index <= End; ++Index)
2687 Reserved.set(AMDGPU::VReg_96RegClass.getRegister(Index));
2689 for (int Index = std::max(0, Begin - 3); Index <= End; ++Index)
2690 Reserved.set(AMDGPU::VReg_128RegClass.getRegister(Index));
2692 for (int Index = std::max(0, Begin - 7); Index <= End; ++Index)
2693 Reserved.set(AMDGPU::VReg_256RegClass.getRegister(Index));
2695 for (int Index = std::max(0, Begin - 15); Index <= End; ++Index)
2696 Reserved.set(AMDGPU::VReg_512RegClass.getRegister(Index));
2699 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI,
2700 unsigned OperandName) const {
2701 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName);
2705 return &MI.getOperand(Idx);
2708 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const {
2709 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT;
2710 if (ST.isAmdHsaOS()) {
2711 RsrcDataFormat |= (1ULL << 56);
2713 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
2715 RsrcDataFormat |= (2ULL << 59);
2718 return RsrcDataFormat;