nv50/ir: Add support for unlimited instruction arguments.
[mesa.git] / src / gallium / drivers / nv50 / codegen / nv50_ir.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 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
20 * SOFTWARE.
21 */
22
23 #include "nv50_ir.h"
24 #include "nv50_ir_target.h"
25 #include "nv50_ir_driver.h"
26
27 extern "C" {
28 #include "nv50/nv50_program.h"
29 #include "nv50/nv50_debug.h"
30 }
31
32 namespace nv50_ir {
33
34 Modifier::Modifier(operation op)
35 {
36 switch (op) {
37 case OP_NEG: bits = NV50_IR_MOD_NEG; break;
38 case OP_ABS: bits = NV50_IR_MOD_ABS; break;
39 case OP_SAT: bits = NV50_IR_MOD_SAT; break;
40 case OP_NOT: bits = NV50_IR_MOD_NOT; break;
41 default:
42 bits = 0;
43 break;
44 }
45 }
46
47 Modifier Modifier::operator*(const Modifier m) const
48 {
49 unsigned int a, b, c;
50
51 b = m.bits;
52 if (this->bits & NV50_IR_MOD_ABS)
53 b &= ~NV50_IR_MOD_NEG;
54
55 a = (this->bits ^ b) & (NV50_IR_MOD_NOT | NV50_IR_MOD_NEG);
56 c = (this->bits | m.bits) & (NV50_IR_MOD_ABS | NV50_IR_MOD_SAT);
57
58 return Modifier(a | c);
59 }
60
61 ValueRef::ValueRef() : value(NULL), insn(NULL)
62 {
63 indirect[0] = -1;
64 indirect[1] = -1;
65 usedAsPtr = false;
66 }
67
68 ValueRef::ValueRef(const ValueRef& ref) : value(NULL), insn(ref.insn)
69 {
70 set(ref);
71 usedAsPtr = ref.usedAsPtr;
72 }
73
74 ValueRef::~ValueRef()
75 {
76 this->set(NULL);
77 }
78
79 ImmediateValue *ValueRef::getImmediate() const
80 {
81 Value *src = value;
82
83 while (src) {
84 if (src->reg.file == FILE_IMMEDIATE)
85 return src->asImm();
86
87 Instruction *insn = src->getUniqueInsn();
88
89 src = (insn && insn->op == OP_MOV) ? insn->getSrc(0) : NULL;
90 }
91 return NULL;
92 }
93
94 ValueDef::ValueDef() : value(NULL), insn(NULL)
95 {
96 // nothing to do
97 }
98
99 ValueDef::ValueDef(const ValueDef& def) : value(NULL), insn(NULL)
100 {
101 set(def.get());
102 }
103
104 ValueDef::~ValueDef()
105 {
106 this->set(NULL);
107 }
108
109 void
110 ValueRef::set(const ValueRef &ref)
111 {
112 this->set(ref.get());
113 mod = ref.mod;
114 indirect[0] = ref.indirect[0];
115 indirect[1] = ref.indirect[1];
116 }
117
118 void
119 ValueRef::set(Value *refVal)
120 {
121 if (value == refVal)
122 return;
123 if (value)
124 value->uses.remove(this);
125 if (refVal)
126 refVal->uses.push_back(this);
127
128 value = refVal;
129 }
130
131 void
132 ValueDef::set(Value *defVal)
133 {
134 if (value == defVal)
135 return;
136 if (value)
137 value->defs.remove(this);
138 if (defVal)
139 defVal->defs.push_back(this);
140
141 value = defVal;
142 }
143
144 void
145 ValueDef::replace(Value *repVal, bool doSet)
146 {
147 if (value == repVal)
148 return;
149
150 while (value->refCount())
151 value->uses.front()->set(repVal);
152
153 if (doSet)
154 set(repVal);
155 }
156
157 Value::Value()
158 {
159 join = this;
160 memset(&reg, 0, sizeof(reg));
161 reg.size = 4;
162 }
163
164 bool
165 Value::coalesce(Value *jval, bool force)
166 {
167 Value *repr = this->join; // new representative
168 Value *jrep = jval->join;
169
170 if (reg.file != jval->reg.file || reg.size != jval->reg.size) {
171 if (!force)
172 return false;
173 ERROR("forced coalescing of values of different sizes/files");
174 }
175
176 if (!force && (repr->reg.data.id != jrep->reg.data.id)) {
177 if (repr->reg.data.id >= 0 &&
178 jrep->reg.data.id >= 0)
179 return false;
180 if (jrep->reg.data.id >= 0) {
181 repr = jval->join;
182 jrep = this->join;
183 jval = this;
184 }
185
186 // need to check all fixed register values of the program for overlap
187 Function *func = defs.front()->getInsn()->bb->getFunction();
188
189 // TODO: put values in by register-id bins per function
190 ArrayList::Iterator iter = func->allLValues.iterator();
191 for (; !iter.end(); iter.next()) {
192 Value *fixed = reinterpret_cast<Value *>(iter.get());
193 assert(fixed);
194 if (fixed->reg.data.id == repr->reg.data.id)
195 if (fixed->livei.overlaps(jrep->livei))
196 return false;
197 }
198 }
199 if (repr->livei.overlaps(jrep->livei)) {
200 if (!force)
201 return false;
202 // do we really want this ? if at all, only for constraint ops
203 INFO("NOTE: forced coalescing with live range overlap\n");
204 }
205
206 for (DefIterator it = jrep->defs.begin(); it != jrep->defs.end(); ++it)
207 (*it)->get()->join = repr;
208
209 repr->defs.insert(repr->defs.end(),
210 jrep->defs.begin(), jrep->defs.end());
211 repr->livei.unify(jrep->livei);
212
213 assert(repr->join == repr && jval->join == repr);
214 return true;
215 }
216
217 LValue::LValue(Function *fn, DataFile file)
218 {
219 reg.file = file;
220 reg.size = (file != FILE_PREDICATE) ? 4 : 1;
221 reg.data.id = -1;
222
223 affinity = -1;
224
225 fn->add(this, this->id);
226 }
227
228 LValue::LValue(Function *fn, LValue *lval)
229 {
230 assert(lval);
231
232 reg.file = lval->reg.file;
233 reg.size = lval->reg.size;
234 reg.data.id = -1;
235
236 affinity = -1;
237
238 fn->add(this, this->id);
239 }
240
241 Value *LValue::clone(Function *func) const
242 {
243 LValue *that = new_LValue(func, reg.file);
244
245 that->reg.size = this->reg.size;
246 that->reg.type = this->reg.type;
247 that->reg.data = this->reg.data;
248
249 return that;
250 }
251
252 Symbol::Symbol(Program *prog, DataFile f, ubyte fidx)
253 {
254 baseSym = NULL;
255
256 reg.file = f;
257 reg.fileIndex = fidx;
258 reg.data.offset = 0;
259
260 prog->add(this, this->id);
261 }
262
263 Value *
264 Symbol::clone(Function *func) const
265 {
266 Program *prog = func->getProgram();
267
268 Symbol *that = new_Symbol(prog, reg.file, reg.fileIndex);
269
270 that->reg.size = this->reg.size;
271 that->reg.type = this->reg.type;
272 that->reg.data = this->reg.data;
273
274 that->baseSym = this->baseSym;
275
276 return that;
277 }
278
279 ImmediateValue::ImmediateValue(Program *prog, uint32_t uval)
280 {
281 memset(&reg, 0, sizeof(reg));
282
283 reg.file = FILE_IMMEDIATE;
284 reg.size = 4;
285 reg.type = TYPE_U32;
286
287 reg.data.u32 = uval;
288
289 prog->add(this, this->id);
290 }
291
292 ImmediateValue::ImmediateValue(Program *prog, float fval)
293 {
294 memset(&reg, 0, sizeof(reg));
295
296 reg.file = FILE_IMMEDIATE;
297 reg.size = 4;
298 reg.type = TYPE_F32;
299
300 reg.data.f32 = fval;
301
302 prog->add(this, this->id);
303 }
304
305 ImmediateValue::ImmediateValue(Program *prog, double dval)
306 {
307 memset(&reg, 0, sizeof(reg));
308
309 reg.file = FILE_IMMEDIATE;
310 reg.size = 8;
311 reg.type = TYPE_F64;
312
313 reg.data.f64 = dval;
314
315 prog->add(this, this->id);
316 }
317
318 ImmediateValue::ImmediateValue(const ImmediateValue *proto, DataType ty)
319 {
320 reg = proto->reg;
321
322 reg.type = ty;
323 reg.size = typeSizeof(ty);
324 }
325
326 bool
327 ImmediateValue::isInteger(const int i) const
328 {
329 switch (reg.type) {
330 case TYPE_S8:
331 return reg.data.s8 == i;
332 case TYPE_U8:
333 return reg.data.u8 == i;
334 case TYPE_S16:
335 return reg.data.s16 == i;
336 case TYPE_U16:
337 return reg.data.u16 == i;
338 case TYPE_S32:
339 case TYPE_U32:
340 return reg.data.s32 == i; // as if ...
341 case TYPE_F32:
342 return reg.data.f32 == static_cast<float>(i);
343 case TYPE_F64:
344 return reg.data.f64 == static_cast<double>(i);
345 default:
346 return false;
347 }
348 }
349
350 bool
351 ImmediateValue::isNegative() const
352 {
353 switch (reg.type) {
354 case TYPE_S8: return reg.data.s8 < 0;
355 case TYPE_S16: return reg.data.s16 < 0;
356 case TYPE_S32:
357 case TYPE_U32: return reg.data.s32 < 0;
358 case TYPE_F32: return reg.data.u32 & (1 << 31);
359 case TYPE_F64: return reg.data.u64 & (1ULL << 63);
360 default:
361 return false;
362 }
363 }
364
365 bool
366 ImmediateValue::isPow2() const
367 {
368 switch (reg.type) {
369 case TYPE_U8:
370 case TYPE_U16:
371 case TYPE_U32: return util_is_power_of_two(reg.data.u32);
372 default:
373 return false;
374 }
375 }
376
377 void
378 ImmediateValue::applyLog2()
379 {
380 switch (reg.type) {
381 case TYPE_S8:
382 case TYPE_S16:
383 case TYPE_S32:
384 assert(!this->isNegative());
385 // fall through
386 case TYPE_U8:
387 case TYPE_U16:
388 case TYPE_U32:
389 reg.data.u32 = util_logbase2(reg.data.u32);
390 break;
391 case TYPE_F32:
392 reg.data.f32 = log2f(reg.data.f32);
393 break;
394 case TYPE_F64:
395 reg.data.f64 = log2(reg.data.f64);
396 break;
397 default:
398 assert(0);
399 break;
400 }
401 }
402
403 bool
404 ImmediateValue::compare(CondCode cc, float fval) const
405 {
406 if (reg.type != TYPE_F32)
407 ERROR("immediate value is not of type f32");
408
409 switch (static_cast<CondCode>(cc & 7)) {
410 case CC_TR: return true;
411 case CC_FL: return false;
412 case CC_LT: return reg.data.f32 < fval;
413 case CC_LE: return reg.data.f32 <= fval;
414 case CC_GT: return reg.data.f32 > fval;
415 case CC_GE: return reg.data.f32 >= fval;
416 case CC_EQ: return reg.data.f32 == fval;
417 case CC_NE: return reg.data.f32 != fval;
418 default:
419 assert(0);
420 return false;
421 }
422 }
423
424 bool
425 Value::interfers(const Value *that) const
426 {
427 uint32_t idA, idB;
428
429 if (that->reg.file != reg.file || that->reg.fileIndex != reg.fileIndex)
430 return false;
431 if (this->asImm())
432 return false;
433
434 if (this->asSym()) {
435 idA = this->join->reg.data.offset;
436 idB = that->join->reg.data.offset;
437 } else {
438 idA = this->join->reg.data.id * this->reg.size;
439 idB = that->join->reg.data.id * that->reg.size;
440 }
441
442 if (idA < idB)
443 return (idA + this->reg.size > idB);
444 else
445 if (idA > idB)
446 return (idB + that->reg.size > idA);
447 else
448 return (idA == idB);
449 }
450
451 bool
452 Value::equals(const Value *that, bool strict) const
453 {
454 that = that->join;
455
456 if (strict)
457 return this == that;
458
459 if (that->reg.file != reg.file || that->reg.fileIndex != reg.fileIndex)
460 return false;
461 if (that->reg.size != this->reg.size)
462 return false;
463
464 if (that->reg.data.id != this->reg.data.id)
465 return false;
466
467 return true;
468 }
469
470 bool
471 ImmediateValue::equals(const Value *that, bool strict) const
472 {
473 const ImmediateValue *imm = that->asImm();
474 if (!imm)
475 return false;
476 return reg.data.u64 == imm->reg.data.u64;
477 }
478
479 bool
480 Symbol::equals(const Value *that, bool strict) const
481 {
482 if (reg.file != that->reg.file || reg.fileIndex != that->reg.fileIndex)
483 return false;
484 assert(that->asSym());
485
486 if (this->baseSym != that->asSym()->baseSym)
487 return false;
488
489 return this->reg.data.offset == that->reg.data.offset;
490 }
491
492 void Instruction::init()
493 {
494 next = prev = 0;
495
496 cc = CC_ALWAYS;
497 rnd = ROUND_N;
498 cache = CACHE_CA;
499 subOp = 0;
500
501 saturate = 0;
502 join = terminator = 0;
503 ftz = dnz = 0;
504 atomic = 0;
505 perPatch = 0;
506 fixed = 0;
507 encSize = 0;
508 ipa = 0;
509
510 lanes = 0xf;
511
512 postFactor = 0;
513
514 predSrc = -1;
515 flagsDef = -1;
516 flagsSrc = -1;
517 }
518
519 Instruction::Instruction()
520 {
521 init();
522
523 op = OP_NOP;
524 dType = sType = TYPE_F32;
525
526 id = -1;
527 bb = 0;
528 }
529
530 Instruction::Instruction(Function *fn, operation opr, DataType ty)
531 {
532 init();
533
534 op = opr;
535 dType = sType = ty;
536
537 fn->add(this, id);
538 }
539
540 Instruction::~Instruction()
541 {
542 if (bb) {
543 Function *fn = bb->getFunction();
544 bb->remove(this);
545 fn->allInsns.remove(id);
546 }
547
548 for (int s = 0; srcExists(s); ++s)
549 setSrc(s, NULL);
550 // must unlink defs too since the list pointers will get deallocated
551 for (int d = 0; defExists(d); ++d)
552 setDef(d, NULL);
553 }
554
555 void
556 Instruction::setDef(int i, Value *val)
557 {
558 int size = def.size();
559 if (i >= size) {
560 def.resize(i + 1);
561 while (size <= i)
562 def[size++].setInsn(this);
563 }
564 def[i].set(val);
565 }
566
567 void
568 Instruction::setSrc(int s, Value *val)
569 {
570 int size = src.size();
571 if (s >= size) {
572 src.resize(s + 1);
573 while (size <= s)
574 src[size++].setInsn(this);
575 }
576 src[s].set(val);
577 }
578
579 void
580 Instruction::setSrc(int s, const ValueRef& ref)
581 {
582 setSrc(s, ref.get());
583 src[s].mod = ref.mod;
584 }
585
586 void
587 Instruction::swapSources(int a, int b)
588 {
589 Value *value = src[a].get();
590 Modifier m = src[a].mod;
591
592 setSrc(a, src[b]);
593
594 src[b].set(value);
595 src[b].mod = m;
596 }
597
598 void
599 Instruction::takeExtraSources(int s, Value *values[3])
600 {
601 values[0] = getIndirect(s, 0);
602 if (values[0])
603 setIndirect(s, 0, NULL);
604
605 values[1] = getIndirect(s, 1);
606 if (values[1])
607 setIndirect(s, 1, NULL);
608
609 values[2] = getPredicate();
610 if (values[2])
611 setPredicate(cc, NULL);
612 }
613
614 void
615 Instruction::putExtraSources(int s, Value *values[3])
616 {
617 if (values[0])
618 setIndirect(s, 0, values[0]);
619 if (values[1])
620 setIndirect(s, 1, values[1]);
621 if (values[2])
622 setPredicate(cc, values[2]);
623 }
624
625 Instruction *
626 Instruction::clone(bool deep) const
627 {
628 Instruction *insn = new_Instruction(bb->getFunction(), op, dType);
629 assert(!asCmp() && !asFlow());
630 cloneBase(insn, deep);
631 return insn;
632 }
633
634 void
635 Instruction::cloneBase(Instruction *insn, bool deep) const
636 {
637 insn->sType = this->sType;
638
639 insn->cc = this->cc;
640 insn->rnd = this->rnd;
641 insn->cache = this->cache;
642 insn->subOp = this->subOp;
643
644 insn->saturate = this->saturate;
645 insn->atomic = this->atomic;
646 insn->ftz = this->ftz;
647 insn->dnz = this->dnz;
648 insn->ipa = this->ipa;
649 insn->lanes = this->lanes;
650 insn->perPatch = this->perPatch;
651
652 insn->postFactor = this->postFactor;
653
654 if (deep) {
655 if (!bb)
656 return;
657 Function *fn = bb->getFunction();
658 for (int d = 0; this->defExists(d); ++d)
659 insn->setDef(d, this->getDef(d)->clone(fn));
660 } else {
661 for (int d = 0; this->defExists(d); ++d)
662 insn->setDef(d, this->getDef(d));
663 }
664
665 for (int s = 0; this->srcExists(s); ++s)
666 insn->setSrc(s, this->src[s]);
667
668 insn->predSrc = this->predSrc;
669 insn->flagsDef = this->flagsDef;
670 insn->flagsSrc = this->flagsSrc;
671 }
672
673 unsigned int
674 Instruction::defCount(unsigned int mask) const
675 {
676 unsigned int i, n;
677
678 for (n = 0, i = 0; this->defExists(i); ++i, mask >>= 1)
679 n += mask & 1;
680 return n;
681 }
682
683 unsigned int
684 Instruction::srcCount(unsigned int mask) const
685 {
686 unsigned int i, n;
687
688 for (n = 0, i = 0; this->srcExists(i); ++i, mask >>= 1)
689 n += mask & 1;
690 return n;
691 }
692
693 bool
694 Instruction::setIndirect(int s, int dim, Value *value)
695 {
696 assert(this->srcExists(s));
697
698 int p = src[s].indirect[dim];
699 if (p < 0) {
700 if (!value)
701 return true;
702 p = src.size();
703 }
704 setSrc(p, value);
705 src[p].usedAsPtr = (value != 0);
706 src[s].indirect[dim] = value ? p : -1;
707 return true;
708 }
709
710 bool
711 Instruction::setPredicate(CondCode ccode, Value *value)
712 {
713 cc = ccode;
714
715 if (!value) {
716 if (predSrc >= 0) {
717 src[predSrc] = 0;
718 predSrc = -1;
719 }
720 return true;
721 }
722
723 if (predSrc < 0)
724 predSrc = src.size();
725
726 setSrc(predSrc, value);
727 return true;
728 }
729
730 bool
731 Instruction::writesPredicate() const
732 {
733 for (int d = 0; defExists(d); ++d)
734 if (getDef(d)->inFile(FILE_PREDICATE) || getDef(d)->inFile(FILE_FLAGS))
735 return true;
736 return false;
737 }
738
739 static bool
740 insnCheckCommutation(const Instruction *a, const Instruction *b)
741 {
742 for (int d = 0; a->defExists(d); ++d)
743 for (int s = 0; b->srcExists(s); ++s)
744 if (a->getDef(d)->interfers(b->getSrc(s)))
745 return false;
746 return true;
747 }
748
749 bool
750 Instruction::isCommutationLegal(const Instruction *i) const
751 {
752 bool ret = true;
753 ret = ret && insnCheckCommutation(this, i);
754 ret = ret && insnCheckCommutation(i, this);
755 return ret;
756 }
757
758 TexInstruction::TexInstruction(Function *fn, operation op)
759 : Instruction(fn, op, TYPE_F32)
760 {
761 memset(&tex, 0, sizeof(tex));
762
763 tex.rIndirectSrc = -1;
764 tex.sIndirectSrc = -1;
765 }
766
767 TexInstruction::~TexInstruction()
768 {
769 for (int c = 0; c < 3; ++c) {
770 dPdx[c].set(NULL);
771 dPdy[c].set(NULL);
772 }
773 }
774
775 Instruction *
776 TexInstruction::clone(bool deep) const
777 {
778 TexInstruction *tex = new_TexInstruction(bb->getFunction(), op);
779 cloneBase(tex, deep);
780
781 tex->tex = this->tex;
782
783 if (op == OP_TXD) {
784 for (unsigned int c = 0; c < tex->tex.target.getDim(); ++c) {
785 tex->dPdx[c].set(dPdx[c]);
786 tex->dPdy[c].set(dPdy[c]);
787 }
788 }
789
790 return tex;
791 }
792
793 const struct TexInstruction::Target::Desc TexInstruction::Target::descTable[] =
794 {
795 { "1D", 1, 1, false, false, false },
796 { "2D", 2, 2, false, false, false },
797 { "2D_MS", 2, 2, false, false, false },
798 { "3D", 3, 3, false, false, false },
799 { "CUBE", 2, 3, false, true, false },
800 { "1D_SHADOW", 1, 1, false, false, true },
801 { "2D_SHADOW", 2, 2, false, false, true },
802 { "CUBE_SHADOW", 2, 3, false, true, true },
803 { "1D_ARRAY", 1, 2, true, false, false },
804 { "2D_ARRAY", 2, 3, true, false, false },
805 { "2D_MS_ARRAY", 2, 3, true, false, false },
806 { "CUBE_ARRAY", 2, 4, true, true, false },
807 { "1D_ARRAY_SHADOW", 1, 2, true, false, true },
808 { "2D_ARRAY_SHADOW", 2, 3, true, false, true },
809 { "RECT", 2, 2, false, false, false },
810 { "RECT_SHADOW", 2, 2, false, false, true },
811 { "CUBE_ARRAY_SHADOW", 2, 4, true, true, true },
812 { "BUFFER", 1, 1, false, false, false },
813 };
814
815 CmpInstruction::CmpInstruction(Function *fn, operation op)
816 : Instruction(fn, op, TYPE_F32)
817 {
818 setCond = CC_ALWAYS;
819 }
820
821 Instruction *
822 CmpInstruction::clone(bool deep) const
823 {
824 CmpInstruction *cmp = new_CmpInstruction(bb->getFunction(), op);
825 cloneBase(cmp, deep);
826 cmp->setCond = setCond;
827 cmp->dType = dType;
828 return cmp;
829 }
830
831 FlowInstruction::FlowInstruction(Function *fn, operation op,
832 BasicBlock *targ)
833 : Instruction(fn, op, TYPE_NONE)
834 {
835 target.bb = targ;
836
837 if (op == OP_BRA ||
838 op == OP_CONT || op == OP_BREAK ||
839 op == OP_RET || op == OP_EXIT)
840 terminator = 1;
841 else
842 if (op == OP_JOIN)
843 terminator = targ ? 1 : 0;
844
845 allWarp = absolute = limit = 0;
846 }
847
848 Program::Program(Type type, Target *arch)
849 : progType(type),
850 target(arch),
851 mem_Instruction(sizeof(Instruction), 6),
852 mem_CmpInstruction(sizeof(CmpInstruction), 4),
853 mem_TexInstruction(sizeof(TexInstruction), 4),
854 mem_FlowInstruction(sizeof(FlowInstruction), 4),
855 mem_LValue(sizeof(LValue), 8),
856 mem_Symbol(sizeof(Symbol), 7),
857 mem_ImmediateValue(sizeof(ImmediateValue), 7)
858 {
859 code = NULL;
860 binSize = 0;
861
862 maxGPR = -1;
863
864 main = new Function(this, "MAIN");
865
866 dbgFlags = 0;
867 }
868
869 Program::~Program()
870 {
871 if (main)
872 delete main;
873 }
874
875 void Program::releaseInstruction(Instruction *insn)
876 {
877 // TODO: make this not suck so much
878
879 insn->~Instruction();
880
881 if (insn->asCmp())
882 mem_CmpInstruction.release(insn);
883 else
884 if (insn->asTex())
885 mem_TexInstruction.release(insn);
886 else
887 if (insn->asFlow())
888 mem_FlowInstruction.release(insn);
889 else
890 mem_Instruction.release(insn);
891 }
892
893 void Program::releaseValue(Value *value)
894 {
895 if (value->asLValue())
896 mem_LValue.release(value);
897 else
898 if (value->asImm())
899 mem_ImmediateValue.release(value);
900 else
901 if (value->asSym())
902 mem_Symbol.release(value);
903 }
904
905
906 } // namespace nv50_ir
907
908 extern "C" {
909
910 static void
911 nv50_ir_init_prog_info(struct nv50_ir_prog_info *info)
912 {
913 #if defined(PIPE_SHADER_HULL) && defined(PIPE_SHADER_DOMAIN)
914 if (info->type == PIPE_SHADER_HULL || info->type == PIPE_SHADER_DOMAIN) {
915 info->prop.tp.domain = PIPE_PRIM_MAX;
916 info->prop.tp.outputPrim = PIPE_PRIM_MAX;
917 }
918 #endif
919 if (info->type == PIPE_SHADER_GEOMETRY) {
920 info->prop.gp.instanceCount = 1;
921 info->prop.gp.maxVertices = 1;
922 }
923 info->io.clipDistance = 0xff;
924 info->io.pointSize = 0xff;
925 info->io.vertexId = 0xff;
926 info->io.edgeFlagIn = 0xff;
927 info->io.edgeFlagOut = 0xff;
928 info->io.fragDepth = 0xff;
929 info->io.sampleMask = 0xff;
930 info->io.backFaceColor[0] = info->io.backFaceColor[1] = 0xff;
931 }
932
933 int
934 nv50_ir_generate_code(struct nv50_ir_prog_info *info)
935 {
936 int ret = 0;
937
938 nv50_ir::Program::Type type;
939
940 nv50_ir_init_prog_info(info);
941
942 #define PROG_TYPE_CASE(a, b) \
943 case PIPE_SHADER_##a: type = nv50_ir::Program::TYPE_##b; break
944
945 switch (info->type) {
946 PROG_TYPE_CASE(VERTEX, VERTEX);
947 // PROG_TYPE_CASE(HULL, TESSELLATION_CONTROL);
948 // PROG_TYPE_CASE(DOMAIN, TESSELLATION_EVAL);
949 PROG_TYPE_CASE(GEOMETRY, GEOMETRY);
950 PROG_TYPE_CASE(FRAGMENT, FRAGMENT);
951 default:
952 type = nv50_ir::Program::TYPE_COMPUTE;
953 break;
954 }
955 INFO_DBG(info->dbgFlags, VERBOSE, "translating program of type %u\n", type);
956
957 nv50_ir::Target *targ = nv50_ir::Target::create(info->target);
958 if (!targ)
959 return -1;
960
961 nv50_ir::Program *prog = new nv50_ir::Program(type, targ);
962 if (!prog)
963 return -1;
964 prog->dbgFlags = info->dbgFlags;
965
966 switch (info->bin.sourceRep) {
967 #if 0
968 case PIPE_IR_LLVM:
969 case PIPE_IR_GLSL:
970 return -1;
971 case PIPE_IR_SM4:
972 ret = prog->makeFromSM4(info) ? 0 : -2;
973 break;
974 case PIPE_IR_TGSI:
975 #endif
976 default:
977 ret = prog->makeFromTGSI(info) ? 0 : -2;
978 break;
979 }
980 if (ret < 0)
981 goto out;
982 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
983 prog->print();
984
985 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_PRE_SSA);
986
987 prog->convertToSSA();
988
989 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
990 prog->print();
991
992 prog->optimizeSSA(info->optLevel);
993 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_SSA);
994
995 if (prog->dbgFlags & NV50_IR_DEBUG_BASIC)
996 prog->print();
997
998 if (!prog->registerAllocation()) {
999 ret = -4;
1000 goto out;
1001 }
1002 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_POST_RA);
1003
1004 prog->optimizePostRA(info->optLevel);
1005
1006 if (!prog->emitBinary(info)) {
1007 ret = -5;
1008 goto out;
1009 }
1010
1011 out:
1012 INFO_DBG(prog->dbgFlags, VERBOSE, "nv50_ir_generate_code: ret = %i\n", ret);
1013
1014 info->bin.maxGPR = prog->maxGPR;
1015 info->bin.code = prog->code;
1016 info->bin.codeSize = prog->binSize;
1017
1018 delete prog;
1019 nv50_ir::Target::destroy(targ);
1020
1021 return ret;
1022 }
1023
1024 } // extern "C"