From 32aaf89823de11e98cb59d5ec78c66cd3e74bcd4 Mon Sep 17 00:00:00 2001 From: Kenneth Graunke Date: Mon, 15 Nov 2010 14:35:46 -0800 Subject: glsl: Rename various ir_* files to lower_* and opt_*. This helps distinguish between lowering passes, optimization passes, and other compiler code. --- src/glsl/opt_copy_propagation.cpp | 348 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 348 insertions(+) create mode 100644 src/glsl/opt_copy_propagation.cpp (limited to 'src/glsl/opt_copy_propagation.cpp') diff --git a/src/glsl/opt_copy_propagation.cpp b/src/glsl/opt_copy_propagation.cpp new file mode 100644 index 00000000000..0fe8fa6c419 --- /dev/null +++ b/src/glsl/opt_copy_propagation.cpp @@ -0,0 +1,348 @@ +/* + * 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. + */ + +/** + * \file ir_copy_propagation.cpp + * + * Moves usage of recently-copied variables to the previous copy of + * the variable. + * + * This should reduce the number of MOV instructions in the generated + * programs unless copy propagation is also done on the LIR, and may + * help anyway by triggering other optimizations that live in the HIR. + */ + +#include "ir.h" +#include "ir_visitor.h" +#include "ir_basic_block.h" +#include "ir_optimization.h" +#include "glsl_types.h" + +class acp_entry : public exec_node +{ +public: + acp_entry(ir_variable *lhs, ir_variable *rhs) + { + assert(lhs); + assert(rhs); + this->lhs = lhs; + this->rhs = rhs; + } + + ir_variable *lhs; + ir_variable *rhs; +}; + + +class kill_entry : public exec_node +{ +public: + kill_entry(ir_variable *var) + { + assert(var); + this->var = var; + } + + ir_variable *var; +}; + +class ir_copy_propagation_visitor : public ir_hierarchical_visitor { +public: + ir_copy_propagation_visitor() + { + progress = false; + mem_ctx = talloc_new(0); + this->acp = new(mem_ctx) exec_list; + this->kills = new(mem_ctx) exec_list; + } + ~ir_copy_propagation_visitor() + { + talloc_free(mem_ctx); + } + + virtual ir_visitor_status visit(class ir_dereference_variable *); + virtual ir_visitor_status visit_enter(class ir_loop *); + virtual ir_visitor_status visit_enter(class ir_function_signature *); + virtual ir_visitor_status visit_enter(class ir_function *); + virtual ir_visitor_status visit_leave(class ir_assignment *); + virtual ir_visitor_status visit_enter(class ir_call *); + virtual ir_visitor_status visit_enter(class ir_if *); + + void add_copy(ir_assignment *ir); + void kill(ir_variable *ir); + void handle_if_block(exec_list *instructions); + + /** List of acp_entry: The available copies to propagate */ + exec_list *acp; + /** + * List of kill_entry: The variables whose values were killed in this + * block. + */ + exec_list *kills; + + bool progress; + + bool killed_all; + + void *mem_ctx; +}; + +ir_visitor_status +ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir) +{ + /* Treat entry into a function signature as a completely separate + * block. Any instructions at global scope will be shuffled into + * main() at link time, so they're irrelevant to us. + */ + exec_list *orig_acp = this->acp; + exec_list *orig_kills = this->kills; + bool orig_killed_all = this->killed_all; + + this->acp = new(mem_ctx) exec_list; + this->kills = new(mem_ctx) exec_list; + this->killed_all = false; + + visit_list_elements(this, &ir->body); + + this->kills = orig_kills; + this->acp = orig_acp; + this->killed_all = orig_killed_all; + + return visit_continue_with_parent; +} + +ir_visitor_status +ir_copy_propagation_visitor::visit_leave(ir_assignment *ir) +{ + kill(ir->lhs->variable_referenced()); + + add_copy(ir); + + return visit_continue; +} + +ir_visitor_status +ir_copy_propagation_visitor::visit_enter(ir_function *ir) +{ + (void) ir; + return visit_continue; +} + +/** + * Replaces dereferences of ACP RHS variables with ACP LHS variables. + * + * This is where the actual copy propagation occurs. Note that the + * rewriting of ir_dereference means that the ir_dereference instance + * must not be shared by multiple IR operations! + */ +ir_visitor_status +ir_copy_propagation_visitor::visit(ir_dereference_variable *ir) +{ + if (this->in_assignee) + return visit_continue; + + ir_variable *var = ir->var; + + foreach_iter(exec_list_iterator, iter, *this->acp) { + acp_entry *entry = (acp_entry *)iter.get(); + + if (var == entry->lhs) { + ir->var = entry->rhs; + this->progress = true; + break; + } + } + + return visit_continue; +} + + +ir_visitor_status +ir_copy_propagation_visitor::visit_enter(ir_call *ir) +{ + /* Do copy propagation on call parameters, but skip any out params */ + exec_list_iterator sig_param_iter = ir->get_callee()->parameters.iterator(); + foreach_iter(exec_list_iterator, iter, ir->actual_parameters) { + ir_variable *sig_param = (ir_variable *)sig_param_iter.get(); + ir_instruction *ir = (ir_instruction *)iter.get(); + if (sig_param->mode != ir_var_out && sig_param->mode != ir_var_inout) { + ir->accept(this); + } + sig_param_iter.next(); + } + + /* Since we're unlinked, we don't (necssarily) know the side effects of + * this call. So kill all copies. + */ + acp->make_empty(); + this->killed_all = true; + + return visit_continue_with_parent; +} + +void +ir_copy_propagation_visitor::handle_if_block(exec_list *instructions) +{ + exec_list *orig_acp = this->acp; + exec_list *orig_kills = this->kills; + bool orig_killed_all = this->killed_all; + + this->acp = new(mem_ctx) exec_list; + this->kills = new(mem_ctx) exec_list; + this->killed_all = false; + + /* Populate the initial acp with a copy of the original */ + foreach_iter(exec_list_iterator, iter, *orig_acp) { + acp_entry *a = (acp_entry *)iter.get(); + this->acp->push_tail(new(this->mem_ctx) acp_entry(a->lhs, a->rhs)); + } + + visit_list_elements(this, instructions); + + if (this->killed_all) { + orig_acp->make_empty(); + } + + exec_list *new_kills = this->kills; + this->kills = orig_kills; + this->acp = orig_acp; + this->killed_all = this->killed_all || orig_killed_all; + + foreach_iter(exec_list_iterator, iter, *new_kills) { + kill_entry *k = (kill_entry *)iter.get(); + kill(k->var); + } +} + +ir_visitor_status +ir_copy_propagation_visitor::visit_enter(ir_if *ir) +{ + ir->condition->accept(this); + + handle_if_block(&ir->then_instructions); + handle_if_block(&ir->else_instructions); + + /* handle_if_block() already descended into the children. */ + return visit_continue_with_parent; +} + +ir_visitor_status +ir_copy_propagation_visitor::visit_enter(ir_loop *ir) +{ + exec_list *orig_acp = this->acp; + exec_list *orig_kills = this->kills; + bool orig_killed_all = this->killed_all; + + /* FINISHME: For now, the initial acp for loops is totally empty. + * We could go through once, then go through again with the acp + * cloned minus the killed entries after the first run through. + */ + this->acp = new(mem_ctx) exec_list; + this->kills = new(mem_ctx) exec_list; + this->killed_all = false; + + visit_list_elements(this, &ir->body_instructions); + + if (this->killed_all) { + orig_acp->make_empty(); + } + + exec_list *new_kills = this->kills; + this->kills = orig_kills; + this->acp = orig_acp; + this->killed_all = this->killed_all || orig_killed_all; + + foreach_iter(exec_list_iterator, iter, *new_kills) { + kill_entry *k = (kill_entry *)iter.get(); + kill(k->var); + } + + /* already descended into the children. */ + return visit_continue_with_parent; +} + +void +ir_copy_propagation_visitor::kill(ir_variable *var) +{ + assert(var != NULL); + + /* Remove any entries currently in the ACP for this kill. */ + foreach_iter(exec_list_iterator, iter, *acp) { + acp_entry *entry = (acp_entry *)iter.get(); + + if (entry->lhs == var || entry->rhs == var) { + entry->remove(); + } + } + + /* Add the LHS variable to the list of killed variables in this block. + */ + this->kills->push_tail(new(this->mem_ctx) kill_entry(var)); +} + +/** + * Adds an entry to the available copy list if it's a plain assignment + * of a variable to a variable. + */ +void +ir_copy_propagation_visitor::add_copy(ir_assignment *ir) +{ + acp_entry *entry; + + if (ir->condition) { + ir_constant *condition = ir->condition->as_constant(); + if (!condition || !condition->value.b[0]) + return; + } + + ir_variable *lhs_var = ir->whole_variable_written(); + ir_variable *rhs_var = ir->rhs->whole_variable_referenced(); + + if ((lhs_var != NULL) && (rhs_var != NULL)) { + if (lhs_var == rhs_var) { + /* This is a dumb assignment, but we've conveniently noticed + * it here. Removing it now would mess up the loop iteration + * calling us. Just flag it to not execute, and someone else + * will clean up the mess. + */ + ir->condition = new(talloc_parent(ir)) ir_constant(false); + this->progress = true; + } else { + entry = new(this->mem_ctx) acp_entry(lhs_var, rhs_var); + this->acp->push_tail(entry); + } + } +} + +/** + * Does a copy propagation pass on the code present in the instruction stream. + */ +bool +do_copy_propagation(exec_list *instructions) +{ + ir_copy_propagation_visitor v; + + visit_list_elements(&v, instructions); + + return v.progress; +} -- cgit v1.2.3 From df883eb1575a740bf91e01cbe2eaa4dbc1f9f154 Mon Sep 17 00:00:00 2001 From: Chad Versace Date: Wed, 17 Nov 2010 10:43:10 -0800 Subject: glsl: Fix Doxygen tag \file in recently renamed files --- src/glsl/lower_div_to_mul_rcp.cpp | 2 +- src/glsl/lower_explog_to_explog2.cpp | 2 +- src/glsl/lower_if_to_cond_assign.cpp | 2 +- src/glsl/lower_jumps.cpp | 2 +- src/glsl/lower_mat_op_to_vec.cpp | 2 +- src/glsl/lower_mod_to_fract.cpp | 2 +- src/glsl/lower_sub_to_add_neg.cpp | 2 +- src/glsl/lower_vec_index_to_cond_assign.cpp | 2 +- src/glsl/lower_vec_index_to_swizzle.cpp | 2 +- src/glsl/opt_algebraic.cpp | 2 +- src/glsl/opt_constant_folding.cpp | 2 +- src/glsl/opt_constant_propagation.cpp | 2 +- src/glsl/opt_constant_variable.cpp | 2 +- src/glsl/opt_copy_propagation.cpp | 2 +- src/glsl/opt_dead_code.cpp | 2 +- src/glsl/opt_dead_code_local.cpp | 2 +- src/glsl/opt_dead_functions.cpp | 2 +- src/glsl/opt_function_inlining.cpp | 2 +- src/glsl/opt_if_simplification.cpp | 2 +- src/glsl/opt_noop_swizzle.cpp | 2 +- src/glsl/opt_structure_splitting.cpp | 2 +- src/glsl/opt_swizzle_swizzle.cpp | 2 +- src/glsl/opt_tree_grafting.cpp | 2 +- 23 files changed, 23 insertions(+), 23 deletions(-) (limited to 'src/glsl/opt_copy_propagation.cpp') diff --git a/src/glsl/lower_div_to_mul_rcp.cpp b/src/glsl/lower_div_to_mul_rcp.cpp index 640d5d64f98..d34c4bc46d7 100644 --- a/src/glsl/lower_div_to_mul_rcp.cpp +++ b/src/glsl/lower_div_to_mul_rcp.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_div_to_mul_rcp.cpp + * \file lower_div_to_mul_rcp.cpp * * Breaks an ir_unop_div expression down to op0 * (rcp(op1)). * diff --git a/src/glsl/lower_explog_to_explog2.cpp b/src/glsl/lower_explog_to_explog2.cpp index 78694a2029d..8ad02625443 100644 --- a/src/glsl/lower_explog_to_explog2.cpp +++ b/src/glsl/lower_explog_to_explog2.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_explog_to_explog2.cpp + * \file lower_explog_to_explog2.cpp * * Many GPUs don't have a base e log or exponent instruction, but they * do have base 2 versions, so this pass converts exp and log to exp2 diff --git a/src/glsl/lower_if_to_cond_assign.cpp b/src/glsl/lower_if_to_cond_assign.cpp index 0b87413941a..cf48cfb8d61 100644 --- a/src/glsl/lower_if_to_cond_assign.cpp +++ b/src/glsl/lower_if_to_cond_assign.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_if_to_cond_assign.cpp + * \file lower_if_to_cond_assign.cpp * * This attempts to flatten all if statements to conditional * assignments for GPUs that don't do control flow. diff --git a/src/glsl/lower_jumps.cpp b/src/glsl/lower_jumps.cpp index b69cc1ec315..e1e7a5b0073 100644 --- a/src/glsl/lower_jumps.cpp +++ b/src/glsl/lower_jumps.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_lower_jumps.cpp + * \file lower_jumps.cpp */ #include "glsl_types.h" diff --git a/src/glsl/lower_mat_op_to_vec.cpp b/src/glsl/lower_mat_op_to_vec.cpp index 244fe489280..4965df8976d 100644 --- a/src/glsl/lower_mat_op_to_vec.cpp +++ b/src/glsl/lower_mat_op_to_vec.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_mat_op_to_vec.cpp + * \file lower_mat_op_to_vec.cpp * * Breaks matrix operation expressions down to a series of vector operations. * diff --git a/src/glsl/lower_mod_to_fract.cpp b/src/glsl/lower_mod_to_fract.cpp index c82a1f64fdd..ff907ae97cc 100644 --- a/src/glsl/lower_mod_to_fract.cpp +++ b/src/glsl/lower_mod_to_fract.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_mod_to_fract.cpp + * \file lower_mod_to_fract.cpp * * Breaks an ir_unop_mod expression down to (op1 * fract(op0 / op1)) * diff --git a/src/glsl/lower_sub_to_add_neg.cpp b/src/glsl/lower_sub_to_add_neg.cpp index 7ed8c1495e3..9e4019709b4 100644 --- a/src/glsl/lower_sub_to_add_neg.cpp +++ b/src/glsl/lower_sub_to_add_neg.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_sub_to_add_neg.cpp + * \file lower_sub_to_add_neg.cpp * * Breaks an ir_binop_sub expression down to add(op0, neg(op1)) * diff --git a/src/glsl/lower_vec_index_to_cond_assign.cpp b/src/glsl/lower_vec_index_to_cond_assign.cpp index cd8dedf2fe1..f8011a16e5b 100644 --- a/src/glsl/lower_vec_index_to_cond_assign.cpp +++ b/src/glsl/lower_vec_index_to_cond_assign.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_vec_index_to_cond_assign.cpp + * \file lower_vec_index_to_cond_assign.cpp * * Turns indexing into vector types to a series of conditional moves * of each channel's swizzle into a temporary. diff --git a/src/glsl/lower_vec_index_to_swizzle.cpp b/src/glsl/lower_vec_index_to_swizzle.cpp index 969dc8f94a2..9ae43c0db17 100644 --- a/src/glsl/lower_vec_index_to_swizzle.cpp +++ b/src/glsl/lower_vec_index_to_swizzle.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_vec_index_to_swizzle.cpp + * \file lower_vec_index_to_swizzle.cpp * * Turns constant indexing into vector types to swizzles. This will * let other swizzle-aware optimization passes catch these constructs, diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp index c7f5c3b4d63..88b6c485d31 100644 --- a/src/glsl/opt_algebraic.cpp +++ b/src/glsl/opt_algebraic.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_algebraic.cpp + * \file opt_algebraic.cpp * * Takes advantage of association, commutivity, and other algebraic * properties to simplify expressions. diff --git a/src/glsl/opt_constant_folding.cpp b/src/glsl/opt_constant_folding.cpp index 554c54fae3a..d69ca75fe03 100644 --- a/src/glsl/opt_constant_folding.cpp +++ b/src/glsl/opt_constant_folding.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_constant_folding.cpp + * \file opt_constant_folding.cpp * Replace constant-valued expressions with references to constant values. */ diff --git a/src/glsl/opt_constant_propagation.cpp b/src/glsl/opt_constant_propagation.cpp index 5d875b7be00..6719fc81863 100644 --- a/src/glsl/opt_constant_propagation.cpp +++ b/src/glsl/opt_constant_propagation.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_constant_propagation.cpp + * \file opt_constant_propagation.cpp * * Tracks assignments of constants to channels of variables, and * usage of those constant channels with direct usage of the constants. diff --git a/src/glsl/opt_constant_variable.cpp b/src/glsl/opt_constant_variable.cpp index 1fb73e765e1..8068d0c1435 100644 --- a/src/glsl/opt_constant_variable.cpp +++ b/src/glsl/opt_constant_variable.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_constant_variable.cpp + * \file opt_constant_variable.cpp * * Marks variables assigned a single constant value over the course * of the program as constant. diff --git a/src/glsl/opt_copy_propagation.cpp b/src/glsl/opt_copy_propagation.cpp index 0fe8fa6c419..8d07fefbfd0 100644 --- a/src/glsl/opt_copy_propagation.cpp +++ b/src/glsl/opt_copy_propagation.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_copy_propagation.cpp + * \file opt_copy_propagation.cpp * * Moves usage of recently-copied variables to the previous copy of * the variable. diff --git a/src/glsl/opt_dead_code.cpp b/src/glsl/opt_dead_code.cpp index 5cf5e99add4..cb500d2d105 100644 --- a/src/glsl/opt_dead_code.cpp +++ b/src/glsl/opt_dead_code.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_dead_code.cpp + * \file opt_dead_code.cpp * * Eliminates dead assignments and variable declarations from the code. */ diff --git a/src/glsl/opt_dead_code_local.cpp b/src/glsl/opt_dead_code_local.cpp index 4bbedf0ff94..5689e7d20db 100644 --- a/src/glsl/opt_dead_code_local.cpp +++ b/src/glsl/opt_dead_code_local.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_dead_code_local.cpp + * \file opt_dead_code_local.cpp * * Eliminates local dead assignments from the code. * diff --git a/src/glsl/opt_dead_functions.cpp b/src/glsl/opt_dead_functions.cpp index 16037a2632d..cf91cb6d368 100644 --- a/src/glsl/opt_dead_functions.cpp +++ b/src/glsl/opt_dead_functions.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_dead_functions.cpp + * \file opt_dead_functions.cpp * * Eliminates unused functions from the linked program. */ diff --git a/src/glsl/opt_function_inlining.cpp b/src/glsl/opt_function_inlining.cpp index 147c1824c1a..169fd826882 100644 --- a/src/glsl/opt_function_inlining.cpp +++ b/src/glsl/opt_function_inlining.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_function_inlining.cpp + * \file opt_function_inlining.cpp * * Replaces calls to functions with the body of the function. */ diff --git a/src/glsl/opt_if_simplification.cpp b/src/glsl/opt_if_simplification.cpp index 021615ebd12..618bacfecf1 100644 --- a/src/glsl/opt_if_simplification.cpp +++ b/src/glsl/opt_if_simplification.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_if_simplification.cpp + * \file opt_if_simplification.cpp * * Moves constant branches of if statements out to the surrounding * instruction stream. diff --git a/src/glsl/opt_noop_swizzle.cpp b/src/glsl/opt_noop_swizzle.cpp index 0403dfa4e93..0a906aaf1da 100644 --- a/src/glsl/opt_noop_swizzle.cpp +++ b/src/glsl/opt_noop_swizzle.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_noop_swizzle.cpp + * \file opt_noop_swizzle.cpp * * If a swizzle doesn't change the order or count of components, then * remove the swizzle so that other optimization passes see the value diff --git a/src/glsl/opt_structure_splitting.cpp b/src/glsl/opt_structure_splitting.cpp index ff3ec936eb8..d6191002c2f 100644 --- a/src/glsl/opt_structure_splitting.cpp +++ b/src/glsl/opt_structure_splitting.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_structure_splitting.cpp + * \file opt_structure_splitting.cpp * * If a structure is only ever referenced by its components, then * split those components out to individual variables so they can be diff --git a/src/glsl/opt_swizzle_swizzle.cpp b/src/glsl/opt_swizzle_swizzle.cpp index 0ffb4fa3133..bc442fa8693 100644 --- a/src/glsl/opt_swizzle_swizzle.cpp +++ b/src/glsl/opt_swizzle_swizzle.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_swizzle_swizzle.cpp + * \file opt_swizzle_swizzle.cpp * * Eliminates the second swizzle in a swizzle chain. */ diff --git a/src/glsl/opt_tree_grafting.cpp b/src/glsl/opt_tree_grafting.cpp index 9b569b8284f..9917c045b1d 100644 --- a/src/glsl/opt_tree_grafting.cpp +++ b/src/glsl/opt_tree_grafting.cpp @@ -22,7 +22,7 @@ */ /** - * \file ir_tree_grafting.cpp + * \file opt_tree_grafting.cpp * * Takes assignments to variables that are dereferenced only once and * pastes the RHS expression into where the variable is dereferenced. -- cgit v1.2.3