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