glsl: Remove "discard" support from lower_jumps.
[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 };
40
41 struct block_record
42 {
43 /* minimum jump strength (of lowered IR, not pre-lowering IR)
44 *
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)
47 *
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.
51 */
52 jump_strength min_strength;
53
54 /* can anything clear the execute flag? */
55 bool may_clear_execute_flag;
56
57 block_record()
58 {
59 this->min_strength = strength_none;
60 this->may_clear_execute_flag = false;
61 }
62 };
63
64 struct loop_record
65 {
66 ir_function_signature* signature;
67 ir_loop* loop;
68
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;
72
73 bool may_set_return_flag;
74
75 ir_variable* break_flag;
76 ir_variable* execute_flag; /* cleared to emulate continue */
77
78 loop_record(ir_function_signature* p_signature = 0, ir_loop* p_loop = 0)
79 {
80 this->signature = p_signature;
81 this->loop = p_loop;
82 this->nesting_depth = 0;
83 this->in_if_at_the_end_of_the_loop = false;
84 this->may_set_return_flag = false;
85 this->break_flag = 0;
86 this->execute_flag = 0;
87 }
88
89 ir_variable* get_execute_flag()
90 {
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);
97 }
98 return this->execute_flag;
99 }
100
101 ir_variable* get_break_flag()
102 {
103 assert(this->loop);
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));
108 }
109 return this->break_flag;
110 }
111 };
112
113 struct function_record
114 {
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;
118 bool is_main;
119 unsigned nesting_depth;
120
121 function_record(ir_function_signature* p_signature = 0)
122 {
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);
128 }
129
130 ir_variable* get_return_flag()
131 {
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);
136 }
137 return this->return_flag;
138 }
139
140 ir_variable* get_return_value()
141 {
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);
146 }
147 return this->return_value;
148 }
149 };
150
151 struct ir_lower_jumps_visitor : public ir_control_flow_visitor {
152 bool progress;
153
154 struct function_record function;
155 struct loop_record loop;
156 struct block_record block;
157
158 bool pull_out_jumps;
159 bool lower_continue;
160 bool lower_break;
161 bool lower_sub_return;
162 bool lower_main_return;
163
164 ir_lower_jumps_visitor()
165 {
166 this->progress = false;
167 }
168
169 void truncate_after_instruction(exec_node *ir)
170 {
171 if (!ir)
172 return;
173
174 while (!ir->get_next()->is_tail_sentinel()) {
175 ((ir_instruction *)ir->get_next())->remove();
176 this->progress = true;
177 }
178 }
179
180 void move_outer_block_inside(ir_instruction *ir, exec_list *inner_block)
181 {
182 while (!ir->get_next()->is_tail_sentinel()) {
183 ir_instruction *move_ir = (ir_instruction *)ir->get_next();
184
185 move_ir->remove();
186 inner_block->push_tail(move_ir);
187 }
188 }
189
190 virtual void visit(class ir_loop_jump * ir)
191 {
192 truncate_after_instruction(ir);
193 this->block.min_strength = ir->is_break() ? strength_break : strength_continue;
194 }
195
196 virtual void visit(class ir_return * ir)
197 {
198 truncate_after_instruction(ir);
199 this->block.min_strength = strength_return;
200 }
201
202 virtual void visit(class ir_discard * ir)
203 {
204 }
205
206 enum jump_strength get_jump_strength(ir_instruction* ir)
207 {
208 if(!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;
213 else
214 return strength_continue;
215 } else if(ir->ir_type == ir_type_return)
216 return strength_return;
217 else
218 return strength_none;
219 }
220
221 bool should_lower_jump(ir_jump* ir)
222 {
223 unsigned strength = get_jump_strength(ir);
224 bool lower;
225 switch(strength)
226 {
227 case strength_none:
228 lower = false; /* don't change this, code relies on it */
229 break;
230 case strength_continue:
231 lower = lower_continue;
232 break;
233 case strength_break:
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)))
238 lower = false;
239 else
240 lower = lower_break;
241 break;
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())
245 lower = false;
246 else if (this->function.is_main)
247 lower = lower_main_return;
248 else
249 lower = lower_sub_return;
250 break;
251 }
252 return lower;
253 }
254
255 block_record visit_block(exec_list* list)
256 {
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;
262 return ret;
263 }
264
265 virtual void visit(ir_if *ir)
266 {
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;
269
270 ++this->function.nesting_depth;
271 ++this->loop.nesting_depth;
272
273 block_record block_records[2];
274 ir_jump* jumps[2];
275
276 block_records[0] = visit_block(&ir->then_instructions);
277 block_records[1] = visit_block(&ir->else_instructions);
278
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;
282 jumps[i] = 0;
283 if(!list.is_empty() && get_jump_strength((ir_instruction*)list.get_tail()))
284 jumps[i] = (ir_jump*)list.get_tail();
285 }
286
287 for(;;) {
288 jump_strength jump_strengths[2];
289
290 for(unsigned i = 0; i < 2; ++i) {
291 if(jumps[i]) {
292 jump_strengths[i] = block_records[i].min_strength;
293 assert(jump_strengths[i] == get_jump_strength(jumps[i]));
294 } else
295 jump_strengths[i] = strength_none;
296 }
297
298 /* move both jumps out if possible */
299 if(pull_out_jumps && jump_strengths[0] == jump_strengths[1]) {
300 bool unify = true;
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));
308 else
309 unify = false;
310
311 if(unify) {
312 jumps[0]->remove();
313 jumps[1]->remove();
314 this->progress = true;
315
316 jumps[0] = 0;
317 jumps[1] = 0;
318 block_records[0].min_strength = strength_none;
319 block_records[1].min_strength = strength_none;
320 break;
321 }
322 }
323
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
326 */
327 bool should_lower[2];
328 for(unsigned i = 0; i < 2; ++i)
329 should_lower[i] = should_lower_jump(jumps[i]);
330
331 int lower;
332 if(should_lower[1] && should_lower[0])
333 lower = jump_strengths[1] > jump_strengths[0];
334 else if(should_lower[0])
335 lower = 0;
336 else if(should_lower[1])
337 lower = 1;
338 else
339 break;
340
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));
346 }
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;
355 } else
356 goto lower_continue;
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.
360 *
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.
363 *
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.
366 */
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));
368 goto lower_continue;
369 } else if(jump_strengths[lower] == strength_continue) {
370 lower_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));
373 jumps[lower] = 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;
377 break;
378 }
379 }
380
381 /* move out a jump out if possible */
382 if(pull_out_jumps) {
383 int move_out = -1;
384 if(jumps[0] && block_records[1].min_strength >= strength_continue)
385 move_out = 0;
386 else if(jumps[1] && block_records[0].min_strength >= strength_continue)
387 move_out = 1;
388
389 if(move_out >= 0)
390 {
391 jumps[move_out]->remove();
392 ir->insert_after(jumps[move_out]);
393 jumps[move_out] = 0;
394 block_records[move_out].min_strength = strength_none;
395 this->progress = true;
396 }
397 }
398
399 if(block_records[0].min_strength < block_records[1].min_strength)
400 this->block.min_strength = block_records[0].min_strength;
401 else
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;
404
405 if(this->block.min_strength)
406 truncate_after_instruction(ir);
407 else if(this->block.may_clear_execute_flag)
408 {
409 int move_into = -1;
410 if(block_records[0].min_strength && !block_records[1].may_clear_execute_flag)
411 move_into = 1;
412 else if(block_records[1].min_strength && !block_records[0].may_clear_execute_flag)
413 move_into = 0;
414
415 if(move_into >= 0) {
416 assert(!block_records[move_into].min_strength && !block_records[move_into].may_clear_execute_flag); /* otherwise, we just truncated */
417
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);
422
423 exec_list list;
424 list.head = next;
425 block_records[move_into] = visit_block(&list);
426
427 this->progress = true;
428 goto retry;
429 }
430 } else {
431 ir_instruction* ir_after;
432 for(ir_after = (ir_instruction*)ir->get_next(); !ir_after->is_tail_sentinel();)
433 {
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);
440 ir_after->remove();
441 ir_after = ir_next;
442 continue;
443 }
444 }
445 ir_after = (ir_instruction*)ir_after->get_next();
446
447 /* only set this if we find any unprotected instruction */
448 this->progress = true;
449 }
450
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);
456 }
457 }
458 }
459 --this->loop.nesting_depth;
460 --this->function.nesting_depth;
461 }
462
463 virtual void visit(ir_loop *ir)
464 {
465 ++this->function.nesting_depth;
466 loop_record saved_loop = this->loop;
467 this->loop = loop_record(this->function.signature, ir);
468
469 block_record body = visit_block(&ir->body_instructions);
470
471 if(body.min_strength >= strength_break) {
472 /* FINISHME: turn the this->loop into an if, or replace it with its body */
473 }
474
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);
479 }
480
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);
486 }
487
488 this->loop = saved_loop;
489 --this->function.nesting_depth;
490 }
491
492 virtual void visit(ir_function_signature *ir)
493 {
494 /* these are not strictly necessary */
495 assert(!this->function.signature);
496 assert(!this->loop.loop);
497
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);
502
503 assert(!this->loop.loop);
504 visit_block(&ir->body);
505
506 if(this->function.return_value)
507 ir->body.push_tail(new(ir) ir_return(new (ir) ir_dereference_variable(this->function.return_value)));
508
509 this->loop = saved_loop;
510 this->function = saved_function;
511 }
512
513 virtual void visit(class ir_function * ir)
514 {
515 visit_block(&ir->signatures);
516 }
517 };
518
519 bool
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)
521 {
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;
528
529 do {
530 v.progress = false;
531 visit_exec_list(instructions, &v);
532 } while (v.progress);
533
534 return v.progress;
535 }