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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "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
*targ
;
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 setImmediate(const Instruction
*, const int s
); // needs op already set
56 void setImmediateS8(const ValueRef
&);
58 void emitCondCode(CondCode cc
, int pos
);
59 void emitInterpMode(const Instruction
*);
60 void emitLoadStoreType(DataType ty
);
61 void emitCachingMode(CacheMode c
);
63 void emitShortSrc2(const ValueRef
&);
65 inline uint8_t getSRegEncoding(const ValueRef
&);
67 void roundMode_A(const Instruction
*);
68 void roundMode_C(const Instruction
*);
69 void roundMode_CS(const Instruction
*);
71 void emitNegAbs12(const Instruction
*);
73 void emitNOP(const Instruction
*);
75 void emitLOAD(const Instruction
*);
76 void emitSTORE(const Instruction
*);
77 void emitMOV(const Instruction
*);
79 void emitINTERP(const Instruction
*);
80 void emitPFETCH(const Instruction
*);
81 void emitVFETCH(const Instruction
*);
82 void emitEXPORT(const Instruction
*);
83 void emitOUT(const Instruction
*);
85 void emitUADD(const Instruction
*);
86 void emitFADD(const Instruction
*);
87 void emitUMUL(const Instruction
*);
88 void emitFMUL(const Instruction
*);
89 void emitIMAD(const Instruction
*);
90 void emitFMAD(const Instruction
*);
92 void emitNOT(Instruction
*);
93 void emitLogicOp(const Instruction
*, uint8_t subOp
);
94 void emitPOPC(const Instruction
*);
95 void emitINSBF(const Instruction
*);
96 void emitShift(const Instruction
*);
98 void emitSFnOp(const Instruction
*, uint8_t subOp
);
100 void emitCVT(Instruction
*);
101 void emitMINMAX(const Instruction
*);
102 void emitPreOp(const Instruction
*);
104 void emitSET(const CmpInstruction
*);
105 void emitSLCT(const CmpInstruction
*);
106 void emitSELP(const Instruction
*);
108 void emitTEXBAR(const Instruction
*);
109 void emitTEX(const TexInstruction
*);
110 void emitTEXCSAA(const TexInstruction
*);
111 void emitTXQ(const TexInstruction
*);
112 void emitPIXLD(const TexInstruction
*);
114 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
116 void emitFlow(const Instruction
*);
118 inline void defId(const ValueDef
&, const int pos
);
119 inline void srcId(const ValueRef
&, const int pos
);
120 inline void srcId(const ValueRef
*, const int pos
);
121 inline void srcId(const Instruction
*, int s
, const int pos
);
123 inline void srcAddr32(const ValueRef
&, const int pos
); // address / 4
125 inline bool isLIMM(const ValueRef
&, DataType ty
);
128 // for better visibility
129 #define HEX64(h, l) 0x##h##l##ULL
131 #define SDATA(a) ((a).rep()->reg.data)
132 #define DDATA(a) ((a).rep()->reg.data)
134 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
136 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
139 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
141 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
144 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
146 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
147 code
[pos
/ 32] |= r
<< (pos
% 32);
150 void CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, const int pos
)
152 code
[pos
/ 32] |= (SDATA(src
).offset
>> 2) << (pos
% 32);
155 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
157 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
160 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
162 const ImmediateValue
*imm
= ref
.get()->asImm();
164 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
168 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
171 case ROUND_M
: code
[1] |= 1 << 23; break;
172 case ROUND_P
: code
[1] |= 2 << 23; break;
173 case ROUND_Z
: code
[1] |= 3 << 23; break;
175 assert(insn
->rnd
== ROUND_N
);
181 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
183 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
184 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
185 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
186 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
189 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
194 case CC_LT
: val
= 0x1; break;
195 case CC_LTU
: val
= 0x9; break;
196 case CC_EQ
: val
= 0x2; break;
197 case CC_EQU
: val
= 0xa; break;
198 case CC_LE
: val
= 0x3; break;
199 case CC_LEU
: val
= 0xb; break;
200 case CC_GT
: val
= 0x4; break;
201 case CC_GTU
: val
= 0xc; break;
202 case CC_NE
: val
= 0x5; break;
203 case CC_NEU
: val
= 0xd; break;
204 case CC_GE
: val
= 0x6; break;
205 case CC_GEU
: val
= 0xe; break;
206 case CC_TR
: val
= 0xf; break;
207 case CC_FL
: val
= 0x0; break;
209 case CC_A
: val
= 0x14; break;
210 case CC_NA
: val
= 0x13; break;
211 case CC_S
: val
= 0x15; break;
212 case CC_NS
: val
= 0x12; break;
213 case CC_C
: val
= 0x16; break;
214 case CC_NC
: val
= 0x11; break;
215 case CC_O
: val
= 0x17; break;
216 case CC_NO
: val
= 0x10; break;
220 assert(!"invalid condition code");
223 code
[pos
/ 32] |= val
<< (pos
% 32);
227 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
229 if (i
->predSrc
>= 0) {
230 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
231 srcId(i
->src(i
->predSrc
), 10);
232 if (i
->cc
== CC_NOT_P
)
233 code
[0] |= 0x2000; // negate
240 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
242 Symbol
*sym
= src
.get()->asSym();
246 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
247 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
251 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
253 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
257 u32
= imm
->reg
.data
.u32
;
259 if ((code
[0] & 0xf) == 0x2) {
261 code
[0] |= (u32
& 0x3f) << 26;
264 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
266 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
267 assert(!(code
[1] & 0xc000));
269 code
[0] |= (u32
& 0x3f) << 26;
270 code
[1] |= 0xc000 | (u32
>> 6);
273 assert(!(u32
& 0x00000fff));
274 assert(!(code
[1] & 0xc000));
275 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
276 code
[1] |= 0xc000 | (u32
>> 18);
280 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
282 const ImmediateValue
*imm
= ref
.get()->asImm();
284 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
286 assert(s8
== imm
->reg
.data
.s32
);
288 code
[0] |= (s8
& 0x3f) << 26;
289 code
[0] |= (s8
>> 6) << 8;
293 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
300 defId(i
->def(0), 14);
303 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
306 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
307 switch (i
->getSrc(s
)->reg
.file
) {
308 case FILE_MEMORY_CONST
:
309 assert(!(code
[1] & 0xc000));
310 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
311 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
312 setAddress16(i
->src(s
));
316 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
317 assert(!(code
[1] & 0xc000));
321 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
323 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
326 // ignore here, can be predicate or flags, but must not be address
333 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
340 defId(i
->def(0), 14);
342 switch (i
->src(0).getFile()) {
343 case FILE_MEMORY_CONST
:
344 assert(!(code
[1] & 0xc000));
345 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
346 setAddress16(i
->src(0));
349 assert(!(code
[1] & 0xc000));
353 srcId(i
->src(0), 26);
356 // ignore here, can be predicate or flags, but must not be address
362 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
367 if (opc
== 0x0d || opc
== 0x0e)
370 defId(i
->def(0), 14);
371 srcId(i
->src(0), 20);
373 assert(pred
|| (i
->predSrc
< 0));
377 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
378 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
379 assert(!(code
[0] & (0x300 >> ss2a
)));
380 switch (i
->src(s
).get()->reg
.fileIndex
) {
381 case 0: code
[0] |= 0x100 >> ss2a
; break;
382 case 1: code
[0] |= 0x200 >> ss2a
; break;
383 case 16: code
[0] |= 0x300 >> ss2a
; break;
385 ERROR("invalid c[] space for short form\n");
389 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
391 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
393 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
395 setImmediateS8(i
->src(s
));
397 if (i
->src(s
).getFile() == FILE_GPR
) {
398 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
404 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
406 if (src
.getFile() == FILE_MEMORY_CONST
) {
407 switch (src
.get()->reg
.fileIndex
) {
408 case 0: code
[0] |= 0x100; break;
409 case 1: code
[0] |= 0x200; break;
410 case 16: code
[0] |= 0x300; break;
412 assert(!"unsupported file index for short op");
418 assert(src
.getFile() == FILE_GPR
);
423 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
425 code
[0] = 0x000001e4;
426 code
[1] = 0x40000000;
431 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
433 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
435 if (i
->encSize
== 8) {
436 if (isLIMM(i
->src(1), TYPE_F32
)) {
437 emitForm_A(i
, HEX64(20000000, 00000002));
439 emitForm_A(i
, HEX64(30000000, 00000000));
441 if (i
->src(2).mod
.neg())
454 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
455 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
463 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
465 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
467 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
469 if (i
->encSize
== 8) {
470 if (isLIMM(i
->src(1), TYPE_F32
)) {
471 assert(i
->postFactor
== 0); // constant folded, hopefully
472 emitForm_A(i
, HEX64(30000000, 00000002));
474 emitForm_A(i
, HEX64(58000000, 00000000));
476 code
[1] |= ((i
->postFactor
> 0) ?
477 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
480 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
491 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
492 emitForm_S(i
, 0xa8, true);
497 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
499 if (i
->encSize
== 8) {
500 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
501 emitForm_A(i
, HEX64(10000000, 00000002));
503 emitForm_A(i
, HEX64(50000000, 00000003));
505 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
507 if (i
->sType
== TYPE_S32
)
509 if (i
->dType
== TYPE_S32
)
512 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
514 if (i
->sType
== TYPE_S32
)
520 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
522 if (i
->encSize
== 8) {
523 if (isLIMM(i
->src(1), TYPE_F32
)) {
524 assert(!i
->saturate
);
525 emitForm_A(i
, HEX64(28000000, 00000002));
527 code
[0] |= i
->src(0).mod
.abs() << 7;
528 code
[0] |= i
->src(0).mod
.neg() << 9;
530 if (i
->src(1).mod
.abs())
531 code
[1] &= 0xfdffffff;
532 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
533 code
[1] ^= 0x02000000;
535 emitForm_A(i
, HEX64(50000000, 00000000));
542 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
547 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
548 !i
->src(0).mod
.abs() &&
549 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
551 emitForm_S(i
, 0x49, true);
553 if (i
->src(0).mod
.neg())
559 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
563 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
564 assert(!i
->src(0).mod
.neg() || !i
->src(1).mod
.neg());
566 if (i
->src(0).mod
.neg())
568 if (i
->src(1).mod
.neg())
570 if (i
->op
== OP_SUB
) {
572 assert(addOp
!= 0x300); // would be add-plus-one
575 if (i
->encSize
== 8) {
576 if (isLIMM(i
->src(1), TYPE_U32
)) {
577 emitForm_A(i
, HEX64(08000000, 00000002));
579 code
[1] |= 1 << 26; // write carry
581 emitForm_A(i
, HEX64(48000000, 00000003));
583 code
[1] |= 1 << 16; // write carry
589 if (i
->flagsSrc
>= 0) // add carry
592 assert(!(addOp
& 0x100));
593 emitForm_S(i
, (addOp
>> 3) |
594 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
600 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
602 assert(i
->encSize
== 8);
603 emitForm_A(i
, HEX64(20000000, 00000003));
605 if (isSignedType(i
->dType
))
607 if (isSignedType(i
->sType
))
610 code
[1] |= i
->saturate
<< 24;
612 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
613 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
615 if (i
->src(2).mod
.neg()) code
[0] |= 0x10;
616 if (i
->src(1).mod
.neg() ^
617 i
->src(0).mod
.neg()) code
[0] |= 0x20;
619 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
624 CodeEmitterNVC0::emitNOT(Instruction
*i
)
626 assert(i
->encSize
== 8);
627 i
->setSrc(1, i
->src(0));
628 emitForm_A(i
, HEX64(68000000, 000001c3
));
632 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
634 if (i
->encSize
== 8) {
635 if (isLIMM(i
->src(1), TYPE_U32
)) {
636 emitForm_A(i
, HEX64(38000000, 00000002));
641 emitForm_A(i
, HEX64(68000000, 00000003));
646 code
[0] |= subOp
<< 6;
648 if (i
->srcExists(2)) // carry
651 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
652 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
654 emitForm_S(i
, (subOp
<< 5) |
655 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
660 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
662 emitForm_A(i
, HEX64(54000000, 00000004));
664 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
665 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
669 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
671 emitForm_A(i
, HEX64(28000000, 30000000));
675 CodeEmitterNVC0::emitShift(const Instruction
*i
)
677 if (i
->op
== OP_SHR
) {
678 emitForm_A(i
, HEX64(58000000, 00000003)
679 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
681 emitForm_A(i
, HEX64(60000000, 00000003));
684 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
689 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
691 if (i
->encSize
== 8) {
692 emitForm_B(i
, HEX64(60000000, 00000000));
694 if (i
->op
== OP_PREEX2
)
697 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
698 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
700 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
705 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
707 if (i
->encSize
== 8) {
708 code
[0] = 0x00000000 | (subOp
<< 26);
709 code
[1] = 0xc8000000;
713 defId(i
->def(0), 14);
714 srcId(i
->src(0), 20);
716 assert(i
->src(0).getFile() == FILE_GPR
);
718 if (i
->saturate
) code
[0] |= 1 << 5;
720 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
721 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
723 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
725 assert(!i
->src(0).mod
.neg());
726 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
731 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
735 assert(i
->encSize
== 8);
737 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
742 if (!isFloatType(i
->dType
))
743 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
750 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
753 case ROUND_M
: code
[1] |= 1 << 17; break;
754 case ROUND_P
: code
[1] |= 2 << 17; break;
755 case ROUND_Z
: code
[1] |= 3 << 17; break;
756 case ROUND_NI
: code
[0] |= 1 << 7; break;
757 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
758 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
759 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
762 assert(!"invalid round mode");
768 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
772 case ROUND_MI
: code
[0] |= 1 << 16; break;
774 case ROUND_PI
: code
[0] |= 2 << 16; break;
776 case ROUND_ZI
: code
[0] |= 3 << 16; break;
783 CodeEmitterNVC0::emitCVT(Instruction
*i
)
785 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
788 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
789 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
790 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
795 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
796 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
797 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
799 if (i
->encSize
== 8) {
800 emitForm_B(i
, HEX64(10000000, 00000004));
804 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
805 code
[0] |= util_logbase2(typeSizeof(i
->dType
)) << 20;
806 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
812 if (neg
&& i
->op
!= OP_ABS
)
818 if (isSignedIntType(i
->dType
))
820 if (isSignedIntType(i
->sType
))
823 if (isFloatType(i
->dType
)) {
824 if (!isFloatType(i
->sType
))
825 code
[1] |= 0x08000000;
827 if (isFloatType(i
->sType
))
828 code
[1] |= 0x04000000;
830 code
[1] |= 0x0c000000;
833 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
836 if (isFloatType(i
->dType
)) {
837 if (isFloatType(i
->sType
))
840 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
842 assert(isFloatType(i
->sType
));
844 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
847 if (neg
) code
[0] |= 1 << 16;
848 if (sat
) code
[0] |= 1 << 18;
849 if (abs
) code
[0] |= 1 << 19;
856 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
861 if (i
->sType
== TYPE_F64
)
864 if (!isFloatType(i
->sType
))
867 if (isFloatType(i
->dType
) || isSignedIntType(i
->sType
))
871 case OP_SET_AND
: hi
= 0x10000000; break;
872 case OP_SET_OR
: hi
= 0x10200000; break;
873 case OP_SET_XOR
: hi
= 0x10400000; break;
878 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
881 srcId(i
->src(2), 32 + 17);
883 if (i
->def(0).getFile() == FILE_PREDICATE
) {
884 if (i
->sType
== TYPE_F32
)
885 code
[1] += 0x10000000;
887 code
[1] += 0x08000000;
890 defId(i
->def(0), 17);
892 defId(i
->def(1), 14);
900 emitCondCode(i
->setCond
, 32 + 23);
905 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
911 op
= HEX64(30000000, 00000023);
914 op
= HEX64(30000000, 00000003);
917 op
= HEX64(38000000, 00000000);
920 assert(!"invalid type for SLCT");
926 CondCode cc
= i
->setCond
;
928 if (i
->src(2).mod
.neg())
929 cc
= reverseCondCode(cc
);
931 emitCondCode(cc
, 32 + 23);
937 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
939 emitForm_A(i
, HEX64(20000000, 00000004));
941 if (i
->cc
== CC_NOT_P
|| i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
945 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
947 code
[0] = 0x00000006 | (i
->subOp
<< 26);
948 code
[1] = 0xf0000000;
950 emitCondCode(i
->predSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
953 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
955 code
[0] = 0x00000086;
956 code
[1] = 0xd0000000;
959 code
[1] |= i
->tex
.s
<< 8;
964 defId(i
->def(0), 14);
965 srcId(i
->src(0), 20);
969 isNextIndependentTex(const TexInstruction
*i
)
971 if (!i
->next
|| !isTextureOp(i
->next
->op
))
973 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
975 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
979 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
981 code
[0] = 0x00000006;
983 if (isNextIndependentTex(i
))
984 code
[0] |= 0x080; // t mode
986 code
[0] |= 0x100; // p mode
992 case OP_TEX
: code
[1] = 0x80000000; break;
993 case OP_TXB
: code
[1] = 0x84000000; break;
994 case OP_TXL
: code
[1] = 0x86000000; break;
995 case OP_TXF
: code
[1] = 0x90000000; break;
996 case OP_TXG
: code
[1] = 0xa0000000; break;
997 case OP_TXD
: code
[1] = 0xe0000000; break;
999 assert(!"invalid texture op");
1002 if (i
->op
== OP_TXF
) {
1003 if (!i
->tex
.levelZero
)
1004 code
[1] |= 0x02000000;
1006 if (i
->tex
.levelZero
) {
1007 code
[1] |= 0x02000000;
1010 if (i
->tex
.derivAll
)
1013 defId(i
->def(0), 14);
1014 srcId(i
->src(0), 20);
1018 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1020 code
[1] |= i
->tex
.mask
<< 14;
1022 code
[1] |= i
->tex
.r
;
1023 code
[1] |= i
->tex
.s
<< 8;
1024 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1025 code
[1] |= 1 << 18; // in 1st source (with array index)
1028 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1029 if (i
->tex
.target
.isCube())
1031 if (i
->tex
.target
.isArray())
1033 if (i
->tex
.target
.isShadow())
1036 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1038 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1040 if (i
->op
== OP_TXL
)
1041 code
[1] &= ~(1 << 26);
1043 if (i
->op
== OP_TXF
)
1044 code
[1] &= ~(1 << 25);
1046 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1047 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1050 if (i
->tex
.useOffsets
) // in vecSrc0.w
1057 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1059 code
[0] = 0x00000086;
1060 code
[1] = 0xc0000000;
1062 switch (i
->tex
.query
) {
1063 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1064 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1065 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1066 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1067 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1068 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1070 assert(!"invalid texture query");
1074 code
[1] |= i
->tex
.mask
<< 14;
1076 code
[1] |= i
->tex
.r
;
1077 code
[1] |= i
->tex
.s
<< 8;
1078 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1081 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1083 defId(i
->def(0), 14);
1084 srcId(i
->src(0), 20);
1091 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1093 code
[0] = 0x00000000 | (laneMask
<< 6);
1094 code
[1] = 0x48000000 | qOp
;
1096 defId(i
->def(0), 14);
1097 srcId(i
->src(0), 20);
1098 srcId(i
->srcExists(1) ? i
->src(1) : i
->src(0), 26);
1100 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1101 code
[0] |= 1 << 9; // dall
1107 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1109 const FlowInstruction
*f
= i
->asFlow();
1111 unsigned mask
; // bit 0: predicate, bit 1: target
1113 code
[0] = 0x00000007;
1117 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1118 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1123 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1124 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1129 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1130 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1131 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1132 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1133 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1135 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1136 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1137 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1138 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1140 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1141 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1142 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1144 assert(!"invalid flow operation");
1150 if (i
->flagsSrc
< 0)
1162 if (f
->op
== OP_CALL
) {
1164 assert(f
->absolute
);
1165 uint32_t pcAbs
= targ
->getBuiltinOffset(f
->target
.builtin
);
1166 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1167 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1169 assert(!f
->absolute
);
1170 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1171 code
[0] |= (pcRel
& 0x3f) << 26;
1172 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1176 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1177 // currently we don't want absolute branches
1178 assert(!f
->absolute
);
1179 code
[0] |= (pcRel
& 0x3f) << 26;
1180 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1185 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1187 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1189 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1190 code
[1] = 0x00000000 | (prim
>> 6);
1194 defId(i
->def(0), 14);
1195 srcId(i
->src(1), 20);
1199 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1201 code
[0] = 0x00000006;
1202 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1206 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1207 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1211 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1213 defId(i
->def(0), 14);
1214 srcId(i
->src(0).getIndirect(0), 20);
1215 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1219 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1221 unsigned int size
= typeSizeof(i
->dType
);
1223 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1224 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1226 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1233 assert(i
->src(1).getFile() == FILE_GPR
);
1235 srcId(i
->src(0).getIndirect(0), 20);
1236 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1237 srcId(i
->src(1), 26);
1241 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1243 code
[0] = 0x00000006;
1244 code
[1] = 0x1c000000;
1248 defId(i
->def(0), 14); // new secret address
1249 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1251 assert(i
->src(0).getFile() == FILE_GPR
);
1253 if (i
->op
== OP_EMIT
)
1255 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1259 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1261 code
[0] |= SDATA(i
->src(1)).u32
<< 26;
1263 srcId(i
->src(1), 26);
1268 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1270 if (i
->encSize
== 8) {
1271 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1273 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1275 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1280 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1282 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1284 if (i
->encSize
== 8) {
1285 code
[0] = 0x00000000;
1286 code
[1] = 0xc0000000 | (base
& 0xffff);
1291 if (i
->op
== OP_PINTERP
)
1292 srcId(i
->src(1), 26);
1294 code
[0] |= 0x3f << 26;
1296 srcId(i
->src(0).getIndirect(0), 20);
1298 assert(i
->op
== OP_PINTERP
);
1299 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1300 srcId(i
->src(1), 20);
1305 defId(i
->def(0), 14);
1307 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1308 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 17);
1310 code
[1] |= 0x3f << 17;
1314 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1347 assert(!"invalid type");
1354 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1375 assert(!"invalid caching mode");
1382 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1386 switch (i
->src(0).getFile()) {
1387 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1388 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1389 case FILE_MEMORY_SHARED
: opc
= 0xc9000000; break;
1391 assert(!"invalid memory file");
1395 code
[0] = 0x00000005;
1398 setAddress16(i
->src(0));
1399 srcId(i
->src(1), 14);
1400 srcId(i
->src(0).getIndirect(0), 20);
1404 emitLoadStoreType(i
->dType
);
1405 emitCachingMode(i
->cache
);
1409 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1413 code
[0] = 0x00000005;
1415 switch (i
->src(0).getFile()) {
1416 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1417 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1418 case FILE_MEMORY_SHARED
: opc
= 0xc1000000; break;
1419 case FILE_MEMORY_CONST
:
1420 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1421 emitMOV(i
); // not sure if this is any better
1424 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1425 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1428 assert(!"invalid memory file");
1434 defId(i
->def(0), 14);
1436 setAddress16(i
->src(0));
1437 srcId(i
->src(0).getIndirect(0), 20);
1441 emitLoadStoreType(i
->dType
);
1442 emitCachingMode(i
->cache
);
1446 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1448 switch (SDATA(ref
).sv
.sv
) {
1449 case SV_LANEID
: return 0x00;
1450 case SV_PHYSID
: return 0x03;
1451 case SV_VERTEX_COUNT
: return 0x10;
1452 case SV_INVOCATION_ID
: return 0x11;
1453 case SV_YDIR
: return 0x12;
1454 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1455 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1456 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1457 case SV_GRIDID
: return 0x2c;
1458 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1459 case SV_LBASE
: return 0x34;
1460 case SV_SBASE
: return 0x30;
1461 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1463 assert(!"no sreg for system value");
1469 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1471 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1472 uint8_t sr
= getSRegEncoding(i
->src(0));
1474 if (i
->encSize
== 8) {
1475 code
[0] = 0x00000004 | (sr
<< 26);
1476 code
[1] = 0x2c000000;
1478 code
[0] = 0x40000008 | (sr
<< 20);
1480 defId(i
->def(0), 14);
1484 if (i
->encSize
== 8) {
1487 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1488 opc
= HEX64(18000000, 000001e2
);
1490 if (i
->src(0).getFile() == FILE_PREDICATE
)
1491 opc
= HEX64(080e0000
, 1c000004
);
1493 opc
= HEX64(28000000, 00000004);
1495 opc
|= i
->lanes
<< 5;
1501 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1502 imm
= SDATA(i
->src(0)).u32
;
1503 if (imm
& 0xfff00000) {
1504 assert(!(imm
& 0x000fffff));
1505 code
[0] = 0x00000318 | imm
;
1507 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1508 code
[0] = 0x00000118 | (imm
<< 20);
1512 emitShortSrc2(i
->src(0));
1514 defId(i
->def(0), 14);
1521 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
1523 unsigned int size
= insn
->encSize
;
1525 if (writeIssueDelays
&& !(codeSize
& 0x3f))
1528 if (!insn
->encSize
) {
1529 ERROR("skipping unencodable instruction: "); insn
->print();
1532 if (codeSize
+ size
> codeSizeLimit
) {
1533 ERROR("code emitter output buffer too small\n");
1537 if (writeIssueDelays
) {
1538 if (!(codeSize
& 0x3f)) {
1539 code
[0] = 0x00000007; // cf issue delay "instruction"
1540 code
[1] = 0x20000000;
1544 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
1545 uint32_t *data
= code
- (id
* 2 + 2);
1547 data
[0] |= insn
->sched
<< (id
* 8 + 4);
1550 data
[0] |= insn
->sched
<< 28;
1551 data
[1] |= insn
->sched
>> 4;
1553 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
1557 // assert that instructions with multiple defs don't corrupt registers
1558 for (int d
= 0; insn
->defExists(d
); ++d
)
1559 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
1593 if (isFloatType(insn
->dType
))
1599 if (isFloatType(insn
->dType
))
1606 if (isFloatType(insn
->dType
))
1615 emitLogicOp(insn
, 0);
1618 emitLogicOp(insn
, 1);
1621 emitLogicOp(insn
, 2);
1631 emitSET(insn
->asCmp());
1637 emitSLCT(insn
->asCmp());
1679 emitTEX(insn
->asTex());
1682 emitTXQ(insn
->asTex());
1704 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
1707 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
1710 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
1722 ERROR("operation should have been eliminated");
1728 ERROR("operation should have been lowered\n");
1731 ERROR("unknow op\n");
1737 assert(insn
->encSize
== 8);
1740 code
+= insn
->encSize
/ 4;
1741 codeSize
+= insn
->encSize
;
1746 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
1748 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
1750 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
1753 if (i
->ftz
|| i
->saturate
|| i
->join
)
1755 if (i
->rnd
!= ROUND_N
)
1757 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
1760 if (i
->op
== OP_PINTERP
) {
1761 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
1764 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
1768 for (int s
= 0; i
->srcExists(s
); ++s
) {
1769 if (i
->src(s
).isIndirect(0))
1772 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
1773 if (SDATA(i
->src(s
)).offset
>= 0x100)
1775 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
1776 i
->getSrc(s
)->reg
.fileIndex
!= 16)
1779 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
1780 if (i
->dType
== TYPE_F32
) {
1781 if (SDATA(i
->src(s
)).u32
>= 0x100)
1784 if (SDATA(i
->src(s
)).u32
> 0xff)
1789 if (i
->op
== OP_CVT
)
1791 if (i
->src(s
).mod
!= Modifier(0)) {
1792 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
1793 if (i
->op
!= OP_RSQ
)
1795 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
1796 if (i
->op
!= OP_ADD
|| s
!= 0)
1804 // Simplified, erring on safe side.
1805 class SchedDataCalculator
: public Pass
1808 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
1814 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
1815 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
1816 int tex
; // TEX to non-TEX delay 17 (0x11)
1817 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
1818 int imul
; // integer MUL to MUL delay 3
1827 void rebase(const int base
)
1829 const int delta
= this->base
- base
;
1834 for (int i
= 0; i
< 64; ++i
) {
1838 for (int i
= 0; i
< 8; ++i
) {
1845 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
1855 memset(&rd
, 0, sizeof(rd
));
1856 memset(&wr
, 0, sizeof(wr
));
1857 memset(&res
, 0, sizeof(res
));
1859 int getLatest(const ScoreData
& d
) const
1862 for (int i
= 0; i
< 64; ++i
)
1865 for (int i
= 0; i
< 8; ++i
)
1872 inline int getLatestRd() const
1874 return getLatest(rd
);
1876 inline int getLatestWr() const
1878 return getLatest(wr
);
1880 inline int getLatest() const
1882 const int a
= getLatestRd();
1883 const int b
= getLatestWr();
1885 int max
= MAX2(a
, b
);
1886 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
1887 max
= MAX2(res
.ld
[f
], max
);
1888 max
= MAX2(res
.st
[f
], max
);
1890 max
= MAX2(res
.sfu
, max
);
1891 max
= MAX2(res
.imul
, max
);
1892 max
= MAX2(res
.tex
, max
);
1895 void setMax(const RegScores
*that
)
1897 for (int i
= 0; i
< 64; ++i
) {
1898 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
1899 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
1901 for (int i
= 0; i
< 8; ++i
) {
1902 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
1903 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
1905 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
1906 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
1908 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
1909 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
1910 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
1912 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
1913 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
1914 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
1916 void print(int cycle
)
1918 for (int i
= 0; i
< 64; ++i
) {
1919 if (rd
.r
[i
] > cycle
)
1920 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
1921 if (wr
.r
[i
] > cycle
)
1922 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
1924 for (int i
= 0; i
< 8; ++i
) {
1925 if (rd
.p
[i
] > cycle
)
1926 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
1927 if (wr
.p
[i
] > cycle
)
1928 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
1931 INFO("rd $c @ %i\n", rd
.c
);
1933 INFO("wr $c @ %i\n", wr
.c
);
1934 if (res
.sfu
> cycle
)
1935 INFO("sfu @ %i\n", res
.sfu
);
1936 if (res
.imul
> cycle
)
1937 INFO("imul @ %i\n", res
.imul
);
1938 if (res
.tex
> cycle
)
1939 INFO("tex @ %i\n", res
.tex
);
1943 RegScores
*score
; // for current BB
1944 std::vector
<RegScores
> scoreBoards
;
1951 bool visit(Function
*);
1952 bool visit(BasicBlock
*);
1954 void commitInsn(const Instruction
*, int cycle
);
1955 int calcDelay(const Instruction
*, int cycle
) const;
1956 void setDelay(Instruction
*, int delay
, Instruction
*next
);
1958 void recordRd(const Value
*, const int ready
);
1959 void recordWr(const Value
*, const int ready
);
1960 void checkRd(const Value
*, int cycle
, int& delay
) const;
1961 void checkWr(const Value
*, int cycle
, int& delay
) const;
1963 int getCycles(const Instruction
*, int origDelay
) const;
1967 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
1969 if (insn
->op
== OP_EXIT
)
1970 delay
= MAX2(delay
, 14);
1972 if (insn
->op
== OP_TEXBAR
) {
1973 // TODO: except if results not used before EXIT
1976 if (insn
->op
== OP_JOIN
|| insn
->join
) {
1979 if (delay
>= 0 || prevData
== 0x04 ||
1980 !next
|| !targ
->canDualIssue(insn
, next
)) {
1981 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
1982 if (prevOp
== OP_EXPORT
)
1983 insn
->sched
|= 0x40;
1985 insn
->sched
|= 0x20;
1987 insn
->sched
= 0x04; // dual-issue
1990 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
1991 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
1994 prevData
= insn
->sched
;
1998 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
2000 if (insn
->sched
& 0x80) {
2001 int c
= (insn
->sched
& 0x0f) * 2 + 1;
2002 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
2006 if (insn
->sched
& 0x60)
2007 return (insn
->sched
& 0x1f) + 1;
2008 return (insn
->sched
== 0x04) ? 0 : 32;
2012 SchedDataCalculator::visit(Function
*func
)
2014 scoreBoards
.resize(func
->cfg
.getSize());
2015 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
2016 scoreBoards
[i
].wipe();
2021 SchedDataCalculator::visit(BasicBlock
*bb
)
2024 Instruction
*next
= NULL
;
2030 score
= &scoreBoards
.at(bb
->getId());
2032 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
2033 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
2034 if (in
->getExit()) {
2035 if (prevData
!= 0x04)
2036 prevData
= in
->getExit()->sched
;
2037 prevOp
= in
->getExit()->op
;
2039 if (ei
.getType() != Graph::Edge::BACK
)
2040 score
->setMax(&scoreBoards
.at(in
->getId()));
2041 // back branches will wait until all target dependencies are satisfied
2043 if (bb
->cfg
.incidentCount() > 1)
2046 #ifdef NVC0_DEBUG_SCHED_DATA
2047 INFO("=== BB:%i initial scores\n", bb
->getId());
2048 score
->print(cycle
);
2051 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
2054 commitInsn(insn
, cycle
);
2055 int delay
= calcDelay(next
, cycle
);
2056 setDelay(insn
, delay
, next
);
2057 cycle
+= getCycles(insn
, delay
);
2059 #ifdef NVC0_DEBUG_SCHED_DATA
2060 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
2067 commitInsn(insn
, cycle
);
2071 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
2072 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
2074 if (ei
.getType() != Graph::Edge::BACK
) {
2075 // only test the first instruction of the outgoing block
2076 next
= out
->getEntry();
2078 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
2080 // wait until all dependencies are satisfied
2081 const int regsFree
= score
->getLatest();
2082 next
= out
->getFirst();
2083 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
2084 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
2085 c
+= getCycles(next
, bbDelay
);
2090 if (bb
->cfg
.outgoingCount() != 1)
2092 setDelay(insn
, bbDelay
, next
);
2093 cycle
+= getCycles(insn
, bbDelay
);
2095 score
->rebase(cycle
); // common base for initializing out blocks' scores
2099 #define NVE4_MAX_ISSUE_DELAY 0x1f
2101 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
2103 int delay
= 0, ready
= cycle
;
2105 for (int s
= 0; insn
->srcExists(s
); ++s
)
2106 checkRd(insn
->getSrc(s
), cycle
, delay
);
2107 // WAR & WAW don't seem to matter
2108 // for (int s = 0; insn->srcExists(s); ++s)
2109 // recordRd(insn->getSrc(s), cycle);
2111 switch (Target::getOpClass(insn
->op
)) {
2113 ready
= score
->res
.sfu
;
2116 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2117 ready
= score
->res
.imul
;
2119 case OPCLASS_TEXTURE
:
2120 ready
= score
->res
.tex
;
2123 ready
= score
->res
.ld
[insn
->src(0).getFile()];
2126 ready
= score
->res
.st
[insn
->src(0).getFile()];
2131 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
2132 ready
= MAX2(ready
, score
->res
.tex
);
2134 delay
= MAX2(delay
, ready
- cycle
);
2136 // if can issue next cycle, delay is 0, not 1
2137 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
2141 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
2143 const int ready
= cycle
+ targ
->getLatency(insn
);
2145 for (int d
= 0; insn
->defExists(d
); ++d
)
2146 recordWr(insn
->getDef(d
), ready
);
2147 // WAR & WAW don't seem to matter
2148 // for (int s = 0; insn->srcExists(s); ++s)
2149 // recordRd(insn->getSrc(s), cycle);
2151 switch (Target::getOpClass(insn
->op
)) {
2153 score
->res
.sfu
= cycle
+ 4;
2156 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2157 score
->res
.imul
= cycle
+ 4;
2159 case OPCLASS_TEXTURE
:
2160 score
->res
.tex
= cycle
+ 18;
2163 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
2165 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
2166 score
->res
.st
[insn
->src(0).getFile()] = ready
;
2169 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
2170 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
2173 if (insn
->op
== OP_TEXBAR
)
2174 score
->res
.tex
= cycle
;
2180 #ifdef NVC0_DEBUG_SCHED_DATA
2181 score
->print(cycle
);
2186 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
2191 switch (v
->reg
.file
) {
2194 b
= a
+ v
->reg
.size
/ 4;
2195 for (int r
= a
; r
< b
; ++r
)
2196 ready
= MAX2(ready
, score
->rd
.r
[r
]);
2198 case FILE_PREDICATE
:
2199 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
2202 ready
= MAX2(ready
, score
->rd
.c
);
2204 case FILE_SHADER_INPUT
:
2205 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
2206 case FILE_MEMORY_LOCAL
:
2207 case FILE_MEMORY_CONST
:
2208 case FILE_MEMORY_SHARED
:
2209 case FILE_MEMORY_GLOBAL
:
2210 case FILE_SYSTEM_VALUE
:
2211 // TODO: any restrictions here ?
2213 case FILE_IMMEDIATE
:
2220 delay
= MAX2(delay
, ready
- cycle
);
2224 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
2229 switch (v
->reg
.file
) {
2232 b
= a
+ v
->reg
.size
/ 4;
2233 for (int r
= a
; r
< b
; ++r
)
2234 ready
= MAX2(ready
, score
->wr
.r
[r
]);
2236 case FILE_PREDICATE
:
2237 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
2240 assert(v
->reg
.file
== FILE_FLAGS
);
2241 ready
= MAX2(ready
, score
->wr
.c
);
2245 delay
= MAX2(delay
, ready
- cycle
);
2249 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
2251 int a
= v
->reg
.data
.id
;
2253 if (v
->reg
.file
== FILE_GPR
) {
2254 int b
= a
+ v
->reg
.size
/ 4;
2255 for (int r
= a
; r
< b
; ++r
)
2256 score
->rd
.r
[r
] = ready
;
2258 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
2259 if (v
->reg
.file
== FILE_PREDICATE
) {
2260 score
->rd
.p
[a
] = ready
+ 4;
2262 assert(v
->reg
.file
== FILE_FLAGS
);
2263 score
->rd
.c
= ready
+ 4;
2268 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
2270 int a
= v
->reg
.data
.id
;
2272 if (v
->reg
.file
== FILE_GPR
) {
2273 int b
= a
+ v
->reg
.size
/ 4;
2274 for (int r
= a
; r
< b
; ++r
)
2275 score
->wr
.r
[r
] = ready
;
2277 if (v
->reg
.file
== FILE_PREDICATE
) {
2278 score
->wr
.p
[a
] = ready
;
2280 if (v
->reg
.file
== FILE_FLAGS
) {
2281 score
->wr
.c
= ready
;
2286 CodeEmitterNVC0::prepareEmission(Function
*func
)
2288 const Target
*targ
= func
->getProgram()->getTarget();
2290 CodeEmitter::prepareEmission(func
);
2292 if (targ
->hasSWSched
) {
2293 SchedDataCalculator
sched(targ
);
2294 sched
.run(func
, true, true);
2298 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
2299 : CodeEmitter(target
),
2300 writeIssueDelays(target
->hasSWSched
)
2303 codeSize
= codeSizeLimit
= 0;
2308 TargetNVC0::getCodeEmitter(Program::Type type
)
2310 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
2311 emit
->setProgramType(type
);
2315 } // namespace nv50_ir