1*c5015010STom Stellard //===-- R600AsmPrinter.cpp - R600 Assebly printer  ------------------------===//
2*c5015010STom Stellard //
3*c5015010STom Stellard //                     The LLVM Compiler Infrastructure
4*c5015010STom Stellard //
5*c5015010STom Stellard // This file is distributed under the University of Illinois Open Source
6*c5015010STom Stellard // License. See LICENSE.TXT for details.
7*c5015010STom Stellard //
8*c5015010STom Stellard //===----------------------------------------------------------------------===//
9*c5015010STom Stellard //
10*c5015010STom Stellard /// \file
11*c5015010STom Stellard ///
12*c5015010STom Stellard /// The R600AsmPrinter is used to print both assembly string and also binary
13*c5015010STom Stellard /// code.  When passed an MCAsmStreamer it prints assembly and when passed
14*c5015010STom Stellard /// an MCObjectStreamer it outputs binary code.
15*c5015010STom Stellard //
16*c5015010STom Stellard //===----------------------------------------------------------------------===//
17*c5015010STom Stellard 
18*c5015010STom Stellard #include "R600AsmPrinter.h"
19*c5015010STom Stellard #include "AMDGPUSubtarget.h"
20*c5015010STom Stellard #include "R600Defines.h"
21*c5015010STom Stellard #include "R600MachineFunctionInfo.h"
22*c5015010STom Stellard #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
23*c5015010STom Stellard #include "llvm/BinaryFormat/ELF.h"
24*c5015010STom Stellard #include "llvm/MC/MCContext.h"
25*c5015010STom Stellard #include "llvm/MC/MCSectionELF.h"
26*c5015010STom Stellard #include "llvm/MC/MCStreamer.h"
27*c5015010STom Stellard #include "llvm/Target/TargetLoweringObjectFile.h"
28*c5015010STom Stellard 
29*c5015010STom Stellard using namespace llvm;
30*c5015010STom Stellard 
31*c5015010STom Stellard AsmPrinter *
32*c5015010STom Stellard llvm::createR600AsmPrinterPass(TargetMachine &TM,
33*c5015010STom Stellard                                std::unique_ptr<MCStreamer> &&Streamer) {
34*c5015010STom Stellard   return new R600AsmPrinter(TM, std::move(Streamer));
35*c5015010STom Stellard }
36*c5015010STom Stellard 
37*c5015010STom Stellard R600AsmPrinter::R600AsmPrinter(TargetMachine &TM,
38*c5015010STom Stellard                                std::unique_ptr<MCStreamer> Streamer)
39*c5015010STom Stellard   : AsmPrinter(TM, std::move(Streamer)) { }
40*c5015010STom Stellard 
41*c5015010STom Stellard StringRef R600AsmPrinter::getPassName() const {
42*c5015010STom Stellard   return "R600 Assembly Printer";
43*c5015010STom Stellard }
44*c5015010STom Stellard 
45*c5015010STom Stellard void R600AsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
46*c5015010STom Stellard   unsigned MaxGPR = 0;
47*c5015010STom Stellard   bool killPixel = false;
48*c5015010STom Stellard   const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
49*c5015010STom Stellard   const R600RegisterInfo *RI = STM.getRegisterInfo();
50*c5015010STom Stellard   const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
51*c5015010STom Stellard 
52*c5015010STom Stellard   for (const MachineBasicBlock &MBB : MF) {
53*c5015010STom Stellard     for (const MachineInstr &MI : MBB) {
54*c5015010STom Stellard       if (MI.getOpcode() == AMDGPU::KILLGT)
55*c5015010STom Stellard         killPixel = true;
56*c5015010STom Stellard       unsigned numOperands = MI.getNumOperands();
57*c5015010STom Stellard       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
58*c5015010STom Stellard         const MachineOperand &MO = MI.getOperand(op_idx);
59*c5015010STom Stellard         if (!MO.isReg())
60*c5015010STom Stellard           continue;
61*c5015010STom Stellard         unsigned HWReg = RI->getHWRegIndex(MO.getReg());
62*c5015010STom Stellard 
63*c5015010STom Stellard         // Register with value > 127 aren't GPR
64*c5015010STom Stellard         if (HWReg > 127)
65*c5015010STom Stellard           continue;
66*c5015010STom Stellard         MaxGPR = std::max(MaxGPR, HWReg);
67*c5015010STom Stellard       }
68*c5015010STom Stellard     }
69*c5015010STom Stellard   }
70*c5015010STom Stellard 
71*c5015010STom Stellard   unsigned RsrcReg;
72*c5015010STom Stellard   if (STM.getGeneration() >= R600Subtarget::EVERGREEN) {
73*c5015010STom Stellard     // Evergreen / Northern Islands
74*c5015010STom Stellard     switch (MF.getFunction().getCallingConv()) {
75*c5015010STom Stellard     default: LLVM_FALLTHROUGH;
76*c5015010STom Stellard     case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
77*c5015010STom Stellard     case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
78*c5015010STom Stellard     case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
79*c5015010STom Stellard     case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
80*c5015010STom Stellard     }
81*c5015010STom Stellard   } else {
82*c5015010STom Stellard     // R600 / R700
83*c5015010STom Stellard     switch (MF.getFunction().getCallingConv()) {
84*c5015010STom Stellard     default: LLVM_FALLTHROUGH;
85*c5015010STom Stellard     case CallingConv::AMDGPU_GS: LLVM_FALLTHROUGH;
86*c5015010STom Stellard     case CallingConv::AMDGPU_CS: LLVM_FALLTHROUGH;
87*c5015010STom Stellard     case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
88*c5015010STom Stellard     case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
89*c5015010STom Stellard     }
90*c5015010STom Stellard   }
91*c5015010STom Stellard 
92*c5015010STom Stellard   OutStreamer->EmitIntValue(RsrcReg, 4);
93*c5015010STom Stellard   OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
94*c5015010STom Stellard                            S_STACK_SIZE(MFI->CFStackSize), 4);
95*c5015010STom Stellard   OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
96*c5015010STom Stellard   OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
97*c5015010STom Stellard 
98*c5015010STom Stellard   if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
99*c5015010STom Stellard     OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
100*c5015010STom Stellard     OutStreamer->EmitIntValue(alignTo(MFI->getLDSSize(), 4) >> 2, 4);
101*c5015010STom Stellard   }
102*c5015010STom Stellard }
103*c5015010STom Stellard 
104*c5015010STom Stellard bool R600AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
105*c5015010STom Stellard 
106*c5015010STom Stellard   SetupMachineFunction(MF);
107*c5015010STom Stellard 
108*c5015010STom Stellard   MCContext &Context = getObjFileLowering().getContext();
109*c5015010STom Stellard   MCSectionELF *ConfigSection =
110*c5015010STom Stellard       Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
111*c5015010STom Stellard   OutStreamer->SwitchSection(ConfigSection);
112*c5015010STom Stellard 
113*c5015010STom Stellard   EmitProgramInfoR600(MF);
114*c5015010STom Stellard 
115*c5015010STom Stellard   EmitFunctionBody();
116*c5015010STom Stellard 
117*c5015010STom Stellard   if (isVerbose()) {
118*c5015010STom Stellard     MCSectionELF *CommentSection =
119*c5015010STom Stellard         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
120*c5015010STom Stellard     OutStreamer->SwitchSection(CommentSection);
121*c5015010STom Stellard 
122*c5015010STom Stellard     R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
123*c5015010STom Stellard     OutStreamer->emitRawComment(
124*c5015010STom Stellard       Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->CFStackSize)));
125*c5015010STom Stellard   }
126*c5015010STom Stellard 
127*c5015010STom Stellard   return false;
128*c5015010STom Stellard }
129*c5015010STom Stellard 
130