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