2 * Copyright © 2017 Gert Wollny
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:
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
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.
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"
32 /* std::sort is significantly faster than qsort */
41 #include "program/prog_print.h"
42 #include "util/debug.h"
48 /* If <windows.h> is included this is defined and clashes with
49 * std::numeric_limits<>::max()
55 using std::numeric_limits
;
57 /* Without c++11 define the nullptr for forward-compatibility
58 * and better readibility */
59 #if __cplusplus < 201103L
64 /* Prepare to make it possible to specify log file */
65 static std::ostream
& debug_log
= cerr
;
67 /* Helper function to check whether we want to seen debugging output */
68 static inline bool is_debug_enabled ()
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;
75 #define RENAME_DEBUG(X) if (is_debug_enabled()) do { X; } while (false);
77 #define RENAME_DEBUG(X)
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 */
95 prog_scope(prog_scope
*parent
, prog_scope_type type
, int id
,
96 int depth
, int begin
);
98 prog_scope_type
type() const;
99 prog_scope
*parent() const;
100 int nesting_depth() const;
104 int loop_break_line() const;
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;
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;
120 bool break_is_for_switchcase() const;
121 bool contains_range_of(const prog_scope
& other
) const;
123 void set_end(int end
);
124 void set_loop_break_line(int line
);
127 prog_scope_type scope_type
;
129 int scope_nesting_depth
;
133 prog_scope
*parent_scope
;
136 /* Some storage class to encapsulate the prog_scope (de-)allocations */
137 class prog_scope_storage
{
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
);
149 /* Class to track the access to a component of a temporary register. */
151 class temp_comp_access
{
155 void record_read(int line
, prog_scope
*scope
);
156 void record_write(int line
, prog_scope
*scope
);
157 lifetime
get_required_lifetime();
159 void propagate_lifetime_to_dominant_write_scope();
160 bool conditional_ifelse_write_in_loop() const;
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
);
166 prog_scope
*last_read_scope
;
167 prog_scope
*first_read_scope
;
168 prog_scope
*first_write_scope
;
175 /* This member variable tracks the current resolution of conditional writing
176 * to this temporary in IF/ELSE clauses.
178 * The initial value "conditionality_untouched" indicates that this
179 * temporary has not yet been written to within an if clause.
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.
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.
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
194 int conditionality_in_loop_id
;
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
;
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
206 unsigned int if_scope_write_flags
;
208 int next_ifelse_nesting_depth
;
209 static const int supported_ifelse_nesting_depth
= 32;
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.
215 const prog_scope
*current_unpaired_if_write_scope
;
217 /* Flag to resolve read-before-write in the else scope. */
218 bool was_written_in_current_else_scope
;
222 temp_comp_access::conditionality_untouched
= numeric_limits
<int>::max();
225 temp_comp_access::write_is_unconditional
= numeric_limits
<int>::max() - 1;
227 /* Class to track the access to all components of a temporary register. */
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();
235 void update_access_mask(int mask
);
237 temp_comp_access comp
[4];
239 bool needs_component_tracking
;
242 prog_scope_storage::prog_scope_storage(void *mc
, int n
):
246 storage
= ralloc_array(mem_ctx
, prog_scope
, n
);
249 prog_scope_storage::~prog_scope_storage()
251 ralloc_free(storage
);
255 prog_scope_storage::create(prog_scope
*p
, prog_scope_type type
, int id
,
256 int lvl
, int s_begin
)
258 storage
[current_slot
] = prog_scope(p
, type
, id
, lvl
, s_begin
);
259 return &storage
[current_slot
++];
262 prog_scope::prog_scope(prog_scope
*parent
, prog_scope_type type
, int id
,
263 int depth
, int scope_begin
):
266 scope_nesting_depth(depth
),
267 scope_begin(scope_begin
),
269 break_loop_line(numeric_limits
<int>::max()),
274 prog_scope_type
prog_scope::type() const
279 prog_scope
*prog_scope::parent() const
284 int prog_scope::nesting_depth() const
286 return scope_nesting_depth
;
289 bool prog_scope::is_loop() const
291 return (scope_type
== loop_body
);
294 bool prog_scope::is_in_loop() const
296 if (scope_type
== loop_body
)
300 return parent_scope
->is_in_loop();
305 const prog_scope
*prog_scope::innermost_loop() const
307 if (scope_type
== loop_body
)
311 return parent_scope
->innermost_loop();
316 const prog_scope
*prog_scope::outermost_loop() const
318 const prog_scope
*loop
= nullptr;
319 const prog_scope
*p
= this;
322 if (p
->type() == loop_body
)
330 bool prog_scope::is_child_of_ifelse_id_sibling(const prog_scope
*scope
) const
332 const prog_scope
*my_parent
= in_parent_ifelse_scope();
334 /* is a direct child? */
335 if (my_parent
== scope
)
337 /* is a child of the conditions sibling? */
338 if (my_parent
->id() == scope
->id())
340 my_parent
= my_parent
->in_parent_ifelse_scope();
345 bool prog_scope::is_child_of(const prog_scope
*scope
) const
347 const prog_scope
*my_parent
= parent();
349 if (my_parent
== scope
)
351 my_parent
= my_parent
->parent();
356 const prog_scope
*prog_scope::enclosing_conditional() const
358 if (is_conditional())
362 return parent_scope
->enclosing_conditional();
367 bool prog_scope::contains_range_of(const prog_scope
& other
) const
369 return (begin() <= other
.begin()) && (end() >= other
.end());
372 bool prog_scope::is_conditional() const
374 return scope_type
== if_branch
||
375 scope_type
== else_branch
||
376 scope_type
== switch_case_branch
||
377 scope_type
== switch_default_branch
;
380 const prog_scope
*prog_scope::in_else_scope() const
382 if (scope_type
== else_branch
)
386 return parent_scope
->in_else_scope();
391 const prog_scope
*prog_scope::in_parent_ifelse_scope() const
394 return parent_scope
->in_ifelse_scope();
399 const prog_scope
*prog_scope::in_ifelse_scope() const
401 if (scope_type
== if_branch
||
402 scope_type
== else_branch
)
406 return parent_scope
->in_ifelse_scope();
411 bool prog_scope::is_switchcase_scope_in_loop() const
413 return (scope_type
== switch_case_branch
||
414 scope_type
== switch_default_branch
) &&
418 bool prog_scope::break_is_for_switchcase() const
420 if (scope_type
== loop_body
)
423 if (scope_type
== switch_case_branch
||
424 scope_type
== switch_default_branch
||
425 scope_type
== switch_body
)
429 return parent_scope
->break_is_for_switchcase();
434 int prog_scope::id() const
439 int prog_scope::begin() const
444 int prog_scope::end() const
449 void prog_scope::set_end(int end
)
455 void prog_scope::set_loop_break_line(int line
)
457 if (scope_type
== loop_body
) {
458 break_loop_line
= MIN2(break_loop_line
, line
);
461 parent()->set_loop_break_line(line
);
465 int prog_scope::loop_break_line() const
467 return break_loop_line
;
470 temp_access::temp_access():
472 needs_component_tracking(false)
476 void temp_access::update_access_mask(int mask
)
478 if (access_mask
&& access_mask
!= mask
)
479 needs_component_tracking
= true;
483 void temp_access::record_write(int line
, prog_scope
*scope
, int writemask
)
485 update_access_mask(writemask
);
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
);
497 void temp_access::record_read(int line
, prog_scope
*scope
, int swizzle
)
500 for (int idx
= 0; idx
< 4; ++idx
) {
501 int swz
= GET_SWZ(swizzle
, idx
);
502 readmask
|= (1 << swz
) & 0xF;
504 update_access_mask(readmask
);
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
);
516 inline static lifetime
make_lifetime(int b
, int e
)
524 lifetime
temp_access::get_required_lifetime()
526 lifetime result
= make_lifetime(-1, -1);
528 unsigned mask
= access_mask
;
530 unsigned chan
= u_bit_scan(&mask
);
531 lifetime lt
= comp
[chan
].get_required_lifetime();
534 if ((result
.begin
< 0) || (result
.begin
> lt
.begin
))
535 result
.begin
= lt
.begin
;
538 if (lt
.end
> result
.end
)
541 if (!needs_component_tracking
)
547 temp_comp_access::temp_comp_access():
548 last_read_scope(nullptr),
549 first_read_scope(nullptr),
550 first_write_scope(nullptr),
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)
563 void temp_comp_access::record_read(int line
, prog_scope
*scope
)
565 last_read_scope
= scope
;
568 if (first_read
> line
) {
570 first_read_scope
= scope
;
573 /* If the conditionality of the first write is already resolved then
574 * no further checks are required.
576 if (conditionality_in_loop_id
== write_is_unconditional
||
577 conditionality_in_loop_id
== write_is_conditional
)
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())) {
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.
589 if ((conditionality_in_loop_id
!= write_is_conditional
) &&
590 (conditionality_in_loop_id
!= enclosing_loop
->id())) {
592 if (current_unpaired_if_write_scope
) {
594 /* Has been written in this or a parent scope? - this makes the temporary
595 * unconditionally set at this point.
597 if (scope
->is_child_of(current_unpaired_if_write_scope
))
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())
605 if (was_written_in_current_else_scope
)
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.
614 conditionality_in_loop_id
= write_is_conditional
;
619 void temp_comp_access::record_write(int line
, prog_scope
*scope
)
623 if (first_write
< 0) {
625 first_write_scope
= scope
;
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.
631 const prog_scope
*conditional
= scope
->enclosing_conditional();
632 if (!conditional
|| !conditional
->innermost_loop()) {
633 conditionality_in_loop_id
= write_is_unconditional
;
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
)
642 /* If the nesting depth is larger than the supported level,
643 * then we assume conditional writes.
645 if (next_ifelse_nesting_depth
>= supported_ifelse_nesting_depth
) {
646 conditionality_in_loop_id
= write_is_conditional
;
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.
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
);
659 void temp_comp_access::record_ifelse_write(const prog_scope
& scope
)
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).
665 conditionality_in_loop_id
= conditionality_unresolved
;
666 was_written_in_current_else_scope
= false;
667 record_if_write(scope
);
669 was_written_in_current_else_scope
= true;
670 record_else_write(scope
);
674 void temp_comp_access::record_if_write(const prog_scope
& scope
)
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.
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.
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
++;
698 void temp_comp_access::record_else_write(const prog_scope
& scope
)
700 int mask
= 1 << (next_ifelse_nesting_depth
- 1);
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.
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
;
713 /* The following code deals with propagating unconditionality from
714 * inner levels of nested IF/ELSE to the outer levels like in
717 * 2: if (a) { <- start scope A
722 * 7: } else { <- start scope B
725 * A: else <- start scope C
731 const prog_scope
*parent_ifelse
= scope
.parent()->in_ifelse_scope();
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
740 current_unpaired_if_write_scope
= parent_ifelse
;
742 current_unpaired_if_write_scope
= nullptr;
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
751 if (parent_ifelse
&& parent_ifelse
->is_in_loop()) {
752 record_ifelse_write(*parent_ifelse
);
754 conditionality_in_loop_id
= scope
.innermost_loop()->id();
757 /* The temporary was not written in the IF branch corresponding
758 * to this ELSE branch, hence the write is conditional.
760 conditionality_in_loop_id
= write_is_conditional
;
764 bool temp_comp_access::conditional_ifelse_write_in_loop() const
766 return conditionality_in_loop_id
<= conditionality_unresolved
;
769 void temp_comp_access::propagate_lifetime_to_dominant_write_scope()
771 first_write
= first_write_scope
->begin();
772 int lr
= first_write_scope
->end();
778 lifetime
temp_comp_access::get_required_lifetime()
780 bool keep_for_full_loop
= false;
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.
788 return make_lifetime(-1, -1);
790 assert(first_write_scope
);
792 /* Only written to, just make sure the register component is not
793 * reused in the range it is used to write to
795 if (!last_read_scope
)
796 return make_lifetime(first_write
, last_write
+ 1);
798 const prog_scope
*enclosing_scope_first_read
= first_read_scope
;
799 const prog_scope
*enclosing_scope_first_write
= first_write_scope
;
801 /* We read before writing in a loop
802 * hence the value must survive the loops
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();
810 /* A conditional write within a (nested) loop must survive the outermost
811 * loop if the last read was not within the same scope.
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();
821 /* Evaluate the scope that is shared by all: required first write scope,
822 * required first read before write scope, and last read scope.
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
;
828 if (last_read_scope
->contains_range_of(*enclosing_scope
))
829 enclosing_scope
= last_read_scope
;
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
);
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.
844 if (last_read_scope
->is_loop())
845 last_read
= last_read_scope
->end();
847 last_read_scope
= last_read_scope
->parent();
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.
853 if (keep_for_full_loop
&& first_write_scope
->is_loop())
854 propagate_lifetime_to_dominant_write_scope();
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.
862 if (first_write_scope
->loop_break_line() < first_write
) {
863 keep_for_full_loop
= true;
864 propagate_lifetime_to_dominant_write_scope();
867 first_write_scope
= first_write_scope
->parent();
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();
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.
878 if (last_write
>= last_read
)
879 last_read
= last_write
+ 1;
881 /* Here we are at the same scope, all is resolved */
882 return make_lifetime(first_write
, last_read
);
885 /* Helper class for sorting and searching the registers based
887 class access_record
{
894 bool operator < (const access_record
& rhs
) const {
895 return begin
< rhs
.begin
;
899 class access_recorder
{
901 access_recorder(int _ntemps
);
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
);
907 void get_required_lifetimes(struct lifetime
*lifetimes
);
915 access_recorder::access_recorder(int _ntemps
):
918 acc
= new temp_access
[ntemps
];
921 access_recorder::~access_recorder()
926 void access_recorder::record_read(const st_src_reg
& src
, int line
,
929 if (src
.file
== PROGRAM_TEMPORARY
)
930 acc
[src
.index
].record_read(line
, scope
, src
.swizzle
);
933 record_read(*src
.reladdr
, line
, scope
);
935 record_read(*src
.reladdr2
, line
, scope
);
938 void access_recorder::record_write(const st_dst_reg
& dst
, int line
,
941 if (dst
.file
== PROGRAM_TEMPORARY
)
942 acc
[dst
.index
].record_write(line
, scope
, dst
.writemask
);
945 record_read(*dst
.reladdr
, line
, scope
);
947 record_read(*dst
.reladdr2
, line
, scope
);
950 void access_recorder::get_required_lifetimes(struct lifetime
*lifetimes
)
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");
959 RENAME_DEBUG(debug_log
<< "==================================\n\n");
965 /* Function used for debugging. */
966 static void dump_instruction(ostream
& os
, int line
, prog_scope
*scope
,
967 const glsl_to_tgsi_instruction
& inst
);
970 /* Scan the program and estimate the required register life times.
971 * The array lifetimes must be pre-allocated
974 get_temp_registers_required_lifetimes(void *mem_ctx
, exec_list
*instructions
,
975 int ntemps
, struct lifetime
*lifetimes
)
981 bool is_at_end
= false;
984 /* Count scopes to allocate the needed space without the need for
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
)
998 prog_scope_storage
scopes(mem_ctx
, n_scopes
);
1000 access_recorder
access(ntemps
);
1002 prog_scope
*cur_scope
= scopes
.create(nullptr, outer_scope
, 0, 0, line
);
1004 RENAME_DEBUG(debug_log
<< "========= Begin shader ============\n");
1006 foreach_in_list(glsl_to_tgsi_instruction
, inst
, instructions
) {
1008 assert(!"GLSL_TO_TGSI: shader has instructions past end marker");
1012 RENAME_DEBUG(dump_instruction(debug_log
, line
, cur_scope
, *inst
));
1015 case TGSI_OPCODE_BGNLOOP
: {
1016 cur_scope
= scopes
.create(cur_scope
, loop_body
, loop_id
++,
1017 cur_scope
->nesting_depth() + 1, line
);
1020 case TGSI_OPCODE_ENDLOOP
: {
1021 cur_scope
->set_end(line
);
1022 cur_scope
= cur_scope
->parent();
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);
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(),
1042 case TGSI_OPCODE_END
: {
1043 cur_scope
->set_end(line
);
1047 case TGSI_OPCODE_ENDIF
: {
1048 cur_scope
->set_end(line
- 1);
1049 cur_scope
= cur_scope
->parent();
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.
1060 access
.record_read(inst
->src
[0], line
, cur_scope
);
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.
1069 if (cur_scope
->type() != switch_body
)
1070 cur_scope
= cur_scope
->parent();
1072 cur_scope
= cur_scope
->parent();
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();
1081 assert(num_inst_src_regs(inst
) == 1);
1082 access
.record_read(inst
->src
[0], line
, switch_scope
);
1084 /* Fall through to allocate the scope. */
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
,
1094 switch_scope
->nesting_depth() + 1,
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);
1102 case TGSI_OPCODE_BRK
: {
1103 if (cur_scope
->break_is_for_switchcase()) {
1104 cur_scope
->set_end(line
- 1);
1106 cur_scope
->set_loop_break_line(line
);
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.
1120 for (unsigned j
= 0; j
< num_inst_src_regs(inst
); j
++) {
1121 access
.record_read(inst
->src
[j
], line
, cur_scope
);
1123 for (unsigned j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
1124 access
.record_read(inst
->tex_offsets
[j
], line
, cur_scope
);
1126 for (unsigned j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
1127 access
.record_write(inst
->dst
[j
], line
, cur_scope
);
1134 RENAME_DEBUG(debug_log
<< "==================================\n\n");
1136 /* Make sure last scope is closed, even though no
1137 * TGSI_OPCODE_END was given.
1139 if (cur_scope
->end() < 0)
1140 cur_scope
->set_end(line
- 1);
1142 access
.get_required_lifetimes(lifetimes
);
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.
1152 static access_record
*
1153 find_next_rename(access_record
* start
, access_record
* end
, int bound
)
1155 int delta
= (end
- start
);
1158 int half
= delta
>> 1;
1159 access_record
* middle
= start
+ half
;
1161 if (bound
<= middle
->begin
) {
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);
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
)
1187 access_record
*reg_access
= ralloc_array(mem_ctx
, access_record
, ntemps
);
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;
1201 std::sort(reg_access
, reg_access
+ used_temps
);
1203 std::qsort(reg_access
, used_temps
, sizeof(access_record
), access_record_compare
);
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;
1211 while (trgt
!= reg_access_end
) {
1212 access_record
*src
= find_next_rename(search_start
, reg_access_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
;
1219 /* Since we only search forward, don't remove the renamed
1220 * register just now, only mark it. */
1223 if (first_erase
== reg_access_end
)
1226 search_start
= src
+ 1;
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;
1234 while (inp
!= reg_access_end
) {
1240 reg_access_end
= outp
;
1241 first_erase
= reg_access_end
;
1244 search_start
= trgt
+ 1;
1247 ralloc_free(reg_access
);
1250 /* Code below used for debugging */
1253 void dump_instruction(ostream
& os
, int line
, prog_scope
*scope
,
1254 const glsl_to_tgsi_instruction
& inst
)
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
))
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
)
1270 os
<< setw(4) << line
<< ": ";
1271 os
<< setw(indent
* 4) << " ";