2 * Copyright 2011 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "nv50_ir_target_nvc0.h"
27 // Argh, all these assertions ...
29 class CodeEmitterNVC0
: public CodeEmitter
32 CodeEmitterNVC0(const TargetNVC0
*);
34 virtual bool emitInstruction(Instruction
*);
35 virtual uint32_t getMinEncodingSize(const Instruction
*) const;
37 inline void setProgramType(Program::Type pType
) { progType
= pType
; }
40 const TargetNVC0
*targ
;
42 Program::Type progType
;
45 void emitForm_A(const Instruction
*, uint64_t);
46 void emitForm_B(const Instruction
*, uint64_t);
47 void emitForm_S(const Instruction
*, uint32_t, bool pred
);
49 void emitPredicate(const Instruction
*);
51 void setAddress16(const ValueRef
&);
52 void setImmediate(const Instruction
*, const int s
); // needs op already set
53 void setImmediateS8(const ValueRef
&);
55 void emitCondCode(CondCode cc
, int pos
);
56 void emitInterpMode(const Instruction
*);
57 void emitLoadStoreType(DataType ty
);
58 void emitCachingMode(CacheMode c
);
60 void emitShortSrc2(const ValueRef
&);
62 inline uint8_t getSRegEncoding(const ValueRef
&);
64 void roundMode_A(const Instruction
*);
65 void roundMode_C(const Instruction
*);
66 void roundMode_CS(const Instruction
*);
68 void emitNegAbs12(const Instruction
*);
70 void emitNOP(const Instruction
*);
72 void emitLOAD(const Instruction
*);
73 void emitSTORE(const Instruction
*);
74 void emitMOV(const Instruction
*);
76 void emitINTERP(const Instruction
*);
77 void emitPFETCH(const Instruction
*);
78 void emitVFETCH(const Instruction
*);
79 void emitEXPORT(const Instruction
*);
80 void emitOUT(const Instruction
*);
82 void emitUADD(const Instruction
*);
83 void emitFADD(const Instruction
*);
84 void emitUMUL(const Instruction
*);
85 void emitFMUL(const Instruction
*);
86 void emitIMAD(const Instruction
*);
87 void emitFMAD(const Instruction
*);
89 void emitNOT(Instruction
*);
90 void emitLogicOp(const Instruction
*, uint8_t subOp
);
91 void emitPOPC(const Instruction
*);
92 void emitINSBF(const Instruction
*);
93 void emitShift(const Instruction
*);
95 void emitSFnOp(const Instruction
*, uint8_t subOp
);
97 void emitCVT(Instruction
*);
98 void emitMINMAX(const Instruction
*);
99 void emitPreOp(const Instruction
*);
101 void emitSET(const CmpInstruction
*);
102 void emitSLCT(const CmpInstruction
*);
103 void emitSELP(const Instruction
*);
105 void emitTEX(const TexInstruction
*);
106 void emitTEXCSAA(const TexInstruction
*);
107 void emitTXQ(const TexInstruction
*);
108 void emitPIXLD(const TexInstruction
*);
110 void emitQUADOP(const Instruction
*, uint8_t qOp
, uint8_t laneMask
);
112 void emitFlow(const Instruction
*);
114 inline void defId(const ValueDef
&, const int pos
);
115 inline void srcId(const ValueRef
&, const int pos
);
117 inline void srcAddr32(const ValueRef
&, const int pos
); // address / 4
119 inline void srcId(const ValueRef
*, const int pos
);
121 inline bool isLIMM(const ValueRef
&, DataType ty
);
124 // for better visibility
125 #define HEX64(h, l) 0x##h##l##ULL
127 #define SDATA(a) ((a).rep()->reg.data)
128 #define DDATA(a) ((a).rep()->reg.data)
130 void CodeEmitterNVC0::srcId(const ValueRef
& src
, const int pos
)
132 code
[pos
/ 32] |= (src
.get() ? SDATA(src
).id
: 63) << (pos
% 32);
135 void CodeEmitterNVC0::srcId(const ValueRef
*src
, const int pos
)
137 code
[pos
/ 32] |= (src
? SDATA(*src
).id
: 63) << (pos
% 32);
140 void CodeEmitterNVC0::srcAddr32(const ValueRef
& src
, const int pos
)
142 code
[pos
/ 32] |= (SDATA(src
).offset
>> 2) << (pos
% 32);
145 void CodeEmitterNVC0::defId(const ValueDef
& def
, const int pos
)
147 code
[pos
/ 32] |= (def
.get() ? DDATA(def
).id
: 63) << (pos
% 32);
150 bool CodeEmitterNVC0::isLIMM(const ValueRef
& ref
, DataType ty
)
152 const ImmediateValue
*imm
= ref
.get()->asImm();
154 return imm
&& (imm
->reg
.data
.u32
& ((ty
== TYPE_F32
) ? 0xfff : 0xfff00000));
158 CodeEmitterNVC0::roundMode_A(const Instruction
*insn
)
161 case ROUND_M
: code
[1] |= 1 << 23; break;
162 case ROUND_P
: code
[1] |= 2 << 23; break;
163 case ROUND_Z
: code
[1] |= 3 << 23; break;
165 assert(insn
->rnd
== ROUND_N
);
171 CodeEmitterNVC0::emitNegAbs12(const Instruction
*i
)
173 if (i
->src
[1].mod
.abs()) code
[0] |= 1 << 6;
174 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 7;
175 if (i
->src
[1].mod
.neg()) code
[0] |= 1 << 8;
176 if (i
->src
[0].mod
.neg()) code
[0] |= 1 << 9;
179 void CodeEmitterNVC0::emitCondCode(CondCode cc
, int pos
)
184 case CC_LT
: val
= 0x1; break;
185 case CC_LTU
: val
= 0x9; break;
186 case CC_EQ
: val
= 0x2; break;
187 case CC_EQU
: val
= 0xa; break;
188 case CC_LE
: val
= 0x3; break;
189 case CC_LEU
: val
= 0xb; break;
190 case CC_GT
: val
= 0x4; break;
191 case CC_GTU
: val
= 0xc; break;
192 case CC_NE
: val
= 0x5; break;
193 case CC_NEU
: val
= 0xd; break;
194 case CC_GE
: val
= 0x6; break;
195 case CC_GEU
: val
= 0xe; break;
196 case CC_TR
: val
= 0xf; break;
197 case CC_FL
: val
= 0x0; break;
199 case CC_A
: val
= 0x14; break;
200 case CC_NA
: val
= 0x13; break;
201 case CC_S
: val
= 0x15; break;
202 case CC_NS
: val
= 0x12; break;
203 case CC_C
: val
= 0x16; break;
204 case CC_NC
: val
= 0x11; break;
205 case CC_O
: val
= 0x17; break;
206 case CC_NO
: val
= 0x10; break;
210 assert(!"invalid condition code");
213 code
[pos
/ 32] |= val
<< (pos
% 32);
217 CodeEmitterNVC0::emitPredicate(const Instruction
*i
)
219 if (i
->predSrc
>= 0) {
220 assert(i
->getPredicate()->reg
.file
== FILE_PREDICATE
);
221 srcId(i
->src
[i
->predSrc
], 10);
222 if (i
->cc
== CC_NOT_P
)
223 code
[0] |= 0x2000; // negate
230 CodeEmitterNVC0::setAddress16(const ValueRef
& src
)
232 Symbol
*sym
= src
.get()->asSym();
236 code
[0] |= (sym
->reg
.data
.offset
& 0x003f) << 26;
237 code
[1] |= (sym
->reg
.data
.offset
& 0xffc0) >> 6;
241 CodeEmitterNVC0::setImmediate(const Instruction
*i
, const int s
)
243 const ImmediateValue
*imm
= i
->src
[s
].get()->asImm();
247 u32
= imm
->reg
.data
.u32
;
249 if ((code
[0] & 0xf) == 0x2) {
251 code
[0] |= (u32
& 0x3f) << 26;
254 if ((code
[0] & 0xf) == 0x3 || (code
[0] & 0xf) == 4) {
256 assert((u32
& 0xfff00000) == 0 || (u32
& 0xfff00000) == 0xfff00000);
257 assert(!(code
[1] & 0xc000));
259 code
[0] |= (u32
& 0x3f) << 26;
260 code
[1] |= 0xc000 | (u32
>> 6);
263 assert(!(u32
& 0x00000fff));
264 assert(!(code
[1] & 0xc000));
265 code
[0] |= ((u32
>> 12) & 0x3f) << 26;
266 code
[1] |= 0xc000 | (u32
>> 18);
270 void CodeEmitterNVC0::setImmediateS8(const ValueRef
&ref
)
272 const ImmediateValue
*imm
= ref
.get()->asImm();
274 int8_t s8
= static_cast<int8_t>(imm
->reg
.data
.s32
);
276 assert(s8
== imm
->reg
.data
.s32
);
278 code
[0] |= (s8
& 0x3f) << 26;
279 code
[0] |= (s8
>> 6) << 8;
283 CodeEmitterNVC0::emitForm_A(const Instruction
*i
, uint64_t opc
)
290 defId(i
->def
[0], 14);
293 if (i
->srcExists(2) && i
->getSrc(2)->reg
.file
== FILE_MEMORY_CONST
)
296 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
297 switch (i
->getSrc(s
)->reg
.file
) {
298 case FILE_MEMORY_CONST
:
299 assert(!(code
[1] & 0xc000));
300 code
[1] |= (s
== 2) ? 0x8000 : 0x4000;
301 code
[1] |= i
->getSrc(s
)->reg
.fileIndex
<< 10;
302 setAddress16(i
->src
[s
]);
306 i
->op
== OP_MOV
|| i
->op
== OP_PRESIN
|| i
->op
== OP_PREEX2
);
307 assert(!(code
[1] & 0xc000));
311 if ((s
== 2) && ((code
[0] & 0x7) == 2)) // LIMM: 3rd src == dst
313 srcId(i
->src
[s
], s
? ((s
== 2) ? 49 : s1
) : 20);
316 // ignore here, can be predicate or flags, but must not be address
323 CodeEmitterNVC0::emitForm_B(const Instruction
*i
, uint64_t opc
)
330 defId(i
->def
[0], 14);
332 switch (i
->src
[0].getFile()) {
333 case FILE_MEMORY_CONST
:
334 assert(!(code
[1] & 0xc000));
335 code
[1] |= 0x4000 | (i
->src
[0].get()->reg
.fileIndex
<< 10);
336 setAddress16(i
->src
[0]);
339 assert(!(code
[1] & 0xc000));
343 srcId(i
->src
[0], 26);
346 // ignore here, can be predicate or flags, but must not be address
352 CodeEmitterNVC0::emitForm_S(const Instruction
*i
, uint32_t opc
, bool pred
)
357 if (opc
== 0x0d || opc
== 0x0e)
360 defId(i
->def
[0], 14);
361 srcId(i
->src
[0], 20);
363 assert(pred
|| (i
->predSrc
< 0));
367 for (int s
= 1; s
< 3 && i
->srcExists(s
); ++s
) {
368 if (i
->src
[s
].get()->reg
.file
== FILE_MEMORY_CONST
) {
369 assert(!(code
[0] & (0x300 >> ss2a
)));
370 switch (i
->src
[s
].get()->reg
.fileIndex
) {
371 case 0: code
[0] |= 0x100 >> ss2a
; break;
372 case 1: code
[0] |= 0x200 >> ss2a
; break;
373 case 16: code
[0] |= 0x300 >> ss2a
; break;
375 ERROR("invalid c[] space for short form\n");
379 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 24;
381 code
[0] |= i
->getSrc(s
)->reg
.data
.offset
<< 6;
383 if (i
->src
[s
].getFile() == FILE_IMMEDIATE
) {
385 setImmediateS8(i
->src
[s
]);
387 if (i
->src
[s
].getFile() == FILE_GPR
) {
388 srcId(i
->src
[s
], (s
== 1) ? 26 : 8);
394 CodeEmitterNVC0::emitShortSrc2(const ValueRef
&src
)
396 if (src
.getFile() == FILE_MEMORY_CONST
) {
397 switch (src
.get()->reg
.fileIndex
) {
398 case 0: code
[0] |= 0x100; break;
399 case 1: code
[0] |= 0x200; break;
400 case 16: code
[0] |= 0x300; break;
402 assert(!"unsupported file index for short op");
408 assert(src
.getFile() == FILE_GPR
);
413 CodeEmitterNVC0::emitNOP(const Instruction
*i
)
415 code
[0] = 0x000001e4;
416 code
[1] = 0x40000000;
421 CodeEmitterNVC0::emitFMAD(const Instruction
*i
)
423 bool neg1
= (i
->src
[0].mod
^ i
->src
[1].mod
).neg();
425 if (i
->encSize
== 8) {
426 if (isLIMM(i
->src
[1], TYPE_F32
)) {
427 emitForm_A(i
, HEX64(20000000, 00000002));
429 emitForm_A(i
, HEX64(30000000, 00000000));
431 if (i
->src
[2].mod
.neg())
444 assert(!i
->saturate
&& !i
->src
[2].mod
.neg());
445 emitForm_S(i
, (i
->src
[2].getFile() == FILE_MEMORY_CONST
) ? 0x2e : 0x0e,
453 CodeEmitterNVC0::emitFMUL(const Instruction
*i
)
455 bool neg
= (i
->src
[0].mod
^ i
->src
[1].mod
).neg();
457 assert(i
->postFactor
>= -3 && i
->postFactor
<= 3);
459 if (i
->encSize
== 8) {
460 if (isLIMM(i
->src
[1], TYPE_F32
)) {
461 assert(i
->postFactor
== 0); // constant folded, hopefully
462 emitForm_A(i
, HEX64(30000000, 00000002));
464 emitForm_A(i
, HEX64(58000000, 00000000));
466 code
[1] |= ((i
->postFactor
> 0) ?
467 (7 - i
->postFactor
) : (0 - i
->postFactor
)) << 17;
470 code
[1] ^= 1 << 25; // aliases with LIMM sign bit
481 assert(!neg
&& !i
->saturate
&& !i
->ftz
&& !i
->postFactor
);
482 emitForm_S(i
, 0xa8, true);
487 CodeEmitterNVC0::emitUMUL(const Instruction
*i
)
489 if (i
->encSize
== 8) {
490 if (i
->src
[1].getFile() == FILE_IMMEDIATE
) {
491 emitForm_A(i
, HEX64(10000000, 00000002));
493 emitForm_A(i
, HEX64(50000000, 00000003));
495 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
497 if (i
->sType
== TYPE_S32
)
499 if (i
->dType
== TYPE_S32
)
502 emitForm_S(i
, i
->src
[1].getFile() == FILE_IMMEDIATE
? 0xaa : 0x2a, true);
504 if (i
->sType
== TYPE_S32
)
510 CodeEmitterNVC0::emitFADD(const Instruction
*i
)
512 if (i
->encSize
== 8) {
513 if (isLIMM(i
->src
[1], TYPE_F32
)) {
514 emitForm_A(i
, HEX64(28000000, 00000002));
516 assert(!i
->src
[1].mod
.neg() && !i
->src
[1].mod
.abs() && !i
->saturate
);
518 emitForm_A(i
, HEX64(50000000, 00000000));
526 if (i
->op
== OP_SUB
) code
[0] ^= 1 << 8;
531 assert(!i
->saturate
&& i
->op
!= OP_SUB
&&
532 !i
->src
[0].mod
.abs() &&
533 !i
->src
[1].mod
.neg() && !i
->src
[1].mod
.abs());
535 emitForm_S(i
, 0x49, true);
537 if (i
->src
[0].mod
.neg())
543 CodeEmitterNVC0::emitUADD(const Instruction
*i
)
547 assert(!i
->src
[0].mod
.abs() && !i
->src
[1].mod
.abs());
548 assert(!i
->src
[0].mod
.neg() || !i
->src
[1].mod
.neg());
550 if (i
->src
[0].mod
.neg())
552 if (i
->src
[1].mod
.neg())
554 if (i
->op
== OP_SUB
) {
556 assert(addOp
!= 0x300); // would be add-plus-one
559 if (i
->encSize
== 8) {
560 if (isLIMM(i
->src
[1], TYPE_U32
)) {
561 emitForm_A(i
, HEX64(08000000, 00000002));
562 if (i
->def
[1].exists())
563 code
[1] |= 1 << 26; // write carry
565 emitForm_A(i
, HEX64(48000000, 00000003));
566 if (i
->def
[1].exists())
567 code
[1] |= 1 << 16; // write carry
573 if (i
->flagsSrc
>= 0) // add carry
576 assert(!(addOp
& 0x100));
577 emitForm_S(i
, (addOp
>> 3) |
578 ((i
->src
[1].getFile() == FILE_IMMEDIATE
) ? 0xac : 0x2c), true);
584 CodeEmitterNVC0::emitIMAD(const Instruction
*i
)
586 assert(i
->encSize
== 8);
587 emitForm_A(i
, HEX64(20000000, 00000003));
589 if (isSignedType(i
->dType
))
591 if (isSignedType(i
->sType
))
594 code
[1] |= i
->saturate
<< 24;
596 if (i
->flagsDef
>= 0) code
[1] |= 1 << 16;
597 if (i
->flagsSrc
>= 0) code
[1] |= 1 << 23;
599 if (i
->src
[2].mod
.neg()) code
[0] |= 0x10;
600 if (i
->src
[1].mod
.neg() ^
601 i
->src
[0].mod
.neg()) code
[0] |= 0x20;
603 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
)
608 CodeEmitterNVC0::emitNOT(Instruction
*i
)
610 assert(i
->encSize
== 8);
611 i
->src
[1].set(i
->src
[0]);
612 emitForm_A(i
, HEX64(68000000, 000001c3
));
616 CodeEmitterNVC0::emitLogicOp(const Instruction
*i
, uint8_t subOp
)
618 if (i
->encSize
== 8) {
619 if (isLIMM(i
->src
[1], TYPE_U32
)) {
620 emitForm_A(i
, HEX64(38000000, 00000002));
622 if (i
->src
[2].exists())
625 emitForm_A(i
, HEX64(68000000, 00000003));
627 if (i
->src
[2].exists())
630 code
[0] |= subOp
<< 6;
632 if (i
->src
[2].exists()) // carry
635 if (i
->src
[0].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
636 if (i
->src
[1].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
638 emitForm_S(i
, (subOp
<< 5) |
639 ((i
->src
[1].getFile() == FILE_IMMEDIATE
) ? 0x1d : 0x8d), true);
644 CodeEmitterNVC0::emitPOPC(const Instruction
*i
)
646 emitForm_A(i
, HEX64(54000000, 00000004));
648 if (i
->src
[0].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 9;
649 if (i
->src
[1].mod
& Modifier(NV50_IR_MOD_NOT
)) code
[0] |= 1 << 8;
653 CodeEmitterNVC0::emitINSBF(const Instruction
*i
)
655 emitForm_A(i
, HEX64(28000000, 30000000));
659 CodeEmitterNVC0::emitShift(const Instruction
*i
)
661 if (i
->op
== OP_SHR
) {
662 emitForm_A(i
, HEX64(58000000, 00000003)
663 | (isSignedType(i
->dType
) ? 0x20 : 0x00));
665 emitForm_A(i
, HEX64(60000000, 00000003));
669 code
[0] |= 1 << 9; // clamp shift amount
673 CodeEmitterNVC0::emitPreOp(const Instruction
*i
)
675 if (i
->encSize
== 8) {
676 emitForm_B(i
, HEX64(60000000, 00000000));
678 if (i
->op
== OP_PREEX2
)
681 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 6;
682 if (i
->src
[0].mod
.neg()) code
[0] |= 1 << 8;
684 emitForm_S(i
, i
->op
== OP_PREEX2
? 0x74000008 : 0x70000008, true);
689 CodeEmitterNVC0::emitSFnOp(const Instruction
*i
, uint8_t subOp
)
691 if (i
->encSize
== 8) {
692 code
[0] = 0x00000000 | (subOp
<< 26);
693 code
[1] = 0xc8000000;
697 defId(i
->def
[0], 14);
698 srcId(i
->src
[0], 20);
700 assert(i
->src
[0].getFile() == FILE_GPR
);
702 if (i
->saturate
) code
[0] |= 1 << 5;
704 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 7;
705 if (i
->src
[0].mod
.neg()) code
[0] |= 1 << 9;
707 emitForm_S(i
, 0x80000008 | (subOp
<< 26), true);
709 assert(!i
->src
[0].mod
.neg());
710 if (i
->src
[0].mod
.abs()) code
[0] |= 1 << 30;
715 CodeEmitterNVC0::emitMINMAX(const Instruction
*i
)
719 assert(i
->encSize
== 8);
721 op
= (i
->op
== OP_MIN
) ? 0x080e000000000000ULL
: 0x081e000000000000ULL
;
726 if (!isFloatType(i
->dType
))
727 op
|= isSignedType(i
->dType
) ? 0x23 : 0x03;
734 CodeEmitterNVC0::roundMode_C(const Instruction
*i
)
737 case ROUND_M
: code
[1] |= 1 << 17; break;
738 case ROUND_P
: code
[1] |= 2 << 17; break;
739 case ROUND_Z
: code
[1] |= 3 << 17; break;
740 case ROUND_NI
: code
[0] |= 1 << 7; break;
741 case ROUND_MI
: code
[0] |= 1 << 7; code
[1] |= 1 << 17; break;
742 case ROUND_PI
: code
[0] |= 1 << 7; code
[1] |= 2 << 17; break;
743 case ROUND_ZI
: code
[0] |= 1 << 7; code
[1] |= 3 << 17; break;
746 assert(!"invalid round mode");
752 CodeEmitterNVC0::roundMode_CS(const Instruction
*i
)
756 case ROUND_MI
: code
[0] |= 1 << 16; break;
758 case ROUND_PI
: code
[0] |= 2 << 16; break;
760 case ROUND_ZI
: code
[0] |= 3 << 16; break;
767 CodeEmitterNVC0::emitCVT(Instruction
*i
)
769 const bool f2f
= isFloatType(i
->dType
) && isFloatType(i
->sType
);
772 case OP_CEIL
: i
->rnd
= f2f
? ROUND_PI
: ROUND_P
; break;
773 case OP_FLOOR
: i
->rnd
= f2f
? ROUND_MI
: ROUND_M
; break;
774 case OP_TRUNC
: i
->rnd
= f2f
? ROUND_ZI
: ROUND_Z
; break;
779 const bool sat
= (i
->op
== OP_SAT
) || i
->saturate
;
780 const bool abs
= (i
->op
== OP_ABS
) || i
->src
[0].mod
.abs();
781 const bool neg
= (i
->op
== OP_NEG
) || i
->src
[0].mod
.neg();
783 if (i
->encSize
== 8) {
784 emitForm_B(i
, HEX64(10000000, 00000004));
788 code
[0] |= util_logbase2(i
->def
[0].getSize()) << 20;
789 code
[0] |= util_logbase2(i
->src
[0].getSize()) << 23;
795 if (neg
&& i
->op
!= OP_ABS
)
801 if (isSignedIntType(i
->dType
))
803 if (isSignedIntType(i
->sType
))
806 if (isFloatType(i
->dType
)) {
807 if (!isFloatType(i
->sType
))
808 code
[1] |= 0x08000000;
810 if (isFloatType(i
->sType
))
811 code
[1] |= 0x04000000;
813 code
[1] |= 0x0c000000;
816 if (i
->op
== OP_CEIL
|| i
->op
== OP_FLOOR
|| i
->op
== OP_TRUNC
) {
819 if (isFloatType(i
->dType
)) {
820 if (isFloatType(i
->sType
))
823 code
[0] = 0x088 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
825 assert(isFloatType(i
->sType
));
827 code
[0] = 0x288 | (isSignedType(i
->sType
) ? (1 << 8) : 0);
830 if (neg
) code
[0] |= 1 << 16;
831 if (sat
) code
[0] |= 1 << 18;
832 if (abs
) code
[0] |= 1 << 19;
839 CodeEmitterNVC0::emitSET(const CmpInstruction
*i
)
844 if (i
->sType
== TYPE_F64
)
847 if (!isFloatType(i
->sType
))
850 if (isFloatType(i
->dType
) || isSignedIntType(i
->sType
))
854 case OP_SET_AND
: hi
= 0x10000000; break;
855 case OP_SET_OR
: hi
= 0x10200000; break;
856 case OP_SET_XOR
: hi
= 0x10400000; break;
861 emitForm_A(i
, (static_cast<uint64_t>(hi
) << 32) | lo
);
863 if (i
->def
[0].getFile() == FILE_PREDICATE
) {
864 if (i
->sType
== TYPE_F32
)
865 code
[1] += 0x10000000;
867 code
[1] += 0x08000000;
870 defId(i
->def
[0], 17);
872 defId(i
->def
[1], 14);
880 emitCondCode(i
->setCond
, 32 + 23);
885 CodeEmitterNVC0::emitSLCT(const CmpInstruction
*i
)
891 op
= HEX64(30000000, 00000023);
894 op
= HEX64(30000000, 00000003);
897 op
= HEX64(38000000, 00000000);
900 assert(!"invalid type for SLCT");
906 CondCode cc
= i
->setCond
;
908 if (i
->src
[2].mod
.neg())
909 cc
= reverseCondCode(cc
);
911 emitCondCode(cc
, 32 + 23);
917 void CodeEmitterNVC0::emitSELP(const Instruction
*i
)
919 emitForm_A(i
, HEX64(20000000, 00000004));
921 if (i
->cc
== CC_NOT_P
|| i
->src
[2].mod
& Modifier(NV50_IR_MOD_NOT
))
925 void CodeEmitterNVC0::emitTEXCSAA(const TexInstruction
*i
)
927 code
[0] = 0x00000086;
928 code
[1] = 0xd0000000;
931 code
[1] |= i
->tex
.s
<< 8;
936 defId(i
->def
[0], 14);
937 srcId(i
->src
[0], 20);
941 CodeEmitterNVC0::emitTEX(const TexInstruction
*i
)
943 code
[0] = 0x00000006;
946 code
[0] |= 0x80; // normal/t/p mode = t, XXX: what is this ?
952 case OP_TEX
: code
[1] = 0x80000000; break;
953 case OP_TXB
: code
[1] = 0x84000000; break;
954 case OP_TXL
: code
[1] = 0x86000000; break;
955 case OP_TXF
: code
[1] = 0x92000000; break;
956 case OP_TXG
: code
[1] = 0xa0000000; break;
957 case OP_TXD
: code
[1] = 0xe0000000; break;
959 assert(!"invalid texture op");
962 defId(i
->def
[0], 14);
963 srcId(i
->src
[0], 20);
967 if (i
->op
== OP_TXG
) code
[0] |= i
->tex
.gatherComp
<< 5;
969 code
[1] |= i
->tex
.mask
<< 14;
972 code
[1] |= i
->tex
.s
<< 8;
973 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0)
974 code
[1] |= 1 << 18; // in 1st source (with array index)
977 code
[1] |= (i
->tex
.target
.getDim() - 1) << 20;
978 if (i
->tex
.target
.isCube())
980 if (i
->tex
.target
.isArray())
982 if (i
->tex
.target
.isShadow())
985 int src1
= i
->tex
.target
.getArgCount();
987 if (i
->src
[src1
].getFile() == FILE_IMMEDIATE
) { // lzero
989 code
[1] &= ~(1 << 26);
992 code
[1] &= ~(1 << 25);
994 if (i
->tex
.target
== TEX_TARGET_2D_MS
||
995 i
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
998 if (i
->tex
.useOffsets
) // in vecSrc0.w
1001 srcId(i
->src
[src1
], 26);
1005 CodeEmitterNVC0::emitTXQ(const TexInstruction
*i
)
1007 code
[0] = 0x00000086;
1008 code
[1] = 0xc0000000;
1010 switch (i
->tex
.query
) {
1011 case TXQ_DIMS
: code
[1] |= 0 << 22; break;
1012 case TXQ_TYPE
: code
[1] |= 1 << 22; break;
1013 case TXQ_SAMPLE_POSITION
: code
[1] |= 2 << 22; break;
1014 case TXQ_FILTER
: code
[1] |= 3 << 22; break;
1015 case TXQ_LOD
: code
[1] |= 4 << 22; break;
1016 case TXQ_BORDER_COLOUR
: code
[1] |= 5 << 22; break;
1018 assert(!"invalid texture query");
1022 code
[1] |= i
->tex
.mask
<< 14;
1024 code
[1] |= i
->tex
.r
;
1025 code
[1] |= i
->tex
.s
<< 8;
1026 if (i
->tex
.sIndirectSrc
>= 0 || i
->tex
.rIndirectSrc
>= 0)
1029 defId(i
->def
[0], 14);
1030 srcId(i
->src
[0], 20);
1031 srcId(i
->src
[1], 26);
1037 CodeEmitterNVC0::emitQUADOP(const Instruction
*i
, uint8_t qOp
, uint8_t laneMask
)
1039 code
[0] = 0x00000000 | (laneMask
<< 6);
1040 code
[1] = 0x48000000 | qOp
;
1042 defId(i
->def
[0], 14);
1043 srcId(i
->src
[0], 20);
1044 srcId(i
->srcExists(1) ? i
->src
[1] : i
->src
[0], 26);
1050 CodeEmitterNVC0::emitFlow(const Instruction
*i
)
1052 const FlowInstruction
*f
= i
->asFlow();
1054 unsigned mask
; // bit 0: predicate, bit 1: target
1056 code
[0] = 0x00000007;
1060 code
[1] = f
->absolute
? 0x00000000 : 0x40000000;
1061 if (i
->src
[0].getFile() == FILE_MEMORY_CONST
||
1062 i
->src
[1].getFile() == FILE_MEMORY_CONST
)
1067 code
[1] = f
->absolute
? 0x10000000 : 0x50000000;
1068 if (i
->src
[0].getFile() == FILE_MEMORY_CONST
)
1073 case OP_EXIT
: code
[1] = 0x80000000; mask
= 1; break;
1074 case OP_RET
: code
[1] = 0x90000000; mask
= 1; break;
1075 case OP_DISCARD
: code
[1] = 0x98000000; mask
= 1; break;
1076 case OP_BREAK
: code
[1] = 0xa8000000; mask
= 1; break;
1077 case OP_CONT
: code
[1] = 0xb0000000; mask
= 1; break;
1079 case OP_JOINAT
: code
[1] = 0x60000000; mask
= 2; break;
1080 case OP_PREBREAK
: code
[1] = 0x68000000; mask
= 2; break;
1081 case OP_PRECONT
: code
[1] = 0x70000000; mask
= 2; break;
1082 case OP_PRERET
: code
[1] = 0x78000000; mask
= 2; break;
1084 case OP_QUADON
: code
[1] = 0xc0000000; mask
= 0; break;
1085 case OP_QUADPOP
: code
[1] = 0xc8000000; mask
= 0; break;
1086 case OP_BRKPT
: code
[1] = 0xd0000000; mask
= 0; break;
1088 assert(!"invalid flow operation");
1094 if (i
->flagsSrc
< 0)
1106 if (f
->op
== OP_CALL
) {
1108 assert(f
->absolute
);
1109 uint32_t pcAbs
= targ
->getBuiltinOffset(f
->target
.builtin
);
1110 addReloc(RelocEntry::TYPE_BUILTIN
, 0, pcAbs
, 0xfc000000, 26);
1111 addReloc(RelocEntry::TYPE_BUILTIN
, 1, pcAbs
, 0x03ffffff, -6);
1113 assert(!f
->absolute
);
1114 int32_t pcRel
= f
->target
.fn
->binPos
- (codeSize
+ 8);
1115 code
[0] |= (pcRel
& 0x3f) << 26;
1116 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1120 int32_t pcRel
= f
->target
.bb
->binPos
- (codeSize
+ 8);
1121 // currently we don't want absolute branches
1122 assert(!f
->absolute
);
1123 code
[0] |= (pcRel
& 0x3f) << 26;
1124 code
[1] |= (pcRel
>> 6) & 0x3ffff;
1129 CodeEmitterNVC0::emitPFETCH(const Instruction
*i
)
1131 uint32_t prim
= i
->src
[0].get()->reg
.data
.u32
;
1133 code
[0] = 0x00000006 | ((prim
& 0x3f) << 26);
1134 code
[1] = 0x00000000 | (prim
>> 6);
1138 defId(i
->def
[0], 14);
1139 srcId(i
->src
[1], 20);
1143 CodeEmitterNVC0::emitVFETCH(const Instruction
*i
)
1145 code
[0] = 0x00000006;
1146 code
[1] = 0x06000000 | i
->src
[0].get()->reg
.data
.offset
;
1150 if (i
->getSrc(0)->reg
.file
== FILE_SHADER_OUTPUT
)
1151 code
[0] |= 0x200; // yes, TCPs can read from *outputs* of other threads
1155 code
[0] |= (i
->defCount(0xf) - 1) << 5;
1157 defId(i
->def
[0], 14);
1158 srcId(i
->src
[0].getIndirect(0), 20);
1159 srcId(i
->src
[0].getIndirect(1), 26); // vertex address
1163 CodeEmitterNVC0::emitEXPORT(const Instruction
*i
)
1165 unsigned int size
= typeSizeof(i
->dType
);
1167 code
[0] = 0x00000006 | ((size
/ 4 - 1) << 5);
1168 code
[1] = 0x0a000000 | i
->src
[0].get()->reg
.data
.offset
;
1170 assert(size
!= 12 && !(code
[1] & (size
- 1)));
1177 assert(i
->src
[1].getFile() == FILE_GPR
);
1179 srcId(i
->src
[0].getIndirect(0), 20);
1180 srcId(i
->src
[0].getIndirect(1), 32 + 17); // vertex base address
1181 srcId(i
->src
[1], 26);
1185 CodeEmitterNVC0::emitOUT(const Instruction
*i
)
1187 code
[0] = 0x00000006;
1188 code
[1] = 0x1c000000;
1192 defId(i
->def
[0], 14); // new secret address
1193 srcId(i
->src
[0], 20); // old secret address, should be 0 initially
1195 assert(i
->src
[0].getFile() == FILE_GPR
);
1197 if (i
->op
== OP_EMIT
)
1199 if (i
->op
== OP_RESTART
|| i
->subOp
== NV50_IR_SUBOP_EMIT_RESTART
)
1203 if (i
->src
[1].getFile() == FILE_IMMEDIATE
) {
1205 code
[0] |= SDATA(i
->src
[1]).u32
<< 26;
1207 srcId(i
->src
[1], 26);
1212 CodeEmitterNVC0::emitInterpMode(const Instruction
*i
)
1214 if (i
->encSize
== 8) {
1215 code
[0] |= i
->ipa
<< 6; // TODO: INTERP_SAMPLEID
1217 if (i
->getInterpMode() == NV50_IR_INTERP_SC
)
1219 assert(i
->op
== OP_PINTERP
&& i
->getSampleMode() == 0);
1224 CodeEmitterNVC0::emitINTERP(const Instruction
*i
)
1226 const uint32_t base
= i
->getSrc(0)->reg
.data
.offset
;
1228 if (i
->encSize
== 8) {
1229 code
[0] = 0x00000000;
1230 code
[1] = 0xc0000000 | (base
& 0xffff);
1235 if (i
->op
== OP_PINTERP
)
1236 srcId(i
->src
[1], 26);
1238 code
[0] |= 0x3f << 26;
1240 srcId(i
->src
[0].getIndirect(0), 20);
1242 assert(i
->op
== OP_PINTERP
);
1243 code
[0] = 0x00000009 | ((base
& 0xc) << 6) | ((base
>> 4) << 26);
1244 srcId(i
->src
[1], 20);
1249 defId(i
->def
[0], 14);
1251 if (i
->getSampleMode() == NV50_IR_INTERP_OFFSET
)
1252 srcId(i
->src
[i
->op
== OP_PINTERP
? 2 : 1], 17);
1254 code
[1] |= 0x3f << 17;
1258 CodeEmitterNVC0::emitLoadStoreType(DataType ty
)
1291 assert(!"invalid type");
1298 CodeEmitterNVC0::emitCachingMode(CacheMode c
)
1319 assert(!"invalid caching mode");
1326 CodeEmitterNVC0::emitSTORE(const Instruction
*i
)
1330 switch (i
->src
[0].getFile()) {
1331 case FILE_MEMORY_GLOBAL
: opc
= 0x90000000; break;
1332 case FILE_MEMORY_LOCAL
: opc
= 0xc8000000; break;
1333 case FILE_MEMORY_SHARED
: opc
= 0xc9000000; break;
1335 assert(!"invalid memory file");
1339 code
[0] = 0x00000005;
1342 setAddress16(i
->src
[0]);
1343 srcId(i
->src
[1], 14);
1344 srcId(i
->src
[0].getIndirect(0), 20);
1348 emitLoadStoreType(i
->dType
);
1349 emitCachingMode(i
->cache
);
1353 CodeEmitterNVC0::emitLOAD(const Instruction
*i
)
1357 code
[0] = 0x00000005;
1359 switch (i
->src
[0].getFile()) {
1360 case FILE_MEMORY_GLOBAL
: opc
= 0x80000000; break;
1361 case FILE_MEMORY_LOCAL
: opc
= 0xc0000000; break;
1362 case FILE_MEMORY_SHARED
: opc
= 0xc1000000; break;
1363 case FILE_MEMORY_CONST
:
1364 if (!i
->src
[0].isIndirect(0) && typeSizeof(i
->dType
) == 4) {
1365 emitMOV(i
); // not sure if this is any better
1368 opc
= 0x14000000 | (i
->src
[0].get()->reg
.fileIndex
<< 10);
1369 code
[0] = 0x00000006 | (i
->subOp
<< 8);
1372 assert(!"invalid memory file");
1378 defId(i
->def
[0], 14);
1380 setAddress16(i
->src
[0]);
1381 srcId(i
->src
[0].getIndirect(0), 20);
1385 emitLoadStoreType(i
->dType
);
1386 emitCachingMode(i
->cache
);
1390 CodeEmitterNVC0::getSRegEncoding(const ValueRef
& ref
)
1392 switch (SDATA(ref
).sv
.sv
) {
1393 case SV_LANEID
: return 0x00;
1394 case SV_PHYSID
: return 0x03;
1395 case SV_VERTEX_COUNT
: return 0x10;
1396 case SV_INVOCATION_ID
: return 0x11;
1397 case SV_YDIR
: return 0x12;
1398 case SV_TID
: return 0x21 + SDATA(ref
).sv
.index
;
1399 case SV_CTAID
: return 0x25 + SDATA(ref
).sv
.index
;
1400 case SV_NTID
: return 0x29 + SDATA(ref
).sv
.index
;
1401 case SV_GRIDID
: return 0x2c;
1402 case SV_NCTAID
: return 0x2d + SDATA(ref
).sv
.index
;
1403 case SV_LBASE
: return 0x34;
1404 case SV_SBASE
: return 0x30;
1405 case SV_CLOCK
: return 0x50 + SDATA(ref
).sv
.index
;
1407 assert(!"no sreg for system value");
1413 CodeEmitterNVC0::emitMOV(const Instruction
*i
)
1415 if (i
->src
[0].getFile() == FILE_SYSTEM_VALUE
) {
1416 uint8_t sr
= getSRegEncoding(i
->src
[0]);
1418 if (i
->encSize
== 8) {
1419 code
[0] = 0x00000004 | (sr
<< 26);
1420 code
[1] = 0x2c000000;
1422 code
[0] = 0x40000008 | (sr
<< 20);
1424 defId(i
->def
[0], 14);
1428 if (i
->encSize
== 8) {
1431 if (i
->src
[0].getFile() == FILE_IMMEDIATE
)
1432 opc
= HEX64(18000000, 000001e2
);
1434 if (i
->src
[0].getFile() == FILE_PREDICATE
)
1435 opc
= HEX64(080e0000
, 1c000004
);
1437 opc
= HEX64(28000000, 00000004);
1439 opc
|= i
->lanes
<< 5;
1445 if (i
->src
[0].getFile() == FILE_IMMEDIATE
) {
1446 imm
= SDATA(i
->src
[0]).u32
;
1447 if (imm
& 0xfff00000) {
1448 assert(!(imm
& 0x000fffff));
1449 code
[0] = 0x00000318 | imm
;
1451 assert(imm
< 0x800 || ((int32_t)imm
>= -0x800));
1452 code
[0] = 0x00000118 | (imm
<< 20);
1456 emitShortSrc2(i
->src
[0]);
1458 defId(i
->def
[0], 14);
1465 CodeEmitterNVC0::emitInstruction(Instruction
*insn
)
1467 if (!insn
->encSize
) {
1468 ERROR("skipping unencodable instruction: "); insn
->print();
1471 if (codeSize
+ insn
->encSize
> codeSizeLimit
) {
1472 ERROR("code emitter output buffer too small\n");
1476 // assert that instructions with multiple defs don't corrupt registers
1477 for (int d
= 0; insn
->defExists(d
); ++d
)
1478 assert(insn
->asTex() || insn
->def
[d
].rep()->reg
.data
.id
>= 0);
1512 if (isFloatType(insn
->dType
))
1518 if (isFloatType(insn
->dType
))
1525 if (isFloatType(insn
->dType
))
1534 emitLogicOp(insn
, 0);
1537 emitLogicOp(insn
, 1);
1540 emitLogicOp(insn
, 2);
1550 emitSET(insn
->asCmp());
1556 emitSLCT(insn
->asCmp());
1598 emitTEX(insn
->asTex());
1601 emitTXQ(insn
->asTex());
1620 emitQUADOP(insn
, insn
->subOp
, insn
->lanes
);
1623 emitQUADOP(insn
, insn
->src
[0].mod
.neg() ? 0x66 : 0x99, 0x4);
1626 emitQUADOP(insn
, insn
->src
[0].mod
.neg() ? 0x5a : 0xa5, 0x5);
1638 ERROR("operation should have been eliminated");
1644 ERROR("operation should have been lowered\n");
1647 ERROR("unknow op\n");
1653 assert(insn
->encSize
== 8);
1656 code
+= insn
->encSize
/ 4;
1657 codeSize
+= insn
->encSize
;
1662 CodeEmitterNVC0::getMinEncodingSize(const Instruction
*i
) const
1664 const Target::OpInfo
&info
= targ
->getOpInfo(i
);
1666 if (info
.minEncSize
== 8 || 1)
1669 if (i
->ftz
|| i
->saturate
|| i
->join
)
1671 if (i
->rnd
!= ROUND_N
)
1673 if (i
->predSrc
>= 0 && i
->op
== OP_MAD
)
1676 if (i
->op
== OP_PINTERP
) {
1677 if (i
->getSampleMode() || 1) // XXX: grr, short op doesn't work
1680 if (i
->op
== OP_MOV
&& i
->lanes
!= 0xf) {
1684 for (int s
= 0; i
->srcExists(s
); ++s
) {
1685 if (i
->src
[s
].isIndirect(0))
1688 if (i
->src
[s
].getFile() == FILE_MEMORY_CONST
) {
1689 if (SDATA(i
->src
[s
]).offset
>= 0x100)
1691 if (i
->getSrc(s
)->reg
.fileIndex
> 1 &&
1692 i
->getSrc(s
)->reg
.fileIndex
!= 16)
1695 if (i
->src
[s
].getFile() == FILE_IMMEDIATE
) {
1696 if (i
->dType
== TYPE_F32
) {
1697 if (SDATA(i
->src
[s
]).u32
>= 0x100)
1700 if (SDATA(i
->src
[s
]).u32
> 0xff)
1705 if (i
->op
== OP_CVT
)
1707 if (i
->src
[s
].mod
!= Modifier(0)) {
1708 if (i
->src
[s
].mod
== Modifier(NV50_IR_MOD_ABS
))
1709 if (i
->op
!= OP_RSQ
)
1711 if (i
->src
[s
].mod
== Modifier(NV50_IR_MOD_NEG
))
1712 if (i
->op
!= OP_ADD
|| s
!= 0)
1720 CodeEmitterNVC0::CodeEmitterNVC0(const TargetNVC0
*target
) : targ(target
)
1723 codeSize
= codeSizeLimit
= 0;
1728 TargetNVC0::getCodeEmitter(Program::Type type
)
1730 CodeEmitterNVC0
*emit
= new CodeEmitterNVC0(this);
1731 emit
->setProgramType(type
);
1735 } // namespace nv50_ir