ac420454e8801149cb018533bdaaf36ba340ef16
[mesa.git] / src / glsl / ir_vec_index_to_cond_assign.cpp
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 /**
25 * \file ir_vec_index_to_cond_assign.cpp
26 *
27 * Turns indexing into vector types to a series of conditional moves
28 * of each channel's swizzle into a temporary.
29 *
30 * Most GPUs don't have a native way to do this operation, and this
31 * works around that. For drivers using both this pass and
32 * ir_vec_index_to_swizzle, there's a risk that this pass will happen
33 * before sufficient constant folding to find that the array index is
34 * constant. However, we hope that other optimization passes,
35 * particularly constant folding of assignment conditions and copy
36 * propagation, will result in the same code in the end.
37 */
38
39 #include "ir.h"
40 #include "ir_visitor.h"
41 #include "ir_optimization.h"
42 #include "glsl_types.h"
43
44 /**
45 * Visitor class for replacing expressions with ir_constant values.
46 */
47
48 class ir_vec_index_to_cond_assign_visitor : public ir_hierarchical_visitor {
49 public:
50 ir_vec_index_to_cond_assign_visitor()
51 {
52 progress = false;
53 }
54
55 ir_rvalue *convert_vec_index_to_cond_assign(ir_rvalue *val);
56
57 virtual ir_visitor_status visit_enter(ir_expression *);
58 virtual ir_visitor_status visit_enter(ir_swizzle *);
59 virtual ir_visitor_status visit_leave(ir_assignment *);
60 virtual ir_visitor_status visit_enter(ir_return *);
61 virtual ir_visitor_status visit_enter(ir_call *);
62 virtual ir_visitor_status visit_enter(ir_if *);
63
64 bool progress;
65 };
66
67 ir_rvalue *
68 ir_vec_index_to_cond_assign_visitor::convert_vec_index_to_cond_assign(ir_rvalue *ir)
69 {
70 ir_dereference_array *orig_deref = ir->as_dereference_array();
71 ir_assignment *assign;
72 ir_variable *index, *var;
73 ir_dereference *deref;
74 ir_expression *condition;
75 ir_swizzle *swizzle;
76 int i;
77
78 if (!orig_deref)
79 return ir;
80
81 if (orig_deref->array->type->is_matrix() ||
82 orig_deref->array->type->is_array())
83 return ir;
84
85 assert(orig_deref->array_index->type->base_type == GLSL_TYPE_INT);
86
87 /* Store the index to a temporary to avoid reusing its tree. */
88 index = new(base_ir) ir_variable(glsl_type::int_type,
89 "vec_index_tmp_i");
90 base_ir->insert_before(index);
91 deref = new(base_ir) ir_dereference_variable(index);
92 assign = new(base_ir) ir_assignment(deref, orig_deref->array_index, NULL);
93 base_ir->insert_before(assign);
94
95 /* Temporary where we store whichever value we swizzle out. */
96 var = new(base_ir) ir_variable(ir->type, "vec_index_tmp_v");
97 base_ir->insert_before(var);
98
99 /* Generate a conditional move of each vector element to the temp. */
100 for (i = 0; i < orig_deref->array->type->vector_elements; i++) {
101 deref = new(base_ir) ir_dereference_variable(index);
102 condition = new(base_ir) ir_expression(ir_binop_equal,
103 glsl_type::bool_type,
104 deref,
105 new(base_ir) ir_constant(i));
106
107 /* Just clone the rest of the deref chain when trying to get at the
108 * underlying variable.
109 */
110 swizzle = new(base_ir) ir_swizzle(orig_deref->array->clone(NULL),
111 i, 0, 0, 0, 1);
112
113 deref = new(base_ir) ir_dereference_variable(var);
114 assign = new(base_ir) ir_assignment(deref, swizzle, condition);
115 base_ir->insert_before(assign);
116 }
117
118 this->progress = true;
119 return new(base_ir) ir_dereference_variable(var);
120 }
121
122 ir_visitor_status
123 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_expression *ir)
124 {
125 unsigned int i;
126
127 for (i = 0; i < ir->get_num_operands(); i++) {
128 ir->operands[i] = convert_vec_index_to_cond_assign(ir->operands[i]);
129 }
130
131 return visit_continue;
132 }
133
134 ir_visitor_status
135 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_swizzle *ir)
136 {
137 /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
138 * the result of indexing a vector is. But maybe at some point we'll end up
139 * using swizzling of scalars for vector construction.
140 */
141 ir->val = convert_vec_index_to_cond_assign(ir->val);
142
143 return visit_continue;
144 }
145
146 ir_visitor_status
147 ir_vec_index_to_cond_assign_visitor::visit_leave(ir_assignment *ir)
148 {
149 ir_variable *index, *var;
150 ir_dereference_variable *deref;
151 ir_assignment *assign;
152 int i;
153
154 ir->rhs = convert_vec_index_to_cond_assign(ir->rhs);
155 if (ir->condition)
156 ir->condition = convert_vec_index_to_cond_assign(ir->condition);
157
158 /* Last, handle the LHS */
159 ir_dereference_array *orig_deref = ir->lhs->as_dereference_array();
160
161 if (!orig_deref ||
162 orig_deref->array->type->is_matrix() ||
163 orig_deref->array->type->is_array())
164 return visit_continue;
165
166 assert(orig_deref->array_index->type->base_type == GLSL_TYPE_INT);
167
168 /* Store the index to a temporary to avoid reusing its tree. */
169 index = new(ir) ir_variable(glsl_type::int_type, "vec_index_tmp_i");
170 ir->insert_before(index);
171 deref = new(ir) ir_dereference_variable(index);
172 assign = new(ir) ir_assignment(deref, orig_deref->array_index, NULL);
173 ir->insert_before(assign);
174
175 /* Store the RHS to a temporary to avoid reusing its tree. */
176 var = new(ir) ir_variable(ir->rhs->type, "vec_index_tmp_v");
177 ir->insert_before(var);
178 deref = new(ir) ir_dereference_variable(var);
179 assign = new(ir) ir_assignment(deref, ir->rhs, NULL);
180 ir->insert_before(assign);
181
182 /* Generate a conditional move of each vector element to the temp. */
183 for (i = 0; i < orig_deref->array->type->vector_elements; i++) {
184 ir_rvalue *condition, *swizzle;
185
186 deref = new(ir) ir_dereference_variable(index);
187 condition = new(ir) ir_expression(ir_binop_equal,
188 glsl_type::bool_type,
189 deref,
190 new(ir) ir_constant(i));
191
192 /* Just clone the rest of the deref chain when trying to get at the
193 * underlying variable.
194 */
195 swizzle = new(ir) ir_swizzle(orig_deref->array->clone(NULL),
196 i, 0, 0, 0, 1);
197
198 deref = new(ir) ir_dereference_variable(var);
199 assign = new(ir) ir_assignment(swizzle, deref, condition);
200 ir->insert_before(assign);
201 }
202 ir->remove();
203
204 this->progress = true;
205
206 return visit_continue;
207 }
208
209 ir_visitor_status
210 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_call *ir)
211 {
212 foreach_iter(exec_list_iterator, iter, *ir) {
213 ir_rvalue *param = (ir_rvalue *)iter.get();
214 ir_rvalue *new_param = convert_vec_index_to_cond_assign(param);
215
216 if (new_param != param) {
217 param->insert_before(new_param);
218 param->remove();
219 }
220 }
221
222 return visit_continue;
223 }
224
225 ir_visitor_status
226 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_return *ir)
227 {
228 if (ir->value) {
229 ir->value = convert_vec_index_to_cond_assign(ir->value);
230 }
231
232 return visit_continue;
233 }
234
235 ir_visitor_status
236 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_if *ir)
237 {
238 ir->condition = convert_vec_index_to_cond_assign(ir->condition);
239
240 return visit_continue;
241 }
242
243 bool
244 do_vec_index_to_cond_assign(exec_list *instructions)
245 {
246 ir_vec_index_to_cond_assign_visitor v;
247
248 visit_list_elements(&v, instructions);
249
250 return v.progress;
251 }