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"
113 class lower_discard_visitor
: public ir_hierarchical_visitor
{
115 lower_discard_visitor()
117 this->progress
= false;
120 ir_visitor_status
visit_leave(ir_if
*);
125 } /* anonymous namespace */
128 lower_discard(exec_list
*instructions
)
130 lower_discard_visitor v
;
132 visit_list_elements(&v
, instructions
);
139 find_discard(exec_list
&instructions
)
141 foreach_in_list(ir_instruction
, node
, &instructions
) {
142 ir_discard
*ir
= node
->as_discard();
151 replace_discard(void *mem_ctx
, ir_variable
*var
, ir_discard
*ir
)
153 ir_rvalue
*condition
= ir
->condition
;
155 /* For unconditional discards, use "true" as the condition. */
156 if (condition
== NULL
)
157 condition
= new(mem_ctx
) ir_constant(true);
159 ir_assignment
*assignment
=
160 new(mem_ctx
) ir_assignment(new(mem_ctx
) ir_dereference_variable(var
),
163 ir
->replace_with(assignment
);
168 lower_discard_visitor::visit_leave(ir_if
*ir
)
170 ir_discard
*then_discard
= find_discard(ir
->then_instructions
);
171 ir_discard
*else_discard
= find_discard(ir
->else_instructions
);
173 if (then_discard
== NULL
&& else_discard
== NULL
)
174 return visit_continue
;
176 void *mem_ctx
= ralloc_parent(ir
);
178 ir_variable
*temp
= new(mem_ctx
) ir_variable(glsl_type::bool_type
,
181 ir_assignment
*temp_initializer
=
182 new(mem_ctx
) ir_assignment(new(mem_ctx
) ir_dereference_variable(temp
),
183 new(mem_ctx
) ir_constant(false), NULL
);
185 ir
->insert_before(temp
);
186 ir
->insert_before(temp_initializer
);
188 if (then_discard
!= NULL
)
189 replace_discard(mem_ctx
, temp
, then_discard
);
191 if (else_discard
!= NULL
)
192 replace_discard(mem_ctx
, temp
, else_discard
);
194 ir_discard
*discard
= then_discard
!= NULL
? then_discard
: else_discard
;
195 discard
->condition
= new(mem_ctx
) ir_dereference_variable(temp
);
196 ir
->insert_after(discard
);
198 this->progress
= true;
200 return visit_continue
;