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