i965: Move pre-draw resolve buffers to dd::UpdateState
[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 */
38 bool
39 dead_control_flow_eliminate(backend_visitor *v)
40 {
41 bool progress = false;
42
43 v->calculate_cfg();
44
45 for (int b = 0; b < v->cfg->num_blocks; b++) {
46 bblock_t *block = v->cfg->blocks[b];
47 bool found = false;
48
49 /* ENDIF instructions, by definition, can only be found at the start of
50 * basic blocks.
51 */
52 backend_instruction *endif_inst = block->start;
53 if (endif_inst->opcode != BRW_OPCODE_ENDIF)
54 continue;
55
56 backend_instruction *if_inst = NULL, *else_inst = NULL;
57 backend_instruction *prev_inst = (backend_instruction *) endif_inst->prev;
58 if (prev_inst->opcode == BRW_OPCODE_ELSE) {
59 else_inst = prev_inst;
60 found = true;
61
62 prev_inst = (backend_instruction *) prev_inst->prev;
63 }
64
65 if (prev_inst->opcode == BRW_OPCODE_IF) {
66 if_inst = prev_inst;
67 found = true;
68 } else {
69 /* Don't remove the ENDIF if we didn't find a dead IF. */
70 endif_inst = NULL;
71 }
72
73 if (found) {
74 if (if_inst)
75 if_inst->remove();
76 if (else_inst)
77 else_inst->remove();
78 if (endif_inst)
79 endif_inst->remove();
80 progress = true;
81 }
82 }
83
84 if (progress)
85 v->invalidate_live_intervals();
86
87 return progress;
88 }