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 static inline bool
skip_inst(struct qinst * inst)36 skip_inst(struct qinst *inst)
37 {
38 return inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU;
39 }
40
41 bool
vir_opt_small_immediates(struct v3d_compile * c)42 vir_opt_small_immediates(struct v3d_compile *c)
43 {
44 bool progress = false;
45
46 /* Shader-db shows that small immediates generally lead to higher
47 * instruction counts for geometry stages.
48 */
49 if (c->s->info.stage != MESA_SHADER_FRAGMENT &&
50 c->s->info.stage != MESA_SHADER_COMPUTE) {
51 return progress;
52 }
53
54 vir_for_each_inst_inorder(inst, c) {
55 if (skip_inst(inst))
56 continue;
57
58 /* The small immediate value sits in the raddr B field, so we
59 * can't have 2 small immediates in one instruction (unless
60 * they're the same value, but that should be optimized away
61 * elsewhere). Since 7.x we can encode small immediates in
62 * any raddr field, but each instruction can still only use
63 * one.
64 */
65 bool uses_small_imm = false;
66 for (int i = 0; i < vir_get_nsrc(inst); i++) {
67 if (inst->src[i].file == QFILE_SMALL_IMM)
68 uses_small_imm = true;
69 }
70 if (uses_small_imm)
71 continue;
72
73 for (int i = 0; i < vir_get_nsrc(inst); i++) {
74 if (inst->src[i].file != QFILE_TEMP)
75 continue;
76
77 /* See if it's a uniform load. */
78 struct qinst *src_def = c->defs[inst->src[i].index];
79 if (!src_def || !src_def->qpu.sig.ldunif)
80 continue;
81 int uniform = src_def->uniform;
82
83 if (c->uniform_contents[uniform] != QUNIFORM_CONSTANT)
84 continue;
85
86 /* Check if the uniform is suitable as a small
87 * immediate.
88 */
89 uint32_t imm = c->uniform_data[uniform];
90 uint32_t packed;
91 if (!v3d_qpu_small_imm_pack(c->devinfo, imm, &packed))
92 continue;
93
94 /* Check that we don't have any other signals already
95 * that would be incompatible with small_imm.
96 */
97 struct v3d_qpu_sig new_sig = inst->qpu.sig;
98 uint32_t sig_packed;
99 if (c->devinfo->ver == 42) {
100 new_sig.small_imm_b = true;
101 } else {
102 if (vir_is_add(inst)) {
103 if (i == 0)
104 new_sig.small_imm_a = true;
105 else
106 new_sig.small_imm_b = true;
107 } else {
108 if (i == 0)
109 new_sig.small_imm_c = true;
110 else
111 new_sig.small_imm_d = true;
112 }
113 }
114
115 if (!v3d_qpu_sig_pack(c->devinfo, &new_sig, &sig_packed))
116 continue;
117
118 if (debug) {
119 fprintf(stderr, "opt_small_immediate() from: ");
120 vir_dump_inst(c, inst);
121 fprintf(stderr, "\n");
122 }
123 inst->qpu.sig.small_imm_a = new_sig.small_imm_a;
124 inst->qpu.sig.small_imm_b = new_sig.small_imm_b;
125 inst->qpu.sig.small_imm_c = new_sig.small_imm_c;
126 inst->qpu.sig.small_imm_d = new_sig.small_imm_d;
127 inst->qpu.raddr_b = packed;
128
129 inst->src[i].file = QFILE_SMALL_IMM;
130 inst->src[i].index = imm;
131 if (debug) {
132 fprintf(stderr, "to: ");
133 vir_dump_inst(c, inst);
134 fprintf(stderr, "\n");
135 }
136 progress = true;
137 break;
138 }
139 }
140
141 return progress;
142 }
143