2010-07-19 09:36:43 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-17 10:43:10 -08:00
|
|
|
* \file lower_if_to_cond_assign.cpp
|
2010-07-19 09:36:43 -07:00
|
|
|
*
|
2010-12-27 00:22:38 -08:00
|
|
|
* This attempts to flatten if-statements to conditional assignments for
|
|
|
|
|
* GPUs with limited or no flow control support.
|
2010-07-19 09:36:43 -07:00
|
|
|
*
|
|
|
|
|
* It can't handle other control flow being inside of its block, such
|
|
|
|
|
* as calls or loops. Hopefully loop unrolling and inlining will take
|
|
|
|
|
* care of those.
|
2010-12-27 00:22:38 -08:00
|
|
|
*
|
|
|
|
|
* Drivers for GPUs with no control flow support should simply call
|
|
|
|
|
*
|
|
|
|
|
* lower_if_to_cond_assign(instructions)
|
|
|
|
|
*
|
|
|
|
|
* to attempt to flatten all if-statements.
|
|
|
|
|
*
|
|
|
|
|
* Some GPUs (such as i965 prior to gen6) do support control flow, but have a
|
|
|
|
|
* maximum nesting depth N. Drivers for such hardware can call
|
|
|
|
|
*
|
|
|
|
|
* lower_if_to_cond_assign(instructions, N)
|
|
|
|
|
*
|
|
|
|
|
* to attempt to flatten any if-statements appearing at depth > N.
|
2010-07-19 09:36:43 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "glsl_types.h"
|
|
|
|
|
#include "ir.h"
|
|
|
|
|
|
|
|
|
|
class ir_if_to_cond_assign_visitor : public ir_hierarchical_visitor {
|
|
|
|
|
public:
|
2010-12-27 00:22:38 -08:00
|
|
|
ir_if_to_cond_assign_visitor(unsigned max_depth)
|
2010-07-19 09:36:43 -07:00
|
|
|
{
|
|
|
|
|
this->progress = false;
|
2010-12-27 00:22:38 -08:00
|
|
|
this->max_depth = max_depth;
|
|
|
|
|
this->depth = 0;
|
2010-07-19 09:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
2010-12-27 00:22:38 -08:00
|
|
|
ir_visitor_status visit_enter(ir_if *);
|
2010-07-19 09:36:43 -07:00
|
|
|
ir_visitor_status visit_leave(ir_if *);
|
|
|
|
|
|
|
|
|
|
bool progress;
|
2010-12-27 00:22:38 -08:00
|
|
|
unsigned max_depth;
|
|
|
|
|
unsigned depth;
|
2010-07-19 09:36:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool
|
2010-12-27 00:22:38 -08:00
|
|
|
lower_if_to_cond_assign(exec_list *instructions, unsigned max_depth)
|
2010-07-19 09:36:43 -07:00
|
|
|
{
|
2010-12-27 00:22:38 -08:00
|
|
|
ir_if_to_cond_assign_visitor v(max_depth);
|
2010-07-19 09:36:43 -07:00
|
|
|
|
|
|
|
|
visit_list_elements(&v, instructions);
|
|
|
|
|
|
|
|
|
|
return v.progress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
check_control_flow(ir_instruction *ir, void *data)
|
|
|
|
|
{
|
|
|
|
|
bool *found_control_flow = (bool *)data;
|
|
|
|
|
switch (ir->ir_type) {
|
|
|
|
|
case ir_type_call:
|
|
|
|
|
case ir_type_discard:
|
|
|
|
|
case ir_type_loop:
|
|
|
|
|
case ir_type_loop_jump:
|
|
|
|
|
case ir_type_return:
|
|
|
|
|
*found_control_flow = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
move_block_to_cond_assign(void *mem_ctx,
|
2011-08-01 13:28:11 -07:00
|
|
|
ir_if *if_ir, ir_rvalue *cond_expr,
|
|
|
|
|
exec_list *instructions)
|
2010-07-19 09:36:43 -07:00
|
|
|
{
|
2011-08-01 13:36:12 -07:00
|
|
|
foreach_list_safe(node, instructions) {
|
|
|
|
|
ir_instruction *ir = (ir_instruction *) node;
|
2010-07-19 09:36:43 -07:00
|
|
|
|
|
|
|
|
if (ir->ir_type == ir_type_assignment) {
|
|
|
|
|
ir_assignment *assign = (ir_assignment *)ir;
|
|
|
|
|
|
|
|
|
|
if (!assign->condition) {
|
2011-08-01 13:28:11 -07:00
|
|
|
assign->condition = cond_expr->clone(mem_ctx, NULL);
|
2010-07-19 09:36:43 -07:00
|
|
|
} else {
|
2011-08-01 13:28:11 -07:00
|
|
|
assign->condition =
|
|
|
|
|
new(mem_ctx) ir_expression(ir_binop_logic_and,
|
|
|
|
|
glsl_type::bool_type,
|
|
|
|
|
cond_expr->clone(mem_ctx, NULL),
|
|
|
|
|
assign->condition);
|
2010-07-19 09:36:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now, move from the if block to the block surrounding it. */
|
|
|
|
|
ir->remove();
|
|
|
|
|
if_ir->insert_before(ir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-27 00:22:38 -08:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_if_to_cond_assign_visitor::visit_enter(ir_if *ir)
|
|
|
|
|
{
|
|
|
|
|
(void) ir;
|
|
|
|
|
this->depth++;
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 09:36:43 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_if_to_cond_assign_visitor::visit_leave(ir_if *ir)
|
|
|
|
|
{
|
2010-12-27 00:22:38 -08:00
|
|
|
/* Only flatten when beyond the GPU's maximum supported nesting depth. */
|
2011-06-02 12:42:48 -07:00
|
|
|
if (this->depth-- <= this->max_depth)
|
2010-12-27 00:22:38 -08:00
|
|
|
return visit_continue;
|
|
|
|
|
|
2010-07-19 09:36:43 -07:00
|
|
|
bool found_control_flow = false;
|
|
|
|
|
ir_assignment *assign;
|
|
|
|
|
ir_dereference_variable *deref;
|
|
|
|
|
|
|
|
|
|
/* Check that both blocks don't contain anything we can't support. */
|
|
|
|
|
foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
|
|
|
|
|
ir_instruction *then_ir = (ir_instruction *)then_iter.get();
|
|
|
|
|
visit_tree(then_ir, check_control_flow, &found_control_flow);
|
|
|
|
|
}
|
|
|
|
|
foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
|
|
|
|
|
ir_instruction *else_ir = (ir_instruction *)else_iter.get();
|
|
|
|
|
visit_tree(else_ir, check_control_flow, &found_control_flow);
|
|
|
|
|
}
|
|
|
|
|
if (found_control_flow)
|
|
|
|
|
return visit_continue;
|
|
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
void *mem_ctx = ralloc_parent(ir);
|
2010-07-19 09:36:43 -07:00
|
|
|
|
2011-08-01 13:55:46 -07:00
|
|
|
/* Store the condition to a variable. Move all of the instructions from
|
|
|
|
|
* the then-clause of the if-statement. Use the condition variable as a
|
|
|
|
|
* condition for all assignments.
|
2010-07-19 09:36:43 -07:00
|
|
|
*/
|
2011-08-01 13:55:46 -07:00
|
|
|
ir_variable *const then_var =
|
|
|
|
|
new(mem_ctx) ir_variable(glsl_type::bool_type,
|
|
|
|
|
"if_to_cond_assign_then",
|
|
|
|
|
ir_var_temporary);
|
|
|
|
|
ir->insert_before(then_var);
|
|
|
|
|
|
|
|
|
|
ir_dereference_variable *then_cond =
|
|
|
|
|
new(mem_ctx) ir_dereference_variable(then_var);
|
|
|
|
|
|
|
|
|
|
assign = new(mem_ctx) ir_assignment(then_cond, ir->condition);
|
2010-07-19 09:36:43 -07:00
|
|
|
ir->insert_before(assign);
|
|
|
|
|
|
2011-08-01 13:55:46 -07:00
|
|
|
move_block_to_cond_assign(mem_ctx, ir, then_cond,
|
2011-08-01 13:28:11 -07:00
|
|
|
&ir->then_instructions);
|
|
|
|
|
|
2011-08-01 13:55:46 -07:00
|
|
|
/* If there are instructions in the else-clause, store the inverse of the
|
|
|
|
|
* condition to a variable. Move all of the instructions from the
|
|
|
|
|
* else-clause if the if-statement. Use the (inverse) condition variable
|
|
|
|
|
* as a condition for all assignments.
|
|
|
|
|
*/
|
|
|
|
|
if (!ir->else_instructions.is_empty()) {
|
|
|
|
|
ir_variable *const else_var =
|
|
|
|
|
new(mem_ctx) ir_variable(glsl_type::bool_type,
|
|
|
|
|
"if_to_cond_assign_else",
|
|
|
|
|
ir_var_temporary);
|
|
|
|
|
ir->insert_before(else_var);
|
|
|
|
|
|
|
|
|
|
ir_dereference_variable *else_cond =
|
|
|
|
|
new(mem_ctx) ir_dereference_variable(else_var);
|
|
|
|
|
|
|
|
|
|
ir_rvalue *inverse =
|
|
|
|
|
new(mem_ctx) ir_expression(ir_unop_logic_not,
|
|
|
|
|
then_cond->clone(mem_ctx, NULL));
|
|
|
|
|
|
|
|
|
|
assign = new(mem_ctx) ir_assignment(else_cond, inverse);
|
|
|
|
|
ir->insert_before(assign);
|
|
|
|
|
|
|
|
|
|
move_block_to_cond_assign(mem_ctx, ir, else_cond,
|
|
|
|
|
&ir->else_instructions);
|
|
|
|
|
}
|
2010-07-19 09:36:43 -07:00
|
|
|
|
|
|
|
|
ir->remove();
|
|
|
|
|
|
|
|
|
|
this->progress = true;
|
|
|
|
|
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|