101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2010 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg/**
2501e04c3fSmrg * \file opt_constant_folding.cpp
2601e04c3fSmrg * Replace constant-valued expressions with references to constant values.
2701e04c3fSmrg */
2801e04c3fSmrg
2901e04c3fSmrg#include "ir.h"
3001e04c3fSmrg#include "ir_visitor.h"
3101e04c3fSmrg#include "ir_rvalue_visitor.h"
3201e04c3fSmrg#include "ir_optimization.h"
3301e04c3fSmrg#include "compiler/glsl_types.h"
3401e04c3fSmrg
3501e04c3fSmrgnamespace {
3601e04c3fSmrg
3701e04c3fSmrg/**
3801e04c3fSmrg * Visitor class for replacing expressions with ir_constant values.
3901e04c3fSmrg */
4001e04c3fSmrg
4101e04c3fSmrgclass ir_constant_folding_visitor : public ir_rvalue_visitor {
4201e04c3fSmrgpublic:
4301e04c3fSmrg   ir_constant_folding_visitor()
4401e04c3fSmrg   {
4501e04c3fSmrg      this->progress = false;
4601e04c3fSmrg   }
4701e04c3fSmrg
4801e04c3fSmrg   virtual ~ir_constant_folding_visitor()
4901e04c3fSmrg   {
5001e04c3fSmrg      /* empty */
5101e04c3fSmrg   }
5201e04c3fSmrg
5301e04c3fSmrg   virtual ir_visitor_status visit_enter(ir_discard *ir);
5401e04c3fSmrg   virtual ir_visitor_status visit_enter(ir_assignment *ir);
5501e04c3fSmrg   virtual ir_visitor_status visit_enter(ir_call *ir);
5601e04c3fSmrg
5701e04c3fSmrg   virtual void handle_rvalue(ir_rvalue **rvalue);
5801e04c3fSmrg
5901e04c3fSmrg   bool progress;
6001e04c3fSmrg};
6101e04c3fSmrg
6201e04c3fSmrg} /* unnamed namespace */
6301e04c3fSmrg
6401e04c3fSmrgbool
6501e04c3fSmrgir_constant_fold(ir_rvalue **rvalue)
6601e04c3fSmrg{
6701e04c3fSmrg   if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
6801e04c3fSmrg      return false;
6901e04c3fSmrg
7001e04c3fSmrg   /* Note that we do rvalue visitoring on leaving.  So if an
7101e04c3fSmrg    * expression has a non-constant operand, no need to go looking
7201e04c3fSmrg    * down it to find if it's constant.  This cuts the time of this
7301e04c3fSmrg    * pass down drastically.
7401e04c3fSmrg    */
7501e04c3fSmrg   ir_expression *expr = (*rvalue)->as_expression();
7601e04c3fSmrg   if (expr) {
7701e04c3fSmrg      for (unsigned int i = 0; i < expr->num_operands; i++) {
7801e04c3fSmrg	 if (!expr->operands[i]->as_constant())
7901e04c3fSmrg	    return false;
8001e04c3fSmrg      }
8101e04c3fSmrg   }
8201e04c3fSmrg
8301e04c3fSmrg   /* Ditto for swizzles. */
8401e04c3fSmrg   ir_swizzle *swiz = (*rvalue)->as_swizzle();
8501e04c3fSmrg   if (swiz && !swiz->val->as_constant())
8601e04c3fSmrg      return false;
8701e04c3fSmrg
8801e04c3fSmrg   /* Ditto for array dereferences */
8901e04c3fSmrg   ir_dereference_array *array_ref = (*rvalue)->as_dereference_array();
9001e04c3fSmrg   if (array_ref && (!array_ref->array->as_constant() ||
9101e04c3fSmrg                     !array_ref->array_index->as_constant()))
9201e04c3fSmrg      return false;
9301e04c3fSmrg
9401e04c3fSmrg   /* No constant folding can be performed on variable dereferences.  We need
9501e04c3fSmrg    * to explicitly avoid them, as calling constant_expression_value() on a
9601e04c3fSmrg    * variable dereference will return a clone of var->constant_value.  This
9701e04c3fSmrg    * would make us propagate the value into the tree, which isn't our job.
9801e04c3fSmrg    */
9901e04c3fSmrg   ir_dereference_variable *var_ref = (*rvalue)->as_dereference_variable();
10001e04c3fSmrg   if (var_ref)
10101e04c3fSmrg      return false;
10201e04c3fSmrg
10301e04c3fSmrg   ir_constant *constant =
10401e04c3fSmrg      (*rvalue)->constant_expression_value(ralloc_parent(*rvalue));
10501e04c3fSmrg   if (constant) {
10601e04c3fSmrg      *rvalue = constant;
10701e04c3fSmrg      return true;
10801e04c3fSmrg   }
10901e04c3fSmrg   return false;
11001e04c3fSmrg}
11101e04c3fSmrg
11201e04c3fSmrgvoid
11301e04c3fSmrgir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue)
11401e04c3fSmrg{
11501e04c3fSmrg   if (ir_constant_fold(rvalue))
11601e04c3fSmrg      this->progress = true;
11701e04c3fSmrg}
11801e04c3fSmrg
11901e04c3fSmrgir_visitor_status
12001e04c3fSmrgir_constant_folding_visitor::visit_enter(ir_discard *ir)
12101e04c3fSmrg{
12201e04c3fSmrg   if (ir->condition) {
12301e04c3fSmrg      ir->condition->accept(this);
12401e04c3fSmrg      handle_rvalue(&ir->condition);
12501e04c3fSmrg
12601e04c3fSmrg      ir_constant *const_val = ir->condition->as_constant();
12701e04c3fSmrg      /* If the condition is constant, either remove the condition or
12801e04c3fSmrg       * remove the never-executed assignment.
12901e04c3fSmrg       */
13001e04c3fSmrg      if (const_val) {
13101e04c3fSmrg         if (const_val->value.b[0])
13201e04c3fSmrg            ir->condition = NULL;
13301e04c3fSmrg         else
13401e04c3fSmrg            ir->remove();
13501e04c3fSmrg         this->progress = true;
13601e04c3fSmrg      }
13701e04c3fSmrg   }
13801e04c3fSmrg
13901e04c3fSmrg   return visit_continue_with_parent;
14001e04c3fSmrg}
14101e04c3fSmrg
14201e04c3fSmrgir_visitor_status
14301e04c3fSmrgir_constant_folding_visitor::visit_enter(ir_assignment *ir)
14401e04c3fSmrg{
14501e04c3fSmrg   ir->rhs->accept(this);
14601e04c3fSmrg   handle_rvalue(&ir->rhs);
14701e04c3fSmrg
14801e04c3fSmrg   if (ir->condition) {
14901e04c3fSmrg      ir->condition->accept(this);
15001e04c3fSmrg      handle_rvalue(&ir->condition);
15101e04c3fSmrg
15201e04c3fSmrg      ir_constant *const_val = ir->condition->as_constant();
15301e04c3fSmrg      /* If the condition is constant, either remove the condition or
15401e04c3fSmrg       * remove the never-executed assignment.
15501e04c3fSmrg       */
15601e04c3fSmrg      if (const_val) {
15701e04c3fSmrg	 if (const_val->value.b[0])
15801e04c3fSmrg	    ir->condition = NULL;
15901e04c3fSmrg	 else
16001e04c3fSmrg	    ir->remove();
16101e04c3fSmrg	 this->progress = true;
16201e04c3fSmrg      }
16301e04c3fSmrg   }
16401e04c3fSmrg
16501e04c3fSmrg   /* Don't descend into the LHS because we want it to stay as a
16601e04c3fSmrg    * variable dereference.  FINISHME: We probably should to get array
16701e04c3fSmrg    * indices though.
16801e04c3fSmrg    */
16901e04c3fSmrg   return visit_continue_with_parent;
17001e04c3fSmrg}
17101e04c3fSmrg
17201e04c3fSmrgir_visitor_status
17301e04c3fSmrgir_constant_folding_visitor::visit_enter(ir_call *ir)
17401e04c3fSmrg{
17501e04c3fSmrg   /* Attempt to constant fold parameters */
17601e04c3fSmrg   foreach_two_lists(formal_node, &ir->callee->parameters,
17701e04c3fSmrg                     actual_node, &ir->actual_parameters) {
17801e04c3fSmrg      ir_rvalue *param_rval = (ir_rvalue *) actual_node;
17901e04c3fSmrg      ir_variable *sig_param = (ir_variable *) formal_node;
18001e04c3fSmrg
18101e04c3fSmrg      if (sig_param->data.mode == ir_var_function_in
18201e04c3fSmrg          || sig_param->data.mode == ir_var_const_in) {
18301e04c3fSmrg	 ir_rvalue *new_param = param_rval;
18401e04c3fSmrg
18501e04c3fSmrg	 handle_rvalue(&new_param);
18601e04c3fSmrg	 if (new_param != param_rval) {
18701e04c3fSmrg	    param_rval->replace_with(new_param);
18801e04c3fSmrg	 }
18901e04c3fSmrg      }
19001e04c3fSmrg   }
19101e04c3fSmrg
19201e04c3fSmrg   /* Next, see if the call can be replaced with an assignment of a constant */
19301e04c3fSmrg   ir_constant *const_val = ir->constant_expression_value(ralloc_parent(ir));
19401e04c3fSmrg
19501e04c3fSmrg   if (const_val != NULL) {
19601e04c3fSmrg      ir_assignment *assignment =
19701e04c3fSmrg	 new(ralloc_parent(ir)) ir_assignment(ir->return_deref, const_val);
19801e04c3fSmrg      ir->replace_with(assignment);
19901e04c3fSmrg   }
20001e04c3fSmrg
20101e04c3fSmrg   return visit_continue_with_parent;
20201e04c3fSmrg}
20301e04c3fSmrg
20401e04c3fSmrgbool
20501e04c3fSmrgdo_constant_folding(exec_list *instructions)
20601e04c3fSmrg{
20701e04c3fSmrg   ir_constant_folding_visitor constant_folding;
20801e04c3fSmrg
20901e04c3fSmrg   visit_list_elements(&constant_folding, instructions);
21001e04c3fSmrg
21101e04c3fSmrg   return constant_folding.progress;
21201e04c3fSmrg}
213