Debian Codegraph @

src/panfrost/compiler/bifrost/bi_opt_dce.c

raw
1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "util/u_memory.h"
8 #include "compiler.h"
9
10 /**
11 * A simple SSA-based dead code elimination pass.
12 * This pass assumes that no loop header phis are dead.
13 */
14
15 void
16 bi_opt_dce(bi_context *ctx, bool partial)
17 {
18 BITSET_WORD *seen = BITSET_CALLOC(ctx->ssa_alloc);
19
20 bi_foreach_block(ctx, block) {
21 if (block->loop_header) {
22 bi_foreach_instr_in_block(block, I) {
23 if (I->op != BI_OPCODE_PHI) {
24 break;
25 }
26
27 bi_foreach_ssa_src(I, s) {
28 BITSET_SET(seen, I->src[s].value);
29 }
30 }
31 }
32 }
33
34 bi_foreach_block_rev(ctx, block) {
35 bi_foreach_instr_in_block_safe_rev(block, I) {
36 if (block->loop_header && I->op == BI_OPCODE_PHI)
37 break;
38
39 bool needed = false;
40
41 bi_foreach_ssa_dest(I, d) {
42 if (BITSET_TEST(seen, I->dest[d].value)) {
43 needed = true;
44 } else if (partial) {
45 I->dest[d] = bi_null();
46 }
47 }
48
49 if (!needed && !bi_side_effects(I)) {
50 bi_remove_instruction(I);
51 } else {
52 bi_foreach_ssa_src(I, s) {
53 BITSET_SET(seen, I->src[s].value);
54 }
55 }
56 }
57 }
58
59 free(seen);
60 }
61
62 /* Post-RA liveness-based dead code analysis to clean up results of bundling */
63
64 uint64_t MUST_CHECK
65 bi_postra_liveness_ins(uint64_t live, bi_instr *ins)
66 {
67 bi_foreach_dest(ins, d) {
68 if (ins->dest[d].type == BI_INDEX_REGISTER) {
69 unsigned nr = bi_count_write_registers(ins, d);
70 unsigned reg = ins->dest[d].value;
71 live &= ~(BITFIELD64_MASK(nr) << reg);
72 }
73 }
74
75 bi_foreach_src(ins, s) {
76 if (ins->src[s].type == BI_INDEX_REGISTER) {
77 unsigned nr = bi_count_read_registers(ins, s);
78 unsigned reg = ins->src[s].value;
79 live |= (BITFIELD64_MASK(nr) << reg);
80 }
81 }
82
83 return live;
84 }
85
86 static bool
87 bi_postra_liveness_block(bi_block *blk)
88 {
89 bi_foreach_successor(blk, succ)
90 blk->reg_live_out |= succ->reg_live_in;
91
92 uint64_t live = blk->reg_live_out;
93
94 bi_foreach_instr_in_block_rev(blk, ins)
95 live = bi_postra_liveness_ins(live, ins);
96
97 bool progress = blk->reg_live_in != live;
98 blk->reg_live_in = live;
99 return progress;
100 }
101
102 /* Globally, liveness analysis uses a fixed-point algorithm based on a
103 * worklist. We initialize a work list with the exit block. We iterate the work
104 * list to compute live_in from live_out for each block on the work list,
105 * adding the predecessors of the block to the work list if we made progress.
106 */
107
108 void
109 bi_postra_liveness(bi_context *ctx)
110 {
111 u_worklist worklist;
112 bi_worklist_init(ctx, &worklist);
113
114 bi_foreach_block(ctx, block) {
115 block->reg_live_out = block->reg_live_in = 0;
116
117 bi_worklist_push_tail(&worklist, block);
118 }
119
120 while (!u_worklist_is_empty(&worklist)) {
121 /* Pop off in reverse order since liveness is backwards */
122 bi_block *blk = bi_worklist_pop_tail(&worklist);
123
124 /* Update liveness information. If we made progress, we need to
125 * reprocess the predecessors
126 */
127 if (bi_postra_liveness_block(blk)) {
128 bi_foreach_predecessor(blk, pred)
129 bi_worklist_push_head(&worklist, *pred);
130 }
131 }
132
133 u_worklist_fini(&worklist);
134 }
135
136 void
137 bi_opt_dce_post_ra(bi_context *ctx)
138 {
139 bi_postra_liveness(ctx);
140
141 bi_foreach_block_rev(ctx, block) {
142 uint64_t live = block->reg_live_out;
143
144 bi_foreach_instr_in_block_rev(block, ins) {
145 if (ins->op == BI_OPCODE_DTSEL_IMM)
146 ins->dest[0] = bi_null();
147
148 bi_foreach_dest(ins, d) {
149 if (ins->dest[d].type != BI_INDEX_REGISTER)
150 continue;
151
152 unsigned nr = bi_count_write_registers(ins, d);
153 unsigned reg = ins->dest[d].value;
154 uint64_t mask = (BITFIELD64_MASK(nr) << reg);
155 bool cullable = (ins->op != BI_OPCODE_BLEND);
156 cullable &= !bi_get_opcode_props(ins)->sr_write;
157
158 if (!(live & mask) && cullable)
159 ins->dest[d] = bi_null();
160 }
161
162 live = bi_postra_liveness_ins(live, ins);
163 }
164 }
165 }