summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/vc4/vc4_opt_vpm.c
blob: 6f196e7d1b9922cfda0efa1393756a5e15326029 (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
/*
 * 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_vpm.c
 *
 * This modifies instructions that exclusively consume a value read from the
 * VPM to directly read the VPM if other operands allow it.
 */

#include "vc4_qir.h"

bool
qir_opt_vpm(struct vc4_compile *c)
{
        if (c->stage == QSTAGE_FRAG)
                return false;

        /* For now, only do this pass when we don't have control flow. */
        struct qblock *block = qir_entry_block(c);
        if (block != qir_exit_block(c))
                return false;

        bool progress = false;
        uint32_t use_count[c->num_temps];
        memset(&use_count, 0, sizeof(use_count));

        qir_for_each_inst_inorder(inst, c) {
                for (int i = 0; i < qir_get_nsrc(inst); i++) {
                        if (inst->src[i].file == QFILE_TEMP) {
                                uint32_t temp = inst->src[i].index;
                                use_count[temp]++;
                        }
                }
        }

        /* For instructions reading from a temporary that contains a VPM read
         * result, try to move the instruction up in place of the VPM read.
         */
        qir_for_each_inst_inorder(inst, c) {
                if (!inst)
                        continue;

                if (qir_depends_on_flags(inst) || inst->sf)
                        continue;

                if (qir_has_side_effects(c, inst) ||
                    qir_has_side_effect_reads(c, inst) ||
                    qir_is_tex(inst))
                        continue;

                for (int j = 0; j < qir_get_nsrc(inst); j++) {
                        if (inst->src[j].file != QFILE_TEMP ||
                            inst->src[j].pack)
                                continue;

                        uint32_t temp = inst->src[j].index;

                        /* Since VPM reads pull from a FIFO, we only get to
                         * read each VPM entry once (unless we reset the read
                         * pointer).  That means we can't copy-propagate a VPM
                         * read to multiple locations.
                         */
                        if (use_count[temp] != 1)
                                continue;

                        struct qinst *mov = c->defs[temp];
                        if (!mov ||
                            (mov->op != QOP_MOV &&
                             mov->op != QOP_FMOV &&
                             mov->op != QOP_MMOV) ||
                            mov->src[0].file != QFILE_VPM) {
                                continue;
                        }

                        uint32_t temps = 0;
                        for (int k = 0; k < qir_get_nsrc(inst); k++) {
                                if (inst->src[k].file == QFILE_TEMP)
                                        temps++;
                        }

                        /* The instruction is safe to reorder if its other
                         * sources are independent of previous instructions
                         */
                        if (temps == 1) {
                                inst->src[j] = mov->src[0];

                                list_del(&inst->link);
                                list_addtail(&inst->link, &mov->link);
                                qir_remove_instruction(c, mov);

                                progress = true;
                                break;
                        }
                }
        }

        return progress;
}