1 //===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- C++ -*-===//
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 /// \brief AMDGPU Assembly printer class.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
17 
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include <vector>
20 
21 namespace llvm {
22 
23 class AMDGPUAsmPrinter final : public AsmPrinter {
24 private:
25   struct SIProgramInfo {
26     SIProgramInfo() :
27       VGPRBlocks(0),
28       SGPRBlocks(0),
29       Priority(0),
30       FloatMode(0),
31       Priv(0),
32       DX10Clamp(0),
33       DebugMode(0),
34       IEEEMode(0),
35       ScratchSize(0),
36       ComputePGMRSrc1(0),
37       LDSBlocks(0),
38       ScratchBlocks(0),
39       ComputePGMRSrc2(0),
40       NumVGPR(0),
41       NumSGPR(0),
42       FlatUsed(false),
43       ReservedVGPRFirst(0),
44       ReservedVGPRCount(0),
45       VCCUsed(false),
46       CodeLen(0) {}
47 
48     // Fields set in PGM_RSRC1 pm4 packet.
49     uint32_t VGPRBlocks;
50     uint32_t SGPRBlocks;
51     uint32_t Priority;
52     uint32_t FloatMode;
53     uint32_t Priv;
54     uint32_t DX10Clamp;
55     uint32_t DebugMode;
56     uint32_t IEEEMode;
57     uint32_t ScratchSize;
58 
59     uint64_t ComputePGMRSrc1;
60 
61     // Fields set in PGM_RSRC2 pm4 packet.
62     uint32_t LDSBlocks;
63     uint32_t ScratchBlocks;
64 
65     uint64_t ComputePGMRSrc2;
66 
67     uint32_t NumVGPR;
68     uint32_t NumSGPR;
69     uint32_t LDSSize;
70     bool FlatUsed;
71 
72     // If ReservedVGPRCount is 0 then must be 0. Otherwise, this is the first
73     // fixed VGPR number reserved.
74     uint16_t ReservedVGPRFirst;
75     // The number of consecutive VGPRs reserved.
76     uint16_t ReservedVGPRCount;
77 
78     // Bonus information for debugging.
79     bool VCCUsed;
80     uint64_t CodeLen;
81   };
82 
83   void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF) const;
84   void findNumUsedRegistersSI(const MachineFunction &MF,
85                               unsigned &NumSGPR,
86                               unsigned &NumVGPR) const;
87 
88   /// \brief Emit register usage information so that the GPU driver
89   /// can correctly setup the GPU state.
90   void EmitProgramInfoR600(const MachineFunction &MF);
91   void EmitProgramInfoSI(const MachineFunction &MF, const SIProgramInfo &KernelInfo);
92   void EmitAmdKernelCodeT(const MachineFunction &MF,
93                           const SIProgramInfo &KernelInfo) const;
94 
95 public:
96   explicit AMDGPUAsmPrinter(TargetMachine &TM,
97                             std::unique_ptr<MCStreamer> Streamer);
98 
99   bool runOnMachineFunction(MachineFunction &MF) override;
100 
101   const char *getPassName() const override {
102     return "AMDGPU Assembly Printer";
103   }
104 
105   /// Implemented in AMDGPUMCInstLower.cpp
106   void EmitInstruction(const MachineInstr *MI) override;
107 
108   void EmitFunctionBodyStart() override;
109 
110   void EmitFunctionEntryLabel() override;
111 
112   void EmitGlobalVariable(const GlobalVariable *GV) override;
113 
114   void EmitStartOfAsmFile(Module &M) override;
115 
116   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
117                        unsigned AsmVariant, const char *ExtraCode,
118                        raw_ostream &O) override;
119 
120 protected:
121   std::vector<std::string> DisasmLines, HexLines;
122   size_t DisasmLineMaxLen;
123 };
124 
125 } // End anonymous llvm
126 
127 #endif
128