Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===-- NVPTXAsmPrinter.h - 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H |
| 16 | #define LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 17 | |
| 18 | #include "NVPTX.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 19 | #include "NVPTXSubtarget.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 20 | #include "NVPTXTargetMachine.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/AsmPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCAsmInfo.h" |
| 26 | #include "llvm/MC/MCExpr.h" |
| 27 | #include "llvm/MC/MCSymbol.h" |
| 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 31 | #include <fstream> |
| 32 | |
| 33 | // The ptx syntax and format is very different from that usually seem in a .s |
| 34 | // file, |
| 35 | // therefore we are not able to use the MCAsmStreamer interface here. |
| 36 | // |
| 37 | // We are handcrafting the output method here. |
| 38 | // |
| 39 | // A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer |
| 40 | // (subclass of MCStreamer). |
| 41 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 42 | namespace llvm { |
| 43 | |
| 44 | class LineReader { |
| 45 | private: |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 46 | unsigned theCurLine; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 47 | std::ifstream fstr; |
| 48 | char buff[512]; |
| 49 | std::string theFileName; |
| 50 | SmallVector<unsigned, 32> lineOffset; |
| 51 | public: |
| 52 | LineReader(std::string filename) { |
| 53 | theCurLine = 0; |
| 54 | fstr.open(filename.c_str()); |
| 55 | theFileName = filename; |
| 56 | } |
| 57 | std::string fileName() { return theFileName; } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 58 | ~LineReader() { fstr.close(); } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 59 | std::string readLine(unsigned line); |
| 60 | }; |
| 61 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 62 | class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter { |
| 63 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 64 | class AggBuffer { |
| 65 | // Used to buffer the emitted string for initializing global |
| 66 | // aggregates. |
| 67 | // |
| 68 | // Normally an aggregate (array, vector or structure) is emitted |
| 69 | // as a u8[]. However, if one element/field of the aggregate |
| 70 | // is a non-NULL address, then the aggregate is emitted as u32[] |
| 71 | // or u64[]. |
| 72 | // |
| 73 | // We first layout the aggregate in 'buffer' in bytes, except for |
| 74 | // those symbol addresses. For the i-th symbol address in the |
| 75 | //aggregate, its corresponding 4-byte or 8-byte elements in 'buffer' |
| 76 | // are filled with 0s. symbolPosInBuffer[i-1] records its position |
| 77 | // in 'buffer', and Symbols[i-1] records the Value*. |
| 78 | // |
| 79 | // Once we have this AggBuffer setup, we can choose how to print |
| 80 | // it out. |
| 81 | public: |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 82 | unsigned numSymbols; // number of symbol addresses |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
Dylan Noblesmith | 802b6ce | 2014-08-25 01:59:29 +0000 | [diff] [blame] | 85 | const unsigned size; // size of the buffer in bytes |
| 86 | std::vector<unsigned char> buffer; // the buffer |
| 87 | SmallVector<unsigned, 4> symbolPosInBuffer; |
| 88 | SmallVector<const Value *, 4> Symbols; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 89 | unsigned curpos; |
| 90 | raw_ostream &O; |
| 91 | NVPTXAsmPrinter &AP; |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 92 | bool EmitGeneric; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 93 | |
| 94 | public: |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame^] | 95 | AggBuffer(unsigned size, raw_ostream &O, NVPTXAsmPrinter &AP) |
| 96 | : size(size), buffer(size), O(O), AP(AP) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 97 | curpos = 0; |
| 98 | numSymbols = 0; |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 99 | EmitGeneric = AP.EmitGeneric; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 100 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 101 | unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 102 | assert((curpos + Num) <= size); |
| 103 | assert((curpos + Bytes) <= size); |
| 104 | for (int i = 0; i < Num; ++i) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 105 | buffer[curpos] = Ptr[i]; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 106 | curpos++; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 107 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 108 | for (int i = Num; i < Bytes; ++i) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 109 | buffer[curpos] = 0; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 110 | curpos++; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 111 | } |
| 112 | return curpos; |
| 113 | } |
| 114 | unsigned addZeros(int Num) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 115 | assert((curpos + Num) <= size); |
| 116 | for (int i = 0; i < Num; ++i) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 117 | buffer[curpos] = 0; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 118 | curpos++; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 119 | } |
| 120 | return curpos; |
| 121 | } |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 122 | void addSymbol(const Value *GVar) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 123 | symbolPosInBuffer.push_back(curpos); |
| 124 | Symbols.push_back(GVar); |
| 125 | numSymbols++; |
| 126 | } |
| 127 | void print() { |
| 128 | if (numSymbols == 0) { |
| 129 | // print out in bytes |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 130 | for (unsigned i = 0; i < size; i++) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 131 | if (i) |
| 132 | O << ", "; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 133 | O << (unsigned int) buffer[i]; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 134 | } |
Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 135 | } else { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 136 | // print out in 4-bytes or 8-bytes |
| 137 | unsigned int pos = 0; |
| 138 | unsigned int nSym = 0; |
| 139 | unsigned int nextSymbolPos = symbolPosInBuffer[nSym]; |
| 140 | unsigned int nBytes = 4; |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 141 | if (static_cast<const NVPTXTargetMachine &>(AP.TM).is64Bit()) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 142 | nBytes = 8; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 143 | for (pos = 0; pos < size; pos += nBytes) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 144 | if (pos) |
| 145 | O << ", "; |
| 146 | if (pos == nextSymbolPos) { |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 147 | const Value *v = Symbols[nSym]; |
| 148 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) { |
Rafael Espindola | 79858aa | 2013-10-29 17:07:16 +0000 | [diff] [blame] | 149 | MCSymbol *Name = AP.getSymbol(GVar); |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 150 | PointerType *PTy = dyn_cast<PointerType>(GVar->getType()); |
| 151 | bool IsNonGenericPointer = false; |
| 152 | if (PTy && PTy->getAddressSpace() != 0) { |
| 153 | IsNonGenericPointer = true; |
| 154 | } |
| 155 | if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) { |
| 156 | O << "generic("; |
| 157 | O << *Name; |
| 158 | O << ")"; |
| 159 | } else { |
| 160 | O << *Name; |
| 161 | } |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 162 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) { |
Matt Arsenault | 31a52ad | 2014-12-16 19:16:17 +0000 | [diff] [blame] | 163 | O << *AP.lowerConstant(Cexpr); |
Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 164 | } else |
| 165 | llvm_unreachable("symbol type unknown"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 166 | nSym++; |
| 167 | if (nSym >= numSymbols) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 168 | nextSymbolPos = size + 1; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 169 | else |
| 170 | nextSymbolPos = symbolPosInBuffer[nSym]; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 171 | } else if (nBytes == 4) |
Dylan Noblesmith | 802b6ce | 2014-08-25 01:59:29 +0000 | [diff] [blame] | 172 | O << *(unsigned int *)(&buffer[pos]); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 173 | else |
Dylan Noblesmith | 802b6ce | 2014-08-25 01:59:29 +0000 | [diff] [blame] | 174 | O << *(unsigned long long *)(&buffer[pos]); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | friend class AggBuffer; |
| 181 | |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 182 | void emitSrcInText(StringRef filename, unsigned line); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 183 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 184 | private: |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 185 | const char *getPassName() const override { return "NVPTX Assembly Printer"; } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 186 | |
| 187 | const Function *F; |
| 188 | std::string CurrentFnName; |
| 189 | |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 190 | void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override; |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 191 | void EmitFunctionEntryLabel() override; |
| 192 | void EmitFunctionBodyStart() override; |
| 193 | void EmitFunctionBodyEnd() override; |
| 194 | void emitImplicitDef(const MachineInstr *MI) const override; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 195 | |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 196 | void EmitInstruction(const MachineInstr *) override; |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 197 | void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI); |
| 198 | bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp); |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 199 | MCOperand GetSymbolRef(const MCSymbol *Symbol); |
Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 200 | unsigned encodeVirtualRegister(unsigned Reg); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 201 | |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 202 | void EmitAlignment(unsigned NumBits, const GlobalValue *GV = nullptr) const {} |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 203 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 204 | void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier, |
| 205 | raw_ostream &O); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 206 | void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O, |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 207 | const char *Modifier = nullptr); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 208 | void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const; |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 209 | void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O, |
| 210 | bool = false); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 211 | void printParamName(int paramIndex, raw_ostream &O); |
| 212 | void printParamName(Function::const_arg_iterator I, int paramIndex, |
| 213 | raw_ostream &O); |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 214 | void emitGlobals(const Module &M); |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 215 | void emitHeader(Module &M, raw_ostream &O, const NVPTXSubtarget &STI); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 216 | void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const; |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 217 | void emitVirtualRegister(unsigned int vr, raw_ostream &); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 218 | void emitFunctionExternParamList(const MachineFunction &MF); |
| 219 | void emitFunctionParamList(const Function *, raw_ostream &O); |
| 220 | void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O); |
| 221 | void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 222 | void emitFunctionTempData(const MachineFunction &MF, unsigned &FrameSize); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 223 | bool isImageType(const Type *Ty); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 224 | void printReturnValStr(const Function *, raw_ostream &O); |
| 225 | void printReturnValStr(const MachineFunction &MF, raw_ostream &O); |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 226 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 227 | unsigned AsmVariant, const char *ExtraCode, |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 228 | raw_ostream &) override; |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 229 | void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O, |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 230 | const char *Modifier = nullptr); |
Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 231 | bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 232 | unsigned AsmVariant, const char *ExtraCode, |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 233 | raw_ostream &) override; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 234 | protected: |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 235 | bool doInitialization(Module &M) override; |
| 236 | bool doFinalization(Module &M) override; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 237 | |
| 238 | private: |
| 239 | std::string CurrentBankselLabelInBasicBlock; |
| 240 | |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 241 | bool GlobalsEmitted; |
| 242 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 243 | // This is specific per MachineFunction. |
| 244 | const MachineRegisterInfo *MRI; |
| 245 | // The contents are specific for each |
| 246 | // MachineFunction. But the size of the |
| 247 | // array is not. |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 248 | typedef DenseMap<unsigned, unsigned> VRegMap; |
| 249 | typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap; |
| 250 | VRegRCMap VRegMapping; |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 251 | |
| 252 | // Cache the subtarget here. |
| 253 | const NVPTXSubtarget *nvptxSubtarget; |
| 254 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 255 | // Build the map between type name and ID based on module's type |
| 256 | // symbol table. |
| 257 | std::map<const Type *, std::string> TypeNameMap; |
| 258 | |
| 259 | // List of variables demoted to a function scope. |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 260 | std::map<const Function *, std::vector<const GlobalVariable *> > localDecls; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 261 | |
| 262 | // To record filename to ID mapping |
| 263 | std::map<std::string, unsigned> filenameMap; |
| 264 | void recordAndEmitFilenames(Module &); |
| 265 | |
| 266 | void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 267 | void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const; |
| 268 | std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const; |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 269 | void printScalarConstant(const Constant *CPV, raw_ostream &O); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 270 | void printFPConstant(const ConstantFP *Fp, raw_ostream &O); |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 271 | void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer); |
| 272 | void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 273 | |
| 274 | void printOperandProper(const MachineOperand &MO); |
| 275 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 276 | void emitLinkageDirective(const GlobalValue *V, raw_ostream &O); |
Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 277 | void emitDeclarations(const Module &, raw_ostream &O); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 278 | void emitDeclaration(const Function *, raw_ostream &O); |
| 279 | |
| 280 | static const char *getRegisterName(unsigned RegNo); |
| 281 | void emitDemotedVars(const Function *, raw_ostream &); |
| 282 | |
Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 283 | bool lowerImageHandleOperand(const MachineInstr *MI, unsigned OpNo, |
| 284 | MCOperand &MCOp); |
| 285 | void lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp); |
| 286 | |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 287 | bool isLoopHeaderOfNoUnroll(const MachineBasicBlock &MBB) const; |
| 288 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 289 | LineReader *reader; |
| 290 | LineReader *getReader(std::string); |
Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 291 | |
| 292 | // Used to control the need to emit .generic() in the initializer of |
| 293 | // module scope variables. |
| 294 | // Although ptx supports the hybrid mode like the following, |
| 295 | // .global .u32 a; |
| 296 | // .global .u32 b; |
| 297 | // .global .u32 addr[] = {a, generic(b)} |
| 298 | // we have difficulty representing the difference in the NVVM IR. |
| 299 | // |
| 300 | // Since the address value should always be generic in CUDA C and always |
| 301 | // be specific in OpenCL, we use this simple control here. |
| 302 | // |
| 303 | bool EmitGeneric; |
| 304 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 305 | public: |
David Blaikie | 9459832 | 2015-01-18 20:29:04 +0000 | [diff] [blame] | 306 | NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) |
| 307 | : AsmPrinter(TM, std::move(Streamer)), |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 308 | EmitGeneric(static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == |
| 309 | NVPTX::CUDA) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 310 | CurrentBankselLabelInBasicBlock = ""; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 311 | reader = nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | ~NVPTXAsmPrinter() { |
| 315 | if (!reader) |
| 316 | delete reader; |
| 317 | } |
| 318 | |
Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 319 | bool runOnMachineFunction(MachineFunction &F) override { |
| 320 | nvptxSubtarget = &F.getSubtarget<NVPTXSubtarget>(); |
| 321 | return AsmPrinter::runOnMachineFunction(F); |
| 322 | } |
Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 323 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 324 | AU.addRequired<MachineLoopInfo>(); |
| 325 | AsmPrinter::getAnalysisUsage(AU); |
| 326 | } |
| 327 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 328 | bool ignoreLoc(const MachineInstr &); |
| 329 | |
Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 330 | std::string getVirtualRegisterName(unsigned) const; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 331 | |
| 332 | DebugLoc prevDebugLoc; |
| 333 | void emitLineNumberAsDotLoc(const MachineInstr &); |
| 334 | }; |
| 335 | } // end of namespace |
| 336 | |
| 337 | #endif |