Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
Chad Versace | df883eb | 2010-11-17 10:43:10 -0800 | [diff] [blame] | 25 | * \file opt_constant_folding.cpp |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 26 | * Replace constant-valued expressions with references to constant values. |
| 27 | */ |
| 28 | |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 29 | #include "ir.h" |
| 30 | #include "ir_visitor.h" |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 31 | #include "ir_rvalue_visitor.h" |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 32 | #include "ir_optimization.h" |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 33 | #include "glsl_types.h" |
| 34 | |
Eric Anholt | 337d9c9 | 2012-05-29 16:18:37 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Visitor class for replacing expressions with ir_constant values. |
| 39 | */ |
| 40 | |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 41 | class ir_constant_folding_visitor : public ir_rvalue_visitor { |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 42 | public: |
| 43 | ir_constant_folding_visitor() |
| 44 | { |
Eric Anholt | 6ecf62f | 2010-08-04 16:07:41 -0700 | [diff] [blame] | 45 | this->progress = false; |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | virtual ~ir_constant_folding_visitor() |
| 49 | { |
| 50 | /* empty */ |
| 51 | } |
| 52 | |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 53 | virtual ir_visitor_status visit_enter(ir_assignment *ir); |
Eric Anholt | 428a3cd | 2010-08-22 18:26:42 -0700 | [diff] [blame] | 54 | virtual ir_visitor_status visit_enter(ir_call *ir); |
Eric Anholt | 3d23f0a | 2010-08-04 16:06:00 -0700 | [diff] [blame] | 55 | |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 56 | virtual void handle_rvalue(ir_rvalue **rvalue); |
Eric Anholt | 6ecf62f | 2010-08-04 16:07:41 -0700 | [diff] [blame] | 57 | |
| 58 | bool progress; |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Eric Anholt | 337d9c9 | 2012-05-29 16:18:37 -0700 | [diff] [blame] | 61 | } /* unnamed namespace */ |
| 62 | |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 63 | void |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 64 | ir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue) |
Eric Anholt | 3d23f0a | 2010-08-04 16:06:00 -0700 | [diff] [blame] | 65 | { |
Eric Anholt | 8dbdcb0 | 2010-08-04 16:18:18 -0700 | [diff] [blame] | 66 | if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant) |
Eric Anholt | 3d23f0a | 2010-08-04 16:06:00 -0700 | [diff] [blame] | 67 | return; |
| 68 | |
Eric Anholt | 2db7bb9 | 2010-08-26 10:42:47 -0700 | [diff] [blame] | 69 | /* Note that we do rvalue visitoring on leaving. So if an |
| 70 | * expression has a non-constant operand, no need to go looking |
| 71 | * down it to find if it's constant. This cuts the time of this |
| 72 | * pass down drastically. |
| 73 | */ |
| 74 | ir_expression *expr = (*rvalue)->as_expression(); |
| 75 | if (expr) { |
| 76 | for (unsigned int i = 0; i < expr->get_num_operands(); i++) { |
| 77 | if (!expr->operands[i]->as_constant()) |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | |
Kenneth Graunke | 84a40ce | 2014-09-12 15:16:57 -0700 | [diff] [blame] | 82 | /* Ditto for swizzles. */ |
| 83 | ir_swizzle *swiz = (*rvalue)->as_swizzle(); |
| 84 | if (swiz && !swiz->val->as_constant()) |
| 85 | return; |
| 86 | |
Eric Anholt | 3d23f0a | 2010-08-04 16:06:00 -0700 | [diff] [blame] | 87 | ir_constant *constant = (*rvalue)->constant_expression_value(); |
| 88 | if (constant) { |
| 89 | *rvalue = constant; |
Eric Anholt | 6ecf62f | 2010-08-04 16:07:41 -0700 | [diff] [blame] | 90 | this->progress = true; |
Eric Anholt | 3d23f0a | 2010-08-04 16:06:00 -0700 | [diff] [blame] | 91 | } else { |
| 92 | (*rvalue)->accept(this); |
| 93 | } |
| 94 | } |
| 95 | |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 96 | ir_visitor_status |
| 97 | ir_constant_folding_visitor::visit_enter(ir_assignment *ir) |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 98 | { |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 99 | ir->rhs->accept(this); |
| 100 | handle_rvalue(&ir->rhs); |
Eric Anholt | 14f8e16 | 2010-07-20 11:43:28 -0700 | [diff] [blame] | 101 | |
| 102 | if (ir->condition) { |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 103 | ir->condition->accept(this); |
| 104 | handle_rvalue(&ir->condition); |
| 105 | |
| 106 | ir_constant *const_val = ir->condition->as_constant(); |
Eric Anholt | 14f8e16 | 2010-07-20 11:43:28 -0700 | [diff] [blame] | 107 | /* If the condition is constant, either remove the condition or |
| 108 | * remove the never-executed assignment. |
| 109 | */ |
Eric Anholt | 14f8e16 | 2010-07-20 11:43:28 -0700 | [diff] [blame] | 110 | if (const_val) { |
| 111 | if (const_val->value.b[0]) |
| 112 | ir->condition = NULL; |
| 113 | else |
| 114 | ir->remove(); |
Eric Anholt | 8dbdcb0 | 2010-08-04 16:18:18 -0700 | [diff] [blame] | 115 | this->progress = true; |
Eric Anholt | 14f8e16 | 2010-07-20 11:43:28 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 118 | |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 119 | /* Don't descend into the LHS because we want it to stay as a |
| 120 | * variable dereference. FINISHME: We probably should to get array |
| 121 | * indices though. |
| 122 | */ |
| 123 | return visit_continue_with_parent; |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 124 | } |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 125 | |
Eric Anholt | 428a3cd | 2010-08-22 18:26:42 -0700 | [diff] [blame] | 126 | ir_visitor_status |
| 127 | ir_constant_folding_visitor::visit_enter(ir_call *ir) |
| 128 | { |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 129 | /* Attempt to constant fold parameters */ |
Kenneth Graunke | 48d0faa | 2014-01-10 16:39:17 -0800 | [diff] [blame] | 130 | foreach_two_lists(formal_node, &ir->callee->parameters, |
| 131 | actual_node, &ir->actual_parameters) { |
| 132 | ir_rvalue *param_rval = (ir_rvalue *) actual_node; |
| 133 | ir_variable *sig_param = (ir_variable *) formal_node; |
Eric Anholt | 428a3cd | 2010-08-22 18:26:42 -0700 | [diff] [blame] | 134 | |
Tapani Pälli | 33ee2c6 | 2013-12-12 13:51:01 +0200 | [diff] [blame] | 135 | if (sig_param->data.mode == ir_var_function_in |
| 136 | || sig_param->data.mode == ir_var_const_in) { |
Eric Anholt | 428a3cd | 2010-08-22 18:26:42 -0700 | [diff] [blame] | 137 | ir_rvalue *new_param = param_rval; |
| 138 | |
| 139 | handle_rvalue(&new_param); |
| 140 | if (new_param != param_rval) { |
| 141 | param_rval->replace_with(new_param); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Kenneth Graunke | d884f60 | 2012-03-20 15:56:37 -0700 | [diff] [blame] | 146 | /* Next, see if the call can be replaced with an assignment of a constant */ |
| 147 | ir_constant *const_val = ir->constant_expression_value(); |
| 148 | |
| 149 | if (const_val != NULL) { |
| 150 | ir_assignment *assignment = |
| 151 | new(ralloc_parent(ir)) ir_assignment(ir->return_deref, const_val); |
| 152 | ir->replace_with(assignment); |
| 153 | } |
| 154 | |
Eric Anholt | 428a3cd | 2010-08-22 18:26:42 -0700 | [diff] [blame] | 155 | return visit_continue_with_parent; |
| 156 | } |
| 157 | |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 158 | bool |
| 159 | do_constant_folding(exec_list *instructions) |
| 160 | { |
| 161 | ir_constant_folding_visitor constant_folding; |
| 162 | |
Eric Anholt | 6606fde | 2010-08-22 18:15:20 -0700 | [diff] [blame] | 163 | visit_list_elements(&constant_folding, instructions); |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 164 | |
Eric Anholt | 6ecf62f | 2010-08-04 16:07:41 -0700 | [diff] [blame] | 165 | return constant_folding.progress; |
Eric Anholt | bdd9b1f | 2010-05-05 11:45:30 -0700 | [diff] [blame] | 166 | } |