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 lower_discard.cpp
27 * This pass moves discards out of if-statements.
29 * Case 1: The "then" branch contains a conditional discard:
30 * ---------------------------------------------------------
52 * Case 2: The "else" branch contains a conditional discard:
53 * ---------------------------------------------------------
75 * Case 3: Both branches contain a conditional discard:
76 * ----------------------------------------------------
102 * If there are multiple conditional discards, we need only deal with one of
103 * them. Repeatedly applying this pass will take care of the others.
105 * Unconditional discards are treated as having a condition of "true".
108 #include "glsl_types.h"
111 class lower_discard_visitor
: public ir_hierarchical_visitor
{
113 lower_discard_visitor()
115 this->progress
= false;
118 ir_visitor_status
visit_leave(ir_if
*);
125 lower_discard(exec_list
*instructions
)
127 lower_discard_visitor v
;
129 visit_list_elements(&v
, instructions
);
136 find_discard(exec_list
&instructions
)
138 foreach_list(n
, &instructions
) {
139 ir_discard
*ir
= ((ir_instruction
*) n
)->as_discard();
148 replace_discard(void *mem_ctx
, ir_variable
*var
, ir_discard
*ir
)
150 ir_rvalue
*condition
= ir
->condition
;
152 /* For unconditional discards, use "true" as the condition. */
153 if (condition
== NULL
)
154 condition
= new(mem_ctx
) ir_constant(true);
156 ir_assignment
*assignment
=
157 new(mem_ctx
) ir_assignment(new(mem_ctx
) ir_dereference_variable(var
),
160 ir
->replace_with(assignment
);
165 lower_discard_visitor::visit_leave(ir_if
*ir
)
167 ir_discard
*then_discard
= find_discard(ir
->then_instructions
);
168 ir_discard
*else_discard
= find_discard(ir
->else_instructions
);
170 if (then_discard
== NULL
&& else_discard
== NULL
)
171 return visit_continue
;
173 void *mem_ctx
= talloc_parent(ir
);
175 ir_variable
*temp
= new(mem_ctx
) ir_variable(glsl_type::bool_type
,
178 ir_assignment
*temp_initializer
=
179 new(mem_ctx
) ir_assignment(new(mem_ctx
) ir_dereference_variable(temp
),
180 new(mem_ctx
) ir_constant(false), NULL
);
182 ir
->insert_before(temp
);
183 ir
->insert_before(temp_initializer
);
185 if (then_discard
!= NULL
)
186 replace_discard(mem_ctx
, temp
, then_discard
);
188 if (else_discard
!= NULL
)
189 replace_discard(mem_ctx
, temp
, else_discard
);
191 ir_discard
*discard
= then_discard
!= NULL
? then_discard
: else_discard
;
192 discard
->condition
= new(mem_ctx
) ir_dereference_variable(temp
);
193 ir
->insert_after(discard
);
195 this->progress
= true;
197 return visit_continue
;