blob: 9f6a63536493b802d5ce7735e8c41219272daa8f [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/StringExtras.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000022#include "llvm/CodeGen/AsmPrinter.h"
Benjamin Kramer391be792016-01-27 19:29:56 +000023#include "llvm/CodeGen/MachineLoopInfo.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"
Benjamin Kramer391be792016-01-27 19:29:56 +000027#include "llvm/MC/MCStreamer.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000028#include "llvm/MC/MCSymbol.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000029#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
Justin Holewinskiae556d32012-05-04 20:18:50 +000042namespace llvm {
Pete Cooper3de83e42015-05-15 21:58:42 +000043 class MCOperand;
Justin Holewinskiae556d32012-05-04 20:18:50 +000044
45class LineReader {
46private:
Justin Holewinski0497ab12013-03-30 14:29:21 +000047 unsigned theCurLine;
Justin Holewinskiae556d32012-05-04 20:18:50 +000048 std::ifstream fstr;
49 char buff[512];
50 std::string theFileName;
51 SmallVector<unsigned, 32> lineOffset;
52public:
53 LineReader(std::string filename) {
54 theCurLine = 0;
55 fstr.open(filename.c_str());
56 theFileName = filename;
57 }
58 std::string fileName() { return theFileName; }
Justin Holewinski0497ab12013-03-30 14:29:21 +000059 ~LineReader() { fstr.close(); }
Justin Holewinskiae556d32012-05-04 20:18:50 +000060 std::string readLine(unsigned line);
61};
62
Justin Holewinskiae556d32012-05-04 20:18:50 +000063class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
64
Justin Holewinskiae556d32012-05-04 20:18:50 +000065 class AggBuffer {
66 // Used to buffer the emitted string for initializing global
67 // aggregates.
68 //
69 // Normally an aggregate (array, vector or structure) is emitted
70 // as a u8[]. However, if one element/field of the aggregate
71 // is a non-NULL address, then the aggregate is emitted as u32[]
72 // or u64[].
73 //
74 // We first layout the aggregate in 'buffer' in bytes, except for
75 // those symbol addresses. For the i-th symbol address in the
76 //aggregate, its corresponding 4-byte or 8-byte elements in 'buffer'
77 // are filled with 0s. symbolPosInBuffer[i-1] records its position
78 // in 'buffer', and Symbols[i-1] records the Value*.
79 //
80 // Once we have this AggBuffer setup, we can choose how to print
81 // it out.
82 public:
Justin Holewinskiae556d32012-05-04 20:18:50 +000083 unsigned numSymbols; // number of symbol addresses
Justin Holewinskiae556d32012-05-04 20:18:50 +000084
85 private:
Dylan Noblesmith802b6ce2014-08-25 01:59:29 +000086 const unsigned size; // size of the buffer in bytes
87 std::vector<unsigned char> buffer; // the buffer
88 SmallVector<unsigned, 4> symbolPosInBuffer;
89 SmallVector<const Value *, 4> Symbols;
Jingyue Wu312fd022015-04-24 02:57:30 +000090 // SymbolsBeforeStripping[i] is the original form of Symbols[i] before
91 // stripping pointer casts, i.e.,
92 // Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts().
93 //
94 // We need to keep these values because AggBuffer::print decides whether to
95 // emit a "generic()" cast for Symbols[i] depending on the address space of
96 // SymbolsBeforeStripping[i].
97 SmallVector<const Value *, 4> SymbolsBeforeStripping;
Justin Holewinskiae556d32012-05-04 20:18:50 +000098 unsigned curpos;
99 raw_ostream &O;
100 NVPTXAsmPrinter &AP;
Justin Holewinski9d852a82014-04-09 15:39:11 +0000101 bool EmitGeneric;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000102
103 public:
David Blaikie9f380a32015-03-16 18:06:57 +0000104 AggBuffer(unsigned size, raw_ostream &O, NVPTXAsmPrinter &AP)
105 : size(size), buffer(size), O(O), AP(AP) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000106 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 Holewinskiae556d32012-05-04 20:18:50 +0000110 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000111 assert((curpos + Num) <= size);
112 assert((curpos + Bytes) <= size);
113 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000114 buffer[curpos] = Ptr[i];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000115 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000116 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000117 for (int i = Num; i < Bytes; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000118 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000119 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000120 }
121 return curpos;
122 }
123 unsigned addZeros(int Num) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000124 assert((curpos + Num) <= size);
125 for (int i = 0; i < Num; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000126 buffer[curpos] = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000127 curpos++;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000128 }
129 return curpos;
130 }
Jingyue Wu312fd022015-04-24 02:57:30 +0000131 void addSymbol(const Value *GVar, const Value *GVarBeforeStripping) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000132 symbolPosInBuffer.push_back(curpos);
133 Symbols.push_back(GVar);
Jingyue Wu312fd022015-04-24 02:57:30 +0000134 SymbolsBeforeStripping.push_back(GVarBeforeStripping);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000135 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;
Eric Christopher6aad8b12015-02-19 00:08:14 +0000151 if (static_cast<const NVPTXTargetMachine &>(AP.TM).is64Bit())
Justin Holewinskiae556d32012-05-04 20:18:50 +0000152 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];
Jingyue Wu312fd022015-04-24 02:57:30 +0000158 const Value *v0 = SymbolsBeforeStripping[nSym];
Justin Holewinski01f89f02013-05-20 12:13:32 +0000159 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
Rafael Espindola79858aa2013-10-29 17:07:16 +0000160 MCSymbol *Name = AP.getSymbol(GVar);
Jingyue Wu312fd022015-04-24 02:57:30 +0000161 PointerType *PTy = dyn_cast<PointerType>(v0->getType());
162 bool IsNonGenericPointer = false; // Is v0 a non-generic pointer?
Justin Holewinski9d852a82014-04-09 15:39:11 +0000163 if (PTy && PTy->getAddressSpace() != 0) {
164 IsNonGenericPointer = true;
165 }
166 if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
167 O << "generic(";
Matt Arsenault8b643552015-06-09 00:31:39 +0000168 Name->print(O, AP.MAI);
Justin Holewinski9d852a82014-04-09 15:39:11 +0000169 O << ")";
170 } else {
Matt Arsenault8b643552015-06-09 00:31:39 +0000171 Name->print(O, AP.MAI);
Justin Holewinski9d852a82014-04-09 15:39:11 +0000172 }
Justin Holewinski3d2a9762015-04-28 17:18:30 +0000173 } else if (const ConstantExpr *CExpr = dyn_cast<ConstantExpr>(v0)) {
174 const MCExpr *Expr =
175 AP.lowerConstantForGV(cast<Constant>(CExpr), false);
176 AP.printMCExpr(*Expr, O);
Craig Topperbdf39a42012-05-24 07:02:50 +0000177 } else
178 llvm_unreachable("symbol type unknown");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000179 nSym++;
180 if (nSym >= numSymbols)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000181 nextSymbolPos = size + 1;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000182 else
183 nextSymbolPos = symbolPosInBuffer[nSym];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000184 } else if (nBytes == 4)
Dylan Noblesmith802b6ce2014-08-25 01:59:29 +0000185 O << *(unsigned int *)(&buffer[pos]);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000186 else
Dylan Noblesmith802b6ce2014-08-25 01:59:29 +0000187 O << *(unsigned long long *)(&buffer[pos]);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000188 }
189 }
190 }
191 };
192
193 friend class AggBuffer;
194
Craig Topper2865c982014-04-29 07:57:44 +0000195 void emitSrcInText(StringRef filename, unsigned line);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000196
Justin Holewinski0497ab12013-03-30 14:29:21 +0000197private:
Craig Topper2865c982014-04-29 07:57:44 +0000198 const char *getPassName() const override { return "NVPTX Assembly Printer"; }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000199
200 const Function *F;
201 std::string CurrentFnName;
202
Jingyue Wu0220df02015-02-01 02:27:45 +0000203 void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override;
Craig Topper2865c982014-04-29 07:57:44 +0000204 void EmitFunctionEntryLabel() override;
205 void EmitFunctionBodyStart() override;
206 void EmitFunctionBodyEnd() override;
207 void emitImplicitDef(const MachineInstr *MI) const override;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000208
Craig Topper2865c982014-04-29 07:57:44 +0000209 void EmitInstruction(const MachineInstr *) override;
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000210 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
211 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
Justin Holewinski30d56a72014-04-09 15:39:15 +0000212 MCOperand GetSymbolRef(const MCSymbol *Symbol);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000213 unsigned encodeVirtualRegister(unsigned Reg);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000214
Justin Holewinski0497ab12013-03-30 14:29:21 +0000215 void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
216 raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000217 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
Craig Toppere73658d2014-04-28 04:05:08 +0000218 const char *Modifier = nullptr);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000219 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
220 bool = false);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000221 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);
Eric Christopher6aad8b12015-02-19 00:08:14 +0000224 void emitHeader(Module &M, raw_ostream &O, const NVPTXSubtarget &STI);
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 emitFunctionParamList(const Function *, raw_ostream &O);
228 void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
229 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
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 Holewinski3d2a9762015-04-28 17:18:30 +0000240
241 const MCExpr *lowerConstantForGV(const Constant *CV, bool ProcessingGeneric);
242 void printMCExpr(const MCExpr &Expr, raw_ostream &OS);
243
Justin Holewinskiae556d32012-05-04 20:18:50 +0000244protected:
Craig Topper2865c982014-04-29 07:57:44 +0000245 bool doInitialization(Module &M) override;
246 bool doFinalization(Module &M) override;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000247
248private:
249 std::string CurrentBankselLabelInBasicBlock;
250
Justin Holewinski01f89f02013-05-20 12:13:32 +0000251 bool GlobalsEmitted;
252
Justin Holewinskiae556d32012-05-04 20:18:50 +0000253 // This is specific per MachineFunction.
254 const MachineRegisterInfo *MRI;
255 // The contents are specific for each
256 // MachineFunction. But the size of the
257 // array is not.
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +0000258 typedef DenseMap<unsigned, unsigned> VRegMap;
259 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
260 VRegRCMap VRegMapping;
Eric Christopher6aad8b12015-02-19 00:08:14 +0000261
262 // Cache the subtarget here.
263 const NVPTXSubtarget *nvptxSubtarget;
264
Justin Holewinskiae556d32012-05-04 20:18:50 +0000265 // Build the map between type name and ID based on module's type
266 // symbol table.
Craig Toppere3dcce92015-08-01 22:20:21 +0000267 std::map<Type *, std::string> TypeNameMap;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000268
269 // List of variables demoted to a function scope.
Justin Holewinski01f89f02013-05-20 12:13:32 +0000270 std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000271
272 // To record filename to ID mapping
273 std::map<std::string, unsigned> filenameMap;
274 void recordAndEmitFilenames(Module &);
275
276 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000277 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
Craig Toppere3dcce92015-08-01 22:20:21 +0000278 std::string getPTXFundamentalTypeStr(Type *Ty, bool = true) const;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000279 void printScalarConstant(const Constant *CPV, raw_ostream &O);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000280 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000281 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
282 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000283
Justin Holewinski0497ab12013-03-30 14:29:21 +0000284 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
Justin Holewinski01f89f02013-05-20 12:13:32 +0000285 void emitDeclarations(const Module &, raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000286 void emitDeclaration(const Function *, raw_ostream &O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000287 void emitDemotedVars(const Function *, raw_ostream &);
288
Justin Holewinski30d56a72014-04-09 15:39:15 +0000289 bool lowerImageHandleOperand(const MachineInstr *MI, unsigned OpNo,
290 MCOperand &MCOp);
291 void lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp);
292
Jingyue Wu0220df02015-02-01 02:27:45 +0000293 bool isLoopHeaderOfNoUnroll(const MachineBasicBlock &MBB) const;
294
Justin Holewinskiae556d32012-05-04 20:18:50 +0000295 LineReader *reader;
296 LineReader *getReader(std::string);
Justin Holewinski9d852a82014-04-09 15:39:11 +0000297
298 // Used to control the need to emit .generic() in the initializer of
299 // module scope variables.
300 // Although ptx supports the hybrid mode like the following,
301 // .global .u32 a;
302 // .global .u32 b;
303 // .global .u32 addr[] = {a, generic(b)}
304 // we have difficulty representing the difference in the NVVM IR.
305 //
306 // Since the address value should always be generic in CUDA C and always
307 // be specific in OpenCL, we use this simple control here.
308 //
309 bool EmitGeneric;
310
Justin Holewinskiae556d32012-05-04 20:18:50 +0000311public:
David Blaikie94598322015-01-18 20:29:04 +0000312 NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
313 : AsmPrinter(TM, std::move(Streamer)),
Eric Christopher6aad8b12015-02-19 00:08:14 +0000314 EmitGeneric(static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() ==
315 NVPTX::CUDA) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000316 CurrentBankselLabelInBasicBlock = "";
Craig Toppere73658d2014-04-28 04:05:08 +0000317 reader = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000318 }
319
320 ~NVPTXAsmPrinter() {
321 if (!reader)
322 delete reader;
323 }
324
Eric Christopher6aad8b12015-02-19 00:08:14 +0000325 bool runOnMachineFunction(MachineFunction &F) override {
326 nvptxSubtarget = &F.getSubtarget<NVPTXSubtarget>();
327 return AsmPrinter::runOnMachineFunction(F);
328 }
Jingyue Wu0220df02015-02-01 02:27:45 +0000329 void getAnalysisUsage(AnalysisUsage &AU) const override {
330 AU.addRequired<MachineLoopInfo>();
331 AsmPrinter::getAnalysisUsage(AU);
332 }
333
Justin Holewinskiae556d32012-05-04 20:18:50 +0000334 bool ignoreLoc(const MachineInstr &);
335
Justin Holewinski660597d2013-10-11 12:39:36 +0000336 std::string getVirtualRegisterName(unsigned) const;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000337
338 DebugLoc prevDebugLoc;
339 void emitLineNumberAsDotLoc(const MachineInstr &);
340};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000341} // end of namespace
Justin Holewinskiae556d32012-05-04 20:18:50 +0000342
343#endif