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
);
61 void setPDSTL(const Instruction
*, const int d
);
63 void emitCondCode(CondCode cc
, int pos
);
64 void emitInterpMode(const Instruction
*);
65 void emitLoadStoreType(DataType ty
);
66 void emitSUGType(DataType
);
67 void emitSUAddr(const TexInstruction
*);
68 void emitSUDim(const TexInstruction
*);
69 void emitCachingMode(CacheMode c
);
71 void emitShortSrc2(const ValueRef
&);
73 inline uint8_t getSRegEncoding(const ValueRef
&);
75 void roundMode_A(const Instruction
*);
76 void roundMode_C(const Instruction
*);
77 void roundMode_CS(const Instruction
*);
79 void emitNegAbs12(const Instruction
*);
81 void emitNOP(const Instruction
*);
83 void emitLOAD(const Instruction
*);
84 void emitSTORE(const Instruction
*);
85 void emitMOV(const Instruction
*);
86 void emitATOM(const Instruction
*);
87 void emitMEMBAR(const Instruction
*);
88 void emitCCTL(const Instruction
*);
90 void emitINTERP(const Instruction
*);
91 void emitAFETCH(const Instruction
*);
92 void emitPFETCH(const Instruction
*);
93 void emitVFETCH(const Instruction
*);
94 void emitEXPORT(const Instruction
*);
95 void emitOUT(const Instruction
*);
97 void emitUADD(const Instruction
*);
98 void emitFADD(const Instruction
*);
99 void emitDADD(const Instruction
*);
100 void emitUMUL(const Instruction
*);
101 void emitFMUL(const Instruction
*);
102 void emitDMUL(const Instruction
*);
103 void emitIMAD(const Instruction
*);
104 void emitISAD(const Instruction
*);
105 void emitSHLADD(const Instruction
*a
);
106 void emitFMAD(const Instruction
*);
107 void emitDMAD(const Instruction
*);
108 void emitMADSP(const Instruction
*);
110 void emitNOT(Instruction
*);
111 void emitLogicOp(const Instruction
*, uint8_t subOp
);
112 void emitPOPC(const Instruction
*);
113 void emitINSBF(const Instruction
*);
114 void emitEXTBF(const Instruction
*);
115 void emitBFIND(const Instruction
*);
116 void emitPERMT(const Instruction
*);
117 void emitShift(const Instruction
*);
119 void emitSFnOp(const Instruction
*, uint8_t subOp
);
121 void emitCVT(Instruction
*);
122 void emitMINMAX(const Instruction
*);
123 void emitPreOp(const Instruction
*);
125 void emitSET(const CmpInstruction
*);
126 void emitSLCT(const CmpInstruction
*);
127 void emitSELP(const Instruction
*);
129 void emitTEXBAR(const Instruction
*);
130 void emitTEX(const TexInstruction
*);
131 void emitTEXCSAA(const TexInstruction
*);
132 void emitTXQ(const TexInstruction
*);
134 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
136 void emitFlow(const Instruction
*);
137 void emitBAR(const Instruction
*);
139 void emitSUCLAMPMode(uint16_t);
140 void emitSUCalc(Instruction
*);
141 void emitSULDGB(const TexInstruction
*);
142 void emitSUSTGx(const TexInstruction
*);
144 void emitSULDB(const TexInstruction
*);
145 void emitSUSTx(const TexInstruction
*);
146 void emitSULEA(const TexInstruction
*);
148 void emitVSHL(const Instruction
*);
149 void emitVectorSubOp(const Instruction
*);
151 void emitPIXLD(const Instruction
*);
153 void emitVOTE(const Instruction
*);
155 inline void defId(const ValueDef
&, const int pos
);
156 inline void defId(const Instruction
*, int d
, const int pos
);
157 inline void srcId(const ValueRef
&, const int pos
);
158 inline void srcId(const ValueRef
*, const int pos
);
159 inline void srcId(const Instruction
*, int s
, const int pos
);
160 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
162 inline bool isLIMM(const ValueRef
&, DataType ty
);
165 // for better visibility
166 #define HEX64(h, l) 0x##h##l##ULL
168 #define SDATA(a) ((a).rep()->reg.data)
169 #define DDATA(a) ((a).rep()->reg.data)
171 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
173 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
176 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
178 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
181 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
183 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
184 code
[pos
/ 32] |= r
<< (pos
% 32);
188 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
190 const uint32_t offset
= SDATA(src
).offset
>> shr
;
192 code
[pos
/ 32] |= offset
<< (pos
% 32);
193 if (pos
&& (pos
< 32))
194 code
[1] |= offset
>> (32 - pos
);
197 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
199 code
[pos
/ 32] |= (def
.get() && def
.getFile() != FILE_FLAGS
? DDATA(def
).id
: 63) << (pos
% 32);
202 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, const int pos
)
204 if (insn
->defExists(d
))
205 defId(insn
->def(d
), pos
);
207 code
[pos
/ 32] |= 63 << (pos
% 32);
210 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
212 const ImmediateValue
*imm
= ref
.get()->asImm();
214 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
218 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
221 case ROUND_M
: code
[1] |= 1 << 23; break;
222 case ROUND_P
: code
[1] |= 2 << 23; break;
223 case ROUND_Z
: code
[1] |= 3 << 23; break;
225 assert(insn
->rnd
== ROUND_N
);
231 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
233 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
234 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
235 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
236 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
239 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
244 case CC_LT
: val
= 0x1; break;
245 case CC_LTU
: val
= 0x9; break;
246 case CC_EQ
: val
= 0x2; break;
247 case CC_EQU
: val
= 0xa; break;
248 case CC_LE
: val
= 0x3; break;
249 case CC_LEU
: val
= 0xb; break;
250 case CC_GT
: val
= 0x4; break;
251 case CC_GTU
: val
= 0xc; break;
252 case CC_NE
: val
= 0x5; break;
253 case CC_NEU
: val
= 0xd; break;
254 case CC_GE
: val
= 0x6; break;
255 case CC_GEU
: val
= 0xe; break;
256 case CC_TR
: val
= 0xf; break;
257 case CC_FL
: val
= 0x0; break;
259 case CC_A
: val
= 0x14; break;
260 case CC_NA
: val
= 0x13; break;
261 case CC_S
: val
= 0x15; break;
262 case CC_NS
: val
= 0x12; break;
263 case CC_C
: val
= 0x16; break;
264 case CC_NC
: val
= 0x11; break;
265 case CC_O
: val
= 0x17; break;
266 case CC_NO
: val
= 0x10; break;
270 assert(!"invalid condition code");
273 code
[pos
/ 32] |= val
<< (pos
% 32);
277 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
279 if (i
->predSrc
>= 0) {
280 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
281 srcId(i
->src(i
->predSrc
), 10);
282 if (i
->cc
== CC_NOT_P
)
283 code
[0] |= 0x2000; // negate
290 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
292 switch (src
.getFile()) {
293 case FILE_MEMORY_GLOBAL
:
294 srcAddr32(src
, 26, 0);
296 case FILE_MEMORY_LOCAL
:
297 case FILE_MEMORY_SHARED
:
301 assert(src
.getFile() == FILE_MEMORY_CONST
);
308 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
310 Symbol
*sym
= src
.get()->asSym();
314 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
315 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
319 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
321 Symbol
*sym
= src
.get()->asSym();
325 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
326 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
330 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
332 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
336 u32
= imm
->reg
.data
.u32
;
338 if ((code
[0] & 0xf) == 0x1) {
340 uint64_t u64
= imm
->reg
.data
.u64
;
341 assert(!(u64
& 0x00000fffffffffffULL
));
342 assert(!(code
[1] & 0xc000));
343 code
[0] |= ((u64
>> 44) & 0x3f) << 26;
344 code
[1] |= 0xc000 | (u64
>> 50);
346 if ((code
[0] & 0xf) == 0x2) {
348 code
[0] |= (u32
& 0x3f) << 26;
351 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
353 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
354 assert(!(code
[1] & 0xc000));
356 code
[0] |= (u32
& 0x3f) << 26;
357 code
[1] |= 0xc000 | (u32
>> 6);
360 assert(!(u32
& 0x00000fff));
361 assert(!(code
[1] & 0xc000));
362 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
363 code
[1] |= 0xc000 | (u32
>> 18);
367 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
369 const ImmediateValue
*imm
= ref
.get()->asImm();
371 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
373 assert(s8
== imm
->reg
.data
.s32
);
375 code
[0] |= (s8
& 0x3f) << 26;
376 code
[0] |= (s8
>> 6) << 8;
379 void CodeEmitterNVC0::setPDSTL(const Instruction
*i
, const int d
)
381 assert(d
< 0 || (i
->defExists(d
) && i
->def(d
).getFile() == FILE_PREDICATE
));
383 uint32_t pred
= d
>= 0 ? DDATA(i
->def(d
)).id
: 7;
385 code
[0] |= (pred
& 3) << 8;
386 code
[1] |= (pred
& 4) << (26 - 2);
390 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
397 defId(i
->def(0), 14);
400 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
403 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
404 switch (i
->getSrc(s
)->reg
.file
) {
405 case FILE_MEMORY_CONST
:
406 assert(!(code
[1] & 0xc000));
407 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
408 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
409 setAddress16(i
->src(s
));
413 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
414 assert(!(code
[1] & 0xc000));
418 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
420 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
423 if (i
->op
== OP_SELP
) {
424 // OP_SELP is used to implement shared+atomics on Fermi.
425 assert(s
== 2 && i
->src(s
).getFile() == FILE_PREDICATE
);
426 srcId(i
->src(s
), 49);
428 // ignore here, can be predicate or flags, but must not be address
435 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
442 defId(i
->def(0), 14);
444 switch (i
->src(0).getFile()) {
445 case FILE_MEMORY_CONST
:
446 assert(!(code
[1] & 0xc000));
447 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
448 setAddress16(i
->src(0));
451 assert(!(code
[1] & 0xc000));
455 srcId(i
->src(0), 26);
458 // ignore here, can be predicate or flags, but must not be address
464 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
469 if (opc
== 0x0d || opc
== 0x0e)
472 defId(i
->def(0), 14);
473 srcId(i
->src(0), 20);
475 assert(pred
|| (i
->predSrc
< 0));
479 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
480 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
481 assert(!(code
[0] & (0x300 >> ss2a
)));
482 switch (i
->src(s
).get()->reg
.fileIndex
) {
483 case 0: code
[0] |= 0x100 >> ss2a
; break;
484 case 1: code
[0] |= 0x200 >> ss2a
; break;
485 case 16: code
[0] |= 0x300 >> ss2a
; break;
487 ERROR("invalid c[] space for short form\n");
491 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
493 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
495 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
497 setImmediateS8(i
->src(s
));
499 if (i
->src(s
).getFile() == FILE_GPR
) {
500 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
506 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
508 if (src
.getFile() == FILE_MEMORY_CONST
) {
509 switch (src
.get()->reg
.fileIndex
) {
510 case 0: code
[0] |= 0x100; break;
511 case 1: code
[0] |= 0x200; break;
512 case 16: code
[0] |= 0x300; break;
514 assert(!"unsupported file index for short op");
517 srcAddr32(src
, 20, 2);
520 assert(src
.getFile() == FILE_GPR
);
525 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
527 code
[0] = 0x000001e4;
528 code
[1] = 0x40000000;
533 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
535 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
537 if (i
->encSize
== 8) {
538 if (isLIMM(i
->src(1), TYPE_F32
)) {
539 emitForm_A(i
, HEX64(20000000, 00000002));
541 emitForm_A(i
, HEX64(30000000, 00000000));
543 if (i
->src(2).mod
.neg())
560 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
561 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
569 CodeEmitterNVC0::emitDMAD(const Instruction
*i
)
571 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
573 emitForm_A(i
, HEX64(20000000, 00000001));
575 if (i
->src(2).mod
.neg())
583 assert(!i
->saturate
);
588 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
590 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
592 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
594 if (i
->encSize
== 8) {
595 if (isLIMM(i
->src(1), TYPE_F32
)) {
596 assert(i
->postFactor
== 0); // constant folded, hopefully
597 emitForm_A(i
, HEX64(30000000, 00000002));
599 emitForm_A(i
, HEX64(58000000, 00000000));
601 code
[1] |= ((i
->postFactor
> 0) ?
602 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
605 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
616 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
617 emitForm_S(i
, 0xa8, true);
622 CodeEmitterNVC0::emitDMUL(const Instruction
*i
)
624 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
626 emitForm_A(i
, HEX64(50000000, 00000001));
632 assert(!i
->saturate
);
635 assert(!i
->postFactor
);
639 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
641 if (i
->encSize
== 8) {
642 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
643 emitForm_A(i
, HEX64(10000000, 00000002));
645 emitForm_A(i
, HEX64(50000000, 00000003));
647 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
649 if (i
->sType
== TYPE_S32
)
651 if (i
->dType
== TYPE_S32
)
654 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
656 if (i
->sType
== TYPE_S32
)
662 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
664 if (i
->encSize
== 8) {
665 if (isLIMM(i
->src(1), TYPE_F32
)) {
666 assert(!i
->saturate
);
667 emitForm_A(i
, HEX64(28000000, 00000002));
669 code
[0] |= i
->src(0).mod
.abs() << 7;
670 code
[0] |= i
->src(0).mod
.neg() << 9;
672 if (i
->src(1).mod
.abs())
673 code
[1] &= 0xfdffffff;
674 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
675 code
[1] ^= 0x02000000;
677 emitForm_A(i
, HEX64(50000000, 00000000));
684 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
689 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
690 !i
->src(0).mod
.abs() &&
691 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
693 emitForm_S(i
, 0x49, true);
695 if (i
->src(0).mod
.neg())
701 CodeEmitterNVC0::emitDADD(const Instruction
*i
)
703 assert(i
->encSize
== 8);
704 emitForm_A(i
, HEX64(48000000, 00000001));
706 assert(!i
->saturate
);
714 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
718 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
720 if (i
->src(0).mod
.neg())
722 if (i
->src(1).mod
.neg())
727 assert(addOp
!= 0x300); // would be add-plus-one
729 if (i
->encSize
== 8) {
730 if (isLIMM(i
->src(1), TYPE_U32
)) {
731 emitForm_A(i
, HEX64(08000000, 00000002));
732 if (i
->flagsDef
>= 0)
733 code
[1] |= 1 << 26; // write carry
735 emitForm_A(i
, HEX64(48000000, 00000003));
736 if (i
->flagsDef
>= 0)
737 code
[1] |= 1 << 16; // write carry
743 if (i
->flagsSrc
>= 0) // add carry
746 assert(!(addOp
& 0x100));
747 emitForm_S(i
, (addOp
>> 3) |
748 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
753 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
756 i
->src(2).mod
.neg() | ((i
->src(0).mod
.neg() ^ i
->src(1).mod
.neg()) << 1);
758 assert(i
->encSize
== 8);
759 emitForm_A(i
, HEX64(20000000, 00000003));
762 code
[0] |= addOp
<< 8;
764 if (isSignedType(i
->dType
))
766 if (isSignedType(i
->sType
))
769 code
[1] |= i
->saturate
<< 24;
771 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
772 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
774 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
779 CodeEmitterNVC0::emitSHLADD(const Instruction
*i
)
781 uint8_t addOp
= (i
->src(0).mod
.neg() << 1) | i
->src(2).mod
.neg();
782 const ImmediateValue
*imm
= i
->src(1).get()->asImm();
785 code
[0] = 0x00000003;
786 code
[1] = 0x40000000 | addOp
<< 23;
790 defId(i
->def(0), 14);
791 srcId(i
->src(0), 20);
793 if (i
->flagsDef
>= 0)
796 assert(!(imm
->reg
.data
.u32
& 0xffffffe0));
797 code
[0] |= imm
->reg
.data
.u32
<< 5;
799 switch (i
->src(2).getFile()) {
801 srcId(i
->src(2), 26);
803 case FILE_MEMORY_CONST
:
805 code
[1] |= i
->getSrc(2)->reg
.fileIndex
<< 10;
806 setAddress16(i
->src(2));
812 assert(!"bad src2 file");
818 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
820 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
822 emitForm_A(i
, HEX64(00000000, 00000003));
824 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
825 code
[1] |= 0x01800000;
827 code
[0] |= (i
->subOp
& 0x00f) << 7;
828 code
[0] |= (i
->subOp
& 0x0f0) << 1;
829 code
[0] |= (i
->subOp
& 0x100) >> 3;
830 code
[0] |= (i
->subOp
& 0x200) >> 2;
831 code
[1] |= (i
->subOp
& 0xc00) << 13;
834 if (i
->flagsDef
>= 0)
839 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
841 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
842 assert(i
->encSize
== 8);
844 emitForm_A(i
, HEX64(38000000, 00000003));
846 if (i
->dType
== TYPE_S32
)
851 CodeEmitterNVC0::emitNOT(Instruction
*i
)
853 assert(i
->encSize
== 8);
854 i
->setSrc(1, i
->src(0));
855 emitForm_A(i
, HEX64(68000000, 000001c3
));
859 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
861 if (i
->def(0).getFile() == FILE_PREDICATE
) {
862 code
[0] = 0x00000004 | (subOp
<< 30);
863 code
[1] = 0x0c000000;
867 defId(i
->def(0), 17);
868 srcId(i
->src(0), 20);
869 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
870 srcId(i
->src(1), 26);
871 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
873 if (i
->defExists(1)) {
874 defId(i
->def(1), 14);
879 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
880 code
[1] |= subOp
<< 21;
881 srcId(i
->src(2), 49);
882 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[1] |= 1 << 20;
884 code
[1] |= 0x000e0000;
887 if (i
->encSize
== 8) {
888 if (isLIMM(i
->src(1), TYPE_U32
)) {
889 emitForm_A(i
, HEX64(38000000, 00000002));
891 if (i
->flagsDef
>= 0)
894 emitForm_A(i
, HEX64(68000000, 00000003));
896 if (i
->flagsDef
>= 0)
899 code
[0] |= subOp
<< 6;
901 if (i
->flagsSrc
>= 0) // carry
904 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
905 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
907 emitForm_S(i
, (subOp
<< 5) |
908 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
913 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
915 emitForm_A(i
, HEX64(54000000, 00000004));
917 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
918 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
922 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
924 emitForm_A(i
, HEX64(28000000, 00000003));
928 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
930 emitForm_A(i
, HEX64(70000000, 00000003));
932 if (i
->dType
== TYPE_S32
)
934 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
939 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
941 emitForm_B(i
, HEX64(78000000, 00000003));
943 if (i
->dType
== TYPE_S32
)
945 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
947 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
952 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
954 emitForm_A(i
, HEX64(24000000, 00000004));
956 code
[0] |= i
->subOp
<< 5;
960 CodeEmitterNVC0::emitShift(const Instruction
*i
)
962 if (i
->op
== OP_SHR
) {
963 emitForm_A(i
, HEX64(58000000, 00000003)
964 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
966 emitForm_A(i
, HEX64(60000000, 00000003));
969 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
974 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
976 if (i
->encSize
== 8) {
977 emitForm_B(i
, HEX64(60000000, 00000000));
979 if (i
->op
== OP_PREEX2
)
982 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
983 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
985 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
990 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
992 if (i
->encSize
== 8) {
993 code
[0] = 0x00000000 | (subOp
<< 26);
994 code
[1] = 0xc8000000;
998 defId(i
->def(0), 14);
999 srcId(i
->src(0), 20);
1001 assert(i
->src(0).getFile() == FILE_GPR
);
1003 if (i
->saturate
) code
[0] |= 1 << 5;
1005 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
1006 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
1008 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
1010 assert(!i
->src(0).mod
.neg());
1011 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
1016 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
1020 assert(i
->encSize
== 8);
1022 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
1027 if (!isFloatType(i
->dType
)) {
1028 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
1029 op
|= i
->subOp
<< 6;
1031 if (i
->dType
== TYPE_F64
)
1037 if (i
->flagsDef
>= 0)
1042 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
1045 case ROUND_M
: code
[1] |= 1 << 17; break;
1046 case ROUND_P
: code
[1] |= 2 << 17; break;
1047 case ROUND_Z
: code
[1] |= 3 << 17; break;
1048 case ROUND_NI
: code
[0] |= 1 << 7; break;
1049 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
1050 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
1051 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
1052 case ROUND_N
: break;
1054 assert(!"invalid round mode");
1060 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
1064 case ROUND_MI
: code
[0] |= 1 << 16; break;
1066 case ROUND_PI
: code
[0] |= 2 << 16; break;
1068 case ROUND_ZI
: code
[0] |= 3 << 16; break;
1075 CodeEmitterNVC0::emitCVT(Instruction
*i
)
1077 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
1081 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
1082 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
1083 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
1088 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
1089 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
1090 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
1092 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
1097 if (i
->encSize
== 8) {
1098 emitForm_B(i
, HEX64(10000000, 00000004));
1102 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
1103 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
1104 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
1106 // for 8/16 source types, the byte/word is in subOp. word 1 is
1107 // represented as 2.
1108 if (!isFloatType(i
->sType
))
1109 code
[1] |= i
->subOp
<< 0x17;
1111 code
[1] |= i
->subOp
<< 0x18;
1117 if (neg
&& i
->op
!= OP_ABS
)
1123 if (isSignedIntType(dType
))
1125 if (isSignedIntType(i
->sType
))
1128 if (isFloatType(dType
)) {
1129 if (!isFloatType(i
->sType
))
1130 code
[1] |= 0x08000000;
1132 if (isFloatType(i
->sType
))
1133 code
[1] |= 0x04000000;
1135 code
[1] |= 0x0c000000;
1138 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
1141 if (isFloatType(dType
)) {
1142 if (isFloatType(i
->sType
))
1145 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1147 assert(isFloatType(i
->sType
));
1149 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1152 if (neg
) code
[0] |= 1 << 16;
1153 if (sat
) code
[0] |= 1 << 18;
1154 if (abs
) code
[0] |= 1 << 19;
1161 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1166 if (i
->sType
== TYPE_F64
)
1169 if (!isFloatType(i
->sType
))
1172 if (isSignedIntType(i
->sType
))
1174 if (isFloatType(i
->dType
)) {
1175 if (isFloatType(i
->sType
))
1182 case OP_SET_AND
: hi
= 0x10000000; break;
1183 case OP_SET_OR
: hi
= 0x10200000; break;
1184 case OP_SET_XOR
: hi
= 0x10400000; break;
1189 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1191 if (i
->op
!= OP_SET
)
1192 srcId(i
->src(2), 32 + 17);
1194 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1195 if (i
->sType
== TYPE_F32
)
1196 code
[1] += 0x10000000;
1198 code
[1] += 0x08000000;
1200 code
[0] &= ~0xfc000;
1201 defId(i
->def(0), 17);
1202 if (i
->defExists(1))
1203 defId(i
->def(1), 14);
1210 if (i
->flagsSrc
>= 0)
1213 emitCondCode(i
->setCond
, 32 + 23);
1218 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1224 op
= HEX64(30000000, 00000023);
1227 op
= HEX64(30000000, 00000003);
1230 op
= HEX64(38000000, 00000000);
1233 assert(!"invalid type for SLCT");
1239 CondCode cc
= i
->setCond
;
1241 if (i
->src(2).mod
.neg())
1242 cc
= reverseCondCode(cc
);
1244 emitCondCode(cc
, 32 + 23);
1251 selpFlip(const FixupEntry
*entry
, uint32_t *code
, const FixupData
& data
)
1253 int loc
= entry
->loc
;
1254 if (data
.force_persample_interp
)
1255 code
[loc
+ 1] |= 1 << 20;
1257 code
[loc
+ 1] &= ~(1 << 20);
1260 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1262 emitForm_A(i
, HEX64(20000000, 00000004));
1264 if (i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1267 if (i
->subOp
== 1) {
1268 addInterp(0, 0, selpFlip
);
1272 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1274 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1275 code
[1] = 0xf0000000;
1277 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1280 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1282 code
[0] = 0x00000086;
1283 code
[1] = 0xd0000000;
1285 code
[1] |= i
->tex
.r
;
1286 code
[1] |= i
->tex
.s
<< 8;
1288 if (i
->tex
.liveOnly
)
1291 defId(i
->def(0), 14);
1292 srcId(i
->src(0), 20);
1296 isNextIndependentTex(const TexInstruction
*i
)
1298 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1300 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1302 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1306 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1308 code
[0] = 0x00000006;
1310 if (isNextIndependentTex(i
))
1311 code
[0] |= 0x080; // t mode
1313 code
[0] |= 0x100; // p mode
1315 if (i
->tex
.liveOnly
)
1319 case OP_TEX
: code
[1] = 0x80000000; break;
1320 case OP_TXB
: code
[1] = 0x84000000; break;
1321 case OP_TXL
: code
[1] = 0x86000000; break;
1322 case OP_TXF
: code
[1] = 0x90000000; break;
1323 case OP_TXG
: code
[1] = 0xa0000000; break;
1324 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1325 case OP_TXD
: code
[1] = 0xe0000000; break;
1327 assert(!"invalid texture op");
1330 if (i
->op
== OP_TXF
) {
1331 if (!i
->tex
.levelZero
)
1332 code
[1] |= 0x02000000;
1334 if (i
->tex
.levelZero
) {
1335 code
[1] |= 0x02000000;
1338 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1341 defId(i
->def(0), 14);
1342 srcId(i
->src(0), 20);
1346 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1348 code
[1] |= i
->tex
.mask
<< 14;
1350 code
[1] |= i
->tex
.r
;
1351 code
[1] |= i
->tex
.s
<< 8;
1352 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1353 code
[1] |= 1 << 18; // in 1st source (with array index)
1356 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1357 if (i
->tex
.target
.isCube())
1359 if (i
->tex
.target
.isArray())
1361 if (i
->tex
.target
.isShadow())
1364 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1366 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1368 if (i
->op
== OP_TXL
)
1369 code
[1] &= ~(1 << 26);
1371 if (i
->op
== OP_TXF
)
1372 code
[1] &= ~(1 << 25);
1374 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1375 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1378 if (i
->tex
.useOffsets
== 1)
1380 if (i
->tex
.useOffsets
== 4)
1387 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1389 code
[0] = 0x00000086;
1390 code
[1] = 0xc0000000;
1392 switch (i
->tex
.query
) {
1393 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1394 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1395 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1396 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1397 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1398 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1400 assert(!"invalid texture query");
1404 code
[1] |= i
->tex
.mask
<< 14;
1406 code
[1] |= i
->tex
.r
;
1407 code
[1] |= i
->tex
.s
<< 8;
1408 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1411 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1413 defId(i
->def(0), 14);
1414 srcId(i
->src(0), 20);
1421 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1423 code
[0] = 0x00000200 | (laneMask
<< 6); // dall
1424 code
[1] = 0x48000000 | qOp
;
1426 defId(i
->def(0), 14);
1427 srcId(i
->src(0), 20);
1428 srcId((i
->srcExists(1) && i
->predSrc
!= 1) ? i
->src(1) : i
->src(0), 26);
1434 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1436 const FlowInstruction
*f
= i
->asFlow();
1438 unsigned mask
; // bit 0: predicate, bit 1: target
1440 code
[0] = 0x00000007;
1444 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1445 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1450 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1452 code
[0] |= 0x4000; // indirect calls always use c[] source
1456 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1457 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1458 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1459 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1460 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1462 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1463 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1464 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1465 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1467 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1468 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1469 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1471 assert(!"invalid flow operation");
1477 if (i
->flagsSrc
< 0)
1490 if (code
[0] & 0x4000) {
1491 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1492 setAddress16(i
->src(0));
1493 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1494 if (f
->op
== OP_BRA
)
1495 srcId(f
->src(0).getIndirect(0), 20);
1501 if (f
->op
== OP_CALL
) {
1506 assert(f
->absolute
);
1507 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1508 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1509 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1511 assert(!f
->absolute
);
1512 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1513 code
[0] |= (pcRel
& 0x3f) << 26;
1514 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1518 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1519 if (writeIssueDelays
&& !(f
->target
.bb
->binPos
& 0x3f))
1521 // currently we don't want absolute branches
1522 assert(!f
->absolute
);
1523 code
[0] |= (pcRel
& 0x3f) << 26;
1524 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1529 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1531 Value
*rDef
= NULL
, *pDef
= NULL
;
1534 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1535 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1536 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1537 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1540 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1543 code
[1] = 0x50000000;
1545 code
[0] |= 63 << 14;
1551 if (i
->src(0).getFile() == FILE_GPR
) {
1552 srcId(i
->src(0), 20);
1554 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1556 code
[0] |= imm
->reg
.data
.u32
<< 20;
1561 if (i
->src(1).getFile() == FILE_GPR
) {
1562 srcId(i
->src(1), 26);
1564 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1566 assert(imm
->reg
.data
.u32
<= 0xfff);
1567 code
[0] |= imm
->reg
.data
.u32
<< 26;
1568 code
[1] |= imm
->reg
.data
.u32
>> 6;
1572 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1573 srcId(i
->src(2), 32 + 17);
1574 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1580 if (i
->defExists(0)) {
1581 if (i
->def(0).getFile() == FILE_GPR
)
1582 rDef
= i
->getDef(0);
1584 pDef
= i
->getDef(0);
1586 if (i
->defExists(1)) {
1587 if (i
->def(1).getFile() == FILE_GPR
)
1588 rDef
= i
->getDef(1);
1590 pDef
= i
->getDef(1);
1594 code
[0] &= ~(63 << 14);
1598 code
[1] &= ~(7 << 21);
1599 defId(pDef
, 32 + 21);
1604 CodeEmitterNVC0::emitAFETCH(const Instruction
*i
)
1606 code
[0] = 0x00000006;
1607 code
[1] = 0x0c000000 | (i
->src(0).get()->reg
.data
.offset
& 0x7ff);
1609 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1614 defId(i
->def(0), 14);
1615 srcId(i
->src(0).getIndirect(0), 20);
1619 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1621 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1623 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1624 code
[1] = 0x00000000 | (prim
>> 6);
1628 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1630 defId(i
->def(0), 14);
1635 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1637 code
[0] = 0x00000006;
1638 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1642 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1643 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1647 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1649 defId(i
->def(0), 14);
1650 srcId(i
->src(0).getIndirect(0), 20);
1651 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1655 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1657 unsigned int size
= typeSizeof(i
->dType
);
1659 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1660 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1662 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1669 assert(i
->src(1).getFile() == FILE_GPR
);
1671 srcId(i
->src(0).getIndirect(0), 20);
1672 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1673 srcId(i
->src(1), 26);
1677 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1679 code
[0] = 0x00000006;
1680 code
[1] = 0x1c000000;
1684 defId(i
->def(0), 14); // new secret address
1685 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1687 assert(i
->src(0).getFile() == FILE_GPR
);
1689 if (i
->op
== OP_EMIT
)
1691 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1695 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1696 unsigned int stream
= SDATA(i
->src(1)).u32
;
1700 code
[0] |= stream
<< 26;
1705 srcId(i
->src(1), 26);
1710 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1712 if (i
->encSize
== 8) {
1713 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1715 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1717 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1722 interpApply(const FixupEntry
*entry
, uint32_t *code
, const FixupData
& data
)
1724 int ipa
= entry
->ipa
;
1725 int reg
= entry
->reg
;
1726 int loc
= entry
->loc
;
1728 if (data
.flatshade
&&
1729 (ipa
& NV50_IR_INTERP_MODE_MASK
) == NV50_IR_INTERP_SC
) {
1730 ipa
= NV50_IR_INTERP_FLAT
;
1732 } else if (data
.force_persample_interp
&&
1733 (ipa
& NV50_IR_INTERP_SAMPLE_MASK
) == NV50_IR_INTERP_DEFAULT
&&
1734 (ipa
& NV50_IR_INTERP_MODE_MASK
) != NV50_IR_INTERP_FLAT
) {
1735 ipa
|= NV50_IR_INTERP_CENTROID
;
1737 code
[loc
+ 0] &= ~(0xf << 6);
1738 code
[loc
+ 0] |= ipa
<< 6;
1739 code
[loc
+ 0] &= ~(0x3f << 26);
1740 code
[loc
+ 0] |= reg
<< 26;
1744 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1746 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1748 if (i
->encSize
== 8) {
1749 code
[0] = 0x00000000;
1750 code
[1] = 0xc0000000 | (base
& 0xffff);
1755 if (i
->op
== OP_PINTERP
) {
1756 srcId(i
->src(1), 26);
1757 addInterp(i
->ipa
, SDATA(i
->src(1)).id
, interpApply
);
1759 code
[0] |= 0x3f << 26;
1760 addInterp(i
->ipa
, 0x3f, interpApply
);
1763 srcId(i
->src(0).getIndirect(0), 20);
1765 assert(i
->op
== OP_PINTERP
);
1766 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1767 srcId(i
->src(1), 20);
1772 defId(i
->def(0), 14);
1774 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1775 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1777 code
[1] |= 0x3f << 17;
1781 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1814 assert(!"invalid type");
1821 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1842 assert(!"invalid caching mode");
1849 uses64bitAddress(const Instruction
*ldst
)
1851 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1852 ldst
->src(0).isIndirect(0) &&
1853 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1857 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1861 switch (i
->src(0).getFile()) {
1862 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1863 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1864 case FILE_MEMORY_SHARED
:
1865 if (i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1866 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1875 assert(!"invalid memory file");
1879 code
[0] = 0x00000005;
1882 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
) {
1883 // Unlocked store on shared memory can fail.
1884 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
&&
1885 i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1886 assert(i
->defExists(0));
1891 setAddressByFile(i
->src(0));
1892 srcId(i
->src(1), 14);
1893 srcId(i
->src(0).getIndirect(0), 20);
1894 if (uses64bitAddress(i
))
1899 emitLoadStoreType(i
->dType
);
1900 emitCachingMode(i
->cache
);
1904 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1908 code
[0] = 0x00000005;
1910 switch (i
->src(0).getFile()) {
1911 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1912 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1913 case FILE_MEMORY_SHARED
:
1914 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1915 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1923 case FILE_MEMORY_CONST
:
1924 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1925 emitMOV(i
); // not sure if this is any better
1928 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1929 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1932 assert(!"invalid memory file");
1939 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
) {
1940 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1941 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
1944 } else if (i
->defExists(1)) { // r, p
1947 assert(!"Expected predicate dest for load locked");
1953 defId(i
->def(r
), 14);
1955 code
[0] |= 63 << 14;
1958 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1961 defId(i
->def(p
), 32 + 18);
1964 setAddressByFile(i
->src(0));
1965 srcId(i
->src(0).getIndirect(0), 20);
1966 if (uses64bitAddress(i
))
1971 emitLoadStoreType(i
->dType
);
1972 emitCachingMode(i
->cache
);
1976 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1978 switch (SDATA(ref
).sv
.sv
) {
1979 case SV_LANEID
: return 0x00;
1980 case SV_PHYSID
: return 0x03;
1981 case SV_VERTEX_COUNT
: return 0x10;
1982 case SV_INVOCATION_ID
: return 0x11;
1983 case SV_YDIR
: return 0x12;
1984 case SV_THREAD_KILL
: return 0x13;
1985 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1986 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1987 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1988 case SV_GRIDID
: return 0x2c;
1989 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1990 case SV_LBASE
: return 0x34;
1991 case SV_SBASE
: return 0x30;
1992 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1994 assert(!"no sreg for system value");
2000 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
2002 if (i
->def(0).getFile() == FILE_PREDICATE
) {
2003 if (i
->src(0).getFile() == FILE_GPR
) {
2004 code
[0] = 0xfc01c003;
2005 code
[1] = 0x1a8e0000;
2006 srcId(i
->src(0), 20);
2008 code
[0] = 0x0001c004;
2009 code
[1] = 0x0c0e0000;
2010 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
2012 if (!i
->getSrc(0)->reg
.data
.u32
)
2015 srcId(i
->src(0), 20);
2018 defId(i
->def(0), 17);
2021 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
2022 uint8_t sr
= getSRegEncoding(i
->src(0));
2024 if (i
->encSize
== 8) {
2025 code
[0] = 0x00000004 | (sr
<< 26);
2026 code
[1] = 0x2c000000;
2028 code
[0] = 0x40000008 | (sr
<< 20);
2030 defId(i
->def(0), 14);
2034 if (i
->encSize
== 8) {
2037 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
2038 opc
= HEX64(18000000, 000001e2
);
2040 if (i
->src(0).getFile() == FILE_PREDICATE
)
2041 opc
= HEX64(080e0000
, 1c000004
);
2043 opc
= HEX64(28000000, 00000004);
2045 if (i
->src(0).getFile() != FILE_PREDICATE
)
2046 opc
|= i
->lanes
<< 5;
2050 // Explicitly emit the predicate source as emitForm_B skips it.
2051 if (i
->src(0).getFile() == FILE_PREDICATE
)
2052 srcId(i
->src(0), 20);
2056 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
2057 imm
= SDATA(i
->src(0)).u32
;
2058 if (imm
& 0xfff00000) {
2059 assert(!(imm
& 0x000fffff));
2060 code
[0] = 0x00000318 | imm
;
2062 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
2063 code
[0] = 0x00000118 | (imm
<< 20);
2067 emitShortSrc2(i
->src(0));
2069 defId(i
->def(0), 14);
2076 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
2078 const bool hasDst
= i
->defExists(0);
2079 const bool casOrExch
=
2080 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
2081 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
2083 if (i
->dType
== TYPE_U64
) {
2085 case NV50_IR_SUBOP_ATOM_ADD
:
2088 code
[1] = 0x507e0000;
2090 code
[1] = 0x10000000;
2092 case NV50_IR_SUBOP_ATOM_EXCH
:
2094 code
[1] = 0x507e0000;
2096 case NV50_IR_SUBOP_ATOM_CAS
:
2098 code
[1] = 0x50000000;
2101 assert(!"invalid u64 red op");
2105 if (i
->dType
== TYPE_U32
) {
2107 case NV50_IR_SUBOP_ATOM_EXCH
:
2109 code
[1] = 0x507e0000;
2111 case NV50_IR_SUBOP_ATOM_CAS
:
2113 code
[1] = 0x50000000;
2116 code
[0] = 0x5 | (i
->subOp
<< 5);
2118 code
[1] = 0x507e0000;
2120 code
[1] = 0x10000000;
2124 if (i
->dType
== TYPE_S32
) {
2125 assert(i
->subOp
<= 2);
2126 code
[0] = 0x205 | (i
->subOp
<< 5);
2128 code
[1] = 0x587e0000;
2130 code
[1] = 0x18000000;
2132 if (i
->dType
== TYPE_F32
) {
2133 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
2136 code
[1] = 0x687e0000;
2138 code
[1] = 0x28000000;
2143 srcId(i
->src(1), 14);
2146 defId(i
->def(0), 32 + 11);
2149 code
[1] |= 63 << 11;
2151 if (hasDst
|| casOrExch
) {
2152 const int32_t offset
= SDATA(i
->src(0)).offset
;
2153 assert(offset
< 0x80000 && offset
>= -0x80000);
2154 code
[0] |= offset
<< 26;
2155 code
[1] |= (offset
& 0x1ffc0) >> 6;
2156 code
[1] |= (offset
& 0xe0000) << 6;
2158 srcAddr32(i
->src(0), 26, 0);
2160 if (i
->getIndirect(0, 0)) {
2161 srcId(i
->getIndirect(0, 0), 20);
2162 if (i
->getIndirect(0, 0)->reg
.size
== 8)
2165 code
[0] |= 63 << 20;
2168 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
2169 assert(i
->src(1).getSize() == 2 * typeSizeof(i
->sType
));
2170 code
[1] |= (SDATA(i
->src(1)).id
+ 1) << 17;
2175 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
2177 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
2178 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
2179 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
2182 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
2185 code
[1] = 0xe0000000;
2191 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
2193 code
[0] = 0x00000005 | (i
->subOp
<< 5);
2195 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
2196 code
[1] = 0x98000000;
2197 srcAddr32(i
->src(0), 28, 2);
2199 code
[1] = 0xd0000000;
2200 setAddress24(i
->src(0));
2202 if (uses64bitAddress(i
))
2204 srcId(i
->src(0).getIndirect(0), 20);
2212 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
2215 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
2216 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
2217 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
2218 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
2219 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
2220 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
2221 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
2222 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
2223 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
2224 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
2225 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
2226 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
2227 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
2228 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
2229 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
2230 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
2235 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
2240 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
2242 ImmediateValue
*imm
= NULL
;
2245 if (i
->srcExists(2)) {
2246 imm
= i
->getSrc(2)->asImm();
2248 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
2252 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
2253 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
2254 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
2261 if (i
->op
== OP_SUCLAMP
) {
2262 if (i
->dType
== TYPE_S32
)
2264 emitSUCLAMPMode(i
->subOp
);
2267 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2270 if (i
->op
!= OP_SUEAU
) {
2271 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2272 code
[0] |= 63 << 14;
2273 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2275 if (i
->defExists(1)) { // r, p
2276 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2277 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2283 assert(i
->op
== OP_SUCLAMP
);
2285 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2290 CodeEmitterNVC0::emitSUGType(DataType ty
)
2293 case TYPE_S32
: code
[1] |= 1 << 13; break;
2294 case TYPE_U8
: code
[1] |= 2 << 13; break;
2295 case TYPE_S8
: code
[1] |= 3 << 13; break;
2297 assert(ty
== TYPE_U32
);
2303 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2305 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2307 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2308 assert(offset
== (offset
& 0xfffc));
2311 code
[0] |= offset
<< 24;
2312 code
[1] |= offset
>> 8;
2313 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2317 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2319 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2320 code
[1] |= 0x7 << 17;
2322 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2324 srcId(i
->src(s
), 32 + 17);
2329 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2332 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2334 emitLoadStoreType(i
->dType
);
2335 emitSUGType(i
->sType
);
2336 emitCachingMode(i
->cache
);
2339 defId(i
->def(0), 14); // destination
2340 srcId(i
->src(0), 20); // address
2342 if (i
->src(1).getFile() == FILE_GPR
)
2343 srcId(i
->src(1), 26);
2350 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2353 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2355 if (i
->op
== OP_SUSTP
)
2356 code
[1] |= i
->tex
.mask
<< 22;
2358 emitLoadStoreType(i
->dType
);
2359 emitSUGType(i
->sType
);
2360 emitCachingMode(i
->cache
);
2363 srcId(i
->src(0), 20); // address
2365 if (i
->src(1).getFile() == FILE_GPR
)
2366 srcId(i
->src(1), 26);
2369 srcId(i
->src(3), 14); // values
2374 CodeEmitterNVC0::emitSUAddr(const TexInstruction
*i
)
2376 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2378 if (i
->tex
.rIndirectSrc
< 0) {
2379 code
[1] |= 0x00004000;
2380 code
[0] |= i
->tex
.r
<< 26;
2382 srcId(i
, i
->tex
.rIndirectSrc
, 26);
2387 CodeEmitterNVC0::emitSUDim(const TexInstruction
*i
)
2389 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2391 code
[1] |= (i
->tex
.target
.getDim() - 1) << 12;
2392 if (i
->tex
.target
.isArray() || i
->tex
.target
.isCube() ||
2393 i
->tex
.target
.getDim() == 3) {
2394 // use e2d mode for 3-dim images, arrays and cubes.
2398 srcId(i
->src(0), 20);
2402 CodeEmitterNVC0::emitSULEA(const TexInstruction
*i
)
2404 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2407 code
[1] = 0xf0000000;
2410 emitLoadStoreType(i
->sType
);
2412 defId(i
->def(0), 14);
2414 if (i
->defExists(1)) {
2415 defId(i
->def(1), 32 + 22);
2425 CodeEmitterNVC0::emitSULDB(const TexInstruction
*i
)
2427 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2430 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2433 emitLoadStoreType(i
->dType
);
2435 defId(i
->def(0), 14);
2437 emitCachingMode(i
->cache
);
2443 CodeEmitterNVC0::emitSUSTx(const TexInstruction
*i
)
2445 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2448 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2450 if (i
->op
== OP_SUSTP
)
2451 code
[1] |= i
->tex
.mask
<< 17;
2453 emitLoadStoreType(i
->dType
);
2457 srcId(i
->src(1), 14);
2459 emitCachingMode(i
->cache
);
2465 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2467 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2469 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2470 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2471 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2472 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2475 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2476 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2477 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2478 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2479 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2480 code
[1] |= (i
->mask
& 0x3) << 2;
2483 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2484 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2485 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2486 code
[1] |= (i
->mask
& 0x3) << 2;
2487 code
[1] |= (i
->mask
& 0xc) << 21;
2496 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2500 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2501 case 0: opc
|= 0xe8ULL
<< 56; break;
2502 case 1: opc
|= 0xb4ULL
<< 56; break;
2503 case 2: opc
|= 0x94ULL
<< 56; break;
2508 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2509 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2510 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2512 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2513 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2520 if (i
->flagsDef
>= 0)
2525 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2527 assert(i
->encSize
== 8);
2528 emitForm_A(i
, HEX64(10000000, 00000006));
2529 code
[0] |= i
->subOp
<< 5;
2530 code
[1] |= 0x00e00000;
2534 CodeEmitterNVC0::emitVOTE(const Instruction
*i
)
2536 assert(i
->src(0).getFile() == FILE_PREDICATE
);
2538 code
[0] = 0x00000004 | (i
->subOp
<< 5);
2539 code
[1] = 0x48000000;
2544 for (int d
= 0; i
->defExists(d
); d
++) {
2545 if (i
->def(d
).getFile() == FILE_PREDICATE
) {
2548 defId(i
->def(d
), 32 + 22);
2549 } else if (i
->def(d
).getFile() == FILE_GPR
) {
2552 defId(i
->def(d
), 14);
2554 assert(!"Unhandled def");
2558 code
[0] |= 63 << 14;
2561 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
2563 srcId(i
->src(0), 20);
2567 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2569 unsigned int size
= insn
->encSize
;
2571 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2574 if (!insn
->encSize
) {
2575 ERROR("skipping unencodable instruction: "); insn
->print();
2578 if (codeSize
+ size
> codeSizeLimit
) {
2579 ERROR("code emitter output buffer too small\n");
2583 if (writeIssueDelays
) {
2584 if (!(codeSize
& 0x3f)) {
2585 code
[0] = 0x00000007; // cf issue delay "instruction"
2586 code
[1] = 0x20000000;
2590 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2591 uint32_t *data
= code
- (id
* 2 + 2);
2593 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2596 data
[0] |= insn
->sched
<< 28;
2597 data
[1] |= insn
->sched
>> 4;
2599 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2603 // assert that instructions with multiple defs don't corrupt registers
2604 for (int d
= 0; insn
->defExists(d
); ++d
)
2605 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2642 if (insn
->dType
== TYPE_F64
)
2644 else if (isFloatType(insn
->dType
))
2650 if (insn
->dType
== TYPE_F64
)
2652 else if (isFloatType(insn
->dType
))
2659 if (insn
->dType
== TYPE_F64
)
2661 else if (isFloatType(insn
->dType
))
2676 emitLogicOp(insn
, 0);
2679 emitLogicOp(insn
, 1);
2682 emitLogicOp(insn
, 2);
2692 emitSET(insn
->asCmp());
2698 emitSLCT(insn
->asCmp());
2713 if (insn
->def(0).getFile() == FILE_PREDICATE
||
2714 insn
->src(0).getFile() == FILE_PREDICATE
)
2720 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
2723 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
2748 emitTEX(insn
->asTex());
2751 emitTXQ(insn
->asTex());
2765 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2766 emitSULDGB(insn
->asTex());
2768 emitSULDB(insn
->asTex());
2772 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2773 emitSUSTGx(insn
->asTex());
2775 emitSUSTx(insn
->asTex());
2778 emitSULEA(insn
->asTex());
2800 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2803 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2806 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2848 ERROR("operation should have been eliminated");
2854 ERROR("operation should have been lowered\n");
2857 ERROR("unknown op: %u\n", insn
->op
);
2863 assert(insn
->encSize
== 8);
2866 code
+= insn
->encSize
/ 4;
2867 codeSize
+= insn
->encSize
;
2872 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2874 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2876 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2879 if (i
->ftz
|| i
->saturate
|| i
->join
)
2881 if (i
->rnd
!= ROUND_N
)
2883 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2886 if (i
->op
== OP_PINTERP
) {
2887 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2890 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2894 for (int s
= 0; i
->srcExists(s
); ++s
) {
2895 if (i
->src(s
).isIndirect(0))
2898 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2899 if (SDATA(i
->src(s
)).offset
>= 0x100)
2901 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2902 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2905 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2906 if (i
->dType
== TYPE_F32
) {
2907 if (SDATA(i
->src(s
)).u32
>= 0x100)
2910 if (SDATA(i
->src(s
)).u32
> 0xff)
2915 if (i
->op
== OP_CVT
)
2917 if (i
->src(s
).mod
!= Modifier(0)) {
2918 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2919 if (i
->op
!= OP_RSQ
)
2921 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2922 if (i
->op
!= OP_ADD
|| s
!= 0)
2930 // Simplified, erring on safe side.
2931 class SchedDataCalculator
: public Pass
2934 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2940 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2941 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2942 int tex
; // TEX to non-TEX delay 17 (0x11)
2943 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2944 int imul
; // integer MUL to MUL delay 3
2954 void rebase(const int base
)
2956 const int delta
= this->base
- base
;
2961 for (int i
= 0; i
< regs
; ++i
) {
2965 for (int i
= 0; i
< 8; ++i
) {
2972 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2982 memset(&rd
, 0, sizeof(rd
));
2983 memset(&wr
, 0, sizeof(wr
));
2984 memset(&res
, 0, sizeof(res
));
2987 int getLatest(const ScoreData
& d
) const
2990 for (int i
= 0; i
< regs
; ++i
)
2993 for (int i
= 0; i
< 8; ++i
)
3000 inline int getLatestRd() const
3002 return getLatest(rd
);
3004 inline int getLatestWr() const
3006 return getLatest(wr
);
3008 inline int getLatest() const
3010 const int a
= getLatestRd();
3011 const int b
= getLatestWr();
3013 int max
= MAX2(a
, b
);
3014 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
3015 max
= MAX2(res
.ld
[f
], max
);
3016 max
= MAX2(res
.st
[f
], max
);
3018 max
= MAX2(res
.sfu
, max
);
3019 max
= MAX2(res
.imul
, max
);
3020 max
= MAX2(res
.tex
, max
);
3023 void setMax(const RegScores
*that
)
3025 for (int i
= 0; i
< regs
; ++i
) {
3026 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
3027 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
3029 for (int i
= 0; i
< 8; ++i
) {
3030 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
3031 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
3033 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
3034 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
3036 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
3037 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
3038 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
3040 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
3041 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
3042 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
3044 void print(int cycle
)
3046 for (int i
= 0; i
< regs
; ++i
) {
3047 if (rd
.r
[i
] > cycle
)
3048 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
3049 if (wr
.r
[i
] > cycle
)
3050 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
3052 for (int i
= 0; i
< 8; ++i
) {
3053 if (rd
.p
[i
] > cycle
)
3054 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
3055 if (wr
.p
[i
] > cycle
)
3056 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
3059 INFO("rd $c @ %i\n", rd
.c
);
3061 INFO("wr $c @ %i\n", wr
.c
);
3062 if (res
.sfu
> cycle
)
3063 INFO("sfu @ %i\n", res
.sfu
);
3064 if (res
.imul
> cycle
)
3065 INFO("imul @ %i\n", res
.imul
);
3066 if (res
.tex
> cycle
)
3067 INFO("tex @ %i\n", res
.tex
);
3071 RegScores
*score
; // for current BB
3072 std::vector
<RegScores
> scoreBoards
;
3078 bool visit(Function
*);
3079 bool visit(BasicBlock
*);
3081 void commitInsn(const Instruction
*, int cycle
);
3082 int calcDelay(const Instruction
*, int cycle
) const;
3083 void setDelay(Instruction
*, int delay
, Instruction
*next
);
3085 void recordRd(const Value
*, const int ready
);
3086 void recordWr(const Value
*, const int ready
);
3087 void checkRd(const Value
*, int cycle
, int& delay
) const;
3088 void checkWr(const Value
*, int cycle
, int& delay
) const;
3090 int getCycles(const Instruction
*, int origDelay
) const;
3094 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
3096 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
3097 delay
= MAX2(delay
, 14);
3099 if (insn
->op
== OP_TEXBAR
) {
3100 // TODO: except if results not used before EXIT
3103 if (insn
->op
== OP_JOIN
|| insn
->join
) {
3106 if (delay
>= 0 || prevData
== 0x04 ||
3107 !next
|| !targ
->canDualIssue(insn
, next
)) {
3108 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
3109 if (prevOp
== OP_EXPORT
)
3110 insn
->sched
|= 0x40;
3112 insn
->sched
|= 0x20;
3114 insn
->sched
= 0x04; // dual-issue
3117 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
3118 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
3121 prevData
= insn
->sched
;
3125 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
3127 if (insn
->sched
& 0x80) {
3128 int c
= (insn
->sched
& 0x0f) * 2 + 1;
3129 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
3133 if (insn
->sched
& 0x60)
3134 return (insn
->sched
& 0x1f) + 1;
3135 return (insn
->sched
== 0x04) ? 0 : 32;
3139 SchedDataCalculator::visit(Function
*func
)
3141 int regs
= targ
->getFileSize(FILE_GPR
) + 1;
3142 scoreBoards
.resize(func
->cfg
.getSize());
3143 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
3144 scoreBoards
[i
].wipe(regs
);
3149 SchedDataCalculator::visit(BasicBlock
*bb
)
3152 Instruction
*next
= NULL
;
3158 score
= &scoreBoards
.at(bb
->getId());
3160 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
3161 // back branches will wait until all target dependencies are satisfied
3162 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
3164 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
3165 if (in
->getExit()) {
3166 if (prevData
!= 0x04)
3167 prevData
= in
->getExit()->sched
;
3168 prevOp
= in
->getExit()->op
;
3170 score
->setMax(&scoreBoards
.at(in
->getId()));
3172 if (bb
->cfg
.incidentCount() > 1)
3175 #ifdef NVC0_DEBUG_SCHED_DATA
3176 INFO("=== BB:%i initial scores\n", bb
->getId());
3177 score
->print(cycle
);
3180 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
3183 commitInsn(insn
, cycle
);
3184 int delay
= calcDelay(next
, cycle
);
3185 setDelay(insn
, delay
, next
);
3186 cycle
+= getCycles(insn
, delay
);
3188 #ifdef NVC0_DEBUG_SCHED_DATA
3189 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
3196 commitInsn(insn
, cycle
);
3200 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
3201 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
3203 if (ei
.getType() != Graph::Edge::BACK
) {
3204 // only test the first instruction of the outgoing block
3205 next
= out
->getEntry();
3207 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
3209 // wait until all dependencies are satisfied
3210 const int regsFree
= score
->getLatest();
3211 next
= out
->getFirst();
3212 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
3213 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
3214 c
+= getCycles(next
, bbDelay
);
3219 if (bb
->cfg
.outgoingCount() != 1)
3221 setDelay(insn
, bbDelay
, next
);
3222 cycle
+= getCycles(insn
, bbDelay
);
3224 score
->rebase(cycle
); // common base for initializing out blocks' scores
3228 #define NVE4_MAX_ISSUE_DELAY 0x1f
3230 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
3232 int delay
= 0, ready
= cycle
;
3234 for (int s
= 0; insn
->srcExists(s
); ++s
)
3235 checkRd(insn
->getSrc(s
), cycle
, delay
);
3236 // WAR & WAW don't seem to matter
3237 // for (int s = 0; insn->srcExists(s); ++s)
3238 // recordRd(insn->getSrc(s), cycle);
3240 switch (Target::getOpClass(insn
->op
)) {
3242 ready
= score
->res
.sfu
;
3245 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3246 ready
= score
->res
.imul
;
3248 case OPCLASS_TEXTURE
:
3249 ready
= score
->res
.tex
;
3252 ready
= score
->res
.ld
[insn
->src(0).getFile()];
3255 ready
= score
->res
.st
[insn
->src(0).getFile()];
3260 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
3261 ready
= MAX2(ready
, score
->res
.tex
);
3263 delay
= MAX2(delay
, ready
- cycle
);
3265 // if can issue next cycle, delay is 0, not 1
3266 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
3270 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
3272 const int ready
= cycle
+ targ
->getLatency(insn
);
3274 for (int d
= 0; insn
->defExists(d
); ++d
)
3275 recordWr(insn
->getDef(d
), ready
);
3276 // WAR & WAW don't seem to matter
3277 // for (int s = 0; insn->srcExists(s); ++s)
3278 // recordRd(insn->getSrc(s), cycle);
3280 switch (Target::getOpClass(insn
->op
)) {
3282 score
->res
.sfu
= cycle
+ 4;
3285 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3286 score
->res
.imul
= cycle
+ 4;
3288 case OPCLASS_TEXTURE
:
3289 score
->res
.tex
= cycle
+ 18;
3292 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
3294 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
3295 score
->res
.st
[insn
->src(0).getFile()] = ready
;
3298 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
3299 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
3302 if (insn
->op
== OP_TEXBAR
)
3303 score
->res
.tex
= cycle
;
3309 #ifdef NVC0_DEBUG_SCHED_DATA
3310 score
->print(cycle
);
3315 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
3320 switch (v
->reg
.file
) {
3323 b
= a
+ v
->reg
.size
/ 4;
3324 for (int r
= a
; r
< b
; ++r
)
3325 ready
= MAX2(ready
, score
->rd
.r
[r
]);
3327 case FILE_PREDICATE
:
3328 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
3331 ready
= MAX2(ready
, score
->rd
.c
);
3333 case FILE_SHADER_INPUT
:
3334 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
3335 case FILE_MEMORY_LOCAL
:
3336 case FILE_MEMORY_CONST
:
3337 case FILE_MEMORY_SHARED
:
3338 case FILE_MEMORY_GLOBAL
:
3339 case FILE_SYSTEM_VALUE
:
3340 // TODO: any restrictions here ?
3342 case FILE_IMMEDIATE
:
3349 delay
= MAX2(delay
, ready
- cycle
);
3353 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
3358 switch (v
->reg
.file
) {
3361 b
= a
+ v
->reg
.size
/ 4;
3362 for (int r
= a
; r
< b
; ++r
)
3363 ready
= MAX2(ready
, score
->wr
.r
[r
]);
3365 case FILE_PREDICATE
:
3366 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
3369 assert(v
->reg
.file
== FILE_FLAGS
);
3370 ready
= MAX2(ready
, score
->wr
.c
);
3374 delay
= MAX2(delay
, ready
- cycle
);
3378 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
3380 int a
= v
->reg
.data
.id
;
3382 if (v
->reg
.file
== FILE_GPR
) {
3383 int b
= a
+ v
->reg
.size
/ 4;
3384 for (int r
= a
; r
< b
; ++r
)
3385 score
->rd
.r
[r
] = ready
;
3387 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
3388 if (v
->reg
.file
== FILE_PREDICATE
) {
3389 score
->rd
.p
[a
] = ready
+ 4;
3391 assert(v
->reg
.file
== FILE_FLAGS
);
3392 score
->rd
.c
= ready
+ 4;
3397 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
3399 int a
= v
->reg
.data
.id
;
3401 if (v
->reg
.file
== FILE_GPR
) {
3402 int b
= a
+ v
->reg
.size
/ 4;
3403 for (int r
= a
; r
< b
; ++r
)
3404 score
->wr
.r
[r
] = ready
;
3406 if (v
->reg
.file
== FILE_PREDICATE
) {
3407 score
->wr
.p
[a
] = ready
;
3409 if (v
->reg
.file
== FILE_FLAGS
) {
3410 score
->wr
.c
= ready
;
3415 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
3417 SchedDataCalculator
sched(targ
);
3418 return sched
.run(func
, true, true);
3422 CodeEmitterNVC0::prepareEmission(Function
*func
)
3424 CodeEmitter::prepareEmission(func
);
3426 if (targ
->hasSWSched
)
3427 calculateSchedDataNVC0(targ
, func
);
3430 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3431 : CodeEmitter(target
),
3433 writeIssueDelays(target
->hasSWSched
)
3436 codeSize
= codeSizeLimit
= 0;
3441 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3443 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3444 emit
->setProgramType(type
);
3449 TargetNVC0::getCodeEmitter(Program::Type type
)
3451 if (chipset
>= NVISA_GK20A_CHIPSET
)
3452 return createCodeEmitterGK110(type
);
3453 return createCodeEmitterNVC0(type
);
3456 } // namespace nv50_ir