Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===-- NVPTXAsmPrinter.cpp - NVPTX LLVM assembly writer ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to NVPTX assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "NVPTXAsmPrinter.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "InstPrinter/NVPTXInstPrinter.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/NVPTXBaseInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/NVPTXMCAsmInfo.h" |
Alexey Bataev | 64ad0ad | 2018-12-06 16:02:09 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/NVPTXTargetStreamer.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 20 | #include "NVPTX.h" |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 21 | #include "NVPTXMCExpr.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 22 | #include "NVPTXMachineFunctionInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "NVPTXRegisterInfo.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 24 | #include "NVPTXSubtarget.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "NVPTXTargetMachine.h" |
| 26 | #include "NVPTXUtilities.h" |
| 27 | #include "cl_common_defines.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/APFloat.h" |
| 29 | #include "llvm/ADT/APInt.h" |
| 30 | #include "llvm/ADT/DenseMap.h" |
| 31 | #include "llvm/ADT/DenseSet.h" |
| 32 | #include "llvm/ADT/SmallString.h" |
| 33 | #include "llvm/ADT/SmallVector.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringRef.h" |
| 36 | #include "llvm/ADT/Triple.h" |
| 37 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/ConstantFolding.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/Analysis.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/MachineFunction.h" |
| 43 | #include "llvm/CodeGen/MachineInstr.h" |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 45 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 46 | #include "llvm/CodeGen/MachineOperand.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/TargetLowering.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 49 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Craig Topper | 2fa1436 | 2018-03-29 17:21:10 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/ValueTypes.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 51 | #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 Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 56 | #include "llvm/IR/DebugInfo.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 57 | #include "llvm/IR/DebugInfoMetadata.h" |
| 58 | #include "llvm/IR/DebugLoc.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 59 | #include "llvm/IR/DerivedTypes.h" |
| 60 | #include "llvm/IR/Function.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 61 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 62 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 63 | #include "llvm/IR/Instruction.h" |
| 64 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 65 | #include "llvm/IR/Module.h" |
| 66 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Type.h" |
| 68 | #include "llvm/IR/User.h" |
| 69 | #include "llvm/MC/MCExpr.h" |
Pete Cooper | 81902a3 | 2015-05-15 22:19:42 +0000 | [diff] [blame] | 70 | #include "llvm/MC/MCInst.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 71 | #include "llvm/MC/MCInstrDesc.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 72 | #include "llvm/MC/MCStreamer.h" |
| 73 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 74 | #include "llvm/Support/Casting.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 75 | #include "llvm/Support/CommandLine.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 76 | #include "llvm/Support/ErrorHandling.h" |
David Blaikie | 13e77db | 2018-03-23 23:58:25 +0000 | [diff] [blame] | 77 | #include "llvm/Support/MachineValueType.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 78 | #include "llvm/Support/Path.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 79 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 80 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 81 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 82 | #include "llvm/Target/TargetMachine.h" |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 83 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 84 | #include <cassert> |
| 85 | #include <cstdint> |
| 86 | #include <cstring> |
| 87 | #include <new> |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 88 | #include <string> |
| 89 | #include <utility> |
| 90 | #include <vector> |
| 91 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 92 | using namespace llvm; |
| 93 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 94 | #define DEPOTNAME "__local_depot" |
| 95 | |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 96 | /// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V |
| 97 | /// depends. |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 98 | static void |
| 99 | DiscoverDependentGlobals(const Value *V, |
| 100 | DenseSet<const GlobalVariable *> &Globals) { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 101 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 102 | Globals.insert(GV); |
| 103 | else { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 104 | if (const User *U = dyn_cast<User>(V)) { |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 105 | for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) { |
| 106 | DiscoverDependentGlobals(U->getOperand(i), Globals); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 111 | |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 112 | /// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable |
| 113 | /// instances to be emitted, but only after any dependents have been added |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 114 | /// first.s |
| 115 | static void |
| 116 | VisitGlobalVariableForEmission(const GlobalVariable *GV, |
| 117 | SmallVectorImpl<const GlobalVariable *> &Order, |
| 118 | DenseSet<const GlobalVariable *> &Visited, |
| 119 | DenseSet<const GlobalVariable *> &Visiting) { |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 120 | // Have we already visited this one? |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 121 | if (Visited.count(GV)) |
| 122 | return; |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 123 | |
| 124 | // Do we have a circular dependency? |
Benjamin Kramer | 2c99e41 | 2014-10-10 15:32:50 +0000 | [diff] [blame] | 125 | if (!Visiting.insert(GV).second) |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 126 | report_fatal_error("Circular dependency found in global variable set"); |
| 127 | |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 128 | // Make sure we visit all dependents first |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 129 | DenseSet<const GlobalVariable *> Others; |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 130 | for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i) |
| 131 | DiscoverDependentGlobals(GV->getOperand(i), Others); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 132 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 133 | for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(), |
| 134 | E = Others.end(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 135 | I != E; ++I) |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 136 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 143 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 144 | void NVPTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 145 | MCInst Inst; |
| 146 | lowerToMCInst(MI, Inst); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 147 | EmitToStreamer(*OutStreamer, Inst); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 150 | // Handle symbol backtracking for targets that do not support image handles |
| 151 | bool NVPTXAsmPrinter::lowerImageHandleOperand(const MachineInstr *MI, |
| 152 | unsigned OpNo, MCOperand &MCOp) { |
| 153 | const MachineOperand &MO = MI->getOperand(OpNo); |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 154 | const MCInstrDesc &MCID = MI->getDesc(); |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 155 | |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 156 | if (MCID.TSFlags & NVPTXII::IsTexFlag) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 157 | // This is a texture fetch, so operand 4 is a texref and operand 5 is |
| 158 | // a samplerref |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 159 | if (OpNo == 4 && MO.isImm()) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 160 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 161 | return true; |
| 162 | } |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 163 | if (OpNo == 5 && MO.isImm() && !(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 164 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | return false; |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 169 | } 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 Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 175 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | return false; |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 180 | } else if (MCID.TSFlags & NVPTXII::IsSustFlag) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 181 | // This is a surface store, so operand 0 is a surfref |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 182 | if (OpNo == 0 && MO.isImm()) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 183 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | return false; |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 188 | } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 189 | // This is a query, so operand 1 is a surfref/texref |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 190 | if (OpNo == 1 && MO.isImm()) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 191 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | return false; |
| 196 | } |
Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 197 | |
| 198 | return false; |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void NVPTXAsmPrinter::lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp) { |
| 202 | // Ewwww |
Matthias Braun | 7a75a91 | 2018-11-05 23:49:14 +0000 | [diff] [blame] | 203 | LLVMTargetMachine &TM = const_cast<LLVMTargetMachine&>(MF->getTarget()); |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 204 | 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 Parsons | 06ac79c | 2016-11-02 16:43:50 +0000 | [diff] [blame] | 209 | MCOp = GetSymbolRef(OutContext.getOrCreateSymbol(StringRef(*SymNamePtr))); |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 212 | void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) { |
| 213 | OutMI.setOpcode(MI->getOpcode()); |
Justin Holewinski | 3d49e5c | 2013-11-15 12:30:04 +0000 | [diff] [blame] | 214 | // Special: Do not mangle symbol operand of CALL_PROTOTYPE |
| 215 | if (MI->getOpcode() == NVPTX::CALL_PROTOTYPE) { |
| 216 | const MachineOperand &MO = MI->getOperand(0); |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 217 | OutMI.addOperand(GetSymbolRef( |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 218 | OutContext.getOrCreateSymbol(Twine(MO.getSymbolName())))); |
Justin Holewinski | 3d49e5c | 2013-11-15 12:30:04 +0000 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 222 | const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>(); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 223 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 224 | const MachineOperand &MO = MI->getOperand(i); |
| 225 | |
| 226 | MCOperand MCOp; |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 227 | if (!STI.hasImageHandles()) { |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 228 | if (lowerImageHandleOperand(MI, i, MCOp)) { |
| 229 | OutMI.addOperand(MCOp); |
| 230 | continue; |
| 231 | } |
| 232 | } |
| 233 | |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 234 | if (lowerOperand(MO, MCOp)) |
| 235 | OutMI.addOperand(MCOp); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | bool 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 Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 244 | MCOp = MCOperand::createReg(encodeVirtualRegister(MO.getReg())); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 245 | break; |
| 246 | case MachineOperand::MO_Immediate: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 247 | MCOp = MCOperand::createImm(MO.getImm()); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 248 | break; |
| 249 | case MachineOperand::MO_MachineBasicBlock: |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 250 | MCOp = MCOperand::createExpr(MCSymbolRefExpr::create( |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 251 | MO.getMBB()->getSymbol(), OutContext)); |
| 252 | break; |
| 253 | case MachineOperand::MO_ExternalSymbol: |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 254 | MCOp = GetSymbolRef(GetExternalSymbolSymbol(MO.getSymbolName())); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 255 | break; |
| 256 | case MachineOperand::MO_GlobalAddress: |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 257 | MCOp = GetSymbolRef(getSymbol(MO.getGlobal())); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 258 | break; |
| 259 | case MachineOperand::MO_FPImmediate: { |
| 260 | const ConstantFP *Cnt = MO.getFPImm(); |
Benjamin Kramer | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 261 | const APFloat &Val = Cnt->getValueAPF(); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 262 | |
| 263 | switch (Cnt->getType()->getTypeID()) { |
| 264 | default: report_fatal_error("Unsupported FP type"); break; |
Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 265 | case Type::HalfTyID: |
| 266 | MCOp = MCOperand::createExpr( |
| 267 | NVPTXFloatMCExpr::createConstantFPHalf(Val, OutContext)); |
| 268 | break; |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 269 | case Type::FloatTyID: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 270 | MCOp = MCOperand::createExpr( |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 271 | NVPTXFloatMCExpr::createConstantFPSingle(Val, OutContext)); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 272 | break; |
| 273 | case Type::DoubleTyID: |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 274 | MCOp = MCOperand::createExpr( |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 275 | NVPTXFloatMCExpr::createConstantFPDouble(Val, OutContext)); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 276 | break; |
| 277 | } |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | unsigned NVPTXAsmPrinter::encodeVirtualRegister(unsigned Reg) { |
Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 285 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 286 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 287 | |
Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 288 | DenseMap<unsigned, unsigned> &RegMap = VRegMapping[RC]; |
| 289 | unsigned RegNum = RegMap[Reg]; |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 290 | |
Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 291 | // 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 Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 306 | } else if (RC == &NVPTX::Float16RegsRegClass) { |
| 307 | Ret = (7 << 28); |
Artem Belevich | 620db1f | 2017-02-23 22:38:24 +0000 | [diff] [blame] | 308 | } else if (RC == &NVPTX::Float16x2RegsRegClass) { |
| 309 | Ret = (8 << 28); |
Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 310 | } else { |
| 311 | report_fatal_error("Bad register class"); |
| 312 | } |
| 313 | |
| 314 | // Insert the vreg number |
| 315 | Ret |= (RegNum & 0x0FFFFFFF); |
| 316 | return Ret; |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 317 | } else { |
Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 318 | // 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 Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 321 | } |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 324 | MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol *Symbol) { |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 325 | const MCExpr *Expr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 326 | Expr = MCSymbolRefExpr::create(Symbol, MCSymbolRefExpr::VK_None, |
Justin Holewinski | 8b24e1e | 2013-08-06 23:06:42 +0000 | [diff] [blame] | 327 | OutContext); |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 328 | return MCOperand::createExpr(Expr); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 331 | void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) { |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 332 | const DataLayout &DL = getDataLayout(); |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 333 | const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F); |
| 334 | const TargetLowering *TLI = STI.getTargetLowering(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 335 | |
| 336 | Type *Ty = F->getReturnType(); |
| 337 | |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 338 | bool isABI = (STI.getSmVersion() >= 20); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 339 | |
| 340 | if (Ty->getTypeID() == Type::VoidTyID) |
| 341 | return; |
| 342 | |
| 343 | O << " ("; |
| 344 | |
| 345 | if (isABI) { |
Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 346 | if (Ty->isFloatingPointTy() || (Ty->isIntegerTy() && !Ty->isIntegerTy(128))) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 347 | unsigned size = 0; |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 348 | if (auto *ITy = dyn_cast<IntegerType>(Ty)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 349 | size = ITy->getBitWidth(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 350 | } else { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 351 | assert(Ty->isFloatingPointTy() && "Floating point type expected here"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 352 | size = Ty->getPrimitiveSizeInBits(); |
| 353 | } |
Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 354 | // 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 359 | |
| 360 | O << ".param .b" << size << " func_retval0"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 361 | } else if (isa<PointerType>(Ty)) { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 362 | O << ".param .b" << TLI->getPointerTy(DL).getSizeInBits() |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 363 | << " func_retval0"; |
Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 364 | } else if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) { |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 365 | unsigned totalsz = DL.getTypeAllocSize(Ty); |
Artem Belevich | 74158b5 | 2016-07-20 18:39:52 +0000 | [diff] [blame] | 366 | unsigned retAlignment = 0; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 367 | if (!getAlign(*F, 0, retAlignment)) |
Artem Belevich | 74158b5 | 2016-07-20 18:39:52 +0000 | [diff] [blame] | 368 | retAlignment = DL.getABITypeAlignment(Ty); |
| 369 | O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz |
| 370 | << "]"; |
Craig Topper | d3c02f1 | 2015-01-05 10:15:49 +0000 | [diff] [blame] | 371 | } else |
| 372 | llvm_unreachable("Unknown return type"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 373 | } else { |
| 374 | SmallVector<EVT, 16> vtparts; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 375 | ComputeValueVTs(*TLI, DL, Ty, vtparts); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 376 | unsigned idx = 0; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 377 | for (unsigned i = 0, e = vtparts.size(); i != e; ++i) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 378 | 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 385 | for (unsigned j = 0, je = elems; j != je; ++j) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 386 | unsigned sz = elemtype.getSizeInBits(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 387 | if (elemtype.isInteger() && (sz < 32)) |
| 388 | sz = 32; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 389 | O << ".reg .b" << sz << " func_retval" << idx; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 390 | if (j < je - 1) |
| 391 | O << ", "; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 392 | ++idx; |
| 393 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 394 | if (i < e - 1) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 395 | O << ", "; |
| 396 | } |
| 397 | } |
| 398 | O << ") "; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF, |
| 402 | raw_ostream &O) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 403 | const Function &F = MF.getFunction(); |
| 404 | printReturnValStr(&F, O); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 407 | // Return true if MBB is the header of a loop marked with |
| 408 | // llvm.loop.unroll.disable. |
Jingyue Wu | 5bbcdaa | 2015-02-03 17:57:38 +0000 | [diff] [blame] | 409 | // TODO: consider "#pragma unroll 1" which is equivalent to "#pragma nounroll". |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 410 | bool NVPTXAsmPrinter::isLoopHeaderOfNoUnroll( |
| 411 | const MachineBasicBlock &MBB) const { |
| 412 | MachineLoopInfo &LI = getAnalysis<MachineLoopInfo>(); |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 413 | // We insert .pragma "nounroll" only to the loop header. |
Benjamin Kramer | db220db | 2015-06-02 15:28:27 +0000 | [diff] [blame] | 414 | if (!LI.isLoopHeader(&MBB)) |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 415 | 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 Smith | 1d15a9f | 2016-03-25 00:35:38 +0000 | [diff] [blame] | 427 | if (MDNode *LoopID = |
| 428 | PBB->getTerminator()->getMetadata(LLVMContext::MD_loop)) { |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 429 | if (GetUnrollMetadata(LoopID, "llvm.loop.unroll.disable")) |
| 430 | return true; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | void NVPTXAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const { |
| 438 | AsmPrinter::EmitBasicBlockStart(MBB); |
| 439 | if (isLoopHeaderOfNoUnroll(MBB)) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 440 | OutStreamer->EmitRawText(StringRef("\t.pragma \"nounroll\";\n")); |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 443 | void NVPTXAsmPrinter::EmitFunctionEntryLabel() { |
| 444 | SmallString<128> Str; |
| 445 | raw_svector_ostream O(Str); |
| 446 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 447 | if (!GlobalsEmitted) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 448 | emitGlobals(*MF->getFunction().getParent()); |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 449 | GlobalsEmitted = true; |
| 450 | } |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 451 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 452 | // Set up |
| 453 | MRI = &MF->getRegInfo(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 454 | F = &MF->getFunction(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 455 | emitLinkageDirective(F, O); |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 456 | if (isKernelFunction(*F)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 457 | O << ".entry "; |
| 458 | else { |
| 459 | O << ".func "; |
| 460 | printReturnValStr(*MF, O); |
| 461 | } |
| 462 | |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 463 | CurrentFnSym->print(O, MAI); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 464 | |
| 465 | emitFunctionParamList(*MF, O); |
| 466 | |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 467 | if (isKernelFunction(*F)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 468 | emitKernelFunctionDirectives(*F, O); |
| 469 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 470 | OutStreamer->EmitRawText(O.str()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 471 | |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 472 | VRegMapping.clear(); |
| 473 | // Emit open brace for function body. |
| 474 | OutStreamer->EmitRawText(StringRef("{\n")); |
| 475 | setAndEmitFunctionVirtualRegisters(*MF); |
| 476 | } |
| 477 | |
| 478 | bool NVPTXAsmPrinter::runOnMachineFunction(MachineFunction &F) { |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 479 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void NVPTXAsmPrinter::EmitFunctionBodyStart() { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 490 | SmallString<128> Str; |
| 491 | raw_svector_ostream O(Str); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 492 | emitDemotedVars(&MF->getFunction(), O); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 493 | OutStreamer->EmitRawText(O.str()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void NVPTXAsmPrinter::EmitFunctionBodyEnd() { |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 497 | VRegMapping.clear(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Alexey Bataev | 4dd7558 | 2018-07-26 16:29:52 +0000 | [diff] [blame] | 500 | const MCSymbol *NVPTXAsmPrinter::getFunctionFrameSymbol() const { |
| 501 | SmallString<128> Str; |
| 502 | raw_svector_ostream(Str) << DEPOTNAME << getFunctionNumber(); |
| 503 | return OutContext.getOrCreateSymbol(Str); |
| 504 | } |
| 505 | |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 506 | void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const { |
| 507 | unsigned RegNo = MI->getOperand(0).getReg(); |
Andrew Kaylor | 5c73e1f | 2015-03-24 23:37:10 +0000 | [diff] [blame] | 508 | if (TargetRegisterInfo::isVirtualRegister(RegNo)) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 509 | OutStreamer->AddComment(Twine("implicit-def: ") + |
| 510 | getVirtualRegisterName(RegNo)); |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 511 | } else { |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 512 | const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 513 | OutStreamer->AddComment(Twine("implicit-def: ") + |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 514 | STI.getRegisterInfo()->getName(RegNo)); |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 515 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 516 | OutStreamer->AddBlankLine(); |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 519 | void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F, |
| 520 | raw_ostream &O) const { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 521 | // 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 Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 526 | if (!getReqNTIDx(F, reqntidx)) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 527 | reqntidx = 1; |
| 528 | else |
| 529 | specified = true; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 530 | if (!getReqNTIDy(F, reqntidy)) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 531 | reqntidy = 1; |
| 532 | else |
| 533 | specified = true; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 534 | if (!getReqNTIDz(F, reqntidz)) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 535 | reqntidz = 1; |
| 536 | else |
| 537 | specified = true; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 538 | |
| 539 | if (specified) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 540 | O << ".reqntid " << reqntidx << ", " << reqntidy << ", " << reqntidz |
| 541 | << "\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 542 | |
| 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 Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 548 | if (!getMaxNTIDx(F, maxntidx)) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 549 | maxntidx = 1; |
| 550 | else |
| 551 | specified = true; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 552 | if (!getMaxNTIDy(F, maxntidy)) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 553 | maxntidy = 1; |
| 554 | else |
| 555 | specified = true; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 556 | if (!getMaxNTIDz(F, maxntidz)) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 557 | maxntidz = 1; |
| 558 | else |
| 559 | specified = true; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 560 | |
| 561 | if (specified) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 562 | O << ".maxntid " << maxntidx << ", " << maxntidy << ", " << maxntidz |
| 563 | << "\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 564 | |
| 565 | unsigned mincta; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 566 | if (getMinCTASm(F, mincta)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 567 | O << ".minnctapersm " << mincta << "\n"; |
Justin Lebar | 19bf9d2 | 2016-12-14 22:32:50 +0000 | [diff] [blame] | 568 | |
| 569 | unsigned maxnreg; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 570 | if (getMaxNReg(F, maxnreg)) |
Justin Lebar | 19bf9d2 | 2016-12-14 22:32:50 +0000 | [diff] [blame] | 571 | O << ".maxnreg " << maxnreg << "\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 574 | std::string |
| 575 | NVPTXAsmPrinter::getVirtualRegisterName(unsigned Reg) const { |
| 576 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 577 | |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 578 | std::string Name; |
| 579 | raw_string_ostream NameStr(Name); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 580 | |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 581 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 595 | void NVPTXAsmPrinter::emitVirtualRegister(unsigned int vr, |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 596 | raw_ostream &O) { |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 597 | O << getVirtualRegisterName(vr); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 600 | void 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 605 | O << "_" << vecelem[Imm]; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 606 | else if (0 == strcmp(Modifier, "vecv4comm1")) { |
| 607 | if ((Imm < 0) || (Imm > 3)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 608 | O << "//"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 609 | } else if (0 == strcmp(Modifier, "vecv4comm2")) { |
| 610 | if ((Imm < 4) || (Imm > 7)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 611 | O << "//"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 612 | } 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 618 | O << "//"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 619 | } else if (0 == strcmp(Modifier, "vecv2comm2")) { |
| 620 | if ((Imm < 2) || (Imm > 3)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 621 | O << "//"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 622 | } else if (0 == strcmp(Modifier, "vecv2pos")) { |
| 623 | if (Imm < 0) |
| 624 | Imm = 0; |
| 625 | O << "_" << vecelem[Imm % 2]; |
| 626 | } else |
Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 627 | llvm_unreachable("Unknown Modifier on immediate operand"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 630 | void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 631 | emitLinkageDirective(F, O); |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 632 | if (isKernelFunction(*F)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 633 | O << ".entry "; |
| 634 | else |
| 635 | O << ".func "; |
| 636 | printReturnValStr(F, O); |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 637 | getSymbol(F)->print(O, MAI); |
| 638 | O << "\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 639 | emitFunctionParamList(F, O); |
| 640 | O << ";\n"; |
| 641 | } |
| 642 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 643 | static bool usedInGlobalVarDef(const Constant *C) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 644 | if (!C) |
| 645 | return false; |
| 646 | |
| 647 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
Jingyue Wu | 4be014a | 2015-07-31 05:09:47 +0000 | [diff] [blame] | 648 | return GV->getName() != "llvm.used"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 651 | for (const User *U : C->users()) |
| 652 | if (const Constant *C = dyn_cast<Constant>(U)) |
| 653 | if (usedInGlobalVarDef(C)) |
| 654 | return true; |
| 655 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 656 | return false; |
| 657 | } |
| 658 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 659 | static bool usedInOneFunc(const User *U, Function const *&oneFunc) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 660 | if (const GlobalVariable *othergv = dyn_cast<GlobalVariable>(U)) { |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 661 | if (othergv->getName() == "llvm.used") |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 662 | 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 672 | } else |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 673 | return false; |
| 674 | } |
| 675 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 676 | for (const User *UU : U->users()) |
Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 677 | if (!usedInOneFunc(UU, oneFunc)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 678 | return false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 679 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 680 | 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 | */ |
| 690 | static bool canDemoteGlobalVar(const GlobalVariable *gv, Function const *&f) { |
Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 691 | if (!gv->hasInternalLinkage()) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 692 | return false; |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 693 | PointerType *Pty = gv->getType(); |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 694 | if (Pty->getAddressSpace() != ADDRESS_SPACE_SHARED) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 695 | return false; |
| 696 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 697 | const Function *oneFunc = nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 698 | |
| 699 | bool flag = usedInOneFunc(gv, oneFunc); |
Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 700 | if (!flag) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 701 | return false; |
| 702 | if (!oneFunc) |
| 703 | return false; |
| 704 | f = oneFunc; |
| 705 | return true; |
| 706 | } |
| 707 | |
| 708 | static bool useFuncSeen(const Constant *C, |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 709 | DenseMap<const Function *, bool> &seenMap) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 710 | for (const User *U : C->users()) { |
| 711 | if (const Constant *cu = dyn_cast<Constant>(U)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 712 | if (useFuncSeen(cu, seenMap)) |
| 713 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 714 | } else if (const Instruction *I = dyn_cast<Instruction>(U)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 715 | const BasicBlock *bb = I->getParent(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 716 | if (!bb) |
| 717 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 718 | const Function *caller = bb->getParent(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 719 | if (!caller) |
| 720 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 721 | if (seenMap.find(caller) != seenMap.end()) |
| 722 | return true; |
| 723 | } |
| 724 | } |
| 725 | return false; |
| 726 | } |
| 727 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 728 | void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) { |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 729 | DenseMap<const Function *, bool> seenMap; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 730 | for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
Duncan P. N. Exon Smith | 61149b8 | 2015-10-20 00:54:09 +0000 | [diff] [blame] | 731 | const Function *F = &*FI; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 732 | |
Justin Lebar | 49fac56 | 2018-12-26 19:12:31 +0000 | [diff] [blame] | 733 | if (F->getAttributes().hasFnAttribute("nvptx-libcall-callee")) { |
| 734 | emitDeclaration(F, O); |
| 735 | continue; |
| 736 | } |
| 737 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 738 | if (F->isDeclaration()) { |
| 739 | if (F->use_empty()) |
| 740 | continue; |
| 741 | if (F->getIntrinsicID()) |
| 742 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 743 | emitDeclaration(F, O); |
| 744 | continue; |
| 745 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 746 | for (const User *U : F->users()) { |
| 747 | if (const Constant *C = dyn_cast<Constant>(U)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 748 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 752 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 758 | emitDeclaration(F, O); |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 763 | if (!isa<Instruction>(U)) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 764 | continue; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 765 | const Instruction *instr = cast<Instruction>(U); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 766 | const BasicBlock *bb = instr->getParent(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 767 | if (!bb) |
| 768 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 769 | const Function *caller = bb->getParent(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 770 | if (!caller) |
| 771 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 772 | |
| 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 777 | emitDeclaration(F, O); |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | seenMap[F] = true; |
| 782 | } |
| 783 | } |
| 784 | |
Justin Lebar | ead59f4 | 2016-01-30 01:07:38 +0000 | [diff] [blame] | 785 | static 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 792 | bool NVPTXAsmPrinter::doInitialization(Module &M) { |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 793 | // 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 Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 796 | const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM); |
Justin Lebar | 7f41fe3 | 2018-12-22 01:30:37 +0000 | [diff] [blame] | 797 | const auto* STI = static_cast<const NVPTXSubtarget*>(NTM.getSubtargetImpl()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 798 | |
Justin Lebar | 3a5f579 | 2016-01-23 21:12:20 +0000 | [diff] [blame] | 799 | if (M.alias_size()) { |
| 800 | report_fatal_error("Module has aliases, which NVPTX does not support."); |
| 801 | return true; // error |
| 802 | } |
Justin Lebar | ead59f4 | 2016-01-30 01:07:38 +0000 | [diff] [blame] | 803 | 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 Lebar | 3a5f579 | 2016-01-23 21:12:20 +0000 | [diff] [blame] | 813 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 814 | SmallString<128> Str1; |
| 815 | raw_svector_ostream OS1(Str1); |
| 816 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 817 | // We need to call the parent's one explicitly. |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 818 | bool Result = AsmPrinter::doInitialization(M); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 819 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 820 | // Emit header before any dwarf directives are emitted below. |
Justin Lebar | 7f41fe3 | 2018-12-22 01:30:37 +0000 | [diff] [blame] | 821 | emitHeader(M, OS1, *STI); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 822 | OutStreamer->EmitRawText(OS1.str()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 823 | |
Justin Holewinski | d2bbdf0 | 2013-07-01 13:00:14 +0000 | [diff] [blame] | 824 | // Emit module-level inline asm if it exists. |
| 825 | if (!M.getModuleInlineAsm().empty()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 826 | 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 Holewinski | d2bbdf0 | 2013-07-01 13:00:14 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame] | 834 | GlobalsEmitted = false; |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 835 | |
| 836 | return Result; |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | void NVPTXAsmPrinter::emitGlobals(const Module &M) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 840 | SmallString<128> Str2; |
| 841 | raw_svector_ostream OS2(Str2); |
| 842 | |
| 843 | emitDeclarations(M, OS2); |
| 844 | |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 845 | // 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 Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 850 | SmallVector<const GlobalVariable *, 8> Globals; |
| 851 | DenseSet<const GlobalVariable *> GVVisited; |
| 852 | DenseSet<const GlobalVariable *> GVVisiting; |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 853 | |
| 854 | // Visit each global variable, in order |
Duncan P. N. Exon Smith | 61149b8 | 2015-10-20 00:54:09 +0000 | [diff] [blame] | 855 | for (const GlobalVariable &I : M.globals()) |
| 856 | VisitGlobalVariableForEmission(&I, Globals, GVVisited, GVVisiting); |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 857 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 858 | assert(GVVisited.size() == M.getGlobalList().size() && |
Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 859 | "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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 865 | |
| 866 | OS2 << '\n'; |
| 867 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 868 | OutStreamer->EmitRawText(OS2.str()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 871 | void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O, |
| 872 | const NVPTXSubtarget &STI) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 873 | O << "//\n"; |
| 874 | O << "// Generated by LLVM NVPTX Back-End\n"; |
| 875 | O << "//\n"; |
| 876 | O << "\n"; |
| 877 | |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 878 | unsigned PTXVersion = STI.getPTXVersion(); |
Justin Holewinski | 1812ee9 | 2012-11-12 03:16:43 +0000 | [diff] [blame] | 879 | O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 880 | |
| 881 | O << ".target "; |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 882 | O << STI.getTargetName(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 883 | |
Eric Christopher | ca929f2 | 2015-02-19 00:22:47 +0000 | [diff] [blame] | 884 | const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM); |
| 885 | if (NTM.getDrvInterface() == NVPTX::NVCL) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 886 | O << ", texmode_independent"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 887 | |
Alexey Bataev | 2e1a782 | 2018-12-06 16:25:35 +0000 | [diff] [blame] | 888 | 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 Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 902 | // FIXME: remove comment once debug info is properly supported. |
Alexey Bataev | 2e1a782 | 2018-12-06 16:25:35 +0000 | [diff] [blame] | 903 | if (MMI && MMI->hasDebugInfo() && HasFullDebugInfo) |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 904 | O << "//, debug"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 905 | |
| 906 | O << "\n"; |
| 907 | |
| 908 | O << ".address_size "; |
Eric Christopher | ca929f2 | 2015-02-19 00:22:47 +0000 | [diff] [blame] | 909 | if (NTM.is64Bit()) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 910 | O << "64"; |
| 911 | else |
| 912 | O << "32"; |
| 913 | O << "\n"; |
| 914 | |
| 915 | O << "\n"; |
| 916 | } |
| 917 | |
| 918 | bool NVPTXAsmPrinter::doFinalization(Module &M) { |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 919 | bool HasDebugInfo = MMI && MMI->hasDebugInfo(); |
| 920 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 921 | // 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 928 | // 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 Noblesmith | c9e2a27 | 2014-08-26 02:03:35 +0000 | [diff] [blame] | 933 | GlobalVariable **gv_array = new GlobalVariable *[n]; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 934 | |
| 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 938 | I != E; ++I) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 939 | 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 949 | for (i = 0; i < n; i++) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 950 | global_list.insert(global_list.end(), gv_array[i]); |
| 951 | |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 952 | clearAnnotationCache(&M); |
Dylan Noblesmith | c9e2a27 | 2014-08-26 02:03:35 +0000 | [diff] [blame] | 953 | |
| 954 | delete[] gv_array; |
Eric Christopher | 68f2218 | 2018-05-18 03:13:08 +0000 | [diff] [blame] | 955 | // FIXME: remove comment once debug info is properly supported. |
| 956 | // Close the last emitted section |
| 957 | if (HasDebugInfo) |
| 958 | OutStreamer->EmitRawText("//\t}"); |
| 959 | |
Alexey Bataev | 64ad0ad | 2018-12-06 16:02:09 +0000 | [diff] [blame] | 960 | // Output last DWARF .file directives, if any. |
| 961 | static_cast<NVPTXTargetStreamer *>(OutStreamer->getTargetStreamer()) |
| 962 | ->outputDwarfFileDirectives(); |
| 963 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 964 | return ret; |
| 965 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 966 | //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 Holewinski | 7d5bf66 | 2014-06-27 18:35:10 +0000 | [diff] [blame] | 984 | // for any linkage other than |
| 985 | // internal, private, linker_private, |
| 986 | // linker_private_weak, linker_private_weak_def_auto, |
| 987 | // we emit -> .weak. |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 988 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 989 | void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V, |
| 990 | raw_ostream &O) { |
Eric Christopher | beffc4e | 2015-02-19 00:08:23 +0000 | [diff] [blame] | 991 | if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 992 | 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 Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 1010 | msg.append(V->getName()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1011 | msg.append("has unsupported appending linkage type"); |
| 1012 | llvm_unreachable(msg.c_str()); |
Justin Holewinski | 7d5bf66 | 2014-06-27 18:35:10 +0000 | [diff] [blame] | 1013 | } else if (!V->hasInternalLinkage() && |
| 1014 | !V->hasPrivateLinkage()) { |
| 1015 | O << ".weak "; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1020 | void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar, |
| 1021 | raw_ostream &O, |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1022 | bool processDemoted) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1023 | // Skip meta data |
| 1024 | if (GVar->hasSection()) { |
Rafael Espindola | 83658d6 | 2016-05-11 18:21:59 +0000 | [diff] [blame] | 1025 | if (GVar->getSection() == "llvm.metadata") |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1026 | return; |
| 1027 | } |
| 1028 | |
Justin Holewinski | 73cb5de | 2014-06-27 18:35:53 +0000 | [diff] [blame] | 1029 | // Skip LLVM intrinsic global variables |
| 1030 | if (GVar->getName().startswith("llvm.") || |
| 1031 | GVar->getName().startswith("nvvm.")) |
| 1032 | return; |
| 1033 | |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1034 | const DataLayout &DL = getDataLayout(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1035 | |
| 1036 | // GlobalVariables are always constant pointers themselves. |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1037 | PointerType *PTy = GVar->getType(); |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1038 | Type *ETy = GVar->getValueType(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1039 | |
| 1040 | if (GVar->hasExternalLinkage()) { |
| 1041 | if (GVar->hasInitializer()) |
| 1042 | O << ".visible "; |
| 1043 | else |
| 1044 | O << ".extern "; |
Justin Holewinski | d73767a | 2014-06-27 18:35:56 +0000 | [diff] [blame] | 1045 | } else if (GVar->hasLinkOnceLinkage() || GVar->hasWeakLinkage() || |
| 1046 | GVar->hasAvailableExternallyLinkage() || |
| 1047 | GVar->hasCommonLinkage()) { |
| 1048 | O << ".weak "; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1051 | if (isTexture(*GVar)) { |
| 1052 | O << ".global .texref " << getTextureName(*GVar) << ";\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1053 | return; |
| 1054 | } |
| 1055 | |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1056 | if (isSurface(*GVar)) { |
| 1057 | O << ".global .surfref " << getSurfaceName(*GVar) << ";\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1058 | 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 Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1070 | if (isSampler(*GVar)) { |
| 1071 | O << ".global .samplerref " << getSamplerName(*GVar); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1072 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1073 | const Constant *Initializer = nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1074 | if (GVar->hasInitializer()) |
| 1075 | Initializer = GVar->getInitializer(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1076 | const ConstantInt *CI = nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1077 | if (Initializer) |
| 1078 | CI = dyn_cast<ConstantInt>(Initializer); |
| 1079 | if (CI) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1080 | unsigned sample = CI->getZExtValue(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1081 | |
| 1082 | O << " = { "; |
| 1083 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1084 | for (int i = 0, |
| 1085 | addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE); |
| 1086 | i < 3; i++) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1087 | O << "addr_mode_" << i << " = "; |
| 1088 | switch (addr) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1089 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1104 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1105 | O << ", "; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1106 | } |
| 1107 | O << "filter_mode = "; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1108 | 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 Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 1116 | llvm_unreachable("Anisotropic filtering is not supported"); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1117 | default: |
| 1118 | O << "nearest"; |
| 1119 | break; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1120 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1121 | if (!((sample & __CLK_NORMALIZED_MASK) >> __CLK_NORMALIZED_BASE)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1122 | O << ", force_unnormalized_coords = 1"; |
| 1123 | } |
| 1124 | O << " }"; |
| 1125 | } |
| 1126 | |
| 1127 | O << ";\n"; |
| 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | if (GVar->hasPrivateLinkage()) { |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1132 | if (strncmp(GVar->getName().data(), "unrollpragma", 12) == 0) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | |
| 1135 | // FIXME - need better way (e.g. Metadata) to avoid generating this global |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1136 | if (strncmp(GVar->getName().data(), "filename", 8) == 0) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1137 | return; |
| 1138 | if (GVar->use_empty()) |
| 1139 | return; |
| 1140 | } |
| 1141 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1142 | const Function *demotedFunc = nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1143 | if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) { |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 1144 | O << "// " << GVar->getName() << " has been demoted\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1145 | if (localDecls.find(demotedFunc) != localDecls.end()) |
| 1146 | localDecls[demotedFunc].push_back(GVar); |
| 1147 | else { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1148 | std::vector<const GlobalVariable *> temp; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1149 | temp.push_back(GVar); |
| 1150 | localDecls[demotedFunc] = temp; |
| 1151 | } |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | O << "."; |
| 1156 | emitPTXAddressSpace(PTy->getAddressSpace(), O); |
Justin Holewinski | 773ca40 | 2014-06-27 18:35:58 +0000 | [diff] [blame] | 1157 | |
| 1158 | if (isManaged(*GVar)) { |
| 1159 | O << " .attribute(.managed)"; |
| 1160 | } |
| 1161 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1162 | if (GVar->getAlignment() == 0) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1163 | O << " .align " << (int)DL.getPrefTypeAlignment(ETy); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1164 | else |
| 1165 | O << " .align " << GVar->getAlignment(); |
| 1166 | |
Justin Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 1167 | if (ETy->isFloatingPointTy() || ETy->isPointerTy() || |
| 1168 | (ETy->isIntegerTy() && ETy->getScalarSizeInBits() <= 64)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1169 | O << " ."; |
Justin Holewinski | 700b6fa | 2013-05-20 12:13:28 +0000 | [diff] [blame] | 1170 | // 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1175 | O << " "; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1176 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Ptx allows variable initilization only for constant and global state |
| 1179 | // spaces. |
Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1180 | if (GVar->hasInitializer()) { |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1181 | if ((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) || |
| 1182 | (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) { |
Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1183 | const Constant *Initializer = GVar->getInitializer(); |
Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1184 | // 'undef' is treated as there is no value specified. |
Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1185 | if (!Initializer->isNullValue() && !isa<UndefValue>(Initializer)) { |
| 1186 | O << " = "; |
| 1187 | printScalarConstant(Initializer, O); |
| 1188 | } |
| 1189 | } else { |
Jingyue Wu | fcec098 | 2015-08-22 05:40:26 +0000 | [diff] [blame] | 1190 | // 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 Kramer | dba7ee9 | 2015-05-28 11:24:24 +0000 | [diff] [blame] | 1195 | report_fatal_error("initial value of '" + GVar->getName() + |
| 1196 | "' is not allowed in addrspace(" + |
| 1197 | Twine(PTy->getAddressSpace()) + ")"); |
Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1198 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | } else { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1202 | unsigned int ElementSize = 0; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1203 | |
| 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 Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 1209 | case Type::IntegerTyID: // Integers larger than 64 bits |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1210 | case Type::StructTyID: |
| 1211 | case Type::ArrayTyID: |
| 1212 | case Type::VectorTyID: |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1213 | ElementSize = DL.getTypeStoreSize(ETy); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1214 | // Ptx allows variable initilization only for constant and |
| 1215 | // global state spaces. |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1216 | if (((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) || |
| 1217 | (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) && |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1218 | GVar->hasInitializer()) { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1219 | const Constant *Initializer = GVar->getInitializer(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1220 | if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1221 | AggBuffer aggBuffer(ElementSize, O, *this); |
| 1222 | bufferAggregateConstant(Initializer, &aggBuffer); |
| 1223 | if (aggBuffer.numSymbols) { |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1224 | if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1225 | O << " .u64 "; |
| 1226 | getSymbol(GVar)->print(O, MAI); |
| 1227 | O << "["; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1228 | O << ElementSize / 8; |
| 1229 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1230 | O << " .u32 "; |
| 1231 | getSymbol(GVar)->print(O, MAI); |
| 1232 | O << "["; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1233 | O << ElementSize / 4; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1234 | } |
| 1235 | O << "]"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1236 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1237 | O << " .b8 "; |
| 1238 | getSymbol(GVar)->print(O, MAI); |
| 1239 | O << "["; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1240 | O << ElementSize; |
| 1241 | O << "]"; |
| 1242 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1243 | O << " = {"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1244 | aggBuffer.print(); |
| 1245 | O << "}"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1246 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1247 | O << " .b8 "; |
| 1248 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1249 | if (ElementSize) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1250 | O << "["; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1251 | O << ElementSize; |
| 1252 | O << "]"; |
| 1253 | } |
| 1254 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1255 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1256 | O << " .b8 "; |
| 1257 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1258 | if (ElementSize) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1259 | O << "["; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1260 | O << ElementSize; |
| 1261 | O << "]"; |
| 1262 | } |
| 1263 | } |
| 1264 | break; |
| 1265 | default: |
Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 1266 | llvm_unreachable("type not supported yet"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1267 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1268 | } |
| 1269 | O << ";\n"; |
| 1270 | } |
| 1271 | |
| 1272 | void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) { |
| 1273 | if (localDecls.find(f) == localDecls.end()) |
| 1274 | return; |
| 1275 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1276 | std::vector<const GlobalVariable *> &gvars = localDecls[f]; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1277 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1278 | for (unsigned i = 0, e = gvars.size(); i != e; ++i) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1279 | O << "\t// demoted variable\n\t"; |
| 1280 | printModuleLevelGV(gvars[i], O, true); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace, |
| 1285 | raw_ostream &O) const { |
| 1286 | switch (AddressSpace) { |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1287 | case ADDRESS_SPACE_LOCAL: |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1288 | O << "local"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1289 | break; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1290 | case ADDRESS_SPACE_GLOBAL: |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1291 | O << "global"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1292 | break; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1293 | case ADDRESS_SPACE_CONST: |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1294 | O << "const"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1295 | break; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1296 | case ADDRESS_SPACE_SHARED: |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1297 | O << "shared"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | default: |
Justin Lebar | 321b443 | 2018-02-08 00:50:04 +0000 | [diff] [blame] | 1300 | report_fatal_error("Bad address space found while emitting PTX: " + |
| 1301 | llvm::Twine(AddressSpace)); |
Justin Holewinski | 36a5099 | 2013-02-09 13:34:15 +0000 | [diff] [blame] | 1302 | break; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1306 | std::string |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1307 | NVPTXAsmPrinter::getPTXFundamentalTypeStr(Type *Ty, bool useB4PTR) const { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1308 | 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 Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 1325 | case Type::HalfTyID: |
| 1326 | // fp16 is stored as .b16 for compatibility with pre-sm_53 PTX assembly. |
| 1327 | return "b16"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1328 | case Type::FloatTyID: |
| 1329 | return "f32"; |
| 1330 | case Type::DoubleTyID: |
| 1331 | return "f64"; |
| 1332 | case Type::PointerTyID: |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1333 | if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1334 | if (useB4PTR) |
| 1335 | return "b64"; |
| 1336 | else |
| 1337 | return "u64"; |
| 1338 | else if (useB4PTR) |
| 1339 | return "b32"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1340 | else |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1341 | return "u32"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1342 | } |
| 1343 | llvm_unreachable("unexpected type"); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1344 | return nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1347 | void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar, |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1348 | raw_ostream &O) { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1349 | const DataLayout &DL = getDataLayout(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1350 | |
| 1351 | // GlobalVariables are always constant pointers themselves. |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1352 | Type *ETy = GVar->getValueType(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1353 | |
| 1354 | O << "."; |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1355 | emitPTXAddressSpace(GVar->getType()->getAddressSpace(), O); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1356 | if (GVar->getAlignment() == 0) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1357 | O << " .align " << (int)DL.getPrefTypeAlignment(ETy); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1358 | else |
| 1359 | O << " .align " << GVar->getAlignment(); |
| 1360 | |
Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 1361 | // 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 Kumar | b3091da | 2018-07-06 20:17:42 +0000 | [diff] [blame] | 1369 | if (ETy->isFloatingPointTy() || ETy->isIntOrPtrTy()) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1370 | O << " ."; |
| 1371 | O << getPTXFundamentalTypeStr(ETy); |
| 1372 | O << " "; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1373 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1374 | return; |
| 1375 | } |
| 1376 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1377 | int64_t ElementSize = 0; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1378 | |
| 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 Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1387 | ElementSize = DL.getTypeStoreSize(ETy); |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1388 | O << " .b8 "; |
| 1389 | getSymbol(GVar)->print(O, MAI); |
| 1390 | O << "["; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1391 | if (ElementSize) { |
Benjamin Kramer | dba7ee9 | 2015-05-28 11:24:24 +0000 | [diff] [blame] | 1392 | O << ElementSize; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1393 | } |
| 1394 | O << "]"; |
| 1395 | break; |
| 1396 | default: |
Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 1397 | llvm_unreachable("type not supported yet"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1398 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1401 | static unsigned int getOpenCLAlignment(const DataLayout &DL, Type *Ty) { |
Rafael Espindola | 0801334 | 2013-12-07 19:34:20 +0000 | [diff] [blame] | 1402 | if (Ty->isSingleValueType()) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1403 | return DL.getPrefTypeAlignment(Ty); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1404 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1405 | auto *ATy = dyn_cast<ArrayType>(Ty); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1406 | if (ATy) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1407 | return getOpenCLAlignment(DL, ATy->getElementType()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1408 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1409 | auto *STy = dyn_cast<StructType>(Ty); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1410 | if (STy) { |
| 1411 | unsigned int alignStruct = 1; |
| 1412 | // Go through each element of the struct and find the |
| 1413 | // largest alignment. |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1414 | for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1415 | Type *ETy = STy->getElementType(i); |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1416 | unsigned int align = getOpenCLAlignment(DL, ETy); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1417 | if (align > alignStruct) |
| 1418 | alignStruct = align; |
| 1419 | } |
| 1420 | return alignStruct; |
| 1421 | } |
| 1422 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1423 | auto *FTy = dyn_cast<FunctionType>(Ty); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1424 | if (FTy) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1425 | return DL.getPointerPrefAlignment(); |
| 1426 | return DL.getPrefTypeAlignment(Ty); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I, |
| 1430 | int paramIndex, raw_ostream &O) { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1431 | getSymbol(I->getParent())->print(O, MAI); |
| 1432 | O << "_param_" << paramIndex; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1435 | void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1436 | const DataLayout &DL = getDataLayout(); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1437 | const AttributeList &PAL = F->getAttributes(); |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 1438 | const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F); |
| 1439 | const TargetLowering *TLI = STI.getTargetLowering(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1440 | Function::const_arg_iterator I, E; |
| 1441 | unsigned paramIndex = 0; |
| 1442 | bool first = true; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1443 | bool isKernelFunc = isKernelFunction(*F); |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 1444 | bool isABI = (STI.getSmVersion() >= 20); |
| 1445 | bool hasImageHandles = STI.hasImageHandles(); |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1446 | MVT thePointerTy = TLI->getPointerTy(DL); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1447 | |
Justin Lebar | 2a161f9 | 2016-01-23 21:12:17 +0000 | [diff] [blame] | 1448 | if (F->arg_empty()) { |
| 1449 | O << "()\n"; |
| 1450 | return; |
| 1451 | } |
| 1452 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1453 | O << "(\n"; |
| 1454 | |
| 1455 | for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, paramIndex++) { |
Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1456 | Type *Ty = I->getType(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (!first) |
| 1459 | O << ",\n"; |
| 1460 | |
| 1461 | first = false; |
| 1462 | |
| 1463 | // Handle image/sampler parameters |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1464 | 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 Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 1469 | if (hasImageHandles) |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1470 | O << "\t.param .u64 .ptr .surfref "; |
| 1471 | else |
| 1472 | O << "\t.param .surfref "; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1473 | CurrentFnSym->print(O, MAI); |
| 1474 | O << "_param_" << paramIndex; |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1475 | } |
| 1476 | else { // Default image is read_only |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 1477 | if (hasImageHandles) |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1478 | O << "\t.param .u64 .ptr .texref "; |
| 1479 | else |
| 1480 | O << "\t.param .texref "; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1481 | CurrentFnSym->print(O, MAI); |
| 1482 | O << "_param_" << paramIndex; |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1483 | } |
| 1484 | } else { |
Artem Belevich | f802b93 | 2018-12-12 18:31:04 +0000 | [diff] [blame] | 1485 | if (hasImageHandles) |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1486 | O << "\t.param .u64 .ptr .samplerref "; |
| 1487 | else |
| 1488 | O << "\t.param .samplerref "; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1489 | CurrentFnSym->print(O, MAI); |
| 1490 | O << "_param_" << paramIndex; |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1491 | } |
| 1492 | continue; |
| 1493 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 1496 | if (!PAL.hasParamAttribute(paramIndex, Attribute::ByVal)) { |
Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 1497 | if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) { |
Gautam Chakrabarti | 2c28340 | 2014-01-28 18:35:29 +0000 | [diff] [blame] | 1498 | // Just print .param .align <a> .b8 .param[size]; |
Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1499 | // <a> = PAL.getparamalignment |
| 1500 | // size = typeallocsize of element type |
Reid Kleckner | 859f8b5 | 2017-04-28 20:34:27 +0000 | [diff] [blame] | 1501 | unsigned align = PAL.getParamAlignment(paramIndex); |
Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1502 | if (align == 0) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1503 | align = DL.getABITypeAlignment(Ty); |
Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1504 | |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1505 | unsigned sz = DL.getTypeAllocSize(Ty); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1506 | O << "\t.param .align " << align << " .b8 "; |
Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1507 | printParamName(I, paramIndex, O); |
| 1508 | O << "[" << sz << "]"; |
| 1509 | |
| 1510 | continue; |
| 1511 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1512 | // Just a scalar |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1513 | auto *PTy = dyn_cast<PointerType>(Ty); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1514 | if (isKernelFunc) { |
| 1515 | if (PTy) { |
| 1516 | // Special handling for pointer arguments to kernel |
| 1517 | O << "\t.param .u" << thePointerTy.getSizeInBits() << " "; |
| 1518 | |
Eric Christopher | beffc4e | 2015-02-19 00:08:23 +0000 | [diff] [blame] | 1519 | if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() != |
| 1520 | NVPTX::CUDA) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1521 | Type *ETy = PTy->getElementType(); |
| 1522 | int addrSpace = PTy->getAddressSpace(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1523 | switch (addrSpace) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1524 | default: |
| 1525 | O << ".ptr "; |
| 1526 | break; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1527 | case ADDRESS_SPACE_CONST: |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1528 | O << ".ptr .const "; |
| 1529 | break; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1530 | case ADDRESS_SPACE_SHARED: |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1531 | O << ".ptr .shared "; |
| 1532 | break; |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1533 | case ADDRESS_SPACE_GLOBAL: |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1534 | O << ".ptr .global "; |
| 1535 | break; |
| 1536 | } |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1537 | O << ".align " << (int)getOpenCLAlignment(DL, ETy) << " "; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1538 | } |
| 1539 | printParamName(I, paramIndex, O); |
| 1540 | continue; |
| 1541 | } |
| 1542 | |
| 1543 | // non-pointer scalar to kernel func |
Justin Holewinski | 700b6fa | 2013-05-20 12:13:28 +0000 | [diff] [blame] | 1544 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1551 | printParamName(I, paramIndex, O); |
| 1552 | continue; |
| 1553 | } |
| 1554 | // Non-kernel function, just print .param .b<size> for ABI |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 1555 | // and .reg .b<size> for non-ABI |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1556 | unsigned sz = 0; |
| 1557 | if (isa<IntegerType>(Ty)) { |
| 1558 | sz = cast<IntegerType>(Ty)->getBitWidth(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1559 | if (sz < 32) |
| 1560 | sz = 32; |
| 1561 | } else if (isa<PointerType>(Ty)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1562 | sz = thePointerTy.getSizeInBits(); |
Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 1563 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1568 | 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 Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1579 | auto *PTy = dyn_cast<PointerType>(Ty); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1580 | assert(PTy && "Param with byval attribute should be a pointer type"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1581 | Type *ETy = PTy->getElementType(); |
| 1582 | |
| 1583 | if (isABI || isKernelFunc) { |
Gautam Chakrabarti | 2c28340 | 2014-01-28 18:35:29 +0000 | [diff] [blame] | 1584 | // Just print .param .align <a> .b8 .param[size]; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1585 | // <a> = PAL.getparamalignment |
| 1586 | // size = typeallocsize of element type |
Reid Kleckner | 859f8b5 | 2017-04-28 20:34:27 +0000 | [diff] [blame] | 1587 | unsigned align = PAL.getParamAlignment(paramIndex); |
Justin Holewinski | 2dc9d07 | 2012-11-09 23:50:24 +0000 | [diff] [blame] | 1588 | if (align == 0) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1589 | align = DL.getABITypeAlignment(ETy); |
Artem Belevich | 052b1ed | 2016-07-18 19:54:56 +0000 | [diff] [blame] | 1590 | // 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 Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1603 | unsigned sz = DL.getTypeAllocSize(ETy); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1604 | O << "\t.param .align " << align << " .b8 "; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1605 | 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 Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1614 | ComputeValueVTs(*TLI, DL, ETy, vtparts); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1615 | for (unsigned i = 0, e = vtparts.size(); i != e; ++i) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1616 | 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1623 | for (unsigned j = 0, je = elems; j != je; ++j) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1624 | unsigned sz = elemtype.getSizeInBits(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1625 | if (elemtype.isInteger() && (sz < 32)) |
| 1626 | sz = 32; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1627 | O << "\t.reg .b" << sz << " "; |
| 1628 | printParamName(I, paramIndex, O); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1629 | if (j < je - 1) |
| 1630 | O << ",\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1631 | ++paramIndex; |
| 1632 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1633 | if (i < e - 1) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1634 | O << ",\n"; |
| 1635 | } |
| 1636 | --paramIndex; |
| 1637 | continue; |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | O << "\n)\n"; |
| 1642 | } |
| 1643 | |
| 1644 | void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF, |
| 1645 | raw_ostream &O) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1646 | const Function &F = MF.getFunction(); |
| 1647 | emitFunctionParamList(&F, O); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1650 | void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters( |
| 1651 | const MachineFunction &MF) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1652 | 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 Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1657 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1658 | //unsigned numRegClasses = TRI->getNumRegClasses(); |
| 1659 | |
| 1660 | // Emit the Fake Stack Object |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1661 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1662 | int NumBytes = (int) MFI.getStackSize(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1663 | if (NumBytes) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1664 | O << "\t.local .align " << MFI.getMaxAlignment() << " .b8 \t" << DEPOTNAME |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1665 | << getFunctionNumber() << "[" << NumBytes << "];\n"; |
Eric Christopher | 02389e3 | 2015-02-19 00:08:27 +0000 | [diff] [blame] | 1666 | if (static_cast<const NVPTXTargetMachine &>(MF.getTarget()).is64Bit()) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1667 | O << "\t.reg .b64 \t%SP;\n"; |
| 1668 | O << "\t.reg .b64 \t%SPL;\n"; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1669 | } else { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1670 | 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1680 | for (unsigned i = 0; i < numVRs; i++) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1681 | unsigned int vr = TRI->index2VirtReg(i); |
| 1682 | const TargetRegisterClass *RC = MRI->getRegClass(vr); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1683 | DenseMap<unsigned, unsigned> ®map = VRegMapping[RC]; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1684 | int n = regmap.size(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1685 | regmap.insert(std::make_pair(vr, n + 1)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | // Emit register declarations |
| 1689 | // @TODO: Extract out the real register usage |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1690 | // 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 Holewinski | 3e037d9 | 2014-07-16 16:26:58 +0000 | [diff] [blame] | 1694 | // O << "\t.reg .s64 %rd<" << NVPTXNumRegisters << ">;\n"; |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1695 | // O << "\t.reg .f32 %f<" << NVPTXNumRegisters << ">;\n"; |
Justin Holewinski | 3e037d9 | 2014-07-16 16:26:58 +0000 | [diff] [blame] | 1696 | // O << "\t.reg .f64 %fd<" << NVPTXNumRegisters << ">;\n"; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1697 | |
| 1698 | // Emit declaration of the virtual registers or 'physical' registers for |
| 1699 | // each register class |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1700 | for (unsigned i=0; i< TRI->getNumRegClasses(); i++) { |
| 1701 | const TargetRegisterClass *RC = TRI->getRegClass(i); |
| 1702 | DenseMap<unsigned, unsigned> ®map = VRegMapping[RC]; |
| 1703 | std::string rcname = getNVPTXRegClassName(RC); |
| 1704 | std::string rcStr = getNVPTXRegClassStr(RC); |
| 1705 | int n = regmap.size(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1706 | |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1707 | // 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1713 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1714 | OutStreamer->EmitRawText(O.str()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1717 | void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1718 | APFloat APF = APFloat(Fp->getValueAPF()); // make a copy |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1719 | bool ignored; |
| 1720 | unsigned int numHex; |
| 1721 | const char *lead; |
| 1722 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1723 | if (Fp->getType()->getTypeID() == Type::FloatTyID) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1724 | numHex = 8; |
| 1725 | lead = "0f"; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1726 | APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &ignored); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1727 | } else if (Fp->getType()->getTypeID() == Type::DoubleTyID) { |
| 1728 | numHex = 16; |
| 1729 | lead = "0d"; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1730 | APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &ignored); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1731 | } else |
| 1732 | llvm_unreachable("unsupported fp type"); |
| 1733 | |
| 1734 | APInt API = APF.bitcastToAPInt(); |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 1735 | O << lead << format_hex_no_prefix(API.getZExtValue(), numHex, /*Upper=*/true); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1738 | void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) { |
| 1739 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1740 | O << CI->getValue(); |
| 1741 | return; |
| 1742 | } |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1743 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1744 | printFPConstant(CFP, O); |
| 1745 | return; |
| 1746 | } |
| 1747 | if (isa<ConstantPointerNull>(CPV)) { |
| 1748 | O << "0"; |
| 1749 | return; |
| 1750 | } |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1751 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1752 | bool IsNonGenericPointer = false; |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1753 | if (GVar->getType()->getAddressSpace() != 0) { |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1754 | IsNonGenericPointer = true; |
| 1755 | } |
| 1756 | if (EmitGeneric && !isa<Function>(CPV) && !IsNonGenericPointer) { |
| 1757 | O << "generic("; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1758 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1759 | O << ")"; |
| 1760 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1761 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1762 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1763 | return; |
| 1764 | } |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1765 | if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
| 1766 | const Value *v = Cexpr->stripPointerCasts(); |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1767 | PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType()); |
| 1768 | bool IsNonGenericPointer = false; |
| 1769 | if (PTy && PTy->getAddressSpace() != 0) { |
| 1770 | IsNonGenericPointer = true; |
| 1771 | } |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1772 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) { |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1773 | if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) { |
| 1774 | O << "generic("; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1775 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1776 | O << ")"; |
| 1777 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1778 | getSymbol(GVar)->print(O, MAI); |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1779 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1782 | lowerConstant(CPV)->print(O, MAI); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1783 | return; |
| 1784 | } |
| 1785 | } |
| 1786 | llvm_unreachable("Not scalar type found in printScalarConstant()"); |
| 1787 | } |
| 1788 | |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1789 | // These utility functions assure we get the right sequence of bytes for a given |
| 1790 | // type even for big-endian machines |
| 1791 | template <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 | } |
| 1798 | static 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 | } |
| 1805 | static 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 Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1813 | void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1814 | AggBuffer *aggBuffer) { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1815 | const DataLayout &DL = getDataLayout(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1816 | |
| 1817 | if (isa<UndefValue>(CPV) || CPV->isNullValue()) { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1818 | int s = DL.getTypeAllocSize(CPV->getType()); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1819 | if (s < Bytes) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1820 | s = Bytes; |
| 1821 | aggBuffer->addZeros(s); |
| 1822 | return; |
| 1823 | } |
| 1824 | |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1825 | unsigned char ptr[8]; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1826 | switch (CPV->getType()->getTypeID()) { |
| 1827 | |
| 1828 | case Type::IntegerTyID: { |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1829 | Type *ETy = CPV->getType(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1830 | if (ETy == Type::getInt8Ty(CPV->getContext())) { |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 1831 | unsigned char c = (unsigned char)cast<ConstantInt>(CPV)->getZExtValue(); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1832 | ConvertIntToBytes<>(ptr, c); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1833 | aggBuffer->addBytes(ptr, 1, Bytes); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1834 | } else if (ETy == Type::getInt16Ty(CPV->getContext())) { |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 1835 | short int16 = (short)cast<ConstantInt>(CPV)->getZExtValue(); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1836 | ConvertIntToBytes<>(ptr, int16); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1837 | aggBuffer->addBytes(ptr, 2, Bytes); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1838 | } else if (ETy == Type::getInt32Ty(CPV->getContext())) { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1839 | if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1840 | int int32 = (int)(constInt->getZExtValue()); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1841 | ConvertIntToBytes<>(ptr, int32); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1842 | aggBuffer->addBytes(ptr, 4, Bytes); |
| 1843 | break; |
David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 1844 | } else if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
| 1845 | if (const auto *constInt = dyn_cast_or_null<ConstantInt>( |
| 1846 | ConstantFoldConstant(Cexpr, DL))) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1847 | int int32 = (int)(constInt->getZExtValue()); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1848 | ConvertIntToBytes<>(ptr, int32); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1849 | aggBuffer->addBytes(ptr, 4, Bytes); |
| 1850 | break; |
| 1851 | } |
| 1852 | if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
| 1853 | Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1854 | aggBuffer->addSymbol(v, Cexpr->getOperand(0)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1855 | aggBuffer->addZeros(4); |
| 1856 | break; |
| 1857 | } |
| 1858 | } |
Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 1859 | llvm_unreachable("unsupported integer const type"); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1860 | } else if (ETy == Type::getInt64Ty(CPV->getContext())) { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1861 | if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1862 | long long int64 = (long long)(constInt->getZExtValue()); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1863 | ConvertIntToBytes<>(ptr, int64); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1864 | aggBuffer->addBytes(ptr, 8, Bytes); |
| 1865 | break; |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1866 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 1867 | if (const auto *constInt = dyn_cast_or_null<ConstantInt>( |
| 1868 | ConstantFoldConstant(Cexpr, DL))) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1869 | long long int64 = (long long)(constInt->getZExtValue()); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1870 | ConvertIntToBytes<>(ptr, int64); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1871 | aggBuffer->addBytes(ptr, 8, Bytes); |
| 1872 | break; |
| 1873 | } |
| 1874 | if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
| 1875 | Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1876 | aggBuffer->addSymbol(v, Cexpr->getOperand(0)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1877 | aggBuffer->addZeros(8); |
| 1878 | break; |
| 1879 | } |
| 1880 | } |
| 1881 | llvm_unreachable("unsupported integer const type"); |
Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 1882 | } else |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1883 | llvm_unreachable("unsupported integer const type"); |
| 1884 | break; |
| 1885 | } |
Artem Belevich | f256dec | 2018-04-06 22:25:08 +0000 | [diff] [blame] | 1886 | case Type::HalfTyID: |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1887 | case Type::FloatTyID: |
| 1888 | case Type::DoubleTyID: { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1889 | const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV); |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1890 | Type *Ty = CFP->getType(); |
Artem Belevich | f256dec | 2018-04-06 22:25:08 +0000 | [diff] [blame] | 1891 | 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1897 | float float32 = (float) CFP->getValueAPF().convertToFloat(); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1898 | ConvertFloatToBytes(ptr, float32); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1899 | aggBuffer->addBytes(ptr, 4, Bytes); |
| 1900 | } else if (Ty == Type::getDoubleTy(CPV->getContext())) { |
| 1901 | double float64 = CFP->getValueAPF().convertToDouble(); |
Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1902 | ConvertDoubleToBytes(ptr, float64); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1903 | aggBuffer->addBytes(ptr, 8, Bytes); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1904 | } else { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1905 | llvm_unreachable("unsupported fp const type"); |
| 1906 | } |
| 1907 | break; |
| 1908 | } |
| 1909 | case Type::PointerTyID: { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1910 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1911 | aggBuffer->addSymbol(GVar, GVar); |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1912 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
| 1913 | const Value *v = Cexpr->stripPointerCasts(); |
Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1914 | aggBuffer->addSymbol(v, Cexpr); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1915 | } |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1916 | unsigned int s = DL.getTypeAllocSize(CPV->getType()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1917 | aggBuffer->addZeros(s); |
| 1918 | break; |
| 1919 | } |
| 1920 | |
| 1921 | case Type::ArrayTyID: |
| 1922 | case Type::VectorTyID: |
| 1923 | case Type::StructTyID: { |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 1924 | if (isa<ConstantAggregate>(CPV) || isa<ConstantDataSequential>(CPV)) { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1925 | int ElementSize = DL.getTypeAllocSize(CPV->getType()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1926 | bufferAggregateConstant(CPV, aggBuffer); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1927 | if (Bytes > ElementSize) |
| 1928 | aggBuffer->addZeros(Bytes - ElementSize); |
| 1929 | } else if (isa<ConstantAggregateZero>(CPV)) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1930 | 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 Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1941 | void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV, |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1942 | AggBuffer *aggBuffer) { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1943 | const DataLayout &DL = getDataLayout(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1944 | int Bytes; |
| 1945 | |
Justin Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 1946 | // 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 Topper | fc947bc | 2017-04-18 17:14:21 +0000 | [diff] [blame] | 1952 | Val.lshrInPlace(8); |
Justin Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 1953 | } |
| 1954 | return; |
| 1955 | } |
| 1956 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1957 | // 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1966 | dyn_cast<ConstantDataSequential>(CPV)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1967 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1974 | 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 Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1978 | if (i == (e - 1)) |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1979 | Bytes = DL.getStructLayout(ST)->getElementOffset(0) + |
| 1980 | DL.getTypeAllocSize(ST) - |
| 1981 | DL.getStructLayout(ST)->getElementOffset(i); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1982 | else |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1983 | Bytes = DL.getStructLayout(ST)->getElementOffset(i + 1) - |
| 1984 | DL.getStructLayout(ST)->getElementOffset(i); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1985 | bufferLEByte(cast<Constant>(CPV->getOperand(i)), Bytes, aggBuffer); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | return; |
| 1989 | } |
Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 1990 | llvm_unreachable("unsupported constant type in printAggregateConstant()"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 1993 | /// 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. |
| 1997 | const MCExpr * |
| 1998 | NVPTXAsmPrinter::lowerConstantForGV(const Constant *CV, bool ProcessingGeneric) { |
| 1999 | MCContext &Ctx = OutContext; |
| 2000 | |
| 2001 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2002 | return MCConstantExpr::create(0, Ctx); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2003 | |
| 2004 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2005 | return MCConstantExpr::create(CI->getZExtValue(), Ctx); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2006 | |
| 2007 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
| 2008 | const MCSymbolRefExpr *Expr = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2009 | MCSymbolRefExpr::create(getSymbol(GV), Ctx); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2010 | if (ProcessingGeneric) { |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2011 | return NVPTXGenericMCSymbolRefExpr::create(Expr, Ctx); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2012 | } 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 Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 2027 | if (Constant *C = ConstantFoldConstant(CE, getDataLayout())) |
| 2028 | if (C && C != CE) |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2029 | 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 Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2037 | !MF ? nullptr : MF->getFunction().getParent()); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2038 | 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 Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2051 | !MF ? nullptr : MF->getFunction().getParent()); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2052 | report_fatal_error(OS.str()); |
| 2053 | } |
| 2054 | |
| 2055 | case Instruction::GetElementPtr: { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2056 | const DataLayout &DL = getDataLayout(); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2057 | |
| 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 Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2068 | return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx), |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2069 | 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 Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 2077 | LLVM_FALLTHROUGH; |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2078 | case Instruction::BitCast: |
| 2079 | return lowerConstantForGV(CE->getOperand(0), ProcessingGeneric); |
| 2080 | |
| 2081 | case Instruction::IntToPtr: { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2082 | const DataLayout &DL = getDataLayout(); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2083 | |
| 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 Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2093 | const DataLayout &DL = getDataLayout(); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2094 | |
| 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 Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2111 | const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx); |
| 2112 | return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2113 | } |
| 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 Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2122 | case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | // Copy of MCExpr::print customized for NVPTX |
| 2129 | void NVPTXAsmPrinter::printMCExpr(const MCExpr &Expr, raw_ostream &OS) { |
| 2130 | switch (Expr.getKind()) { |
| 2131 | case MCExpr::Target: |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2132 | return cast<MCTargetExpr>(&Expr)->printImpl(OS, MAI); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2133 | 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 Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2140 | Sym.print(OS, MAI); |
Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2141 | 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 Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2199 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 2200 | /// |
| 2201 | bool 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 | |
| 2222 | bool 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 | |
| 2235 | void 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 Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 2246 | emitVirtualRegister(MO.getReg(), O); |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2247 | } |
| 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 Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2265 | getSymbol(MO.getGlobal())->print(O, MAI); |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2266 | break; |
| 2267 | |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2268 | case MachineOperand::MO_MachineBasicBlock: |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2269 | MO.getMBB()->getSymbol()->print(O, MAI); |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2270 | return; |
| 2271 | |
| 2272 | default: |
| 2273 | llvm_unreachable("Operand type not supported."); |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | void NVPTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 2278 | raw_ostream &O, const char *Modifier) { |
| 2279 | printOperand(MI, opNum, O); |
| 2280 | |
Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 2281 | if (Modifier && strcmp(Modifier, "add") == 0) { |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2282 | 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 Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2293 | // Force static initialization. |
| 2294 | extern "C" void LLVMInitializeNVPTXAsmPrinter() { |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 2295 | RegisterAsmPrinter<NVPTXAsmPrinter> X(getTheNVPTXTarget32()); |
| 2296 | RegisterAsmPrinter<NVPTXAsmPrinter> Y(getTheNVPTXTarget64()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2297 | } |