r600g/sb: initial commit of the optimizing shader backend
[mesa.git] / src / gallium / drivers / r600 / sb / sb_ir.h
1 /*
2 * Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Vadim Girlin
25 */
26
27 #ifndef R600_SB_IR_H_
28 #define R600_SB_IR_H_
29
30 #include <algorithm>
31 #include <stdint.h>
32 #include <iostream>
33 #include <vector>
34 #include <set>
35 #include <algorithm>
36
37 #include "sb_bc.h"
38
39 namespace r600_sb {
40
41 enum special_regs {
42 SV_ALU_PRED = 128,
43 SV_EXEC_MASK,
44 SV_AR_INDEX,
45 SV_VALID_MASK
46 };
47
48 class node;
49 class value;
50 class shader;
51
52 struct sel_chan
53 {
54 unsigned id;
55
56 sel_chan(unsigned id = 0) : id(id) {}
57 sel_chan(unsigned sel, unsigned chan) : id(((sel << 2) | chan) + 1) {}
58
59 unsigned sel() const { return sel(id); }
60 unsigned chan() const {return chan(id); }
61 operator unsigned() const {return id;}
62
63 static unsigned sel(unsigned idx) { return (idx-1) >> 2; }
64 static unsigned chan(unsigned idx) { return (idx-1) & 3; }
65 };
66
67 inline std::ostream& operator <<(std::ostream& o, sel_chan r) {
68 static const char * ch = "xyzw";
69 o << r.sel() << "." << ch[r.chan()];
70 return o;
71 }
72
73 typedef std::vector<value*> vvec;
74
75 class sb_pool {
76 protected:
77 static const unsigned SB_POOL_ALIGN = 8;
78 static const unsigned SB_POOL_DEFAULT_BLOCK_SIZE = (1 << 16);
79
80 typedef std::vector<void*> block_vector;
81
82 unsigned block_size;
83 block_vector blocks;
84 unsigned total_size;
85
86 public:
87 sb_pool(unsigned block_size = SB_POOL_DEFAULT_BLOCK_SIZE)
88 : block_size(block_size), blocks(), total_size() {}
89
90 virtual ~sb_pool() { free_all(); }
91
92 void* allocate(unsigned sz);
93
94 protected:
95 void free_all();
96 };
97
98 template <typename V, typename Comp = std::less<V> >
99 class sb_set {
100 typedef std::vector<V> data_vector;
101 data_vector vec;
102 public:
103
104 typedef typename data_vector::iterator iterator;
105 typedef typename data_vector::const_iterator const_iterator;
106
107 sb_set() : vec() {}
108 ~sb_set() { }
109
110 iterator begin() { return vec.begin(); }
111 iterator end() { return vec.end(); }
112 const_iterator begin() const { return vec.begin(); }
113 const_iterator end() const { return vec.end(); }
114
115 void add_set(const sb_set& s) {
116 data_vector t;
117 t.reserve(vec.size() + s.vec.size());
118 std::set_union(vec.begin(), vec.end(), s.vec.begin(), s.vec.end(),
119 std::inserter(t, t.begin()), Comp());
120 vec.swap(t);
121 }
122
123 iterator lower_bound(const V& v) {
124 return std::lower_bound(vec.begin(), vec.end(), v, Comp());
125 }
126
127 std::pair<iterator, bool> insert(const V& v) {
128 iterator P = lower_bound(v);
129 if (P != vec.end() && is_equal(*P, v))
130 return std::make_pair(P, false);
131 return std::make_pair(vec.insert(P, v), true);
132 }
133
134 unsigned erase(const V& v) {
135 iterator P = lower_bound(v);
136 if (P == vec.end() || !is_equal(*P, v))
137 return 0;
138 vec.erase(P);
139 return 1;
140 }
141
142 void clear() { vec.clear(); }
143
144 bool empty() { return vec.empty(); }
145
146 bool is_equal(const V& v1, const V& v2) {
147 return !Comp()(v1, v2) && !Comp()(v2, v1);
148 }
149
150 iterator find(const V& v) {
151 iterator P = lower_bound(v);
152 return (P != vec.end() && is_equal(*P, v)) ? P : vec.end();
153 }
154
155 unsigned size() { return vec.size(); }
156 void erase(iterator I) { vec.erase(I); }
157 };
158
159 template <typename K, typename V, typename KComp = std::less<K> >
160 class sb_map {
161 typedef std::pair<K, V> datatype;
162
163 struct Comp {
164 bool operator()(const datatype &v1, const datatype &v2) {
165 return KComp()(v1.first, v2.first);
166 }
167 };
168
169 typedef sb_set<datatype, Comp> dataset;
170
171 dataset set;
172
173 public:
174
175 sb_map() : set() {}
176
177 typedef typename dataset::iterator iterator;
178
179 iterator begin() { return set.begin(); }
180 iterator end() { return set.end(); }
181
182 void clear() { set.clear(); }
183
184 V& operator[](const K& key) {
185 datatype P = std::make_pair(key, V());
186 iterator F = set.find(P);
187 if (F == set.end()) {
188 return (*(set.insert(P).first)).second;
189 } else {
190 return (*F).second;
191 }
192 }
193
194 std::pair<iterator, bool> insert(const datatype& d) {
195 return set.insert(d);
196 }
197
198 iterator find(const K& key) {
199 return set.find(std::make_pair(key, V()));
200 }
201
202 unsigned erase(const K& key) {
203 return set.erase(std::make_pair(key, V()));
204 }
205
206 void erase(iterator I) {
207 set.erase(I);
208 }
209 };
210
211 class sb_bitset {
212 typedef uint32_t basetype;
213 static const unsigned bt_bits = sizeof(basetype) << 3;
214 std::vector<basetype> data;
215 unsigned bit_size;
216
217 public:
218
219 sb_bitset() : data(), bit_size() {}
220
221 bool get(unsigned id);
222 void set(unsigned id, bool bit = true);
223 bool set_chk(unsigned id, bool bit = true);
224
225 void clear();
226 void resize(unsigned size);
227
228 unsigned size() { return bit_size; }
229
230 unsigned find_bit(unsigned start = 0);
231
232 void swap(sb_bitset & bs2);
233
234 bool operator==(const sb_bitset &bs2);
235 bool operator!=(const sb_bitset &bs2) { return !(*this == bs2); }
236
237 sb_bitset& operator|=(const sb_bitset &bs2) {
238 if (bit_size < bs2.bit_size) {
239 resize(bs2.bit_size);
240 }
241
242 for (unsigned i = 0, c = std::min(data.size(), bs2.data.size()); i < c;
243 ++i) {
244 data[i] |= bs2.data[i];
245 }
246 return *this;
247 }
248
249 sb_bitset& operator&=(const sb_bitset &bs2);
250 sb_bitset& mask(const sb_bitset &bs2);
251
252 friend sb_bitset operator|(const sb_bitset &b1, const sb_bitset &b2) {
253 sb_bitset nbs(b1);
254 nbs |= b2;
255 return nbs;
256 }
257 };
258
259 class value;
260
261 enum value_kind {
262 VLK_REG,
263 VLK_REL_REG,
264 VLK_SPECIAL_REG,
265 VLK_TEMP,
266
267 VLK_CONST,
268 VLK_KCACHE,
269 VLK_PARAM,
270 VLK_SPECIAL_CONST,
271
272 VLK_UNDEF
273 };
274
275
276
277 class sb_value_pool : protected sb_pool {
278 unsigned aligned_elt_size;
279
280 public:
281 sb_value_pool(unsigned elt_size, unsigned block_elts = 256)
282 : sb_pool(block_elts * (aligned_elt_size = ((elt_size +
283 SB_POOL_ALIGN - 1) & ~(SB_POOL_ALIGN - 1)))) {}
284
285 virtual ~sb_value_pool() { delete_all(); }
286
287 value* create(value_kind k, sel_chan regid, unsigned ver);
288
289 value* operator[](unsigned id) {
290 unsigned offset = id * aligned_elt_size;
291 unsigned block_id;
292 if (offset < block_size) {
293 block_id = 0;
294 } else {
295 block_id = offset / block_size;
296 offset = offset % block_size;
297 }
298 return (value*)((char*)blocks[block_id] + offset);
299 }
300
301 unsigned size() { return total_size / aligned_elt_size; }
302
303 protected:
304 void delete_all();
305 };
306
307
308
309
310
311 class sb_value_set {
312
313 sb_bitset bs;
314
315 public:
316 sb_value_set() : bs() {}
317
318 class iterator {
319 sb_value_pool &vp;
320 sb_value_set *s;
321 unsigned nb;
322 public:
323 iterator(shader &sh, sb_value_set *s, unsigned nb = 0);
324
325
326 iterator& operator++() {
327 if (nb + 1 < s->bs.size())
328 nb = s->bs.find_bit(nb + 1);
329 else
330 nb = s->bs.size();
331 return *this;
332 }
333 bool operator !=(const iterator &i) {
334 return s != i.s || nb != i.nb;
335 }
336 bool operator ==(const iterator &i) { return !(*this != i); }
337 value* operator *() {
338 return vp[nb];
339 }
340
341
342 };
343
344 iterator begin(shader &sh) {
345 return iterator(sh, this, bs.size() ? bs.find_bit(0) : 0);
346 }
347 iterator end(shader &sh) { return iterator(sh, this, bs.size()); }
348
349 bool add_set_checked(sb_value_set & s2);
350
351 void add_set(sb_value_set & s2) {
352 if (bs.size() < s2.bs.size())
353 bs.resize(s2.bs.size());
354 bs |= s2.bs;
355 }
356
357 void remove_set(sb_value_set & s2);
358
359 bool add_vec(vvec &vv);
360
361 bool add_val(value *v);
362 bool contains(value *v);
363
364 bool remove_val(value *v);
365
366 bool remove_vec(vvec &vv);
367
368 void clear();
369
370 bool empty();
371 };
372
373 typedef sb_value_set val_set;
374
375 struct gpr_array {
376 sel_chan base_gpr; // original gpr
377 sel_chan gpr; // assigned by regalloc
378 unsigned array_size;
379
380 gpr_array(sel_chan base_gpr, unsigned array_size) : base_gpr(base_gpr),
381 array_size(array_size) {}
382
383 unsigned hash() { return (base_gpr << 10) * array_size; }
384
385 val_set interferences;
386 vvec refs;
387
388 bool is_dead();
389
390 };
391
392 typedef std::vector<gpr_array*> regarray_vec;
393
394 enum value_flags {
395 VLF_UNDEF = (1 << 0),
396 VLF_READONLY = (1 << 1),
397 VLF_DEAD = (1 << 2),
398
399 VLF_PIN_REG = (1 << 3),
400 VLF_PIN_CHAN = (1 << 4),
401
402 // opposite to alu clause local value - goes through alu clause boundary
403 // (can't use temp gpr, can't recolor in the alu scheduler, etc)
404 VLF_GLOBAL = (1 << 5),
405 VLF_FIXED = (1 << 6),
406 VLF_PVPS = (1 << 7),
407
408 VLF_PREALLOC = (1 << 8)
409 };
410
411 inline value_flags operator |(value_flags l, value_flags r) {
412 return (value_flags)((unsigned)l|(unsigned)r);
413 }
414 inline value_flags operator &(value_flags l, value_flags r) {
415 return (value_flags)((unsigned)l&(unsigned)r);
416 }
417 inline value_flags operator ~(value_flags l) {
418 return (value_flags)(~(unsigned)l);
419 }
420 inline value_flags& operator |=(value_flags &l, value_flags r) {
421 l = l | r;
422 return l;
423 }
424 inline value_flags& operator &=(value_flags &l, value_flags r) {
425 l = l & r;
426 return l;
427 }
428
429 struct value;
430
431 std::ostream& operator << (std::ostream &o, value &v);
432
433 typedef uint32_t value_hash;
434
435 enum use_kind {
436 UK_SRC,
437 UK_SRC_REL,
438 UK_DST_REL,
439 UK_MAYDEF,
440 UK_MAYUSE,
441 UK_PRED,
442 UK_COND
443 };
444
445 struct use_info {
446 use_info *next;
447 node *op;
448 use_kind kind;
449 int arg;
450
451 use_info(node *n, use_kind kind, int arg, use_info* next)
452 : next(next), op(n), kind(kind), arg(arg) {}
453 };
454
455 enum constraint_kind {
456 CK_SAME_REG,
457 CK_PACKED_BS,
458 CK_PHI
459 };
460
461 class shader;
462 class sb_value_pool;
463 class ra_chunk;
464 class ra_constraint;
465
466 class value {
467 protected:
468 value(unsigned sh_id, value_kind k, sel_chan select, unsigned ver = 0)
469 : kind(k), flags(),
470 rel(), array(),
471 version(ver), select(select), pin_gpr(select), gpr(),
472 gvn_source(), ghash(),
473 def(), adef(), uses(), constraint(), chunk(),
474 literal_value(), uid(sh_id) {}
475
476 ~value() { delete_uses(); }
477
478 friend class sb_value_pool;
479 public:
480 value_kind kind;
481 value_flags flags;
482
483 vvec mdef;
484 vvec muse;
485 value *rel;
486 gpr_array *array;
487
488 unsigned version;
489
490 sel_chan select;
491 sel_chan pin_gpr;
492 sel_chan gpr;
493
494 value *gvn_source;
495 value_hash ghash;
496
497 node *def, *adef;
498 use_info *uses;
499
500 ra_constraint *constraint;
501 ra_chunk *chunk;
502
503 literal literal_value;
504
505 bool is_const() { return kind == VLK_CONST || kind == VLK_UNDEF; }
506
507 bool is_AR() {
508 return is_special_reg() && select == sel_chan(SV_AR_INDEX, 0);
509 }
510
511 node* any_def() {
512 assert(!(def && adef));
513 return def ? def : adef;
514 }
515
516 value* gvalue() {
517 value *v = this;
518 while (v->gvn_source && v != v->gvn_source)
519 // FIXME we really shouldn't have such chains
520 v = v->gvn_source;
521 return v;
522 }
523
524 bool is_float_0_or_1() {
525 value *v = gvalue();
526 return v->is_const() && (v->literal_value == literal(0)
527 || v->literal_value == literal(1.0f));
528 }
529
530 bool is_undef() { return gvalue()->kind == VLK_UNDEF; }
531
532 bool is_any_gpr() {
533 return (kind == VLK_REG || kind == VLK_TEMP);
534 }
535
536 bool is_agpr() {
537 return array && is_any_gpr();
538 }
539
540 // scalar gpr, as opposed to element of gpr array
541 bool is_sgpr() {
542 return !array && is_any_gpr();
543 }
544
545 bool is_special_reg() { return kind == VLK_SPECIAL_REG; }
546 bool is_any_reg() { return is_any_gpr() || is_special_reg(); }
547 bool is_kcache() { return kind == VLK_KCACHE; }
548 bool is_rel() { return kind == VLK_REL_REG; }
549 bool is_readonly() { return flags & VLF_READONLY; }
550
551 bool is_chan_pinned() { return flags & VLF_PIN_CHAN; }
552 bool is_reg_pinned() { return flags & VLF_PIN_REG; }
553
554 bool is_global();
555 void set_global();
556 void set_prealloc();
557
558 bool is_prealloc();
559
560 bool is_fixed();
561 void fix();
562
563 bool is_dead() { return flags & VLF_DEAD; }
564
565 literal & get_const_value() {
566 value *v = gvalue();
567 assert(v->is_const());
568 return v->literal_value;
569 }
570
571 // true if needs to be encoded as literal in alu
572 bool is_literal() {
573 return is_const()
574 && literal_value != literal(0)
575 && literal_value != literal(1)
576 && literal_value != literal(-1)
577 && literal_value != literal(0.5)
578 && literal_value != literal(1.0);
579 }
580
581 void add_use(node *n, use_kind kind, int arg);
582
583 value_hash hash();
584 value_hash rel_hash();
585
586 void assign_source(value *v) {
587 assert(!gvn_source || gvn_source == this);
588 gvn_source = v->gvalue();
589 }
590
591 bool v_equal(value *v) { return gvalue() == v->gvalue(); }
592
593 unsigned use_count();
594 void delete_uses();
595
596 sel_chan get_final_gpr() {
597 if (array && array->gpr) {
598 int reg_offset = select.sel() - array->base_gpr.sel();
599 if (rel && rel->is_const())
600 reg_offset += rel->get_const_value().i;
601 return array->gpr + (reg_offset << 2);
602 } else {
603 return gpr;
604 }
605 }
606
607 unsigned get_final_chan() {
608 if (array) {
609 assert(array->gpr);
610 return array->gpr.chan();
611 } else {
612 assert(gpr);
613 return gpr.chan();
614 }
615 }
616
617 val_set interferences;
618 unsigned uid;
619 };
620
621 class expr_handler;
622
623 class value_table {
624 typedef std::vector<value*> vt_item;
625 typedef std::vector<vt_item> vt_table;
626
627 expr_handler &ex;
628
629 unsigned size_bits;
630 unsigned size;
631 unsigned size_mask;
632
633 vt_table hashtable;
634
635 unsigned cnt;
636
637 public:
638
639 value_table(expr_handler &ex, unsigned size_bits = 10)
640 : ex(ex), size_bits(size_bits), size(1u << size_bits),
641 size_mask(size - 1), hashtable(size), cnt() {}
642
643 ~value_table() {}
644
645 void add_value(value* v);
646
647 bool expr_equal(value* l, value* r);
648
649 unsigned count() { return cnt; }
650
651 void get_values(vvec & v);
652 };
653
654 class sb_context;
655
656 enum node_type {
657 NT_UNKNOWN,
658 NT_LIST,
659 NT_OP,
660 NT_REGION,
661 NT_REPEAT,
662 NT_DEPART,
663 NT_IF,
664 };
665
666 enum node_subtype {
667 NST_UNKNOWN,
668 NST_LIST,
669 NST_ALU_GROUP,
670 NST_ALU_CLAUSE,
671 NST_ALU_INST,
672 NST_ALU_PACKED_INST,
673 NST_CF_INST,
674 NST_FETCH_INST,
675 NST_TEX_CLAUSE,
676 NST_VTX_CLAUSE,
677
678 NST_BB,
679
680 NST_PHI,
681 NST_PSI,
682 NST_COPY,
683
684 NST_LOOP_PHI_CONTAINER,
685 NST_LOOP_CONTINUE,
686 NST_LOOP_BREAK
687 };
688
689 enum node_flags {
690 NF_EMPTY = 0,
691 NF_DEAD = (1 << 0),
692 NF_REG_CONSTRAINT = (1 << 1),
693 NF_CHAN_CONSTRAINT = (1 << 2),
694 NF_ALU_4SLOT = (1 << 3),
695 NF_CONTAINER = (1 << 4),
696
697 NF_COPY_MOV = (1 << 5),
698
699 NF_DONT_KILL = (1 << 6),
700 NF_DONT_HOIST = (1 << 7),
701 NF_DONT_MOVE = (1 << 8),
702
703 // for KILLxx - we want to schedule them as early as possible
704 NF_SCHEDULE_EARLY = (1 << 9)
705 };
706
707 inline node_flags operator |(node_flags l, node_flags r) {
708 return (node_flags)((unsigned)l|(unsigned)r);
709 }
710 inline node_flags& operator |=(node_flags &l, node_flags r) {
711 l = l | r;
712 return l;
713 }
714
715 inline node_flags& operator &=(node_flags &l, node_flags r) {
716 l = (node_flags)((unsigned)l & (unsigned)r);
717 return l;
718 }
719
720 inline node_flags operator ~(node_flags r) {
721 return (node_flags)~(unsigned)r;
722 }
723
724 struct node_stats {
725 unsigned alu_count;
726 unsigned alu_kill_count;
727 unsigned alu_copy_mov_count;
728 unsigned cf_count;
729 unsigned fetch_count;
730 unsigned region_count;
731 unsigned loop_count;
732 unsigned phi_count;
733 unsigned loop_phi_count;
734 unsigned depart_count;
735 unsigned repeat_count;
736 unsigned if_count;
737
738 node_stats() : alu_count(), alu_kill_count(), alu_copy_mov_count(),
739 cf_count(), fetch_count(), region_count(),
740 loop_count(), phi_count(), loop_phi_count(), depart_count(),
741 repeat_count(), if_count() {}
742
743 void dump();
744 };
745
746 class shader;
747
748 class vpass;
749
750 class container_node;
751 class region_node;
752
753 class node {
754
755 protected:
756 node(node_type nt, node_subtype nst, node_flags flags = NF_EMPTY)
757 : prev(), next(), parent(),
758 type(nt), subtype(nst), flags(flags),
759 pred(), dst(), src() {}
760
761 virtual ~node() {};
762
763 public:
764 node *prev, *next;
765 container_node *parent;
766
767 node_type type;
768 node_subtype subtype;
769 node_flags flags;
770
771 value *pred;
772
773 vvec dst;
774 vvec src;
775
776 virtual bool is_valid() { return true; }
777 virtual bool accept(vpass &p, bool enter);
778
779 void insert_before(node *n);
780 void insert_after(node *n);
781 void replace_with(node *n);
782 void remove();
783
784 virtual value_hash hash();
785 value_hash hash_src();
786
787 virtual bool fold_dispatch(expr_handler *ex);
788
789 bool is_container() { return flags & NF_CONTAINER; }
790
791 bool is_alu_packed() { return subtype == NST_ALU_PACKED_INST; }
792 bool is_alu_inst() { return subtype == NST_ALU_INST; }
793 bool is_alu_group() { return subtype == NST_ALU_GROUP; }
794 bool is_alu_clause() { return subtype == NST_ALU_CLAUSE; }
795
796 bool is_fetch_clause() {
797 return subtype == NST_TEX_CLAUSE || subtype == NST_VTX_CLAUSE;
798 }
799
800 bool is_copy() { return subtype == NST_COPY; }
801 bool is_copy_mov() { return flags & NF_COPY_MOV; }
802 bool is_any_alu() { return is_alu_inst() || is_alu_packed() || is_copy(); }
803
804 bool is_fetch_inst() { return subtype == NST_FETCH_INST; }
805 bool is_cf_inst() { return subtype == NST_CF_INST; }
806
807 bool is_region() { return type == NT_REGION; }
808 bool is_depart() { return type == NT_DEPART; }
809 bool is_repeat() { return type == NT_REPEAT; }
810 bool is_if() { return type == NT_IF; }
811 bool is_bb() { return subtype == NST_BB; }
812
813 bool is_phi() { return subtype == NST_PHI; }
814
815 bool is_dead() { return flags & NF_DEAD; }
816
817 bool is_cf_op(unsigned op);
818 bool is_alu_op(unsigned op);
819 bool is_fetch_op(unsigned op);
820
821 unsigned cf_op_flags();
822 unsigned alu_op_flags();
823 unsigned alu_op_slot_flags();
824 unsigned fetch_op_flags();
825
826 bool is_mova();
827 bool is_pred_set();
828
829 bool vec_uses_ar(vvec &vv) {
830 for (vvec::iterator I = vv.begin(), E = vv.end(); I != E; ++I) {
831 value *v = *I;
832 if (v && v->rel && !v->rel->is_const())
833 return true;
834 }
835 return false;
836 }
837
838 bool uses_ar() {
839 return vec_uses_ar(dst) || vec_uses_ar(src);
840 }
841
842
843 region_node* get_parent_region();
844
845 friend class shader;
846 };
847
848 class container_node : public node {
849 public:
850
851 container_node(node_type nt = NT_LIST, node_subtype nst = NST_LIST,
852 node_flags flags = NF_EMPTY)
853 : node(nt, nst, flags | NF_CONTAINER), first(), last(),
854 live_after(), live_before() {}
855
856 // child items list
857 node *first, *last;
858
859 val_set live_after;
860 val_set live_before;
861
862 class iterator {
863 node *p;
864 public:
865 iterator(node *pp = NULL) : p(pp) {}
866 iterator & operator ++() { p = p->next; return *this;}
867 iterator & operator --() { p = p->prev; return *this;}
868 node* operator *() { return p; }
869 node* operator ->() { return p; }
870 const iterator advance(int n) {
871 if (!n) return *this;
872 iterator I(p);
873 if (n > 0) while (n--) ++I;
874 else while (n++) --I;
875 return I;
876 }
877 const iterator operator +(int n) { return advance(n); }
878 const iterator operator -(int n) { return advance(-n); }
879 bool operator !=(const iterator &i) { return p != i.p; }
880 bool operator ==(const iterator &i) { return p == i.p; }
881 };
882
883 class riterator {
884 iterator i;
885 public:
886 riterator(node *p = NULL) : i(p) {}
887 riterator & operator ++() { --i; return *this;}
888 riterator & operator --() { ++i; return *this;}
889 node* operator *() { return *i; }
890 node* operator ->() { return *i; }
891 bool operator !=(const riterator &r) { return i != r.i; }
892 bool operator ==(const riterator &r) { return i == r.i; }
893 };
894
895 iterator begin() { return first; }
896 iterator end() { return NULL; }
897 riterator rbegin() { return last; }
898 riterator rend() { return NULL; }
899
900 bool empty() { assert(first != NULL || first == last); return !first; }
901 unsigned count();
902
903 // used with node containers that represent shceduling queues
904 // ignores copies and takes into account alu_packed_node items
905 unsigned real_alu_count();
906
907 void push_back(node *n);
908 void push_front(node *n);
909
910 void insert_node_before(node *s, node *n);
911 void insert_node_after(node *s, node *n);
912
913 void append_from(container_node *c);
914
915 // remove range [b..e) from some container and assign to this container
916 void move(iterator b, iterator e);
917
918 void expand();
919 void expand(container_node *n);
920 void remove_node(node *n);
921
922 node *cut(iterator b, iterator e);
923
924 void clear() { first = last = NULL; }
925
926 virtual bool is_valid() { return true; }
927 virtual bool accept(vpass &p, bool enter);
928 virtual bool fold_dispatch(expr_handler *ex);
929
930 node* front() { return first; }
931 node* back() { return last; }
932
933 void collect_stats(node_stats &s);
934
935 friend class shader;
936
937
938 };
939
940 typedef container_node::iterator node_iterator;
941 typedef container_node::riterator node_riterator;
942
943 class alu_group_node : public container_node {
944 protected:
945 alu_group_node() : container_node(NT_LIST, NST_ALU_GROUP), literals() {}
946 public:
947
948 std::vector<literal> literals;
949
950 virtual bool is_valid() { return subtype == NST_ALU_GROUP; }
951 virtual bool accept(vpass &p, bool enter);
952
953
954 unsigned literal_chan(literal l) {
955 std::vector<literal>::iterator F =
956 std::find(literals.begin(), literals.end(), l);
957 assert(F != literals.end());
958 return F - literals.begin();
959 }
960
961 friend class shader;
962 };
963
964 class cf_node : public container_node {
965 protected:
966 cf_node() : container_node(NT_OP, NST_CF_INST), bc(), jump_target(),
967 jump_after_target() {};
968 public:
969 bc_cf bc;
970
971 cf_node *jump_target;
972 bool jump_after_target;
973
974 virtual bool is_valid() { return subtype == NST_CF_INST; }
975 virtual bool accept(vpass &p, bool enter);
976 virtual bool fold_dispatch(expr_handler *ex);
977
978 void jump(cf_node *c) { jump_target = c; jump_after_target = false; }
979 void jump_after(cf_node *c) { jump_target = c; jump_after_target = true; }
980
981 friend class shader;
982 };
983
984 class alu_node : public node {
985 protected:
986 alu_node() : node(NT_OP, NST_ALU_INST), bc() {};
987 public:
988 bc_alu bc;
989
990 virtual bool is_valid() { return subtype == NST_ALU_INST; }
991 virtual bool accept(vpass &p, bool enter);
992 virtual bool fold_dispatch(expr_handler *ex);
993
994 unsigned forced_bank_swizzle() {
995 return ((bc.op_ptr->flags & AF_INTERP) && (bc.slot_flags == AF_4V)) ?
996 VEC_210 : 0;
997 }
998
999 // return param index + 1 if instruction references interpolation param,
1000 // otherwise 0
1001 unsigned interp_param();
1002
1003 alu_group_node *get_alu_group_node();
1004
1005 friend class shader;
1006 };
1007
1008 // for multi-slot instrs - DOT/INTERP/... (maybe useful for 64bit pairs later)
1009 class alu_packed_node : public container_node {
1010 protected:
1011 alu_packed_node() : container_node(NT_OP, NST_ALU_PACKED_INST) {}
1012 public:
1013
1014 const alu_op_info* op_ptr() {
1015 return static_cast<alu_node*>(first)->bc.op_ptr;
1016 }
1017 unsigned op() { return static_cast<alu_node*>(first)->bc.op; }
1018 void init_args();
1019
1020 virtual bool is_valid() { return subtype == NST_ALU_PACKED_INST; }
1021 virtual bool accept(vpass &p, bool enter);
1022 virtual bool fold_dispatch(expr_handler *ex);
1023
1024 unsigned get_slot_mask();
1025 void update_packed_items(sb_context &ctx);
1026
1027 friend class shader;
1028 };
1029
1030 class fetch_node : public node {
1031 protected:
1032 fetch_node() : node(NT_OP, NST_FETCH_INST), bc() {};
1033 public:
1034 bc_fetch bc;
1035
1036 virtual bool is_valid() { return subtype == NST_FETCH_INST; }
1037 virtual bool accept(vpass &p, bool enter);
1038 virtual bool fold_dispatch(expr_handler *ex);
1039
1040 bool uses_grad() { return bc.op_ptr->flags & FF_USEGRAD; }
1041
1042 friend class shader;
1043 };
1044
1045 class region_node;
1046
1047 class repeat_node : public container_node {
1048 protected:
1049 repeat_node(region_node *target, unsigned id)
1050 : container_node(NT_REPEAT, NST_LIST), target(target), rep_id(id) {}
1051 public:
1052 region_node *target;
1053 unsigned rep_id;
1054
1055 virtual bool accept(vpass &p, bool enter);
1056
1057 friend class shader;
1058 };
1059
1060 class depart_node : public container_node {
1061 protected:
1062 depart_node(region_node *target, unsigned id)
1063 : container_node(NT_DEPART, NST_LIST), target(target), dep_id(id) {}
1064 public:
1065 region_node *target;
1066 unsigned dep_id;
1067
1068 virtual bool accept(vpass &p, bool enter);
1069
1070 friend class shader;
1071 };
1072
1073 class if_node : public container_node {
1074 protected:
1075 if_node() : container_node(NT_IF, NST_LIST), cond() {};
1076 public:
1077 value *cond; // glued to pseudo output (dst[2]) of the PRED_SETxxx
1078
1079 virtual bool accept(vpass &p, bool enter);
1080
1081 friend class shader;
1082 };
1083
1084 typedef std::vector<depart_node*> depart_vec;
1085 typedef std::vector<repeat_node*> repeat_vec;
1086
1087 class region_node : public container_node {
1088 protected:
1089 region_node(unsigned id) : container_node(NT_REGION, NST_LIST), region_id(id),
1090 loop_phi(), phi(), vars_defined(), departs(), repeats() {}
1091 public:
1092 unsigned region_id;
1093
1094 container_node *loop_phi;
1095 container_node *phi;
1096
1097 val_set vars_defined;
1098
1099 depart_vec departs;
1100 repeat_vec repeats;
1101
1102 virtual bool accept(vpass &p, bool enter);
1103
1104 unsigned dep_count() { return departs.size(); }
1105 unsigned rep_count() { return repeats.size() + 1; }
1106
1107 bool is_loop() { return !repeats.empty(); }
1108
1109 container_node* get_entry_code_location() {
1110 node *p = first;
1111 while (p && (p->is_depart() || p->is_repeat()))
1112 p = static_cast<container_node*>(p)->first;
1113
1114 container_node *c = static_cast<container_node*>(p);
1115 if (c->is_bb())
1116 return c;
1117 else
1118 return c->parent;
1119 }
1120
1121 void expand_depart(depart_node *d);
1122 void expand_repeat(repeat_node *r);
1123
1124 friend class shader;
1125 };
1126
1127 class bb_node : public container_node {
1128 protected:
1129 bb_node(unsigned id, unsigned loop_level)
1130 : container_node(NT_LIST, NST_BB), id(id), loop_level(loop_level) {}
1131 public:
1132 unsigned id;
1133 unsigned loop_level;
1134
1135 virtual bool accept(vpass &p, bool enter);
1136
1137 friend class shader;
1138 };
1139
1140
1141 typedef std::vector<region_node*> regions_vec;
1142 typedef std::vector<bb_node*> bbs_vec;
1143 typedef std::list<node*> sched_queue;
1144 typedef sched_queue::iterator sq_iterator;
1145 typedef std::vector<node*> node_vec;
1146 typedef std::list<node*> node_list;
1147 typedef std::set<node*> node_set;
1148
1149
1150
1151 } // namespace r600_sb
1152
1153 #endif /* R600_SB_IR_H_ */