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 v3d_opt_small_immediates.c
26 *
27 * Turns references to small constant uniform values into small immediates
28 * fields.
29 */
30
31#include "v3d_compiler.h"
32
33static bool debug;
34
35bool
36vir_opt_small_immediates(struct v3d_compile *c)
37{
38        bool progress = false;
39
40        vir_for_each_inst_inorder(inst, c) {
41                if (inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU)
42                        continue;
43
44                /* The small immediate value sits in the raddr B field, so we
45                 * can't have 2 small immediates in one instruction (unless
46                 * they're the same value, but that should be optimized away
47                 * elsewhere).
48                 */
49                bool uses_small_imm = false;
50                for (int i = 0; i < vir_get_nsrc(inst); i++) {
51                        if (inst->src[i].file == QFILE_SMALL_IMM)
52                                uses_small_imm = true;
53                }
54                if (uses_small_imm)
55                        continue;
56
57                for (int i = 0; i < vir_get_nsrc(inst); i++) {
58                        if (inst->src[i].file != QFILE_TEMP)
59                                continue;
60
61                        /* See if it's a uniform load. */
62                        struct qinst *src_def = c->defs[inst->src[i].index];
63                        if (!src_def || !src_def->qpu.sig.ldunif)
64                                continue;
65                        int uniform = src_def->uniform;
66
67                        if (c->uniform_contents[uniform] != QUNIFORM_CONSTANT)
68                                continue;
69
70                        /* Check if the uniform is suitable as a small
71                         * immediate.
72                         */
73                        uint32_t imm = c->uniform_data[uniform];
74                        uint32_t packed;
75                        if (!v3d_qpu_small_imm_pack(c->devinfo, imm, &packed))
76                                continue;
77
78                        /* Check that we don't have any other signals already
79                         * that would be incompatible with small_imm.
80                         */
81                        struct v3d_qpu_sig new_sig = inst->qpu.sig;
82                        uint32_t sig_packed;
83                        new_sig.small_imm = true;
84                        if (!v3d_qpu_sig_pack(c->devinfo, &new_sig, &sig_packed))
85                                continue;
86
87                        if (debug) {
88                                fprintf(stderr, "opt_small_immediate() from: ");
89                                vir_dump_inst(c, inst);
90                                fprintf(stderr, "\n");
91                        }
92                        inst->qpu.sig.small_imm = true;
93                        inst->qpu.raddr_b = packed;
94
95                        inst->src[i].file = QFILE_SMALL_IMM;
96                        inst->src[i].index = imm;
97                        if (debug) {
98                                fprintf(stderr, "to: ");
99                                vir_dump_inst(c, inst);
100                                fprintf(stderr, "\n");
101                        }
102                        progress = true;
103                        break;
104                }
105        }
106
107        return progress;
108}
109