2010-05-12 20:10:41 +01: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 18:43:10 +00:00
|
|
|
* \file opt_constant_variable.cpp
|
2010-05-12 20:10:41 +01:00
|
|
|
*
|
|
|
|
* Marks variables assigned a single constant value over the course
|
|
|
|
* of the program as constant.
|
|
|
|
*
|
|
|
|
* The goal here is to trigger further constant folding and then dead
|
|
|
|
* code elimination. This is common with vector/matrix constructors
|
|
|
|
* and calls to builtin functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ir.h"
|
|
|
|
#include "ir_visitor.h"
|
|
|
|
#include "ir_optimization.h"
|
|
|
|
#include "glsl_types.h"
|
2015-09-05 08:22:57 +01:00
|
|
|
#include "util/hash_table.h"
|
2010-05-12 20:10:41 +01:00
|
|
|
|
2012-05-30 00:18:37 +01:00
|
|
|
namespace {
|
|
|
|
|
2010-05-12 20:10:41 +01:00
|
|
|
struct assignment_entry {
|
|
|
|
int assignment_count;
|
|
|
|
ir_variable *var;
|
|
|
|
ir_constant *constval;
|
2010-07-29 22:54:01 +01:00
|
|
|
bool our_scope;
|
2010-05-12 20:10:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class ir_constant_variable_visitor : public ir_hierarchical_visitor {
|
|
|
|
public:
|
2010-07-29 22:54:01 +01:00
|
|
|
virtual ir_visitor_status visit_enter(ir_dereference_variable *);
|
|
|
|
virtual ir_visitor_status visit(ir_variable *);
|
2010-05-12 20:10:41 +01:00
|
|
|
virtual ir_visitor_status visit_enter(ir_assignment *);
|
2010-07-20 22:21:43 +01:00
|
|
|
virtual ir_visitor_status visit_enter(ir_call *);
|
2010-05-12 20:10:41 +01:00
|
|
|
|
2015-09-05 08:22:57 +01:00
|
|
|
struct hash_table *ht;
|
2010-05-12 20:10:41 +01:00
|
|
|
};
|
|
|
|
|
2012-05-30 00:18:37 +01:00
|
|
|
} /* unnamed namespace */
|
|
|
|
|
2010-05-12 20:10:41 +01:00
|
|
|
static struct assignment_entry *
|
2015-09-05 08:22:57 +01:00
|
|
|
get_assignment_entry(ir_variable *var, struct hash_table *ht)
|
2010-05-12 20:10:41 +01:00
|
|
|
{
|
2015-09-05 08:22:57 +01:00
|
|
|
struct hash_entry *hte = _mesa_hash_table_search(ht, var);
|
2010-05-12 20:10:41 +01:00
|
|
|
struct assignment_entry *entry;
|
|
|
|
|
2015-09-05 08:22:57 +01:00
|
|
|
if (hte) {
|
|
|
|
entry = (struct assignment_entry *) hte->data;
|
|
|
|
} else {
|
|
|
|
entry = (struct assignment_entry *) calloc(1, sizeof(*entry));
|
|
|
|
entry->var = var;
|
|
|
|
_mesa_hash_table_insert(ht, var, entry);
|
2010-05-12 20:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2010-07-29 22:54:01 +01:00
|
|
|
ir_visitor_status
|
|
|
|
ir_constant_variable_visitor::visit(ir_variable *ir)
|
|
|
|
{
|
2015-09-05 08:22:57 +01:00
|
|
|
struct assignment_entry *entry = get_assignment_entry(ir, this->ht);
|
2010-07-29 22:54:01 +01:00
|
|
|
entry->our_scope = true;
|
|
|
|
return visit_continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip derefs of variables so that we can detect declarations. */
|
|
|
|
ir_visitor_status
|
|
|
|
ir_constant_variable_visitor::visit_enter(ir_dereference_variable *ir)
|
|
|
|
{
|
|
|
|
(void)ir;
|
|
|
|
return visit_continue_with_parent;
|
|
|
|
}
|
|
|
|
|
2010-05-12 20:10:41 +01:00
|
|
|
ir_visitor_status
|
|
|
|
ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
|
|
|
|
{
|
|
|
|
ir_constant *constval;
|
|
|
|
struct assignment_entry *entry;
|
|
|
|
|
2015-09-05 08:22:57 +01:00
|
|
|
entry = get_assignment_entry(ir->lhs->variable_referenced(), this->ht);
|
2010-05-12 20:10:41 +01:00
|
|
|
assert(entry);
|
|
|
|
entry->assignment_count++;
|
|
|
|
|
|
|
|
/* If it's already constant, don't do the work. */
|
|
|
|
if (entry->var->constant_value)
|
|
|
|
return visit_continue;
|
|
|
|
|
|
|
|
/* OK, now find if we actually have all the right conditions for
|
|
|
|
* this to be a constant value assigned to the var.
|
|
|
|
*/
|
2011-01-29 21:59:14 +00:00
|
|
|
if (ir->condition)
|
|
|
|
return visit_continue;
|
2010-05-12 20:10:41 +01:00
|
|
|
|
2010-08-03 02:48:25 +01:00
|
|
|
ir_variable *var = ir->whole_variable_written();
|
2010-05-12 20:10:41 +01:00
|
|
|
if (!var)
|
|
|
|
return visit_continue;
|
|
|
|
|
2015-04-24 10:15:48 +01:00
|
|
|
/* Ignore buffer variables, since the underlying storage is shared
|
|
|
|
* and we can't be sure that this variable won't be written by another
|
|
|
|
* thread.
|
|
|
|
*/
|
|
|
|
if (var->data.mode == ir_var_shader_storage)
|
|
|
|
return visit_continue;
|
|
|
|
|
2010-05-12 20:10:41 +01:00
|
|
|
constval = ir->rhs->constant_expression_value();
|
|
|
|
if (!constval)
|
|
|
|
return visit_continue;
|
|
|
|
|
|
|
|
/* Mark this entry as having a constant assignment (if the
|
|
|
|
* assignment count doesn't go >1). do_constant_variable will fix
|
|
|
|
* up the variable with the constant value later.
|
|
|
|
*/
|
|
|
|
entry->constval = constval;
|
|
|
|
|
|
|
|
return visit_continue;
|
|
|
|
}
|
|
|
|
|
2010-07-20 22:21:43 +01:00
|
|
|
ir_visitor_status
|
|
|
|
ir_constant_variable_visitor::visit_enter(ir_call *ir)
|
|
|
|
{
|
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 22:56:37 +00:00
|
|
|
/* Mark any out parameters as assigned to */
|
glsl: Use a new foreach_two_lists macro for walking two lists at once.
When handling function calls, we often want to walk through the list of
formal parameters and list of actual parameters at the same time.
(Both are guaranteed to be the same length.)
Previously, we used a pattern of:
exec_list_iterator 1st_iter = <1st list>.iterator();
foreach_iter(exec_list_iterator, 2nd_iter, <2nd list>) {
...
1st_iter.next();
}
This was awkward, since you had to manually iterate through one of
the two lists.
This patch introduces a foreach_two_lists macro which safely walks
through two lists at the same time, so you can simply do:
foreach_two_lists(1st_node, <1st list>, 2nd_node, <2nd list>) {
...
}
v2: Rename macro from foreach_list2 to foreach_two_lists, as suggested
by Ian Romanick.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2014-01-11 00:39:17 +00:00
|
|
|
foreach_two_lists(formal_node, &ir->callee->parameters,
|
|
|
|
actual_node, &ir->actual_parameters) {
|
|
|
|
ir_rvalue *param_rval = (ir_rvalue *) actual_node;
|
|
|
|
ir_variable *param = (ir_variable *) formal_node;
|
2010-07-20 22:21:43 +01:00
|
|
|
|
2013-12-12 11:51:01 +00:00
|
|
|
if (param->data.mode == ir_var_function_out ||
|
|
|
|
param->data.mode == ir_var_function_inout) {
|
2010-07-20 22:21:43 +01:00
|
|
|
ir_variable *var = param_rval->variable_referenced();
|
|
|
|
struct assignment_entry *entry;
|
|
|
|
|
|
|
|
assert(var);
|
2015-09-05 08:22:57 +01:00
|
|
|
entry = get_assignment_entry(var, this->ht);
|
2010-07-20 22:21:43 +01:00
|
|
|
entry->assignment_count++;
|
|
|
|
}
|
|
|
|
}
|
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 22:56:37 +00:00
|
|
|
|
|
|
|
/* Mark the return storage as having been assigned to */
|
|
|
|
if (ir->return_deref != NULL) {
|
|
|
|
ir_variable *var = ir->return_deref->variable_referenced();
|
|
|
|
struct assignment_entry *entry;
|
|
|
|
|
|
|
|
assert(var);
|
2015-09-05 08:22:57 +01:00
|
|
|
entry = get_assignment_entry(var, this->ht);
|
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 22:56:37 +00:00
|
|
|
entry->assignment_count++;
|
|
|
|
}
|
|
|
|
|
2010-07-20 22:21:43 +01:00
|
|
|
return visit_continue;
|
|
|
|
}
|
|
|
|
|
2010-05-12 20:10:41 +01:00
|
|
|
/**
|
|
|
|
* Does a copy propagation pass on the code present in the instruction stream.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
do_constant_variable(exec_list *instructions)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
ir_constant_variable_visitor v;
|
|
|
|
|
2015-09-05 08:22:57 +01:00
|
|
|
v.ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
|
|
|
|
_mesa_key_pointer_equal);
|
2010-05-12 20:10:41 +01:00
|
|
|
v.run(instructions);
|
|
|
|
|
2015-09-05 08:22:57 +01:00
|
|
|
struct hash_entry *hte;
|
|
|
|
hash_table_foreach(v.ht, hte) {
|
|
|
|
struct assignment_entry *entry = (struct assignment_entry *) hte->data;
|
2010-05-12 20:10:41 +01:00
|
|
|
|
2010-07-29 22:54:01 +01:00
|
|
|
if (entry->assignment_count == 1 && entry->constval && entry->our_scope) {
|
2010-05-12 20:10:41 +01:00
|
|
|
entry->var->constant_value = entry->constval;
|
|
|
|
progress = true;
|
|
|
|
}
|
2015-09-05 08:22:57 +01:00
|
|
|
hte->data = NULL;
|
2010-05-12 20:10:41 +01:00
|
|
|
free(entry);
|
|
|
|
}
|
2015-09-05 08:22:57 +01:00
|
|
|
_mesa_hash_table_destroy(v.ht, NULL);
|
2010-05-12 20:10:41 +01:00
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
do_constant_variable_unlinked(exec_list *instructions)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
|
2014-06-25 05:34:05 +01:00
|
|
|
foreach_in_list(ir_instruction, ir, instructions) {
|
2010-05-12 20:10:41 +01:00
|
|
|
ir_function *f = ir->as_function();
|
|
|
|
if (f) {
|
2014-06-25 05:34:05 +01:00
|
|
|
foreach_in_list(ir_function_signature, sig, &f->signatures) {
|
2010-05-12 20:10:41 +01:00
|
|
|
if (do_constant_variable(&sig->body))
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|