i965: Move down genX_upload_sbe in profiles.
[mesa.git] / src / intel / compiler / brw_fs_cmod_propagation.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_fs.h"
25 #include "brw_cfg.h"
26 #include "brw_eu.h"
27
28 /** @file brw_fs_cmod_propagation.cpp
29 *
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
32 * sequence
33 *
34 * add(8) g70<1>F g69<8,8,1>F 4096F
35 * cmp.ge.f0(8) null g70<8,8,1>F 0F
36 *
37 * we can do the comparison as part of the ADD instruction directly:
38 *
39 * add.ge.f0(8) g70<1>F g69<8,8,1>F 4096F
40 *
41 * If there had been a use of the flag register and another CMP using g70
42 *
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
46 *
47 * we can recognize that the CMP is generating the flag value that already
48 * exists and therefore remove the instruction.
49 */
50
51 using namespace brw;
52
53 static bool
54 cmod_propagate_cmp_to_add(const gen_device_info *devinfo, bblock_t *block,
55 fs_inst *inst)
56 {
57 bool read_flag = false;
58 const unsigned flags_written = inst->flags_written();
59
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) {
64 bool negate;
65
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).
70 */
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]))) {
75 negate = false;
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]))) {
80 negate = true;
81 } else {
82 goto not_match;
83 }
84
85 /* If the scan instruction writes a different flag register than the
86 * instruction we're trying to propagate from, bail.
87 *
88 * FINISHME: The second part of the condition may be too strong.
89 * Perhaps (scan_inst->flags_written() & flags_written) !=
90 * flags_written?
91 */
92 if (scan_inst->flags_written() != 0 &&
93 scan_inst->flags_written() != flags_written)
94 goto not_match;
95
96 /* From the Sky Lake PRM Vol. 7 "Assigning Conditional Mods":
97 *
98 * * Note that the [post condition signal] bits generated at
99 * the output of a compute are before the .sat.
100 *
101 * So we don't have to bail if scan_inst has saturate.
102 */
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;
107
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;
112 inst->remove(block);
113 return true;
114 }
115 break;
116 }
117
118 not_match:
119 if ((scan_inst->flags_written() & flags_written) != 0)
120 break;
121
122 read_flag = read_flag ||
123 (scan_inst->flags_read(devinfo) & flags_written) != 0;
124 }
125
126 return false;
127 }
128
129 /**
130 * Propagate conditional modifiers from NOT instructions
131 *
132 * Attempt to convert sequences like
133 *
134 * or(8) g78<8,8,1> g76<8,8,1>UD g77<8,8,1>UD
135 * ...
136 * not.nz.f0(8) null g78<8,8,1>UD
137 *
138 * into
139 *
140 * or.z.f0(8) g78<8,8,1> g76<8,8,1>UD g77<8,8,1>UD
141 */
142 static bool
143 cmod_propagate_not(const gen_device_info *devinfo, bblock_t *block,
144 fs_inst *inst)
145 {
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();
149
150 if (cond != BRW_CONDITIONAL_Z && cond != BRW_CONDITIONAL_NZ)
151 return false;
152
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)
158 break;
159
160 if (scan_inst->is_partial_write() ||
161 scan_inst->dst.offset != inst->src[0].offset ||
162 scan_inst->exec_size != inst->exec_size)
163 break;
164
165 /* If the scan instruction writes a different flag register than the
166 * instruction we're trying to propagate from, bail.
167 *
168 * FINISHME: The second part of the condition may be too strong.
169 * Perhaps (scan_inst->flags_written() & flags_written) !=
170 * flags_written?
171 */
172 if (scan_inst->flags_written() != 0 &&
173 scan_inst->flags_written() != flags_written)
174 break;
175
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;
180 inst->remove(block);
181 return true;
182 }
183 break;
184 }
185
186 if ((scan_inst->flags_written() & flags_written) != 0)
187 break;
188
189 read_flag = read_flag ||
190 (scan_inst->flags_read(devinfo) & flags_written) != 0;
191 }
192
193 return false;
194 }
195
196 static bool
197 opt_cmod_propagation_local(const gen_device_info *devinfo, bblock_t *block)
198 {
199 bool progress = false;
200 int ip = block->end_ip + 1;
201
202 foreach_inst_in_block_reverse_safe(fs_inst, inst, block) {
203 ip--;
204
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))
213 continue;
214
215 /* An ABS source modifier can only be handled when processing a compare
216 * with a value other than zero.
217 */
218 if (inst->src[0].abs &&
219 (inst->opcode != BRW_OPCODE_CMP || inst->src[1].is_zero()))
220 continue;
221
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
227 * on a whim.
228 */
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))
233 continue;
234
235 if (inst->opcode == BRW_OPCODE_MOV &&
236 inst->conditional_mod != BRW_CONDITIONAL_NZ)
237 continue;
238
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
241 * instruction.
242 *
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).
247 */
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))
251 progress = true;
252
253 continue;
254 }
255
256 if (inst->opcode == BRW_OPCODE_NOT) {
257 progress = cmod_propagate_not(devinfo, block, inst) || progress;
258 continue;
259 }
260
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.
268 *
269 * FINISHME: The second part of the condition may be too strong.
270 * Perhaps (scan_inst->flags_written() & flags_written) !=
271 * flags_written?
272 */
273 if (scan_inst->flags_written() != 0 &&
274 scan_inst->flags_written() != flags_written)
275 break;
276
277 if (scan_inst->is_partial_write() ||
278 scan_inst->dst.offset != inst->src[0].offset ||
279 scan_inst->exec_size != inst->exec_size)
280 break;
281
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)) {
286 inst->remove(block);
287 progress = true;
288 break;
289 }
290
291 /* If the AND wasn't handled by the previous case, it isn't safe
292 * to remove it.
293 */
294 if (inst->opcode == BRW_OPCODE_AND)
295 break;
296
297 /* Not safe to use inequality operators if the types are different
298 */
299 if (scan_inst->dst.type != inst->src[0].type &&
300 inst->conditional_mod != BRW_CONDITIONAL_Z &&
301 inst->conditional_mod != BRW_CONDITIONAL_NZ)
302 break;
303
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
308 */
309 if (type_sz(scan_inst->dst.type) != type_sz(inst->dst.type))
310 break;
311
312 /* We should propagate from a MOV to another instruction in a
313 * sequence like:
314 *
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
317 */
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)) {
323 break;
324 }
325 } else if (brw_reg_type_is_floating_point(scan_inst->dst.type) !=
326 brw_reg_type_is_floating_point(inst->dst.type)) {
327 break;
328 }
329 }
330
331 /* If the instruction generating inst's source also wrote the
332 * flag, and inst is doing a simple .nz comparison, then inst
333 * is redundant - the appropriate value is already in the flag
334 * register. Delete inst.
335 */
336 if (inst->conditional_mod == BRW_CONDITIONAL_NZ &&
337 !inst->src[0].negate &&
338 scan_inst->flags_written()) {
339 inst->remove(block);
340 progress = true;
341 break;
342 }
343
344 /* The conditional mod of the CMP/CMPN instructions behaves
345 * specially because the flag output is not calculated from the
346 * result of the instruction, but the other way around, which
347 * means that even if the condmod to propagate and the condmod
348 * from the CMP instruction are the same they will in general give
349 * different results because they are evaluated based on different
350 * inputs.
351 */
352 if (scan_inst->opcode == BRW_OPCODE_CMP ||
353 scan_inst->opcode == BRW_OPCODE_CMPN)
354 break;
355
356 /* From the Sky Lake PRM, Vol 2a, "Multiply":
357 *
358 * "When multiplying integer data types, if one of the sources
359 * is a DW, the resulting full precision data is stored in
360 * the accumulator. However, if the destination data type is
361 * either W or DW, the low bits of the result are written to
362 * the destination register and the remaining high bits are
363 * discarded. This results in undefined Overflow and Sign
364 * flags. Therefore, conditional modifiers and saturation
365 * (.sat) cannot be used in this case."
366 *
367 * We just disallow cmod propagation on all integer multiplies.
368 */
369 if (!brw_reg_type_is_floating_point(scan_inst->dst.type) &&
370 scan_inst->opcode == BRW_OPCODE_MUL)
371 break;
372
373 enum brw_conditional_mod cond =
374 inst->src[0].negate ? brw_swap_cmod(inst->conditional_mod)
375 : inst->conditional_mod;
376
377 /* From the Sky Lake PRM Vol. 7 "Assigning Conditional Mods":
378 *
379 * * Note that the [post condition signal] bits generated at
380 * the output of a compute are before the .sat.
381 *
382 * This limits the cases where we can propagate the conditional
383 * modifier. If scan_inst has a saturate modifier, then we can
384 * only propagate from inst if inst is 'scan_inst <= 0',
385 * 'scan_inst == 0', 'scan_inst != 0', or 'scan_inst > 0'. If
386 * inst is 'scan_inst == 0', the conditional modifier must be
387 * replace with LE. Likewise, if inst is 'scan_inst != 0', the
388 * conditional modifier must be replace with G.
389 *
390 * The only other cases are 'scan_inst < 0' (which is a
391 * contradiction) and 'scan_inst >= 0' (which is a tautology).
392 */
393 if (scan_inst->saturate) {
394 if (scan_inst->dst.type != BRW_REGISTER_TYPE_F)
395 break;
396
397 if (cond != BRW_CONDITIONAL_Z &&
398 cond != BRW_CONDITIONAL_NZ &&
399 cond != BRW_CONDITIONAL_LE &&
400 cond != BRW_CONDITIONAL_G)
401 break;
402
403 if (inst->opcode != BRW_OPCODE_MOV &&
404 inst->opcode != BRW_OPCODE_CMP)
405 break;
406
407 /* inst->src[1].is_zero() was tested before, but be safe
408 * against possible future changes in this code.
409 */
410 assert(inst->opcode != BRW_OPCODE_CMP || inst->src[1].is_zero());
411
412 if (cond == BRW_CONDITIONAL_Z)
413 cond = BRW_CONDITIONAL_LE;
414 else if (cond == BRW_CONDITIONAL_NZ)
415 cond = BRW_CONDITIONAL_G;
416 }
417
418 /* Otherwise, try propagating the conditional. */
419 if (scan_inst->can_do_cmod() &&
420 ((!read_flag && scan_inst->conditional_mod == BRW_CONDITIONAL_NONE) ||
421 scan_inst->conditional_mod == cond)) {
422 scan_inst->conditional_mod = cond;
423 scan_inst->flag_subreg = inst->flag_subreg;
424 inst->remove(block);
425 progress = true;
426 }
427 break;
428 }
429
430 if ((scan_inst->flags_written() & flags_written) != 0)
431 break;
432
433 read_flag = read_flag ||
434 (scan_inst->flags_read(devinfo) & flags_written) != 0;
435 }
436 }
437
438 return progress;
439 }
440
441 bool
442 fs_visitor::opt_cmod_propagation()
443 {
444 bool progress = false;
445
446 foreach_block_reverse(block, cfg) {
447 progress = opt_cmod_propagation_local(devinfo, block) || progress;
448 }
449
450 if (progress)
451 invalidate_analysis(DEPENDENCY_INSTRUCTIONS);
452
453 return progress;
454 }