mesa/st/glsl_to_tgsi: Mark first write as unconditional when appropriate
[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 "util/bitscan.h"
29 #include <limits>
30 #include <cstdlib>
31
32 /* std::sort is significantly faster than qsort */
33 #define USE_STL_SORT
34 #ifdef USE_STL_SORT
35 #include <algorithm>
36 #endif
37
38 #ifndef NDEBUG
39 #include <iostream>
40 #include <iomanip>
41 #include "program/prog_print.h"
42 #include "util/debug.h"
43 using std::cerr;
44 using std::setw;
45 using std::ostream;
46 #endif
47
48 /* If <windows.h> is included this is defined and clashes with
49 * std::numeric_limits<>::max()
50 */
51 #ifdef max
52 #undef max
53 #endif
54
55 using std::numeric_limits;
56
57 /* Without c++11 define the nullptr for forward-compatibility
58 * and better readibility */
59 #if __cplusplus < 201103L
60 #define nullptr 0
61 #endif
62
63 #ifndef NDEBUG
64 /* Prepare to make it possible to specify log file */
65 static std::ostream& debug_log = cerr;
66
67 /* Helper function to check whether we want to seen debugging output */
68 static inline bool is_debug_enabled ()
69 {
70 static int debug_enabled = -1;
71 if (debug_enabled < 0)
72 debug_enabled = env_var_as_boolean("GLSL_TO_TGSI_RENAME_DEBUG", false);
73 return debug_enabled > 0;
74 }
75 #define RENAME_DEBUG(X) if (is_debug_enabled()) do { X; } while (false);
76 #else
77 #define RENAME_DEBUG(X)
78 #endif
79
80 namespace {
81
82 enum prog_scope_type {
83 outer_scope, /* Outer program scope */
84 loop_body, /* Inside a loop */
85 if_branch, /* Inside if branch */
86 else_branch, /* Inside else branch */
87 switch_body, /* Inside switch statmenet */
88 switch_case_branch, /* Inside switch case statmenet */
89 switch_default_branch, /* Inside switch default statmenet */
90 undefined_scope
91 };
92
93 class prog_scope {
94 public:
95 prog_scope(prog_scope *parent, prog_scope_type type, int id,
96 int depth, int begin);
97
98 prog_scope_type type() const;
99 prog_scope *parent() const;
100 int nesting_depth() const;
101 int id() const;
102 int end() const;
103 int begin() const;
104 int loop_break_line() const;
105
106 const prog_scope *in_else_scope() const;
107 const prog_scope *in_ifelse_scope() const;
108 const prog_scope *in_parent_ifelse_scope() const;
109 const prog_scope *innermost_loop() const;
110 const prog_scope *outermost_loop() const;
111 const prog_scope *enclosing_conditional() const;
112
113 bool is_loop() const;
114 bool is_in_loop() const;
115 bool is_switchcase_scope_in_loop() const;
116 bool is_conditional() const;
117 bool is_child_of(const prog_scope *scope) const;
118 bool is_child_of_ifelse_id_sibling(const prog_scope *scope) const;
119
120 bool break_is_for_switchcase() const;
121 bool contains_range_of(const prog_scope& other) const;
122
123 void set_end(int end);
124 void set_loop_break_line(int line);
125
126 private:
127 prog_scope_type scope_type;
128 int scope_id;
129 int scope_nesting_depth;
130 int scope_begin;
131 int scope_end;
132 int break_loop_line;
133 prog_scope *parent_scope;
134 };
135
136 /* Some storage class to encapsulate the prog_scope (de-)allocations */
137 class prog_scope_storage {
138 public:
139 prog_scope_storage(void *mem_ctx, int n);
140 ~prog_scope_storage();
141 prog_scope * create(prog_scope *p, prog_scope_type type, int id,
142 int lvl, int s_begin);
143 private:
144 void *mem_ctx;
145 int current_slot;
146 prog_scope *storage;
147 };
148
149 /* Class to track the access to a component of a temporary register. */
150
151 class temp_comp_access {
152 public:
153 temp_comp_access();
154
155 void record_read(int line, prog_scope *scope);
156 void record_write(int line, prog_scope *scope);
157 lifetime get_required_lifetime();
158 private:
159 void propagate_lifetime_to_dominant_write_scope();
160 bool conditional_ifelse_write_in_loop() const;
161
162 void record_ifelse_write(const prog_scope& scope);
163 void record_if_write(const prog_scope& scope);
164 void record_else_write(const prog_scope& scope);
165
166 prog_scope *last_read_scope;
167 prog_scope *first_read_scope;
168 prog_scope *first_write_scope;
169
170 int first_write;
171 int last_read;
172 int last_write;
173 int first_read;
174
175 /* This member variable tracks the current resolution of conditional writing
176 * to this temporary in IF/ELSE clauses.
177 *
178 * The initial value "conditionality_untouched" indicates that this
179 * temporary has not yet been written to within an if clause.
180 *
181 * A positive (other than "conditionality_untouched") number refers to the
182 * last loop id for which the write was resolved as unconditional. With each
183 * new loop this value will be overwitten by "conditionality_unresolved"
184 * on entering the first IF clause writing this temporary.
185 *
186 * The value "conditionality_unresolved" indicates that no resolution has
187 * been achieved so far. If the variable is set to this value at the end of
188 * the processing of the whole shader it also indicates a conditional write.
189 *
190 * The value "write_is_conditional" marks that the variable is written
191 * conditionally (i.e. not in all relevant IF/ELSE code path pairs) in at
192 * least one loop.
193 */
194 int conditionality_in_loop_id;
195
196 /* Helper constants to make the tracking code more readable. */
197 static const int write_is_conditional = -1;
198 static const int conditionality_unresolved = 0;
199 static const int conditionality_untouched;
200 static const int write_is_unconditional;
201
202 /* A bit field tracking the nexting levels of if-else clauses where the
203 * temporary has (so far) been written to in the if branch, but not in the
204 * else branch.
205 */
206 unsigned int if_scope_write_flags;
207
208 int next_ifelse_nesting_depth;
209 static const int supported_ifelse_nesting_depth = 32;
210
211 /* Tracks the last if scope in which the temporary was written to
212 * without a write in the correspondig else branch. Is also used
213 * to track read-before-write in the according scope.
214 */
215 const prog_scope *current_unpaired_if_write_scope;
216
217 /* Flag to resolve read-before-write in the else scope. */
218 bool was_written_in_current_else_scope;
219 };
220
221 const int
222 temp_comp_access::conditionality_untouched = numeric_limits<int>::max();
223
224 const int
225 temp_comp_access::write_is_unconditional = numeric_limits<int>::max() - 1;
226
227 /* Class to track the access to all components of a temporary register. */
228 class temp_access {
229 public:
230 temp_access();
231 void record_read(int line, prog_scope *scope, int swizzle);
232 void record_write(int line, prog_scope *scope, int writemask);
233 lifetime get_required_lifetime();
234 private:
235 void update_access_mask(int mask);
236
237 temp_comp_access comp[4];
238 int access_mask;
239 bool needs_component_tracking;
240 };
241
242 prog_scope_storage::prog_scope_storage(void *mc, int n):
243 mem_ctx(mc),
244 current_slot(0)
245 {
246 storage = ralloc_array(mem_ctx, prog_scope, n);
247 }
248
249 prog_scope_storage::~prog_scope_storage()
250 {
251 ralloc_free(storage);
252 }
253
254 prog_scope*
255 prog_scope_storage::create(prog_scope *p, prog_scope_type type, int id,
256 int lvl, int s_begin)
257 {
258 storage[current_slot] = prog_scope(p, type, id, lvl, s_begin);
259 return &storage[current_slot++];
260 }
261
262 prog_scope::prog_scope(prog_scope *parent, prog_scope_type type, int id,
263 int depth, int scope_begin):
264 scope_type(type),
265 scope_id(id),
266 scope_nesting_depth(depth),
267 scope_begin(scope_begin),
268 scope_end(-1),
269 break_loop_line(numeric_limits<int>::max()),
270 parent_scope(parent)
271 {
272 }
273
274 prog_scope_type prog_scope::type() const
275 {
276 return scope_type;
277 }
278
279 prog_scope *prog_scope::parent() const
280 {
281 return parent_scope;
282 }
283
284 int prog_scope::nesting_depth() const
285 {
286 return scope_nesting_depth;
287 }
288
289 bool prog_scope::is_loop() const
290 {
291 return (scope_type == loop_body);
292 }
293
294 bool prog_scope::is_in_loop() const
295 {
296 if (scope_type == loop_body)
297 return true;
298
299 if (parent_scope)
300 return parent_scope->is_in_loop();
301
302 return false;
303 }
304
305 const prog_scope *prog_scope::innermost_loop() const
306 {
307 if (scope_type == loop_body)
308 return this;
309
310 if (parent_scope)
311 return parent_scope->innermost_loop();
312
313 return nullptr;
314 }
315
316 const prog_scope *prog_scope::outermost_loop() const
317 {
318 const prog_scope *loop = nullptr;
319 const prog_scope *p = this;
320
321 do {
322 if (p->type() == loop_body)
323 loop = p;
324 p = p->parent();
325 } while (p);
326
327 return loop;
328 }
329
330 bool prog_scope::is_child_of_ifelse_id_sibling(const prog_scope *scope) const
331 {
332 const prog_scope *my_parent = in_parent_ifelse_scope();
333 while (my_parent) {
334 /* is a direct child? */
335 if (my_parent == scope)
336 return false;
337 /* is a child of the conditions sibling? */
338 if (my_parent->id() == scope->id())
339 return true;
340 my_parent = my_parent->in_parent_ifelse_scope();
341 }
342 return false;
343 }
344
345 bool prog_scope::is_child_of(const prog_scope *scope) const
346 {
347 const prog_scope *my_parent = parent();
348 while (my_parent) {
349 if (my_parent == scope)
350 return true;
351 my_parent = my_parent->parent();
352 }
353 return false;
354 }
355
356 const prog_scope *prog_scope::enclosing_conditional() const
357 {
358 if (is_conditional())
359 return this;
360
361 if (parent_scope)
362 return parent_scope->enclosing_conditional();
363
364 return nullptr;
365 }
366
367 bool prog_scope::contains_range_of(const prog_scope& other) const
368 {
369 return (begin() <= other.begin()) && (end() >= other.end());
370 }
371
372 bool prog_scope::is_conditional() const
373 {
374 return scope_type == if_branch ||
375 scope_type == else_branch ||
376 scope_type == switch_case_branch ||
377 scope_type == switch_default_branch;
378 }
379
380 const prog_scope *prog_scope::in_else_scope() const
381 {
382 if (scope_type == else_branch)
383 return this;
384
385 if (parent_scope)
386 return parent_scope->in_else_scope();
387
388 return nullptr;
389 }
390
391 const prog_scope *prog_scope::in_parent_ifelse_scope() const
392 {
393 if (parent_scope)
394 return parent_scope->in_ifelse_scope();
395 else
396 return nullptr;
397 }
398
399 const prog_scope *prog_scope::in_ifelse_scope() const
400 {
401 if (scope_type == if_branch ||
402 scope_type == else_branch)
403 return this;
404
405 if (parent_scope)
406 return parent_scope->in_ifelse_scope();
407
408 return nullptr;
409 }
410
411 bool prog_scope::is_switchcase_scope_in_loop() const
412 {
413 return (scope_type == switch_case_branch ||
414 scope_type == switch_default_branch) &&
415 is_in_loop();
416 }
417
418 bool prog_scope::break_is_for_switchcase() const
419 {
420 if (scope_type == loop_body)
421 return false;
422
423 if (scope_type == switch_case_branch ||
424 scope_type == switch_default_branch ||
425 scope_type == switch_body)
426 return true;
427
428 if (parent_scope)
429 return parent_scope->break_is_for_switchcase();
430
431 return false;
432 }
433
434 int prog_scope::id() const
435 {
436 return scope_id;
437 }
438
439 int prog_scope::begin() const
440 {
441 return scope_begin;
442 }
443
444 int prog_scope::end() const
445 {
446 return scope_end;
447 }
448
449 void prog_scope::set_end(int end)
450 {
451 if (scope_end == -1)
452 scope_end = end;
453 }
454
455 void prog_scope::set_loop_break_line(int line)
456 {
457 if (scope_type == loop_body) {
458 break_loop_line = MIN2(break_loop_line, line);
459 } else {
460 if (parent_scope)
461 parent()->set_loop_break_line(line);
462 }
463 }
464
465 int prog_scope::loop_break_line() const
466 {
467 return break_loop_line;
468 }
469
470 temp_access::temp_access():
471 access_mask(0),
472 needs_component_tracking(false)
473 {
474 }
475
476 void temp_access::update_access_mask(int mask)
477 {
478 if (access_mask && access_mask != mask)
479 needs_component_tracking = true;
480 access_mask |= mask;
481 }
482
483 void temp_access::record_write(int line, prog_scope *scope, int writemask)
484 {
485 update_access_mask(writemask);
486
487 if (writemask & WRITEMASK_X)
488 comp[0].record_write(line, scope);
489 if (writemask & WRITEMASK_Y)
490 comp[1].record_write(line, scope);
491 if (writemask & WRITEMASK_Z)
492 comp[2].record_write(line, scope);
493 if (writemask & WRITEMASK_W)
494 comp[3].record_write(line, scope);
495 }
496
497 void temp_access::record_read(int line, prog_scope *scope, int swizzle)
498 {
499 int readmask = 0;
500 for (int idx = 0; idx < 4; ++idx) {
501 int swz = GET_SWZ(swizzle, idx);
502 readmask |= (1 << swz) & 0xF;
503 }
504 update_access_mask(readmask);
505
506 if (readmask & WRITEMASK_X)
507 comp[0].record_read(line, scope);
508 if (readmask & WRITEMASK_Y)
509 comp[1].record_read(line, scope);
510 if (readmask & WRITEMASK_Z)
511 comp[2].record_read(line, scope);
512 if (readmask & WRITEMASK_W)
513 comp[3].record_read(line, scope);
514 }
515
516 inline static lifetime make_lifetime(int b, int e)
517 {
518 lifetime lt;
519 lt.begin = b;
520 lt.end = e;
521 return lt;
522 }
523
524 lifetime temp_access::get_required_lifetime()
525 {
526 lifetime result = make_lifetime(-1, -1);
527
528 unsigned mask = access_mask;
529 while (mask) {
530 unsigned chan = u_bit_scan(&mask);
531 lifetime lt = comp[chan].get_required_lifetime();
532
533 if (lt.begin >= 0) {
534 if ((result.begin < 0) || (result.begin > lt.begin))
535 result.begin = lt.begin;
536 }
537
538 if (lt.end > result.end)
539 result.end = lt.end;
540
541 if (!needs_component_tracking)
542 break;
543 }
544 return result;
545 }
546
547 temp_comp_access::temp_comp_access():
548 last_read_scope(nullptr),
549 first_read_scope(nullptr),
550 first_write_scope(nullptr),
551 first_write(-1),
552 last_read(-1),
553 last_write(-1),
554 first_read(numeric_limits<int>::max()),
555 conditionality_in_loop_id(conditionality_untouched),
556 if_scope_write_flags(0),
557 next_ifelse_nesting_depth(0),
558 current_unpaired_if_write_scope(nullptr),
559 was_written_in_current_else_scope(false)
560 {
561 }
562
563 void temp_comp_access::record_read(int line, prog_scope *scope)
564 {
565 last_read_scope = scope;
566 last_read = line;
567
568 if (first_read > line) {
569 first_read = line;
570 first_read_scope = scope;
571 }
572
573 /* If the conditionality of the first write is already resolved then
574 * no further checks are required.
575 */
576 if (conditionality_in_loop_id == write_is_unconditional ||
577 conditionality_in_loop_id == write_is_conditional)
578 return;
579
580 /* Check whether we are in a condition within a loop */
581 const prog_scope *ifelse_scope = scope->in_ifelse_scope();
582 const prog_scope *enclosing_loop;
583 if (ifelse_scope && (enclosing_loop = ifelse_scope->innermost_loop())) {
584
585 /* If we have either not yet written to this register nor writes are
586 * resolved as unconditional in the enclosing loop then check whether
587 * we read before write in an IF/ELSE branch.
588 */
589 if ((conditionality_in_loop_id != write_is_conditional) &&
590 (conditionality_in_loop_id != enclosing_loop->id())) {
591
592 if (current_unpaired_if_write_scope) {
593
594 /* Has been written in this or a parent scope? - this makes the temporary
595 * unconditionally set at this point.
596 */
597 if (scope->is_child_of(current_unpaired_if_write_scope))
598 return;
599
600 /* Has been written in the same scope before it was read? */
601 if (ifelse_scope->type() == if_branch) {
602 if (current_unpaired_if_write_scope->id() == scope->id())
603 return;
604 } else {
605 if (was_written_in_current_else_scope)
606 return;
607 }
608 }
609
610 /* The temporary was read (conditionally) before it is written, hence
611 * it should survive a loop. This can be signaled like if it were
612 * conditionally written.
613 */
614 conditionality_in_loop_id = write_is_conditional;
615 }
616 }
617 }
618
619 void temp_comp_access::record_write(int line, prog_scope *scope)
620 {
621 last_write = line;
622
623 if (first_write < 0) {
624 first_write = line;
625 first_write_scope = scope;
626
627 /* If the first write we encounter is not in a conditional branch, or
628 * the conditional write is not within a loop, then this is to be
629 * considered an unconditional dominant write.
630 */
631 const prog_scope *conditional = scope->enclosing_conditional();
632 if (!conditional || !conditional->innermost_loop()) {
633 conditionality_in_loop_id = write_is_unconditional;
634 }
635 }
636
637 /* The conditionality of the first write is already resolved. */
638 if (conditionality_in_loop_id == write_is_unconditional ||
639 conditionality_in_loop_id == write_is_conditional)
640 return;
641
642 /* If the nesting depth is larger than the supported level,
643 * then we assume conditional writes.
644 */
645 if (next_ifelse_nesting_depth >= supported_ifelse_nesting_depth) {
646 conditionality_in_loop_id = write_is_conditional;
647 return;
648 }
649
650 /* If we are in an IF/ELSE scope within a loop and the loop has not
651 * been resolved already, then record this write.
652 */
653 const prog_scope *ifelse_scope = scope->in_ifelse_scope();
654 if (ifelse_scope && ifelse_scope->innermost_loop() &&
655 ifelse_scope->innermost_loop()->id() != conditionality_in_loop_id)
656 record_ifelse_write(*ifelse_scope);
657 }
658
659 void temp_comp_access::record_ifelse_write(const prog_scope& scope)
660 {
661 if (scope.type() == if_branch) {
662 /* The first write in an IF branch within a loop implies unresolved
663 * conditionality (if it was untouched or unconditional before).
664 */
665 conditionality_in_loop_id = conditionality_unresolved;
666 was_written_in_current_else_scope = false;
667 record_if_write(scope);
668 } else {
669 was_written_in_current_else_scope = true;
670 record_else_write(scope);
671 }
672 }
673
674 void temp_comp_access::record_if_write(const prog_scope& scope)
675 {
676 /* Don't record write if this IF scope if it ...
677 * - is not the first write in this IF scope,
678 * - has already been written in a parent IF scope.
679 * In both cases this write is a secondary write that doesn't contribute
680 * to resolve conditionality.
681 *
682 * Record the write if it
683 * - is the first one (obviously),
684 * - happens in an IF branch that is a child of the ELSE branch of the
685 * last active IF/ELSE pair. In this case recording this write is used to
686 * established whether the write is (un-)conditional in the scope enclosing
687 * this outer IF/ELSE pair.
688 */
689 if (!current_unpaired_if_write_scope ||
690 (current_unpaired_if_write_scope->id() != scope.id() &&
691 scope.is_child_of_ifelse_id_sibling(current_unpaired_if_write_scope))) {
692 if_scope_write_flags |= 1 << next_ifelse_nesting_depth;
693 current_unpaired_if_write_scope = &scope;
694 next_ifelse_nesting_depth++;
695 }
696 }
697
698 void temp_comp_access::record_else_write(const prog_scope& scope)
699 {
700 int mask = 1 << (next_ifelse_nesting_depth - 1);
701
702 /* If the temporary was written in an IF branch on the same scope level
703 * and this branch is the sibling of this ELSE branch, then we have a
704 * pair of writes that makes write access to this temporary unconditional
705 * in the enclosing scope.
706 */
707
708 if ((if_scope_write_flags & mask) &&
709 (scope.id() == current_unpaired_if_write_scope->id())) {
710 --next_ifelse_nesting_depth;
711 if_scope_write_flags &= ~mask;
712
713 /* The following code deals with propagating unconditionality from
714 * inner levels of nested IF/ELSE to the outer levels like in
715 *
716 * 1: var t;
717 * 2: if (a) { <- start scope A
718 * 3: if (b)
719 * 4: t = ...
720 * 5: else
721 * 6: t = ...
722 * 7: } else { <- start scope B
723 * 8: if (c)
724 * 9: t = ...
725 * A: else <- start scope C
726 * B: t = ...
727 * C: }
728 *
729 */
730
731 const prog_scope *parent_ifelse = scope.parent()->in_ifelse_scope();
732
733 if (1 << (next_ifelse_nesting_depth - 1) & if_scope_write_flags) {
734 /* We are at the end of scope C and already recorded a write
735 * within an IF scope (A), the sibling of the parent ELSE scope B,
736 * and it is not yet resolved. Mark that as the last relevant
737 * IF scope. Below the write will be resolved for the A/B
738 * scope pair.
739 */
740 current_unpaired_if_write_scope = parent_ifelse;
741 } else {
742 current_unpaired_if_write_scope = nullptr;
743 }
744
745 /* If some parent is IF/ELSE and in a loop then propagate the
746 * write to that scope. Otherwise the write is unconditional
747 * because it happens in both corresponding IF/ELSE branches
748 * in this loop, and hence, record the loop id to signal the
749 * resolution.
750 */
751 if (parent_ifelse && parent_ifelse->is_in_loop()) {
752 record_ifelse_write(*parent_ifelse);
753 } else {
754 conditionality_in_loop_id = scope.innermost_loop()->id();
755 }
756 } else {
757 /* The temporary was not written in the IF branch corresponding
758 * to this ELSE branch, hence the write is conditional.
759 */
760 conditionality_in_loop_id = write_is_conditional;
761 }
762 }
763
764 bool temp_comp_access::conditional_ifelse_write_in_loop() const
765 {
766 return conditionality_in_loop_id <= conditionality_unresolved;
767 }
768
769 void temp_comp_access::propagate_lifetime_to_dominant_write_scope()
770 {
771 first_write = first_write_scope->begin();
772 int lr = first_write_scope->end();
773
774 if (last_read < lr)
775 last_read = lr;
776 }
777
778 lifetime temp_comp_access::get_required_lifetime()
779 {
780 bool keep_for_full_loop = false;
781
782 /* This register component is not used at all, or only read,
783 * mark it as unused and ignore it when renaming.
784 * glsl_to_tgsi_visitor::renumber_registers will take care of
785 * eliminating registers that are not written to.
786 */
787 if (last_write < 0)
788 return make_lifetime(-1, -1);
789
790 assert(first_write_scope);
791
792 /* Only written to, just make sure the register component is not
793 * reused in the range it is used to write to
794 */
795 if (!last_read_scope)
796 return make_lifetime(first_write, last_write + 1);
797
798 const prog_scope *enclosing_scope_first_read = first_read_scope;
799 const prog_scope *enclosing_scope_first_write = first_write_scope;
800
801 /* We read before writing in a loop
802 * hence the value must survive the loops
803 */
804 if ((first_read <= first_write) &&
805 first_read_scope->is_in_loop()) {
806 keep_for_full_loop = true;
807 enclosing_scope_first_read = first_read_scope->outermost_loop();
808 }
809
810 /* A conditional write within a (nested) loop must survive the outermost
811 * loop if the last read was not within the same scope.
812 */
813 const prog_scope *conditional = enclosing_scope_first_write->enclosing_conditional();
814 if (conditional && !conditional->contains_range_of(*last_read_scope) &&
815 (conditional->is_switchcase_scope_in_loop() ||
816 conditional_ifelse_write_in_loop())) {
817 keep_for_full_loop = true;
818 enclosing_scope_first_write = conditional->outermost_loop();
819 }
820
821 /* Evaluate the scope that is shared by all: required first write scope,
822 * required first read before write scope, and last read scope.
823 */
824 const prog_scope *enclosing_scope = enclosing_scope_first_read;
825 if (enclosing_scope_first_write->contains_range_of(*enclosing_scope))
826 enclosing_scope = enclosing_scope_first_write;
827
828 if (last_read_scope->contains_range_of(*enclosing_scope))
829 enclosing_scope = last_read_scope;
830
831 while (!enclosing_scope->contains_range_of(*enclosing_scope_first_write) ||
832 !enclosing_scope->contains_range_of(*last_read_scope)) {
833 enclosing_scope = enclosing_scope->parent();
834 assert(enclosing_scope);
835 }
836
837 /* Propagate the last read scope to the target scope */
838 while (enclosing_scope->nesting_depth() < last_read_scope->nesting_depth()) {
839 /* If the read is in a loop and we have to move up the scope we need to
840 * extend the life time to the end of this current loop because at this
841 * point we don't know whether the component was written before
842 * un-conditionally in the same loop.
843 */
844 if (last_read_scope->is_loop())
845 last_read = last_read_scope->end();
846
847 last_read_scope = last_read_scope->parent();
848 }
849
850 /* If the variable has to be kept for the whole loop, and we
851 * are currently in a loop, then propagate the life time.
852 */
853 if (keep_for_full_loop && first_write_scope->is_loop())
854 propagate_lifetime_to_dominant_write_scope();
855
856 /* Propagate the first_dominant_write scope to the target scope */
857 while (enclosing_scope->nesting_depth() < first_write_scope->nesting_depth()) {
858 /* Propagate lifetime if there was a break in a loop and the write was
859 * after the break inside that loop. Note, that this is only needed if
860 * we move up in the scopes.
861 */
862 if (first_write_scope->loop_break_line() < first_write) {
863 keep_for_full_loop = true;
864 propagate_lifetime_to_dominant_write_scope();
865 }
866
867 first_write_scope = first_write_scope->parent();
868
869 /* Propagte lifetime if we are now in a loop */
870 if (keep_for_full_loop && first_write_scope->is_loop())
871 propagate_lifetime_to_dominant_write_scope();
872 }
873
874 /* The last write past the last read is dead code, but we have to
875 * ensure that the component is not reused too early, hence extend the
876 * lifetime past the last write.
877 */
878 if (last_write >= last_read)
879 last_read = last_write + 1;
880
881 /* Here we are at the same scope, all is resolved */
882 return make_lifetime(first_write, last_read);
883 }
884
885 /* Helper class for sorting and searching the registers based
886 * on life times. */
887 class access_record {
888 public:
889 int begin;
890 int end;
891 int reg;
892 bool erase;
893
894 bool operator < (const access_record& rhs) const {
895 return begin < rhs.begin;
896 }
897 };
898
899 class access_recorder {
900 public:
901 access_recorder(int _ntemps);
902 ~access_recorder();
903
904 void record_read(const st_src_reg& src, int line, prog_scope *scope);
905 void record_write(const st_dst_reg& src, int line, prog_scope *scope);
906
907 void get_required_lifetimes(struct lifetime *lifetimes);
908 private:
909
910 int ntemps;
911 temp_access *acc;
912
913 };
914
915 access_recorder::access_recorder(int _ntemps):
916 ntemps(_ntemps)
917 {
918 acc = new temp_access[ntemps];
919 }
920
921 access_recorder::~access_recorder()
922 {
923 delete[] acc;
924 }
925
926 void access_recorder::record_read(const st_src_reg& src, int line,
927 prog_scope *scope)
928 {
929 if (src.file == PROGRAM_TEMPORARY)
930 acc[src.index].record_read(line, scope, src.swizzle);
931
932 if (src.reladdr)
933 record_read(*src.reladdr, line, scope);
934 if (src.reladdr2)
935 record_read(*src.reladdr2, line, scope);
936 }
937
938 void access_recorder::record_write(const st_dst_reg& dst, int line,
939 prog_scope *scope)
940 {
941 if (dst.file == PROGRAM_TEMPORARY)
942 acc[dst.index].record_write(line, scope, dst.writemask);
943
944 if (dst.reladdr)
945 record_read(*dst.reladdr, line, scope);
946 if (dst.reladdr2)
947 record_read(*dst.reladdr2, line, scope);
948 }
949
950 void access_recorder::get_required_lifetimes(struct lifetime *lifetimes)
951 {
952 RENAME_DEBUG(debug_log << "========= lifetimes ==============\n");
953 for(int i = 0; i < ntemps; ++i) {
954 RENAME_DEBUG(debug_log<< setw(4) << i);
955 lifetimes[i] = acc[i].get_required_lifetime();
956 RENAME_DEBUG(debug_log << ": [" << lifetimes[i].begin << ", "
957 << lifetimes[i].end << "]\n");
958 }
959 RENAME_DEBUG(debug_log << "==================================\n\n");
960 }
961
962 }
963
964 #ifndef NDEBUG
965 /* Function used for debugging. */
966 static void dump_instruction(ostream& os, int line, prog_scope *scope,
967 const glsl_to_tgsi_instruction& inst);
968 #endif
969
970 /* Scan the program and estimate the required register life times.
971 * The array lifetimes must be pre-allocated
972 */
973 bool
974 get_temp_registers_required_lifetimes(void *mem_ctx, exec_list *instructions,
975 int ntemps, struct lifetime *lifetimes)
976 {
977 int line = 0;
978 int loop_id = 1;
979 int if_id = 1;
980 int switch_id = 0;
981 bool is_at_end = false;
982 int n_scopes = 1;
983
984 /* Count scopes to allocate the needed space without the need for
985 * re-allocation
986 */
987 foreach_in_list(glsl_to_tgsi_instruction, inst, instructions) {
988 if (inst->op == TGSI_OPCODE_BGNLOOP ||
989 inst->op == TGSI_OPCODE_SWITCH ||
990 inst->op == TGSI_OPCODE_CASE ||
991 inst->op == TGSI_OPCODE_IF ||
992 inst->op == TGSI_OPCODE_UIF ||
993 inst->op == TGSI_OPCODE_ELSE ||
994 inst->op == TGSI_OPCODE_DEFAULT)
995 ++n_scopes;
996 }
997
998 prog_scope_storage scopes(mem_ctx, n_scopes);
999
1000 access_recorder access(ntemps);
1001
1002 prog_scope *cur_scope = scopes.create(nullptr, outer_scope, 0, 0, line);
1003
1004 RENAME_DEBUG(debug_log << "========= Begin shader ============\n");
1005
1006 foreach_in_list(glsl_to_tgsi_instruction, inst, instructions) {
1007 if (is_at_end) {
1008 assert(!"GLSL_TO_TGSI: shader has instructions past end marker");
1009 break;
1010 }
1011
1012 RENAME_DEBUG(dump_instruction(debug_log, line, cur_scope, *inst));
1013
1014 switch (inst->op) {
1015 case TGSI_OPCODE_BGNLOOP: {
1016 cur_scope = scopes.create(cur_scope, loop_body, loop_id++,
1017 cur_scope->nesting_depth() + 1, line);
1018 break;
1019 }
1020 case TGSI_OPCODE_ENDLOOP: {
1021 cur_scope->set_end(line);
1022 cur_scope = cur_scope->parent();
1023 assert(cur_scope);
1024 break;
1025 }
1026 case TGSI_OPCODE_IF:
1027 case TGSI_OPCODE_UIF: {
1028 assert(num_inst_src_regs(inst) == 1);
1029 access.record_read(inst->src[0], line, cur_scope);
1030 cur_scope = scopes.create(cur_scope, if_branch, if_id++,
1031 cur_scope->nesting_depth() + 1, line + 1);
1032 break;
1033 }
1034 case TGSI_OPCODE_ELSE: {
1035 assert(cur_scope->type() == if_branch);
1036 cur_scope->set_end(line - 1);
1037 cur_scope = scopes.create(cur_scope->parent(), else_branch,
1038 cur_scope->id(), cur_scope->nesting_depth(),
1039 line + 1);
1040 break;
1041 }
1042 case TGSI_OPCODE_END: {
1043 cur_scope->set_end(line);
1044 is_at_end = true;
1045 break;
1046 }
1047 case TGSI_OPCODE_ENDIF: {
1048 cur_scope->set_end(line - 1);
1049 cur_scope = cur_scope->parent();
1050 assert(cur_scope);
1051 break;
1052 }
1053 case TGSI_OPCODE_SWITCH: {
1054 assert(num_inst_src_regs(inst) == 1);
1055 prog_scope *scope = scopes.create(cur_scope, switch_body, switch_id++,
1056 cur_scope->nesting_depth() + 1, line);
1057 /* We record the read only for the SWITCH statement itself, like it
1058 * is used by the only consumer of TGSI_OPCODE_SWITCH in tgsi_exec.c.
1059 */
1060 access.record_read(inst->src[0], line, cur_scope);
1061 cur_scope = scope;
1062 break;
1063 }
1064 case TGSI_OPCODE_ENDSWITCH: {
1065 cur_scope->set_end(line - 1);
1066 /* Remove the case level, it might not have been
1067 * closed with a break.
1068 */
1069 if (cur_scope->type() != switch_body)
1070 cur_scope = cur_scope->parent();
1071
1072 cur_scope = cur_scope->parent();
1073 assert(cur_scope);
1074 break;
1075 }
1076 case TGSI_OPCODE_CASE: {
1077 /* Take care of tracking the registers. */
1078 prog_scope *switch_scope = cur_scope->type() == switch_body ?
1079 cur_scope : cur_scope->parent();
1080
1081 assert(num_inst_src_regs(inst) == 1);
1082 access.record_read(inst->src[0], line, switch_scope);
1083
1084 /* Fall through to allocate the scope. */
1085 }
1086 case TGSI_OPCODE_DEFAULT: {
1087 prog_scope_type t = inst->op == TGSI_OPCODE_CASE ? switch_case_branch
1088 : switch_default_branch;
1089 prog_scope *switch_scope = (cur_scope->type() == switch_body) ?
1090 cur_scope : cur_scope->parent();
1091 assert(switch_scope->type() == switch_body);
1092 prog_scope *scope = scopes.create(switch_scope, t,
1093 switch_scope->id(),
1094 switch_scope->nesting_depth() + 1,
1095 line);
1096 /* Previous case falls through, so scope was not yet closed. */
1097 if ((cur_scope != switch_scope) && (cur_scope->end() == -1))
1098 cur_scope->set_end(line - 1);
1099 cur_scope = scope;
1100 break;
1101 }
1102 case TGSI_OPCODE_BRK: {
1103 if (cur_scope->break_is_for_switchcase()) {
1104 cur_scope->set_end(line - 1);
1105 } else {
1106 cur_scope->set_loop_break_line(line);
1107 }
1108 break;
1109 }
1110 case TGSI_OPCODE_CAL:
1111 case TGSI_OPCODE_RET:
1112 /* These opcodes are not supported and if a subroutine would
1113 * be called in a shader, then the lifetime tracking would have
1114 * to follow that call to see which registers are used there.
1115 * Since this is not done, we have to bail out here and signal
1116 * that no register merge will take place.
1117 */
1118 return false;
1119 default: {
1120 for (unsigned j = 0; j < num_inst_src_regs(inst); j++) {
1121 access.record_read(inst->src[j], line, cur_scope);
1122 }
1123 for (unsigned j = 0; j < inst->tex_offset_num_offset; j++) {
1124 access.record_read(inst->tex_offsets[j], line, cur_scope);
1125 }
1126 for (unsigned j = 0; j < num_inst_dst_regs(inst); j++) {
1127 access.record_write(inst->dst[j], line, cur_scope);
1128 }
1129 }
1130 }
1131 ++line;
1132 }
1133
1134 RENAME_DEBUG(debug_log << "==================================\n\n");
1135
1136 /* Make sure last scope is closed, even though no
1137 * TGSI_OPCODE_END was given.
1138 */
1139 if (cur_scope->end() < 0)
1140 cur_scope->set_end(line - 1);
1141
1142 access.get_required_lifetimes(lifetimes);
1143 return true;
1144 }
1145
1146 /* Find the next register between [start, end) that has a life time starting
1147 * at or after bound by using a binary search.
1148 * start points at the beginning of the search range,
1149 * end points at the element past the end of the search range, and
1150 * the array comprising [start, end) must be sorted in ascending order.
1151 */
1152 static access_record*
1153 find_next_rename(access_record* start, access_record* end, int bound)
1154 {
1155 int delta = (end - start);
1156
1157 while (delta > 0) {
1158 int half = delta >> 1;
1159 access_record* middle = start + half;
1160
1161 if (bound <= middle->begin) {
1162 delta = half;
1163 } else {
1164 start = middle;
1165 ++start;
1166 delta -= half + 1;
1167 }
1168 }
1169
1170 return start;
1171 }
1172
1173 #ifndef USE_STL_SORT
1174 static int access_record_compare (const void *a, const void *b) {
1175 const access_record *aa = static_cast<const access_record*>(a);
1176 const access_record *bb = static_cast<const access_record*>(b);
1177 return aa->begin < bb->begin ? -1 : (aa->begin > bb->begin ? 1 : 0);
1178 }
1179 #endif
1180
1181 /* This functions evaluates the register merges by using a binary
1182 * search to find suitable merge candidates. */
1183 void get_temp_registers_remapping(void *mem_ctx, int ntemps,
1184 const struct lifetime* lifetimes,
1185 struct rename_reg_pair *result)
1186 {
1187 access_record *reg_access = ralloc_array(mem_ctx, access_record, ntemps);
1188
1189 int used_temps = 0;
1190 for (int i = 0; i < ntemps; ++i) {
1191 if (lifetimes[i].begin >= 0) {
1192 reg_access[used_temps].begin = lifetimes[i].begin;
1193 reg_access[used_temps].end = lifetimes[i].end;
1194 reg_access[used_temps].reg = i;
1195 reg_access[used_temps].erase = false;
1196 ++used_temps;
1197 }
1198 }
1199
1200 #ifdef USE_STL_SORT
1201 std::sort(reg_access, reg_access + used_temps);
1202 #else
1203 std::qsort(reg_access, used_temps, sizeof(access_record), access_record_compare);
1204 #endif
1205
1206 access_record *trgt = reg_access;
1207 access_record *reg_access_end = reg_access + used_temps;
1208 access_record *first_erase = reg_access_end;
1209 access_record *search_start = trgt + 1;
1210
1211 while (trgt != reg_access_end) {
1212 access_record *src = find_next_rename(search_start, reg_access_end,
1213 trgt->end);
1214 if (src != reg_access_end) {
1215 result[src->reg].new_reg = trgt->reg;
1216 result[src->reg].valid = true;
1217 trgt->end = src->end;
1218
1219 /* Since we only search forward, don't remove the renamed
1220 * register just now, only mark it. */
1221 src->erase = true;
1222
1223 if (first_erase == reg_access_end)
1224 first_erase = src;
1225
1226 search_start = src + 1;
1227 } else {
1228 /* Moving to the next target register it is time to remove
1229 * the already merged registers from the search range */
1230 if (first_erase != reg_access_end) {
1231 access_record *outp = first_erase;
1232 access_record *inp = first_erase + 1;
1233
1234 while (inp != reg_access_end) {
1235 if (!inp->erase)
1236 *outp++ = *inp;
1237 ++inp;
1238 }
1239
1240 reg_access_end = outp;
1241 first_erase = reg_access_end;
1242 }
1243 ++trgt;
1244 search_start = trgt + 1;
1245 }
1246 }
1247 ralloc_free(reg_access);
1248 }
1249
1250 /* Code below used for debugging */
1251 #ifndef NDEBUG
1252 static
1253 void dump_instruction(ostream& os, int line, prog_scope *scope,
1254 const glsl_to_tgsi_instruction& inst)
1255 {
1256 const struct tgsi_opcode_info *info = inst.info;
1257 int indent = scope->nesting_depth();
1258 if ((scope->type() == switch_case_branch ||
1259 scope->type() == switch_default_branch) &&
1260 (info->opcode == TGSI_OPCODE_CASE ||
1261 info->opcode == TGSI_OPCODE_DEFAULT))
1262 --indent;
1263
1264 if (info->opcode == TGSI_OPCODE_ENDIF ||
1265 info->opcode == TGSI_OPCODE_ELSE ||
1266 info->opcode == TGSI_OPCODE_ENDLOOP ||
1267 info->opcode == TGSI_OPCODE_ENDSWITCH)
1268 --indent;
1269
1270 os << setw(4) << line << ": ";
1271 os << setw(indent * 4) << " ";
1272 os << inst << "\n";
1273 }
1274 #endif