1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2010 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg/**
25b8e80941Smrg * \file opt_constant_folding.cpp
26b8e80941Smrg * Replace constant-valued expressions with references to constant values.
27b8e80941Smrg */
28b8e80941Smrg
29b8e80941Smrg#include "ir.h"
30b8e80941Smrg#include "ir_visitor.h"
31b8e80941Smrg#include "ir_rvalue_visitor.h"
32b8e80941Smrg#include "ir_optimization.h"
33b8e80941Smrg#include "compiler/glsl_types.h"
34b8e80941Smrg
35b8e80941Smrgnamespace {
36b8e80941Smrg
37b8e80941Smrg/**
38b8e80941Smrg * Visitor class for replacing expressions with ir_constant values.
39b8e80941Smrg */
40b8e80941Smrg
41b8e80941Smrgclass ir_constant_folding_visitor : public ir_rvalue_visitor {
42b8e80941Smrgpublic:
43b8e80941Smrg   ir_constant_folding_visitor()
44b8e80941Smrg   {
45b8e80941Smrg      this->progress = false;
46b8e80941Smrg   }
47b8e80941Smrg
48b8e80941Smrg   virtual ~ir_constant_folding_visitor()
49b8e80941Smrg   {
50b8e80941Smrg      /* empty */
51b8e80941Smrg   }
52b8e80941Smrg
53b8e80941Smrg   virtual ir_visitor_status visit_enter(ir_discard *ir);
54b8e80941Smrg   virtual ir_visitor_status visit_enter(ir_assignment *ir);
55b8e80941Smrg   virtual ir_visitor_status visit_enter(ir_call *ir);
56b8e80941Smrg
57b8e80941Smrg   virtual void handle_rvalue(ir_rvalue **rvalue);
58b8e80941Smrg
59b8e80941Smrg   bool progress;
60b8e80941Smrg};
61b8e80941Smrg
62b8e80941Smrg} /* unnamed namespace */
63b8e80941Smrg
64b8e80941Smrgbool
65b8e80941Smrgir_constant_fold(ir_rvalue **rvalue)
66b8e80941Smrg{
67b8e80941Smrg   if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
68b8e80941Smrg      return false;
69b8e80941Smrg
70b8e80941Smrg   /* Note that we do rvalue visitoring on leaving.  So if an
71b8e80941Smrg    * expression has a non-constant operand, no need to go looking
72b8e80941Smrg    * down it to find if it's constant.  This cuts the time of this
73b8e80941Smrg    * pass down drastically.
74b8e80941Smrg    */
75b8e80941Smrg   ir_expression *expr = (*rvalue)->as_expression();
76b8e80941Smrg   if (expr) {
77b8e80941Smrg      for (unsigned int i = 0; i < expr->num_operands; i++) {
78b8e80941Smrg	 if (!expr->operands[i]->as_constant())
79b8e80941Smrg	    return false;
80b8e80941Smrg      }
81b8e80941Smrg   }
82b8e80941Smrg
83b8e80941Smrg   /* Ditto for swizzles. */
84b8e80941Smrg   ir_swizzle *swiz = (*rvalue)->as_swizzle();
85b8e80941Smrg   if (swiz && !swiz->val->as_constant())
86b8e80941Smrg      return false;
87b8e80941Smrg
88b8e80941Smrg   /* Ditto for array dereferences */
89b8e80941Smrg   ir_dereference_array *array_ref = (*rvalue)->as_dereference_array();
90b8e80941Smrg   if (array_ref && (!array_ref->array->as_constant() ||
91b8e80941Smrg                     !array_ref->array_index->as_constant()))
92b8e80941Smrg      return false;
93b8e80941Smrg
94b8e80941Smrg   /* No constant folding can be performed on variable dereferences.  We need
95b8e80941Smrg    * to explicitly avoid them, as calling constant_expression_value() on a
96b8e80941Smrg    * variable dereference will return a clone of var->constant_value.  This
97b8e80941Smrg    * would make us propagate the value into the tree, which isn't our job.
98b8e80941Smrg    */
99b8e80941Smrg   ir_dereference_variable *var_ref = (*rvalue)->as_dereference_variable();
100b8e80941Smrg   if (var_ref)
101b8e80941Smrg      return false;
102b8e80941Smrg
103b8e80941Smrg   ir_constant *constant =
104b8e80941Smrg      (*rvalue)->constant_expression_value(ralloc_parent(*rvalue));
105b8e80941Smrg   if (constant) {
106b8e80941Smrg      *rvalue = constant;
107b8e80941Smrg      return true;
108b8e80941Smrg   }
109b8e80941Smrg   return false;
110b8e80941Smrg}
111b8e80941Smrg
112b8e80941Smrgvoid
113b8e80941Smrgir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue)
114b8e80941Smrg{
115b8e80941Smrg   if (ir_constant_fold(rvalue))
116b8e80941Smrg      this->progress = true;
117b8e80941Smrg}
118b8e80941Smrg
119b8e80941Smrgir_visitor_status
120b8e80941Smrgir_constant_folding_visitor::visit_enter(ir_discard *ir)
121b8e80941Smrg{
122b8e80941Smrg   if (ir->condition) {
123b8e80941Smrg      ir->condition->accept(this);
124b8e80941Smrg      handle_rvalue(&ir->condition);
125b8e80941Smrg
126b8e80941Smrg      ir_constant *const_val = ir->condition->as_constant();
127b8e80941Smrg      /* If the condition is constant, either remove the condition or
128b8e80941Smrg       * remove the never-executed assignment.
129b8e80941Smrg       */
130b8e80941Smrg      if (const_val) {
131b8e80941Smrg         if (const_val->value.b[0])
132b8e80941Smrg            ir->condition = NULL;
133b8e80941Smrg         else
134b8e80941Smrg            ir->remove();
135b8e80941Smrg         this->progress = true;
136b8e80941Smrg      }
137b8e80941Smrg   }
138b8e80941Smrg
139b8e80941Smrg   return visit_continue_with_parent;
140b8e80941Smrg}
141b8e80941Smrg
142b8e80941Smrgir_visitor_status
143b8e80941Smrgir_constant_folding_visitor::visit_enter(ir_assignment *ir)
144b8e80941Smrg{
145b8e80941Smrg   ir->rhs->accept(this);
146b8e80941Smrg   handle_rvalue(&ir->rhs);
147b8e80941Smrg
148b8e80941Smrg   if (ir->condition) {
149b8e80941Smrg      ir->condition->accept(this);
150b8e80941Smrg      handle_rvalue(&ir->condition);
151b8e80941Smrg
152b8e80941Smrg      ir_constant *const_val = ir->condition->as_constant();
153b8e80941Smrg      /* If the condition is constant, either remove the condition or
154b8e80941Smrg       * remove the never-executed assignment.
155b8e80941Smrg       */
156b8e80941Smrg      if (const_val) {
157b8e80941Smrg	 if (const_val->value.b[0])
158b8e80941Smrg	    ir->condition = NULL;
159b8e80941Smrg	 else
160b8e80941Smrg	    ir->remove();
161b8e80941Smrg	 this->progress = true;
162b8e80941Smrg      }
163b8e80941Smrg   }
164b8e80941Smrg
165b8e80941Smrg   /* Don't descend into the LHS because we want it to stay as a
166b8e80941Smrg    * variable dereference.  FINISHME: We probably should to get array
167b8e80941Smrg    * indices though.
168b8e80941Smrg    */
169b8e80941Smrg   return visit_continue_with_parent;
170b8e80941Smrg}
171b8e80941Smrg
172b8e80941Smrgir_visitor_status
173b8e80941Smrgir_constant_folding_visitor::visit_enter(ir_call *ir)
174b8e80941Smrg{
175b8e80941Smrg   /* Attempt to constant fold parameters */
176b8e80941Smrg   foreach_two_lists(formal_node, &ir->callee->parameters,
177b8e80941Smrg                     actual_node, &ir->actual_parameters) {
178b8e80941Smrg      ir_rvalue *param_rval = (ir_rvalue *) actual_node;
179b8e80941Smrg      ir_variable *sig_param = (ir_variable *) formal_node;
180b8e80941Smrg
181b8e80941Smrg      if (sig_param->data.mode == ir_var_function_in
182b8e80941Smrg          || sig_param->data.mode == ir_var_const_in) {
183b8e80941Smrg	 ir_rvalue *new_param = param_rval;
184b8e80941Smrg
185b8e80941Smrg	 handle_rvalue(&new_param);
186b8e80941Smrg	 if (new_param != param_rval) {
187b8e80941Smrg	    param_rval->replace_with(new_param);
188b8e80941Smrg	 }
189b8e80941Smrg      }
190b8e80941Smrg   }
191b8e80941Smrg
192b8e80941Smrg   /* Next, see if the call can be replaced with an assignment of a constant */
193b8e80941Smrg   ir_constant *const_val = ir->constant_expression_value(ralloc_parent(ir));
194b8e80941Smrg
195b8e80941Smrg   if (const_val != NULL) {
196b8e80941Smrg      ir_assignment *assignment =
197b8e80941Smrg	 new(ralloc_parent(ir)) ir_assignment(ir->return_deref, const_val);
198b8e80941Smrg      ir->replace_with(assignment);
199b8e80941Smrg   }
200b8e80941Smrg
201b8e80941Smrg   return visit_continue_with_parent;
202b8e80941Smrg}
203b8e80941Smrg
204b8e80941Smrgbool
205b8e80941Smrgdo_constant_folding(exec_list *instructions)
206b8e80941Smrg{
207b8e80941Smrg   ir_constant_folding_visitor constant_folding;
208b8e80941Smrg
209b8e80941Smrg   visit_list_elements(&constant_folding, instructions);
210b8e80941Smrg
211b8e80941Smrg   return constant_folding.progress;
212b8e80941Smrg}
213