pan/midgard: Do not propagate swizzles into writeout
[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 bool
29 midgard_opt_copy_prop(compiler_context *ctx, midgard_block *block)
30 {
31 bool progress = false;
32
33 mir_foreach_instr_in_block_safe(block, ins) {
34 if (ins->type != TAG_ALU_4) continue;
35 if (!OP_IS_MOVE(ins->alu.op)) continue;
36
37 unsigned from = ins->src[1];
38 unsigned to = ins->dest;
39
40 /* We only work on pure SSA */
41
42 if (to >= SSA_FIXED_MINIMUM) continue;
43 if (from >= SSA_FIXED_MINIMUM) continue;
44 if (to & IS_REG) continue;
45 if (from & IS_REG) continue;
46
47 /* Constant propagation is not handled here, either */
48 if (ins->has_inline_constant) continue;
49 if (ins->has_constants) continue;
50
51 /* Modifier propagation is not handled here */
52 if (mir_nontrivial_source2_mod_simple(ins)) continue;
53 if (mir_nontrivial_outmod(ins)) continue;
54
55 /* Shortened arguments (bias for textures, extra load/store
56 * arguments, etc.) do not get a swizzle, only a start
57 * component and even that is restricted. Fragment writeout
58 * doesn't even get that much */
59
60 bool skip = false;
61
62 mir_foreach_instr_global(ctx, q) {
63 bool is_tex = q->type == TAG_TEXTURE_4;
64 bool is_ldst = q->type == TAG_LOAD_STORE_4;
65 bool is_writeout = q->compact_branch && q->writeout;
66
67 if (!(is_tex || is_ldst || is_writeout)) continue;
68
69 /* For textures, we get one real swizzle. For stores,
70 * we also get one. For loads, we get none. */
71
72 unsigned start =
73 is_tex ? 1 :
74 OP_IS_STORE(q->load_store.op) ? 1 : 0;
75
76 mir_foreach_src(q, s) {
77 if ((s >= start) && q->src[s] == to) {
78 skip = true;
79 break;
80 }
81 }
82 }
83
84 if (skip)
85 continue;
86
87 /* We're clear -- rewrite, composing the swizzle */
88 midgard_vector_alu_src src2 =
89 vector_alu_from_unsigned(ins->alu.src2);
90
91 mir_rewrite_index_src_swizzle(ctx, to, from, src2.swizzle);
92 mir_remove_instruction(ins);
93 progress |= true;
94 }
95
96 return progress;
97 }