2010-04-05 23:24:28 +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_folding.cpp
|
2010-04-05 23:24:28 +01:00
|
|
|
* Replace constant-valued expressions with references to constant values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ir.h"
|
|
|
|
#include "ir_visitor.h"
|
2010-08-23 02:15:20 +01:00
|
|
|
#include "ir_rvalue_visitor.h"
|
2010-05-05 19:45:30 +01:00
|
|
|
#include "ir_optimization.h"
|
2010-04-05 23:24:28 +01:00
|
|
|
#include "glsl_types.h"
|
|
|
|
|
2012-05-30 00:18:37 +01:00
|
|
|
namespace {
|
|
|
|
|
2010-04-05 23:24:28 +01:00
|
|
|
/**
|
|
|
|
* Visitor class for replacing expressions with ir_constant values.
|
|
|
|
*/
|
|
|
|
|
2010-08-23 02:15:20 +01:00
|
|
|
class ir_constant_folding_visitor : public ir_rvalue_visitor {
|
2010-05-05 19:45:30 +01:00
|
|
|
public:
|
|
|
|
ir_constant_folding_visitor()
|
|
|
|
{
|
2010-08-05 00:07:41 +01:00
|
|
|
this->progress = false;
|
2010-05-05 19:45:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ir_constant_folding_visitor()
|
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
}
|
|
|
|
|
2010-08-23 02:15:20 +01:00
|
|
|
virtual ir_visitor_status visit_enter(ir_assignment *ir);
|
2010-08-23 02:26:42 +01:00
|
|
|
virtual ir_visitor_status visit_enter(ir_call *ir);
|
2010-08-05 00:06:00 +01:00
|
|
|
|
2010-08-23 02:15:20 +01:00
|
|
|
virtual void handle_rvalue(ir_rvalue **rvalue);
|
2010-08-05 00:07:41 +01:00
|
|
|
|
|
|
|
bool progress;
|
2010-05-05 19:45:30 +01:00
|
|
|
};
|
|
|
|
|
2012-05-30 00:18:37 +01:00
|
|
|
} /* unnamed namespace */
|
|
|
|
|
2010-08-05 00:06:00 +01:00
|
|
|
void
|
2010-08-23 02:15:20 +01:00
|
|
|
ir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue)
|
2010-08-05 00:06:00 +01:00
|
|
|
{
|
2010-08-05 00:18:18 +01:00
|
|
|
if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
|
2010-08-05 00:06:00 +01:00
|
|
|
return;
|
|
|
|
|
2010-08-26 18:42:47 +01:00
|
|
|
/* Note that we do rvalue visitoring on leaving. So if an
|
|
|
|
* expression has a non-constant operand, no need to go looking
|
|
|
|
* down it to find if it's constant. This cuts the time of this
|
|
|
|
* pass down drastically.
|
|
|
|
*/
|
|
|
|
ir_expression *expr = (*rvalue)->as_expression();
|
|
|
|
if (expr) {
|
|
|
|
for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
|
|
|
|
if (!expr->operands[i]->as_constant())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 00:06:00 +01:00
|
|
|
ir_constant *constant = (*rvalue)->constant_expression_value();
|
|
|
|
if (constant) {
|
|
|
|
*rvalue = constant;
|
2010-08-05 00:07:41 +01:00
|
|
|
this->progress = true;
|
2010-08-05 00:06:00 +01:00
|
|
|
} else {
|
|
|
|
(*rvalue)->accept(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-23 02:15:20 +01:00
|
|
|
ir_visitor_status
|
|
|
|
ir_constant_folding_visitor::visit_enter(ir_assignment *ir)
|
2010-04-05 23:24:28 +01:00
|
|
|
{
|
2010-08-23 02:15:20 +01:00
|
|
|
ir->rhs->accept(this);
|
|
|
|
handle_rvalue(&ir->rhs);
|
2010-07-20 19:43:28 +01:00
|
|
|
|
|
|
|
if (ir->condition) {
|
2010-08-23 02:15:20 +01:00
|
|
|
ir->condition->accept(this);
|
|
|
|
handle_rvalue(&ir->condition);
|
|
|
|
|
|
|
|
ir_constant *const_val = ir->condition->as_constant();
|
2010-07-20 19:43:28 +01:00
|
|
|
/* If the condition is constant, either remove the condition or
|
|
|
|
* remove the never-executed assignment.
|
|
|
|
*/
|
|
|
|
if (const_val) {
|
|
|
|
if (const_val->value.b[0])
|
|
|
|
ir->condition = NULL;
|
|
|
|
else
|
|
|
|
ir->remove();
|
2010-08-05 00:18:18 +01:00
|
|
|
this->progress = true;
|
2010-07-20 19:43:28 +01:00
|
|
|
}
|
|
|
|
}
|
2010-04-05 23:24:28 +01:00
|
|
|
|
2010-08-23 02:15:20 +01:00
|
|
|
/* Don't descend into the LHS because we want it to stay as a
|
|
|
|
* variable dereference. FINISHME: We probably should to get array
|
|
|
|
* indices though.
|
|
|
|
*/
|
|
|
|
return visit_continue_with_parent;
|
2010-04-06 00:28:15 +01:00
|
|
|
}
|
2010-05-05 19:45:30 +01:00
|
|
|
|
2010-08-23 02:26:42 +01:00
|
|
|
ir_visitor_status
|
|
|
|
ir_constant_folding_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
|
|
|
/* Attempt to constant fold parameters */
|
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 *sig_param = (ir_variable *) formal_node;
|
2010-08-23 02:26:42 +01:00
|
|
|
|
2013-12-12 11:51:01 +00:00
|
|
|
if (sig_param->data.mode == ir_var_function_in
|
|
|
|
|| sig_param->data.mode == ir_var_const_in) {
|
2010-08-23 02:26:42 +01:00
|
|
|
ir_rvalue *new_param = param_rval;
|
|
|
|
|
|
|
|
handle_rvalue(&new_param);
|
|
|
|
if (new_param != param_rval) {
|
|
|
|
param_rval->replace_with(new_param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/* Next, see if the call can be replaced with an assignment of a constant */
|
|
|
|
ir_constant *const_val = ir->constant_expression_value();
|
|
|
|
|
|
|
|
if (const_val != NULL) {
|
|
|
|
ir_assignment *assignment =
|
|
|
|
new(ralloc_parent(ir)) ir_assignment(ir->return_deref, const_val);
|
|
|
|
ir->replace_with(assignment);
|
|
|
|
}
|
|
|
|
|
2010-08-23 02:26:42 +01:00
|
|
|
return visit_continue_with_parent;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:45:30 +01:00
|
|
|
bool
|
|
|
|
do_constant_folding(exec_list *instructions)
|
|
|
|
{
|
|
|
|
ir_constant_folding_visitor constant_folding;
|
|
|
|
|
2010-08-23 02:15:20 +01:00
|
|
|
visit_list_elements(&constant_folding, instructions);
|
2010-05-05 19:45:30 +01:00
|
|
|
|
2010-08-05 00:07:41 +01:00
|
|
|
return constant_folding.progress;
|
2010-05-05 19:45:30 +01:00
|
|
|
}
|