Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- SIInstrInfo.cpp - SI Instruction Information ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// \brief SI Implementation of TargetInstrInfo. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | |
| 16 | #include "SIInstrInfo.h" |
| 17 | #include "AMDGPUTargetMachine.h" |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 18 | #include "SIDefines.h" |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 19 | #include "SIMachineFunctionInfo.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Function.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstrDesc.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 27 | SIInstrInfo::SIInstrInfo(const AMDGPUSubtarget &st) |
| 28 | : AMDGPUInstrInfo(st), |
| 29 | RI(st) { } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 30 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // TargetInstrInfo callbacks |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 35 | void |
| 36 | SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 37 | MachineBasicBlock::iterator MI, DebugLoc DL, |
| 38 | unsigned DestReg, unsigned SrcReg, |
| 39 | bool KillSrc) const { |
| 40 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 41 | // If we are trying to copy to or from SCC, there is a bug somewhere else in |
| 42 | // the backend. While it may be theoretically possible to do this, it should |
| 43 | // never be necessary. |
| 44 | assert(DestReg != AMDGPU::SCC && SrcReg != AMDGPU::SCC); |
| 45 | |
Craig Topper | 0afd0ab | 2013-07-15 06:39:13 +0000 | [diff] [blame] | 46 | static const int16_t Sub0_15[] = { |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 47 | AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, |
| 48 | AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, |
| 49 | AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11, |
| 50 | AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15, 0 |
| 51 | }; |
| 52 | |
Craig Topper | 0afd0ab | 2013-07-15 06:39:13 +0000 | [diff] [blame] | 53 | static const int16_t Sub0_7[] = { |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 54 | AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, |
| 55 | AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, 0 |
| 56 | }; |
| 57 | |
Craig Topper | 0afd0ab | 2013-07-15 06:39:13 +0000 | [diff] [blame] | 58 | static const int16_t Sub0_3[] = { |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 59 | AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, 0 |
| 60 | }; |
| 61 | |
Craig Topper | 0afd0ab | 2013-07-15 06:39:13 +0000 | [diff] [blame] | 62 | static const int16_t Sub0_2[] = { |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 63 | AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, 0 |
| 64 | }; |
| 65 | |
Craig Topper | 0afd0ab | 2013-07-15 06:39:13 +0000 | [diff] [blame] | 66 | static const int16_t Sub0_1[] = { |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 67 | AMDGPU::sub0, AMDGPU::sub1, 0 |
| 68 | }; |
| 69 | |
| 70 | unsigned Opcode; |
| 71 | const int16_t *SubIndices; |
| 72 | |
Christian Konig | 082c661 | 2013-03-26 14:04:12 +0000 | [diff] [blame] | 73 | if (AMDGPU::M0 == DestReg) { |
| 74 | // Check if M0 isn't already set to this value |
| 75 | for (MachineBasicBlock::reverse_iterator E = MBB.rend(), |
| 76 | I = MachineBasicBlock::reverse_iterator(MI); I != E; ++I) { |
| 77 | |
| 78 | if (!I->definesRegister(AMDGPU::M0)) |
| 79 | continue; |
| 80 | |
| 81 | unsigned Opc = I->getOpcode(); |
| 82 | if (Opc != TargetOpcode::COPY && Opc != AMDGPU::S_MOV_B32) |
| 83 | break; |
| 84 | |
| 85 | if (!I->readsRegister(SrcReg)) |
| 86 | break; |
| 87 | |
| 88 | // The copy isn't necessary |
| 89 | return; |
| 90 | } |
| 91 | } |
| 92 | |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 93 | if (AMDGPU::SReg_32RegClass.contains(DestReg)) { |
| 94 | assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); |
| 95 | BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) |
| 96 | .addReg(SrcReg, getKillRegState(KillSrc)); |
| 97 | return; |
| 98 | |
Tom Stellard | aac1889 | 2013-02-07 19:39:43 +0000 | [diff] [blame] | 99 | } else if (AMDGPU::SReg_64RegClass.contains(DestReg)) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 100 | assert(AMDGPU::SReg_64RegClass.contains(SrcReg)); |
| 101 | BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) |
| 102 | .addReg(SrcReg, getKillRegState(KillSrc)); |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 103 | return; |
| 104 | |
| 105 | } else if (AMDGPU::SReg_128RegClass.contains(DestReg)) { |
| 106 | assert(AMDGPU::SReg_128RegClass.contains(SrcReg)); |
| 107 | Opcode = AMDGPU::S_MOV_B32; |
| 108 | SubIndices = Sub0_3; |
| 109 | |
| 110 | } else if (AMDGPU::SReg_256RegClass.contains(DestReg)) { |
| 111 | assert(AMDGPU::SReg_256RegClass.contains(SrcReg)); |
| 112 | Opcode = AMDGPU::S_MOV_B32; |
| 113 | SubIndices = Sub0_7; |
| 114 | |
| 115 | } else if (AMDGPU::SReg_512RegClass.contains(DestReg)) { |
| 116 | assert(AMDGPU::SReg_512RegClass.contains(SrcReg)); |
| 117 | Opcode = AMDGPU::S_MOV_B32; |
| 118 | SubIndices = Sub0_15; |
| 119 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 120 | } else if (AMDGPU::VReg_32RegClass.contains(DestReg)) { |
| 121 | assert(AMDGPU::VReg_32RegClass.contains(SrcReg) || |
NAKAMURA Takumi | 4bb85f9 | 2013-10-28 04:07:23 +0000 | [diff] [blame] | 122 | AMDGPU::SReg_32RegClass.contains(SrcReg)); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 123 | BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg) |
| 124 | .addReg(SrcReg, getKillRegState(KillSrc)); |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 125 | return; |
| 126 | |
| 127 | } else if (AMDGPU::VReg_64RegClass.contains(DestReg)) { |
| 128 | assert(AMDGPU::VReg_64RegClass.contains(SrcReg) || |
NAKAMURA Takumi | 4bb85f9 | 2013-10-28 04:07:23 +0000 | [diff] [blame] | 129 | AMDGPU::SReg_64RegClass.contains(SrcReg)); |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 130 | Opcode = AMDGPU::V_MOV_B32_e32; |
| 131 | SubIndices = Sub0_1; |
| 132 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 133 | } else if (AMDGPU::VReg_96RegClass.contains(DestReg)) { |
| 134 | assert(AMDGPU::VReg_96RegClass.contains(SrcReg)); |
| 135 | Opcode = AMDGPU::V_MOV_B32_e32; |
| 136 | SubIndices = Sub0_2; |
| 137 | |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 138 | } else if (AMDGPU::VReg_128RegClass.contains(DestReg)) { |
| 139 | assert(AMDGPU::VReg_128RegClass.contains(SrcReg) || |
NAKAMURA Takumi | 4bb85f9 | 2013-10-28 04:07:23 +0000 | [diff] [blame] | 140 | AMDGPU::SReg_128RegClass.contains(SrcReg)); |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 141 | Opcode = AMDGPU::V_MOV_B32_e32; |
| 142 | SubIndices = Sub0_3; |
| 143 | |
| 144 | } else if (AMDGPU::VReg_256RegClass.contains(DestReg)) { |
| 145 | assert(AMDGPU::VReg_256RegClass.contains(SrcReg) || |
NAKAMURA Takumi | 4bb85f9 | 2013-10-28 04:07:23 +0000 | [diff] [blame] | 146 | AMDGPU::SReg_256RegClass.contains(SrcReg)); |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 147 | Opcode = AMDGPU::V_MOV_B32_e32; |
| 148 | SubIndices = Sub0_7; |
| 149 | |
| 150 | } else if (AMDGPU::VReg_512RegClass.contains(DestReg)) { |
| 151 | assert(AMDGPU::VReg_512RegClass.contains(SrcReg) || |
NAKAMURA Takumi | 4bb85f9 | 2013-10-28 04:07:23 +0000 | [diff] [blame] | 152 | AMDGPU::SReg_512RegClass.contains(SrcReg)); |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 153 | Opcode = AMDGPU::V_MOV_B32_e32; |
| 154 | SubIndices = Sub0_15; |
| 155 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 156 | } else { |
Christian Konig | d0e3da1 | 2013-03-01 09:46:27 +0000 | [diff] [blame] | 157 | llvm_unreachable("Can't copy register!"); |
| 158 | } |
| 159 | |
| 160 | while (unsigned SubIdx = *SubIndices++) { |
| 161 | MachineInstrBuilder Builder = BuildMI(MBB, MI, DL, |
| 162 | get(Opcode), RI.getSubReg(DestReg, SubIdx)); |
| 163 | |
| 164 | Builder.addReg(RI.getSubReg(SrcReg, SubIdx), getKillRegState(KillSrc)); |
| 165 | |
| 166 | if (*SubIndices) |
| 167 | Builder.addReg(DestReg, RegState::Define | RegState::Implicit); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 171 | unsigned SIInstrInfo::commuteOpcode(unsigned Opcode) const { |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 172 | int NewOpc; |
| 173 | |
| 174 | // Try to map original to commuted opcode |
| 175 | if ((NewOpc = AMDGPU::getCommuteRev(Opcode)) != -1) |
| 176 | return NewOpc; |
| 177 | |
| 178 | // Try to map commuted to original opcode |
| 179 | if ((NewOpc = AMDGPU::getCommuteOrig(Opcode)) != -1) |
| 180 | return NewOpc; |
| 181 | |
| 182 | return Opcode; |
| 183 | } |
| 184 | |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 185 | void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 186 | MachineBasicBlock::iterator MI, |
| 187 | unsigned SrcReg, bool isKill, |
| 188 | int FrameIndex, |
| 189 | const TargetRegisterClass *RC, |
| 190 | const TargetRegisterInfo *TRI) const { |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 191 | MachineFunction *MF = MBB.getParent(); |
| 192 | SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); |
| 193 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 194 | DebugLoc DL = MBB.findDebugLoc(MI); |
| 195 | unsigned KillFlag = isKill ? RegState::Kill : 0; |
| 196 | |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 197 | if (RI.hasVGPRs(RC)) { |
| 198 | LLVMContext &Ctx = MF->getFunction()->getContext(); |
| 199 | Ctx.emitError("SIInstrInfo::storeRegToStackSlot - Can't spill VGPR!"); |
| 200 | BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), AMDGPU::VGPR0) |
| 201 | .addReg(SrcReg); |
| 202 | } else if (TRI->getCommonSubClass(RC, &AMDGPU::SGPR_32RegClass)) { |
| 203 | unsigned Lane = MFI->SpillTracker.reserveLanes(MRI, MF); |
| 204 | unsigned TgtReg = MFI->SpillTracker.LaneVGPR; |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 205 | |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 206 | BuildMI(MBB, MI, DL, get(AMDGPU::V_WRITELANE_B32), TgtReg) |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 207 | .addReg(SrcReg, KillFlag) |
| 208 | .addImm(Lane); |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 209 | MFI->SpillTracker.addSpilledReg(FrameIndex, TgtReg, Lane); |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 210 | } else if (RI.isSGPRClass(RC)) { |
| 211 | // We are only allowed to create one new instruction when spilling |
| 212 | // registers, so we need to use pseudo instruction for vector |
| 213 | // registers. |
| 214 | // |
| 215 | // Reserve a spot in the spill tracker for each sub-register of |
| 216 | // the vector register. |
| 217 | unsigned NumSubRegs = RC->getSize() / 4; |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 218 | unsigned FirstLane = MFI->SpillTracker.reserveLanes(MRI, MF, NumSubRegs); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 219 | MFI->SpillTracker.addSpilledReg(FrameIndex, MFI->SpillTracker.LaneVGPR, |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 220 | FirstLane); |
| 221 | |
| 222 | unsigned Opcode; |
| 223 | switch (RC->getSize() * 8) { |
| 224 | case 64: Opcode = AMDGPU::SI_SPILL_S64_SAVE; break; |
| 225 | case 128: Opcode = AMDGPU::SI_SPILL_S128_SAVE; break; |
| 226 | case 256: Opcode = AMDGPU::SI_SPILL_S256_SAVE; break; |
| 227 | case 512: Opcode = AMDGPU::SI_SPILL_S512_SAVE; break; |
| 228 | default: llvm_unreachable("Cannot spill register class"); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 229 | } |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 230 | |
| 231 | BuildMI(MBB, MI, DL, get(Opcode), MFI->SpillTracker.LaneVGPR) |
| 232 | .addReg(SrcReg) |
| 233 | .addImm(FrameIndex); |
| 234 | } else { |
| 235 | llvm_unreachable("VGPR spilling not supported"); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
| 240 | MachineBasicBlock::iterator MI, |
| 241 | unsigned DestReg, int FrameIndex, |
| 242 | const TargetRegisterClass *RC, |
| 243 | const TargetRegisterInfo *TRI) const { |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 244 | MachineFunction *MF = MBB.getParent(); |
| 245 | SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 246 | DebugLoc DL = MBB.findDebugLoc(MI); |
Tom Stellard | 4e07b1d | 2014-06-10 21:20:41 +0000 | [diff] [blame] | 247 | |
| 248 | if (RI.hasVGPRs(RC)) { |
| 249 | LLVMContext &Ctx = MF->getFunction()->getContext(); |
| 250 | Ctx.emitError("SIInstrInfo::loadRegToStackSlot - Can't retrieve spilled VGPR!"); |
| 251 | BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg) |
| 252 | .addImm(0); |
| 253 | } else if (RI.isSGPRClass(RC)){ |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 254 | unsigned Opcode; |
| 255 | switch(RC->getSize() * 8) { |
Tom Stellard | 060ae39 | 2014-06-10 21:20:38 +0000 | [diff] [blame] | 256 | case 32: Opcode = AMDGPU::SI_SPILL_S32_RESTORE; break; |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 257 | case 64: Opcode = AMDGPU::SI_SPILL_S64_RESTORE; break; |
| 258 | case 128: Opcode = AMDGPU::SI_SPILL_S128_RESTORE; break; |
| 259 | case 256: Opcode = AMDGPU::SI_SPILL_S256_RESTORE; break; |
| 260 | case 512: Opcode = AMDGPU::SI_SPILL_S512_RESTORE; break; |
| 261 | default: llvm_unreachable("Cannot spill register class"); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 262 | } |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 263 | |
| 264 | SIMachineFunctionInfo::SpilledReg Spill = |
| 265 | MFI->SpillTracker.getSpilledReg(FrameIndex); |
| 266 | |
| 267 | BuildMI(MBB, MI, DL, get(Opcode), DestReg) |
| 268 | .addReg(Spill.VGPR) |
| 269 | .addImm(FrameIndex); |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 270 | } else { |
| 271 | llvm_unreachable("VGPR spilling not supported"); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 275 | static unsigned getNumSubRegsForSpillOp(unsigned Op) { |
| 276 | |
| 277 | switch (Op) { |
| 278 | case AMDGPU::SI_SPILL_S512_SAVE: |
| 279 | case AMDGPU::SI_SPILL_S512_RESTORE: |
| 280 | return 16; |
| 281 | case AMDGPU::SI_SPILL_S256_SAVE: |
| 282 | case AMDGPU::SI_SPILL_S256_RESTORE: |
| 283 | return 8; |
| 284 | case AMDGPU::SI_SPILL_S128_SAVE: |
| 285 | case AMDGPU::SI_SPILL_S128_RESTORE: |
| 286 | return 4; |
| 287 | case AMDGPU::SI_SPILL_S64_SAVE: |
| 288 | case AMDGPU::SI_SPILL_S64_RESTORE: |
| 289 | return 2; |
Tom Stellard | 060ae39 | 2014-06-10 21:20:38 +0000 | [diff] [blame] | 290 | case AMDGPU::SI_SPILL_S32_RESTORE: |
| 291 | return 1; |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 292 | default: llvm_unreachable("Invalid spill opcode"); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void SIInstrInfo::insertNOPs(MachineBasicBlock::iterator MI, |
| 297 | int Count) const { |
| 298 | while (Count > 0) { |
| 299 | int Arg; |
| 300 | if (Count >= 8) |
| 301 | Arg = 7; |
| 302 | else |
| 303 | Arg = Count - 1; |
| 304 | Count -= 8; |
| 305 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(AMDGPU::S_NOP)) |
| 306 | .addImm(Arg); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | bool SIInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const { |
| 311 | SIMachineFunctionInfo *MFI = |
| 312 | MI->getParent()->getParent()->getInfo<SIMachineFunctionInfo>(); |
| 313 | MachineBasicBlock &MBB = *MI->getParent(); |
| 314 | DebugLoc DL = MBB.findDebugLoc(MI); |
| 315 | switch (MI->getOpcode()) { |
| 316 | default: return AMDGPUInstrInfo::expandPostRAPseudo(MI); |
| 317 | |
| 318 | // SGPR register spill |
| 319 | case AMDGPU::SI_SPILL_S512_SAVE: |
| 320 | case AMDGPU::SI_SPILL_S256_SAVE: |
| 321 | case AMDGPU::SI_SPILL_S128_SAVE: |
| 322 | case AMDGPU::SI_SPILL_S64_SAVE: { |
| 323 | unsigned NumSubRegs = getNumSubRegsForSpillOp(MI->getOpcode()); |
| 324 | unsigned FrameIndex = MI->getOperand(2).getImm(); |
| 325 | |
| 326 | for (unsigned i = 0, e = NumSubRegs; i < e; ++i) { |
| 327 | SIMachineFunctionInfo::SpilledReg Spill; |
| 328 | unsigned SubReg = RI.getPhysRegSubReg(MI->getOperand(1).getReg(), |
| 329 | &AMDGPU::SGPR_32RegClass, i); |
| 330 | Spill = MFI->SpillTracker.getSpilledReg(FrameIndex); |
| 331 | |
| 332 | BuildMI(MBB, MI, DL, get(AMDGPU::V_WRITELANE_B32), |
| 333 | MI->getOperand(0).getReg()) |
| 334 | .addReg(SubReg) |
| 335 | .addImm(Spill.Lane + i); |
| 336 | } |
| 337 | MI->eraseFromParent(); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | // SGPR register restore |
| 342 | case AMDGPU::SI_SPILL_S512_RESTORE: |
| 343 | case AMDGPU::SI_SPILL_S256_RESTORE: |
| 344 | case AMDGPU::SI_SPILL_S128_RESTORE: |
Tom Stellard | 060ae39 | 2014-06-10 21:20:38 +0000 | [diff] [blame] | 345 | case AMDGPU::SI_SPILL_S64_RESTORE: |
| 346 | case AMDGPU::SI_SPILL_S32_RESTORE: { |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 347 | unsigned NumSubRegs = getNumSubRegsForSpillOp(MI->getOpcode()); |
| 348 | |
| 349 | for (unsigned i = 0, e = NumSubRegs; i < e; ++i) { |
| 350 | SIMachineFunctionInfo::SpilledReg Spill; |
| 351 | unsigned FrameIndex = MI->getOperand(2).getImm(); |
| 352 | unsigned SubReg = RI.getPhysRegSubReg(MI->getOperand(0).getReg(), |
| 353 | &AMDGPU::SGPR_32RegClass, i); |
| 354 | Spill = MFI->SpillTracker.getSpilledReg(FrameIndex); |
| 355 | |
| 356 | BuildMI(MBB, MI, DL, get(AMDGPU::V_READLANE_B32), SubReg) |
| 357 | .addReg(MI->getOperand(1).getReg()) |
| 358 | .addImm(Spill.Lane + i); |
| 359 | } |
Tom Stellard | 060ae39 | 2014-06-10 21:20:38 +0000 | [diff] [blame] | 360 | insertNOPs(MI, 3); |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 361 | MI->eraseFromParent(); |
| 362 | break; |
| 363 | } |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 364 | case AMDGPU::SI_CONSTDATA_PTR: { |
| 365 | unsigned Reg = MI->getOperand(0).getReg(); |
| 366 | unsigned RegLo = RI.getSubReg(Reg, AMDGPU::sub0); |
| 367 | unsigned RegHi = RI.getSubReg(Reg, AMDGPU::sub1); |
| 368 | |
| 369 | BuildMI(MBB, MI, DL, get(AMDGPU::S_GETPC_B64), Reg); |
| 370 | |
| 371 | // Add 32-bit offset from this instruction to the start of the constant data. |
| 372 | BuildMI(MBB, MI, DL, get(AMDGPU::S_ADD_I32), RegLo) |
| 373 | .addReg(RegLo) |
| 374 | .addTargetIndex(AMDGPU::TI_CONSTDATA_START) |
| 375 | .addReg(AMDGPU::SCC, RegState::Define | RegState::Implicit); |
| 376 | BuildMI(MBB, MI, DL, get(AMDGPU::S_ADDC_U32), RegHi) |
| 377 | .addReg(RegHi) |
| 378 | .addImm(0) |
| 379 | .addReg(AMDGPU::SCC, RegState::Define | RegState::Implicit) |
| 380 | .addReg(AMDGPU::SCC, RegState::Implicit); |
| 381 | MI->eraseFromParent(); |
| 382 | break; |
| 383 | } |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | |
Christian Konig | 76edd4f | 2013-02-26 17:52:29 +0000 | [diff] [blame] | 388 | MachineInstr *SIInstrInfo::commuteInstruction(MachineInstr *MI, |
| 389 | bool NewMI) const { |
| 390 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 391 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 392 | if (MI->getNumOperands() < 3 || !MI->getOperand(1).isReg()) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 393 | return nullptr; |
Christian Konig | 76edd4f | 2013-02-26 17:52:29 +0000 | [diff] [blame] | 394 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 395 | // Cannot commute VOP2 if src0 is SGPR. |
| 396 | if (isVOP2(MI->getOpcode()) && MI->getOperand(1).isReg() && |
| 397 | RI.isSGPRClass(MRI.getRegClass(MI->getOperand(1).getReg()))) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 398 | return nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 399 | |
| 400 | if (!MI->getOperand(2).isReg()) { |
| 401 | // XXX: Commute instructions with FPImm operands |
| 402 | if (NewMI || MI->getOperand(2).isFPImm() || |
| 403 | (!isVOP2(MI->getOpcode()) && !isVOP3(MI->getOpcode()))) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 404 | return nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // XXX: Commute VOP3 instructions with abs and neg set. |
| 408 | if (isVOP3(MI->getOpcode()) && |
| 409 | (MI->getOperand(AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 410 | AMDGPU::OpName::abs)).getImm() || |
| 411 | MI->getOperand(AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 412 | AMDGPU::OpName::neg)).getImm())) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 413 | return nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 414 | |
| 415 | unsigned Reg = MI->getOperand(1).getReg(); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 416 | unsigned SubReg = MI->getOperand(1).getSubReg(); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 417 | MI->getOperand(1).ChangeToImmediate(MI->getOperand(2).getImm()); |
| 418 | MI->getOperand(2).ChangeToRegister(Reg, false); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 419 | MI->getOperand(2).setSubReg(SubReg); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 420 | } else { |
| 421 | MI = TargetInstrInfo::commuteInstruction(MI, NewMI); |
| 422 | } |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 423 | |
| 424 | if (MI) |
| 425 | MI->setDesc(get(commuteOpcode(MI->getOpcode()))); |
| 426 | |
| 427 | return MI; |
Christian Konig | 76edd4f | 2013-02-26 17:52:29 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Tom Stellard | 26a3b67 | 2013-10-22 18:19:10 +0000 | [diff] [blame] | 430 | MachineInstr *SIInstrInfo::buildMovInstr(MachineBasicBlock *MBB, |
| 431 | MachineBasicBlock::iterator I, |
| 432 | unsigned DstReg, |
| 433 | unsigned SrcReg) const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 434 | return BuildMI(*MBB, I, MBB->findDebugLoc(I), get(AMDGPU::V_MOV_B32_e32), |
| 435 | DstReg) .addReg(SrcReg); |
Tom Stellard | 26a3b67 | 2013-10-22 18:19:10 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 438 | bool SIInstrInfo::isMov(unsigned Opcode) const { |
| 439 | switch(Opcode) { |
| 440 | default: return false; |
| 441 | case AMDGPU::S_MOV_B32: |
| 442 | case AMDGPU::S_MOV_B64: |
| 443 | case AMDGPU::V_MOV_B32_e32: |
| 444 | case AMDGPU::V_MOV_B32_e64: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | bool |
| 450 | SIInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const { |
| 451 | return RC != &AMDGPU::EXECRegRegClass; |
| 452 | } |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 453 | |
Tom Stellard | 30f5941 | 2014-03-31 14:01:56 +0000 | [diff] [blame] | 454 | bool |
| 455 | SIInstrInfo::isTriviallyReMaterializable(const MachineInstr *MI, |
| 456 | AliasAnalysis *AA) const { |
| 457 | switch(MI->getOpcode()) { |
| 458 | default: return AMDGPUInstrInfo::isTriviallyReMaterializable(MI, AA); |
| 459 | case AMDGPU::S_MOV_B32: |
| 460 | case AMDGPU::S_MOV_B64: |
| 461 | case AMDGPU::V_MOV_B32_e32: |
| 462 | return MI->getOperand(1).isImm(); |
| 463 | } |
| 464 | } |
| 465 | |
Tom Stellard | 5d7aaae | 2014-02-10 16:58:30 +0000 | [diff] [blame] | 466 | namespace llvm { |
| 467 | namespace AMDGPU { |
| 468 | // Helper function generated by tablegen. We are wrapping this with |
| 469 | // an SIInstrInfo function that reutrns bool rather than int. |
| 470 | int isDS(uint16_t Opcode); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | bool SIInstrInfo::isDS(uint16_t Opcode) const { |
| 475 | return ::AMDGPU::isDS(Opcode) != -1; |
| 476 | } |
| 477 | |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 478 | int SIInstrInfo::isMIMG(uint16_t Opcode) const { |
| 479 | return get(Opcode).TSFlags & SIInstrFlags::MIMG; |
| 480 | } |
| 481 | |
Michel Danzer | 20680b1 | 2013-08-16 16:19:24 +0000 | [diff] [blame] | 482 | int SIInstrInfo::isSMRD(uint16_t Opcode) const { |
| 483 | return get(Opcode).TSFlags & SIInstrFlags::SMRD; |
| 484 | } |
| 485 | |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 486 | bool SIInstrInfo::isVOP1(uint16_t Opcode) const { |
| 487 | return get(Opcode).TSFlags & SIInstrFlags::VOP1; |
| 488 | } |
| 489 | |
| 490 | bool SIInstrInfo::isVOP2(uint16_t Opcode) const { |
| 491 | return get(Opcode).TSFlags & SIInstrFlags::VOP2; |
| 492 | } |
| 493 | |
| 494 | bool SIInstrInfo::isVOP3(uint16_t Opcode) const { |
| 495 | return get(Opcode).TSFlags & SIInstrFlags::VOP3; |
| 496 | } |
| 497 | |
| 498 | bool SIInstrInfo::isVOPC(uint16_t Opcode) const { |
| 499 | return get(Opcode).TSFlags & SIInstrFlags::VOPC; |
| 500 | } |
| 501 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 502 | bool SIInstrInfo::isSALUInstr(const MachineInstr &MI) const { |
| 503 | return get(MI.getOpcode()).TSFlags & SIInstrFlags::SALU; |
| 504 | } |
| 505 | |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 506 | bool SIInstrInfo::isInlineConstant(const APInt &Imm) const { |
| 507 | int32_t Val = Imm.getSExtValue(); |
| 508 | if (Val >= -16 && Val <= 64) |
| 509 | return true; |
Tom Stellard | d008446 | 2014-03-17 17:03:52 +0000 | [diff] [blame] | 510 | |
| 511 | // The actual type of the operand does not seem to matter as long |
| 512 | // as the bits match one of the inline immediate values. For example: |
| 513 | // |
| 514 | // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal, |
| 515 | // so it is a legal inline immediate. |
| 516 | // |
| 517 | // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in |
| 518 | // floating-point, so it is a legal inline immediate. |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 519 | |
| 520 | return (APInt::floatToBits(0.0f) == Imm) || |
| 521 | (APInt::floatToBits(1.0f) == Imm) || |
| 522 | (APInt::floatToBits(-1.0f) == Imm) || |
| 523 | (APInt::floatToBits(0.5f) == Imm) || |
| 524 | (APInt::floatToBits(-0.5f) == Imm) || |
| 525 | (APInt::floatToBits(2.0f) == Imm) || |
| 526 | (APInt::floatToBits(-2.0f) == Imm) || |
| 527 | (APInt::floatToBits(4.0f) == Imm) || |
| 528 | (APInt::floatToBits(-4.0f) == Imm); |
| 529 | } |
| 530 | |
| 531 | bool SIInstrInfo::isInlineConstant(const MachineOperand &MO) const { |
| 532 | if (MO.isImm()) |
| 533 | return isInlineConstant(APInt(32, MO.getImm(), true)); |
| 534 | |
| 535 | if (MO.isFPImm()) { |
| 536 | APFloat FpImm = MO.getFPImm()->getValueAPF(); |
| 537 | return isInlineConstant(FpImm.bitcastToAPInt()); |
| 538 | } |
| 539 | |
| 540 | return false; |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | bool SIInstrInfo::isLiteralConstant(const MachineOperand &MO) const { |
| 544 | return (MO.isImm() || MO.isFPImm()) && !isInlineConstant(MO); |
| 545 | } |
| 546 | |
Matt Arsenault | becb140 | 2014-06-23 18:28:31 +0000 | [diff] [blame] | 547 | static bool compareMachineOp(const MachineOperand &Op0, |
| 548 | const MachineOperand &Op1) { |
| 549 | if (Op0.getType() != Op1.getType()) |
| 550 | return false; |
| 551 | |
| 552 | switch (Op0.getType()) { |
| 553 | case MachineOperand::MO_Register: |
| 554 | return Op0.getReg() == Op1.getReg(); |
| 555 | case MachineOperand::MO_Immediate: |
| 556 | return Op0.getImm() == Op1.getImm(); |
| 557 | case MachineOperand::MO_FPImmediate: |
| 558 | return Op0.getFPImm() == Op1.getFPImm(); |
| 559 | default: |
| 560 | llvm_unreachable("Didn't expect to be comparing these operand types"); |
| 561 | } |
| 562 | } |
| 563 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame^] | 564 | bool SIInstrInfo::isImmOperandLegal(const MachineInstr *MI, unsigned OpNo, |
| 565 | const MachineOperand &MO) const { |
| 566 | const MCOperandInfo &OpInfo = get(MI->getOpcode()).OpInfo[OpNo]; |
| 567 | |
| 568 | assert(MO.isImm() || MO.isFPImm()); |
| 569 | |
| 570 | if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE) |
| 571 | return true; |
| 572 | |
| 573 | if (OpInfo.RegClass < 0) |
| 574 | return false; |
| 575 | |
| 576 | return RI.regClassCanUseImmediate(OpInfo.RegClass); |
| 577 | } |
| 578 | |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 579 | bool SIInstrInfo::verifyInstruction(const MachineInstr *MI, |
| 580 | StringRef &ErrInfo) const { |
| 581 | uint16_t Opcode = MI->getOpcode(); |
| 582 | int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); |
| 583 | int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1); |
| 584 | int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2); |
| 585 | |
Tom Stellard | ca700e4 | 2014-03-17 17:03:49 +0000 | [diff] [blame] | 586 | // Make sure the number of operands is correct. |
| 587 | const MCInstrDesc &Desc = get(Opcode); |
| 588 | if (!Desc.isVariadic() && |
| 589 | Desc.getNumOperands() != MI->getNumExplicitOperands()) { |
| 590 | ErrInfo = "Instruction has wrong number of operands."; |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | // Make sure the register classes are correct |
| 595 | for (unsigned i = 0, e = Desc.getNumOperands(); i != e; ++i) { |
| 596 | switch (Desc.OpInfo[i].OperandType) { |
Tom Stellard | a305f93 | 2014-07-02 20:53:44 +0000 | [diff] [blame] | 597 | case MCOI::OPERAND_REGISTER: { |
| 598 | int RegClass = Desc.OpInfo[i].RegClass; |
| 599 | if (!RI.regClassCanUseImmediate(RegClass) && |
| 600 | (MI->getOperand(i).isImm() || MI->getOperand(i).isFPImm())) { |
| 601 | ErrInfo = "Expected register, but got immediate"; |
| 602 | return false; |
| 603 | } |
| 604 | } |
Tom Stellard | ca700e4 | 2014-03-17 17:03:49 +0000 | [diff] [blame] | 605 | break; |
| 606 | case MCOI::OPERAND_IMMEDIATE: |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame^] | 607 | // Check if this operand is an immediate. |
| 608 | // FrameIndex operands will be replaced by immediates, so they are |
| 609 | // allowed. |
| 610 | if (!MI->getOperand(i).isImm() && !MI->getOperand(i).isFPImm() && |
| 611 | !MI->getOperand(i).isFI()) { |
Tom Stellard | ca700e4 | 2014-03-17 17:03:49 +0000 | [diff] [blame] | 612 | ErrInfo = "Expected immediate, but got non-immediate"; |
| 613 | return false; |
| 614 | } |
| 615 | // Fall-through |
| 616 | default: |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | if (!MI->getOperand(i).isReg()) |
| 621 | continue; |
| 622 | |
| 623 | int RegClass = Desc.OpInfo[i].RegClass; |
| 624 | if (RegClass != -1) { |
| 625 | unsigned Reg = MI->getOperand(i).getReg(); |
| 626 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 627 | continue; |
| 628 | |
| 629 | const TargetRegisterClass *RC = RI.getRegClass(RegClass); |
| 630 | if (!RC->contains(Reg)) { |
| 631 | ErrInfo = "Operand has incorrect register class."; |
| 632 | return false; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 638 | // Verify VOP* |
| 639 | if (isVOP1(Opcode) || isVOP2(Opcode) || isVOP3(Opcode) || isVOPC(Opcode)) { |
| 640 | unsigned ConstantBusCount = 0; |
| 641 | unsigned SGPRUsed = AMDGPU::NoRegister; |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 642 | for (int i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 643 | const MachineOperand &MO = MI->getOperand(i); |
| 644 | if (MO.isReg() && MO.isUse() && |
| 645 | !TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 646 | |
| 647 | // EXEC register uses the constant bus. |
| 648 | if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC) |
| 649 | ++ConstantBusCount; |
| 650 | |
| 651 | // SGPRs use the constant bus |
| 652 | if (MO.getReg() == AMDGPU::M0 || MO.getReg() == AMDGPU::VCC || |
| 653 | (!MO.isImplicit() && |
| 654 | (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) || |
| 655 | AMDGPU::SGPR_64RegClass.contains(MO.getReg())))) { |
| 656 | if (SGPRUsed != MO.getReg()) { |
| 657 | ++ConstantBusCount; |
| 658 | SGPRUsed = MO.getReg(); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | // Literal constants use the constant bus. |
| 663 | if (isLiteralConstant(MO)) |
| 664 | ++ConstantBusCount; |
| 665 | } |
| 666 | if (ConstantBusCount > 1) { |
| 667 | ErrInfo = "VOP* instruction uses the constant bus more than once"; |
| 668 | return false; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // Verify SRC1 for VOP2 and VOPC |
| 673 | if (Src1Idx != -1 && (isVOP2(Opcode) || isVOPC(Opcode))) { |
| 674 | const MachineOperand &Src1 = MI->getOperand(Src1Idx); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 675 | if (Src1.isImm() || Src1.isFPImm()) { |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 676 | ErrInfo = "VOP[2C] src1 cannot be an immediate."; |
| 677 | return false; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // Verify VOP3 |
| 682 | if (isVOP3(Opcode)) { |
| 683 | if (Src0Idx != -1 && isLiteralConstant(MI->getOperand(Src0Idx))) { |
| 684 | ErrInfo = "VOP3 src0 cannot be a literal constant."; |
| 685 | return false; |
| 686 | } |
| 687 | if (Src1Idx != -1 && isLiteralConstant(MI->getOperand(Src1Idx))) { |
| 688 | ErrInfo = "VOP3 src1 cannot be a literal constant."; |
| 689 | return false; |
| 690 | } |
| 691 | if (Src2Idx != -1 && isLiteralConstant(MI->getOperand(Src2Idx))) { |
| 692 | ErrInfo = "VOP3 src2 cannot be a literal constant."; |
| 693 | return false; |
| 694 | } |
| 695 | } |
Matt Arsenault | becb140 | 2014-06-23 18:28:31 +0000 | [diff] [blame] | 696 | |
| 697 | // Verify misc. restrictions on specific instructions. |
| 698 | if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32 || |
| 699 | Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64) { |
| 700 | MI->dump(); |
| 701 | |
| 702 | const MachineOperand &Src0 = MI->getOperand(2); |
| 703 | const MachineOperand &Src1 = MI->getOperand(3); |
| 704 | const MachineOperand &Src2 = MI->getOperand(4); |
| 705 | if (Src0.isReg() && Src1.isReg() && Src2.isReg()) { |
| 706 | if (!compareMachineOp(Src0, Src1) && |
| 707 | !compareMachineOp(Src0, Src2)) { |
| 708 | ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2"; |
| 709 | return false; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 714 | return true; |
| 715 | } |
| 716 | |
Matt Arsenault | f14032a | 2013-11-15 22:02:28 +0000 | [diff] [blame] | 717 | unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) { |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 718 | switch (MI.getOpcode()) { |
| 719 | default: return AMDGPU::INSTRUCTION_LIST_END; |
| 720 | case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; |
| 721 | case AMDGPU::COPY: return AMDGPU::COPY; |
| 722 | case AMDGPU::PHI: return AMDGPU::PHI; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 723 | case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 724 | case AMDGPU::S_MOV_B32: |
| 725 | return MI.getOperand(1).isReg() ? |
Tom Stellard | 8c12fd9 | 2014-03-24 16:12:34 +0000 | [diff] [blame] | 726 | AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; |
Matt Arsenault | 43b8e4e | 2013-11-18 20:09:29 +0000 | [diff] [blame] | 727 | case AMDGPU::S_ADD_I32: return AMDGPU::V_ADD_I32_e32; |
| 728 | case AMDGPU::S_ADDC_U32: return AMDGPU::V_ADDC_U32_e32; |
| 729 | case AMDGPU::S_SUB_I32: return AMDGPU::V_SUB_I32_e32; |
| 730 | case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; |
Matt Arsenault | 8e2581b | 2014-03-21 18:01:18 +0000 | [diff] [blame] | 731 | case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e32; |
| 732 | case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e32; |
| 733 | case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e32; |
| 734 | case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e32; |
| 735 | case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e32; |
| 736 | case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e32; |
| 737 | case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e32; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 738 | case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; |
| 739 | case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64; |
| 740 | case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; |
| 741 | case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64; |
| 742 | case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; |
| 743 | case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64; |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 744 | case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32; |
| 745 | case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32; |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 746 | case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32; |
| 747 | case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32; |
Matt Arsenault | 43160e7 | 2014-06-18 17:13:57 +0000 | [diff] [blame] | 748 | case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; |
Matt Arsenault | 2c33562 | 2014-04-09 07:16:16 +0000 | [diff] [blame] | 749 | case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 750 | case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; |
Matt Arsenault | 0cb92e1 | 2014-04-11 19:25:18 +0000 | [diff] [blame] | 751 | case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32; |
| 752 | case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32; |
| 753 | case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32; |
| 754 | case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32; |
| 755 | case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32; |
| 756 | case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32; |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 757 | case AMDGPU::S_LOAD_DWORD_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 758 | case AMDGPU::S_LOAD_DWORD_SGPR: return AMDGPU::BUFFER_LOAD_DWORD_ADDR64; |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 759 | case AMDGPU::S_LOAD_DWORDX2_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 760 | case AMDGPU::S_LOAD_DWORDX2_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64; |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 761 | case AMDGPU::S_LOAD_DWORDX4_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 762 | case AMDGPU::S_LOAD_DWORDX4_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64; |
Matt Arsenault | b5b5110 | 2014-06-10 19:18:21 +0000 | [diff] [blame] | 763 | case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e32; |
Matt Arsenault | 295b86e | 2014-06-17 17:36:27 +0000 | [diff] [blame] | 764 | case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; |
Matt Arsenault | 8579601 | 2014-06-17 17:36:24 +0000 | [diff] [blame] | 765 | case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const { |
| 770 | return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END; |
| 771 | } |
| 772 | |
| 773 | const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, |
| 774 | unsigned OpNo) const { |
| 775 | const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); |
| 776 | const MCInstrDesc &Desc = get(MI.getOpcode()); |
| 777 | if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || |
| 778 | Desc.OpInfo[OpNo].RegClass == -1) |
| 779 | return MRI.getRegClass(MI.getOperand(OpNo).getReg()); |
| 780 | |
| 781 | unsigned RCID = Desc.OpInfo[OpNo].RegClass; |
| 782 | return RI.getRegClass(RCID); |
| 783 | } |
| 784 | |
| 785 | bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const { |
| 786 | switch (MI.getOpcode()) { |
| 787 | case AMDGPU::COPY: |
| 788 | case AMDGPU::REG_SEQUENCE: |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 789 | case AMDGPU::PHI: |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 790 | case AMDGPU::INSERT_SUBREG: |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 791 | return RI.hasVGPRs(getOpRegClass(MI, 0)); |
| 792 | default: |
| 793 | return RI.hasVGPRs(getOpRegClass(MI, OpNo)); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | void SIInstrInfo::legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const { |
| 798 | MachineBasicBlock::iterator I = MI; |
| 799 | MachineOperand &MO = MI->getOperand(OpIdx); |
| 800 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 801 | unsigned RCID = get(MI->getOpcode()).OpInfo[OpIdx].RegClass; |
| 802 | const TargetRegisterClass *RC = RI.getRegClass(RCID); |
| 803 | unsigned Opcode = AMDGPU::V_MOV_B32_e32; |
| 804 | if (MO.isReg()) { |
| 805 | Opcode = AMDGPU::COPY; |
| 806 | } else if (RI.isSGPRClass(RC)) { |
Matt Arsenault | 671a005 | 2013-11-14 10:08:50 +0000 | [diff] [blame] | 807 | Opcode = AMDGPU::S_MOV_B32; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Matt Arsenault | 3a4d86a | 2013-11-18 20:09:55 +0000 | [diff] [blame] | 810 | const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); |
| 811 | unsigned Reg = MRI.createVirtualRegister(VRC); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 812 | BuildMI(*MI->getParent(), I, MI->getParent()->findDebugLoc(I), get(Opcode), |
| 813 | Reg).addOperand(MO); |
| 814 | MO.ChangeToRegister(Reg, false); |
| 815 | } |
| 816 | |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 817 | unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, |
| 818 | MachineRegisterInfo &MRI, |
| 819 | MachineOperand &SuperReg, |
| 820 | const TargetRegisterClass *SuperRC, |
| 821 | unsigned SubIdx, |
| 822 | const TargetRegisterClass *SubRC) |
| 823 | const { |
| 824 | assert(SuperReg.isReg()); |
| 825 | |
| 826 | unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC); |
| 827 | unsigned SubReg = MRI.createVirtualRegister(SubRC); |
| 828 | |
| 829 | // Just in case the super register is itself a sub-register, copy it to a new |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 830 | // value so we don't need to worry about merging its subreg index with the |
| 831 | // SubIdx passed to this function. The register coalescer should be able to |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 832 | // eliminate this extra copy. |
| 833 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY), |
| 834 | NewSuperReg) |
| 835 | .addOperand(SuperReg); |
| 836 | |
| 837 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY), |
| 838 | SubReg) |
| 839 | .addReg(NewSuperReg, 0, SubIdx); |
| 840 | return SubReg; |
| 841 | } |
| 842 | |
Matt Arsenault | 248b7b6 | 2014-03-24 20:08:09 +0000 | [diff] [blame] | 843 | MachineOperand SIInstrInfo::buildExtractSubRegOrImm( |
| 844 | MachineBasicBlock::iterator MII, |
| 845 | MachineRegisterInfo &MRI, |
| 846 | MachineOperand &Op, |
| 847 | const TargetRegisterClass *SuperRC, |
| 848 | unsigned SubIdx, |
| 849 | const TargetRegisterClass *SubRC) const { |
| 850 | if (Op.isImm()) { |
| 851 | // XXX - Is there a better way to do this? |
| 852 | if (SubIdx == AMDGPU::sub0) |
| 853 | return MachineOperand::CreateImm(Op.getImm() & 0xFFFFFFFF); |
| 854 | if (SubIdx == AMDGPU::sub1) |
| 855 | return MachineOperand::CreateImm(Op.getImm() >> 32); |
| 856 | |
| 857 | llvm_unreachable("Unhandled register index for immediate"); |
| 858 | } |
| 859 | |
| 860 | unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, |
| 861 | SubIdx, SubRC); |
| 862 | return MachineOperand::CreateReg(SubReg, false); |
| 863 | } |
| 864 | |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 865 | unsigned SIInstrInfo::split64BitImm(SmallVectorImpl<MachineInstr *> &Worklist, |
| 866 | MachineBasicBlock::iterator MI, |
| 867 | MachineRegisterInfo &MRI, |
| 868 | const TargetRegisterClass *RC, |
| 869 | const MachineOperand &Op) const { |
| 870 | MachineBasicBlock *MBB = MI->getParent(); |
| 871 | DebugLoc DL = MI->getDebugLoc(); |
| 872 | unsigned LoDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 873 | unsigned HiDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 874 | unsigned Dst = MRI.createVirtualRegister(RC); |
| 875 | |
| 876 | MachineInstr *Lo = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32), |
| 877 | LoDst) |
| 878 | .addImm(Op.getImm() & 0xFFFFFFFF); |
| 879 | MachineInstr *Hi = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32), |
| 880 | HiDst) |
| 881 | .addImm(Op.getImm() >> 32); |
| 882 | |
| 883 | BuildMI(*MBB, MI, DL, get(TargetOpcode::REG_SEQUENCE), Dst) |
| 884 | .addReg(LoDst) |
| 885 | .addImm(AMDGPU::sub0) |
| 886 | .addReg(HiDst) |
| 887 | .addImm(AMDGPU::sub1); |
| 888 | |
| 889 | Worklist.push_back(Lo); |
| 890 | Worklist.push_back(Hi); |
| 891 | |
| 892 | return Dst; |
| 893 | } |
| 894 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 895 | void SIInstrInfo::legalizeOperands(MachineInstr *MI) const { |
| 896 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 897 | int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 898 | AMDGPU::OpName::src0); |
| 899 | int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 900 | AMDGPU::OpName::src1); |
| 901 | int Src2Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 902 | AMDGPU::OpName::src2); |
| 903 | |
| 904 | // Legalize VOP2 |
| 905 | if (isVOP2(MI->getOpcode()) && Src1Idx != -1) { |
Matt Arsenault | 08f7e37 | 2013-11-18 20:09:50 +0000 | [diff] [blame] | 906 | MachineOperand &Src0 = MI->getOperand(Src0Idx); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 907 | MachineOperand &Src1 = MI->getOperand(Src1Idx); |
Matt Arsenault | f476045 | 2013-11-14 08:06:38 +0000 | [diff] [blame] | 908 | |
Matt Arsenault | 08f7e37 | 2013-11-18 20:09:50 +0000 | [diff] [blame] | 909 | // If the instruction implicitly reads VCC, we can't have any SGPR operands, |
| 910 | // so move any. |
| 911 | bool ReadsVCC = MI->readsRegister(AMDGPU::VCC, &RI); |
| 912 | if (ReadsVCC && Src0.isReg() && |
| 913 | RI.isSGPRClass(MRI.getRegClass(Src0.getReg()))) { |
| 914 | legalizeOpWithMove(MI, Src0Idx); |
| 915 | return; |
| 916 | } |
| 917 | |
| 918 | if (ReadsVCC && Src1.isReg() && |
| 919 | RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) { |
| 920 | legalizeOpWithMove(MI, Src1Idx); |
| 921 | return; |
| 922 | } |
| 923 | |
Matt Arsenault | f476045 | 2013-11-14 08:06:38 +0000 | [diff] [blame] | 924 | // Legalize VOP2 instructions where src1 is not a VGPR. An SGPR input must |
| 925 | // be the first operand, and there can only be one. |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 926 | if (Src1.isImm() || Src1.isFPImm() || |
| 927 | (Src1.isReg() && RI.isSGPRClass(MRI.getRegClass(Src1.getReg())))) { |
| 928 | if (MI->isCommutable()) { |
| 929 | if (commuteInstruction(MI)) |
| 930 | return; |
| 931 | } |
| 932 | legalizeOpWithMove(MI, Src1Idx); |
| 933 | } |
| 934 | } |
| 935 | |
Matt Arsenault | 08f7e37 | 2013-11-18 20:09:50 +0000 | [diff] [blame] | 936 | // XXX - Do any VOP3 instructions read VCC? |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 937 | // Legalize VOP3 |
| 938 | if (isVOP3(MI->getOpcode())) { |
| 939 | int VOP3Idx[3] = {Src0Idx, Src1Idx, Src2Idx}; |
| 940 | unsigned SGPRReg = AMDGPU::NoRegister; |
| 941 | for (unsigned i = 0; i < 3; ++i) { |
| 942 | int Idx = VOP3Idx[i]; |
| 943 | if (Idx == -1) |
| 944 | continue; |
| 945 | MachineOperand &MO = MI->getOperand(Idx); |
| 946 | |
| 947 | if (MO.isReg()) { |
| 948 | if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg()))) |
| 949 | continue; // VGPRs are legal |
| 950 | |
Matt Arsenault | f0b1e3a | 2013-11-18 20:09:21 +0000 | [diff] [blame] | 951 | assert(MO.getReg() != AMDGPU::SCC && "SCC operand to VOP3 instruction"); |
| 952 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 953 | if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) { |
| 954 | SGPRReg = MO.getReg(); |
| 955 | // We can use one SGPR in each VOP3 instruction. |
| 956 | continue; |
| 957 | } |
| 958 | } else if (!isLiteralConstant(MO)) { |
| 959 | // If it is not a register and not a literal constant, then it must be |
| 960 | // an inline constant which is always legal. |
| 961 | continue; |
| 962 | } |
| 963 | // If we make it this far, then the operand is not legal and we must |
| 964 | // legalize it. |
| 965 | legalizeOpWithMove(MI, Idx); |
| 966 | } |
| 967 | } |
| 968 | |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 969 | // Legalize REG_SEQUENCE and PHI |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 970 | // The register class of the operands much be the same type as the register |
| 971 | // class of the output. |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 972 | if (MI->getOpcode() == AMDGPU::REG_SEQUENCE || |
| 973 | MI->getOpcode() == AMDGPU::PHI) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 974 | const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 975 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) { |
| 976 | if (!MI->getOperand(i).isReg() || |
| 977 | !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg())) |
| 978 | continue; |
| 979 | const TargetRegisterClass *OpRC = |
| 980 | MRI.getRegClass(MI->getOperand(i).getReg()); |
| 981 | if (RI.hasVGPRs(OpRC)) { |
| 982 | VRC = OpRC; |
| 983 | } else { |
| 984 | SRC = OpRC; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | // If any of the operands are VGPR registers, then they all most be |
| 989 | // otherwise we will create illegal VGPR->SGPR copies when legalizing |
| 990 | // them. |
| 991 | if (VRC || !RI.isSGPRClass(getOpRegClass(*MI, 0))) { |
| 992 | if (!VRC) { |
| 993 | assert(SRC); |
| 994 | VRC = RI.getEquivalentVGPRClass(SRC); |
| 995 | } |
| 996 | RC = VRC; |
| 997 | } else { |
| 998 | RC = SRC; |
| 999 | } |
| 1000 | |
| 1001 | // Update all the operands so they have the same type. |
| 1002 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) { |
| 1003 | if (!MI->getOperand(i).isReg() || |
| 1004 | !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg())) |
| 1005 | continue; |
| 1006 | unsigned DstReg = MRI.createVirtualRegister(RC); |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 1007 | MachineBasicBlock *InsertBB; |
| 1008 | MachineBasicBlock::iterator Insert; |
| 1009 | if (MI->getOpcode() == AMDGPU::REG_SEQUENCE) { |
| 1010 | InsertBB = MI->getParent(); |
| 1011 | Insert = MI; |
| 1012 | } else { |
| 1013 | // MI is a PHI instruction. |
| 1014 | InsertBB = MI->getOperand(i + 1).getMBB(); |
| 1015 | Insert = InsertBB->getFirstTerminator(); |
| 1016 | } |
| 1017 | BuildMI(*InsertBB, Insert, MI->getDebugLoc(), |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1018 | get(AMDGPU::COPY), DstReg) |
| 1019 | .addOperand(MI->getOperand(i)); |
| 1020 | MI->getOperand(i).setReg(DstReg); |
| 1021 | } |
| 1022 | } |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 1023 | |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 1024 | // Legalize INSERT_SUBREG |
| 1025 | // src0 must have the same register class as dst |
| 1026 | if (MI->getOpcode() == AMDGPU::INSERT_SUBREG) { |
| 1027 | unsigned Dst = MI->getOperand(0).getReg(); |
| 1028 | unsigned Src0 = MI->getOperand(1).getReg(); |
| 1029 | const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); |
| 1030 | const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); |
| 1031 | if (DstRC != Src0RC) { |
| 1032 | MachineBasicBlock &MBB = *MI->getParent(); |
| 1033 | unsigned NewSrc0 = MRI.createVirtualRegister(DstRC); |
| 1034 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::COPY), NewSrc0) |
| 1035 | .addReg(Src0); |
| 1036 | MI->getOperand(1).setReg(NewSrc0); |
| 1037 | } |
| 1038 | return; |
| 1039 | } |
| 1040 | |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 1041 | // Legalize MUBUF* instructions |
| 1042 | // FIXME: If we start using the non-addr64 instructions for compute, we |
| 1043 | // may need to legalize them here. |
| 1044 | |
| 1045 | int SRsrcIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 1046 | AMDGPU::OpName::srsrc); |
| 1047 | int VAddrIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 1048 | AMDGPU::OpName::vaddr); |
| 1049 | if (SRsrcIdx != -1 && VAddrIdx != -1) { |
| 1050 | const TargetRegisterClass *VAddrRC = |
| 1051 | RI.getRegClass(get(MI->getOpcode()).OpInfo[VAddrIdx].RegClass); |
| 1052 | |
| 1053 | if(VAddrRC->getSize() == 8 && |
| 1054 | MRI.getRegClass(MI->getOperand(SRsrcIdx).getReg()) != VAddrRC) { |
| 1055 | // We have a MUBUF instruction that uses a 64-bit vaddr register and |
| 1056 | // srsrc has the incorrect register class. In order to fix this, we |
| 1057 | // need to extract the pointer from the resource descriptor (srsrc), |
| 1058 | // add it to the value of vadd, then store the result in the vaddr |
| 1059 | // operand. Then, we need to set the pointer field of the resource |
| 1060 | // descriptor to zero. |
| 1061 | |
| 1062 | MachineBasicBlock &MBB = *MI->getParent(); |
| 1063 | MachineOperand &SRsrcOp = MI->getOperand(SRsrcIdx); |
| 1064 | MachineOperand &VAddrOp = MI->getOperand(VAddrIdx); |
| 1065 | unsigned SRsrcPtrLo, SRsrcPtrHi, VAddrLo, VAddrHi; |
| 1066 | unsigned NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
| 1067 | unsigned NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
| 1068 | unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); |
| 1069 | unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 1070 | unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1071 | unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1072 | unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); |
| 1073 | |
| 1074 | // SRsrcPtrLo = srsrc:sub0 |
| 1075 | SRsrcPtrLo = buildExtractSubReg(MI, MRI, SRsrcOp, |
| 1076 | &AMDGPU::VReg_128RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass); |
| 1077 | |
| 1078 | // SRsrcPtrHi = srsrc:sub1 |
| 1079 | SRsrcPtrHi = buildExtractSubReg(MI, MRI, SRsrcOp, |
| 1080 | &AMDGPU::VReg_128RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass); |
| 1081 | |
| 1082 | // VAddrLo = vaddr:sub0 |
| 1083 | VAddrLo = buildExtractSubReg(MI, MRI, VAddrOp, |
| 1084 | &AMDGPU::VReg_64RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass); |
| 1085 | |
| 1086 | // VAddrHi = vaddr:sub1 |
| 1087 | VAddrHi = buildExtractSubReg(MI, MRI, VAddrOp, |
| 1088 | &AMDGPU::VReg_64RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass); |
| 1089 | |
| 1090 | // NewVaddrLo = SRsrcPtrLo + VAddrLo |
| 1091 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADD_I32_e32), |
| 1092 | NewVAddrLo) |
| 1093 | .addReg(SRsrcPtrLo) |
| 1094 | .addReg(VAddrLo) |
| 1095 | .addReg(AMDGPU::VCC, RegState::Define | RegState::Implicit); |
| 1096 | |
| 1097 | // NewVaddrHi = SRsrcPtrHi + VAddrHi |
| 1098 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADDC_U32_e32), |
| 1099 | NewVAddrHi) |
| 1100 | .addReg(SRsrcPtrHi) |
| 1101 | .addReg(VAddrHi) |
| 1102 | .addReg(AMDGPU::VCC, RegState::ImplicitDefine) |
| 1103 | .addReg(AMDGPU::VCC, RegState::Implicit); |
| 1104 | |
| 1105 | // NewVaddr = {NewVaddrHi, NewVaddrLo} |
| 1106 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), |
| 1107 | NewVAddr) |
| 1108 | .addReg(NewVAddrLo) |
| 1109 | .addImm(AMDGPU::sub0) |
| 1110 | .addReg(NewVAddrHi) |
| 1111 | .addImm(AMDGPU::sub1); |
| 1112 | |
| 1113 | // Zero64 = 0 |
| 1114 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B64), |
| 1115 | Zero64) |
| 1116 | .addImm(0); |
| 1117 | |
| 1118 | // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} |
| 1119 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1120 | SRsrcFormatLo) |
| 1121 | .addImm(AMDGPU::RSRC_DATA_FORMAT & 0xFFFFFFFF); |
| 1122 | |
| 1123 | // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} |
| 1124 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1125 | SRsrcFormatHi) |
| 1126 | .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32); |
| 1127 | |
| 1128 | // NewSRsrc = {Zero64, SRsrcFormat} |
| 1129 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), |
| 1130 | NewSRsrc) |
| 1131 | .addReg(Zero64) |
| 1132 | .addImm(AMDGPU::sub0_sub1) |
| 1133 | .addReg(SRsrcFormatLo) |
| 1134 | .addImm(AMDGPU::sub2) |
| 1135 | .addReg(SRsrcFormatHi) |
| 1136 | .addImm(AMDGPU::sub3); |
| 1137 | |
| 1138 | // Update the instruction to use NewVaddr |
| 1139 | MI->getOperand(VAddrIdx).setReg(NewVAddr); |
| 1140 | // Update the instruction to use NewSRsrc |
| 1141 | MI->getOperand(SRsrcIdx).setReg(NewSRsrc); |
| 1142 | } |
| 1143 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1146 | void SIInstrInfo::moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI) const { |
| 1147 | MachineBasicBlock *MBB = MI->getParent(); |
| 1148 | switch (MI->getOpcode()) { |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1149 | case AMDGPU::S_LOAD_DWORD_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1150 | case AMDGPU::S_LOAD_DWORD_SGPR: |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1151 | case AMDGPU::S_LOAD_DWORDX2_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1152 | case AMDGPU::S_LOAD_DWORDX2_SGPR: |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1153 | case AMDGPU::S_LOAD_DWORDX4_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1154 | case AMDGPU::S_LOAD_DWORDX4_SGPR: |
| 1155 | unsigned NewOpcode = getVALUOp(*MI); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1156 | unsigned RegOffset; |
| 1157 | unsigned ImmOffset; |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1158 | |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1159 | if (MI->getOperand(2).isReg()) { |
| 1160 | RegOffset = MI->getOperand(2).getReg(); |
| 1161 | ImmOffset = 0; |
| 1162 | } else { |
| 1163 | assert(MI->getOperand(2).isImm()); |
| 1164 | // SMRD instructions take a dword offsets and MUBUF instructions |
| 1165 | // take a byte offset. |
| 1166 | ImmOffset = MI->getOperand(2).getImm() << 2; |
| 1167 | RegOffset = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1168 | if (isUInt<12>(ImmOffset)) { |
| 1169 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1170 | RegOffset) |
| 1171 | .addImm(0); |
| 1172 | } else { |
| 1173 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1174 | RegOffset) |
| 1175 | .addImm(ImmOffset); |
| 1176 | ImmOffset = 0; |
| 1177 | } |
| 1178 | } |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1179 | |
| 1180 | unsigned SRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1181 | unsigned DWord0 = RegOffset; |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1182 | unsigned DWord1 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1183 | unsigned DWord2 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1184 | unsigned DWord3 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1185 | |
| 1186 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord1) |
| 1187 | .addImm(0); |
| 1188 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord2) |
| 1189 | .addImm(AMDGPU::RSRC_DATA_FORMAT & 0xFFFFFFFF); |
| 1190 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord3) |
| 1191 | .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32); |
| 1192 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), SRsrc) |
| 1193 | .addReg(DWord0) |
| 1194 | .addImm(AMDGPU::sub0) |
| 1195 | .addReg(DWord1) |
| 1196 | .addImm(AMDGPU::sub1) |
| 1197 | .addReg(DWord2) |
| 1198 | .addImm(AMDGPU::sub2) |
| 1199 | .addReg(DWord3) |
| 1200 | .addImm(AMDGPU::sub3); |
| 1201 | MI->setDesc(get(NewOpcode)); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1202 | if (MI->getOperand(2).isReg()) { |
| 1203 | MI->getOperand(2).setReg(MI->getOperand(1).getReg()); |
| 1204 | } else { |
| 1205 | MI->getOperand(2).ChangeToRegister(MI->getOperand(1).getReg(), false); |
| 1206 | } |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1207 | MI->getOperand(1).setReg(SRsrc); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1208 | MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(ImmOffset)); |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1212 | void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const { |
| 1213 | SmallVector<MachineInstr *, 128> Worklist; |
| 1214 | Worklist.push_back(&TopInst); |
| 1215 | |
| 1216 | while (!Worklist.empty()) { |
| 1217 | MachineInstr *Inst = Worklist.pop_back_val(); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1218 | MachineBasicBlock *MBB = Inst->getParent(); |
| 1219 | MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); |
| 1220 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1221 | unsigned Opcode = Inst->getOpcode(); |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1222 | unsigned NewOpcode = getVALUOp(*Inst); |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1223 | |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1224 | // Handle some special cases |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1225 | switch (Opcode) { |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1226 | default: |
| 1227 | if (isSMRD(Inst->getOpcode())) { |
| 1228 | moveSMRDToVALU(Inst, MRI); |
| 1229 | } |
| 1230 | break; |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 1231 | case AMDGPU::S_MOV_B64: { |
| 1232 | DebugLoc DL = Inst->getDebugLoc(); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1233 | |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 1234 | // If the source operand is a register we can replace this with a |
| 1235 | // copy. |
| 1236 | if (Inst->getOperand(1).isReg()) { |
| 1237 | MachineInstr *Copy = BuildMI(*MBB, Inst, DL, get(TargetOpcode::COPY)) |
| 1238 | .addOperand(Inst->getOperand(0)) |
| 1239 | .addOperand(Inst->getOperand(1)); |
| 1240 | Worklist.push_back(Copy); |
| 1241 | } else { |
| 1242 | // Otherwise, we need to split this into two movs, because there is |
| 1243 | // no 64-bit VALU move instruction. |
| 1244 | unsigned Reg = Inst->getOperand(0).getReg(); |
| 1245 | unsigned Dst = split64BitImm(Worklist, |
| 1246 | Inst, |
| 1247 | MRI, |
| 1248 | MRI.getRegClass(Reg), |
| 1249 | Inst->getOperand(1)); |
| 1250 | MRI.replaceRegWith(Reg, Dst); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1251 | } |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 1252 | Inst->eraseFromParent(); |
| 1253 | continue; |
| 1254 | } |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1255 | case AMDGPU::S_AND_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1256 | splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1257 | Inst->eraseFromParent(); |
| 1258 | continue; |
| 1259 | |
| 1260 | case AMDGPU::S_OR_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1261 | splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1262 | Inst->eraseFromParent(); |
| 1263 | continue; |
| 1264 | |
| 1265 | case AMDGPU::S_XOR_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1266 | splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1267 | Inst->eraseFromParent(); |
| 1268 | continue; |
| 1269 | |
| 1270 | case AMDGPU::S_NOT_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1271 | splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1272 | Inst->eraseFromParent(); |
| 1273 | continue; |
| 1274 | |
Matt Arsenault | 8333e43 | 2014-06-10 19:18:24 +0000 | [diff] [blame] | 1275 | case AMDGPU::S_BCNT1_I32_B64: |
| 1276 | splitScalar64BitBCNT(Worklist, Inst); |
| 1277 | Inst->eraseFromParent(); |
| 1278 | continue; |
| 1279 | |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1280 | case AMDGPU::S_BFE_U64: |
| 1281 | case AMDGPU::S_BFE_I64: |
| 1282 | case AMDGPU::S_BFM_B64: |
| 1283 | llvm_unreachable("Moving this op to VALU not implemented"); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 1286 | if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { |
| 1287 | // We cannot move this instruction to the VALU, so we should try to |
| 1288 | // legalize its operands instead. |
| 1289 | legalizeOperands(Inst); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1290 | continue; |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 1291 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1292 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1293 | // Use the new VALU Opcode. |
| 1294 | const MCInstrDesc &NewDesc = get(NewOpcode); |
| 1295 | Inst->setDesc(NewDesc); |
| 1296 | |
Matt Arsenault | f0b1e3a | 2013-11-18 20:09:21 +0000 | [diff] [blame] | 1297 | // Remove any references to SCC. Vector instructions can't read from it, and |
| 1298 | // We're just about to add the implicit use / defs of VCC, and we don't want |
| 1299 | // both. |
| 1300 | for (unsigned i = Inst->getNumOperands() - 1; i > 0; --i) { |
| 1301 | MachineOperand &Op = Inst->getOperand(i); |
| 1302 | if (Op.isReg() && Op.getReg() == AMDGPU::SCC) |
| 1303 | Inst->RemoveOperand(i); |
| 1304 | } |
| 1305 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1306 | if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { |
| 1307 | // We are converting these to a BFE, so we need to add the missing |
| 1308 | // operands for the size and offset. |
| 1309 | unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1310 | Inst->addOperand(Inst->getOperand(1)); |
| 1311 | Inst->getOperand(1).ChangeToImmediate(0); |
| 1312 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1313 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1314 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1315 | Inst->addOperand(MachineOperand::CreateImm(Size)); |
| 1316 | |
| 1317 | // XXX - Other pointless operands. There are 4, but it seems you only need |
| 1318 | // 3 to not hit an assertion later in MCInstLower. |
| 1319 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1320 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | b5b5110 | 2014-06-10 19:18:21 +0000 | [diff] [blame] | 1321 | } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { |
| 1322 | // The VALU version adds the second operand to the result, so insert an |
| 1323 | // extra 0 operand. |
| 1324 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1327 | addDescImplicitUseDef(NewDesc, Inst); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1328 | |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1329 | if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { |
| 1330 | const MachineOperand &OffsetWidthOp = Inst->getOperand(2); |
| 1331 | // If we need to move this to VGPRs, we need to unpack the second operand |
| 1332 | // back into the 2 separate ones for bit offset and width. |
| 1333 | assert(OffsetWidthOp.isImm() && |
| 1334 | "Scalar BFE is only implemented for constant width and offset"); |
| 1335 | uint32_t Imm = OffsetWidthOp.getImm(); |
| 1336 | |
| 1337 | uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. |
| 1338 | uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. |
| 1339 | |
| 1340 | Inst->RemoveOperand(2); // Remove old immediate. |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1341 | Inst->addOperand(Inst->getOperand(1)); |
| 1342 | Inst->getOperand(1).ChangeToImmediate(0); |
Matt Arsenault | 4b0402e | 2014-05-13 23:45:50 +0000 | [diff] [blame] | 1343 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1344 | Inst->addOperand(MachineOperand::CreateImm(Offset)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1345 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1346 | Inst->addOperand(MachineOperand::CreateImm(BitWidth)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1347 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1348 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1351 | // Update the destination register class. |
Tom Stellard | e1a2445 | 2014-04-17 21:00:01 +0000 | [diff] [blame] | 1352 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1353 | const TargetRegisterClass *NewDstRC = getOpRegClass(*Inst, 0); |
| 1354 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1355 | switch (Opcode) { |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1356 | // For target instructions, getOpRegClass just returns the virtual |
| 1357 | // register class associated with the operand, so we need to find an |
| 1358 | // equivalent VGPR register class in order to move the instruction to the |
| 1359 | // VALU. |
| 1360 | case AMDGPU::COPY: |
| 1361 | case AMDGPU::PHI: |
| 1362 | case AMDGPU::REG_SEQUENCE: |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 1363 | case AMDGPU::INSERT_SUBREG: |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1364 | if (RI.hasVGPRs(NewDstRC)) |
| 1365 | continue; |
| 1366 | NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); |
| 1367 | if (!NewDstRC) |
| 1368 | continue; |
| 1369 | break; |
| 1370 | default: |
| 1371 | break; |
| 1372 | } |
| 1373 | |
| 1374 | unsigned DstReg = Inst->getOperand(0).getReg(); |
| 1375 | unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC); |
| 1376 | MRI.replaceRegWith(DstReg, NewDstReg); |
| 1377 | |
Tom Stellard | e1a2445 | 2014-04-17 21:00:01 +0000 | [diff] [blame] | 1378 | // Legalize the operands |
| 1379 | legalizeOperands(Inst); |
| 1380 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1381 | for (MachineRegisterInfo::use_iterator I = MRI.use_begin(NewDstReg), |
| 1382 | E = MRI.use_end(); I != E; ++I) { |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1383 | MachineInstr &UseMI = *I->getParent(); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1384 | if (!canReadVGPR(UseMI, I.getOperandNo())) { |
| 1385 | Worklist.push_back(&UseMI); |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1391 | //===----------------------------------------------------------------------===// |
| 1392 | // Indirect addressing callbacks |
| 1393 | //===----------------------------------------------------------------------===// |
| 1394 | |
| 1395 | unsigned SIInstrInfo::calculateIndirectAddress(unsigned RegIndex, |
| 1396 | unsigned Channel) const { |
| 1397 | assert(Channel == 0); |
| 1398 | return RegIndex; |
| 1399 | } |
| 1400 | |
Tom Stellard | 26a3b67 | 2013-10-22 18:19:10 +0000 | [diff] [blame] | 1401 | const TargetRegisterClass *SIInstrInfo::getIndirectAddrRegClass() const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1402 | return &AMDGPU::VReg_32RegClass; |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1405 | void SIInstrInfo::splitScalar64BitUnaryOp( |
| 1406 | SmallVectorImpl<MachineInstr *> &Worklist, |
| 1407 | MachineInstr *Inst, |
| 1408 | unsigned Opcode) const { |
| 1409 | MachineBasicBlock &MBB = *Inst->getParent(); |
| 1410 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1411 | |
| 1412 | MachineOperand &Dest = Inst->getOperand(0); |
| 1413 | MachineOperand &Src0 = Inst->getOperand(1); |
| 1414 | DebugLoc DL = Inst->getDebugLoc(); |
| 1415 | |
| 1416 | MachineBasicBlock::iterator MII = Inst; |
| 1417 | |
| 1418 | const MCInstrDesc &InstDesc = get(Opcode); |
| 1419 | const TargetRegisterClass *Src0RC = Src0.isReg() ? |
| 1420 | MRI.getRegClass(Src0.getReg()) : |
| 1421 | &AMDGPU::SGPR_32RegClass; |
| 1422 | |
| 1423 | const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); |
| 1424 | |
| 1425 | MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1426 | AMDGPU::sub0, Src0SubRC); |
| 1427 | |
| 1428 | const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); |
| 1429 | const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0); |
| 1430 | |
| 1431 | unsigned DestSub0 = MRI.createVirtualRegister(DestRC); |
| 1432 | MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0) |
| 1433 | .addOperand(SrcReg0Sub0); |
| 1434 | |
| 1435 | MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1436 | AMDGPU::sub1, Src0SubRC); |
| 1437 | |
| 1438 | unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC); |
| 1439 | MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1) |
| 1440 | .addOperand(SrcReg0Sub1); |
| 1441 | |
| 1442 | unsigned FullDestReg = MRI.createVirtualRegister(DestRC); |
| 1443 | BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) |
| 1444 | .addReg(DestSub0) |
| 1445 | .addImm(AMDGPU::sub0) |
| 1446 | .addReg(DestSub1) |
| 1447 | .addImm(AMDGPU::sub1); |
| 1448 | |
| 1449 | MRI.replaceRegWith(Dest.getReg(), FullDestReg); |
| 1450 | |
| 1451 | // Try to legalize the operands in case we need to swap the order to keep it |
| 1452 | // valid. |
| 1453 | Worklist.push_back(LoHalf); |
| 1454 | Worklist.push_back(HiHalf); |
| 1455 | } |
| 1456 | |
| 1457 | void SIInstrInfo::splitScalar64BitBinaryOp( |
| 1458 | SmallVectorImpl<MachineInstr *> &Worklist, |
| 1459 | MachineInstr *Inst, |
| 1460 | unsigned Opcode) const { |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1461 | MachineBasicBlock &MBB = *Inst->getParent(); |
| 1462 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1463 | |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1464 | MachineOperand &Dest = Inst->getOperand(0); |
| 1465 | MachineOperand &Src0 = Inst->getOperand(1); |
| 1466 | MachineOperand &Src1 = Inst->getOperand(2); |
| 1467 | DebugLoc DL = Inst->getDebugLoc(); |
| 1468 | |
| 1469 | MachineBasicBlock::iterator MII = Inst; |
| 1470 | |
| 1471 | const MCInstrDesc &InstDesc = get(Opcode); |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1472 | const TargetRegisterClass *Src0RC = Src0.isReg() ? |
| 1473 | MRI.getRegClass(Src0.getReg()) : |
| 1474 | &AMDGPU::SGPR_32RegClass; |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1475 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1476 | const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); |
| 1477 | const TargetRegisterClass *Src1RC = Src1.isReg() ? |
| 1478 | MRI.getRegClass(Src1.getReg()) : |
| 1479 | &AMDGPU::SGPR_32RegClass; |
| 1480 | |
| 1481 | const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); |
| 1482 | |
| 1483 | MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1484 | AMDGPU::sub0, Src0SubRC); |
| 1485 | MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, |
| 1486 | AMDGPU::sub0, Src1SubRC); |
| 1487 | |
| 1488 | const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); |
| 1489 | const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0); |
| 1490 | |
| 1491 | unsigned DestSub0 = MRI.createVirtualRegister(DestRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1492 | MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0) |
Matt Arsenault | 248b7b6 | 2014-03-24 20:08:09 +0000 | [diff] [blame] | 1493 | .addOperand(SrcReg0Sub0) |
| 1494 | .addOperand(SrcReg1Sub0); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1495 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1496 | MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1497 | AMDGPU::sub1, Src0SubRC); |
| 1498 | MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, |
| 1499 | AMDGPU::sub1, Src1SubRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1500 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1501 | unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1502 | MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1) |
Matt Arsenault | 248b7b6 | 2014-03-24 20:08:09 +0000 | [diff] [blame] | 1503 | .addOperand(SrcReg0Sub1) |
| 1504 | .addOperand(SrcReg1Sub1); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1505 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1506 | unsigned FullDestReg = MRI.createVirtualRegister(DestRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1507 | BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) |
| 1508 | .addReg(DestSub0) |
| 1509 | .addImm(AMDGPU::sub0) |
| 1510 | .addReg(DestSub1) |
| 1511 | .addImm(AMDGPU::sub1); |
| 1512 | |
| 1513 | MRI.replaceRegWith(Dest.getReg(), FullDestReg); |
| 1514 | |
| 1515 | // Try to legalize the operands in case we need to swap the order to keep it |
| 1516 | // valid. |
| 1517 | Worklist.push_back(LoHalf); |
| 1518 | Worklist.push_back(HiHalf); |
| 1519 | } |
| 1520 | |
Matt Arsenault | 8333e43 | 2014-06-10 19:18:24 +0000 | [diff] [blame] | 1521 | void SIInstrInfo::splitScalar64BitBCNT(SmallVectorImpl<MachineInstr *> &Worklist, |
| 1522 | MachineInstr *Inst) const { |
| 1523 | MachineBasicBlock &MBB = *Inst->getParent(); |
| 1524 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1525 | |
| 1526 | MachineBasicBlock::iterator MII = Inst; |
| 1527 | DebugLoc DL = Inst->getDebugLoc(); |
| 1528 | |
| 1529 | MachineOperand &Dest = Inst->getOperand(0); |
| 1530 | MachineOperand &Src = Inst->getOperand(1); |
| 1531 | |
| 1532 | const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e32); |
| 1533 | const TargetRegisterClass *SrcRC = Src.isReg() ? |
| 1534 | MRI.getRegClass(Src.getReg()) : |
| 1535 | &AMDGPU::SGPR_32RegClass; |
| 1536 | |
| 1537 | unsigned MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1538 | unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1539 | |
| 1540 | const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); |
| 1541 | |
| 1542 | MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, |
| 1543 | AMDGPU::sub0, SrcSubRC); |
| 1544 | MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, |
| 1545 | AMDGPU::sub1, SrcSubRC); |
| 1546 | |
| 1547 | MachineInstr *First = BuildMI(MBB, MII, DL, InstDesc, MidReg) |
| 1548 | .addOperand(SrcRegSub0) |
| 1549 | .addImm(0); |
| 1550 | |
| 1551 | MachineInstr *Second = BuildMI(MBB, MII, DL, InstDesc, ResultReg) |
| 1552 | .addOperand(SrcRegSub1) |
| 1553 | .addReg(MidReg); |
| 1554 | |
| 1555 | MRI.replaceRegWith(Dest.getReg(), ResultReg); |
| 1556 | |
| 1557 | Worklist.push_back(First); |
| 1558 | Worklist.push_back(Second); |
| 1559 | } |
| 1560 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1561 | void SIInstrInfo::addDescImplicitUseDef(const MCInstrDesc &NewDesc, |
| 1562 | MachineInstr *Inst) const { |
| 1563 | // Add the implict and explicit register definitions. |
| 1564 | if (NewDesc.ImplicitUses) { |
| 1565 | for (unsigned i = 0; NewDesc.ImplicitUses[i]; ++i) { |
| 1566 | unsigned Reg = NewDesc.ImplicitUses[i]; |
| 1567 | Inst->addOperand(MachineOperand::CreateReg(Reg, false, true)); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | if (NewDesc.ImplicitDefs) { |
| 1572 | for (unsigned i = 0; NewDesc.ImplicitDefs[i]; ++i) { |
| 1573 | unsigned Reg = NewDesc.ImplicitDefs[i]; |
| 1574 | Inst->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1579 | MachineInstrBuilder SIInstrInfo::buildIndirectWrite( |
| 1580 | MachineBasicBlock *MBB, |
| 1581 | MachineBasicBlock::iterator I, |
| 1582 | unsigned ValueReg, |
| 1583 | unsigned Address, unsigned OffsetReg) const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1584 | const DebugLoc &DL = MBB->findDebugLoc(I); |
| 1585 | unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister( |
| 1586 | getIndirectIndexBegin(*MBB->getParent())); |
| 1587 | |
| 1588 | return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_DST_V1)) |
| 1589 | .addReg(IndirectBaseReg, RegState::Define) |
| 1590 | .addOperand(I->getOperand(0)) |
| 1591 | .addReg(IndirectBaseReg) |
| 1592 | .addReg(OffsetReg) |
| 1593 | .addImm(0) |
| 1594 | .addReg(ValueReg); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | MachineInstrBuilder SIInstrInfo::buildIndirectRead( |
| 1598 | MachineBasicBlock *MBB, |
| 1599 | MachineBasicBlock::iterator I, |
| 1600 | unsigned ValueReg, |
| 1601 | unsigned Address, unsigned OffsetReg) const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1602 | const DebugLoc &DL = MBB->findDebugLoc(I); |
| 1603 | unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister( |
| 1604 | getIndirectIndexBegin(*MBB->getParent())); |
| 1605 | |
| 1606 | return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_SRC)) |
| 1607 | .addOperand(I->getOperand(0)) |
| 1608 | .addOperand(I->getOperand(1)) |
| 1609 | .addReg(IndirectBaseReg) |
| 1610 | .addReg(OffsetReg) |
| 1611 | .addImm(0); |
| 1612 | |
| 1613 | } |
| 1614 | |
| 1615 | void SIInstrInfo::reserveIndirectRegisters(BitVector &Reserved, |
| 1616 | const MachineFunction &MF) const { |
| 1617 | int End = getIndirectIndexEnd(MF); |
| 1618 | int Begin = getIndirectIndexBegin(MF); |
| 1619 | |
| 1620 | if (End == -1) |
| 1621 | return; |
| 1622 | |
| 1623 | |
| 1624 | for (int Index = Begin; Index <= End; ++Index) |
| 1625 | Reserved.set(AMDGPU::VReg_32RegClass.getRegister(Index)); |
| 1626 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1627 | for (int Index = std::max(0, Begin - 1); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1628 | Reserved.set(AMDGPU::VReg_64RegClass.getRegister(Index)); |
| 1629 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1630 | for (int Index = std::max(0, Begin - 2); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1631 | Reserved.set(AMDGPU::VReg_96RegClass.getRegister(Index)); |
| 1632 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1633 | for (int Index = std::max(0, Begin - 3); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1634 | Reserved.set(AMDGPU::VReg_128RegClass.getRegister(Index)); |
| 1635 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1636 | for (int Index = std::max(0, Begin - 7); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1637 | Reserved.set(AMDGPU::VReg_256RegClass.getRegister(Index)); |
| 1638 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1639 | for (int Index = std::max(0, Begin - 15); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1640 | Reserved.set(AMDGPU::VReg_512RegClass.getRegister(Index)); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1641 | } |