blob: 3363c7300f8dc690eed71bf0ad814d0f9a5bca02 [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"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000028#include "llvm/MC/MCStreamer.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000029#include "llvm/MC/MCSymbol.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000030#include "llvm/Target/Mangler.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000032#include "llvm/Target/TargetRegistry.h"
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000033#include "llvm/Support/CommandLine.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000036#include "llvm/Support/MathExtras.h"
Che-Liang Chioudf659632010-11-08 03:06:08 +000037#include "llvm/Support/raw_ostream.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000038
39using namespace llvm;
40
41namespace {
Che-Liang Chioudf659632010-11-08 03:06:08 +000042class PTXAsmPrinter : public AsmPrinter {
43public:
44 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
45 : AsmPrinter(TM, Streamer) {}
Eric Christopher50880d02010-09-18 18:52:28 +000046
Che-Liang Chioudf659632010-11-08 03:06:08 +000047 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000048
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000049 bool doFinalization(Module &M);
50
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +000051 virtual void EmitStartOfAsmFile(Module &M);
52
Che-Liang Chioudf659632010-11-08 03:06:08 +000053 virtual bool runOnMachineFunction(MachineFunction &MF);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000054
Che-Liang Chioudf659632010-11-08 03:06:08 +000055 virtual void EmitFunctionBodyStart();
56 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
57
58 virtual void EmitInstruction(const MachineInstr *MI);
59
60 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000061 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
62 const char *Modifier = 0);
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000063 void printParamOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
64 const char *Modifier = 0);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +000065 void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
Che-Liang Chioudf659632010-11-08 03:06:08 +000066
67 // autogen'd.
68 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
69 static const char *getRegisterName(unsigned RegNo);
70
71private:
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000072 void EmitVariableDeclaration(const GlobalVariable *gv);
Che-Liang Chioudf659632010-11-08 03:06:08 +000073 void EmitFunctionDeclaration();
74}; // class PTXAsmPrinter
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000075} // namespace
76
Che-Liang Chioudf659632010-11-08 03:06:08 +000077static const char PARAM_PREFIX[] = "__param_";
78
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +000079static const char *getRegisterTypeName(unsigned RegNo) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000080#define TEST_REGCLS(cls, clsstr) \
Che-Liang Chioudf659632010-11-08 03:06:08 +000081 if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
Che-Liang Chiou3f409f72010-11-17 08:08:49 +000082 TEST_REGCLS(Preds, pred);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000083 TEST_REGCLS(RRegu16, u16);
84 TEST_REGCLS(RRegu32, u32);
85 TEST_REGCLS(RRegu64, u64);
86 TEST_REGCLS(RRegf32, f32);
87 TEST_REGCLS(RRegf64, f64);
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 Chioufc7072c2010-12-22 10:38:51 +000094static const char *getStateSpaceName(unsigned addressSpace) {
Che-Liang Chioud34f19f2010-12-30 10:41:27 +000095 switch (addressSpace) {
96 default: llvm_unreachable("Unknown state space");
97 case PTX::GLOBAL: return "global";
98 case PTX::CONSTANT: return "const";
99 case PTX::LOCAL: return "local";
100 case PTX::PARAMETER: return "param";
101 case PTX::SHARED: return "shared";
102 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000103 return NULL;
104}
105
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000106static const char *getTypeName(const Type* type) {
107 while (true) {
108 switch (type->getTypeID()) {
109 default: llvm_unreachable("Unknown type");
110 case Type::FloatTyID: return ".f32";
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000111 case Type::DoubleTyID: return ".f64";
112 case Type::IntegerTyID:
113 switch (type->getPrimitiveSizeInBits()) {
114 default: llvm_unreachable("Unknown integer bit-width");
115 case 16: return ".u16";
116 case 32: return ".u32";
117 case 64: return ".u64";
118 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000119 case Type::ArrayTyID:
120 case Type::PointerTyID:
121 type = dyn_cast<const SequentialType>(type)->getElementType();
122 break;
123 }
124 }
125 return NULL;
126}
127
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000128bool PTXAsmPrinter::doFinalization(Module &M) {
129 // XXX Temproarily remove global variables so that doFinalization() will not
130 // emit them again (global variables are emitted at beginning).
131
132 Module::GlobalListType &global_list = M.getGlobalList();
133 int i, n = global_list.size();
134 GlobalVariable **gv_array = new GlobalVariable* [n];
135
136 // first, back-up GlobalVariable in gv_array
137 i = 0;
138 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
139 I != E; ++I)
140 gv_array[i++] = &*I;
141
142 // second, empty global_list
143 while (!global_list.empty())
144 global_list.remove(global_list.begin());
145
146 // call doFinalization
147 bool ret = AsmPrinter::doFinalization(M);
148
149 // now we restore global variables
150 for (i = 0; i < n; i ++)
151 global_list.insert(global_list.end(), gv_array[i]);
152
153 delete[] gv_array;
154 return ret;
155}
156
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000157void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
158{
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000159 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
160
161 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
162 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
Justin Holewinski12785e82011-03-03 13:34:29 +0000163 (ST.supportsDouble() ? ""
164 : ", map_f64_to_f32")));
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000165 OutStreamer.AddBlankLine();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000166
167 // declare global variables
168 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
169 i != e; ++i)
170 EmitVariableDeclaration(i);
Che-Liang Chiou21d8b9b2010-11-30 10:14:14 +0000171}
172
Che-Liang Chioudf659632010-11-08 03:06:08 +0000173bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
174 SetupMachineFunction(MF);
175 EmitFunctionDeclaration();
176 EmitFunctionBody();
177 return false;
178}
179
180void PTXAsmPrinter::EmitFunctionBodyStart() {
181 OutStreamer.EmitRawText(Twine("{"));
182
183 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
184
185 // Print local variable definition
186 for (PTXMachineFunctionInfo::reg_iterator
187 i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
188 unsigned reg = *i;
189
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000190 std::string def = "\t.reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000191 def += getRegisterTypeName(reg);
192 def += ' ';
193 def += getRegisterName(reg);
194 def += ';';
195 OutStreamer.EmitRawText(Twine(def));
196 }
197}
198
Eric Christopher50880d02010-09-18 18:52:28 +0000199void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000200 std::string str;
201 str.reserve(64);
202
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000203 raw_string_ostream OS(str);
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000204
205 // Emit predicate
206 printPredicateOperand(MI, OS);
207
208 // Write instruction to str
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000209 printInstruction(MI, OS);
210 OS << ';';
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000211 OS.flush();
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000212
Che-Liang Chiou3608d2a2010-12-01 11:45:53 +0000213 StringRef strref = StringRef(str);
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000214 OutStreamer.EmitRawText(strref);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000215}
216
217void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
218 raw_ostream &OS) {
219 const MachineOperand &MO = MI->getOperand(opNum);
220
221 switch (MO.getType()) {
222 default:
223 llvm_unreachable("<unknown operand type>");
224 break;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000225 case MachineOperand::MO_GlobalAddress:
226 OS << *Mang->getSymbol(MO.getGlobal());
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000227 break;
228 case MachineOperand::MO_Immediate:
229 OS << (int) MO.getImm();
230 break;
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000231 case MachineOperand::MO_MachineBasicBlock:
232 OS << *MO.getMBB()->getSymbol();
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();
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000239 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
240 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
241 if (isFloat) {
242 OS << "0F";
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000243 }
244 else {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000245 OS << "0D";
246 }
247 // Emit the encoded floating-point value.
248 if (constFP.getZExtValue() > 0) {
249 OS << constFP.toString(16, false);
250 }
251 else {
252 OS << "00000000";
253 // If We have a double-precision zero, pad to 8-bytes.
254 if (!isFloat) {
255 OS << "00000000";
256 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000257 }
258 break;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000259 }
Eric Christopher50880d02010-09-18 18:52:28 +0000260}
261
Che-Liang Chiou3f8e6172010-11-30 07:34:44 +0000262void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
263 raw_ostream &OS, const char *Modifier) {
264 printOperand(MI, opNum, OS);
265
266 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
267 return; // don't print "+0"
268
269 OS << "+";
270 printOperand(MI, opNum+1, OS);
271}
272
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000273void PTXAsmPrinter::printParamOperand(const MachineInstr *MI, int opNum,
274 raw_ostream &OS, const char *Modifier) {
275 OS << PARAM_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
276}
277
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000278void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
279 // Check to see if this is a special global used by LLVM, if so, emit it.
280 if (EmitSpecialLLVMGlobal(gv))
281 return;
282
283 MCSymbol *gvsym = Mang->getSymbol(gv);
284
285 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
286
287 std::string decl;
288
289 // check if it is defined in some other translation unit
290 if (gv->isDeclaration())
291 decl += ".extern ";
292
293 // state space: e.g., .global
294 decl += ".";
295 decl += getStateSpaceName(gv->getType()->getAddressSpace());
296 decl += " ";
297
298 // alignment (optional)
299 unsigned alignment = gv->getAlignment();
300 if (alignment != 0) {
301 decl += ".align ";
302 decl += utostr(Log2_32(gv->getAlignment()));
303 decl += " ";
304 }
305
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000306
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000307 if (PointerType::classof(gv->getType())) {
308 const PointerType* pointerTy = dyn_cast<const PointerType>(gv->getType());
309 const Type* elementTy = pointerTy->getElementType();
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000310
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000311 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
312
313 const ArrayType* arrayTy = dyn_cast<const ArrayType>(elementTy);
314 elementTy = arrayTy->getElementType();
315
Justin Holewinski8af78c92011-03-18 19:24:28 +0000316 unsigned numElements = arrayTy->getNumElements();
317
318 while (elementTy->isArrayTy()) {
319
320 arrayTy = dyn_cast<const ArrayType>(elementTy);
321 elementTy = arrayTy->getElementType();
322
323 numElements *= arrayTy->getNumElements();
324 }
325
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000326 // FIXME: isPrimitiveType() == false for i16?
327 assert(elementTy->isSingleValueType() &&
328 "Non-primitive types are not handled");
329
330 // Compute the size of the array, in bytes.
331 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
Justin Holewinski8af78c92011-03-18 19:24:28 +0000332 * numElements;
Justin Holewinskiae3ce172011-03-14 15:40:11 +0000333
334 decl += ".b8 ";
335 decl += gvsym->getName();
336 decl += "[";
337 decl += utostr(arraySize);
338 decl += "]";
339 }
340 else {
341 // Note: this is currently the fall-through case and most likely generates
342 // incorrect code.
343 decl += getTypeName(gv->getType());
344 decl += " ";
345
346 decl += gvsym->getName();
347
348 if (ArrayType::classof(gv->getType()) ||
349 PointerType::classof(gv->getType()))
350 decl += "[]";
351 }
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000352
353 decl += ";";
354
355 OutStreamer.EmitRawText(Twine(decl));
356
357 OutStreamer.AddBlankLine();
358}
359
Che-Liang Chioudf659632010-11-08 03:06:08 +0000360void PTXAsmPrinter::EmitFunctionDeclaration() {
361 // The function label could have already been emitted if two symbols end up
362 // conflicting due to asm renaming. Detect this and emit an error.
363 if (!CurrentFnSym->isUndefined()) {
364 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
365 "' label emitted multiple times to assembly file");
366 return;
367 }
368
369 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
370 const bool isKernel = MFI->isKernel();
371 unsigned reg;
372
373 std::string decl = isKernel ? ".entry" : ".func";
374
375 // Print return register
376 reg = MFI->retReg();
377 if (!isKernel && reg != PTX::NoRegister) {
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000378 decl += " (.reg ."; // FIXME: could it return in .param space?
Che-Liang Chioudf659632010-11-08 03:06:08 +0000379 decl += getRegisterTypeName(reg);
380 decl += " ";
381 decl += getRegisterName(reg);
382 decl += ")";
383 }
384
385 // Print function name
386 decl += " ";
387 decl += CurrentFnSym->getName().str();
388
389 // Print parameter list
390 if (!MFI->argRegEmpty()) {
391 decl += " (";
392 if (isKernel) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000393 unsigned cnt = 0;
Che-Liang Chiou8902ecb2011-03-18 11:23:56 +0000394 for(PTXMachineFunctionInfo::reg_iterator
395 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
Che-Liang Chiou31c488c2011-03-02 07:58:46 +0000396 i != e; ++i) {
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000397 reg = *i;
398 assert(reg != PTX::NoRegister && "Not a valid register!");
399 if (i != b)
Che-Liang Chioudf659632010-11-08 03:06:08 +0000400 decl += ", ";
Che-Liang Chiouf48817c2011-03-02 07:36:48 +0000401 decl += ".param .";
402 decl += getRegisterTypeName(reg);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000403 decl += " ";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000404 decl += PARAM_PREFIX;
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000405 decl += utostr(++cnt);
Che-Liang Chioudf659632010-11-08 03:06:08 +0000406 }
407 } else {
Che-Liang Chiou8902ecb2011-03-18 11:23:56 +0000408 for (PTXMachineFunctionInfo::reg_iterator
409 i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i;
Che-Liang Chiou31c488c2011-03-02 07:58:46 +0000410 i != e; ++i) {
Che-Liang Chioudf659632010-11-08 03:06:08 +0000411 reg = *i;
412 assert(reg != PTX::NoRegister && "Not a valid register!");
413 if (i != b)
414 decl += ", ";
Che-Liang Chiou3f409f72010-11-17 08:08:49 +0000415 decl += ".reg .";
Che-Liang Chioudf659632010-11-08 03:06:08 +0000416 decl += getRegisterTypeName(reg);
417 decl += " ";
418 decl += getRegisterName(reg);
419 }
420 }
421 decl += ")";
422 }
423
424 OutStreamer.EmitRawText(Twine(decl));
425}
426
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000427void PTXAsmPrinter::
428printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
429 int i = MI->findFirstPredOperandIdx();
430 if (i == -1)
431 llvm_unreachable("missing predicate operand");
432
433 unsigned reg = MI->getOperand(i).getReg();
434 int predOp = MI->getOperand(i+1).getImm();
435
436 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
437
Che-Liang Chiouf78847e2011-03-14 11:26:01 +0000438 if (reg != PTX::NoRegister) {
Che-Liang Chiouc2ec0f92011-03-13 17:26:00 +0000439 O << '@';
440 if (predOp == PTX::PRED_NEGATE)
441 O << '!';
442 O << getRegisterName(reg);
443 }
444}
445
Eric Christopher50880d02010-09-18 18:52:28 +0000446#include "PTXGenAsmWriter.inc"
447
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000448// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +0000449extern "C" void LLVMInitializePTXAsmPrinter() {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000450 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
451 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000452}