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 "bi_builder.h"
26 #include "compiler.h"
27
28 /* SSA copy propagation */
29
30 static bool
bi_reads_fau(bi_instr * ins)31 bi_reads_fau(bi_instr *ins)
32 {
33 bi_foreach_src(ins, s) {
34 if (ins->src[s].type == BI_INDEX_FAU)
35 return true;
36 }
37
38 return false;
39 }
40
41 void
bi_opt_copy_prop(bi_context * ctx)42 bi_opt_copy_prop(bi_context *ctx)
43 {
44 /* Chase SPLIT of COLLECT. Instruction selection usually avoids this
45 * pattern (due to the split cache), but it is inevitably generated by
46 * the UBO pushing pass.
47 */
48 bi_instr **collects = calloc(sizeof(bi_instr *), ctx->ssa_alloc);
49 bi_foreach_instr_global_safe(ctx, I) {
50 if (I->op == BI_OPCODE_COLLECT_I32) {
51 /* Rewrite trivial collects while we're at it */
52 if (I->nr_srcs == 1)
53 I->op = BI_OPCODE_MOV_I32;
54
55 collects[I->dest[0].value] = I;
56 } else if (I->op == BI_OPCODE_SPLIT_I32) {
57 /* Rewrite trivial splits while we're at it */
58 if (I->nr_dests == 1)
59 I->op = BI_OPCODE_MOV_I32;
60
61 bi_instr *collect = collects[I->src[0].value];
62 if (!collect)
63 continue;
64
65 /* Lower the split to moves, copyprop cleans up */
66 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
67
68 bi_foreach_dest(I, d)
69 bi_mov_i32_to(&b, I->dest[d], collect->src[d]);
70
71 bi_remove_instruction(I);
72 }
73 }
74
75 free(collects);
76
77 bi_index *replacement = calloc(sizeof(bi_index), ctx->ssa_alloc);
78
79 bi_foreach_instr_global_safe(ctx, ins) {
80 if (ins->op == BI_OPCODE_MOV_I32 &&
81 ins->src[0].type != BI_INDEX_REGISTER) {
82 bi_index replace = ins->src[0];
83
84 /* Peek through one layer so copyprop converges in one
85 * iteration for chained moves */
86 if (bi_is_ssa(replace)) {
87 bi_index chained = replacement[replace.value];
88
89 if (!bi_is_null(chained))
90 replace = chained;
91 }
92
93 assert(ins->nr_dests == 1);
94 replacement[ins->dest[0].value] = replace;
95 }
96
97 bi_foreach_src(ins, s) {
98 bi_index use = ins->src[s];
99
100 if (use.type != BI_INDEX_NORMAL)
101 continue;
102 if (bi_is_staging_src(ins, s))
103 continue;
104
105 bi_index repl = replacement[use.value];
106
107 if (repl.type == BI_INDEX_CONSTANT && bi_reads_fau(ins))
108 continue;
109
110 if (!bi_is_null(repl))
111 bi_replace_src(ins, s, repl);
112 }
113 }
114
115 free(replacement);
116 }
117