8e7fc31eced5c34f423f333fccd035c13b5e290b
[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 LValue *
242 LValue::clone(ClonePolicy<Function>& pol) const
243 {
244 LValue *that = new_LValue(pol.context(), reg.file);
245
246 pol.set<Value>(this, that);
247
248 that->reg.size = this->reg.size;
249 that->reg.type = this->reg.type;
250 that->reg.data = this->reg.data;
251
252 return that;
253 }
254
255 Symbol::Symbol(Program *prog, DataFile f, ubyte fidx)
256 {
257 baseSym = NULL;
258
259 reg.file = f;
260 reg.fileIndex = fidx;
261 reg.data.offset = 0;
262
263 prog->add(this, this->id);
264 }
265
266 Symbol *
267 Symbol::clone(ClonePolicy<Function>& pol) const
268 {
269 Program *prog = pol.context()->getProgram();
270
271 Symbol *that = new_Symbol(prog, reg.file, reg.fileIndex);
272
273 pol.set<Value>(this, that);
274
275 that->reg.size = this->reg.size;
276 that->reg.type = this->reg.type;
277 that->reg.data = this->reg.data;
278
279 that->baseSym = this->baseSym;
280
281 return that;
282 }
283
284 ImmediateValue::ImmediateValue(Program *prog, uint32_t uval)
285 {
286 memset(&reg, 0, sizeof(reg));
287
288 reg.file = FILE_IMMEDIATE;
289 reg.size = 4;
290 reg.type = TYPE_U32;
291
292 reg.data.u32 = uval;
293
294 prog->add(this, this->id);
295 }
296
297 ImmediateValue::ImmediateValue(Program *prog, float fval)
298 {
299 memset(&reg, 0, sizeof(reg));
300
301 reg.file = FILE_IMMEDIATE;
302 reg.size = 4;
303 reg.type = TYPE_F32;
304
305 reg.data.f32 = fval;
306
307 prog->add(this, this->id);
308 }
309
310 ImmediateValue::ImmediateValue(Program *prog, double dval)
311 {
312 memset(&reg, 0, sizeof(reg));
313
314 reg.file = FILE_IMMEDIATE;
315 reg.size = 8;
316 reg.type = TYPE_F64;
317
318 reg.data.f64 = dval;
319
320 prog->add(this, this->id);
321 }
322
323 ImmediateValue::ImmediateValue(const ImmediateValue *proto, DataType ty)
324 {
325 reg = proto->reg;
326
327 reg.type = ty;
328 reg.size = typeSizeof(ty);
329 }
330
331 ImmediateValue *
332 ImmediateValue::clone(ClonePolicy<Function>& pol) const
333 {
334 Program *prog = pol.context()->getProgram();
335 ImmediateValue *that = new_ImmediateValue(prog, 0u);
336
337 pol.set<Value>(this, that);
338
339 that->reg.size = this->reg.size;
340 that->reg.type = this->reg.type;
341 that->reg.data = this->reg.data;
342
343 return that;
344 }
345
346 bool
347 ImmediateValue::isInteger(const int i) const
348 {
349 switch (reg.type) {
350 case TYPE_S8:
351 return reg.data.s8 == i;
352 case TYPE_U8:
353 return reg.data.u8 == i;
354 case TYPE_S16:
355 return reg.data.s16 == i;
356 case TYPE_U16:
357 return reg.data.u16 == i;
358 case TYPE_S32:
359 case TYPE_U32:
360 return reg.data.s32 == i; // as if ...
361 case TYPE_F32:
362 return reg.data.f32 == static_cast<float>(i);
363 case TYPE_F64:
364 return reg.data.f64 == static_cast<double>(i);
365 default:
366 return false;
367 }
368 }
369
370 bool
371 ImmediateValue::isNegative() const
372 {
373 switch (reg.type) {
374 case TYPE_S8: return reg.data.s8 < 0;
375 case TYPE_S16: return reg.data.s16 < 0;
376 case TYPE_S32:
377 case TYPE_U32: return reg.data.s32 < 0;
378 case TYPE_F32: return reg.data.u32 & (1 << 31);
379 case TYPE_F64: return reg.data.u64 & (1ULL << 63);
380 default:
381 return false;
382 }
383 }
384
385 bool
386 ImmediateValue::isPow2() const
387 {
388 switch (reg.type) {
389 case TYPE_U8:
390 case TYPE_U16:
391 case TYPE_U32: return util_is_power_of_two(reg.data.u32);
392 default:
393 return false;
394 }
395 }
396
397 void
398 ImmediateValue::applyLog2()
399 {
400 switch (reg.type) {
401 case TYPE_S8:
402 case TYPE_S16:
403 case TYPE_S32:
404 assert(!this->isNegative());
405 // fall through
406 case TYPE_U8:
407 case TYPE_U16:
408 case TYPE_U32:
409 reg.data.u32 = util_logbase2(reg.data.u32);
410 break;
411 case TYPE_F32:
412 reg.data.f32 = log2f(reg.data.f32);
413 break;
414 case TYPE_F64:
415 reg.data.f64 = log2(reg.data.f64);
416 break;
417 default:
418 assert(0);
419 break;
420 }
421 }
422
423 bool
424 ImmediateValue::compare(CondCode cc, float fval) const
425 {
426 if (reg.type != TYPE_F32)
427 ERROR("immediate value is not of type f32");
428
429 switch (static_cast<CondCode>(cc & 7)) {
430 case CC_TR: return true;
431 case CC_FL: return false;
432 case CC_LT: return reg.data.f32 < fval;
433 case CC_LE: return reg.data.f32 <= fval;
434 case CC_GT: return reg.data.f32 > fval;
435 case CC_GE: return reg.data.f32 >= fval;
436 case CC_EQ: return reg.data.f32 == fval;
437 case CC_NE: return reg.data.f32 != fval;
438 default:
439 assert(0);
440 return false;
441 }
442 }
443
444 bool
445 Value::interfers(const Value *that) const
446 {
447 uint32_t idA, idB;
448
449 if (that->reg.file != reg.file || that->reg.fileIndex != reg.fileIndex)
450 return false;
451 if (this->asImm())
452 return false;
453
454 if (this->asSym()) {
455 idA = this->join->reg.data.offset;
456 idB = that->join->reg.data.offset;
457 } else {
458 idA = this->join->reg.data.id * this->reg.size;
459 idB = that->join->reg.data.id * that->reg.size;
460 }
461
462 if (idA < idB)
463 return (idA + this->reg.size > idB);
464 else
465 if (idA > idB)
466 return (idB + that->reg.size > idA);
467 else
468 return (idA == idB);
469 }
470
471 bool
472 Value::equals(const Value *that, bool strict) const
473 {
474 that = that->join;
475
476 if (strict)
477 return this == that;
478
479 if (that->reg.file != reg.file || that->reg.fileIndex != reg.fileIndex)
480 return false;
481 if (that->reg.size != this->reg.size)
482 return false;
483
484 if (that->reg.data.id != this->reg.data.id)
485 return false;
486
487 return true;
488 }
489
490 bool
491 ImmediateValue::equals(const Value *that, bool strict) const
492 {
493 const ImmediateValue *imm = that->asImm();
494 if (!imm)
495 return false;
496 return reg.data.u64 == imm->reg.data.u64;
497 }
498
499 bool
500 Symbol::equals(const Value *that, bool strict) const
501 {
502 if (reg.file != that->reg.file || reg.fileIndex != that->reg.fileIndex)
503 return false;
504 assert(that->asSym());
505
506 if (this->baseSym != that->asSym()->baseSym)
507 return false;
508
509 return this->reg.data.offset == that->reg.data.offset;
510 }
511
512 void Instruction::init()
513 {
514 next = prev = 0;
515
516 cc = CC_ALWAYS;
517 rnd = ROUND_N;
518 cache = CACHE_CA;
519 subOp = 0;
520
521 saturate = 0;
522 join = terminator = 0;
523 ftz = dnz = 0;
524 atomic = 0;
525 perPatch = 0;
526 fixed = 0;
527 encSize = 0;
528 ipa = 0;
529
530 lanes = 0xf;
531
532 postFactor = 0;
533
534 predSrc = -1;
535 flagsDef = -1;
536 flagsSrc = -1;
537 }
538
539 Instruction::Instruction()
540 {
541 init();
542
543 op = OP_NOP;
544 dType = sType = TYPE_F32;
545
546 id = -1;
547 bb = 0;
548 }
549
550 Instruction::Instruction(Function *fn, operation opr, DataType ty)
551 {
552 init();
553
554 op = opr;
555 dType = sType = ty;
556
557 fn->add(this, id);
558 }
559
560 Instruction::~Instruction()
561 {
562 if (bb) {
563 Function *fn = bb->getFunction();
564 bb->remove(this);
565 fn->allInsns.remove(id);
566 }
567
568 for (int s = 0; srcExists(s); ++s)
569 setSrc(s, NULL);
570 // must unlink defs too since the list pointers will get deallocated
571 for (int d = 0; defExists(d); ++d)
572 setDef(d, NULL);
573 }
574
575 void
576 Instruction::setDef(int i, Value *val)
577 {
578 int size = defs.size();
579 if (i >= size) {
580 defs.resize(i + 1);
581 while (size <= i)
582 defs[size++].setInsn(this);
583 }
584 defs[i].set(val);
585 }
586
587 void
588 Instruction::setSrc(int s, Value *val)
589 {
590 int size = srcs.size();
591 if (s >= size) {
592 srcs.resize(s + 1);
593 while (size <= s)
594 srcs[size++].setInsn(this);
595 }
596 srcs[s].set(val);
597 }
598
599 void
600 Instruction::setSrc(int s, const ValueRef& ref)
601 {
602 setSrc(s, ref.get());
603 srcs[s].mod = ref.mod;
604 }
605
606 void
607 Instruction::swapSources(int a, int b)
608 {
609 Value *value = srcs[a].get();
610 Modifier m = srcs[a].mod;
611
612 setSrc(a, srcs[b]);
613
614 srcs[b].set(value);
615 srcs[b].mod = m;
616 }
617
618 void
619 Instruction::takeExtraSources(int s, Value *values[3])
620 {
621 values[0] = getIndirect(s, 0);
622 if (values[0])
623 setIndirect(s, 0, NULL);
624
625 values[1] = getIndirect(s, 1);
626 if (values[1])
627 setIndirect(s, 1, NULL);
628
629 values[2] = getPredicate();
630 if (values[2])
631 setPredicate(cc, NULL);
632 }
633
634 void
635 Instruction::putExtraSources(int s, Value *values[3])
636 {
637 if (values[0])
638 setIndirect(s, 0, values[0]);
639 if (values[1])
640 setIndirect(s, 1, values[1]);
641 if (values[2])
642 setPredicate(cc, values[2]);
643 }
644
645 Instruction *
646 Instruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
647 {
648 if (!i)
649 i = new_Instruction(pol.context(), op, dType);
650 assert(typeid(*i) == typeid(*this));
651
652 pol.set<Instruction>(this, i);
653
654 i->sType = sType;
655
656 i->rnd = rnd;
657 i->cache = cache;
658 i->subOp = subOp;
659
660 i->saturate = saturate;
661 i->join = join;
662 i->exit = exit;
663 i->atomic = atomic;
664 i->ftz = ftz;
665 i->dnz = dnz;
666 i->ipa = ipa;
667 i->lanes = lanes;
668 i->perPatch = perPatch;
669
670 i->postFactor = postFactor;
671
672 for (int d = 0; defExists(d); ++d)
673 i->setDef(d, pol.get(getDef(d)));
674
675 for (int s = 0; srcExists(s); ++s) {
676 i->setSrc(s, pol.get(getSrc(s)));
677 i->src(s).mod = src(s).mod;
678 }
679
680 i->cc = cc;
681 i->predSrc = predSrc;
682 i->flagsDef = flagsDef;
683 i->flagsSrc = flagsSrc;
684
685 return i;
686 }
687
688 unsigned int
689 Instruction::defCount(unsigned int mask) const
690 {
691 unsigned int i, n;
692
693 for (n = 0, i = 0; this->defExists(i); ++i, mask >>= 1)
694 n += mask & 1;
695 return n;
696 }
697
698 unsigned int
699 Instruction::srcCount(unsigned int mask) const
700 {
701 unsigned int i, n;
702
703 for (n = 0, i = 0; this->srcExists(i); ++i, mask >>= 1)
704 n += mask & 1;
705 return n;
706 }
707
708 bool
709 Instruction::setIndirect(int s, int dim, Value *value)
710 {
711 assert(this->srcExists(s));
712
713 int p = srcs[s].indirect[dim];
714 if (p < 0) {
715 if (!value)
716 return true;
717 p = srcs.size();
718 while (p > 0 && !srcExists(p - 1))
719 --p;
720 }
721 setSrc(p, value);
722 srcs[p].usedAsPtr = (value != 0);
723 srcs[s].indirect[dim] = value ? p : -1;
724 return true;
725 }
726
727 bool
728 Instruction::setPredicate(CondCode ccode, Value *value)
729 {
730 cc = ccode;
731
732 if (!value) {
733 if (predSrc >= 0) {
734 srcs[predSrc].set(NULL);
735 predSrc = -1;
736 }
737 return true;
738 }
739
740 if (predSrc < 0) {
741 predSrc = srcs.size();
742 while (predSrc > 0 && !srcExists(predSrc - 1))
743 --predSrc;
744 }
745
746 setSrc(predSrc, value);
747 return true;
748 }
749
750 bool
751 Instruction::writesPredicate() const
752 {
753 for (int d = 0; defExists(d); ++d)
754 if (getDef(d)->inFile(FILE_PREDICATE) || getDef(d)->inFile(FILE_FLAGS))
755 return true;
756 return false;
757 }
758
759 static bool
760 insnCheckCommutation(const Instruction *a, const Instruction *b)
761 {
762 for (int d = 0; a->defExists(d); ++d)
763 for (int s = 0; b->srcExists(s); ++s)
764 if (a->getDef(d)->interfers(b->getSrc(s)))
765 return false;
766 return true;
767 }
768
769 bool
770 Instruction::isCommutationLegal(const Instruction *i) const
771 {
772 bool ret = true;
773 ret = ret && insnCheckCommutation(this, i);
774 ret = ret && insnCheckCommutation(i, this);
775 return ret;
776 }
777
778 TexInstruction::TexInstruction(Function *fn, operation op)
779 : Instruction(fn, op, TYPE_F32)
780 {
781 memset(&tex, 0, sizeof(tex));
782
783 tex.rIndirectSrc = -1;
784 tex.sIndirectSrc = -1;
785 }
786
787 TexInstruction::~TexInstruction()
788 {
789 for (int c = 0; c < 3; ++c) {
790 dPdx[c].set(NULL);
791 dPdy[c].set(NULL);
792 }
793 }
794
795 TexInstruction *
796 TexInstruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
797 {
798 TexInstruction *tex = (i ? static_cast<TexInstruction *>(i) :
799 new_TexInstruction(pol.context(), op));
800
801 Instruction::clone(pol, tex);
802
803 tex->tex = this->tex;
804
805 if (op == OP_TXD) {
806 for (unsigned int c = 0; c < tex->tex.target.getDim(); ++c) {
807 tex->dPdx[c].set(dPdx[c]);
808 tex->dPdy[c].set(dPdy[c]);
809 }
810 }
811
812 return tex;
813 }
814
815 const struct TexInstruction::Target::Desc TexInstruction::Target::descTable[] =
816 {
817 { "1D", 1, 1, false, false, false },
818 { "2D", 2, 2, false, false, false },
819 { "2D_MS", 2, 2, false, false, false },
820 { "3D", 3, 3, false, false, false },
821 { "CUBE", 2, 3, false, true, false },
822 { "1D_SHADOW", 1, 1, false, false, true },
823 { "2D_SHADOW", 2, 2, false, false, true },
824 { "CUBE_SHADOW", 2, 3, false, true, true },
825 { "1D_ARRAY", 1, 2, true, false, false },
826 { "2D_ARRAY", 2, 3, true, false, false },
827 { "2D_MS_ARRAY", 2, 3, true, false, false },
828 { "CUBE_ARRAY", 2, 4, true, true, false },
829 { "1D_ARRAY_SHADOW", 1, 2, true, false, true },
830 { "2D_ARRAY_SHADOW", 2, 3, true, false, true },
831 { "RECT", 2, 2, false, false, false },
832 { "RECT_SHADOW", 2, 2, false, false, true },
833 { "CUBE_ARRAY_SHADOW", 2, 4, true, true, true },
834 { "BUFFER", 1, 1, false, false, false },
835 };
836
837 CmpInstruction::CmpInstruction(Function *fn, operation op)
838 : Instruction(fn, op, TYPE_F32)
839 {
840 setCond = CC_ALWAYS;
841 }
842
843 CmpInstruction *
844 CmpInstruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
845 {
846 CmpInstruction *cmp = (i ? static_cast<CmpInstruction *>(i) :
847 new_CmpInstruction(pol.context(), op));
848 cmp->dType = dType;
849 Instruction::clone(pol, cmp);
850 cmp->setCond = setCond;
851 return cmp;
852 }
853
854 FlowInstruction::FlowInstruction(Function *fn, operation op,
855 BasicBlock *targ)
856 : Instruction(fn, op, TYPE_NONE)
857 {
858 target.bb = targ;
859
860 if (op == OP_BRA ||
861 op == OP_CONT || op == OP_BREAK ||
862 op == OP_RET || op == OP_EXIT)
863 terminator = 1;
864 else
865 if (op == OP_JOIN)
866 terminator = targ ? 1 : 0;
867
868 allWarp = absolute = limit = 0;
869 }
870
871 FlowInstruction *
872 FlowInstruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
873 {
874 FlowInstruction *flow = (i ? static_cast<FlowInstruction *>(i) :
875 new_FlowInstruction(pol.context(), op, NULL));
876
877 Instruction::clone(pol, flow);
878 flow->allWarp = allWarp;
879 flow->absolute = absolute;
880 flow->limit = limit;
881 flow->builtin = builtin;
882
883 if (builtin)
884 flow->target.builtin = target.builtin;
885 else
886 if (op == OP_CALL)
887 flow->target.fn = target.fn;
888 else
889 if (target.bb)
890 flow->target.bb = pol.get<BasicBlock>(target.bb);
891
892 return flow;
893 }
894
895 Program::Program(Type type, Target *arch)
896 : progType(type),
897 target(arch),
898 mem_Instruction(sizeof(Instruction), 6),
899 mem_CmpInstruction(sizeof(CmpInstruction), 4),
900 mem_TexInstruction(sizeof(TexInstruction), 4),
901 mem_FlowInstruction(sizeof(FlowInstruction), 4),
902 mem_LValue(sizeof(LValue), 8),
903 mem_Symbol(sizeof(Symbol), 7),
904 mem_ImmediateValue(sizeof(ImmediateValue), 7)
905 {
906 code = NULL;
907 binSize = 0;
908
909 maxGPR = -1;
910
911 main = new Function(this, "MAIN");
912
913 dbgFlags = 0;
914 }
915
916 Program::~Program()
917 {
918 for (ArrayList::Iterator it = allFuncs.iterator(); !it.end(); it.next())
919 delete reinterpret_cast<Function *>(it.get());
920
921 for (ArrayList::Iterator it = allRValues.iterator(); !it.end(); it.next())
922 releaseValue(reinterpret_cast<Value *>(it.get()));
923 }
924
925 void Program::releaseInstruction(Instruction *insn)
926 {
927 // TODO: make this not suck so much
928
929 insn->~Instruction();
930
931 if (insn->asCmp())
932 mem_CmpInstruction.release(insn);
933 else
934 if (insn->asTex())
935 mem_TexInstruction.release(insn);
936 else
937 if (insn->asFlow())
938 mem_FlowInstruction.release(insn);
939 else
940 mem_Instruction.release(insn);
941 }
942
943 void Program::releaseValue(Value *value)
944 {
945 value->~Value();
946
947 if (value->asLValue())
948 mem_LValue.release(value);
949 else
950 if (value->asImm())
951 mem_ImmediateValue.release(value);
952 else
953 if (value->asSym())
954 mem_Symbol.release(value);
955 }
956
957
958 } // namespace nv50_ir
959
960 extern "C" {
961
962 static void
963 nv50_ir_init_prog_info(struct nv50_ir_prog_info *info)
964 {
965 #if defined(PIPE_SHADER_HULL) && defined(PIPE_SHADER_DOMAIN)
966 if (info->type == PIPE_SHADER_HULL || info->type == PIPE_SHADER_DOMAIN) {
967 info->prop.tp.domain = PIPE_PRIM_MAX;
968 info->prop.tp.outputPrim = PIPE_PRIM_MAX;
969 }
970 #endif
971 if (info->type == PIPE_SHADER_GEOMETRY) {
972 info->prop.gp.instanceCount = 1;
973 info->prop.gp.maxVertices = 1;
974 }
975 info->io.clipDistance = 0xff;
976 info->io.pointSize = 0xff;
977 info->io.vertexId = 0xff;
978 info->io.edgeFlagIn = 0xff;
979 info->io.edgeFlagOut = 0xff;
980 info->io.fragDepth = 0xff;
981 info->io.sampleMask = 0xff;
982 info->io.backFaceColor[0] = info->io.backFaceColor[1] = 0xff;
983 }
984
985 int
986 nv50_ir_generate_code(struct nv50_ir_prog_info *info)
987 {
988 int ret = 0;
989
990 nv50_ir::Program::Type type;
991
992 nv50_ir_init_prog_info(info);
993
994 #define PROG_TYPE_CASE(a, b) \
995 case PIPE_SHADER_##a: type = nv50_ir::Program::TYPE_##b; break
996
997 switch (info->type) {
998 PROG_TYPE_CASE(VERTEX, VERTEX);
999 // PROG_TYPE_CASE(HULL, TESSELLATION_CONTROL);
1000 // PROG_TYPE_CASE(DOMAIN, TESSELLATION_EVAL);
1001 PROG_TYPE_CASE(GEOMETRY, GEOMETRY);
1002 PROG_TYPE_CASE(FRAGMENT, FRAGMENT);
1003 default:
1004 type = nv50_ir::Program::TYPE_COMPUTE;
1005 break;
1006 }
1007 INFO_DBG(info->dbgFlags, VERBOSE, "translating program of type %u\n", type);
1008
1009 nv50_ir::Target *targ = nv50_ir::Target::create(info->target);
1010 if (!targ)
1011 return -1;
1012
1013 nv50_ir::Program *prog = new nv50_ir::Program(type, targ);
1014 if (!prog)
1015 return -1;
1016 prog->dbgFlags = info->dbgFlags;
1017
1018 switch (info->bin.sourceRep) {
1019 #if 0
1020 case PIPE_IR_LLVM:
1021 case PIPE_IR_GLSL:
1022 return -1;
1023 case PIPE_IR_SM4:
1024 ret = prog->makeFromSM4(info) ? 0 : -2;
1025 break;
1026 case PIPE_IR_TGSI:
1027 #endif
1028 default:
1029 ret = prog->makeFromTGSI(info) ? 0 : -2;
1030 break;
1031 }
1032 if (ret < 0)
1033 goto out;
1034 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
1035 prog->print();
1036
1037 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_PRE_SSA);
1038
1039 prog->convertToSSA();
1040
1041 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
1042 prog->print();
1043
1044 prog->optimizeSSA(info->optLevel);
1045 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_SSA);
1046
1047 if (prog->dbgFlags & NV50_IR_DEBUG_BASIC)
1048 prog->print();
1049
1050 if (!prog->registerAllocation()) {
1051 ret = -4;
1052 goto out;
1053 }
1054 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_POST_RA);
1055
1056 prog->optimizePostRA(info->optLevel);
1057
1058 if (!prog->emitBinary(info)) {
1059 ret = -5;
1060 goto out;
1061 }
1062
1063 out:
1064 INFO_DBG(prog->dbgFlags, VERBOSE, "nv50_ir_generate_code: ret = %i\n", ret);
1065
1066 info->bin.maxGPR = prog->maxGPR;
1067 info->bin.code = prog->code;
1068 info->bin.codeSize = prog->binSize;
1069
1070 delete prog;
1071 nv50_ir::Target::destroy(targ);
1072
1073 return ret;
1074 }
1075
1076 } // extern "C"