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