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
28 #include "glsl_types.h"
35 strength_always_clears_execute_flag
,
43 /* minimum jump strength (of lowered IR, not pre-lowering IR)
45 * If the block ends with a jump, must be the strength of the jump.
46 * Otherwise, the jump would be dead and have been deleted before)
48 * 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
49 * (e.g. an if with a return in one branch, and a break in the other, while not lowering them)
50 * Note that identical jumps are usually unified though.
52 jump_strength min_strength
;
54 /* can anything clear the execute flag? */
55 bool may_clear_execute_flag
;
59 this->min_strength
= strength_none
;
60 this->may_clear_execute_flag
= false;
66 ir_function_signature
* signature
;
69 /* used to avoid lowering the break used to represent lowered breaks */
70 unsigned nesting_depth
;
71 bool in_if_at_the_end_of_the_loop
;
73 bool may_set_return_flag
;
75 ir_variable
* break_flag
;
76 ir_variable
* execute_flag
; /* cleared to emulate continue */
78 loop_record(ir_function_signature
* p_signature
= 0, ir_loop
* p_loop
= 0)
80 this->signature
= p_signature
;
82 this->nesting_depth
= 0;
83 this->in_if_at_the_end_of_the_loop
= false;
84 this->may_set_return_flag
= false;
86 this->execute_flag
= 0;
89 ir_variable
* get_execute_flag()
91 /* also supported for the "function loop" */
92 if(!this->execute_flag
) {
93 exec_list
& list
= this->loop
? this->loop
->body_instructions
: signature
->body
;
94 this->execute_flag
= new(this->signature
) ir_variable(glsl_type::bool_type
, "execute_flag", ir_var_temporary
);
95 list
.push_head(new(this->signature
) ir_assignment(new(this->signature
) ir_dereference_variable(execute_flag
), new(this->signature
) ir_constant(true), 0));
96 list
.push_head(this->execute_flag
);
98 return this->execute_flag
;
101 ir_variable
* get_break_flag()
104 if(!this->break_flag
) {
105 this->break_flag
= new(this->signature
) ir_variable(glsl_type::bool_type
, "break_flag", ir_var_temporary
);
106 this->loop
->insert_before(this->break_flag
);
107 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 return this->break_flag
;
113 struct function_record
115 ir_function_signature
* signature
;
116 ir_variable
* return_flag
; /* used to break out of all loops and then jump to the return instruction */
117 ir_variable
* return_value
;
119 unsigned nesting_depth
;
121 function_record(ir_function_signature
* p_signature
= 0)
123 this->signature
= p_signature
;
124 this->return_flag
= 0;
125 this->return_value
= 0;
126 this->nesting_depth
= 0;
127 this->is_main
= this->signature
&& (strcmp(this->signature
->function_name(), "main") == 0);
130 ir_variable
* get_return_flag()
132 if(!this->return_flag
) {
133 this->return_flag
= new(this->signature
) ir_variable(glsl_type::bool_type
, "return_flag", ir_var_temporary
);
134 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));
135 this->signature
->body
.push_head(this->return_flag
);
137 return this->return_flag
;
140 ir_variable
* get_return_value()
142 if(!this->return_value
) {
143 assert(!this->signature
->return_type
->is_void());
144 return_value
= new(this->signature
) ir_variable(this->signature
->return_type
, "return_value", ir_var_temporary
);
145 this->signature
->body
.push_head(this->return_value
);
147 return this->return_value
;
151 struct ir_lower_jumps_visitor
: public ir_control_flow_visitor
{
154 struct function_record function
;
155 struct loop_record loop
;
156 struct block_record block
;
161 bool lower_sub_return
;
162 bool lower_main_return
;
164 ir_lower_jumps_visitor()
166 this->progress
= false;
169 void truncate_after_instruction(exec_node
*ir
)
174 while (!ir
->get_next()->is_tail_sentinel()) {
175 ((ir_instruction
*)ir
->get_next())->remove();
176 this->progress
= true;
180 void move_outer_block_inside(ir_instruction
*ir
, exec_list
*inner_block
)
182 while (!ir
->get_next()->is_tail_sentinel()) {
183 ir_instruction
*move_ir
= (ir_instruction
*)ir
->get_next();
186 inner_block
->push_tail(move_ir
);
190 virtual void visit(class ir_loop_jump
* ir
)
192 truncate_after_instruction(ir
);
193 this->block
.min_strength
= ir
->is_break() ? strength_break
: strength_continue
;
196 virtual void visit(class ir_return
* ir
)
198 truncate_after_instruction(ir
);
199 this->block
.min_strength
= strength_return
;
202 virtual void visit(class ir_discard
* ir
)
206 enum jump_strength
get_jump_strength(ir_instruction
* ir
)
209 return strength_none
;
210 else if(ir
->ir_type
== ir_type_loop_jump
) {
211 if(((ir_loop_jump
*)ir
)->is_break())
212 return strength_break
;
214 return strength_continue
;
215 } else if(ir
->ir_type
== ir_type_return
)
216 return strength_return
;
218 return strength_none
;
221 bool should_lower_jump(ir_jump
* ir
)
223 unsigned strength
= get_jump_strength(ir
);
228 lower
= false; /* don't change this, code relies on it */
230 case strength_continue
:
231 lower
= lower_continue
;
234 assert(this->loop
.loop
);
235 /* never lower "canonical break" */
236 if(ir
->get_next()->is_tail_sentinel() && (this->loop
.nesting_depth
== 0
237 || (this->loop
.nesting_depth
== 1 && this->loop
.in_if_at_the_end_of_the_loop
)))
242 case strength_return
:
243 /* never lower return at the end of a this->function */
244 if(this->function
.nesting_depth
== 0 && ir
->get_next()->is_tail_sentinel())
246 else if (this->function
.is_main
)
247 lower
= lower_main_return
;
249 lower
= lower_sub_return
;
255 block_record
visit_block(exec_list
* list
)
257 block_record saved_block
= this->block
;
258 this->block
= block_record();
259 visit_exec_list(list
, this);
260 block_record ret
= this->block
;
261 this->block
= saved_block
;
265 virtual void visit(ir_if
*ir
)
267 if(this->loop
.nesting_depth
== 0 && ir
->get_next()->is_tail_sentinel())
268 this->loop
.in_if_at_the_end_of_the_loop
= true;
270 ++this->function
.nesting_depth
;
271 ++this->loop
.nesting_depth
;
273 block_record block_records
[2];
276 block_records
[0] = visit_block(&ir
->then_instructions
);
277 block_records
[1] = visit_block(&ir
->else_instructions
);
279 retry
: /* we get here if we put code after the if inside a branch */
280 for(unsigned i
= 0; i
< 2; ++i
) {
281 exec_list
& list
= i
? ir
->else_instructions
: ir
->then_instructions
;
283 if(!list
.is_empty() && get_jump_strength((ir_instruction
*)list
.get_tail()))
284 jumps
[i
] = (ir_jump
*)list
.get_tail();
288 jump_strength jump_strengths
[2];
290 for(unsigned i
= 0; i
< 2; ++i
) {
292 jump_strengths
[i
] = block_records
[i
].min_strength
;
293 assert(jump_strengths
[i
] == get_jump_strength(jumps
[i
]));
295 jump_strengths
[i
] = strength_none
;
298 /* move both jumps out if possible */
299 if(pull_out_jumps
&& jump_strengths
[0] == jump_strengths
[1]) {
301 if(jump_strengths
[0] == strength_continue
)
302 ir
->insert_after(new(ir
) ir_loop_jump(ir_loop_jump::jump_continue
));
303 else if(jump_strengths
[0] == strength_break
)
304 ir
->insert_after(new(ir
) ir_loop_jump(ir_loop_jump::jump_break
));
305 /* FINISHME: unify returns with identical expressions */
306 else if(jump_strengths
[0] == strength_return
&& this->function
.signature
->return_type
->is_void())
307 ir
->insert_after(new(ir
) ir_return(NULL
));
314 this->progress
= true;
318 block_records
[0].min_strength
= strength_none
;
319 block_records
[1].min_strength
= strength_none
;
324 /* lower a jump: if both need to lowered, start with the strongest one, so that
325 * we might later unify the lowered version with the other one
327 bool should_lower
[2];
328 for(unsigned i
= 0; i
< 2; ++i
)
329 should_lower
[i
] = should_lower_jump(jumps
[i
]);
332 if(should_lower
[1] && should_lower
[0])
333 lower
= jump_strengths
[1] > jump_strengths
[0];
334 else if(should_lower
[0])
336 else if(should_lower
[1])
341 if(jump_strengths
[lower
] == strength_return
) {
342 ir_variable
* return_flag
= this->function
.get_return_flag();
343 if(!this->function
.signature
->return_type
->is_void()) {
344 ir_variable
* return_value
= this->function
.get_return_value();
345 jumps
[lower
]->insert_before(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(return_value
), ((ir_return
*)jumps
[lower
])->value
, NULL
));
347 jumps
[lower
]->insert_before(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(return_flag
), new (ir
) ir_constant(true), NULL
));
348 this->loop
.may_set_return_flag
= true;
349 if(this->loop
.loop
) {
350 ir_loop_jump
* lowered
= 0;
351 lowered
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
352 block_records
[lower
].min_strength
= strength_break
;
353 jumps
[lower
]->replace_with(lowered
);
354 jumps
[lower
] = lowered
;
357 this->progress
= true;
358 } else if(jump_strengths
[lower
] == strength_break
) {
359 /* We can't lower to an actual continue because that would execute the increment.
361 * In the lowered code, we instead put the break check between the this->loop body and the increment,
362 * which is impossible with a real continue as defined by the GLSL IR currently.
364 * Smarter options (such as undoing the increment) are possible but it's not worth implementing them,
365 * because if break is lowered, continue is almost surely lowered too.
367 jumps
[lower
]->insert_before(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(this->loop
.get_break_flag()), new (ir
) ir_constant(true), 0));
369 } else if(jump_strengths
[lower
] == strength_continue
) {
371 ir_variable
* execute_flag
= this->loop
.get_execute_flag();
372 jumps
[lower
]->replace_with(new(ir
) ir_assignment(new (ir
) ir_dereference_variable(execute_flag
), new (ir
) ir_constant(false), 0));
374 block_records
[lower
].min_strength
= strength_always_clears_execute_flag
;
375 block_records
[lower
].may_clear_execute_flag
= true;
376 this->progress
= true;
381 /* move out a jump out if possible */
384 if(jumps
[0] && block_records
[1].min_strength
>= strength_continue
)
386 else if(jumps
[1] && block_records
[0].min_strength
>= strength_continue
)
391 jumps
[move_out
]->remove();
392 ir
->insert_after(jumps
[move_out
]);
394 block_records
[move_out
].min_strength
= strength_none
;
395 this->progress
= true;
399 if(block_records
[0].min_strength
< block_records
[1].min_strength
)
400 this->block
.min_strength
= block_records
[0].min_strength
;
402 this->block
.min_strength
= block_records
[1].min_strength
;
403 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
;
405 if(this->block
.min_strength
)
406 truncate_after_instruction(ir
);
407 else if(this->block
.may_clear_execute_flag
)
410 if(block_records
[0].min_strength
&& !block_records
[1].may_clear_execute_flag
)
412 else if(block_records
[1].min_strength
&& !block_records
[0].may_clear_execute_flag
)
416 assert(!block_records
[move_into
].min_strength
&& !block_records
[move_into
].may_clear_execute_flag
); /* otherwise, we just truncated */
418 exec_list
* list
= move_into
? &ir
->else_instructions
: &ir
->then_instructions
;
419 exec_node
* next
= ir
->get_next();
420 if(!next
->is_tail_sentinel()) {
421 move_outer_block_inside(ir
, list
);
425 block_records
[move_into
] = visit_block(&list
);
427 this->progress
= true;
431 ir_instruction
* ir_after
;
432 for(ir_after
= (ir_instruction
*)ir
->get_next(); !ir_after
->is_tail_sentinel();)
434 ir_if
* ir_if
= ir_after
->as_if();
435 if(ir_if
&& ir_if
->else_instructions
.is_empty()) {
436 ir_dereference_variable
* ir_if_cond_deref
= ir_if
->condition
->as_dereference_variable();
437 if(ir_if_cond_deref
&& ir_if_cond_deref
->var
== this->loop
.execute_flag
) {
438 ir_instruction
* ir_next
= (ir_instruction
*)ir_after
->get_next();
439 ir_after
->insert_before(&ir_if
->then_instructions
);
445 ir_after
= (ir_instruction
*)ir_after
->get_next();
447 /* only set this if we find any unprotected instruction */
448 this->progress
= true;
451 if(!ir
->get_next()->is_tail_sentinel()) {
452 assert(this->loop
.execute_flag
);
453 ir_if
* if_execute
= new(ir
) ir_if(new(ir
) ir_dereference_variable(this->loop
.execute_flag
));
454 move_outer_block_inside(ir
, &if_execute
->then_instructions
);
455 ir
->insert_after(if_execute
);
459 --this->loop
.nesting_depth
;
460 --this->function
.nesting_depth
;
463 virtual void visit(ir_loop
*ir
)
465 ++this->function
.nesting_depth
;
466 loop_record saved_loop
= this->loop
;
467 this->loop
= loop_record(this->function
.signature
, ir
);
469 block_record body
= visit_block(&ir
->body_instructions
);
471 if(body
.min_strength
>= strength_break
) {
472 /* FINISHME: turn the this->loop into an if, or replace it with its body */
475 if(this->loop
.break_flag
) {
476 ir_if
* break_if
= new(ir
) ir_if(new(ir
) ir_dereference_variable(this->loop
.break_flag
));
477 break_if
->then_instructions
.push_tail(new(ir
) ir_loop_jump(ir_loop_jump::jump_break
));
478 ir
->body_instructions
.push_tail(break_if
);
481 if(this->loop
.may_set_return_flag
) {
482 assert(this->function
.return_flag
);
483 ir_if
* return_if
= new(ir
) ir_if(new(ir
) ir_dereference_variable(this->function
.return_flag
));
484 return_if
->then_instructions
.push_tail(new(ir
) ir_loop_jump(saved_loop
.loop
? ir_loop_jump::jump_break
: ir_loop_jump::jump_continue
));
485 ir
->insert_after(return_if
);
488 this->loop
= saved_loop
;
489 --this->function
.nesting_depth
;
492 virtual void visit(ir_function_signature
*ir
)
494 /* these are not strictly necessary */
495 assert(!this->function
.signature
);
496 assert(!this->loop
.loop
);
498 function_record saved_function
= this->function
;
499 loop_record saved_loop
= this->loop
;
500 this->function
= function_record(ir
);
501 this->loop
= loop_record(ir
);
503 assert(!this->loop
.loop
);
504 visit_block(&ir
->body
);
506 if(this->function
.return_value
)
507 ir
->body
.push_tail(new(ir
) ir_return(new (ir
) ir_dereference_variable(this->function
.return_value
)));
509 this->loop
= saved_loop
;
510 this->function
= saved_function
;
513 virtual void visit(class ir_function
* ir
)
515 visit_block(&ir
->signatures
);
520 do_lower_jumps(exec_list
*instructions
, bool pull_out_jumps
, bool lower_sub_return
, bool lower_main_return
, bool lower_continue
, bool lower_break
)
522 ir_lower_jumps_visitor v
;
523 v
.pull_out_jumps
= pull_out_jumps
;
524 v
.lower_continue
= lower_continue
;
525 v
.lower_break
= lower_break
;
526 v
.lower_sub_return
= lower_sub_return
;
527 v
.lower_main_return
= lower_main_return
;
531 visit_exec_list(instructions
, &v
);
532 } while (v
.progress
);