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 "util/bitscan.h"
26 #include "compiler.h"
27 #include "midgard_ops.h"
28
29 static bool
identity_swizzle(midgard_instruction * mov)30 identity_swizzle(midgard_instruction *mov)
31 {
32 /* Write mask should be minimal in SSA form */
33 assert(mir_is_ssa(mov->src[1]));
34 assert(mov->mask == BITFIELD_MASK(util_last_bit(mov->mask)));
35
36 for (unsigned i = 0; i < util_last_bit(mov->mask); ++i) {
37 if (mov->swizzle[1][i] != i)
38 return false;
39 }
40
41 return true;
42 }
43
44 bool
midgard_opt_copy_prop(compiler_context * ctx,midgard_block * block)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)
51 continue;
52 if (!OP_IS_MOVE(ins->op))
53 continue;
54 if (ins->is_pack)
55 continue;
56
57 unsigned from = ins->src[1];
58 unsigned to = ins->dest;
59
60 /* We only work on pure SSA */
61
62 if (to & PAN_IS_REG)
63 continue;
64 if (from & PAN_IS_REG)
65 continue;
66
67 /* Constant propagation is not handled here, either */
68 if (ins->has_inline_constant)
69 continue;
70 if (ins->has_constants)
71 continue;
72
73 /* Modifier propagation is not handled here */
74 if (mir_nontrivial_mod(ins, 1, false))
75 continue;
76 if (mir_nontrivial_outmod(ins))
77 continue;
78
79 /* Shortened arguments (bias for textures, extra load/store
80 * arguments, etc.) do not get a swizzle, only a start
81 * component and even that is restricted. Fragment writeout
82 * doesn't even get that much */
83
84 bool no_swizzle = false;
85
86 mir_foreach_instr_global(ctx, q) {
87 bool is_tex = q->type == TAG_TEXTURE_4;
88 bool is_ldst = q->type == TAG_LOAD_STORE_4;
89 bool is_branch = q->compact_branch;
90
91 if (!(is_tex || is_ldst || is_branch))
92 continue;
93
94 /* For textures, we get a real swizzle for the
95 * coordinate and the content. For stores, we get one.
96 * For loads, we get none. */
97
98 unsigned start = is_tex ? 2 : OP_IS_STORE(q->op) ? 1 : 0;
99
100 mir_foreach_src(q, s) {
101 if ((s >= start) && q->src[s] == to) {
102 no_swizzle = true;
103 break;
104 }
105 }
106 }
107
108 if (no_swizzle && !identity_swizzle(ins))
109 continue;
110
111 if (ctx->blend_src1 == to)
112 ctx->blend_src1 = from;
113
114 /* We're clear -- rewrite, composing the swizzle */
115 mir_rewrite_index_src_swizzle(ctx, to, from, ins->swizzle[1]);
116 mir_remove_instruction(ins);
117 progress |= true;
118 }
119
120 return progress;
121 }
122