mesa/st: glsl_to_tgsi: add tests for the new temporary lifetime tracker
[mesa.git] / src / mesa / state_tracker / st_glsl_to_tgsi_temprename.cpp
1 /*
2 * Copyright © 2017 Gert Wollny
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 (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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "st_glsl_to_tgsi_temprename.h"
25 #include <tgsi/tgsi_info.h>
26 #include <tgsi/tgsi_strings.h>
27 #include <program/prog_instruction.h>
28 #include <limits>
29 #include <cstdlib>
30
31 /* std::sort is significantly faster than qsort */
32 #define USE_STL_SORT
33 #ifdef USE_STL_SORT
34 #include <algorithm>
35 #endif
36
37 #ifndef NDEBUG
38 #include <iostream>
39 #include <iomanip>
40 #include <program/prog_print.h>
41 #include <util/debug.h>
42 using std::cerr;
43 using std::setw;
44 #endif
45
46 using std::numeric_limits;
47
48 /* Without c++11 define the nullptr for forward-compatibility
49 * and better readibility */
50 #if __cplusplus < 201103L
51 #define nullptr 0
52 #endif
53
54 #ifndef NDEBUG
55 /* Helper function to check whether we want to seen debugging output */
56 static inline bool is_debug_enabled ()
57 {
58 static int debug_enabled = -1;
59 if (debug_enabled < 0)
60 debug_enabled = env_var_as_boolean("GLSL_TO_TGSI_RENAME_DEBUG", false);
61 return debug_enabled > 0;
62 }
63 #define RENAME_DEBUG(X) if (is_debug_enabled()) do { X; } while (false);
64 #else
65 #define RENAME_DEBUG(X)
66 #endif
67
68 namespace {
69
70 enum prog_scope_type {
71 outer_scope, /* Outer program scope */
72 loop_body, /* Inside a loop */
73 if_branch, /* Inside if branch */
74 else_branch, /* Inside else branch */
75 switch_body, /* Inside switch statmenet */
76 switch_case_branch, /* Inside switch case statmenet */
77 switch_default_branch, /* Inside switch default statmenet */
78 undefined_scope
79 };
80
81 class prog_scope {
82 public:
83 prog_scope(prog_scope *parent, prog_scope_type type, int id,
84 int depth, int begin);
85
86 prog_scope_type type() const;
87 prog_scope *parent() const;
88 int nesting_depth() const;
89 int id() const;
90 int end() const;
91 int begin() const;
92 int loop_break_line() const;
93
94 const prog_scope *in_ifelse_scope() const;
95 const prog_scope *in_switchcase_scope() const;
96 const prog_scope *innermost_loop() const;
97 const prog_scope *outermost_loop() const;
98 const prog_scope *enclosing_conditional() const;
99
100 bool is_loop() const;
101 bool is_in_loop() const;
102 bool is_conditional() const;
103 bool is_conditional_in_loop() const;
104
105 bool break_is_for_switchcase() const;
106 bool contains_range_of(const prog_scope& other) const;
107 const st_src_reg *switch_register() const;
108
109 void set_end(int end);
110 void set_loop_break_line(int line);
111
112 private:
113 prog_scope_type scope_type;
114 int scope_id;
115 int scope_nesting_depth;
116 int scope_begin;
117 int scope_end;
118 int break_loop_line;
119 prog_scope *parent_scope;
120 const st_src_reg *switch_reg;
121 };
122
123 /* Some storage class to encapsulate the prog_scope (de-)allocations */
124 class prog_scope_storage {
125 public:
126 prog_scope_storage(void *mem_ctx, int n);
127 ~prog_scope_storage();
128 prog_scope * create(prog_scope *p, prog_scope_type type, int id,
129 int lvl, int s_begin);
130 private:
131 void *mem_ctx;
132 int current_slot;
133 prog_scope *storage;
134 };
135
136 class temp_comp_access {
137 public:
138 temp_comp_access();
139 void record_read(int line, prog_scope *scope);
140 void record_write(int line, prog_scope *scope);
141 lifetime get_required_lifetime();
142 private:
143 void propagate_lifetime_to_dominant_write_scope();
144
145 prog_scope *last_read_scope;
146 prog_scope *first_read_scope;
147 prog_scope *first_write_scope;
148 int first_write;
149 int last_read;
150 int last_write;
151 int first_read;
152 bool keep_for_full_loop;
153 };
154
155 class temp_access {
156 public:
157 temp_access();
158 void record_read(int line, prog_scope *scope, int swizzle);
159 void record_write(int line, prog_scope *scope, int writemask);
160 lifetime get_required_lifetime();
161 private:
162 void update_access_mask(int mask);
163
164 temp_comp_access comp[4];
165 int access_mask;
166 bool needs_component_tracking;
167 };
168
169 prog_scope_storage::prog_scope_storage(void *mc, int n):
170 mem_ctx(mc),
171 current_slot(0)
172 {
173 storage = ralloc_array(mem_ctx, prog_scope, n);
174 }
175
176 prog_scope_storage::~prog_scope_storage()
177 {
178 ralloc_free(storage);
179 }
180
181 prog_scope*
182 prog_scope_storage::create(prog_scope *p, prog_scope_type type, int id,
183 int lvl, int s_begin)
184 {
185 storage[current_slot] = prog_scope(p, type, id, lvl, s_begin);
186 return &storage[current_slot++];
187 }
188
189 prog_scope::prog_scope(prog_scope *parent, prog_scope_type type, int id,
190 int depth, int scope_begin):
191 scope_type(type),
192 scope_id(id),
193 scope_nesting_depth(depth),
194 scope_begin(scope_begin),
195 scope_end(-1),
196 break_loop_line(numeric_limits<int>::max()),
197 parent_scope(parent),
198 switch_reg(nullptr)
199 {
200 }
201
202 prog_scope_type prog_scope::type() const
203 {
204 return scope_type;
205 }
206
207 prog_scope *prog_scope::parent() const
208 {
209 return parent_scope;
210 }
211
212 int prog_scope::nesting_depth() const
213 {
214 return scope_nesting_depth;
215 }
216
217 bool prog_scope::is_loop() const
218 {
219 return (scope_type == loop_body);
220 }
221
222 bool prog_scope::is_in_loop() const
223 {
224 if (scope_type == loop_body)
225 return true;
226
227 if (parent_scope)
228 return parent_scope->is_in_loop();
229
230 return false;
231 }
232
233 bool prog_scope::is_conditional_in_loop() const
234 {
235 return is_conditional() && is_in_loop();
236 }
237
238 const prog_scope *prog_scope::innermost_loop() const
239 {
240 if (scope_type == loop_body)
241 return this;
242
243 if (parent_scope)
244 return parent_scope->innermost_loop();
245
246 return nullptr;
247 }
248
249 const prog_scope *prog_scope::outermost_loop() const
250 {
251 const prog_scope *loop = nullptr;
252 const prog_scope *p = this;
253
254 do {
255 if (p->type() == loop_body)
256 loop = p;
257 p = p->parent();
258 } while (p);
259
260 return loop;
261 }
262
263 const prog_scope *prog_scope::enclosing_conditional() const
264 {
265 if (is_conditional())
266 return this;
267
268 if (parent_scope)
269 return parent_scope->enclosing_conditional();
270
271 return nullptr;
272 }
273
274 bool prog_scope::contains_range_of(const prog_scope& other) const
275 {
276 return (begin() <= other.begin()) && (end() >= other.end());
277 }
278
279 bool prog_scope::is_conditional() const
280 {
281 return scope_type == if_branch ||
282 scope_type == else_branch ||
283 scope_type == switch_case_branch ||
284 scope_type == switch_default_branch;
285 }
286
287 const prog_scope *prog_scope::in_ifelse_scope() const
288 {
289 if (scope_type == if_branch ||
290 scope_type == else_branch)
291 return this;
292
293 if (parent_scope)
294 return parent_scope->in_ifelse_scope();
295
296 return nullptr;
297 }
298
299 const st_src_reg *prog_scope::switch_register() const
300 {
301 return switch_reg;
302 }
303
304 const prog_scope *prog_scope::in_switchcase_scope() const
305 {
306 if (scope_type == switch_case_branch ||
307 scope_type == switch_default_branch)
308 return this;
309
310 if (parent_scope)
311 return parent_scope->in_switchcase_scope();
312
313 return nullptr;
314 }
315
316 bool prog_scope::break_is_for_switchcase() const
317 {
318 if (scope_type == loop_body)
319 return false;
320
321 if (scope_type == switch_case_branch ||
322 scope_type == switch_default_branch ||
323 scope_type == switch_body)
324 return true;
325
326 if (parent_scope)
327 return parent_scope->break_is_for_switchcase();
328
329 return false;
330 }
331
332 int prog_scope::id() const
333 {
334 return scope_id;
335 }
336
337 int prog_scope::begin() const
338 {
339 return scope_begin;
340 }
341
342 int prog_scope::end() const
343 {
344 return scope_end;
345 }
346
347 void prog_scope::set_end(int end)
348 {
349 if (scope_end == -1)
350 scope_end = end;
351 }
352
353 void prog_scope::set_loop_break_line(int line)
354 {
355 if (scope_type == loop_body) {
356 break_loop_line = MIN2(break_loop_line, line);
357 } else {
358 if (parent_scope)
359 parent()->set_loop_break_line(line);
360 }
361 }
362
363 int prog_scope::loop_break_line() const
364 {
365 return break_loop_line;
366 }
367
368 temp_access::temp_access():
369 access_mask(0),
370 needs_component_tracking(false)
371 {
372 }
373
374 void temp_access::update_access_mask(int mask)
375 {
376 if (access_mask && access_mask != mask)
377 needs_component_tracking = true;
378 access_mask |= mask;
379 }
380
381 void temp_access::record_write(int line, prog_scope *scope, int writemask)
382 {
383 update_access_mask(writemask);
384
385 if (writemask & WRITEMASK_X)
386 comp[0].record_write(line, scope);
387 if (writemask & WRITEMASK_Y)
388 comp[1].record_write(line, scope);
389 if (writemask & WRITEMASK_Z)
390 comp[2].record_write(line, scope);
391 if (writemask & WRITEMASK_W)
392 comp[3].record_write(line, scope);
393 }
394
395 void temp_access::record_read(int line, prog_scope *scope, int swizzle)
396 {
397 int readmask = 0;
398 for (int idx = 0; idx < 4; ++idx) {
399 int swz = GET_SWZ(swizzle, idx);
400 readmask |= (1 << swz) & 0xF;
401 }
402 update_access_mask(readmask);
403
404 if (readmask & WRITEMASK_X)
405 comp[0].record_read(line, scope);
406 if (readmask & WRITEMASK_Y)
407 comp[1].record_read(line, scope);
408 if (readmask & WRITEMASK_Z)
409 comp[2].record_read(line, scope);
410 if (readmask & WRITEMASK_W)
411 comp[3].record_read(line, scope);
412 }
413
414 inline static lifetime make_lifetime(int b, int e)
415 {
416 lifetime lt;
417 lt.begin = b;
418 lt.end = e;
419 return lt;
420 }
421
422 lifetime temp_access::get_required_lifetime()
423 {
424 lifetime result = make_lifetime(-1, -1);
425
426 unsigned mask = access_mask;
427 while (mask) {
428 unsigned chan = u_bit_scan(&mask);
429 lifetime lt = comp[chan].get_required_lifetime();
430
431 if (lt.begin >= 0) {
432 if ((result.begin < 0) || (result.begin > lt.begin))
433 result.begin = lt.begin;
434 }
435
436 if (lt.end > result.end)
437 result.end = lt.end;
438
439 if (!needs_component_tracking)
440 break;
441 }
442 return result;
443 }
444
445 temp_comp_access::temp_comp_access():
446 last_read_scope(nullptr),
447 first_read_scope(nullptr),
448 first_write_scope(nullptr),
449 first_write(-1),
450 last_read(-1),
451 last_write(-1),
452 first_read(numeric_limits<int>::max())
453 {
454 }
455
456 void temp_comp_access::record_read(int line, prog_scope *scope)
457 {
458 last_read_scope = scope;
459 last_read = line;
460
461 if (first_read > line) {
462 first_read = line;
463 first_read_scope = scope;
464 }
465 }
466
467 void temp_comp_access::record_write(int line, prog_scope *scope)
468 {
469 last_write = line;
470
471 if (first_write < 0) {
472 first_write = line;
473 first_write_scope = scope;
474 }
475 }
476
477 void temp_comp_access::propagate_lifetime_to_dominant_write_scope()
478 {
479 first_write = first_write_scope->begin();
480 int lr = first_write_scope->end();
481
482 if (last_read < lr)
483 last_read = lr;
484 }
485
486 lifetime temp_comp_access::get_required_lifetime()
487 {
488 bool keep_for_full_loop = false;
489
490 /* This register component is not used at all, or only read,
491 * mark it as unused and ignore it when renaming.
492 * glsl_to_tgsi_visitor::renumber_registers will take care of
493 * eliminating registers that are not written to.
494 */
495 if (last_write < 0)
496 return make_lifetime(-1, -1);
497
498 assert(first_write_scope);
499
500 /* Only written to, just make sure the register component is not
501 * reused in the range it is used to write to
502 */
503 if (!last_read_scope)
504 return make_lifetime(first_write, last_write + 1);
505
506 const prog_scope *enclosing_scope_first_read = first_read_scope;
507 const prog_scope *enclosing_scope_first_write = first_write_scope;
508
509 /* We read before writing in a loop
510 * hence the value must survive the loops
511 */
512 if ((first_read <= first_write) &&
513 first_read_scope->is_in_loop()) {
514 keep_for_full_loop = true;
515 enclosing_scope_first_read = first_read_scope->outermost_loop();
516 }
517
518 /* A conditional write within a nested loop must survive
519 * the outermost loop, but only if it is read outside
520 * the condition scope where we write.
521 */
522 const prog_scope *conditional = enclosing_scope_first_write->enclosing_conditional();
523 if (conditional && conditional->is_in_loop() &&
524 !conditional->contains_range_of(*last_read_scope)) {
525 keep_for_full_loop = true;
526 enclosing_scope_first_write = conditional->outermost_loop();
527 }
528
529 /* Evaluate the scope that is shared by all: required first write scope,
530 * required first read before write scope, and last read scope.
531 */
532 const prog_scope *enclosing_scope = enclosing_scope_first_read;
533 if (enclosing_scope_first_write->contains_range_of(*enclosing_scope))
534 enclosing_scope = enclosing_scope_first_write;
535
536 if (enclosing_scope_first_read->contains_range_of(*enclosing_scope))
537 enclosing_scope = enclosing_scope_first_read;
538
539 while (!enclosing_scope->contains_range_of(*enclosing_scope_first_write) ||
540 !enclosing_scope->contains_range_of(*last_read_scope)) {
541 enclosing_scope = enclosing_scope->parent();
542 assert(enclosing_scope);
543 }
544
545 /* Propagate the last read scope to the target scope */
546 while (enclosing_scope->nesting_depth() < last_read_scope->nesting_depth()) {
547 /* If the read is in a loop and we have to move up the scope we need to
548 * extend the life time to the end of this current loop because at this
549 * point we don't know whether the component was written before
550 * un-conditionally in the same loop.
551 */
552 if (last_read_scope->is_loop())
553 last_read = last_read_scope->end();
554
555 last_read_scope = last_read_scope->parent();
556 }
557
558 /* If the variable has to be kept for the whole loop, and we
559 * are currently in a loop, then propagate the life time.
560 */
561 if (keep_for_full_loop && first_write_scope->is_loop())
562 propagate_lifetime_to_dominant_write_scope();
563
564 /* Propagate the first_dominant_write scope to the target scope */
565 while (enclosing_scope->nesting_depth() < first_write_scope->nesting_depth()) {
566 /* Propagate lifetime if there was a break in a loop and the write was
567 * after the break inside that loop. Note, that this is only needed if
568 * we move up in the scopes.
569 */
570 if (first_write_scope->loop_break_line() < first_write) {
571 keep_for_full_loop = true;
572 propagate_lifetime_to_dominant_write_scope();
573 }
574
575 first_write_scope = first_write_scope->parent();
576
577 /* Propagte lifetime if we are now in a loop */
578 if (keep_for_full_loop && first_write_scope->is_loop())
579 propagate_lifetime_to_dominant_write_scope();
580 }
581
582 /* The last write past the last read is dead code, but we have to
583 * ensure that the component is not reused too early, hence extend the
584 * lifetime past the last write.
585 */
586 if (last_write >= last_read)
587 last_read = last_write + 1;
588
589 /* Here we are at the same scope, all is resolved */
590 return make_lifetime(first_write, last_read);
591 }
592
593 }
594
595 #ifndef NDEBUG
596 /* Function used for debugging. */
597 static void dump_instruction(int line, prog_scope *scope,
598 const glsl_to_tgsi_instruction& inst);
599 #endif
600
601 /* Scan the program and estimate the required register life times.
602 * The array lifetimes must be pre-allocated
603 */
604 bool
605 get_temp_registers_required_lifetimes(void *mem_ctx, exec_list *instructions,
606 int ntemps, struct lifetime *lifetimes)
607 {
608 int line = 0;
609 int loop_id = 0;
610 int if_id = 0;
611 int switch_id = 0;
612 bool is_at_end = false;
613 int n_scopes = 1;
614
615 /* Count scopes to allocate the needed space without the need for
616 * re-allocation
617 */
618 foreach_in_list(glsl_to_tgsi_instruction, inst, instructions) {
619 if (inst->op == TGSI_OPCODE_BGNLOOP ||
620 inst->op == TGSI_OPCODE_SWITCH ||
621 inst->op == TGSI_OPCODE_CASE ||
622 inst->op == TGSI_OPCODE_IF ||
623 inst->op == TGSI_OPCODE_UIF ||
624 inst->op == TGSI_OPCODE_ELSE ||
625 inst->op == TGSI_OPCODE_DEFAULT)
626 ++n_scopes;
627 }
628
629 prog_scope_storage scopes(mem_ctx, n_scopes);
630 temp_access *acc = new temp_access[ntemps];
631
632 prog_scope *cur_scope = scopes.create(nullptr, outer_scope, 0, 0, line);
633
634 RENAME_DEBUG(cerr << "========= Begin shader ============\n");
635
636 foreach_in_list(glsl_to_tgsi_instruction, inst, instructions) {
637 if (is_at_end) {
638 assert(!"GLSL_TO_TGSI: shader has instructions past end marker");
639 break;
640 }
641
642 RENAME_DEBUG(dump_instruction(line, cur_scope, *inst));
643
644 switch (inst->op) {
645 case TGSI_OPCODE_BGNLOOP: {
646 cur_scope = scopes.create(cur_scope, loop_body, loop_id++,
647 cur_scope->nesting_depth() + 1, line);
648 break;
649 }
650 case TGSI_OPCODE_ENDLOOP: {
651 cur_scope->set_end(line);
652 cur_scope = cur_scope->parent();
653 assert(cur_scope);
654 break;
655 }
656 case TGSI_OPCODE_IF:
657 case TGSI_OPCODE_UIF: {
658 assert(num_inst_src_regs(inst) == 1);
659 const st_src_reg& src = inst->src[0];
660 if (src.file == PROGRAM_TEMPORARY)
661 acc[src.index].record_read(line, cur_scope, src.swizzle);
662 cur_scope = scopes.create(cur_scope, if_branch, if_id++,
663 cur_scope->nesting_depth() + 1, line + 1);
664 break;
665 }
666 case TGSI_OPCODE_ELSE: {
667 assert(cur_scope->type() == if_branch);
668 cur_scope->set_end(line - 1);
669 cur_scope = scopes.create(cur_scope->parent(), else_branch,
670 cur_scope->id(), cur_scope->nesting_depth(),
671 line + 1);
672 break;
673 }
674 case TGSI_OPCODE_END: {
675 cur_scope->set_end(line);
676 is_at_end = true;
677 break;
678 }
679 case TGSI_OPCODE_ENDIF: {
680 cur_scope->set_end(line - 1);
681 cur_scope = cur_scope->parent();
682 assert(cur_scope);
683 break;
684 }
685 case TGSI_OPCODE_SWITCH: {
686 assert(num_inst_src_regs(inst) == 1);
687 const st_src_reg& src = inst->src[0];
688 prog_scope *scope = scopes.create(cur_scope, switch_body, switch_id++,
689 cur_scope->nesting_depth() + 1, line);
690 /* We record the read only for the SWITCH statement itself, like it
691 * is used by the only consumer of TGSI_OPCODE_SWITCH in tgsi_exec.c.
692 */
693 if (src.file == PROGRAM_TEMPORARY)
694 acc[src.index].record_read(line, cur_scope, src.swizzle);
695 cur_scope = scope;
696 break;
697 }
698 case TGSI_OPCODE_ENDSWITCH: {
699 cur_scope->set_end(line - 1);
700 /* Remove the case level, it might not have been
701 * closed with a break.
702 */
703 if (cur_scope->type() != switch_body)
704 cur_scope = cur_scope->parent();
705
706 cur_scope = cur_scope->parent();
707 assert(cur_scope);
708 break;
709 }
710 case TGSI_OPCODE_CASE: {
711 /* Take care of tracking the registers. */
712 prog_scope *switch_scope = cur_scope->type() == switch_body ?
713 cur_scope : cur_scope->parent();
714
715 assert(num_inst_src_regs(inst) == 1);
716 const st_src_reg& src = inst->src[0];
717 if (src.file == PROGRAM_TEMPORARY)
718 acc[src.index].record_read(line, switch_scope, src.swizzle);
719
720 /* Fall through to allocate the scope. */
721 }
722 case TGSI_OPCODE_DEFAULT: {
723 prog_scope_type t = inst->op == TGSI_OPCODE_CASE ? switch_case_branch
724 : switch_default_branch;
725 prog_scope *switch_scope = (cur_scope->type() == switch_body) ?
726 cur_scope : cur_scope->parent();
727 assert(switch_scope->type() == switch_body);
728 prog_scope *scope = scopes.create(switch_scope, t,
729 switch_scope->id(),
730 switch_scope->nesting_depth() + 1,
731 line);
732 /* Previous case falls through, so scope was not yet closed. */
733 if ((cur_scope != switch_scope) && (cur_scope->end() == -1))
734 cur_scope->set_end(line - 1);
735 cur_scope = scope;
736 break;
737 }
738 case TGSI_OPCODE_BRK: {
739 if (cur_scope->break_is_for_switchcase()) {
740 cur_scope->set_end(line - 1);
741 } else {
742 cur_scope->set_loop_break_line(line);
743 }
744 break;
745 }
746 case TGSI_OPCODE_CAL:
747 case TGSI_OPCODE_RET:
748 /* These opcodes are not supported and if a subroutine would
749 * be called in a shader, then the lifetime tracking would have
750 * to follow that call to see which registers are used there.
751 * Since this is not done, we have to bail out here and signal
752 * that no register merge will take place.
753 */
754 return false;
755 default: {
756 for (unsigned j = 0; j < num_inst_src_regs(inst); j++) {
757 const st_src_reg& src = inst->src[j];
758 if (src.file == PROGRAM_TEMPORARY)
759 acc[src.index].record_read(line, cur_scope, src.swizzle);
760 }
761 for (unsigned j = 0; j < inst->tex_offset_num_offset; j++) {
762 const st_src_reg& src = inst->tex_offsets[j];
763 if (src.file == PROGRAM_TEMPORARY)
764 acc[src.index].record_read(line, cur_scope, src.swizzle);
765 }
766 for (unsigned j = 0; j < num_inst_dst_regs(inst); j++) {
767 const st_dst_reg& dst = inst->dst[j];
768 if (dst.file == PROGRAM_TEMPORARY)
769 acc[dst.index].record_write(line, cur_scope, dst.writemask);
770 }
771 }
772 }
773 ++line;
774 }
775
776 RENAME_DEBUG(cerr << "==================================\n\n");
777
778 /* Make sure last scope is closed, even though no
779 * TGSI_OPCODE_END was given.
780 */
781 if (cur_scope->end() < 0)
782 cur_scope->set_end(line - 1);
783
784 RENAME_DEBUG(cerr << "========= lifetimes ==============\n");
785 for(int i = 0; i < ntemps; ++i) {
786 RENAME_DEBUG(cerr << setw(4) << i);
787 lifetimes[i] = acc[i].get_required_lifetime();
788 RENAME_DEBUG(cerr << ": [" << lifetimes[i].begin << ", "
789 << lifetimes[i].end << "]\n");
790 }
791 RENAME_DEBUG(cerr << "==================================\n\n");
792
793 delete[] acc;
794 return true;
795 }
796
797 /* Code below used for debugging */
798 #ifndef NDEBUG
799 static const char swizzle_txt[] = "xyzw";
800
801 static const char *tgsi_file_names[PROGRAM_FILE_MAX] = {
802 "TEMP", "ARRAY", "IN", "OUT", "STATE", "CONST",
803 "UNIFORM", "WO", "ADDR", "SAMPLER", "SV", "UNDEF",
804 "IMM", "BUF", "MEM", "IMAGE"
805 };
806
807 static
808 void dump_instruction(int line, prog_scope *scope,
809 const glsl_to_tgsi_instruction& inst)
810 {
811 const struct tgsi_opcode_info *info = tgsi_get_opcode_info(inst.op);
812
813 int indent = scope->nesting_depth();
814 if ((scope->type() == switch_case_branch ||
815 scope->type() == switch_default_branch) &&
816 (info->opcode == TGSI_OPCODE_CASE ||
817 info->opcode == TGSI_OPCODE_DEFAULT))
818 --indent;
819
820 if (info->opcode == TGSI_OPCODE_ENDIF ||
821 info->opcode == TGSI_OPCODE_ELSE ||
822 info->opcode == TGSI_OPCODE_ENDLOOP ||
823 info->opcode == TGSI_OPCODE_ENDSWITCH)
824 --indent;
825
826 cerr << setw(4) << line << ": ";
827 for (int i = 0; i < indent; ++i)
828 cerr << " ";
829 cerr << tgsi_get_opcode_name(info->opcode) << " ";
830
831 bool has_operators = false;
832 for (unsigned j = 0; j < num_inst_dst_regs(&inst); j++) {
833 has_operators = true;
834 if (j > 0)
835 cerr << ", ";
836
837 const st_dst_reg& dst = inst.dst[j];
838 cerr << tgsi_file_names[dst.file];
839
840 if (dst.file == PROGRAM_ARRAY)
841 cerr << "(" << dst.array_id << ")";
842
843 cerr << "[" << dst.index << "]";
844
845 if (dst.writemask != TGSI_WRITEMASK_XYZW) {
846 cerr << ".";
847 if (dst.writemask & TGSI_WRITEMASK_X) cerr << "x";
848 if (dst.writemask & TGSI_WRITEMASK_Y) cerr << "y";
849 if (dst.writemask & TGSI_WRITEMASK_Z) cerr << "z";
850 if (dst.writemask & TGSI_WRITEMASK_W) cerr << "w";
851 }
852 }
853 if (has_operators)
854 cerr << " := ";
855
856 for (unsigned j = 0; j < num_inst_src_regs(&inst); j++) {
857 if (j > 0)
858 cerr << ", ";
859
860 const st_src_reg& src = inst.src[j];
861 cerr << tgsi_file_names[src.file]
862 << "[" << src.index << "]";
863 if (src.swizzle != SWIZZLE_XYZW) {
864 cerr << ".";
865 for (int idx = 0; idx < 4; ++idx) {
866 int swz = GET_SWZ(src.swizzle, idx);
867 if (swz < 4) {
868 cerr << swizzle_txt[swz];
869 }
870 }
871 }
872 }
873
874 if (inst.tex_offset_num_offset > 0) {
875 cerr << ", TEXOFS: ";
876 for (unsigned j = 0; j < inst.tex_offset_num_offset; j++) {
877 if (j > 0)
878 cerr << ", ";
879
880 const st_src_reg& src = inst.tex_offsets[j];
881 cerr << tgsi_file_names[src.file]
882 << "[" << src.index << "]";
883 if (src.swizzle != SWIZZLE_XYZW) {
884 cerr << ".";
885 for (int idx = 0; idx < 4; ++idx) {
886 int swz = GET_SWZ(src.swizzle, idx);
887 if (swz < 4) {
888 cerr << swizzle_txt[swz];
889 }
890 }
891 }
892 }
893 }
894 cerr << "\n";
895 }
896 #endif