2010-05-05 10:37:25 -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 opt_dead_code_local.cpp
|
2010-05-05 10:37:25 -07:00
|
|
|
*
|
|
|
|
|
* Eliminates local dead assignments from the code.
|
|
|
|
|
*
|
|
|
|
|
* This operates on basic blocks, tracking assignments and finding if
|
|
|
|
|
* they're used before the variable is completely reassigned.
|
|
|
|
|
*
|
|
|
|
|
* Compare this to ir_dead_code.cpp, which operates globally looking
|
|
|
|
|
* for assignments to variables that are never read.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ir.h"
|
|
|
|
|
#include "ir_basic_block.h"
|
|
|
|
|
#include "ir_optimization.h"
|
|
|
|
|
#include "glsl_types.h"
|
|
|
|
|
|
|
|
|
|
static bool debug = false;
|
|
|
|
|
|
2012-05-29 16:18:37 -07:00
|
|
|
namespace {
|
|
|
|
|
|
2010-05-05 10:37:25 -07:00
|
|
|
class assignment_entry : public exec_node
|
|
|
|
|
{
|
|
|
|
|
public:
|
2012-02-23 11:51:04 -08:00
|
|
|
assignment_entry(ir_variable *lhs, ir_assignment *ir)
|
2010-05-05 10:37:25 -07:00
|
|
|
{
|
|
|
|
|
assert(lhs);
|
|
|
|
|
assert(ir);
|
|
|
|
|
this->lhs = lhs;
|
|
|
|
|
this->ir = ir;
|
2012-02-23 11:51:04 -08:00
|
|
|
this->available = ir->write_mask;
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir_variable *lhs;
|
2012-02-23 11:51:04 -08:00
|
|
|
ir_assignment *ir;
|
|
|
|
|
|
|
|
|
|
/* bitmask of xyzw channels written that haven't been used so far. */
|
|
|
|
|
int available;
|
2010-05-05 10:37:25 -07:00
|
|
|
};
|
|
|
|
|
|
2010-05-14 14:31:25 -07:00
|
|
|
class kill_for_derefs_visitor : public ir_hierarchical_visitor {
|
|
|
|
|
public:
|
|
|
|
|
kill_for_derefs_visitor(exec_list *assignments)
|
|
|
|
|
{
|
|
|
|
|
this->assignments = assignments;
|
|
|
|
|
}
|
2010-05-05 10:37:25 -07:00
|
|
|
|
2012-02-23 11:51:04 -08:00
|
|
|
void kill_channels(ir_variable *const var, int used)
|
2010-05-14 14:31:25 -07:00
|
|
|
{
|
2013-11-22 02:10:15 -08:00
|
|
|
foreach_list_safe(n, this->assignments) {
|
|
|
|
|
assignment_entry *entry = (assignment_entry *) n;
|
2010-05-05 10:37:25 -07:00
|
|
|
|
2010-05-14 14:31:25 -07:00
|
|
|
if (entry->lhs == var) {
|
2012-02-23 11:51:04 -08:00
|
|
|
if (var->type->is_scalar() || var->type->is_vector()) {
|
|
|
|
|
if (debug)
|
|
|
|
|
printf("kill %s (0x%01x - 0x%01x)\n", entry->lhs->name,
|
|
|
|
|
entry->available, used);
|
|
|
|
|
entry->available &= ~used;
|
|
|
|
|
if (!entry->available)
|
|
|
|
|
entry->remove();
|
|
|
|
|
} else {
|
|
|
|
|
if (debug)
|
|
|
|
|
printf("kill %s\n", entry->lhs->name);
|
|
|
|
|
entry->remove();
|
|
|
|
|
}
|
2010-05-14 14:31:25 -07:00
|
|
|
}
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
2012-02-23 11:51:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ir_visitor_status visit(ir_dereference_variable *ir)
|
|
|
|
|
{
|
|
|
|
|
kill_channels(ir->var, ~0);
|
2010-05-14 14:31:25 -07:00
|
|
|
|
|
|
|
|
return visit_continue;
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
2012-02-23 11:51:04 -08:00
|
|
|
virtual ir_visitor_status visit(ir_swizzle *ir)
|
|
|
|
|
{
|
|
|
|
|
ir_dereference_variable *deref = ir->val->as_dereference_variable();
|
|
|
|
|
if (!deref)
|
|
|
|
|
return visit_continue;
|
|
|
|
|
|
|
|
|
|
int used = 0;
|
|
|
|
|
used |= 1 << ir->mask.x;
|
|
|
|
|
used |= 1 << ir->mask.y;
|
|
|
|
|
used |= 1 << ir->mask.z;
|
|
|
|
|
used |= 1 << ir->mask.w;
|
|
|
|
|
|
|
|
|
|
kill_channels(deref->var, used);
|
|
|
|
|
|
|
|
|
|
return visit_continue_with_parent;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-15 09:26:35 -06:00
|
|
|
virtual ir_visitor_status visit(ir_emit_vertex *ir)
|
|
|
|
|
{
|
|
|
|
|
/* For the purpose of dead code elimination, emitting a vertex counts as
|
|
|
|
|
* "reading" all of the currently assigned output variables.
|
|
|
|
|
*/
|
2013-11-22 02:10:15 -08:00
|
|
|
foreach_list_safe(n, this->assignments) {
|
|
|
|
|
assignment_entry *entry = (assignment_entry *) n;
|
2013-12-12 13:51:01 +02:00
|
|
|
if (entry->lhs->data.mode == ir_var_shader_out) {
|
2013-02-15 09:26:35 -06:00
|
|
|
if (debug)
|
|
|
|
|
printf("kill %s\n", entry->lhs->name);
|
|
|
|
|
entry->remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-14 14:31:25 -07:00
|
|
|
private:
|
|
|
|
|
exec_list *assignments;
|
|
|
|
|
};
|
2010-05-05 10:37:25 -07:00
|
|
|
|
2010-05-19 13:47:39 +02:00
|
|
|
class array_index_visit : public ir_hierarchical_visitor {
|
|
|
|
|
public:
|
|
|
|
|
array_index_visit(ir_hierarchical_visitor *v)
|
|
|
|
|
{
|
|
|
|
|
this->visitor = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ir_visitor_status visit_enter(class ir_dereference_array *ir)
|
|
|
|
|
{
|
2010-05-19 13:52:29 +02:00
|
|
|
ir->array_index->accept(visitor);
|
2010-05-19 13:47:39 +02:00
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void run(ir_instruction *ir, ir_hierarchical_visitor *v)
|
|
|
|
|
{
|
|
|
|
|
array_index_visit top_visit(v);
|
|
|
|
|
ir->accept(& top_visit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir_hierarchical_visitor *visitor;
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-29 16:18:37 -07:00
|
|
|
} /* unnamed namespace */
|
2010-05-19 13:47:39 +02:00
|
|
|
|
2010-05-05 10:37:25 -07:00
|
|
|
/**
|
|
|
|
|
* Adds an entry to the available copy list if it's a plain assignment
|
|
|
|
|
* of a variable to a variable.
|
|
|
|
|
*/
|
|
|
|
|
static bool
|
2010-06-25 13:36:14 -07:00
|
|
|
process_assignment(void *ctx, ir_assignment *ir, exec_list *assignments)
|
2010-05-05 10:37:25 -07:00
|
|
|
{
|
|
|
|
|
ir_variable *var = NULL;
|
|
|
|
|
bool progress = false;
|
2010-05-14 14:31:25 -07:00
|
|
|
kill_for_derefs_visitor v(assignments);
|
2010-05-05 10:37:25 -07:00
|
|
|
|
|
|
|
|
/* Kill assignment entries for things used to produce this assignment. */
|
2010-05-14 14:31:25 -07:00
|
|
|
ir->rhs->accept(&v);
|
2010-05-05 10:37:25 -07:00
|
|
|
if (ir->condition) {
|
2010-05-14 14:31:25 -07:00
|
|
|
ir->condition->accept(&v);
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-19 13:47:39 +02:00
|
|
|
/* Kill assignment enties used as array indices.
|
2010-05-05 10:37:25 -07:00
|
|
|
*/
|
2010-05-19 13:47:39 +02:00
|
|
|
array_index_visit::run(ir->lhs, &v);
|
|
|
|
|
var = ir->lhs->variable_referenced();
|
2010-05-05 10:37:25 -07:00
|
|
|
assert(var);
|
|
|
|
|
|
|
|
|
|
/* Now, check if we did a whole-variable assignment. */
|
2012-02-23 11:51:04 -08:00
|
|
|
if (!ir->condition) {
|
|
|
|
|
ir_dereference_variable *deref_var = ir->lhs->as_dereference_variable();
|
|
|
|
|
|
|
|
|
|
/* If it's a vector type, we can do per-channel elimination of
|
|
|
|
|
* use of the RHS.
|
2010-05-05 10:37:25 -07:00
|
|
|
*/
|
2012-02-23 11:51:04 -08:00
|
|
|
if (deref_var && (deref_var->var->type->is_scalar() ||
|
|
|
|
|
deref_var->var->type->is_vector())) {
|
2010-05-05 10:37:25 -07:00
|
|
|
|
2012-02-23 11:51:04 -08:00
|
|
|
if (debug)
|
|
|
|
|
printf("looking for %s.0x%01x to remove\n", var->name,
|
|
|
|
|
ir->write_mask);
|
|
|
|
|
|
2013-11-22 02:10:15 -08:00
|
|
|
foreach_list_safe(n, assignments) {
|
|
|
|
|
assignment_entry *entry = (assignment_entry *) n;
|
2012-02-23 11:51:04 -08:00
|
|
|
|
|
|
|
|
if (entry->lhs != var)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
int remove = entry->available & ir->write_mask;
|
|
|
|
|
if (debug) {
|
|
|
|
|
printf("%s 0x%01x - 0x%01x = 0x%01x\n",
|
|
|
|
|
var->name,
|
|
|
|
|
entry->ir->write_mask,
|
|
|
|
|
remove, entry->ir->write_mask & ~remove);
|
|
|
|
|
}
|
|
|
|
|
if (remove) {
|
|
|
|
|
progress = true;
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
|
printf("rewriting:\n ");
|
|
|
|
|
entry->ir->print();
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry->ir->write_mask &= ~remove;
|
|
|
|
|
entry->available &= ~remove;
|
|
|
|
|
if (entry->ir->write_mask == 0) {
|
|
|
|
|
/* Delete the dead assignment. */
|
|
|
|
|
entry->ir->remove();
|
|
|
|
|
entry->remove();
|
|
|
|
|
} else {
|
|
|
|
|
void *mem_ctx = ralloc_parent(entry->ir);
|
|
|
|
|
/* Reswizzle the RHS arguments according to the new
|
|
|
|
|
* write_mask.
|
|
|
|
|
*/
|
|
|
|
|
unsigned components[4];
|
|
|
|
|
unsigned channels = 0;
|
|
|
|
|
unsigned next = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if ((entry->ir->write_mask | remove) & (1 << i)) {
|
|
|
|
|
if (!(remove & (1 << i)))
|
|
|
|
|
components[channels++] = next;
|
|
|
|
|
next++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry->ir->rhs = new(mem_ctx) ir_swizzle(entry->ir->rhs,
|
|
|
|
|
components,
|
|
|
|
|
channels);
|
|
|
|
|
if (debug) {
|
|
|
|
|
printf("to:\n ");
|
|
|
|
|
entry->ir->print();
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (ir->whole_variable_written() != NULL) {
|
|
|
|
|
/* We did a whole-variable assignment. So, any instruction in
|
|
|
|
|
* the assignment list with the same LHS is dead.
|
|
|
|
|
*/
|
|
|
|
|
if (debug)
|
|
|
|
|
printf("looking for %s to remove\n", var->name);
|
2013-11-22 02:10:15 -08:00
|
|
|
foreach_list_safe(n, assignments) {
|
|
|
|
|
assignment_entry *entry = (assignment_entry *) n;
|
2012-02-23 11:51:04 -08:00
|
|
|
|
|
|
|
|
if (entry->lhs == var) {
|
|
|
|
|
if (debug)
|
|
|
|
|
printf("removing %s\n", var->name);
|
|
|
|
|
entry->ir->remove();
|
|
|
|
|
entry->remove();
|
|
|
|
|
progress = true;
|
|
|
|
|
}
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
glsl: Convert ir_call to be a statement rather than a value.
Aside from ir_call, our IR is cleanly split into two classes:
- Statements (typeless; used for side effects, control flow)
- Values (deeply nestable, pure, typed expression trees)
Unfortunately, ir_call confused all this:
- For void functions, we placed ir_call directly in the instruction
stream, treating it as an untyped statement. Yet, it was a subclass
of ir_rvalue, and no other ir_rvalue could be used in this way.
- For functions with a return value, ir_call could be placed in
arbitrary expression trees. While this fit naturally with the source
language, it meant that expressions might not be pure, making it
difficult to transform and optimize them. To combat this, we always
emitted ir_call directly in the RHS of an ir_assignment, only using
a temporary variable in expression trees. Many passes relied on this
assumption; the acos and atan built-ins violated it.
This patch makes ir_call a statement (ir_instruction) rather than a
value (ir_rvalue). Non-void calls now take a ir_dereference of a
variable, and store the return value there---effectively a call and
assignment rolled into one. They cannot be embedded in expressions.
All expression trees are now pure, without exception.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2012-03-20 15:56:37 -07:00
|
|
|
/* Add this instruction to the assignment list available to be removed. */
|
2010-06-23 18:11:51 -07:00
|
|
|
assignment_entry *entry = new(ctx) assignment_entry(var, ir);
|
2010-05-05 10:37:25 -07:00
|
|
|
assignments->push_tail(entry);
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
|
printf("add %s\n", var->name);
|
|
|
|
|
|
|
|
|
|
printf("current entries\n");
|
2013-11-22 01:25:42 -08:00
|
|
|
foreach_list(n, assignments) {
|
|
|
|
|
assignment_entry *entry = (assignment_entry *) n;
|
2010-05-05 10:37:25 -07:00
|
|
|
|
2012-02-23 11:51:04 -08:00
|
|
|
printf(" %s (0x%01x)\n", entry->lhs->name, entry->available);
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
dead_code_local_basic_block(ir_instruction *first,
|
|
|
|
|
ir_instruction *last,
|
|
|
|
|
void *data)
|
|
|
|
|
{
|
|
|
|
|
ir_instruction *ir, *ir_next;
|
|
|
|
|
/* List of avaialble_copy */
|
|
|
|
|
exec_list assignments;
|
|
|
|
|
bool *out_progress = (bool *)data;
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
void *ctx = ralloc_context(NULL);
|
2010-05-05 10:37:25 -07:00
|
|
|
/* Safe looping, since process_assignment */
|
|
|
|
|
for (ir = first, ir_next = (ir_instruction *)first->next;;
|
|
|
|
|
ir = ir_next, ir_next = (ir_instruction *)ir->next) {
|
|
|
|
|
ir_assignment *ir_assign = ir->as_assignment();
|
|
|
|
|
|
|
|
|
|
if (debug) {
|
2010-06-22 12:09:21 -07:00
|
|
|
ir->print();
|
2010-05-05 10:37:25 -07:00
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ir_assign) {
|
2010-06-25 13:36:14 -07:00
|
|
|
progress = process_assignment(ctx, ir_assign, &assignments) || progress;
|
2010-05-05 10:37:25 -07:00
|
|
|
} else {
|
2010-05-14 14:31:25 -07:00
|
|
|
kill_for_derefs_visitor kill(&assignments);
|
|
|
|
|
ir->accept(&kill);
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ir == last)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*out_progress = progress;
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free(ctx);
|
2010-05-05 10:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does a copy propagation pass on the code present in the instruction stream.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
do_dead_code_local(exec_list *instructions)
|
|
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
|
|
call_for_basic_blocks(instructions, dead_code_local_basic_block, &progress);
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|