blob: a6059974ab3d41f3d263af8dfbe6da4efa64bd6a [file] [log] [blame]
Nick Lewyckyf7a3c502010-09-07 18:14:24 +00001//===-- PTXAsmPrinter.cpp - PTX 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 PTX assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Che-Liang Chioudf659632010-11-08 03:06:08 +000015#define DEBUG_TYPE "ptx-asm-printer"
16
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000017#include "PTX.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000018#include "PTXMachineFunctionInfo.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000019#include "PTXTargetMachine.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
Eric Christopher50880d02010-09-18 18:52:28 +000022#include "llvm/ADT/SmallString.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Twine.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000025#include "llvm/CodeGen/AsmPrinter.h"
Eric Christopher50880d02010-09-18 18:52:28 +000026#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000028#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000029#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000031#include "llvm/Target/TargetRegistry.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000032#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000033#include "llvm/Support/Debug.h"
34#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000035#include "llvm/Support/MathExtras.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000037
38using namespace llvm;
39
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000040static cl::opt<std::string>
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000041OptPTXVersion("ptx-version", cl::desc("Set PTX version"), cl::init("1.4"));
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000042
43static cl::opt<std::string>
44OptPTXTarget("ptx-target", cl::desc("Set GPU target (comma-separated list)"),
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000045 cl::init("sm_10"));
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000046
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000047namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000048class PTXAsmPrinter : public AsmPrinter {
49public:
50 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
51 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000052
Che-Liang Chioudf659632010-11-08 03:06:08 +000053 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000054
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000055 bool doFinalization(Module &M);
56
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000057 virtual void EmitStartOfAsmFile(Module &M);
58
Che-Liang Chioudf659632010-11-08 03:06:08 +000059 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000060
Che-Liang Chioudf659632010-11-08 03:06:08 +000061 virtual void EmitFunctionBodyStart();
62 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
63
64 virtual void EmitInstruction(const MachineInstr *MI);
65
66 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000067 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
68 const char *Modifier = 0);
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000069 void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
70 const char *Modifier = 0);
Che-Liang Chioudf659632010-11-08 03:06:08 +000071
72 // autogen'd.
73 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
74 static const char *getRegisterName(unsigned RegNo);
75
76private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000077 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000078 void EmitFunctionDeclaration();
79}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000080} // namespace
81
Che-Liang Chioudf659632010-11-08 03:06:08 +000082static const char PARAM_PREFIX[] = "__param_";
83
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000084static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioudf659632010-11-08 03:06:08 +000085#define TEST_REGCLS(cls, clsstr) \
86 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000087 TEST_REGCLS(RRegs32, s32);
88 TEST_REGCLS(Preds, pred);
Che-Liang Chioudf659632010-11-08 03:06:08 +000089#undef TEST_REGCLS
90
91 llvm_unreachable("Not in any register class!");
92 return NULL;
93}
94
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000095static const char *getInstructionTypeName(const MachineInstr *MI) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000096 for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
97 const MachineOperand &MO = MI->getOperand(i);
98 if (MO.getType() == MachineOperand::MO_Register)
99 return getRegisterTypeName(MO.getReg());
100 }
101
102 llvm_unreachable("No reg operand found in instruction!");
103 return NULL;
104}
105
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000106static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +0000107 switch (addressSpace) {
108 default: llvm_unreachable("Unknown state space");
109 case PTX::GLOBAL: return "global";
110 case PTX::CONSTANT: return "const";
111 case PTX::LOCAL: return "local";
112 case PTX::PARAMETER: return "param";
113 case PTX::SHARED: return "shared";
114 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000115 return NULL;
116}
117
118bool PTXAsmPrinter::doFinalization(Module &M) {
119 // XXX Temproarily remove global variables so that doFinalization() will not
120 // emit them again (global variables are emitted at beginning).
121
122 Module::GlobalListType &global_list = M.getGlobalList();
123 int i, n = global_list.size();
124 GlobalVariable **gv_array = new GlobalVariable* [n];
125
126 // first, back-up GlobalVariable in gv_array
127 i = 0;
128 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
129 I != E; ++I)
130 gv_array[i++] = &*I;
131
132 // second, empty global_list
133 while (!global_list.empty())
134 global_list.remove(global_list.begin());
135
136 // call doFinalization
137 bool ret = AsmPrinter::doFinalization(M);
138
139 // now we restore global variables
140 for (i = 0; i < n; i ++)
141 global_list.insert(global_list.end(), gv_array[i]);
142
143 delete[] gv_array;
144 return ret;
145}
146
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000147void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
148{
149 OutStreamer.EmitRawText(Twine("\t.version " + OptPTXVersion));
150 OutStreamer.EmitRawText(Twine("\t.target " + OptPTXTarget));
151 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000152
153 // declare global variables
154 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
155 i != e; ++i)
156 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000157}
158
Che-Liang Chioudf659632010-11-08 03:06:08 +0000159bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
160 SetupMachineFunction(MF);
161 EmitFunctionDeclaration();
162 EmitFunctionBody();
163 return false;
164}
165
166void PTXAsmPrinter::EmitFunctionBodyStart() {
167 OutStreamer.EmitRawText(Twine("{"));
168
169 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
170
171 // Print local variable definition
172 for (PTXMachineFunctionInfo::reg_iterator
173 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
174 unsigned reg = *i;
175
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000176 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000177 def += getRegisterTypeName(reg);
178 def += ' ';
179 def += getRegisterName(reg);
180 def += ';';
181 OutStreamer.EmitRawText(Twine(def));
182 }
183}
184
Eric Christopher50880d02010-09-18 18:52:28 +0000185void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000186 std::string str;
187 str.reserve(64);
188
189 // Write instruction to str
190 raw_string_ostream OS(str);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000191 printInstruction(MI, OS);
192 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000193 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000194
195 // Replace "%type" if found
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000196 size_t pos;
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000197 if ((pos = str.find("%type")) != std::string::npos)
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000198 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000199
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000200 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000201 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000202}
203
204void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
205 raw_ostream &OS) {
206 const MachineOperand &MO = MI->getOperand(opNum);
207
208 switch (MO.getType()) {
209 default:
210 llvm_unreachable("<unknown operand type>");
211 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000212 case MachineOperand::MO_GlobalAddress:
213 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000214 break;
215 case MachineOperand::MO_Immediate:
216 OS << (int) MO.getImm();
217 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000218 case MachineOperand::MO_Register:
219 OS << getRegisterName(MO.getReg());
220 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000221 }
Eric Christopher50880d02010-09-18 18:52:28 +0000222}
223
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000224void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
225 raw_ostream &OS, const char *Modifier) {
226 printOperand(MI, opNum, OS);
227
228 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
229 return; // don't print "+0"
230
231 OS << "+";
232 printOperand(MI, opNum+1, OS);
233}
234
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000235void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
236 raw_ostream &OS, const char *Modifier) {
237 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
238}
239
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000240void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
241 // Check to see if this is a special global used by LLVM, if so, emit it.
242 if (EmitSpecialLLVMGlobal(gv))
243 return;
244
245 MCSymbol *gvsym = Mang->getSymbol(gv);
246
247 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
248
249 std::string decl;
250
251 // check if it is defined in some other translation unit
252 if (gv->isDeclaration())
253 decl += ".extern ";
254
255 // state space: e.g., .global
256 decl += ".";
257 decl += getStateSpaceName(gv->getType()->getAddressSpace());
258 decl += " ";
259
260 // alignment (optional)
261 unsigned alignment = gv->getAlignment();
262 if (alignment != 0) {
263 decl += ".align ";
264 decl += utostr(Log2_32(gv->getAlignment()));
265 decl += " ";
266 }
267
268 // TODO: add types
269 decl += ".s32 ";
270
271 decl += gvsym->getName();
272
273 if (ArrayType::classof(gv->getType()) || PointerType::classof(gv->getType()))
274 decl += "[]";
275
276 decl += ";";
277
278 OutStreamer.EmitRawText(Twine(decl));
279
280 OutStreamer.AddBlankLine();
281}
282
Che-Liang Chioudf659632010-11-08 03:06:08 +0000283void PTXAsmPrinter::EmitFunctionDeclaration() {
284 // The function label could have already been emitted if two symbols end up
285 // conflicting due to asm renaming. Detect this and emit an error.
286 if (!CurrentFnSym->isUndefined()) {
287 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
288 "' label emitted multiple times to assembly file");
289 return;
290 }
291
292 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
293 const bool isKernel = MFI->isKernel();
294 unsigned reg;
295
296 std::string decl = isKernel ? ".entry" : ".func";
297
298 // Print return register
299 reg = MFI->retReg();
300 if (!isKernel && reg != PTX::NoRegister) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000301 decl += " (.reg ."; // FIXME: could it return in .param space?
Che-Liang Chioudf659632010-11-08 03:06:08 +0000302 decl += getRegisterTypeName(reg);
303 decl += " ";
304 decl += getRegisterName(reg);
305 decl += ")";
306 }
307
308 // Print function name
309 decl += " ";
310 decl += CurrentFnSym->getName().str();
311
312 // Print parameter list
313 if (!MFI->argRegEmpty()) {
314 decl += " (";
315 if (isKernel) {
316 for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
317 if (i != 0)
318 decl += ", ";
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000319 decl += ".param .s32 "; // TODO: add types
Che-Liang Chioudf659632010-11-08 03:06:08 +0000320 decl += PARAM_PREFIX;
321 decl += utostr(i + 1);
322 }
323 } else {
324 for (PTXMachineFunctionInfo::reg_iterator
325 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
326 reg = *i;
327 assert(reg != PTX::NoRegister && "Not a valid register!");
328 if (i != b)
329 decl += ", ";
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000330 decl += ".reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000331 decl += getRegisterTypeName(reg);
332 decl += " ";
333 decl += getRegisterName(reg);
334 }
335 }
336 decl += ")";
337 }
338
339 OutStreamer.EmitRawText(Twine(decl));
340}
341
Eric Christopher50880d02010-09-18 18:52:28 +0000342#include "PTXGenAsmWriter.inc"
343
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000344// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000345extern "C" void LLVMInitializePTXAsmPrinter() {
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000346 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
347}