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 emitCachingMode(CacheMode c
);
68 void emitShortSrc2(const ValueRef
&);
70 inline uint8_t getSRegEncoding(const ValueRef
&);
72 void roundMode_A(const Instruction
*);
73 void roundMode_C(const Instruction
*);
74 void roundMode_CS(const Instruction
*);
76 void emitNegAbs12(const Instruction
*);
78 void emitNOP(const Instruction
*);
80 void emitLOAD(const Instruction
*);
81 void emitSTORE(const Instruction
*);
82 void emitMOV(const Instruction
*);
83 void emitATOM(const Instruction
*);
84 void emitMEMBAR(const Instruction
*);
85 void emitCCTL(const Instruction
*);
87 void emitINTERP(const Instruction
*);
88 void emitAFETCH(const Instruction
*);
89 void emitPFETCH(const Instruction
*);
90 void emitVFETCH(const Instruction
*);
91 void emitEXPORT(const Instruction
*);
92 void emitOUT(const Instruction
*);
94 void emitUADD(const Instruction
*);
95 void emitFADD(const Instruction
*);
96 void emitDADD(const Instruction
*);
97 void emitUMUL(const Instruction
*);
98 void emitFMUL(const Instruction
*);
99 void emitDMUL(const Instruction
*);
100 void emitIMAD(const Instruction
*);
101 void emitISAD(const Instruction
*);
102 void emitFMAD(const Instruction
*);
103 void emitDMAD(const Instruction
*);
104 void emitMADSP(const Instruction
*);
106 void emitNOT(Instruction
*);
107 void emitLogicOp(const Instruction
*, uint8_t subOp
);
108 void emitPOPC(const Instruction
*);
109 void emitINSBF(const Instruction
*);
110 void emitEXTBF(const Instruction
*);
111 void emitBFIND(const Instruction
*);
112 void emitPERMT(const Instruction
*);
113 void emitShift(const Instruction
*);
115 void emitSFnOp(const Instruction
*, uint8_t subOp
);
117 void emitCVT(Instruction
*);
118 void emitMINMAX(const Instruction
*);
119 void emitPreOp(const Instruction
*);
121 void emitSET(const CmpInstruction
*);
122 void emitSLCT(const CmpInstruction
*);
123 void emitSELP(const Instruction
*);
125 void emitTEXBAR(const Instruction
*);
126 void emitTEX(const TexInstruction
*);
127 void emitTEXCSAA(const TexInstruction
*);
128 void emitTXQ(const TexInstruction
*);
130 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
132 void emitFlow(const Instruction
*);
133 void emitBAR(const Instruction
*);
135 void emitSUCLAMPMode(uint16_t);
136 void emitSUCalc(Instruction
*);
137 void emitSULDGB(const TexInstruction
*);
138 void emitSUSTGx(const TexInstruction
*);
140 void emitVSHL(const Instruction
*);
141 void emitVectorSubOp(const Instruction
*);
143 void emitPIXLD(const Instruction
*);
145 void emitVOTE(const Instruction
*);
147 inline void defId(const ValueDef
&, const int pos
);
148 inline void defId(const Instruction
*, int d
, const int pos
);
149 inline void srcId(const ValueRef
&, const int pos
);
150 inline void srcId(const ValueRef
*, const int pos
);
151 inline void srcId(const Instruction
*, int s
, const int pos
);
152 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
154 inline bool isLIMM(const ValueRef
&, DataType ty
);
157 // for better visibility
158 #define HEX64(h, l) 0x##h##l##ULL
160 #define SDATA(a) ((a).rep()->reg.data)
161 #define DDATA(a) ((a).rep()->reg.data)
163 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
165 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
168 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
170 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
173 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
175 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
176 code
[pos
/ 32] |= r
<< (pos
% 32);
180 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
182 const uint32_t offset
= SDATA(src
).offset
>> shr
;
184 code
[pos
/ 32] |= offset
<< (pos
% 32);
185 if (pos
&& (pos
< 32))
186 code
[1] |= offset
>> (32 - pos
);
189 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
191 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
194 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
196 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
197 code
[pos
/ 32] |= r
<< (pos
% 32);
200 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
202 const ImmediateValue
*imm
= ref
.get()->asImm();
204 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
208 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
211 case ROUND_M
: code
[1] |= 1 << 23; break;
212 case ROUND_P
: code
[1] |= 2 << 23; break;
213 case ROUND_Z
: code
[1] |= 3 << 23; break;
215 assert(insn
->rnd
== ROUND_N
);
221 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
223 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
224 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
225 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
226 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
229 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
234 case CC_LT
: val
= 0x1; break;
235 case CC_LTU
: val
= 0x9; break;
236 case CC_EQ
: val
= 0x2; break;
237 case CC_EQU
: val
= 0xa; break;
238 case CC_LE
: val
= 0x3; break;
239 case CC_LEU
: val
= 0xb; break;
240 case CC_GT
: val
= 0x4; break;
241 case CC_GTU
: val
= 0xc; break;
242 case CC_NE
: val
= 0x5; break;
243 case CC_NEU
: val
= 0xd; break;
244 case CC_GE
: val
= 0x6; break;
245 case CC_GEU
: val
= 0xe; break;
246 case CC_TR
: val
= 0xf; break;
247 case CC_FL
: val
= 0x0; break;
249 case CC_A
: val
= 0x14; break;
250 case CC_NA
: val
= 0x13; break;
251 case CC_S
: val
= 0x15; break;
252 case CC_NS
: val
= 0x12; break;
253 case CC_C
: val
= 0x16; break;
254 case CC_NC
: val
= 0x11; break;
255 case CC_O
: val
= 0x17; break;
256 case CC_NO
: val
= 0x10; break;
260 assert(!"invalid condition code");
263 code
[pos
/ 32] |= val
<< (pos
% 32);
267 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
269 if (i
->predSrc
>= 0) {
270 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
271 srcId(i
->src(i
->predSrc
), 10);
272 if (i
->cc
== CC_NOT_P
)
273 code
[0] |= 0x2000; // negate
280 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
282 switch (src
.getFile()) {
283 case FILE_MEMORY_GLOBAL
:
284 srcAddr32(src
, 26, 0);
286 case FILE_MEMORY_LOCAL
:
287 case FILE_MEMORY_SHARED
:
291 assert(src
.getFile() == FILE_MEMORY_CONST
);
298 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
300 Symbol
*sym
= src
.get()->asSym();
304 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
305 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
309 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
311 Symbol
*sym
= src
.get()->asSym();
315 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
316 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
320 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
322 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
326 u32
= imm
->reg
.data
.u32
;
328 if ((code
[0] & 0xf) == 0x1) {
330 uint64_t u64
= imm
->reg
.data
.u64
;
331 assert(!(u64
& 0x00000fffffffffffULL
));
332 assert(!(code
[1] & 0xc000));
333 code
[0] |= ((u64
>> 44) & 0x3f) << 26;
334 code
[1] |= 0xc000 | (u64
>> 50);
336 if ((code
[0] & 0xf) == 0x2) {
338 code
[0] |= (u32
& 0x3f) << 26;
341 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
343 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
344 assert(!(code
[1] & 0xc000));
346 code
[0] |= (u32
& 0x3f) << 26;
347 code
[1] |= 0xc000 | (u32
>> 6);
350 assert(!(u32
& 0x00000fff));
351 assert(!(code
[1] & 0xc000));
352 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
353 code
[1] |= 0xc000 | (u32
>> 18);
357 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
359 const ImmediateValue
*imm
= ref
.get()->asImm();
361 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
363 assert(s8
== imm
->reg
.data
.s32
);
365 code
[0] |= (s8
& 0x3f) << 26;
366 code
[0] |= (s8
>> 6) << 8;
370 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
377 defId(i
->def(0), 14);
380 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
383 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
384 switch (i
->getSrc(s
)->reg
.file
) {
385 case FILE_MEMORY_CONST
:
386 assert(!(code
[1] & 0xc000));
387 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
388 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
389 setAddress16(i
->src(s
));
393 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
394 assert(!(code
[1] & 0xc000));
398 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
400 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
403 if (i
->op
== OP_SELP
) {
404 // OP_SELP is used to implement shared+atomics on Fermi.
405 assert(s
== 2 && i
->src(s
).getFile() == FILE_PREDICATE
);
406 srcId(i
->src(s
), 49);
408 // ignore here, can be predicate or flags, but must not be address
415 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
422 defId(i
->def(0), 14);
424 switch (i
->src(0).getFile()) {
425 case FILE_MEMORY_CONST
:
426 assert(!(code
[1] & 0xc000));
427 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
428 setAddress16(i
->src(0));
431 assert(!(code
[1] & 0xc000));
435 srcId(i
->src(0), 26);
438 // ignore here, can be predicate or flags, but must not be address
444 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
449 if (opc
== 0x0d || opc
== 0x0e)
452 defId(i
->def(0), 14);
453 srcId(i
->src(0), 20);
455 assert(pred
|| (i
->predSrc
< 0));
459 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
460 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
461 assert(!(code
[0] & (0x300 >> ss2a
)));
462 switch (i
->src(s
).get()->reg
.fileIndex
) {
463 case 0: code
[0] |= 0x100 >> ss2a
; break;
464 case 1: code
[0] |= 0x200 >> ss2a
; break;
465 case 16: code
[0] |= 0x300 >> ss2a
; break;
467 ERROR("invalid c[] space for short form\n");
471 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
473 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
475 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
477 setImmediateS8(i
->src(s
));
479 if (i
->src(s
).getFile() == FILE_GPR
) {
480 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
486 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
488 if (src
.getFile() == FILE_MEMORY_CONST
) {
489 switch (src
.get()->reg
.fileIndex
) {
490 case 0: code
[0] |= 0x100; break;
491 case 1: code
[0] |= 0x200; break;
492 case 16: code
[0] |= 0x300; break;
494 assert(!"unsupported file index for short op");
497 srcAddr32(src
, 20, 2);
500 assert(src
.getFile() == FILE_GPR
);
505 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
507 code
[0] = 0x000001e4;
508 code
[1] = 0x40000000;
513 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
515 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
517 if (i
->encSize
== 8) {
518 if (isLIMM(i
->src(1), TYPE_F32
)) {
519 emitForm_A(i
, HEX64(20000000, 00000002));
521 emitForm_A(i
, HEX64(30000000, 00000000));
523 if (i
->src(2).mod
.neg())
536 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
537 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
545 CodeEmitterNVC0::emitDMAD(const Instruction
*i
)
547 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
549 emitForm_A(i
, HEX64(20000000, 00000001));
551 if (i
->src(2).mod
.neg())
559 assert(!i
->saturate
);
564 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
566 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
568 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
570 if (i
->encSize
== 8) {
571 if (isLIMM(i
->src(1), TYPE_F32
)) {
572 assert(i
->postFactor
== 0); // constant folded, hopefully
573 emitForm_A(i
, HEX64(30000000, 00000002));
575 emitForm_A(i
, HEX64(58000000, 00000000));
577 code
[1] |= ((i
->postFactor
> 0) ?
578 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
581 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
592 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
593 emitForm_S(i
, 0xa8, true);
598 CodeEmitterNVC0::emitDMUL(const Instruction
*i
)
600 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
602 emitForm_A(i
, HEX64(50000000, 00000001));
608 assert(!i
->saturate
);
611 assert(!i
->postFactor
);
615 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
617 if (i
->encSize
== 8) {
618 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
619 emitForm_A(i
, HEX64(10000000, 00000002));
621 emitForm_A(i
, HEX64(50000000, 00000003));
623 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
625 if (i
->sType
== TYPE_S32
)
627 if (i
->dType
== TYPE_S32
)
630 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
632 if (i
->sType
== TYPE_S32
)
638 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
640 if (i
->encSize
== 8) {
641 if (isLIMM(i
->src(1), TYPE_F32
)) {
642 assert(!i
->saturate
);
643 emitForm_A(i
, HEX64(28000000, 00000002));
645 code
[0] |= i
->src(0).mod
.abs() << 7;
646 code
[0] |= i
->src(0).mod
.neg() << 9;
648 if (i
->src(1).mod
.abs())
649 code
[1] &= 0xfdffffff;
650 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
651 code
[1] ^= 0x02000000;
653 emitForm_A(i
, HEX64(50000000, 00000000));
660 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
665 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
666 !i
->src(0).mod
.abs() &&
667 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
669 emitForm_S(i
, 0x49, true);
671 if (i
->src(0).mod
.neg())
677 CodeEmitterNVC0::emitDADD(const Instruction
*i
)
679 assert(i
->encSize
== 8);
680 emitForm_A(i
, HEX64(48000000, 00000001));
682 assert(!i
->saturate
);
690 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
694 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
695 assert(!i
->src(0).mod
.neg() || !i
->src(1).mod
.neg());
697 if (i
->src(0).mod
.neg())
699 if (i
->src(1).mod
.neg())
701 if (i
->op
== OP_SUB
) {
703 assert(addOp
!= 0x300); // would be add-plus-one
706 if (i
->encSize
== 8) {
707 if (isLIMM(i
->src(1), TYPE_U32
)) {
708 emitForm_A(i
, HEX64(08000000, 00000002));
710 code
[1] |= 1 << 26; // write carry
712 emitForm_A(i
, HEX64(48000000, 00000003));
714 code
[1] |= 1 << 16; // write carry
720 if (i
->flagsSrc
>= 0) // add carry
723 assert(!(addOp
& 0x100));
724 emitForm_S(i
, (addOp
>> 3) |
725 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
731 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
733 assert(i
->encSize
== 8);
734 emitForm_A(i
, HEX64(20000000, 00000003));
736 if (isSignedType(i
->dType
))
738 if (isSignedType(i
->sType
))
741 code
[1] |= i
->saturate
<< 24;
743 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
744 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
746 if (i
->src(2).mod
.neg()) code
[0] |= 0x10;
747 if (i
->src(1).mod
.neg() ^
748 i
->src(0).mod
.neg()) code
[0] |= 0x20;
750 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
755 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
757 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
759 emitForm_A(i
, HEX64(00000000, 00000003));
761 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
762 code
[1] |= 0x01800000;
764 code
[0] |= (i
->subOp
& 0x00f) << 7;
765 code
[0] |= (i
->subOp
& 0x0f0) << 1;
766 code
[0] |= (i
->subOp
& 0x100) >> 3;
767 code
[0] |= (i
->subOp
& 0x200) >> 2;
768 code
[1] |= (i
->subOp
& 0xc00) << 13;
771 if (i
->flagsDef
>= 0)
776 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
778 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
779 assert(i
->encSize
== 8);
781 emitForm_A(i
, HEX64(38000000, 00000003));
783 if (i
->dType
== TYPE_S32
)
788 CodeEmitterNVC0::emitNOT(Instruction
*i
)
790 assert(i
->encSize
== 8);
791 i
->setSrc(1, i
->src(0));
792 emitForm_A(i
, HEX64(68000000, 000001c3
));
796 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
798 if (i
->def(0).getFile() == FILE_PREDICATE
) {
799 code
[0] = 0x00000004 | (subOp
<< 30);
800 code
[1] = 0x0c000000;
804 defId(i
->def(0), 17);
805 srcId(i
->src(0), 20);
806 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
807 srcId(i
->src(1), 26);
808 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
810 if (i
->defExists(1)) {
811 defId(i
->def(1), 14);
816 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
817 code
[1] |= subOp
<< 21;
818 srcId(i
->src(2), 49);
819 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[1] |= 1 << 20;
821 code
[1] |= 0x000e0000;
824 if (i
->encSize
== 8) {
825 if (isLIMM(i
->src(1), TYPE_U32
)) {
826 emitForm_A(i
, HEX64(38000000, 00000002));
828 if (i
->flagsDef
>= 0)
831 emitForm_A(i
, HEX64(68000000, 00000003));
833 if (i
->flagsDef
>= 0)
836 code
[0] |= subOp
<< 6;
838 if (i
->flagsSrc
>= 0) // carry
841 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
842 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
844 emitForm_S(i
, (subOp
<< 5) |
845 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
850 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
852 emitForm_A(i
, HEX64(54000000, 00000004));
854 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
855 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
859 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
861 emitForm_A(i
, HEX64(28000000, 00000003));
865 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
867 emitForm_A(i
, HEX64(70000000, 00000003));
869 if (i
->dType
== TYPE_S32
)
871 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
876 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
878 emitForm_B(i
, HEX64(78000000, 00000003));
880 if (i
->dType
== TYPE_S32
)
882 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
884 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
889 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
891 emitForm_A(i
, HEX64(24000000, 00000004));
893 code
[0] |= i
->subOp
<< 5;
897 CodeEmitterNVC0::emitShift(const Instruction
*i
)
899 if (i
->op
== OP_SHR
) {
900 emitForm_A(i
, HEX64(58000000, 00000003)
901 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
903 emitForm_A(i
, HEX64(60000000, 00000003));
906 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
911 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
913 if (i
->encSize
== 8) {
914 emitForm_B(i
, HEX64(60000000, 00000000));
916 if (i
->op
== OP_PREEX2
)
919 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
920 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
922 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
927 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
929 if (i
->encSize
== 8) {
930 code
[0] = 0x00000000 | (subOp
<< 26);
931 code
[1] = 0xc8000000;
935 defId(i
->def(0), 14);
936 srcId(i
->src(0), 20);
938 assert(i
->src(0).getFile() == FILE_GPR
);
940 if (i
->saturate
) code
[0] |= 1 << 5;
942 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
943 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
945 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
947 assert(!i
->src(0).mod
.neg());
948 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
953 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
957 assert(i
->encSize
== 8);
959 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
964 if (!isFloatType(i
->dType
))
965 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
966 if (i
->dType
== TYPE_F64
)
974 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
977 case ROUND_M
: code
[1] |= 1 << 17; break;
978 case ROUND_P
: code
[1] |= 2 << 17; break;
979 case ROUND_Z
: code
[1] |= 3 << 17; break;
980 case ROUND_NI
: code
[0] |= 1 << 7; break;
981 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
982 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
983 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
986 assert(!"invalid round mode");
992 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
996 case ROUND_MI
: code
[0] |= 1 << 16; break;
998 case ROUND_PI
: code
[0] |= 2 << 16; break;
1000 case ROUND_ZI
: code
[0] |= 3 << 16; break;
1007 CodeEmitterNVC0::emitCVT(Instruction
*i
)
1009 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
1013 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
1014 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
1015 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
1020 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
1021 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
1022 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
1024 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
1029 if (i
->encSize
== 8) {
1030 emitForm_B(i
, HEX64(10000000, 00000004));
1034 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
1035 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
1036 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
1038 // for 8/16 source types, the byte/word is in subOp. word 1 is
1039 // represented as 2.
1040 if (!isFloatType(i
->sType
))
1041 code
[1] |= i
->subOp
<< 0x17;
1043 code
[1] |= i
->subOp
<< 0x18;
1049 if (neg
&& i
->op
!= OP_ABS
)
1055 if (isSignedIntType(dType
))
1057 if (isSignedIntType(i
->sType
))
1060 if (isFloatType(dType
)) {
1061 if (!isFloatType(i
->sType
))
1062 code
[1] |= 0x08000000;
1064 if (isFloatType(i
->sType
))
1065 code
[1] |= 0x04000000;
1067 code
[1] |= 0x0c000000;
1070 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
1073 if (isFloatType(dType
)) {
1074 if (isFloatType(i
->sType
))
1077 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1079 assert(isFloatType(i
->sType
));
1081 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1084 if (neg
) code
[0] |= 1 << 16;
1085 if (sat
) code
[0] |= 1 << 18;
1086 if (abs
) code
[0] |= 1 << 19;
1093 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1098 if (i
->sType
== TYPE_F64
)
1101 if (!isFloatType(i
->sType
))
1104 if (isSignedIntType(i
->sType
))
1106 if (isFloatType(i
->dType
)) {
1107 if (isFloatType(i
->sType
))
1114 case OP_SET_AND
: hi
= 0x10000000; break;
1115 case OP_SET_OR
: hi
= 0x10200000; break;
1116 case OP_SET_XOR
: hi
= 0x10400000; break;
1121 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1123 if (i
->op
!= OP_SET
)
1124 srcId(i
->src(2), 32 + 17);
1126 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1127 if (i
->sType
== TYPE_F32
)
1128 code
[1] += 0x10000000;
1130 code
[1] += 0x08000000;
1132 code
[0] &= ~0xfc000;
1133 defId(i
->def(0), 17);
1134 if (i
->defExists(1))
1135 defId(i
->def(1), 14);
1143 emitCondCode(i
->setCond
, 32 + 23);
1148 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1154 op
= HEX64(30000000, 00000023);
1157 op
= HEX64(30000000, 00000003);
1160 op
= HEX64(38000000, 00000000);
1163 assert(!"invalid type for SLCT");
1169 CondCode cc
= i
->setCond
;
1171 if (i
->src(2).mod
.neg())
1172 cc
= reverseCondCode(cc
);
1174 emitCondCode(cc
, 32 + 23);
1180 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1182 emitForm_A(i
, HEX64(20000000, 00000004));
1184 if (i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1188 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1190 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1191 code
[1] = 0xf0000000;
1193 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1196 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1198 code
[0] = 0x00000086;
1199 code
[1] = 0xd0000000;
1201 code
[1] |= i
->tex
.r
;
1202 code
[1] |= i
->tex
.s
<< 8;
1204 if (i
->tex
.liveOnly
)
1207 defId(i
->def(0), 14);
1208 srcId(i
->src(0), 20);
1212 isNextIndependentTex(const TexInstruction
*i
)
1214 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1216 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1218 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1222 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1224 code
[0] = 0x00000006;
1226 if (isNextIndependentTex(i
))
1227 code
[0] |= 0x080; // t mode
1229 code
[0] |= 0x100; // p mode
1231 if (i
->tex
.liveOnly
)
1235 case OP_TEX
: code
[1] = 0x80000000; break;
1236 case OP_TXB
: code
[1] = 0x84000000; break;
1237 case OP_TXL
: code
[1] = 0x86000000; break;
1238 case OP_TXF
: code
[1] = 0x90000000; break;
1239 case OP_TXG
: code
[1] = 0xa0000000; break;
1240 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1241 case OP_TXD
: code
[1] = 0xe0000000; break;
1243 assert(!"invalid texture op");
1246 if (i
->op
== OP_TXF
) {
1247 if (!i
->tex
.levelZero
)
1248 code
[1] |= 0x02000000;
1250 if (i
->tex
.levelZero
) {
1251 code
[1] |= 0x02000000;
1254 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1257 defId(i
->def(0), 14);
1258 srcId(i
->src(0), 20);
1262 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1264 code
[1] |= i
->tex
.mask
<< 14;
1266 code
[1] |= i
->tex
.r
;
1267 code
[1] |= i
->tex
.s
<< 8;
1268 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1269 code
[1] |= 1 << 18; // in 1st source (with array index)
1272 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1273 if (i
->tex
.target
.isCube())
1275 if (i
->tex
.target
.isArray())
1277 if (i
->tex
.target
.isShadow())
1280 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1282 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1284 if (i
->op
== OP_TXL
)
1285 code
[1] &= ~(1 << 26);
1287 if (i
->op
== OP_TXF
)
1288 code
[1] &= ~(1 << 25);
1290 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1291 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1294 if (i
->tex
.useOffsets
== 1)
1296 if (i
->tex
.useOffsets
== 4)
1303 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1305 code
[0] = 0x00000086;
1306 code
[1] = 0xc0000000;
1308 switch (i
->tex
.query
) {
1309 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1310 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1311 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1312 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1313 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1314 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1316 assert(!"invalid texture query");
1320 code
[1] |= i
->tex
.mask
<< 14;
1322 code
[1] |= i
->tex
.r
;
1323 code
[1] |= i
->tex
.s
<< 8;
1324 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1327 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1329 defId(i
->def(0), 14);
1330 srcId(i
->src(0), 20);
1337 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1339 code
[0] = 0x00000000 | (laneMask
<< 6);
1340 code
[1] = 0x48000000 | qOp
;
1342 defId(i
->def(0), 14);
1343 srcId(i
->src(0), 20);
1344 srcId((i
->srcExists(1) && i
->predSrc
!= 1) ? i
->src(1) : i
->src(0), 26);
1346 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1347 code
[0] |= 1 << 9; // dall
1353 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1355 const FlowInstruction
*f
= i
->asFlow();
1357 unsigned mask
; // bit 0: predicate, bit 1: target
1359 code
[0] = 0x00000007;
1363 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1364 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1369 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1371 code
[0] |= 0x4000; // indirect calls always use c[] source
1375 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1376 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1377 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1378 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1379 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1381 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1382 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1383 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1384 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1386 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1387 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1388 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1390 assert(!"invalid flow operation");
1396 if (i
->flagsSrc
< 0)
1409 if (code
[0] & 0x4000) {
1410 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1411 setAddress16(i
->src(0));
1412 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1413 if (f
->op
== OP_BRA
)
1414 srcId(f
->src(0).getIndirect(0), 20);
1420 if (f
->op
== OP_CALL
) {
1425 assert(f
->absolute
);
1426 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1427 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1428 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1430 assert(!f
->absolute
);
1431 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1432 code
[0] |= (pcRel
& 0x3f) << 26;
1433 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1437 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1438 if (writeIssueDelays
&& !(f
->target
.bb
->binPos
& 0x3f))
1440 // currently we don't want absolute branches
1441 assert(!f
->absolute
);
1442 code
[0] |= (pcRel
& 0x3f) << 26;
1443 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1448 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1450 Value
*rDef
= NULL
, *pDef
= NULL
;
1453 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1454 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1455 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1456 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1459 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1462 code
[1] = 0x50000000;
1464 code
[0] |= 63 << 14;
1470 if (i
->src(0).getFile() == FILE_GPR
) {
1471 srcId(i
->src(0), 20);
1473 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1475 code
[0] |= imm
->reg
.data
.u32
<< 20;
1480 if (i
->src(1).getFile() == FILE_GPR
) {
1481 srcId(i
->src(1), 26);
1483 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1485 assert(imm
->reg
.data
.u32
<= 0xfff);
1486 code
[0] |= imm
->reg
.data
.u32
<< 26;
1487 code
[1] |= imm
->reg
.data
.u32
>> 6;
1491 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1492 srcId(i
->src(2), 32 + 17);
1493 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1499 if (i
->defExists(0)) {
1500 if (i
->def(0).getFile() == FILE_GPR
)
1501 rDef
= i
->getDef(0);
1503 pDef
= i
->getDef(0);
1505 if (i
->defExists(1)) {
1506 if (i
->def(1).getFile() == FILE_GPR
)
1507 rDef
= i
->getDef(1);
1509 pDef
= i
->getDef(1);
1513 code
[0] &= ~(63 << 14);
1517 code
[1] &= ~(7 << 21);
1518 defId(pDef
, 32 + 21);
1523 CodeEmitterNVC0::emitAFETCH(const Instruction
*i
)
1525 code
[0] = 0x00000006;
1526 code
[1] = 0x0c000000 | (i
->src(0).get()->reg
.data
.offset
& 0x7ff);
1528 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1533 defId(i
->def(0), 14);
1534 srcId(i
->src(0).getIndirect(0), 20);
1538 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1540 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1542 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1543 code
[1] = 0x00000000 | (prim
>> 6);
1547 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1549 defId(i
->def(0), 14);
1554 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1556 code
[0] = 0x00000006;
1557 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1561 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1562 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1566 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1568 defId(i
->def(0), 14);
1569 srcId(i
->src(0).getIndirect(0), 20);
1570 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1574 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1576 unsigned int size
= typeSizeof(i
->dType
);
1578 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1579 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1581 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1588 assert(i
->src(1).getFile() == FILE_GPR
);
1590 srcId(i
->src(0).getIndirect(0), 20);
1591 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1592 srcId(i
->src(1), 26);
1596 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1598 code
[0] = 0x00000006;
1599 code
[1] = 0x1c000000;
1603 defId(i
->def(0), 14); // new secret address
1604 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1606 assert(i
->src(0).getFile() == FILE_GPR
);
1608 if (i
->op
== OP_EMIT
)
1610 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1614 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1615 unsigned int stream
= SDATA(i
->src(1)).u32
;
1619 code
[0] |= stream
<< 26;
1624 srcId(i
->src(1), 26);
1629 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1631 if (i
->encSize
== 8) {
1632 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1634 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1636 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1641 interpApply(const InterpEntry
*entry
, uint32_t *code
,
1642 bool force_persample_interp
, bool flatshade
)
1644 int ipa
= entry
->ipa
;
1645 int reg
= entry
->reg
;
1646 int loc
= entry
->loc
;
1649 (ipa
& NV50_IR_INTERP_MODE_MASK
) == NV50_IR_INTERP_SC
) {
1650 ipa
= NV50_IR_INTERP_FLAT
;
1652 } else if (force_persample_interp
&&
1653 (ipa
& NV50_IR_INTERP_SAMPLE_MASK
) == NV50_IR_INTERP_DEFAULT
&&
1654 (ipa
& NV50_IR_INTERP_MODE_MASK
) != NV50_IR_INTERP_FLAT
) {
1655 ipa
|= NV50_IR_INTERP_CENTROID
;
1657 code
[loc
+ 0] &= ~(0xf << 6);
1658 code
[loc
+ 0] |= ipa
<< 6;
1659 code
[loc
+ 0] &= ~(0x3f << 26);
1660 code
[loc
+ 0] |= reg
<< 26;
1664 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1666 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1668 if (i
->encSize
== 8) {
1669 code
[0] = 0x00000000;
1670 code
[1] = 0xc0000000 | (base
& 0xffff);
1675 if (i
->op
== OP_PINTERP
) {
1676 srcId(i
->src(1), 26);
1677 addInterp(i
->ipa
, SDATA(i
->src(1)).id
, interpApply
);
1679 code
[0] |= 0x3f << 26;
1680 addInterp(i
->ipa
, 0x3f, interpApply
);
1683 srcId(i
->src(0).getIndirect(0), 20);
1685 assert(i
->op
== OP_PINTERP
);
1686 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1687 srcId(i
->src(1), 20);
1692 defId(i
->def(0), 14);
1694 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1695 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1697 code
[1] |= 0x3f << 17;
1701 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1734 assert(!"invalid type");
1741 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1762 assert(!"invalid caching mode");
1769 uses64bitAddress(const Instruction
*ldst
)
1771 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1772 ldst
->src(0).isIndirect(0) &&
1773 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1777 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1781 switch (i
->src(0).getFile()) {
1782 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1783 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1784 case FILE_MEMORY_SHARED
:
1785 if (i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1786 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1795 assert(!"invalid memory file");
1799 code
[0] = 0x00000005;
1802 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
) {
1803 // Unlocked store on shared memory can fail.
1804 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
&&
1805 i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
) {
1806 assert(i
->defExists(0));
1807 defId(i
->def(0), 8);
1811 setAddressByFile(i
->src(0));
1812 srcId(i
->src(1), 14);
1813 srcId(i
->src(0).getIndirect(0), 20);
1814 if (uses64bitAddress(i
))
1819 emitLoadStoreType(i
->dType
);
1820 emitCachingMode(i
->cache
);
1824 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1828 code
[0] = 0x00000005;
1830 switch (i
->src(0).getFile()) {
1831 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1832 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1833 case FILE_MEMORY_SHARED
:
1834 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1835 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1843 case FILE_MEMORY_CONST
:
1844 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1845 emitMOV(i
); // not sure if this is any better
1848 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1849 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1852 assert(!"invalid memory file");
1858 if (i
->src(0).getFile() == FILE_MEMORY_SHARED
) {
1859 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
) {
1860 assert(i
->defExists(1));
1861 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
1862 defId(i
->def(1), 8);
1864 defId(i
->def(1), 32 + 18);
1868 defId(i
->def(0), 14);
1870 setAddressByFile(i
->src(0));
1871 srcId(i
->src(0).getIndirect(0), 20);
1872 if (uses64bitAddress(i
))
1877 emitLoadStoreType(i
->dType
);
1878 emitCachingMode(i
->cache
);
1882 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1884 switch (SDATA(ref
).sv
.sv
) {
1885 case SV_LANEID
: return 0x00;
1886 case SV_PHYSID
: return 0x03;
1887 case SV_VERTEX_COUNT
: return 0x10;
1888 case SV_INVOCATION_ID
: return 0x11;
1889 case SV_YDIR
: return 0x12;
1890 case SV_THREAD_KILL
: return 0x13;
1891 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1892 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1893 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1894 case SV_GRIDID
: return 0x2c;
1895 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1896 case SV_LBASE
: return 0x34;
1897 case SV_SBASE
: return 0x30;
1898 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1900 assert(!"no sreg for system value");
1906 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1908 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1909 if (i
->src(0).getFile() == FILE_GPR
) {
1910 code
[0] = 0xfc01c003;
1911 code
[1] = 0x1a8e0000;
1912 srcId(i
->src(0), 20);
1914 code
[0] = 0x0001c004;
1915 code
[1] = 0x0c0e0000;
1916 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1918 if (!i
->getSrc(0)->reg
.data
.u32
)
1921 srcId(i
->src(0), 20);
1924 defId(i
->def(0), 17);
1927 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1928 uint8_t sr
= getSRegEncoding(i
->src(0));
1930 if (i
->encSize
== 8) {
1931 code
[0] = 0x00000004 | (sr
<< 26);
1932 code
[1] = 0x2c000000;
1934 code
[0] = 0x40000008 | (sr
<< 20);
1936 defId(i
->def(0), 14);
1940 if (i
->encSize
== 8) {
1943 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1944 opc
= HEX64(18000000, 000001e2
);
1946 if (i
->src(0).getFile() == FILE_PREDICATE
)
1947 opc
= HEX64(080e0000
, 1c000004
);
1949 opc
= HEX64(28000000, 00000004);
1951 opc
|= i
->lanes
<< 5;
1957 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1958 imm
= SDATA(i
->src(0)).u32
;
1959 if (imm
& 0xfff00000) {
1960 assert(!(imm
& 0x000fffff));
1961 code
[0] = 0x00000318 | imm
;
1963 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1964 code
[0] = 0x00000118 | (imm
<< 20);
1968 emitShortSrc2(i
->src(0));
1970 defId(i
->def(0), 14);
1977 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
1979 const bool hasDst
= i
->defExists(0);
1980 const bool casOrExch
=
1981 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
1982 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
1984 if (i
->dType
== TYPE_U64
) {
1986 case NV50_IR_SUBOP_ATOM_ADD
:
1989 code
[1] = 0x507e0000;
1991 code
[1] = 0x10000000;
1993 case NV50_IR_SUBOP_ATOM_EXCH
:
1995 code
[1] = 0x507e0000;
1997 case NV50_IR_SUBOP_ATOM_CAS
:
1999 code
[1] = 0x50000000;
2002 assert(!"invalid u64 red op");
2006 if (i
->dType
== TYPE_U32
) {
2008 case NV50_IR_SUBOP_ATOM_EXCH
:
2010 code
[1] = 0x507e0000;
2012 case NV50_IR_SUBOP_ATOM_CAS
:
2014 code
[1] = 0x50000000;
2017 code
[0] = 0x5 | (i
->subOp
<< 5);
2019 code
[1] = 0x507e0000;
2021 code
[1] = 0x10000000;
2025 if (i
->dType
== TYPE_S32
) {
2026 assert(i
->subOp
<= 2);
2027 code
[0] = 0x205 | (i
->subOp
<< 5);
2029 code
[1] = 0x587e0000;
2031 code
[1] = 0x18000000;
2033 if (i
->dType
== TYPE_F32
) {
2034 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
2037 code
[1] = 0x687e0000;
2039 code
[1] = 0x28000000;
2044 srcId(i
->src(1), 14);
2047 defId(i
->def(0), 32 + 11);
2050 code
[1] |= 63 << 11;
2052 if (hasDst
|| casOrExch
) {
2053 const int32_t offset
= SDATA(i
->src(0)).offset
;
2054 assert(offset
< 0x80000 && offset
>= -0x80000);
2055 code
[0] |= offset
<< 26;
2056 code
[1] |= (offset
& 0x1ffc0) >> 6;
2057 code
[1] |= (offset
& 0xe0000) << 6;
2059 srcAddr32(i
->src(0), 26, 0);
2061 if (i
->getIndirect(0, 0)) {
2062 srcId(i
->getIndirect(0, 0), 20);
2063 if (i
->getIndirect(0, 0)->reg
.size
== 8)
2066 code
[0] |= 63 << 20;
2069 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
2070 assert(i
->src(1).getSize() == 2 * typeSizeof(i
->sType
));
2071 code
[1] |= (SDATA(i
->src(1)).id
+ 1) << 17;
2076 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
2078 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
2079 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
2080 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
2083 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
2086 code
[1] = 0xe0000000;
2092 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
2094 code
[0] = 0x00000005 | (i
->subOp
<< 5);
2096 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
2097 code
[1] = 0x98000000;
2098 srcAddr32(i
->src(0), 28, 2);
2100 code
[1] = 0xd0000000;
2101 setAddress24(i
->src(0));
2103 if (uses64bitAddress(i
))
2105 srcId(i
->src(0).getIndirect(0), 20);
2113 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
2116 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
2117 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
2118 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
2119 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
2120 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
2121 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
2122 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
2123 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
2124 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
2125 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
2126 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
2127 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
2128 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
2129 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
2130 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
2131 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
2136 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
2141 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
2143 ImmediateValue
*imm
= NULL
;
2146 if (i
->srcExists(2)) {
2147 imm
= i
->getSrc(2)->asImm();
2149 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
2153 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
2154 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
2155 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
2162 if (i
->op
== OP_SUCLAMP
) {
2163 if (i
->dType
== TYPE_S32
)
2165 emitSUCLAMPMode(i
->subOp
);
2168 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2171 if (i
->op
!= OP_SUEAU
) {
2172 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2173 code
[0] |= 63 << 14;
2174 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2176 if (i
->defExists(1)) { // r, p
2177 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2178 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2184 assert(i
->op
== OP_SUCLAMP
);
2186 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2191 CodeEmitterNVC0::emitSUGType(DataType ty
)
2194 case TYPE_S32
: code
[1] |= 1 << 13; break;
2195 case TYPE_U8
: code
[1] |= 2 << 13; break;
2196 case TYPE_S8
: code
[1] |= 3 << 13; break;
2198 assert(ty
== TYPE_U32
);
2204 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2206 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2208 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2209 assert(offset
== (offset
& 0xfffc));
2212 code
[0] |= offset
<< 24;
2213 code
[1] |= offset
>> 8;
2214 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2218 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2220 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2221 code
[1] |= 0x7 << 17;
2223 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2225 srcId(i
->src(s
), 32 + 17);
2230 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2233 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2235 emitLoadStoreType(i
->dType
);
2236 emitSUGType(i
->sType
);
2237 emitCachingMode(i
->cache
);
2240 defId(i
->def(0), 14); // destination
2241 srcId(i
->src(0), 20); // address
2243 if (i
->src(1).getFile() == FILE_GPR
)
2244 srcId(i
->src(1), 26);
2251 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2254 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2256 if (i
->op
== OP_SUSTP
)
2257 code
[1] |= i
->tex
.mask
<< 22;
2259 emitLoadStoreType(i
->dType
);
2260 emitSUGType(i
->sType
);
2261 emitCachingMode(i
->cache
);
2264 srcId(i
->src(0), 20); // address
2266 if (i
->src(1).getFile() == FILE_GPR
)
2267 srcId(i
->src(1), 26);
2270 srcId(i
->src(3), 14); // values
2275 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2277 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2279 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2280 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2281 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2282 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2285 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2286 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2287 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2288 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2289 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2290 code
[1] |= (i
->mask
& 0x3) << 2;
2293 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2294 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2295 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2296 code
[1] |= (i
->mask
& 0x3) << 2;
2297 code
[1] |= (i
->mask
& 0xc) << 21;
2306 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2310 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2311 case 0: opc
|= 0xe8ULL
<< 56; break;
2312 case 1: opc
|= 0xb4ULL
<< 56; break;
2313 case 2: opc
|= 0x94ULL
<< 56; break;
2318 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2319 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2320 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2322 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2323 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2330 if (i
->flagsDef
>= 0)
2335 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2337 assert(i
->encSize
== 8);
2338 emitForm_A(i
, HEX64(10000000, 00000006));
2339 code
[0] |= i
->subOp
<< 5;
2340 code
[1] |= 0x00e00000;
2344 CodeEmitterNVC0::emitVOTE(const Instruction
*i
)
2346 assert(i
->src(0).getFile() == FILE_PREDICATE
&&
2347 i
->def(1).getFile() == FILE_PREDICATE
);
2349 code
[0] = 0x00000004 | (i
->subOp
<< 5);
2350 code
[1] = 0x48000000;
2354 defId(i
->def(0), 14);
2355 defId(i
->def(1), 32 + 22);
2356 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
2358 srcId(i
->src(0), 20);
2362 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2364 unsigned int size
= insn
->encSize
;
2366 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2369 if (!insn
->encSize
) {
2370 ERROR("skipping unencodable instruction: "); insn
->print();
2373 if (codeSize
+ size
> codeSizeLimit
) {
2374 ERROR("code emitter output buffer too small\n");
2378 if (writeIssueDelays
) {
2379 if (!(codeSize
& 0x3f)) {
2380 code
[0] = 0x00000007; // cf issue delay "instruction"
2381 code
[1] = 0x20000000;
2385 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2386 uint32_t *data
= code
- (id
* 2 + 2);
2388 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2391 data
[0] |= insn
->sched
<< 28;
2392 data
[1] |= insn
->sched
>> 4;
2394 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2398 // assert that instructions with multiple defs don't corrupt registers
2399 for (int d
= 0; insn
->defExists(d
); ++d
)
2400 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2437 if (insn
->dType
== TYPE_F64
)
2439 else if (isFloatType(insn
->dType
))
2445 if (insn
->dType
== TYPE_F64
)
2447 else if (isFloatType(insn
->dType
))
2454 if (insn
->dType
== TYPE_F64
)
2456 else if (isFloatType(insn
->dType
))
2468 emitLogicOp(insn
, 0);
2471 emitLogicOp(insn
, 1);
2474 emitLogicOp(insn
, 2);
2484 emitSET(insn
->asCmp());
2490 emitSLCT(insn
->asCmp());
2505 if (insn
->def(0).getFile() == FILE_PREDICATE
||
2506 insn
->src(0).getFile() == FILE_PREDICATE
)
2512 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
2515 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
2540 emitTEX(insn
->asTex());
2543 emitTXQ(insn
->asTex());
2557 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2558 emitSULDGB(insn
->asTex());
2560 ERROR("SULDB not yet supported on < nve4\n");
2564 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2565 emitSUSTGx(insn
->asTex());
2567 ERROR("SUSTx not yet supported on < nve4\n");
2589 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2592 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2595 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2637 ERROR("operation should have been eliminated");
2643 ERROR("operation should have been lowered\n");
2646 ERROR("unknown op: %u\n", insn
->op
);
2652 assert(insn
->encSize
== 8);
2655 code
+= insn
->encSize
/ 4;
2656 codeSize
+= insn
->encSize
;
2661 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2663 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2665 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2668 if (i
->ftz
|| i
->saturate
|| i
->join
)
2670 if (i
->rnd
!= ROUND_N
)
2672 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2675 if (i
->op
== OP_PINTERP
) {
2676 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2679 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2683 for (int s
= 0; i
->srcExists(s
); ++s
) {
2684 if (i
->src(s
).isIndirect(0))
2687 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2688 if (SDATA(i
->src(s
)).offset
>= 0x100)
2690 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2691 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2694 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2695 if (i
->dType
== TYPE_F32
) {
2696 if (SDATA(i
->src(s
)).u32
>= 0x100)
2699 if (SDATA(i
->src(s
)).u32
> 0xff)
2704 if (i
->op
== OP_CVT
)
2706 if (i
->src(s
).mod
!= Modifier(0)) {
2707 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2708 if (i
->op
!= OP_RSQ
)
2710 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2711 if (i
->op
!= OP_ADD
|| s
!= 0)
2719 // Simplified, erring on safe side.
2720 class SchedDataCalculator
: public Pass
2723 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2729 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2730 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2731 int tex
; // TEX to non-TEX delay 17 (0x11)
2732 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2733 int imul
; // integer MUL to MUL delay 3
2743 void rebase(const int base
)
2745 const int delta
= this->base
- base
;
2750 for (int i
= 0; i
< regs
; ++i
) {
2754 for (int i
= 0; i
< 8; ++i
) {
2761 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2771 memset(&rd
, 0, sizeof(rd
));
2772 memset(&wr
, 0, sizeof(wr
));
2773 memset(&res
, 0, sizeof(res
));
2776 int getLatest(const ScoreData
& d
) const
2779 for (int i
= 0; i
< regs
; ++i
)
2782 for (int i
= 0; i
< 8; ++i
)
2789 inline int getLatestRd() const
2791 return getLatest(rd
);
2793 inline int getLatestWr() const
2795 return getLatest(wr
);
2797 inline int getLatest() const
2799 const int a
= getLatestRd();
2800 const int b
= getLatestWr();
2802 int max
= MAX2(a
, b
);
2803 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2804 max
= MAX2(res
.ld
[f
], max
);
2805 max
= MAX2(res
.st
[f
], max
);
2807 max
= MAX2(res
.sfu
, max
);
2808 max
= MAX2(res
.imul
, max
);
2809 max
= MAX2(res
.tex
, max
);
2812 void setMax(const RegScores
*that
)
2814 for (int i
= 0; i
< regs
; ++i
) {
2815 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
2816 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
2818 for (int i
= 0; i
< 8; ++i
) {
2819 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
2820 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
2822 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
2823 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
2825 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2826 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
2827 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
2829 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
2830 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
2831 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
2833 void print(int cycle
)
2835 for (int i
= 0; i
< regs
; ++i
) {
2836 if (rd
.r
[i
] > cycle
)
2837 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
2838 if (wr
.r
[i
] > cycle
)
2839 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
2841 for (int i
= 0; i
< 8; ++i
) {
2842 if (rd
.p
[i
] > cycle
)
2843 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
2844 if (wr
.p
[i
] > cycle
)
2845 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
2848 INFO("rd $c @ %i\n", rd
.c
);
2850 INFO("wr $c @ %i\n", wr
.c
);
2851 if (res
.sfu
> cycle
)
2852 INFO("sfu @ %i\n", res
.sfu
);
2853 if (res
.imul
> cycle
)
2854 INFO("imul @ %i\n", res
.imul
);
2855 if (res
.tex
> cycle
)
2856 INFO("tex @ %i\n", res
.tex
);
2860 RegScores
*score
; // for current BB
2861 std::vector
<RegScores
> scoreBoards
;
2867 bool visit(Function
*);
2868 bool visit(BasicBlock
*);
2870 void commitInsn(const Instruction
*, int cycle
);
2871 int calcDelay(const Instruction
*, int cycle
) const;
2872 void setDelay(Instruction
*, int delay
, Instruction
*next
);
2874 void recordRd(const Value
*, const int ready
);
2875 void recordWr(const Value
*, const int ready
);
2876 void checkRd(const Value
*, int cycle
, int& delay
) const;
2877 void checkWr(const Value
*, int cycle
, int& delay
) const;
2879 int getCycles(const Instruction
*, int origDelay
) const;
2883 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
2885 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
2886 delay
= MAX2(delay
, 14);
2888 if (insn
->op
== OP_TEXBAR
) {
2889 // TODO: except if results not used before EXIT
2892 if (insn
->op
== OP_JOIN
|| insn
->join
) {
2895 if (delay
>= 0 || prevData
== 0x04 ||
2896 !next
|| !targ
->canDualIssue(insn
, next
)) {
2897 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
2898 if (prevOp
== OP_EXPORT
)
2899 insn
->sched
|= 0x40;
2901 insn
->sched
|= 0x20;
2903 insn
->sched
= 0x04; // dual-issue
2906 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
2907 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
2910 prevData
= insn
->sched
;
2914 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
2916 if (insn
->sched
& 0x80) {
2917 int c
= (insn
->sched
& 0x0f) * 2 + 1;
2918 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
2922 if (insn
->sched
& 0x60)
2923 return (insn
->sched
& 0x1f) + 1;
2924 return (insn
->sched
== 0x04) ? 0 : 32;
2928 SchedDataCalculator::visit(Function
*func
)
2930 int regs
= targ
->getFileSize(FILE_GPR
) + 1;
2931 scoreBoards
.resize(func
->cfg
.getSize());
2932 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
2933 scoreBoards
[i
].wipe(regs
);
2938 SchedDataCalculator::visit(BasicBlock
*bb
)
2941 Instruction
*next
= NULL
;
2947 score
= &scoreBoards
.at(bb
->getId());
2949 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
2950 // back branches will wait until all target dependencies are satisfied
2951 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
2953 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
2954 if (in
->getExit()) {
2955 if (prevData
!= 0x04)
2956 prevData
= in
->getExit()->sched
;
2957 prevOp
= in
->getExit()->op
;
2959 score
->setMax(&scoreBoards
.at(in
->getId()));
2961 if (bb
->cfg
.incidentCount() > 1)
2964 #ifdef NVC0_DEBUG_SCHED_DATA
2965 INFO("=== BB:%i initial scores\n", bb
->getId());
2966 score
->print(cycle
);
2969 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
2972 commitInsn(insn
, cycle
);
2973 int delay
= calcDelay(next
, cycle
);
2974 setDelay(insn
, delay
, next
);
2975 cycle
+= getCycles(insn
, delay
);
2977 #ifdef NVC0_DEBUG_SCHED_DATA
2978 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
2985 commitInsn(insn
, cycle
);
2989 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
2990 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
2992 if (ei
.getType() != Graph::Edge::BACK
) {
2993 // only test the first instruction of the outgoing block
2994 next
= out
->getEntry();
2996 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
2998 // wait until all dependencies are satisfied
2999 const int regsFree
= score
->getLatest();
3000 next
= out
->getFirst();
3001 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
3002 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
3003 c
+= getCycles(next
, bbDelay
);
3008 if (bb
->cfg
.outgoingCount() != 1)
3010 setDelay(insn
, bbDelay
, next
);
3011 cycle
+= getCycles(insn
, bbDelay
);
3013 score
->rebase(cycle
); // common base for initializing out blocks' scores
3017 #define NVE4_MAX_ISSUE_DELAY 0x1f
3019 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
3021 int delay
= 0, ready
= cycle
;
3023 for (int s
= 0; insn
->srcExists(s
); ++s
)
3024 checkRd(insn
->getSrc(s
), cycle
, delay
);
3025 // WAR & WAW don't seem to matter
3026 // for (int s = 0; insn->srcExists(s); ++s)
3027 // recordRd(insn->getSrc(s), cycle);
3029 switch (Target::getOpClass(insn
->op
)) {
3031 ready
= score
->res
.sfu
;
3034 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3035 ready
= score
->res
.imul
;
3037 case OPCLASS_TEXTURE
:
3038 ready
= score
->res
.tex
;
3041 ready
= score
->res
.ld
[insn
->src(0).getFile()];
3044 ready
= score
->res
.st
[insn
->src(0).getFile()];
3049 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
3050 ready
= MAX2(ready
, score
->res
.tex
);
3052 delay
= MAX2(delay
, ready
- cycle
);
3054 // if can issue next cycle, delay is 0, not 1
3055 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
3059 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
3061 const int ready
= cycle
+ targ
->getLatency(insn
);
3063 for (int d
= 0; insn
->defExists(d
); ++d
)
3064 recordWr(insn
->getDef(d
), ready
);
3065 // WAR & WAW don't seem to matter
3066 // for (int s = 0; insn->srcExists(s); ++s)
3067 // recordRd(insn->getSrc(s), cycle);
3069 switch (Target::getOpClass(insn
->op
)) {
3071 score
->res
.sfu
= cycle
+ 4;
3074 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3075 score
->res
.imul
= cycle
+ 4;
3077 case OPCLASS_TEXTURE
:
3078 score
->res
.tex
= cycle
+ 18;
3081 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
3083 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
3084 score
->res
.st
[insn
->src(0).getFile()] = ready
;
3087 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
3088 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
3091 if (insn
->op
== OP_TEXBAR
)
3092 score
->res
.tex
= cycle
;
3098 #ifdef NVC0_DEBUG_SCHED_DATA
3099 score
->print(cycle
);
3104 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
3109 switch (v
->reg
.file
) {
3112 b
= a
+ v
->reg
.size
/ 4;
3113 for (int r
= a
; r
< b
; ++r
)
3114 ready
= MAX2(ready
, score
->rd
.r
[r
]);
3116 case FILE_PREDICATE
:
3117 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
3120 ready
= MAX2(ready
, score
->rd
.c
);
3122 case FILE_SHADER_INPUT
:
3123 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
3124 case FILE_MEMORY_LOCAL
:
3125 case FILE_MEMORY_CONST
:
3126 case FILE_MEMORY_SHARED
:
3127 case FILE_MEMORY_GLOBAL
:
3128 case FILE_SYSTEM_VALUE
:
3129 // TODO: any restrictions here ?
3131 case FILE_IMMEDIATE
:
3138 delay
= MAX2(delay
, ready
- cycle
);
3142 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
3147 switch (v
->reg
.file
) {
3150 b
= a
+ v
->reg
.size
/ 4;
3151 for (int r
= a
; r
< b
; ++r
)
3152 ready
= MAX2(ready
, score
->wr
.r
[r
]);
3154 case FILE_PREDICATE
:
3155 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
3158 assert(v
->reg
.file
== FILE_FLAGS
);
3159 ready
= MAX2(ready
, score
->wr
.c
);
3163 delay
= MAX2(delay
, ready
- cycle
);
3167 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
3169 int a
= v
->reg
.data
.id
;
3171 if (v
->reg
.file
== FILE_GPR
) {
3172 int b
= a
+ v
->reg
.size
/ 4;
3173 for (int r
= a
; r
< b
; ++r
)
3174 score
->rd
.r
[r
] = ready
;
3176 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
3177 if (v
->reg
.file
== FILE_PREDICATE
) {
3178 score
->rd
.p
[a
] = ready
+ 4;
3180 assert(v
->reg
.file
== FILE_FLAGS
);
3181 score
->rd
.c
= ready
+ 4;
3186 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
3188 int a
= v
->reg
.data
.id
;
3190 if (v
->reg
.file
== FILE_GPR
) {
3191 int b
= a
+ v
->reg
.size
/ 4;
3192 for (int r
= a
; r
< b
; ++r
)
3193 score
->wr
.r
[r
] = ready
;
3195 if (v
->reg
.file
== FILE_PREDICATE
) {
3196 score
->wr
.p
[a
] = ready
;
3198 if (v
->reg
.file
== FILE_FLAGS
) {
3199 score
->wr
.c
= ready
;
3204 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
3206 SchedDataCalculator
sched(targ
);
3207 return sched
.run(func
, true, true);
3211 CodeEmitterNVC0::prepareEmission(Function
*func
)
3213 CodeEmitter::prepareEmission(func
);
3215 if (targ
->hasSWSched
)
3216 calculateSchedDataNVC0(targ
, func
);
3219 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3220 : CodeEmitter(target
),
3222 writeIssueDelays(target
->hasSWSched
)
3225 codeSize
= codeSizeLimit
= 0;
3230 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3232 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3233 emit
->setProgramType(type
);
3238 TargetNVC0::getCodeEmitter(Program::Type type
)
3240 if (chipset
>= NVISA_GK20A_CHIPSET
)
3241 return createCodeEmitterGK110(type
);
3242 return createCodeEmitterNVC0(type
);
3245 } // namespace nv50_ir