ir_copy_propagation: New pass to rewrite dereferences to avoid copies.

This is pretty basic.  Right now it only handles pure assignments --
same type on each side, no swizzling, and only within basic blocks.
This commit is contained in:
Eric Anholt 2010-05-04 13:04:40 -07:00
parent 05a4e59c24
commit 5c89f0ecb9
9 changed files with 748 additions and 1 deletions

View File

@ -30,14 +30,20 @@ glsl_SOURCES = \
ast_expr.cpp ast_to_hir.cpp ast_function.cpp ast_type.cpp \
ir.cpp hir_field_selection.cpp builtin_function.cpp \
ir_print_visitor.cpp ir_variable.cpp ir_function.cpp \
ir_basic_block.cpp \
ir_basic_block.h \
ir_constant_expression.cpp \
ir_constant_folding.cpp \
ir_copy_propagation.cpp \
ir_copy_propagation.h \
ir_dead_code.cpp \
ir_expression_flattening.cpp \
ir_function_can_inline.cpp \
ir_function_inlining.cpp \
ir_if_simplification.cpp \
ir_reader.cpp s_expression.cpp
ir_reader.cpp s_expression.cpp \
ir_visit_tree.cpp \
ir_visit_tree.h
BUILT_SOURCES = glsl_parser.h glsl_parser.cpp glsl_lexer.cpp
CLEANFILES = $(BUILT_SOURCES)

View File

@ -35,6 +35,7 @@
#include "glsl_parser_extras.h"
#include "glsl_parser.h"
#include "ir_constant_folding.h"
#include "ir_copy_propagation.h"
#include "ir_dead_code.h"
#include "ir_function_inlining.h"
#include "ir_if_simplification.h"
@ -786,6 +787,7 @@ main(int argc, char **argv)
progress = do_function_inlining(&instructions) || progress;
progress = do_if_simplification(&instructions) || progress;
progress = do_copy_propagation(&instructions) || progress;
progress = do_dead_code_unlinked(&instructions) || progress;
/* Constant folding */

6
ir.h
View File

