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
,
44 /* minimum jump strength (of lowered IR, not pre-lowering IR)
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)
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.
53 jump_strength min_strength
;
55 /* can anything clear the execute flag? */
56 bool may_clear_execute_flag
;
60 this->min_strength
= strength_none
;
61 this->may_clear_execute_flag
= false;
67 ir_function_signature
* signature
;
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
;
74 bool may_set_return_flag
;
76 ir_variable
* break_flag
;
77 ir_variable
* execute_flag
; /* cleared to emulate continue */
79 loop_record(ir_function_signature
* p_signature
= 0, ir_loop
* p_loop
= 0)
81 this->signature
= p_signature
;
83 this->nesting_depth
= 0;
84 this->in_if_at_the_end_of_the_loop
= false;
85 this->may_set_return_flag
= false;
87 this->execute_flag
= 0;
90 ir_variable
* get_execute_flag()
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
);
99 return this->execute_flag
;
102 ir_variable
* get_break_flag()
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));
110 return this->break_flag
;
114 struct function_record
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
;
120 unsigned nesting_depth
;
122 function_record(ir_function_signature
* p_signature
= 0)
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);
131 ir_variable
* get_return_flag()
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
);
138 return this->return_flag
;
141 ir_variable
* get_return_value()
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
);
148 return this->return_value
;
152 struct ir_lower_jumps_visitor
: public ir_control_flow_visitor
{
155 struct function_record function
;
156 struct loop_record loop
;
157 struct block_record block
;
162 bool lower_sub_return
;
163 bool lower_main_return
;
165 ir_lower_jumps_visitor()
167 this->progress
= false;
170 void truncate_after_instruction(exec_node
*ir
)
175 while (!ir
->get_next()->is_tail_sentinel()) {
176 ((ir_instruction
*)ir
->get_next())->remove();
177 this->progress
= true;
181 void move_outer_block_inside(ir_instruction
*ir
, exec_list
*inner_block
)
183 while (!ir
->get_next()->is_tail_sentinel()) {
184 ir_instruction
*move_ir
= (ir_instruction
*)ir
->get_next();
187 inner_block
->push_tail(move_ir
);
191 virtual void visit(class ir_loop_jump
* ir
)
193 truncate_after_instruction(ir
);
194 this->block
.min_strength
= ir
->is_break() ? strength_break
: strength_continue
;
197 virtual void visit(class ir_return
* ir
)
199 truncate_after_instruction(ir
);
200 this->block
.min_strength
= strength_return
;
203 virtual void visit(class ir_discard
* ir
)
205 truncate_after_instruction(ir
);
206 this->block
.min_strength
= strength_discard
;
209 enum jump_strength
get_jump_strength(ir_instruction
* 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
;
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
;
223 return strength_none
;
226 bool should_lower_jump(ir_jump
* ir
)
228 unsigned strength
= get_jump_strength(ir
);
233 lower
= false; /* don't change this, code relies on it */
235 case strength_continue
:
236 lower
= lower_continue
;
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
)))
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())
251 else if (this->function
.is_main
)
252 lower
= lower_main_return
;
254 lower
= lower_sub_return
;
256 case strength_discard
:
257 lower
= false; /* probably nothing needs this lowered */
263 block_record
visit_block(exec_list
* list
)
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
;
273 virtual void visit(ir_if
*ir
)
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;
278 ++this->function
.nesting_depth
;
279 ++this->loop
.nesting_depth
;
281 block_record block_records
[2];
284 block_records
[0] = visit_block(&ir
->then_instructions
);
285 block_records
[1] = visit_block(&ir
->else_instructions
);
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
;
291 if(!list
.is_empty() && get_jump_strength((ir_instruction
*)list
.get_tail()))
292 jumps
[i
] = (ir_jump
*)list
.get_tail();
296 jump_strength jump_strengths
[2];
298 for(unsigned i
= 0; i
< 2; ++i
) {
300 jump_strengths
[i
] = block_records
[i
].min_strength
;
301 assert(jump_strengths
[i
] == get_jump_strength(jumps
[i
]));
303 jump_strengths
[i
] = strength_none
;
306 /* move both jumps out if possible */
307 if(pull_out_jumps
&& jump_strengths
[0] == jump_strengths
[1]) {
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 */
323 this->progress
= true;
327 block_records
[0].min_strength
= strength_none
;
328 block_records
[1].min_strength
= strength_none
;
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
336 bool should_lower
[2];
337 for(unsigned i
= 0; i
< 2; ++i
)
338 should_lower
[i
] = should_lower_jump(jumps
[i
]);
341 if(should_lower
[1] && should_lower
[0])
342 lower
= jump_strengths
[1] > jump_strengths
[0];
343 else if(should_lower
[0])
345 else if(should_lower
[1])
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
));
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
;
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.
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.
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.
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));
378 } else if(jump_strengths
[lower
] == strength_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));
383 block_records
[lower
].min_strength
= strength_always_clears_execute_flag
;
384 block_records
[lower
].may_clear_execute_flag
= true;
385 this->progress
= true;
390 /* move out a jump out if possible */
393 if(jumps
[0] && block_records
[1].min_strength
>= strength_continue
)
395 else if(jumps
[1] && block_records
[0].min_strength
>= strength_continue
)
400 jumps
[move_out
]->remove();
401 ir
->insert_after(jumps
[move_out
]);
403 block_records
[move_out
].min_strength
= strength_none
;
404 this->progress
= true;
408 if(block_records
[0].min_strength
< block_records
[1].min_strength
)
409 this->block
.min_strength
= block_records
[0].min_strength
;
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
;
414 if(this->block
.min_strength
)
415 truncate_after_instruction(ir
);
416 else if(this->block
.may_clear_execute_flag
)
419 if(block_records
[0].min_strength
&& !block_records
[1].may_clear_execute_flag
)
421 else if(block_records
[1].min_strength
&& !block_records
[0].may_clear_execute_flag
)
425 assert(!block_records
[move_into
].min_strength
&& !block_records
[move_into
].may_clear_execute_flag
); /* otherwise, we just truncated */
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
);
434 block_records
[move_into
] = visit_block(&list
);
436 this->progress
= true;
440 ir_instruction
* ir_after
;
441 for(ir_after
= (ir_instruction
*)ir
->get_next(); !ir_after
->is_tail_sentinel();)
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
);
454 ir_after
= (ir_instruction
*)ir_after
->get_next();
456 /* only set this if we find any unprotected instruction */
457 this->progress
= true;
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
);
468 --this->loop
.nesting_depth
;
469 --this->function
.nesting_depth
;
472 virtual void visit(ir_loop
*ir
)
474 ++this->function
.nesting_depth
;
475 loop_record saved_loop
= this->loop
;
476 this->loop
= loop_record(this->function
.signature
, ir
);
478 block_record body
= visit_block(&ir
->body_instructions
);
480 if(body
.min_strength
>= strength_break
) {
481 /* FINISHME: turn the this->loop into an if, or replace it with its body */
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
);
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
);
497 this->loop
= saved_loop
;
498 --this->function
.nesting_depth
;
501 virtual void visit(ir_function_signature
*ir
)
503 /* these are not strictly necessary */
504 assert(!this->function
.signature
);
505 assert(!this->loop
.loop
);
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
);
512 assert(!this->loop
.loop
);
513 visit_block(&ir
->body
);
515 if(this->function
.return_value
)
516 ir
->body
.push_tail(new(ir
) ir_return(new (ir
) ir_dereference_variable(this->function
.return_value
)));
518 this->loop
= saved_loop
;
519 this->function
= saved_function
;
522 virtual void visit(class ir_function
* ir
)
524 visit_block(&ir
->signatures
);
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
)
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
;
540 visit_exec_list(instructions
, &v
);
541 } while (v
.progress
);