1 //===-- PTXAsmPrinter.cpp - PTX LLVM assembly writer ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to PTX assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "ptx-asm-printer"
18 #include "PTXMachineFunctionInfo.h"
19 #include "PTXParamManager.h"
20 #include "PTXRegisterInfo.h"
21 #include "PTXTargetMachine.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Analysis/DebugInfo.h"
28 #include "llvm/CodeGen/AsmPrinter.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/MC/MCContext.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Target/Mangler.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/raw_ostream.h"
48 class PTXAsmPrinter : public AsmPrinter {
50 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
51 : AsmPrinter(TM, Streamer) {}
53 const char *getPassName() const { return "PTX Assembly Printer"; }
55 bool doFinalization(Module &M);
57 virtual void EmitStartOfAsmFile(Module &M);
59 virtual bool runOnMachineFunction(MachineFunction &MF);
61 virtual void EmitFunctionBodyStart();
62 virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
64 virtual void EmitInstruction(const MachineInstr *MI);
66 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
67 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
68 const char *Modifier = 0);
69 void printReturnOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
70 const char *Modifier = 0);
71 void printPredicateOperand(const MachineInstr *MI, raw_ostream &O);
73 void printCall(const MachineInstr *MI, raw_ostream &O);
75 unsigned GetOrCreateSourceID(StringRef FileName,
79 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
80 static const char *getRegisterName(unsigned RegNo);
83 void EmitVariableDeclaration(const GlobalVariable *gv);
84 void EmitFunctionDeclaration();
86 StringMap<unsigned> SourceIdMap;
87 }; // class PTXAsmPrinter
90 static const char PARAM_PREFIX[] = "__param_";
91 static const char RETURN_PREFIX[] = "__ret_";
93 static const char *getRegisterTypeName(unsigned RegNo,
94 const MachineRegisterInfo& MRI) {
95 const TargetRegisterClass *TRC = MRI.getRegClass(RegNo);
97 #define TEST_REGCLS(cls, clsstr) \
98 if (PTX::cls ## RegisterClass == TRC) return # clsstr;
100 TEST_REGCLS(RegPred, pred);
101 TEST_REGCLS(RegI16, b16);
102 TEST_REGCLS(RegI32, b32);
103 TEST_REGCLS(RegI64, b64);
104 TEST_REGCLS(RegF32, b32);
105 TEST_REGCLS(RegF64, b64);
108 llvm_unreachable("Not in any register class!");
112 static const char *getStateSpaceName(unsigned addressSpace) {
113 switch (addressSpace) {
114 default: llvm_unreachable("Unknown state space");
115 case PTX::GLOBAL: return "global";
116 case PTX::CONSTANT: return "const";
117 case PTX::LOCAL: return "local";
118 case PTX::PARAMETER: return "param";
119 case PTX::SHARED: return "shared";
124 static const char *getTypeName(Type* type) {
126 switch (type->getTypeID()) {
127 default: llvm_unreachable("Unknown type");
128 case Type::FloatTyID: return ".f32";
129 case Type::DoubleTyID: return ".f64";
130 case Type::IntegerTyID:
131 switch (type->getPrimitiveSizeInBits()) {
132 default: llvm_unreachable("Unknown integer bit-width");
133 case 16: return ".u16";
134 case 32: return ".u32";
135 case 64: return ".u64";
137 case Type::ArrayTyID:
138 case Type::PointerTyID:
139 type = dyn_cast<SequentialType>(type)->getElementType();
146 bool PTXAsmPrinter::doFinalization(Module &M) {
147 // XXX Temproarily remove global variables so that doFinalization() will not
148 // emit them again (global variables are emitted at beginning).
150 Module::GlobalListType &global_list = M.getGlobalList();
151 int i, n = global_list.size();
152 GlobalVariable **gv_array = new GlobalVariable* [n];
154 // first, back-up GlobalVariable in gv_array
156 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
160 // second, empty global_list
161 while (!global_list.empty())
162 global_list.remove(global_list.begin());
164 // call doFinalization
165 bool ret = AsmPrinter::doFinalization(M);
167 // now we restore global variables
168 for (i = 0; i < n; i ++)
169 global_list.insert(global_list.end(), gv_array[i]);
175 void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
177 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
179 OutStreamer.EmitRawText(Twine("\t.version " + ST.getPTXVersionString()));
180 OutStreamer.EmitRawText(Twine("\t.target " + ST.getTargetString() +
181 (ST.supportsDouble() ? ""
182 : ", map_f64_to_f32")));
183 // .address_size directive is optional, but it must immediately follow
184 // the .target directive if present within a module
185 if (ST.supportsPTX23()) {
186 std::string addrSize = ST.is64Bit() ? "64" : "32";
187 OutStreamer.EmitRawText(Twine("\t.address_size " + addrSize));
190 OutStreamer.AddBlankLine();
192 // Define any .file directives
193 DebugInfoFinder DbgFinder;
194 DbgFinder.processModule(M);
196 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
197 E = DbgFinder.compile_unit_end(); I != E; ++I) {
198 DICompileUnit DIUnit(*I);
199 StringRef FN = DIUnit.getFilename();
200 StringRef Dir = DIUnit.getDirectory();
201 GetOrCreateSourceID(FN, Dir);
204 OutStreamer.AddBlankLine();
206 // declare global variables
207 for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
209 EmitVariableDeclaration(i);
212 bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
213 SetupMachineFunction(MF);
214 EmitFunctionDeclaration();
219 void PTXAsmPrinter::EmitFunctionBodyStart() {
220 OutStreamer.EmitRawText(Twine("{"));
222 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
223 const PTXParamManager &PM = MFI->getParamManager();
225 // Print register definitions
230 numRegs = MFI->getNumRegistersForClass(PTX::RegPredRegisterClass);
232 regDefs += "\t.reg .pred %p<";
233 regDefs += utostr(numRegs);
238 numRegs = MFI->getNumRegistersForClass(PTX::RegI16RegisterClass);
240 regDefs += "\t.reg .b16 %rh<";
241 regDefs += utostr(numRegs);
246 numRegs = MFI->getNumRegistersForClass(PTX::RegI32RegisterClass);
248 regDefs += "\t.reg .b32 %r<";
249 regDefs += utostr(numRegs);
254 numRegs = MFI->getNumRegistersForClass(PTX::RegI64RegisterClass);
256 regDefs += "\t.reg .b64 %rd<";
257 regDefs += utostr(numRegs);
262 numRegs = MFI->getNumRegistersForClass(PTX::RegF32RegisterClass);
264 regDefs += "\t.reg .f32 %f<";
265 regDefs += utostr(numRegs);
270 numRegs = MFI->getNumRegistersForClass(PTX::RegF64RegisterClass);
272 regDefs += "\t.reg .f64 %fd<";
273 regDefs += utostr(numRegs);
278 for (PTXParamManager::param_iterator i = PM.local_begin(), e = PM.local_end();
280 regDefs += "\t.param .b";
281 regDefs += utostr(PM.getParamSize(*i));
283 regDefs += PM.getParamName(*i);
287 OutStreamer.EmitRawText(Twine(regDefs));
290 const MachineFrameInfo* FrameInfo = MF->getFrameInfo();
291 DEBUG(dbgs() << "Have " << FrameInfo->getNumObjects()
292 << " frame object(s)\n");
293 for (unsigned i = 0, e = FrameInfo->getNumObjects(); i != e; ++i) {
294 DEBUG(dbgs() << "Size of object: " << FrameInfo->getObjectSize(i) << "\n");
295 if (FrameInfo->getObjectSize(i) > 0) {
296 std::string def = "\t.local .align ";
297 def += utostr(FrameInfo->getObjectAlignment(i));
302 def += utostr(FrameInfo->getObjectSize(i)); // Convert to bits
305 OutStreamer.EmitRawText(Twine(def));
309 //unsigned Index = 1;
310 // Print parameter passing params
311 //for (PTXMachineFunctionInfo::param_iterator
312 // i = MFI->paramBegin(), e = MFI->paramEnd(); i != e; ++i) {
313 // std::string def = "\t.param .b";
314 // def += utostr(*i);
316 // def += utostr(Index);
319 // OutStreamer.EmitRawText(Twine(def));
323 void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
327 raw_string_ostream OS(str);
329 DebugLoc DL = MI->getDebugLoc();
330 if (!DL.isUnknown()) {
332 const MDNode *S = DL.getScope(MF->getFunction()->getContext());
334 // This is taken from DwarfDebug.cpp, which is conveniently not a public
340 DIDescriptor Scope(S);
341 if (Scope.isCompileUnit()) {
343 Fn = CU.getFilename();
344 Dir = CU.getDirectory();
345 } else if (Scope.isFile()) {
347 Fn = F.getFilename();
348 Dir = F.getDirectory();
349 } else if (Scope.isSubprogram()) {
351 Fn = SP.getFilename();
352 Dir = SP.getDirectory();
353 } else if (Scope.isLexicalBlock()) {
354 DILexicalBlock DB(S);
355 Fn = DB.getFilename();
356 Dir = DB.getDirectory();
358 assert(0 && "Unexpected scope info");
360 Src = GetOrCreateSourceID(Fn, Dir);
362 OutStreamer.EmitDwarfLocDirective(Src, DL.getLine(), DL.getCol(),
365 const MCDwarfLoc& MDL = OutContext.getCurrentDwarfLoc();
368 OS << utostr(MDL.getFileNum());
370 OS << utostr(MDL.getLine());
372 OS << utostr(MDL.getColumn());
378 printPredicateOperand(MI, OS);
380 // Write instruction to str
381 if (MI->getOpcode() == PTX::CALL) {
384 printInstruction(MI, OS);
389 StringRef strref = StringRef(str);
390 OutStreamer.EmitRawText(strref);
393 void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
395 const MachineOperand &MO = MI->getOperand(opNum);
396 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
398 switch (MO.getType()) {
400 llvm_unreachable("<unknown operand type>");
402 case MachineOperand::MO_GlobalAddress:
403 OS << *Mang->getSymbol(MO.getGlobal());
405 case MachineOperand::MO_Immediate:
406 OS << (long) MO.getImm();
408 case MachineOperand::MO_MachineBasicBlock:
409 OS << *MO.getMBB()->getSymbol();
411 case MachineOperand::MO_Register:
412 OS << MFI->getRegisterName(MO.getReg());
414 case MachineOperand::MO_ExternalSymbol:
415 OS << MO.getSymbolName();
417 case MachineOperand::MO_FPImmediate:
418 APInt constFP = MO.getFPImm()->getValueAPF().bitcastToAPInt();
419 bool isFloat = MO.getFPImm()->getType()->getTypeID() == Type::FloatTyID;
420 // Emit 0F for 32-bit floats and 0D for 64-bit doubles.
427 // Emit the encoded floating-point value.
428 if (constFP.getZExtValue() > 0) {
429 OS << constFP.toString(16, false);
433 // If We have a double-precision zero, pad to 8-bytes.
442 void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
443 raw_ostream &OS, const char *Modifier) {
444 printOperand(MI, opNum, OS);
446 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
447 return; // don't print "+0"
450 printOperand(MI, opNum+1, OS);
453 void PTXAsmPrinter::printReturnOperand(const MachineInstr *MI, int opNum,
454 raw_ostream &OS, const char *Modifier) {
455 //OS << RETURN_PREFIX << (int) MI->getOperand(opNum).getImm() + 1;
459 void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
460 // Check to see if this is a special global used by LLVM, if so, emit it.
461 if (EmitSpecialLLVMGlobal(gv))
464 MCSymbol *gvsym = Mang->getSymbol(gv);
466 assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
470 // check if it is defined in some other translation unit
471 if (gv->isDeclaration())
474 // state space: e.g., .global
476 decl += getStateSpaceName(gv->getType()->getAddressSpace());
479 // alignment (optional)
480 unsigned alignment = gv->getAlignment();
481 if (alignment != 0) {
483 decl += utostr(std::max(1U, Log2_32(gv->getAlignment())));
488 if (PointerType::classof(gv->getType())) {
489 PointerType* pointerTy = dyn_cast<PointerType>(gv->getType());
490 Type* elementTy = pointerTy->getElementType();
493 decl += gvsym->getName();
496 if (elementTy->isArrayTy())
498 assert(elementTy->isArrayTy() && "Only pointers to arrays are supported");
500 ArrayType* arrayTy = dyn_cast<ArrayType>(elementTy);
501 elementTy = arrayTy->getElementType();
503 unsigned numElements = arrayTy->getNumElements();
505 while (elementTy->isArrayTy()) {
507 arrayTy = dyn_cast<ArrayType>(elementTy);
508 elementTy = arrayTy->getElementType();
510 numElements *= arrayTy->getNumElements();
513 // FIXME: isPrimitiveType() == false for i16?
514 assert(elementTy->isSingleValueType() &&
515 "Non-primitive types are not handled");
517 // Compute the size of the array, in bytes.
518 uint64_t arraySize = (elementTy->getPrimitiveSizeInBits() >> 3)
521 decl += utostr(arraySize);
526 // handle string constants (assume ConstantArray means string)
528 if (gv->hasInitializer())
530 const Constant *C = gv->getInitializer();
531 if (const ConstantArray *CA = dyn_cast<ConstantArray>(C))
535 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
537 if (i > 0) decl += ",";
540 utohexstr(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
548 // Note: this is currently the fall-through case and most likely generates
550 decl += getTypeName(gv->getType());
553 decl += gvsym->getName();
555 if (ArrayType::classof(gv->getType()) ||
556 PointerType::classof(gv->getType()))
562 OutStreamer.EmitRawText(Twine(decl));
564 OutStreamer.AddBlankLine();
567 void PTXAsmPrinter::EmitFunctionDeclaration() {
568 // The function label could have already been emitted if two symbols end up
569 // conflicting due to asm renaming. Detect this and emit an error.
570 if (!CurrentFnSym->isUndefined()) {
571 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
572 "' label emitted multiple times to assembly file");
576 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
577 const PTXParamManager &PM = MFI->getParamManager();
578 const bool isKernel = MFI->isKernel();
579 const PTXSubtarget& ST = TM.getSubtarget<PTXSubtarget>();
580 const MachineRegisterInfo& MRI = MF->getRegInfo();
582 std::string decl = isKernel ? ".entry" : ".func";
588 if (ST.useParamSpaceForDeviceArgs()) {
589 for (PTXParamManager::param_iterator i = PM.ret_begin(), e = PM.ret_end(),
590 b = i; i != e; ++i) {
596 decl += utostr(PM.getParamSize(*i));
598 decl += PM.getParamName(*i);
601 for (PTXMachineFunctionInfo::reg_iterator
602 i = MFI->retreg_begin(), e = MFI->retreg_end(), b = i;
608 decl += getRegisterTypeName(*i, MRI);
610 decl += MFI->getRegisterName(*i);
616 // Print function name
618 decl += CurrentFnSym->getName().str();
625 if (isKernel || ST.useParamSpaceForDeviceArgs()) {
626 for (PTXParamManager::param_iterator i = PM.arg_begin(), e = PM.arg_end(),
627 b = i; i != e; ++i) {
633 decl += utostr(PM.getParamSize(*i));
635 decl += PM.getParamName(*i);
638 for (PTXMachineFunctionInfo::reg_iterator
639 i = MFI->argreg_begin(), e = MFI->argreg_end(), b = i;
646 decl += getRegisterTypeName(*i, MRI);
648 decl += MFI->getRegisterName(*i);
653 OutStreamer.EmitRawText(Twine(decl));
657 printPredicateOperand(const MachineInstr *MI, raw_ostream &O) {
658 int i = MI->findFirstPredOperandIdx();
660 llvm_unreachable("missing predicate operand");
662 unsigned reg = MI->getOperand(i).getReg();
663 int predOp = MI->getOperand(i+1).getImm();
664 const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
666 DEBUG(dbgs() << "predicate: (" << reg << ", " << predOp << ")\n");
668 if (reg != PTX::NoRegister) {
670 if (predOp == PTX::PRED_NEGATE)
672 O << MFI->getRegisterName(reg);
677 printCall(const MachineInstr *MI, raw_ostream &O) {
679 // The first two operands are the predicate slot
681 while (!MI->getOperand(Index).isGlobal()) {
687 printOperand(MI, Index, O);
695 assert(MI->getOperand(Index).isGlobal() &&
696 "A GlobalAddress must follow the return arguments");
698 const GlobalValue *Address = MI->getOperand(Index).getGlobal();
699 O << Address->getName() << ", (";
702 while (Index < MI->getNumOperands()) {
703 printOperand(MI, Index, O);
704 if (Index < MI->getNumOperands()-1) {
713 unsigned PTXAsmPrinter::GetOrCreateSourceID(StringRef FileName,
715 // If FE did not provide a file name, then assume stdin.
716 if (FileName.empty())
717 return GetOrCreateSourceID("<stdin>", StringRef());
719 // MCStream expects full path name as filename.
720 if (!DirName.empty() && !sys::path::is_absolute(FileName)) {
721 SmallString<128> FullPathName = DirName;
722 sys::path::append(FullPathName, FileName);
723 // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
724 return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
727 StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
728 if (Entry.getValue())
729 return Entry.getValue();
731 unsigned SrcId = SourceIdMap.size();
732 Entry.setValue(SrcId);
734 // Print out a .file directive to specify files for .loc directives.
735 OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
740 #include "PTXGenAsmWriter.inc"
742 // Force static initialization.
743 extern "C" void LLVMInitializePTXAsmPrinter() {
744 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTX32Target);
745 RegisterAsmPrinter<PTXAsmPrinter> Y(ThePTX64Target);