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 <stdio.h>
|
|
|
|
#include "ir.h"
|
|
|
|
#include "ir_visitor.h"
|
2010-05-05 09:26:46 -07:00
|
|
|
#include "ir_print_visitor.h"
|
2010-05-04 13:04:40 -07:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 *);
|
2010-05-19 13:20:12 +02:00
|
|
|
virtual void visit(ir_dereference_variable *);
|
|
|
|
virtual void visit(ir_dereference_array *);
|
|
|
|
virtual void visit(ir_dereference_record *);
|
2010-05-04 13:04:40 -07:00
|
|
|
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
|
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-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-19 13:20:12 +02:00
|
|
|
void
|
|
|
|
ir_copy_propagation_visitor::visit(ir_dereference_array *ir)
|
|
|
|
{
|
2010-05-19 13:52:29 +02:00
|
|
|
ir->array->accept(this);
|
|
|
|
ir->array_index->accept(this);
|
2010-05-19 13:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ir_copy_propagation_visitor::visit(ir_dereference_record *ir)
|
|
|
|
{
|
2010-05-19 13:52:29 +02:00
|
|
|
ir->record->accept(this);
|
2010-05-19 13:20:12 +02:00
|
|
|
}
|
|
|
|
|
2010-05-04 13:04:40 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-26 11:32:52 -07:00
|
|
|
ir_variable *lhs_var = ir->lhs->whole_variable_referenced();
|
|
|
|
ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
|
|
|
|
|
|
|
|
if ((lhs_var != NULL) && (rhs_var != NULL)) {
|
|
|
|
entry = new acp_entry(lhs_var, rhs_var);
|
|
|
|
acp->push_tail(entry);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
add_copy(ir_assign, &acp);
|
|
|
|
}
|
|
|
|
if (ir == last)
|
|
|
|
break;
|
|
|
|
}
|
2010-05-05 09:31:53 -07:00
|
|
|
*out_progress = progress;
|
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;
|
|
|
|
}
|