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/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/MC/MCInstrDesc.h"
26 SIInstrInfo::SIInstrInfo(AMDGPUTargetMachine &tm)
27 : AMDGPUInstrInfo(tm),
30 //===----------------------------------------------------------------------===//
31 // TargetInstrInfo callbacks
32 //===----------------------------------------------------------------------===//
35 SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
36 MachineBasicBlock::iterator MI, DebugLoc DL,
37 unsigned DestReg, unsigned SrcReg,
40 // If we are trying to copy to or from SCC, there is a bug somewhere else in
41 // the backend. While it may be theoretically possible to do this, it should
42 // never be necessary.
43 assert(DestReg != AMDGPU::SCC && SrcReg != AMDGPU::SCC);
45 static const int16_t Sub0_15[] = {
46 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
47 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
48 AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11,
49 AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15, 0
52 static const int16_t Sub0_7[] = {
53 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
54 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, 0
57 static const int16_t Sub0_3[] = {
58 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, 0
61 static const int16_t Sub0_2[] = {
62 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, 0
65 static const int16_t Sub0_1[] = {
66 AMDGPU::sub0, AMDGPU::sub1, 0
70 const int16_t *SubIndices;
72 if (AMDGPU::M0 == DestReg) {
73 // Check if M0 isn't already set to this value
74 for (MachineBasicBlock::reverse_iterator E = MBB.rend(),
75 I = MachineBasicBlock::reverse_iterator(MI); I != E; ++I) {
77 if (!I->definesRegister(AMDGPU::M0))
80 unsigned Opc = I->getOpcode();
81 if (Opc != TargetOpcode::COPY && Opc != AMDGPU::S_MOV_B32)
84 if (!I->readsRegister(SrcReg))
87 // The copy isn't necessary
92 if (AMDGPU::SReg_32RegClass.contains(DestReg)) {
93 assert(AMDGPU::SReg_32RegClass.contains(SrcReg));
94 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg)
95 .addReg(SrcReg, getKillRegState(KillSrc));
98 } else if (AMDGPU::SReg_64RegClass.contains(DestReg)) {
99 assert(AMDGPU::SReg_64RegClass.contains(SrcReg));
100 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
101 .addReg(SrcReg, getKillRegState(KillSrc));
104 } else if (AMDGPU::SReg_128RegClass.contains(DestReg)) {
105 assert(AMDGPU::SReg_128RegClass.contains(SrcReg));
106 Opcode = AMDGPU::S_MOV_B32;
109 } else if (AMDGPU::SReg_256RegClass.contains(DestReg)) {
110 assert(AMDGPU::SReg_256RegClass.contains(SrcReg));
111 Opcode = AMDGPU::S_MOV_B32;
114 } else if (AMDGPU::SReg_512RegClass.contains(DestReg)) {
115 assert(AMDGPU::SReg_512RegClass.contains(SrcReg));
116 Opcode = AMDGPU::S_MOV_B32;
117 SubIndices = Sub0_15;
119 } else if (AMDGPU::VReg_32RegClass.contains(DestReg)) {
120 assert(AMDGPU::VReg_32RegClass.contains(SrcReg) ||
121 AMDGPU::SReg_32RegClass.contains(SrcReg));
122 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg)
123 .addReg(SrcReg, getKillRegState(KillSrc));
126 } else if (AMDGPU::VReg_64RegClass.contains(DestReg)) {
127 assert(AMDGPU::VReg_64RegClass.contains(SrcReg) ||
128 AMDGPU::SReg_64RegClass.contains(SrcReg));
129 Opcode = AMDGPU::V_MOV_B32_e32;
132 } else if (AMDGPU::VReg_96RegClass.contains(DestReg)) {
133 assert(AMDGPU::VReg_96RegClass.contains(SrcReg));
134 Opcode = AMDGPU::V_MOV_B32_e32;
137 } else if (AMDGPU::VReg_128RegClass.contains(DestReg)) {
138 assert(AMDGPU::VReg_128RegClass.contains(SrcReg) ||
139 AMDGPU::SReg_128RegClass.contains(SrcReg));
140 Opcode = AMDGPU::V_MOV_B32_e32;
143 } else if (AMDGPU::VReg_256RegClass.contains(DestReg)) {
144 assert(AMDGPU::VReg_256RegClass.contains(SrcReg) ||
145 AMDGPU::SReg_256RegClass.contains(SrcReg));
146 Opcode = AMDGPU::V_MOV_B32_e32;
149 } else if (AMDGPU::VReg_512RegClass.contains(DestReg)) {
150 assert(AMDGPU::VReg_512RegClass.contains(SrcReg) ||
151 AMDGPU::SReg_512RegClass.contains(SrcReg));
152 Opcode = AMDGPU::V_MOV_B32_e32;
153 SubIndices = Sub0_15;
156 llvm_unreachable("Can't copy register!");
159 while (unsigned SubIdx = *SubIndices++) {
160 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL,
161 get(Opcode), RI.getSubReg(DestReg, SubIdx));
163 Builder.addReg(RI.getSubReg(SrcReg, SubIdx), getKillRegState(KillSrc));
166 Builder.addReg(DestReg, RegState::Define | RegState::Implicit);
170 unsigned SIInstrInfo::commuteOpcode(unsigned Opcode) const {
173 // Try to map original to commuted opcode
174 if ((NewOpc = AMDGPU::getCommuteRev(Opcode)) != -1)
177 // Try to map commuted to original opcode
178 if ((NewOpc = AMDGPU::getCommuteOrig(Opcode)) != -1)
184 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
185 MachineBasicBlock::iterator MI,
186 unsigned SrcReg, bool isKill,
188 const TargetRegisterClass *RC,
189 const TargetRegisterInfo *TRI) const {
190 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
191 SIMachineFunctionInfo *MFI = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
192 DebugLoc DL = MBB.findDebugLoc(MI);
193 unsigned KillFlag = isKill ? RegState::Kill : 0;
195 if (TRI->getCommonSubClass(RC, &AMDGPU::SGPR_32RegClass)) {
196 unsigned Lane = MFI->SpillTracker.getNextLane(MRI);
197 BuildMI(MBB, MI, DL, get(AMDGPU::V_WRITELANE_B32),
198 MFI->SpillTracker.LaneVGPR)
199 .addReg(SrcReg, KillFlag)
201 MFI->SpillTracker.addSpilledReg(FrameIndex, MFI->SpillTracker.LaneVGPR,
204 for (unsigned i = 0, e = RC->getSize() / 4; i != e; ++i) {
205 unsigned SubReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
206 BuildMI(MBB, MI, MBB.findDebugLoc(MI), get(AMDGPU::COPY), SubReg)
207 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i));
208 storeRegToStackSlot(MBB, MI, SubReg, isKill, FrameIndex + i,
209 &AMDGPU::SReg_32RegClass, TRI);
214 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
215 MachineBasicBlock::iterator MI,
216 unsigned DestReg, int FrameIndex,
217 const TargetRegisterClass *RC,
218 const TargetRegisterInfo *TRI) const {
219 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
220 SIMachineFunctionInfo *MFI = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
221 DebugLoc DL = MBB.findDebugLoc(MI);
222 if (TRI->getCommonSubClass(RC, &AMDGPU::SReg_32RegClass)) {
223 SIMachineFunctionInfo::SpilledReg Spill =
224 MFI->SpillTracker.getSpilledReg(FrameIndex);
226 BuildMI(MBB, MI, DL, get(AMDGPU::V_READLANE_B32), DestReg)
230 for (unsigned i = 0, e = RC->getSize() / 4; i != e; ++i) {
231 unsigned Flags = RegState::Define;
233 Flags |= RegState::Undef;
235 unsigned SubReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
236 loadRegFromStackSlot(MBB, MI, SubReg, FrameIndex + i,
237 &AMDGPU::SReg_32RegClass, TRI);
238 BuildMI(MBB, MI, DL, get(AMDGPU::COPY))
239 .addReg(DestReg, Flags, RI.getSubRegFromChannel(i))
245 MachineInstr *SIInstrInfo::commuteInstruction(MachineInstr *MI,
248 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
249 if (MI->getNumOperands() < 3 || !MI->getOperand(1).isReg())
252 // Cannot commute VOP2 if src0 is SGPR.
253 if (isVOP2(MI->getOpcode()) && MI->getOperand(1).isReg() &&
254 RI.isSGPRClass(MRI.getRegClass(MI->getOperand(1).getReg())))
257 if (!MI->getOperand(2).isReg()) {
258 // XXX: Commute instructions with FPImm operands
259 if (NewMI || MI->getOperand(2).isFPImm() ||
260 (!isVOP2(MI->getOpcode()) && !isVOP3(MI->getOpcode()))) {
264 // XXX: Commute VOP3 instructions with abs and neg set.
265 if (isVOP3(MI->getOpcode()) &&
266 (MI->getOperand(AMDGPU::getNamedOperandIdx(MI->getOpcode(),
267 AMDGPU::OpName::abs)).getImm() ||
268 MI->getOperand(AMDGPU::getNamedOperandIdx(MI->getOpcode(),
269 AMDGPU::OpName::neg)).getImm()))
272 unsigned Reg = MI->getOperand(1).getReg();
273 unsigned SubReg = MI->getOperand(1).getSubReg();
274 MI->getOperand(1).ChangeToImmediate(MI->getOperand(2).getImm());
275 MI->getOperand(2).ChangeToRegister(Reg, false);
276 MI->getOperand(2).setSubReg(SubReg);
278 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
282 MI->setDesc(get(commuteOpcode(MI->getOpcode())));
287 MachineInstr *SIInstrInfo::buildMovInstr(MachineBasicBlock *MBB,
288 MachineBasicBlock::iterator I,
290 unsigned SrcReg) const {
291 return BuildMI(*MBB, I, MBB->findDebugLoc(I), get(AMDGPU::V_MOV_B32_e32),
292 DstReg) .addReg(SrcReg);
295 bool SIInstrInfo::isMov(unsigned Opcode) const {
297 default: return false;
298 case AMDGPU::S_MOV_B32:
299 case AMDGPU::S_MOV_B64:
300 case AMDGPU::V_MOV_B32_e32:
301 case AMDGPU::V_MOV_B32_e64:
307 SIInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
308 return RC != &AMDGPU::EXECRegRegClass;
312 SIInstrInfo::isTriviallyReMaterializable(const MachineInstr *MI,
313 AliasAnalysis *AA) const {
314 switch(MI->getOpcode()) {
315 default: return AMDGPUInstrInfo::isTriviallyReMaterializable(MI, AA);
316 case AMDGPU::S_MOV_B32:
317 case AMDGPU::S_MOV_B64:
318 case AMDGPU::V_MOV_B32_e32:
319 return MI->getOperand(1).isImm();
325 // Helper function generated by tablegen. We are wrapping this with
326 // an SIInstrInfo function that reutrns bool rather than int.
327 int isDS(uint16_t Opcode);
331 bool SIInstrInfo::isDS(uint16_t Opcode) const {
332 return ::AMDGPU::isDS(Opcode) != -1;
335 int SIInstrInfo::isMIMG(uint16_t Opcode) const {
336 return get(Opcode).TSFlags & SIInstrFlags::MIMG;
339 int SIInstrInfo::isSMRD(uint16_t Opcode) const {
340 return get(Opcode).TSFlags & SIInstrFlags::SMRD;
343 bool SIInstrInfo::isVOP1(uint16_t Opcode) const {
344 return get(Opcode).TSFlags & SIInstrFlags::VOP1;
347 bool SIInstrInfo::isVOP2(uint16_t Opcode) const {
348 return get(Opcode).TSFlags & SIInstrFlags::VOP2;
351 bool SIInstrInfo::isVOP3(uint16_t Opcode) const {
352 return get(Opcode).TSFlags & SIInstrFlags::VOP3;
355 bool SIInstrInfo::isVOPC(uint16_t Opcode) const {
356 return get(Opcode).TSFlags & SIInstrFlags::VOPC;
359 bool SIInstrInfo::isSALUInstr(const MachineInstr &MI) const {
360 return get(MI.getOpcode()).TSFlags & SIInstrFlags::SALU;
363 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const {
364 int32_t Val = Imm.getSExtValue();
365 if (Val >= -16 && Val <= 64)
368 // The actual type of the operand does not seem to matter as long
369 // as the bits match one of the inline immediate values. For example:
371 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
372 // so it is a legal inline immediate.
374 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
375 // floating-point, so it is a legal inline immediate.
377 return (APInt::floatToBits(0.0f) == Imm) ||
378 (APInt::floatToBits(1.0f) == Imm) ||
379 (APInt::floatToBits(-1.0f) == Imm) ||
380 (APInt::floatToBits(0.5f) == Imm) ||
381 (APInt::floatToBits(-0.5f) == Imm) ||
382 (APInt::floatToBits(2.0f) == Imm) ||
383 (APInt::floatToBits(-2.0f) == Imm) ||
384 (APInt::floatToBits(4.0f) == Imm) ||
385 (APInt::floatToBits(-4.0f) == Imm);
388 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO) const {
390 return isInlineConstant(APInt(32, MO.getImm(), true));
393 APFloat FpImm = MO.getFPImm()->getValueAPF();
394 return isInlineConstant(FpImm.bitcastToAPInt());
400 bool SIInstrInfo::isLiteralConstant(const MachineOperand &MO) const {
401 return (MO.isImm() || MO.isFPImm()) && !isInlineConstant(MO);
404 bool SIInstrInfo::verifyInstruction(const MachineInstr *MI,
405 StringRef &ErrInfo) const {
406 uint16_t Opcode = MI->getOpcode();
407 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
408 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
409 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
411 // Make sure the number of operands is correct.
412 const MCInstrDesc &Desc = get(Opcode);
413 if (!Desc.isVariadic() &&
414 Desc.getNumOperands() != MI->getNumExplicitOperands()) {
415 ErrInfo = "Instruction has wrong number of operands.";
419 // Make sure the register classes are correct
420 for (unsigned i = 0, e = Desc.getNumOperands(); i != e; ++i) {
421 switch (Desc.OpInfo[i].OperandType) {
422 case MCOI::OPERAND_REGISTER:
424 case MCOI::OPERAND_IMMEDIATE:
425 if (!MI->getOperand(i).isImm() && !MI->getOperand(i).isFPImm()) {
426 ErrInfo = "Expected immediate, but got non-immediate";
434 if (!MI->getOperand(i).isReg())
437 int RegClass = Desc.OpInfo[i].RegClass;
438 if (RegClass != -1) {
439 unsigned Reg = MI->getOperand(i).getReg();
440 if (TargetRegisterInfo::isVirtualRegister(Reg))
443 const TargetRegisterClass *RC = RI.getRegClass(RegClass);
444 if (!RC->contains(Reg)) {
445 ErrInfo = "Operand has incorrect register class.";
453 if (isVOP1(Opcode) || isVOP2(Opcode) || isVOP3(Opcode) || isVOPC(Opcode)) {
454 unsigned ConstantBusCount = 0;
455 unsigned SGPRUsed = AMDGPU::NoRegister;
456 for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
457 const MachineOperand &MO = MI->getOperand(i);
458 if (MO.isReg() && MO.isUse() &&
459 !TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
461 // EXEC register uses the constant bus.
462 if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC)
465 // SGPRs use the constant bus
466 if (MO.getReg() == AMDGPU::M0 || MO.getReg() == AMDGPU::VCC ||
468 (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) ||
469 AMDGPU::SGPR_64RegClass.contains(MO.getReg())))) {
470 if (SGPRUsed != MO.getReg()) {
472 SGPRUsed = MO.getReg();
476 // Literal constants use the constant bus.
477 if (isLiteralConstant(MO))
480 if (ConstantBusCount > 1) {
481 ErrInfo = "VOP* instruction uses the constant bus more than once";
486 // Verify SRC1 for VOP2 and VOPC
487 if (Src1Idx != -1 && (isVOP2(Opcode) || isVOPC(Opcode))) {
488 const MachineOperand &Src1 = MI->getOperand(Src1Idx);
489 if (Src1.isImm() || Src1.isFPImm()) {
490 ErrInfo = "VOP[2C] src1 cannot be an immediate.";
496 if (isVOP3(Opcode)) {
497 if (Src0Idx != -1 && isLiteralConstant(MI->getOperand(Src0Idx))) {
498 ErrInfo = "VOP3 src0 cannot be a literal constant.";
501 if (Src1Idx != -1 && isLiteralConstant(MI->getOperand(Src1Idx))) {
502 ErrInfo = "VOP3 src1 cannot be a literal constant.";
505 if (Src2Idx != -1 && isLiteralConstant(MI->getOperand(Src2Idx))) {
506 ErrInfo = "VOP3 src2 cannot be a literal constant.";
513 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) {
514 switch (MI.getOpcode()) {
515 default: return AMDGPU::INSTRUCTION_LIST_END;
516 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE;
517 case AMDGPU::COPY: return AMDGPU::COPY;
518 case AMDGPU::PHI: return AMDGPU::PHI;
519 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG;
520 case AMDGPU::S_MOV_B32:
521 return MI.getOperand(1).isReg() ?
522 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32;
523 case AMDGPU::S_ADD_I32: return AMDGPU::V_ADD_I32_e32;
524 case AMDGPU::S_ADDC_U32: return AMDGPU::V_ADDC_U32_e32;
525 case AMDGPU::S_SUB_I32: return AMDGPU::V_SUB_I32_e32;
526 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32;
527 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e32;
528 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e32;
529 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e32;
530 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e32;
531 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e32;
532 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e32;
533 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e32;
534 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32;
535 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64;
536 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32;
537 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64;
538 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32;
539 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64;
540 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32;
541 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32;
542 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32;
543 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32;
544 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32;
545 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32;
546 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32;
547 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32;
548 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32;
549 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32;
550 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32;
551 case AMDGPU::S_LOAD_DWORD_SGPR: return AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
552 case AMDGPU::S_LOAD_DWORDX2_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
553 case AMDGPU::S_LOAD_DWORDX4_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
557 bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const {
558 return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END;
561 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI,
562 unsigned OpNo) const {
563 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
564 const MCInstrDesc &Desc = get(MI.getOpcode());
565 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
566 Desc.OpInfo[OpNo].RegClass == -1)
567 return MRI.getRegClass(MI.getOperand(OpNo).getReg());
569 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
570 return RI.getRegClass(RCID);
573 bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const {
574 switch (MI.getOpcode()) {
576 case AMDGPU::REG_SEQUENCE:
578 return RI.hasVGPRs(getOpRegClass(MI, 0));
580 return RI.hasVGPRs(getOpRegClass(MI, OpNo));
584 void SIInstrInfo::legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const {
585 MachineBasicBlock::iterator I = MI;
586 MachineOperand &MO = MI->getOperand(OpIdx);
587 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
588 unsigned RCID = get(MI->getOpcode()).OpInfo[OpIdx].RegClass;
589 const TargetRegisterClass *RC = RI.getRegClass(RCID);
590 unsigned Opcode = AMDGPU::V_MOV_B32_e32;
592 Opcode = AMDGPU::COPY;
593 } else if (RI.isSGPRClass(RC)) {
594 Opcode = AMDGPU::S_MOV_B32;
597 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
598 unsigned Reg = MRI.createVirtualRegister(VRC);
599 BuildMI(*MI->getParent(), I, MI->getParent()->findDebugLoc(I), get(Opcode),
601 MO.ChangeToRegister(Reg, false);
604 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI,
605 MachineRegisterInfo &MRI,
606 MachineOperand &SuperReg,
607 const TargetRegisterClass *SuperRC,
609 const TargetRegisterClass *SubRC)
611 assert(SuperReg.isReg());
613 unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC);
614 unsigned SubReg = MRI.createVirtualRegister(SubRC);
616 // Just in case the super register is itself a sub-register, copy it to a new
617 // value so we don't need to wory about merging its subreg index with the
618 // SubIdx passed to this function. The register coalescer should be able to
619 // eliminate this extra copy.
620 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY),
622 .addOperand(SuperReg);
624 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY),
626 .addReg(NewSuperReg, 0, SubIdx);
630 MachineOperand SIInstrInfo::buildExtractSubRegOrImm(
631 MachineBasicBlock::iterator MII,
632 MachineRegisterInfo &MRI,
634 const TargetRegisterClass *SuperRC,
636 const TargetRegisterClass *SubRC) const {
638 // XXX - Is there a better way to do this?
639 if (SubIdx == AMDGPU::sub0)
640 return MachineOperand::CreateImm(Op.getImm() & 0xFFFFFFFF);
641 if (SubIdx == AMDGPU::sub1)
642 return MachineOperand::CreateImm(Op.getImm() >> 32);
644 llvm_unreachable("Unhandled register index for immediate");
647 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
649 return MachineOperand::CreateReg(SubReg, false);
652 unsigned SIInstrInfo::split64BitImm(SmallVectorImpl<MachineInstr *> &Worklist,
653 MachineBasicBlock::iterator MI,
654 MachineRegisterInfo &MRI,
655 const TargetRegisterClass *RC,
656 const MachineOperand &Op) const {
657 MachineBasicBlock *MBB = MI->getParent();
658 DebugLoc DL = MI->getDebugLoc();
659 unsigned LoDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
660 unsigned HiDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
661 unsigned Dst = MRI.createVirtualRegister(RC);
663 MachineInstr *Lo = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32),
665 .addImm(Op.getImm() & 0xFFFFFFFF);
666 MachineInstr *Hi = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32),
668 .addImm(Op.getImm() >> 32);
670 BuildMI(*MBB, MI, DL, get(TargetOpcode::REG_SEQUENCE), Dst)
672 .addImm(AMDGPU::sub0)
674 .addImm(AMDGPU::sub1);
676 Worklist.push_back(Lo);
677 Worklist.push_back(Hi);
682 void SIInstrInfo::legalizeOperands(MachineInstr *MI) const {
683 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
684 int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
685 AMDGPU::OpName::src0);
686 int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
687 AMDGPU::OpName::src1);
688 int Src2Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
689 AMDGPU::OpName::src2);
692 if (isVOP2(MI->getOpcode()) && Src1Idx != -1) {
693 MachineOperand &Src0 = MI->getOperand(Src0Idx);
694 MachineOperand &Src1 = MI->getOperand(Src1Idx);
696 // If the instruction implicitly reads VCC, we can't have any SGPR operands,
698 bool ReadsVCC = MI->readsRegister(AMDGPU::VCC, &RI);
699 if (ReadsVCC && Src0.isReg() &&
700 RI.isSGPRClass(MRI.getRegClass(Src0.getReg()))) {
701 legalizeOpWithMove(MI, Src0Idx);
705 if (ReadsVCC && Src1.isReg() &&
706 RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) {
707 legalizeOpWithMove(MI, Src1Idx);
711 // Legalize VOP2 instructions where src1 is not a VGPR. An SGPR input must
712 // be the first operand, and there can only be one.
713 if (Src1.isImm() || Src1.isFPImm() ||
714 (Src1.isReg() && RI.isSGPRClass(MRI.getRegClass(Src1.getReg())))) {
715 if (MI->isCommutable()) {
716 if (commuteInstruction(MI))
719 legalizeOpWithMove(MI, Src1Idx);
723 // XXX - Do any VOP3 instructions read VCC?
725 if (isVOP3(MI->getOpcode())) {
726 int VOP3Idx[3] = {Src0Idx, Src1Idx, Src2Idx};
727 unsigned SGPRReg = AMDGPU::NoRegister;
728 for (unsigned i = 0; i < 3; ++i) {
729 int Idx = VOP3Idx[i];
732 MachineOperand &MO = MI->getOperand(Idx);
735 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg())))
736 continue; // VGPRs are legal
738 assert(MO.getReg() != AMDGPU::SCC && "SCC operand to VOP3 instruction");
740 if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) {
741 SGPRReg = MO.getReg();
742 // We can use one SGPR in each VOP3 instruction.
745 } else if (!isLiteralConstant(MO)) {
746 // If it is not a register and not a literal constant, then it must be
747 // an inline constant which is always legal.
750 // If we make it this far, then the operand is not legal and we must
752 legalizeOpWithMove(MI, Idx);
756 // Legalize REG_SEQUENCE and PHI
757 // The register class of the operands much be the same type as the register
758 // class of the output.
759 if (MI->getOpcode() == AMDGPU::REG_SEQUENCE ||
760 MI->getOpcode() == AMDGPU::PHI) {
761 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr;
762 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
763 if (!MI->getOperand(i).isReg() ||
764 !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
766 const TargetRegisterClass *OpRC =
767 MRI.getRegClass(MI->getOperand(i).getReg());
768 if (RI.hasVGPRs(OpRC)) {
775 // If any of the operands are VGPR registers, then they all most be
776 // otherwise we will create illegal VGPR->SGPR copies when legalizing
778 if (VRC || !RI.isSGPRClass(getOpRegClass(*MI, 0))) {
781 VRC = RI.getEquivalentVGPRClass(SRC);
788 // Update all the operands so they have the same type.
789 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
790 if (!MI->getOperand(i).isReg() ||
791 !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
793 unsigned DstReg = MRI.createVirtualRegister(RC);
794 MachineBasicBlock *InsertBB;
795 MachineBasicBlock::iterator Insert;
796 if (MI->getOpcode() == AMDGPU::REG_SEQUENCE) {
797 InsertBB = MI->getParent();
800 // MI is a PHI instruction.
801 InsertBB = MI->getOperand(i + 1).getMBB();
802 Insert = InsertBB->getFirstTerminator();
804 BuildMI(*InsertBB, Insert, MI->getDebugLoc(),
805 get(AMDGPU::COPY), DstReg)
806 .addOperand(MI->getOperand(i));
807 MI->getOperand(i).setReg(DstReg);
811 // Legalize MUBUF* instructions
812 // FIXME: If we start using the non-addr64 instructions for compute, we
813 // may need to legalize them here.
815 int SRsrcIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
816 AMDGPU::OpName::srsrc);
817 int VAddrIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
818 AMDGPU::OpName::vaddr);
819 if (SRsrcIdx != -1 && VAddrIdx != -1) {
820 const TargetRegisterClass *VAddrRC =
821 RI.getRegClass(get(MI->getOpcode()).OpInfo[VAddrIdx].RegClass);
823 if(VAddrRC->getSize() == 8 &&
824 MRI.getRegClass(MI->getOperand(SRsrcIdx).getReg()) != VAddrRC) {
825 // We have a MUBUF instruction that uses a 64-bit vaddr register and
826 // srsrc has the incorrect register class. In order to fix this, we
827 // need to extract the pointer from the resource descriptor (srsrc),
828 // add it to the value of vadd, then store the result in the vaddr
829 // operand. Then, we need to set the pointer field of the resource
830 // descriptor to zero.
832 MachineBasicBlock &MBB = *MI->getParent();
833 MachineOperand &SRsrcOp = MI->getOperand(SRsrcIdx);
834 MachineOperand &VAddrOp = MI->getOperand(VAddrIdx);
835 unsigned SRsrcPtrLo, SRsrcPtrHi, VAddrLo, VAddrHi;
836 unsigned NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
837 unsigned NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
838 unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
839 unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
840 unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
841 unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
842 unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass);
844 // SRsrcPtrLo = srsrc:sub0
845 SRsrcPtrLo = buildExtractSubReg(MI, MRI, SRsrcOp,
846 &AMDGPU::VReg_128RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass);
848 // SRsrcPtrHi = srsrc:sub1
849 SRsrcPtrHi = buildExtractSubReg(MI, MRI, SRsrcOp,
850 &AMDGPU::VReg_128RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass);
852 // VAddrLo = vaddr:sub0
853 VAddrLo = buildExtractSubReg(MI, MRI, VAddrOp,
854 &AMDGPU::VReg_64RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass);
856 // VAddrHi = vaddr:sub1
857 VAddrHi = buildExtractSubReg(MI, MRI, VAddrOp,
858 &AMDGPU::VReg_64RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass);
860 // NewVaddrLo = SRsrcPtrLo + VAddrLo
861 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADD_I32_e32),
865 .addReg(AMDGPU::VCC, RegState::Define | RegState::Implicit);
867 // NewVaddrHi = SRsrcPtrHi + VAddrHi
868 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADDC_U32_e32),
872 .addReg(AMDGPU::VCC, RegState::ImplicitDefine)
873 .addReg(AMDGPU::VCC, RegState::Implicit);
875 // NewVaddr = {NewVaddrHi, NewVaddrLo}
876 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
879 .addImm(AMDGPU::sub0)
881 .addImm(AMDGPU::sub1);
884 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B64),
888 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
889 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
891 .addImm(AMDGPU::RSRC_DATA_FORMAT & 0xFFFFFFFF);
893 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
894 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
896 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
898 // NewSRsrc = {Zero64, SRsrcFormat}
899 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
902 .addImm(AMDGPU::sub0_sub1)
903 .addReg(SRsrcFormatLo)
904 .addImm(AMDGPU::sub2)
905 .addReg(SRsrcFormatHi)
906 .addImm(AMDGPU::sub3);
908 // Update the instruction to use NewVaddr
909 MI->getOperand(VAddrIdx).setReg(NewVAddr);
910 // Update the instruction to use NewSRsrc
911 MI->getOperand(SRsrcIdx).setReg(NewSRsrc);
916 void SIInstrInfo::moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI) const {
917 MachineBasicBlock *MBB = MI->getParent();
918 switch (MI->getOpcode()) {
919 case AMDGPU::S_LOAD_DWORD_SGPR:
920 case AMDGPU::S_LOAD_DWORDX2_SGPR:
921 case AMDGPU::S_LOAD_DWORDX4_SGPR:
922 unsigned NewOpcode = getVALUOp(*MI);
923 unsigned Offset = MI->getOperand(2).getReg();
926 unsigned SRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass);
927 unsigned DWord0 = Offset;
928 unsigned DWord1 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
929 unsigned DWord2 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
930 unsigned DWord3 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
932 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord1)
934 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord2)
935 .addImm(AMDGPU::RSRC_DATA_FORMAT & 0xFFFFFFFF);
936 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord3)
937 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
938 BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), SRsrc)
940 .addImm(AMDGPU::sub0)
942 .addImm(AMDGPU::sub1)
944 .addImm(AMDGPU::sub2)
946 .addImm(AMDGPU::sub3);
947 MI->setDesc(get(NewOpcode));
948 MI->getOperand(2).setReg(MI->getOperand(1).getReg());
949 MI->getOperand(1).setReg(SRsrc);
950 MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(0));
954 void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const {
955 SmallVector<MachineInstr *, 128> Worklist;
956 Worklist.push_back(&TopInst);
958 while (!Worklist.empty()) {
959 MachineInstr *Inst = Worklist.pop_back_val();
960 MachineBasicBlock *MBB = Inst->getParent();
961 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
963 unsigned Opcode = Inst->getOpcode();
964 unsigned NewOpcode = getVALUOp(*Inst);
966 // Handle some special cases
969 if (isSMRD(Inst->getOpcode())) {
970 moveSMRDToVALU(Inst, MRI);
973 case AMDGPU::S_MOV_B64: {
974 DebugLoc DL = Inst->getDebugLoc();
976 // If the source operand is a register we can replace this with a
978 if (Inst->getOperand(1).isReg()) {
979 MachineInstr *Copy = BuildMI(*MBB, Inst, DL, get(TargetOpcode::COPY))
980 .addOperand(Inst->getOperand(0))
981 .addOperand(Inst->getOperand(1));
982 Worklist.push_back(Copy);
984 // Otherwise, we need to split this into two movs, because there is
985 // no 64-bit VALU move instruction.
986 unsigned Reg = Inst->getOperand(0).getReg();
987 unsigned Dst = split64BitImm(Worklist,
990 MRI.getRegClass(Reg),
991 Inst->getOperand(1));
992 MRI.replaceRegWith(Reg, Dst);
994 Inst->eraseFromParent();
997 case AMDGPU::S_AND_B64:
998 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_AND_B32);
999 Inst->eraseFromParent();
1002 case AMDGPU::S_OR_B64:
1003 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_OR_B32);
1004 Inst->eraseFromParent();
1007 case AMDGPU::S_XOR_B64:
1008 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_XOR_B32);
1009 Inst->eraseFromParent();
1012 case AMDGPU::S_NOT_B64:
1013 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_NOT_B32);
1014 Inst->eraseFromParent();
1017 case AMDGPU::S_BFE_U64:
1018 case AMDGPU::S_BFE_I64:
1019 case AMDGPU::S_BFM_B64:
1020 llvm_unreachable("Moving this op to VALU not implemented");
1023 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
1024 // We cannot move this instruction to the VALU, so we should try to
1025 // legalize its operands instead.
1026 legalizeOperands(Inst);
1030 // Use the new VALU Opcode.
1031 const MCInstrDesc &NewDesc = get(NewOpcode);
1032 Inst->setDesc(NewDesc);
1034 // Remove any references to SCC. Vector instructions can't read from it, and
1035 // We're just about to add the implicit use / defs of VCC, and we don't want
1037 for (unsigned i = Inst->getNumOperands() - 1; i > 0; --i) {
1038 MachineOperand &Op = Inst->getOperand(i);
1039 if (Op.isReg() && Op.getReg() == AMDGPU::SCC)
1040 Inst->RemoveOperand(i);
1043 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) {
1044 // We are converting these to a BFE, so we need to add the missing
1045 // operands for the size and offset.
1046 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16;
1047 Inst->addOperand(MachineOperand::CreateImm(0));
1048 Inst->addOperand(MachineOperand::CreateImm(Size));
1050 // XXX - Other pointless operands. There are 4, but it seems you only need
1051 // 3 to not hit an assertion later in MCInstLower.
1052 Inst->addOperand(MachineOperand::CreateImm(0));
1053 Inst->addOperand(MachineOperand::CreateImm(0));
1054 Inst->addOperand(MachineOperand::CreateImm(0));
1055 Inst->addOperand(MachineOperand::CreateImm(0));
1058 addDescImplicitUseDef(NewDesc, Inst);
1060 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) {
1061 const MachineOperand &OffsetWidthOp = Inst->getOperand(2);
1062 // If we need to move this to VGPRs, we need to unpack the second operand
1063 // back into the 2 separate ones for bit offset and width.
1064 assert(OffsetWidthOp.isImm() &&
1065 "Scalar BFE is only implemented for constant width and offset");
1066 uint32_t Imm = OffsetWidthOp.getImm();
1068 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0].
1069 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16].
1071 Inst->RemoveOperand(2); // Remove old immediate.
1072 Inst->addOperand(MachineOperand::CreateImm(Offset));
1073 Inst->addOperand(MachineOperand::CreateImm(BitWidth));
1075 Inst->addOperand(MachineOperand::CreateImm(0));
1076 Inst->addOperand(MachineOperand::CreateImm(0));
1077 Inst->addOperand(MachineOperand::CreateImm(0));
1078 Inst->addOperand(MachineOperand::CreateImm(0));
1081 // Update the destination register class.
1083 const TargetRegisterClass *NewDstRC = getOpRegClass(*Inst, 0);
1086 // For target instructions, getOpRegClass just returns the virtual
1087 // register class associated with the operand, so we need to find an
1088 // equivalent VGPR register class in order to move the instruction to the
1092 case AMDGPU::REG_SEQUENCE:
1093 case AMDGPU::INSERT_SUBREG:
1094 if (RI.hasVGPRs(NewDstRC))
1096 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
1104 unsigned DstReg = Inst->getOperand(0).getReg();
1105 unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC);
1106 MRI.replaceRegWith(DstReg, NewDstReg);
1108 // Legalize the operands
1109 legalizeOperands(Inst);
1111 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(NewDstReg),
1112 E = MRI.use_end(); I != E; ++I) {
1113 MachineInstr &UseMI = *I->getParent();
1114 if (!canReadVGPR(UseMI, I.getOperandNo())) {
1115 Worklist.push_back(&UseMI);
1121 //===----------------------------------------------------------------------===//
1122 // Indirect addressing callbacks
1123 //===----------------------------------------------------------------------===//
1125 unsigned SIInstrInfo::calculateIndirectAddress(unsigned RegIndex,
1126 unsigned Channel) const {
1127 assert(Channel == 0);
1131 const TargetRegisterClass *SIInstrInfo::getIndirectAddrRegClass() const {
1132 return &AMDGPU::VReg_32RegClass;
1135 void SIInstrInfo::splitScalar64BitOp(SmallVectorImpl<MachineInstr *> &Worklist,
1137 unsigned Opcode) const {
1138 MachineBasicBlock &MBB = *Inst->getParent();
1139 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1141 MachineOperand &Dest = Inst->getOperand(0);
1142 MachineOperand &Src0 = Inst->getOperand(1);
1143 MachineOperand &Src1 = Inst->getOperand(2);
1144 DebugLoc DL = Inst->getDebugLoc();
1146 MachineBasicBlock::iterator MII = Inst;
1148 const MCInstrDesc &InstDesc = get(Opcode);
1149 const TargetRegisterClass *Src0RC = Src0.isReg() ?
1150 MRI.getRegClass(Src0.getReg()) :
1151 &AMDGPU::SGPR_32RegClass;
1153 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
1154 const TargetRegisterClass *Src1RC = Src1.isReg() ?
1155 MRI.getRegClass(Src1.getReg()) :
1156 &AMDGPU::SGPR_32RegClass;
1158 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
1160 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
1161 AMDGPU::sub0, Src0SubRC);
1162 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
1163 AMDGPU::sub0, Src1SubRC);
1165 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
1166 const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0);
1168 unsigned DestSub0 = MRI.createVirtualRegister(DestRC);
1169 MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0)
1170 .addOperand(SrcReg0Sub0)
1171 .addOperand(SrcReg1Sub0);
1173 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
1174 AMDGPU::sub1, Src0SubRC);
1175 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
1176 AMDGPU::sub1, Src1SubRC);
1178 unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC);
1179 MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1)
1180 .addOperand(SrcReg0Sub1)
1181 .addOperand(SrcReg1Sub1);
1183 unsigned FullDestReg = MRI.createVirtualRegister(DestRC);
1184 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
1186 .addImm(AMDGPU::sub0)
1188 .addImm(AMDGPU::sub1);
1190 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
1192 // Try to legalize the operands in case we need to swap the order to keep it
1194 Worklist.push_back(LoHalf);
1195 Worklist.push_back(HiHalf);
1198 void SIInstrInfo::addDescImplicitUseDef(const MCInstrDesc &NewDesc,
1199 MachineInstr *Inst) const {
1200 // Add the implict and explicit register definitions.
1201 if (NewDesc.ImplicitUses) {
1202 for (unsigned i = 0; NewDesc.ImplicitUses[i]; ++i) {
1203 unsigned Reg = NewDesc.ImplicitUses[i];
1204 Inst->addOperand(MachineOperand::CreateReg(Reg, false, true));
1208 if (NewDesc.ImplicitDefs) {
1209 for (unsigned i = 0; NewDesc.ImplicitDefs[i]; ++i) {
1210 unsigned Reg = NewDesc.ImplicitDefs[i];
1211 Inst->addOperand(MachineOperand::CreateReg(Reg, true, true));
1216 MachineInstrBuilder SIInstrInfo::buildIndirectWrite(
1217 MachineBasicBlock *MBB,
1218 MachineBasicBlock::iterator I,
1220 unsigned Address, unsigned OffsetReg) const {
1221 const DebugLoc &DL = MBB->findDebugLoc(I);
1222 unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister(
1223 getIndirectIndexBegin(*MBB->getParent()));
1225 return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_DST_V1))
1226 .addReg(IndirectBaseReg, RegState::Define)
1227 .addOperand(I->getOperand(0))
1228 .addReg(IndirectBaseReg)
1234 MachineInstrBuilder SIInstrInfo::buildIndirectRead(
1235 MachineBasicBlock *MBB,
1236 MachineBasicBlock::iterator I,
1238 unsigned Address, unsigned OffsetReg) const {
1239 const DebugLoc &DL = MBB->findDebugLoc(I);
1240 unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister(
1241 getIndirectIndexBegin(*MBB->getParent()));
1243 return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_SRC))
1244 .addOperand(I->getOperand(0))
1245 .addOperand(I->getOperand(1))
1246 .addReg(IndirectBaseReg)
1252 void SIInstrInfo::reserveIndirectRegisters(BitVector &Reserved,
1253 const MachineFunction &MF) const {
1254 int End = getIndirectIndexEnd(MF);
1255 int Begin = getIndirectIndexBegin(MF);
1261 for (int Index = Begin; Index <= End; ++Index)
1262 Reserved.set(AMDGPU::VReg_32RegClass.getRegister(Index));
1264 for (int Index = std::max(0, Begin - 1); Index <= End; ++Index)
1265 Reserved.set(AMDGPU::VReg_64RegClass.getRegister(Index));
1267 for (int Index = std::max(0, Begin - 2); Index <= End; ++Index)
1268 Reserved.set(AMDGPU::VReg_96RegClass.getRegister(Index));
1270 for (int Index = std::max(0, Begin - 3); Index <= End; ++Index)
1271 Reserved.set(AMDGPU::VReg_128RegClass.getRegister(Index));
1273 for (int Index = std::max(0, Begin - 7); Index <= End; ++Index)
1274 Reserved.set(AMDGPU::VReg_256RegClass.getRegister(Index));
1276 for (int Index = std::max(0, Begin - 15); Index <= End; ++Index)
1277 Reserved.set(AMDGPU::VReg_512RegClass.getRegister(Index));