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