summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/r600/sb/sb_pass.h
blob: 0346df1b16786d4b748ba7568f699a046178e319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
 * Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *      Vadim Girlin
 */

#ifndef SB_PASS_H_
#define SB_PASS_H_

#include <stack>

namespace r600_sb {

class pass {
protected:
	sb_context &ctx;
	shader &sh;

public:
	pass(shader &s);

	virtual int run();

	virtual ~pass() {}
};

class vpass : public pass {

public:

	vpass(shader &s) : pass(s) {}

	virtual int init();
	virtual int done();

	virtual int run();
	virtual void run_on(container_node &n);

	virtual bool visit(node &n, bool enter);
	virtual bool visit(container_node &n, bool enter);
	virtual bool visit(alu_group_node &n, bool enter);
	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(alu_packed_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);
	virtual bool visit(region_node &n, bool enter);
	virtual bool visit(repeat_node &n, bool enter);
	virtual bool visit(depart_node &n, bool enter);
	virtual bool visit(if_node &n, bool enter);
	virtual bool visit(bb_node &n, bool enter);

};

class rev_vpass : public vpass {

public:
	rev_vpass(shader &s) : vpass(s) {}

	virtual void run_on(container_node &n);
};


// =================== PASSES

class bytecode;

class bc_dump : public vpass {
	using vpass::visit;

	uint32_t *bc_data;
	unsigned ndw;

	unsigned id;

	unsigned new_group, group_index;

public:

	bc_dump(shader &s, bytecode *bc = NULL);

	bc_dump(shader &s, uint32_t *bc_ptr, unsigned ndw) :
		vpass(s), bc_data(bc_ptr), ndw(ndw), id(), new_group(), group_index() {}

	virtual int init();
	virtual int done();

	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);

	void dump_dw(unsigned dw_id, unsigned count = 2);

	void dump(cf_node& n);
	void dump(alu_node& n);
	void dump(fetch_node& n);
};


class dce_cleanup : public vpass {
	using vpass::visit;

	bool remove_unused;

public:

	dce_cleanup(shader &s) : vpass(s),
		remove_unused(s.dce_flags & DF_REMOVE_UNUSED) {}

	virtual bool visit(node &n, bool enter);
	virtual bool visit(alu_group_node &n, bool enter);
	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(alu_packed_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);
	virtual bool visit(region_node &n, bool enter);
	virtual bool visit(container_node &n, bool enter);

private:

	void cleanup_dst(node &n);
	bool cleanup_dst_vec(vvec &vv);

};


class def_use : public pass {

public:

	def_use(shader &sh) : pass(sh) {}

	virtual int run();
	void run_on(node *n, bool defs);

private:

	void process_uses(node *n);
	void process_defs(node *n, vvec &vv, bool arr_def);
	void process_phi(container_node *c, bool defs, bool uses);
};



class dump : public vpass {
	using vpass::visit;

	int level;

public:

	dump(shader &s) : vpass(s), level(0) {}

	virtual bool visit(node &n, bool enter);
	virtual bool visit(container_node &n, bool enter);
	virtual bool visit(alu_group_node &n, bool enter);
	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(alu_packed_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);
	virtual bool visit(region_node &n, bool enter);
	virtual bool visit(repeat_node &n, bool enter);
	virtual bool visit(depart_node &n, bool enter);
	virtual bool visit(if_node &n, bool enter);
	virtual bool visit(bb_node &n, bool enter);


	static void dump_op(node &n, const char *name);
	static void dump_vec(const vvec & vv);
	static void dump_set(shader &sh, val_set & v);

	static void dump_rels(vvec & vv);

	static void dump_val(value *v);
	static void dump_op(node *n);

	static void dump_op_list(container_node *c);
	static void dump_queue(sched_queue &q);

	static void dump_alu(alu_node *n);

private:

	void indent();

	void dump_common(node &n);
	void dump_flags(node &n);

	void dump_live_values(container_node &n, bool before);
};


// Global Code Motion

class gcm : public pass {

	sched_queue bu_ready[SQ_NUM];
	sched_queue bu_ready_next[SQ_NUM];
	sched_queue bu_ready_early[SQ_NUM];
	sched_queue ready;
	sched_queue ready_above;

