1 /*
2 * Copyright © 2019 Google.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/ralloc.h"
7
8 #include "ir3.h"
9
10 static bool
is_safe_conv(struct ir3_instruction * instr,type_t src_type,opc_t * src_opc)11 is_safe_conv(struct ir3_instruction *instr, type_t src_type, opc_t *src_opc)
12 {
13 if (instr->opc != OPC_MOV)
14 return false;
15
16 /* Only allow half->full or full->half without any type conversion (like
17 * int to float).
18 */
19 if (type_size(instr->cat1.src_type) == type_size(instr->cat1.dst_type) ||
20 full_type(instr->cat1.src_type) != full_type(instr->cat1.dst_type))
21 return false;
22
23 /* mul.s24/u24 always return 32b result regardless of its sources size,
24 * hence we cannot guarantee the high 16b of dst being zero or sign extended.
25 */
26 if ((*src_opc == OPC_MUL_S24 || *src_opc == OPC_MUL_U24) &&
27 type_size(instr->cat1.src_type) == 16)
28 return false;
29
30 /* mad.x24 doesn't work with 16-bit in/out */
31 if (*src_opc == OPC_MAD_S24 || *src_opc == OPC_MAD_U24)
32 return false;
33
34 struct ir3_register *dst = instr->dsts[0];
35 struct ir3_register *src = instr->srcs[0];
36
37 /* disallow conversions that cannot be folded into
38 * alu instructions:
39 */
40 if (instr->cat1.round != ROUND_ZERO)
41 return false;
42
43 if (dst->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
44 return false;
45 if (src->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
46 return false;
47
48 /* Check that the source of the conv matches the type of the src
49 * instruction.
50 */
51 if (src_type == instr->cat1.src_type)
52 return true;
53
54 /* We can handle mismatches with integer types by converting the opcode
55 * but not when an integer is reinterpreted as a float or vice-versa. We
56 * can't handle types with different sizes.
57 */
58 if (type_float(src_type) != type_float(instr->cat1.src_type) ||
59 type_size(src_type) != type_size(instr->cat1.src_type))
60 return false;
61
62 /* We have types with mismatched signedness. Mismatches on the signedness
63 * don't matter when narrowing:
64 */
65 if (type_size(instr->cat1.dst_type) < type_size(instr->cat1.src_type))
66 return true;
67
68 /* Try swapping the opcode: */
69 bool can_swap = true;
70 *src_opc = ir3_try_swap_signedness(*src_opc, &can_swap);
71 return can_swap;
72 }
73
74 static bool
all_uses_safe_conv(struct ir3_instruction * conv_src,type_t src_type)75 all_uses_safe_conv(struct ir3_instruction *conv_src, type_t src_type)
76 {
77 opc_t opc = conv_src->opc;
78 bool first = true;
79 foreach_ssa_use (use, conv_src) {
80 opc_t new_opc = opc;
81 if (!is_safe_conv(use, src_type, &new_opc))
82 return false;
83 /* Check if multiple uses have conflicting requirements on the opcode.
84 */
85 if (!first && opc != new_opc)
86 return false;
87 first = false;
88 opc = new_opc;
89 }
90 conv_src->opc = opc;
91 return true;
92 }
93
94 /* For an instruction which has a conversion folded in, re-write the
95 * uses of *all* conv's that used that src to be a simple mov that
96 * cp can eliminate. This avoids invalidating the SSA uses, it just
97 * shifts the use to a simple mov.
98 */
99 static void
rewrite_src_uses(struct ir3_instruction * src)100 rewrite_src_uses(struct ir3_instruction *src)
101 {
102 foreach_ssa_use (use, src) {
103 assert(use->opc == OPC_MOV);
104
105 if (is_half(src)) {
106 use->srcs[0]->flags |= IR3_REG_HALF;
107 } else {
108 use->srcs[0]->flags &= ~IR3_REG_HALF;
109 }
110
111 use->cat1.src_type = use->cat1.dst_type;
112 }
113 }
114
115 static bool
try_conversion_folding(struct ir3_instruction * conv)116 try_conversion_folding(struct ir3_instruction *conv)
117 {
118 struct ir3_instruction *src;
119
120 if (conv->opc != OPC_MOV)
121 return false;
122
123 /* Don't fold in conversions to/from shared */
124 if ((conv->srcs[0]->flags & IR3_REG_SHARED) !=
125 (conv->dsts[0]->flags & IR3_REG_SHARED))
126 return false;
127
128 /* NOTE: we can have non-ssa srcs after copy propagation: */
129 src = ssa(conv->srcs[0]);
130 if (!src)
131 return false;
132
133 if (!is_alu(src))
134 return false;
135
136 bool can_fold;
137 type_t base_type = ir3_output_conv_type(src, &can_fold);
138 if (!can_fold)
139 return false;
140
141 type_t src_type = ir3_output_conv_src_type(src, base_type);
142 type_t dst_type = ir3_output_conv_dst_type(src, base_type);
143
144 /* Avoid cases where we've already folded in a conversion. We assume that
145 * if there is a chain of conversions that's foldable then it's been
146 * folded in NIR already.
147 */
148 if (src_type != dst_type)
149 return false;
150
151 if (!all_uses_safe_conv(src, src_type))
152 return false;
153
154 ir3_set_dst_type(src, is_half(conv));
155 rewrite_src_uses(src);
156
157 return true;
158 }
159
160 bool
ir3_cf(struct ir3 * ir)161 ir3_cf(struct ir3 *ir)
162 {
163 void *mem_ctx = ralloc_context(NULL);
164 bool progress = false;
165
166 ir3_find_ssa_uses(ir, mem_ctx, false);
167
168 foreach_block (block, &ir->block_list) {
169 foreach_instr (instr, &block->instr_list) {
170 progress |= try_conversion_folding(instr);
171 }
172 }
173
174 ralloc_free(mem_ctx);
175
176 return progress;
177 }
178