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
38 struct aeb_entry
: public exec_node
{
39 /** The instruction that generates the expression value. */
42 /** The temporary where the value is stored. */
48 is_expression(const fs_visitor
*v
, const fs_inst
*const inst
)
50 switch (inst
->opcode
) {
64 case SHADER_OPCODE_MULH
:
74 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
75 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
76 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
77 case FS_OPCODE_CINTERP
:
78 case FS_OPCODE_LINTERP
:
79 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
80 case SHADER_OPCODE_BROADCAST
:
82 case SHADER_OPCODE_RCP
:
83 case SHADER_OPCODE_RSQ
:
84 case SHADER_OPCODE_SQRT
:
85 case SHADER_OPCODE_EXP2
:
86 case SHADER_OPCODE_LOG2
:
87 case SHADER_OPCODE_POW
:
88 case SHADER_OPCODE_INT_QUOTIENT
:
89 case SHADER_OPCODE_INT_REMAINDER
:
90 case SHADER_OPCODE_SIN
:
91 case SHADER_OPCODE_COS
:
92 return inst
->mlen
< 2;
93 case SHADER_OPCODE_LOAD_PAYLOAD
:
94 return !inst
->is_copy_payload(v
->alloc
);
96 return inst
->is_send_from_grf() && !inst
->has_side_effects() &&
102 operands_match(const fs_inst
*a
, const fs_inst
*b
, bool *negate
)
107 if (a
->opcode
== BRW_OPCODE_MAD
) {
108 return xs
[0].equals(ys
[0]) &&
109 ((xs
[1].equals(ys
[1]) && xs
[2].equals(ys
[2])) ||
110 (xs
[2].equals(ys
[1]) && xs
[1].equals(ys
[2])));
111 } else if (a
->opcode
== BRW_OPCODE_MUL
&& a
->dst
.type
== BRW_REGISTER_TYPE_F
) {
112 bool xs0_negate
= xs
[0].negate
;
113 bool xs1_negate
= xs
[1].file
== IMM
? xs
[1].f
< 0.0f
115 bool ys0_negate
= ys
[0].negate
;
116 bool ys1_negate
= ys
[1].file
== IMM
? ys
[1].f
< 0.0f
118 float xs1_imm
= xs
[1].f
;
119 float ys1_imm
= ys
[1].f
;
121 xs
[0].negate
= false;
122 xs
[1].negate
= false;
123 ys
[0].negate
= false;
124 ys
[1].negate
= false;
125 xs
[1].f
= fabsf(xs
[1].f
);
126 ys
[1].f
= fabsf(ys
[1].f
);
128 bool ret
= (xs
[0].equals(ys
[0]) && xs
[1].equals(ys
[1])) ||
129 (xs
[1].equals(ys
[0]) && xs
[0].equals(ys
[1]));
131 xs
[0].negate
= xs0_negate
;
132 xs
[1].negate
= xs
[1].file
== IMM
? false : xs1_negate
;
133 ys
[0].negate
= ys0_negate
;
134 ys
[1].negate
= ys
[1].file
== IMM
? false : ys1_negate
;
138 *negate
= (xs0_negate
!= xs1_negate
) != (ys0_negate
!= ys1_negate
);
140 } else if (!a
->is_commutative()) {
142 for (int i
= 0; i
< a
->sources
; i
++) {
143 if (!xs
[i
].equals(ys
[i
])) {
150 return (xs
[0].equals(ys
[0]) && xs
[1].equals(ys
[1])) ||
151 (xs
[1].equals(ys
[0]) && xs
[0].equals(ys
[1]));
156 instructions_match(fs_inst
*a
, fs_inst
*b
, bool *negate
)
158 return a
->opcode
== b
->opcode
&&
159 a
->force_writemask_all
== b
->force_writemask_all
&&
160 a
->exec_size
== b
->exec_size
&&
161 a
->force_sechalf
== b
->force_sechalf
&&
162 a
->saturate
== b
->saturate
&&
163 a
->predicate
== b
->predicate
&&
164 a
->predicate_inverse
== b
->predicate_inverse
&&
165 a
->conditional_mod
== b
->conditional_mod
&&
166 a
->flag_subreg
== b
->flag_subreg
&&
167 a
->dst
.type
== b
->dst
.type
&&
168 a
->offset
== b
->offset
&&
169 a
->mlen
== b
->mlen
&&
170 a
->regs_written
== b
->regs_written
&&
171 a
->base_mrf
== b
->base_mrf
&&
173 a
->header_size
== b
->header_size
&&
174 a
->shadow_compare
== b
->shadow_compare
&&
175 a
->pi_noperspective
== b
->pi_noperspective
&&
176 a
->sources
== b
->sources
&&
177 operands_match(a
, b
, negate
);
181 create_copy_instr(const fs_builder
&bld
, fs_inst
*inst
, fs_reg src
, bool negate
)
183 int written
= inst
->regs_written
;
184 int dst_width
= inst
->exec_size
/ 8;
187 if (written
> dst_width
) {
189 int sources
, header_size
;
190 if (inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
) {
191 sources
= inst
->sources
;
192 header_size
= inst
->header_size
;
194 assert(written
% dst_width
== 0);
195 sources
= written
/ dst_width
;
199 assert(src
.file
== GRF
);
200 payload
= ralloc_array(bld
.shader
->mem_ctx
, fs_reg
, sources
);
201 for (int i
= 0; i
< header_size
; i
++) {
205 for (int i
= header_size
; i
< sources
; i
++) {
207 src
= offset(src
, bld
, 1);
209 copy
= bld
.LOAD_PAYLOAD(inst
->dst
, payload
, sources
, header_size
);
211 copy
= bld
.MOV(inst
->dst
, src
);
212 copy
->src
[0].negate
= negate
;
214 assert(copy
->regs_written
== written
);
218 fs_visitor::opt_cse_local(bblock_t
*block
)
220 bool progress
= false;
223 void *cse_ctx
= ralloc_context(NULL
);
225 int ip
= block
->start_ip
;
226 foreach_inst_in_block(fs_inst
, inst
, block
) {
227 /* Skip some cases. */
228 if (is_expression(this, inst
) && !inst
->is_partial_write() &&
229 (inst
->dst
.file
!= HW_REG
|| inst
->dst
.is_null()))
234 foreach_in_list_use_after(aeb_entry
, entry
, &aeb
) {
235 /* Match current instruction's expression against those in AEB. */
236 if (!(entry
->generator
->dst
.is_null() && !inst
->dst
.is_null()) &&
237 instructions_match(inst
, entry
->generator
, &negate
)) {
245 if (inst
->opcode
!= BRW_OPCODE_MOV
||
246 (inst
->opcode
== BRW_OPCODE_MOV
&&
247 inst
->src
[0].file
== IMM
&&
248 inst
->src
[0].type
== BRW_REGISTER_TYPE_VF
)) {
249 /* Our first sighting of this expression. Create an entry. */
250 aeb_entry
*entry
= ralloc(cse_ctx
, aeb_entry
);
251 entry
->tmp
= reg_undef
;
252 entry
->generator
= inst
;
253 aeb
.push_tail(entry
);
256 /* This is at least our second sighting of this expression.
257 * If we don't have a temporary already, make one.
259 bool no_existing_temp
= entry
->tmp
.file
== BAD_FILE
;
260 if (no_existing_temp
&& !entry
->generator
->dst
.is_null()) {
261 const fs_builder ibld
= fs_builder(this, block
, entry
->generator
)
262 .at(block
, entry
->generator
->next
);
263 int written
= entry
->generator
->regs_written
;
265 entry
->tmp
= fs_reg(GRF
, alloc
.allocate(written
),
266 entry
->generator
->dst
.type
);
268 create_copy_instr(ibld
, entry
->generator
, entry
->tmp
, false);
270 entry
->generator
->dst
= entry
->tmp
;
274 if (!inst
->dst
.is_null()) {
275 assert(inst
->regs_written
== entry
->generator
->regs_written
);
276 assert(inst
->dst
.type
== entry
->tmp
.type
);
277 const fs_builder
ibld(this, block
, inst
);
279 create_copy_instr(ibld
, inst
, entry
->tmp
, negate
);
282 /* Set our iterator so that next time through the loop inst->next
283 * will get the instruction in the basic block after the one we've
286 fs_inst
*prev
= (fs_inst
*)inst
->prev
;
293 foreach_in_list_safe(aeb_entry
, entry
, &aeb
) {
294 /* Kill all AEB entries that write a different value to or read from
295 * the flag register if we just wrote it.
297 if (inst
->writes_flag()) {
298 bool negate
; /* dummy */
299 if (entry
->generator
->reads_flag() ||
300 (entry
->generator
->writes_flag() &&
301 !instructions_match(inst
, entry
->generator
, &negate
))) {
308 for (int i
= 0; i
< entry
->generator
->sources
; i
++) {
309 fs_reg
*src_reg
= &entry
->generator
->src
[i
];
311 /* Kill all AEB entries that use the destination we just
314 if (inst
->overwrites_reg(entry
->generator
->src
[i
])) {
320 /* Kill any AEB entries using registers that don't get reused any
321 * more -- a sure sign they'll fail operands_match().
323 if (src_reg
->file
== GRF
&& virtual_grf_end
[src_reg
->nr
] < ip
) {
334 ralloc_free(cse_ctx
);
340 fs_visitor::opt_cse()
342 bool progress
= false;
344 calculate_live_intervals();
346 foreach_block (block
, cfg
) {
347 progress
= opt_cse_local(block
) || progress
;
351 invalidate_live_intervals();