xref: /aosp_15_r20/external/mesa3d/src/nouveau/codegen/nv50_ir_target_nvc0.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "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_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 smregs = (chipset >= NVISA_GK104_CHIPSET) ? 65536 : 32768;
269    const unsigned int bs = (chipset >= NVISA_GV100_CHIPSET) ? 16 : 0;
270    unsigned int gprs;
271 
272    /* probably because of ugprs? */
273    if (chipset >= NVISA_GV100_CHIPSET)
274       gprs = 253;
275    else if (chipset >= NVISA_GK20A_CHIPSET)
276       gprs = 255;
277    else
278       gprs = 63;
279 
280    switch (file) {
281    case FILE_NULL:          return 0;
282    case FILE_GPR:           return MIN2(gprs, smregs / threads);
283    case FILE_PREDICATE:     return 7;
284    case FILE_FLAGS:         return 1;
285    case FILE_ADDRESS:       return 0;
286    case FILE_BARRIER:       return bs;
287    case FILE_IMMEDIATE:     return 0;
288    case FILE_MEMORY_CONST:  return 65536;
289    case FILE_SHADER_INPUT:  return 0x400;
290    case FILE_SHADER_OUTPUT: return 0x400;
291    case FILE_MEMORY_BUFFER: return 0xffffffff;
292    case FILE_MEMORY_GLOBAL: return 0xffffffff;
293    case FILE_MEMORY_SHARED: return 16 << 10;
294    case FILE_MEMORY_LOCAL:  return 48 << 10;
295    case FILE_SYSTEM_VALUE:  return 32;
296    case FILE_THREAD_STATE:  return bs;
297    default:
298       assert(!"invalid file");
299       return 0;
300    }
301 }
302 
303 unsigned int
getFileUnit(DataFile file) const304 TargetNVC0::getFileUnit(DataFile file) const
305 {
306    if (file == FILE_GPR || file == FILE_ADDRESS || file == FILE_SYSTEM_VALUE ||
307        file == FILE_BARRIER || file == FILE_THREAD_STATE)
308       return 2;
309    return 0;
310 }
311 
312 uint32_t
getSVAddress(DataFile shaderFile,const Symbol * sym) const313 TargetNVC0::getSVAddress(DataFile shaderFile, const Symbol *sym) const
314 {
315    const int idx = sym->reg.data.sv.index;
316    const SVSemantic sv = sym->reg.data.sv.sv;
317 
318    const bool isInput = shaderFile == FILE_SHADER_INPUT;
319    const bool kepler = getChipset() >= NVISA_GK104_CHIPSET;
320 
321    switch (sv) {
322    case SV_POSITION:       return 0x070 + idx * 4;
323    case SV_INSTANCE_ID:    return 0x2f8;
324    case SV_VERTEX_ID:      return 0x2fc;
325    case SV_PRIMITIVE_ID:   return isInput ? 0x060 : 0x040;
326    case SV_LAYER:          return 0x064;
327    case SV_VIEWPORT_INDEX: return 0x068;
328    case SV_POINT_SIZE:     return 0x06c;
329    case SV_CLIP_DISTANCE:  return 0x2c0 + idx * 4;
330    case SV_POINT_COORD:    return 0x2e0 + idx * 4;
331    case SV_FACE:           return 0x3fc;
332    case SV_TESS_OUTER:     return 0x000 + idx * 4;
333    case SV_TESS_INNER:     return 0x010 + idx * 4;
334    case SV_TESS_COORD:     return 0x2f0 + idx * 4;
335    case SV_NTID:           return kepler ? (0x00 + idx * 4) : ~0;
336    case SV_NCTAID:         return kepler ? (0x0c + idx * 4) : ~0;
337    case SV_GRIDID:         return kepler ? 0x18 : ~0;
338    case SV_WORK_DIM:       return 0x1c;
339    case SV_SAMPLE_INDEX:   return 0;
340    case SV_SAMPLE_POS:     return 0;
341    case SV_SAMPLE_MASK:    return 0;
342    case SV_BASEVERTEX:     return 0;
343    case SV_BASEINSTANCE:   return 0;
344    case SV_DRAWID:         return 0;
345    default:
346       return 0xffffffff;
347    }
348 }
349 
350 bool
insnCanLoad(const Instruction * i,int s,const Instruction * ld) const351 TargetNVC0::insnCanLoad(const Instruction *i, int s,
352                         const Instruction *ld) const
353 {
354    DataFile sf = ld->src(0).getFile();
355 
356    // immediate 0 can be represented by GPR $r63/$r255
357    if (sf == FILE_IMMEDIATE && ld->getSrc(0)->reg.data.u64 == 0)
358       return (!i->isPseudo() &&
359               !i->asTex() &&
360               i->op != OP_EXPORT && i->op != OP_STORE);
361 
362    if (s >= opInfo[i->op].srcNr)
363       return false;
364    if (!(opInfo[i->op].srcFiles[s] & (1 << (int)sf)))
365       return false;
366 
367    // indirect loads can only be done by OP_LOAD/VFETCH/INTERP on nvc0
368    if (ld->src(0).isIndirect(0))
369       return false;
370    // these are implemented using shf.r and shf.l which can't load consts
371    if ((i->op == OP_SHL || i->op == OP_SHR) && typeSizeof(i->sType) == 8 &&
372        sf == FILE_MEMORY_CONST)
373       return false;
374    // constant buffer loads can't be used with cbcc xmads
375    if (i->op == OP_XMAD && sf == FILE_MEMORY_CONST &&
376        (i->subOp & NV50_IR_SUBOP_XMAD_CMODE_MASK) == NV50_IR_SUBOP_XMAD_CBCC)
377       return false;
378    // constant buffer loads for the third operand can't be used with psl/mrg xmads
379    if (i->op == OP_XMAD && sf == FILE_MEMORY_CONST && s == 2 &&
380        (i->subOp & (NV50_IR_SUBOP_XMAD_PSL | NV50_IR_SUBOP_XMAD_MRG)))
381       return false;
382    // for xmads, immediates can't have the h1 flag set
383    if (i->op == OP_XMAD && sf == FILE_IMMEDIATE && s < 2 &&
384        i->subOp & NV50_IR_SUBOP_XMAD_H1(s))
385       return false;
386 
387    for (int k = 0; i->srcExists(k); ++k) {
388       if (i->src(k).getFile() == FILE_IMMEDIATE) {
389          if (k == 2 && i->op == OP_SUCLAMP) // special case
390             continue;
391          if (k == 1 && i->op == OP_SHLADD) // special case
392             continue;
393          if (i->getSrc(k)->reg.data.u64 != 0)
394             return false;
395       } else
396       if (i->src(k).getFile() != FILE_GPR &&
397           i->src(k).getFile() != FILE_PREDICATE &&
398           i->src(k).getFile() != FILE_FLAGS) {
399          return false;
400       }
401    }
402 
403    // only loads can do sub 4 byte addressing
404    if (sf == FILE_MEMORY_CONST &&
405        (ld->getSrc(0)->reg.data.offset & 0x3)
406        && i->op != OP_LOAD)
407       return false;
408 
409    // not all instructions support full 32 bit immediates
410    if (sf == FILE_IMMEDIATE) {
411       Storage &reg = ld->getSrc(0)->asImm()->reg;
412 
413       if (opInfo[i->op].immdBits != 0xffffffff || typeSizeof(i->sType) > 4) {
414          switch (i->sType) {
415          case TYPE_F64:
416             if (reg.data.u64 & 0x00000fffffffffffULL)
417                return false;
418             break;
419          case TYPE_F32:
420             if (reg.data.u32 & 0xfff)
421                return false;
422             break;
423          case TYPE_S32:
424          case TYPE_U32:
425             // with u32, 0xfffff counts as 0xffffffff as well
426             if (reg.data.s32 > 0x7ffff || reg.data.s32 < -0x80000)
427                return false;
428             // XMADs can only have 16-bit immediates
429             if (i->op == OP_XMAD && reg.data.u32 > 0xffff)
430                return false;
431             break;
432          case TYPE_U8:
433          case TYPE_S8:
434          case TYPE_U16:
435          case TYPE_S16:
436          case TYPE_F16:
437             break;
438          default:
439             return false;
440          }
441       } else
442       if (i->op == OP_ADD && i->sType == TYPE_F32) {
443          // add f32 LIMM cannot saturate
444          if (i->saturate && (reg.data.u32 & 0xfff))
445             return false;
446       }
447    }
448 
449    return true;
450 }
451 
452 bool
insnCanLoadOffset(const Instruction * insn,int s,int offset) const453 TargetNVC0::insnCanLoadOffset(const Instruction *insn, int s, int offset) const
454 {
455    const ValueRef& ref = insn->src(s);
456    offset += insn->src(s).get()->reg.data.offset;
457    if (ref.getFile() == FILE_MEMORY_CONST &&
458        (insn->op != OP_LOAD || insn->subOp != NV50_IR_SUBOP_LDC_IS))
459       return offset >= -0x8000 && offset < 0x8000;
460    return true;
461 }
462 
463 bool
isAccessSupported(DataFile file,DataType ty) const464 TargetNVC0::isAccessSupported(DataFile file, DataType ty) const
465 {
466    if (ty == TYPE_NONE)
467       return false;
468    if (file == FILE_MEMORY_CONST) {
469       if (getChipset() >= NVISA_GM107_CHIPSET)
470          return typeSizeof(ty) <= 4;
471       else
472       if (getChipset() >= NVISA_GK104_CHIPSET) // wrong encoding ?
473          return typeSizeof(ty) <= 8;
474    }
475    if (ty == TYPE_B96)
476       return false;
477    return true;
478 }
479 
480 bool
isOpSupported(operation op,DataType ty) const481 TargetNVC0::isOpSupported(operation op, DataType ty) const
482 {
483    if (op == OP_SAD && ty != TYPE_S32 && ty != TYPE_U32)
484       return false;
485    if (op == OP_SQRT || op == OP_DIV || op == OP_MOD)
486       return false;
487    if (op == OP_XMAD)
488       return false;
489    return true;
490 }
491 
492 bool
isModSupported(const Instruction * insn,int s,Modifier mod) const493 TargetNVC0::isModSupported(const Instruction *insn, int s, Modifier mod) const
494 {
495    if (!isFloatType(insn->dType)) {
496       switch (insn->op) {
497       case OP_ABS:
498       case OP_NEG:
499       case OP_CVT:
500       case OP_CEIL:
501       case OP_FLOOR:
502       case OP_TRUNC:
503       case OP_AND:
504       case OP_OR:
505       case OP_XOR:
506       case OP_POPCNT:
507       case OP_BFIND:
508       case OP_XMAD:
509          break;
510       case OP_SET:
511          if (insn->sType != TYPE_F32)
512             return false;
513          break;
514       case OP_ADD:
515          if (mod.abs())
516             return false;
517          if (insn->src(s ? 0 : 1).mod.neg())
518             return false;
519          break;
520       case OP_SUB:
521          if (s == 0)
522             return insn->src(1).mod.neg() ? false : true;
523          break;
524       case OP_SHLADD:
525          if (s == 1)
526             return false;
527          if (insn->src(s ? 0 : 2).mod.neg())
528             return false;
529          break;
530       default:
531          return false;
532       }
533    }
534    if (s >= opInfo[insn->op].srcNr || s >= 3)
535       return false;
536    return (mod & Modifier(opInfo[insn->op].srcMods[s])) == mod;
537 }
538 
539 bool
mayPredicate(const Instruction * insn,const Value * pred) const540 TargetNVC0::mayPredicate(const Instruction *insn, const Value *pred) const
541 {
542    if (insn->getPredicate())
543       return false;
544    return opInfo[insn->op].predicate;
545 }
546 
547 bool
isSatSupported(const Instruction * insn) const548 TargetNVC0::isSatSupported(const Instruction *insn) const
549 {
550    if (insn->op == OP_CVT)
551       return true;
552    if (!(opInfo[insn->op].dstMods & NV50_IR_MOD_SAT))
553       return false;
554 
555    if (insn->dType == TYPE_U32)
556       return (insn->op == OP_ADD) || (insn->op == OP_MAD);
557 
558    // add f32 LIMM cannot saturate
559    if (insn->op == OP_ADD && insn->sType == TYPE_F32) {
560       if (insn->getSrc(1)->asImm() &&
561           insn->getSrc(1)->reg.data.u32 & 0xfff)
562          return false;
563    }
564 
565    return insn->dType == TYPE_F32;
566 }
567 
568 bool
isPostMultiplySupported(operation op,float f,int & e) const569 TargetNVC0::isPostMultiplySupported(operation op, float f, int& e) const
570 {
571    if (op != OP_MUL)
572       return false;
573    f = fabsf(f);
574    e = static_cast<int>(log2f(f));
575    if (e < -3 || e > 3)
576       return false;
577    return f == exp2f(static_cast<float>(e));
578 }
579 
580 // TODO: better values
581 // this could be more precise, e.g. depending on the issue-to-read/write delay
582 // of the depending instruction, but it's good enough
getLatency(const Instruction * i) const583 int TargetNVC0::getLatency(const Instruction *i) const
584 {
585    if (chipset >= 0xe4) {
586       if (i->dType == TYPE_F64 || i->sType == TYPE_F64)
587          return 20;
588       switch (i->op) {
589       case OP_LINTERP:
590       case OP_PINTERP:
591          return 15;
592       case OP_LOAD:
593          if (i->src(0).getFile() == FILE_MEMORY_CONST)
594             return 9;
595          FALLTHROUGH;
596       case OP_VFETCH:
597          return 24;
598       default:
599          if (Target::getOpClass(i->op) == OPCLASS_TEXTURE)
600             return 17;
601          if (i->op == OP_MUL && i->dType != TYPE_F32)
602             return 15;
603          return 9;
604       }
605    } else {
606       if (i->op == OP_LOAD) {
607          if (i->cache == CACHE_CV)
608             return 700;
609          return 48;
610       }
611       return 24;
612    }
613    return 32;
614 }
615 
616 // These are "inverse" throughput values, i.e. the number of cycles required
617 // to issue a specific instruction for a full warp (32 threads).
618 //
619 // Assuming we have more than 1 warp in flight, a higher issue latency results
620 // in a lower result latency since the MP will have spent more time with other
621 // warps.
622 // This also helps to determine the number of cycles between instructions in
623 // a single warp.
624 //
getThroughput(const Instruction * i) const625 int TargetNVC0::getThroughput(const Instruction *i) const
626 {
627    // TODO: better values
628    if (i->dType == TYPE_F32) {
629       switch (i->op) {
630       case OP_ADD:
631       case OP_MUL:
632       case OP_MAD:
633       case OP_FMA:
634          return 1;
635       case OP_CVT:
636       case OP_CEIL:
637       case OP_FLOOR:
638       case OP_TRUNC:
639       case OP_SET:
640       case OP_SLCT:
641       case OP_MIN:
642       case OP_MAX:
643          return 2;
644       case OP_RCP:
645       case OP_RSQ:
646       case OP_LG2:
647       case OP_SIN:
648       case OP_COS:
649       case OP_PRESIN:
650       case OP_PREEX2:
651       default:
652          return 8;
653       }
654    } else
655    if (i->dType == TYPE_U32 || i->dType == TYPE_S32) {
656       switch (i->op) {
657       case OP_ADD:
658       case OP_AND:
659       case OP_OR:
660       case OP_XOR:
661       case OP_NOT:
662          return 1;
663       case OP_MUL:
664       case OP_MAD:
665       case OP_CVT:
666       case OP_SET:
667       case OP_SLCT:
668       case OP_SHL:
669       case OP_SHR:
670       case OP_NEG:
671       case OP_ABS:
672       case OP_MIN:
673       case OP_MAX:
674       default:
675          return 2;
676       }
677    } else
678    if (i->dType == TYPE_F64) {
679       return 2;
680    } else {
681       return 1;
682    }
683 }
684 
canDualIssue(const Instruction * a,const Instruction * b) const685 bool TargetNVC0::canDualIssue(const Instruction *a, const Instruction *b) const
686 {
687    const OpClass clA = operationClass[a->op];
688    const OpClass clB = operationClass[b->op];
689 
690    if (getChipset() >= 0xe4) {
691       // not texturing
692       // not if the 2nd instruction isn't necessarily executed
693       if (clA == OPCLASS_TEXTURE || clA == OPCLASS_FLOW)
694          return false;
695 
696       // Check that a and b don't write to the same sources, nor that b reads
697       // anything that a writes.
698       if (!a->canCommuteDefDef(b) || !a->canCommuteDefSrc(b))
699          return false;
700 
701       // anything with MOV
702       if (a->op == OP_MOV || b->op == OP_MOV)
703          return true;
704       if (clA == clB) {
705          switch (clA) {
706          // there might be more
707          case OPCLASS_COMPARE:
708             if ((a->op == OP_MIN || a->op == OP_MAX) &&
709                 (b->op == OP_MIN || b->op == OP_MAX))
710                break;
711             return false;
712          case OPCLASS_ARITH:
713             break;
714          default:
715             return false;
716          }
717          // only F32 arith or integer additions
718          return (a->dType == TYPE_F32 || a->op == OP_ADD ||
719                  b->dType == TYPE_F32 || b->op == OP_ADD);
720       }
721       // nothing with TEXBAR
722       if (a->op == OP_TEXBAR || b->op == OP_TEXBAR)
723          return false;
724       // no loads and stores accessing the same space
725       if ((clA == OPCLASS_LOAD && clB == OPCLASS_STORE) ||
726           (clB == OPCLASS_LOAD && clA == OPCLASS_STORE))
727          if (a->src(0).getFile() == b->src(0).getFile())
728             return false;
729       // no > 32-bit ops
730       if (typeSizeof(a->dType) > 4 || typeSizeof(b->dType) > 4 ||
731           typeSizeof(a->sType) > 4 || typeSizeof(b->sType) > 4)
732          return false;
733       return true;
734    } else {
735       return false; // info not needed (yet)
736    }
737 }
738 
739 } // namespace nv50_ir
740