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