dc0461a641b6f847e1da4a5e428df951b370a1a3
[oota-llvm.git] / lib / Target / R600 / AMDGPUAsmPrinter.cpp
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"
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"
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);
56   if (OutStreamer.hasRawTextSupport()) {
57     OutStreamer.EmitRawText("@" + MF.getName() + ":");
58   }
59
60   const MCSectionELF *ConfigSection = getObjFileLowering().getContext()
61                                               .getELFSection(".AMDGPU.config",
62                                               ELF::SHT_NULL, 0,
63                                               SectionKind::getReadOnly());
64   OutStreamer.SwitchSection(ConfigSection);
65   if (STM.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
66     EmitProgramInfoSI(MF);
67   } else {
68     EmitProgramInfoR600(MF);
69   }
70   OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
71   EmitFunctionBody();
72   return false;
73 }
74
75 void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) {
76   unsigned MaxGPR = 0;
77   const R600RegisterInfo * RI =
78                 static_cast<const R600RegisterInfo*>(TM.getRegisterInfo());
79   R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
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);
102   OutStreamer.EmitIntValue(MFI->StackSize, 4);
103 }
104
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());
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:
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;
154         } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
155           isSGPR = false;
156           width = 3;
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;
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;
172         } else {
173           assert(!"Unknown register class");
174         }
175         hwReg = RI->getEncodingValue(reg) & 0xff;
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>();
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   }
204 }