1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "compiler.h"
26 #include "util/u_memory.h"
27
28 bool
bi_opt_dead_code_eliminate(bi_context * ctx,bi_block * block)29 bi_opt_dead_code_eliminate(bi_context *ctx, bi_block *block)
30 {
31 bool progress = false;
32 unsigned temp_count = bi_max_temp(ctx);
33
34 bi_invalidate_liveness(ctx);
35 bi_compute_liveness(ctx);
36
37 uint16_t *live = mem_dup(block->base.live_out, temp_count * sizeof(uint16_t));
38
39 bi_foreach_instr_in_block_safe_rev(block, ins) {
40 if (ins->dest && !(ins->dest & BIR_SPECIAL)) {
41 if (!live[ins->dest]) {
42 bi_remove_instruction(ins);
43 progress |= true;
44 }
45 }
46
47 bi_liveness_ins_update(live, ins, temp_count);
48 }
49
50 free(live);
51
52 return progress;
53 }
54