Reduce double set lookups. NFC.
[oota-llvm.git] / lib / Target / NVPTX / NVPTXAsmPrinter.cpp
1 //===-- NVPTXAsmPrinter.cpp - NVPTX 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 NVPTX assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "NVPTXAsmPrinter.h"
16 #include "InstPrinter/NVPTXInstPrinter.h"
17 #include "MCTargetDesc/NVPTXMCAsmInfo.h"
18 #include "NVPTX.h"
19 #include "NVPTXInstrInfo.h"
20 #include "NVPTXMachineFunctionInfo.h"
21 #include "NVPTXMCExpr.h"
22 #include "NVPTXRegisterInfo.h"
23 #include "NVPTXTargetMachine.h"
24 #include "NVPTXUtilities.h"
25 #include "cl_common_defines.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/Analysis/ConstantFolding.h"
28 #include "llvm/CodeGen/Analysis.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/IR/DebugInfo.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Mangler.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbol.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/FormattedStream.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/TargetRegistry.h"
46 #include "llvm/Support/TimeValue.h"
47 #include "llvm/Target/TargetLoweringObjectFile.h"
48 #include <sstream>
49 using namespace llvm;
50
51 #define DEPOTNAME "__local_depot"
52
53 static cl::opt<bool>
54 EmitLineNumbers("nvptx-emit-line-numbers", cl::Hidden,
55                 cl::desc("NVPTX Specific: Emit Line numbers even without -G"),
56                 cl::init(true));
57
58 static cl::opt<bool>
59 InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore, cl::Hidden,
60               cl::desc("NVPTX Specific: Emit source line in ptx file"),
61               cl::init(false));
62
63 namespace {
64 /// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V
65 /// depends.
66 void DiscoverDependentGlobals(const Value *V,
67                               DenseSet<const GlobalVariable *> &Globals) {
68   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
69     Globals.insert(GV);
70   else {
71     if (const User *U = dyn_cast<User>(V)) {
72       for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) {
73         DiscoverDependentGlobals(U->getOperand(i), Globals);
74       }
75     }
76   }
77 }
78
79 /// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable
80 /// instances to be emitted, but only after any dependents have been added
81 /// first.
82 void VisitGlobalVariableForEmission(
83     const GlobalVariable *GV, SmallVectorImpl<const GlobalVariable *> &Order,
84     DenseSet<const GlobalVariable *> &Visited,
85     DenseSet<const GlobalVariable *> &Visiting) {
86   // Have we already visited this one?
87   if (Visited.count(GV))
88     return;
89
90   // Do we have a circular dependency?
91   if (!Visiting.insert(GV).second)
92     report_fatal_error("Circular dependency found in global variable set");
93
94   // Make sure we visit all dependents first
95   DenseSet<const GlobalVariable *> Others;
96   for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i)
97     DiscoverDependentGlobals(GV->getOperand(i), Others);
98
99   for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(),
100                                                   E = Others.end();
101        I != E; ++I)
102     VisitGlobalVariableForEmission(*I, Order, Visited, Visiting);
103
104   // Now we can visit ourself
105   Order.push_back(GV);
106   Visited.insert(GV);
107   Visiting.erase(GV);
108 }
109 }
110
111 // @TODO: This is a copy from AsmPrinter.cpp.  The function is static, so we
112 // cannot just link to the existing version.
113 /// LowerConstant - Lower the specified LLVM Constant to an MCExpr.
114 ///
115 using namespace nvptx;
116 const MCExpr *nvptx::LowerConstant(const Constant *CV, AsmPrinter &AP) {
117   MCContext &Ctx = AP.OutContext;
118
119   if (CV->isNullValue() || isa<UndefValue>(CV))
120     return MCConstantExpr::Create(0, Ctx);
121
122   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
123     return MCConstantExpr::Create(CI->getZExtValue(), Ctx);
124
125   if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
126     return MCSymbolRefExpr::Create(AP.getSymbol(GV), Ctx);
127
128   if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
129     return MCSymbolRefExpr::Create(AP.GetBlockAddressSymbol(BA), Ctx);
130
131   const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
132   if (!CE)
133     llvm_unreachable("Unknown constant value to lower!");
134
135   switch (CE->getOpcode()) {
136   default:
137     // If the code isn't optimized, there may be outstanding folding
138     // opportunities. Attempt to fold the expression using DataLayout as a
139     // last resort before giving up.
140     if (Constant *C = ConstantFoldConstantExpression(
141             CE, AP.TM.getSubtargetImpl()->getDataLayout()))
142       if (C != CE)
143         return LowerConstant(C, AP);
144
145     // Otherwise report the problem to the user.
146     {
147       std::string S;
148       raw_string_ostream OS(S);
149       OS << "Unsupported expression in static initializer: ";
150       CE->printAsOperand(OS, /*PrintType=*/ false,
151                          !AP.MF ? nullptr : AP.MF->getFunction()->getParent());
152       report_fatal_error(OS.str());
153     }
154   case Instruction::AddrSpaceCast: {
155     // Strip any addrspace(1)->addrspace(0) addrspace casts. These will be
156     // handled by the generic() logic in the MCExpr printer
157     PointerType *DstTy            = cast<PointerType>(CE->getType());
158     PointerType *SrcTy            = cast<PointerType>(CE->getOperand(0)->getType());
159     if (SrcTy->getAddressSpace() == 1 && DstTy->getAddressSpace() == 0) {
160       return LowerConstant(cast<const Constant>(CE->getOperand(0)), AP);
161     }
162     std::string S;
163     raw_string_ostream OS(S);
164     OS << "Unsupported expression in static initializer: ";
165     CE->printAsOperand(OS, /*PrintType=*/ false,
166                        !AP.MF ? nullptr : AP.MF->getFunction()->getParent());
167     report_fatal_error(OS.str());
168   }
169   case Instruction::GetElementPtr: {
170     const DataLayout &TD = *AP.TM.getSubtargetImpl()->getDataLayout();
171     // Generate a symbolic expression for the byte address
172     APInt OffsetAI(TD.getPointerSizeInBits(), 0);
173     cast<GEPOperator>(CE)->accumulateConstantOffset(TD, OffsetAI);
174
175     const MCExpr *Base = LowerConstant(CE->getOperand(0), AP);
176     if (!OffsetAI)
177       return Base;
178
179     int64_t Offset = OffsetAI.getSExtValue();
180     return MCBinaryExpr::CreateAdd(Base, MCConstantExpr::Create(Offset, Ctx),
181                                    Ctx);
182   }
183
184   case Instruction::Trunc:
185     // We emit the value and depend on the assembler to truncate the generated
186     // expression properly.  This is important for differences between
187     // blockaddress labels.  Since the two labels are in the same function, it
188     // is reasonable to treat their delta as a 32-bit value.
189   // FALL THROUGH.
190   case Instruction::BitCast:
191     return LowerConstant(CE->getOperand(0), AP);
192
193   case Instruction::IntToPtr: {
194     const DataLayout &TD = *AP.TM.getSubtargetImpl()->getDataLayout();
195     // Handle casts to pointers by changing them into casts to the appropriate
196     // integer type.  This promotes constant folding and simplifies this code.
197     Constant *Op = CE->getOperand(0);
198     Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()),
199                                       false /*ZExt*/);
200     return LowerConstant(Op, AP);
201   }
202
203   case Instruction::PtrToInt: {
204     const DataLayout &TD = *AP.TM.getSubtargetImpl()->getDataLayout();
205     // Support only foldable casts to/from pointers that can be eliminated by
206     // changing the pointer to the appropriately sized integer type.
207     Constant *Op = CE->getOperand(0);
208     Type *Ty = CE->getType();
209
210     const MCExpr *OpExpr = LowerConstant(Op, AP);
211
212     // We can emit the pointer value into this slot if the slot is an
213     // integer slot equal to the size of the pointer.
214     if (TD.getTypeAllocSize(Ty) == TD.getTypeAllocSize(Op->getType()))
215       return OpExpr;
216
217     // Otherwise the pointer is smaller than the resultant integer, mask off
218     // the high bits so we are sure to get a proper truncation if the input is
219     // a constant expr.
220     unsigned InBits = TD.getTypeAllocSizeInBits(Op->getType());
221     const MCExpr *MaskExpr =
222         MCConstantExpr::Create(~0ULL >> (64 - InBits), Ctx);
223     return MCBinaryExpr::CreateAnd(OpExpr, MaskExpr, Ctx);
224   }
225
226     // The MC library also has a right-shift operator, but it isn't consistently
227   // signed or unsigned between different targets.
228   case Instruction::Add:
229   case Instruction::Sub:
230   case Instruction::Mul:
231   case Instruction::SDiv:
232   case Instruction::SRem:
233   case Instruction::Shl:
234   case Instruction::And:
235   case Instruction::Or:
236   case Instruction::Xor: {
237     const MCExpr *LHS = LowerConstant(CE->getOperand(0), AP);
238     const MCExpr *RHS = LowerConstant(CE->getOperand(1), AP);
239     switch (CE->getOpcode()) {
240     default:
241       llvm_unreachable("Unknown binary operator constant cast expr");
242     case Instruction::Add:
243       return MCBinaryExpr::CreateAdd(LHS, RHS, Ctx);
244     case Instruction::Sub:
245       return MCBinaryExpr::CreateSub(LHS, RHS, Ctx);
246     case Instruction::Mul:
247       return MCBinaryExpr::CreateMul(LHS, RHS, Ctx);
248     case Instruction::SDiv:
249       return MCBinaryExpr::CreateDiv(LHS, RHS, Ctx);
250     case Instruction::SRem:
251       return MCBinaryExpr::CreateMod(LHS, RHS, Ctx);
252     case Instruction::Shl:
253       return MCBinaryExpr::CreateShl(LHS, RHS, Ctx);
254     case Instruction::And:
255       return MCBinaryExpr::CreateAnd(LHS, RHS, Ctx);
256     case Instruction::Or:
257       return MCBinaryExpr::CreateOr(LHS, RHS, Ctx);
258     case Instruction::Xor:
259       return MCBinaryExpr::CreateXor(LHS, RHS, Ctx);
260     }
261   }
262   }
263 }
264
265 void NVPTXAsmPrinter::emitLineNumberAsDotLoc(const MachineInstr &MI) {
266   if (!EmitLineNumbers)
267     return;
268   if (ignoreLoc(MI))
269     return;
270
271   DebugLoc curLoc = MI.getDebugLoc();
272
273   if (prevDebugLoc.isUnknown() && curLoc.isUnknown())
274     return;
275
276   if (prevDebugLoc == curLoc)
277     return;
278
279   prevDebugLoc = curLoc;
280
281   if (curLoc.isUnknown())
282     return;
283
284   const MachineFunction *MF = MI.getParent()->getParent();
285   //const TargetMachine &TM = MF->getTarget();
286
287   const LLVMContext &ctx = MF->getFunction()->getContext();
288   DIScope Scope(curLoc.getScope(ctx));
289
290   assert((!Scope || Scope.isScope()) &&
291     "Scope of a DebugLoc should be null or a DIScope.");
292   if (!Scope)
293      return;
294
295   StringRef fileName(Scope.getFilename());
296   StringRef dirName(Scope.getDirectory());
297   SmallString<128> FullPathName = dirName;
298   if (!dirName.empty() && !sys::path::is_absolute(fileName)) {
299     sys::path::append(FullPathName, fileName);
300     fileName = FullPathName.str();
301   }
302
303   if (filenameMap.find(fileName.str()) == filenameMap.end())
304     return;
305
306   // Emit the line from the source file.
307   if (InterleaveSrc)
308     this->emitSrcInText(fileName.str(), curLoc.getLine());
309
310   std::stringstream temp;
311   temp << "\t.loc " << filenameMap[fileName.str()] << " " << curLoc.getLine()
312        << " " << curLoc.getCol();
313   OutStreamer.EmitRawText(Twine(temp.str().c_str()));
314 }
315
316 void NVPTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
317   SmallString<128> Str;
318   raw_svector_ostream OS(Str);
319   if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)
320     emitLineNumberAsDotLoc(*MI);
321
322   MCInst Inst;
323   lowerToMCInst(MI, Inst);
324   EmitToStreamer(OutStreamer, Inst);
325 }
326
327 // Handle symbol backtracking for targets that do not support image handles
328 bool NVPTXAsmPrinter::lowerImageHandleOperand(const MachineInstr *MI,
329                                            unsigned OpNo, MCOperand &MCOp) {
330   const MachineOperand &MO = MI->getOperand(OpNo);
331   const MCInstrDesc &MCID = MI->getDesc();
332
333   if (MCID.TSFlags & NVPTXII::IsTexFlag) {
334     // This is a texture fetch, so operand 4 is a texref and operand 5 is
335     // a samplerref
336     if (OpNo == 4 && MO.isImm()) {
337       lowerImageHandleSymbol(MO.getImm(), MCOp);
338       return true;
339     }
340     if (OpNo == 5 && MO.isImm() && !(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) {
341       lowerImageHandleSymbol(MO.getImm(), MCOp);
342       return true;
343     }
344
345     return false;
346   } else if (MCID.TSFlags & NVPTXII::IsSuldMask) {
347     unsigned VecSize =
348       1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1);
349
350     // For a surface load of vector size N, the Nth operand will be the surfref
351     if (OpNo == VecSize && MO.isImm()) {
352       lowerImageHandleSymbol(MO.getImm(), MCOp);
353       return true;
354     }
355
356     return false;
357   } else if (MCID.TSFlags & NVPTXII::IsSustFlag) {
358     // This is a surface store, so operand 0 is a surfref
359     if (OpNo == 0 && MO.isImm()) {
360       lowerImageHandleSymbol(MO.getImm(), MCOp);
361       return true;
362     }
363
364     return false;
365   } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) {
366     // This is a query, so operand 1 is a surfref/texref
367     if (OpNo == 1 && MO.isImm()) {
368       lowerImageHandleSymbol(MO.getImm(), MCOp);
369       return true;
370     }
371
372     return false;
373   }
374
375   return false;
376 }
377
378 void NVPTXAsmPrinter::lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp) {
379   // Ewwww
380   TargetMachine &TM = const_cast<TargetMachine&>(MF->getTarget());
381   NVPTXTargetMachine &nvTM = static_cast<NVPTXTargetMachine&>(TM);
382   const NVPTXMachineFunctionInfo *MFI = MF->getInfo<NVPTXMachineFunctionInfo>();
383   const char *Sym = MFI->getImageHandleSymbol(Index);
384   std::string *SymNamePtr =
385     nvTM.getManagedStrPool()->getManagedString(Sym);
386   MCOp = GetSymbolRef(OutContext.GetOrCreateSymbol(
387     StringRef(SymNamePtr->c_str())));
388 }
389
390 void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) {
391   OutMI.setOpcode(MI->getOpcode());
392   const NVPTXSubtarget &ST = TM.getSubtarget<NVPTXSubtarget>();
393
394   // Special: Do not mangle symbol operand of CALL_PROTOTYPE
395   if (MI->getOpcode() == NVPTX::CALL_PROTOTYPE) {
396     const MachineOperand &MO = MI->getOperand(0);
397     OutMI.addOperand(GetSymbolRef(
398       OutContext.GetOrCreateSymbol(Twine(MO.getSymbolName()))));
399     return;
400   }
401
402   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
403     const MachineOperand &MO = MI->getOperand(i);
404
405     MCOperand MCOp;
406     if (!ST.hasImageHandles()) {
407       if (lowerImageHandleOperand(MI, i, MCOp)) {
408         OutMI.addOperand(MCOp);
409         continue;
410       }
411     }
412
413     if (lowerOperand(MO, MCOp))
414       OutMI.addOperand(MCOp);
415   }
416 }
417
418 bool NVPTXAsmPrinter::lowerOperand(const MachineOperand &MO,
419                                    MCOperand &MCOp) {
420   switch (MO.getType()) {
421   default: llvm_unreachable("unknown operand type");
422   case MachineOperand::MO_Register:
423     MCOp = MCOperand::CreateReg(encodeVirtualRegister(MO.getReg()));
424     break;
425   case MachineOperand::MO_Immediate:
426     MCOp = MCOperand::CreateImm(MO.getImm());
427     break;
428   case MachineOperand::MO_MachineBasicBlock:
429     MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
430         MO.getMBB()->getSymbol(), OutContext));
431     break;
432   case MachineOperand::MO_ExternalSymbol:
433     MCOp = GetSymbolRef(GetExternalSymbolSymbol(MO.getSymbolName()));
434     break;
435   case MachineOperand::MO_GlobalAddress:
436     MCOp = GetSymbolRef(getSymbol(MO.getGlobal()));
437     break;
438   case MachineOperand::MO_FPImmediate: {
439     const ConstantFP *Cnt = MO.getFPImm();
440     APFloat Val = Cnt->getValueAPF();
441
442     switch (Cnt->getType()->getTypeID()) {
443     default: report_fatal_error("Unsupported FP type"); break;
444     case Type::FloatTyID:
445       MCOp = MCOperand::CreateExpr(
446         NVPTXFloatMCExpr::CreateConstantFPSingle(Val, OutContext));
447       break;
448     case Type::DoubleTyID:
449       MCOp = MCOperand::CreateExpr(
450         NVPTXFloatMCExpr::CreateConstantFPDouble(Val, OutContext));
451       break;
452     }
453     break;
454   }
455   }
456   return true;
457 }
458
459 unsigned NVPTXAsmPrinter::encodeVirtualRegister(unsigned Reg) {
460   if (TargetRegisterInfo::isVirtualRegister(Reg)) {
461     const TargetRegisterClass *RC = MRI->getRegClass(Reg);
462
463     DenseMap<unsigned, unsigned> &RegMap = VRegMapping[RC];
464     unsigned RegNum = RegMap[Reg];
465
466     // Encode the register class in the upper 4 bits
467     // Must be kept in sync with NVPTXInstPrinter::printRegName
468     unsigned Ret = 0;
469     if (RC == &NVPTX::Int1RegsRegClass) {
470       Ret = (1 << 28);
471     } else if (RC == &NVPTX::Int16RegsRegClass) {
472       Ret = (2 << 28);
473     } else if (RC == &NVPTX::Int32RegsRegClass) {
474       Ret = (3 << 28);
475     } else if (RC == &NVPTX::Int64RegsRegClass) {
476       Ret = (4 << 28);
477     } else if (RC == &NVPTX::Float32RegsRegClass) {
478       Ret = (5 << 28);
479     } else if (RC == &NVPTX::Float64RegsRegClass) {
480       Ret = (6 << 28);
481     } else {
482       report_fatal_error("Bad register class");
483     }
484
485     // Insert the vreg number
486     Ret |= (RegNum & 0x0FFFFFFF);
487     return Ret;
488   } else {
489     // Some special-use registers are actually physical registers.
490     // Encode this as the register class ID of 0 and the real register ID.
491     return Reg & 0x0FFFFFFF;
492   }
493 }
494
495 MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol *Symbol) {
496   const MCExpr *Expr;
497   Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None,
498                                  OutContext);
499   return MCOperand::CreateExpr(Expr);
500 }
501
502 void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
503   const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
504   const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
505
506   Type *Ty = F->getReturnType();
507
508   bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
509
510   if (Ty->getTypeID() == Type::VoidTyID)
511     return;
512
513   O << " (";
514
515   if (isABI) {
516     if (Ty->isFloatingPointTy() || Ty->isIntegerTy()) {
517       unsigned size = 0;
518       if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
519         size = ITy->getBitWidth();
520         if (size < 32)
521           size = 32;
522       } else {
523         assert(Ty->isFloatingPointTy() && "Floating point type expected here");
524         size = Ty->getPrimitiveSizeInBits();
525       }
526
527       O << ".param .b" << size << " func_retval0";
528     } else if (isa<PointerType>(Ty)) {
529       O << ".param .b" << TLI->getPointerTy().getSizeInBits()
530         << " func_retval0";
531     } else {
532       if ((Ty->getTypeID() == Type::StructTyID) || isa<VectorType>(Ty)) {
533         unsigned totalsz = TD->getTypeAllocSize(Ty);
534         unsigned retAlignment = 0;
535         if (!llvm::getAlign(*F, 0, retAlignment))
536           retAlignment = TD->getABITypeAlignment(Ty);
537         O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz
538           << "]";
539       } else
540         assert(false && "Unknown return type");
541     }
542   } else {
543     SmallVector<EVT, 16> vtparts;
544     ComputeValueVTs(*TLI, Ty, vtparts);
545     unsigned idx = 0;
546     for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
547       unsigned elems = 1;
548       EVT elemtype = vtparts[i];
549       if (vtparts[i].isVector()) {
550         elems = vtparts[i].getVectorNumElements();
551         elemtype = vtparts[i].getVectorElementType();
552       }
553
554       for (unsigned j = 0, je = elems; j != je; ++j) {
555         unsigned sz = elemtype.getSizeInBits();
556         if (elemtype.isInteger() && (sz < 32))
557           sz = 32;
558         O << ".reg .b" << sz << " func_retval" << idx;
559         if (j < je - 1)
560           O << ", ";
561         ++idx;
562       }
563       if (i < e - 1)
564         O << ", ";
565     }
566   }
567   O << ") ";
568   return;
569 }
570
571 void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF,
572                                         raw_ostream &O) {
573   const Function *F = MF.getFunction();
574   printReturnValStr(F, O);
575 }
576
577 void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
578   SmallString<128> Str;
579   raw_svector_ostream O(Str);
580
581   if (!GlobalsEmitted) {
582     emitGlobals(*MF->getFunction()->getParent());
583     GlobalsEmitted = true;
584   }
585   
586   // Set up
587   MRI = &MF->getRegInfo();
588   F = MF->getFunction();
589   emitLinkageDirective(F, O);
590   if (llvm::isKernelFunction(*F))
591     O << ".entry ";
592   else {
593     O << ".func ";
594     printReturnValStr(*MF, O);
595   }
596
597   O << *CurrentFnSym;
598
599   emitFunctionParamList(*MF, O);
600
601   if (llvm::isKernelFunction(*F))
602     emitKernelFunctionDirectives(*F, O);
603
604   OutStreamer.EmitRawText(O.str());
605
606   prevDebugLoc = DebugLoc();
607 }
608
609 void NVPTXAsmPrinter::EmitFunctionBodyStart() {
610   VRegMapping.clear();
611   OutStreamer.EmitRawText(StringRef("{\n"));
612   setAndEmitFunctionVirtualRegisters(*MF);
613
614   SmallString<128> Str;
615   raw_svector_ostream O(Str);
616   emitDemotedVars(MF->getFunction(), O);
617   OutStreamer.EmitRawText(O.str());
618 }
619
620 void NVPTXAsmPrinter::EmitFunctionBodyEnd() {
621   OutStreamer.EmitRawText(StringRef("}\n"));
622   VRegMapping.clear();
623 }
624
625 void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
626   unsigned RegNo = MI->getOperand(0).getReg();
627   const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
628   if (TRI->isVirtualRegister(RegNo)) {
629     OutStreamer.AddComment(Twine("implicit-def: ") +
630                            getVirtualRegisterName(RegNo));
631   } else {
632     OutStreamer.AddComment(
633         Twine("implicit-def: ") +
634         TM.getSubtargetImpl()->getRegisterInfo()->getName(RegNo));
635   }
636   OutStreamer.AddBlankLine();
637 }
638
639 void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F,
640                                                    raw_ostream &O) const {
641   // If the NVVM IR has some of reqntid* specified, then output
642   // the reqntid directive, and set the unspecified ones to 1.
643   // If none of reqntid* is specified, don't output reqntid directive.
644   unsigned reqntidx, reqntidy, reqntidz;
645   bool specified = false;
646   if (llvm::getReqNTIDx(F, reqntidx) == false)
647     reqntidx = 1;
648   else
649     specified = true;
650   if (llvm::getReqNTIDy(F, reqntidy) == false)
651     reqntidy = 1;
652   else
653     specified = true;
654   if (llvm::getReqNTIDz(F, reqntidz) == false)
655     reqntidz = 1;
656   else
657     specified = true;
658
659   if (specified)
660     O << ".reqntid " << reqntidx << ", " << reqntidy << ", " << reqntidz
661       << "\n";
662
663   // If the NVVM IR has some of maxntid* specified, then output
664   // the maxntid directive, and set the unspecified ones to 1.
665   // If none of maxntid* is specified, don't output maxntid directive.
666   unsigned maxntidx, maxntidy, maxntidz;
667   specified = false;
668   if (llvm::getMaxNTIDx(F, maxntidx) == false)
669     maxntidx = 1;
670   else
671     specified = true;
672   if (llvm::getMaxNTIDy(F, maxntidy) == false)
673     maxntidy = 1;
674   else
675     specified = true;
676   if (llvm::getMaxNTIDz(F, maxntidz) == false)
677     maxntidz = 1;
678   else
679     specified = true;
680
681   if (specified)
682     O << ".maxntid " << maxntidx << ", " << maxntidy << ", " << maxntidz
683       << "\n";
684
685   unsigned mincta;
686   if (llvm::getMinCTASm(F, mincta))
687     O << ".minnctapersm " << mincta << "\n";
688 }
689
690 std::string
691 NVPTXAsmPrinter::getVirtualRegisterName(unsigned Reg) const {
692   const TargetRegisterClass *RC = MRI->getRegClass(Reg);
693
694   std::string Name;
695   raw_string_ostream NameStr(Name);
696
697   VRegRCMap::const_iterator I = VRegMapping.find(RC);
698   assert(I != VRegMapping.end() && "Bad register class");
699   const DenseMap<unsigned, unsigned> &RegMap = I->second;
700
701   VRegMap::const_iterator VI = RegMap.find(Reg);
702   assert(VI != RegMap.end() && "Bad virtual register");
703   unsigned MappedVR = VI->second;
704
705   NameStr << getNVPTXRegClassStr(RC) << MappedVR;
706
707   NameStr.flush();
708   return Name;
709 }
710
711 void NVPTXAsmPrinter::emitVirtualRegister(unsigned int vr,
712                                           raw_ostream &O) {
713   O << getVirtualRegisterName(vr);
714 }
715
716 void NVPTXAsmPrinter::printVecModifiedImmediate(
717     const MachineOperand &MO, const char *Modifier, raw_ostream &O) {
718   static const char vecelem[] = { '0', '1', '2', '3', '0', '1', '2', '3' };
719   int Imm = (int) MO.getImm();
720   if (0 == strcmp(Modifier, "vecelem"))
721     O << "_" << vecelem[Imm];
722   else if (0 == strcmp(Modifier, "vecv4comm1")) {
723     if ((Imm < 0) || (Imm > 3))
724       O << "//";
725   } else if (0 == strcmp(Modifier, "vecv4comm2")) {
726     if ((Imm < 4) || (Imm > 7))
727       O << "//";
728   } else if (0 == strcmp(Modifier, "vecv4pos")) {
729     if (Imm < 0)
730       Imm = 0;
731     O << "_" << vecelem[Imm % 4];
732   } else if (0 == strcmp(Modifier, "vecv2comm1")) {
733     if ((Imm < 0) || (Imm > 1))
734       O << "//";
735   } else if (0 == strcmp(Modifier, "vecv2comm2")) {
736     if ((Imm < 2) || (Imm > 3))
737       O << "//";
738   } else if (0 == strcmp(Modifier, "vecv2pos")) {
739     if (Imm < 0)
740       Imm = 0;
741     O << "_" << vecelem[Imm % 2];
742   } else
743     llvm_unreachable("Unknown Modifier on immediate operand");
744 }
745
746
747
748 void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) {
749
750   emitLinkageDirective(F, O);
751   if (llvm::isKernelFunction(*F))
752     O << ".entry ";
753   else
754     O << ".func ";
755   printReturnValStr(F, O);
756   O << *getSymbol(F) << "\n";
757   emitFunctionParamList(F, O);
758   O << ";\n";
759 }
760
761 static bool usedInGlobalVarDef(const Constant *C) {
762   if (!C)
763     return false;
764
765   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
766     if (GV->getName().str() == "llvm.used")
767       return false;
768     return true;
769   }
770
771   for (const User *U : C->users())
772     if (const Constant *C = dyn_cast<Constant>(U))
773       if (usedInGlobalVarDef(C))
774         return true;
775
776   return false;
777 }
778
779 static bool usedInOneFunc(const User *U, Function const *&oneFunc) {
780   if (const GlobalVariable *othergv = dyn_cast<GlobalVariable>(U)) {
781     if (othergv->getName().str() == "llvm.used")
782       return true;
783   }
784
785   if (const Instruction *instr = dyn_cast<Instruction>(U)) {
786     if (instr->getParent() && instr->getParent()->getParent()) {
787       const Function *curFunc = instr->getParent()->getParent();
788       if (oneFunc && (curFunc != oneFunc))
789         return false;
790       oneFunc = curFunc;
791       return true;
792     } else
793       return false;
794   }
795
796   if (const MDNode *md = dyn_cast<MDNode>(U))
797     if (md->hasName() && ((md->getName().str() == "llvm.dbg.gv") ||
798                           (md->getName().str() == "llvm.dbg.sp")))
799       return true;
800
801   for (const User *UU : U->users())
802     if (usedInOneFunc(UU, oneFunc) == false)
803       return false;
804
805   return true;
806 }
807
808 /* Find out if a global variable can be demoted to local scope.
809  * Currently, this is valid for CUDA shared variables, which have local
810  * scope and global lifetime. So the conditions to check are :
811  * 1. Is the global variable in shared address space?
812  * 2. Does it have internal linkage?
813  * 3. Is the global variable referenced only in one function?
814  */
815 static bool canDemoteGlobalVar(const GlobalVariable *gv, Function const *&f) {
816   if (gv->hasInternalLinkage() == false)
817     return false;
818   const PointerType *Pty = gv->getType();
819   if (Pty->getAddressSpace() != llvm::ADDRESS_SPACE_SHARED)
820     return false;
821
822   const Function *oneFunc = nullptr;
823
824   bool flag = usedInOneFunc(gv, oneFunc);
825   if (flag == false)
826     return false;
827   if (!oneFunc)
828     return false;
829   f = oneFunc;
830   return true;
831 }
832
833 static bool useFuncSeen(const Constant *C,
834                         llvm::DenseMap<const Function *, bool> &seenMap) {
835   for (const User *U : C->users()) {
836     if (const Constant *cu = dyn_cast<Constant>(U)) {
837       if (useFuncSeen(cu, seenMap))
838         return true;
839     } else if (const Instruction *I = dyn_cast<Instruction>(U)) {
840       const BasicBlock *bb = I->getParent();
841       if (!bb)
842         continue;
843       const Function *caller = bb->getParent();
844       if (!caller)
845         continue;
846       if (seenMap.find(caller) != seenMap.end())
847         return true;
848     }
849   }
850   return false;
851 }
852
853 void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
854   llvm::DenseMap<const Function *, bool> seenMap;
855   for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
856     const Function *F = FI;
857
858     if (F->isDeclaration()) {
859       if (F->use_empty())
860         continue;
861       if (F->getIntrinsicID())
862         continue;
863       emitDeclaration(F, O);
864       continue;
865     }
866     for (const User *U : F->users()) {
867       if (const Constant *C = dyn_cast<Constant>(U)) {
868         if (usedInGlobalVarDef(C)) {
869           // The use is in the initialization of a global variable
870           // that is a function pointer, so print a declaration
871           // for the original function
872           emitDeclaration(F, O);
873           break;
874         }
875         // Emit a declaration of this function if the function that
876         // uses this constant expr has already been seen.
877         if (useFuncSeen(C, seenMap)) {
878           emitDeclaration(F, O);
879           break;
880         }
881       }
882
883       if (!isa<Instruction>(U))
884         continue;
885       const Instruction *instr = cast<Instruction>(U);
886       const BasicBlock *bb = instr->getParent();
887       if (!bb)
888         continue;
889       const Function *caller = bb->getParent();
890       if (!caller)
891         continue;
892
893       // If a caller has already been seen, then the caller is
894       // appearing in the module before the callee. so print out
895       // a declaration for the callee.
896       if (seenMap.find(caller) != seenMap.end()) {
897         emitDeclaration(F, O);
898         break;
899       }
900     }
901     seenMap[F] = true;
902   }
903 }
904
905 void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) {
906   DebugInfoFinder DbgFinder;
907   DbgFinder.processModule(M);
908
909   unsigned i = 1;
910   for (DICompileUnit DIUnit : DbgFinder.compile_units()) {
911     StringRef Filename(DIUnit.getFilename());
912     StringRef Dirname(DIUnit.getDirectory());
913     SmallString<128> FullPathName = Dirname;
914     if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
915       sys::path::append(FullPathName, Filename);
916       Filename = FullPathName.str();
917     }
918     if (filenameMap.find(Filename.str()) != filenameMap.end())
919       continue;
920     filenameMap[Filename.str()] = i;
921     OutStreamer.EmitDwarfFileDirective(i, "", Filename.str());
922     ++i;
923   }
924
925   for (DISubprogram SP : DbgFinder.subprograms()) {
926     StringRef Filename(SP.getFilename());
927     StringRef Dirname(SP.getDirectory());
928     SmallString<128> FullPathName = Dirname;
929     if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
930       sys::path::append(FullPathName, Filename);
931       Filename = FullPathName.str();
932     }
933     if (filenameMap.find(Filename.str()) != filenameMap.end())
934       continue;
935     filenameMap[Filename.str()] = i;
936     ++i;
937   }
938 }
939
940 bool NVPTXAsmPrinter::doInitialization(Module &M) {
941
942   SmallString<128> Str1;
943   raw_svector_ostream OS1(Str1);
944
945   MMI = getAnalysisIfAvailable<MachineModuleInfo>();
946   MMI->AnalyzeModule(M);
947
948   // We need to call the parent's one explicitly.
949   //bool Result = AsmPrinter::doInitialization(M);
950
951   // Initialize TargetLoweringObjectFile.
952   const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
953       .Initialize(OutContext, TM);
954
955   Mang = new Mangler(TM.getSubtargetImpl()->getDataLayout());
956
957   // Emit header before any dwarf directives are emitted below.
958   emitHeader(M, OS1);
959   OutStreamer.EmitRawText(OS1.str());
960
961   // Already commented out
962   //bool Result = AsmPrinter::doInitialization(M);
963
964   // Emit module-level inline asm if it exists.
965   if (!M.getModuleInlineAsm().empty()) {
966     OutStreamer.AddComment("Start of file scope inline assembly");
967     OutStreamer.AddBlankLine();
968     OutStreamer.EmitRawText(StringRef(M.getModuleInlineAsm()));
969     OutStreamer.AddBlankLine();
970     OutStreamer.AddComment("End of file scope inline assembly");
971     OutStreamer.AddBlankLine();
972   }
973
974   if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)
975     recordAndEmitFilenames(M);
976
977   GlobalsEmitted = false;
978     
979   return false; // success
980 }
981
982 void NVPTXAsmPrinter::emitGlobals(const Module &M) {
983   SmallString<128> Str2;
984   raw_svector_ostream OS2(Str2);
985
986   emitDeclarations(M, OS2);
987
988   // As ptxas does not support forward references of globals, we need to first
989   // sort the list of module-level globals in def-use order. We visit each
990   // global variable in order, and ensure that we emit it *after* its dependent
991   // globals. We use a little extra memory maintaining both a set and a list to
992   // have fast searches while maintaining a strict ordering.
993   SmallVector<const GlobalVariable *, 8> Globals;
994   DenseSet<const GlobalVariable *> GVVisited;
995   DenseSet<const GlobalVariable *> GVVisiting;
996
997   // Visit each global variable, in order
998   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
999        I != E; ++I)
1000     VisitGlobalVariableForEmission(I, Globals, GVVisited, GVVisiting);
1001
1002   assert(GVVisited.size() == M.getGlobalList().size() &&
1003          "Missed a global variable");
1004   assert(GVVisiting.size() == 0 && "Did not fully process a global variable");
1005
1006   // Print out module-level global variables in proper order
1007   for (unsigned i = 0, e = Globals.size(); i != e; ++i)
1008     printModuleLevelGV(Globals[i], OS2);
1009
1010   OS2 << '\n';
1011
1012   OutStreamer.EmitRawText(OS2.str());
1013 }
1014
1015 void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O) {
1016   O << "//\n";
1017   O << "// Generated by LLVM NVPTX Back-End\n";
1018   O << "//\n";
1019   O << "\n";
1020
1021   unsigned PTXVersion = nvptxSubtarget.getPTXVersion();
1022   O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n";
1023
1024   O << ".target ";
1025   O << nvptxSubtarget.getTargetName();
1026
1027   if (nvptxSubtarget.getDrvInterface() == NVPTX::NVCL)
1028     O << ", texmode_independent";
1029   if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA) {
1030     if (!nvptxSubtarget.hasDouble())
1031       O << ", map_f64_to_f32";
1032   }
1033
1034   if (MAI->doesSupportDebugInformation())
1035     O << ", debug";
1036
1037   O << "\n";
1038
1039   O << ".address_size ";
1040   if (nvptxSubtarget.is64Bit())
1041     O << "64";
1042   else
1043     O << "32";
1044   O << "\n";
1045
1046   O << "\n";
1047 }
1048
1049 bool NVPTXAsmPrinter::doFinalization(Module &M) {
1050
1051   // If we did not emit any functions, then the global declarations have not
1052   // yet been emitted.
1053   if (!GlobalsEmitted) {
1054     emitGlobals(M);
1055     GlobalsEmitted = true;
1056   }
1057
1058   // XXX Temproarily remove global variables so that doFinalization() will not
1059   // emit them again (global variables are emitted at beginning).
1060
1061   Module::GlobalListType &global_list = M.getGlobalList();
1062   int i, n = global_list.size();
1063   GlobalVariable **gv_array = new GlobalVariable *[n];
1064
1065   // first, back-up GlobalVariable in gv_array
1066   i = 0;
1067   for (Module::global_iterator I = global_list.begin(), E = global_list.end();
1068        I != E; ++I)
1069     gv_array[i++] = &*I;
1070
1071   // second, empty global_list
1072   while (!global_list.empty())
1073     global_list.remove(global_list.begin());
1074
1075   // call doFinalization
1076   bool ret = AsmPrinter::doFinalization(M);
1077
1078   // now we restore global variables
1079   for (i = 0; i < n; i++)
1080     global_list.insert(global_list.end(), gv_array[i]);
1081
1082   clearAnnotationCache(&M);
1083
1084   delete[] gv_array;
1085   return ret;
1086
1087   //bool Result = AsmPrinter::doFinalization(M);
1088   // Instead of calling the parents doFinalization, we may
1089   // clone parents doFinalization and customize here.
1090   // Currently, we if NVISA out the EmitGlobals() in
1091   // parent's doFinalization, which is too intrusive.
1092   //
1093   // Same for the doInitialization.
1094   //return Result;
1095 }
1096
1097 // This function emits appropriate linkage directives for
1098 // functions and global variables.
1099 //
1100 // extern function declaration            -> .extern
1101 // extern function definition             -> .visible
1102 // external global variable with init     -> .visible
1103 // external without init                  -> .extern
1104 // appending                              -> not allowed, assert.
1105 // for any linkage other than
1106 // internal, private, linker_private,
1107 // linker_private_weak, linker_private_weak_def_auto,
1108 // we emit                                -> .weak.
1109
1110 void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V,
1111                                            raw_ostream &O) {
1112   if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA) {
1113     if (V->hasExternalLinkage()) {
1114       if (isa<GlobalVariable>(V)) {
1115         const GlobalVariable *GVar = cast<GlobalVariable>(V);
1116         if (GVar) {
1117           if (GVar->hasInitializer())
1118             O << ".visible ";
1119           else
1120             O << ".extern ";
1121         }
1122       } else if (V->isDeclaration())
1123         O << ".extern ";
1124       else
1125         O << ".visible ";
1126     } else if (V->hasAppendingLinkage()) {
1127       std::string msg;
1128       msg.append("Error: ");
1129       msg.append("Symbol ");
1130       if (V->hasName())
1131         msg.append(V->getName().str());
1132       msg.append("has unsupported appending linkage type");
1133       llvm_unreachable(msg.c_str());
1134     } else if (!V->hasInternalLinkage() &&
1135                !V->hasPrivateLinkage()) {
1136       O << ".weak ";
1137     }
1138   }
1139 }
1140
1141 void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
1142                                          raw_ostream &O,
1143                                          bool processDemoted) {
1144
1145   // Skip meta data
1146   if (GVar->hasSection()) {
1147     if (GVar->getSection() == StringRef("llvm.metadata"))
1148       return;
1149   }
1150
1151   // Skip LLVM intrinsic global variables
1152   if (GVar->getName().startswith("llvm.") ||
1153       GVar->getName().startswith("nvvm."))
1154     return;
1155
1156   const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
1157
1158   // GlobalVariables are always constant pointers themselves.
1159   const PointerType *PTy = GVar->getType();
1160   Type *ETy = PTy->getElementType();
1161
1162   if (GVar->hasExternalLinkage()) {
1163     if (GVar->hasInitializer())
1164       O << ".visible ";
1165     else
1166       O << ".extern ";
1167   } else if (GVar->hasLinkOnceLinkage() || GVar->hasWeakLinkage() ||
1168              GVar->hasAvailableExternallyLinkage() ||
1169              GVar->hasCommonLinkage()) {
1170     O << ".weak ";
1171   }
1172
1173   if (llvm::isTexture(*GVar)) {
1174     O << ".global .texref " << llvm::getTextureName(*GVar) << ";\n";
1175     return;
1176   }
1177
1178   if (llvm::isSurface(*GVar)) {
1179     O << ".global .surfref " << llvm::getSurfaceName(*GVar) << ";\n";
1180     return;
1181   }
1182
1183   if (GVar->isDeclaration()) {
1184     // (extern) declarations, no definition or initializer
1185     // Currently the only known declaration is for an automatic __local
1186     // (.shared) promoted to global.
1187     emitPTXGlobalVariable(GVar, O);
1188     O << ";\n";
1189     return;
1190   }
1191
1192   if (llvm::isSampler(*GVar)) {
1193     O << ".global .samplerref " << llvm::getSamplerName(*GVar);
1194
1195     const Constant *Initializer = nullptr;
1196     if (GVar->hasInitializer())
1197       Initializer = GVar->getInitializer();
1198     const ConstantInt *CI = nullptr;
1199     if (Initializer)
1200       CI = dyn_cast<ConstantInt>(Initializer);
1201     if (CI) {
1202       unsigned sample = CI->getZExtValue();
1203
1204       O << " = { ";
1205
1206       for (int i = 0,
1207                addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE);
1208            i < 3; i++) {
1209         O << "addr_mode_" << i << " = ";
1210         switch (addr) {
1211         case 0:
1212           O << "wrap";
1213           break;
1214         case 1:
1215           O << "clamp_to_border";
1216           break;
1217         case 2:
1218           O << "clamp_to_edge";
1219           break;
1220         case 3:
1221           O << "wrap";
1222           break;
1223         case 4:
1224           O << "mirror";
1225           break;
1226         }
1227         O << ", ";
1228       }
1229       O << "filter_mode = ";
1230       switch ((sample & __CLK_FILTER_MASK) >> __CLK_FILTER_BASE) {
1231       case 0:
1232         O << "nearest";
1233         break;
1234       case 1:
1235         O << "linear";
1236         break;
1237       case 2:
1238         llvm_unreachable("Anisotropic filtering is not supported");
1239       default:
1240         O << "nearest";
1241         break;
1242       }
1243       if (!((sample & __CLK_NORMALIZED_MASK) >> __CLK_NORMALIZED_BASE)) {
1244         O << ", force_unnormalized_coords = 1";
1245       }
1246       O << " }";
1247     }
1248
1249     O << ";\n";
1250     return;
1251   }
1252
1253   if (GVar->hasPrivateLinkage()) {
1254
1255     if (!strncmp(GVar->getName().data(), "unrollpragma", 12))
1256       return;
1257
1258     // FIXME - need better way (e.g. Metadata) to avoid generating this global
1259     if (!strncmp(GVar->getName().data(), "filename", 8))
1260       return;
1261     if (GVar->use_empty())
1262       return;
1263   }
1264
1265   const Function *demotedFunc = nullptr;
1266   if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) {
1267     O << "// " << GVar->getName().str() << " has been demoted\n";
1268     if (localDecls.find(demotedFunc) != localDecls.end())
1269       localDecls[demotedFunc].push_back(GVar);
1270     else {
1271       std::vector<const GlobalVariable *> temp;
1272       temp.push_back(GVar);
1273       localDecls[demotedFunc] = temp;
1274     }
1275     return;
1276   }
1277
1278   O << ".";
1279   emitPTXAddressSpace(PTy->getAddressSpace(), O);
1280
1281   if (isManaged(*GVar)) {
1282     O << " .attribute(.managed)";
1283   }
1284
1285   if (GVar->getAlignment() == 0)
1286     O << " .align " << (int) TD->getPrefTypeAlignment(ETy);
1287   else
1288     O << " .align " << GVar->getAlignment();
1289
1290   if (ETy->isSingleValueType()) {
1291     O << " .";
1292     // Special case: ABI requires that we use .u8 for predicates
1293     if (ETy->isIntegerTy(1))
1294       O << "u8";
1295     else
1296       O << getPTXFundamentalTypeStr(ETy, false);
1297     O << " ";
1298     O << *getSymbol(GVar);
1299
1300     // Ptx allows variable initilization only for constant and global state
1301     // spaces.
1302     if (GVar->hasInitializer()) {
1303       if ((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) ||
1304           (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) {
1305         const Constant *Initializer = GVar->getInitializer();
1306         // 'undef' is treated as there is no value spefied.
1307         if (!Initializer->isNullValue() && !isa<UndefValue>(Initializer)) {
1308           O << " = ";
1309           printScalarConstant(Initializer, O);
1310         }
1311       } else {
1312         // The frontend adds zero-initializer to variables that don't have an
1313         // initial value, so skip warning for this case.
1314         if (!GVar->getInitializer()->isNullValue()) {
1315           std::string warnMsg = "initial value of '" + GVar->getName().str() +
1316               "' is not allowed in addrspace(" +
1317               llvm::utostr_32(PTy->getAddressSpace()) + ")";
1318           report_fatal_error(warnMsg.c_str());
1319         }
1320       }
1321     }
1322   } else {
1323     unsigned int ElementSize = 0;
1324
1325     // Although PTX has direct support for struct type and array type and
1326     // LLVM IR is very similar to PTX, the LLVM CodeGen does not support for
1327     // targets that support these high level field accesses. Structs, arrays
1328     // and vectors are lowered into arrays of bytes.
1329     switch (ETy->getTypeID()) {
1330     case Type::StructTyID:
1331     case Type::ArrayTyID:
1332     case Type::VectorTyID:
1333       ElementSize = TD->getTypeStoreSize(ETy);
1334       // Ptx allows variable initilization only for constant and
1335       // global state spaces.
1336       if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) ||
1337            (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) &&
1338           GVar->hasInitializer()) {
1339         const Constant *Initializer = GVar->getInitializer();
1340         if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) {
1341           AggBuffer aggBuffer(ElementSize, O, *this);
1342           bufferAggregateConstant(Initializer, &aggBuffer);
1343           if (aggBuffer.numSymbols) {
1344             if (nvptxSubtarget.is64Bit()) {
1345               O << " .u64 " << *getSymbol(GVar) << "[";
1346               O << ElementSize / 8;
1347             } else {
1348               O << " .u32 " << *getSymbol(GVar) << "[";
1349               O << ElementSize / 4;
1350             }
1351             O << "]";
1352           } else {
1353             O << " .b8 " << *getSymbol(GVar) << "[";
1354             O << ElementSize;
1355             O << "]";
1356           }
1357           O << " = {";
1358           aggBuffer.print();
1359           O << "}";
1360         } else {
1361           O << " .b8 " << *getSymbol(GVar);
1362           if (ElementSize) {
1363             O << "[";
1364             O << ElementSize;
1365             O << "]";
1366           }
1367         }
1368       } else {
1369         O << " .b8 " << *getSymbol(GVar);
1370         if (ElementSize) {
1371           O << "[";
1372           O << ElementSize;
1373           O << "]";
1374         }
1375       }
1376       break;
1377     default:
1378       llvm_unreachable("type not supported yet");
1379     }
1380
1381   }
1382   O << ";\n";
1383 }
1384
1385 void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) {
1386   if (localDecls.find(f) == localDecls.end())
1387     return;
1388
1389   std::vector<const GlobalVariable *> &gvars = localDecls[f];
1390
1391   for (unsigned i = 0, e = gvars.size(); i != e; ++i) {
1392     O << "\t// demoted variable\n\t";
1393     printModuleLevelGV(gvars[i], O, true);
1394   }
1395 }
1396
1397 void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace,
1398                                           raw_ostream &O) const {
1399   switch (AddressSpace) {
1400   case llvm::ADDRESS_SPACE_LOCAL:
1401     O << "local";
1402     break;
1403   case llvm::ADDRESS_SPACE_GLOBAL:
1404     O << "global";
1405     break;
1406   case llvm::ADDRESS_SPACE_CONST:
1407     O << "const";
1408     break;
1409   case llvm::ADDRESS_SPACE_SHARED:
1410     O << "shared";
1411     break;
1412   default:
1413     report_fatal_error("Bad address space found while emitting PTX");
1414     break;
1415   }
1416 }
1417
1418 std::string
1419 NVPTXAsmPrinter::getPTXFundamentalTypeStr(const Type *Ty, bool useB4PTR) const {
1420   switch (Ty->getTypeID()) {
1421   default:
1422     llvm_unreachable("unexpected type");
1423     break;
1424   case Type::IntegerTyID: {
1425     unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth();
1426     if (NumBits == 1)
1427       return "pred";
1428     else if (NumBits <= 64) {
1429       std::string name = "u";
1430       return name + utostr(NumBits);
1431     } else {
1432       llvm_unreachable("Integer too large");
1433       break;
1434     }
1435     break;
1436   }
1437   case Type::FloatTyID:
1438     return "f32";
1439   case Type::DoubleTyID:
1440     return "f64";
1441   case Type::PointerTyID:
1442     if (nvptxSubtarget.is64Bit())
1443       if (useB4PTR)
1444         return "b64";
1445       else
1446         return "u64";
1447     else if (useB4PTR)
1448       return "b32";
1449     else
1450       return "u32";
1451   }
1452   llvm_unreachable("unexpected type");
1453   return nullptr;
1454 }
1455
1456 void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar,
1457                                             raw_ostream &O) {
1458
1459   const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
1460
1461   // GlobalVariables are always constant pointers themselves.
1462   const PointerType *PTy = GVar->getType();
1463   Type *ETy = PTy->getElementType();
1464
1465   O << ".";
1466   emitPTXAddressSpace(PTy->getAddressSpace(), O);
1467   if (GVar->getAlignment() == 0)
1468     O << " .align " << (int) TD->getPrefTypeAlignment(ETy);
1469   else
1470     O << " .align " << GVar->getAlignment();
1471
1472   if (ETy->isSingleValueType()) {
1473     O << " .";
1474     O << getPTXFundamentalTypeStr(ETy);
1475     O << " ";
1476     O << *getSymbol(GVar);
1477     return;
1478   }
1479
1480   int64_t ElementSize = 0;
1481
1482   // Although PTX has direct support for struct type and array type and LLVM IR
1483   // is very similar to PTX, the LLVM CodeGen does not support for targets that
1484   // support these high level field accesses. Structs and arrays are lowered
1485   // into arrays of bytes.
1486   switch (ETy->getTypeID()) {
1487   case Type::StructTyID:
1488   case Type::ArrayTyID:
1489   case Type::VectorTyID:
1490     ElementSize = TD->getTypeStoreSize(ETy);
1491     O << " .b8 " << *getSymbol(GVar) << "[";
1492     if (ElementSize) {
1493       O << itostr(ElementSize);
1494     }
1495     O << "]";
1496     break;
1497   default:
1498     llvm_unreachable("type not supported yet");
1499   }
1500   return;
1501 }
1502
1503 static unsigned int getOpenCLAlignment(const DataLayout *TD, Type *Ty) {
1504   if (Ty->isSingleValueType())
1505     return TD->getPrefTypeAlignment(Ty);
1506
1507   const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1508   if (ATy)
1509     return getOpenCLAlignment(TD, ATy->getElementType());
1510
1511   const VectorType *VTy = dyn_cast<VectorType>(Ty);
1512   if (VTy) {
1513     Type *ETy = VTy->getElementType();
1514     unsigned int numE = VTy->getNumElements();
1515     unsigned int alignE = TD->getPrefTypeAlignment(ETy);
1516     if (numE == 3)
1517       return 4 * alignE;
1518     else
1519       return numE * alignE;
1520   }
1521
1522   const StructType *STy = dyn_cast<StructType>(Ty);
1523   if (STy) {
1524     unsigned int alignStruct = 1;
1525     // Go through each element of the struct and find the
1526     // largest alignment.
1527     for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
1528       Type *ETy = STy->getElementType(i);
1529       unsigned int align = getOpenCLAlignment(TD, ETy);
1530       if (align > alignStruct)
1531         alignStruct = align;
1532     }
1533     return alignStruct;
1534   }
1535
1536   const FunctionType *FTy = dyn_cast<FunctionType>(Ty);
1537   if (FTy)
1538     return TD->getPointerPrefAlignment();
1539   return TD->getPrefTypeAlignment(Ty);
1540 }
1541
1542 void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I,
1543                                      int paramIndex, raw_ostream &O) {
1544   if ((nvptxSubtarget.getDrvInterface() == NVPTX::NVCL) ||
1545       (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA))
1546     O << *getSymbol(I->getParent()) << "_param_" << paramIndex;
1547   else {
1548     std::string argName = I->getName();
1549     const char *p = argName.c_str();
1550     while (*p) {
1551       if (*p == '.')
1552         O << "_";
1553       else
1554         O << *p;
1555       p++;
1556     }
1557   }
1558 }
1559
1560 void NVPTXAsmPrinter::printParamName(int paramIndex, raw_ostream &O) {
1561   Function::const_arg_iterator I, E;
1562   int i = 0;
1563
1564   if ((nvptxSubtarget.getDrvInterface() == NVPTX::NVCL) ||
1565       (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)) {
1566     O << *CurrentFnSym << "_param_" << paramIndex;
1567     return;
1568   }
1569
1570   for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, i++) {
1571     if (i == paramIndex) {
1572       printParamName(I, paramIndex, O);
1573       return;
1574     }
1575   }
1576   llvm_unreachable("paramIndex out of bound");
1577 }
1578
1579 void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
1580   const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
1581   const AttributeSet &PAL = F->getAttributes();
1582   const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
1583   Function::const_arg_iterator I, E;
1584   unsigned paramIndex = 0;
1585   bool first = true;
1586   bool isKernelFunc = llvm::isKernelFunction(*F);
1587   bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
1588   MVT thePointerTy = TLI->getPointerTy();
1589
1590   O << "(\n";
1591
1592   for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, paramIndex++) {
1593     Type *Ty = I->getType();
1594
1595     if (!first)
1596       O << ",\n";
1597
1598     first = false;
1599
1600     // Handle image/sampler parameters
1601     if (isKernelFunction(*F)) {
1602       if (isSampler(*I) || isImage(*I)) {
1603         if (isImage(*I)) {
1604           std::string sname = I->getName();
1605           if (isImageWriteOnly(*I) || isImageReadWrite(*I)) {
1606             if (nvptxSubtarget.hasImageHandles())
1607               O << "\t.param .u64 .ptr .surfref ";
1608             else
1609               O << "\t.param .surfref ";
1610             O << *CurrentFnSym << "_param_" << paramIndex;
1611           }
1612           else { // Default image is read_only
1613             if (nvptxSubtarget.hasImageHandles())
1614               O << "\t.param .u64 .ptr .texref ";
1615             else
1616               O << "\t.param .texref ";
1617             O << *CurrentFnSym << "_param_" << paramIndex;
1618           }
1619         } else {
1620           if (nvptxSubtarget.hasImageHandles())
1621             O << "\t.param .u64 .ptr .samplerref ";
1622           else
1623             O << "\t.param .samplerref ";
1624           O << *CurrentFnSym << "_param_" << paramIndex;
1625         }
1626         continue;
1627       }
1628     }
1629
1630     if (PAL.hasAttribute(paramIndex + 1, Attribute::ByVal) == false) {
1631       if (Ty->isAggregateType() || Ty->isVectorTy()) {
1632         // Just print .param .align <a> .b8 .param[size];
1633         // <a> = PAL.getparamalignment
1634         // size = typeallocsize of element type
1635         unsigned align = PAL.getParamAlignment(paramIndex + 1);
1636         if (align == 0)
1637           align = TD->getABITypeAlignment(Ty);
1638
1639         unsigned sz = TD->getTypeAllocSize(Ty);
1640         O << "\t.param .align " << align << " .b8 ";
1641         printParamName(I, paramIndex, O);
1642         O << "[" << sz << "]";
1643
1644         continue;
1645       }
1646       // Just a scalar
1647       const PointerType *PTy = dyn_cast<PointerType>(Ty);
1648       if (isKernelFunc) {
1649         if (PTy) {
1650           // Special handling for pointer arguments to kernel
1651           O << "\t.param .u" << thePointerTy.getSizeInBits() << " ";
1652
1653           if (nvptxSubtarget.getDrvInterface() != NVPTX::CUDA) {
1654             Type *ETy = PTy->getElementType();
1655             int addrSpace = PTy->getAddressSpace();
1656             switch (addrSpace) {
1657             default:
1658               O << ".ptr ";
1659               break;
1660             case llvm::ADDRESS_SPACE_CONST:
1661               O << ".ptr .const ";
1662               break;
1663             case llvm::ADDRESS_SPACE_SHARED:
1664               O << ".ptr .shared ";
1665               break;
1666             case llvm::ADDRESS_SPACE_GLOBAL:
1667               O << ".ptr .global ";
1668               break;
1669             }
1670             O << ".align " << (int) getOpenCLAlignment(TD, ETy) << " ";
1671           }
1672           printParamName(I, paramIndex, O);
1673           continue;
1674         }
1675
1676         // non-pointer scalar to kernel func
1677         O << "\t.param .";
1678         // Special case: predicate operands become .u8 types
1679         if (Ty->isIntegerTy(1))
1680           O << "u8";
1681         else
1682           O << getPTXFundamentalTypeStr(Ty);
1683         O << " ";
1684         printParamName(I, paramIndex, O);
1685         continue;
1686       }
1687       // Non-kernel function, just print .param .b<size> for ABI
1688       // and .reg .b<size> for non-ABI
1689       unsigned sz = 0;
1690       if (isa<IntegerType>(Ty)) {
1691         sz = cast<IntegerType>(Ty)->getBitWidth();
1692         if (sz < 32)
1693           sz = 32;
1694       } else if (isa<PointerType>(Ty))
1695         sz = thePointerTy.getSizeInBits();
1696       else
1697         sz = Ty->getPrimitiveSizeInBits();
1698       if (isABI)
1699         O << "\t.param .b" << sz << " ";
1700       else
1701         O << "\t.reg .b" << sz << " ";
1702       printParamName(I, paramIndex, O);
1703       continue;
1704     }
1705
1706     // param has byVal attribute. So should be a pointer
1707     const PointerType *PTy = dyn_cast<PointerType>(Ty);
1708     assert(PTy && "Param with byval attribute should be a pointer type");
1709     Type *ETy = PTy->getElementType();
1710
1711     if (isABI || isKernelFunc) {
1712       // Just print .param .align <a> .b8 .param[size];
1713       // <a> = PAL.getparamalignment
1714       // size = typeallocsize of element type
1715       unsigned align = PAL.getParamAlignment(paramIndex + 1);
1716       if (align == 0)
1717         align = TD->getABITypeAlignment(ETy);
1718
1719       unsigned sz = TD->getTypeAllocSize(ETy);
1720       O << "\t.param .align " << align << " .b8 ";
1721       printParamName(I, paramIndex, O);
1722       O << "[" << sz << "]";
1723       continue;
1724     } else {
1725       // Split the ETy into constituent parts and
1726       // print .param .b<size> <name> for each part.
1727       // Further, if a part is vector, print the above for
1728       // each vector element.
1729       SmallVector<EVT, 16> vtparts;
1730       ComputeValueVTs(*TLI, ETy, vtparts);
1731       for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
1732         unsigned elems = 1;
1733         EVT elemtype = vtparts[i];
1734         if (vtparts[i].isVector()) {
1735           elems = vtparts[i].getVectorNumElements();
1736           elemtype = vtparts[i].getVectorElementType();
1737         }
1738
1739         for (unsigned j = 0, je = elems; j != je; ++j) {
1740           unsigned sz = elemtype.getSizeInBits();
1741           if (elemtype.isInteger() && (sz < 32))
1742             sz = 32;
1743           O << "\t.reg .b" << sz << " ";
1744           printParamName(I, paramIndex, O);
1745           if (j < je - 1)
1746             O << ",\n";
1747           ++paramIndex;
1748         }
1749         if (i < e - 1)
1750           O << ",\n";
1751       }
1752       --paramIndex;
1753       continue;
1754     }
1755   }
1756
1757   O << "\n)\n";
1758 }
1759
1760 void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF,
1761                                             raw_ostream &O) {
1762   const Function *F = MF.getFunction();
1763   emitFunctionParamList(F, O);
1764 }
1765
1766 void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters(
1767     const MachineFunction &MF) {
1768   SmallString<128> Str;
1769   raw_svector_ostream O(Str);
1770
1771   // Map the global virtual register number to a register class specific
1772   // virtual register number starting from 1 with that class.
1773   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1774   //unsigned numRegClasses = TRI->getNumRegClasses();
1775
1776   // Emit the Fake Stack Object
1777   const MachineFrameInfo *MFI = MF.getFrameInfo();
1778   int NumBytes = (int) MFI->getStackSize();
1779   if (NumBytes) {
1780     O << "\t.local .align " << MFI->getMaxAlignment() << " .b8 \t" << DEPOTNAME
1781       << getFunctionNumber() << "[" << NumBytes << "];\n";
1782     if (nvptxSubtarget.is64Bit()) {
1783       O << "\t.reg .b64 \t%SP;\n";
1784       O << "\t.reg .b64 \t%SPL;\n";
1785     } else {
1786       O << "\t.reg .b32 \t%SP;\n";
1787       O << "\t.reg .b32 \t%SPL;\n";
1788     }
1789   }
1790
1791   // Go through all virtual registers to establish the mapping between the
1792   // global virtual
1793   // register number and the per class virtual register number.
1794   // We use the per class virtual register number in the ptx output.
1795   unsigned int numVRs = MRI->getNumVirtRegs();
1796   for (unsigned i = 0; i < numVRs; i++) {
1797     unsigned int vr = TRI->index2VirtReg(i);
1798     const TargetRegisterClass *RC = MRI->getRegClass(vr);
1799     DenseMap<unsigned, unsigned> &regmap = VRegMapping[RC];
1800     int n = regmap.size();
1801     regmap.insert(std::make_pair(vr, n + 1));
1802   }
1803
1804   // Emit register declarations
1805   // @TODO: Extract out the real register usage
1806   // O << "\t.reg .pred %p<" << NVPTXNumRegisters << ">;\n";
1807   // O << "\t.reg .s16 %rc<" << NVPTXNumRegisters << ">;\n";
1808   // O << "\t.reg .s16 %rs<" << NVPTXNumRegisters << ">;\n";
1809   // O << "\t.reg .s32 %r<" << NVPTXNumRegisters << ">;\n";
1810   // O << "\t.reg .s64 %rd<" << NVPTXNumRegisters << ">;\n";
1811   // O << "\t.reg .f32 %f<" << NVPTXNumRegisters << ">;\n";
1812   // O << "\t.reg .f64 %fd<" << NVPTXNumRegisters << ">;\n";
1813
1814   // Emit declaration of the virtual registers or 'physical' registers for
1815   // each register class
1816   for (unsigned i=0; i< TRI->getNumRegClasses(); i++) {
1817     const TargetRegisterClass *RC = TRI->getRegClass(i);
1818     DenseMap<unsigned, unsigned> &regmap = VRegMapping[RC];
1819     std::string rcname = getNVPTXRegClassName(RC);
1820     std::string rcStr = getNVPTXRegClassStr(RC);
1821     int n = regmap.size();
1822
1823     // Only declare those registers that may be used.
1824     if (n) {
1825        O << "\t.reg " << rcname << " \t" << rcStr << "<" << (n+1)
1826          << ">;\n";
1827     }
1828   }
1829
1830   OutStreamer.EmitRawText(O.str());
1831 }
1832
1833 void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) {
1834   APFloat APF = APFloat(Fp->getValueAPF()); // make a copy
1835   bool ignored;
1836   unsigned int numHex;
1837   const char *lead;
1838
1839   if (Fp->getType()->getTypeID() == Type::FloatTyID) {
1840     numHex = 8;
1841     lead = "0f";
1842     APF.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &ignored);
1843   } else if (Fp->getType()->getTypeID() == Type::DoubleTyID) {
1844     numHex = 16;
1845     lead = "0d";
1846     APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
1847   } else
1848     llvm_unreachable("unsupported fp type");
1849
1850   APInt API = APF.bitcastToAPInt();
1851   std::string hexstr(utohexstr(API.getZExtValue()));
1852   O << lead;
1853   if (hexstr.length() < numHex)
1854     O << std::string(numHex - hexstr.length(), '0');
1855   O << utohexstr(API.getZExtValue());
1856 }
1857
1858 void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
1859   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) {
1860     O << CI->getValue();
1861     return;
1862   }
1863   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
1864     printFPConstant(CFP, O);
1865     return;
1866   }
1867   if (isa<ConstantPointerNull>(CPV)) {
1868     O << "0";
1869     return;
1870   }
1871   if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
1872     PointerType *PTy = dyn_cast<PointerType>(GVar->getType());
1873     bool IsNonGenericPointer = false;
1874     if (PTy && PTy->getAddressSpace() != 0) {
1875       IsNonGenericPointer = true;
1876     }
1877     if (EmitGeneric && !isa<Function>(CPV) && !IsNonGenericPointer) {
1878       O << "generic(";
1879       O << *getSymbol(GVar);
1880       O << ")";
1881     } else {
1882       O << *getSymbol(GVar);
1883     }
1884     return;
1885   }
1886   if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1887     const Value *v = Cexpr->stripPointerCasts();
1888     PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType());
1889     bool IsNonGenericPointer = false;
1890     if (PTy && PTy->getAddressSpace() != 0) {
1891       IsNonGenericPointer = true;
1892     }
1893     if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
1894       if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
1895         O << "generic(";
1896         O << *getSymbol(GVar);
1897         O << ")";
1898       } else {
1899         O << *getSymbol(GVar);
1900       }
1901       return;
1902     } else {
1903       O << *LowerConstant(CPV, *this);
1904       return;
1905     }
1906   }
1907   llvm_unreachable("Not scalar type found in printScalarConstant()");
1908 }
1909
1910 void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
1911                                    AggBuffer *aggBuffer) {
1912
1913   const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
1914
1915   if (isa<UndefValue>(CPV) || CPV->isNullValue()) {
1916     int s = TD->getTypeAllocSize(CPV->getType());
1917     if (s < Bytes)
1918       s = Bytes;
1919     aggBuffer->addZeros(s);
1920     return;
1921   }
1922
1923   unsigned char *ptr;
1924   switch (CPV->getType()->getTypeID()) {
1925
1926   case Type::IntegerTyID: {
1927     const Type *ETy = CPV->getType();
1928     if (ETy == Type::getInt8Ty(CPV->getContext())) {
1929       unsigned char c =
1930           (unsigned char)(dyn_cast<ConstantInt>(CPV))->getZExtValue();
1931       ptr = &c;
1932       aggBuffer->addBytes(ptr, 1, Bytes);
1933     } else if (ETy == Type::getInt16Ty(CPV->getContext())) {
1934       short int16 = (short)(dyn_cast<ConstantInt>(CPV))->getZExtValue();
1935       ptr = (unsigned char *)&int16;
1936       aggBuffer->addBytes(ptr, 2, Bytes);
1937     } else if (ETy == Type::getInt32Ty(CPV->getContext())) {
1938       if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1939         int int32 = (int)(constInt->getZExtValue());
1940         ptr = (unsigned char *)&int32;
1941         aggBuffer->addBytes(ptr, 4, Bytes);
1942         break;
1943       } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1944         if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
1945                 ConstantFoldConstantExpression(Cexpr, TD))) {
1946           int int32 = (int)(constInt->getZExtValue());
1947           ptr = (unsigned char *)&int32;
1948           aggBuffer->addBytes(ptr, 4, Bytes);
1949           break;
1950         }
1951         if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1952           Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1953           aggBuffer->addSymbol(v);
1954           aggBuffer->addZeros(4);
1955           break;
1956         }
1957       }
1958       llvm_unreachable("unsupported integer const type");
1959     } else if (ETy == Type::getInt64Ty(CPV->getContext())) {
1960       if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1961         long long int64 = (long long)(constInt->getZExtValue());
1962         ptr = (unsigned char *)&int64;
1963         aggBuffer->addBytes(ptr, 8, Bytes);
1964         break;
1965       } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1966         if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
1967                 ConstantFoldConstantExpression(Cexpr, TD))) {
1968           long long int64 = (long long)(constInt->getZExtValue());
1969           ptr = (unsigned char *)&int64;
1970           aggBuffer->addBytes(ptr, 8, Bytes);
1971           break;
1972         }
1973         if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1974           Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1975           aggBuffer->addSymbol(v);
1976           aggBuffer->addZeros(8);
1977           break;
1978         }
1979       }
1980       llvm_unreachable("unsupported integer const type");
1981     } else
1982       llvm_unreachable("unsupported integer const type");
1983     break;
1984   }
1985   case Type::FloatTyID:
1986   case Type::DoubleTyID: {
1987     const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV);
1988     const Type *Ty = CFP->getType();
1989     if (Ty == Type::getFloatTy(CPV->getContext())) {
1990       float float32 = (float) CFP->getValueAPF().convertToFloat();
1991       ptr = (unsigned char *)&float32;
1992       aggBuffer->addBytes(ptr, 4, Bytes);
1993     } else if (Ty == Type::getDoubleTy(CPV->getContext())) {
1994       double float64 = CFP->getValueAPF().convertToDouble();
1995       ptr = (unsigned char *)&float64;
1996       aggBuffer->addBytes(ptr, 8, Bytes);
1997     } else {
1998       llvm_unreachable("unsupported fp const type");
1999     }
2000     break;
2001   }
2002   case Type::PointerTyID: {
2003     if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
2004       aggBuffer->addSymbol(GVar);
2005     } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
2006       const Value *v = Cexpr->stripPointerCasts();
2007       aggBuffer->addSymbol(v);
2008     }
2009     unsigned int s = TD->getTypeAllocSize(CPV->getType());
2010     aggBuffer->addZeros(s);
2011     break;
2012   }
2013
2014   case Type::ArrayTyID:
2015   case Type::VectorTyID:
2016   case Type::StructTyID: {
2017     if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV) ||
2018         isa<ConstantStruct>(CPV) || isa<ConstantDataSequential>(CPV)) {
2019       int ElementSize = TD->getTypeAllocSize(CPV->getType());
2020       bufferAggregateConstant(CPV, aggBuffer);
2021       if (Bytes > ElementSize)
2022         aggBuffer->addZeros(Bytes - ElementSize);
2023     } else if (isa<ConstantAggregateZero>(CPV))
2024       aggBuffer->addZeros(Bytes);
2025     else
2026       llvm_unreachable("Unexpected Constant type");
2027     break;
2028   }
2029
2030   default:
2031     llvm_unreachable("unsupported type");
2032   }
2033 }
2034
2035 void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV,
2036                                               AggBuffer *aggBuffer) {
2037   const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
2038   int Bytes;
2039
2040   // Old constants
2041   if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) {
2042     if (CPV->getNumOperands())
2043       for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i)
2044         bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer);
2045     return;
2046   }
2047
2048   if (const ConstantDataSequential *CDS =
2049           dyn_cast<ConstantDataSequential>(CPV)) {
2050     if (CDS->getNumElements())
2051       for (unsigned i = 0; i < CDS->getNumElements(); ++i)
2052         bufferLEByte(cast<Constant>(CDS->getElementAsConstant(i)), 0,
2053                      aggBuffer);
2054     return;
2055   }
2056
2057   if (isa<ConstantStruct>(CPV)) {
2058     if (CPV->getNumOperands()) {
2059       StructType *ST = cast<StructType>(CPV->getType());
2060       for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) {
2061         if (i == (e - 1))
2062           Bytes = TD->getStructLayout(ST)->getElementOffset(0) +
2063                   TD->getTypeAllocSize(ST) -
2064                   TD->getStructLayout(ST)->getElementOffset(i);
2065         else
2066           Bytes = TD->getStructLayout(ST)->getElementOffset(i + 1) -
2067                   TD->getStructLayout(ST)->getElementOffset(i);
2068         bufferLEByte(cast<Constant>(CPV->getOperand(i)), Bytes, aggBuffer);
2069       }
2070     }
2071     return;
2072   }
2073   llvm_unreachable("unsupported constant type in printAggregateConstant()");
2074 }
2075
2076 // buildTypeNameMap - Run through symbol table looking for type names.
2077 //
2078
2079 bool NVPTXAsmPrinter::isImageType(const Type *Ty) {
2080
2081   std::map<const Type *, std::string>::iterator PI = TypeNameMap.find(Ty);
2082
2083   if (PI != TypeNameMap.end() && (!PI->second.compare("struct._image1d_t") ||
2084                                   !PI->second.compare("struct._image2d_t") ||
2085                                   !PI->second.compare("struct._image3d_t")))
2086     return true;
2087
2088   return false;
2089 }
2090
2091
2092 bool NVPTXAsmPrinter::ignoreLoc(const MachineInstr &MI) {
2093   switch (MI.getOpcode()) {
2094   default:
2095     return false;
2096   case NVPTX::CallArgBeginInst:
2097   case NVPTX::CallArgEndInst0:
2098   case NVPTX::CallArgEndInst1:
2099   case NVPTX::CallArgF32:
2100   case NVPTX::CallArgF64:
2101   case NVPTX::CallArgI16:
2102   case NVPTX::CallArgI32:
2103   case NVPTX::CallArgI32imm:
2104   case NVPTX::CallArgI64:
2105   case NVPTX::CallArgParam:
2106   case NVPTX::CallVoidInst:
2107   case NVPTX::CallVoidInstReg:
2108   case NVPTX::Callseq_End:
2109   case NVPTX::CallVoidInstReg64:
2110   case NVPTX::DeclareParamInst:
2111   case NVPTX::DeclareRetMemInst:
2112   case NVPTX::DeclareRetRegInst:
2113   case NVPTX::DeclareRetScalarInst:
2114   case NVPTX::DeclareScalarParamInst:
2115   case NVPTX::DeclareScalarRegInst:
2116   case NVPTX::StoreParamF32:
2117   case NVPTX::StoreParamF64:
2118   case NVPTX::StoreParamI16:
2119   case NVPTX::StoreParamI32:
2120   case NVPTX::StoreParamI64:
2121   case NVPTX::StoreParamI8:
2122   case NVPTX::StoreRetvalF32:
2123   case NVPTX::StoreRetvalF64:
2124   case NVPTX::StoreRetvalI16:
2125   case NVPTX::StoreRetvalI32:
2126   case NVPTX::StoreRetvalI64:
2127   case NVPTX::StoreRetvalI8:
2128   case NVPTX::LastCallArgF32:
2129   case NVPTX::LastCallArgF64:
2130   case NVPTX::LastCallArgI16:
2131   case NVPTX::LastCallArgI32:
2132   case NVPTX::LastCallArgI32imm:
2133   case NVPTX::LastCallArgI64:
2134   case NVPTX::LastCallArgParam:
2135   case NVPTX::LoadParamMemF32:
2136   case NVPTX::LoadParamMemF64:
2137   case NVPTX::LoadParamMemI16:
2138   case NVPTX::LoadParamMemI32:
2139   case NVPTX::LoadParamMemI64:
2140   case NVPTX::LoadParamMemI8:
2141   case NVPTX::PrototypeInst:
2142   case NVPTX::DBG_VALUE:
2143     return true;
2144   }
2145   return false;
2146 }
2147
2148 /// PrintAsmOperand - Print out an operand for an inline asm expression.
2149 ///
2150 bool NVPTXAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
2151                                       unsigned AsmVariant,
2152                                       const char *ExtraCode, raw_ostream &O) {
2153   if (ExtraCode && ExtraCode[0]) {
2154     if (ExtraCode[1] != 0)
2155       return true; // Unknown modifier.
2156
2157     switch (ExtraCode[0]) {
2158     default:
2159       // See if this is a generic print operand
2160       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
2161     case 'r':
2162       break;
2163     }
2164   }
2165
2166   printOperand(MI, OpNo, O);
2167
2168   return false;
2169 }
2170
2171 bool NVPTXAsmPrinter::PrintAsmMemoryOperand(
2172     const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant,
2173     const char *ExtraCode, raw_ostream &O) {
2174   if (ExtraCode && ExtraCode[0])
2175     return true; // Unknown modifier
2176
2177   O << '[';
2178   printMemOperand(MI, OpNo, O);
2179   O << ']';
2180
2181   return false;
2182 }
2183
2184 void NVPTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
2185                                    raw_ostream &O, const char *Modifier) {
2186   const MachineOperand &MO = MI->getOperand(opNum);
2187   switch (MO.getType()) {
2188   case MachineOperand::MO_Register:
2189     if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
2190       if (MO.getReg() == NVPTX::VRDepot)
2191         O << DEPOTNAME << getFunctionNumber();
2192       else
2193         O << NVPTXInstPrinter::getRegisterName(MO.getReg());
2194     } else {
2195       emitVirtualRegister(MO.getReg(), O);
2196     }
2197     return;
2198
2199   case MachineOperand::MO_Immediate:
2200     if (!Modifier)
2201       O << MO.getImm();
2202     else if (strstr(Modifier, "vec") == Modifier)
2203       printVecModifiedImmediate(MO, Modifier, O);
2204     else
2205       llvm_unreachable(
2206           "Don't know how to handle modifier on immediate operand");
2207     return;
2208
2209   case MachineOperand::MO_FPImmediate:
2210     printFPConstant(MO.getFPImm(), O);
2211     break;
2212
2213   case MachineOperand::MO_GlobalAddress:
2214     O << *getSymbol(MO.getGlobal());
2215     break;
2216
2217   case MachineOperand::MO_MachineBasicBlock:
2218     O << *MO.getMBB()->getSymbol();
2219     return;
2220
2221   default:
2222     llvm_unreachable("Operand type not supported.");
2223   }
2224 }
2225
2226 void NVPTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
2227                                       raw_ostream &O, const char *Modifier) {
2228   printOperand(MI, opNum, O);
2229
2230   if (Modifier && !strcmp(Modifier, "add")) {
2231     O << ", ";
2232     printOperand(MI, opNum + 1, O);
2233   } else {
2234     if (MI->getOperand(opNum + 1).isImm() &&
2235         MI->getOperand(opNum + 1).getImm() == 0)
2236       return; // don't print ',0' or '+0'
2237     O << "+";
2238     printOperand(MI, opNum + 1, O);
2239   }
2240 }
2241
2242
2243 // Force static initialization.
2244 extern "C" void LLVMInitializeNVPTXBackendAsmPrinter() {
2245   RegisterAsmPrinter<NVPTXAsmPrinter> X(TheNVPTXTarget32);
2246   RegisterAsmPrinter<NVPTXAsmPrinter> Y(TheNVPTXTarget64);
2247 }
2248
2249 void NVPTXAsmPrinter::emitSrcInText(StringRef filename, unsigned line) {
2250   std::stringstream temp;
2251   LineReader *reader = this->getReader(filename.str());
2252   temp << "\n//";
2253   temp << filename.str();
2254   temp << ":";
2255   temp << line;
2256   temp << " ";
2257   temp << reader->readLine(line);
2258   temp << "\n";
2259   this->OutStreamer.EmitRawText(Twine(temp.str()));
2260 }
2261
2262 LineReader *NVPTXAsmPrinter::getReader(std::string filename) {
2263   if (!reader) {
2264     reader = new LineReader(filename);
2265   }
2266
2267   if (reader->fileName() != filename) {
2268     delete reader;
2269     reader = new LineReader(filename);
2270   }
2271
2272   return reader;
2273 }
2274
2275 std::string LineReader::readLine(unsigned lineNum) {
2276   if (lineNum < theCurLine) {
2277     theCurLine = 0;
2278     fstr.seekg(0, std::ios::beg);
2279   }
2280   while (theCurLine < lineNum) {
2281     fstr.getline(buff, 500);
2282     theCurLine++;
2283   }
2284   return buff;
2285 }
2286
2287 // Force static initialization.
2288 extern "C" void LLVMInitializeNVPTXAsmPrinter() {
2289   RegisterAsmPrinter<NVPTXAsmPrinter> X(TheNVPTXTarget32);
2290   RegisterAsmPrinter<NVPTXAsmPrinter> Y(TheNVPTXTarget64);
2291 }