	container_node pending;

	struct op_info {
		bb_node* top_bb;
		bb_node* bottom_bb;
		op_info() : top_bb(), bottom_bb() {}
	};

	typedef std::map<node*, op_info> op_info_map;

	typedef std::map<node*, unsigned> nuc_map;

	op_info_map op_map;
	nuc_map uses;

	typedef std::vector<nuc_map> nuc_stack;

	nuc_stack nuc_stk;
	unsigned ucs_level;

	bb_node * bu_bb;

	vvec pending_defs;

	node_list pending_nodes;

	unsigned cur_sq;

	// for register pressure tracking in bottom-up pass
	val_set live;
	int live_count;

	static const int rp_threshold = 100;

	bool pending_exec_mask_update;

public:

	gcm(shader &sh) : pass(sh),
		bu_ready(), bu_ready_next(), bu_ready_early(),
		ready(), op_map(), uses(), nuc_stk(1), ucs_level(),
		bu_bb(), pending_defs(), pending_nodes(), cur_sq(),
		live(), live_count(), pending_exec_mask_update() {}

	virtual int run();

private:

	void collect_instructions(container_node *c, bool early_pass);

	void sched_early(container_node *n);
	void td_sched_bb(bb_node *bb);
	bool td_is_ready(node *n);
	void td_release_uses(vvec &v);
	void td_release_val(value *v);
	void td_schedule(bb_node *bb, node *n);

	void sched_late(container_node *n);
	void bu_sched_bb(bb_node *bb);
	void bu_release_defs(vvec &v, bool src);
	void bu_release_phi_defs(container_node *p, unsigned op);
	bool bu_is_ready(node *n);
	void bu_release_val(value *v);
	void bu_release_op(node * n);
	void bu_find_best_bb(node *n, op_info &oi);
	void bu_schedule(container_node *bb, node *n);

	void push_uc_stack();
	void pop_uc_stack();

	void init_def_count(nuc_map &m, container_node &s);
	void init_use_count(nuc_map &m, container_node &s);
	unsigned get_uc_vec(vvec &vv);
	unsigned get_dc_vec(vvec &vv, bool src);

	void add_ready(node *n);

	void dump_uc_stack();

	unsigned real_alu_count(sched_queue &q, unsigned max);

	// check if we have not less than threshold ready alu instructions
	bool check_alu_ready_count(unsigned threshold);
};


class gvn : public vpass {
	using vpass::visit;

public:

	gvn(shader &sh) : vpass(sh) {}

	virtual bool visit(node &n, bool enter);
	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(alu_packed_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);
	virtual bool visit(region_node &n, bool enter);

private:

	void process_op(node &n, bool rewrite = true);

	// returns true if the value was rewritten
	bool process_src(value* &v, bool rewrite);


	void process_alu_src_constants(node &n, value* &v);
};


class if_conversion : public pass {

public:

	if_conversion(shader &sh) : pass(sh) {}

	virtual int run();

	bool run_on(region_node *r);

	void convert_kill_instructions(region_node *r, value *em, bool branch,
	                               container_node *c);

	bool check_and_convert(region_node *r);

	alu_node* convert_phi(value *select, node *phi);

};


class liveness : public rev_vpass {
	using vpass::visit;

	val_set live;
	bool live_changed;

public:

	liveness(shader &s) : rev_vpass(s), live_changed(false) {}

	virtual int init();

	virtual bool visit(node &n, bool enter);
	virtual bool visit(bb_node &n, bool enter);
	virtual bool visit(container_node &n, bool enter);
	virtual bool visit(alu_group_node &n, bool enter);
	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(alu_packed_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);
	virtual bool visit(region_node &n, bool enter);
	virtual bool visit(repeat_node &n, bool enter);
	virtual bool visit(depart_node &n, bool enter);
	virtual bool visit(if_node &n, bool enter);

private:

	void update_interferences();
	void process_op(node &n);

	bool remove_val(value *v);
	bool remove_vec(vvec &v);
	bool process_outs(node& n);
	void process_ins(node& n);

	void process_phi_outs(container_node *phi);
	void process_phi_branch(container_node *phi, unsigned id);

	bool process_maydef(value *v);

