1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "rogue.h"
25 #include "rogue_builder.h"
26 #include "util/macros.h"
27
28 #include <stdbool.h>
29
30 /**
31 * \file rogue_copy_prop.c
32 *
33 * \brief Contains the rogue_copy_prop pass.
34 */
35
can_back_prop(rogue_alu_instr * mov)36 static bool can_back_prop(rogue_alu_instr *mov)
37 {
38 /* TODO: Check for src/dst modifiers when support is added for them. */
39
40 if (!rogue_ref_is_reg(&mov->src[0].ref) ||
41 !rogue_ref_is_reg(&mov->dst[0].ref))
42 return false;
43
44 if (mov->src[0].ref.reg->regarray)
45 return false;
46
47 /* Vertex outputs require uvsw.write; only back-propagate if the parent
48 * instruction is also a mov. */
49 if (mov->dst[0].ref.reg->class == ROGUE_REG_CLASS_VTXOUT) {
50 rogue_reg_write *write =
51 list_first_entry(&mov->src[0].ref.reg->writes, rogue_reg_write, link);
52
53 if (write->instr->type != ROGUE_INSTR_TYPE_ALU)
54 return false;
55
56 rogue_alu_instr *alu = rogue_instr_as_alu(write->instr);
57 if (alu->op != ROGUE_ALU_OP_MOV)
58 return false;
59 }
60
61 /* Is the source register only written to once? */
62 if (!list_is_singular(&mov->src[0].ref.reg->writes))
63 return false;
64
65 /* Is this the only instruction that writes to this register? */
66 if (!list_is_singular(&mov->dst[0].ref.reg->writes))
67 return false;
68
69 return true;
70 }
71
can_forward_prop(rogue_alu_instr * mov)72 static bool can_forward_prop(rogue_alu_instr *mov)
73 {
74 /* TODO: Check for src/dst modifiers when support is added for them. */
75
76 if (!rogue_ref_is_reg(&mov->src[0].ref) ||
77 !rogue_ref_is_reg(&mov->dst[0].ref))
78 return false;
79
80 if (mov->dst[0].ref.reg->regarray)
81 return false;
82
83 if (mov->dst[0].ref.reg->class != ROGUE_REG_CLASS_SSA)
84 return false;
85
86 /* Is the source register written to more than once (driver-supplied regs can
87 * have zero writes)? */
88 if (list_length(&mov->src[0].ref.reg->writes) > 1)
89 return false;
90
91 /* Is this the only instruction that writes to this register? */
92 if (!list_is_singular(&mov->dst[0].ref.reg->writes))
93 return false;
94
95 return true;
96 }
97
rogue_back_prop(rogue_alu_instr * mov)98 static bool rogue_back_prop(rogue_alu_instr *mov)
99 {
100 rogue_reg *mov_src = mov->src[0].ref.reg;
101 rogue_reg *mov_dst = mov->dst[0].ref.reg;
102
103 rogue_reg_write *write =
104 list_first_entry(&mov_src->writes, rogue_reg_write, link);
105
106 if (!rogue_dst_reg_replace(write, mov_dst))
107 return false;
108
109 rogue_instr_delete(&mov->instr);
110
111 return true;
112 }
113
rogue_forward_prop(rogue_alu_instr * mov)114 static bool rogue_forward_prop(rogue_alu_instr *mov)
115 {
116 bool success = true;
117
118 rogue_reg *mov_src = mov->src[0].ref.reg;
119 rogue_reg *mov_dst = mov->dst[0].ref.reg;
120
121 rogue_foreach_reg_use_safe (use, mov_dst)
122 if (rogue_can_replace_reg_use(use, mov_src))
123 success &= rogue_src_reg_replace(use, mov_src);
124 else
125 success = false;
126
127 if (!success)
128 return false;
129
130 rogue_instr_delete(&mov->instr);
131
132 return true;
133 }
134
135 /* Copy propagation pass. */
136 /* Forward and back, so that regarrays can be handled to a degree (making sure
137 * to propagate the regarray register rather than replace it and mess up the
138 * contiguous numbering). */
139 PUBLIC
rogue_copy_prop(rogue_shader * shader)140 bool rogue_copy_prop(rogue_shader *shader)
141 {
142 if (shader->is_grouped)
143 return false;
144
145 bool progress = false;
146
147 rogue_foreach_instr_in_shader_safe (instr, shader) {
148 if (instr->type != ROGUE_INSTR_TYPE_ALU)
149 continue;
150
151 rogue_alu_instr *mov = rogue_instr_as_alu(instr);
152 if (mov->op != ROGUE_ALU_OP_MOV)
153 continue;
154
155 if (can_forward_prop(mov))
156 progress |= rogue_forward_prop(mov);
157 else if (can_back_prop(mov))
158 progress |= rogue_back_prop(mov);
159 }
160
161 return progress;
162 }
163