xref: /aosp_15_r20/external/mesa3d/src/freedreno/ir3/ir3_cf.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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    struct ir3_register *dst = instr->dsts[0];
31    struct ir3_register *src = instr->srcs[0];
32 
33    /* disallow conversions that cannot be folded into
34     * alu instructions:
35     */
36    if (instr->cat1.round != ROUND_ZERO)
37       return false;
38 
39    if (dst->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
40       return false;
41    if (src->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
42       return false;
43 
44    /* Check that the source of the conv matches the type of the src
45     * instruction.
46     */
47    if (src_type == instr->cat1.src_type)
48       return true;
49 
50    /* We can handle mismatches with integer types by converting the opcode
51     * but not when an integer is reinterpreted as a float or vice-versa.
52     */
53    if (type_float(src_type) != type_float(instr->cat1.src_type))
54       return false;
55 
56    /* We have types with mismatched signedness. Mismatches on the signedness
57     * don't matter when narrowing:
58     */
59    if (type_size(instr->cat1.dst_type) < type_size(instr->cat1.src_type))
60       return true;
61 
62    /* Try swapping the opcode: */
63    bool can_swap = true;
64    *src_opc = ir3_try_swap_signedness(*src_opc, &can_swap);
65    return can_swap;
66 }
67 
68 static bool
all_uses_safe_conv(struct ir3_instruction * conv_src,type_t src_type)69 all_uses_safe_conv(struct ir3_instruction *conv_src, type_t src_type)
70 {
71    opc_t opc = conv_src->opc;
72    bool first = true;
73    foreach_ssa_use (use, conv_src) {
74       opc_t new_opc = opc;
75       if (!is_safe_conv(use, src_type, &new_opc))
76          return false;
77       /* Check if multiple uses have conflicting requirements on the opcode.
78        */
79       if (!first && opc != new_opc)
80          return false;
81       first = false;
82       opc = new_opc;
83    }
84    conv_src->opc = opc;
85    return true;
86 }
87 
88 /* For an instruction which has a conversion folded in, re-write the
89  * uses of *all* conv's that used that src to be a simple mov that
90  * cp can eliminate.  This avoids invalidating the SSA uses, it just
91  * shifts the use to a simple mov.
92  */
93 static void
rewrite_src_uses(struct ir3_instruction * src)94 rewrite_src_uses(struct ir3_instruction *src)
95 {
96    foreach_ssa_use (use, src) {
97       assert(use->opc == OPC_MOV);
98 
99       if (is_half(src)) {
100          use->srcs[0]->flags |= IR3_REG_HALF;
101       } else {
102          use->srcs[0]->flags &= ~IR3_REG_HALF;
103       }
104 
105       use->cat1.src_type = use->cat1.dst_type;
106    }
107 }
108 
109 static bool
try_conversion_folding(struct ir3_instruction * conv)110 try_conversion_folding(struct ir3_instruction *conv)
111 {
112    struct ir3_instruction *src;
113 
114    if (conv->opc != OPC_MOV)
115       return false;
116 
117    /* Don't fold in conversions to/from shared */
118    if ((conv->srcs[0]->flags & IR3_REG_SHARED) !=
119        (conv->dsts[0]->flags & IR3_REG_SHARED))
120       return false;
121 
122    /* NOTE: we can have non-ssa srcs after copy propagation: */
123    src = ssa(conv->srcs[0]);
124    if (!src)
125       return false;
126 
127    if (!is_alu(src))
128       return false;
129 
130    bool can_fold;
131    type_t base_type = ir3_output_conv_type(src, &can_fold);
132    if (!can_fold)
133       return false;
134 
135    type_t src_type = ir3_output_conv_src_type(src, base_type);
136    type_t dst_type = ir3_output_conv_dst_type(src, base_type);
137 
138    /* Avoid cases where we've already folded in a conversion. We assume that
139     * if there is a chain of conversions that's foldable then it's been
140     * folded in NIR already.
141     */
142    if (src_type != dst_type)
143       return false;
144 
145    if (!all_uses_safe_conv(src, src_type))
146       return false;
147 
148    ir3_set_dst_type(src, is_half(conv));
149    rewrite_src_uses(src);
150 
151    return true;
152 }
153 
154 bool
ir3_cf(struct ir3 * ir)155 ir3_cf(struct ir3 *ir)
156 {
157    void *mem_ctx = ralloc_context(NULL);
158    bool progress = false;
159 
160    ir3_find_ssa_uses(ir, mem_ctx, false);
161 
162    foreach_block (block, &ir->block_list) {
163       foreach_instr (instr, &block->instr_list) {
164          progress |= try_conversion_folding(instr);
165       }
166    }
167 
168    ralloc_free(mem_ctx);
169 
170    return progress;
171 }
172