nv50/ir: make Instruction::src/def container private
[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 = defs.size();
559 if (i >= size) {
560 defs.resize(i + 1);
561 while (size <= i)
562 defs[size++].setInsn(this);
563 }
564 defs[i].set(val);
565 }
566
567 void
568 Instruction::setSrc(int s, Value *val)
569 {
570 int size = srcs.size();
571 if (s >= size) {
572 srcs.resize(s + 1);
573 while (size <= s)
574 srcs[size++].setInsn(this);
575 }
576 srcs[s].set(val);
577 }
578
579 void
580 Instruction::setSrc(int s, const ValueRef& ref)
581 {
582 setSrc(s, ref.get());
583 srcs[s].mod = ref.mod;
584 }
585
586 void
587 Instruction::swapSources(int a, int b)
588 {
589 Value *value = srcs[a].get();
590 Modifier m = srcs[a].mod;
591
592 setSrc(a, srcs[b]);
593
594 srcs[b].set(value);
595 srcs[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->srcs[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 = srcs[s].indirect[dim];
699 if (p < 0) {
700 if (!value)
701 return true;
702 p = srcs.size();
703 while (p > 0 && !srcExists(p - 1))
704 --p;
705 }
706 setSrc(p, value);
707 srcs[p].usedAsPtr = (value != 0);
708 srcs[s].indirect[dim] = value ? p : -1;
709 return true;
710 }
711
712 bool
713 Instruction::setPredicate(CondCode ccode, Value *value)
714 {
715 cc = ccode;
716
717 if (!value) {
718 if (predSrc >= 0) {
719 srcs[predSrc].set(NULL);
720 predSrc = -1;
721 }
722 return true;
723 }
724
725 if (predSrc < 0) {
726 predSrc = srcs.size();
727 while (predSrc > 0 && !srcExists(predSrc - 1))
728 --predSrc;
729 }
730
731 setSrc(predSrc, value);
732 return true;
733 }
734
735 bool
736 Instruction::writesPredicate() const
737 {
738 for (int d = 0; defExists(d); ++d)
739 if (getDef(d)->inFile(FILE_PREDICATE) || getDef(d)->inFile(FILE_FLAGS))
740 return true;
741 return false;
742 }
743
744 static bool
745 insnCheckCommutation(const Instruction *a, const Instruction *b)
746 {
747 for (int d = 0; a->defExists(d); ++d)
748 for (int s = 0; b->srcExists(s); ++s)
749 if (a->getDef(d)->interfers(b->getSrc(s)))
750 return false;
751 return true;
752 }
753
754 bool
755 Instruction::isCommutationLegal(const Instruction *i) const
756 {
757 bool ret = true;
758 ret = ret && insnCheckCommutation(this, i);
759 ret = ret && insnCheckCommutation(i, this);
760 return ret;
761 }
762
763 TexInstruction::TexInstruction(Function *fn, operation op)
764 : Instruction(fn, op, TYPE_F32)
765 {
766 memset(&tex, 0, sizeof(tex));
767
768 tex.rIndirectSrc = -1;
769 tex.sIndirectSrc = -1;
770 }
771
772 TexInstruction::~TexInstruction()
773 {
774 for (int c = 0; c < 3; ++c) {
775 dPdx[c].set(NULL);
776 dPdy[c].set(NULL);
777 }
778 }
779
780 Instruction *
781 TexInstruction::clone(bool deep) const
782 {
783 TexInstruction *tex = new_TexInstruction(bb->getFunction(), op);
784 cloneBase(tex, deep);
785
786 tex->tex = this->tex;
787
788 if (op == OP_TXD) {
789 for (unsigned int c = 0; c < tex->tex.target.getDim(); ++c) {
790 tex->dPdx[c].set(dPdx[c]);
791 tex->dPdy[c].set(dPdy[c]);
792 }
793 }
794
795 return tex;
796 }
797
798 const struct TexInstruction::Target::Desc TexInstruction::Target::descTable[] =
799 {
800 { "1D", 1, 1, false, false, false },
801 { "2D", 2, 2, false, false, false },
802 { "2D_MS", 2, 2, false, false, false },
803 { "3D", 3, 3, false, false, false },
804 { "CUBE", 2, 3, false, true, false },
805 { "1D_SHADOW", 1, 1, false, false, true },
806 { "2D_SHADOW", 2, 2, false, false, true },
807 { "CUBE_SHADOW", 2, 3, false, true, true },
808 { "1D_ARRAY", 1, 2, true, false, false },
809 { "2D_ARRAY", 2, 3, true, false, false },
810 { "2D_MS_ARRAY", 2, 3, true, false, false },
811 { "CUBE_ARRAY", 2, 4, true, true, false },
812 { "1D_ARRAY_SHADOW", 1, 2, true, false, true },
813 { "2D_ARRAY_SHADOW", 2, 3, true, false, true },
814 { "RECT", 2, 2, false, false, false },
815 { "RECT_SHADOW", 2, 2, false, false, true },
816 { "CUBE_ARRAY_SHADOW", 2, 4, true, true, true },
817 { "BUFFER", 1, 1, false, false, false },
818 };
819
820 CmpInstruction::CmpInstruction(Function *fn, operation op)
821 : Instruction(fn, op, TYPE_F32)
822 {
823 setCond = CC_ALWAYS;
824 }
825
826 Instruction *
827 CmpInstruction::clone(bool deep) const
828 {
829 CmpInstruction *cmp = new_CmpInstruction(bb->getFunction(), op);
830 cloneBase(cmp, deep);
831 cmp->setCond = setCond;
832 cmp->dType = dType;
833 return cmp;
834 }
835
836 FlowInstruction::FlowInstruction(Function *fn, operation op,
837 BasicBlock *targ)
838 : Instruction(fn, op, TYPE_NONE)
839 {
840 target.bb = targ;
841
842 if (op == OP_BRA ||
843 op == OP_CONT || op == OP_BREAK ||
844 op == OP_RET || op == OP_EXIT)
845 terminator = 1;
846 else
847 if (op == OP_JOIN)
848 terminator = targ ? 1 : 0;
849
850 allWarp = absolute = limit = 0;
851 }
852
853 Program::Program(Type type, Target *arch)
854 : progType(type),
855 target(arch),
856 mem_Instruction(sizeof(Instruction), 6),
857 mem_CmpInstruction(sizeof(CmpInstruction), 4),
858 mem_TexInstruction(sizeof(TexInstruction), 4),
859 mem_FlowInstruction(sizeof(FlowInstruction), 4),
860 mem_LValue(sizeof(LValue), 8),
861 mem_Symbol(sizeof(Symbol), 7),
862 mem_ImmediateValue(sizeof(ImmediateValue), 7)
863 {
864 code = NULL;
865 binSize = 0;
866
867 maxGPR = -1;
868
869 main = new Function(this, "MAIN");
870
871 dbgFlags = 0;
872 }
873
874 Program::~Program()
875 {
876 if (main)
877 delete main;
878 }
879
880 void Program::releaseInstruction(Instruction *insn)
881 {
882 // TODO: make this not suck so much
883
884 insn->~Instruction();
885
886 if (insn->asCmp())
887 mem_CmpInstruction.release(insn);
888 else
889 if (insn->asTex())
890 mem_TexInstruction.release(insn);
891 else
892 if (insn->asFlow())
893 mem_FlowInstruction.release(insn);
894 else
895 mem_Instruction.release(insn);
896 }
897
898 void Program::releaseValue(Value *value)
899 {
900 if (value->asLValue())
901 mem_LValue.release(value);
902 else
903 if (value->asImm())
904 mem_ImmediateValue.release(value);
905 else
906 if (value->asSym())
907 mem_Symbol.release(value);
908 }
909
910
911 } // namespace nv50_ir
912
913 extern "C" {
914
915 static void
916 nv50_ir_init_prog_info(struct nv50_ir_prog_info *info)
917 {
918 #if defined(PIPE_SHADER_HULL) && defined(PIPE_SHADER_DOMAIN)
919 if (info->type == PIPE_SHADER_HULL || info->type == PIPE_SHADER_DOMAIN) {
920 info->prop.tp.domain = PIPE_PRIM_MAX;
921 info->prop.tp.outputPrim = PIPE_PRIM_MAX;
922 }
923 #endif
924 if (info->type == PIPE_SHADER_GEOMETRY) {
925 info->prop.gp.instanceCount = 1;
926 info->prop.gp.maxVertices = 1;
927 }
928 info->io.clipDistance = 0xff;
929 info->io.pointSize = 0xff;
930 info->io.vertexId = 0xff;
931 info->io.edgeFlagIn = 0xff;
932 info->io.edgeFlagOut = 0xff;
933 info->io.fragDepth = 0xff;
934 info->io.sampleMask = 0xff;
935 info->io.backFaceColor[0] = info->io.backFaceColor[1] = 0xff;
936 }
937
938 int
939 nv50_ir_generate_code(struct nv50_ir_prog_info *info)
940 {
941 int ret = 0;
942
943 nv50_ir::Program::Type type;
944
945 nv50_ir_init_prog_info(info);
946
947 #define PROG_TYPE_CASE(a, b) \
948 case PIPE_SHADER_##a: type = nv50_ir::Program::TYPE_##b; break
949
950 switch (info->type) {
951 PROG_TYPE_CASE(VERTEX, VERTEX);
952 // PROG_TYPE_CASE(HULL, TESSELLATION_CONTROL);
953 // PROG_TYPE_CASE(DOMAIN, TESSELLATION_EVAL);
954 PROG_TYPE_CASE(GEOMETRY, GEOMETRY);
955 PROG_TYPE_CASE(FRAGMENT, FRAGMENT);
956 default:
957 type = nv50_ir::Program::TYPE_COMPUTE;
958 break;
959 }
960 INFO_DBG(info->dbgFlags, VERBOSE, "translating program of type %u\n", type);
961
962 nv50_ir::Target *targ = nv50_ir::Target::create(info->target);
963 if (!targ)
964 return -1;
965
966 nv50_ir::Program *prog = new nv50_ir::Program(type, targ);
967 if (!prog)
968 return -1;
969 prog->dbgFlags = info->dbgFlags;
970
971 switch (info->bin.sourceRep) {
972 #if 0
973 case PIPE_IR_LLVM:
974 case PIPE_IR_GLSL:
975 return -1;
976 case PIPE_IR_SM4:
977 ret = prog->makeFromSM4(info) ? 0 : -2;
978 break;
979 case PIPE_IR_TGSI:
980 #endif
981 default:
982 ret = prog->makeFromTGSI(info) ? 0 : -2;
983 break;
984 }
985 if (ret < 0)
986 goto out;
987 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
988 prog->print();
989
990 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_PRE_SSA);
991
992 prog->convertToSSA();
993
994 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
995 prog->print();
996
997 prog->optimizeSSA(info->optLevel);
998 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_SSA);
999
1000 if (prog->dbgFlags & NV50_IR_DEBUG_BASIC)
1001 prog->print();
1002
1003 if (!prog->registerAllocation()) {
1004 ret = -4;
1005 goto out;
1006 }
1007 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_POST_RA);
1008
1009 prog->optimizePostRA(info->optLevel);
1010
1011 if (!prog->emitBinary(info)) {
1012 ret = -5;
1013 goto out;
1014 }
1015
1016 out:
1017 INFO_DBG(prog->dbgFlags, VERBOSE, "nv50_ir_generate_code: ret = %i\n", ret);
1018
1019 info->bin.maxGPR = prog->maxGPR;
1020 info->bin.code = prog->code;
1021 info->bin.codeSize = prog->binSize;
1022
1023 delete prog;
1024 nv50_ir::Target::destroy(targ);
1025
1026 return ret;
1027 }
1028
1029 } // extern "C"