Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===// |
| 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 | /// |
| 12 | /// The AMDGPUAsmPrinter is used to print both assembly string and also binary |
| 13 | /// code. When passed an MCAsmStreamer it prints assembly and when passed |
| 14 | /// an MCObjectStreamer it outputs binary code. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // |
| 18 | |
| 19 | |
| 20 | #include "AMDGPUAsmPrinter.h" |
| 21 | #include "AMDGPU.h" |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 22 | #include "SIDefines.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 23 | #include "SIMachineFunctionInfo.h" |
| 24 | #include "SIRegisterInfo.h" |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame] | 25 | #include "R600MachineFunctionInfo.h" |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 26 | #include "R600RegisterInfo.h" |
Tom Stellard | 3a7beafb3 | 2013-04-15 17:51:30 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCContext.h" |
| 28 | #include "llvm/MC/MCSectionELF.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCStreamer.h" |
Tom Stellard | 3a7beafb3 | 2013-04-15 17:51:30 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ELF.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | |
| 37 | static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm, |
| 38 | MCStreamer &Streamer) { |
| 39 | return new AMDGPUAsmPrinter(tm, Streamer); |
| 40 | } |
| 41 | |
| 42 | extern "C" void LLVMInitializeR600AsmPrinter() { |
| 43 | TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass); |
| 44 | } |
| 45 | |
| 46 | /// We need to override this function so we can avoid |
| 47 | /// the call to EmitFunctionHeader(), which the MCPureStreamer can't handle. |
| 48 | bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 49 | const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>(); |
| 50 | if (STM.dumpCode()) { |
| 51 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 52 | MF.dump(); |
| 53 | #endif |
| 54 | } |
| 55 | SetupMachineFunction(MF); |
Tom Stellard | 2e5e7a5 | 2013-02-05 17:09:11 +0000 | [diff] [blame] | 56 | if (OutStreamer.hasRawTextSupport()) { |
| 57 | OutStreamer.EmitRawText("@" + MF.getName() + ":"); |
| 58 | } |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 59 | |
| 60 | const MCSectionELF *ConfigSection = getObjFileLowering().getContext() |
| 61 | .getELFSection(".AMDGPU.config", |
Tom Stellard | 34e4068 | 2013-04-24 23:56:14 +0000 | [diff] [blame] | 62 | ELF::SHT_PROGBITS, 0, |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 63 | SectionKind::getReadOnly()); |
| 64 | OutStreamer.SwitchSection(ConfigSection); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 65 | if (STM.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) { |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 66 | EmitProgramInfoSI(MF); |
| 67 | } else { |
| 68 | EmitProgramInfoR600(MF); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 69 | } |
Tom Stellard | 3a7beafb3 | 2013-04-15 17:51:30 +0000 | [diff] [blame] | 70 | OutStreamer.SwitchSection(getObjFileLowering().getTextSection()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 71 | EmitFunctionBody(); |
| 72 | return false; |
| 73 | } |
| 74 | |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 75 | void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) { |
| 76 | unsigned MaxGPR = 0; |
Vincent Lejeune | 4a0beb5 | 2013-04-30 00:13:13 +0000 | [diff] [blame^] | 77 | bool killPixel = false; |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 78 | const R600RegisterInfo * RI = |
| 79 | static_cast<const R600RegisterInfo*>(TM.getRegisterInfo()); |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame] | 80 | R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 81 | |
| 82 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 83 | BB != BB_E; ++BB) { |
| 84 | MachineBasicBlock &MBB = *BB; |
| 85 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 86 | I != E; ++I) { |
| 87 | MachineInstr &MI = *I; |
Vincent Lejeune | 4a0beb5 | 2013-04-30 00:13:13 +0000 | [diff] [blame^] | 88 | if (MI.getOpcode() == AMDGPU::KILLGT) |
| 89 | killPixel = true; |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 90 | unsigned numOperands = MI.getNumOperands(); |
| 91 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 92 | MachineOperand & MO = MI.getOperand(op_idx); |
| 93 | if (!MO.isReg()) |
| 94 | continue; |
| 95 | unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff; |
| 96 | |
| 97 | // Register with value > 127 aren't GPR |
| 98 | if (HWReg > 127) |
| 99 | continue; |
| 100 | MaxGPR = std::max(MaxGPR, HWReg); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | OutStreamer.EmitIntValue(MaxGPR + 1, 4); |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame] | 105 | OutStreamer.EmitIntValue(MFI->StackSize, 4); |
Vincent Lejeune | 4a0beb5 | 2013-04-30 00:13:13 +0000 | [diff] [blame^] | 106 | OutStreamer.EmitIntValue(killPixel, 4); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 110 | unsigned MaxSGPR = 0; |
| 111 | unsigned MaxVGPR = 0; |
| 112 | bool VCCUsed = false; |
| 113 | const SIRegisterInfo * RI = |
| 114 | static_cast<const SIRegisterInfo*>(TM.getRegisterInfo()); |
| 115 | |
| 116 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 117 | BB != BB_E; ++BB) { |
| 118 | MachineBasicBlock &MBB = *BB; |
| 119 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 120 | I != E; ++I) { |
| 121 | MachineInstr &MI = *I; |
| 122 | |
| 123 | unsigned numOperands = MI.getNumOperands(); |
| 124 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 125 | MachineOperand & MO = MI.getOperand(op_idx); |
| 126 | unsigned maxUsed; |
| 127 | unsigned width = 0; |
| 128 | bool isSGPR = false; |
| 129 | unsigned reg; |
| 130 | unsigned hwReg; |
| 131 | if (!MO.isReg()) { |
| 132 | continue; |
| 133 | } |
| 134 | reg = MO.getReg(); |
| 135 | if (reg == AMDGPU::VCC) { |
| 136 | VCCUsed = true; |
| 137 | continue; |
| 138 | } |
| 139 | switch (reg) { |
| 140 | default: break; |
| 141 | case AMDGPU::EXEC: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 142 | case AMDGPU::M0: |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | if (AMDGPU::SReg_32RegClass.contains(reg)) { |
| 147 | isSGPR = true; |
| 148 | width = 1; |
| 149 | } else if (AMDGPU::VReg_32RegClass.contains(reg)) { |
| 150 | isSGPR = false; |
| 151 | width = 1; |
| 152 | } else if (AMDGPU::SReg_64RegClass.contains(reg)) { |
| 153 | isSGPR = true; |
| 154 | width = 2; |
| 155 | } else if (AMDGPU::VReg_64RegClass.contains(reg)) { |
| 156 | isSGPR = false; |
| 157 | width = 2; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 158 | } else if (AMDGPU::VReg_96RegClass.contains(reg)) { |
| 159 | isSGPR = false; |
| 160 | width = 3; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 161 | } else if (AMDGPU::SReg_128RegClass.contains(reg)) { |
| 162 | isSGPR = true; |
| 163 | width = 4; |
| 164 | } else if (AMDGPU::VReg_128RegClass.contains(reg)) { |
| 165 | isSGPR = false; |
| 166 | width = 4; |
| 167 | } else if (AMDGPU::SReg_256RegClass.contains(reg)) { |
| 168 | isSGPR = true; |
| 169 | width = 8; |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 170 | } else if (AMDGPU::VReg_256RegClass.contains(reg)) { |
| 171 | isSGPR = false; |
| 172 | width = 8; |
| 173 | } else if (AMDGPU::VReg_512RegClass.contains(reg)) { |
| 174 | isSGPR = false; |
| 175 | width = 16; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 176 | } else { |
| 177 | assert(!"Unknown register class"); |
| 178 | } |
Tom Stellard | 1c822a8 | 2013-02-07 19:39:45 +0000 | [diff] [blame] | 179 | hwReg = RI->getEncodingValue(reg) & 0xff; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 180 | maxUsed = hwReg + width - 1; |
| 181 | if (isSGPR) { |
| 182 | MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR; |
| 183 | } else { |
| 184 | MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | if (VCCUsed) { |
| 190 | MaxSGPR += 2; |
| 191 | } |
| 192 | SIMachineFunctionInfo * MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 193 | unsigned RsrcReg; |
| 194 | switch (MFI->ShaderType) { |
| 195 | default: // Fall through |
| 196 | case ShaderType::COMPUTE: RsrcReg = R_00B848_COMPUTE_PGM_RSRC1; break; |
| 197 | case ShaderType::GEOMETRY: RsrcReg = R_00B228_SPI_SHADER_PGM_RSRC1_GS; break; |
| 198 | case ShaderType::PIXEL: RsrcReg = R_00B028_SPI_SHADER_PGM_RSRC1_PS; break; |
| 199 | case ShaderType::VERTEX: RsrcReg = R_00B128_SPI_SHADER_PGM_RSRC1_VS; break; |
| 200 | } |
| 201 | |
| 202 | OutStreamer.EmitIntValue(RsrcReg, 4); |
| 203 | OutStreamer.EmitIntValue(S_00B028_VGPRS(MaxVGPR / 4) | S_00B028_SGPRS(MaxSGPR / 8), 4); |
| 204 | if (MFI->ShaderType == ShaderType::PIXEL) { |
| 205 | OutStreamer.EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4); |
| 206 | OutStreamer.EmitIntValue(MFI->PSInputAddr, 4); |
| 207 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 208 | } |