Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/panfrost/bifrost/bi_opt_dce.c
4564 views
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
/* A simple liveness-based dead code elimination pass. */
29
30
void
31
bi_opt_dead_code_eliminate(bi_context *ctx)
32
{
33
unsigned temp_count = bi_max_temp(ctx);
34
35
bi_invalidate_liveness(ctx);
36
bi_compute_liveness(ctx);
37
38
bi_foreach_block_rev(ctx, _block) {
39
bi_block *block = (bi_block *) _block;
40
uint16_t *live = rzalloc_array(_block, uint16_t, temp_count);
41
42
pan_foreach_successor(_block, succ) {
43
for (unsigned i = 0; i < temp_count; ++i)
44
live[i] |= succ->live_in[i];
45
}
46
47
bi_foreach_instr_in_block_safe_rev(block, ins) {
48
bool all_null = true;
49
50
bi_foreach_dest(ins, d) {
51
unsigned index = bi_get_node(ins->dest[d]);
52
53
if (index < temp_count && !(live[index] & bi_writemask(ins, d)))
54
ins->dest[d] = bi_null();
55
56
all_null &= bi_is_null(ins->dest[d]);
57
}
58
59
if (all_null && !bi_side_effects(ins->op))
60
bi_remove_instruction(ins);
61
else
62
bi_liveness_ins_update(live, ins, temp_count);
63
}
64
65
ralloc_free(block->base.live_in);
66
block->base.live_in = live;
67
}
68
}
69
70
/* Post-RA liveness-based dead code analysis to clean up results of bundling */
71
72
uint64_t
73
bi_postra_liveness_ins(uint64_t live, bi_instr *ins)
74
{
75
bi_foreach_dest(ins, d) {
76
if (ins->dest[d].type == BI_INDEX_REGISTER) {
77
unsigned nr = bi_count_write_registers(ins, d);
78
unsigned reg = ins->dest[d].value;
79
live &= ~(BITFIELD64_MASK(nr) << reg);
80
}
81
}
82
83
bi_foreach_src(ins, s) {
84
if (ins->src[s].type == BI_INDEX_REGISTER) {
85
unsigned nr = bi_count_read_registers(ins, s);
86
unsigned reg = ins->src[s].value;
87
live |= (BITFIELD64_MASK(nr) << reg);
88
}
89
}
90
91
return live;
92
}
93
94
static bool
95
bi_postra_liveness_block(bi_block *blk)
96
{
97
pan_foreach_successor((&blk->base), _succ) {
98
bi_block *succ = (bi_block *) _succ;
99
blk->reg_live_out |= succ->reg_live_in;
100
}
101
102
uint64_t live = blk->reg_live_out;
103
104
bi_foreach_instr_in_block_rev(blk, ins)
105
live = bi_postra_liveness_ins(live, ins);
106
107
bool progress = blk->reg_live_in != live;
108
blk->reg_live_in = live;
109
return progress;
110
}
111
112
/* Globally, liveness analysis uses a fixed-point algorithm based on a
113
* worklist. We initialize a work list with the exit block. We iterate the work
114
* list to compute live_in from live_out for each block on the work list,
115
* adding the predecessors of the block to the work list if we made progress.
116
*/
117
118
void
119
bi_postra_liveness(bi_context *ctx)
120
{
121
struct set *work_list = _mesa_set_create(NULL,
122
_mesa_hash_pointer,
123
_mesa_key_pointer_equal);
124
125
struct set *visited = _mesa_set_create(NULL,
126
_mesa_hash_pointer,
127
_mesa_key_pointer_equal);
128
129
struct set_entry *cur;
130
cur = _mesa_set_add(work_list, pan_exit_block(&ctx->blocks));
131
132
bi_foreach_block(ctx, _block) {
133
bi_block *block = (bi_block *) _block;
134
block->reg_live_out = block->reg_live_in = 0;
135
}
136
137
do {
138
bi_block *blk = (struct bi_block *) cur->key;
139
_mesa_set_remove(work_list, cur);
140
141
/* Update its liveness information */
142
bool progress = bi_postra_liveness_block(blk);
143
144
/* If we made progress, we need to process the predecessors */
145
146
if (progress || !_mesa_set_search(visited, blk)) {
147
pan_foreach_predecessor((&blk->base), pred)
148
_mesa_set_add(work_list, pred);
149
}
150
151
_mesa_set_add(visited, blk);
152
} while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
153
154
_mesa_set_destroy(visited, NULL);
155
_mesa_set_destroy(work_list, NULL);
156
}
157
158
void
159
bi_opt_dce_post_ra(bi_context *ctx)
160
{
161
bi_postra_liveness(ctx);
162
163
bi_foreach_block_rev(ctx, _block) {
164
bi_block *block = (bi_block *) _block;
165
uint64_t live = block->reg_live_out;
166
167
bi_foreach_instr_in_block_rev(block, ins) {
168
bi_foreach_dest(ins, d) {
169
if (ins->dest[d].type != BI_INDEX_REGISTER)
170
continue;
171
172
unsigned nr = bi_count_write_registers(ins, d);
173
unsigned reg = ins->dest[d].value;
174
uint64_t mask = (BITFIELD64_MASK(nr) << reg);
175
bool cullable = (ins->op != BI_OPCODE_BLEND);
176
177
if (!(live & mask) && cullable)
178
ins->dest[d] = bi_null();
179
}
180
181
live = bi_postra_liveness_ins(live, ins);
182
}
183
}
184
}
185
186