Merge branch 'lp-offset-twoside'
[mesa.git] / src / glsl / lower_jumps.cpp
1 /*
2 * Copyright © 2010 Luca Barbieri
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_jumps.cpp
26 */
27
28 #include "glsl_types.h"
29 #include <string.h>
30 #include "ir.h"
31
32 enum jump_strength
33 {
34 strength_none,
35 strength_always_clears_execute_flag,
36 strength_continue,
37 strength_break,
38 strength_return,
39 strength_discard
40 };
41
42 struct block_record
43 {
44 /* minimum jump strength (of lowered IR, not pre-lowering IR)
45 *
46 * If the block ends with a jump, must be the strength of the jump.
47 * Otherwise, the jump would be dead and have been deleted before)
48 *
49 * If the block doesn't end with a jump, it can be different than strength_none if all paths before it lead to some jump
50 * (e.g. an if with a return in one branch, and a break in the other, while not lowering them)
51 * Note that identical jumps are usually unified though.
52 */
53 jump_strength min_strength;
54
55 /* can anything clear the execute flag? */
56 bool may_clear_execute_flag;
57
58 block_record()
59 {
60 this->min_strength = strength_none;
61 this->may_clear_execute_flag = false;
62 }
63 };
64
65 struct loop_record
66 {
67 ir_function_signature* signature;
68 ir_loop* loop;
69
70 /* used to avoid lowering the break used to represent lowered breaks */
71 unsigned nesting_depth;
72 bool in_if_at_the_end_of_the_loop;
73
74 bool may_set_return_flag;
75
76 ir_variable* break_flag;
77 ir_variable* execute_flag; /* cleared to emulate continue */
78
79 loop_record(ir_function_signature* p_signature = 0, ir_loop* p_loop = 0)
80 {
81 this->signature = p_signature;
82 this->loop = p_loop;
83 this->nesting_depth = 0;
84 this->in_if_at_the_end_of_the_loop = false;
85 this->may_set_return_flag = false;
86 this->break_flag = 0;
87 this->execute_flag = 0;
88 }
89
90 ir_variable* get_execute_flag()
91 {
92 /* also supported for the "function loop" */
93 if(!this->execute_flag) {
94 exec_list& list = this->loop ? this->loop->body_instructions : signature->body;
95 this->execute_flag = new(this->signature) ir_variable(glsl_type::bool_type, "execute_flag", ir_var_temporary);
96 list.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(execute_flag), new(this->signature) ir_constant(true), 0));
97 list.push_head(this->execute_flag);
98 }
99 return this->execute_flag;
100 }
101
102 ir_variable* get_break_flag()
103 {
104 assert(this->loop);
105 if(!this->break_flag) {
106 this->break_flag = new(this->signature) ir_variable(glsl_type::bool_type, "break_flag", ir_var_temporary);
107 this->loop->insert_before(this->break_flag);
108 this->loop->insert_before(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(break_flag), new(this->signature) ir_constant(false), 0));
109 }
110 return this->break_flag;
111 }
112 };
113
114 struct function_record
115 {
116 ir_function_signature* signature;
117 ir_variable* return_flag; /* used to break out of all loops and then jump to the return instruction */
118 ir_variable* return_value;
119 bool is_main;
120 unsigned nesting_depth;
121
122 function_record(ir_function_signature* p_signature = 0)
123 {
124 this->signature = p_signature;
125 this->return_flag = 0;
126 this->return_value = 0;
127 this->nesting_depth = 0;
128 this->is_main = this->signature && (strcmp(this->signature->function_name(), "main") == 0);
129 }
130
131 ir_variable* get_return_flag()
132 {
133 if(!this->return_flag) {
134 this->return_flag = new(this->signature) ir_variable(glsl_type::bool_type, "return_flag", ir_var_temporary);
135 this->signature->body.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(return_flag), new(this->signature) ir_constant(false), 0));
136 this->signature->body.push_head(this->return_flag);
137 }
138 return this->return_flag;
139 }
140
141 ir_variable* get_return_value()
142 {
143 if(!this->return_value) {
144 assert(!this->signature->return_type->is_void());
145 return_value = new(this->signature) ir_variable(this->signature->return_type, "return_value", ir_var_temporary);
146 this->signature->body.push_head(this->return_value);
147 }
148 return this->return_value;
149 }
150 };
151
152 struct ir_lower_jumps_visitor : public ir_control_flow_visitor {
153 bool progress;
154
155 struct function_record function;
156 struct loop_record loop;
157 struct block_record block;
158
159 bool pull_out_jumps;
160 bool lower_continue;
161 bool lower_break;
162 bool lower_sub_return;
163 bool lower_main_return;
164
165 ir_lower_jumps_visitor()
166 {
167 this->progress = false;
168 }
169
170 void truncate_after_instruction(exec_node *ir)
171 {
172 if (!ir)
173 return;
174
175 while (!ir->get_next()->is_tail_sentinel()) {
176 ((ir_instruction *)ir->get_next())->remove();
177 this->progress = true;
178 }
179 }
180
181 void move_outer_block_inside(ir_instruction *ir, exec_list *inner_block)
182 {
183 while (!ir->get_next()->is_tail_sentinel()) {
184 ir_instruction *move_ir = (ir_instruction *)ir->get_next();
185
186 move_ir->remove();
187 inner_block->push_tail(move_ir);
188 }
189 }
190
191 virtual void visit(class ir_loop_jump * ir)
192 {
193 truncate_after_instruction(ir);
194 this->block.min_strength = ir->is_break() ? strength_break : strength_continue;
195 }
196
197 virtual void visit(class ir_return * ir)
198 {
199 truncate_after_instruction(ir);
200 this->block.min_strength = strength_return;
201 }
202
203 virtual void visit(class ir_discard * ir)
204 {
205 truncate_after_instruction(ir);
206 this->block.min_strength = strength_discard;
207 }
208
209 enum jump_strength get_jump_strength(ir_instruction* ir)
210 {
211 if(!ir)
212 return strength_none;
213 else if(ir->ir_type == ir_type_loop_jump) {
214 if(((ir_loop_jump*)ir)->is_break())
215 return strength_break;
216 else
217 return strength_continue;
218 } else if(ir->ir_type == ir_type_return)
219 return strength_return;
220 else if(ir->ir_type == ir_type_discard)
221 return strength_discard;
222 else
223 return strength_none;
224 }
225
226 bool should_lower_jump(ir_jump* ir)
227 {
228 unsigned strength = get_jump_strength(ir);
229 bool lower;
230 switch(strength)
231 {
232 case strength_none:
233 lower = false; /* don't change this, code relies on it */
234 break;
235 case strength_continue:
236 lower = lower_continue;
237 break;
238 case strength_break:
239 assert(this->loop.loop);
240 /* never lower "canonical break" */
241 if(ir->get_next()->is_tail_sentinel() && (this->loop.nesting_depth == 0
242 || (this->loop.nesting_depth == 1 && this->loop.in_if_at_the_end_of_the_loop)))
243 lower = false;
244 else
245 lower = lower_break;
246 break;
247 case strength_return:
248 /* never lower return at the end of a this->function */
249 if(this->function.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
250 lower = false;
251 else if (this->function.is_main)
252 lower = lower_main_return;
253 else
254 lower = lower_sub_return;
255 break;
256 case strength_discard:
257 lower = false; /* probably nothing needs this lowered */
258 break;
259 }
260 return lower;
261 }
262
263 block_record visit_block(exec_list* list)
264 {
265 block_record saved_block = this->block;
266 this->block = block_record();
267 visit_exec_list(list, this);
268 block_record ret = this->block;
269 this->block = saved_block;
270 return ret;
271 }
272
273 virtual void visit(ir_if *ir)
274 {
275 if(this->loop.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
276 this->loop.in_if_at_the_end_of_the_loop = true;
277
278 ++this->function.nesting_depth;
279 ++this->loop.nesting_depth;
280
281 block_record block_records[2];
282 ir_jump* jumps[2];
283
284 block_records[0] = visit_block(&ir->then_instructions);
285 block_records[1] = visit_block(&ir->else_instructions);
286
287 retry: /* we get here if we put code after the if inside a branch */
288 for(unsigned i = 0; i < 2; ++i) {
289 exec_list& list = i ? ir->else_instructions : ir->then_instructions;
290 jumps[i] = 0;
291 if(!list.is_empty() && get_jump_strength((ir_instruction*)list.get_tail()))
292 jumps[i] = (ir_jump*)list.get_tail();
293 }
294
295 for(;;) {
296 jump_strength jump_strengths[2];
297
298 for(unsigned i = 0; i < 2; ++i) {
299 if(jumps[i]) {
300 jump_strengths[i] = block_records[i].min_strength;
301 assert(jump_strengths[i] == get_jump_strength(jumps[i]));
302 } else
303 jump_strengths[i] = strength_none;
304 }
305
306 /* move both jumps out if possible */
307 if(pull_out_jumps && jump_strengths[0] == jump_strengths[1]) {
308 bool unify = true;
309 if(jump_strengths[0] == strength_continue)
310 ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_continue));
311 else if(jump_strengths[0] == strength_break)
312 ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
313 /* FINISHME: unify returns with identical expressions */
314 else if(jump_strengths[0] == strength_return && this->function.signature->return_type->is_void())
315 ir->insert_after(new(ir) ir_return(NULL));
316 /* FINISHME: unify discards */
317 else
318 unify = false;
319
320 if(unify) {
321 jumps[0]->remove();
322 jumps[1]->remove();
323 this->progress = true;
324
325 jumps[0] = 0;
326 jumps[1] = 0;
327 block_records[0].min_strength = strength_none;
328 block_records[1].min_strength = strength_none;
329 break;
330 }
331 }
332
333 /* lower a jump: if both need to lowered, start with the strongest one, so that
334 * we might later unify the lowered version with the other one
335 */
336 bool should_lower[2];
337 for(unsigned i = 0; i < 2; ++i)
338 should_lower[i] = should_lower_jump(jumps[i]);
339
340 int lower;
341 if(should_lower[1] && should_lower[0])
342 lower = jump_strengths[1] > jump_strengths[0];
343 else if(should_lower[0])
344 lower = 0;
345 else if(should_lower[1])
346 lower = 1;
347 else
348 break;
349
350 if(jump_strengths[lower] == strength_return) {
351 ir_variable* return_flag = this->function.get_return_flag();
352 if(!this->function.signature->return_type->is_void()) {
353 ir_variable* return_value = this->function.get_return_value();
354 jumps[lower]->insert_before(new(ir) ir_assignment(new (ir) ir_dereference_variable(return_value), ((ir_return*)jumps[lower])->value, NULL));
355 }
356 jumps[lower]->insert_before(new(ir) ir_assignment(new (ir) ir_dereference_variable(return_flag), new (ir) ir_constant(true), NULL));
357 this->loop.may_set_return_flag = true;
358 if(this->loop.loop) {
359 ir_loop_jump* lowered = 0;
360 lowered = new(ir) ir_loop_jump(ir_loop_jump::jump_break);
361 block_records[lower].min_strength = strength_break;
362 jumps[lower]->replace_with(lowered);
363 jumps[lower] = lowered;
364 } else
365 goto lower_continue;
366 this->progress = true;
367 } else if(jump_strengths[lower] == strength_break) {
368 /* We can't lower to an actual continue because that would execute the increment.
369 *
370 * In the lowered code, we instead put the break check between the this->loop body and the increment,
371 * which is impossible with a real continue as defined by the GLSL IR currently.
372 *
373 * Smarter options (such as undoing the increment) are possible but it's not worth implementing them,
374 * because if break is lowered, continue is almost surely lowered too.
375 */
376 jumps[lower]->insert_before(new(ir) ir_assignment(new (ir) ir_dereference_variable(this->loop.get_break_flag()), new (ir) ir_constant(true), 0));
377 goto lower_continue;
378 } else if(jump_strengths[lower] == strength_continue) {
379 lower_continue:
380 ir_variable* execute_flag = this->loop.get_execute_flag();
381 jumps[lower]->replace_with(new(ir) ir_assignment(new (ir) ir_dereference_variable(execute_flag), new (ir) ir_constant(false), 0));
382 jumps[lower] = 0;
383 block_records[lower].min_strength = strength_always_clears_execute_flag;
384 block_records[lower].may_clear_execute_flag = true;
385 this->progress = true;
386 break;
387 }
388 }
389
390 /* move out a jump out if possible */
391 if(pull_out_jumps) {
392 int move_out = -1;
393 if(jumps[0] && block_records[1].min_strength >= strength_continue)
394 move_out = 0;
395 else if(jumps[1] && block_records[0].min_strength >= strength_continue)
396 move_out = 1;
397
398 if(move_out >= 0)
399 {
400 jumps[move_out]->remove();
401 ir->insert_after(jumps[move_out]);
402 jumps[move_out] = 0;
403 block_records[move_out].min_strength = strength_none;
404 this->progress = true;
405 }
406 }
407
408 if(block_records[0].min_strength < block_records[1].min_strength)
409 this->block.min_strength = block_records[0].min_strength;
410 else
411 this->block.min_strength = block_records[1].min_strength;
412 this->block.may_clear_execute_flag = this->block.may_clear_execute_flag || block_records[0].may_clear_execute_flag || block_records[1].may_clear_execute_flag;
413
414 if(this->block.min_strength)
415 truncate_after_instruction(ir);
416 else if(this->block.may_clear_execute_flag)
417 {
418 int move_into = -1;
419 if(block_records[0].min_strength && !block_records[1].may_clear_execute_flag)
420 move_into = 1;
421 else if(block_records[1].min_strength && !block_records[0].may_clear_execute_flag)
422 move_into = 0;
423
424 if(move_into >= 0) {
425 assert(!block_records[move_into].min_strength && !block_records[move_into].may_clear_execute_flag); /* otherwise, we just truncated */
426
427 exec_list* list = move_into ? &ir->else_instructions : &ir->then_instructions;
428 exec_node* next = ir->get_next();
429 if(!next->is_tail_sentinel()) {
430 move_outer_block_inside(ir, list);
431
432 exec_list list;
433 list.head = next;
434 block_records[move_into] = visit_block(&list);
435
436 this->progress = true;
437 goto retry;
438 }
439 } else {
440 ir_instruction* ir_after;
441 for(ir_after = (ir_instruction*)ir->get_next(); !ir_after->is_tail_sentinel();)
442 {
443 ir_if* ir_if = ir_after->as_if();
444 if(ir_if && ir_if->else_instructions.is_empty()) {
445 ir_dereference_variable* ir_if_cond_deref = ir_if->condition->as_dereference_variable();
446 if(ir_if_cond_deref && ir_if_cond_deref->var == this->loop.execute_flag) {
447 ir_instruction* ir_next = (ir_instruction*)ir_after->get_next();
448 ir_after->insert_before(&ir_if->then_instructions);
449 ir_after->remove();
450 ir_after = ir_next;
451 continue;
452 }
453 }
454 ir_after = (ir_instruction*)ir_after->get_next();
455
456 /* only set this if we find any unprotected instruction */
457 this->progress = true;
458 }
459
460 if(!ir->get_next()->is_tail_sentinel()) {
461 assert(this->loop.execute_flag);
462 ir_if* if_execute = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.execute_flag));
463 move_outer_block_inside(ir, &if_execute->then_instructions);
464 ir->insert_after(if_execute);
465 }
466 }
467 }
468 --this->loop.nesting_depth;
469 --this->function.nesting_depth;
470 }
471
472 virtual void visit(ir_loop *ir)
473 {
474 ++this->function.nesting_depth;
475 loop_record saved_loop = this->loop;
476 this->loop = loop_record(this->function.signature, ir);
477
478 block_record body = visit_block(&ir->body_instructions);
479
480 if(body.min_strength >= strength_break) {
481 /* FINISHME: turn the this->loop into an if, or replace it with its body */
482 }
483
484 if(this->loop.break_flag) {
485 ir_if* break_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.break_flag));
486 break_if->then_instructions.push_tail(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
487 ir->body_instructions.push_tail(break_if);
488 }
489
490 if(this->loop.may_set_return_flag) {
491 assert(this->function.return_flag);
492 ir_if* return_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->function.return_flag));
493 return_if->then_instructions.push_tail(new(ir) ir_loop_jump(saved_loop.loop ? ir_loop_jump::jump_break : ir_loop_jump::jump_continue));
494 ir->insert_after(return_if);
495 }
496
497 this->loop = saved_loop;
498 --this->function.nesting_depth;
499 }
500
501 virtual void visit(ir_function_signature *ir)
502 {
503 /* these are not strictly necessary */
504 assert(!this->function.signature);
505 assert(!this->loop.loop);
506
507 function_record saved_function = this->function;
508 loop_record saved_loop = this->loop;
509 this->function = function_record(ir);
510 this->loop = loop_record(ir);
511
512 assert(!this->loop.loop);
513 visit_block(&ir->body);
514
515 if(this->function.return_value)
516 ir->body.push_tail(new(ir) ir_return(new (ir) ir_dereference_variable(this->function.return_value)));
517
518 this->loop = saved_loop;
519 this->function = saved_function;
520 }
521
522 virtual void visit(class ir_function * ir)
523 {
524 visit_block(&ir->signatures);
525 }
526 };
527
528 bool
529 do_lower_jumps(exec_list *instructions, bool pull_out_jumps, bool lower_sub_return, bool lower_main_return, bool lower_continue, bool lower_break)
530 {
531 ir_lower_jumps_visitor v;
532 v.pull_out_jumps = pull_out_jumps;
533 v.lower_continue = lower_continue;
534 v.lower_break = lower_break;
535 v.lower_sub_return = lower_sub_return;
536 v.lower_main_return = lower_main_return;
537
538 do {
539 v.progress = false;
540 visit_exec_list(instructions, &v);
541 } while (v.progress);
542
543 return v.progress;
544 }