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 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.
23 #include "codegen/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;
36 virtual void prepareEmission(Function
*);
38 inline void setProgramType(Program::Type pType
) { progType
= pType
; }
41 const TargetNVC0
*targNVC0
;
43 Program::Type progType
;
45 const bool writeIssueDelays
;
48 void emitForm_A(const Instruction
*, uint64_t);
49 void emitForm_B(const Instruction
*, uint64_t);
50 void emitForm_S(const Instruction
*, uint32_t, bool pred
);
52 void emitPredicate(const Instruction
*);
54 void setAddress16(const ValueRef
&);
55 void setAddress24(const ValueRef
&);
56 void setAddressByFile(const ValueRef
&);
57 void setImmediate(const Instruction
*, const int s
); // needs op already set
58 void setImmediateS8(const ValueRef
&);
59 void setSUConst16(const Instruction
*, const int s
);
60 void setSUPred(const Instruction
*, const int s
);
62 void emitCondCode(CondCode cc
, int pos
);
63 void emitInterpMode(const Instruction
*);
64 void emitLoadStoreType(DataType ty
);
65 void emitSUGType(DataType
);
66 void emitSUAddr(const TexInstruction
*);
67 void emitSUDim(const TexInstruction
*);
68 void emitCachingMode(CacheMode c
);
70 void emitShortSrc2(const ValueRef
&);
72 inline uint8_t getSRegEncoding(const ValueRef
&);
74 void roundMode_A(const Instruction
*);
75 void roundMode_C(const Instruction
*);
76 void roundMode_CS(const Instruction
*);
78 void emitNegAbs12(const Instruction
*);
80 void emitNOP(const Instruction
*);
82 void emitLOAD(const Instruction
*);
83 void emitSTORE(const Instruction
*);
84 void emitMOV(const Instruction
*);
85 void emitATOM(const Instruction
*);
86 void emitMEMBAR(const Instruction
*);
87 void emitCCTL(const Instruction
*);
89 void emitINTERP(const Instruction
*);
90 void emitAFETCH(const Instruction
*);
91 void emitPFETCH(const Instruction
*);
92 void emitVFETCH(const Instruction
*);
93 void emitEXPORT(const Instruction
*);
94 void emitOUT(const Instruction
*);
96 void emitUADD(const Instruction
*);
97 void emitFADD(const Instruction
*);
98 void emitDADD(const Instruction
*);
99 void emitUMUL(const Instruction
*);
100 void emitFMUL(const Instruction
*);
101 void emitDMUL(const Instruction
*);
102 void emitIMAD(const Instruction
*);
103 void emitISAD(const Instruction
*);
104 void emitFMAD(const Instruction
*);
105 void emitDMAD(const Instruction
*);
106 void emitMADSP(const Instruction
*);
108 void emitNOT(Instruction
*);
109 void emitLogicOp(const Instruction
*, uint8_t subOp
);
110 void emitPOPC(const Instruction
*);
111 void emitINSBF(const Instruction
*);
112 void emitEXTBF(const Instruction
*);
113 void emitBFIND(const Instruction
*);
114 void emitPERMT(const Instruction
*);
115 void emitShift(const Instruction
*);
117 void emitSFnOp(const Instruction
*, uint8_t subOp
);
119 void emitCVT(Instruction
*);
120 void emitMINMAX(const Instruction
*);
121 void emitPreOp(const Instruction
*);
123 void emitSET(const CmpInstruction
*);
124 void emitSLCT(const CmpInstruction
*);
125 void emitSELP(const Instruction
*);
127 void emitTEXBAR(const Instruction
*);
128 void emitTEX(const TexInstruction
*);
129 void emitTEXCSAA(const TexInstruction
*);
130 void emitTXQ(const TexInstruction
*);
132 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
134 void emitFlow(const Instruction
*);
135 void emitBAR(const Instruction
*);
137 void emitSUCLAMPMode(uint16_t);
138 void emitSUCalc(Instruction
*);
139 void emitSULDGB(const TexInstruction
*);
140 void emitSUSTGx(const TexInstruction
*);
142 void emitSULDB(const TexInstruction
*);
143 void emitSUSTx(const TexInstruction
*);
144 void emitSULEA(const TexInstruction
*);
146 void emitVSHL(const Instruction
*);
147 void emitVectorSubOp(const Instruction
*);
149 void emitPIXLD(const Instruction
*);
151 void emitVOTE(const Instruction
*);
153 inline void defId(const ValueDef
&, const int pos
);
154 inline void defId(const Instruction
*, int d
, const int pos
);
155 inline void srcId(const ValueRef
&, const int pos
);
156 inline void srcId(const ValueRef
*, const int pos
);
157 inline void srcId(const Instruction
*, int s
, const int pos
);
158 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
160 inline bool isLIMM(const ValueRef
&, DataType ty
);
163 // for better visibility
164 #define HEX64(h, l) 0x##h##l##ULL
166 #define SDATA(a) ((a).rep()->reg.data)
167 #define DDATA(a) ((a).rep()->reg.data)
169 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
171 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
174 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
176 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
179 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
181 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
182 code
[pos
/ 32] |= r
<< (pos
% 32);
186 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
188 const uint32_t offset
= SDATA(src
).offset
>> shr
;
190 code
[pos
/ 32] |= offset
<< (pos
% 32);
191 if (pos
&& (pos
< 32))
192 code
[1] |= offset
>> (32 - pos
);
195 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
197 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
200 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
202 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
203 code
[pos
/ 32] |= r
<< (pos
% 32);
206 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
208 const ImmediateValue
*imm
= ref
.get()->asImm();
210 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
214 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
217 case ROUND_M
: code
[1] |= 1 << 23; break;
218 case ROUND_P
: code
[1] |= 2 << 23; break;
219 case ROUND_Z
: code
[1] |= 3 << 23; break;
221 assert(insn
->rnd
== ROUND_N
);
227 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
229 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
230 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
231 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
232 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
235 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
240 case CC_LT
: val
= 0x1; break;
241 case CC_LTU
: val
= 0x9; break;
242 case CC_EQ
: val
= 0x2; break;
243 case CC_EQU
: val
= 0xa; break;
244 case CC_LE
: val
= 0x3; break;
245 case CC_LEU
: val
= 0xb; break;
246 case CC_GT
: val
= 0x4; break;
247 case CC_GTU
: val
= 0xc; break;
248 case CC_NE
: val
= 0x5; break;
249 case CC_NEU
: val
= 0xd; break;
250 case CC_GE
: val
= 0x6; break;
251 case CC_GEU
: val
= 0xe; break;
252 case CC_TR
: val
= 0xf; break;
253 case CC_FL
: val
= 0x0; break;
255 case CC_A
: val
= 0x14; break;
256 case CC_NA
: val
= 0x13; break;
257 case CC_S
: val
= 0x15; break;
258 case CC_NS
: val
= 0x12; break;
259 case CC_C
: val
= 0x16; break;
260 case CC_NC
: val
= 0x11; break;
261 case CC_O
: val
= 0x17; break;
262 case CC_NO
: val
= 0x10; break;
266 assert(!"invalid condition code");
269 code
[pos
/ 32] |= val
<< (pos
% 32);
273 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
275 if (i
->predSrc
>= 0) {
276 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
277 srcId(i
->src(i
->predSrc
), 10);
278 if (i
->cc
== CC_NOT_P
)
279 code
[0] |= 0x2000; // negate
286 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
288 switch (src
.getFile()) {
289 case FILE_MEMORY_GLOBAL
:
290 srcAddr32(src
, 26, 0);
292 case FILE_MEMORY_LOCAL
:
293 case FILE_MEMORY_SHARED
:
297 assert(src
.getFile() == FILE_MEMORY_CONST
);
304 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
306 Symbol
*sym
= src
.get()->asSym();
310 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
311 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
315 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
317 Symbol
*sym
= src
.get()->asSym();
321 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
322 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
326 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
328 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
332 u32
= imm
->reg
.data
.u32
;
334 if ((code
[0] & 0xf) == 0x1) {
336 uint64_t u64
= imm
->reg
.data
.u64
;
337 assert(!(u64
& 0x00000fffffffffffULL
));
338 assert(!(code
[1] & 0xc000));
339 code
[0] |= ((u64
>> 44) & 0x3f) << 26;
340 code
[1] |= 0xc000 | (u64
>> 50);
342 if ((code
[0] & 0xf) == 0x2) {
344 code
[0] |= (u32
& 0x3f) << 26;
347 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
349 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
350 assert(!(code
[1] & 0xc000));
352 code
[0] |= (u32
& 0x3f) << 26;
353 code
[1] |= 0xc000 | (u32
>> 6);
356 assert(!(u32
& 0x00000fff));
357 assert(!(code
[1] & 0xc000));
358 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
359 code
[1] |= 0xc000 | (u32
>> 18);
363 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
365 const ImmediateValue
*imm
= ref
.get()->asImm();
367 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
369 assert(s8
== imm
->reg
.data
.s32
);
371 code
[0] |= (s8
& 0x3f) << 26;
372 code
[0] |= (s8
>> 6) << 8;
376 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
383 defId(i
->def(0), 14);
386 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
389 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
390 switch (i
->getSrc(s
)->reg
.file
) {
391 case FILE_MEMORY_CONST
:
392 assert(!(code
[1] & 0xc000));
393 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
394 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
395 setAddress16(i
->src(s
));
399 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
400 assert(!(code
[1] & 0xc000));
404 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
406 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
409 if (i
->op
== OP_SELP
) {
410 // OP_SELP is used to implement shared+atomics on Fermi.
411 assert(s
== 2 && i
->src(s
).getFile() == FILE_PREDICATE
);
412 srcId(i
->src(s
), 49);
414 // ignore here, can be predicate or flags, but must not be address
421 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
428 defId(i
->def(0), 14);
430 switch (i
->src(0).getFile()) {
431 case FILE_MEMORY_CONST
:
432 assert(!(code
[1] & 0xc000));
433 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
434 setAddress16(i
->src(0));
437 assert(!(code
[1] & 0xc000));
441 srcId(i
->src(0), 26);
444 // ignore here, can be predicate or flags, but must not be address
450 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
455 if (opc
== 0x0d || opc
== 0x0e)
458 defId(i
->def(0), 14);
459 srcId(i
->src(0), 20);
461 assert(pred
|| (i
->predSrc
< 0));
465 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
466 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
467 assert(!(code
[0] & (0x300 >> ss2a
)));
468 switch (i
->src(s
).get()->reg
.fileIndex
) {
469 case 0: code
[0] |= 0x100 >> ss2a
; break;
470 case 1: code
[0] |= 0x200 >> ss2a
; break;
471 case 16: code
[0] |= 0x300 >> ss2a
; break;
473 ERROR("invalid c[] space for short form\n");
477 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
479 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
481 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
483 setImmediateS8(i
->src(s
));
485 if (i
->src(s
).getFile() == FILE_GPR
) {
486 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
492 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
494 if (src
.getFile() == FILE_MEMORY_CONST
) {
495 switch (src
.get()->reg
.fileIndex
) {
496 case 0: code
[0] |= 0x100; break;
497 case 1: code
[0] |= 0x200; break;
498 case 16: code
[0] |= 0x300; break;
500 assert(!"unsupported file index for short op");
503 srcAddr32(src
, 20, 2);
506 assert(src
.getFile() == FILE_GPR
);
511 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
513 code
[0] = 0x000001e4;
514 code
[1] = 0x40000000;
519 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
521 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
523 if (i
->encSize
== 8) {
524 if (isLIMM(i
->src(1), TYPE_F32
)) {
525 emitForm_A(i
, HEX64(20000000, 00000002));
527 emitForm_A(i
, HEX64(30000000, 00000000));
529 if (i
->src(2).mod
.neg())
542 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
543 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
551 CodeEmitterNVC0::emitDMAD(const Instruction
*i
)
553 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
555 emitForm_A(i
, HEX64(20000000, 00000001));
557 if (i
->src(2).mod
.neg())
565 assert(!i
->saturate
);
570 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
572 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
574 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
576 if (i
->encSize
== 8) {
577 if (isLIMM(i
->src(1), TYPE_F32
)) {
578 assert(i
->postFactor
== 0); // constant folded, hopefully
579 emitForm_A(i
, HEX64(30000000, 00000002));
581 emitForm_A(i
, HEX64(58000000, 00000000));
583 code
[1] |= ((i
->postFactor
> 0) ?
584 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
587 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
598 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
599 emitForm_S(i
, 0xa8, true);
604 CodeEmitterNVC0::emitDMUL(const Instruction
*i
)
606 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
608 emitForm_A(i
, HEX64(50000000, 00000001));
614 assert(!i
->saturate
);
617 assert(!i
->postFactor
);
621 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
623 if (i
->encSize
== 8) {
624 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
625 emitForm_A(i
, HEX64(10000000, 00000002));
627 emitForm_A(i
, HEX64(50000000, 00000003));
629 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
631 if (i
->sType
== TYPE_S32
)
633 if (i
->dType
== TYPE_S32
)
636 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
638 if (i
->sType
== TYPE_S32
)
644 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
646 if (i
->encSize
== 8) {
647 if (isLIMM(i
->src(1), TYPE_F32
)) {
648 assert(!i
->saturate
);
649 emitForm_A(i
, HEX64(28000000, 00000002));
651 code
[0] |= i
->src(0).mod
.abs() << 7;
652 code
[0] |= i
->src(0).mod
.neg() << 9;
654 if (i
->src(1).mod
.abs())
655 code
[1] &= 0xfdffffff;
656 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
657 code
[1] ^= 0x02000000;
659 emitForm_A(i
, HEX64(50000000, 00000000));
666 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
671 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
672 !i
->src(0).mod
.abs() &&
673 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
675 emitForm_S(i
, 0x49, true);
677 if (i
->src(0).mod
.neg())
683 CodeEmitterNVC0::emitDADD(const Instruction
*i
)
685 assert(i
->encSize
== 8);
686 emitForm_A(i
, HEX64(48000000, 00000001));
688 assert(!i
->saturate
);
696 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
700 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
702 if (i
->src(0).mod
.neg())
704 if (i
->src(1).mod
.neg())
709 assert(addOp
!= 0x300); // would be add-plus-one
711 if (i
->encSize
== 8) {
712 if (isLIMM(i
->src(1), TYPE_U32
)) {
713 emitForm_A(i
, HEX64(08000000, 00000002));
715 code
[1] |= 1 << 26; // write carry
717 emitForm_A(i
, HEX64(48000000, 00000003));
719 code
[1] |= 1 << 16; // write carry
725 if (i
->flagsSrc
>= 0) // add carry
728 assert(!(addOp
& 0x100));
729 emitForm_S(i
, (addOp
>> 3) |
730 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
736 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
739 (i
->src(2).mod
.neg() << 1) | (i
->src(0).mod
.neg() ^ i
->src(1).mod
.neg());
741 assert(i
->encSize
== 8);
742 emitForm_A(i
, HEX64(20000000, 00000003));
745 code
[0] |= addOp
<< 8;
747 if (isSignedType(i
->dType
))
749 if (isSignedType(i
->sType
))
752 code
[1] |= i
->saturate
<< 24;
754 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
755 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
757 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
762 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
764 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
766 emitForm_A(i
, HEX64(00000000, 00000003));
768 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
769 code
[1] |= 0x01800000;
771 code
[0] |= (i
->subOp
& 0x00f) << 7;
772 code
[0] |= (i
->subOp
& 0x0f0) << 1;
773 code
[0] |= (i
->subOp
& 0x100) >> 3;
774 code
[0] |= (i
->subOp
& 0x200) >> 2;
775 code
[1] |= (i
->subOp
& 0xc00) << 13;
778 if (i
->flagsDef
>= 0)
783 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
785 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
786 assert(i
->encSize
== 8);
788 emitForm_A(i
, HEX64(38000000, 00000003));
790 if (i
->dType
== TYPE_S32
)
795 CodeEmitterNVC0::emitNOT(Instruction
*i
)
797 assert(i
->encSize
== 8);
798 i
->setSrc(1, i
->src(0));
799 emitForm_A(i
, HEX64(68000000, 000001c3
));
803 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
805 if (i
->def(0).getFile() == FILE_PREDICATE
) {
806 code
[0] = 0x00000004 | (subOp
<< 30);
807 code
[1] = 0x0c000000;
811 defId(i
->def(0), 17);
812 srcId(i
->src(0), 20);
813 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
814 srcId(i
->src(1), 26);
815 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
817 if (i
->defExists(1)) {
818 defId(i
->def(1), 14);
823 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
824 code
[1] |= subOp
<< 21;
825 srcId(i
->src(2), 49);
826 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[1] |= 1 << 20;
828 code
[1] |= 0x000e0000;
831 if (i
->encSize
== 8) {
832 if (isLIMM(i
->src(1), TYPE_U32
)) {
833 emitForm_A(i
, HEX64(38000000, 00000002));
835 if (i
->flagsDef
>= 0)
838 emitForm_A(i
, HEX64(68000000, 00000003));
840 if (i
->flagsDef
>= 0)
843 code
[0] |= subOp
<< 6;
845 if (i
->flagsSrc
>= 0) // carry
848 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
849 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
851 emitForm_S(i
, (subOp
<< 5) |
852 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
857 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
859 emitForm_A(i
, HEX64(54000000, 00000004));
861 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
862 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
866 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
868 emitForm_A(i
, HEX64(28000000, 00000003));
872 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
874 emitForm_A(i
, HEX64(70000000, 00000003));
876 if (i
->dType
== TYPE_S32
)
878 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
883 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
885 emitForm_B(i
, HEX64(78000000, 00000003));
887 if (i
->dType
== TYPE_S32
)
889 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
891 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
896 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
898 emitForm_A(i
, HEX64(24000000, 00000004));
900 code
[0] |= i
->subOp
<< 5;
904 CodeEmitterNVC0::emitShift(const Instruction
*i
)
906 if (i
->op
== OP_SHR
) {
907 emitForm_A(i
, HEX64(58000000, 00000003)
908 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
910 emitForm_A(i
, HEX64(60000000, 00000003));
913 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
918 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
920 if (i
->encSize
== 8) {
921 emitForm_B(i
, HEX64(60000000, 00000000));
923 if (i
->op
== OP_PREEX2
)
926 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
927 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
929 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
934 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
936 if (i
->encSize
== 8) {
937 code
[0] = 0x00000000 | (subOp
<< 26);
938 code
[1] = 0xc8000000;
942 defId(i
->def(0), 14);
943 srcId(i
->src(0), 20);
945 assert(i
->src(0).getFile() == FILE_GPR
);
947 if (i
->saturate
) code
[0] |= 1 << 5;
949 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
950 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
952 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
954 assert(!i
->src(0).mod
.neg());
955 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
960 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
964 assert(i
->encSize
== 8);
966 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
971 if (!isFloatType(i
->dType
))
972 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
973 if (i
->dType
== TYPE_F64
)
981 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
984 case ROUND_M
: code
[1] |= 1 << 17; break;
985 case ROUND_P
: code
[1] |= 2 << 17; break;
986 case ROUND_Z
: code
[1] |= 3 << 17; break;
987 case ROUND_NI
: code
[0] |= 1 << 7; break;
988 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
989 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
990 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
993 assert(!"invalid round mode");
999 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
1003 case ROUND_MI
: code
[0] |= 1 << 16; break;
1005 case ROUND_PI
: code
[0] |= 2 << 16; break;
1007 case ROUND_ZI
: code
[0] |= 3 << 16; break;
1014 CodeEmitterNVC0::emitCVT(Instruction
*i
)
1016 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
1020 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
1021 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
1022 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
1027 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
1028 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
1029 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
1031 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
1036 if (i
->encSize
== 8) {
1037 emitForm_B(i
, HEX64(10000000, 00000004));
1041 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
1042 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
1043 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
1045 // for 8/16 source types, the byte/word is in subOp. word 1 is
1046 // represented as 2.
1047 if (!isFloatType(i
->sType
))
1048 code
[1] |= i
->subOp
<< 0x17;
1050 code
[1] |= i
->subOp
<< 0x18;
1056 if (neg
&& i
->op
!= OP_ABS
)
1062 if (isSignedIntType(dType
))
1064 if (isSignedIntType(i
->sType
))
1067 if (isFloatType(dType
)) {
1068 if (!isFloatType(i
->sType
))
1069 code
[1] |= 0x08000000;
1071 if (isFloatType(i
->sType
))
1072 code
[1] |= 0x04000000;
1074 code
[1] |= 0x0c000000;
1077 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
1080 if (isFloatType(dType
)) {
1081 if (isFloatType(i
->sType
))
1084 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1086 assert(isFloatType(i
->sType
));
1088 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1091 if (neg
) code
[0] |= 1 << 16;
1092 if (sat
) code
[0] |= 1 << 18;
1093 if (abs
) code
[0] |= 1 << 19;
1100 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1105 if (i
->sType
== TYPE_F64
)
1108 if (!isFloatType(i
->sType
))
1111 if (isSignedIntType(i
->sType
))
1113 if (isFloatType(i
->dType
)) {
1114 if (isFloatType(i
->sType
))
1121 case OP_SET_AND
: hi
= 0x10000000; break;
1122 case OP_SET_OR
: hi
= 0x10200000; break;
1123 case OP_SET_XOR
: hi
= 0x10400000; break;
1128 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1130 if (i
->op
!= OP_SET
)
1131 srcId(i
->src(2), 32 + 17);
1133 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1134 if (i
->sType
== TYPE_F32
)
1135 code
[1] += 0x10000000;
1137 code
[1] += 0x08000000;
1139 code
[0] &= ~0xfc000;
1140 defId(i
->def(0), 17);
1141 if (i
->defExists(1))
1142 defId(i
->def(1), 14);
1150 emitCondCode(i
->setCond
, 32 + 23);
1155 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1161 op
= HEX64(30000000, 00000023);
1164 op
= HEX64(30000000, 00000003);
1167 op
= HEX64(38000000, 00000000);
1170 assert(!"invalid type for SLCT");
1176 CondCode cc
= i
->setCond
;
1178 if (i
->src(2).mod
.neg())
1179 cc
= reverseCondCode(cc
);
1181 emitCondCode(cc
, 32 + 23);
1188 selpFlip(const FixupEntry
*entry
, uint32_t *code
, const FixupData
& data
)
1190 int loc
= entry
->loc
;
1191 if (data
.force_persample_interp
)
1192 code
[loc
+ 1] |= 1 << 20;
1194 code
[loc
+ 1] &= ~(1 << 20);
1197 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1199 emitForm_A(i
, HEX64(20000000, 00000004));
1201 if (i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1204 if (i
->subOp
== 1) {
1205 addInterp(0, 0, selpFlip
);
1209 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1211 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1212 code
[1] = 0xf0000000;
1214 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1217 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1219 code
[0] = 0x00000086;
1220 code
[1] = 0xd0000000;
1222 code
[1] |= i
->tex
.r
;
1223 code
[1] |= i
->tex
.s
<< 8;
1225 if (i
->tex
.liveOnly
)
1228 defId(i
->def(0), 14);
1229 srcId(i
->src(0), 20);
1233 isNextIndependentTex(const TexInstruction
*i
)
1235 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1237 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1239 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1243 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1245 code
[0] = 0x00000006;
1247 if (isNextIndependentTex(i
))
1248 code
[0] |= 0x080; // t mode
1250 code
[0] |= 0x100; // p mode
1252 if (i
->tex
.liveOnly
)
1256 case OP_TEX
: code
[1] = 0x80000000; break;
1257 case OP_TXB
: code
[1] = 0x84000000; break;
1258 case OP_TXL
: code
[1] = 0x86000000; break;
1259 case OP_TXF
: code
[1] = 0x90000000; break;
1260 case OP_TXG
: code
[1] = 0xa0000000; break;
1261 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1262 case OP_TXD
: code
[1] = 0xe0000000; break;
1264 assert(!"invalid texture op");
1267 if (i
->op
== OP_TXF
) {
1268 if (!i
->tex
.levelZero
)
1269 code
[1] |= 0x02000000;
1271 if (i
->tex
.levelZero
) {
1272 code
[1] |= 0x02000000;
1275 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1278 defId(i
->def(0), 14);
1279 srcId(i
->src(0), 20);
1283 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1285 code
[1] |= i
->tex
.mask
<< 14;
1287 code
[1] |= i
->tex
.r
;
1288 code
[1] |= i
->tex
.s
<< 8;
1289 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1290 code
[1] |= 1 << 18; // in 1st source (with array index)
1293 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1294 if (i
->tex
.target
.isCube())
1296 if (i
->tex
.target
.isArray())
1298 if (i
->tex
.target
.isShadow())
1301 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1303 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1305 if (i
->op
== OP_TXL
)
1306 code
[1] &= ~(1 << 26);
1308 if (i
->op
== OP_TXF
)
1309 code
[1] &= ~(1 << 25);
1311 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1312 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1315 if (i
->tex
.useOffsets
== 1)
1317 if (i
->tex
.useOffsets
== 4)
1324 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1326 code
[0] = 0x00000086;
1327 code
[1] = 0xc0000000;
1329 switch (i
->tex
.query
) {
1330 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1331 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1332 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1333 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1334 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1335 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1337 assert(!"invalid texture query");
1341 code
[1] |= i
->tex
.mask
<< 14;
1343 code
[1] |= i
->tex
.r
;
1344 code
[1] |= i
->tex
.s
<< 8;
1345 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1348 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1350 defId(i
->def(0), 14);
1351 srcId(i
->src(0), 20);
1358 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1360 code
[0] = 0x00000200 | (laneMask
<< 6); // dall
1361 code
[1] = 0x48000000 | qOp
;
1363 defId(i
->def(0), 14);
1364 srcId(i
->src(0), 20);
1365 srcId((i
->srcExists(1) && i
->predSrc
!= 1) ? i
->src(1) : i
->src(0), 26);
1371 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1373 const FlowInstruction
*f
= i
->asFlow();
1375 unsigned mask
; // bit 0: predicate, bit 1: target
1377 code
[0] = 0x00000007;
1381 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1382 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1387 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1389 code
[0] |= 0x4000; // indirect calls always use c[] source
1393 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1394 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1395 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1396 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1397 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1399 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1400 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1401 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1402 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1404 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1405 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1406 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1408 assert(!"invalid flow operation");
1414 if (i
->flagsSrc
< 0)
1427 if (code
[0] & 0x4000) {
1428 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1429 setAddress16(i
->src(0));
1430 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1431 if (f
->op
== OP_BRA
)
1432 srcId(f
->src(0).getIndirect(0), 20);
1438 if (f
->op
== OP_CALL
) {
1443 assert(f
->absolute
);
1444 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1445 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1446 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1448 assert(!f
->absolute
);
1449 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1450 code
[0] |= (pcRel
& 0x3f) << 26;
1451 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1455 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1456 if (writeIssueDelays
&& !(f
->target
.bb
->binPos
& 0x3f))
1458 // currently we don't want absolute branches
1459 assert(!f
->absolute
);
1460 code
[0] |= (pcRel
& 0x3f) << 26;
1461 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1466 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1468 Value
*rDef
= NULL
, *pDef
= NULL
;
1471 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1472 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1473 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1474 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1477 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1480 code
[1] = 0x50000000;
1482 code
[0] |= 63 << 14;
1488 if (i
->src(0).getFile() == FILE_GPR
) {
1489 srcId(i
->src(0), 20);
1491 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1493 code
[0] |= imm
->reg
.data
.u32
<< 20;
1498 if (i
->src(1).getFile() == FILE_GPR
) {
1499 srcId(i
->src(1), 26);
1501 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1503 assert(imm
->reg
.data
.u32
<= 0xfff);
1504 code
[0] |= imm
->reg
.data
.u32
<< 26;
1505 code
[1] |= imm
->reg
.data
.u32
>> 6;
1509 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1510 srcId(i
->src(2), 32 + 17);
1511 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1517 if (i
->defExists(0)) {
1518 if (i
->def(0).getFile() == FILE_GPR
)
1519 rDef
= i
->getDef(0);
1521 pDef
= i
->getDef(0);
1523 if (i
->defExists(1)) {
1524 if (i
->def(1).getFile() == FILE_GPR
)
1525 rDef
= i
->getDef(1);
1527 pDef
= i
->getDef(1);
1531 code
[0] &= ~(63 << 14);
1535 code
[1] &= ~(7 << 21);
1536 defId(pDef
, 32 + 21);
1541 CodeEmitterNVC0::emitAFETCH(const Instruction
*i
)
1543 code
[0] = 0x00000006;
1544 code
[1] = 0x0c000000 | (i
->src(0).get()->reg
.data
.offset
& 0x7ff);
1546 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1551 defId(i
->def(0), 14);
1552 srcId(i
->src(0).getIndirect(0), 20);
1556 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1558 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1560 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1561 code
[1] = 0x00000000 | (prim
>> 6);
1565 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1567 defId(i
->def(0), 14);
1572 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1574 code
[0] = 0x00000006;
1575 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1579 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1580 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1584 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1586 defId(i
->def(0), 14);
1587 srcId(i
->src(0).getIndirect(0), 20);
1588 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1592 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1594 unsigned int size
= typeSizeof(i
->dType
);
1596 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1597 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1599 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1606 assert(i
->src(1).getFile() == FILE_GPR
);
1608 srcId(i
->src(0).getIndirect(0), 20);
1609 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1610 srcId(i
->src(1), 26);
1614 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1616 code
[0] = 0x00000006;
1617 code
[1] = 0x1c000000;
1621 defId(i
->def(0), 14); // new secret address
1622 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1624 assert(i
->src(0).getFile() == FILE_GPR
);
1626 if (i
->op
== OP_EMIT
)
1628 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1632 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1633 unsigned int stream
= SDATA(i
->src(1)).u32
;
1637 code
[0] |= stream
<< 26;
1642 srcId(i
->src(1), 26);
1647 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1649 if (i
->encSize
== 8) {
1650 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1652 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1654 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1659 interpApply(const FixupEntry
*entry
, uint32_t *code
, const FixupData
& data
)
1661 int ipa
= entry
->ipa
;
1662 int reg
= entry
->reg
;
1663 int loc
= entry
->loc
;
1665 if (data
.flatshade
&&
1666 (ipa
& NV50_IR_INTERP_MODE_MASK
) == NV50_IR_INTERP_SC
) {
1667 ipa
= NV50_IR_INTERP_FLAT
;
1669 } else if (data
.force_persample_interp
&&
1670 (ipa
& NV50_IR_INTERP_SAMPLE_MASK
) == NV50_IR_INTERP_DEFAULT
&&
1671 (ipa
& NV50_IR_INTERP_MODE_MASK
) != NV50_IR_INTERP_FLAT
) {
1672 ipa
|= NV50_IR_INTERP_CENTROID
;
1674 code
[loc
+ 0] &= ~(0xf << 6);
1675 code
[loc
+ 0] |= ipa
<< 6;
1676 code
[loc
+ 0] &= ~(0x3f << 26);
1677 code
[loc
+ 0] |= reg
<< 26;
1681 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1683 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1685 if (i
->encSize
== 8) {
1686 code
[0] = 0x00000000;
1687 code
[1] = 0xc0000000 | (base
& 0xffff);
1692 if (i
->op
== OP_PINTERP
) {
1693 srcId(i
->src(1), 26);
1694 addInterp(i
->ipa
, SDATA(i
->src(1)).id
, interpApply
);
1696 code
[0] |= 0x3f << 26;
1697 addInterp(i
->ipa
, 0x3f, interpApply
);
1700 srcId(i
->src(0).getIndirect(0), 20);
1702 assert(i
->op
== OP_PINTERP
);
1703 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1704 srcId(i
->src(1), 20);
1709 defId(i
->def(0), 14);
1711 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1712 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1714 code
[1] |= 0x3f << 17;
1718 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1751 assert(!"invalid type");
1758 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1779 assert(!"invalid caching mode");
1786 uses64bitAddress(const Instruction
*ldst
)
1788 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1789 ldst
->src(0).isIndirect(0) &&
1790 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1794 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1798 switch (i
->src(0).getFile()) {
1799 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1800 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1801 case FILE_MEMORY_SHARED
:
1802 if (i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1803 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1812 assert(!"invalid memory file");
1816 code
[0] = 0x00000005;
1819 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
) {
1820 // Unlocked store on shared memory can fail.
1821 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
&&
1822 i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1823 assert(i
->defExists(0));
1824 defId(i
->def(0), 8);
1828 setAddressByFile(i
->src(0));
1829 srcId(i
->src(1), 14);
1830 srcId(i
->src(0).getIndirect(0), 20);
1831 if (uses64bitAddress(i
))
1836 emitLoadStoreType(i
->dType
);
1837 emitCachingMode(i
->cache
);
1841 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1845 code
[0] = 0x00000005;
1847 switch (i
->src(0).getFile()) {
1848 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1849 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1850 case FILE_MEMORY_SHARED
:
1851 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1852 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1860 case FILE_MEMORY_CONST
:
1861 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1862 emitMOV(i
); // not sure if this is any better
1865 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1866 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1869 assert(!"invalid memory file");
1876 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
) {
1877 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1878 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
1881 } else if (i
->defExists(1)) { // r, p
1884 assert(!"Expected predicate dest for load locked");
1890 defId(i
->def(r
), 14);
1892 code
[0] |= 63 << 14;
1895 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1896 defId(i
->def(p
), 8);
1898 defId(i
->def(p
), 32 + 18);
1901 setAddressByFile(i
->src(0));
1902 srcId(i
->src(0).getIndirect(0), 20);
1903 if (uses64bitAddress(i
))
1908 emitLoadStoreType(i
->dType
);
1909 emitCachingMode(i
->cache
);
1913 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1915 switch (SDATA(ref
).sv
.sv
) {
1916 case SV_LANEID
: return 0x00;
1917 case SV_PHYSID
: return 0x03;
1918 case SV_VERTEX_COUNT
: return 0x10;
1919 case SV_INVOCATION_ID
: return 0x11;
1920 case SV_YDIR
: return 0x12;
1921 case SV_THREAD_KILL
: return 0x13;
1922 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1923 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1924 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1925 case SV_GRIDID
: return 0x2c;
1926 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1927 case SV_LBASE
: return 0x34;
1928 case SV_SBASE
: return 0x30;
1929 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1931 assert(!"no sreg for system value");
1937 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1939 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1940 if (i
->src(0).getFile() == FILE_GPR
) {
1941 code
[0] = 0xfc01c003;
1942 code
[1] = 0x1a8e0000;
1943 srcId(i
->src(0), 20);
1945 code
[0] = 0x0001c004;
1946 code
[1] = 0x0c0e0000;
1947 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1949 if (!i
->getSrc(0)->reg
.data
.u32
)
1952 srcId(i
->src(0), 20);
1955 defId(i
->def(0), 17);
1958 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1959 uint8_t sr
= getSRegEncoding(i
->src(0));
1961 if (i
->encSize
== 8) {
1962 code
[0] = 0x00000004 | (sr
<< 26);
1963 code
[1] = 0x2c000000;
1965 code
[0] = 0x40000008 | (sr
<< 20);
1967 defId(i
->def(0), 14);
1971 if (i
->encSize
== 8) {
1974 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1975 opc
= HEX64(18000000, 000001e2
);
1977 if (i
->src(0).getFile() == FILE_PREDICATE
)
1978 opc
= HEX64(080e0000
, 1c000004
);
1980 opc
= HEX64(28000000, 00000004);
1982 if (i
->src(0).getFile() != FILE_PREDICATE
)
1983 opc
|= i
->lanes
<< 5;
1987 // Explicitly emit the predicate source as emitForm_B skips it.
1988 if (i
->src(0).getFile() == FILE_PREDICATE
)
1989 srcId(i
->src(0), 20);
1993 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1994 imm
= SDATA(i
->src(0)).u32
;
1995 if (imm
& 0xfff00000) {
1996 assert(!(imm
& 0x000fffff));
1997 code
[0] = 0x00000318 | imm
;
1999 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
2000 code
[0] = 0x00000118 | (imm
<< 20);
2004 emitShortSrc2(i
->src(0));
2006 defId(i
->def(0), 14);
2013 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
2015 const bool hasDst
= i
->defExists(0);
2016 const bool casOrExch
=
2017 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
2018 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
2020 if (i
->dType
== TYPE_U64
) {
2022 case NV50_IR_SUBOP_ATOM_ADD
:
2025 code
[1] = 0x507e0000;
2027 code
[1] = 0x10000000;
2029 case NV50_IR_SUBOP_ATOM_EXCH
:
2031 code
[1] = 0x507e0000;
2033 case NV50_IR_SUBOP_ATOM_CAS
:
2035 code
[1] = 0x50000000;
2038 assert(!"invalid u64 red op");
2042 if (i
->dType
== TYPE_U32
) {
2044 case NV50_IR_SUBOP_ATOM_EXCH
:
2046 code
[1] = 0x507e0000;
2048 case NV50_IR_SUBOP_ATOM_CAS
:
2050 code
[1] = 0x50000000;
2053 code
[0] = 0x5 | (i
->subOp
<< 5);
2055 code
[1] = 0x507e0000;
2057 code
[1] = 0x10000000;
2061 if (i
->dType
== TYPE_S32
) {
2062 assert(i
->subOp
<= 2);
2063 code
[0] = 0x205 | (i
->subOp
<< 5);
2065 code
[1] = 0x587e0000;
2067 code
[1] = 0x18000000;
2069 if (i
->dType
== TYPE_F32
) {
2070 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
2073 code
[1] = 0x687e0000;
2075 code
[1] = 0x28000000;
2080 srcId(i
->src(1), 14);
2083 defId(i
->def(0), 32 + 11);
2086 code
[1] |= 63 << 11;
2088 if (hasDst
|| casOrExch
) {
2089 const int32_t offset
= SDATA(i
->src(0)).offset
;
2090 assert(offset
< 0x80000 && offset
>= -0x80000);
2091 code
[0] |= offset
<< 26;
2092 code
[1] |= (offset
& 0x1ffc0) >> 6;
2093 code
[1] |= (offset
& 0xe0000) << 6;
2095 srcAddr32(i
->src(0), 26, 0);
2097 if (i
->getIndirect(0, 0)) {
2098 srcId(i
->getIndirect(0, 0), 20);
2099 if (i
->getIndirect(0, 0)->reg
.size
== 8)
2102 code
[0] |= 63 << 20;
2105 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
2106 assert(i
->src(1).getSize() == 2 * typeSizeof(i
->sType
));
2107 code
[1] |= (SDATA(i
->src(1)).id
+ 1) << 17;
2112 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
2114 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
2115 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
2116 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
2119 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
2122 code
[1] = 0xe0000000;
2128 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
2130 code
[0] = 0x00000005 | (i
->subOp
<< 5);
2132 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
2133 code
[1] = 0x98000000;
2134 srcAddr32(i
->src(0), 28, 2);
2136 code
[1] = 0xd0000000;
2137 setAddress24(i
->src(0));
2139 if (uses64bitAddress(i
))
2141 srcId(i
->src(0).getIndirect(0), 20);
2149 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
2152 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
2153 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
2154 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
2155 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
2156 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
2157 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
2158 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
2159 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
2160 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
2161 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
2162 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
2163 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
2164 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
2165 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
2166 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
2167 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
2172 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
2177 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
2179 ImmediateValue
*imm
= NULL
;
2182 if (i
->srcExists(2)) {
2183 imm
= i
->getSrc(2)->asImm();
2185 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
2189 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
2190 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
2191 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
2198 if (i
->op
== OP_SUCLAMP
) {
2199 if (i
->dType
== TYPE_S32
)
2201 emitSUCLAMPMode(i
->subOp
);
2204 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2207 if (i
->op
!= OP_SUEAU
) {
2208 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2209 code
[0] |= 63 << 14;
2210 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2212 if (i
->defExists(1)) { // r, p
2213 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2214 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2220 assert(i
->op
== OP_SUCLAMP
);
2222 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2227 CodeEmitterNVC0::emitSUGType(DataType ty
)
2230 case TYPE_S32
: code
[1] |= 1 << 13; break;
2231 case TYPE_U8
: code
[1] |= 2 << 13; break;
2232 case TYPE_S8
: code
[1] |= 3 << 13; break;
2234 assert(ty
== TYPE_U32
);
2240 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2242 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2244 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2245 assert(offset
== (offset
& 0xfffc));
2248 code
[0] |= offset
<< 24;
2249 code
[1] |= offset
>> 8;
2250 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2254 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2256 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2257 code
[1] |= 0x7 << 17;
2259 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2261 srcId(i
->src(s
), 32 + 17);
2266 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2269 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2271 emitLoadStoreType(i
->dType
);
2272 emitSUGType(i
->sType
);
2273 emitCachingMode(i
->cache
);
2276 defId(i
->def(0), 14); // destination
2277 srcId(i
->src(0), 20); // address
2279 if (i
->src(1).getFile() == FILE_GPR
)
2280 srcId(i
->src(1), 26);
2287 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2290 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2292 if (i
->op
== OP_SUSTP
)
2293 code
[1] |= i
->tex
.mask
<< 22;
2295 emitLoadStoreType(i
->dType
);
2296 emitSUGType(i
->sType
);
2297 emitCachingMode(i
->cache
);
2300 srcId(i
->src(0), 20); // address
2302 if (i
->src(1).getFile() == FILE_GPR
)
2303 srcId(i
->src(1), 26);
2306 srcId(i
->src(3), 14); // values
2311 CodeEmitterNVC0::emitSUAddr(const TexInstruction
*i
)
2313 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2315 if (i
->tex
.rIndirectSrc
< 0) {
2316 code
[1] |= 0x00004000;
2317 code
[0] |= i
->tex
.r
<< 26;
2319 srcId(i
, i
->tex
.rIndirectSrc
, 26);
2324 CodeEmitterNVC0::emitSUDim(const TexInstruction
*i
)
2326 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2328 code
[1] |= (i
->tex
.target
.getDim() - 1) << 12;
2329 if (i
->tex
.target
.isArray() || i
->tex
.target
.isCube() ||
2330 i
->tex
.target
.getDim() == 3) {
2331 // use e2d mode for 3-dim images, arrays and cubes.
2335 srcId(i
->src(0), 20);
2339 CodeEmitterNVC0::emitSULEA(const TexInstruction
*i
)
2341 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2344 code
[1] = 0xf0000000;
2347 emitLoadStoreType(i
->sType
);
2349 defId(i
->def(0), 14);
2351 if (i
->defExists(1)) {
2352 defId(i
->def(1), 32 + 22);
2362 CodeEmitterNVC0::emitSULDB(const TexInstruction
*i
)
2364 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2367 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2370 emitLoadStoreType(i
->dType
);
2372 defId(i
->def(0), 14);
2374 emitCachingMode(i
->cache
);
2380 CodeEmitterNVC0::emitSUSTx(const TexInstruction
*i
)
2382 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2385 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2387 if (i
->op
== OP_SUSTP
)
2388 code
[1] |= i
->tex
.mask
<< 17;
2390 emitLoadStoreType(i
->dType
);
2394 srcId(i
->src(1), 14);
2396 emitCachingMode(i
->cache
);
2402 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2404 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2406 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2407 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2408 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2409 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2412 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2413 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2414 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2415 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2416 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2417 code
[1] |= (i
->mask
& 0x3) << 2;
2420 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2421 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2422 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2423 code
[1] |= (i
->mask
& 0x3) << 2;
2424 code
[1] |= (i
->mask
& 0xc) << 21;
2433 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2437 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2438 case 0: opc
|= 0xe8ULL
<< 56; break;
2439 case 1: opc
|= 0xb4ULL
<< 56; break;
2440 case 2: opc
|= 0x94ULL
<< 56; break;
2445 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2446 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2447 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2449 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2450 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2457 if (i
->flagsDef
>= 0)
2462 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2464 assert(i
->encSize
== 8);
2465 emitForm_A(i
, HEX64(10000000, 00000006));
2466 code
[0] |= i
->subOp
<< 5;
2467 code
[1] |= 0x00e00000;
2471 CodeEmitterNVC0::emitVOTE(const Instruction
*i
)
2473 assert(i
->src(0).getFile() == FILE_PREDICATE
);
2475 code
[0] = 0x00000004 | (i
->subOp
<< 5);
2476 code
[1] = 0x48000000;
2481 for (int d
= 0; i
->defExists(d
); d
++) {
2482 if (i
->def(d
).getFile() == FILE_PREDICATE
) {
2485 defId(i
->def(d
), 32 + 22);
2486 } else if (i
->def(d
).getFile() == FILE_GPR
) {
2489 defId(i
->def(d
), 14);
2491 assert(!"Unhandled def");
2495 code
[0] |= 63 << 14;
2498 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
2500 srcId(i
->src(0), 20);
2504 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2506 unsigned int size
= insn
->encSize
;
2508 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2511 if (!insn
->encSize
) {
2512 ERROR("skipping unencodable instruction: "); insn
->print();
2515 if (codeSize
+ size
> codeSizeLimit
) {
2516 ERROR("code emitter output buffer too small\n");
2520 if (writeIssueDelays
) {
2521 if (!(codeSize
& 0x3f)) {
2522 code
[0] = 0x00000007; // cf issue delay "instruction"
2523 code
[1] = 0x20000000;
2527 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2528 uint32_t *data
= code
- (id
* 2 + 2);
2530 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2533 data
[0] |= insn
->sched
<< 28;
2534 data
[1] |= insn
->sched
>> 4;
2536 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2540 // assert that instructions with multiple defs don't corrupt registers
2541 for (int d
= 0; insn
->defExists(d
); ++d
)
2542 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2579 if (insn
->dType
== TYPE_F64
)
2581 else if (isFloatType(insn
->dType
))
2587 if (insn
->dType
== TYPE_F64
)
2589 else if (isFloatType(insn
->dType
))
2596 if (insn
->dType
== TYPE_F64
)
2598 else if (isFloatType(insn
->dType
))
2610 emitLogicOp(insn
, 0);
2613 emitLogicOp(insn
, 1);
2616 emitLogicOp(insn
, 2);
2626 emitSET(insn
->asCmp());
2632 emitSLCT(insn
->asCmp());
2647 if (insn
->def(0).getFile() == FILE_PREDICATE
||
2648 insn
->src(0).getFile() == FILE_PREDICATE
)
2654 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
2657 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
2682 emitTEX(insn
->asTex());
2685 emitTXQ(insn
->asTex());
2699 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2700 emitSULDGB(insn
->asTex());
2702 emitSULDB(insn
->asTex());
2706 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2707 emitSUSTGx(insn
->asTex());
2709 emitSUSTx(insn
->asTex());
2712 emitSULEA(insn
->asTex());
2734 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2737 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2740 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2782 ERROR("operation should have been eliminated");
2788 ERROR("operation should have been lowered\n");
2791 ERROR("unknown op: %u\n", insn
->op
);
2797 assert(insn
->encSize
== 8);
2800 code
+= insn
->encSize
/ 4;
2801 codeSize
+= insn
->encSize
;
2806 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2808 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2810 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2813 if (i
->ftz
|| i
->saturate
|| i
->join
)
2815 if (i
->rnd
!= ROUND_N
)
2817 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2820 if (i
->op
== OP_PINTERP
) {
2821 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2824 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2828 for (int s
= 0; i
->srcExists(s
); ++s
) {
2829 if (i
->src(s
).isIndirect(0))
2832 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2833 if (SDATA(i
->src(s
)).offset
>= 0x100)
2835 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2836 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2839 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2840 if (i
->dType
== TYPE_F32
) {
2841 if (SDATA(i
->src(s
)).u32
>= 0x100)
2844 if (SDATA(i
->src(s
)).u32
> 0xff)
2849 if (i
->op
== OP_CVT
)
2851 if (i
->src(s
).mod
!= Modifier(0)) {
2852 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2853 if (i
->op
!= OP_RSQ
)
2855 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2856 if (i
->op
!= OP_ADD
|| s
!= 0)
2864 // Simplified, erring on safe side.
2865 class SchedDataCalculator
: public Pass
2868 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2874 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2875 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2876 int tex
; // TEX to non-TEX delay 17 (0x11)
2877 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2878 int imul
; // integer MUL to MUL delay 3
2888 void rebase(const int base
)
2890 const int delta
= this->base
- base
;
2895 for (int i
= 0; i
< regs
; ++i
) {
2899 for (int i
= 0; i
< 8; ++i
) {
2906 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2916 memset(&rd
, 0, sizeof(rd
));
2917 memset(&wr
, 0, sizeof(wr
));
2918 memset(&res
, 0, sizeof(res
));
2921 int getLatest(const ScoreData
& d
) const
2924 for (int i
= 0; i
< regs
; ++i
)
2927 for (int i
= 0; i
< 8; ++i
)
2934 inline int getLatestRd() const
2936 return getLatest(rd
);
2938 inline int getLatestWr() const
2940 return getLatest(wr
);
2942 inline int getLatest() const
2944 const int a
= getLatestRd();
2945 const int b
= getLatestWr();
2947 int max
= MAX2(a
, b
);
2948 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2949 max
= MAX2(res
.ld
[f
], max
);
2950 max
= MAX2(res
.st
[f
], max
);
2952 max
= MAX2(res
.sfu
, max
);
2953 max
= MAX2(res
.imul
, max
);
2954 max
= MAX2(res
.tex
, max
);
2957 void setMax(const RegScores
*that
)
2959 for (int i
= 0; i
< regs
; ++i
) {
2960 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
2961 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
2963 for (int i
= 0; i
< 8; ++i
) {
2964 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
2965 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
2967 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
2968 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
2970 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2971 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
2972 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
2974 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
2975 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
2976 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
2978 void print(int cycle
)
2980 for (int i
= 0; i
< regs
; ++i
) {
2981 if (rd
.r
[i
] > cycle
)
2982 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
2983 if (wr
.r
[i
] > cycle
)
2984 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
2986 for (int i
= 0; i
< 8; ++i
) {
2987 if (rd
.p
[i
] > cycle
)
2988 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
2989 if (wr
.p
[i
] > cycle
)
2990 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
2993 INFO("rd $c @ %i\n", rd
.c
);
2995 INFO("wr $c @ %i\n", wr
.c
);
2996 if (res
.sfu
> cycle
)
2997 INFO("sfu @ %i\n", res
.sfu
);
2998 if (res
.imul
> cycle
)
2999 INFO("imul @ %i\n", res
.imul
);
3000 if (res
.tex
> cycle
)
3001 INFO("tex @ %i\n", res
.tex
);
3005 RegScores
*score
; // for current BB
3006 std::vector
<RegScores
> scoreBoards
;
3012 bool visit(Function
*);
3013 bool visit(BasicBlock
*);
3015 void commitInsn(const Instruction
*, int cycle
);
3016 int calcDelay(const Instruction
*, int cycle
) const;
3017 void setDelay(Instruction
*, int delay
, Instruction
*next
);
3019 void recordRd(const Value
*, const int ready
);
3020 void recordWr(const Value
*, const int ready
);
3021 void checkRd(const Value
*, int cycle
, int& delay
) const;
3022 void checkWr(const Value
*, int cycle
, int& delay
) const;
3024 int getCycles(const Instruction
*, int origDelay
) const;
3028 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
3030 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
3031 delay
= MAX2(delay
, 14);
3033 if (insn
->op
== OP_TEXBAR
) {
3034 // TODO: except if results not used before EXIT
3037 if (insn
->op
== OP_JOIN
|| insn
->join
) {
3040 if (delay
>= 0 || prevData
== 0x04 ||
3041 !next
|| !targ
->canDualIssue(insn
, next
)) {
3042 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
3043 if (prevOp
== OP_EXPORT
)
3044 insn
->sched
|= 0x40;
3046 insn
->sched
|= 0x20;
3048 insn
->sched
= 0x04; // dual-issue
3051 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
3052 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
3055 prevData
= insn
->sched
;
3059 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
3061 if (insn
->sched
& 0x80) {
3062 int c
= (insn
->sched
& 0x0f) * 2 + 1;
3063 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
3067 if (insn
->sched
& 0x60)
3068 return (insn
->sched
& 0x1f) + 1;
3069 return (insn
->sched
== 0x04) ? 0 : 32;
3073 SchedDataCalculator::visit(Function
*func
)
3075 int regs
= targ
->getFileSize(FILE_GPR
) + 1;
3076 scoreBoards
.resize(func
->cfg
.getSize());
3077 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
3078 scoreBoards
[i
].wipe(regs
);
3083 SchedDataCalculator::visit(BasicBlock
*bb
)
3086 Instruction
*next
= NULL
;
3092 score
= &scoreBoards
.at(bb
->getId());
3094 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
3095 // back branches will wait until all target dependencies are satisfied
3096 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
3098 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
3099 if (in
->getExit()) {
3100 if (prevData
!= 0x04)
3101 prevData
= in
->getExit()->sched
;
3102 prevOp
= in
->getExit()->op
;
3104 score
->setMax(&scoreBoards
.at(in
->getId()));
3106 if (bb
->cfg
.incidentCount() > 1)
3109 #ifdef NVC0_DEBUG_SCHED_DATA
3110 INFO("=== BB:%i initial scores\n", bb
->getId());
3111 score
->print(cycle
);
3114 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
3117 commitInsn(insn
, cycle
);
3118 int delay
= calcDelay(next
, cycle
);
3119 setDelay(insn
, delay
, next
);
3120 cycle
+= getCycles(insn
, delay
);
3122 #ifdef NVC0_DEBUG_SCHED_DATA
3123 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
3130 commitInsn(insn
, cycle
);
3134 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
3135 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
3137 if (ei
.getType() != Graph::Edge::BACK
) {
3138 // only test the first instruction of the outgoing block
3139 next
= out
->getEntry();
3141 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
3143 // wait until all dependencies are satisfied
3144 const int regsFree
= score
->getLatest();
3145 next
= out
->getFirst();
3146 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
3147 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
3148 c
+= getCycles(next
, bbDelay
);
3153 if (bb
->cfg
.outgoingCount() != 1)
3155 setDelay(insn
, bbDelay
, next
);
3156 cycle
+= getCycles(insn
, bbDelay
);
3158 score
->rebase(cycle
); // common base for initializing out blocks' scores
3162 #define NVE4_MAX_ISSUE_DELAY 0x1f
3164 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
3166 int delay
= 0, ready
= cycle
;
3168 for (int s
= 0; insn
->srcExists(s
); ++s
)
3169 checkRd(insn
->getSrc(s
), cycle
, delay
);
3170 // WAR & WAW don't seem to matter
3171 // for (int s = 0; insn->srcExists(s); ++s)
3172 // recordRd(insn->getSrc(s), cycle);
3174 switch (Target::getOpClass(insn
->op
)) {
3176 ready
= score
->res
.sfu
;
3179 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3180 ready
= score
->res
.imul
;
3182 case OPCLASS_TEXTURE
:
3183 ready
= score
->res
.tex
;
3186 ready
= score
->res
.ld
[insn
->src(0).getFile()];
3189 ready
= score
->res
.st
[insn
->src(0).getFile()];
3194 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
3195 ready
= MAX2(ready
, score
->res
.tex
);
3197 delay
= MAX2(delay
, ready
- cycle
);
3199 // if can issue next cycle, delay is 0, not 1
3200 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
3204 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
3206 const int ready
= cycle
+ targ
->getLatency(insn
);
3208 for (int d
= 0; insn
->defExists(d
); ++d
)
3209 recordWr(insn
->getDef(d
), ready
);
3210 // WAR & WAW don't seem to matter
3211 // for (int s = 0; insn->srcExists(s); ++s)
3212 // recordRd(insn->getSrc(s), cycle);
3214 switch (Target::getOpClass(insn
->op
)) {
3216 score
->res
.sfu
= cycle
+ 4;
3219 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3220 score
->res
.imul
= cycle
+ 4;
3222 case OPCLASS_TEXTURE
:
3223 score
->res
.tex
= cycle
+ 18;
3226 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
3228 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
3229 score
->res
.st
[insn
->src(0).getFile()] = ready
;
3232 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
3233 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
3236 if (insn
->op
== OP_TEXBAR
)
3237 score
->res
.tex
= cycle
;
3243 #ifdef NVC0_DEBUG_SCHED_DATA
3244 score
->print(cycle
);
3249 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
3254 switch (v
->reg
.file
) {
3257 b
= a
+ v
->reg
.size
/ 4;
3258 for (int r
= a
; r
< b
; ++r
)
3259 ready
= MAX2(ready
, score
->rd
.r
[r
]);
3261 case FILE_PREDICATE
:
3262 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
3265 ready
= MAX2(ready
, score
->rd
.c
);
3267 case FILE_SHADER_INPUT
:
3268 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
3269 case FILE_MEMORY_LOCAL
:
3270 case FILE_MEMORY_CONST
:
3271 case FILE_MEMORY_SHARED
:
3272 case FILE_MEMORY_GLOBAL
:
3273 case FILE_SYSTEM_VALUE
:
3274 // TODO: any restrictions here ?
3276 case FILE_IMMEDIATE
:
3283 delay
= MAX2(delay
, ready
- cycle
);
3287 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
3292 switch (v
->reg
.file
) {
3295 b
= a
+ v
->reg
.size
/ 4;
3296 for (int r
= a
; r
< b
; ++r
)
3297 ready
= MAX2(ready
, score
->wr
.r
[r
]);
3299 case FILE_PREDICATE
:
3300 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
3303 assert(v
->reg
.file
== FILE_FLAGS
);
3304 ready
= MAX2(ready
, score
->wr
.c
);
3308 delay
= MAX2(delay
, ready
- cycle
);
3312 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
3314 int a
= v
->reg
.data
.id
;
3316 if (v
->reg
.file
== FILE_GPR
) {
3317 int b
= a
+ v
->reg
.size
/ 4;
3318 for (int r
= a
; r
< b
; ++r
)
3319 score
->rd
.r
[r
] = ready
;
3321 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
3322 if (v
->reg
.file
== FILE_PREDICATE
) {
3323 score
->rd
.p
[a
] = ready
+ 4;
3325 assert(v
->reg
.file
== FILE_FLAGS
);
3326 score
->rd
.c
= ready
+ 4;
3331 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
3333 int a
= v
->reg
.data
.id
;
3335 if (v
->reg
.file
== FILE_GPR
) {
3336 int b
= a
+ v
->reg
.size
/ 4;
3337 for (int r
= a
; r
< b
; ++r
)
3338 score
->wr
.r
[r
] = ready
;
3340 if (v
->reg
.file
== FILE_PREDICATE
) {
3341 score
->wr
.p
[a
] = ready
;
3343 if (v
->reg
.file
== FILE_FLAGS
) {
3344 score
->wr
.c
= ready
;
3349 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
3351 SchedDataCalculator
sched(targ
);
3352 return sched
.run(func
, true, true);
3356 CodeEmitterNVC0::prepareEmission(Function
*func
)
3358 CodeEmitter::prepareEmission(func
);
3360 if (targ
->hasSWSched
)
3361 calculateSchedDataNVC0(targ
, func
);
3364 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3365 : CodeEmitter(target
),
3367 writeIssueDelays(target
->hasSWSched
)
3370 codeSize
= codeSizeLimit
= 0;
3375 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3377 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3378 emit
->setProgramType(type
);
3383 TargetNVC0::getCodeEmitter(Program::Type type
)
3385 if (chipset
>= NVISA_GK20A_CHIPSET
)
3386 return createCodeEmitterGK110(type
);
3387 return createCodeEmitterNVC0(type
);
3390 } // namespace nv50_ir