blob: a8a82c6340617ec6d3ab129bb6641458fe921cc7 [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//===-- 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
Bill Wendlinge38859d2012-06-28 00:05:13 +000015#include "NVPTXAsmPrinter.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "InstPrinter/NVPTXInstPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "MCTargetDesc/NVPTXMCAsmInfo.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000018#include "NVPTX.h"
19#include "NVPTXInstrInfo.h"
Justin Holewinskia2a63d22013-08-06 14:13:27 +000020#include "NVPTXMCExpr.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "NVPTXMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "NVPTXRegisterInfo.h"
23#include "NVPTXTargetMachine.h"
24#include "NVPTXUtilities.h"
25#include "cl_common_defines.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000026#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Analysis/ConstantFolding.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000028#include "llvm/CodeGen/Analysis.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Jingyue Wu0220df02015-02-01 02:27:45 +000030#include "llvm/CodeGen/MachineLoopInfo.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000033#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/GlobalVariable.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000037#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/Module.h"
39#include "llvm/IR/Operator.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000040#include "llvm/MC/MCStreamer.h"
41#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/CommandLine.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/FormattedStream.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000045#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Support/TargetRegistry.h"
47#include "llvm/Support/TimeValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Target/TargetLoweringObjectFile.h"
Jingyue Wu0220df02015-02-01 02:27:45 +000049#include "llvm/Transforms/Utils/UnrollLoop.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000050#include <sstream>
Justin Holewinskiae556d32012-05-04 20:18:50 +000051using namespace llvm;
52
Justin Holewinskiae556d32012-05-04 20:18:50 +000053#define DEPOTNAME "__local_depot"
54
55static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000056EmitLineNumbers("nvptx-emit-line-numbers", cl::Hidden,
Justin Holewinskiae556d32012-05-04 20:18:50 +000057 cl::desc("NVPTX Specific: Emit Line numbers even without -G"),
58 cl::init(true));
59
Benjamin Kramer7ad41002013-10-27 11:31:46 +000060static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000061InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore, cl::Hidden,
Justin Holewinski0497ab12013-03-30 14:29:21 +000062 cl::desc("NVPTX Specific: Emit source line in ptx file"),
Benjamin Kramer7ad41002013-10-27 11:31:46 +000063 cl::init(false));
Justin Holewinskiae556d32012-05-04 20:18:50 +000064
Justin Holewinski2c5ac702012-11-16 21:03:51 +000065namespace {
66/// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V
67/// depends.
Justin Holewinski01f89f02013-05-20 12:13:32 +000068void DiscoverDependentGlobals(const Value *V,
69 DenseSet<const GlobalVariable *> &Globals) {
70 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
Justin Holewinski2c5ac702012-11-16 21:03:51 +000071 Globals.insert(GV);
72 else {
Justin Holewinski01f89f02013-05-20 12:13:32 +000073 if (const User *U = dyn_cast<User>(V)) {
Justin Holewinski2c5ac702012-11-16 21:03:51 +000074 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) {
75 DiscoverDependentGlobals(U->getOperand(i), Globals);
76 }
77 }
78 }
79}
Justin Holewinskiae556d32012-05-04 20:18:50 +000080
Justin Holewinski2c5ac702012-11-16 21:03:51 +000081/// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable
82/// instances to be emitted, but only after any dependents have been added
83/// first.
Justin Holewinski0497ab12013-03-30 14:29:21 +000084void VisitGlobalVariableForEmission(
Justin Holewinski01f89f02013-05-20 12:13:32 +000085 const GlobalVariable *GV, SmallVectorImpl<const GlobalVariable *> &Order,
86 DenseSet<const GlobalVariable *> &Visited,
87 DenseSet<const GlobalVariable *> &Visiting) {
Justin Holewinski2c5ac702012-11-16 21:03:51 +000088 // Have we already visited this one?
Justin Holewinski0497ab12013-03-30 14:29:21 +000089 if (Visited.count(GV))
90 return;
Justin Holewinski2c5ac702012-11-16 21:03:51 +000091
92 // Do we have a circular dependency?
Benjamin Kramer2c99e412014-10-10 15:32:50 +000093 if (!Visiting.insert(GV).second)
Justin Holewinski2c5ac702012-11-16 21:03:51 +000094 report_fatal_error("Circular dependency found in global variable set");
95
Justin Holewinski2c5ac702012-11-16 21:03:51 +000096 // Make sure we visit all dependents first
Justin Holewinski01f89f02013-05-20 12:13:32 +000097 DenseSet<const GlobalVariable *> Others;
Justin Holewinski2c5ac702012-11-16 21:03:51 +000098 for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i)
99 DiscoverDependentGlobals(GV->getOperand(i), Others);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000100
Justin Holewinski01f89f02013-05-20 12:13:32 +0000101 for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(),
102 E = Others.end();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000103 I != E; ++I)
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000104 VisitGlobalVariableForEmission(*I, Order, Visited, Visiting);
105
106 // Now we can visit ourself
107 Order.push_back(GV);
108 Visited.insert(GV);
109 Visiting.erase(GV);
110}
111}
Justin Holewinskiae556d32012-05-04 20:18:50 +0000112
Justin Holewinski0497ab12013-03-30 14:29:21 +0000113void NVPTXAsmPrinter::emitLineNumberAsDotLoc(const MachineInstr &MI) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000114 if (!EmitLineNumbers)
115 return;
116 if (ignoreLoc(MI))
117 return;
118
119 DebugLoc curLoc = MI.getDebugLoc();
120
121 if (prevDebugLoc.isUnknown() && curLoc.isUnknown())
122 return;
123
124 if (prevDebugLoc == curLoc)
125 return;
126
127 prevDebugLoc = curLoc;
128
129 if (curLoc.isUnknown())
130 return;
131
Justin Holewinskiae556d32012-05-04 20:18:50 +0000132 const MachineFunction *MF = MI.getParent()->getParent();
133 //const TargetMachine &TM = MF->getTarget();
134
135 const LLVMContext &ctx = MF->getFunction()->getContext();
136 DIScope Scope(curLoc.getScope(ctx));
137
Manman Ren983a16c2013-06-28 05:43:10 +0000138 assert((!Scope || Scope.isScope()) &&
139 "Scope of a DebugLoc should be null or a DIScope.");
140 if (!Scope)
141 return;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000142
143 StringRef fileName(Scope.getFilename());
144 StringRef dirName(Scope.getDirectory());
145 SmallString<128> FullPathName = dirName;
146 if (!dirName.empty() && !sys::path::is_absolute(fileName)) {
147 sys::path::append(FullPathName, fileName);
148 fileName = FullPathName.str();
149 }
150
151 if (filenameMap.find(fileName.str()) == filenameMap.end())
152 return;
153
Justin Holewinskiae556d32012-05-04 20:18:50 +0000154 // Emit the line from the source file.
Benjamin Kramer7ad41002013-10-27 11:31:46 +0000155 if (InterleaveSrc)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000156 this->emitSrcInText(fileName.str(), curLoc.getLine());
157
158 std::stringstream temp;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000159 temp << "\t.loc " << filenameMap[fileName.str()] << " " << curLoc.getLine()
160 << " " << curLoc.getCol();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000161 OutStreamer.EmitRawText(Twine(temp.str().c_str()));
162}
163
164void NVPTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
165 SmallString<128> Str;
166 raw_svector_ostream OS(Str);
Eric Christopherbeffc4e2015-02-19 00:08:23 +0000167 if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000168 emitLineNumberAsDotLoc(*MI);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000169
170 MCInst Inst;
171 lowerToMCInst(MI, Inst);
David Woodhousee6c13e42014-01-28 23:12:42 +0000172 EmitToStreamer(OutStreamer, Inst);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000173}
174
Justin Holewinski30d56a72014-04-09 15:39:15 +0000175// Handle symbol backtracking for targets that do not support image handles
176bool NVPTXAsmPrinter::lowerImageHandleOperand(const MachineInstr *MI,
177 unsigned OpNo, MCOperand &MCOp) {
178 const MachineOperand &MO = MI->getOperand(OpNo);
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000179 const MCInstrDesc &MCID = MI->getDesc();
Justin Holewinski30d56a72014-04-09 15:39:15 +0000180
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000181 if (MCID.TSFlags & NVPTXII::IsTexFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000182 // This is a texture fetch, so operand 4 is a texref and operand 5 is
183 // a samplerref
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000184 if (OpNo == 4 && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000185 lowerImageHandleSymbol(MO.getImm(), MCOp);
186 return true;
187 }
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000188 if (OpNo == 5 && MO.isImm() && !(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000189 lowerImageHandleSymbol(MO.getImm(), MCOp);
190 return true;
191 }
192
193 return false;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000194 } else if (MCID.TSFlags & NVPTXII::IsSuldMask) {
195 unsigned VecSize =
196 1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1);
197
198 // For a surface load of vector size N, the Nth operand will be the surfref
199 if (OpNo == VecSize && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000200 lowerImageHandleSymbol(MO.getImm(), MCOp);
201 return true;
202 }
203
204 return false;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000205 } else if (MCID.TSFlags & NVPTXII::IsSustFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000206 // This is a surface store, so operand 0 is a surfref
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000207 if (OpNo == 0 && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000208 lowerImageHandleSymbol(MO.getImm(), MCOp);
209 return true;
210 }
211
212 return false;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000213 } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000214 // This is a query, so operand 1 is a surfref/texref
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000215 if (OpNo == 1 && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000216 lowerImageHandleSymbol(MO.getImm(), MCOp);
217 return true;
218 }
219
220 return false;
221 }
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000222
223 return false;
Justin Holewinski30d56a72014-04-09 15:39:15 +0000224}
225
226void NVPTXAsmPrinter::lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp) {
227 // Ewwww
228 TargetMachine &TM = const_cast<TargetMachine&>(MF->getTarget());
229 NVPTXTargetMachine &nvTM = static_cast<NVPTXTargetMachine&>(TM);
230 const NVPTXMachineFunctionInfo *MFI = MF->getInfo<NVPTXMachineFunctionInfo>();
231 const char *Sym = MFI->getImageHandleSymbol(Index);
232 std::string *SymNamePtr =
233 nvTM.getManagedStrPool()->getManagedString(Sym);
234 MCOp = GetSymbolRef(OutContext.GetOrCreateSymbol(
235 StringRef(SymNamePtr->c_str())));
236}
237
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000238void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) {
239 OutMI.setOpcode(MI->getOpcode());
Justin Holewinski3d49e5c2013-11-15 12:30:04 +0000240 // Special: Do not mangle symbol operand of CALL_PROTOTYPE
241 if (MI->getOpcode() == NVPTX::CALL_PROTOTYPE) {
242 const MachineOperand &MO = MI->getOperand(0);
Justin Holewinski30d56a72014-04-09 15:39:15 +0000243 OutMI.addOperand(GetSymbolRef(
Justin Holewinski3d49e5c2013-11-15 12:30:04 +0000244 OutContext.GetOrCreateSymbol(Twine(MO.getSymbolName()))));
245 return;
246 }
247
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000248 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
249 const MachineOperand &MO = MI->getOperand(i);
250
251 MCOperand MCOp;
Eric Christopher6aad8b12015-02-19 00:08:14 +0000252 if (!nvptxSubtarget->hasImageHandles()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000253 if (lowerImageHandleOperand(MI, i, MCOp)) {
254 OutMI.addOperand(MCOp);
255 continue;
256 }
257 }
258
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000259 if (lowerOperand(MO, MCOp))
260 OutMI.addOperand(MCOp);
261 }
262}
263
264bool NVPTXAsmPrinter::lowerOperand(const MachineOperand &MO,
265 MCOperand &MCOp) {
266 switch (MO.getType()) {
267 default: llvm_unreachable("unknown operand type");
268 case MachineOperand::MO_Register:
269 MCOp = MCOperand::CreateReg(encodeVirtualRegister(MO.getReg()));
270 break;
271 case MachineOperand::MO_Immediate:
272 MCOp = MCOperand::CreateImm(MO.getImm());
273 break;
274 case MachineOperand::MO_MachineBasicBlock:
275 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
276 MO.getMBB()->getSymbol(), OutContext));
277 break;
278 case MachineOperand::MO_ExternalSymbol:
Justin Holewinski30d56a72014-04-09 15:39:15 +0000279 MCOp = GetSymbolRef(GetExternalSymbolSymbol(MO.getSymbolName()));
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000280 break;
281 case MachineOperand::MO_GlobalAddress:
Justin Holewinski30d56a72014-04-09 15:39:15 +0000282 MCOp = GetSymbolRef(getSymbol(MO.getGlobal()));
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000283 break;
284 case MachineOperand::MO_FPImmediate: {
285 const ConstantFP *Cnt = MO.getFPImm();
286 APFloat Val = Cnt->getValueAPF();
287
288 switch (Cnt->getType()->getTypeID()) {
289 default: report_fatal_error("Unsupported FP type"); break;
290 case Type::FloatTyID:
291 MCOp = MCOperand::CreateExpr(
292 NVPTXFloatMCExpr::CreateConstantFPSingle(Val, OutContext));
293 break;
294 case Type::DoubleTyID:
295 MCOp = MCOperand::CreateExpr(
296 NVPTXFloatMCExpr::CreateConstantFPDouble(Val, OutContext));
297 break;
298 }
299 break;
300 }
301 }
302 return true;
303}
304
305unsigned NVPTXAsmPrinter::encodeVirtualRegister(unsigned Reg) {
Justin Holewinski871ec932013-08-06 14:13:31 +0000306 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
307 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000308
Justin Holewinski871ec932013-08-06 14:13:31 +0000309 DenseMap<unsigned, unsigned> &RegMap = VRegMapping[RC];
310 unsigned RegNum = RegMap[Reg];
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000311
Justin Holewinski871ec932013-08-06 14:13:31 +0000312 // Encode the register class in the upper 4 bits
313 // Must be kept in sync with NVPTXInstPrinter::printRegName
314 unsigned Ret = 0;
315 if (RC == &NVPTX::Int1RegsRegClass) {
316 Ret = (1 << 28);
317 } else if (RC == &NVPTX::Int16RegsRegClass) {
318 Ret = (2 << 28);
319 } else if (RC == &NVPTX::Int32RegsRegClass) {
320 Ret = (3 << 28);
321 } else if (RC == &NVPTX::Int64RegsRegClass) {
322 Ret = (4 << 28);
323 } else if (RC == &NVPTX::Float32RegsRegClass) {
324 Ret = (5 << 28);
325 } else if (RC == &NVPTX::Float64RegsRegClass) {
326 Ret = (6 << 28);
327 } else {
328 report_fatal_error("Bad register class");
329 }
330
331 // Insert the vreg number
332 Ret |= (RegNum & 0x0FFFFFFF);
333 return Ret;
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000334 } else {
Justin Holewinski871ec932013-08-06 14:13:31 +0000335 // Some special-use registers are actually physical registers.
336 // Encode this as the register class ID of 0 and the real register ID.
337 return Reg & 0x0FFFFFFF;
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000338 }
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000339}
340
Justin Holewinski30d56a72014-04-09 15:39:15 +0000341MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol *Symbol) {
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000342 const MCExpr *Expr;
Justin Holewinski8b24e1e2013-08-06 23:06:42 +0000343 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None,
344 OutContext);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000345 return MCOperand::CreateExpr(Expr);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000346}
347
Justin Holewinski0497ab12013-03-30 14:29:21 +0000348void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
Eric Christopher8b770652015-01-26 19:03:15 +0000349 const DataLayout *TD = TM.getDataLayout();
Eric Christopher6aad8b12015-02-19 00:08:14 +0000350 const TargetLowering *TLI = nvptxSubtarget->getTargetLowering();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000351
352 Type *Ty = F->getReturnType();
353
Eric Christopher6aad8b12015-02-19 00:08:14 +0000354 bool isABI = (nvptxSubtarget->getSmVersion() >= 20);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000355
356 if (Ty->getTypeID() == Type::VoidTyID)
357 return;
358
359 O << " (";
360
361 if (isABI) {
Rafael Espindola08013342013-12-07 19:34:20 +0000362 if (Ty->isFloatingPointTy() || Ty->isIntegerTy()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000363 unsigned size = 0;
364 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
365 size = ITy->getBitWidth();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000366 if (size < 32)
367 size = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000368 } else {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000369 assert(Ty->isFloatingPointTy() && "Floating point type expected here");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000370 size = Ty->getPrimitiveSizeInBits();
371 }
372
373 O << ".param .b" << size << " func_retval0";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000374 } else if (isa<PointerType>(Ty)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000375 O << ".param .b" << TLI->getPointerTy().getSizeInBits()
Justin Holewinski0497ab12013-03-30 14:29:21 +0000376 << " func_retval0";
Craig Topperd3c02f12015-01-05 10:15:49 +0000377 } else if ((Ty->getTypeID() == Type::StructTyID) || isa<VectorType>(Ty)) {
378 unsigned totalsz = TD->getTypeAllocSize(Ty);
379 unsigned retAlignment = 0;
380 if (!llvm::getAlign(*F, 0, retAlignment))
381 retAlignment = TD->getABITypeAlignment(Ty);
382 O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz
383 << "]";
384 } else
385 llvm_unreachable("Unknown return type");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000386 } else {
387 SmallVector<EVT, 16> vtparts;
388 ComputeValueVTs(*TLI, Ty, vtparts);
389 unsigned idx = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000390 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000391 unsigned elems = 1;
392 EVT elemtype = vtparts[i];
393 if (vtparts[i].isVector()) {
394 elems = vtparts[i].getVectorNumElements();
395 elemtype = vtparts[i].getVectorElementType();
396 }
397
Justin Holewinski0497ab12013-03-30 14:29:21 +0000398 for (unsigned j = 0, je = elems; j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000399 unsigned sz = elemtype.getSizeInBits();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000400 if (elemtype.isInteger() && (sz < 32))
401 sz = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000402 O << ".reg .b" << sz << " func_retval" << idx;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000403 if (j < je - 1)
404 O << ", ";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000405 ++idx;
406 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000407 if (i < e - 1)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000408 O << ", ";
409 }
410 }
411 O << ") ";
412 return;
413}
414
415void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF,
416 raw_ostream &O) {
417 const Function *F = MF.getFunction();
418 printReturnValStr(F, O);
419}
420
Jingyue Wu0220df02015-02-01 02:27:45 +0000421// Return true if MBB is the header of a loop marked with
422// llvm.loop.unroll.disable.
Jingyue Wu5bbcdaa2015-02-03 17:57:38 +0000423// TODO: consider "#pragma unroll 1" which is equivalent to "#pragma nounroll".
Jingyue Wu0220df02015-02-01 02:27:45 +0000424bool NVPTXAsmPrinter::isLoopHeaderOfNoUnroll(
425 const MachineBasicBlock &MBB) const {
426 MachineLoopInfo &LI = getAnalysis<MachineLoopInfo>();
Jingyue Wu5bbcdaa2015-02-03 17:57:38 +0000427 // TODO: isLoopHeader() should take "const MachineBasicBlock *".
Jingyue Wu0220df02015-02-01 02:27:45 +0000428 // We insert .pragma "nounroll" only to the loop header.
429 if (!LI.isLoopHeader(const_cast<MachineBasicBlock *>(&MBB)))
430 return false;
431
432 // llvm.loop.unroll.disable is marked on the back edges of a loop. Therefore,
433 // we iterate through each back edge of the loop with header MBB, and check
434 // whether its metadata contains llvm.loop.unroll.disable.
435 for (auto I = MBB.pred_begin(); I != MBB.pred_end(); ++I) {
436 const MachineBasicBlock *PMBB = *I;
437 if (LI.getLoopFor(PMBB) != LI.getLoopFor(&MBB)) {
438 // Edges from other loops to MBB are not back edges.
439 continue;
440 }
441 if (const BasicBlock *PBB = PMBB->getBasicBlock()) {
Jingyue Wu49a766e2015-02-02 20:41:11 +0000442 if (MDNode *LoopID = PBB->getTerminator()->getMetadata("llvm.loop")) {
Jingyue Wu0220df02015-02-01 02:27:45 +0000443 if (GetUnrollMetadata(LoopID, "llvm.loop.unroll.disable"))
444 return true;
445 }
446 }
447 }
448 return false;
449}
450
451void NVPTXAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
452 AsmPrinter::EmitBasicBlockStart(MBB);
453 if (isLoopHeaderOfNoUnroll(MBB))
454 OutStreamer.EmitRawText(StringRef("\t.pragma \"nounroll\";\n"));
455}
456
Justin Holewinskiae556d32012-05-04 20:18:50 +0000457void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
458 SmallString<128> Str;
459 raw_svector_ostream O(Str);
460
Justin Holewinski01f89f02013-05-20 12:13:32 +0000461 if (!GlobalsEmitted) {
462 emitGlobals(*MF->getFunction()->getParent());
463 GlobalsEmitted = true;
464 }
465
Justin Holewinskiae556d32012-05-04 20:18:50 +0000466 // Set up
467 MRI = &MF->getRegInfo();
468 F = MF->getFunction();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000469 emitLinkageDirective(F, O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000470 if (llvm::isKernelFunction(*F))
471 O << ".entry ";
472 else {
473 O << ".func ";
474 printReturnValStr(*MF, O);
475 }
476
477 O << *CurrentFnSym;
478
479 emitFunctionParamList(*MF, O);
480
481 if (llvm::isKernelFunction(*F))
482 emitKernelFunctionDirectives(*F, O);
483
484 OutStreamer.EmitRawText(O.str());
485
486 prevDebugLoc = DebugLoc();
487}
488
489void NVPTXAsmPrinter::EmitFunctionBodyStart() {
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +0000490 VRegMapping.clear();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000491 OutStreamer.EmitRawText(StringRef("{\n"));
492 setAndEmitFunctionVirtualRegisters(*MF);
493
494 SmallString<128> Str;
495 raw_svector_ostream O(Str);
496 emitDemotedVars(MF->getFunction(), O);
497 OutStreamer.EmitRawText(O.str());
498}
499
500void NVPTXAsmPrinter::EmitFunctionBodyEnd() {
501 OutStreamer.EmitRawText(StringRef("}\n"));
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +0000502 VRegMapping.clear();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000503}
504
Justin Holewinski660597d2013-10-11 12:39:36 +0000505void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
506 unsigned RegNo = MI->getOperand(0).getReg();
Eric Christopher6aad8b12015-02-19 00:08:14 +0000507 const TargetRegisterInfo *TRI = nvptxSubtarget->getRegisterInfo();
Justin Holewinski660597d2013-10-11 12:39:36 +0000508 if (TRI->isVirtualRegister(RegNo)) {
509 OutStreamer.AddComment(Twine("implicit-def: ") +
510 getVirtualRegisterName(RegNo));
511 } else {
Eric Christopher6aad8b12015-02-19 00:08:14 +0000512 OutStreamer.AddComment(Twine("implicit-def: ") +
513 nvptxSubtarget->getRegisterInfo()->getName(RegNo));
Justin Holewinski660597d2013-10-11 12:39:36 +0000514 }
515 OutStreamer.AddBlankLine();
516}
517
Justin Holewinski0497ab12013-03-30 14:29:21 +0000518void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F,
519 raw_ostream &O) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000520 // If the NVVM IR has some of reqntid* specified, then output
521 // the reqntid directive, and set the unspecified ones to 1.
522 // If none of reqntid* is specified, don't output reqntid directive.
523 unsigned reqntidx, reqntidy, reqntidz;
524 bool specified = false;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000525 if (llvm::getReqNTIDx(F, reqntidx) == false)
526 reqntidx = 1;
527 else
528 specified = true;
529 if (llvm::getReqNTIDy(F, reqntidy) == false)
530 reqntidy = 1;
531 else
532 specified = true;
533 if (llvm::getReqNTIDz(F, reqntidz) == false)
534 reqntidz = 1;
535 else
536 specified = true;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000537
538 if (specified)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000539 O << ".reqntid " << reqntidx << ", " << reqntidy << ", " << reqntidz
540 << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000541
542 // If the NVVM IR has some of maxntid* specified, then output
543 // the maxntid directive, and set the unspecified ones to 1.
544 // If none of maxntid* is specified, don't output maxntid directive.
545 unsigned maxntidx, maxntidy, maxntidz;
546 specified = false;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000547 if (llvm::getMaxNTIDx(F, maxntidx) == false)
548 maxntidx = 1;
549 else
550 specified = true;
551 if (llvm::getMaxNTIDy(F, maxntidy) == false)
552 maxntidy = 1;
553 else
554 specified = true;
555 if (llvm::getMaxNTIDz(F, maxntidz) == false)
556 maxntidz = 1;
557 else
558 specified = true;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000559
560 if (specified)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000561 O << ".maxntid " << maxntidx << ", " << maxntidy << ", " << maxntidz
562 << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000563
564 unsigned mincta;
565 if (llvm::getMinCTASm(F, mincta))
566 O << ".minnctapersm " << mincta << "\n";
567}
568
Justin Holewinski660597d2013-10-11 12:39:36 +0000569std::string
570NVPTXAsmPrinter::getVirtualRegisterName(unsigned Reg) const {
571 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000572
Justin Holewinski660597d2013-10-11 12:39:36 +0000573 std::string Name;
574 raw_string_ostream NameStr(Name);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000575
Justin Holewinski660597d2013-10-11 12:39:36 +0000576 VRegRCMap::const_iterator I = VRegMapping.find(RC);
577 assert(I != VRegMapping.end() && "Bad register class");
578 const DenseMap<unsigned, unsigned> &RegMap = I->second;
579
580 VRegMap::const_iterator VI = RegMap.find(Reg);
581 assert(VI != RegMap.end() && "Bad virtual register");
582 unsigned MappedVR = VI->second;
583
584 NameStr << getNVPTXRegClassStr(RC) << MappedVR;
585
586 NameStr.flush();
587 return Name;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000588}
589
Justin Holewinski660597d2013-10-11 12:39:36 +0000590void NVPTXAsmPrinter::emitVirtualRegister(unsigned int vr,
Justin Holewinski0497ab12013-03-30 14:29:21 +0000591 raw_ostream &O) {
Justin Holewinski660597d2013-10-11 12:39:36 +0000592 O << getVirtualRegisterName(vr);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000593}
594
Justin Holewinski0497ab12013-03-30 14:29:21 +0000595void NVPTXAsmPrinter::printVecModifiedImmediate(
596 const MachineOperand &MO, const char *Modifier, raw_ostream &O) {
597 static const char vecelem[] = { '0', '1', '2', '3', '0', '1', '2', '3' };
598 int Imm = (int) MO.getImm();
599 if (0 == strcmp(Modifier, "vecelem"))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000600 O << "_" << vecelem[Imm];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000601 else if (0 == strcmp(Modifier, "vecv4comm1")) {
602 if ((Imm < 0) || (Imm > 3))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000603 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000604 } else if (0 == strcmp(Modifier, "vecv4comm2")) {
605 if ((Imm < 4) || (Imm > 7))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000606 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000607 } else if (0 == strcmp(Modifier, "vecv4pos")) {
608 if (Imm < 0)
609 Imm = 0;
610 O << "_" << vecelem[Imm % 4];
611 } else if (0 == strcmp(Modifier, "vecv2comm1")) {
612 if ((Imm < 0) || (Imm > 1))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000613 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000614 } else if (0 == strcmp(Modifier, "vecv2comm2")) {
615 if ((Imm < 2) || (Imm > 3))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000616 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000617 } else if (0 == strcmp(Modifier, "vecv2pos")) {
618 if (Imm < 0)
619 Imm = 0;
620 O << "_" << vecelem[Imm % 2];
621 } else
Craig Topperbdf39a42012-05-24 07:02:50 +0000622 llvm_unreachable("Unknown Modifier on immediate operand");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000623}
624
Justin Holewinskidc5e3b62013-06-28 17:58:04 +0000625
626
Justin Holewinski0497ab12013-03-30 14:29:21 +0000627void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000628
Justin Holewinski0497ab12013-03-30 14:29:21 +0000629 emitLinkageDirective(F, O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000630 if (llvm::isKernelFunction(*F))
631 O << ".entry ";
632 else
633 O << ".func ";
634 printReturnValStr(F, O);
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +0000635 O << *getSymbol(F) << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000636 emitFunctionParamList(F, O);
637 O << ";\n";
638}
639
Justin Holewinski0497ab12013-03-30 14:29:21 +0000640static bool usedInGlobalVarDef(const Constant *C) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000641 if (!C)
642 return false;
643
644 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
645 if (GV->getName().str() == "llvm.used")
646 return false;
647 return true;
648 }
649
Chandler Carruthcdf47882014-03-09 03:16:01 +0000650 for (const User *U : C->users())
651 if (const Constant *C = dyn_cast<Constant>(U))
652 if (usedInGlobalVarDef(C))
653 return true;
654
Justin Holewinskiae556d32012-05-04 20:18:50 +0000655 return false;
656}
657
Justin Holewinski0497ab12013-03-30 14:29:21 +0000658static bool usedInOneFunc(const User *U, Function const *&oneFunc) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000659 if (const GlobalVariable *othergv = dyn_cast<GlobalVariable>(U)) {
660 if (othergv->getName().str() == "llvm.used")
661 return true;
662 }
663
664 if (const Instruction *instr = dyn_cast<Instruction>(U)) {
665 if (instr->getParent() && instr->getParent()->getParent()) {
666 const Function *curFunc = instr->getParent()->getParent();
667 if (oneFunc && (curFunc != oneFunc))
668 return false;
669 oneFunc = curFunc;
670 return true;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000671 } else
Justin Holewinskiae556d32012-05-04 20:18:50 +0000672 return false;
673 }
674
Chandler Carruthcdf47882014-03-09 03:16:01 +0000675 for (const User *UU : U->users())
676 if (usedInOneFunc(UU, oneFunc) == false)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000677 return false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000678
Justin Holewinskiae556d32012-05-04 20:18:50 +0000679 return true;
680}
681
682/* Find out if a global variable can be demoted to local scope.
683 * Currently, this is valid for CUDA shared variables, which have local
684 * scope and global lifetime. So the conditions to check are :
685 * 1. Is the global variable in shared address space?
686 * 2. Does it have internal linkage?
687 * 3. Is the global variable referenced only in one function?
688 */
689static bool canDemoteGlobalVar(const GlobalVariable *gv, Function const *&f) {
690 if (gv->hasInternalLinkage() == false)
691 return false;
692 const PointerType *Pty = gv->getType();
693 if (Pty->getAddressSpace() != llvm::ADDRESS_SPACE_SHARED)
694 return false;
695
Craig Topper062a2ba2014-04-25 05:30:21 +0000696 const Function *oneFunc = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000697
698 bool flag = usedInOneFunc(gv, oneFunc);
699 if (flag == false)
700 return false;
701 if (!oneFunc)
702 return false;
703 f = oneFunc;
704 return true;
705}
706
707static bool useFuncSeen(const Constant *C,
708 llvm::DenseMap<const Function *, bool> &seenMap) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000709 for (const User *U : C->users()) {
710 if (const Constant *cu = dyn_cast<Constant>(U)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000711 if (useFuncSeen(cu, seenMap))
712 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000713 } else if (const Instruction *I = dyn_cast<Instruction>(U)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000714 const BasicBlock *bb = I->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000715 if (!bb)
716 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000717 const Function *caller = bb->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000718 if (!caller)
719 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000720 if (seenMap.find(caller) != seenMap.end())
721 return true;
722 }
723 }
724 return false;
725}
726
Justin Holewinski01f89f02013-05-20 12:13:32 +0000727void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000728 llvm::DenseMap<const Function *, bool> seenMap;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000729 for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000730 const Function *F = FI;
731
732 if (F->isDeclaration()) {
733 if (F->use_empty())
734 continue;
735 if (F->getIntrinsicID())
736 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000737 emitDeclaration(F, O);
738 continue;
739 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000740 for (const User *U : F->users()) {
741 if (const Constant *C = dyn_cast<Constant>(U)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000742 if (usedInGlobalVarDef(C)) {
743 // The use is in the initialization of a global variable
744 // that is a function pointer, so print a declaration
745 // for the original function
Justin Holewinskiae556d32012-05-04 20:18:50 +0000746 emitDeclaration(F, O);
747 break;
748 }
749 // Emit a declaration of this function if the function that
750 // uses this constant expr has already been seen.
751 if (useFuncSeen(C, seenMap)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000752 emitDeclaration(F, O);
753 break;
754 }
755 }
756
Chandler Carruthcdf47882014-03-09 03:16:01 +0000757 if (!isa<Instruction>(U))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000758 continue;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000759 const Instruction *instr = cast<Instruction>(U);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000760 const BasicBlock *bb = instr->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000761 if (!bb)
762 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000763 const Function *caller = bb->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000764 if (!caller)
765 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000766
767 // If a caller has already been seen, then the caller is
768 // appearing in the module before the callee. so print out
769 // a declaration for the callee.
770 if (seenMap.find(caller) != seenMap.end()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000771 emitDeclaration(F, O);
772 break;
773 }
774 }
775 seenMap[F] = true;
776 }
777}
778
779void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) {
780 DebugInfoFinder DbgFinder;
781 DbgFinder.processModule(M);
782
Justin Holewinski0497ab12013-03-30 14:29:21 +0000783 unsigned i = 1;
Alon Mishnead312152014-03-18 09:41:07 +0000784 for (DICompileUnit DIUnit : DbgFinder.compile_units()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000785 StringRef Filename(DIUnit.getFilename());
786 StringRef Dirname(DIUnit.getDirectory());
787 SmallString<128> FullPathName = Dirname;
788 if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
789 sys::path::append(FullPathName, Filename);
790 Filename = FullPathName.str();
791 }
792 if (filenameMap.find(Filename.str()) != filenameMap.end())
793 continue;
794 filenameMap[Filename.str()] = i;
795 OutStreamer.EmitDwarfFileDirective(i, "", Filename.str());
796 ++i;
797 }
798
Alon Mishnead312152014-03-18 09:41:07 +0000799 for (DISubprogram SP : DbgFinder.subprograms()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000800 StringRef Filename(SP.getFilename());
801 StringRef Dirname(SP.getDirectory());
802 SmallString<128> FullPathName = Dirname;
803 if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
804 sys::path::append(FullPathName, Filename);
805 Filename = FullPathName.str();
806 }
807 if (filenameMap.find(Filename.str()) != filenameMap.end())
808 continue;
809 filenameMap[Filename.str()] = i;
810 ++i;
811 }
812}
813
Justin Holewinski0497ab12013-03-30 14:29:21 +0000814bool NVPTXAsmPrinter::doInitialization(Module &M) {
Eric Christopher6aad8b12015-02-19 00:08:14 +0000815 // Construct a default subtarget off of the TargetMachine defaults. The
816 // rest of NVPTX isn't friendly to change subtargets per function and
817 // so the default TargetMachine will have all of the options.
818 StringRef TT = TM.getTargetTriple();
819 StringRef CPU = TM.getTargetCPU();
820 StringRef FS = TM.getTargetFeatureString();
821 const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM);
Eric Christopher02389e32015-02-19 00:08:27 +0000822 const NVPTXSubtarget STI(TT, CPU, FS, NTM);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000823
824 SmallString<128> Str1;
825 raw_svector_ostream OS1(Str1);
826
827 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
828 MMI->AnalyzeModule(M);
829
830 // We need to call the parent's one explicitly.
831 //bool Result = AsmPrinter::doInitialization(M);
832
833 // Initialize TargetLoweringObjectFile.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000834 const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
835 .Initialize(OutContext, TM);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000836
Eric Christopher8b770652015-01-26 19:03:15 +0000837 Mang = new Mangler(TM.getDataLayout());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000838
839 // Emit header before any dwarf directives are emitted below.
Eric Christopher6aad8b12015-02-19 00:08:14 +0000840 emitHeader(M, OS1, STI);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000841 OutStreamer.EmitRawText(OS1.str());
842
Justin Holewinskiae556d32012-05-04 20:18:50 +0000843 // Already commented out
844 //bool Result = AsmPrinter::doInitialization(M);
845
Justin Holewinskid2bbdf02013-07-01 13:00:14 +0000846 // Emit module-level inline asm if it exists.
847 if (!M.getModuleInlineAsm().empty()) {
848 OutStreamer.AddComment("Start of file scope inline assembly");
849 OutStreamer.AddBlankLine();
850 OutStreamer.EmitRawText(StringRef(M.getModuleInlineAsm()));
851 OutStreamer.AddBlankLine();
852 OutStreamer.AddComment("End of file scope inline assembly");
853 OutStreamer.AddBlankLine();
854 }
855
Eric Christopher6aad8b12015-02-19 00:08:14 +0000856 // If we're not NVCL we're CUDA, go ahead and emit filenames.
857 if (Triple(TM.getTargetTriple()).getOS() != Triple::NVCL)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000858 recordAndEmitFilenames(M);
859
Justin Holewinski01f89f02013-05-20 12:13:32 +0000860 GlobalsEmitted = false;
861
862 return false; // success
863}
864
865void NVPTXAsmPrinter::emitGlobals(const Module &M) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000866 SmallString<128> Str2;
867 raw_svector_ostream OS2(Str2);
868
869 emitDeclarations(M, OS2);
870
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000871 // As ptxas does not support forward references of globals, we need to first
872 // sort the list of module-level globals in def-use order. We visit each
873 // global variable in order, and ensure that we emit it *after* its dependent
874 // globals. We use a little extra memory maintaining both a set and a list to
875 // have fast searches while maintaining a strict ordering.
Justin Holewinski01f89f02013-05-20 12:13:32 +0000876 SmallVector<const GlobalVariable *, 8> Globals;
877 DenseSet<const GlobalVariable *> GVVisited;
878 DenseSet<const GlobalVariable *> GVVisiting;
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000879
880 // Visit each global variable, in order
Justin Holewinski01f89f02013-05-20 12:13:32 +0000881 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
882 I != E; ++I)
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000883 VisitGlobalVariableForEmission(I, Globals, GVVisited, GVVisiting);
884
Justin Holewinski0497ab12013-03-30 14:29:21 +0000885 assert(GVVisited.size() == M.getGlobalList().size() &&
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000886 "Missed a global variable");
887 assert(GVVisiting.size() == 0 && "Did not fully process a global variable");
888
889 // Print out module-level global variables in proper order
890 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
891 printModuleLevelGV(Globals[i], OS2);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000892
893 OS2 << '\n';
894
895 OutStreamer.EmitRawText(OS2.str());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000896}
897
Eric Christopher6aad8b12015-02-19 00:08:14 +0000898void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O,
899 const NVPTXSubtarget &STI) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000900 O << "//\n";
901 O << "// Generated by LLVM NVPTX Back-End\n";
902 O << "//\n";
903 O << "\n";
904
Eric Christopher6aad8b12015-02-19 00:08:14 +0000905 unsigned PTXVersion = STI.getPTXVersion();
Justin Holewinski1812ee92012-11-12 03:16:43 +0000906 O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000907
908 O << ".target ";
Eric Christopher6aad8b12015-02-19 00:08:14 +0000909 O << STI.getTargetName();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000910
Eric Christopherca929f22015-02-19 00:22:47 +0000911 const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM);
912 if (NTM.getDrvInterface() == NVPTX::NVCL)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000913 O << ", texmode_independent";
Eric Christopherbeffc4e2015-02-19 00:08:23 +0000914 else {
Eric Christopher6aad8b12015-02-19 00:08:14 +0000915 if (!STI.hasDouble())
Justin Holewinskiae556d32012-05-04 20:18:50 +0000916 O << ", map_f64_to_f32";
917 }
918
919 if (MAI->doesSupportDebugInformation())
920 O << ", debug";
921
922 O << "\n";
923
924 O << ".address_size ";
Eric Christopherca929f22015-02-19 00:22:47 +0000925 if (NTM.is64Bit())
Justin Holewinskiae556d32012-05-04 20:18:50 +0000926 O << "64";
927 else
928 O << "32";
929 O << "\n";
930
931 O << "\n";
932}
933
934bool NVPTXAsmPrinter::doFinalization(Module &M) {
Justin Holewinski01f89f02013-05-20 12:13:32 +0000935 // If we did not emit any functions, then the global declarations have not
936 // yet been emitted.
937 if (!GlobalsEmitted) {
938 emitGlobals(M);
939 GlobalsEmitted = true;
940 }
941
Justin Holewinskiae556d32012-05-04 20:18:50 +0000942 // XXX Temproarily remove global variables so that doFinalization() will not
943 // emit them again (global variables are emitted at beginning).
944
945 Module::GlobalListType &global_list = M.getGlobalList();
946 int i, n = global_list.size();
Dylan Noblesmithc9e2a272014-08-26 02:03:35 +0000947 GlobalVariable **gv_array = new GlobalVariable *[n];
Justin Holewinskiae556d32012-05-04 20:18:50 +0000948
949 // first, back-up GlobalVariable in gv_array
950 i = 0;
951 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000952 I != E; ++I)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000953 gv_array[i++] = &*I;
954
955 // second, empty global_list
956 while (!global_list.empty())
957 global_list.remove(global_list.begin());
958
959 // call doFinalization
960 bool ret = AsmPrinter::doFinalization(M);
961
962 // now we restore global variables
Justin Holewinski0497ab12013-03-30 14:29:21 +0000963 for (i = 0; i < n; i++)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000964 global_list.insert(global_list.end(), gv_array[i]);
965
Justin Holewinski59596952014-04-09 15:38:52 +0000966 clearAnnotationCache(&M);
Dylan Noblesmithc9e2a272014-08-26 02:03:35 +0000967
968 delete[] gv_array;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000969 return ret;
970
Justin Holewinskiae556d32012-05-04 20:18:50 +0000971 //bool Result = AsmPrinter::doFinalization(M);
972 // Instead of calling the parents doFinalization, we may
973 // clone parents doFinalization and customize here.
974 // Currently, we if NVISA out the EmitGlobals() in
975 // parent's doFinalization, which is too intrusive.
976 //
977 // Same for the doInitialization.
978 //return Result;
979}
980
981// This function emits appropriate linkage directives for
982// functions and global variables.
983//
984// extern function declaration -> .extern
985// extern function definition -> .visible
986// external global variable with init -> .visible
987// external without init -> .extern
988// appending -> not allowed, assert.
Justin Holewinski7d5bf662014-06-27 18:35:10 +0000989// for any linkage other than
990// internal, private, linker_private,
991// linker_private_weak, linker_private_weak_def_auto,
992// we emit -> .weak.
Justin Holewinskiae556d32012-05-04 20:18:50 +0000993
Justin Holewinski0497ab12013-03-30 14:29:21 +0000994void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V,
995 raw_ostream &O) {
Eric Christopherbeffc4e2015-02-19 00:08:23 +0000996 if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000997 if (V->hasExternalLinkage()) {
998 if (isa<GlobalVariable>(V)) {
999 const GlobalVariable *GVar = cast<GlobalVariable>(V);
1000 if (GVar) {
1001 if (GVar->hasInitializer())
1002 O << ".visible ";
1003 else
1004 O << ".extern ";
1005 }
1006 } else if (V->isDeclaration())
1007 O << ".extern ";
1008 else
1009 O << ".visible ";
1010 } else if (V->hasAppendingLinkage()) {
1011 std::string msg;
1012 msg.append("Error: ");
1013 msg.append("Symbol ");
1014 if (V->hasName())
1015 msg.append(V->getName().str());
1016 msg.append("has unsupported appending linkage type");
1017 llvm_unreachable(msg.c_str());
Justin Holewinski7d5bf662014-06-27 18:35:10 +00001018 } else if (!V->hasInternalLinkage() &&
1019 !V->hasPrivateLinkage()) {
1020 O << ".weak ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001021 }
1022 }
1023}
1024
Justin Holewinski01f89f02013-05-20 12:13:32 +00001025void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
1026 raw_ostream &O,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001027 bool processDemoted) {
1028
1029 // Skip meta data
1030 if (GVar->hasSection()) {
Rafael Espindola64c1e182014-06-03 02:41:57 +00001031 if (GVar->getSection() == StringRef("llvm.metadata"))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001032 return;
1033 }
1034
Justin Holewinski73cb5de2014-06-27 18:35:53 +00001035 // Skip LLVM intrinsic global variables
1036 if (GVar->getName().startswith("llvm.") ||
1037 GVar->getName().startswith("nvvm."))
1038 return;
1039
Eric Christopher8b770652015-01-26 19:03:15 +00001040 const DataLayout *TD = TM.getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001041
1042 // GlobalVariables are always constant pointers themselves.
1043 const PointerType *PTy = GVar->getType();
1044 Type *ETy = PTy->getElementType();
1045
1046 if (GVar->hasExternalLinkage()) {
1047 if (GVar->hasInitializer())
1048 O << ".visible ";
1049 else
1050 O << ".extern ";
Justin Holewinskid73767a2014-06-27 18:35:56 +00001051 } else if (GVar->hasLinkOnceLinkage() || GVar->hasWeakLinkage() ||
1052 GVar->hasAvailableExternallyLinkage() ||
1053 GVar->hasCommonLinkage()) {
1054 O << ".weak ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001055 }
1056
1057 if (llvm::isTexture(*GVar)) {
1058 O << ".global .texref " << llvm::getTextureName(*GVar) << ";\n";
1059 return;
1060 }
1061
1062 if (llvm::isSurface(*GVar)) {
1063 O << ".global .surfref " << llvm::getSurfaceName(*GVar) << ";\n";
1064 return;
1065 }
1066
1067 if (GVar->isDeclaration()) {
1068 // (extern) declarations, no definition or initializer
1069 // Currently the only known declaration is for an automatic __local
1070 // (.shared) promoted to global.
1071 emitPTXGlobalVariable(GVar, O);
1072 O << ";\n";
1073 return;
1074 }
1075
1076 if (llvm::isSampler(*GVar)) {
1077 O << ".global .samplerref " << llvm::getSamplerName(*GVar);
1078
Craig Topper062a2ba2014-04-25 05:30:21 +00001079 const Constant *Initializer = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001080 if (GVar->hasInitializer())
1081 Initializer = GVar->getInitializer();
Craig Topper062a2ba2014-04-25 05:30:21 +00001082 const ConstantInt *CI = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001083 if (Initializer)
1084 CI = dyn_cast<ConstantInt>(Initializer);
1085 if (CI) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001086 unsigned sample = CI->getZExtValue();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001087
1088 O << " = { ";
1089
Justin Holewinski0497ab12013-03-30 14:29:21 +00001090 for (int i = 0,
1091 addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE);
1092 i < 3; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001093 O << "addr_mode_" << i << " = ";
1094 switch (addr) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001095 case 0:
1096 O << "wrap";
1097 break;
1098 case 1:
1099 O << "clamp_to_border";
1100 break;
1101 case 2:
1102 O << "clamp_to_edge";
1103 break;
1104 case 3:
1105 O << "wrap";
1106 break;
1107 case 4:
1108 O << "mirror";
1109 break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001110 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001111 O << ", ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001112 }
1113 O << "filter_mode = ";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001114 switch ((sample & __CLK_FILTER_MASK) >> __CLK_FILTER_BASE) {
1115 case 0:
1116 O << "nearest";
1117 break;
1118 case 1:
1119 O << "linear";
1120 break;
1121 case 2:
Craig Topper2a30d782014-06-18 05:05:13 +00001122 llvm_unreachable("Anisotropic filtering is not supported");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001123 default:
1124 O << "nearest";
1125 break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001126 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001127 if (!((sample & __CLK_NORMALIZED_MASK) >> __CLK_NORMALIZED_BASE)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001128 O << ", force_unnormalized_coords = 1";
1129 }
1130 O << " }";
1131 }
1132
1133 O << ";\n";
1134 return;
1135 }
1136
1137 if (GVar->hasPrivateLinkage()) {
1138
1139 if (!strncmp(GVar->getName().data(), "unrollpragma", 12))
1140 return;
1141
1142 // FIXME - need better way (e.g. Metadata) to avoid generating this global
1143 if (!strncmp(GVar->getName().data(), "filename", 8))
1144 return;
1145 if (GVar->use_empty())
1146 return;
1147 }
1148
Craig Topper062a2ba2014-04-25 05:30:21 +00001149 const Function *demotedFunc = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001150 if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) {
1151 O << "// " << GVar->getName().str() << " has been demoted\n";
1152 if (localDecls.find(demotedFunc) != localDecls.end())
1153 localDecls[demotedFunc].push_back(GVar);
1154 else {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001155 std::vector<const GlobalVariable *> temp;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001156 temp.push_back(GVar);
1157 localDecls[demotedFunc] = temp;
1158 }
1159 return;
1160 }
1161
1162 O << ".";
1163 emitPTXAddressSpace(PTy->getAddressSpace(), O);
Justin Holewinski773ca402014-06-27 18:35:58 +00001164
1165 if (isManaged(*GVar)) {
1166 O << " .attribute(.managed)";
1167 }
1168
Justin Holewinskiae556d32012-05-04 20:18:50 +00001169 if (GVar->getAlignment() == 0)
1170 O << " .align " << (int) TD->getPrefTypeAlignment(ETy);
1171 else
1172 O << " .align " << GVar->getAlignment();
1173
Jingyue Wue4c9cf02014-12-17 17:59:04 +00001174 if (ETy->isFloatingPointTy() || ETy->isIntegerTy() || ETy->isPointerTy()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001175 O << " .";
Justin Holewinski700b6fa2013-05-20 12:13:28 +00001176 // Special case: ABI requires that we use .u8 for predicates
1177 if (ETy->isIntegerTy(1))
1178 O << "u8";
1179 else
1180 O << getPTXFundamentalTypeStr(ETy, false);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001181 O << " ";
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001182 O << *getSymbol(GVar);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001183
1184 // Ptx allows variable initilization only for constant and global state
1185 // spaces.
Justin Holewinski549c7732014-06-27 18:36:01 +00001186 if (GVar->hasInitializer()) {
1187 if ((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) ||
1188 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) {
1189 const Constant *Initializer = GVar->getInitializer();
1190 // 'undef' is treated as there is no value spefied.
1191 if (!Initializer->isNullValue() && !isa<UndefValue>(Initializer)) {
1192 O << " = ";
1193 printScalarConstant(Initializer, O);
1194 }
1195 } else {
1196 // The frontend adds zero-initializer to variables that don't have an
1197 // initial value, so skip warning for this case.
1198 if (!GVar->getInitializer()->isNullValue()) {
1199 std::string warnMsg = "initial value of '" + GVar->getName().str() +
1200 "' is not allowed in addrspace(" +
1201 llvm::utostr_32(PTy->getAddressSpace()) + ")";
1202 report_fatal_error(warnMsg.c_str());
1203 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001204 }
1205 }
1206 } else {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001207 unsigned int ElementSize = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001208
1209 // Although PTX has direct support for struct type and array type and
1210 // LLVM IR is very similar to PTX, the LLVM CodeGen does not support for
1211 // targets that support these high level field accesses. Structs, arrays
1212 // and vectors are lowered into arrays of bytes.
1213 switch (ETy->getTypeID()) {
1214 case Type::StructTyID:
1215 case Type::ArrayTyID:
1216 case Type::VectorTyID:
1217 ElementSize = TD->getTypeStoreSize(ETy);
1218 // Ptx allows variable initilization only for constant and
1219 // global state spaces.
1220 if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) ||
Justin Holewinski0497ab12013-03-30 14:29:21 +00001221 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) &&
1222 GVar->hasInitializer()) {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001223 const Constant *Initializer = GVar->getInitializer();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001224 if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001225 AggBuffer aggBuffer(ElementSize, O, *this);
1226 bufferAggregateConstant(Initializer, &aggBuffer);
1227 if (aggBuffer.numSymbols) {
Eric Christopher6aad8b12015-02-19 00:08:14 +00001228 if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) {
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001229 O << " .u64 " << *getSymbol(GVar) << "[";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001230 O << ElementSize / 8;
1231 } else {
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001232 O << " .u32 " << *getSymbol(GVar) << "[";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001233 O << ElementSize / 4;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001234 }
1235 O << "]";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001236 } else {
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001237 O << " .b8 " << *getSymbol(GVar) << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001238 O << ElementSize;
1239 O << "]";
1240 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001241 O << " = {";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001242 aggBuffer.print();
1243 O << "}";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001244 } else {
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001245 O << " .b8 " << *getSymbol(GVar);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001246 if (ElementSize) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001247 O << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001248 O << ElementSize;
1249 O << "]";
1250 }
1251 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001252 } else {
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001253 O << " .b8 " << *getSymbol(GVar);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001254 if (ElementSize) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001255 O << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001256 O << ElementSize;
1257 O << "]";
1258 }
1259 }
1260 break;
1261 default:
Craig Topper2a30d782014-06-18 05:05:13 +00001262 llvm_unreachable("type not supported yet");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001263 }
1264
1265 }
1266 O << ";\n";
1267}
1268
1269void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) {
1270 if (localDecls.find(f) == localDecls.end())
1271 return;
1272
Justin Holewinski01f89f02013-05-20 12:13:32 +00001273 std::vector<const GlobalVariable *> &gvars = localDecls[f];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001274
Justin Holewinski0497ab12013-03-30 14:29:21 +00001275 for (unsigned i = 0, e = gvars.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001276 O << "\t// demoted variable\n\t";
1277 printModuleLevelGV(gvars[i], O, true);
1278 }
1279}
1280
1281void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace,
1282 raw_ostream &O) const {
1283 switch (AddressSpace) {
1284 case llvm::ADDRESS_SPACE_LOCAL:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001285 O << "local";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001286 break;
1287 case llvm::ADDRESS_SPACE_GLOBAL:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001288 O << "global";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001289 break;
1290 case llvm::ADDRESS_SPACE_CONST:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001291 O << "const";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001292 break;
1293 case llvm::ADDRESS_SPACE_SHARED:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001294 O << "shared";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001295 break;
1296 default:
Justin Holewinski36a50992013-02-09 13:34:15 +00001297 report_fatal_error("Bad address space found while emitting PTX");
1298 break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001299 }
1300}
1301
Justin Holewinski0497ab12013-03-30 14:29:21 +00001302std::string
1303NVPTXAsmPrinter::getPTXFundamentalTypeStr(const Type *Ty, bool useB4PTR) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001304 switch (Ty->getTypeID()) {
1305 default:
1306 llvm_unreachable("unexpected type");
1307 break;
1308 case Type::IntegerTyID: {
1309 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth();
1310 if (NumBits == 1)
1311 return "pred";
1312 else if (NumBits <= 64) {
1313 std::string name = "u";
1314 return name + utostr(NumBits);
1315 } else {
1316 llvm_unreachable("Integer too large");
1317 break;
1318 }
1319 break;
1320 }
1321 case Type::FloatTyID:
1322 return "f32";
1323 case Type::DoubleTyID:
1324 return "f64";
1325 case Type::PointerTyID:
Eric Christopher6aad8b12015-02-19 00:08:14 +00001326 if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit())
Justin Holewinski0497ab12013-03-30 14:29:21 +00001327 if (useB4PTR)
1328 return "b64";
1329 else
1330 return "u64";
1331 else if (useB4PTR)
1332 return "b32";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001333 else
Justin Holewinski0497ab12013-03-30 14:29:21 +00001334 return "u32";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001335 }
1336 llvm_unreachable("unexpected type");
Craig Topper062a2ba2014-04-25 05:30:21 +00001337 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001338}
1339
Justin Holewinski0497ab12013-03-30 14:29:21 +00001340void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001341 raw_ostream &O) {
1342
Eric Christopher8b770652015-01-26 19:03:15 +00001343 const DataLayout *TD = TM.getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001344
1345 // GlobalVariables are always constant pointers themselves.
1346 const PointerType *PTy = GVar->getType();
1347 Type *ETy = PTy->getElementType();
1348
1349 O << ".";
1350 emitPTXAddressSpace(PTy->getAddressSpace(), O);
1351 if (GVar->getAlignment() == 0)
1352 O << " .align " << (int) TD->getPrefTypeAlignment(ETy);
1353 else
1354 O << " .align " << GVar->getAlignment();
1355
Jingyue Wue4c9cf02014-12-17 17:59:04 +00001356 if (ETy->isFloatingPointTy() || ETy->isIntegerTy() || ETy->isPointerTy()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001357 O << " .";
1358 O << getPTXFundamentalTypeStr(ETy);
1359 O << " ";
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001360 O << *getSymbol(GVar);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001361 return;
1362 }
1363
Justin Holewinski0497ab12013-03-30 14:29:21 +00001364 int64_t ElementSize = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001365
1366 // Although PTX has direct support for struct type and array type and LLVM IR
1367 // is very similar to PTX, the LLVM CodeGen does not support for targets that
1368 // support these high level field accesses. Structs and arrays are lowered
1369 // into arrays of bytes.
1370 switch (ETy->getTypeID()) {
1371 case Type::StructTyID:
1372 case Type::ArrayTyID:
1373 case Type::VectorTyID:
1374 ElementSize = TD->getTypeStoreSize(ETy);
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00001375 O << " .b8 " << *getSymbol(GVar) << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001376 if (ElementSize) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001377 O << itostr(ElementSize);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001378 }
1379 O << "]";
1380 break;
1381 default:
Craig Topper2a30d782014-06-18 05:05:13 +00001382 llvm_unreachable("type not supported yet");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001383 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001384 return;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001385}
1386
Justin Holewinski0497ab12013-03-30 14:29:21 +00001387static unsigned int getOpenCLAlignment(const DataLayout *TD, Type *Ty) {
Rafael Espindola08013342013-12-07 19:34:20 +00001388 if (Ty->isSingleValueType())
Justin Holewinskiae556d32012-05-04 20:18:50 +00001389 return TD->getPrefTypeAlignment(Ty);
1390
1391 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1392 if (ATy)
1393 return getOpenCLAlignment(TD, ATy->getElementType());
1394
Justin Holewinskiae556d32012-05-04 20:18:50 +00001395 const StructType *STy = dyn_cast<StructType>(Ty);
1396 if (STy) {
1397 unsigned int alignStruct = 1;
1398 // Go through each element of the struct and find the
1399 // largest alignment.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001400 for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001401 Type *ETy = STy->getElementType(i);
1402 unsigned int align = getOpenCLAlignment(TD, ETy);
1403 if (align > alignStruct)
1404 alignStruct = align;
1405 }
1406 return alignStruct;
1407 }
1408
1409 const FunctionType *FTy = dyn_cast<FunctionType>(Ty);
1410 if (FTy)
Chandler Carruth5da3f052012-11-01 09:14:31 +00001411 return TD->getPointerPrefAlignment();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001412 return TD->getPrefTypeAlignment(Ty);
1413}
1414
1415void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I,
1416 int paramIndex, raw_ostream &O) {
Eric Christopherbeffc4e2015-02-19 00:08:23 +00001417 O << *getSymbol(I->getParent()) << "_param_" << paramIndex;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001418}
1419
1420void NVPTXAsmPrinter::printParamName(int paramIndex, raw_ostream &O) {
Eric Christopherbeffc4e2015-02-19 00:08:23 +00001421 O << *CurrentFnSym << "_param_" << paramIndex;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001422}
1423
Justin Holewinski0497ab12013-03-30 14:29:21 +00001424void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
Eric Christopher8b770652015-01-26 19:03:15 +00001425 const DataLayout *TD = TM.getDataLayout();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001426 const AttributeSet &PAL = F->getAttributes();
Eric Christopher6aad8b12015-02-19 00:08:14 +00001427 const TargetLowering *TLI = nvptxSubtarget->getTargetLowering();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001428 Function::const_arg_iterator I, E;
1429 unsigned paramIndex = 0;
1430 bool first = true;
1431 bool isKernelFunc = llvm::isKernelFunction(*F);
Eric Christopher6aad8b12015-02-19 00:08:14 +00001432 bool isABI = (nvptxSubtarget->getSmVersion() >= 20);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001433 MVT thePointerTy = TLI->getPointerTy();
1434
1435 O << "(\n";
1436
1437 for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, paramIndex++) {
Justin Holewinskie9884092013-03-24 21:17:47 +00001438 Type *Ty = I->getType();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001439
1440 if (!first)
1441 O << ",\n";
1442
1443 first = false;
1444
1445 // Handle image/sampler parameters
Justin Holewinski30d56a72014-04-09 15:39:15 +00001446 if (isKernelFunction(*F)) {
1447 if (isSampler(*I) || isImage(*I)) {
1448 if (isImage(*I)) {
1449 std::string sname = I->getName();
1450 if (isImageWriteOnly(*I) || isImageReadWrite(*I)) {
Eric Christopher6aad8b12015-02-19 00:08:14 +00001451 if (nvptxSubtarget->hasImageHandles())
Justin Holewinski30d56a72014-04-09 15:39:15 +00001452 O << "\t.param .u64 .ptr .surfref ";
1453 else
1454 O << "\t.param .surfref ";
1455 O << *CurrentFnSym << "_param_" << paramIndex;
1456 }
1457 else { // Default image is read_only
Eric Christopher6aad8b12015-02-19 00:08:14 +00001458 if (nvptxSubtarget->hasImageHandles())
Justin Holewinski30d56a72014-04-09 15:39:15 +00001459 O << "\t.param .u64 .ptr .texref ";
1460 else
1461 O << "\t.param .texref ";
1462 O << *CurrentFnSym << "_param_" << paramIndex;
1463 }
1464 } else {
Eric Christopher6aad8b12015-02-19 00:08:14 +00001465 if (nvptxSubtarget->hasImageHandles())
Justin Holewinski30d56a72014-04-09 15:39:15 +00001466 O << "\t.param .u64 .ptr .samplerref ";
1467 else
1468 O << "\t.param .samplerref ";
1469 O << *CurrentFnSym << "_param_" << paramIndex;
1470 }
1471 continue;
1472 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001473 }
1474
Justin Holewinski0497ab12013-03-30 14:29:21 +00001475 if (PAL.hasAttribute(paramIndex + 1, Attribute::ByVal) == false) {
Gautam Chakrabarti2c283402014-01-28 18:35:29 +00001476 if (Ty->isAggregateType() || Ty->isVectorTy()) {
1477 // Just print .param .align <a> .b8 .param[size];
Justin Holewinskie9884092013-03-24 21:17:47 +00001478 // <a> = PAL.getparamalignment
1479 // size = typeallocsize of element type
Justin Holewinski0497ab12013-03-30 14:29:21 +00001480 unsigned align = PAL.getParamAlignment(paramIndex + 1);
Justin Holewinskie9884092013-03-24 21:17:47 +00001481 if (align == 0)
1482 align = TD->getABITypeAlignment(Ty);
1483
1484 unsigned sz = TD->getTypeAllocSize(Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001485 O << "\t.param .align " << align << " .b8 ";
Justin Holewinskie9884092013-03-24 21:17:47 +00001486 printParamName(I, paramIndex, O);
1487 O << "[" << sz << "]";
1488
1489 continue;
1490 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001491 // Just a scalar
1492 const PointerType *PTy = dyn_cast<PointerType>(Ty);
1493 if (isKernelFunc) {
1494 if (PTy) {
1495 // Special handling for pointer arguments to kernel
1496 O << "\t.param .u" << thePointerTy.getSizeInBits() << " ";
1497
Eric Christopherbeffc4e2015-02-19 00:08:23 +00001498 if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() !=
1499 NVPTX::CUDA) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001500 Type *ETy = PTy->getElementType();
1501 int addrSpace = PTy->getAddressSpace();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001502 switch (addrSpace) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001503 default:
1504 O << ".ptr ";
1505 break;
Justin Holewinskib96d1392013-06-10 13:29:47 +00001506 case llvm::ADDRESS_SPACE_CONST:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001507 O << ".ptr .const ";
1508 break;
1509 case llvm::ADDRESS_SPACE_SHARED:
1510 O << ".ptr .shared ";
1511 break;
1512 case llvm::ADDRESS_SPACE_GLOBAL:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001513 O << ".ptr .global ";
1514 break;
1515 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001516 O << ".align " << (int) getOpenCLAlignment(TD, ETy) << " ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001517 }
1518 printParamName(I, paramIndex, O);
1519 continue;
1520 }
1521
1522 // non-pointer scalar to kernel func
Justin Holewinski700b6fa2013-05-20 12:13:28 +00001523 O << "\t.param .";
1524 // Special case: predicate operands become .u8 types
1525 if (Ty->isIntegerTy(1))
1526 O << "u8";
1527 else
1528 O << getPTXFundamentalTypeStr(Ty);
1529 O << " ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001530 printParamName(I, paramIndex, O);
1531 continue;
1532 }
1533 // Non-kernel function, just print .param .b<size> for ABI
Alp Tokerf907b892013-12-05 05:44:44 +00001534 // and .reg .b<size> for non-ABI
Justin Holewinskiae556d32012-05-04 20:18:50 +00001535 unsigned sz = 0;
1536 if (isa<IntegerType>(Ty)) {
1537 sz = cast<IntegerType>(Ty)->getBitWidth();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001538 if (sz < 32)
1539 sz = 32;
1540 } else if (isa<PointerType>(Ty))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001541 sz = thePointerTy.getSizeInBits();
1542 else
1543 sz = Ty->getPrimitiveSizeInBits();
1544 if (isABI)
1545 O << "\t.param .b" << sz << " ";
1546 else
1547 O << "\t.reg .b" << sz << " ";
1548 printParamName(I, paramIndex, O);
1549 continue;
1550 }
1551
1552 // param has byVal attribute. So should be a pointer
1553 const PointerType *PTy = dyn_cast<PointerType>(Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001554 assert(PTy && "Param with byval attribute should be a pointer type");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001555 Type *ETy = PTy->getElementType();
1556
1557 if (isABI || isKernelFunc) {
Gautam Chakrabarti2c283402014-01-28 18:35:29 +00001558 // Just print .param .align <a> .b8 .param[size];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001559 // <a> = PAL.getparamalignment
1560 // size = typeallocsize of element type
Justin Holewinski0497ab12013-03-30 14:29:21 +00001561 unsigned align = PAL.getParamAlignment(paramIndex + 1);
Justin Holewinski2dc9d072012-11-09 23:50:24 +00001562 if (align == 0)
1563 align = TD->getABITypeAlignment(ETy);
1564
Justin Holewinskiae556d32012-05-04 20:18:50 +00001565 unsigned sz = TD->getTypeAllocSize(ETy);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001566 O << "\t.param .align " << align << " .b8 ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001567 printParamName(I, paramIndex, O);
1568 O << "[" << sz << "]";
1569 continue;
1570 } else {
1571 // Split the ETy into constituent parts and
1572 // print .param .b<size> <name> for each part.
1573 // Further, if a part is vector, print the above for
1574 // each vector element.
1575 SmallVector<EVT, 16> vtparts;
1576 ComputeValueVTs(*TLI, ETy, vtparts);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001577 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001578 unsigned elems = 1;
1579 EVT elemtype = vtparts[i];
1580 if (vtparts[i].isVector()) {
1581 elems = vtparts[i].getVectorNumElements();
1582 elemtype = vtparts[i].getVectorElementType();
1583 }
1584
Justin Holewinski0497ab12013-03-30 14:29:21 +00001585 for (unsigned j = 0, je = elems; j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001586 unsigned sz = elemtype.getSizeInBits();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001587 if (elemtype.isInteger() && (sz < 32))
1588 sz = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001589 O << "\t.reg .b" << sz << " ";
1590 printParamName(I, paramIndex, O);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001591 if (j < je - 1)
1592 O << ",\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001593 ++paramIndex;
1594 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001595 if (i < e - 1)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001596 O << ",\n";
1597 }
1598 --paramIndex;
1599 continue;
1600 }
1601 }
1602
1603 O << "\n)\n";
1604}
1605
1606void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF,
1607 raw_ostream &O) {
1608 const Function *F = MF.getFunction();
1609 emitFunctionParamList(F, O);
1610}
1611
Justin Holewinski0497ab12013-03-30 14:29:21 +00001612void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters(
1613 const MachineFunction &MF) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001614 SmallString<128> Str;
1615 raw_svector_ostream O(Str);
1616
1617 // Map the global virtual register number to a register class specific
1618 // virtual register number starting from 1 with that class.
Eric Christopherfc6de422014-08-05 02:39:49 +00001619 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001620 //unsigned numRegClasses = TRI->getNumRegClasses();
1621
1622 // Emit the Fake Stack Object
1623 const MachineFrameInfo *MFI = MF.getFrameInfo();
1624 int NumBytes = (int) MFI->getStackSize();
1625 if (NumBytes) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001626 O << "\t.local .align " << MFI->getMaxAlignment() << " .b8 \t" << DEPOTNAME
1627 << getFunctionNumber() << "[" << NumBytes << "];\n";
Eric Christopher02389e32015-02-19 00:08:27 +00001628 if (static_cast<const NVPTXTargetMachine &>(MF.getTarget()).is64Bit()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001629 O << "\t.reg .b64 \t%SP;\n";
1630 O << "\t.reg .b64 \t%SPL;\n";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001631 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001632 O << "\t.reg .b32 \t%SP;\n";
1633 O << "\t.reg .b32 \t%SPL;\n";
1634 }
1635 }
1636
1637 // Go through all virtual registers to establish the mapping between the
1638 // global virtual
1639 // register number and the per class virtual register number.
1640 // We use the per class virtual register number in the ptx output.
1641 unsigned int numVRs = MRI->getNumVirtRegs();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001642 for (unsigned i = 0; i < numVRs; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001643 unsigned int vr = TRI->index2VirtReg(i);
1644 const TargetRegisterClass *RC = MRI->getRegClass(vr);
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001645 DenseMap<unsigned, unsigned> &regmap = VRegMapping[RC];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001646 int n = regmap.size();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001647 regmap.insert(std::make_pair(vr, n + 1));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001648 }
1649
1650 // Emit register declarations
1651 // @TODO: Extract out the real register usage
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001652 // O << "\t.reg .pred %p<" << NVPTXNumRegisters << ">;\n";
1653 // O << "\t.reg .s16 %rc<" << NVPTXNumRegisters << ">;\n";
1654 // O << "\t.reg .s16 %rs<" << NVPTXNumRegisters << ">;\n";
1655 // O << "\t.reg .s32 %r<" << NVPTXNumRegisters << ">;\n";
Justin Holewinski3e037d92014-07-16 16:26:58 +00001656 // O << "\t.reg .s64 %rd<" << NVPTXNumRegisters << ">;\n";
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001657 // O << "\t.reg .f32 %f<" << NVPTXNumRegisters << ">;\n";
Justin Holewinski3e037d92014-07-16 16:26:58 +00001658 // O << "\t.reg .f64 %fd<" << NVPTXNumRegisters << ">;\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001659
1660 // Emit declaration of the virtual registers or 'physical' registers for
1661 // each register class
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001662 for (unsigned i=0; i< TRI->getNumRegClasses(); i++) {
1663 const TargetRegisterClass *RC = TRI->getRegClass(i);
1664 DenseMap<unsigned, unsigned> &regmap = VRegMapping[RC];
1665 std::string rcname = getNVPTXRegClassName(RC);
1666 std::string rcStr = getNVPTXRegClassStr(RC);
1667 int n = regmap.size();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001668
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001669 // Only declare those registers that may be used.
1670 if (n) {
1671 O << "\t.reg " << rcname << " \t" << rcStr << "<" << (n+1)
1672 << ">;\n";
1673 }
1674 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001675
1676 OutStreamer.EmitRawText(O.str());
1677}
1678
Justin Holewinskiae556d32012-05-04 20:18:50 +00001679void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001680 APFloat APF = APFloat(Fp->getValueAPF()); // make a copy
Justin Holewinskiae556d32012-05-04 20:18:50 +00001681 bool ignored;
1682 unsigned int numHex;
1683 const char *lead;
1684
Justin Holewinski0497ab12013-03-30 14:29:21 +00001685 if (Fp->getType()->getTypeID() == Type::FloatTyID) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001686 numHex = 8;
1687 lead = "0f";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001688 APF.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &ignored);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001689 } else if (Fp->getType()->getTypeID() == Type::DoubleTyID) {
1690 numHex = 16;
1691 lead = "0d";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001692 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001693 } else
1694 llvm_unreachable("unsupported fp type");
1695
1696 APInt API = APF.bitcastToAPInt();
1697 std::string hexstr(utohexstr(API.getZExtValue()));
1698 O << lead;
1699 if (hexstr.length() < numHex)
1700 O << std::string(numHex - hexstr.length(), '0');
1701 O << utohexstr(API.getZExtValue());
1702}
1703
Justin Holewinski01f89f02013-05-20 12:13:32 +00001704void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
1705 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001706 O << CI->getValue();
1707 return;
1708 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001709 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001710 printFPConstant(CFP, O);
1711 return;
1712 }
1713 if (isa<ConstantPointerNull>(CPV)) {
1714 O << "0";
1715 return;
1716 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001717 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
Justin Holewinski9d852a82014-04-09 15:39:11 +00001718 PointerType *PTy = dyn_cast<PointerType>(GVar->getType());
1719 bool IsNonGenericPointer = false;
1720 if (PTy && PTy->getAddressSpace() != 0) {
1721 IsNonGenericPointer = true;
1722 }
1723 if (EmitGeneric && !isa<Function>(CPV) && !IsNonGenericPointer) {
1724 O << "generic(";
1725 O << *getSymbol(GVar);
1726 O << ")";
1727 } else {
1728 O << *getSymbol(GVar);
1729 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001730 return;
1731 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001732 if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1733 const Value *v = Cexpr->stripPointerCasts();
Justin Holewinski9d852a82014-04-09 15:39:11 +00001734 PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType());
1735 bool IsNonGenericPointer = false;
1736 if (PTy && PTy->getAddressSpace() != 0) {
1737 IsNonGenericPointer = true;
1738 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001739 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
Justin Holewinski9d852a82014-04-09 15:39:11 +00001740 if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
1741 O << "generic(";
1742 O << *getSymbol(GVar);
1743 O << ")";
1744 } else {
1745 O << *getSymbol(GVar);
1746 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001747 return;
1748 } else {
Matt Arsenault31a52ad2014-12-16 19:16:17 +00001749 O << *lowerConstant(CPV);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001750 return;
1751 }
1752 }
1753 llvm_unreachable("Not scalar type found in printScalarConstant()");
1754}
1755
Justin Holewinski01f89f02013-05-20 12:13:32 +00001756void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001757 AggBuffer *aggBuffer) {
1758
Eric Christopher8b770652015-01-26 19:03:15 +00001759 const DataLayout *TD = TM.getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001760
1761 if (isa<UndefValue>(CPV) || CPV->isNullValue()) {
1762 int s = TD->getTypeAllocSize(CPV->getType());
Justin Holewinski0497ab12013-03-30 14:29:21 +00001763 if (s < Bytes)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001764 s = Bytes;
1765 aggBuffer->addZeros(s);
1766 return;
1767 }
1768
1769 unsigned char *ptr;
1770 switch (CPV->getType()->getTypeID()) {
1771
1772 case Type::IntegerTyID: {
1773 const Type *ETy = CPV->getType();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001774 if (ETy == Type::getInt8Ty(CPV->getContext())) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001775 unsigned char c =
1776 (unsigned char)(dyn_cast<ConstantInt>(CPV))->getZExtValue();
1777 ptr = &c;
1778 aggBuffer->addBytes(ptr, 1, Bytes);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001779 } else if (ETy == Type::getInt16Ty(CPV->getContext())) {
1780 short int16 = (short)(dyn_cast<ConstantInt>(CPV))->getZExtValue();
1781 ptr = (unsigned char *)&int16;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001782 aggBuffer->addBytes(ptr, 2, Bytes);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001783 } else if (ETy == Type::getInt32Ty(CPV->getContext())) {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001784 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001785 int int32 = (int)(constInt->getZExtValue());
1786 ptr = (unsigned char *)&int32;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001787 aggBuffer->addBytes(ptr, 4, Bytes);
1788 break;
Justin Holewinski01f89f02013-05-20 12:13:32 +00001789 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1790 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001791 ConstantFoldConstantExpression(Cexpr, *TD))) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001792 int int32 = (int)(constInt->getZExtValue());
1793 ptr = (unsigned char *)&int32;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001794 aggBuffer->addBytes(ptr, 4, Bytes);
1795 break;
1796 }
1797 if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1798 Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1799 aggBuffer->addSymbol(v);
1800 aggBuffer->addZeros(4);
1801 break;
1802 }
1803 }
Craig Topperbdf39a42012-05-24 07:02:50 +00001804 llvm_unreachable("unsupported integer const type");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001805 } else if (ETy == Type::getInt64Ty(CPV->getContext())) {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001806 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001807 long long int64 = (long long)(constInt->getZExtValue());
1808 ptr = (unsigned char *)&int64;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001809 aggBuffer->addBytes(ptr, 8, Bytes);
1810 break;
Justin Holewinski01f89f02013-05-20 12:13:32 +00001811 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1812 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001813 ConstantFoldConstantExpression(Cexpr, *TD))) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001814 long long int64 = (long long)(constInt->getZExtValue());
1815 ptr = (unsigned char *)&int64;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001816 aggBuffer->addBytes(ptr, 8, Bytes);
1817 break;
1818 }
1819 if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1820 Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1821 aggBuffer->addSymbol(v);
1822 aggBuffer->addZeros(8);
1823 break;
1824 }
1825 }
1826 llvm_unreachable("unsupported integer const type");
Craig Topperbdf39a42012-05-24 07:02:50 +00001827 } else
Justin Holewinskiae556d32012-05-04 20:18:50 +00001828 llvm_unreachable("unsupported integer const type");
1829 break;
1830 }
1831 case Type::FloatTyID:
1832 case Type::DoubleTyID: {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001833 const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001834 const Type *Ty = CFP->getType();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001835 if (Ty == Type::getFloatTy(CPV->getContext())) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001836 float float32 = (float) CFP->getValueAPF().convertToFloat();
1837 ptr = (unsigned char *)&float32;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001838 aggBuffer->addBytes(ptr, 4, Bytes);
1839 } else if (Ty == Type::getDoubleTy(CPV->getContext())) {
1840 double float64 = CFP->getValueAPF().convertToDouble();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001841 ptr = (unsigned char *)&float64;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001842 aggBuffer->addBytes(ptr, 8, Bytes);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001843 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001844 llvm_unreachable("unsupported fp const type");
1845 }
1846 break;
1847 }
1848 case Type::PointerTyID: {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001849 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001850 aggBuffer->addSymbol(GVar);
Justin Holewinski01f89f02013-05-20 12:13:32 +00001851 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1852 const Value *v = Cexpr->stripPointerCasts();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001853 aggBuffer->addSymbol(v);
1854 }
1855 unsigned int s = TD->getTypeAllocSize(CPV->getType());
1856 aggBuffer->addZeros(s);
1857 break;
1858 }
1859
1860 case Type::ArrayTyID:
1861 case Type::VectorTyID:
1862 case Type::StructTyID: {
1863 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV) ||
Justin Holewinski95564bd2013-09-19 12:51:46 +00001864 isa<ConstantStruct>(CPV) || isa<ConstantDataSequential>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001865 int ElementSize = TD->getTypeAllocSize(CPV->getType());
1866 bufferAggregateConstant(CPV, aggBuffer);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001867 if (Bytes > ElementSize)
1868 aggBuffer->addZeros(Bytes - ElementSize);
1869 } else if (isa<ConstantAggregateZero>(CPV))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001870 aggBuffer->addZeros(Bytes);
1871 else
1872 llvm_unreachable("Unexpected Constant type");
1873 break;
1874 }
1875
1876 default:
1877 llvm_unreachable("unsupported type");
1878 }
1879}
1880
Justin Holewinski01f89f02013-05-20 12:13:32 +00001881void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001882 AggBuffer *aggBuffer) {
Eric Christopher8b770652015-01-26 19:03:15 +00001883 const DataLayout *TD = TM.getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001884 int Bytes;
1885
1886 // Old constants
1887 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) {
1888 if (CPV->getNumOperands())
1889 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i)
1890 bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer);
1891 return;
1892 }
1893
1894 if (const ConstantDataSequential *CDS =
Justin Holewinski0497ab12013-03-30 14:29:21 +00001895 dyn_cast<ConstantDataSequential>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001896 if (CDS->getNumElements())
1897 for (unsigned i = 0; i < CDS->getNumElements(); ++i)
1898 bufferLEByte(cast<Constant>(CDS->getElementAsConstant(i)), 0,
1899 aggBuffer);
1900 return;
1901 }
1902
Justin Holewinskiae556d32012-05-04 20:18:50 +00001903 if (isa<ConstantStruct>(CPV)) {
1904 if (CPV->getNumOperands()) {
1905 StructType *ST = cast<StructType>(CPV->getType());
1906 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001907 if (i == (e - 1))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001908 Bytes = TD->getStructLayout(ST)->getElementOffset(0) +
Justin Holewinski0497ab12013-03-30 14:29:21 +00001909 TD->getTypeAllocSize(ST) -
1910 TD->getStructLayout(ST)->getElementOffset(i);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001911 else
Justin Holewinski0497ab12013-03-30 14:29:21 +00001912 Bytes = TD->getStructLayout(ST)->getElementOffset(i + 1) -
1913 TD->getStructLayout(ST)->getElementOffset(i);
1914 bufferLEByte(cast<Constant>(CPV->getOperand(i)), Bytes, aggBuffer);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001915 }
1916 }
1917 return;
1918 }
Craig Topperbdf39a42012-05-24 07:02:50 +00001919 llvm_unreachable("unsupported constant type in printAggregateConstant()");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001920}
1921
1922// buildTypeNameMap - Run through symbol table looking for type names.
1923//
1924
Justin Holewinskiae556d32012-05-04 20:18:50 +00001925bool NVPTXAsmPrinter::isImageType(const Type *Ty) {
1926
1927 std::map<const Type *, std::string>::iterator PI = TypeNameMap.find(Ty);
1928
Justin Holewinski0497ab12013-03-30 14:29:21 +00001929 if (PI != TypeNameMap.end() && (!PI->second.compare("struct._image1d_t") ||
1930 !PI->second.compare("struct._image2d_t") ||
1931 !PI->second.compare("struct._image3d_t")))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001932 return true;
1933
1934 return false;
1935}
1936
Justin Holewinskiae556d32012-05-04 20:18:50 +00001937
Justin Holewinski0497ab12013-03-30 14:29:21 +00001938bool NVPTXAsmPrinter::ignoreLoc(const MachineInstr &MI) {
1939 switch (MI.getOpcode()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001940 default:
1941 return false;
Justin Holewinski0497ab12013-03-30 14:29:21 +00001942 case NVPTX::CallArgBeginInst:
1943 case NVPTX::CallArgEndInst0:
1944 case NVPTX::CallArgEndInst1:
1945 case NVPTX::CallArgF32:
1946 case NVPTX::CallArgF64:
1947 case NVPTX::CallArgI16:
1948 case NVPTX::CallArgI32:
1949 case NVPTX::CallArgI32imm:
1950 case NVPTX::CallArgI64:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001951 case NVPTX::CallArgParam:
1952 case NVPTX::CallVoidInst:
1953 case NVPTX::CallVoidInstReg:
1954 case NVPTX::Callseq_End:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001955 case NVPTX::CallVoidInstReg64:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001956 case NVPTX::DeclareParamInst:
1957 case NVPTX::DeclareRetMemInst:
1958 case NVPTX::DeclareRetRegInst:
1959 case NVPTX::DeclareRetScalarInst:
1960 case NVPTX::DeclareScalarParamInst:
1961 case NVPTX::DeclareScalarRegInst:
1962 case NVPTX::StoreParamF32:
1963 case NVPTX::StoreParamF64:
1964 case NVPTX::StoreParamI16:
1965 case NVPTX::StoreParamI32:
1966 case NVPTX::StoreParamI64:
1967 case NVPTX::StoreParamI8:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001968 case NVPTX::StoreRetvalF32:
1969 case NVPTX::StoreRetvalF64:
1970 case NVPTX::StoreRetvalI16:
1971 case NVPTX::StoreRetvalI32:
1972 case NVPTX::StoreRetvalI64:
1973 case NVPTX::StoreRetvalI8:
1974 case NVPTX::LastCallArgF32:
1975 case NVPTX::LastCallArgF64:
1976 case NVPTX::LastCallArgI16:
1977 case NVPTX::LastCallArgI32:
1978 case NVPTX::LastCallArgI32imm:
1979 case NVPTX::LastCallArgI64:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001980 case NVPTX::LastCallArgParam:
1981 case NVPTX::LoadParamMemF32:
1982 case NVPTX::LoadParamMemF64:
1983 case NVPTX::LoadParamMemI16:
1984 case NVPTX::LoadParamMemI32:
1985 case NVPTX::LoadParamMemI64:
1986 case NVPTX::LoadParamMemI8:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001987 case NVPTX::PrototypeInst:
1988 case NVPTX::DBG_VALUE:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001989 return true;
1990 }
1991 return false;
1992}
1993
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00001994/// PrintAsmOperand - Print out an operand for an inline asm expression.
1995///
1996bool NVPTXAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1997 unsigned AsmVariant,
1998 const char *ExtraCode, raw_ostream &O) {
1999 if (ExtraCode && ExtraCode[0]) {
2000 if (ExtraCode[1] != 0)
2001 return true; // Unknown modifier.
2002
2003 switch (ExtraCode[0]) {
2004 default:
2005 // See if this is a generic print operand
2006 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
2007 case 'r':
2008 break;
2009 }
2010 }
2011
2012 printOperand(MI, OpNo, O);
2013
2014 return false;
2015}
2016
2017bool NVPTXAsmPrinter::PrintAsmMemoryOperand(
2018 const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant,
2019 const char *ExtraCode, raw_ostream &O) {
2020 if (ExtraCode && ExtraCode[0])
2021 return true; // Unknown modifier
2022
2023 O << '[';
2024 printMemOperand(MI, OpNo, O);
2025 O << ']';
2026
2027 return false;
2028}
2029
2030void NVPTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
2031 raw_ostream &O, const char *Modifier) {
2032 const MachineOperand &MO = MI->getOperand(opNum);
2033 switch (MO.getType()) {
2034 case MachineOperand::MO_Register:
2035 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
2036 if (MO.getReg() == NVPTX::VRDepot)
2037 O << DEPOTNAME << getFunctionNumber();
2038 else
2039 O << NVPTXInstPrinter::getRegisterName(MO.getReg());
2040 } else {
Justin Holewinski660597d2013-10-11 12:39:36 +00002041 emitVirtualRegister(MO.getReg(), O);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002042 }
2043 return;
2044
2045 case MachineOperand::MO_Immediate:
2046 if (!Modifier)
2047 O << MO.getImm();
2048 else if (strstr(Modifier, "vec") == Modifier)
2049 printVecModifiedImmediate(MO, Modifier, O);
2050 else
2051 llvm_unreachable(
2052 "Don't know how to handle modifier on immediate operand");
2053 return;
2054
2055 case MachineOperand::MO_FPImmediate:
2056 printFPConstant(MO.getFPImm(), O);
2057 break;
2058
2059 case MachineOperand::MO_GlobalAddress:
Eli Bendersky6a0ccfb2014-03-31 16:11:57 +00002060 O << *getSymbol(MO.getGlobal());
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002061 break;
2062
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002063 case MachineOperand::MO_MachineBasicBlock:
2064 O << *MO.getMBB()->getSymbol();
2065 return;
2066
2067 default:
2068 llvm_unreachable("Operand type not supported.");
2069 }
2070}
2071
2072void NVPTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
2073 raw_ostream &O, const char *Modifier) {
2074 printOperand(MI, opNum, O);
2075
2076 if (Modifier && !strcmp(Modifier, "add")) {
2077 O << ", ";
2078 printOperand(MI, opNum + 1, O);
2079 } else {
2080 if (MI->getOperand(opNum + 1).isImm() &&
2081 MI->getOperand(opNum + 1).getImm() == 0)
2082 return; // don't print ',0' or '+0'
2083 O << "+";
2084 printOperand(MI, opNum + 1, O);
2085 }
2086}
2087
Justin Holewinskiae556d32012-05-04 20:18:50 +00002088void NVPTXAsmPrinter::emitSrcInText(StringRef filename, unsigned line) {
2089 std::stringstream temp;
Justin Holewinski0497ab12013-03-30 14:29:21 +00002090 LineReader *reader = this->getReader(filename.str());
Justin Holewinskiae556d32012-05-04 20:18:50 +00002091 temp << "\n//";
2092 temp << filename.str();
2093 temp << ":";
2094 temp << line;
2095 temp << " ";
2096 temp << reader->readLine(line);
2097 temp << "\n";
2098 this->OutStreamer.EmitRawText(Twine(temp.str()));
2099}
2100
Justin Holewinskiae556d32012-05-04 20:18:50 +00002101LineReader *NVPTXAsmPrinter::getReader(std::string filename) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002102 if (!reader) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002103 reader = new LineReader(filename);
Justin Holewinskiae556d32012-05-04 20:18:50 +00002104 }
2105
2106 if (reader->fileName() != filename) {
2107 delete reader;
Justin Holewinski0497ab12013-03-30 14:29:21 +00002108 reader = new LineReader(filename);
Justin Holewinskiae556d32012-05-04 20:18:50 +00002109 }
2110
2111 return reader;
2112}
2113
Justin Holewinski0497ab12013-03-30 14:29:21 +00002114std::string LineReader::readLine(unsigned lineNum) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00002115 if (lineNum < theCurLine) {
2116 theCurLine = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +00002117 fstr.seekg(0, std::ios::beg);
Justin Holewinskiae556d32012-05-04 20:18:50 +00002118 }
2119 while (theCurLine < lineNum) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002120 fstr.getline(buff, 500);
Justin Holewinskiae556d32012-05-04 20:18:50 +00002121 theCurLine++;
2122 }
2123 return buff;
2124}
2125
2126// Force static initialization.
2127extern "C" void LLVMInitializeNVPTXAsmPrinter() {
2128 RegisterAsmPrinter<NVPTXAsmPrinter> X(TheNVPTXTarget32);
2129 RegisterAsmPrinter<NVPTXAsmPrinter> Y(TheNVPTXTarget64);
2130}