2010-05-04 13:04:40 -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-05-05 09:38:09 -07:00
|
|
|
* \file ir_copy_propagation.cpp
|
2010-05-04 13:04:40 -07:00
|
|
|
*
|
2010-05-05 09:38:09 -07:00
|
|
|
* Moves usage of recently-copied variables to the previous copy of
|
|
|
|
* the variable within basic blocks.
|
|
|
|
*
|
|
|
|
* 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.
|
2010-05-04 13:04:40 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ir.h"
|
|
|
|
#include "ir_visitor.h"
|
|
|
|
#include "ir_basic_block.h"
|
2010-05-05 11:45:30 -07:00
|
|
|
#include "ir_optimization.h"
|
2010-05-04 13:04:40 -07:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
|
2010-05-04 13:04:40 -07:00
|
|
|
public:
|
|
|
|
ir_copy_propagation_visitor(exec_list *acp)
|
|
|
|
{
|
|
|
|
progress = false;
|
2010-05-26 17:04:19 -07:00
|
|
|
in_lhs = false;
|
2010-05-04 13:04:40 -07:00
|
|
|
this->acp = acp;
|
|
|
|
}
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
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_enter(class ir_assignment *);
|
|
|
|
virtual ir_visitor_status visit_enter(class ir_call *);
|
|
|
|
virtual ir_visitor_status visit_enter(class ir_if *);
|
2010-05-04 13:04:40 -07:00
|
|
|
|
|
|
|
/** List of acp_entry */
|
|
|
|
exec_list *acp;
|
|
|
|
bool progress;
|
2010-05-26 17:04:19 -07:00
|
|
|
|
|
|
|
/** Currently in the LHS of an assignment? */
|
|
|
|
bool in_lhs;
|
2010-05-04 13:04:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
ir_visitor_status
|
|
|
|
ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
|
|
|
(void)ir;
|
2010-05-26 17:04:19 -07:00
|
|
|
return visit_continue_with_parent;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
ir_visitor_status
|
|
|
|
ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
|
|
|
(void)ir;
|
2010-05-26 17:04:19 -07:00
|
|
|
return visit_continue_with_parent;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
ir_visitor_status
|
|
|
|
ir_copy_propagation_visitor::visit_enter(ir_assignment *ir)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
2010-07-12 15:32:37 -07:00
|
|
|
ir_visitor_status s;
|
|
|
|
|
|
|
|
/* Inline the rest of ir_assignment::accept(ir_hv *v), wrapping the
|
|
|
|
* LHS part with setting in_lhs so that we can avoid copy
|
|
|
|
* propagating into the LHS.
|
|
|
|
*
|
|
|
|
* Note that this means we won't copy propagate into the derefs of
|
|
|
|
* an array index. Oh well.
|
|
|
|
*/
|
2010-05-26 17:04:19 -07:00
|
|
|
this->in_lhs = true;
|
2010-07-12 15:32:37 -07:00
|
|
|
s = ir->lhs->accept(this);
|
|
|
|
this->in_lhs = false;
|
|
|
|
if (s != visit_continue)
|
|
|
|
return (s == visit_continue_with_parent) ? visit_continue : s;
|
|
|
|
|
|
|
|
s = ir->rhs->accept(this);
|
|
|
|
if (s != visit_continue)
|
|
|
|
return (s == visit_continue_with_parent) ? visit_continue : s;
|
|
|
|
|
|
|
|
if (ir->condition)
|
|
|
|
s = ir->condition->accept(this);
|
|
|
|
|
|
|
|
return (s == visit_stop) ? s : visit_continue_with_parent;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
ir_visitor_status
|
|
|
|
ir_copy_propagation_visitor::visit_enter(ir_function *ir)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
|
|
|
(void) ir;
|
2010-05-26 17:04:19 -07:00
|
|
|
return visit_continue_with_parent;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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!
|
|
|
|
*/
|
2010-05-26 17:04:19 -07:00
|
|
|
ir_visitor_status
|
2010-05-19 13:20:12 +02:00
|
|
|
ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
2010-05-26 17:04:19 -07:00
|
|
|
/* Ignores the LHS. Don't want to rewrite the LHS to point at some
|
|
|
|
* other storage!
|
|
|
|
*/
|
|
|
|
if (this->in_lhs) {
|
|
|
|
return visit_continue;
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:20:12 +02:00
|
|
|
ir_variable *var = ir->variable_referenced();
|
2010-05-04 13:04:40 -07:00
|
|
|
|
2010-05-19 13:20:12 +02:00
|
|
|
foreach_iter(exec_list_iterator, iter, *this->acp) {
|
|
|
|
acp_entry *entry = (acp_entry *)iter.get();
|
2010-05-04 13:04:40 -07:00
|
|
|
|
2010-05-19 13:20:12 +02:00
|
|
|
if (var == entry->lhs) {
|
|
|
|
ir->var = entry->rhs;
|
|
|
|
this->progress = true;
|
|
|
|
break;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
return visit_continue;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
ir_visitor_status
|
|
|
|
ir_copy_propagation_visitor::visit_enter(ir_call *ir)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
|
|
|
(void)ir;
|
|
|
|
|
|
|
|
/* Note, if we were to do copy propagation to parameters of calls, we'd
|
|
|
|
* have to be careful about out params.
|
|
|
|
*/
|
2010-05-26 17:04:19 -07:00
|
|
|
return visit_continue_with_parent;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-26 17:04:19 -07:00
|
|
|
ir_visitor_status
|
|
|
|
ir_copy_propagation_visitor::visit_enter(ir_if *ir)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
|
|
|
ir->condition->accept(this);
|
2010-05-26 17:04:19 -07:00
|
|
|
|
|
|
|
/* Do not traverse into the body of the if-statement since that is a
|
|
|
|
* different basic block.
|
|
|
|
*/
|
|
|
|
return visit_continue_with_parent;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 09:31:53 -07:00
|
|
|
static bool
|
2010-05-04 13:04:40 -07:00
|
|
|
propagate_copies(ir_instruction *ir, exec_list *acp)
|
|
|
|
{
|
|
|
|
ir_copy_propagation_visitor v(acp);
|
|
|
|
|
|
|
|
ir->accept(&v);
|
2010-05-05 09:31:53 -07:00
|
|
|
|
|
|
|
return v.progress;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kill_invalidated_copies(ir_assignment *ir, exec_list *acp)
|
|
|
|
{
|
2010-05-14 17:36:00 -07:00
|
|
|
ir_variable *var = ir->lhs->variable_referenced();
|
|
|
|
assert(var != NULL);
|
2010-05-04 13:04:40 -07:00
|
|
|
|
2010-05-14 17:36:00 -07:00
|
|
|
foreach_iter(exec_list_iterator, iter, *acp) {
|
|
|
|
acp_entry *entry = (acp_entry *)iter.get();
|
|
|
|
|
|
|
|
if (entry->lhs == var || entry->rhs == var) {
|
|
|
|
entry->remove();
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an entry to the available copy list if it's a plain assignment
|
|
|
|
* of a variable to a variable.
|
|
|
|
*/
|
2010-08-04 23:23:15 -07:00
|
|
|
static bool
|
2010-06-25 13:44:09 -07:00
|
|
|
add_copy(void *ctx, ir_assignment *ir, exec_list *acp)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
|
|
|
acp_entry *entry;
|
|
|
|
|
|
|
|
if (ir->condition) {
|
|
|
|
ir_constant *condition = ir->condition->as_constant();
|
|
|
|
if (!condition || !condition->value.b[0])
|
2010-08-04 23:23:15 -07:00
|
|
|
return false;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
2010-08-02 18:48:25 -07:00
|
|
|
ir_variable *lhs_var = ir->whole_variable_written();
|
2010-05-26 11:32:52 -07:00
|
|
|
ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
|
|
|
|
|
|
|
|
if ((lhs_var != NULL) && (rhs_var != NULL)) {
|
2010-08-04 23:23:15 -07:00
|
|
|
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);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
entry = new(ctx) acp_entry(lhs_var, rhs_var);
|
|
|
|
acp->push_tail(entry);
|
|
|
|
}
|
2010-05-26 11:32:52 -07:00
|
|
|
}
|
2010-08-04 23:23:15 -07:00
|
|
|
|
|
|
|
return false;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
copy_propagation_basic_block(ir_instruction *first,
|
2010-05-05 09:31:53 -07:00
|
|
|
ir_instruction *last,
|
|
|
|
void *data)
|
2010-05-04 13:04:40 -07:00
|
|
|
{
|
|
|
|
ir_instruction *ir;
|
|
|
|
/* List of avaialble_copy */
|
|
|
|
exec_list acp;
|
2010-05-05 09:31:53 -07:00
|
|
|
bool *out_progress = (bool *)data;
|
|
|
|
bool progress = false;
|
2010-05-04 13:04:40 -07:00
|
|
|
|
2010-06-25 13:36:14 -07:00
|
|
|
void *ctx = talloc_new(NULL);
|
2010-05-04 13:04:40 -07:00
|
|
|
for (ir = first;; ir = (ir_instruction *)ir->next) {
|
|
|
|
ir_assignment *ir_assign = ir->as_assignment();
|
|
|
|
|
2010-05-05 09:31:53 -07:00
|
|
|
progress = propagate_copies(ir, &acp) || progress;
|
2010-05-04 13:04:40 -07:00
|
|
|
|
|
|
|
if (ir_assign) {
|
|
|
|
kill_invalidated_copies(ir_assign, &acp);
|
|
|
|
|
2010-08-04 23:23:15 -07:00
|
|
|
progress = add_copy(ctx, ir_assign, &acp) || progress;
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
if (ir == last)
|
|
|
|
break;
|
|
|
|
}
|
2010-05-05 09:31:53 -07:00
|
|
|
*out_progress = progress;
|
2010-06-25 13:44:09 -07:00
|
|
|
talloc_free(ctx);
|
2010-05-04 13:04:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does a copy propagation pass on the code present in the instruction stream.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
do_copy_propagation(exec_list *instructions)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
|
2010-05-05 09:31:53 -07:00
|
|
|
call_for_basic_blocks(instructions, copy_propagation_basic_block, &progress);
|
2010-05-04 13:04:40 -07:00
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|