1 //===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
20 #include "AMDGPUAsmPrinter.h"
22 #include "SIDefines.h"
23 #include "SIMachineFunctionInfo.h"
24 #include "SIRegisterInfo.h"
25 #include "R600MachineFunctionInfo.h"
26 #include "R600RegisterInfo.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCSectionELF.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Support/ELF.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
37 static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm,
38 MCStreamer &Streamer) {
39 return new AMDGPUAsmPrinter(tm, Streamer);
42 extern "C" void LLVMInitializeR600AsmPrinter() {
43 TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
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>();
51 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
55 SetupMachineFunction(MF);
56 if (OutStreamer.hasRawTextSupport()) {
57 OutStreamer.EmitRawText("@" + MF.getName() + ":");
60 const MCSectionELF *ConfigSection = getObjFileLowering().getContext()
61 .getELFSection(".AMDGPU.config",
63 SectionKind::getReadOnly());
64 OutStreamer.SwitchSection(ConfigSection);
65 if (STM.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
66 EmitProgramInfoSI(MF);
68 EmitProgramInfoR600(MF);
70 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
75 void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) {
77 const R600RegisterInfo * RI =
78 static_cast<const R600RegisterInfo*>(TM.getRegisterInfo());
79 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
81 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
83 MachineBasicBlock &MBB = *BB;
84 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
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);
92 unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
94 // Register with value > 127 aren't GPR
97 MaxGPR = std::max(MaxGPR, HWReg);
101 OutStreamer.EmitIntValue(MaxGPR + 1, 4);
102 OutStreamer.EmitIntValue(MFI->StackSize, 4);
105 void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF) {
106 unsigned MaxSGPR = 0;
107 unsigned MaxVGPR = 0;
108 bool VCCUsed = false;
109 const SIRegisterInfo * RI =
110 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
112 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
114 MachineBasicBlock &MBB = *BB;
115 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
117 MachineInstr &MI = *I;
119 unsigned numOperands = MI.getNumOperands();
120 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
121 MachineOperand & MO = MI.getOperand(op_idx);
131 if (reg == AMDGPU::VCC) {
142 if (AMDGPU::SReg_32RegClass.contains(reg)) {
145 } else if (AMDGPU::VReg_32RegClass.contains(reg)) {
148 } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
151 } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
154 } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
157 } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
160 } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
163 } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
166 } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
169 } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
173 assert(!"Unknown register class");
175 hwReg = RI->getEncodingValue(reg) & 0xff;
176 maxUsed = hwReg + width - 1;
178 MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
180 MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
188 SIMachineFunctionInfo * MFI = MF.getInfo<SIMachineFunctionInfo>();
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;
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);