| 1 |
/* |
| 2 |
* Copyright (C) 2018 Alyssa Rosenzweig |
| 3 |
* Copyright (C) 2019 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "util/u_memory.h" |
| 8 |
#include "compiler.h" |
| 9 |
#include "midgard_ops.h" |
| 10 |
|
| 11 |
/* SIMD-aware dead code elimination. Perform liveness analysis step-by-step, |
| 12 |
* removing dead components. If an instruction ends up with a zero mask, the |
| 13 |
* instruction in total is dead and should be removed. */ |
| 14 |
|
| 15 |
static bool |
| 16 |
can_cull_mask(compiler_context *ctx, midgard_instruction *ins) |
| 17 |
{ |
| 18 |
if (ins->dest >= ctx->temp_count) |
| 19 |
return false; |
| 20 |
|
| 21 |
if (ins->dest == ctx->blend_src1) |
| 22 |
return false; |
| 23 |
|
| 24 |
if (ins->type == TAG_LOAD_STORE_4) |
| 25 |
if (load_store_opcode_props[ins->op].props & LDST_SPECIAL_MASK) |
| 26 |
return false; |
| 27 |
|
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
static bool |
| 32 |
can_dce(midgard_instruction *ins) |
| 33 |
{ |
| 34 |
if (ins->mask) |
| 35 |
return false; |
| 36 |
|
| 37 |
if (ins->compact_branch) |
| 38 |
return false; |
| 39 |
|
| 40 |
if (ins->type == TAG_LOAD_STORE_4) |
| 41 |
if (load_store_opcode_props[ins->op].props & LDST_SIDE_FX) |
| 42 |
return false; |
| 43 |
|
| 44 |
if (ins->type == TAG_TEXTURE_4) |
| 45 |
if (ins->op == midgard_tex_op_barrier) |
| 46 |
return false; |
| 47 |
|
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
static bool |
| 52 |
midgard_opt_dead_code_eliminate_block(compiler_context *ctx, |
| 53 |
midgard_block *block) |
| 54 |
{ |
| 55 |
bool progress = false; |
| 56 |
|
| 57 |
uint16_t *live = |
| 58 |
mem_dup(block->live_out, ctx->temp_count * sizeof(uint16_t)); |
| 59 |
|
| 60 |
mir_foreach_instr_in_block_rev(block, ins) { |
| 61 |
if (can_cull_mask(ctx, ins)) { |
| 62 |
unsigned type_size = nir_alu_type_get_type_size(ins->dest_type); |
| 63 |
unsigned round_size = type_size; |
| 64 |
unsigned oldmask = ins->mask; |
| 65 |
|
| 66 |
/* Make sure we're packable */ |
| 67 |
if (type_size < 32 && ins->type == TAG_LOAD_STORE_4) |
| 68 |
round_size = 32; |
| 69 |
|
| 70 |
unsigned rounded = mir_round_bytemask_up(live[ins->dest], round_size); |
| 71 |
unsigned cmask = mir_from_bytemask(rounded, type_size); |
| 72 |
|
| 73 |
ins->mask &= cmask; |
| 74 |
progress |= (ins->mask != oldmask); |
| 75 |
} |
| 76 |
|
| 77 |
mir_liveness_ins_update(live, ins, ctx->temp_count); |
| 78 |
} |
| 79 |
|
| 80 |
mir_foreach_instr_in_block_safe(block, ins) { |
| 81 |
if (can_dce(ins)) { |
| 82 |
mir_remove_instruction(ins); |
| 83 |
progress = true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
free(live); |
| 88 |
|
| 89 |
return progress; |
| 90 |
} |
| 91 |
|
| 92 |
bool |
| 93 |
midgard_opt_dead_code_eliminate(compiler_context *ctx) |
| 94 |
{ |
| 95 |
/* We track liveness. In fact, it's ok if we assume more things are |
| 96 |
* live than they actually are, that just reduces the effectiveness of |
| 97 |
* this iterations lightly. And DCE has the effect of strictly reducing |
| 98 |
* liveness, so we can run DCE across all blocks while only computing |
| 99 |
* liveness at the beginning. */ |
| 100 |
|
| 101 |
mir_invalidate_liveness(ctx); |
| 102 |
mir_compute_liveness(ctx); |
| 103 |
|
| 104 |
bool progress = false; |
| 105 |
|
| 106 |
mir_foreach_block(ctx, block) { |
| 107 |
progress |= |
| 108 |
midgard_opt_dead_code_eliminate_block(ctx, (midgard_block *)block); |
| 109 |
} |
| 110 |
|
| 111 |
return progress; |
| 112 |
} |
| 113 |
|
| 114 |
/* Removes dead moves, that is, moves with a destination overwritten before |
| 115 |
* being read. Normally handled implicitly as part of DCE, but this has to run |
| 116 |
* after the out-of-SSA pass */ |
| 117 |
|
| 118 |
bool |
| 119 |
midgard_opt_dead_move_eliminate(compiler_context *ctx, midgard_block *block) |
| 120 |
{ |
| 121 |
bool progress = false; |
| 122 |
|
| 123 |
mir_foreach_instr_in_block_safe(block, ins) { |
| 124 |
if (ins->type != TAG_ALU_4) |
| 125 |
continue; |
| 126 |
if (ins->compact_branch) |
| 127 |
continue; |
| 128 |
if (!OP_IS_MOVE(ins->op)) |
| 129 |
continue; |
| 130 |
|
| 131 |
/* Check if it's overwritten in this block before being read */ |
| 132 |
bool overwritten = false; |
| 133 |
|
| 134 |
mir_foreach_instr_in_block_from(block, q, mir_next_op(ins)) { |
| 135 |
/* Check if used */ |
| 136 |
if (mir_has_arg(q, ins->dest)) |
| 137 |
break; |
| 138 |
|
| 139 |
/* Check if overwritten */ |
| 140 |
if (q->dest == ins->dest) { |
| 141 |
/* Special case to vec4; component tracking is |
| 142 |
* harder */ |
| 143 |
|
| 144 |
overwritten = (q->mask == 0xF); |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if (overwritten) { |
| 150 |
mir_remove_instruction(ins); |
| 151 |
progress = true; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return progress; |
| 156 |
} |