@ -62,6 +62,7 @@ public:
virtual class ir_return * as_return() { return NULL; }
virtual class ir_if * as_if() { return NULL; }
virtual class ir_swizzle * as_swizzle() { return NULL; }
virtual class ir_constant * as_constant() { return NULL; }
/*@}*/
protected:
@ -787,6 +788,11 @@ public:
ir_constant(int i);
ir_constant(float f);
virtual ir_constant *as_constant()
{
return this;
}
virtual void accept(ir_visitor *v)
{
v->visit(this);

136
ir_basic_block.cpp Normal file
View File

@ -0,0 +1,136 @@
/*
* 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_basic_block.cpp
*
* Basic block analysis of instruction streams.
*/
#include <stdio.h>
#include "ir.h"
#include "ir_visitor.h"
#include "ir_visit_tree.h"
#include "ir_basic_block.h"
#include "glsl_types.h"
static void
has_call_callback(ir_instruction *ir, void *data)
{
bool *has_call = (bool *)data;
*has_call = *has_call || ir->as_call();
}
/**
* Calls a user function for every basic block in the instruction stream.
*
* Basic block analysis is pretty easy in our IR thanks to the lack of
* unstructured control flow. We've got:
*
* ir_loop (for () {}, while () {}, do {} while ())
* ir_loop_jump (
* ir_if () {}
* ir_return
* ir_call()
*
* Note that the basic blocks returned by this don't encompass all
* operations performed by the program -- for example, if conditions
* don't get returned, nor do the assignments that will be generated
* for ir_call parameters.
*/
void call_for_basic_blocks(exec_list *instructions,
void (*callback)(ir_instruction *first,
ir_instruction *last))
{
ir_instruction *leader = NULL;
ir_instruction *last = NULL;
foreach_iter(exec_list_iterator, iter, *instructions) {
ir_instruction *ir = (ir_instruction *)iter.get();
ir_if *ir_if;
ir_loop *ir_loop;
ir_function *ir_function;
if (!leader)
leader = ir;
if ((ir_if = ir->as_if())) {
callback(leader, ir);
leader = NULL;
call_for_basic_blocks(&ir_if->then_instructions, callback);
call_for_basic_blocks(&ir_if->else_instructions, callback);
} else if ((ir_loop = ir->as_loop())) {
callback(leader, ir);
leader = NULL;
call_for_basic_blocks(&ir_loop->body_instructions, callback);
} else if (ir->as_return() || ir->as_call()) {
callback(leader, ir);
leader = NULL;
} else if ((ir_function = ir->as_function())) {
/* A function definition doesn't interrupt our basic block
* since execution doesn't go into it. We should process the
* bodies of its signatures for BBs, though.
*
* Note that we miss an opportunity for producing more
* maximal BBs between the instructions that precede main()
* and the body of main(). Perhaps those instructions ought
* to live inside of main().
*/
foreach_iter(exec_list_iterator, fun_iter, *ir_function) {
ir_function_signature *ir_sig;
ir_sig = (ir_function_signature *)fun_iter.get();
call_for_basic_blocks(&ir_sig->body, callback);
}
} else if (ir->as_assignment()) {
bool has_call = false;
/* If there's a call in the expression tree being assigned,
* then that ends the BB too.
*
* The assumption is that any consumer of the basic block
* walker is fine with the fact that the call is somewhere in
* the tree even if portions of the tree may be evaluated
* after the call.
*
* A consumer that has an issue with this could not process
* the last instruction of the basic block. If doing so,
* expression flattener may be useful before using the basic
* block finder to get more maximal basic blocks out.
*/
ir_visit_tree(ir, has_call_callback, &has_call);
if (has_call) {
callback(leader, ir);
leader = NULL;
}
}
last = ir;
}
if (leader) {
callback(leader, last);
}
}

26
ir_basic_block.h Normal file
View File

@ -0,0 +1,26 @@
/*
* 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.
*/
void call_for_basic_blocks(exec_list *instructions,
void (*callback)(ir_instruction *first,
ir_instruction *last));

313
ir_copy_propagation.cpp Normal file
View File

@ -0,0 +1,313 @@
/*
* 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_dead_code.cpp
*
* Eliminates dead assignments and variable declarations from the code.
*/
#include <stdio.h>
#include "ir.h"
#include "ir_visitor.h"
#include "ir_basic_block.h"
#include "ir_copy_propagation.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 ir_copy_propagation_visitor : public ir_visitor {
public:
ir_copy_propagation_visitor(exec_list *acp)
{
progress = false;
this->acp = acp;
}
/**
* \name Visit methods
*
* As typical for the visitor pattern, there must be one \c visit method for
* each concrete subclass of \c ir_instruction. Virtual base classes within
* the hierarchy should not have \c visit methods.
*/
/*@{*/
virtual void visit(ir_variable *);
virtual void visit(ir_loop *);
virtual void visit(ir_loop_jump *);
virtual void visit(ir_function_signature *);
virtual void visit(ir_function *);
virtual void visit(ir_expression *);
virtual void visit(ir_swizzle *);
virtual void visit(ir_dereference *);
virtual void visit(ir_assignment *);
virtual void visit(ir_constant *);
virtual void visit(ir_call *);
virtual void visit(ir_return *);
virtual void visit(ir_if *);
/*@}*/
/** List of acp_entry */
exec_list *acp;
bool progress;
};
void
ir_copy_propagation_visitor::visit(ir_variable *ir)
{
(void)ir;
}
void
ir_copy_propagation_visitor::visit(ir_loop *ir)
{
(void)ir;
}
void
ir_copy_propagation_visitor::visit(ir_loop_jump *ir)
{
(void) ir;
}
void
ir_copy_propagation_visitor::visit(ir_function_signature *ir)
{
(void)ir;
}
void
ir_copy_propagation_visitor::visit(ir_function *ir)
{
(void) ir;
}
void
ir_copy_propagation_visitor::visit(ir_expression *ir)
{
unsigned int operand;
for (operand = 0; operand < ir->get_num_operands(); operand++) {
ir->operands[operand]->accept(this);
}
}
void
ir_copy_propagation_visitor::visit(ir_swizzle *ir)
{
ir->val->accept(this);
}
/**
* 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!
*/
void
ir_copy_propagation_visitor::visit(ir_dereference *ir)
{
ir_variable *var;
if (ir->mode == ir_dereference::ir_reference_array) {
ir->selector.array_index->accept(this);
}
var = ir->var->as_variable();
if (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;
}
}
} else {
ir->var->accept(this);
}
}
void
ir_copy_propagation_visitor::visit(ir_assignment *ir)
{
if (ir->condition)
ir->condition->accept(this);
/* Ignores the LHS. Don't want to rewrite the LHS to point at some
* other storage!
*/
ir->rhs->accept(this);
}
void
ir_copy_propagation_visitor::visit(ir_constant *ir)
{
(void) ir;
}
void
ir_copy_propagation_visitor::visit(ir_call *ir)
{
(void)ir;
/* Note, if we were to do copy propagation to parameters of calls, we'd
* have to be careful about out params.
*/
}
void
ir_copy_propagation_visitor::visit(ir_return *ir)
{
ir_rvalue *val = ir->get_value();
if (val)
val->accept(this);
}
void
ir_copy_propagation_visitor::visit(ir_if *ir)
{
ir->condition->accept(this);
}
static void
propagate_copies(ir_instruction *ir, exec_list *acp)
{
ir_copy_propagation_visitor v(acp);
ir->accept(&v);
}
static void
kill_invalidated_copies(ir_assignment *ir, exec_list *acp)
{
ir_dereference *lhs_deref = ir->lhs->as_dereference();
/* Only handle simple dereferences for now. */
if (lhs_deref &&
lhs_deref->mode == ir_dereference::ir_reference_variable) {
ir_variable *var = lhs_deref->var->as_variable();
foreach_iter(exec_list_iterator, iter, *acp) {
acp_entry *entry = (acp_entry *)iter.get();
if (entry->lhs == var || entry->rhs == var) {
entry->remove();
}
}
} else {
/* FINISHME: Only clear out the entries we overwrote here. */
acp->make_empty();
}
}
/**
* Adds an entry to the available copy list if it's a plain assignment
* of a variable to a variable.
*/
static void
add_copy(ir_assignment *ir, exec_list *acp)
{
acp_entry *entry;
if (ir->condition) {
ir_constant *condition = ir->condition->as_constant();
if (!condition || !condition->value.b[0])
return;
}
ir_dereference *lhs_deref = ir->lhs->as_dereference();
if (!lhs_deref || lhs_deref->mode != ir_dereference::ir_reference_variable)
return;
ir_variable *lhs_var = lhs_deref->var->as_variable();
ir_dereference *rhs_deref = ir->rhs->as_dereference();
if (!rhs_deref || rhs_deref->mode != ir_dereference::ir_reference_variable)
return;
ir_variable *rhs_var = rhs_deref->var->as_variable();
entry = new acp_entry(lhs_var, rhs_var);
acp->push_tail(entry);
}
static void
copy_propagation_basic_block(ir_instruction *first,
ir_instruction *last)
{
ir_instruction *ir;
/* List of avaialble_copy */
exec_list acp;
for (ir = first;; ir = (ir_instruction *)ir->next) {
ir_assignment *ir_assign = ir->as_assignment();
propagate_copies(ir, &acp);
if (ir_assign) {
kill_invalidated_copies(ir_assign, &acp);
add_copy(ir_assign, &acp);
}
if (ir == last)
break;
}
}
/**
* Does a copy propagation pass on the code present in the instruction stream.
*/
bool
do_copy_propagation(exec_list *instructions)
{
bool progress = false;
call_for_basic_blocks(instructions, copy_propagation_basic_block);
/* FINISHME: Return a legit progress value. */
return progress;
}