	bool add_vec(vvec &vv, bool src);

	void update_src_vec(vvec &vv, bool src);
};


struct bool_op_info {
	bool invert;
	unsigned int_cvt;

	alu_node *n;
};

class peephole : public pass {

public:

	peephole(shader &sh) : pass(sh) {}

	virtual int run();

	void run_on(container_node *c);

	void optimize_cc_op(alu_node *a);

	void optimize_cc_op2(alu_node *a);
	void optimize_CNDcc_op(alu_node *a);

	bool get_bool_op_info(value *b, bool_op_info& bop);
	bool get_bool_flt_to_int_source(alu_node* &a);
	void convert_float_setcc(alu_node *f2i, alu_node *s);
};


class psi_ops : public rev_vpass {
	using rev_vpass::visit;

public:

	psi_ops(shader &s) : rev_vpass(s) {}

	virtual bool visit(node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);

	bool try_inline(node &n);
	bool try_reduce(node &n);
	bool eliminate(node &n);

	void unpredicate(node *n);
};


// check correctness of the generated code, e.g.:
// - expected source operand value is the last value written to its gpr,
// - all arguments of phi node should be allocated to the same gpr,
// TODO other tests
class ra_checker : public pass {

	typedef std::map<sel_chan, value *> reg_value_map;

	typedef std::vector<reg_value_map> regmap_stack;

	regmap_stack rm_stack;
	unsigned rm_stk_level;

	value* prev_dst[5];

public:

	ra_checker(shader &sh) : pass(sh), rm_stk_level(0), prev_dst() {}

	virtual int run();

	void run_on(container_node *c);

	void dump_error(const error_info &e);
	void dump_all_errors();

private:

	reg_value_map& rmap() { return rm_stack[rm_stk_level]; }

	void push_stack();
	void pop_stack();

	// when going out of the alu clause, values in the clause temporary gprs,
	// AR, predicate values, PS/PV are destroyed
	void kill_alu_only_regs();
	void error(node *n, unsigned id, std::string msg);

	void check_phi_src(container_node *p, unsigned id);
	void process_phi_dst(container_node *p);
	void check_alu_group(alu_group_node *g);
	void process_op_dst(node *n);
	void check_op_src(node *n);
	void check_src_vec(node *n, unsigned id, vvec &vv, bool src);
	void check_value_gpr(node *n, unsigned id, value *v);
};

// =======================================


class ra_coalesce : public pass {

public:

	ra_coalesce(shader &sh) : pass(sh) {}

	virtual int run();
};



// =======================================

class ra_init : public pass {

public:

	ra_init(shader &sh) : pass(sh), prev_chans() {

		// The parameter below affects register channels distribution.
		// For cayman (VLIW-4) we're trying to distribute the channels
		// uniformly, this means significantly better alu slots utilization
		// at the expense of higher gpr usage. Hopefully this will improve
		// performance, though it has to be proven with real benchmarks yet.
		// For VLIW-5 this method could also slightly improve slots
		// utilization, but increased register pressure seems more significant
		// and overall performance effect is negative according to some
		// benchmarks, so it's not used currently. Basically, VLIW-5 doesn't
		// really need it because trans slot (unrestricted by register write
		// channel) allows to consume most deviations from uniform channel
		// distribution.
		// Value 3 means that for new allocation we'll use channel that differs
		// from 3 last used channels. 0 for VLIW-5 effectively turns this off.

		ra_tune = sh.get_ctx().is_cayman() ? 3 : 0;
	}

	virtual int run();

private:

	unsigned prev_chans;
	unsigned ra_tune;

	void add_prev_chan(unsigned chan);
	unsigned get_preferable_chan_mask();

	void ra_node(container_node *c);
	void process_op(node *n);

	void color(value *v);

	void color_bs_constraint(ra_constraint *c);

	void assign_color(value *v, sel_chan c);
	void alloc_arrays();
};

// =======================================

class ra_split : public pass {

public:

	ra_split(shader &sh) : pass(sh) {}

	virtual int run();

	void split(container_node *n);
	void split_op(node *n);
	void split_alu_packed(alu_packed_node *n);
	void split_vector_inst(node *n);

