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