summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/vc4/vc4_opt_algebraic.c
blob: 5b435832b92fa6f78d0a1e501096146bd8fd7b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright © 2014 Broadcom
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * @file vc4_opt_algebraic.c
 *
 * This is the optimization pass for miscellaneous changes to instructions
 * where we can simplify the operation by some knowledge about the specific
 * operations.
 *
 * Mostly this will be a matter of turning things into MOVs so that they can
 * later be copy-propagated out.
 */

#include "vc4_qir.h"
#include "util/u_math.h"

static bool debug;

static void
dump_from(struct vc4_compile *c, struct qinst *inst)
{
        if (!debug)
                return;

        fprintf(stderr, "optimizing: ");
        qir_dump_inst(c, inst);
        fprintf(stderr, "\n");
}

static void
dump_to(struct vc4_compile *c, struct qinst *inst)
{
        if (!debug)
                return;

        fprintf(stderr, "to: ");
        qir_dump_inst(c, inst);
        fprintf(stderr, "\n");
}

static bool
is_constant_value(struct vc4_compile *c, struct qreg reg,
                  uint32_t val)
{
        if (reg.file == QFILE_UNIF &&
            c->uniform_contents[reg.index] == QUNIFORM_CONSTANT &&
            c->uniform_data[reg.index] == val) {
                return true;
        }

        if (reg.file == QFILE_SMALL_IMM && reg.index == val)
                return true;

        return false;
}

static bool
is_zero(struct vc4_compile *c, struct qreg reg)
{
        reg = qir_follow_movs(c, reg);
        return is_constant_value(c, reg, 0);
}

static bool
is_1f(struct vc4_compile *c, struct qreg reg)
{
        reg = qir_follow_movs(c, reg);
        return is_constant_value(c, reg, fui(1.0));
}

static void
replace_with_mov(struct vc4_compile *c, struct qinst *inst, struct qreg arg)
{
        dump_from(c, inst);
        inst->op = QOP_MOV;
        inst->src[0] = arg;
        inst->src[1] = c->undef;
        dump_to(c, inst);
}

static bool
replace_x_0_with_x(struct vc4_compile *c,
                 struct qinst *inst,
                 int arg)
{
        if (!is_zero(c, inst->src[arg]))
                return false;
        replace_with_mov(c, inst, inst->src[1 - arg]);
        return true;
}

static bool
replace_x_0_with_0(struct vc4_compile *c,
                  struct qinst *inst,
                  int arg)
{
        if (!is_zero(c, inst->src[arg]))
                return false;
        replace_with_mov(c, inst, inst->src[arg]);
        return true;
}

static bool
fmul_replace_one(struct vc4_compile *c,
                 struct qinst *inst,
                 int arg)
{
        if (!is_1f(c, inst->src[arg]))
                return false;
        replace_with_mov(c, inst, inst->src[1 - arg]);
        return true;
}

bool
qir_opt_algebraic(struct vc4_compile *c)
{
        bool progress = false;

        list_for_each_entry(struct qinst, inst, &c->instructions, link) {
                switch (inst->op) {
                case QOP_SEL_X_Y_ZS:
                case QOP_SEL_X_Y_ZC:
                case QOP_SEL_X_Y_NS:
                case QOP_SEL_X_Y_NC:
                        if (is_zero(c, inst->src[1])) {
                                /* Replace references to a 0 uniform value
                                 * with the SEL_X_0 equivalent.
                                 */
                                dump_from(c, inst);
                                inst->op -= (QOP_SEL_X_Y_ZS - QOP_SEL_X_0_ZS);
                                inst->src[1] = c->undef;
                                progress = true;
                                dump_to(c, inst);
                                break;
                        }

                        if (is_zero(c, inst->src[0])) {
                                /* Replace references to a 0 uniform value
                                 * with the SEL_X_0 equivalent, flipping the
                                 * condition being evaluated since the operand
                                 * order is flipped.
                                 */
                                dump_from(c, inst);
                                inst->op -= QOP_SEL_X_Y_ZS;
                                inst->op ^= 1;
                                inst->op += QOP_SEL_X_0_ZS;
                                inst->src[0] = inst->src[1];
                                inst->src[1] = c->undef;
                                progress = true;
                                dump_to(c, inst);
                                break;
                        }

                        break;

                case QOP_FSUB:
                case QOP_SUB:
                        if (is_zero(c, inst->src[1])) {
                                replace_with_mov(c, inst, inst->src[0]);
                        }
                        break;

                case QOP_ADD:
                        if (replace_x_0_with_x(c, inst, 0) ||
                            replace_x_0_with_x(c, inst, 1)) {
                                progress = true;
                                break;
                        }
                        break;

                case QOP_FADD:
                        if (replace_x_0_with_x(c, inst, 0) ||
                            replace_x_0_with_x(c, inst, 1)) {
                                progress = true;
                                break;
                        }

                        /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
                        if (inst->src[1].file == QFILE_TEMP &&
                            c->defs[inst->src[1].index] &&
                            c->defs[inst->src[1].index]->op == QOP_FSUB) {
                                struct qinst *fsub = c->defs[inst->src[1].index];
                                if (is_zero(c, fsub->src[0])) {
                                        dump_from(c, inst);
                                        inst->op = QOP_FSUB;
                                        inst->src[1] = fsub->src[1];
                                        progress = true;
                                        dump_to(c, inst);
                                        break;
                                }
                        }

                        /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
                        if (inst->src[0].file == QFILE_TEMP &&
                            c->defs[inst->src[0].index] &&
                            c->defs[inst->src[0].index]->op == QOP_FSUB) {
                                struct qinst *fsub = c->defs[inst->src[0].index];
                                if (is_zero(c, fsub->src[0])) {
                                        dump_from(c, inst);
                                        inst->op = QOP_FSUB;
                                        inst->src[0] = inst->src[1];
                                        inst->src[1] = fsub->src[1];
                                        dump_to(c, inst);
                                        progress = true;
                                        break;
                                }
                        }
                        break;

                case QOP_FMUL:
                        if (!inst->dst.pack &&
                            (replace_x_0_with_0(c, inst, 0) ||
                             replace_x_0_with_0(c, inst, 1) ||
                             fmul_replace_one(c, inst, 0) ||
                             fmul_replace_one(c, inst, 1))) {
                                progress = true;
                                break;
                        }
                        break;

                case QOP_MUL24:
                        if (!inst->dst.pack &&
                            (replace_x_0_with_0(c, inst, 0) ||
                             replace_x_0_with_0(c, inst, 1))) {
                                progress = true;
                                break;
                        }
                        break;

                case QOP_AND:
                        if (replace_x_0_with_0(c, inst, 0) ||
                            replace_x_0_with_0(c, inst, 1)) {
                                progress = true;
                                break;
                        }

                        if (is_constant_value(c, inst->src[0], ~0)) {
                                replace_with_mov(c, inst, inst->src[1]);
                                progress = true;
                                break;
                        }
                        if (is_constant_value(c, inst->src[1], ~0)) {
                                replace_with_mov(c, inst, inst->src[0]);
                                progress = true;
                                break;
                        }
                        break;

                case QOP_OR:
                        if (replace_x_0_with_x(c, inst, 0) ||
                            replace_x_0_with_x(c, inst, 1)) {
                                progress = true;
                                break;
                        }
                        break;

                case QOP_RCP:
                        if (is_1f(c, inst->src[0])) {
                                replace_with_mov(c, inst, inst->src[0]);
                                progress = true;
                                break;
                        }
                        break;

                default:
                        break;
                }
        }

        return progress;
}