R600/SI: Add a MUBUF store pattern for Reg+Imm offsets
[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 "R600Defines.h"
23 #include "R600MachineFunctionInfo.h"
24 #include "R600RegisterInfo.h"
25 #include "SIDefines.h"
26 #include "SIMachineFunctionInfo.h"
27 #include "SIRegisterInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/Support/ELF.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Target/TargetLoweringObjectFile.h"
35
36 using namespace llvm;
37
38
39 static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm,
40                                               MCStreamer &Streamer) {
41   return new AMDGPUAsmPrinter(tm, Streamer);
42 }
43
44 extern "C" void LLVMInitializeR600AsmPrinter() {
45   TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
46 }
47
48 AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
49     : AsmPrinter(TM, Streamer) {
50   DisasmEnabled = TM.getSubtarget<AMDGPUSubtarget>().dumpCode();
51 }
52
53 /// We need to override this function so we can avoid
54 /// the call to EmitFunctionHeader(), which the MCPureStreamer can't handle.
55 bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
56   SetupMachineFunction(MF);
57
58   OutStreamer.emitRawComment(Twine('@') + MF.getName() + Twine(':'));
59
60   MCContext &Context = getObjFileLowering().getContext();
61   const MCSectionELF *ConfigSection = Context.getELFSection(".AMDGPU.config",
62                                               ELF::SHT_PROGBITS, 0,
63                                               SectionKind::getReadOnly());
64   OutStreamer.SwitchSection(ConfigSection);
65
66   const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
67   SIProgramInfo KernelInfo;
68   if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
69     findNumUsedRegistersSI(MF, KernelInfo.NumSGPR, KernelInfo.NumVGPR);
70     EmitProgramInfoSI(MF, KernelInfo);
71   } else {
72     EmitProgramInfoR600(MF);
73   }
74
75   DisasmLines.clear();
76   HexLines.clear();
77   DisasmLineMaxLen = 0;
78
79   OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
80   EmitFunctionBody();
81
82   if (isVerbose()) {
83     const MCSectionELF *CommentSection
84       = Context.getELFSection(".AMDGPU.csdata",
85                               ELF::SHT_PROGBITS, 0,
86                               SectionKind::getReadOnly());
87     OutStreamer.SwitchSection(CommentSection);
88
89     if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
90       OutStreamer.emitRawComment(" Kernel info:", false);
91       OutStreamer.emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
92                                  false);
93       OutStreamer.emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
94                                  false);
95     } else {
96       R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
97       OutStreamer.emitRawComment(
98         Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
99     }
100   }
101
102   if (STM.dumpCode()) {
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104     MF.dump();
105 #endif
106
107     if (DisasmEnabled) {
108       OutStreamer.SwitchSection(Context.getELFSection(".AMDGPU.disasm",
109                                                   ELF::SHT_NOTE, 0,
110                                                   SectionKind::getReadOnly()));
111
112       for (size_t i = 0; i < DisasmLines.size(); ++i) {
113         std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
114         Comment += " ; " + HexLines[i] + "\n";
115
116         OutStreamer.EmitBytes(StringRef(DisasmLines[i]));
117         OutStreamer.EmitBytes(StringRef(Comment));
118       }
119     }
120   }
121
122   return false;
123 }
124
125 void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) {
126   unsigned MaxGPR = 0;
127   bool killPixel = false;
128   const R600RegisterInfo * RI =
129                 static_cast<const R600RegisterInfo*>(TM.getRegisterInfo());
130   R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
131   const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
132
133   for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
134                                                   BB != BB_E; ++BB) {
135     MachineBasicBlock &MBB = *BB;
136     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
137                                                     I != E; ++I) {
138       MachineInstr &MI = *I;
139       if (MI.getOpcode() == AMDGPU::KILLGT)
140         killPixel = true;
141       unsigned numOperands = MI.getNumOperands();
142       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
143         MachineOperand & MO = MI.getOperand(op_idx);
144         if (!MO.isReg())
145           continue;
146         unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
147
148         // Register with value > 127 aren't GPR
149         if (HWReg > 127)
150           continue;
151         MaxGPR = std::max(MaxGPR, HWReg);
152       }
153     }
154   }
155
156   unsigned RsrcReg;
157   if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
158     // Evergreen / Northern Islands
159     switch (MFI->ShaderType) {
160     default: // Fall through
161     case ShaderType::COMPUTE:  RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
162     case ShaderType::GEOMETRY: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
163     case ShaderType::PIXEL:    RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
164     case ShaderType::VERTEX:   RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
165     }
166   } else {
167     // R600 / R700
168     switch (MFI->ShaderType) {
169     default: // Fall through
170     case ShaderType::GEOMETRY: // Fall through
171     case ShaderType::COMPUTE:  // Fall through
172     case ShaderType::VERTEX:   RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
173     case ShaderType::PIXEL:    RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
174     }
175   }
176
177   OutStreamer.EmitIntValue(RsrcReg, 4);
178   OutStreamer.EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
179                            S_STACK_SIZE(MFI->StackSize), 4);
180   OutStreamer.EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
181   OutStreamer.EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
182
183   if (MFI->ShaderType == ShaderType::COMPUTE) {
184     OutStreamer.EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
185     OutStreamer.EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4);
186   }
187 }
188
189 void AMDGPUAsmPrinter::findNumUsedRegistersSI(MachineFunction &MF,
190                                               unsigned &NumSGPR,
191                                               unsigned &NumVGPR) const {
192   unsigned MaxSGPR = 0;
193   unsigned MaxVGPR = 0;
194   bool VCCUsed = false;
195   const SIRegisterInfo * RI =
196                 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
197
198   for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
199                                                   BB != BB_E; ++BB) {
200     MachineBasicBlock &MBB = *BB;
201     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
202                                                     I != E; ++I) {
203       MachineInstr &MI = *I;
204
205       unsigned numOperands = MI.getNumOperands();
206       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
207         MachineOperand &MO = MI.getOperand(op_idx);
208         unsigned width = 0;
209         bool isSGPR = false;
210
211         if (!MO.isReg()) {
212           continue;
213         }
214         unsigned reg = MO.getReg();
215         if (reg == AMDGPU::VCC) {
216           VCCUsed = true;
217           continue;
218         }
219
220         switch (reg) {
221         default: break;
222         case AMDGPU::SCC:
223         case AMDGPU::EXEC:
224         case AMDGPU::M0:
225           continue;
226         }
227
228         if (AMDGPU::SReg_32RegClass.contains(reg)) {
229           isSGPR = true;
230           width = 1;
231         } else if (AMDGPU::VReg_32RegClass.contains(reg)) {
232           isSGPR = false;
233           width = 1;
234         } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
235           isSGPR = true;
236           width = 2;
237         } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
238           isSGPR = false;
239           width = 2;
240         } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
241           isSGPR = false;
242           width = 3;
243         } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
244           isSGPR = true;
245           width = 4;
246         } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
247           isSGPR = false;
248           width = 4;
249         } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
250           isSGPR = true;
251           width = 8;
252         } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
253           isSGPR = false;
254           width = 8;
255         } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
256           isSGPR = true;
257           width = 16;
258         } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
259           isSGPR = false;
260           width = 16;
261         } else {
262           llvm_unreachable("Unknown register class");
263         }
264         unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
265         unsigned maxUsed = hwReg + width - 1;
266         if (isSGPR) {
267           MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
268         } else {
269           MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
270         }
271       }
272     }
273   }
274
275   if (VCCUsed)
276     MaxSGPR += 2;
277
278   NumSGPR = MaxSGPR;
279   NumVGPR = MaxVGPR;
280 }
281
282 void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &Out,
283                                         MachineFunction &MF) const {
284   findNumUsedRegistersSI(MF, Out.NumSGPR, Out.NumVGPR);
285 }
286
287 void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF,
288                                          const SIProgramInfo &KernelInfo) {
289   const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
290
291   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
292   unsigned RsrcReg;
293   switch (MFI->ShaderType) {
294   default: // Fall through
295   case ShaderType::COMPUTE:  RsrcReg = R_00B848_COMPUTE_PGM_RSRC1; break;
296   case ShaderType::GEOMETRY: RsrcReg = R_00B228_SPI_SHADER_PGM_RSRC1_GS; break;
297   case ShaderType::PIXEL:    RsrcReg = R_00B028_SPI_SHADER_PGM_RSRC1_PS; break;
298   case ShaderType::VERTEX:   RsrcReg = R_00B128_SPI_SHADER_PGM_RSRC1_VS; break;
299   }
300
301   OutStreamer.EmitIntValue(RsrcReg, 4);
302   OutStreamer.EmitIntValue(S_00B028_VGPRS(KernelInfo.NumVGPR / 4) |
303                            S_00B028_SGPRS(KernelInfo.NumSGPR / 8), 4);
304
305   unsigned LDSAlignShift;
306   if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
307     // LDS is allocated in 64 dword blocks
308     LDSAlignShift = 8;
309   } else {
310     // LDS is allocated in 128 dword blocks
311     LDSAlignShift = 9;
312   }
313   unsigned LDSBlocks =
314           RoundUpToAlignment(MFI->LDSSize, 1 << LDSAlignShift) >> LDSAlignShift;
315
316   if (MFI->ShaderType == ShaderType::COMPUTE) {
317     OutStreamer.EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
318     OutStreamer.EmitIntValue(S_00B84C_LDS_SIZE(LDSBlocks), 4);
319   }
320   if (MFI->ShaderType == ShaderType::PIXEL) {
321     OutStreamer.EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
322     OutStreamer.EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(LDSBlocks), 4);
323     OutStreamer.EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
324     OutStreamer.EmitIntValue(MFI->PSInputAddr, 4);
325   }
326 }