501d4af7915964c738f19656c0e0deb6b082fce3
[mesa.git] / src / gallium / drivers / nouveau / codegen / nv50_ir_emit_gk110.cpp
1 /*
2 * Copyright 2012 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 // CodeEmitter for GK110 encoding of the Fermi/Kepler ISA.
26
27 namespace nv50_ir {
28
29 class CodeEmitterGK110 : public CodeEmitter
30 {
31 public:
32 CodeEmitterGK110(const TargetNVC0 *);
33
34 virtual bool emitInstruction(Instruction *);
35 virtual uint32_t getMinEncodingSize(const Instruction *) const;
36 virtual void prepareEmission(Function *);
37
38 inline void setProgramType(Program::Type pType) { progType = pType; }
39
40 private:
41 const TargetNVC0 *targNVC0;
42
43 Program::Type progType;
44
45 const bool writeIssueDelays;
46
47 private:
48 void emitForm_21(const Instruction *, uint32_t opc2, uint32_t opc1);
49 void emitForm_C(const Instruction *, uint32_t opc, uint8_t ctg);
50 void emitForm_L(const Instruction *, uint32_t opc, uint8_t ctg, Modifier);
51
52 void emitPredicate(const Instruction *);
53
54 void setCAddress14(const ValueRef&);
55 void setShortImmediate(const Instruction *, const int s);
56 void setImmediate32(const Instruction *, const int s, Modifier);
57 void setSUConst16(const Instruction *, const int s);
58
59 void modNegAbsF32_3b(const Instruction *, const int s);
60
61 void emitCondCode(CondCode cc, int pos, uint8_t mask);
62 void emitInterpMode(const Instruction *);
63 void emitLoadStoreType(DataType ty, const int pos);
64 void emitCachingMode(CacheMode c, const int pos);
65 void emitSUGType(DataType, const int pos);
66 void emitSUCachingMode(CacheMode c);
67
68 inline uint8_t getSRegEncoding(const ValueRef&);
69
70 void emitRoundMode(RoundMode, const int pos, const int rintPos);
71 void emitRoundModeF(RoundMode, const int pos);
72 void emitRoundModeI(RoundMode, const int pos);
73
74 void emitNegAbs12(const Instruction *);
75
76 void emitNOP(const Instruction *);
77
78 void emitLOAD(const Instruction *);
79 void emitSTORE(const Instruction *);
80 void emitMOV(const Instruction *);
81 void emitATOM(const Instruction *);
82 void emitCCTL(const Instruction *);
83
84 void emitINTERP(const Instruction *);
85 void emitAFETCH(const Instruction *);
86 void emitPFETCH(const Instruction *);
87 void emitVFETCH(const Instruction *);
88 void emitEXPORT(const Instruction *);
89 void emitOUT(const Instruction *);
90
91 void emitUADD(const Instruction *);
92 void emitFADD(const Instruction *);
93 void emitDADD(const Instruction *);
94 void emitIMUL(const Instruction *);
95 void emitFMUL(const Instruction *);
96 void emitDMUL(const Instruction *);
97 void emitIMAD(const Instruction *);
98 void emitISAD(const Instruction *);
99 void emitFMAD(const Instruction *);
100 void emitDMAD(const Instruction *);
101 void emitMADSP(const Instruction *i);
102
103 void emitNOT(const Instruction *);
104 void emitLogicOp(const Instruction *, uint8_t subOp);
105 void emitPOPC(const Instruction *);
106 void emitINSBF(const Instruction *);
107 void emitEXTBF(const Instruction *);
108 void emitBFIND(const Instruction *);
109 void emitPERMT(const Instruction *);
110 void emitShift(const Instruction *);
111
112 void emitSFnOp(const Instruction *, uint8_t subOp);
113
114 void emitCVT(const Instruction *);
115 void emitMINMAX(const Instruction *);
116 void emitPreOp(const Instruction *);
117
118 void emitSET(const CmpInstruction *);
119 void emitSLCT(const CmpInstruction *);
120 void emitSELP(const Instruction *);
121
122 void emitTEXBAR(const Instruction *);
123 void emitTEX(const TexInstruction *);
124 void emitTEXCSAA(const TexInstruction *);
125 void emitTXQ(const TexInstruction *);
126
127 void emitQUADOP(const Instruction *, uint8_t qOp, uint8_t laneMask);
128
129 void emitPIXLD(const Instruction *);
130
131 void emitBAR(const Instruction *);
132 void emitMEMBAR(const Instruction *);
133
134 void emitFlow(const Instruction *);
135
136 void emitVOTE(const Instruction *);
137
138 void emitSULDGB(const TexInstruction *);
139 void emitSUSTGx(const TexInstruction *);
140 void emitSUCLAMPMode(uint16_t);
141 void emitSUCalc(Instruction *);
142
143 void emitVSHL(const Instruction *);
144 void emitVectorSubOp(const Instruction *);
145
146 inline void defId(const ValueDef&, const int pos);
147 inline void srcId(const ValueRef&, const int pos);
148 inline void srcId(const ValueRef *, const int pos);
149 inline void srcId(const Instruction *, int s, const int pos);
150
151 inline void srcAddr32(const ValueRef&, const int pos); // address / 4
152
153 inline bool isLIMM(const ValueRef&, DataType ty, bool mod = false);
154 };
155
156 #define GK110_GPR_ZERO 255
157
158 #define NEG_(b, s) \
159 if (i->src(s).mod.neg()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
160 #define ABS_(b, s) \
161 if (i->src(s).mod.abs()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
162
163 #define NOT_(b, s) if (i->src(s).mod & Modifier(NV50_IR_MOD_NOT)) \
164 code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
165
166 #define FTZ_(b) if (i->ftz) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
167 #define DNZ_(b) if (i->dnz) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
168
169 #define SAT_(b) if (i->saturate) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
170
171 #define RND_(b, t) emitRoundMode##t(i->rnd, 0x##b)
172
173 #define SDATA(a) ((a).rep()->reg.data)
174 #define DDATA(a) ((a).rep()->reg.data)
175
176 void CodeEmitterGK110::srcId(const ValueRef& src, const int pos)
177 {
178 code[pos / 32] |= (src.get() ? SDATA(src).id : GK110_GPR_ZERO) << (pos % 32);
179 }
180
181 void CodeEmitterGK110::srcId(const ValueRef *src, const int pos)
182 {
183 code[pos / 32] |= (src ? SDATA(*src).id : GK110_GPR_ZERO) << (pos % 32);
184 }
185
186 void CodeEmitterGK110::srcId(const Instruction *insn, int s, int pos)
187 {
188 int r = insn->srcExists(s) ? SDATA(insn->src(s)).id : GK110_GPR_ZERO;
189 code[pos / 32] |= r << (pos % 32);
190 }
191
192 void CodeEmitterGK110::srcAddr32(const ValueRef& src, const int pos)
193 {
194 code[pos / 32] |= (SDATA(src).offset >> 2) << (pos % 32);
195 }
196
197 void CodeEmitterGK110::defId(const ValueDef& def, const int pos)
198 {
199 code[pos / 32] |= (def.get() ? DDATA(def).id : GK110_GPR_ZERO) << (pos % 32);
200 }
201
202 bool CodeEmitterGK110::isLIMM(const ValueRef& ref, DataType ty, bool mod)
203 {
204 const ImmediateValue *imm = ref.get()->asImm();
205
206 return imm && (imm->reg.data.u32 & ((ty == TYPE_F32) ? 0xfff : 0xfff00000));
207 }
208
209 void
210 CodeEmitterGK110::emitRoundMode(RoundMode rnd, const int pos, const int rintPos)
211 {
212 bool rint = false;
213 uint8_t n;
214
215 switch (rnd) {
216 case ROUND_MI: rint = true; /* fall through */ case ROUND_M: n = 1; break;
217 case ROUND_PI: rint = true; /* fall through */ case ROUND_P: n = 2; break;
218 case ROUND_ZI: rint = true; /* fall through */ case ROUND_Z: n = 3; break;
219 default:
220 rint = rnd == ROUND_NI;
221 n = 0;
222 assert(rnd == ROUND_N || rnd == ROUND_NI);
223 break;
224 }
225 code[pos / 32] |= n << (pos % 32);
226 if (rint && rintPos >= 0)
227 code[rintPos / 32] |= 1 << (rintPos % 32);
228 }
229
230 void
231 CodeEmitterGK110::emitRoundModeF(RoundMode rnd, const int pos)
232 {
233 uint8_t n;
234
235 switch (rnd) {
236 case ROUND_M: n = 1; break;
237 case ROUND_P: n = 2; break;
238 case ROUND_Z: n = 3; break;
239 default:
240 n = 0;
241 assert(rnd == ROUND_N);
242 break;
243 }
244 code[pos / 32] |= n << (pos % 32);
245 }
246
247 void
248 CodeEmitterGK110::emitRoundModeI(RoundMode rnd, const int pos)
249 {
250 uint8_t n;
251
252 switch (rnd) {
253 case ROUND_MI: n = 1; break;
254 case ROUND_PI: n = 2; break;
255 case ROUND_ZI: n = 3; break;
256 default:
257 n = 0;
258 assert(rnd == ROUND_NI);
259 break;
260 }
261 code[pos / 32] |= n << (pos % 32);
262 }
263
264 void CodeEmitterGK110::emitCondCode(CondCode cc, int pos, uint8_t mask)
265 {
266 uint8_t n;
267
268 switch (cc) {
269 case CC_FL: n = 0x00; break;
270 case CC_LT: n = 0x01; break;
271 case CC_EQ: n = 0x02; break;
272 case CC_LE: n = 0x03; break;
273 case CC_GT: n = 0x04; break;
274 case CC_NE: n = 0x05; break;
275 case CC_GE: n = 0x06; break;
276 case CC_LTU: n = 0x09; break;
277 case CC_EQU: n = 0x0a; break;
278 case CC_LEU: n = 0x0b; break;
279 case CC_GTU: n = 0x0c; break;
280 case CC_NEU: n = 0x0d; break;
281 case CC_GEU: n = 0x0e; break;
282 case CC_TR: n = 0x0f; break;
283 case CC_NO: n = 0x10; break;
284 case CC_NC: n = 0x11; break;
285 case CC_NS: n = 0x12; break;
286 case CC_NA: n = 0x13; break;
287 case CC_A: n = 0x14; break;
288 case CC_S: n = 0x15; break;
289 case CC_C: n = 0x16; break;
290 case CC_O: n = 0x17; break;
291 default:
292 n = 0;
293 assert(!"invalid condition code");
294 break;
295 }
296 code[pos / 32] |= (n & mask) << (pos % 32);
297 }
298
299 void
300 CodeEmitterGK110::emitPredicate(const Instruction *i)
301 {
302 if (i->predSrc >= 0) {
303 srcId(i->src(i->predSrc), 18);
304 if (i->cc == CC_NOT_P)
305 code[0] |= 8 << 18; // negate
306 assert(i->getPredicate()->reg.file == FILE_PREDICATE);
307 } else {
308 code[0] |= 7 << 18;
309 }
310 }
311
312 void
313 CodeEmitterGK110::setCAddress14(const ValueRef& src)
314 {
315 const Storage& res = src.get()->asSym()->reg;
316 const int32_t addr = res.data.offset / 4;
317
318 code[0] |= (addr & 0x01ff) << 23;
319 code[1] |= (addr & 0x3e00) >> 9;
320 code[1] |= res.fileIndex << 5;
321 }
322
323 void
324 CodeEmitterGK110::setShortImmediate(const Instruction *i, const int s)
325 {
326 const uint32_t u32 = i->getSrc(s)->asImm()->reg.data.u32;
327 const uint64_t u64 = i->getSrc(s)->asImm()->reg.data.u64;
328
329 if (i->sType == TYPE_F32) {
330 assert(!(u32 & 0x00000fff));
331 code[0] |= ((u32 & 0x001ff000) >> 12) << 23;
332 code[1] |= ((u32 & 0x7fe00000) >> 21);
333 code[1] |= ((u32 & 0x80000000) >> 4);
334 } else
335 if (i->sType == TYPE_F64) {
336 assert(!(u64 & 0x00000fffffffffffULL));
337 code[0] |= ((u64 & 0x001ff00000000000ULL) >> 44) << 23;
338 code[1] |= ((u64 & 0x7fe0000000000000ULL) >> 53);
339 code[1] |= ((u64 & 0x8000000000000000ULL) >> 36);
340 } else {
341 assert((u32 & 0xfff00000) == 0 || (u32 & 0xfff00000) == 0xfff00000);
342 code[0] |= (u32 & 0x001ff) << 23;
343 code[1] |= (u32 & 0x7fe00) >> 9;
344 code[1] |= (u32 & 0x80000) << 8;
345 }
346 }
347
348 void
349 CodeEmitterGK110::setImmediate32(const Instruction *i, const int s,
350 Modifier mod)
351 {
352 uint32_t u32 = i->getSrc(s)->asImm()->reg.data.u32;
353
354 if (mod) {
355 ImmediateValue imm(i->getSrc(s)->asImm(), i->sType);
356 mod.applyTo(imm);
357 u32 = imm.reg.data.u32;
358 }
359
360 code[0] |= u32 << 23;
361 code[1] |= u32 >> 9;
362 }
363
364 void
365 CodeEmitterGK110::emitForm_L(const Instruction *i, uint32_t opc, uint8_t ctg,
366 Modifier mod)
367 {
368 code[0] = ctg;
369 code[1] = opc << 20;
370
371 emitPredicate(i);
372
373 defId(i->def(0), 2);
374
375 for (int s = 0; s < 3 && i->srcExists(s); ++s) {
376 switch (i->src(s).getFile()) {
377 case FILE_GPR:
378 srcId(i->src(s), s ? 42 : 10);
379 break;
380 case FILE_IMMEDIATE:
381 setImmediate32(i, s, mod);
382 break;
383 default:
384 break;
385 }
386 }
387 }
388
389
390 void
391 CodeEmitterGK110::emitForm_C(const Instruction *i, uint32_t opc, uint8_t ctg)
392 {
393 code[0] = ctg;
394 code[1] = opc << 20;
395
396 emitPredicate(i);
397
398 defId(i->def(0), 2);
399
400 switch (i->src(0).getFile()) {
401 case FILE_MEMORY_CONST:
402 code[1] |= 0x4 << 28;
403 setCAddress14(i->src(0));
404 break;
405 case FILE_GPR:
406 code[1] |= 0xc << 28;
407 srcId(i->src(0), 23);
408 break;
409 default:
410 assert(0);
411 break;
412 }
413 }
414
415 // 0x2 for GPR, c[] and 0x1 for short immediate
416 void
417 CodeEmitterGK110::emitForm_21(const Instruction *i, uint32_t opc2,
418 uint32_t opc1)
419 {
420 const bool imm = i->srcExists(1) && i->src(1).getFile() == FILE_IMMEDIATE;
421
422 int s1 = 23;
423 if (i->srcExists(2) && i->src(2).getFile() == FILE_MEMORY_CONST)
424 s1 = 42;
425
426 if (imm) {
427 code[0] = 0x1;
428 code[1] = opc1 << 20;
429 } else {
430 code[0] = 0x2;
431 code[1] = (0xc << 28) | (opc2 << 20);
432 }
433
434 emitPredicate(i);
435
436 defId(i->def(0), 2);
437
438 for (int s = 0; s < 3 && i->srcExists(s); ++s) {
439 switch (i->src(s).getFile()) {
440 case FILE_MEMORY_CONST:
441 code[1] &= (s == 2) ? ~(0x4 << 28) : ~(0x8 << 28);
442 setCAddress14(i->src(s));
443 break;
444 case FILE_IMMEDIATE:
445 setShortImmediate(i, s);
446 break;
447 case FILE_GPR:
448 srcId(i->src(s), s ? ((s == 2) ? 42 : s1) : 10);
449 break;
450 default:
451 if (i->op == OP_SELP) {
452 assert(s == 2 && i->src(s).getFile() == FILE_PREDICATE);
453 srcId(i->src(s), 42);
454 }
455 // ignore here, can be predicate or flags, but must not be address
456 break;
457 }
458 }
459 // 0x0 = invalid
460 // 0xc = rrr
461 // 0x8 = rrc
462 // 0x4 = rcr
463 assert(imm || (code[1] & (0xc << 28)));
464 }
465
466 inline void
467 CodeEmitterGK110::modNegAbsF32_3b(const Instruction *i, const int s)
468 {
469 if (i->src(s).mod.abs()) code[1] &= ~(1 << 27);
470 if (i->src(s).mod.neg()) code[1] ^= (1 << 27);
471 }
472
473 void
474 CodeEmitterGK110::emitNOP(const Instruction *i)
475 {
476 code[0] = 0x00003c02;
477 code[1] = 0x85800000;
478
479 if (i)
480 emitPredicate(i);
481 else
482 code[0] = 0x001c3c02;
483 }
484
485 void
486 CodeEmitterGK110::emitFMAD(const Instruction *i)
487 {
488 assert(!isLIMM(i->src(1), TYPE_F32));
489
490 emitForm_21(i, 0x0c0, 0x940);
491
492 NEG_(34, 2);
493 SAT_(35);
494 RND_(36, F);
495 FTZ_(38);
496 DNZ_(39);
497
498 bool neg1 = (i->src(0).mod ^ i->src(1).mod).neg();
499
500 if (code[0] & 0x1) {
501 if (neg1)
502 code[1] ^= 1 << 27;
503 } else
504 if (neg1) {
505 code[1] |= 1 << 19;
506 }
507 }
508
509 void
510 CodeEmitterGK110::emitDMAD(const Instruction *i)
511 {
512 assert(!i->saturate);
513 assert(!i->ftz);
514
515 emitForm_21(i, 0x1b8, 0xb38);
516
517 NEG_(34, 2);
518 RND_(36, F);
519
520 bool neg1 = (i->src(0).mod ^ i->src(1).mod).neg();
521
522 if (code[0] & 0x1) {
523 if (neg1)
524 code[1] ^= 1 << 27;
525 } else
526 if (neg1) {
527 code[1] |= 1 << 19;
528 }
529 }
530
531 void
532 CodeEmitterGK110::emitMADSP(const Instruction *i)
533 {
534 emitForm_21(i, 0x140, 0xa40);
535
536 if (i->subOp == NV50_IR_SUBOP_MADSP_SD) {
537 code[1] |= 0x00c00000;
538 } else {
539 code[1] |= (i->subOp & 0x00f) << 19; // imadp1
540 code[1] |= (i->subOp & 0x0f0) << 20; // imadp2
541 code[1] |= (i->subOp & 0x100) << 11; // imadp3
542 code[1] |= (i->subOp & 0x200) << 15; // imadp3
543 code[1] |= (i->subOp & 0xc00) << 12; // imadp3
544 }
545
546 if (i->flagsDef >= 0)
547 code[1] |= 1 << 18;
548 }
549
550 void
551 CodeEmitterGK110::emitFMUL(const Instruction *i)
552 {
553 bool neg = (i->src(0).mod ^ i->src(1).mod).neg();
554
555 assert(i->postFactor >= -3 && i->postFactor <= 3);
556
557 if (isLIMM(i->src(1), TYPE_F32)) {
558 emitForm_L(i, 0x200, 0x2, Modifier(0));
559
560 FTZ_(38);
561 DNZ_(39);
562 SAT_(3a);
563 if (neg)
564 code[1] ^= 1 << 22;
565
566 assert(i->postFactor == 0);
567 } else {
568 emitForm_21(i, 0x234, 0xc34);
569 code[1] |= ((i->postFactor > 0) ?
570 (7 - i->postFactor) : (0 - i->postFactor)) << 12;
571
572 RND_(2a, F);
573 FTZ_(2f);
574 DNZ_(30);
575 SAT_(35);
576
577 if (code[0] & 0x1) {
578 if (neg)
579 code[1] ^= 1 << 27;
580 } else
581 if (neg) {
582 code[1] |= 1 << 19;
583 }
584 }
585 }
586
587 void
588 CodeEmitterGK110::emitDMUL(const Instruction *i)
589 {
590 bool neg = (i->src(0).mod ^ i->src(1).mod).neg();
591
592 assert(!i->postFactor);
593 assert(!i->saturate);
594 assert(!i->ftz);
595 assert(!i->dnz);
596
597 emitForm_21(i, 0x240, 0xc40);
598
599 RND_(2a, F);
600
601 if (code[0] & 0x1) {
602 if (neg)
603 code[1] ^= 1 << 27;
604 } else
605 if (neg) {
606 code[1] |= 1 << 19;
607 }
608 }
609
610 void
611 CodeEmitterGK110::emitIMUL(const Instruction *i)
612 {
613 assert(!i->src(0).mod.neg() && !i->src(1).mod.neg());
614 assert(!i->src(0).mod.abs() && !i->src(1).mod.abs());
615
616 if (i->src(1).getFile() == FILE_IMMEDIATE) {
617 emitForm_L(i, 0x280, 2, Modifier(0));
618
619 if (i->subOp == NV50_IR_SUBOP_MUL_HIGH)
620 code[1] |= 1 << 24;
621 if (i->sType == TYPE_S32)
622 code[1] |= 3 << 25;
623 } else {
624 emitForm_21(i, 0x21c, 0xc1c);
625
626 if (i->subOp == NV50_IR_SUBOP_MUL_HIGH)
627 code[1] |= 1 << 10;
628 if (i->sType == TYPE_S32)
629 code[1] |= 3 << 11;
630 }
631 }
632
633 void
634 CodeEmitterGK110::emitFADD(const Instruction *i)
635 {
636 if (isLIMM(i->src(1), TYPE_F32)) {
637 assert(i->rnd == ROUND_N);
638 assert(!i->saturate);
639
640 Modifier mod = i->src(1).mod ^
641 Modifier(i->op == OP_SUB ? NV50_IR_MOD_NEG : 0);
642
643 emitForm_L(i, 0x400, 0, mod);
644
645 FTZ_(3a);
646 NEG_(3b, 0);
647 ABS_(39, 0);
648 } else {
649 emitForm_21(i, 0x22c, 0xc2c);
650
651 FTZ_(2f);
652 RND_(2a, F);
653 ABS_(31, 0);
654 NEG_(33, 0);
655 SAT_(35);
656
657 if (code[0] & 0x1) {
658 modNegAbsF32_3b(i, 1);
659 if (i->op == OP_SUB) code[1] ^= 1 << 27;
660 } else {
661 ABS_(34, 1);
662 NEG_(30, 1);
663 if (i->op == OP_SUB) code[1] ^= 1 << 16;
664 }
665 }
666 }
667
668 void
669 CodeEmitterGK110::emitDADD(const Instruction *i)
670 {
671 assert(!i->saturate);
672 assert(!i->ftz);
673
674 emitForm_21(i, 0x238, 0xc38);
675 RND_(2a, F);
676 ABS_(31, 0);
677 NEG_(33, 0);
678 if (code[0] & 0x1) {
679 modNegAbsF32_3b(i, 1);
680 if (i->op == OP_SUB) code[1] ^= 1 << 27;
681 } else {
682 NEG_(30, 1);
683 ABS_(34, 1);
684 if (i->op == OP_SUB) code[1] ^= 1 << 16;
685 }
686 }
687
688 void
689 CodeEmitterGK110::emitUADD(const Instruction *i)
690 {
691 uint8_t addOp = (i->src(0).mod.neg() << 1) | i->src(1).mod.neg();
692
693 if (i->op == OP_SUB)
694 addOp ^= 1;
695
696 assert(!i->src(0).mod.abs() && !i->src(1).mod.abs());
697
698 if (isLIMM(i->src(1), TYPE_S32)) {
699 emitForm_L(i, 0x400, 1, Modifier((addOp & 1) ? NV50_IR_MOD_NEG : 0));
700
701 if (addOp & 2)
702 code[1] |= 1 << 27;
703
704 assert(!i->defExists(1));
705 assert(i->flagsSrc < 0);
706
707 SAT_(39);
708 } else {
709 emitForm_21(i, 0x208, 0xc08);
710
711 assert(addOp != 3); // would be add-plus-one
712
713 code[1] |= addOp << 19;
714
715 if (i->defExists(1))
716 code[1] |= 1 << 18; // write carry
717 if (i->flagsSrc >= 0)
718 code[1] |= 1 << 14; // add carry
719
720 SAT_(35);
721 }
722 }
723
724 // TODO: shl-add
725 void
726 CodeEmitterGK110::emitIMAD(const Instruction *i)
727 {
728 uint8_t addOp =
729 (i->src(2).mod.neg() << 1) | (i->src(0).mod.neg() ^ i->src(1).mod.neg());
730
731 emitForm_21(i, 0x100, 0xa00);
732
733 assert(addOp != 3);
734 code[1] |= addOp << 26;
735
736 if (i->sType == TYPE_S32)
737 code[1] |= (1 << 19) | (1 << 24);
738
739 if (i->subOp == NV50_IR_SUBOP_MUL_HIGH)
740 code[1] |= 1 << 25;
741
742 if (i->flagsDef >= 0) code[1] |= 1 << 18;
743 if (i->flagsSrc >= 0) code[1] |= 1 << 20;
744
745 SAT_(35);
746 }
747
748 void
749 CodeEmitterGK110::emitISAD(const Instruction *i)
750 {
751 assert(i->dType == TYPE_S32 || i->dType == TYPE_U32);
752
753 emitForm_21(i, 0x1f4, 0xb74);
754
755 if (i->dType == TYPE_S32)
756 code[1] |= 1 << 19;
757 }
758
759 void
760 CodeEmitterGK110::emitNOT(const Instruction *i)
761 {
762 code[0] = 0x0003fc02; // logop(mov2) dst, 0, not src
763 code[1] = 0x22003800;
764
765 emitPredicate(i);
766
767 defId(i->def(0), 2);
768
769 switch (i->src(0).getFile()) {
770 case FILE_GPR:
771 code[1] |= 0xc << 28;
772 srcId(i->src(0), 23);
773 break;
774 case FILE_MEMORY_CONST:
775 code[1] |= 0x4 << 28;
776 setCAddress14(i->src(1));
777 break;
778 default:
779 assert(0);
780 break;
781 }
782 }
783
784 void
785 CodeEmitterGK110::emitLogicOp(const Instruction *i, uint8_t subOp)
786 {
787 if (i->def(0).getFile() == FILE_PREDICATE) {
788 code[0] = 0x00000002 | (subOp << 27);
789 code[1] = 0x84800000;
790
791 emitPredicate(i);
792
793 defId(i->def(0), 5);
794 srcId(i->src(0), 14);
795 if (i->src(0).mod == Modifier(NV50_IR_MOD_NOT)) code[0] |= 1 << 17;
796 srcId(i->src(1), 32);
797 if (i->src(1).mod == Modifier(NV50_IR_MOD_NOT)) code[1] |= 1 << 3;
798
799 if (i->defExists(1)) {
800 defId(i->def(1), 2);
801 } else {
802 code[0] |= 7 << 2;
803 }
804 // (a OP b) OP c
805 if (i->predSrc != 2 && i->srcExists(2)) {
806 code[1] |= subOp << 16;
807 srcId(i->src(2), 42);
808 if (i->src(2).mod == Modifier(NV50_IR_MOD_NOT)) code[1] |= 1 << 13;
809 } else {
810 code[1] |= 7 << 10;
811 }
812 } else
813 if (isLIMM(i->src(1), TYPE_S32)) {
814 emitForm_L(i, 0x200, 0, i->src(1).mod);
815 code[1] |= subOp << 24;
816 NOT_(3a, 0);
817 } else {
818 emitForm_21(i, 0x220, 0xc20);
819 code[1] |= subOp << 12;
820 NOT_(2a, 0);
821 NOT_(2b, 1);
822 }
823 }
824
825 void
826 CodeEmitterGK110::emitPOPC(const Instruction *i)
827 {
828 assert(!isLIMM(i->src(1), TYPE_S32, true));
829
830 emitForm_21(i, 0x204, 0xc04);
831
832 NOT_(2a, 0);
833 if (!(code[0] & 0x1))
834 NOT_(2b, 1);
835 }
836
837 void
838 CodeEmitterGK110::emitINSBF(const Instruction *i)
839 {
840 emitForm_21(i, 0x1f8, 0xb78);
841 }
842
843 void
844 CodeEmitterGK110::emitEXTBF(const Instruction *i)
845 {
846 emitForm_21(i, 0x600, 0xc00);
847
848 if (i->dType == TYPE_S32)
849 code[1] |= 0x80000;
850 if (i->subOp == NV50_IR_SUBOP_EXTBF_REV)
851 code[1] |= 0x800;
852 }
853
854 void
855 CodeEmitterGK110::emitBFIND(const Instruction *i)
856 {
857 emitForm_C(i, 0x218, 0x2);
858
859 if (i->dType == TYPE_S32)
860 code[1] |= 0x80000;
861 if (i->src(0).mod == Modifier(NV50_IR_MOD_NOT))
862 code[1] |= 0x800;
863 if (i->subOp == NV50_IR_SUBOP_BFIND_SAMT)
864 code[1] |= 0x1000;
865 }
866
867 void
868 CodeEmitterGK110::emitPERMT(const Instruction *i)
869 {
870 emitForm_21(i, 0x1e0, 0xb60);
871
872 code[1] |= i->subOp << 19;
873 }
874
875 void
876 CodeEmitterGK110::emitShift(const Instruction *i)
877 {
878 if (i->op == OP_SHR) {
879 emitForm_21(i, 0x214, 0xc14);
880 if (isSignedType(i->dType))
881 code[1] |= 1 << 19;
882 } else {
883 emitForm_21(i, 0x224, 0xc24);
884 }
885
886 if (i->subOp == NV50_IR_SUBOP_SHIFT_WRAP)
887 code[1] |= 1 << 10;
888 }
889
890 void
891 CodeEmitterGK110::emitPreOp(const Instruction *i)
892 {
893 emitForm_C(i, 0x248, 0x2);
894
895 if (i->op == OP_PREEX2)
896 code[1] |= 1 << 10;
897
898 NEG_(30, 0);
899 ABS_(34, 0);
900 }
901
902 void
903 CodeEmitterGK110::emitSFnOp(const Instruction *i, uint8_t subOp)
904 {
905 code[0] = 0x00000002 | (subOp << 23);
906 code[1] = 0x84000000;
907
908 emitPredicate(i);
909
910 defId(i->def(0), 2);
911 srcId(i->src(0), 10);
912
913 NEG_(33, 0);
914 ABS_(31, 0);
915 SAT_(35);
916 }
917
918 void
919 CodeEmitterGK110::emitMINMAX(const Instruction *i)
920 {
921 uint32_t op2, op1;
922
923 switch (i->dType) {
924 case TYPE_U32:
925 case TYPE_S32:
926 op2 = 0x210;
927 op1 = 0xc10;
928 break;
929 case TYPE_F32:
930 op2 = 0x230;
931 op1 = 0xc30;
932 break;
933 case TYPE_F64:
934 op2 = 0x228;
935 op1 = 0xc28;
936 break;
937 default:
938 assert(0);
939 op2 = 0;
940 op1 = 0;
941 break;
942 }
943 emitForm_21(i, op2, op1);
944
945 if (i->dType == TYPE_S32)
946 code[1] |= 1 << 19;
947 code[1] |= (i->op == OP_MIN) ? 0x1c00 : 0x3c00; // [!]pt
948
949 FTZ_(2f);
950 ABS_(31, 0);
951 NEG_(33, 0);
952 if (code[0] & 0x1) {
953 modNegAbsF32_3b(i, 1);
954 } else {
955 ABS_(34, 1);
956 NEG_(30, 1);
957 }
958 }
959
960 void
961 CodeEmitterGK110::emitCVT(const Instruction *i)
962 {
963 const bool f2f = isFloatType(i->dType) && isFloatType(i->sType);
964 const bool f2i = !isFloatType(i->dType) && isFloatType(i->sType);
965 const bool i2f = isFloatType(i->dType) && !isFloatType(i->sType);
966
967 bool sat = i->saturate;
968 bool abs = i->src(0).mod.abs();
969 bool neg = i->src(0).mod.neg();
970
971 RoundMode rnd = i->rnd;
972
973 switch (i->op) {
974 case OP_CEIL: rnd = f2f ? ROUND_PI : ROUND_P; break;
975 case OP_FLOOR: rnd = f2f ? ROUND_MI : ROUND_M; break;
976 case OP_TRUNC: rnd = f2f ? ROUND_ZI : ROUND_Z; break;
977 case OP_SAT: sat = true; break;
978 case OP_NEG: neg = !neg; break;
979 case OP_ABS: abs = true; neg = false; break;
980 default:
981 break;
982 }
983
984 DataType dType;
985
986 if (i->op == OP_NEG && i->dType == TYPE_U32)
987 dType = TYPE_S32;
988 else
989 dType = i->dType;
990
991
992 uint32_t op;
993
994 if (f2f) op = 0x254;
995 else if (f2i) op = 0x258;
996 else if (i2f) op = 0x25c;
997 else op = 0x260;
998
999 emitForm_C(i, op, 0x2);
1000
1001 FTZ_(2f);
1002 if (neg) code[1] |= 1 << 16;
1003 if (abs) code[1] |= 1 << 20;
1004 if (sat) code[1] |= 1 << 21;
1005
1006 emitRoundMode(rnd, 32 + 10, f2f ? (32 + 13) : -1);
1007
1008 code[0] |= typeSizeofLog2(dType) << 10;
1009 code[0] |= typeSizeofLog2(i->sType) << 12;
1010 code[1] |= i->subOp << 12;
1011
1012 if (isSignedIntType(dType))
1013 code[0] |= 0x4000;
1014 if (isSignedIntType(i->sType))
1015 code[0] |= 0x8000;
1016 }
1017
1018 void
1019 CodeEmitterGK110::emitSET(const CmpInstruction *i)
1020 {
1021 uint16_t op1, op2;
1022
1023 if (i->def(0).getFile() == FILE_PREDICATE) {
1024 switch (i->sType) {
1025 case TYPE_F32: op2 = 0x1d8; op1 = 0xb58; break;
1026 case TYPE_F64: op2 = 0x1c0; op1 = 0xb40; break;
1027 default:
1028 op2 = 0x1b0;
1029 op1 = 0xb30;
1030 break;
1031 }
1032 emitForm_21(i, op2, op1);
1033
1034 NEG_(2e, 0);
1035 ABS_(9, 0);
1036 if (!(code[0] & 0x1)) {
1037 NEG_(8, 1);
1038 ABS_(2f, 1);
1039 } else {
1040 modNegAbsF32_3b(i, 1);
1041 }
1042 FTZ_(32);
1043
1044 // normal DST field is negated predicate result
1045 code[0] = (code[0] & ~0xfc) | ((code[0] << 3) & 0xe0);
1046 if (i->defExists(1))
1047 defId(i->def(1), 2);
1048 else
1049 code[0] |= 0x1c;
1050 } else {
1051 switch (i->sType) {
1052 case TYPE_F32: op2 = 0x000; op1 = 0x800; break;
1053 case TYPE_F64: op2 = 0x080; op1 = 0x900; break;
1054 default:
1055 op2 = 0x1a8;
1056 op1 = 0xb28;
1057 break;
1058 }
1059 emitForm_21(i, op2, op1);
1060
1061 NEG_(2e, 0);
1062 ABS_(39, 0);
1063 if (!(code[0] & 0x1)) {
1064 NEG_(38, 1);
1065 ABS_(2f, 1);
1066 } else {
1067 modNegAbsF32_3b(i, 1);
1068 }
1069 FTZ_(3a);
1070
1071 if (i->dType == TYPE_F32) {
1072 if (isFloatType(i->sType))
1073 code[1] |= 1 << 23;
1074 else
1075 code[1] |= 1 << 15;
1076 }
1077 }
1078 if (i->sType == TYPE_S32)
1079 code[1] |= 1 << 19;
1080
1081 if (i->op != OP_SET) {
1082 switch (i->op) {
1083 case OP_SET_AND: code[1] |= 0x0 << 16; break;
1084 case OP_SET_OR: code[1] |= 0x1 << 16; break;
1085 case OP_SET_XOR: code[1] |= 0x2 << 16; break;
1086 default:
1087 assert(0);
1088 break;
1089 }
1090 srcId(i->src(2), 0x2a);
1091 } else {
1092 code[1] |= 0x7 << 10;
1093 }
1094 emitCondCode(i->setCond,
1095 isFloatType(i->sType) ? 0x33 : 0x34,
1096 isFloatType(i->sType) ? 0xf : 0x7);
1097 }
1098
1099 void
1100 CodeEmitterGK110::emitSLCT(const CmpInstruction *i)
1101 {
1102 CondCode cc = i->setCond;
1103 if (i->src(2).mod.neg())
1104 cc = reverseCondCode(cc);
1105
1106 if (i->dType == TYPE_F32) {
1107 emitForm_21(i, 0x1d0, 0xb50);
1108 FTZ_(32);
1109 emitCondCode(cc, 0x33, 0xf);
1110 } else {
1111 emitForm_21(i, 0x1a0, 0xb20);
1112 emitCondCode(cc, 0x34, 0x7);
1113 }
1114 }
1115
1116 static void
1117 selpFlip(const FixupEntry *entry, uint32_t *code, const FixupData& data)
1118 {
1119 int loc = entry->loc;
1120 if (data.force_persample_interp)
1121 code[loc + 1] |= 1 << 13;
1122 else
1123 code[loc + 1] &= ~(1 << 13);
1124 }
1125
1126 void CodeEmitterGK110::emitSELP(const Instruction *i)
1127 {
1128 emitForm_21(i, 0x250, 0x050);
1129
1130 if (i->src(2).mod & Modifier(NV50_IR_MOD_NOT))
1131 code[1] |= 1 << 13;
1132
1133 if (i->subOp == 1) {
1134 addInterp(0, 0, selpFlip);
1135 }
1136 }
1137
1138 void CodeEmitterGK110::emitTEXBAR(const Instruction *i)
1139 {
1140 code[0] = 0x0000003e | (i->subOp << 23);
1141 code[1] = 0x77000000;
1142
1143 emitPredicate(i);
1144 }
1145
1146 void CodeEmitterGK110::emitTEXCSAA(const TexInstruction *i)
1147 {
1148 code[0] = 0x00000002;
1149 code[1] = 0x76c00000;
1150
1151 code[1] |= i->tex.r << 9;
1152 // code[1] |= i->tex.s << (9 + 8);
1153
1154 if (i->tex.liveOnly)
1155 code[0] |= 0x80000000;
1156
1157 defId(i->def(0), 2);
1158 srcId(i->src(0), 10);
1159 }
1160
1161 static inline bool
1162 isNextIndependentTex(const TexInstruction *i)
1163 {
1164 if (!i->next || !isTextureOp(i->next->op))
1165 return false;
1166 if (i->getDef(0)->interfers(i->next->getSrc(0)))
1167 return false;
1168 return !i->next->srcExists(1) || !i->getDef(0)->interfers(i->next->getSrc(1));
1169 }
1170
1171 void
1172 CodeEmitterGK110::emitTEX(const TexInstruction *i)
1173 {
1174 const bool ind = i->tex.rIndirectSrc >= 0;
1175
1176 if (ind) {
1177 code[0] = 0x00000002;
1178 switch (i->op) {
1179 case OP_TXD:
1180 code[1] = 0x7e000000;
1181 break;
1182 case OP_TXLQ:
1183 code[1] = 0x7e800000;
1184 break;
1185 case OP_TXF:
1186 code[1] = 0x78000000;
1187 break;
1188 case OP_TXG:
1189 code[1] = 0x7dc00000;
1190 break;
1191 default:
1192 code[1] = 0x7d800000;
1193 break;
1194 }
1195 } else {
1196 switch (i->op) {
1197 case OP_TXD:
1198 code[0] = 0x00000002;
1199 code[1] = 0x76000000;
1200 code[1] |= i->tex.r << 9;
1201 break;
1202 case OP_TXLQ:
1203 code[0] = 0x00000002;
1204 code[1] = 0x76800000;
1205 code[1] |= i->tex.r << 9;
1206 break;
1207 case OP_TXF:
1208 code[0] = 0x00000002;
1209 code[1] = 0x70000000;
1210 code[1] |= i->tex.r << 13;
1211 break;
1212 case OP_TXG:
1213 code[0] = 0x00000001;
1214 code[1] = 0x70000000;
1215 code[1] |= i->tex.r << 15;
1216 break;
1217 default:
1218 code[0] = 0x00000001;
1219 code[1] = 0x60000000;
1220 code[1] |= i->tex.r << 15;
1221 break;
1222 }
1223 }
1224
1225 code[1] |= isNextIndependentTex(i) ? 0x1 : 0x2; // t : p mode
1226
1227 if (i->tex.liveOnly)
1228 code[0] |= 0x80000000;
1229
1230 switch (i->op) {
1231 case OP_TEX: break;
1232 case OP_TXB: code[1] |= 0x2000; break;
1233 case OP_TXL: code[1] |= 0x3000; break;
1234 case OP_TXF: break;
1235 case OP_TXG: break;
1236 case OP_TXD: break;
1237 case OP_TXLQ: break;
1238 default:
1239 assert(!"invalid texture op");
1240 break;
1241 }
1242
1243 if (i->op == OP_TXF) {
1244 if (!i->tex.levelZero)
1245 code[1] |= 0x1000;
1246 } else
1247 if (i->tex.levelZero) {
1248 code[1] |= 0x1000;
1249 }
1250
1251 if (i->op != OP_TXD && i->tex.derivAll)
1252 code[1] |= 0x200;
1253
1254 emitPredicate(i);
1255
1256 code[1] |= i->tex.mask << 2;
1257
1258 const int src1 = (i->predSrc == 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1259
1260 defId(i->def(0), 2);
1261 srcId(i->src(0), 10);
1262 srcId(i, src1, 23);
1263
1264 if (i->op == OP_TXG) code[1] |= i->tex.gatherComp << 13;
1265
1266 // texture target:
1267 code[1] |= (i->tex.target.isCube() ? 3 : (i->tex.target.getDim() - 1)) << 7;
1268 if (i->tex.target.isArray())
1269 code[1] |= 0x40;
1270 if (i->tex.target.isShadow())
1271 code[1] |= 0x400;
1272 if (i->tex.target == TEX_TARGET_2D_MS ||
1273 i->tex.target == TEX_TARGET_2D_MS_ARRAY)
1274 code[1] |= 0x800;
1275
1276 if (i->srcExists(src1) && i->src(src1).getFile() == FILE_IMMEDIATE) {
1277 // ?
1278 }
1279
1280 if (i->tex.useOffsets == 1) {
1281 switch (i->op) {
1282 case OP_TXF: code[1] |= 0x200; break;
1283 case OP_TXD: code[1] |= 0x00400000; break;
1284 default: code[1] |= 0x800; break;
1285 }
1286 }
1287 if (i->tex.useOffsets == 4)
1288 code[1] |= 0x1000;
1289 }
1290
1291 void
1292 CodeEmitterGK110::emitTXQ(const TexInstruction *i)
1293 {
1294 code[0] = 0x00000002;
1295 code[1] = 0x75400001;
1296
1297 switch (i->tex.query) {
1298 case TXQ_DIMS: code[0] |= 0x01 << 25; break;
1299 case TXQ_TYPE: code[0] |= 0x02 << 25; break;
1300 case TXQ_SAMPLE_POSITION: code[0] |= 0x05 << 25; break;
1301 case TXQ_FILTER: code[0] |= 0x10 << 25; break;
1302 case TXQ_LOD: code[0] |= 0x12 << 25; break;
1303 case TXQ_BORDER_COLOUR: code[0] |= 0x16 << 25; break;
1304 default:
1305 assert(!"invalid texture query");
1306 break;
1307 }
1308
1309 code[1] |= i->tex.mask << 2;
1310 code[1] |= i->tex.r << 9;
1311 if (/*i->tex.sIndirectSrc >= 0 || */i->tex.rIndirectSrc >= 0)
1312 code[1] |= 0x08000000;
1313
1314 defId(i->def(0), 2);
1315 srcId(i->src(0), 10);
1316
1317 emitPredicate(i);
1318 }
1319
1320 void
1321 CodeEmitterGK110::emitQUADOP(const Instruction *i, uint8_t qOp, uint8_t laneMask)
1322 {
1323 code[0] = 0x00000002 | ((qOp & 1) << 31);
1324 code[1] = 0x7fc00000 | (qOp >> 1) | (laneMask << 12);
1325
1326 defId(i->def(0), 2);
1327 srcId(i->src(0), 10);
1328 srcId((i->srcExists(1) && i->predSrc != 1) ? i->src(1) : i->src(0), 23);
1329
1330 if (i->op == OP_QUADOP && progType != Program::TYPE_FRAGMENT)
1331 code[1] |= 1 << 9; // dall
1332
1333 emitPredicate(i);
1334 }
1335
1336 void
1337 CodeEmitterGK110::emitPIXLD(const Instruction *i)
1338 {
1339 emitForm_L(i, 0x7f4, 2, Modifier(0));
1340 code[1] |= i->subOp << 2;
1341 code[1] |= 0x00070000;
1342 }
1343
1344 void
1345 CodeEmitterGK110::emitBAR(const Instruction *i)
1346 {
1347 code[0] = 0x00000002;
1348 code[1] = 0x85400000;
1349
1350 switch (i->subOp) {
1351 case NV50_IR_SUBOP_BAR_ARRIVE: code[1] |= 0x08; break;
1352 case NV50_IR_SUBOP_BAR_RED_AND: code[1] |= 0x50; break;
1353 case NV50_IR_SUBOP_BAR_RED_OR: code[1] |= 0x90; break;
1354 case NV50_IR_SUBOP_BAR_RED_POPC: code[1] |= 0x10; break;
1355 default:
1356 assert(i->subOp == NV50_IR_SUBOP_BAR_SYNC);
1357 break;
1358 }
1359
1360 emitPredicate(i);
1361
1362 // barrier id
1363 if (i->src(0).getFile() == FILE_GPR) {
1364 srcId(i->src(0), 10);
1365 } else {
1366 ImmediateValue *imm = i->getSrc(0)->asImm();
1367 assert(imm);
1368 code[0] |= imm->reg.data.u32 << 10;
1369 code[1] |= 0x8000;
1370 }
1371
1372 // thread count
1373 if (i->src(1).getFile() == FILE_GPR) {
1374 srcId(i->src(1), 23);
1375 } else {
1376 ImmediateValue *imm = i->getSrc(0)->asImm();
1377 assert(imm);
1378 assert(imm->reg.data.u32 <= 0xfff);
1379 code[0] |= imm->reg.data.u32 << 23;
1380 code[1] |= imm->reg.data.u32 >> 9;
1381 code[1] |= 0x4000;
1382 }
1383
1384 if (i->srcExists(2) && (i->predSrc != 2)) {
1385 srcId(i->src(2), 32 + 10);
1386 if (i->src(2).mod == Modifier(NV50_IR_MOD_NOT))
1387 code[1] |= 1 << 13;
1388 } else {
1389 code[1] |= 7 << 10;
1390 }
1391 }
1392
1393 void CodeEmitterGK110::emitMEMBAR(const Instruction *i)
1394 {
1395 code[0] = 0x00000002 | NV50_IR_SUBOP_MEMBAR_SCOPE(i->subOp) << 8;
1396 code[1] = 0x7cc00000;
1397
1398 emitPredicate(i);
1399 }
1400
1401 void
1402 CodeEmitterGK110::emitFlow(const Instruction *i)
1403 {
1404 const FlowInstruction *f = i->asFlow();
1405
1406 unsigned mask; // bit 0: predicate, bit 1: target
1407
1408 code[0] = 0x00000000;
1409
1410 switch (i->op) {
1411 case OP_BRA:
1412 code[1] = f->absolute ? 0x10800000 : 0x12000000;
1413 if (i->srcExists(0) && i->src(0).getFile() == FILE_MEMORY_CONST)
1414 code[0] |= 0x80;
1415 mask = 3;
1416 break;
1417 case OP_CALL:
1418 code[1] = f->absolute ? 0x11000000 : 0x13000000;
1419 if (i->srcExists(0) && i->src(0).getFile() == FILE_MEMORY_CONST)
1420 code[0] |= 0x80;
1421 mask = 2;
1422 break;
1423
1424 case OP_EXIT: code[1] = 0x18000000; mask = 1; break;
1425 case OP_RET: code[1] = 0x19000000; mask = 1; break;
1426 case OP_DISCARD: code[1] = 0x19800000; mask = 1; break;
1427 case OP_BREAK: code[1] = 0x1a000000; mask = 1; break;
1428 case OP_CONT: code[1] = 0x1a800000; mask = 1; break;
1429
1430 case OP_JOINAT: code[1] = 0x14800000; mask = 2; break;
1431 case OP_PREBREAK: code[1] = 0x15000000; mask = 2; break;
1432 case OP_PRECONT: code[1] = 0x15800000; mask = 2; break;
1433 case OP_PRERET: code[1] = 0x13800000; mask = 2; break;
1434
1435 case OP_QUADON: code[1] = 0x1b800000; mask = 0; break;
1436 case OP_QUADPOP: code[1] = 0x1c000000; mask = 0; break;
1437 case OP_BRKPT: code[1] = 0x00000000; mask = 0; break;
1438 default:
1439 assert(!"invalid flow operation");
1440 return;
1441 }
1442
1443 if (mask & 1) {
1444 emitPredicate(i);
1445 if (i->flagsSrc < 0)
1446 code[0] |= 0x3c;
1447 }
1448
1449 if (!f)
1450 return;
1451
1452 if (f->allWarp)
1453 code[0] |= 1 << 9;
1454 if (f->limit)
1455 code[0] |= 1 << 8;
1456
1457 if (f->op == OP_CALL) {
1458 if (f->builtin) {
1459 assert(f->absolute);
1460 uint32_t pcAbs = targNVC0->getBuiltinOffset(f->target.builtin);
1461 addReloc(RelocEntry::TYPE_BUILTIN, 0, pcAbs, 0xff800000, 23);
1462 addReloc(RelocEntry::TYPE_BUILTIN, 1, pcAbs, 0x007fffff, -9);
1463 } else {
1464 assert(!f->absolute);
1465 int32_t pcRel = f->target.fn->binPos - (codeSize + 8);
1466 code[0] |= (pcRel & 0x1ff) << 23;
1467 code[1] |= (pcRel >> 9) & 0x7fff;
1468 }
1469 } else
1470 if (mask & 2) {
1471 int32_t pcRel = f->target.bb->binPos - (codeSize + 8);
1472 if (writeIssueDelays && !(f->target.bb->binPos & 0x3f))
1473 pcRel += 8;
1474 // currently we don't want absolute branches
1475 assert(!f->absolute);
1476 code[0] |= (pcRel & 0x1ff) << 23;
1477 code[1] |= (pcRel >> 9) & 0x7fff;
1478 }
1479 }
1480
1481 void
1482 CodeEmitterGK110::emitVOTE(const Instruction *i)
1483 {
1484 assert(i->src(0).getFile() == FILE_PREDICATE);
1485
1486 code[0] = 0x00000002;
1487 code[1] = 0x86c00000 | (i->subOp << 19);
1488
1489 emitPredicate(i);
1490
1491 unsigned rp = 0;
1492 for (int d = 0; i->defExists(d); d++) {
1493 if (i->def(d).getFile() == FILE_PREDICATE) {
1494 assert(!(rp & 2));
1495 rp |= 2;
1496 defId(i->def(d), 48);
1497 } else if (i->def(d).getFile() == FILE_GPR) {
1498 assert(!(rp & 1));
1499 rp |= 1;
1500 defId(i->def(d), 2);
1501 } else {
1502 assert(!"Unhandled def");
1503 }
1504 }
1505 if (!(rp & 1))
1506 code[0] |= 255 << 2;
1507 if (!(rp & 2))
1508 code[1] |= 7 << 16;
1509 if (i->src(0).mod == Modifier(NV50_IR_MOD_NOT))
1510 code[1] |= 1 << 13;
1511 srcId(i->src(0), 42);
1512 }
1513
1514 void
1515 CodeEmitterGK110::emitSUGType(DataType ty, const int pos)
1516 {
1517 uint8_t n = 0;
1518
1519 switch (ty) {
1520 case TYPE_S32: n = 1; break;
1521 case TYPE_U8: n = 2; break;
1522 case TYPE_S8: n = 3; break;
1523 default:
1524 assert(ty == TYPE_U32);
1525 break;
1526 }
1527 code[pos / 32] |= n << (pos % 32);
1528 }
1529
1530 void
1531 CodeEmitterGK110::emitSUCachingMode(CacheMode c)
1532 {
1533 uint8_t n = 0;
1534
1535 switch (c) {
1536 case CACHE_CA:
1537 // case CACHE_WB:
1538 n = 0;
1539 break;
1540 case CACHE_CG:
1541 n = 1;
1542 break;
1543 case CACHE_CS:
1544 n = 2;
1545 break;
1546 case CACHE_CV:
1547 // case CACHE_WT:
1548 n = 3;
1549 break;
1550 default:
1551 assert(!"invalid caching mode");
1552 break;
1553 }
1554 code[0] |= (n & 1) << 31;
1555 code[1] |= (n & 2) >> 1;
1556 }
1557
1558 void
1559 CodeEmitterGK110::setSUConst16(const Instruction *i, const int s)
1560 {
1561 const uint32_t offset = i->getSrc(s)->reg.data.offset;
1562
1563 assert(offset == (offset & 0xfffc));
1564
1565 code[0] |= offset << 21;
1566 code[1] |= offset >> 11;
1567 code[1] |= i->getSrc(s)->reg.fileIndex << 5;
1568 }
1569
1570 void
1571 CodeEmitterGK110::emitSULDGB(const TexInstruction *i)
1572 {
1573 code[0] = 0x00000002;
1574 code[1] = 0x30000000 | (i->subOp << 14);
1575
1576 if (i->src(1).getFile() == FILE_MEMORY_CONST) {
1577 emitLoadStoreType(i->dType, 0x38);
1578 emitCachingMode(i->cache, 0x36);
1579
1580 // format
1581 setSUConst16(i, 1);
1582 } else {
1583 assert(i->src(1).getFile() == FILE_GPR);
1584 code[1] |= 0x49800000;
1585
1586 emitLoadStoreType(i->dType, 0x21);
1587 emitSUCachingMode(i->cache);
1588
1589 srcId(i->src(1), 23);
1590 }
1591
1592 emitSUGType(i->sType, 0x34);
1593
1594 emitPredicate(i);
1595 defId(i->def(0), 2); // destination
1596 srcId(i->src(0), 10); // address
1597
1598 // surface predicate
1599 if (!i->srcExists(2) || (i->predSrc == 2)) {
1600 code[1] |= 0x7 << 10;
1601 } else {
1602 if (i->src(2).mod == Modifier(NV50_IR_MOD_NOT))
1603 code[1] |= 1 << 13;
1604 srcId(i->src(2), 32 + 10);
1605 }
1606 }
1607
1608 void
1609 CodeEmitterGK110::emitSUSTGx(const TexInstruction *i)
1610 {
1611 assert(i->op == OP_SUSTP);
1612
1613 code[0] = 0x00000002;
1614 code[1] = 0x38000000;
1615
1616 if (i->src(1).getFile() == FILE_MEMORY_CONST) {
1617 code[0] |= i->subOp << 2;
1618
1619 if (i->op == OP_SUSTP)
1620 code[0] |= i->tex.mask << 4;
1621
1622 emitSUGType(i->sType, 0x8);
1623 emitCachingMode(i->cache, 0x36);
1624
1625 // format
1626 setSUConst16(i, 1);
1627 } else {
1628 assert(i->src(1).getFile() == FILE_GPR);
1629
1630 code[0] |= i->subOp << 23;
1631 code[1] |= 0x41c00000;
1632
1633 if (i->op == OP_SUSTP)
1634 code[0] |= i->tex.mask << 25;
1635
1636 emitSUGType(i->sType, 0x1d);
1637 emitSUCachingMode(i->cache);
1638
1639 srcId(i->src(1), 2);
1640 }
1641
1642 emitPredicate(i);
1643 srcId(i->src(0), 10); // address
1644 srcId(i->src(3), 42); // values
1645
1646 // surface predicate
1647 if (!i->srcExists(2) || (i->predSrc == 2)) {
1648 code[1] |= 0x7 << 18;
1649 } else {
1650 if (i->src(2).mod == Modifier(NV50_IR_MOD_NOT))
1651 code[1] |= 1 << 21;
1652 srcId(i->src(2), 32 + 18);
1653 }
1654 }
1655
1656 void
1657 CodeEmitterGK110::emitSUCLAMPMode(uint16_t subOp)
1658 {
1659 uint8_t m;
1660 switch (subOp & ~NV50_IR_SUBOP_SUCLAMP_2D) {
1661 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m = 0; break;
1662 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m = 1; break;
1663 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m = 2; break;
1664 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m = 3; break;
1665 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m = 4; break;
1666 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m = 5; break;
1667 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m = 6; break;
1668 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m = 7; break;
1669 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m = 8; break;
1670 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m = 9; break;
1671 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m = 10; break;
1672 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m = 11; break;
1673 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m = 12; break;
1674 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m = 13; break;
1675 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m = 14; break;
1676 default:
1677 return;
1678 }
1679 code[1] |= m << 20;
1680 if (subOp & NV50_IR_SUBOP_SUCLAMP_2D)
1681 code[1] |= 1 << 24;
1682 }
1683
1684 void
1685 CodeEmitterGK110::emitSUCalc(Instruction *i)
1686 {
1687 ImmediateValue *imm = NULL;
1688 uint64_t opc1, opc2;
1689
1690 if (i->srcExists(2)) {
1691 imm = i->getSrc(2)->asImm();
1692 if (imm)
1693 i->setSrc(2, NULL); // special case, make emitForm_21 not assert
1694 }
1695
1696 switch (i->op) {
1697 case OP_SUCLAMP: opc1 = 0xb00; opc2 = 0x580; break;
1698 case OP_SUBFM: opc1 = 0xb68; opc2 = 0x1e8; break;
1699 case OP_SUEAU: opc1 = 0xb6c; opc2 = 0x1ec; break;
1700 default:
1701 assert(0);
1702 return;
1703 }
1704 emitForm_21(i, opc2, opc1);
1705
1706 if (i->op == OP_SUCLAMP) {
1707 if (i->dType == TYPE_S32)
1708 code[1] |= 1 << 19;
1709 emitSUCLAMPMode(i->subOp);
1710 }
1711
1712 if (i->op == OP_SUBFM && i->subOp == NV50_IR_SUBOP_SUBFM_3D)
1713 code[1] |= 1 << 18;
1714
1715 if (i->op != OP_SUEAU) {
1716 const uint8_t pos = i->op == OP_SUBFM ? 19 : 16;
1717 if (i->def(0).getFile() == FILE_PREDICATE) { // p, #
1718 code[0] |= 255 << 2;
1719 code[1] |= i->getDef(1)->reg.data.id << pos;
1720 } else
1721 if (i->defExists(1)) { // r, p
1722 assert(i->def(1).getFile() == FILE_PREDICATE);
1723 code[1] |= i->getDef(1)->reg.data.id << pos;
1724 } else { // r, #
1725 code[1] |= 7 << pos;
1726 }
1727 }
1728
1729 if (imm) {
1730 assert(i->op == OP_SUCLAMP);
1731 i->setSrc(2, imm);
1732 code[1] |= (imm->reg.data.u32 & 0x3f) << 10; // sint6
1733 }
1734 }
1735
1736
1737 void
1738 CodeEmitterGK110::emitVectorSubOp(const Instruction *i)
1739 {
1740 switch (NV50_IR_SUBOP_Vn(i->subOp)) {
1741 case 0:
1742 code[1] |= (i->subOp & 0x000f) << 7; // vsrc1
1743 code[1] |= (i->subOp & 0x00e0) >> 6; // vsrc2
1744 code[1] |= (i->subOp & 0x0100) << 13; // vsrc2
1745 code[1] |= (i->subOp & 0x3c00) << 12; // vdst
1746 break;
1747 default:
1748 assert(0);
1749 break;
1750 }
1751 }
1752
1753 void
1754 CodeEmitterGK110::emitVSHL(const Instruction *i)
1755 {
1756 code[0] = 0x00000002;
1757 code[1] = 0xb8000000;
1758
1759 assert(NV50_IR_SUBOP_Vn(i->subOp) == 0);
1760
1761 if (isSignedType(i->dType)) code[1] |= 1 << 25;
1762 if (isSignedType(i->sType)) code[1] |= 1 << 19;
1763
1764 emitVectorSubOp(i);
1765
1766 emitPredicate(i);
1767 defId(i->def(0), 2);
1768 srcId(i->src(0), 10);
1769
1770 if (i->getSrc(1)->reg.file == FILE_IMMEDIATE) {
1771 ImmediateValue *imm = i->getSrc(1)->asImm();
1772 assert(imm);
1773 code[0] |= (imm->reg.data.u32 & 0x01ff) << 23;
1774 code[1] |= (imm->reg.data.u32 & 0xfe00) >> 9;
1775 } else {
1776 assert(i->getSrc(1)->reg.file == FILE_GPR);
1777 code[1] |= 1 << 21;
1778 srcId(i->src(1), 23);
1779 }
1780 srcId(i->src(2), 42);
1781
1782 if (i->saturate)
1783 code[0] |= 1 << 22;
1784 if (i->flagsDef >= 0)
1785 code[1] |= 1 << 18;
1786 }
1787
1788 void
1789 CodeEmitterGK110::emitAFETCH(const Instruction *i)
1790 {
1791 uint32_t offset = i->src(0).get()->reg.data.offset & 0x7ff;
1792
1793 code[0] = 0x00000002 | (offset << 23);
1794 code[1] = 0x7d000000 | (offset >> 9);
1795
1796 if (i->getSrc(0)->reg.file == FILE_SHADER_OUTPUT)
1797 code[1] |= 0x8;
1798
1799 emitPredicate(i);
1800
1801 defId(i->def(0), 2);
1802 srcId(i->src(0).getIndirect(0), 10);
1803 }
1804
1805 void
1806 CodeEmitterGK110::emitPFETCH(const Instruction *i)
1807 {
1808 uint32_t prim = i->src(0).get()->reg.data.u32;
1809
1810 code[0] = 0x00000002 | ((prim & 0xff) << 23);
1811 code[1] = 0x7f800000;
1812
1813 emitPredicate(i);
1814
1815 const int src1 = (i->predSrc == 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1816
1817 defId(i->def(0), 2);
1818 srcId(i, src1, 10);
1819 }
1820
1821 void
1822 CodeEmitterGK110::emitVFETCH(const Instruction *i)
1823 {
1824 unsigned int size = typeSizeof(i->dType);
1825 uint32_t offset = i->src(0).get()->reg.data.offset;
1826
1827 code[0] = 0x00000002 | (offset << 23);
1828 code[1] = 0x7ec00000 | (offset >> 9);
1829 code[1] |= (size / 4 - 1) << 18;
1830
1831 if (i->perPatch)
1832 code[1] |= 0x4;
1833 if (i->getSrc(0)->reg.file == FILE_SHADER_OUTPUT)
1834 code[1] |= 0x8; // yes, TCPs can read from *outputs* of other threads
1835
1836 emitPredicate(i);
1837
1838 defId(i->def(0), 2);
1839 srcId(i->src(0).getIndirect(0), 10);
1840 srcId(i->src(0).getIndirect(1), 32 + 10); // vertex address
1841 }
1842
1843 void
1844 CodeEmitterGK110::emitEXPORT(const Instruction *i)
1845 {
1846 unsigned int size = typeSizeof(i->dType);
1847 uint32_t offset = i->src(0).get()->reg.data.offset;
1848
1849 code[0] = 0x00000002 | (offset << 23);
1850 code[1] = 0x7f000000 | (offset >> 9);
1851 code[1] |= (size / 4 - 1) << 18;
1852
1853 if (i->perPatch)
1854 code[1] |= 0x4;
1855
1856 emitPredicate(i);
1857
1858 assert(i->src(1).getFile() == FILE_GPR);
1859
1860 srcId(i->src(0).getIndirect(0), 10);
1861 srcId(i->src(0).getIndirect(1), 32 + 10); // vertex base address
1862 srcId(i->src(1), 2);
1863 }
1864
1865 void
1866 CodeEmitterGK110::emitOUT(const Instruction *i)
1867 {
1868 assert(i->src(0).getFile() == FILE_GPR);
1869
1870 emitForm_21(i, 0x1f0, 0xb70);
1871
1872 if (i->op == OP_EMIT)
1873 code[1] |= 1 << 10;
1874 if (i->op == OP_RESTART || i->subOp == NV50_IR_SUBOP_EMIT_RESTART)
1875 code[1] |= 1 << 11;
1876 }
1877
1878 void
1879 CodeEmitterGK110::emitInterpMode(const Instruction *i)
1880 {
1881 code[1] |= (i->ipa & 0x3) << 21; // TODO: INTERP_SAMPLEID
1882 code[1] |= (i->ipa & 0xc) << (19 - 2);
1883 }
1884
1885 static void
1886 interpApply(const FixupEntry *entry, uint32_t *code, const FixupData& data)
1887 {
1888 int ipa = entry->ipa;
1889 int reg = entry->reg;
1890 int loc = entry->loc;
1891
1892 if (data.flatshade &&
1893 (ipa & NV50_IR_INTERP_MODE_MASK) == NV50_IR_INTERP_SC) {
1894 ipa = NV50_IR_INTERP_FLAT;
1895 reg = 0xff;
1896 } else if (data.force_persample_interp &&
1897 (ipa & NV50_IR_INTERP_SAMPLE_MASK) == NV50_IR_INTERP_DEFAULT &&
1898 (ipa & NV50_IR_INTERP_MODE_MASK) != NV50_IR_INTERP_FLAT) {
1899 ipa |= NV50_IR_INTERP_CENTROID;
1900 }
1901 code[loc + 1] &= ~(0xf << 19);
1902 code[loc + 1] |= (ipa & 0x3) << 21;
1903 code[loc + 1] |= (ipa & 0xc) << (19 - 2);
1904 code[loc + 0] &= ~(0xff << 23);
1905 code[loc + 0] |= reg << 23;
1906 }
1907
1908 void
1909 CodeEmitterGK110::emitINTERP(const Instruction *i)
1910 {
1911 const uint32_t base = i->getSrc(0)->reg.data.offset;
1912
1913 code[0] = 0x00000002 | (base << 31);
1914 code[1] = 0x74800000 | (base >> 1);
1915
1916 if (i->saturate)
1917 code[1] |= 1 << 18;
1918
1919 if (i->op == OP_PINTERP) {
1920 srcId(i->src(1), 23);
1921 addInterp(i->ipa, SDATA(i->src(1)).id, interpApply);
1922 } else {
1923 code[0] |= 0xff << 23;
1924 addInterp(i->ipa, 0xff, interpApply);
1925 }
1926
1927 srcId(i->src(0).getIndirect(0), 10);
1928 emitInterpMode(i);
1929
1930 emitPredicate(i);
1931 defId(i->def(0), 2);
1932
1933 if (i->getSampleMode() == NV50_IR_INTERP_OFFSET)
1934 srcId(i->src(i->op == OP_PINTERP ? 2 : 1), 32 + 10);
1935 else
1936 code[1] |= 0xff << 10;
1937 }
1938
1939 void
1940 CodeEmitterGK110::emitLoadStoreType(DataType ty, const int pos)
1941 {
1942 uint8_t n;
1943
1944 switch (ty) {
1945 case TYPE_U8:
1946 n = 0;
1947 break;
1948 case TYPE_S8:
1949 n = 1;
1950 break;
1951 case TYPE_U16:
1952 n = 2;
1953 break;
1954 case TYPE_S16:
1955 n = 3;
1956 break;
1957 case TYPE_F32:
1958 case TYPE_U32:
1959 case TYPE_S32:
1960 n = 4;
1961 break;
1962 case TYPE_F64:
1963 case TYPE_U64:
1964 case TYPE_S64:
1965 n = 5;
1966 break;
1967 case TYPE_B128:
1968 n = 6;
1969 break;
1970 default:
1971 n = 0;
1972 assert(!"invalid ld/st type");
1973 break;
1974 }
1975 code[pos / 32] |= n << (pos % 32);
1976 }
1977
1978 void
1979 CodeEmitterGK110::emitCachingMode(CacheMode c, const int pos)
1980 {
1981 uint8_t n;
1982
1983 switch (c) {
1984 case CACHE_CA:
1985 // case CACHE_WB:
1986 n = 0;
1987 break;
1988 case CACHE_CG:
1989 n = 1;
1990 break;
1991 case CACHE_CS:
1992 n = 2;
1993 break;
1994 case CACHE_CV:
1995 // case CACHE_WT:
1996 n = 3;
1997 break;
1998 default:
1999 n = 0;
2000 assert(!"invalid caching mode");
2001 break;
2002 }
2003 code[pos / 32] |= n << (pos % 32);
2004 }
2005
2006 void
2007 CodeEmitterGK110::emitSTORE(const Instruction *i)
2008 {
2009 int32_t offset = SDATA(i->src(0)).offset;
2010
2011 switch (i->src(0).getFile()) {
2012 case FILE_MEMORY_GLOBAL: code[1] = 0xe0000000; code[0] = 0x00000000; break;
2013 case FILE_MEMORY_LOCAL: code[1] = 0x7a800000; code[0] = 0x00000002; break;
2014 case FILE_MEMORY_SHARED:
2015 code[0] = 0x00000002;
2016 if (i->subOp == NV50_IR_SUBOP_STORE_UNLOCKED)
2017 code[1] = 0x78400000;
2018 else
2019 code[1] = 0x7ac00000;
2020 break;
2021 default:
2022 assert(!"invalid memory file");
2023 break;
2024 }
2025
2026 if (code[0] & 0x2) {
2027 offset &= 0xffffff;
2028 emitLoadStoreType(i->dType, 0x33);
2029 if (i->src(0).getFile() == FILE_MEMORY_LOCAL)
2030 emitCachingMode(i->cache, 0x2f);
2031 } else {
2032 emitLoadStoreType(i->dType, 0x38);
2033 emitCachingMode(i->cache, 0x3b);
2034 }
2035 code[0] |= offset << 23;
2036 code[1] |= offset >> 9;
2037
2038 // Unlocked store on shared memory can fail.
2039 if (i->src(0).getFile() == FILE_MEMORY_SHARED &&
2040 i->subOp == NV50_IR_SUBOP_STORE_UNLOCKED) {
2041 assert(i->defExists(0));
2042 defId(i->def(0), 32 + 16);
2043 }
2044
2045 emitPredicate(i);
2046
2047 srcId(i->src(1), 2);
2048 srcId(i->src(0).getIndirect(0), 10);
2049 if (i->src(0).getFile() == FILE_MEMORY_GLOBAL &&
2050 i->src(0).isIndirect(0) &&
2051 i->getIndirect(0, 0)->reg.size == 8)
2052 code[1] |= 1 << 23;
2053 }
2054
2055 void
2056 CodeEmitterGK110::emitLOAD(const Instruction *i)
2057 {
2058 int32_t offset = SDATA(i->src(0)).offset;
2059
2060 switch (i->src(0).getFile()) {
2061 case FILE_MEMORY_GLOBAL: code[1] = 0xc0000000; code[0] = 0x00000000; break;
2062 case FILE_MEMORY_LOCAL: code[1] = 0x7a000000; code[0] = 0x00000002; break;
2063 case FILE_MEMORY_SHARED:
2064 code[0] = 0x00000002;
2065 if (i->subOp == NV50_IR_SUBOP_LOAD_LOCKED)
2066 code[1] = 0x77400000;
2067 else
2068 code[1] = 0x7a400000;
2069 break;
2070 case FILE_MEMORY_CONST:
2071 if (!i->src(0).isIndirect(0) && typeSizeof(i->dType) == 4) {
2072 emitMOV(i);
2073 return;
2074 }
2075 offset &= 0xffff;
2076 code[0] = 0x00000002;
2077 code[1] = 0x7c800000 | (i->src(0).get()->reg.fileIndex << 7);
2078 code[1] |= i->subOp << 15;
2079 break;
2080 default:
2081 assert(!"invalid memory file");
2082 break;
2083 }
2084
2085 if (code[0] & 0x2) {
2086 offset &= 0xffffff;
2087 emitLoadStoreType(i->dType, 0x33);
2088 if (i->src(0).getFile() == FILE_MEMORY_LOCAL)
2089 emitCachingMode(i->cache, 0x2f);
2090 } else {
2091 emitLoadStoreType(i->dType, 0x38);
2092 emitCachingMode(i->cache, 0x3b);
2093 }
2094 code[0] |= offset << 23;
2095 code[1] |= offset >> 9;
2096
2097 // Locked store on shared memory can fail.
2098 int r = 0, p = -1;
2099 if (i->src(0).getFile() == FILE_MEMORY_SHARED &&
2100 i->subOp == NV50_IR_SUBOP_LOAD_LOCKED) {
2101 if (i->def(0).getFile() == FILE_PREDICATE) { // p, #
2102 r = -1;
2103 p = 0;
2104 } else if (i->defExists(1)) { // r, p
2105 p = 1;
2106 } else {
2107 assert(!"Expected predicate dest for load locked");
2108 }
2109 }
2110
2111 emitPredicate(i);
2112
2113 if (r >= 0)
2114 defId(i->def(r), 2);
2115 else
2116 code[0] |= 255 << 2;
2117
2118 if (p >= 0)
2119 defId(i->def(p), 32 + 16);
2120
2121 if (i->getIndirect(0, 0)) {
2122 srcId(i->src(0).getIndirect(0), 10);
2123 if (i->getIndirect(0, 0)->reg.size == 8)
2124 code[1] |= 1 << 23;
2125 } else {
2126 code[0] |= 255 << 10;
2127 }
2128 }
2129
2130 uint8_t
2131 CodeEmitterGK110::getSRegEncoding(const ValueRef& ref)
2132 {
2133 switch (SDATA(ref).sv.sv) {
2134 case SV_LANEID: return 0x00;
2135 case SV_PHYSID: return 0x03;
2136 case SV_VERTEX_COUNT: return 0x10;
2137 case SV_INVOCATION_ID: return 0x11;
2138 case SV_YDIR: return 0x12;
2139 case SV_THREAD_KILL: return 0x13;
2140 case SV_TID: return 0x21 + SDATA(ref).sv.index;
2141 case SV_CTAID: return 0x25 + SDATA(ref).sv.index;
2142 case SV_NTID: return 0x29 + SDATA(ref).sv.index;
2143 case SV_GRIDID: return 0x2c;
2144 case SV_NCTAID: return 0x2d + SDATA(ref).sv.index;
2145 case SV_LBASE: return 0x34;
2146 case SV_SBASE: return 0x30;
2147 case SV_CLOCK: return 0x50 + SDATA(ref).sv.index;
2148 default:
2149 assert(!"no sreg for system value");
2150 return 0;
2151 }
2152 }
2153
2154 void
2155 CodeEmitterGK110::emitMOV(const Instruction *i)
2156 {
2157 if (i->def(0).getFile() == FILE_PREDICATE) {
2158 if (i->src(0).getFile() == FILE_GPR) {
2159 // Use ISETP.NE.AND dst, PT, src, RZ, PT
2160 code[0] = 0x00000002;
2161 code[1] = 0xdb500000;
2162
2163 code[0] |= 0x7 << 2;
2164 code[0] |= 0xff << 23;
2165 code[1] |= 0x7 << 10;
2166 srcId(i->src(0), 10);
2167 } else
2168 if (i->src(0).getFile() == FILE_PREDICATE) {
2169 // Use PSETP.AND.AND dst, PT, src, PT, PT
2170 code[0] = 0x00000002;
2171 code[1] = 0x84800000;
2172
2173 code[0] |= 0x7 << 2;
2174 code[1] |= 0x7 << 0;
2175 code[1] |= 0x7 << 10;
2176
2177 srcId(i->src(0), 14);
2178 } else {
2179 assert(!"Unexpected source for predicate destination");
2180 emitNOP(i);
2181 }
2182 emitPredicate(i);
2183 defId(i->def(0), 5);
2184 } else
2185 if (i->src(0).getFile() == FILE_SYSTEM_VALUE) {
2186 code[0] = 0x00000002 | (getSRegEncoding(i->src(0)) << 23);
2187 code[1] = 0x86400000;
2188 emitPredicate(i);
2189 defId(i->def(0), 2);
2190 } else
2191 if (i->src(0).getFile() == FILE_IMMEDIATE) {
2192 code[0] = 0x00000002 | (i->lanes << 14);
2193 code[1] = 0x74000000;
2194 emitPredicate(i);
2195 defId(i->def(0), 2);
2196 setImmediate32(i, 0, Modifier(0));
2197 } else
2198 if (i->src(0).getFile() == FILE_PREDICATE) {
2199 code[0] = 0x00000002;
2200 code[1] = 0x84401c07;
2201 emitPredicate(i);
2202 defId(i->def(0), 2);
2203 srcId(i->src(0), 14);
2204 } else {
2205 emitForm_C(i, 0x24c, 2);
2206 code[1] |= i->lanes << 10;
2207 }
2208 }
2209
2210 static inline bool
2211 uses64bitAddress(const Instruction *ldst)
2212 {
2213 return ldst->src(0).getFile() == FILE_MEMORY_GLOBAL &&
2214 ldst->src(0).isIndirect(0) &&
2215 ldst->getIndirect(0, 0)->reg.size == 8;
2216 }
2217
2218 void
2219 CodeEmitterGK110::emitATOM(const Instruction *i)
2220 {
2221 const bool hasDst = i->defExists(0);
2222 const bool exch = i->subOp == NV50_IR_SUBOP_ATOM_EXCH;
2223
2224 code[0] = 0x00000002;
2225 if (i->subOp == NV50_IR_SUBOP_ATOM_CAS)
2226 code[1] = 0x77800000;
2227 else
2228 code[1] = 0x68000000;
2229
2230 switch (i->subOp) {
2231 case NV50_IR_SUBOP_ATOM_CAS: break;
2232 case NV50_IR_SUBOP_ATOM_EXCH: code[1] |= 0x04000000; break;
2233 default: code[1] |= i->subOp << 23; break;
2234 }
2235
2236 switch (i->dType) {
2237 case TYPE_U32: break;
2238 case TYPE_S32: code[1] |= 0x00100000; break;
2239 case TYPE_U64: code[1] |= 0x00200000; break;
2240 case TYPE_F32: code[1] |= 0x00300000; break;
2241 case TYPE_B128: code[1] |= 0x00400000; break; /* TODO: U128 */
2242 case TYPE_S64: code[1] |= 0x00500000; break;
2243 default: assert(!"unsupported type"); break;
2244 }
2245
2246 emitPredicate(i);
2247
2248 /* TODO: cas: check that src regs line up */
2249 /* TODO: cas: flip bits if $r255 is used */
2250 srcId(i->src(1), 23);
2251
2252 if (hasDst) {
2253 defId(i->def(0), 2);
2254 } else
2255 if (!exch) {
2256 code[0] |= 255 << 2;
2257 }
2258
2259 if (hasDst || !exch) {
2260 const int32_t offset = SDATA(i->src(0)).offset;
2261 assert(offset < 0x80000 && offset >= -0x80000);
2262 code[0] |= (offset & 1) << 31;
2263 code[1] |= (offset & 0xffffe) >> 1;
2264 } else {
2265 srcAddr32(i->src(0), 31);
2266 }
2267
2268 if (i->getIndirect(0, 0)) {
2269 srcId(i->getIndirect(0, 0), 10);
2270 if (i->getIndirect(0, 0)->reg.size == 8)
2271 code[1] |= 1 << 19;
2272 } else {
2273 code[0] |= 255 << 10;
2274 }
2275 }
2276
2277 void
2278 CodeEmitterGK110::emitCCTL(const Instruction *i)
2279 {
2280 int32_t offset = SDATA(i->src(0)).offset;
2281
2282 code[0] = 0x00000002 | (i->subOp << 2);
2283
2284 if (i->src(0).getFile() == FILE_MEMORY_GLOBAL) {
2285 code[1] = 0x7b000000;
2286 } else {
2287 code[1] = 0x7c000000;
2288 offset &= 0xffffff;
2289 }
2290 code[0] |= offset << 23;
2291 code[1] |= offset >> 9;
2292
2293 if (uses64bitAddress(i))
2294 code[1] |= 1 << 23;
2295 srcId(i->src(0).getIndirect(0), 10);
2296
2297 emitPredicate(i);
2298 }
2299
2300 bool
2301 CodeEmitterGK110::emitInstruction(Instruction *insn)
2302 {
2303 const unsigned int size = (writeIssueDelays && !(codeSize & 0x3f)) ? 16 : 8;
2304
2305 if (insn->encSize != 8) {
2306 ERROR("skipping unencodable instruction: ");
2307 insn->print();
2308 return false;
2309 } else
2310 if (codeSize + size > codeSizeLimit) {
2311 ERROR("code emitter output buffer too small\n");
2312 return false;
2313 }
2314
2315 if (writeIssueDelays) {
2316 int id = (codeSize & 0x3f) / 8 - 1;
2317 if (id < 0) {
2318 id += 1;
2319 code[0] = 0x00000000; // cf issue delay "instruction"
2320 code[1] = 0x08000000;
2321 code += 2;
2322 codeSize += 8;
2323 }
2324 uint32_t *data = code - (id * 2 + 2);
2325
2326 switch (id) {
2327 case 0: data[0] |= insn->sched << 2; break;
2328 case 1: data[0] |= insn->sched << 10; break;
2329 case 2: data[0] |= insn->sched << 18; break;
2330 case 3: data[0] |= insn->sched << 26; data[1] |= insn->sched >> 6; break;
2331 case 4: data[1] |= insn->sched << 2; break;
2332 case 5: data[1] |= insn->sched << 10; break;
2333 case 6: data[1] |= insn->sched << 18; break;
2334 default:
2335 assert(0);
2336 break;
2337 }
2338 }
2339
2340 // assert that instructions with multiple defs don't corrupt registers
2341 for (int d = 0; insn->defExists(d); ++d)
2342 assert(insn->asTex() || insn->def(d).rep()->reg.data.id >= 0);
2343
2344 switch (insn->op) {
2345 case OP_MOV:
2346 case OP_RDSV:
2347 emitMOV(insn);
2348 break;
2349 case OP_NOP:
2350 break;
2351 case OP_LOAD:
2352 emitLOAD(insn);
2353 break;
2354 case OP_STORE:
2355 emitSTORE(insn);
2356 break;
2357 case OP_LINTERP:
2358 case OP_PINTERP:
2359 emitINTERP(insn);
2360 break;
2361 case OP_VFETCH:
2362 emitVFETCH(insn);
2363 break;
2364 case OP_EXPORT:
2365 emitEXPORT(insn);
2366 break;
2367 case OP_AFETCH:
2368 emitAFETCH(insn);
2369 break;
2370 case OP_PFETCH:
2371 emitPFETCH(insn);
2372 break;
2373 case OP_EMIT:
2374 case OP_RESTART:
2375 emitOUT(insn);
2376 break;
2377 case OP_ADD:
2378 case OP_SUB:
2379 if (insn->dType == TYPE_F64)
2380 emitDADD(insn);
2381 else if (isFloatType(insn->dType))
2382 emitFADD(insn);
2383 else
2384 emitUADD(insn);
2385 break;
2386 case OP_MUL:
2387 if (insn->dType == TYPE_F64)
2388 emitDMUL(insn);
2389 else if (isFloatType(insn->dType))
2390 emitFMUL(insn);
2391 else
2392 emitIMUL(insn);
2393 break;
2394 case OP_MAD:
2395 case OP_FMA:
2396 if (insn->dType == TYPE_F64)
2397 emitDMAD(insn);
2398 else if (isFloatType(insn->dType))
2399 emitFMAD(insn);
2400 else
2401 emitIMAD(insn);
2402 break;
2403 case OP_MADSP:
2404 emitMADSP(insn);
2405 break;
2406 case OP_SAD:
2407 emitISAD(insn);
2408 break;
2409 case OP_NOT:
2410 emitNOT(insn);
2411 break;
2412 case OP_AND:
2413 emitLogicOp(insn, 0);
2414 break;
2415 case OP_OR:
2416 emitLogicOp(insn, 1);
2417 break;
2418 case OP_XOR:
2419 emitLogicOp(insn, 2);
2420 break;
2421 case OP_SHL:
2422 case OP_SHR:
2423 emitShift(insn);
2424 break;
2425 case OP_SET:
2426 case OP_SET_AND:
2427 case OP_SET_OR:
2428 case OP_SET_XOR:
2429 emitSET(insn->asCmp());
2430 break;
2431 case OP_SELP:
2432 emitSELP(insn);
2433 break;
2434 case OP_SLCT:
2435 emitSLCT(insn->asCmp());
2436 break;
2437 case OP_MIN:
2438 case OP_MAX:
2439 emitMINMAX(insn);
2440 break;
2441 case OP_ABS:
2442 case OP_NEG:
2443 case OP_CEIL:
2444 case OP_FLOOR:
2445 case OP_TRUNC:
2446 case OP_SAT:
2447 emitCVT(insn);
2448 break;
2449 case OP_CVT:
2450 if (insn->def(0).getFile() == FILE_PREDICATE ||
2451 insn->src(0).getFile() == FILE_PREDICATE)
2452 emitMOV(insn);
2453 else
2454 emitCVT(insn);
2455 break;
2456 case OP_RSQ:
2457 emitSFnOp(insn, 5 + 2 * insn->subOp);
2458 break;
2459 case OP_RCP:
2460 emitSFnOp(insn, 4 + 2 * insn->subOp);
2461 break;
2462 case OP_LG2:
2463 emitSFnOp(insn, 3);
2464 break;
2465 case OP_EX2:
2466 emitSFnOp(insn, 2);
2467 break;
2468 case OP_SIN:
2469 emitSFnOp(insn, 1);
2470 break;
2471 case OP_COS:
2472 emitSFnOp(insn, 0);
2473 break;
2474 case OP_PRESIN:
2475 case OP_PREEX2:
2476 emitPreOp(insn);
2477 break;
2478 case OP_TEX:
2479 case OP_TXB:
2480 case OP_TXL:
2481 case OP_TXD:
2482 case OP_TXF:
2483 case OP_TXG:
2484 case OP_TXLQ:
2485 emitTEX(insn->asTex());
2486 break;
2487 case OP_TXQ:
2488 emitTXQ(insn->asTex());
2489 break;
2490 case OP_TEXBAR:
2491 emitTEXBAR(insn);
2492 break;
2493 case OP_PIXLD:
2494 emitPIXLD(insn);
2495 break;
2496 case OP_BRA:
2497 case OP_CALL:
2498 case OP_PRERET:
2499 case OP_RET:
2500 case OP_DISCARD:
2501 case OP_EXIT:
2502 case OP_PRECONT:
2503 case OP_CONT:
2504 case OP_PREBREAK:
2505 case OP_BREAK:
2506 case OP_JOINAT:
2507 case OP_BRKPT:
2508 case OP_QUADON:
2509 case OP_QUADPOP:
2510 emitFlow(insn);
2511 break;
2512 case OP_QUADOP:
2513 emitQUADOP(insn, insn->subOp, insn->lanes);
2514 break;
2515 case OP_DFDX:
2516 emitQUADOP(insn, insn->src(0).mod.neg() ? 0x66 : 0x99, 0x4);
2517 break;
2518 case OP_DFDY:
2519 emitQUADOP(insn, insn->src(0).mod.neg() ? 0x5a : 0xa5, 0x5);
2520 break;
2521 case OP_POPCNT:
2522 emitPOPC(insn);
2523 break;
2524 case OP_INSBF:
2525 emitINSBF(insn);
2526 break;
2527 case OP_EXTBF:
2528 emitEXTBF(insn);
2529 break;
2530 case OP_BFIND:
2531 emitBFIND(insn);
2532 break;
2533 case OP_PERMT:
2534 emitPERMT(insn);
2535 break;
2536 case OP_JOIN:
2537 emitNOP(insn);
2538 insn->join = 1;
2539 break;
2540 case OP_BAR:
2541 emitBAR(insn);
2542 break;
2543 case OP_MEMBAR:
2544 emitMEMBAR(insn);
2545 break;
2546 case OP_ATOM:
2547 emitATOM(insn);
2548 break;
2549 case OP_CCTL:
2550 emitCCTL(insn);
2551 break;
2552 case OP_VOTE:
2553 emitVOTE(insn);
2554 break;
2555 case OP_SULDB:
2556 emitSULDGB(insn->asTex());
2557 break;
2558 case OP_SUSTB:
2559 case OP_SUSTP:
2560 emitSUSTGx(insn->asTex());
2561 break;
2562 case OP_SUBFM:
2563 case OP_SUCLAMP:
2564 case OP_SUEAU:
2565 emitSUCalc(insn);
2566 break;
2567 case OP_VSHL:
2568 emitVSHL(insn);
2569 break;
2570 case OP_PHI:
2571 case OP_UNION:
2572 case OP_CONSTRAINT:
2573 ERROR("operation should have been eliminated");
2574 return false;
2575 case OP_EXP:
2576 case OP_LOG:
2577 case OP_SQRT:
2578 case OP_POW:
2579 ERROR("operation should have been lowered\n");
2580 return false;
2581 default:
2582 ERROR("unknown op: %u\n", insn->op);
2583 return false;
2584 }
2585
2586 if (insn->join)
2587 code[0] |= 1 << 22;
2588
2589 code += 2;
2590 codeSize += 8;
2591 return true;
2592 }
2593
2594 uint32_t
2595 CodeEmitterGK110::getMinEncodingSize(const Instruction *i) const
2596 {
2597 // No more short instruction encodings.
2598 return 8;
2599 }
2600
2601 void
2602 CodeEmitterGK110::prepareEmission(Function *func)
2603 {
2604 const Target *targ = func->getProgram()->getTarget();
2605
2606 CodeEmitter::prepareEmission(func);
2607
2608 if (targ->hasSWSched)
2609 calculateSchedDataNVC0(targ, func);
2610 }
2611
2612 CodeEmitterGK110::CodeEmitterGK110(const TargetNVC0 *target)
2613 : CodeEmitter(target),
2614 targNVC0(target),
2615 writeIssueDelays(target->hasSWSched)
2616 {
2617 code = NULL;
2618 codeSize = codeSizeLimit = 0;
2619 relocInfo = NULL;
2620 }
2621
2622 CodeEmitter *
2623 TargetNVC0::createCodeEmitterGK110(Program::Type type)
2624 {
2625 CodeEmitterGK110 *emit = new CodeEmitterGK110(this);
2626 emit->setProgramType(type);
2627 return emit;
2628 }
2629
2630 } // namespace nv50_ir