blob: 9a9c33b6f52d43cdbaddc001b29c673e85393432 [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
15#ifndef NVPTXASMPRINTER_H
16#define NVPTXASMPRINTER_H
17
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 Holewinski0497ab12013-03-30 14:29:21 +000089 unsigned size; // size of the buffer in bytes
Justin Holewinskiae556d32012-05-04 20:18:50 +000090 unsigned char *buffer; // the buffer
91 unsigned numSymbols; // number of symbol addresses
92 SmallVector<unsigned, 4> symbolPosInBuffer;
Justin Holewinski01f89f02013-05-20 12:13:32 +000093 SmallVector<const Value *, 4> Symbols;
Justin Holewinskiae556d32012-05-04 20:18:50 +000094
95 private:
96 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)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000103 : O(_O), AP(_AP) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000104 buffer = new unsigned char[_size];
105 size = _size;
106 curpos = 0;
107 numSymbols = 0;
Justin Holewinski9d852a82014-04-09 15:39:11 +0000108 EmitGeneric = AP.EmitGeneric;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000109 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000110 ~AggBuffer() { delete[] buffer; }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000111 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000112 assert((curpos + Num) <= size);
113 assert((curpos + Bytes) <= size);
114 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000115 buffer[curpos] = Ptr[i];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000116 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000117 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000118 for (int i = Num; i < Bytes; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000119 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000120 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000121 }
122 return curpos;
123 }
124 unsigned addZeros(int Num) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000125 assert((curpos + Num) <= size);
126 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000127 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000128 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000129 }
130 return curpos;
131 }
Justin Holewinski01f89f02013-05-20 12:13:32 +0000132 void addSymbol(const Value *GVar) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000133 symbolPosInBuffer.push_back(curpos);
134 Symbols.push_back(GVar);
135 numSymbols++;
136 }
137 void print() {
138 if (numSymbols == 0) {
139 // print out in bytes
Justin Holewinski0497ab12013-03-30 14:29:21 +0000140 for (unsigned i = 0; i < size; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000141 if (i)
142 O << ", ";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000143 O << (unsigned int) buffer[i];
Justin Holewinskiae556d32012-05-04 20:18:50 +0000144 }
Craig Topperbdf39a42012-05-24 07:02:50 +0000145 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000146 // print out in 4-bytes or 8-bytes
147 unsigned int pos = 0;
148 unsigned int nSym = 0;
149 unsigned int nextSymbolPos = symbolPosInBuffer[nSym];
150 unsigned int nBytes = 4;
151 if (AP.nvptxSubtarget.is64Bit())
152 nBytes = 8;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000153 for (pos = 0; pos < size; pos += nBytes) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000154 if (pos)
155 O << ", ";
156 if (pos == nextSymbolPos) {
Justin Holewinski01f89f02013-05-20 12:13:32 +0000157 const Value *v = Symbols[nSym];
158 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
Rafael Espindola79858aa2013-10-29 17:07:16 +0000159 MCSymbol *Name = AP.getSymbol(GVar);
Justin Holewinski9d852a82014-04-09 15:39:11 +0000160 PointerType *PTy = dyn_cast<PointerType>(GVar->getType());
161 bool IsNonGenericPointer = false;
162 if (PTy && PTy->getAddressSpace() != 0) {
163 IsNonGenericPointer = true;
164 }
165 if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
166 O << "generic(";
167 O << *Name;
168 O << ")";
169 } else {
170 O << *Name;
171 }
Justin Holewinski01f89f02013-05-20 12:13:32 +0000172 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000173 O << *nvptx::LowerConstant(Cexpr, AP);
Craig Topperbdf39a42012-05-24 07:02:50 +0000174 } else
175 llvm_unreachable("symbol type unknown");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000176 nSym++;
177 if (nSym >= numSymbols)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000178 nextSymbolPos = size + 1;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000179 else
180 nextSymbolPos = symbolPosInBuffer[nSym];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000181 } else if (nBytes == 4)
182 O << *(unsigned int *)(buffer + pos);
183 else
184 O << *(unsigned long long *)(buffer + pos);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000185 }
186 }
187 }
188 };
189
190 friend class AggBuffer;
191
192 virtual void emitSrcInText(StringRef filename, unsigned line);
193
Justin Holewinski0497ab12013-03-30 14:29:21 +0000194private:
195 virtual const char *getPassName() const { return "NVPTX Assembly Printer"; }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000196
197 const Function *F;
198 std::string CurrentFnName;
199
200 void EmitFunctionEntryLabel();
201 void EmitFunctionBodyStart();
202 void EmitFunctionBodyEnd();
Justin Holewinski660597d2013-10-11 12:39:36 +0000203 void emitImplicitDef(const MachineInstr *MI) const;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000204
205 void EmitInstruction(const MachineInstr *);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000206 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
207 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
208 MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol);
209 unsigned encodeVirtualRegister(unsigned Reg);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000210
211 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const {}
212
Justin Holewinski0497ab12013-03-30 14:29:21 +0000213 void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
214 raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000215 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
Justin Holewinski0497ab12013-03-30 14:29:21 +0000216 const char *Modifier = 0);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000217 void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000218 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
219 bool = false);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000220 void printParamName(int paramIndex, raw_ostream &O);
221 void printParamName(Function::const_arg_iterator I, int paramIndex,
222 raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000223 void emitGlobals(const Module &M);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000224 void emitHeader(Module &M, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000225 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
Justin Holewinski660597d2013-10-11 12:39:36 +0000226 void emitVirtualRegister(unsigned int vr, raw_ostream &);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000227 void emitFunctionExternParamList(const MachineFunction &MF);
228 void emitFunctionParamList(const Function *, raw_ostream &O);
229 void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
230 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000231 void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000232 bool isImageType(const Type *Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000233 void printReturnValStr(const Function *, raw_ostream &O);
234 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +0000235 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
236 unsigned AsmVariant, const char *ExtraCode,
237 raw_ostream &);
238 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
239 const char *Modifier = 0);
240 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
241 unsigned AsmVariant, const char *ExtraCode,
242 raw_ostream &);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000243protected:
244 bool doInitialization(Module &M);
245 bool doFinalization(Module &M);
246
247private:
248 std::string CurrentBankselLabelInBasicBlock;
249
Justin Holewinski01f89f02013-05-20 12:13:32 +0000250 bool GlobalsEmitted;
251
Justin Holewinskiae556d32012-05-04 20:18:50 +0000252 // This is specific per MachineFunction.
253 const MachineRegisterInfo *MRI;
254 // The contents are specific for each
255 // MachineFunction. But the size of the
256 // array is not.
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +0000257 typedef DenseMap<unsigned, unsigned> VRegMap;
258 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
259 VRegRCMap VRegMapping;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000260 // cache the subtarget here.
261 const NVPTXSubtarget &nvptxSubtarget;
262 // Build the map between type name and ID based on module's type
263 // symbol table.
264 std::map<const Type *, std::string> TypeNameMap;
265
266 // List of variables demoted to a function scope.
Justin Holewinski01f89f02013-05-20 12:13:32 +0000267 std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000268
269 // To record filename to ID mapping
270 std::map<std::string, unsigned> filenameMap;
271 void recordAndEmitFilenames(Module &);
272
273 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000274 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
275 std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000276 void printScalarConstant(const Constant *CPV, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000277 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000278 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
279 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000280
281 void printOperandProper(const MachineOperand &MO);
282
Justin Holewinski0497ab12013-03-30 14:29:21 +0000283 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000284 void emitDeclarations(const Module &, raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000285 void emitDeclaration(const Function *, raw_ostream &O);
286
287 static const char *getRegisterName(unsigned RegNo);
288 void emitDemotedVars(const Function *, raw_ostream &);
289
290 LineReader *reader;
291 LineReader *getReader(std::string);
Justin Holewinski9d852a82014-04-09 15:39:11 +0000292
293 // Used to control the need to emit .generic() in the initializer of
294 // module scope variables.
295 // Although ptx supports the hybrid mode like the following,
296 // .global .u32 a;
297 // .global .u32 b;
298 // .global .u32 addr[] = {a, generic(b)}
299 // we have difficulty representing the difference in the NVVM IR.
300 //
301 // Since the address value should always be generic in CUDA C and always
302 // be specific in OpenCL, we use this simple control here.
303 //
304 bool EmitGeneric;
305
Justin Holewinskiae556d32012-05-04 20:18:50 +0000306public:
Justin Holewinski0497ab12013-03-30 14:29:21 +0000307 NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
308 : AsmPrinter(TM, Streamer),
309 nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000310 CurrentBankselLabelInBasicBlock = "";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000311 reader = NULL;
Justin Holewinski9d852a82014-04-09 15:39:11 +0000312 EmitGeneric = (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000313 }
314
315 ~NVPTXAsmPrinter() {
316 if (!reader)
317 delete reader;
318 }
319
320 bool ignoreLoc(const MachineInstr &);
321
Justin Holewinski660597d2013-10-11 12:39:36 +0000322 std::string getVirtualRegisterName(unsigned) const;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000323
324 DebugLoc prevDebugLoc;
325 void emitLineNumberAsDotLoc(const MachineInstr &);
326};
327} // end of namespace
328
329#endif