pan/midgard: Move DCE into its own file
[mesa.git] / src / panfrost / midgard / midgard_opt_copy_prop.c
1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019 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 "midgard_ops.h"
27
28 static bool
29 mir_nontrivial_outmod(midgard_instruction *ins)
30 {
31 bool is_int = midgard_is_integer_op(ins->alu.op);
32 unsigned mod = ins->alu.outmod;
33
34 /* Type conversion is a sort of outmod */
35 if (ins->alu.dest_override != midgard_dest_override_none)
36 return true;
37
38 if (is_int)
39 return mod != midgard_outmod_int_wrap;
40 else
41 return mod != midgard_outmod_none;
42 }
43
44 bool
45 midgard_opt_copy_prop(compiler_context *ctx, midgard_block *block)
46 {
47 bool progress = false;
48
49 mir_foreach_instr_in_block_safe(block, ins) {
50 if (ins->type != TAG_ALU_4) continue;
51 if (!OP_IS_MOVE(ins->alu.op)) continue;
52
53 unsigned from = ins->ssa_args.src1;
54 unsigned to = ins->ssa_args.dest;
55
56 /* We only work on pure SSA */
57
58 if (to >= SSA_FIXED_MINIMUM) continue;
59 if (from >= SSA_FIXED_MINIMUM) continue;
60 if (to >= ctx->func->impl->ssa_alloc) continue;
61 if (from >= ctx->func->impl->ssa_alloc) continue;
62
63 /* Constant propagation is not handled here, either */
64 if (ins->ssa_args.inline_constant) continue;
65 if (ins->has_constants) continue;
66
67 /* Modifier propagation is not handled here */
68 if (mir_nontrivial_source2_mod(ins)) continue;
69 if (mir_nontrivial_outmod(ins)) continue;
70
71 /* We're clear -- rewrite */
72 mir_rewrite_index_src(ctx, to, from);
73 mir_remove_instruction(ins);
74 progress |= true;
75 }
76
77 return progress;
78 }