1 /*
2 * Copyright 2021 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "agx_compiler.h"
7
8 /* SSA-based scalar dead code elimination */
9
10 void
agx_dce(agx_context * ctx,bool partial)11 agx_dce(agx_context *ctx, bool partial)
12 {
13 bool progress;
14 do {
15 progress = false;
16
17 BITSET_WORD *seen = calloc(BITSET_WORDS(ctx->alloc), sizeof(BITSET_WORD));
18
19 agx_foreach_instr_global(ctx, I) {
20 agx_foreach_ssa_src(I, s) {
21 BITSET_SET(seen, I->src[s].value);
22 }
23 }
24
25 agx_foreach_instr_global_safe_rev(ctx, I) {
26 bool needed = false;
27
28 agx_foreach_ssa_dest(I, d) {
29 /* Eliminate destinations that are never read, as RA needs to
30 * handle them specially. Visible only for instructions that write
31 * multiple destinations (splits) or that write a destination but
32 * cannot be DCE'd (atomics).
33 */
34 if (BITSET_TEST(seen, I->dest[d].value)) {
35 needed = true;
36 } else if (partial) {
37 I->dest[d] = agx_null();
38 progress = true;
39 }
40 }
41
42 if (!needed && agx_opcodes_info[I->op].can_eliminate) {
43 agx_remove_instruction(I);
44 progress = true;
45 }
46 }
47
48 free(seen);
49 } while (progress);
50 }
51