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