2 * Copyright © 2012 Intel Corporation
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
27 /** @file brw_fs_cse.cpp
29 * Support for local common subexpression elimination.
31 * See Muchnick's Advanced Compiler Design and Implementation, section
36 struct aeb_entry
: public exec_node
{
37 /** The instruction that generates the expression value. */
40 /** The temporary where the value is stored. */
46 is_copy_payload(const fs_inst
*inst
)
48 const int reg
= inst
->src
[0].reg
;
49 if (inst
->src
[0].reg_offset
!= 0)
52 for (int i
= 1; i
< inst
->sources
; i
++) {
53 if (inst
->src
[i
].reg
!= reg
||
54 inst
->src
[i
].reg_offset
!= i
) {
62 is_expression(const fs_inst
*const inst
)
64 switch (inst
->opcode
) {
86 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
87 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
88 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
89 case FS_OPCODE_CINTERP
:
90 case FS_OPCODE_LINTERP
:
92 case SHADER_OPCODE_RCP
:
93 case SHADER_OPCODE_RSQ
:
94 case SHADER_OPCODE_SQRT
:
95 case SHADER_OPCODE_EXP2
:
96 case SHADER_OPCODE_LOG2
:
97 case SHADER_OPCODE_POW
:
98 case SHADER_OPCODE_INT_QUOTIENT
:
99 case SHADER_OPCODE_INT_REMAINDER
:
100 case SHADER_OPCODE_SIN
:
101 case SHADER_OPCODE_COS
:
102 return inst
->mlen
== 0;
103 case SHADER_OPCODE_LOAD_PAYLOAD
:
104 return !is_copy_payload(inst
);
106 return inst
->is_send_from_grf() && !inst
->has_side_effects();
111 is_expression_commutative(enum opcode op
)
126 operands_match(fs_inst
*a
, fs_inst
*b
)
131 if (!is_expression_commutative(a
->opcode
)) {
133 for (int i
= 0; i
< a
->sources
; i
++) {
134 if (!xs
[i
].equals(ys
[i
])) {
141 return (xs
[0].equals(ys
[0]) && xs
[1].equals(ys
[1])) ||
142 (xs
[1].equals(ys
[0]) && xs
[0].equals(ys
[1]));
147 instructions_match(fs_inst
*a
, fs_inst
*b
)
149 return a
->opcode
== b
->opcode
&&
150 a
->saturate
== b
->saturate
&&
151 a
->predicate
== b
->predicate
&&
152 a
->predicate_inverse
== b
->predicate_inverse
&&
153 a
->conditional_mod
== b
->conditional_mod
&&
154 a
->dst
.type
== b
->dst
.type
&&
155 a
->sources
== b
->sources
&&
156 (a
->is_tex() ? (a
->texture_offset
== b
->texture_offset
&&
157 a
->mlen
== b
->mlen
&&
158 a
->regs_written
== b
->regs_written
&&
159 a
->base_mrf
== b
->base_mrf
&&
161 a
->header_present
== b
->header_present
&&
162 a
->shadow_compare
== b
->shadow_compare
)
164 operands_match(a
, b
);
168 fs_visitor::opt_cse_local(bblock_t
*block
)
170 bool progress
= false;
173 void *cse_ctx
= ralloc_context(NULL
);
175 int ip
= block
->start_ip
;
176 foreach_inst_in_block(fs_inst
, inst
, block
) {
177 /* Skip some cases. */
178 if (is_expression(inst
) && !inst
->is_partial_write() &&
179 (inst
->dst
.file
!= HW_REG
|| inst
->dst
.is_null()))
183 foreach_in_list_use_after(aeb_entry
, entry
, &aeb
) {
184 /* Match current instruction's expression against those in AEB. */
185 if (instructions_match(inst
, entry
->generator
)) {
193 /* Our first sighting of this expression. Create an entry. */
194 aeb_entry
*entry
= ralloc(cse_ctx
, aeb_entry
);
195 entry
->tmp
= reg_undef
;
196 entry
->generator
= inst
;
197 aeb
.push_tail(entry
);
199 /* This is at least our second sighting of this expression.
200 * If we don't have a temporary already, make one.
202 bool no_existing_temp
= entry
->tmp
.file
== BAD_FILE
;
203 if (no_existing_temp
&& !entry
->generator
->dst
.is_null()) {
204 int written
= entry
->generator
->regs_written
;
206 fs_reg orig_dst
= entry
->generator
->dst
;
207 fs_reg tmp
= fs_reg(GRF
, virtual_grf_alloc(written
),
210 entry
->generator
->dst
= tmp
;
214 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, written
);
215 for (int i
= 0; i
< written
; i
++) {
217 sources
[i
].reg_offset
= i
;
219 copy
= LOAD_PAYLOAD(orig_dst
, sources
, written
);
221 copy
= MOV(orig_dst
, tmp
);
222 copy
->force_writemask_all
=
223 entry
->generator
->force_writemask_all
;
225 entry
->generator
->insert_after(copy
);
229 if (!inst
->dst
.is_null()) {
230 int written
= inst
->regs_written
;
231 assert(written
== entry
->generator
->regs_written
);
232 assert(inst
->dst
.type
== entry
->tmp
.type
);
233 fs_reg dst
= inst
->dst
;
234 fs_reg tmp
= entry
->tmp
;
237 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, written
);
238 for (int i
= 0; i
< written
; i
++) {
240 sources
[i
].reg_offset
= i
;
242 copy
= LOAD_PAYLOAD(dst
, sources
, written
);
244 copy
= MOV(dst
, tmp
);
245 copy
->force_writemask_all
= inst
->force_writemask_all
;
247 inst
->insert_before(copy
);
250 /* Set our iterator so that next time through the loop inst->next
251 * will get the instruction in the basic block after the one we've
254 fs_inst
*prev
= (fs_inst
*)inst
->prev
;
258 /* Appending an instruction may have changed our bblock end. */
259 if (inst
== block
->end
) {
267 foreach_in_list_safe(aeb_entry
, entry
, &aeb
) {
268 /* Kill all AEB entries that write a different value to or read from
269 * the flag register if we just wrote it.
271 if (inst
->writes_flag()) {
272 if (entry
->generator
->reads_flag() ||
273 (entry
->generator
->writes_flag() &&
274 !instructions_match(inst
, entry
->generator
))) {
281 for (int i
= 0; i
< entry
->generator
->sources
; i
++) {
282 fs_reg
*src_reg
= &entry
->generator
->src
[i
];
284 /* Kill all AEB entries that use the destination we just
287 if (inst
->overwrites_reg(entry
->generator
->src
[i
])) {
293 /* Kill any AEB entries using registers that don't get reused any
294 * more -- a sure sign they'll fail operands_match().
296 if (src_reg
->file
== GRF
&& virtual_grf_end
[src_reg
->reg
] < ip
) {
307 ralloc_free(cse_ctx
);
313 fs_visitor::opt_cse()
315 bool progress
= false;
317 calculate_live_intervals();
319 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
320 bblock_t
*block
= cfg
->blocks
[b
];
322 progress
= opt_cse_local(block
) || progress
;
326 invalidate_live_intervals();