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 emitPERMT(const Instruction
*);
108 void emitShift(const Instruction
*);
110 void emitSFnOp(const Instruction
*, uint8_t subOp
);
112 void emitCVT(Instruction
*);
113 void emitMINMAX(const Instruction
*);
114 void emitPreOp(const Instruction
*);
116 void emitSET(const CmpInstruction
*);
117 void emitSLCT(const CmpInstruction
*);
118 void emitSELP(const Instruction
*);
120 void emitTEXBAR(const Instruction
*);
121 void emitTEX(const TexInstruction
*);
122 void emitTEXCSAA(const TexInstruction
*);
123 void emitTXQ(const TexInstruction
*);
125 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
127 void emitFlow(const Instruction
*);
128 void emitBAR(const Instruction
*);
130 void emitSUCLAMPMode(uint16_t);
131 void emitSUCalc(Instruction
*);
132 void emitSULDGB(const TexInstruction
*);
133 void emitSUSTGx(const TexInstruction
*);
135 void emitVSHL(const Instruction
*);
136 void emitVectorSubOp(const Instruction
*);
138 inline void defId(const ValueDef
&, const int pos
);
139 inline void defId(const Instruction
*, int d
, const int pos
);
140 inline void srcId(const ValueRef
&, const int pos
);
141 inline void srcId(const ValueRef
*, const int pos
);
142 inline void srcId(const Instruction
*, int s
, const int pos
);
143 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
145 inline bool isLIMM(const ValueRef
&, DataType ty
);
148 // for better visibility
149 #define HEX64(h, l) 0x##h##l##ULL
151 #define SDATA(a) ((a).rep()->reg.data)
152 #define DDATA(a) ((a).rep()->reg.data)
154 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
156 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
159 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
161 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
164 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
166 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
167 code
[pos
/ 32] |= r
<< (pos
% 32);
171 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
173 const uint32_t offset
= SDATA(src
).offset
>> shr
;
175 code
[pos
/ 32] |= offset
<< (pos
% 32);
176 if (pos
&& (pos
< 32))
177 code
[1] |= offset
>> (32 - pos
);
180 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
182 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
185 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
187 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
188 code
[pos
/ 32] |= r
<< (pos
% 32);
191 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
193 const ImmediateValue
*imm
= ref
.get()->asImm();
195 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
199 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
202 case ROUND_M
: code
[1] |= 1 << 23; break;
203 case ROUND_P
: code
[1] |= 2 << 23; break;
204 case ROUND_Z
: code
[1] |= 3 << 23; break;
206 assert(insn
->rnd
== ROUND_N
);
212 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
214 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
215 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
216 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
217 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
220 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
225 case CC_LT
: val
= 0x1; break;
226 case CC_LTU
: val
= 0x9; break;
227 case CC_EQ
: val
= 0x2; break;
228 case CC_EQU
: val
= 0xa; break;
229 case CC_LE
: val
= 0x3; break;
230 case CC_LEU
: val
= 0xb; break;
231 case CC_GT
: val
= 0x4; break;
232 case CC_GTU
: val
= 0xc; break;
233 case CC_NE
: val
= 0x5; break;
234 case CC_NEU
: val
= 0xd; break;
235 case CC_GE
: val
= 0x6; break;
236 case CC_GEU
: val
= 0xe; break;
237 case CC_TR
: val
= 0xf; break;
238 case CC_FL
: val
= 0x0; break;
240 case CC_A
: val
= 0x14; break;
241 case CC_NA
: val
= 0x13; break;
242 case CC_S
: val
= 0x15; break;
243 case CC_NS
: val
= 0x12; break;
244 case CC_C
: val
= 0x16; break;
245 case CC_NC
: val
= 0x11; break;
246 case CC_O
: val
= 0x17; break;
247 case CC_NO
: val
= 0x10; break;
251 assert(!"invalid condition code");
254 code
[pos
/ 32] |= val
<< (pos
% 32);
258 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
260 if (i
->predSrc
>= 0) {
261 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
262 srcId(i
->src(i
->predSrc
), 10);
263 if (i
->cc
== CC_NOT_P
)
264 code
[0] |= 0x2000; // negate
271 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
273 switch (src
.getFile()) {
274 case FILE_MEMORY_GLOBAL
:
275 srcAddr32(src
, 26, 0);
277 case FILE_MEMORY_LOCAL
:
278 case FILE_MEMORY_SHARED
:
282 assert(src
.getFile() == FILE_MEMORY_CONST
);
289 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
291 Symbol
*sym
= src
.get()->asSym();
295 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
296 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
300 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
302 Symbol
*sym
= src
.get()->asSym();
306 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
307 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
311 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
313 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
317 u32
= imm
->reg
.data
.u32
;
319 if ((code
[0] & 0xf) == 0x2) {
321 code
[0] |= (u32
& 0x3f) << 26;
324 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
326 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
327 assert(!(code
[1] & 0xc000));
329 code
[0] |= (u32
& 0x3f) << 26;
330 code
[1] |= 0xc000 | (u32
>> 6);
333 assert(!(u32
& 0x00000fff));
334 assert(!(code
[1] & 0xc000));
335 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
336 code
[1] |= 0xc000 | (u32
>> 18);
340 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
342 const ImmediateValue
*imm
= ref
.get()->asImm();
344 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
346 assert(s8
== imm
->reg
.data
.s32
);
348 code
[0] |= (s8
& 0x3f) << 26;
349 code
[0] |= (s8
>> 6) << 8;
353 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
360 defId(i
->def(0), 14);
363 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
366 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
367 switch (i
->getSrc(s
)->reg
.file
) {
368 case FILE_MEMORY_CONST
:
369 assert(!(code
[1] & 0xc000));
370 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
371 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
372 setAddress16(i
->src(s
));
376 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
377 assert(!(code
[1] & 0xc000));
381 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
383 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
386 // ignore here, can be predicate or flags, but must not be address
393 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
400 defId(i
->def(0), 14);
402 switch (i
->src(0).getFile()) {
403 case FILE_MEMORY_CONST
:
404 assert(!(code
[1] & 0xc000));
405 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
406 setAddress16(i
->src(0));
409 assert(!(code
[1] & 0xc000));
413 srcId(i
->src(0), 26);
416 // ignore here, can be predicate or flags, but must not be address
422 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
427 if (opc
== 0x0d || opc
== 0x0e)
430 defId(i
->def(0), 14);
431 srcId(i
->src(0), 20);
433 assert(pred
|| (i
->predSrc
< 0));
437 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
438 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
439 assert(!(code
[0] & (0x300 >> ss2a
)));
440 switch (i
->src(s
).get()->reg
.fileIndex
) {
441 case 0: code
[0] |= 0x100 >> ss2a
; break;
442 case 1: code
[0] |= 0x200 >> ss2a
; break;
443 case 16: code
[0] |= 0x300 >> ss2a
; break;
445 ERROR("invalid c[] space for short form\n");
449 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
451 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
453 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
455 setImmediateS8(i
->src(s
));
457 if (i
->src(s
).getFile() == FILE_GPR
) {
458 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
464 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
466 if (src
.getFile() == FILE_MEMORY_CONST
) {
467 switch (src
.get()->reg
.fileIndex
) {
468 case 0: code
[0] |= 0x100; break;
469 case 1: code
[0] |= 0x200; break;
470 case 16: code
[0] |= 0x300; break;
472 assert(!"unsupported file index for short op");
475 srcAddr32(src
, 20, 2);
478 assert(src
.getFile() == FILE_GPR
);
483 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
485 code
[0] = 0x000001e4;
486 code
[1] = 0x40000000;
491 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
493 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
495 if (i
->encSize
== 8) {
496 if (isLIMM(i
->src(1), TYPE_F32
)) {
497 emitForm_A(i
, HEX64(20000000, 00000002));
499 emitForm_A(i
, HEX64(30000000, 00000000));
501 if (i
->src(2).mod
.neg())
514 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
515 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
523 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
525 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
527 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
529 if (i
->encSize
== 8) {
530 if (isLIMM(i
->src(1), TYPE_F32
)) {
531 assert(i
->postFactor
== 0); // constant folded, hopefully
532 emitForm_A(i
, HEX64(30000000, 00000002));
534 emitForm_A(i
, HEX64(58000000, 00000000));
536 code
[1] |= ((i
->postFactor
> 0) ?
537 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
540 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
551 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
552 emitForm_S(i
, 0xa8, true);
557 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
559 if (i
->encSize
== 8) {
560 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
561 emitForm_A(i
, HEX64(10000000, 00000002));
563 emitForm_A(i
, HEX64(50000000, 00000003));
565 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
567 if (i
->sType
== TYPE_S32
)
569 if (i
->dType
== TYPE_S32
)
572 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
574 if (i
->sType
== TYPE_S32
)
580 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
582 if (i
->encSize
== 8) {
583 if (isLIMM(i
->src(1), TYPE_F32
)) {
584 assert(!i
->saturate
);
585 emitForm_A(i
, HEX64(28000000, 00000002));
587 code
[0] |= i
->src(0).mod
.abs() << 7;
588 code
[0] |= i
->src(0).mod
.neg() << 9;
590 if (i
->src(1).mod
.abs())
591 code
[1] &= 0xfdffffff;
592 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
593 code
[1] ^= 0x02000000;
595 emitForm_A(i
, HEX64(50000000, 00000000));
602 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
607 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
608 !i
->src(0).mod
.abs() &&
609 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
611 emitForm_S(i
, 0x49, true);
613 if (i
->src(0).mod
.neg())
619 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
623 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
624 assert(!i
->src(0).mod
.neg() || !i
->src(1).mod
.neg());
626 if (i
->src(0).mod
.neg())
628 if (i
->src(1).mod
.neg())
630 if (i
->op
== OP_SUB
) {
632 assert(addOp
!= 0x300); // would be add-plus-one
635 if (i
->encSize
== 8) {
636 if (isLIMM(i
->src(1), TYPE_U32
)) {
637 emitForm_A(i
, HEX64(08000000, 00000002));
639 code
[1] |= 1 << 26; // write carry
641 emitForm_A(i
, HEX64(48000000, 00000003));
643 code
[1] |= 1 << 16; // write carry
649 if (i
->flagsSrc
>= 0) // add carry
652 assert(!(addOp
& 0x100));
653 emitForm_S(i
, (addOp
>> 3) |
654 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
660 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
662 assert(i
->encSize
== 8);
663 emitForm_A(i
, HEX64(20000000, 00000003));
665 if (isSignedType(i
->dType
))
667 if (isSignedType(i
->sType
))
670 code
[1] |= i
->saturate
<< 24;
672 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
673 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
675 if (i
->src(2).mod
.neg()) code
[0] |= 0x10;
676 if (i
->src(1).mod
.neg() ^
677 i
->src(0).mod
.neg()) code
[0] |= 0x20;
679 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
684 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
686 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
688 emitForm_A(i
, HEX64(00000000, 00000003));
690 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
691 code
[1] |= 0x01800000;
693 code
[0] |= (i
->subOp
& 0x00f) << 7;
694 code
[0] |= (i
->subOp
& 0x0f0) << 1;
695 code
[0] |= (i
->subOp
& 0x100) >> 3;
696 code
[0] |= (i
->subOp
& 0x200) >> 2;
697 code
[1] |= (i
->subOp
& 0xc00) << 13;
700 if (i
->flagsDef
>= 0)
705 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
707 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
708 assert(i
->encSize
== 8);
710 emitForm_A(i
, HEX64(38000000, 00000003));
712 if (i
->dType
== TYPE_S32
)
717 CodeEmitterNVC0::emitNOT(Instruction
*i
)
719 assert(i
->encSize
== 8);
720 i
->setSrc(1, i
->src(0));
721 emitForm_A(i
, HEX64(68000000, 000001c3
));
725 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
727 if (i
->def(0).getFile() == FILE_PREDICATE
) {
728 code
[0] = 0x00000004 | (subOp
<< 30);
729 code
[1] = 0x0c000000;
733 defId(i
->def(0), 17);
734 srcId(i
->src(0), 20);
735 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
736 srcId(i
->src(1), 26);
737 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
739 if (i
->defExists(1)) {
740 defId(i
->def(1), 14);
745 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
746 code
[1] |= subOp
<< 21;
747 srcId(i
->src(2), 17);
748 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 20;
750 code
[1] |= 0x000e0000;
753 if (i
->encSize
== 8) {
754 if (isLIMM(i
->src(1), TYPE_U32
)) {
755 emitForm_A(i
, HEX64(38000000, 00000002));
757 if (i
->flagsDef
>= 0)
760 emitForm_A(i
, HEX64(68000000, 00000003));
762 if (i
->flagsDef
>= 0)
765 code
[0] |= subOp
<< 6;
767 if (i
->flagsSrc
>= 0) // carry
770 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
771 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
773 emitForm_S(i
, (subOp
<< 5) |
774 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
779 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
781 emitForm_A(i
, HEX64(54000000, 00000004));
783 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
784 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
788 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
790 emitForm_A(i
, HEX64(28000000, 00000003));
794 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
796 emitForm_A(i
, HEX64(70000000, 00000003));
798 if (i
->dType
== TYPE_S32
)
800 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
805 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
807 emitForm_A(i
, HEX64(24000000, 00000004));
809 code
[0] |= i
->subOp
<< 5;
813 CodeEmitterNVC0::emitShift(const Instruction
*i
)
815 if (i
->op
== OP_SHR
) {
816 emitForm_A(i
, HEX64(58000000, 00000003)
817 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
819 emitForm_A(i
, HEX64(60000000, 00000003));
822 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
827 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
829 if (i
->encSize
== 8) {
830 emitForm_B(i
, HEX64(60000000, 00000000));
832 if (i
->op
== OP_PREEX2
)
835 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
836 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
838 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
843 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
845 if (i
->encSize
== 8) {
846 code
[0] = 0x00000000 | (subOp
<< 26);
847 code
[1] = 0xc8000000;
851 defId(i
->def(0), 14);
852 srcId(i
->src(0), 20);
854 assert(i
->src(0).getFile() == FILE_GPR
);
856 if (i
->saturate
) code
[0] |= 1 << 5;
858 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
859 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
861 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
863 assert(!i
->src(0).mod
.neg());
864 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
869 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
873 assert(i
->encSize
== 8);
875 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
880 if (!isFloatType(i
->dType
))
881 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
888 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
891 case ROUND_M
: code
[1] |= 1 << 17; break;
892 case ROUND_P
: code
[1] |= 2 << 17; break;
893 case ROUND_Z
: code
[1] |= 3 << 17; break;
894 case ROUND_NI
: code
[0] |= 1 << 7; break;
895 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
896 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
897 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
900 assert(!"invalid round mode");
906 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
910 case ROUND_MI
: code
[0] |= 1 << 16; break;
912 case ROUND_PI
: code
[0] |= 2 << 16; break;
914 case ROUND_ZI
: code
[0] |= 3 << 16; break;
921 CodeEmitterNVC0::emitCVT(Instruction
*i
)
923 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
927 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
928 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
929 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
934 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
935 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
936 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
938 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
943 if (i
->encSize
== 8) {
944 emitForm_B(i
, HEX64(10000000, 00000004));
948 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
949 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
950 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
956 if (neg
&& i
->op
!= OP_ABS
)
962 if (isSignedIntType(dType
))
964 if (isSignedIntType(i
->sType
))
967 if (isFloatType(dType
)) {
968 if (!isFloatType(i
->sType
))
969 code
[1] |= 0x08000000;
971 if (isFloatType(i
->sType
))
972 code
[1] |= 0x04000000;
974 code
[1] |= 0x0c000000;
977 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
980 if (isFloatType(dType
)) {
981 if (isFloatType(i
->sType
))
984 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
986 assert(isFloatType(i
->sType
));
988 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
991 if (neg
) code
[0] |= 1 << 16;
992 if (sat
) code
[0] |= 1 << 18;
993 if (abs
) code
[0] |= 1 << 19;
1000 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1005 if (i
->sType
== TYPE_F64
)
1008 if (!isFloatType(i
->sType
))
1011 if (isFloatType(i
->dType
) || isSignedIntType(i
->sType
))
1015 case OP_SET_AND
: hi
= 0x10000000; break;
1016 case OP_SET_OR
: hi
= 0x10200000; break;
1017 case OP_SET_XOR
: hi
= 0x10400000; break;
1022 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1024 if (i
->op
!= OP_SET
)
1025 srcId(i
->src(2), 32 + 17);
1027 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1028 if (i
->sType
== TYPE_F32
)
1029 code
[1] += 0x10000000;
1031 code
[1] += 0x08000000;
1033 code
[0] &= ~0xfc000;
1034 defId(i
->def(0), 17);
1035 if (i
->defExists(1))
1036 defId(i
->def(1), 14);
1044 emitCondCode(i
->setCond
, 32 + 23);
1049 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1055 op
= HEX64(30000000, 00000023);
1058 op
= HEX64(30000000, 00000003);
1061 op
= HEX64(38000000, 00000000);
1064 assert(!"invalid type for SLCT");
1070 CondCode cc
= i
->setCond
;
1072 if (i
->src(2).mod
.neg())
1073 cc
= reverseCondCode(cc
);
1075 emitCondCode(cc
, 32 + 23);
1081 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1083 emitForm_A(i
, HEX64(20000000, 00000004));
1085 if (i
->cc
== CC_NOT_P
|| i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1089 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1091 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1092 code
[1] = 0xf0000000;
1094 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1097 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1099 code
[0] = 0x00000086;
1100 code
[1] = 0xd0000000;
1102 code
[1] |= i
->tex
.r
;
1103 code
[1] |= i
->tex
.s
<< 8;
1105 if (i
->tex
.liveOnly
)
1108 defId(i
->def(0), 14);
1109 srcId(i
->src(0), 20);
1113 isNextIndependentTex(const TexInstruction
*i
)
1115 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1117 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1119 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1123 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1125 code
[0] = 0x00000006;
1127 if (isNextIndependentTex(i
))
1128 code
[0] |= 0x080; // t mode
1130 code
[0] |= 0x100; // p mode
1132 if (i
->tex
.liveOnly
)
1136 case OP_TEX
: code
[1] = 0x80000000; break;
1137 case OP_TXB
: code
[1] = 0x84000000; break;
1138 case OP_TXL
: code
[1] = 0x86000000; break;
1139 case OP_TXF
: code
[1] = 0x90000000; break;
1140 case OP_TXG
: code
[1] = 0xa0000000; break;
1141 case OP_TXD
: code
[1] = 0xe0000000; break;
1143 assert(!"invalid texture op");
1146 if (i
->op
== OP_TXF
) {
1147 if (!i
->tex
.levelZero
)
1148 code
[1] |= 0x02000000;
1150 if (i
->tex
.levelZero
) {
1151 code
[1] |= 0x02000000;
1154 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1157 defId(i
->def(0), 14);
1158 srcId(i
->src(0), 20);
1162 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1164 code
[1] |= i
->tex
.mask
<< 14;
1166 code
[1] |= i
->tex
.r
;
1167 code
[1] |= i
->tex
.s
<< 8;
1168 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1169 code
[1] |= 1 << 18; // in 1st source (with array index)
1172 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1173 if (i
->tex
.target
.isCube())
1175 if (i
->tex
.target
.isArray())
1177 if (i
->tex
.target
.isShadow())
1180 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1182 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1184 if (i
->op
== OP_TXL
)
1185 code
[1] &= ~(1 << 26);
1187 if (i
->op
== OP_TXF
)
1188 code
[1] &= ~(1 << 25);
1190 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1191 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1194 if (i
->tex
.useOffsets
) // in vecSrc0.w
1201 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1203 code
[0] = 0x00000086;
1204 code
[1] = 0xc0000000;
1206 switch (i
->tex
.query
) {
1207 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1208 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1209 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1210 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1211 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1212 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1214 assert(!"invalid texture query");
1218 code
[1] |= i
->tex
.mask
<< 14;
1220 code
[1] |= i
->tex
.r
;
1221 code
[1] |= i
->tex
.s
<< 8;
1222 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1225 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1227 defId(i
->def(0), 14);
1228 srcId(i
->src(0), 20);
1235 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1237 code
[0] = 0x00000000 | (laneMask
<< 6);
1238 code
[1] = 0x48000000 | qOp
;
1240 defId(i
->def(0), 14);
1241 srcId(i
->src(0), 20);
1242 srcId(i
->srcExists(1) ? i
->src(1) : i
->src(0), 26);
1244 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1245 code
[0] |= 1 << 9; // dall
1251 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1253 const FlowInstruction
*f
= i
->asFlow();
1255 unsigned mask
; // bit 0: predicate, bit 1: target
1257 code
[0] = 0x00000007;
1261 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1262 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1267 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1269 code
[0] |= 0x4000; // indirect calls always use c[] source
1273 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1274 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1275 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1276 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1277 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1279 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1280 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1281 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1282 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1284 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1285 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1286 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1288 assert(!"invalid flow operation");
1294 if (i
->flagsSrc
< 0)
1307 if (code
[0] & 0x4000) {
1308 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1309 setAddress16(i
->src(0));
1310 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1311 if (f
->op
== OP_BRA
)
1312 srcId(f
->src(0).getIndirect(0), 20);
1318 if (f
->op
== OP_CALL
) {
1323 assert(f
->absolute
);
1324 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1325 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1326 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1328 assert(!f
->absolute
);
1329 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1330 code
[0] |= (pcRel
& 0x3f) << 26;
1331 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1335 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1336 // currently we don't want absolute branches
1337 assert(!f
->absolute
);
1338 code
[0] |= (pcRel
& 0x3f) << 26;
1339 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1344 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1346 Value
*rDef
= NULL
, *pDef
= NULL
;
1349 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1350 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1351 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1352 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1355 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1358 code
[1] = 0x50000000;
1360 code
[0] |= 63 << 14;
1366 if (i
->src(0).getFile() == FILE_GPR
) {
1367 srcId(i
->src(0), 20);
1369 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1371 code
[0] |= imm
->reg
.data
.u32
<< 20;
1375 if (i
->src(1).getFile() == FILE_GPR
) {
1376 srcId(i
->src(1), 26);
1378 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1380 code
[0] |= imm
->reg
.data
.u32
<< 26;
1381 code
[1] |= imm
->reg
.data
.u32
>> 6;
1384 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1385 srcId(i
->src(2), 32 + 17);
1386 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1392 if (i
->defExists(0)) {
1393 if (i
->def(0).getFile() == FILE_GPR
)
1394 rDef
= i
->getDef(0);
1396 pDef
= i
->getDef(0);
1398 if (i
->defExists(1)) {
1399 if (i
->def(1).getFile() == FILE_GPR
)
1400 rDef
= i
->getDef(1);
1402 pDef
= i
->getDef(1);
1406 code
[0] &= ~(63 << 14);
1410 code
[1] &= ~(7 << 21);
1411 defId(pDef
, 32 + 21);
1416 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1418 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1420 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1421 code
[1] = 0x00000000 | (prim
>> 6);
1425 defId(i
->def(0), 14);
1426 srcId(i
->src(1), 20);
1430 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1432 code
[0] = 0x00000006;
1433 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1437 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1438 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1442 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1444 defId(i
->def(0), 14);
1445 srcId(i
->src(0).getIndirect(0), 20);
1446 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1450 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1452 unsigned int size
= typeSizeof(i
->dType
);
1454 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1455 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1457 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1464 assert(i
->src(1).getFile() == FILE_GPR
);
1466 srcId(i
->src(0).getIndirect(0), 20);
1467 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1468 srcId(i
->src(1), 26);
1472 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1474 code
[0] = 0x00000006;
1475 code
[1] = 0x1c000000;
1479 defId(i
->def(0), 14); // new secret address
1480 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1482 assert(i
->src(0).getFile() == FILE_GPR
);
1484 if (i
->op
== OP_EMIT
)
1486 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1490 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1491 // Using immediate encoding here triggers an invalid opcode error
1492 // or random results when error reporting is disabled.
1493 // TODO: figure this out when we get multiple vertex streams
1494 assert(SDATA(i
->src(1)).u32
== 0);
1496 // code[1] |= 0xc000;
1497 // code[0] |= SDATA(i->src(1)).u32 << 26;
1499 srcId(i
->src(1), 26);
1504 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1506 if (i
->encSize
== 8) {
1507 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1509 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1511 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1516 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1518 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1520 if (i
->encSize
== 8) {
1521 code
[0] = 0x00000000;
1522 code
[1] = 0xc0000000 | (base
& 0xffff);
1527 if (i
->op
== OP_PINTERP
)
1528 srcId(i
->src(1), 26);
1530 code
[0] |= 0x3f << 26;
1532 srcId(i
->src(0).getIndirect(0), 20);
1534 assert(i
->op
== OP_PINTERP
);
1535 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1536 srcId(i
->src(1), 20);
1541 defId(i
->def(0), 14);
1543 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1544 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 17);
1546 code
[1] |= 0x3f << 17;
1550 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1583 assert(!"invalid type");
1590 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1611 assert(!"invalid caching mode");
1618 uses64bitAddress(const Instruction
*ldst
)
1620 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1621 ldst
->src(0).isIndirect(0) &&
1622 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1626 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1630 switch (i
->src(0).getFile()) {
1631 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1632 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1633 case FILE_MEMORY_SHARED
: opc
= 0xc9000000; break;
1635 assert(!"invalid memory file");
1639 code
[0] = 0x00000005;
1642 setAddressByFile(i
->src(0));
1643 srcId(i
->src(1), 14);
1644 srcId(i
->src(0).getIndirect(0), 20);
1645 if (uses64bitAddress(i
))
1650 emitLoadStoreType(i
->dType
);
1651 emitCachingMode(i
->cache
);
1655 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1659 code
[0] = 0x00000005;
1661 switch (i
->src(0).getFile()) {
1662 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1663 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1664 case FILE_MEMORY_SHARED
: opc
= 0xc1000000; break;
1665 case FILE_MEMORY_CONST
:
1666 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1667 emitMOV(i
); // not sure if this is any better
1670 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1671 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1674 assert(!"invalid memory file");
1680 defId(i
->def(0), 14);
1682 setAddressByFile(i
->src(0));
1683 srcId(i
->src(0).getIndirect(0), 20);
1684 if (uses64bitAddress(i
))
1689 emitLoadStoreType(i
->dType
);
1690 emitCachingMode(i
->cache
);
1694 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1696 switch (SDATA(ref
).sv
.sv
) {
1697 case SV_LANEID
: return 0x00;
1698 case SV_PHYSID
: return 0x03;
1699 case SV_VERTEX_COUNT
: return 0x10;
1700 case SV_INVOCATION_ID
: return 0x11;
1701 case SV_YDIR
: return 0x12;
1702 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1703 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1704 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1705 case SV_GRIDID
: return 0x2c;
1706 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1707 case SV_LBASE
: return 0x34;
1708 case SV_SBASE
: return 0x30;
1709 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1711 assert(!"no sreg for system value");
1717 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1719 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1720 if (i
->src(0).getFile() == FILE_GPR
) {
1721 code
[0] = 0xfc01c003;
1722 code
[1] = 0x1a8e0000;
1723 srcId(i
->src(0), 20);
1725 code
[0] = 0x0001c004;
1726 code
[1] = 0x0c0e0000;
1727 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1729 if (!i
->getSrc(0)->reg
.data
.u32
)
1732 srcId(i
->src(0), 20);
1735 defId(i
->def(0), 17);
1738 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1739 uint8_t sr
= getSRegEncoding(i
->src(0));
1741 if (i
->encSize
== 8) {
1742 code
[0] = 0x00000004 | (sr
<< 26);
1743 code
[1] = 0x2c000000;
1745 code
[0] = 0x40000008 | (sr
<< 20);
1747 defId(i
->def(0), 14);
1751 if (i
->encSize
== 8) {
1754 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1755 opc
= HEX64(18000000, 000001e2
);
1757 if (i
->src(0).getFile() == FILE_PREDICATE
)
1758 opc
= HEX64(080e0000
, 1c000004
);
1760 opc
= HEX64(28000000, 00000004);
1762 opc
|= i
->lanes
<< 5;
1768 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1769 imm
= SDATA(i
->src(0)).u32
;
1770 if (imm
& 0xfff00000) {
1771 assert(!(imm
& 0x000fffff));
1772 code
[0] = 0x00000318 | imm
;
1774 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1775 code
[0] = 0x00000118 | (imm
<< 20);
1779 emitShortSrc2(i
->src(0));
1781 defId(i
->def(0), 14);
1788 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
1790 const bool hasDst
= i
->defExists(0);
1791 const bool casOrExch
=
1792 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
1793 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
1795 if (i
->dType
== TYPE_U64
) {
1797 case NV50_IR_SUBOP_ATOM_ADD
:
1800 code
[1] = 0x507e0000;
1802 code
[1] = 0x10000000;
1804 case NV50_IR_SUBOP_ATOM_EXCH
:
1806 code
[1] = 0x507e0000;
1808 case NV50_IR_SUBOP_ATOM_CAS
:
1810 code
[1] = 0x50000000;
1813 assert(!"invalid u64 red op");
1817 if (i
->dType
== TYPE_U32
) {
1819 case NV50_IR_SUBOP_ATOM_EXCH
:
1821 code
[1] = 0x507e0000;
1823 case NV50_IR_SUBOP_ATOM_CAS
:
1825 code
[1] = 0x50000000;
1828 code
[0] = 0x5 | (i
->subOp
<< 5);
1830 code
[1] = 0x507e0000;
1832 code
[1] = 0x10000000;
1836 if (i
->dType
== TYPE_S32
) {
1837 assert(i
->subOp
<= 2);
1838 code
[0] = 0x205 | (i
->subOp
<< 5);
1840 code
[1] = 0x587e0000;
1842 code
[1] = 0x18000000;
1844 if (i
->dType
== TYPE_F32
) {
1845 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
1848 code
[1] = 0x687e0000;
1850 code
[1] = 0x28000000;
1855 srcId(i
->src(1), 14);
1858 defId(i
->def(0), 32 + 11);
1861 code
[1] |= 63 << 11;
1863 if (hasDst
|| casOrExch
) {
1864 const int32_t offset
= SDATA(i
->src(0)).offset
;
1865 assert(offset
< 0x80000 && offset
>= -0x80000);
1866 code
[0] |= offset
<< 26;
1867 code
[1] |= (offset
& 0x1ffc0) >> 6;
1868 code
[1] |= (offset
& 0xe0000) << 6;
1870 srcAddr32(i
->src(0), 26, 0);
1872 if (i
->getIndirect(0, 0)) {
1873 srcId(i
->getIndirect(0, 0), 20);
1874 if (i
->getIndirect(0, 0)->reg
.size
== 8)
1877 code
[0] |= 63 << 20;
1880 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
1881 srcId(i
->src(2), 32 + 17);
1885 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
1887 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
1888 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
1889 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
1892 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
1895 code
[1] = 0xe0000000;
1901 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
1903 code
[0] = 0x00000005 | (i
->subOp
<< 5);
1905 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
1906 code
[1] = 0x98000000;
1907 srcAddr32(i
->src(0), 28, 2);
1909 code
[1] = 0xd0000000;
1910 setAddress24(i
->src(0));
1912 if (uses64bitAddress(i
))
1914 srcId(i
->src(0).getIndirect(0), 20);
1922 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
1925 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
1926 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
1927 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
1928 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
1929 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
1930 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
1931 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
1932 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
1933 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
1934 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
1935 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
1936 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
1937 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
1938 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
1939 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
1940 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
1945 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
1950 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
1952 ImmediateValue
*imm
= NULL
;
1955 if (i
->srcExists(2)) {
1956 imm
= i
->getSrc(2)->asImm();
1958 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
1962 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
1963 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
1964 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
1971 if (i
->op
== OP_SUCLAMP
) {
1972 if (i
->dType
== TYPE_S32
)
1974 emitSUCLAMPMode(i
->subOp
);
1977 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
1980 if (i
->op
!= OP_SUEAU
) {
1981 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
1982 code
[0] |= 63 << 14;
1983 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
1985 if (i
->defExists(1)) { // r, p
1986 assert(i
->def(1).getFile() == FILE_PREDICATE
);
1987 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
1993 assert(i
->op
== OP_SUCLAMP
);
1995 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2000 CodeEmitterNVC0::emitSUGType(DataType ty
)
2003 case TYPE_S32
: code
[1] |= 1 << 13; break;
2004 case TYPE_U8
: code
[1] |= 2 << 13; break;
2005 case TYPE_S8
: code
[1] |= 3 << 13; break;
2007 assert(ty
== TYPE_U32
);
2013 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2015 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2017 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2018 assert(offset
== (offset
& 0xfffc));
2021 code
[0] |= offset
<< 24;
2022 code
[1] |= offset
>> 8;
2023 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2027 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2029 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2030 code
[1] |= 0x7 << 17;
2032 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2034 srcId(i
->src(s
), 32 + 17);
2039 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2042 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2044 emitLoadStoreType(i
->dType
);
2045 emitSUGType(i
->sType
);
2046 emitCachingMode(i
->cache
);
2049 defId(i
->def(0), 14); // destination
2050 srcId(i
->src(0), 20); // address
2052 if (i
->src(1).getFile() == FILE_GPR
)
2053 srcId(i
->src(1), 26);
2060 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2063 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2065 if (i
->op
== OP_SUSTP
)
2066 code
[1] |= i
->tex
.mask
<< 22;
2068 emitLoadStoreType(i
->dType
);
2069 emitSUGType(i
->sType
);
2070 emitCachingMode(i
->cache
);
2073 srcId(i
->src(0), 20); // address
2075 if (i
->src(1).getFile() == FILE_GPR
)
2076 srcId(i
->src(1), 26);
2079 srcId(i
->src(3), 14); // values
2084 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2086 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2088 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2089 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2090 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2091 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2094 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2095 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2096 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2097 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2098 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2099 code
[1] |= (i
->mask
& 0x3) << 2;
2102 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2103 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2104 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2105 code
[1] |= (i
->mask
& 0x3) << 2;
2106 code
[1] |= (i
->mask
& 0xc) << 21;
2115 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2119 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2120 case 0: opc
|= 0xe8ULL
<< 56; break;
2121 case 1: opc
|= 0xb4ULL
<< 56; break;
2122 case 2: opc
|= 0x94ULL
<< 56; break;
2127 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2128 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2129 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2131 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2132 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2139 if (i
->flagsDef
>= 0)
2144 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2146 unsigned int size
= insn
->encSize
;
2148 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2151 if (!insn
->encSize
) {
2152 ERROR("skipping unencodable instruction: "); insn
->print();
2155 if (codeSize
+ size
> codeSizeLimit
) {
2156 ERROR("code emitter output buffer too small\n");
2160 if (writeIssueDelays
) {
2161 if (!(codeSize
& 0x3f)) {
2162 code
[0] = 0x00000007; // cf issue delay "instruction"
2163 code
[1] = 0x20000000;
2167 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2168 uint32_t *data
= code
- (id
* 2 + 2);
2170 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2173 data
[0] |= insn
->sched
<< 28;
2174 data
[1] |= insn
->sched
>> 4;
2176 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2180 // assert that instructions with multiple defs don't corrupt registers
2181 for (int d
= 0; insn
->defExists(d
); ++d
)
2182 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2216 if (isFloatType(insn
->dType
))
2222 if (isFloatType(insn
->dType
))
2229 if (isFloatType(insn
->dType
))
2241 emitLogicOp(insn
, 0);
2244 emitLogicOp(insn
, 1);
2247 emitLogicOp(insn
, 2);
2257 emitSET(insn
->asCmp());
2263 emitSLCT(insn
->asCmp());
2305 emitTEX(insn
->asTex());
2308 emitTXQ(insn
->asTex());
2322 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2323 emitSULDGB(insn
->asTex());
2325 ERROR("SULDB not yet supported on < nve4\n");
2329 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2330 emitSUSTGx(insn
->asTex());
2332 ERROR("SUSTx not yet supported on < nve4\n");
2354 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2357 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2360 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2393 ERROR("operation should have been eliminated");
2399 ERROR("operation should have been lowered\n");
2402 ERROR("unknow op\n");
2408 assert(insn
->encSize
== 8);
2411 code
+= insn
->encSize
/ 4;
2412 codeSize
+= insn
->encSize
;
2417 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2419 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2421 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2424 if (i
->ftz
|| i
->saturate
|| i
->join
)
2426 if (i
->rnd
!= ROUND_N
)
2428 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2431 if (i
->op
== OP_PINTERP
) {
2432 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2435 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2439 for (int s
= 0; i
->srcExists(s
); ++s
) {
2440 if (i
->src(s
).isIndirect(0))
2443 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2444 if (SDATA(i
->src(s
)).offset
>= 0x100)
2446 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2447 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2450 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2451 if (i
->dType
== TYPE_F32
) {
2452 if (SDATA(i
->src(s
)).u32
>= 0x100)
2455 if (SDATA(i
->src(s
)).u32
> 0xff)
2460 if (i
->op
== OP_CVT
)
2462 if (i
->src(s
).mod
!= Modifier(0)) {
2463 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2464 if (i
->op
!= OP_RSQ
)
2466 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2467 if (i
->op
!= OP_ADD
|| s
!= 0)
2475 // Simplified, erring on safe side.
2476 class SchedDataCalculator
: public Pass
2479 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2485 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2486 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2487 int tex
; // TEX to non-TEX delay 17 (0x11)
2488 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2489 int imul
; // integer MUL to MUL delay 3
2498 void rebase(const int base
)
2500 const int delta
= this->base
- base
;
2505 for (int i
= 0; i
< 64; ++i
) {
2509 for (int i
= 0; i
< 8; ++i
) {
2516 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2526 memset(&rd
, 0, sizeof(rd
));
2527 memset(&wr
, 0, sizeof(wr
));
2528 memset(&res
, 0, sizeof(res
));
2530 int getLatest(const ScoreData
& d
) const
2533 for (int i
= 0; i
< 64; ++i
)
2536 for (int i
= 0; i
< 8; ++i
)
2543 inline int getLatestRd() const
2545 return getLatest(rd
);
2547 inline int getLatestWr() const
2549 return getLatest(wr
);
2551 inline int getLatest() const
2553 const int a
= getLatestRd();
2554 const int b
= getLatestWr();
2556 int max
= MAX2(a
, b
);
2557 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2558 max
= MAX2(res
.ld
[f
], max
);
2559 max
= MAX2(res
.st
[f
], max
);
2561 max
= MAX2(res
.sfu
, max
);
2562 max
= MAX2(res
.imul
, max
);
2563 max
= MAX2(res
.tex
, max
);
2566 void setMax(const RegScores
*that
)
2568 for (int i
= 0; i
< 64; ++i
) {
2569 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
2570 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
2572 for (int i
= 0; i
< 8; ++i
) {
2573 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
2574 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
2576 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
2577 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
2579 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2580 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
2581 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
2583 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
2584 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
2585 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
2587 void print(int cycle
)
2589 for (int i
= 0; i
< 64; ++i
) {
2590 if (rd
.r
[i
] > cycle
)
2591 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
2592 if (wr
.r
[i
] > cycle
)
2593 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
2595 for (int i
= 0; i
< 8; ++i
) {
2596 if (rd
.p
[i
] > cycle
)
2597 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
2598 if (wr
.p
[i
] > cycle
)
2599 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
2602 INFO("rd $c @ %i\n", rd
.c
);
2604 INFO("wr $c @ %i\n", wr
.c
);
2605 if (res
.sfu
> cycle
)
2606 INFO("sfu @ %i\n", res
.sfu
);
2607 if (res
.imul
> cycle
)
2608 INFO("imul @ %i\n", res
.imul
);
2609 if (res
.tex
> cycle
)
2610 INFO("tex @ %i\n", res
.tex
);
2614 RegScores
*score
; // for current BB
2615 std::vector
<RegScores
> scoreBoards
;
2622 bool visit(Function
*);
2623 bool visit(BasicBlock
*);
2625 void commitInsn(const Instruction
*, int cycle
);
2626 int calcDelay(const Instruction
*, int cycle
) const;
2627 void setDelay(Instruction
*, int delay
, Instruction
*next
);
2629 void recordRd(const Value
*, const int ready
);
2630 void recordWr(const Value
*, const int ready
);
2631 void checkRd(const Value
*, int cycle
, int& delay
) const;
2632 void checkWr(const Value
*, int cycle
, int& delay
) const;
2634 int getCycles(const Instruction
*, int origDelay
) const;
2638 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
2640 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
2641 delay
= MAX2(delay
, 14);
2643 if (insn
->op
== OP_TEXBAR
) {
2644 // TODO: except if results not used before EXIT
2647 if (insn
->op
== OP_JOIN
|| insn
->join
) {
2650 if (delay
>= 0 || prevData
== 0x04 ||
2651 !next
|| !targ
->canDualIssue(insn
, next
)) {
2652 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
2653 if (prevOp
== OP_EXPORT
)
2654 insn
->sched
|= 0x40;
2656 insn
->sched
|= 0x20;
2658 insn
->sched
= 0x04; // dual-issue
2661 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
2662 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
2665 prevData
= insn
->sched
;
2669 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
2671 if (insn
->sched
& 0x80) {
2672 int c
= (insn
->sched
& 0x0f) * 2 + 1;
2673 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
2677 if (insn
->sched
& 0x60)
2678 return (insn
->sched
& 0x1f) + 1;
2679 return (insn
->sched
== 0x04) ? 0 : 32;
2683 SchedDataCalculator::visit(Function
*func
)
2685 scoreBoards
.resize(func
->cfg
.getSize());
2686 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
2687 scoreBoards
[i
].wipe();
2692 SchedDataCalculator::visit(BasicBlock
*bb
)
2695 Instruction
*next
= NULL
;
2701 score
= &scoreBoards
.at(bb
->getId());
2703 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
2704 // back branches will wait until all target dependencies are satisfied
2705 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
2707 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
2708 if (in
->getExit()) {
2709 if (prevData
!= 0x04)
2710 prevData
= in
->getExit()->sched
;
2711 prevOp
= in
->getExit()->op
;
2713 score
->setMax(&scoreBoards
.at(in
->getId()));
2715 if (bb
->cfg
.incidentCount() > 1)
2718 #ifdef NVC0_DEBUG_SCHED_DATA
2719 INFO("=== BB:%i initial scores\n", bb
->getId());
2720 score
->print(cycle
);
2723 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
2726 commitInsn(insn
, cycle
);
2727 int delay
= calcDelay(next
, cycle
);
2728 setDelay(insn
, delay
, next
);
2729 cycle
+= getCycles(insn
, delay
);
2731 #ifdef NVC0_DEBUG_SCHED_DATA
2732 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
2739 commitInsn(insn
, cycle
);
2743 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
2744 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
2746 if (ei
.getType() != Graph::Edge::BACK
) {
2747 // only test the first instruction of the outgoing block
2748 next
= out
->getEntry();
2750 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
2752 // wait until all dependencies are satisfied
2753 const int regsFree
= score
->getLatest();
2754 next
= out
->getFirst();
2755 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
2756 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
2757 c
+= getCycles(next
, bbDelay
);
2762 if (bb
->cfg
.outgoingCount() != 1)
2764 setDelay(insn
, bbDelay
, next
);
2765 cycle
+= getCycles(insn
, bbDelay
);
2767 score
->rebase(cycle
); // common base for initializing out blocks' scores
2771 #define NVE4_MAX_ISSUE_DELAY 0x1f
2773 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
2775 int delay
= 0, ready
= cycle
;
2777 for (int s
= 0; insn
->srcExists(s
); ++s
)
2778 checkRd(insn
->getSrc(s
), cycle
, delay
);
2779 // WAR & WAW don't seem to matter
2780 // for (int s = 0; insn->srcExists(s); ++s)
2781 // recordRd(insn->getSrc(s), cycle);
2783 switch (Target::getOpClass(insn
->op
)) {
2785 ready
= score
->res
.sfu
;
2788 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2789 ready
= score
->res
.imul
;
2791 case OPCLASS_TEXTURE
:
2792 ready
= score
->res
.tex
;
2795 ready
= score
->res
.ld
[insn
->src(0).getFile()];
2798 ready
= score
->res
.st
[insn
->src(0).getFile()];
2803 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
2804 ready
= MAX2(ready
, score
->res
.tex
);
2806 delay
= MAX2(delay
, ready
- cycle
);
2808 // if can issue next cycle, delay is 0, not 1
2809 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
2813 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
2815 const int ready
= cycle
+ targ
->getLatency(insn
);
2817 for (int d
= 0; insn
->defExists(d
); ++d
)
2818 recordWr(insn
->getDef(d
), ready
);
2819 // WAR & WAW don't seem to matter
2820 // for (int s = 0; insn->srcExists(s); ++s)
2821 // recordRd(insn->getSrc(s), cycle);
2823 switch (Target::getOpClass(insn
->op
)) {
2825 score
->res
.sfu
= cycle
+ 4;
2828 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2829 score
->res
.imul
= cycle
+ 4;
2831 case OPCLASS_TEXTURE
:
2832 score
->res
.tex
= cycle
+ 18;
2835 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
2837 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
2838 score
->res
.st
[insn
->src(0).getFile()] = ready
;
2841 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
2842 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
2845 if (insn
->op
== OP_TEXBAR
)
2846 score
->res
.tex
= cycle
;
2852 #ifdef NVC0_DEBUG_SCHED_DATA
2853 score
->print(cycle
);
2858 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
2863 switch (v
->reg
.file
) {
2866 b
= a
+ v
->reg
.size
/ 4;
2867 for (int r
= a
; r
< b
; ++r
)
2868 ready
= MAX2(ready
, score
->rd
.r
[r
]);
2870 case FILE_PREDICATE
:
2871 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
2874 ready
= MAX2(ready
, score
->rd
.c
);
2876 case FILE_SHADER_INPUT
:
2877 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
2878 case FILE_MEMORY_LOCAL
:
2879 case FILE_MEMORY_CONST
:
2880 case FILE_MEMORY_SHARED
:
2881 case FILE_MEMORY_GLOBAL
:
2882 case FILE_SYSTEM_VALUE
:
2883 // TODO: any restrictions here ?
2885 case FILE_IMMEDIATE
:
2892 delay
= MAX2(delay
, ready
- cycle
);
2896 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
2901 switch (v
->reg
.file
) {
2904 b
= a
+ v
->reg
.size
/ 4;
2905 for (int r
= a
; r
< b
; ++r
)
2906 ready
= MAX2(ready
, score
->wr
.r
[r
]);
2908 case FILE_PREDICATE
:
2909 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
2912 assert(v
->reg
.file
== FILE_FLAGS
);
2913 ready
= MAX2(ready
, score
->wr
.c
);
2917 delay
= MAX2(delay
, ready
- cycle
);
2921 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
2923 int a
= v
->reg
.data
.id
;
2925 if (v
->reg
.file
== FILE_GPR
) {
2926 int b
= a
+ v
->reg
.size
/ 4;
2927 for (int r
= a
; r
< b
; ++r
)
2928 score
->rd
.r
[r
] = ready
;
2930 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
2931 if (v
->reg
.file
== FILE_PREDICATE
) {
2932 score
->rd
.p
[a
] = ready
+ 4;
2934 assert(v
->reg
.file
== FILE_FLAGS
);
2935 score
->rd
.c
= ready
+ 4;
2940 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
2942 int a
= v
->reg
.data
.id
;
2944 if (v
->reg
.file
== FILE_GPR
) {
2945 int b
= a
+ v
->reg
.size
/ 4;
2946 for (int r
= a
; r
< b
; ++r
)
2947 score
->wr
.r
[r
] = ready
;
2949 if (v
->reg
.file
== FILE_PREDICATE
) {
2950 score
->wr
.p
[a
] = ready
;
2952 if (v
->reg
.file
== FILE_FLAGS
) {
2953 score
->wr
.c
= ready
;
2958 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
2960 SchedDataCalculator
sched(targ
);
2961 return sched
.run(func
, true, true);
2965 CodeEmitterNVC0::prepareEmission(Function
*func
)
2967 CodeEmitter::prepareEmission(func
);
2969 if (targ
->hasSWSched
)
2970 calculateSchedDataNVC0(targ
, func
);
2973 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
2974 : CodeEmitter(target
),
2976 writeIssueDelays(target
->hasSWSched
)
2979 codeSize
= codeSizeLimit
= 0;
2984 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
2986 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
2987 emit
->setProgramType(type
);
2992 TargetNVC0::getCodeEmitter(Program::Type type
)
2994 if (chipset
>= NVISA_GK110_CHIPSET
)
2995 return createCodeEmitterGK110(type
);
2996 return createCodeEmitterNVC0(type
);
2999 } // namespace nv50_ir