1 /*
2  * Copyright 2011 Christoph Bumiller
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "codegen/nv50_ir_target_nvc0.h"
24 
25 namespace nv50_ir {
26 
getTargetNVC0(unsigned int chipset)27 Target *getTargetNVC0(unsigned int chipset)
28 {
29    return new TargetNVC0(chipset);
30 }
31 
TargetNVC0(unsigned int card)32 TargetNVC0::TargetNVC0(unsigned int card) :
33    Target(card < 0x110, false, card >= 0xe4 && card < 0x140)
34 {
35    chipset = card;
36    initOpInfo();
37 }
38 
39 // BULTINS / LIBRARY FUNCTIONS:
40 
41 // lazyness -> will just hardcode everything for the time being
42 
43 #include "lib/gf100.asm.h"
44 #include "lib/gk104.asm.h"
45 #include "lib/gk110.asm.h"
46 
47 void
getBuiltinCode(const uint32_t ** code,uint32_t * size) const48 TargetNVC0::getBuiltinCode(const uint32_t **code, uint32_t *size) const
49 {
50    switch (chipset & ~0xf) {
51    case 0xe0:
52       if (chipset < NVISA_GK20A_CHIPSET) {
53          *code = (const uint32_t *)&gk104_builtin_code[0];
54          *size = sizeof(gk104_builtin_code);
55          break;
56       }
57       FALLTHROUGH; /* for GK20A */
58    case 0xf0:
59    case 0x100:
60       *code = (const uint32_t *)&gk110_builtin_code[0];
61       *size = sizeof(gk110_builtin_code);
62       break;
63    default:
64       *code = (const uint32_t *)&gf100_builtin_code[0];
65       *size = sizeof(gf100_builtin_code);
66       break;
67    }
68 }
69 
70 uint32_t
getBuiltinOffset(int builtin) const71 TargetNVC0::getBuiltinOffset(int builtin) const
72 {
73    assert(builtin < NVC0_BUILTIN_COUNT);
74 
75    switch (chipset & ~0xf) {
76    case 0xe0:
77       if (chipset < NVISA_GK20A_CHIPSET)
78          return gk104_builtin_offsets[builtin];
79       FALLTHROUGH; /* for GK20A */
80    case 0xf0:
81    case 0x100:
82       return gk110_builtin_offsets[builtin];
83    default:
84       return gf100_builtin_offsets[builtin];
85    }
86 }
87 
88 struct nvc0_opProperties
89 {
90    operation op;
91    unsigned int mNeg   : 4;
92    unsigned int mAbs   : 4;
93    unsigned int mNot   : 4;
94    unsigned int mSat   : 4;
95    unsigned int fConst : 3;
96    unsigned int fImmd  : 4; // last bit indicates if full immediate is suppoted
97 };
98 
99 static const struct nvc0_opProperties _initProps[] =
100 {
101    //           neg  abs  not  sat  c[]  imm
102    { OP_ADD,    0x3, 0x3, 0x0, 0x8, 0x2, 0x2 | 0x8 },
103    { OP_SUB,    0x3, 0x3, 0x0, 0x0, 0x2, 0x2 | 0x8 },
104    { OP_MUL,    0x3, 0x0, 0x0, 0x8, 0x2, 0x2 | 0x8 },
105    { OP_MAX,    0x3, 0x3, 0x0, 0x0, 0x2, 0x2 },
106    { OP_MIN,    0x3, 0x3, 0x0, 0x0, 0x2, 0x2 },
107    { OP_MAD,    0x7, 0x0, 0x0, 0x8, 0x6, 0x2 }, // special c[] constraint
108    { OP_FMA,    0x7, 0x0, 0x0, 0x8, 0x6, 0x2 }, // keep the same as OP_MAD
109    { OP_SHLADD, 0x5, 0x0, 0x0, 0x0, 0x4, 0x6 },
110    { OP_MADSP,  0x0, 0x0, 0x0, 0x0, 0x6, 0x2 },
111    { OP_ABS,    0x0, 0x0, 0x0, 0x0, 0x1, 0x0 },
112    { OP_NEG,    0x0, 0x1, 0x0, 0x0, 0x1, 0x0 },
113    { OP_CVT,    0x1, 0x1, 0x0, 0x8, 0x1, 0x0 },
114    { OP_CEIL,   0x1, 0x1, 0x0, 0x8, 0x1, 0x0 },
115    { OP_FLOOR,  0x1, 0x1, 0x0, 0x8, 0x1, 0x0 },
116    { OP_TRUNC,  0x1, 0x1, 0x0, 0x8, 0x1, 0x0 },
117    { OP_AND,    0x0, 0x0, 0x3, 0x0, 0x2, 0x2 | 0x8 },
118    { OP_OR,     0x0, 0x0, 0x3, 0x0, 0x2, 0x2 | 0x8 },
119    { OP_XOR,    0x0, 0x0, 0x3, 0x0, 0x2, 0x2 | 0x8 },
120    { OP_SHL,    0x0, 0x0, 0x0, 0x0, 0x2, 0x2 },
121    { OP_SHR,    0x0, 0x0, 0x0, 0x0, 0x2, 0x2 },
122    { OP_SET,    0x3, 0x3, 0x0, 0x0, 0x2, 0x2 },
123    { OP_SLCT,   0x4, 0x0, 0x0, 0x0, 0x6, 0x2 }, // special c[] constraint
124    { OP_PREEX2, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1 },
125    { OP_PRESIN, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1 },
126    { OP_COS,    0x1, 0x1, 0x0, 0x8, 0x0, 0x0 },
127    { OP_SIN,    0x1, 0x1, 0x0, 0x8, 0x0, 0x0 },
128    { OP_EX2,    0x1, 0x1, 0x0, 0x8, 0x0, 0x0 },
129    { OP_LG2,    0x1, 0x1, 0x0, 0x8, 0x0, 0x0 },
130    { OP_RCP,    0x1, 0x1, 0x0, 0x8, 0x0, 0x0 },
131    { OP_RSQ,    0x1, 0x1, 0x0, 0x8, 0x0, 0x0 },
132    { OP_SQRT,   0x1, 0x1, 0x0, 0x8, 0x0, 0x0 },
133    { OP_DFDX,   0x1, 0x0, 0x0, 0x0, 0x0, 0x0 },
134    { OP_DFDY,   0x1, 0x0, 0x0, 0x0, 0x0, 0x0 },
135    { OP_CALL,   0x0, 0x0, 0x0, 0x0, 0x1, 0x0 },
136    { OP_POPCNT, 0x0, 0x0, 0x3, 0x0, 0x2, 0x2 },
137    { OP_INSBF,  0x0, 0x0, 0x0, 0x0, 0x6, 0x2 },
138    { OP_EXTBF,  0x0, 0x0, 0x0, 0x0, 0x2, 0x2 },
139    { OP_BFIND,  0x0, 0x0, 0x1, 0x0, 0x1, 0x1 },
140    { OP_PERMT,  0x0, 0x0, 0x0, 0x0, 0x6, 0x2 },
141    { OP_SET_AND, 0x3, 0x3, 0x0, 0x0, 0x2, 0x2 },
142    { OP_SET_OR, 0x3, 0x3, 0x0, 0x0, 0x2, 0x2 },
143    { OP_SET_XOR, 0x3, 0x3, 0x0, 0x0, 0x2, 0x2 },
144    // saturate only:
145    { OP_LINTERP, 0x0, 0x0, 0x0, 0x8, 0x0, 0x0 },
146    { OP_PINTERP, 0x0, 0x0, 0x0, 0x8, 0x0, 0x0 },
147 };
148 
149 static const struct nvc0_opProperties _initPropsNVE4[] = {
150    { OP_SULDB,   0x0, 0x0, 0x0, 0x0, 0x2, 0x0 },
151    { OP_SUSTB,   0x0, 0x0, 0x0, 0x0, 0x2, 0x0 },
152    { OP_SUSTP,   0x0, 0x0, 0x0, 0x0, 0x2, 0x0 },
153    { OP_SUCLAMP, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2 },
154    { OP_SUBFM,   0x0, 0x0, 0x0, 0x0, 0x6, 0x2 },
155    { OP_SUEAU,   0x0, 0x0, 0x0, 0x0, 0x6, 0x2 }
156 };
157 
158 static const struct nvc0_opProperties _initPropsGM107[] = {
159    { OP_SULDB,   0x0, 0x0, 0x0, 0x0, 0x0, 0x2 },
160    { OP_SULDP,   0x0, 0x0, 0x0, 0x0, 0x0, 0x2 },
161    { OP_SUSTB,   0x0, 0x0, 0x0, 0x0, 0x0, 0x4 },
162    { OP_SUSTP,   0x0, 0x0, 0x0, 0x0, 0x0, 0x4 },
163    { OP_SUREDB,  0x0, 0x0, 0x0, 0x0, 0x0, 0x4 },
164    { OP_SUREDP,  0x0, 0x0, 0x0, 0x0, 0x0, 0x4 },
165    { OP_XMAD,    0x0, 0x0, 0x0, 0x0, 0x6, 0x2 },
166 };
167 
initProps(const struct nvc0_opProperties * props,int size)168 void TargetNVC0::initProps(const struct nvc0_opProperties *props, int size)
169 {
170    for (int i = 0; i < size; ++i) {
171       const struct nvc0_opProperties *prop = &props[i];
172 
173       for (int s = 0; s < 3; ++s) {
174          if (prop->mNeg & (1 << s))
175             opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NEG;
176          if (prop->mAbs & (1 << s))
177             opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_ABS;
178          if (prop->mNot & (1 << s))
179             opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NOT;
180          if (prop->fConst & (1 << s))
181             opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_MEMORY_CONST;
182          if (prop->fImmd & (1 << s))
183             opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_IMMEDIATE;
184          if (prop->fImmd & 8)
185             opInfo[prop->op].immdBits = 0xffffffff;
186       }
187       if (prop->mSat & 8)
188          opInfo[prop->op].dstMods = NV50_IR_MOD_SAT;
189    }
190 }
191 
initOpInfo()192 void TargetNVC0::initOpInfo()
193 {
194    unsigned int i, j;
195 
196    static const operation commutative[] =
197    {
198       OP_ADD, OP_MUL, OP_MAD, OP_FMA, OP_AND, OP_OR, OP_XOR, OP_MAX, OP_MIN,
199       OP_SET_AND, OP_SET_OR, OP_SET_XOR, OP_SET, OP_SELP, OP_SLCT
200    };
201 
202    static const operation shortForm[] =
203    {
204       OP_ADD, OP_MUL, OP_MAD, OP_FMA, OP_AND, OP_OR, OP_XOR, OP_MAX, OP_MIN
205    };
206 
207    static const operation noDest[] =
208    {
209       OP_STORE, OP_WRSV, OP_EXPORT, OP_BRA, OP_CALL, OP_RET, OP_EXIT,
210       OP_DISCARD, OP_CONT, OP_BREAK, OP_PRECONT, OP_PREBREAK, OP_PRERET,
211       OP_JOIN, OP_JOINAT, OP_BRKPT, OP_MEMBAR, OP_EMIT, OP_RESTART,
212       OP_QUADON, OP_QUADPOP, OP_TEXBAR, OP_SUSTB, OP_SUSTP, OP_SUREDP,
213       OP_SUREDB, OP_BAR
214    };
215 
216    static const operation noPred[] =
217    {
218       OP_CALL, OP_PRERET, OP_QUADON, OP_QUADPOP,
219       OP_JOINAT, OP_PREBREAK, OP_PRECONT, OP_BRKPT
220    };
221 
222    for (i = 0; i < DATA_FILE_COUNT; ++i)
223       nativeFileMap[i] = (DataFile)i;
224    nativeFileMap[FILE_ADDRESS] = FILE_GPR;
225 
226    for (i = 0; i < OP_LAST; ++i) {
227       opInfo[i].variants = NULL;
228       opInfo[i].op = (operation)i;
229       opInfo[i].srcTypes = 1 << (int)TYPE_F32;
230       opInfo[i].dstTypes = 1 << (int)TYPE_F32;
231       opInfo[i].immdBits = 0;
232       opInfo[i].srcNr = operationSrcNr[i];
233 
234       for (j = 0; j < opInfo[i].srcNr; ++j) {
235          opInfo[i].srcMods[j] = 0;
236          opInfo[i].srcFiles[j] = 1 << (int)FILE_GPR;
237       }
238       opInfo[i].dstMods = 0;
239       opInfo[i].dstFiles = 1 << (int)FILE_GPR;
240 
241       opInfo[i].hasDest = 1;
242       opInfo[i].vector = (i >= OP_TEX && i <= OP_TEXCSAA);
243       opInfo[i].commutative = false; /* set below */
244       opInfo[i].pseudo = (i < OP_MOV);
245       opInfo[i].predicate = !opInfo[i].pseudo;
246       opInfo[i].flow = (i >= OP_BRA && i <= OP_JOIN);
247       opInfo[i].minEncSize = 8; /* set below */
248    }
249    for (i = 0; i < ARRAY_SIZE(commutative); ++i)
250       opInfo[commutative[i]].commutative = true;
251    for (i = 0; i < ARRAY_SIZE(shortForm); ++i)
252       opInfo[shortForm[i]].minEncSize = 4;
253    for (i = 0; i < ARRAY_SIZE(noDest); ++i)
254       opInfo[noDest[i]].hasDest = 0;
255    for (i = 0; i < ARRAY_SIZE(noPred); ++i)
256       opInfo[noPred[i]].predicate = 0;
257 
258    initProps(_initProps, ARRAY_SIZE(_initProps));
259    if (chipset >= NVISA_GM107_CHIPSET)
260       initProps(_initPropsGM107, ARRAY_SIZE(_initPropsGM107));
261    else if (chipset >= NVISA_GK104_CHIPSET)
262       initProps(_initPropsNVE4, ARRAY_SIZE(_initPropsNVE4));
263 }
264 
265 unsigned int
getFileSize(DataFile file) const266 TargetNVC0::getFileSize(DataFile file) const
267 {
268    const unsigned int gprs = (chipset >= NVISA_GK20A_CHIPSET) ? 255 : 63;
269    const unsigned int smregs = (chipset >= NVISA_GK104_CHIPSET) ? 65536 : 32768;
270    const unsigned int bs = (chipset >= NVISA_GV100_CHIPSET) ? 16 : 0;
271    switch (file) {
272    case FILE_NULL:          return 0;
273    case FILE_GPR:           return MIN2(gprs, smregs / threads);
274    case FILE_PREDICATE:     return 7;
275    case FILE_FLAGS:         return 1;
276    case FILE_ADDRESS:       return 0;
277    case FILE_BARRIER:       return bs;
278    case FILE_IMMEDIATE:     return 0;
279    case FILE_MEMORY_CONST:  return 65536;
280    case FILE_SHADER_INPUT:  return 0x400;
281    case FILE_SHADER_OUTPUT: return 0x400;
282    case FILE_MEMORY_BUFFER: return 0xffffffff;
283    case FILE_MEMORY_GLOBAL: return 0xffffffff;
284    case FILE_MEMORY_SHARED: return 16 << 10;
285    case FILE_MEMORY_LOCAL:  return 48 << 10;
286    case FILE_SYSTEM_VALUE:  return 32;
287    case FILE_THREAD_STATE:  return bs;
288    default:
289       assert(!"invalid file");
290       return 0;
291    }
292 }
293 
294 unsigned int
getFileUnit(DataFile file) const295 TargetNVC0::getFileUnit(DataFile file) const
296 {
297    if (file == FILE_GPR || file == FILE_ADDRESS || file == FILE_SYSTEM_VALUE ||
298        file == FILE_BARRIER || file == FILE_THREAD_STATE)
299       return 2;
300    return 0;
301 }
302 
303 uint32_t
getSVAddress(DataFile shaderFile,const Symbol * sym) const304 TargetNVC0::getSVAddress(DataFile shaderFile, const Symbol *sym) const
305 {
306    const int idx = sym->reg.data.sv.index;
307    const SVSemantic sv = sym->reg.data.sv.sv;
308 
309    const bool isInput = shaderFile == FILE_SHADER_INPUT;
310    const bool kepler = getChipset() >= NVISA_GK104_CHIPSET;
311 
312    switch (sv) {
313    case SV_POSITION:       return 0x070 + idx * 4;
314    case SV_INSTANCE_ID:    return 0x2f8;
315    case SV_VERTEX_ID:      return 0x2fc;
316    case SV_PRIMITIVE_ID:   return isInput ? 0x060 : 0x040;
317    case SV_LAYER:          return 0x064;
318    case SV_VIEWPORT_INDEX: return 0x068;
319    case SV_POINT_SIZE:     return 0x06c;
320    case SV_CLIP_DISTANCE:  return 0x2c0 + idx * 4;
321    case SV_POINT_COORD:    return 0x2e0 + idx * 4;
322    case SV_FACE:           return 0x3fc;
323    case SV_TESS_OUTER:     return 0x000 + idx * 4;
324    case SV_TESS_INNER:     return 0x010 + idx * 4;
325    case SV_TESS_COORD:     return 0x2f0 + idx * 4;
326    case SV_NTID:           return kepler ? (0x00 + idx * 4) : ~0;
327    case SV_NCTAID:         return kepler ? (0x0c + idx * 4) : ~0;
328    case SV_GRIDID:         return kepler ? 0x18 : ~0;
329    case SV_WORK_DIM:       return 0x1c;
330    case SV_SAMPLE_INDEX:   return 0;
331    case SV_SAMPLE_POS:     return 0;
332    case SV_SAMPLE_MASK:    return 0;
333    case SV_BASEVERTEX:     return 0;
334    case SV_BASEINSTANCE:   return 0;
335    case SV_DRAWID:         return 0;
336    default:
337       return 0xffffffff;
338    }
339 }
340 
341 bool
insnCanLoad(const Instruction * i,int s,const Instruction * ld) const342 TargetNVC0::insnCanLoad(const Instruction *i, int s,
343                         const Instruction *ld) const
344 {
345    DataFile sf = ld->src(0).getFile();
346 
347    // immediate 0 can be represented by GPR $r63/$r255
348    if (sf == FILE_IMMEDIATE && ld->getSrc(0)->reg.data.u64 == 0)
349       return (!i->isPseudo() &&
350               !i->asTex() &&
351               i->op != OP_EXPORT && i->op != OP_STORE);
352 
353    if (s >= opInfo[i->op].srcNr)
354       return false;
355    if (!(opInfo[i->op].srcFiles[s] & (1 << (int)sf)))
356       return false;
357 
358    // indirect loads can only be done by OP_LOAD/VFETCH/INTERP on nvc0
359    if (ld->src(0).isIndirect(0))
360       return false;
361    // these are implemented using shf.r and shf.l which can't load consts
362    if ((i->op == OP_SHL || i->op == OP_SHR) && typeSizeof(i->sType) == 8 &&
363        sf == FILE_MEMORY_CONST)
364       return false;
365    // constant buffer loads can't be used with cbcc xmads
366    if (i->op == OP_XMAD && sf == FILE_MEMORY_CONST &&
367        (i->subOp & NV50_IR_SUBOP_XMAD_CMODE_MASK) == NV50_IR_SUBOP_XMAD_CBCC)
368       return false;
369    // constant buffer loads for the third operand can't be used with psl/mrg xmads
370    if (i->op == OP_XMAD && sf == FILE_MEMORY_CONST && s == 2 &&
371        (i->subOp & (NV50_IR_SUBOP_XMAD_PSL | NV50_IR_SUBOP_XMAD_MRG)))
372       return false;
373    // for xmads, immediates can't have the h1 flag set
374    if (i->op == OP_XMAD && sf == FILE_IMMEDIATE && s < 2 &&
375        i->subOp & NV50_IR_SUBOP_XMAD_H1(s))
376       return false;
377 
378    for (int k = 0; i->srcExists(k); ++k) {
379       if (i->src(k).getFile() == FILE_IMMEDIATE) {
380          if (k == 2 && i->op == OP_SUCLAMP) // special case
381             continue;
382          if (k == 1 && i->op == OP_SHLADD) // special case
383             continue;
384          if (i->getSrc(k)->reg.data.u64 != 0)
385             return false;
386       } else
387       if (i->src(k).getFile() != FILE_GPR &&
388           i->src(k).getFile() != FILE_PREDICATE &&
389           i->src(k).getFile() != FILE_FLAGS) {
390          return false;
391       }
392    }
393 
394    // only loads can do sub 4 byte addressing
395    if (sf == FILE_MEMORY_CONST &&
396        (ld->getSrc(0)->reg.data.offset & 0x3)
397        && i->op != OP_LOAD)
398       return false;
399 
400    // not all instructions support full 32 bit immediates
401    if (sf == FILE_IMMEDIATE) {
402       Storage &reg = ld->getSrc(0)->asImm()->reg;
403 
404       if (opInfo[i->op].immdBits != 0xffffffff || typeSizeof(i->sType) > 4) {
405          switch (i->sType) {
406          case TYPE_F64:
407             if (reg.data.u64 & 0x00000fffffffffffULL)
408                return false;
409             break;
410          case TYPE_F32:
411             if (reg.data.u32 & 0xfff)
412                return false;
413             break;
414          case TYPE_S32:
415          case TYPE_U32:
416             // with u32, 0xfffff counts as 0xffffffff as well
417             if (reg.data.s32 > 0x7ffff || reg.data.s32 < -0x80000)
418                return false;
419             // XMADs can only have 16-bit immediates
420             if (i->op == OP_XMAD && reg.data.u32 > 0xffff)
421                return false;
422             break;
423          case TYPE_U8:
424          case TYPE_S8:
425          case TYPE_U16:
426          case TYPE_S16:
427          case TYPE_F16:
428             break;
429          default:
430             return false;
431          }
432       } else
433       if (i->op == OP_ADD && i->sType == TYPE_F32) {
434          // add f32 LIMM cannot saturate
435          if (i->saturate && (reg.data.u32 & 0xfff))
436             return false;
437       }
438    }
439 
440    return true;
441 }
442 
443 bool
insnCanLoadOffset(const Instruction * insn,int s,int offset) const444 TargetNVC0::insnCanLoadOffset(const Instruction *insn, int s, int offset) const
445 {
446    const ValueRef& ref = insn->src(s);
447    offset += insn->src(s).get()->reg.data.offset;
448    if (ref.getFile() == FILE_MEMORY_CONST &&
449        (insn->op != OP_LOAD || insn->subOp != NV50_IR_SUBOP_LDC_IS))
450       return offset >= -0x8000 && offset < 0x8000;
451    return true;
452 }
453 
454 bool
isAccessSupported(DataFile file,DataType ty) const455 TargetNVC0::isAccessSupported(DataFile file, DataType ty) const
456 {
457    if (ty == TYPE_NONE)
458       return false;
459    if (file == FILE_MEMORY_CONST) {
460       if (getChipset() >= NVISA_GM107_CHIPSET)
461          return typeSizeof(ty) <= 4;
462       else
463       if (getChipset() >= NVISA_GK104_CHIPSET) // wrong encoding ?
464          return typeSizeof(ty) <= 8;
465    }
466    if (ty == TYPE_B96)
467       return false;
468    return true;
469 }
470 
471 bool
isOpSupported(operation op,DataType ty) const472 TargetNVC0::isOpSupported(operation op, DataType ty) const
473 {
474    if (op == OP_SAD && ty != TYPE_S32 && ty != TYPE_U32)
475       return false;
476    if (op == OP_POW || op == OP_SQRT || op == OP_DIV || op == OP_MOD)
477       return false;
478    if (op == OP_XMAD)
479       return false;
480    return true;
481 }
482 
483 bool
isModSupported(const Instruction * insn,int s,Modifier mod) const484 TargetNVC0::isModSupported(const Instruction *insn, int s, Modifier mod) const
485 {
486    if (!isFloatType(insn->dType)) {
487       switch (insn->op) {
488       case OP_ABS:
489       case OP_NEG:
490       case OP_CVT:
491       case OP_CEIL:
492       case OP_FLOOR:
493       case OP_TRUNC:
494       case OP_AND:
495       case OP_OR:
496       case OP_XOR:
497       case OP_POPCNT:
498       case OP_BFIND:
499       case OP_XMAD:
500          break;
501       case OP_SET:
502          if (insn->sType != TYPE_F32)
503             return false;
504          break;
505       case OP_ADD:
506          if (mod.abs())
507             return false;
508          if (insn->src(s ? 0 : 1).mod.neg())
509             return false;
510          break;
511       case OP_SUB:
512          if (s == 0)
513             return insn->src(1).mod.neg() ? false : true;
514          break;
515       case OP_SHLADD:
516          if (s == 1)
517             return false;
518          if (insn->src(s ? 0 : 2).mod.neg())
519             return false;
520          break;
521       default:
522          return false;
523       }
524    }
525    if (s >= opInfo[insn->op].srcNr || s >= 3)
526       return false;
527    return (mod & Modifier(opInfo[insn->op].srcMods[s])) == mod;
528 }
529 
530 bool
mayPredicate(const Instruction * insn,const Value * pred) const531 TargetNVC0::mayPredicate(const Instruction *insn, const Value *pred) const
532 {
533    if (insn->getPredicate())
534       return false;
535    return opInfo[insn->op].predicate;
536 }
537 
538 bool
isSatSupported(const Instruction * insn) const539 TargetNVC0::isSatSupported(const Instruction *insn) const
540 {
541    if (insn->op == OP_CVT)
542       return true;
543    if (!(opInfo[insn->op].dstMods & NV50_IR_MOD_SAT))
544       return false;
545 
546    if (insn->dType == TYPE_U32)
547       return (insn->op == OP_ADD) || (insn->op == OP_MAD);
548 
549    // add f32 LIMM cannot saturate
550    if (insn->op == OP_ADD && insn->sType == TYPE_F32) {
551       if (insn->getSrc(1)->asImm() &&
552           insn->getSrc(1)->reg.data.u32 & 0xfff)
553          return false;
554    }
555 
556    return insn->dType == TYPE_F32;
557 }
558 
559 bool
isPostMultiplySupported(operation op,float f,int & e) const560 TargetNVC0::isPostMultiplySupported(operation op, float f, int& e) const
561 {
562    if (op != OP_MUL)
563       return false;
564    f = fabsf(f);
565    e = static_cast<int>(log2f(f));
566    if (e < -3 || e > 3)
567       return false;
568    return f == exp2f(static_cast<float>(e));
569 }
570 
571 // TODO: better values
572 // this could be more precise, e.g. depending on the issue-to-read/write delay
573 // of the depending instruction, but it's good enough
getLatency(const Instruction * i) const574 int TargetNVC0::getLatency(const Instruction *i) const
575 {
576    if (chipset >= 0xe4) {
577       if (i->dType == TYPE_F64 || i->sType == TYPE_F64)
578          return 20;
579       switch (i->op) {
580       case OP_LINTERP:
581       case OP_PINTERP:
582          return 15;
583       case OP_LOAD:
584          if (i->src(0).getFile() == FILE_MEMORY_CONST)
585             return 9;
586          FALLTHROUGH;
587       case OP_VFETCH:
588          return 24;
589       default:
590          if (Target::getOpClass(i->op) == OPCLASS_TEXTURE)
591             return 17;
592          if (i->op == OP_MUL && i->dType != TYPE_F32)
593             return 15;
594          return 9;
595       }
596    } else {
597       if (i->op == OP_LOAD) {
598          if (i->cache == CACHE_CV)
599             return 700;
600          return 48;
601       }
602       return 24;
603    }
604    return 32;
605 }
606 
607 // These are "inverse" throughput values, i.e. the number of cycles required
608 // to issue a specific instruction for a full warp (32 threads).
609 //
610 // Assuming we have more than 1 warp in flight, a higher issue latency results
611 // in a lower result latency since the MP will have spent more time with other
612 // warps.
613 // This also helps to determine the number of cycles between instructions in
614 // a single warp.
615 //
getThroughput(const Instruction * i) const616 int TargetNVC0::getThroughput(const Instruction *i) const
617 {
618    // TODO: better values
619    if (i->dType == TYPE_F32) {
620       switch (i->op) {
621       case OP_ADD:
622       case OP_MUL:
623       case OP_MAD:
624       case OP_FMA:
625          return 1;
626       case OP_CVT:
627       case OP_CEIL:
628       case OP_FLOOR:
629       case OP_TRUNC:
630       case OP_SET:
631       case OP_SLCT:
632       case OP_MIN:
633       case OP_MAX:
634          return 2;
635       case OP_RCP:
636       case OP_RSQ:
637       case OP_LG2:
638       case OP_SIN:
639       case OP_COS:
640       case OP_PRESIN:
641       case OP_PREEX2:
642       default:
643          return 8;
644       }
645    } else
646    if (i->dType == TYPE_U32 || i->dType == TYPE_S32) {
647       switch (i->op) {
648       case OP_ADD:
649       case OP_AND:
650       case OP_OR:
651       case OP_XOR:
652       case OP_NOT:
653          return 1;
654       case OP_MUL:
655       case OP_MAD:
656       case OP_CVT:
657       case OP_SET:
658       case OP_SLCT:
659       case OP_SHL:
660       case OP_SHR:
661       case OP_NEG:
662       case OP_ABS:
663       case OP_MIN:
664       case OP_MAX:
665       default:
666          return 2;
667       }
668    } else
669    if (i->dType == TYPE_F64) {
670       return 2;
671    } else {
672       return 1;
673    }
674 }
675 
canDualIssue(const Instruction * a,const Instruction * b) const676 bool TargetNVC0::canDualIssue(const Instruction *a, const Instruction *b) const
677 {
678    const OpClass clA = operationClass[a->op];
679    const OpClass clB = operationClass[b->op];
680 
681    if (getChipset() >= 0xe4) {
682       // not texturing
683       // not if the 2nd instruction isn't necessarily executed
684       if (clA == OPCLASS_TEXTURE || clA == OPCLASS_FLOW)
685          return false;
686 
687       // Check that a and b don't write to the same sources, nor that b reads
688       // anything that a writes.
689       if (!a->canCommuteDefDef(b) || !a->canCommuteDefSrc(b))
690          return false;
691 
692       // anything with MOV
693       if (a->op == OP_MOV || b->op == OP_MOV)
694          return true;
695       if (clA == clB) {
696          switch (clA) {
697          // there might be more
698          case OPCLASS_COMPARE:
699             if ((a->op == OP_MIN || a->op == OP_MAX) &&
700                 (b->op == OP_MIN || b->op == OP_MAX))
701                break;
702             return false;
703          case OPCLASS_ARITH:
704             break;
705          default:
706             return false;
707          }
708          // only F32 arith or integer additions
709          return (a->dType == TYPE_F32 || a->op == OP_ADD ||
710                  b->dType == TYPE_F32 || b->op == OP_ADD);
711       }
712       // nothing with TEXBAR
713       if (a->op == OP_TEXBAR || b->op == OP_TEXBAR)
714          return false;
715       // no loads and stores accessing the same space
716       if ((clA == OPCLASS_LOAD && clB == OPCLASS_STORE) ||
717           (clB == OPCLASS_LOAD && clA == OPCLASS_STORE))
718          if (a->src(0).getFile() == b->src(0).getFile())
719             return false;
720       // no > 32-bit ops
721       if (typeSizeof(a->dType) > 4 || typeSizeof(b->dType) > 4 ||
722           typeSizeof(a->sType) > 4 || typeSizeof(b->sType) > 4)
723          return false;
724       return true;
725    } else {
726       return false; // info not needed (yet)
727    }
728 }
729 
730 } // namespace nv50_ir
731