Debian Codegraph @

src/gallium/drivers/nouveau/codegen/nv50_ir.cpp

raw
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 "nv50_ir.h"
24 #include "nv50_ir_target.h"
25 #include "nv50_ir_driver.h"
26
27 namespace nv50_ir {
28
29 Modifier::Modifier(operation op)
30 {
31 switch (op) {
32 case OP_NEG: bits = NV50_IR_MOD_NEG; break;
33 case OP_ABS: bits = NV50_IR_MOD_ABS; break;
34 case OP_SAT: bits = NV50_IR_MOD_SAT; break;
35 case OP_NOT: bits = NV50_IR_MOD_NOT; break;
36 default:
37 bits = 0;
38 break;
39 }
40 }
41
42 Modifier Modifier::operator*(const Modifier m) const
43 {
44 unsigned int a, b, c;
45
46 b = m.bits;
47 if (this->bits & NV50_IR_MOD_ABS)
48 b &= ~NV50_IR_MOD_NEG;
49
50 a = (this->bits ^ b) & (NV50_IR_MOD_NOT | NV50_IR_MOD_NEG);
51 c = (this->bits | m.bits) & (NV50_IR_MOD_ABS | NV50_IR_MOD_SAT);
52
53 return Modifier(a | c);
54 }
55
56 ValueRef::ValueRef(Value *v) : value(NULL), insn(NULL)
57 {
58 indirect[0] = -1;
59 indirect[1] = -1;
60 usedAsPtr = false;
61 set(v);
62 }
63
64 ValueRef::ValueRef(const ValueRef& ref) : value(NULL), insn(ref.insn)
65 {
66 set(ref);
67 usedAsPtr = ref.usedAsPtr;
68 }
69
70 ValueRef::~ValueRef()
71 {
72 this->set(NULL);
73 }
74
75 bool ValueRef::getImmediate(ImmediateValue &imm) const
76 {
77 const ValueRef *src = this;
78 Modifier m;
79 DataType type = src->insn->sType;
80
81 while (src) {
82 if (src->mod) {
83 if (src->insn->sType != type)
84 break;
85 m *= src->mod;
86 }
87 if (src->getFile() == FILE_IMMEDIATE) {
88 imm = *(src->value->asImm());
89 // The immediate's type isn't required to match its use, it's
90 // more of a hint; applying a modifier makes use of that hint.
91 imm.reg.type = type;
92 m.applyTo(imm);
93 return true;
94 }
95
96 Instruction *insn = src->value->getUniqueInsn();
97
98 if (insn && insn->op == OP_MOV) {
99 src = &insn->src(0);
100 if (src->mod)
101 WARN("OP_MOV with modifier encountered !\n");
102 } else {
103 src = NULL;
104 }
105 }
106 return false;
107 }
108
109 ValueDef::ValueDef(Value *v) : value(NULL), origin(NULL), insn(NULL)
110 {
111 set(v);
112 }
113
114 ValueDef::ValueDef(const ValueDef& def) : value(NULL), origin(NULL), insn(NULL)
115 {
116 set(def.get());
117 }
118
119 ValueDef::~ValueDef()
120 {
121 this->set(NULL);
122 }
123
124 void
125 ValueRef::set(const ValueRef &ref)
126 {
127 this->set(ref.get());
128 mod = ref.mod;
129 indirect[0] = ref.indirect[0];
130 indirect[1] = ref.indirect[1];
131 }
132
133 void
134 ValueRef::set(Value *refVal)
135 {
136 if (value == refVal)
137 return;
138 if (value)
139 value->uses.erase(this);
140 if (refVal)
141 refVal->uses.insert(this);
142
143 value = refVal;
144 }
145
146 void
147 ValueDef::set(Value *defVal)
148 {
149 if (value == defVal)
150 return;
151 if (value)
152 value->defs.remove(this);
153 if (defVal)
154 defVal->defs.push_back(this);
155
156 value = defVal;
157 }
158
159 // Check if we can replace this definition's value by the value in @rep,
160 // including the source modifiers, i.e. make sure that all uses support
161 // @rep.mod.
162 bool
163 ValueDef::mayReplace(const ValueRef &rep)
164 {
165 if (!rep.mod)
166 return true;
167
168 if (!insn || !insn->bb) // Unbound instruction ?
169 return false;
170
171 const Target *target = insn->bb->getProgram()->getTarget();
172
173 for (Value::UseIterator it = value->uses.begin(); it != value->uses.end();
174 ++it) {
175 Instruction *insn = (*it)->getInsn();
176 int s = -1;
177
178 for (int i = 0; insn->srcExists(i); ++i) {
179 if (insn->src(i).get() == value) {
180 // If there are multiple references to us we'd have to check if the
181 // combination of mods is still supported, but just bail for now.
182 if (&insn->src(i) != (*it))
183 return false;
184 s = i;
185 }
186 }
187 assert(s >= 0); // integrity of uses list
188
189 if (!target->isModSupported(insn, s, rep.mod))
190 return false;
191 }
192 return true;
193 }
194
195 void
196 ValueDef::replace(const ValueRef &repVal, bool doSet)
197 {
198 assert(mayReplace(repVal));
199
200 if (value == repVal.get())
201 return;
202
203 while (!value->uses.empty()) {
204 ValueRef *ref = *value->uses.begin();
205 ref->set(repVal.get());
206 ref->mod *= repVal.mod;
207 }
208
209 if (doSet)
210 set(repVal.get());
211 }
212
213 Value::Value() : id(-1)
214 {
215 join = this;
216 memset(&reg, 0, sizeof(reg));
217 reg.size = 4;
218 }
219
220 LValue::LValue(Function *fn, DataFile file)
221 {
222 reg.file = file;
223 reg.size = (file != FILE_PREDICATE) ? 4 : 1;
224 reg.data.id = -1;
225
226 compMask = 0;
227 compound = 0;
228 ssa = 0;
229 noSpill = 0;
230
231 fn->add(this, this->id);
232 }
233
234 LValue::LValue(Function *fn, LValue *lval)
235 {
236 assert(lval);
237
238 reg.file = lval->reg.file;
239 reg.size = lval->reg.size;
240 reg.data.id = -1;
241
242 compMask = 0;
243 compound = 0;
244 ssa = 0;
245 noSpill = 0;
246
247 fn->add(this, this->id);
248 }
249
250 LValue *
251 LValue::clone(ClonePolicy<Function>& pol) const
252 {
253 LValue *that = new_LValue(pol.context(), reg.file);
254
255 pol.set<Value>(this, that);
256
257 that->reg.size = this->reg.size;
258 that->reg.type = this->reg.type;
259 that->reg.data = this->reg.data;
260
261 return that;
262 }
263
264 bool
265 LValue::isUniform() const
266 {
267 if (defs.size() > 1)
268 return false;
269 Instruction *insn = getInsn();
270 if (!insn)
271 return false;
272 // let's not try too hard here for now ...
273 return !insn->srcExists(1) && insn->getSrc(0)->isUniform();
274 }
275
276 Symbol::Symbol(Program *prog, DataFile f, uint8_t fidx)
277 {
278 baseSym = NULL;
279
280 reg.file = f;
281 reg.fileIndex = fidx;
282 reg.data.offset = 0;
283
284 prog->add(this, this->id);
285 }
286
287 Symbol *
288 Symbol::clone(ClonePolicy<Function>& pol) const
289 {
290 Program *prog = pol.context()->getProgram();
291
292 Symbol *that = new_Symbol(prog, reg.file, reg.fileIndex);
293
294 pol.set<Value>(this, that);
295
296 that->reg.size = this->reg.size;
297 that->reg.type = this->reg.type;
298 that->reg.data = this->reg.data;
299
300 that->baseSym = this->baseSym;
301
302 return that;
303 }
304
305 bool
306 Symbol::isUniform() const
307 {
308 return
309 reg.file != FILE_SYSTEM_VALUE &&
310 reg.file != FILE_MEMORY_LOCAL &&
311 reg.file != FILE_SHADER_INPUT;
312 }
313
314 ImmediateValue::ImmediateValue(Program *prog, uint32_t uval)
315 {
316 memset(&reg, 0, sizeof(reg));
317
318 reg.file = FILE_IMMEDIATE;
319 reg.size = 4;
320 reg.type = TYPE_U32;
321
322 reg.data.u32 = uval;
323
324 prog->add(this, this->id);
325 }
326
327 ImmediateValue::ImmediateValue(Program *prog, float fval)
328 {
329 memset(&reg, 0, sizeof(reg));
330
331 reg.file = FILE_IMMEDIATE;
332 reg.size = 4;
333 reg.type = TYPE_F32;
334
335 reg.data.f32 = fval;
336
337 prog->add(this, this->id);
338 }
339
340 ImmediateValue::ImmediateValue(Program *prog, double dval)
341 {
342 memset(&reg, 0, sizeof(reg));
343
344 reg.file = FILE_IMMEDIATE;
345 reg.size = 8;
346 reg.type = TYPE_F64;
347
348 reg.data.f64 = dval;
349
350 prog->add(this, this->id);
351 }
352
353 ImmediateValue::ImmediateValue(const ImmediateValue *proto, DataType ty)
354 {
355 reg = proto->reg;
356
357 reg.type = ty;
358 reg.size = typeSizeof(ty);
359 }
360
361 ImmediateValue *
362 ImmediateValue::clone(ClonePolicy<Function>& pol) const
363 {
364 Program *prog = pol.context()->getProgram();
365 ImmediateValue *that = new_ImmediateValue(prog, 0u);
366
367 pol.set<Value>(this, that);
368
369 that->reg.size = this->reg.size;
370 that->reg.type = this->reg.type;
371 that->reg.data = this->reg.data;
372
373 return that;
374 }
375
376 bool
377 ImmediateValue::isInteger(const int i) const
378 {
379 switch (reg.type) {
380 case TYPE_S8:
381 return reg.data.s8 == i;
382 case TYPE_U8:
383 return reg.data.u8 == i;
384 case TYPE_S16:
385 return reg.data.s16 == i;
386 case TYPE_U16:
387 return reg.data.u16 == i;
388 case TYPE_S32:
389 case TYPE_U32:
390 return reg.data.s32 == i; // as if ...
391 case TYPE_S64:
392 case TYPE_U64:
393 return reg.data.s64 == i; // as if ...
394 case TYPE_F32:
395 return reg.data.f32 == static_cast<float>(i);
396 case TYPE_F64:
397 return reg.data.f64 == static_cast<double>(i);
398 default:
399 return false;
400 }
401 }
402
403 bool
404 ImmediateValue::isNegative() const
405 {
406 switch (reg.type) {
407 case TYPE_S8: return reg.data.s8 < 0;
408 case TYPE_S16: return reg.data.s16 < 0;
409 case TYPE_S32:
410 case TYPE_U32: return reg.data.s32 < 0;
411 case TYPE_F32: return reg.data.u32 & (1 << 31);
412 case TYPE_F64: return reg.data.u64 & (1ULL << 63);
413 default:
414 return false;
415 }
416 }
417
418 bool
419 ImmediateValue::isPow2() const
420 {
421 if (reg.type == TYPE_U64 || reg.type == TYPE_S64)
422 return util_is_power_of_two_or_zero64(reg.data.u64);
423 else
424 return util_is_power_of_two_or_zero(reg.data.u32);
425 }
426
427 void
428 ImmediateValue::applyLog2()
429 {
430 switch (reg.type) {
431 case TYPE_S8:
432 case TYPE_S16:
433 case TYPE_S32:
434 assert(!this->isNegative());
435 FALLTHROUGH;
436 case TYPE_U8:
437 case TYPE_U16:
438 case TYPE_U32:
439 reg.data.u32 = util_logbase2(reg.data.u32);
440 break;
441 case TYPE_S64:
442 assert(!this->isNegative());
443 FALLTHROUGH;
444 case TYPE_U64:
445 reg.data.u64 = util_logbase2_64(reg.data.u64);
446 break;
447 case TYPE_F32:
448 reg.data.f32 = log2f(reg.data.f32);
449 break;
450 case TYPE_F64:
451 reg.data.f64 = log2(reg.data.f64);
452 break;
453 default:
454 assert(0);
455 break;
456 }
457 }
458
459 bool
460 ImmediateValue::compare(CondCode cc, float fval) const
461 {
462 if (reg.type != TYPE_F32)
463 ERROR("immediate value is not of type f32");
464
465 switch (static_cast<CondCode>(cc & 7)) {
466 case CC_TR: return true;
467 case CC_FL: return false;
468 case CC_LT: return reg.data.f32 < fval;
469 case CC_LE: return reg.data.f32 <= fval;
470 case CC_GT: return reg.data.f32 > fval;
471 case CC_GE: return reg.data.f32 >= fval;
472 case CC_EQ: return reg.data.f32 == fval;
473 case CC_NE: return reg.data.f32 != fval;
474 default:
475 assert(0);
476 return false;
477 }
478 }
479
480 ImmediateValue&
481 ImmediateValue::operator=(const ImmediateValue &that)
482 {
483 this->reg = that.reg;
484 return (*this);
485 }
486
487 bool
488 Value::interfers(const Value *that) const
489 {
490 uint32_t idA, idB;
491
492 if (that->reg.file != reg.file || that->reg.fileIndex != reg.fileIndex)
493 return false;
494 if (this->asImm())
495 return false;
496
497 if (this->asSym()) {
498 idA = this->join->reg.data.offset;
499 idB = that->join->reg.data.offset;
500 } else {
501 idA = this->join->reg.data.id * MIN2(this->reg.size, 4);
502 idB = that->join->reg.data.id * MIN2(that->reg.size, 4);
503 }
504
505 if (idA < idB)
506 return (idA + this->reg.size > idB);
507 else
508 if (idA > idB)
509 return (idB + that->reg.size > idA);
510 else
511 return (idA == idB);
512 }
513
514 bool
515 Value::equals(const Value *that, bool strict) const
516 {
517 if (strict)
518 return this == that;
519
520 if (that->reg.file != reg.file || that->reg.fileIndex != reg.fileIndex)
521 return false;
522 if (that->reg.size != this->reg.size)
523 return false;
524
525 if (that->reg.data.id != this->reg.data.id)
526 return false;
527
528 return true;
529 }
530
531 bool
532 ImmediateValue::equals(const Value *that, bool strict) const
533 {
534 const ImmediateValue *imm = that->asImm();
535 if (!imm)
536 return false;
537 return reg.data.u64 == imm->reg.data.u64;
538 }
539
540 bool
541 Symbol::equals(const Value *that, bool strict) const
542 {
543 if (reg.file != that->reg.file || reg.fileIndex != that->reg.fileIndex)
544 return false;
545 assert(that->asSym());
546
547 if (this->baseSym != that->asSym()->baseSym)
548 return false;
549
550 if (reg.file == FILE_SYSTEM_VALUE)
551 return (this->reg.data.sv.sv == that->reg.data.sv.sv &&
552 this->reg.data.sv.index == that->reg.data.sv.index);
553 return this->reg.data.offset == that->reg.data.offset;
554 }
555
556 void Instruction::init()
557 {
558 next = prev = 0;
559 serial = 0;
560
561 cc = CC_ALWAYS;
562 rnd = ROUND_N;
563 cache = CACHE_CA;
564 subOp = 0;
565
566 saturate = 0;
567 join = 0;
568 exit = 0;
569 terminator = 0;
570 ftz = 0;
571 dnz = 0;
572 perPatch = 0;
573 fixed = 0;
574 encSize = 0;
575 ipa = 0;
576 mask = 0;
577 precise = 0;
578
579 lanes = 0xf;
580
581 postFactor = 0;
582
583 predSrc = -1;
584 flagsDef = -1;
585 flagsSrc = -1;
586
587 sched = 0;
588 bb = NULL;
589 }
590
591 Instruction::Instruction()
592 {
593 init();
594
595 op = OP_NOP;
596 dType = sType = TYPE_F32;
597
598 id = -1;
599 }
600
601 Instruction::Instruction(Function *fn, operation opr, DataType ty)
602 {
603 init();
604
605 op = opr;
606 dType = sType = ty;
607
608 fn->add(this, id);
609 }
610
611 Instruction::~Instruction()
612 {
613 if (bb) {
614 Function *fn = bb->getFunction();
615 bb->remove(this);
616 fn->allInsns.remove(id);
617 }
618
619 for (int s = 0; srcExists(s); ++s)
620 setSrc(s, NULL);
621 // must unlink defs too since the list pointers will get deallocated
622 for (int d = 0; defExists(d); ++d)
623 setDef(d, NULL);
624 }
625
626 void
627 Instruction::setDef(int i, Value *val)
628 {
629 int size = defs.size();
630 if (i >= size) {
631 defs.resize(i + 1);
632 while (size <= i)
633 defs[size++].setInsn(this);
634 }
635 defs[i].set(val);
636 }
637
638 void
639 Instruction::setSrc(int s, Value *val)
640 {
641 int size = srcs.size();
642 if (s >= size) {
643 srcs.resize(s + 1);
644 while (size <= s)
645 srcs[size++].setInsn(this);
646 }
647 srcs[s].set(val);
648 }
649
650 void
651 Instruction::setSrc(int s, const ValueRef& ref)
652 {
653 setSrc(s, ref.get());
654 srcs[s].mod = ref.mod;
655 }
656
657 void
658 Instruction::swapSources(int a, int b)
659 {
660 Value *value = srcs[a].get();
661 Modifier m = srcs[a].mod;
662
663 setSrc(a, srcs[b]);
664
665 srcs[b].set(value);
666 srcs[b].mod = m;
667 }
668
669 static inline void moveSourcesAdjustIndex(int8_t &index, int s, int delta)
670 {
671 if (index >= s)
672 index += delta;
673 else
674 if ((delta < 0) && (index >= (s + delta)))
675 index = -1;
676 }
677
678 // Moves sources [@s,last_source] by @delta.
679 // If @delta < 0, sources [@s - abs(@delta), @s) are erased.
680 void
681 Instruction::moveSources(const int s, const int delta)
682 {
683 if (delta == 0)
684 return;
685 assert(s + delta >= 0);
686
687 int k;
688
689 for (k = 0; srcExists(k); ++k) {
690 for (int i = 0; i < 2; ++i)
691 moveSourcesAdjustIndex(src(k).indirect[i], s, delta);
692 }
693 moveSourcesAdjustIndex(predSrc, s, delta);
694 moveSourcesAdjustIndex(flagsSrc, s, delta);
695 if (asTex()) {
696 TexInstruction *tex = asTex();
697 moveSourcesAdjustIndex(tex->tex.rIndirectSrc, s, delta);
698 moveSourcesAdjustIndex(tex->tex.sIndirectSrc, s, delta);
699 }
700
701 if (delta > 0) {
702 --k;
703 for (int p = k + delta; k >= s; --k, --p)
704 setSrc(p, src(k));
705 } else {
706 int p;
707 for (p = s; p < k; ++p)
708 setSrc(p + delta, src(p));
709 for (; (p + delta) < k; ++p)
710 setSrc(p + delta, NULL);
711 }
712 }
713
714 void
715 Instruction::takeExtraSources(int s, Value *values[3])
716 {
717 values[0] = getIndirect(s, 0);
718 if (values[0])
719 setIndirect(s, 0, NULL);
720
721 values[1] = getIndirect(s, 1);
722 if (values[1])
723 setIndirect(s, 1, NULL);
724
725 values[2] = getPredicate();
726 if (values[2])
727 setPredicate(cc, NULL);
728 }
729
730 void
731 Instruction::putExtraSources(int s, Value *values[3])
732 {
733 if (values[0])
734 setIndirect(s, 0, values[0]);
735 if (values[1])
736 setIndirect(s, 1, values[1]);
737 if (values[2])
738 setPredicate(cc, values[2]);
739 }
740
741 Instruction *
742 Instruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
743 {
744 if (!i)
745 i = new_Instruction(pol.context(), op, dType);
746 #if !defined(NDEBUG) && defined(__cpp_rtti)
747 assert(typeid(*i) == typeid(*this));
748 #endif
749
750 pol.set<Instruction>(this, i);
751
752 i->sType = sType;
753
754 i->rnd = rnd;
755 i->cache = cache;
756 i->subOp = subOp;
757
758 i->saturate = saturate;
759 i->join = join;
760 i->exit = exit;
761 i->mask = mask;
762 i->ftz = ftz;
763 i->dnz = dnz;
764 i->ipa = ipa;
765 i->lanes = lanes;
766 i->perPatch = perPatch;
767
768 i->postFactor = postFactor;
769
770 for (int d = 0; defExists(d); ++d)
771 i->setDef(d, pol.get(getDef(d)));
772
773 for (int s = 0; srcExists(s); ++s) {
774 i->setSrc(s, pol.get(getSrc(s)));
775 i->src(s).mod = src(s).mod;
776 }
777
778 i->cc = cc;
779 i->predSrc = predSrc;
780 i->flagsDef = flagsDef;
781 i->flagsSrc = flagsSrc;
782
783 return i;
784 }
785
786 unsigned int
787 Instruction::defCount(unsigned int mask, bool singleFile) const
788 {
789 unsigned int i, n;
790
791 if (singleFile) {
792 unsigned int d = ffs(mask);
793 if (!d)
794 return 0;
795 for (i = d--; defExists(i); ++i)
796 if (getDef(i)->reg.file != getDef(d)->reg.file)
797 mask &= ~(1 << i);
798 }
799
800 for (n = 0, i = 0; this->defExists(i); ++i, mask >>= 1)
801 n += mask & 1;
802 return n;
803 }
804
805 unsigned int
806 Instruction::srcCount(unsigned int mask, bool singleFile) const
807 {
808 unsigned int i, n;
809
810 if (singleFile) {
811 unsigned int s = ffs(mask);
812 if (!s)
813 return 0;
814 for (i = s--; srcExists(i); ++i)
815 if (getSrc(i)->reg.file != getSrc(s)->reg.file)
816 mask &= ~(1 << i);
817 }
818
819 for (n = 0, i = 0; this->srcExists(i); ++i, mask >>= 1)
820 n += mask & 1;
821 return n;
822 }
823
824 bool
825 Instruction::setIndirect(int s, int dim, Value *value)
826 {
827 assert(this->srcExists(s));
828
829 int p = srcs[s].indirect[dim];
830 if (p < 0) {
831 if (!value)
832 return true;
833 p = srcs.size();
834 while (p > 0 && !srcExists(p - 1))
835 --p;
836 }
837 setSrc(p, value);
838 srcs[p].usedAsPtr = (value != 0);
839 srcs[s].indirect[dim] = value ? p : -1;
840 return true;
841 }
842
843 bool
844 Instruction::setPredicate(CondCode ccode, Value *value)
845 {
846 cc = ccode;
847
848 if (!value) {
849 if (predSrc >= 0) {
850 srcs[predSrc].set(NULL);
851 predSrc = -1;
852 }
853 return true;
854 }
855
856 if (predSrc < 0) {
857 predSrc = srcs.size();
858 while (predSrc > 0 && !srcExists(predSrc - 1))
859 --predSrc;
860 }
861
862 setSrc(predSrc, value);
863 return true;
864 }
865
866 bool
867 Instruction::writesPredicate() const
868 {
869 for (int d = 0; defExists(d); ++d)
870 if (getDef(d)->inFile(FILE_PREDICATE) || getDef(d)->inFile(FILE_FLAGS))
871 return true;
872 return false;
873 }
874
875 bool
876 Instruction::canCommuteDefSrc(const Instruction *i) const
877 {
878 for (int d = 0; defExists(d); ++d)
879 for (int s = 0; i->srcExists(s); ++s)
880 if (getDef(d)->interfers(i->getSrc(s)))
881 return false;
882 return true;
883 }
884
885 bool
886 Instruction::canCommuteDefDef(const Instruction *i) const
887 {
888 for (int d = 0; defExists(d); ++d)
889 for (int c = 0; i->defExists(c); ++c)
890 if (getDef(d)->interfers(i->getDef(c)))
891 return false;
892 return true;
893 }
894
895 bool
896 Instruction::isCommutationLegal(const Instruction *i) const
897 {
898 return canCommuteDefDef(i) &&
899 canCommuteDefSrc(i) &&
900 i->canCommuteDefSrc(this);
901 }
902
903 TexInstruction::TexInstruction(Function *fn, operation op)
904 : Instruction(fn, op, TYPE_F32), tex()
905 {
906 tex.rIndirectSrc = -1;
907 tex.sIndirectSrc = -1;
908
909 if (op == OP_TXF)
910 sType = TYPE_U32;
911 }
912
913 TexInstruction::~TexInstruction()
914 {
915 for (int c = 0; c < 3; ++c) {
916 dPdx[c].set(NULL);
917 dPdy[c].set(NULL);
918 }
919 for (int n = 0; n < 4; ++n)
920 for (int c = 0; c < 3; ++c)
921 offset[n][c].set(NULL);
922 }
923
924 TexInstruction *
925 TexInstruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
926 {
927 TexInstruction *tex = (i ? static_cast<TexInstruction *>(i) :
928 new_TexInstruction(pol.context(), op));
929
930 Instruction::clone(pol, tex);
931
932 tex->tex = this->tex;
933
934 if (op == OP_TXD) {
935 for (unsigned int c = 0; c < tex->tex.target.getDim(); ++c) {
936 tex->dPdx[c].set(dPdx[c]);
937 tex->dPdy[c].set(dPdy[c]);
938 }
939 }
940
941 for (int n = 0; n < tex->tex.useOffsets; ++n)
942 for (int c = 0; c < 3; ++c)
943 tex->offset[n][c].set(offset[n][c]);
944
945 return tex;
946 }
947
948 const struct TexInstruction::Target::Desc TexInstruction::Target::descTable[] =
949 {
950 { "1D", 1, 1, false, false, false },
951 { "2D", 2, 2, false, false, false },
952 { "2D_MS", 2, 3, false, false, false },
953 { "3D", 3, 3, false, false, false },
954 { "CUBE", 2, 3, false, true, false },
955 { "1D_SHADOW", 1, 1, false, false, true },
956 { "2D_SHADOW", 2, 2, false, false, true },
957 { "CUBE_SHADOW", 2, 3, false, true, true },
958 { "1D_ARRAY", 1, 2, true, false, false },
959 { "2D_ARRAY", 2, 3, true, false, false },
960 { "2D_MS_ARRAY", 2, 4, true, false, false },
961 { "CUBE_ARRAY", 2, 4, true, true, false },
962 { "1D_ARRAY_SHADOW", 1, 2, true, false, true },
963 { "2D_ARRAY_SHADOW", 2, 3, true, false, true },
964 { "RECT", 2, 2, false, false, false },
965 { "RECT_SHADOW", 2, 2, false, false, true },
966 { "CUBE_ARRAY_SHADOW", 2, 4, true, true, true },
967 { "BUFFER", 1, 1, false, false, false },
968 };
969
970 const struct TexInstruction::ImgFormatDesc TexInstruction::formatTable[] =
971 {
972 { "RGBA32F", 4, { 32, 32, 32, 32 }, FLOAT },
973 { "RGBA16F", 4, { 16, 16, 16, 16 }, FLOAT },
974 { "RG32F", 2, { 32, 32, 0, 0 }, FLOAT },
975 { "RG16F", 2, { 16, 16, 0, 0 }, FLOAT },
976 { "R11G11B10F", 3, { 11, 11, 10, 0 }, FLOAT },
977 { "R32F", 1, { 32, 0, 0, 0 }, FLOAT },
978 { "R16F", 1, { 16, 0, 0, 0 }, FLOAT },
979
980 { "RGBA32UI", 4, { 32, 32, 32, 32 }, UINT },
981 { "RGBA16UI", 4, { 16, 16, 16, 16 }, UINT },
982 { "RGB10A2UI", 4, { 10, 10, 10, 2 }, UINT },
983 { "RGBA8UI", 4, { 8, 8, 8, 8 }, UINT },
984 { "RG32UI", 2, { 32, 32, 0, 0 }, UINT },
985 { "RG16UI", 2, { 16, 16, 0, 0 }, UINT },
986 { "RG8UI", 2, { 8, 8, 0, 0 }, UINT },
987 { "R32UI", 1, { 32, 0, 0, 0 }, UINT },
988 { "R16UI", 1, { 16, 0, 0, 0 }, UINT },
989 { "R8UI", 1, { 8, 0, 0, 0 }, UINT },
990
991 { "RGBA32I", 4, { 32, 32, 32, 32 }, SINT },
992 { "RGBA16I", 4, { 16, 16, 16, 16 }, SINT },
993 { "RGBA8I", 4, { 8, 8, 8, 8 }, SINT },
994 { "RG32I", 2, { 32, 32, 0, 0 }, SINT },
995 { "RG16I", 2, { 16, 16, 0, 0 }, SINT },
996 { "RG8I", 2, { 8, 8, 0, 0 }, SINT },
997 { "R32I", 1, { 32, 0, 0, 0 }, SINT },
998 { "R16I", 1, { 16, 0, 0, 0 }, SINT },
999 { "R8I", 1, { 8, 0, 0, 0 }, SINT },
1000
1001 { "RGBA16", 4, { 16, 16, 16, 16 }, UNORM },
1002 { "RGB10A2", 4, { 10, 10, 10, 2 }, UNORM },
1003 { "RGBA8", 4, { 8, 8, 8, 8 }, UNORM },
1004 { "RG16", 2, { 16, 16, 0, 0 }, UNORM },
1005 { "RG8", 2, { 8, 8, 0, 0 }, UNORM },
1006 { "R16", 1, { 16, 0, 0, 0 }, UNORM },
1007 { "R8", 1, { 8, 0, 0, 0 }, UNORM },
1008
1009 { "RGBA16_SNORM", 4, { 16, 16, 16, 16 }, SNORM },
1010 { "RGBA8_SNORM", 4, { 8, 8, 8, 8 }, SNORM },
1011 { "RG16_SNORM", 2, { 16, 16, 0, 0 }, SNORM },
1012 { "RG8_SNORM", 2, { 8, 8, 0, 0 }, SNORM },
1013 { "R16_SNORM", 1, { 16, 0, 0, 0 }, SNORM },
1014 { "R8_SNORM", 1, { 8, 0, 0, 0 }, SNORM },
1015
1016 { "BGRA8", 4, { 8, 8, 8, 8 }, UNORM, true },
1017 };
1018
1019 const struct TexInstruction::ImgFormatDesc *
1020 TexInstruction::translateImgFormat(enum pipe_format format)
1021 {
1022
1023 #define FMT_CASE(a, b) \
1024 case PIPE_FORMAT_ ## a: return &formatTable[nv50_ir::FMT_ ## b]
1025
1026 switch (format) {
1027 case PIPE_FORMAT_NONE: return NULL;
1028
1029 FMT_CASE(R32G32B32A32_FLOAT, RGBA32F);
1030 FMT_CASE(R16G16B16A16_FLOAT, RGBA16F);
1031 FMT_CASE(R32G32_FLOAT, RG32F);
1032 FMT_CASE(R16G16_FLOAT, RG16F);
1033 FMT_CASE(R11G11B10_FLOAT, R11G11B10F);
1034 FMT_CASE(R32_FLOAT, R32F);
1035 FMT_CASE(R16_FLOAT, R16F);
1036
1037 FMT_CASE(R32G32B32A32_UINT, RGBA32UI);
1038 FMT_CASE(R16G16B16A16_UINT, RGBA16UI);
1039 FMT_CASE(R10G10B10A2_UINT, RGB10A2UI);
1040 FMT_CASE(R8G8B8A8_UINT, RGBA8UI);
1041 FMT_CASE(R32G32_UINT, RG32UI);
1042 FMT_CASE(R16G16_UINT, RG16UI);
1043 FMT_CASE(R8G8_UINT, RG8UI);
1044 FMT_CASE(R32_UINT, R32UI);
1045 FMT_CASE(R16_UINT, R16UI);
1046 FMT_CASE(R8_UINT, R8UI);
1047
1048 FMT_CASE(R32G32B32A32_SINT, RGBA32I);
1049 FMT_CASE(R16G16B16A16_SINT, RGBA16I);
1050 FMT_CASE(R8G8B8A8_SINT, RGBA8I);
1051 FMT_CASE(R32G32_SINT, RG32I);
1052 FMT_CASE(R16G16_SINT, RG16I);
1053 FMT_CASE(R8G8_SINT, RG8I);
1054 FMT_CASE(R32_SINT, R32I);
1055 FMT_CASE(R16_SINT, R16I);
1056 FMT_CASE(R8_SINT, R8I);
1057
1058 FMT_CASE(R16G16B16A16_UNORM, RGBA16);
1059 FMT_CASE(R10G10B10A2_UNORM, RGB10A2);
1060 FMT_CASE(R8G8B8A8_UNORM, RGBA8);
1061 FMT_CASE(R16G16_UNORM, RG16);
1062 FMT_CASE(R8G8_UNORM, RG8);
1063 FMT_CASE(R16_UNORM, R16);
1064 FMT_CASE(R8_UNORM, R8);
1065
1066 FMT_CASE(R16G16B16A16_SNORM, RGBA16_SNORM);
1067 FMT_CASE(R8G8B8A8_SNORM, RGBA8_SNORM);
1068 FMT_CASE(R16G16_SNORM, RG16_SNORM);
1069 FMT_CASE(R8G8_SNORM, RG8_SNORM);
1070 FMT_CASE(R16_SNORM, R16_SNORM);
1071 FMT_CASE(R8_SNORM, R8_SNORM);
1072
1073 FMT_CASE(B8G8R8A8_UNORM, BGRA8);
1074
1075 default:
1076 assert(!"Unexpected format");
1077 return NULL;
1078 }
1079 }
1080
1081 void
1082 TexInstruction::setIndirectR(Value *v)
1083 {
1084 int p = ((tex.rIndirectSrc < 0) && v) ? srcs.size() : tex.rIndirectSrc;
1085 if (p >= 0) {
1086 tex.rIndirectSrc = p;
1087 setSrc(p, v);
1088 srcs[p].usedAsPtr = !!v;
1089 }
1090 }
1091
1092 void
1093 TexInstruction::setIndirectS(Value *v)
1094 {
1095 int p = ((tex.sIndirectSrc < 0) && v) ? srcs.size() : tex.sIndirectSrc;
1096 if (p >= 0) {
1097 tex.sIndirectSrc = p;
1098 setSrc(p, v);
1099 srcs[p].usedAsPtr = !!v;
1100 }
1101 }
1102
1103 CmpInstruction::CmpInstruction(Function *fn, operation op)
1104 : Instruction(fn, op, TYPE_F32)
1105 {
1106 setCond = CC_ALWAYS;
1107 }
1108
1109 CmpInstruction *
1110 CmpInstruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
1111 {
1112 CmpInstruction *cmp = (i ? static_cast<CmpInstruction *>(i) :
1113 new_CmpInstruction(pol.context(), op));
1114 cmp->dType = dType;
1115 Instruction::clone(pol, cmp);
1116 cmp->setCond = setCond;
1117 return cmp;
1118 }
1119
1120 FlowInstruction::FlowInstruction(Function *fn, operation op, void *targ)
1121 : Instruction(fn, op, TYPE_NONE)
1122 {
1123 if (op == OP_CALL)
1124 target.fn = reinterpret_cast<Function *>(targ);
1125 else
1126 target.bb = reinterpret_cast<BasicBlock *>(targ);
1127
1128 if (op == OP_BRA ||
1129 op == OP_CONT || op == OP_BREAK ||
1130 op == OP_RET || op == OP_EXIT)
1131 terminator = 1;
1132 else
1133 if (op == OP_JOIN)
1134 terminator = targ ? 1 : 0;
1135
1136 allWarp = absolute = limit = builtin = indirect = 0;
1137 }
1138
1139 FlowInstruction *
1140 FlowInstruction::clone(ClonePolicy<Function>& pol, Instruction *i) const
1141 {
1142 FlowInstruction *flow = (i ? static_cast<FlowInstruction *>(i) :
1143 new_FlowInstruction(pol.context(), op, NULL));
1144
1145 Instruction::clone(pol, flow);
1146 flow->allWarp = allWarp;
1147 flow->absolute = absolute;
1148 flow->limit = limit;
1149 flow->builtin = builtin;
1150
1151 if (builtin)
1152 flow->target.builtin = target.builtin;
1153 else
1154 if (op == OP_CALL)
1155 flow->target.fn = target.fn;
1156 else
1157 if (target.bb)
1158 flow->target.bb = pol.get<BasicBlock>(target.bb);
1159
1160 return flow;
1161 }
1162
1163 Program::Program(Type type, Target *arch)
1164 : progType(type),
1165 target(arch),
1166 tlsSize(0),
1167 mem_Instruction(sizeof(Instruction), 6),
1168 mem_CmpInstruction(sizeof(CmpInstruction), 4),
1169 mem_TexInstruction(sizeof(TexInstruction), 4),
1170 mem_FlowInstruction(sizeof(FlowInstruction), 4),
1171 mem_LValue(sizeof(LValue), 8),
1172 mem_Symbol(sizeof(Symbol), 7),
1173 mem_ImmediateValue(sizeof(ImmediateValue), 7),
1174 driver(NULL),
1175 driver_out(NULL)
1176 {
1177 code = NULL;
1178 binSize = 0;
1179
1180 maxGPR = -1;
1181 fp64 = false;
1182 persampleInvocation = false;
1183
1184 main = new Function(this, "MAIN", ~0);
1185 calls.insert(&main->call);
1186
1187 dbgFlags = 0;
1188 optLevel = 0;
1189
1190 targetPriv = NULL;
1191 }
1192
1193 Program::~Program()
1194 {
1195 for (ArrayList::Iterator it = allFuncs.iterator(); !it.end(); it.next())
1196 delete reinterpret_cast<Function *>(it.get());
1197
1198 for (ArrayList::Iterator it = allRValues.iterator(); !it.end(); it.next())
1199 releaseValue(reinterpret_cast<Value *>(it.get()));
1200 }
1201
1202 void Program::releaseInstruction(Instruction *insn)
1203 {
1204 // TODO: make this not suck so much
1205
1206 insn->~Instruction();
1207
1208 if (insn->asCmp())
1209 mem_CmpInstruction.release(insn);
1210 else
1211 if (insn->asTex())
1212 mem_TexInstruction.release(insn);
1213 else
1214 if (insn->asFlow())
1215 mem_FlowInstruction.release(insn);
1216 else
1217 mem_Instruction.release(insn);
1218 }
1219
1220 void Program::releaseValue(Value *value)
1221 {
1222 value->~Value();
1223
1224 if (value->asLValue())
1225 mem_LValue.release(value);
1226 else
1227 if (value->asImm())
1228 mem_ImmediateValue.release(value);
1229 else
1230 if (value->asSym())
1231 mem_Symbol.release(value);
1232 }
1233
1234
1235 } // namespace nv50_ir
1236
1237 extern "C" {
1238
1239 static void
1240 nv50_ir_init_prog_info(struct nv50_ir_prog_info *info,
1241 struct nv50_ir_prog_info_out *info_out)
1242 {
1243 info_out->target = info->target;
1244 info_out->type = info->type;
1245 if (info->type == MESA_SHADER_TESS_CTRL || info->type == MESA_SHADER_TESS_EVAL) {
1246 info_out->prop.tp.domain = MESA_PRIM_COUNT;
1247 info_out->prop.tp.outputPrim = MESA_PRIM_COUNT;
1248 }
1249 if (info->type == MESA_SHADER_GEOMETRY) {
1250 info_out->prop.gp.instanceCount = 1;
1251 info_out->prop.gp.maxVertices = 1;
1252 }
1253 if (info->type == MESA_SHADER_COMPUTE) {
1254 info->prop.cp.numThreads[0] =
1255 info->prop.cp.numThreads[1] =
1256 info->prop.cp.numThreads[2] = 1;
1257 }
1258 info_out->bin.smemSize = info->bin.smemSize;
1259 info_out->io.instanceId = 0xff;
1260 info_out->io.vertexId = 0xff;
1261 info_out->io.edgeFlagIn = 0xff;
1262 info_out->io.edgeFlagOut = 0xff;
1263 info_out->io.fragDepth = 0xff;
1264 info_out->io.sampleMask = 0xff;
1265 }
1266
1267 int
1268 nv50_ir_generate_code(struct nv50_ir_prog_info *info,
1269 struct nv50_ir_prog_info_out *info_out)
1270 {
1271 int ret = 0;
1272
1273 nv50_ir::Program::Type type;
1274
1275 nv50_ir_init_prog_info(info, info_out);
1276
1277 #define PROG_TYPE_CASE(a, b) \
1278 case MESA_SHADER_##a: type = nv50_ir::Program::TYPE_##b; break
1279
1280 switch (info->type) {
1281 PROG_TYPE_CASE(VERTEX, VERTEX);
1282 PROG_TYPE_CASE(TESS_CTRL, TESSELLATION_CONTROL);
1283 PROG_TYPE_CASE(TESS_EVAL, TESSELLATION_EVAL);
1284 PROG_TYPE_CASE(GEOMETRY, GEOMETRY);
1285 PROG_TYPE_CASE(FRAGMENT, FRAGMENT);
1286 PROG_TYPE_CASE(COMPUTE, COMPUTE);
1287 default:
1288 INFO_DBG(info->dbgFlags, VERBOSE, "unsupported program type %u\n", info->type);
1289 return -1;
1290 }
1291 INFO_DBG(info->dbgFlags, VERBOSE, "translating program of type %u\n", type);
1292
1293 nv50_ir::Target *targ = nv50_ir::Target::create(info->target);
1294 if (!targ)
1295 return -1;
1296
1297 nv50_ir::Program *prog = new nv50_ir::Program(type, targ);
1298 if (!prog) {
1299 nv50_ir::Target::destroy(targ);
1300 return -1;
1301 }
1302 prog->driver = info;
1303 prog->driver_out = info_out;
1304 prog->dbgFlags = info->dbgFlags;
1305 prog->optLevel = info->optLevel;
1306
1307 ret = prog->makeFromNIR(info, info_out) ? 0 : -2;
1308 if (ret < 0)
1309 goto out;
1310 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
1311 prog->print();
1312
1313 targ->parseDriverInfo(info, info_out);
1314 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_PRE_SSA);
1315
1316 prog->convertToSSA();
1317
1318 if (prog->dbgFlags & NV50_IR_DEBUG_VERBOSE)
1319 prog->print();
1320
1321 prog->optimizeSSA(info->optLevel);
1322 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_SSA);
1323
1324 if (prog->dbgFlags & NV50_IR_DEBUG_BASIC)
1325 prog->print();
1326
1327 if (!prog->registerAllocation()) {
1328 ret = -4;
1329 goto out;
1330 }
1331 prog->getTarget()->runLegalizePass(prog, nv50_ir::CG_STAGE_POST_RA);
1332
1333 prog->optimizePostRA(info->optLevel);
1334
1335 if (!prog->emitBinary(info_out)) {
1336 ret = -5;
1337 goto out;
1338 }
1339
1340 out:
1341 INFO_DBG(prog->dbgFlags, VERBOSE, "nv50_ir_generate_code: ret = %i\n", ret);
1342
1343 info_out->bin.maxGPR = prog->maxGPR;
1344 info_out->bin.code = prog->code;
1345 info_out->bin.codeSize = prog->binSize;
1346 info_out->bin.tlsSpace = align(prog->tlsSize, 0x10);
1347
1348 delete prog;
1349 nv50_ir::Target::destroy(targ);
1350
1351 return ret;
1352 }
1353
1354 } // extern "C"