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 emitUMUL(const Instruction
*);
96 void emitFMUL(const Instruction
*);
97 void emitIMAD(const Instruction
*);
98 void emitISAD(const Instruction
*);
99 void emitFMAD(const Instruction
*);
100 void emitMADSP(const Instruction
*);
102 void emitNOT(Instruction
*);
103 void emitLogicOp(const Instruction
*, uint8_t subOp
);
104 void emitPOPC(const Instruction
*);
105 void emitINSBF(const Instruction
*);
106 void emitEXTBF(const Instruction
*);
107 void emitBFIND(const Instruction
*);
108 void emitPERMT(const Instruction
*);
109 void emitShift(const Instruction
*);
111 void emitSFnOp(const Instruction
*, uint8_t subOp
);
113 void emitCVT(Instruction
*);
114 void emitMINMAX(const Instruction
*);
115 void emitPreOp(const Instruction
*);
117 void emitSET(const CmpInstruction
*);
118 void emitSLCT(const CmpInstruction
*);
119 void emitSELP(const Instruction
*);
121 void emitTEXBAR(const Instruction
*);
122 void emitTEX(const TexInstruction
*);
123 void emitTEXCSAA(const TexInstruction
*);
124 void emitTXQ(const TexInstruction
*);
126 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
128 void emitFlow(const Instruction
*);
129 void emitBAR(const Instruction
*);
131 void emitSUCLAMPMode(uint16_t);
132 void emitSUCalc(Instruction
*);
133 void emitSULDGB(const TexInstruction
*);
134 void emitSUSTGx(const TexInstruction
*);
136 void emitVSHL(const Instruction
*);
137 void emitVectorSubOp(const Instruction
*);
139 void emitPIXLD(const Instruction
*);
141 inline void defId(const ValueDef
&, const int pos
);
142 inline void defId(const Instruction
*, int d
, const int pos
);
143 inline void srcId(const ValueRef
&, const int pos
);
144 inline void srcId(const ValueRef
*, const int pos
);
145 inline void srcId(const Instruction
*, int s
, const int pos
);
146 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
148 inline bool isLIMM(const ValueRef
&, DataType ty
);
151 // for better visibility
152 #define HEX64(h, l) 0x##h##l##ULL
154 #define SDATA(a) ((a).rep()->reg.data)
155 #define DDATA(a) ((a).rep()->reg.data)
157 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
159 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
162 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
164 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
167 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
169 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
170 code
[pos
/ 32] |= r
<< (pos
% 32);
174 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
176 const uint32_t offset
= SDATA(src
).offset
>> shr
;
178 code
[pos
/ 32] |= offset
<< (pos
% 32);
179 if (pos
&& (pos
< 32))
180 code
[1] |= offset
>> (32 - pos
);
183 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
185 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
188 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
190 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
191 code
[pos
/ 32] |= r
<< (pos
% 32);
194 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
196 const ImmediateValue
*imm
= ref
.get()->asImm();
198 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
202 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
205 case ROUND_M
: code
[1] |= 1 << 23; break;
206 case ROUND_P
: code
[1] |= 2 << 23; break;
207 case ROUND_Z
: code
[1] |= 3 << 23; break;
209 assert(insn
->rnd
== ROUND_N
);
215 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
217 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
218 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
219 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
220 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
223 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
228 case CC_LT
: val
= 0x1; break;
229 case CC_LTU
: val
= 0x9; break;
230 case CC_EQ
: val
= 0x2; break;
231 case CC_EQU
: val
= 0xa; break;
232 case CC_LE
: val
= 0x3; break;
233 case CC_LEU
: val
= 0xb; break;
234 case CC_GT
: val
= 0x4; break;
235 case CC_GTU
: val
= 0xc; break;
236 case CC_NE
: val
= 0x5; break;
237 case CC_NEU
: val
= 0xd; break;
238 case CC_GE
: val
= 0x6; break;
239 case CC_GEU
: val
= 0xe; break;
240 case CC_TR
: val
= 0xf; break;
241 case CC_FL
: val
= 0x0; break;
243 case CC_A
: val
= 0x14; break;
244 case CC_NA
: val
= 0x13; break;
245 case CC_S
: val
= 0x15; break;
246 case CC_NS
: val
= 0x12; break;
247 case CC_C
: val
= 0x16; break;
248 case CC_NC
: val
= 0x11; break;
249 case CC_O
: val
= 0x17; break;
250 case CC_NO
: val
= 0x10; break;
254 assert(!"invalid condition code");
257 code
[pos
/ 32] |= val
<< (pos
% 32);
261 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
263 if (i
->predSrc
>= 0) {
264 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
265 srcId(i
->src(i
->predSrc
), 10);
266 if (i
->cc
== CC_NOT_P
)
267 code
[0] |= 0x2000; // negate
274 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
276 switch (src
.getFile()) {
277 case FILE_MEMORY_GLOBAL
:
278 srcAddr32(src
, 26, 0);
280 case FILE_MEMORY_LOCAL
:
281 case FILE_MEMORY_SHARED
:
285 assert(src
.getFile() == FILE_MEMORY_CONST
);
292 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
294 Symbol
*sym
= src
.get()->asSym();
298 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
299 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
303 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
305 Symbol
*sym
= src
.get()->asSym();
309 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
310 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
314 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
316 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
320 u32
= imm
->reg
.data
.u32
;
322 if ((code
[0] & 0xf) == 0x2) {
324 code
[0] |= (u32
& 0x3f) << 26;
327 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
329 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
330 assert(!(code
[1] & 0xc000));
332 code
[0] |= (u32
& 0x3f) << 26;
333 code
[1] |= 0xc000 | (u32
>> 6);
336 assert(!(u32
& 0x00000fff));
337 assert(!(code
[1] & 0xc000));
338 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
339 code
[1] |= 0xc000 | (u32
>> 18);
343 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
345 const ImmediateValue
*imm
= ref
.get()->asImm();
347 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
349 assert(s8
== imm
->reg
.data
.s32
);
351 code
[0] |= (s8
& 0x3f) << 26;
352 code
[0] |= (s8
>> 6) << 8;
356 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
363 defId(i
->def(0), 14);
366 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
369 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
370 switch (i
->getSrc(s
)->reg
.file
) {
371 case FILE_MEMORY_CONST
:
372 assert(!(code
[1] & 0xc000));
373 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
374 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
375 setAddress16(i
->src(s
));
379 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
380 assert(!(code
[1] & 0xc000));
384 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
386 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
389 // ignore here, can be predicate or flags, but must not be address
396 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
403 defId(i
->def(0), 14);
405 switch (i
->src(0).getFile()) {
406 case FILE_MEMORY_CONST
:
407 assert(!(code
[1] & 0xc000));
408 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
409 setAddress16(i
->src(0));
412 assert(!(code
[1] & 0xc000));
416 srcId(i
->src(0), 26);
419 // ignore here, can be predicate or flags, but must not be address
425 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
430 if (opc
== 0x0d || opc
== 0x0e)
433 defId(i
->def(0), 14);
434 srcId(i
->src(0), 20);
436 assert(pred
|| (i
->predSrc
< 0));
440 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
441 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
442 assert(!(code
[0] & (0x300 >> ss2a
)));
443 switch (i
->src(s
).get()->reg
.fileIndex
) {
444 case 0: code
[0] |= 0x100 >> ss2a
; break;
445 case 1: code
[0] |= 0x200 >> ss2a
; break;
446 case 16: code
[0] |= 0x300 >> ss2a
; break;
448 ERROR("invalid c[] space for short form\n");
452 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
454 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
456 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
458 setImmediateS8(i
->src(s
));
460 if (i
->src(s
).getFile() == FILE_GPR
) {
461 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
467 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
469 if (src
.getFile() == FILE_MEMORY_CONST
) {
470 switch (src
.get()->reg
.fileIndex
) {
471 case 0: code
[0] |= 0x100; break;
472 case 1: code
[0] |= 0x200; break;
473 case 16: code
[0] |= 0x300; break;
475 assert(!"unsupported file index for short op");
478 srcAddr32(src
, 20, 2);
481 assert(src
.getFile() == FILE_GPR
);
486 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
488 code
[0] = 0x000001e4;
489 code
[1] = 0x40000000;
494 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
496 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
498 if (i
->encSize
== 8) {
499 if (isLIMM(i
->src(1), TYPE_F32
)) {
500 emitForm_A(i
, HEX64(20000000, 00000002));
502 emitForm_A(i
, HEX64(30000000, 00000000));
504 if (i
->src(2).mod
.neg())
517 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
518 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
526 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
528 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
530 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
532 if (i
->encSize
== 8) {
533 if (isLIMM(i
->src(1), TYPE_F32
)) {
534 assert(i
->postFactor
== 0); // constant folded, hopefully
535 emitForm_A(i
, HEX64(30000000, 00000002));
537 emitForm_A(i
, HEX64(58000000, 00000000));
539 code
[1] |= ((i
->postFactor
> 0) ?
540 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
543 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
554 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
555 emitForm_S(i
, 0xa8, true);
560 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
562 if (i
->encSize
== 8) {
563 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
564 emitForm_A(i
, HEX64(10000000, 00000002));
566 emitForm_A(i
, HEX64(50000000, 00000003));
568 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
570 if (i
->sType
== TYPE_S32
)
572 if (i
->dType
== TYPE_S32
)
575 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
577 if (i
->sType
== TYPE_S32
)
583 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
585 if (i
->encSize
== 8) {
586 if (isLIMM(i
->src(1), TYPE_F32
)) {
587 assert(!i
->saturate
);
588 emitForm_A(i
, HEX64(28000000, 00000002));
590 code
[0] |= i
->src(0).mod
.abs() << 7;
591 code
[0] |= i
->src(0).mod
.neg() << 9;
593 if (i
->src(1).mod
.abs())
594 code
[1] &= 0xfdffffff;
595 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
596 code
[1] ^= 0x02000000;
598 emitForm_A(i
, HEX64(50000000, 00000000));
605 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
610 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
611 !i
->src(0).mod
.abs() &&
612 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
614 emitForm_S(i
, 0x49, true);
616 if (i
->src(0).mod
.neg())
622 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
626 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
627 assert(!i
->src(0).mod
.neg() || !i
->src(1).mod
.neg());
629 if (i
->src(0).mod
.neg())
631 if (i
->src(1).mod
.neg())
633 if (i
->op
== OP_SUB
) {
635 assert(addOp
!= 0x300); // would be add-plus-one
638 if (i
->encSize
== 8) {
639 if (isLIMM(i
->src(1), TYPE_U32
)) {
640 emitForm_A(i
, HEX64(08000000, 00000002));
642 code
[1] |= 1 << 26; // write carry
644 emitForm_A(i
, HEX64(48000000, 00000003));
646 code
[1] |= 1 << 16; // write carry
652 if (i
->flagsSrc
>= 0) // add carry
655 assert(!(addOp
& 0x100));
656 emitForm_S(i
, (addOp
>> 3) |
657 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
663 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
665 assert(i
->encSize
== 8);
666 emitForm_A(i
, HEX64(20000000, 00000003));
668 if (isSignedType(i
->dType
))
670 if (isSignedType(i
->sType
))
673 code
[1] |= i
->saturate
<< 24;
675 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
676 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
678 if (i
->src(2).mod
.neg()) code
[0] |= 0x10;
679 if (i
->src(1).mod
.neg() ^
680 i
->src(0).mod
.neg()) code
[0] |= 0x20;
682 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
687 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
689 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
691 emitForm_A(i
, HEX64(00000000, 00000003));
693 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
694 code
[1] |= 0x01800000;
696 code
[0] |= (i
->subOp
& 0x00f) << 7;
697 code
[0] |= (i
->subOp
& 0x0f0) << 1;
698 code
[0] |= (i
->subOp
& 0x100) >> 3;
699 code
[0] |= (i
->subOp
& 0x200) >> 2;
700 code
[1] |= (i
->subOp
& 0xc00) << 13;
703 if (i
->flagsDef
>= 0)
708 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
710 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
711 assert(i
->encSize
== 8);
713 emitForm_A(i
, HEX64(38000000, 00000003));
715 if (i
->dType
== TYPE_S32
)
720 CodeEmitterNVC0::emitNOT(Instruction
*i
)
722 assert(i
->encSize
== 8);
723 i
->setSrc(1, i
->src(0));
724 emitForm_A(i
, HEX64(68000000, 000001c3
));
728 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
730 if (i
->def(0).getFile() == FILE_PREDICATE
) {
731 code
[0] = 0x00000004 | (subOp
<< 30);
732 code
[1] = 0x0c000000;
736 defId(i
->def(0), 17);
737 srcId(i
->src(0), 20);
738 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
739 srcId(i
->src(1), 26);
740 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
742 if (i
->defExists(1)) {
743 defId(i
->def(1), 14);
748 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
749 code
[1] |= subOp
<< 21;
750 srcId(i
->src(2), 17);
751 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 20;
753 code
[1] |= 0x000e0000;
756 if (i
->encSize
== 8) {
757 if (isLIMM(i
->src(1), TYPE_U32
)) {
758 emitForm_A(i
, HEX64(38000000, 00000002));
760 if (i
->flagsDef
>= 0)
763 emitForm_A(i
, HEX64(68000000, 00000003));
765 if (i
->flagsDef
>= 0)
768 code
[0] |= subOp
<< 6;
770 if (i
->flagsSrc
>= 0) // carry
773 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
774 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
776 emitForm_S(i
, (subOp
<< 5) |
777 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
782 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
784 emitForm_A(i
, HEX64(54000000, 00000004));
786 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
787 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
791 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
793 emitForm_A(i
, HEX64(28000000, 00000003));
797 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
799 emitForm_A(i
, HEX64(70000000, 00000003));
801 if (i
->dType
== TYPE_S32
)
803 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
808 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
810 emitForm_B(i
, HEX64(78000000, 00000003));
812 if (i
->dType
== TYPE_S32
)
814 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
816 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
821 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
823 emitForm_A(i
, HEX64(24000000, 00000004));
825 code
[0] |= i
->subOp
<< 5;
829 CodeEmitterNVC0::emitShift(const Instruction
*i
)
831 if (i
->op
== OP_SHR
) {
832 emitForm_A(i
, HEX64(58000000, 00000003)
833 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
835 emitForm_A(i
, HEX64(60000000, 00000003));
838 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
843 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
845 if (i
->encSize
== 8) {
846 emitForm_B(i
, HEX64(60000000, 00000000));
848 if (i
->op
== OP_PREEX2
)
851 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
852 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
854 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
859 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
861 if (i
->encSize
== 8) {
862 code
[0] = 0x00000000 | (subOp
<< 26);
863 code
[1] = 0xc8000000;
867 defId(i
->def(0), 14);
868 srcId(i
->src(0), 20);
870 assert(i
->src(0).getFile() == FILE_GPR
);
872 if (i
->saturate
) code
[0] |= 1 << 5;
874 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
875 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
877 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
879 assert(!i
->src(0).mod
.neg());
880 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
885 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
889 assert(i
->encSize
== 8);
891 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
896 if (!isFloatType(i
->dType
))
897 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
904 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
907 case ROUND_M
: code
[1] |= 1 << 17; break;
908 case ROUND_P
: code
[1] |= 2 << 17; break;
909 case ROUND_Z
: code
[1] |= 3 << 17; break;
910 case ROUND_NI
: code
[0] |= 1 << 7; break;
911 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
912 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
913 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
916 assert(!"invalid round mode");
922 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
926 case ROUND_MI
: code
[0] |= 1 << 16; break;
928 case ROUND_PI
: code
[0] |= 2 << 16; break;
930 case ROUND_ZI
: code
[0] |= 3 << 16; break;
937 CodeEmitterNVC0::emitCVT(Instruction
*i
)
939 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
943 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
944 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
945 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
950 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
951 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
952 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
954 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
959 if (i
->encSize
== 8) {
960 emitForm_B(i
, HEX64(10000000, 00000004));
964 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
965 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
966 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
972 if (neg
&& i
->op
!= OP_ABS
)
978 if (isSignedIntType(dType
))
980 if (isSignedIntType(i
->sType
))
983 if (isFloatType(dType
)) {
984 if (!isFloatType(i
->sType
))
985 code
[1] |= 0x08000000;
987 if (isFloatType(i
->sType
))
988 code
[1] |= 0x04000000;
990 code
[1] |= 0x0c000000;
993 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
996 if (isFloatType(dType
)) {
997 if (isFloatType(i
->sType
))
1000 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1002 assert(isFloatType(i
->sType
));
1004 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1007 if (neg
) code
[0] |= 1 << 16;
1008 if (sat
) code
[0] |= 1 << 18;
1009 if (abs
) code
[0] |= 1 << 19;
1016 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1021 if (i
->sType
== TYPE_F64
)
1024 if (!isFloatType(i
->sType
))
1027 if (isFloatType(i
->dType
) || isSignedIntType(i
->sType
))
1031 case OP_SET_AND
: hi
= 0x10000000; break;
1032 case OP_SET_OR
: hi
= 0x10200000; break;
1033 case OP_SET_XOR
: hi
= 0x10400000; break;
1038 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1040 if (i
->op
!= OP_SET
)
1041 srcId(i
->src(2), 32 + 17);
1043 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1044 if (i
->sType
== TYPE_F32
)
1045 code
[1] += 0x10000000;
1047 code
[1] += 0x08000000;
1049 code
[0] &= ~0xfc000;
1050 defId(i
->def(0), 17);
1051 if (i
->defExists(1))
1052 defId(i
->def(1), 14);
1060 emitCondCode(i
->setCond
, 32 + 23);
1065 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1071 op
= HEX64(30000000, 00000023);
1074 op
= HEX64(30000000, 00000003);
1077 op
= HEX64(38000000, 00000000);
1080 assert(!"invalid type for SLCT");
1086 CondCode cc
= i
->setCond
;
1088 if (i
->src(2).mod
.neg())
1089 cc
= reverseCondCode(cc
);
1091 emitCondCode(cc
, 32 + 23);
1097 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1099 emitForm_A(i
, HEX64(20000000, 00000004));
1101 if (i
->cc
== CC_NOT_P
|| i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1105 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1107 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1108 code
[1] = 0xf0000000;
1110 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1113 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1115 code
[0] = 0x00000086;
1116 code
[1] = 0xd0000000;
1118 code
[1] |= i
->tex
.r
;
1119 code
[1] |= i
->tex
.s
<< 8;
1121 if (i
->tex
.liveOnly
)
1124 defId(i
->def(0), 14);
1125 srcId(i
->src(0), 20);
1129 isNextIndependentTex(const TexInstruction
*i
)
1131 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1133 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1135 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1139 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1141 code
[0] = 0x00000006;
1143 if (isNextIndependentTex(i
))
1144 code
[0] |= 0x080; // t mode
1146 code
[0] |= 0x100; // p mode
1148 if (i
->tex
.liveOnly
)
1152 case OP_TEX
: code
[1] = 0x80000000; break;
1153 case OP_TXB
: code
[1] = 0x84000000; break;
1154 case OP_TXL
: code
[1] = 0x86000000; break;
1155 case OP_TXF
: code
[1] = 0x90000000; break;
1156 case OP_TXG
: code
[1] = 0xa0000000; break;
1157 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1158 case OP_TXD
: code
[1] = 0xe0000000; break;
1160 assert(!"invalid texture op");
1163 if (i
->op
== OP_TXF
) {
1164 if (!i
->tex
.levelZero
)
1165 code
[1] |= 0x02000000;
1167 if (i
->tex
.levelZero
) {
1168 code
[1] |= 0x02000000;
1171 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1174 defId(i
->def(0), 14);
1175 srcId(i
->src(0), 20);
1179 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1181 code
[1] |= i
->tex
.mask
<< 14;
1183 code
[1] |= i
->tex
.r
;
1184 code
[1] |= i
->tex
.s
<< 8;
1185 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1186 code
[1] |= 1 << 18; // in 1st source (with array index)
1189 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1190 if (i
->tex
.target
.isCube())
1192 if (i
->tex
.target
.isArray())
1194 if (i
->tex
.target
.isShadow())
1197 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1199 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1201 if (i
->op
== OP_TXL
)
1202 code
[1] &= ~(1 << 26);
1204 if (i
->op
== OP_TXF
)
1205 code
[1] &= ~(1 << 25);
1207 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1208 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1211 if (i
->tex
.useOffsets
== 1)
1213 if (i
->tex
.useOffsets
== 4)
1220 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1222 code
[0] = 0x00000086;
1223 code
[1] = 0xc0000000;
1225 switch (i
->tex
.query
) {
1226 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1227 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1228 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1229 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1230 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1231 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1233 assert(!"invalid texture query");
1237 code
[1] |= i
->tex
.mask
<< 14;
1239 code
[1] |= i
->tex
.r
;
1240 code
[1] |= i
->tex
.s
<< 8;
1241 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1244 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1246 defId(i
->def(0), 14);
1247 srcId(i
->src(0), 20);
1254 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1256 code
[0] = 0x00000000 | (laneMask
<< 6);
1257 code
[1] = 0x48000000 | qOp
;
1259 defId(i
->def(0), 14);
1260 srcId(i
->src(0), 20);
1261 srcId(i
->srcExists(1) ? i
->src(1) : i
->src(0), 26);
1263 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1264 code
[0] |= 1 << 9; // dall
1270 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1272 const FlowInstruction
*f
= i
->asFlow();
1274 unsigned mask
; // bit 0: predicate, bit 1: target
1276 code
[0] = 0x00000007;
1280 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1281 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1286 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1288 code
[0] |= 0x4000; // indirect calls always use c[] source
1292 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1293 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1294 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1295 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1296 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1298 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1299 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1300 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1301 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1303 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1304 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1305 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1307 assert(!"invalid flow operation");
1313 if (i
->flagsSrc
< 0)
1326 if (code
[0] & 0x4000) {
1327 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1328 setAddress16(i
->src(0));
1329 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1330 if (f
->op
== OP_BRA
)
1331 srcId(f
->src(0).getIndirect(0), 20);
1337 if (f
->op
== OP_CALL
) {
1342 assert(f
->absolute
);
1343 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1344 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1345 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1347 assert(!f
->absolute
);
1348 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1349 code
[0] |= (pcRel
& 0x3f) << 26;
1350 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1354 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1355 // currently we don't want absolute branches
1356 assert(!f
->absolute
);
1357 code
[0] |= (pcRel
& 0x3f) << 26;
1358 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1363 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1365 Value
*rDef
= NULL
, *pDef
= NULL
;
1368 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1369 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1370 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1371 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1374 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1377 code
[1] = 0x50000000;
1379 code
[0] |= 63 << 14;
1385 if (i
->src(0).getFile() == FILE_GPR
) {
1386 srcId(i
->src(0), 20);
1388 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1390 code
[0] |= imm
->reg
.data
.u32
<< 20;
1394 if (i
->src(1).getFile() == FILE_GPR
) {
1395 srcId(i
->src(1), 26);
1397 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1399 code
[0] |= imm
->reg
.data
.u32
<< 26;
1400 code
[1] |= imm
->reg
.data
.u32
>> 6;
1403 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1404 srcId(i
->src(2), 32 + 17);
1405 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1411 if (i
->defExists(0)) {
1412 if (i
->def(0).getFile() == FILE_GPR
)
1413 rDef
= i
->getDef(0);
1415 pDef
= i
->getDef(0);
1417 if (i
->defExists(1)) {
1418 if (i
->def(1).getFile() == FILE_GPR
)
1419 rDef
= i
->getDef(1);
1421 pDef
= i
->getDef(1);
1425 code
[0] &= ~(63 << 14);
1429 code
[1] &= ~(7 << 21);
1430 defId(pDef
, 32 + 21);
1435 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1437 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1439 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1440 code
[1] = 0x00000000 | (prim
>> 6);
1444 defId(i
->def(0), 14);
1445 srcId(i
->src(1), 20);
1449 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1451 code
[0] = 0x00000006;
1452 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1456 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1457 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1461 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1463 defId(i
->def(0), 14);
1464 srcId(i
->src(0).getIndirect(0), 20);
1465 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1469 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1471 unsigned int size
= typeSizeof(i
->dType
);
1473 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1474 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1476 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1483 assert(i
->src(1).getFile() == FILE_GPR
);
1485 srcId(i
->src(0).getIndirect(0), 20);
1486 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1487 srcId(i
->src(1), 26);
1491 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1493 code
[0] = 0x00000006;
1494 code
[1] = 0x1c000000;
1498 defId(i
->def(0), 14); // new secret address
1499 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1501 assert(i
->src(0).getFile() == FILE_GPR
);
1503 if (i
->op
== OP_EMIT
)
1505 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1509 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1510 unsigned int stream
= SDATA(i
->src(1)).u32
;
1514 code
[0] |= stream
<< 26;
1519 srcId(i
->src(1), 26);
1524 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1526 if (i
->encSize
== 8) {
1527 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1529 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1531 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1536 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1538 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1540 if (i
->encSize
== 8) {
1541 code
[0] = 0x00000000;
1542 code
[1] = 0xc0000000 | (base
& 0xffff);
1547 if (i
->op
== OP_PINTERP
)
1548 srcId(i
->src(1), 26);
1550 code
[0] |= 0x3f << 26;
1552 srcId(i
->src(0).getIndirect(0), 20);
1554 assert(i
->op
== OP_PINTERP
);
1555 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1556 srcId(i
->src(1), 20);
1561 defId(i
->def(0), 14);
1563 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1564 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1566 code
[1] |= 0x3f << 17;
1570 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1603 assert(!"invalid type");
1610 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1631 assert(!"invalid caching mode");
1638 uses64bitAddress(const Instruction
*ldst
)
1640 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1641 ldst
->src(0).isIndirect(0) &&
1642 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1646 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1650 switch (i
->src(0).getFile()) {
1651 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1652 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1653 case FILE_MEMORY_SHARED
: opc
= 0xc9000000; break;
1655 assert(!"invalid memory file");
1659 code
[0] = 0x00000005;
1662 setAddressByFile(i
->src(0));
1663 srcId(i
->src(1), 14);
1664 srcId(i
->src(0).getIndirect(0), 20);
1665 if (uses64bitAddress(i
))
1670 emitLoadStoreType(i
->dType
);
1671 emitCachingMode(i
->cache
);
1675 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1679 code
[0] = 0x00000005;
1681 switch (i
->src(0).getFile()) {
1682 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1683 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1684 case FILE_MEMORY_SHARED
: opc
= 0xc1000000; break;
1685 case FILE_MEMORY_CONST
:
1686 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1687 emitMOV(i
); // not sure if this is any better
1690 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1691 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1694 assert(!"invalid memory file");
1700 defId(i
->def(0), 14);
1702 setAddressByFile(i
->src(0));
1703 srcId(i
->src(0).getIndirect(0), 20);
1704 if (uses64bitAddress(i
))
1709 emitLoadStoreType(i
->dType
);
1710 emitCachingMode(i
->cache
);
1714 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1716 switch (SDATA(ref
).sv
.sv
) {
1717 case SV_LANEID
: return 0x00;
1718 case SV_PHYSID
: return 0x03;
1719 case SV_VERTEX_COUNT
: return 0x10;
1720 case SV_INVOCATION_ID
: return 0x11;
1721 case SV_YDIR
: return 0x12;
1722 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1723 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1724 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1725 case SV_GRIDID
: return 0x2c;
1726 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1727 case SV_LBASE
: return 0x34;
1728 case SV_SBASE
: return 0x30;
1729 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1731 assert(!"no sreg for system value");
1737 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1739 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1740 if (i
->src(0).getFile() == FILE_GPR
) {
1741 code
[0] = 0xfc01c003;
1742 code
[1] = 0x1a8e0000;
1743 srcId(i
->src(0), 20);
1745 code
[0] = 0x0001c004;
1746 code
[1] = 0x0c0e0000;
1747 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1749 if (!i
->getSrc(0)->reg
.data
.u32
)
1752 srcId(i
->src(0), 20);
1755 defId(i
->def(0), 17);
1758 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1759 uint8_t sr
= getSRegEncoding(i
->src(0));
1761 if (i
->encSize
== 8) {
1762 code
[0] = 0x00000004 | (sr
<< 26);
1763 code
[1] = 0x2c000000;
1765 code
[0] = 0x40000008 | (sr
<< 20);
1767 defId(i
->def(0), 14);
1771 if (i
->encSize
== 8) {
1774 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1775 opc
= HEX64(18000000, 000001e2
);
1777 if (i
->src(0).getFile() == FILE_PREDICATE
)
1778 opc
= HEX64(080e0000
, 1c000004
);
1780 opc
= HEX64(28000000, 00000004);
1782 opc
|= i
->lanes
<< 5;
1788 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1789 imm
= SDATA(i
->src(0)).u32
;
1790 if (imm
& 0xfff00000) {
1791 assert(!(imm
& 0x000fffff));
1792 code
[0] = 0x00000318 | imm
;
1794 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1795 code
[0] = 0x00000118 | (imm
<< 20);
1799 emitShortSrc2(i
->src(0));
1801 defId(i
->def(0), 14);
1808 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
1810 const bool hasDst
= i
->defExists(0);
1811 const bool casOrExch
=
1812 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
1813 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
1815 if (i
->dType
== TYPE_U64
) {
1817 case NV50_IR_SUBOP_ATOM_ADD
:
1820 code
[1] = 0x507e0000;
1822 code
[1] = 0x10000000;
1824 case NV50_IR_SUBOP_ATOM_EXCH
:
1826 code
[1] = 0x507e0000;
1828 case NV50_IR_SUBOP_ATOM_CAS
:
1830 code
[1] = 0x50000000;
1833 assert(!"invalid u64 red op");
1837 if (i
->dType
== TYPE_U32
) {
1839 case NV50_IR_SUBOP_ATOM_EXCH
:
1841 code
[1] = 0x507e0000;
1843 case NV50_IR_SUBOP_ATOM_CAS
:
1845 code
[1] = 0x50000000;
1848 code
[0] = 0x5 | (i
->subOp
<< 5);
1850 code
[1] = 0x507e0000;
1852 code
[1] = 0x10000000;
1856 if (i
->dType
== TYPE_S32
) {
1857 assert(i
->subOp
<= 2);
1858 code
[0] = 0x205 | (i
->subOp
<< 5);
1860 code
[1] = 0x587e0000;
1862 code
[1] = 0x18000000;
1864 if (i
->dType
== TYPE_F32
) {
1865 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
1868 code
[1] = 0x687e0000;
1870 code
[1] = 0x28000000;
1875 srcId(i
->src(1), 14);
1878 defId(i
->def(0), 32 + 11);
1881 code
[1] |= 63 << 11;
1883 if (hasDst
|| casOrExch
) {
1884 const int32_t offset
= SDATA(i
->src(0)).offset
;
1885 assert(offset
< 0x80000 && offset
>= -0x80000);
1886 code
[0] |= offset
<< 26;
1887 code
[1] |= (offset
& 0x1ffc0) >> 6;
1888 code
[1] |= (offset
& 0xe0000) << 6;
1890 srcAddr32(i
->src(0), 26, 0);
1892 if (i
->getIndirect(0, 0)) {
1893 srcId(i
->getIndirect(0, 0), 20);
1894 if (i
->getIndirect(0, 0)->reg
.size
== 8)
1897 code
[0] |= 63 << 20;
1900 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
1901 srcId(i
->src(2), 32 + 17);
1905 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
1907 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
1908 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
1909 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
1912 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
1915 code
[1] = 0xe0000000;
1921 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
1923 code
[0] = 0x00000005 | (i
->subOp
<< 5);
1925 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
1926 code
[1] = 0x98000000;
1927 srcAddr32(i
->src(0), 28, 2);
1929 code
[1] = 0xd0000000;
1930 setAddress24(i
->src(0));
1932 if (uses64bitAddress(i
))
1934 srcId(i
->src(0).getIndirect(0), 20);
1942 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
1945 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
1946 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
1947 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
1948 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
1949 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
1950 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
1951 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
1952 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
1953 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
1954 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
1955 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
1956 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
1957 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
1958 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
1959 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
1960 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
1965 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
1970 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
1972 ImmediateValue
*imm
= NULL
;
1975 if (i
->srcExists(2)) {
1976 imm
= i
->getSrc(2)->asImm();
1978 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
1982 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
1983 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
1984 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
1991 if (i
->op
== OP_SUCLAMP
) {
1992 if (i
->dType
== TYPE_S32
)
1994 emitSUCLAMPMode(i
->subOp
);
1997 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2000 if (i
->op
!= OP_SUEAU
) {
2001 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2002 code
[0] |= 63 << 14;
2003 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2005 if (i
->defExists(1)) { // r, p
2006 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2007 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2013 assert(i
->op
== OP_SUCLAMP
);
2015 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2020 CodeEmitterNVC0::emitSUGType(DataType ty
)
2023 case TYPE_S32
: code
[1] |= 1 << 13; break;
2024 case TYPE_U8
: code
[1] |= 2 << 13; break;
2025 case TYPE_S8
: code
[1] |= 3 << 13; break;
2027 assert(ty
== TYPE_U32
);
2033 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2035 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2037 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2038 assert(offset
== (offset
& 0xfffc));
2041 code
[0] |= offset
<< 24;
2042 code
[1] |= offset
>> 8;
2043 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2047 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2049 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2050 code
[1] |= 0x7 << 17;
2052 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2054 srcId(i
->src(s
), 32 + 17);
2059 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2062 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2064 emitLoadStoreType(i
->dType
);
2065 emitSUGType(i
->sType
);
2066 emitCachingMode(i
->cache
);
2069 defId(i
->def(0), 14); // destination
2070 srcId(i
->src(0), 20); // address
2072 if (i
->src(1).getFile() == FILE_GPR
)
2073 srcId(i
->src(1), 26);
2080 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2083 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2085 if (i
->op
== OP_SUSTP
)
2086 code
[1] |= i
->tex
.mask
<< 22;
2088 emitLoadStoreType(i
->dType
);
2089 emitSUGType(i
->sType
);
2090 emitCachingMode(i
->cache
);
2093 srcId(i
->src(0), 20); // address
2095 if (i
->src(1).getFile() == FILE_GPR
)
2096 srcId(i
->src(1), 26);
2099 srcId(i
->src(3), 14); // values
2104 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2106 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2108 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2109 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2110 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2111 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2114 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2115 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2116 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2117 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2118 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2119 code
[1] |= (i
->mask
& 0x3) << 2;
2122 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2123 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2124 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2125 code
[1] |= (i
->mask
& 0x3) << 2;
2126 code
[1] |= (i
->mask
& 0xc) << 21;
2135 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2139 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2140 case 0: opc
|= 0xe8ULL
<< 56; break;
2141 case 1: opc
|= 0xb4ULL
<< 56; break;
2142 case 2: opc
|= 0x94ULL
<< 56; break;
2147 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2148 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2149 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2151 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2152 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2159 if (i
->flagsDef
>= 0)
2164 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2166 assert(i
->encSize
== 8);
2167 emitForm_A(i
, HEX64(10000000, 00000006));
2168 code
[0] |= i
->subOp
<< 5;
2169 code
[1] |= 0x00e00000;
2173 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2175 unsigned int size
= insn
->encSize
;
2177 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2180 if (!insn
->encSize
) {
2181 ERROR("skipping unencodable instruction: "); insn
->print();
2184 if (codeSize
+ size
> codeSizeLimit
) {
2185 ERROR("code emitter output buffer too small\n");
2189 if (writeIssueDelays
) {
2190 if (!(codeSize
& 0x3f)) {
2191 code
[0] = 0x00000007; // cf issue delay "instruction"
2192 code
[1] = 0x20000000;
2196 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2197 uint32_t *data
= code
- (id
* 2 + 2);
2199 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2202 data
[0] |= insn
->sched
<< 28;
2203 data
[1] |= insn
->sched
>> 4;
2205 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2209 // assert that instructions with multiple defs don't corrupt registers
2210 for (int d
= 0; insn
->defExists(d
); ++d
)
2211 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2245 if (isFloatType(insn
->dType
))
2251 if (isFloatType(insn
->dType
))
2258 if (isFloatType(insn
->dType
))
2270 emitLogicOp(insn
, 0);
2273 emitLogicOp(insn
, 1);
2276 emitLogicOp(insn
, 2);
2286 emitSET(insn
->asCmp());
2292 emitSLCT(insn
->asCmp());
2336 emitTEX(insn
->asTex());
2339 emitTXQ(insn
->asTex());
2353 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2354 emitSULDGB(insn
->asTex());
2356 ERROR("SULDB not yet supported on < nve4\n");
2360 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2361 emitSUSTGx(insn
->asTex());
2363 ERROR("SUSTx not yet supported on < nve4\n");
2385 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2388 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2391 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2430 ERROR("operation should have been eliminated");
2436 ERROR("operation should have been lowered\n");
2439 ERROR("unknow op\n");
2445 assert(insn
->encSize
== 8);
2448 code
+= insn
->encSize
/ 4;
2449 codeSize
+= insn
->encSize
;
2454 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2456 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2458 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2461 if (i
->ftz
|| i
->saturate
|| i
->join
)
2463 if (i
->rnd
!= ROUND_N
)
2465 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2468 if (i
->op
== OP_PINTERP
) {
2469 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2472 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2476 for (int s
= 0; i
->srcExists(s
); ++s
) {
2477 if (i
->src(s
).isIndirect(0))
2480 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2481 if (SDATA(i
->src(s
)).offset
>= 0x100)
2483 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2484 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2487 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2488 if (i
->dType
== TYPE_F32
) {
2489 if (SDATA(i
->src(s
)).u32
>= 0x100)
2492 if (SDATA(i
->src(s
)).u32
> 0xff)
2497 if (i
->op
== OP_CVT
)
2499 if (i
->src(s
).mod
!= Modifier(0)) {
2500 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2501 if (i
->op
!= OP_RSQ
)
2503 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2504 if (i
->op
!= OP_ADD
|| s
!= 0)
2512 // Simplified, erring on safe side.
2513 class SchedDataCalculator
: public Pass
2516 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2522 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2523 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2524 int tex
; // TEX to non-TEX delay 17 (0x11)
2525 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2526 int imul
; // integer MUL to MUL delay 3
2535 void rebase(const int base
)
2537 const int delta
= this->base
- base
;
2542 for (int i
= 0; i
< 64; ++i
) {
2546 for (int i
= 0; i
< 8; ++i
) {
2553 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2563 memset(&rd
, 0, sizeof(rd
));
2564 memset(&wr
, 0, sizeof(wr
));
2565 memset(&res
, 0, sizeof(res
));
2567 int getLatest(const ScoreData
& d
) const
2570 for (int i
= 0; i
< 64; ++i
)
2573 for (int i
= 0; i
< 8; ++i
)
2580 inline int getLatestRd() const
2582 return getLatest(rd
);
2584 inline int getLatestWr() const
2586 return getLatest(wr
);
2588 inline int getLatest() const
2590 const int a
= getLatestRd();
2591 const int b
= getLatestWr();
2593 int max
= MAX2(a
, b
);
2594 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2595 max
= MAX2(res
.ld
[f
], max
);
2596 max
= MAX2(res
.st
[f
], max
);
2598 max
= MAX2(res
.sfu
, max
);
2599 max
= MAX2(res
.imul
, max
);
2600 max
= MAX2(res
.tex
, max
);
2603 void setMax(const RegScores
*that
)
2605 for (int i
= 0; i
< 64; ++i
) {
2606 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
2607 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
2609 for (int i
= 0; i
< 8; ++i
) {
2610 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
2611 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
2613 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
2614 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
2616 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2617 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
2618 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
2620 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
2621 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
2622 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
2624 void print(int cycle
)
2626 for (int i
= 0; i
< 64; ++i
) {
2627 if (rd
.r
[i
] > cycle
)
2628 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
2629 if (wr
.r
[i
] > cycle
)
2630 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
2632 for (int i
= 0; i
< 8; ++i
) {
2633 if (rd
.p
[i
] > cycle
)
2634 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
2635 if (wr
.p
[i
] > cycle
)
2636 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
2639 INFO("rd $c @ %i\n", rd
.c
);
2641 INFO("wr $c @ %i\n", wr
.c
);
2642 if (res
.sfu
> cycle
)
2643 INFO("sfu @ %i\n", res
.sfu
);
2644 if (res
.imul
> cycle
)
2645 INFO("imul @ %i\n", res
.imul
);
2646 if (res
.tex
> cycle
)
2647 INFO("tex @ %i\n", res
.tex
);
2651 RegScores
*score
; // for current BB
2652 std::vector
<RegScores
> scoreBoards
;
2659 bool visit(Function
*);
2660 bool visit(BasicBlock
*);
2662 void commitInsn(const Instruction
*, int cycle
);
2663 int calcDelay(const Instruction
*, int cycle
) const;
2664 void setDelay(Instruction
*, int delay
, Instruction
*next
);
2666 void recordRd(const Value
*, const int ready
);
2667 void recordWr(const Value
*, const int ready
);
2668 void checkRd(const Value
*, int cycle
, int& delay
) const;
2669 void checkWr(const Value
*, int cycle
, int& delay
) const;
2671 int getCycles(const Instruction
*, int origDelay
) const;
2675 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
2677 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
2678 delay
= MAX2(delay
, 14);
2680 if (insn
->op
== OP_TEXBAR
) {
2681 // TODO: except if results not used before EXIT
2684 if (insn
->op
== OP_JOIN
|| insn
->join
) {
2687 if (delay
>= 0 || prevData
== 0x04 ||
2688 !next
|| !targ
->canDualIssue(insn
, next
)) {
2689 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
2690 if (prevOp
== OP_EXPORT
)
2691 insn
->sched
|= 0x40;
2693 insn
->sched
|= 0x20;
2695 insn
->sched
= 0x04; // dual-issue
2698 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
2699 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
2702 prevData
= insn
->sched
;
2706 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
2708 if (insn
->sched
& 0x80) {
2709 int c
= (insn
->sched
& 0x0f) * 2 + 1;
2710 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
2714 if (insn
->sched
& 0x60)
2715 return (insn
->sched
& 0x1f) + 1;
2716 return (insn
->sched
== 0x04) ? 0 : 32;
2720 SchedDataCalculator::visit(Function
*func
)
2722 scoreBoards
.resize(func
->cfg
.getSize());
2723 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
2724 scoreBoards
[i
].wipe();
2729 SchedDataCalculator::visit(BasicBlock
*bb
)
2732 Instruction
*next
= NULL
;
2738 score
= &scoreBoards
.at(bb
->getId());
2740 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
2741 // back branches will wait until all target dependencies are satisfied
2742 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
2744 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
2745 if (in
->getExit()) {
2746 if (prevData
!= 0x04)
2747 prevData
= in
->getExit()->sched
;
2748 prevOp
= in
->getExit()->op
;
2750 score
->setMax(&scoreBoards
.at(in
->getId()));
2752 if (bb
->cfg
.incidentCount() > 1)
2755 #ifdef NVC0_DEBUG_SCHED_DATA
2756 INFO("=== BB:%i initial scores\n", bb
->getId());
2757 score
->print(cycle
);
2760 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
2763 commitInsn(insn
, cycle
);
2764 int delay
= calcDelay(next
, cycle
);
2765 setDelay(insn
, delay
, next
);
2766 cycle
+= getCycles(insn
, delay
);
2768 #ifdef NVC0_DEBUG_SCHED_DATA
2769 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
2776 commitInsn(insn
, cycle
);
2780 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
2781 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
2783 if (ei
.getType() != Graph::Edge::BACK
) {
2784 // only test the first instruction of the outgoing block
2785 next
= out
->getEntry();
2787 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
2789 // wait until all dependencies are satisfied
2790 const int regsFree
= score
->getLatest();
2791 next
= out
->getFirst();
2792 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
2793 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
2794 c
+= getCycles(next
, bbDelay
);
2799 if (bb
->cfg
.outgoingCount() != 1)
2801 setDelay(insn
, bbDelay
, next
);
2802 cycle
+= getCycles(insn
, bbDelay
);
2804 score
->rebase(cycle
); // common base for initializing out blocks' scores
2808 #define NVE4_MAX_ISSUE_DELAY 0x1f
2810 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
2812 int delay
= 0, ready
= cycle
;
2814 for (int s
= 0; insn
->srcExists(s
); ++s
)
2815 checkRd(insn
->getSrc(s
), cycle
, delay
);
2816 // WAR & WAW don't seem to matter
2817 // for (int s = 0; insn->srcExists(s); ++s)
2818 // recordRd(insn->getSrc(s), cycle);
2820 switch (Target::getOpClass(insn
->op
)) {
2822 ready
= score
->res
.sfu
;
2825 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2826 ready
= score
->res
.imul
;
2828 case OPCLASS_TEXTURE
:
2829 ready
= score
->res
.tex
;
2832 ready
= score
->res
.ld
[insn
->src(0).getFile()];
2835 ready
= score
->res
.st
[insn
->src(0).getFile()];
2840 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
2841 ready
= MAX2(ready
, score
->res
.tex
);
2843 delay
= MAX2(delay
, ready
- cycle
);
2845 // if can issue next cycle, delay is 0, not 1
2846 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
2850 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
2852 const int ready
= cycle
+ targ
->getLatency(insn
);
2854 for (int d
= 0; insn
->defExists(d
); ++d
)
2855 recordWr(insn
->getDef(d
), ready
);
2856 // WAR & WAW don't seem to matter
2857 // for (int s = 0; insn->srcExists(s); ++s)
2858 // recordRd(insn->getSrc(s), cycle);
2860 switch (Target::getOpClass(insn
->op
)) {
2862 score
->res
.sfu
= cycle
+ 4;
2865 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2866 score
->res
.imul
= cycle
+ 4;
2868 case OPCLASS_TEXTURE
:
2869 score
->res
.tex
= cycle
+ 18;
2872 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
2874 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
2875 score
->res
.st
[insn
->src(0).getFile()] = ready
;
2878 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
2879 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
2882 if (insn
->op
== OP_TEXBAR
)
2883 score
->res
.tex
= cycle
;
2889 #ifdef NVC0_DEBUG_SCHED_DATA
2890 score
->print(cycle
);
2895 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
2900 switch (v
->reg
.file
) {
2903 b
= a
+ v
->reg
.size
/ 4;
2904 for (int r
= a
; r
< b
; ++r
)
2905 ready
= MAX2(ready
, score
->rd
.r
[r
]);
2907 case FILE_PREDICATE
:
2908 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
2911 ready
= MAX2(ready
, score
->rd
.c
);
2913 case FILE_SHADER_INPUT
:
2914 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
2915 case FILE_MEMORY_LOCAL
:
2916 case FILE_MEMORY_CONST
:
2917 case FILE_MEMORY_SHARED
:
2918 case FILE_MEMORY_GLOBAL
:
2919 case FILE_SYSTEM_VALUE
:
2920 // TODO: any restrictions here ?
2922 case FILE_IMMEDIATE
:
2929 delay
= MAX2(delay
, ready
- cycle
);
2933 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
2938 switch (v
->reg
.file
) {
2941 b
= a
+ v
->reg
.size
/ 4;
2942 for (int r
= a
; r
< b
; ++r
)
2943 ready
= MAX2(ready
, score
->wr
.r
[r
]);
2945 case FILE_PREDICATE
:
2946 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
2949 assert(v
->reg
.file
== FILE_FLAGS
);
2950 ready
= MAX2(ready
, score
->wr
.c
);
2954 delay
= MAX2(delay
, ready
- cycle
);
2958 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
2960 int a
= v
->reg
.data
.id
;
2962 if (v
->reg
.file
== FILE_GPR
) {
2963 int b
= a
+ v
->reg
.size
/ 4;
2964 for (int r
= a
; r
< b
; ++r
)
2965 score
->rd
.r
[r
] = ready
;
2967 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
2968 if (v
->reg
.file
== FILE_PREDICATE
) {
2969 score
->rd
.p
[a
] = ready
+ 4;
2971 assert(v
->reg
.file
== FILE_FLAGS
);
2972 score
->rd
.c
= ready
+ 4;
2977 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
2979 int a
= v
->reg
.data
.id
;
2981 if (v
->reg
.file
== FILE_GPR
) {
2982 int b
= a
+ v
->reg
.size
/ 4;
2983 for (int r
= a
; r
< b
; ++r
)
2984 score
->wr
.r
[r
] = ready
;
2986 if (v
->reg
.file
== FILE_PREDICATE
) {
2987 score
->wr
.p
[a
] = ready
;
2989 if (v
->reg
.file
== FILE_FLAGS
) {
2990 score
->wr
.c
= ready
;
2995 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
2997 SchedDataCalculator
sched(targ
);
2998 return sched
.run(func
, true, true);
3002 CodeEmitterNVC0::prepareEmission(Function
*func
)
3004 CodeEmitter::prepareEmission(func
);
3006 if (targ
->hasSWSched
)
3007 calculateSchedDataNVC0(targ
, func
);
3010 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3011 : CodeEmitter(target
),
3013 writeIssueDelays(target
->hasSWSched
)
3016 codeSize
= codeSizeLimit
= 0;
3021 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3023 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3024 emit
->setProgramType(type
);
3029 TargetNVC0::getCodeEmitter(Program::Type type
)
3031 if (chipset
>= NVISA_GK20A_CHIPSET
)
3032 return createCodeEmitterGK110(type
);
3033 return createCodeEmitterNVC0(type
);
3036 } // namespace nv50_ir