Debian Codegraph @

gcc/config/i386/x86-tune-sched.cc

raw
1 /* Scheduler hooks for IA-32 which implement CPU specific logic.
2 Copyright (C) 1988-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #define IN_TARGET_CODE 1
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "cfghooks.h"
29 #include "tm_p.h"
30 #include "target.h"
31 #include "insn-config.h"
32 #include "insn-attr.h"
33 #include "insn-opinit.h"
34 #include "recog.h"
35 #include "tm-constrs.h"
36
37 /* Return the maximum number of instructions a cpu can issue. */
38
39 int
40 ix86_issue_rate (void)
41 {
42 switch (ix86_tune)
43 {
44 case PROCESSOR_PENTIUM:
45 case PROCESSOR_LAKEMONT:
46 case PROCESSOR_BONNELL:
47 case PROCESSOR_SILVERMONT:
48 case PROCESSOR_INTEL:
49 case PROCESSOR_K6:
50 case PROCESSOR_BTVER2:
51 case PROCESSOR_PENTIUM4:
52 case PROCESSOR_NOCONA:
53 return 2;
54
55 case PROCESSOR_PENTIUMPRO:
56 case PROCESSOR_ATHLON:
57 case PROCESSOR_K8:
58 case PROCESSOR_AMDFAM10:
59 case PROCESSOR_BTVER1:
60 case PROCESSOR_LUJIAZUI:
61 return 3;
62
63 case PROCESSOR_BDVER1:
64 case PROCESSOR_BDVER2:
65 case PROCESSOR_BDVER3:
66 case PROCESSOR_BDVER4:
67 case PROCESSOR_ZNVER1:
68 case PROCESSOR_ZNVER2:
69 case PROCESSOR_ZNVER3:
70 case PROCESSOR_ZNVER4:
71 case PROCESSOR_CORE2:
72 case PROCESSOR_NEHALEM:
73 case PROCESSOR_SANDYBRIDGE:
74 case PROCESSOR_HASWELL:
75 case PROCESSOR_TREMONT:
76 case PROCESSOR_SKYLAKE:
77 case PROCESSOR_SKYLAKE_AVX512:
78 case PROCESSOR_CASCADELAKE:
79 case PROCESSOR_CANNONLAKE:
80 case PROCESSOR_ALDERLAKE:
81 case PROCESSOR_YONGFENG:
82 case PROCESSOR_SHIJIDADAO:
83 case PROCESSOR_GENERIC:
84 /* For znver5 decoder can handle 4 or 8 instructions per cycle,
85 op cache 12 instruction/cycle, dispatch 8 instructions
86 integer rename 8 instructions and Fp 6 instructions.
87
88 The scheduler, without understanding out of order nature of the CPU
89 is not going to be able to use more than 4 instructions since that
90 is limits of the decoders. */
91 case PROCESSOR_ZNVER5:
92 case PROCESSOR_C86_4G_M4:
93 case PROCESSOR_C86_4G_M6:
94 case PROCESSOR_C86_4G_M7:
95 return 4;
96
97 case PROCESSOR_ICELAKE_CLIENT:
98 case PROCESSOR_ICELAKE_SERVER:
99 case PROCESSOR_TIGERLAKE:
100 case PROCESSOR_COOPERLAKE:
101 case PROCESSOR_ROCKETLAKE:
102 return 5;
103
104 case PROCESSOR_SAPPHIRERAPIDS:
105 return 6;
106
107 default:
108 return 1;
109 }
110 }
111
112 /* Return true iff USE_INSN has a memory address with operands set by
113 SET_INSN. */
114
115 bool
116 ix86_agi_dependent (rtx_insn *set_insn, rtx_insn *use_insn)
117 {
118 int i;
119 extract_insn_cached (use_insn);
120 for (i = recog_data.n_operands - 1; i >= 0; --i)
121 if (MEM_P (recog_data.operand[i]))
122 {
123 rtx addr = XEXP (recog_data.operand[i], 0);
124 if (modified_in_p (addr, set_insn) != 0)
125 {
126 /* No AGI stall if SET_INSN is a push or pop and USE_INSN
127 has SP based memory (unless index reg is modified in a pop). */
128 rtx set = single_set (set_insn);
129 if (set
130 && (push_operand (SET_DEST (set), GET_MODE (SET_DEST (set)))
131 || pop_operand (SET_SRC (set), GET_MODE (SET_SRC (set)))))
132 {
133 struct ix86_address parts;
134 if (ix86_decompose_address (addr, &parts)
135 && parts.base == stack_pointer_rtx
136 && (parts.index == NULL_RTX
137 || MEM_P (SET_DEST (set))
138 || !modified_in_p (parts.index, set_insn)))
139 return false;
140 }
141 return true;
142 }
143 return false;
144 }
145 return false;
146 }
147
148 /* A subroutine of ix86_adjust_cost -- return TRUE iff INSN reads flags set
149 by DEP_INSN and nothing set by DEP_INSN. */
150
151 static bool
152 ix86_flags_dependent (rtx_insn *insn, rtx_insn *dep_insn, enum attr_type insn_type)
153 {
154 rtx set, set2;
155
156 /* Simplify the test for uninteresting insns. */
157 if (insn_type != TYPE_SETCC
158 && insn_type != TYPE_ICMOV
159 && insn_type != TYPE_FCMOV
160 && insn_type != TYPE_IBR)
161 return false;
162
163 if ((set = single_set (dep_insn)) != 0)
164 {
165 set = SET_DEST (set);
166 set2 = NULL_RTX;
167 }
168 else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
169 && XVECLEN (PATTERN (dep_insn), 0) == 2
170 && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
171 && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
172 {
173 set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
174 set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
175 }
176 else
177 return false;
178
179 if (!REG_P (set) || REGNO (set) != FLAGS_REG)
180 return false;
181
182 /* This test is true if the dependent insn reads the flags but
183 not any other potentially set register. */
184 if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
185 return false;
186
187 if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
188 return false;
189
190 return true;
191 }
192
193 /* Helper function for exact_store_load_dependency.
194 Return true if addr is found in insn. */
195 static bool
196 exact_dependency_1 (rtx addr, rtx insn)
197 {
198 enum rtx_code code;
199 const char *format_ptr;
200 int i, j;
201
202 code = GET_CODE (insn);
203 switch (code)
204 {
205 case MEM:
206 if (rtx_equal_p (addr, insn))
207 return true;
208 break;
209 case REG:
210 CASE_CONST_ANY:
211 case SYMBOL_REF:
212 case CODE_LABEL:
213 case PC:
214 case EXPR_LIST:
215 return false;
216 default:
217 break;
218 }
219
220 format_ptr = GET_RTX_FORMAT (code);
221 for (i = 0; i < GET_RTX_LENGTH (code); i++)
222 {
223 switch (*format_ptr++)
224 {
225 case 'e':
226 if (exact_dependency_1 (addr, XEXP (insn, i)))
227 return true;
228 break;
229 case 'E':
230 for (j = 0; j < XVECLEN (insn, i); j++)
231 if (exact_dependency_1 (addr, XVECEXP (insn, i, j)))
232 return true;
233 break;
234 }
235 }
236 return false;
237 }
238
239 /* Return true if there exists exact dependency for store & load, i.e.
240 the same memory address is used in them. */
241 static bool
242 exact_store_load_dependency (rtx_insn *store, rtx_insn *load)
243 {
244 rtx set1, set2;
245
246 set1 = single_set (store);
247 if (!set1)
248 return false;
249 if (!MEM_P (SET_DEST (set1)))
250 return false;
251 set2 = single_set (load);
252 if (!set2)
253 return false;
254 if (exact_dependency_1 (SET_DEST (set1), SET_SRC (set2)))
255 return true;
256 return false;
257 }
258
259
260 /* This function corrects the value of COST (latency) based on the relationship
261 between INSN and DEP_INSN through a dependence of type DEP_TYPE, and strength
262 DW. It should return the new value.
263
264 On x86 CPUs this is most commonly used to model the fact that valus of
265 registers used to compute address of memory operand needs to be ready
266 earlier than values of registers used in the actual operation. */
267
268 int
269 ix86_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
270 unsigned int)
271 {
272 enum attr_type insn_type, dep_insn_type;
273 enum attr_memory memory;
274 rtx set, set2;
275 int dep_insn_code_number;
276
277 /* Anti and output dependencies have zero cost on all CPUs. */
278 if (dep_type != 0)
279 return 0;
280
281 dep_insn_code_number = recog_memoized (dep_insn);
282
283 /* If we can't recognize the insns, we can't really do anything. */
284 if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
285 return cost;
286
287 insn_type = get_attr_type (insn);
288 dep_insn_type = get_attr_type (dep_insn);
289
290 switch (ix86_tune)
291 {
292 case PROCESSOR_PENTIUM:
293 case PROCESSOR_LAKEMONT:
294 /* Address Generation Interlock adds a cycle of latency. */
295 if (insn_type == TYPE_LEA)
296 {
297 rtx addr = PATTERN (insn);
298
299 if (GET_CODE (addr) == PARALLEL)
300 addr = XVECEXP (addr, 0, 0);
301
302 gcc_assert (GET_CODE (addr) == SET);
303
304 addr = SET_SRC (addr);
305 if (modified_in_p (addr, dep_insn))
306 cost += 1;
307 }
308 else if (ix86_agi_dependent (dep_insn, insn))
309 cost += 1;
310
311 /* ??? Compares pair with jump/setcc. */
312 if (ix86_flags_dependent (insn, dep_insn, insn_type))
313 cost = 0;
314
315 /* Floating point stores require value to be ready one cycle earlier. */
316 if (insn_type == TYPE_FMOV
317 && get_attr_memory (insn) == MEMORY_STORE
318 && !ix86_agi_dependent (dep_insn, insn))
319 cost += 1;
320 break;
321
322 case PROCESSOR_PENTIUMPRO:
323 /* INT->FP conversion is expensive. */
324 if (get_attr_fp_int_src (dep_insn))
325 cost += 5;
326
327 /* There is one cycle extra latency between an FP op and a store. */
328 if (insn_type == TYPE_FMOV
329 && (set = single_set (dep_insn)) != NULL_RTX
330 && (set2 = single_set (insn)) != NULL_RTX
331 && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
332 && MEM_P (SET_DEST (set2)))
333 cost += 1;
334
335 memory = get_attr_memory (insn);
336
337 /* Show ability of reorder buffer to hide latency of load by executing
338 in parallel with previous instruction in case
339 previous instruction is not needed to compute the address. */
340 if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
341 && !ix86_agi_dependent (dep_insn, insn))
342 {
343 /* Claim moves to take one cycle, as core can issue one load
344 at time and the next load can start cycle later. */
345 if (dep_insn_type == TYPE_IMOV
346 || dep_insn_type == TYPE_FMOV)
347 cost = 1;
348 else if (cost > 1)
349 cost--;
350 }
351 break;
352
353 case PROCESSOR_K6:
354 /* The esp dependency is resolved before
355 the instruction is really finished. */
356 if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
357 && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
358 return 1;
359
360 /* INT->FP conversion is expensive. */
361 if (get_attr_fp_int_src (dep_insn))
362 cost += 5;
363
364 memory = get_attr_memory (insn);
365
366 /* Show ability of reorder buffer to hide latency of load by executing
367 in parallel with previous instruction in case
368 previous instruction is not needed to compute the address. */
369 if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
370 && !ix86_agi_dependent (dep_insn, insn))
371 {
372 /* Claim moves to take one cycle, as core can issue one load
373 at time and the next load can start cycle later. */
374 if (dep_insn_type == TYPE_IMOV
375 || dep_insn_type == TYPE_FMOV)
376 cost = 1;
377 else if (cost > 2)
378 cost -= 2;
379 else
380 cost = 1;
381 }
382 break;
383
384 case PROCESSOR_AMDFAM10:
385 case PROCESSOR_BDVER1:
386 case PROCESSOR_BDVER2:
387 case PROCESSOR_BDVER3:
388 case PROCESSOR_BDVER4:
389 case PROCESSOR_BTVER1:
390 case PROCESSOR_BTVER2:
391 /* Stack engine allows to execute push&pop instructions in parall. */
392 if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
393 && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
394 return 0;
395 /* FALLTHRU */
396
397 case PROCESSOR_ATHLON:
398 case PROCESSOR_K8:
399 memory = get_attr_memory (insn);
400
401 /* Show ability of reorder buffer to hide latency of load by executing
402 in parallel with previous instruction in case
403 previous instruction is not needed to compute the address. */
404 if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
405 && !ix86_agi_dependent (dep_insn, insn))
406 {
407 enum attr_unit unit = get_attr_unit (insn);
408 int loadcost = 3;
409
410 /* Because of the difference between the length of integer and
411 floating unit pipeline preparation stages, the memory operands
412 for floating point are cheaper.
413
414 ??? For Athlon it the difference is most probably 2. */
415 if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
416 loadcost = 3;
417 else
418 loadcost = TARGET_CPU_P (ATHLON) ? 2 : 0;
419
420 if (cost >= loadcost)
421 cost -= loadcost;
422 else
423 cost = 0;
424 }
425 break;
426
427 case PROCESSOR_ZNVER1:
428 case PROCESSOR_ZNVER2:
429 case PROCESSOR_ZNVER3:
430 case PROCESSOR_ZNVER4:
431 case PROCESSOR_ZNVER5:
432 case PROCESSOR_C86_4G_M4:
433 case PROCESSOR_C86_4G_M6:
434 case PROCESSOR_C86_4G_M7:
435 /* Stack engine allows to execute push&pop instructions in parall. */
436 if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
437 && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
438 return 0;
439
440 memory = get_attr_memory (insn);
441
442 /* Show ability of reorder buffer to hide latency of load by executing
443 in parallel with previous instruction in case
444 previous instruction is not needed to compute the address. */
445 if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
446 && !ix86_agi_dependent (dep_insn, insn))
447 {
448 enum attr_unit unit = get_attr_unit (insn);
449 int loadcost;
450
451 /* TODO: On znver5 complex addressing modes have
452 greater latency. */
453 if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
454 loadcost = 4;
455 else
456 loadcost = 7;
457
458 if (cost >= loadcost)
459 cost -= loadcost;
460 else
461 cost = 0;
462 }
463 break;
464
465 case PROCESSOR_YONGFENG:
466 case PROCESSOR_SHIJIDADAO:
467 /* Stack engine allows to execute push&pop instructions in parallel. */
468 if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
469 && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
470 return 0;
471 /* FALLTHRU */
472
473 case PROCESSOR_LUJIAZUI:
474 memory = get_attr_memory (insn);
475
476 /* Show ability of reorder buffer to hide latency of load by executing
477 in parallel with previous instruction in case
478 previous instruction is not needed to compute the address. */
479 if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
480 && !ix86_agi_dependent (dep_insn, insn))
481 {
482 int loadcost = 4;
483
484 if (cost >= loadcost)
485 cost -= loadcost;
486 else
487 cost = 0;
488 }
489 break;
490
491 case PROCESSOR_CORE2:
492 case PROCESSOR_NEHALEM:
493 case PROCESSOR_SANDYBRIDGE:
494 case PROCESSOR_HASWELL:
495 case PROCESSOR_TREMONT:
496 case PROCESSOR_ALDERLAKE:
497 case PROCESSOR_GENERIC:
498 /* Stack engine allows to execute push&pop instructions in parall. */
499 if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
500 && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
501 return 0;
502
503 memory = get_attr_memory (insn);
504
505 /* Show ability of reorder buffer to hide latency of load by executing
506 in parallel with previous instruction in case
507 previous instruction is not needed to compute the address. */
508 if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
509 && !ix86_agi_dependent (dep_insn, insn))
510 {
511 if (cost >= 4)
512 cost -= 4;
513 else
514 cost = 0;
515 }
516 break;
517
518 case PROCESSOR_SILVERMONT:
519 case PROCESSOR_INTEL:
520 if (!reload_completed)
521 return cost;
522
523 /* Increase cost of integer loads. */
524 memory = get_attr_memory (dep_insn);
525 if (memory == MEMORY_LOAD || memory == MEMORY_BOTH)
526 {
527 enum attr_unit unit = get_attr_unit (dep_insn);
528 if (unit == UNIT_INTEGER && cost == 1)
529 {
530 if (memory == MEMORY_LOAD)
531 cost = 3;
532 else
533 {
534 /* Increase cost of ld/st for short int types only
535 because of store forwarding issue. */
536 rtx set = single_set (dep_insn);
537 if (set && (GET_MODE (SET_DEST (set)) == QImode
538 || GET_MODE (SET_DEST (set)) == HImode))
539 {
540 /* Increase cost of store/load insn if exact
541 dependence exists and it is load insn. */
542 enum attr_memory insn_memory = get_attr_memory (insn);
543 if (insn_memory == MEMORY_LOAD
544 && exact_store_load_dependency (dep_insn, insn))
545 cost = 3;
546 }
547 }
548 }
549 }
550
551 default:
552 break;
553 }
554
555 return cost;
556 }
557
558 /* How many alternative schedules to try. This should be as wide as the
559 scheduling freedom in the DFA, but no wider. Making this value too
560 large results extra work for the scheduler. */
561
562 int
563 ia32_multipass_dfa_lookahead (void)
564 {
565 /* Generally, we want haifa-sched:max_issue() to look ahead as far
566 as many instructions can be executed on a cycle, i.e.,
567 issue_rate. */
568 if (reload_completed)
569 return ix86_issue_rate ();
570 /* Don't use lookahead for pre-reload schedule to save compile time. */
571 return 0;
572 }
573
574 /* Return true if target platform supports macro-fusion. */
575
576 bool
577 ix86_macro_fusion_p ()
578 {
579 return TARGET_FUSE_CMP_AND_BRANCH;
580 }
581
582 /* Check whether MOV is a reg-reg move and ALU is an
583 ALU operation that allows macro-op fusion. */
584
585 static bool
586 ix86_fuse_mov_alu_p (rtx_insn *mov, rtx_insn *alu)
587 {
588 /* Validate mov:
589 - It should be reg-reg move with opcode 0x89 or 0x8B. */
590 rtx set1 = PATTERN (mov);
591 if (GET_CODE (set1) != SET
592 || !GENERAL_REG_P (SET_SRC (set1))
593 || !GENERAL_REG_P (SET_DEST (set1)))
594 return false;
595 rtx reg = SET_DEST (set1);
596 /* - it should have 0x89 or 0x8B opcode. */
597 if (!INTEGRAL_MODE_P (GET_MODE (reg))
598 || GET_MODE_SIZE (GET_MODE (reg)) < 2
599 || GET_MODE_SIZE (GET_MODE (reg)) > 8)
600 return false;
601 /* Validate ALU. */
602 if (GET_CODE (PATTERN (alu)) != PARALLEL)
603 return false;
604 rtx set2 = XVECEXP (PATTERN (alu), 0, 0);
605 if (GET_CODE (set2) != SET)
606 return false;
607 /* If this is instruction setting both compare and normal
608 register, the first set always sets flags, while
609 second set writes to the output operan. Pick
610 the second set. */
611 if (GET_CODE (SET_SRC (set2)) == COMPARE)
612 {
613 set2 = XVECEXP (PATTERN (alu), 0, 1);
614 if (GET_CODE (set2) != SET)
615 return false;
616 }
617 /* Match one of:
618 ADD ADC AND XOR OR SUB SBB INC DEC NOT SAL SHL SHR SAR
619 We also may add insn attribute to handle some of sporadic
620 case we output those with different RTX expressions. */
621
622 if (GET_CODE (SET_SRC (set2)) != PLUS
623 && GET_CODE (SET_SRC (set2)) != MINUS
624 && GET_CODE (SET_SRC (set2)) != XOR
625 && GET_CODE (SET_SRC (set2)) != AND
626 && GET_CODE (SET_SRC (set2)) != IOR
627 && GET_CODE (SET_SRC (set2)) != NOT
628 && GET_CODE (SET_SRC (set2)) != ASHIFT
629 && GET_CODE (SET_SRC (set2)) != ASHIFTRT
630 && GET_CODE (SET_SRC (set2)) != LSHIFTRT)
631 return false;
632 rtx op0 = XEXP (SET_SRC (set2), 0);
633 rtx op1 = GET_CODE (SET_SRC (set2)) != NOT ? XEXP (SET_SRC (set2), 1) : NULL;
634 /* One of operands should be register. */
635 if (op1 && (!REG_P (op0) || REGNO (op0) != REGNO (reg)))
636 std::swap (op0, op1);
637 if (!REG_P (op0) || REGNO (op0) != REGNO (reg))
638 return false;
639 if (op1
640 && !REG_P (op1)
641 && !x86_64_immediate_operand (op1, VOIDmode))
642 return false;
643 /* Only one of two parameters must be move destination. */
644 if (op1 && REG_P (op1) && REGNO (op1) == REGNO (reg))
645 return false;
646 return true;
647 }
648
649 /* Check whether current microarchitecture support macro fusion
650 for insn pair "CONDGEN + CONDJMP". Refer to
651 "Intel Architectures Optimization Reference Manual". */
652
653 bool
654 ix86_macro_fusion_pair_p (rtx_insn *condgen, rtx_insn *condjmp)
655 {
656 if (TARGET_FUSE_MOV_AND_ALU
657 && ix86_fuse_mov_alu_p (condgen, condjmp))
658 return true;
659 rtx src, imm = NULL_RTX;
660 enum rtx_code ccode;
661 rtx compare_set = NULL_RTX, test_if, cond;
662 rtx alu_set = NULL_RTX, addr = NULL_RTX;
663 rtx alu_clobber = NULL_RTX;
664 enum attr_type condgen_type;
665
666 if (!any_condjump_p (condjmp))
667 return false;
668
669 unsigned int condreg1, condreg2;
670 rtx cc_reg_1;
671 targetm.fixed_condition_code_regs (&condreg1, &condreg2);
672 cc_reg_1 = gen_rtx_REG (CCmode, condreg1);
673 if (!reg_referenced_p (cc_reg_1, PATTERN (condjmp))
674 || !condgen
675 || !modified_in_p (cc_reg_1, condgen))
676 return false;
677
678 condgen_type = get_attr_type (condgen);
679 if (condgen_type == TYPE_MULTI
680 && INSN_CODE (condgen) == code_for_stack_protect_test_1 (ptr_mode)
681 && TARGET_FUSE_ALU_AND_BRANCH)
682 {
683 /* stack_protect_test_<mode> ends with a sub, which subtracts
684 a non-rip special memory operand from a GPR. */
685 src = NULL_RTX;
686 alu_set = XVECEXP (PATTERN (condgen), 0, 1);
687 goto handle_stack_protect_test;
688 }
689 /* ??? zen5 can fuse cmp, test, sub, add, inc, dec, or, and xor.
690 Cores can not fuse or and xor which will pass the test below
691 since type is ALU. */
692 else if (condgen_type != TYPE_TEST
693 && condgen_type != TYPE_ICMP
694 && condgen_type != TYPE_INCDEC
695 && condgen_type != TYPE_ALU)
696 return false;
697
698 compare_set = single_set (condgen);
699 if (compare_set == NULL_RTX && !TARGET_FUSE_ALU_AND_BRANCH)
700 return false;
701
702 if (compare_set == NULL_RTX)
703 {
704 int i;
705 rtx pat = PATTERN (condgen);
706 for (i = 0; i < XVECLEN (pat, 0); i++)
707 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
708 {
709 rtx set_src = SET_SRC (XVECEXP (pat, 0, i));
710 if (GET_CODE (set_src) == COMPARE)
711 compare_set = XVECEXP (pat, 0, i);
712 else
713 alu_set = XVECEXP (pat, 0, i);
714 }
715 /* We also possibly generated ALU instruction only to set
716 flags. In this case there will be clobber. */
717 else if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
718 && GENERAL_REG_P (XEXP (XVECEXP (pat, 0, i), 0)))
719 alu_clobber = XVECEXP (pat, 0, i);
720 }
721 if (compare_set == NULL_RTX)
722 return false;
723 src = SET_SRC (compare_set);
724 if (GET_CODE (src) != COMPARE)
725 return false;
726
727 /* Check for memory operand. */
728 if (MEM_P (XEXP (src, 0)))
729 addr = XEXP (XEXP (src, 0), 0);
730 else if (MEM_P (XEXP (src, 1)))
731 addr = XEXP (XEXP (src, 1), 0);
732 /* Some CPUs, i.e. tigerlake and cooperlake does not fuse
733 ALU with memory operand. */
734 if (addr && !TARGET_FUSE_ALU_AND_BRANCH_MEM)
735 return false;
736 if (CONST_INT_P (XEXP (src, 0)))
737 imm = XEXP (src, 0);
738 else if (CONST_INT_P (XEXP (src, 1)))
739 imm = XEXP (src, 1);
740 /* Check that the instruction really has immediate.
741 In particular compare with 0 is done using test with no immediate. */
742 if (imm && !get_attr_length_immediate (condgen))
743 imm = NULL;
744 /* Macro-fusion for cmp/test MEM-IMM + conditional jmp is not
745 supported. */
746 if (addr && imm && !TARGET_FUSE_ALU_AND_BRANCH_MEM_IMM)
747 return false;
748
749 /* No fusion for RIP-relative address. */
750 if (addr && !TARGET_FUSE_ALU_AND_BRANCH_RIP_RELATIVE)
751 {
752 ix86_address parts;
753 int ok = ix86_decompose_address (addr, &parts);
754 gcc_assert (ok);
755
756 if (ix86_rip_relative_addr_p (&parts))
757 return false;
758 }
759 /* Znver5 supports fussion fusion with their reg/reg, reg/imm and
760 reg/mem forms. They are also supported when the instruction has an
761 immediate and displacement that meets the criteria of 4 byte displacement
762 and 2 byte immediate or the case of 2 byte displacement and 4 byte
763 immediate. We do not know the displacement size, so we ignore this
764 limitation. */
765
766 handle_stack_protect_test:
767 test_if = SET_SRC (pc_set (condjmp));
768 cond = XEXP (test_if, 0);
769 ccode = GET_CODE (cond);
770 /* Check whether conditional jump use Sign or Overflow Flags. */
771 if (!TARGET_FUSE_CMP_AND_BRANCH_SOFLAGS
772 && (ccode == GE || ccode == GT || ccode == LE || ccode == LT))
773 return false;
774
775 /* Return true for TYPE_TEST and TYPE_ICMP. */
776 if (condgen_type == TYPE_TEST || condgen_type == TYPE_ICMP)
777 return true;
778
779 /* The following is the case that macro-fusion for alu + jmp. */
780 if (!TARGET_FUSE_ALU_AND_BRANCH || (!alu_set && !alu_clobber))
781 return false;
782
783 /* No fusion for alu op with memory destination operand. */
784 if (alu_set && MEM_P (SET_DEST (alu_set)))
785 return false;
786
787
788 /* Macro-fusion for inc/dec + unsigned conditional jump is not
789 supported on some CPUs while supported on others (znver5 and core_avx512).
790 We however never generate it, so we do not need a specific tune for it. */
791 gcc_checking_assert (!(condgen_type == TYPE_INCDEC
792 && (ccode == GEU || ccode == GTU || ccode == LEU || ccode == LTU)));
793
794 return true;
795 }