blob: 25f26fa4c41cfa93f9f56d3e5a571dd14f47ce38 [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 Chiouf7172022011-02-28 06:34:09 +000087 TEST_REGCLS(RRegf32, f32);
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000088 TEST_REGCLS(RRegs32, s32);
89 TEST_REGCLS(Preds, pred);
Che-Liang Chioudf659632010-11-08 03:06:08 +000090#undef TEST_REGCLS
91
92 llvm_unreachable("Not in any register class!");
93 return NULL;
94}
95
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000096static const char *getInstructionTypeName(const MachineInstr *MI) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000097 for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
98 const MachineOperand &MO = MI->getOperand(i);
99 if (MO.getType() == MachineOperand::MO_Register)
100 return getRegisterTypeName(MO.getReg());
101 }
102
103 llvm_unreachable("No reg operand found in instruction!");
104 return NULL;
105}
106
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000107static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +0000108 switch (addressSpace) {
109 default: llvm_unreachable("Unknown state space");
110 case PTX::GLOBAL: return "global";
111 case PTX::CONSTANT: return "const";
112 case PTX::LOCAL: return "local";
113 case PTX::PARAMETER: return "param";
114 case PTX::SHARED: return "shared";
115 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000116 return NULL;
117}
118
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000119static const char *getTypeName(const Type* type) {
120 while (true) {
121 switch (type->getTypeID()) {
122 default: llvm_unreachable("Unknown type");
123 case Type::FloatTyID: return ".f32";
124 case Type::IntegerTyID: return ".s32"; // TODO: Handle 64-bit types.
125 case Type::ArrayTyID:
126 case Type::PointerTyID:
127 type = dyn_cast<const SequentialType>(type)->getElementType();
128 break;
129 }
130 }
131 return NULL;
132}
133
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000134bool PTXAsmPrinter::doFinalization(Module &M) {
135 // XXX Temproarily remove global variables so that doFinalization() will not
136 // emit them again (global variables are emitted at beginning).
137
138 Module::GlobalListType &global_list = M.getGlobalList();
139 int i, n = global_list.size();
140 GlobalVariable **gv_array = new GlobalVariable* [n];
141
142 // first, back-up GlobalVariable in gv_array
143 i = 0;
144 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
145 I != E; ++I)
146 gv_array[i++] = &*I;
147
148 // second, empty global_list
149 while (!global_list.empty())
150 global_list.remove(global_list.begin());
151
152 // call doFinalization
153 bool ret = AsmPrinter::doFinalization(M);
154
155 // now we restore global variables
156 for (i = 0; i < n; i ++)
157 global_list.insert(global_list.end(), gv_array[i]);
158
159 delete[] gv_array;
160 return ret;
161}
162
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000163void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
164{
165 OutStreamer.EmitRawText(Twine("\t.version " + OptPTXVersion));
166 OutStreamer.EmitRawText(Twine("\t.target " + OptPTXTarget));
167 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000168
169 // declare global variables
170 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
171 i != e; ++i)
172 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000173}
174
Che-Liang Chioudf659632010-11-08 03:06:08 +0000175bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
176 SetupMachineFunction(MF);
177 EmitFunctionDeclaration();
178 EmitFunctionBody();
179 return false;
180}
181
182void PTXAsmPrinter::EmitFunctionBodyStart() {
183 OutStreamer.EmitRawText(Twine("{"));
184
185 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
186
187 // Print local variable definition
188 for (PTXMachineFunctionInfo::reg_iterator
189 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
190 unsigned reg = *i;
191
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000192 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000193 def += getRegisterTypeName(reg);
194 def += ' ';
195 def += getRegisterName(reg);
196 def += ';';
197 OutStreamer.EmitRawText(Twine(def));
198 }
199}
200
Eric Christopher50880d02010-09-18 18:52:28 +0000201void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000202 std::string str;
203 str.reserve(64);
204
205 // Write instruction to str
206 raw_string_ostream OS(str);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000207 printInstruction(MI, OS);
208 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000209 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000210
211 // Replace "%type" if found
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000212 size_t pos;
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000213 if ((pos = str.find("%type")) != std::string::npos)
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000214 str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000215
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000216 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000217 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000218}
219
220void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
221 raw_ostream &OS) {
222 const MachineOperand &MO = MI->getOperand(opNum);
223
224 switch (MO.getType()) {
225 default:
226 llvm_unreachable("<unknown operand type>");
227 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000228 case MachineOperand::MO_GlobalAddress:
229 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000230 break;
231 case MachineOperand::MO_Immediate:
232 OS << (int) MO.getImm();
233 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000234 case MachineOperand::MO_Register:
235 OS << getRegisterName(MO.getReg());
236 break;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000237 case MachineOperand::MO_FPImmediate:
238 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
239 if (constFP.getZExtValue() > 0) {
240 OS << "0F" << constFP.toString(16, false);
241 }
242 else {
243 OS << "0F00000000";
244 }
245 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000246 }
Eric Christopher50880d02010-09-18 18:52:28 +0000247}
248
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000249void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
250 raw_ostream &OS, const char *Modifier) {
251 printOperand(MI, opNum, OS);
252
253 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
254 return; // don't print "+0"
255
256 OS << "+";
257 printOperand(MI, opNum+1, OS);
258}
259
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000260void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
261 raw_ostream &OS, const char *Modifier) {
262 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
263}
264
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000265void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
266 // Check to see if this is a special global used by LLVM, if so, emit it.
267 if (EmitSpecialLLVMGlobal(gv))
268 return;
269
270 MCSymbol *gvsym = Mang->getSymbol(gv);
271
272 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
273
274 std::string decl;
275
276 // check if it is defined in some other translation unit
277 if (gv->isDeclaration())
278 decl += ".extern ";
279
280 // state space: e.g., .global
281 decl += ".";
282 decl += getStateSpaceName(gv->getType()->getAddressSpace());
283 decl += " ";
284
285 // alignment (optional)
286 unsigned alignment = gv->getAlignment();
287 if (alignment != 0) {
288 decl += ".align ";
289 decl += utostr(Log2_32(gv->getAlignment()));
290 decl += " ";
291 }
292
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000293 decl += getTypeName(gv->getType());
294 decl += " ";
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000295
296 decl += gvsym->getName();
297
298 if (ArrayType::classof(gv->getType()) || PointerType::classof(gv->getType()))
299 decl += "[]";
300
301 decl += ";";
302
303 OutStreamer.EmitRawText(Twine(decl));
304
305 OutStreamer.AddBlankLine();
306}
307
Che-Liang Chioudf659632010-11-08 03:06:08 +0000308void PTXAsmPrinter::EmitFunctionDeclaration() {
309 // The function label could have already been emitted if two symbols end up
310 // conflicting due to asm renaming. Detect this and emit an error.
311 if (!CurrentFnSym->isUndefined()) {
312 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
313 "' label emitted multiple times to assembly file");
314 return;
315 }
316
317 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
318 const bool isKernel = MFI->isKernel();
319 unsigned reg;
320
321 std::string decl = isKernel ? ".entry" : ".func";
322
323 // Print return register
324 reg = MFI->retReg();
325 if (!isKernel && reg != PTX::NoRegister) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000326 decl += " (.reg ."; // FIXME: could it return in .param space?
Che-Liang Chioudf659632010-11-08 03:06:08 +0000327 decl += getRegisterTypeName(reg);
328 decl += " ";
329 decl += getRegisterName(reg);
330 decl += ")";
331 }
332
333 // Print function name
334 decl += " ";
335 decl += CurrentFnSym->getName().str();
336
337 // Print parameter list
338 if (!MFI->argRegEmpty()) {
339 decl += " (";
340 if (isKernel) {
341 for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
342 if (i != 0)
343 decl += ", ";
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000344 decl += ".param .s32 "; // TODO: add types
Che-Liang Chioudf659632010-11-08 03:06:08 +0000345 decl += PARAM_PREFIX;
346 decl += utostr(i + 1);
347 }
348 } else {
349 for (PTXMachineFunctionInfo::reg_iterator
350 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
351 reg = *i;
352 assert(reg != PTX::NoRegister && "Not a valid register!");
353 if (i != b)
354 decl += ", ";
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000355 decl += ".reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000356 decl += getRegisterTypeName(reg);
357 decl += " ";
358 decl += getRegisterName(reg);
359 }
360 }
361 decl += ")";
362 }
363
364 OutStreamer.EmitRawText(Twine(decl));
365}
366
Eric Christopher50880d02010-09-18 18:52:28 +0000367#include "PTXGenAsmWriter.inc"
368
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000369// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000370extern "C" void LLVMInitializePTXAsmPrinter() {
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000371 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
372}