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;
543 bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const {
544 return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END;
547 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI,
548 unsigned OpNo) const {
549 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
550 const MCInstrDesc &Desc = get(MI.getOpcode());
551 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() ||
552 Desc.OpInfo[OpNo].RegClass == -1)
553 return MRI.getRegClass(MI.getOperand(OpNo).getReg());
555 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
556 return RI.getRegClass(RCID);
559 bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const {
560 switch (MI.getOpcode()) {
562 case AMDGPU::REG_SEQUENCE:
563 return RI.hasVGPRs(getOpRegClass(MI, 0));
565 return RI.hasVGPRs(getOpRegClass(MI, OpNo));
569 void SIInstrInfo::legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const {
570 MachineBasicBlock::iterator I = MI;
571 MachineOperand &MO = MI->getOperand(OpIdx);
572 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
573 unsigned RCID = get(MI->getOpcode()).OpInfo[OpIdx].RegClass;
574 const TargetRegisterClass *RC = RI.getRegClass(RCID);
575 unsigned Opcode = AMDGPU::V_MOV_B32_e32;
577 Opcode = AMDGPU::COPY;
578 } else if (RI.isSGPRClass(RC)) {
579 Opcode = AMDGPU::S_MOV_B32;
582 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC);
583 unsigned Reg = MRI.createVirtualRegister(VRC);
584 BuildMI(*MI->getParent(), I, MI->getParent()->findDebugLoc(I), get(Opcode),
586 MO.ChangeToRegister(Reg, false);
589 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI,
590 MachineRegisterInfo &MRI,
591 MachineOperand &SuperReg,
592 const TargetRegisterClass *SuperRC,
594 const TargetRegisterClass *SubRC)
596 assert(SuperReg.isReg());
598 unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC);
599 unsigned SubReg = MRI.createVirtualRegister(SubRC);
601 // Just in case the super register is itself a sub-register, copy it to a new
602 // value so we don't need to wory about merging its subreg index with the
603 // SubIdx passed to this function. The register coalescer should be able to
604 // eliminate this extra copy.
605 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY),
607 .addOperand(SuperReg);
609 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY),
611 .addReg(NewSuperReg, 0, SubIdx);
615 MachineOperand SIInstrInfo::buildExtractSubRegOrImm(
616 MachineBasicBlock::iterator MII,
617 MachineRegisterInfo &MRI,
619 const TargetRegisterClass *SuperRC,
621 const TargetRegisterClass *SubRC) const {
623 // XXX - Is there a better way to do this?
624 if (SubIdx == AMDGPU::sub0)
625 return MachineOperand::CreateImm(Op.getImm() & 0xFFFFFFFF);
626 if (SubIdx == AMDGPU::sub1)
627 return MachineOperand::CreateImm(Op.getImm() >> 32);
629 llvm_unreachable("Unhandled register index for immediate");
632 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC,
634 return MachineOperand::CreateReg(SubReg, false);
637 unsigned SIInstrInfo::split64BitImm(SmallVectorImpl<MachineInstr *> &Worklist,
638 MachineBasicBlock::iterator MI,
639 MachineRegisterInfo &MRI,
640 const TargetRegisterClass *RC,
641 const MachineOperand &Op) const {
642 MachineBasicBlock *MBB = MI->getParent();
643 DebugLoc DL = MI->getDebugLoc();
644 unsigned LoDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
645 unsigned HiDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
646 unsigned Dst = MRI.createVirtualRegister(RC);
648 MachineInstr *Lo = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32),
650 .addImm(Op.getImm() & 0xFFFFFFFF);
651 MachineInstr *Hi = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32),
653 .addImm(Op.getImm() >> 32);
655 BuildMI(*MBB, MI, DL, get(TargetOpcode::REG_SEQUENCE), Dst)
657 .addImm(AMDGPU::sub0)
659 .addImm(AMDGPU::sub1);
661 Worklist.push_back(Lo);
662 Worklist.push_back(Hi);
667 void SIInstrInfo::legalizeOperands(MachineInstr *MI) const {
668 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
669 int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
670 AMDGPU::OpName::src0);
671 int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
672 AMDGPU::OpName::src1);
673 int Src2Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
674 AMDGPU::OpName::src2);
677 if (isVOP2(MI->getOpcode()) && Src1Idx != -1) {
678 MachineOperand &Src0 = MI->getOperand(Src0Idx);
679 MachineOperand &Src1 = MI->getOperand(Src1Idx);
681 // If the instruction implicitly reads VCC, we can't have any SGPR operands,
683 bool ReadsVCC = MI->readsRegister(AMDGPU::VCC, &RI);
684 if (ReadsVCC && Src0.isReg() &&
685 RI.isSGPRClass(MRI.getRegClass(Src0.getReg()))) {
686 legalizeOpWithMove(MI, Src0Idx);
690 if (ReadsVCC && Src1.isReg() &&
691 RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) {
692 legalizeOpWithMove(MI, Src1Idx);
696 // Legalize VOP2 instructions where src1 is not a VGPR. An SGPR input must
697 // be the first operand, and there can only be one.
698 if (Src1.isImm() || Src1.isFPImm() ||
699 (Src1.isReg() && RI.isSGPRClass(MRI.getRegClass(Src1.getReg())))) {
700 if (MI->isCommutable()) {
701 if (commuteInstruction(MI))
704 legalizeOpWithMove(MI, Src1Idx);
708 // XXX - Do any VOP3 instructions read VCC?
710 if (isVOP3(MI->getOpcode())) {
711 int VOP3Idx[3] = {Src0Idx, Src1Idx, Src2Idx};
712 unsigned SGPRReg = AMDGPU::NoRegister;
713 for (unsigned i = 0; i < 3; ++i) {
714 int Idx = VOP3Idx[i];
717 MachineOperand &MO = MI->getOperand(Idx);
720 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg())))
721 continue; // VGPRs are legal
723 assert(MO.getReg() != AMDGPU::SCC && "SCC operand to VOP3 instruction");
725 if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) {
726 SGPRReg = MO.getReg();
727 // We can use one SGPR in each VOP3 instruction.
730 } else if (!isLiteralConstant(MO)) {
731 // If it is not a register and not a literal constant, then it must be
732 // an inline constant which is always legal.
735 // If we make it this far, then the operand is not legal and we must
737 legalizeOpWithMove(MI, Idx);
741 // Legalize REG_SEQUENCE
742 // The register class of the operands much be the same type as the register
743 // class of the output.
744 if (MI->getOpcode() == AMDGPU::REG_SEQUENCE) {
745 const TargetRegisterClass *RC = NULL, *SRC = NULL, *VRC = NULL;
746 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
747 if (!MI->getOperand(i).isReg() ||
748 !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
750 const TargetRegisterClass *OpRC =
751 MRI.getRegClass(MI->getOperand(i).getReg());
752 if (RI.hasVGPRs(OpRC)) {
759 // If any of the operands are VGPR registers, then they all most be
760 // otherwise we will create illegal VGPR->SGPR copies when legalizing
762 if (VRC || !RI.isSGPRClass(getOpRegClass(*MI, 0))) {
765 VRC = RI.getEquivalentVGPRClass(SRC);
772 // Update all the operands so they have the same type.
773 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
774 if (!MI->getOperand(i).isReg() ||
775 !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
777 unsigned DstReg = MRI.createVirtualRegister(RC);
778 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
779 get(AMDGPU::COPY), DstReg)
780 .addOperand(MI->getOperand(i));
781 MI->getOperand(i).setReg(DstReg);
785 // Legalize MUBUF* instructions
786 // FIXME: If we start using the non-addr64 instructions for compute, we
787 // may need to legalize them here.
789 int SRsrcIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
790 AMDGPU::OpName::srsrc);
791 int VAddrIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
792 AMDGPU::OpName::vaddr);
793 if (SRsrcIdx != -1 && VAddrIdx != -1) {
794 const TargetRegisterClass *VAddrRC =
795 RI.getRegClass(get(MI->getOpcode()).OpInfo[VAddrIdx].RegClass);
797 if(VAddrRC->getSize() == 8 &&
798 MRI.getRegClass(MI->getOperand(SRsrcIdx).getReg()) != VAddrRC) {
799 // We have a MUBUF instruction that uses a 64-bit vaddr register and
800 // srsrc has the incorrect register class. In order to fix this, we
801 // need to extract the pointer from the resource descriptor (srsrc),
802 // add it to the value of vadd, then store the result in the vaddr
803 // operand. Then, we need to set the pointer field of the resource
804 // descriptor to zero.
806 MachineBasicBlock &MBB = *MI->getParent();
807 MachineOperand &SRsrcOp = MI->getOperand(SRsrcIdx);
808 MachineOperand &VAddrOp = MI->getOperand(VAddrIdx);
809 unsigned SRsrcPtrLo, SRsrcPtrHi, VAddrLo, VAddrHi;
810 unsigned NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
811 unsigned NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
812 unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass);
813 unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
814 unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
815 unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
816 unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass);
818 // SRsrcPtrLo = srsrc:sub0
819 SRsrcPtrLo = buildExtractSubReg(MI, MRI, SRsrcOp,
820 &AMDGPU::VReg_128RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass);
822 // SRsrcPtrHi = srsrc:sub1
823 SRsrcPtrHi = buildExtractSubReg(MI, MRI, SRsrcOp,
824 &AMDGPU::VReg_128RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass);
826 // VAddrLo = vaddr:sub0
827 VAddrLo = buildExtractSubReg(MI, MRI, VAddrOp,
828 &AMDGPU::VReg_64RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass);
830 // VAddrHi = vaddr:sub1
831 VAddrHi = buildExtractSubReg(MI, MRI, VAddrOp,
832 &AMDGPU::VReg_64RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass);
834 // NewVaddrLo = SRsrcPtrLo + VAddrLo
835 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADD_I32_e32),
839 .addReg(AMDGPU::VCC, RegState::Define | RegState::Implicit);
841 // NewVaddrHi = SRsrcPtrHi + VAddrHi
842 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADDC_U32_e32),
846 .addReg(AMDGPU::VCC, RegState::ImplicitDefine)
847 .addReg(AMDGPU::VCC, RegState::Implicit);
849 // NewVaddr = {NewVaddrHi, NewVaddrLo}
850 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
853 .addImm(AMDGPU::sub0)
855 .addImm(AMDGPU::sub1);
858 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B64),
862 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0}
863 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
865 .addImm(AMDGPU::RSRC_DATA_FORMAT & 0xFFFFFFFF);
867 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32}
868 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32),
870 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
872 // NewSRsrc = {Zero64, SRsrcFormat}
873 BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE),
876 .addImm(AMDGPU::sub0_sub1)
877 .addReg(SRsrcFormatLo)
878 .addImm(AMDGPU::sub2)
879 .addReg(SRsrcFormatHi)
880 .addImm(AMDGPU::sub3);
882 // Update the instruction to use NewVaddr
883 MI->getOperand(VAddrIdx).setReg(NewVAddr);
884 // Update the instruction to use NewSRsrc
885 MI->getOperand(SRsrcIdx).setReg(NewSRsrc);
890 void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const {
891 SmallVector<MachineInstr *, 128> Worklist;
892 Worklist.push_back(&TopInst);
894 while (!Worklist.empty()) {
895 MachineInstr *Inst = Worklist.pop_back_val();
896 MachineBasicBlock *MBB = Inst->getParent();
897 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
899 // Handle some special cases
900 switch(Inst->getOpcode()) {
901 case AMDGPU::S_MOV_B64: {
902 DebugLoc DL = Inst->getDebugLoc();
904 // If the source operand is a register we can replace this with a
906 if (Inst->getOperand(1).isReg()) {
907 MachineInstr *Copy = BuildMI(*MBB, Inst, DL, get(TargetOpcode::COPY))
908 .addOperand(Inst->getOperand(0))
909 .addOperand(Inst->getOperand(1));
910 Worklist.push_back(Copy);
912 // Otherwise, we need to split this into two movs, because there is
913 // no 64-bit VALU move instruction.
914 unsigned Reg = Inst->getOperand(0).getReg();
915 unsigned Dst = split64BitImm(Worklist,
918 MRI.getRegClass(Reg),
919 Inst->getOperand(1));
920 MRI.replaceRegWith(Reg, Dst);
922 Inst->eraseFromParent();
925 case AMDGPU::S_AND_B64:
926 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_AND_B32);
927 Inst->eraseFromParent();
930 case AMDGPU::S_OR_B64:
931 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_OR_B32);
932 Inst->eraseFromParent();
935 case AMDGPU::S_XOR_B64:
936 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_XOR_B32);
937 Inst->eraseFromParent();
940 case AMDGPU::S_NOT_B64:
941 splitScalar64BitOp(Worklist, Inst, AMDGPU::S_NOT_B32);
942 Inst->eraseFromParent();
945 case AMDGPU::S_BFE_U64:
946 case AMDGPU::S_BFE_I64:
947 case AMDGPU::S_BFM_B64:
948 llvm_unreachable("Moving this op to VALU not implemented");
951 unsigned NewOpcode = getVALUOp(*Inst);
952 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) {
953 // We cannot move this instruction to the VALU, so we should try to
954 // legalize its operands instead.
955 legalizeOperands(Inst);
959 // Use the new VALU Opcode.
960 const MCInstrDesc &NewDesc = get(NewOpcode);
961 Inst->setDesc(NewDesc);
963 // Remove any references to SCC. Vector instructions can't read from it, and
964 // We're just about to add the implicit use / defs of VCC, and we don't want
966 for (unsigned i = Inst->getNumOperands() - 1; i > 0; --i) {
967 MachineOperand &Op = Inst->getOperand(i);
968 if (Op.isReg() && Op.getReg() == AMDGPU::SCC)
969 Inst->RemoveOperand(i);
972 // Add the implict and explicit register definitions.
973 if (NewDesc.ImplicitUses) {
974 for (unsigned i = 0; NewDesc.ImplicitUses[i]; ++i) {
975 unsigned Reg = NewDesc.ImplicitUses[i];
976 Inst->addOperand(MachineOperand::CreateReg(Reg, false, true));
980 if (NewDesc.ImplicitDefs) {
981 for (unsigned i = 0; NewDesc.ImplicitDefs[i]; ++i) {
982 unsigned Reg = NewDesc.ImplicitDefs[i];
983 Inst->addOperand(MachineOperand::CreateReg(Reg, true, true));
987 legalizeOperands(Inst);
989 // Update the destination register class.
990 const TargetRegisterClass *NewDstRC = getOpRegClass(*Inst, 0);
992 switch (Inst->getOpcode()) {
993 // For target instructions, getOpRegClass just returns the virtual
994 // register class associated with the operand, so we need to find an
995 // equivalent VGPR register class in order to move the instruction to the
999 case AMDGPU::REG_SEQUENCE:
1000 case AMDGPU::INSERT_SUBREG:
1001 if (RI.hasVGPRs(NewDstRC))
1003 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC);
1011 unsigned DstReg = Inst->getOperand(0).getReg();
1012 unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC);
1013 MRI.replaceRegWith(DstReg, NewDstReg);
1015 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(NewDstReg),
1016 E = MRI.use_end(); I != E; ++I) {
1017 MachineInstr &UseMI = *I->getParent();
1018 if (!canReadVGPR(UseMI, I.getOperandNo())) {
1019 Worklist.push_back(&UseMI);
1025 //===----------------------------------------------------------------------===//
1026 // Indirect addressing callbacks
1027 //===----------------------------------------------------------------------===//
1029 unsigned SIInstrInfo::calculateIndirectAddress(unsigned RegIndex,
1030 unsigned Channel) const {
1031 assert(Channel == 0);
1035 const TargetRegisterClass *SIInstrInfo::getIndirectAddrRegClass() const {
1036 return &AMDGPU::VReg_32RegClass;
1039 void SIInstrInfo::splitScalar64BitOp(SmallVectorImpl<MachineInstr *> &Worklist,
1041 unsigned Opcode) const {
1042 MachineBasicBlock &MBB = *Inst->getParent();
1043 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1045 MachineOperand &Dest = Inst->getOperand(0);
1046 MachineOperand &Src0 = Inst->getOperand(1);
1047 MachineOperand &Src1 = Inst->getOperand(2);
1048 DebugLoc DL = Inst->getDebugLoc();
1050 MachineBasicBlock::iterator MII = Inst;
1052 const MCInstrDesc &InstDesc = get(Opcode);
1053 const TargetRegisterClass *Src0RC = Src0.isReg() ?
1054 MRI.getRegClass(Src0.getReg()) :
1055 &AMDGPU::SGPR_32RegClass;
1057 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0);
1058 const TargetRegisterClass *Src1RC = Src1.isReg() ?
1059 MRI.getRegClass(Src1.getReg()) :
1060 &AMDGPU::SGPR_32RegClass;
1062 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0);
1064 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
1065 AMDGPU::sub0, Src0SubRC);
1066 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
1067 AMDGPU::sub0, Src1SubRC);
1069 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg());
1070 const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0);
1072 unsigned DestSub0 = MRI.createVirtualRegister(DestRC);
1073 MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0)
1074 .addOperand(SrcReg0Sub0)
1075 .addOperand(SrcReg1Sub0);
1077 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC,
1078 AMDGPU::sub1, Src0SubRC);
1079 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC,
1080 AMDGPU::sub1, Src1SubRC);
1082 unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC);
1083 MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1)
1084 .addOperand(SrcReg0Sub1)
1085 .addOperand(SrcReg1Sub1);
1087 unsigned FullDestReg = MRI.createVirtualRegister(DestRC);
1088 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg)
1090 .addImm(AMDGPU::sub0)
1092 .addImm(AMDGPU::sub1);
1094 MRI.replaceRegWith(Dest.getReg(), FullDestReg);
1096 // Try to legalize the operands in case we need to swap the order to keep it
1098 Worklist.push_back(LoHalf);
1099 Worklist.push_back(HiHalf);
1102 MachineInstrBuilder SIInstrInfo::buildIndirectWrite(
1103 MachineBasicBlock *MBB,
1104 MachineBasicBlock::iterator I,
1106 unsigned Address, unsigned OffsetReg) const {
1107 const DebugLoc &DL = MBB->findDebugLoc(I);
1108 unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister(
1109 getIndirectIndexBegin(*MBB->getParent()));
1111 return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_DST_V1))
1112 .addReg(IndirectBaseReg, RegState::Define)
1113 .addOperand(I->getOperand(0))
1114 .addReg(IndirectBaseReg)
1120 MachineInstrBuilder SIInstrInfo::buildIndirectRead(
1121 MachineBasicBlock *MBB,
1122 MachineBasicBlock::iterator I,
1124 unsigned Address, unsigned OffsetReg) const {
1125 const DebugLoc &DL = MBB->findDebugLoc(I);
1126 unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister(
1127 getIndirectIndexBegin(*MBB->getParent()));
1129 return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_SRC))
1130 .addOperand(I->getOperand(0))
1131 .addOperand(I->getOperand(1))
1132 .addReg(IndirectBaseReg)
1138 void SIInstrInfo::reserveIndirectRegisters(BitVector &Reserved,
1139 const MachineFunction &MF) const {
1140 int End = getIndirectIndexEnd(MF);
1141 int Begin = getIndirectIndexBegin(MF);
1147 for (int Index = Begin; Index <= End; ++Index)
1148 Reserved.set(AMDGPU::VReg_32RegClass.getRegister(Index));
1150 for (int Index = std::max(0, Begin - 1); Index <= End; ++Index)
1151 Reserved.set(AMDGPU::VReg_64RegClass.getRegister(Index));
1153 for (int Index = std::max(0, Begin - 2); Index <= End; ++Index)
1154 Reserved.set(AMDGPU::VReg_96RegClass.getRegister(Index));
1156 for (int Index = std::max(0, Begin - 3); Index <= End; ++Index)
1157 Reserved.set(AMDGPU::VReg_128RegClass.getRegister(Index));
1159 for (int Index = std::max(0, Begin - 7); Index <= End; ++Index)
1160 Reserved.set(AMDGPU::VReg_256RegClass.getRegister(Index));
1162 for (int Index = std::max(0, Begin - 15); Index <= End; ++Index)
1163 Reserved.set(AMDGPU::VReg_512RegClass.getRegister(Index));