i965/cfg: Eliminate an empty then-branch of an if/else/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 * . else in else/endif
36 * - if/else/endif
37 * - else in if/else
38 */
39 bool
40 dead_control_flow_eliminate(backend_shader *s)
41 {
42 bool progress = false;
43
44 foreach_block_safe (block, s->cfg) {
45 bblock_t *prev_block = block->prev();
46 backend_instruction *const inst = block->start();
47 backend_instruction *prev_inst = prev_block->end();
48
49 /* ENDIF instructions, by definition, can only be found at the start of
50 * basic blocks.
51 */
52 if (inst->opcode == BRW_OPCODE_ENDIF) {
53 bool found = false;
54 bblock_t *if_block = NULL, *else_block = NULL, *endif_block = block;
55 backend_instruction *endif_inst = inst;
56
57 backend_instruction *if_inst = NULL, *else_inst = NULL;
58 if (prev_inst->opcode == BRW_OPCODE_ELSE) {
59 else_inst = prev_inst;
60 else_block = endif_block->prev();
61 found = true;
62
63 if (else_block->start_ip == else_block->end_ip) {
64 prev_block = prev_block->prev();
65 prev_inst = prev_block->end();
66 }
67 }
68
69 if (prev_inst->opcode == BRW_OPCODE_IF) {
70 if_inst = prev_inst;
71 if_block = prev_block;
72 found = true;
73 } else {
74 /* Don't remove the ENDIF if we didn't find a dead IF. */
75 endif_inst = NULL;
76 }
77
78 if (found) {
79 bblock_t *earlier_block = NULL, *later_block = NULL;
80
81 if (if_inst) {
82 if (if_block->start_ip == if_block->end_ip) {
83 earlier_block = if_block->prev();
84 } else {
85 earlier_block = if_block;
86 }
87 if_inst->remove(if_block);
88 }
89
90 if (else_inst) {
91 else_inst->remove(else_block);
92 }
93
94 if (endif_inst) {
95 if (endif_block->start_ip == endif_block->end_ip) {
96 later_block = endif_block->next();
97 } else {
98 later_block = endif_block;
99 }
100 endif_inst->remove(endif_block);
101 }
102
103 assert((earlier_block == NULL) == (later_block == NULL));
104 if (earlier_block && earlier_block->can_combine_with(later_block)) {
105 earlier_block->combine_with(later_block);
106
107 /* If ENDIF was in its own block, then we've now deleted it and
108 * merged the two surrounding blocks, the latter of which the
109 * __next block pointer was pointing to.
110 */
111 if (endif_block != later_block) {
112 __next = earlier_block->next();
113 }
114 }
115
116 progress = true;
117 }
118 } else if (inst->opcode == BRW_OPCODE_ELSE &&
119 prev_inst->opcode == BRW_OPCODE_IF) {
120 bblock_t *const else_block = block;
121 backend_instruction *const if_inst = prev_inst;
122 backend_instruction *const else_inst = inst;
123
124 /* Since the else-branch is becoming the new then-branch, the
125 * condition has to be inverted.
126 */
127 if_inst->predicate_inverse = !if_inst->predicate_inverse;
128 else_inst->remove(else_block);
129
130 progress = true;
131 }
132 }
133
134 if (progress)
135 s->invalidate_live_intervals();
136
137 return progress;
138 }