1/*
2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24/**
25 * @file vc4_opt_coalesce_ff_writes.c
26 *
27 * This modifies instructions that generate the value consumed by a VPM or TMU
28 * coordinate write to write directly into the VPM or TMU.
29 */
30
31#include "vc4_qir.h"
32
33bool
34qir_opt_coalesce_ff_writes(struct vc4_compile *c)
35{
36        /* For now, only do this pass when we don't have control flow. */
37        struct qblock *block = qir_entry_block(c);
38        if (block != qir_exit_block(c))
39                return false;
40
41        bool progress = false;
42        uint32_t use_count[c->num_temps];
43        memset(&use_count, 0, sizeof(use_count));
44
45        qir_for_each_inst_inorder(inst, c) {
46                for (int i = 0; i < qir_get_nsrc(inst); i++) {
47                        if (inst->src[i].file == QFILE_TEMP) {
48                                uint32_t temp = inst->src[i].index;
49                                use_count[temp]++;
50                        }
51                }
52        }
53
54        qir_for_each_inst_inorder(mov_inst, c) {
55                if (!qir_is_raw_mov(mov_inst) || mov_inst->sf)
56                        continue;
57                if (mov_inst->src[0].file != QFILE_TEMP)
58                        continue;
59
60                if (!(mov_inst->dst.file == QFILE_VPM || qir_is_tex(mov_inst)))
61                        continue;
62
63                uint32_t temp = mov_inst->src[0].index;
64                if (use_count[temp] != 1)
65                        continue;
66
67                struct qinst *inst = c->defs[temp];
68                if (!inst)
69                        continue;
70
71                /* Don't bother trying to fold in an ALU op using a uniform to
72                 * a texture op, as we'll just have to lower the uniform back
73                 * out.
74                 */
75                if (qir_is_tex(mov_inst) && qir_has_uniform_read(inst))
76                        continue;
77
78                if (qir_depends_on_flags(inst) || inst->sf)
79                        continue;
80
81                if (qir_has_side_effects(c, inst) ||
82                    qir_has_side_effect_reads(c, inst) ||
83                    inst->op == QOP_VARY_ADD_C) {
84                        continue;
85                }
86
87                /* Move the generating instruction into the position of the FF
88                 * write.
89                 */
90                c->defs[inst->dst.index] = NULL;
91                inst->dst.file = mov_inst->dst.file;
92                inst->dst.index = mov_inst->dst.index;
93                if (qir_has_implicit_tex_uniform(mov_inst)) {
94                        inst->src[qir_get_tex_uniform_src(inst)] =
95                                mov_inst->src[qir_get_tex_uniform_src(mov_inst)];
96                }
97
98                list_del(&inst->link);
99                list_addtail(&inst->link, &mov_inst->link);
100
101                qir_remove_instruction(c, mov_inst);
102
103                progress = true;
104        }
105
106        return progress;
107}
108