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