1d68904f9SJames Henderson //===-- R600AsmPrinter.cpp - R600 Assembly printer ------------------------===//
2c5015010STom Stellard //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c5015010STom Stellard //
7c5015010STom Stellard //===----------------------------------------------------------------------===//
8c5015010STom Stellard //
9c5015010STom Stellard /// \file
10c5015010STom Stellard ///
11c5015010STom Stellard /// The R600AsmPrinter is used to print both assembly string and also binary
12c5015010STom Stellard /// code.  When passed an MCAsmStreamer it prints assembly and when passed
13c5015010STom Stellard /// an MCObjectStreamer it outputs binary code.
14c5015010STom Stellard //
15c5015010STom Stellard //===----------------------------------------------------------------------===//
16c5015010STom Stellard 
17c5015010STom Stellard #include "R600AsmPrinter.h"
1848958d02SDaniil Fukalov #include "MCTargetDesc/R600MCTargetDesc.h"
19c5015010STom Stellard #include "R600Defines.h"
20c5015010STom Stellard #include "R600MachineFunctionInfo.h"
21560d7e04Sdfukalov #include "R600Subtarget.h"
22c5015010STom Stellard #include "llvm/BinaryFormat/ELF.h"
23c5015010STom Stellard #include "llvm/MC/MCContext.h"
24c5015010STom Stellard #include "llvm/MC/MCSectionELF.h"
25c5015010STom Stellard #include "llvm/MC/MCStreamer.h"
26c5015010STom Stellard #include "llvm/Target/TargetLoweringObjectFile.h"
27c5015010STom Stellard 
28c5015010STom Stellard using namespace llvm;
29c5015010STom Stellard 
30c5015010STom Stellard AsmPrinter *
createR600AsmPrinterPass(TargetMachine & TM,std::unique_ptr<MCStreamer> && Streamer)31c5015010STom Stellard llvm::createR600AsmPrinterPass(TargetMachine &TM,
32c5015010STom Stellard                                std::unique_ptr<MCStreamer> &&Streamer) {
33c5015010STom Stellard   return new R600AsmPrinter(TM, std::move(Streamer));
34c5015010STom Stellard }
35c5015010STom Stellard 
R600AsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)36c5015010STom Stellard R600AsmPrinter::R600AsmPrinter(TargetMachine &TM,
37c5015010STom Stellard                                std::unique_ptr<MCStreamer> Streamer)
38c5015010STom Stellard   : AsmPrinter(TM, std::move(Streamer)) { }
39c5015010STom Stellard 
getPassName() const40c5015010STom Stellard StringRef R600AsmPrinter::getPassName() const {
41c5015010STom Stellard   return "R600 Assembly Printer";
42c5015010STom Stellard }
43c5015010STom Stellard 
EmitProgramInfoR600(const MachineFunction & MF)44c5015010STom Stellard void R600AsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
45c5015010STom Stellard   unsigned MaxGPR = 0;
46c5015010STom Stellard   bool killPixel = false;
47c5015010STom Stellard   const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
48c5015010STom Stellard   const R600RegisterInfo *RI = STM.getRegisterInfo();
49c5015010STom Stellard   const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
50c5015010STom Stellard 
51c5015010STom Stellard   for (const MachineBasicBlock &MBB : MF) {
52c5015010STom Stellard     for (const MachineInstr &MI : MBB) {
53c5a154dbSTom Stellard       if (MI.getOpcode() == R600::KILLGT)
54c5015010STom Stellard         killPixel = true;
55c5015010STom Stellard       unsigned numOperands = MI.getNumOperands();
56c5015010STom Stellard       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
57c5015010STom Stellard         const MachineOperand &MO = MI.getOperand(op_idx);
58c5015010STom Stellard         if (!MO.isReg())
59c5015010STom Stellard           continue;
60c5015010STom Stellard         unsigned HWReg = RI->getHWRegIndex(MO.getReg());
61c5015010STom Stellard 
62c5015010STom Stellard         // Register with value > 127 aren't GPR
63c5015010STom Stellard         if (HWReg > 127)
64c5015010STom Stellard           continue;
65c5015010STom Stellard         MaxGPR = std::max(MaxGPR, HWReg);
66c5015010STom Stellard       }
67c5015010STom Stellard     }
68c5015010STom Stellard   }
69c5015010STom Stellard 
70c5015010STom Stellard   unsigned RsrcReg;
715bfbae5cSTom Stellard   if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
72c5015010STom Stellard     // Evergreen / Northern Islands
73c5015010STom Stellard     switch (MF.getFunction().getCallingConv()) {
74c5015010STom Stellard     default: LLVM_FALLTHROUGH;
75c5015010STom Stellard     case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
76c5015010STom Stellard     case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
77c5015010STom Stellard     case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
78c5015010STom Stellard     case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
79c5015010STom Stellard     }
80c5015010STom Stellard   } else {
81c5015010STom Stellard     // R600 / R700
82c5015010STom Stellard     switch (MF.getFunction().getCallingConv()) {
83c5015010STom Stellard     default: LLVM_FALLTHROUGH;
84c5015010STom Stellard     case CallingConv::AMDGPU_GS: LLVM_FALLTHROUGH;
85c5015010STom Stellard     case CallingConv::AMDGPU_CS: LLVM_FALLTHROUGH;
86c5015010STom Stellard     case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
87c5015010STom Stellard     case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
88c5015010STom Stellard     }
89c5015010STom Stellard   }
90c5015010STom Stellard 
91692e0c96SFangrui Song   OutStreamer->emitInt32(RsrcReg);
9277497103SFangrui Song   OutStreamer->emitIntValue(S_NUM_GPRS(MaxGPR + 1) |
93c5015010STom Stellard                            S_STACK_SIZE(MFI->CFStackSize), 4);
94692e0c96SFangrui Song   OutStreamer->emitInt32(R_02880C_DB_SHADER_CONTROL);
95692e0c96SFangrui Song   OutStreamer->emitInt32(S_02880C_KILL_ENABLE(killPixel));
96c5015010STom Stellard 
97c5015010STom Stellard   if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
98692e0c96SFangrui Song     OutStreamer->emitInt32(R_0288E8_SQ_LDS_ALLOC);
9977497103SFangrui Song     OutStreamer->emitIntValue(alignTo(MFI->getLDSSize(), 4) >> 2, 4);
100c5015010STom Stellard   }
101c5015010STom Stellard }
102c5015010STom Stellard 
runOnMachineFunction(MachineFunction & MF)103c5015010STom Stellard bool R600AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
104c5015010STom Stellard 
105f5016b79SJan Vesely 
106f5016b79SJan Vesely   // Functions needs to be cacheline (256B) aligned.
10718f805a7SGuillaume Chatelet   MF.ensureAlignment(Align(256));
108f5016b79SJan Vesely 
109c5015010STom Stellard   SetupMachineFunction(MF);
110c5015010STom Stellard 
111c5015010STom Stellard   MCContext &Context = getObjFileLowering().getContext();
112c5015010STom Stellard   MCSectionELF *ConfigSection =
113c5015010STom Stellard       Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
114*adf4142fSFangrui Song   OutStreamer->switchSection(ConfigSection);
115c5015010STom Stellard 
116c5015010STom Stellard   EmitProgramInfoR600(MF);
117c5015010STom Stellard 
1180dce409cSFangrui Song   emitFunctionBody();
119c5015010STom Stellard 
120c5015010STom Stellard   if (isVerbose()) {
121c5015010STom Stellard     MCSectionELF *CommentSection =
122c5015010STom Stellard         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
123*adf4142fSFangrui Song     OutStreamer->switchSection(CommentSection);
124c5015010STom Stellard 
125c5015010STom Stellard     R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
126c5015010STom Stellard     OutStreamer->emitRawComment(
127c5015010STom Stellard       Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->CFStackSize)));
128c5015010STom Stellard   }
129c5015010STom Stellard 
130c5015010STom Stellard   return false;
131c5015010STom Stellard }
132