2 * Copyright 2012 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"
25 // CodeEmitter for GK110 encoding of the Fermi/Kepler ISA.
29 class CodeEmitterGK110
: public CodeEmitter
32 CodeEmitterGK110(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_21(const Instruction
*, uint32_t opc2
, uint32_t opc1
);
49 void emitForm_C(const Instruction
*, uint32_t opc
, uint8_t ctg
);
50 void emitForm_L(const Instruction
*, uint32_t opc
, uint8_t ctg
, Modifier
);
52 void emitPredicate(const Instruction
*);
54 void setCAddress14(const ValueRef
&);
55 void setShortImmediate(const Instruction
*, const int s
);
56 void setImmediate32(const Instruction
*, const int s
, Modifier
);
58 void modNegAbsF32_3b(const Instruction
*, const int s
);
60 void emitCondCode(CondCode cc
, int pos
, uint8_t mask
);
61 void emitInterpMode(const Instruction
*);
62 void emitLoadStoreType(DataType ty
, const int pos
);
63 void emitCachingMode(CacheMode c
, const int pos
);
65 inline uint8_t getSRegEncoding(const ValueRef
&);
67 void emitRoundMode(RoundMode
, const int pos
, const int rintPos
);
68 void emitRoundModeF(RoundMode
, const int pos
);
69 void emitRoundModeI(RoundMode
, const int pos
);
71 void emitNegAbs12(const Instruction
*);
73 void emitNOP(const Instruction
*);
75 void emitLOAD(const Instruction
*);
76 void emitSTORE(const Instruction
*);
77 void emitMOV(const Instruction
*);
79 void emitINTERP(const Instruction
*);
80 void emitPFETCH(const Instruction
*);
81 void emitVFETCH(const Instruction
*);
82 void emitEXPORT(const Instruction
*);
83 void emitOUT(const Instruction
*);
85 void emitUADD(const Instruction
*);
86 void emitFADD(const Instruction
*);
87 void emitIMUL(const Instruction
*);
88 void emitFMUL(const Instruction
*);
89 void emitIMAD(const Instruction
*);
90 void emitISAD(const Instruction
*);
91 void emitFMAD(const Instruction
*);
93 void emitNOT(const Instruction
*);
94 void emitLogicOp(const Instruction
*, uint8_t subOp
);
95 void emitPOPC(const Instruction
*);
96 void emitINSBF(const Instruction
*);
97 void emitShift(const Instruction
*);
99 void emitSFnOp(const Instruction
*, uint8_t subOp
);
101 void emitCVT(const Instruction
*);
102 void emitMINMAX(const Instruction
*);
103 void emitPreOp(const Instruction
*);
105 void emitSET(const CmpInstruction
*);
106 void emitSLCT(const CmpInstruction
*);
107 void emitSELP(const Instruction
*);
109 void emitTEXBAR(const Instruction
*);
110 void emitTEX(const TexInstruction
*);
111 void emitTEXCSAA(const TexInstruction
*);
112 void emitTXQ(const TexInstruction
*);
114 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
116 void emitFlow(const Instruction
*);
118 inline void defId(const ValueDef
&, const int pos
);
119 inline void srcId(const ValueRef
&, const int pos
);
120 inline void srcId(const ValueRef
*, const int pos
);
121 inline void srcId(const Instruction
*, int s
, const int pos
);
123 inline void srcAddr32(const ValueRef
&, const int pos
); // address / 4
125 inline bool isLIMM(const ValueRef
&, DataType ty
, bool mod
= false);
128 #define GK110_GPR_ZERO 255
131 if (i->src(s).mod.neg()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
133 if (i->src(s).mod.abs()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
135 #define NOT_(b, s) if (i->src(s).mod & Modifier(NV50_IR_MOD_NOT)) \
136 code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
138 #define FTZ_(b) if (i->ftz) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
140 #define SAT_(b) if (i->saturate) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
142 #define RND_(b, t) emitRoundMode##t(i->rnd, 0x##b)
144 #define SDATA(a) ((a).rep()->reg.data)
145 #define DDATA(a) ((a).rep()->reg.data)
147 void CodeEmitterGK110::srcId(const ValueRef
& src
, const int pos
)
149 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: GK110_GPR_ZERO
) << (pos
% 32);
152 void CodeEmitterGK110::srcId(const ValueRef
*src
, const int pos
)
154 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: GK110_GPR_ZERO
) << (pos
% 32);
157 void CodeEmitterGK110::srcId(const Instruction
*insn
, int s
, int pos
)
159 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: GK110_GPR_ZERO
;
160 code
[pos
/ 32] |= r
<< (pos
% 32);
163 void CodeEmitterGK110::srcAddr32(const ValueRef
& src
, const int pos
)
165 code
[pos
/ 32] |= (SDATA(src
).offset
>> 2) << (pos
% 32);
168 void CodeEmitterGK110::defId(const ValueDef
& def
, const int pos
)
170 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: GK110_GPR_ZERO
) << (pos
% 32);
173 bool CodeEmitterGK110::isLIMM(const ValueRef
& ref
, DataType ty
, bool mod
)
175 const ImmediateValue
*imm
= ref
.get()->asImm();
177 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
181 CodeEmitterGK110::emitRoundMode(RoundMode rnd
, const int pos
, const int rintPos
)
187 case ROUND_MI
: rint
= true; /* fall through */ case ROUND_M
: n
= 1; break;
188 case ROUND_PI
: rint
= true; /* fall through */ case ROUND_P
: n
= 2; break;
189 case ROUND_ZI
: rint
= true; /* fall through */ case ROUND_Z
: n
= 3; break;
191 rint
= rnd
== ROUND_NI
;
193 assert(rnd
== ROUND_N
|| rnd
== ROUND_NI
);
196 code
[pos
/ 32] |= n
<< (pos
% 32);
197 if (rint
&& rintPos
>= 0)
198 code
[rintPos
/ 32] |= 1 << (rintPos
% 32);
202 CodeEmitterGK110::emitRoundModeF(RoundMode rnd
, const int pos
)
207 case ROUND_M
: n
= 1; break;
208 case ROUND_P
: n
= 2; break;
209 case ROUND_Z
: n
= 3; break;
212 assert(rnd
== ROUND_N
);
215 code
[pos
/ 32] |= n
<< (pos
% 32);
219 CodeEmitterGK110::emitRoundModeI(RoundMode rnd
, const int pos
)
224 case ROUND_MI
: n
= 1; break;
225 case ROUND_PI
: n
= 2; break;
226 case ROUND_ZI
: n
= 3; break;
229 assert(rnd
== ROUND_NI
);
232 code
[pos
/ 32] |= n
<< (pos
% 32);
235 void CodeEmitterGK110::emitCondCode(CondCode cc
, int pos
, uint8_t mask
)
240 case CC_FL
: n
= 0x00; break;
241 case CC_LT
: n
= 0x01; break;
242 case CC_EQ
: n
= 0x02; break;
243 case CC_LE
: n
= 0x03; break;
244 case CC_GT
: n
= 0x04; break;
245 case CC_NE
: n
= 0x05; break;
246 case CC_GE
: n
= 0x06; break;
247 case CC_LTU
: n
= 0x09; break;
248 case CC_EQU
: n
= 0x0a; break;
249 case CC_LEU
: n
= 0x0b; break;
250 case CC_GTU
: n
= 0x0c; break;
251 case CC_NEU
: n
= 0x0d; break;
252 case CC_GEU
: n
= 0x0e; break;
253 case CC_TR
: n
= 0x0f; break;
254 case CC_NO
: n
= 0x10; break;
255 case CC_NC
: n
= 0x11; break;
256 case CC_NS
: n
= 0x12; break;
257 case CC_NA
: n
= 0x13; break;
258 case CC_A
: n
= 0x14; break;
259 case CC_S
: n
= 0x15; break;
260 case CC_C
: n
= 0x16; break;
261 case CC_O
: n
= 0x17; break;
264 assert(!"invalid condition code");
267 code
[pos
/ 32] |= (n
& mask
) << (pos
% 32);
271 CodeEmitterGK110::emitPredicate(const Instruction
*i
)
273 if (i
->predSrc
>= 0) {
274 srcId(i
->src(i
->predSrc
), 18);
275 if (i
->cc
== CC_NOT_P
)
276 code
[0] |= 8 << 18; // negate
277 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
284 CodeEmitterGK110::setCAddress14(const ValueRef
& src
)
286 const int32_t addr
= src
.get()->asSym()->reg
.data
.offset
/ 4;
288 code
[0] |= (addr
& 0x01ff) << 23;
289 code
[1] |= (addr
& 0x3e00) >> 9;
293 CodeEmitterGK110::setShortImmediate(const Instruction
*i
, const int s
)
295 const uint32_t u32
= i
->getSrc(s
)->asImm()->reg
.data
.u32
;
296 const uint64_t u64
= i
->getSrc(s
)->asImm()->reg
.data
.u64
;
298 if (i
->sType
== TYPE_F32
) {
299 assert(!(u32
& 0x00000fff));
300 code
[0] |= ((u32
& 0x001ff000) >> 12) << 23;
301 code
[1] |= ((u32
& 0x7fe00000) >> 21);
302 code
[1] |= ((u32
& 0x80000000) >> 4);
304 if (i
->sType
== TYPE_F64
) {
305 assert(!(u64
& 0x00000fffffffffffULL
));
306 code
[0] |= ((u64
& 0x001ff00000000000ULL
) >> 44) << 23;
307 code
[1] |= ((u64
& 0x7fe0000000000000ULL
) >> 53);
308 code
[1] |= ((u64
& 0x8000000000000000ULL
) >> 36);
310 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
311 code
[0] |= (u32
& 0x001ff) << 23;
312 code
[1] |= (u32
& 0x7fe00) >> 9;
313 code
[1] |= (u32
& 0x80000) << 8;
318 CodeEmitterGK110::setImmediate32(const Instruction
*i
, const int s
,
321 uint32_t u32
= i
->getSrc(s
)->asImm()->reg
.data
.u32
;
324 ImmediateValue
imm(i
->getSrc(s
)->asImm(), i
->sType
);
326 u32
= imm
.reg
.data
.u32
;
329 code
[0] |= u32
<< 23;
334 CodeEmitterGK110::emitForm_L(const Instruction
*i
, uint32_t opc
, uint8_t ctg
,
344 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
345 switch (i
->src(s
).getFile()) {
347 srcId(i
->src(s
), s
? 42 : 10);
350 setImmediate32(i
, s
, mod
);
360 CodeEmitterGK110::emitForm_C(const Instruction
*i
, uint32_t opc
, uint8_t ctg
)
369 switch (i
->src(0).getFile()) {
370 case FILE_MEMORY_CONST
:
371 code
[1] |= 0x4 << 28;
372 setCAddress14(i
->src(0));
375 code
[1] |= 0xc << 28;
376 srcId(i
->src(0), 23);
384 // 0x2 for GPR, c[] and 0x1 for short immediate
386 CodeEmitterGK110::emitForm_21(const Instruction
*i
, uint32_t opc2
,
389 const bool imm
= i
->srcExists(1) && i
->src(1).getFile() == FILE_IMMEDIATE
;
392 if (i
->srcExists(2) && i
->src(2).getFile() == FILE_MEMORY_CONST
)
397 code
[1] = opc1
<< 20;
400 code
[1] = (0xc << 28) | (opc2
<< 20);
407 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
408 switch (i
->src(s
).getFile()) {
409 case FILE_MEMORY_CONST
:
410 code
[1] &= (s
== 2) ? ~(0x4 << 28) : ~(0x8 << 28);
411 setCAddress14(i
->src(s
));
412 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 5;
415 setShortImmediate(i
, s
);
418 srcId(i
->src(s
), s
? ((s
== 2) ? 42 : s1
) : 10);
421 // ignore here, can be predicate or flags, but must not be address
429 assert(imm
|| (code
[1] & (0xc << 28)));
433 CodeEmitterGK110::modNegAbsF32_3b(const Instruction
*i
, const int s
)
435 if (i
->src(s
).mod
.abs()) code
[1] &= ~(1 << 27);
436 if (i
->src(s
).mod
.neg()) code
[1] ^= (1 << 27);
440 CodeEmitterGK110::emitNOP(const Instruction
*i
)
442 code
[0] = 0x00003c02;
443 code
[1] = 0x85800000;
448 code
[0] = 0x001c3c02;
452 CodeEmitterGK110::emitFMAD(const Instruction
*i
)
454 assert(!isLIMM(i
->src(1), TYPE_F32
));
456 emitForm_21(i
, 0x0c0, 0x940);
463 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
475 CodeEmitterGK110::emitFMUL(const Instruction
*i
)
477 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
479 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
481 if (isLIMM(i
->src(1), TYPE_F32
)) {
482 emitForm_L(i
, 0x200, 0x2, Modifier(0));
489 assert(i
->postFactor
== 0);
491 emitForm_21(i
, 0x234, 0xc34);
508 CodeEmitterGK110::emitIMUL(const Instruction
*i
)
510 assert(!i
->src(0).mod
.neg() && !i
->src(1).mod
.neg());
511 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
513 if (isLIMM(i
->src(1), TYPE_S32
)) {
514 emitForm_L(i
, 0x280, 2, Modifier(0));
516 assert(i
->subOp
!= NV50_IR_SUBOP_MUL_HIGH
);
518 if (i
->sType
== TYPE_S32
)
521 emitForm_21(i
, 0x21c, 0xc1c);
523 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
525 if (i
->sType
== TYPE_S32
)
531 CodeEmitterGK110::emitFADD(const Instruction
*i
)
533 if (isLIMM(i
->src(1), TYPE_F32
)) {
534 assert(i
->rnd
== ROUND_N
);
535 assert(!i
->saturate
);
537 Modifier mod
= i
->src(1).mod
^
538 Modifier(i
->op
== OP_SUB
? NV50_IR_MOD_NEG
: 0);
540 emitForm_L(i
, 0x400, 0, mod
);
546 emitForm_21(i
, 0x22c, 0xc2c);
554 modNegAbsF32_3b(i
, 1);
555 if (i
->op
== OP_SUB
) code
[1] ^= 1 << 27;
559 if (i
->op
== OP_SUB
) code
[1] ^= 1 << 16;
565 CodeEmitterGK110::emitUADD(const Instruction
*i
)
567 uint8_t addOp
= (i
->src(0).mod
.neg() << 1) | i
->src(1).mod
.neg();
572 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
574 if (isLIMM(i
->src(1), TYPE_S32
)) {
575 emitForm_L(i
, 0x400, 1, Modifier((addOp
& 1) ? NV50_IR_MOD_NEG
: 0));
580 assert(!i
->defExists(1));
581 assert(i
->flagsSrc
< 0);
585 emitForm_21(i
, 0x208, 0xc08);
587 assert(addOp
!= 3); // would be add-plus-one
589 code
[1] |= addOp
<< 19;
592 code
[1] |= 1 << 18; // write carry
593 if (i
->flagsSrc
>= 0)
594 code
[1] |= 1 << 14; // add carry
602 CodeEmitterGK110::emitIMAD(const Instruction
*i
)
605 (i
->src(2).mod
.neg() << 1) | (i
->src(0).mod
.neg() ^ i
->src(1).mod
.neg());
607 emitForm_21(i
, 0x100, 0xa00);
610 code
[1] |= addOp
<< 26;
612 if (i
->sType
== TYPE_S32
)
613 code
[1] |= (1 << 19) | (1 << 24);
619 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
626 CodeEmitterGK110::emitISAD(const Instruction
*i
)
628 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
630 emitForm_21(i
, 0x1fc, 0xb74);
632 if (i
->dType
== TYPE_S32
)
637 CodeEmitterGK110::emitNOT(const Instruction
*i
)
639 code
[0] = 0x0003fc02; // logop(mov2) dst, 0, not src
640 code
[1] = 0x22003800;
646 switch (i
->src(0).getFile()) {
648 code
[1] |= 0xc << 28;
649 srcId(i
->src(0), 23);
651 case FILE_MEMORY_CONST
:
652 code
[1] |= 0x4 << 28;
653 setCAddress14(i
->src(1));
662 CodeEmitterGK110::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
664 assert(!(i
->src(0).mod
& Modifier(NV50_IR_MOD_NOT
))); // XXX: find me
666 if (isLIMM(i
->src(1), TYPE_S32
)) {
667 emitForm_L(i
, 0x200, 0, i
->src(1).mod
);
668 code
[1] |= subOp
<< 24;
670 emitForm_21(i
, 0x220, 0xc20);
671 code
[1] |= subOp
<< 12;
674 assert(!(code
[0] & 0x1) || !(i
->src(1).mod
& Modifier(NV50_IR_MOD_NOT
)));
678 CodeEmitterGK110::emitPOPC(const Instruction
*i
)
680 assert(!isLIMM(i
->src(1), TYPE_S32
, true));
682 emitForm_21(i
, 0x204, 0xc04);
685 if (!(code
[0] & 0x1))
690 CodeEmitterGK110::emitINSBF(const Instruction
*i
)
692 emitForm_21(i
, 0x1f8, 0xb78);
696 CodeEmitterGK110::emitShift(const Instruction
*i
)
698 const bool sar
= i
->op
== OP_SHR
&& isSignedType(i
->sType
);
701 emitForm_21(i
, 0x214, 0x014);
704 if (i
->op
== OP_SHR
) {
705 // this is actually RSHF
706 emitForm_21(i
, 0x27c, 0x87c);
707 code
[1] |= GK110_GPR_ZERO
<< 10;
709 // this is actually LSHF
710 emitForm_21(i
, 0x1fc, 0xb7c);
711 code
[1] |= GK110_GPR_ZERO
<< 10;
714 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
) {
717 // XXX: find wrap modifier for SHR S32
722 CodeEmitterGK110::emitPreOp(const Instruction
*i
)
724 emitForm_C(i
, 0x248, 0x2);
726 if (i
->op
== OP_PREEX2
)
734 CodeEmitterGK110::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
736 code
[0] = 0x00000002 | (subOp
<< 23);
737 code
[1] = 0x84000000;
742 srcId(i
->src(0), 10);
750 CodeEmitterGK110::emitMINMAX(const Instruction
*i
)
774 emitForm_21(i
, op2
, op1
);
776 if (i
->dType
== TYPE_S32
)
778 code
[1] |= (i
->op
== OP_MIN
) ? 0x1c00 : 0x3c00; // [!]pt
784 modNegAbsF32_3b(i
, 1);
792 CodeEmitterGK110::emitCVT(const Instruction
*i
)
794 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
795 const bool f2i
= !isFloatType(i
->dType
) && isFloatType(i
->sType
);
796 const bool i2f
= isFloatType(i
->dType
) && !isFloatType(i
->sType
);
798 bool sat
= i
->saturate
;
799 bool abs
= i
->src(0).mod
.abs();
800 bool neg
= i
->src(0).mod
.neg();
802 RoundMode rnd
= i
->rnd
;
805 case OP_CEIL
: rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
806 case OP_FLOOR
: rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
807 case OP_TRUNC
: rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
808 case OP_SAT
: sat
= true; break;
809 case OP_NEG
: neg
= !neg
; break;
810 case OP_ABS
: abs
= true; neg
= false; break;
817 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
826 else if (f2i
) op
= 0x258;
827 else if (i2f
) op
= 0x25c;
830 emitForm_C(i
, op
, 0x2);
833 if (neg
) code
[1] |= 1 << 16;
834 if (abs
) code
[1] |= 1 << 20;
835 if (sat
) code
[1] |= 1 << 21;
837 emitRoundMode(rnd
, 32 + 10, f2f
? (32 + 13) : -1);
839 code
[0] |= typeSizeofLog2(dType
) << 10;
840 code
[0] |= typeSizeofLog2(i
->sType
) << 12;
842 if (isSignedIntType(dType
))
844 if (isSignedIntType(i
->sType
))
849 CodeEmitterGK110::emitSET(const CmpInstruction
*i
)
853 if (i
->def(0).getFile() == FILE_PREDICATE
) {
855 case TYPE_F32
: op2
= 0x1d8; op1
= 0xb58; break;
856 case TYPE_F64
: op2
= 0x1c0; op1
= 0xb40; break;
862 emitForm_21(i
, op2
, op1
);
866 if (!(code
[0] & 0x1)) {
870 modNegAbsF32_3b(i
, 1);
874 // normal DST field is negated predicate result
875 code
[0] = (code
[0] & ~0xfc) | ((code
[0] << 3) & 0xe0);
882 case TYPE_F32
: op2
= 0x000; op1
= 0x820; break;
883 case TYPE_F64
: op2
= 0x080; op1
= 0x900; break;
889 emitForm_21(i
, op2
, op1
);
893 if (!(code
[0] & 0x1)) {
897 modNegAbsF32_3b(i
, 1);
901 if (i
->sType
== TYPE_S32
)
904 if (i
->op
!= OP_SET
) {
906 case OP_SET_AND
: code
[1] |= 0x0 << 16; break;
907 case OP_SET_OR
: code
[1] |= 0x1 << 16; break;
908 case OP_SET_XOR
: code
[1] |= 0x2 << 16; break;
913 srcId(i
->src(2), 0x2a);
915 code
[1] |= 0x7 << 10;
917 emitCondCode(i
->setCond
,
918 isFloatType(i
->sType
) ? 0x33 : 0x34,
919 isFloatType(i
->sType
) ? 0xf : 0x7);
923 CodeEmitterGK110::emitSLCT(const CmpInstruction
*i
)
925 CondCode cc
= i
->setCond
;
926 if (i
->src(2).mod
.neg())
927 cc
= reverseCondCode(cc
);
929 if (i
->dType
== TYPE_F32
) {
930 emitForm_21(i
, 0x1d0, 0xb50);
932 emitCondCode(cc
, 0x33, 0xf);
934 emitForm_21(i
, 0x1a4, 0xb20);
935 emitCondCode(cc
, 0x34, 0x7);
939 void CodeEmitterGK110::emitSELP(const Instruction
*i
)
941 emitForm_21(i
, 0x250, 0x050);
943 if ((i
->cc
== CC_NOT_P
) ^ (bool)(i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
)))
947 void CodeEmitterGK110::emitTEXBAR(const Instruction
*i
)
949 code
[0] = 0x00000002 | (i
->subOp
<< 23);
950 code
[1] = 0x77000000;
955 void CodeEmitterGK110::emitTEXCSAA(const TexInstruction
*i
)
957 code
[0] = 0x00000002;
958 code
[1] = 0x76c00000;
960 code
[1] |= i
->tex
.r
<< 9;
961 // code[1] |= i->tex.s << (9 + 8);
964 code
[0] |= 0x80000000;
967 srcId(i
->src(0), 10);
971 isNextIndependentTex(const TexInstruction
*i
)
973 if (!i
->next
|| !isTextureOp(i
->next
->op
))
975 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
977 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
981 CodeEmitterGK110::emitTEX(const TexInstruction
*i
)
983 const bool ind
= i
->tex
.rIndirectSrc
>= 0;
986 code
[0] = 0x00000002;
989 code
[1] = 0x7e000000;
992 code
[1] = 0x78000000;
995 code
[1] = 0x7d800000;
1001 code
[0] = 0x00000002;
1002 code
[1] = 0x76000000;
1003 code
[1] |= i
->tex
.r
<< 9;
1006 code
[0] = 0x00000002;
1007 code
[1] = 0x70000000;
1008 code
[1] |= i
->tex
.r
<< 13;
1011 code
[0] = 0x00000001;
1012 code
[1] = 0x60000000;
1013 code
[1] |= i
->tex
.r
<< 15;
1018 code
[1] |= isNextIndependentTex(i
) ? 0x1 : 0x2; // t : p mode
1020 if (i
->tex
.liveOnly
)
1021 code
[0] |= 0x80000000;
1025 case OP_TXB
: code
[1] |= 0x2000; break;
1026 case OP_TXL
: code
[1] |= 0x3000; break;
1028 case OP_TXG
: break; // XXX
1031 assert(!"invalid texture op");
1035 if (i
->op
== OP_TXF
) {
1036 if (!i
->tex
.levelZero
)
1039 if (i
->tex
.levelZero
) {
1043 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1048 code
[1] |= i
->tex
.mask
<< 2;
1050 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1052 defId(i
->def(0), 2);
1053 srcId(i
->src(0), 10);
1056 // if (i->op == OP_TXG) code[0] |= i->tex.gatherComp << 5;
1059 code
[1] |= (i
->tex
.target
.isCube() ? 3 : (i
->tex
.target
.getDim() - 1)) << 7;
1060 if (i
->tex
.target
.isArray())
1062 if (i
->tex
.target
.isShadow())
1064 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1065 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1068 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1072 if (i
->tex
.useOffsets
) {
1074 case OP_TXF
: code
[1] |= 0x200; break;
1075 default: code
[1] |= 0x800; break;
1081 CodeEmitterGK110::emitTXQ(const TexInstruction
*i
)
1083 code
[0] = 0x00000002;
1084 code
[1] = 0x75400001;
1086 switch (i
->tex
.query
) {
1087 case TXQ_DIMS
: code
[0] |= 0x01 << 25; break;
1088 case TXQ_TYPE
: code
[0] |= 0x02 << 25; break;
1089 case TXQ_SAMPLE_POSITION
: code
[0] |= 0x05 << 25; break;
1090 case TXQ_FILTER
: code
[0] |= 0x10 << 25; break;
1091 case TXQ_LOD
: code
[0] |= 0x12 << 25; break;
1092 case TXQ_BORDER_COLOUR
: code
[0] |= 0x16 << 25; break;
1094 assert(!"invalid texture query");
1098 code
[1] |= i
->tex
.mask
<< 2;
1099 code
[1] |= i
->tex
.r
<< 9;
1100 if (/*i->tex.sIndirectSrc >= 0 || */i
->tex
.rIndirectSrc
>= 0)
1101 code
[1] |= 0x08000000;
1103 defId(i
->def(0), 2);
1104 srcId(i
->src(0), 10);
1110 CodeEmitterGK110::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1112 code
[0] = 0x00000002 | ((qOp
& 1) << 31);
1113 code
[1] = 0x7fc00000 | (qOp
>> 1) | (laneMask
<< 12);
1115 defId(i
->def(0), 2);
1116 srcId(i
->src(0), 10);
1117 srcId(i
->srcExists(1) ? i
->src(1) : i
->src(0), 23);
1119 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1120 code
[1] |= 1 << 9; // dall
1126 CodeEmitterGK110::emitFlow(const Instruction
*i
)
1128 const FlowInstruction
*f
= i
->asFlow();
1130 unsigned mask
; // bit 0: predicate, bit 1: target
1132 code
[0] = 0x00000000;
1136 code
[1] = f
->absolute
? 0x10800000 : 0x12000000;
1137 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1142 code
[1] = f
->absolute
? 0x11000000 : 0x13000000;
1143 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1148 case OP_EXIT
: code
[1] = 0x18000000; mask
= 1; break;
1149 case OP_RET
: code
[1] = 0x19000000; mask
= 1; break;
1150 case OP_DISCARD
: code
[1] = 0x19800000; mask
= 1; break;
1151 case OP_BREAK
: code
[1] = 0x1a000000; mask
= 1; break;
1152 case OP_CONT
: code
[1] = 0x1a800000; mask
= 1; break;
1154 case OP_JOINAT
: code
[1] = 0x14800000; mask
= 2; break;
1155 case OP_PREBREAK
: code
[1] = 0x15000000; mask
= 2; break;
1156 case OP_PRECONT
: code
[1] = 0x15800000; mask
= 2; break;
1157 case OP_PRERET
: code
[1] = 0x13800000; mask
= 2; break;
1159 case OP_QUADON
: code
[1] = 0x1b000000; mask
= 0; break;
1160 case OP_QUADPOP
: code
[1] = 0x1c000000; mask
= 0; break;
1161 case OP_BRKPT
: code
[1] = 0x00000000; mask
= 0; break;
1163 assert(!"invalid flow operation");
1169 if (i
->flagsSrc
< 0)
1181 if (f
->op
== OP_CALL
) {
1183 assert(f
->absolute
);
1184 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1185 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xff800000, 23);
1186 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x007fffff, -9);
1188 assert(!f
->absolute
);
1189 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1190 code
[0] |= (pcRel
& 0x1ff) << 23;
1191 code
[1] |= (pcRel
>> 9) & 0x7fff;
1195 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1196 // currently we don't want absolute branches
1197 assert(!f
->absolute
);
1198 code
[0] |= (pcRel
& 0x1ff) << 23;
1199 code
[1] |= (pcRel
>> 9) & 0x7fff;
1204 CodeEmitterGK110::emitPFETCH(const Instruction
*i
)
1206 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1208 code
[0] = 0x00000002 | ((prim
& 0xff) << 23);
1209 code
[1] = 0x7f800000;
1213 defId(i
->def(0), 2);
1214 srcId(i
->src(1), 10);
1218 CodeEmitterGK110::emitVFETCH(const Instruction
*i
)
1220 unsigned int size
= typeSizeof(i
->dType
);
1221 uint32_t offset
= i
->src(0).get()->reg
.data
.offset
;
1223 code
[0] = 0x00000002 | (offset
<< 23);
1224 code
[1] = 0x7ec00000 | (offset
>> 9);
1225 code
[1] |= (size
/ 4 - 1) << 18;
1230 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1231 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1236 defId(i
->def(0), 2);
1237 srcId(i
->src(0).getIndirect(0), 10);
1238 srcId(i
->src(0).getIndirect(1), 32 + 10); // vertex address
1242 CodeEmitterGK110::emitEXPORT(const Instruction
*i
)
1244 unsigned int size
= typeSizeof(i
->dType
);
1245 uint32_t offset
= i
->src(0).get()->reg
.data
.offset
;
1247 code
[0] = 0x00000002 | (offset
<< 23);
1248 code
[1] = 0x7f000000 | (offset
>> 9);
1249 code
[1] |= (size
/ 4 - 1) << 18;
1258 assert(i
->src(1).getFile() == FILE_GPR
);
1260 srcId(i
->src(0).getIndirect(0), 10);
1261 srcId(i
->src(0).getIndirect(1), 32 + 10); // vertex base address
1262 srcId(i
->src(1), 2);
1266 CodeEmitterGK110::emitOUT(const Instruction
*i
)
1268 assert(i
->src(0).getFile() == FILE_GPR
);
1270 emitForm_21(i
, 0x1f0, 0xb70);
1272 if (i
->op
== OP_EMIT
)
1274 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1279 CodeEmitterGK110::emitInterpMode(const Instruction
*i
)
1281 code
[1] |= i
->ipa
<< 21; // TODO: INTERP_SAMPLEID
1285 CodeEmitterGK110::emitINTERP(const Instruction
*i
)
1287 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1289 code
[0] = 0x00000002 | (base
<< 31);
1290 code
[1] = 0x74800000 | (base
>> 1);
1295 if (i
->op
== OP_PINTERP
)
1296 srcId(i
->src(1), 23);
1298 code
[0] |= 0xff << 23;
1300 srcId(i
->src(0).getIndirect(0), 10);
1304 defId(i
->def(0), 2);
1306 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1307 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 10);
1309 code
[1] |= 0xff << 10;
1313 CodeEmitterGK110::emitLoadStoreType(DataType ty
, const int pos
)
1345 assert(!"invalid ld/st type");
1348 code
[pos
/ 32] |= n
<< (pos
% 32);
1352 CodeEmitterGK110::emitCachingMode(CacheMode c
, const int pos
)
1373 assert(!"invalid caching mode");
1376 code
[pos
/ 32] |= n
<< (pos
% 32);
1380 CodeEmitterGK110::emitSTORE(const Instruction
*i
)
1382 int32_t offset
= SDATA(i
->src(0)).offset
;
1384 switch (i
->src(0).getFile()) {
1385 case FILE_MEMORY_GLOBAL
: code
[1] = 0xe0000000; code
[0] = 0x00000000; break;
1386 case FILE_MEMORY_LOCAL
: code
[1] = 0x7a800000; code
[0] = 0x00000002; break;
1387 case FILE_MEMORY_SHARED
: code
[1] = 0x7ac00000; code
[0] = 0x00000002; break;
1389 assert(!"invalid memory file");
1393 if (i
->src(0).getFile() != FILE_MEMORY_GLOBAL
)
1396 if (code
[0] & 0x2) {
1397 emitLoadStoreType(i
->dType
, 0x33);
1398 if (i
->src(0).getFile() == FILE_MEMORY_LOCAL
)
1399 emitCachingMode(i
->cache
, 0x2f);
1401 emitLoadStoreType(i
->dType
, 0x38);
1402 emitCachingMode(i
->cache
, 0x3b);
1404 code
[0] |= offset
<< 23;
1405 code
[1] |= offset
>> 9;
1409 srcId(i
->src(1), 2);
1410 srcId(i
->src(0).getIndirect(0), 10);
1414 CodeEmitterGK110::emitLOAD(const Instruction
*i
)
1416 int32_t offset
= SDATA(i
->src(0)).offset
;
1418 switch (i
->src(0).getFile()) {
1419 case FILE_MEMORY_GLOBAL
: code
[1] = 0xc0000000; code
[0] = 0x00000000; break;
1420 case FILE_MEMORY_LOCAL
: code
[1] = 0x7a000000; code
[0] = 0x00000002; break;
1421 case FILE_MEMORY_SHARED
: code
[1] = 0x7ac00000; code
[0] = 0x00000002; break;
1422 case FILE_MEMORY_CONST
:
1423 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1428 code
[0] = 0x00000002;
1429 code
[1] = 0x7c800000 | (i
->src(0).get()->reg
.fileIndex
<< 7);
1432 assert(!"invalid memory file");
1436 if (code
[0] & 0x2) {
1438 emitLoadStoreType(i
->dType
, 0x33);
1439 if (i
->src(0).getFile() == FILE_MEMORY_LOCAL
)
1440 emitCachingMode(i
->cache
, 0x2f);
1442 emitLoadStoreType(i
->dType
, 0x38);
1443 emitCachingMode(i
->cache
, 0x3b);
1445 code
[0] |= offset
<< 23;
1446 code
[1] |= offset
>> 9;
1450 defId(i
->def(0), 2);
1451 srcId(i
->src(0).getIndirect(0), 10);
1455 CodeEmitterGK110::getSRegEncoding(const ValueRef
& ref
)
1457 switch (SDATA(ref
).sv
.sv
) {
1458 case SV_LANEID
: return 0x00;
1459 case SV_PHYSID
: return 0x03;
1460 case SV_VERTEX_COUNT
: return 0x10;
1461 case SV_INVOCATION_ID
: return 0x11;
1462 case SV_YDIR
: return 0x12;
1463 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1464 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1465 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1466 case SV_GRIDID
: return 0x2c;
1467 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1468 case SV_LBASE
: return 0x34;
1469 case SV_SBASE
: return 0x30;
1470 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1472 assert(!"no sreg for system value");
1478 CodeEmitterGK110::emitMOV(const Instruction
*i
)
1480 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1481 code
[0] = 0x00000002 | (getSRegEncoding(i
->src(0)) << 23);
1482 code
[1] = 0x86400000;
1484 defId(i
->def(0), 2);
1486 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1487 code
[0] = 0x00000002 | (i
->lanes
<< 14);
1488 code
[1] = 0x74000000;
1490 defId(i
->def(0), 2);
1491 setImmediate32(i
, 0, Modifier(0));
1493 if (i
->src(0).getFile() == FILE_PREDICATE
) {
1494 code
[0] = 0x00000002;
1495 code
[1] = 0x84401c07;
1497 defId(i
->def(0), 2);
1498 srcId(i
->src(0), 14);
1500 emitForm_C(i
, 0x24c, 2);
1501 code
[1] |= i
->lanes
<< 10;
1506 CodeEmitterGK110::emitInstruction(Instruction
*insn
)
1508 const unsigned int size
= (writeIssueDelays
&& !(codeSize
& 0x3f)) ? 16 : 8;
1510 if (insn
->encSize
!= 8) {
1511 ERROR("skipping unencodable instruction: ");
1515 if (codeSize
+ size
> codeSizeLimit
) {
1516 ERROR("code emitter output buffer too small\n");
1520 if (writeIssueDelays
) {
1521 int id
= (codeSize
& 0x3f) / 8 - 1;
1524 code
[0] = 0x00000000; // cf issue delay "instruction"
1525 code
[1] = 0x08000000;
1529 uint32_t *data
= code
- (id
* 2 + 2);
1532 case 0: data
[0] |= insn
->sched
<< 2; break;
1533 case 1: data
[0] |= insn
->sched
<< 10; break;
1534 case 2: data
[0] |= insn
->sched
<< 18; break;
1535 case 3: data
[0] |= insn
->sched
<< 26; data
[1] |= insn
->sched
>> 6; break;
1536 case 4: data
[1] |= insn
->sched
<< 2; break;
1537 case 5: data
[1] |= insn
->sched
<< 10; break;
1538 case 6: data
[1] |= insn
->sched
<< 18; break;
1545 // assert that instructions with multiple defs don't corrupt registers
1546 for (int d
= 0; insn
->defExists(d
); ++d
)
1547 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
1581 if (isFloatType(insn
->dType
))
1587 if (isFloatType(insn
->dType
))
1594 if (isFloatType(insn
->dType
))
1606 emitLogicOp(insn
, 0);
1609 emitLogicOp(insn
, 1);
1612 emitLogicOp(insn
, 2);
1622 emitSET(insn
->asCmp());
1628 emitSLCT(insn
->asCmp());
1670 emitTEX(insn
->asTex());
1673 emitTXQ(insn
->asTex());
1695 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
1698 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
1701 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
1713 ERROR("operation should have been eliminated");
1719 ERROR("operation should have been lowered\n");
1722 ERROR("unknow op\n");
1735 CodeEmitterGK110::getMinEncodingSize(const Instruction
*i
) const
1737 // No more short instruction encodings.
1742 CodeEmitterGK110::prepareEmission(Function
*func
)
1744 const Target
*targ
= func
->getProgram()->getTarget();
1746 CodeEmitter::prepareEmission(func
);
1748 if (targ
->hasSWSched
)
1749 calculateSchedDataNVC0(targ
, func
);
1752 CodeEmitterGK110::CodeEmitterGK110(const TargetNVC0
*target
)
1753 : CodeEmitter(target
),
1755 writeIssueDelays(target
->hasSWSched
)
1758 codeSize
= codeSizeLimit
= 0;
1763 TargetNVC0::createCodeEmitterGK110(Program::Type type
)
1765 CodeEmitterGK110
*emit
= new CodeEmitterGK110(this);
1766 emit
->setProgramType(type
);
1770 } // namespace nv50_ir