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