2 * Copyright © 2010 Luca Barbieri
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:
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
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.
25 * \file lower_jumps.cpp
27 * This pass lowers jumps (break, continue, and return) to if/else structures.
30 * 1. Pull jumps out of ifs where possible
31 * 2. Remove all "continue"s, replacing them with an "execute flag"
32 * 3. Replace all "break" with a single conditional one at the end of the loop
33 * 4. Replace all "return"s with a single return at the end of the function,
34 * for the main function and/or other functions
36 * Applying this pass gives several benefits:
37 * 1. All functions can be inlined.
38 * 2. nv40 and other pre-DX10 chips without "continue" can be supported
39 * 3. nv30 and other pre-DX10 chips with no control flow at all are better
42 * Continues are lowered by adding a per-loop "execute flag", initialized to
43 * true, that when cleared inhibits all execution until the end of the loop.
45 * Breaks are lowered to continues, plus setting a "break flag" that is checked
46 * at the end of the loop, and trigger the unique "break".
48 * Returns are lowered to breaks/continues, plus adding a "return flag" that
49 * causes loops to break again out of their enclosing loops until all the
50 * loops are exited: then the "execute flag" logic will ignore everything
51 * until the end of the function.
53 * Note that "continue" and "return" can also be implemented by adding
54 * a dummy loop and using break.
55 * However, this is bad for hardware with limited nesting depth, and
56 * prevents further optimization, and thus is not currently performed.
59 #include "glsl_types.h"
66 strength_always_clears_execute_flag
,
74 /* minimum jump strength (of lowered IR, not pre-lowering IR)
76 * If the block ends with a jump, must be the strength of the jump.
77 * Otherwise, the jump would be dead and have been deleted before)
79 * 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
80 * (e.g. an if with a return in one branch, and a break in the other, while not lowering them)
81 * Note that identical jumps are usually unified though.
83 jump_strength min_strength
;
85 /* can anything clear the execute flag? */
86 bool may_clear_execute_flag
;
90 this->min_strength
= strength_none
;
91 this->may_clear_execute_flag
= false;
97 ir_function_signature
* signature
;
100 /* used to avoid lowering the break used to represent lowered breaks */
101 unsigned nesting_depth
;
102 bool in_if_at_the_end_of_the_loop
;
104 bool may_set_return_flag
;
106 ir_variable
* break_flag
;
107 ir_variable
* execute_flag
; /* cleared to emulate continue */
109 loop_record(ir_function_signature
* p_signature
= 0, ir_loop
* p_loop
= 0)
111 this->signature
= p_signature
;
113 this->nesting_depth
= 0;
114 this->in_if_at_the_end_of_the_loop
= false;
115 this->may_set_return_flag
= false;
116 this->break_flag
= 0;
117 this->execute_flag
= 0;
120 ir_variable
* get_execute_flag()
122 /* also supported for the "function loop" */
123 if(!this->execute_flag
) {
124 exec_list
& list
= this->loop
? this->loop
->body_instructions
: signature
->body
;
125 this->execute_flag
= new(this->signature
) ir_variable(glsl_type::bool_type
, "execute_flag", ir_var_temporary
);
126 list
.push_head(new(this->signature
) ir_assignment(new(this->signature
) ir_dereference_variable(execute_flag
), new(this->signature
) ir_constant(true), 0));
127 list
.push_head(this->execute_flag
);
129 return this->execute_flag
;
132 ir_variable
* get_break_flag()
135 if(!this->break_flag
) {
136 this->break_flag
= new(this->signature
) ir_variable(glsl_type::bool_type
, "break_flag", ir_var_temporary
);
137 this->loop
->insert_before(this->break_flag
);
138 this->loop
->insert_before(new(this->signature
) ir_assignment(new(this->signature
) ir_dereference_variable(break_flag
), new(this->signature
) ir_constant(false), 0));
140 return this->break_flag
;
144 struct function_record
146 ir_function_signature
* signature
;
147 ir_variable
* return_flag
; /* used to break out of all loops and then jump to the return instruction */
148 ir_variable
* return_value
;
150 unsigned nesting_depth
;
152 function_record(ir_function_signature
* p_signature
= 0)
154 this->signature
= p_signature
;
155 this->return_flag
= 0;
156 this->return_value
= 0;
157 this->nesting_depth
= 0;
158 this->is_main
= this->signature
&& (strcmp(this->signature
->function_name(), "main") == 0);
161 ir_variable
* get_return_flag()
163 if(!this->return_flag
) {
164 this->return_flag
= new(this->signature
) ir_variable(glsl_type::bool_type
, "return_flag", ir_var_temporary
);
165 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));
166 this->signature
->body
.push_head(this->return_flag
);
168 return this->return_flag
;
171 ir_variable
* get_return_value()
173 if(!this->return_value
) {
174 assert(!this->signature
->return_type
->is_void());
175 return_value
= new(this->signature
) ir_variable(this->signature
->return_type
, "return_value", ir_var_temporary
);
176 this->signature
->body
.push_head(this->return_value
);
178 return this->return_value
;
182 struct ir_lower_jumps_visitor
: public ir_control_flow_visitor
{
185 struct function_record function
;
186 struct loop_record loop
;
187 struct block_record block
;
192 bool lower_sub_return
;
193 bool lower_main_return
;
195 ir_lower_jumps_visitor()
197 this->progress
= false;
200 void truncate_after_instruction(exec_node
*ir
)
205 while (!ir
->get_next()->is_tail_sentinel()) {
206 ((ir_instruction
*)ir
->get_next())->remove();
207 this->progress
= true;
211 void move_outer_block_inside(ir_instruction
*ir
, exec_list
*inner_block
)
213 while (!ir
->get_next()->is_tail_sentinel()) {
214 ir_instruction
*move_ir
= (ir_instruction
*)ir
->get_next();
217 inner_block
->push_tail(move_ir
);
221 virtual void visit(class ir_loop_jump
* ir
)
223 truncate_after_instruction(ir
);
224 this->block
.min_strength
= ir
->is_break() ? strength_break
: strength_continue
;
227 virtual void visit(class ir_return
* ir
)
229 truncate_after_instruction(ir
);
230 this->block
.min_strength
= strength_return
;
233 virtual void visit(class ir_discard
* ir
)
237 enum jump_strength
get_jump_strength(ir_instruction
* ir
)
240 return strength_none
;
241 else if(ir
->ir_type
== ir_type_loop_jump
) {
242 if(((ir_loop_jump
*)ir
)->is_break())
243 return strength_break
;
245 return strength_continue
;
246 } else if(ir
->ir_type
== ir_type_return
)
247 return strength_return
;
249 return strength_none
;
252 bool should_lower_jump(ir_jump
* ir
)
254 unsigned strength
= get_jump_strength(ir
);
259 lower
= false; /* don't change this, code relies on it */
261 case strength_continue
:
262 lower
= lower_continue
;
265 assert(this->loop
.loop
);
266 /* never lower "canonical break" */
267 if(ir
->get_next()->is_tail_sentinel() && (this->loop
.nesting_depth
== 0
268 || (this->loop
.nesting_depth
== 1 && this->loop
.in_if_at_the_end_of_the_loop
)))
273 case strength_return
:
274 /* never lower return at the end of a this->function */
275 if(this->function
.nesting_depth
== 0 && ir
->get_next()->is_tail_sentinel())
277 else if (this->function
.is_main
)
278 lower
= lower_main_return
;
280 lower
= lower_sub_return
;
286 block_record
visit_block(exec_list
* list
)
288 block_record saved_block
= this->block
;
289 this->block
= block_record();
290 visit_exec_list(list
, this);
291 block_record ret
= this->block
;
292 this->block
= saved_block
;
296 virtual void visit(ir_if
*ir
)
298 if(this->loop
.nesting_depth
== 0 && ir
->get_next()->is_tail_sentinel())
299 this->loop
.in_if_at_the_end_of_the_loop
= true;
301 ++this->function
.nesting_depth
;
302 ++this->loop
.nesting_depth
;
304 block_record block_records
[2];
307 block_records
[0] = visit_block(&ir
->then_instructions
);
308 block_records
[1] = visit_block(&ir
->else_instructions
);
310 retry
: /* we get here if we put code after the if inside a branch */
311 for(unsigned i
= 0; i
< 2; ++i
) {
312 exec_list
& list
= i
? ir
->else_instructions
: ir
->then_instructions
;
314 if(!list
.is_empty() && get_jump_strength((ir_instruction
*)list
.get_tail()))
315 jumps
[i
] = (ir_jump
*)list
.get_tail();
319 jump_strength jump_strengths
[2];
321 for(unsigned i
= 0; i
< 2; ++i
) {
323 jump_strengths
[i
] = block_records
[i
].min_strength
;
324 assert(jump_strengths
[i
] == get_jump_strength(jumps
[i
]));
326 jump_strengths
[i
] = strength_none
;
329 /* move both jumps out if possible */
330 if(pull_out_jumps
&& jump_strengths
[0] == jump_strengths
[1]) {
332 if(jump_strengths
[0] == strength_continue
)
333 ir
->insert_after(new(ir
) ir_loop_jump(ir_loop_jump::jump_continue
));
334 else if(jump_strengths
[0] == strength_break
)
335 ir
->insert_after(new(ir
) ir_loop_jump(ir_loop_jump::jump_break
));
336 /* FINISHME: unify returns with identical expressions */
337 else if(jump_strengths
[0] == strength_return
&& this->function
.signature
->return_type
->is_void())
338 ir
->insert_after(new(ir
) ir_return(NULL
));
345 this->progress
= true;
349 block_records
[0].min_strength
= strength_none
;
350 block_records
[1].min_strength
= strength_none
;
355 /* lower a jump: if both need to lowered, start with the strongest one, so that
356 * we might later unify the lowered version with the other one
358 bool should_lower
[2];
359 for(unsigned i
= 0; i
< 2; ++i
)
360 should_lower
[i
] = should_lower_jump(jumps
[i
]);
363 if(should_lower
[1] && should_lower
[0])
364 lower
= jump_strengths
[1] > jump_strengths
[0];
365 else if(should_lower
[0])
367 else if(should_lower
[1])
372 if(jump_strengths
[lower
] == strength_return
) {
373 ir_variable
* return_flag
= this->function
.get_return_flag();
374 if(!this->function
.signature
->return_type
->is_void()) {
375 ir_variable
* return_value
= this->function
.get_return_value();
376 jumps
[lower
]->insert_before(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(return_value
), ((ir_return
*)jumps
[lower
])->value
, NULL
));
378 jumps
[lower
]->insert_before(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(return_flag
), new (ir
) ir_constant(true), NULL
));
379 this->loop
.may_set_return_flag
= true;
380 if(this->loop
.loop
) {
381 ir_loop_jump
* lowered
= 0;
382 lowered
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
383 block_records
[lower
].min_strength
= strength_break
;
384 jumps
[lower
]->replace_with(lowered
);
385 jumps
[lower
] = lowered
;
388 this->progress
= true;
389 } else if(jump_strengths
[lower
] == strength_break
) {
390 /* We can't lower to an actual continue because that would execute the increment.
392 * In the lowered code, we instead put the break check between the this->loop body and the increment,
393 * which is impossible with a real continue as defined by the GLSL IR currently.
395 * Smarter options (such as undoing the increment) are possible but it's not worth implementing them,
396 * because if break is lowered, continue is almost surely lowered too.
398 jumps
[lower
]->insert_before(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(this->loop
.get_break_flag()), new (ir
) ir_constant(true), 0));
400 } else if(jump_strengths
[lower
] == strength_continue
) {
402 ir_variable
* execute_flag
= this->loop
.get_execute_flag();
403 jumps
[lower
]->replace_with(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(execute_flag
), new (ir
) ir_constant(false), 0));
405 block_records
[lower
].min_strength
= strength_always_clears_execute_flag
;
406 block_records
[lower
].may_clear_execute_flag
= true;
407 this->progress
= true;
412 /* move out a jump out if possible */
415 if(jumps
[0] && block_records
[1].min_strength
>= strength_continue
)
417 else if(jumps
[1] && block_records
[0].min_strength
>= strength_continue
)
422 jumps
[move_out
]->remove();
423 ir
->insert_after(jumps
[move_out
]);
425 block_records
[move_out
].min_strength
= strength_none
;
426 this->progress
= true;
430 if(block_records
[0].min_strength
< block_records
[1].min_strength
)
431 this->block
.min_strength
= block_records
[0].min_strength
;
433 this->block
.min_strength
= block_records
[1].min_strength
;
434 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
;
436 if(this->block
.min_strength
)
437 truncate_after_instruction(ir
);
438 else if(this->block
.may_clear_execute_flag
)
441 if(block_records
[0].min_strength
&& !block_records
[1].may_clear_execute_flag
)
443 else if(block_records
[1].min_strength
&& !block_records
[0].may_clear_execute_flag
)
447 assert(!block_records
[move_into
].min_strength
&& !block_records
[move_into
].may_clear_execute_flag
); /* otherwise, we just truncated */
449 exec_list
* list
= move_into
? &ir
->else_instructions
: &ir
->then_instructions
;
450 exec_node
* next
= ir
->get_next();
451 if(!next
->is_tail_sentinel()) {
452 move_outer_block_inside(ir
, list
);
456 block_records
[move_into
] = visit_block(&list
);
458 this->progress
= true;
462 ir_instruction
* ir_after
;
463 for(ir_after
= (ir_instruction
*)ir
->get_next(); !ir_after
->is_tail_sentinel();)
465 ir_if
* ir_if
= ir_after
->as_if();
466 if(ir_if
&& ir_if
->else_instructions
.is_empty()) {
467 ir_dereference_variable
* ir_if_cond_deref
= ir_if
->condition
->as_dereference_variable();
468 if(ir_if_cond_deref
&& ir_if_cond_deref
->var
== this->loop
.execute_flag
) {
469 ir_instruction
* ir_next
= (ir_instruction
*)ir_after
->get_next();
470 ir_after
->insert_before(&ir_if
->then_instructions
);
476 ir_after
= (ir_instruction
*)ir_after
->get_next();
478 /* only set this if we find any unprotected instruction */
479 this->progress
= true;
482 if(!ir
->get_next()->is_tail_sentinel()) {
483 assert(this->loop
.execute_flag
);
484 ir_if
* if_execute
= new(ir
) ir_if(new(ir
) ir_dereference_variable(this->loop
.execute_flag
));
485 move_outer_block_inside(ir
, &if_execute
->then_instructions
);
486 ir
->insert_after(if_execute
);
490 --this->loop
.nesting_depth
;
491 --this->function
.nesting_depth
;
494 virtual void visit(ir_loop
*ir
)
496 ++this->function
.nesting_depth
;
497 loop_record saved_loop
= this->loop
;
498 this->loop
= loop_record(this->function
.signature
, ir
);
500 block_record body
= visit_block(&ir
->body_instructions
);
502 if(body
.min_strength
>= strength_break
) {
503 /* FINISHME: turn the this->loop into an if, or replace it with its body */
506 if(this->loop
.break_flag
) {
507 ir_if
* break_if
= new(ir
) ir_if(new(ir
) ir_dereference_variable(this->loop
.break_flag
));
508 break_if
->then_instructions
.push_tail(new(ir
) ir_loop_jump(ir_loop_jump::jump_break
));
509 ir
->body_instructions
.push_tail(break_if
);
512 if(this->loop
.may_set_return_flag
) {
513 assert(this->function
.return_flag
);
514 ir_if
* return_if
= new(ir
) ir_if(new(ir
) ir_dereference_variable(this->function
.return_flag
));
515 saved_loop
.may_set_return_flag
= true;
517 return_if
->then_instructions
.push_tail(new(ir
) ir_loop_jump(ir_loop_jump::jump_break
));
519 move_outer_block_inside(ir
, &return_if
->else_instructions
);
520 ir
->insert_after(return_if
);
523 this->loop
= saved_loop
;
524 --this->function
.nesting_depth
;
527 virtual void visit(ir_function_signature
*ir
)
529 /* these are not strictly necessary */
530 assert(!this->function
.signature
);
531 assert(!this->loop
.loop
);
533 function_record saved_function
= this->function
;
534 loop_record saved_loop
= this->loop
;
535 this->function
= function_record(ir
);
536 this->loop
= loop_record(ir
);
538 assert(!this->loop
.loop
);
539 visit_block(&ir
->body
);
541 if(this->function
.return_value
)
542 ir
->body
.push_tail(new(ir
) ir_return(new (ir
) ir_dereference_variable(this->function
.return_value
)));
544 this->loop
= saved_loop
;
545 this->function
= saved_function
;
548 virtual void visit(class ir_function
* ir
)
550 visit_block(&ir
->signatures
);
555 do_lower_jumps(exec_list
*instructions
, bool pull_out_jumps
, bool lower_sub_return
, bool lower_main_return
, bool lower_continue
, bool lower_break
)
557 ir_lower_jumps_visitor v
;
558 v
.pull_out_jumps
= pull_out_jumps
;
559 v
.lower_continue
= lower_continue
;
560 v
.lower_break
= lower_break
;
561 v
.lower_sub_return
= lower_sub_return
;
562 v
.lower_main_return
= lower_main_return
;
566 visit_exec_list(instructions
, &v
);
567 } while (v
.progress
);