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 | } |
| 364 | } |
| 365 | return true; |
| 366 | } |
| 367 | |
Christian Konig | 76edd4f | 2013-02-26 17:52:29 +0000 | [diff] [blame] | 368 | MachineInstr *SIInstrInfo::commuteInstruction(MachineInstr *MI, |
| 369 | bool NewMI) const { |
| 370 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 371 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 372 | if (MI->getNumOperands() < 3 || !MI->getOperand(1).isReg()) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 373 | return nullptr; |
Christian Konig | 76edd4f | 2013-02-26 17:52:29 +0000 | [diff] [blame] | 374 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 375 | // Cannot commute VOP2 if src0 is SGPR. |
| 376 | if (isVOP2(MI->getOpcode()) && MI->getOperand(1).isReg() && |
| 377 | RI.isSGPRClass(MRI.getRegClass(MI->getOperand(1).getReg()))) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 378 | return nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 379 | |
| 380 | if (!MI->getOperand(2).isReg()) { |
| 381 | // XXX: Commute instructions with FPImm operands |
| 382 | if (NewMI || MI->getOperand(2).isFPImm() || |
| 383 | (!isVOP2(MI->getOpcode()) && !isVOP3(MI->getOpcode()))) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 384 | return nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // XXX: Commute VOP3 instructions with abs and neg set. |
| 388 | if (isVOP3(MI->getOpcode()) && |
| 389 | (MI->getOperand(AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 390 | AMDGPU::OpName::abs)).getImm() || |
| 391 | MI->getOperand(AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 392 | AMDGPU::OpName::neg)).getImm())) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 393 | return nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 394 | |
| 395 | unsigned Reg = MI->getOperand(1).getReg(); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 396 | unsigned SubReg = MI->getOperand(1).getSubReg(); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 397 | MI->getOperand(1).ChangeToImmediate(MI->getOperand(2).getImm()); |
| 398 | MI->getOperand(2).ChangeToRegister(Reg, false); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 399 | MI->getOperand(2).setSubReg(SubReg); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 400 | } else { |
| 401 | MI = TargetInstrInfo::commuteInstruction(MI, NewMI); |
| 402 | } |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 403 | |
| 404 | if (MI) |
| 405 | MI->setDesc(get(commuteOpcode(MI->getOpcode()))); |
| 406 | |
| 407 | return MI; |
Christian Konig | 76edd4f | 2013-02-26 17:52:29 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Tom Stellard | 26a3b67 | 2013-10-22 18:19:10 +0000 | [diff] [blame] | 410 | MachineInstr *SIInstrInfo::buildMovInstr(MachineBasicBlock *MBB, |
| 411 | MachineBasicBlock::iterator I, |
| 412 | unsigned DstReg, |
| 413 | unsigned SrcReg) const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 414 | return BuildMI(*MBB, I, MBB->findDebugLoc(I), get(AMDGPU::V_MOV_B32_e32), |
| 415 | DstReg) .addReg(SrcReg); |
Tom Stellard | 26a3b67 | 2013-10-22 18:19:10 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 418 | bool SIInstrInfo::isMov(unsigned Opcode) const { |
| 419 | switch(Opcode) { |
| 420 | default: return false; |
| 421 | case AMDGPU::S_MOV_B32: |
| 422 | case AMDGPU::S_MOV_B64: |
| 423 | case AMDGPU::V_MOV_B32_e32: |
| 424 | case AMDGPU::V_MOV_B32_e64: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | bool |
| 430 | SIInstrInfo::isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const { |
| 431 | return RC != &AMDGPU::EXECRegRegClass; |
| 432 | } |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 433 | |
Tom Stellard | 30f5941 | 2014-03-31 14:01:56 +0000 | [diff] [blame] | 434 | bool |
| 435 | SIInstrInfo::isTriviallyReMaterializable(const MachineInstr *MI, |
| 436 | AliasAnalysis *AA) const { |
| 437 | switch(MI->getOpcode()) { |
| 438 | default: return AMDGPUInstrInfo::isTriviallyReMaterializable(MI, AA); |
| 439 | case AMDGPU::S_MOV_B32: |
| 440 | case AMDGPU::S_MOV_B64: |
| 441 | case AMDGPU::V_MOV_B32_e32: |
| 442 | return MI->getOperand(1).isImm(); |
| 443 | } |
| 444 | } |
| 445 | |
Tom Stellard | 5d7aaae | 2014-02-10 16:58:30 +0000 | [diff] [blame] | 446 | namespace llvm { |
| 447 | namespace AMDGPU { |
| 448 | // Helper function generated by tablegen. We are wrapping this with |
| 449 | // an SIInstrInfo function that reutrns bool rather than int. |
| 450 | int isDS(uint16_t Opcode); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | bool SIInstrInfo::isDS(uint16_t Opcode) const { |
| 455 | return ::AMDGPU::isDS(Opcode) != -1; |
| 456 | } |
| 457 | |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 458 | int SIInstrInfo::isMIMG(uint16_t Opcode) const { |
| 459 | return get(Opcode).TSFlags & SIInstrFlags::MIMG; |
| 460 | } |
| 461 | |
Michel Danzer | 20680b1 | 2013-08-16 16:19:24 +0000 | [diff] [blame] | 462 | int SIInstrInfo::isSMRD(uint16_t Opcode) const { |
| 463 | return get(Opcode).TSFlags & SIInstrFlags::SMRD; |
| 464 | } |
| 465 | |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 466 | bool SIInstrInfo::isVOP1(uint16_t Opcode) const { |
| 467 | return get(Opcode).TSFlags & SIInstrFlags::VOP1; |
| 468 | } |
| 469 | |
| 470 | bool SIInstrInfo::isVOP2(uint16_t Opcode) const { |
| 471 | return get(Opcode).TSFlags & SIInstrFlags::VOP2; |
| 472 | } |
| 473 | |
| 474 | bool SIInstrInfo::isVOP3(uint16_t Opcode) const { |
| 475 | return get(Opcode).TSFlags & SIInstrFlags::VOP3; |
| 476 | } |
| 477 | |
| 478 | bool SIInstrInfo::isVOPC(uint16_t Opcode) const { |
| 479 | return get(Opcode).TSFlags & SIInstrFlags::VOPC; |
| 480 | } |
| 481 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 482 | bool SIInstrInfo::isSALUInstr(const MachineInstr &MI) const { |
| 483 | return get(MI.getOpcode()).TSFlags & SIInstrFlags::SALU; |
| 484 | } |
| 485 | |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 486 | bool SIInstrInfo::isInlineConstant(const APInt &Imm) const { |
| 487 | int32_t Val = Imm.getSExtValue(); |
| 488 | if (Val >= -16 && Val <= 64) |
| 489 | return true; |
Tom Stellard | d008446 | 2014-03-17 17:03:52 +0000 | [diff] [blame] | 490 | |
| 491 | // The actual type of the operand does not seem to matter as long |
| 492 | // as the bits match one of the inline immediate values. For example: |
| 493 | // |
| 494 | // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal, |
| 495 | // so it is a legal inline immediate. |
| 496 | // |
| 497 | // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in |
| 498 | // floating-point, so it is a legal inline immediate. |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 499 | |
| 500 | return (APInt::floatToBits(0.0f) == Imm) || |
| 501 | (APInt::floatToBits(1.0f) == Imm) || |
| 502 | (APInt::floatToBits(-1.0f) == Imm) || |
| 503 | (APInt::floatToBits(0.5f) == Imm) || |
| 504 | (APInt::floatToBits(-0.5f) == Imm) || |
| 505 | (APInt::floatToBits(2.0f) == Imm) || |
| 506 | (APInt::floatToBits(-2.0f) == Imm) || |
| 507 | (APInt::floatToBits(4.0f) == Imm) || |
| 508 | (APInt::floatToBits(-4.0f) == Imm); |
| 509 | } |
| 510 | |
| 511 | bool SIInstrInfo::isInlineConstant(const MachineOperand &MO) const { |
| 512 | if (MO.isImm()) |
| 513 | return isInlineConstant(APInt(32, MO.getImm(), true)); |
| 514 | |
| 515 | if (MO.isFPImm()) { |
| 516 | APFloat FpImm = MO.getFPImm()->getValueAPF(); |
| 517 | return isInlineConstant(FpImm.bitcastToAPInt()); |
| 518 | } |
| 519 | |
| 520 | return false; |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | bool SIInstrInfo::isLiteralConstant(const MachineOperand &MO) const { |
| 524 | return (MO.isImm() || MO.isFPImm()) && !isInlineConstant(MO); |
| 525 | } |
| 526 | |
| 527 | bool SIInstrInfo::verifyInstruction(const MachineInstr *MI, |
| 528 | StringRef &ErrInfo) const { |
| 529 | uint16_t Opcode = MI->getOpcode(); |
| 530 | int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); |
| 531 | int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1); |
| 532 | int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2); |
| 533 | |
Tom Stellard | ca700e4 | 2014-03-17 17:03:49 +0000 | [diff] [blame] | 534 | // Make sure the number of operands is correct. |
| 535 | const MCInstrDesc &Desc = get(Opcode); |
| 536 | if (!Desc.isVariadic() && |
| 537 | Desc.getNumOperands() != MI->getNumExplicitOperands()) { |
| 538 | ErrInfo = "Instruction has wrong number of operands."; |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | // Make sure the register classes are correct |
| 543 | for (unsigned i = 0, e = Desc.getNumOperands(); i != e; ++i) { |
| 544 | switch (Desc.OpInfo[i].OperandType) { |
| 545 | case MCOI::OPERAND_REGISTER: |
| 546 | break; |
| 547 | case MCOI::OPERAND_IMMEDIATE: |
| 548 | if (!MI->getOperand(i).isImm() && !MI->getOperand(i).isFPImm()) { |
| 549 | ErrInfo = "Expected immediate, but got non-immediate"; |
| 550 | return false; |
| 551 | } |
| 552 | // Fall-through |
| 553 | default: |
| 554 | continue; |
| 555 | } |
| 556 | |
| 557 | if (!MI->getOperand(i).isReg()) |
| 558 | continue; |
| 559 | |
| 560 | int RegClass = Desc.OpInfo[i].RegClass; |
| 561 | if (RegClass != -1) { |
| 562 | unsigned Reg = MI->getOperand(i).getReg(); |
| 563 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 564 | continue; |
| 565 | |
| 566 | const TargetRegisterClass *RC = RI.getRegClass(RegClass); |
| 567 | if (!RC->contains(Reg)) { |
| 568 | ErrInfo = "Operand has incorrect register class."; |
| 569 | return false; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 575 | // Verify VOP* |
| 576 | if (isVOP1(Opcode) || isVOP2(Opcode) || isVOP3(Opcode) || isVOPC(Opcode)) { |
| 577 | unsigned ConstantBusCount = 0; |
| 578 | unsigned SGPRUsed = AMDGPU::NoRegister; |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 579 | for (int i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 580 | const MachineOperand &MO = MI->getOperand(i); |
| 581 | if (MO.isReg() && MO.isUse() && |
| 582 | !TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 583 | |
| 584 | // EXEC register uses the constant bus. |
| 585 | if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC) |
| 586 | ++ConstantBusCount; |
| 587 | |
| 588 | // SGPRs use the constant bus |
| 589 | if (MO.getReg() == AMDGPU::M0 || MO.getReg() == AMDGPU::VCC || |
| 590 | (!MO.isImplicit() && |
| 591 | (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) || |
| 592 | AMDGPU::SGPR_64RegClass.contains(MO.getReg())))) { |
| 593 | if (SGPRUsed != MO.getReg()) { |
| 594 | ++ConstantBusCount; |
| 595 | SGPRUsed = MO.getReg(); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | // Literal constants use the constant bus. |
| 600 | if (isLiteralConstant(MO)) |
| 601 | ++ConstantBusCount; |
| 602 | } |
| 603 | if (ConstantBusCount > 1) { |
| 604 | ErrInfo = "VOP* instruction uses the constant bus more than once"; |
| 605 | return false; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Verify SRC1 for VOP2 and VOPC |
| 610 | if (Src1Idx != -1 && (isVOP2(Opcode) || isVOPC(Opcode))) { |
| 611 | const MachineOperand &Src1 = MI->getOperand(Src1Idx); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 612 | if (Src1.isImm() || Src1.isFPImm()) { |
Tom Stellard | 93fabce | 2013-10-10 17:11:55 +0000 | [diff] [blame] | 613 | ErrInfo = "VOP[2C] src1 cannot be an immediate."; |
| 614 | return false; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | // Verify VOP3 |
| 619 | if (isVOP3(Opcode)) { |
| 620 | if (Src0Idx != -1 && isLiteralConstant(MI->getOperand(Src0Idx))) { |
| 621 | ErrInfo = "VOP3 src0 cannot be a literal constant."; |
| 622 | return false; |
| 623 | } |
| 624 | if (Src1Idx != -1 && isLiteralConstant(MI->getOperand(Src1Idx))) { |
| 625 | ErrInfo = "VOP3 src1 cannot be a literal constant."; |
| 626 | return false; |
| 627 | } |
| 628 | if (Src2Idx != -1 && isLiteralConstant(MI->getOperand(Src2Idx))) { |
| 629 | ErrInfo = "VOP3 src2 cannot be a literal constant."; |
| 630 | return false; |
| 631 | } |
| 632 | } |
| 633 | return true; |
| 634 | } |
| 635 | |
Matt Arsenault | f14032a | 2013-11-15 22:02:28 +0000 | [diff] [blame] | 636 | unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) { |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 637 | switch (MI.getOpcode()) { |
| 638 | default: return AMDGPU::INSTRUCTION_LIST_END; |
| 639 | case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; |
| 640 | case AMDGPU::COPY: return AMDGPU::COPY; |
| 641 | case AMDGPU::PHI: return AMDGPU::PHI; |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 642 | case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 643 | case AMDGPU::S_MOV_B32: |
| 644 | return MI.getOperand(1).isReg() ? |
Tom Stellard | 8c12fd9 | 2014-03-24 16:12:34 +0000 | [diff] [blame] | 645 | AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; |
Matt Arsenault | 43b8e4e | 2013-11-18 20:09:29 +0000 | [diff] [blame] | 646 | case AMDGPU::S_ADD_I32: return AMDGPU::V_ADD_I32_e32; |
| 647 | case AMDGPU::S_ADDC_U32: return AMDGPU::V_ADDC_U32_e32; |
| 648 | case AMDGPU::S_SUB_I32: return AMDGPU::V_SUB_I32_e32; |
| 649 | case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; |
Matt Arsenault | 8e2581b | 2014-03-21 18:01:18 +0000 | [diff] [blame] | 650 | case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e32; |
| 651 | case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e32; |
| 652 | case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e32; |
| 653 | case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e32; |
| 654 | case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e32; |
| 655 | case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e32; |
| 656 | case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e32; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 657 | case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; |
| 658 | case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64; |
| 659 | case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; |
| 660 | case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64; |
| 661 | case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; |
| 662 | case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64; |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 663 | case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32; |
| 664 | case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32; |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 665 | case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32; |
| 666 | case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32; |
Matt Arsenault | 2c33562 | 2014-04-09 07:16:16 +0000 | [diff] [blame] | 667 | case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 668 | case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; |
Matt Arsenault | 0cb92e1 | 2014-04-11 19:25:18 +0000 | [diff] [blame] | 669 | case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32; |
| 670 | case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32; |
| 671 | case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32; |
| 672 | case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32; |
| 673 | case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32; |
| 674 | 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] | 675 | case AMDGPU::S_LOAD_DWORD_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 676 | case AMDGPU::S_LOAD_DWORD_SGPR: return AMDGPU::BUFFER_LOAD_DWORD_ADDR64; |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 677 | case AMDGPU::S_LOAD_DWORDX2_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 678 | case AMDGPU::S_LOAD_DWORDX2_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64; |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 679 | case AMDGPU::S_LOAD_DWORDX4_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 680 | case AMDGPU::S_LOAD_DWORDX4_SGPR: return AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64; |
Matt Arsenault | b5b5110 | 2014-06-10 19:18:21 +0000 | [diff] [blame] | 681 | 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^] | 682 | case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; |
Matt Arsenault | 8579601 | 2014-06-17 17:36:24 +0000 | [diff] [blame] | 683 | case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const { |
| 688 | return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END; |
| 689 | } |
| 690 | |
| 691 | const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, |
| 692 | unsigned OpNo) const { |
| 693 | const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); |
| 694 | const MCInstrDesc &Desc = get(MI.getOpcode()); |
| 695 | if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || |
| 696 | Desc.OpInfo[OpNo].RegClass == -1) |
| 697 | return MRI.getRegClass(MI.getOperand(OpNo).getReg()); |
| 698 | |
| 699 | unsigned RCID = Desc.OpInfo[OpNo].RegClass; |
| 700 | return RI.getRegClass(RCID); |
| 701 | } |
| 702 | |
| 703 | bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const { |
| 704 | switch (MI.getOpcode()) { |
| 705 | case AMDGPU::COPY: |
| 706 | case AMDGPU::REG_SEQUENCE: |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 707 | case AMDGPU::PHI: |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 708 | case AMDGPU::INSERT_SUBREG: |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 709 | return RI.hasVGPRs(getOpRegClass(MI, 0)); |
| 710 | default: |
| 711 | return RI.hasVGPRs(getOpRegClass(MI, OpNo)); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | void SIInstrInfo::legalizeOpWithMove(MachineInstr *MI, unsigned OpIdx) const { |
| 716 | MachineBasicBlock::iterator I = MI; |
| 717 | MachineOperand &MO = MI->getOperand(OpIdx); |
| 718 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 719 | unsigned RCID = get(MI->getOpcode()).OpInfo[OpIdx].RegClass; |
| 720 | const TargetRegisterClass *RC = RI.getRegClass(RCID); |
| 721 | unsigned Opcode = AMDGPU::V_MOV_B32_e32; |
| 722 | if (MO.isReg()) { |
| 723 | Opcode = AMDGPU::COPY; |
| 724 | } else if (RI.isSGPRClass(RC)) { |
Matt Arsenault | 671a005 | 2013-11-14 10:08:50 +0000 | [diff] [blame] | 725 | Opcode = AMDGPU::S_MOV_B32; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Matt Arsenault | 3a4d86a | 2013-11-18 20:09:55 +0000 | [diff] [blame] | 728 | const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); |
| 729 | unsigned Reg = MRI.createVirtualRegister(VRC); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 730 | BuildMI(*MI->getParent(), I, MI->getParent()->findDebugLoc(I), get(Opcode), |
| 731 | Reg).addOperand(MO); |
| 732 | MO.ChangeToRegister(Reg, false); |
| 733 | } |
| 734 | |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 735 | unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, |
| 736 | MachineRegisterInfo &MRI, |
| 737 | MachineOperand &SuperReg, |
| 738 | const TargetRegisterClass *SuperRC, |
| 739 | unsigned SubIdx, |
| 740 | const TargetRegisterClass *SubRC) |
| 741 | const { |
| 742 | assert(SuperReg.isReg()); |
| 743 | |
| 744 | unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC); |
| 745 | unsigned SubReg = MRI.createVirtualRegister(SubRC); |
| 746 | |
| 747 | // 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] | 748 | // value so we don't need to worry about merging its subreg index with the |
| 749 | // SubIdx passed to this function. The register coalescer should be able to |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 750 | // eliminate this extra copy. |
| 751 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY), |
| 752 | NewSuperReg) |
| 753 | .addOperand(SuperReg); |
| 754 | |
| 755 | BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), get(TargetOpcode::COPY), |
| 756 | SubReg) |
| 757 | .addReg(NewSuperReg, 0, SubIdx); |
| 758 | return SubReg; |
| 759 | } |
| 760 | |
Matt Arsenault | 248b7b6 | 2014-03-24 20:08:09 +0000 | [diff] [blame] | 761 | MachineOperand SIInstrInfo::buildExtractSubRegOrImm( |
| 762 | MachineBasicBlock::iterator MII, |
| 763 | MachineRegisterInfo &MRI, |
| 764 | MachineOperand &Op, |
| 765 | const TargetRegisterClass *SuperRC, |
| 766 | unsigned SubIdx, |
| 767 | const TargetRegisterClass *SubRC) const { |
| 768 | if (Op.isImm()) { |
| 769 | // XXX - Is there a better way to do this? |
| 770 | if (SubIdx == AMDGPU::sub0) |
| 771 | return MachineOperand::CreateImm(Op.getImm() & 0xFFFFFFFF); |
| 772 | if (SubIdx == AMDGPU::sub1) |
| 773 | return MachineOperand::CreateImm(Op.getImm() >> 32); |
| 774 | |
| 775 | llvm_unreachable("Unhandled register index for immediate"); |
| 776 | } |
| 777 | |
| 778 | unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, |
| 779 | SubIdx, SubRC); |
| 780 | return MachineOperand::CreateReg(SubReg, false); |
| 781 | } |
| 782 | |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 783 | unsigned SIInstrInfo::split64BitImm(SmallVectorImpl<MachineInstr *> &Worklist, |
| 784 | MachineBasicBlock::iterator MI, |
| 785 | MachineRegisterInfo &MRI, |
| 786 | const TargetRegisterClass *RC, |
| 787 | const MachineOperand &Op) const { |
| 788 | MachineBasicBlock *MBB = MI->getParent(); |
| 789 | DebugLoc DL = MI->getDebugLoc(); |
| 790 | unsigned LoDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 791 | unsigned HiDst = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 792 | unsigned Dst = MRI.createVirtualRegister(RC); |
| 793 | |
| 794 | MachineInstr *Lo = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32), |
| 795 | LoDst) |
| 796 | .addImm(Op.getImm() & 0xFFFFFFFF); |
| 797 | MachineInstr *Hi = BuildMI(*MBB, MI, DL, get(AMDGPU::S_MOV_B32), |
| 798 | HiDst) |
| 799 | .addImm(Op.getImm() >> 32); |
| 800 | |
| 801 | BuildMI(*MBB, MI, DL, get(TargetOpcode::REG_SEQUENCE), Dst) |
| 802 | .addReg(LoDst) |
| 803 | .addImm(AMDGPU::sub0) |
| 804 | .addReg(HiDst) |
| 805 | .addImm(AMDGPU::sub1); |
| 806 | |
| 807 | Worklist.push_back(Lo); |
| 808 | Worklist.push_back(Hi); |
| 809 | |
| 810 | return Dst; |
| 811 | } |
| 812 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 813 | void SIInstrInfo::legalizeOperands(MachineInstr *MI) const { |
| 814 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 815 | int Src0Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 816 | AMDGPU::OpName::src0); |
| 817 | int Src1Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 818 | AMDGPU::OpName::src1); |
| 819 | int Src2Idx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 820 | AMDGPU::OpName::src2); |
| 821 | |
| 822 | // Legalize VOP2 |
| 823 | if (isVOP2(MI->getOpcode()) && Src1Idx != -1) { |
Matt Arsenault | 08f7e37 | 2013-11-18 20:09:50 +0000 | [diff] [blame] | 824 | MachineOperand &Src0 = MI->getOperand(Src0Idx); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 825 | MachineOperand &Src1 = MI->getOperand(Src1Idx); |
Matt Arsenault | f476045 | 2013-11-14 08:06:38 +0000 | [diff] [blame] | 826 | |
Matt Arsenault | 08f7e37 | 2013-11-18 20:09:50 +0000 | [diff] [blame] | 827 | // If the instruction implicitly reads VCC, we can't have any SGPR operands, |
| 828 | // so move any. |
| 829 | bool ReadsVCC = MI->readsRegister(AMDGPU::VCC, &RI); |
| 830 | if (ReadsVCC && Src0.isReg() && |
| 831 | RI.isSGPRClass(MRI.getRegClass(Src0.getReg()))) { |
| 832 | legalizeOpWithMove(MI, Src0Idx); |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | if (ReadsVCC && Src1.isReg() && |
| 837 | RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) { |
| 838 | legalizeOpWithMove(MI, Src1Idx); |
| 839 | return; |
| 840 | } |
| 841 | |
Matt Arsenault | f476045 | 2013-11-14 08:06:38 +0000 | [diff] [blame] | 842 | // Legalize VOP2 instructions where src1 is not a VGPR. An SGPR input must |
| 843 | // be the first operand, and there can only be one. |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 844 | if (Src1.isImm() || Src1.isFPImm() || |
| 845 | (Src1.isReg() && RI.isSGPRClass(MRI.getRegClass(Src1.getReg())))) { |
| 846 | if (MI->isCommutable()) { |
| 847 | if (commuteInstruction(MI)) |
| 848 | return; |
| 849 | } |
| 850 | legalizeOpWithMove(MI, Src1Idx); |
| 851 | } |
| 852 | } |
| 853 | |
Matt Arsenault | 08f7e37 | 2013-11-18 20:09:50 +0000 | [diff] [blame] | 854 | // XXX - Do any VOP3 instructions read VCC? |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 855 | // Legalize VOP3 |
| 856 | if (isVOP3(MI->getOpcode())) { |
| 857 | int VOP3Idx[3] = {Src0Idx, Src1Idx, Src2Idx}; |
| 858 | unsigned SGPRReg = AMDGPU::NoRegister; |
| 859 | for (unsigned i = 0; i < 3; ++i) { |
| 860 | int Idx = VOP3Idx[i]; |
| 861 | if (Idx == -1) |
| 862 | continue; |
| 863 | MachineOperand &MO = MI->getOperand(Idx); |
| 864 | |
| 865 | if (MO.isReg()) { |
| 866 | if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg()))) |
| 867 | continue; // VGPRs are legal |
| 868 | |
Matt Arsenault | f0b1e3a | 2013-11-18 20:09:21 +0000 | [diff] [blame] | 869 | assert(MO.getReg() != AMDGPU::SCC && "SCC operand to VOP3 instruction"); |
| 870 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 871 | if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) { |
| 872 | SGPRReg = MO.getReg(); |
| 873 | // We can use one SGPR in each VOP3 instruction. |
| 874 | continue; |
| 875 | } |
| 876 | } else if (!isLiteralConstant(MO)) { |
| 877 | // If it is not a register and not a literal constant, then it must be |
| 878 | // an inline constant which is always legal. |
| 879 | continue; |
| 880 | } |
| 881 | // If we make it this far, then the operand is not legal and we must |
| 882 | // legalize it. |
| 883 | legalizeOpWithMove(MI, Idx); |
| 884 | } |
| 885 | } |
| 886 | |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 887 | // Legalize REG_SEQUENCE and PHI |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 888 | // The register class of the operands much be the same type as the register |
| 889 | // class of the output. |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 890 | if (MI->getOpcode() == AMDGPU::REG_SEQUENCE || |
| 891 | MI->getOpcode() == AMDGPU::PHI) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 892 | const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 893 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) { |
| 894 | if (!MI->getOperand(i).isReg() || |
| 895 | !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg())) |
| 896 | continue; |
| 897 | const TargetRegisterClass *OpRC = |
| 898 | MRI.getRegClass(MI->getOperand(i).getReg()); |
| 899 | if (RI.hasVGPRs(OpRC)) { |
| 900 | VRC = OpRC; |
| 901 | } else { |
| 902 | SRC = OpRC; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | // If any of the operands are VGPR registers, then they all most be |
| 907 | // otherwise we will create illegal VGPR->SGPR copies when legalizing |
| 908 | // them. |
| 909 | if (VRC || !RI.isSGPRClass(getOpRegClass(*MI, 0))) { |
| 910 | if (!VRC) { |
| 911 | assert(SRC); |
| 912 | VRC = RI.getEquivalentVGPRClass(SRC); |
| 913 | } |
| 914 | RC = VRC; |
| 915 | } else { |
| 916 | RC = SRC; |
| 917 | } |
| 918 | |
| 919 | // Update all the operands so they have the same type. |
| 920 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) { |
| 921 | if (!MI->getOperand(i).isReg() || |
| 922 | !TargetRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg())) |
| 923 | continue; |
| 924 | unsigned DstReg = MRI.createVirtualRegister(RC); |
Tom Stellard | 4f3b04d | 2014-04-17 21:00:07 +0000 | [diff] [blame] | 925 | MachineBasicBlock *InsertBB; |
| 926 | MachineBasicBlock::iterator Insert; |
| 927 | if (MI->getOpcode() == AMDGPU::REG_SEQUENCE) { |
| 928 | InsertBB = MI->getParent(); |
| 929 | Insert = MI; |
| 930 | } else { |
| 931 | // MI is a PHI instruction. |
| 932 | InsertBB = MI->getOperand(i + 1).getMBB(); |
| 933 | Insert = InsertBB->getFirstTerminator(); |
| 934 | } |
| 935 | BuildMI(*InsertBB, Insert, MI->getDebugLoc(), |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 936 | get(AMDGPU::COPY), DstReg) |
| 937 | .addOperand(MI->getOperand(i)); |
| 938 | MI->getOperand(i).setReg(DstReg); |
| 939 | } |
| 940 | } |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 941 | |
Tom Stellard | a568738 | 2014-05-15 14:41:55 +0000 | [diff] [blame] | 942 | // Legalize INSERT_SUBREG |
| 943 | // src0 must have the same register class as dst |
| 944 | if (MI->getOpcode() == AMDGPU::INSERT_SUBREG) { |
| 945 | unsigned Dst = MI->getOperand(0).getReg(); |
| 946 | unsigned Src0 = MI->getOperand(1).getReg(); |
| 947 | const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); |
| 948 | const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); |
| 949 | if (DstRC != Src0RC) { |
| 950 | MachineBasicBlock &MBB = *MI->getParent(); |
| 951 | unsigned NewSrc0 = MRI.createVirtualRegister(DstRC); |
| 952 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::COPY), NewSrc0) |
| 953 | .addReg(Src0); |
| 954 | MI->getOperand(1).setReg(NewSrc0); |
| 955 | } |
| 956 | return; |
| 957 | } |
| 958 | |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 959 | // Legalize MUBUF* instructions |
| 960 | // FIXME: If we start using the non-addr64 instructions for compute, we |
| 961 | // may need to legalize them here. |
| 962 | |
| 963 | int SRsrcIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 964 | AMDGPU::OpName::srsrc); |
| 965 | int VAddrIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), |
| 966 | AMDGPU::OpName::vaddr); |
| 967 | if (SRsrcIdx != -1 && VAddrIdx != -1) { |
| 968 | const TargetRegisterClass *VAddrRC = |
| 969 | RI.getRegClass(get(MI->getOpcode()).OpInfo[VAddrIdx].RegClass); |
| 970 | |
| 971 | if(VAddrRC->getSize() == 8 && |
| 972 | MRI.getRegClass(MI->getOperand(SRsrcIdx).getReg()) != VAddrRC) { |
| 973 | // We have a MUBUF instruction that uses a 64-bit vaddr register and |
| 974 | // srsrc has the incorrect register class. In order to fix this, we |
| 975 | // need to extract the pointer from the resource descriptor (srsrc), |
| 976 | // add it to the value of vadd, then store the result in the vaddr |
| 977 | // operand. Then, we need to set the pointer field of the resource |
| 978 | // descriptor to zero. |
| 979 | |
| 980 | MachineBasicBlock &MBB = *MI->getParent(); |
| 981 | MachineOperand &SRsrcOp = MI->getOperand(SRsrcIdx); |
| 982 | MachineOperand &VAddrOp = MI->getOperand(VAddrIdx); |
| 983 | unsigned SRsrcPtrLo, SRsrcPtrHi, VAddrLo, VAddrHi; |
| 984 | unsigned NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
| 985 | unsigned NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
| 986 | unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); |
| 987 | unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 988 | unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 989 | unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 990 | unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); |
| 991 | |
| 992 | // SRsrcPtrLo = srsrc:sub0 |
| 993 | SRsrcPtrLo = buildExtractSubReg(MI, MRI, SRsrcOp, |
| 994 | &AMDGPU::VReg_128RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass); |
| 995 | |
| 996 | // SRsrcPtrHi = srsrc:sub1 |
| 997 | SRsrcPtrHi = buildExtractSubReg(MI, MRI, SRsrcOp, |
| 998 | &AMDGPU::VReg_128RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass); |
| 999 | |
| 1000 | // VAddrLo = vaddr:sub0 |
| 1001 | VAddrLo = buildExtractSubReg(MI, MRI, VAddrOp, |
| 1002 | &AMDGPU::VReg_64RegClass, AMDGPU::sub0, &AMDGPU::VReg_32RegClass); |
| 1003 | |
| 1004 | // VAddrHi = vaddr:sub1 |
| 1005 | VAddrHi = buildExtractSubReg(MI, MRI, VAddrOp, |
| 1006 | &AMDGPU::VReg_64RegClass, AMDGPU::sub1, &AMDGPU::VReg_32RegClass); |
| 1007 | |
| 1008 | // NewVaddrLo = SRsrcPtrLo + VAddrLo |
| 1009 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADD_I32_e32), |
| 1010 | NewVAddrLo) |
| 1011 | .addReg(SRsrcPtrLo) |
| 1012 | .addReg(VAddrLo) |
| 1013 | .addReg(AMDGPU::VCC, RegState::Define | RegState::Implicit); |
| 1014 | |
| 1015 | // NewVaddrHi = SRsrcPtrHi + VAddrHi |
| 1016 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::V_ADDC_U32_e32), |
| 1017 | NewVAddrHi) |
| 1018 | .addReg(SRsrcPtrHi) |
| 1019 | .addReg(VAddrHi) |
| 1020 | .addReg(AMDGPU::VCC, RegState::ImplicitDefine) |
| 1021 | .addReg(AMDGPU::VCC, RegState::Implicit); |
| 1022 | |
| 1023 | // NewVaddr = {NewVaddrHi, NewVaddrLo} |
| 1024 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), |
| 1025 | NewVAddr) |
| 1026 | .addReg(NewVAddrLo) |
| 1027 | .addImm(AMDGPU::sub0) |
| 1028 | .addReg(NewVAddrHi) |
| 1029 | .addImm(AMDGPU::sub1); |
| 1030 | |
| 1031 | // Zero64 = 0 |
| 1032 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B64), |
| 1033 | Zero64) |
| 1034 | .addImm(0); |
| 1035 | |
| 1036 | // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} |
| 1037 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1038 | SRsrcFormatLo) |
| 1039 | .addImm(AMDGPU::RSRC_DATA_FORMAT & 0xFFFFFFFF); |
| 1040 | |
| 1041 | // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} |
| 1042 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1043 | SRsrcFormatHi) |
| 1044 | .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32); |
| 1045 | |
| 1046 | // NewSRsrc = {Zero64, SRsrcFormat} |
| 1047 | BuildMI(MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), |
| 1048 | NewSRsrc) |
| 1049 | .addReg(Zero64) |
| 1050 | .addImm(AMDGPU::sub0_sub1) |
| 1051 | .addReg(SRsrcFormatLo) |
| 1052 | .addImm(AMDGPU::sub2) |
| 1053 | .addReg(SRsrcFormatHi) |
| 1054 | .addImm(AMDGPU::sub3); |
| 1055 | |
| 1056 | // Update the instruction to use NewVaddr |
| 1057 | MI->getOperand(VAddrIdx).setReg(NewVAddr); |
| 1058 | // Update the instruction to use NewSRsrc |
| 1059 | MI->getOperand(SRsrcIdx).setReg(NewSRsrc); |
| 1060 | } |
| 1061 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1064 | void SIInstrInfo::moveSMRDToVALU(MachineInstr *MI, MachineRegisterInfo &MRI) const { |
| 1065 | MachineBasicBlock *MBB = MI->getParent(); |
| 1066 | switch (MI->getOpcode()) { |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1067 | case AMDGPU::S_LOAD_DWORD_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1068 | case AMDGPU::S_LOAD_DWORD_SGPR: |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1069 | case AMDGPU::S_LOAD_DWORDX2_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1070 | case AMDGPU::S_LOAD_DWORDX2_SGPR: |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1071 | case AMDGPU::S_LOAD_DWORDX4_IMM: |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1072 | case AMDGPU::S_LOAD_DWORDX4_SGPR: |
| 1073 | unsigned NewOpcode = getVALUOp(*MI); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1074 | unsigned RegOffset; |
| 1075 | unsigned ImmOffset; |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1076 | |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1077 | if (MI->getOperand(2).isReg()) { |
| 1078 | RegOffset = MI->getOperand(2).getReg(); |
| 1079 | ImmOffset = 0; |
| 1080 | } else { |
| 1081 | assert(MI->getOperand(2).isImm()); |
| 1082 | // SMRD instructions take a dword offsets and MUBUF instructions |
| 1083 | // take a byte offset. |
| 1084 | ImmOffset = MI->getOperand(2).getImm() << 2; |
| 1085 | RegOffset = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1086 | if (isUInt<12>(ImmOffset)) { |
| 1087 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1088 | RegOffset) |
| 1089 | .addImm(0); |
| 1090 | } else { |
| 1091 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), |
| 1092 | RegOffset) |
| 1093 | .addImm(ImmOffset); |
| 1094 | ImmOffset = 0; |
| 1095 | } |
| 1096 | } |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1097 | |
| 1098 | unsigned SRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1099 | unsigned DWord0 = RegOffset; |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1100 | unsigned DWord1 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1101 | unsigned DWord2 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1102 | unsigned DWord3 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1103 | |
| 1104 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord1) |
| 1105 | .addImm(0); |
| 1106 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord2) |
| 1107 | .addImm(AMDGPU::RSRC_DATA_FORMAT & 0xFFFFFFFF); |
| 1108 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::S_MOV_B32), DWord3) |
| 1109 | .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32); |
| 1110 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), SRsrc) |
| 1111 | .addReg(DWord0) |
| 1112 | .addImm(AMDGPU::sub0) |
| 1113 | .addReg(DWord1) |
| 1114 | .addImm(AMDGPU::sub1) |
| 1115 | .addReg(DWord2) |
| 1116 | .addImm(AMDGPU::sub2) |
| 1117 | .addReg(DWord3) |
| 1118 | .addImm(AMDGPU::sub3); |
| 1119 | MI->setDesc(get(NewOpcode)); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1120 | if (MI->getOperand(2).isReg()) { |
| 1121 | MI->getOperand(2).setReg(MI->getOperand(1).getReg()); |
| 1122 | } else { |
| 1123 | MI->getOperand(2).ChangeToRegister(MI->getOperand(1).getReg(), false); |
| 1124 | } |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1125 | MI->getOperand(1).setReg(SRsrc); |
Tom Stellard | 4c00b52 | 2014-05-09 16:42:22 +0000 | [diff] [blame] | 1126 | MI->addOperand(*MBB->getParent(), MachineOperand::CreateImm(ImmOffset)); |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1130 | void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const { |
| 1131 | SmallVector<MachineInstr *, 128> Worklist; |
| 1132 | Worklist.push_back(&TopInst); |
| 1133 | |
| 1134 | while (!Worklist.empty()) { |
| 1135 | MachineInstr *Inst = Worklist.pop_back_val(); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1136 | MachineBasicBlock *MBB = Inst->getParent(); |
| 1137 | MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); |
| 1138 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1139 | unsigned Opcode = Inst->getOpcode(); |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1140 | unsigned NewOpcode = getVALUOp(*Inst); |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1141 | |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1142 | // Handle some special cases |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1143 | switch (Opcode) { |
Tom Stellard | 0c354f2 | 2014-04-30 15:31:29 +0000 | [diff] [blame] | 1144 | default: |
| 1145 | if (isSMRD(Inst->getOpcode())) { |
| 1146 | moveSMRDToVALU(Inst, MRI); |
| 1147 | } |
| 1148 | break; |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 1149 | case AMDGPU::S_MOV_B64: { |
| 1150 | DebugLoc DL = Inst->getDebugLoc(); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1151 | |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 1152 | // If the source operand is a register we can replace this with a |
| 1153 | // copy. |
| 1154 | if (Inst->getOperand(1).isReg()) { |
| 1155 | MachineInstr *Copy = BuildMI(*MBB, Inst, DL, get(TargetOpcode::COPY)) |
| 1156 | .addOperand(Inst->getOperand(0)) |
| 1157 | .addOperand(Inst->getOperand(1)); |
| 1158 | Worklist.push_back(Copy); |
| 1159 | } else { |
| 1160 | // Otherwise, we need to split this into two movs, because there is |
| 1161 | // no 64-bit VALU move instruction. |
| 1162 | unsigned Reg = Inst->getOperand(0).getReg(); |
| 1163 | unsigned Dst = split64BitImm(Worklist, |
| 1164 | Inst, |
| 1165 | MRI, |
| 1166 | MRI.getRegClass(Reg), |
| 1167 | Inst->getOperand(1)); |
| 1168 | MRI.replaceRegWith(Reg, Dst); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1169 | } |
Matt Arsenault | bd99580 | 2014-03-24 18:26:52 +0000 | [diff] [blame] | 1170 | Inst->eraseFromParent(); |
| 1171 | continue; |
| 1172 | } |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1173 | case AMDGPU::S_AND_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1174 | splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1175 | Inst->eraseFromParent(); |
| 1176 | continue; |
| 1177 | |
| 1178 | case AMDGPU::S_OR_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1179 | splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1180 | Inst->eraseFromParent(); |
| 1181 | continue; |
| 1182 | |
| 1183 | case AMDGPU::S_XOR_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1184 | splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1185 | Inst->eraseFromParent(); |
| 1186 | continue; |
| 1187 | |
| 1188 | case AMDGPU::S_NOT_B64: |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1189 | splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1190 | Inst->eraseFromParent(); |
| 1191 | continue; |
| 1192 | |
Matt Arsenault | 8333e43 | 2014-06-10 19:18:24 +0000 | [diff] [blame] | 1193 | case AMDGPU::S_BCNT1_I32_B64: |
| 1194 | splitScalar64BitBCNT(Worklist, Inst); |
| 1195 | Inst->eraseFromParent(); |
| 1196 | continue; |
| 1197 | |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1198 | case AMDGPU::S_BFE_U64: |
| 1199 | case AMDGPU::S_BFE_I64: |
| 1200 | case AMDGPU::S_BFM_B64: |
| 1201 | llvm_unreachable("Moving this op to VALU not implemented"); |
Tom Stellard | e038720 | 2014-03-21 15:51:54 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 1204 | if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { |
| 1205 | // We cannot move this instruction to the VALU, so we should try to |
| 1206 | // legalize its operands instead. |
| 1207 | legalizeOperands(Inst); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1208 | continue; |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 1209 | } |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1210 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1211 | // Use the new VALU Opcode. |
| 1212 | const MCInstrDesc &NewDesc = get(NewOpcode); |
| 1213 | Inst->setDesc(NewDesc); |
| 1214 | |
Matt Arsenault | f0b1e3a | 2013-11-18 20:09:21 +0000 | [diff] [blame] | 1215 | // Remove any references to SCC. Vector instructions can't read from it, and |
| 1216 | // We're just about to add the implicit use / defs of VCC, and we don't want |
| 1217 | // both. |
| 1218 | for (unsigned i = Inst->getNumOperands() - 1; i > 0; --i) { |
| 1219 | MachineOperand &Op = Inst->getOperand(i); |
| 1220 | if (Op.isReg() && Op.getReg() == AMDGPU::SCC) |
| 1221 | Inst->RemoveOperand(i); |
| 1222 | } |
| 1223 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1224 | if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { |
| 1225 | // We are converting these to a BFE, so we need to add the missing |
| 1226 | // operands for the size and offset. |
| 1227 | unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1228 | Inst->addOperand(Inst->getOperand(1)); |
| 1229 | Inst->getOperand(1).ChangeToImmediate(0); |
| 1230 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1231 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1232 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1233 | Inst->addOperand(MachineOperand::CreateImm(Size)); |
| 1234 | |
| 1235 | // XXX - Other pointless operands. There are 4, but it seems you only need |
| 1236 | // 3 to not hit an assertion later in MCInstLower. |
| 1237 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1238 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | b5b5110 | 2014-06-10 19:18:21 +0000 | [diff] [blame] | 1239 | } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { |
| 1240 | // The VALU version adds the second operand to the result, so insert an |
| 1241 | // extra 0 operand. |
| 1242 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1245 | addDescImplicitUseDef(NewDesc, Inst); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1246 | |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1247 | if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { |
| 1248 | const MachineOperand &OffsetWidthOp = Inst->getOperand(2); |
| 1249 | // If we need to move this to VGPRs, we need to unpack the second operand |
| 1250 | // back into the 2 separate ones for bit offset and width. |
| 1251 | assert(OffsetWidthOp.isImm() && |
| 1252 | "Scalar BFE is only implemented for constant width and offset"); |
| 1253 | uint32_t Imm = OffsetWidthOp.getImm(); |
| 1254 | |
| 1255 | uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. |
| 1256 | uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. |
| 1257 | |
| 1258 | Inst->RemoveOperand(2); // Remove old immediate. |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1259 | Inst->addOperand(Inst->getOperand(1)); |
| 1260 | Inst->getOperand(1).ChangeToImmediate(0); |
Matt Arsenault | 4b0402e | 2014-05-13 23:45:50 +0000 | [diff] [blame] | 1261 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1262 | Inst->addOperand(MachineOperand::CreateImm(Offset)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1263 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1264 | Inst->addOperand(MachineOperand::CreateImm(BitWidth)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1265 | Inst->addOperand(MachineOperand::CreateImm(0)); |
| 1266 | Inst->addOperand(MachineOperand::CreateImm(0)); |
Matt Arsenault | 78b8670 | 2014-04-18 05:19:26 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1269 | // Update the destination register class. |
Tom Stellard | e1a2445 | 2014-04-17 21:00:01 +0000 | [diff] [blame] | 1270 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1271 | const TargetRegisterClass *NewDstRC = getOpRegClass(*Inst, 0); |
| 1272 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1273 | switch (Opcode) { |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1274 | // For target instructions, getOpRegClass just returns the virtual |
| 1275 | // register class associated with the operand, so we need to find an |
| 1276 | // equivalent VGPR register class in order to move the instruction to the |
| 1277 | // VALU. |
| 1278 | case AMDGPU::COPY: |
| 1279 | case AMDGPU::PHI: |
| 1280 | case AMDGPU::REG_SEQUENCE: |
Tom Stellard | 204e61b | 2014-04-07 19:45:45 +0000 | [diff] [blame] | 1281 | case AMDGPU::INSERT_SUBREG: |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1282 | if (RI.hasVGPRs(NewDstRC)) |
| 1283 | continue; |
| 1284 | NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); |
| 1285 | if (!NewDstRC) |
| 1286 | continue; |
| 1287 | break; |
| 1288 | default: |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | unsigned DstReg = Inst->getOperand(0).getReg(); |
| 1293 | unsigned NewDstReg = MRI.createVirtualRegister(NewDstRC); |
| 1294 | MRI.replaceRegWith(DstReg, NewDstReg); |
| 1295 | |
Tom Stellard | e1a2445 | 2014-04-17 21:00:01 +0000 | [diff] [blame] | 1296 | // Legalize the operands |
| 1297 | legalizeOperands(Inst); |
| 1298 | |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1299 | for (MachineRegisterInfo::use_iterator I = MRI.use_begin(NewDstReg), |
| 1300 | E = MRI.use_end(); I != E; ++I) { |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1301 | MachineInstr &UseMI = *I->getParent(); |
Tom Stellard | 8216602 | 2013-11-13 23:36:37 +0000 | [diff] [blame] | 1302 | if (!canReadVGPR(UseMI, I.getOperandNo())) { |
| 1303 | Worklist.push_back(&UseMI); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1309 | //===----------------------------------------------------------------------===// |
| 1310 | // Indirect addressing callbacks |
| 1311 | //===----------------------------------------------------------------------===// |
| 1312 | |
| 1313 | unsigned SIInstrInfo::calculateIndirectAddress(unsigned RegIndex, |
| 1314 | unsigned Channel) const { |
| 1315 | assert(Channel == 0); |
| 1316 | return RegIndex; |
| 1317 | } |
| 1318 | |
Tom Stellard | 26a3b67 | 2013-10-22 18:19:10 +0000 | [diff] [blame] | 1319 | const TargetRegisterClass *SIInstrInfo::getIndirectAddrRegClass() const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1320 | return &AMDGPU::VReg_32RegClass; |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Matt Arsenault | 689f325 | 2014-06-09 16:36:31 +0000 | [diff] [blame] | 1323 | void SIInstrInfo::splitScalar64BitUnaryOp( |
| 1324 | SmallVectorImpl<MachineInstr *> &Worklist, |
| 1325 | MachineInstr *Inst, |
| 1326 | unsigned Opcode) const { |
| 1327 | MachineBasicBlock &MBB = *Inst->getParent(); |
| 1328 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1329 | |
| 1330 | MachineOperand &Dest = Inst->getOperand(0); |
| 1331 | MachineOperand &Src0 = Inst->getOperand(1); |
| 1332 | DebugLoc DL = Inst->getDebugLoc(); |
| 1333 | |
| 1334 | MachineBasicBlock::iterator MII = Inst; |
| 1335 | |
| 1336 | const MCInstrDesc &InstDesc = get(Opcode); |
| 1337 | const TargetRegisterClass *Src0RC = Src0.isReg() ? |
| 1338 | MRI.getRegClass(Src0.getReg()) : |
| 1339 | &AMDGPU::SGPR_32RegClass; |
| 1340 | |
| 1341 | const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); |
| 1342 | |
| 1343 | MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1344 | AMDGPU::sub0, Src0SubRC); |
| 1345 | |
| 1346 | const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); |
| 1347 | const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0); |
| 1348 | |
| 1349 | unsigned DestSub0 = MRI.createVirtualRegister(DestRC); |
| 1350 | MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0) |
| 1351 | .addOperand(SrcReg0Sub0); |
| 1352 | |
| 1353 | MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1354 | AMDGPU::sub1, Src0SubRC); |
| 1355 | |
| 1356 | unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC); |
| 1357 | MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1) |
| 1358 | .addOperand(SrcReg0Sub1); |
| 1359 | |
| 1360 | unsigned FullDestReg = MRI.createVirtualRegister(DestRC); |
| 1361 | BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) |
| 1362 | .addReg(DestSub0) |
| 1363 | .addImm(AMDGPU::sub0) |
| 1364 | .addReg(DestSub1) |
| 1365 | .addImm(AMDGPU::sub1); |
| 1366 | |
| 1367 | MRI.replaceRegWith(Dest.getReg(), FullDestReg); |
| 1368 | |
| 1369 | // Try to legalize the operands in case we need to swap the order to keep it |
| 1370 | // valid. |
| 1371 | Worklist.push_back(LoHalf); |
| 1372 | Worklist.push_back(HiHalf); |
| 1373 | } |
| 1374 | |
| 1375 | void SIInstrInfo::splitScalar64BitBinaryOp( |
| 1376 | SmallVectorImpl<MachineInstr *> &Worklist, |
| 1377 | MachineInstr *Inst, |
| 1378 | unsigned Opcode) const { |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1379 | MachineBasicBlock &MBB = *Inst->getParent(); |
| 1380 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1381 | |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1382 | MachineOperand &Dest = Inst->getOperand(0); |
| 1383 | MachineOperand &Src0 = Inst->getOperand(1); |
| 1384 | MachineOperand &Src1 = Inst->getOperand(2); |
| 1385 | DebugLoc DL = Inst->getDebugLoc(); |
| 1386 | |
| 1387 | MachineBasicBlock::iterator MII = Inst; |
| 1388 | |
| 1389 | const MCInstrDesc &InstDesc = get(Opcode); |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1390 | const TargetRegisterClass *Src0RC = Src0.isReg() ? |
| 1391 | MRI.getRegClass(Src0.getReg()) : |
| 1392 | &AMDGPU::SGPR_32RegClass; |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1393 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1394 | const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); |
| 1395 | const TargetRegisterClass *Src1RC = Src1.isReg() ? |
| 1396 | MRI.getRegClass(Src1.getReg()) : |
| 1397 | &AMDGPU::SGPR_32RegClass; |
| 1398 | |
| 1399 | const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); |
| 1400 | |
| 1401 | MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1402 | AMDGPU::sub0, Src0SubRC); |
| 1403 | MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, |
| 1404 | AMDGPU::sub0, Src1SubRC); |
| 1405 | |
| 1406 | const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); |
| 1407 | const TargetRegisterClass *DestSubRC = RI.getSubRegClass(DestRC, AMDGPU::sub0); |
| 1408 | |
| 1409 | unsigned DestSub0 = MRI.createVirtualRegister(DestRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1410 | MachineInstr *LoHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub0) |
Matt Arsenault | 248b7b6 | 2014-03-24 20:08:09 +0000 | [diff] [blame] | 1411 | .addOperand(SrcReg0Sub0) |
| 1412 | .addOperand(SrcReg1Sub0); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1413 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1414 | MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, |
| 1415 | AMDGPU::sub1, Src0SubRC); |
| 1416 | MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, |
| 1417 | AMDGPU::sub1, Src1SubRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1418 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1419 | unsigned DestSub1 = MRI.createVirtualRegister(DestSubRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1420 | MachineInstr *HiHalf = BuildMI(MBB, MII, DL, InstDesc, DestSub1) |
Matt Arsenault | 248b7b6 | 2014-03-24 20:08:09 +0000 | [diff] [blame] | 1421 | .addOperand(SrcReg0Sub1) |
| 1422 | .addOperand(SrcReg1Sub1); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1423 | |
Matt Arsenault | 684dc80 | 2014-03-24 20:08:13 +0000 | [diff] [blame] | 1424 | unsigned FullDestReg = MRI.createVirtualRegister(DestRC); |
Matt Arsenault | f35182c | 2014-03-24 20:08:05 +0000 | [diff] [blame] | 1425 | BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) |
| 1426 | .addReg(DestSub0) |
| 1427 | .addImm(AMDGPU::sub0) |
| 1428 | .addReg(DestSub1) |
| 1429 | .addImm(AMDGPU::sub1); |
| 1430 | |
| 1431 | MRI.replaceRegWith(Dest.getReg(), FullDestReg); |
| 1432 | |
| 1433 | // Try to legalize the operands in case we need to swap the order to keep it |
| 1434 | // valid. |
| 1435 | Worklist.push_back(LoHalf); |
| 1436 | Worklist.push_back(HiHalf); |
| 1437 | } |
| 1438 | |
Matt Arsenault | 8333e43 | 2014-06-10 19:18:24 +0000 | [diff] [blame] | 1439 | void SIInstrInfo::splitScalar64BitBCNT(SmallVectorImpl<MachineInstr *> &Worklist, |
| 1440 | MachineInstr *Inst) const { |
| 1441 | MachineBasicBlock &MBB = *Inst->getParent(); |
| 1442 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1443 | |
| 1444 | MachineBasicBlock::iterator MII = Inst; |
| 1445 | DebugLoc DL = Inst->getDebugLoc(); |
| 1446 | |
| 1447 | MachineOperand &Dest = Inst->getOperand(0); |
| 1448 | MachineOperand &Src = Inst->getOperand(1); |
| 1449 | |
| 1450 | const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e32); |
| 1451 | const TargetRegisterClass *SrcRC = Src.isReg() ? |
| 1452 | MRI.getRegClass(Src.getReg()) : |
| 1453 | &AMDGPU::SGPR_32RegClass; |
| 1454 | |
| 1455 | unsigned MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1456 | unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1457 | |
| 1458 | const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); |
| 1459 | |
| 1460 | MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, |
| 1461 | AMDGPU::sub0, SrcSubRC); |
| 1462 | MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, |
| 1463 | AMDGPU::sub1, SrcSubRC); |
| 1464 | |
| 1465 | MachineInstr *First = BuildMI(MBB, MII, DL, InstDesc, MidReg) |
| 1466 | .addOperand(SrcRegSub0) |
| 1467 | .addImm(0); |
| 1468 | |
| 1469 | MachineInstr *Second = BuildMI(MBB, MII, DL, InstDesc, ResultReg) |
| 1470 | .addOperand(SrcRegSub1) |
| 1471 | .addReg(MidReg); |
| 1472 | |
| 1473 | MRI.replaceRegWith(Dest.getReg(), ResultReg); |
| 1474 | |
| 1475 | Worklist.push_back(First); |
| 1476 | Worklist.push_back(Second); |
| 1477 | } |
| 1478 | |
Matt Arsenault | 27cc958 | 2014-04-18 01:53:18 +0000 | [diff] [blame] | 1479 | void SIInstrInfo::addDescImplicitUseDef(const MCInstrDesc &NewDesc, |
| 1480 | MachineInstr *Inst) const { |
| 1481 | // Add the implict and explicit register definitions. |
| 1482 | if (NewDesc.ImplicitUses) { |
| 1483 | for (unsigned i = 0; NewDesc.ImplicitUses[i]; ++i) { |
| 1484 | unsigned Reg = NewDesc.ImplicitUses[i]; |
| 1485 | Inst->addOperand(MachineOperand::CreateReg(Reg, false, true)); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | if (NewDesc.ImplicitDefs) { |
| 1490 | for (unsigned i = 0; NewDesc.ImplicitDefs[i]; ++i) { |
| 1491 | unsigned Reg = NewDesc.ImplicitDefs[i]; |
| 1492 | Inst->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1497 | MachineInstrBuilder SIInstrInfo::buildIndirectWrite( |
| 1498 | MachineBasicBlock *MBB, |
| 1499 | MachineBasicBlock::iterator I, |
| 1500 | unsigned ValueReg, |
| 1501 | unsigned Address, unsigned OffsetReg) const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1502 | const DebugLoc &DL = MBB->findDebugLoc(I); |
| 1503 | unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister( |
| 1504 | getIndirectIndexBegin(*MBB->getParent())); |
| 1505 | |
| 1506 | return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_DST_V1)) |
| 1507 | .addReg(IndirectBaseReg, RegState::Define) |
| 1508 | .addOperand(I->getOperand(0)) |
| 1509 | .addReg(IndirectBaseReg) |
| 1510 | .addReg(OffsetReg) |
| 1511 | .addImm(0) |
| 1512 | .addReg(ValueReg); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | MachineInstrBuilder SIInstrInfo::buildIndirectRead( |
| 1516 | MachineBasicBlock *MBB, |
| 1517 | MachineBasicBlock::iterator I, |
| 1518 | unsigned ValueReg, |
| 1519 | unsigned Address, unsigned OffsetReg) const { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1520 | const DebugLoc &DL = MBB->findDebugLoc(I); |
| 1521 | unsigned IndirectBaseReg = AMDGPU::VReg_32RegClass.getRegister( |
| 1522 | getIndirectIndexBegin(*MBB->getParent())); |
| 1523 | |
| 1524 | return BuildMI(*MBB, I, DL, get(AMDGPU::SI_INDIRECT_SRC)) |
| 1525 | .addOperand(I->getOperand(0)) |
| 1526 | .addOperand(I->getOperand(1)) |
| 1527 | .addReg(IndirectBaseReg) |
| 1528 | .addReg(OffsetReg) |
| 1529 | .addImm(0); |
| 1530 | |
| 1531 | } |
| 1532 | |
| 1533 | void SIInstrInfo::reserveIndirectRegisters(BitVector &Reserved, |
| 1534 | const MachineFunction &MF) const { |
| 1535 | int End = getIndirectIndexEnd(MF); |
| 1536 | int Begin = getIndirectIndexBegin(MF); |
| 1537 | |
| 1538 | if (End == -1) |
| 1539 | return; |
| 1540 | |
| 1541 | |
| 1542 | for (int Index = Begin; Index <= End; ++Index) |
| 1543 | Reserved.set(AMDGPU::VReg_32RegClass.getRegister(Index)); |
| 1544 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1545 | for (int Index = std::max(0, Begin - 1); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1546 | Reserved.set(AMDGPU::VReg_64RegClass.getRegister(Index)); |
| 1547 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1548 | for (int Index = std::max(0, Begin - 2); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1549 | Reserved.set(AMDGPU::VReg_96RegClass.getRegister(Index)); |
| 1550 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1551 | for (int Index = std::max(0, Begin - 3); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1552 | Reserved.set(AMDGPU::VReg_128RegClass.getRegister(Index)); |
| 1553 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1554 | for (int Index = std::max(0, Begin - 7); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1555 | Reserved.set(AMDGPU::VReg_256RegClass.getRegister(Index)); |
| 1556 | |
Tom Stellard | 415ef6d | 2013-11-13 23:58:51 +0000 | [diff] [blame] | 1557 | for (int Index = std::max(0, Begin - 15); Index <= End; ++Index) |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1558 | Reserved.set(AMDGPU::VReg_512RegClass.getRegister(Index)); |
Tom Stellard | f3b2a1e | 2013-02-06 17:32:29 +0000 | [diff] [blame] | 1559 | } |