	void split_packed_ins(alu_packed_node *n);

#if 0
	void split_pinned_outs(node *n);
#endif

	void split_vec(vvec &vv, vvec &v1, vvec &v2, bool allow_swz);

	void split_phi_src(container_node *loc, container_node *c, unsigned id,
	                   bool loop);
	void split_phi_dst(node *loc, container_node *c, bool loop);
	void init_phi_constraints(container_node *c);
};



class ssa_prepare : public vpass {
	using vpass::visit;

	typedef std::vector<val_set> vd_stk;
	vd_stk stk;

	unsigned level;

public:
	ssa_prepare(shader &s) : vpass(s), level(0) {}

	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);
	virtual bool visit(region_node &n, bool enter);
	virtual bool visit(repeat_node &n, bool enter);
	virtual bool visit(depart_node &n, bool enter);

private:

	void push_stk() {
		++level;
		if (level + 1 > stk.size())
			stk.resize(level+1);
		else
			stk[level].clear();
	}
	void pop_stk() {
		assert(level);
		--level;
		stk[level].add_set(stk[level + 1]);
	}

	void add_defs(node &n);

	val_set & cur_set() { return stk[level]; }

	container_node* create_phi_nodes(int count);
};

class ssa_rename : public vpass {
	using vpass::visit;

	typedef sb_map<value*, unsigned> def_map;

	def_map def_count;
	std::stack<def_map> rename_stack;

	typedef std::map<uint32_t, value*> val_map;
	val_map values;

public:

	ssa_rename(shader &s) : vpass(s) {}

	virtual int init();

	virtual bool visit(container_node &n, bool enter);
	virtual bool visit(node &n, bool enter);
	virtual bool visit(alu_group_node &n, bool enter);
	virtual bool visit(cf_node &n, bool enter);
	virtual bool visit(alu_node &n, bool enter);
	virtual bool visit(alu_packed_node &n, bool enter);
	virtual bool visit(fetch_node &n, bool enter);
	virtual bool visit(region_node &n, bool enter);
	virtual bool visit(repeat_node &n, bool enter);
	virtual bool visit(depart_node &n, bool enter);
	virtual bool visit(if_node &n, bool enter);

private:

	void push(node *phi);
	void pop();

	unsigned get_index(def_map& m, value* v);
	void set_index(def_map& m, value* v, unsigned index);
	unsigned new_index(def_map& m, value* v);

	value* rename_use(node *n, value* v);
	value* rename_def(node *def, value* v);

	void rename_src_vec(node *n, vvec &vv, bool src);
	void rename_dst_vec(node *def, vvec &vv, bool set_def);

	void rename_src(node *n);
	void rename_dst(node *n);

	void rename_phi_args(container_node *phi, unsigned op, bool def);

	void rename_virt(node *n);
	void rename_virt_val(node *n, value *v);
};

class bc_finalizer : public pass {

	cf_node *last_export[EXP_TYPE_COUNT];
	cf_node *last_cf;

	unsigned ngpr;
	unsigned nstack;

public:

	bc_finalizer(shader &sh) : pass(sh), last_export(), last_cf(), ngpr(),
		nstack() {}

	virtual int run();

	void finalize_loop(region_node *r);
	void finalize_if(region_node *r);

	void run_on(container_node *c);

	void insert_rv6xx_load_ar_workaround(alu_group_node *b4);
	void finalize_alu_group(alu_group_node *g, node *prev_node);
	bool finalize_alu_src(alu_group_node *g, alu_node *a, alu_group_node *prev_node);

	void emit_set_grad(fetch_node* f);
	void finalize_fetch(fetch_node *f);

	void finalize_cf(cf_node *c);

	sel_chan translate_kcache(cf_node *alu, value *v);

	void update_ngpr(unsigned gpr);
	void update_nstack(region_node *r, unsigned add = 0);

	unsigned get_stack_depth(node *n, unsigned &loops, unsigned &ifs,
	                         unsigned add = 0);

	void cf_peephole();

private:
	void copy_fetch_src(fetch_node &dst, fetch_node &src, unsigned arg_start);
	void emit_set_texture_offsets(fetch_node &f);
};


} // namespace r600_sb

#endif /* SB_PASS_H_ */