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 struct qreg src
= vir_follow_movs(c
, inst
->src
[i
]);
60 if (src
.file
!= QFILE_UNIF
||
61 c
->uniform_contents
[src
.index
] !=
66 if (vir_has_implicit_uniform(inst
) &&
67 i
== vir_get_implicit_uniform_src(inst
)) {
68 /* No turning the implicit uniform read into
74 /* Check if the uniform is suitable as a small
77 uint32_t imm
= c
->uniform_data
[src
.index
];
79 if (!v3d_qpu_small_imm_pack(c
->devinfo
, imm
, &packed
))
82 /* Check that we don't have any other signals already
83 * that would be incompatible with small_imm.
85 struct v3d_qpu_sig new_sig
= inst
->qpu
.sig
;
87 new_sig
.small_imm
= true;
88 if (!v3d_qpu_sig_pack(c
->devinfo
, &new_sig
, &sig_packed
))
92 fprintf(stderr
, "opt_small_immediate() from: ");
93 vir_dump_inst(c
, inst
);
94 fprintf(stderr
, "\n");
96 inst
->qpu
.sig
.small_imm
= true;
97 inst
->qpu
.raddr_b
= packed
;
99 inst
->src
[i
].file
= QFILE_SMALL_IMM
;
100 inst
->src
[i
].index
= imm
;
102 fprintf(stderr
, "to: ");
103 vir_dump_inst(c
, inst
);
104 fprintf(stderr
, "\n");