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
33 static bool debug;
34
35 bool
vir_opt_small_immediates(struct v3d_compile * c)36 vir_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). Since 7.x we can encode small immediates in
48 * any raddr field, but each instruction can still only use
49 * one.
50 */
51 bool uses_small_imm = false;
52 for (int i = 0; i < vir_get_nsrc(inst); i++) {
53 if (inst->src[i].file == QFILE_SMALL_IMM)
54 uses_small_imm = true;
55 }
56 if (uses_small_imm)
57 continue;
58
59 for (int i = 0; i < vir_get_nsrc(inst); i++) {
60 if (inst->src[i].file != QFILE_TEMP)
61 continue;
62
63 /* See if it's a uniform load. */
64 struct qinst *src_def = c->defs[inst->src[i].index];
65 if (!src_def || !src_def->qpu.sig.ldunif)
66 continue;
67 int uniform = src_def->uniform;
68
69 if (c->uniform_contents[uniform] != QUNIFORM_CONSTANT)
70 continue;
71
72 /* Check if the uniform is suitable as a small
73 * immediate.
74 */
75 uint32_t imm = c->uniform_data[uniform];
76 uint32_t packed;
77 if (!v3d_qpu_small_imm_pack(c->devinfo, imm, &packed))
78 continue;
79
80 /* Check that we don't have any other signals already
81 * that would be incompatible with small_imm.
82 */
83 struct v3d_qpu_sig new_sig = inst->qpu.sig;
84 uint32_t sig_packed;
85 if (c->devinfo->ver == 42) {
86 new_sig.small_imm_b = true;
87 } else {
88 if (vir_is_add(inst)) {
89 if (i == 0)
90 new_sig.small_imm_a = true;
91 else
92 new_sig.small_imm_b = true;
93 } else {
94 if (i == 0)
95 new_sig.small_imm_c = true;
96 else
97 new_sig.small_imm_d = true;
98 }
99 }
100
101 if (!v3d_qpu_sig_pack(c->devinfo, &new_sig, &sig_packed))
102 continue;
103
104 if (debug) {
105 fprintf(stderr, "opt_small_immediate() from: ");
106 vir_dump_inst(c, inst);
107 fprintf(stderr, "\n");
108 }
109 inst->qpu.sig.small_imm_a = new_sig.small_imm_a;
110 inst->qpu.sig.small_imm_b = new_sig.small_imm_b;
111 inst->qpu.sig.small_imm_c = new_sig.small_imm_c;
112 inst->qpu.sig.small_imm_d = new_sig.small_imm_d;
113 inst->qpu.raddr_b = packed;
114
115 inst->src[i].file = QFILE_SMALL_IMM;
116 inst->src[i].index = imm;
117 if (debug) {
118 fprintf(stderr, "to: ");
119 vir_dump_inst(c, inst);
120 fprintf(stderr, "\n");
121 }
122 progress = true;
123 break;
124 }
125 }
126
127 return progress;
128 }
129