2 * Copyright © 2014 Broadcom
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:
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
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
25 * @file v3d_opt_small_immediates.c
27 * Turns references to small constant uniform values into small immediates
31 #include "v3d_compiler.h"
36 vir_opt_small_immediates(struct v3d_compile
*c
)
38 bool progress
= false;
40 vir_for_each_inst_inorder(inst
, c
) {
41 if (inst
->qpu
.type
!= V3D_QPU_INSTR_TYPE_ALU
)
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
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;
57 for (int i
= 0; i
< vir_get_nsrc(inst
); i
++) {
58 if (inst
->src
[i
].file
!= QFILE_TEMP
)
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
)
65 int uniform
= src_def
->uniform
;
67 if (c
->uniform_contents
[uniform
] != QUNIFORM_CONSTANT
)
70 /* Check if the uniform is suitable as a small
73 uint32_t imm
= c
->uniform_data
[uniform
];
75 if (!v3d_qpu_small_imm_pack(c
->devinfo
, imm
, &packed
))
78 /* Check that we don't have any other signals already
79 * that would be incompatible with small_imm.
81 struct v3d_qpu_sig new_sig
= inst
->qpu
.sig
;
83 new_sig
.small_imm
= true;
84 if (!v3d_qpu_sig_pack(c
->devinfo
, &new_sig
, &sig_packed
))
88 fprintf(stderr
, "opt_small_immediate() from: ");
89 vir_dump_inst(c
, inst
);
90 fprintf(stderr
, "\n");
92 inst
->qpu
.sig
.small_imm
= true;
93 inst
->qpu
.raddr_b
= packed
;
95 inst
->src
[i
].file
= QFILE_SMALL_IMM
;
96 inst
->src
[i
].index
= imm
;
98 fprintf(stderr
, "to: ");
99 vir_dump_inst(c
, inst
);
100 fprintf(stderr
, "\n");