glsl: Drop the round-trip through ast_type_specifier for many builtin types.
[mesa.git] / src / glsl / opt_tree_grafting.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 opt_tree_grafting.cpp
26 *
27 * Takes assignments to variables that are dereferenced only once and
28 * pastes the RHS expression into where the variable is dereferenced.
29 *
30 * In the process of various operations like function inlining and
31 * tertiary op handling, we'll end up with our expression trees having
32 * been chopped up into a series of assignments of short expressions
33 * to temps. Other passes like ir_algebraic.cpp would prefer to see
34 * the deepest expression trees they can to try to optimize them.
35 *
36 * This is a lot like copy propagaton. In comparison, copy
37 * propagation only acts on plain copies, not arbitrary expressions on
38 * the RHS. Generally, we wouldn't want to go pasting some
39 * complicated expression everywhere it got used, though, so we don't
40 * handle expressions in that pass.
41 *
42 * The hard part is making sure we don't move an expression across
43 * some other assignments that would change the value of the
44 * expression. So we split this into two passes: First, find the
45 * variables in our scope which are written to once and read once, and
46 * then go through basic blocks seeing if we find an opportunity to
47 * move those expressions safely.
48 */
49
50 #include "ir.h"
51 #include "ir_visitor.h"
52 #include "ir_variable_refcount.h"
53 #include "ir_basic_block.h"
54 #include "ir_optimization.h"
55 #include "glsl_types.h"
56
57 static bool debug = false;
58
59 class ir_tree_grafting_visitor : public ir_hierarchical_visitor {
60 public:
61 ir_tree_grafting_visitor(ir_assignment *graft_assign,
62 ir_variable *graft_var)
63 {
64 this->progress = false;
65 this->graft_assign = graft_assign;
66 this->graft_var = graft_var;
67 }
68
69 virtual ir_visitor_status visit_leave(class ir_assignment *);
70 virtual ir_visitor_status visit_enter(class ir_call *);
71 virtual ir_visitor_status visit_enter(class ir_expression *);
72 virtual ir_visitor_status visit_enter(class ir_function *);
73 virtual ir_visitor_status visit_enter(class ir_function_signature *);
74 virtual ir_visitor_status visit_enter(class ir_if *);
75 virtual ir_visitor_status visit_enter(class ir_loop *);
76 virtual ir_visitor_status visit_enter(class ir_swizzle *);
77 virtual ir_visitor_status visit_enter(class ir_texture *);
78
79 ir_visitor_status check_graft(ir_instruction *ir, ir_variable *var);
80
81 bool do_graft(ir_rvalue **rvalue);
82
83 bool progress;
84 ir_variable *graft_var;
85 ir_assignment *graft_assign;
86 };
87
88 struct find_deref_info {
89 ir_variable *var;
90 bool found;
91 };
92
93 void
94 dereferences_variable_callback(ir_instruction *ir, void *data)
95 {
96 struct find_deref_info *info = (struct find_deref_info *)data;
97 ir_dereference_variable *deref = ir->as_dereference_variable();
98
99 if (deref && deref->var == info->var)
100 info->found = true;
101 }
102
103 static bool
104 dereferences_variable(ir_instruction *ir, ir_variable *var)
105 {
106 struct find_deref_info info;
107
108 info.var = var;
109 info.found = false;
110
111 visit_tree(ir, dereferences_variable_callback, &info);
112
113 return info.found;
114 }
115
116 bool
117 ir_tree_grafting_visitor::do_graft(ir_rvalue **rvalue)
118 {
119 if (!*rvalue)
120 return false;
121
122 ir_dereference_variable *deref = (*rvalue)->as_dereference_variable();
123
124 if (!deref || deref->var != this->graft_var)
125 return false;
126
127 if (debug) {
128 printf("GRAFTING:\n");
129 this->graft_assign->print();
130 printf("\n");
131 printf("TO:\n");
132 (*rvalue)->print();
133 printf("\n");
134 }
135
136 this->graft_assign->remove();
137 *rvalue = this->graft_assign->rhs;
138
139 this->progress = true;
140 return true;
141 }
142
143 ir_visitor_status
144 ir_tree_grafting_visitor::visit_enter(ir_loop *ir)
145 {
146 (void)ir;
147 /* Do not traverse into the body of the loop since that is a
148 * different basic block.
149 */
150 return visit_stop;
151 }
152
153 /**
154 * Check if we can continue grafting after writing to a variable. If the
155 * expression we're trying to graft references the variable, we must stop.
156 *
157 * \param ir An instruction that writes to a variable.
158 * \param var The variable being updated.
159 */
160 ir_visitor_status
161 ir_tree_grafting_visitor::check_graft(ir_instruction *ir, ir_variable *var)
162 {
163 if (dereferences_variable(this->graft_assign->rhs, var)) {
164 if (debug) {
165 printf("graft killed by: ");
166 ir->print();
167 printf("\n");
168 }
169 return visit_stop;
170 }
171
172 return visit_continue;
173 }
174
175 ir_visitor_status
176 ir_tree_grafting_visitor::visit_leave(ir_assignment *ir)
177 {
178 if (do_graft(&ir->rhs) ||
179 do_graft(&ir->condition))
180 return visit_stop;
181
182 /* If this assignment updates a variable used in the assignment
183 * we're trying to graft, then we're done.
184 */
185 return check_graft(ir, ir->lhs->variable_referenced());
186 }
187
188 ir_visitor_status
189 ir_tree_grafting_visitor::visit_enter(ir_function *ir)
190 {
191 (void) ir;
192 return visit_continue_with_parent;
193 }
194
195 ir_visitor_status
196 ir_tree_grafting_visitor::visit_enter(ir_function_signature *ir)
197 {
198 (void)ir;
199 return visit_continue_with_parent;
200 }
201
202 ir_visitor_status
203 ir_tree_grafting_visitor::visit_enter(ir_call *ir)
204 {
205 exec_list_iterator sig_iter = ir->callee->parameters.iterator();
206 /* Reminder: iterating ir_call iterates its parameters. */
207 foreach_iter(exec_list_iterator, iter, *ir) {
208 ir_variable *sig_param = (ir_variable *)sig_iter.get();
209 ir_rvalue *ir = (ir_rvalue *)iter.get();
210 ir_rvalue *new_ir = ir;
211
212 if (sig_param->mode != ir_var_in && sig_param->mode != ir_var_const_in) {
213 if (check_graft(ir, sig_param) == visit_stop)
214 return visit_stop;
215 continue;
216 }
217
218 if (do_graft(&new_ir)) {
219 ir->replace_with(new_ir);
220 return visit_stop;
221 }
222 sig_iter.next();
223 }
224
225 if (ir->return_deref && check_graft(ir, ir->return_deref->var) == visit_stop)
226 return visit_stop;
227
228 return visit_continue;
229 }
230
231 ir_visitor_status
232 ir_tree_grafting_visitor::visit_enter(ir_expression *ir)
233 {
234 for (unsigned int i = 0; i < ir->get_num_operands(); i++) {
235 if (do_graft(&ir->operands[i]))
236 return visit_stop;
237 }
238
239 return visit_continue;
240 }
241
242 ir_visitor_status
243 ir_tree_grafting_visitor::visit_enter(ir_if *ir)
244 {
245 if (do_graft(&ir->condition))
246 return visit_stop;
247
248 /* Do not traverse into the body of the if-statement since that is a
249 * different basic block.
250 */
251 return visit_continue_with_parent;
252 }
253
254 ir_visitor_status
255 ir_tree_grafting_visitor::visit_enter(ir_swizzle *ir)
256 {
257 if (do_graft(&ir->val))
258 return visit_stop;
259
260 return visit_continue;
261 }
262
263 ir_visitor_status
264 ir_tree_grafting_visitor::visit_enter(ir_texture *ir)
265 {
266 if (do_graft(&ir->coordinate) ||
267 do_graft(&ir->projector) ||
268 do_graft(&ir->offset) ||
269 do_graft(&ir->shadow_comparitor))
270 return visit_stop;
271
272 switch (ir->op) {
273 case ir_tex:
274 break;
275 case ir_txb:
276 if (do_graft(&ir->lod_info.bias))
277 return visit_stop;
278 break;
279 case ir_txf:
280 case ir_txl:
281 case ir_txs:
282 if (do_graft(&ir->lod_info.lod))
283 return visit_stop;
284 break;
285 case ir_txd:
286 if (do_graft(&ir->lod_info.grad.dPdx) ||
287 do_graft(&ir->lod_info.grad.dPdy))
288 return visit_stop;
289 break;
290 }
291
292 return visit_continue;
293 }
294
295 struct tree_grafting_info {
296 ir_variable_refcount_visitor *refs;
297 bool progress;
298 };
299
300 static bool
301 try_tree_grafting(ir_assignment *start,
302 ir_variable *lhs_var,
303 ir_instruction *bb_last)
304 {
305 ir_tree_grafting_visitor v(start, lhs_var);
306
307 if (debug) {
308 printf("trying to graft: ");
309 lhs_var->print();
310 printf("\n");
311 }
312
313 for (ir_instruction *ir = (ir_instruction *)start->next;
314 ir != bb_last->next;
315 ir = (ir_instruction *)ir->next) {
316
317 if (debug) {
318 printf("- ");
319 ir->print();
320 printf("\n");
321 }
322
323 ir_visitor_status s = ir->accept(&v);
324 if (s == visit_stop)
325 return v.progress;
326 }
327
328 return false;
329 }
330
331 static void
332 tree_grafting_basic_block(ir_instruction *bb_first,
333 ir_instruction *bb_last,
334 void *data)
335 {
336 struct tree_grafting_info *info = (struct tree_grafting_info *)data;
337 ir_instruction *ir, *next;
338
339 for (ir = bb_first, next = (ir_instruction *)ir->next;
340 ir != bb_last->next;
341 ir = next, next = (ir_instruction *)ir->next) {
342 ir_assignment *assign = ir->as_assignment();
343
344 if (!assign)
345 continue;
346
347 ir_variable *lhs_var = assign->whole_variable_written();
348 if (!lhs_var)
349 continue;
350
351 if (lhs_var->mode == ir_var_out ||
352 lhs_var->mode == ir_var_inout)
353 continue;
354
355 ir_variable_refcount_entry *entry = info->refs->get_variable_entry(lhs_var);
356
357 if (!entry->declaration ||
358 entry->assigned_count != 1 ||
359 entry->referenced_count != 2)
360 continue;
361
362 assert(assign == entry->assign);
363
364 /* Found a possibly graftable assignment. Now, walk through the
365 * rest of the BB seeing if the deref is here, and if nothing interfered with
366 * pasting its expression's values in between.
367 */
368 info->progress |= try_tree_grafting(assign, lhs_var, bb_last);
369 }
370 }
371
372 /**
373 * Does a copy propagation pass on the code present in the instruction stream.
374 */
375 bool
376 do_tree_grafting(exec_list *instructions)
377 {
378 ir_variable_refcount_visitor refs;
379 struct tree_grafting_info info;
380
381 info.progress = false;
382 info.refs = &refs;
383
384 visit_list_elements(info.refs, instructions);
385
386 call_for_basic_blocks(instructions, tree_grafting_basic_block, &info);
387
388 return info.progress;
389 }