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 emitSHLADD(const Instruction
*a
);
105 void emitFMAD(const Instruction
*);
106 void emitDMAD(const Instruction
*);
107 void emitMADSP(const Instruction
*);
109 void emitNOT(Instruction
*);
110 void emitLogicOp(const Instruction
*, uint8_t subOp
);
111 void emitPOPC(const Instruction
*);
112 void emitINSBF(const Instruction
*);
113 void emitEXTBF(const Instruction
*);
114 void emitBFIND(const Instruction
*);
115 void emitPERMT(const Instruction
*);
116 void emitShift(const Instruction
*);
118 void emitSFnOp(const Instruction
*, uint8_t subOp
);
120 void emitCVT(Instruction
*);
121 void emitMINMAX(const Instruction
*);
122 void emitPreOp(const Instruction
*);
124 void emitSET(const CmpInstruction
*);
125 void emitSLCT(const CmpInstruction
*);
126 void emitSELP(const Instruction
*);
128 void emitTEXBAR(const Instruction
*);
129 void emitTEX(const TexInstruction
*);
130 void emitTEXCSAA(const TexInstruction
*);
131 void emitTXQ(const TexInstruction
*);
133 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
135 void emitFlow(const Instruction
*);
136 void emitBAR(const Instruction
*);
138 void emitSUCLAMPMode(uint16_t);
139 void emitSUCalc(Instruction
*);
140 void emitSULDGB(const TexInstruction
*);
141 void emitSUSTGx(const TexInstruction
*);
143 void emitSULDB(const TexInstruction
*);
144 void emitSUSTx(const TexInstruction
*);
145 void emitSULEA(const TexInstruction
*);
147 void emitVSHL(const Instruction
*);
148 void emitVectorSubOp(const Instruction
*);
150 void emitPIXLD(const Instruction
*);
152 void emitVOTE(const Instruction
*);
154 inline void defId(const ValueDef
&, const int pos
);
155 inline void defId(const Instruction
*, int d
, const int pos
);
156 inline void srcId(const ValueRef
&, const int pos
);
157 inline void srcId(const ValueRef
*, const int pos
);
158 inline void srcId(const Instruction
*, int s
, const int pos
);
159 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
161 inline bool isLIMM(const ValueRef
&, DataType ty
);
164 // for better visibility
165 #define HEX64(h, l) 0x##h##l##ULL
167 #define SDATA(a) ((a).rep()->reg.data)
168 #define DDATA(a) ((a).rep()->reg.data)
170 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
172 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
175 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
177 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
180 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
182 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
183 code
[pos
/ 32] |= r
<< (pos
% 32);
187 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
189 const uint32_t offset
= SDATA(src
).offset
>> shr
;
191 code
[pos
/ 32] |= offset
<< (pos
% 32);
192 if (pos
&& (pos
< 32))
193 code
[1] |= offset
>> (32 - pos
);
196 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
198 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
201 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
203 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
204 code
[pos
/ 32] |= r
<< (pos
% 32);
207 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
209 const ImmediateValue
*imm
= ref
.get()->asImm();
211 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
215 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
218 case ROUND_M
: code
[1] |= 1 << 23; break;
219 case ROUND_P
: code
[1] |= 2 << 23; break;
220 case ROUND_Z
: code
[1] |= 3 << 23; break;
222 assert(insn
->rnd
== ROUND_N
);
228 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
230 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
231 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
232 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
233 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
236 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
241 case CC_LT
: val
= 0x1; break;
242 case CC_LTU
: val
= 0x9; break;
243 case CC_EQ
: val
= 0x2; break;
244 case CC_EQU
: val
= 0xa; break;
245 case CC_LE
: val
= 0x3; break;
246 case CC_LEU
: val
= 0xb; break;
247 case CC_GT
: val
= 0x4; break;
248 case CC_GTU
: val
= 0xc; break;
249 case CC_NE
: val
= 0x5; break;
250 case CC_NEU
: val
= 0xd; break;
251 case CC_GE
: val
= 0x6; break;
252 case CC_GEU
: val
= 0xe; break;
253 case CC_TR
: val
= 0xf; break;
254 case CC_FL
: val
= 0x0; break;
256 case CC_A
: val
= 0x14; break;
257 case CC_NA
: val
= 0x13; break;
258 case CC_S
: val
= 0x15; break;
259 case CC_NS
: val
= 0x12; break;
260 case CC_C
: val
= 0x16; break;
261 case CC_NC
: val
= 0x11; break;
262 case CC_O
: val
= 0x17; break;
263 case CC_NO
: val
= 0x10; break;
267 assert(!"invalid condition code");
270 code
[pos
/ 32] |= val
<< (pos
% 32);
274 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
276 if (i
->predSrc
>= 0) {
277 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
278 srcId(i
->src(i
->predSrc
), 10);
279 if (i
->cc
== CC_NOT_P
)
280 code
[0] |= 0x2000; // negate
287 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
289 switch (src
.getFile()) {
290 case FILE_MEMORY_GLOBAL
:
291 srcAddr32(src
, 26, 0);
293 case FILE_MEMORY_LOCAL
:
294 case FILE_MEMORY_SHARED
:
298 assert(src
.getFile() == FILE_MEMORY_CONST
);
305 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
307 Symbol
*sym
= src
.get()->asSym();
311 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
312 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
316 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
318 Symbol
*sym
= src
.get()->asSym();
322 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
323 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
327 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
329 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
333 u32
= imm
->reg
.data
.u32
;
335 if ((code
[0] & 0xf) == 0x1) {
337 uint64_t u64
= imm
->reg
.data
.u64
;
338 assert(!(u64
& 0x00000fffffffffffULL
));
339 assert(!(code
[1] & 0xc000));
340 code
[0] |= ((u64
>> 44) & 0x3f) << 26;
341 code
[1] |= 0xc000 | (u64
>> 50);
343 if ((code
[0] & 0xf) == 0x2) {
345 code
[0] |= (u32
& 0x3f) << 26;
348 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
350 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
351 assert(!(code
[1] & 0xc000));
353 code
[0] |= (u32
& 0x3f) << 26;
354 code
[1] |= 0xc000 | (u32
>> 6);
357 assert(!(u32
& 0x00000fff));
358 assert(!(code
[1] & 0xc000));
359 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
360 code
[1] |= 0xc000 | (u32
>> 18);
364 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
366 const ImmediateValue
*imm
= ref
.get()->asImm();
368 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
370 assert(s8
== imm
->reg
.data
.s32
);
372 code
[0] |= (s8
& 0x3f) << 26;
373 code
[0] |= (s8
>> 6) << 8;
377 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
384 defId(i
->def(0), 14);
387 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
390 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
391 switch (i
->getSrc(s
)->reg
.file
) {
392 case FILE_MEMORY_CONST
:
393 assert(!(code
[1] & 0xc000));
394 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
395 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
396 setAddress16(i
->src(s
));
400 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
401 assert(!(code
[1] & 0xc000));
405 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
407 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
410 if (i
->op
== OP_SELP
) {
411 // OP_SELP is used to implement shared+atomics on Fermi.
412 assert(s
== 2 && i
->src(s
).getFile() == FILE_PREDICATE
);
413 srcId(i
->src(s
), 49);
415 // ignore here, can be predicate or flags, but must not be address
422 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
429 defId(i
->def(0), 14);
431 switch (i
->src(0).getFile()) {
432 case FILE_MEMORY_CONST
:
433 assert(!(code
[1] & 0xc000));
434 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
435 setAddress16(i
->src(0));
438 assert(!(code
[1] & 0xc000));
442 srcId(i
->src(0), 26);
445 // ignore here, can be predicate or flags, but must not be address
451 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
456 if (opc
== 0x0d || opc
== 0x0e)
459 defId(i
->def(0), 14);
460 srcId(i
->src(0), 20);
462 assert(pred
|| (i
->predSrc
< 0));
466 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
467 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
468 assert(!(code
[0] & (0x300 >> ss2a
)));
469 switch (i
->src(s
).get()->reg
.fileIndex
) {
470 case 0: code
[0] |= 0x100 >> ss2a
; break;
471 case 1: code
[0] |= 0x200 >> ss2a
; break;
472 case 16: code
[0] |= 0x300 >> ss2a
; break;
474 ERROR("invalid c[] space for short form\n");
478 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
480 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
482 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
484 setImmediateS8(i
->src(s
));
486 if (i
->src(s
).getFile() == FILE_GPR
) {
487 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
493 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
495 if (src
.getFile() == FILE_MEMORY_CONST
) {
496 switch (src
.get()->reg
.fileIndex
) {
497 case 0: code
[0] |= 0x100; break;
498 case 1: code
[0] |= 0x200; break;
499 case 16: code
[0] |= 0x300; break;
501 assert(!"unsupported file index for short op");
504 srcAddr32(src
, 20, 2);
507 assert(src
.getFile() == FILE_GPR
);
512 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
514 code
[0] = 0x000001e4;
515 code
[1] = 0x40000000;
520 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
522 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
524 if (i
->encSize
== 8) {
525 if (isLIMM(i
->src(1), TYPE_F32
)) {
526 emitForm_A(i
, HEX64(20000000, 00000002));
528 emitForm_A(i
, HEX64(30000000, 00000000));
530 if (i
->src(2).mod
.neg())
547 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
548 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
556 CodeEmitterNVC0::emitDMAD(const Instruction
*i
)
558 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
560 emitForm_A(i
, HEX64(20000000, 00000001));
562 if (i
->src(2).mod
.neg())
570 assert(!i
->saturate
);
575 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
577 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
579 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
581 if (i
->encSize
== 8) {
582 if (isLIMM(i
->src(1), TYPE_F32
)) {
583 assert(i
->postFactor
== 0); // constant folded, hopefully
584 emitForm_A(i
, HEX64(30000000, 00000002));
586 emitForm_A(i
, HEX64(58000000, 00000000));
588 code
[1] |= ((i
->postFactor
> 0) ?
589 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
592 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
603 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
604 emitForm_S(i
, 0xa8, true);
609 CodeEmitterNVC0::emitDMUL(const Instruction
*i
)
611 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
613 emitForm_A(i
, HEX64(50000000, 00000001));
619 assert(!i
->saturate
);
622 assert(!i
->postFactor
);
626 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
628 if (i
->encSize
== 8) {
629 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
630 emitForm_A(i
, HEX64(10000000, 00000002));
632 emitForm_A(i
, HEX64(50000000, 00000003));
634 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
636 if (i
->sType
== TYPE_S32
)
638 if (i
->dType
== TYPE_S32
)
641 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
643 if (i
->sType
== TYPE_S32
)
649 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
651 if (i
->encSize
== 8) {
652 if (isLIMM(i
->src(1), TYPE_F32
)) {
653 assert(!i
->saturate
);
654 emitForm_A(i
, HEX64(28000000, 00000002));
656 code
[0] |= i
->src(0).mod
.abs() << 7;
657 code
[0] |= i
->src(0).mod
.neg() << 9;
659 if (i
->src(1).mod
.abs())
660 code
[1] &= 0xfdffffff;
661 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
662 code
[1] ^= 0x02000000;
664 emitForm_A(i
, HEX64(50000000, 00000000));
671 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
676 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
677 !i
->src(0).mod
.abs() &&
678 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
680 emitForm_S(i
, 0x49, true);
682 if (i
->src(0).mod
.neg())
688 CodeEmitterNVC0::emitDADD(const Instruction
*i
)
690 assert(i
->encSize
== 8);
691 emitForm_A(i
, HEX64(48000000, 00000001));
693 assert(!i
->saturate
);
701 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
705 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
707 if (i
->src(0).mod
.neg())
709 if (i
->src(1).mod
.neg())
714 assert(addOp
!= 0x300); // would be add-plus-one
716 if (i
->encSize
== 8) {
717 if (isLIMM(i
->src(1), TYPE_U32
)) {
718 emitForm_A(i
, HEX64(08000000, 00000002));
720 code
[1] |= 1 << 26; // write carry
722 emitForm_A(i
, HEX64(48000000, 00000003));
724 code
[1] |= 1 << 16; // write carry
730 if (i
->flagsSrc
>= 0) // add carry
733 assert(!(addOp
& 0x100));
734 emitForm_S(i
, (addOp
>> 3) |
735 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
740 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
743 i
->src(2).mod
.neg() | ((i
->src(0).mod
.neg() ^ i
->src(1).mod
.neg()) << 1);
745 assert(i
->encSize
== 8);
746 emitForm_A(i
, HEX64(20000000, 00000003));
749 code
[0] |= addOp
<< 8;
751 if (isSignedType(i
->dType
))
753 if (isSignedType(i
->sType
))
756 code
[1] |= i
->saturate
<< 24;
758 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
759 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
761 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
766 CodeEmitterNVC0::emitSHLADD(const Instruction
*i
)
768 uint8_t addOp
= (i
->src(0).mod
.neg() << 1) | i
->src(2).mod
.neg();
769 const ImmediateValue
*imm
= i
->src(1).get()->asImm();
772 code
[0] = 0x00000003;
773 code
[1] = 0x40000000 | addOp
<< 23;
777 defId(i
->def(0), 14);
778 srcId(i
->src(0), 20);
780 if (i
->flagsDef
>= 0)
783 assert(!(imm
->reg
.data
.u32
& 0xffffffe0));
784 code
[0] |= imm
->reg
.data
.u32
<< 5;
786 switch (i
->src(2).getFile()) {
788 srcId(i
->src(2), 26);
790 case FILE_MEMORY_CONST
:
792 code
[1] |= i
->getSrc(2)->reg
.fileIndex
<< 10;
793 setAddress16(i
->src(2));
799 assert(!"bad src2 file");
805 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
807 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
809 emitForm_A(i
, HEX64(00000000, 00000003));
811 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
812 code
[1] |= 0x01800000;
814 code
[0] |= (i
->subOp
& 0x00f) << 7;
815 code
[0] |= (i
->subOp
& 0x0f0) << 1;
816 code
[0] |= (i
->subOp
& 0x100) >> 3;
817 code
[0] |= (i
->subOp
& 0x200) >> 2;
818 code
[1] |= (i
->subOp
& 0xc00) << 13;
821 if (i
->flagsDef
>= 0)
826 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
828 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
829 assert(i
->encSize
== 8);
831 emitForm_A(i
, HEX64(38000000, 00000003));
833 if (i
->dType
== TYPE_S32
)
838 CodeEmitterNVC0::emitNOT(Instruction
*i
)
840 assert(i
->encSize
== 8);
841 i
->setSrc(1, i
->src(0));
842 emitForm_A(i
, HEX64(68000000, 000001c3
));
846 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
848 if (i
->def(0).getFile() == FILE_PREDICATE
) {
849 code
[0] = 0x00000004 | (subOp
<< 30);
850 code
[1] = 0x0c000000;
854 defId(i
->def(0), 17);
855 srcId(i
->src(0), 20);
856 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
857 srcId(i
->src(1), 26);
858 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
860 if (i
->defExists(1)) {
861 defId(i
->def(1), 14);
866 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
867 code
[1] |= subOp
<< 21;
868 srcId(i
->src(2), 49);
869 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[1] |= 1 << 20;
871 code
[1] |= 0x000e0000;
874 if (i
->encSize
== 8) {
875 if (isLIMM(i
->src(1), TYPE_U32
)) {
876 emitForm_A(i
, HEX64(38000000, 00000002));
878 if (i
->flagsDef
>= 0)
881 emitForm_A(i
, HEX64(68000000, 00000003));
883 if (i
->flagsDef
>= 0)
886 code
[0] |= subOp
<< 6;
888 if (i
->flagsSrc
>= 0) // carry
891 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
892 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
894 emitForm_S(i
, (subOp
<< 5) |
895 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
900 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
902 emitForm_A(i
, HEX64(54000000, 00000004));
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;
909 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
911 emitForm_A(i
, HEX64(28000000, 00000003));
915 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
917 emitForm_A(i
, HEX64(70000000, 00000003));
919 if (i
->dType
== TYPE_S32
)
921 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
926 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
928 emitForm_B(i
, HEX64(78000000, 00000003));
930 if (i
->dType
== TYPE_S32
)
932 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
934 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
939 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
941 emitForm_A(i
, HEX64(24000000, 00000004));
943 code
[0] |= i
->subOp
<< 5;
947 CodeEmitterNVC0::emitShift(const Instruction
*i
)
949 if (i
->op
== OP_SHR
) {
950 emitForm_A(i
, HEX64(58000000, 00000003)
951 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
953 emitForm_A(i
, HEX64(60000000, 00000003));
956 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
961 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
963 if (i
->encSize
== 8) {
964 emitForm_B(i
, HEX64(60000000, 00000000));
966 if (i
->op
== OP_PREEX2
)
969 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
970 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
972 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
977 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
979 if (i
->encSize
== 8) {
980 code
[0] = 0x00000000 | (subOp
<< 26);
981 code
[1] = 0xc8000000;
985 defId(i
->def(0), 14);
986 srcId(i
->src(0), 20);
988 assert(i
->src(0).getFile() == FILE_GPR
);
990 if (i
->saturate
) code
[0] |= 1 << 5;
992 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
993 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
995 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
997 assert(!i
->src(0).mod
.neg());
998 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
1003 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
1007 assert(i
->encSize
== 8);
1009 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
1014 if (!isFloatType(i
->dType
))
1015 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
1016 if (i
->dType
== TYPE_F64
)
1024 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
1027 case ROUND_M
: code
[1] |= 1 << 17; break;
1028 case ROUND_P
: code
[1] |= 2 << 17; break;
1029 case ROUND_Z
: code
[1] |= 3 << 17; break;
1030 case ROUND_NI
: code
[0] |= 1 << 7; break;
1031 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
1032 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
1033 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
1034 case ROUND_N
: break;
1036 assert(!"invalid round mode");
1042 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
1046 case ROUND_MI
: code
[0] |= 1 << 16; break;
1048 case ROUND_PI
: code
[0] |= 2 << 16; break;
1050 case ROUND_ZI
: code
[0] |= 3 << 16; break;
1057 CodeEmitterNVC0::emitCVT(Instruction
*i
)
1059 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
1063 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
1064 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
1065 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
1070 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
1071 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
1072 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
1074 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
1079 if (i
->encSize
== 8) {
1080 emitForm_B(i
, HEX64(10000000, 00000004));
1084 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
1085 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
1086 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
1088 // for 8/16 source types, the byte/word is in subOp. word 1 is
1089 // represented as 2.
1090 if (!isFloatType(i
->sType
))
1091 code
[1] |= i
->subOp
<< 0x17;
1093 code
[1] |= i
->subOp
<< 0x18;
1099 if (neg
&& i
->op
!= OP_ABS
)
1105 if (isSignedIntType(dType
))
1107 if (isSignedIntType(i
->sType
))
1110 if (isFloatType(dType
)) {
1111 if (!isFloatType(i
->sType
))
1112 code
[1] |= 0x08000000;
1114 if (isFloatType(i
->sType
))
1115 code
[1] |= 0x04000000;
1117 code
[1] |= 0x0c000000;
1120 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
1123 if (isFloatType(dType
)) {
1124 if (isFloatType(i
->sType
))
1127 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1129 assert(isFloatType(i
->sType
));
1131 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1134 if (neg
) code
[0] |= 1 << 16;
1135 if (sat
) code
[0] |= 1 << 18;
1136 if (abs
) code
[0] |= 1 << 19;
1143 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1148 if (i
->sType
== TYPE_F64
)
1151 if (!isFloatType(i
->sType
))
1154 if (isSignedIntType(i
->sType
))
1156 if (isFloatType(i
->dType
)) {
1157 if (isFloatType(i
->sType
))
1164 case OP_SET_AND
: hi
= 0x10000000; break;
1165 case OP_SET_OR
: hi
= 0x10200000; break;
1166 case OP_SET_XOR
: hi
= 0x10400000; break;
1171 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1173 if (i
->op
!= OP_SET
)
1174 srcId(i
->src(2), 32 + 17);
1176 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1177 if (i
->sType
== TYPE_F32
)
1178 code
[1] += 0x10000000;
1180 code
[1] += 0x08000000;
1182 code
[0] &= ~0xfc000;
1183 defId(i
->def(0), 17);
1184 if (i
->defExists(1))
1185 defId(i
->def(1), 14);
1193 emitCondCode(i
->setCond
, 32 + 23);
1198 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1204 op
= HEX64(30000000, 00000023);
1207 op
= HEX64(30000000, 00000003);
1210 op
= HEX64(38000000, 00000000);
1213 assert(!"invalid type for SLCT");
1219 CondCode cc
= i
->setCond
;
1221 if (i
->src(2).mod
.neg())
1222 cc
= reverseCondCode(cc
);
1224 emitCondCode(cc
, 32 + 23);
1231 selpFlip(const FixupEntry
*entry
, uint32_t *code
, const FixupData
& data
)
1233 int loc
= entry
->loc
;
1234 if (data
.force_persample_interp
)
1235 code
[loc
+ 1] |= 1 << 20;
1237 code
[loc
+ 1] &= ~(1 << 20);
1240 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1242 emitForm_A(i
, HEX64(20000000, 00000004));
1244 if (i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1247 if (i
->subOp
== 1) {
1248 addInterp(0, 0, selpFlip
);
1252 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1254 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1255 code
[1] = 0xf0000000;
1257 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1260 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1262 code
[0] = 0x00000086;
1263 code
[1] = 0xd0000000;
1265 code
[1] |= i
->tex
.r
;
1266 code
[1] |= i
->tex
.s
<< 8;
1268 if (i
->tex
.liveOnly
)
1271 defId(i
->def(0), 14);
1272 srcId(i
->src(0), 20);
1276 isNextIndependentTex(const TexInstruction
*i
)
1278 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1280 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1282 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1286 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1288 code
[0] = 0x00000006;
1290 if (isNextIndependentTex(i
))
1291 code
[0] |= 0x080; // t mode
1293 code
[0] |= 0x100; // p mode
1295 if (i
->tex
.liveOnly
)
1299 case OP_TEX
: code
[1] = 0x80000000; break;
1300 case OP_TXB
: code
[1] = 0x84000000; break;
1301 case OP_TXL
: code
[1] = 0x86000000; break;
1302 case OP_TXF
: code
[1] = 0x90000000; break;
1303 case OP_TXG
: code
[1] = 0xa0000000; break;
1304 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1305 case OP_TXD
: code
[1] = 0xe0000000; break;
1307 assert(!"invalid texture op");
1310 if (i
->op
== OP_TXF
) {
1311 if (!i
->tex
.levelZero
)
1312 code
[1] |= 0x02000000;
1314 if (i
->tex
.levelZero
) {
1315 code
[1] |= 0x02000000;
1318 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1321 defId(i
->def(0), 14);
1322 srcId(i
->src(0), 20);
1326 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1328 code
[1] |= i
->tex
.mask
<< 14;
1330 code
[1] |= i
->tex
.r
;
1331 code
[1] |= i
->tex
.s
<< 8;
1332 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1333 code
[1] |= 1 << 18; // in 1st source (with array index)
1336 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1337 if (i
->tex
.target
.isCube())
1339 if (i
->tex
.target
.isArray())
1341 if (i
->tex
.target
.isShadow())
1344 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1346 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1348 if (i
->op
== OP_TXL
)
1349 code
[1] &= ~(1 << 26);
1351 if (i
->op
== OP_TXF
)
1352 code
[1] &= ~(1 << 25);
1354 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1355 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1358 if (i
->tex
.useOffsets
== 1)
1360 if (i
->tex
.useOffsets
== 4)
1367 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1369 code
[0] = 0x00000086;
1370 code
[1] = 0xc0000000;
1372 switch (i
->tex
.query
) {
1373 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1374 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1375 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1376 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1377 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1378 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1380 assert(!"invalid texture query");
1384 code
[1] |= i
->tex
.mask
<< 14;
1386 code
[1] |= i
->tex
.r
;
1387 code
[1] |= i
->tex
.s
<< 8;
1388 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1391 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1393 defId(i
->def(0), 14);
1394 srcId(i
->src(0), 20);
1401 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1403 code
[0] = 0x00000200 | (laneMask
<< 6); // dall
1404 code
[1] = 0x48000000 | qOp
;
1406 defId(i
->def(0), 14);
1407 srcId(i
->src(0), 20);
1408 srcId((i
->srcExists(1) && i
->predSrc
!= 1) ? i
->src(1) : i
->src(0), 26);
1414 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1416 const FlowInstruction
*f
= i
->asFlow();
1418 unsigned mask
; // bit 0: predicate, bit 1: target
1420 code
[0] = 0x00000007;
1424 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1425 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1430 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1432 code
[0] |= 0x4000; // indirect calls always use c[] source
1436 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1437 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1438 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1439 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1440 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1442 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1443 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1444 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1445 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1447 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1448 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1449 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1451 assert(!"invalid flow operation");
1457 if (i
->flagsSrc
< 0)
1470 if (code
[0] & 0x4000) {
1471 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1472 setAddress16(i
->src(0));
1473 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1474 if (f
->op
== OP_BRA
)
1475 srcId(f
->src(0).getIndirect(0), 20);
1481 if (f
->op
== OP_CALL
) {
1486 assert(f
->absolute
);
1487 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1488 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1489 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1491 assert(!f
->absolute
);
1492 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1493 code
[0] |= (pcRel
& 0x3f) << 26;
1494 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1498 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1499 if (writeIssueDelays
&& !(f
->target
.bb
->binPos
& 0x3f))
1501 // currently we don't want absolute branches
1502 assert(!f
->absolute
);
1503 code
[0] |= (pcRel
& 0x3f) << 26;
1504 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1509 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1511 Value
*rDef
= NULL
, *pDef
= NULL
;
1514 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1515 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1516 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1517 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1520 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1523 code
[1] = 0x50000000;
1525 code
[0] |= 63 << 14;
1531 if (i
->src(0).getFile() == FILE_GPR
) {
1532 srcId(i
->src(0), 20);
1534 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1536 code
[0] |= imm
->reg
.data
.u32
<< 20;
1541 if (i
->src(1).getFile() == FILE_GPR
) {
1542 srcId(i
->src(1), 26);
1544 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1546 assert(imm
->reg
.data
.u32
<= 0xfff);
1547 code
[0] |= imm
->reg
.data
.u32
<< 26;
1548 code
[1] |= imm
->reg
.data
.u32
>> 6;
1552 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1553 srcId(i
->src(2), 32 + 17);
1554 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1560 if (i
->defExists(0)) {
1561 if (i
->def(0).getFile() == FILE_GPR
)
1562 rDef
= i
->getDef(0);
1564 pDef
= i
->getDef(0);
1566 if (i
->defExists(1)) {
1567 if (i
->def(1).getFile() == FILE_GPR
)
1568 rDef
= i
->getDef(1);
1570 pDef
= i
->getDef(1);
1574 code
[0] &= ~(63 << 14);
1578 code
[1] &= ~(7 << 21);
1579 defId(pDef
, 32 + 21);
1584 CodeEmitterNVC0::emitAFETCH(const Instruction
*i
)
1586 code
[0] = 0x00000006;
1587 code
[1] = 0x0c000000 | (i
->src(0).get()->reg
.data
.offset
& 0x7ff);
1589 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1594 defId(i
->def(0), 14);
1595 srcId(i
->src(0).getIndirect(0), 20);
1599 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1601 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1603 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1604 code
[1] = 0x00000000 | (prim
>> 6);
1608 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1610 defId(i
->def(0), 14);
1615 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1617 code
[0] = 0x00000006;
1618 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1622 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1623 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1627 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1629 defId(i
->def(0), 14);
1630 srcId(i
->src(0).getIndirect(0), 20);
1631 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1635 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1637 unsigned int size
= typeSizeof(i
->dType
);
1639 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1640 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1642 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1649 assert(i
->src(1).getFile() == FILE_GPR
);
1651 srcId(i
->src(0).getIndirect(0), 20);
1652 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1653 srcId(i
->src(1), 26);
1657 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1659 code
[0] = 0x00000006;
1660 code
[1] = 0x1c000000;
1664 defId(i
->def(0), 14); // new secret address
1665 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1667 assert(i
->src(0).getFile() == FILE_GPR
);
1669 if (i
->op
== OP_EMIT
)
1671 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1675 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1676 unsigned int stream
= SDATA(i
->src(1)).u32
;
1680 code
[0] |= stream
<< 26;
1685 srcId(i
->src(1), 26);
1690 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1692 if (i
->encSize
== 8) {
1693 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1695 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1697 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1702 interpApply(const FixupEntry
*entry
, uint32_t *code
, const FixupData
& data
)
1704 int ipa
= entry
->ipa
;
1705 int reg
= entry
->reg
;
1706 int loc
= entry
->loc
;
1708 if (data
.flatshade
&&
1709 (ipa
& NV50_IR_INTERP_MODE_MASK
) == NV50_IR_INTERP_SC
) {
1710 ipa
= NV50_IR_INTERP_FLAT
;
1712 } else if (data
.force_persample_interp
&&
1713 (ipa
& NV50_IR_INTERP_SAMPLE_MASK
) == NV50_IR_INTERP_DEFAULT
&&
1714 (ipa
& NV50_IR_INTERP_MODE_MASK
) != NV50_IR_INTERP_FLAT
) {
1715 ipa
|= NV50_IR_INTERP_CENTROID
;
1717 code
[loc
+ 0] &= ~(0xf << 6);
1718 code
[loc
+ 0] |= ipa
<< 6;
1719 code
[loc
+ 0] &= ~(0x3f << 26);
1720 code
[loc
+ 0] |= reg
<< 26;
1724 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1726 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1728 if (i
->encSize
== 8) {
1729 code
[0] = 0x00000000;
1730 code
[1] = 0xc0000000 | (base
& 0xffff);
1735 if (i
->op
== OP_PINTERP
) {
1736 srcId(i
->src(1), 26);
1737 addInterp(i
->ipa
, SDATA(i
->src(1)).id
, interpApply
);
1739 code
[0] |= 0x3f << 26;
1740 addInterp(i
->ipa
, 0x3f, interpApply
);
1743 srcId(i
->src(0).getIndirect(0), 20);
1745 assert(i
->op
== OP_PINTERP
);
1746 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1747 srcId(i
->src(1), 20);
1752 defId(i
->def(0), 14);
1754 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1755 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1757 code
[1] |= 0x3f << 17;
1761 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1794 assert(!"invalid type");
1801 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1822 assert(!"invalid caching mode");
1829 uses64bitAddress(const Instruction
*ldst
)
1831 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1832 ldst
->src(0).isIndirect(0) &&
1833 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1837 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1841 switch (i
->src(0).getFile()) {
1842 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1843 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1844 case FILE_MEMORY_SHARED
:
1845 if (i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1846 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1855 assert(!"invalid memory file");
1859 code
[0] = 0x00000005;
1862 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
) {
1863 // Unlocked store on shared memory can fail.
1864 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
&&
1865 i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1866 assert(i
->defExists(0));
1867 defId(i
->def(0), 8);
1871 setAddressByFile(i
->src(0));
1872 srcId(i
->src(1), 14);
1873 srcId(i
->src(0).getIndirect(0), 20);
1874 if (uses64bitAddress(i
))
1879 emitLoadStoreType(i
->dType
);
1880 emitCachingMode(i
->cache
);
1884 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1888 code
[0] = 0x00000005;
1890 switch (i
->src(0).getFile()) {
1891 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1892 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1893 case FILE_MEMORY_SHARED
:
1894 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1895 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1903 case FILE_MEMORY_CONST
:
1904 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1905 emitMOV(i
); // not sure if this is any better
1908 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1909 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1912 assert(!"invalid memory file");
1919 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
) {
1920 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1921 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
1924 } else if (i
->defExists(1)) { // r, p
1927 assert(!"Expected predicate dest for load locked");
1933 defId(i
->def(r
), 14);
1935 code
[0] |= 63 << 14;
1938 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1939 defId(i
->def(p
), 8);
1941 defId(i
->def(p
), 32 + 18);
1944 setAddressByFile(i
->src(0));
1945 srcId(i
->src(0).getIndirect(0), 20);
1946 if (uses64bitAddress(i
))
1951 emitLoadStoreType(i
->dType
);
1952 emitCachingMode(i
->cache
);
1956 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1958 switch (SDATA(ref
).sv
.sv
) {
1959 case SV_LANEID
: return 0x00;
1960 case SV_PHYSID
: return 0x03;
1961 case SV_VERTEX_COUNT
: return 0x10;
1962 case SV_INVOCATION_ID
: return 0x11;
1963 case SV_YDIR
: return 0x12;
1964 case SV_THREAD_KILL
: return 0x13;
1965 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1966 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1967 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1968 case SV_GRIDID
: return 0x2c;
1969 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1970 case SV_LBASE
: return 0x34;
1971 case SV_SBASE
: return 0x30;
1972 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1974 assert(!"no sreg for system value");
1980 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1982 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1983 if (i
->src(0).getFile() == FILE_GPR
) {
1984 code
[0] = 0xfc01c003;
1985 code
[1] = 0x1a8e0000;
1986 srcId(i
->src(0), 20);
1988 code
[0] = 0x0001c004;
1989 code
[1] = 0x0c0e0000;
1990 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1992 if (!i
->getSrc(0)->reg
.data
.u32
)
1995 srcId(i
->src(0), 20);
1998 defId(i
->def(0), 17);
2001 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
2002 uint8_t sr
= getSRegEncoding(i
->src(0));
2004 if (i
->encSize
== 8) {
2005 code
[0] = 0x00000004 | (sr
<< 26);
2006 code
[1] = 0x2c000000;
2008 code
[0] = 0x40000008 | (sr
<< 20);
2010 defId(i
->def(0), 14);
2014 if (i
->encSize
== 8) {
2017 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
2018 opc
= HEX64(18000000, 000001e2
);
2020 if (i
->src(0).getFile() == FILE_PREDICATE
)
2021 opc
= HEX64(080e0000
, 1c000004
);
2023 opc
= HEX64(28000000, 00000004);
2025 if (i
->src(0).getFile() != FILE_PREDICATE
)
2026 opc
|= i
->lanes
<< 5;
2030 // Explicitly emit the predicate source as emitForm_B skips it.
2031 if (i
->src(0).getFile() == FILE_PREDICATE
)
2032 srcId(i
->src(0), 20);
2036 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
2037 imm
= SDATA(i
->src(0)).u32
;
2038 if (imm
& 0xfff00000) {
2039 assert(!(imm
& 0x000fffff));
2040 code
[0] = 0x00000318 | imm
;
2042 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
2043 code
[0] = 0x00000118 | (imm
<< 20);
2047 emitShortSrc2(i
->src(0));
2049 defId(i
->def(0), 14);
2056 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
2058 const bool hasDst
= i
->defExists(0);
2059 const bool casOrExch
=
2060 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
2061 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
2063 if (i
->dType
== TYPE_U64
) {
2065 case NV50_IR_SUBOP_ATOM_ADD
:
2068 code
[1] = 0x507e0000;
2070 code
[1] = 0x10000000;
2072 case NV50_IR_SUBOP_ATOM_EXCH
:
2074 code
[1] = 0x507e0000;
2076 case NV50_IR_SUBOP_ATOM_CAS
:
2078 code
[1] = 0x50000000;
2081 assert(!"invalid u64 red op");
2085 if (i
->dType
== TYPE_U32
) {
2087 case NV50_IR_SUBOP_ATOM_EXCH
:
2089 code
[1] = 0x507e0000;
2091 case NV50_IR_SUBOP_ATOM_CAS
:
2093 code
[1] = 0x50000000;
2096 code
[0] = 0x5 | (i
->subOp
<< 5);
2098 code
[1] = 0x507e0000;
2100 code
[1] = 0x10000000;
2104 if (i
->dType
== TYPE_S32
) {
2105 assert(i
->subOp
<= 2);
2106 code
[0] = 0x205 | (i
->subOp
<< 5);
2108 code
[1] = 0x587e0000;
2110 code
[1] = 0x18000000;
2112 if (i
->dType
== TYPE_F32
) {
2113 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
2116 code
[1] = 0x687e0000;
2118 code
[1] = 0x28000000;
2123 srcId(i
->src(1), 14);
2126 defId(i
->def(0), 32 + 11);
2129 code
[1] |= 63 << 11;
2131 if (hasDst
|| casOrExch
) {
2132 const int32_t offset
= SDATA(i
->src(0)).offset
;
2133 assert(offset
< 0x80000 && offset
>= -0x80000);
2134 code
[0] |= offset
<< 26;
2135 code
[1] |= (offset
& 0x1ffc0) >> 6;
2136 code
[1] |= (offset
& 0xe0000) << 6;
2138 srcAddr32(i
->src(0), 26, 0);
2140 if (i
->getIndirect(0, 0)) {
2141 srcId(i
->getIndirect(0, 0), 20);
2142 if (i
->getIndirect(0, 0)->reg
.size
== 8)
2145 code
[0] |= 63 << 20;
2148 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
2149 assert(i
->src(1).getSize() == 2 * typeSizeof(i
->sType
));
2150 code
[1] |= (SDATA(i
->src(1)).id
+ 1) << 17;
2155 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
2157 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
2158 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
2159 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
2162 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
2165 code
[1] = 0xe0000000;
2171 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
2173 code
[0] = 0x00000005 | (i
->subOp
<< 5);
2175 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
2176 code
[1] = 0x98000000;
2177 srcAddr32(i
->src(0), 28, 2);
2179 code
[1] = 0xd0000000;
2180 setAddress24(i
->src(0));
2182 if (uses64bitAddress(i
))
2184 srcId(i
->src(0).getIndirect(0), 20);
2192 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
2195 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
2196 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
2197 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
2198 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
2199 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
2200 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
2201 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
2202 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
2203 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
2204 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
2205 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
2206 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
2207 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
2208 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
2209 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
2210 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
2215 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
2220 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
2222 ImmediateValue
*imm
= NULL
;
2225 if (i
->srcExists(2)) {
2226 imm
= i
->getSrc(2)->asImm();
2228 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
2232 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
2233 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
2234 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
2241 if (i
->op
== OP_SUCLAMP
) {
2242 if (i
->dType
== TYPE_S32
)
2244 emitSUCLAMPMode(i
->subOp
);
2247 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2250 if (i
->op
!= OP_SUEAU
) {
2251 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2252 code
[0] |= 63 << 14;
2253 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2255 if (i
->defExists(1)) { // r, p
2256 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2257 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2263 assert(i
->op
== OP_SUCLAMP
);
2265 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2270 CodeEmitterNVC0::emitSUGType(DataType ty
)
2273 case TYPE_S32
: code
[1] |= 1 << 13; break;
2274 case TYPE_U8
: code
[1] |= 2 << 13; break;
2275 case TYPE_S8
: code
[1] |= 3 << 13; break;
2277 assert(ty
== TYPE_U32
);
2283 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2285 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2287 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2288 assert(offset
== (offset
& 0xfffc));
2291 code
[0] |= offset
<< 24;
2292 code
[1] |= offset
>> 8;
2293 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2297 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2299 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2300 code
[1] |= 0x7 << 17;
2302 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2304 srcId(i
->src(s
), 32 + 17);
2309 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2312 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2314 emitLoadStoreType(i
->dType
);
2315 emitSUGType(i
->sType
);
2316 emitCachingMode(i
->cache
);
2319 defId(i
->def(0), 14); // destination
2320 srcId(i
->src(0), 20); // address
2322 if (i
->src(1).getFile() == FILE_GPR
)
2323 srcId(i
->src(1), 26);
2330 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2333 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2335 if (i
->op
== OP_SUSTP
)
2336 code
[1] |= i
->tex
.mask
<< 22;
2338 emitLoadStoreType(i
->dType
);
2339 emitSUGType(i
->sType
);
2340 emitCachingMode(i
->cache
);
2343 srcId(i
->src(0), 20); // address
2345 if (i
->src(1).getFile() == FILE_GPR
)
2346 srcId(i
->src(1), 26);
2349 srcId(i
->src(3), 14); // values
2354 CodeEmitterNVC0::emitSUAddr(const TexInstruction
*i
)
2356 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2358 if (i
->tex
.rIndirectSrc
< 0) {
2359 code
[1] |= 0x00004000;
2360 code
[0] |= i
->tex
.r
<< 26;
2362 srcId(i
, i
->tex
.rIndirectSrc
, 26);
2367 CodeEmitterNVC0::emitSUDim(const TexInstruction
*i
)
2369 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2371 code
[1] |= (i
->tex
.target
.getDim() - 1) << 12;
2372 if (i
->tex
.target
.isArray() || i
->tex
.target
.isCube() ||
2373 i
->tex
.target
.getDim() == 3) {
2374 // use e2d mode for 3-dim images, arrays and cubes.
2378 srcId(i
->src(0), 20);
2382 CodeEmitterNVC0::emitSULEA(const TexInstruction
*i
)
2384 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2387 code
[1] = 0xf0000000;
2390 emitLoadStoreType(i
->sType
);
2392 defId(i
->def(0), 14);
2394 if (i
->defExists(1)) {
2395 defId(i
->def(1), 32 + 22);
2405 CodeEmitterNVC0::emitSULDB(const TexInstruction
*i
)
2407 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2410 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2413 emitLoadStoreType(i
->dType
);
2415 defId(i
->def(0), 14);
2417 emitCachingMode(i
->cache
);
2423 CodeEmitterNVC0::emitSUSTx(const TexInstruction
*i
)
2425 assert(targ
->getChipset() < NVISA_GK104_CHIPSET
);
2428 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2430 if (i
->op
== OP_SUSTP
)
2431 code
[1] |= i
->tex
.mask
<< 17;
2433 emitLoadStoreType(i
->dType
);
2437 srcId(i
->src(1), 14);
2439 emitCachingMode(i
->cache
);
2445 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2447 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2449 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2450 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2451 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2452 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2455 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2456 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2457 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2458 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2459 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2460 code
[1] |= (i
->mask
& 0x3) << 2;
2463 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2464 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2465 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2466 code
[1] |= (i
->mask
& 0x3) << 2;
2467 code
[1] |= (i
->mask
& 0xc) << 21;
2476 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2480 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2481 case 0: opc
|= 0xe8ULL
<< 56; break;
2482 case 1: opc
|= 0xb4ULL
<< 56; break;
2483 case 2: opc
|= 0x94ULL
<< 56; break;
2488 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2489 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2490 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2492 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2493 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2500 if (i
->flagsDef
>= 0)
2505 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2507 assert(i
->encSize
== 8);
2508 emitForm_A(i
, HEX64(10000000, 00000006));
2509 code
[0] |= i
->subOp
<< 5;
2510 code
[1] |= 0x00e00000;
2514 CodeEmitterNVC0::emitVOTE(const Instruction
*i
)
2516 assert(i
->src(0).getFile() == FILE_PREDICATE
);
2518 code
[0] = 0x00000004 | (i
->subOp
<< 5);
2519 code
[1] = 0x48000000;
2524 for (int d
= 0; i
->defExists(d
); d
++) {
2525 if (i
->def(d
).getFile() == FILE_PREDICATE
) {
2528 defId(i
->def(d
), 32 + 22);
2529 } else if (i
->def(d
).getFile() == FILE_GPR
) {
2532 defId(i
->def(d
), 14);
2534 assert(!"Unhandled def");
2538 code
[0] |= 63 << 14;
2541 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
2543 srcId(i
->src(0), 20);
2547 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2549 unsigned int size
= insn
->encSize
;
2551 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2554 if (!insn
->encSize
) {
2555 ERROR("skipping unencodable instruction: "); insn
->print();
2558 if (codeSize
+ size
> codeSizeLimit
) {
2559 ERROR("code emitter output buffer too small\n");
2563 if (writeIssueDelays
) {
2564 if (!(codeSize
& 0x3f)) {
2565 code
[0] = 0x00000007; // cf issue delay "instruction"
2566 code
[1] = 0x20000000;
2570 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2571 uint32_t *data
= code
- (id
* 2 + 2);
2573 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2576 data
[0] |= insn
->sched
<< 28;
2577 data
[1] |= insn
->sched
>> 4;
2579 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2583 // assert that instructions with multiple defs don't corrupt registers
2584 for (int d
= 0; insn
->defExists(d
); ++d
)
2585 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2622 if (insn
->dType
== TYPE_F64
)
2624 else if (isFloatType(insn
->dType
))
2630 if (insn
->dType
== TYPE_F64
)
2632 else if (isFloatType(insn
->dType
))
2639 if (insn
->dType
== TYPE_F64
)
2641 else if (isFloatType(insn
->dType
))
2656 emitLogicOp(insn
, 0);
2659 emitLogicOp(insn
, 1);
2662 emitLogicOp(insn
, 2);
2672 emitSET(insn
->asCmp());
2678 emitSLCT(insn
->asCmp());
2693 if (insn
->def(0).getFile() == FILE_PREDICATE
||
2694 insn
->src(0).getFile() == FILE_PREDICATE
)
2700 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
2703 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
2728 emitTEX(insn
->asTex());
2731 emitTXQ(insn
->asTex());
2745 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2746 emitSULDGB(insn
->asTex());
2748 emitSULDB(insn
->asTex());
2752 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2753 emitSUSTGx(insn
->asTex());
2755 emitSUSTx(insn
->asTex());
2758 emitSULEA(insn
->asTex());
2780 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2783 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2786 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2828 ERROR("operation should have been eliminated");
2834 ERROR("operation should have been lowered\n");
2837 ERROR("unknown op: %u\n", insn
->op
);
2843 assert(insn
->encSize
== 8);
2846 code
+= insn
->encSize
/ 4;
2847 codeSize
+= insn
->encSize
;
2852 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2854 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2856 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2859 if (i
->ftz
|| i
->saturate
|| i
->join
)
2861 if (i
->rnd
!= ROUND_N
)
2863 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2866 if (i
->op
== OP_PINTERP
) {
2867 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2870 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2874 for (int s
= 0; i
->srcExists(s
); ++s
) {
2875 if (i
->src(s
).isIndirect(0))
2878 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2879 if (SDATA(i
->src(s
)).offset
>= 0x100)
2881 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2882 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2885 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2886 if (i
->dType
== TYPE_F32
) {
2887 if (SDATA(i
->src(s
)).u32
>= 0x100)
2890 if (SDATA(i
->src(s
)).u32
> 0xff)
2895 if (i
->op
== OP_CVT
)
2897 if (i
->src(s
).mod
!= Modifier(0)) {
2898 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2899 if (i
->op
!= OP_RSQ
)
2901 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2902 if (i
->op
!= OP_ADD
|| s
!= 0)
2910 // Simplified, erring on safe side.
2911 class SchedDataCalculator
: public Pass
2914 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2920 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2921 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2922 int tex
; // TEX to non-TEX delay 17 (0x11)
2923 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2924 int imul
; // integer MUL to MUL delay 3
2934 void rebase(const int base
)
2936 const int delta
= this->base
- base
;
2941 for (int i
= 0; i
< regs
; ++i
) {
2945 for (int i
= 0; i
< 8; ++i
) {
2952 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2962 memset(&rd
, 0, sizeof(rd
));
2963 memset(&wr
, 0, sizeof(wr
));
2964 memset(&res
, 0, sizeof(res
));
2967 int getLatest(const ScoreData
& d
) const
2970 for (int i
= 0; i
< regs
; ++i
)
2973 for (int i
= 0; i
< 8; ++i
)
2980 inline int getLatestRd() const
2982 return getLatest(rd
);
2984 inline int getLatestWr() const
2986 return getLatest(wr
);
2988 inline int getLatest() const
2990 const int a
= getLatestRd();
2991 const int b
= getLatestWr();
2993 int max
= MAX2(a
, b
);
2994 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2995 max
= MAX2(res
.ld
[f
], max
);
2996 max
= MAX2(res
.st
[f
], max
);
2998 max
= MAX2(res
.sfu
, max
);
2999 max
= MAX2(res
.imul
, max
);
3000 max
= MAX2(res
.tex
, max
);
3003 void setMax(const RegScores
*that
)
3005 for (int i
= 0; i
< regs
; ++i
) {
3006 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
3007 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
3009 for (int i
= 0; i
< 8; ++i
) {
3010 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
3011 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
3013 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
3014 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
3016 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
3017 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
3018 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
3020 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
3021 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
3022 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
3024 void print(int cycle
)
3026 for (int i
= 0; i
< regs
; ++i
) {
3027 if (rd
.r
[i
] > cycle
)
3028 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
3029 if (wr
.r
[i
] > cycle
)
3030 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
3032 for (int i
= 0; i
< 8; ++i
) {
3033 if (rd
.p
[i
] > cycle
)
3034 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
3035 if (wr
.p
[i
] > cycle
)
3036 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
3039 INFO("rd $c @ %i\n", rd
.c
);
3041 INFO("wr $c @ %i\n", wr
.c
);
3042 if (res
.sfu
> cycle
)
3043 INFO("sfu @ %i\n", res
.sfu
);
3044 if (res
.imul
> cycle
)
3045 INFO("imul @ %i\n", res
.imul
);
3046 if (res
.tex
> cycle
)
3047 INFO("tex @ %i\n", res
.tex
);
3051 RegScores
*score
; // for current BB
3052 std::vector
<RegScores
> scoreBoards
;
3058 bool visit(Function
*);
3059 bool visit(BasicBlock
*);
3061 void commitInsn(const Instruction
*, int cycle
);
3062 int calcDelay(const Instruction
*, int cycle
) const;
3063 void setDelay(Instruction
*, int delay
, Instruction
*next
);
3065 void recordRd(const Value
*, const int ready
);
3066 void recordWr(const Value
*, const int ready
);
3067 void checkRd(const Value
*, int cycle
, int& delay
) const;
3068 void checkWr(const Value
*, int cycle
, int& delay
) const;
3070 int getCycles(const Instruction
*, int origDelay
) const;
3074 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
3076 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
3077 delay
= MAX2(delay
, 14);
3079 if (insn
->op
== OP_TEXBAR
) {
3080 // TODO: except if results not used before EXIT
3083 if (insn
->op
== OP_JOIN
|| insn
->join
) {
3086 if (delay
>= 0 || prevData
== 0x04 ||
3087 !next
|| !targ
->canDualIssue(insn
, next
)) {
3088 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
3089 if (prevOp
== OP_EXPORT
)
3090 insn
->sched
|= 0x40;
3092 insn
->sched
|= 0x20;
3094 insn
->sched
= 0x04; // dual-issue
3097 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
3098 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
3101 prevData
= insn
->sched
;
3105 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
3107 if (insn
->sched
& 0x80) {
3108 int c
= (insn
->sched
& 0x0f) * 2 + 1;
3109 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
3113 if (insn
->sched
& 0x60)
3114 return (insn
->sched
& 0x1f) + 1;
3115 return (insn
->sched
== 0x04) ? 0 : 32;
3119 SchedDataCalculator::visit(Function
*func
)
3121 int regs
= targ
->getFileSize(FILE_GPR
) + 1;
3122 scoreBoards
.resize(func
->cfg
.getSize());
3123 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
3124 scoreBoards
[i
].wipe(regs
);
3129 SchedDataCalculator::visit(BasicBlock
*bb
)
3132 Instruction
*next
= NULL
;
3138 score
= &scoreBoards
.at(bb
->getId());
3140 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
3141 // back branches will wait until all target dependencies are satisfied
3142 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
3144 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
3145 if (in
->getExit()) {
3146 if (prevData
!= 0x04)
3147 prevData
= in
->getExit()->sched
;
3148 prevOp
= in
->getExit()->op
;
3150 score
->setMax(&scoreBoards
.at(in
->getId()));
3152 if (bb
->cfg
.incidentCount() > 1)
3155 #ifdef NVC0_DEBUG_SCHED_DATA
3156 INFO("=== BB:%i initial scores\n", bb
->getId());
3157 score
->print(cycle
);
3160 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
3163 commitInsn(insn
, cycle
);
3164 int delay
= calcDelay(next
, cycle
);
3165 setDelay(insn
, delay
, next
);
3166 cycle
+= getCycles(insn
, delay
);
3168 #ifdef NVC0_DEBUG_SCHED_DATA
3169 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
3176 commitInsn(insn
, cycle
);
3180 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
3181 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
3183 if (ei
.getType() != Graph::Edge::BACK
) {
3184 // only test the first instruction of the outgoing block
3185 next
= out
->getEntry();
3187 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
3189 // wait until all dependencies are satisfied
3190 const int regsFree
= score
->getLatest();
3191 next
= out
->getFirst();
3192 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
3193 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
3194 c
+= getCycles(next
, bbDelay
);
3199 if (bb
->cfg
.outgoingCount() != 1)
3201 setDelay(insn
, bbDelay
, next
);
3202 cycle
+= getCycles(insn
, bbDelay
);
3204 score
->rebase(cycle
); // common base for initializing out blocks' scores
3208 #define NVE4_MAX_ISSUE_DELAY 0x1f
3210 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
3212 int delay
= 0, ready
= cycle
;
3214 for (int s
= 0; insn
->srcExists(s
); ++s
)
3215 checkRd(insn
->getSrc(s
), cycle
, delay
);
3216 // WAR & WAW don't seem to matter
3217 // for (int s = 0; insn->srcExists(s); ++s)
3218 // recordRd(insn->getSrc(s), cycle);
3220 switch (Target::getOpClass(insn
->op
)) {
3222 ready
= score
->res
.sfu
;
3225 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3226 ready
= score
->res
.imul
;
3228 case OPCLASS_TEXTURE
:
3229 ready
= score
->res
.tex
;
3232 ready
= score
->res
.ld
[insn
->src(0).getFile()];
3235 ready
= score
->res
.st
[insn
->src(0).getFile()];
3240 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
3241 ready
= MAX2(ready
, score
->res
.tex
);
3243 delay
= MAX2(delay
, ready
- cycle
);
3245 // if can issue next cycle, delay is 0, not 1
3246 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
3250 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
3252 const int ready
= cycle
+ targ
->getLatency(insn
);
3254 for (int d
= 0; insn
->defExists(d
); ++d
)
3255 recordWr(insn
->getDef(d
), ready
);
3256 // WAR & WAW don't seem to matter
3257 // for (int s = 0; insn->srcExists(s); ++s)
3258 // recordRd(insn->getSrc(s), cycle);
3260 switch (Target::getOpClass(insn
->op
)) {
3262 score
->res
.sfu
= cycle
+ 4;
3265 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3266 score
->res
.imul
= cycle
+ 4;
3268 case OPCLASS_TEXTURE
:
3269 score
->res
.tex
= cycle
+ 18;
3272 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
3274 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
3275 score
->res
.st
[insn
->src(0).getFile()] = ready
;
3278 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
3279 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
3282 if (insn
->op
== OP_TEXBAR
)
3283 score
->res
.tex
= cycle
;
3289 #ifdef NVC0_DEBUG_SCHED_DATA
3290 score
->print(cycle
);
3295 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
3300 switch (v
->reg
.file
) {
3303 b
= a
+ v
->reg
.size
/ 4;
3304 for (int r
= a
; r
< b
; ++r
)
3305 ready
= MAX2(ready
, score
->rd
.r
[r
]);
3307 case FILE_PREDICATE
:
3308 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
3311 ready
= MAX2(ready
, score
->rd
.c
);
3313 case FILE_SHADER_INPUT
:
3314 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
3315 case FILE_MEMORY_LOCAL
:
3316 case FILE_MEMORY_CONST
:
3317 case FILE_MEMORY_SHARED
:
3318 case FILE_MEMORY_GLOBAL
:
3319 case FILE_SYSTEM_VALUE
:
3320 // TODO: any restrictions here ?
3322 case FILE_IMMEDIATE
:
3329 delay
= MAX2(delay
, ready
- cycle
);
3333 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
3338 switch (v
->reg
.file
) {
3341 b
= a
+ v
->reg
.size
/ 4;
3342 for (int r
= a
; r
< b
; ++r
)
3343 ready
= MAX2(ready
, score
->wr
.r
[r
]);
3345 case FILE_PREDICATE
:
3346 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
3349 assert(v
->reg
.file
== FILE_FLAGS
);
3350 ready
= MAX2(ready
, score
->wr
.c
);
3354 delay
= MAX2(delay
, ready
- cycle
);
3358 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
3360 int a
= v
->reg
.data
.id
;
3362 if (v
->reg
.file
== FILE_GPR
) {
3363 int b
= a
+ v
->reg
.size
/ 4;
3364 for (int r
= a
; r
< b
; ++r
)
3365 score
->rd
.r
[r
] = ready
;
3367 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
3368 if (v
->reg
.file
== FILE_PREDICATE
) {
3369 score
->rd
.p
[a
] = ready
+ 4;
3371 assert(v
->reg
.file
== FILE_FLAGS
);
3372 score
->rd
.c
= ready
+ 4;
3377 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
3379 int a
= v
->reg
.data
.id
;
3381 if (v
->reg
.file
== FILE_GPR
) {
3382 int b
= a
+ v
->reg
.size
/ 4;
3383 for (int r
= a
; r
< b
; ++r
)
3384 score
->wr
.r
[r
] = ready
;
3386 if (v
->reg
.file
== FILE_PREDICATE
) {
3387 score
->wr
.p
[a
] = ready
;
3389 if (v
->reg
.file
== FILE_FLAGS
) {
3390 score
->wr
.c
= ready
;
3395 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
3397 SchedDataCalculator
sched(targ
);
3398 return sched
.run(func
, true, true);
3402 CodeEmitterNVC0::prepareEmission(Function
*func
)
3404 CodeEmitter::prepareEmission(func
);
3406 if (targ
->hasSWSched
)
3407 calculateSchedDataNVC0(targ
, func
);
3410 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3411 : CodeEmitter(target
),
3413 writeIssueDelays(target
->hasSWSched
)
3416 codeSize
= codeSizeLimit
= 0;
3421 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3423 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3424 emit
->setProgramType(type
);
3429 TargetNVC0::getCodeEmitter(Program::Type type
)
3431 if (chipset
>= NVISA_GK20A_CHIPSET
)
3432 return createCodeEmitterGK110(type
);
3433 return createCodeEmitterNVC0(type
);
3436 } // namespace nv50_ir