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 emitDADD(const Instruction
*);
88 void emitIMUL(const Instruction
*);
89 void emitFMUL(const Instruction
*);
90 void emitDMUL(const Instruction
*);
91 void emitIMAD(const Instruction
*);
92 void emitISAD(const Instruction
*);
93 void emitFMAD(const Instruction
*);
94 void emitDMAD(const Instruction
*);
96 void emitNOT(const Instruction
*);
97 void emitLogicOp(const Instruction
*, uint8_t subOp
);
98 void emitPOPC(const Instruction
*);
99 void emitINSBF(const Instruction
*);
100 void emitEXTBF(const Instruction
*);
101 void emitBFIND(const Instruction
*);
102 void emitShift(const Instruction
*);
104 void emitSFnOp(const Instruction
*, uint8_t subOp
);
106 void emitCVT(const Instruction
*);
107 void emitMINMAX(const Instruction
*);
108 void emitPreOp(const Instruction
*);
110 void emitSET(const CmpInstruction
*);
111 void emitSLCT(const CmpInstruction
*);
112 void emitSELP(const Instruction
*);
114 void emitTEXBAR(const Instruction
*);
115 void emitTEX(const TexInstruction
*);
116 void emitTEXCSAA(const TexInstruction
*);
117 void emitTXQ(const TexInstruction
*);
119 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
121 void emitPIXLD(const Instruction
*);
123 void emitFlow(const Instruction
*);
125 inline void defId(const ValueDef
&, const int pos
);
126 inline void srcId(const ValueRef
&, const int pos
);
127 inline void srcId(const ValueRef
*, const int pos
);
128 inline void srcId(const Instruction
*, int s
, const int pos
);
130 inline void srcAddr32(const ValueRef
&, const int pos
); // address / 4
132 inline bool isLIMM(const ValueRef
&, DataType ty
, bool mod
= false);
135 #define GK110_GPR_ZERO 255
138 if (i->src(s).mod.neg()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
140 if (i->src(s).mod.abs()) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
142 #define NOT_(b, s) if (i->src(s).mod & Modifier(NV50_IR_MOD_NOT)) \
143 code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
145 #define FTZ_(b) if (i->ftz) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
146 #define DNZ_(b) if (i->dnz) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
148 #define SAT_(b) if (i->saturate) code[(0x##b) / 32] |= 1 << ((0x##b) % 32)
150 #define RND_(b, t) emitRoundMode##t(i->rnd, 0x##b)
152 #define SDATA(a) ((a).rep()->reg.data)
153 #define DDATA(a) ((a).rep()->reg.data)
155 void CodeEmitterGK110::srcId(const ValueRef
& src
, const int pos
)
157 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: GK110_GPR_ZERO
) << (pos
% 32);
160 void CodeEmitterGK110::srcId(const ValueRef
*src
, const int pos
)
162 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: GK110_GPR_ZERO
) << (pos
% 32);
165 void CodeEmitterGK110::srcId(const Instruction
*insn
, int s
, int pos
)
167 int r
= insn
->srcExists(s
) ? SDATA(insn
->src(s
)).id
: GK110_GPR_ZERO
;
168 code
[pos
/ 32] |= r
<< (pos
% 32);
171 void CodeEmitterGK110::srcAddr32(const ValueRef
& src
, const int pos
)
173 code
[pos
/ 32] |= (SDATA(src
).offset
>> 2) << (pos
% 32);
176 void CodeEmitterGK110::defId(const ValueDef
& def
, const int pos
)
178 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: GK110_GPR_ZERO
) << (pos
% 32);
181 bool CodeEmitterGK110::isLIMM(const ValueRef
& ref
, DataType ty
, bool mod
)
183 const ImmediateValue
*imm
= ref
.get()->asImm();
185 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
189 CodeEmitterGK110::emitRoundMode(RoundMode rnd
, const int pos
, const int rintPos
)
195 case ROUND_MI
: rint
= true; /* fall through */ case ROUND_M
: n
= 1; break;
196 case ROUND_PI
: rint
= true; /* fall through */ case ROUND_P
: n
= 2; break;
197 case ROUND_ZI
: rint
= true; /* fall through */ case ROUND_Z
: n
= 3; break;
199 rint
= rnd
== ROUND_NI
;
201 assert(rnd
== ROUND_N
|| rnd
== ROUND_NI
);
204 code
[pos
/ 32] |= n
<< (pos
% 32);
205 if (rint
&& rintPos
>= 0)
206 code
[rintPos
/ 32] |= 1 << (rintPos
% 32);
210 CodeEmitterGK110::emitRoundModeF(RoundMode rnd
, const int pos
)
215 case ROUND_M
: n
= 1; break;
216 case ROUND_P
: n
= 2; break;
217 case ROUND_Z
: n
= 3; break;
220 assert(rnd
== ROUND_N
);
223 code
[pos
/ 32] |= n
<< (pos
% 32);
227 CodeEmitterGK110::emitRoundModeI(RoundMode rnd
, const int pos
)
232 case ROUND_MI
: n
= 1; break;
233 case ROUND_PI
: n
= 2; break;
234 case ROUND_ZI
: n
= 3; break;
237 assert(rnd
== ROUND_NI
);
240 code
[pos
/ 32] |= n
<< (pos
% 32);
243 void CodeEmitterGK110::emitCondCode(CondCode cc
, int pos
, uint8_t mask
)
248 case CC_FL
: n
= 0x00; break;
249 case CC_LT
: n
= 0x01; break;
250 case CC_EQ
: n
= 0x02; break;
251 case CC_LE
: n
= 0x03; break;
252 case CC_GT
: n
= 0x04; break;
253 case CC_NE
: n
= 0x05; break;
254 case CC_GE
: n
= 0x06; break;
255 case CC_LTU
: n
= 0x09; break;
256 case CC_EQU
: n
= 0x0a; break;
257 case CC_LEU
: n
= 0x0b; break;
258 case CC_GTU
: n
= 0x0c; break;
259 case CC_NEU
: n
= 0x0d; break;
260 case CC_GEU
: n
= 0x0e; break;
261 case CC_TR
: n
= 0x0f; break;
262 case CC_NO
: n
= 0x10; break;
263 case CC_NC
: n
= 0x11; break;
264 case CC_NS
: n
= 0x12; break;
265 case CC_NA
: n
= 0x13; break;
266 case CC_A
: n
= 0x14; break;
267 case CC_S
: n
= 0x15; break;
268 case CC_C
: n
= 0x16; break;
269 case CC_O
: n
= 0x17; break;
272 assert(!"invalid condition code");
275 code
[pos
/ 32] |= (n
& mask
) << (pos
% 32);
279 CodeEmitterGK110::emitPredicate(const Instruction
*i
)
281 if (i
->predSrc
>= 0) {
282 srcId(i
->src(i
->predSrc
), 18);
283 if (i
->cc
== CC_NOT_P
)
284 code
[0] |= 8 << 18; // negate
285 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
292 CodeEmitterGK110::setCAddress14(const ValueRef
& src
)
294 const Storage
& res
= src
.get()->asSym()->reg
;
295 const int32_t addr
= res
.data
.offset
/ 4;
297 code
[0] |= (addr
& 0x01ff) << 23;
298 code
[1] |= (addr
& 0x3e00) >> 9;
299 code
[1] |= res
.fileIndex
<< 5;
303 CodeEmitterGK110::setShortImmediate(const Instruction
*i
, const int s
)
305 const uint32_t u32
= i
->getSrc(s
)->asImm()->reg
.data
.u32
;
306 const uint64_t u64
= i
->getSrc(s
)->asImm()->reg
.data
.u64
;
308 if (i
->sType
== TYPE_F32
) {
309 assert(!(u32
& 0x00000fff));
310 code
[0] |= ((u32
& 0x001ff000) >> 12) << 23;
311 code
[1] |= ((u32
& 0x7fe00000) >> 21);
312 code
[1] |= ((u32
& 0x80000000) >> 4);
314 if (i
->sType
== TYPE_F64
) {
315 assert(!(u64
& 0x00000fffffffffffULL
));
316 code
[0] |= ((u64
& 0x001ff00000000000ULL
) >> 44) << 23;
317 code
[1] |= ((u64
& 0x7fe0000000000000ULL
) >> 53);
318 code
[1] |= ((u64
& 0x8000000000000000ULL
) >> 36);
320 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
321 code
[0] |= (u32
& 0x001ff) << 23;
322 code
[1] |= (u32
& 0x7fe00) >> 9;
323 code
[1] |= (u32
& 0x80000) << 8;
328 CodeEmitterGK110::setImmediate32(const Instruction
*i
, const int s
,
331 uint32_t u32
= i
->getSrc(s
)->asImm()->reg
.data
.u32
;
334 ImmediateValue
imm(i
->getSrc(s
)->asImm(), i
->sType
);
336 u32
= imm
.reg
.data
.u32
;
339 code
[0] |= u32
<< 23;
344 CodeEmitterGK110::emitForm_L(const Instruction
*i
, uint32_t opc
, uint8_t ctg
,
354 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
355 switch (i
->src(s
).getFile()) {
357 srcId(i
->src(s
), s
? 42 : 10);
360 setImmediate32(i
, s
, mod
);
370 CodeEmitterGK110::emitForm_C(const Instruction
*i
, uint32_t opc
, uint8_t ctg
)
379 switch (i
->src(0).getFile()) {
380 case FILE_MEMORY_CONST
:
381 code
[1] |= 0x4 << 28;
382 setCAddress14(i
->src(0));
385 code
[1] |= 0xc << 28;
386 srcId(i
->src(0), 23);
394 // 0x2 for GPR, c[] and 0x1 for short immediate
396 CodeEmitterGK110::emitForm_21(const Instruction
*i
, uint32_t opc2
,
399 const bool imm
= i
->srcExists(1) && i
->src(1).getFile() == FILE_IMMEDIATE
;
402 if (i
->srcExists(2) && i
->src(2).getFile() == FILE_MEMORY_CONST
)
407 code
[1] = opc1
<< 20;
410 code
[1] = (0xc << 28) | (opc2
<< 20);
417 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
418 switch (i
->src(s
).getFile()) {
419 case FILE_MEMORY_CONST
:
420 code
[1] &= (s
== 2) ? ~(0x4 << 28) : ~(0x8 << 28);
421 setCAddress14(i
->src(s
));
424 setShortImmediate(i
, s
);
427 srcId(i
->src(s
), s
? ((s
== 2) ? 42 : s1
) : 10);
430 // ignore here, can be predicate or flags, but must not be address
438 assert(imm
|| (code
[1] & (0xc << 28)));
442 CodeEmitterGK110::modNegAbsF32_3b(const Instruction
*i
, const int s
)
444 if (i
->src(s
).mod
.abs()) code
[1] &= ~(1 << 27);
445 if (i
->src(s
).mod
.neg()) code
[1] ^= (1 << 27);
449 CodeEmitterGK110::emitNOP(const Instruction
*i
)
451 code
[0] = 0x00003c02;
452 code
[1] = 0x85800000;
457 code
[0] = 0x001c3c02;
461 CodeEmitterGK110::emitFMAD(const Instruction
*i
)
463 assert(!isLIMM(i
->src(1), TYPE_F32
));
465 emitForm_21(i
, 0x0c0, 0x940);
473 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
485 CodeEmitterGK110::emitDMAD(const Instruction
*i
)
487 assert(!i
->saturate
);
490 emitForm_21(i
, 0x1b8, 0xb38);
495 bool neg1
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
507 CodeEmitterGK110::emitFMUL(const Instruction
*i
)
509 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
511 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
513 if (isLIMM(i
->src(1), TYPE_F32
)) {
514 emitForm_L(i
, 0x200, 0x2, Modifier(0));
522 assert(i
->postFactor
== 0);
524 emitForm_21(i
, 0x234, 0xc34);
525 code
[1] |= ((i
->postFactor
> 0) ?
526 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 12;
544 CodeEmitterGK110::emitDMUL(const Instruction
*i
)
546 bool neg
= (i
->src(0).mod
^ i
->src(1).mod
).neg();
548 assert(!i
->postFactor
);
549 assert(!i
->saturate
);
553 emitForm_21(i
, 0x240, 0xc40);
567 CodeEmitterGK110::emitIMUL(const Instruction
*i
)
569 assert(!i
->src(0).mod
.neg() && !i
->src(1).mod
.neg());
570 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
572 if (isLIMM(i
->src(1), TYPE_S32
)) {
573 emitForm_L(i
, 0x280, 2, Modifier(0));
575 assert(i
->subOp
!= NV50_IR_SUBOP_MUL_HIGH
);
577 if (i
->sType
== TYPE_S32
)
580 emitForm_21(i
, 0x21c, 0xc1c);
582 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
584 if (i
->sType
== TYPE_S32
)
590 CodeEmitterGK110::emitFADD(const Instruction
*i
)
592 if (isLIMM(i
->src(1), TYPE_F32
)) {
593 assert(i
->rnd
== ROUND_N
);
594 assert(!i
->saturate
);
596 Modifier mod
= i
->src(1).mod
^
597 Modifier(i
->op
== OP_SUB
? NV50_IR_MOD_NEG
: 0);
599 emitForm_L(i
, 0x400, 0, mod
);
605 emitForm_21(i
, 0x22c, 0xc2c);
614 modNegAbsF32_3b(i
, 1);
615 if (i
->op
== OP_SUB
) code
[1] ^= 1 << 27;
619 if (i
->op
== OP_SUB
) code
[1] ^= 1 << 16;
625 CodeEmitterGK110::emitDADD(const Instruction
*i
)
627 assert(!i
->saturate
);
630 emitForm_21(i
, 0x238, 0xc38);
635 modNegAbsF32_3b(i
, 1);
636 if (i
->op
== OP_SUB
) code
[1] ^= 1 << 27;
640 if (i
->op
== OP_SUB
) code
[1] ^= 1 << 16;
645 CodeEmitterGK110::emitUADD(const Instruction
*i
)
647 uint8_t addOp
= (i
->src(0).mod
.neg() << 1) | i
->src(1).mod
.neg();
652 assert(!i
->src(0).mod
.abs() && !i
->src(1).mod
.abs());
654 if (isLIMM(i
->src(1), TYPE_S32
)) {
655 emitForm_L(i
, 0x400, 1, Modifier((addOp
& 1) ? NV50_IR_MOD_NEG
: 0));
660 assert(!i
->defExists(1));
661 assert(i
->flagsSrc
< 0);
665 emitForm_21(i
, 0x208, 0xc08);
667 assert(addOp
!= 3); // would be add-plus-one
669 code
[1] |= addOp
<< 19;
672 code
[1] |= 1 << 18; // write carry
673 if (i
->flagsSrc
>= 0)
674 code
[1] |= 1 << 14; // add carry
682 CodeEmitterGK110::emitIMAD(const Instruction
*i
)
685 (i
->src(2).mod
.neg() << 1) | (i
->src(0).mod
.neg() ^ i
->src(1).mod
.neg());
687 emitForm_21(i
, 0x100, 0xa00);
690 code
[1] |= addOp
<< 26;
692 if (i
->sType
== TYPE_S32
)
693 code
[1] |= (1 << 19) | (1 << 24);
699 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
706 CodeEmitterGK110::emitISAD(const Instruction
*i
)
708 assert(i
->dType
== TYPE_S32
|| i
->dType
== TYPE_U32
);
710 emitForm_21(i
, 0x1f4, 0xb74);
712 if (i
->dType
== TYPE_S32
)
717 CodeEmitterGK110::emitNOT(const Instruction
*i
)
719 code
[0] = 0x0003fc02; // logop(mov2) dst, 0, not src
720 code
[1] = 0x22003800;
726 switch (i
->src(0).getFile()) {
728 code
[1] |= 0xc << 28;
729 srcId(i
->src(0), 23);
731 case FILE_MEMORY_CONST
:
732 code
[1] |= 0x4 << 28;
733 setCAddress14(i
->src(1));
742 CodeEmitterGK110::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
744 if (isLIMM(i
->src(1), TYPE_S32
)) {
745 emitForm_L(i
, 0x200, 0, i
->src(1).mod
);
746 code
[1] |= subOp
<< 24;
749 emitForm_21(i
, 0x220, 0xc20);
750 code
[1] |= subOp
<< 12;
757 CodeEmitterGK110::emitPOPC(const Instruction
*i
)
759 assert(!isLIMM(i
->src(1), TYPE_S32
, true));
761 emitForm_21(i
, 0x204, 0xc04);
764 if (!(code
[0] & 0x1))
769 CodeEmitterGK110::emitINSBF(const Instruction
*i
)
771 emitForm_21(i
, 0x1f8, 0xb78);
775 CodeEmitterGK110::emitEXTBF(const Instruction
*i
)
777 emitForm_21(i
, 0x600, 0xc00);
779 if (i
->dType
== TYPE_S32
)
781 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
786 CodeEmitterGK110::emitBFIND(const Instruction
*i
)
788 emitForm_C(i
, 0x218, 0x2);
790 if (i
->dType
== TYPE_S32
)
792 if (i
->src(0).mod
== Modifier(NV50_IR_MOD_NOT
))
794 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
)
799 CodeEmitterGK110::emitShift(const Instruction
*i
)
801 if (i
->op
== OP_SHR
) {
802 emitForm_21(i
, 0x214, 0xc14);
803 if (isSignedType(i
->dType
))
806 emitForm_21(i
, 0x224, 0xc24);
809 if (i
->subOp
== NV50_IR_SUBOP_SHIFT_WRAP
)
814 CodeEmitterGK110::emitPreOp(const Instruction
*i
)
816 emitForm_C(i
, 0x248, 0x2);
818 if (i
->op
== OP_PREEX2
)
826 CodeEmitterGK110::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
828 code
[0] = 0x00000002 | (subOp
<< 23);
829 code
[1] = 0x84000000;
834 srcId(i
->src(0), 10);
842 CodeEmitterGK110::emitMINMAX(const Instruction
*i
)
866 emitForm_21(i
, op2
, op1
);
868 if (i
->dType
== TYPE_S32
)
870 code
[1] |= (i
->op
== OP_MIN
) ? 0x1c00 : 0x3c00; // [!]pt
876 modNegAbsF32_3b(i
, 1);
884 CodeEmitterGK110::emitCVT(const Instruction
*i
)
886 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
887 const bool f2i
= !isFloatType(i
->dType
) && isFloatType(i
->sType
);
888 const bool i2f
= isFloatType(i
->dType
) && !isFloatType(i
->sType
);
890 bool sat
= i
->saturate
;
891 bool abs
= i
->src(0).mod
.abs();
892 bool neg
= i
->src(0).mod
.neg();
894 RoundMode rnd
= i
->rnd
;
897 case OP_CEIL
: rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
898 case OP_FLOOR
: rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
899 case OP_TRUNC
: rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
900 case OP_SAT
: sat
= true; break;
901 case OP_NEG
: neg
= !neg
; break;
902 case OP_ABS
: abs
= true; neg
= false; break;
909 if (i
->op
== OP_NEG
&& i
->dType
== TYPE_U32
)
918 else if (f2i
) op
= 0x258;
919 else if (i2f
) op
= 0x25c;
922 emitForm_C(i
, op
, 0x2);
925 if (neg
) code
[1] |= 1 << 16;
926 if (abs
) code
[1] |= 1 << 20;
927 if (sat
) code
[1] |= 1 << 21;
929 emitRoundMode(rnd
, 32 + 10, f2f
? (32 + 13) : -1);
931 code
[0] |= typeSizeofLog2(dType
) << 10;
932 code
[0] |= typeSizeofLog2(i
->sType
) << 12;
934 if (isSignedIntType(dType
))
936 if (isSignedIntType(i
->sType
))
941 CodeEmitterGK110::emitSET(const CmpInstruction
*i
)
945 if (i
->def(0).getFile() == FILE_PREDICATE
) {
947 case TYPE_F32
: op2
= 0x1d8; op1
= 0xb58; break;
948 case TYPE_F64
: op2
= 0x1c0; op1
= 0xb40; break;
954 emitForm_21(i
, op2
, op1
);
958 if (!(code
[0] & 0x1)) {
962 modNegAbsF32_3b(i
, 1);
966 // normal DST field is negated predicate result
967 code
[0] = (code
[0] & ~0xfc) | ((code
[0] << 3) & 0xe0);
974 case TYPE_F32
: op2
= 0x000; op1
= 0x800; break;
975 case TYPE_F64
: op2
= 0x080; op1
= 0x900; break;
981 emitForm_21(i
, op2
, op1
);
985 if (!(code
[0] & 0x1)) {
989 modNegAbsF32_3b(i
, 1);
993 if (i
->dType
== TYPE_F32
) {
994 if (isFloatType(i
->sType
))
1000 if (i
->sType
== TYPE_S32
)
1003 if (i
->op
!= OP_SET
) {
1005 case OP_SET_AND
: code
[1] |= 0x0 << 16; break;
1006 case OP_SET_OR
: code
[1] |= 0x1 << 16; break;
1007 case OP_SET_XOR
: code
[1] |= 0x2 << 16; break;
1012 srcId(i
->src(2), 0x2a);
1014 code
[1] |= 0x7 << 10;
1016 emitCondCode(i
->setCond
,
1017 isFloatType(i
->sType
) ? 0x33 : 0x34,
1018 isFloatType(i
->sType
) ? 0xf : 0x7);
1022 CodeEmitterGK110::emitSLCT(const CmpInstruction
*i
)
1024 CondCode cc
= i
->setCond
;
1025 if (i
->src(2).mod
.neg())
1026 cc
= reverseCondCode(cc
);
1028 if (i
->dType
== TYPE_F32
) {
1029 emitForm_21(i
, 0x1d0, 0xb50);
1031 emitCondCode(cc
, 0x33, 0xf);
1033 emitForm_21(i
, 0x1a0, 0xb20);
1034 emitCondCode(cc
, 0x34, 0x7);
1038 void CodeEmitterGK110::emitSELP(const Instruction
*i
)
1040 emitForm_21(i
, 0x250, 0x050);
1042 if ((i
->cc
== CC_NOT_P
) ^ (bool)(i
->src(2).mod
& Modifier(NV50_IR_MOD_NOT
)))
1046 void CodeEmitterGK110::emitTEXBAR(const Instruction
*i
)
1048 code
[0] = 0x0000003e | (i
->subOp
<< 23);
1049 code
[1] = 0x77000000;
1054 void CodeEmitterGK110::emitTEXCSAA(const TexInstruction
*i
)
1056 code
[0] = 0x00000002;
1057 code
[1] = 0x76c00000;
1059 code
[1] |= i
->tex
.r
<< 9;
1060 // code[1] |= i->tex.s << (9 + 8);
1062 if (i
->tex
.liveOnly
)
1063 code
[0] |= 0x80000000;
1065 defId(i
->def(0), 2);
1066 srcId(i
->src(0), 10);
1070 isNextIndependentTex(const TexInstruction
*i
)
1072 if (!i
->next
|| !isTextureOp(i
->next
->op
))
1074 if (i
->getDef(0)->interfers(i
->next
->getSrc(0)))
1076 return !i
->next
->srcExists(1) || !i
->getDef(0)->interfers(i
->next
->getSrc(1));
1080 CodeEmitterGK110::emitTEX(const TexInstruction
*i
)
1082 const bool ind
= i
->tex
.rIndirectSrc
>= 0;
1085 code
[0] = 0x00000002;
1088 code
[1] = 0x7e000000;
1091 code
[1] = 0x7e800000;
1094 code
[1] = 0x78000000;
1097 code
[1] = 0x7dc00000;
1100 code
[1] = 0x7d800000;
1106 code
[0] = 0x00000002;
1107 code
[1] = 0x76000000;
1108 code
[1] |= i
->tex
.r
<< 9;
1111 code
[0] = 0x00000002;
1112 code
[1] = 0x76800000;
1113 code
[1] |= i
->tex
.r
<< 9;
1116 code
[0] = 0x00000002;
1117 code
[1] = 0x70000000;
1118 code
[1] |= i
->tex
.r
<< 13;
1121 code
[0] = 0x00000001;
1122 code
[1] = 0x70000000;
1123 code
[1] |= i
->tex
.r
<< 15;
1126 code
[0] = 0x00000001;
1127 code
[1] = 0x60000000;
1128 code
[1] |= i
->tex
.r
<< 15;
1133 code
[1] |= isNextIndependentTex(i
) ? 0x1 : 0x2; // t : p mode
1135 if (i
->tex
.liveOnly
)
1136 code
[0] |= 0x80000000;
1140 case OP_TXB
: code
[1] |= 0x2000; break;
1141 case OP_TXL
: code
[1] |= 0x3000; break;
1145 case OP_TXLQ
: break;
1147 assert(!"invalid texture op");
1151 if (i
->op
== OP_TXF
) {
1152 if (!i
->tex
.levelZero
)
1155 if (i
->tex
.levelZero
) {
1159 if (i
->op
!= OP_TXD
&& i
->tex
.derivAll
)
1164 code
[1] |= i
->tex
.mask
<< 2;
1166 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1168 defId(i
->def(0), 2);
1169 srcId(i
->src(0), 10);
1172 if (i
->op
== OP_TXG
) code
[1] |= i
->tex
.gatherComp
<< 13;
1175 code
[1] |= (i
->tex
.target
.isCube() ? 3 : (i
->tex
.target
.getDim() - 1)) << 7;
1176 if (i
->tex
.target
.isArray())
1178 if (i
->tex
.target
.isShadow())
1180 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
1181 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1184 if (i
->srcExists(src1
) && i
->src(src1
).getFile() == FILE_IMMEDIATE
) {
1188 if (i
->tex
.useOffsets
== 1) {
1190 case OP_TXF
: code
[1] |= 0x200; break;
1191 case OP_TXD
: code
[1] |= 0x00400000; break;
1192 default: code
[1] |= 0x800; break;
1195 if (i
->tex
.useOffsets
== 4)
1200 CodeEmitterGK110::emitTXQ(const TexInstruction
*i
)
1202 code
[0] = 0x00000002;
1203 code
[1] = 0x75400001;
1205 switch (i
->tex
.query
) {
1206 case TXQ_DIMS
: code
[0] |= 0x01 << 25; break;
1207 case TXQ_TYPE
: code
[0] |= 0x02 << 25; break;
1208 case TXQ_SAMPLE_POSITION
: code
[0] |= 0x05 << 25; break;
1209 case TXQ_FILTER
: code
[0] |= 0x10 << 25; break;
1210 case TXQ_LOD
: code
[0] |= 0x12 << 25; break;
1211 case TXQ_BORDER_COLOUR
: code
[0] |= 0x16 << 25; break;
1213 assert(!"invalid texture query");
1217 code
[1] |= i
->tex
.mask
<< 2;
1218 code
[1] |= i
->tex
.r
<< 9;
1219 if (/*i->tex.sIndirectSrc >= 0 || */i
->tex
.rIndirectSrc
>= 0)
1220 code
[1] |= 0x08000000;
1222 defId(i
->def(0), 2);
1223 srcId(i
->src(0), 10);
1229 CodeEmitterGK110::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1231 code
[0] = 0x00000002 | ((qOp
& 1) << 31);
1232 code
[1] = 0x7fc00000 | (qOp
>> 1) | (laneMask
<< 12);
1234 defId(i
->def(0), 2);
1235 srcId(i
->src(0), 10);
1236 srcId(i
->srcExists(1) ? i
->src(1) : i
->src(0), 23);
1238 if (i
->op
== OP_QUADOP
&& progType
!= Program::TYPE_FRAGMENT
)
1239 code
[1] |= 1 << 9; // dall
1245 CodeEmitterGK110::emitPIXLD(const Instruction
*i
)
1247 emitForm_L(i
, 0x7f4, 2, Modifier(0));
1248 code
[1] |= i
->subOp
<< 2;
1249 code
[1] |= 0x00070000;
1253 CodeEmitterGK110::emitFlow(const Instruction
*i
)
1255 const FlowInstruction
*f
= i
->asFlow();
1257 unsigned mask
; // bit 0: predicate, bit 1: target
1259 code
[0] = 0x00000000;
1263 code
[1] = f
->absolute
? 0x10800000 : 0x12000000;
1264 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1269 code
[1] = f
->absolute
? 0x11000000 : 0x13000000;
1270 if (i
->srcExists(0) && i
->src(0).getFile() == FILE_MEMORY_CONST
)
1275 case OP_EXIT
: code
[1] = 0x18000000; mask
= 1; break;
1276 case OP_RET
: code
[1] = 0x19000000; mask
= 1; break;
1277 case OP_DISCARD
: code
[1] = 0x19800000; mask
= 1; break;
1278 case OP_BREAK
: code
[1] = 0x1a000000; mask
= 1; break;
1279 case OP_CONT
: code
[1] = 0x1a800000; mask
= 1; break;
1281 case OP_JOINAT
: code
[1] = 0x14800000; mask
= 2; break;
1282 case OP_PREBREAK
: code
[1] = 0x15000000; mask
= 2; break;
1283 case OP_PRECONT
: code
[1] = 0x15800000; mask
= 2; break;
1284 case OP_PRERET
: code
[1] = 0x13800000; mask
= 2; break;
1286 case OP_QUADON
: code
[1] = 0x1b800000; mask
= 0; break;
1287 case OP_QUADPOP
: code
[1] = 0x1c000000; mask
= 0; break;
1288 case OP_BRKPT
: code
[1] = 0x00000000; mask
= 0; break;
1290 assert(!"invalid flow operation");
1296 if (i
->flagsSrc
< 0)
1308 if (f
->op
== OP_CALL
) {
1310 assert(f
->absolute
);
1311 uint32_t pcAbs
= targNVC0
->getBuiltinOffset(f
->target
.builtin
);
1312 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xff800000, 23);
1313 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x007fffff, -9);
1315 assert(!f
->absolute
);
1316 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1317 code
[0] |= (pcRel
& 0x1ff) << 23;
1318 code
[1] |= (pcRel
>> 9) & 0x7fff;
1322 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1323 if (writeIssueDelays
&& !(f
->target
.bb
->binPos
& 0x3f))
1325 // currently we don't want absolute branches
1326 assert(!f
->absolute
);
1327 code
[0] |= (pcRel
& 0x1ff) << 23;
1328 code
[1] |= (pcRel
>> 9) & 0x7fff;
1333 CodeEmitterGK110::emitPFETCH(const Instruction
*i
)
1335 uint32_t prim
= i
->src(0).get()->reg
.data
.u32
;
1337 code
[0] = 0x00000002 | ((prim
& 0xff) << 23);
1338 code
[1] = 0x7f800000;
1342 const int src1
= (i
->predSrc
== 1) ? 2 : 1; // if predSrc == 1, !srcExists(2)
1344 defId(i
->def(0), 2);
1349 CodeEmitterGK110::emitVFETCH(const Instruction
*i
)
1351 unsigned int size
= typeSizeof(i
->dType
);
1352 uint32_t offset
= i
->src(0).get()->reg
.data
.offset
;
1354 code
[0] = 0x00000002 | (offset
<< 23);
1355 code
[1] = 0x7ec00000 | (offset
>> 9);
1356 code
[1] |= (size
/ 4 - 1) << 18;
1360 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1361 code
[1] |= 0x8; // yes, TCPs can read from *outputs* of other threads
1365 defId(i
->def(0), 2);
1366 srcId(i
->src(0).getIndirect(0), 10);
1367 srcId(i
->src(0).getIndirect(1), 32 + 10); // vertex address
1371 CodeEmitterGK110::emitEXPORT(const Instruction
*i
)
1373 unsigned int size
= typeSizeof(i
->dType
);
1374 uint32_t offset
= i
->src(0).get()->reg
.data
.offset
;
1376 code
[0] = 0x00000002 | (offset
<< 23);
1377 code
[1] = 0x7f000000 | (offset
>> 9);
1378 code
[1] |= (size
/ 4 - 1) << 18;
1385 assert(i
->src(1).getFile() == FILE_GPR
);
1387 srcId(i
->src(0).getIndirect(0), 10);
1388 srcId(i
->src(0).getIndirect(1), 32 + 10); // vertex base address
1389 srcId(i
->src(1), 2);
1393 CodeEmitterGK110::emitOUT(const Instruction
*i
)
1395 assert(i
->src(0).getFile() == FILE_GPR
);
1397 emitForm_21(i
, 0x1f0, 0xb70);
1399 if (i
->op
== OP_EMIT
)
1401 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1406 CodeEmitterGK110::emitInterpMode(const Instruction
*i
)
1408 code
[1] |= (i
->ipa
& 0x3) << 21; // TODO: INTERP_SAMPLEID
1409 code
[1] |= (i
->ipa
& 0xc) << (19 - 2);
1413 CodeEmitterGK110::emitINTERP(const Instruction
*i
)
1415 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1417 code
[0] = 0x00000002 | (base
<< 31);
1418 code
[1] = 0x74800000 | (base
>> 1);
1423 if (i
->op
== OP_PINTERP
)
1424 srcId(i
->src(1), 23);
1426 code
[0] |= 0xff << 23;
1428 srcId(i
->src(0).getIndirect(0), 10);
1432 defId(i
->def(0), 2);
1434 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1435 srcId(i
->src(i
->op
== OP_PINTERP
? 2 : 1), 32 + 10);
1437 code
[1] |= 0xff << 10;
1441 CodeEmitterGK110::emitLoadStoreType(DataType ty
, const int pos
)
1473 assert(!"invalid ld/st type");
1476 code
[pos
/ 32] |= n
<< (pos
% 32);
1480 CodeEmitterGK110::emitCachingMode(CacheMode c
, const int pos
)
1501 assert(!"invalid caching mode");
1504 code
[pos
/ 32] |= n
<< (pos
% 32);
1508 CodeEmitterGK110::emitSTORE(const Instruction
*i
)
1510 int32_t offset
= SDATA(i
->src(0)).offset
;
1512 switch (i
->src(0).getFile()) {
1513 case FILE_MEMORY_GLOBAL
: code
[1] = 0xe0000000; code
[0] = 0x00000000; break;
1514 case FILE_MEMORY_LOCAL
: code
[1] = 0x7a800000; code
[0] = 0x00000002; break;
1515 case FILE_MEMORY_SHARED
: code
[1] = 0x7ac00000; code
[0] = 0x00000002; break;
1517 assert(!"invalid memory file");
1521 if (i
->src(0).getFile() != FILE_MEMORY_GLOBAL
)
1524 if (code
[0] & 0x2) {
1525 emitLoadStoreType(i
->dType
, 0x33);
1526 if (i
->src(0).getFile() == FILE_MEMORY_LOCAL
)
1527 emitCachingMode(i
->cache
, 0x2f);
1529 emitLoadStoreType(i
->dType
, 0x38);
1530 emitCachingMode(i
->cache
, 0x3b);
1532 code
[0] |= offset
<< 23;
1533 code
[1] |= offset
>> 9;
1537 srcId(i
->src(1), 2);
1538 srcId(i
->src(0).getIndirect(0), 10);
1542 CodeEmitterGK110::emitLOAD(const Instruction
*i
)
1544 int32_t offset
= SDATA(i
->src(0)).offset
;
1546 switch (i
->src(0).getFile()) {
1547 case FILE_MEMORY_GLOBAL
: code
[1] = 0xc0000000; code
[0] = 0x00000000; break;
1548 case FILE_MEMORY_LOCAL
: code
[1] = 0x7a000000; code
[0] = 0x00000002; break;
1549 case FILE_MEMORY_SHARED
: code
[1] = 0x7ac00000; code
[0] = 0x00000002; break;
1550 case FILE_MEMORY_CONST
:
1551 if (!i
->src(0).isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1556 code
[0] = 0x00000002;
1557 code
[1] = 0x7c800000 | (i
->src(0).get()->reg
.fileIndex
<< 7);
1558 code
[1] |= i
->subOp
<< 15;
1561 assert(!"invalid memory file");
1565 if (code
[0] & 0x2) {
1567 emitLoadStoreType(i
->dType
, 0x33);
1568 if (i
->src(0).getFile() == FILE_MEMORY_LOCAL
)
1569 emitCachingMode(i
->cache
, 0x2f);
1571 emitLoadStoreType(i
->dType
, 0x38);
1572 emitCachingMode(i
->cache
, 0x3b);
1574 code
[0] |= offset
<< 23;
1575 code
[1] |= offset
>> 9;
1579 defId(i
->def(0), 2);
1580 srcId(i
->src(0).getIndirect(0), 10);
1584 CodeEmitterGK110::getSRegEncoding(const ValueRef
& ref
)
1586 switch (SDATA(ref
).sv
.sv
) {
1587 case SV_LANEID
: return 0x00;
1588 case SV_PHYSID
: return 0x03;
1589 case SV_VERTEX_COUNT
: return 0x10;
1590 case SV_INVOCATION_ID
: return 0x11;
1591 case SV_YDIR
: return 0x12;
1592 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1593 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1594 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1595 case SV_GRIDID
: return 0x2c;
1596 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1597 case SV_LBASE
: return 0x34;
1598 case SV_SBASE
: return 0x30;
1599 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1601 assert(!"no sreg for system value");
1607 CodeEmitterGK110::emitMOV(const Instruction
*i
)
1609 if (i
->src(0).getFile() == FILE_SYSTEM_VALUE
) {
1610 code
[0] = 0x00000002 | (getSRegEncoding(i
->src(0)) << 23);
1611 code
[1] = 0x86400000;
1613 defId(i
->def(0), 2);
1615 if (i
->src(0).getFile() == FILE_IMMEDIATE
) {
1616 code
[0] = 0x00000002 | (i
->lanes
<< 14);
1617 code
[1] = 0x74000000;
1619 defId(i
->def(0), 2);
1620 setImmediate32(i
, 0, Modifier(0));
1622 if (i
->src(0).getFile() == FILE_PREDICATE
) {
1623 code
[0] = 0x00000002;
1624 code
[1] = 0x84401c07;
1626 defId(i
->def(0), 2);
1627 srcId(i
->src(0), 14);
1629 emitForm_C(i
, 0x24c, 2);
1630 code
[1] |= i
->lanes
<< 10;
1635 CodeEmitterGK110::emitInstruction(Instruction
*insn
)
1637 const unsigned int size
= (writeIssueDelays
&& !(codeSize
& 0x3f)) ? 16 : 8;
1639 if (insn
->encSize
!= 8) {
1640 ERROR("skipping unencodable instruction: ");
1644 if (codeSize
+ size
> codeSizeLimit
) {
1645 ERROR("code emitter output buffer too small\n");
1649 if (writeIssueDelays
) {
1650 int id
= (codeSize
& 0x3f) / 8 - 1;
1653 code
[0] = 0x00000000; // cf issue delay "instruction"
1654 code
[1] = 0x08000000;
1658 uint32_t *data
= code
- (id
* 2 + 2);
1661 case 0: data
[0] |= insn
->sched
<< 2; break;
1662 case 1: data
[0] |= insn
->sched
<< 10; break;
1663 case 2: data
[0] |= insn
->sched
<< 18; break;
1664 case 3: data
[0] |= insn
->sched
<< 26; data
[1] |= insn
->sched
>> 6; break;
1665 case 4: data
[1] |= insn
->sched
<< 2; break;
1666 case 5: data
[1] |= insn
->sched
<< 10; break;
1667 case 6: data
[1] |= insn
->sched
<< 18; break;
1674 // assert that instructions with multiple defs don't corrupt registers
1675 for (int d
= 0; insn
->defExists(d
); ++d
)
1676 assert(insn
->asTex() || insn
->def(d
).rep()->reg
.data
.id
>= 0);
1710 if (insn
->dType
== TYPE_F64
)
1712 else if (isFloatType(insn
->dType
))
1718 if (insn
->dType
== TYPE_F64
)
1720 else if (isFloatType(insn
->dType
))
1727 if (insn
->dType
== TYPE_F64
)
1729 else if (isFloatType(insn
->dType
))
1741 emitLogicOp(insn
, 0);
1744 emitLogicOp(insn
, 1);
1747 emitLogicOp(insn
, 2);
1757 emitSET(insn
->asCmp());
1763 emitSLCT(insn
->asCmp());
1779 emitSFnOp(insn
, 5 + 2 * insn
->subOp
);
1782 emitSFnOp(insn
, 4 + 2 * insn
->subOp
);
1807 emitTEX(insn
->asTex());
1810 emitTXQ(insn
->asTex());
1835 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
1838 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x66 : 0x99, 0x4);
1841 emitQUADOP(insn
, insn
->src(0).mod
.neg() ? 0x5a : 0xa5, 0x5);
1862 ERROR("operation should have been eliminated");
1868 ERROR("operation should have been lowered\n");
1871 ERROR("unknow op\n");
1884 CodeEmitterGK110::getMinEncodingSize(const Instruction
*i
) const
1886 // No more short instruction encodings.
1891 CodeEmitterGK110::prepareEmission(Function
*func
)
1893 const Target
*targ
= func
->getProgram()->getTarget();
1895 CodeEmitter::prepareEmission(func
);
1897 if (targ
->hasSWSched
)
1898 calculateSchedDataNVC0(targ
, func
);
1901 CodeEmitterGK110::CodeEmitterGK110(const TargetNVC0
*target
)
1902 : CodeEmitter(target
),
1904 writeIssueDelays(target
->hasSWSched
)
1907 codeSize
= codeSizeLimit
= 0;
1912 TargetNVC0::createCodeEmitterGK110(Program::Type type
)
1914 CodeEmitterGK110
*emit
= new CodeEmitterGK110(this);
1915 emit
->setProgramType(type
);
1919 } // namespace nv50_ir