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