2 * Copyright © 2014 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
28 /** @file brw_fs_cmod_propagation.cpp
30 * Implements a pass that propagates the conditional modifier from a CMP x 0.0
31 * instruction into the instruction that generated x. For instance, in this
34 * add(8) g70<1>F g69<8,8,1>F 4096F
35 * cmp.ge.f0(8) null g70<8,8,1>F 0F
37 * we can do the comparison as part of the ADD instruction directly:
39 * add.ge.f0(8) g70<1>F g69<8,8,1>F 4096F
41 * If there had been a use of the flag register and another CMP using g70
43 * add.ge.f0(8) g70<1>F g69<8,8,1>F 4096F
44 * (+f0) sel(8) g71<F> g72<8,8,1>F g73<8,8,1>F
45 * cmp.ge.f0(8) null g70<8,8,1>F 0F
47 * we can recognize that the CMP is generating the flag value that already
48 * exists and therefore remove the instruction.
54 cmod_propagate_cmp_to_add(const gen_device_info
*devinfo
, bblock_t
*block
,
57 bool read_flag
= false;
58 const unsigned flags_written
= inst
->flags_written();
60 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
61 if (scan_inst
->opcode
== BRW_OPCODE_ADD
&&
62 !scan_inst
->is_partial_write() &&
63 scan_inst
->exec_size
== inst
->exec_size
) {
66 /* A CMP is basically a subtraction. The result of the
67 * subtraction must be the same as the result of the addition.
68 * This means that one of the operands must be negated. So (a +
69 * b) vs (a == -b) or (a + -b) vs (a == b).
71 if ((inst
->src
[0].equals(scan_inst
->src
[0]) &&
72 inst
->src
[1].negative_equals(scan_inst
->src
[1])) ||
73 (inst
->src
[0].equals(scan_inst
->src
[1]) &&
74 inst
->src
[1].negative_equals(scan_inst
->src
[0]))) {
76 } else if ((inst
->src
[0].negative_equals(scan_inst
->src
[0]) &&
77 inst
->src
[1].equals(scan_inst
->src
[1])) ||
78 (inst
->src
[0].negative_equals(scan_inst
->src
[1]) &&
79 inst
->src
[1].equals(scan_inst
->src
[0]))) {
85 /* If the scan instruction writes a different flag register than the
86 * instruction we're trying to propagate from, bail.
88 * FINISHME: The second part of the condition may be too strong.
89 * Perhaps (scan_inst->flags_written() & flags_written) !=
92 if (scan_inst
->flags_written() != 0 &&
93 scan_inst
->flags_written() != flags_written
)
96 /* From the Sky Lake PRM Vol. 7 "Assigning Conditional Mods":
98 * * Note that the [post condition signal] bits generated at
99 * the output of a compute are before the .sat.
101 * So we don't have to bail if scan_inst has saturate.
103 /* Otherwise, try propagating the conditional. */
104 const enum brw_conditional_mod cond
=
105 negate
? brw_swap_cmod(inst
->conditional_mod
)
106 : inst
->conditional_mod
;
108 if (scan_inst
->can_do_cmod() &&
109 ((!read_flag
&& scan_inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) ||
110 scan_inst
->conditional_mod
== cond
)) {
111 scan_inst
->conditional_mod
= cond
;
119 if ((scan_inst
->flags_written() & flags_written
) != 0)
122 read_flag
= read_flag
||
123 (scan_inst
->flags_read(devinfo
) & flags_written
) != 0;
130 * Propagate conditional modifiers from NOT instructions
132 * Attempt to convert sequences like
134 * or(8) g78<8,8,1> g76<8,8,1>UD g77<8,8,1>UD
136 * not.nz.f0(8) null g78<8,8,1>UD
140 * or.z.f0(8) g78<8,8,1> g76<8,8,1>UD g77<8,8,1>UD
143 cmod_propagate_not(const gen_device_info
*devinfo
, bblock_t
*block
,
146 const enum brw_conditional_mod cond
= brw_negate_cmod(inst
->conditional_mod
);
147 bool read_flag
= false;
148 const unsigned flags_written
= inst
->flags_written();
150 if (cond
!= BRW_CONDITIONAL_Z
&& cond
!= BRW_CONDITIONAL_NZ
)
153 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
154 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
155 inst
->src
[0], inst
->size_read(0))) {
156 if (scan_inst
->opcode
!= BRW_OPCODE_OR
&&
157 scan_inst
->opcode
!= BRW_OPCODE_AND
)
160 if (scan_inst
->is_partial_write() ||
161 scan_inst
->dst
.offset
!= inst
->src
[0].offset
||
162 scan_inst
->exec_size
!= inst
->exec_size
)
165 /* If the scan instruction writes a different flag register than the
166 * instruction we're trying to propagate from, bail.
168 * FINISHME: The second part of the condition may be too strong.
169 * Perhaps (scan_inst->flags_written() & flags_written) !=
172 if (scan_inst
->flags_written() != 0 &&
173 scan_inst
->flags_written() != flags_written
)
176 if (scan_inst
->can_do_cmod() &&
177 ((!read_flag
&& scan_inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) ||
178 scan_inst
->conditional_mod
== cond
)) {
179 scan_inst
->conditional_mod
= cond
;
186 if ((scan_inst
->flags_written() & flags_written
) != 0)
189 read_flag
= read_flag
||
190 (scan_inst
->flags_read(devinfo
) & flags_written
) != 0;
197 opt_cmod_propagation_local(const gen_device_info
*devinfo
, bblock_t
*block
)
199 bool progress
= false;
200 int ip
= block
->end_ip
+ 1;
202 foreach_inst_in_block_reverse_safe(fs_inst
, inst
, block
) {
205 if ((inst
->opcode
!= BRW_OPCODE_AND
&&
206 inst
->opcode
!= BRW_OPCODE_CMP
&&
207 inst
->opcode
!= BRW_OPCODE_MOV
&&
208 inst
->opcode
!= BRW_OPCODE_NOT
) ||
209 inst
->predicate
!= BRW_PREDICATE_NONE
||
210 !inst
->dst
.is_null() ||
211 (inst
->src
[0].file
!= VGRF
&& inst
->src
[0].file
!= ATTR
&&
212 inst
->src
[0].file
!= UNIFORM
))
215 /* An ABS source modifier can only be handled when processing a compare
216 * with a value other than zero.
218 if (inst
->src
[0].abs
&&
219 (inst
->opcode
!= BRW_OPCODE_CMP
|| inst
->src
[1].is_zero()))
222 /* Only an AND.NZ can be propagated. Many AND.Z instructions are
223 * generated (for ir_unop_not in fs_visitor::emit_bool_to_cond_code).
224 * Propagating those would require inverting the condition on the CMP.
225 * This changes both the flag value and the register destination of the
226 * CMP. That result may be used elsewhere, so we can't change its value
229 if (inst
->opcode
== BRW_OPCODE_AND
&&
230 !(inst
->src
[1].is_one() &&
231 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
&&
232 !inst
->src
[0].negate
))
235 if (inst
->opcode
== BRW_OPCODE_MOV
&&
236 inst
->conditional_mod
!= BRW_CONDITIONAL_NZ
)
239 /* A CMP with a second source of zero can match with anything. A CMP
240 * with a second source that is not zero can only match with an ADD
243 * Only apply this optimization to float-point sources. It can fail for
244 * integers. For inputs a = 0x80000000, b = 4, int(0x80000000) < 4, but
245 * int(0x80000000) - 4 overflows and results in 0x7ffffffc. that's not
246 * less than zero, so the flags get set differently than for (a < b).
248 if (inst
->opcode
== BRW_OPCODE_CMP
&& !inst
->src
[1].is_zero()) {
249 if (brw_reg_type_is_floating_point(inst
->src
[0].type
) &&
250 cmod_propagate_cmp_to_add(devinfo
, block
, inst
))
256 if (inst
->opcode
== BRW_OPCODE_NOT
) {
257 progress
= cmod_propagate_not(devinfo
, block
, inst
) || progress
;
261 bool read_flag
= false;
262 const unsigned flags_written
= inst
->flags_written();
263 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
264 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
265 inst
->src
[0], inst
->size_read(0))) {
266 /* If the scan instruction writes a different flag register than
267 * the instruction we're trying to propagate from, bail.
269 * FINISHME: The second part of the condition may be too strong.
270 * Perhaps (scan_inst->flags_written() & flags_written) !=
273 if (scan_inst
->flags_written() != 0 &&
274 scan_inst
->flags_written() != flags_written
)
277 if (scan_inst
->is_partial_write() ||
278 scan_inst
->dst
.offset
!= inst
->src
[0].offset
||
279 scan_inst
->exec_size
!= inst
->exec_size
)
282 /* CMP's result is the same regardless of dest type. */
283 if (inst
->conditional_mod
== BRW_CONDITIONAL_NZ
&&
284 scan_inst
->opcode
== BRW_OPCODE_CMP
&&
285 brw_reg_type_is_integer(inst
->dst
.type
)) {
291 /* If the AND wasn't handled by the previous case, it isn't safe
294 if (inst
->opcode
== BRW_OPCODE_AND
)
297 /* Not safe to use inequality operators if the types are different
299 if (scan_inst
->dst
.type
!= inst
->src
[0].type
&&
300 inst
->conditional_mod
!= BRW_CONDITIONAL_Z
&&
301 inst
->conditional_mod
!= BRW_CONDITIONAL_NZ
)
304 /* Comparisons operate differently for ints and floats */
305 if (scan_inst
->dst
.type
!= inst
->dst
.type
) {
306 /* Comparison result may be altered if the bit-size changes
307 * since that affects range, denorms, etc
309 if (type_sz(scan_inst
->dst
.type
) != type_sz(inst
->dst
.type
))
312 /* We should propagate from a MOV to another instruction in a
315 * and(16) g31<1>UD g20<8,8,1>UD g22<8,8,1>UD
316 * mov.nz.f0(16) null<1>F g31<8,8,1>D
318 if (inst
->opcode
== BRW_OPCODE_MOV
) {
319 if ((inst
->src
[0].type
!= BRW_REGISTER_TYPE_D
&&
320 inst
->src
[0].type
!= BRW_REGISTER_TYPE_UD
) ||
321 (scan_inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
322 scan_inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
)) {
325 } else if (brw_reg_type_is_floating_point(scan_inst
->dst
.type
) !=
326 brw_reg_type_is_floating_point(inst
->dst
.type
)) {
331 /* Knowing following:
332 * - CMP writes to flag register the result of
333 * applying cmod to the `src0 - src1`.
334 * After that it stores the same value to dst.
335 * Other instructions first store their result to
336 * dst, and then store cmod(dst) to the flag
338 * - inst is either CMP or MOV
339 * - inst->dst is null
340 * - inst->src[0] overlaps with scan_inst->dst
341 * - inst->src[1] is zero
342 * - scan_inst wrote to a flag register
344 * There can be three possible paths:
346 * - scan_inst is CMP:
348 * Considering that src0 is either 0x0 (false),
349 * or 0xffffffff (true), and src1 is 0x0:
351 * - If inst's cmod is NZ, we can always remove
352 * scan_inst: NZ is invariant for false and true. This
353 * holds even if src0 is NaN: .nz is the only cmod,
354 * that returns true for NaN.
356 * - .g is invariant if src0 has a UD type
358 * - .l is invariant if src0 has a D type
360 * - scan_inst and inst have the same cmod:
362 * If scan_inst is anything than CMP, it already
363 * wrote the appropriate value to the flag register.
367 * We can change cmod of scan_inst to that of inst,
368 * and remove inst. It is valid as long as we make
369 * sure that no instruction uses the flag register
370 * between scan_inst and inst.
372 if (!inst
->src
[0].negate
&&
373 scan_inst
->flags_written()) {
374 if (scan_inst
->opcode
== BRW_OPCODE_CMP
) {
375 if ((inst
->conditional_mod
== BRW_CONDITIONAL_NZ
) ||
376 (inst
->conditional_mod
== BRW_CONDITIONAL_G
&&
377 inst
->src
[0].type
== BRW_REGISTER_TYPE_UD
) ||
378 (inst
->conditional_mod
== BRW_CONDITIONAL_L
&&
379 inst
->src
[0].type
== BRW_REGISTER_TYPE_D
)) {
384 } else if (scan_inst
->conditional_mod
== inst
->conditional_mod
) {
388 } else if (!read_flag
) {
389 scan_inst
->conditional_mod
= inst
->conditional_mod
;
396 /* The conditional mod of the CMP/CMPN instructions behaves
397 * specially because the flag output is not calculated from the
398 * result of the instruction, but the other way around, which
399 * means that even if the condmod to propagate and the condmod
400 * from the CMP instruction are the same they will in general give
401 * different results because they are evaluated based on different
404 if (scan_inst
->opcode
== BRW_OPCODE_CMP
||
405 scan_inst
->opcode
== BRW_OPCODE_CMPN
)
408 /* From the Sky Lake PRM, Vol 2a, "Multiply":
410 * "When multiplying integer data types, if one of the sources
411 * is a DW, the resulting full precision data is stored in
412 * the accumulator. However, if the destination data type is
413 * either W or DW, the low bits of the result are written to
414 * the destination register and the remaining high bits are
415 * discarded. This results in undefined Overflow and Sign
416 * flags. Therefore, conditional modifiers and saturation
417 * (.sat) cannot be used in this case."
419 * We just disallow cmod propagation on all integer multiplies.
421 if (!brw_reg_type_is_floating_point(scan_inst
->dst
.type
) &&
422 scan_inst
->opcode
== BRW_OPCODE_MUL
)
425 enum brw_conditional_mod cond
=
426 inst
->src
[0].negate
? brw_swap_cmod(inst
->conditional_mod
)
427 : inst
->conditional_mod
;
429 /* From the Sky Lake PRM Vol. 7 "Assigning Conditional Mods":
431 * * Note that the [post condition signal] bits generated at
432 * the output of a compute are before the .sat.
434 * This limits the cases where we can propagate the conditional
435 * modifier. If scan_inst has a saturate modifier, then we can
436 * only propagate from inst if inst is 'scan_inst <= 0',
437 * 'scan_inst == 0', 'scan_inst != 0', or 'scan_inst > 0'. If
438 * inst is 'scan_inst == 0', the conditional modifier must be
439 * replace with LE. Likewise, if inst is 'scan_inst != 0', the
440 * conditional modifier must be replace with G.
442 * The only other cases are 'scan_inst < 0' (which is a
443 * contradiction) and 'scan_inst >= 0' (which is a tautology).
445 if (scan_inst
->saturate
) {
446 if (scan_inst
->dst
.type
!= BRW_REGISTER_TYPE_F
)
449 if (cond
!= BRW_CONDITIONAL_Z
&&
450 cond
!= BRW_CONDITIONAL_NZ
&&
451 cond
!= BRW_CONDITIONAL_LE
&&
452 cond
!= BRW_CONDITIONAL_G
)
455 if (inst
->opcode
!= BRW_OPCODE_MOV
&&
456 inst
->opcode
!= BRW_OPCODE_CMP
)
459 /* inst->src[1].is_zero() was tested before, but be safe
460 * against possible future changes in this code.
462 assert(inst
->opcode
!= BRW_OPCODE_CMP
|| inst
->src
[1].is_zero());
464 if (cond
== BRW_CONDITIONAL_Z
)
465 cond
= BRW_CONDITIONAL_LE
;
466 else if (cond
== BRW_CONDITIONAL_NZ
)
467 cond
= BRW_CONDITIONAL_G
;
470 /* Otherwise, try propagating the conditional. */
471 if (scan_inst
->can_do_cmod() &&
472 ((!read_flag
&& scan_inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) ||
473 scan_inst
->conditional_mod
== cond
)) {
474 scan_inst
->conditional_mod
= cond
;
475 scan_inst
->flag_subreg
= inst
->flag_subreg
;
482 if ((scan_inst
->flags_written() & flags_written
) != 0)
485 read_flag
= read_flag
||
486 (scan_inst
->flags_read(devinfo
) & flags_written
) != 0;
494 fs_visitor::opt_cmod_propagation()
496 bool progress
= false;
498 foreach_block_reverse(block
, cfg
) {
499 progress
= opt_cmod_propagation_local(devinfo
, block
) || progress
;
503 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);