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.
52 opt_cmod_propagation_local(const gen_device_info
*devinfo
, bblock_t
*block
)
54 bool progress
= false;
55 int ip
= block
->end_ip
+ 1;
57 foreach_inst_in_block_reverse_safe(fs_inst
, inst
, block
) {
60 if ((inst
->opcode
!= BRW_OPCODE_AND
&&
61 inst
->opcode
!= BRW_OPCODE_CMP
&&
62 inst
->opcode
!= BRW_OPCODE_MOV
) ||
63 inst
->predicate
!= BRW_PREDICATE_NONE
||
64 !inst
->dst
.is_null() ||
65 (inst
->src
[0].file
!= VGRF
&& inst
->src
[0].file
!= ATTR
&&
66 inst
->src
[0].file
!= UNIFORM
))
69 /* An ABS source modifier can only be handled when processing a compare
70 * with a value other than zero.
72 if (inst
->src
[0].abs
&&
73 (inst
->opcode
!= BRW_OPCODE_CMP
|| inst
->src
[1].is_zero()))
76 /* Only an AND.NZ can be propagated. Many AND.Z instructions are
77 * generated (for ir_unop_not in fs_visitor::emit_bool_to_cond_code).
78 * Propagating those would require inverting the condition on the CMP.
79 * This changes both the flag value and the register destination of the
80 * CMP. That result may be used elsewhere, so we can't change its value
83 if (inst
->opcode
== BRW_OPCODE_AND
&&
84 !(inst
->src
[1].is_one() &&
85 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
&&
86 !inst
->src
[0].negate
))
89 if (inst
->opcode
== BRW_OPCODE_MOV
&&
90 inst
->conditional_mod
!= BRW_CONDITIONAL_NZ
)
93 bool read_flag
= false;
94 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
95 /* A CMP with a second source of zero can match with anything. A CMP
96 * with a second source that is not zero can only match with an ADD
99 if (inst
->opcode
== BRW_OPCODE_CMP
&& !inst
->src
[1].is_zero()) {
102 if (scan_inst
->opcode
!= BRW_OPCODE_ADD
)
105 /* A CMP is basically a subtraction. The result of the
106 * subtraction must be the same as the result of the addition.
107 * This means that one of the operands must be negated. So (a +
108 * b) vs (a == -b) or (a + -b) vs (a == b).
110 if ((inst
->src
[0].equals(scan_inst
->src
[0]) &&
111 inst
->src
[1].negative_equals(scan_inst
->src
[1])) ||
112 (inst
->src
[0].equals(scan_inst
->src
[1]) &&
113 inst
->src
[1].negative_equals(scan_inst
->src
[0]))) {
115 } else if ((inst
->src
[0].negative_equals(scan_inst
->src
[0]) &&
116 inst
->src
[1].equals(scan_inst
->src
[1])) ||
117 (inst
->src
[0].negative_equals(scan_inst
->src
[1]) &&
118 inst
->src
[1].equals(scan_inst
->src
[0]))) {
124 if (scan_inst
->is_partial_write() ||
125 scan_inst
->exec_size
!= inst
->exec_size
)
128 /* From the Sky Lake PRM Vol. 7 "Assigning Conditional Mods":
130 * * Note that the [post condition signal] bits generated at
131 * the output of a compute are before the .sat.
133 * So we don't have to bail if scan_inst has saturate.
136 /* Otherwise, try propagating the conditional. */
137 const enum brw_conditional_mod cond
=
138 negate
? brw_swap_cmod(inst
->conditional_mod
)
139 : inst
->conditional_mod
;
141 if (scan_inst
->can_do_cmod() &&
142 ((!read_flag
&& scan_inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) ||
143 scan_inst
->conditional_mod
== cond
)) {
144 scan_inst
->conditional_mod
= cond
;
151 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
152 inst
->src
[0], inst
->size_read(0))) {
153 if (scan_inst
->is_partial_write() ||
154 scan_inst
->dst
.offset
!= inst
->src
[0].offset
||
155 scan_inst
->exec_size
!= inst
->exec_size
)
158 /* CMP's result is the same regardless of dest type. */
159 if (inst
->conditional_mod
== BRW_CONDITIONAL_NZ
&&
160 scan_inst
->opcode
== BRW_OPCODE_CMP
&&
161 (inst
->dst
.type
== BRW_REGISTER_TYPE_D
||
162 inst
->dst
.type
== BRW_REGISTER_TYPE_UD
)) {
168 /* If the AND wasn't handled by the previous case, it isn't safe
171 if (inst
->opcode
== BRW_OPCODE_AND
)
174 /* Comparisons operate differently for ints and floats */
175 if (scan_inst
->dst
.type
!= inst
->dst
.type
&&
176 (scan_inst
->dst
.type
== BRW_REGISTER_TYPE_F
||
177 inst
->dst
.type
== BRW_REGISTER_TYPE_F
))
180 /* If the instruction generating inst's source also wrote the
181 * flag, and inst is doing a simple .nz comparison, then inst
182 * is redundant - the appropriate value is already in the flag
183 * register. Delete inst.
185 if (inst
->conditional_mod
== BRW_CONDITIONAL_NZ
&&
186 !inst
->src
[0].negate
&&
187 scan_inst
->flags_written()) {
193 /* The conditional mod of the CMP/CMPN instructions behaves
194 * specially because the flag output is not calculated from the
195 * result of the instruction, but the other way around, which
196 * means that even if the condmod to propagate and the condmod
197 * from the CMP instruction are the same they will in general give
198 * different results because they are evaluated based on different
201 if (scan_inst
->opcode
== BRW_OPCODE_CMP
||
202 scan_inst
->opcode
== BRW_OPCODE_CMPN
)
205 /* From the Sky Lake PRM Vol. 7 "Assigning Conditional Mods":
207 * * Note that the [post condition signal] bits generated at
208 * the output of a compute are before the .sat.
210 if (scan_inst
->saturate
)
213 /* From the Sky Lake PRM, Vol 2a, "Multiply":
215 * "When multiplying integer data types, if one of the sources
216 * is a DW, the resulting full precision data is stored in
217 * the accumulator. However, if the destination data type is
218 * either W or DW, the low bits of the result are written to
219 * the destination register and the remaining high bits are
220 * discarded. This results in undefined Overflow and Sign
221 * flags. Therefore, conditional modifiers and saturation
222 * (.sat) cannot be used in this case."
224 * We just disallow cmod propagation on all integer multiplies.
226 if (!brw_reg_type_is_floating_point(scan_inst
->dst
.type
) &&
227 scan_inst
->opcode
== BRW_OPCODE_MUL
)
230 /* Otherwise, try propagating the conditional. */
231 enum brw_conditional_mod cond
=
232 inst
->src
[0].negate
? brw_swap_cmod(inst
->conditional_mod
)
233 : inst
->conditional_mod
;
235 if (scan_inst
->can_do_cmod() &&
236 ((!read_flag
&& scan_inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) ||
237 scan_inst
->conditional_mod
== cond
)) {
238 scan_inst
->conditional_mod
= cond
;
246 if (scan_inst
->flags_written())
249 read_flag
= read_flag
|| scan_inst
->flags_read(devinfo
);
257 fs_visitor::opt_cmod_propagation()
259 bool progress
= false;
261 foreach_block_reverse(block
, cfg
) {
262 progress
= opt_cmod_propagation_local(devinfo
, block
) || progress
;
266 invalidate_live_intervals();