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", |
| 62 | ELF::SHT_NULL, 0, |
| 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; |
| 77 | const R600RegisterInfo * RI = |
| 78 | static_cast<const R600RegisterInfo*>(TM.getRegisterInfo()); |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame^] | 79 | R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 80 | |
| 81 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 82 | BB != BB_E; ++BB) { |
| 83 | MachineBasicBlock &MBB = *BB; |
| 84 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 85 | I != E; ++I) { |
| 86 | MachineInstr &MI = *I; |
| 87 | unsigned numOperands = MI.getNumOperands(); |
| 88 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 89 | MachineOperand & MO = MI.getOperand(op_idx); |
| 90 | if (!MO.isReg()) |
| 91 | continue; |
| 92 | unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff; |
| 93 | |
| 94 | // Register with value > 127 aren't GPR |
| 95 | if (HWReg > 127) |
| 96 | continue; |
| 97 | MaxGPR = std::max(MaxGPR, HWReg); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | OutStreamer.EmitIntValue(MaxGPR + 1, 4); |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame^] | 102 | OutStreamer.EmitIntValue(MFI->StackSize, 4); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 106 | unsigned MaxSGPR = 0; |
| 107 | unsigned MaxVGPR = 0; |
| 108 | bool VCCUsed = false; |
| 109 | const SIRegisterInfo * RI = |
| 110 | static_cast<const SIRegisterInfo*>(TM.getRegisterInfo()); |
| 111 | |
| 112 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 113 | BB != BB_E; ++BB) { |
| 114 | MachineBasicBlock &MBB = *BB; |
| 115 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 116 | I != E; ++I) { |
| 117 | MachineInstr &MI = *I; |
| 118 | |
| 119 | unsigned numOperands = MI.getNumOperands(); |
| 120 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 121 | MachineOperand & MO = MI.getOperand(op_idx); |
| 122 | unsigned maxUsed; |
| 123 | unsigned width = 0; |
| 124 | bool isSGPR = false; |
| 125 | unsigned reg; |
| 126 | unsigned hwReg; |
| 127 | if (!MO.isReg()) { |
| 128 | continue; |
| 129 | } |
| 130 | reg = MO.getReg(); |
| 131 | if (reg == AMDGPU::VCC) { |
| 132 | VCCUsed = true; |
| 133 | continue; |
| 134 | } |
| 135 | switch (reg) { |
| 136 | default: break; |
| 137 | case AMDGPU::EXEC: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 138 | case AMDGPU::M0: |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | if (AMDGPU::SReg_32RegClass.contains(reg)) { |
| 143 | isSGPR = true; |
| 144 | width = 1; |
| 145 | } else if (AMDGPU::VReg_32RegClass.contains(reg)) { |
| 146 | isSGPR = false; |
| 147 | width = 1; |
| 148 | } else if (AMDGPU::SReg_64RegClass.contains(reg)) { |
| 149 | isSGPR = true; |
| 150 | width = 2; |
| 151 | } else if (AMDGPU::VReg_64RegClass.contains(reg)) { |
| 152 | isSGPR = false; |
| 153 | width = 2; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 154 | } else if (AMDGPU::VReg_96RegClass.contains(reg)) { |
| 155 | isSGPR = false; |
| 156 | width = 3; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 157 | } else if (AMDGPU::SReg_128RegClass.contains(reg)) { |
| 158 | isSGPR = true; |
| 159 | width = 4; |
| 160 | } else if (AMDGPU::VReg_128RegClass.contains(reg)) { |
| 161 | isSGPR = false; |
| 162 | width = 4; |
| 163 | } else if (AMDGPU::SReg_256RegClass.contains(reg)) { |
| 164 | isSGPR = true; |
| 165 | width = 8; |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 166 | } else if (AMDGPU::VReg_256RegClass.contains(reg)) { |
| 167 | isSGPR = false; |
| 168 | width = 8; |
| 169 | } else if (AMDGPU::VReg_512RegClass.contains(reg)) { |
| 170 | isSGPR = false; |
| 171 | width = 16; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 172 | } else { |
| 173 | assert(!"Unknown register class"); |
| 174 | } |
Tom Stellard | 1c822a8 | 2013-02-07 19:39:45 +0000 | [diff] [blame] | 175 | hwReg = RI->getEncodingValue(reg) & 0xff; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 176 | maxUsed = hwReg + width - 1; |
| 177 | if (isSGPR) { |
| 178 | MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR; |
| 179 | } else { |
| 180 | MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | if (VCCUsed) { |
| 186 | MaxSGPR += 2; |
| 187 | } |
| 188 | SIMachineFunctionInfo * MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 189 | unsigned RsrcReg; |
| 190 | switch (MFI->ShaderType) { |
| 191 | default: // Fall through |
| 192 | case ShaderType::COMPUTE: RsrcReg = R_00B848_COMPUTE_PGM_RSRC1; break; |
| 193 | case ShaderType::GEOMETRY: RsrcReg = R_00B228_SPI_SHADER_PGM_RSRC1_GS; break; |
| 194 | case ShaderType::PIXEL: RsrcReg = R_00B028_SPI_SHADER_PGM_RSRC1_PS; break; |
| 195 | case ShaderType::VERTEX: RsrcReg = R_00B128_SPI_SHADER_PGM_RSRC1_VS; break; |
| 196 | } |
| 197 | |
| 198 | OutStreamer.EmitIntValue(RsrcReg, 4); |
| 199 | OutStreamer.EmitIntValue(S_00B028_VGPRS(MaxVGPR / 4) | S_00B028_SGPRS(MaxSGPR / 8), 4); |
| 200 | if (MFI->ShaderType == ShaderType::PIXEL) { |
| 201 | OutStreamer.EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4); |
| 202 | OutStreamer.EmitIntValue(MFI->PSInputAddr, 4); |
| 203 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 204 | } |