i965/cfg: Rework to make IF & ELSE blocks flow into ENDIF.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_dead_control_flow.cpp
1 /*
2 * Copyright © 2013 Intel Corporation
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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /** @file brw_dead_control_flow.cpp
25 *
26 * This file implements the dead control flow elimination optimization pass.
27 */
28
29 #include "brw_shader.h"
30 #include "brw_cfg.h"
31
32 /* Look for and eliminate dead control flow:
33 *
34 * - if/endif
35 * - if/else/endif
36 */
37 bool
38 dead_control_flow_eliminate(backend_visitor *v)
39 {
40 bool progress = false;
41
42 cfg_t cfg(v);
43
44 for (int b = 0; b < cfg.num_blocks; b++) {
45 bblock_t *block = cfg.blocks[b];
46 bool found = false;
47
48 /* ENDIF instructions, by definition, can only be found at the start of
49 * basic blocks.
50 */
51 backend_instruction *endif_inst = block->start;
52 if (endif_inst->opcode != BRW_OPCODE_ENDIF)
53 continue;
54
55 backend_instruction *if_inst = NULL, *else_inst = NULL;
56 backend_instruction *prev_inst = (backend_instruction *) endif_inst->prev;
57 if (prev_inst->opcode == BRW_OPCODE_IF) {
58 if_inst = prev_inst;
59 found = true;
60 } else if (prev_inst->opcode == BRW_OPCODE_ELSE) {
61 else_inst = prev_inst;
62
63 prev_inst = (backend_instruction *) prev_inst->prev;
64 if (prev_inst->opcode == BRW_OPCODE_IF) {
65 if_inst = prev_inst;
66 found = true;
67 }
68 }
69
70 if (found) {
71 if_inst->remove();
72 if (else_inst)
73 else_inst->remove();
74 endif_inst->remove();
75 progress = true;
76 }
77 }
78
79 if (progress)
80 v->invalidate_live_intervals();
81
82 return progress;
83 }