blob: 83fa5d3e09a7c3039654810ced4cc47a1400da30 [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//===-- NVPTXAsmPrinter.h - NVPTX LLVM assembly writer --------------------===//
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// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to NVPTX assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
16#define LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
Justin Holewinskiae556d32012-05-04 20:18:50 +000017
18#include "NVPTX.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000019#include "NVPTXSubtarget.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000020#include "NVPTXTargetMachine.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/StringExtras.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000023#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/FormattedStream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000030#include "llvm/Target/TargetMachine.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000031#include <fstream>
32
33// The ptx syntax and format is very different from that usually seem in a .s
34// file,
35// therefore we are not able to use the MCAsmStreamer interface here.
36//
37// We are handcrafting the output method here.
38//
39// A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
40// (subclass of MCStreamer).
41
42// This is defined in AsmPrinter.cpp.
43// Used to process the constant expressions in initializers.
44namespace nvptx {
Justin Holewinski0497ab12013-03-30 14:29:21 +000045const llvm::MCExpr *
46LowerConstant(const llvm::Constant *CV, llvm::AsmPrinter &AP);
Justin Holewinskiae556d32012-05-04 20:18:50 +000047}
48
49namespace llvm {
50
51class LineReader {
52private:
Justin Holewinski0497ab12013-03-30 14:29:21 +000053 unsigned theCurLine;
Justin Holewinskiae556d32012-05-04 20:18:50 +000054 std::ifstream fstr;
55 char buff[512];
56 std::string theFileName;
57 SmallVector<unsigned, 32> lineOffset;
58public:
59 LineReader(std::string filename) {
60 theCurLine = 0;
61 fstr.open(filename.c_str());
62 theFileName = filename;
63 }
64 std::string fileName() { return theFileName; }
Justin Holewinski0497ab12013-03-30 14:29:21 +000065 ~LineReader() { fstr.close(); }
Justin Holewinskiae556d32012-05-04 20:18:50 +000066 std::string readLine(unsigned line);
67};
68
Justin Holewinskiae556d32012-05-04 20:18:50 +000069class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
70
Justin Holewinskiae556d32012-05-04 20:18:50 +000071 class AggBuffer {
72 // Used to buffer the emitted string for initializing global
73 // aggregates.
74 //
75 // Normally an aggregate (array, vector or structure) is emitted
76 // as a u8[]. However, if one element/field of the aggregate
77 // is a non-NULL address, then the aggregate is emitted as u32[]
78 // or u64[].
79 //
80 // We first layout the aggregate in 'buffer' in bytes, except for
81 // those symbol addresses. For the i-th symbol address in the
82 //aggregate, its corresponding 4-byte or 8-byte elements in 'buffer'
83 // are filled with 0s. symbolPosInBuffer[i-1] records its position
84 // in 'buffer', and Symbols[i-1] records the Value*.
85 //
86 // Once we have this AggBuffer setup, we can choose how to print
87 // it out.
88 public:
Justin Holewinskiae556d32012-05-04 20:18:50 +000089 unsigned numSymbols; // number of symbol addresses
Justin Holewinskiae556d32012-05-04 20:18:50 +000090
91 private:
Dylan Noblesmith802b6ce2014-08-25 01:59:29 +000092 const unsigned size; // size of the buffer in bytes
93 std::vector<unsigned char> buffer; // the buffer
94 SmallVector<unsigned, 4> symbolPosInBuffer;
95 SmallVector<const Value *, 4> Symbols;
Justin Holewinskiae556d32012-05-04 20:18:50 +000096 unsigned curpos;
97 raw_ostream &O;
98 NVPTXAsmPrinter &AP;
Justin Holewinski9d852a82014-04-09 15:39:11 +000099 bool EmitGeneric;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000100
101 public:
102 AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP)
Dylan Noblesmith802b6ce2014-08-25 01:59:29 +0000103 : size(_size), buffer(_size), O(_O), AP(_AP) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000104 curpos = 0;
105 numSymbols = 0;
Justin Holewinski9d852a82014-04-09 15:39:11 +0000106 EmitGeneric = AP.EmitGeneric;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000107 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000108 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000109 assert((curpos + Num) <= size);
110 assert((curpos + Bytes) <= size);
111 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000112 buffer[curpos] = Ptr[i];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000113 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000114 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000115 for (int i = Num; i < Bytes; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000116 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000117 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000118 }
119 return curpos;
120 }
121 unsigned addZeros(int Num) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000122 assert((curpos + Num) <= size);
123 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000124 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000125 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000126 }
127 return curpos;
128 }
Justin Holewinski01f89f02013-05-20 12:13:32 +0000129 void addSymbol(const Value *GVar) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000130 symbolPosInBuffer.push_back(curpos);
131 Symbols.push_back(GVar);
132 numSymbols++;
133 }
134 void print() {
135 if (numSymbols == 0) {
136 // print out in bytes
Justin Holewinski0497ab12013-03-30 14:29:21 +0000137 for (unsigned i = 0; i < size; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000138 if (i)
139 O << ", ";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000140 O << (unsigned int) buffer[i];
Justin Holewinskiae556d32012-05-04 20:18:50 +0000141 }
Craig Topperbdf39a42012-05-24 07:02:50 +0000142 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000143 // print out in 4-bytes or 8-bytes
144 unsigned int pos = 0;
145 unsigned int nSym = 0;
146 unsigned int nextSymbolPos = symbolPosInBuffer[nSym];
147 unsigned int nBytes = 4;
148 if (AP.nvptxSubtarget.is64Bit())
149 nBytes = 8;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000150 for (pos = 0; pos < size; pos += nBytes) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000151 if (pos)
152 O << ", ";
153 if (pos == nextSymbolPos) {
Justin Holewinski01f89f02013-05-20 12:13:32 +0000154 const Value *v = Symbols[nSym];
155 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
Rafael Espindola79858aa2013-10-29 17:07:16 +0000156 MCSymbol *Name = AP.getSymbol(GVar);
Justin Holewinski9d852a82014-04-09 15:39:11 +0000157 PointerType *PTy = dyn_cast<PointerType>(GVar->getType());
158 bool IsNonGenericPointer = false;
159 if (PTy && PTy->getAddressSpace() != 0) {
160 IsNonGenericPointer = true;
161 }
162 if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
163 O << "generic(";
164 O << *Name;
165 O << ")";
166 } else {
167 O << *Name;
168 }
Justin Holewinski01f89f02013-05-20 12:13:32 +0000169 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000170 O << *nvptx::LowerConstant(Cexpr, AP);
Craig Topperbdf39a42012-05-24 07:02:50 +0000171 } else
172 llvm_unreachable("symbol type unknown");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000173 nSym++;
174 if (nSym >= numSymbols)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000175 nextSymbolPos = size + 1;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000176 else
177 nextSymbolPos = symbolPosInBuffer[nSym];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000178 } else if (nBytes == 4)
Dylan Noblesmith802b6ce2014-08-25 01:59:29 +0000179 O << *(unsigned int *)(&buffer[pos]);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000180 else
Dylan Noblesmith802b6ce2014-08-25 01:59:29 +0000181 O << *(unsigned long long *)(&buffer[pos]);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000182 }
183 }
184 }
185 };
186
187 friend class AggBuffer;
188
Craig Topper2865c982014-04-29 07:57:44 +0000189 void emitSrcInText(StringRef filename, unsigned line);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000190
Justin Holewinski0497ab12013-03-30 14:29:21 +0000191private:
Craig Topper2865c982014-04-29 07:57:44 +0000192 const char *getPassName() const override { return "NVPTX Assembly Printer"; }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000193
194 const Function *F;
195 std::string CurrentFnName;
196
Craig Topper2865c982014-04-29 07:57:44 +0000197 void EmitFunctionEntryLabel() override;
198 void EmitFunctionBodyStart() override;
199 void EmitFunctionBodyEnd() override;
200 void emitImplicitDef(const MachineInstr *MI) const override;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000201
Craig Topper2865c982014-04-29 07:57:44 +0000202 void EmitInstruction(const MachineInstr *) override;
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000203 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
204 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
Justin Holewinski30d56a72014-04-09 15:39:15 +0000205 MCOperand GetSymbolRef(const MCSymbol *Symbol);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000206 unsigned encodeVirtualRegister(unsigned Reg);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000207
Craig Toppere73658d2014-04-28 04:05:08 +0000208 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = nullptr) const {}
Justin Holewinskiae556d32012-05-04 20:18:50 +0000209
Justin Holewinski0497ab12013-03-30 14:29:21 +0000210 void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
211 raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000212 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
Craig Toppere73658d2014-04-28 04:05:08 +0000213 const char *Modifier = nullptr);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000214 void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000215 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
216 bool = false);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000217 void printParamName(int paramIndex, raw_ostream &O);
218 void printParamName(Function::const_arg_iterator I, int paramIndex,
219 raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000220 void emitGlobals(const Module &M);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000221 void emitHeader(Module &M, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000222 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
Justin Holewinski660597d2013-10-11 12:39:36 +0000223 void emitVirtualRegister(unsigned int vr, raw_ostream &);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000224 void emitFunctionExternParamList(const MachineFunction &MF);
225 void emitFunctionParamList(const Function *, raw_ostream &O);
226 void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
227 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000228 void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000229 bool isImageType(const Type *Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000230 void printReturnValStr(const Function *, raw_ostream &O);
231 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +0000232 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
233 unsigned AsmVariant, const char *ExtraCode,
Craig Topper2865c982014-04-29 07:57:44 +0000234 raw_ostream &) override;
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +0000235 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
Craig Toppere73658d2014-04-28 04:05:08 +0000236 const char *Modifier = nullptr);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +0000237 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
238 unsigned AsmVariant, const char *ExtraCode,
Craig Topper2865c982014-04-29 07:57:44 +0000239 raw_ostream &) override;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000240protected:
Craig Topper2865c982014-04-29 07:57:44 +0000241 bool doInitialization(Module &M) override;
242 bool doFinalization(Module &M) override;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000243
244private:
245 std::string CurrentBankselLabelInBasicBlock;
246
Justin Holewinski01f89f02013-05-20 12:13:32 +0000247 bool GlobalsEmitted;
248
Justin Holewinskiae556d32012-05-04 20:18:50 +0000249 // This is specific per MachineFunction.
250 const MachineRegisterInfo *MRI;
251 // The contents are specific for each
252 // MachineFunction. But the size of the
253 // array is not.
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +0000254 typedef DenseMap<unsigned, unsigned> VRegMap;
255 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
256 VRegRCMap VRegMapping;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000257 // cache the subtarget here.
258 const NVPTXSubtarget &nvptxSubtarget;
259 // Build the map between type name and ID based on module's type
260 // symbol table.
261 std::map<const Type *, std::string> TypeNameMap;
262
263 // List of variables demoted to a function scope.
Justin Holewinski01f89f02013-05-20 12:13:32 +0000264 std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000265
266 // To record filename to ID mapping
267 std::map<std::string, unsigned> filenameMap;
268 void recordAndEmitFilenames(Module &);
269
270 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000271 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
272 std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000273 void printScalarConstant(const Constant *CPV, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000274 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000275 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
276 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000277
278 void printOperandProper(const MachineOperand &MO);
279
Justin Holewinski0497ab12013-03-30 14:29:21 +0000280 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000281 void emitDeclarations(const Module &, raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000282 void emitDeclaration(const Function *, raw_ostream &O);
283
284 static const char *getRegisterName(unsigned RegNo);
285 void emitDemotedVars(const Function *, raw_ostream &);
286
Justin Holewinski30d56a72014-04-09 15:39:15 +0000287 bool lowerImageHandleOperand(const MachineInstr *MI, unsigned OpNo,
288 MCOperand &MCOp);
289 void lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp);
290
Justin Holewinskiae556d32012-05-04 20:18:50 +0000291 LineReader *reader;
292 LineReader *getReader(std::string);
Justin Holewinski9d852a82014-04-09 15:39:11 +0000293
294 // Used to control the need to emit .generic() in the initializer of
295 // module scope variables.
296 // Although ptx supports the hybrid mode like the following,
297 // .global .u32 a;
298 // .global .u32 b;
299 // .global .u32 addr[] = {a, generic(b)}
300 // we have difficulty representing the difference in the NVVM IR.
301 //
302 // Since the address value should always be generic in CUDA C and always
303 // be specific in OpenCL, we use this simple control here.
304 //
305 bool EmitGeneric;
306
Justin Holewinskiae556d32012-05-04 20:18:50 +0000307public:
Justin Holewinski0497ab12013-03-30 14:29:21 +0000308 NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
309 : AsmPrinter(TM, Streamer),
310 nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000311 CurrentBankselLabelInBasicBlock = "";
Craig Toppere73658d2014-04-28 04:05:08 +0000312 reader = nullptr;
Justin Holewinski9d852a82014-04-09 15:39:11 +0000313 EmitGeneric = (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000314 }
315
316 ~NVPTXAsmPrinter() {
317 if (!reader)
318 delete reader;
319 }
320
321 bool ignoreLoc(const MachineInstr &);
322
Justin Holewinski660597d2013-10-11 12:39:36 +0000323 std::string getVirtualRegisterName(unsigned) const;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000324
325 DebugLoc prevDebugLoc;
326 void emitLineNumberAsDotLoc(const MachineInstr &);
327};
328} // end of namespace
329
330#endif