4a63eb184bf98171ec6413376acc9f641ef6c9da
[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
58 void modNegAbsF32_3b(const Instruction *, const int s);
59
60 void emitCondCode(CondCode cc, int pos, uint8_t mask);
61 void emitInterpMode(const Instruction *);
62 void emitLoadStoreType(DataType ty, const int pos);
63 void emitCachingMode(CacheMode c, const int pos);
64
65 inline uint8_t getSRegEncoding(const ValueRef&);
66
67 void emitRoundMode(RoundMode, const int pos, const int rintPos);
68 void emitRoundModeF(RoundMode, const int pos);
69 void emitRoundModeI(RoundMode, const int pos);
70
71 void emitNegAbs12(const Instruction *);
72
73 void emitNOP(const Instruction *);
74
75 void emitLOAD(const Instruction *);
76 void emitSTORE(const Instruction *);
77 void emitMOV(const Instruction *);
78 void emitMEMBAR(const Instruction *);
79
80 void emitINTERP(const Instruction *);
81 void emitAFETCH(const Instruction *);
82 void emitPFETCH(const Instruction *);
83 void emitVFETCH(const Instruction *);
84 void emitEXPORT(const Instruction *);
85 void emitOUT(const Instruction *);
86
87 void emitUADD(const Instruction *);
88 void emitFADD(const Instruction *);
89 void emitDADD(const Instruction *);
90 void emitIMUL(const Instruction *);
91 void emitFMUL(const Instruction *);
92 void emitDMUL(const Instruction *);
93 void emitIMAD(const Instruction *);
94 void emitISAD(const Instruction *);
95 void emitFMAD(const Instruction *);
96 void emitDMAD(const Instruction *);
97
98 void emitNOT(const Instruction *);
99 void emitLogicOp(const Instruction *, uint8_t subOp);
100 void emitPOPC(const Instruction *);
101 void emitINSBF(const Instruction *);
102 void emitEXTBF(const Instruction *);
103 void emitBFIND(const Instruction *);
104 void emitShift(const Instruction *);
105
106 void emitSFnOp(const Instruction *, uint8_t subOp);
107
108 void emitCVT(const Instruction *);
109 void emitMINMAX(const Instruction *);
110 void emitPreOp(const Instruction *);
111
112 void emitSET(const CmpInstruction *);
113 void emitSLCT(const CmpInstruction *);
114 void emitSELP(const Instruction *);
115
116 void emitTEXBAR(const Instruction *);
117 void emitTEX(const TexInstruction *);
118 void emitTEXCSAA(const TexInstruction *);
119 void emitTXQ(const TexInstruction *);
120
121 void emitQUADOP(const Instruction *, uint8_t qOp, uint8_t laneMask);
122
123 void emitPIXLD(const Instruction *);
124
125 void emitBAR(const Instruction *);
126
127 void emitFlow(const Instruction *);
128
129 inline void defId(const ValueDef&, const int pos);
130 inline void srcId(const ValueRef&, const int pos);
131 inline void srcId(const ValueRef *, const int pos);
132 inline void srcId(const Instruction *, int s, const int pos);
133
134 inline void srcAddr32(const ValueRef&, const int pos); // address / 4
135
136 inline bool isLIMM(const ValueRef&, DataType ty, bool mod = false);
137 };
138
139 #define GK110_GPR_ZERO 255
140
141 #define NEG_(b, s) \
142 if (i->src(s).mod.neg()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
143 #define ABS_(b, s) \
144 if (i->src(s).mod.abs()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
145
146 #define NOT_(b, s) if (i->src(s).mod & Modifier(NV50_IR_MOD_NOT)) \
147 code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
148
149 #define FTZ_(b) if (i->ftz) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
150 #define DNZ_(b) if (i->dnz) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
151
152 #define SAT_(b) if (i->saturate) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
153
154 #define RND_(b, t) emitRoundMode##t(i->rnd, 0x##b)
155
156 #define SDATA(a) ((a).rep()->reg.data)
157 #define DDATA(a) ((a).rep()->reg.data)
158
159 void CodeEmitterGK110::srcId(const ValueRef& src, const int pos)
160 {
161 code[pos / 32] |= (src.get() ? SDATA(src).id : GK110_GPR_ZERO) << (pos % 32);
162 }
163
164 void CodeEmitterGK110::srcId(const ValueRef *src, const int pos)
165 {
166 code[pos / 32] |= (src ? SDATA(*src).id : GK110_GPR_ZERO) << (pos % 32);
167 }
168
169 void CodeEmitterGK110::srcId(const Instruction *insn, int s, int pos)
170 {
171 int r = insn->srcExists(s) ? SDATA(insn->src(s)).id : GK110_GPR_ZERO;
172 code[pos / 32] |= r << (pos % 32);
173 }
174
175 void CodeEmitterGK110::srcAddr32(const ValueRef& src, const int pos)
176 {
177 code[pos / 32] |= (SDATA(src).offset >> 2) << (pos % 32);
178 }
179
180 void CodeEmitterGK110::defId(const ValueDef& def, const int pos)
181 {
182 code[pos / 32] |= (def.get() ? DDATA(def).id : GK110_GPR_ZERO) << (pos % 32);
183 }
184
185 bool CodeEmitterGK110::isLIMM(const ValueRef& ref, DataType ty, bool mod)
186 {
187 const ImmediateValue *imm = ref.get()->asImm();
188
189 return imm && (imm->reg.data.u32 & ((ty == TYPE_F32) ? 0xfff : 0xfff00000));
190 }
191
192 void
193 CodeEmitterGK110::emitRoundMode(RoundMode rnd, const int pos, const int rintPos)
194 {
195 bool rint = false;
196 uint8_t n;
197
198 switch (rnd) {
199 case ROUND_MI: rint = true; /* fall through */ case ROUND_M: n = 1; break;
200 case ROUND_PI: rint = true; /* fall through */ case ROUND_P: n = 2; break;
201 case ROUND_ZI: rint = true; /* fall through */ case ROUND_Z: n = 3; break;
202 default:
203 rint = rnd == ROUND_NI;
204 n = 0;
205 assert(rnd == ROUND_N || rnd == ROUND_NI);
206 break;
207 }
208 code[pos / 32] |= n << (pos % 32);
209 if (rint && rintPos >= 0)
210 code[rintPos / 32] |= 1 << (rintPos % 32);
211 }
212
213 void
214 CodeEmitterGK110::emitRoundModeF(RoundMode rnd, const int pos)
215 {
216 uint8_t n;
217
218 switch (rnd) {
219 case ROUND_M: n = 1; break;
220 case ROUND_P: n = 2; break;
221 case ROUND_Z: n = 3; break;
222 default:
223 n = 0;
224 assert(rnd == ROUND_N);
225 break;
226 }
227 code[pos / 32] |= n << (pos % 32);
228 }
229
230 void
231 CodeEmitterGK110::emitRoundModeI(RoundMode rnd, const int pos)
232 {
233 uint8_t n;
234
235 switch (rnd) {
236 case ROUND_MI: n = 1; break;
237 case ROUND_PI: n = 2; break;
238 case ROUND_ZI: n = 3; break;
239 default:
240 n = 0;
241 assert(rnd == ROUND_NI);
242 break;
243 }
244 code[pos / 32] |= n << (pos % 32);
245 }
246
247 void CodeEmitterGK110::emitCondCode(CondCode cc, int pos, uint8_t mask)
248 {
249 uint8_t n;
250
251 switch (cc) {
252 case CC_FL: n = 0x00; break;
253 case CC_LT: n = 0x01; break;
254 case CC_EQ: n = 0x02; break;
255 case CC_LE: n = 0x03; break;
256 case CC_GT: n = 0x04; break;
257 case CC_NE: n = 0x05; break;
258 case CC_GE: n = 0x06; break;
259 case CC_LTU: n = 0x09; break;
260 case CC_EQU: n = 0x0a; break;
261 case CC_LEU: n = 0x0b; break;
262 case CC_GTU: n = 0x0c; break;
263 case CC_NEU: n = 0x0d; break;
264 case CC_GEU: n = 0x0e; break;
265 case CC_TR: n = 0x0f; break;
266 case CC_NO: n = 0x10; break;
267 case CC_NC: n = 0x11; break;
268 case CC_NS: n = 0x12; break;
269 case CC_NA: n = 0x13; break;
270 case CC_A: n = 0x14; break;
271 case CC_S: n = 0x15; break;
272 case CC_C: n = 0x16; break;
273 case CC_O: n = 0x17; break;
274 default:
275 n = 0;
276 assert(!"invalid condition code");
277 break;
278 }
279 code[pos / 32] |= (n & mask) << (pos % 32);
280 }
281
282 void
283 CodeEmitterGK110::emitPredicate(const Instruction *i)
284 {
285 if (i->predSrc >= 0) {
286 srcId(i->src(i->predSrc), 18);
287 if (i->cc == CC_NOT_P)
288 code[0] |= 8 << 18; // negate
289 assert(i->getPredicate()->reg.file == FILE_PREDICATE);
290 } else {
291 code[0] |= 7 << 18;
292 }
293 }
294
295 void
296 CodeEmitterGK110::setCAddress14(const ValueRef& src)
297 {
298 const Storage& res = src.get()->asSym()->reg;
299 const int32_t addr = res.data.offset / 4;
300
301 code[0] |= (addr & 0x01ff) << 23;
302 code[1] |= (addr & 0x3e00) >> 9;
303 code[1] |= res.fileIndex << 5;
304 }
305
306 void
307 CodeEmitterGK110::setShortImmediate(const Instruction *i, const int s)
308 {
309 const uint32_t u32 = i->getSrc(s)->asImm()->reg.data.u32;
310 const uint64_t u64 = i->getSrc(s)->asImm()->reg.data.u64;
311
312 if (i->sType == TYPE_F32) {
313 assert(!(u32 & 0x00000fff));
314 code[0] |= ((u32 & 0x001ff000) >> 12) << 23;
315 code[1] |= ((u32 & 0x7fe00000) >> 21);
316 code[1] |= ((u32 & 0x80000000) >> 4);
317 } else
318 if (i->sType == TYPE_F64) {
319 assert(!(u64 & 0x00000fffffffffffULL));
320 code[0] |= ((u64 & 0x001ff00000000000ULL) >> 44) << 23;
321 code[1] |= ((u64 & 0x7fe0000000000000ULL) >> 53);
322 code[1] |= ((u64 & 0x8000000000000000ULL) >> 36);
323 } else {
324 assert((u32 & 0xfff00000) == 0 || (u32 & 0xfff00000) == 0xfff00000);
325 code[0] |= (u32 & 0x001ff) << 23;
326 code[1] |= (u32 & 0x7fe00) >> 9;
327 code[1] |= (u32 & 0x80000) << 8;
328 }
329 }
330
331 void
332 CodeEmitterGK110::setImmediate32(const Instruction *i, const int s,
333 Modifier mod)
334 {
335 uint32_t u32 = i->getSrc(s)->asImm()->reg.data.u32;
336
337 if (mod) {
338 ImmediateValue imm(i->getSrc(s)->asImm(), i->sType);
339 mod.applyTo(imm);
340 u32 = imm.reg.data.u32;
341 }
342
343 code[0] |= u32 << 23;
344 code[1] |= u32 >> 9;
345 }
346
347 void
348 CodeEmitterGK110::emitForm_L(const Instruction *i, uint32_t opc, uint8_t ctg,
349 Modifier mod)
350 {
351 code[0] = ctg;
352 code[1] = opc << 20;
353
354 emitPredicate(i);
355
356 defId(i->def(0), 2);
357
358 for (int s = 0; s < 3 && i->srcExists(s); ++s) {
359 switch (i->src(s).getFile()) {
360 case FILE_GPR:
361 srcId(i->src(s), s ? 42 : 10);
362 break;
363 case FILE_IMMEDIATE:
364 setImmediate32(i, s, mod);
365 break;
366 default:
367 break;
368 }
369 }
370 }
371
372
373 void
374 CodeEmitterGK110::emitForm_C(const Instruction *i, uint32_t opc, uint8_t ctg)
375 {
376 code[0] = ctg;
377 code[1] = opc << 20;
378
379 emitPredicate(i);
380
381 defId(i->def(0), 2);
382
383 switch (i->src(0).getFile()) {
384 case FILE_MEMORY_CONST:
385 code[1] |= 0x4 << 28;
386 setCAddress14(i->src(0));
387 break;
388 case FILE_GPR:
389 code[1] |= 0xc << 28;
390 srcId(i->src(0), 23);
391 break;
392 default:
393 assert(0);
394 break;
395 }
396 }
397
398 // 0x2 for GPR, c[] and 0x1 for short immediate
399 void
400 CodeEmitterGK110::emitForm_21(const Instruction *i, uint32_t opc2,
401 uint32_t opc1)
402 {
403 const bool imm = i->srcExists(1) && i->src(1).getFile() == FILE_IMMEDIATE;
404
405 int s1 = 23;
406 if (i->srcExists(2) && i->src(2).getFile() == FILE_MEMORY_CONST)
407 s1 = 42;
408
409 if (imm) {
410 code[0] = 0x1;
411 code[1] = opc1 << 20;
412 } else {
413 code[0] = 0x2;
414 code[1] = (0xc << 28) | (opc2 << 20);
415 }
416
417 emitPredicate(i);
418
419 defId(i->def(0), 2);
420
421 for (int s = 0; s < 3 && i->srcExists(s); ++s) {
422 switch (i->src(s).getFile()) {
423 case FILE_MEMORY_CONST:
424 code[1] &= (s == 2) ? ~(0x4 << 28) : ~(0x8 << 28);
425 setCAddress14(i->src(s));
426 break;
427 case FILE_IMMEDIATE:
428 setShortImmediate(i, s);
429 break;
430 case FILE_GPR:
431 srcId(i->src(s), s ? ((s == 2) ? 42 : s1) : 10);
432 break;
433 default:
434 // ignore here, can be predicate or flags, but must not be address
435 break;
436 }
437 }
438 // 0x0 = invalid
439 // 0xc = rrr
440 // 0x8 = rrc
441 // 0x4 = rcr
442 assert(imm || (code[1] & (0xc << 28)));
443 }
444
445 inline void
446 CodeEmitterGK110::modNegAbsF32_3b(const Instruction *i, const int s)
447 {
448 if (i->src(s).mod.abs()) code[1] &= ~(1 << 27);
449 if (i->src(s).mod.neg()) code[1] ^= (1 << 27);
450 }
451
452 void
453 CodeEmitterGK110::emitNOP(const Instruction *i)
454 {
455 code[0] = 0x00003c02;
456 code[1] = 0x85800000;
457
458 if (i)
459 emitPredicate(i);
460 else
461 code[0] = 0x001c3c02;
462 }
463
464 void
465 CodeEmitterGK110::emitFMAD(const Instruction *i)
466 {
467 assert(!isLIMM(i->src(1), TYPE_F32));
468
469 emitForm_21(i, 0x0c0, 0x940);
470
471 NEG_(34, 2);
472 SAT_(35);
473 RND_(36, F);
474 FTZ_(38);
475 DNZ_(39);
476
477 bool neg1 = (i->src(0).mod ^ i->src(1).mod).neg();
478
479 if (code[0] & 0x1) {
480 if (neg1)
481 code[1] ^= 1 << 27;
482 } else
483 if (neg1) {
484 code[1] |= 1 << 19;
485 }
486 }
487
488 void
489 CodeEmitterGK110::emitDMAD(const Instruction *i)
490 {
491 assert(!i->saturate);
492 assert(!i->ftz);
493
494 emitForm_21(i, 0x1b8, 0xb38);
495
496 NEG_(34, 2);
497 RND_(36, F);
498
499 bool neg1 = (i->src(0).mod ^ i->src(1).mod).neg();
500
501 if (code[0] & 0x1) {
502 if (neg1)
503 code[1] ^= 1 << 27;
504 } else
505 if (neg1) {
506 code[1] |= 1 << 19;
507 }
508 }
509
510 void
511 CodeEmitterGK110::emitFMUL(const Instruction *i)
512 {
513 bool neg = (i->src(0).mod ^ i->src(1).mod).neg();
514
515 assert(i->postFactor >= -3 && i->postFactor <= 3);
516
517 if (isLIMM(i->src(1), TYPE_F32)) {
518 emitForm_L(i, 0x200, 0x2, Modifier(0));
519
520 FTZ_(38);
521 DNZ_(39);
522 SAT_(3a);
523 if (neg)
524 code[1] ^= 1 << 22;
525
526 assert(i->postFactor == 0);
527 } else {
528 emitForm_21(i, 0x234, 0xc34);
529 code[1] |= ((i->postFactor > 0) ?
530 (7 - i->postFactor) : (0 - i->postFactor)) << 12;
531
532 RND_(2a, F);
533 FTZ_(2f);
534 DNZ_(30);
535 SAT_(35);
536
537 if (code[0] & 0x1) {
538 if (neg)
539 code[1] ^= 1 << 27;
540 } else
541 if (neg) {
542 code[1] |= 1 << 19;
543 }
544 }
545 }
546
547 void
548 CodeEmitterGK110::emitDMUL(const Instruction *i)
549 {
550 bool neg = (i->src(0).mod ^ i->src(1).mod).neg();
551
552 assert(!i->postFactor);
553 assert(!i->saturate);
554 assert(!i->ftz);
555 assert(!i->dnz);
556
557 emitForm_21(i, 0x240, 0xc40);
558
559 RND_(2a, F);
560
561 if (code[0] & 0x1) {
562 if (neg)
563 code[1] ^= 1 << 27;
564 } else
565 if (neg) {
566 code[1] |= 1 << 19;
567 }
568 }
569
570 void
571 CodeEmitterGK110::emitIMUL(const Instruction *i)
572 {
573 assert(!i->src(0).mod.neg() && !i->src(1).mod.neg());
574 assert(!i->src(0).mod.abs() && !i->src(1).mod.abs());
575
576 if (isLIMM(i->src(1), TYPE_S32)) {
577 emitForm_L(i, 0x280, 2, Modifier(0));
578
579 if (i->subOp == NV50_IR_SUBOP_MUL_HIGH)
580 code[1] |= 1 << 24;
581 if (i->sType == TYPE_S32)
582 code[1] |= 3 << 25;
583 } else {
584 emitForm_21(i, 0x21c, 0xc1c);
585
586 if (i->subOp == NV50_IR_SUBOP_MUL_HIGH)
587 code[1] |= 1 << 10;
588 if (i->sType == TYPE_S32)
589 code[1] |= 3 << 11;
590 }
591 }
592
593 void
594 CodeEmitterGK110::emitFADD(const Instruction *i)
595 {
596 if (isLIMM(i->src(1), TYPE_F32)) {
597 assert(i->rnd == ROUND_N);
598 assert(!i->saturate);
599
600 Modifier mod = i->src(1).mod ^
601 Modifier(i->op == OP_SUB ? NV50_IR_MOD_NEG : 0);
602
603 emitForm_L(i, 0x400, 0, mod);
604
605 FTZ_(3a);
606 NEG_(3b, 0);
607 ABS_(39, 0);
608 } else {
609 emitForm_21(i, 0x22c, 0xc2c);
610
611 FTZ_(2f);
612 RND_(2a, F);
613 ABS_(31, 0);
614 NEG_(33, 0);
615 SAT_(35);
616
617 if (code[0] & 0x1) {
618 modNegAbsF32_3b(i, 1);
619 if (i->op == OP_SUB) code[1] ^= 1 << 27;
620 } else {
621 ABS_(34, 1);
622 NEG_(30, 1);
623 if (i->op == OP_SUB) code[1] ^= 1 << 16;
624 }
625 }
626 }
627
628 void
629 CodeEmitterGK110::emitDADD(const Instruction *i)
630 {
631 assert(!i->saturate);
632 assert(!i->ftz);
633
634 emitForm_21(i, 0x238, 0xc38);
635 RND_(2a, F);
636 ABS_(31, 0);
637 NEG_(33, 0);
638 if (code[0] & 0x1) {
639 modNegAbsF32_3b(i, 1);
640 if (i->op == OP_SUB) code[1] ^= 1 << 27;
641 } else {
642 NEG_(30, 1);
643 ABS_(34, 1);
644 if (i->op == OP_SUB) code[1] ^= 1 << 16;
645 }
646 }
647
648 void
649 CodeEmitterGK110::emitUADD(const Instruction *i)
650 {
651 uint8_t addOp = (i->src(0).mod.neg() << 1) | i->src(1).mod.neg();
652
653 if (i->op == OP_SUB)
654 addOp ^= 1;
655
656 assert(!i->src(0).mod.abs() && !i->src(1).mod.abs());
657
658 if (isLIMM(i->src(1), TYPE_S32)) {
659 emitForm_L(i, 0x400, 1, Modifier((addOp & 1) ? NV50_IR_MOD_NEG : 0));
660
661 if (addOp & 2)
662 code[1] |= 1 << 27;
663
664 assert(!i->defExists(1));
665 assert(i->flagsSrc < 0);
666
667 SAT_(39);
668 } else {
669 emitForm_21(i, 0x208, 0xc08);
670
671 assert(addOp != 3); // would be add-plus-one
672
673 code[1] |= addOp << 19;
674
675 if (i->defExists(1))
676 code[1] |= 1 << 18; // write carry
677 if (i->flagsSrc >= 0)
678 code[1] |= 1 << 14; // add carry
679
680 SAT_(35);
681 }
682 }
683
684 // TODO: shl-add
685 void
686 CodeEmitterGK110::emitIMAD(const Instruction *i)
687 {
688 uint8_t addOp =
689 (i->src(2).mod.neg() << 1) | (i->src(0).mod.neg() ^ i->src(1).mod.neg());
690
691 emitForm_21(i, 0x100, 0xa00);
692
693 assert(addOp != 3);
694 code[1] |= addOp << 26;
695
696 if (i->sType == TYPE_S32)
697 code[1] |= (1 << 19) | (1 << 24);
698
699 if (code[0] & 0x1) {
700 assert(!i->subOp);
701 SAT_(39);
702 } else {
703 if (i->subOp == NV50_IR_SUBOP_MUL_HIGH)
704 code[1] |= 1 << 25;
705 SAT_(35);
706 }
707 }
708
709 void
710 CodeEmitterGK110::emitISAD(const Instruction *i)
711 {
712 assert(i->dType == TYPE_S32 || i->dType == TYPE_U32);
713
714 emitForm_21(i, 0x1f4, 0xb74);
715
716 if (i->dType == TYPE_S32)
717 code[1] |= 1 << 19;
718 }
719
720 void
721 CodeEmitterGK110::emitNOT(const Instruction *i)
722 {
723 code[0] = 0x0003fc02; // logop(mov2) dst, 0, not src
724 code[1] = 0x22003800;
725
726 emitPredicate(i);
727
728 defId(i->def(0), 2);
729
730 switch (i->src(0).getFile()) {
731 case FILE_GPR:
732 code[1] |= 0xc << 28;
733 srcId(i->src(0), 23);
734 break;
735 case FILE_MEMORY_CONST:
736 code[1] |= 0x4 << 28;
737 setCAddress14(i->src(1));
738 break;
739 default:
740 assert(0);
741 break;
742 }
743 }
744
745 void
746 CodeEmitterGK110::emitLogicOp(const Instruction *i, uint8_t subOp)
747 {
748 if (isLIMM(i->src(1), TYPE_S32)) {
749 emitForm_L(i, 0x200, 0, i->src(1).mod);
750 code[1] |= subOp << 24;
751 NOT_(3a, 0);
752 } else {
753 emitForm_21(i, 0x220, 0xc20);
754 code[1] |= subOp << 12;
755 NOT_(2a, 0);
756 NOT_(2b, 1);
757 }
758 }
759
760 void
761 CodeEmitterGK110::emitPOPC(const Instruction *i)
762 {
763 assert(!isLIMM(i->src(1), TYPE_S32, true));
764
765 emitForm_21(i, 0x204, 0xc04);
766
767 NOT_(2a, 0);
768 if (!(code[0] & 0x1))
769 NOT_(2b, 1);
770 }
771
772 void
773 CodeEmitterGK110::emitINSBF(const Instruction *i)
774 {
775 emitForm_21(i, 0x1f8, 0xb78);
776 }
777
778 void
779 CodeEmitterGK110::emitEXTBF(const Instruction *i)
780 {
781 emitForm_21(i, 0x600, 0xc00);
782
783 if (i->dType == TYPE_S32)
784 code[1] |= 0x80000;
785 if (i->subOp == NV50_IR_SUBOP_EXTBF_REV)
786 code[1] |= 0x800;
787 }
788
789 void
790 CodeEmitterGK110::emitBFIND(const Instruction *i)
791 {
792 emitForm_C(i, 0x218, 0x2);
793
794 if (i->dType == TYPE_S32)
795 code[1] |= 0x80000;
796 if (i->src(0).mod == Modifier(NV50_IR_MOD_NOT))
797 code[1] |= 0x800;
798 if (i->subOp == NV50_IR_SUBOP_BFIND_SAMT)
799 code[1] |= 0x1000;
800 }
801
802 void
803 CodeEmitterGK110::emitShift(const Instruction *i)
804 {
805 if (i->op == OP_SHR) {
806 emitForm_21(i, 0x214, 0xc14);
807 if (isSignedType(i->dType))
808 code[1] |= 1 << 19;
809 } else {
810 emitForm_21(i, 0x224, 0xc24);
811 }
812
813 if (i->subOp == NV50_IR_SUBOP_SHIFT_WRAP)
814 code[1] |= 1 << 10;
815 }
816
817 void
818 CodeEmitterGK110::emitPreOp(const Instruction *i)
819 {
820 emitForm_C(i, 0x248, 0x2);
821
822 if (i->op == OP_PREEX2)
823 code[1] |= 1 << 10;
824
825 NEG_(30, 0);
826 ABS_(34, 0);
827 }
828
829 void
830 CodeEmitterGK110::emitSFnOp(const Instruction *i, uint8_t subOp)
831 {
832 code[0] = 0x00000002 | (subOp << 23);
833 code[1] = 0x84000000;
834
835 emitPredicate(i);
836
837 defId(i->def(0), 2);
838 srcId(i->src(0), 10);
839
840 NEG_(33, 0);
841 ABS_(31, 0);
842 SAT_(35);
843 }
844
845 void
846 CodeEmitterGK110::emitMINMAX(const Instruction *i)
847 {
848 uint32_t op2, op1;
849
850 switch (i->dType) {
851 case TYPE_U32:
852 case TYPE_S32:
853 op2 = 0x210;
854 op1 = 0xc10;
855 break;
856 case TYPE_F32:
857 op2 = 0x230;
858 op1 = 0xc30;
859 break;
860 case TYPE_F64:
861 op2 = 0x228;
862 op1 = 0xc28;
863 break;
864 default:
865 assert(0);
866 op2 = 0;
867 op1 = 0;
868 break;
869 }
870 emitForm_21(i, op2, op1);
871
872 if (i->dType == TYPE_S32)
873 code[1] |= 1 << 19;
874 code[1] |= (i->op == OP_MIN) ? 0x1c00 : 0x3c00; // [!]pt
875
876 FTZ_(2f);
877 ABS_(31, 0);
878 NEG_(33, 0);
879 if (code[0] & 0x1) {
880 modNegAbsF32_3b(i, 1);
881 } else {
882 ABS_(34, 1);
883 NEG_(30, 1);
884 }
885 }
886
887 void
888 CodeEmitterGK110::emitCVT(const Instruction *i)
889 {
890 const bool f2f = isFloatType(i->dType) && isFloatType(i->sType);
891 const bool f2i = !isFloatType(i->dType) && isFloatType(i->sType);
892 const bool i2f = isFloatType(i->dType) && !isFloatType(i->sType);
893
894 bool sat = i->saturate;
895 bool abs = i->src(0).mod.abs();
896 bool neg = i->src(0).mod.neg();
897
898 RoundMode rnd = i->rnd;
899
900 switch (i->op) {
901 case OP_CEIL: rnd = f2f ? ROUND_PI : ROUND_P; break;
902 case OP_FLOOR: rnd = f2f ? ROUND_MI : ROUND_M; break;
903 case OP_TRUNC: rnd = f2f ? ROUND_ZI : ROUND_Z; break;
904 case OP_SAT: sat = true; break;
905 case OP_NEG: neg = !neg; break;
906 case OP_ABS: abs = true; neg = false; break;
907 default:
908 break;
909 }
910
911 DataType dType;
912
913 if (i->op == OP_NEG && i->dType == TYPE_U32)
914 dType = TYPE_S32;
915 else
916 dType = i->dType;
917
918
919 uint32_t op;
920
921 if (f2f) op = 0x254;
922 else if (f2i) op = 0x258;
923 else if (i2f) op = 0x25c;
924 else op = 0x260;
925
926 emitForm_C(i, op, 0x2);
927
928 FTZ_(2f);
929 if (neg) code[1] |= 1 << 16;
930 if (abs) code[1] |= 1 << 20;
931 if (sat) code[1] |= 1 << 21;
932
933 emitRoundMode(rnd, 32 + 10, f2f ? (32 + 13) : -1);
934
935 code[0] |= typeSizeofLog2(dType) << 10;
936 code[0] |= typeSizeofLog2(i->sType) << 12;
937 code[1] |= i->subOp << 12;
938
939 if (isSignedIntType(dType))
940 code[0] |= 0x4000;
941 if (isSignedIntType(i->sType))
942 code[0] |= 0x8000;
943 }
944
945 void
946 CodeEmitterGK110::emitSET(const CmpInstruction *i)
947 {
948 uint16_t op1, op2;
949
950 if (i->def(0).getFile() == FILE_PREDICATE) {
951 switch (i->sType) {
952 case TYPE_F32: op2 = 0x1d8; op1 = 0xb58; break;
953 case TYPE_F64: op2 = 0x1c0; op1 = 0xb40; break;
954 default:
955 op2 = 0x1b0;
956 op1 = 0xb30;
957 break;
958 }
959 emitForm_21(i, op2, op1);
960
961 NEG_(2e, 0);
962 ABS_(9, 0);
963 if (!(code[0] & 0x1)) {
964 NEG_(8, 1);
965 ABS_(2f, 1);
966 } else {
967 modNegAbsF32_3b(i, 1);
968 }
969 FTZ_(32);
970
971 // normal DST field is negated predicate result
972 code[0] = (code[0] & ~0xfc) | ((code[0] << 3) & 0xe0);
973 if (i->defExists(1))
974 defId(i->def(1), 2);
975 else
976 code[0] |= 0x1c;
977 } else {
978 switch (i->sType) {
979 case TYPE_F32: op2 = 0x000; op1 = 0x800; break;
980 case TYPE_F64: op2 = 0x080; op1 = 0x900; break;
981 default:
982 op2 = 0x1a8;
983 op1 = 0xb28;
984 break;
985 }
986 emitForm_21(i, op2, op1);
987
988 NEG_(2e, 0);
989 ABS_(39, 0);
990 if (!(code[0] & 0x1)) {
991 NEG_(38, 1);
992 ABS_(2f, 1);
993 } else {
994 modNegAbsF32_3b(i, 1);
995 }
996 FTZ_(3a);
997
998 if (i->dType == TYPE_F32) {
999 if (isFloatType(i->sType))
1000 code[1] |= 1 << 23;
1001 else
1002 code[1] |= 1 << 15;
1003 }
1004 }
1005 if (i->sType == TYPE_S32)
1006 code[1] |= 1 << 19;
1007
1008 if (i->op != OP_SET) {
1009 switch (i->op) {
1010 case OP_SET_AND: code[1] |= 0x0 << 16; break;
1011 case OP_SET_OR: code[1] |= 0x1 << 16; break;
1012 case OP_SET_XOR: code[1] |= 0x2 << 16; break;
1013 default:
1014 assert(0);
1015 break;
1016 }
1017 srcId(i->src(2), 0x2a);
1018 } else {
1019 code[1] |= 0x7 << 10;
1020 }
1021 emitCondCode(i->setCond,
1022 isFloatType(i->sType) ? 0x33 : 0x34,
1023 isFloatType(i->sType) ? 0xf : 0x7);
1024 }
1025
1026 void
1027 CodeEmitterGK110::emitSLCT(const CmpInstruction *i)
1028 {
1029 CondCode cc = i->setCond;
1030 if (i->src(2).mod.neg())
1031 cc = reverseCondCode(cc);
1032
1033 if (i->dType == TYPE_F32) {
1034 emitForm_21(i, 0x1d0, 0xb50);
1035 FTZ_(32);
1036 emitCondCode(cc, 0x33, 0xf);
1037 } else {
1038 emitForm_21(i, 0x1a0, 0xb20);
1039 emitCondCode(cc, 0x34, 0x7);
1040 }
1041 }
1042
1043 void CodeEmitterGK110::emitSELP(const Instruction *i)
1044 {
1045 emitForm_21(i, 0x250, 0x050);
1046
1047 if ((i->cc == CC_NOT_P) ^ (bool)(i->src(2).mod & Modifier(NV50_IR_MOD_NOT)))
1048 code[1] |= 1 << 13;
1049 }
1050
1051 void CodeEmitterGK110::emitTEXBAR(const Instruction *i)
1052 {
1053 code[0] = 0x0000003e | (i->subOp << 23);
1054 code[1] = 0x77000000;
1055
1056 emitPredicate(i);
1057 }
1058
1059 void CodeEmitterGK110::emitTEXCSAA(const TexInstruction *i)
1060 {
1061 code[0] = 0x00000002;
1062 code[1] = 0x76c00000;
1063
1064 code[1] |= i->tex.r << 9;
1065 // code[1] |= i->tex.s << (9 + 8);
1066
1067 if (i->tex.liveOnly)
1068 code[0] |= 0x80000000;
1069
1070 defId(i->def(0), 2);
1071 srcId(i->src(0), 10);
1072 }
1073
1074 static inline bool
1075 isNextIndependentTex(const TexInstruction *i)
1076 {
1077 if (!i->next || !isTextureOp(i->next->op))
1078 return false;
1079 if (i->getDef(0)->interfers(i->next->getSrc(0)))
1080 return false;
1081 return !i->next->srcExists(1) || !i->getDef(0)->interfers(i->next->getSrc(1));
1082 }
1083
1084 void
1085 CodeEmitterGK110::emitTEX(const TexInstruction *i)
1086 {
1087 const bool ind = i->tex.rIndirectSrc >= 0;
1088
1089 if (ind) {
1090 code[0] = 0x00000002;
1091 switch (i->op) {
1092 case OP_TXD:
1093 code[1] = 0x7e000000;
1094 break;
1095 case OP_TXLQ:
1096 code[1] = 0x7e800000;
1097 break;
1098 case OP_TXF:
1099 code[1] = 0x78000000;
1100 break;
1101 case OP_TXG:
1102 code[1] = 0x7dc00000;
1103 break;
1104 default:
1105 code[1] = 0x7d800000;
1106 break;
1107 }
1108 } else {
1109 switch (i->op) {
1110 case OP_TXD:
1111 code[0] = 0x00000002;
1112 code[1] = 0x76000000;
1113 code[1] |= i->tex.r << 9;
1114 break;
1115 case OP_TXLQ:
1116 code[0] = 0x00000002;
1117 code[1] = 0x76800000;
1118 code[1] |= i->tex.r << 9;
1119 break;
1120 case OP_TXF:
1121 code[0] = 0x00000002;
1122 code[1] = 0x70000000;
1123 code[1] |= i->tex.r << 13;
1124 break;
1125 case OP_TXG:
1126 code[0] = 0x00000001;
1127 code[1] = 0x70000000;
1128 code[1] |= i->tex.r << 15;
1129 break;
1130 default:
1131 code[0] = 0x00000001;
1132 code[1] = 0x60000000;
1133 code[1] |= i->tex.r << 15;
1134 break;
1135 }
1136 }
1137
1138 code[1] |= isNextIndependentTex(i) ? 0x1 : 0x2; // t : p mode
1139
1140 if (i->tex.liveOnly)
1141 code[0] |= 0x80000000;
1142
1143 switch (i->op) {
1144 case OP_TEX: break;
1145 case OP_TXB: code[1] |= 0x2000; break;
1146 case OP_TXL: code[1] |= 0x3000; break;
1147 case OP_TXF: break;
1148 case OP_TXG: break;
1149 case OP_TXD: break;
1150 case OP_TXLQ: break;
1151 default:
1152 assert(!"invalid texture op");
1153 break;
1154 }
1155
1156 if (i->op == OP_TXF) {
1157 if (!i->tex.levelZero)
1158 code[1] |= 0x1000;
1159 } else
1160 if (i->tex.levelZero) {
1161 code[1] |= 0x1000;
1162 }
1163
1164 if (i->op != OP_TXD && i->tex.derivAll)
1165 code[1] |= 0x200;
1166
1167 emitPredicate(i);
1168
1169 code[1] |= i->tex.mask << 2;
1170
1171 const int src1 = (i->predSrc == 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1172
1173 defId(i->def(0), 2);
1174 srcId(i->src(0), 10);
1175 srcId(i, src1, 23);
1176
1177 if (i->op == OP_TXG) code[1] |= i->tex.gatherComp << 13;
1178
1179 // texture target:
1180 code[1] |= (i->tex.target.isCube() ? 3 : (i->tex.target.getDim() - 1)) << 7;
1181 if (i->tex.target.isArray())
1182 code[1] |= 0x40;
1183 if (i->tex.target.isShadow())
1184 code[1] |= 0x400;
1185 if (i->tex.target == TEX_TARGET_2D_MS ||
1186 i->tex.target == TEX_TARGET_2D_MS_ARRAY)
1187 code[1] |= 0x800;
1188
1189 if (i->srcExists(src1) && i->src(src1).getFile() == FILE_IMMEDIATE) {
1190 // ?
1191 }
1192
1193 if (i->tex.useOffsets == 1) {
1194 switch (i->op) {
1195 case OP_TXF: code[1] |= 0x200; break;
1196 case OP_TXD: code[1] |= 0x00400000; break;
1197 default: code[1] |= 0x800; break;
1198 }
1199 }
1200 if (i->tex.useOffsets == 4)
1201 code[1] |= 0x1000;
1202 }
1203
1204 void
1205 CodeEmitterGK110::emitTXQ(const TexInstruction *i)
1206 {
1207 code[0] = 0x00000002;
1208 code[1] = 0x75400001;
1209
1210 switch (i->tex.query) {
1211 case TXQ_DIMS: code[0] |= 0x01 << 25; break;
1212 case TXQ_TYPE: code[0] |= 0x02 << 25; break;
1213 case TXQ_SAMPLE_POSITION: code[0] |= 0x05 << 25; break;
1214 case TXQ_FILTER: code[0] |= 0x10 << 25; break;
1215 case TXQ_LOD: code[0] |= 0x12 << 25; break;
1216 case TXQ_BORDER_COLOUR: code[0] |= 0x16 << 25; break;
1217 default:
1218 assert(!"invalid texture query");
1219 break;
1220 }
1221
1222 code[1] |= i->tex.mask << 2;
1223 code[1] |= i->tex.r << 9;
1224 if (/*i->tex.sIndirectSrc >= 0 || */i->tex.rIndirectSrc >= 0)
1225 code[1] |= 0x08000000;
1226
1227 defId(i->def(0), 2);
1228 srcId(i->src(0), 10);
1229
1230 emitPredicate(i);
1231 }
1232
1233 void
1234 CodeEmitterGK110::emitQUADOP(const Instruction *i, uint8_t qOp, uint8_t laneMask)
1235 {
1236 code[0] = 0x00000002 | ((qOp & 1) << 31);
1237 code[1] = 0x7fc00000 | (qOp >> 1) | (laneMask << 12);
1238
1239 defId(i->def(0), 2);
1240 srcId(i->src(0), 10);
1241 srcId(i->srcExists(1) ? i->src(1) : i->src(0), 23);
1242
1243 if (i->op == OP_QUADOP && progType != Program::TYPE_FRAGMENT)
1244 code[1] |= 1 << 9; // dall
1245
1246 emitPredicate(i);
1247 }
1248
1249 void
1250 CodeEmitterGK110::emitPIXLD(const Instruction *i)
1251 {
1252 emitForm_L(i, 0x7f4, 2, Modifier(0));
1253 code[1] |= i->subOp << 2;
1254 code[1] |= 0x00070000;
1255 }
1256
1257 void
1258 CodeEmitterGK110::emitBAR(const Instruction *i)
1259 {
1260 /* TODO */
1261 emitNOP(i);
1262 }
1263
1264 void
1265 CodeEmitterGK110::emitFlow(const Instruction *i)
1266 {
1267 const FlowInstruction *f = i->asFlow();
1268
1269 unsigned mask; // bit 0: predicate, bit 1: target
1270
1271 code[0] = 0x00000000;
1272
1273 switch (i->op) {
1274 case OP_BRA:
1275 code[1] = f->absolute ? 0x10800000 : 0x12000000;
1276 if (i->srcExists(0) && i->src(0).getFile() == FILE_MEMORY_CONST)
1277 code[0] |= 0x80;
1278 mask = 3;
1279 break;
1280 case OP_CALL:
1281 code[1] = f->absolute ? 0x11000000 : 0x13000000;
1282 if (i->srcExists(0) && i->src(0).getFile() == FILE_MEMORY_CONST)
1283 code[0] |= 0x80;
1284 mask = 2;
1285 break;
1286
1287 case OP_EXIT: code[1] = 0x18000000; mask = 1; break;
1288 case OP_RET: code[1] = 0x19000000; mask = 1; break;
1289 case OP_DISCARD: code[1] = 0x19800000; mask = 1; break;
1290 case OP_BREAK: code[1] = 0x1a000000; mask = 1; break;
1291 case OP_CONT: code[1] = 0x1a800000; mask = 1; break;
1292
1293 case OP_JOINAT: code[1] = 0x14800000; mask = 2; break;
1294 case OP_PREBREAK: code[1] = 0x15000000; mask = 2; break;
1295 case OP_PRECONT: code[1] = 0x15800000; mask = 2; break;
1296 case OP_PRERET: code[1] = 0x13800000; mask = 2; break;
1297
1298 case OP_QUADON: code[1] = 0x1b800000; mask = 0; break;
1299 case OP_QUADPOP: code[1] = 0x1c000000; mask = 0; break;
1300 case OP_BRKPT: code[1] = 0x00000000; mask = 0; break;
1301 default:
1302 assert(!"invalid flow operation");
1303 return;
1304 }
1305
1306 if (mask & 1) {
1307 emitPredicate(i);
1308 if (i->flagsSrc < 0)
1309 code[0] |= 0x3c;
1310 }
1311
1312 if (!f)
1313 return;
1314
1315 if (f->allWarp)
1316 code[0] |= 1 << 9;
1317 if (f->limit)
1318 code[0] |= 1 << 8;
1319
1320 if (f->op == OP_CALL) {
1321 if (f->builtin) {
1322 assert(f->absolute);
1323 uint32_t pcAbs = targNVC0->getBuiltinOffset(f->target.builtin);
1324 addReloc(RelocEntry::TYPE_BUILTIN, 0, pcAbs, 0xff800000, 23);
1325 addReloc(RelocEntry::TYPE_BUILTIN, 1, pcAbs, 0x007fffff, -9);
1326 } else {
1327 assert(!f->absolute);
1328 int32_t pcRel = f->target.fn->binPos - (codeSize + 8);
1329 code[0] |= (pcRel & 0x1ff) << 23;
1330 code[1] |= (pcRel >> 9) & 0x7fff;
1331 }
1332 } else
1333 if (mask & 2) {
1334 int32_t pcRel = f->target.bb->binPos - (codeSize + 8);
1335 if (writeIssueDelays && !(f->target.bb->binPos & 0x3f))
1336 pcRel += 8;
1337 // currently we don't want absolute branches
1338 assert(!f->absolute);
1339 code[0] |= (pcRel & 0x1ff) << 23;
1340 code[1] |= (pcRel >> 9) & 0x7fff;
1341 }
1342 }
1343
1344 void
1345 CodeEmitterGK110::emitAFETCH(const Instruction *i)
1346 {
1347 uint32_t offset = i->src(0).get()->reg.data.offset & 0x7ff;
1348
1349 code[0] = 0x00000002 | (offset << 23);
1350 code[1] = 0x7d000000 | (offset >> 9);
1351
1352 if (i->getSrc(0)->reg.file == FILE_SHADER_OUTPUT)
1353 code[1] |= 0x8;
1354
1355 emitPredicate(i);
1356
1357 defId(i->def(0), 2);
1358 srcId(i->src(0).getIndirect(0), 10);
1359 }
1360
1361 void
1362 CodeEmitterGK110::emitPFETCH(const Instruction *i)
1363 {
1364 uint32_t prim = i->src(0).get()->reg.data.u32;
1365
1366 code[0] = 0x00000002 | ((prim & 0xff) << 23);
1367 code[1] = 0x7f800000;
1368
1369 emitPredicate(i);
1370
1371 const int src1 = (i->predSrc == 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1372
1373 defId(i->def(0), 2);
1374 srcId(i, src1, 10);
1375 }
1376
1377 void
1378 CodeEmitterGK110::emitVFETCH(const Instruction *i)
1379 {
1380 unsigned int size = typeSizeof(i->dType);
1381 uint32_t offset = i->src(0).get()->reg.data.offset;
1382
1383 code[0] = 0x00000002 | (offset << 23);
1384 code[1] = 0x7ec00000 | (offset >> 9);
1385 code[1] |= (size / 4 - 1) << 18;
1386
1387 if (i->perPatch)
1388 code[1] |= 0x4;
1389 if (i->getSrc(0)->reg.file == FILE_SHADER_OUTPUT)
1390 code[1] |= 0x8; // yes, TCPs can read from *outputs* of other threads
1391
1392 emitPredicate(i);
1393
1394 defId(i->def(0), 2);
1395 srcId(i->src(0).getIndirect(0), 10);
1396 srcId(i->src(0).getIndirect(1), 32 + 10); // vertex address
1397 }
1398
1399 void
1400 CodeEmitterGK110::emitEXPORT(const Instruction *i)
1401 {
1402 unsigned int size = typeSizeof(i->dType);
1403 uint32_t offset = i->src(0).get()->reg.data.offset;
1404
1405 code[0] = 0x00000002 | (offset << 23);
1406 code[1] = 0x7f000000 | (offset >> 9);
1407 code[1] |= (size / 4 - 1) << 18;
1408
1409 if (i->perPatch)
1410 code[1] |= 0x4;
1411
1412 emitPredicate(i);
1413
1414 assert(i->src(1).getFile() == FILE_GPR);
1415
1416 srcId(i->src(0).getIndirect(0), 10);
1417 srcId(i->src(0).getIndirect(1), 32 + 10); // vertex base address
1418 srcId(i->src(1), 2);
1419 }
1420
1421 void
1422 CodeEmitterGK110::emitOUT(const Instruction *i)
1423 {
1424 assert(i->src(0).getFile() == FILE_GPR);
1425
1426 emitForm_21(i, 0x1f0, 0xb70);
1427
1428 if (i->op == OP_EMIT)
1429 code[1] |= 1 << 10;
1430 if (i->op == OP_RESTART || i->subOp == NV50_IR_SUBOP_EMIT_RESTART)
1431 code[1] |= 1 << 11;
1432 }
1433
1434 void
1435 CodeEmitterGK110::emitInterpMode(const Instruction *i)
1436 {
1437 code[1] |= (i->ipa & 0x3) << 21; // TODO: INTERP_SAMPLEID
1438 code[1] |= (i->ipa & 0xc) << (19 - 2);
1439 }
1440
1441 static void
1442 interpApply(const InterpEntry *entry, uint32_t *code,
1443 bool force_persample_interp, bool flatshade)
1444 {
1445 int ipa = entry->ipa;
1446 int reg = entry->reg;
1447 int loc = entry->loc;
1448
1449 if (flatshade &&
1450 (ipa & NV50_IR_INTERP_MODE_MASK) == NV50_IR_INTERP_SC) {
1451 ipa = NV50_IR_INTERP_FLAT;
1452 reg = 0xff;
1453 } else if (force_persample_interp &&
1454 (ipa & NV50_IR_INTERP_SAMPLE_MASK) == NV50_IR_INTERP_DEFAULT &&
1455 (ipa & NV50_IR_INTERP_MODE_MASK) != NV50_IR_INTERP_FLAT) {
1456 ipa |= NV50_IR_INTERP_CENTROID;
1457 }
1458 code[loc + 1] &= ~(0xf << 19);
1459 code[loc + 1] |= (ipa & 0x3) << 21;
1460 code[loc + 1] |= (ipa & 0xc) << (19 - 2);
1461 code[loc + 0] &= ~(0xff << 23);
1462 code[loc + 0] |= reg << 23;
1463 }
1464
1465 void
1466 CodeEmitterGK110::emitINTERP(const Instruction *i)
1467 {
1468 const uint32_t base = i->getSrc(0)->reg.data.offset;
1469
1470 code[0] = 0x00000002 | (base << 31);
1471 code[1] = 0x74800000 | (base >> 1);
1472
1473 if (i->saturate)
1474 code[1] |= 1 << 18;
1475
1476 if (i->op == OP_PINTERP) {
1477 srcId(i->src(1), 23);
1478 addInterp(i->ipa, SDATA(i->src(1)).id, interpApply);
1479 } else {
1480 code[0] |= 0xff << 23;
1481 addInterp(i->ipa, 0xff, interpApply);
1482 }
1483
1484 srcId(i->src(0).getIndirect(0), 10);
1485 emitInterpMode(i);
1486
1487 emitPredicate(i);
1488 defId(i->def(0), 2);
1489
1490 if (i->getSampleMode() == NV50_IR_INTERP_OFFSET)
1491 srcId(i->src(i->op == OP_PINTERP ? 2 : 1), 32 + 10);
1492 else
1493 code[1] |= 0xff << 10;
1494 }
1495
1496 void
1497 CodeEmitterGK110::emitLoadStoreType(DataType ty, const int pos)
1498 {
1499 uint8_t n;
1500
1501 switch (ty) {
1502 case TYPE_U8:
1503 n = 0;
1504 break;
1505 case TYPE_S8:
1506 n = 1;
1507 break;
1508 case TYPE_U16:
1509 n = 2;
1510 break;
1511 case TYPE_S16:
1512 n = 3;
1513 break;
1514 case TYPE_F32:
1515 case TYPE_U32:
1516 case TYPE_S32:
1517 n = 4;
1518 break;
1519 case TYPE_F64:
1520 case TYPE_U64:
1521 case TYPE_S64:
1522 n = 5;
1523 break;
1524 case TYPE_B128:
1525 n = 6;
1526 break;
1527 default:
1528 n = 0;
1529 assert(!"invalid ld/st type");
1530 break;
1531 }
1532 code[pos / 32] |= n << (pos % 32);
1533 }
1534
1535 void
1536 CodeEmitterGK110::emitCachingMode(CacheMode c, const int pos)
1537 {
1538 uint8_t n;
1539
1540 switch (c) {
1541 case CACHE_CA:
1542 // case CACHE_WB:
1543 n = 0;
1544 break;
1545 case CACHE_CG:
1546 n = 1;
1547 break;
1548 case CACHE_CS:
1549 n = 2;
1550 break;
1551 case CACHE_CV:
1552 // case CACHE_WT:
1553 n = 3;
1554 break;
1555 default:
1556 n = 0;
1557 assert(!"invalid caching mode");
1558 break;
1559 }
1560 code[pos / 32] |= n << (pos % 32);
1561 }
1562
1563 void
1564 CodeEmitterGK110::emitSTORE(const Instruction *i)
1565 {
1566 int32_t offset = SDATA(i->src(0)).offset;
1567
1568 switch (i->src(0).getFile()) {
1569 case FILE_MEMORY_GLOBAL: code[1] = 0xe0000000; code[0] = 0x00000000; break;
1570 case FILE_MEMORY_LOCAL: code[1] = 0x7a800000; code[0] = 0x00000002; break;
1571 case FILE_MEMORY_SHARED: code[1] = 0x7ac00000; code[0] = 0x00000002; break;
1572 default:
1573 assert(!"invalid memory file");
1574 break;
1575 }
1576
1577 if (i->src(0).getFile() != FILE_MEMORY_GLOBAL)
1578 offset &= 0xffffff;
1579
1580 if (code[0] & 0x2) {
1581 emitLoadStoreType(i->dType, 0x33);
1582 if (i->src(0).getFile() == FILE_MEMORY_LOCAL)
1583 emitCachingMode(i->cache, 0x2f);
1584 } else {
1585 emitLoadStoreType(i->dType, 0x38);
1586 emitCachingMode(i->cache, 0x3b);
1587 }
1588 code[0] |= offset << 23;
1589 code[1] |= offset >> 9;
1590
1591 emitPredicate(i);
1592
1593 srcId(i->src(1), 2);
1594 srcId(i->src(0).getIndirect(0), 10);
1595 }
1596
1597 void
1598 CodeEmitterGK110::emitLOAD(const Instruction *i)
1599 {
1600 int32_t offset = SDATA(i->src(0)).offset;
1601
1602 switch (i->src(0).getFile()) {
1603 case FILE_MEMORY_GLOBAL: code[1] = 0xc0000000; code[0] = 0x00000000; break;
1604 case FILE_MEMORY_LOCAL: code[1] = 0x7a000000; code[0] = 0x00000002; break;
1605 case FILE_MEMORY_SHARED: code[1] = 0x7ac00000; code[0] = 0x00000002; break;
1606 case FILE_MEMORY_CONST:
1607 if (!i->src(0).isIndirect(0) && typeSizeof(i->dType) == 4) {
1608 emitMOV(i);
1609 return;
1610 }
1611 offset &= 0xffff;
1612 code[0] = 0x00000002;
1613 code[1] = 0x7c800000 | (i->src(0).get()->reg.fileIndex << 7);
1614 code[1] |= i->subOp << 15;
1615 break;
1616 default:
1617 assert(!"invalid memory file");
1618 break;
1619 }
1620
1621 if (code[0] & 0x2) {
1622 offset &= 0xffffff;
1623 emitLoadStoreType(i->dType, 0x33);
1624 if (i->src(0).getFile() == FILE_MEMORY_LOCAL)
1625 emitCachingMode(i->cache, 0x2f);
1626 } else {
1627 emitLoadStoreType(i->dType, 0x38);
1628 emitCachingMode(i->cache, 0x3b);
1629 }
1630 code[0] |= offset << 23;
1631 code[1] |= offset >> 9;
1632
1633 emitPredicate(i);
1634
1635 defId(i->def(0), 2);
1636 srcId(i->src(0).getIndirect(0), 10);
1637 }
1638
1639 uint8_t
1640 CodeEmitterGK110::getSRegEncoding(const ValueRef& ref)
1641 {
1642 switch (SDATA(ref).sv.sv) {
1643 case SV_LANEID: return 0x00;
1644 case SV_PHYSID: return 0x03;
1645 case SV_VERTEX_COUNT: return 0x10;
1646 case SV_INVOCATION_ID: return 0x11;
1647 case SV_YDIR: return 0x12;
1648 case SV_THREAD_KILL: return 0x13;
1649 case SV_TID: return 0x21 + SDATA(ref).sv.index;
1650 case SV_CTAID: return 0x25 + SDATA(ref).sv.index;
1651 case SV_NTID: return 0x29 + SDATA(ref).sv.index;
1652 case SV_GRIDID: return 0x2c;
1653 case SV_NCTAID: return 0x2d + SDATA(ref).sv.index;
1654 case SV_LBASE: return 0x34;
1655 case SV_SBASE: return 0x30;
1656 case SV_CLOCK: return 0x50 + SDATA(ref).sv.index;
1657 default:
1658 assert(!"no sreg for system value");
1659 return 0;
1660 }
1661 }
1662
1663 void
1664 CodeEmitterGK110::emitMOV(const Instruction *i)
1665 {
1666 if (i->src(0).getFile() == FILE_SYSTEM_VALUE) {
1667 code[0] = 0x00000002 | (getSRegEncoding(i->src(0)) << 23);
1668 code[1] = 0x86400000;
1669 emitPredicate(i);
1670 defId(i->def(0), 2);
1671 } else
1672 if (i->src(0).getFile() == FILE_IMMEDIATE) {
1673 code[0] = 0x00000002 | (i->lanes << 14);
1674 code[1] = 0x74000000;
1675 emitPredicate(i);
1676 defId(i->def(0), 2);
1677 setImmediate32(i, 0, Modifier(0));
1678 } else
1679 if (i->src(0).getFile() == FILE_PREDICATE) {
1680 code[0] = 0x00000002;
1681 code[1] = 0x84401c07;
1682 emitPredicate(i);
1683 defId(i->def(0), 2);
1684 srcId(i->src(0), 14);
1685 } else {
1686 emitForm_C(i, 0x24c, 2);
1687 code[1] |= i->lanes << 10;
1688 }
1689 }
1690
1691 void CodeEmitterGK110::emitMEMBAR(const Instruction *i)
1692 {
1693 code[0] = 0x00000002 | NV50_IR_SUBOP_MEMBAR_SCOPE(i->subOp) << 8;
1694 code[1] = 0x7cc00000;
1695
1696 emitPredicate(i);
1697 }
1698
1699 bool
1700 CodeEmitterGK110::emitInstruction(Instruction *insn)
1701 {
1702 const unsigned int size = (writeIssueDelays && !(codeSize & 0x3f)) ? 16 : 8;
1703
1704 if (insn->encSize != 8) {
1705 ERROR("skipping unencodable instruction: ");
1706 insn->print();
1707 return false;
1708 } else
1709 if (codeSize + size > codeSizeLimit) {
1710 ERROR("code emitter output buffer too small\n");
1711 return false;
1712 }
1713
1714 if (writeIssueDelays) {
1715 int id = (codeSize & 0x3f) / 8 - 1;
1716 if (id < 0) {
1717 id += 1;
1718 code[0] = 0x00000000; // cf issue delay "instruction"
1719 code[1] = 0x08000000;
1720 code += 2;
1721 codeSize += 8;
1722 }
1723 uint32_t *data = code - (id * 2 + 2);
1724
1725 switch (id) {
1726 case 0: data[0] |= insn->sched << 2; break;
1727 case 1: data[0] |= insn->sched << 10; break;
1728 case 2: data[0] |= insn->sched << 18; break;
1729 case 3: data[0] |= insn->sched << 26; data[1] |= insn->sched >> 6; break;
1730 case 4: data[1] |= insn->sched << 2; break;
1731 case 5: data[1] |= insn->sched << 10; break;
1732 case 6: data[1] |= insn->sched << 18; break;
1733 default:
1734 assert(0);
1735 break;
1736 }
1737 }
1738
1739 // assert that instructions with multiple defs don't corrupt registers
1740 for (int d = 0; insn->defExists(d); ++d)
1741 assert(insn->asTex() || insn->def(d).rep()->reg.data.id >= 0);
1742
1743 switch (insn->op) {
1744 case OP_MOV:
1745 case OP_RDSV:
1746 emitMOV(insn);
1747 break;
1748 case OP_NOP:
1749 break;
1750 case OP_LOAD:
1751 emitLOAD(insn);
1752 break;
1753 case OP_STORE:
1754 emitSTORE(insn);
1755 break;
1756 case OP_LINTERP:
1757 case OP_PINTERP:
1758 emitINTERP(insn);
1759 break;
1760 case OP_VFETCH:
1761 emitVFETCH(insn);
1762 break;
1763 case OP_EXPORT:
1764 emitEXPORT(insn);
1765 break;
1766 case OP_AFETCH:
1767 emitAFETCH(insn);
1768 break;
1769 case OP_PFETCH:
1770 emitPFETCH(insn);
1771 break;
1772 case OP_EMIT:
1773 case OP_RESTART:
1774 emitOUT(insn);
1775 break;
1776 case OP_ADD:
1777 case OP_SUB:
1778 if (insn->dType == TYPE_F64)
1779 emitDADD(insn);
1780 else if (isFloatType(insn->dType))
1781 emitFADD(insn);
1782 else
1783 emitUADD(insn);
1784 break;
1785 case OP_MUL:
1786 if (insn->dType == TYPE_F64)
1787 emitDMUL(insn);
1788 else if (isFloatType(insn->dType))
1789 emitFMUL(insn);
1790 else
1791 emitIMUL(insn);
1792 break;
1793 case OP_MAD:
1794 case OP_FMA:
1795 if (insn->dType == TYPE_F64)
1796 emitDMAD(insn);
1797 else if (isFloatType(insn->dType))
1798 emitFMAD(insn);
1799 else
1800 emitIMAD(insn);
1801 break;
1802 case OP_SAD:
1803 emitISAD(insn);
1804 break;
1805 case OP_NOT:
1806 emitNOT(insn);
1807 break;
1808 case OP_AND:
1809 emitLogicOp(insn, 0);
1810 break;
1811 case OP_OR:
1812 emitLogicOp(insn, 1);
1813 break;
1814 case OP_XOR:
1815 emitLogicOp(insn, 2);
1816 break;
1817 case OP_SHL:
1818 case OP_SHR:
1819 emitShift(insn);
1820 break;
1821 case OP_SET:
1822 case OP_SET_AND:
1823 case OP_SET_OR:
1824 case OP_SET_XOR:
1825 emitSET(insn->asCmp());
1826 break;
1827 case OP_SELP:
1828 emitSELP(insn);
1829 break;
1830 case OP_SLCT:
1831 emitSLCT(insn->asCmp());
1832 break;
1833 case OP_MIN:
1834 case OP_MAX:
1835 emitMINMAX(insn);
1836 break;
1837 case OP_ABS:
1838 case OP_NEG:
1839 case OP_CEIL:
1840 case OP_FLOOR:
1841 case OP_TRUNC:
1842 case OP_CVT:
1843 case OP_SAT:
1844 emitCVT(insn);
1845 break;
1846 case OP_RSQ:
1847 emitSFnOp(insn, 5 + 2 * insn->subOp);
1848 break;
1849 case OP_RCP:
1850 emitSFnOp(insn, 4 + 2 * insn->subOp);
1851 break;
1852 case OP_LG2:
1853 emitSFnOp(insn, 3);
1854 break;
1855 case OP_EX2:
1856 emitSFnOp(insn, 2);
1857 break;
1858 case OP_SIN:
1859 emitSFnOp(insn, 1);
1860 break;
1861 case OP_COS:
1862 emitSFnOp(insn, 0);
1863 break;
1864 case OP_PRESIN:
1865 case OP_PREEX2:
1866 emitPreOp(insn);
1867 break;
1868 case OP_TEX:
1869 case OP_TXB:
1870 case OP_TXL:
1871 case OP_TXD:
1872 case OP_TXF:
1873 case OP_TXG:
1874 case OP_TXLQ:
1875 emitTEX(insn->asTex());
1876 break;
1877 case OP_TXQ:
1878 emitTXQ(insn->asTex());
1879 break;
1880 case OP_TEXBAR:
1881 emitTEXBAR(insn);
1882 break;
1883 case OP_PIXLD:
1884 emitPIXLD(insn);
1885 break;
1886 case OP_BRA:
1887 case OP_CALL:
1888 case OP_PRERET:
1889 case OP_RET:
1890 case OP_DISCARD:
1891 case OP_EXIT:
1892 case OP_PRECONT:
1893 case OP_CONT:
1894 case OP_PREBREAK:
1895 case OP_BREAK:
1896 case OP_JOINAT:
1897 case OP_BRKPT:
1898 case OP_QUADON:
1899 case OP_QUADPOP:
1900 emitFlow(insn);
1901 break;
1902 case OP_QUADOP:
1903 emitQUADOP(insn, insn->subOp, insn->lanes);
1904 break;
1905 case OP_DFDX:
1906 emitQUADOP(insn, insn->src(0).mod.neg() ? 0x66 : 0x99, 0x4);
1907 break;
1908 case OP_DFDY:
1909 emitQUADOP(insn, insn->src(0).mod.neg() ? 0x5a : 0xa5, 0x5);
1910 break;
1911 case OP_POPCNT:
1912 emitPOPC(insn);
1913 break;
1914 case OP_INSBF:
1915 emitINSBF(insn);
1916 break;
1917 case OP_EXTBF:
1918 emitEXTBF(insn);
1919 break;
1920 case OP_BFIND:
1921 emitBFIND(insn);
1922 break;
1923 case OP_JOIN:
1924 emitNOP(insn);
1925 insn->join = 1;
1926 break;
1927 case OP_BAR:
1928 emitBAR(insn);
1929 break;
1930 case OP_MEMBAR:
1931 emitMEMBAR(insn);
1932 break;
1933 case OP_PHI:
1934 case OP_UNION:
1935 case OP_CONSTRAINT:
1936 ERROR("operation should have been eliminated");
1937 return false;
1938 case OP_EXP:
1939 case OP_LOG:
1940 case OP_SQRT:
1941 case OP_POW:
1942 ERROR("operation should have been lowered\n");
1943 return false;
1944 default:
1945 ERROR("unknown op: %u\n", insn->op);
1946 return false;
1947 }
1948
1949 if (insn->join)
1950 code[0] |= 1 << 22;
1951
1952 code += 2;
1953 codeSize += 8;
1954 return true;
1955 }
1956
1957 uint32_t
1958 CodeEmitterGK110::getMinEncodingSize(const Instruction *i) const
1959 {
1960 // No more short instruction encodings.
1961 return 8;
1962 }
1963
1964 void
1965 CodeEmitterGK110::prepareEmission(Function *func)
1966 {
1967 const Target *targ = func->getProgram()->getTarget();
1968
1969 CodeEmitter::prepareEmission(func);
1970
1971 if (targ->hasSWSched)
1972 calculateSchedDataNVC0(targ, func);
1973 }
1974
1975 CodeEmitterGK110::CodeEmitterGK110(const TargetNVC0 *target)
1976 : CodeEmitter(target),
1977 targNVC0(target),
1978 writeIssueDelays(target->hasSWSched)
1979 {
1980 code = NULL;
1981 codeSize = codeSizeLimit = 0;
1982 relocInfo = NULL;
1983 }
1984
1985 CodeEmitter *
1986 TargetNVC0::createCodeEmitterGK110(Program::Type type)
1987 {
1988 CodeEmitterGK110 *emit = new CodeEmitterGK110(this);
1989 emit->setProgramType(type);
1990 return emit;
1991 }
1992
1993 } // namespace nv50_ir