2 * Copyright 2011 Christoph Bumiller
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "nv50_ir_target_nvc0.h"
27 // Argh, all these assertions ...
29 class CodeEmitterNVC0
: public CodeEmitter
32 CodeEmitterNVC0(const TargetNVC0
*);
34 virtual bool emitInstruction(Instruction
*);
35 virtual uint32_t getMinEncodingSize(const Instruction
*) const;
37 inline void setProgramType(Program::Type pType
) { progType
= pType
; }
40 const TargetNVC0
*targ
;
42 Program::Type progType
;
45 void emitForm_A(const Instruction
*, uint64_t);
46 void emitForm_B(const Instruction
*, uint64_t);
47 void emitForm_S(const Instruction
*, uint32_t, bool pred
);
49 void emitPredicate(const Instruction
*);
51 void setAddress16(const ValueRef
&);
52 void setImmediate(const Instruction
*, const int s
); // needs op already set
53 void setImmediateS8(const ValueRef
&);
55 void emitCondCode(CondCode cc
, int pos
);
56 void emitInterpMode(const Instruction
*);
57 void emitLoadStoreType(DataType ty
);
58 void emitCachingMode(CacheMode c
);
60 void emitShortSrc2(const ValueRef
&);
62 inline uint8_t getSRegEncoding(const ValueRef
&);
64 void roundMode_A(const Instruction
*);
65 void roundMode_C(const Instruction
*);
66 void roundMode_CS(const Instruction
*);
68 void emitNegAbs12(const Instruction
*);
70 void emitNOP(const Instruction
*);
72 void emitLOAD(const Instruction
*);
73 void emitSTORE(const Instruction
*);
74 void emitMOV(const Instruction
*);
76 void emitINTERP(const Instruction
*);
77 void emitPFETCH(const Instruction
*);
78 void emitVFETCH(const Instruction
*);
79 void emitEXPORT(const Instruction
*);
80 void emitOUT(const Instruction
*);
82 void emitUADD(const Instruction
*);
83 void emitFADD(const Instruction
*);
84 void emitUMUL(const Instruction
*);
85 void emitFMUL(const Instruction
*);
86 void emitIMAD(const Instruction
*);
87 void emitFMAD(const Instruction
*);
89 void emitNOT(Instruction
*);
90 void emitLogicOp(const Instruction
*, uint8_t subOp
);
91 void emitPOPC(const Instruction
*);
92 void emitINSBF(const Instruction
*);
93 void emitShift(const Instruction
*);
95 void emitSFnOp(const Instruction
*, uint8_t subOp
);
97 void emitCVT(Instruction
*);
98 void emitMINMAX(const Instruction
*);
99 void emitPreOp(const Instruction
*);
101 void emitSET(const CmpInstruction
*);
102 void emitSLCT(const CmpInstruction
*);
103 void emitSELP(const Instruction
*);
105 void emitTEX(const TexInstruction
*);
106 void emitTEXCSAA(const TexInstruction
*);
107 void emitTXQ(const TexInstruction
*);
108 void emitPIXLD(const TexInstruction
*);
110 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
112 void emitFlow(const Instruction
*);
114 inline void defId(const ValueDef
&, const int pos
);
115 inline void srcId(const ValueRef
&, const int pos
);
117 inline void srcAddr32(const ValueRef
&, const int pos
); // address / 4
119 inline void srcId(const ValueRef
*, const int pos
);
121 inline bool isLIMM(const ValueRef
&, DataType ty
);
124 // for better visibility
125 #define HEX64(h, l) 0x##h##l##ULL
127 #define SDATA(a) ((a).rep()->reg.data)
128 #define DDATA(a) ((a).rep()->reg.data)
130 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
132 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
135 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
137 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
140 void CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, const int pos
)
142 code
[pos
/ 32] |= (SDATA(src
).offset
>> 2) << (pos
% 32);
145 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
147 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
150 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
152 const ImmediateValue
*imm
= ref
.get()->asImm();
154 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
158 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
161 case ROUND_M
: code
[1] |= 1 << 23; break;
162 case ROUND_P
: code
[1] |= 2 << 23; break;
163 case ROUND_Z
: code
[1] |= 3 << 23; break;
165 assert(insn
->rnd
== ROUND_N
);
171 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
173 if (i
->src
[1].mod
.abs()) code
[0] |= 1 << 6;
174 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 7;
175 if (i
->src
[1].mod
.neg()) code
[0] |= 1 << 8;
176 if (i
->src
[0].mod
.neg()) code
[0] |= 1 << 9;
179 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
184 case CC_LT
: val
= 0x1; break;
185 case CC_LTU
: val
= 0x9; break;
186 case CC_EQ
: val
= 0x2; break;
187 case CC_EQU
: val
= 0xa; break;
188 case CC_LE
: val
= 0x3; break;
189 case CC_LEU
: val
= 0xb; break;
190 case CC_GT
: val
= 0x4; break;
191 case CC_GTU
: val
= 0xc; break;
192 case CC_NE
: val
= 0x5; break;
193 case CC_NEU
: val
= 0xd; break;
194 case CC_GE
: val
= 0x6; break;
195 case CC_GEU
: val
= 0xe; break;
196 case CC_TR
: val
= 0xf; break;
197 case CC_FL
: val
= 0x0; break;
199 case CC_A
: val
= 0x14; break;
200 case CC_NA
: val
= 0x13; break;
201 case CC_S
: val
= 0x15; break;
202 case CC_NS
: val
= 0x12; break;
203 case CC_C
: val
= 0x16; break;
204 case CC_NC
: val
= 0x11; break;
205 case CC_O
: val
= 0x17; break;
206 case CC_NO
: val
= 0x10; break;
210 assert(!"invalid condition code");
213 code
[pos
/ 32] |= val
<< (pos
% 32);
217 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
219 if (i
->predSrc
>= 0) {
220 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
221 srcId(i
->src
[i
->predSrc
], 10);
222 if (i
->cc
== CC_NOT_P
)
223 code
[0] |= 0x2000; // negate
230 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
232 Symbol
*sym
= src
.get()->asSym();
236 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
237 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
241 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
243 const ImmediateValue
*imm
= i
->src
[s
].get()->asImm();
247 u32
= imm
->reg
.data
.u32
;
249 if ((code
[0] & 0xf) == 0x2) {
251 code
[0] |= (u32
& 0x3f) << 26;
254 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
256 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
257 assert(!(code
[1] & 0xc000));
259 code
[0] |= (u32
& 0x3f) << 26;
260 code
[1] |= 0xc000 | (u32
>> 6);
263 assert(!(u32
& 0x00000fff));
264 assert(!(code
[1] & 0xc000));
265 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
266 code
[1] |= 0xc000 | (u32
>> 18);
270 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
272 const ImmediateValue
*imm
= ref
.get()->asImm();
274 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
276 assert(s8
== imm
->reg
.data
.s32
);
278 code
[0] |= (s8
& 0x3f) << 26;
279 code
[0] |= (s8
>> 6) << 8;
283 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
290 defId(i
->def
[0], 14);
293 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
296 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
297 switch (i
->getSrc(s
)->reg
.file
) {
298 case FILE_MEMORY_CONST
:
299 assert(!(code
[1] & 0xc000));
300 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
301 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
302 setAddress16(i
->src
[s
]);
306 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
307 assert(!(code
[1] & 0xc000));
311 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
313 srcId(i
->src
[s
], s
? ((s
== 2) ? 49 : s1
) : 20);
316 // ignore here, can be predicate or flags, but must not be address
323 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
330 defId(i
->def
[0], 14);
332 switch (i
->src
[0].getFile()) {
333 case FILE_MEMORY_CONST
:
334 assert(!(code
[1] & 0xc000));
335 code
[1] |= 0x4000 | (i
->src
[0].get()->reg
.fileIndex
<< 10);
336 setAddress16(i
->src
[0]);
339 assert(!(code
[1] & 0xc000));
343 srcId(i
->src
[0], 26);
346 // ignore here, can be predicate or flags, but must not be address
352 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
357 if (opc
== 0x0d || opc
== 0x0e)
360 defId(i
->def
[0], 14);
361 srcId(i
->src
[0], 20);
363 assert(pred
|| (i
->predSrc
< 0));
367 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
368 if (i
->src
[s
].get()->reg
.file
== FILE_MEMORY_CONST
) {
369 assert(!(code
[0] & (0x300 >> ss2a
)));
370 switch (i
->src
[s
].get()->reg
.fileIndex
) {
371 case 0: code
[0] |= 0x100 >> ss2a
; break;
372 case 1: code
[0] |= 0x200 >> ss2a
; break;
373 case 16: code
[0] |= 0x300 >> ss2a
; break;
375 ERROR("invalid c[] space for short form\n");
379 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
381 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
383 if (i
->src
[s
].getFile() == FILE_IMMEDIATE
) {
385 setImmediateS8(i
->src
[s
]);
387 if (i
->src
[s
].getFile() == FILE_GPR
) {
388 srcId(i
->src
[s
], (s
== 1) ? 26 : 8);
394 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
396 if (src
.getFile() == FILE_MEMORY_CONST
) {
397 switch (src
.get()->reg
.fileIndex
) {
398 case 0: code
[0] |= 0x100; break;
399 case 1: code
[0] |= 0x200; break;
400 case 16: code
[0] |= 0x300; break;
402 assert(!"unsupported file index for short op");
408 assert(src
.getFile() == FILE_GPR
);
413 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
415 code
[0] = 0x000001e4;
416 code
[1] = 0x40000000;
421 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
423 bool neg1
= (i
->src
[0].mod
^ i
->src
[1].mod
).neg();
425 if (i
->encSize
== 8) {
426 if (isLIMM(i
->src
[1], TYPE_F32
)) {
427 emitForm_A(i
, HEX64(20000000, 00000002));
429 emitForm_A(i
, HEX64(30000000, 00000000));
431 if (i
->src
[2].mod
.neg())
444 assert(!i
->saturate
&& !i
->src
[2].mod
.neg());
445 emitForm_S(i
, (i
->src
[2].getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
453 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
455 bool neg
= (i
->src
[0].mod
^ i
->src
[1].mod
).neg();
457 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
459 if (i
->encSize
== 8) {
460 if (isLIMM(i
->src
[1], TYPE_F32
)) {
461 assert(i
->postFactor
== 0); // constant folded, hopefully
462 emitForm_A(i
, HEX64(30000000, 00000002));
464 emitForm_A(i
, HEX64(58000000, 00000000));
466 code
[1] |= ((i
->postFactor
> 0) ?
467 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
470 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
481 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
482 emitForm_S(i
, 0xa8, true);
487 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
489 if (i
->encSize
== 8) {
490 if (i
->src
[1].getFile() == FILE_IMMEDIATE
) {
491 emitForm_A(i
, HEX64(10000000, 00000002));
493 emitForm_A(i
, HEX64(50000000, 00000003));
495 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
497 if (i
->sType
== TYPE_S32
)
499 if (i
->dType
== TYPE_S32
)
502 emitForm_S(i
, i
->src
[1].getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
504 if (i
->sType
== TYPE_S32
)
510 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
512 if (i
->encSize
== 8) {
513 if (isLIMM(i
->src
[1], TYPE_F32
)) {
514 emitForm_A(i
, HEX64(28000000, 00000002));
516 assert(!i
->src
[1].mod
.neg() && !i
->src
[1].mod
.abs() && !i
->saturate
);
518 emitForm_A(i
, HEX64(50000000, 00000000));
526 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
531 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
532 !i
->src
[0].mod
.abs() &&
533 !i
->src
[1].mod
.neg() && !i
->src
[1].mod
.abs());
535 emitForm_S(i
, 0x49, true);
537 if (i
->src
[0].mod
.neg())
543 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
547 assert(!i
->src
[0].mod
.abs() && !i
->src
[1].mod
.abs());
548 assert(!i
->src
[0].mod
.neg() || !i
->src
[1].mod
.neg());
550 if (i
->src
[0].mod
.neg())
552 if (i
->src
[1].mod
.neg())
554 if (i
->op
== OP_SUB
) {
556 assert(addOp
!= 0x300); // would be add-plus-one
559 if (i
->encSize
== 8) {
560 if (isLIMM(i
->src
[1], TYPE_U32
)) {
561 emitForm_A(i
, HEX64(08000000, 00000002));
562 if (i
->def
[1].exists())
563 code
[1] |= 1 << 26; // write carry
565 emitForm_A(i
, HEX64(48000000, 00000003));
566 if (i
->def
[1].exists())
567 code
[1] |= 1 << 16; // write carry
573 if (i
->flagsSrc
>= 0) // add carry
576 assert(!(addOp
& 0x100));
577 emitForm_S(i
, (addOp
>> 3) |
578 ((i
->src
[1].getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
584 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
586 assert(i
->encSize
== 8);
587 emitForm_A(i
, HEX64(20000000, 00000003));
589 if (isSignedType(i
->dType
))
591 if (isSignedType(i
->sType
))
594 code
[1] |= i
->saturate
<< 24;
596 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
597 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
599 if (i
->src
[2].mod
.neg()) code
[0] |= 0x10;
600 if (i
->src
[1].mod
.neg() ^
601 i
->src
[0].mod
.neg()) code
[0] |= 0x20;
603 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
608 CodeEmitterNVC0::emitNOT(Instruction
*i
)
610 assert(i
->encSize
== 8);
611 i
->src
[1].set(i
->src
[0]);
612 emitForm_A(i
, HEX64(68000000, 000001c3
));
616 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
618 if (i
->encSize
== 8) {
619 if (isLIMM(i
->src
[1], TYPE_U32
)) {
620 emitForm_A(i
, HEX64(38000000, 00000002));
622 if (i
->src
[2].exists())
625 emitForm_A(i
, HEX64(68000000, 00000003));
627 if (i
->src
[2].exists())
630 code
[0] |= subOp
<< 6;
632 if (i
->src
[2].exists()) // carry
635 if (i
->src
[0].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
636 if (i
->src
[1].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
638 emitForm_S(i
, (subOp
<< 5) |
639 ((i
->src
[1].getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
644 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
646 emitForm_A(i
, HEX64(54000000, 00000004));
648 if (i
->src
[0].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
649 if (i
->src
[1].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
653 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
655 emitForm_A(i
, HEX64(28000000, 30000000));
659 CodeEmitterNVC0::emitShift(const Instruction
*i
)
661 if (i
->op
== OP_SHR
) {
662 emitForm_A(i
, HEX64(58000000, 00000003)
663 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
665 emitForm_A(i
, HEX64(60000000, 00000003));
668 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
673 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
675 if (i
->encSize
== 8) {
676 emitForm_B(i
, HEX64(60000000, 00000000));
678 if (i
->op
== OP_PREEX2
)
681 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 6;
682 if (i
->src
[0].mod
.neg()) code
[0] |= 1 << 8;
684 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
689 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
691 if (i
->encSize
== 8) {
692 code
[0] = 0x00000000 | (subOp
<< 26);
693 code
[1] = 0xc8000000;
697 defId(i
->def
[0], 14);
698 srcId(i
->src
[0], 20);
700 assert(i
->src
[0].getFile() == FILE_GPR
);
702 if (i
->saturate
) code
[0] |= 1 << 5;
704 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 7;
705 if (i
->src
[0].mod
.neg()) code
[0] |= 1 << 9;
707 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
709 assert(!i
->src
[0].mod
.neg());
710 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 30;
715 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
719 assert(i
->encSize
== 8);
721 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
726 if (!isFloatType(i
->dType
))
727 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
734 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
737 case ROUND_M
: code
[1] |= 1 << 17; break;
738 case ROUND_P
: code
[1] |= 2 << 17; break;
739 case ROUND_Z
: code
[1] |= 3 << 17; break;
740 case ROUND_NI
: code
[0] |= 1 << 7; break;
741 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
742 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
743 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
746 assert(!"invalid round mode");
752 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
756 case ROUND_MI
: code
[0] |= 1 << 16; break;
758 case ROUND_PI
: code
[0] |= 2 << 16; break;
760 case ROUND_ZI
: code
[0] |= 3 << 16; break;
767 CodeEmitterNVC0::emitCVT(Instruction
*i
)
769 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
772 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
773 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
774 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
779 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
780 const bool abs
= (i
->op
== OP_ABS
) || i
->src
[0].mod
.abs();
781 const bool neg
= (i
->op
== OP_NEG
) || i
->src
[0].mod
.neg();
783 if (i
->encSize
== 8) {
784 emitForm_B(i
, HEX64(10000000, 00000004));
788 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
789 code
[0] |= util_logbase2(typeSizeof(i
->dType
)) << 20;
790 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
796 if (neg
&& i
->op
!= OP_ABS
)
802 if (isSignedIntType(i
->dType
))
804 if (isSignedIntType(i
->sType
))
807 if (isFloatType(i
->dType
)) {
808 if (!isFloatType(i
->sType
))
809 code
[1] |= 0x08000000;
811 if (isFloatType(i
->sType
))
812 code
[1] |= 0x04000000;
814 code
[1] |= 0x0c000000;
817 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
820 if (isFloatType(i
->dType
)) {
821 if (isFloatType(i
->sType
))
824 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
826 assert(isFloatType(i
->sType
));
828 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
831 if (neg
) code
[0] |= 1 << 16;
832 if (sat
) code
[0] |= 1 << 18;
833 if (abs
) code
[0] |= 1 << 19;
840 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
845 if (i
->sType
== TYPE_F64
)
848 if (!isFloatType(i
->sType
))
851 if (isFloatType(i
->dType
) || isSignedIntType(i
->sType
))
855 case OP_SET_AND
: hi
= 0x10000000; break;
856 case OP_SET_OR
: hi
= 0x10200000; break;
857 case OP_SET_XOR
: hi
= 0x10400000; break;
862 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
864 if (i
->def
[0].getFile() == FILE_PREDICATE
) {
865 if (i
->sType
== TYPE_F32
)
866 code
[1] += 0x10000000;
868 code
[1] += 0x08000000;
871 defId(i
->def
[0], 17);
873 defId(i
->def
[1], 14);
881 emitCondCode(i
->setCond
, 32 + 23);
886 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
892 op
= HEX64(30000000, 00000023);
895 op
= HEX64(30000000, 00000003);
898 op
= HEX64(38000000, 00000000);
901 assert(!"invalid type for SLCT");
907 CondCode cc
= i
->setCond
;
909 if (i
->src
[2].mod
.neg())
910 cc
= reverseCondCode(cc
);
912 emitCondCode(cc
, 32 + 23);
918 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
920 emitForm_A(i
, HEX64(20000000, 00000004));
922 if (i
->cc
== CC_NOT_P
|| i
->src
[2].mod
& Modifier(NV50_IR_MOD_NOT
))
926 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
928 code
[0] = 0x00000086;
929 code
[1] = 0xd0000000;
932 code
[1] |= i
->tex
.s
<< 8;
937 defId(i
->def
[0], 14);
938 srcId(i
->src
[0], 20);
942 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
944 code
[0] = 0x00000006;
947 code
[0] |= 0x80; // normal/t/p mode = t, XXX: what is this ?
953 case OP_TEX
: code
[1] = 0x80000000; break;
954 case OP_TXB
: code
[1] = 0x84000000; break;
955 case OP_TXL
: code
[1] = 0x86000000; break;
956 case OP_TXF
: code
[1] = 0x90000000; break;
957 case OP_TXG
: code
[1] = 0xa0000000; break;
958 case OP_TXD
: code
[1] = 0xe0000000; break;
960 assert(!"invalid texture op");
963 if (i
->op
== OP_TXF
) {
964 if (!i
->tex
.levelZero
)
965 code
[1] |= 0x02000000;
967 if (i
->tex
.levelZero
) {
968 code
[1] |= 0x02000000;
974 defId(i
->def
[0], 14);
975 srcId(i
->src
[0], 20);
979 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
981 code
[1] |= i
->tex
.mask
<< 14;
984 code
[1] |= i
->tex
.s
<< 8;
985 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
986 code
[1] |= 1 << 18; // in 1st source (with array index)
989 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
990 if (i
->tex
.target
.isCube())
992 if (i
->tex
.target
.isArray())
994 if (i
->tex
.target
.isShadow())
997 int src1
= i
->tex
.target
.getArgCount();
998 if (i
->op
== OP_TXD
&& i
->tex
.useOffsets
)
1001 if (i
->src
[src1
].getFile() == FILE_IMMEDIATE
) { // lzero
1002 if (i
->op
== OP_TXL
)
1003 code
[1] &= ~(1 << 26);
1005 if (i
->op
== OP_TXF
)
1006 code
[1] &= ~(1 << 25);
1008 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1009 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1012 if (i
->tex
.useOffsets
) // in vecSrc0.w
1015 srcId(i
->src
[src1
], 26);
1019 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1021 code
[0] = 0x00000086;
1022 code
[1] = 0xc0000000;
1024 switch (i
->tex
.query
) {
1025 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1026 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1027 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1028 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1029 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1030 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1032 assert(!"invalid texture query");
1036 code
[1] |= i
->tex
.mask
<< 14;
1038 code
[1] |= i
->tex
.r
;
1039 code
[1] |= i
->tex
.s
<< 8;
1040 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1043 defId(i
->def
[0], 14);
1044 srcId(i
->src
[0], 20);
1045 srcId(i
->src
[1], 26);
1051 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1053 code
[0] = 0x00000000 | (laneMask
<< 6);
1054 code
[1] = 0x48000000 | qOp
;
1056 defId(i
->def
[0], 14);
1057 srcId(i
->src
[0], 20);
1058 srcId(i
->srcExists(1) ? i
->src
[1] : i
->src
[0], 26);
1060 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1061 code
[0] |= 1 << 9; // dall
1067 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1069 const FlowInstruction
*f
= i
->asFlow();
1071 unsigned mask
; // bit 0: predicate, bit 1: target
1073 code
[0] = 0x00000007;
1077 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1078 if (i
->src
[0].getFile() == FILE_MEMORY_CONST
||
1079 i
->src
[1].getFile() == FILE_MEMORY_CONST
)
1084 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1085 if (i
->src
[0].getFile() == FILE_MEMORY_CONST
)
1090 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1091 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1092 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1093 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1094 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1096 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1097 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1098 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1099 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1101 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1102 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1103 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1105 assert(!"invalid flow operation");
1111 if (i
->flagsSrc
< 0)
1123 if (f
->op
== OP_CALL
) {
1125 assert(f
->absolute
);
1126 uint32_t pcAbs
= targ
->getBuiltinOffset(f
->target
.builtin
);
1127 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1128 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1130 assert(!f
->absolute
);
1131 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1132 code
[0] |= (pcRel
& 0x3f) << 26;
1133 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1137 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1138 // currently we don't want absolute branches
1139 assert(!f
->absolute
);
1140 code
[0] |= (pcRel
& 0x3f) << 26;
1141 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1146 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1148 uint32_t prim
= i
->src
[0].get()->reg
.data
.u32
;
1150 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1151 code
[1] = 0x00000000 | (prim
>> 6);
1155 defId(i
->def
[0], 14);
1156 srcId(i
->src
[1], 20);
1160 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1162 code
[0] = 0x00000006;
1163 code
[1] = 0x06000000 | i
->src
[0].get()->reg
.data
.offset
;
1167 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1168 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1172 code
[0] |= (i
->defCount(0xf) - 1) << 5;
1174 defId(i
->def
[0], 14);
1175 srcId(i
->src
[0].getIndirect(0), 20);
1176 srcId(i
->src
[0].getIndirect(1), 26); // vertex address
1180 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1182 unsigned int size
= typeSizeof(i
->dType
);
1184 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1185 code
[1] = 0x0a000000 | i
->src
[0].get()->reg
.data
.offset
;
1187 assert(size
!= 12 && !(code
[1] & (size
- 1)));
1194 assert(i
->src
[1].getFile() == FILE_GPR
);
1196 srcId(i
->src
[0].getIndirect(0), 20);
1197 srcId(i
->src
[0].getIndirect(1), 32 + 17); // vertex base address
1198 srcId(i
->src
[1], 26);
1202 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1204 code
[0] = 0x00000006;
1205 code
[1] = 0x1c000000;
1209 defId(i
->def
[0], 14); // new secret address
1210 srcId(i
->src
[0], 20); // old secret address, should be 0 initially
1212 assert(i
->src
[0].getFile() == FILE_GPR
);
1214 if (i
->op
== OP_EMIT
)
1216 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1220 if (i
->src
[1].getFile() == FILE_IMMEDIATE
) {
1222 code
[0] |= SDATA(i
->src
[1]).u32
<< 26;
1224 srcId(i
->src
[1], 26);
1229 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1231 if (i
->encSize
== 8) {
1232 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1234 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1236 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1241 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1243 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1245 if (i
->encSize
== 8) {
1246 code
[0] = 0x00000000;
1247 code
[1] = 0xc0000000 | (base
& 0xffff);
1252 if (i
->op
== OP_PINTERP
)
1253 srcId(i
->src
[1], 26);
1255 code
[0] |= 0x3f << 26;
1257 srcId(i
->src
[0].getIndirect(0), 20);
1259 assert(i
->op
== OP_PINTERP
);
1260 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1261 srcId(i
->src
[1], 20);
1266 defId(i
->def
[0], 14);
1268 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1269 srcId(i
->src
[i
->op
== OP_PINTERP
? 2 : 1], 17);
1271 code
[1] |= 0x3f << 17;
1275 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1308 assert(!"invalid type");
1315 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1336 assert(!"invalid caching mode");
1343 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1347 switch (i
->src
[0].getFile()) {
1348 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1349 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1350 case FILE_MEMORY_SHARED
: opc
= 0xc9000000; break;
1352 assert(!"invalid memory file");
1356 code
[0] = 0x00000005;
1359 setAddress16(i
->src
[0]);
1360 srcId(i
->src
[1], 14);
1361 srcId(i
->src
[0].getIndirect(0), 20);
1365 emitLoadStoreType(i
->dType
);
1366 emitCachingMode(i
->cache
);
1370 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1374 code
[0] = 0x00000005;
1376 switch (i
->src
[0].getFile()) {
1377 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1378 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1379 case FILE_MEMORY_SHARED
: opc
= 0xc1000000; break;
1380 case FILE_MEMORY_CONST
:
1381 if (!i
->src
[0].isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1382 emitMOV(i
); // not sure if this is any better
1385 opc
= 0x14000000 | (i
->src
[0].get()->reg
.fileIndex
<< 10);
1386 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1389 assert(!"invalid memory file");
1395 defId(i
->def
[0], 14);
1397 setAddress16(i
->src
[0]);
1398 srcId(i
->src
[0].getIndirect(0), 20);
1402 emitLoadStoreType(i
->dType
);
1403 emitCachingMode(i
->cache
);
1407 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1409 switch (SDATA(ref
).sv
.sv
) {
1410 case SV_LANEID
: return 0x00;
1411 case SV_PHYSID
: return 0x03;
1412 case SV_VERTEX_COUNT
: return 0x10;
1413 case SV_INVOCATION_ID
: return 0x11;
1414 case SV_YDIR
: return 0x12;
1415 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1416 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1417 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1418 case SV_GRIDID
: return 0x2c;
1419 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1420 case SV_LBASE
: return 0x34;
1421 case SV_SBASE
: return 0x30;
1422 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1424 assert(!"no sreg for system value");
1430 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1432 if (i
->src
[0].getFile() == FILE_SYSTEM_VALUE
) {
1433 uint8_t sr
= getSRegEncoding(i
->src
[0]);
1435 if (i
->encSize
== 8) {
1436 code
[0] = 0x00000004 | (sr
<< 26);
1437 code
[1] = 0x2c000000;
1439 code
[0] = 0x40000008 | (sr
<< 20);
1441 defId(i
->def
[0], 14);
1445 if (i
->encSize
== 8) {
1448 if (i
->src
[0].getFile() == FILE_IMMEDIATE
)
1449 opc
= HEX64(18000000, 000001e2
);
1451 if (i
->src
[0].getFile() == FILE_PREDICATE
)
1452 opc
= HEX64(080e0000
, 1c000004
);
1454 opc
= HEX64(28000000, 00000004);
1456 opc
|= i
->lanes
<< 5;
1462 if (i
->src
[0].getFile() == FILE_IMMEDIATE
) {
1463 imm
= SDATA(i
->src
[0]).u32
;
1464 if (imm
& 0xfff00000) {
1465 assert(!(imm
& 0x000fffff));
1466 code
[0] = 0x00000318 | imm
;
1468 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1469 code
[0] = 0x00000118 | (imm
<< 20);
1473 emitShortSrc2(i
->src
[0]);
1475 defId(i
->def
[0], 14);
1482 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
1484 if (!insn
->encSize
) {
1485 ERROR("skipping unencodable instruction: "); insn
->print();
1488 if (codeSize
+ insn
->encSize
> codeSizeLimit
) {
1489 ERROR("code emitter output buffer too small\n");
1493 // assert that instructions with multiple defs don't corrupt registers
1494 for (int d
= 0; insn
->defExists(d
); ++d
)
1495 assert(insn
->asTex() || insn
->def
[d
].rep()->reg
.data
.id
>= 0);
1529 if (isFloatType(insn
->dType
))
1535 if (isFloatType(insn
->dType
))
1542 if (isFloatType(insn
->dType
))
1551 emitLogicOp(insn
, 0);
1554 emitLogicOp(insn
, 1);
1557 emitLogicOp(insn
, 2);
1567 emitSET(insn
->asCmp());
1573 emitSLCT(insn
->asCmp());
1615 emitTEX(insn
->asTex());
1618 emitTXQ(insn
->asTex());
1637 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
1640 emitQUADOP(insn
, insn
->src
[0].mod
.neg() ? 0x66 : 0x99, 0x4);
1643 emitQUADOP(insn
, insn
->src
[0].mod
.neg() ? 0x5a : 0xa5, 0x5);
1655 ERROR("operation should have been eliminated");
1661 ERROR("operation should have been lowered\n");
1664 ERROR("unknow op\n");
1670 assert(insn
->encSize
== 8);
1673 code
+= insn
->encSize
/ 4;
1674 codeSize
+= insn
->encSize
;
1679 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
1681 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
1683 if (info
.minEncSize
== 8 || 1)
1686 if (i
->ftz
|| i
->saturate
|| i
->join
)
1688 if (i
->rnd
!= ROUND_N
)
1690 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
1693 if (i
->op
== OP_PINTERP
) {
1694 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
1697 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
1701 for (int s
= 0; i
->srcExists(s
); ++s
) {
1702 if (i
->src
[s
].isIndirect(0))
1705 if (i
->src
[s
].getFile() == FILE_MEMORY_CONST
) {
1706 if (SDATA(i
->src
[s
]).offset
>= 0x100)
1708 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
1709 i
->getSrc(s
)->reg
.fileIndex
!= 16)
1712 if (i
->src
[s
].getFile() == FILE_IMMEDIATE
) {
1713 if (i
->dType
== TYPE_F32
) {
1714 if (SDATA(i
->src
[s
]).u32
>= 0x100)
1717 if (SDATA(i
->src
[s
]).u32
> 0xff)
1722 if (i
->op
== OP_CVT
)
1724 if (i
->src
[s
].mod
!= Modifier(0)) {
1725 if (i
->src
[s
].mod
== Modifier(NV50_IR_MOD_ABS
))
1726 if (i
->op
!= OP_RSQ
)
1728 if (i
->src
[s
].mod
== Modifier(NV50_IR_MOD_NEG
))
1729 if (i
->op
!= OP_ADD
|| s
!= 0)
1737 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
) : targ(target
)
1740 codeSize
= codeSizeLimit
= 0;
1745 TargetNVC0::getCodeEmitter(Program::Type type
)
1747 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
1748 emit
->setProgramType(type
);
1752 } // namespace nv50_ir