OSDN Git Service

nv50/ir: fix emission of 8-byte wide interp instruction
[android-x86/external-mesa.git] / src / gallium / drivers / nouveau / codegen / nv50_ir_emit_nv50.cpp
1 /*
2  * Copyright 2011 Christoph Bumiller
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  */
22
23 #include "codegen/nv50_ir.h"
24 #include "codegen/nv50_ir_target_nv50.h"
25
26 namespace nv50_ir {
27
28 #define NV50_OP_ENC_LONG     0
29 #define NV50_OP_ENC_SHORT    1
30 #define NV50_OP_ENC_IMM      2
31 #define NV50_OP_ENC_LONG_ALT 3
32
33 class CodeEmitterNV50 : public CodeEmitter
34 {
35 public:
36    CodeEmitterNV50(const TargetNV50 *);
37
38    virtual bool emitInstruction(Instruction *);
39
40    virtual uint32_t getMinEncodingSize(const Instruction *) const;
41
42    inline void setProgramType(Program::Type pType) { progType = pType; }
43
44    virtual void prepareEmission(Function *);
45
46 private:
47    Program::Type progType;
48
49    const TargetNV50 *targNV50;
50
51 private:
52    inline void defId(const ValueDef&, const int pos);
53    inline void srcId(const ValueRef&, const int pos);
54    inline void srcId(const ValueRef *, const int pos);
55
56    inline void srcAddr16(const ValueRef&, bool adj, const int pos);
57    inline void srcAddr8(const ValueRef&, const int pos);
58
59    void emitFlagsRd(const Instruction *);
60    void emitFlagsWr(const Instruction *);
61
62    void emitCondCode(CondCode cc, DataType ty, int pos);
63
64    inline void setARegBits(unsigned int);
65
66    void setAReg16(const Instruction *, int s);
67    void setImmediate(const Instruction *, int s);
68
69    void setDst(const Value *);
70    void setDst(const Instruction *, int d);
71    void setSrcFileBits(const Instruction *, int enc);
72    void setSrc(const Instruction *, unsigned int s, int slot);
73
74    void emitForm_MAD(const Instruction *);
75    void emitForm_ADD(const Instruction *);
76    void emitForm_MUL(const Instruction *);
77    void emitForm_IMM(const Instruction *);
78
79    void emitLoadStoreSizeLG(DataType ty, int pos);
80    void emitLoadStoreSizeCS(DataType ty);
81
82    void roundMode_MAD(const Instruction *);
83    void roundMode_CVT(RoundMode);
84
85    void emitMNeg12(const Instruction *);
86
87    void emitLOAD(const Instruction *);
88    void emitSTORE(const Instruction *);
89    void emitMOV(const Instruction *);
90    void emitRDSV(const Instruction *);
91    void emitNOP();
92    void emitINTERP(const Instruction *);
93    void emitPFETCH(const Instruction *);
94    void emitOUT(const Instruction *);
95
96    void emitUADD(const Instruction *);
97    void emitAADD(const Instruction *);
98    void emitFADD(const Instruction *);
99    void emitIMUL(const Instruction *);
100    void emitFMUL(const Instruction *);
101    void emitFMAD(const Instruction *);
102    void emitIMAD(const Instruction *);
103    void emitISAD(const Instruction *);
104
105    void emitMINMAX(const Instruction *);
106
107    void emitPreOp(const Instruction *);
108    void emitSFnOp(const Instruction *, uint8_t subOp);
109
110    void emitShift(const Instruction *);
111    void emitARL(const Instruction *, unsigned int shl);
112    void emitLogicOp(const Instruction *);
113    void emitNOT(const Instruction *);
114
115    void emitCVT(const Instruction *);
116    void emitSET(const Instruction *);
117
118    void emitTEX(const TexInstruction *);
119    void emitTXQ(const TexInstruction *);
120    void emitTEXPREP(const TexInstruction *);
121
122    void emitQUADOP(const Instruction *, uint8_t lane, uint8_t quOp);
123
124    void emitFlow(const Instruction *, uint8_t flowOp);
125    void emitPRERETEmu(const FlowInstruction *);
126    void emitBAR(const Instruction *);
127
128    void emitATOM(const Instruction *);
129 };
130
131 #define SDATA(a) ((a).rep()->reg.data)
132 #define DDATA(a) ((a).rep()->reg.data)
133
134 void CodeEmitterNV50::srcId(const ValueRef& src, const int pos)
135 {
136    assert(src.get());
137    code[pos / 32] |= SDATA(src).id << (pos % 32);
138 }
139
140 void CodeEmitterNV50::srcId(const ValueRef *src, const int pos)
141 {
142    assert(src->get());
143    code[pos / 32] |= SDATA(*src).id << (pos % 32);
144 }
145
146 void CodeEmitterNV50::srcAddr16(const ValueRef& src, bool adj, const int pos)
147 {
148    assert(src.get());
149
150    int32_t offset = SDATA(src).offset;
151
152    assert(!adj || src.get()->reg.size <= 4);
153    if (adj)
154       offset /= src.get()->reg.size;
155
156    assert(offset <= 0x7fff && offset >= (int32_t)-0x8000 && (pos % 32) <= 16);
157
158    if (offset < 0)
159       offset &= adj ? (0xffff >> (src.get()->reg.size >> 1)) : 0xffff;
160
161    code[pos / 32] |= offset << (pos % 32);
162 }
163
164 void CodeEmitterNV50::srcAddr8(const ValueRef& src, const int pos)
165 {
166    assert(src.get());
167
168    uint32_t offset = SDATA(src).offset;
169
170    assert((offset <= 0x1fc || offset == 0x3fc) && !(offset & 0x3));
171
172    code[pos / 32] |= (offset >> 2) << (pos % 32);
173 }
174
175 void CodeEmitterNV50::defId(const ValueDef& def, const int pos)
176 {
177    assert(def.get() && def.getFile() != FILE_SHADER_OUTPUT);
178
179    code[pos / 32] |= DDATA(def).id << (pos % 32);
180 }
181
182 void
183 CodeEmitterNV50::roundMode_MAD(const Instruction *insn)
184 {
185    switch (insn->rnd) {
186    case ROUND_M: code[1] |= 1 << 22; break;
187    case ROUND_P: code[1] |= 2 << 22; break;
188    case ROUND_Z: code[1] |= 3 << 22; break;
189    default:
190       assert(insn->rnd == ROUND_N);
191       break;
192    }
193 }
194
195 void
196 CodeEmitterNV50::emitMNeg12(const Instruction *i)
197 {
198    code[1] |= i->src(0).mod.neg() << 26;
199    code[1] |= i->src(1).mod.neg() << 27;
200 }
201
202 void CodeEmitterNV50::emitCondCode(CondCode cc, DataType ty, int pos)
203 {
204    uint8_t enc;
205
206    assert(pos >= 32 || pos <= 27);
207
208    switch (cc) {
209    case CC_LT:  enc = 0x1; break;
210    case CC_LTU: enc = 0x9; break;
211    case CC_EQ:  enc = 0x2; break;
212    case CC_EQU: enc = 0xa; break;
213    case CC_LE:  enc = 0x3; break;
214    case CC_LEU: enc = 0xb; break;
215    case CC_GT:  enc = 0x4; break;
216    case CC_GTU: enc = 0xc; break;
217    case CC_NE:  enc = 0x5; break;
218    case CC_NEU: enc = 0xd; break;
219    case CC_GE:  enc = 0x6; break;
220    case CC_GEU: enc = 0xe; break;
221    case CC_TR:  enc = 0xf; break;
222    case CC_FL:  enc = 0x0; break;
223
224    case CC_O:  enc = 0x10; break;
225    case CC_C:  enc = 0x11; break;
226    case CC_A:  enc = 0x12; break;
227    case CC_S:  enc = 0x13; break;
228    case CC_NS: enc = 0x1c; break;
229    case CC_NA: enc = 0x1d; break;
230    case CC_NC: enc = 0x1e; break;
231    case CC_NO: enc = 0x1f; break;
232
233    default:
234       enc = 0;
235       assert(!"invalid condition code");
236       break;
237    }
238    if (ty != TYPE_NONE && !isFloatType(ty))
239       enc &= ~0x8; // unordered only exists for float types
240
241    code[pos / 32] |= enc << (pos % 32);
242 }
243
244 void
245 CodeEmitterNV50::emitFlagsRd(const Instruction *i)
246 {
247    int s = (i->flagsSrc >= 0) ? i->flagsSrc : i->predSrc;
248
249    assert(!(code[1] & 0x00003f80));
250
251    if (s >= 0) {
252       assert(i->getSrc(s)->reg.file == FILE_FLAGS);
253       emitCondCode(i->cc, TYPE_NONE, 32 + 7);
254       srcId(i->src(s), 32 + 12);
255    } else {
256       code[1] |= 0x0780;
257    }
258 }
259
260 void
261 CodeEmitterNV50::emitFlagsWr(const Instruction *i)
262 {
263    assert(!(code[1] & 0x70));
264
265    int flagsDef = i->flagsDef;
266
267    // find flags definition and check that it is the last def
268    if (flagsDef < 0) {
269       for (int d = 0; i->defExists(d); ++d)
270          if (i->def(d).getFile() == FILE_FLAGS)
271             flagsDef = d;
272       if (flagsDef >= 0 && 0) // TODO: enforce use of flagsDef at some point
273          WARN("Instruction::flagsDef was not set properly\n");
274    }
275    if (flagsDef == 0 && i->defExists(1))
276       WARN("flags def should not be the primary definition\n");
277
278    if (flagsDef >= 0)
279       code[1] |= (DDATA(i->def(flagsDef)).id << 4) | 0x40;
280
281 }
282
283 void
284 CodeEmitterNV50::setARegBits(unsigned int u)
285 {
286    code[0] |= (u & 3) << 26;
287    code[1] |= (u & 4);
288 }
289
290 void
291 CodeEmitterNV50::setAReg16(const Instruction *i, int s)
292 {
293    if (i->srcExists(s)) {
294       s = i->src(s).indirect[0];
295       if (s >= 0)
296          setARegBits(SDATA(i->src(s)).id + 1);
297    }
298 }
299
300 void
301 CodeEmitterNV50::setImmediate(const Instruction *i, int s)
302 {
303    const ImmediateValue *imm = i->src(s).get()->asImm();
304    assert(imm);
305
306    uint32_t u = imm->reg.data.u32;
307
308    if (i->src(s).mod & Modifier(NV50_IR_MOD_NOT))
309       u = ~u;
310
311    code[1] |= 3;
312    code[0] |= (u & 0x3f) << 16;
313    code[1] |= (u >> 6) << 2;
314 }
315
316 void
317 CodeEmitterNV50::setDst(const Value *dst)
318 {
319    const Storage *reg = &dst->join->reg;
320
321    assert(reg->file != FILE_ADDRESS);
322
323    if (reg->data.id < 0 || reg->file == FILE_FLAGS) {
324       code[0] |= (127 << 2) | 1;
325       code[1] |= 8;
326    } else {
327       int id;
328       if (reg->file == FILE_SHADER_OUTPUT) {
329          code[1] |= 8;
330          id = reg->data.offset / 4;
331       } else {
332          id = reg->data.id;
333       }
334       code[0] |= id << 2;
335    }
336 }
337
338 void
339 CodeEmitterNV50::setDst(const Instruction *i, int d)
340 {
341    if (i->defExists(d)) {
342       setDst(i->getDef(d));
343    } else
344    if (!d) {
345       code[0] |= 0x01fc; // bit bucket
346       code[1] |= 0x0008;
347    }
348 }
349
350 // 3 * 2 bits:
351 // 0: r
352 // 1: a/s
353 // 2: c
354 // 3: i
355 void
356 CodeEmitterNV50::setSrcFileBits(const Instruction *i, int enc)
357 {
358    uint8_t mode = 0;
359
360    for (unsigned int s = 0; s < Target::operationSrcNr[i->op]; ++s) {
361       switch (i->src(s).getFile()) {
362       case FILE_GPR:
363          break;
364       case FILE_MEMORY_SHARED:
365       case FILE_SHADER_INPUT:
366          mode |= 1 << (s * 2);
367          break;
368       case FILE_MEMORY_CONST:
369          mode |= 2 << (s * 2);
370          break;
371       case FILE_IMMEDIATE:
372          mode |= 3 << (s * 2);
373          break;
374       default:
375               ERROR("invalid file on source %i: %u\n", s, i->src(s).getFile());
376          assert(0);
377          break;
378       }
379    }
380    switch (mode) {
381    case 0x00: // rrr
382       break;
383    case 0x01: // arr/grr
384       if (progType == Program::TYPE_GEOMETRY && i->src(0).isIndirect(0)) {
385          code[0] |= 0x01800000;
386          if (enc == NV50_OP_ENC_LONG || enc == NV50_OP_ENC_LONG_ALT)
387             code[1] |= 0x00200000;
388       } else {
389          if (enc == NV50_OP_ENC_SHORT)
390             code[0] |= 0x01000000;
391          else
392             code[1] |= 0x00200000;
393       }
394       break;
395    case 0x03: // irr
396       assert(i->op == OP_MOV);
397       return;
398    case 0x0c: // rir
399       break;
400    case 0x0d: // gir
401       assert(progType == Program::TYPE_GEOMETRY ||
402              progType == Program::TYPE_COMPUTE);
403       code[0] |= 0x01000000;
404       if (progType == Program::TYPE_GEOMETRY && i->src(0).isIndirect(0)) {
405          int reg = i->src(0).getIndirect(0)->rep()->reg.data.id;
406          assert(reg < 3);
407          code[0] |= (reg + 1) << 26;
408       }
409       break;
410    case 0x08: // rcr
411       code[0] |= (enc == NV50_OP_ENC_LONG_ALT) ? 0x01000000 : 0x00800000;
412       code[1] |= (i->getSrc(1)->reg.fileIndex << 22);
413       break;
414    case 0x09: // acr/gcr
415       if (progType == Program::TYPE_GEOMETRY && i->src(0).isIndirect(0)) {
416          code[0] |= 0x01800000;
417       } else {
418          code[0] |= (enc == NV50_OP_ENC_LONG_ALT) ? 0x01000000 : 0x00800000;
419          code[1] |= 0x00200000;
420       }
421       code[1] |= (i->getSrc(1)->reg.fileIndex << 22);
422       break;
423    case 0x20: // rrc
424       code[0] |= 0x01000000;
425       code[1] |= (i->getSrc(2)->reg.fileIndex << 22);
426       break;
427    case 0x21: // arc
428       code[0] |= 0x01000000;
429       code[1] |= 0x00200000 | (i->getSrc(2)->reg.fileIndex << 22);
430       assert(progType != Program::TYPE_GEOMETRY);
431       break;
432    default:
433       ERROR("not encodable: %x\n", mode);
434       assert(0);
435       break;
436    }
437    if (progType != Program::TYPE_COMPUTE)
438       return;
439
440    if ((mode & 3) == 1) {
441       const int pos = i->src(1).getFile() == FILE_IMMEDIATE ? 13 : 14;
442
443       switch (i->getSrc(0)->reg.type) {
444       case TYPE_U8:
445          break;
446       case TYPE_U16:
447          code[0] |= 1 << pos;
448          break;
449       case TYPE_S16:
450          code[0] |= 2 << pos;
451          break;
452       default:
453          code[0] |= 3 << pos;
454          assert(i->getSrc(0)->reg.size == 4);
455          break;
456       }
457    }
458 }
459
460 void
461 CodeEmitterNV50::setSrc(const Instruction *i, unsigned int s, int slot)
462 {
463    if (Target::operationSrcNr[i->op] <= s)
464       return;
465    const Storage *reg = &i->src(s).rep()->reg;
466
467    unsigned int id = (reg->file == FILE_GPR) ?
468       reg->data.id :
469       reg->data.offset >> (reg->size >> 1); // no > 4 byte sources here
470
471    switch (slot) {
472    case 0: code[0] |= id << 9; break;
473    case 1: code[0] |= id << 16; break;
474    case 2: code[1] |= id << 14; break;
475    default:
476       assert(0);
477       break;
478    }
479 }
480
481 // the default form:
482 //  - long instruction
483 //  - 1 to 3 sources in slots 0, 1, 2 (rrr, arr, rcr, acr, rrc, arc, gcr, grr)
484 //  - address & flags
485 void
486 CodeEmitterNV50::emitForm_MAD(const Instruction *i)
487 {
488    assert(i->encSize == 8);
489    code[0] |= 1;
490
491    emitFlagsRd(i);
492    emitFlagsWr(i);
493
494    setDst(i, 0);
495
496    setSrcFileBits(i, NV50_OP_ENC_LONG);
497    setSrc(i, 0, 0);
498    setSrc(i, 1, 1);
499    setSrc(i, 2, 2);
500
501    if (i->getIndirect(0, 0)) {
502       assert(!i->srcExists(1) || !i->getIndirect(1, 0));
503       assert(!i->srcExists(2) || !i->getIndirect(2, 0));
504       setAReg16(i, 0);
505    } else if (i->srcExists(1) && i->getIndirect(1, 0)) {
506       assert(!i->srcExists(2) || !i->getIndirect(2, 0));
507       setAReg16(i, 1);
508    } else {
509       setAReg16(i, 2);
510    }
511 }
512
513 // like default form, but 2nd source in slot 2, and no 3rd source
514 void
515 CodeEmitterNV50::emitForm_ADD(const Instruction *i)
516 {
517    assert(i->encSize == 8);
518    code[0] |= 1;
519
520    emitFlagsRd(i);
521    emitFlagsWr(i);
522
523    setDst(i, 0);
524
525    setSrcFileBits(i, NV50_OP_ENC_LONG_ALT);
526    setSrc(i, 0, 0);
527    setSrc(i, 1, 2);
528
529    if (i->getIndirect(0, 0)) {
530       assert(!i->getIndirect(1, 0));
531       setAReg16(i, 0);
532    } else {
533       setAReg16(i, 1);
534    }
535 }
536
537 // default short form (rr, ar, rc, gr)
538 void
539 CodeEmitterNV50::emitForm_MUL(const Instruction *i)
540 {
541    assert(i->encSize == 4 && !(code[0] & 1));
542    assert(i->defExists(0));
543    assert(!i->getPredicate());
544
545    setDst(i, 0);
546
547    setSrcFileBits(i, NV50_OP_ENC_SHORT);
548    setSrc(i, 0, 0);
549    setSrc(i, 1, 1);
550 }
551
552 // usual immediate form
553 // - 1 to 3 sources where second is immediate (rir, gir)
554 // - no address or predicate possible
555 void
556 CodeEmitterNV50::emitForm_IMM(const Instruction *i)
557 {
558    assert(i->encSize == 8);
559    code[0] |= 1;
560
561    assert(i->defExists(0) && i->srcExists(0));
562
563    setDst(i, 0);
564
565    setSrcFileBits(i, NV50_OP_ENC_IMM);
566    if (Target::operationSrcNr[i->op] > 1) {
567       setSrc(i, 0, 0);
568       setImmediate(i, 1);
569       // If there is another source, it has to be the same as the dest reg.
570    } else {
571       setImmediate(i, 0);
572    }
573 }
574
575 void
576 CodeEmitterNV50::emitLoadStoreSizeLG(DataType ty, int pos)
577 {
578    uint8_t enc;
579
580    switch (ty) {
581    case TYPE_F32: // fall through
582    case TYPE_S32: // fall through
583    case TYPE_U32:  enc = 0x6; break;
584    case TYPE_B128: enc = 0x5; break;
585    case TYPE_F64: // fall through
586    case TYPE_S64: // fall through
587    case TYPE_U64:  enc = 0x4; break;
588    case TYPE_S16:  enc = 0x3; break;
589    case TYPE_U16:  enc = 0x2; break;
590    case TYPE_S8:   enc = 0x1; break;
591    case TYPE_U8:   enc = 0x0; break;
592    default:
593       enc = 0;
594       assert(!"invalid load/store type");
595       break;
596    }
597    code[pos / 32] |= enc << (pos % 32);
598 }
599
600 void
601 CodeEmitterNV50::emitLoadStoreSizeCS(DataType ty)
602 {
603    switch (ty) {
604    case TYPE_U8: break;
605    case TYPE_U16: code[1] |= 0x4000; break;
606    case TYPE_S16: code[1] |= 0x8000; break;
607    case TYPE_F32:
608    case TYPE_S32:
609    case TYPE_U32: code[1] |= 0xc000; break;
610    default:
611       assert(0);
612       break;
613    }
614 }
615
616 void
617 CodeEmitterNV50::emitLOAD(const Instruction *i)
618 {
619    DataFile sf = i->src(0).getFile();
620    int32_t offset = i->getSrc(0)->reg.data.offset;
621
622    switch (sf) {
623    case FILE_SHADER_INPUT:
624       if (progType == Program::TYPE_GEOMETRY && i->src(0).isIndirect(0))
625          code[0] = 0x11800001;
626       else
627          // use 'mov' where we can
628          code[0] = i->src(0).isIndirect(0) ? 0x00000001 : 0x10000001;
629       code[1] = 0x00200000 | (i->lanes << 14);
630       if (typeSizeof(i->dType) == 4)
631          code[1] |= 0x04000000;
632       break;
633    case FILE_MEMORY_SHARED:
634       if (targ->getChipset() >= 0x84) {
635          assert(offset <= (int32_t)(0x3fff * typeSizeof(i->sType)));
636          code[0] = 0x10000001;
637          code[1] = 0x40000000;
638
639          if (typeSizeof(i->dType) == 4)
640             code[1] |= 0x04000000;
641
642          emitLoadStoreSizeCS(i->sType);
643       } else {
644          assert(offset <= (int32_t)(0x1f * typeSizeof(i->sType)));
645          code[0] = 0x10000001;
646          code[1] = 0x00200000 | (i->lanes << 14);
647          emitLoadStoreSizeCS(i->sType);
648       }
649       break;
650    case FILE_MEMORY_CONST:
651       code[0] = 0x10000001;
652       code[1] = 0x20000000 | (i->getSrc(0)->reg.fileIndex << 22);
653       if (typeSizeof(i->dType) == 4)
654          code[1] |= 0x04000000;
655       emitLoadStoreSizeCS(i->sType);
656       break;
657    case FILE_MEMORY_LOCAL:
658       code[0] = 0xd0000001;
659       code[1] = 0x40000000;
660       break;
661    case FILE_MEMORY_GLOBAL:
662       code[0] = 0xd0000001 | (i->getSrc(0)->reg.fileIndex << 16);
663       code[1] = 0x80000000;
664       break;
665    default:
666       assert(!"invalid load source file");
667       break;
668    }
669    if (sf == FILE_MEMORY_LOCAL ||
670        sf == FILE_MEMORY_GLOBAL)
671       emitLoadStoreSizeLG(i->sType, 21 + 32);
672
673    setDst(i, 0);
674
675    emitFlagsRd(i);
676    emitFlagsWr(i);
677
678    if (i->src(0).getFile() == FILE_MEMORY_GLOBAL) {
679       srcId(*i->src(0).getIndirect(0), 9);
680    } else {
681       setAReg16(i, 0);
682       srcAddr16(i->src(0), i->src(0).getFile() != FILE_MEMORY_LOCAL, 9);
683    }
684 }
685
686 void
687 CodeEmitterNV50::emitSTORE(const Instruction *i)
688 {
689    DataFile f = i->getSrc(0)->reg.file;
690    int32_t offset = i->getSrc(0)->reg.data.offset;
691
692    switch (f) {
693    case FILE_SHADER_OUTPUT:
694       code[0] = 0x00000001 | ((offset >> 2) << 9);
695       code[1] = 0x80c00000;
696       srcId(i->src(1), 32 + 14);
697       break;
698    case FILE_MEMORY_GLOBAL:
699       code[0] = 0xd0000001 | (i->getSrc(0)->reg.fileIndex << 16);
700       code[1] = 0xa0000000;
701       emitLoadStoreSizeLG(i->dType, 21 + 32);
702       srcId(i->src(1), 2);
703       break;
704    case FILE_MEMORY_LOCAL:
705       code[0] = 0xd0000001;
706       code[1] = 0x60000000;
707       emitLoadStoreSizeLG(i->dType, 21 + 32);
708       srcId(i->src(1), 2);
709       break;
710    case FILE_MEMORY_SHARED:
711       code[0] = 0x00000001;
712       code[1] = 0xe0000000;
713       switch (typeSizeof(i->dType)) {
714       case 1:
715          code[0] |= offset << 9;
716          code[1] |= 0x00400000;
717          break;
718       case 2:
719          code[0] |= (offset >> 1) << 9;
720          break;
721       case 4:
722          code[0] |= (offset >> 2) << 9;
723          code[1] |= 0x04200000;
724          break;
725       default:
726          assert(0);
727          break;
728       }
729       srcId(i->src(1), 32 + 14);
730       break;
731    default:
732       assert(!"invalid store destination file");
733       break;
734    }
735
736    if (f == FILE_MEMORY_GLOBAL)
737       srcId(*i->src(0).getIndirect(0), 9);
738    else
739       setAReg16(i, 0);
740
741    if (f == FILE_MEMORY_LOCAL)
742       srcAddr16(i->src(0), false, 9);
743
744    emitFlagsRd(i);
745 }
746
747 void
748 CodeEmitterNV50::emitMOV(const Instruction *i)
749 {
750    DataFile sf = i->getSrc(0)->reg.file;
751    DataFile df = i->getDef(0)->reg.file;
752
753    assert(sf == FILE_GPR || df == FILE_GPR);
754
755    if (sf == FILE_FLAGS) {
756       code[0] = 0x00000001;
757       code[1] = 0x20000000;
758       defId(i->def(0), 2);
759       srcId(i->src(0), 12);
760       emitFlagsRd(i);
761    } else
762    if (sf == FILE_ADDRESS) {
763       code[0] = 0x00000001;
764       code[1] = 0x40000000;
765       defId(i->def(0), 2);
766       setARegBits(SDATA(i->src(0)).id + 1);
767       emitFlagsRd(i);
768    } else
769    if (df == FILE_FLAGS) {
770       code[0] = 0x00000001;
771       code[1] = 0xa0000000;
772       defId(i->def(0), 4);
773       srcId(i->src(0), 9);
774       emitFlagsRd(i);
775    } else
776    if (sf == FILE_IMMEDIATE) {
777       code[0] = 0x10008001;
778       code[1] = 0x00000003;
779       emitForm_IMM(i);
780    } else {
781       if (i->encSize == 4) {
782          code[0] = 0x10008000;
783       } else {
784          code[0] = 0x10000001;
785          code[1] = (typeSizeof(i->dType) == 2) ? 0 : 0x04000000;
786          code[1] |= (i->lanes << 14);
787          emitFlagsRd(i);
788       }
789       defId(i->def(0), 2);
790       srcId(i->src(0), 9);
791    }
792    if (df == FILE_SHADER_OUTPUT) {
793       assert(i->encSize == 8);
794       code[1] |= 0x8;
795    }
796 }
797
798 static inline uint8_t getSRegEncoding(const ValueRef &ref)
799 {
800    switch (SDATA(ref).sv.sv) {
801    case SV_PHYSID:        return 0;
802    case SV_CLOCK:         return 1;
803    case SV_VERTEX_STRIDE: return 3;
804 // case SV_PM_COUNTER:    return 4 + SDATA(ref).sv.index;
805    case SV_SAMPLE_INDEX:  return 8;
806    default:
807       assert(!"no sreg for system value");
808       return 0;
809    }
810 }
811
812 void
813 CodeEmitterNV50::emitRDSV(const Instruction *i)
814 {
815    code[0] = 0x00000001;
816    code[1] = 0x60000000 | (getSRegEncoding(i->src(0)) << 14);
817    defId(i->def(0), 2);
818    emitFlagsRd(i);
819 }
820
821 void
822 CodeEmitterNV50::emitNOP()
823 {
824    code[0] = 0xf0000001;
825    code[1] = 0xe0000000;
826 }
827
828 void
829 CodeEmitterNV50::emitQUADOP(const Instruction *i, uint8_t lane, uint8_t quOp)
830 {
831    code[0] = 0xc0000000 | (lane << 16);
832    code[1] = 0x80000000;
833
834    code[0] |= (quOp & 0x03) << 20;
835    code[1] |= (quOp & 0xfc) << 20;
836
837    emitForm_ADD(i);
838
839    if (!i->srcExists(1))
840       srcId(i->src(0), 32 + 14);
841 }
842
843 /* NOTE: This returns the base address of a vertex inside the primitive.
844  * src0 is an immediate, the index (not offset) of the vertex
845  * inside the primitive. XXX: signed or unsigned ?
846  * src1 (may be NULL) should use whatever units the hardware requires
847  * (on nv50 this is bytes, so, relative index * 4; signed 16 bit value).
848  */
849 void
850 CodeEmitterNV50::emitPFETCH(const Instruction *i)
851 {
852    const uint32_t prim = i->src(0).get()->reg.data.u32;
853    assert(prim <= 127);
854
855    if (i->def(0).getFile() == FILE_ADDRESS) {
856       // shl $aX a[] 0
857       code[0] = 0x00000001 | ((DDATA(i->def(0)).id + 1) << 2);
858       code[1] = 0xc0200000;
859       code[0] |= prim << 9;
860       assert(!i->srcExists(1));
861    } else
862    if (i->srcExists(1)) {
863       // ld b32 $rX a[$aX+base]
864       code[0] = 0x00000001;
865       code[1] = 0x04200000 | (0xf << 14);
866       defId(i->def(0), 2);
867       code[0] |= prim << 9;
868       setARegBits(SDATA(i->src(1)).id + 1);
869    } else {
870       // mov b32 $rX a[]
871       code[0] = 0x10000001;
872       code[1] = 0x04200000 | (0xf << 14);
873       defId(i->def(0), 2);
874       code[0] |= prim << 9;
875    }
876    emitFlagsRd(i);
877 }
878
879 void
880 CodeEmitterNV50::emitINTERP(const Instruction *i)
881 {
882    code[0] = 0x80000000;
883
884    defId(i->def(0), 2);
885    srcAddr8(i->src(0), 16);
886
887    if (i->encSize != 8 && i->getInterpMode() == NV50_IR_INTERP_FLAT) {
888       code[0] |= 1 << 8;
889    } else {
890       if (i->op == OP_PINTERP) {
891          code[0] |= 1 << 25;
892          srcId(i->src(1), 9);
893       }
894       if (i->getSampleMode() == NV50_IR_INTERP_CENTROID)
895          code[0] |= 1 << 24;
896    }
897
898    if (i->encSize == 8) {
899       if (i->getInterpMode() == NV50_IR_INTERP_FLAT)
900          code[1] = 4 << 16;
901       else
902          code[1] = (code[0] & (3 << 24)) >> (24 - 16);
903       code[0] &= ~0x03000000;
904       code[0] |= 1;
905       emitFlagsRd(i);
906    }
907 }
908
909 void
910 CodeEmitterNV50::emitMINMAX(const Instruction *i)
911 {
912    if (i->dType == TYPE_F64) {
913       code[0] = 0xe0000000;
914       code[1] = (i->op == OP_MIN) ? 0xa0000000 : 0xc0000000;
915    } else {
916       code[0] = 0x30000000;
917       code[1] = 0x80000000;
918       if (i->op == OP_MIN)
919          code[1] |= 0x20000000;
920
921       switch (i->dType) {
922       case TYPE_F32: code[0] |= 0x80000000; break;
923       case TYPE_S32: code[1] |= 0x8c000000; break;
924       case TYPE_U32: code[1] |= 0x84000000; break;
925       case TYPE_S16: code[1] |= 0x80000000; break;
926       case TYPE_U16: break;
927       default:
928          assert(0);
929          break;
930       }
931       code[1] |= i->src(0).mod.abs() << 20;
932       code[1] |= i->src(0).mod.neg() << 26;
933       code[1] |= i->src(1).mod.abs() << 19;
934       code[1] |= i->src(1).mod.neg() << 27;
935    }
936    emitForm_MAD(i);
937 }
938
939 void
940 CodeEmitterNV50::emitFMAD(const Instruction *i)
941 {
942    const int neg_mul = i->src(0).mod.neg() ^ i->src(1).mod.neg();
943    const int neg_add = i->src(2).mod.neg();
944
945    code[0] = 0xe0000000;
946
947    if (i->src(1).getFile() == FILE_IMMEDIATE) {
948       code[1] = 0;
949       emitForm_IMM(i);
950       code[0] |= neg_mul << 15;
951       code[0] |= neg_add << 22;
952       if (i->saturate)
953          code[0] |= 1 << 8;
954    } else
955    if (i->encSize == 4) {
956       emitForm_MUL(i);
957       code[0] |= neg_mul << 15;
958       code[0] |= neg_add << 22;
959       if (i->saturate)
960          code[0] |= 1 << 8;
961    } else {
962       code[1]  = neg_mul << 26;
963       code[1] |= neg_add << 27;
964       if (i->saturate)
965          code[1] |= 1 << 29;
966       emitForm_MAD(i);
967    }
968 }
969
970 void
971 CodeEmitterNV50::emitFADD(const Instruction *i)
972 {
973    const int neg0 = i->src(0).mod.neg();
974    const int neg1 = i->src(1).mod.neg() ^ ((i->op == OP_SUB) ? 1 : 0);
975
976    code[0] = 0xb0000000;
977
978    assert(!(i->src(0).mod | i->src(1).mod).abs());
979
980    if (i->src(1).getFile() == FILE_IMMEDIATE) {
981       code[1] = 0;
982       emitForm_IMM(i);
983       code[0] |= neg0 << 15;
984       code[0] |= neg1 << 22;
985       if (i->saturate)
986          code[0] |= 1 << 8;
987    } else
988    if (i->encSize == 8) {
989       code[1] = 0;
990       emitForm_ADD(i);
991       code[1] |= neg0 << 26;
992       code[1] |= neg1 << 27;
993       if (i->saturate)
994          code[1] |= 1 << 29;
995    } else {
996       emitForm_MUL(i);
997       code[0] |= neg0 << 15;
998       code[0] |= neg1 << 22;
999       if (i->saturate)
1000          code[0] |= 1 << 8;
1001    }
1002 }
1003
1004 void
1005 CodeEmitterNV50::emitUADD(const Instruction *i)
1006 {
1007    const int neg0 = i->src(0).mod.neg();
1008    const int neg1 = i->src(1).mod.neg() ^ ((i->op == OP_SUB) ? 1 : 0);
1009
1010    code[0] = 0x20008000;
1011
1012    if (i->src(1).getFile() == FILE_IMMEDIATE) {
1013       code[1] = 0;
1014       emitForm_IMM(i);
1015    } else
1016    if (i->encSize == 8) {
1017       code[0] = 0x20000000;
1018       code[1] = (typeSizeof(i->dType) == 2) ? 0 : 0x04000000;
1019       emitForm_ADD(i);
1020    } else {
1021       emitForm_MUL(i);
1022    }
1023    assert(!(neg0 && neg1));
1024    code[0] |= neg0 << 28;
1025    code[0] |= neg1 << 22;
1026
1027    if (i->flagsSrc >= 0) {
1028       // addc == sub | subr
1029       assert(!(code[0] & 0x10400000) && !i->getPredicate());
1030       code[0] |= 0x10400000;
1031       srcId(i->src(i->flagsSrc), 32 + 12);
1032    }
1033 }
1034
1035 void
1036 CodeEmitterNV50::emitAADD(const Instruction *i)
1037 {
1038    const int s = (i->op == OP_MOV) ? 0 : 1;
1039
1040    code[0] = 0xd0000001 | (i->getSrc(s)->reg.data.u16 << 9);
1041    code[1] = 0x20000000;
1042
1043    code[0] |= (DDATA(i->def(0)).id + 1) << 2;
1044
1045    emitFlagsRd(i);
1046
1047    if (s && i->srcExists(0))
1048       setARegBits(SDATA(i->src(0)).id + 1);
1049 }
1050
1051 void
1052 CodeEmitterNV50::emitIMUL(const Instruction *i)
1053 {
1054    code[0] = 0x40000000;
1055
1056    if (i->encSize == 8) {
1057       code[1] = (i->sType == TYPE_S16) ? (0x8000 | 0x4000) : 0x0000;
1058       emitForm_MAD(i);
1059    } else {
1060       if (i->sType == TYPE_S16)
1061          code[0] |= 0x8100;
1062       emitForm_MUL(i);
1063    }
1064 }
1065
1066 void
1067 CodeEmitterNV50::emitFMUL(const Instruction *i)
1068 {
1069    const int neg = (i->src(0).mod ^ i->src(1).mod).neg();
1070
1071    code[0] = 0xc0000000;
1072
1073    if (i->src(1).getFile() == FILE_IMMEDIATE) {
1074       code[1] = 0;
1075       emitForm_IMM(i);
1076       if (neg)
1077          code[0] |= 0x8000;
1078       if (i->saturate)
1079          code[0] |= 1 << 8;
1080    } else
1081    if (i->encSize == 8) {
1082       code[1] = i->rnd == ROUND_Z ? 0x0000c000 : 0;
1083       if (neg)
1084          code[1] |= 0x08000000;
1085       if (i->saturate)
1086          code[1] |= 1 << 20;
1087       emitForm_MAD(i);
1088    } else {
1089       emitForm_MUL(i);
1090       if (neg)
1091          code[0] |= 0x8000;
1092       if (i->saturate)
1093          code[0] |= 1 << 8;
1094    }
1095 }
1096
1097 void
1098 CodeEmitterNV50::emitIMAD(const Instruction *i)
1099 {
1100    code[0] = 0x60000000;
1101    if (isSignedType(i->sType))
1102       code[1] = i->saturate ? 0x40000000 : 0x20000000;
1103    else
1104       code[1] = 0x00000000;
1105
1106    int neg1 = i->src(0).mod.neg() ^ i->src(1).mod.neg();
1107    int neg2 = i->src(2).mod.neg();
1108
1109    assert(!(neg1 & neg2));
1110    code[1] |= neg1 << 27;
1111    code[1] |= neg2 << 26;
1112
1113    emitForm_MAD(i);
1114
1115    if (i->flagsSrc >= 0) {
1116       // add with carry from $cX
1117       assert(!(code[1] & 0x0c000000) && !i->getPredicate());
1118       code[1] |= 0xc << 24;
1119       srcId(i->src(i->flagsSrc), 32 + 12);
1120    }
1121 }
1122
1123 void
1124 CodeEmitterNV50::emitISAD(const Instruction *i)
1125 {
1126    if (i->encSize == 8) {
1127       code[0] = 0x50000000;
1128       switch (i->sType) {
1129       case TYPE_U32: code[1] = 0x04000000; break;
1130       case TYPE_S32: code[1] = 0x0c000000; break;
1131       case TYPE_U16: code[1] = 0x00000000; break;
1132       case TYPE_S16: code[1] = 0x08000000; break;
1133       default:
1134          assert(0);
1135          break;
1136       }
1137       emitForm_MAD(i);
1138    } else {
1139       switch (i->sType) {
1140       case TYPE_U32: code[0] = 0x50008000; break;
1141       case TYPE_S32: code[0] = 0x50008100; break;
1142       case TYPE_U16: code[0] = 0x50000000; break;
1143       case TYPE_S16: code[0] = 0x50000100; break;
1144       default:
1145          assert(0);
1146          break;
1147       }
1148       emitForm_MUL(i);
1149    }
1150 }
1151
1152 void
1153 CodeEmitterNV50::emitSET(const Instruction *i)
1154 {
1155    code[0] = 0x30000000;
1156    code[1] = 0x60000000;
1157
1158    emitCondCode(i->asCmp()->setCond, i->sType, 32 + 14);
1159
1160    switch (i->sType) {
1161    case TYPE_F32: code[0] |= 0x80000000; break;
1162    case TYPE_S32: code[1] |= 0x0c000000; break;
1163    case TYPE_U32: code[1] |= 0x04000000; break;
1164    case TYPE_S16: code[1] |= 0x08000000; break;
1165    case TYPE_U16: break;
1166    default:
1167       assert(0);
1168       break;
1169    }
1170    if (i->src(0).mod.neg()) code[1] |= 0x04000000;
1171    if (i->src(1).mod.neg()) code[1] |= 0x08000000;
1172    if (i->src(0).mod.abs()) code[1] |= 0x00100000;
1173    if (i->src(1).mod.abs()) code[1] |= 0x00080000;
1174
1175    emitForm_MAD(i);
1176 }
1177
1178 void
1179 CodeEmitterNV50::roundMode_CVT(RoundMode rnd)
1180 {
1181    switch (rnd) {
1182    case ROUND_NI: code[1] |= 0x08000000; break;
1183    case ROUND_M:  code[1] |= 0x00020000; break;
1184    case ROUND_MI: code[1] |= 0x08020000; break;
1185    case ROUND_P:  code[1] |= 0x00040000; break;
1186    case ROUND_PI: code[1] |= 0x08040000; break;
1187    case ROUND_Z:  code[1] |= 0x00060000; break;
1188    case ROUND_ZI: code[1] |= 0x08060000; break;
1189    default:
1190       assert(rnd == ROUND_N);
1191       break;
1192    }
1193 }
1194
1195 void
1196 CodeEmitterNV50::emitCVT(const Instruction *i)
1197 {
1198    const bool f2f = isFloatType(i->dType) && isFloatType(i->sType);
1199    RoundMode rnd;
1200    DataType dType;
1201
1202    switch (i->op) {
1203    case OP_CEIL:  rnd = f2f ? ROUND_PI : ROUND_P; break;
1204    case OP_FLOOR: rnd = f2f ? ROUND_MI : ROUND_M; break;
1205    case OP_TRUNC: rnd = f2f ? ROUND_ZI : ROUND_Z; break;
1206    default:
1207       rnd = i->rnd;
1208       break;
1209    }
1210
1211    if (i->op == OP_NEG && i->dType == TYPE_U32)
1212       dType = TYPE_S32;
1213    else
1214       dType = i->dType;
1215
1216    code[0] = 0xa0000000;
1217
1218    switch (dType) {
1219    case TYPE_F64:
1220       switch (i->sType) {
1221       case TYPE_F64: code[1] = 0xc4404000; break;
1222       case TYPE_S64: code[1] = 0x44414000; break;
1223       case TYPE_U64: code[1] = 0x44404000; break;
1224       case TYPE_F32: code[1] = 0xc4400000; break;
1225       case TYPE_S32: code[1] = 0x44410000; break;
1226       case TYPE_U32: code[1] = 0x44400000; break;
1227       default:
1228          assert(0);
1229          break;
1230       }
1231       break;
1232    case TYPE_S64:
1233       switch (i->sType) {
1234       case TYPE_F64: code[1] = 0x8c404000; break;
1235       case TYPE_F32: code[1] = 0x8c400000; break;
1236       default:
1237          assert(0);
1238          break;
1239       }
1240       break;
1241    case TYPE_U64:
1242       switch (i->sType) {
1243       case TYPE_F64: code[1] = 0x84404000; break;
1244       case TYPE_F32: code[1] = 0x84400000; break;
1245       default:
1246          assert(0);
1247          break;
1248       }
1249       break;
1250    case TYPE_F32:
1251       switch (i->sType) {
1252       case TYPE_F64: code[1] = 0xc0404000; break;
1253       case TYPE_S64: code[1] = 0x40414000; break;
1254       case TYPE_U64: code[1] = 0x40404000; break;
1255       case TYPE_F32: code[1] = 0xc4004000; break;
1256       case TYPE_S32: code[1] = 0x44014000; break;
1257       case TYPE_U32: code[1] = 0x44004000; break;
1258       case TYPE_F16: code[1] = 0xc4000000; break;
1259       case TYPE_U16: code[1] = 0x44000000; break;
1260       default:
1261          assert(0);
1262          break;
1263       }
1264       break;
1265    case TYPE_S32:
1266       switch (i->sType) {
1267       case TYPE_F64: code[1] = 0x88404000; break;
1268       case TYPE_F32: code[1] = 0x8c004000; break;
1269       case TYPE_S32: code[1] = 0x0c014000; break;
1270       case TYPE_U32: code[1] = 0x0c004000; break;
1271       case TYPE_F16: code[1] = 0x8c000000; break;
1272       case TYPE_S16: code[1] = 0x0c010000; break;
1273       case TYPE_U16: code[1] = 0x0c000000; break;
1274       case TYPE_S8:  code[1] = 0x0c018000; break;
1275       case TYPE_U8:  code[1] = 0x0c008000; break;
1276       default:
1277          assert(0);
1278          break;
1279       }
1280       break;
1281    case TYPE_U32:
1282       switch (i->sType) {
1283       case TYPE_F64: code[1] = 0x80404000; break;
1284       case TYPE_F32: code[1] = 0x84004000; break;
1285       case TYPE_S32: code[1] = 0x04014000; break;
1286       case TYPE_U32: code[1] = 0x04004000; break;
1287       case TYPE_F16: code[1] = 0x84000000; break;
1288       case TYPE_S16: code[1] = 0x04010000; break;
1289       case TYPE_U16: code[1] = 0x04000000; break;
1290       case TYPE_S8:  code[1] = 0x04018000; break;
1291       case TYPE_U8:  code[1] = 0x04008000; break;
1292       default:
1293          assert(0);
1294          break;
1295       }
1296       break;
1297    case TYPE_S16:
1298    case TYPE_U16:
1299    case TYPE_S8:
1300    case TYPE_U8:
1301    default:
1302       assert(0);
1303       break;
1304    }
1305    if (typeSizeof(i->sType) == 1 && i->getSrc(0)->reg.size == 4)
1306       code[1] |= 0x00004000;
1307
1308    roundMode_CVT(rnd);
1309
1310    switch (i->op) {
1311    case OP_ABS: code[1] |= 1 << 20; break;
1312    case OP_SAT: code[1] |= 1 << 19; break;
1313    case OP_NEG: code[1] |= 1 << 29; break;
1314    default:
1315       break;
1316    }
1317    code[1] ^= i->src(0).mod.neg() << 29;
1318    code[1] |= i->src(0).mod.abs() << 20;
1319    if (i->saturate)
1320       code[1] |= 1 << 19;
1321
1322    assert(i->op != OP_ABS || !i->src(0).mod.neg());
1323
1324    emitForm_MAD(i);
1325 }
1326
1327 void
1328 CodeEmitterNV50::emitPreOp(const Instruction *i)
1329 {
1330    code[0] = 0xb0000000;
1331    code[1] = (i->op == OP_PREEX2) ? 0xc0004000 : 0xc0000000;
1332
1333    code[1] |= i->src(0).mod.abs() << 20;
1334    code[1] |= i->src(0).mod.neg() << 26;
1335
1336    emitForm_MAD(i);
1337 }
1338
1339 void
1340 CodeEmitterNV50::emitSFnOp(const Instruction *i, uint8_t subOp)
1341 {
1342    code[0] = 0x90000000;
1343
1344    if (i->encSize == 4) {
1345       assert(i->op == OP_RCP);
1346       code[0] |= i->src(0).mod.abs() << 15;
1347       code[0] |= i->src(0).mod.neg() << 22;
1348       emitForm_MUL(i);
1349    } else {
1350       code[1] = subOp << 29;
1351       code[1] |= i->src(0).mod.abs() << 20;
1352       code[1] |= i->src(0).mod.neg() << 26;
1353       emitForm_MAD(i);
1354    }
1355 }
1356
1357 void
1358 CodeEmitterNV50::emitNOT(const Instruction *i)
1359 {
1360    code[0] = 0xd0000000;
1361    code[1] = 0x0002c000;
1362
1363    switch (i->sType) {
1364    case TYPE_U32:
1365    case TYPE_S32:
1366       code[1] |= 0x04000000;
1367       break;
1368    default:
1369       break;
1370    }
1371    emitForm_MAD(i);
1372    setSrc(i, 0, 1);
1373 }
1374
1375 void
1376 CodeEmitterNV50::emitLogicOp(const Instruction *i)
1377 {
1378    code[0] = 0xd0000000;
1379    code[1] = 0;
1380
1381    if (i->src(1).getFile() == FILE_IMMEDIATE) {
1382       switch (i->op) {
1383       case OP_OR:  code[0] |= 0x0100; break;
1384       case OP_XOR: code[0] |= 0x8000; break;
1385       default:
1386          assert(i->op == OP_AND);
1387          break;
1388       }
1389       if (i->src(0).mod & Modifier(NV50_IR_MOD_NOT))
1390          code[0] |= 1 << 22;
1391
1392       emitForm_IMM(i);
1393    } else {
1394       switch (i->op) {
1395       case OP_AND: code[1] = 0x04000000; break;
1396       case OP_OR:  code[1] = 0x04004000; break;
1397       case OP_XOR: code[1] = 0x04008000; break;
1398       default:
1399          assert(0);
1400          break;
1401       }
1402       if (i->src(0).mod & Modifier(NV50_IR_MOD_NOT))
1403          code[1] |= 1 << 16;
1404       if (i->src(1).mod & Modifier(NV50_IR_MOD_NOT))
1405          code[1] |= 1 << 17;
1406
1407       emitForm_MAD(i);
1408    }
1409 }
1410
1411 void
1412 CodeEmitterNV50::emitARL(const Instruction *i, unsigned int shl)
1413 {
1414    code[0] = 0x00000001 | (shl << 16);
1415    code[1] = 0xc0000000;
1416
1417    code[0] |= (DDATA(i->def(0)).id + 1) << 2;
1418
1419    setSrcFileBits(i, NV50_OP_ENC_IMM);
1420    setSrc(i, 0, 0);
1421    emitFlagsRd(i);
1422 }
1423
1424 void
1425 CodeEmitterNV50::emitShift(const Instruction *i)
1426 {
1427    if (i->def(0).getFile() == FILE_ADDRESS) {
1428       assert(i->srcExists(1) && i->src(1).getFile() == FILE_IMMEDIATE);
1429       emitARL(i, i->getSrc(1)->reg.data.u32 & 0x3f);
1430    } else {
1431       code[0] = 0x30000001;
1432       code[1] = (i->op == OP_SHR) ? 0xe4000000 : 0xc4000000;
1433       if (i->op == OP_SHR && isSignedType(i->sType))
1434           code[1] |= 1 << 27;
1435
1436       if (i->src(1).getFile() == FILE_IMMEDIATE) {
1437          code[1] |= 1 << 20;
1438          code[0] |= (i->getSrc(1)->reg.data.u32 & 0x7f) << 16;
1439          defId(i->def(0), 2);
1440          srcId(i->src(0), 9);
1441          emitFlagsRd(i);
1442       } else {
1443          emitForm_MAD(i);
1444       }
1445    }
1446 }
1447
1448 void
1449 CodeEmitterNV50::emitOUT(const Instruction *i)
1450 {
1451    code[0] = (i->op == OP_EMIT) ? 0xf0000201 : 0xf0000401;
1452    code[1] = 0xc0000000;
1453
1454    emitFlagsRd(i);
1455 }
1456
1457 void
1458 CodeEmitterNV50::emitTEX(const TexInstruction *i)
1459 {
1460    code[0] = 0xf0000001;
1461    code[1] = 0x00000000;
1462
1463    switch (i->op) {
1464    case OP_TXB:
1465       code[1] = 0x20000000;
1466       break;
1467    case OP_TXL:
1468       code[1] = 0x40000000;
1469       break;
1470    case OP_TXF:
1471       code[0] |= 0x01000000;
1472       break;
1473    case OP_TXG:
1474       code[0] |= 0x01000000;
1475       code[1] = 0x80000000;
1476       break;
1477    case OP_TXLQ:
1478       code[1] = 0x60020000;
1479       break;
1480    default:
1481       assert(i->op == OP_TEX);
1482       break;
1483    }
1484
1485    code[0] |= i->tex.r << 9;
1486    code[0] |= i->tex.s << 17;
1487
1488    int argc = i->tex.target.getArgCount();
1489
1490    if (i->op == OP_TXB || i->op == OP_TXL || i->op == OP_TXF)
1491       argc += 1;
1492    if (i->tex.target.isShadow())
1493       argc += 1;
1494    assert(argc <= 4);
1495
1496    code[0] |= (argc - 1) << 22;
1497
1498    if (i->tex.target.isCube()) {
1499       code[0] |= 0x08000000;
1500    } else
1501    if (i->tex.useOffsets) {
1502       code[1] |= (i->tex.offset[0] & 0xf) << 24;
1503       code[1] |= (i->tex.offset[1] & 0xf) << 20;
1504       code[1] |= (i->tex.offset[2] & 0xf) << 16;
1505    }
1506
1507    code[0] |= (i->tex.mask & 0x3) << 25;
1508    code[1] |= (i->tex.mask & 0xc) << 12;
1509
1510    if (i->tex.liveOnly)
1511       code[1] |= 4;
1512
1513    defId(i->def(0), 2);
1514
1515    emitFlagsRd(i);
1516 }
1517
1518 void
1519 CodeEmitterNV50::emitTXQ(const TexInstruction *i)
1520 {
1521    assert(i->tex.query == TXQ_DIMS);
1522
1523    code[0] = 0xf0000001;
1524    code[1] = 0x60000000;
1525
1526    code[0] |= i->tex.r << 9;
1527    code[0] |= i->tex.s << 17;
1528
1529    code[0] |= (i->tex.mask & 0x3) << 25;
1530    code[1] |= (i->tex.mask & 0xc) << 12;
1531
1532    defId(i->def(0), 2);
1533
1534    emitFlagsRd(i);
1535 }
1536
1537 void
1538 CodeEmitterNV50::emitTEXPREP(const TexInstruction *i)
1539 {
1540    code[0] = 0xf8000001 | (3 << 22) | (i->tex.s << 17) | (i->tex.r << 9);
1541    code[1] = 0x60010000;
1542
1543    code[0] |= (i->tex.mask & 0x3) << 25;
1544    code[1] |= (i->tex.mask & 0xc) << 12;
1545    defId(i->def(0), 2);
1546
1547    emitFlagsRd(i);
1548 }
1549
1550 void
1551 CodeEmitterNV50::emitPRERETEmu(const FlowInstruction *i)
1552 {
1553    uint32_t pos = i->target.bb->binPos + 8; // +8 to skip an op */
1554
1555    code[0] = 0x10000003; // bra
1556    code[1] = 0x00000780; // always
1557
1558    switch (i->subOp) {
1559    case NV50_IR_SUBOP_EMU_PRERET + 0: // bra to the call
1560       break;
1561    case NV50_IR_SUBOP_EMU_PRERET + 1: // bra to skip the call
1562       pos += 8;
1563       break;
1564    default:
1565       assert(i->subOp == (NV50_IR_SUBOP_EMU_PRERET + 2));
1566       code[0] = 0x20000003; // call
1567       code[1] = 0x00000000; // no predicate
1568       break;
1569    }
1570    addReloc(RelocEntry::TYPE_CODE, 0, pos, 0x07fff800, 9);
1571    addReloc(RelocEntry::TYPE_CODE, 1, pos, 0x000fc000, -4);
1572 }
1573
1574 void
1575 CodeEmitterNV50::emitFlow(const Instruction *i, uint8_t flowOp)
1576 {
1577    const FlowInstruction *f = i->asFlow();
1578    bool hasPred = false;
1579    bool hasTarg = false;
1580
1581    code[0] = 0x00000003 | (flowOp << 28);
1582    code[1] = 0x00000000;
1583
1584    switch (i->op) {
1585    case OP_BRA:
1586       hasPred = true;
1587       hasTarg = true;
1588       break;
1589    case OP_BREAK:
1590    case OP_BRKPT:
1591    case OP_DISCARD:
1592    case OP_RET:
1593       hasPred = true;
1594       break;
1595    case OP_CALL:
1596    case OP_PREBREAK:
1597    case OP_JOINAT:
1598       hasTarg = true;
1599       break;
1600    case OP_PRERET:
1601       hasTarg = true;
1602       if (i->subOp >= NV50_IR_SUBOP_EMU_PRERET) {
1603          emitPRERETEmu(f);
1604          return;
1605       }
1606       break;
1607    default:
1608       break;
1609    }
1610
1611    if (hasPred)
1612       emitFlagsRd(i);
1613
1614    if (hasTarg && f) {
1615       uint32_t pos;
1616
1617       if (f->op == OP_CALL) {
1618          if (f->builtin) {
1619             pos = targNV50->getBuiltinOffset(f->target.builtin);
1620          } else {
1621             pos = f->target.fn->binPos;
1622          }
1623       } else {
1624          pos = f->target.bb->binPos;
1625       }
1626
1627       code[0] |= ((pos >>  2) & 0xffff) << 11;
1628       code[1] |= ((pos >> 18) & 0x003f) << 14;
1629
1630       RelocEntry::Type relocTy;
1631
1632       relocTy = f->builtin ? RelocEntry::TYPE_BUILTIN : RelocEntry::TYPE_CODE;
1633
1634       addReloc(relocTy, 0, pos, 0x07fff800, 9);
1635       addReloc(relocTy, 1, pos, 0x000fc000, -4);
1636    }
1637 }
1638
1639 void
1640 CodeEmitterNV50::emitBAR(const Instruction *i)
1641 {
1642    ImmediateValue *barId = i->getSrc(0)->asImm();
1643    assert(barId);
1644
1645    code[0] = 0x82000003 | (barId->reg.data.u32 << 21);
1646    code[1] = 0x00004000;
1647
1648    if (i->subOp == NV50_IR_SUBOP_BAR_SYNC)
1649       code[0] |= 1 << 26;
1650 }
1651
1652 void
1653 CodeEmitterNV50::emitATOM(const Instruction *i)
1654 {
1655    uint8_t subOp;
1656    switch (i->subOp) {
1657    case NV50_IR_SUBOP_ATOM_ADD:  subOp = 0x0; break;
1658    case NV50_IR_SUBOP_ATOM_MIN:  subOp = 0x7; break;
1659    case NV50_IR_SUBOP_ATOM_MAX:  subOp = 0x6; break;
1660    case NV50_IR_SUBOP_ATOM_INC:  subOp = 0x4; break;
1661    case NV50_IR_SUBOP_ATOM_DEC:  subOp = 0x5; break;
1662    case NV50_IR_SUBOP_ATOM_AND:  subOp = 0xa; break;
1663    case NV50_IR_SUBOP_ATOM_OR:   subOp = 0xb; break;
1664    case NV50_IR_SUBOP_ATOM_XOR:  subOp = 0xc; break;
1665    case NV50_IR_SUBOP_ATOM_CAS:  subOp = 0x2; break;
1666    case NV50_IR_SUBOP_ATOM_EXCH: subOp = 0x1; break;
1667    default:
1668       assert(!"invalid subop");
1669       return;
1670    }
1671    code[0] = 0xd0000001;
1672    code[1] = 0xe0c00000 | (subOp << 2);
1673    if (isSignedType(i->dType))
1674       code[1] |= 1 << 21;
1675
1676    // args
1677    emitFlagsRd(i);
1678    setDst(i, 0);
1679    setSrc(i, 1, 1);
1680    if (i->subOp == NV50_IR_SUBOP_ATOM_CAS)
1681       setSrc(i, 2, 2);
1682
1683    // g[] pointer
1684    code[0] |= i->getSrc(0)->reg.fileIndex << 23;
1685    srcId(i->getIndirect(0, 0), 9);
1686 }
1687
1688 bool
1689 CodeEmitterNV50::emitInstruction(Instruction *insn)
1690 {
1691    if (!insn->encSize) {
1692       ERROR("skipping unencodable instruction: "); insn->print();
1693       return false;
1694    } else
1695    if (codeSize + insn->encSize > codeSizeLimit) {
1696       ERROR("code emitter output buffer too small\n");
1697       return false;
1698    }
1699
1700    if (insn->bb->getProgram()->dbgFlags & NV50_IR_DEBUG_BASIC) {
1701       INFO("EMIT: "); insn->print();
1702    }
1703
1704    switch (insn->op) {
1705    case OP_MOV:
1706       emitMOV(insn);
1707       break;
1708    case OP_EXIT:
1709    case OP_NOP:
1710    case OP_JOIN:
1711       emitNOP();
1712       break;
1713    case OP_VFETCH:
1714    case OP_LOAD:
1715       emitLOAD(insn);
1716       break;
1717    case OP_EXPORT:
1718    case OP_STORE:
1719       emitSTORE(insn);
1720       break;
1721    case OP_PFETCH:
1722       emitPFETCH(insn);
1723       break;
1724    case OP_RDSV:
1725       emitRDSV(insn);
1726       break;
1727    case OP_LINTERP:
1728    case OP_PINTERP:
1729       emitINTERP(insn);
1730       break;
1731    case OP_ADD:
1732    case OP_SUB:
1733       if (isFloatType(insn->dType))
1734          emitFADD(insn);
1735       else if (insn->getDef(0)->reg.file == FILE_ADDRESS)
1736          emitAADD(insn);
1737       else
1738          emitUADD(insn);
1739       break;
1740    case OP_MUL:
1741       if (isFloatType(insn->dType))
1742          emitFMUL(insn);
1743       else
1744          emitIMUL(insn);
1745       break;
1746    case OP_MAD:
1747    case OP_FMA:
1748       if (isFloatType(insn->dType))
1749          emitFMAD(insn);
1750       else
1751          emitIMAD(insn);
1752       break;
1753    case OP_SAD:
1754       emitISAD(insn);
1755       break;
1756    case OP_NOT:
1757       emitNOT(insn);
1758       break;
1759    case OP_AND:
1760    case OP_OR:
1761    case OP_XOR:
1762       emitLogicOp(insn);
1763       break;
1764    case OP_SHL:
1765    case OP_SHR:
1766       emitShift(insn);
1767       break;
1768    case OP_SET:
1769       emitSET(insn);
1770       break;
1771    case OP_MIN:
1772    case OP_MAX:
1773       emitMINMAX(insn);
1774       break;
1775    case OP_CEIL:
1776    case OP_FLOOR:
1777    case OP_TRUNC:
1778    case OP_ABS:
1779    case OP_NEG:
1780    case OP_SAT:
1781       emitCVT(insn);
1782       break;
1783    case OP_CVT:
1784       if (insn->def(0).getFile() == FILE_ADDRESS)
1785          emitARL(insn, 0);
1786       else
1787       if (insn->def(0).getFile() == FILE_FLAGS ||
1788           insn->src(0).getFile() == FILE_FLAGS ||
1789           insn->src(0).getFile() == FILE_ADDRESS)
1790          emitMOV(insn);
1791       else
1792          emitCVT(insn);
1793       break;
1794    case OP_RCP:
1795       emitSFnOp(insn, 0);
1796       break;
1797    case OP_RSQ:
1798       emitSFnOp(insn, 2);
1799       break;
1800    case OP_LG2:
1801       emitSFnOp(insn, 3);
1802       break;
1803    case OP_SIN:
1804       emitSFnOp(insn, 4);
1805       break;
1806    case OP_COS:
1807       emitSFnOp(insn, 5);
1808       break;
1809    case OP_EX2:
1810       emitSFnOp(insn, 6);
1811       break;
1812    case OP_PRESIN:
1813    case OP_PREEX2:
1814       emitPreOp(insn);
1815       break;
1816    case OP_TEX:
1817    case OP_TXB:
1818    case OP_TXL:
1819    case OP_TXF:
1820    case OP_TXG:
1821    case OP_TXLQ:
1822       emitTEX(insn->asTex());
1823       break;
1824    case OP_TXQ:
1825       emitTXQ(insn->asTex());
1826       break;
1827    case OP_TEXPREP:
1828       emitTEXPREP(insn->asTex());
1829       break;
1830    case OP_EMIT:
1831    case OP_RESTART:
1832       emitOUT(insn);
1833       break;
1834    case OP_DISCARD:
1835       emitFlow(insn, 0x0);
1836       break;
1837    case OP_BRA:
1838       emitFlow(insn, 0x1);
1839       break;
1840    case OP_CALL:
1841       emitFlow(insn, 0x2);
1842       break;
1843    case OP_RET:
1844       emitFlow(insn, 0x3);
1845       break;
1846    case OP_PREBREAK:
1847       emitFlow(insn, 0x4);
1848       break;
1849    case OP_BREAK:
1850       emitFlow(insn, 0x5);
1851       break;
1852    case OP_QUADON:
1853       emitFlow(insn, 0x6);
1854       break;
1855    case OP_QUADPOP:
1856       emitFlow(insn, 0x7);
1857       break;
1858    case OP_JOINAT:
1859       emitFlow(insn, 0xa);
1860       break;
1861    case OP_PRERET:
1862       emitFlow(insn, 0xd);
1863       break;
1864    case OP_QUADOP:
1865       emitQUADOP(insn, insn->lanes, insn->subOp);
1866       break;
1867    case OP_DFDX:
1868       emitQUADOP(insn, 4, insn->src(0).mod.neg() ? 0x66 : 0x99);
1869       break;
1870    case OP_DFDY:
1871       emitQUADOP(insn, 5, insn->src(0).mod.neg() ? 0x5a : 0xa5);
1872       break;
1873    case OP_ATOM:
1874       emitATOM(insn);
1875       break;
1876    case OP_BAR:
1877       emitBAR(insn);
1878       break;
1879    case OP_PHI:
1880    case OP_UNION:
1881    case OP_CONSTRAINT:
1882       ERROR("operation should have been eliminated\n");
1883       return false;
1884    case OP_EXP:
1885    case OP_LOG:
1886    case OP_SQRT:
1887    case OP_POW:
1888    case OP_SELP:
1889    case OP_SLCT:
1890    case OP_TXD:
1891    case OP_PRECONT:
1892    case OP_CONT:
1893    case OP_POPCNT:
1894    case OP_INSBF:
1895    case OP_EXTBF:
1896       ERROR("operation should have been lowered\n");
1897       return false;
1898    default:
1899       ERROR("unknown op: %u\n", insn->op);
1900       return false;
1901    }
1902    if (insn->join || insn->op == OP_JOIN)
1903       code[1] |= 0x2;
1904    else
1905    if (insn->exit || insn->op == OP_EXIT)
1906       code[1] |= 0x1;
1907
1908    assert((insn->encSize == 8) == (code[0] & 1));
1909
1910    code += insn->encSize / 4;
1911    codeSize += insn->encSize;
1912    return true;
1913 }
1914
1915 uint32_t
1916 CodeEmitterNV50::getMinEncodingSize(const Instruction *i) const
1917 {
1918    const Target::OpInfo &info = targ->getOpInfo(i);
1919
1920    if (info.minEncSize > 4)
1921       return 8;
1922
1923    // check constraints on dst and src operands
1924    for (int d = 0; i->defExists(d); ++d) {
1925       if (i->def(d).rep()->reg.data.id > 63 ||
1926           i->def(d).rep()->reg.file != FILE_GPR)
1927          return 8;
1928    }
1929
1930    for (int s = 0; i->srcExists(s); ++s) {
1931       DataFile sf = i->src(s).getFile();
1932       if (sf != FILE_GPR)
1933          if (sf != FILE_SHADER_INPUT || progType != Program::TYPE_FRAGMENT)
1934             return 8;
1935       if (i->src(s).rep()->reg.data.id > 63)
1936          return 8;
1937    }
1938
1939    // check modifiers & rounding
1940    if (i->join || i->lanes != 0xf || i->exit)
1941       return 8;
1942    if (i->op == OP_MUL && i->rnd != ROUND_N)
1943       return 8;
1944
1945    if (i->asTex())
1946       return 8; // TODO: short tex encoding
1947
1948    // check constraints on short MAD
1949    if (info.srcNr >= 2 && i->srcExists(2)) {
1950       if (!i->defExists(0) || !isFloatType(i->dType) ||
1951           i->def(0).rep()->reg.data.id != i->src(2).rep()->reg.data.id)
1952          return 8;
1953    }
1954
1955    return info.minEncSize;
1956 }
1957
1958 // Change the encoding size of an instruction after BBs have been scheduled.
1959 static void
1960 makeInstructionLong(Instruction *insn)
1961 {
1962    if (insn->encSize == 8)
1963       return;
1964    Function *fn = insn->bb->getFunction();
1965    int n = 0;
1966    int adj = 4;
1967
1968    for (Instruction *i = insn->next; i && i->encSize == 4; ++n, i = i->next);
1969
1970    if (n & 1) {
1971       adj = 8;
1972       insn->next->encSize = 8;
1973    } else
1974    if (insn->prev && insn->prev->encSize == 4) {
1975       adj = 8;
1976       insn->prev->encSize = 8;
1977    }
1978    insn->encSize = 8;
1979
1980    for (int i = fn->bbCount - 1; i >= 0 && fn->bbArray[i] != insn->bb; --i) {
1981       fn->bbArray[i]->binPos += 4;
1982    }
1983    fn->binSize += adj;
1984    insn->bb->binSize += adj;
1985 }
1986
1987 static bool
1988 trySetExitModifier(Instruction *insn)
1989 {
1990    if (insn->op == OP_DISCARD ||
1991        insn->op == OP_QUADON ||
1992        insn->op == OP_QUADPOP)
1993       return false;
1994    for (int s = 0; insn->srcExists(s); ++s)
1995       if (insn->src(s).getFile() == FILE_IMMEDIATE)
1996          return false;
1997    if (insn->asFlow()) {
1998       if (insn->op == OP_CALL) // side effects !
1999          return false;
2000       if (insn->getPredicate()) // cannot do conditional exit (or can we ?)
2001          return false;
2002       insn->op = OP_EXIT;
2003    }
2004    insn->exit = 1;
2005    makeInstructionLong(insn);
2006    return true;
2007 }
2008
2009 static void
2010 replaceExitWithModifier(Function *func)
2011 {
2012    BasicBlock *epilogue = BasicBlock::get(func->cfgExit);
2013
2014    if (!epilogue->getExit() ||
2015        epilogue->getExit()->op != OP_EXIT) // only main will use OP_EXIT
2016       return;
2017
2018    if (epilogue->getEntry()->op != OP_EXIT) {
2019       Instruction *insn = epilogue->getExit()->prev;
2020       if (!insn || !trySetExitModifier(insn))
2021          return;
2022       insn->exit = 1;
2023    } else {
2024       for (Graph::EdgeIterator ei = func->cfgExit->incident();
2025            !ei.end(); ei.next()) {
2026          BasicBlock *bb = BasicBlock::get(ei.getNode());
2027          Instruction *i = bb->getExit();
2028
2029          if (!i || !trySetExitModifier(i))
2030             return;
2031       }
2032    }
2033    epilogue->binSize -= 8;
2034    func->binSize -= 8;
2035    delete_Instruction(func->getProgram(), epilogue->getExit());
2036 }
2037
2038 void
2039 CodeEmitterNV50::prepareEmission(Function *func)
2040 {
2041    CodeEmitter::prepareEmission(func);
2042
2043    replaceExitWithModifier(func);
2044 }
2045
2046 CodeEmitterNV50::CodeEmitterNV50(const TargetNV50 *target) :
2047    CodeEmitter(target), targNV50(target)
2048 {
2049    targ = target; // specialized
2050    code = NULL;
2051    codeSize = codeSizeLimit = 0;
2052    relocInfo = NULL;
2053 }
2054
2055 CodeEmitter *
2056 TargetNV50::getCodeEmitter(Program::Type type)
2057 {
2058    CodeEmitterNV50 *emit = new CodeEmitterNV50(this);
2059    emit->setProgramType(type);
2060    return emit;
2061 }
2062
2063 } // namespace nv50_ir