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 emitPFETCH(const Instruction
*);
89 void emitVFETCH(const Instruction
*);
90 void emitEXPORT(const Instruction
*);
91 void emitOUT(const Instruction
*);
93 void emitUADD(const Instruction
*);
94 void emitFADD(const Instruction
*);
95 void emitDADD(const Instruction
*);
96 void emitUMUL(const Instruction
*);
97 void emitFMUL(const Instruction
*);
98 void emitDMUL(const Instruction
*);
99 void emitIMAD(const Instruction
*);
100 void emitISAD(const Instruction
*);
101 void emitFMAD(const Instruction
*);
102 void emitDMAD(const Instruction
*);
103 void emitMADSP(const Instruction
*);
105 void emitNOT(Instruction
*);
106 void emitLogicOp(const Instruction
*, uint8_t subOp
);
107 void emitPOPC(const Instruction
*);
108 void emitINSBF(const Instruction
*);
109 void emitEXTBF(const Instruction
*);
110 void emitBFIND(const Instruction
*);
111 void emitPERMT(const Instruction
*);
112 void emitShift(const Instruction
*);
114 void emitSFnOp(const Instruction
*, uint8_t subOp
);
116 void emitCVT(Instruction
*);
117 void emitMINMAX(const Instruction
*);
118 void emitPreOp(const Instruction
*);
120 void emitSET(const CmpInstruction
*);
121 void emitSLCT(const CmpInstruction
*);
122 void emitSELP(const Instruction
*);
124 void emitTEXBAR(const Instruction
*);
125 void emitTEX(const TexInstruction
*);
126 void emitTEXCSAA(const TexInstruction
*);
127 void emitTXQ(const TexInstruction
*);
129 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
131 void emitFlow(const Instruction
*);
132 void emitBAR(const Instruction
*);
134 void emitSUCLAMPMode(uint16_t);
135 void emitSUCalc(Instruction
*);
136 void emitSULDGB(const TexInstruction
*);
137 void emitSUSTGx(const TexInstruction
*);
139 void emitVSHL(const Instruction
*);
140 void emitVectorSubOp(const Instruction
*);
142 void emitPIXLD(const Instruction
*);
144 inline void defId(const ValueDef
&, const int pos
);
145 inline void defId(const Instruction
*, int d
, const int pos
);
146 inline void srcId(const ValueRef
&, const int pos
);
147 inline void srcId(const ValueRef
*, const int pos
);
148 inline void srcId(const Instruction
*, int s
, const int pos
);
149 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
151 inline bool isLIMM(const ValueRef
&, DataType ty
);
154 // for better visibility
155 #define HEX64(h, l) 0x##h##l##ULL
157 #define SDATA(a) ((a).rep()->reg.data)
158 #define DDATA(a) ((a).rep()->reg.data)
160 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
162 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
165 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
167 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
170 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
172 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
173 code
[pos
/ 32] |= r
<< (pos
% 32);
177 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
179 const uint32_t offset
= SDATA(src
).offset
>> shr
;
181 code
[pos
/ 32] |= offset
<< (pos
% 32);
182 if (pos
&& (pos
< 32))
183 code
[1] |= offset
>> (32 - pos
);
186 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
188 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
191 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
193 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
194 code
[pos
/ 32] |= r
<< (pos
% 32);
197 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
199 const ImmediateValue
*imm
= ref
.get()->asImm();
201 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
205 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
208 case ROUND_M
: code
[1] |= 1 << 23; break;
209 case ROUND_P
: code
[1] |= 2 << 23; break;
210 case ROUND_Z
: code
[1] |= 3 << 23; break;
212 assert(insn
->rnd
== ROUND_N
);
218 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
220 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
221 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
222 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
223 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
226 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
231 case CC_LT
: val
= 0x1; break;
232 case CC_LTU
: val
= 0x9; break;
233 case CC_EQ
: val
= 0x2; break;
234 case CC_EQU
: val
= 0xa; break;
235 case CC_LE
: val
= 0x3; break;
236 case CC_LEU
: val
= 0xb; break;
237 case CC_GT
: val
= 0x4; break;
238 case CC_GTU
: val
= 0xc; break;
239 case CC_NE
: val
= 0x5; break;
240 case CC_NEU
: val
= 0xd; break;
241 case CC_GE
: val
= 0x6; break;
242 case CC_GEU
: val
= 0xe; break;
243 case CC_TR
: val
= 0xf; break;
244 case CC_FL
: val
= 0x0; break;
246 case CC_A
: val
= 0x14; break;
247 case CC_NA
: val
= 0x13; break;
248 case CC_S
: val
= 0x15; break;
249 case CC_NS
: val
= 0x12; break;
250 case CC_C
: val
= 0x16; break;
251 case CC_NC
: val
= 0x11; break;
252 case CC_O
: val
= 0x17; break;
253 case CC_NO
: val
= 0x10; break;
257 assert(!"invalid condition code");
260 code
[pos
/ 32] |= val
<< (pos
% 32);
264 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
266 if (i
->predSrc
>= 0) {
267 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
268 srcId(i
->src(i
->predSrc
), 10);
269 if (i
->cc
== CC_NOT_P
)
270 code
[0] |= 0x2000; // negate
277 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
279 switch (src
.getFile()) {
280 case FILE_MEMORY_GLOBAL
:
281 srcAddr32(src
, 26, 0);
283 case FILE_MEMORY_LOCAL
:
284 case FILE_MEMORY_SHARED
:
288 assert(src
.getFile() == FILE_MEMORY_CONST
);
295 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
297 Symbol
*sym
= src
.get()->asSym();
301 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
302 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
306 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
308 Symbol
*sym
= src
.get()->asSym();
312 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
313 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
317 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
319 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
323 u32
= imm
->reg
.data
.u32
;
325 if ((code
[0] & 0xf) == 0x2) {
327 code
[0] |= (u32
& 0x3f) << 26;
330 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
332 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
333 assert(!(code
[1] & 0xc000));
335 code
[0] |= (u32
& 0x3f) << 26;
336 code
[1] |= 0xc000 | (u32
>> 6);
339 assert(!(u32
& 0x00000fff));
340 assert(!(code
[1] & 0xc000));
341 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
342 code
[1] |= 0xc000 | (u32
>> 18);
346 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
348 const ImmediateValue
*imm
= ref
.get()->asImm();
350 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
352 assert(s8
== imm
->reg
.data
.s32
);
354 code
[0] |= (s8
& 0x3f) << 26;
355 code
[0] |= (s8
>> 6) << 8;
359 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
366 defId(i
->def(0), 14);
369 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
372 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
373 switch (i
->getSrc(s
)->reg
.file
) {
374 case FILE_MEMORY_CONST
:
375 assert(!(code
[1] & 0xc000));
376 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
377 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
378 setAddress16(i
->src(s
));
382 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
383 assert(!(code
[1] & 0xc000));
387 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
389 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
392 // ignore here, can be predicate or flags, but must not be address
399 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
406 defId(i
->def(0), 14);
408 switch (i
->src(0).getFile()) {
409 case FILE_MEMORY_CONST
:
410 assert(!(code
[1] & 0xc000));
411 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
412 setAddress16(i
->src(0));
415 assert(!(code
[1] & 0xc000));
419 srcId(i
->src(0), 26);
422 // ignore here, can be predicate or flags, but must not be address
428 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
433 if (opc
== 0x0d || opc
== 0x0e)
436 defId(i
->def(0), 14);
437 srcId(i
->src(0), 20);
439 assert(pred
|| (i
->predSrc
< 0));
443 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
444 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
445 assert(!(code
[0] & (0x300 >> ss2a
)));
446 switch (i
->src(s
).get()->reg
.fileIndex
) {
447 case 0: code
[0] |= 0x100 >> ss2a
; break;
448 case 1: code
[0] |= 0x200 >> ss2a
; break;
449 case 16: code
[0] |= 0x300 >> ss2a
; break;
451 ERROR("invalid c[] space for short form\n");
455 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
457 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
459 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
461 setImmediateS8(i
->src(s
));
463 if (i
->src(s
).getFile() == FILE_GPR
) {
464 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
470 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
472 if (src
.getFile() == FILE_MEMORY_CONST
) {
473 switch (src
.get()->reg
.fileIndex
) {
474 case 0: code
[0] |= 0x100; break;
475 case 1: code
[0] |= 0x200; break;
476 case 16: code
[0] |= 0x300; break;
478 assert(!"unsupported file index for short op");
481 srcAddr32(src
, 20, 2);
484 assert(src
.getFile() == FILE_GPR
);
489 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
491 code
[0] = 0x000001e4;
492 code
[1] = 0x40000000;
497 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
499 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
501 if (i
->encSize
== 8) {
502 if (isLIMM(i
->src(1), TYPE_F32
)) {
503 emitForm_A(i
, HEX64(20000000, 00000002));
505 emitForm_A(i
, HEX64(30000000, 00000000));
507 if (i
->src(2).mod
.neg())
520 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
521 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
529 CodeEmitterNVC0::emitDMAD(const Instruction
*i
)
531 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
533 emitForm_A(i
, HEX64(20000000, 00000001));
535 if (i
->src(2).mod
.neg())
543 assert(!i
->saturate
);
548 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
550 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
552 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
554 if (i
->encSize
== 8) {
555 if (isLIMM(i
->src(1), TYPE_F32
)) {
556 assert(i
->postFactor
== 0); // constant folded, hopefully
557 emitForm_A(i
, HEX64(30000000, 00000002));
559 emitForm_A(i
, HEX64(58000000, 00000000));
561 code
[1] |= ((i
->postFactor
> 0) ?
562 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
565 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
576 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
577 emitForm_S(i
, 0xa8, true);
582 CodeEmitterNVC0::emitDMUL(const Instruction
*i
)
584 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
586 emitForm_A(i
, HEX64(50000000, 00000001));
592 assert(!i
->saturate
);
595 assert(!i
->postFactor
);
599 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
601 if (i
->encSize
== 8) {
602 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
603 emitForm_A(i
, HEX64(10000000, 00000002));
605 emitForm_A(i
, HEX64(50000000, 00000003));
607 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
609 if (i
->sType
== TYPE_S32
)
611 if (i
->dType
== TYPE_S32
)
614 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
616 if (i
->sType
== TYPE_S32
)
622 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
624 if (i
->encSize
== 8) {
625 if (isLIMM(i
->src(1), TYPE_F32
)) {
626 assert(!i
->saturate
);
627 emitForm_A(i
, HEX64(28000000, 00000002));
629 code
[0] |= i
->src(0).mod
.abs() << 7;
630 code
[0] |= i
->src(0).mod
.neg() << 9;
632 if (i
->src(1).mod
.abs())
633 code
[1] &= 0xfdffffff;
634 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
635 code
[1] ^= 0x02000000;
637 emitForm_A(i
, HEX64(50000000, 00000000));
644 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
649 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
650 !i
->src(0).mod
.abs() &&
651 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
653 emitForm_S(i
, 0x49, true);
655 if (i
->src(0).mod
.neg())
661 CodeEmitterNVC0::emitDADD(const Instruction
*i
)
663 assert(i
->encSize
== 8);
664 emitForm_A(i
, HEX64(48000000, 00000001));
666 assert(!i
->saturate
);
674 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
678 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
679 assert(!i
->src(0).mod
.neg() || !i
->src(1).mod
.neg());
681 if (i
->src(0).mod
.neg())
683 if (i
->src(1).mod
.neg())
685 if (i
->op
== OP_SUB
) {
687 assert(addOp
!= 0x300); // would be add-plus-one
690 if (i
->encSize
== 8) {
691 if (isLIMM(i
->src(1), TYPE_U32
)) {
692 emitForm_A(i
, HEX64(08000000, 00000002));
694 code
[1] |= 1 << 26; // write carry
696 emitForm_A(i
, HEX64(48000000, 00000003));
698 code
[1] |= 1 << 16; // write carry
704 if (i
->flagsSrc
>= 0) // add carry
707 assert(!(addOp
& 0x100));
708 emitForm_S(i
, (addOp
>> 3) |
709 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
715 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
717 assert(i
->encSize
== 8);
718 emitForm_A(i
, HEX64(20000000, 00000003));
720 if (isSignedType(i
->dType
))
722 if (isSignedType(i
->sType
))
725 code
[1] |= i
->saturate
<< 24;
727 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
728 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
730 if (i
->src(2).mod
.neg()) code
[0] |= 0x10;
731 if (i
->src(1).mod
.neg() ^
732 i
->src(0).mod
.neg()) code
[0] |= 0x20;
734 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
739 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
741 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
743 emitForm_A(i
, HEX64(00000000, 00000003));
745 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
746 code
[1] |= 0x01800000;
748 code
[0] |= (i
->subOp
& 0x00f) << 7;
749 code
[0] |= (i
->subOp
& 0x0f0) << 1;
750 code
[0] |= (i
->subOp
& 0x100) >> 3;
751 code
[0] |= (i
->subOp
& 0x200) >> 2;
752 code
[1] |= (i
->subOp
& 0xc00) << 13;
755 if (i
->flagsDef
>= 0)
760 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
762 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
763 assert(i
->encSize
== 8);
765 emitForm_A(i
, HEX64(38000000, 00000003));
767 if (i
->dType
== TYPE_S32
)
772 CodeEmitterNVC0::emitNOT(Instruction
*i
)
774 assert(i
->encSize
== 8);
775 i
->setSrc(1, i
->src(0));
776 emitForm_A(i
, HEX64(68000000, 000001c3
));
780 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
782 if (i
->def(0).getFile() == FILE_PREDICATE
) {
783 code
[0] = 0x00000004 | (subOp
<< 30);
784 code
[1] = 0x0c000000;
788 defId(i
->def(0), 17);
789 srcId(i
->src(0), 20);
790 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
791 srcId(i
->src(1), 26);
792 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
794 if (i
->defExists(1)) {
795 defId(i
->def(1), 14);
800 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
801 code
[1] |= subOp
<< 21;
802 srcId(i
->src(2), 17);
803 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 20;
805 code
[1] |= 0x000e0000;
808 if (i
->encSize
== 8) {
809 if (isLIMM(i
->src(1), TYPE_U32
)) {
810 emitForm_A(i
, HEX64(38000000, 00000002));
812 if (i
->flagsDef
>= 0)
815 emitForm_A(i
, HEX64(68000000, 00000003));
817 if (i
->flagsDef
>= 0)
820 code
[0] |= subOp
<< 6;
822 if (i
->flagsSrc
>= 0) // carry
825 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
826 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
828 emitForm_S(i
, (subOp
<< 5) |
829 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
834 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
836 emitForm_A(i
, HEX64(54000000, 00000004));
838 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
839 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
843 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
845 emitForm_A(i
, HEX64(28000000, 00000003));
849 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
851 emitForm_A(i
, HEX64(70000000, 00000003));
853 if (i
->dType
== TYPE_S32
)
855 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
860 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
862 emitForm_B(i
, HEX64(78000000, 00000003));
864 if (i
->dType
== TYPE_S32
)
866 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
868 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
873 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
875 emitForm_A(i
, HEX64(24000000, 00000004));
877 code
[0] |= i
->subOp
<< 5;
881 CodeEmitterNVC0::emitShift(const Instruction
*i
)
883 if (i
->op
== OP_SHR
) {
884 emitForm_A(i
, HEX64(58000000, 00000003)
885 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
887 emitForm_A(i
, HEX64(60000000, 00000003));
890 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
895 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
897 if (i
->encSize
== 8) {
898 emitForm_B(i
, HEX64(60000000, 00000000));
900 if (i
->op
== OP_PREEX2
)
903 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
904 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
906 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
911 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
913 if (i
->encSize
== 8) {
914 code
[0] = 0x00000000 | (subOp
<< 26);
915 code
[1] = 0xc8000000;
919 defId(i
->def(0), 14);
920 srcId(i
->src(0), 20);
922 assert(i
->src(0).getFile() == FILE_GPR
);
924 if (i
->saturate
) code
[0] |= 1 << 5;
926 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
927 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
929 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
931 assert(!i
->src(0).mod
.neg());
932 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
937 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
941 assert(i
->encSize
== 8);
943 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
948 if (!isFloatType(i
->dType
))
949 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
950 if (i
->dType
== TYPE_F64
)
958 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
961 case ROUND_M
: code
[1] |= 1 << 17; break;
962 case ROUND_P
: code
[1] |= 2 << 17; break;
963 case ROUND_Z
: code
[1] |= 3 << 17; break;
964 case ROUND_NI
: code
[0] |= 1 << 7; break;
965 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
966 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
967 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
970 assert(!"invalid round mode");
976 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
980 case ROUND_MI
: code
[0] |= 1 << 16; break;
982 case ROUND_PI
: code
[0] |= 2 << 16; break;
984 case ROUND_ZI
: code
[0] |= 3 << 16; break;
991 CodeEmitterNVC0::emitCVT(Instruction
*i
)
993 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
997 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
998 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
999 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
1004 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
1005 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
1006 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
1008 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
1013 if (i
->encSize
== 8) {
1014 emitForm_B(i
, HEX64(10000000, 00000004));
1018 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
1019 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
1020 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
1026 if (neg
&& i
->op
!= OP_ABS
)
1032 if (isSignedIntType(dType
))
1034 if (isSignedIntType(i
->sType
))
1037 if (isFloatType(dType
)) {
1038 if (!isFloatType(i
->sType
))
1039 code
[1] |= 0x08000000;
1041 if (isFloatType(i
->sType
))
1042 code
[1] |= 0x04000000;
1044 code
[1] |= 0x0c000000;
1047 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
1050 if (isFloatType(dType
)) {
1051 if (isFloatType(i
->sType
))
1054 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1056 assert(isFloatType(i
->sType
));
1058 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1061 if (neg
) code
[0] |= 1 << 16;
1062 if (sat
) code
[0] |= 1 << 18;
1063 if (abs
) code
[0] |= 1 << 19;
1070 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1075 if (i
->sType
== TYPE_F64
)
1078 if (!isFloatType(i
->sType
))
1081 if (isFloatType(i
->dType
) || isSignedIntType(i
->sType
))
1085 case OP_SET_AND
: hi
= 0x10000000; break;
1086 case OP_SET_OR
: hi
= 0x10200000; break;
1087 case OP_SET_XOR
: hi
= 0x10400000; break;
1092 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1094 if (i
->op
!= OP_SET
)
1095 srcId(i
->src(2), 32 + 17);
1097 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1098 if (i
->sType
== TYPE_F32
)
1099 code
[1] += 0x10000000;
1101 code
[1] += 0x08000000;
1103 code
[0] &= ~0xfc000;
1104 defId(i
->def(0), 17);
1105 if (i
->defExists(1))
1106 defId(i
->def(1), 14);
1114 emitCondCode(i
->setCond
, 32 + 23);
1119 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1125 op
= HEX64(30000000, 00000023);
1128 op
= HEX64(30000000, 00000003);
1131 op
= HEX64(38000000, 00000000);
1134 assert(!"invalid type for SLCT");
1140 CondCode cc
= i
->setCond
;
1142 if (i
->src(2).mod
.neg())
1143 cc
= reverseCondCode(cc
);
1145 emitCondCode(cc
, 32 + 23);
1151 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1153 emitForm_A(i
, HEX64(20000000, 00000004));
1155 if (i
->cc
== CC_NOT_P
|| i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1159 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1161 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1162 code
[1] = 0xf0000000;
1164 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1167 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1169 code
[0] = 0x00000086;
1170 code
[1] = 0xd0000000;
1172 code
[1] |= i
->tex
.r
;
1173 code
[1] |= i
->tex
.s
<< 8;
1175 if (i
->tex
.liveOnly
)
1178 defId(i
->def(0), 14);
1179 srcId(i
->src(0), 20);
1183 isNextIndependentTex(const TexInstruction
*i
)
1185 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1187 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1189 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1193 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1195 code
[0] = 0x00000006;
1197 if (isNextIndependentTex(i
))
1198 code
[0] |= 0x080; // t mode
1200 code
[0] |= 0x100; // p mode
1202 if (i
->tex
.liveOnly
)
1206 case OP_TEX
: code
[1] = 0x80000000; break;
1207 case OP_TXB
: code
[1] = 0x84000000; break;
1208 case OP_TXL
: code
[1] = 0x86000000; break;
1209 case OP_TXF
: code
[1] = 0x90000000; break;
1210 case OP_TXG
: code
[1] = 0xa0000000; break;
1211 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1212 case OP_TXD
: code
[1] = 0xe0000000; break;
1214 assert(!"invalid texture op");
1217 if (i
->op
== OP_TXF
) {
1218 if (!i
->tex
.levelZero
)
1219 code
[1] |= 0x02000000;
1221 if (i
->tex
.levelZero
) {
1222 code
[1] |= 0x02000000;
1225 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1228 defId(i
->def(0), 14);
1229 srcId(i
->src(0), 20);
1233 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1235 code
[1] |= i
->tex
.mask
<< 14;
1237 code
[1] |= i
->tex
.r
;
1238 code
[1] |= i
->tex
.s
<< 8;
1239 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1240 code
[1] |= 1 << 18; // in 1st source (with array index)
1243 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1244 if (i
->tex
.target
.isCube())
1246 if (i
->tex
.target
.isArray())
1248 if (i
->tex
.target
.isShadow())
1251 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1253 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1255 if (i
->op
== OP_TXL
)
1256 code
[1] &= ~(1 << 26);
1258 if (i
->op
== OP_TXF
)
1259 code
[1] &= ~(1 << 25);
1261 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1262 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1265 if (i
->tex
.useOffsets
== 1)
1267 if (i
->tex
.useOffsets
== 4)
1274 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1276 code
[0] = 0x00000086;
1277 code
[1] = 0xc0000000;
1279 switch (i
->tex
.query
) {
1280 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1281 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1282 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1283 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1284 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1285 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1287 assert(!"invalid texture query");
1291 code
[1] |= i
->tex
.mask
<< 14;
1293 code
[1] |= i
->tex
.r
;
1294 code
[1] |= i
->tex
.s
<< 8;
1295 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1298 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1300 defId(i
->def(0), 14);
1301 srcId(i
->src(0), 20);
1308 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1310 code
[0] = 0x00000000 | (laneMask
<< 6);
1311 code
[1] = 0x48000000 | qOp
;
1313 defId(i
->def(0), 14);
1314 srcId(i
->src(0), 20);
1315 srcId(i
->srcExists(1) ? i
->src(1) : i
->src(0), 26);
1317 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1318 code
[0] |= 1 << 9; // dall
1324 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1326 const FlowInstruction
*f
= i
->asFlow();
1328 unsigned mask
; // bit 0: predicate, bit 1: target
1330 code
[0] = 0x00000007;
1334 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1335 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1340 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1342 code
[0] |= 0x4000; // indirect calls always use c[] source
1346 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1347 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1348 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1349 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1350 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1352 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1353 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1354 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1355 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1357 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1358 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1359 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1361 assert(!"invalid flow operation");
1367 if (i
->flagsSrc
< 0)
1380 if (code
[0] & 0x4000) {
1381 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1382 setAddress16(i
->src(0));
1383 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1384 if (f
->op
== OP_BRA
)
1385 srcId(f
->src(0).getIndirect(0), 20);
1391 if (f
->op
== OP_CALL
) {
1396 assert(f
->absolute
);
1397 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1398 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1399 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1401 assert(!f
->absolute
);
1402 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1403 code
[0] |= (pcRel
& 0x3f) << 26;
1404 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1408 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1409 // currently we don't want absolute branches
1410 assert(!f
->absolute
);
1411 code
[0] |= (pcRel
& 0x3f) << 26;
1412 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1417 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1419 Value
*rDef
= NULL
, *pDef
= NULL
;
1422 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1423 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1424 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1425 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1428 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1431 code
[1] = 0x50000000;
1433 code
[0] |= 63 << 14;
1439 if (i
->src(0).getFile() == FILE_GPR
) {
1440 srcId(i
->src(0), 20);
1442 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1444 code
[0] |= imm
->reg
.data
.u32
<< 20;
1448 if (i
->src(1).getFile() == FILE_GPR
) {
1449 srcId(i
->src(1), 26);
1451 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1453 code
[0] |= imm
->reg
.data
.u32
<< 26;
1454 code
[1] |= imm
->reg
.data
.u32
>> 6;
1457 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1458 srcId(i
->src(2), 32 + 17);
1459 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1465 if (i
->defExists(0)) {
1466 if (i
->def(0).getFile() == FILE_GPR
)
1467 rDef
= i
->getDef(0);
1469 pDef
= i
->getDef(0);
1471 if (i
->defExists(1)) {
1472 if (i
->def(1).getFile() == FILE_GPR
)
1473 rDef
= i
->getDef(1);
1475 pDef
= i
->getDef(1);
1479 code
[0] &= ~(63 << 14);
1483 code
[1] &= ~(7 << 21);
1484 defId(pDef
, 32 + 21);
1489 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1491 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1493 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1494 code
[1] = 0x00000000 | (prim
>> 6);
1498 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1500 defId(i
->def(0), 14);
1505 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1507 code
[0] = 0x00000006;
1508 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1512 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1513 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1517 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1519 defId(i
->def(0), 14);
1520 srcId(i
->src(0).getIndirect(0), 20);
1521 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1525 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1527 unsigned int size
= typeSizeof(i
->dType
);
1529 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1530 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1532 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1539 assert(i
->src(1).getFile() == FILE_GPR
);
1541 srcId(i
->src(0).getIndirect(0), 20);
1542 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1543 srcId(i
->src(1), 26);
1547 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1549 code
[0] = 0x00000006;
1550 code
[1] = 0x1c000000;
1554 defId(i
->def(0), 14); // new secret address
1555 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1557 assert(i
->src(0).getFile() == FILE_GPR
);
1559 if (i
->op
== OP_EMIT
)
1561 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1565 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1566 unsigned int stream
= SDATA(i
->src(1)).u32
;
1570 code
[0] |= stream
<< 26;
1575 srcId(i
->src(1), 26);
1580 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1582 if (i
->encSize
== 8) {
1583 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1585 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1587 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1592 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1594 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1596 if (i
->encSize
== 8) {
1597 code
[0] = 0x00000000;
1598 code
[1] = 0xc0000000 | (base
& 0xffff);
1603 if (i
->op
== OP_PINTERP
)
1604 srcId(i
->src(1), 26);
1606 code
[0] |= 0x3f << 26;
1608 srcId(i
->src(0).getIndirect(0), 20);
1610 assert(i
->op
== OP_PINTERP
);
1611 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1612 srcId(i
->src(1), 20);
1617 defId(i
->def(0), 14);
1619 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1620 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1622 code
[1] |= 0x3f << 17;
1626 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1659 assert(!"invalid type");
1666 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1687 assert(!"invalid caching mode");
1694 uses64bitAddress(const Instruction
*ldst
)
1696 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1697 ldst
->src(0).isIndirect(0) &&
1698 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1702 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1706 switch (i
->src(0).getFile()) {
1707 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1708 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1709 case FILE_MEMORY_SHARED
: opc
= 0xc9000000; break;
1711 assert(!"invalid memory file");
1715 code
[0] = 0x00000005;
1718 setAddressByFile(i
->src(0));
1719 srcId(i
->src(1), 14);
1720 srcId(i
->src(0).getIndirect(0), 20);
1721 if (uses64bitAddress(i
))
1726 emitLoadStoreType(i
->dType
);
1727 emitCachingMode(i
->cache
);
1731 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1735 code
[0] = 0x00000005;
1737 switch (i
->src(0).getFile()) {
1738 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1739 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1740 case FILE_MEMORY_SHARED
: opc
= 0xc1000000; break;
1741 case FILE_MEMORY_CONST
:
1742 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1743 emitMOV(i
); // not sure if this is any better
1746 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1747 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1750 assert(!"invalid memory file");
1756 defId(i
->def(0), 14);
1758 setAddressByFile(i
->src(0));
1759 srcId(i
->src(0).getIndirect(0), 20);
1760 if (uses64bitAddress(i
))
1765 emitLoadStoreType(i
->dType
);
1766 emitCachingMode(i
->cache
);
1770 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1772 switch (SDATA(ref
).sv
.sv
) {
1773 case SV_LANEID
: return 0x00;
1774 case SV_PHYSID
: return 0x03;
1775 case SV_VERTEX_COUNT
: return 0x10;
1776 case SV_INVOCATION_ID
: return 0x11;
1777 case SV_YDIR
: return 0x12;
1778 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1779 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1780 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1781 case SV_GRIDID
: return 0x2c;
1782 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1783 case SV_LBASE
: return 0x34;
1784 case SV_SBASE
: return 0x30;
1785 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1787 assert(!"no sreg for system value");
1793 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1795 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1796 if (i
->src(0).getFile() == FILE_GPR
) {
1797 code
[0] = 0xfc01c003;
1798 code
[1] = 0x1a8e0000;
1799 srcId(i
->src(0), 20);
1801 code
[0] = 0x0001c004;
1802 code
[1] = 0x0c0e0000;
1803 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1805 if (!i
->getSrc(0)->reg
.data
.u32
)
1808 srcId(i
->src(0), 20);
1811 defId(i
->def(0), 17);
1814 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1815 uint8_t sr
= getSRegEncoding(i
->src(0));
1817 if (i
->encSize
== 8) {
1818 code
[0] = 0x00000004 | (sr
<< 26);
1819 code
[1] = 0x2c000000;
1821 code
[0] = 0x40000008 | (sr
<< 20);
1823 defId(i
->def(0), 14);
1827 if (i
->encSize
== 8) {
1830 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1831 opc
= HEX64(18000000, 000001e2
);
1833 if (i
->src(0).getFile() == FILE_PREDICATE
)
1834 opc
= HEX64(080e0000
, 1c000004
);
1836 opc
= HEX64(28000000, 00000004);
1838 opc
|= i
->lanes
<< 5;
1844 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1845 imm
= SDATA(i
->src(0)).u32
;
1846 if (imm
& 0xfff00000) {
1847 assert(!(imm
& 0x000fffff));
1848 code
[0] = 0x00000318 | imm
;
1850 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1851 code
[0] = 0x00000118 | (imm
<< 20);
1855 emitShortSrc2(i
->src(0));
1857 defId(i
->def(0), 14);
1864 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
1866 const bool hasDst
= i
->defExists(0);
1867 const bool casOrExch
=
1868 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
1869 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
1871 if (i
->dType
== TYPE_U64
) {
1873 case NV50_IR_SUBOP_ATOM_ADD
:
1876 code
[1] = 0x507e0000;
1878 code
[1] = 0x10000000;
1880 case NV50_IR_SUBOP_ATOM_EXCH
:
1882 code
[1] = 0x507e0000;
1884 case NV50_IR_SUBOP_ATOM_CAS
:
1886 code
[1] = 0x50000000;
1889 assert(!"invalid u64 red op");
1893 if (i
->dType
== TYPE_U32
) {
1895 case NV50_IR_SUBOP_ATOM_EXCH
:
1897 code
[1] = 0x507e0000;
1899 case NV50_IR_SUBOP_ATOM_CAS
:
1901 code
[1] = 0x50000000;
1904 code
[0] = 0x5 | (i
->subOp
<< 5);
1906 code
[1] = 0x507e0000;
1908 code
[1] = 0x10000000;
1912 if (i
->dType
== TYPE_S32
) {
1913 assert(i
->subOp
<= 2);
1914 code
[0] = 0x205 | (i
->subOp
<< 5);
1916 code
[1] = 0x587e0000;
1918 code
[1] = 0x18000000;
1920 if (i
->dType
== TYPE_F32
) {
1921 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
1924 code
[1] = 0x687e0000;
1926 code
[1] = 0x28000000;
1931 srcId(i
->src(1), 14);
1934 defId(i
->def(0), 32 + 11);
1937 code
[1] |= 63 << 11;
1939 if (hasDst
|| casOrExch
) {
1940 const int32_t offset
= SDATA(i
->src(0)).offset
;
1941 assert(offset
< 0x80000 && offset
>= -0x80000);
1942 code
[0] |= offset
<< 26;
1943 code
[1] |= (offset
& 0x1ffc0) >> 6;
1944 code
[1] |= (offset
& 0xe0000) << 6;
1946 srcAddr32(i
->src(0), 26, 0);
1948 if (i
->getIndirect(0, 0)) {
1949 srcId(i
->getIndirect(0, 0), 20);
1950 if (i
->getIndirect(0, 0)->reg
.size
== 8)
1953 code
[0] |= 63 << 20;
1956 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
1957 srcId(i
->src(2), 32 + 17);
1961 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
1963 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
1964 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
1965 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
1968 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
1971 code
[1] = 0xe0000000;
1977 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
1979 code
[0] = 0x00000005 | (i
->subOp
<< 5);
1981 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
1982 code
[1] = 0x98000000;
1983 srcAddr32(i
->src(0), 28, 2);
1985 code
[1] = 0xd0000000;
1986 setAddress24(i
->src(0));
1988 if (uses64bitAddress(i
))
1990 srcId(i
->src(0).getIndirect(0), 20);
1998 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
2001 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
2002 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
2003 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
2004 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
2005 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
2006 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
2007 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
2008 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
2009 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
2010 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
2011 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
2012 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
2013 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
2014 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
2015 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
2016 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
2021 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
2026 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
2028 ImmediateValue
*imm
= NULL
;
2031 if (i
->srcExists(2)) {
2032 imm
= i
->getSrc(2)->asImm();
2034 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
2038 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
2039 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
2040 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
2047 if (i
->op
== OP_SUCLAMP
) {
2048 if (i
->dType
== TYPE_S32
)
2050 emitSUCLAMPMode(i
->subOp
);
2053 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2056 if (i
->op
!= OP_SUEAU
) {
2057 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2058 code
[0] |= 63 << 14;
2059 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2061 if (i
->defExists(1)) { // r, p
2062 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2063 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2069 assert(i
->op
== OP_SUCLAMP
);
2071 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2076 CodeEmitterNVC0::emitSUGType(DataType ty
)
2079 case TYPE_S32
: code
[1] |= 1 << 13; break;
2080 case TYPE_U8
: code
[1] |= 2 << 13; break;
2081 case TYPE_S8
: code
[1] |= 3 << 13; break;
2083 assert(ty
== TYPE_U32
);
2089 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2091 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2093 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2094 assert(offset
== (offset
& 0xfffc));
2097 code
[0] |= offset
<< 24;
2098 code
[1] |= offset
>> 8;
2099 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2103 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2105 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2106 code
[1] |= 0x7 << 17;
2108 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2110 srcId(i
->src(s
), 32 + 17);
2115 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2118 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2120 emitLoadStoreType(i
->dType
);
2121 emitSUGType(i
->sType
);
2122 emitCachingMode(i
->cache
);
2125 defId(i
->def(0), 14); // destination
2126 srcId(i
->src(0), 20); // address
2128 if (i
->src(1).getFile() == FILE_GPR
)
2129 srcId(i
->src(1), 26);
2136 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2139 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2141 if (i
->op
== OP_SUSTP
)
2142 code
[1] |= i
->tex
.mask
<< 22;
2144 emitLoadStoreType(i
->dType
);
2145 emitSUGType(i
->sType
);
2146 emitCachingMode(i
->cache
);
2149 srcId(i
->src(0), 20); // address
2151 if (i
->src(1).getFile() == FILE_GPR
)
2152 srcId(i
->src(1), 26);
2155 srcId(i
->src(3), 14); // values
2160 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2162 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2164 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2165 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2166 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2167 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2170 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2171 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2172 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2173 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2174 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2175 code
[1] |= (i
->mask
& 0x3) << 2;
2178 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2179 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2180 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2181 code
[1] |= (i
->mask
& 0x3) << 2;
2182 code
[1] |= (i
->mask
& 0xc) << 21;
2191 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2195 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2196 case 0: opc
|= 0xe8ULL
<< 56; break;
2197 case 1: opc
|= 0xb4ULL
<< 56; break;
2198 case 2: opc
|= 0x94ULL
<< 56; break;
2203 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2204 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2205 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2207 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2208 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2215 if (i
->flagsDef
>= 0)
2220 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2222 assert(i
->encSize
== 8);
2223 emitForm_A(i
, HEX64(10000000, 00000006));
2224 code
[0] |= i
->subOp
<< 5;
2225 code
[1] |= 0x00e00000;
2229 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2231 unsigned int size
= insn
->encSize
;
2233 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2236 if (!insn
->encSize
) {
2237 ERROR("skipping unencodable instruction: "); insn
->print();
2240 if (codeSize
+ size
> codeSizeLimit
) {
2241 ERROR("code emitter output buffer too small\n");
2245 if (writeIssueDelays
) {
2246 if (!(codeSize
& 0x3f)) {
2247 code
[0] = 0x00000007; // cf issue delay "instruction"
2248 code
[1] = 0x20000000;
2252 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2253 uint32_t *data
= code
- (id
* 2 + 2);
2255 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2258 data
[0] |= insn
->sched
<< 28;
2259 data
[1] |= insn
->sched
>> 4;
2261 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2265 // assert that instructions with multiple defs don't corrupt registers
2266 for (int d
= 0; insn
->defExists(d
); ++d
)
2267 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2301 if (insn
->dType
== TYPE_F64
)
2303 else if (isFloatType(insn
->dType
))
2309 if (insn
->dType
== TYPE_F64
)
2311 else if (isFloatType(insn
->dType
))
2318 if (insn
->dType
== TYPE_F64
)
2320 else if (isFloatType(insn
->dType
))
2332 emitLogicOp(insn
, 0);
2335 emitLogicOp(insn
, 1);
2338 emitLogicOp(insn
, 2);
2348 emitSET(insn
->asCmp());
2354 emitSLCT(insn
->asCmp());
2370 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
2373 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
2398 emitTEX(insn
->asTex());
2401 emitTXQ(insn
->asTex());
2415 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2416 emitSULDGB(insn
->asTex());
2418 ERROR("SULDB not yet supported on < nve4\n");
2422 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2423 emitSUSTGx(insn
->asTex());
2425 ERROR("SUSTx not yet supported on < nve4\n");
2447 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2450 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2453 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2492 ERROR("operation should have been eliminated");
2498 ERROR("operation should have been lowered\n");
2501 ERROR("unknow op\n");
2507 assert(insn
->encSize
== 8);
2510 code
+= insn
->encSize
/ 4;
2511 codeSize
+= insn
->encSize
;
2516 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2518 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2520 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2523 if (i
->ftz
|| i
->saturate
|| i
->join
)
2525 if (i
->rnd
!= ROUND_N
)
2527 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2530 if (i
->op
== OP_PINTERP
) {
2531 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2534 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2538 for (int s
= 0; i
->srcExists(s
); ++s
) {
2539 if (i
->src(s
).isIndirect(0))
2542 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2543 if (SDATA(i
->src(s
)).offset
>= 0x100)
2545 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2546 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2549 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2550 if (i
->dType
== TYPE_F32
) {
2551 if (SDATA(i
->src(s
)).u32
>= 0x100)
2554 if (SDATA(i
->src(s
)).u32
> 0xff)
2559 if (i
->op
== OP_CVT
)
2561 if (i
->src(s
).mod
!= Modifier(0)) {
2562 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2563 if (i
->op
!= OP_RSQ
)
2565 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2566 if (i
->op
!= OP_ADD
|| s
!= 0)
2574 // Simplified, erring on safe side.
2575 class SchedDataCalculator
: public Pass
2578 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2584 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2585 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2586 int tex
; // TEX to non-TEX delay 17 (0x11)
2587 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2588 int imul
; // integer MUL to MUL delay 3
2597 void rebase(const int base
)
2599 const int delta
= this->base
- base
;
2604 for (int i
= 0; i
< 64; ++i
) {
2608 for (int i
= 0; i
< 8; ++i
) {
2615 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2625 memset(&rd
, 0, sizeof(rd
));
2626 memset(&wr
, 0, sizeof(wr
));
2627 memset(&res
, 0, sizeof(res
));
2629 int getLatest(const ScoreData
& d
) const
2632 for (int i
= 0; i
< 64; ++i
)
2635 for (int i
= 0; i
< 8; ++i
)
2642 inline int getLatestRd() const
2644 return getLatest(rd
);
2646 inline int getLatestWr() const
2648 return getLatest(wr
);
2650 inline int getLatest() const
2652 const int a
= getLatestRd();
2653 const int b
= getLatestWr();
2655 int max
= MAX2(a
, b
);
2656 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2657 max
= MAX2(res
.ld
[f
], max
);
2658 max
= MAX2(res
.st
[f
], max
);
2660 max
= MAX2(res
.sfu
, max
);
2661 max
= MAX2(res
.imul
, max
);
2662 max
= MAX2(res
.tex
, max
);
2665 void setMax(const RegScores
*that
)
2667 for (int i
= 0; i
< 64; ++i
) {
2668 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
2669 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
2671 for (int i
= 0; i
< 8; ++i
) {
2672 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
2673 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
2675 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
2676 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
2678 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2679 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
2680 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
2682 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
2683 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
2684 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
2686 void print(int cycle
)
2688 for (int i
= 0; i
< 64; ++i
) {
2689 if (rd
.r
[i
] > cycle
)
2690 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
2691 if (wr
.r
[i
] > cycle
)
2692 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
2694 for (int i
= 0; i
< 8; ++i
) {
2695 if (rd
.p
[i
] > cycle
)
2696 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
2697 if (wr
.p
[i
] > cycle
)
2698 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
2701 INFO("rd $c @ %i\n", rd
.c
);
2703 INFO("wr $c @ %i\n", wr
.c
);
2704 if (res
.sfu
> cycle
)
2705 INFO("sfu @ %i\n", res
.sfu
);
2706 if (res
.imul
> cycle
)
2707 INFO("imul @ %i\n", res
.imul
);
2708 if (res
.tex
> cycle
)
2709 INFO("tex @ %i\n", res
.tex
);
2713 RegScores
*score
; // for current BB
2714 std::vector
<RegScores
> scoreBoards
;
2721 bool visit(Function
*);
2722 bool visit(BasicBlock
*);
2724 void commitInsn(const Instruction
*, int cycle
);
2725 int calcDelay(const Instruction
*, int cycle
) const;
2726 void setDelay(Instruction
*, int delay
, Instruction
*next
);
2728 void recordRd(const Value
*, const int ready
);
2729 void recordWr(const Value
*, const int ready
);
2730 void checkRd(const Value
*, int cycle
, int& delay
) const;
2731 void checkWr(const Value
*, int cycle
, int& delay
) const;
2733 int getCycles(const Instruction
*, int origDelay
) const;
2737 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
2739 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
2740 delay
= MAX2(delay
, 14);
2742 if (insn
->op
== OP_TEXBAR
) {
2743 // TODO: except if results not used before EXIT
2746 if (insn
->op
== OP_JOIN
|| insn
->join
) {
2749 if (delay
>= 0 || prevData
== 0x04 ||
2750 !next
|| !targ
->canDualIssue(insn
, next
)) {
2751 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
2752 if (prevOp
== OP_EXPORT
)
2753 insn
->sched
|= 0x40;
2755 insn
->sched
|= 0x20;
2757 insn
->sched
= 0x04; // dual-issue
2760 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
2761 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
2764 prevData
= insn
->sched
;
2768 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
2770 if (insn
->sched
& 0x80) {
2771 int c
= (insn
->sched
& 0x0f) * 2 + 1;
2772 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
2776 if (insn
->sched
& 0x60)
2777 return (insn
->sched
& 0x1f) + 1;
2778 return (insn
->sched
== 0x04) ? 0 : 32;
2782 SchedDataCalculator::visit(Function
*func
)
2784 scoreBoards
.resize(func
->cfg
.getSize());
2785 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
2786 scoreBoards
[i
].wipe();
2791 SchedDataCalculator::visit(BasicBlock
*bb
)
2794 Instruction
*next
= NULL
;
2800 score
= &scoreBoards
.at(bb
->getId());
2802 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
2803 // back branches will wait until all target dependencies are satisfied
2804 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
2806 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
2807 if (in
->getExit()) {
2808 if (prevData
!= 0x04)
2809 prevData
= in
->getExit()->sched
;
2810 prevOp
= in
->getExit()->op
;
2812 score
->setMax(&scoreBoards
.at(in
->getId()));
2814 if (bb
->cfg
.incidentCount() > 1)
2817 #ifdef NVC0_DEBUG_SCHED_DATA
2818 INFO("=== BB:%i initial scores\n", bb
->getId());
2819 score
->print(cycle
);
2822 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
2825 commitInsn(insn
, cycle
);
2826 int delay
= calcDelay(next
, cycle
);
2827 setDelay(insn
, delay
, next
);
2828 cycle
+= getCycles(insn
, delay
);
2830 #ifdef NVC0_DEBUG_SCHED_DATA
2831 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
2838 commitInsn(insn
, cycle
);
2842 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
2843 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
2845 if (ei
.getType() != Graph::Edge::BACK
) {
2846 // only test the first instruction of the outgoing block
2847 next
= out
->getEntry();
2849 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
2851 // wait until all dependencies are satisfied
2852 const int regsFree
= score
->getLatest();
2853 next
= out
->getFirst();
2854 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
2855 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
2856 c
+= getCycles(next
, bbDelay
);
2861 if (bb
->cfg
.outgoingCount() != 1)
2863 setDelay(insn
, bbDelay
, next
);
2864 cycle
+= getCycles(insn
, bbDelay
);
2866 score
->rebase(cycle
); // common base for initializing out blocks' scores
2870 #define NVE4_MAX_ISSUE_DELAY 0x1f
2872 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
2874 int delay
= 0, ready
= cycle
;
2876 for (int s
= 0; insn
->srcExists(s
); ++s
)
2877 checkRd(insn
->getSrc(s
), cycle
, delay
);
2878 // WAR & WAW don't seem to matter
2879 // for (int s = 0; insn->srcExists(s); ++s)
2880 // recordRd(insn->getSrc(s), cycle);
2882 switch (Target::getOpClass(insn
->op
)) {
2884 ready
= score
->res
.sfu
;
2887 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2888 ready
= score
->res
.imul
;
2890 case OPCLASS_TEXTURE
:
2891 ready
= score
->res
.tex
;
2894 ready
= score
->res
.ld
[insn
->src(0).getFile()];
2897 ready
= score
->res
.st
[insn
->src(0).getFile()];
2902 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
2903 ready
= MAX2(ready
, score
->res
.tex
);
2905 delay
= MAX2(delay
, ready
- cycle
);
2907 // if can issue next cycle, delay is 0, not 1
2908 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
2912 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
2914 const int ready
= cycle
+ targ
->getLatency(insn
);
2916 for (int d
= 0; insn
->defExists(d
); ++d
)
2917 recordWr(insn
->getDef(d
), ready
);
2918 // WAR & WAW don't seem to matter
2919 // for (int s = 0; insn->srcExists(s); ++s)
2920 // recordRd(insn->getSrc(s), cycle);
2922 switch (Target::getOpClass(insn
->op
)) {
2924 score
->res
.sfu
= cycle
+ 4;
2927 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2928 score
->res
.imul
= cycle
+ 4;
2930 case OPCLASS_TEXTURE
:
2931 score
->res
.tex
= cycle
+ 18;
2934 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
2936 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
2937 score
->res
.st
[insn
->src(0).getFile()] = ready
;
2940 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
2941 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
2944 if (insn
->op
== OP_TEXBAR
)
2945 score
->res
.tex
= cycle
;
2951 #ifdef NVC0_DEBUG_SCHED_DATA
2952 score
->print(cycle
);
2957 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
2962 switch (v
->reg
.file
) {
2965 b
= a
+ v
->reg
.size
/ 4;
2966 for (int r
= a
; r
< b
; ++r
)
2967 ready
= MAX2(ready
, score
->rd
.r
[r
]);
2969 case FILE_PREDICATE
:
2970 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
2973 ready
= MAX2(ready
, score
->rd
.c
);
2975 case FILE_SHADER_INPUT
:
2976 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
2977 case FILE_MEMORY_LOCAL
:
2978 case FILE_MEMORY_CONST
:
2979 case FILE_MEMORY_SHARED
:
2980 case FILE_MEMORY_GLOBAL
:
2981 case FILE_SYSTEM_VALUE
:
2982 // TODO: any restrictions here ?
2984 case FILE_IMMEDIATE
:
2991 delay
= MAX2(delay
, ready
- cycle
);
2995 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
3000 switch (v
->reg
.file
) {
3003 b
= a
+ v
->reg
.size
/ 4;
3004 for (int r
= a
; r
< b
; ++r
)
3005 ready
= MAX2(ready
, score
->wr
.r
[r
]);
3007 case FILE_PREDICATE
:
3008 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
3011 assert(v
->reg
.file
== FILE_FLAGS
);
3012 ready
= MAX2(ready
, score
->wr
.c
);
3016 delay
= MAX2(delay
, ready
- cycle
);
3020 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
3022 int a
= v
->reg
.data
.id
;
3024 if (v
->reg
.file
== FILE_GPR
) {
3025 int b
= a
+ v
->reg
.size
/ 4;
3026 for (int r
= a
; r
< b
; ++r
)
3027 score
->rd
.r
[r
] = ready
;
3029 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
3030 if (v
->reg
.file
== FILE_PREDICATE
) {
3031 score
->rd
.p
[a
] = ready
+ 4;
3033 assert(v
->reg
.file
== FILE_FLAGS
);
3034 score
->rd
.c
= ready
+ 4;
3039 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
3041 int a
= v
->reg
.data
.id
;
3043 if (v
->reg
.file
== FILE_GPR
) {
3044 int b
= a
+ v
->reg
.size
/ 4;
3045 for (int r
= a
; r
< b
; ++r
)
3046 score
->wr
.r
[r
] = ready
;
3048 if (v
->reg
.file
== FILE_PREDICATE
) {
3049 score
->wr
.p
[a
] = ready
;
3051 if (v
->reg
.file
== FILE_FLAGS
) {
3052 score
->wr
.c
= ready
;
3057 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
3059 SchedDataCalculator
sched(targ
);
3060 return sched
.run(func
, true, true);
3064 CodeEmitterNVC0::prepareEmission(Function
*func
)
3066 CodeEmitter::prepareEmission(func
);
3068 if (targ
->hasSWSched
)
3069 calculateSchedDataNVC0(targ
, func
);
3072 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3073 : CodeEmitter(target
),
3075 writeIssueDelays(target
->hasSWSched
)
3078 codeSize
= codeSizeLimit
= 0;
3083 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3085 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3086 emit
->setProgramType(type
);
3091 TargetNVC0::getCodeEmitter(Program::Type type
)
3093 if (chipset
>= NVISA_GK20A_CHIPSET
)
3094 return createCodeEmitterGK110(type
);
3095 return createCodeEmitterNVC0(type
);
3098 } // namespace nv50_ir