2 * Copyright © 2016 Intel Corporation
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.
25 * \file propagate_invariance.cpp
26 * Propagate the "invariant" and "precise" qualifiers to variables used to
27 * compute invariant or precise values.
29 * The GLSL spec (depending on what version you read) says, among the
30 * conditions for getting bit-for-bit the same values on an invariant output:
32 * "All operations in the consuming expressions and any intermediate
33 * expressions must be the same, with the same order of operands and same
34 * associativity, to give the same order of evaluation."
36 * This effectively means that if a variable is used to compute an invariant
37 * value then that variable becomes invariant. The same should apply to the
38 * "precise" qualifier.
42 #include "ir_visitor.h"
43 #include "ir_rvalue_visitor.h"
44 #include "ir_optimization.h"
45 #include "compiler/glsl_types.h"
49 class ir_invariance_propagation_visitor
: public ir_hierarchical_visitor
{
51 ir_invariance_propagation_visitor()
53 this->progress
= false;
57 virtual ~ir_invariance_propagation_visitor()
62 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
63 virtual ir_visitor_status
visit_leave(ir_assignment
*ir
);
64 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
);
70 } /* unnamed namespace */
73 ir_invariance_propagation_visitor::visit_enter(ir_assignment
*ir
)
75 assert(this->dst_var
== NULL
);
76 ir_variable
*var
= ir
->lhs
->variable_referenced();
77 if (var
->data
.invariant
|| var
->data
.precise
) {
79 return visit_continue
;
81 return visit_continue_with_parent
;
86 ir_invariance_propagation_visitor::visit_leave(ir_assignment
*ir
)
90 return visit_continue
;
94 ir_invariance_propagation_visitor::visit(ir_dereference_variable
*ir
)
96 if (this->dst_var
== NULL
)
97 return visit_continue
;
99 if (this->dst_var
->data
.invariant
) {
100 if (!ir
->var
->data
.invariant
)
101 this->progress
= true;
103 ir
->var
->data
.invariant
= true;
106 if (this->dst_var
->data
.precise
) {
107 if (!ir
->var
->data
.precise
)
108 this->progress
= true;
110 ir
->var
->data
.precise
= true;
113 return visit_continue
;
117 propagate_invariance(exec_list
*instructions
)
119 ir_invariance_propagation_visitor visitor
;
122 visitor
.progress
= false;
123 visit_list_elements(&visitor
, instructions
);
124 } while (visitor
.progress
);