29e3ee3120167ddf2a9e5a5c33222be56e7f9f70
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 vc4_qir_lower_uniforms.c
27 * This is the pre-code-generation pass for fixing up instructions that try to
28 * read from multiple uniform values.
32 #include "util/hash_table.h"
33 #include "util/u_math.h"
35 static inline uint32_t
36 index_hash(const void *key
)
38 return (uintptr_t)key
;
42 index_compare(const void *a
, const void *b
)
48 add_uniform(struct hash_table
*ht
, struct qreg reg
)
50 struct hash_entry
*entry
;
51 void *key
= (void *)(uintptr_t)(reg
.index
+ 1);
53 entry
= _mesa_hash_table_search(ht
, key
);
57 _mesa_hash_table_insert(ht
, key
, (void *)(uintptr_t)1);
62 remove_uniform(struct hash_table
*ht
, struct qreg reg
)
64 struct hash_entry
*entry
;
65 void *key
= (void *)(uintptr_t)(reg
.index
+ 1);
67 entry
= _mesa_hash_table_search(ht
, key
);
70 if (entry
->data
== NULL
)
71 _mesa_hash_table_remove(ht
, entry
);
75 is_lowerable_uniform(struct qinst
*inst
, int i
)
77 if (inst
->src
[i
].file
!= QFILE_UNIF
)
85 qir_lower_uniforms(struct vc4_compile
*c
)
87 struct hash_table
*ht
=
88 _mesa_hash_table_create(c
, index_hash
, index_compare
);
90 /* Walk the instruction list, finding which instructions have more
91 * than one uniform referenced, and add those uniform values to the
94 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
95 uint32_t nsrc
= qir_get_op_nsrc(inst
->op
);
98 for (int i
= 0; i
< nsrc
; i
++) {
99 if (inst
->src
[i
].file
== QFILE_UNIF
)
106 for (int i
= 0; i
< nsrc
; i
++) {
107 if (is_lowerable_uniform(inst
, i
))
108 add_uniform(ht
, inst
->src
[i
]);
112 while (ht
->entries
) {
113 /* Find the most commonly used uniform in instructions that
114 * need a uniform lowered.
116 uint32_t max_count
= 0;
117 uint32_t max_index
= 0;
118 struct hash_entry
*entry
;
119 hash_table_foreach(ht
, entry
) {
120 uint32_t count
= (uintptr_t)entry
->data
;
121 uint32_t index
= (uintptr_t)entry
->key
- 1;
122 if (count
> max_count
) {
128 /* Now, find the instructions using this uniform and make them
129 * reference a temp instead.
131 struct qreg temp
= qir_get_temp(c
);
132 struct qreg unif
= { QFILE_UNIF
, max_index
};
133 struct qinst
*mov
= qir_inst(QOP_MOV
, temp
, unif
, c
->undef
);
134 list_add(&mov
->link
, &c
->instructions
);
135 c
->defs
[temp
.index
] = mov
;
136 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
137 uint32_t nsrc
= qir_get_op_nsrc(inst
->op
);
140 for (int i
= 0; i
< nsrc
; i
++) {
141 if (inst
->src
[i
].file
== QFILE_UNIF
)
148 for (int i
= 0; i
< nsrc
; i
++) {
149 if (is_lowerable_uniform(inst
, i
) &&
150 inst
->src
[i
].index
== max_index
) {
152 remove_uniform(ht
, unif
);
157 /* If the instruction doesn't need lowering any more,
158 * then drop it from the list.
161 for (int i
= 0; i
< nsrc
; i
++) {
162 if (is_lowerable_uniform(inst
, i
))
163 remove_uniform(ht
, inst
->src
[i
]);
169 _mesa_hash_table_destroy(ht
, NULL
);