blob: 872287eeea8f5afe1725abc7bbf972ad91211a9c [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>
41OptPTXVersion("ptx-version", cl::desc("Set PTX version"),
42 cl::init("1.4"));
43
44static cl::opt<std::string>
45OptPTXTarget("ptx-target", cl::desc("Set GPU target (comma-separated list)"),
46 cl::init("sm_10"));
47
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000048namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000049class PTXAsmPrinter : public AsmPrinter {
50public:
51 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
52 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000053
Che-Liang Chioudf659632010-11-08 03:06:08 +000054 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000055
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000056 bool doFinalization(Module &M);
57
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000058 virtual void EmitStartOfAsmFile(Module &M);
59
Che-Liang Chioudf659632010-11-08 03:06:08 +000060 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000061
Che-Liang Chioudf659632010-11-08 03:06:08 +000062 virtual void EmitFunctionBodyStart();
63 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
64
65 virtual void EmitInstruction(const MachineInstr *MI);
66
67 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000068 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
69 const char *Modifier = 0);
Che-Liang Chioudf659632010-11-08 03:06:08 +000070
71 // autogen'd.
72 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
73 static const char *getRegisterName(unsigned RegNo);
74
75private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000076 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000077 void EmitFunctionDeclaration();
78}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000079} // namespace
80
Che-Liang Chioudf659632010-11-08 03:06:08 +000081static const char PARAM_PREFIX[] = "__param_";
82
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000083static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioudf659632010-11-08 03:06:08 +000084#define TEST_REGCLS(cls, clsstr) \
85 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000086 TEST_REGCLS(RRegs32, s32);
87 TEST_REGCLS(Preds, pred);
Che-Liang Chioudf659632010-11-08 03:06:08 +000088#undef TEST_REGCLS
89
90 llvm_unreachable("Not in any register class!");
91 return NULL;
92}
93
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000094static const char *getInstructionTypeName(const MachineInstr *MI) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000095 for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
96 const MachineOperand &MO = MI->getOperand(i);
97 if (MO.getType() == MachineOperand::MO_Register)
98 return getRegisterTypeName(MO.getReg());
99 }
100
101 llvm_unreachable("No reg operand found in instruction!");
102 return NULL;
103}
104
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000105static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +0000106 switch (addressSpace) {
107 default: llvm_unreachable("Unknown state space");
108 case PTX::GLOBAL: return "global";
109 case PTX::CONSTANT: return "const";
110 case PTX::LOCAL: return "local";
111 case PTX::PARAMETER: return "param";
112 case PTX::SHARED: return "shared";
113 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000114 return NULL;
115}
116
117bool PTXAsmPrinter::doFinalization(Module &M) {
118 // XXX Temproarily remove global variables so that doFinalization() will not
119 // emit them again (global variables are emitted at beginning).
120
121 Module::GlobalListType &global_list = M.getGlobalList();
122 int i, n = global_list.size();
123 GlobalVariable **gv_array = new GlobalVariable* [n];
124
125 // first, back-up GlobalVariable in gv_array
126 i = 0;
127 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
128 I != E; ++I)
129 gv_array[i++] = &*I;
130
131 // second, empty global_list
132 while (!global_list.empty())
133 global_list.remove(global_list.begin());
134
135 // call doFinalization
136 bool ret = AsmPrinter::doFinalization(M);
137
138 // now we restore global variables
139 for (i = 0; i < n; i ++)
140 global_list.insert(global_list.end(), gv_array[i]);
141
142 delete[] gv_array;
143 return ret;
144}
145
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000146void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
147{
148 OutStreamer.EmitRawText(Twine("\t.version " + OptPTXVersion));
149 OutStreamer.EmitRawText(Twine("\t.target " + OptPTXTarget));
150 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000151
152 // declare global variables
153 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
154 i != e; ++i)
155 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000156}
157
Che-Liang Chioudf659632010-11-08 03:06:08 +0000158bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
159 SetupMachineFunction(MF);
160 EmitFunctionDeclaration();
161 EmitFunctionBody();
162 return false;
163}
164
165void PTXAsmPrinter::EmitFunctionBodyStart() {
166 OutStreamer.EmitRawText(Twine("{"));
167
168 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
169
170 // Print local variable definition
171 for (PTXMachineFunctionInfo::reg_iterator
172 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
173 unsigned reg = *i;
174
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000175 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000176 def += getRegisterTypeName(reg);
177 def += ' ';
178 def += getRegisterName(reg);
179 def += ';';
180 OutStreamer.EmitRawText(Twine(def));
181 }
182}
183
Eric Christopher50880d02010-09-18 18:52:28 +0000184void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000185 std::string str;
186 str.reserve(64);
187
188 // Write instruction to str
189 raw_string_ostream OS(str);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000190 printInstruction(MI, OS);
191 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000192 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000193
194 // Replace "%type" if found
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000195 size_t pos;
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000196 if ((pos = str.find("%type")) != std::string::npos)
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000197 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000198
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000199 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000200 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000201}
202
203void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
204 raw_ostream &OS) {
205 const MachineOperand &MO = MI->getOperand(opNum);
206
207 switch (MO.getType()) {
208 default:
209 llvm_unreachable("<unknown operand type>");
210 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000211 case MachineOperand::MO_GlobalAddress:
212 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000213 break;
214 case MachineOperand::MO_Immediate:
215 OS << (int) MO.getImm();
216 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000217 case MachineOperand::MO_Register:
218 OS << getRegisterName(MO.getReg());
219 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000220 }
Eric Christopher50880d02010-09-18 18:52:28 +0000221}
222
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000223void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
224 raw_ostream &OS, const char *Modifier) {
225 printOperand(MI, opNum, OS);
226
227 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
228 return; // don't print "+0"
229
230 OS << "+";
231 printOperand(MI, opNum+1, OS);
232}
233
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000234void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
235 // Check to see if this is a special global used by LLVM, if so, emit it.
236 if (EmitSpecialLLVMGlobal(gv))
237 return;
238
239 MCSymbol *gvsym = Mang->getSymbol(gv);
240
241 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
242
243 std::string decl;
244
245 // check if it is defined in some other translation unit
246 if (gv->isDeclaration())
247 decl += ".extern ";
248
249 // state space: e.g., .global
250 decl += ".";
251 decl += getStateSpaceName(gv->getType()->getAddressSpace());
252 decl += " ";
253
254 // alignment (optional)
255 unsigned alignment = gv->getAlignment();
256 if (alignment != 0) {
257 decl += ".align ";
258 decl += utostr(Log2_32(gv->getAlignment()));
259 decl += " ";
260 }
261
262 // TODO: add types
263 decl += ".s32 ";
264
265 decl += gvsym->getName();
266
267 if (ArrayType::classof(gv->getType()) || PointerType::classof(gv->getType()))
268 decl += "[]";
269
270 decl += ";";
271
272 OutStreamer.EmitRawText(Twine(decl));
273
274 OutStreamer.AddBlankLine();
275}
276
Che-Liang Chioudf659632010-11-08 03:06:08 +0000277void PTXAsmPrinter::EmitFunctionDeclaration() {
278 // The function label could have already been emitted if two symbols end up
279 // conflicting due to asm renaming. Detect this and emit an error.
280 if (!CurrentFnSym->isUndefined()) {
281 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
282 "' label emitted multiple times to assembly file");
283 return;
284 }
285
286 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
287 const bool isKernel = MFI->isKernel();
288 unsigned reg;
289
290 std::string decl = isKernel ? ".entry" : ".func";
291
292 // Print return register
293 reg = MFI->retReg();
294 if (!isKernel && reg != PTX::NoRegister) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000295 decl += " (.reg ."; // FIXME: could it return in .param space?
Che-Liang Chioudf659632010-11-08 03:06:08 +0000296 decl += getRegisterTypeName(reg);
297 decl += " ";
298 decl += getRegisterName(reg);
299 decl += ")";
300 }
301
302 // Print function name
303 decl += " ";
304 decl += CurrentFnSym->getName().str();
305
306 // Print parameter list
307 if (!MFI->argRegEmpty()) {
308 decl += " (";
309 if (isKernel) {
310 for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
311 if (i != 0)
312 decl += ", ";
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000313 decl += ".param .s32 "; // TODO: add types
Che-Liang Chioudf659632010-11-08 03:06:08 +0000314 decl += PARAM_PREFIX;
315 decl += utostr(i + 1);
316 }
317 } else {
318 for (PTXMachineFunctionInfo::reg_iterator
319 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
320 reg = *i;
321 assert(reg != PTX::NoRegister && "Not a valid register!");
322 if (i != b)
323 decl += ", ";
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000324 decl += ".reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000325 decl += getRegisterTypeName(reg);
326 decl += " ";
327 decl += getRegisterName(reg);
328 }
329 }
330 decl += ")";
331 }
332
333 OutStreamer.EmitRawText(Twine(decl));
334}
335
Eric Christopher50880d02010-09-18 18:52:28 +0000336#include "PTXGenAsmWriter.inc"
337
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000338// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000339extern "C" void LLVMInitializePTXAsmPrinter() {
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000340 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
341}