24
ir_copy_propagation.h Normal file
View File

@ -0,0 +1,24 @@
/*
* 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.
*/
bool do_copy_propagation(exec_list *instructions);

207
ir_visit_tree.cpp Normal file
View File

@ -0,0 +1,207 @@
/*
* 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_dead_code.cpp
*
* Eliminates dead assignments and variable declarations from the code.
*/
#define NULL 0
#include "ir.h"
#include "ir_visitor.h"
#include "ir_visit_tree.h"
class ir_tree_visitor : public ir_visitor {
public:
/**
* \name Visit methods
*
* As typical for the visitor pattern, there must be one \c visit method for
* each concrete subclass of \c ir_instruction. Virtual base classes within
* the hierarchy should not have \c visit methods.
*/
/*@{*/
virtual void visit(ir_variable *);
virtual void visit(ir_loop *);
virtual void visit(ir_loop_jump *);
virtual void visit(ir_function_signature *);
virtual void visit(ir_function *);
virtual void visit(ir_expression *);
virtual void visit(ir_swizzle *);
virtual void visit(ir_dereference *);
virtual void visit(ir_assignment *);
virtual void visit(ir_constant *);
virtual void visit(ir_call *);
virtual void visit(ir_return *);
virtual void visit(ir_if *);
/*@}*/
void (*callback)(ir_instruction *ir, void *data);
void *data;
};
void
ir_tree_visitor::visit(ir_variable *ir)
{
this->callback(ir, this->data);
}
void
ir_tree_visitor::visit(ir_loop *ir)
{
this->callback(ir, this->data);
visit_exec_list(&ir->body_instructions, this);
if (ir->from)
ir->from->accept(this);
if (ir->to)
ir->to->accept(this);
if (ir->increment)
ir->increment->accept(this);
}
void
ir_tree_visitor::visit(ir_loop_jump *ir)
{
this->callback(ir, this->data);
}
void
ir_tree_visitor::visit(ir_function_signature *ir)
{
this->callback(ir, this->data);
visit_exec_list(&ir->body, this);
}
void
ir_tree_visitor::visit(ir_function *ir)
{
this->callback(ir, this->data);
/* FINISHME: Do we want to walk into functions? */
}
void
ir_tree_visitor::visit(ir_expression *ir)
{
unsigned int operand;
this->callback(ir, this->data);
for (operand = 0; operand < ir->get_num_operands(); operand++) {
ir->operands[operand]->accept(this);
}
}
void
ir_tree_visitor::visit(ir_swizzle *ir)
{
this->callback(ir, this->data);
ir->val->accept(this);
}
void
ir_tree_visitor::visit(ir_dereference *ir)
{
this->callback(ir, this->data);
if (ir->mode == ir_dereference::ir_reference_array) {
ir->selector.array_index->accept(this);
}
}
void
ir_tree_visitor::visit(ir_assignment *ir)
{
this->callback(ir, this->data);
ir->lhs->accept(this);
ir->rhs->accept(this);
if (ir->condition)
ir->condition->accept(this);
}
void
ir_tree_visitor::visit(ir_constant *ir)
{
this->callback(ir, this->data);
}
void
ir_tree_visitor::visit(ir_call *ir)
{
this->callback(ir, this->data);
foreach_iter(exec_list_iterator, iter, *ir) {
ir_rvalue *param = (ir_rvalue *)iter.get();
param->accept(this);
}
}
void
ir_tree_visitor::visit(ir_return *ir)
{
ir_rvalue *val = ir->get_value();
this->callback(ir, this->data);
if (val)
val->accept(this);
}
void
ir_tree_visitor::visit(ir_if *ir)
{
this->callback(ir, this->data);
ir->condition->accept(this);
visit_exec_list(&ir->then_instructions, this);
visit_exec_list(&ir->else_instructions, this);
}
void ir_visit_tree(ir_instruction *ir,
void (*callback)(ir_instruction *ir,
void *data),
void *data)
{
ir_tree_visitor v;
v.callback = callback;
v.data = data;
ir->accept(&v);
}

27
ir_visit_tree.h Normal file
View File

@ -0,0 +1,27 @@
/*
* 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.
*/
void ir_visit_tree(ir_instruction *ir,
void (*callback)(ir_instruction *ir,
void *data),
void *data);