blob: 6284ad8b82e81698ac90b7966f7a659f9ce5c5f4 [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "NVPTXAsmPrinter.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "InstPrinter/NVPTXInstPrinter.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000017#include "MCTargetDesc/NVPTXBaseInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "MCTargetDesc/NVPTXMCAsmInfo.h"
Alexey Bataev64ad0ad2018-12-06 16:02:09 +000019#include "MCTargetDesc/NVPTXTargetStreamer.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000020#include "NVPTX.h"
Justin Holewinskia2a63d22013-08-06 14:13:27 +000021#include "NVPTXMCExpr.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "NVPTXMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "NVPTXRegisterInfo.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000024#include "NVPTXSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "NVPTXTargetMachine.h"
26#include "NVPTXUtilities.h"
27#include "cl_common_defines.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000028#include "llvm/ADT/APFloat.h"
29#include "llvm/ADT/APInt.h"
30#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/DenseSet.h"
32#include "llvm/ADT/SmallString.h"
33#include "llvm/ADT/SmallVector.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000034#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000035#include "llvm/ADT/StringRef.h"
36#include "llvm/ADT/Triple.h"
37#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Analysis/ConstantFolding.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000039#include "llvm/CodeGen/Analysis.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000040#include "llvm/CodeGen/MachineBasicBlock.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000041#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000042#include "llvm/CodeGen/MachineFunction.h"
43#include "llvm/CodeGen/MachineInstr.h"
Jingyue Wu0220df02015-02-01 02:27:45 +000044#include "llvm/CodeGen/MachineLoopInfo.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000045#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000046#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000048#include "llvm/CodeGen/TargetLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000049#include "llvm/CodeGen/TargetRegisterInfo.h"
Craig Topper2fa14362018-03-29 17:21:10 +000050#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000051#include "llvm/IR/Attributes.h"
52#include "llvm/IR/BasicBlock.h"
53#include "llvm/IR/Constant.h"
54#include "llvm/IR/Constants.h"
55#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000056#include "llvm/IR/DebugInfo.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000057#include "llvm/IR/DebugInfoMetadata.h"
58#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000059#include "llvm/IR/DerivedTypes.h"
60#include "llvm/IR/Function.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000061#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000062#include "llvm/IR/GlobalVariable.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000063#include "llvm/IR/Instruction.h"
64#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000065#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000067#include "llvm/IR/Type.h"
68#include "llvm/IR/User.h"
69#include "llvm/MC/MCExpr.h"
Pete Cooper81902a32015-05-15 22:19:42 +000070#include "llvm/MC/MCInst.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000071#include "llvm/MC/MCInstrDesc.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000072#include "llvm/MC/MCStreamer.h"
73#include "llvm/MC/MCSymbol.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000074#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000075#include "llvm/Support/CommandLine.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000076#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000077#include "llvm/Support/MachineValueType.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000078#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000079#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000080#include "llvm/Support/raw_ostream.h"
David Blaikie6054e652018-03-23 23:58:19 +000081#include "llvm/Target/TargetLoweringObjectFile.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000082#include "llvm/Target/TargetMachine.h"
Jingyue Wu0220df02015-02-01 02:27:45 +000083#include "llvm/Transforms/Utils/UnrollLoop.h"
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000084#include <cassert>
85#include <cstdint>
86#include <cstring>
87#include <new>
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000088#include <string>
89#include <utility>
90#include <vector>
91
Justin Holewinskiae556d32012-05-04 20:18:50 +000092using namespace llvm;
93
Justin Holewinskiae556d32012-05-04 20:18:50 +000094#define DEPOTNAME "__local_depot"
95
Justin Holewinski2c5ac702012-11-16 21:03:51 +000096/// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V
97/// depends.
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +000098static void
99DiscoverDependentGlobals(const Value *V,
100 DenseSet<const GlobalVariable *> &Globals) {
Justin Holewinski01f89f02013-05-20 12:13:32 +0000101 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000102 Globals.insert(GV);
103 else {
Justin Holewinski01f89f02013-05-20 12:13:32 +0000104 if (const User *U = dyn_cast<User>(V)) {
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000105 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) {
106 DiscoverDependentGlobals(U->getOperand(i), Globals);
107 }
108 }
109 }
110}
Justin Holewinskiae556d32012-05-04 20:18:50 +0000111
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000112/// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable
113/// instances to be emitted, but only after any dependents have been added
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000114/// first.s
115static void
116VisitGlobalVariableForEmission(const GlobalVariable *GV,
117 SmallVectorImpl<const GlobalVariable *> &Order,
118 DenseSet<const GlobalVariable *> &Visited,
119 DenseSet<const GlobalVariable *> &Visiting) {
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000120 // Have we already visited this one?
Justin Holewinski0497ab12013-03-30 14:29:21 +0000121 if (Visited.count(GV))
122 return;
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000123
124 // Do we have a circular dependency?
Benjamin Kramer2c99e412014-10-10 15:32:50 +0000125 if (!Visiting.insert(GV).second)
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000126 report_fatal_error("Circular dependency found in global variable set");
127
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000128 // Make sure we visit all dependents first
Justin Holewinski01f89f02013-05-20 12:13:32 +0000129 DenseSet<const GlobalVariable *> Others;
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000130 for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i)
131 DiscoverDependentGlobals(GV->getOperand(i), Others);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000132
Justin Holewinski01f89f02013-05-20 12:13:32 +0000133 for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(),
134 E = Others.end();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000135 I != E; ++I)
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000136 VisitGlobalVariableForEmission(*I, Order, Visited, Visiting);
137
138 // Now we can visit ourself
139 Order.push_back(GV);
140 Visited.insert(GV);
141 Visiting.erase(GV);
142}
Justin Holewinskiae556d32012-05-04 20:18:50 +0000143
Justin Holewinskiae556d32012-05-04 20:18:50 +0000144void NVPTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000145 MCInst Inst;
146 lowerToMCInst(MI, Inst);
Lang Hames9ff69c82015-04-24 19:11:51 +0000147 EmitToStreamer(*OutStreamer, Inst);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000148}
149
Justin Holewinski30d56a72014-04-09 15:39:15 +0000150// Handle symbol backtracking for targets that do not support image handles
151bool NVPTXAsmPrinter::lowerImageHandleOperand(const MachineInstr *MI,
152 unsigned OpNo, MCOperand &MCOp) {
153 const MachineOperand &MO = MI->getOperand(OpNo);
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000154 const MCInstrDesc &MCID = MI->getDesc();
Justin Holewinski30d56a72014-04-09 15:39:15 +0000155
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000156 if (MCID.TSFlags & NVPTXII::IsTexFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000157 // This is a texture fetch, so operand 4 is a texref and operand 5 is
158 // a samplerref
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000159 if (OpNo == 4 && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000160 lowerImageHandleSymbol(MO.getImm(), MCOp);
161 return true;
162 }
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000163 if (OpNo == 5 && MO.isImm() && !(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000164 lowerImageHandleSymbol(MO.getImm(), MCOp);
165 return true;
166 }
167
168 return false;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000169 } else if (MCID.TSFlags & NVPTXII::IsSuldMask) {
170 unsigned VecSize =
171 1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1);
172
173 // For a surface load of vector size N, the Nth operand will be the surfref
174 if (OpNo == VecSize && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000175 lowerImageHandleSymbol(MO.getImm(), MCOp);
176 return true;
177 }
178
179 return false;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000180 } else if (MCID.TSFlags & NVPTXII::IsSustFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000181 // This is a surface store, so operand 0 is a surfref
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000182 if (OpNo == 0 && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000183 lowerImageHandleSymbol(MO.getImm(), MCOp);
184 return true;
185 }
186
187 return false;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000188 } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000189 // This is a query, so operand 1 is a surfref/texref
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000190 if (OpNo == 1 && MO.isImm()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000191 lowerImageHandleSymbol(MO.getImm(), MCOp);
192 return true;
193 }
194
195 return false;
196 }
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000197
198 return false;
Justin Holewinski30d56a72014-04-09 15:39:15 +0000199}
200
201void NVPTXAsmPrinter::lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp) {
202 // Ewwww
Matthias Braun7a75a912018-11-05 23:49:14 +0000203 LLVMTargetMachine &TM = const_cast<LLVMTargetMachine&>(MF->getTarget());
Justin Holewinski30d56a72014-04-09 15:39:15 +0000204 NVPTXTargetMachine &nvTM = static_cast<NVPTXTargetMachine&>(TM);
205 const NVPTXMachineFunctionInfo *MFI = MF->getInfo<NVPTXMachineFunctionInfo>();
206 const char *Sym = MFI->getImageHandleSymbol(Index);
207 std::string *SymNamePtr =
208 nvTM.getManagedStrPool()->getManagedString(Sym);
Malcolm Parsons06ac79c2016-11-02 16:43:50 +0000209 MCOp = GetSymbolRef(OutContext.getOrCreateSymbol(StringRef(*SymNamePtr)));
Justin Holewinski30d56a72014-04-09 15:39:15 +0000210}
211
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000212void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) {
213 OutMI.setOpcode(MI->getOpcode());
Justin Holewinski3d49e5c2013-11-15 12:30:04 +0000214 // Special: Do not mangle symbol operand of CALL_PROTOTYPE
215 if (MI->getOpcode() == NVPTX::CALL_PROTOTYPE) {
216 const MachineOperand &MO = MI->getOperand(0);
Justin Holewinski30d56a72014-04-09 15:39:15 +0000217 OutMI.addOperand(GetSymbolRef(
Jim Grosbach6f482002015-05-18 18:43:14 +0000218 OutContext.getOrCreateSymbol(Twine(MO.getSymbolName()))));
Justin Holewinski3d49e5c2013-11-15 12:30:04 +0000219 return;
220 }
221
Artem Belevichf802b932018-12-12 18:31:04 +0000222 const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>();
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000223 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
224 const MachineOperand &MO = MI->getOperand(i);
225
226 MCOperand MCOp;
Artem Belevichf802b932018-12-12 18:31:04 +0000227 if (!STI.hasImageHandles()) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000228 if (lowerImageHandleOperand(MI, i, MCOp)) {
229 OutMI.addOperand(MCOp);
230 continue;
231 }
232 }
233
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000234 if (lowerOperand(MO, MCOp))
235 OutMI.addOperand(MCOp);
236 }
237}
238
239bool NVPTXAsmPrinter::lowerOperand(const MachineOperand &MO,
240 MCOperand &MCOp) {
241 switch (MO.getType()) {
242 default: llvm_unreachable("unknown operand type");
243 case MachineOperand::MO_Register:
Jim Grosbache9119e42015-05-13 18:37:00 +0000244 MCOp = MCOperand::createReg(encodeVirtualRegister(MO.getReg()));
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000245 break;
246 case MachineOperand::MO_Immediate:
Jim Grosbache9119e42015-05-13 18:37:00 +0000247 MCOp = MCOperand::createImm(MO.getImm());
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000248 break;
249 case MachineOperand::MO_MachineBasicBlock:
Jim Grosbach13760bd2015-05-30 01:25:56 +0000250 MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000251 MO.getMBB()->getSymbol(), OutContext));
252 break;
253 case MachineOperand::MO_ExternalSymbol:
Justin Holewinski30d56a72014-04-09 15:39:15 +0000254 MCOp = GetSymbolRef(GetExternalSymbolSymbol(MO.getSymbolName()));
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000255 break;
256 case MachineOperand::MO_GlobalAddress:
Justin Holewinski30d56a72014-04-09 15:39:15 +0000257 MCOp = GetSymbolRef(getSymbol(MO.getGlobal()));
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000258 break;
259 case MachineOperand::MO_FPImmediate: {
260 const ConstantFP *Cnt = MO.getFPImm();
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000261 const APFloat &Val = Cnt->getValueAPF();
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000262
263 switch (Cnt->getType()->getTypeID()) {
264 default: report_fatal_error("Unsupported FP type"); break;
Artem Belevich64dc9be2017-01-13 20:56:17 +0000265 case Type::HalfTyID:
266 MCOp = MCOperand::createExpr(
267 NVPTXFloatMCExpr::createConstantFPHalf(Val, OutContext));
268 break;
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000269 case Type::FloatTyID:
Jim Grosbache9119e42015-05-13 18:37:00 +0000270 MCOp = MCOperand::createExpr(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000271 NVPTXFloatMCExpr::createConstantFPSingle(Val, OutContext));
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000272 break;
273 case Type::DoubleTyID:
Jim Grosbache9119e42015-05-13 18:37:00 +0000274 MCOp = MCOperand::createExpr(
Jim Grosbach13760bd2015-05-30 01:25:56 +0000275 NVPTXFloatMCExpr::createConstantFPDouble(Val, OutContext));
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000276 break;
277 }
278 break;
279 }
280 }
281 return true;
282}
283
284unsigned NVPTXAsmPrinter::encodeVirtualRegister(unsigned Reg) {
Justin Holewinski871ec932013-08-06 14:13:31 +0000285 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
286 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000287
Justin Holewinski871ec932013-08-06 14:13:31 +0000288 DenseMap<unsigned, unsigned> &RegMap = VRegMapping[RC];
289 unsigned RegNum = RegMap[Reg];
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000290
Justin Holewinski871ec932013-08-06 14:13:31 +0000291 // Encode the register class in the upper 4 bits
292 // Must be kept in sync with NVPTXInstPrinter::printRegName
293 unsigned Ret = 0;
294 if (RC == &NVPTX::Int1RegsRegClass) {
295 Ret = (1 << 28);
296 } else if (RC == &NVPTX::Int16RegsRegClass) {
297 Ret = (2 << 28);
298 } else if (RC == &NVPTX::Int32RegsRegClass) {
299 Ret = (3 << 28);
300 } else if (RC == &NVPTX::Int64RegsRegClass) {
301 Ret = (4 << 28);
302 } else if (RC == &NVPTX::Float32RegsRegClass) {
303 Ret = (5 << 28);
304 } else if (RC == &NVPTX::Float64RegsRegClass) {
305 Ret = (6 << 28);
Artem Belevich64dc9be2017-01-13 20:56:17 +0000306 } else if (RC == &NVPTX::Float16RegsRegClass) {
307 Ret = (7 << 28);
Artem Belevich620db1f2017-02-23 22:38:24 +0000308 } else if (RC == &NVPTX::Float16x2RegsRegClass) {
309 Ret = (8 << 28);
Justin Holewinski871ec932013-08-06 14:13:31 +0000310 } else {
311 report_fatal_error("Bad register class");
312 }
313
314 // Insert the vreg number
315 Ret |= (RegNum & 0x0FFFFFFF);
316 return Ret;
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000317 } else {
Justin Holewinski871ec932013-08-06 14:13:31 +0000318 // Some special-use registers are actually physical registers.
319 // Encode this as the register class ID of 0 and the real register ID.
320 return Reg & 0x0FFFFFFF;
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000321 }
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000322}
323
Justin Holewinski30d56a72014-04-09 15:39:15 +0000324MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol *Symbol) {
Justin Holewinskia2a63d22013-08-06 14:13:27 +0000325 const MCExpr *Expr;
Jim Grosbach13760bd2015-05-30 01:25:56 +0000326 Expr = MCSymbolRefExpr::create(Symbol, MCSymbolRefExpr::VK_None,
Justin Holewinski8b24e1e2013-08-06 23:06:42 +0000327 OutContext);
Jim Grosbache9119e42015-05-13 18:37:00 +0000328 return MCOperand::createExpr(Expr);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000329}
330
Justin Holewinski0497ab12013-03-30 14:29:21 +0000331void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
Mehdi Amini56228da2015-07-09 01:57:34 +0000332 const DataLayout &DL = getDataLayout();
Artem Belevichf802b932018-12-12 18:31:04 +0000333 const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F);
334 const TargetLowering *TLI = STI.getTargetLowering();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000335
336 Type *Ty = F->getReturnType();
337
Artem Belevichf802b932018-12-12 18:31:04 +0000338 bool isABI = (STI.getSmVersion() >= 20);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000339
340 if (Ty->getTypeID() == Type::VoidTyID)
341 return;
342
343 O << " (";
344
345 if (isABI) {
Artem Belevichd7a73822017-07-20 21:16:03 +0000346 if (Ty->isFloatingPointTy() || (Ty->isIntegerTy() && !Ty->isIntegerTy(128))) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000347 unsigned size = 0;
Craig Toppere3dcce92015-08-01 22:20:21 +0000348 if (auto *ITy = dyn_cast<IntegerType>(Ty)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000349 size = ITy->getBitWidth();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000350 } else {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000351 assert(Ty->isFloatingPointTy() && "Floating point type expected here");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000352 size = Ty->getPrimitiveSizeInBits();
353 }
Artem Belevich64dc9be2017-01-13 20:56:17 +0000354 // PTX ABI requires all scalar return values to be at least 32
355 // bits in size. fp16 normally uses .b16 as its storage type in
356 // PTX, so its size must be adjusted here, too.
357 if (size < 32)
358 size = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000359
360 O << ".param .b" << size << " func_retval0";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000361 } else if (isa<PointerType>(Ty)) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000362 O << ".param .b" << TLI->getPointerTy(DL).getSizeInBits()
Justin Holewinski0497ab12013-03-30 14:29:21 +0000363 << " func_retval0";
Artem Belevichd7a73822017-07-20 21:16:03 +0000364 } else if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) {
Mehdi Amini56228da2015-07-09 01:57:34 +0000365 unsigned totalsz = DL.getTypeAllocSize(Ty);
Artem Belevich74158b52016-07-20 18:39:52 +0000366 unsigned retAlignment = 0;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000367 if (!getAlign(*F, 0, retAlignment))
Artem Belevich74158b52016-07-20 18:39:52 +0000368 retAlignment = DL.getABITypeAlignment(Ty);
369 O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz
370 << "]";
Craig Topperd3c02f12015-01-05 10:15:49 +0000371 } else
372 llvm_unreachable("Unknown return type");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000373 } else {
374 SmallVector<EVT, 16> vtparts;
Mehdi Amini56228da2015-07-09 01:57:34 +0000375 ComputeValueVTs(*TLI, DL, Ty, vtparts);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000376 unsigned idx = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000377 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000378 unsigned elems = 1;
379 EVT elemtype = vtparts[i];
380 if (vtparts[i].isVector()) {
381 elems = vtparts[i].getVectorNumElements();
382 elemtype = vtparts[i].getVectorElementType();
383 }
384
Justin Holewinski0497ab12013-03-30 14:29:21 +0000385 for (unsigned j = 0, je = elems; j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000386 unsigned sz = elemtype.getSizeInBits();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000387 if (elemtype.isInteger() && (sz < 32))
388 sz = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000389 O << ".reg .b" << sz << " func_retval" << idx;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000390 if (j < je - 1)
391 O << ", ";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000392 ++idx;
393 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000394 if (i < e - 1)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000395 O << ", ";
396 }
397 }
398 O << ") ";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000399}
400
401void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF,
402 raw_ostream &O) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000403 const Function &F = MF.getFunction();
404 printReturnValStr(&F, O);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000405}
406
Jingyue Wu0220df02015-02-01 02:27:45 +0000407// Return true if MBB is the header of a loop marked with
408// llvm.loop.unroll.disable.
Jingyue Wu5bbcdaa2015-02-03 17:57:38 +0000409// TODO: consider "#pragma unroll 1" which is equivalent to "#pragma nounroll".
Jingyue Wu0220df02015-02-01 02:27:45 +0000410bool NVPTXAsmPrinter::isLoopHeaderOfNoUnroll(
411 const MachineBasicBlock &MBB) const {
412 MachineLoopInfo &LI = getAnalysis<MachineLoopInfo>();
Jingyue Wu0220df02015-02-01 02:27:45 +0000413 // We insert .pragma "nounroll" only to the loop header.
Benjamin Kramerdb220db2015-06-02 15:28:27 +0000414 if (!LI.isLoopHeader(&MBB))
Jingyue Wu0220df02015-02-01 02:27:45 +0000415 return false;
416
417 // llvm.loop.unroll.disable is marked on the back edges of a loop. Therefore,
418 // we iterate through each back edge of the loop with header MBB, and check
419 // whether its metadata contains llvm.loop.unroll.disable.
420 for (auto I = MBB.pred_begin(); I != MBB.pred_end(); ++I) {
421 const MachineBasicBlock *PMBB = *I;
422 if (LI.getLoopFor(PMBB) != LI.getLoopFor(&MBB)) {
423 // Edges from other loops to MBB are not back edges.
424 continue;
425 }
426 if (const BasicBlock *PBB = PMBB->getBasicBlock()) {
Duncan P. N. Exon Smith1d15a9f2016-03-25 00:35:38 +0000427 if (MDNode *LoopID =
428 PBB->getTerminator()->getMetadata(LLVMContext::MD_loop)) {
Jingyue Wu0220df02015-02-01 02:27:45 +0000429 if (GetUnrollMetadata(LoopID, "llvm.loop.unroll.disable"))
430 return true;
431 }
432 }
433 }
434 return false;
435}
436
437void NVPTXAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
438 AsmPrinter::EmitBasicBlockStart(MBB);
439 if (isLoopHeaderOfNoUnroll(MBB))
Lang Hames9ff69c82015-04-24 19:11:51 +0000440 OutStreamer->EmitRawText(StringRef("\t.pragma \"nounroll\";\n"));
Jingyue Wu0220df02015-02-01 02:27:45 +0000441}
442
Justin Holewinskiae556d32012-05-04 20:18:50 +0000443void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
444 SmallString<128> Str;
445 raw_svector_ostream O(Str);
446
Justin Holewinski01f89f02013-05-20 12:13:32 +0000447 if (!GlobalsEmitted) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000448 emitGlobals(*MF->getFunction().getParent());
Justin Holewinski01f89f02013-05-20 12:13:32 +0000449 GlobalsEmitted = true;
450 }
Eric Christopher68f22182018-05-18 03:13:08 +0000451
Justin Holewinskiae556d32012-05-04 20:18:50 +0000452 // Set up
453 MRI = &MF->getRegInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +0000454 F = &MF->getFunction();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000455 emitLinkageDirective(F, O);
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000456 if (isKernelFunction(*F))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000457 O << ".entry ";
458 else {
459 O << ".func ";
460 printReturnValStr(*MF, O);
461 }
462
Matt Arsenault8b643552015-06-09 00:31:39 +0000463 CurrentFnSym->print(O, MAI);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000464
465 emitFunctionParamList(*MF, O);
466
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000467 if (isKernelFunction(*F))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000468 emitKernelFunctionDirectives(*F, O);
469
Lang Hames9ff69c82015-04-24 19:11:51 +0000470 OutStreamer->EmitRawText(O.str());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000471
Eric Christopher68f22182018-05-18 03:13:08 +0000472 VRegMapping.clear();
473 // Emit open brace for function body.
474 OutStreamer->EmitRawText(StringRef("{\n"));
475 setAndEmitFunctionVirtualRegisters(*MF);
476}
477
478bool NVPTXAsmPrinter::runOnMachineFunction(MachineFunction &F) {
Eric Christopher68f22182018-05-18 03:13:08 +0000479 bool Result = AsmPrinter::runOnMachineFunction(F);
480 // Emit closing brace for the body of function F.
481 // The closing brace must be emitted here because we need to emit additional
482 // debug labels/data after the last basic block.
483 // We need to emit the closing brace here because we don't have function that
484 // finished emission of the function body.
485 OutStreamer->EmitRawText(StringRef("}\n"));
486 return Result;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000487}
488
489void NVPTXAsmPrinter::EmitFunctionBodyStart() {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000490 SmallString<128> Str;
491 raw_svector_ostream O(Str);
Matthias Braunf1caa282017-12-15 22:22:58 +0000492 emitDemotedVars(&MF->getFunction(), O);
Lang Hames9ff69c82015-04-24 19:11:51 +0000493 OutStreamer->EmitRawText(O.str());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000494}
495
496void NVPTXAsmPrinter::EmitFunctionBodyEnd() {
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +0000497 VRegMapping.clear();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000498}
499
Alexey Bataev4dd75582018-07-26 16:29:52 +0000500const MCSymbol *NVPTXAsmPrinter::getFunctionFrameSymbol() const {
501 SmallString<128> Str;
502 raw_svector_ostream(Str) << DEPOTNAME << getFunctionNumber();
503 return OutContext.getOrCreateSymbol(Str);
504}
505
Justin Holewinski660597d2013-10-11 12:39:36 +0000506void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
507 unsigned RegNo = MI->getOperand(0).getReg();
Andrew Kaylor5c73e1f2015-03-24 23:37:10 +0000508 if (TargetRegisterInfo::isVirtualRegister(RegNo)) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000509 OutStreamer->AddComment(Twine("implicit-def: ") +
510 getVirtualRegisterName(RegNo));
Justin Holewinski660597d2013-10-11 12:39:36 +0000511 } else {
Artem Belevichf802b932018-12-12 18:31:04 +0000512 const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>();
Lang Hames9ff69c82015-04-24 19:11:51 +0000513 OutStreamer->AddComment(Twine("implicit-def: ") +
Artem Belevichf802b932018-12-12 18:31:04 +0000514 STI.getRegisterInfo()->getName(RegNo));
Justin Holewinski660597d2013-10-11 12:39:36 +0000515 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000516 OutStreamer->AddBlankLine();
Justin Holewinski660597d2013-10-11 12:39:36 +0000517}
518
Justin Holewinski0497ab12013-03-30 14:29:21 +0000519void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F,
520 raw_ostream &O) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000521 // If the NVVM IR has some of reqntid* specified, then output
522 // the reqntid directive, and set the unspecified ones to 1.
523 // If none of reqntid* is specified, don't output reqntid directive.
524 unsigned reqntidx, reqntidy, reqntidz;
525 bool specified = false;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000526 if (!getReqNTIDx(F, reqntidx))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000527 reqntidx = 1;
528 else
529 specified = true;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000530 if (!getReqNTIDy(F, reqntidy))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000531 reqntidy = 1;
532 else
533 specified = true;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000534 if (!getReqNTIDz(F, reqntidz))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000535 reqntidz = 1;
536 else
537 specified = true;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000538
539 if (specified)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000540 O << ".reqntid " << reqntidx << ", " << reqntidy << ", " << reqntidz
541 << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000542
543 // If the NVVM IR has some of maxntid* specified, then output
544 // the maxntid directive, and set the unspecified ones to 1.
545 // If none of maxntid* is specified, don't output maxntid directive.
546 unsigned maxntidx, maxntidy, maxntidz;
547 specified = false;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000548 if (!getMaxNTIDx(F, maxntidx))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000549 maxntidx = 1;
550 else
551 specified = true;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000552 if (!getMaxNTIDy(F, maxntidy))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000553 maxntidy = 1;
554 else
555 specified = true;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000556 if (!getMaxNTIDz(F, maxntidz))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000557 maxntidz = 1;
558 else
559 specified = true;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000560
561 if (specified)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000562 O << ".maxntid " << maxntidx << ", " << maxntidy << ", " << maxntidz
563 << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000564
565 unsigned mincta;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000566 if (getMinCTASm(F, mincta))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000567 O << ".minnctapersm " << mincta << "\n";
Justin Lebar19bf9d22016-12-14 22:32:50 +0000568
569 unsigned maxnreg;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000570 if (getMaxNReg(F, maxnreg))
Justin Lebar19bf9d22016-12-14 22:32:50 +0000571 O << ".maxnreg " << maxnreg << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000572}
573
Justin Holewinski660597d2013-10-11 12:39:36 +0000574std::string
575NVPTXAsmPrinter::getVirtualRegisterName(unsigned Reg) const {
576 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000577
Justin Holewinski660597d2013-10-11 12:39:36 +0000578 std::string Name;
579 raw_string_ostream NameStr(Name);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000580
Justin Holewinski660597d2013-10-11 12:39:36 +0000581 VRegRCMap::const_iterator I = VRegMapping.find(RC);
582 assert(I != VRegMapping.end() && "Bad register class");
583 const DenseMap<unsigned, unsigned> &RegMap = I->second;
584
585 VRegMap::const_iterator VI = RegMap.find(Reg);
586 assert(VI != RegMap.end() && "Bad virtual register");
587 unsigned MappedVR = VI->second;
588
589 NameStr << getNVPTXRegClassStr(RC) << MappedVR;
590
591 NameStr.flush();
592 return Name;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000593}
594
Justin Holewinski660597d2013-10-11 12:39:36 +0000595void NVPTXAsmPrinter::emitVirtualRegister(unsigned int vr,
Justin Holewinski0497ab12013-03-30 14:29:21 +0000596 raw_ostream &O) {
Justin Holewinski660597d2013-10-11 12:39:36 +0000597 O << getVirtualRegisterName(vr);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000598}
599
Justin Holewinski0497ab12013-03-30 14:29:21 +0000600void NVPTXAsmPrinter::printVecModifiedImmediate(
601 const MachineOperand &MO, const char *Modifier, raw_ostream &O) {
602 static const char vecelem[] = { '0', '1', '2', '3', '0', '1', '2', '3' };
603 int Imm = (int) MO.getImm();
604 if (0 == strcmp(Modifier, "vecelem"))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000605 O << "_" << vecelem[Imm];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000606 else if (0 == strcmp(Modifier, "vecv4comm1")) {
607 if ((Imm < 0) || (Imm > 3))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000608 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000609 } else if (0 == strcmp(Modifier, "vecv4comm2")) {
610 if ((Imm < 4) || (Imm > 7))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000611 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000612 } else if (0 == strcmp(Modifier, "vecv4pos")) {
613 if (Imm < 0)
614 Imm = 0;
615 O << "_" << vecelem[Imm % 4];
616 } else if (0 == strcmp(Modifier, "vecv2comm1")) {
617 if ((Imm < 0) || (Imm > 1))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000618 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000619 } else if (0 == strcmp(Modifier, "vecv2comm2")) {
620 if ((Imm < 2) || (Imm > 3))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000621 O << "//";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000622 } else if (0 == strcmp(Modifier, "vecv2pos")) {
623 if (Imm < 0)
624 Imm = 0;
625 O << "_" << vecelem[Imm % 2];
626 } else
Craig Topperbdf39a42012-05-24 07:02:50 +0000627 llvm_unreachable("Unknown Modifier on immediate operand");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000628}
629
Justin Holewinski0497ab12013-03-30 14:29:21 +0000630void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000631 emitLinkageDirective(F, O);
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000632 if (isKernelFunction(*F))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000633 O << ".entry ";
634 else
635 O << ".func ";
636 printReturnValStr(F, O);
Matt Arsenault8b643552015-06-09 00:31:39 +0000637 getSymbol(F)->print(O, MAI);
638 O << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000639 emitFunctionParamList(F, O);
640 O << ";\n";
641}
642
Justin Holewinski0497ab12013-03-30 14:29:21 +0000643static bool usedInGlobalVarDef(const Constant *C) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000644 if (!C)
645 return false;
646
647 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Jingyue Wu4be014a2015-07-31 05:09:47 +0000648 return GV->getName() != "llvm.used";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000649 }
650
Chandler Carruthcdf47882014-03-09 03:16:01 +0000651 for (const User *U : C->users())
652 if (const Constant *C = dyn_cast<Constant>(U))
653 if (usedInGlobalVarDef(C))
654 return true;
655
Justin Holewinskiae556d32012-05-04 20:18:50 +0000656 return false;
657}
658
Justin Holewinski0497ab12013-03-30 14:29:21 +0000659static bool usedInOneFunc(const User *U, Function const *&oneFunc) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000660 if (const GlobalVariable *othergv = dyn_cast<GlobalVariable>(U)) {
Yaron Keren075759a2015-03-30 15:42:36 +0000661 if (othergv->getName() == "llvm.used")
Justin Holewinskiae556d32012-05-04 20:18:50 +0000662 return true;
663 }
664
665 if (const Instruction *instr = dyn_cast<Instruction>(U)) {
666 if (instr->getParent() && instr->getParent()->getParent()) {
667 const Function *curFunc = instr->getParent()->getParent();
668 if (oneFunc && (curFunc != oneFunc))
669 return false;
670 oneFunc = curFunc;
671 return true;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000672 } else
Justin Holewinskiae556d32012-05-04 20:18:50 +0000673 return false;
674 }
675
Chandler Carruthcdf47882014-03-09 03:16:01 +0000676 for (const User *UU : U->users())
Eli Bendersky3e840192015-03-23 16:26:23 +0000677 if (!usedInOneFunc(UU, oneFunc))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000678 return false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000679
Justin Holewinskiae556d32012-05-04 20:18:50 +0000680 return true;
681}
682
683/* Find out if a global variable can be demoted to local scope.
684 * Currently, this is valid for CUDA shared variables, which have local
685 * scope and global lifetime. So the conditions to check are :
686 * 1. Is the global variable in shared address space?
687 * 2. Does it have internal linkage?
688 * 3. Is the global variable referenced only in one function?
689 */
690static bool canDemoteGlobalVar(const GlobalVariable *gv, Function const *&f) {
Eli Bendersky3e840192015-03-23 16:26:23 +0000691 if (!gv->hasInternalLinkage())
Justin Holewinskiae556d32012-05-04 20:18:50 +0000692 return false;
Craig Toppere3dcce92015-08-01 22:20:21 +0000693 PointerType *Pty = gv->getType();
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000694 if (Pty->getAddressSpace() != ADDRESS_SPACE_SHARED)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000695 return false;
696
Craig Topper062a2ba2014-04-25 05:30:21 +0000697 const Function *oneFunc = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000698
699 bool flag = usedInOneFunc(gv, oneFunc);
Eli Bendersky3e840192015-03-23 16:26:23 +0000700 if (!flag)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000701 return false;
702 if (!oneFunc)
703 return false;
704 f = oneFunc;
705 return true;
706}
707
708static bool useFuncSeen(const Constant *C,
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000709 DenseMap<const Function *, bool> &seenMap) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000710 for (const User *U : C->users()) {
711 if (const Constant *cu = dyn_cast<Constant>(U)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000712 if (useFuncSeen(cu, seenMap))
713 return true;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000714 } else if (const Instruction *I = dyn_cast<Instruction>(U)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000715 const BasicBlock *bb = I->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000716 if (!bb)
717 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000718 const Function *caller = bb->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000719 if (!caller)
720 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000721 if (seenMap.find(caller) != seenMap.end())
722 return true;
723 }
724 }
725 return false;
726}
727
Justin Holewinski01f89f02013-05-20 12:13:32 +0000728void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +0000729 DenseMap<const Function *, bool> seenMap;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000730 for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
Duncan P. N. Exon Smith61149b82015-10-20 00:54:09 +0000731 const Function *F = &*FI;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000732
Justin Lebar49fac562018-12-26 19:12:31 +0000733 if (F->getAttributes().hasFnAttribute("nvptx-libcall-callee")) {
734 emitDeclaration(F, O);
735 continue;
736 }
737
Justin Holewinskiae556d32012-05-04 20:18:50 +0000738 if (F->isDeclaration()) {
739 if (F->use_empty())
740 continue;
741 if (F->getIntrinsicID())
742 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000743 emitDeclaration(F, O);
744 continue;
745 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000746 for (const User *U : F->users()) {
747 if (const Constant *C = dyn_cast<Constant>(U)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000748 if (usedInGlobalVarDef(C)) {
749 // The use is in the initialization of a global variable
750 // that is a function pointer, so print a declaration
751 // for the original function
Justin Holewinskiae556d32012-05-04 20:18:50 +0000752 emitDeclaration(F, O);
753 break;
754 }
755 // Emit a declaration of this function if the function that
756 // uses this constant expr has already been seen.
757 if (useFuncSeen(C, seenMap)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000758 emitDeclaration(F, O);
759 break;
760 }
761 }
762
Chandler Carruthcdf47882014-03-09 03:16:01 +0000763 if (!isa<Instruction>(U))
Justin Holewinski0497ab12013-03-30 14:29:21 +0000764 continue;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000765 const Instruction *instr = cast<Instruction>(U);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000766 const BasicBlock *bb = instr->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000767 if (!bb)
768 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000769 const Function *caller = bb->getParent();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000770 if (!caller)
771 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000772
773 // If a caller has already been seen, then the caller is
774 // appearing in the module before the callee. so print out
775 // a declaration for the callee.
776 if (seenMap.find(caller) != seenMap.end()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000777 emitDeclaration(F, O);
778 break;
779 }
780 }
781 seenMap[F] = true;
782 }
783}
784
Justin Lebaread59f42016-01-30 01:07:38 +0000785static bool isEmptyXXStructor(GlobalVariable *GV) {
786 if (!GV) return true;
787 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
788 if (!InitList) return true; // Not an array; we don't know how to parse.
789 return InitList->getNumOperands() == 0;
790}
791
Justin Holewinski0497ab12013-03-30 14:29:21 +0000792bool NVPTXAsmPrinter::doInitialization(Module &M) {
Eric Christopher6aad8b12015-02-19 00:08:14 +0000793 // Construct a default subtarget off of the TargetMachine defaults. The
794 // rest of NVPTX isn't friendly to change subtargets per function and
795 // so the default TargetMachine will have all of the options.
Eric Christopher6aad8b12015-02-19 00:08:14 +0000796 const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM);
Justin Lebar7f41fe32018-12-22 01:30:37 +0000797 const auto* STI = static_cast<const NVPTXSubtarget*>(NTM.getSubtargetImpl());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000798
Justin Lebar3a5f5792016-01-23 21:12:20 +0000799 if (M.alias_size()) {
800 report_fatal_error("Module has aliases, which NVPTX does not support.");
801 return true; // error
802 }
Justin Lebaread59f42016-01-30 01:07:38 +0000803 if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_ctors"))) {
804 report_fatal_error(
805 "Module has a nontrivial global ctor, which NVPTX does not support.");
806 return true; // error
807 }
808 if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_dtors"))) {
809 report_fatal_error(
810 "Module has a nontrivial global dtor, which NVPTX does not support.");
811 return true; // error
812 }
Justin Lebar3a5f5792016-01-23 21:12:20 +0000813
Justin Holewinskiae556d32012-05-04 20:18:50 +0000814 SmallString<128> Str1;
815 raw_svector_ostream OS1(Str1);
816
Justin Holewinskiae556d32012-05-04 20:18:50 +0000817 // We need to call the parent's one explicitly.
Eric Christopher68f22182018-05-18 03:13:08 +0000818 bool Result = AsmPrinter::doInitialization(M);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000819
Justin Holewinskiae556d32012-05-04 20:18:50 +0000820 // Emit header before any dwarf directives are emitted below.
Justin Lebar7f41fe32018-12-22 01:30:37 +0000821 emitHeader(M, OS1, *STI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000822 OutStreamer->EmitRawText(OS1.str());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000823
Justin Holewinskid2bbdf02013-07-01 13:00:14 +0000824 // Emit module-level inline asm if it exists.
825 if (!M.getModuleInlineAsm().empty()) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000826 OutStreamer->AddComment("Start of file scope inline assembly");
827 OutStreamer->AddBlankLine();
828 OutStreamer->EmitRawText(StringRef(M.getModuleInlineAsm()));
829 OutStreamer->AddBlankLine();
830 OutStreamer->AddComment("End of file scope inline assembly");
831 OutStreamer->AddBlankLine();
Justin Holewinskid2bbdf02013-07-01 13:00:14 +0000832 }
833
Eric Christopher80244252018-05-01 00:10:13 +0000834 GlobalsEmitted = false;
Eric Christopher68f22182018-05-18 03:13:08 +0000835
836 return Result;
Justin Holewinski01f89f02013-05-20 12:13:32 +0000837}
838
839void NVPTXAsmPrinter::emitGlobals(const Module &M) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000840 SmallString<128> Str2;
841 raw_svector_ostream OS2(Str2);
842
843 emitDeclarations(M, OS2);
844
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000845 // As ptxas does not support forward references of globals, we need to first
846 // sort the list of module-level globals in def-use order. We visit each
847 // global variable in order, and ensure that we emit it *after* its dependent
848 // globals. We use a little extra memory maintaining both a set and a list to
849 // have fast searches while maintaining a strict ordering.
Justin Holewinski01f89f02013-05-20 12:13:32 +0000850 SmallVector<const GlobalVariable *, 8> Globals;
851 DenseSet<const GlobalVariable *> GVVisited;
852 DenseSet<const GlobalVariable *> GVVisiting;
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000853
854 // Visit each global variable, in order
Duncan P. N. Exon Smith61149b82015-10-20 00:54:09 +0000855 for (const GlobalVariable &I : M.globals())
856 VisitGlobalVariableForEmission(&I, Globals, GVVisited, GVVisiting);
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000857
Justin Holewinski0497ab12013-03-30 14:29:21 +0000858 assert(GVVisited.size() == M.getGlobalList().size() &&
Justin Holewinski2c5ac702012-11-16 21:03:51 +0000859 "Missed a global variable");
860 assert(GVVisiting.size() == 0 && "Did not fully process a global variable");
861
862 // Print out module-level global variables in proper order
863 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
864 printModuleLevelGV(Globals[i], OS2);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000865
866 OS2 << '\n';
867
Lang Hames9ff69c82015-04-24 19:11:51 +0000868 OutStreamer->EmitRawText(OS2.str());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000869}
870
Eric Christopher6aad8b12015-02-19 00:08:14 +0000871void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O,
872 const NVPTXSubtarget &STI) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000873 O << "//\n";
874 O << "// Generated by LLVM NVPTX Back-End\n";
875 O << "//\n";
876 O << "\n";
877
Eric Christopher6aad8b12015-02-19 00:08:14 +0000878 unsigned PTXVersion = STI.getPTXVersion();
Justin Holewinski1812ee92012-11-12 03:16:43 +0000879 O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000880
881 O << ".target ";
Eric Christopher6aad8b12015-02-19 00:08:14 +0000882 O << STI.getTargetName();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000883
Eric Christopherca929f22015-02-19 00:22:47 +0000884 const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM);
885 if (NTM.getDrvInterface() == NVPTX::NVCL)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000886 O << ", texmode_independent";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000887
Alexey Bataev2e1a7822018-12-06 16:25:35 +0000888 bool HasFullDebugInfo = false;
889 for (DICompileUnit *CU : M.debug_compile_units()) {
890 switch(CU->getEmissionKind()) {
891 case DICompileUnit::NoDebug:
892 case DICompileUnit::DebugDirectivesOnly:
893 break;
894 case DICompileUnit::LineTablesOnly:
895 case DICompileUnit::FullDebug:
896 HasFullDebugInfo = true;
897 break;
898 }
899 if (HasFullDebugInfo)
900 break;
901 }
Eric Christopher68f22182018-05-18 03:13:08 +0000902 // FIXME: remove comment once debug info is properly supported.
Alexey Bataev2e1a7822018-12-06 16:25:35 +0000903 if (MMI && MMI->hasDebugInfo() && HasFullDebugInfo)
Eric Christopher68f22182018-05-18 03:13:08 +0000904 O << "//, debug";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000905
906 O << "\n";
907
908 O << ".address_size ";
Eric Christopherca929f22015-02-19 00:22:47 +0000909 if (NTM.is64Bit())
Justin Holewinskiae556d32012-05-04 20:18:50 +0000910 O << "64";
911 else
912 O << "32";
913 O << "\n";
914
915 O << "\n";
916}
917
918bool NVPTXAsmPrinter::doFinalization(Module &M) {
Eric Christopher68f22182018-05-18 03:13:08 +0000919 bool HasDebugInfo = MMI && MMI->hasDebugInfo();
920
Justin Holewinski01f89f02013-05-20 12:13:32 +0000921 // If we did not emit any functions, then the global declarations have not
922 // yet been emitted.
923 if (!GlobalsEmitted) {
924 emitGlobals(M);
925 GlobalsEmitted = true;
926 }
927
Justin Holewinskiae556d32012-05-04 20:18:50 +0000928 // XXX Temproarily remove global variables so that doFinalization() will not
929 // emit them again (global variables are emitted at beginning).
930
931 Module::GlobalListType &global_list = M.getGlobalList();
932 int i, n = global_list.size();
Dylan Noblesmithc9e2a272014-08-26 02:03:35 +0000933 GlobalVariable **gv_array = new GlobalVariable *[n];
Justin Holewinskiae556d32012-05-04 20:18:50 +0000934
935 // first, back-up GlobalVariable in gv_array
936 i = 0;
937 for (Module::global_iterator I = global_list.begin(), E = global_list.end();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000938 I != E; ++I)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000939 gv_array[i++] = &*I;
940
941 // second, empty global_list
942 while (!global_list.empty())
943 global_list.remove(global_list.begin());
944
945 // call doFinalization
946 bool ret = AsmPrinter::doFinalization(M);
947
948 // now we restore global variables
Justin Holewinski0497ab12013-03-30 14:29:21 +0000949 for (i = 0; i < n; i++)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000950 global_list.insert(global_list.end(), gv_array[i]);
951
Justin Holewinski59596952014-04-09 15:38:52 +0000952 clearAnnotationCache(&M);
Dylan Noblesmithc9e2a272014-08-26 02:03:35 +0000953
954 delete[] gv_array;
Eric Christopher68f22182018-05-18 03:13:08 +0000955 // FIXME: remove comment once debug info is properly supported.
956 // Close the last emitted section
957 if (HasDebugInfo)
958 OutStreamer->EmitRawText("//\t}");
959
Alexey Bataev64ad0ad2018-12-06 16:02:09 +0000960 // Output last DWARF .file directives, if any.
961 static_cast<NVPTXTargetStreamer *>(OutStreamer->getTargetStreamer())
962 ->outputDwarfFileDirectives();
963
Justin Holewinskiae556d32012-05-04 20:18:50 +0000964 return ret;
965
Justin Holewinskiae556d32012-05-04 20:18:50 +0000966 //bool Result = AsmPrinter::doFinalization(M);
967 // Instead of calling the parents doFinalization, we may
968 // clone parents doFinalization and customize here.
969 // Currently, we if NVISA out the EmitGlobals() in
970 // parent's doFinalization, which is too intrusive.
971 //
972 // Same for the doInitialization.
973 //return Result;
974}
975
976// This function emits appropriate linkage directives for
977// functions and global variables.
978//
979// extern function declaration -> .extern
980// extern function definition -> .visible
981// external global variable with init -> .visible
982// external without init -> .extern
983// appending -> not allowed, assert.
Justin Holewinski7d5bf662014-06-27 18:35:10 +0000984// for any linkage other than
985// internal, private, linker_private,
986// linker_private_weak, linker_private_weak_def_auto,
987// we emit -> .weak.
Justin Holewinskiae556d32012-05-04 20:18:50 +0000988
Justin Holewinski0497ab12013-03-30 14:29:21 +0000989void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V,
990 raw_ostream &O) {
Eric Christopherbeffc4e2015-02-19 00:08:23 +0000991 if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000992 if (V->hasExternalLinkage()) {
993 if (isa<GlobalVariable>(V)) {
994 const GlobalVariable *GVar = cast<GlobalVariable>(V);
995 if (GVar) {
996 if (GVar->hasInitializer())
997 O << ".visible ";
998 else
999 O << ".extern ";
1000 }
1001 } else if (V->isDeclaration())
1002 O << ".extern ";
1003 else
1004 O << ".visible ";
1005 } else if (V->hasAppendingLinkage()) {
1006 std::string msg;
1007 msg.append("Error: ");
1008 msg.append("Symbol ");
1009 if (V->hasName())
Yaron Keren075759a2015-03-30 15:42:36 +00001010 msg.append(V->getName());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001011 msg.append("has unsupported appending linkage type");
1012 llvm_unreachable(msg.c_str());
Justin Holewinski7d5bf662014-06-27 18:35:10 +00001013 } else if (!V->hasInternalLinkage() &&
1014 !V->hasPrivateLinkage()) {
1015 O << ".weak ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001016 }
1017 }
1018}
1019
Justin Holewinski01f89f02013-05-20 12:13:32 +00001020void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
1021 raw_ostream &O,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001022 bool processDemoted) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001023 // Skip meta data
1024 if (GVar->hasSection()) {
Rafael Espindola83658d62016-05-11 18:21:59 +00001025 if (GVar->getSection() == "llvm.metadata")
Justin Holewinskiae556d32012-05-04 20:18:50 +00001026 return;
1027 }
1028
Justin Holewinski73cb5de2014-06-27 18:35:53 +00001029 // Skip LLVM intrinsic global variables
1030 if (GVar->getName().startswith("llvm.") ||
1031 GVar->getName().startswith("nvvm."))
1032 return;
1033
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001034 const DataLayout &DL = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001035
1036 // GlobalVariables are always constant pointers themselves.
Craig Toppere3dcce92015-08-01 22:20:21 +00001037 PointerType *PTy = GVar->getType();
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00001038 Type *ETy = GVar->getValueType();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001039
1040 if (GVar->hasExternalLinkage()) {
1041 if (GVar->hasInitializer())
1042 O << ".visible ";
1043 else
1044 O << ".extern ";
Justin Holewinskid73767a2014-06-27 18:35:56 +00001045 } else if (GVar->hasLinkOnceLinkage() || GVar->hasWeakLinkage() ||
1046 GVar->hasAvailableExternallyLinkage() ||
1047 GVar->hasCommonLinkage()) {
1048 O << ".weak ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001049 }
1050
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001051 if (isTexture(*GVar)) {
1052 O << ".global .texref " << getTextureName(*GVar) << ";\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001053 return;
1054 }
1055
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001056 if (isSurface(*GVar)) {
1057 O << ".global .surfref " << getSurfaceName(*GVar) << ";\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001058 return;
1059 }
1060
1061 if (GVar->isDeclaration()) {
1062 // (extern) declarations, no definition or initializer
1063 // Currently the only known declaration is for an automatic __local
1064 // (.shared) promoted to global.
1065 emitPTXGlobalVariable(GVar, O);
1066 O << ";\n";
1067 return;
1068 }
1069
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001070 if (isSampler(*GVar)) {
1071 O << ".global .samplerref " << getSamplerName(*GVar);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001072
Craig Topper062a2ba2014-04-25 05:30:21 +00001073 const Constant *Initializer = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001074 if (GVar->hasInitializer())
1075 Initializer = GVar->getInitializer();
Craig Topper062a2ba2014-04-25 05:30:21 +00001076 const ConstantInt *CI = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001077 if (Initializer)
1078 CI = dyn_cast<ConstantInt>(Initializer);
1079 if (CI) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001080 unsigned sample = CI->getZExtValue();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001081
1082 O << " = { ";
1083
Justin Holewinski0497ab12013-03-30 14:29:21 +00001084 for (int i = 0,
1085 addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE);
1086 i < 3; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001087 O << "addr_mode_" << i << " = ";
1088 switch (addr) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001089 case 0:
1090 O << "wrap";
1091 break;
1092 case 1:
1093 O << "clamp_to_border";
1094 break;
1095 case 2:
1096 O << "clamp_to_edge";
1097 break;
1098 case 3:
1099 O << "wrap";
1100 break;
1101 case 4:
1102 O << "mirror";
1103 break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001104 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001105 O << ", ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001106 }
1107 O << "filter_mode = ";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001108 switch ((sample & __CLK_FILTER_MASK) >> __CLK_FILTER_BASE) {
1109 case 0:
1110 O << "nearest";
1111 break;
1112 case 1:
1113 O << "linear";
1114 break;
1115 case 2:
Craig Topper2a30d782014-06-18 05:05:13 +00001116 llvm_unreachable("Anisotropic filtering is not supported");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001117 default:
1118 O << "nearest";
1119 break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001120 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001121 if (!((sample & __CLK_NORMALIZED_MASK) >> __CLK_NORMALIZED_BASE)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001122 O << ", force_unnormalized_coords = 1";
1123 }
1124 O << " }";
1125 }
1126
1127 O << ";\n";
1128 return;
1129 }
1130
1131 if (GVar->hasPrivateLinkage()) {
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001132 if (strncmp(GVar->getName().data(), "unrollpragma", 12) == 0)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001133 return;
1134
1135 // FIXME - need better way (e.g. Metadata) to avoid generating this global
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001136 if (strncmp(GVar->getName().data(), "filename", 8) == 0)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001137 return;
1138 if (GVar->use_empty())
1139 return;
1140 }
1141
Craig Topper062a2ba2014-04-25 05:30:21 +00001142 const Function *demotedFunc = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001143 if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) {
Yaron Keren075759a2015-03-30 15:42:36 +00001144 O << "// " << GVar->getName() << " has been demoted\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001145 if (localDecls.find(demotedFunc) != localDecls.end())
1146 localDecls[demotedFunc].push_back(GVar);
1147 else {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001148 std::vector<const GlobalVariable *> temp;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001149 temp.push_back(GVar);
1150 localDecls[demotedFunc] = temp;
1151 }
1152 return;
1153 }
1154
1155 O << ".";
1156 emitPTXAddressSpace(PTy->getAddressSpace(), O);
Justin Holewinski773ca402014-06-27 18:35:58 +00001157
1158 if (isManaged(*GVar)) {
1159 O << " .attribute(.managed)";
1160 }
1161
Justin Holewinskiae556d32012-05-04 20:18:50 +00001162 if (GVar->getAlignment() == 0)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001163 O << " .align " << (int)DL.getPrefTypeAlignment(ETy);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001164 else
1165 O << " .align " << GVar->getAlignment();
1166
Justin Lebar1cf6bf42017-01-18 00:29:53 +00001167 if (ETy->isFloatingPointTy() || ETy->isPointerTy() ||
1168 (ETy->isIntegerTy() && ETy->getScalarSizeInBits() <= 64)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001169 O << " .";
Justin Holewinski700b6fa2013-05-20 12:13:28 +00001170 // Special case: ABI requires that we use .u8 for predicates
1171 if (ETy->isIntegerTy(1))
1172 O << "u8";
1173 else
1174 O << getPTXFundamentalTypeStr(ETy, false);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001175 O << " ";
Matt Arsenault8b643552015-06-09 00:31:39 +00001176 getSymbol(GVar)->print(O, MAI);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001177
1178 // Ptx allows variable initilization only for constant and global state
1179 // spaces.
Justin Holewinski549c7732014-06-27 18:36:01 +00001180 if (GVar->hasInitializer()) {
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001181 if ((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) ||
1182 (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) {
Justin Holewinski549c7732014-06-27 18:36:01 +00001183 const Constant *Initializer = GVar->getInitializer();
Jingyue Wu312fd022015-04-24 02:57:30 +00001184 // 'undef' is treated as there is no value specified.
Justin Holewinski549c7732014-06-27 18:36:01 +00001185 if (!Initializer->isNullValue() && !isa<UndefValue>(Initializer)) {
1186 O << " = ";
1187 printScalarConstant(Initializer, O);
1188 }
1189 } else {
Jingyue Wufcec0982015-08-22 05:40:26 +00001190 // The frontend adds zero-initializer to device and constant variables
1191 // that don't have an initial value, and UndefValue to shared
1192 // variables, so skip warning for this case.
1193 if (!GVar->getInitializer()->isNullValue() &&
1194 !isa<UndefValue>(GVar->getInitializer())) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001195 report_fatal_error("initial value of '" + GVar->getName() +
1196 "' is not allowed in addrspace(" +
1197 Twine(PTy->getAddressSpace()) + ")");
Justin Holewinski549c7732014-06-27 18:36:01 +00001198 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001199 }
1200 }
1201 } else {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001202 unsigned int ElementSize = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001203
1204 // Although PTX has direct support for struct type and array type and
1205 // LLVM IR is very similar to PTX, the LLVM CodeGen does not support for
1206 // targets that support these high level field accesses. Structs, arrays
1207 // and vectors are lowered into arrays of bytes.
1208 switch (ETy->getTypeID()) {
Justin Lebar1cf6bf42017-01-18 00:29:53 +00001209 case Type::IntegerTyID: // Integers larger than 64 bits
Justin Holewinskiae556d32012-05-04 20:18:50 +00001210 case Type::StructTyID:
1211 case Type::ArrayTyID:
1212 case Type::VectorTyID:
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001213 ElementSize = DL.getTypeStoreSize(ETy);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001214 // Ptx allows variable initilization only for constant and
1215 // global state spaces.
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001216 if (((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) ||
1217 (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) &&
Justin Holewinski0497ab12013-03-30 14:29:21 +00001218 GVar->hasInitializer()) {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001219 const Constant *Initializer = GVar->getInitializer();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001220 if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001221 AggBuffer aggBuffer(ElementSize, O, *this);
1222 bufferAggregateConstant(Initializer, &aggBuffer);
1223 if (aggBuffer.numSymbols) {
Eric Christopher6aad8b12015-02-19 00:08:14 +00001224 if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) {
Matt Arsenault8b643552015-06-09 00:31:39 +00001225 O << " .u64 ";
1226 getSymbol(GVar)->print(O, MAI);
1227 O << "[";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001228 O << ElementSize / 8;
1229 } else {
Matt Arsenault8b643552015-06-09 00:31:39 +00001230 O << " .u32 ";
1231 getSymbol(GVar)->print(O, MAI);
1232 O << "[";
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 {
Matt Arsenault8b643552015-06-09 00:31:39 +00001237 O << " .b8 ";
1238 getSymbol(GVar)->print(O, MAI);
1239 O << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001240 O << ElementSize;
1241 O << "]";
1242 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001243 O << " = {";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001244 aggBuffer.print();
1245 O << "}";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001246 } else {
Matt Arsenault8b643552015-06-09 00:31:39 +00001247 O << " .b8 ";
1248 getSymbol(GVar)->print(O, MAI);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001249 if (ElementSize) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001250 O << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001251 O << ElementSize;
1252 O << "]";
1253 }
1254 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001255 } else {
Matt Arsenault8b643552015-06-09 00:31:39 +00001256 O << " .b8 ";
1257 getSymbol(GVar)->print(O, MAI);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001258 if (ElementSize) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001259 O << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001260 O << ElementSize;
1261 O << "]";
1262 }
1263 }
1264 break;
1265 default:
Craig Topper2a30d782014-06-18 05:05:13 +00001266 llvm_unreachable("type not supported yet");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001267 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001268 }
1269 O << ";\n";
1270}
1271
1272void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) {
1273 if (localDecls.find(f) == localDecls.end())
1274 return;
1275
Justin Holewinski01f89f02013-05-20 12:13:32 +00001276 std::vector<const GlobalVariable *> &gvars = localDecls[f];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001277
Justin Holewinski0497ab12013-03-30 14:29:21 +00001278 for (unsigned i = 0, e = gvars.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001279 O << "\t// demoted variable\n\t";
1280 printModuleLevelGV(gvars[i], O, true);
1281 }
1282}
1283
1284void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace,
1285 raw_ostream &O) const {
1286 switch (AddressSpace) {
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001287 case ADDRESS_SPACE_LOCAL:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001288 O << "local";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001289 break;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001290 case ADDRESS_SPACE_GLOBAL:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001291 O << "global";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001292 break;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001293 case ADDRESS_SPACE_CONST:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001294 O << "const";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001295 break;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001296 case ADDRESS_SPACE_SHARED:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001297 O << "shared";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001298 break;
1299 default:
Justin Lebar321b4432018-02-08 00:50:04 +00001300 report_fatal_error("Bad address space found while emitting PTX: " +
1301 llvm::Twine(AddressSpace));
Justin Holewinski36a50992013-02-09 13:34:15 +00001302 break;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001303 }
1304}
1305
Justin Holewinski0497ab12013-03-30 14:29:21 +00001306std::string
Craig Toppere3dcce92015-08-01 22:20:21 +00001307NVPTXAsmPrinter::getPTXFundamentalTypeStr(Type *Ty, bool useB4PTR) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001308 switch (Ty->getTypeID()) {
1309 default:
1310 llvm_unreachable("unexpected type");
1311 break;
1312 case Type::IntegerTyID: {
1313 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth();
1314 if (NumBits == 1)
1315 return "pred";
1316 else if (NumBits <= 64) {
1317 std::string name = "u";
1318 return name + utostr(NumBits);
1319 } else {
1320 llvm_unreachable("Integer too large");
1321 break;
1322 }
1323 break;
1324 }
Artem Belevich64dc9be2017-01-13 20:56:17 +00001325 case Type::HalfTyID:
1326 // fp16 is stored as .b16 for compatibility with pre-sm_53 PTX assembly.
1327 return "b16";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001328 case Type::FloatTyID:
1329 return "f32";
1330 case Type::DoubleTyID:
1331 return "f64";
1332 case Type::PointerTyID:
Eric Christopher6aad8b12015-02-19 00:08:14 +00001333 if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit())
Justin Holewinski0497ab12013-03-30 14:29:21 +00001334 if (useB4PTR)
1335 return "b64";
1336 else
1337 return "u64";
1338 else if (useB4PTR)
1339 return "b32";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001340 else
Justin Holewinski0497ab12013-03-30 14:29:21 +00001341 return "u32";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001342 }
1343 llvm_unreachable("unexpected type");
Craig Topper062a2ba2014-04-25 05:30:21 +00001344 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001345}
1346
Justin Holewinski0497ab12013-03-30 14:29:21 +00001347void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001348 raw_ostream &O) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001349 const DataLayout &DL = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001350
1351 // GlobalVariables are always constant pointers themselves.
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00001352 Type *ETy = GVar->getValueType();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001353
1354 O << ".";
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00001355 emitPTXAddressSpace(GVar->getType()->getAddressSpace(), O);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001356 if (GVar->getAlignment() == 0)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001357 O << " .align " << (int)DL.getPrefTypeAlignment(ETy);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001358 else
1359 O << " .align " << GVar->getAlignment();
1360
Artem Belevichd7a73822017-07-20 21:16:03 +00001361 // Special case for i128
1362 if (ETy->isIntegerTy(128)) {
1363 O << " .b8 ";
1364 getSymbol(GVar)->print(O, MAI);
1365 O << "[16]";
1366 return;
1367 }
1368
Vedant Kumarb3091da2018-07-06 20:17:42 +00001369 if (ETy->isFloatingPointTy() || ETy->isIntOrPtrTy()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001370 O << " .";
1371 O << getPTXFundamentalTypeStr(ETy);
1372 O << " ";
Matt Arsenault8b643552015-06-09 00:31:39 +00001373 getSymbol(GVar)->print(O, MAI);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001374 return;
1375 }
1376
Justin Holewinski0497ab12013-03-30 14:29:21 +00001377 int64_t ElementSize = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001378
1379 // Although PTX has direct support for struct type and array type and LLVM IR
1380 // is very similar to PTX, the LLVM CodeGen does not support for targets that
1381 // support these high level field accesses. Structs and arrays are lowered
1382 // into arrays of bytes.
1383 switch (ETy->getTypeID()) {
1384 case Type::StructTyID:
1385 case Type::ArrayTyID:
1386 case Type::VectorTyID:
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001387 ElementSize = DL.getTypeStoreSize(ETy);
Matt Arsenault8b643552015-06-09 00:31:39 +00001388 O << " .b8 ";
1389 getSymbol(GVar)->print(O, MAI);
1390 O << "[";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001391 if (ElementSize) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001392 O << ElementSize;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001393 }
1394 O << "]";
1395 break;
1396 default:
Craig Topper2a30d782014-06-18 05:05:13 +00001397 llvm_unreachable("type not supported yet");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001398 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001399}
1400
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001401static unsigned int getOpenCLAlignment(const DataLayout &DL, Type *Ty) {
Rafael Espindola08013342013-12-07 19:34:20 +00001402 if (Ty->isSingleValueType())
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001403 return DL.getPrefTypeAlignment(Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001404
Craig Toppere3dcce92015-08-01 22:20:21 +00001405 auto *ATy = dyn_cast<ArrayType>(Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001406 if (ATy)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001407 return getOpenCLAlignment(DL, ATy->getElementType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001408
Craig Toppere3dcce92015-08-01 22:20:21 +00001409 auto *STy = dyn_cast<StructType>(Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001410 if (STy) {
1411 unsigned int alignStruct = 1;
1412 // Go through each element of the struct and find the
1413 // largest alignment.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001414 for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001415 Type *ETy = STy->getElementType(i);
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001416 unsigned int align = getOpenCLAlignment(DL, ETy);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001417 if (align > alignStruct)
1418 alignStruct = align;
1419 }
1420 return alignStruct;
1421 }
1422
Craig Toppere3dcce92015-08-01 22:20:21 +00001423 auto *FTy = dyn_cast<FunctionType>(Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001424 if (FTy)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001425 return DL.getPointerPrefAlignment();
1426 return DL.getPrefTypeAlignment(Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001427}
1428
1429void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I,
1430 int paramIndex, raw_ostream &O) {
Matt Arsenault8b643552015-06-09 00:31:39 +00001431 getSymbol(I->getParent())->print(O, MAI);
1432 O << "_param_" << paramIndex;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001433}
1434
Justin Holewinski0497ab12013-03-30 14:29:21 +00001435void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001436 const DataLayout &DL = getDataLayout();
Reid Klecknerb5180542017-03-21 16:57:19 +00001437 const AttributeList &PAL = F->getAttributes();
Artem Belevichf802b932018-12-12 18:31:04 +00001438 const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F);
1439 const TargetLowering *TLI = STI.getTargetLowering();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001440 Function::const_arg_iterator I, E;
1441 unsigned paramIndex = 0;
1442 bool first = true;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001443 bool isKernelFunc = isKernelFunction(*F);
Artem Belevichf802b932018-12-12 18:31:04 +00001444 bool isABI = (STI.getSmVersion() >= 20);
1445 bool hasImageHandles = STI.hasImageHandles();
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001446 MVT thePointerTy = TLI->getPointerTy(DL);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001447
Justin Lebar2a161f92016-01-23 21:12:17 +00001448 if (F->arg_empty()) {
1449 O << "()\n";
1450 return;
1451 }
1452
Justin Holewinskiae556d32012-05-04 20:18:50 +00001453 O << "(\n";
1454
1455 for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, paramIndex++) {
Justin Holewinskie9884092013-03-24 21:17:47 +00001456 Type *Ty = I->getType();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001457
1458 if (!first)
1459 O << ",\n";
1460
1461 first = false;
1462
1463 // Handle image/sampler parameters
Justin Holewinski30d56a72014-04-09 15:39:15 +00001464 if (isKernelFunction(*F)) {
1465 if (isSampler(*I) || isImage(*I)) {
1466 if (isImage(*I)) {
1467 std::string sname = I->getName();
1468 if (isImageWriteOnly(*I) || isImageReadWrite(*I)) {
Artem Belevichf802b932018-12-12 18:31:04 +00001469 if (hasImageHandles)
Justin Holewinski30d56a72014-04-09 15:39:15 +00001470 O << "\t.param .u64 .ptr .surfref ";
1471 else
1472 O << "\t.param .surfref ";
Matt Arsenault8b643552015-06-09 00:31:39 +00001473 CurrentFnSym->print(O, MAI);
1474 O << "_param_" << paramIndex;
Justin Holewinski30d56a72014-04-09 15:39:15 +00001475 }
1476 else { // Default image is read_only
Artem Belevichf802b932018-12-12 18:31:04 +00001477 if (hasImageHandles)
Justin Holewinski30d56a72014-04-09 15:39:15 +00001478 O << "\t.param .u64 .ptr .texref ";
1479 else
1480 O << "\t.param .texref ";
Matt Arsenault8b643552015-06-09 00:31:39 +00001481 CurrentFnSym->print(O, MAI);
1482 O << "_param_" << paramIndex;
Justin Holewinski30d56a72014-04-09 15:39:15 +00001483 }
1484 } else {
Artem Belevichf802b932018-12-12 18:31:04 +00001485 if (hasImageHandles)
Justin Holewinski30d56a72014-04-09 15:39:15 +00001486 O << "\t.param .u64 .ptr .samplerref ";
1487 else
1488 O << "\t.param .samplerref ";
Matt Arsenault8b643552015-06-09 00:31:39 +00001489 CurrentFnSym->print(O, MAI);
1490 O << "_param_" << paramIndex;
Justin Holewinski30d56a72014-04-09 15:39:15 +00001491 }
1492 continue;
1493 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001494 }
1495
Reid Klecknerf021fab2017-04-13 23:12:13 +00001496 if (!PAL.hasParamAttribute(paramIndex, Attribute::ByVal)) {
Artem Belevichd7a73822017-07-20 21:16:03 +00001497 if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) {
Gautam Chakrabarti2c283402014-01-28 18:35:29 +00001498 // Just print .param .align <a> .b8 .param[size];
Justin Holewinskie9884092013-03-24 21:17:47 +00001499 // <a> = PAL.getparamalignment
1500 // size = typeallocsize of element type
Reid Kleckner859f8b52017-04-28 20:34:27 +00001501 unsigned align = PAL.getParamAlignment(paramIndex);
Justin Holewinskie9884092013-03-24 21:17:47 +00001502 if (align == 0)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001503 align = DL.getABITypeAlignment(Ty);
Justin Holewinskie9884092013-03-24 21:17:47 +00001504
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001505 unsigned sz = DL.getTypeAllocSize(Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001506 O << "\t.param .align " << align << " .b8 ";
Justin Holewinskie9884092013-03-24 21:17:47 +00001507 printParamName(I, paramIndex, O);
1508 O << "[" << sz << "]";
1509
1510 continue;
1511 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001512 // Just a scalar
Craig Toppere3dcce92015-08-01 22:20:21 +00001513 auto *PTy = dyn_cast<PointerType>(Ty);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001514 if (isKernelFunc) {
1515 if (PTy) {
1516 // Special handling for pointer arguments to kernel
1517 O << "\t.param .u" << thePointerTy.getSizeInBits() << " ";
1518
Eric Christopherbeffc4e2015-02-19 00:08:23 +00001519 if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() !=
1520 NVPTX::CUDA) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001521 Type *ETy = PTy->getElementType();
1522 int addrSpace = PTy->getAddressSpace();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001523 switch (addrSpace) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001524 default:
1525 O << ".ptr ";
1526 break;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001527 case ADDRESS_SPACE_CONST:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001528 O << ".ptr .const ";
1529 break;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001530 case ADDRESS_SPACE_SHARED:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001531 O << ".ptr .shared ";
1532 break;
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00001533 case ADDRESS_SPACE_GLOBAL:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001534 O << ".ptr .global ";
1535 break;
1536 }
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001537 O << ".align " << (int)getOpenCLAlignment(DL, ETy) << " ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001538 }
1539 printParamName(I, paramIndex, O);
1540 continue;
1541 }
1542
1543 // non-pointer scalar to kernel func
Justin Holewinski700b6fa2013-05-20 12:13:28 +00001544 O << "\t.param .";
1545 // Special case: predicate operands become .u8 types
1546 if (Ty->isIntegerTy(1))
1547 O << "u8";
1548 else
1549 O << getPTXFundamentalTypeStr(Ty);
1550 O << " ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001551 printParamName(I, paramIndex, O);
1552 continue;
1553 }
1554 // Non-kernel function, just print .param .b<size> for ABI
Alp Tokerf907b892013-12-05 05:44:44 +00001555 // and .reg .b<size> for non-ABI
Justin Holewinskiae556d32012-05-04 20:18:50 +00001556 unsigned sz = 0;
1557 if (isa<IntegerType>(Ty)) {
1558 sz = cast<IntegerType>(Ty)->getBitWidth();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001559 if (sz < 32)
1560 sz = 32;
1561 } else if (isa<PointerType>(Ty))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001562 sz = thePointerTy.getSizeInBits();
Artem Belevich64dc9be2017-01-13 20:56:17 +00001563 else if (Ty->isHalfTy())
1564 // PTX ABI requires all scalar parameters to be at least 32
1565 // bits in size. fp16 normally uses .b16 as its storage type
1566 // in PTX, so its size must be adjusted here, too.
1567 sz = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001568 else
1569 sz = Ty->getPrimitiveSizeInBits();
1570 if (isABI)
1571 O << "\t.param .b" << sz << " ";
1572 else
1573 O << "\t.reg .b" << sz << " ";
1574 printParamName(I, paramIndex, O);
1575 continue;
1576 }
1577
1578 // param has byVal attribute. So should be a pointer
Craig Toppere3dcce92015-08-01 22:20:21 +00001579 auto *PTy = dyn_cast<PointerType>(Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001580 assert(PTy && "Param with byval attribute should be a pointer type");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001581 Type *ETy = PTy->getElementType();
1582
1583 if (isABI || isKernelFunc) {
Gautam Chakrabarti2c283402014-01-28 18:35:29 +00001584 // Just print .param .align <a> .b8 .param[size];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001585 // <a> = PAL.getparamalignment
1586 // size = typeallocsize of element type
Reid Kleckner859f8b52017-04-28 20:34:27 +00001587 unsigned align = PAL.getParamAlignment(paramIndex);
Justin Holewinski2dc9d072012-11-09 23:50:24 +00001588 if (align == 0)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001589 align = DL.getABITypeAlignment(ETy);
Artem Belevich052b1ed2016-07-18 19:54:56 +00001590 // Work around a bug in ptxas. When PTX code takes address of
1591 // byval parameter with alignment < 4, ptxas generates code to
1592 // spill argument into memory. Alas on sm_50+ ptxas generates
1593 // SASS code that fails with misaligned access. To work around
1594 // the problem, make sure that we align byval parameters by at
1595 // least 4. Matching change must be made in LowerCall() where we
1596 // prepare parameters for the call.
1597 //
1598 // TODO: this will need to be undone when we get to support multi-TU
1599 // device-side compilation as it breaks ABI compatibility with nvcc.
1600 // Hopefully ptxas bug is fixed by then.
1601 if (!isKernelFunc && align < 4)
1602 align = 4;
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001603 unsigned sz = DL.getTypeAllocSize(ETy);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001604 O << "\t.param .align " << align << " .b8 ";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001605 printParamName(I, paramIndex, O);
1606 O << "[" << sz << "]";
1607 continue;
1608 } else {
1609 // Split the ETy into constituent parts and
1610 // print .param .b<size> <name> for each part.
1611 // Further, if a part is vector, print the above for
1612 // each vector element.
1613 SmallVector<EVT, 16> vtparts;
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001614 ComputeValueVTs(*TLI, DL, ETy, vtparts);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001615 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001616 unsigned elems = 1;
1617 EVT elemtype = vtparts[i];
1618 if (vtparts[i].isVector()) {
1619 elems = vtparts[i].getVectorNumElements();
1620 elemtype = vtparts[i].getVectorElementType();
1621 }
1622
Justin Holewinski0497ab12013-03-30 14:29:21 +00001623 for (unsigned j = 0, je = elems; j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001624 unsigned sz = elemtype.getSizeInBits();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001625 if (elemtype.isInteger() && (sz < 32))
1626 sz = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001627 O << "\t.reg .b" << sz << " ";
1628 printParamName(I, paramIndex, O);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001629 if (j < je - 1)
1630 O << ",\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001631 ++paramIndex;
1632 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001633 if (i < e - 1)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001634 O << ",\n";
1635 }
1636 --paramIndex;
1637 continue;
1638 }
1639 }
1640
1641 O << "\n)\n";
1642}
1643
1644void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF,
1645 raw_ostream &O) {
Matthias Braunf1caa282017-12-15 22:22:58 +00001646 const Function &F = MF.getFunction();
1647 emitFunctionParamList(&F, O);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001648}
1649
Justin Holewinski0497ab12013-03-30 14:29:21 +00001650void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters(
1651 const MachineFunction &MF) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001652 SmallString<128> Str;
1653 raw_svector_ostream O(Str);
1654
1655 // Map the global virtual register number to a register class specific
1656 // virtual register number starting from 1 with that class.
Eric Christopherfc6de422014-08-05 02:39:49 +00001657 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001658 //unsigned numRegClasses = TRI->getNumRegClasses();
1659
1660 // Emit the Fake Stack Object
Matthias Braun941a7052016-07-28 18:40:00 +00001661 const MachineFrameInfo &MFI = MF.getFrameInfo();
1662 int NumBytes = (int) MFI.getStackSize();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001663 if (NumBytes) {
Matthias Braun941a7052016-07-28 18:40:00 +00001664 O << "\t.local .align " << MFI.getMaxAlignment() << " .b8 \t" << DEPOTNAME
Justin Holewinski0497ab12013-03-30 14:29:21 +00001665 << getFunctionNumber() << "[" << NumBytes << "];\n";
Eric Christopher02389e32015-02-19 00:08:27 +00001666 if (static_cast<const NVPTXTargetMachine &>(MF.getTarget()).is64Bit()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001667 O << "\t.reg .b64 \t%SP;\n";
1668 O << "\t.reg .b64 \t%SPL;\n";
Justin Holewinski0497ab12013-03-30 14:29:21 +00001669 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001670 O << "\t.reg .b32 \t%SP;\n";
1671 O << "\t.reg .b32 \t%SPL;\n";
1672 }
1673 }
1674
1675 // Go through all virtual registers to establish the mapping between the
1676 // global virtual
1677 // register number and the per class virtual register number.
1678 // We use the per class virtual register number in the ptx output.
1679 unsigned int numVRs = MRI->getNumVirtRegs();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001680 for (unsigned i = 0; i < numVRs; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001681 unsigned int vr = TRI->index2VirtReg(i);
1682 const TargetRegisterClass *RC = MRI->getRegClass(vr);
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001683 DenseMap<unsigned, unsigned> &regmap = VRegMapping[RC];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001684 int n = regmap.size();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001685 regmap.insert(std::make_pair(vr, n + 1));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001686 }
1687
1688 // Emit register declarations
1689 // @TODO: Extract out the real register usage
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001690 // O << "\t.reg .pred %p<" << NVPTXNumRegisters << ">;\n";
1691 // O << "\t.reg .s16 %rc<" << NVPTXNumRegisters << ">;\n";
1692 // O << "\t.reg .s16 %rs<" << NVPTXNumRegisters << ">;\n";
1693 // O << "\t.reg .s32 %r<" << NVPTXNumRegisters << ">;\n";
Justin Holewinski3e037d92014-07-16 16:26:58 +00001694 // O << "\t.reg .s64 %rd<" << NVPTXNumRegisters << ">;\n";
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001695 // O << "\t.reg .f32 %f<" << NVPTXNumRegisters << ">;\n";
Justin Holewinski3e037d92014-07-16 16:26:58 +00001696 // O << "\t.reg .f64 %fd<" << NVPTXNumRegisters << ">;\n";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001697
1698 // Emit declaration of the virtual registers or 'physical' registers for
1699 // each register class
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001700 for (unsigned i=0; i< TRI->getNumRegClasses(); i++) {
1701 const TargetRegisterClass *RC = TRI->getRegClass(i);
1702 DenseMap<unsigned, unsigned> &regmap = VRegMapping[RC];
1703 std::string rcname = getNVPTXRegClassName(RC);
1704 std::string rcStr = getNVPTXRegClassStr(RC);
1705 int n = regmap.size();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001706
Justin Holewinskidbb3b2f2013-05-31 12:14:49 +00001707 // Only declare those registers that may be used.
1708 if (n) {
1709 O << "\t.reg " << rcname << " \t" << rcStr << "<" << (n+1)
1710 << ">;\n";
1711 }
1712 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001713
Lang Hames9ff69c82015-04-24 19:11:51 +00001714 OutStreamer->EmitRawText(O.str());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001715}
1716
Justin Holewinskiae556d32012-05-04 20:18:50 +00001717void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001718 APFloat APF = APFloat(Fp->getValueAPF()); // make a copy
Justin Holewinskiae556d32012-05-04 20:18:50 +00001719 bool ignored;
1720 unsigned int numHex;
1721 const char *lead;
1722
Justin Holewinski0497ab12013-03-30 14:29:21 +00001723 if (Fp->getType()->getTypeID() == Type::FloatTyID) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001724 numHex = 8;
1725 lead = "0f";
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001726 APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &ignored);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001727 } else if (Fp->getType()->getTypeID() == Type::DoubleTyID) {
1728 numHex = 16;
1729 lead = "0d";
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001730 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &ignored);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001731 } else
1732 llvm_unreachable("unsupported fp type");
1733
1734 APInt API = APF.bitcastToAPInt();
Benjamin Kramer3a13ed62017-12-28 16:58:54 +00001735 O << lead << format_hex_no_prefix(API.getZExtValue(), numHex, /*Upper=*/true);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001736}
1737
Justin Holewinski01f89f02013-05-20 12:13:32 +00001738void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
1739 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001740 O << CI->getValue();
1741 return;
1742 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001743 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001744 printFPConstant(CFP, O);
1745 return;
1746 }
1747 if (isa<ConstantPointerNull>(CPV)) {
1748 O << "0";
1749 return;
1750 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001751 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
Justin Holewinski9d852a82014-04-09 15:39:11 +00001752 bool IsNonGenericPointer = false;
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00001753 if (GVar->getType()->getAddressSpace() != 0) {
Justin Holewinski9d852a82014-04-09 15:39:11 +00001754 IsNonGenericPointer = true;
1755 }
1756 if (EmitGeneric && !isa<Function>(CPV) && !IsNonGenericPointer) {
1757 O << "generic(";
Matt Arsenault8b643552015-06-09 00:31:39 +00001758 getSymbol(GVar)->print(O, MAI);
Justin Holewinski9d852a82014-04-09 15:39:11 +00001759 O << ")";
1760 } else {
Matt Arsenault8b643552015-06-09 00:31:39 +00001761 getSymbol(GVar)->print(O, MAI);
Justin Holewinski9d852a82014-04-09 15:39:11 +00001762 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001763 return;
1764 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001765 if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1766 const Value *v = Cexpr->stripPointerCasts();
Justin Holewinski9d852a82014-04-09 15:39:11 +00001767 PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType());
1768 bool IsNonGenericPointer = false;
1769 if (PTy && PTy->getAddressSpace() != 0) {
1770 IsNonGenericPointer = true;
1771 }
Justin Holewinski01f89f02013-05-20 12:13:32 +00001772 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
Justin Holewinski9d852a82014-04-09 15:39:11 +00001773 if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
1774 O << "generic(";
Matt Arsenault8b643552015-06-09 00:31:39 +00001775 getSymbol(GVar)->print(O, MAI);
Justin Holewinski9d852a82014-04-09 15:39:11 +00001776 O << ")";
1777 } else {
Matt Arsenault8b643552015-06-09 00:31:39 +00001778 getSymbol(GVar)->print(O, MAI);
Justin Holewinski9d852a82014-04-09 15:39:11 +00001779 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001780 return;
1781 } else {
Matt Arsenault8b643552015-06-09 00:31:39 +00001782 lowerConstant(CPV)->print(O, MAI);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001783 return;
1784 }
1785 }
1786 llvm_unreachable("Not scalar type found in printScalarConstant()");
1787}
1788
Samuel Antaocd501352015-06-09 16:29:34 +00001789// These utility functions assure we get the right sequence of bytes for a given
1790// type even for big-endian machines
1791template <typename T> static void ConvertIntToBytes(unsigned char *p, T val) {
1792 int64_t vp = (int64_t)val;
1793 for (unsigned i = 0; i < sizeof(T); ++i) {
1794 p[i] = (unsigned char)vp;
1795 vp >>= 8;
1796 }
1797}
1798static void ConvertFloatToBytes(unsigned char *p, float val) {
1799 int32_t *vp = (int32_t *)&val;
1800 for (unsigned i = 0; i < sizeof(int32_t); ++i) {
1801 p[i] = (unsigned char)*vp;
1802 *vp >>= 8;
1803 }
1804}
1805static void ConvertDoubleToBytes(unsigned char *p, double val) {
1806 int64_t *vp = (int64_t *)&val;
1807 for (unsigned i = 0; i < sizeof(int64_t); ++i) {
1808 p[i] = (unsigned char)*vp;
1809 *vp >>= 8;
1810 }
1811}
1812
Justin Holewinski01f89f02013-05-20 12:13:32 +00001813void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001814 AggBuffer *aggBuffer) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001815 const DataLayout &DL = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001816
1817 if (isa<UndefValue>(CPV) || CPV->isNullValue()) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001818 int s = DL.getTypeAllocSize(CPV->getType());
Justin Holewinski0497ab12013-03-30 14:29:21 +00001819 if (s < Bytes)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001820 s = Bytes;
1821 aggBuffer->addZeros(s);
1822 return;
1823 }
1824
Samuel Antaocd501352015-06-09 16:29:34 +00001825 unsigned char ptr[8];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001826 switch (CPV->getType()->getTypeID()) {
1827
1828 case Type::IntegerTyID: {
Craig Toppere3dcce92015-08-01 22:20:21 +00001829 Type *ETy = CPV->getType();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001830 if (ETy == Type::getInt8Ty(CPV->getContext())) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001831 unsigned char c = (unsigned char)cast<ConstantInt>(CPV)->getZExtValue();
Samuel Antaocd501352015-06-09 16:29:34 +00001832 ConvertIntToBytes<>(ptr, c);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001833 aggBuffer->addBytes(ptr, 1, Bytes);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001834 } else if (ETy == Type::getInt16Ty(CPV->getContext())) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +00001835 short int16 = (short)cast<ConstantInt>(CPV)->getZExtValue();
Samuel Antaocd501352015-06-09 16:29:34 +00001836 ConvertIntToBytes<>(ptr, int16);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001837 aggBuffer->addBytes(ptr, 2, Bytes);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001838 } else if (ETy == Type::getInt32Ty(CPV->getContext())) {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001839 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001840 int int32 = (int)(constInt->getZExtValue());
Samuel Antaocd501352015-06-09 16:29:34 +00001841 ConvertIntToBytes<>(ptr, int32);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001842 aggBuffer->addBytes(ptr, 4, Bytes);
1843 break;
David Majnemerd536f232016-07-29 03:27:26 +00001844 } else if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1845 if (const auto *constInt = dyn_cast_or_null<ConstantInt>(
1846 ConstantFoldConstant(Cexpr, DL))) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001847 int int32 = (int)(constInt->getZExtValue());
Samuel Antaocd501352015-06-09 16:29:34 +00001848 ConvertIntToBytes<>(ptr, int32);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001849 aggBuffer->addBytes(ptr, 4, Bytes);
1850 break;
1851 }
1852 if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1853 Value *v = Cexpr->getOperand(0)->stripPointerCasts();
Jingyue Wu312fd022015-04-24 02:57:30 +00001854 aggBuffer->addSymbol(v, Cexpr->getOperand(0));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001855 aggBuffer->addZeros(4);
1856 break;
1857 }
1858 }
Craig Topperbdf39a42012-05-24 07:02:50 +00001859 llvm_unreachable("unsupported integer const type");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001860 } else if (ETy == Type::getInt64Ty(CPV->getContext())) {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001861 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001862 long long int64 = (long long)(constInt->getZExtValue());
Samuel Antaocd501352015-06-09 16:29:34 +00001863 ConvertIntToBytes<>(ptr, int64);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001864 aggBuffer->addBytes(ptr, 8, Bytes);
1865 break;
Justin Holewinski01f89f02013-05-20 12:13:32 +00001866 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
David Majnemerd536f232016-07-29 03:27:26 +00001867 if (const auto *constInt = dyn_cast_or_null<ConstantInt>(
1868 ConstantFoldConstant(Cexpr, DL))) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001869 long long int64 = (long long)(constInt->getZExtValue());
Samuel Antaocd501352015-06-09 16:29:34 +00001870 ConvertIntToBytes<>(ptr, int64);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001871 aggBuffer->addBytes(ptr, 8, Bytes);
1872 break;
1873 }
1874 if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1875 Value *v = Cexpr->getOperand(0)->stripPointerCasts();
Jingyue Wu312fd022015-04-24 02:57:30 +00001876 aggBuffer->addSymbol(v, Cexpr->getOperand(0));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001877 aggBuffer->addZeros(8);
1878 break;
1879 }
1880 }
1881 llvm_unreachable("unsupported integer const type");
Craig Topperbdf39a42012-05-24 07:02:50 +00001882 } else
Justin Holewinskiae556d32012-05-04 20:18:50 +00001883 llvm_unreachable("unsupported integer const type");
1884 break;
1885 }
Artem Belevichf256dec2018-04-06 22:25:08 +00001886 case Type::HalfTyID:
Justin Holewinskiae556d32012-05-04 20:18:50 +00001887 case Type::FloatTyID:
1888 case Type::DoubleTyID: {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001889 const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV);
Craig Toppere3dcce92015-08-01 22:20:21 +00001890 Type *Ty = CFP->getType();
Artem Belevichf256dec2018-04-06 22:25:08 +00001891 if (Ty == Type::getHalfTy(CPV->getContext())) {
1892 APInt API = CFP->getValueAPF().bitcastToAPInt();
1893 uint16_t float16 = API.getLoBits(16).getZExtValue();
1894 ConvertIntToBytes<>(ptr, float16);
1895 aggBuffer->addBytes(ptr, 2, Bytes);
1896 } else if (Ty == Type::getFloatTy(CPV->getContext())) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001897 float float32 = (float) CFP->getValueAPF().convertToFloat();
Samuel Antaocd501352015-06-09 16:29:34 +00001898 ConvertFloatToBytes(ptr, float32);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001899 aggBuffer->addBytes(ptr, 4, Bytes);
1900 } else if (Ty == Type::getDoubleTy(CPV->getContext())) {
1901 double float64 = CFP->getValueAPF().convertToDouble();
Samuel Antaocd501352015-06-09 16:29:34 +00001902 ConvertDoubleToBytes(ptr, float64);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001903 aggBuffer->addBytes(ptr, 8, Bytes);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001904 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001905 llvm_unreachable("unsupported fp const type");
1906 }
1907 break;
1908 }
1909 case Type::PointerTyID: {
Justin Holewinski01f89f02013-05-20 12:13:32 +00001910 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
Jingyue Wu312fd022015-04-24 02:57:30 +00001911 aggBuffer->addSymbol(GVar, GVar);
Justin Holewinski01f89f02013-05-20 12:13:32 +00001912 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1913 const Value *v = Cexpr->stripPointerCasts();
Jingyue Wu312fd022015-04-24 02:57:30 +00001914 aggBuffer->addSymbol(v, Cexpr);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001915 }
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001916 unsigned int s = DL.getTypeAllocSize(CPV->getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001917 aggBuffer->addZeros(s);
1918 break;
1919 }
1920
1921 case Type::ArrayTyID:
1922 case Type::VectorTyID:
1923 case Type::StructTyID: {
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00001924 if (isa<ConstantAggregate>(CPV) || isa<ConstantDataSequential>(CPV)) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001925 int ElementSize = DL.getTypeAllocSize(CPV->getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001926 bufferAggregateConstant(CPV, aggBuffer);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001927 if (Bytes > ElementSize)
1928 aggBuffer->addZeros(Bytes - ElementSize);
1929 } else if (isa<ConstantAggregateZero>(CPV))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001930 aggBuffer->addZeros(Bytes);
1931 else
1932 llvm_unreachable("Unexpected Constant type");
1933 break;
1934 }
1935
1936 default:
1937 llvm_unreachable("unsupported type");
1938 }
1939}
1940
Justin Holewinski01f89f02013-05-20 12:13:32 +00001941void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV,
Justin Holewinskiae556d32012-05-04 20:18:50 +00001942 AggBuffer *aggBuffer) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001943 const DataLayout &DL = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001944 int Bytes;
1945
Justin Lebar1cf6bf42017-01-18 00:29:53 +00001946 // Integers of arbitrary width
1947 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) {
1948 APInt Val = CI->getValue();
1949 for (unsigned I = 0, E = DL.getTypeAllocSize(CPV->getType()); I < E; ++I) {
1950 uint8_t Byte = Val.getLoBits(8).getZExtValue();
1951 aggBuffer->addBytes(&Byte, 1, 1);
Craig Topperfc947bc2017-04-18 17:14:21 +00001952 Val.lshrInPlace(8);
Justin Lebar1cf6bf42017-01-18 00:29:53 +00001953 }
1954 return;
1955 }
1956
Justin Holewinskiae556d32012-05-04 20:18:50 +00001957 // Old constants
1958 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) {
1959 if (CPV->getNumOperands())
1960 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i)
1961 bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer);
1962 return;
1963 }
1964
1965 if (const ConstantDataSequential *CDS =
Justin Holewinski0497ab12013-03-30 14:29:21 +00001966 dyn_cast<ConstantDataSequential>(CPV)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001967 if (CDS->getNumElements())
1968 for (unsigned i = 0; i < CDS->getNumElements(); ++i)
1969 bufferLEByte(cast<Constant>(CDS->getElementAsConstant(i)), 0,
1970 aggBuffer);
1971 return;
1972 }
1973
Justin Holewinskiae556d32012-05-04 20:18:50 +00001974 if (isa<ConstantStruct>(CPV)) {
1975 if (CPV->getNumOperands()) {
1976 StructType *ST = cast<StructType>(CPV->getType());
1977 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001978 if (i == (e - 1))
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001979 Bytes = DL.getStructLayout(ST)->getElementOffset(0) +
1980 DL.getTypeAllocSize(ST) -
1981 DL.getStructLayout(ST)->getElementOffset(i);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001982 else
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001983 Bytes = DL.getStructLayout(ST)->getElementOffset(i + 1) -
1984 DL.getStructLayout(ST)->getElementOffset(i);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001985 bufferLEByte(cast<Constant>(CPV->getOperand(i)), Bytes, aggBuffer);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001986 }
1987 }
1988 return;
1989 }
Craig Topperbdf39a42012-05-24 07:02:50 +00001990 llvm_unreachable("unsupported constant type in printAggregateConstant()");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001991}
1992
Justin Holewinski3d2a9762015-04-28 17:18:30 +00001993/// lowerConstantForGV - Return an MCExpr for the given Constant. This is mostly
1994/// a copy from AsmPrinter::lowerConstant, except customized to only handle
1995/// expressions that are representable in PTX and create
1996/// NVPTXGenericMCSymbolRefExpr nodes for addrspacecast instructions.
1997const MCExpr *
1998NVPTXAsmPrinter::lowerConstantForGV(const Constant *CV, bool ProcessingGeneric) {
1999 MCContext &Ctx = OutContext;
2000
2001 if (CV->isNullValue() || isa<UndefValue>(CV))
Jim Grosbach13760bd2015-05-30 01:25:56 +00002002 return MCConstantExpr::create(0, Ctx);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002003
2004 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
Jim Grosbach13760bd2015-05-30 01:25:56 +00002005 return MCConstantExpr::create(CI->getZExtValue(), Ctx);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002006
2007 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
2008 const MCSymbolRefExpr *Expr =
Jim Grosbach13760bd2015-05-30 01:25:56 +00002009 MCSymbolRefExpr::create(getSymbol(GV), Ctx);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002010 if (ProcessingGeneric) {
Jim Grosbach13760bd2015-05-30 01:25:56 +00002011 return NVPTXGenericMCSymbolRefExpr::create(Expr, Ctx);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002012 } else {
2013 return Expr;
2014 }
2015 }
2016
2017 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
2018 if (!CE) {
2019 llvm_unreachable("Unknown constant value to lower!");
2020 }
2021
2022 switch (CE->getOpcode()) {
2023 default:
2024 // If the code isn't optimized, there may be outstanding folding
2025 // opportunities. Attempt to fold the expression using DataLayout as a
2026 // last resort before giving up.
David Majnemerd536f232016-07-29 03:27:26 +00002027 if (Constant *C = ConstantFoldConstant(CE, getDataLayout()))
2028 if (C && C != CE)
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002029 return lowerConstantForGV(C, ProcessingGeneric);
2030
2031 // Otherwise report the problem to the user.
2032 {
2033 std::string S;
2034 raw_string_ostream OS(S);
2035 OS << "Unsupported expression in static initializer: ";
2036 CE->printAsOperand(OS, /*PrintType=*/false,
Matthias Braunf1caa282017-12-15 22:22:58 +00002037 !MF ? nullptr : MF->getFunction().getParent());
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002038 report_fatal_error(OS.str());
2039 }
2040
2041 case Instruction::AddrSpaceCast: {
2042 // Strip the addrspacecast and pass along the operand
2043 PointerType *DstTy = cast<PointerType>(CE->getType());
2044 if (DstTy->getAddressSpace() == 0) {
2045 return lowerConstantForGV(cast<const Constant>(CE->getOperand(0)), true);
2046 }
2047 std::string S;
2048 raw_string_ostream OS(S);
2049 OS << "Unsupported expression in static initializer: ";
2050 CE->printAsOperand(OS, /*PrintType=*/ false,
Matthias Braunf1caa282017-12-15 22:22:58 +00002051 !MF ? nullptr : MF->getFunction().getParent());
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002052 report_fatal_error(OS.str());
2053 }
2054
2055 case Instruction::GetElementPtr: {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002056 const DataLayout &DL = getDataLayout();
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002057
2058 // Generate a symbolic expression for the byte address
2059 APInt OffsetAI(DL.getPointerTypeSizeInBits(CE->getType()), 0);
2060 cast<GEPOperator>(CE)->accumulateConstantOffset(DL, OffsetAI);
2061
2062 const MCExpr *Base = lowerConstantForGV(CE->getOperand(0),
2063 ProcessingGeneric);
2064 if (!OffsetAI)
2065 return Base;
2066
2067 int64_t Offset = OffsetAI.getSExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +00002068 return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx),
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002069 Ctx);
2070 }
2071
2072 case Instruction::Trunc:
2073 // We emit the value and depend on the assembler to truncate the generated
2074 // expression properly. This is important for differences between
2075 // blockaddress labels. Since the two labels are in the same function, it
2076 // is reasonable to treat their delta as a 32-bit value.
Justin Bognercd1d5aa2016-08-17 20:30:52 +00002077 LLVM_FALLTHROUGH;
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002078 case Instruction::BitCast:
2079 return lowerConstantForGV(CE->getOperand(0), ProcessingGeneric);
2080
2081 case Instruction::IntToPtr: {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002082 const DataLayout &DL = getDataLayout();
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002083
2084 // Handle casts to pointers by changing them into casts to the appropriate
2085 // integer type. This promotes constant folding and simplifies this code.
2086 Constant *Op = CE->getOperand(0);
2087 Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()),
2088 false/*ZExt*/);
2089 return lowerConstantForGV(Op, ProcessingGeneric);
2090 }
2091
2092 case Instruction::PtrToInt: {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002093 const DataLayout &DL = getDataLayout();
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002094
2095 // Support only foldable casts to/from pointers that can be eliminated by
2096 // changing the pointer to the appropriately sized integer type.
2097 Constant *Op = CE->getOperand(0);
2098 Type *Ty = CE->getType();
2099
2100 const MCExpr *OpExpr = lowerConstantForGV(Op, ProcessingGeneric);
2101
2102 // We can emit the pointer value into this slot if the slot is an
2103 // integer slot equal to the size of the pointer.
2104 if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType()))
2105 return OpExpr;
2106
2107 // Otherwise the pointer is smaller than the resultant integer, mask off
2108 // the high bits so we are sure to get a proper truncation if the input is
2109 // a constant expr.
2110 unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType());
Jim Grosbach13760bd2015-05-30 01:25:56 +00002111 const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx);
2112 return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002113 }
2114
2115 // The MC library also has a right-shift operator, but it isn't consistently
2116 // signed or unsigned between different targets.
2117 case Instruction::Add: {
2118 const MCExpr *LHS = lowerConstantForGV(CE->getOperand(0), ProcessingGeneric);
2119 const MCExpr *RHS = lowerConstantForGV(CE->getOperand(1), ProcessingGeneric);
2120 switch (CE->getOpcode()) {
2121 default: llvm_unreachable("Unknown binary operator constant cast expr");
Jim Grosbach13760bd2015-05-30 01:25:56 +00002122 case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002123 }
2124 }
2125 }
2126}
2127
2128// Copy of MCExpr::print customized for NVPTX
2129void NVPTXAsmPrinter::printMCExpr(const MCExpr &Expr, raw_ostream &OS) {
2130 switch (Expr.getKind()) {
2131 case MCExpr::Target:
Matt Arsenault8b643552015-06-09 00:31:39 +00002132 return cast<MCTargetExpr>(&Expr)->printImpl(OS, MAI);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002133 case MCExpr::Constant:
2134 OS << cast<MCConstantExpr>(Expr).getValue();
2135 return;
2136
2137 case MCExpr::SymbolRef: {
2138 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Expr);
2139 const MCSymbol &Sym = SRE.getSymbol();
Matt Arsenault8b643552015-06-09 00:31:39 +00002140 Sym.print(OS, MAI);
Justin Holewinski3d2a9762015-04-28 17:18:30 +00002141 return;
2142 }
2143
2144 case MCExpr::Unary: {
2145 const MCUnaryExpr &UE = cast<MCUnaryExpr>(Expr);
2146 switch (UE.getOpcode()) {
2147 case MCUnaryExpr::LNot: OS << '!'; break;
2148 case MCUnaryExpr::Minus: OS << '-'; break;
2149 case MCUnaryExpr::Not: OS << '~'; break;
2150 case MCUnaryExpr::Plus: OS << '+'; break;
2151 }
2152 printMCExpr(*UE.getSubExpr(), OS);
2153 return;
2154 }
2155
2156 case MCExpr::Binary: {
2157 const MCBinaryExpr &BE = cast<MCBinaryExpr>(Expr);
2158
2159 // Only print parens around the LHS if it is non-trivial.
2160 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS()) ||
2161 isa<NVPTXGenericMCSymbolRefExpr>(BE.getLHS())) {
2162 printMCExpr(*BE.getLHS(), OS);
2163 } else {
2164 OS << '(';
2165 printMCExpr(*BE.getLHS(), OS);
2166 OS<< ')';
2167 }
2168
2169 switch (BE.getOpcode()) {
2170 case MCBinaryExpr::Add:
2171 // Print "X-42" instead of "X+-42".
2172 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
2173 if (RHSC->getValue() < 0) {
2174 OS << RHSC->getValue();
2175 return;
2176 }
2177 }
2178
2179 OS << '+';
2180 break;
2181 default: llvm_unreachable("Unhandled binary operator");
2182 }
2183
2184 // Only print parens around the LHS if it is non-trivial.
2185 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
2186 printMCExpr(*BE.getRHS(), OS);
2187 } else {
2188 OS << '(';
2189 printMCExpr(*BE.getRHS(), OS);
2190 OS << ')';
2191 }
2192 return;
2193 }
2194 }
2195
2196 llvm_unreachable("Invalid expression kind!");
2197}
2198
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002199/// PrintAsmOperand - Print out an operand for an inline asm expression.
2200///
2201bool NVPTXAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
2202 unsigned AsmVariant,
2203 const char *ExtraCode, raw_ostream &O) {
2204 if (ExtraCode && ExtraCode[0]) {
2205 if (ExtraCode[1] != 0)
2206 return true; // Unknown modifier.
2207
2208 switch (ExtraCode[0]) {
2209 default:
2210 // See if this is a generic print operand
2211 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
2212 case 'r':
2213 break;
2214 }
2215 }
2216
2217 printOperand(MI, OpNo, O);
2218
2219 return false;
2220}
2221
2222bool NVPTXAsmPrinter::PrintAsmMemoryOperand(
2223 const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant,
2224 const char *ExtraCode, raw_ostream &O) {
2225 if (ExtraCode && ExtraCode[0])
2226 return true; // Unknown modifier
2227
2228 O << '[';
2229 printMemOperand(MI, OpNo, O);
2230 O << ']';
2231
2232 return false;
2233}
2234
2235void NVPTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
2236 raw_ostream &O, const char *Modifier) {
2237 const MachineOperand &MO = MI->getOperand(opNum);
2238 switch (MO.getType()) {
2239 case MachineOperand::MO_Register:
2240 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
2241 if (MO.getReg() == NVPTX::VRDepot)
2242 O << DEPOTNAME << getFunctionNumber();
2243 else
2244 O << NVPTXInstPrinter::getRegisterName(MO.getReg());
2245 } else {
Justin Holewinski660597d2013-10-11 12:39:36 +00002246 emitVirtualRegister(MO.getReg(), O);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002247 }
2248 return;
2249
2250 case MachineOperand::MO_Immediate:
2251 if (!Modifier)
2252 O << MO.getImm();
2253 else if (strstr(Modifier, "vec") == Modifier)
2254 printVecModifiedImmediate(MO, Modifier, O);
2255 else
2256 llvm_unreachable(
2257 "Don't know how to handle modifier on immediate operand");
2258 return;
2259
2260 case MachineOperand::MO_FPImmediate:
2261 printFPConstant(MO.getFPImm(), O);
2262 break;
2263
2264 case MachineOperand::MO_GlobalAddress:
Matt Arsenault8b643552015-06-09 00:31:39 +00002265 getSymbol(MO.getGlobal())->print(O, MAI);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002266 break;
2267
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002268 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +00002269 MO.getMBB()->getSymbol()->print(O, MAI);
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002270 return;
2271
2272 default:
2273 llvm_unreachable("Operand type not supported.");
2274 }
2275}
2276
2277void NVPTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
2278 raw_ostream &O, const char *Modifier) {
2279 printOperand(MI, opNum, O);
2280
Eugene Zelenkoc9f1f6b2017-01-09 22:16:51 +00002281 if (Modifier && strcmp(Modifier, "add") == 0) {
Justin Holewinskiaaa8b6e2013-08-24 01:17:23 +00002282 O << ", ";
2283 printOperand(MI, opNum + 1, O);
2284 } else {
2285 if (MI->getOperand(opNum + 1).isImm() &&
2286 MI->getOperand(opNum + 1).getImm() == 0)
2287 return; // don't print ',0' or '+0'
2288 O << "+";
2289 printOperand(MI, opNum + 1, O);
2290 }
2291}
2292
Justin Holewinskiae556d32012-05-04 20:18:50 +00002293// Force static initialization.
2294extern "C" void LLVMInitializeNVPTXAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00002295 RegisterAsmPrinter<NVPTXAsmPrinter> X(getTheNVPTXTarget32());
2296 RegisterAsmPrinter<NVPTXAsmPrinter> Y(getTheNVPTXTarget64());
Justin Holewinskiae556d32012-05-04 20:18:50 +00002297}