Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/freedreno/ir3/ir3_cf.c
4565 views
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
28
static bool
29
is_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
struct ir3_register *dst = instr->dsts[0];
42
struct ir3_register *src = instr->srcs[0];
43
44
/* disallow conversions that cannot be folded into
45
* alu instructions:
46
*/
47
if (instr->cat1.round != ROUND_ZERO)
48
return false;
49
50
if (dst->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
51
return false;
52
if (src->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
53
return false;
54
55
/* Check that the source of the conv matches the type of the src
56
* instruction.
57
*/
58
if (src_type == instr->cat1.src_type)
59
return true;
60
61
/* We can handle mismatches with integer types by converting the opcode
62
* but not when an integer is reinterpreted as a float or vice-versa.
63
*/
64
if (type_float(src_type) != type_float(instr->cat1.src_type))
65
return false;
66
67
/* We have types with mismatched signedness. Mismatches on the signedness
68
* don't matter when narrowing:
69
*/
70
if (type_size(instr->cat1.dst_type) < type_size(instr->cat1.src_type))
71
return true;
72
73
/* Try swapping the opcode: */
74
bool can_swap = true;
75
*src_opc = ir3_try_swap_signedness(*src_opc, &can_swap);
76
return can_swap;
77
}
78
79
static bool
80
all_uses_safe_conv(struct ir3_instruction *conv_src, type_t src_type)
81
{
82
opc_t opc = conv_src->opc;
83
bool first = true;
84
foreach_ssa_use (use, conv_src) {
85
opc_t new_opc = opc;
86
if (!is_safe_conv(use, src_type, &new_opc))
87
return false;
88
/* Check if multiple uses have conflicting requirements on the opcode.
89
*/
90
if (!first && opc != new_opc)
91
return false;
92
first = false;
93
opc = new_opc;
94
}
95
conv_src->opc = opc;
96
return true;
97
}
98
99
/* For an instruction which has a conversion folded in, re-write the
100
* uses of *all* conv's that used that src to be a simple mov that
101
* cp can eliminate. This avoids invalidating the SSA uses, it just
102
* shifts the use to a simple mov.
103
*/
104
static void
105
rewrite_src_uses(struct ir3_instruction *src)
106
{
107
foreach_ssa_use (use, src) {
108
assert(use->opc == OPC_MOV);
109
110
if (is_half(src)) {
111
use->srcs[0]->flags |= IR3_REG_HALF;
112
} else {
113
use->srcs[0]->flags &= ~IR3_REG_HALF;
114
}
115
116
use->cat1.src_type = use->cat1.dst_type;
117
}
118
}
119
120
static bool
121
try_conversion_folding(struct ir3_instruction *conv)
122
{
123
struct ir3_instruction *src;
124
125
if (conv->opc != OPC_MOV)
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
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
179