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