glsl: Return ir_variable from compare_index_block
[mesa.git] / src / compiler / glsl / lower_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 lower_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 "compiler/glsl_types.h"
43
44 namespace {
45
46 /**
47 * Visitor class for replacing expressions with ir_constant values.
48 */
49
50 class ir_vec_index_to_cond_assign_visitor : public ir_hierarchical_visitor {
51 public:
52 ir_vec_index_to_cond_assign_visitor()
53 : progress(false)
54 {
55 /* empty */
56 }
57
58 ir_rvalue *convert_vec_index_to_cond_assign(void *mem_ctx,
59 ir_rvalue *orig_vector,
60 ir_rvalue *orig_index,
61 const glsl_type *type);
62
63 ir_rvalue *convert_vector_extract_to_cond_assign(ir_rvalue *ir);
64
65 virtual ir_visitor_status visit_enter(ir_expression *);
66 virtual ir_visitor_status visit_enter(ir_swizzle *);
67 virtual ir_visitor_status visit_leave(ir_assignment *);
68 virtual ir_visitor_status visit_enter(ir_return *);
69 virtual ir_visitor_status visit_enter(ir_call *);
70 virtual ir_visitor_status visit_enter(ir_if *);
71
72 bool progress;
73 };
74
75 } /* anonymous namespace */
76
77 ir_rvalue *
78 ir_vec_index_to_cond_assign_visitor::convert_vec_index_to_cond_assign(void *mem_ctx,
79 ir_rvalue *orig_vector,
80 ir_rvalue *orig_index,
81 const glsl_type *type)
82 {
83 ir_assignment *assign, *value_assign;
84 ir_variable *index, *var, *value;
85 ir_dereference *deref, *deref_value;
86 unsigned i;
87
88
89 exec_list list;
90
91 /* Store the index to a temporary to avoid reusing its tree. */
92 assert(orig_index->type == glsl_type::int_type ||
93 orig_index->type == glsl_type::uint_type);
94 index = new(base_ir) ir_variable(orig_index->type,
95 "vec_index_tmp_i",
96 ir_var_temporary);
97 list.push_tail(index);
98 deref = new(base_ir) ir_dereference_variable(index);
99 assign = new(base_ir) ir_assignment(deref, orig_index, NULL);
100 list.push_tail(assign);
101
102 /* Store the value inside a temp, thus avoiding matrixes duplication */
103 value = new(base_ir) ir_variable(orig_vector->type, "vec_value_tmp",
104 ir_var_temporary);
105 list.push_tail(value);
106 deref_value = new(base_ir) ir_dereference_variable(value);
107 value_assign = new(base_ir) ir_assignment(deref_value, orig_vector);
108 list.push_tail(value_assign);
109
110 /* Temporary where we store whichever value we swizzle out. */
111 var = new(base_ir) ir_variable(type, "vec_index_tmp_v",
112 ir_var_temporary);
113 list.push_tail(var);
114
115 /* Generate a single comparison condition "mask" for all of the components
116 * in the vector.
117 */
118 ir_variable *const cond =
119 compare_index_block(&list, index, 0,
120 orig_vector->type->vector_elements,
121 mem_ctx);
122
123 /* Generate a conditional move of each vector element to the temp. */
124 for (i = 0; i < orig_vector->type->vector_elements; i++) {
125 ir_rvalue *condition_swizzle =
126 new(base_ir) ir_swizzle(new(mem_ctx) ir_dereference_variable(cond),
127 i, 0, 0, 0, 1);
128
129 /* Just clone the rest of the deref chain when trying to get at the
130 * underlying variable.
131 */
132 ir_rvalue *swizzle =
133 new(base_ir) ir_swizzle(deref_value->clone(mem_ctx, NULL),
134 i, 0, 0, 0, 1);
135
136 deref = new(base_ir) ir_dereference_variable(var);
137 assign = new(base_ir) ir_assignment(deref, swizzle, condition_swizzle);
138 list.push_tail(assign);
139 }
140
141 /* Put all of the new instructions in the IR stream before the old
142 * instruction.
143 */
144 base_ir->insert_before(&list);
145
146 this->progress = true;
147 return new(base_ir) ir_dereference_variable(var);
148 }
149
150 ir_rvalue *
151 ir_vec_index_to_cond_assign_visitor::convert_vector_extract_to_cond_assign(ir_rvalue *ir)
152 {
153 ir_expression *const expr = ir->as_expression();
154
155 if (expr == NULL || expr->operation != ir_binop_vector_extract)
156 return ir;
157
158 return convert_vec_index_to_cond_assign(ralloc_parent(ir),
159 expr->operands[0],
160 expr->operands[1],
161 ir->type);
162 }
163
164 ir_visitor_status
165 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_expression *ir)
166 {
167 for (unsigned i = 0; i < ir->num_operands; i++)
168 ir->operands[i] = convert_vector_extract_to_cond_assign(ir->operands[i]);
169
170 return visit_continue;
171 }
172
173 ir_visitor_status
174 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_swizzle *ir)
175 {
176 /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
177 * the result of indexing a vector is. But maybe at some point we'll end up
178 * using swizzling of scalars for vector construction.
179 */
180 ir->val = convert_vector_extract_to_cond_assign(ir->val);
181
182 return visit_continue;
183 }
184
185 ir_visitor_status
186 ir_vec_index_to_cond_assign_visitor::visit_leave(ir_assignment *ir)
187 {
188 ir->rhs = convert_vector_extract_to_cond_assign(ir->rhs);
189
190 if (ir->condition)
191 ir->condition = convert_vector_extract_to_cond_assign(ir->condition);
192
193 return visit_continue;
194 }
195
196 ir_visitor_status
197 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_call *ir)
198 {
199 foreach_in_list_safe(ir_rvalue, param, &ir->actual_parameters) {
200 ir_rvalue *new_param = convert_vector_extract_to_cond_assign(param);
201
202 if (new_param != param) {
203 param->replace_with(new_param);
204 }
205 }
206
207 return visit_continue;
208 }
209
210 ir_visitor_status
211 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_return *ir)
212 {
213 if (ir->value)
214 ir->value = convert_vector_extract_to_cond_assign(ir->value);
215
216 return visit_continue;
217 }
218
219 ir_visitor_status
220 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_if *ir)
221 {
222 ir->condition = convert_vector_extract_to_cond_assign(ir->condition);
223
224 return visit_continue;
225 }
226
227 bool
228 do_vec_index_to_cond_assign(exec_list *instructions)
229 {
230 ir_vec_index_to_cond_assign_visitor v;
231
232 visit_list_elements(&v, instructions);
233
234 return v.progress;
235 }