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 inline void defId(const ValueDef
&, const int pos
);
146 inline void defId(const Instruction
*, int d
, const int pos
);
147 inline void srcId(const ValueRef
&, const int pos
);
148 inline void srcId(const ValueRef
*, const int pos
);
149 inline void srcId(const Instruction
*, int s
, const int pos
);
150 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
152 inline bool isLIMM(const ValueRef
&, DataType ty
);
155 // for better visibility
156 #define HEX64(h, l) 0x##h##l##ULL
158 #define SDATA(a) ((a).rep()->reg.data)
159 #define DDATA(a) ((a).rep()->reg.data)
161 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
163 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
166 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
168 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
171 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
173 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
174 code
[pos
/ 32] |= r
<< (pos
% 32);
178 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
180 const uint32_t offset
= SDATA(src
).offset
>> shr
;
182 code
[pos
/ 32] |= offset
<< (pos
% 32);
183 if (pos
&& (pos
< 32))
184 code
[1] |= offset
>> (32 - pos
);
187 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
189 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
192 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
194 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
195 code
[pos
/ 32] |= r
<< (pos
% 32);
198 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
200 const ImmediateValue
*imm
= ref
.get()->asImm();
202 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
206 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
209 case ROUND_M
: code
[1] |= 1 << 23; break;
210 case ROUND_P
: code
[1] |= 2 << 23; break;
211 case ROUND_Z
: code
[1] |= 3 << 23; break;
213 assert(insn
->rnd
== ROUND_N
);
219 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
221 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
222 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
223 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
224 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
227 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
232 case CC_LT
: val
= 0x1; break;
233 case CC_LTU
: val
= 0x9; break;
234 case CC_EQ
: val
= 0x2; break;
235 case CC_EQU
: val
= 0xa; break;
236 case CC_LE
: val
= 0x3; break;
237 case CC_LEU
: val
= 0xb; break;
238 case CC_GT
: val
= 0x4; break;
239 case CC_GTU
: val
= 0xc; break;
240 case CC_NE
: val
= 0x5; break;
241 case CC_NEU
: val
= 0xd; break;
242 case CC_GE
: val
= 0x6; break;
243 case CC_GEU
: val
= 0xe; break;
244 case CC_TR
: val
= 0xf; break;
245 case CC_FL
: val
= 0x0; break;
247 case CC_A
: val
= 0x14; break;
248 case CC_NA
: val
= 0x13; break;
249 case CC_S
: val
= 0x15; break;
250 case CC_NS
: val
= 0x12; break;
251 case CC_C
: val
= 0x16; break;
252 case CC_NC
: val
= 0x11; break;
253 case CC_O
: val
= 0x17; break;
254 case CC_NO
: val
= 0x10; break;
258 assert(!"invalid condition code");
261 code
[pos
/ 32] |= val
<< (pos
% 32);
265 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
267 if (i
->predSrc
>= 0) {
268 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
269 srcId(i
->src(i
->predSrc
), 10);
270 if (i
->cc
== CC_NOT_P
)
271 code
[0] |= 0x2000; // negate
278 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
280 switch (src
.getFile()) {
281 case FILE_MEMORY_GLOBAL
:
282 srcAddr32(src
, 26, 0);
284 case FILE_MEMORY_LOCAL
:
285 case FILE_MEMORY_SHARED
:
289 assert(src
.getFile() == FILE_MEMORY_CONST
);
296 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
298 Symbol
*sym
= src
.get()->asSym();
302 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
303 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
307 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
309 Symbol
*sym
= src
.get()->asSym();
313 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
314 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
318 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
320 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
324 u32
= imm
->reg
.data
.u32
;
326 if ((code
[0] & 0xf) == 0x1) {
328 uint64_t u64
= imm
->reg
.data
.u64
;
329 assert(!(u64
& 0x00000fffffffffffULL
));
330 assert(!(code
[1] & 0xc000));
331 code
[0] |= ((u64
>> 44) & 0x3f) << 26;
332 code
[1] |= 0xc000 | (u64
>> 50);
334 if ((code
[0] & 0xf) == 0x2) {
336 code
[0] |= (u32
& 0x3f) << 26;
339 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
341 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
342 assert(!(code
[1] & 0xc000));
344 code
[0] |= (u32
& 0x3f) << 26;
345 code
[1] |= 0xc000 | (u32
>> 6);
348 assert(!(u32
& 0x00000fff));
349 assert(!(code
[1] & 0xc000));
350 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
351 code
[1] |= 0xc000 | (u32
>> 18);
355 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
357 const ImmediateValue
*imm
= ref
.get()->asImm();
359 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
361 assert(s8
== imm
->reg
.data
.s32
);
363 code
[0] |= (s8
& 0x3f) << 26;
364 code
[0] |= (s8
>> 6) << 8;
368 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
375 defId(i
->def(0), 14);
378 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
381 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
382 switch (i
->getSrc(s
)->reg
.file
) {
383 case FILE_MEMORY_CONST
:
384 assert(!(code
[1] & 0xc000));
385 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
386 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
387 setAddress16(i
->src(s
));
391 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
392 assert(!(code
[1] & 0xc000));
396 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
398 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
401 // ignore here, can be predicate or flags, but must not be address
408 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
415 defId(i
->def(0), 14);
417 switch (i
->src(0).getFile()) {
418 case FILE_MEMORY_CONST
:
419 assert(!(code
[1] & 0xc000));
420 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
421 setAddress16(i
->src(0));
424 assert(!(code
[1] & 0xc000));
428 srcId(i
->src(0), 26);
431 // ignore here, can be predicate or flags, but must not be address
437 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
442 if (opc
== 0x0d || opc
== 0x0e)
445 defId(i
->def(0), 14);
446 srcId(i
->src(0), 20);
448 assert(pred
|| (i
->predSrc
< 0));
452 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
453 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
454 assert(!(code
[0] & (0x300 >> ss2a
)));
455 switch (i
->src(s
).get()->reg
.fileIndex
) {
456 case 0: code
[0] |= 0x100 >> ss2a
; break;
457 case 1: code
[0] |= 0x200 >> ss2a
; break;
458 case 16: code
[0] |= 0x300 >> ss2a
; break;
460 ERROR("invalid c[] space for short form\n");
464 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
466 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
468 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
470 setImmediateS8(i
->src(s
));
472 if (i
->src(s
).getFile() == FILE_GPR
) {
473 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
479 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
481 if (src
.getFile() == FILE_MEMORY_CONST
) {
482 switch (src
.get()->reg
.fileIndex
) {
483 case 0: code
[0] |= 0x100; break;
484 case 1: code
[0] |= 0x200; break;
485 case 16: code
[0] |= 0x300; break;
487 assert(!"unsupported file index for short op");
490 srcAddr32(src
, 20, 2);
493 assert(src
.getFile() == FILE_GPR
);
498 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
500 code
[0] = 0x000001e4;
501 code
[1] = 0x40000000;
506 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
508 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
510 if (i
->encSize
== 8) {
511 if (isLIMM(i
->src(1), TYPE_F32
)) {
512 emitForm_A(i
, HEX64(20000000, 00000002));
514 emitForm_A(i
, HEX64(30000000, 00000000));
516 if (i
->src(2).mod
.neg())
529 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
530 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
538 CodeEmitterNVC0::emitDMAD(const Instruction
*i
)
540 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
542 emitForm_A(i
, HEX64(20000000, 00000001));
544 if (i
->src(2).mod
.neg())
552 assert(!i
->saturate
);
557 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
559 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
561 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
563 if (i
->encSize
== 8) {
564 if (isLIMM(i
->src(1), TYPE_F32
)) {
565 assert(i
->postFactor
== 0); // constant folded, hopefully
566 emitForm_A(i
, HEX64(30000000, 00000002));
568 emitForm_A(i
, HEX64(58000000, 00000000));
570 code
[1] |= ((i
->postFactor
> 0) ?
571 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
574 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
585 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
586 emitForm_S(i
, 0xa8, true);
591 CodeEmitterNVC0::emitDMUL(const Instruction
*i
)
593 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
595 emitForm_A(i
, HEX64(50000000, 00000001));
601 assert(!i
->saturate
);
604 assert(!i
->postFactor
);
608 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
610 if (i
->encSize
== 8) {
611 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
612 emitForm_A(i
, HEX64(10000000, 00000002));
614 emitForm_A(i
, HEX64(50000000, 00000003));
616 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
618 if (i
->sType
== TYPE_S32
)
620 if (i
->dType
== TYPE_S32
)
623 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
625 if (i
->sType
== TYPE_S32
)
631 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
633 if (i
->encSize
== 8) {
634 if (isLIMM(i
->src(1), TYPE_F32
)) {
635 assert(!i
->saturate
);
636 emitForm_A(i
, HEX64(28000000, 00000002));
638 code
[0] |= i
->src(0).mod
.abs() << 7;
639 code
[0] |= i
->src(0).mod
.neg() << 9;
641 if (i
->src(1).mod
.abs())
642 code
[1] &= 0xfdffffff;
643 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
644 code
[1] ^= 0x02000000;
646 emitForm_A(i
, HEX64(50000000, 00000000));
653 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
658 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
659 !i
->src(0).mod
.abs() &&
660 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
662 emitForm_S(i
, 0x49, true);
664 if (i
->src(0).mod
.neg())
670 CodeEmitterNVC0::emitDADD(const Instruction
*i
)
672 assert(i
->encSize
== 8);
673 emitForm_A(i
, HEX64(48000000, 00000001));
675 assert(!i
->saturate
);
683 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
687 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
688 assert(!i
->src(0).mod
.neg() || !i
->src(1).mod
.neg());
690 if (i
->src(0).mod
.neg())
692 if (i
->src(1).mod
.neg())
694 if (i
->op
== OP_SUB
) {
696 assert(addOp
!= 0x300); // would be add-plus-one
699 if (i
->encSize
== 8) {
700 if (isLIMM(i
->src(1), TYPE_U32
)) {
701 emitForm_A(i
, HEX64(08000000, 00000002));
703 code
[1] |= 1 << 26; // write carry
705 emitForm_A(i
, HEX64(48000000, 00000003));
707 code
[1] |= 1 << 16; // write carry
713 if (i
->flagsSrc
>= 0) // add carry
716 assert(!(addOp
& 0x100));
717 emitForm_S(i
, (addOp
>> 3) |
718 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
724 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
726 assert(i
->encSize
== 8);
727 emitForm_A(i
, HEX64(20000000, 00000003));
729 if (isSignedType(i
->dType
))
731 if (isSignedType(i
->sType
))
734 code
[1] |= i
->saturate
<< 24;
736 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
737 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
739 if (i
->src(2).mod
.neg()) code
[0] |= 0x10;
740 if (i
->src(1).mod
.neg() ^
741 i
->src(0).mod
.neg()) code
[0] |= 0x20;
743 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
748 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
750 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
752 emitForm_A(i
, HEX64(00000000, 00000003));
754 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
755 code
[1] |= 0x01800000;
757 code
[0] |= (i
->subOp
& 0x00f) << 7;
758 code
[0] |= (i
->subOp
& 0x0f0) << 1;
759 code
[0] |= (i
->subOp
& 0x100) >> 3;
760 code
[0] |= (i
->subOp
& 0x200) >> 2;
761 code
[1] |= (i
->subOp
& 0xc00) << 13;
764 if (i
->flagsDef
>= 0)
769 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
771 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
772 assert(i
->encSize
== 8);
774 emitForm_A(i
, HEX64(38000000, 00000003));
776 if (i
->dType
== TYPE_S32
)
781 CodeEmitterNVC0::emitNOT(Instruction
*i
)
783 assert(i
->encSize
== 8);
784 i
->setSrc(1, i
->src(0));
785 emitForm_A(i
, HEX64(68000000, 000001c3
));
789 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
791 if (i
->def(0).getFile() == FILE_PREDICATE
) {
792 code
[0] = 0x00000004 | (subOp
<< 30);
793 code
[1] = 0x0c000000;
797 defId(i
->def(0), 17);
798 srcId(i
->src(0), 20);
799 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
800 srcId(i
->src(1), 26);
801 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
803 if (i
->defExists(1)) {
804 defId(i
->def(1), 14);
809 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
810 code
[1] |= subOp
<< 21;
811 srcId(i
->src(2), 17);
812 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 20;
814 code
[1] |= 0x000e0000;
817 if (i
->encSize
== 8) {
818 if (isLIMM(i
->src(1), TYPE_U32
)) {
819 emitForm_A(i
, HEX64(38000000, 00000002));
821 if (i
->flagsDef
>= 0)
824 emitForm_A(i
, HEX64(68000000, 00000003));
826 if (i
->flagsDef
>= 0)
829 code
[0] |= subOp
<< 6;
831 if (i
->flagsSrc
>= 0) // carry
834 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
835 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
837 emitForm_S(i
, (subOp
<< 5) |
838 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
843 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
845 emitForm_A(i
, HEX64(54000000, 00000004));
847 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
848 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
852 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
854 emitForm_A(i
, HEX64(28000000, 00000003));
858 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
860 emitForm_A(i
, HEX64(70000000, 00000003));
862 if (i
->dType
== TYPE_S32
)
864 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
869 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
871 emitForm_B(i
, HEX64(78000000, 00000003));
873 if (i
->dType
== TYPE_S32
)
875 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
877 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
882 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
884 emitForm_A(i
, HEX64(24000000, 00000004));
886 code
[0] |= i
->subOp
<< 5;
890 CodeEmitterNVC0::emitShift(const Instruction
*i
)
892 if (i
->op
== OP_SHR
) {
893 emitForm_A(i
, HEX64(58000000, 00000003)
894 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
896 emitForm_A(i
, HEX64(60000000, 00000003));
899 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
904 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
906 if (i
->encSize
== 8) {
907 emitForm_B(i
, HEX64(60000000, 00000000));
909 if (i
->op
== OP_PREEX2
)
912 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
913 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
915 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
920 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
922 if (i
->encSize
== 8) {
923 code
[0] = 0x00000000 | (subOp
<< 26);
924 code
[1] = 0xc8000000;
928 defId(i
->def(0), 14);
929 srcId(i
->src(0), 20);
931 assert(i
->src(0).getFile() == FILE_GPR
);
933 if (i
->saturate
) code
[0] |= 1 << 5;
935 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
936 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
938 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
940 assert(!i
->src(0).mod
.neg());
941 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
946 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
950 assert(i
->encSize
== 8);
952 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
957 if (!isFloatType(i
->dType
))
958 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
959 if (i
->dType
== TYPE_F64
)
967 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
970 case ROUND_M
: code
[1] |= 1 << 17; break;
971 case ROUND_P
: code
[1] |= 2 << 17; break;
972 case ROUND_Z
: code
[1] |= 3 << 17; break;
973 case ROUND_NI
: code
[0] |= 1 << 7; break;
974 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
975 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
976 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
979 assert(!"invalid round mode");
985 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
989 case ROUND_MI
: code
[0] |= 1 << 16; break;
991 case ROUND_PI
: code
[0] |= 2 << 16; break;
993 case ROUND_ZI
: code
[0] |= 3 << 16; break;
1000 CodeEmitterNVC0::emitCVT(Instruction
*i
)
1002 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
1006 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
1007 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
1008 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
1013 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
1014 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
1015 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
1017 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
1022 if (i
->encSize
== 8) {
1023 emitForm_B(i
, HEX64(10000000, 00000004));
1027 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
1028 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
1029 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
1031 // for 8/16 source types, the byte/word is in subOp. word 1 is
1032 // represented as 2.
1033 if (!isFloatType(i
->sType
))
1034 code
[1] |= i
->subOp
<< 0x17;
1036 code
[1] |= i
->subOp
<< 0x18;
1042 if (neg
&& i
->op
!= OP_ABS
)
1048 if (isSignedIntType(dType
))
1050 if (isSignedIntType(i
->sType
))
1053 if (isFloatType(dType
)) {
1054 if (!isFloatType(i
->sType
))
1055 code
[1] |= 0x08000000;
1057 if (isFloatType(i
->sType
))
1058 code
[1] |= 0x04000000;
1060 code
[1] |= 0x0c000000;
1063 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
1066 if (isFloatType(dType
)) {
1067 if (isFloatType(i
->sType
))
1070 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1072 assert(isFloatType(i
->sType
));
1074 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1077 if (neg
) code
[0] |= 1 << 16;
1078 if (sat
) code
[0] |= 1 << 18;
1079 if (abs
) code
[0] |= 1 << 19;
1086 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1091 if (i
->sType
== TYPE_F64
)
1094 if (!isFloatType(i
->sType
))
1097 if (isSignedIntType(i
->sType
))
1099 if (isFloatType(i
->dType
)) {
1100 if (isFloatType(i
->sType
))
1107 case OP_SET_AND
: hi
= 0x10000000; break;
1108 case OP_SET_OR
: hi
= 0x10200000; break;
1109 case OP_SET_XOR
: hi
= 0x10400000; break;
1114 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1116 if (i
->op
!= OP_SET
)
1117 srcId(i
->src(2), 32 + 17);
1119 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1120 if (i
->sType
== TYPE_F32
)
1121 code
[1] += 0x10000000;
1123 code
[1] += 0x08000000;
1125 code
[0] &= ~0xfc000;
1126 defId(i
->def(0), 17);
1127 if (i
->defExists(1))
1128 defId(i
->def(1), 14);
1136 emitCondCode(i
->setCond
, 32 + 23);
1141 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1147 op
= HEX64(30000000, 00000023);
1150 op
= HEX64(30000000, 00000003);
1153 op
= HEX64(38000000, 00000000);
1156 assert(!"invalid type for SLCT");
1162 CondCode cc
= i
->setCond
;
1164 if (i
->src(2).mod
.neg())
1165 cc
= reverseCondCode(cc
);
1167 emitCondCode(cc
, 32 + 23);
1173 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1175 emitForm_A(i
, HEX64(20000000, 00000004));
1177 if (i
->cc
== CC_NOT_P
|| i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1181 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1183 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1184 code
[1] = 0xf0000000;
1186 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1189 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1191 code
[0] = 0x00000086;
1192 code
[1] = 0xd0000000;
1194 code
[1] |= i
->tex
.r
;
1195 code
[1] |= i
->tex
.s
<< 8;
1197 if (i
->tex
.liveOnly
)
1200 defId(i
->def(0), 14);
1201 srcId(i
->src(0), 20);
1205 isNextIndependentTex(const TexInstruction
*i
)
1207 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1209 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1211 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1215 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1217 code
[0] = 0x00000006;
1219 if (isNextIndependentTex(i
))
1220 code
[0] |= 0x080; // t mode
1222 code
[0] |= 0x100; // p mode
1224 if (i
->tex
.liveOnly
)
1228 case OP_TEX
: code
[1] = 0x80000000; break;
1229 case OP_TXB
: code
[1] = 0x84000000; break;
1230 case OP_TXL
: code
[1] = 0x86000000; break;
1231 case OP_TXF
: code
[1] = 0x90000000; break;
1232 case OP_TXG
: code
[1] = 0xa0000000; break;
1233 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1234 case OP_TXD
: code
[1] = 0xe0000000; break;
1236 assert(!"invalid texture op");
1239 if (i
->op
== OP_TXF
) {
1240 if (!i
->tex
.levelZero
)
1241 code
[1] |= 0x02000000;
1243 if (i
->tex
.levelZero
) {
1244 code
[1] |= 0x02000000;
1247 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1250 defId(i
->def(0), 14);
1251 srcId(i
->src(0), 20);
1255 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1257 code
[1] |= i
->tex
.mask
<< 14;
1259 code
[1] |= i
->tex
.r
;
1260 code
[1] |= i
->tex
.s
<< 8;
1261 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1262 code
[1] |= 1 << 18; // in 1st source (with array index)
1265 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1266 if (i
->tex
.target
.isCube())
1268 if (i
->tex
.target
.isArray())
1270 if (i
->tex
.target
.isShadow())
1273 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1275 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1277 if (i
->op
== OP_TXL
)
1278 code
[1] &= ~(1 << 26);
1280 if (i
->op
== OP_TXF
)
1281 code
[1] &= ~(1 << 25);
1283 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1284 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1287 if (i
->tex
.useOffsets
== 1)
1289 if (i
->tex
.useOffsets
== 4)
1296 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1298 code
[0] = 0x00000086;
1299 code
[1] = 0xc0000000;
1301 switch (i
->tex
.query
) {
1302 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1303 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1304 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1305 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1306 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1307 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1309 assert(!"invalid texture query");
1313 code
[1] |= i
->tex
.mask
<< 14;
1315 code
[1] |= i
->tex
.r
;
1316 code
[1] |= i
->tex
.s
<< 8;
1317 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1320 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1322 defId(i
->def(0), 14);
1323 srcId(i
->src(0), 20);
1330 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1332 code
[0] = 0x00000000 | (laneMask
<< 6);
1333 code
[1] = 0x48000000 | qOp
;
1335 defId(i
->def(0), 14);
1336 srcId(i
->src(0), 20);
1337 srcId(i
->srcExists(1) ? i
->src(1) : i
->src(0), 26);
1339 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1340 code
[0] |= 1 << 9; // dall
1346 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1348 const FlowInstruction
*f
= i
->asFlow();
1350 unsigned mask
; // bit 0: predicate, bit 1: target
1352 code
[0] = 0x00000007;
1356 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1357 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1362 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1364 code
[0] |= 0x4000; // indirect calls always use c[] source
1368 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1369 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1370 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1371 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1372 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1374 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1375 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1376 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1377 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1379 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1380 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1381 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1383 assert(!"invalid flow operation");
1389 if (i
->flagsSrc
< 0)
1402 if (code
[0] & 0x4000) {
1403 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1404 setAddress16(i
->src(0));
1405 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1406 if (f
->op
== OP_BRA
)
1407 srcId(f
->src(0).getIndirect(0), 20);
1413 if (f
->op
== OP_CALL
) {
1418 assert(f
->absolute
);
1419 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1420 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1421 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1423 assert(!f
->absolute
);
1424 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1425 code
[0] |= (pcRel
& 0x3f) << 26;
1426 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1430 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1431 if (writeIssueDelays
&& !(f
->target
.bb
->binPos
& 0x3f))
1433 // currently we don't want absolute branches
1434 assert(!f
->absolute
);
1435 code
[0] |= (pcRel
& 0x3f) << 26;
1436 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1441 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1443 Value
*rDef
= NULL
, *pDef
= NULL
;
1446 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1447 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1448 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1449 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1452 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1455 code
[1] = 0x50000000;
1457 code
[0] |= 63 << 14;
1463 if (i
->src(0).getFile() == FILE_GPR
) {
1464 srcId(i
->src(0), 20);
1466 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1468 code
[0] |= imm
->reg
.data
.u32
<< 20;
1473 if (i
->src(1).getFile() == FILE_GPR
) {
1474 srcId(i
->src(1), 26);
1476 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1478 code
[0] |= imm
->reg
.data
.u32
<< 26;
1479 code
[1] |= imm
->reg
.data
.u32
>> 6;
1483 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1484 srcId(i
->src(2), 32 + 17);
1485 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1491 if (i
->defExists(0)) {
1492 if (i
->def(0).getFile() == FILE_GPR
)
1493 rDef
= i
->getDef(0);
1495 pDef
= i
->getDef(0);
1497 if (i
->defExists(1)) {
1498 if (i
->def(1).getFile() == FILE_GPR
)
1499 rDef
= i
->getDef(1);
1501 pDef
= i
->getDef(1);
1505 code
[0] &= ~(63 << 14);
1509 code
[1] &= ~(7 << 21);
1510 defId(pDef
, 32 + 21);
1515 CodeEmitterNVC0::emitAFETCH(const Instruction
*i
)
1517 code
[0] = 0x00000006;
1518 code
[1] = 0x0c000000 | (i
->src(0).get()->reg
.data
.offset
& 0x7ff);
1520 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1525 defId(i
->def(0), 14);
1526 srcId(i
->src(0).getIndirect(0), 20);
1530 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1532 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1534 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1535 code
[1] = 0x00000000 | (prim
>> 6);
1539 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1541 defId(i
->def(0), 14);
1546 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1548 code
[0] = 0x00000006;
1549 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1553 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1554 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1558 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1560 defId(i
->def(0), 14);
1561 srcId(i
->src(0).getIndirect(0), 20);
1562 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1566 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1568 unsigned int size
= typeSizeof(i
->dType
);
1570 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1571 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1573 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1580 assert(i
->src(1).getFile() == FILE_GPR
);
1582 srcId(i
->src(0).getIndirect(0), 20);
1583 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1584 srcId(i
->src(1), 26);
1588 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1590 code
[0] = 0x00000006;
1591 code
[1] = 0x1c000000;
1595 defId(i
->def(0), 14); // new secret address
1596 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1598 assert(i
->src(0).getFile() == FILE_GPR
);
1600 if (i
->op
== OP_EMIT
)
1602 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1606 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1607 unsigned int stream
= SDATA(i
->src(1)).u32
;
1611 code
[0] |= stream
<< 26;
1616 srcId(i
->src(1), 26);
1621 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1623 if (i
->encSize
== 8) {
1624 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1626 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1628 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1633 interpApply(const InterpEntry
*entry
, uint32_t *code
,
1634 bool force_persample_interp
, bool flatshade
)
1636 int ipa
= entry
->ipa
;
1637 int reg
= entry
->reg
;
1638 int loc
= entry
->loc
;
1641 (ipa
& NV50_IR_INTERP_MODE_MASK
) == NV50_IR_INTERP_SC
) {
1642 ipa
= NV50_IR_INTERP_FLAT
;
1644 } else if (force_persample_interp
&&
1645 (ipa
& NV50_IR_INTERP_SAMPLE_MASK
) == NV50_IR_INTERP_DEFAULT
&&
1646 (ipa
& NV50_IR_INTERP_MODE_MASK
) != NV50_IR_INTERP_FLAT
) {
1647 ipa
|= NV50_IR_INTERP_CENTROID
;
1649 code
[loc
+ 0] &= ~(0xf << 6);
1650 code
[loc
+ 0] |= ipa
<< 6;
1651 code
[loc
+ 0] &= ~(0x3f << 26);
1652 code
[loc
+ 0] |= reg
<< 26;
1656 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1658 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1660 if (i
->encSize
== 8) {
1661 code
[0] = 0x00000000;
1662 code
[1] = 0xc0000000 | (base
& 0xffff);
1667 if (i
->op
== OP_PINTERP
) {
1668 srcId(i
->src(1), 26);
1669 addInterp(i
->ipa
, SDATA(i
->src(1)).id
, interpApply
);
1671 code
[0] |= 0x3f << 26;
1672 addInterp(i
->ipa
, 0x3f, interpApply
);
1675 srcId(i
->src(0).getIndirect(0), 20);
1677 assert(i
->op
== OP_PINTERP
);
1678 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1679 srcId(i
->src(1), 20);
1684 defId(i
->def(0), 14);
1686 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1687 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1689 code
[1] |= 0x3f << 17;
1693 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1726 assert(!"invalid type");
1733 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1754 assert(!"invalid caching mode");
1761 uses64bitAddress(const Instruction
*ldst
)
1763 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1764 ldst
->src(0).isIndirect(0) &&
1765 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1769 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1773 switch (i
->src(0).getFile()) {
1774 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1775 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1776 case FILE_MEMORY_SHARED
: opc
= 0xc9000000; break;
1778 assert(!"invalid memory file");
1782 code
[0] = 0x00000005;
1785 setAddressByFile(i
->src(0));
1786 srcId(i
->src(1), 14);
1787 srcId(i
->src(0).getIndirect(0), 20);
1788 if (uses64bitAddress(i
))
1793 emitLoadStoreType(i
->dType
);
1794 emitCachingMode(i
->cache
);
1798 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1802 code
[0] = 0x00000005;
1804 switch (i
->src(0).getFile()) {
1805 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1806 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1807 case FILE_MEMORY_SHARED
: opc
= 0xc1000000; break;
1808 case FILE_MEMORY_CONST
:
1809 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1810 emitMOV(i
); // not sure if this is any better
1813 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1814 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1817 assert(!"invalid memory file");
1823 defId(i
->def(0), 14);
1825 setAddressByFile(i
->src(0));
1826 srcId(i
->src(0).getIndirect(0), 20);
1827 if (uses64bitAddress(i
))
1832 emitLoadStoreType(i
->dType
);
1833 emitCachingMode(i
->cache
);
1837 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1839 switch (SDATA(ref
).sv
.sv
) {
1840 case SV_LANEID
: return 0x00;
1841 case SV_PHYSID
: return 0x03;
1842 case SV_VERTEX_COUNT
: return 0x10;
1843 case SV_INVOCATION_ID
: return 0x11;
1844 case SV_YDIR
: return 0x12;
1845 case SV_THREAD_KILL
: return 0x13;
1846 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1847 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1848 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1849 case SV_GRIDID
: return 0x2c;
1850 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1851 case SV_LBASE
: return 0x34;
1852 case SV_SBASE
: return 0x30;
1853 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1855 assert(!"no sreg for system value");
1861 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1863 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1864 if (i
->src(0).getFile() == FILE_GPR
) {
1865 code
[0] = 0xfc01c003;
1866 code
[1] = 0x1a8e0000;
1867 srcId(i
->src(0), 20);
1869 code
[0] = 0x0001c004;
1870 code
[1] = 0x0c0e0000;
1871 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1873 if (!i
->getSrc(0)->reg
.data
.u32
)
1876 srcId(i
->src(0), 20);
1879 defId(i
->def(0), 17);
1882 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1883 uint8_t sr
= getSRegEncoding(i
->src(0));
1885 if (i
->encSize
== 8) {
1886 code
[0] = 0x00000004 | (sr
<< 26);
1887 code
[1] = 0x2c000000;
1889 code
[0] = 0x40000008 | (sr
<< 20);
1891 defId(i
->def(0), 14);
1895 if (i
->encSize
== 8) {
1898 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1899 opc
= HEX64(18000000, 000001e2
);
1901 if (i
->src(0).getFile() == FILE_PREDICATE
)
1902 opc
= HEX64(080e0000
, 1c000004
);
1904 opc
= HEX64(28000000, 00000004);
1906 opc
|= i
->lanes
<< 5;
1912 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1913 imm
= SDATA(i
->src(0)).u32
;
1914 if (imm
& 0xfff00000) {
1915 assert(!(imm
& 0x000fffff));
1916 code
[0] = 0x00000318 | imm
;
1918 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1919 code
[0] = 0x00000118 | (imm
<< 20);
1923 emitShortSrc2(i
->src(0));
1925 defId(i
->def(0), 14);
1932 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
1934 const bool hasDst
= i
->defExists(0);
1935 const bool casOrExch
=
1936 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
1937 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
1939 if (i
->dType
== TYPE_U64
) {
1941 case NV50_IR_SUBOP_ATOM_ADD
:
1944 code
[1] = 0x507e0000;
1946 code
[1] = 0x10000000;
1948 case NV50_IR_SUBOP_ATOM_EXCH
:
1950 code
[1] = 0x507e0000;
1952 case NV50_IR_SUBOP_ATOM_CAS
:
1954 code
[1] = 0x50000000;
1957 assert(!"invalid u64 red op");
1961 if (i
->dType
== TYPE_U32
) {
1963 case NV50_IR_SUBOP_ATOM_EXCH
:
1965 code
[1] = 0x507e0000;
1967 case NV50_IR_SUBOP_ATOM_CAS
:
1969 code
[1] = 0x50000000;
1972 code
[0] = 0x5 | (i
->subOp
<< 5);
1974 code
[1] = 0x507e0000;
1976 code
[1] = 0x10000000;
1980 if (i
->dType
== TYPE_S32
) {
1981 assert(i
->subOp
<= 2);
1982 code
[0] = 0x205 | (i
->subOp
<< 5);
1984 code
[1] = 0x587e0000;
1986 code
[1] = 0x18000000;
1988 if (i
->dType
== TYPE_F32
) {
1989 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
1992 code
[1] = 0x687e0000;
1994 code
[1] = 0x28000000;
1999 srcId(i
->src(1), 14);
2002 defId(i
->def(0), 32 + 11);
2005 code
[1] |= 63 << 11;
2007 if (hasDst
|| casOrExch
) {
2008 const int32_t offset
= SDATA(i
->src(0)).offset
;
2009 assert(offset
< 0x80000 && offset
>= -0x80000);
2010 code
[0] |= offset
<< 26;
2011 code
[1] |= (offset
& 0x1ffc0) >> 6;
2012 code
[1] |= (offset
& 0xe0000) << 6;
2014 srcAddr32(i
->src(0), 26, 0);
2016 if (i
->getIndirect(0, 0)) {
2017 srcId(i
->getIndirect(0, 0), 20);
2018 if (i
->getIndirect(0, 0)->reg
.size
== 8)
2021 code
[0] |= 63 << 20;
2024 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
2025 srcId(i
->src(2), 32 + 17);
2029 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
2031 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
2032 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
2033 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
2036 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
2039 code
[1] = 0xe0000000;
2045 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
2047 code
[0] = 0x00000005 | (i
->subOp
<< 5);
2049 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
2050 code
[1] = 0x98000000;
2051 srcAddr32(i
->src(0), 28, 2);
2053 code
[1] = 0xd0000000;
2054 setAddress24(i
->src(0));
2056 if (uses64bitAddress(i
))
2058 srcId(i
->src(0).getIndirect(0), 20);
2066 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
2069 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
2070 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
2071 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
2072 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
2073 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
2074 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
2075 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
2076 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
2077 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
2078 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
2079 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
2080 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
2081 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
2082 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
2083 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
2084 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
2089 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
2094 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
2096 ImmediateValue
*imm
= NULL
;
2099 if (i
->srcExists(2)) {
2100 imm
= i
->getSrc(2)->asImm();
2102 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
2106 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
2107 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
2108 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
2115 if (i
->op
== OP_SUCLAMP
) {
2116 if (i
->dType
== TYPE_S32
)
2118 emitSUCLAMPMode(i
->subOp
);
2121 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2124 if (i
->op
!= OP_SUEAU
) {
2125 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2126 code
[0] |= 63 << 14;
2127 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2129 if (i
->defExists(1)) { // r, p
2130 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2131 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2137 assert(i
->op
== OP_SUCLAMP
);
2139 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2144 CodeEmitterNVC0::emitSUGType(DataType ty
)
2147 case TYPE_S32
: code
[1] |= 1 << 13; break;
2148 case TYPE_U8
: code
[1] |= 2 << 13; break;
2149 case TYPE_S8
: code
[1] |= 3 << 13; break;
2151 assert(ty
== TYPE_U32
);
2157 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2159 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2161 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2162 assert(offset
== (offset
& 0xfffc));
2165 code
[0] |= offset
<< 24;
2166 code
[1] |= offset
>> 8;
2167 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2171 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2173 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2174 code
[1] |= 0x7 << 17;
2176 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2178 srcId(i
->src(s
), 32 + 17);
2183 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2186 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2188 emitLoadStoreType(i
->dType
);
2189 emitSUGType(i
->sType
);
2190 emitCachingMode(i
->cache
);
2193 defId(i
->def(0), 14); // destination
2194 srcId(i
->src(0), 20); // address
2196 if (i
->src(1).getFile() == FILE_GPR
)
2197 srcId(i
->src(1), 26);
2204 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2207 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2209 if (i
->op
== OP_SUSTP
)
2210 code
[1] |= i
->tex
.mask
<< 22;
2212 emitLoadStoreType(i
->dType
);
2213 emitSUGType(i
->sType
);
2214 emitCachingMode(i
->cache
);
2217 srcId(i
->src(0), 20); // address
2219 if (i
->src(1).getFile() == FILE_GPR
)
2220 srcId(i
->src(1), 26);
2223 srcId(i
->src(3), 14); // values
2228 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2230 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2232 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2233 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2234 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2235 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2238 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2239 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2240 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2241 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2242 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2243 code
[1] |= (i
->mask
& 0x3) << 2;
2246 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2247 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2248 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2249 code
[1] |= (i
->mask
& 0x3) << 2;
2250 code
[1] |= (i
->mask
& 0xc) << 21;
2259 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2263 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2264 case 0: opc
|= 0xe8ULL
<< 56; break;
2265 case 1: opc
|= 0xb4ULL
<< 56; break;
2266 case 2: opc
|= 0x94ULL
<< 56; break;
2271 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2272 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2273 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2275 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2276 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2283 if (i
->flagsDef
>= 0)
2288 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2290 assert(i
->encSize
== 8);
2291 emitForm_A(i
, HEX64(10000000, 00000006));
2292 code
[0] |= i
->subOp
<< 5;
2293 code
[1] |= 0x00e00000;
2297 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2299 unsigned int size
= insn
->encSize
;
2301 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2304 if (!insn
->encSize
) {
2305 ERROR("skipping unencodable instruction: "); insn
->print();
2308 if (codeSize
+ size
> codeSizeLimit
) {
2309 ERROR("code emitter output buffer too small\n");
2313 if (writeIssueDelays
) {
2314 if (!(codeSize
& 0x3f)) {
2315 code
[0] = 0x00000007; // cf issue delay "instruction"
2316 code
[1] = 0x20000000;
2320 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2321 uint32_t *data
= code
- (id
* 2 + 2);
2323 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2326 data
[0] |= insn
->sched
<< 28;
2327 data
[1] |= insn
->sched
>> 4;
2329 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2333 // assert that instructions with multiple defs don't corrupt registers
2334 for (int d
= 0; insn
->defExists(d
); ++d
)
2335 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2372 if (insn
->dType
== TYPE_F64
)
2374 else if (isFloatType(insn
->dType
))
2380 if (insn
->dType
== TYPE_F64
)
2382 else if (isFloatType(insn
->dType
))
2389 if (insn
->dType
== TYPE_F64
)
2391 else if (isFloatType(insn
->dType
))
2403 emitLogicOp(insn
, 0);
2406 emitLogicOp(insn
, 1);
2409 emitLogicOp(insn
, 2);
2419 emitSET(insn
->asCmp());
2425 emitSLCT(insn
->asCmp());
2441 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
2444 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
2469 emitTEX(insn
->asTex());
2472 emitTXQ(insn
->asTex());
2486 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2487 emitSULDGB(insn
->asTex());
2489 ERROR("SULDB not yet supported on < nve4\n");
2493 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2494 emitSUSTGx(insn
->asTex());
2496 ERROR("SUSTx not yet supported on < nve4\n");
2518 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2521 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2524 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2563 ERROR("operation should have been eliminated");
2569 ERROR("operation should have been lowered\n");
2572 ERROR("unknown op: %u\n", insn
->op
);
2578 assert(insn
->encSize
== 8);
2581 code
+= insn
->encSize
/ 4;
2582 codeSize
+= insn
->encSize
;
2587 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2589 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2591 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2594 if (i
->ftz
|| i
->saturate
|| i
->join
)
2596 if (i
->rnd
!= ROUND_N
)
2598 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2601 if (i
->op
== OP_PINTERP
) {
2602 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2605 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2609 for (int s
= 0; i
->srcExists(s
); ++s
) {
2610 if (i
->src(s
).isIndirect(0))
2613 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2614 if (SDATA(i
->src(s
)).offset
>= 0x100)
2616 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2617 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2620 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2621 if (i
->dType
== TYPE_F32
) {
2622 if (SDATA(i
->src(s
)).u32
>= 0x100)
2625 if (SDATA(i
->src(s
)).u32
> 0xff)
2630 if (i
->op
== OP_CVT
)
2632 if (i
->src(s
).mod
!= Modifier(0)) {
2633 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2634 if (i
->op
!= OP_RSQ
)
2636 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2637 if (i
->op
!= OP_ADD
|| s
!= 0)
2645 // Simplified, erring on safe side.
2646 class SchedDataCalculator
: public Pass
2649 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2655 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2656 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2657 int tex
; // TEX to non-TEX delay 17 (0x11)
2658 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2659 int imul
; // integer MUL to MUL delay 3
2669 void rebase(const int base
)
2671 const int delta
= this->base
- base
;
2676 for (int i
= 0; i
< regs
; ++i
) {
2680 for (int i
= 0; i
< 8; ++i
) {
2687 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2697 memset(&rd
, 0, sizeof(rd
));
2698 memset(&wr
, 0, sizeof(wr
));
2699 memset(&res
, 0, sizeof(res
));
2702 int getLatest(const ScoreData
& d
) const
2705 for (int i
= 0; i
< regs
; ++i
)
2708 for (int i
= 0; i
< 8; ++i
)
2715 inline int getLatestRd() const
2717 return getLatest(rd
);
2719 inline int getLatestWr() const
2721 return getLatest(wr
);
2723 inline int getLatest() const
2725 const int a
= getLatestRd();
2726 const int b
= getLatestWr();
2728 int max
= MAX2(a
, b
);
2729 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2730 max
= MAX2(res
.ld
[f
], max
);
2731 max
= MAX2(res
.st
[f
], max
);
2733 max
= MAX2(res
.sfu
, max
);
2734 max
= MAX2(res
.imul
, max
);
2735 max
= MAX2(res
.tex
, max
);
2738 void setMax(const RegScores
*that
)
2740 for (int i
= 0; i
< regs
; ++i
) {
2741 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
2742 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
2744 for (int i
= 0; i
< 8; ++i
) {
2745 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
2746 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
2748 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
2749 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
2751 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2752 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
2753 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
2755 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
2756 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
2757 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
2759 void print(int cycle
)
2761 for (int i
= 0; i
< regs
; ++i
) {
2762 if (rd
.r
[i
] > cycle
)
2763 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
2764 if (wr
.r
[i
] > cycle
)
2765 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
2767 for (int i
= 0; i
< 8; ++i
) {
2768 if (rd
.p
[i
] > cycle
)
2769 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
2770 if (wr
.p
[i
] > cycle
)
2771 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
2774 INFO("rd $c @ %i\n", rd
.c
);
2776 INFO("wr $c @ %i\n", wr
.c
);
2777 if (res
.sfu
> cycle
)
2778 INFO("sfu @ %i\n", res
.sfu
);
2779 if (res
.imul
> cycle
)
2780 INFO("imul @ %i\n", res
.imul
);
2781 if (res
.tex
> cycle
)
2782 INFO("tex @ %i\n", res
.tex
);
2786 RegScores
*score
; // for current BB
2787 std::vector
<RegScores
> scoreBoards
;
2793 bool visit(Function
*);
2794 bool visit(BasicBlock
*);
2796 void commitInsn(const Instruction
*, int cycle
);
2797 int calcDelay(const Instruction
*, int cycle
) const;
2798 void setDelay(Instruction
*, int delay
, Instruction
*next
);
2800 void recordRd(const Value
*, const int ready
);
2801 void recordWr(const Value
*, const int ready
);
2802 void checkRd(const Value
*, int cycle
, int& delay
) const;
2803 void checkWr(const Value
*, int cycle
, int& delay
) const;
2805 int getCycles(const Instruction
*, int origDelay
) const;
2809 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
2811 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
2812 delay
= MAX2(delay
, 14);
2814 if (insn
->op
== OP_TEXBAR
) {
2815 // TODO: except if results not used before EXIT
2818 if (insn
->op
== OP_JOIN
|| insn
->join
) {
2821 if (delay
>= 0 || prevData
== 0x04 ||
2822 !next
|| !targ
->canDualIssue(insn
, next
)) {
2823 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
2824 if (prevOp
== OP_EXPORT
)
2825 insn
->sched
|= 0x40;
2827 insn
->sched
|= 0x20;
2829 insn
->sched
= 0x04; // dual-issue
2832 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
2833 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
2836 prevData
= insn
->sched
;
2840 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
2842 if (insn
->sched
& 0x80) {
2843 int c
= (insn
->sched
& 0x0f) * 2 + 1;
2844 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
2848 if (insn
->sched
& 0x60)
2849 return (insn
->sched
& 0x1f) + 1;
2850 return (insn
->sched
== 0x04) ? 0 : 32;
2854 SchedDataCalculator::visit(Function
*func
)
2856 int regs
= targ
->getFileSize(FILE_GPR
) + 1;
2857 scoreBoards
.resize(func
->cfg
.getSize());
2858 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
2859 scoreBoards
[i
].wipe(regs
);
2864 SchedDataCalculator::visit(BasicBlock
*bb
)
2867 Instruction
*next
= NULL
;
2873 score
= &scoreBoards
.at(bb
->getId());
2875 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
2876 // back branches will wait until all target dependencies are satisfied
2877 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
2879 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
2880 if (in
->getExit()) {
2881 if (prevData
!= 0x04)
2882 prevData
= in
->getExit()->sched
;
2883 prevOp
= in
->getExit()->op
;
2885 score
->setMax(&scoreBoards
.at(in
->getId()));
2887 if (bb
->cfg
.incidentCount() > 1)
2890 #ifdef NVC0_DEBUG_SCHED_DATA
2891 INFO("=== BB:%i initial scores\n", bb
->getId());
2892 score
->print(cycle
);
2895 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
2898 commitInsn(insn
, cycle
);
2899 int delay
= calcDelay(next
, cycle
);
2900 setDelay(insn
, delay
, next
);
2901 cycle
+= getCycles(insn
, delay
);
2903 #ifdef NVC0_DEBUG_SCHED_DATA
2904 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
2911 commitInsn(insn
, cycle
);
2915 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
2916 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
2918 if (ei
.getType() != Graph::Edge::BACK
) {
2919 // only test the first instruction of the outgoing block
2920 next
= out
->getEntry();
2922 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
2924 // wait until all dependencies are satisfied
2925 const int regsFree
= score
->getLatest();
2926 next
= out
->getFirst();
2927 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
2928 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
2929 c
+= getCycles(next
, bbDelay
);
2934 if (bb
->cfg
.outgoingCount() != 1)
2936 setDelay(insn
, bbDelay
, next
);
2937 cycle
+= getCycles(insn
, bbDelay
);
2939 score
->rebase(cycle
); // common base for initializing out blocks' scores
2943 #define NVE4_MAX_ISSUE_DELAY 0x1f
2945 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
2947 int delay
= 0, ready
= cycle
;
2949 for (int s
= 0; insn
->srcExists(s
); ++s
)
2950 checkRd(insn
->getSrc(s
), cycle
, delay
);
2951 // WAR & WAW don't seem to matter
2952 // for (int s = 0; insn->srcExists(s); ++s)
2953 // recordRd(insn->getSrc(s), cycle);
2955 switch (Target::getOpClass(insn
->op
)) {
2957 ready
= score
->res
.sfu
;
2960 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2961 ready
= score
->res
.imul
;
2963 case OPCLASS_TEXTURE
:
2964 ready
= score
->res
.tex
;
2967 ready
= score
->res
.ld
[insn
->src(0).getFile()];
2970 ready
= score
->res
.st
[insn
->src(0).getFile()];
2975 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
2976 ready
= MAX2(ready
, score
->res
.tex
);
2978 delay
= MAX2(delay
, ready
- cycle
);
2980 // if can issue next cycle, delay is 0, not 1
2981 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
2985 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
2987 const int ready
= cycle
+ targ
->getLatency(insn
);
2989 for (int d
= 0; insn
->defExists(d
); ++d
)
2990 recordWr(insn
->getDef(d
), ready
);
2991 // WAR & WAW don't seem to matter
2992 // for (int s = 0; insn->srcExists(s); ++s)
2993 // recordRd(insn->getSrc(s), cycle);
2995 switch (Target::getOpClass(insn
->op
)) {
2997 score
->res
.sfu
= cycle
+ 4;
3000 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3001 score
->res
.imul
= cycle
+ 4;
3003 case OPCLASS_TEXTURE
:
3004 score
->res
.tex
= cycle
+ 18;
3007 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
3009 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
3010 score
->res
.st
[insn
->src(0).getFile()] = ready
;
3013 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
3014 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
3017 if (insn
->op
== OP_TEXBAR
)
3018 score
->res
.tex
= cycle
;
3024 #ifdef NVC0_DEBUG_SCHED_DATA
3025 score
->print(cycle
);
3030 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
3035 switch (v
->reg
.file
) {
3038 b
= a
+ v
->reg
.size
/ 4;
3039 for (int r
= a
; r
< b
; ++r
)
3040 ready
= MAX2(ready
, score
->rd
.r
[r
]);
3042 case FILE_PREDICATE
:
3043 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
3046 ready
= MAX2(ready
, score
->rd
.c
);
3048 case FILE_SHADER_INPUT
:
3049 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
3050 case FILE_MEMORY_LOCAL
:
3051 case FILE_MEMORY_CONST
:
3052 case FILE_MEMORY_SHARED
:
3053 case FILE_MEMORY_GLOBAL
:
3054 case FILE_SYSTEM_VALUE
:
3055 // TODO: any restrictions here ?
3057 case FILE_IMMEDIATE
:
3064 delay
= MAX2(delay
, ready
- cycle
);
3068 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
3073 switch (v
->reg
.file
) {
3076 b
= a
+ v
->reg
.size
/ 4;
3077 for (int r
= a
; r
< b
; ++r
)
3078 ready
= MAX2(ready
, score
->wr
.r
[r
]);
3080 case FILE_PREDICATE
:
3081 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
3084 assert(v
->reg
.file
== FILE_FLAGS
);
3085 ready
= MAX2(ready
, score
->wr
.c
);
3089 delay
= MAX2(delay
, ready
- cycle
);
3093 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
3095 int a
= v
->reg
.data
.id
;
3097 if (v
->reg
.file
== FILE_GPR
) {
3098 int b
= a
+ v
->reg
.size
/ 4;
3099 for (int r
= a
; r
< b
; ++r
)
3100 score
->rd
.r
[r
] = ready
;
3102 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
3103 if (v
->reg
.file
== FILE_PREDICATE
) {
3104 score
->rd
.p
[a
] = ready
+ 4;
3106 assert(v
->reg
.file
== FILE_FLAGS
);
3107 score
->rd
.c
= ready
+ 4;
3112 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
3114 int a
= v
->reg
.data
.id
;
3116 if (v
->reg
.file
== FILE_GPR
) {
3117 int b
= a
+ v
->reg
.size
/ 4;
3118 for (int r
= a
; r
< b
; ++r
)
3119 score
->wr
.r
[r
] = ready
;
3121 if (v
->reg
.file
== FILE_PREDICATE
) {
3122 score
->wr
.p
[a
] = ready
;
3124 if (v
->reg
.file
== FILE_FLAGS
) {
3125 score
->wr
.c
= ready
;
3130 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
3132 SchedDataCalculator
sched(targ
);
3133 return sched
.run(func
, true, true);
3137 CodeEmitterNVC0::prepareEmission(Function
*func
)
3139 CodeEmitter::prepareEmission(func
);
3141 if (targ
->hasSWSched
)
3142 calculateSchedDataNVC0(targ
, func
);
3145 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3146 : CodeEmitter(target
),
3148 writeIssueDelays(target
->hasSWSched
)
3151 codeSize
= codeSizeLimit
= 0;
3156 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3158 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3159 emit
->setProgramType(type
);
3164 TargetNVC0::getCodeEmitter(Program::Type type
)
3166 if (chipset
>= NVISA_GK20A_CHIPSET
)
3167 return createCodeEmitterGK110(type
);
3168 return createCodeEmitterNVC0(type
);
3171 } // namespace nv50_ir