1 //===-- NVPTXAsmPrinter.cpp - NVPTX 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 NVPTX assembly language.
13 //===----------------------------------------------------------------------===//
15 #include "NVPTXAsmPrinter.h"
16 #include "MCTargetDesc/NVPTXMCAsmInfo.h"
18 #include "NVPTXInstrInfo.h"
19 #include "NVPTXMCExpr.h"
20 #include "NVPTXRegisterInfo.h"
21 #include "NVPTXTargetMachine.h"
22 #include "NVPTXUtilities.h"
23 #include "cl_common_defines.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Analysis/ConstantFolding.h"
26 #include "llvm/Assembly/Writer.h"
27 #include "llvm/CodeGen/Analysis.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/DebugInfo.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/FormattedStream.h"
42 #include "llvm/Support/Path.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TimeValue.h"
45 #include "llvm/Target/Mangler.h"
46 #include "llvm/Target/TargetLoweringObjectFile.h"
50 bool RegAllocNilUsed = true;
52 #define DEPOTNAME "__local_depot"
55 EmitLineNumbers("nvptx-emit-line-numbers",
56 cl::desc("NVPTX Specific: Emit Line numbers even without -G"),
59 namespace llvm { bool InterleaveSrcInPtx = false; }
61 static cl::opt<bool, true>
62 InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore,
63 cl::desc("NVPTX Specific: Emit source line in ptx file"),
64 cl::location(llvm::InterleaveSrcInPtx));
67 /// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V
69 void DiscoverDependentGlobals(const Value *V,
70 DenseSet<const GlobalVariable *> &Globals) {
71 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
74 if (const User *U = dyn_cast<User>(V)) {
75 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) {
76 DiscoverDependentGlobals(U->getOperand(i), Globals);
82 /// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable
83 /// instances to be emitted, but only after any dependents have been added
85 void VisitGlobalVariableForEmission(
86 const GlobalVariable *GV, SmallVectorImpl<const GlobalVariable *> &Order,
87 DenseSet<const GlobalVariable *> &Visited,
88 DenseSet<const GlobalVariable *> &Visiting) {
89 // Have we already visited this one?
90 if (Visited.count(GV))
93 // Do we have a circular dependency?
94 if (Visiting.count(GV))
95 report_fatal_error("Circular dependency found in global variable set");
97 // Start visiting this global
100 // Make sure we visit all dependents first
101 DenseSet<const GlobalVariable *> Others;
102 for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i)
103 DiscoverDependentGlobals(GV->getOperand(i), Others);
105 for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(),
108 VisitGlobalVariableForEmission(*I, Order, Visited, Visiting);
110 // Now we can visit ourself
117 // @TODO: This is a copy from AsmPrinter.cpp. The function is static, so we
118 // cannot just link to the existing version.
119 /// LowerConstant - Lower the specified LLVM Constant to an MCExpr.
121 using namespace nvptx;
122 const MCExpr *nvptx::LowerConstant(const Constant *CV, AsmPrinter &AP) {
123 MCContext &Ctx = AP.OutContext;
125 if (CV->isNullValue() || isa<UndefValue>(CV))
126 return MCConstantExpr::Create(0, Ctx);
128 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
129 return MCConstantExpr::Create(CI->getZExtValue(), Ctx);
131 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
132 return MCSymbolRefExpr::Create(AP.Mang->getSymbol(GV), Ctx);
134 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
135 return MCSymbolRefExpr::Create(AP.GetBlockAddressSymbol(BA), Ctx);
137 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
139 llvm_unreachable("Unknown constant value to lower!");
141 switch (CE->getOpcode()) {
143 // If the code isn't optimized, there may be outstanding folding
144 // opportunities. Attempt to fold the expression using DataLayout as a
145 // last resort before giving up.
146 if (Constant *C = ConstantFoldConstantExpression(CE, AP.TM.getDataLayout()))
148 return LowerConstant(C, AP);
150 // Otherwise report the problem to the user.
153 raw_string_ostream OS(S);
154 OS << "Unsupported expression in static initializer: ";
155 WriteAsOperand(OS, CE, /*PrintType=*/ false,
156 !AP.MF ? 0 : AP.MF->getFunction()->getParent());
157 report_fatal_error(OS.str());
159 case Instruction::GetElementPtr: {
160 const DataLayout &TD = *AP.TM.getDataLayout();
161 // Generate a symbolic expression for the byte address
162 APInt OffsetAI(TD.getPointerSizeInBits(), 0);
163 cast<GEPOperator>(CE)->accumulateConstantOffset(TD, OffsetAI);
165 const MCExpr *Base = LowerConstant(CE->getOperand(0), AP);
169 int64_t Offset = OffsetAI.getSExtValue();
170 return MCBinaryExpr::CreateAdd(Base, MCConstantExpr::Create(Offset, Ctx),
174 case Instruction::Trunc:
175 // We emit the value and depend on the assembler to truncate the generated
176 // expression properly. This is important for differences between
177 // blockaddress labels. Since the two labels are in the same function, it
178 // is reasonable to treat their delta as a 32-bit value.
180 case Instruction::BitCast:
181 return LowerConstant(CE->getOperand(0), AP);
183 case Instruction::IntToPtr: {
184 const DataLayout &TD = *AP.TM.getDataLayout();
185 // Handle casts to pointers by changing them into casts to the appropriate
186 // integer type. This promotes constant folding and simplifies this code.
187 Constant *Op = CE->getOperand(0);
188 Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()),
190 return LowerConstant(Op, AP);
193 case Instruction::PtrToInt: {
194 const DataLayout &TD = *AP.TM.getDataLayout();
195 // Support only foldable casts to/from pointers that can be eliminated by
196 // changing the pointer to the appropriately sized integer type.
197 Constant *Op = CE->getOperand(0);
198 Type *Ty = CE->getType();
200 const MCExpr *OpExpr = LowerConstant(Op, AP);
202 // We can emit the pointer value into this slot if the slot is an
203 // integer slot equal to the size of the pointer.
204 if (TD.getTypeAllocSize(Ty) == TD.getTypeAllocSize(Op->getType()))
207 // Otherwise the pointer is smaller than the resultant integer, mask off
208 // the high bits so we are sure to get a proper truncation if the input is
210 unsigned InBits = TD.getTypeAllocSizeInBits(Op->getType());
211 const MCExpr *MaskExpr =
212 MCConstantExpr::Create(~0ULL >> (64 - InBits), Ctx);
213 return MCBinaryExpr::CreateAnd(OpExpr, MaskExpr, Ctx);
216 // The MC library also has a right-shift operator, but it isn't consistently
217 // signed or unsigned between different targets.
218 case Instruction::Add:
219 case Instruction::Sub:
220 case Instruction::Mul:
221 case Instruction::SDiv:
222 case Instruction::SRem:
223 case Instruction::Shl:
224 case Instruction::And:
225 case Instruction::Or:
226 case Instruction::Xor: {
227 const MCExpr *LHS = LowerConstant(CE->getOperand(0), AP);
228 const MCExpr *RHS = LowerConstant(CE->getOperand(1), AP);
229 switch (CE->getOpcode()) {
231 llvm_unreachable("Unknown binary operator constant cast expr");
232 case Instruction::Add:
233 return MCBinaryExpr::CreateAdd(LHS, RHS, Ctx);
234 case Instruction::Sub:
235 return MCBinaryExpr::CreateSub(LHS, RHS, Ctx);
236 case Instruction::Mul:
237 return MCBinaryExpr::CreateMul(LHS, RHS, Ctx);
238 case Instruction::SDiv:
239 return MCBinaryExpr::CreateDiv(LHS, RHS, Ctx);
240 case Instruction::SRem:
241 return MCBinaryExpr::CreateMod(LHS, RHS, Ctx);
242 case Instruction::Shl:
243 return MCBinaryExpr::CreateShl(LHS, RHS, Ctx);
244 case Instruction::And:
245 return MCBinaryExpr::CreateAnd(LHS, RHS, Ctx);
246 case Instruction::Or:
247 return MCBinaryExpr::CreateOr(LHS, RHS, Ctx);
248 case Instruction::Xor:
249 return MCBinaryExpr::CreateXor(LHS, RHS, Ctx);
255 void NVPTXAsmPrinter::emitLineNumberAsDotLoc(const MachineInstr &MI) {
256 if (!EmitLineNumbers)
261 DebugLoc curLoc = MI.getDebugLoc();
263 if (prevDebugLoc.isUnknown() && curLoc.isUnknown())
266 if (prevDebugLoc == curLoc)
269 prevDebugLoc = curLoc;
271 if (curLoc.isUnknown())
274 const MachineFunction *MF = MI.getParent()->getParent();
275 //const TargetMachine &TM = MF->getTarget();
277 const LLVMContext &ctx = MF->getFunction()->getContext();
278 DIScope Scope(curLoc.getScope(ctx));
280 assert((!Scope || Scope.isScope()) &&
281 "Scope of a DebugLoc should be null or a DIScope.");
285 StringRef fileName(Scope.getFilename());
286 StringRef dirName(Scope.getDirectory());
287 SmallString<128> FullPathName = dirName;
288 if (!dirName.empty() && !sys::path::is_absolute(fileName)) {
289 sys::path::append(FullPathName, fileName);
290 fileName = FullPathName.str();
293 if (filenameMap.find(fileName.str()) == filenameMap.end())
296 // Emit the line from the source file.
297 if (llvm::InterleaveSrcInPtx)
298 this->emitSrcInText(fileName.str(), curLoc.getLine());
300 std::stringstream temp;
301 temp << "\t.loc " << filenameMap[fileName.str()] << " " << curLoc.getLine()
302 << " " << curLoc.getCol();
303 OutStreamer.EmitRawText(Twine(temp.str().c_str()));
306 void NVPTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
307 SmallString<128> Str;
308 raw_svector_ostream OS(Str);
309 if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)
310 emitLineNumberAsDotLoc(*MI);
313 lowerToMCInst(MI, Inst);
314 OutStreamer.EmitInstruction(Inst);
317 void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) {
318 OutMI.setOpcode(MI->getOpcode());
320 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
321 const MachineOperand &MO = MI->getOperand(i);
324 if (lowerOperand(MO, MCOp))
325 OutMI.addOperand(MCOp);
329 bool NVPTXAsmPrinter::lowerOperand(const MachineOperand &MO,
331 switch (MO.getType()) {
332 default: llvm_unreachable("unknown operand type");
333 case MachineOperand::MO_Register:
334 MCOp = MCOperand::CreateReg(encodeVirtualRegister(MO.getReg()));
336 case MachineOperand::MO_Immediate:
337 MCOp = MCOperand::CreateImm(MO.getImm());
339 case MachineOperand::MO_MachineBasicBlock:
340 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
341 MO.getMBB()->getSymbol(), OutContext));
343 case MachineOperand::MO_ExternalSymbol:
344 MCOp = GetSymbolRef(MO, GetExternalSymbolSymbol(MO.getSymbolName()));
346 case MachineOperand::MO_GlobalAddress:
347 MCOp = GetSymbolRef(MO, Mang->getSymbol(MO.getGlobal()));
349 case MachineOperand::MO_FPImmediate: {
350 const ConstantFP *Cnt = MO.getFPImm();
351 APFloat Val = Cnt->getValueAPF();
353 switch (Cnt->getType()->getTypeID()) {
354 default: report_fatal_error("Unsupported FP type"); break;
355 case Type::FloatTyID:
356 MCOp = MCOperand::CreateExpr(
357 NVPTXFloatMCExpr::CreateConstantFPSingle(Val, OutContext));
359 case Type::DoubleTyID:
360 MCOp = MCOperand::CreateExpr(
361 NVPTXFloatMCExpr::CreateConstantFPDouble(Val, OutContext));
370 unsigned NVPTXAsmPrinter::encodeVirtualRegister(unsigned Reg) {
371 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
372 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
374 DenseMap<unsigned, unsigned> &RegMap = VRegMapping[RC];
375 unsigned RegNum = RegMap[Reg];
377 // Encode the register class in the upper 4 bits
378 // Must be kept in sync with NVPTXInstPrinter::printRegName
380 if (RC == &NVPTX::Int1RegsRegClass) {
382 } else if (RC == &NVPTX::Int16RegsRegClass) {
384 } else if (RC == &NVPTX::Int32RegsRegClass) {
386 } else if (RC == &NVPTX::Int64RegsRegClass) {
388 } else if (RC == &NVPTX::Float32RegsRegClass) {
390 } else if (RC == &NVPTX::Float64RegsRegClass) {
393 report_fatal_error("Bad register class");
396 // Insert the vreg number
397 Ret |= (RegNum & 0x0FFFFFFF);
400 // Some special-use registers are actually physical registers.
401 // Encode this as the register class ID of 0 and the real register ID.
402 return Reg & 0x0FFFFFFF;
406 MCOperand NVPTXAsmPrinter::GetSymbolRef(const MachineOperand &MO,
407 const MCSymbol *Symbol) {
409 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None,
411 return MCOperand::CreateExpr(Expr);
414 void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
415 const DataLayout *TD = TM.getDataLayout();
416 const TargetLowering *TLI = TM.getTargetLowering();
418 Type *Ty = F->getReturnType();
420 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
422 if (Ty->getTypeID() == Type::VoidTyID)
428 if (Ty->isPrimitiveType() || Ty->isIntegerTy()) {
430 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
431 size = ITy->getBitWidth();
435 assert(Ty->isFloatingPointTy() && "Floating point type expected here");
436 size = Ty->getPrimitiveSizeInBits();
439 O << ".param .b" << size << " func_retval0";
440 } else if (isa<PointerType>(Ty)) {
441 O << ".param .b" << TLI->getPointerTy().getSizeInBits()
444 if ((Ty->getTypeID() == Type::StructTyID) || isa<VectorType>(Ty)) {
445 SmallVector<EVT, 16> vtparts;
446 ComputeValueVTs(*TLI, Ty, vtparts);
447 unsigned totalsz = 0;
448 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
450 EVT elemtype = vtparts[i];
451 if (vtparts[i].isVector()) {
452 elems = vtparts[i].getVectorNumElements();
453 elemtype = vtparts[i].getVectorElementType();
455 for (unsigned j = 0, je = elems; j != je; ++j) {
456 unsigned sz = elemtype.getSizeInBits();
457 if (elemtype.isInteger() && (sz < 8))
462 unsigned retAlignment = 0;
463 if (!llvm::getAlign(*F, 0, retAlignment))
464 retAlignment = TD->getABITypeAlignment(Ty);
465 O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz
468 assert(false && "Unknown return type");
471 SmallVector<EVT, 16> vtparts;
472 ComputeValueVTs(*TLI, Ty, vtparts);
474 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
476 EVT elemtype = vtparts[i];
477 if (vtparts[i].isVector()) {
478 elems = vtparts[i].getVectorNumElements();
479 elemtype = vtparts[i].getVectorElementType();
482 for (unsigned j = 0, je = elems; j != je; ++j) {
483 unsigned sz = elemtype.getSizeInBits();
484 if (elemtype.isInteger() && (sz < 32))
486 O << ".reg .b" << sz << " func_retval" << idx;
499 void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF,
501 const Function *F = MF.getFunction();
502 printReturnValStr(F, O);
505 void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
506 SmallString<128> Str;
507 raw_svector_ostream O(Str);
509 if (!GlobalsEmitted) {
510 emitGlobals(*MF->getFunction()->getParent());
511 GlobalsEmitted = true;
515 MRI = &MF->getRegInfo();
516 F = MF->getFunction();
517 emitLinkageDirective(F, O);
518 if (llvm::isKernelFunction(*F))
522 printReturnValStr(*MF, O);
527 emitFunctionParamList(*MF, O);
529 if (llvm::isKernelFunction(*F))
530 emitKernelFunctionDirectives(*F, O);
532 OutStreamer.EmitRawText(O.str());
534 prevDebugLoc = DebugLoc();
537 void NVPTXAsmPrinter::EmitFunctionBodyStart() {
539 OutStreamer.EmitRawText(StringRef("{\n"));
540 setAndEmitFunctionVirtualRegisters(*MF);
542 SmallString<128> Str;
543 raw_svector_ostream O(Str);
544 emitDemotedVars(MF->getFunction(), O);
545 OutStreamer.EmitRawText(O.str());
548 void NVPTXAsmPrinter::EmitFunctionBodyEnd() {
549 OutStreamer.EmitRawText(StringRef("}\n"));
553 void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F,
554 raw_ostream &O) const {
555 // If the NVVM IR has some of reqntid* specified, then output
556 // the reqntid directive, and set the unspecified ones to 1.
557 // If none of reqntid* is specified, don't output reqntid directive.
558 unsigned reqntidx, reqntidy, reqntidz;
559 bool specified = false;
560 if (llvm::getReqNTIDx(F, reqntidx) == false)
564 if (llvm::getReqNTIDy(F, reqntidy) == false)
568 if (llvm::getReqNTIDz(F, reqntidz) == false)
574 O << ".reqntid " << reqntidx << ", " << reqntidy << ", " << reqntidz
577 // If the NVVM IR has some of maxntid* specified, then output
578 // the maxntid directive, and set the unspecified ones to 1.
579 // If none of maxntid* is specified, don't output maxntid directive.
580 unsigned maxntidx, maxntidy, maxntidz;
582 if (llvm::getMaxNTIDx(F, maxntidx) == false)
586 if (llvm::getMaxNTIDy(F, maxntidy) == false)
590 if (llvm::getMaxNTIDz(F, maxntidz) == false)
596 O << ".maxntid " << maxntidx << ", " << maxntidy << ", " << maxntidz
600 if (llvm::getMinCTASm(F, mincta))
601 O << ".minnctapersm " << mincta << "\n";
604 void NVPTXAsmPrinter::getVirtualRegisterName(unsigned vr, bool isVec,
606 const TargetRegisterClass *RC = MRI->getRegClass(vr);
608 DenseMap<unsigned, unsigned> ®map = VRegMapping[RC];
609 unsigned mapped_vr = regmap[vr];
612 O << getNVPTXRegClassStr(RC) << mapped_vr;
615 report_fatal_error("Bad register!");
618 void NVPTXAsmPrinter::emitVirtualRegister(unsigned int vr, bool isVec,
620 getVirtualRegisterName(vr, isVec, O);
623 void NVPTXAsmPrinter::printVecModifiedImmediate(
624 const MachineOperand &MO, const char *Modifier, raw_ostream &O) {
625 static const char vecelem[] = { '0', '1', '2', '3', '0', '1', '2', '3' };
626 int Imm = (int) MO.getImm();
627 if (0 == strcmp(Modifier, "vecelem"))
628 O << "_" << vecelem[Imm];
629 else if (0 == strcmp(Modifier, "vecv4comm1")) {
630 if ((Imm < 0) || (Imm > 3))
632 } else if (0 == strcmp(Modifier, "vecv4comm2")) {
633 if ((Imm < 4) || (Imm > 7))
635 } else if (0 == strcmp(Modifier, "vecv4pos")) {
638 O << "_" << vecelem[Imm % 4];
639 } else if (0 == strcmp(Modifier, "vecv2comm1")) {
640 if ((Imm < 0) || (Imm > 1))
642 } else if (0 == strcmp(Modifier, "vecv2comm2")) {
643 if ((Imm < 2) || (Imm > 3))
645 } else if (0 == strcmp(Modifier, "vecv2pos")) {
648 O << "_" << vecelem[Imm % 2];
650 llvm_unreachable("Unknown Modifier on immediate operand");
655 void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) {
657 emitLinkageDirective(F, O);
658 if (llvm::isKernelFunction(*F))
662 printReturnValStr(F, O);
663 O << *Mang->getSymbol(F) << "\n";
664 emitFunctionParamList(F, O);
668 static bool usedInGlobalVarDef(const Constant *C) {
672 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
673 if (GV->getName().str() == "llvm.used")
678 for (Value::const_use_iterator ui = C->use_begin(), ue = C->use_end();
680 const Constant *C = dyn_cast<Constant>(*ui);
681 if (usedInGlobalVarDef(C))
687 static bool usedInOneFunc(const User *U, Function const *&oneFunc) {
688 if (const GlobalVariable *othergv = dyn_cast<GlobalVariable>(U)) {
689 if (othergv->getName().str() == "llvm.used")
693 if (const Instruction *instr = dyn_cast<Instruction>(U)) {
694 if (instr->getParent() && instr->getParent()->getParent()) {
695 const Function *curFunc = instr->getParent()->getParent();
696 if (oneFunc && (curFunc != oneFunc))
704 if (const MDNode *md = dyn_cast<MDNode>(U))
705 if (md->hasName() && ((md->getName().str() == "llvm.dbg.gv") ||
706 (md->getName().str() == "llvm.dbg.sp")))
709 for (User::const_use_iterator ui = U->use_begin(), ue = U->use_end();
711 if (usedInOneFunc(*ui, oneFunc) == false)
717 /* Find out if a global variable can be demoted to local scope.
718 * Currently, this is valid for CUDA shared variables, which have local
719 * scope and global lifetime. So the conditions to check are :
720 * 1. Is the global variable in shared address space?
721 * 2. Does it have internal linkage?
722 * 3. Is the global variable referenced only in one function?
724 static bool canDemoteGlobalVar(const GlobalVariable *gv, Function const *&f) {
725 if (gv->hasInternalLinkage() == false)
727 const PointerType *Pty = gv->getType();
728 if (Pty->getAddressSpace() != llvm::ADDRESS_SPACE_SHARED)
731 const Function *oneFunc = 0;
733 bool flag = usedInOneFunc(gv, oneFunc);
742 static bool useFuncSeen(const Constant *C,
743 llvm::DenseMap<const Function *, bool> &seenMap) {
744 for (Value::const_use_iterator ui = C->use_begin(), ue = C->use_end();
746 if (const Constant *cu = dyn_cast<Constant>(*ui)) {
747 if (useFuncSeen(cu, seenMap))
749 } else if (const Instruction *I = dyn_cast<Instruction>(*ui)) {
750 const BasicBlock *bb = I->getParent();
753 const Function *caller = bb->getParent();
756 if (seenMap.find(caller) != seenMap.end())
763 void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
764 llvm::DenseMap<const Function *, bool> seenMap;
765 for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
766 const Function *F = FI;
768 if (F->isDeclaration()) {
771 if (F->getIntrinsicID())
773 emitDeclaration(F, O);
776 for (Value::const_use_iterator iter = F->use_begin(),
777 iterEnd = F->use_end();
778 iter != iterEnd; ++iter) {
779 if (const Constant *C = dyn_cast<Constant>(*iter)) {
780 if (usedInGlobalVarDef(C)) {
781 // The use is in the initialization of a global variable
782 // that is a function pointer, so print a declaration
783 // for the original function
784 emitDeclaration(F, O);
787 // Emit a declaration of this function if the function that
788 // uses this constant expr has already been seen.
789 if (useFuncSeen(C, seenMap)) {
790 emitDeclaration(F, O);
795 if (!isa<Instruction>(*iter))
797 const Instruction *instr = cast<Instruction>(*iter);
798 const BasicBlock *bb = instr->getParent();
801 const Function *caller = bb->getParent();
805 // If a caller has already been seen, then the caller is
806 // appearing in the module before the callee. so print out
807 // a declaration for the callee.
808 if (seenMap.find(caller) != seenMap.end()) {
809 emitDeclaration(F, O);
817 void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) {
818 DebugInfoFinder DbgFinder;
819 DbgFinder.processModule(M);
822 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
823 E = DbgFinder.compile_unit_end();
825 DICompileUnit DIUnit(*I);
826 StringRef Filename(DIUnit.getFilename());
827 StringRef Dirname(DIUnit.getDirectory());
828 SmallString<128> FullPathName = Dirname;
829 if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
830 sys::path::append(FullPathName, Filename);
831 Filename = FullPathName.str();
833 if (filenameMap.find(Filename.str()) != filenameMap.end())
835 filenameMap[Filename.str()] = i;
836 OutStreamer.EmitDwarfFileDirective(i, "", Filename.str());
840 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
841 E = DbgFinder.subprogram_end();
844 StringRef Filename(SP.getFilename());
845 StringRef Dirname(SP.getDirectory());
846 SmallString<128> FullPathName = Dirname;
847 if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
848 sys::path::append(FullPathName, Filename);
849 Filename = FullPathName.str();
851 if (filenameMap.find(Filename.str()) != filenameMap.end())
853 filenameMap[Filename.str()] = i;
858 bool NVPTXAsmPrinter::doInitialization(Module &M) {
860 SmallString<128> Str1;
861 raw_svector_ostream OS1(Str1);
863 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
864 MMI->AnalyzeModule(M);
866 // We need to call the parent's one explicitly.
867 //bool Result = AsmPrinter::doInitialization(M);
869 // Initialize TargetLoweringObjectFile.
870 const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
871 .Initialize(OutContext, TM);
873 Mang = new Mangler(OutContext, &TM);
875 // Emit header before any dwarf directives are emitted below.
877 OutStreamer.EmitRawText(OS1.str());
879 // Already commented out
880 //bool Result = AsmPrinter::doInitialization(M);
882 // Emit module-level inline asm if it exists.
883 if (!M.getModuleInlineAsm().empty()) {
884 OutStreamer.AddComment("Start of file scope inline assembly");
885 OutStreamer.AddBlankLine();
886 OutStreamer.EmitRawText(StringRef(M.getModuleInlineAsm()));
887 OutStreamer.AddBlankLine();
888 OutStreamer.AddComment("End of file scope inline assembly");
889 OutStreamer.AddBlankLine();
892 if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)
893 recordAndEmitFilenames(M);
895 GlobalsEmitted = false;
897 return false; // success
900 void NVPTXAsmPrinter::emitGlobals(const Module &M) {
901 SmallString<128> Str2;
902 raw_svector_ostream OS2(Str2);
904 emitDeclarations(M, OS2);
906 // As ptxas does not support forward references of globals, we need to first
907 // sort the list of module-level globals in def-use order. We visit each
908 // global variable in order, and ensure that we emit it *after* its dependent
909 // globals. We use a little extra memory maintaining both a set and a list to
910 // have fast searches while maintaining a strict ordering.
911 SmallVector<const GlobalVariable *, 8> Globals;
912 DenseSet<const GlobalVariable *> GVVisited;
913 DenseSet<const GlobalVariable *> GVVisiting;
915 // Visit each global variable, in order
916 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
918 VisitGlobalVariableForEmission(I, Globals, GVVisited, GVVisiting);
920 assert(GVVisited.size() == M.getGlobalList().size() &&
921 "Missed a global variable");
922 assert(GVVisiting.size() == 0 && "Did not fully process a global variable");
924 // Print out module-level global variables in proper order
925 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
926 printModuleLevelGV(Globals[i], OS2);
930 OutStreamer.EmitRawText(OS2.str());
933 void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O) {
935 O << "// Generated by LLVM NVPTX Back-End\n";
939 unsigned PTXVersion = nvptxSubtarget.getPTXVersion();
940 O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n";
943 O << nvptxSubtarget.getTargetName();
945 if (nvptxSubtarget.getDrvInterface() == NVPTX::NVCL)
946 O << ", texmode_independent";
947 if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA) {
948 if (!nvptxSubtarget.hasDouble())
949 O << ", map_f64_to_f32";
952 if (MAI->doesSupportDebugInformation())
957 O << ".address_size ";
958 if (nvptxSubtarget.is64Bit())
967 bool NVPTXAsmPrinter::doFinalization(Module &M) {
969 // If we did not emit any functions, then the global declarations have not
971 if (!GlobalsEmitted) {
973 GlobalsEmitted = true;
976 // XXX Temproarily remove global variables so that doFinalization() will not
977 // emit them again (global variables are emitted at beginning).
979 Module::GlobalListType &global_list = M.getGlobalList();
980 int i, n = global_list.size();
981 GlobalVariable **gv_array = new GlobalVariable *[n];
983 // first, back-up GlobalVariable in gv_array
985 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
989 // second, empty global_list
990 while (!global_list.empty())
991 global_list.remove(global_list.begin());
993 // call doFinalization
994 bool ret = AsmPrinter::doFinalization(M);
996 // now we restore global variables
997 for (i = 0; i < n; i++)
998 global_list.insert(global_list.end(), gv_array[i]);
1003 //bool Result = AsmPrinter::doFinalization(M);
1004 // Instead of calling the parents doFinalization, we may
1005 // clone parents doFinalization and customize here.
1006 // Currently, we if NVISA out the EmitGlobals() in
1007 // parent's doFinalization, which is too intrusive.
1009 // Same for the doInitialization.
1013 // This function emits appropriate linkage directives for
1014 // functions and global variables.
1016 // extern function declaration -> .extern
1017 // extern function definition -> .visible
1018 // external global variable with init -> .visible
1019 // external without init -> .extern
1020 // appending -> not allowed, assert.
1022 void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V,
1024 if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA) {
1025 if (V->hasExternalLinkage()) {
1026 if (isa<GlobalVariable>(V)) {
1027 const GlobalVariable *GVar = cast<GlobalVariable>(V);
1029 if (GVar->hasInitializer())
1034 } else if (V->isDeclaration())
1038 } else if (V->hasAppendingLinkage()) {
1040 msg.append("Error: ");
1041 msg.append("Symbol ");
1043 msg.append(V->getName().str());
1044 msg.append("has unsupported appending linkage type");
1045 llvm_unreachable(msg.c_str());
1050 void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
1052 bool processDemoted) {
1055 if (GVar->hasSection()) {
1056 if (GVar->getSection() == "llvm.metadata")
1060 const DataLayout *TD = TM.getDataLayout();
1062 // GlobalVariables are always constant pointers themselves.
1063 const PointerType *PTy = GVar->getType();
1064 Type *ETy = PTy->getElementType();
1066 if (GVar->hasExternalLinkage()) {
1067 if (GVar->hasInitializer())
1073 if (llvm::isTexture(*GVar)) {
1074 O << ".global .texref " << llvm::getTextureName(*GVar) << ";\n";
1078 if (llvm::isSurface(*GVar)) {
1079 O << ".global .surfref " << llvm::getSurfaceName(*GVar) << ";\n";
1083 if (GVar->isDeclaration()) {
1084 // (extern) declarations, no definition or initializer
1085 // Currently the only known declaration is for an automatic __local
1086 // (.shared) promoted to global.
1087 emitPTXGlobalVariable(GVar, O);
1092 if (llvm::isSampler(*GVar)) {
1093 O << ".global .samplerref " << llvm::getSamplerName(*GVar);
1095 const Constant *Initializer = NULL;
1096 if (GVar->hasInitializer())
1097 Initializer = GVar->getInitializer();
1098 const ConstantInt *CI = NULL;
1100 CI = dyn_cast<ConstantInt>(Initializer);
1102 unsigned sample = CI->getZExtValue();
1107 addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE);
1109 O << "addr_mode_" << i << " = ";
1115 O << "clamp_to_border";
1118 O << "clamp_to_edge";
1129 O << "filter_mode = ";
1130 switch ((sample & __CLK_FILTER_MASK) >> __CLK_FILTER_BASE) {
1138 assert(0 && "Anisotropic filtering is not supported");
1143 if (!((sample & __CLK_NORMALIZED_MASK) >> __CLK_NORMALIZED_BASE)) {
1144 O << ", force_unnormalized_coords = 1";
1153 if (GVar->hasPrivateLinkage()) {
1155 if (!strncmp(GVar->getName().data(), "unrollpragma", 12))
1158 // FIXME - need better way (e.g. Metadata) to avoid generating this global
1159 if (!strncmp(GVar->getName().data(), "filename", 8))
1161 if (GVar->use_empty())
1165 const Function *demotedFunc = 0;
1166 if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) {
1167 O << "// " << GVar->getName().str() << " has been demoted\n";
1168 if (localDecls.find(demotedFunc) != localDecls.end())
1169 localDecls[demotedFunc].push_back(GVar);
1171 std::vector<const GlobalVariable *> temp;
1172 temp.push_back(GVar);
1173 localDecls[demotedFunc] = temp;
1179 emitPTXAddressSpace(PTy->getAddressSpace(), O);
1180 if (GVar->getAlignment() == 0)
1181 O << " .align " << (int) TD->getPrefTypeAlignment(ETy);
1183 O << " .align " << GVar->getAlignment();
1185 if (ETy->isPrimitiveType() || ETy->isIntegerTy() || isa<PointerType>(ETy)) {
1187 // Special case: ABI requires that we use .u8 for predicates
1188 if (ETy->isIntegerTy(1))
1191 O << getPTXFundamentalTypeStr(ETy, false);
1193 O << *Mang->getSymbol(GVar);
1195 // Ptx allows variable initilization only for constant and global state
1197 if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) ||
1198 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) &&
1199 GVar->hasInitializer()) {
1200 const Constant *Initializer = GVar->getInitializer();
1201 if (!Initializer->isNullValue()) {
1203 printScalarConstant(Initializer, O);
1207 unsigned int ElementSize = 0;
1209 // Although PTX has direct support for struct type and array type and
1210 // LLVM IR is very similar to PTX, the LLVM CodeGen does not support for
1211 // targets that support these high level field accesses. Structs, arrays
1212 // and vectors are lowered into arrays of bytes.
1213 switch (ETy->getTypeID()) {
1214 case Type::StructTyID:
1215 case Type::ArrayTyID:
1216 case Type::VectorTyID:
1217 ElementSize = TD->getTypeStoreSize(ETy);
1218 // Ptx allows variable initilization only for constant and
1219 // global state spaces.
1220 if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) ||
1221 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) &&
1222 GVar->hasInitializer()) {
1223 const Constant *Initializer = GVar->getInitializer();
1224 if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) {
1225 AggBuffer aggBuffer(ElementSize, O, *this);
1226 bufferAggregateConstant(Initializer, &aggBuffer);
1227 if (aggBuffer.numSymbols) {
1228 if (nvptxSubtarget.is64Bit()) {
1229 O << " .u64 " << *Mang->getSymbol(GVar) << "[";
1230 O << ElementSize / 8;
1232 O << " .u32 " << *Mang->getSymbol(GVar) << "[";
1233 O << ElementSize / 4;
1237 O << " .b8 " << *Mang->getSymbol(GVar) << "[";
1245 O << " .b8 " << *Mang->getSymbol(GVar);
1253 O << " .b8 " << *Mang->getSymbol(GVar);
1262 assert(0 && "type not supported yet");
1269 void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) {
1270 if (localDecls.find(f) == localDecls.end())
1273 std::vector<const GlobalVariable *> &gvars = localDecls[f];
1275 for (unsigned i = 0, e = gvars.size(); i != e; ++i) {
1276 O << "\t// demoted variable\n\t";
1277 printModuleLevelGV(gvars[i], O, true);
1281 void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace,
1282 raw_ostream &O) const {
1283 switch (AddressSpace) {
1284 case llvm::ADDRESS_SPACE_LOCAL:
1287 case llvm::ADDRESS_SPACE_GLOBAL:
1290 case llvm::ADDRESS_SPACE_CONST:
1293 case llvm::ADDRESS_SPACE_SHARED:
1297 report_fatal_error("Bad address space found while emitting PTX");
1303 NVPTXAsmPrinter::getPTXFundamentalTypeStr(const Type *Ty, bool useB4PTR) const {
1304 switch (Ty->getTypeID()) {
1306 llvm_unreachable("unexpected type");
1308 case Type::IntegerTyID: {
1309 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth();
1312 else if (NumBits <= 64) {
1313 std::string name = "u";
1314 return name + utostr(NumBits);
1316 llvm_unreachable("Integer too large");
1321 case Type::FloatTyID:
1323 case Type::DoubleTyID:
1325 case Type::PointerTyID:
1326 if (nvptxSubtarget.is64Bit())
1336 llvm_unreachable("unexpected type");
1340 void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar,
1343 const DataLayout *TD = TM.getDataLayout();
1345 // GlobalVariables are always constant pointers themselves.
1346 const PointerType *PTy = GVar->getType();
1347 Type *ETy = PTy->getElementType();
1350 emitPTXAddressSpace(PTy->getAddressSpace(), O);
1351 if (GVar->getAlignment() == 0)
1352 O << " .align " << (int) TD->getPrefTypeAlignment(ETy);
1354 O << " .align " << GVar->getAlignment();
1356 if (ETy->isPrimitiveType() || ETy->isIntegerTy() || isa<PointerType>(ETy)) {
1358 O << getPTXFundamentalTypeStr(ETy);
1360 O << *Mang->getSymbol(GVar);
1364 int64_t ElementSize = 0;
1366 // Although PTX has direct support for struct type and array type and LLVM IR
1367 // is very similar to PTX, the LLVM CodeGen does not support for targets that
1368 // support these high level field accesses. Structs and arrays are lowered
1369 // into arrays of bytes.
1370 switch (ETy->getTypeID()) {
1371 case Type::StructTyID:
1372 case Type::ArrayTyID:
1373 case Type::VectorTyID:
1374 ElementSize = TD->getTypeStoreSize(ETy);
1375 O << " .b8 " << *Mang->getSymbol(GVar) << "[";
1377 O << itostr(ElementSize);
1382 assert(0 && "type not supported yet");
1387 static unsigned int getOpenCLAlignment(const DataLayout *TD, Type *Ty) {
1388 if (Ty->isPrimitiveType() || Ty->isIntegerTy() || isa<PointerType>(Ty))
1389 return TD->getPrefTypeAlignment(Ty);
1391 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1393 return getOpenCLAlignment(TD, ATy->getElementType());
1395 const VectorType *VTy = dyn_cast<VectorType>(Ty);
1397 Type *ETy = VTy->getElementType();
1398 unsigned int numE = VTy->getNumElements();
1399 unsigned int alignE = TD->getPrefTypeAlignment(ETy);
1403 return numE * alignE;
1406 const StructType *STy = dyn_cast<StructType>(Ty);
1408 unsigned int alignStruct = 1;
1409 // Go through each element of the struct and find the
1410 // largest alignment.
1411 for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
1412 Type *ETy = STy->getElementType(i);
1413 unsigned int align = getOpenCLAlignment(TD, ETy);
1414 if (align > alignStruct)
1415 alignStruct = align;
1420 const FunctionType *FTy = dyn_cast<FunctionType>(Ty);
1422 return TD->getPointerPrefAlignment();
1423 return TD->getPrefTypeAlignment(Ty);
1426 void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I,
1427 int paramIndex, raw_ostream &O) {
1428 if ((nvptxSubtarget.getDrvInterface() == NVPTX::NVCL) ||
1429 (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA))
1430 O << *Mang->getSymbol(I->getParent()) << "_param_" << paramIndex;
1432 std::string argName = I->getName();
1433 const char *p = argName.c_str();
1444 void NVPTXAsmPrinter::printParamName(int paramIndex, raw_ostream &O) {
1445 Function::const_arg_iterator I, E;
1448 if ((nvptxSubtarget.getDrvInterface() == NVPTX::NVCL) ||
1449 (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)) {
1450 O << *CurrentFnSym << "_param_" << paramIndex;
1454 for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, i++) {
1455 if (i == paramIndex) {
1456 printParamName(I, paramIndex, O);
1460 llvm_unreachable("paramIndex out of bound");
1463 void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
1464 const DataLayout *TD = TM.getDataLayout();
1465 const AttributeSet &PAL = F->getAttributes();
1466 const TargetLowering *TLI = TM.getTargetLowering();
1467 Function::const_arg_iterator I, E;
1468 unsigned paramIndex = 0;
1470 bool isKernelFunc = llvm::isKernelFunction(*F);
1471 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
1472 MVT thePointerTy = TLI->getPointerTy();
1476 for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, paramIndex++) {
1477 Type *Ty = I->getType();
1484 // Handle image/sampler parameters
1485 if (llvm::isSampler(*I) || llvm::isImage(*I)) {
1486 if (llvm::isImage(*I)) {
1487 std::string sname = I->getName();
1488 if (llvm::isImageWriteOnly(*I))
1489 O << "\t.param .surfref " << *Mang->getSymbol(F) << "_param_"
1491 else // Default image is read_only
1492 O << "\t.param .texref " << *Mang->getSymbol(F) << "_param_"
1494 } else // Should be llvm::isSampler(*I)
1495 O << "\t.param .samplerref " << *Mang->getSymbol(F) << "_param_"
1500 if (PAL.hasAttribute(paramIndex + 1, Attribute::ByVal) == false) {
1501 if (Ty->isVectorTy()) {
1502 // Just print .param .b8 .align <a> .param[size];
1503 // <a> = PAL.getparamalignment
1504 // size = typeallocsize of element type
1505 unsigned align = PAL.getParamAlignment(paramIndex + 1);
1507 align = TD->getABITypeAlignment(Ty);
1509 unsigned sz = TD->getTypeAllocSize(Ty);
1510 O << "\t.param .align " << align << " .b8 ";
1511 printParamName(I, paramIndex, O);
1512 O << "[" << sz << "]";
1517 const PointerType *PTy = dyn_cast<PointerType>(Ty);
1520 // Special handling for pointer arguments to kernel
1521 O << "\t.param .u" << thePointerTy.getSizeInBits() << " ";
1523 if (nvptxSubtarget.getDrvInterface() != NVPTX::CUDA) {
1524 Type *ETy = PTy->getElementType();
1525 int addrSpace = PTy->getAddressSpace();
1526 switch (addrSpace) {
1530 case llvm::ADDRESS_SPACE_CONST:
1531 O << ".ptr .const ";
1533 case llvm::ADDRESS_SPACE_SHARED:
1534 O << ".ptr .shared ";
1536 case llvm::ADDRESS_SPACE_GLOBAL:
1537 O << ".ptr .global ";
1540 O << ".align " << (int) getOpenCLAlignment(TD, ETy) << " ";
1542 printParamName(I, paramIndex, O);
1546 // non-pointer scalar to kernel func
1548 // Special case: predicate operands become .u8 types
1549 if (Ty->isIntegerTy(1))
1552 O << getPTXFundamentalTypeStr(Ty);
1554 printParamName(I, paramIndex, O);
1557 // Non-kernel function, just print .param .b<size> for ABI
1558 // and .reg .b<size> for non ABY
1560 if (isa<IntegerType>(Ty)) {
1561 sz = cast<IntegerType>(Ty)->getBitWidth();
1564 } else if (isa<PointerType>(Ty))
1565 sz = thePointerTy.getSizeInBits();
1567 sz = Ty->getPrimitiveSizeInBits();
1569 O << "\t.param .b" << sz << " ";
1571 O << "\t.reg .b" << sz << " ";
1572 printParamName(I, paramIndex, O);
1576 // param has byVal attribute. So should be a pointer
1577 const PointerType *PTy = dyn_cast<PointerType>(Ty);
1578 assert(PTy && "Param with byval attribute should be a pointer type");
1579 Type *ETy = PTy->getElementType();
1581 if (isABI || isKernelFunc) {
1582 // Just print .param .b8 .align <a> .param[size];
1583 // <a> = PAL.getparamalignment
1584 // size = typeallocsize of element type
1585 unsigned align = PAL.getParamAlignment(paramIndex + 1);
1587 align = TD->getABITypeAlignment(ETy);
1589 unsigned sz = TD->getTypeAllocSize(ETy);
1590 O << "\t.param .align " << align << " .b8 ";
1591 printParamName(I, paramIndex, O);
1592 O << "[" << sz << "]";
1595 // Split the ETy into constituent parts and
1596 // print .param .b<size> <name> for each part.
1597 // Further, if a part is vector, print the above for
1598 // each vector element.
1599 SmallVector<EVT, 16> vtparts;
1600 ComputeValueVTs(*TLI, ETy, vtparts);
1601 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
1603 EVT elemtype = vtparts[i];
1604 if (vtparts[i].isVector()) {
1605 elems = vtparts[i].getVectorNumElements();
1606 elemtype = vtparts[i].getVectorElementType();
1609 for (unsigned j = 0, je = elems; j != je; ++j) {
1610 unsigned sz = elemtype.getSizeInBits();
1611 if (elemtype.isInteger() && (sz < 32))
1613 O << "\t.reg .b" << sz << " ";
1614 printParamName(I, paramIndex, O);
1630 void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF,
1632 const Function *F = MF.getFunction();
1633 emitFunctionParamList(F, O);
1636 void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters(
1637 const MachineFunction &MF) {
1638 SmallString<128> Str;
1639 raw_svector_ostream O(Str);
1641 // Map the global virtual register number to a register class specific
1642 // virtual register number starting from 1 with that class.
1643 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
1644 //unsigned numRegClasses = TRI->getNumRegClasses();
1646 // Emit the Fake Stack Object
1647 const MachineFrameInfo *MFI = MF.getFrameInfo();
1648 int NumBytes = (int) MFI->getStackSize();
1650 O << "\t.local .align " << MFI->getMaxAlignment() << " .b8 \t" << DEPOTNAME
1651 << getFunctionNumber() << "[" << NumBytes << "];\n";
1652 if (nvptxSubtarget.is64Bit()) {
1653 O << "\t.reg .b64 \t%SP;\n";
1654 O << "\t.reg .b64 \t%SPL;\n";
1656 O << "\t.reg .b32 \t%SP;\n";
1657 O << "\t.reg .b32 \t%SPL;\n";
1661 // Go through all virtual registers to establish the mapping between the
1663 // register number and the per class virtual register number.
1664 // We use the per class virtual register number in the ptx output.
1665 unsigned int numVRs = MRI->getNumVirtRegs();
1666 for (unsigned i = 0; i < numVRs; i++) {
1667 unsigned int vr = TRI->index2VirtReg(i);
1668 const TargetRegisterClass *RC = MRI->getRegClass(vr);
1669 DenseMap<unsigned, unsigned> ®map = VRegMapping[RC];
1670 int n = regmap.size();
1671 regmap.insert(std::make_pair(vr, n + 1));
1674 // Emit register declarations
1675 // @TODO: Extract out the real register usage
1676 // O << "\t.reg .pred %p<" << NVPTXNumRegisters << ">;\n";
1677 // O << "\t.reg .s16 %rc<" << NVPTXNumRegisters << ">;\n";
1678 // O << "\t.reg .s16 %rs<" << NVPTXNumRegisters << ">;\n";
1679 // O << "\t.reg .s32 %r<" << NVPTXNumRegisters << ">;\n";
1680 // O << "\t.reg .s64 %rl<" << NVPTXNumRegisters << ">;\n";
1681 // O << "\t.reg .f32 %f<" << NVPTXNumRegisters << ">;\n";
1682 // O << "\t.reg .f64 %fl<" << NVPTXNumRegisters << ">;\n";
1684 // Emit declaration of the virtual registers or 'physical' registers for
1685 // each register class
1686 for (unsigned i=0; i< TRI->getNumRegClasses(); i++) {
1687 const TargetRegisterClass *RC = TRI->getRegClass(i);
1688 DenseMap<unsigned, unsigned> ®map = VRegMapping[RC];
1689 std::string rcname = getNVPTXRegClassName(RC);
1690 std::string rcStr = getNVPTXRegClassStr(RC);
1691 int n = regmap.size();
1693 // Only declare those registers that may be used.
1695 O << "\t.reg " << rcname << " \t" << rcStr << "<" << (n+1)
1700 OutStreamer.EmitRawText(O.str());
1703 void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) {
1704 APFloat APF = APFloat(Fp->getValueAPF()); // make a copy
1706 unsigned int numHex;
1709 if (Fp->getType()->getTypeID() == Type::FloatTyID) {
1712 APF.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &ignored);
1713 } else if (Fp->getType()->getTypeID() == Type::DoubleTyID) {
1716 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
1718 llvm_unreachable("unsupported fp type");
1720 APInt API = APF.bitcastToAPInt();
1721 std::string hexstr(utohexstr(API.getZExtValue()));
1723 if (hexstr.length() < numHex)
1724 O << std::string(numHex - hexstr.length(), '0');
1725 O << utohexstr(API.getZExtValue());
1728 void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
1729 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) {
1730 O << CI->getValue();
1733 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
1734 printFPConstant(CFP, O);
1737 if (isa<ConstantPointerNull>(CPV)) {
1741 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
1742 O << *Mang->getSymbol(GVar);
1745 if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1746 const Value *v = Cexpr->stripPointerCasts();
1747 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
1748 O << *Mang->getSymbol(GVar);
1751 O << *LowerConstant(CPV, *this);
1755 llvm_unreachable("Not scalar type found in printScalarConstant()");
1758 void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
1759 AggBuffer *aggBuffer) {
1761 const DataLayout *TD = TM.getDataLayout();
1763 if (isa<UndefValue>(CPV) || CPV->isNullValue()) {
1764 int s = TD->getTypeAllocSize(CPV->getType());
1767 aggBuffer->addZeros(s);
1772 switch (CPV->getType()->getTypeID()) {
1774 case Type::IntegerTyID: {
1775 const Type *ETy = CPV->getType();
1776 if (ETy == Type::getInt8Ty(CPV->getContext())) {
1778 (unsigned char)(dyn_cast<ConstantInt>(CPV))->getZExtValue();
1780 aggBuffer->addBytes(ptr, 1, Bytes);
1781 } else if (ETy == Type::getInt16Ty(CPV->getContext())) {
1782 short int16 = (short)(dyn_cast<ConstantInt>(CPV))->getZExtValue();
1783 ptr = (unsigned char *)&int16;
1784 aggBuffer->addBytes(ptr, 2, Bytes);
1785 } else if (ETy == Type::getInt32Ty(CPV->getContext())) {
1786 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1787 int int32 = (int)(constInt->getZExtValue());
1788 ptr = (unsigned char *)&int32;
1789 aggBuffer->addBytes(ptr, 4, Bytes);
1791 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1792 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
1793 ConstantFoldConstantExpression(Cexpr, TD))) {
1794 int int32 = (int)(constInt->getZExtValue());
1795 ptr = (unsigned char *)&int32;
1796 aggBuffer->addBytes(ptr, 4, Bytes);
1799 if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1800 Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1801 aggBuffer->addSymbol(v);
1802 aggBuffer->addZeros(4);
1806 llvm_unreachable("unsupported integer const type");
1807 } else if (ETy == Type::getInt64Ty(CPV->getContext())) {
1808 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1809 long long int64 = (long long)(constInt->getZExtValue());
1810 ptr = (unsigned char *)&int64;
1811 aggBuffer->addBytes(ptr, 8, Bytes);
1813 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1814 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
1815 ConstantFoldConstantExpression(Cexpr, TD))) {
1816 long long int64 = (long long)(constInt->getZExtValue());
1817 ptr = (unsigned char *)&int64;
1818 aggBuffer->addBytes(ptr, 8, Bytes);
1821 if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1822 Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1823 aggBuffer->addSymbol(v);
1824 aggBuffer->addZeros(8);
1828 llvm_unreachable("unsupported integer const type");
1830 llvm_unreachable("unsupported integer const type");
1833 case Type::FloatTyID:
1834 case Type::DoubleTyID: {
1835 const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV);
1836 const Type *Ty = CFP->getType();
1837 if (Ty == Type::getFloatTy(CPV->getContext())) {
1838 float float32 = (float) CFP->getValueAPF().convertToFloat();
1839 ptr = (unsigned char *)&float32;
1840 aggBuffer->addBytes(ptr, 4, Bytes);
1841 } else if (Ty == Type::getDoubleTy(CPV->getContext())) {
1842 double float64 = CFP->getValueAPF().convertToDouble();
1843 ptr = (unsigned char *)&float64;
1844 aggBuffer->addBytes(ptr, 8, Bytes);
1846 llvm_unreachable("unsupported fp const type");
1850 case Type::PointerTyID: {
1851 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
1852 aggBuffer->addSymbol(GVar);
1853 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1854 const Value *v = Cexpr->stripPointerCasts();
1855 aggBuffer->addSymbol(v);
1857 unsigned int s = TD->getTypeAllocSize(CPV->getType());
1858 aggBuffer->addZeros(s);
1862 case Type::ArrayTyID:
1863 case Type::VectorTyID:
1864 case Type::StructTyID: {
1865 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV) ||
1866 isa<ConstantStruct>(CPV)) {
1867 int ElementSize = TD->getTypeAllocSize(CPV->getType());
1868 bufferAggregateConstant(CPV, aggBuffer);
1869 if (Bytes > ElementSize)
1870 aggBuffer->addZeros(Bytes - ElementSize);
1871 } else if (isa<ConstantAggregateZero>(CPV))
1872 aggBuffer->addZeros(Bytes);
1874 llvm_unreachable("Unexpected Constant type");
1879 llvm_unreachable("unsupported type");
1883 void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV,
1884 AggBuffer *aggBuffer) {
1885 const DataLayout *TD = TM.getDataLayout();
1889 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) {
1890 if (CPV->getNumOperands())
1891 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i)
1892 bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer);
1896 if (const ConstantDataSequential *CDS =
1897 dyn_cast<ConstantDataSequential>(CPV)) {
1898 if (CDS->getNumElements())
1899 for (unsigned i = 0; i < CDS->getNumElements(); ++i)
1900 bufferLEByte(cast<Constant>(CDS->getElementAsConstant(i)), 0,
1905 if (isa<ConstantStruct>(CPV)) {
1906 if (CPV->getNumOperands()) {
1907 StructType *ST = cast<StructType>(CPV->getType());
1908 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) {
1910 Bytes = TD->getStructLayout(ST)->getElementOffset(0) +
1911 TD->getTypeAllocSize(ST) -
1912 TD->getStructLayout(ST)->getElementOffset(i);
1914 Bytes = TD->getStructLayout(ST)->getElementOffset(i + 1) -
1915 TD->getStructLayout(ST)->getElementOffset(i);
1916 bufferLEByte(cast<Constant>(CPV->getOperand(i)), Bytes, aggBuffer);
1921 llvm_unreachable("unsupported constant type in printAggregateConstant()");
1924 // buildTypeNameMap - Run through symbol table looking for type names.
1927 bool NVPTXAsmPrinter::isImageType(const Type *Ty) {
1929 std::map<const Type *, std::string>::iterator PI = TypeNameMap.find(Ty);
1931 if (PI != TypeNameMap.end() && (!PI->second.compare("struct._image1d_t") ||
1932 !PI->second.compare("struct._image2d_t") ||
1933 !PI->second.compare("struct._image3d_t")))
1940 bool NVPTXAsmPrinter::ignoreLoc(const MachineInstr &MI) {
1941 switch (MI.getOpcode()) {
1944 case NVPTX::CallArgBeginInst:
1945 case NVPTX::CallArgEndInst0:
1946 case NVPTX::CallArgEndInst1:
1947 case NVPTX::CallArgF32:
1948 case NVPTX::CallArgF64:
1949 case NVPTX::CallArgI16:
1950 case NVPTX::CallArgI32:
1951 case NVPTX::CallArgI32imm:
1952 case NVPTX::CallArgI64:
1953 case NVPTX::CallArgParam:
1954 case NVPTX::CallVoidInst:
1955 case NVPTX::CallVoidInstReg:
1956 case NVPTX::Callseq_End:
1957 case NVPTX::CallVoidInstReg64:
1958 case NVPTX::DeclareParamInst:
1959 case NVPTX::DeclareRetMemInst:
1960 case NVPTX::DeclareRetRegInst:
1961 case NVPTX::DeclareRetScalarInst:
1962 case NVPTX::DeclareScalarParamInst:
1963 case NVPTX::DeclareScalarRegInst:
1964 case NVPTX::StoreParamF32:
1965 case NVPTX::StoreParamF64:
1966 case NVPTX::StoreParamI16:
1967 case NVPTX::StoreParamI32:
1968 case NVPTX::StoreParamI64:
1969 case NVPTX::StoreParamI8:
1970 case NVPTX::StoreRetvalF32:
1971 case NVPTX::StoreRetvalF64:
1972 case NVPTX::StoreRetvalI16:
1973 case NVPTX::StoreRetvalI32:
1974 case NVPTX::StoreRetvalI64:
1975 case NVPTX::StoreRetvalI8:
1976 case NVPTX::LastCallArgF32:
1977 case NVPTX::LastCallArgF64:
1978 case NVPTX::LastCallArgI16:
1979 case NVPTX::LastCallArgI32:
1980 case NVPTX::LastCallArgI32imm:
1981 case NVPTX::LastCallArgI64:
1982 case NVPTX::LastCallArgParam:
1983 case NVPTX::LoadParamMemF32:
1984 case NVPTX::LoadParamMemF64:
1985 case NVPTX::LoadParamMemI16:
1986 case NVPTX::LoadParamMemI32:
1987 case NVPTX::LoadParamMemI64:
1988 case NVPTX::LoadParamMemI8:
1989 case NVPTX::PrototypeInst:
1990 case NVPTX::DBG_VALUE:
1996 // Force static initialization.
1997 extern "C" void LLVMInitializeNVPTXBackendAsmPrinter() {
1998 RegisterAsmPrinter<NVPTXAsmPrinter> X(TheNVPTXTarget32);
1999 RegisterAsmPrinter<NVPTXAsmPrinter> Y(TheNVPTXTarget64);
2002 void NVPTXAsmPrinter::emitSrcInText(StringRef filename, unsigned line) {
2003 std::stringstream temp;
2004 LineReader *reader = this->getReader(filename.str());
2006 temp << filename.str();
2010 temp << reader->readLine(line);
2012 this->OutStreamer.EmitRawText(Twine(temp.str()));
2015 LineReader *NVPTXAsmPrinter::getReader(std::string filename) {
2016 if (reader == NULL) {
2017 reader = new LineReader(filename);
2020 if (reader->fileName() != filename) {
2022 reader = new LineReader(filename);
2028 std::string LineReader::readLine(unsigned lineNum) {
2029 if (lineNum < theCurLine) {
2031 fstr.seekg(0, std::ios::beg);
2033 while (theCurLine < lineNum) {
2034 fstr.getline(buff, 500);
2040 // Force static initialization.
2041 extern "C" void LLVMInitializeNVPTXAsmPrinter() {
2042 RegisterAsmPrinter<NVPTXAsmPrinter> X(TheNVPTXTarget32);
2043 RegisterAsmPrinter<NVPTXAsmPrinter> Y(TheNVPTXTarget64);