blob: cd27fb5d82efcbe1432dfb782467a6da135193b0 [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) {
106 if (addressSpace <= 255)
107 return "global";
108 // TODO Add more state spaces
109
110 llvm_unreachable("Unknown state space");
111 return NULL;
112}
113
114bool PTXAsmPrinter::doFinalization(Module &M) {
115 // XXX Temproarily remove global variables so that doFinalization() will not
116 // emit them again (global variables are emitted at beginning).
117
118 Module::GlobalListType &global_list = M.getGlobalList();
119 int i, n = global_list.size();
120 GlobalVariable **gv_array = new GlobalVariable* [n];
121
122 // first, back-up GlobalVariable in gv_array
123 i = 0;
124 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
125 I != E; ++I)
126 gv_array[i++] = &*I;
127
128 // second, empty global_list
129 while (!global_list.empty())
130 global_list.remove(global_list.begin());
131
132 // call doFinalization
133 bool ret = AsmPrinter::doFinalization(M);
134
135 // now we restore global variables
136 for (i = 0; i < n; i ++)
137 global_list.insert(global_list.end(), gv_array[i]);
138
139 delete[] gv_array;
140 return ret;
141}
142
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000143void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
144{
145 OutStreamer.EmitRawText(Twine("\t.version " + OptPTXVersion));
146 OutStreamer.EmitRawText(Twine("\t.target " + OptPTXTarget));
147 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000148
149 // declare global variables
150 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
151 i != e; ++i)
152 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000153}
154
Che-Liang Chioudf659632010-11-08 03:06:08 +0000155bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
156 SetupMachineFunction(MF);
157 EmitFunctionDeclaration();
158 EmitFunctionBody();
159 return false;
160}
161
162void PTXAsmPrinter::EmitFunctionBodyStart() {
163 OutStreamer.EmitRawText(Twine("{"));
164
165 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
166
167 // Print local variable definition
168 for (PTXMachineFunctionInfo::reg_iterator
169 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
170 unsigned reg = *i;
171
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000172 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000173 def += getRegisterTypeName(reg);
174 def += ' ';
175 def += getRegisterName(reg);
176 def += ';';
177 OutStreamer.EmitRawText(Twine(def));
178 }
179}
180
Eric Christopher50880d02010-09-18 18:52:28 +0000181void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000182 std::string str;
183 str.reserve(64);
184
185 // Write instruction to str
186 raw_string_ostream OS(str);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000187 printInstruction(MI, OS);
188 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000189 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000190
191 // Replace "%type" if found
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000192 size_t pos;
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000193 if ((pos = str.find("%type")) != std::string::npos)
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000194 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000195
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000196 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000197 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000198}
199
200void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
201 raw_ostream &OS) {
202 const MachineOperand &MO = MI->getOperand(opNum);
203
204 switch (MO.getType()) {
205 default:
206 llvm_unreachable("<unknown operand type>");
207 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000208 case MachineOperand::MO_GlobalAddress:
209 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000210 break;
211 case MachineOperand::MO_Immediate:
212 OS << (int) MO.getImm();
213 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000214 case MachineOperand::MO_Register:
215 OS << getRegisterName(MO.getReg());
216 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000217 }
Eric Christopher50880d02010-09-18 18:52:28 +0000218}
219
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000220void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
221 raw_ostream &OS, const char *Modifier) {
222 printOperand(MI, opNum, OS);
223
224 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
225 return; // don't print "+0"
226
227 OS << "+";
228 printOperand(MI, opNum+1, OS);
229}
230
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000231void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
232 // Check to see if this is a special global used by LLVM, if so, emit it.
233 if (EmitSpecialLLVMGlobal(gv))
234 return;
235
236 MCSymbol *gvsym = Mang->getSymbol(gv);
237
238 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
239
240 std::string decl;
241
242 // check if it is defined in some other translation unit
243 if (gv->isDeclaration())
244 decl += ".extern ";
245
246 // state space: e.g., .global
247 decl += ".";
248 decl += getStateSpaceName(gv->getType()->getAddressSpace());
249 decl += " ";
250
251 // alignment (optional)
252 unsigned alignment = gv->getAlignment();
253 if (alignment != 0) {
254 decl += ".align ";
255 decl += utostr(Log2_32(gv->getAlignment()));
256 decl += " ";
257 }
258
259 // TODO: add types
260 decl += ".s32 ";
261
262 decl += gvsym->getName();
263
264 if (ArrayType::classof(gv->getType()) || PointerType::classof(gv->getType()))
265 decl += "[]";
266
267 decl += ";";
268
269 OutStreamer.EmitRawText(Twine(decl));
270
271 OutStreamer.AddBlankLine();
272}
273
Che-Liang Chioudf659632010-11-08 03:06:08 +0000274void PTXAsmPrinter::EmitFunctionDeclaration() {
275 // The function label could have already been emitted if two symbols end up
276 // conflicting due to asm renaming. Detect this and emit an error.
277 if (!CurrentFnSym->isUndefined()) {
278 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
279 "' label emitted multiple times to assembly file");
280 return;
281 }
282
283 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
284 const bool isKernel = MFI->isKernel();
285 unsigned reg;
286
287 std::string decl = isKernel ? ".entry" : ".func";
288
289 // Print return register
290 reg = MFI->retReg();
291 if (!isKernel && reg != PTX::NoRegister) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000292 decl += " (.reg ."; // FIXME: could it return in .param space?
Che-Liang Chioudf659632010-11-08 03:06:08 +0000293 decl += getRegisterTypeName(reg);
294 decl += " ";
295 decl += getRegisterName(reg);
296 decl += ")";
297 }
298
299 // Print function name
300 decl += " ";
301 decl += CurrentFnSym->getName().str();
302
303 // Print parameter list
304 if (!MFI->argRegEmpty()) {
305 decl += " (";
306 if (isKernel) {
307 for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
308 if (i != 0)
309 decl += ", ";
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000310 decl += ".param .s32 "; // TODO: add types
Che-Liang Chioudf659632010-11-08 03:06:08 +0000311 decl += PARAM_PREFIX;
312 decl += utostr(i + 1);
313 }
314 } else {
315 for (PTXMachineFunctionInfo::reg_iterator
316 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
317 reg = *i;
318 assert(reg != PTX::NoRegister && "Not a valid register!");
319 if (i != b)
320 decl += ", ";
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000321 decl += ".reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000322 decl += getRegisterTypeName(reg);
323 decl += " ";
324 decl += getRegisterName(reg);
325 }
326 }
327 decl += ")";
328 }
329
330 OutStreamer.EmitRawText(Twine(decl));
331}
332
Eric Christopher50880d02010-09-18 18:52:28 +0000333#include "PTXGenAsmWriter.inc"
334
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000335// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000336extern "C" void LLVMInitializePTXAsmPrinter() {
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000337 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
338}