2 * Copyright 2011 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include "codegen/nv50_ir_target_nvc0.h"
27 // Argh, all these assertions ...
29 class CodeEmitterNVC0
: public CodeEmitter
32 CodeEmitterNVC0(const TargetNVC0
*);
34 virtual bool emitInstruction(Instruction
*);
35 virtual uint32_t getMinEncodingSize(const Instruction
*) const;
36 virtual void prepareEmission(Function
*);
38 inline void setProgramType(Program::Type pType
) { progType
= pType
; }
41 const TargetNVC0
*targNVC0
;
43 Program::Type progType
;
45 const bool writeIssueDelays
;
48 void emitForm_A(const Instruction
*, uint64_t);
49 void emitForm_B(const Instruction
*, uint64_t);
50 void emitForm_S(const Instruction
*, uint32_t, bool pred
);
52 void emitPredicate(const Instruction
*);
54 void setAddress16(const ValueRef
&);
55 void setAddress24(const ValueRef
&);
56 void setAddressByFile(const ValueRef
&);
57 void setImmediate(const Instruction
*, const int s
); // needs op already set
58 void setImmediateS8(const ValueRef
&);
59 void setSUConst16(const Instruction
*, const int s
);
60 void setSUPred(const Instruction
*, const int s
);
62 void emitCondCode(CondCode cc
, int pos
);
63 void emitInterpMode(const Instruction
*);
64 void emitLoadStoreType(DataType ty
);
65 void emitSUGType(DataType
);
66 void emitCachingMode(CacheMode c
);
68 void emitShortSrc2(const ValueRef
&);
70 inline uint8_t getSRegEncoding(const ValueRef
&);
72 void roundMode_A(const Instruction
*);
73 void roundMode_C(const Instruction
*);
74 void roundMode_CS(const Instruction
*);
76 void emitNegAbs12(const Instruction
*);
78 void emitNOP(const Instruction
*);
80 void emitLOAD(const Instruction
*);
81 void emitSTORE(const Instruction
*);
82 void emitMOV(const Instruction
*);
83 void emitATOM(const Instruction
*);
84 void emitMEMBAR(const Instruction
*);
85 void emitCCTL(const Instruction
*);
87 void emitINTERP(const Instruction
*);
88 void emitAFETCH(const Instruction
*);
89 void emitPFETCH(const Instruction
*);
90 void emitVFETCH(const Instruction
*);
91 void emitEXPORT(const Instruction
*);
92 void emitOUT(const Instruction
*);
94 void emitUADD(const Instruction
*);
95 void emitFADD(const Instruction
*);
96 void emitDADD(const Instruction
*);
97 void emitUMUL(const Instruction
*);
98 void emitFMUL(const Instruction
*);
99 void emitDMUL(const Instruction
*);
100 void emitIMAD(const Instruction
*);
101 void emitISAD(const Instruction
*);
102 void emitFMAD(const Instruction
*);
103 void emitDMAD(const Instruction
*);
104 void emitMADSP(const Instruction
*);
106 void emitNOT(Instruction
*);
107 void emitLogicOp(const Instruction
*, uint8_t subOp
);
108 void emitPOPC(const Instruction
*);
109 void emitINSBF(const Instruction
*);
110 void emitEXTBF(const Instruction
*);
111 void emitBFIND(const Instruction
*);
112 void emitPERMT(const Instruction
*);
113 void emitShift(const Instruction
*);
115 void emitSFnOp(const Instruction
*, uint8_t subOp
);
117 void emitCVT(Instruction
*);
118 void emitMINMAX(const Instruction
*);
119 void emitPreOp(const Instruction
*);
121 void emitSET(const CmpInstruction
*);
122 void emitSLCT(const CmpInstruction
*);
123 void emitSELP(const Instruction
*);
125 void emitTEXBAR(const Instruction
*);
126 void emitTEX(const TexInstruction
*);
127 void emitTEXCSAA(const TexInstruction
*);
128 void emitTXQ(const TexInstruction
*);
130 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
132 void emitFlow(const Instruction
*);
133 void emitBAR(const Instruction
*);
135 void emitSUCLAMPMode(uint16_t);
136 void emitSUCalc(Instruction
*);
137 void emitSULDGB(const TexInstruction
*);
138 void emitSUSTGx(const TexInstruction
*);
140 void emitVSHL(const Instruction
*);
141 void emitVectorSubOp(const Instruction
*);
143 void emitPIXLD(const Instruction
*);
145 inline void defId(const ValueDef
&, const int pos
);
146 inline void defId(const Instruction
*, int d
, const int pos
);
147 inline void srcId(const ValueRef
&, const int pos
);
148 inline void srcId(const ValueRef
*, const int pos
);
149 inline void srcId(const Instruction
*, int s
, const int pos
);
150 inline void srcAddr32(const ValueRef
&, int pos
, int shr
);
152 inline bool isLIMM(const ValueRef
&, DataType ty
);
155 // for better visibility
156 #define HEX64(h, l) 0x##h##l##ULL
158 #define SDATA(a) ((a).rep()->reg.data)
159 #define DDATA(a) ((a).rep()->reg.data)
161 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
163 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
166 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
168 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
171 void CodeEmitterNVC0::srcId(const Instruction
*insn
, int s
, int pos
)
173 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: 63;
174 code
[pos
/ 32] |= r
<< (pos
% 32);
178 CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, int pos
, int shr
)
180 const uint32_t offset
= SDATA(src
).offset
>> shr
;
182 code
[pos
/ 32] |= offset
<< (pos
% 32);
183 if (pos
&& (pos
< 32))
184 code
[1] |= offset
>> (32 - pos
);
187 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
189 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
192 void CodeEmitterNVC0::defId(const Instruction
*insn
, int d
, int pos
)
194 int r
= insn
->defExists(d
) ? DDATA(insn
->def(d
)).id
: 63;
195 code
[pos
/ 32] |= r
<< (pos
% 32);
198 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
200 const ImmediateValue
*imm
= ref
.get()->asImm();
202 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
206 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
209 case ROUND_M
: code
[1] |= 1 << 23; break;
210 case ROUND_P
: code
[1] |= 2 << 23; break;
211 case ROUND_Z
: code
[1] |= 3 << 23; break;
213 assert(insn
->rnd
== ROUND_N
);
219 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
221 if (i
->src(1).mod
.abs()) code
[0] |= 1 << 6;
222 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
223 if (i
->src(1).mod
.neg()) code
[0] |= 1 << 8;
224 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
227 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
232 case CC_LT
: val
= 0x1; break;
233 case CC_LTU
: val
= 0x9; break;
234 case CC_EQ
: val
= 0x2; break;
235 case CC_EQU
: val
= 0xa; break;
236 case CC_LE
: val
= 0x3; break;
237 case CC_LEU
: val
= 0xb; break;
238 case CC_GT
: val
= 0x4; break;
239 case CC_GTU
: val
= 0xc; break;
240 case CC_NE
: val
= 0x5; break;
241 case CC_NEU
: val
= 0xd; break;
242 case CC_GE
: val
= 0x6; break;
243 case CC_GEU
: val
= 0xe; break;
244 case CC_TR
: val
= 0xf; break;
245 case CC_FL
: val
= 0x0; break;
247 case CC_A
: val
= 0x14; break;
248 case CC_NA
: val
= 0x13; break;
249 case CC_S
: val
= 0x15; break;
250 case CC_NS
: val
= 0x12; break;
251 case CC_C
: val
= 0x16; break;
252 case CC_NC
: val
= 0x11; break;
253 case CC_O
: val
= 0x17; break;
254 case CC_NO
: val
= 0x10; break;
258 assert(!"invalid condition code");
261 code
[pos
/ 32] |= val
<< (pos
% 32);
265 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
267 if (i
->predSrc
>= 0) {
268 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
269 srcId(i
->src(i
->predSrc
), 10);
270 if (i
->cc
== CC_NOT_P
)
271 code
[0] |= 0x2000; // negate
278 CodeEmitterNVC0::setAddressByFile(const ValueRef
& src
)
280 switch (src
.getFile()) {
281 case FILE_MEMORY_GLOBAL
:
282 srcAddr32(src
, 26, 0);
284 case FILE_MEMORY_LOCAL
:
285 case FILE_MEMORY_SHARED
:
289 assert(src
.getFile() == FILE_MEMORY_CONST
);
296 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
298 Symbol
*sym
= src
.get()->asSym();
302 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
303 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
307 CodeEmitterNVC0::setAddress24(const ValueRef
& src
)
309 Symbol
*sym
= src
.get()->asSym();
313 code
[0] |= (sym
->reg
.data
.offset
& 0x00003f) << 26;
314 code
[1] |= (sym
->reg
.data
.offset
& 0xffffc0) >> 6;
318 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
320 const ImmediateValue
*imm
= i
->src(s
).get()->asImm();
324 u32
= imm
->reg
.data
.u32
;
326 if ((code
[0] & 0xf) == 0x1) {
328 uint64_t u64
= imm
->reg
.data
.u64
;
329 assert(!(u64
& 0x00000fffffffffffULL
));
330 assert(!(code
[1] & 0xc000));
331 code
[0] |= ((u64
>> 44) & 0x3f) << 26;
332 code
[1] |= 0xc000 | (u64
>> 50);
334 if ((code
[0] & 0xf) == 0x2) {
336 code
[0] |= (u32
& 0x3f) << 26;
339 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
341 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
342 assert(!(code
[1] & 0xc000));
344 code
[0] |= (u32
& 0x3f) << 26;
345 code
[1] |= 0xc000 | (u32
>> 6);
348 assert(!(u32
& 0x00000fff));
349 assert(!(code
[1] & 0xc000));
350 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
351 code
[1] |= 0xc000 | (u32
>> 18);
355 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
357 const ImmediateValue
*imm
= ref
.get()->asImm();
359 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
361 assert(s8
== imm
->reg
.data
.s32
);
363 code
[0] |= (s8
& 0x3f) << 26;
364 code
[0] |= (s8
>> 6) << 8;
368 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
375 defId(i
->def(0), 14);
378 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
381 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
382 switch (i
->getSrc(s
)->reg
.file
) {
383 case FILE_MEMORY_CONST
:
384 assert(!(code
[1] & 0xc000));
385 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
386 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
387 setAddress16(i
->src(s
));
391 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
392 assert(!(code
[1] & 0xc000));
396 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
398 srcId(i
->src(s
), s
? ((s
== 2) ? 49 : s1
) : 20);
401 if (i
->op
== OP_SELP
) {
402 // OP_SELP is used to implement shared+atomics on Fermi.
403 assert(s
== 2 && i
->src(s
).getFile() == FILE_PREDICATE
);
404 srcId(i
->src(s
), 49);
406 // ignore here, can be predicate or flags, but must not be address
413 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
420 defId(i
->def(0), 14);
422 switch (i
->src(0).getFile()) {
423 case FILE_MEMORY_CONST
:
424 assert(!(code
[1] & 0xc000));
425 code
[1] |= 0x4000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
426 setAddress16(i
->src(0));
429 assert(!(code
[1] & 0xc000));
433 srcId(i
->src(0), 26);
436 // ignore here, can be predicate or flags, but must not be address
442 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
447 if (opc
== 0x0d || opc
== 0x0e)
450 defId(i
->def(0), 14);
451 srcId(i
->src(0), 20);
453 assert(pred
|| (i
->predSrc
< 0));
457 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
458 if (i
->src(s
).get()->reg
.file
== FILE_MEMORY_CONST
) {
459 assert(!(code
[0] & (0x300 >> ss2a
)));
460 switch (i
->src(s
).get()->reg
.fileIndex
) {
461 case 0: code
[0] |= 0x100 >> ss2a
; break;
462 case 1: code
[0] |= 0x200 >> ss2a
; break;
463 case 16: code
[0] |= 0x300 >> ss2a
; break;
465 ERROR("invalid c[] space for short form\n");
469 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
471 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
473 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
475 setImmediateS8(i
->src(s
));
477 if (i
->src(s
).getFile() == FILE_GPR
) {
478 srcId(i
->src(s
), (s
== 1) ? 26 : 8);
484 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
486 if (src
.getFile() == FILE_MEMORY_CONST
) {
487 switch (src
.get()->reg
.fileIndex
) {
488 case 0: code
[0] |= 0x100; break;
489 case 1: code
[0] |= 0x200; break;
490 case 16: code
[0] |= 0x300; break;
492 assert(!"unsupported file index for short op");
495 srcAddr32(src
, 20, 2);
498 assert(src
.getFile() == FILE_GPR
);
503 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
505 code
[0] = 0x000001e4;
506 code
[1] = 0x40000000;
511 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
513 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
515 if (i
->encSize
== 8) {
516 if (isLIMM(i
->src(1), TYPE_F32
)) {
517 emitForm_A(i
, HEX64(20000000, 00000002));
519 emitForm_A(i
, HEX64(30000000, 00000000));
521 if (i
->src(2).mod
.neg())
534 assert(!i
->saturate
&& !i
->src(2).mod
.neg());
535 emitForm_S(i
, (i
->src(2).getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
543 CodeEmitterNVC0::emitDMAD(const Instruction
*i
)
545 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
547 emitForm_A(i
, HEX64(20000000, 00000001));
549 if (i
->src(2).mod
.neg())
557 assert(!i
->saturate
);
562 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
564 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
566 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
568 if (i
->encSize
== 8) {
569 if (isLIMM(i
->src(1), TYPE_F32
)) {
570 assert(i
->postFactor
== 0); // constant folded, hopefully
571 emitForm_A(i
, HEX64(30000000, 00000002));
573 emitForm_A(i
, HEX64(58000000, 00000000));
575 code
[1] |= ((i
->postFactor
> 0) ?
576 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
579 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
590 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
591 emitForm_S(i
, 0xa8, true);
596 CodeEmitterNVC0::emitDMUL(const Instruction
*i
)
598 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
600 emitForm_A(i
, HEX64(50000000, 00000001));
606 assert(!i
->saturate
);
609 assert(!i
->postFactor
);
613 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
615 if (i
->encSize
== 8) {
616 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
617 emitForm_A(i
, HEX64(10000000, 00000002));
619 emitForm_A(i
, HEX64(50000000, 00000003));
621 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
623 if (i
->sType
== TYPE_S32
)
625 if (i
->dType
== TYPE_S32
)
628 emitForm_S(i
, i
->src(1).getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
630 if (i
->sType
== TYPE_S32
)
636 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
638 if (i
->encSize
== 8) {
639 if (isLIMM(i
->src(1), TYPE_F32
)) {
640 assert(!i
->saturate
);
641 emitForm_A(i
, HEX64(28000000, 00000002));
643 code
[0] |= i
->src(0).mod
.abs() << 7;
644 code
[0] |= i
->src(0).mod
.neg() << 9;
646 if (i
->src(1).mod
.abs())
647 code
[1] &= 0xfdffffff;
648 if ((i
->op
== OP_SUB
) != static_cast<bool>(i
->src(1).mod
.neg()))
649 code
[1] ^= 0x02000000;
651 emitForm_A(i
, HEX64(50000000, 00000000));
658 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
663 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
664 !i
->src(0).mod
.abs() &&
665 !i
->src(1).mod
.neg() && !i
->src(1).mod
.abs());
667 emitForm_S(i
, 0x49, true);
669 if (i
->src(0).mod
.neg())
675 CodeEmitterNVC0::emitDADD(const Instruction
*i
)
677 assert(i
->encSize
== 8);
678 emitForm_A(i
, HEX64(48000000, 00000001));
680 assert(!i
->saturate
);
688 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
692 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
693 assert(!i
->src(0).mod
.neg() || !i
->src(1).mod
.neg());
695 if (i
->src(0).mod
.neg())
697 if (i
->src(1).mod
.neg())
699 if (i
->op
== OP_SUB
) {
701 assert(addOp
!= 0x300); // would be add-plus-one
704 if (i
->encSize
== 8) {
705 if (isLIMM(i
->src(1), TYPE_U32
)) {
706 emitForm_A(i
, HEX64(08000000, 00000002));
708 code
[1] |= 1 << 26; // write carry
710 emitForm_A(i
, HEX64(48000000, 00000003));
712 code
[1] |= 1 << 16; // write carry
718 if (i
->flagsSrc
>= 0) // add carry
721 assert(!(addOp
& 0x100));
722 emitForm_S(i
, (addOp
>> 3) |
723 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
729 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
731 assert(i
->encSize
== 8);
732 emitForm_A(i
, HEX64(20000000, 00000003));
734 if (isSignedType(i
->dType
))
736 if (isSignedType(i
->sType
))
739 code
[1] |= i
->saturate
<< 24;
741 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
742 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
744 if (i
->src(2).mod
.neg()) code
[0] |= 0x10;
745 if (i
->src(1).mod
.neg() ^
746 i
->src(0).mod
.neg()) code
[0] |= 0x20;
748 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
753 CodeEmitterNVC0::emitMADSP(const Instruction
*i
)
755 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
);
757 emitForm_A(i
, HEX64(00000000, 00000003));
759 if (i
->subOp
== NV50_IR_SUBOP_MADSP_SD
) {
760 code
[1] |= 0x01800000;
762 code
[0] |= (i
->subOp
& 0x00f) << 7;
763 code
[0] |= (i
->subOp
& 0x0f0) << 1;
764 code
[0] |= (i
->subOp
& 0x100) >> 3;
765 code
[0] |= (i
->subOp
& 0x200) >> 2;
766 code
[1] |= (i
->subOp
& 0xc00) << 13;
769 if (i
->flagsDef
>= 0)
774 CodeEmitterNVC0::emitISAD(const Instruction
*i
)
776 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
777 assert(i
->encSize
== 8);
779 emitForm_A(i
, HEX64(38000000, 00000003));
781 if (i
->dType
== TYPE_S32
)
786 CodeEmitterNVC0::emitNOT(Instruction
*i
)
788 assert(i
->encSize
== 8);
789 i
->setSrc(1, i
->src(0));
790 emitForm_A(i
, HEX64(68000000, 000001c3
));
794 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
796 if (i
->def(0).getFile() == FILE_PREDICATE
) {
797 code
[0] = 0x00000004 | (subOp
<< 30);
798 code
[1] = 0x0c000000;
802 defId(i
->def(0), 17);
803 srcId(i
->src(0), 20);
804 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 23;
805 srcId(i
->src(1), 26);
806 if (i
->src(1).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 29;
808 if (i
->defExists(1)) {
809 defId(i
->def(1), 14);
814 if (i
->predSrc
!= 2 && i
->srcExists(2)) {
815 code
[1] |= subOp
<< 21;
816 srcId(i
->src(2), 17);
817 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 20;
819 code
[1] |= 0x000e0000;
822 if (i
->encSize
== 8) {
823 if (isLIMM(i
->src(1), TYPE_U32
)) {
824 emitForm_A(i
, HEX64(38000000, 00000002));
826 if (i
->flagsDef
>= 0)
829 emitForm_A(i
, HEX64(68000000, 00000003));
831 if (i
->flagsDef
>= 0)
834 code
[0] |= subOp
<< 6;
836 if (i
->flagsSrc
>= 0) // carry
839 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
840 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
842 emitForm_S(i
, (subOp
<< 5) |
843 ((i
->src(1).getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
848 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
850 emitForm_A(i
, HEX64(54000000, 00000004));
852 if (i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
853 if (i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
857 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
859 emitForm_A(i
, HEX64(28000000, 00000003));
863 CodeEmitterNVC0::emitEXTBF(const Instruction
*i
)
865 emitForm_A(i
, HEX64(70000000, 00000003));
867 if (i
->dType
== TYPE_S32
)
869 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
874 CodeEmitterNVC0::emitBFIND(const Instruction
*i
)
876 emitForm_B(i
, HEX64(78000000, 00000003));
878 if (i
->dType
== TYPE_S32
)
880 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
882 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
887 CodeEmitterNVC0::emitPERMT(const Instruction
*i
)
889 emitForm_A(i
, HEX64(24000000, 00000004));
891 code
[0] |= i
->subOp
<< 5;
895 CodeEmitterNVC0::emitShift(const Instruction
*i
)
897 if (i
->op
== OP_SHR
) {
898 emitForm_A(i
, HEX64(58000000, 00000003)
899 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
901 emitForm_A(i
, HEX64(60000000, 00000003));
904 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
909 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
911 if (i
->encSize
== 8) {
912 emitForm_B(i
, HEX64(60000000, 00000000));
914 if (i
->op
== OP_PREEX2
)
917 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 6;
918 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 8;
920 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
925 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
927 if (i
->encSize
== 8) {
928 code
[0] = 0x00000000 | (subOp
<< 26);
929 code
[1] = 0xc8000000;
933 defId(i
->def(0), 14);
934 srcId(i
->src(0), 20);
936 assert(i
->src(0).getFile() == FILE_GPR
);
938 if (i
->saturate
) code
[0] |= 1 << 5;
940 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 7;
941 if (i
->src(0).mod
.neg()) code
[0] |= 1 << 9;
943 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
945 assert(!i
->src(0).mod
.neg());
946 if (i
->src(0).mod
.abs()) code
[0] |= 1 << 30;
951 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
955 assert(i
->encSize
== 8);
957 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
962 if (!isFloatType(i
->dType
))
963 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
964 if (i
->dType
== TYPE_F64
)
972 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
975 case ROUND_M
: code
[1] |= 1 << 17; break;
976 case ROUND_P
: code
[1] |= 2 << 17; break;
977 case ROUND_Z
: code
[1] |= 3 << 17; break;
978 case ROUND_NI
: code
[0] |= 1 << 7; break;
979 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
980 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
981 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
984 assert(!"invalid round mode");
990 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
994 case ROUND_MI
: code
[0] |= 1 << 16; break;
996 case ROUND_PI
: code
[0] |= 2 << 16; break;
998 case ROUND_ZI
: code
[0] |= 3 << 16; break;
1005 CodeEmitterNVC0::emitCVT(Instruction
*i
)
1007 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
1011 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
1012 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
1013 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
1018 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
1019 const bool abs
= (i
->op
== OP_ABS
) || i
->src(0).mod
.abs();
1020 const bool neg
= (i
->op
== OP_NEG
) || i
->src(0).mod
.neg();
1022 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
1027 if (i
->encSize
== 8) {
1028 emitForm_B(i
, HEX64(10000000, 00000004));
1032 // cvt u16 f32 sets high bits to 0, so we don't have to use Value::Size()
1033 code
[0] |= util_logbase2(typeSizeof(dType
)) << 20;
1034 code
[0] |= util_logbase2(typeSizeof(i
->sType
)) << 23;
1036 // for 8/16 source types, the byte/word is in subOp. word 1 is
1037 // represented as 2.
1038 if (!isFloatType(i
->sType
))
1039 code
[1] |= i
->subOp
<< 0x17;
1041 code
[1] |= i
->subOp
<< 0x18;
1047 if (neg
&& i
->op
!= OP_ABS
)
1053 if (isSignedIntType(dType
))
1055 if (isSignedIntType(i
->sType
))
1058 if (isFloatType(dType
)) {
1059 if (!isFloatType(i
->sType
))
1060 code
[1] |= 0x08000000;
1062 if (isFloatType(i
->sType
))
1063 code
[1] |= 0x04000000;
1065 code
[1] |= 0x0c000000;
1068 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
1071 if (isFloatType(dType
)) {
1072 if (isFloatType(i
->sType
))
1075 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1077 assert(isFloatType(i
->sType
));
1079 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
1082 if (neg
) code
[0] |= 1 << 16;
1083 if (sat
) code
[0] |= 1 << 18;
1084 if (abs
) code
[0] |= 1 << 19;
1091 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
1096 if (i
->sType
== TYPE_F64
)
1099 if (!isFloatType(i
->sType
))
1102 if (isSignedIntType(i
->sType
))
1104 if (isFloatType(i
->dType
)) {
1105 if (isFloatType(i
->sType
))
1112 case OP_SET_AND
: hi
= 0x10000000; break;
1113 case OP_SET_OR
: hi
= 0x10200000; break;
1114 case OP_SET_XOR
: hi
= 0x10400000; break;
1119 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
1121 if (i
->op
!= OP_SET
)
1122 srcId(i
->src(2), 32 + 17);
1124 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1125 if (i
->sType
== TYPE_F32
)
1126 code
[1] += 0x10000000;
1128 code
[1] += 0x08000000;
1130 code
[0] &= ~0xfc000;
1131 defId(i
->def(0), 17);
1132 if (i
->defExists(1))
1133 defId(i
->def(1), 14);
1141 emitCondCode(i
->setCond
, 32 + 23);
1146 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
1152 op
= HEX64(30000000, 00000023);
1155 op
= HEX64(30000000, 00000003);
1158 op
= HEX64(38000000, 00000000);
1161 assert(!"invalid type for SLCT");
1167 CondCode cc
= i
->setCond
;
1169 if (i
->src(2).mod
.neg())
1170 cc
= reverseCondCode(cc
);
1172 emitCondCode(cc
, 32 + 23);
1178 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
1180 emitForm_A(i
, HEX64(20000000, 00000004));
1182 if (i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
))
1186 void CodeEmitterNVC0::emitTEXBAR(const Instruction
*i
)
1188 code
[0] = 0x00000006 | (i
->subOp
<< 26);
1189 code
[1] = 0xf0000000;
1191 emitCondCode(i
->flagsSrc
>= 0 ? i
->cc
: CC_ALWAYS
, 5);
1194 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
1196 code
[0] = 0x00000086;
1197 code
[1] = 0xd0000000;
1199 code
[1] |= i
->tex
.r
;
1200 code
[1] |= i
->tex
.s
<< 8;
1202 if (i
->tex
.liveOnly
)
1205 defId(i
->def(0), 14);
1206 srcId(i
->src(0), 20);
1210 isNextIndependentTex(const TexInstruction
*i
)
1212 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1214 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1216 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1220 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
1222 code
[0] = 0x00000006;
1224 if (isNextIndependentTex(i
))
1225 code
[0] |= 0x080; // t mode
1227 code
[0] |= 0x100; // p mode
1229 if (i
->tex
.liveOnly
)
1233 case OP_TEX
: code
[1] = 0x80000000; break;
1234 case OP_TXB
: code
[1] = 0x84000000; break;
1235 case OP_TXL
: code
[1] = 0x86000000; break;
1236 case OP_TXF
: code
[1] = 0x90000000; break;
1237 case OP_TXG
: code
[1] = 0xa0000000; break;
1238 case OP_TXLQ
: code
[1] = 0xb0000000; break;
1239 case OP_TXD
: code
[1] = 0xe0000000; break;
1241 assert(!"invalid texture op");
1244 if (i
->op
== OP_TXF
) {
1245 if (!i
->tex
.levelZero
)
1246 code
[1] |= 0x02000000;
1248 if (i
->tex
.levelZero
) {
1249 code
[1] |= 0x02000000;
1252 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1255 defId(i
->def(0), 14);
1256 srcId(i
->src(0), 20);
1260 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
1262 code
[1] |= i
->tex
.mask
<< 14;
1264 code
[1] |= i
->tex
.r
;
1265 code
[1] |= i
->tex
.s
<< 8;
1266 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
1267 code
[1] |= 1 << 18; // in 1st source (with array index)
1270 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
1271 if (i
->tex
.target
.isCube())
1273 if (i
->tex
.target
.isArray())
1275 if (i
->tex
.target
.isShadow())
1278 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1280 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1282 if (i
->op
== OP_TXL
)
1283 code
[1] &= ~(1 << 26);
1285 if (i
->op
== OP_TXF
)
1286 code
[1] &= ~(1 << 25);
1288 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1289 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1292 if (i
->tex
.useOffsets
== 1)
1294 if (i
->tex
.useOffsets
== 4)
1301 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1303 code
[0] = 0x00000086;
1304 code
[1] = 0xc0000000;
1306 switch (i
->tex
.query
) {
1307 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1308 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1309 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1310 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1311 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1312 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1314 assert(!"invalid texture query");
1318 code
[1] |= i
->tex
.mask
<< 14;
1320 code
[1] |= i
->tex
.r
;
1321 code
[1] |= i
->tex
.s
<< 8;
1322 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1325 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1327 defId(i
->def(0), 14);
1328 srcId(i
->src(0), 20);
1335 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1337 code
[0] = 0x00000000 | (laneMask
<< 6);
1338 code
[1] = 0x48000000 | qOp
;
1340 defId(i
->def(0), 14);
1341 srcId(i
->src(0), 20);
1342 srcId((i
->srcExists(1) && i
->predSrc
!= 1) ? i
->src(1) : i
->src(0), 26);
1344 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1345 code
[0] |= 1 << 9; // dall
1351 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1353 const FlowInstruction
*f
= i
->asFlow();
1355 unsigned mask
; // bit 0: predicate, bit 1: target
1357 code
[0] = 0x00000007;
1361 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1362 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1367 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1369 code
[0] |= 0x4000; // indirect calls always use c[] source
1373 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1374 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1375 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1376 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1377 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1379 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1380 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1381 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1382 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1384 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1385 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1386 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1388 assert(!"invalid flow operation");
1394 if (i
->flagsSrc
< 0)
1407 if (code
[0] & 0x4000) {
1408 assert(i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
);
1409 setAddress16(i
->src(0));
1410 code
[1] |= i
->getSrc(0)->reg
.fileIndex
<< 10;
1411 if (f
->op
== OP_BRA
)
1412 srcId(f
->src(0).getIndirect(0), 20);
1418 if (f
->op
== OP_CALL
) {
1423 assert(f
->absolute
);
1424 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1425 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1426 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1428 assert(!f
->absolute
);
1429 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1430 code
[0] |= (pcRel
& 0x3f) << 26;
1431 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1435 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1436 if (writeIssueDelays
&& !(f
->target
.bb
->binPos
& 0x3f))
1438 // currently we don't want absolute branches
1439 assert(!f
->absolute
);
1440 code
[0] |= (pcRel
& 0x3f) << 26;
1441 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1446 CodeEmitterNVC0::emitBAR(const Instruction
*i
)
1448 Value
*rDef
= NULL
, *pDef
= NULL
;
1451 case NV50_IR_SUBOP_BAR_ARRIVE
: code
[0] = 0x84; break;
1452 case NV50_IR_SUBOP_BAR_RED_AND
: code
[0] = 0x24; break;
1453 case NV50_IR_SUBOP_BAR_RED_OR
: code
[0] = 0x44; break;
1454 case NV50_IR_SUBOP_BAR_RED_POPC
: code
[0] = 0x04; break;
1457 assert(i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
);
1460 code
[1] = 0x50000000;
1462 code
[0] |= 63 << 14;
1468 if (i
->src(0).getFile() == FILE_GPR
) {
1469 srcId(i
->src(0), 20);
1471 ImmediateValue
*imm
= i
->getSrc(0)->asImm();
1473 code
[0] |= imm
->reg
.data
.u32
<< 20;
1478 if (i
->src(1).getFile() == FILE_GPR
) {
1479 srcId(i
->src(1), 26);
1481 ImmediateValue
*imm
= i
->getSrc(1)->asImm();
1483 code
[0] |= imm
->reg
.data
.u32
<< 26;
1484 code
[1] |= imm
->reg
.data
.u32
>> 6;
1488 if (i
->srcExists(2) && (i
->predSrc
!= 2)) {
1489 srcId(i
->src(2), 32 + 17);
1490 if (i
->src(2).mod
== Modifier(NV50_IR_MOD_NOT
))
1496 if (i
->defExists(0)) {
1497 if (i
->def(0).getFile() == FILE_GPR
)
1498 rDef
= i
->getDef(0);
1500 pDef
= i
->getDef(0);
1502 if (i
->defExists(1)) {
1503 if (i
->def(1).getFile() == FILE_GPR
)
1504 rDef
= i
->getDef(1);
1506 pDef
= i
->getDef(1);
1510 code
[0] &= ~(63 << 14);
1514 code
[1] &= ~(7 << 21);
1515 defId(pDef
, 32 + 21);
1520 CodeEmitterNVC0::emitAFETCH(const Instruction
*i
)
1522 code
[0] = 0x00000006;
1523 code
[1] = 0x0c000000 | (i
->src(0).get()->reg
.data
.offset
& 0x7ff);
1525 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1530 defId(i
->def(0), 14);
1531 srcId(i
->src(0).getIndirect(0), 20);
1535 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1537 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1539 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1540 code
[1] = 0x00000000 | (prim
>> 6);
1544 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1546 defId(i
->def(0), 14);
1551 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1553 code
[0] = 0x00000006;
1554 code
[1] = 0x06000000 | i
->src(0).get()->reg
.data
.offset
;
1558 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1559 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1563 code
[0] |= ((i
->getDef(0)->reg
.size
/ 4) - 1) << 5;
1565 defId(i
->def(0), 14);
1566 srcId(i
->src(0).getIndirect(0), 20);
1567 srcId(i
->src(0).getIndirect(1), 26); // vertex address
1571 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1573 unsigned int size
= typeSizeof(i
->dType
);
1575 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1576 code
[1] = 0x0a000000 | i
->src(0).get()->reg
.data
.offset
;
1578 assert(!(code
[1] & ((size
== 12) ? 15 : (size
- 1))));
1585 assert(i
->src(1).getFile() == FILE_GPR
);
1587 srcId(i
->src(0).getIndirect(0), 20);
1588 srcId(i
->src(0).getIndirect(1), 32 + 17); // vertex base address
1589 srcId(i
->src(1), 26);
1593 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1595 code
[0] = 0x00000006;
1596 code
[1] = 0x1c000000;
1600 defId(i
->def(0), 14); // new secret address
1601 srcId(i
->src(0), 20); // old secret address, should be 0 initially
1603 assert(i
->src(0).getFile() == FILE_GPR
);
1605 if (i
->op
== OP_EMIT
)
1607 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1611 if (i
->src(1).getFile() == FILE_IMMEDIATE
) {
1612 unsigned int stream
= SDATA(i
->src(1)).u32
;
1616 code
[0] |= stream
<< 26;
1621 srcId(i
->src(1), 26);
1626 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1628 if (i
->encSize
== 8) {
1629 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1631 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1633 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1638 interpApply(const InterpEntry
*entry
, uint32_t *code
,
1639 bool force_persample_interp
, bool flatshade
)
1641 int ipa
= entry
->ipa
;
1642 int reg
= entry
->reg
;
1643 int loc
= entry
->loc
;
1646 (ipa
& NV50_IR_INTERP_MODE_MASK
) == NV50_IR_INTERP_SC
) {
1647 ipa
= NV50_IR_INTERP_FLAT
;
1649 } else if (force_persample_interp
&&
1650 (ipa
& NV50_IR_INTERP_SAMPLE_MASK
) == NV50_IR_INTERP_DEFAULT
&&
1651 (ipa
& NV50_IR_INTERP_MODE_MASK
) != NV50_IR_INTERP_FLAT
) {
1652 ipa
|= NV50_IR_INTERP_CENTROID
;
1654 code
[loc
+ 0] &= ~(0xf << 6);
1655 code
[loc
+ 0] |= ipa
<< 6;
1656 code
[loc
+ 0] &= ~(0x3f << 26);
1657 code
[loc
+ 0] |= reg
<< 26;
1661 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1663 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1665 if (i
->encSize
== 8) {
1666 code
[0] = 0x00000000;
1667 code
[1] = 0xc0000000 | (base
& 0xffff);
1672 if (i
->op
== OP_PINTERP
) {
1673 srcId(i
->src(1), 26);
1674 addInterp(i
->ipa
, SDATA(i
->src(1)).id
, interpApply
);
1676 code
[0] |= 0x3f << 26;
1677 addInterp(i
->ipa
, 0x3f, interpApply
);
1680 srcId(i
->src(0).getIndirect(0), 20);
1682 assert(i
->op
== OP_PINTERP
);
1683 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1684 srcId(i
->src(1), 20);
1689 defId(i
->def(0), 14);
1691 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1692 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 17);
1694 code
[1] |= 0x3f << 17;
1698 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1731 assert(!"invalid type");
1738 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1759 assert(!"invalid caching mode");
1766 uses64bitAddress(const Instruction
*ldst
)
1768 return ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
&&
1769 ldst
->src(0).isIndirect(0) &&
1770 ldst
->getIndirect(0, 0)->reg
.size
== 8;
1774 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1778 switch (i
->src(0).getFile()) {
1779 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1780 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1781 case FILE_MEMORY_SHARED
:
1783 if (i
->subOp
== NV50_IR_SUBOP_STORE_UNLOCKED
)
1789 assert(!"invalid memory file");
1793 code
[0] = 0x00000005;
1796 setAddressByFile(i
->src(0));
1797 srcId(i
->src(1), 14);
1798 srcId(i
->src(0).getIndirect(0), 20);
1799 if (uses64bitAddress(i
))
1804 emitLoadStoreType(i
->dType
);
1805 emitCachingMode(i
->cache
);
1809 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1813 code
[0] = 0x00000005;
1815 switch (i
->src(0).getFile()) {
1816 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1817 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1818 case FILE_MEMORY_SHARED
:
1820 if (i
->subOp
== NV50_IR_SUBOP_LOAD_LOCKED
)
1825 case FILE_MEMORY_CONST
:
1826 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1827 emitMOV(i
); // not sure if this is any better
1830 opc
= 0x14000000 | (i
->src(0).get()->reg
.fileIndex
<< 10);
1831 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1834 assert(!"invalid memory file");
1840 defId(i
->def(0), 14);
1842 setAddressByFile(i
->src(0));
1843 srcId(i
->src(0).getIndirect(0), 20);
1844 if (uses64bitAddress(i
))
1849 emitLoadStoreType(i
->dType
);
1850 emitCachingMode(i
->cache
);
1854 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1856 switch (SDATA(ref
).sv
.sv
) {
1857 case SV_LANEID
: return 0x00;
1858 case SV_PHYSID
: return 0x03;
1859 case SV_VERTEX_COUNT
: return 0x10;
1860 case SV_INVOCATION_ID
: return 0x11;
1861 case SV_YDIR
: return 0x12;
1862 case SV_THREAD_KILL
: return 0x13;
1863 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1864 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1865 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1866 case SV_GRIDID
: return 0x2c;
1867 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1868 case SV_LBASE
: return 0x34;
1869 case SV_SBASE
: return 0x30;
1870 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1872 assert(!"no sreg for system value");
1878 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1880 if (i
->def(0).getFile() == FILE_PREDICATE
) {
1881 if (i
->src(0).getFile() == FILE_GPR
) {
1882 code
[0] = 0xfc01c003;
1883 code
[1] = 0x1a8e0000;
1884 srcId(i
->src(0), 20);
1886 code
[0] = 0x0001c004;
1887 code
[1] = 0x0c0e0000;
1888 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1890 if (!i
->getSrc(0)->reg
.data
.u32
)
1893 srcId(i
->src(0), 20);
1896 defId(i
->def(0), 17);
1899 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1900 uint8_t sr
= getSRegEncoding(i
->src(0));
1902 if (i
->encSize
== 8) {
1903 code
[0] = 0x00000004 | (sr
<< 26);
1904 code
[1] = 0x2c000000;
1906 code
[0] = 0x40000008 | (sr
<< 20);
1908 defId(i
->def(0), 14);
1912 if (i
->encSize
== 8) {
1915 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
1916 opc
= HEX64(18000000, 000001e2
);
1918 if (i
->src(0).getFile() == FILE_PREDICATE
)
1919 opc
= HEX64(080e0000
, 1c000004
);
1921 opc
= HEX64(28000000, 00000004);
1923 opc
|= i
->lanes
<< 5;
1929 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1930 imm
= SDATA(i
->src(0)).u32
;
1931 if (imm
& 0xfff00000) {
1932 assert(!(imm
& 0x000fffff));
1933 code
[0] = 0x00000318 | imm
;
1935 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1936 code
[0] = 0x00000118 | (imm
<< 20);
1940 emitShortSrc2(i
->src(0));
1942 defId(i
->def(0), 14);
1949 CodeEmitterNVC0::emitATOM(const Instruction
*i
)
1951 const bool hasDst
= i
->defExists(0);
1952 const bool casOrExch
=
1953 i
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
||
1954 i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
;
1956 if (i
->dType
== TYPE_U64
) {
1958 case NV50_IR_SUBOP_ATOM_ADD
:
1961 code
[1] = 0x507e0000;
1963 code
[1] = 0x10000000;
1965 case NV50_IR_SUBOP_ATOM_EXCH
:
1967 code
[1] = 0x507e0000;
1969 case NV50_IR_SUBOP_ATOM_CAS
:
1971 code
[1] = 0x50000000;
1974 assert(!"invalid u64 red op");
1978 if (i
->dType
== TYPE_U32
) {
1980 case NV50_IR_SUBOP_ATOM_EXCH
:
1982 code
[1] = 0x507e0000;
1984 case NV50_IR_SUBOP_ATOM_CAS
:
1986 code
[1] = 0x50000000;
1989 code
[0] = 0x5 | (i
->subOp
<< 5);
1991 code
[1] = 0x507e0000;
1993 code
[1] = 0x10000000;
1997 if (i
->dType
== TYPE_S32
) {
1998 assert(i
->subOp
<= 2);
1999 code
[0] = 0x205 | (i
->subOp
<< 5);
2001 code
[1] = 0x587e0000;
2003 code
[1] = 0x18000000;
2005 if (i
->dType
== TYPE_F32
) {
2006 assert(i
->subOp
== NV50_IR_SUBOP_ATOM_ADD
);
2009 code
[1] = 0x687e0000;
2011 code
[1] = 0x28000000;
2016 srcId(i
->src(1), 14);
2019 defId(i
->def(0), 32 + 11);
2022 code
[1] |= 63 << 11;
2024 if (hasDst
|| casOrExch
) {
2025 const int32_t offset
= SDATA(i
->src(0)).offset
;
2026 assert(offset
< 0x80000 && offset
>= -0x80000);
2027 code
[0] |= offset
<< 26;
2028 code
[1] |= (offset
& 0x1ffc0) >> 6;
2029 code
[1] |= (offset
& 0xe0000) << 6;
2031 srcAddr32(i
->src(0), 26, 0);
2033 if (i
->getIndirect(0, 0)) {
2034 srcId(i
->getIndirect(0, 0), 20);
2035 if (i
->getIndirect(0, 0)->reg
.size
== 8)
2038 code
[0] |= 63 << 20;
2041 if (i
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
2042 assert(i
->src(1).getSize() == 2 * typeSizeof(i
->sType
));
2043 code
[1] |= (SDATA(i
->src(1)).id
+ 1) << 17;
2048 CodeEmitterNVC0::emitMEMBAR(const Instruction
*i
)
2050 switch (NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
)) {
2051 case NV50_IR_SUBOP_MEMBAR_CTA
: code
[0] = 0x05; break;
2052 case NV50_IR_SUBOP_MEMBAR_GL
: code
[0] = 0x25; break;
2055 assert(NV50_IR_SUBOP_MEMBAR_SCOPE(i
->subOp
) == NV50_IR_SUBOP_MEMBAR_SYS
);
2058 code
[1] = 0xe0000000;
2064 CodeEmitterNVC0::emitCCTL(const Instruction
*i
)
2066 code
[0] = 0x00000005 | (i
->subOp
<< 5);
2068 if (i
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
2069 code
[1] = 0x98000000;
2070 srcAddr32(i
->src(0), 28, 2);
2072 code
[1] = 0xd0000000;
2073 setAddress24(i
->src(0));
2075 if (uses64bitAddress(i
))
2077 srcId(i
->src(0).getIndirect(0), 20);
2085 CodeEmitterNVC0::emitSUCLAMPMode(uint16_t subOp
)
2088 switch (subOp
& ~NV50_IR_SUBOP_SUCLAMP_2D
) {
2089 case NV50_IR_SUBOP_SUCLAMP_SD(0, 1): m
= 0; break;
2090 case NV50_IR_SUBOP_SUCLAMP_SD(1, 1): m
= 1; break;
2091 case NV50_IR_SUBOP_SUCLAMP_SD(2, 1): m
= 2; break;
2092 case NV50_IR_SUBOP_SUCLAMP_SD(3, 1): m
= 3; break;
2093 case NV50_IR_SUBOP_SUCLAMP_SD(4, 1): m
= 4; break;
2094 case NV50_IR_SUBOP_SUCLAMP_PL(0, 1): m
= 5; break;
2095 case NV50_IR_SUBOP_SUCLAMP_PL(1, 1): m
= 6; break;
2096 case NV50_IR_SUBOP_SUCLAMP_PL(2, 1): m
= 7; break;
2097 case NV50_IR_SUBOP_SUCLAMP_PL(3, 1): m
= 8; break;
2098 case NV50_IR_SUBOP_SUCLAMP_PL(4, 1): m
= 9; break;
2099 case NV50_IR_SUBOP_SUCLAMP_BL(0, 1): m
= 10; break;
2100 case NV50_IR_SUBOP_SUCLAMP_BL(1, 1): m
= 11; break;
2101 case NV50_IR_SUBOP_SUCLAMP_BL(2, 1): m
= 12; break;
2102 case NV50_IR_SUBOP_SUCLAMP_BL(3, 1): m
= 13; break;
2103 case NV50_IR_SUBOP_SUCLAMP_BL(4, 1): m
= 14; break;
2108 if (subOp
& NV50_IR_SUBOP_SUCLAMP_2D
)
2113 CodeEmitterNVC0::emitSUCalc(Instruction
*i
)
2115 ImmediateValue
*imm
= NULL
;
2118 if (i
->srcExists(2)) {
2119 imm
= i
->getSrc(2)->asImm();
2121 i
->setSrc(2, NULL
); // special case, make emitForm_A not assert
2125 case OP_SUCLAMP
: opc
= HEX64(58000000, 00000004); break;
2126 case OP_SUBFM
: opc
= HEX64(5c000000
, 00000004); break;
2127 case OP_SUEAU
: opc
= HEX64(60000000, 00000004); break;
2134 if (i
->op
== OP_SUCLAMP
) {
2135 if (i
->dType
== TYPE_S32
)
2137 emitSUCLAMPMode(i
->subOp
);
2140 if (i
->op
== OP_SUBFM
&& i
->subOp
== NV50_IR_SUBOP_SUBFM_3D
)
2143 if (i
->op
!= OP_SUEAU
) {
2144 if (i
->def(0).getFile() == FILE_PREDICATE
) { // p, #
2145 code
[0] |= 63 << 14;
2146 code
[1] |= i
->getDef(0)->reg
.data
.id
<< 23;
2148 if (i
->defExists(1)) { // r, p
2149 assert(i
->def(1).getFile() == FILE_PREDICATE
);
2150 code
[1] |= i
->getDef(1)->reg
.data
.id
<< 23;
2156 assert(i
->op
== OP_SUCLAMP
);
2158 code
[1] |= (imm
->reg
.data
.u32
& 0x3f) << 17; // sint6
2163 CodeEmitterNVC0::emitSUGType(DataType ty
)
2166 case TYPE_S32
: code
[1] |= 1 << 13; break;
2167 case TYPE_U8
: code
[1] |= 2 << 13; break;
2168 case TYPE_S8
: code
[1] |= 3 << 13; break;
2170 assert(ty
== TYPE_U32
);
2176 CodeEmitterNVC0::setSUConst16(const Instruction
*i
, const int s
)
2178 const uint32_t offset
= i
->getSrc(s
)->reg
.data
.offset
;
2180 assert(i
->src(s
).getFile() == FILE_MEMORY_CONST
);
2181 assert(offset
== (offset
& 0xfffc));
2184 code
[0] |= offset
<< 24;
2185 code
[1] |= offset
>> 8;
2186 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 8;
2190 CodeEmitterNVC0::setSUPred(const Instruction
*i
, const int s
)
2192 if (!i
->srcExists(s
) || (i
->predSrc
== s
)) {
2193 code
[1] |= 0x7 << 17;
2195 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NOT
))
2197 srcId(i
->src(s
), 32 + 17);
2202 CodeEmitterNVC0::emitSULDGB(const TexInstruction
*i
)
2205 code
[1] = 0xd4000000 | (i
->subOp
<< 15);
2207 emitLoadStoreType(i
->dType
);
2208 emitSUGType(i
->sType
);
2209 emitCachingMode(i
->cache
);
2212 defId(i
->def(0), 14); // destination
2213 srcId(i
->src(0), 20); // address
2215 if (i
->src(1).getFile() == FILE_GPR
)
2216 srcId(i
->src(1), 26);
2223 CodeEmitterNVC0::emitSUSTGx(const TexInstruction
*i
)
2226 code
[1] = 0xdc000000 | (i
->subOp
<< 15);
2228 if (i
->op
== OP_SUSTP
)
2229 code
[1] |= i
->tex
.mask
<< 22;
2231 emitLoadStoreType(i
->dType
);
2232 emitSUGType(i
->sType
);
2233 emitCachingMode(i
->cache
);
2236 srcId(i
->src(0), 20); // address
2238 if (i
->src(1).getFile() == FILE_GPR
)
2239 srcId(i
->src(1), 26);
2242 srcId(i
->src(3), 14); // values
2247 CodeEmitterNVC0::emitVectorSubOp(const Instruction
*i
)
2249 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2251 code
[1] |= (i
->subOp
& 0x000f) << 12; // vsrc1
2252 code
[1] |= (i
->subOp
& 0x00e0) >> 5; // vsrc2
2253 code
[1] |= (i
->subOp
& 0x0100) << 7; // vsrc2
2254 code
[1] |= (i
->subOp
& 0x3c00) << 13; // vdst
2257 code
[1] |= (i
->subOp
& 0x000f) << 8; // v2src1
2258 code
[1] |= (i
->subOp
& 0x0010) << 11; // v2src1
2259 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v2src2
2260 code
[1] |= (i
->subOp
& 0x0200) << 6; // v2src2
2261 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2262 code
[1] |= (i
->mask
& 0x3) << 2;
2265 code
[1] |= (i
->subOp
& 0x000f) << 8; // v4src1
2266 code
[1] |= (i
->subOp
& 0x01e0) >> 1; // v4src2
2267 code
[1] |= (i
->subOp
& 0x3c00) << 2; // v4dst
2268 code
[1] |= (i
->mask
& 0x3) << 2;
2269 code
[1] |= (i
->mask
& 0xc) << 21;
2278 CodeEmitterNVC0::emitVSHL(const Instruction
*i
)
2282 switch (NV50_IR_SUBOP_Vn(i
->subOp
)) {
2283 case 0: opc
|= 0xe8ULL
<< 56; break;
2284 case 1: opc
|= 0xb4ULL
<< 56; break;
2285 case 2: opc
|= 0x94ULL
<< 56; break;
2290 if (NV50_IR_SUBOP_Vn(i
->subOp
) == 1) {
2291 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x2a;
2292 if (isSignedType(i
->sType
)) opc
|= (1 << 6) | (1 << 5);
2294 if (isSignedType(i
->dType
)) opc
|= 1ULL << 0x39;
2295 if (isSignedType(i
->sType
)) opc
|= 1 << 6;
2302 if (i
->flagsDef
>= 0)
2307 CodeEmitterNVC0::emitPIXLD(const Instruction
*i
)
2309 assert(i
->encSize
== 8);
2310 emitForm_A(i
, HEX64(10000000, 00000006));
2311 code
[0] |= i
->subOp
<< 5;
2312 code
[1] |= 0x00e00000;
2316 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
2318 unsigned int size
= insn
->encSize
;
2320 if (writeIssueDelays
&& !(codeSize
& 0x3f))
2323 if (!insn
->encSize
) {
2324 ERROR("skipping unencodable instruction: "); insn
->print();
2327 if (codeSize
+ size
> codeSizeLimit
) {
2328 ERROR("code emitter output buffer too small\n");
2332 if (writeIssueDelays
) {
2333 if (!(codeSize
& 0x3f)) {
2334 code
[0] = 0x00000007; // cf issue delay "instruction"
2335 code
[1] = 0x20000000;
2339 const unsigned int id
= (codeSize
& 0x3f) / 8 - 1;
2340 uint32_t *data
= code
- (id
* 2 + 2);
2342 data
[0] |= insn
->sched
<< (id
* 8 + 4);
2345 data
[0] |= insn
->sched
<< 28;
2346 data
[1] |= insn
->sched
>> 4;
2348 data
[1] |= insn
->sched
<< ((id
- 4) * 8 + 4);
2352 // assert that instructions with multiple defs don't corrupt registers
2353 for (int d
= 0; insn
->defExists(d
); ++d
)
2354 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
2391 if (insn
->dType
== TYPE_F64
)
2393 else if (isFloatType(insn
->dType
))
2399 if (insn
->dType
== TYPE_F64
)
2401 else if (isFloatType(insn
->dType
))
2408 if (insn
->dType
== TYPE_F64
)
2410 else if (isFloatType(insn
->dType
))
2422 emitLogicOp(insn
, 0);
2425 emitLogicOp(insn
, 1);
2428 emitLogicOp(insn
, 2);
2438 emitSET(insn
->asCmp());
2444 emitSLCT(insn
->asCmp());
2459 if (insn
->def(0).getFile() == FILE_PREDICATE
||
2460 insn
->src(0).getFile() == FILE_PREDICATE
)
2466 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
2469 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
2494 emitTEX(insn
->asTex());
2497 emitTXQ(insn
->asTex());
2511 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2512 emitSULDGB(insn
->asTex());
2514 ERROR("SULDB not yet supported on < nve4\n");
2518 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2519 emitSUSTGx(insn
->asTex());
2521 ERROR("SUSTx not yet supported on < nve4\n");
2543 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
2546 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
2549 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
2588 ERROR("operation should have been eliminated");
2594 ERROR("operation should have been lowered\n");
2597 ERROR("unknown op: %u\n", insn
->op
);
2603 assert(insn
->encSize
== 8);
2606 code
+= insn
->encSize
/ 4;
2607 codeSize
+= insn
->encSize
;
2612 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
2614 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
2616 if (writeIssueDelays
|| info
.minEncSize
== 8 || 1)
2619 if (i
->ftz
|| i
->saturate
|| i
->join
)
2621 if (i
->rnd
!= ROUND_N
)
2623 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
2626 if (i
->op
== OP_PINTERP
) {
2627 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
2630 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
2634 for (int s
= 0; i
->srcExists(s
); ++s
) {
2635 if (i
->src(s
).isIndirect(0))
2638 if (i
->src(s
).getFile() == FILE_MEMORY_CONST
) {
2639 if (SDATA(i
->src(s
)).offset
>= 0x100)
2641 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
2642 i
->getSrc(s
)->reg
.fileIndex
!= 16)
2645 if (i
->src(s
).getFile() == FILE_IMMEDIATE
) {
2646 if (i
->dType
== TYPE_F32
) {
2647 if (SDATA(i
->src(s
)).u32
>= 0x100)
2650 if (SDATA(i
->src(s
)).u32
> 0xff)
2655 if (i
->op
== OP_CVT
)
2657 if (i
->src(s
).mod
!= Modifier(0)) {
2658 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_ABS
))
2659 if (i
->op
!= OP_RSQ
)
2661 if (i
->src(s
).mod
== Modifier(NV50_IR_MOD_NEG
))
2662 if (i
->op
!= OP_ADD
|| s
!= 0)
2670 // Simplified, erring on safe side.
2671 class SchedDataCalculator
: public Pass
2674 SchedDataCalculator(const Target
*targ
) : targ(targ
) { }
2680 int st
[DATA_FILE_COUNT
]; // LD to LD delay 3
2681 int ld
[DATA_FILE_COUNT
]; // ST to ST delay 3
2682 int tex
; // TEX to non-TEX delay 17 (0x11)
2683 int sfu
; // SFU to SFU delay 3 (except PRE-ops)
2684 int imul
; // integer MUL to MUL delay 3
2694 void rebase(const int base
)
2696 const int delta
= this->base
- base
;
2701 for (int i
= 0; i
< regs
; ++i
) {
2705 for (int i
= 0; i
< 8; ++i
) {
2712 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2722 memset(&rd
, 0, sizeof(rd
));
2723 memset(&wr
, 0, sizeof(wr
));
2724 memset(&res
, 0, sizeof(res
));
2727 int getLatest(const ScoreData
& d
) const
2730 for (int i
= 0; i
< regs
; ++i
)
2733 for (int i
= 0; i
< 8; ++i
)
2740 inline int getLatestRd() const
2742 return getLatest(rd
);
2744 inline int getLatestWr() const
2746 return getLatest(wr
);
2748 inline int getLatest() const
2750 const int a
= getLatestRd();
2751 const int b
= getLatestWr();
2753 int max
= MAX2(a
, b
);
2754 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2755 max
= MAX2(res
.ld
[f
], max
);
2756 max
= MAX2(res
.st
[f
], max
);
2758 max
= MAX2(res
.sfu
, max
);
2759 max
= MAX2(res
.imul
, max
);
2760 max
= MAX2(res
.tex
, max
);
2763 void setMax(const RegScores
*that
)
2765 for (int i
= 0; i
< regs
; ++i
) {
2766 rd
.r
[i
] = MAX2(rd
.r
[i
], that
->rd
.r
[i
]);
2767 wr
.r
[i
] = MAX2(wr
.r
[i
], that
->wr
.r
[i
]);
2769 for (int i
= 0; i
< 8; ++i
) {
2770 rd
.p
[i
] = MAX2(rd
.p
[i
], that
->rd
.p
[i
]);
2771 wr
.p
[i
] = MAX2(wr
.p
[i
], that
->wr
.p
[i
]);
2773 rd
.c
= MAX2(rd
.c
, that
->rd
.c
);
2774 wr
.c
= MAX2(wr
.c
, that
->wr
.c
);
2776 for (unsigned int f
= 0; f
< DATA_FILE_COUNT
; ++f
) {
2777 res
.ld
[f
] = MAX2(res
.ld
[f
], that
->res
.ld
[f
]);
2778 res
.st
[f
] = MAX2(res
.st
[f
], that
->res
.st
[f
]);
2780 res
.sfu
= MAX2(res
.sfu
, that
->res
.sfu
);
2781 res
.imul
= MAX2(res
.imul
, that
->res
.imul
);
2782 res
.tex
= MAX2(res
.tex
, that
->res
.tex
);
2784 void print(int cycle
)
2786 for (int i
= 0; i
< regs
; ++i
) {
2787 if (rd
.r
[i
] > cycle
)
2788 INFO("rd $r%i @ %i\n", i
, rd
.r
[i
]);
2789 if (wr
.r
[i
] > cycle
)
2790 INFO("wr $r%i @ %i\n", i
, wr
.r
[i
]);
2792 for (int i
= 0; i
< 8; ++i
) {
2793 if (rd
.p
[i
] > cycle
)
2794 INFO("rd $p%i @ %i\n", i
, rd
.p
[i
]);
2795 if (wr
.p
[i
] > cycle
)
2796 INFO("wr $p%i @ %i\n", i
, wr
.p
[i
]);
2799 INFO("rd $c @ %i\n", rd
.c
);
2801 INFO("wr $c @ %i\n", wr
.c
);
2802 if (res
.sfu
> cycle
)
2803 INFO("sfu @ %i\n", res
.sfu
);
2804 if (res
.imul
> cycle
)
2805 INFO("imul @ %i\n", res
.imul
);
2806 if (res
.tex
> cycle
)
2807 INFO("tex @ %i\n", res
.tex
);
2811 RegScores
*score
; // for current BB
2812 std::vector
<RegScores
> scoreBoards
;
2818 bool visit(Function
*);
2819 bool visit(BasicBlock
*);
2821 void commitInsn(const Instruction
*, int cycle
);
2822 int calcDelay(const Instruction
*, int cycle
) const;
2823 void setDelay(Instruction
*, int delay
, Instruction
*next
);
2825 void recordRd(const Value
*, const int ready
);
2826 void recordWr(const Value
*, const int ready
);
2827 void checkRd(const Value
*, int cycle
, int& delay
) const;
2828 void checkWr(const Value
*, int cycle
, int& delay
) const;
2830 int getCycles(const Instruction
*, int origDelay
) const;
2834 SchedDataCalculator::setDelay(Instruction
*insn
, int delay
, Instruction
*next
)
2836 if (insn
->op
== OP_EXIT
|| insn
->op
== OP_RET
)
2837 delay
= MAX2(delay
, 14);
2839 if (insn
->op
== OP_TEXBAR
) {
2840 // TODO: except if results not used before EXIT
2843 if (insn
->op
== OP_JOIN
|| insn
->join
) {
2846 if (delay
>= 0 || prevData
== 0x04 ||
2847 !next
|| !targ
->canDualIssue(insn
, next
)) {
2848 insn
->sched
= static_cast<uint8_t>(MAX2(delay
, 0));
2849 if (prevOp
== OP_EXPORT
)
2850 insn
->sched
|= 0x40;
2852 insn
->sched
|= 0x20;
2854 insn
->sched
= 0x04; // dual-issue
2857 if (prevData
!= 0x04 || prevOp
!= OP_EXPORT
)
2858 if (insn
->sched
!= 0x04 || insn
->op
== OP_EXPORT
)
2861 prevData
= insn
->sched
;
2865 SchedDataCalculator::getCycles(const Instruction
*insn
, int origDelay
) const
2867 if (insn
->sched
& 0x80) {
2868 int c
= (insn
->sched
& 0x0f) * 2 + 1;
2869 if (insn
->op
== OP_TEXBAR
&& origDelay
> 0)
2873 if (insn
->sched
& 0x60)
2874 return (insn
->sched
& 0x1f) + 1;
2875 return (insn
->sched
== 0x04) ? 0 : 32;
2879 SchedDataCalculator::visit(Function
*func
)
2881 int regs
= targ
->getFileSize(FILE_GPR
) + 1;
2882 scoreBoards
.resize(func
->cfg
.getSize());
2883 for (size_t i
= 0; i
< scoreBoards
.size(); ++i
)
2884 scoreBoards
[i
].wipe(regs
);
2889 SchedDataCalculator::visit(BasicBlock
*bb
)
2892 Instruction
*next
= NULL
;
2898 score
= &scoreBoards
.at(bb
->getId());
2900 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
2901 // back branches will wait until all target dependencies are satisfied
2902 if (ei
.getType() == Graph::Edge::BACK
) // sched would be uninitialized
2904 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
2905 if (in
->getExit()) {
2906 if (prevData
!= 0x04)
2907 prevData
= in
->getExit()->sched
;
2908 prevOp
= in
->getExit()->op
;
2910 score
->setMax(&scoreBoards
.at(in
->getId()));
2912 if (bb
->cfg
.incidentCount() > 1)
2915 #ifdef NVC0_DEBUG_SCHED_DATA
2916 INFO("=== BB:%i initial scores\n", bb
->getId());
2917 score
->print(cycle
);
2920 for (insn
= bb
->getEntry(); insn
&& insn
->next
; insn
= insn
->next
) {
2923 commitInsn(insn
, cycle
);
2924 int delay
= calcDelay(next
, cycle
);
2925 setDelay(insn
, delay
, next
);
2926 cycle
+= getCycles(insn
, delay
);
2928 #ifdef NVC0_DEBUG_SCHED_DATA
2929 INFO("cycle %i, sched %02x\n", cycle
, insn
->sched
);
2936 commitInsn(insn
, cycle
);
2940 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
2941 BasicBlock
*out
= BasicBlock::get(ei
.getNode());
2943 if (ei
.getType() != Graph::Edge::BACK
) {
2944 // only test the first instruction of the outgoing block
2945 next
= out
->getEntry();
2947 bbDelay
= MAX2(bbDelay
, calcDelay(next
, cycle
));
2949 // wait until all dependencies are satisfied
2950 const int regsFree
= score
->getLatest();
2951 next
= out
->getFirst();
2952 for (int c
= cycle
; next
&& c
< regsFree
; next
= next
->next
) {
2953 bbDelay
= MAX2(bbDelay
, calcDelay(next
, c
));
2954 c
+= getCycles(next
, bbDelay
);
2959 if (bb
->cfg
.outgoingCount() != 1)
2961 setDelay(insn
, bbDelay
, next
);
2962 cycle
+= getCycles(insn
, bbDelay
);
2964 score
->rebase(cycle
); // common base for initializing out blocks' scores
2968 #define NVE4_MAX_ISSUE_DELAY 0x1f
2970 SchedDataCalculator::calcDelay(const Instruction
*insn
, int cycle
) const
2972 int delay
= 0, ready
= cycle
;
2974 for (int s
= 0; insn
->srcExists(s
); ++s
)
2975 checkRd(insn
->getSrc(s
), cycle
, delay
);
2976 // WAR & WAW don't seem to matter
2977 // for (int s = 0; insn->srcExists(s); ++s)
2978 // recordRd(insn->getSrc(s), cycle);
2980 switch (Target::getOpClass(insn
->op
)) {
2982 ready
= score
->res
.sfu
;
2985 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
2986 ready
= score
->res
.imul
;
2988 case OPCLASS_TEXTURE
:
2989 ready
= score
->res
.tex
;
2992 ready
= score
->res
.ld
[insn
->src(0).getFile()];
2995 ready
= score
->res
.st
[insn
->src(0).getFile()];
3000 if (Target::getOpClass(insn
->op
) != OPCLASS_TEXTURE
)
3001 ready
= MAX2(ready
, score
->res
.tex
);
3003 delay
= MAX2(delay
, ready
- cycle
);
3005 // if can issue next cycle, delay is 0, not 1
3006 return MIN2(delay
- 1, NVE4_MAX_ISSUE_DELAY
);
3010 SchedDataCalculator::commitInsn(const Instruction
*insn
, int cycle
)
3012 const int ready
= cycle
+ targ
->getLatency(insn
);
3014 for (int d
= 0; insn
->defExists(d
); ++d
)
3015 recordWr(insn
->getDef(d
), ready
);
3016 // WAR & WAW don't seem to matter
3017 // for (int s = 0; insn->srcExists(s); ++s)
3018 // recordRd(insn->getSrc(s), cycle);
3020 switch (Target::getOpClass(insn
->op
)) {
3022 score
->res
.sfu
= cycle
+ 4;
3025 if (insn
->op
== OP_MUL
&& !isFloatType(insn
->dType
))
3026 score
->res
.imul
= cycle
+ 4;
3028 case OPCLASS_TEXTURE
:
3029 score
->res
.tex
= cycle
+ 18;
3032 if (insn
->src(0).getFile() == FILE_MEMORY_CONST
)
3034 score
->res
.ld
[insn
->src(0).getFile()] = cycle
+ 4;
3035 score
->res
.st
[insn
->src(0).getFile()] = ready
;
3038 score
->res
.st
[insn
->src(0).getFile()] = cycle
+ 4;
3039 score
->res
.ld
[insn
->src(0).getFile()] = ready
;
3042 if (insn
->op
== OP_TEXBAR
)
3043 score
->res
.tex
= cycle
;
3049 #ifdef NVC0_DEBUG_SCHED_DATA
3050 score
->print(cycle
);
3055 SchedDataCalculator::checkRd(const Value
*v
, int cycle
, int& delay
) const
3060 switch (v
->reg
.file
) {
3063 b
= a
+ v
->reg
.size
/ 4;
3064 for (int r
= a
; r
< b
; ++r
)
3065 ready
= MAX2(ready
, score
->rd
.r
[r
]);
3067 case FILE_PREDICATE
:
3068 ready
= MAX2(ready
, score
->rd
.p
[v
->reg
.data
.id
]);
3071 ready
= MAX2(ready
, score
->rd
.c
);
3073 case FILE_SHADER_INPUT
:
3074 case FILE_SHADER_OUTPUT
: // yes, TCPs can read outputs
3075 case FILE_MEMORY_LOCAL
:
3076 case FILE_MEMORY_CONST
:
3077 case FILE_MEMORY_SHARED
:
3078 case FILE_MEMORY_GLOBAL
:
3079 case FILE_SYSTEM_VALUE
:
3080 // TODO: any restrictions here ?
3082 case FILE_IMMEDIATE
:
3089 delay
= MAX2(delay
, ready
- cycle
);
3093 SchedDataCalculator::checkWr(const Value
*v
, int cycle
, int& delay
) const
3098 switch (v
->reg
.file
) {
3101 b
= a
+ v
->reg
.size
/ 4;
3102 for (int r
= a
; r
< b
; ++r
)
3103 ready
= MAX2(ready
, score
->wr
.r
[r
]);
3105 case FILE_PREDICATE
:
3106 ready
= MAX2(ready
, score
->wr
.p
[v
->reg
.data
.id
]);
3109 assert(v
->reg
.file
== FILE_FLAGS
);
3110 ready
= MAX2(ready
, score
->wr
.c
);
3114 delay
= MAX2(delay
, ready
- cycle
);
3118 SchedDataCalculator::recordWr(const Value
*v
, const int ready
)
3120 int a
= v
->reg
.data
.id
;
3122 if (v
->reg
.file
== FILE_GPR
) {
3123 int b
= a
+ v
->reg
.size
/ 4;
3124 for (int r
= a
; r
< b
; ++r
)
3125 score
->rd
.r
[r
] = ready
;
3127 // $c, $pX: shorter issue-to-read delay (at least as exec pred and carry)
3128 if (v
->reg
.file
== FILE_PREDICATE
) {
3129 score
->rd
.p
[a
] = ready
+ 4;
3131 assert(v
->reg
.file
== FILE_FLAGS
);
3132 score
->rd
.c
= ready
+ 4;
3137 SchedDataCalculator::recordRd(const Value
*v
, const int ready
)
3139 int a
= v
->reg
.data
.id
;
3141 if (v
->reg
.file
== FILE_GPR
) {
3142 int b
= a
+ v
->reg
.size
/ 4;
3143 for (int r
= a
; r
< b
; ++r
)
3144 score
->wr
.r
[r
] = ready
;
3146 if (v
->reg
.file
== FILE_PREDICATE
) {
3147 score
->wr
.p
[a
] = ready
;
3149 if (v
->reg
.file
== FILE_FLAGS
) {
3150 score
->wr
.c
= ready
;
3155 calculateSchedDataNVC0(const Target
*targ
, Function
*func
)
3157 SchedDataCalculator
sched(targ
);
3158 return sched
.run(func
, true, true);
3162 CodeEmitterNVC0::prepareEmission(Function
*func
)
3164 CodeEmitter::prepareEmission(func
);
3166 if (targ
->hasSWSched
)
3167 calculateSchedDataNVC0(targ
, func
);
3170 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
)
3171 : CodeEmitter(target
),
3173 writeIssueDelays(target
->hasSWSched
)
3176 codeSize
= codeSizeLimit
= 0;
3181 TargetNVC0::createCodeEmitterNVC0(Program::Type type
)
3183 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
3184 emit
->setProgramType(type
);
3189 TargetNVC0::getCodeEmitter(Program::Type type
)
3191 if (chipset
>= NVISA_GK20A_CHIPSET
)
3192 return createCodeEmitterGK110(type
);
3193 return createCodeEmitterNVC0(type
);
3196 } // namespace nv50_ir