2 * Copyright © 2010 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 opt_discard_simplification.cpp
27 * This pass simplifies if-statements and loops containing unconditional
30 * Case 1: Both branches contain unconditional discards:
31 * -----------------------------------------------------
47 * Case 2: The "then" clause contains an unconditional discard:
48 * ------------------------------------------------------------
66 * Case 3: The "else" clause contains an unconditional discard:
67 * ------------------------------------------------------------
86 #include "glsl_types.h"
89 class discard_simplifier
: public ir_hierarchical_visitor
{
93 this->progress
= false;
96 ir_visitor_status
visit_enter(ir_if
*);
97 ir_visitor_status
visit_enter(ir_loop
*);
98 ir_visitor_status
visit_enter(ir_assignment
*);
104 find_unconditional_discard(exec_list
&instructions
)
106 foreach_list(n
, &instructions
) {
107 ir_discard
*ir
= ((ir_instruction
*) n
)->as_discard();
108 if (ir
!= NULL
&& ir
->condition
== NULL
)
115 is_only_instruction(ir_discard
*discard
)
117 return (discard
->prev
->is_head_sentinel() &&
118 discard
->next
->is_tail_sentinel());
121 /* We only care about the top level instructions, so don't descend
125 discard_simplifier::visit_enter(ir_assignment
*ir
)
127 return visit_continue_with_parent
;
131 discard_simplifier::visit_enter(ir_if
*ir
)
133 ir_discard
*then_discard
= find_unconditional_discard(ir
->then_instructions
);
134 ir_discard
*else_discard
= find_unconditional_discard(ir
->else_instructions
);
136 if (then_discard
== NULL
&& else_discard
== NULL
)
137 return visit_continue
;
139 /* If both branches result in discard, replace whole if with discard. */
140 if (then_discard
!= NULL
&& else_discard
!= NULL
) {
141 this->progress
= true;
142 ir
->replace_with(then_discard
);
143 return visit_continue_with_parent
;
146 /* Otherwise, one branch has a discard. */
147 if (then_discard
!= NULL
&& !is_only_instruction(then_discard
)) {
148 this->progress
= true;
149 ir
->then_instructions
.make_empty();
150 ir
->then_instructions
.push_tail(then_discard
);
151 } else if (else_discard
!= NULL
&& !is_only_instruction(else_discard
)) {
152 this->progress
= true;
153 ir
->else_instructions
.make_empty();
154 ir
->else_instructions
.push_tail(else_discard
);
157 visit_list_elements(this, &ir
->then_instructions
);
158 return visit_continue_with_parent
;
162 discard_simplifier::visit_enter(ir_loop
*ir
)
164 ir_discard
*discard
= find_unconditional_discard(ir
->body_instructions
);
167 ir
->replace_with(discard
);
168 return visit_continue_with_parent
;
171 return visit_continue
;
175 do_discard_simplification(exec_list
*instructions
)
177 /* Look for a top-level unconditional discard */
178 ir_discard
*discard
= find_unconditional_discard(*instructions
);
179 if (discard
!= NULL
) {
180 instructions
->make_empty();
181 instructions
->push_tail(discard
);
185 discard_simplifier v
;
187 visit_list_elements(&v
, instructions
);