Line data Source code
1 : /* If-conversion support.
2 : Copyright (C) 2000-2026 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 it
7 : 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, but WITHOUT
12 : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 : or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 : 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 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "target.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "cfghooks.h"
28 : #include "df.h"
29 : #include "memmodel.h"
30 : #include "tm_p.h"
31 : #include "expmed.h"
32 : #include "optabs.h"
33 : #include "regs.h"
34 : #include "emit-rtl.h"
35 : #include "recog.h"
36 :
37 : #include "cfgrtl.h"
38 : #include "cfganal.h"
39 : #include "cfgcleanup.h"
40 : #include "explow.h"
41 : #include "expr.h"
42 : #include "output.h"
43 : #include "cfgloop.h"
44 : #include "tree-pass.h"
45 : #include "dbgcnt.h"
46 : #include "shrink-wrap.h"
47 : #include "rtl-iter.h"
48 : #include "rtlanal.h"
49 : #include "ifcvt.h"
50 : #include "selftest.h"
51 :
52 : /* The number of instructions this pass may make run unconditionally in place
53 : of a branch. The conditional-execution path uses it as the number of insns
54 : it may predicate. The branchless paths use it as the number of insns they
55 : may speculate. The default charges the cost of an unpredictable branch,
56 : plus one. Some targets override it. */
57 :
58 : #ifndef MAX_CONDITIONAL_EXECUTE
59 : #define MAX_CONDITIONAL_EXECUTE \
60 : (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
61 : + 1)
62 : #endif
63 :
64 : #define NULL_BLOCK ((basic_block) NULL)
65 :
66 : /* Which of the three RTL if-conversion passes is running. ce1 runs before
67 : combine, ce2 after combine and ce3 after reload. The cost model treats the
68 : pre-combine run specially, so the phase is recorded here. */
69 : enum ifcvt_phase
70 : {
71 : IFCVT_BEFORE_COMBINE, /* ce1. */
72 : IFCVT_AFTER_COMBINE, /* ce2. */
73 : IFCVT_AFTER_RELOAD /* ce3. */
74 : };
75 :
76 : /* The if-conversion pass currently running. */
77 : static ifcvt_phase ifcvt_pass_phase;
78 :
79 : /* True if the target has the cbranchcc4 optab. */
80 : static bool have_cbranchcc4;
81 :
82 : /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
83 : static int num_possible_if_blocks;
84 :
85 : /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
86 : execution. */
87 : static int num_updated_if_blocks;
88 :
89 : /* # of changes made. */
90 : static int num_true_changes;
91 :
92 : /* Whether this pass over the function converted anything, so that another
93 : pass may find further opportunities in what it left behind. Every
94 : successful conversion sets it, not only the conditional-execution one. */
95 : static bool ifcvt_changed_p;
96 :
97 : /* Blocks that are inside a loop at the start of this pass. */
98 : static bitmap_head bbs_in_loops;
99 :
100 : /* Forward references. */
101 : static bool noce_operand_ok (const_rtx);
102 : static void merge_if_block (ce_if_block *);
103 : static bool find_cond_trap (basic_block, edge, edge);
104 : static bool cond_exec_find_if_block (ce_if_block *);
105 : static bool find_if_case_1 (basic_block, edge, edge);
106 : static bool find_if_case_2 (basic_block, edge, edge);
107 : static bool dead_or_predicable (basic_block, basic_block, basic_block,
108 : edge, bool);
109 : static rtx_insn *block_has_only_trap (basic_block);
110 : static void init_noce_multiple_sets_info (basic_block,
111 : auto_delete_vec<noce_multiple_sets_info> &);
112 : static bool noce_convert_multiple_sets_1 (noce_if_info *, rtx,
113 : auto_delete_vec<noce_multiple_sets_info> &,
114 : auto_delete_vec<noce_multiple_sets_info> &, unsigned,
115 : const vec<unsigned> &, int *, bool *);
116 :
117 : /* Count the number of non-jump active insns in BB. */
118 :
119 : static int
120 0 : count_bb_insns (const_basic_block bb)
121 : {
122 0 : int count = 0;
123 0 : rtx_insn *insn = BB_HEAD (bb);
124 :
125 0 : while (1)
126 : {
127 0 : if (active_insn_p (insn) && !JUMP_P (insn))
128 0 : count++;
129 :
130 0 : if (insn == BB_END (bb))
131 : break;
132 0 : insn = NEXT_INSN (insn);
133 : }
134 :
135 0 : return count;
136 : }
137 :
138 : /* Determine whether the total insn_cost on non-jump insns in
139 : basic block BB is less than MAX_COST. This function returns
140 : false if the cost of any instruction could not be estimated.
141 :
142 : The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
143 : as those insns are being speculated. MAX_COST is scaled with SCALE
144 : plus a small fudge factor. */
145 :
146 : static bool
147 3081827 : cheap_bb_rtx_cost_p (const_basic_block bb,
148 : profile_probability prob, int max_cost)
149 : {
150 3081827 : int count = 0;
151 3081827 : rtx_insn *insn = BB_HEAD (bb);
152 3081827 : bool speed = optimize_bb_for_speed_p (bb);
153 3081827 : int scale = prob.initialized_p () ? prob.to_reg_br_prob_base ()
154 : : REG_BR_PROB_BASE;
155 :
156 : /* Set scale to REG_BR_PROB_BASE to void the identical scaling
157 : applied to insn_cost when optimizing for size. Only do
158 : this after combine because if-conversion might interfere with
159 : passes before combine.
160 :
161 : Use optimize_function_for_speed_p instead of the pre-defined
162 : variable speed to make sure it is set to same value for all
163 : basic blocks in one if-conversion transformation. */
164 3081827 : if (!optimize_function_for_speed_p (cfun)
165 3081827 : && ifcvt_pass_phase != IFCVT_BEFORE_COMBINE)
166 : scale = REG_BR_PROB_BASE;
167 : /* Our branch probability/scaling factors are just estimates and don't
168 : account for cases where we can get speculation for free and other
169 : secondary benefits. So we fudge the scale factor to make speculating
170 : appear a little more profitable when optimizing for performance. */
171 : else
172 3029802 : scale += REG_BR_PROB_BASE / 8;
173 :
174 :
175 3081827 : max_cost *= scale;
176 :
177 12937239 : while (1)
178 : {
179 16019066 : if (NONJUMP_INSN_P (insn))
180 : {
181 : /* Inline-asm's cost is not very estimatable.
182 : It could be a costly instruction but the
183 : estimate would be the same as a non costly
184 : instruction. */
185 4619388 : if (asm_noperands (PATTERN (insn)) >= 0)
186 : return false;
187 :
188 4616695 : int cost = insn_cost (insn, speed) * REG_BR_PROB_BASE;
189 4616695 : if (cost == 0)
190 : return false;
191 :
192 : /* If this instruction is the load or set of a "stack" register,
193 : such as a floating point register on x87, then the cost of
194 : speculatively executing this insn may need to include
195 : the additional cost of popping its result off of the
196 : register stack. Unfortunately, correctly recognizing and
197 : accounting for this additional overhead is tricky, so for
198 : now we simply prohibit such speculative execution. */
199 : #ifdef STACK_REGS
200 4565833 : {
201 4565833 : rtx set = single_set (insn);
202 4565833 : if (set && STACK_REG_P (SET_DEST (set)))
203 : return false;
204 : }
205 : #endif
206 :
207 4561935 : count += cost;
208 4561935 : if (count >= max_cost)
209 : return false;
210 : }
211 11399678 : else if (CALL_P (insn))
212 : return false;
213 :
214 13537953 : if (insn == BB_END (bb))
215 : break;
216 12937239 : insn = NEXT_INSN (insn);
217 12937239 : }
218 :
219 : return true;
220 : }
221 :
222 : /* Return the first non-jump active insn in the basic block. */
223 :
224 : static rtx_insn *
225 1544479 : first_active_insn (basic_block bb)
226 : {
227 1544479 : rtx_insn *insn = BB_HEAD (bb);
228 :
229 1544479 : if (LABEL_P (insn))
230 : {
231 283982 : if (insn == BB_END (bb))
232 : return NULL;
233 283982 : insn = NEXT_INSN (insn);
234 : }
235 :
236 5212158 : while (NOTE_P (insn) || DEBUG_INSN_P (insn))
237 : {
238 3667679 : if (insn == BB_END (bb))
239 : return NULL;
240 3667679 : insn = NEXT_INSN (insn);
241 : }
242 :
243 1544479 : if (JUMP_P (insn))
244 0 : return NULL;
245 :
246 : return insn;
247 : }
248 :
249 : /* Return the last non-jump active insn in the basic block. */
250 :
251 : static rtx_insn *
252 2557305 : last_active_insn (basic_block bb, bool skip_use_p)
253 : {
254 2557305 : rtx_insn *insn = BB_END (bb);
255 2557305 : rtx_insn *head = BB_HEAD (bb);
256 :
257 2557305 : while (NOTE_P (insn)
258 : || JUMP_P (insn)
259 : || DEBUG_INSN_P (insn)
260 5828073 : || (skip_use_p
261 0 : && NONJUMP_INSN_P (insn)
262 0 : && GET_CODE (PATTERN (insn)) == USE))
263 : {
264 3271016 : if (insn == head)
265 : return NULL;
266 3270768 : insn = PREV_INSN (insn);
267 : }
268 :
269 2557057 : if (LABEL_P (insn))
270 94 : return NULL;
271 :
272 : return insn;
273 : }
274 :
275 : /* Return the active insn before INSN inside basic block CURR_BB. */
276 :
277 : static rtx_insn *
278 0 : find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
279 : {
280 0 : if (!insn || insn == BB_HEAD (curr_bb))
281 : return NULL;
282 :
283 0 : while ((insn = PREV_INSN (insn)) != NULL_RTX)
284 : {
285 0 : if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
286 : break;
287 :
288 : /* No other active insn all the way to the start of the basic block. */
289 0 : if (insn == BB_HEAD (curr_bb))
290 : return NULL;
291 : }
292 :
293 : return insn;
294 : }
295 :
296 : /* Return the active insn after INSN inside basic block CURR_BB. */
297 :
298 : static rtx_insn *
299 0 : find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
300 : {
301 0 : if (!insn || insn == BB_END (curr_bb))
302 : return NULL;
303 :
304 0 : while ((insn = NEXT_INSN (insn)) != NULL_RTX)
305 : {
306 0 : if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
307 : break;
308 :
309 : /* No other active insn all the way to the end of the basic block. */
310 0 : if (insn == BB_END (curr_bb))
311 : return NULL;
312 : }
313 :
314 : return insn;
315 : }
316 :
317 : /* Return the basic block reached by falling through the basic block BB. */
318 :
319 : static basic_block
320 0 : block_fallthru (basic_block bb)
321 : {
322 0 : edge e = find_fallthru_edge (bb->succs);
323 :
324 0 : return (e) ? e->dest : NULL_BLOCK;
325 : }
326 :
327 : /* Return true if RTXs A and B can be safely interchanged. */
328 :
329 : static bool
330 496903 : rtx_interchangeable_p (const_rtx a, const_rtx b)
331 : {
332 496903 : if (!rtx_equal_p (a, b))
333 : return false;
334 :
335 181909 : if (GET_CODE (a) != MEM)
336 : return true;
337 :
338 : /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
339 : reference is not. Interchanging a dead type-unsafe memory reference with
340 : a live type-safe one creates a live type-unsafe memory reference, in other
341 : words, it makes the program illegal.
342 : We check here conservatively whether the two memory references have equal
343 : memory attributes. */
344 :
345 2160 : return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
346 : }
347 :
348 :
349 : /* Go through the insns from START to END, converting them to conditional
350 : execution format if possible. CE_INFO describes the if-block being
351 : converted and is only read by the target IFCVT_MODIFY_INSN macro. TEST is
352 : the conditional-execution test to predicate them on and PROB_VAL the
353 : probability that it holds. MOD_OK allows one insn that modifies TEST, in
354 : which case it must be the last one. Return TRUE if all of the non-note
355 : insns were processed. */
356 :
357 : static bool
358 0 : cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
359 : rtx_insn *start, rtx_insn *end, rtx test,
360 : profile_probability prob_val, bool mod_ok)
361 : {
362 0 : bool must_be_last = false;
363 0 : rtx_insn *insn;
364 0 : rtx xtest;
365 0 : rtx pattern;
366 :
367 0 : if (!start || !end)
368 : return false;
369 :
370 0 : for (insn = start; ; insn = NEXT_INSN (insn))
371 : {
372 : /* dwarf2out can't cope with conditional prologues. */
373 0 : if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
374 : return false;
375 :
376 0 : if (NOTE_P (insn) || DEBUG_INSN_P (insn))
377 0 : goto insn_done;
378 :
379 0 : gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
380 :
381 : /* dwarf2out can't cope with conditional unwind info. */
382 0 : if (RTX_FRAME_RELATED_P (insn))
383 : return false;
384 :
385 : /* Remove USE insns that get in the way. */
386 0 : if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
387 : {
388 : /* ??? Ug. Actually unlinking the thing is problematic,
389 : given what we'd have to coordinate with our callers. */
390 0 : SET_INSN_DELETED (insn);
391 0 : goto insn_done;
392 : }
393 :
394 : /* Last insn wasn't last? */
395 0 : if (must_be_last)
396 : return false;
397 :
398 0 : if (modified_in_p (test, insn))
399 : {
400 0 : if (!mod_ok)
401 : return false;
402 : must_be_last = true;
403 : }
404 :
405 : /* Now build the conditional form of the instruction. */
406 0 : pattern = PATTERN (insn);
407 0 : xtest = copy_rtx (test);
408 :
409 : /* If this is already a COND_EXEC, rewrite the test to be an AND of the
410 : two conditions. */
411 0 : if (GET_CODE (pattern) == COND_EXEC)
412 : {
413 0 : if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
414 : return false;
415 :
416 0 : xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
417 : COND_EXEC_TEST (pattern));
418 0 : pattern = COND_EXEC_CODE (pattern);
419 : }
420 :
421 0 : pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
422 :
423 : /* If the machine needs to modify the insn being conditionally executed,
424 : say for example to force a constant integer operand into a temp
425 : register, do so here. */
426 : #ifdef IFCVT_MODIFY_INSN
427 : IFCVT_MODIFY_INSN (ce_info, pattern, insn);
428 : if (! pattern)
429 : return false;
430 : #endif
431 :
432 0 : validate_change (insn, &PATTERN (insn), pattern, 1);
433 :
434 0 : if (CALL_P (insn) && prob_val.initialized_p ())
435 0 : validate_change (insn, ®_NOTES (insn),
436 : gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
437 : prob_val.to_reg_br_prob_note (),
438 : REG_NOTES (insn)), 1);
439 :
440 0 : insn_done:
441 0 : if (insn == end)
442 : break;
443 0 : }
444 :
445 : return true;
446 : }
447 :
448 : /* Return the condition for a jump. Do not do any special processing. */
449 :
450 : static rtx
451 200330 : cond_exec_get_condition (rtx_insn *jump, bool get_reversed = false)
452 : {
453 200330 : rtx test_if, cond;
454 :
455 200330 : if (any_condjump_p (jump))
456 200330 : test_if = SET_SRC (pc_set (jump));
457 : else
458 : return NULL_RTX;
459 200330 : cond = XEXP (test_if, 0);
460 :
461 : /* If this branches to JUMP_LABEL when the condition is false,
462 : reverse the condition. */
463 200330 : if (get_reversed
464 200330 : || (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
465 0 : && label_ref_label (XEXP (test_if, 2))
466 0 : == JUMP_LABEL (jump)))
467 : {
468 100165 : enum rtx_code rev = reversed_comparison_code (cond, jump);
469 100165 : if (rev == UNKNOWN)
470 : return NULL_RTX;
471 :
472 100165 : cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
473 : XEXP (cond, 1));
474 : }
475 :
476 : return cond;
477 : }
478 :
479 : /* Given a simple IF-THEN or IF-THEN-ELSE block described by CE_INFO, attempt
480 : to convert it to conditional execution. DO_MULTIPLE_P allows CE_INFO's
481 : recorded chain of && or || test blocks to be converted as well. Return
482 : TRUE if we were successful at converting the block. */
483 :
484 : static bool
485 0 : cond_exec_process_if_block (ce_if_block *ce_info, bool do_multiple_p)
486 : {
487 0 : basic_block test_bb = ce_info->test_bb; /* last test block */
488 0 : basic_block then_bb = ce_info->then_bb; /* THEN */
489 0 : basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
490 0 : rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
491 0 : rtx_insn *then_start; /* first insn in THEN block */
492 0 : rtx_insn *then_end; /* last insn + 1 in THEN block */
493 0 : rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
494 0 : rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
495 0 : int max; /* max # of insns to convert. */
496 0 : bool then_mod_ok; /* whether conditional mods are ok in THEN */
497 0 : rtx true_expr; /* test for else block insns */
498 0 : rtx false_expr; /* test for then block insns */
499 0 : profile_probability true_prob_val;/* probability of else block */
500 0 : profile_probability false_prob_val;/* probability of then block */
501 0 : rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
502 0 : rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
503 0 : rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
504 0 : rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
505 0 : int then_n_insns, else_n_insns, n_insns;
506 0 : enum rtx_code false_code;
507 0 : rtx note;
508 :
509 : /* If test is comprised of && or || elements, and we've failed at handling
510 : all of them together, just use the last test if it is the special case of
511 : && elements without an ELSE block. */
512 0 : if (!do_multiple_p && ce_info->num_multiple_test_blocks)
513 : {
514 0 : if (else_bb || ! ce_info->and_and_p)
515 : return false;
516 :
517 0 : ce_info->test_bb = test_bb = ce_info->last_test_bb;
518 0 : ce_info->num_multiple_test_blocks = 0;
519 0 : ce_info->num_and_and_blocks = 0;
520 0 : ce_info->num_or_or_blocks = 0;
521 : }
522 :
523 : /* Find the conditional jump to the ELSE or JOIN part, and isolate
524 : the test. */
525 0 : test_expr = cond_exec_get_condition (BB_END (test_bb));
526 0 : if (! test_expr)
527 : return false;
528 :
529 : /* If the conditional jump is more than just a conditional jump,
530 : then we cannot do conditional execution conversion on this block. */
531 0 : if (! onlyjump_p (BB_END (test_bb)))
532 : return false;
533 :
534 : /* Collect the bounds of where we're to search, skipping any labels, jumps
535 : and notes at the beginning and end of the block. Then count the total
536 : number of insns and see if it is small enough to convert. */
537 0 : then_start = first_active_insn (then_bb);
538 0 : then_end = last_active_insn (then_bb, true);
539 0 : then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
540 0 : n_insns = then_n_insns;
541 0 : max = MAX_CONDITIONAL_EXECUTE;
542 :
543 0 : if (else_bb)
544 : {
545 0 : int n_matching;
546 :
547 0 : max *= 2;
548 0 : else_start = first_active_insn (else_bb);
549 0 : else_end = last_active_insn (else_bb, true);
550 0 : else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
551 0 : n_insns += else_n_insns;
552 :
553 : /* Look for matching sequences at the head and tail of the two blocks,
554 : and limit the range of insns to be converted if possible. */
555 0 : n_matching = flow_find_cross_jump (then_bb, else_bb,
556 : &then_first_tail, &else_first_tail,
557 : NULL);
558 0 : if (then_first_tail == BB_HEAD (then_bb))
559 0 : then_start = then_end = NULL;
560 0 : if (else_first_tail == BB_HEAD (else_bb))
561 0 : else_start = else_end = NULL;
562 :
563 0 : if (n_matching > 0)
564 : {
565 0 : if (then_end)
566 0 : then_end = find_active_insn_before (then_bb, then_first_tail);
567 0 : if (else_end)
568 0 : else_end = find_active_insn_before (else_bb, else_first_tail);
569 0 : n_insns -= 2 * n_matching;
570 : }
571 :
572 0 : if (then_start
573 0 : && else_start
574 : && then_n_insns > n_matching
575 0 : && else_n_insns > n_matching)
576 : {
577 0 : int longest_match = MIN (then_n_insns - n_matching,
578 : else_n_insns - n_matching);
579 0 : n_matching
580 0 : = flow_find_head_matching_sequence (then_bb, else_bb,
581 : &then_last_head,
582 : &else_last_head,
583 : longest_match);
584 :
585 0 : if (n_matching > 0)
586 : {
587 0 : rtx_insn *insn;
588 :
589 : /* We won't pass the insns in the head sequence to
590 : cond_exec_process_insns, so we need to test them here
591 : to make sure that they don't clobber the condition. */
592 0 : for (insn = BB_HEAD (then_bb);
593 0 : insn != NEXT_INSN (then_last_head);
594 0 : insn = NEXT_INSN (insn))
595 0 : if (!LABEL_P (insn) && !NOTE_P (insn)
596 0 : && !DEBUG_INSN_P (insn)
597 0 : && modified_in_p (test_expr, insn))
598 : return false;
599 : }
600 :
601 0 : if (then_last_head == then_end)
602 0 : then_start = then_end = NULL;
603 0 : if (else_last_head == else_end)
604 0 : else_start = else_end = NULL;
605 :
606 0 : if (n_matching > 0)
607 : {
608 0 : if (then_start)
609 0 : then_start = find_active_insn_after (then_bb, then_last_head);
610 0 : if (else_start)
611 0 : else_start = find_active_insn_after (else_bb, else_last_head);
612 0 : n_insns -= 2 * n_matching;
613 : }
614 : }
615 : }
616 :
617 0 : if (n_insns > max)
618 : return false;
619 :
620 : /* Map test_expr/test_jump into the appropriate MD tests to use on
621 : the conditionally executed code. */
622 :
623 0 : true_expr = test_expr;
624 :
625 0 : false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
626 0 : if (false_code != UNKNOWN)
627 0 : false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
628 : XEXP (true_expr, 0), XEXP (true_expr, 1));
629 : else
630 : false_expr = NULL_RTX;
631 :
632 : #ifdef IFCVT_MODIFY_TESTS
633 : /* If the machine description needs to modify the tests, such as setting a
634 : conditional execution register from a comparison, it can do so here. */
635 : IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
636 :
637 : /* See if the conversion failed. */
638 : if (!true_expr || !false_expr)
639 : goto fail;
640 : #endif
641 :
642 0 : note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
643 0 : if (note)
644 : {
645 0 : true_prob_val = profile_probability::from_reg_br_prob_note (XINT (note, 0));
646 0 : false_prob_val = true_prob_val.invert ();
647 : }
648 : else
649 : {
650 : true_prob_val = profile_probability::uninitialized ();
651 : false_prob_val = profile_probability::uninitialized ();
652 : }
653 :
654 : /* If we have && or || tests, do them here. These tests are in the adjacent
655 : blocks after the first block containing the test. */
656 0 : if (ce_info->num_multiple_test_blocks > 0)
657 : {
658 0 : basic_block bb = test_bb;
659 0 : basic_block last_test_bb = ce_info->last_test_bb;
660 :
661 0 : if (! false_expr)
662 0 : goto fail;
663 :
664 0 : do
665 : {
666 0 : rtx_insn *start, *end;
667 0 : rtx t, f;
668 0 : enum rtx_code f_code;
669 :
670 0 : bb = block_fallthru (bb);
671 0 : start = first_active_insn (bb);
672 0 : end = last_active_insn (bb, true);
673 0 : if (start
674 0 : && ! cond_exec_process_insns (ce_info, start, end, false_expr,
675 : false_prob_val, false))
676 0 : goto fail;
677 :
678 : /* If the conditional jump is more than just a conditional jump, then
679 : we cannot do conditional execution conversion on this block. */
680 0 : if (! onlyjump_p (BB_END (bb)))
681 0 : goto fail;
682 :
683 : /* Find the conditional jump and isolate the test. */
684 0 : t = cond_exec_get_condition (BB_END (bb));
685 0 : if (! t)
686 0 : goto fail;
687 :
688 0 : f_code = reversed_comparison_code (t, BB_END (bb));
689 0 : if (f_code == UNKNOWN)
690 0 : goto fail;
691 :
692 0 : f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
693 0 : if (ce_info->and_and_p)
694 : {
695 0 : t = gen_rtx_AND (GET_MODE (t), true_expr, t);
696 0 : f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
697 : }
698 : else
699 : {
700 0 : t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
701 0 : f = gen_rtx_AND (GET_MODE (t), false_expr, f);
702 : }
703 :
704 : /* If the machine description needs to modify the tests, such as
705 : setting a conditional execution register from a comparison, it can
706 : do so here. */
707 : #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
708 : IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
709 :
710 : /* See if the conversion failed. */
711 : if (!t || !f)
712 : goto fail;
713 : #endif
714 :
715 0 : true_expr = t;
716 0 : false_expr = f;
717 : }
718 0 : while (bb != last_test_bb);
719 : }
720 :
721 : /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
722 : on then THEN block. */
723 0 : then_mod_ok = (else_bb == NULL_BLOCK);
724 :
725 : /* Go through the THEN and ELSE blocks converting the insns if possible
726 : to conditional execution. */
727 :
728 0 : if (then_end
729 0 : && (! false_expr
730 0 : || ! cond_exec_process_insns (ce_info, then_start, then_end,
731 : false_expr, false_prob_val,
732 : then_mod_ok)))
733 0 : goto fail;
734 :
735 0 : if (else_bb && else_end
736 0 : && ! cond_exec_process_insns (ce_info, else_start, else_end,
737 : true_expr, true_prob_val, true))
738 0 : goto fail;
739 :
740 : /* If we cannot apply the changes, fail. Do not go through the normal fail
741 : processing, since apply_change_group will call cancel_changes. */
742 0 : if (! apply_change_group ())
743 : {
744 : #ifdef IFCVT_MODIFY_CANCEL
745 : /* Cancel any machine dependent changes. */
746 : IFCVT_MODIFY_CANCEL (ce_info);
747 : #endif
748 : return false;
749 : }
750 :
751 : #ifdef IFCVT_MODIFY_FINAL
752 : /* Do any machine dependent final modifications. */
753 : IFCVT_MODIFY_FINAL (ce_info);
754 : #endif
755 :
756 : /* Conversion succeeded. */
757 0 : if (dump_file)
758 0 : fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
759 : n_insns, (n_insns == 1) ? " was" : "s were");
760 :
761 : /* Merge the blocks! If we had matching sequences, make sure to delete one
762 : copy at the appropriate location first: delete the copy in the THEN branch
763 : for a tail sequence so that the remaining one is executed last for both
764 : branches, and delete the copy in the ELSE branch for a head sequence so
765 : that the remaining one is executed first for both branches. */
766 0 : if (then_first_tail)
767 : {
768 0 : rtx_insn *from = then_first_tail;
769 0 : if (!INSN_P (from))
770 0 : from = find_active_insn_after (then_bb, from);
771 0 : delete_insn_chain (from, get_last_bb_insn (then_bb), false);
772 : }
773 0 : if (else_last_head)
774 0 : delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
775 :
776 0 : merge_if_block (ce_info);
777 0 : ifcvt_changed_p = true;
778 0 : return true;
779 :
780 0 : fail:
781 : #ifdef IFCVT_MODIFY_CANCEL
782 : /* Cancel any machine dependent changes. */
783 : IFCVT_MODIFY_CANCEL (ce_info);
784 : #endif
785 :
786 0 : cancel_changes (0);
787 0 : return false;
788 : }
789 :
790 : /* Return the condition to use when the arms of IF_INFO's if-region are
791 : swapped, and set *CODE to its comparison code. When the reversed condition
792 : has no rtx of its own the original condition is returned alongside the
793 : reversed code, which is the form the store-flag and conditional-move
794 : expanders take. *CODE is UNKNOWN if the condition cannot be reversed. */
795 :
796 : static rtx
797 184756 : noce_reversed_cond (noce_if_info *if_info, enum rtx_code *code)
798 : {
799 184756 : if (if_info->rev_cond)
800 : {
801 184756 : *code = GET_CODE (if_info->rev_cond);
802 184756 : return if_info->rev_cond;
803 : }
804 :
805 0 : *code = reversed_comparison_code (if_info->cond, if_info->jump);
806 0 : return if_info->cond;
807 : }
808 :
809 : /* Return the comparison code for reversed condition for IF_INFO,
810 : or UNKNOWN if reversing the condition is not possible. */
811 :
812 : static inline enum rtx_code
813 107072 : noce_reversed_cond_code (noce_if_info *if_info)
814 : {
815 107072 : enum rtx_code code;
816 11252 : noce_reversed_cond (if_info, &code);
817 107072 : return code;
818 : }
819 :
820 : /* Return true if INSN is a unit-cost register SET supported by the parallel
821 : cost model when optimizing for SPEED_P. */
822 :
823 : static bool
824 509116 : noce_parallel_costed_insn_p (rtx_insn *insn, bool speed_p)
825 : {
826 509116 : rtx set = single_set (insn);
827 509116 : if (!set || multiple_sets (insn))
828 : return false;
829 :
830 508888 : rtx dest = SET_DEST (set);
831 508888 : rtx pat = PATTERN (insn);
832 :
833 508888 : return (REG_P (dest)
834 507271 : && !contains_mem_rtx_p (pat)
835 478076 : && !side_effects_p (pat)
836 478072 : && !may_trap_p (pat)
837 982293 : && set_rtx_cost (set, speed_p) == COSTS_N_INSNS (1));
838 : }
839 :
840 : /* Map from a register number to the dependency level at which the current
841 : value of the register is available. */
842 :
843 : typedef hash_map<int_hash<unsigned int, -1U>, unsigned int> noce_ready_map;
844 :
845 : /* Store in *LEVEL the dependency level of the instruction that PROPERTIES
846 : describes, using the register availability in READY, then update READY
847 : for its definitions. Return false, leaving READY unchanged, if PROPERTIES
848 : has a non-register reference, otherwise return true. The caller must
849 : check that every register the instruction sets is set as a whole. */
850 :
851 : static bool
852 454071 : noce_dependency_level (noce_ready_map &ready,
853 : const vec_rtx_properties &properties,
854 : unsigned int *level)
855 : {
856 454071 : *level = 0;
857 1681878 : for (auto ref : properties.refs ())
858 : {
859 1227811 : if (!ref.is_reg ())
860 4 : return false;
861 1227807 : if (ref.is_read ())
862 679780 : if (unsigned int *time = ready.get (ref.regno))
863 277297 : *level = MAX (*level, *time);
864 : }
865 :
866 : /* Read all inputs before recording the definitions. A SET of a register
867 : defines every register number it covers, and a clobber produces no
868 : value. */
869 1681870 : for (auto ref : properties.refs ())
870 1227803 : if (ref.is_write () && !ref.is_clobber ())
871 : {
872 454071 : gcc_checking_assert (!ref.in_subreg ());
873 454071 : ready.put (ref.regno, *level + 1);
874 : }
875 454067 : return true;
876 : }
877 :
878 : /* Estimate the cost of SEQ using the target issue rate. Unit-cost register
879 : operations are grouped by RAW dependency level. The cost of each level is
880 : its instruction count divided by the issue rate, rounded up. Fall back to
881 : serial cost if any instruction cannot be modeled.
882 :
883 : In an instrumented build of all SPEC CPU2017 Integer rate benchmarks
884 : for RISC-V, the analysis handled 16,947 of 30,120 candidates (56.26%).
885 : It reduced the cost for 15,024 of the handled candidates (88.65%).
886 : The parallel-to-serial cost ratios for the handled candidates were:
887 :
888 : 0.8 < ratio <= 1.0 11.54 percent
889 : 0.6 < ratio <= 0.8 47.31 percent
890 : 0.4 < ratio <= 0.6 40.24 percent
891 : 0.2 < ratio <= 0.4 0.91 percent
892 : 0.0 < ratio <= 0.2 0.00 percent. */
893 :
894 : unsigned int
895 200526 : noce_parallel_seq_cost (rtx_insn *seq, bool speed_p)
896 : {
897 200526 : unsigned int serial_cost = seq_cost (seq, speed_p);
898 :
899 200526 : if (!speed_p || !targetm.sched.issue_rate)
900 : return serial_cost;
901 :
902 140270 : unsigned int issue_rate = MAX (targetm.sched.issue_rate (), 1);
903 :
904 : /* Track register availability and instruction counts per level. */
905 140270 : noce_ready_map ready;
906 140270 : auto_vec<unsigned int> insns_per_level;
907 :
908 594297 : for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
909 : {
910 509092 : if (!NONDEBUG_INSN_P (insn))
911 0 : continue;
912 :
913 509092 : if (!noce_parallel_costed_insn_p (insn, speed_p))
914 55065 : return serial_cost;
915 :
916 454027 : vec_rtx_properties properties;
917 454027 : properties.add_insn (insn, false);
918 454027 : if (properties.has_asm)
919 : return serial_cost;
920 :
921 454027 : unsigned int level;
922 454027 : if (!noce_dependency_level (ready, properties, &level))
923 : return serial_cost;
924 454027 : if (insns_per_level.length () <= level)
925 261890 : insns_per_level.safe_grow_cleared (level + 1, true);
926 454027 : ++insns_per_level[level];
927 509092 : }
928 :
929 : unsigned int parallel_cost = 0;
930 299563 : for (unsigned int i = 0; i < insns_per_level.length (); ++i)
931 214358 : parallel_cost += COSTS_N_INSNS (CEIL (insns_per_level[i], issue_rate));
932 :
933 : /* set_rtx_cost and insn_cost can disagree, so cap the estimate at the
934 : serial cost. */
935 85205 : return MIN (serial_cost, parallel_cost);
936 140270 : }
937 :
938 : /* Return true if SEQ is a good candidate as a replacement for the
939 : if-convertible sequence described in IF_INFO.
940 : This is the default implementation that targets can override
941 : through a target hook. */
942 :
943 : bool
944 200526 : default_noce_conversion_profitable_p (rtx_insn *seq,
945 : noce_if_info *if_info)
946 : {
947 200526 : bool speed_p = if_info->speed_p;
948 :
949 : /* Cost up the new sequence. */
950 200526 : unsigned int cost = noce_parallel_seq_cost (seq, speed_p);
951 :
952 200526 : if (cost <= if_info->original_cost)
953 : return true;
954 :
955 : /* When compiling for size, we can make a reasonably accurately guess
956 : at the size growth. When compiling for speed, use the maximum. */
957 93980 : return speed_p && cost <= if_info->max_seq_cost;
958 : }
959 :
960 : /* Helper function for noce_try_store_flag*. Return NULL_RTX on failure,
961 : including when REVERSEP is requested but the condition cannot be
962 : reversed; callers may rely on this and need not pre-check. */
963 :
964 : static rtx
965 58353 : noce_emit_store_flag (noce_if_info *if_info, rtx x, bool reversep,
966 : int normalize)
967 : {
968 58353 : rtx cond = if_info->cond;
969 58353 : bool cond_complex;
970 58353 : enum rtx_code code;
971 :
972 58353 : cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
973 58353 : || ! general_operand (XEXP (cond, 1), VOIDmode));
974 :
975 : /* If earliest == jump, or when the condition is complex, try to
976 : build the store_flag insn directly. */
977 :
978 34889 : if (cond_complex)
979 : {
980 23464 : rtx set = pc_set (if_info->jump);
981 23464 : cond = XEXP (SET_SRC (set), 0);
982 23464 : if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
983 23464 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
984 0 : reversep = !reversep;
985 23464 : if (if_info->then_else_reversed)
986 20651 : reversep = !reversep;
987 : }
988 34889 : else if (reversep
989 17934 : && if_info->rev_cond
990 17934 : && general_operand (XEXP (if_info->rev_cond, 0), VOIDmode)
991 52823 : && general_operand (XEXP (if_info->rev_cond, 1), VOIDmode))
992 : {
993 17934 : cond = if_info->rev_cond;
994 17934 : reversep = false;
995 : }
996 :
997 58353 : if (reversep)
998 4260 : code = reversed_comparison_code (cond, if_info->jump);
999 : else
1000 54093 : code = GET_CODE (cond);
1001 :
1002 : /* reversed_comparison_code returns UNKNOWN for an unordered code, or a
1003 : CC-mode compare it cannot trace; neither path below can use that. */
1004 58353 : if (code == UNKNOWN)
1005 : return NULL_RTX;
1006 :
1007 58353 : if ((if_info->cond_earliest == if_info->jump || cond_complex)
1008 23464 : && (normalize == 0 || STORE_FLAG_VALUE == normalize))
1009 : {
1010 17954 : rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
1011 : XEXP (cond, 1));
1012 17954 : rtx set = gen_rtx_SET (x, src);
1013 :
1014 17954 : start_sequence ();
1015 17954 : rtx_insn *insn = emit_insn (set);
1016 :
1017 17954 : if (recog_memoized (insn) >= 0)
1018 : {
1019 8097 : rtx_insn *seq = end_sequence ();
1020 8097 : emit_insn (seq);
1021 :
1022 8097 : if_info->cond_earliest = if_info->jump;
1023 :
1024 8097 : return x;
1025 : }
1026 :
1027 9857 : end_sequence ();
1028 : }
1029 :
1030 : /* Don't even try if the comparison operands or the mode of X are weird. */
1031 50256 : if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
1032 : return NULL_RTX;
1033 :
1034 : /* Don't try if mode of X is more than the max fixed mode size. */
1035 104637 : if (known_le (MAX_FIXED_MODE_SIZE, GET_MODE_BITSIZE (GET_MODE (x))))
1036 : return NULL_RTX;
1037 :
1038 33549 : return emit_store_flag (x, code, XEXP (cond, 0),
1039 : XEXP (cond, 1), VOIDmode,
1040 33549 : (code == LTU || code == LEU
1041 67098 : || code == GEU || code == GTU), normalize);
1042 : }
1043 :
1044 : /* Return true if X can be safely forced into a register by copy_to_mode_reg
1045 : / force_operand. */
1046 :
1047 : static bool
1048 2124717 : noce_can_force_operand (rtx x)
1049 : {
1050 2125166 : if (general_operand (x, VOIDmode))
1051 : return true;
1052 565967 : if (SUBREG_P (x))
1053 : {
1054 449 : if (!noce_can_force_operand (SUBREG_REG (x)))
1055 : return false;
1056 : return true;
1057 : }
1058 565518 : if (ARITHMETIC_P (x))
1059 : {
1060 467812 : if (!noce_can_force_operand (XEXP (x, 0))
1061 467812 : || !noce_can_force_operand (XEXP (x, 1)))
1062 : return false;
1063 462327 : switch (GET_CODE (x))
1064 : {
1065 : case MULT:
1066 : case MOD:
1067 : case UDIV:
1068 : case UMOD:
1069 : return true;
1070 1898 : case DIV:
1071 1898 : if (INTEGRAL_MODE_P (GET_MODE (x)))
1072 : return true;
1073 : /* FALLTHRU */
1074 448488 : default:
1075 448488 : auto optab = code_to_optab (GET_CODE (x));
1076 448488 : if (!optab)
1077 : return false;
1078 439458 : return optab_handler (optab, GET_MODE (x)) != CODE_FOR_nothing;
1079 : }
1080 : }
1081 97706 : if (UNARY_P (x))
1082 : {
1083 88853 : if (!noce_can_force_operand (XEXP (x, 0)))
1084 : return false;
1085 88832 : switch (GET_CODE (x))
1086 : {
1087 : case ZERO_EXTEND:
1088 : case SIGN_EXTEND:
1089 : case TRUNCATE:
1090 : case FLOAT_EXTEND:
1091 : case FLOAT_TRUNCATE:
1092 : case FIX:
1093 : case UNSIGNED_FIX:
1094 : case FLOAT:
1095 : case UNSIGNED_FLOAT:
1096 : return true;
1097 13488 : default:
1098 13488 : auto optab = code_to_optab (GET_CODE (x));
1099 13488 : if (!optab)
1100 : return false;
1101 13488 : return optab_handler (optab, GET_MODE (x)) != CODE_FOR_nothing;
1102 : }
1103 : }
1104 : return false;
1105 : }
1106 :
1107 : /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
1108 : X is the destination/target and Y is the value to copy. */
1109 :
1110 : static void
1111 150238 : noce_emit_move_insn (rtx x, rtx y)
1112 : {
1113 150238 : machine_mode outmode;
1114 150238 : rtx outer, inner;
1115 150238 : poly_int64 bitpos;
1116 :
1117 150238 : if (GET_CODE (x) != STRICT_LOW_PART)
1118 : {
1119 150238 : rtx_insn *seq, *insn;
1120 150238 : rtx target;
1121 150238 : optab ot;
1122 :
1123 150238 : start_sequence ();
1124 : /* Check that the SET_SRC is reasonable before calling emit_move_insn,
1125 : otherwise construct a suitable SET pattern ourselves. */
1126 150318 : insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
1127 150318 : ? emit_move_insn (x, y)
1128 109936 : : emit_insn (gen_rtx_SET (x, y));
1129 150238 : seq = end_sequence ();
1130 :
1131 150238 : if (recog_memoized (insn) <= 0)
1132 : {
1133 69156 : if (GET_CODE (x) == ZERO_EXTRACT)
1134 : {
1135 0 : rtx op = XEXP (x, 0);
1136 0 : unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
1137 0 : unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
1138 :
1139 : /* store_bit_field expects START to be relative to
1140 : BYTES_BIG_ENDIAN and adjusts this value for machines with
1141 : BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
1142 : invoke store_bit_field again it is necessary to have the START
1143 : value from the first call. */
1144 0 : if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1145 : {
1146 : if (MEM_P (op))
1147 : start = BITS_PER_UNIT - start - size;
1148 : else
1149 : {
1150 : gcc_assert (REG_P (op));
1151 : start = BITS_PER_WORD - start - size;
1152 : }
1153 : }
1154 :
1155 0 : gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
1156 0 : store_bit_field (op, size, start, 0, 0, GET_MODE (x), y, false,
1157 : false);
1158 0 : return;
1159 : }
1160 :
1161 69156 : switch (GET_RTX_CLASS (GET_CODE (y)))
1162 : {
1163 1414 : case RTX_UNARY:
1164 1414 : ot = code_to_optab (GET_CODE (y));
1165 1414 : if (ot && noce_can_force_operand (XEXP (y, 0)))
1166 : {
1167 1180 : start_sequence ();
1168 1180 : target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
1169 1180 : if (target != NULL_RTX)
1170 : {
1171 1180 : if (target != x)
1172 0 : emit_move_insn (x, target);
1173 1180 : seq = get_insns ();
1174 : }
1175 1180 : end_sequence ();
1176 : }
1177 : break;
1178 :
1179 36921 : case RTX_BIN_ARITH:
1180 36921 : case RTX_COMM_ARITH:
1181 36921 : ot = code_to_optab (GET_CODE (y));
1182 36921 : if (ot
1183 36921 : && noce_can_force_operand (XEXP (y, 0))
1184 73842 : && noce_can_force_operand (XEXP (y, 1)))
1185 : {
1186 36921 : start_sequence ();
1187 36921 : target = expand_binop (GET_MODE (y), ot,
1188 : XEXP (y, 0), XEXP (y, 1),
1189 : x, 0, OPTAB_DIRECT);
1190 36921 : if (target != NULL_RTX)
1191 : {
1192 36921 : if (target != x)
1193 0 : emit_move_insn (x, target);
1194 36921 : seq = get_insns ();
1195 : }
1196 36921 : end_sequence ();
1197 : }
1198 : break;
1199 :
1200 : default:
1201 : break;
1202 : }
1203 : }
1204 :
1205 150238 : emit_insn (seq);
1206 150238 : return;
1207 : }
1208 :
1209 0 : outer = XEXP (x, 0);
1210 0 : inner = XEXP (outer, 0);
1211 0 : outmode = GET_MODE (outer);
1212 0 : bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1213 0 : store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1214 : 0, 0, outmode, y, false, false);
1215 : }
1216 :
1217 : /* Return the CC reg if it is used in COND. */
1218 :
1219 : static rtx
1220 4122968 : cc_in_cond (rtx cond)
1221 : {
1222 4122968 : if (have_cbranchcc4 && cond
1223 4122968 : && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1224 143436 : return XEXP (cond, 0);
1225 :
1226 : return NULL_RTX;
1227 : }
1228 :
1229 : struct noce_cc_reg_set
1230 : {
1231 : HARD_REG_SET set;
1232 : unsigned int fixed_regno1;
1233 : unsigned int fixed_regno2;
1234 : };
1235 :
1236 : /* If DEST is a hard condition-code register, add it to the set in DATA.
1237 : PAT is the SET or CLOBBER that writes DEST. */
1238 :
1239 : static void
1240 1033649 : noce_record_cc_reg_set (rtx dest, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1241 : {
1242 1033649 : noce_cc_reg_set *cc_regs = (noce_cc_reg_set *) data;
1243 :
1244 1032013 : if (REG_P (dest) && HARD_REGISTER_P (dest)
1245 1394962 : && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_CC
1246 0 : || REGNO (dest) == targetm.flags_regnum
1247 0 : || REGNO (dest) == cc_regs->fixed_regno1
1248 0 : || REGNO (dest) == cc_regs->fixed_regno2))
1249 361313 : add_to_hard_reg_set (&cc_regs->set, GET_MODE (dest), REGNO (dest));
1250 1033649 : }
1251 :
1252 : /* Return true if SEQ writes a condition-code register that still holds a
1253 : value which is live on exit from TEST_BB.
1254 :
1255 : An if-converted sequence is emitted immediately before the jump that ends
1256 : TEST_BB, and the conversion then removes that jump, so a condition code
1257 : that is live out of TEST_BB is one which a later block still reads.
1258 : Expanding a conditional move can emit a fresh comparison, and that would
1259 : destroy it. */
1260 :
1261 : static bool
1262 229927 : noce_clobbers_live_cc_p (basic_block test_bb, rtx_insn *seq)
1263 : {
1264 229927 : noce_cc_reg_set cc_regs;
1265 229927 : CLEAR_HARD_REG_SET (cc_regs.set);
1266 229927 : cc_regs.fixed_regno1 = INVALID_REGNUM;
1267 229927 : cc_regs.fixed_regno2 = INVALID_REGNUM;
1268 229927 : targetm.fixed_condition_code_regs (&cc_regs.fixed_regno1,
1269 : &cc_regs.fixed_regno2);
1270 1343442 : for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1271 883588 : note_stores (insn, noce_record_cc_reg_set, &cc_regs);
1272 :
1273 229927 : bitmap live_out = df_get_live_out (test_bb);
1274 229927 : hard_reg_set_iterator hrsi;
1275 229927 : unsigned int regno;
1276 420561 : EXECUTE_IF_SET_IN_HARD_REG_SET (cc_regs.set, 0, regno, hrsi)
1277 191080 : if (bitmap_bit_p (live_out, regno))
1278 : return true;
1279 :
1280 : return false;
1281 : }
1282 :
1283 : /* Return sequence of instructions generated by if conversion. This
1284 : function calls end_sequence() to end the current stream, ensures
1285 : that the instructions are unshared, recognizable non-jump insns.
1286 : On failure, this function returns a NULL_RTX. */
1287 :
1288 : static rtx_insn *
1289 228466 : end_ifcvt_sequence (noce_if_info *if_info)
1290 : {
1291 228466 : rtx_insn *insn;
1292 228466 : rtx_insn *seq = get_insns ();
1293 228466 : rtx cc = cc_in_cond (if_info->cond);
1294 :
1295 228466 : set_used_flags (if_info->x);
1296 228466 : set_used_flags (if_info->cond);
1297 228466 : set_used_flags (if_info->a);
1298 228466 : set_used_flags (if_info->b);
1299 :
1300 1188252 : for (insn = seq; insn; insn = NEXT_INSN (insn))
1301 731320 : set_used_flags (insn);
1302 :
1303 228466 : unshare_all_rtl_in_chain (seq);
1304 228466 : end_sequence ();
1305 :
1306 : /* Make sure that all of the instructions emitted are recognizable,
1307 : and that we haven't introduced a new jump instruction.
1308 : As an exercise for the reader, build a general mechanism that
1309 : allows proper placement of required clobbers. */
1310 1155546 : for (insn = seq; insn; insn = NEXT_INSN (insn))
1311 730724 : if (JUMP_P (insn)
1312 730724 : || recog_memoized (insn) == -1
1313 : /* Make sure new generated code does not clobber CC. */
1314 1429417 : || (cc && set_of (cc, insn)))
1315 : return NULL;
1316 :
1317 : /* CC above only covers the condition code read by the converted branch. */
1318 196356 : if (noce_clobbers_live_cc_p (if_info->test_bb, seq))
1319 446 : return NULL;
1320 :
1321 : return seq;
1322 : }
1323 :
1324 : /* Return true iff the then and else basic block (if it exists)
1325 : consist of a single simple set instruction. */
1326 :
1327 : static bool
1328 3190423 : noce_simple_bbs (noce_if_info *if_info)
1329 : {
1330 0 : if (!if_info->then_simple)
1331 : return false;
1332 :
1333 2954154 : if (if_info->else_bb)
1334 0 : return if_info->else_simple;
1335 :
1336 : return true;
1337 : }
1338 :
1339 : /* Commit the wound-up candidate SEQ produced by a noce transform for IF_INFO.
1340 : Reject it (returning false, emitting nothing) if it failed to build, or if
1341 : CHECK_PROFITABLE and the target deems it not worthwhile. Otherwise emit it
1342 : before the branch, record NAME as the winning transform and return true.
1343 : This is the shared tail of the noce_try_* matchers. */
1344 :
1345 : static bool
1346 213644 : noce_commit_sequence (noce_if_info *if_info, rtx_insn *seq,
1347 : bool check_profitable, const char *name)
1348 : {
1349 213644 : if (!seq
1350 139172 : || (check_profitable
1351 137450 : && !targetm.noce_conversion_profitable_p (seq, if_info)))
1352 : return false;
1353 :
1354 141983 : emit_insn_before_setloc (seq, if_info->jump,
1355 141983 : if_info->insn_a->location ());
1356 141983 : if_info->transform_name = name;
1357 141983 : return true;
1358 : }
1359 :
1360 : /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1361 : "if (a == b) x = a; else x = b" into "x = b". */
1362 :
1363 : static bool
1364 254000 : noce_try_move (noce_if_info *if_info)
1365 : {
1366 254000 : rtx cond = if_info->cond;
1367 254000 : enum rtx_code code = GET_CODE (cond);
1368 254000 : rtx y;
1369 254000 : rtx_insn *seq;
1370 :
1371 254000 : if (code != NE && code != EQ)
1372 : return false;
1373 :
1374 258149 : if (!noce_simple_bbs (if_info))
1375 : return false;
1376 :
1377 : /* This optimization isn't valid if either A or B could be a NaN
1378 : or a signed zero. */
1379 178024 : if (HONOR_NANS (if_info->x)
1380 178024 : || HONOR_SIGNED_ZEROS (if_info->x))
1381 : return false;
1382 :
1383 : /* Check whether the operands of the comparison are A and B, in
1384 : either order. */
1385 151407 : if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1386 2463 : && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1387 153858 : || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1388 15811 : && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1389 : {
1390 24 : if (!rtx_interchangeable_p (if_info->a, if_info->b))
1391 : return false;
1392 :
1393 0 : y = (code == EQ) ? if_info->a : if_info->b;
1394 :
1395 : /* Avoid generating the move if the source is the destination. */
1396 0 : if (! rtx_equal_p (if_info->x, y))
1397 : {
1398 0 : start_sequence ();
1399 0 : noce_emit_move_insn (if_info->x, y);
1400 0 : seq = end_ifcvt_sequence (if_info);
1401 0 : if (!seq)
1402 : return false;
1403 :
1404 0 : emit_insn_before_setloc (seq, if_info->jump,
1405 0 : if_info->insn_a->location ());
1406 : }
1407 0 : if_info->transform_name = "noce_try_move";
1408 0 : return true;
1409 : }
1410 : return false;
1411 : }
1412 :
1413 : /* TEMP holds an all-ones or all-zeros splat of a tested sign bit. Return an
1414 : rtx that narrows it to VAL or zero, expanded into TARGET where possible, or
1415 : NULL_RTX if the expansion failed.
1416 :
1417 : The obvious form is a mask, but since the value is known to be -1 or 0 a
1418 : shift is sometimes cheaper: a logical right shift constructs 2^n-1 and a
1419 : left shift constructs ~(2^n-1). Some targets do not have efficient shifts,
1420 : so build the RTL for each applicable form and expand the cheapest. */
1421 :
1422 : static rtx
1423 85 : noce_splat_to_const (machine_mode mode, rtx temp, HOST_WIDE_INT val,
1424 : rtx target)
1425 : {
1426 85 : rtx and_form = gen_rtx_AND (mode, temp, GEN_INT (val));
1427 170 : rtx shift_left = gen_rtx_ASHIFT (mode, temp, GEN_INT (ctz_hwi (val)));
1428 85 : HOST_WIDE_INT rshift_count
1429 170 : = clz_hwi (val) & (GET_MODE_PRECISION (mode).to_constant () - 1);
1430 85 : rtx shift_right = gen_rtx_LSHIFTRT (mode, temp, GEN_INT (rshift_count));
1431 85 : bool speed_p = optimize_insn_for_speed_p ();
1432 :
1433 85 : if (exact_log2 (val + HOST_WIDE_INT_1U) >= 0
1434 52 : && (rtx_cost (shift_right, mode, SET, 1, speed_p)
1435 26 : < rtx_cost (and_form, mode, SET, 1, speed_p)))
1436 4 : return expand_simple_binop (mode, LSHIFTRT, temp, GEN_INT (rshift_count),
1437 4 : target, false, OPTAB_WIDEN);
1438 :
1439 81 : if (exact_log2 (~val + HOST_WIDE_INT_1U) >= 0
1440 0 : && (rtx_cost (shift_left, mode, SET, 1, speed_p)
1441 0 : < rtx_cost (and_form, mode, SET, 1, speed_p)))
1442 0 : return expand_simple_binop (mode, ASHIFT, temp, GEN_INT (ctz_hwi (val)),
1443 0 : target, false, OPTAB_WIDEN);
1444 :
1445 81 : return expand_simple_binop (mode, AND, temp, GEN_INT (val), target, false,
1446 81 : OPTAB_WIDEN);
1447 : }
1448 :
1449 : /* If a sign bit test is selecting across constants, we may be able
1450 : to generate efficient code utilizing the -1/0 result of a sign
1451 : bit splat idiom.
1452 :
1453 : Do this before trying the generalized conditional move as these
1454 : (when applicable) are hopefully faster than a conditional move. */
1455 :
1456 : static bool
1457 201655 : noce_try_sign_bit_splat (noce_if_info *if_info)
1458 : {
1459 201655 : rtx cond = if_info->cond;
1460 201655 : enum rtx_code code = GET_CODE (cond);
1461 :
1462 : /* We're looking for sign bit tests, so only a few cases are
1463 : interesting. LT/GE 0 LE/GT -1. */
1464 201655 : if (((code == LT || code == GE)
1465 11450 : && XEXP (cond, 1) == CONST0_RTX (GET_MODE (cond)))
1466 199127 : || ((code == LE || code == GT)
1467 11670 : && XEXP (cond, 1) == CONSTM1_RTX (GET_MODE (cond))))
1468 : ;
1469 : else
1470 : return false;
1471 :
1472 : /* It would be good if this could be extended since constant synthesis
1473 : on some platforms will result in blocks which fail this test. */
1474 11004 : if (!noce_simple_bbs (if_info))
1475 : return false;
1476 :
1477 : /* Only try this for constants in the true/false arms and a REG
1478 : destination. We could select between 0 and a REG pretty
1479 : easily with a logical AND. */
1480 6191 : if (!CONST_INT_P (if_info->a)
1481 2015 : || !CONST_INT_P (if_info->b)
1482 677 : || !REG_P (if_info->x))
1483 : return false;
1484 :
1485 677 : machine_mode mode = GET_MODE (if_info->x);
1486 :
1487 : /* If the mode of the destination does not match the mode of
1488 : the value we're testing, then this optimization is not valid. */
1489 677 : if (mode != GET_MODE (XEXP (cond, 0)))
1490 : return false;
1491 :
1492 210 : HOST_WIDE_INT val_a = INTVAL (if_info->a);
1493 210 : HOST_WIDE_INT val_b = INTVAL (if_info->b);
1494 :
1495 210 : rtx_insn *seq;
1496 210 : start_sequence ();
1497 :
1498 : /* We're testing the sign bit of this operand. */
1499 210 : rtx condop = XEXP (cond, 0);
1500 :
1501 : /* To splat the sign bit we arithmetically shift the
1502 : input value right by the size of the object - 1 bits.
1503 :
1504 : Note some targets do not have strong shifters, but do have
1505 : alternative ways to generate the sign bit splat. The
1506 : profitability test when we end the sequence should reject
1507 : cases when the branchy sequence is better. */
1508 210 : int splat_count = GET_MODE_BITSIZE (GET_MODE (condop)).to_constant () - 1;
1509 210 : rtx splat = GEN_INT (splat_count);
1510 :
1511 210 : rtx temp;
1512 210 : temp = expand_simple_binop (GET_MODE (XEXP (cond, 0)), ASHIFTRT,
1513 : XEXP (cond, 0), splat, NULL_RTX,
1514 : false, OPTAB_WIDEN);
1515 210 : if (!temp)
1516 0 : goto fail;
1517 :
1518 : /* IOR of anything with -1 still results in -1. So we can
1519 : IOR the other operand to generate a select between -1 and
1520 : an arbitrary constant. */
1521 210 : if (val_a == -1)
1522 : {
1523 47 : if (code == LT || code == LE)
1524 : {
1525 2 : temp = expand_simple_unop (mode, NOT, temp, NULL_RTX, true);
1526 2 : if (!temp)
1527 0 : goto fail;
1528 : }
1529 :
1530 47 : temp = expand_simple_binop (mode, IOR, temp, GEN_INT (val_b),
1531 : if_info->x, false, OPTAB_WIDEN);
1532 : }
1533 : /* AND of anything with 0 is still zero. So we can AND
1534 : with the -1 operand with the a constant to select
1535 : between the constant and zero. */
1536 163 : else if (val_b == 0)
1537 : {
1538 0 : if (code == LT || code == LE)
1539 : {
1540 0 : temp = expand_simple_unop (mode, NOT, temp, NULL_RTX, true);
1541 0 : if (!temp)
1542 0 : goto fail;
1543 : }
1544 :
1545 0 : temp = noce_splat_to_const (mode, temp, val_a, if_info->x);
1546 : }
1547 : /* Same cases, but with the test or arms swapped. These
1548 : can be realized as well, though it typically costs
1549 : an extra instruction. */
1550 163 : else if (val_b == -1)
1551 : {
1552 46 : if (code != LT && code != LE)
1553 : {
1554 2 : temp = expand_simple_unop (mode, NOT, temp, NULL_RTX, true);
1555 2 : if (!temp)
1556 0 : goto fail;
1557 : }
1558 :
1559 46 : temp = expand_simple_binop (mode, IOR, temp, GEN_INT (val_a),
1560 : if_info->x, false, OPTAB_WIDEN);
1561 : }
1562 117 : else if (val_a == 0)
1563 : {
1564 85 : if (code != LT && code != LE)
1565 : {
1566 10 : temp = expand_simple_unop (mode, NOT, temp, NULL_RTX, true);
1567 10 : if (!temp)
1568 0 : goto fail;
1569 : }
1570 :
1571 85 : temp = noce_splat_to_const (mode, temp, val_b, if_info->x);
1572 : }
1573 : /* Nothing worked. */
1574 : else
1575 : temp = NULL_RTX;
1576 :
1577 178 : if (!temp)
1578 32 : goto fail;
1579 :
1580 : /* Move into the final destination if the value wasn't
1581 : constructed there. */
1582 178 : if (if_info->x != temp)
1583 0 : emit_move_insn (if_info->x, temp);
1584 :
1585 : /* This ends the sequence and tests the cost model. */
1586 178 : seq = end_ifcvt_sequence (if_info);
1587 178 : return noce_commit_sequence (if_info, seq, true, "splat_sign_bit_trivial");
1588 :
1589 32 : fail:
1590 32 : end_ifcvt_sequence (if_info);
1591 32 : return false;
1592 : }
1593 :
1594 :
1595 : /* Try forming an IF_THEN_ELSE (cond, b, a) and collapsing that
1596 : through simplify_rtx. Sometimes that can eliminate the IF_THEN_ELSE.
1597 : If that is the case, emit the result into x. */
1598 :
1599 : static bool
1600 254000 : noce_try_ifelse_collapse (noce_if_info * if_info)
1601 : {
1602 339373 : if (!noce_simple_bbs (if_info))
1603 : return false;
1604 :
1605 235154 : machine_mode mode = GET_MODE (if_info->x);
1606 235154 : rtx if_then_else = simplify_gen_ternary (IF_THEN_ELSE, mode, mode,
1607 : if_info->cond, if_info->b,
1608 : if_info->a);
1609 :
1610 235154 : if (GET_CODE (if_then_else) == IF_THEN_ELSE)
1611 : return false;
1612 :
1613 48273 : rtx_insn *seq;
1614 48273 : start_sequence ();
1615 48273 : noce_emit_move_insn (if_info->x, if_then_else);
1616 48273 : seq = end_ifcvt_sequence (if_info);
1617 48273 : return noce_commit_sequence (if_info, seq, false, "noce_try_ifelse_collapse");
1618 : }
1619 :
1620 :
1621 : /* Convert "if (test) x = 1; else x = 0".
1622 :
1623 : Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1624 : tried in noce_try_store_flag_constants after noce_try_cmove has had
1625 : a go at the conversion. */
1626 :
1627 : static bool
1628 236548 : noce_try_store_flag (noce_if_info *if_info)
1629 : {
1630 236548 : bool reversep;
1631 236548 : rtx target;
1632 236548 : rtx_insn *seq;
1633 :
1634 308634 : if (!noce_simple_bbs (if_info))
1635 : return false;
1636 :
1637 217702 : if (CONST_INT_P (if_info->b)
1638 73098 : && INTVAL (if_info->b) == STORE_FLAG_VALUE
1639 13475 : && if_info->a == const0_rtx)
1640 : reversep = false;
1641 207166 : else if (if_info->b == const0_rtx
1642 30166 : && CONST_INT_P (if_info->a)
1643 20739 : && INTVAL (if_info->a) == STORE_FLAG_VALUE)
1644 : reversep = true;
1645 : else
1646 : return false;
1647 :
1648 30822 : start_sequence ();
1649 :
1650 30822 : target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1651 30822 : if (target)
1652 : {
1653 26199 : if (target != if_info->x)
1654 3022 : noce_emit_move_insn (if_info->x, target);
1655 :
1656 26199 : seq = end_ifcvt_sequence (if_info);
1657 26199 : return noce_commit_sequence (if_info, seq, false, "noce_try_store_flag");
1658 : }
1659 : else
1660 : {
1661 4623 : end_sequence ();
1662 4623 : return false;
1663 : }
1664 : }
1665 :
1666 :
1667 : /* Convert "if (test) x = -A; else x = A" into
1668 : x = A; if (test) x = -x if the machine can do the
1669 : conditional negate form of this cheaply.
1670 : Try this before noce_try_cmove that will just load the
1671 : immediates into two registers and do a conditional select
1672 : between them. If the target has a conditional negate or
1673 : conditional invert operation we can save a potentially
1674 : expensive constant synthesis. */
1675 :
1676 : static bool
1677 210261 : noce_try_inverse_constants (noce_if_info *if_info)
1678 : {
1679 272666 : if (!noce_simple_bbs (if_info))
1680 : return false;
1681 :
1682 191415 : if (!CONST_INT_P (if_info->a)
1683 51468 : || !CONST_INT_P (if_info->b)
1684 21178 : || !REG_P (if_info->x))
1685 : return false;
1686 :
1687 21178 : machine_mode mode = GET_MODE (if_info->x);
1688 :
1689 21178 : HOST_WIDE_INT val_a = INTVAL (if_info->a);
1690 21178 : HOST_WIDE_INT val_b = INTVAL (if_info->b);
1691 :
1692 21178 : rtx cond = if_info->cond;
1693 :
1694 21178 : rtx x = if_info->x;
1695 21178 : rtx target;
1696 :
1697 21178 : start_sequence ();
1698 :
1699 21178 : rtx_code code;
1700 21178 : if (val_b != HOST_WIDE_INT_MIN && val_a == -val_b)
1701 : code = NEG;
1702 19866 : else if (val_a == ~val_b)
1703 : code = NOT;
1704 : else
1705 : {
1706 19798 : end_sequence ();
1707 19798 : return false;
1708 : }
1709 :
1710 1380 : rtx tmp = gen_reg_rtx (mode);
1711 1380 : noce_emit_move_insn (tmp, if_info->a);
1712 :
1713 1380 : target = emit_conditional_neg_or_complement (x, code, mode, cond, tmp, tmp);
1714 :
1715 1380 : if (target)
1716 : {
1717 0 : rtx_insn *seq = get_insns ();
1718 :
1719 0 : if (!seq)
1720 : {
1721 0 : end_sequence ();
1722 0 : return false;
1723 : }
1724 :
1725 0 : if (target != if_info->x)
1726 0 : noce_emit_move_insn (if_info->x, target);
1727 :
1728 0 : seq = end_ifcvt_sequence (if_info);
1729 0 : return noce_commit_sequence (if_info, seq, false,
1730 : "noce_try_inverse_constants");
1731 : }
1732 :
1733 1380 : end_sequence ();
1734 1380 : return false;
1735 : }
1736 :
1737 : /* Check if OP is supported by conditional zero based if conversion,
1738 : returning TRUE if satisfied otherwise FALSE.
1739 :
1740 : OP is the operation to check. */
1741 :
1742 : static bool
1743 587400 : noce_cond_zero_binary_op_supported (rtx op)
1744 : {
1745 587400 : enum rtx_code opcode = GET_CODE (op);
1746 :
1747 0 : if (opcode == PLUS || opcode == MINUS || opcode == IOR || opcode == XOR
1748 : || opcode == ASHIFT || opcode == ASHIFTRT || opcode == LSHIFTRT
1749 : || opcode == ROTATE || opcode == ROTATERT || opcode == AND)
1750 0 : return true;
1751 :
1752 : return false;
1753 : }
1754 :
1755 : /* Convert "if (test) x = a; else x = a OP c " where c is 2^n.
1756 :
1757 : We can shift the output of a set flag insn left to generate 2^n
1758 : cheaply, then apply OP unconditionally. This works even if the
1759 : target does not have a conditional zero idiom. On targets such
1760 : as RISC-V this sequence may also encode better.
1761 :
1762 : This is based on noce_try_store_flag_constants. */
1763 :
1764 : static bool
1765 207132 : noce_try_shifted_store_flag (noce_if_info *if_info)
1766 : {
1767 207132 : rtx target;
1768 207132 : rtx_insn *seq;
1769 207132 : machine_mode mode = GET_MODE (if_info->x);
1770 207132 : rtx common = NULL_RTX;
1771 207132 : rtx_code code = UNKNOWN;
1772 :
1773 207132 : if (STORE_FLAG_VALUE != 1)
1774 : return false;
1775 :
1776 266483 : if (!noce_simple_bbs (if_info))
1777 : return false;
1778 :
1779 188286 : rtx a = if_info->a;
1780 188286 : rtx b = if_info->b;
1781 :
1782 : /* Most binary operators are allowed, essentially the same set
1783 : as the condzero arithmetic paths, with the exception of AND
1784 : which could be handled if we were inclined. */
1785 188286 : int shiftval;
1786 188286 : bool reversed = false;
1787 188286 : if (noce_cond_zero_binary_op_supported (a)
1788 24930 : && GET_CODE (a) != AND
1789 24714 : && GET_CODE (b) == REG
1790 20546 : && rtx_equal_p (XEXP (a, 0), b)
1791 18490 : && CONST_INT_P (XEXP (a, 1))
1792 9094 : && pow2p_hwi (INTVAL (XEXP (a, 1))))
1793 : {
1794 6708 : code = GET_CODE (a);
1795 6708 : common = XEXP (a, 0);
1796 6708 : shiftval = exact_log2 (INTVAL (XEXP (a, 1)));
1797 :
1798 : /* When the condition is true, we branch around A and thus we want
1799 : the condition reversed from what you might expect. */
1800 : reversed = true;
1801 : }
1802 181578 : else if (noce_cond_zero_binary_op_supported (b)
1803 2324 : && GET_CODE (b) != AND
1804 2284 : && GET_CODE (a) == REG
1805 229 : && rtx_equal_p (XEXP (b, 0), a)
1806 92 : && CONST_INT_P (XEXP (b, 1))
1807 71 : && pow2p_hwi (INTVAL (XEXP (b, 1))))
1808 : {
1809 40 : code = GET_CODE (b);
1810 40 : common = XEXP (b, 0);
1811 40 : shiftval = exact_log2 (INTVAL (XEXP (b, 1)));
1812 : }
1813 :
1814 :
1815 : /* If CODE hasn't been reset, then the basic expression form was wrong and
1816 : we can not optimize. */
1817 6748 : if (code == UNKNOWN)
1818 : return false;
1819 :
1820 6748 : start_sequence ();
1821 :
1822 : /* If X and COMMON are the same, then we're going to need a temporary. */
1823 6748 : if (rtx_equal_p (common, if_info->x))
1824 : {
1825 6172 : common = gen_reg_rtx (mode);
1826 6172 : noce_emit_move_insn (common, if_info->x);
1827 : }
1828 :
1829 6748 : target = noce_emit_store_flag (if_info, if_info->x, reversed, 1);
1830 6748 : if (!target)
1831 : {
1832 742 : end_sequence ();
1833 742 : return false;
1834 : }
1835 :
1836 : /* Right now TARGET has the value 1/0. A left shift will produce the
1837 : target value. Unnecessary if the shift value is zero. */
1838 6006 : if (shiftval != 0)
1839 1206 : target = expand_simple_binop (mode, ASHIFT, target,
1840 : GEN_INT (shiftval),
1841 : NULL_RTX, 0, OPTAB_WIDEN);
1842 :
1843 6006 : if (!target)
1844 : {
1845 0 : end_sequence ();
1846 0 : return false;
1847 : }
1848 :
1849 : /* Now we just need to apply the code to COMMON and TARGET. */
1850 6006 : target = expand_simple_binop (mode, code,
1851 : common, target,
1852 : NULL_RTX, 0, OPTAB_WIDEN);
1853 :
1854 6006 : if (!target)
1855 : {
1856 0 : end_sequence ();
1857 0 : return false;
1858 : }
1859 :
1860 : /* If necessary, store the result into the proper destination. */
1861 6006 : if (target != if_info->x)
1862 6006 : noce_emit_move_insn (if_info->x, target);
1863 :
1864 6006 : seq = end_ifcvt_sequence (if_info);
1865 6006 : return noce_commit_sequence (if_info, seq, true,
1866 6006 : "noce_try_shifted_store_flag");
1867 : }
1868 :
1869 :
1870 : /* Set *DIFF to ITRUE - IFALSE truncated to MODE and return true. Return
1871 : false, leaving *DIFF alone, when that difference is not representable and
1872 : so cannot be used to build a store-flag sequence. */
1873 :
1874 : static bool
1875 21726 : noce_representable_diff_p (HOST_WIDE_INT ifalse, HOST_WIDE_INT itrue,
1876 : machine_mode mode, HOST_WIDE_INT *diff)
1877 : {
1878 21726 : HOST_WIDE_INT d = (unsigned HOST_WIDE_INT) itrue - ifalse;
1879 :
1880 21726 : if ((d > 0) != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1881 : return false;
1882 :
1883 21591 : *diff = trunc_int_for_mode (d, mode);
1884 21591 : return true;
1885 : }
1886 :
1887 : /* Convert "if (test) x = a; else x = b", for A and B constant.
1888 : Also allow A = y + c1, B = y + c2, with a common y between A
1889 : and B. */
1890 :
1891 : static bool
1892 210261 : noce_try_store_flag_constants (noce_if_info *if_info)
1893 : {
1894 210261 : rtx target;
1895 210261 : rtx_insn *seq;
1896 210261 : bool reversep;
1897 210261 : HOST_WIDE_INT itrue, ifalse, diff, tmp;
1898 210261 : int normalize;
1899 210261 : bool can_reverse;
1900 210261 : machine_mode mode = GET_MODE (if_info->x);
1901 210261 : rtx common = NULL_RTX;
1902 :
1903 210261 : rtx a = if_info->a;
1904 210261 : rtx b = if_info->b;
1905 :
1906 : /* Handle cases like x := test ? y + 3 : y + 4. */
1907 210261 : if (GET_CODE (a) == PLUS
1908 23798 : && GET_CODE (b) == PLUS
1909 1556 : && CONST_INT_P (XEXP (a, 1))
1910 631 : && CONST_INT_P (XEXP (b, 1))
1911 550 : && rtx_equal_p (XEXP (a, 0), XEXP (b, 0))
1912 : /* Allow expressions that are not using the result or plain
1913 : registers where we handle overlap below. */
1914 210761 : && (REG_P (XEXP (a, 0))
1915 4 : || (noce_operand_ok (XEXP (a, 0))
1916 4 : && ! reg_overlap_mentioned_p (if_info->x, XEXP (a, 0)))))
1917 : {
1918 500 : common = XEXP (a, 0);
1919 500 : a = XEXP (a, 1);
1920 500 : b = XEXP (b, 1);
1921 : }
1922 :
1923 272666 : if (!noce_simple_bbs (if_info))
1924 : return false;
1925 :
1926 191415 : if (CONST_INT_P (a)
1927 51956 : && CONST_INT_P (b))
1928 : {
1929 21666 : ifalse = INTVAL (a);
1930 21666 : itrue = INTVAL (b);
1931 21666 : bool subtract_flag_p = false;
1932 :
1933 21666 : if (!noce_representable_diff_p (ifalse, itrue, mode, &diff))
1934 : return false;
1935 :
1936 21552 : can_reverse = noce_reversed_cond_code (if_info) != UNKNOWN;
1937 21552 : reversep = false;
1938 21552 : if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1939 : {
1940 11846 : normalize = 0;
1941 : /* We could collapse these cases but it is easier to follow the
1942 : diff/STORE_FLAG_VALUE combinations when they are listed
1943 : explicitly. */
1944 :
1945 : /* test ? 3 : 4
1946 : => 4 + (test != 0). */
1947 11846 : if (diff < 0 && STORE_FLAG_VALUE < 0)
1948 : reversep = false;
1949 : /* test ? 4 : 3
1950 : => can_reverse | 4 + (test == 0)
1951 : !can_reverse | 3 - (test != 0). */
1952 11846 : else if (diff > 0 && STORE_FLAG_VALUE < 0)
1953 : {
1954 : reversep = can_reverse;
1955 : subtract_flag_p = !can_reverse;
1956 : /* If we need to subtract the flag and we have PLUS-immediate
1957 : A and B then it is unlikely to be beneficial to play tricks
1958 : here. */
1959 : if (subtract_flag_p && common)
1960 : return false;
1961 : }
1962 : /* test ? 3 : 4
1963 : => can_reverse | 3 + (test == 0)
1964 : !can_reverse | 4 - (test != 0). */
1965 11846 : else if (diff < 0 && STORE_FLAG_VALUE > 0)
1966 : {
1967 5926 : reversep = can_reverse;
1968 5926 : subtract_flag_p = !can_reverse;
1969 : /* If we need to subtract the flag and we have PLUS-immediate
1970 : A and B then it is unlikely to be beneficial to play tricks
1971 : here. */
1972 5926 : if (subtract_flag_p && common)
1973 : return false;
1974 : }
1975 : /* test ? 4 : 3
1976 : => 4 + (test != 0). */
1977 : else if (diff > 0 && STORE_FLAG_VALUE > 0)
1978 : reversep = false;
1979 : else
1980 : gcc_unreachable ();
1981 : }
1982 : /* Is this (cond) ? 2^n : 0? */
1983 559 : else if (ifalse == 0 && pow2p_hwi (itrue)
1984 9706 : && STORE_FLAG_VALUE == 1)
1985 : normalize = 1;
1986 : /* Is this (cond) ? 0 : 2^n? */
1987 386 : else if (itrue == 0 && pow2p_hwi (ifalse)
1988 9699 : && STORE_FLAG_VALUE == 1)
1989 : {
1990 : normalize = 1;
1991 : reversep = true;
1992 : }
1993 : /* Is this (cond) ? -1 : x? */
1994 : else if (itrue == -1
1995 : && STORE_FLAG_VALUE == -1)
1996 : normalize = -1;
1997 : /* Is this (cond) ? x : -1? */
1998 : else if (ifalse == -1
1999 : && STORE_FLAG_VALUE == -1)
2000 : {
2001 : normalize = -1;
2002 : reversep = true;
2003 : }
2004 : else
2005 : return false;
2006 :
2007 5926 : if (reversep)
2008 : {
2009 5927 : std::swap (itrue, ifalse);
2010 5927 : diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
2011 : }
2012 :
2013 11854 : start_sequence ();
2014 :
2015 : /* If we have x := test ? x + 3 : x + 4 then move the original
2016 : x out of the way while we store flags. */
2017 11854 : if (common && rtx_equal_p (common, if_info->x))
2018 : {
2019 3 : common = gen_reg_rtx (mode);
2020 3 : noce_emit_move_insn (common, if_info->x);
2021 : }
2022 :
2023 11854 : target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
2024 11854 : if (! target)
2025 : {
2026 4659 : end_sequence ();
2027 4659 : return false;
2028 : }
2029 :
2030 : /* if (test) x = 3; else x = 4;
2031 : => x = 3 + (test == 0); */
2032 7195 : if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
2033 : {
2034 : /* Add the common part now. This may allow combine to merge this
2035 : with the store flag operation earlier into some sort of conditional
2036 : increment/decrement if the target allows it. */
2037 7187 : if (common)
2038 27 : target = expand_simple_binop (mode, PLUS,
2039 : target, common,
2040 : target, 0, OPTAB_WIDEN);
2041 :
2042 : /* Always use ifalse here. It should have been swapped with itrue
2043 : when appropriate when reversep is true. */
2044 14374 : target = expand_simple_binop (mode, subtract_flag_p ? MINUS : PLUS,
2045 7187 : gen_int_mode (ifalse, mode), target,
2046 : if_info->x, 0, OPTAB_WIDEN);
2047 : }
2048 : /* Other cases are not beneficial when the original A and B are PLUS
2049 : expressions. */
2050 8 : else if (common)
2051 : {
2052 0 : end_sequence ();
2053 0 : return false;
2054 : }
2055 : /* if (test) x = 8; else x = 0;
2056 : => x = (test != 0) << 3; */
2057 8 : else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
2058 : {
2059 8 : target = expand_simple_binop (mode, ASHIFT,
2060 : target, GEN_INT (tmp), if_info->x, 0,
2061 : OPTAB_WIDEN);
2062 : }
2063 :
2064 : /* if (test) x = -1; else x = b;
2065 : => x = -(test != 0) | b; */
2066 0 : else if (itrue == -1)
2067 : {
2068 0 : target = expand_simple_binop (mode, IOR,
2069 0 : target, gen_int_mode (ifalse, mode),
2070 : if_info->x, 0, OPTAB_WIDEN);
2071 : }
2072 : else
2073 : {
2074 0 : end_sequence ();
2075 0 : return false;
2076 : }
2077 :
2078 7195 : if (! target)
2079 : {
2080 0 : end_sequence ();
2081 0 : return false;
2082 : }
2083 :
2084 7195 : if (target != if_info->x)
2085 0 : noce_emit_move_insn (if_info->x, target);
2086 :
2087 7195 : seq = end_ifcvt_sequence (if_info);
2088 7195 : return noce_commit_sequence (if_info, seq, true,
2089 7195 : "noce_try_store_flag_constants");
2090 : }
2091 :
2092 : return false;
2093 : }
2094 :
2095 : /* This is trying to capture cases like dest = cond ? a : -1.
2096 :
2097 : The basic idea is to use a store-flag insn to generate a -1/0
2098 : value (directly or indirectly), then IOR that with the other
2099 : input. */
2100 : static bool
2101 201477 : noce_try_store_flag_logical (noce_if_info *if_info)
2102 : {
2103 201477 : rtx a = if_info->a;
2104 201477 : rtx b = if_info->b;
2105 201477 : rtx dest = if_info->x;
2106 201477 : machine_mode mode = GET_MODE (dest);
2107 :
2108 201477 : if (STORE_FLAG_VALUE != -1 && STORE_FLAG_VALUE != 1)
2109 : return false;
2110 :
2111 260532 : if (!noce_simple_bbs (if_info))
2112 : return false;
2113 :
2114 182631 : bool swapped = STORE_FLAG_VALUE == -1;
2115 182631 : if (a == CONSTM1_RTX (GET_MODE (dest))
2116 1750 : && if_info->rev_cond)
2117 : {
2118 : std::swap (a, b);
2119 182631 : swapped = !swapped;
2120 : }
2121 :
2122 182631 : if (b != CONSTM1_RTX (GET_MODE (dest)))
2123 : return false;
2124 :
2125 : /* If the other arm is not a REG/SUBREG, then punt. This is primarily to
2126 : let the target handle the constant case, which it can likely do better.
2127 : It also means we don't have to worry about non terminal expressions. */
2128 2712 : if (!REG_P (a) && !SUBREG_P (a))
2129 : return false;
2130 :
2131 : /* At this point we've got dest = cond ? a : -1. Emit the store flag and
2132 : adjust its value (if necessary) to -1/0. */
2133 1131 : start_sequence ();
2134 1131 : rtx temp = gen_reg_rtx (mode);
2135 1131 : rtx target = noce_emit_store_flag (if_info, temp, !swapped, 0);
2136 1131 : if (!target)
2137 : {
2138 88 : end_sequence ();
2139 88 : return false;
2140 : }
2141 :
2142 1043 : if (STORE_FLAG_VALUE == 1)
2143 : {
2144 1043 : rtx x = gen_rtx_PLUS (mode, target, CONSTM1_RTX (mode));
2145 1043 : emit_move_insn (target, x);
2146 : }
2147 :
2148 : /* Now we've got -1/0 in TARGET. We can just IOR with A. */
2149 1043 : rtx x = gen_rtx_IOR (mode, target, a);
2150 1043 : emit_move_insn (dest, x);
2151 :
2152 : /* We've generated all the RTL, make sure it recognizes and is
2153 : profitable. */
2154 1043 : rtx_insn *seq = end_ifcvt_sequence (if_info);
2155 1043 : return noce_commit_sequence (if_info, seq, true,
2156 1043 : "noce_try_store_flag_logical");
2157 : }
2158 :
2159 : /* Convert "if (test) foo++" into "foo += (test != 0)", and
2160 : similarly for "foo--". */
2161 :
2162 : static bool
2163 158499 : noce_try_addcc (noce_if_info *if_info)
2164 : {
2165 158499 : rtx target;
2166 158499 : rtx_insn *seq;
2167 158499 : bool subtract;
2168 158499 : int normalize;
2169 :
2170 200949 : if (!noce_simple_bbs (if_info))
2171 : return false;
2172 :
2173 139653 : if (GET_CODE (if_info->a) == PLUS
2174 15013 : && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
2175 150905 : && noce_reversed_cond_code (if_info) != UNKNOWN)
2176 : {
2177 11252 : enum rtx_code code;
2178 11252 : rtx cond = noce_reversed_cond (if_info, &code);
2179 :
2180 : /* First try to use addcc pattern. */
2181 11252 : if (general_operand (XEXP (cond, 0), VOIDmode)
2182 11252 : && general_operand (XEXP (cond, 1), VOIDmode))
2183 : {
2184 10948 : start_sequence ();
2185 32844 : target = emit_conditional_add (if_info->x, code,
2186 : XEXP (cond, 0),
2187 : XEXP (cond, 1),
2188 : VOIDmode,
2189 : if_info->b,
2190 10948 : XEXP (if_info->a, 1),
2191 10948 : GET_MODE (if_info->x),
2192 10547 : (code == LTU || code == GEU
2193 21442 : || code == LEU || code == GTU));
2194 10948 : if (target)
2195 : {
2196 341 : if (target != if_info->x)
2197 0 : noce_emit_move_insn (if_info->x, target);
2198 :
2199 341 : seq = end_ifcvt_sequence (if_info);
2200 341 : return noce_commit_sequence (if_info, seq, true,
2201 1185 : "noce_try_addcc");
2202 : }
2203 10607 : end_sequence ();
2204 : }
2205 :
2206 : /* If that fails, construct conditional increment or decrement using
2207 : setcc. We're changing a branch and an increment to a comparison and
2208 : an ADD/SUB. */
2209 10911 : if (XEXP (if_info->a, 1) == const1_rtx
2210 9928 : || XEXP (if_info->a, 1) == constm1_rtx)
2211 : {
2212 1538 : start_sequence ();
2213 1538 : if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
2214 : subtract = false, normalize = 0;
2215 555 : else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
2216 : subtract = true, normalize = 0;
2217 : else
2218 0 : subtract = false, normalize = INTVAL (XEXP (if_info->a, 1));
2219 :
2220 :
2221 1538 : target = noce_emit_store_flag (if_info,
2222 1538 : gen_reg_rtx (GET_MODE (if_info->x)),
2223 : true, normalize);
2224 :
2225 1538 : if (target)
2226 1164 : target = expand_simple_binop (GET_MODE (if_info->x),
2227 : subtract ? MINUS : PLUS,
2228 : if_info->b, target, if_info->x,
2229 : 0, OPTAB_WIDEN);
2230 844 : if (target)
2231 : {
2232 844 : if (target != if_info->x)
2233 0 : noce_emit_move_insn (if_info->x, target);
2234 :
2235 844 : seq = end_ifcvt_sequence (if_info);
2236 844 : return noce_commit_sequence (if_info, seq, true,
2237 844 : "noce_try_addcc");
2238 : }
2239 694 : end_sequence ();
2240 : }
2241 : }
2242 :
2243 : return false;
2244 : }
2245 :
2246 : /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
2247 :
2248 : static bool
2249 157578 : noce_try_store_flag_mask (noce_if_info *if_info)
2250 : {
2251 157578 : rtx target;
2252 157578 : rtx_insn *seq;
2253 :
2254 199987 : if (!noce_simple_bbs (if_info))
2255 : return false;
2256 :
2257 : /* Match "if (test) x = 0;" and, with the arms the other way round,
2258 : "if (!test) x = 0;". */
2259 138732 : bool zero_a = (if_info->a == const0_rtx
2260 138732 : && (REG_P (if_info->b)
2261 1713 : || rtx_equal_p (if_info->b, if_info->x)));
2262 138732 : bool zero_b = (if_info->b == const0_rtx
2263 138732 : && (REG_P (if_info->a)
2264 12702 : || rtx_equal_p (if_info->a, if_info->x)));
2265 :
2266 138730 : if (zero_a || zero_b)
2267 : {
2268 1149 : bool reversep = !zero_a;
2269 1149 : start_sequence ();
2270 1149 : target = noce_emit_store_flag (if_info,
2271 1149 : gen_reg_rtx (GET_MODE (if_info->x)),
2272 : reversep, -1);
2273 1149 : if (target)
2274 299 : target = expand_simple_binop (GET_MODE (if_info->x), AND,
2275 : reversep ? if_info->a : if_info->b,
2276 : target, if_info->x, 0,
2277 : OPTAB_WIDEN);
2278 :
2279 299 : if (target)
2280 : {
2281 299 : if (target != if_info->x)
2282 0 : noce_emit_move_insn (if_info->x, target);
2283 :
2284 299 : seq = end_ifcvt_sequence (if_info);
2285 299 : return noce_commit_sequence (if_info, seq, true,
2286 299 : "noce_try_store_flag_mask");
2287 : }
2288 :
2289 850 : end_sequence ();
2290 : }
2291 :
2292 : return false;
2293 : }
2294 :
2295 : /* Emit a conditional move selecting VTRUE or VFALSE into X, and return the
2296 : destination it landed in, or NULL_RTX on failure. CODE, CMP_A and CMP_B
2297 : give the canonicalized comparison. CC_CMP and REV_CC_CMP, when given, are
2298 : the non-canonicalized condition and its reverse, which let a target emit
2299 : the move without materializing its own compare. */
2300 :
2301 : static rtx
2302 528620 : noce_emit_cmove (noce_if_info *if_info, rtx x, enum rtx_code code,
2303 : rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue,
2304 : rtx cc_cmp = NULL, rtx rev_cc_cmp = NULL)
2305 : {
2306 528620 : rtx target;
2307 528620 : bool unsignedp;
2308 :
2309 : /* If earliest == jump, try to build the cmove insn directly.
2310 : This is helpful when combine has created some complex condition
2311 : (like for alpha's cmovlbs) that we can't hope to regenerate
2312 : through the normal interface. */
2313 :
2314 528620 : if (if_info->cond_earliest == if_info->jump)
2315 : {
2316 5043 : rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
2317 5043 : rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
2318 : cond, vtrue, vfalse);
2319 5043 : rtx set = gen_rtx_SET (x, if_then_else);
2320 :
2321 5043 : start_sequence ();
2322 5043 : rtx_insn *insn = emit_insn (set);
2323 :
2324 5043 : if (recog_memoized (insn) >= 0)
2325 : {
2326 1559 : rtx_insn *seq = end_sequence ();
2327 1559 : emit_insn (seq);
2328 :
2329 1559 : return x;
2330 : }
2331 :
2332 3484 : end_sequence ();
2333 : }
2334 :
2335 1054122 : unsignedp = (code == LTU || code == GEU
2336 527061 : || code == LEU || code == GTU);
2337 :
2338 527061 : if (cc_cmp != NULL_RTX && rev_cc_cmp != NULL_RTX)
2339 108866 : target = emit_conditional_move (x, cc_cmp, rev_cc_cmp,
2340 108866 : vtrue, vfalse, GET_MODE (x));
2341 : else
2342 : {
2343 : /* Don't even try if the comparison operands are weird
2344 : except that the target supports cbranchcc4. */
2345 418195 : if (! general_operand (cmp_a, GET_MODE (cmp_a))
2346 418195 : || ! general_operand (cmp_b, GET_MODE (cmp_b)))
2347 : {
2348 24999 : if (!have_cbranchcc4
2349 24999 : || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
2350 3055 : || cmp_b != const0_rtx)
2351 : return NULL_RTX;
2352 : }
2353 :
2354 396251 : target = emit_conditional_move (x, { code, cmp_a, cmp_b, VOIDmode },
2355 396251 : vtrue, vfalse, GET_MODE (x),
2356 : unsignedp);
2357 : }
2358 :
2359 505117 : if (target)
2360 : return target;
2361 :
2362 : /* We might be faced with a situation like:
2363 :
2364 : x = (reg:M TARGET)
2365 : vtrue = (subreg:M (reg:N VTRUE) BYTE)
2366 : vfalse = (subreg:M (reg:N VFALSE) BYTE)
2367 :
2368 : We can't do a conditional move in mode M, but it's possible that we
2369 : could do a conditional move in mode N instead and take a subreg of
2370 : the result.
2371 :
2372 : If we can't create new pseudos, though, don't bother. */
2373 45065 : if (reload_completed)
2374 : return NULL_RTX;
2375 :
2376 45065 : if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
2377 : {
2378 22 : rtx reg_vtrue = SUBREG_REG (vtrue);
2379 22 : rtx reg_vfalse = SUBREG_REG (vfalse);
2380 22 : poly_uint64 byte_vtrue = SUBREG_BYTE (vtrue);
2381 22 : poly_uint64 byte_vfalse = SUBREG_BYTE (vfalse);
2382 22 : rtx promoted_target;
2383 :
2384 22 : if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
2385 22 : || maybe_ne (byte_vtrue, byte_vfalse)
2386 22 : || (SUBREG_PROMOTED_VAR_P (vtrue)
2387 22 : != SUBREG_PROMOTED_VAR_P (vfalse))
2388 22 : || (SUBREG_PROMOTED_GET (vtrue)
2389 22 : != SUBREG_PROMOTED_GET (vfalse)))
2390 : return NULL_RTX;
2391 :
2392 22 : promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
2393 :
2394 44 : target = emit_conditional_move (promoted_target,
2395 : { code, cmp_a, cmp_b, VOIDmode },
2396 : reg_vtrue, reg_vfalse,
2397 22 : GET_MODE (reg_vtrue), unsignedp);
2398 : /* Nope, couldn't do it in that mode either. */
2399 22 : if (!target)
2400 : return NULL_RTX;
2401 :
2402 6 : target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
2403 6 : SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
2404 6 : SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
2405 6 : emit_move_insn (x, target);
2406 6 : return x;
2407 : }
2408 : else
2409 : return NULL_RTX;
2410 : }
2411 :
2412 : /* Return true if X is a constant or register operand suitable for simple
2413 : conditional-move handling. */
2414 :
2415 : static bool
2416 2559794 : noce_simple_cmove_operand_p (rtx x)
2417 : {
2418 2559794 : return CONSTANT_P (x) || register_operand (x, VOIDmode);
2419 : }
2420 :
2421 : /* Try only simple constants and registers here. More complex cases are
2422 : handled in noce_try_cmove_arith, which the dispatch table reaches after
2423 : the store-flag matchers have had a go. */
2424 :
2425 : static bool
2426 201477 : noce_try_cmove (noce_if_info *if_info)
2427 : {
2428 201477 : enum rtx_code code;
2429 201477 : rtx target;
2430 201477 : rtx_insn *seq;
2431 :
2432 260532 : if (!noce_simple_bbs (if_info))
2433 : return false;
2434 :
2435 182631 : if (noce_simple_cmove_operand_p (if_info->a)
2436 182631 : && noce_simple_cmove_operand_p (if_info->b))
2437 : {
2438 83539 : start_sequence ();
2439 :
2440 83539 : code = GET_CODE (if_info->cond);
2441 83539 : target = noce_emit_cmove (if_info, if_info->x, code,
2442 : XEXP (if_info->cond, 0),
2443 : XEXP (if_info->cond, 1),
2444 : if_info->a, if_info->b);
2445 :
2446 83539 : if (target)
2447 : {
2448 57166 : if (target != if_info->x)
2449 0 : noce_emit_move_insn (if_info->x, target);
2450 :
2451 57166 : seq = end_ifcvt_sequence (if_info);
2452 57166 : return noce_commit_sequence (if_info, seq, true, "noce_try_cmove");
2453 : }
2454 : /* If both a and b are constants try a last-ditch transformation:
2455 : if (test) x = a; else x = b;
2456 : => x = (-(test != 0) & (b - a)) + a;
2457 : Try this only if the target-specific expansion above has failed.
2458 : The target-specific expander may want to generate sequences that
2459 : we don't know about, so give them a chance before trying this
2460 : approach. */
2461 26373 : else if (!targetm.have_conditional_execution ()
2462 26373 : && CONST_INT_P (if_info->a) && CONST_INT_P (if_info->b))
2463 : {
2464 5111 : machine_mode mode = GET_MODE (if_info->x);
2465 5111 : HOST_WIDE_INT ifalse = INTVAL (if_info->a);
2466 5111 : HOST_WIDE_INT itrue = INTVAL (if_info->b);
2467 5111 : rtx target = noce_emit_store_flag (if_info, if_info->x, false, -1);
2468 5111 : if (!target)
2469 : {
2470 5051 : end_sequence ();
2471 5051 : return false;
2472 : }
2473 :
2474 60 : HOST_WIDE_INT diff;
2475 60 : if (!noce_representable_diff_p (ifalse, itrue, mode, &diff))
2476 : {
2477 21 : end_sequence ();
2478 21 : return false;
2479 : }
2480 :
2481 39 : target = expand_simple_binop (mode, AND,
2482 39 : target, gen_int_mode (diff, mode),
2483 : if_info->x, 0, OPTAB_WIDEN);
2484 39 : if (target)
2485 39 : target = expand_simple_binop (mode, PLUS,
2486 39 : target, gen_int_mode (ifalse, mode),
2487 : if_info->x, 0, OPTAB_WIDEN);
2488 39 : if (target)
2489 : {
2490 39 : if (target != if_info->x)
2491 0 : noce_emit_move_insn (if_info->x, target);
2492 :
2493 39 : seq = end_ifcvt_sequence (if_info);
2494 39 : return noce_commit_sequence (if_info, seq, true,
2495 39 : "noce_try_cmove");
2496 : }
2497 : else
2498 : {
2499 0 : end_sequence ();
2500 0 : return false;
2501 : }
2502 : }
2503 : else
2504 21262 : end_sequence ();
2505 : }
2506 :
2507 : return false;
2508 : }
2509 :
2510 : /* Return true if X contains a conditional code mode rtx. */
2511 :
2512 : static bool
2513 2910833 : contains_ccmode_rtx_p (rtx x)
2514 : {
2515 2910833 : subrtx_iterator::array_type array;
2516 7436849 : FOR_EACH_SUBRTX (iter, array, x, ALL)
2517 4573272 : if (GET_MODE_CLASS (GET_MODE (*iter)) == MODE_CC)
2518 47256 : return true;
2519 :
2520 2863577 : return false;
2521 2910833 : }
2522 :
2523 : /* Helper for bb_valid_for_noce_process_p. Validate that
2524 : the rtx insn INSN is a single set that does not set
2525 : the conditional register CC and is in general valid for
2526 : if-conversion. */
2527 :
2528 : static bool
2529 3637524 : insn_valid_noce_process_p (rtx_insn *insn, rtx cc)
2530 : {
2531 3637524 : if (!insn
2532 3637182 : || !NONJUMP_INSN_P (insn)
2533 6580838 : || (cc && set_of (cc, insn)))
2534 : return false;
2535 :
2536 2931364 : rtx sset = single_set (insn);
2537 :
2538 : /* Currently support only simple single sets in test_bb. */
2539 2931364 : if (!sset
2540 2925023 : || !noce_operand_ok (SET_DEST (sset))
2541 2910833 : || contains_ccmode_rtx_p (SET_DEST (sset))
2542 5794941 : || !noce_operand_ok (SET_SRC (sset)))
2543 135447 : return false;
2544 :
2545 : return true;
2546 : }
2547 :
2548 :
2549 : /* Return true iff the registers that the insns in BB_A set do not get
2550 : used in BB_B. If TO_RENAME is non-NULL then it is a location that will be
2551 : renamed later by the caller and so conflicts on it should be ignored
2552 : in this function. */
2553 :
2554 : static bool
2555 55886 : bbs_ok_for_cmove_arith (basic_block bb_a, basic_block bb_b, rtx to_rename)
2556 : {
2557 55886 : rtx_insn *a_insn;
2558 55886 : auto_bitmap bba_sets (®_obstack);
2559 :
2560 55886 : df_ref def;
2561 55886 : df_ref use;
2562 :
2563 301898 : FOR_BB_INSNS (bb_a, a_insn)
2564 : {
2565 246012 : if (!active_insn_p (a_insn))
2566 167538 : continue;
2567 :
2568 78474 : rtx sset_a = single_set (a_insn);
2569 :
2570 78474 : if (!sset_a)
2571 : return false;
2572 : /* Record all registers that BB_A sets. */
2573 190478 : FOR_EACH_INSN_DEF (def, a_insn)
2574 112004 : if (!(to_rename && DF_REF_REG (def) == to_rename))
2575 57365 : bitmap_set_bit (bba_sets, DF_REF_REGNO (def));
2576 : }
2577 :
2578 55886 : rtx_insn *b_insn;
2579 :
2580 301928 : FOR_BB_INSNS (bb_b, b_insn)
2581 : {
2582 246146 : if (!active_insn_p (b_insn))
2583 167691 : continue;
2584 :
2585 78455 : rtx sset_b = single_set (b_insn);
2586 :
2587 78455 : if (!sset_b)
2588 : return false;
2589 :
2590 : /* Make sure this is a REG and not some instance
2591 : of ZERO_EXTRACT or non-paradoxical SUBREG or other dangerous stuff.
2592 : If we have a memory destination then we have a pair of simple
2593 : basic blocks performing an operation of the form [addr] = c ? a : b.
2594 : bb_valid_for_noce_process_p will have ensured that these are
2595 : the only stores present. In that case [addr] should be the location
2596 : to be renamed. Assert that the callers set this up properly. */
2597 78455 : if (MEM_P (SET_DEST (sset_b)))
2598 1238 : gcc_assert (rtx_equal_p (SET_DEST (sset_b), to_rename));
2599 77217 : else if (!REG_P (SET_DEST (sset_b))
2600 133056 : && !paradoxical_subreg_p (SET_DEST (sset_b)))
2601 : return false;
2602 :
2603 : /* If the insn uses a reg set in BB_A return false. */
2604 151789 : FOR_EACH_INSN_USE (use, b_insn)
2605 73438 : if (bitmap_bit_p (bba_sets, DF_REF_REGNO (use)))
2606 : return false;
2607 :
2608 : }
2609 :
2610 : return true;
2611 55886 : }
2612 :
2613 : /* Emit copies of all the active instructions in BB except the last.
2614 : This is a helper for noce_try_cmove_arith. */
2615 :
2616 : static void
2617 14542 : noce_emit_all_but_last (basic_block bb)
2618 : {
2619 14542 : rtx_insn *last = last_active_insn (bb, false);
2620 14542 : rtx_insn *insn;
2621 153581 : FOR_BB_INSNS (bb, insn)
2622 : {
2623 124497 : if (insn != last && active_insn_p (insn))
2624 : {
2625 36816 : rtx_insn *to_emit = as_a <rtx_insn *> (copy_rtx (insn));
2626 :
2627 36816 : emit_insn (PATTERN (to_emit));
2628 : }
2629 : }
2630 14542 : }
2631 :
2632 : /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
2633 : the resulting insn or NULL if it's not a valid insn. */
2634 :
2635 : static rtx_insn *
2636 145813 : noce_emit_insn (rtx to_emit)
2637 : {
2638 145813 : gcc_assert (to_emit);
2639 145813 : rtx_insn *insn = emit_insn (to_emit);
2640 :
2641 145813 : if (recog_memoized (insn) < 0)
2642 201 : return NULL;
2643 :
2644 : return insn;
2645 : }
2646 :
2647 : /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
2648 : and including the penultimate one in BB if it is not simple
2649 : (as indicated by SIMPLE). Then emit LAST_INSN as the last
2650 : insn in the block. The reason for that is that LAST_INSN may
2651 : have been modified by the preparation in noce_try_cmove_arith. */
2652 :
2653 : static bool
2654 148328 : noce_emit_bb (rtx last_insn, basic_block bb, bool simple)
2655 : {
2656 148328 : if (bb && !simple)
2657 14542 : noce_emit_all_but_last (bb);
2658 :
2659 148328 : if (last_insn && !noce_emit_insn (last_insn))
2660 201 : return false;
2661 :
2662 : return true;
2663 : }
2664 :
2665 : /* Try more complex cases involving conditional_move. */
2666 :
2667 : static bool
2668 147406 : noce_try_cmove_arith (noce_if_info *if_info)
2669 : {
2670 147406 : rtx a = if_info->a;
2671 147406 : rtx b = if_info->b;
2672 147406 : rtx x = if_info->x;
2673 147406 : rtx orig_a, orig_b;
2674 147406 : rtx_insn *insn_a, *insn_b;
2675 147406 : bool a_simple = if_info->then_simple;
2676 147406 : bool b_simple = if_info->else_simple;
2677 147406 : basic_block then_bb = if_info->then_bb;
2678 147406 : basic_block else_bb = if_info->else_bb;
2679 147406 : rtx target;
2680 147406 : bool is_mem = false;
2681 147406 : enum rtx_code code;
2682 147406 : rtx cond = if_info->cond;
2683 147406 : rtx_insn *ifcvt_seq;
2684 :
2685 : /* A conditional move from two memory sources is equivalent to a
2686 : conditional on their addresses followed by a load. Don't do this
2687 : early because it'll screw alias analysis. Note that we've
2688 : already checked for no side effects. */
2689 147406 : if (cse_not_expected
2690 47939 : && MEM_P (a) && MEM_P (b)
2691 150132 : && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b))
2692 : {
2693 2725 : machine_mode address_mode = get_address_mode (a);
2694 :
2695 2725 : a = XEXP (a, 0);
2696 2725 : b = XEXP (b, 0);
2697 2725 : x = gen_reg_rtx (address_mode);
2698 2725 : is_mem = true;
2699 : }
2700 :
2701 : /* ??? We could handle this if we knew that a load from A or B could
2702 : not trap or fault. This is also true if we've already loaded
2703 : from the address along the path from ENTRY. */
2704 144681 : else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
2705 : return false;
2706 :
2707 : /* if (test) x = a + b; else x = c - d;
2708 : => y = a + b;
2709 : x = c - d;
2710 : if (test)
2711 : x = y;
2712 : */
2713 :
2714 94346 : code = GET_CODE (cond);
2715 94346 : insn_a = if_info->insn_a;
2716 94346 : insn_b = if_info->insn_b;
2717 :
2718 94346 : machine_mode x_mode = GET_MODE (x);
2719 :
2720 94346 : if (!can_conditionally_move_p (x_mode))
2721 : return false;
2722 :
2723 : /* Possibly rearrange operands to make things come out more natural. */
2724 74268 : if (noce_reversed_cond_code (if_info) != UNKNOWN)
2725 : {
2726 74268 : bool reversep = false;
2727 74268 : if (rtx_equal_p (b, x))
2728 : reversep = true;
2729 47069 : else if (general_operand (b, GET_MODE (b)))
2730 : reversep = true;
2731 :
2732 : if (reversep)
2733 : {
2734 66238 : cond = noce_reversed_cond (if_info, &code);
2735 66238 : std::swap (a, b);
2736 66238 : std::swap (insn_a, insn_b);
2737 66238 : std::swap (a_simple, b_simple);
2738 66238 : std::swap (then_bb, else_bb);
2739 : }
2740 : }
2741 :
2742 28795 : if (then_bb && else_bb
2743 102263 : && (!bbs_ok_for_cmove_arith (then_bb, else_bb, if_info->orig_x)
2744 27891 : || !bbs_ok_for_cmove_arith (else_bb, then_bb, if_info->orig_x)))
2745 : return false;
2746 :
2747 74164 : start_sequence ();
2748 :
2749 : /* If one of the blocks is empty then the corresponding B or A value
2750 : came from the test block. The non-empty complex block that we will
2751 : emit might clobber the register used by B or A, so move it to a pseudo
2752 : first. */
2753 :
2754 74164 : rtx tmp_a = NULL_RTX;
2755 74164 : rtx tmp_b = NULL_RTX;
2756 :
2757 74164 : if (b_simple || !else_bb)
2758 61700 : tmp_b = gen_reg_rtx (x_mode);
2759 :
2760 74164 : if (a_simple || !then_bb)
2761 72086 : tmp_a = gen_reg_rtx (x_mode);
2762 :
2763 74164 : orig_a = a;
2764 74164 : orig_b = b;
2765 :
2766 74164 : rtx emit_a = NULL_RTX;
2767 74164 : rtx emit_b = NULL_RTX;
2768 74164 : rtx_insn *tmp_insn = NULL;
2769 74164 : bool modified_in_a = false;
2770 74164 : bool modified_in_b = false;
2771 : /* If either operand is complex, load it into a register first.
2772 : The best way to do this is to copy the original insn. In this
2773 : way we preserve any clobbers etc that the insn may have had.
2774 : This is of course not possible in the IS_MEM case. */
2775 :
2776 74164 : if (! general_operand (a, GET_MODE (a)) || tmp_a)
2777 : {
2778 :
2779 73429 : if (is_mem)
2780 : {
2781 2724 : rtx reg = gen_reg_rtx (GET_MODE (a));
2782 2724 : emit_a = gen_rtx_SET (reg, a);
2783 : }
2784 : else
2785 : {
2786 70705 : if (insn_a)
2787 : {
2788 43445 : a = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2789 :
2790 43445 : rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
2791 43445 : rtx set = single_set (copy_of_a);
2792 43445 : SET_DEST (set) = a;
2793 :
2794 43445 : emit_a = PATTERN (copy_of_a);
2795 : }
2796 : else
2797 : {
2798 27260 : rtx tmp_reg = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2799 27260 : emit_a = gen_rtx_SET (tmp_reg, a);
2800 27260 : a = tmp_reg;
2801 : }
2802 : }
2803 : }
2804 :
2805 74164 : if (! general_operand (b, GET_MODE (b)) || tmp_b)
2806 : {
2807 72384 : if (is_mem)
2808 : {
2809 2724 : rtx reg = gen_reg_rtx (GET_MODE (b));
2810 2724 : emit_b = gen_rtx_SET (reg, b);
2811 : }
2812 : else
2813 : {
2814 69660 : if (insn_b)
2815 : {
2816 69468 : b = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2817 69468 : rtx_insn *copy_of_b = as_a <rtx_insn *> (copy_rtx (insn_b));
2818 69468 : rtx set = single_set (copy_of_b);
2819 :
2820 69468 : SET_DEST (set) = b;
2821 69468 : emit_b = PATTERN (copy_of_b);
2822 : }
2823 : else
2824 : {
2825 192 : rtx tmp_reg = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2826 192 : emit_b = gen_rtx_SET (tmp_reg, b);
2827 192 : b = tmp_reg;
2828 : }
2829 : }
2830 : }
2831 :
2832 74164 : modified_in_a = emit_a != NULL_RTX && modified_in_p (orig_b, emit_a);
2833 74164 : if (tmp_b && then_bb)
2834 : {
2835 78838 : FOR_BB_INSNS (then_bb, tmp_insn)
2836 : /* Don't check inside insn_a. We will have changed it to emit_a
2837 : with a destination that doesn't conflict. */
2838 59440 : if (!(insn_a && tmp_insn == insn_a)
2839 99482 : && modified_in_p (orig_b, tmp_insn))
2840 : {
2841 : modified_in_a = true;
2842 : break;
2843 : }
2844 :
2845 : }
2846 :
2847 74164 : modified_in_b = emit_b != NULL_RTX && modified_in_p (orig_a, emit_b);
2848 74164 : if (tmp_a && else_bb)
2849 : {
2850 337828 : FOR_BB_INSNS (else_bb, tmp_insn)
2851 : /* Don't check inside insn_b. We will have changed it to emit_b
2852 : with a destination that doesn't conflict. */
2853 266521 : if (!(insn_b && tmp_insn == insn_b)
2854 461735 : && modified_in_p (orig_a, tmp_insn))
2855 : {
2856 : modified_in_b = true;
2857 : break;
2858 : }
2859 : }
2860 :
2861 : /* If insn to set up A clobbers any registers B depends on, try to
2862 : swap insn that sets up A with the one that sets up B. If even
2863 : that doesn't help, punt. */
2864 74164 : if (modified_in_a && !modified_in_b)
2865 : {
2866 0 : if (!noce_emit_bb (emit_b, else_bb, b_simple))
2867 0 : goto end_seq_and_fail;
2868 :
2869 0 : if (!noce_emit_bb (emit_a, then_bb, a_simple))
2870 0 : goto end_seq_and_fail;
2871 : }
2872 74164 : else if (!modified_in_a)
2873 : {
2874 74164 : if (!noce_emit_bb (emit_a, then_bb, a_simple))
2875 0 : goto end_seq_and_fail;
2876 :
2877 74164 : if (!noce_emit_bb (emit_b, else_bb, b_simple))
2878 201 : goto end_seq_and_fail;
2879 : }
2880 : else
2881 0 : goto end_seq_and_fail;
2882 :
2883 73963 : target = noce_emit_cmove (if_info, x, code, XEXP (cond, 0), XEXP (cond, 1),
2884 : a, b);
2885 :
2886 73963 : if (! target)
2887 18421 : goto end_seq_and_fail;
2888 :
2889 : /* If we're handling a memory for above, emit the load now. */
2890 55542 : if (is_mem)
2891 : {
2892 2326 : rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
2893 :
2894 : /* Copy over flags as appropriate. */
2895 2326 : if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
2896 0 : MEM_VOLATILE_P (mem) = 1;
2897 2326 : if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
2898 2167 : set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
2899 4652 : set_mem_align (mem,
2900 2326 : MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
2901 :
2902 2326 : gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
2903 2326 : set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
2904 :
2905 2326 : noce_emit_move_insn (if_info->x, mem);
2906 : }
2907 53216 : else if (target != x)
2908 0 : noce_emit_move_insn (x, target);
2909 :
2910 55542 : ifcvt_seq = end_ifcvt_sequence (if_info);
2911 55542 : return noce_commit_sequence (if_info, ifcvt_seq, true,
2912 55542 : "noce_try_cmove_arith");
2913 :
2914 18622 : end_seq_and_fail:
2915 18622 : end_sequence ();
2916 18622 : return false;
2917 : }
2918 :
2919 : /* For most cases, the simplified condition we found is the best
2920 : choice, but this is not the case for the min/max/abs transforms.
2921 : For these we wish to know that it is A or B in the condition. */
2922 :
2923 : static rtx
2924 160865 : noce_get_alt_condition (noce_if_info *if_info, rtx target,
2925 : rtx_insn **earliest)
2926 : {
2927 160865 : rtx cond, set;
2928 160865 : rtx_insn *insn;
2929 160865 : bool reverse;
2930 :
2931 : /* If target is already mentioned in the known condition, return it. */
2932 160865 : if (reg_mentioned_p (target, if_info->cond))
2933 : {
2934 8582 : *earliest = if_info->cond_earliest;
2935 8582 : return if_info->cond;
2936 : }
2937 :
2938 152283 : set = pc_set (if_info->jump);
2939 152283 : cond = XEXP (SET_SRC (set), 0);
2940 152283 : reverse
2941 304566 : = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2942 152283 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
2943 152283 : if (if_info->then_else_reversed)
2944 23897 : reverse = !reverse;
2945 :
2946 : /* If we're looking for a constant, try to make the conditional
2947 : have that constant in it. There are two reasons why it may
2948 : not have the constant we want:
2949 :
2950 : 1. GCC may have needed to put the constant in a register, because
2951 : the target can't compare directly against that constant. For
2952 : this case, we look for a SET immediately before the comparison
2953 : that puts a constant in that register.
2954 :
2955 : 2. GCC may have canonicalized the conditional, for example
2956 : replacing "if x < 4" with "if x <= 3". We can undo that (or
2957 : make equivalent types of changes) to get the constants we need
2958 : if they're off by one in the right direction. */
2959 :
2960 152283 : if (CONST_INT_P (target))
2961 : {
2962 46369 : enum rtx_code code = GET_CODE (if_info->cond);
2963 46369 : rtx op_a = XEXP (if_info->cond, 0);
2964 46369 : rtx op_b = XEXP (if_info->cond, 1);
2965 46369 : rtx_insn *prev_insn;
2966 :
2967 : /* First, look to see if we put a constant in a register. */
2968 46369 : prev_insn = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2969 46369 : if (prev_insn
2970 46258 : && BLOCK_FOR_INSN (prev_insn)
2971 46258 : == BLOCK_FOR_INSN (if_info->cond_earliest)
2972 36656 : && INSN_P (prev_insn)
2973 78842 : && GET_CODE (PATTERN (prev_insn)) == SET)
2974 : {
2975 24775 : rtx src = find_reg_equal_equiv_note (prev_insn);
2976 24775 : if (!src)
2977 24614 : src = SET_SRC (PATTERN (prev_insn));
2978 24775 : if (CONST_INT_P (src))
2979 : {
2980 14638 : if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
2981 : op_a = src;
2982 14320 : else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
2983 1073 : op_b = src;
2984 :
2985 14638 : if (CONST_INT_P (op_a))
2986 : {
2987 318 : std::swap (op_a, op_b);
2988 318 : code = swap_condition (code);
2989 : }
2990 : }
2991 : }
2992 :
2993 : /* Now, look to see if we can get the right constant by
2994 : adjusting the conditional. */
2995 46369 : if (CONST_INT_P (op_b))
2996 : {
2997 24150 : HOST_WIDE_INT desired_val = INTVAL (target);
2998 24150 : HOST_WIDE_INT actual_val = INTVAL (op_b);
2999 :
3000 24150 : switch (code)
3001 : {
3002 1198 : case LT:
3003 1198 : if (desired_val != HOST_WIDE_INT_MAX
3004 1183 : && actual_val == desired_val + 1)
3005 : {
3006 174 : code = LE;
3007 174 : op_b = GEN_INT (desired_val);
3008 : }
3009 : break;
3010 4 : case LE:
3011 4 : if (desired_val != HOST_WIDE_INT_MIN
3012 4 : && actual_val == desired_val - 1)
3013 : {
3014 1 : code = LT;
3015 1 : op_b = GEN_INT (desired_val);
3016 : }
3017 : break;
3018 1712 : case GT:
3019 1712 : if (desired_val != HOST_WIDE_INT_MIN
3020 1712 : && actual_val == desired_val - 1)
3021 : {
3022 245 : code = GE;
3023 245 : op_b = GEN_INT (desired_val);
3024 : }
3025 : break;
3026 40 : case GE:
3027 40 : if (desired_val != HOST_WIDE_INT_MAX
3028 25 : && actual_val == desired_val + 1)
3029 : {
3030 3 : code = GT;
3031 3 : op_b = GEN_INT (desired_val);
3032 : }
3033 : break;
3034 : default:
3035 : break;
3036 : }
3037 : }
3038 :
3039 : /* If we made any changes, generate a new conditional that is
3040 : equivalent to what we started with, but has the right
3041 : constants in it. */
3042 46369 : if (code != GET_CODE (if_info->cond)
3043 45629 : || op_a != XEXP (if_info->cond, 0)
3044 45628 : || op_b != XEXP (if_info->cond, 1))
3045 : {
3046 1814 : cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
3047 1814 : *earliest = if_info->cond_earliest;
3048 1814 : return cond;
3049 : }
3050 : }
3051 :
3052 150469 : cond = canonicalize_condition (if_info->jump, cond, reverse,
3053 : earliest, target, have_cbranchcc4, true);
3054 150469 : if (! cond || ! reg_mentioned_p (target, cond))
3055 : return NULL;
3056 :
3057 : /* We almost certainly searched back to a different place.
3058 : Need to re-verify correct lifetimes. */
3059 :
3060 : /* X may not be mentioned in the range (cond_earliest, jump]. */
3061 0 : for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
3062 0 : if (NONDEBUG_INSN_P (insn)
3063 0 : && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
3064 : return NULL;
3065 :
3066 : /* A and B may not be modified in the range [cond_earliest, jump). */
3067 0 : for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
3068 0 : if (INSN_P (insn)
3069 0 : && (modified_in_p (if_info->a, insn)
3070 0 : || modified_in_p (if_info->b, insn)))
3071 : return NULL;
3072 :
3073 : return cond;
3074 : }
3075 :
3076 : /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
3077 :
3078 : static bool
3079 210349 : noce_try_minmax (noce_if_info *if_info)
3080 : {
3081 210349 : rtx cond, target;
3082 210349 : rtx_insn *earliest, *seq;
3083 210349 : enum rtx_code code, op;
3084 210349 : bool unsignedp;
3085 :
3086 272777 : if (!noce_simple_bbs (if_info))
3087 : return false;
3088 :
3089 : /* ??? Reject modes with NaNs or signed zeros since we don't know how
3090 : they will be resolved with an SMIN/SMAX. It wouldn't be too hard
3091 : to get the target to tell us... */
3092 191503 : if (HONOR_SIGNED_ZEROS (if_info->x)
3093 191503 : || HONOR_NANS (if_info->x))
3094 : return false;
3095 :
3096 159920 : cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
3097 159920 : if (!cond)
3098 : return false;
3099 :
3100 : /* Verify the condition is of the form we expect, and canonicalize
3101 : the comparison code. */
3102 10371 : code = GET_CODE (cond);
3103 10371 : if (rtx_equal_p (XEXP (cond, 0), if_info->a))
3104 : {
3105 2733 : if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
3106 : return false;
3107 : }
3108 7638 : else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
3109 : {
3110 4865 : if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
3111 : return false;
3112 49 : code = swap_condition (code);
3113 : }
3114 : else
3115 : return false;
3116 :
3117 : /* Determine what sort of operation this is. Note that the code is for
3118 : a taken branch, so the code->operation mapping appears backwards. */
3119 64 : switch (code)
3120 : {
3121 : case LT:
3122 : case LE:
3123 : case UNLT:
3124 : case UNLE:
3125 : op = SMAX;
3126 : unsignedp = false;
3127 : break;
3128 19 : case GT:
3129 19 : case GE:
3130 19 : case UNGT:
3131 19 : case UNGE:
3132 19 : op = SMIN;
3133 19 : unsignedp = false;
3134 19 : break;
3135 3 : case LTU:
3136 3 : case LEU:
3137 3 : op = UMAX;
3138 3 : unsignedp = true;
3139 3 : break;
3140 20 : case GTU:
3141 20 : case GEU:
3142 20 : op = UMIN;
3143 20 : unsignedp = true;
3144 20 : break;
3145 : default:
3146 : return false;
3147 : }
3148 :
3149 64 : start_sequence ();
3150 :
3151 64 : target = expand_simple_binop (GET_MODE (if_info->x), op,
3152 : if_info->a, if_info->b,
3153 : if_info->x, unsignedp, OPTAB_WIDEN);
3154 64 : if (! target)
3155 : {
3156 0 : end_sequence ();
3157 0 : return false;
3158 : }
3159 64 : if (target != if_info->x)
3160 0 : noce_emit_move_insn (if_info->x, target);
3161 :
3162 64 : seq = end_ifcvt_sequence (if_info);
3163 64 : if (!seq)
3164 : return false;
3165 :
3166 64 : emit_insn_before_setloc (seq, if_info->jump, if_info->insn_a->location ());
3167 64 : if_info->cond = cond;
3168 64 : if_info->cond_earliest = earliest;
3169 64 : if_info->rev_cond = NULL_RTX;
3170 64 : if_info->transform_name = "noce_try_minmax";
3171 :
3172 64 : return true;
3173 : }
3174 :
3175 : /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
3176 : "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
3177 : etc. */
3178 :
3179 : static bool
3180 210285 : noce_try_abs (noce_if_info *if_info)
3181 : {
3182 210285 : rtx cond, target, a, b, c;
3183 210285 : rtx_insn *earliest, *seq;
3184 210285 : bool negate;
3185 210285 : bool one_cmpl = false;
3186 :
3187 272708 : if (!noce_simple_bbs (if_info))
3188 : return false;
3189 :
3190 : /* Reject modes with signed zeros or NaNs. */
3191 191439 : if (HONOR_SIGNED_ZEROS (if_info->x)
3192 191439 : || HONOR_NANS (if_info->x))
3193 : return false;
3194 :
3195 : /* Recognize A and B as constituting an ABS or NABS. The canonical
3196 : form is a branch around the negation, taken when the object is the
3197 : first operand of a comparison against 0 that evaluates to true. */
3198 159856 : a = if_info->a;
3199 159856 : b = if_info->b;
3200 159856 : if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
3201 : negate = false;
3202 158941 : else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
3203 : {
3204 : std::swap (a, b);
3205 : negate = true;
3206 : }
3207 158911 : else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
3208 : {
3209 : negate = false;
3210 : one_cmpl = true;
3211 : }
3212 158911 : else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
3213 : {
3214 : std::swap (a, b);
3215 : negate = true;
3216 : one_cmpl = true;
3217 : }
3218 : else
3219 : return false;
3220 :
3221 945 : cond = noce_get_alt_condition (if_info, b, &earliest);
3222 945 : if (!cond)
3223 : return false;
3224 :
3225 : /* Verify the condition is of the form we expect. */
3226 25 : if (rtx_equal_p (XEXP (cond, 0), b))
3227 25 : c = XEXP (cond, 1);
3228 0 : else if (rtx_equal_p (XEXP (cond, 1), b))
3229 : {
3230 0 : c = XEXP (cond, 0);
3231 0 : negate = !negate;
3232 : }
3233 : else
3234 : return false;
3235 :
3236 : /* Verify that C is zero. Search one step backward for a
3237 : REG_EQUAL note or a simple source if necessary. */
3238 25 : if (REG_P (c))
3239 : {
3240 0 : rtx set;
3241 0 : rtx_insn *insn = prev_nonnote_nondebug_insn (earliest);
3242 0 : if (insn
3243 0 : && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
3244 0 : && (set = single_set (insn))
3245 0 : && rtx_equal_p (SET_DEST (set), c))
3246 : {
3247 0 : rtx note = find_reg_equal_equiv_note (insn);
3248 0 : if (note)
3249 0 : c = XEXP (note, 0);
3250 : else
3251 0 : c = SET_SRC (set);
3252 : }
3253 : else
3254 : return false;
3255 : }
3256 25 : if (MEM_P (c)
3257 0 : && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
3258 25 : && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
3259 0 : c = get_pool_constant (XEXP (c, 0));
3260 :
3261 : /* Work around funny ideas get_condition has wrt canonicalization.
3262 : Note that these rtx constants are known to be CONST_INT, and
3263 : therefore imply integer comparisons.
3264 : The one_cmpl case is more complicated, as we want to handle
3265 : only x < 0 ? ~x : x or x >= 0 ? x : ~x to one_cmpl_abs (x)
3266 : and x < 0 ? x : ~x or x >= 0 ? ~x : x to ~one_cmpl_abs (x),
3267 : but not other cases (x > -1 is equivalent of x >= 0). */
3268 25 : if (c == constm1_rtx && GET_CODE (cond) == GT)
3269 : ;
3270 1 : else if (c == const1_rtx && GET_CODE (cond) == LT)
3271 : {
3272 0 : if (one_cmpl)
3273 : return false;
3274 : }
3275 1 : else if (c == CONST0_RTX (GET_MODE (b)))
3276 : {
3277 0 : if (one_cmpl
3278 0 : && GET_CODE (cond) != GE
3279 0 : && GET_CODE (cond) != LT)
3280 : return false;
3281 : }
3282 : else
3283 : return false;
3284 :
3285 : /* Determine what sort of operation this is. */
3286 24 : switch (GET_CODE (cond))
3287 : {
3288 0 : case LT:
3289 0 : case LE:
3290 0 : case UNLT:
3291 0 : case UNLE:
3292 0 : negate = !negate;
3293 0 : break;
3294 : case GT:
3295 : case GE:
3296 : case UNGT:
3297 : case UNGE:
3298 : break;
3299 : default:
3300 : return false;
3301 : }
3302 :
3303 24 : start_sequence ();
3304 24 : if (one_cmpl)
3305 0 : target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
3306 : if_info->x);
3307 : else
3308 24 : target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
3309 :
3310 : /* ??? It's a quandary whether cmove would be better here, especially
3311 : for integers. Perhaps combine will clean things up. */
3312 24 : if (target && negate)
3313 : {
3314 0 : if (one_cmpl)
3315 0 : target = expand_simple_unop (GET_MODE (target), NOT, target,
3316 : if_info->x, 0);
3317 : else
3318 0 : target = expand_simple_unop (GET_MODE (target), NEG, target,
3319 : if_info->x, 0);
3320 : }
3321 :
3322 24 : if (! target)
3323 : {
3324 0 : end_sequence ();
3325 0 : return false;
3326 : }
3327 :
3328 24 : if (target != if_info->x)
3329 0 : noce_emit_move_insn (if_info->x, target);
3330 :
3331 24 : seq = end_ifcvt_sequence (if_info);
3332 24 : if (!seq)
3333 : return false;
3334 :
3335 24 : emit_insn_before_setloc (seq, if_info->jump, if_info->insn_a->location ());
3336 24 : if_info->cond = cond;
3337 24 : if_info->cond_earliest = earliest;
3338 24 : if_info->rev_cond = NULL_RTX;
3339 24 : if_info->transform_name = "noce_try_abs";
3340 :
3341 24 : return true;
3342 : }
3343 :
3344 : /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
3345 :
3346 : static bool
3347 111929 : noce_try_sign_mask (noce_if_info *if_info)
3348 : {
3349 111929 : rtx cond, t, m, c;
3350 111929 : rtx_insn *seq;
3351 111929 : machine_mode mode;
3352 111929 : enum rtx_code code;
3353 111929 : bool t_unconditional;
3354 :
3355 136529 : if (!noce_simple_bbs (if_info))
3356 : return false;
3357 :
3358 100425 : cond = if_info->cond;
3359 100425 : code = GET_CODE (cond);
3360 100425 : m = XEXP (cond, 0);
3361 100425 : c = XEXP (cond, 1);
3362 :
3363 100425 : t = NULL_RTX;
3364 100425 : if (if_info->a == const0_rtx)
3365 : {
3366 2671 : if ((code == LT && c == const0_rtx)
3367 2642 : || (code == LE && c == constm1_rtx))
3368 29 : t = if_info->b;
3369 : }
3370 97754 : else if (if_info->b == const0_rtx)
3371 : {
3372 10394 : if ((code == GE && c == const0_rtx)
3373 10394 : || (code == GT && c == constm1_rtx))
3374 : t = if_info->a;
3375 : }
3376 :
3377 95 : if (! t || side_effects_p (t))
3378 : return false;
3379 :
3380 : /* We currently don't handle different modes. */
3381 95 : mode = GET_MODE (t);
3382 95 : if (GET_MODE (m) != mode)
3383 : return false;
3384 :
3385 : /* This is only profitable if T is unconditionally executed/evaluated in the
3386 : original insn sequence or T is cheap and can't trap or fault. The former
3387 : happens if B is the non-zero (T) value and if INSN_B was taken from
3388 : TEST_BB, or there was no INSN_B which can happen for e.g. conditional
3389 : stores to memory. For the cost computation use the block TEST_BB where
3390 : the evaluation will end up after the transformation. */
3391 43 : t_unconditional
3392 86 : = (t == if_info->b
3393 43 : && (if_info->insn_b == NULL_RTX
3394 0 : || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
3395 86 : if (!(t_unconditional
3396 43 : || ((set_src_cost (t, mode, if_info->speed_p)
3397 : < COSTS_N_INSNS (2))
3398 37 : && !may_trap_or_fault_p (t))))
3399 : return false;
3400 :
3401 0 : if (!noce_can_force_operand (t))
3402 : return false;
3403 :
3404 0 : start_sequence ();
3405 : /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
3406 : "(signed) m >> 31" directly. This benefits targets with specialized
3407 : insns to obtain the signmask, but still uses ashr_optab otherwise. */
3408 0 : m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
3409 0 : t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
3410 : : NULL_RTX;
3411 :
3412 0 : if (!t)
3413 : {
3414 0 : end_sequence ();
3415 0 : return false;
3416 : }
3417 :
3418 0 : noce_emit_move_insn (if_info->x, t);
3419 :
3420 0 : seq = end_ifcvt_sequence (if_info);
3421 0 : return noce_commit_sequence (if_info, seq, false, "noce_try_sign_mask");
3422 : }
3423 :
3424 : /* Return the arithmetic operation in X, looking through an extension. */
3425 :
3426 : static rtx
3427 217536 : noce_cond_arith_operation (rtx x)
3428 : {
3429 217536 : enum rtx_code code = GET_CODE (x);
3430 217536 : if (code == SIGN_EXTEND || code == ZERO_EXTEND)
3431 525 : x = XEXP (x, 0);
3432 :
3433 217536 : return noce_cond_zero_binary_op_supported (x) ? x : NULL_RTX;
3434 : }
3435 :
3436 : /* Return true if X contains an EXT_CODE extension from its mode to
3437 : OUTER_MODE. */
3438 :
3439 : static bool
3440 0 : noce_operand_known_extended_p (rtx x, machine_mode outer_mode,
3441 : enum rtx_code ext_code)
3442 : {
3443 0 : machine_mode inner_mode = GET_MODE (x);
3444 0 : unsigned int precision = GET_MODE_PRECISION (outer_mode).to_constant ();
3445 0 : unsigned int inner_precision
3446 0 : = GET_MODE_PRECISION (inner_mode).to_constant ();
3447 0 : if (ext_code == SIGN_EXTEND)
3448 0 : return (num_sign_bit_copies (x, outer_mode)
3449 0 : > precision - inner_precision);
3450 :
3451 0 : if (ext_code == ZERO_EXTEND)
3452 0 : return (HWI_COMPUTABLE_MODE_P (outer_mode)
3453 0 : && (nonzero_bits (x, outer_mode)
3454 0 : & ~GET_MODE_MASK (inner_mode)) == 0);
3455 :
3456 : return false;
3457 : }
3458 :
3459 : /* Return the register or constant that EXP selects on: EXP itself for a
3460 : register or a constant, the inner register of a subreg, and NULL_RTX for
3461 : anything else. */
3462 :
3463 : static rtx
3464 28290 : get_base_reg_or_constant (rtx exp)
3465 : {
3466 0 : if (REG_P (exp))
3467 : return exp;
3468 5169 : else if (SUBREG_P (exp))
3469 317 : return SUBREG_REG (exp);
3470 4852 : else if (CONST_INT_P (exp))
3471 0 : return exp;
3472 : return NULL_RTX;
3473 : }
3474 :
3475 : /* Try to convert if-then-else with conditional zero, returning TRUE on
3476 : success or FALSE on failure. IF_INFO describes the if-conversion scenario
3477 : under consideration.
3478 :
3479 : It verifies the branch structure on the left and transforms it into the
3480 : branchless sequence on the right, with a backend-provided conditional zero
3481 : or orig for operand z. If true, tmp is z, 0 otherwise (y op 0 is the same
3482 : as y for most op).
3483 :
3484 : if (cond) | tmp = cond ? z : 0
3485 : x = y op z | x = y op tmp
3486 : else |
3487 : x = y |
3488 :
3489 : AND is special as it needs to be handled differently.
3490 :
3491 : tmp = !cond ? y : 0
3492 : x = (y & z) | tmp
3493 :
3494 : Also for AND try:
3495 :
3496 : tmp = cond ? z : -1
3497 : x = y op tmp
3498 :
3499 : to see if it is cheaper to produce `!cond ? y : 0` or `cond ? z : -1`. */
3500 :
3501 : static bool
3502 157569 : noce_try_cond_arith (noce_if_info *if_info)
3503 : {
3504 157569 : rtx target, a, b, a_op0, a_op1, outer_a;
3505 157569 : rtx cond = if_info->cond;
3506 157569 : rtx_code code = GET_CODE (cond);
3507 157569 : rtx_insn *seq;
3508 157569 : rtx_code op;
3509 157569 : machine_mode mode = GET_MODE (if_info->x);
3510 157569 : rtx a_val = if_info->a;
3511 :
3512 : /* Scalar integral modes are only supported here.
3513 : Could support scalar floating point but that
3514 : would be only with -ffast-math and might
3515 : be worse than a branch. */
3516 157569 : if (!SCALAR_INT_MODE_P (mode))
3517 : return false;
3518 :
3519 : /* Even if the block is not simple it can have
3520 : an extraneous copy which can be traced back to
3521 : the definition and the value of 'a' can be
3522 : computed and copied from there. For this we
3523 : require that there are exactly 2 non debug
3524 : non note insns in then_bb one a copy and the other
3525 : its definition. We also check the pattern of
3526 : def matches def_set so as to not drop a clobber. */
3527 164275 : if (!noce_simple_bbs (if_info))
3528 : {
3529 17760 : rtx_insn *def = prev_nonnote_nondebug_insn_bb (if_info->insn_a);
3530 17760 : rtx def_set = def ? single_set (def) : NULL_RTX;
3531 :
3532 14114 : if (!def_set
3533 14114 : || !REG_P (SET_DEST (def_set))
3534 14086 : || !rtx_equal_p (SET_DEST (def_set), if_info->a)
3535 3162 : || PATTERN (def) != def_set
3536 15 : || first_active_insn (if_info->then_bb) != def
3537 8 : || (if_info->else_bb && !if_info->else_simple)
3538 14115 : || !REG_P (if_info->orig_x))
3539 : return false;
3540 :
3541 0 : a_val = SET_SRC (def_set);
3542 : }
3543 :
3544 108768 : a = copy_rtx (a_val);
3545 108768 : b = copy_rtx (if_info->b);
3546 108768 : rtx a_arith = noce_cond_arith_operation (a);
3547 108768 : rtx b_arith = noce_cond_arith_operation (b);
3548 :
3549 : /* Canonicalize x = y : (y op z) to x = (y op z) : y. */
3550 108768 : if (REG_P (a) && b_arith)
3551 : {
3552 194 : cond = noce_reversed_cond (if_info, &code);
3553 194 : std::swap (a, b);
3554 194 : a_arith = b_arith;
3555 : }
3556 :
3557 : /* Check if x = (y op z) : y is supported by czero based ifcvt. */
3558 108574 : else if (!(a_arith && REG_P (b)))
3559 94290 : goto fail;
3560 :
3561 14478 : if (code == UNKNOWN)
3562 0 : goto fail;
3563 :
3564 14478 : outer_a = a == a_arith ? NULL_RTX : a;
3565 14478 : a = a_arith;
3566 14478 : op = GET_CODE (a);
3567 :
3568 : /* EXTEND (y op z) : y is valid only when Y already contains the same
3569 : extension of its lowpart. AND uses a different transformation. */
3570 14478 : if (outer_a
3571 0 : && (op == AND
3572 0 : || GET_MODE (outer_a) != mode
3573 0 : || !SCALAR_INT_MODE_P (GET_MODE (a))))
3574 0 : goto fail;
3575 :
3576 : /* Canonicalize x = (z op y) : y to x = (y op z) : y */
3577 14478 : a_op1 = get_base_reg_or_constant (XEXP (a, 1));
3578 13802 : if (a_op1 && rtx_equal_p (a_op1, b) && COMMUTATIVE_ARITH_P (a))
3579 : {
3580 23 : std::swap (XEXP (a, 0), XEXP (a, 1));
3581 23 : a_op1 = get_base_reg_or_constant (XEXP (a, 1));
3582 : }
3583 :
3584 14455 : if (a_op1 == NULL_RTX)
3585 689 : goto fail;
3586 :
3587 : /* Ensure the cond is of form: x = (y op z) : y */
3588 13789 : a_op0 = get_base_reg_or_constant (XEXP (a, 0));
3589 13786 : if (!(a_op0 && rtx_equal_p (a_op0, b)))
3590 2035 : goto fail;
3591 :
3592 11754 : if (outer_a
3593 11754 : && (!SUBREG_P (XEXP (a, 0))
3594 0 : || !subreg_lowpart_p (XEXP (a, 0))
3595 0 : || GET_MODE (SUBREG_REG (XEXP (a, 0))) != mode
3596 0 : || !noce_operand_known_extended_p (XEXP (a, 0), mode,
3597 0 : GET_CODE (outer_a))))
3598 0 : goto fail;
3599 :
3600 11754 : start_sequence ();
3601 :
3602 11754 : if (CONST_INT_P (XEXP (a, 1)))
3603 2920 : target = gen_reg_rtx (GET_MODE (XEXP (a, 0)));
3604 : else
3605 8834 : target = gen_reg_rtx (GET_MODE (XEXP (a, op != AND)));
3606 :
3607 : /* AND requires !cond, instead we swap ops around. */
3608 11754 : target = noce_emit_cmove (if_info, target, code,
3609 : XEXP (cond, 0), XEXP (cond, 1),
3610 : op != AND ? XEXP (a, 1) : const0_rtx,
3611 : op != AND ? const0_rtx : XEXP (a, 0));
3612 11754 : if (!target)
3613 : {
3614 1235 : end_sequence ();
3615 1235 : rtx tmp = XEXP (a, op != AND);
3616 : /* If the cmove fails and this was a lowpart subreg,
3617 : then try the reg part and then putting back the lowpart
3618 : afterwards. */
3619 1235 : if (GET_CODE (tmp) != SUBREG || !subreg_lowpart_p (tmp))
3620 : return false;
3621 0 : tmp = SUBREG_REG (tmp);
3622 : /* Only handle integer scalar modes for the inner mode of
3623 : the subreg. */
3624 0 : if (!SCALAR_INT_MODE_P (GET_MODE (tmp)))
3625 : return false;
3626 :
3627 0 : start_sequence ();
3628 0 : target = gen_reg_rtx (GET_MODE (tmp));
3629 0 : target = noce_emit_cmove (if_info, target, code,
3630 : XEXP (cond, 0), XEXP (cond, 1),
3631 : op != AND ? tmp : const0_rtx,
3632 : op != AND ? const0_rtx : tmp);
3633 0 : if (!target)
3634 0 : goto end_seq_n_fail;
3635 0 : target = rtl_hooks.gen_lowpart_no_emit (GET_MODE (XEXP (a, op != AND)), target);
3636 0 : gcc_assert (target);
3637 : }
3638 :
3639 : /* Every path through the fallback above either returns or produces a
3640 : conditional move, so TARGET is non-null below. */
3641 : gcc_checking_assert (target);
3642 :
3643 10519 : if (outer_a)
3644 : {
3645 0 : XEXP (a, 1) = target;
3646 0 : noce_emit_move_insn (if_info->x, outer_a);
3647 0 : target = if_info->x;
3648 0 : goto success;
3649 : }
3650 :
3651 : /* For AND, try `cond ? z : -1` to see if that is cheaper or the same cost.
3652 : In some cases it will be cheaper to produce the -1 rather than the 0 case. */
3653 10519 : if (op == AND)
3654 : {
3655 92 : rtx_insn *seq0 = end_sequence ();
3656 92 : unsigned cost0 = seq_cost (seq0, if_info->speed_p);
3657 :
3658 : /* Produce `cond ? z : -1`. */
3659 92 : rtx targetm1;
3660 92 : start_sequence ();
3661 92 : targetm1 = gen_reg_rtx (GET_MODE (XEXP (a, 1)));
3662 92 : targetm1 = noce_emit_cmove (if_info, targetm1, code,
3663 : XEXP (cond, 0), XEXP (cond, 1),
3664 : XEXP (a, 1), constm1_rtx);
3665 92 : rtx_insn *seqm1 = end_sequence ();
3666 92 : unsigned costm1 = seq_cost (seqm1, if_info->speed_p);
3667 92 : if (!targetm1)
3668 86 : costm1 = -1u;
3669 :
3670 : /* If -1 is cheaper or the same cost to producing 0, then use that. */
3671 92 : if (costm1 <= cost0)
3672 : {
3673 6 : push_to_sequence (seqm1);
3674 6 : targetm1 = expand_simple_binop (mode, op, a_op0, targetm1,
3675 : if_info->x, 0, OPTAB_WIDEN);
3676 6 : if (targetm1)
3677 : {
3678 6 : target = targetm1;
3679 6 : goto success;
3680 : }
3681 0 : end_sequence ();
3682 : }
3683 :
3684 : /* For 0 the produce sequence is:
3685 : tmp = !cond ? y : 0
3686 : x = (y & z) | tmp */
3687 86 : push_to_sequence (seq0);
3688 86 : rtx a_bin = gen_reg_rtx (mode);
3689 86 : noce_emit_move_insn (a_bin, a);
3690 :
3691 86 : target = expand_simple_binop (mode, IOR, a_bin, target, if_info->x, 0,
3692 : OPTAB_WIDEN);
3693 86 : if (!target)
3694 0 : goto end_seq_n_fail;
3695 86 : goto success;
3696 : }
3697 :
3698 10427 : target = expand_simple_binop (mode, op, a_op0, target, if_info->x, 0,
3699 : OPTAB_WIDEN);
3700 :
3701 10427 : if (!target)
3702 0 : goto end_seq_n_fail;
3703 :
3704 10427 : success:
3705 10519 : if (target != if_info->x)
3706 0 : noce_emit_move_insn (if_info->x, target);
3707 :
3708 10519 : seq = end_ifcvt_sequence (if_info);
3709 10519 : return noce_commit_sequence (if_info, seq, true, "noce_try_cond_arith");
3710 :
3711 0 : end_seq_n_fail:
3712 0 : end_sequence ();
3713 :
3714 157569 : fail:
3715 :
3716 : return false;
3717 : }
3718 :
3719 : /* Optimize away "if (x & C) x |= C" and similar bit manipulation
3720 : transformations. */
3721 :
3722 : static bool
3723 210349 : noce_try_bitop (noce_if_info *if_info)
3724 : {
3725 210349 : rtx cond, x, a, result;
3726 210349 : rtx_insn *seq;
3727 210349 : scalar_int_mode mode;
3728 210349 : enum rtx_code code;
3729 210349 : int bitnum;
3730 :
3731 210349 : x = if_info->x;
3732 210349 : cond = if_info->cond;
3733 210349 : code = GET_CODE (cond);
3734 :
3735 : /* Check for an integer operation. */
3736 210349 : if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
3737 : return false;
3738 :
3739 233893 : if (!noce_simple_bbs (if_info))
3740 : return false;
3741 :
3742 : /* Check for no else condition. */
3743 158635 : if (! rtx_equal_p (x, if_info->b))
3744 : return false;
3745 :
3746 : /* Check for a suitable condition. */
3747 84607 : if (code != NE && code != EQ)
3748 : return false;
3749 60308 : if (XEXP (cond, 1) != const0_rtx)
3750 : return false;
3751 41461 : cond = XEXP (cond, 0);
3752 :
3753 : /* ??? We could also handle AND here. */
3754 41461 : if (GET_CODE (cond) == ZERO_EXTRACT)
3755 : {
3756 253 : if (XEXP (cond, 1) != const1_rtx
3757 114 : || !CONST_INT_P (XEXP (cond, 2))
3758 367 : || ! rtx_equal_p (x, XEXP (cond, 0)))
3759 : return false;
3760 4 : bitnum = INTVAL (XEXP (cond, 2));
3761 4 : if (BITS_BIG_ENDIAN)
3762 : bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
3763 4 : if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
3764 : return false;
3765 : }
3766 : else
3767 : return false;
3768 :
3769 4 : a = if_info->a;
3770 4 : if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
3771 : {
3772 : /* Check for "if (X & C) x = x op C". */
3773 0 : if (! rtx_equal_p (x, XEXP (a, 0))
3774 0 : || !CONST_INT_P (XEXP (a, 1))
3775 0 : || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3776 0 : != HOST_WIDE_INT_1U << bitnum)
3777 : return false;
3778 :
3779 : /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
3780 : /* if ((x & C) != 0) x |= C; is transformed to nothing. */
3781 0 : if (GET_CODE (a) == IOR)
3782 0 : result = (code == NE) ? a : NULL_RTX;
3783 0 : else if (code == NE)
3784 : {
3785 : /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
3786 0 : result = gen_int_mode (HOST_WIDE_INT_1 << bitnum, mode);
3787 0 : result = simplify_gen_binary (IOR, mode, x, result);
3788 : }
3789 : else
3790 : {
3791 : /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
3792 0 : result = gen_int_mode (~(HOST_WIDE_INT_1 << bitnum), mode);
3793 0 : result = simplify_gen_binary (AND, mode, x, result);
3794 : }
3795 : }
3796 4 : else if (GET_CODE (a) == AND)
3797 : {
3798 : /* Check for "if (X & C) x &= ~C". */
3799 0 : if (! rtx_equal_p (x, XEXP (a, 0))
3800 0 : || !CONST_INT_P (XEXP (a, 1))
3801 0 : || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3802 0 : != (~(HOST_WIDE_INT_1 << bitnum) & GET_MODE_MASK (mode)))
3803 : return false;
3804 :
3805 : /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
3806 : /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
3807 0 : result = (code == EQ) ? a : NULL_RTX;
3808 : }
3809 : else
3810 : return false;
3811 :
3812 0 : if (result)
3813 : {
3814 0 : start_sequence ();
3815 0 : noce_emit_move_insn (x, result);
3816 0 : seq = end_ifcvt_sequence (if_info);
3817 0 : if (!seq)
3818 : return false;
3819 :
3820 0 : emit_insn_before_setloc (seq, if_info->jump,
3821 0 : if_info->insn_a->location ());
3822 : }
3823 0 : if_info->transform_name = "noce_try_bitop";
3824 0 : return true;
3825 : }
3826 :
3827 :
3828 : /* Similar to get_condition, only the resulting condition must be
3829 : valid at JUMP, instead of at EARLIEST.
3830 :
3831 : If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
3832 : THEN block of the caller, and we have to reverse the condition. */
3833 :
3834 : static rtx
3835 4301815 : noce_get_condition (rtx_insn *jump, rtx_insn **earliest,
3836 : bool then_else_reversed)
3837 : {
3838 4301815 : rtx cond, set, tmp;
3839 4301815 : bool reverse;
3840 :
3841 4301815 : if (! any_condjump_p (jump))
3842 : return NULL_RTX;
3843 :
3844 4301815 : set = pc_set (jump);
3845 :
3846 : /* If this branches to JUMP_LABEL when the condition is false,
3847 : reverse the condition. */
3848 8603630 : reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
3849 4301815 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
3850 :
3851 : /* We may have to reverse because the caller's if block is not canonical,
3852 : i.e. the THEN block isn't the fallthrough block for the TEST block
3853 : (see find_if_header). */
3854 4301815 : if (then_else_reversed)
3855 1899907 : reverse = !reverse;
3856 :
3857 : /* If the condition variable is a register and is MODE_INT, accept it. */
3858 :
3859 4301815 : cond = XEXP (SET_SRC (set), 0);
3860 4301815 : tmp = XEXP (cond, 0);
3861 4301109 : if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
3862 4301833 : && (GET_MODE (tmp) != BImode
3863 0 : || !targetm.small_register_classes_for_mode_p (BImode)))
3864 : {
3865 18 : *earliest = jump;
3866 :
3867 18 : if (reverse)
3868 9 : cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
3869 : GET_MODE (cond), tmp, XEXP (cond, 1));
3870 : return cond;
3871 : }
3872 :
3873 : /* Otherwise, fall back on canonicalize_condition to do the dirty
3874 : work of manipulating MODE_CC values and COMPARE rtx codes. */
3875 4301797 : tmp = canonicalize_condition (jump, cond, reverse, earliest,
3876 : NULL_RTX, have_cbranchcc4, true);
3877 :
3878 : /* We don't handle side-effects in the condition, like handling
3879 : REG_INC notes and making sure no duplicate conditions are emitted. */
3880 4301797 : if (tmp != NULL_RTX && side_effects_p (tmp))
3881 8758 : return NULL_RTX;
3882 :
3883 : return tmp;
3884 : }
3885 :
3886 : /* Return true if OP is ok for if-then-else processing. */
3887 :
3888 : static bool
3889 9034492 : noce_operand_ok (const_rtx op)
3890 : {
3891 9034492 : if (side_effects_p (op))
3892 : return false;
3893 :
3894 : /* We special-case memories, so handle any of them with
3895 : no address side effects. */
3896 9012357 : if (MEM_P (op))
3897 1607626 : return ! side_effects_p (XEXP (op, 0));
3898 :
3899 7404731 : return ! may_trap_p (op);
3900 : }
3901 :
3902 : /* Return true iff basic block TEST_BB is valid for noce if-conversion.
3903 : The condition used in this if-conversion is in COND.
3904 : In practice, check that TEST_BB ends with a single set
3905 : x := a and all previous computations
3906 : in TEST_BB don't produce any values that are live after TEST_BB.
3907 : In other words, all the insns in TEST_BB are there only
3908 : to compute a value for x. Add the rtx cost of the insns
3909 : in TEST_BB to COST. Record whether TEST_BB is a single simple
3910 : set instruction in SIMPLE_P. */
3911 :
3912 : static bool
3913 2144355 : bb_valid_for_noce_process_p (basic_block test_bb, rtx cond,
3914 : unsigned int *cost, bool *simple_p)
3915 : {
3916 2144355 : if (!test_bb)
3917 : return false;
3918 :
3919 2144355 : rtx_insn *last_insn = last_active_insn (test_bb, false);
3920 2144355 : rtx last_set = NULL_RTX;
3921 :
3922 2144355 : rtx cc = cc_in_cond (cond);
3923 :
3924 2144355 : if (!insn_valid_noce_process_p (last_insn, cc))
3925 : return false;
3926 :
3927 : /* Punt on blocks ending with asm goto or jumps with other side-effects,
3928 : last_active_insn ignores JUMP_INSNs. */
3929 1541234 : if (JUMP_P (BB_END (test_bb)) && !onlyjump_p (BB_END (test_bb)))
3930 : return false;
3931 :
3932 1541232 : last_set = single_set (last_insn);
3933 :
3934 1541232 : rtx x = SET_DEST (last_set);
3935 1541232 : rtx_insn *first_insn = first_active_insn (test_bb);
3936 1541232 : rtx first_set = single_set (first_insn);
3937 :
3938 1541232 : if (!first_set)
3939 : return false;
3940 :
3941 : /* We have a single simple set, that's okay. */
3942 1526782 : bool speed_p = optimize_bb_for_speed_p (test_bb);
3943 :
3944 1526782 : if (first_insn == last_insn)
3945 : {
3946 551285 : *simple_p = noce_operand_ok (SET_DEST (first_set));
3947 551285 : *cost += pattern_cost (first_set, speed_p);
3948 551285 : return *simple_p;
3949 : }
3950 :
3951 975497 : rtx_insn *prev_last_insn = PREV_INSN (last_insn);
3952 975497 : gcc_assert (prev_last_insn);
3953 :
3954 : /* For now, disallow setting x multiple times in test_bb. */
3955 975497 : if (REG_P (x) && reg_set_between_p (x, first_insn, prev_last_insn))
3956 : return false;
3957 :
3958 827987 : auto_bitmap test_bb_temps (®_obstack);
3959 :
3960 : /* The regs that are live out of test_bb. */
3961 827987 : bitmap test_bb_live_out = df_get_live_out (test_bb);
3962 :
3963 827987 : int potential_cost = pattern_cost (last_set, speed_p);
3964 827987 : rtx_insn *insn;
3965 4563005 : FOR_BB_INSNS (test_bb, insn)
3966 : {
3967 4483473 : if (insn != last_insn)
3968 : {
3969 4403941 : if (!active_insn_p (insn))
3970 2910772 : continue;
3971 :
3972 1493169 : if (!insn_valid_noce_process_p (insn, cc))
3973 : return false;
3974 :
3975 1254683 : rtx sset = single_set (insn);
3976 1254683 : gcc_assert (sset);
3977 1254683 : rtx dest = SET_DEST (sset);
3978 1254683 : if (SUBREG_P (dest))
3979 18621 : dest = SUBREG_REG (dest);
3980 :
3981 1254683 : if (contains_mem_rtx_p (SET_SRC (sset))
3982 882346 : || !REG_P (dest)
3983 2008351 : || reg_overlap_mentioned_p (dest, cond))
3984 : return false;
3985 :
3986 744714 : potential_cost += pattern_cost (sset, speed_p);
3987 744714 : bitmap_set_bit (test_bb_temps, REGNO (dest));
3988 : }
3989 : }
3990 :
3991 : /* If any of the intermediate results in test_bb are live after test_bb
3992 : then fail. */
3993 79532 : if (bitmap_intersect_p (test_bb_live_out, test_bb_temps))
3994 : return false;
3995 :
3996 30867 : *cost += potential_cost;
3997 30867 : *simple_p = false;
3998 30867 : return true;
3999 827987 : }
4000 :
4001 : /* Helper function to emit a cmov sequence encapsulated in
4002 : start_sequence () and end_sequence (). If NEED_CMOV is true
4003 : we call noce_emit_cmove to create a cmove sequence. Otherwise emit
4004 : a simple move. If successful, store the first instruction of the
4005 : sequence in TEMP_DEST and the sequence costs in SEQ_COST. */
4006 :
4007 : static rtx_insn*
4008 402364 : try_emit_cmove_seq (noce_if_info *if_info, rtx temp,
4009 : rtx cond, rtx new_val, rtx old_val, bool need_cmov,
4010 : unsigned *cost, rtx *temp_dest,
4011 : rtx cc_cmp = NULL, rtx rev_cc_cmp = NULL)
4012 : {
4013 402364 : rtx_insn *seq = NULL;
4014 402364 : *cost = 0;
4015 :
4016 402364 : rtx x = XEXP (cond, 0);
4017 402364 : rtx y = XEXP (cond, 1);
4018 402364 : rtx_code cond_code = GET_CODE (cond);
4019 :
4020 402364 : start_sequence ();
4021 :
4022 402364 : if (need_cmov)
4023 324248 : *temp_dest = noce_emit_cmove (if_info, temp, cond_code,
4024 : x, y, new_val, old_val, cc_cmp, rev_cc_cmp);
4025 : else
4026 : {
4027 78116 : *temp_dest = temp;
4028 78116 : if (if_info->then_else_reversed)
4029 1940 : noce_emit_move_insn (temp, old_val);
4030 : else
4031 76176 : noce_emit_move_insn (temp, new_val);
4032 : }
4033 :
4034 402364 : if (*temp_dest != NULL_RTX)
4035 : {
4036 396022 : seq = get_insns ();
4037 396022 : *cost = seq_cost (seq, if_info->speed_p);
4038 : }
4039 :
4040 402364 : end_sequence ();
4041 :
4042 402364 : return seq;
4043 : }
4044 :
4045 : /* Finish a successful noce if-conversion of IF_INFO whose replacement insns
4046 : have already been emitted before the branch. Delete the now-dead THEN
4047 : block, and ELSE too if this was a diamond. An IF-THEN-JOIN also has an edge
4048 : from TEST_BB straight to JOIN_BB that bypassed THEN, and that goes as well.
4049 : An IF-THEN-ELSE-JOIN has no such edge, and deleting ELSE_BB removes its two
4050 : edges instead. Redirect TEST_BB to JOIN_BB and merge the two when the tail
4051 : no longer needs its own block. Count the converted if-block and the CFG
4052 : changes it took. */
4053 :
4054 : static void
4055 179216 : noce_finish_if_conversion (noce_if_info *if_info)
4056 : {
4057 179216 : basic_block test_bb = if_info->test_bb;
4058 179216 : basic_block then_bb = if_info->then_bb;
4059 179216 : basic_block else_bb = if_info->else_bb;
4060 179216 : basic_block join_bb = if_info->join_bb;
4061 :
4062 179216 : if (else_bb)
4063 : {
4064 76465 : delete_basic_block (else_bb);
4065 76465 : num_true_changes++;
4066 : }
4067 : else
4068 102751 : remove_edge (find_edge (test_bb, join_bb));
4069 :
4070 179216 : remove_edge (find_edge (then_bb, join_bb));
4071 179216 : redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
4072 179216 : delete_basic_block (then_bb);
4073 179216 : num_true_changes++;
4074 :
4075 179216 : if (can_merge_blocks_p (test_bb, join_bb))
4076 : {
4077 135492 : merge_blocks (test_bb, join_bb);
4078 135492 : num_true_changes++;
4079 : }
4080 :
4081 179216 : num_updated_if_blocks++;
4082 179216 : }
4083 :
4084 : /* We have something like:
4085 :
4086 : if (x > y)
4087 : { i = EXPR_A; j = EXPR_B; k = EXPR_C; }
4088 :
4089 : Make it:
4090 :
4091 : tmp_i = (x > y) ? EXPR_A : i;
4092 : tmp_j = (x > y) ? EXPR_B : j;
4093 : tmp_k = (x > y) ? EXPR_C : k;
4094 : i = tmp_i;
4095 : j = tmp_j;
4096 : k = tmp_k;
4097 :
4098 : Subsequent passes are expected to clean up the extra moves.
4099 :
4100 : Look for special cases such as writes to one register which are
4101 : read back in another SET, as might occur in a swap idiom or
4102 : similar.
4103 :
4104 : These look like:
4105 :
4106 : if (x > y)
4107 : i = a;
4108 : j = i;
4109 :
4110 : Which we want to rewrite to:
4111 :
4112 : tmp_i = (x > y) ? a : i;
4113 : tmp_j = (x > y) ? tmp_i : j;
4114 : i = tmp_i;
4115 : j = tmp_j;
4116 :
4117 : We can catch these when looking at (SET x y) by keeping a list of the
4118 : registers we would have targeted before if-conversion and looking back
4119 : through it for an overlap with Y. If we find one, we rewire the
4120 : conditional set to use the temporary we introduced earlier.
4121 :
4122 : IF_INFO contains the useful information about the block structure and
4123 : jump instructions. For an IF-THEN-ELSE-JOIN, first evaluate the else
4124 : arm's sets into temporaries and retain their final values for the
4125 : conditional moves. Return true if the replacement is valid and profitable
4126 : and its CFG changes have been committed, otherwise return false. */
4127 :
4128 : static bool
4129 51559 : noce_convert_multiple_sets (noce_if_info *if_info)
4130 : {
4131 51559 : basic_block test_bb = if_info->test_bb;
4132 51559 : basic_block then_bb = if_info->then_bb;
4133 51559 : basic_block else_bb = if_info->else_bb;
4134 51559 : rtx_insn *jump = if_info->jump;
4135 51559 : rtx_insn *cond_earliest;
4136 51559 : rtx_insn *insn;
4137 :
4138 51559 : start_sequence ();
4139 :
4140 : /* Decompose the condition attached to the jump. */
4141 51559 : rtx cond = noce_get_condition (jump, &cond_earliest, false);
4142 :
4143 51559 : auto_delete_vec<noce_multiple_sets_info> insn_info;
4144 51559 : init_noce_multiple_sets_info (then_bb, insn_info);
4145 51559 : gcc_assert (!insn_info.is_empty ());
4146 51559 : unsigned then_count = insn_info.length ();
4147 51559 : location_t sequence_location
4148 51559 : = insn_info.last ()->unmodified_insn->location ();
4149 :
4150 51559 : auto_delete_vec<noce_multiple_sets_info> else_insn_info;
4151 51559 : auto_vec<unsigned> else_only_indices;
4152 51559 : if (else_bb)
4153 : {
4154 23066 : init_noce_multiple_sets_info (else_bb, else_insn_info);
4155 :
4156 : /* Add one output entry for each live-out destination that is set only
4157 : by the else arm. Record the last definition, which holds the
4158 : value that reaches the join. */
4159 23066 : auto_bitmap then_dests, seen;
4160 87517 : for (unsigned i = 0; i < then_count; ++i)
4161 64451 : bitmap_set_bit (then_dests, REGNO (insn_info[i]->target));
4162 :
4163 86868 : for (int i = else_insn_info.length () - 1; i >= 0; --i)
4164 40736 : if (else_insn_info[i]->need_cmov
4165 39242 : && !bitmap_bit_p (then_dests,
4166 39242 : REGNO (else_insn_info[i]->target))
4167 48058 : && bitmap_set_bit (seen, REGNO (else_insn_info[i]->target)))
4168 7322 : else_only_indices.safe_push (i);
4169 23066 : else_only_indices.reverse ();
4170 :
4171 23066 : unsigned i, else_index;
4172 60469 : FOR_EACH_VEC_ELT (else_only_indices, i, else_index)
4173 : {
4174 7322 : noce_multiple_sets_info *info = new noce_multiple_sets_info;
4175 7322 : noce_multiple_sets_info *else_info = else_insn_info[else_index];
4176 7322 : info->target = else_info->target;
4177 7322 : info->temporary = NULL_RTX;
4178 7322 : info->unmodified_insn = else_info->unmodified_insn;
4179 7322 : info->need_cmov = true;
4180 7322 : insn_info.safe_push (info);
4181 : }
4182 23066 : }
4183 :
4184 : /* Include else-only outputs when enforcing the conversion limit. */
4185 51559 : if (insn_info.length () > (unsigned) param_max_rtl_if_conversion_insns)
4186 : {
4187 0 : end_sequence ();
4188 0 : return false;
4189 : }
4190 :
4191 51559 : int last_needs_comparison = -1;
4192 :
4193 51559 : bool use_cond_earliest = false;
4194 :
4195 51559 : bool ok = noce_convert_multiple_sets_1
4196 51559 : (if_info, cond, insn_info, else_insn_info, then_count, else_only_indices,
4197 : &last_needs_comparison, &use_cond_earliest);
4198 51559 : if (!ok)
4199 : return false;
4200 :
4201 : /* Always perform a second attempt that uses information gathered in the
4202 : first. At least we can omit creating temporaries until we definitely
4203 : need them. The sequence created in the second attempt is never worse
4204 : than the first. */
4205 :
4206 48606 : end_sequence ();
4207 48606 : start_sequence ();
4208 48606 : ok = noce_convert_multiple_sets_1
4209 48606 : (if_info, cond, insn_info, else_insn_info, then_count, else_only_indices,
4210 : &last_needs_comparison, &use_cond_earliest);
4211 :
4212 : /* Actually we should not fail anymore if we reached here,
4213 : but better still check. */
4214 48606 : if (!ok)
4215 : return false;
4216 :
4217 : /* Now fixup the assignments.
4218 : PR116405: Iterate in reverse order and keep track of the targets so that
4219 : a move does not overwrite a subsequent value when multiple instructions
4220 : have the same target. */
4221 48606 : unsigned i;
4222 48606 : noce_multiple_sets_info *info;
4223 48606 : auto_bitmap set_targets (®_obstack);
4224 230488 : FOR_EACH_VEC_ELT_REVERSE (insn_info, i, info)
4225 : {
4226 133276 : gcc_checking_assert (REG_P (info->target));
4227 :
4228 133276 : if (info->target != info->temporary
4229 133276 : && !bitmap_bit_p (set_targets, REGNO (info->target)))
4230 4171 : noce_emit_move_insn (info->target, info->temporary);
4231 :
4232 133276 : bitmap_set_bit (set_targets, REGNO (info->target));
4233 : }
4234 :
4235 : /* Actually emit the sequence if it isn't too expensive. */
4236 48606 : rtx_insn *seq = get_insns ();
4237 :
4238 : /* If the created sequence does not use cond_earliest (but the jump
4239 : does) add its cost to the original_cost before comparing costs. */
4240 48606 : unsigned int original_cost = if_info->original_cost;
4241 48606 : if (if_info->jump != if_info->cond_earliest && !use_cond_earliest)
4242 37022 : if_info->original_cost += insn_cost (if_info->cond_earliest,
4243 : if_info->speed_p);
4244 :
4245 48606 : if (!targetm.noce_conversion_profitable_p (seq, if_info))
4246 : {
4247 14987 : end_sequence ();
4248 14987 : return false;
4249 : }
4250 :
4251 : /* Restore the original cost in case we do not succeed below. */
4252 33619 : if_info->original_cost = original_cost;
4253 :
4254 222894 : for (insn = seq; insn; insn = NEXT_INSN (insn))
4255 189275 : set_used_flags (insn);
4256 :
4257 : /* Mark all our temporaries and targets as used. */
4258 184627 : for (const noce_multiple_sets_info *msi : insn_info)
4259 : {
4260 83770 : set_used_flags (msi->temporary);
4261 83770 : set_used_flags (msi->target);
4262 : }
4263 :
4264 33619 : set_used_flags (cond);
4265 :
4266 33619 : unshare_all_rtl_in_chain (seq);
4267 33619 : end_sequence ();
4268 :
4269 33619 : if (!seq)
4270 : return false;
4271 :
4272 222688 : for (insn = seq; insn; insn = NEXT_INSN (insn))
4273 189117 : if (JUMP_P (insn) || CALL_P (insn)
4274 189117 : || recog_memoized (insn) == -1)
4275 : return false;
4276 :
4277 : /* This path does not go through end_ifcvt_sequence, so apply the same rule
4278 : about condition codes that outlive the if-region. */
4279 33571 : if (noce_clobbers_live_cc_p (test_bb, seq))
4280 : return false;
4281 :
4282 33571 : emit_insn_before_setloc (seq, if_info->jump, sequence_location);
4283 :
4284 33571 : noce_finish_if_conversion (if_info);
4285 :
4286 33571 : if_info->transform_name = "noce_convert_multiple_sets";
4287 33571 : return true;
4288 100165 : }
4289 :
4290 : /* Return true if any insn in SEQ modifies CC_CMP, or REV_CC_CMP when there is
4291 : one. Such a sequence cannot be used to select on that condition. */
4292 :
4293 : static bool
4294 212851 : noce_seq_clobbers_cc_cmp_p (rtx_insn *seq, rtx cc_cmp, rtx rev_cc_cmp)
4295 : {
4296 318711 : for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
4297 226636 : if (modified_in_p (cc_cmp, insn)
4298 226636 : || (rev_cc_cmp && modified_in_p (rev_cc_cmp, insn)))
4299 : return true;
4300 :
4301 : return false;
4302 : }
4303 :
4304 : /* Try to emit the multiple-set conversion described by IF_INFO, selecting on
4305 : the already decoded jump condition COND. INSN_INFO holds THEN_COUNT
4306 : then-arm entries followed by entries for the else-only definitions recorded
4307 : in ELSE_ONLY_INDICES. For a diamond, evaluate ELSE_INSN_INFO first and
4308 : retain its final values for the conditional moves.
4309 :
4310 : LAST_NEEDS_COMPARISON is -1 on the first attempt. Record in it the last set
4311 : that needs a temporary to preserve the comparison, then use that boundary
4312 : on the second attempt. Set USE_COND_EARLIEST if the emitted sequence uses
4313 : IF_INFO->cond_earliest. Return true if the complete sequence was
4314 : emitted. */
4315 :
4316 : static bool
4317 100165 : noce_convert_multiple_sets_1 (noce_if_info *if_info, rtx cond,
4318 : auto_delete_vec<noce_multiple_sets_info> &insn_info,
4319 : auto_delete_vec<noce_multiple_sets_info>
4320 : &else_insn_info,
4321 : unsigned then_count,
4322 : const vec<unsigned> &else_only_indices,
4323 : int *last_needs_comparison,
4324 : bool *use_cond_earliest)
4325 : {
4326 100165 : rtx_insn *jump = if_info->jump;
4327 :
4328 100165 : rtx cc_cmp = cond_exec_get_condition (jump);
4329 100165 : if (cc_cmp)
4330 100165 : cc_cmp = copy_rtx (cc_cmp);
4331 100165 : rtx rev_cc_cmp = cond_exec_get_condition (jump, /* get_reversed */ true);
4332 100165 : if (rev_cc_cmp)
4333 100165 : rev_cc_cmp = copy_rtx (rev_cc_cmp);
4334 :
4335 100165 : int count = 0;
4336 100165 : bool second_try = *last_needs_comparison != -1;
4337 100165 : *use_cond_earliest = false;
4338 :
4339 : /* For an IF-THEN-ELSE-JOIN, emit the else block's computations first into
4340 : fresh temporaries. This leaves the incoming register values available to
4341 : the then arm. The conditional moves below select the then or else
4342 : values. */
4343 100165 : if (if_info->else_bb)
4344 : {
4345 44190 : int else_count = 0;
4346 44190 : rtx_insn *before_else = get_last_insn ();
4347 44190 : location_t saved_location = curr_insn_location ();
4348 44190 : rtx_insn *else_insn;
4349 261204 : FOR_BB_INSNS (if_info->else_bb, else_insn)
4350 : {
4351 217014 : if (!active_insn_p (else_insn))
4352 143031 : continue;
4353 :
4354 73983 : noce_multiple_sets_info *info = else_insn_info[else_count];
4355 73983 : rtx set = single_set (else_insn);
4356 73983 : gcc_checking_assert (set && REG_P (SET_DEST (set))
4357 : && !HARD_REGISTER_P (SET_DEST (set)));
4358 :
4359 73983 : rtx target = SET_DEST (set);
4360 73983 : rtx value = copy_rtx (SET_SRC (set));
4361 73983 : int i, ii;
4362 153737 : FOR_EACH_VEC_ELT (info->rewired_src, i, ii)
4363 5771 : value = simplify_replace_rtx (value,
4364 5771 : else_insn_info[ii]->target,
4365 5771 : else_insn_info[ii]->temporary);
4366 :
4367 73983 : set_curr_insn_location (else_insn->location ());
4368 73983 : rtx temporary = copy_to_mode_reg (GET_MODE (target), value);
4369 :
4370 73983 : info->temporary = temporary;
4371 73983 : else_count++;
4372 : }
4373 :
4374 44190 : set_curr_insn_location (saved_location);
4375 :
4376 88380 : gcc_checking_assert (else_count == (int) else_insn_info.length ());
4377 :
4378 : /* These insns run ahead of the conditional moves. If they change a
4379 : register the comparison reads we cannot reuse it, so bail. If they
4380 : only clobber the condition code, drop the shared compare so that every
4381 : move re-materializes its own. */
4382 :
4383 44190 : rtx_insn *first_else
4384 44190 : = before_else ? NEXT_INSN (before_else) : get_insns ();
4385 111781 : for (rtx_insn *ei = first_else; ei; ei = NEXT_INSN (ei))
4386 : {
4387 69396 : if (modified_in_p (cond, ei))
4388 : {
4389 1805 : end_sequence ();
4390 1805 : return false;
4391 : }
4392 67591 : if (cc_cmp
4393 67591 : && (modified_in_p (cc_cmp, ei)
4394 36058 : || (rev_cc_cmp && modified_in_p (rev_cc_cmp, ei))))
4395 : {
4396 : cc_cmp = NULL_RTX;
4397 : rev_cc_cmp = NULL_RTX;
4398 : }
4399 : }
4400 : }
4401 :
4402 733894 : for (count = 0; count < (int) insn_info.length (); ++count)
4403 : {
4404 269735 : noce_multiple_sets_info *info = insn_info[count];
4405 269735 : rtx_insn *insn = info->unmodified_insn;
4406 269735 : rtx set = single_set (insn);
4407 269735 : gcc_checking_assert (set);
4408 :
4409 269735 : rtx target = info->target;
4410 269735 : rtx temp;
4411 269735 : rtx new_val;
4412 269735 : rtx old_val;
4413 :
4414 269735 : if ((unsigned) count < then_count)
4415 : {
4416 255224 : new_val = SET_SRC (set);
4417 :
4418 255224 : int i, ii;
4419 315740 : FOR_EACH_VEC_ELT (info->rewired_src, i, ii)
4420 60516 : new_val = simplify_replace_rtx (new_val, insn_info[ii]->target,
4421 60516 : insn_info[ii]->temporary);
4422 :
4423 255224 : old_val = target;
4424 :
4425 : /* Use the final value assigned to TARGET on the else arm.
4426 : Scanning in reverse is important when the arm assigns the same
4427 : register more than once. */
4428 255224 : if (if_info->else_bb)
4429 336066 : for (int j = else_insn_info.length () - 1; j >= 0; --j)
4430 157972 : if (rtx_equal_p (target, else_insn_info[j]->target))
4431 : {
4432 49172 : old_val = else_insn_info[j]->temporary;
4433 49172 : break;
4434 : }
4435 : }
4436 : else
4437 : {
4438 14511 : unsigned else_index
4439 14511 : = else_only_indices[(unsigned) count - then_count];
4440 14511 : noce_multiple_sets_info *else_info = else_insn_info[else_index];
4441 14511 : gcc_checking_assert (rtx_equal_p (target, else_info->target));
4442 :
4443 14511 : new_val = target;
4444 14511 : old_val = else_info->temporary;
4445 : }
4446 :
4447 : /* As we are transforming
4448 : if (x > y)
4449 : {
4450 : a = b;
4451 : c = d;
4452 : }
4453 : into
4454 : a = (x > y) ...
4455 : c = (x > y) ...
4456 :
4457 : we potentially check x > y before every set.
4458 : Even though the check might be removed by subsequent passes, this means
4459 : that we cannot transform
4460 : if (x > y)
4461 : {
4462 : x = y;
4463 : ...
4464 : }
4465 : into
4466 : x = (x > y) ...
4467 : ...
4468 : since this would invalidate x and the following to-be-removed checks.
4469 : Therefore we introduce a temporary every time we are about to
4470 : overwrite a variable used in the check. Costing of a sequence with
4471 : these is going to be inaccurate so only use temporaries when
4472 : needed.
4473 :
4474 : If performing a second try, we know how many insns require a
4475 : temporary. For the last of these, we can omit creating one. */
4476 269735 : if (reg_overlap_mentioned_p (target, cond)
4477 269735 : && (!second_try || count < *last_needs_comparison))
4478 26952 : temp = gen_reg_rtx (GET_MODE (target));
4479 : else
4480 : temp = target;
4481 :
4482 : /* We have identified swap-style idioms before. A normal
4483 : set will need to be a cmov while the first instruction of a swap-style
4484 : idiom can be a regular move. This helps with costing. */
4485 269735 : bool need_cmov = info->need_cmov;
4486 :
4487 : /* If we had a non-canonical conditional jump (i.e. one where
4488 : the fallthrough is to the "else" case) we need to reverse
4489 : the conditional select. */
4490 269735 : if (if_info->then_else_reversed)
4491 54369 : std::swap (old_val, new_val);
4492 :
4493 : /* Try emitting a conditional move passing the backend the
4494 : canonicalized comparison. The backend is then able to
4495 : recognize expressions like
4496 :
4497 : if (x > y)
4498 : y = x;
4499 :
4500 : as min/max and emit an insn, accordingly. */
4501 269735 : unsigned cost1 = 0, cost2 = 0;
4502 269735 : rtx_insn *seq, *seq1, *seq2 = NULL;
4503 269735 : rtx temp_dest = NULL_RTX, temp_dest1 = NULL_RTX, temp_dest2 = NULL_RTX;
4504 269735 : bool read_comparison = false;
4505 :
4506 269735 : seq1 = try_emit_cmove_seq (if_info, temp, cond,
4507 : new_val, old_val, need_cmov,
4508 : &cost1, &temp_dest1);
4509 :
4510 : /* Here, we try to pass the backend a non-canonicalized cc comparison
4511 : as well. This allows the backend to emit a cmov directly without
4512 : creating an additional compare for each. If successful, costing
4513 : is easier and this sequence is usually preferred. */
4514 269735 : if (cc_cmp)
4515 : {
4516 132629 : seq2 = try_emit_cmove_seq (if_info, temp, cond,
4517 : new_val, old_val, need_cmov,
4518 : &cost2, &temp_dest2, cc_cmp, rev_cc_cmp);
4519 :
4520 : /* The if_then_else in SEQ2 may be affected when cc_cmp/rev_cc_cmp is
4521 : clobbered. We can't safely use the sequence in this case. */
4522 132629 : if (noce_seq_clobbers_cc_cmp_p (seq2, cc_cmp, rev_cc_cmp))
4523 197643 : seq2 = NULL;
4524 : }
4525 :
4526 : /* The backend might have created a sequence that uses the
4527 : condition as a value. Check this. */
4528 :
4529 : /* We cannot handle anything more complex than a reg or constant. */
4530 269735 : if (!REG_P (XEXP (cond, 0)) && !CONSTANT_P (XEXP (cond, 0)))
4531 269735 : read_comparison = true;
4532 :
4533 269735 : if (!REG_P (XEXP (cond, 1)) && !CONSTANT_P (XEXP (cond, 1)))
4534 269735 : read_comparison = true;
4535 :
4536 269735 : rtx_insn *walk = seq2;
4537 269735 : int if_then_else_count = 0;
4538 349856 : while (walk && !read_comparison)
4539 : {
4540 80121 : rtx exprs_to_check[2];
4541 80121 : unsigned int exprs_count = 0;
4542 :
4543 80121 : rtx set = single_set (walk);
4544 80121 : if (set && XEXP (set, 1)
4545 80121 : && GET_CODE (XEXP (set, 1)) == IF_THEN_ELSE)
4546 : {
4547 : /* We assume that this is the cmove created by the backend that
4548 : naturally uses the condition. */
4549 50092 : exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 1);
4550 50092 : exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 2);
4551 50092 : if_then_else_count++;
4552 : }
4553 30029 : else if (NONDEBUG_INSN_P (walk))
4554 30029 : exprs_to_check[exprs_count++] = PATTERN (walk);
4555 :
4556 : /* Bail if we get more than one if_then_else because the assumption
4557 : above may be incorrect. */
4558 80121 : if (if_then_else_count > 1)
4559 : {
4560 0 : read_comparison = true;
4561 0 : break;
4562 : }
4563 :
4564 210334 : for (unsigned int i = 0; i < exprs_count; i++)
4565 : {
4566 130213 : subrtx_iterator::array_type array;
4567 290871 : FOR_EACH_SUBRTX (iter, array, exprs_to_check[i], NONCONST)
4568 191658 : if (*iter != NULL_RTX
4569 191658 : && (reg_overlap_mentioned_p (XEXP (cond, 0), *iter)
4570 173421 : || reg_overlap_mentioned_p (XEXP (cond, 1), *iter)))
4571 : {
4572 : read_comparison = true;
4573 : break;
4574 : }
4575 130213 : }
4576 :
4577 80121 : walk = NEXT_INSN (walk);
4578 : }
4579 :
4580 : /* Check which version is less expensive. */
4581 269735 : if (seq1 != NULL_RTX && (cost1 <= cost2 || seq2 == NULL_RTX))
4582 : {
4583 216838 : seq = seq1;
4584 216838 : temp_dest = temp_dest1;
4585 216838 : if (!second_try)
4586 109500 : *last_needs_comparison = count;
4587 : }
4588 1226 : else if (seq2 != NULL_RTX)
4589 : {
4590 51907 : seq = seq2;
4591 51907 : temp_dest = temp_dest2;
4592 51907 : if (!second_try && read_comparison)
4593 8761 : *last_needs_comparison = count;
4594 51907 : *use_cond_earliest = true;
4595 : }
4596 : else
4597 : {
4598 : /* Nothing worked, bail out. */
4599 990 : end_sequence ();
4600 1148 : return false;
4601 : }
4602 :
4603 : /* Although we use temporaries if there is register overlap of COND and
4604 : TARGET, it is possible that SEQ modifies COND anyway. For example,
4605 : COND may use the flags register and if INSN clobbers flags then
4606 : we may be unable to emit a valid sequence (e.g. in x86 that would
4607 : require saving and restoring the flags register). */
4608 161407 : if (!second_try)
4609 422134 : for (rtx_insn *iter = seq; iter; iter = NEXT_INSN (iter))
4610 286823 : if (modified_in_p (cond, iter))
4611 : {
4612 158 : end_sequence ();
4613 158 : return false;
4614 : }
4615 :
4616 : /* If SEQ clobbers registers mentioned in cc_cmp/rev_cc_cmp we have to
4617 : fall back on SEQ1 from that point on. Only check when we use SEQ1,
4618 : since SEQ2 has been tested already. */
4619 268587 : if (cc_cmp && seq == seq1
4620 268587 : && noce_seq_clobbers_cc_cmp_p (seq, cc_cmp, rev_cc_cmp))
4621 : {
4622 : cc_cmp = NULL_RTX;
4623 : rev_cc_cmp = NULL_RTX;
4624 : }
4625 :
4626 : /* End the sub sequence and emit to the main sequence. */
4627 268587 : emit_insn (seq);
4628 :
4629 : /* Bookkeeping. */
4630 268587 : info->temporary = temp_dest;
4631 : }
4632 :
4633 : /* Even if we did not actually need the comparison, we want to make sure
4634 : to try a second time in order to get rid of the temporaries. */
4635 97212 : if (*last_needs_comparison == -1)
4636 1037 : *last_needs_comparison = 0;
4637 :
4638 : return true;
4639 : }
4640 :
4641 : /* Fill INSN_INFO with one entry per active insn in BB. Find local swap-style
4642 : idioms and mark the first insn (1) that is only a temporary as not needing
4643 : a conditional move, as it is going to be dead afterwards anyway.
4644 :
4645 : (1) int tmp = a;
4646 : a = b;
4647 : b = tmp;
4648 :
4649 : ifcvt
4650 : -->
4651 :
4652 : tmp = a;
4653 : a = cond ? b : a_old;
4654 : b = cond ? tmp : b_old;
4655 :
4656 : Additionally, store the index of insns like (2) when a subsequent
4657 : SET reads from their destination.
4658 :
4659 : (2) int c = a;
4660 : int d = c;
4661 :
4662 : ifcvt
4663 : -->
4664 :
4665 : c = cond ? a : c_old;
4666 : d = cond ? d : c; // Need to use c rather than c_old here. */
4667 :
4668 : static void
4669 74625 : init_noce_multiple_sets_info (basic_block bb,
4670 : auto_delete_vec<noce_multiple_sets_info> &insn_info)
4671 : {
4672 74625 : rtx_insn *insn;
4673 74625 : int count = 0;
4674 74625 : auto_vec<rtx> dests;
4675 74625 : bitmap bb_live_out = df_get_live_out (bb);
4676 :
4677 : /* Iterate over all SETs, storing the destinations in DEST.
4678 : - If we encounter a previously changed register,
4679 : rewire the read to the original source.
4680 : - If we encounter a SET that writes to a destination
4681 : that is not live after this block then the register
4682 : does not need to be moved conditionally. */
4683 500255 : FOR_BB_INSNS (bb, insn)
4684 : {
4685 425630 : if (!active_insn_p (insn))
4686 247806 : continue;
4687 :
4688 177824 : noce_multiple_sets_info *info = new noce_multiple_sets_info;
4689 177824 : info->temporary = NULL_RTX;
4690 177824 : insn_info.safe_push (info);
4691 :
4692 177824 : rtx set = single_set (insn);
4693 177824 : gcc_checking_assert (set);
4694 :
4695 177824 : rtx src = SET_SRC (set);
4696 177824 : rtx dest = SET_DEST (set);
4697 :
4698 177824 : gcc_checking_assert (REG_P (dest) && !HARD_REGISTER_P (dest));
4699 177824 : info->target = dest;
4700 177824 : info->unmodified_insn = insn;
4701 177824 : info->need_cmov = bitmap_bit_p (bb_live_out, REGNO (dest));
4702 :
4703 : /* Check if the current SET's source mentions any previously seen
4704 : destination. Keep only the newest definition of each pseudo register.
4705 : This is quadratic but the number of insns in BB
4706 : is bounded by PARAM_MAX_RTL_IF_CONVERSION_INSNS. */
4707 177824 : auto_bitmap rewired_regs;
4708 352281 : for (int i = count - 1; i >= 0; --i)
4709 174457 : if (reg_mentioned_p (dests[i], src)
4710 174457 : && bitmap_set_bit (rewired_regs, REGNO (dests[i])))
4711 34375 : insn_info[count]->rewired_src.safe_push (i);
4712 :
4713 177824 : dests.safe_push (dest);
4714 177824 : count++;
4715 177824 : }
4716 74625 : }
4717 :
4718 : /* Return true iff basic block TEST_BB is suitable for conversion to a
4719 : series of conditional moves. Unless REQUIRE_MULTIPLE is false, also check
4720 : that we have more than one set (other routines can handle a single set
4721 : better than we would). A diamond arm may have a single set.
4722 : Require no more than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets. While going
4723 : through the insns, store the sum of their potential costs in COST. On
4724 : success, if LIVE_OUT_DESTS is nonnull, record the distinct pseudo
4725 : destinations that are live out of
4726 : TEST_BB. If HAS_NON_SIMPLE_SRC is nonnull, set it when an instruction
4727 : source is neither a constant nor a register operand. On success, if
4728 : INSN_COUNT is nonnull, store the number of active sets in it. */
4729 :
4730 : static bool
4731 2033846 : bb_ok_for_noce_convert_multiple_sets (basic_block test_bb, unsigned *cost,
4732 : bool require_multiple = true,
4733 : bitmap live_out_dests = NULL,
4734 : bool *has_non_simple_src = NULL,
4735 : unsigned *insn_count = NULL)
4736 : {
4737 2033846 : rtx_insn *insn;
4738 2033846 : unsigned count = 0;
4739 2033846 : unsigned param = param_max_rtl_if_conversion_insns;
4740 2033846 : bool speed_p = optimize_bb_for_speed_p (test_bb);
4741 2033846 : unsigned potential_cost = 0;
4742 2033846 : if (live_out_dests)
4743 963462 : bitmap_clear (live_out_dests);
4744 2033846 : if (has_non_simple_src)
4745 963462 : *has_non_simple_src = false;
4746 2033846 : bitmap bb_live_out = NULL;
4747 2033846 : if (live_out_dests)
4748 963462 : bb_live_out = df_get_live_out (test_bb);
4749 :
4750 11934533 : FOR_BB_INSNS (test_bb, insn)
4751 : {
4752 : /* Skip over notes etc. */
4753 11561212 : if (!active_insn_p (insn))
4754 8947449 : continue;
4755 :
4756 : /* We only handle SET insns. */
4757 2613763 : rtx set = single_set (insn);
4758 2613763 : if (set == NULL_RTX)
4759 : return false;
4760 :
4761 2537361 : rtx dest = SET_DEST (set);
4762 2537361 : rtx src = SET_SRC (set);
4763 :
4764 2537361 : if (has_non_simple_src && !noce_simple_cmove_operand_p (src))
4765 597799 : *has_non_simple_src = true;
4766 :
4767 : /* Dependency rewiring is keyed by register number, so restrict
4768 : destinations to pseudos. Hard-register definitions can overlap
4769 : without having the same mode. Do not handle anything involving
4770 : memory loads/stores since it might violate data-race-freedom
4771 : guarantees. Make sure we can force SRC to a register as that may
4772 : be needed in try_emit_cmove_seq. */
4773 2108163 : if (!REG_P (dest) || HARD_REGISTER_P (dest)
4774 1664649 : || contains_mem_rtx_p (src)
4775 3564141 : || !noce_can_force_operand (src))
4776 : return false;
4777 :
4778 : /* Destination and source must be appropriate. */
4779 1008768 : if (!noce_operand_ok (dest) || !noce_operand_ok (src))
4780 : return false;
4781 :
4782 : /* We must be able to conditionally move in this mode. */
4783 983223 : if (!can_conditionally_move_p (GET_MODE (dest)))
4784 : return false;
4785 :
4786 953238 : if (bb_live_out && bitmap_bit_p (bb_live_out, REGNO (dest)))
4787 408345 : bitmap_set_bit (live_out_dests, REGNO (dest));
4788 :
4789 953238 : potential_cost += insn_cost (insn, speed_p);
4790 :
4791 953238 : count++;
4792 : }
4793 :
4794 373321 : *cost += potential_cost;
4795 :
4796 : /* If we would only put out one conditional move, the other strategies
4797 : this pass tries are better optimized and will be more appropriate, so
4798 : require more than one set unless REQUIRE_MULTIPLE is false. A diamond
4799 : arm may have one set.
4800 : Some targets want to strictly limit the number of conditional moves
4801 : that are emitted, they set this through PARAM, we need to respect
4802 : that. */
4803 630288 : bool ok = count >= (require_multiple ? 2u : 1u) && count <= param;
4804 373321 : if (ok && insn_count)
4805 255236 : *insn_count = count;
4806 : return ok;
4807 : }
4808 :
4809 : /* Compute average of two given costs weighted by relative probabilities
4810 : of respective basic blocks in an IF-THEN-ELSE. E is the IF-THEN edge.
4811 : With P as the probability to take the IF-THEN branch, return
4812 : P * THEN_COST + (1 - P) * ELSE_COST. Evaluate this as
4813 : ELSE_COST + P * (THEN_COST - ELSE_COST) when THEN_COST >= ELSE_COST, and
4814 : THEN_COST + (1 - P) * (ELSE_COST - THEN_COST) otherwise. Both forms pass
4815 : a nonnegative value to profile_probability::apply and make its rounding
4816 : independent of the CFG arm order. */
4817 : static unsigned
4818 279106 : average_cost (unsigned then_cost, unsigned else_cost, edge e)
4819 : {
4820 279106 : if (then_cost < else_cost)
4821 9736 : return then_cost
4822 9736 : + e->probability.invert ().apply ((gcov_type) else_cost - then_cost);
4823 :
4824 269370 : return else_cost
4825 269370 : + e->probability.apply ((gcov_type) then_cost - else_cost);
4826 : }
4827 :
4828 : /* Return the estimated cost of the original, un-converted if-region described
4829 : by IF_INFO whose THEN and ELSE arms cost THEN_COST and ELSE_COST. BASE_COST
4830 : already accounts for the branch, and any compare, that the conversion
4831 : removes. When optimizing for speed (SPEED_P) only one arm runs, so charge
4832 : the branch-probability-weighted average of the two. When optimizing for
4833 : size both arms are emitted, so sum them. */
4834 :
4835 : static unsigned
4836 323224 : noce_original_region_cost (const noce_if_info *if_info, bool speed_p,
4837 : unsigned base_cost, unsigned then_cost,
4838 : unsigned else_cost)
4839 : {
4840 323224 : if (speed_p)
4841 279106 : return base_cost + average_cost (then_cost, else_cost,
4842 279106 : find_edge (if_info->test_bb,
4843 558212 : if_info->then_bb));
4844 44118 : return base_cost + then_cost + else_cost;
4845 : }
4846 :
4847 : /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
4848 : it without using conditional execution. Return TRUE if we were successful
4849 : at converting the block. */
4850 :
4851 : static bool
4852 1895932 : noce_process_if_block (noce_if_info *if_info)
4853 : {
4854 1895932 : basic_block test_bb = if_info->test_bb; /* test block */
4855 1895932 : basic_block then_bb = if_info->then_bb; /* THEN */
4856 1895932 : basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
4857 1895932 : rtx_insn *jump = if_info->jump;
4858 1895932 : rtx cond = if_info->cond;
4859 1895932 : rtx_insn *insn_a, *insn_b;
4860 1895932 : rtx set_a, set_b;
4861 1895932 : rtx orig_x, x, a, b;
4862 :
4863 : /* We're looking for patterns of the form
4864 :
4865 : (1) if (...) x = a; else x = b;
4866 : (2) x = b; if (...) x = a;
4867 : (3) if (...) x = a; // as if with an initial x = x.
4868 : (4) if (...) { x = a; y = b; z = c; } // Like 3, for multiple SETS.
4869 : (5) A multi-set IF-THEN-ELSE-JOIN.
4870 : The later patterns require jumps to be more expensive.
4871 : For the diamond case, defer diamonds with only register and constant
4872 : assignments in both arms to the existing simple conditional-move
4873 : handling. Require at least two distinct live-out pseudos across the
4874 : arms.
4875 : ??? For future expansion, further expand the "multiple X" rules. */
4876 :
4877 : /* The base cost recorded so far covers the branch, and any compare, that
4878 : if-conversion removes. Each candidate path below derives its estimate
4879 : from this base plus the cost of the arms it folds in. If a target
4880 : re-uses the existing CC comparison, noce_convert_multiple_sets accounts
4881 : for that against the base before it calls
4882 : noce_conversion_profitable_p. */
4883 1895932 : unsigned base_cost = if_info->original_cost;
4884 1895932 : bool speed_p = optimize_bb_for_speed_p (test_bb);
4885 :
4886 1895932 : unsigned ms_then_cost = 0, ms_else_cost = 0;
4887 1895932 : unsigned ms_then_insn_count = 0, ms_else_insn_count = 0;
4888 1895932 : bool ms_then_has_non_simple_src, ms_else_has_non_simple_src;
4889 1895932 : auto_bitmap ms_then_live_out_dests, ms_else_live_out_dests;
4890 1895932 : noce_if_info ms_if_info = *if_info;
4891 1895932 : bool multiple_sets_p = false;
4892 :
4893 1895932 : if (HAVE_conditional_move)
4894 : {
4895 1895932 : if (!else_bb)
4896 1070384 : multiple_sets_p
4897 1070384 : = bb_ok_for_noce_convert_multiple_sets (then_bb, &ms_then_cost);
4898 825548 : else if (!if_info->then_else_reversed
4899 825548 : && bb_ok_for_noce_convert_multiple_sets
4900 825548 : (then_bb, &ms_then_cost, false,
4901 : ms_then_live_out_dests, &ms_then_has_non_simple_src,
4902 : &ms_then_insn_count)
4903 963462 : && bb_ok_for_noce_convert_multiple_sets
4904 137914 : (else_bb, &ms_else_cost, false,
4905 : ms_else_live_out_dests, &ms_else_has_non_simple_src,
4906 : &ms_else_insn_count))
4907 : {
4908 117322 : auto_bitmap ms_live_out_dests;
4909 117322 : bitmap_ior (ms_live_out_dests, ms_then_live_out_dests,
4910 117322 : ms_else_live_out_dests);
4911 117322 : unsigned output_count = bitmap_count_bits (ms_live_out_dests);
4912 :
4913 117322 : if (output_count >= 2
4914 50153 : && output_count <= (unsigned) param_max_rtl_if_conversion_insns
4915 50151 : && (ms_then_has_non_simple_src || ms_else_has_non_simple_src))
4916 117322 : multiple_sets_p = true;
4917 117322 : }
4918 : }
4919 :
4920 : /* The diamond conversion evaluates the else arm unconditionally.
4921 : Honor the target limit on the number of instructions made
4922 : unconditional. */
4923 1187706 : if (multiple_sets_p
4924 54658 : && ms_if_info.else_bb
4925 1213871 : && ms_else_insn_count > (unsigned) MAX_CONDITIONAL_EXECUTE)
4926 : multiple_sets_p = false;
4927 :
4928 1892833 : if (multiple_sets_p)
4929 : {
4930 : /* The original code runs the comparison and one arm. Estimate that cost
4931 : and let noce_convert_multiple_sets convert only if the conditional
4932 : moves come out cheaper. */
4933 51559 : ms_if_info.original_cost
4934 103118 : = ms_if_info.else_bb
4935 51559 : ? noce_original_region_cost (&ms_if_info, speed_p, base_cost,
4936 : ms_then_cost, ms_else_cost)
4937 28493 : : base_cost + ms_then_cost;
4938 51559 : if (noce_convert_multiple_sets (&ms_if_info))
4939 : {
4940 33571 : if (dump_file && ms_if_info.transform_name)
4941 3 : fprintf (dump_file, "if-conversion succeeded through %s\n",
4942 : ms_if_info.transform_name);
4943 : return true;
4944 : }
4945 : }
4946 :
4947 1862361 : unsigned int then_cost = 0, else_cost = 0;
4948 1862361 : if (!bb_valid_for_noce_process_p (then_bb, cond, &then_cost,
4949 : &if_info->then_simple))
4950 : return false;
4951 :
4952 483902 : if (else_bb
4953 483902 : && !bb_valid_for_noce_process_p (else_bb, cond, &else_cost,
4954 : &if_info->else_simple))
4955 : return false;
4956 :
4957 300158 : if_info->original_cost
4958 300158 : = noce_original_region_cost (if_info, speed_p, base_cost, then_cost,
4959 : else_cost);
4960 :
4961 300158 : insn_a = last_active_insn (then_bb, false);
4962 300158 : set_a = single_set (insn_a);
4963 300158 : gcc_assert (set_a);
4964 :
4965 300158 : x = SET_DEST (set_a);
4966 300158 : a = SET_SRC (set_a);
4967 :
4968 : /* Look for the other potential set. Make sure we've got equivalent
4969 : destinations. */
4970 : /* ??? This is overconservative. Storing to two different mems is
4971 : as easy as conditionally computing the address. Storing to a
4972 : single mem merely requires a scratch memory to use as one of the
4973 : destination addresses; often the memory immediately below the
4974 : stack pointer is available for this. */
4975 300158 : set_b = NULL_RTX;
4976 300158 : if (else_bb)
4977 : {
4978 98250 : insn_b = last_active_insn (else_bb, false);
4979 98250 : set_b = single_set (insn_b);
4980 98250 : gcc_assert (set_b);
4981 :
4982 98250 : if (!rtx_interchangeable_p (x, SET_DEST (set_b)))
4983 : return false;
4984 : }
4985 : else
4986 : {
4987 201908 : insn_b = if_info->cond_earliest;
4988 545585 : do
4989 545585 : insn_b = prev_nonnote_nondebug_insn (insn_b);
4990 : while (insn_b
4991 542778 : && (BLOCK_FOR_INSN (insn_b)
4992 542778 : == BLOCK_FOR_INSN (if_info->cond_earliest))
4993 1202319 : && !modified_in_p (x, insn_b));
4994 :
4995 : /* We're going to be moving the evaluation of B down from above
4996 : COND_EARLIEST to JUMP. Make sure the relevant data is still
4997 : intact. */
4998 201908 : if (! insn_b
4999 199101 : || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
5000 111149 : || !NONJUMP_INSN_P (insn_b)
5001 104443 : || (set_b = single_set (insn_b)) == NULL_RTX
5002 102599 : || ! rtx_interchangeable_p (x, SET_DEST (set_b))
5003 87885 : || ! noce_operand_ok (SET_SRC (set_b))
5004 85879 : || reg_overlap_mentioned_p (x, SET_SRC (set_b))
5005 84443 : || modified_between_p (SET_SRC (set_b), insn_b, jump)
5006 : /* Avoid extending the lifetime of hard registers on small
5007 : register class machines. */
5008 82333 : || (REG_P (SET_SRC (set_b))
5009 21047 : && HARD_REGISTER_P (SET_SRC (set_b))
5010 3734 : && targetm.small_register_classes_for_mode_p
5011 3734 : (GET_MODE (SET_SRC (set_b))))
5012 : /* Likewise with X. In particular this can happen when
5013 : noce_get_condition looks farther back in the instruction
5014 : stream than one might expect. */
5015 78599 : || reg_overlap_mentioned_p (x, cond)
5016 65319 : || reg_overlap_mentioned_p (x, a)
5017 262365 : || modified_between_p (x, insn_b, jump))
5018 : {
5019 : insn_b = NULL;
5020 : set_b = NULL_RTX;
5021 : }
5022 : }
5023 :
5024 : /* If x has side effects then only the if-then-else form is safe to
5025 : convert. But even in that case we would need to restore any notes
5026 : (such as REG_INC) at then end. That can be tricky if
5027 : noce_emit_move_insn expands to more than one insn, so disable the
5028 : optimization entirely for now if there are side effects. */
5029 294833 : if (side_effects_p (x))
5030 : return false;
5031 :
5032 294833 : b = (set_b ? SET_SRC (set_b) : x);
5033 :
5034 : /* Only operate on register destinations, and even then avoid extending
5035 : the lifetime of hard registers on small register class machines. */
5036 294833 : orig_x = x;
5037 294833 : if_info->orig_x = orig_x;
5038 294833 : if (!REG_P (x)
5039 294833 : || (HARD_REGISTER_P (x)
5040 2 : && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
5041 : {
5042 69880 : if (GET_MODE (x) == BLKmode)
5043 : return false;
5044 :
5045 69638 : if (GET_CODE (x) == ZERO_EXTRACT
5046 1 : && (!CONST_INT_P (XEXP (x, 1))
5047 1 : || !CONST_INT_P (XEXP (x, 2))))
5048 : return false;
5049 :
5050 69638 : x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
5051 : ? XEXP (x, 0) : x));
5052 : }
5053 :
5054 : /* Don't operate on sources that may trap or are volatile. */
5055 294591 : if (! noce_operand_ok (a) || ! noce_operand_ok (b))
5056 : return false;
5057 :
5058 322276 : retry:
5059 : /* Set up the info block for our subroutines. */
5060 322276 : if_info->insn_a = insn_a;
5061 322276 : if_info->insn_b = insn_b;
5062 322276 : if_info->x = x;
5063 322276 : if_info->a = a;
5064 322276 : if_info->b = b;
5065 :
5066 : /* Try optimizations in some approximation of a useful order. */
5067 : /* ??? Should first look to see if X is live incoming at all. If it
5068 : isn't, we don't need anything but an unconditional set. */
5069 :
5070 : /* Look and see if A and B are really the same. Avoid creating silly
5071 : cmove constructs that no one will fix up later. */
5072 322276 : if (noce_simple_bbs (if_info)
5073 299784 : && rtx_interchangeable_p (a, b))
5074 : {
5075 : /* If we have an INSN_B, we don't have to create any new rtl. Just
5076 : move the instruction that we already have. If we don't have an
5077 : INSN_B, that means that A == X, and we've got a noop move. In
5078 : that case don't do anything and let the code below delete INSN_A. */
5079 342 : if (insn_b && else_bb)
5080 : {
5081 333 : rtx note;
5082 :
5083 333 : if (insn_b == BB_END (else_bb))
5084 223 : BB_END (else_bb) = PREV_INSN (insn_b);
5085 333 : reorder_insns (insn_b, insn_b, PREV_INSN (jump));
5086 :
5087 : /* If there was a REG_EQUAL note, delete it since it may have been
5088 : true due to this insn being after a jump. */
5089 333 : if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
5090 0 : remove_note (insn_b, note);
5091 :
5092 342 : insn_b = NULL;
5093 : }
5094 : /* If we have "x = b; if (...) x = a;", and x has side-effects, then
5095 : x must be executed twice. */
5096 9 : else if (insn_b && side_effects_p (orig_x))
5097 : return false;
5098 :
5099 342 : x = orig_x;
5100 342 : goto success;
5101 : }
5102 :
5103 321934 : if (!set_b && MEM_P (orig_x))
5104 : /* We want to avoid store speculation to avoid cases like
5105 : if (pthread_mutex_trylock(mutex))
5106 : ++global_variable;
5107 : Rather than go to much effort here, we rely on the SSA optimizers,
5108 : which do a good enough job these days. */
5109 : return false;
5110 :
5111 254000 : if (noce_try_move (if_info))
5112 0 : goto success;
5113 254000 : if (noce_try_ifelse_collapse (if_info))
5114 17452 : goto success;
5115 236548 : if (noce_try_store_flag (if_info))
5116 26199 : goto success;
5117 210349 : if (noce_try_bitop (if_info))
5118 0 : goto success;
5119 210349 : if (noce_try_minmax (if_info))
5120 64 : goto success;
5121 210285 : if (noce_try_abs (if_info))
5122 24 : goto success;
5123 210261 : if (noce_try_inverse_constants (if_info))
5124 0 : goto success;
5125 210261 : if (!targetm.have_conditional_execution ()
5126 210261 : && noce_try_store_flag_constants (if_info))
5127 3129 : goto success;
5128 207132 : if (!targetm.have_conditional_execution ()
5129 207132 : && noce_try_shifted_store_flag (if_info))
5130 5477 : goto success;
5131 201655 : if (noce_try_sign_bit_splat (if_info))
5132 178 : goto success;
5133 201477 : if (!targetm.have_conditional_execution ()
5134 201477 : && noce_try_store_flag_logical (if_info))
5135 0 : goto success;
5136 201477 : if (HAVE_conditional_move
5137 201477 : && noce_try_cmove (if_info))
5138 42978 : goto success;
5139 158499 : if (! targetm.have_conditional_execution ())
5140 : {
5141 158499 : if (noce_try_addcc (if_info))
5142 921 : goto success;
5143 157578 : if (noce_try_store_flag_mask (if_info))
5144 9 : goto success;
5145 157569 : if (HAVE_conditional_move
5146 157569 : && noce_try_cond_arith (if_info))
5147 10163 : goto success;
5148 147406 : if (HAVE_conditional_move
5149 147406 : && noce_try_cmove_arith (if_info))
5150 35477 : goto success;
5151 111929 : if (noce_try_sign_mask (if_info))
5152 0 : goto success;
5153 : }
5154 :
5155 111929 : if (!else_bb && set_b)
5156 : {
5157 27685 : insn_b = NULL;
5158 27685 : set_b = NULL_RTX;
5159 27685 : b = orig_x;
5160 27685 : goto retry;
5161 : }
5162 :
5163 : return false;
5164 :
5165 142413 : success:
5166 142413 : if (dump_file && if_info->transform_name)
5167 8 : fprintf (dump_file, "if-conversion succeeded through %s\n",
5168 : if_info->transform_name);
5169 :
5170 : /* If we used a temporary, fix it up now. */
5171 142413 : if (orig_x != x)
5172 : {
5173 683 : rtx_insn *seq;
5174 :
5175 683 : start_sequence ();
5176 683 : noce_emit_move_insn (orig_x, x);
5177 683 : seq = get_insns ();
5178 683 : set_used_flags (orig_x);
5179 683 : unshare_all_rtl_in_chain (seq);
5180 683 : end_sequence ();
5181 :
5182 683 : emit_insn_before_setloc (seq, BB_END (test_bb), insn_a->location ());
5183 : }
5184 :
5185 : /* The original THEN and ELSE blocks may now be removed and the test block
5186 : redirected to the join block. */
5187 142413 : noce_finish_if_conversion (if_info);
5188 :
5189 142413 : return true;
5190 1895932 : }
5191 :
5192 : /* Check whether a block is suitable for conditional move conversion.
5193 : Every insn must be a simple set of a register to a constant or a
5194 : register. For each assignment, store the value in the pointer map
5195 : VALS, keyed by register pointer, then store the register pointer in
5196 : REGS. COND is the condition we will test. */
5197 :
5198 : static bool
5199 1750147 : check_cond_move_block (basic_block bb,
5200 : hash_map<rtx, rtx> *vals,
5201 : vec<rtx> *regs,
5202 : rtx cond)
5203 : {
5204 1750147 : rtx_insn *insn;
5205 1750147 : rtx cc = cc_in_cond (cond);
5206 :
5207 : /* We can only handle simple jumps at the end of the basic block.
5208 : It is almost impossible to update the CFG otherwise. */
5209 1750147 : insn = BB_END (bb);
5210 1750147 : if (JUMP_P (insn) && !onlyjump_p (insn))
5211 : return false;
5212 :
5213 9934047 : FOR_BB_INSNS (bb, insn)
5214 : {
5215 9871345 : rtx set, dest, src;
5216 :
5217 9871345 : if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
5218 8048364 : continue;
5219 1822981 : set = single_set (insn);
5220 1822981 : if (!set)
5221 1687392 : return false;
5222 :
5223 1748668 : dest = SET_DEST (set);
5224 1748668 : src = SET_SRC (set);
5225 1748668 : if (!REG_P (dest)
5226 1748668 : || (HARD_REGISTER_P (dest)
5227 319554 : && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
5228 : return false;
5229 :
5230 1034398 : if (!noce_simple_cmove_operand_p (src))
5231 : return false;
5232 :
5233 158036 : if (side_effects_p (src) || side_effects_p (dest))
5234 : return false;
5235 :
5236 158036 : if (may_trap_p (src) || may_trap_p (dest))
5237 : return false;
5238 :
5239 : /* Don't try to handle this if the source register was
5240 : modified earlier in the block. */
5241 158036 : if ((REG_P (src)
5242 51771 : && vals->get (src))
5243 209552 : || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
5244 5376 : && vals->get (SUBREG_REG (src))))
5245 : return false;
5246 :
5247 : /* Don't try to handle this if the destination register was
5248 : modified earlier in the block. */
5249 157726 : if (vals->get (dest))
5250 : return false;
5251 :
5252 : /* Don't try to handle this if the condition uses the
5253 : destination register. */
5254 157726 : if (reg_overlap_mentioned_p (dest, cond))
5255 : return false;
5256 :
5257 : /* Don't try to handle this if the source register is modified
5258 : later in the block. */
5259 139000 : if (!CONSTANT_P (src)
5260 139000 : && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
5261 : return false;
5262 :
5263 : /* Skip it if the instruction to be moved might clobber CC. */
5264 135589 : if (cc && set_of (cc, insn))
5265 : return false;
5266 :
5267 135589 : vals->put (dest, src);
5268 :
5269 135589 : regs->safe_push (dest);
5270 : }
5271 :
5272 : return true;
5273 : }
5274 :
5275 : /* Given a basic block BB suitable for conditional move conversion,
5276 : a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
5277 : the register values depending on COND, emit the insns in the block as
5278 : conditional moves. If ELSE_BLOCK is true, THEN_BB was already
5279 : processed. The caller has started a sequence for the conversion.
5280 : Return true if successful, false if something goes wrong. */
5281 :
5282 : static bool
5283 36781 : cond_move_convert_if_block (noce_if_info *if_infop,
5284 : basic_block bb, rtx cond,
5285 : hash_map<rtx, rtx> *then_vals,
5286 : hash_map<rtx, rtx> *else_vals,
5287 : bool else_block_p)
5288 : {
5289 36781 : enum rtx_code code;
5290 36781 : rtx_insn *insn;
5291 36781 : rtx cond_arg0, cond_arg1;
5292 :
5293 36781 : code = GET_CODE (cond);
5294 36781 : cond_arg0 = XEXP (cond, 0);
5295 36781 : cond_arg1 = XEXP (cond, 1);
5296 :
5297 123269 : FOR_BB_INSNS (bb, insn)
5298 : {
5299 101034 : rtx set, target, dest, t, e;
5300 :
5301 : /* ??? Maybe emit conditional debug insn? */
5302 101034 : if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
5303 66010 : continue;
5304 45671 : set = single_set (insn);
5305 45671 : gcc_assert (set && REG_P (SET_DEST (set)));
5306 :
5307 45671 : dest = SET_DEST (set);
5308 :
5309 45671 : rtx *then_slot = then_vals->get (dest);
5310 45671 : rtx *else_slot = else_vals->get (dest);
5311 45671 : t = then_slot ? *then_slot : NULL_RTX;
5312 45671 : e = else_slot ? *else_slot : NULL_RTX;
5313 :
5314 45671 : if (else_block_p)
5315 : {
5316 : /* If this register was set in the then block, we already
5317 : handled this case there. */
5318 11816 : if (t)
5319 10647 : continue;
5320 1169 : t = dest;
5321 1169 : gcc_assert (e);
5322 : }
5323 : else
5324 : {
5325 33855 : gcc_assert (t);
5326 33855 : if (!e)
5327 22441 : e = dest;
5328 : }
5329 :
5330 35024 : if (if_infop->cond_inverted)
5331 0 : std::swap (t, e);
5332 :
5333 35024 : target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
5334 : t, e);
5335 35024 : if (!target)
5336 14546 : return false;
5337 :
5338 20478 : if (target != dest)
5339 0 : noce_emit_move_insn (dest, target);
5340 : }
5341 :
5342 : return true;
5343 : }
5344 :
5345 : /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
5346 : it using only conditional moves. Return TRUE if we were successful at
5347 : converting the block. */
5348 :
5349 : static bool
5350 1719948 : cond_move_process_if_block (noce_if_info *if_info)
5351 : {
5352 1719948 : basic_block then_bb = if_info->then_bb;
5353 1719948 : basic_block else_bb = if_info->else_bb;
5354 1719948 : rtx_insn *jump = if_info->jump;
5355 1719948 : rtx cond = if_info->cond;
5356 1719948 : rtx_insn *seq, *loc_insn;
5357 1719948 : int c;
5358 1719948 : auto_vec<rtx> then_regs;
5359 1719948 : auto_vec<rtx> else_regs;
5360 1719948 : int limit = param_max_rtl_if_conversion_insns;
5361 :
5362 : /* Build a mapping for each block to the value used for each
5363 : register. */
5364 1719948 : hash_map<rtx, rtx> then_vals;
5365 1719948 : hash_map<rtx, rtx> else_vals;
5366 :
5367 : /* Make sure the blocks are suitable. */
5368 1719948 : if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
5369 1719948 : || (else_bb
5370 30199 : && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
5371 : return false;
5372 :
5373 : /* Make sure the blocks can be used together. If the same register
5374 : is set in both blocks, and is not set to a constant in both
5375 : cases, then both blocks must set it to the same register. We
5376 : have already verified that if it is set to a register, that the
5377 : source register does not change after the assignment. Also count
5378 : the number of registers set in only one of the blocks. */
5379 32503 : c = 0;
5380 132106 : for (rtx reg : then_regs)
5381 : {
5382 38262 : rtx *then_slot = then_vals.get (reg);
5383 38262 : rtx *else_slot = else_vals.get (reg);
5384 :
5385 38262 : gcc_checking_assert (then_slot);
5386 38262 : if (!else_slot)
5387 22990 : ++c;
5388 : else
5389 : {
5390 15272 : rtx then_val = *then_slot;
5391 15272 : rtx else_val = *else_slot;
5392 4491 : if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
5393 19019 : && !rtx_equal_p (then_val, else_val))
5394 3241 : return false;
5395 : }
5396 : }
5397 :
5398 : /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
5399 59028 : for (rtx reg : else_regs)
5400 : {
5401 13172 : gcc_checking_assert (else_vals.get (reg));
5402 13172 : if (!then_vals.get (reg))
5403 1292 : ++c;
5404 : }
5405 :
5406 : /* Make sure it is reasonable to convert this block. What matters
5407 : is the number of assignments currently made in only one of the
5408 : branches, since if we convert we are going to always execute
5409 : them. */
5410 29262 : if (c > MAX_CONDITIONAL_EXECUTE
5411 29262 : || c > limit)
5412 : return false;
5413 :
5414 : /* Try to emit the conditional moves. First do the then block,
5415 : then do anything left in the else blocks. */
5416 29248 : start_sequence ();
5417 29248 : if (!cond_move_convert_if_block (if_info, then_bb, cond,
5418 : &then_vals, &else_vals, false)
5419 29248 : || (else_bb
5420 7533 : && !cond_move_convert_if_block (if_info, else_bb, cond,
5421 : &then_vals, &else_vals, true)))
5422 : {
5423 14546 : end_sequence ();
5424 14546 : return false;
5425 : }
5426 14702 : seq = end_ifcvt_sequence (if_info);
5427 14702 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
5428 : return false;
5429 :
5430 3232 : loc_insn = first_active_insn (then_bb);
5431 3232 : if (!loc_insn)
5432 : {
5433 0 : loc_insn = first_active_insn (else_bb);
5434 0 : gcc_assert (loc_insn);
5435 : }
5436 3232 : emit_insn_before_setloc (seq, jump, loc_insn->location ());
5437 :
5438 3232 : noce_finish_if_conversion (if_info);
5439 :
5440 3232 : return true;
5441 1719948 : }
5442 :
5443 :
5444 : /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
5445 : IF-THEN-ELSE-JOIN block.
5446 :
5447 : If so, we'll try to convert the insns to not require the branch,
5448 : using only transformations that do not require conditional execution.
5449 :
5450 : Return TRUE if we were successful at converting the block. */
5451 :
5452 : static bool
5453 9964033 : noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
5454 : int pass)
5455 : {
5456 9964033 : basic_block then_bb, else_bb, join_bb;
5457 9964033 : bool then_else_reversed = false;
5458 9964033 : rtx_insn *jump;
5459 9964033 : rtx_insn *cond_earliest;
5460 9964033 : noce_if_info if_info = {};
5461 9964033 : bool speed_p = optimize_bb_for_speed_p (test_bb);
5462 :
5463 : /* We only ever should get here before reload. */
5464 9964033 : gcc_assert (!reload_completed);
5465 :
5466 : /* Recognize an IF-THEN-ELSE-JOIN block. */
5467 9964033 : if (single_pred_p (then_edge->dest)
5468 12602272 : && single_succ_p (then_edge->dest)
5469 3528135 : && single_pred_p (else_edge->dest)
5470 1843514 : && single_succ_p (else_edge->dest)
5471 11132357 : && single_succ (then_edge->dest) == single_succ (else_edge->dest))
5472 : {
5473 : then_bb = then_edge->dest;
5474 : else_bb = else_edge->dest;
5475 : join_bb = single_succ (then_bb);
5476 : }
5477 : /* Recognize an IF-THEN-JOIN block. */
5478 17210759 : else if (single_pred_p (then_edge->dest)
5479 7284940 : && single_succ_p (then_edge->dest)
5480 11712376 : && single_succ (then_edge->dest) == else_edge->dest)
5481 : {
5482 : then_bb = then_edge->dest;
5483 : else_bb = NULL_BLOCK;
5484 : join_bb = else_edge->dest;
5485 : }
5486 : /* Recognize an IF-ELSE-JOIN block. We can have those because the order
5487 : of basic blocks in cfglayout mode does not matter, so the fallthrough
5488 : edge can go to any basic block (and not just to bb->next_bb, like in
5489 : cfgrtl mode). */
5490 8136622 : else if (single_pred_p (else_edge->dest)
5491 3863943 : && single_succ_p (else_edge->dest)
5492 9562777 : && single_succ (else_edge->dest) == then_edge->dest)
5493 : {
5494 : /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
5495 : To make this work, we have to invert the THEN and ELSE blocks
5496 : and reverse the jump condition. */
5497 : then_bb = else_edge->dest;
5498 : else_bb = NULL_BLOCK;
5499 : join_bb = single_succ (then_bb);
5500 : then_else_reversed = true;
5501 : }
5502 : else
5503 : /* Not a form we can handle. */
5504 : return false;
5505 :
5506 : /* The edges of the THEN and ELSE blocks cannot have complex edges. */
5507 1964118 : if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
5508 : return false;
5509 1910199 : if (else_bb
5510 1910199 : && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
5511 : return false;
5512 :
5513 1900162 : num_possible_if_blocks++;
5514 :
5515 1900162 : if (dump_file)
5516 : {
5517 102 : fprintf (dump_file,
5518 : "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
5519 : (else_bb) ? "-ELSE" : "",
5520 : pass, test_bb->index, then_bb->index);
5521 :
5522 63 : if (else_bb)
5523 24 : fprintf (dump_file, ", else %d", else_bb->index);
5524 :
5525 63 : fprintf (dump_file, ", join %d\n", join_bb->index);
5526 : }
5527 :
5528 : /* If the conditional jump is more than just a conditional
5529 : jump, then we cannot do if-conversion on this block. */
5530 1900162 : jump = BB_END (test_bb);
5531 1900162 : if (! onlyjump_p (jump))
5532 : return false;
5533 :
5534 : /* Initialize an IF_INFO struct to pass around. */
5535 1899907 : if_info.test_bb = test_bb;
5536 1899907 : if_info.then_bb = then_bb;
5537 1899907 : if_info.else_bb = else_bb;
5538 1899907 : if_info.join_bb = join_bb;
5539 1899907 : if_info.cond = noce_get_condition (jump, &cond_earliest,
5540 : then_else_reversed);
5541 1899907 : rtx_insn *rev_cond_earliest;
5542 1899907 : if_info.rev_cond = noce_get_condition (jump, &rev_cond_earliest,
5543 : !then_else_reversed);
5544 1899907 : if (!if_info.cond && !if_info.rev_cond)
5545 : return false;
5546 1895932 : if (!if_info.cond)
5547 : {
5548 0 : std::swap (if_info.cond, if_info.rev_cond);
5549 0 : std::swap (cond_earliest, rev_cond_earliest);
5550 0 : if_info.cond_inverted = true;
5551 : }
5552 : /* We must be comparing objects whose modes imply the size. */
5553 1895932 : if (GET_MODE (XEXP (if_info.cond, 0)) == BLKmode)
5554 : return false;
5555 1895932 : gcc_assert (if_info.rev_cond == NULL_RTX
5556 : || rev_cond_earliest == cond_earliest);
5557 1895932 : if_info.cond_earliest = cond_earliest;
5558 1895932 : if_info.jump = jump;
5559 1895932 : if_info.then_else_reversed = then_else_reversed;
5560 1895932 : if_info.speed_p = speed_p;
5561 1895932 : if_info.in_loop = bitmap_bit_p (&bbs_in_loops, test_bb->index);
5562 1895932 : if_info.max_seq_cost
5563 1895932 : = targetm.max_noce_ifcvt_seq_cost (then_edge);
5564 : /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
5565 : that they are valid to transform. We can't easily get back to the insn
5566 : for COND (and it may not exist if we had to canonicalize to get COND).
5567 : It is assumed that the costs of a jump insn are dependent on the
5568 : branch costs. */
5569 1895932 : if_info.original_cost += insn_cost (if_info.jump, if_info.speed_p);
5570 :
5571 : /* Do the real work. */
5572 :
5573 : /* ??? noce_process_if_block has not yet been updated to handle
5574 : inverted conditions. */
5575 1895932 : if (!if_info.cond_inverted && noce_process_if_block (&if_info))
5576 : return true;
5577 :
5578 1719948 : if (HAVE_conditional_move
5579 1719948 : && cond_move_process_if_block (&if_info))
5580 : return true;
5581 :
5582 : return false;
5583 : }
5584 :
5585 :
5586 : /* Merge BB into COMBO_BB. BB has no successor edges left, so if COMBO_BB
5587 : still has another successor the BARRIER that follows BB is no longer needed
5588 : and it is in fact incorrect to leave it in the insn stream. */
5589 :
5590 : static void
5591 0 : merge_block_into_combo (basic_block combo_bb, basic_block bb)
5592 : {
5593 0 : if (EDGE_COUNT (bb->succs) == 0 && EDGE_COUNT (combo_bb->succs) > 1)
5594 : {
5595 0 : rtx_insn *end = NEXT_INSN (BB_END (bb));
5596 0 : while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
5597 0 : end = NEXT_INSN (end);
5598 :
5599 0 : if (end && BARRIER_P (end))
5600 0 : delete_insn (end);
5601 : }
5602 :
5603 0 : merge_blocks (combo_bb, bb);
5604 0 : num_true_changes++;
5605 0 : }
5606 :
5607 : /* Merge the blocks and mark for local life update. */
5608 :
5609 : static void
5610 0 : merge_if_block (ce_if_block *ce_info)
5611 : {
5612 0 : basic_block test_bb = ce_info->test_bb; /* last test block */
5613 0 : basic_block then_bb = ce_info->then_bb; /* THEN */
5614 0 : basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
5615 0 : basic_block join_bb = ce_info->join_bb; /* join block */
5616 0 : basic_block combo_bb;
5617 :
5618 : /* All block merging is done into the lower block numbers. */
5619 :
5620 0 : combo_bb = test_bb;
5621 0 : df_set_bb_dirty (test_bb);
5622 :
5623 : /* Merge any basic blocks to handle && and || subtests. Each of
5624 : the blocks are on the fallthru path from the predecessor block. */
5625 0 : if (ce_info->num_multiple_test_blocks > 0)
5626 : {
5627 0 : basic_block bb = test_bb;
5628 0 : basic_block last_test_bb = ce_info->last_test_bb;
5629 0 : basic_block fallthru = block_fallthru (bb);
5630 :
5631 0 : do
5632 : {
5633 0 : bb = fallthru;
5634 0 : fallthru = block_fallthru (bb);
5635 0 : merge_blocks (combo_bb, bb);
5636 0 : num_true_changes++;
5637 : }
5638 0 : while (bb != last_test_bb);
5639 : }
5640 :
5641 : /* Merge TEST block into THEN block. Normally the THEN block won't have a
5642 : label, but it might if there were || tests. That label's count should be
5643 : zero, and it normally should be removed. */
5644 :
5645 0 : if (then_bb)
5646 0 : merge_block_into_combo (combo_bb, then_bb);
5647 :
5648 : /* The ELSE block, if it existed, had a label. That label count
5649 : will almost always be zero, but odd things can happen when labels
5650 : get their addresses taken. */
5651 0 : if (else_bb)
5652 0 : merge_block_into_combo (combo_bb, else_bb);
5653 :
5654 : /* If there was no join block reported, that means it was not adjacent
5655 : to the others, and so we cannot merge them. */
5656 :
5657 0 : if (! join_bb)
5658 : {
5659 0 : rtx_insn *last = BB_END (combo_bb);
5660 :
5661 : /* The outgoing edge for the current COMBO block should already
5662 : be correct. Verify this. */
5663 0 : if (EDGE_COUNT (combo_bb->succs) == 0)
5664 0 : gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
5665 : || (NONJUMP_INSN_P (last)
5666 : && GET_CODE (PATTERN (last)) == TRAP_IF
5667 : && (TRAP_CONDITION (PATTERN (last))
5668 : == const_true_rtx)));
5669 :
5670 : else
5671 : /* There should still be something at the end of the THEN or ELSE
5672 : blocks taking us to our final destination. */
5673 0 : gcc_assert (JUMP_P (last)
5674 : || (EDGE_SUCC (combo_bb, 0)->dest
5675 : == EXIT_BLOCK_PTR_FOR_FN (cfun)
5676 : && CALL_P (last)
5677 : && SIBLING_CALL_P (last))
5678 : || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
5679 : && can_throw_internal (last)));
5680 : }
5681 :
5682 : /* The JOIN block may have had quite a number of other predecessors too.
5683 : Since we've already merged the TEST, THEN and ELSE blocks, we should
5684 : have only one remaining edge from our if-then-else diamond. If there
5685 : is more than one remaining edge, it must come from elsewhere. There
5686 : may be zero incoming edges if the THEN block didn't actually join
5687 : back up (as with a call to a non-return function). */
5688 0 : else if (EDGE_COUNT (join_bb->preds) < 2
5689 0 : && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5690 : {
5691 : /* We can merge the JOIN cleanly and update the dataflow try
5692 : again on this pass.*/
5693 0 : merge_blocks (combo_bb, join_bb);
5694 0 : num_true_changes++;
5695 : }
5696 : else
5697 : {
5698 : /* We cannot merge the JOIN. */
5699 :
5700 : /* The outgoing edge for the current COMBO block should already
5701 : be correct. Verify this. */
5702 0 : gcc_assert (single_succ_p (combo_bb)
5703 : && single_succ (combo_bb) == join_bb);
5704 :
5705 : /* Remove the jump and cruft from the end of the COMBO block. */
5706 0 : if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5707 0 : tidy_fallthru_edge (single_succ_edge (combo_bb));
5708 : }
5709 :
5710 0 : num_updated_if_blocks++;
5711 0 : }
5712 :
5713 : /* Find a block ending in a simple IF condition and try to transform it
5714 : in some way. When converting a multi-block condition, put the new code
5715 : in the first such block and delete the rest. Return a pointer to this
5716 : first block if some transformation was done. Return NULL otherwise. */
5717 :
5718 : static basic_block
5719 40237442 : find_if_header (basic_block test_bb, int pass)
5720 : {
5721 40237442 : ce_if_block ce_info;
5722 40237442 : edge then_edge;
5723 40237442 : edge else_edge;
5724 :
5725 : /* The kind of block we're looking for has exactly two successors. */
5726 40237442 : if (EDGE_COUNT (test_bb->succs) != 2)
5727 : return NULL;
5728 :
5729 20760445 : then_edge = EDGE_SUCC (test_bb, 0);
5730 20760445 : else_edge = EDGE_SUCC (test_bb, 1);
5731 :
5732 20760445 : if (df_get_bb_dirty (then_edge->dest))
5733 : return NULL;
5734 20738448 : if (df_get_bb_dirty (else_edge->dest))
5735 : return NULL;
5736 :
5737 : /* Neither edge should be abnormal. */
5738 20702051 : if ((then_edge->flags & EDGE_COMPLEX)
5739 18910991 : || (else_edge->flags & EDGE_COMPLEX))
5740 : return NULL;
5741 :
5742 : /* Nor exit the loop. */
5743 18492499 : if ((then_edge->flags & EDGE_LOOP_EXIT)
5744 16976128 : || (else_edge->flags & EDGE_LOOP_EXIT))
5745 : return NULL;
5746 :
5747 : /* The THEN edge is canonically the one that falls through. */
5748 14635602 : if (then_edge->flags & EDGE_FALLTHRU)
5749 : ;
5750 7009608 : else if (else_edge->flags & EDGE_FALLTHRU)
5751 : std::swap (then_edge, else_edge);
5752 : else
5753 : /* Otherwise this must be a multiway branch of some sort. */
5754 : return NULL;
5755 :
5756 14635255 : memset (&ce_info, 0, sizeof (ce_info));
5757 14635255 : ce_info.test_bb = test_bb;
5758 14635255 : ce_info.then_bb = then_edge->dest;
5759 14635255 : ce_info.else_bb = else_edge->dest;
5760 14635255 : ce_info.pass = pass;
5761 :
5762 : #ifdef IFCVT_MACHDEP_INIT
5763 : IFCVT_MACHDEP_INIT (&ce_info);
5764 : #endif
5765 :
5766 14635255 : if (!reload_completed
5767 14635255 : && noce_find_if_block (test_bb, then_edge, else_edge, pass))
5768 179216 : goto success;
5769 :
5770 14456039 : if (reload_completed
5771 4671222 : && targetm.have_conditional_execution ()
5772 14456039 : && cond_exec_find_if_block (&ce_info))
5773 0 : goto success;
5774 :
5775 9784817 : if (!reload_completed && targetm.have_trap ()
5776 9784817 : && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
5777 14456039 : && find_cond_trap (test_bb, then_edge, else_edge))
5778 0 : goto success;
5779 :
5780 14456039 : if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
5781 14456039 : && (reload_completed || !targetm.have_conditional_execution ()))
5782 : {
5783 14456039 : if (find_if_case_1 (test_bb, then_edge, else_edge))
5784 21258 : goto success;
5785 14434781 : if (find_if_case_2 (test_bb, then_edge, else_edge))
5786 126455 : goto success;
5787 : }
5788 :
5789 : return NULL;
5790 :
5791 326929 : success:
5792 326929 : if (dump_file)
5793 28 : fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
5794 : /* Set this so we continue looking. */
5795 326929 : ifcvt_changed_p = true;
5796 326929 : return ce_info.test_bb;
5797 : }
5798 :
5799 : /* CUR_BB has two edges, one falling through to the next block and one
5800 : jumping to TARGET_BB, so it can be part of an && test or an || test.
5801 : Return the number of non-note, non-jump, non-USE/CLOBBER insns in it, or
5802 : -1 if CUR_BB is not of that form. */
5803 :
5804 : static int
5805 0 : block_jumps_and_fallthru (basic_block cur_bb, basic_block target_bb)
5806 : {
5807 0 : edge cur_edge;
5808 0 : bool fallthru_p = false;
5809 0 : bool jump_p = false;
5810 0 : rtx_insn *insn;
5811 0 : rtx_insn *end;
5812 0 : int n_insns = 0;
5813 0 : edge_iterator ei;
5814 :
5815 0 : if (!cur_bb || !target_bb)
5816 : return -1;
5817 :
5818 : /* If no edges, obviously it doesn't jump or fallthru. */
5819 0 : if (EDGE_COUNT (cur_bb->succs) == 0)
5820 : return 0;
5821 :
5822 0 : FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
5823 : {
5824 0 : if (cur_edge->flags & EDGE_COMPLEX)
5825 : /* Anything complex isn't what we want. */
5826 : return -1;
5827 :
5828 0 : else if (cur_edge->flags & EDGE_FALLTHRU)
5829 : fallthru_p = true;
5830 :
5831 0 : else if (cur_edge->dest == target_bb)
5832 : jump_p = true;
5833 :
5834 : else
5835 : return -1;
5836 : }
5837 :
5838 0 : if (!jump_p || !fallthru_p)
5839 : return -1;
5840 :
5841 : /* Don't allow calls in the block, since this is used to group && and ||
5842 : together for conditional execution support. ??? we should support
5843 : conditional execution support across calls for IA-64 some day, but
5844 : for now it makes the code simpler. */
5845 0 : end = BB_END (cur_bb);
5846 0 : insn = BB_HEAD (cur_bb);
5847 :
5848 0 : while (insn)
5849 : {
5850 0 : if (CALL_P (insn))
5851 : return -1;
5852 :
5853 0 : if (INSN_P (insn)
5854 0 : && !JUMP_P (insn)
5855 0 : && !DEBUG_INSN_P (insn)
5856 0 : && GET_CODE (PATTERN (insn)) != USE
5857 0 : && GET_CODE (PATTERN (insn)) != CLOBBER)
5858 0 : n_insns++;
5859 :
5860 0 : if (insn == end)
5861 : break;
5862 :
5863 0 : insn = NEXT_INSN (insn);
5864 : }
5865 :
5866 : return n_insns;
5867 : }
5868 :
5869 : /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
5870 : block. If so, we'll try to convert the insns to not require the branch.
5871 : Return TRUE if we were successful at converting the block. */
5872 :
5873 : static bool
5874 0 : cond_exec_find_if_block (ce_if_block *ce_info)
5875 : {
5876 0 : basic_block test_bb = ce_info->test_bb;
5877 0 : basic_block then_bb = ce_info->then_bb;
5878 0 : basic_block else_bb = ce_info->else_bb;
5879 0 : basic_block join_bb = NULL_BLOCK;
5880 0 : edge cur_edge;
5881 0 : basic_block next;
5882 0 : edge_iterator ei;
5883 :
5884 0 : ce_info->last_test_bb = test_bb;
5885 :
5886 : /* We only ever should get here after reload,
5887 : and if we have conditional execution. */
5888 0 : gcc_assert (reload_completed && targetm.have_conditional_execution ());
5889 :
5890 : /* Discover if any fall through predecessors of the current test basic block
5891 : were && tests (which jump to the else block) or || tests (which jump to
5892 : the then block). */
5893 0 : if (single_pred_p (test_bb)
5894 0 : && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
5895 : {
5896 0 : basic_block bb = single_pred (test_bb);
5897 0 : basic_block target_bb;
5898 0 : int max_insns = MAX_CONDITIONAL_EXECUTE;
5899 0 : int n_insns;
5900 :
5901 : /* Determine if the preceding block is an && or || block. */
5902 0 : if ((n_insns = block_jumps_and_fallthru (bb, else_bb)) >= 0)
5903 : {
5904 0 : ce_info->and_and_p = true;
5905 0 : target_bb = else_bb;
5906 : }
5907 0 : else if ((n_insns = block_jumps_and_fallthru (bb, then_bb)) >= 0)
5908 : {
5909 0 : ce_info->and_and_p = false;
5910 0 : target_bb = then_bb;
5911 : }
5912 : else
5913 : target_bb = NULL_BLOCK;
5914 :
5915 0 : if (target_bb && n_insns <= max_insns)
5916 : {
5917 : int total_insns = 0;
5918 : int blocks = 0;
5919 :
5920 : /* Found at least one && or || block, look for more. */
5921 0 : do
5922 : {
5923 0 : ce_info->test_bb = test_bb = bb;
5924 0 : total_insns += n_insns;
5925 0 : blocks++;
5926 :
5927 0 : if (!single_pred_p (bb))
5928 : break;
5929 :
5930 0 : bb = single_pred (bb);
5931 0 : n_insns = block_jumps_and_fallthru (bb, target_bb);
5932 : }
5933 0 : while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
5934 :
5935 0 : ce_info->num_multiple_test_blocks = blocks;
5936 0 : ce_info->num_multiple_test_insns = total_insns;
5937 :
5938 0 : if (ce_info->and_and_p)
5939 0 : ce_info->num_and_and_blocks = blocks;
5940 : else
5941 0 : ce_info->num_or_or_blocks = blocks;
5942 : }
5943 : }
5944 :
5945 : /* The THEN block of an IF-THEN combo must have exactly one predecessor,
5946 : other than any || blocks which jump to the THEN block. */
5947 0 : if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
5948 : return false;
5949 :
5950 : /* The edges of the THEN and ELSE blocks cannot have complex edges. */
5951 0 : FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
5952 : {
5953 0 : if (cur_edge->flags & EDGE_COMPLEX)
5954 : return false;
5955 : }
5956 :
5957 0 : FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
5958 : {
5959 0 : if (cur_edge->flags & EDGE_COMPLEX)
5960 : return false;
5961 : }
5962 :
5963 : /* The THEN block of an IF-THEN combo must have zero or one successors. */
5964 0 : if (EDGE_COUNT (then_bb->succs) > 0
5965 0 : && (!single_succ_p (then_bb)
5966 0 : || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
5967 0 : || (epilogue_completed
5968 0 : && tablejump_p (BB_END (then_bb), NULL, NULL))))
5969 : return false;
5970 :
5971 : /* If the THEN block has no successors, conditional execution can still
5972 : make a conditional call. Don't do this unless the ELSE block has
5973 : only one incoming edge -- the CFG manipulation is too ugly otherwise.
5974 : Check for the last insn of the THEN block being an indirect jump, which
5975 : is listed as not having any successors, but confuses the rest of the CE
5976 : code processing. ??? we should fix this in the future. */
5977 0 : if (EDGE_COUNT (then_bb->succs) == 0)
5978 : {
5979 0 : if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5980 : {
5981 0 : rtx_insn *last_insn = BB_END (then_bb);
5982 :
5983 0 : while (last_insn
5984 0 : && NOTE_P (last_insn)
5985 0 : && last_insn != BB_HEAD (then_bb))
5986 0 : last_insn = PREV_INSN (last_insn);
5987 :
5988 0 : if (last_insn
5989 0 : && JUMP_P (last_insn)
5990 0 : && ! simplejump_p (last_insn))
5991 : return false;
5992 :
5993 : join_bb = else_bb;
5994 : else_bb = NULL_BLOCK;
5995 : }
5996 : else
5997 : return false;
5998 : }
5999 :
6000 : /* If the THEN block's successor is the other edge out of the TEST block,
6001 : then we have an IF-THEN combo without an ELSE. */
6002 0 : else if (single_succ (then_bb) == else_bb)
6003 : {
6004 : join_bb = else_bb;
6005 : else_bb = NULL_BLOCK;
6006 : }
6007 :
6008 : /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
6009 : has exactly one predecessor and one successor, and the outgoing edge
6010 : is not complex, then we have an IF-THEN-ELSE combo. */
6011 0 : else if (single_succ_p (else_bb)
6012 0 : && single_succ (then_bb) == single_succ (else_bb)
6013 0 : && single_pred_p (else_bb)
6014 0 : && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
6015 0 : && !(epilogue_completed
6016 0 : && tablejump_p (BB_END (else_bb), NULL, NULL)))
6017 0 : join_bb = single_succ (else_bb);
6018 :
6019 : /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
6020 : else
6021 : return false;
6022 :
6023 0 : num_possible_if_blocks++;
6024 :
6025 0 : if (dump_file)
6026 : {
6027 0 : fprintf (dump_file,
6028 : "\nIF-THEN%s block found, pass %d, start block %d "
6029 : "[insn %d], then %d [%d]",
6030 : (else_bb) ? "-ELSE" : "",
6031 : ce_info->pass,
6032 : test_bb->index,
6033 0 : BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
6034 : then_bb->index,
6035 0 : BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
6036 :
6037 0 : if (else_bb)
6038 0 : fprintf (dump_file, ", else %d [%d]",
6039 : else_bb->index,
6040 0 : BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
6041 :
6042 0 : fprintf (dump_file, ", join %d [%d]",
6043 : join_bb->index,
6044 0 : BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
6045 :
6046 0 : if (ce_info->num_multiple_test_blocks > 0)
6047 0 : fprintf (dump_file, ", %d %s block%s last test %d [%d]",
6048 : ce_info->num_multiple_test_blocks,
6049 0 : (ce_info->and_and_p) ? "&&" : "||",
6050 : (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
6051 : ce_info->last_test_bb->index,
6052 0 : ((BB_HEAD (ce_info->last_test_bb))
6053 0 : ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
6054 : : -1));
6055 :
6056 0 : fputc ('\n', dump_file);
6057 : }
6058 :
6059 : /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
6060 : first condition for free, since we've already asserted that there's a
6061 : fallthru edge from IF to THEN. Likewise for the && and || blocks, since
6062 : we checked the FALLTHRU flag, those are already adjacent to the last IF
6063 : block. */
6064 : /* ??? As an enhancement, move the ELSE block. Have to deal with
6065 : BLOCK notes, if by no other means than backing out the merge if they
6066 : exist. Sticky enough I don't want to think about it now. */
6067 0 : next = then_bb;
6068 0 : if (else_bb && (next = next->next_bb) != else_bb)
6069 : return false;
6070 0 : if ((next = next->next_bb) != join_bb
6071 0 : && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
6072 : {
6073 0 : if (else_bb)
6074 : join_bb = NULL;
6075 : else
6076 : return false;
6077 : }
6078 :
6079 : /* Do the real work. */
6080 :
6081 0 : ce_info->else_bb = else_bb;
6082 0 : ce_info->join_bb = join_bb;
6083 :
6084 : /* If we have && and || tests, try to first handle combining the && and ||
6085 : tests into the conditional code, and if that fails, go back and handle
6086 : it without the && and ||, which at present handles the && case if there
6087 : was no ELSE block. */
6088 0 : if (cond_exec_process_if_block (ce_info, true))
6089 : return true;
6090 :
6091 0 : if (ce_info->num_multiple_test_blocks)
6092 : {
6093 0 : cancel_changes (0);
6094 :
6095 0 : if (cond_exec_process_if_block (ce_info, false))
6096 : return true;
6097 : }
6098 :
6099 : return false;
6100 : }
6101 :
6102 : /* Convert a branch over a trap, or a branch
6103 : to a trap, into a conditional trap. */
6104 :
6105 : static bool
6106 0 : find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
6107 : {
6108 0 : basic_block then_bb = then_edge->dest;
6109 0 : basic_block else_bb = else_edge->dest;
6110 0 : basic_block other_bb, trap_bb;
6111 0 : rtx_insn *trap, *jump;
6112 0 : rtx cond;
6113 0 : rtx_insn *cond_earliest;
6114 :
6115 : /* Locate the block with the trap instruction. */
6116 : /* ??? While we look for no successors, we really ought to allow
6117 : EH successors. Need to fix merge_if_block for that to work. */
6118 0 : if ((trap = block_has_only_trap (then_bb)) != NULL)
6119 : trap_bb = then_bb, other_bb = else_bb;
6120 0 : else if ((trap = block_has_only_trap (else_bb)) != NULL)
6121 : trap_bb = else_bb, other_bb = then_bb;
6122 : else
6123 : return false;
6124 :
6125 0 : if (dump_file)
6126 : {
6127 0 : fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
6128 : test_bb->index, trap_bb->index);
6129 : }
6130 :
6131 : /* If this is not a standard conditional jump, we can't parse it. */
6132 0 : jump = BB_END (test_bb);
6133 0 : cond = noce_get_condition (jump, &cond_earliest, then_bb == trap_bb);
6134 0 : if (! cond)
6135 : return false;
6136 :
6137 : /* If the conditional jump is more than just a conditional jump, then
6138 : we cannot do if-conversion on this block. Give up for returnjump_p,
6139 : changing a conditional return followed by unconditional trap for
6140 : conditional trap followed by unconditional return is likely not
6141 : beneficial and harder to handle. */
6142 0 : if (! onlyjump_p (jump) || returnjump_p (jump))
6143 : return false;
6144 :
6145 : /* We must be comparing objects whose modes imply the size. */
6146 0 : if (GET_MODE (XEXP (cond, 0)) == BLKmode)
6147 : return false;
6148 :
6149 : /* Attempt to generate the conditional trap. */
6150 0 : rtx_insn *seq = gen_cond_trap (GET_CODE (cond), copy_rtx (XEXP (cond, 0)),
6151 : copy_rtx (XEXP (cond, 1)),
6152 0 : TRAP_CODE (PATTERN (trap)));
6153 0 : if (seq == NULL)
6154 : return false;
6155 :
6156 : /* If that results in an invalid insn, back out. */
6157 0 : for (rtx_insn *x = seq; x; x = NEXT_INSN (x))
6158 0 : if (reload_completed
6159 0 : ? !valid_insn_p (x)
6160 0 : : recog_memoized (x) < 0)
6161 : return false;
6162 :
6163 0 : if (noce_clobbers_live_cc_p (test_bb, seq))
6164 : return false;
6165 :
6166 : /* Emit the new insns before cond_earliest. */
6167 0 : emit_insn_before_setloc (seq, cond_earliest, trap->location ());
6168 :
6169 : /* Delete the trap block if possible. */
6170 0 : remove_edge (trap_bb == then_bb ? then_edge : else_edge);
6171 0 : df_set_bb_dirty (test_bb);
6172 0 : df_set_bb_dirty (then_bb);
6173 0 : df_set_bb_dirty (else_bb);
6174 :
6175 0 : if (EDGE_COUNT (trap_bb->preds) == 0)
6176 : {
6177 0 : delete_basic_block (trap_bb);
6178 0 : num_true_changes++;
6179 : }
6180 :
6181 : /* Wire together the blocks again. */
6182 0 : if (current_ir_type () == IR_RTL_CFGLAYOUT)
6183 0 : single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
6184 0 : else if (trap_bb == then_bb)
6185 : {
6186 0 : rtx lab = JUMP_LABEL (jump);
6187 0 : rtx_insn *seq = targetm.gen_jump (lab);
6188 0 : rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
6189 0 : LABEL_NUSES (lab) += 1;
6190 0 : JUMP_LABEL (newjump) = lab;
6191 0 : emit_barrier_after (newjump);
6192 : }
6193 0 : delete_insn (jump);
6194 :
6195 0 : if (can_merge_blocks_p (test_bb, other_bb))
6196 : {
6197 0 : merge_blocks (test_bb, other_bb);
6198 0 : num_true_changes++;
6199 : }
6200 :
6201 0 : num_updated_if_blocks++;
6202 0 : return true;
6203 : }
6204 :
6205 : /* Subroutine of find_cond_trap: if BB contains only a trap insn,
6206 : return it. */
6207 :
6208 : static rtx_insn *
6209 0 : block_has_only_trap (basic_block bb)
6210 : {
6211 0 : rtx_insn *trap;
6212 :
6213 : /* We're not the exit block. */
6214 0 : if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
6215 : return NULL;
6216 :
6217 : /* The block must have no successors. */
6218 0 : if (EDGE_COUNT (bb->succs) > 0)
6219 : return NULL;
6220 :
6221 : /* The only instruction in the THEN block must be the trap. */
6222 0 : trap = first_active_insn (bb);
6223 0 : if (! (trap == BB_END (bb)
6224 0 : && GET_CODE (PATTERN (trap)) == TRAP_IF
6225 0 : && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
6226 0 : return NULL;
6227 :
6228 : return trap;
6229 : }
6230 :
6231 : /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
6232 : transformable, but not necessarily the other. There need be no
6233 : JOIN block.
6234 :
6235 : Return TRUE if we were successful at converting the block.
6236 :
6237 : Cases we'd like to look at:
6238 :
6239 : (1)
6240 : if (test) goto over; // x not live
6241 : x = a;
6242 : goto label;
6243 : over:
6244 :
6245 : becomes
6246 :
6247 : x = a;
6248 : if (! test) goto label;
6249 :
6250 : (2)
6251 : if (test) goto E; // x not live
6252 : x = big();
6253 : goto L;
6254 : E:
6255 : x = b;
6256 : goto M;
6257 :
6258 : becomes
6259 :
6260 : x = b;
6261 : if (test) goto M;
6262 : x = big();
6263 : goto L;
6264 :
6265 : (3) // This one's really only interesting for targets that can do
6266 : // multiway branching, e.g. IA-64 BBB bundles. For other targets
6267 : // it results in multiple branches on a cache line, which often
6268 : // does not sit well with predictors.
6269 :
6270 : if (test1) goto E; // predicted not taken
6271 : x = a;
6272 : if (test2) goto F;
6273 : ...
6274 : E:
6275 : x = b;
6276 : J:
6277 :
6278 : becomes
6279 :
6280 : x = a;
6281 : if (test1) goto E;
6282 : if (test2) goto F;
6283 :
6284 : Notes:
6285 :
6286 : (A) Don't do (2) if the branch is predicted against the block we're
6287 : eliminating. Do it anyway if we can eliminate a branch; this requires
6288 : that the sole successor of the eliminated block postdominate the other
6289 : side of the if.
6290 :
6291 : (B) With CE, on (3) we can steal from both sides of the if, creating
6292 :
6293 : if (test1) x = a;
6294 : if (!test1) x = b;
6295 : if (test1) goto J;
6296 : if (test2) goto F;
6297 : ...
6298 : J:
6299 :
6300 : Again, this is most useful if J postdominates.
6301 :
6302 : (C) CE substitutes for helpful life information.
6303 :
6304 : (D) These heuristics need a lot of work. */
6305 :
6306 : /* Return true if the if-case formed by TEST_BB, THEN_BB and ELSE_BB is one we
6307 : may convert at all.
6308 :
6309 : If we are partitioning hot/cold basic blocks, we don't want to mess up
6310 : unconditional or indirect jumps that cross between hot and cold sections.
6311 : Basic block partitioning may result in some jumps that appear to be
6312 : optimizable (or blocks that appear to be mergeable), but which really must
6313 : be left untouched (they are required to make it safely across partition
6314 : boundaries). See the comments at the top of
6315 : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details.
6316 :
6317 : TEST_BB must also end in a conditional jump with no other side-effects. */
6318 :
6319 : static bool
6320 28737608 : if_case_blocks_ok_p (basic_block test_bb, basic_block then_bb,
6321 : basic_block else_bb)
6322 : {
6323 28737608 : if ((BB_END (then_bb)
6324 28737608 : && JUMP_P (BB_END (then_bb))
6325 15083123 : && CROSSING_JUMP_P (BB_END (then_bb)))
6326 28256490 : || (JUMP_P (BB_END (test_bb))
6327 28256490 : && CROSSING_JUMP_P (BB_END (test_bb)))
6328 56785868 : || (BB_END (else_bb)
6329 28048260 : && JUMP_P (BB_END (else_bb))
6330 14144190 : && CROSSING_JUMP_P (BB_END (else_bb))))
6331 : return false;
6332 :
6333 28002694 : return onlyjump_p (BB_END (test_bb));
6334 : }
6335 :
6336 : /* Tests for case 1 above. */
6337 :
6338 : static bool
6339 14456039 : find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
6340 : {
6341 14456039 : basic_block then_bb = then_edge->dest;
6342 14456039 : basic_block else_bb = else_edge->dest;
6343 14456039 : basic_block new_bb;
6344 14456039 : int then_bb_index;
6345 14456039 : profile_probability then_prob;
6346 14456039 : rtx else_target = NULL_RTX;
6347 :
6348 14456039 : if (!if_case_blocks_ok_p (test_bb, then_bb, else_bb))
6349 : return false;
6350 :
6351 : /* THEN has one successor. */
6352 14087901 : if (!single_succ_p (then_bb))
6353 : return false;
6354 :
6355 : /* THEN does not fall through, but is not strange either. */
6356 6312937 : if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
6357 : return false;
6358 :
6359 : /* THEN has one predecessor. */
6360 1239691 : if (!single_pred_p (then_bb))
6361 : return false;
6362 :
6363 : /* THEN must do something. */
6364 1116284 : if (forwarder_block_p (then_bb))
6365 : return false;
6366 :
6367 710178 : num_possible_if_blocks++;
6368 710178 : if (dump_file)
6369 8 : fprintf (dump_file,
6370 : "\nIF-CASE-1 found, start %d, then %d\n",
6371 : test_bb->index, then_bb->index);
6372 :
6373 710178 : then_prob = then_edge->probability.invert ();
6374 :
6375 : /* We're speculating from the THEN path, we want to make sure the cost
6376 : of speculation is within reason. */
6377 1343393 : if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
6378 1343393 : COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
6379 : predictable_edge_p (then_edge)))))
6380 : return false;
6381 :
6382 144177 : if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
6383 : {
6384 0 : rtx_insn *jump = BB_END (else_edge->src);
6385 0 : gcc_assert (JUMP_P (jump));
6386 0 : else_target = JUMP_LABEL (jump);
6387 : }
6388 :
6389 : /* Registers set are dead, or are predicable. */
6390 144177 : if (! dead_or_predicable (test_bb, then_bb, else_bb,
6391 : single_succ_edge (then_bb), true))
6392 : return false;
6393 :
6394 : /* Conversion went ok, including moving the insns and fixing up the
6395 : jump. Adjust the CFG to match. */
6396 :
6397 : /* We can avoid creating a new basic block if then_bb is immediately
6398 : followed by else_bb, i.e. deleting then_bb allows test_bb to fall
6399 : through to else_bb. */
6400 :
6401 21258 : if (then_bb->next_bb == else_bb
6402 9914 : && then_bb->prev_bb == test_bb
6403 9914 : && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
6404 : {
6405 9914 : redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
6406 9914 : new_bb = 0;
6407 : }
6408 11344 : else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
6409 0 : new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
6410 : else_bb, else_target);
6411 : else
6412 11344 : new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
6413 : else_bb);
6414 :
6415 21258 : df_set_bb_dirty (test_bb);
6416 21258 : df_set_bb_dirty (else_bb);
6417 :
6418 21258 : then_bb_index = then_bb->index;
6419 21258 : delete_basic_block (then_bb);
6420 :
6421 : /* Make rest of code believe that the newly created block is the THEN_BB
6422 : block we removed. */
6423 21258 : if (new_bb)
6424 : {
6425 : /* NEW_BB takes the index, and so the bbs_in_loops bit, of THEN_BB.
6426 : That bit is only read for a block that ends in a conditional jump. */
6427 11344 : gcc_checking_assert (single_succ_p (new_bb));
6428 11344 : df_bb_replace (then_bb_index, new_bb);
6429 : /* This should have been done above via force_nonfallthru_and_redirect
6430 : (possibly called from redirect_edge_and_branch_force). */
6431 11344 : gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
6432 : }
6433 :
6434 21258 : num_true_changes++;
6435 21258 : num_updated_if_blocks++;
6436 21258 : return true;
6437 : }
6438 :
6439 : /* Test for case 2 above. */
6440 :
6441 : static bool
6442 14434781 : find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
6443 : {
6444 14434781 : basic_block then_bb = then_edge->dest;
6445 14434781 : basic_block else_bb = else_edge->dest;
6446 14434781 : edge else_succ;
6447 14434781 : profile_probability then_prob, else_prob;
6448 :
6449 : /* We do not want to speculate (empty) loop latches. */
6450 14434781 : if (current_loops
6451 5646807 : && else_bb->loop_father->latch == else_bb)
6452 : return false;
6453 :
6454 14281569 : if (!if_case_blocks_ok_p (test_bb, then_bb, else_bb))
6455 : return false;
6456 :
6457 : /* ELSE has one successor. */
6458 13913432 : if (!single_succ_p (else_bb))
6459 : return false;
6460 : else
6461 5879239 : else_succ = single_succ_edge (else_bb);
6462 :
6463 : /* ELSE outgoing edge is not complex. */
6464 5879239 : if (else_succ->flags & EDGE_COMPLEX)
6465 : return false;
6466 :
6467 : /* ELSE has one predecessor. */
6468 5720649 : if (!single_pred_p (else_bb))
6469 : return false;
6470 :
6471 : /* THEN is not EXIT. */
6472 2989749 : if (then_bb->index < NUM_FIXED_BLOCKS)
6473 : return false;
6474 :
6475 2989749 : else_prob = else_edge->probability;
6476 2989749 : then_prob = else_prob.invert ();
6477 :
6478 : /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
6479 2989749 : if (else_prob > then_prob)
6480 : ;
6481 2010916 : else if (else_succ->dest->index < NUM_FIXED_BLOCKS
6482 2010916 : || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
6483 : else_succ->dest))
6484 : ;
6485 : else
6486 : return false;
6487 :
6488 2371649 : num_possible_if_blocks++;
6489 2371649 : if (dump_file)
6490 47 : fprintf (dump_file,
6491 : "\nIF-CASE-2 found, start %d, else %d\n",
6492 : test_bb->index, else_bb->index);
6493 :
6494 : /* We're speculating from the ELSE path, we want to make sure the cost
6495 : of speculation is within reason. */
6496 4573413 : if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
6497 4573413 : COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
6498 : predictable_edge_p (else_edge)))))
6499 : return false;
6500 :
6501 : /* Registers set are dead, or are predicable. */
6502 456537 : if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, false))
6503 : return false;
6504 :
6505 : /* Conversion went ok, including moving the insns and fixing up the
6506 : jump. Adjust the CFG to match. */
6507 :
6508 126455 : df_set_bb_dirty (test_bb);
6509 126455 : df_set_bb_dirty (then_bb);
6510 126455 : delete_basic_block (else_bb);
6511 :
6512 126455 : num_true_changes++;
6513 126455 : num_updated_if_blocks++;
6514 :
6515 : /* ??? We may now fallthru from one of THEN's successors into a join
6516 : block. Rerun cleanup_cfg? Examine things manually? Wait? */
6517 :
6518 126455 : return true;
6519 : }
6520 :
6521 : /* Used by the code above to perform the actual rtl transformations.
6522 : Return TRUE if successful.
6523 :
6524 : TEST_BB is the block containing the conditional branch. MERGE_BB is the
6525 : block containing the code to manipulate. OTHER_BB is the other successor
6526 : of TEST_BB, the one the code is being moved past. DEST_EDGE is an edge
6527 : representing a jump to the join block; after the conversion, TEST_BB should
6528 : be branching to its destination. REVERSEP is true if the sense of the
6529 : branch should be reversed. */
6530 :
6531 : static bool
6532 600714 : dead_or_predicable (basic_block test_bb, basic_block merge_bb,
6533 : basic_block other_bb, edge dest_edge, bool reversep)
6534 : {
6535 600714 : basic_block new_dest = dest_edge->dest;
6536 600714 : rtx_insn *head, *end, *jump;
6537 600714 : rtx_insn *earliest = NULL;
6538 600714 : rtx old_dest;
6539 600714 : auto_bitmap merge_set (®_obstack);
6540 : /* Number of pending changes. */
6541 600714 : int n_validated_changes = 0;
6542 600714 : rtx new_dest_label = NULL_RTX;
6543 :
6544 600714 : jump = BB_END (test_bb);
6545 :
6546 : /* Find the extent of the real code in the merge block. */
6547 600714 : head = BB_HEAD (merge_bb);
6548 600714 : end = BB_END (merge_bb);
6549 :
6550 759067 : while (DEBUG_INSN_P (end) && end != head)
6551 158353 : end = PREV_INSN (end);
6552 :
6553 : /* If merge_bb ends with a tablejump, predicating/moving insn's
6554 : into test_bb and then deleting merge_bb will result in the jumptable
6555 : that follows merge_bb being removed along with merge_bb and then we
6556 : get an unresolved reference to the jumptable. */
6557 600714 : if (tablejump_p (end, NULL, NULL))
6558 : return false;
6559 :
6560 600714 : if (LABEL_P (head))
6561 456602 : head = NEXT_INSN (head);
6562 600714 : while (DEBUG_INSN_P (head) && head != end)
6563 0 : head = NEXT_INSN (head);
6564 600714 : if (NOTE_P (head))
6565 : {
6566 600714 : if (head == end)
6567 : {
6568 103172 : head = end = NULL;
6569 103172 : goto no_body;
6570 : }
6571 497542 : head = NEXT_INSN (head);
6572 1418128 : while (DEBUG_INSN_P (head) && head != end)
6573 423044 : head = NEXT_INSN (head);
6574 : }
6575 :
6576 497542 : if (JUMP_P (end))
6577 : {
6578 208267 : if (!onlyjump_p (end))
6579 : return false;
6580 167608 : if (head == end)
6581 : {
6582 6 : head = end = NULL;
6583 6 : goto no_body;
6584 : }
6585 167602 : end = PREV_INSN (end);
6586 453909 : while (DEBUG_INSN_P (end) && end != head)
6587 118705 : end = PREV_INSN (end);
6588 : }
6589 :
6590 : /* Don't move frame-related insn across the conditional branch. This
6591 : can lead to one of the paths of the branch having wrong unwind info. */
6592 456877 : if (epilogue_completed)
6593 : {
6594 : rtx_insn *insn = head;
6595 87813 : while (1)
6596 : {
6597 295165 : if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
6598 : return false;
6599 288730 : if (insn == end)
6600 : break;
6601 87813 : insn = NEXT_INSN (insn);
6602 87813 : }
6603 : }
6604 :
6605 : /* Disable handling dead code by conditional execution if the machine needs
6606 : to do anything funny with the tests, etc. */
6607 : #ifndef IFCVT_MODIFY_TESTS
6608 450442 : if (targetm.have_conditional_execution ())
6609 : {
6610 : /* In the conditional execution case, we have things easy. We know
6611 : the condition is reversible. We don't have to check life info
6612 : because we're going to conditionally execute the code anyway.
6613 : All that's left is making sure the insns involved can actually
6614 : be predicated. */
6615 :
6616 0 : rtx cond;
6617 :
6618 : /* If the conditional jump is more than just a conditional jump,
6619 : then we cannot do conditional execution conversion on this block. */
6620 0 : if (!onlyjump_p (jump))
6621 0 : goto nce;
6622 :
6623 0 : cond = cond_exec_get_condition (jump);
6624 0 : if (! cond)
6625 0 : goto nce;
6626 :
6627 0 : rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
6628 0 : profile_probability prob_val
6629 0 : = (note ? profile_probability::from_reg_br_prob_note (XINT (note, 0))
6630 : : profile_probability::uninitialized ());
6631 :
6632 0 : if (reversep)
6633 : {
6634 0 : enum rtx_code rev = reversed_comparison_code (cond, jump);
6635 0 : if (rev == UNKNOWN)
6636 0 : return false;
6637 0 : cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
6638 : XEXP (cond, 1));
6639 0 : prob_val = prob_val.invert ();
6640 : }
6641 :
6642 0 : if (cond_exec_process_insns (NULL, head, end, cond, prob_val, false)
6643 0 : && verify_changes (0))
6644 0 : n_validated_changes = num_validated_changes ();
6645 : else
6646 0 : cancel_changes (0);
6647 :
6648 0 : earliest = jump;
6649 : }
6650 450442 : nce:
6651 : #endif
6652 :
6653 : /* If we allocated new pseudos (e.g. in the conditional move
6654 : expander called from noce_emit_cmove), we must resize the
6655 : array first. */
6656 450442 : if (max_regno < max_reg_num ())
6657 86127 : max_regno = max_reg_num ();
6658 :
6659 : /* Try the NCE path if the CE path did not result in any changes. */
6660 450442 : if (n_validated_changes == 0)
6661 : {
6662 450442 : rtx cond;
6663 450442 : rtx_insn *insn;
6664 450442 : bool success;
6665 :
6666 : /* In the non-conditional execution case, we have to verify that there
6667 : are no trapping operations, no calls, no references to memory, and
6668 : that any registers modified are dead at the branch site. */
6669 :
6670 450442 : if (!any_condjump_p (jump))
6671 305875 : return false;
6672 :
6673 : /* Find the extent of the conditional. */
6674 450442 : cond = noce_get_condition (jump, &earliest, false);
6675 450442 : if (!cond)
6676 : return false;
6677 :
6678 449634 : auto_bitmap live (®_obstack);
6679 449634 : simulate_backwards_to_point (merge_bb, live, end);
6680 449634 : success = can_move_insns_across (head, end, earliest, jump,
6681 : merge_bb, live,
6682 : df_get_live_in (other_bb), NULL);
6683 449634 : if (!success)
6684 : return false;
6685 :
6686 : /* Collect the set of registers set in MERGE_BB. */
6687 744874 : FOR_BB_INSNS (merge_bb, insn)
6688 599746 : if (NONDEBUG_INSN_P (insn))
6689 188793 : df_simulate_find_defs (insn, merge_set);
6690 :
6691 : /* If shrink-wrapping, disable this optimization when test_bb is
6692 : the first basic block and merge_bb exits. The idea is to not
6693 : move code setting up a return register as that may clobber a
6694 : register used to pass function parameters, which then must be
6695 : saved in caller-saved regs. A caller-saved reg requires the
6696 : prologue, killing a shrink-wrap opportunity. */
6697 145128 : if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
6698 114340 : && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
6699 158865 : && single_succ_p (new_dest)
6700 14298 : && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
6701 158150 : && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
6702 : {
6703 13022 : unsigned int i;
6704 :
6705 13022 : auto_bitmap return_regs (®_obstack);
6706 :
6707 : /* Start off with the intersection of regs used to pass
6708 : params and regs used to return values. */
6709 1237090 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6710 1224068 : if (FUNCTION_ARG_REGNO_P (i)
6711 1224068 : && targetm.calls.function_value_regno_p (i))
6712 50932 : bitmap_set_bit (return_regs, INCOMING_REGNO (i));
6713 :
6714 13022 : bitmap_and_into (return_regs,
6715 13022 : df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
6716 13022 : bitmap_and_into (return_regs,
6717 13022 : df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
6718 13022 : if (!bitmap_empty_p (return_regs))
6719 : {
6720 5121 : FOR_BB_INSNS_REVERSE (new_dest, insn)
6721 4540 : if (NONDEBUG_INSN_P (insn))
6722 : {
6723 2309 : df_ref def;
6724 :
6725 : /* If this insn sets any reg in return_regs, add all
6726 : reg uses to the set of regs we're interested in. */
6727 2914 : FOR_EACH_INSN_DEF (def, insn)
6728 1770 : if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
6729 : {
6730 1165 : df_simulate_uses (insn, return_regs);
6731 1165 : break;
6732 : }
6733 : }
6734 581 : if (bitmap_intersect_p (merge_set, return_regs))
6735 561 : return false;
6736 : }
6737 13022 : }
6738 449634 : }
6739 :
6740 0 : no_body:
6741 : /* We don't want to use normal invert_jump or redirect_jump because
6742 : we don't want to delete_insn called. Also, we want to do our own
6743 : change group management. */
6744 :
6745 247745 : old_dest = JUMP_LABEL (jump);
6746 247745 : if (other_bb != new_dest)
6747 : {
6748 247729 : if (!any_condjump_p (jump))
6749 0 : goto cancel;
6750 :
6751 247729 : if (JUMP_P (BB_END (dest_edge->src)))
6752 22394 : new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
6753 225335 : else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
6754 100032 : new_dest_label = ret_rtx;
6755 : else
6756 125303 : new_dest_label = block_label (new_dest);
6757 :
6758 247729 : rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
6759 247729 : if (reversep
6760 247729 : ? ! invert_jump_1 (jump_insn, new_dest_label)
6761 226471 : : ! redirect_jump_1 (jump_insn, new_dest_label))
6762 0 : goto cancel;
6763 : }
6764 :
6765 247745 : if (verify_changes (n_validated_changes))
6766 147713 : confirm_change_group ();
6767 : else
6768 100032 : goto cancel;
6769 :
6770 147713 : if (other_bb != new_dest)
6771 : {
6772 147697 : redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
6773 : 0, reversep);
6774 :
6775 147697 : redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
6776 147697 : if (reversep)
6777 : {
6778 21258 : std::swap (BRANCH_EDGE (test_bb)->probability,
6779 21258 : FALLTHRU_EDGE (test_bb)->probability);
6780 21258 : update_br_prob_note (test_bb);
6781 : }
6782 : }
6783 :
6784 : /* Move the insns out of MERGE_BB to before the branch. */
6785 147713 : if (head != NULL)
6786 : {
6787 144567 : rtx_insn *insn;
6788 :
6789 144567 : if (end == BB_END (merge_bb))
6790 111144 : BB_END (merge_bb) = PREV_INSN (head);
6791 :
6792 : /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
6793 : notes being moved might become invalid. */
6794 144567 : insn = head;
6795 173443 : do
6796 : {
6797 173443 : rtx note;
6798 :
6799 173443 : if (! INSN_P (insn))
6800 3770 : continue;
6801 169673 : note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
6802 169673 : if (! note)
6803 157775 : continue;
6804 11898 : remove_note (insn, note);
6805 202319 : } while (insn != end && (insn = NEXT_INSN (insn)));
6806 :
6807 : /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
6808 : notes referring to the registers being set might become invalid.
6809 : MERGE_SET is empty on the conditional-execution path, which does not
6810 : move anything. */
6811 144567 : unsigned i;
6812 144567 : bitmap_iterator bi;
6813 325609 : EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
6814 181042 : remove_reg_equal_equiv_notes_for_regno (i);
6815 :
6816 144567 : reorder_insns (head, end, PREV_INSN (earliest));
6817 : }
6818 :
6819 : /* Remove the jump and edge if we can. */
6820 147713 : if (other_bb == new_dest)
6821 : {
6822 16 : delete_insn (jump);
6823 16 : remove_edge (BRANCH_EDGE (test_bb));
6824 : /* ??? Can't merge blocks here, as then_bb is still in use.
6825 : At minimum, the merge will get done just before bb-reorder. */
6826 : }
6827 :
6828 : return true;
6829 :
6830 100032 : cancel:
6831 100032 : cancel_changes (0);
6832 :
6833 100032 : return false;
6834 600714 : }
6835 :
6836 : /* Main entry point for all if-conversion. PHASE selects the ce1, ce2 or ce3
6837 : run: before combine, after combine, or after reload. */
6838 :
6839 : static void
6840 3203321 : if_convert (ifcvt_phase phase)
6841 : {
6842 3203321 : basic_block bb;
6843 3203321 : int pass;
6844 :
6845 3203321 : if (optimize == 1)
6846 : {
6847 240937 : df_live_add_problem ();
6848 240937 : df_live_set_all_dirty ();
6849 : }
6850 :
6851 : /* Record which pass is running for the cost model. */
6852 3203321 : ifcvt_pass_phase = phase;
6853 3203321 : have_cbranchcc4 = (direct_optab_handler (cbranch_optab, CCmode)
6854 3203321 : != CODE_FOR_nothing);
6855 3203321 : num_possible_if_blocks = 0;
6856 3203321 : num_updated_if_blocks = 0;
6857 3203321 : num_true_changes = 0;
6858 :
6859 3203321 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
6860 3203321 : mark_loop_exit_edges ();
6861 3203321 : bitmap_initialize (&bbs_in_loops, &bitmap_default_obstack);
6862 36587543 : FOR_EACH_BB_FN (bb, cfun)
6863 33384222 : if (bb_loop_depth (bb) != 0)
6864 8096427 : bitmap_set_bit (&bbs_in_loops, bb->index);
6865 3203321 : loop_optimizer_finalize ();
6866 3203321 : free_dominance_info (CDI_DOMINATORS);
6867 :
6868 : /* Compute postdominators. */
6869 3203321 : calculate_dominance_info (CDI_POST_DOMINATORS);
6870 :
6871 3203321 : df_set_flags (DF_LR_RUN_DCE);
6872 :
6873 : /* Go through each of the basic blocks looking for things to convert. If we
6874 : have conditional execution, we make multiple passes to allow us to handle
6875 : IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
6876 3203321 : pass = 0;
6877 3374580 : do
6878 : {
6879 3374580 : df_analyze ();
6880 : /* Only need to do dce on the first pass. */
6881 3374580 : df_clear_flags (DF_LR_RUN_DCE);
6882 3374580 : ifcvt_changed_p = false;
6883 3374580 : pass++;
6884 :
6885 3374580 : if (dump_file && pass > 1)
6886 24 : fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
6887 :
6888 43777550 : FOR_EACH_BB_FN (bb, cfun)
6889 : {
6890 : basic_block new_bb;
6891 40729899 : while (!df_get_bb_dirty (bb)
6892 80640412 : && (new_bb = find_if_header (bb, pass)) != NULL)
6893 : bb = new_bb;
6894 : }
6895 :
6896 3374580 : if (dump_file && ifcvt_changed_p)
6897 24 : print_rtl_with_bb (dump_file, get_insns (), dump_flags);
6898 : }
6899 : while (ifcvt_changed_p);
6900 :
6901 3203321 : if (dump_file)
6902 90 : fprintf (dump_file, "\n\n========== no more changes\n");
6903 :
6904 3203321 : free_dominance_info (CDI_POST_DOMINATORS);
6905 :
6906 3203321 : bitmap_release (&bbs_in_loops);
6907 :
6908 3203321 : if (dump_file)
6909 90 : fflush (dump_file);
6910 :
6911 3203321 : clear_aux_for_blocks ();
6912 :
6913 : /* If we allocated new pseudos, we must resize the array for sched1. */
6914 3203321 : if (max_regno < max_reg_num ())
6915 657351 : max_regno = max_reg_num ();
6916 :
6917 : /* Write the final stats. */
6918 3203321 : if (dump_file && num_possible_if_blocks > 0)
6919 : {
6920 64 : fprintf (dump_file,
6921 : "\n%d possible IF blocks searched.\n",
6922 : num_possible_if_blocks);
6923 64 : fprintf (dump_file,
6924 : "%d IF blocks converted.\n",
6925 : num_updated_if_blocks);
6926 64 : fprintf (dump_file,
6927 : "%d true changes made.\n\n\n",
6928 : num_true_changes);
6929 : }
6930 :
6931 3203321 : if (optimize == 1)
6932 240937 : df_remove_problem (df_live);
6933 :
6934 : /* Some non-cold blocks may now be only reachable from cold blocks.
6935 : Fix that up. */
6936 3203321 : fixup_partitions ();
6937 :
6938 3203321 : checking_verify_flow_info ();
6939 3203321 : }
6940 :
6941 : /* If-conversion and CFG cleanup. */
6942 : static void
6943 1070214 : rest_of_handle_if_conversion (void)
6944 : {
6945 1070214 : int flags = 0;
6946 :
6947 1070214 : if (flag_if_conversion)
6948 : {
6949 1067760 : if (dump_file)
6950 : {
6951 37 : dump_reg_info (dump_file);
6952 37 : dump_flow_info (dump_file, dump_flags);
6953 : }
6954 1067760 : cleanup_cfg (CLEANUP_EXPENSIVE);
6955 1067760 : if_convert (IFCVT_BEFORE_COMBINE);
6956 1067760 : if (num_updated_if_blocks)
6957 : /* Get rid of any dead CC-related instructions. */
6958 1070214 : flags |= CLEANUP_FORCE_FAST_DCE;
6959 : }
6960 :
6961 1070214 : cleanup_cfg (flags);
6962 1070214 : }
6963 :
6964 : namespace {
6965 :
6966 : const pass_data pass_data_rtl_ifcvt =
6967 : {
6968 : RTL_PASS, /* type */
6969 : "ce1", /* name */
6970 : OPTGROUP_NONE, /* optinfo_flags */
6971 : TV_IFCVT, /* tv_id */
6972 : 0, /* properties_required */
6973 : 0, /* properties_provided */
6974 : 0, /* properties_destroyed */
6975 : 0, /* todo_flags_start */
6976 : TODO_df_finish, /* todo_flags_finish */
6977 : };
6978 :
6979 : /* ce2 reruns if-conversion after combine has simplified things enough to
6980 : meet the sequence-length restrictions. */
6981 : const pass_data pass_data_if_after_combine =
6982 : {
6983 : RTL_PASS, /* type */
6984 : "ce2", /* name */
6985 : OPTGROUP_NONE, /* optinfo_flags */
6986 : TV_IFCVT, /* tv_id */
6987 : 0, /* properties_required */
6988 : 0, /* properties_provided */
6989 : 0, /* properties_destroyed */
6990 : 0, /* todo_flags_start */
6991 : TODO_df_finish, /* todo_flags_finish */
6992 : };
6993 :
6994 : /* ce3 reruns if-conversion after reload. */
6995 : const pass_data pass_data_if_after_reload =
6996 : {
6997 : RTL_PASS, /* type */
6998 : "ce3", /* name */
6999 : OPTGROUP_NONE, /* optinfo_flags */
7000 : TV_IFCVT2, /* tv_id */
7001 : 0, /* properties_required */
7002 : 0, /* properties_provided */
7003 : 0, /* properties_destroyed */
7004 : 0, /* todo_flags_start */
7005 : TODO_df_finish, /* todo_flags_finish */
7006 : };
7007 :
7008 : /* The three if-conversion passes (ce1/ce2/ce3) share this class. M_PHASE
7009 : selects the gate condition and the work: ce1 (before combine) also cleans
7010 : up the CFG and is gated only on optimize, while ce2 and ce3 rerun once
7011 : combine and reload have simplified the RTL. */
7012 :
7013 : class pass_rtl_ifcvt : public rtl_opt_pass
7014 : {
7015 : public:
7016 908955 : pass_rtl_ifcvt (gcc::context *ctxt, const pass_data &data, ifcvt_phase phase)
7017 1817910 : : rtl_opt_pass (data, ctxt), m_phase (phase)
7018 : {}
7019 :
7020 : /* opt_pass methods: */
7021 4593393 : bool gate (function *) final override
7022 : {
7023 4593393 : if (optimize == 0)
7024 : return false;
7025 3210687 : switch (m_phase)
7026 : {
7027 1070229 : case IFCVT_BEFORE_COMBINE:
7028 1070229 : return dbg_cnt (if_conversion);
7029 1070229 : case IFCVT_AFTER_COMBINE:
7030 1070229 : return flag_if_conversion && dbg_cnt (if_after_combine);
7031 1070229 : case IFCVT_AFTER_RELOAD:
7032 1070229 : return flag_if_conversion2 && dbg_cnt (if_after_reload);
7033 : }
7034 0 : gcc_unreachable ();
7035 : }
7036 :
7037 3205775 : unsigned int execute (function *) final override
7038 : {
7039 3205775 : if (m_phase == IFCVT_BEFORE_COMBINE)
7040 1070214 : rest_of_handle_if_conversion ();
7041 : else
7042 2135561 : if_convert (m_phase);
7043 3205775 : return 0;
7044 : }
7045 :
7046 : private:
7047 : ifcvt_phase m_phase;
7048 :
7049 : }; // class pass_rtl_ifcvt
7050 :
7051 : } // anon namespace
7052 :
7053 : rtl_opt_pass *
7054 302985 : make_pass_rtl_ifcvt (gcc::context *ctxt)
7055 : {
7056 302985 : return new pass_rtl_ifcvt (ctxt, pass_data_rtl_ifcvt, IFCVT_BEFORE_COMBINE);
7057 : }
7058 :
7059 : rtl_opt_pass *
7060 302985 : make_pass_if_after_combine (gcc::context *ctxt)
7061 : {
7062 302985 : return new pass_rtl_ifcvt (ctxt, pass_data_if_after_combine,
7063 302985 : IFCVT_AFTER_COMBINE);
7064 : }
7065 :
7066 : rtl_opt_pass *
7067 302985 : make_pass_if_after_reload (gcc::context *ctxt)
7068 : {
7069 302985 : return new pass_rtl_ifcvt (ctxt, pass_data_if_after_reload,
7070 302985 : IFCVT_AFTER_RELOAD);
7071 : }
7072 :
7073 : #if CHECKING_P
7074 :
7075 : namespace selftest {
7076 :
7077 : /* Return the dependency level of PAT and update READY. */
7078 :
7079 : static unsigned int
7080 40 : test_noce_level (noce_ready_map &ready, rtx pat)
7081 : {
7082 40 : vec_rtx_properties properties;
7083 40 : properties.add_pattern (pat);
7084 40 : unsigned int level;
7085 40 : ASSERT_TRUE (noce_dependency_level (ready, properties, &level));
7086 40 : return level;
7087 40 : }
7088 :
7089 : /* Check read-before-write handling and replacement of complete definitions. */
7090 :
7091 : static void
7092 4 : test_noce_pseudo_dependencies ()
7093 : {
7094 4 : noce_ready_map ready;
7095 4 : rtx a = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 1);
7096 4 : rtx b = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 2);
7097 4 : rtx increment = gen_rtx_SET (a, gen_rtx_PLUS (SImode, a, const1_rtx));
7098 :
7099 4 : ASSERT_EQ (0U, test_noce_level (ready, gen_rtx_SET (a, const0_rtx)));
7100 4 : ASSERT_EQ (1U, test_noce_level (ready, increment));
7101 4 : ASSERT_EQ (2U, test_noce_level (ready, increment));
7102 4 : ASSERT_EQ (0U, test_noce_level (ready, gen_rtx_SET (a, const0_rtx)));
7103 4 : ASSERT_EQ (1U, test_noce_level (ready, gen_rtx_SET (b, a)));
7104 4 : }
7105 :
7106 : /* Check references outside the SET and rejection before updating READY. */
7107 :
7108 : static void
7109 4 : test_noce_parallel_references ()
7110 : {
7111 4 : noce_ready_map ready;
7112 4 : rtx a = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 1);
7113 4 : rtx b = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 2);
7114 4 : rtx c = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 3);
7115 4 : ready.put (REGNO (a), 4);
7116 4 : ready.put (REGNO (c), 7);
7117 4 : rtx pat = gen_rtx_PARALLEL (VOIDmode,
7118 : gen_rtvec (3, gen_rtx_SET (b, const0_rtx),
7119 : gen_rtx_USE (VOIDmode, a),
7120 : gen_rtx_CLOBBER (VOIDmode, c)));
7121 4 : ASSERT_EQ (4U, test_noce_level (ready, pat));
7122 4 : ASSERT_EQ (5U, *ready.get (REGNO (b)));
7123 4 : ASSERT_EQ (7U, *ready.get (REGNO (c)));
7124 :
7125 4 : rtx addr = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 4);
7126 4 : vec_rtx_properties properties;
7127 4 : properties.add_pattern (gen_rtx_SET (b, gen_rtx_MEM (SImode, addr)));
7128 4 : unsigned int level;
7129 4 : ASSERT_FALSE (noce_dependency_level (ready, properties, &level));
7130 4 : ASSERT_EQ (5U, *ready.get (REGNO (b)));
7131 4 : }
7132 :
7133 : /* Check that each register number of a hard register that spans several is
7134 : tracked on its own. */
7135 :
7136 : static void
7137 4 : test_noce_hard_register_dependencies ()
7138 : {
7139 4 : for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno)
7140 : {
7141 4 : if (!targetm.hard_regno_mode_ok (regno, TImode)
7142 4 : || !targetm.hard_regno_mode_ok (regno, QImode))
7143 0 : continue;
7144 4 : rtx wide = gen_raw_REG (TImode, regno);
7145 4 : unsigned int end = END_REGNO (wide);
7146 4 : if (end > FIRST_PSEUDO_REGISTER || end <= regno + 1
7147 4 : || (regno <= STACK_POINTER_REGNUM && STACK_POINTER_REGNUM < end))
7148 0 : continue;
7149 :
7150 4 : noce_ready_map ready;
7151 4 : ready.put (end - 1, 4);
7152 4 : ASSERT_EQ (0U, test_noce_level (ready, gen_rtx_SET (wide, const0_rtx)));
7153 12 : for (unsigned int i = regno; i < end; ++i)
7154 8 : ASSERT_EQ (1U, *ready.get (i));
7155 4 : rtx dest = gen_raw_REG (TImode, LAST_VIRTUAL_REGISTER + 1);
7156 4 : ASSERT_EQ (1U, test_noce_level (ready, gen_rtx_SET (dest, wide)));
7157 :
7158 4 : ready.put (end - 1, 6);
7159 4 : rtx narrow = gen_raw_REG (QImode, regno);
7160 4 : ASSERT_EQ (0U, test_noce_level (ready, gen_rtx_SET (narrow, const0_rtx)));
7161 4 : ASSERT_EQ (6U, test_noce_level (ready, gen_rtx_SET (dest, wide)));
7162 4 : return;
7163 4 : }
7164 : }
7165 :
7166 : /* Check full-pattern rejection before target costing. */
7167 :
7168 : static void
7169 4 : test_noce_parallel_cost_gates ()
7170 : {
7171 4 : rtx a = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 1);
7172 4 : rtx b = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 2);
7173 4 : rtx set = gen_rtx_SET (a, const0_rtx);
7174 4 : ASSERT_FALSE (side_effects_p (set));
7175 4 : rtx pat = gen_rtx_PARALLEL (VOIDmode,
7176 : gen_rtvec (2, set, gen_rtx_USE (VOIDmode, b)));
7177 4 : if (set_rtx_cost (set, true) == COSTS_N_INSNS (1))
7178 : {
7179 4 : ASSERT_TRUE (noce_parallel_costed_insn_p (make_insn_raw (set), true));
7180 4 : ASSERT_TRUE (noce_parallel_costed_insn_p (make_insn_raw (pat), true));
7181 : }
7182 :
7183 : /* A partial write is not a unit-cost set. The lowpart of a narrower mode
7184 : does not sit at offset zero on a big-endian target, and a target need not
7185 : accept the subreg at all, so ask before building it. */
7186 4 : poly_uint64 byte = subreg_lowpart_offset (QImode, SImode);
7187 4 : if (validate_subreg (QImode, SImode, a, byte))
7188 : {
7189 4 : rtx partial = gen_rtx_SUBREG (QImode, a, byte);
7190 4 : rtx_insn *part_insn = make_insn_raw (gen_rtx_SET (partial, const0_rtx));
7191 4 : ASSERT_FALSE (noce_parallel_costed_insn_p (part_insn, true));
7192 : }
7193 :
7194 4 : pat = gen_rtx_PARALLEL (VOIDmode,
7195 : gen_rtvec (2, set, gen_rtx_SET (b, const0_rtx)));
7196 4 : rtx_insn *insn = make_insn_raw (pat);
7197 4 : add_reg_note (insn, REG_UNUSED, b);
7198 4 : ASSERT_TRUE (multiple_sets (insn));
7199 4 : ASSERT_FALSE (noce_parallel_costed_insn_p (insn, true));
7200 :
7201 4 : rtx addr = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 3);
7202 4 : rtx mem_use = gen_rtx_USE (VOIDmode, gen_rtx_MEM (SImode, addr));
7203 4 : pat = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, mem_use));
7204 4 : ASSERT_FALSE (noce_parallel_costed_insn_p (make_insn_raw (pat), true));
7205 :
7206 4 : rtx vol = gen_rtx_UNSPEC_VOLATILE (SImode, gen_rtvec (1, b), 0);
7207 4 : pat = gen_rtx_PARALLEL (VOIDmode,
7208 : gen_rtvec (2, set, gen_rtx_USE (VOIDmode, vol)));
7209 4 : ASSERT_FALSE (noce_parallel_costed_insn_p (make_insn_raw (pat), true));
7210 4 : }
7211 :
7212 : /* Run the selftests in this file. */
7213 :
7214 : void
7215 4 : ifcvt_cc_tests ()
7216 : {
7217 4 : test_noce_pseudo_dependencies ();
7218 4 : test_noce_parallel_references ();
7219 4 : test_noce_hard_register_dependencies ();
7220 4 : test_noce_parallel_cost_gates ();
7221 4 : }
7222 :
7223 : } // namespace selftest
7224 :
7225 : #endif /* CHECKING_P. */
|