Branch data Line data Source code
1 : : /* If-conversion support.
2 : : Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify 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 "expr.h"
41 : : #include "output.h"
42 : : #include "cfgloop.h"
43 : : #include "tree-pass.h"
44 : : #include "dbgcnt.h"
45 : : #include "shrink-wrap.h"
46 : : #include "rtl-iter.h"
47 : : #include "ifcvt.h"
48 : :
49 : : #ifndef MAX_CONDITIONAL_EXECUTE
50 : : #define MAX_CONDITIONAL_EXECUTE \
51 : : (BRANCH_COST (optimize_function_for_speed_p (cfun), false) \
52 : : + 1)
53 : : #endif
54 : :
55 : : #define IFCVT_MULTIPLE_DUMPS 1
56 : :
57 : : #define NULL_BLOCK ((basic_block) NULL)
58 : :
59 : : /* True if after combine pass. */
60 : : static bool ifcvt_after_combine;
61 : :
62 : : /* True if the target has the cbranchcc4 optab. */
63 : : static bool have_cbranchcc4;
64 : :
65 : : /* # of IF-THEN or IF-THEN-ELSE blocks we looked at */
66 : : static int num_possible_if_blocks;
67 : :
68 : : /* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
69 : : execution. */
70 : : static int num_updated_if_blocks;
71 : :
72 : : /* # of changes made. */
73 : : static int num_true_changes;
74 : :
75 : : /* Whether conditional execution changes were made. */
76 : : static bool cond_exec_changed_p;
77 : :
78 : : /* Forward references. */
79 : : static int count_bb_insns (const_basic_block);
80 : : static bool cheap_bb_rtx_cost_p (const_basic_block, profile_probability, int);
81 : : static rtx_insn *first_active_insn (basic_block);
82 : : static rtx_insn *last_active_insn (basic_block, bool);
83 : : static rtx_insn *find_active_insn_before (basic_block, rtx_insn *);
84 : : static rtx_insn *find_active_insn_after (basic_block, rtx_insn *);
85 : : static basic_block block_fallthru (basic_block);
86 : : static rtx cond_exec_get_condition (rtx_insn *, bool);
87 : : static rtx noce_get_condition (rtx_insn *, rtx_insn **, bool);
88 : : static bool noce_operand_ok (const_rtx);
89 : : static void merge_if_block (ce_if_block *);
90 : : static bool find_cond_trap (basic_block, edge, edge);
91 : : static basic_block find_if_header (basic_block, int);
92 : : static int block_jumps_and_fallthru (basic_block, basic_block);
93 : : static bool noce_find_if_block (basic_block, edge, edge, int);
94 : : static bool cond_exec_find_if_block (ce_if_block *);
95 : : static bool find_if_case_1 (basic_block, edge, edge);
96 : : static bool find_if_case_2 (basic_block, edge, edge);
97 : : static bool dead_or_predicable (basic_block, basic_block, basic_block,
98 : : edge, bool);
99 : : static void noce_emit_move_insn (rtx, rtx);
100 : : static rtx_insn *block_has_only_trap (basic_block);
101 : : static void init_noce_multiple_sets_info (basic_block,
102 : : auto_delete_vec<noce_multiple_sets_info> &);
103 : : static bool noce_convert_multiple_sets_1 (struct noce_if_info *,
104 : : auto_delete_vec<noce_multiple_sets_info> &, int *);
105 : :
106 : : /* Count the number of non-jump active insns in BB. */
107 : :
108 : : static int
109 : 0 : count_bb_insns (const_basic_block bb)
110 : : {
111 : 0 : int count = 0;
112 : 0 : rtx_insn *insn = BB_HEAD (bb);
113 : :
114 : 0 : while (1)
115 : : {
116 : 0 : if (active_insn_p (insn) && !JUMP_P (insn))
117 : 0 : count++;
118 : :
119 : 0 : if (insn == BB_END (bb))
120 : : break;
121 : 0 : insn = NEXT_INSN (insn);
122 : : }
123 : :
124 : 0 : return count;
125 : : }
126 : :
127 : : /* Determine whether the total insn_cost on non-jump insns in
128 : : basic block BB is less than MAX_COST. This function returns
129 : : false if the cost of any instruction could not be estimated.
130 : :
131 : : The cost of the non-jump insns in BB is scaled by REG_BR_PROB_BASE
132 : : as those insns are being speculated. MAX_COST is scaled with SCALE
133 : : plus a small fudge factor. */
134 : :
135 : : static bool
136 : 3036067 : cheap_bb_rtx_cost_p (const_basic_block bb,
137 : : profile_probability prob, int max_cost)
138 : : {
139 : 3036067 : int count = 0;
140 : 3036067 : rtx_insn *insn = BB_HEAD (bb);
141 : 3036067 : bool speed = optimize_bb_for_speed_p (bb);
142 : 3036067 : int scale = prob.initialized_p () ? prob.to_reg_br_prob_base ()
143 : : : REG_BR_PROB_BASE;
144 : :
145 : : /* Set scale to REG_BR_PROB_BASE to void the identical scaling
146 : : applied to insn_cost when optimizing for size. Only do
147 : : this after combine because if-conversion might interfere with
148 : : passes before combine.
149 : :
150 : : Use optimize_function_for_speed_p instead of the pre-defined
151 : : variable speed to make sure it is set to same value for all
152 : : basic blocks in one if-conversion transformation. */
153 : 3036067 : if (!optimize_function_for_speed_p (cfun) && ifcvt_after_combine)
154 : : scale = REG_BR_PROB_BASE;
155 : : /* Our branch probability/scaling factors are just estimates and don't
156 : : account for cases where we can get speculation for free and other
157 : : secondary benefits. So we fudge the scale factor to make speculating
158 : : appear a little more profitable when optimizing for performance. */
159 : : else
160 : 2985603 : scale += REG_BR_PROB_BASE / 8;
161 : :
162 : :
163 : 3036067 : max_cost *= scale;
164 : :
165 : 11806677 : while (1)
166 : : {
167 : 14842744 : if (NONJUMP_INSN_P (insn))
168 : : {
169 : : /* Inline-asm's cost is not very estimatable.
170 : : It could be a costly instruction but the
171 : : estimate would be the same as a non costly
172 : : instruction. */
173 : 4551922 : if (asm_noperands (PATTERN (insn)) >= 0)
174 : : return false;
175 : :
176 : 4549317 : int cost = insn_cost (insn, speed) * REG_BR_PROB_BASE;
177 : 4549317 : if (cost == 0)
178 : : return false;
179 : :
180 : : /* If this instruction is the load or set of a "stack" register,
181 : : such as a floating point register on x87, then the cost of
182 : : speculatively executing this insn may need to include
183 : : the additional cost of popping its result off of the
184 : : register stack. Unfortunately, correctly recognizing and
185 : : accounting for this additional overhead is tricky, so for
186 : : now we simply prohibit such speculative execution. */
187 : : #ifdef STACK_REGS
188 : 4499773 : {
189 : 4499773 : rtx set = single_set (insn);
190 : 4499773 : if (set && STACK_REG_P (SET_DEST (set)))
191 : : return false;
192 : : }
193 : : #endif
194 : :
195 : 4495968 : count += cost;
196 : 4495968 : if (count >= max_cost)
197 : : return false;
198 : : }
199 : 10290822 : else if (CALL_P (insn))
200 : : return false;
201 : :
202 : 12433620 : if (insn == BB_END (bb))
203 : : break;
204 : 11806677 : insn = NEXT_INSN (insn);
205 : 11806677 : }
206 : :
207 : : return true;
208 : : }
209 : :
210 : : /* Return the first non-jump active insn in the basic block. */
211 : :
212 : : static rtx_insn *
213 : 1559185 : first_active_insn (basic_block bb)
214 : : {
215 : 1559185 : rtx_insn *insn = BB_HEAD (bb);
216 : :
217 : 1559185 : if (LABEL_P (insn))
218 : : {
219 : 281292 : if (insn == BB_END (bb))
220 : : return NULL;
221 : 281292 : insn = NEXT_INSN (insn);
222 : : }
223 : :
224 : 4870843 : while (NOTE_P (insn) || DEBUG_INSN_P (insn))
225 : : {
226 : 3311662 : if (insn == BB_END (bb))
227 : : return NULL;
228 : 3311658 : insn = NEXT_INSN (insn);
229 : : }
230 : :
231 : 1559181 : if (JUMP_P (insn))
232 : : return NULL;
233 : :
234 : : return insn;
235 : : }
236 : :
237 : : /* Return the last non-jump active (non-jump) insn in the basic block. */
238 : :
239 : : static rtx_insn *
240 : 2571208 : last_active_insn (basic_block bb, bool skip_use_p)
241 : : {
242 : 2571208 : rtx_insn *insn = BB_END (bb);
243 : 2571208 : rtx_insn *head = BB_HEAD (bb);
244 : :
245 : 2571208 : while (NOTE_P (insn)
246 : : || JUMP_P (insn)
247 : : || DEBUG_INSN_P (insn)
248 : 5867642 : || (skip_use_p
249 : 0 : && NONJUMP_INSN_P (insn)
250 : 0 : && GET_CODE (PATTERN (insn)) == USE))
251 : : {
252 : 3296744 : if (insn == head)
253 : : return NULL;
254 : 3296434 : insn = PREV_INSN (insn);
255 : : }
256 : :
257 : 2570898 : if (LABEL_P (insn))
258 : 95 : return NULL;
259 : :
260 : : return insn;
261 : : }
262 : :
263 : : /* Return the active insn before INSN inside basic block CURR_BB. */
264 : :
265 : : static rtx_insn *
266 : 0 : find_active_insn_before (basic_block curr_bb, rtx_insn *insn)
267 : : {
268 : 0 : if (!insn || insn == BB_HEAD (curr_bb))
269 : : return NULL;
270 : :
271 : 0 : while ((insn = PREV_INSN (insn)) != NULL_RTX)
272 : : {
273 : 0 : if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
274 : : break;
275 : :
276 : : /* No other active insn all the way to the start of the basic block. */
277 : 0 : if (insn == BB_HEAD (curr_bb))
278 : : return NULL;
279 : : }
280 : :
281 : : return insn;
282 : : }
283 : :
284 : : /* Return the active insn after INSN inside basic block CURR_BB. */
285 : :
286 : : static rtx_insn *
287 : 0 : find_active_insn_after (basic_block curr_bb, rtx_insn *insn)
288 : : {
289 : 0 : if (!insn || insn == BB_END (curr_bb))
290 : : return NULL;
291 : :
292 : 0 : while ((insn = NEXT_INSN (insn)) != NULL_RTX)
293 : : {
294 : 0 : if (NONJUMP_INSN_P (insn) || JUMP_P (insn) || CALL_P (insn))
295 : : break;
296 : :
297 : : /* No other active insn all the way to the end of the basic block. */
298 : 0 : if (insn == BB_END (curr_bb))
299 : : return NULL;
300 : : }
301 : :
302 : : return insn;
303 : : }
304 : :
305 : : /* Return the basic block reached by falling though the basic block BB. */
306 : :
307 : : static basic_block
308 : 0 : block_fallthru (basic_block bb)
309 : : {
310 : 0 : edge e = find_fallthru_edge (bb->succs);
311 : :
312 : 0 : return (e) ? e->dest : NULL_BLOCK;
313 : : }
314 : :
315 : : /* Return true if RTXs A and B can be safely interchanged. */
316 : :
317 : : static bool
318 : 512786 : rtx_interchangeable_p (const_rtx a, const_rtx b)
319 : : {
320 : 512786 : if (!rtx_equal_p (a, b))
321 : : return false;
322 : :
323 : 182700 : if (GET_CODE (a) != MEM)
324 : : return true;
325 : :
326 : : /* A dead type-unsafe memory reference is legal, but a live type-unsafe memory
327 : : reference is not. Interchanging a dead type-unsafe memory reference with
328 : : a live type-safe one creates a live type-unsafe memory reference, in other
329 : : words, it makes the program illegal.
330 : : We check here conservatively whether the two memory references have equal
331 : : memory attributes. */
332 : :
333 : 2629 : return mem_attrs_eq_p (get_mem_attrs (a), get_mem_attrs (b));
334 : : }
335 : :
336 : :
337 : : /* Go through a bunch of insns, converting them to conditional
338 : : execution format if possible. Return TRUE if all of the non-note
339 : : insns were processed. */
340 : :
341 : : static bool
342 : 0 : cond_exec_process_insns (ce_if_block *ce_info ATTRIBUTE_UNUSED,
343 : : /* if block information */rtx_insn *start,
344 : : /* first insn to look at */rtx end,
345 : : /* last insn to look at */rtx test,
346 : : /* conditional execution test */profile_probability
347 : : prob_val,
348 : : /* probability of branch taken. */bool mod_ok)
349 : : {
350 : 0 : bool must_be_last = false;
351 : 0 : rtx_insn *insn;
352 : 0 : rtx xtest;
353 : 0 : rtx pattern;
354 : :
355 : 0 : if (!start || !end)
356 : : return false;
357 : :
358 : 0 : for (insn = start; ; insn = NEXT_INSN (insn))
359 : : {
360 : : /* dwarf2out can't cope with conditional prologues. */
361 : 0 : if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
362 : : return false;
363 : :
364 : 0 : if (NOTE_P (insn) || DEBUG_INSN_P (insn))
365 : 0 : goto insn_done;
366 : :
367 : 0 : gcc_assert (NONJUMP_INSN_P (insn) || CALL_P (insn));
368 : :
369 : : /* dwarf2out can't cope with conditional unwind info. */
370 : 0 : if (RTX_FRAME_RELATED_P (insn))
371 : : return false;
372 : :
373 : : /* Remove USE insns that get in the way. */
374 : 0 : if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
375 : : {
376 : : /* ??? Ug. Actually unlinking the thing is problematic,
377 : : given what we'd have to coordinate with our callers. */
378 : 0 : SET_INSN_DELETED (insn);
379 : 0 : goto insn_done;
380 : : }
381 : :
382 : : /* Last insn wasn't last? */
383 : 0 : if (must_be_last)
384 : : return false;
385 : :
386 : 0 : if (modified_in_p (test, insn))
387 : : {
388 : 0 : if (!mod_ok)
389 : : return false;
390 : : must_be_last = true;
391 : : }
392 : :
393 : : /* Now build the conditional form of the instruction. */
394 : 0 : pattern = PATTERN (insn);
395 : 0 : xtest = copy_rtx (test);
396 : :
397 : : /* If this is already a COND_EXEC, rewrite the test to be an AND of the
398 : : two conditions. */
399 : 0 : if (GET_CODE (pattern) == COND_EXEC)
400 : : {
401 : 0 : if (GET_MODE (xtest) != GET_MODE (COND_EXEC_TEST (pattern)))
402 : : return false;
403 : :
404 : 0 : xtest = gen_rtx_AND (GET_MODE (xtest), xtest,
405 : : COND_EXEC_TEST (pattern));
406 : 0 : pattern = COND_EXEC_CODE (pattern);
407 : : }
408 : :
409 : 0 : pattern = gen_rtx_COND_EXEC (VOIDmode, xtest, pattern);
410 : :
411 : : /* If the machine needs to modify the insn being conditionally executed,
412 : : say for example to force a constant integer operand into a temp
413 : : register, do so here. */
414 : : #ifdef IFCVT_MODIFY_INSN
415 : : IFCVT_MODIFY_INSN (ce_info, pattern, insn);
416 : : if (! pattern)
417 : : return false;
418 : : #endif
419 : :
420 : 0 : validate_change (insn, &PATTERN (insn), pattern, 1);
421 : :
422 : 0 : if (CALL_P (insn) && prob_val.initialized_p ())
423 : 0 : validate_change (insn, ®_NOTES (insn),
424 : : gen_rtx_INT_LIST ((machine_mode) REG_BR_PROB,
425 : : prob_val.to_reg_br_prob_note (),
426 : : REG_NOTES (insn)), 1);
427 : :
428 : 0 : insn_done:
429 : 0 : if (insn == end)
430 : : break;
431 : 0 : }
432 : :
433 : : return true;
434 : : }
435 : :
436 : : /* Return the condition for a jump. Do not do any special processing. */
437 : :
438 : : static rtx
439 : 153670 : cond_exec_get_condition (rtx_insn *jump, bool get_reversed = false)
440 : : {
441 : 153670 : rtx test_if, cond;
442 : :
443 : 153670 : if (any_condjump_p (jump))
444 : 153670 : test_if = SET_SRC (pc_set (jump));
445 : : else
446 : : return NULL_RTX;
447 : 153670 : cond = XEXP (test_if, 0);
448 : :
449 : : /* If this branches to JUMP_LABEL when the condition is false,
450 : : reverse the condition. */
451 : 153670 : if (get_reversed
452 : 153670 : || (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
453 : 0 : && label_ref_label (XEXP (test_if, 2))
454 : 0 : == JUMP_LABEL (jump)))
455 : : {
456 : 76835 : enum rtx_code rev = reversed_comparison_code (cond, jump);
457 : 76835 : if (rev == UNKNOWN)
458 : : return NULL_RTX;
459 : :
460 : 76835 : cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
461 : : XEXP (cond, 1));
462 : : }
463 : :
464 : : return cond;
465 : : }
466 : :
467 : : /* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
468 : : to conditional execution. Return TRUE if we were successful at
469 : : converting the block. */
470 : :
471 : : static bool
472 : 0 : cond_exec_process_if_block (ce_if_block * ce_info,
473 : : /* if block information */bool do_multiple_p)
474 : : {
475 : 0 : basic_block test_bb = ce_info->test_bb; /* last test block */
476 : 0 : basic_block then_bb = ce_info->then_bb; /* THEN */
477 : 0 : basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
478 : 0 : rtx test_expr; /* expression in IF_THEN_ELSE that is tested */
479 : 0 : rtx_insn *then_start; /* first insn in THEN block */
480 : 0 : rtx_insn *then_end; /* last insn + 1 in THEN block */
481 : 0 : rtx_insn *else_start = NULL; /* first insn in ELSE block or NULL */
482 : 0 : rtx_insn *else_end = NULL; /* last insn + 1 in ELSE block */
483 : 0 : int max; /* max # of insns to convert. */
484 : 0 : bool then_mod_ok; /* whether conditional mods are ok in THEN */
485 : 0 : rtx true_expr; /* test for else block insns */
486 : 0 : rtx false_expr; /* test for then block insns */
487 : 0 : profile_probability true_prob_val;/* probability of else block */
488 : 0 : profile_probability false_prob_val;/* probability of then block */
489 : 0 : rtx_insn *then_last_head = NULL; /* Last match at the head of THEN */
490 : 0 : rtx_insn *else_last_head = NULL; /* Last match at the head of ELSE */
491 : 0 : rtx_insn *then_first_tail = NULL; /* First match at the tail of THEN */
492 : 0 : rtx_insn *else_first_tail = NULL; /* First match at the tail of ELSE */
493 : 0 : int then_n_insns, else_n_insns, n_insns;
494 : 0 : enum rtx_code false_code;
495 : 0 : rtx note;
496 : :
497 : : /* If test is comprised of && or || elements, and we've failed at handling
498 : : all of them together, just use the last test if it is the special case of
499 : : && elements without an ELSE block. */
500 : 0 : if (!do_multiple_p && ce_info->num_multiple_test_blocks)
501 : : {
502 : 0 : if (else_bb || ! ce_info->and_and_p)
503 : : return false;
504 : :
505 : 0 : ce_info->test_bb = test_bb = ce_info->last_test_bb;
506 : 0 : ce_info->num_multiple_test_blocks = 0;
507 : 0 : ce_info->num_and_and_blocks = 0;
508 : 0 : ce_info->num_or_or_blocks = 0;
509 : : }
510 : :
511 : : /* Find the conditional jump to the ELSE or JOIN part, and isolate
512 : : the test. */
513 : 0 : test_expr = cond_exec_get_condition (BB_END (test_bb));
514 : 0 : if (! test_expr)
515 : : return false;
516 : :
517 : : /* If the conditional jump is more than just a conditional jump,
518 : : then we cannot do conditional execution conversion on this block. */
519 : 0 : if (! onlyjump_p (BB_END (test_bb)))
520 : : return false;
521 : :
522 : : /* Collect the bounds of where we're to search, skipping any labels, jumps
523 : : and notes at the beginning and end of the block. Then count the total
524 : : number of insns and see if it is small enough to convert. */
525 : 0 : then_start = first_active_insn (then_bb);
526 : 0 : then_end = last_active_insn (then_bb, true);
527 : 0 : then_n_insns = ce_info->num_then_insns = count_bb_insns (then_bb);
528 : 0 : n_insns = then_n_insns;
529 : 0 : max = MAX_CONDITIONAL_EXECUTE;
530 : :
531 : 0 : if (else_bb)
532 : : {
533 : 0 : int n_matching;
534 : :
535 : 0 : max *= 2;
536 : 0 : else_start = first_active_insn (else_bb);
537 : 0 : else_end = last_active_insn (else_bb, true);
538 : 0 : else_n_insns = ce_info->num_else_insns = count_bb_insns (else_bb);
539 : 0 : n_insns += else_n_insns;
540 : :
541 : : /* Look for matching sequences at the head and tail of the two blocks,
542 : : and limit the range of insns to be converted if possible. */
543 : 0 : n_matching = flow_find_cross_jump (then_bb, else_bb,
544 : : &then_first_tail, &else_first_tail,
545 : : NULL);
546 : 0 : if (then_first_tail == BB_HEAD (then_bb))
547 : 0 : then_start = then_end = NULL;
548 : 0 : if (else_first_tail == BB_HEAD (else_bb))
549 : 0 : else_start = else_end = NULL;
550 : :
551 : 0 : if (n_matching > 0)
552 : : {
553 : 0 : if (then_end)
554 : 0 : then_end = find_active_insn_before (then_bb, then_first_tail);
555 : 0 : if (else_end)
556 : 0 : else_end = find_active_insn_before (else_bb, else_first_tail);
557 : 0 : n_insns -= 2 * n_matching;
558 : : }
559 : :
560 : 0 : if (then_start
561 : 0 : && else_start
562 : : && then_n_insns > n_matching
563 : 0 : && else_n_insns > n_matching)
564 : : {
565 : 0 : int longest_match = MIN (then_n_insns - n_matching,
566 : : else_n_insns - n_matching);
567 : 0 : n_matching
568 : 0 : = flow_find_head_matching_sequence (then_bb, else_bb,
569 : : &then_last_head,
570 : : &else_last_head,
571 : : longest_match);
572 : :
573 : 0 : if (n_matching > 0)
574 : : {
575 : 0 : rtx_insn *insn;
576 : :
577 : : /* We won't pass the insns in the head sequence to
578 : : cond_exec_process_insns, so we need to test them here
579 : : to make sure that they don't clobber the condition. */
580 : 0 : for (insn = BB_HEAD (then_bb);
581 : 0 : insn != NEXT_INSN (then_last_head);
582 : 0 : insn = NEXT_INSN (insn))
583 : 0 : if (!LABEL_P (insn) && !NOTE_P (insn)
584 : 0 : && !DEBUG_INSN_P (insn)
585 : 0 : && modified_in_p (test_expr, insn))
586 : : return false;
587 : : }
588 : :
589 : 0 : if (then_last_head == then_end)
590 : 0 : then_start = then_end = NULL;
591 : 0 : if (else_last_head == else_end)
592 : 0 : else_start = else_end = NULL;
593 : :
594 : 0 : if (n_matching > 0)
595 : : {
596 : 0 : if (then_start)
597 : 0 : then_start = find_active_insn_after (then_bb, then_last_head);
598 : 0 : if (else_start)
599 : 0 : else_start = find_active_insn_after (else_bb, else_last_head);
600 : 0 : n_insns -= 2 * n_matching;
601 : : }
602 : : }
603 : : }
604 : :
605 : 0 : if (n_insns > max)
606 : : return false;
607 : :
608 : : /* Map test_expr/test_jump into the appropriate MD tests to use on
609 : : the conditionally executed code. */
610 : :
611 : 0 : true_expr = test_expr;
612 : :
613 : 0 : false_code = reversed_comparison_code (true_expr, BB_END (test_bb));
614 : 0 : if (false_code != UNKNOWN)
615 : 0 : false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
616 : : XEXP (true_expr, 0), XEXP (true_expr, 1));
617 : : else
618 : : false_expr = NULL_RTX;
619 : :
620 : : #ifdef IFCVT_MODIFY_TESTS
621 : : /* If the machine description needs to modify the tests, such as setting a
622 : : conditional execution register from a comparison, it can do so here. */
623 : : IFCVT_MODIFY_TESTS (ce_info, true_expr, false_expr);
624 : :
625 : : /* See if the conversion failed. */
626 : : if (!true_expr || !false_expr)
627 : : goto fail;
628 : : #endif
629 : :
630 : 0 : note = find_reg_note (BB_END (test_bb), REG_BR_PROB, NULL_RTX);
631 : 0 : if (note)
632 : : {
633 : 0 : true_prob_val = profile_probability::from_reg_br_prob_note (XINT (note, 0));
634 : 0 : false_prob_val = true_prob_val.invert ();
635 : : }
636 : : else
637 : : {
638 : : true_prob_val = profile_probability::uninitialized ();
639 : : false_prob_val = profile_probability::uninitialized ();
640 : : }
641 : :
642 : : /* If we have && or || tests, do them here. These tests are in the adjacent
643 : : blocks after the first block containing the test. */
644 : 0 : if (ce_info->num_multiple_test_blocks > 0)
645 : : {
646 : 0 : basic_block bb = test_bb;
647 : 0 : basic_block last_test_bb = ce_info->last_test_bb;
648 : :
649 : 0 : if (! false_expr)
650 : 0 : goto fail;
651 : :
652 : 0 : do
653 : : {
654 : 0 : rtx_insn *start, *end;
655 : 0 : rtx t, f;
656 : 0 : enum rtx_code f_code;
657 : :
658 : 0 : bb = block_fallthru (bb);
659 : 0 : start = first_active_insn (bb);
660 : 0 : end = last_active_insn (bb, true);
661 : 0 : if (start
662 : 0 : && ! cond_exec_process_insns (ce_info, start, end, false_expr,
663 : : false_prob_val, false))
664 : 0 : goto fail;
665 : :
666 : : /* If the conditional jump is more than just a conditional jump, then
667 : : we cannot do conditional execution conversion on this block. */
668 : 0 : if (! onlyjump_p (BB_END (bb)))
669 : 0 : goto fail;
670 : :
671 : : /* Find the conditional jump and isolate the test. */
672 : 0 : t = cond_exec_get_condition (BB_END (bb));
673 : 0 : if (! t)
674 : 0 : goto fail;
675 : :
676 : 0 : f_code = reversed_comparison_code (t, BB_END (bb));
677 : 0 : if (f_code == UNKNOWN)
678 : 0 : goto fail;
679 : :
680 : 0 : f = gen_rtx_fmt_ee (f_code, GET_MODE (t), XEXP (t, 0), XEXP (t, 1));
681 : 0 : if (ce_info->and_and_p)
682 : : {
683 : 0 : t = gen_rtx_AND (GET_MODE (t), true_expr, t);
684 : 0 : f = gen_rtx_IOR (GET_MODE (t), false_expr, f);
685 : : }
686 : : else
687 : : {
688 : 0 : t = gen_rtx_IOR (GET_MODE (t), true_expr, t);
689 : 0 : f = gen_rtx_AND (GET_MODE (t), false_expr, f);
690 : : }
691 : :
692 : : /* If the machine description needs to modify the tests, such as
693 : : setting a conditional execution register from a comparison, it can
694 : : do so here. */
695 : : #ifdef IFCVT_MODIFY_MULTIPLE_TESTS
696 : : IFCVT_MODIFY_MULTIPLE_TESTS (ce_info, bb, t, f);
697 : :
698 : : /* See if the conversion failed. */
699 : : if (!t || !f)
700 : : goto fail;
701 : : #endif
702 : :
703 : 0 : true_expr = t;
704 : 0 : false_expr = f;
705 : : }
706 : 0 : while (bb != last_test_bb);
707 : : }
708 : :
709 : : /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
710 : : on then THEN block. */
711 : 0 : then_mod_ok = (else_bb == NULL_BLOCK);
712 : :
713 : : /* Go through the THEN and ELSE blocks converting the insns if possible
714 : : to conditional execution. */
715 : :
716 : 0 : if (then_end
717 : 0 : && (! false_expr
718 : 0 : || ! cond_exec_process_insns (ce_info, then_start, then_end,
719 : : false_expr, false_prob_val,
720 : : then_mod_ok)))
721 : 0 : goto fail;
722 : :
723 : 0 : if (else_bb && else_end
724 : 0 : && ! cond_exec_process_insns (ce_info, else_start, else_end,
725 : : true_expr, true_prob_val, true))
726 : 0 : goto fail;
727 : :
728 : : /* If we cannot apply the changes, fail. Do not go through the normal fail
729 : : processing, since apply_change_group will call cancel_changes. */
730 : 0 : if (! apply_change_group ())
731 : : {
732 : : #ifdef IFCVT_MODIFY_CANCEL
733 : : /* Cancel any machine dependent changes. */
734 : : IFCVT_MODIFY_CANCEL (ce_info);
735 : : #endif
736 : : return false;
737 : : }
738 : :
739 : : #ifdef IFCVT_MODIFY_FINAL
740 : : /* Do any machine dependent final modifications. */
741 : : IFCVT_MODIFY_FINAL (ce_info);
742 : : #endif
743 : :
744 : : /* Conversion succeeded. */
745 : 0 : if (dump_file)
746 : 0 : fprintf (dump_file, "%d insn%s converted to conditional execution.\n",
747 : : n_insns, (n_insns == 1) ? " was" : "s were");
748 : :
749 : : /* Merge the blocks! If we had matching sequences, make sure to delete one
750 : : copy at the appropriate location first: delete the copy in the THEN branch
751 : : for a tail sequence so that the remaining one is executed last for both
752 : : branches, and delete the copy in the ELSE branch for a head sequence so
753 : : that the remaining one is executed first for both branches. */
754 : 0 : if (then_first_tail)
755 : : {
756 : 0 : rtx_insn *from = then_first_tail;
757 : 0 : if (!INSN_P (from))
758 : 0 : from = find_active_insn_after (then_bb, from);
759 : 0 : delete_insn_chain (from, get_last_bb_insn (then_bb), false);
760 : : }
761 : 0 : if (else_last_head)
762 : 0 : delete_insn_chain (first_active_insn (else_bb), else_last_head, false);
763 : :
764 : 0 : merge_if_block (ce_info);
765 : 0 : cond_exec_changed_p = true;
766 : 0 : return true;
767 : :
768 : 0 : fail:
769 : : #ifdef IFCVT_MODIFY_CANCEL
770 : : /* Cancel any machine dependent changes. */
771 : : IFCVT_MODIFY_CANCEL (ce_info);
772 : : #endif
773 : :
774 : 0 : cancel_changes (0);
775 : 0 : return false;
776 : : }
777 : :
778 : : static rtx noce_emit_store_flag (struct noce_if_info *, rtx, bool, int);
779 : : static bool noce_try_move (struct noce_if_info *);
780 : : static bool noce_try_ifelse_collapse (struct noce_if_info *);
781 : : static bool noce_try_store_flag (struct noce_if_info *);
782 : : static bool noce_try_addcc (struct noce_if_info *);
783 : : static bool noce_try_store_flag_constants (struct noce_if_info *);
784 : : static bool noce_try_store_flag_mask (struct noce_if_info *);
785 : : static rtx noce_emit_cmove (struct noce_if_info *, rtx, enum rtx_code, rtx,
786 : : rtx, rtx, rtx, rtx = NULL, rtx = NULL);
787 : : static bool noce_try_cmove (struct noce_if_info *);
788 : : static bool noce_try_cmove_arith (struct noce_if_info *);
789 : : static rtx noce_get_alt_condition (struct noce_if_info *, rtx, rtx_insn **);
790 : : static bool noce_try_minmax (struct noce_if_info *);
791 : : static bool noce_try_abs (struct noce_if_info *);
792 : : static bool noce_try_sign_mask (struct noce_if_info *);
793 : : static int noce_try_cond_zero_arith (struct noce_if_info *);
794 : :
795 : : /* Return the comparison code for reversed condition for IF_INFO,
796 : : or UNKNOWN if reversing the condition is not possible. */
797 : :
798 : : static inline enum rtx_code
799 : 363104 : noce_reversed_cond_code (struct noce_if_info *if_info)
800 : : {
801 : 363104 : if (if_info->rev_cond)
802 : 363104 : return GET_CODE (if_info->rev_cond);
803 : 0 : return reversed_comparison_code (if_info->cond, if_info->jump);
804 : : }
805 : :
806 : : /* Return true if SEQ is a good candidate as a replacement for the
807 : : if-convertible sequence described in IF_INFO.
808 : : This is the default implementation that targets can override
809 : : through a target hook. */
810 : :
811 : : bool
812 : 211230 : default_noce_conversion_profitable_p (rtx_insn *seq,
813 : : struct noce_if_info *if_info)
814 : : {
815 : 211230 : bool speed_p = if_info->speed_p;
816 : :
817 : : /* Cost up the new sequence. */
818 : 211230 : unsigned int cost = seq_cost (seq, speed_p);
819 : :
820 : 211230 : if (cost <= if_info->original_cost)
821 : : return true;
822 : :
823 : : /* When compiling for size, we can make a reasonably accurately guess
824 : : at the size growth. When compiling for speed, use the maximum. */
825 : 178707 : return speed_p && cost <= if_info->max_seq_cost;
826 : : }
827 : :
828 : : /* Helper function for noce_try_store_flag*. */
829 : :
830 : : static rtx
831 : 47285 : noce_emit_store_flag (struct noce_if_info *if_info, rtx x, bool reversep,
832 : : int normalize)
833 : : {
834 : 47285 : rtx cond = if_info->cond;
835 : 47285 : bool cond_complex;
836 : 47285 : enum rtx_code code;
837 : :
838 : 47285 : cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
839 : 47285 : || ! general_operand (XEXP (cond, 1), VOIDmode));
840 : :
841 : : /* If earliest == jump, or when the condition is complex, try to
842 : : build the store_flag insn directly. */
843 : :
844 : 47285 : if (cond_complex)
845 : : {
846 : 23309 : rtx set = pc_set (if_info->jump);
847 : 23309 : cond = XEXP (SET_SRC (set), 0);
848 : 23309 : if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
849 : 23309 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
850 : 0 : reversep = !reversep;
851 : 23309 : if (if_info->then_else_reversed)
852 : 20673 : reversep = !reversep;
853 : : }
854 : 23976 : else if (reversep
855 : 17118 : && if_info->rev_cond
856 : 17118 : && general_operand (XEXP (if_info->rev_cond, 0), VOIDmode)
857 : 41094 : && general_operand (XEXP (if_info->rev_cond, 1), VOIDmode))
858 : : {
859 : 17118 : cond = if_info->rev_cond;
860 : 17118 : reversep = false;
861 : : }
862 : :
863 : 47285 : if (reversep)
864 : 4205 : code = reversed_comparison_code (cond, if_info->jump);
865 : : else
866 : 43080 : code = GET_CODE (cond);
867 : :
868 : 47285 : if ((if_info->cond_earliest == if_info->jump || cond_complex)
869 : 23309 : && (normalize == 0 || STORE_FLAG_VALUE == normalize))
870 : : {
871 : 17885 : rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
872 : : XEXP (cond, 1));
873 : 17885 : rtx set = gen_rtx_SET (x, src);
874 : :
875 : 17885 : start_sequence ();
876 : 17885 : rtx_insn *insn = emit_insn (set);
877 : :
878 : 17885 : if (recog_memoized (insn) >= 0)
879 : : {
880 : 7843 : rtx_insn *seq = end_sequence ();
881 : 7843 : emit_insn (seq);
882 : :
883 : 7843 : if_info->cond_earliest = if_info->jump;
884 : :
885 : 7843 : return x;
886 : : }
887 : :
888 : 10042 : end_sequence ();
889 : : }
890 : :
891 : : /* Don't even try if the comparison operands or the mode of X are weird. */
892 : 39442 : if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
893 : 15466 : return NULL_RTX;
894 : :
895 : 23976 : return emit_store_flag (x, code, XEXP (cond, 0),
896 : : XEXP (cond, 1), VOIDmode,
897 : 23976 : (code == LTU || code == LEU
898 : 47952 : || code == GEU || code == GTU), normalize);
899 : : }
900 : :
901 : : /* Return true if X can be safely forced into a register by copy_to_mode_reg
902 : : / force_operand. */
903 : :
904 : : static bool
905 : 1909520 : noce_can_force_operand (rtx x)
906 : : {
907 : 1909520 : if (general_operand (x, VOIDmode))
908 : : return true;
909 : 411209 : if (SUBREG_P (x))
910 : : {
911 : 1210 : if (!noce_can_force_operand (SUBREG_REG (x)))
912 : : return false;
913 : : return true;
914 : : }
915 : 409999 : if (ARITHMETIC_P (x))
916 : : {
917 : 351809 : if (!noce_can_force_operand (XEXP (x, 0))
918 : 351809 : || !noce_can_force_operand (XEXP (x, 1)))
919 : 4072 : return false;
920 : 347737 : switch (GET_CODE (x))
921 : : {
922 : : case MULT:
923 : : case DIV:
924 : : case MOD:
925 : : case UDIV:
926 : : case UMOD:
927 : : return true;
928 : 339415 : default:
929 : 339415 : return code_to_optab (GET_CODE (x));
930 : : }
931 : : }
932 : 58190 : if (UNARY_P (x))
933 : : {
934 : 51399 : if (!noce_can_force_operand (XEXP (x, 0)))
935 : : return false;
936 : 51387 : switch (GET_CODE (x))
937 : : {
938 : : case ZERO_EXTEND:
939 : : case SIGN_EXTEND:
940 : : case TRUNCATE:
941 : : case FLOAT_EXTEND:
942 : : case FLOAT_TRUNCATE:
943 : : case FIX:
944 : : case UNSIGNED_FIX:
945 : : case FLOAT:
946 : : case UNSIGNED_FLOAT:
947 : : return true;
948 : 8573 : default:
949 : 8573 : return code_to_optab (GET_CODE (x));
950 : : }
951 : : }
952 : : return false;
953 : : }
954 : :
955 : : /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
956 : : X is the destination/target and Y is the value to copy. */
957 : :
958 : : static void
959 : 116578 : noce_emit_move_insn (rtx x, rtx y)
960 : : {
961 : 116578 : machine_mode outmode;
962 : 116578 : rtx outer, inner;
963 : 116578 : poly_int64 bitpos;
964 : :
965 : 116578 : if (GET_CODE (x) != STRICT_LOW_PART)
966 : : {
967 : 116578 : rtx_insn *seq, *insn;
968 : 116578 : rtx target;
969 : 116578 : optab ot;
970 : :
971 : 116578 : start_sequence ();
972 : : /* Check that the SET_SRC is reasonable before calling emit_move_insn,
973 : : otherwise construct a suitable SET pattern ourselves. */
974 : 116639 : insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
975 : 116639 : ? emit_move_insn (x, y)
976 : 90203 : : emit_insn (gen_rtx_SET (x, y));
977 : 116578 : seq = end_sequence ();
978 : :
979 : 116578 : if (recog_memoized (insn) <= 0)
980 : : {
981 : 58800 : if (GET_CODE (x) == ZERO_EXTRACT)
982 : : {
983 : 0 : rtx op = XEXP (x, 0);
984 : 0 : unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
985 : 0 : unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
986 : :
987 : : /* store_bit_field expects START to be relative to
988 : : BYTES_BIG_ENDIAN and adjusts this value for machines with
989 : : BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
990 : : invoke store_bit_field again it is necessary to have the START
991 : : value from the first call. */
992 : 0 : if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
993 : : {
994 : : if (MEM_P (op))
995 : : start = BITS_PER_UNIT - start - size;
996 : : else
997 : : {
998 : : gcc_assert (REG_P (op));
999 : : start = BITS_PER_WORD - start - size;
1000 : : }
1001 : : }
1002 : :
1003 : 0 : gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
1004 : 0 : store_bit_field (op, size, start, 0, 0, GET_MODE (x), y, false,
1005 : : false);
1006 : 0 : return;
1007 : : }
1008 : :
1009 : 58800 : switch (GET_RTX_CLASS (GET_CODE (y)))
1010 : : {
1011 : 1246 : case RTX_UNARY:
1012 : 1246 : ot = code_to_optab (GET_CODE (y));
1013 : 1246 : if (ot && noce_can_force_operand (XEXP (y, 0)))
1014 : : {
1015 : 908 : start_sequence ();
1016 : 908 : target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
1017 : 908 : if (target != NULL_RTX)
1018 : : {
1019 : 908 : if (target != x)
1020 : 0 : emit_move_insn (x, target);
1021 : 908 : seq = get_insns ();
1022 : : }
1023 : 908 : end_sequence ();
1024 : : }
1025 : : break;
1026 : :
1027 : 36392 : case RTX_BIN_ARITH:
1028 : 36392 : case RTX_COMM_ARITH:
1029 : 36392 : ot = code_to_optab (GET_CODE (y));
1030 : 36392 : if (ot
1031 : 36392 : && noce_can_force_operand (XEXP (y, 0))
1032 : 72784 : && noce_can_force_operand (XEXP (y, 1)))
1033 : : {
1034 : 36392 : start_sequence ();
1035 : 36392 : target = expand_binop (GET_MODE (y), ot,
1036 : : XEXP (y, 0), XEXP (y, 1),
1037 : : x, 0, OPTAB_DIRECT);
1038 : 36392 : if (target != NULL_RTX)
1039 : : {
1040 : 36364 : if (target != x)
1041 : 0 : emit_move_insn (x, target);
1042 : 36364 : seq = get_insns ();
1043 : : }
1044 : 36392 : end_sequence ();
1045 : : }
1046 : : break;
1047 : :
1048 : : default:
1049 : : break;
1050 : : }
1051 : : }
1052 : :
1053 : 116578 : emit_insn (seq);
1054 : 116578 : return;
1055 : : }
1056 : :
1057 : 0 : outer = XEXP (x, 0);
1058 : 0 : inner = XEXP (outer, 0);
1059 : 0 : outmode = GET_MODE (outer);
1060 : 0 : bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1061 : 0 : store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1062 : : 0, 0, outmode, y, false, false);
1063 : : }
1064 : :
1065 : : /* Return the CC reg if it is used in COND. */
1066 : :
1067 : : static rtx
1068 : 4198005 : cc_in_cond (rtx cond)
1069 : : {
1070 : 4198005 : if (have_cbranchcc4 && cond
1071 : 4198005 : && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1072 : 74034 : return XEXP (cond, 0);
1073 : :
1074 : : return NULL_RTX;
1075 : : }
1076 : :
1077 : : /* Return sequence of instructions generated by if conversion. This
1078 : : function calls end_sequence() to end the current stream, ensures
1079 : : that the instructions are unshared, recognizable non-jump insns.
1080 : : On failure, this function returns a NULL_RTX. */
1081 : :
1082 : : static rtx_insn *
1083 : 241566 : end_ifcvt_sequence (struct noce_if_info *if_info)
1084 : : {
1085 : 241566 : rtx_insn *insn;
1086 : 241566 : rtx_insn *seq = get_insns ();
1087 : 241566 : rtx cc = cc_in_cond (if_info->cond);
1088 : :
1089 : 241566 : set_used_flags (if_info->x);
1090 : 241566 : set_used_flags (if_info->cond);
1091 : 241566 : set_used_flags (if_info->a);
1092 : 241566 : set_used_flags (if_info->b);
1093 : :
1094 : 1367949 : for (insn = seq; insn; insn = NEXT_INSN (insn))
1095 : 884817 : set_used_flags (insn);
1096 : :
1097 : 241566 : unshare_all_rtl_in_chain (seq);
1098 : 241566 : end_sequence ();
1099 : :
1100 : : /* Make sure that all of the instructions emitted are recognizable,
1101 : : and that we haven't introduced a new jump instruction.
1102 : : As an exercise for the reader, build a general mechanism that
1103 : : allows proper placement of required clobbers. */
1104 : 1343273 : for (insn = seq; insn; insn = NEXT_INSN (insn))
1105 : 882100 : if (JUMP_P (insn)
1106 : 882100 : || recog_memoized (insn) == -1
1107 : : /* Make sure new generated code does not clobber CC. */
1108 : 1742321 : || (cc && set_of (cc, insn)))
1109 : 21959 : return NULL;
1110 : :
1111 : : return seq;
1112 : : }
1113 : :
1114 : : /* Return true iff the then and else basic block (if it exists)
1115 : : consist of a single simple set instruction. */
1116 : :
1117 : : static bool
1118 : 3354687 : noce_simple_bbs (struct noce_if_info *if_info)
1119 : : {
1120 : 0 : if (!if_info->then_simple)
1121 : : return false;
1122 : :
1123 : 3064961 : if (if_info->else_bb)
1124 : 0 : return if_info->else_simple;
1125 : :
1126 : : return true;
1127 : : }
1128 : :
1129 : : /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1130 : : "if (a == b) x = a; else x = b" into "x = b". */
1131 : :
1132 : : static bool
1133 : 276330 : noce_try_move (struct noce_if_info *if_info)
1134 : : {
1135 : 276330 : rtx cond = if_info->cond;
1136 : 276330 : enum rtx_code code = GET_CODE (cond);
1137 : 276330 : rtx y;
1138 : 276330 : rtx_insn *seq;
1139 : :
1140 : 276330 : if (code != NE && code != EQ)
1141 : : return false;
1142 : :
1143 : 524759 : if (!noce_simple_bbs (if_info))
1144 : : return false;
1145 : :
1146 : : /* This optimization isn't valid if either A or B could be a NaN
1147 : : or a signed zero. */
1148 : 193833 : if (HONOR_NANS (if_info->x)
1149 : 193833 : || HONOR_SIGNED_ZEROS (if_info->x))
1150 : 54400 : return false;
1151 : :
1152 : : /* Check whether the operands of the comparison are A and in
1153 : : either order. */
1154 : 139433 : if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1155 : 2243 : && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1156 : 141665 : || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1157 : 1959 : && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1158 : : {
1159 : 16 : if (!rtx_interchangeable_p (if_info->a, if_info->b))
1160 : : return false;
1161 : :
1162 : 0 : y = (code == EQ) ? if_info->a : if_info->b;
1163 : :
1164 : : /* Avoid generating the move if the source is the destination. */
1165 : 0 : if (! rtx_equal_p (if_info->x, y))
1166 : : {
1167 : 0 : start_sequence ();
1168 : 0 : noce_emit_move_insn (if_info->x, y);
1169 : 0 : seq = end_ifcvt_sequence (if_info);
1170 : 0 : if (!seq)
1171 : : return false;
1172 : :
1173 : 0 : emit_insn_before_setloc (seq, if_info->jump,
1174 : 0 : INSN_LOCATION (if_info->insn_a));
1175 : : }
1176 : 0 : if_info->transform_name = "noce_try_move";
1177 : 0 : return true;
1178 : : }
1179 : : return false;
1180 : : }
1181 : :
1182 : : /* Try forming an IF_THEN_ELSE (cond, b, a) and collapsing that
1183 : : through simplify_rtx. Sometimes that can eliminate the IF_THEN_ELSE.
1184 : : If that is the case, emit the result into x. */
1185 : :
1186 : : static bool
1187 : 276330 : noce_try_ifelse_collapse (struct noce_if_info * if_info)
1188 : : {
1189 : 591714 : if (!noce_simple_bbs (if_info))
1190 : : return false;
1191 : :
1192 : 251165 : machine_mode mode = GET_MODE (if_info->x);
1193 : 251165 : rtx if_then_else = simplify_gen_ternary (IF_THEN_ELSE, mode, mode,
1194 : : if_info->cond, if_info->b,
1195 : : if_info->a);
1196 : :
1197 : 251165 : if (GET_CODE (if_then_else) == IF_THEN_ELSE)
1198 : : return false;
1199 : :
1200 : 23277 : rtx_insn *seq;
1201 : 23277 : start_sequence ();
1202 : 23277 : noce_emit_move_insn (if_info->x, if_then_else);
1203 : 23277 : seq = end_ifcvt_sequence (if_info);
1204 : 23277 : if (!seq)
1205 : : return false;
1206 : :
1207 : 2115 : emit_insn_before_setloc (seq, if_info->jump,
1208 : 2115 : INSN_LOCATION (if_info->insn_a));
1209 : :
1210 : 2115 : if_info->transform_name = "noce_try_ifelse_collapse";
1211 : 2115 : return true;
1212 : : }
1213 : :
1214 : :
1215 : : /* Convert "if (test) x = 1; else x = 0".
1216 : :
1217 : : Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1218 : : tried in noce_try_store_flag_constants after noce_try_cmove has had
1219 : : a go at the conversion. */
1220 : :
1221 : : static bool
1222 : 274215 : noce_try_store_flag (struct noce_if_info *if_info)
1223 : : {
1224 : 274215 : bool reversep;
1225 : 274215 : rtx target;
1226 : 274215 : rtx_insn *seq;
1227 : :
1228 : 573019 : if (!noce_simple_bbs (if_info))
1229 : : return false;
1230 : :
1231 : 249050 : if (CONST_INT_P (if_info->b)
1232 : 68884 : && INTVAL (if_info->b) == STORE_FLAG_VALUE
1233 : 3707 : && if_info->a == const0_rtx)
1234 : : reversep = false;
1235 : 247876 : else if (if_info->b == const0_rtx
1236 : 32862 : && CONST_INT_P (if_info->a)
1237 : 20403 : && INTVAL (if_info->a) == STORE_FLAG_VALUE
1238 : 267865 : && noce_reversed_cond_code (if_info) != UNKNOWN)
1239 : : reversep = true;
1240 : : else
1241 : 227887 : return false;
1242 : :
1243 : 21163 : start_sequence ();
1244 : :
1245 : 21163 : target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1246 : 21163 : if (target)
1247 : : {
1248 : 16545 : if (target != if_info->x)
1249 : 2950 : noce_emit_move_insn (if_info->x, target);
1250 : :
1251 : 16545 : seq = end_ifcvt_sequence (if_info);
1252 : 16545 : if (! seq)
1253 : : return false;
1254 : :
1255 : 16543 : emit_insn_before_setloc (seq, if_info->jump,
1256 : 16543 : INSN_LOCATION (if_info->insn_a));
1257 : 16543 : if_info->transform_name = "noce_try_store_flag";
1258 : 16543 : return true;
1259 : : }
1260 : : else
1261 : : {
1262 : 4618 : end_sequence ();
1263 : 4618 : return false;
1264 : : }
1265 : : }
1266 : :
1267 : :
1268 : : /* Convert "if (test) x = -A; else x = A" into
1269 : : x = A; if (test) x = -x if the machine can do the
1270 : : conditional negate form of this cheaply.
1271 : : Try this before noce_try_cmove that will just load the
1272 : : immediates into two registers and do a conditional select
1273 : : between them. If the target has a conditional negate or
1274 : : conditional invert operation we can save a potentially
1275 : : expensive constant synthesis. */
1276 : :
1277 : : static bool
1278 : 257592 : noce_try_inverse_constants (struct noce_if_info *if_info)
1279 : : {
1280 : 555934 : if (!noce_simple_bbs (if_info))
1281 : : return false;
1282 : :
1283 : 232427 : if (!CONST_INT_P (if_info->a)
1284 : 51219 : || !CONST_INT_P (if_info->b)
1285 : 20277 : || !REG_P (if_info->x))
1286 : : return false;
1287 : :
1288 : 20277 : machine_mode mode = GET_MODE (if_info->x);
1289 : :
1290 : 20277 : HOST_WIDE_INT val_a = INTVAL (if_info->a);
1291 : 20277 : HOST_WIDE_INT val_b = INTVAL (if_info->b);
1292 : :
1293 : 20277 : rtx cond = if_info->cond;
1294 : :
1295 : 20277 : rtx x = if_info->x;
1296 : 20277 : rtx target;
1297 : :
1298 : 20277 : start_sequence ();
1299 : :
1300 : 20277 : rtx_code code;
1301 : 20277 : if (val_b != HOST_WIDE_INT_MIN && val_a == -val_b)
1302 : : code = NEG;
1303 : 19290 : else if (val_a == ~val_b)
1304 : : code = NOT;
1305 : : else
1306 : : {
1307 : 19173 : end_sequence ();
1308 : 19173 : return false;
1309 : : }
1310 : :
1311 : 1104 : rtx tmp = gen_reg_rtx (mode);
1312 : 1104 : noce_emit_move_insn (tmp, if_info->a);
1313 : :
1314 : 1104 : target = emit_conditional_neg_or_complement (x, code, mode, cond, tmp, tmp);
1315 : :
1316 : 1104 : if (target)
1317 : : {
1318 : 0 : rtx_insn *seq = get_insns ();
1319 : :
1320 : 0 : if (!seq)
1321 : : {
1322 : 0 : end_sequence ();
1323 : 0 : return false;
1324 : : }
1325 : :
1326 : 0 : if (target != if_info->x)
1327 : 0 : noce_emit_move_insn (if_info->x, target);
1328 : :
1329 : 0 : seq = end_ifcvt_sequence (if_info);
1330 : :
1331 : 0 : if (!seq)
1332 : : return false;
1333 : :
1334 : 0 : emit_insn_before_setloc (seq, if_info->jump,
1335 : 0 : INSN_LOCATION (if_info->insn_a));
1336 : 0 : if_info->transform_name = "noce_try_inverse_constants";
1337 : 0 : return true;
1338 : : }
1339 : :
1340 : 1104 : end_sequence ();
1341 : 1104 : return false;
1342 : : }
1343 : :
1344 : :
1345 : : /* Convert "if (test) x = a; else x = b", for A and B constant.
1346 : : Also allow A = y + c1, B = y + c2, with a common y between A
1347 : : and B. */
1348 : :
1349 : : static bool
1350 : 257592 : noce_try_store_flag_constants (struct noce_if_info *if_info)
1351 : : {
1352 : 257592 : rtx target;
1353 : 257592 : rtx_insn *seq;
1354 : 257592 : bool reversep;
1355 : 257592 : HOST_WIDE_INT itrue, ifalse, diff, tmp;
1356 : 257592 : int normalize;
1357 : 257592 : bool can_reverse;
1358 : 257592 : machine_mode mode = GET_MODE (if_info->x);
1359 : 257592 : rtx common = NULL_RTX;
1360 : :
1361 : 257592 : rtx a = if_info->a;
1362 : 257592 : rtx b = if_info->b;
1363 : :
1364 : : /* Handle cases like x := test ? y + 3 : y + 4. */
1365 : 257592 : if (GET_CODE (a) == PLUS
1366 : 31936 : && GET_CODE (b) == PLUS
1367 : 1738 : && CONST_INT_P (XEXP (a, 1))
1368 : 725 : && CONST_INT_P (XEXP (b, 1))
1369 : 634 : && rtx_equal_p (XEXP (a, 0), XEXP (b, 0))
1370 : : /* Allow expressions that are not using the result or plain
1371 : : registers where we handle overlap below. */
1372 : 258127 : && (REG_P (XEXP (a, 0))
1373 : 10 : || (noce_operand_ok (XEXP (a, 0))
1374 : 10 : && ! reg_overlap_mentioned_p (if_info->x, XEXP (a, 0)))))
1375 : : {
1376 : 535 : common = XEXP (a, 0);
1377 : 535 : a = XEXP (a, 1);
1378 : 535 : b = XEXP (b, 1);
1379 : : }
1380 : :
1381 : 323507 : if (!noce_simple_bbs (if_info))
1382 : : return false;
1383 : :
1384 : 232427 : if (CONST_INT_P (a)
1385 : 51742 : && CONST_INT_P (b))
1386 : : {
1387 : 20800 : ifalse = INTVAL (a);
1388 : 20800 : itrue = INTVAL (b);
1389 : 20800 : bool subtract_flag_p = false;
1390 : :
1391 : 20800 : diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1392 : : /* Make sure we can represent the difference between the two values. */
1393 : 20800 : if ((diff > 0)
1394 : 20800 : != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1395 : : return false;
1396 : :
1397 : 20774 : diff = trunc_int_for_mode (diff, mode);
1398 : :
1399 : 20774 : can_reverse = noce_reversed_cond_code (if_info) != UNKNOWN;
1400 : 20774 : reversep = false;
1401 : 20774 : if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1402 : : {
1403 : 11367 : normalize = 0;
1404 : : /* We could collapse these cases but it is easier to follow the
1405 : : diff/STORE_FLAG_VALUE combinations when they are listed
1406 : : explicitly. */
1407 : :
1408 : : /* test ? 3 : 4
1409 : : => 4 + (test != 0). */
1410 : 11367 : if (diff < 0 && STORE_FLAG_VALUE < 0)
1411 : : reversep = false;
1412 : : /* test ? 4 : 3
1413 : : => can_reverse | 4 + (test == 0)
1414 : : !can_reverse | 3 - (test != 0). */
1415 : 11367 : else if (diff > 0 && STORE_FLAG_VALUE < 0)
1416 : : {
1417 : : reversep = can_reverse;
1418 : : subtract_flag_p = !can_reverse;
1419 : : /* If we need to subtract the flag and we have PLUS-immediate
1420 : : A and B then it is unlikely to be beneficial to play tricks
1421 : : here. */
1422 : : if (subtract_flag_p && common)
1423 : : return false;
1424 : : }
1425 : : /* test ? 3 : 4
1426 : : => can_reverse | 3 + (test == 0)
1427 : : !can_reverse | 4 - (test != 0). */
1428 : 11367 : else if (diff < 0 && STORE_FLAG_VALUE > 0)
1429 : : {
1430 : 5519 : reversep = can_reverse;
1431 : 5519 : subtract_flag_p = !can_reverse;
1432 : : /* If we need to subtract the flag and we have PLUS-immediate
1433 : : A and B then it is unlikely to be beneficial to play tricks
1434 : : here. */
1435 : 5519 : if (subtract_flag_p && common)
1436 : : return false;
1437 : : }
1438 : : /* test ? 4 : 3
1439 : : => 4 + (test != 0). */
1440 : : else if (diff > 0 && STORE_FLAG_VALUE > 0)
1441 : : reversep = false;
1442 : : else
1443 : : gcc_unreachable ();
1444 : : }
1445 : : /* Is this (cond) ? 2^n : 0? */
1446 : 2044 : else if (ifalse == 0 && pow2p_hwi (itrue)
1447 : 9407 : && STORE_FLAG_VALUE == 1)
1448 : : normalize = 1;
1449 : : /* Is this (cond) ? 0 : 2^n? */
1450 : 255507 : else if (itrue == 0 && pow2p_hwi (ifalse) && can_reverse
1451 : 9403 : && STORE_FLAG_VALUE == 1)
1452 : : {
1453 : : normalize = 1;
1454 : : reversep = true;
1455 : : }
1456 : : /* Is this (cond) ? -1 : x? */
1457 : : else if (itrue == -1
1458 : : && STORE_FLAG_VALUE == -1)
1459 : : normalize = -1;
1460 : : /* Is this (cond) ? x : -1? */
1461 : : else if (ifalse == -1 && can_reverse
1462 : : && STORE_FLAG_VALUE == -1)
1463 : : {
1464 : : normalize = -1;
1465 : : reversep = true;
1466 : : }
1467 : : else
1468 : : return false;
1469 : :
1470 : 5519 : if (reversep)
1471 : : {
1472 : 5520 : std::swap (itrue, ifalse);
1473 : 5520 : diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1474 : : }
1475 : :
1476 : 11373 : start_sequence ();
1477 : :
1478 : : /* If we have x := test ? x + 3 : x + 4 then move the original
1479 : : x out of the way while we store flags. */
1480 : 11373 : if (common && rtx_equal_p (common, if_info->x))
1481 : : {
1482 : 15 : common = gen_reg_rtx (mode);
1483 : 15 : noce_emit_move_insn (common, if_info->x);
1484 : : }
1485 : :
1486 : 11373 : target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1487 : 11373 : if (! target)
1488 : : {
1489 : 4618 : end_sequence ();
1490 : 4618 : return false;
1491 : : }
1492 : :
1493 : : /* if (test) x = 3; else x = 4;
1494 : : => x = 3 + (test == 0); */
1495 : 6755 : if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1496 : : {
1497 : : /* Add the common part now. This may allow combine to merge this
1498 : : with the store flag operation earlier into some sort of conditional
1499 : : increment/decrement if the target allows it. */
1500 : 6749 : if (common)
1501 : 89 : target = expand_simple_binop (mode, PLUS,
1502 : : target, common,
1503 : : target, 0, OPTAB_WIDEN);
1504 : :
1505 : : /* Always use ifalse here. It should have been swapped with itrue
1506 : : when appropriate when reversep is true. */
1507 : 13498 : target = expand_simple_binop (mode, subtract_flag_p ? MINUS : PLUS,
1508 : 6749 : gen_int_mode (ifalse, mode), target,
1509 : : if_info->x, 0, OPTAB_WIDEN);
1510 : : }
1511 : : /* Other cases are not beneficial when the original A and B are PLUS
1512 : : expressions. */
1513 : 6 : else if (common)
1514 : : {
1515 : 0 : end_sequence ();
1516 : 0 : return false;
1517 : : }
1518 : : /* if (test) x = 8; else x = 0;
1519 : : => x = (test != 0) << 3; */
1520 : 6 : else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1521 : : {
1522 : 6 : target = expand_simple_binop (mode, ASHIFT,
1523 : : target, GEN_INT (tmp), if_info->x, 0,
1524 : : OPTAB_WIDEN);
1525 : : }
1526 : :
1527 : : /* if (test) x = -1; else x = b;
1528 : : => x = -(test != 0) | b; */
1529 : 0 : else if (itrue == -1)
1530 : : {
1531 : 0 : target = expand_simple_binop (mode, IOR,
1532 : 0 : target, gen_int_mode (ifalse, mode),
1533 : : if_info->x, 0, OPTAB_WIDEN);
1534 : : }
1535 : : else
1536 : : {
1537 : 0 : end_sequence ();
1538 : 0 : return false;
1539 : : }
1540 : :
1541 : 6755 : if (! target)
1542 : : {
1543 : 0 : end_sequence ();
1544 : 0 : return false;
1545 : : }
1546 : :
1547 : 6755 : if (target != if_info->x)
1548 : 0 : noce_emit_move_insn (if_info->x, target);
1549 : :
1550 : 6755 : seq = end_ifcvt_sequence (if_info);
1551 : 6755 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1552 : 4371 : return false;
1553 : :
1554 : 2384 : emit_insn_before_setloc (seq, if_info->jump,
1555 : 2384 : INSN_LOCATION (if_info->insn_a));
1556 : 2384 : if_info->transform_name = "noce_try_store_flag_constants";
1557 : :
1558 : 2384 : return true;
1559 : : }
1560 : :
1561 : : return false;
1562 : : }
1563 : :
1564 : : /* Convert "if (test) foo++" into "foo += (test != 0)", and
1565 : : similarly for "foo--". */
1566 : :
1567 : : static bool
1568 : 209307 : noce_try_addcc (struct noce_if_info *if_info)
1569 : : {
1570 : 209307 : rtx target;
1571 : 209307 : rtx_insn *seq;
1572 : 209307 : bool subtract;
1573 : 209307 : int normalize;
1574 : :
1575 : 432409 : if (!noce_simple_bbs (if_info))
1576 : : return false;
1577 : :
1578 : 184142 : if (GET_CODE (if_info->a) == PLUS
1579 : 26861 : && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1580 : 206143 : && noce_reversed_cond_code (if_info) != UNKNOWN)
1581 : : {
1582 : 22001 : rtx cond = if_info->rev_cond;
1583 : 22001 : enum rtx_code code;
1584 : :
1585 : 22001 : if (cond == NULL_RTX)
1586 : : {
1587 : 0 : cond = if_info->cond;
1588 : 0 : code = reversed_comparison_code (cond, if_info->jump);
1589 : : }
1590 : : else
1591 : 22001 : code = GET_CODE (cond);
1592 : :
1593 : : /* First try to use addcc pattern. */
1594 : 22001 : if (general_operand (XEXP (cond, 0), VOIDmode)
1595 : 22001 : && general_operand (XEXP (cond, 1), VOIDmode))
1596 : : {
1597 : 20384 : start_sequence ();
1598 : 61152 : target = emit_conditional_add (if_info->x, code,
1599 : : XEXP (cond, 0),
1600 : : XEXP (cond, 1),
1601 : : VOIDmode,
1602 : : if_info->b,
1603 : 20384 : XEXP (if_info->a, 1),
1604 : 20384 : GET_MODE (if_info->x),
1605 : 20384 : (code == LTU || code == GEU
1606 : 20384 : || code == LEU || code == GTU));
1607 : 20384 : if (target)
1608 : : {
1609 : 1233 : if (target != if_info->x)
1610 : 0 : noce_emit_move_insn (if_info->x, target);
1611 : :
1612 : 1233 : seq = end_ifcvt_sequence (if_info);
1613 : 1233 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1614 : 64 : return false;
1615 : :
1616 : 1169 : emit_insn_before_setloc (seq, if_info->jump,
1617 : 1169 : INSN_LOCATION (if_info->insn_a));
1618 : 1169 : if_info->transform_name = "noce_try_addcc";
1619 : :
1620 : 1169 : return true;
1621 : : }
1622 : 19151 : end_sequence ();
1623 : : }
1624 : :
1625 : : /* If that fails, construct conditional increment or decrement using
1626 : : setcc. We're changing a branch and an increment to a comparison and
1627 : : an ADD/SUB. */
1628 : 20768 : if (XEXP (if_info->a, 1) == const1_rtx
1629 : 15097 : || XEXP (if_info->a, 1) == constm1_rtx)
1630 : : {
1631 : 8251 : start_sequence ();
1632 : 8251 : if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1633 : : subtract = false, normalize = 0;
1634 : 2580 : else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1635 : : subtract = true, normalize = 0;
1636 : : else
1637 : 0 : subtract = false, normalize = INTVAL (XEXP (if_info->a, 1));
1638 : :
1639 : :
1640 : 8251 : target = noce_emit_store_flag (if_info,
1641 : 8251 : gen_reg_rtx (GET_MODE (if_info->x)),
1642 : : true, normalize);
1643 : :
1644 : 8251 : if (target)
1645 : 12310 : target = expand_simple_binop (GET_MODE (if_info->x),
1646 : : subtract ? MINUS : PLUS,
1647 : : if_info->b, target, if_info->x,
1648 : : 0, OPTAB_WIDEN);
1649 : 7445 : if (target)
1650 : : {
1651 : 7445 : if (target != if_info->x)
1652 : 0 : noce_emit_move_insn (if_info->x, target);
1653 : :
1654 : 7445 : seq = end_ifcvt_sequence (if_info);
1655 : 7445 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1656 : 1286 : return false;
1657 : :
1658 : 6159 : emit_insn_before_setloc (seq, if_info->jump,
1659 : 6159 : INSN_LOCATION (if_info->insn_a));
1660 : 6159 : if_info->transform_name = "noce_try_addcc";
1661 : 6159 : return true;
1662 : : }
1663 : 806 : end_sequence ();
1664 : : }
1665 : : }
1666 : :
1667 : : return false;
1668 : : }
1669 : :
1670 : : /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1671 : :
1672 : : static bool
1673 : 201979 : noce_try_store_flag_mask (struct noce_if_info *if_info)
1674 : : {
1675 : 201979 : rtx target;
1676 : 201979 : rtx_insn *seq;
1677 : 201979 : bool reversep;
1678 : :
1679 : 424994 : if (!noce_simple_bbs (if_info))
1680 : : return false;
1681 : :
1682 : 176814 : reversep = false;
1683 : :
1684 : 176814 : if ((if_info->a == const0_rtx
1685 : 3003 : && (REG_P (if_info->b) || rtx_equal_p (if_info->b, if_info->x)))
1686 : 178259 : || ((reversep = (noce_reversed_cond_code (if_info) != UNKNOWN))
1687 : 175256 : && if_info->b == const0_rtx
1688 : 16311 : && (REG_P (if_info->a) || rtx_equal_p (if_info->a, if_info->x))))
1689 : : {
1690 : 1568 : start_sequence ();
1691 : 1568 : target = noce_emit_store_flag (if_info,
1692 : 1568 : gen_reg_rtx (GET_MODE (if_info->x)),
1693 : : reversep, -1);
1694 : 1568 : if (target)
1695 : 780 : target = expand_simple_binop (GET_MODE (if_info->x), AND,
1696 : : reversep ? if_info->a : if_info->b,
1697 : : target, if_info->x, 0,
1698 : : OPTAB_WIDEN);
1699 : :
1700 : 780 : if (target)
1701 : : {
1702 : 780 : if (target != if_info->x)
1703 : 0 : noce_emit_move_insn (if_info->x, target);
1704 : :
1705 : 780 : seq = end_ifcvt_sequence (if_info);
1706 : 780 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1707 : 778 : return false;
1708 : :
1709 : 2 : emit_insn_before_setloc (seq, if_info->jump,
1710 : 2 : INSN_LOCATION (if_info->insn_a));
1711 : 2 : if_info->transform_name = "noce_try_store_flag_mask";
1712 : :
1713 : 2 : return true;
1714 : : }
1715 : :
1716 : 788 : end_sequence ();
1717 : : }
1718 : :
1719 : : return false;
1720 : : }
1721 : :
1722 : : /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1723 : :
1724 : : static rtx
1725 : 572433 : noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1726 : : rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue, rtx cc_cmp,
1727 : : rtx rev_cc_cmp)
1728 : : {
1729 : 572433 : rtx target ATTRIBUTE_UNUSED;
1730 : 572433 : bool unsignedp ATTRIBUTE_UNUSED;
1731 : :
1732 : : /* If earliest == jump, try to build the cmove insn directly.
1733 : : This is helpful when combine has created some complex condition
1734 : : (like for alpha's cmovlbs) that we can't hope to regenerate
1735 : : through the normal interface. */
1736 : :
1737 : 572433 : if (if_info->cond_earliest == if_info->jump)
1738 : : {
1739 : 4906 : rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1740 : 4906 : rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1741 : : cond, vtrue, vfalse);
1742 : 4906 : rtx set = gen_rtx_SET (x, if_then_else);
1743 : :
1744 : 4906 : start_sequence ();
1745 : 4906 : rtx_insn *insn = emit_insn (set);
1746 : :
1747 : 4906 : if (recog_memoized (insn) >= 0)
1748 : : {
1749 : 1428 : rtx_insn *seq = end_sequence ();
1750 : 1428 : emit_insn (seq);
1751 : :
1752 : 1428 : return x;
1753 : : }
1754 : :
1755 : 3478 : end_sequence ();
1756 : : }
1757 : :
1758 : 1142010 : unsignedp = (code == LTU || code == GEU
1759 : 571005 : || code == LEU || code == GTU);
1760 : :
1761 : 571005 : if (cc_cmp != NULL_RTX && rev_cc_cmp != NULL_RTX)
1762 : 118102 : target = emit_conditional_move (x, cc_cmp, rev_cc_cmp,
1763 : 118102 : vtrue, vfalse, GET_MODE (x));
1764 : : else
1765 : : {
1766 : : /* Don't even try if the comparison operands are weird
1767 : : except that the target supports cbranchcc4. */
1768 : 452903 : if (! general_operand (cmp_a, GET_MODE (cmp_a))
1769 : 452903 : || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1770 : : {
1771 : 39758 : if (!have_cbranchcc4
1772 : 39758 : || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1773 : 2960 : || cmp_b != const0_rtx)
1774 : : return NULL_RTX;
1775 : : }
1776 : :
1777 : 416105 : target = emit_conditional_move (x, { code, cmp_a, cmp_b, VOIDmode },
1778 : 416105 : vtrue, vfalse, GET_MODE (x),
1779 : : unsignedp);
1780 : : }
1781 : :
1782 : 534207 : if (target)
1783 : : return target;
1784 : :
1785 : : /* We might be faced with a situation like:
1786 : :
1787 : : x = (reg:M TARGET)
1788 : : vtrue = (subreg:M (reg:N VTRUE) BYTE)
1789 : : vfalse = (subreg:M (reg:N VFALSE) BYTE)
1790 : :
1791 : : We can't do a conditional move in mode M, but it's possible that we
1792 : : could do a conditional move in mode N instead and take a subreg of
1793 : : the result.
1794 : :
1795 : : If we can't create new pseudos, though, don't bother. */
1796 : 42736 : if (reload_completed)
1797 : : return NULL_RTX;
1798 : :
1799 : 42736 : if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1800 : : {
1801 : 33 : rtx reg_vtrue = SUBREG_REG (vtrue);
1802 : 33 : rtx reg_vfalse = SUBREG_REG (vfalse);
1803 : 33 : poly_uint64 byte_vtrue = SUBREG_BYTE (vtrue);
1804 : 33 : poly_uint64 byte_vfalse = SUBREG_BYTE (vfalse);
1805 : 33 : rtx promoted_target;
1806 : :
1807 : 33 : if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1808 : 33 : || maybe_ne (byte_vtrue, byte_vfalse)
1809 : 33 : || (SUBREG_PROMOTED_VAR_P (vtrue)
1810 : 33 : != SUBREG_PROMOTED_VAR_P (vfalse))
1811 : 33 : || (SUBREG_PROMOTED_GET (vtrue)
1812 : 33 : != SUBREG_PROMOTED_GET (vfalse)))
1813 : : return NULL_RTX;
1814 : :
1815 : 33 : promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1816 : :
1817 : 66 : target = emit_conditional_move (promoted_target,
1818 : : { code, cmp_a, cmp_b, VOIDmode },
1819 : : reg_vtrue, reg_vfalse,
1820 : 33 : GET_MODE (reg_vtrue), unsignedp);
1821 : : /* Nope, couldn't do it in that mode either. */
1822 : 33 : if (!target)
1823 : : return NULL_RTX;
1824 : :
1825 : 7 : target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1826 : 7 : SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1827 : 7 : SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1828 : 7 : emit_move_insn (x, target);
1829 : 7 : return x;
1830 : : }
1831 : : else
1832 : : return NULL_RTX;
1833 : : }
1834 : :
1835 : : /* Emit a conditional zero, returning TARGET or NULL_RTX upon failure.
1836 : : IF_INFO describes the if-conversion scenario under consideration.
1837 : : CZERO_CODE selects the condition (EQ/NE).
1838 : : NON_ZERO_OP is the nonzero operand of the conditional move
1839 : : TARGET is the desired output register. */
1840 : :
1841 : : static rtx
1842 : 102 : noce_emit_czero (struct noce_if_info *if_info, enum rtx_code czero_code,
1843 : : rtx non_zero_op, rtx target)
1844 : : {
1845 : 102 : machine_mode mode = GET_MODE (target);
1846 : 102 : rtx cond_op0 = XEXP (if_info->cond, 0);
1847 : 102 : rtx czero_cond
1848 : 102 : = gen_rtx_fmt_ee (czero_code, GET_MODE (cond_op0), cond_op0, const0_rtx);
1849 : 102 : rtx if_then_else
1850 : 102 : = gen_rtx_IF_THEN_ELSE (mode, czero_cond, const0_rtx, non_zero_op);
1851 : 102 : rtx set = gen_rtx_SET (target, if_then_else);
1852 : :
1853 : 102 : rtx_insn *insn = make_insn_raw (set);
1854 : :
1855 : 102 : if (recog_memoized (insn) >= 0)
1856 : : {
1857 : 0 : add_insn (insn);
1858 : 0 : return target;
1859 : : }
1860 : :
1861 : : return NULL_RTX;
1862 : : }
1863 : :
1864 : : /* Try only simple constants and registers here. More complex cases
1865 : : are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1866 : : has had a go at it. */
1867 : :
1868 : : static bool
1869 : 255208 : noce_try_cmove (struct noce_if_info *if_info)
1870 : : {
1871 : 255208 : enum rtx_code code;
1872 : 255208 : rtx target;
1873 : 255208 : rtx_insn *seq;
1874 : :
1875 : 502899 : if (!noce_simple_bbs (if_info))
1876 : : return false;
1877 : :
1878 : 168215 : if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1879 : 265322 : && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1880 : : {
1881 : 91067 : start_sequence ();
1882 : :
1883 : 91067 : code = GET_CODE (if_info->cond);
1884 : 91067 : target = noce_emit_cmove (if_info, if_info->x, code,
1885 : : XEXP (if_info->cond, 0),
1886 : : XEXP (if_info->cond, 1),
1887 : : if_info->a, if_info->b);
1888 : :
1889 : 91067 : if (target)
1890 : : {
1891 : 63751 : if (target != if_info->x)
1892 : 0 : noce_emit_move_insn (if_info->x, target);
1893 : :
1894 : 63751 : seq = end_ifcvt_sequence (if_info);
1895 : 63751 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1896 : 17875 : return false;
1897 : :
1898 : 45876 : emit_insn_before_setloc (seq, if_info->jump,
1899 : 45876 : INSN_LOCATION (if_info->insn_a));
1900 : 45876 : if_info->transform_name = "noce_try_cmove";
1901 : :
1902 : 45876 : return true;
1903 : : }
1904 : : /* If both a and b are constants try a last-ditch transformation:
1905 : : if (test) x = a; else x = b;
1906 : : => x = (-(test != 0) & (b - a)) + a;
1907 : : Try this only if the target-specific expansion above has failed.
1908 : : The target-specific expander may want to generate sequences that
1909 : : we don't know about, so give them a chance before trying this
1910 : : approach. */
1911 : 27316 : else if (!targetm.have_conditional_execution ()
1912 : 27316 : && CONST_INT_P (if_info->a) && CONST_INT_P (if_info->b))
1913 : : {
1914 : 4930 : machine_mode mode = GET_MODE (if_info->x);
1915 : 4930 : HOST_WIDE_INT ifalse = INTVAL (if_info->a);
1916 : 4930 : HOST_WIDE_INT itrue = INTVAL (if_info->b);
1917 : 4930 : rtx target = noce_emit_store_flag (if_info, if_info->x, false, -1);
1918 : 4930 : if (!target)
1919 : : {
1920 : 4636 : end_sequence ();
1921 : 4636 : return false;
1922 : : }
1923 : :
1924 : 294 : HOST_WIDE_INT diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1925 : : /* Make sure we can represent the difference
1926 : : between the two values. */
1927 : 294 : if ((diff > 0)
1928 : 294 : != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1929 : : {
1930 : 17 : end_sequence ();
1931 : 17 : return false;
1932 : : }
1933 : :
1934 : 277 : diff = trunc_int_for_mode (diff, mode);
1935 : 277 : target = expand_simple_binop (mode, AND,
1936 : 277 : target, gen_int_mode (diff, mode),
1937 : : if_info->x, 0, OPTAB_WIDEN);
1938 : 277 : if (target)
1939 : 277 : target = expand_simple_binop (mode, PLUS,
1940 : 277 : target, gen_int_mode (ifalse, mode),
1941 : : if_info->x, 0, OPTAB_WIDEN);
1942 : 277 : if (target)
1943 : : {
1944 : 277 : if (target != if_info->x)
1945 : 0 : noce_emit_move_insn (if_info->x, target);
1946 : :
1947 : 277 : seq = end_ifcvt_sequence (if_info);
1948 : 277 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1949 : 252 : return false;
1950 : :
1951 : 25 : emit_insn_before_setloc (seq, if_info->jump,
1952 : 25 : INSN_LOCATION (if_info->insn_a));
1953 : 25 : if_info->transform_name = "noce_try_cmove";
1954 : 25 : return true;
1955 : : }
1956 : : else
1957 : : {
1958 : 0 : end_sequence ();
1959 : 0 : return false;
1960 : : }
1961 : : }
1962 : : else
1963 : 22386 : end_sequence ();
1964 : : }
1965 : :
1966 : : return false;
1967 : : }
1968 : :
1969 : : /* Return true if X contains a conditional code mode rtx. */
1970 : :
1971 : : static bool
1972 : 2991145 : contains_ccmode_rtx_p (rtx x)
1973 : : {
1974 : 2991145 : subrtx_iterator::array_type array;
1975 : 7555072 : FOR_EACH_SUBRTX (iter, array, x, ALL)
1976 : 4615119 : if (GET_MODE_CLASS (GET_MODE (*iter)) == MODE_CC)
1977 : 51192 : return true;
1978 : :
1979 : 2939953 : return false;
1980 : 2991145 : }
1981 : :
1982 : : /* Helper for bb_valid_for_noce_process_p. Validate that
1983 : : the rtx insn INSN is a single set that does not set
1984 : : the conditional register CC and is in general valid for
1985 : : if-conversion. */
1986 : :
1987 : : static bool
1988 : 3698166 : insn_valid_noce_process_p (rtx_insn *insn, rtx cc)
1989 : : {
1990 : 3698166 : if (!insn
1991 : 3697761 : || !NONJUMP_INSN_P (insn)
1992 : 6717269 : || (cc && set_of (cc, insn)))
1993 : 689443 : return false;
1994 : :
1995 : 3008723 : rtx sset = single_set (insn);
1996 : :
1997 : : /* Currently support only simple single sets in test_bb. */
1998 : 3008723 : if (!sset
1999 : 3004560 : || !noce_operand_ok (SET_DEST (sset))
2000 : 2991145 : || contains_ccmode_rtx_p (SET_DEST (sset))
2001 : 5948676 : || !noce_operand_ok (SET_SRC (sset)))
2002 : 134479 : return false;
2003 : :
2004 : : return true;
2005 : : }
2006 : :
2007 : :
2008 : : /* Return true iff the registers that the insns in BB_A set do not get
2009 : : used in BB_B. If TO_RENAME is non-NULL then it is a location that will be
2010 : : renamed later by the caller and so conflicts on it should be ignored
2011 : : in this function. */
2012 : :
2013 : : static bool
2014 : 71751 : bbs_ok_for_cmove_arith (basic_block bb_a, basic_block bb_b, rtx to_rename)
2015 : : {
2016 : 71751 : rtx_insn *a_insn;
2017 : 71751 : bitmap bba_sets = BITMAP_ALLOC (®_obstack);
2018 : :
2019 : 71751 : df_ref def;
2020 : 71751 : df_ref use;
2021 : :
2022 : 361060 : FOR_BB_INSNS (bb_a, a_insn)
2023 : : {
2024 : 289309 : if (!active_insn_p (a_insn))
2025 : 192635 : continue;
2026 : :
2027 : 96674 : rtx sset_a = single_set (a_insn);
2028 : :
2029 : 96674 : if (!sset_a)
2030 : : {
2031 : 0 : BITMAP_FREE (bba_sets);
2032 : 0 : return false;
2033 : : }
2034 : : /* Record all registers that BB_A sets. */
2035 : 234897 : FOR_EACH_INSN_DEF (def, a_insn)
2036 : 138223 : if (!(to_rename && DF_REF_REG (def) == to_rename))
2037 : 66854 : bitmap_set_bit (bba_sets, DF_REF_REGNO (def));
2038 : : }
2039 : :
2040 : 71751 : rtx_insn *b_insn;
2041 : :
2042 : 361030 : FOR_BB_INSNS (bb_b, b_insn)
2043 : : {
2044 : 289374 : if (!active_insn_p (b_insn))
2045 : 192703 : continue;
2046 : :
2047 : 96671 : rtx sset_b = single_set (b_insn);
2048 : :
2049 : 96671 : if (!sset_b)
2050 : : {
2051 : 0 : BITMAP_FREE (bba_sets);
2052 : 0 : return false;
2053 : : }
2054 : :
2055 : : /* Make sure this is a REG and not some instance
2056 : : of ZERO_EXTRACT or non-paradoxical SUBREG or other dangerous stuff.
2057 : : If we have a memory destination then we have a pair of simple
2058 : : basic blocks performing an operation of the form [addr] = c ? a : b.
2059 : : bb_valid_for_noce_process_p will have ensured that these are
2060 : : the only stores present. In that case [addr] should be the location
2061 : : to be renamed. Assert that the callers set this up properly. */
2062 : 96671 : if (MEM_P (SET_DEST (sset_b)))
2063 : 376 : gcc_assert (rtx_equal_p (SET_DEST (sset_b), to_rename));
2064 : 96295 : else if (!REG_P (SET_DEST (sset_b))
2065 : 96295 : && !paradoxical_subreg_p (SET_DEST (sset_b)))
2066 : : {
2067 : 34 : BITMAP_FREE (bba_sets);
2068 : 34 : return false;
2069 : : }
2070 : :
2071 : : /* If the insn uses a reg set in BB_A return false. */
2072 : 186206 : FOR_EACH_INSN_USE (use, b_insn)
2073 : : {
2074 : 89630 : if (bitmap_bit_p (bba_sets, DF_REF_REGNO (use)))
2075 : : {
2076 : 61 : BITMAP_FREE (bba_sets);
2077 : 61 : return false;
2078 : : }
2079 : : }
2080 : :
2081 : : }
2082 : :
2083 : 71656 : BITMAP_FREE (bba_sets);
2084 : 71656 : return true;
2085 : : }
2086 : :
2087 : : /* Emit copies of all the active instructions in BB except the last.
2088 : : This is a helper for noce_try_cmove_arith. */
2089 : :
2090 : : static void
2091 : 21440 : noce_emit_all_but_last (basic_block bb)
2092 : : {
2093 : 21440 : rtx_insn *last = last_active_insn (bb, false);
2094 : 21440 : rtx_insn *insn;
2095 : 188243 : FOR_BB_INSNS (bb, insn)
2096 : : {
2097 : 145363 : if (insn != last && active_insn_p (insn))
2098 : : {
2099 : 64493 : rtx_insn *to_emit = as_a <rtx_insn *> (copy_rtx (insn));
2100 : :
2101 : 64493 : emit_insn (PATTERN (to_emit));
2102 : : }
2103 : : }
2104 : 21440 : }
2105 : :
2106 : : /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
2107 : : the resulting insn or NULL if it's not a valid insn. */
2108 : :
2109 : : static rtx_insn *
2110 : 246533 : noce_emit_insn (rtx to_emit)
2111 : : {
2112 : 246533 : gcc_assert (to_emit);
2113 : 246533 : rtx_insn *insn = emit_insn (to_emit);
2114 : :
2115 : 246533 : if (recog_memoized (insn) < 0)
2116 : 212 : return NULL;
2117 : :
2118 : : return insn;
2119 : : }
2120 : :
2121 : : /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
2122 : : and including the penultimate one in BB if it is not simple
2123 : : (as indicated by SIMPLE). Then emit LAST_INSN as the last
2124 : : insn in the block. The reason for that is that LAST_INSN may
2125 : : have been modified by the preparation in noce_try_cmove_arith. */
2126 : :
2127 : : static bool
2128 : 249905 : noce_emit_bb (rtx last_insn, basic_block bb, bool simple)
2129 : : {
2130 : 249905 : if (bb && !simple)
2131 : 21440 : noce_emit_all_but_last (bb);
2132 : :
2133 : 249905 : if (last_insn && !noce_emit_insn (last_insn))
2134 : : return false;
2135 : :
2136 : : return true;
2137 : : }
2138 : :
2139 : : /* Try more complex cases involving conditional_move. */
2140 : :
2141 : : static bool
2142 : 201977 : noce_try_cmove_arith (struct noce_if_info *if_info)
2143 : : {
2144 : 201977 : rtx a = if_info->a;
2145 : 201977 : rtx b = if_info->b;
2146 : 201977 : rtx x = if_info->x;
2147 : 201977 : rtx orig_a, orig_b;
2148 : 201977 : rtx_insn *insn_a, *insn_b;
2149 : 201977 : bool a_simple = if_info->then_simple;
2150 : 201977 : bool b_simple = if_info->else_simple;
2151 : 201977 : basic_block then_bb = if_info->then_bb;
2152 : 201977 : basic_block else_bb = if_info->else_bb;
2153 : 201977 : rtx target;
2154 : 201977 : bool is_mem = false;
2155 : 201977 : enum rtx_code code;
2156 : 201977 : rtx cond = if_info->cond;
2157 : 201977 : rtx_insn *ifcvt_seq;
2158 : :
2159 : : /* A conditional move from two memory sources is equivalent to a
2160 : : conditional on their addresses followed by a load. Don't do this
2161 : : early because it'll screw alias analysis. Note that we've
2162 : : already checked for no side effects. */
2163 : 201977 : if (cse_not_expected
2164 : 68967 : && MEM_P (a) && MEM_P (b)
2165 : 206434 : && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b))
2166 : : {
2167 : 4457 : machine_mode address_mode = get_address_mode (a);
2168 : :
2169 : 4457 : a = XEXP (a, 0);
2170 : 4457 : b = XEXP (b, 0);
2171 : 4457 : x = gen_reg_rtx (address_mode);
2172 : 4457 : is_mem = true;
2173 : : }
2174 : :
2175 : : /* ??? We could handle this if we knew that a load from A or B could
2176 : : not trap or fault. This is also true if we've already loaded
2177 : : from the address along the path from ENTRY. */
2178 : 197520 : else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
2179 : 57868 : return false;
2180 : :
2181 : : /* if (test) x = a + b; else x = c - d;
2182 : : => y = a + b;
2183 : : x = c - d;
2184 : : if (test)
2185 : : x = y;
2186 : : */
2187 : :
2188 : 144109 : code = GET_CODE (cond);
2189 : 144109 : insn_a = if_info->insn_a;
2190 : 144109 : insn_b = if_info->insn_b;
2191 : :
2192 : 144109 : machine_mode x_mode = GET_MODE (x);
2193 : :
2194 : 144109 : if (!can_conditionally_move_p (x_mode))
2195 : : return false;
2196 : :
2197 : : /* Possibly rearrange operands to make things come out more natural. */
2198 : 125052 : if (noce_reversed_cond_code (if_info) != UNKNOWN)
2199 : : {
2200 : 125052 : bool reversep = false;
2201 : 125052 : if (rtx_equal_p (b, x))
2202 : : reversep = true;
2203 : 66258 : else if (general_operand (b, GET_MODE (b)))
2204 : : reversep = true;
2205 : :
2206 : : if (reversep)
2207 : : {
2208 : 115296 : if (if_info->rev_cond)
2209 : : {
2210 : 115296 : cond = if_info->rev_cond;
2211 : 115296 : code = GET_CODE (cond);
2212 : : }
2213 : : else
2214 : 0 : code = reversed_comparison_code (cond, if_info->jump);
2215 : : std::swap (a, b);
2216 : : std::swap (insn_a, insn_b);
2217 : : std::swap (a_simple, b_simple);
2218 : : std::swap (then_bb, else_bb);
2219 : : }
2220 : : }
2221 : :
2222 : 37470 : if (then_bb && else_bb
2223 : 160975 : && (!bbs_ok_for_cmove_arith (then_bb, else_bb, if_info->orig_x)
2224 : 35828 : || !bbs_ok_for_cmove_arith (else_bb, then_bb, if_info->orig_x)))
2225 : 95 : return false;
2226 : :
2227 : 124957 : start_sequence ();
2228 : :
2229 : : /* If one of the blocks is empty then the corresponding B or A value
2230 : : came from the test block. The non-empty complex block that we will
2231 : : emit might clobber the register used by B or A, so move it to a pseudo
2232 : : first. */
2233 : :
2234 : 124957 : rtx tmp_a = NULL_RTX;
2235 : 124957 : rtx tmp_b = NULL_RTX;
2236 : :
2237 : 124957 : if (b_simple || !else_bb)
2238 : 106539 : tmp_b = gen_reg_rtx (x_mode);
2239 : :
2240 : 124957 : if (a_simple || !then_bb)
2241 : 121935 : tmp_a = gen_reg_rtx (x_mode);
2242 : :
2243 : 124957 : orig_a = a;
2244 : 124957 : orig_b = b;
2245 : :
2246 : 124957 : rtx emit_a = NULL_RTX;
2247 : 124957 : rtx emit_b = NULL_RTX;
2248 : 124957 : rtx_insn *tmp_insn = NULL;
2249 : 124957 : bool modified_in_a = false;
2250 : 124957 : bool modified_in_b = false;
2251 : : /* If either operand is complex, load it into a register first.
2252 : : The best way to do this is to copy the original insn. In this
2253 : : way we preserve any clobbers etc that the insn may have had.
2254 : : This is of course not possible in the IS_MEM case. */
2255 : :
2256 : 124957 : if (! general_operand (a, GET_MODE (a)) || tmp_a)
2257 : : {
2258 : :
2259 : 123869 : if (is_mem)
2260 : : {
2261 : 4457 : rtx reg = gen_reg_rtx (GET_MODE (a));
2262 : 4457 : emit_a = gen_rtx_SET (reg, a);
2263 : : }
2264 : : else
2265 : : {
2266 : 119412 : if (insn_a)
2267 : : {
2268 : 60576 : a = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2269 : :
2270 : 60576 : rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
2271 : 60576 : rtx set = single_set (copy_of_a);
2272 : 60576 : SET_DEST (set) = a;
2273 : :
2274 : 60576 : emit_a = PATTERN (copy_of_a);
2275 : : }
2276 : : else
2277 : : {
2278 : 58836 : rtx tmp_reg = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2279 : 58836 : emit_a = gen_rtx_SET (tmp_reg, a);
2280 : 58836 : a = tmp_reg;
2281 : : }
2282 : : }
2283 : : }
2284 : :
2285 : 124957 : if (! general_operand (b, GET_MODE (b)) || tmp_b)
2286 : : {
2287 : 122673 : if (is_mem)
2288 : : {
2289 : 4457 : rtx reg = gen_reg_rtx (GET_MODE (b));
2290 : 4457 : emit_b = gen_rtx_SET (reg, b);
2291 : : }
2292 : : else
2293 : : {
2294 : 118216 : if (insn_b)
2295 : : {
2296 : 118029 : b = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2297 : 118029 : rtx_insn *copy_of_b = as_a <rtx_insn *> (copy_rtx (insn_b));
2298 : 118029 : rtx set = single_set (copy_of_b);
2299 : :
2300 : 118029 : SET_DEST (set) = b;
2301 : 118029 : emit_b = PATTERN (copy_of_b);
2302 : : }
2303 : : else
2304 : : {
2305 : 187 : rtx tmp_reg = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2306 : 187 : emit_b = gen_rtx_SET (tmp_reg, b);
2307 : 187 : b = tmp_reg;
2308 : : }
2309 : : }
2310 : : }
2311 : :
2312 : 124957 : modified_in_a = emit_a != NULL_RTX && modified_in_p (orig_b, emit_a);
2313 : 124957 : if (tmp_b && then_bb)
2314 : : {
2315 : 120093 : FOR_BB_INSNS (then_bb, tmp_insn)
2316 : : /* Don't check inside insn_a. We will have changed it to emit_a
2317 : : with a destination that doesn't conflict. */
2318 : 93052 : if (!(insn_a && tmp_insn == insn_a)
2319 : 159063 : && modified_in_p (orig_b, tmp_insn))
2320 : : {
2321 : : modified_in_a = true;
2322 : : break;
2323 : : }
2324 : :
2325 : : }
2326 : :
2327 : 124957 : modified_in_b = emit_b != NULL_RTX && modified_in_p (orig_a, emit_b);
2328 : 124957 : if (tmp_a && else_bb)
2329 : : {
2330 : 513368 : FOR_BB_INSNS (else_bb, tmp_insn)
2331 : : /* Don't check inside insn_b. We will have changed it to emit_b
2332 : : with a destination that doesn't conflict. */
2333 : 392465 : if (!(insn_b && tmp_insn == insn_b)
2334 : 664027 : && modified_in_p (orig_a, tmp_insn))
2335 : : {
2336 : : modified_in_b = true;
2337 : : break;
2338 : : }
2339 : : }
2340 : :
2341 : : /* If insn to set up A clobbers any registers B depends on, try to
2342 : : swap insn that sets up A with the one that sets up B. If even
2343 : : that doesn't help, punt. */
2344 : 124957 : if (modified_in_a && !modified_in_b)
2345 : : {
2346 : 0 : if (!noce_emit_bb (emit_b, else_bb, b_simple))
2347 : 0 : goto end_seq_and_fail;
2348 : :
2349 : 0 : if (!noce_emit_bb (emit_a, then_bb, a_simple))
2350 : 0 : goto end_seq_and_fail;
2351 : : }
2352 : 124957 : else if (!modified_in_a)
2353 : : {
2354 : 124957 : if (!noce_emit_bb (emit_a, then_bb, a_simple))
2355 : 9 : goto end_seq_and_fail;
2356 : :
2357 : 124948 : if (!noce_emit_bb (emit_b, else_bb, b_simple))
2358 : 203 : goto end_seq_and_fail;
2359 : : }
2360 : : else
2361 : 0 : goto end_seq_and_fail;
2362 : :
2363 : 124745 : target = noce_emit_cmove (if_info, x, code, XEXP (cond, 0), XEXP (cond, 1),
2364 : : a, b);
2365 : :
2366 : 124745 : if (! target)
2367 : 29884 : goto end_seq_and_fail;
2368 : :
2369 : : /* If we're handling a memory for above, emit the load now. */
2370 : 94861 : if (is_mem)
2371 : : {
2372 : 2330 : rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
2373 : :
2374 : : /* Copy over flags as appropriate. */
2375 : 2330 : if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
2376 : 0 : MEM_VOLATILE_P (mem) = 1;
2377 : 2330 : if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
2378 : 2298 : set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
2379 : 4660 : set_mem_align (mem,
2380 : 2330 : MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
2381 : :
2382 : 2330 : gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
2383 : 2330 : set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
2384 : :
2385 : 2330 : noce_emit_move_insn (if_info->x, mem);
2386 : : }
2387 : 92531 : else if (target != x)
2388 : 0 : noce_emit_move_insn (x, target);
2389 : :
2390 : 94861 : ifcvt_seq = end_ifcvt_sequence (if_info);
2391 : 94861 : if (!ifcvt_seq || !targetm.noce_conversion_profitable_p (ifcvt_seq, if_info))
2392 : 57156 : return false;
2393 : :
2394 : 37705 : emit_insn_before_setloc (ifcvt_seq, if_info->jump,
2395 : 37705 : INSN_LOCATION (if_info->insn_a));
2396 : 37705 : if_info->transform_name = "noce_try_cmove_arith";
2397 : 37705 : return true;
2398 : :
2399 : 30096 : end_seq_and_fail:
2400 : 30096 : end_sequence ();
2401 : 30096 : return false;
2402 : : }
2403 : :
2404 : : /* For most cases, the simplified condition we found is the best
2405 : : choice, but this is not the case for the min/max/abs transforms.
2406 : : For these we wish to know that it is A or B in the condition. */
2407 : :
2408 : : static rtx
2409 : 174796 : noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
2410 : : rtx_insn **earliest)
2411 : : {
2412 : 174796 : rtx cond, set;
2413 : 174796 : rtx_insn *insn;
2414 : 174796 : bool reverse;
2415 : :
2416 : : /* If target is already mentioned in the known condition, return it. */
2417 : 174796 : if (reg_mentioned_p (target, if_info->cond))
2418 : : {
2419 : 9113 : *earliest = if_info->cond_earliest;
2420 : 9113 : return if_info->cond;
2421 : : }
2422 : :
2423 : 165683 : set = pc_set (if_info->jump);
2424 : 165683 : cond = XEXP (SET_SRC (set), 0);
2425 : 165683 : reverse
2426 : 331366 : = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2427 : 165683 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
2428 : 165683 : if (if_info->then_else_reversed)
2429 : 25832 : reverse = !reverse;
2430 : :
2431 : : /* If we're looking for a constant, try to make the conditional
2432 : : have that constant in it. There are two reasons why it may
2433 : : not have the constant we want:
2434 : :
2435 : : 1. GCC may have needed to put the constant in a register, because
2436 : : the target can't compare directly against that constant. For
2437 : : this case, we look for a SET immediately before the comparison
2438 : : that puts a constant in that register.
2439 : :
2440 : : 2. GCC may have canonicalized the conditional, for example
2441 : : replacing "if x < 4" with "if x <= 3". We can undo that (or
2442 : : make equivalent types of changes) to get the constants we need
2443 : : if they're off by one in the right direction. */
2444 : :
2445 : 165683 : if (CONST_INT_P (target))
2446 : : {
2447 : 45294 : enum rtx_code code = GET_CODE (if_info->cond);
2448 : 45294 : rtx op_a = XEXP (if_info->cond, 0);
2449 : 45294 : rtx op_b = XEXP (if_info->cond, 1);
2450 : 45294 : rtx_insn *prev_insn;
2451 : :
2452 : : /* First, look to see if we put a constant in a register. */
2453 : 45294 : prev_insn = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2454 : 45294 : if (prev_insn
2455 : 45190 : && BLOCK_FOR_INSN (prev_insn)
2456 : 45190 : == BLOCK_FOR_INSN (if_info->cond_earliest)
2457 : 36540 : && INSN_P (prev_insn)
2458 : 77635 : && GET_CODE (PATTERN (prev_insn)) == SET)
2459 : : {
2460 : 24515 : rtx src = find_reg_equal_equiv_note (prev_insn);
2461 : 24515 : if (!src)
2462 : 24408 : src = SET_SRC (PATTERN (prev_insn));
2463 : 24515 : if (CONST_INT_P (src))
2464 : : {
2465 : 12688 : if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
2466 : : op_a = src;
2467 : 12439 : else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
2468 : 1071 : op_b = src;
2469 : :
2470 : 12688 : if (CONST_INT_P (op_a))
2471 : : {
2472 : 249 : std::swap (op_a, op_b);
2473 : 249 : code = swap_condition (code);
2474 : : }
2475 : : }
2476 : : }
2477 : :
2478 : : /* Now, look to see if we can get the right constant by
2479 : : adjusting the conditional. */
2480 : 45294 : if (CONST_INT_P (op_b))
2481 : : {
2482 : 23140 : HOST_WIDE_INT desired_val = INTVAL (target);
2483 : 23140 : HOST_WIDE_INT actual_val = INTVAL (op_b);
2484 : :
2485 : 23140 : switch (code)
2486 : : {
2487 : 832 : case LT:
2488 : 832 : if (desired_val != HOST_WIDE_INT_MAX
2489 : 814 : && actual_val == desired_val + 1)
2490 : : {
2491 : 156 : code = LE;
2492 : 156 : op_b = GEN_INT (desired_val);
2493 : : }
2494 : : break;
2495 : 3 : case LE:
2496 : 3 : if (desired_val != HOST_WIDE_INT_MIN
2497 : 3 : && actual_val == desired_val - 1)
2498 : : {
2499 : 0 : code = LT;
2500 : 0 : op_b = GEN_INT (desired_val);
2501 : : }
2502 : : break;
2503 : 1536 : case GT:
2504 : 1536 : if (desired_val != HOST_WIDE_INT_MIN
2505 : 1536 : && actual_val == desired_val - 1)
2506 : : {
2507 : 229 : code = GE;
2508 : 229 : op_b = GEN_INT (desired_val);
2509 : : }
2510 : : break;
2511 : 39 : case GE:
2512 : 39 : if (desired_val != HOST_WIDE_INT_MAX
2513 : 24 : && actual_val == desired_val + 1)
2514 : : {
2515 : 3 : code = GT;
2516 : 3 : op_b = GEN_INT (desired_val);
2517 : : }
2518 : : break;
2519 : : default:
2520 : : break;
2521 : : }
2522 : : }
2523 : :
2524 : : /* If we made any changes, generate a new conditional that is
2525 : : equivalent to what we started with, but has the right
2526 : : constants in it. */
2527 : 45294 : if (code != GET_CODE (if_info->cond)
2528 : 44657 : || op_a != XEXP (if_info->cond, 0)
2529 : 44657 : || op_b != XEXP (if_info->cond, 1))
2530 : : {
2531 : 1708 : cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
2532 : 1708 : *earliest = if_info->cond_earliest;
2533 : 1708 : return cond;
2534 : : }
2535 : : }
2536 : :
2537 : 163975 : cond = canonicalize_condition (if_info->jump, cond, reverse,
2538 : : earliest, target, have_cbranchcc4, true);
2539 : 163975 : if (! cond || ! reg_mentioned_p (target, cond))
2540 : 163975 : return NULL;
2541 : :
2542 : : /* We almost certainly searched back to a different place.
2543 : : Need to re-verify correct lifetimes. */
2544 : :
2545 : : /* X may not be mentioned in the range (cond_earliest, jump]. */
2546 : 0 : for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
2547 : 0 : if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
2548 : : return NULL;
2549 : :
2550 : : /* A and B may not be modified in the range [cond_earliest, jump). */
2551 : 0 : for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
2552 : 0 : if (INSN_P (insn)
2553 : 0 : && (modified_in_p (if_info->a, insn)
2554 : 0 : || modified_in_p (if_info->b, insn)))
2555 : 0 : return NULL;
2556 : :
2557 : : return cond;
2558 : : }
2559 : :
2560 : : /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
2561 : :
2562 : : static bool
2563 : 257672 : noce_try_minmax (struct noce_if_info *if_info)
2564 : : {
2565 : 257672 : rtx cond, target;
2566 : 257672 : rtx_insn *earliest, *seq;
2567 : 257672 : enum rtx_code code, op;
2568 : 257672 : bool unsignedp;
2569 : :
2570 : 556067 : if (!noce_simple_bbs (if_info))
2571 : : return false;
2572 : :
2573 : : /* ??? Reject modes with NaNs or signed zeros since we don't know how
2574 : : they will be resolved with an SMIN/SMAX. It wouldn't be too hard
2575 : : to get the target to tell us... */
2576 : 232507 : if (HONOR_SIGNED_ZEROS (if_info->x)
2577 : 232507 : || HONOR_NANS (if_info->x))
2578 : 58808 : return false;
2579 : :
2580 : 173699 : cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
2581 : 173699 : if (!cond)
2582 : : return false;
2583 : :
2584 : : /* Verify the condition is of the form we expect, and canonicalize
2585 : : the comparison code. */
2586 : 10791 : code = GET_CODE (cond);
2587 : 10791 : if (rtx_equal_p (XEXP (cond, 0), if_info->a))
2588 : : {
2589 : 2470 : if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
2590 : : return false;
2591 : : }
2592 : 8321 : else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
2593 : : {
2594 : 5841 : if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
2595 : : return false;
2596 : 39 : code = swap_condition (code);
2597 : : }
2598 : : else
2599 : : return false;
2600 : :
2601 : : /* Determine what sort of operation this is. Note that the code is for
2602 : : a taken branch, so the code->operation mapping appears backwards. */
2603 : 56 : switch (code)
2604 : : {
2605 : : case LT:
2606 : : case LE:
2607 : : case UNLT:
2608 : : case UNLE:
2609 : : op = SMAX;
2610 : : unsignedp = false;
2611 : : break;
2612 : 8 : case GT:
2613 : 8 : case GE:
2614 : 8 : case UNGT:
2615 : 8 : case UNGE:
2616 : 8 : op = SMIN;
2617 : 8 : unsignedp = false;
2618 : 8 : break;
2619 : 3 : case LTU:
2620 : 3 : case LEU:
2621 : 3 : op = UMAX;
2622 : 3 : unsignedp = true;
2623 : 3 : break;
2624 : 22 : case GTU:
2625 : 22 : case GEU:
2626 : 22 : op = UMIN;
2627 : 22 : unsignedp = true;
2628 : 22 : break;
2629 : : default:
2630 : : return false;
2631 : : }
2632 : :
2633 : 56 : start_sequence ();
2634 : :
2635 : 56 : target = expand_simple_binop (GET_MODE (if_info->x), op,
2636 : : if_info->a, if_info->b,
2637 : : if_info->x, unsignedp, OPTAB_WIDEN);
2638 : 56 : if (! target)
2639 : : {
2640 : 0 : end_sequence ();
2641 : 0 : return false;
2642 : : }
2643 : 56 : if (target != if_info->x)
2644 : 0 : noce_emit_move_insn (if_info->x, target);
2645 : :
2646 : 56 : seq = end_ifcvt_sequence (if_info);
2647 : 56 : if (!seq)
2648 : : return false;
2649 : :
2650 : 56 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2651 : 56 : if_info->cond = cond;
2652 : 56 : if_info->cond_earliest = earliest;
2653 : 56 : if_info->rev_cond = NULL_RTX;
2654 : 56 : if_info->transform_name = "noce_try_minmax";
2655 : :
2656 : 56 : return true;
2657 : : }
2658 : :
2659 : : /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2660 : : "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2661 : : etc. */
2662 : :
2663 : : static bool
2664 : 257616 : noce_try_abs (struct noce_if_info *if_info)
2665 : : {
2666 : 257616 : rtx cond, target, a, b, c;
2667 : 257616 : rtx_insn *earliest, *seq;
2668 : 257616 : bool negate;
2669 : 257616 : bool one_cmpl = false;
2670 : :
2671 : 555976 : if (!noce_simple_bbs (if_info))
2672 : : return false;
2673 : :
2674 : : /* Reject modes with signed zeros. */
2675 : 232451 : if (HONOR_SIGNED_ZEROS (if_info->x))
2676 : : return false;
2677 : :
2678 : : /* Recognize A and B as constituting an ABS or NABS. The canonical
2679 : : form is a branch around the negation, taken when the object is the
2680 : : first operand of a comparison against 0 that evaluates to true. */
2681 : 173652 : a = if_info->a;
2682 : 173652 : b = if_info->b;
2683 : 173652 : if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2684 : : negate = false;
2685 : 172573 : else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2686 : : {
2687 : : std::swap (a, b);
2688 : : negate = true;
2689 : : }
2690 : 172555 : else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2691 : : {
2692 : : negate = false;
2693 : : one_cmpl = true;
2694 : : }
2695 : 172555 : else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2696 : : {
2697 : : std::swap (a, b);
2698 : : negate = true;
2699 : : one_cmpl = true;
2700 : : }
2701 : : else
2702 : 172555 : return false;
2703 : :
2704 : 1097 : cond = noce_get_alt_condition (if_info, b, &earliest);
2705 : 1097 : if (!cond)
2706 : : return false;
2707 : :
2708 : : /* Verify the condition is of the form we expect. */
2709 : 30 : if (rtx_equal_p (XEXP (cond, 0), b))
2710 : 30 : c = XEXP (cond, 1);
2711 : 0 : else if (rtx_equal_p (XEXP (cond, 1), b))
2712 : : {
2713 : 0 : c = XEXP (cond, 0);
2714 : 0 : negate = !negate;
2715 : : }
2716 : : else
2717 : : return false;
2718 : :
2719 : : /* Verify that C is zero. Search one step backward for a
2720 : : REG_EQUAL note or a simple source if necessary. */
2721 : 30 : if (REG_P (c))
2722 : : {
2723 : 0 : rtx set;
2724 : 0 : rtx_insn *insn = prev_nonnote_nondebug_insn (earliest);
2725 : 0 : if (insn
2726 : 0 : && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2727 : 0 : && (set = single_set (insn))
2728 : 0 : && rtx_equal_p (SET_DEST (set), c))
2729 : : {
2730 : 0 : rtx note = find_reg_equal_equiv_note (insn);
2731 : 0 : if (note)
2732 : 0 : c = XEXP (note, 0);
2733 : : else
2734 : 0 : c = SET_SRC (set);
2735 : : }
2736 : : else
2737 : 0 : return false;
2738 : : }
2739 : 30 : if (MEM_P (c)
2740 : 0 : && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2741 : 30 : && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2742 : 0 : c = get_pool_constant (XEXP (c, 0));
2743 : :
2744 : : /* Work around funny ideas get_condition has wrt canonicalization.
2745 : : Note that these rtx constants are known to be CONST_INT, and
2746 : : therefore imply integer comparisons.
2747 : : The one_cmpl case is more complicated, as we want to handle
2748 : : only x < 0 ? ~x : x or x >= 0 ? x : ~x to one_cmpl_abs (x)
2749 : : and x < 0 ? x : ~x or x >= 0 ? ~x : x to ~one_cmpl_abs (x),
2750 : : but not other cases (x > -1 is equivalent of x >= 0). */
2751 : 30 : if (c == constm1_rtx && GET_CODE (cond) == GT)
2752 : : ;
2753 : 6 : else if (c == const1_rtx && GET_CODE (cond) == LT)
2754 : : {
2755 : 0 : if (one_cmpl)
2756 : : return false;
2757 : : }
2758 : 6 : else if (c == CONST0_RTX (GET_MODE (b)))
2759 : : {
2760 : 0 : if (one_cmpl
2761 : 0 : && GET_CODE (cond) != GE
2762 : 0 : && GET_CODE (cond) != LT)
2763 : : return false;
2764 : : }
2765 : : else
2766 : : return false;
2767 : :
2768 : : /* Determine what sort of operation this is. */
2769 : 24 : switch (GET_CODE (cond))
2770 : : {
2771 : 0 : case LT:
2772 : 0 : case LE:
2773 : 0 : case UNLT:
2774 : 0 : case UNLE:
2775 : 0 : negate = !negate;
2776 : 0 : break;
2777 : : case GT:
2778 : : case GE:
2779 : : case UNGT:
2780 : : case UNGE:
2781 : : break;
2782 : : default:
2783 : : return false;
2784 : : }
2785 : :
2786 : 24 : start_sequence ();
2787 : 24 : if (one_cmpl)
2788 : 0 : target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2789 : : if_info->x);
2790 : : else
2791 : 24 : target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2792 : :
2793 : : /* ??? It's a quandary whether cmove would be better here, especially
2794 : : for integers. Perhaps combine will clean things up. */
2795 : 24 : if (target && negate)
2796 : : {
2797 : 0 : if (one_cmpl)
2798 : 0 : target = expand_simple_unop (GET_MODE (target), NOT, target,
2799 : : if_info->x, 0);
2800 : : else
2801 : 0 : target = expand_simple_unop (GET_MODE (target), NEG, target,
2802 : : if_info->x, 0);
2803 : : }
2804 : :
2805 : 24 : if (! target)
2806 : : {
2807 : 0 : end_sequence ();
2808 : 0 : return false;
2809 : : }
2810 : :
2811 : 24 : if (target != if_info->x)
2812 : 0 : noce_emit_move_insn (if_info->x, target);
2813 : :
2814 : 24 : seq = end_ifcvt_sequence (if_info);
2815 : 24 : if (!seq)
2816 : : return false;
2817 : :
2818 : 24 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2819 : 24 : if_info->cond = cond;
2820 : 24 : if_info->cond_earliest = earliest;
2821 : 24 : if_info->rev_cond = NULL_RTX;
2822 : 24 : if_info->transform_name = "noce_try_abs";
2823 : :
2824 : 24 : return true;
2825 : : }
2826 : :
2827 : : /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2828 : :
2829 : : static bool
2830 : 164272 : noce_try_sign_mask (struct noce_if_info *if_info)
2831 : : {
2832 : 164272 : rtx cond, t, m, c;
2833 : 164272 : rtx_insn *seq;
2834 : 164272 : machine_mode mode;
2835 : 164272 : enum rtx_code code;
2836 : 164272 : bool t_unconditional;
2837 : :
2838 : 339383 : if (!noce_simple_bbs (if_info))
2839 : : return false;
2840 : :
2841 : 145566 : cond = if_info->cond;
2842 : 145566 : code = GET_CODE (cond);
2843 : 145566 : m = XEXP (cond, 0);
2844 : 145566 : c = XEXP (cond, 1);
2845 : :
2846 : 145566 : t = NULL_RTX;
2847 : 145566 : if (if_info->a == const0_rtx)
2848 : : {
2849 : 2858 : if ((code == LT && c == const0_rtx)
2850 : 2823 : || (code == LE && c == constm1_rtx))
2851 : 35 : t = if_info->b;
2852 : : }
2853 : 142708 : else if (if_info->b == const0_rtx)
2854 : : {
2855 : 13176 : if ((code == GE && c == const0_rtx)
2856 : 13176 : || (code == GT && c == constm1_rtx))
2857 : : t = if_info->a;
2858 : : }
2859 : :
2860 : 136 : if (! t || side_effects_p (t))
2861 : 145430 : return false;
2862 : :
2863 : : /* We currently don't handle different modes. */
2864 : 136 : mode = GET_MODE (t);
2865 : 136 : if (GET_MODE (m) != mode)
2866 : : return false;
2867 : :
2868 : : /* This is only profitable if T is unconditionally executed/evaluated in the
2869 : : original insn sequence or T is cheap and can't trap or fault. The former
2870 : : happens if B is the non-zero (T) value and if INSN_B was taken from
2871 : : TEST_BB, or there was no INSN_B which can happen for e.g. conditional
2872 : : stores to memory. For the cost computation use the block TEST_BB where
2873 : : the evaluation will end up after the transformation. */
2874 : 64 : t_unconditional
2875 : 149 : = (t == if_info->b
2876 : 64 : && (if_info->insn_b == NULL_RTX
2877 : 0 : || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2878 : 128 : if (!(t_unconditional
2879 : 64 : || ((set_src_cost (t, mode, if_info->speed_p)
2880 : : < COSTS_N_INSNS (2))
2881 : 58 : && !may_trap_or_fault_p (t))))
2882 : 43 : return false;
2883 : :
2884 : 21 : if (!noce_can_force_operand (t))
2885 : : return false;
2886 : :
2887 : 0 : start_sequence ();
2888 : : /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2889 : : "(signed) m >> 31" directly. This benefits targets with specialized
2890 : : insns to obtain the signmask, but still uses ashr_optab otherwise. */
2891 : 0 : m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2892 : 0 : t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2893 : : : NULL_RTX;
2894 : :
2895 : 0 : if (!t)
2896 : : {
2897 : 0 : end_sequence ();
2898 : 0 : return false;
2899 : : }
2900 : :
2901 : 0 : noce_emit_move_insn (if_info->x, t);
2902 : :
2903 : 0 : seq = end_ifcvt_sequence (if_info);
2904 : 0 : if (!seq)
2905 : : return false;
2906 : :
2907 : 0 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2908 : 0 : if_info->transform_name = "noce_try_sign_mask";
2909 : :
2910 : 0 : return true;
2911 : : }
2912 : :
2913 : : /* Check if OP is supported by conditional zero based if conversion,
2914 : : returning TRUE if satisfied otherwise FALSE.
2915 : :
2916 : : OP is the operation to check. */
2917 : :
2918 : : static bool
2919 : 91473 : noce_cond_zero_binary_op_supported (rtx op)
2920 : : {
2921 : 91473 : enum rtx_code opcode = GET_CODE (op);
2922 : :
2923 : 0 : if (opcode == PLUS || opcode == MINUS || opcode == IOR || opcode == XOR
2924 : : || opcode == ASHIFT || opcode == ASHIFTRT || opcode == LSHIFTRT
2925 : : || opcode == ROTATE || opcode == ROTATERT || opcode == AND)
2926 : 0 : return true;
2927 : :
2928 : : return false;
2929 : : }
2930 : :
2931 : : /* Helper function to return REG itself,
2932 : : otherwise NULL_RTX for other RTX_CODE. */
2933 : :
2934 : : static rtx
2935 : 4152 : get_base_reg (rtx exp)
2936 : : {
2937 : 0 : if (REG_P (exp))
2938 : 0 : return exp;
2939 : :
2940 : : return NULL_RTX;
2941 : : }
2942 : :
2943 : : /* Check if IF-BB and THEN-BB satisfy the condition for conditional zero
2944 : : based if conversion, returning TRUE if satisfied otherwise FALSE.
2945 : :
2946 : : IF_INFO describes the if-conversion scenario under consideration.
2947 : : COMMON_PTR points to the common REG of canonicalized IF_INFO->A and
2948 : : IF_INFO->B.
2949 : : CZERO_CODE_PTR points to the comparison code to use in czero RTX.
2950 : : A_PTR points to the A expression of canonicalized IF_INFO->A.
2951 : : TO_REPLACE points to the RTX to be replaced by czero RTX destnation. */
2952 : :
2953 : : static bool
2954 : 201977 : noce_bbs_ok_for_cond_zero_arith (struct noce_if_info *if_info, rtx *common_ptr,
2955 : : rtx *bin_exp_ptr,
2956 : : enum rtx_code *czero_code_ptr, rtx *a_ptr,
2957 : : rtx **to_replace)
2958 : : {
2959 : 201977 : rtx common = NULL_RTX;
2960 : 201977 : rtx cond = if_info->cond;
2961 : 201977 : rtx a = copy_rtx (if_info->a);
2962 : 201977 : rtx b = copy_rtx (if_info->b);
2963 : 201977 : rtx bin_op1 = NULL_RTX;
2964 : 201977 : enum rtx_code czero_code = UNKNOWN;
2965 : 201977 : bool reverse = false;
2966 : 201977 : rtx op0, op1, bin_exp;
2967 : :
2968 : 248180 : if (!noce_simple_bbs (if_info))
2969 : : return false;
2970 : :
2971 : : /* COND must be EQ or NE comparision of a reg and 0. */
2972 : 176812 : if (GET_CODE (cond) != NE && GET_CODE (cond) != EQ)
2973 : : return false;
2974 : 144873 : if (!REG_P (XEXP (cond, 0)) || !rtx_equal_p (XEXP (cond, 1), const0_rtx))
2975 : 64477 : return false;
2976 : :
2977 : : /* Canonicalize x = y : (y op z) to x = (y op z) : y. */
2978 : 80396 : if (REG_P (a) && noce_cond_zero_binary_op_supported (b))
2979 : : {
2980 : : std::swap (a, b);
2981 : : reverse = !reverse;
2982 : : }
2983 : :
2984 : : /* Check if x = (y op z) : y is supported by czero based ifcvt. */
2985 : 203951 : if (!(noce_cond_zero_binary_op_supported (a) && REG_P (b)))
2986 : : return false;
2987 : :
2988 : 2076 : bin_exp = a;
2989 : :
2990 : : /* Canonicalize x = (z op y) : y to x = (y op z) : y */
2991 : 2076 : op1 = get_base_reg (XEXP (bin_exp, 1));
2992 : 160 : if (op1 && rtx_equal_p (op1, b) && COMMUTATIVE_ARITH_P (bin_exp))
2993 : 1 : std::swap (XEXP (bin_exp, 0), XEXP (bin_exp, 1));
2994 : :
2995 : 2076 : op0 = get_base_reg (XEXP (bin_exp, 0));
2996 : 1997 : if (op0 && rtx_equal_p (op0, b))
2997 : : {
2998 : 1462 : common = b;
2999 : 1462 : bin_op1 = XEXP (bin_exp, 1);
3000 : 2892 : czero_code = (reverse ^ (GET_CODE (bin_exp) == AND))
3001 : 1462 : ? noce_reversed_cond_code (if_info)
3002 : 1430 : : GET_CODE (cond);
3003 : : }
3004 : : else
3005 : 614 : return false;
3006 : :
3007 : 1462 : if (czero_code == UNKNOWN)
3008 : : return false;
3009 : :
3010 : 1462 : if (REG_P (bin_op1))
3011 : 102 : *to_replace = &XEXP (bin_exp, 1);
3012 : : else
3013 : : return false;
3014 : :
3015 : 102 : *common_ptr = common;
3016 : 102 : *bin_exp_ptr = bin_exp;
3017 : 102 : *czero_code_ptr = czero_code;
3018 : 102 : *a_ptr = a;
3019 : :
3020 : 102 : return true;
3021 : : }
3022 : :
3023 : : /* Try to covert if-then-else with conditional zero,
3024 : : returning TURE on success or FALSE on failure.
3025 : : IF_INFO describes the if-conversion scenario under consideration. */
3026 : :
3027 : : static int
3028 : 201977 : noce_try_cond_zero_arith (struct noce_if_info *if_info)
3029 : : {
3030 : 201977 : rtx target, rtmp, a;
3031 : 201977 : rtx_insn *seq;
3032 : 201977 : machine_mode mode = GET_MODE (if_info->x);
3033 : 201977 : rtx common = NULL_RTX;
3034 : 201977 : enum rtx_code czero_code = UNKNOWN;
3035 : 201977 : rtx bin_exp = NULL_RTX;
3036 : 201977 : enum rtx_code bin_code = UNKNOWN;
3037 : 201977 : rtx non_zero_op = NULL_RTX;
3038 : 201977 : rtx *to_replace = NULL;
3039 : :
3040 : 201977 : if (!noce_bbs_ok_for_cond_zero_arith (if_info, &common, &bin_exp, &czero_code,
3041 : : &a, &to_replace))
3042 : : return false;
3043 : :
3044 : 102 : start_sequence ();
3045 : :
3046 : 102 : bin_code = GET_CODE (bin_exp);
3047 : :
3048 : 102 : if (bin_code == AND)
3049 : : {
3050 : 2 : rtmp = gen_reg_rtx (mode);
3051 : 2 : noce_emit_move_insn (rtmp, a);
3052 : :
3053 : 2 : target = noce_emit_czero (if_info, czero_code, common, if_info->x);
3054 : 2 : if (!target)
3055 : : {
3056 : 2 : end_sequence ();
3057 : 2 : return false;
3058 : : }
3059 : :
3060 : 0 : target = expand_simple_binop (mode, IOR, rtmp, target, if_info->x, 0,
3061 : : OPTAB_WIDEN);
3062 : 0 : if (!target)
3063 : : {
3064 : 0 : end_sequence ();
3065 : 0 : return false;
3066 : : }
3067 : :
3068 : 0 : if (target != if_info->x)
3069 : 0 : noce_emit_move_insn (if_info->x, target);
3070 : : }
3071 : : else
3072 : : {
3073 : 100 : non_zero_op = *to_replace;
3074 : : /* If x is used in both input and out like x = c ? x + z : x,
3075 : : use a new reg to avoid modifying x */
3076 : 100 : if (common && rtx_equal_p (common, if_info->x))
3077 : 68 : target = gen_reg_rtx (mode);
3078 : : else
3079 : 32 : target = if_info->x;
3080 : :
3081 : 100 : target = noce_emit_czero (if_info, czero_code, non_zero_op, target);
3082 : 100 : if (!target || !to_replace)
3083 : : {
3084 : 100 : end_sequence ();
3085 : 100 : return false;
3086 : : }
3087 : :
3088 : 0 : *to_replace = target;
3089 : 0 : noce_emit_move_insn (if_info->x, a);
3090 : : }
3091 : :
3092 : 0 : seq = end_ifcvt_sequence (if_info);
3093 : 0 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
3094 : 0 : return false;
3095 : :
3096 : 0 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
3097 : 0 : if_info->transform_name = "noce_try_cond_zero_arith";
3098 : 0 : return true;
3099 : : }
3100 : :
3101 : : /* Optimize away "if (x & C) x |= C" and similar bit manipulation
3102 : : transformations. */
3103 : :
3104 : : static bool
3105 : 257672 : noce_try_bitop (struct noce_if_info *if_info)
3106 : : {
3107 : 257672 : rtx cond, x, a, result;
3108 : 257672 : rtx_insn *seq;
3109 : 257672 : scalar_int_mode mode;
3110 : 257672 : enum rtx_code code;
3111 : 257672 : int bitnum;
3112 : :
3113 : 257672 : x = if_info->x;
3114 : 257672 : cond = if_info->cond;
3115 : 257672 : code = GET_CODE (cond);
3116 : :
3117 : : /* Check for an integer operation. */
3118 : 257672 : if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
3119 : : return false;
3120 : :
3121 : 490761 : if (!noce_simple_bbs (if_info))
3122 : : return false;
3123 : :
3124 : : /* Check for no else condition. */
3125 : 172357 : if (! rtx_equal_p (x, if_info->b))
3126 : : return false;
3127 : :
3128 : : /* Check for a suitable condition. */
3129 : 94003 : if (code != NE && code != EQ)
3130 : : return false;
3131 : 65108 : if (XEXP (cond, 1) != const0_rtx)
3132 : : return false;
3133 : 44425 : cond = XEXP (cond, 0);
3134 : :
3135 : : /* ??? We could also handle AND here. */
3136 : 44425 : if (GET_CODE (cond) == ZERO_EXTRACT)
3137 : : {
3138 : 448 : if (XEXP (cond, 1) != const1_rtx
3139 : 189 : || !CONST_INT_P (XEXP (cond, 2))
3140 : 637 : || ! rtx_equal_p (x, XEXP (cond, 0)))
3141 : 445 : return false;
3142 : 3 : bitnum = INTVAL (XEXP (cond, 2));
3143 : 3 : if (BITS_BIG_ENDIAN)
3144 : : bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
3145 : 3 : if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
3146 : : return false;
3147 : : }
3148 : : else
3149 : : return false;
3150 : :
3151 : 3 : a = if_info->a;
3152 : 3 : if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
3153 : : {
3154 : : /* Check for "if (X & C) x = x op C". */
3155 : 0 : if (! rtx_equal_p (x, XEXP (a, 0))
3156 : 0 : || !CONST_INT_P (XEXP (a, 1))
3157 : 0 : || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3158 : 0 : != HOST_WIDE_INT_1U << bitnum)
3159 : : return false;
3160 : :
3161 : : /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
3162 : : /* if ((x & C) != 0) x |= C; is transformed to nothing. */
3163 : 0 : if (GET_CODE (a) == IOR)
3164 : 0 : result = (code == NE) ? a : NULL_RTX;
3165 : 0 : else if (code == NE)
3166 : : {
3167 : : /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
3168 : 0 : result = gen_int_mode (HOST_WIDE_INT_1 << bitnum, mode);
3169 : 0 : result = simplify_gen_binary (IOR, mode, x, result);
3170 : : }
3171 : : else
3172 : : {
3173 : : /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
3174 : 0 : result = gen_int_mode (~(HOST_WIDE_INT_1 << bitnum), mode);
3175 : 0 : result = simplify_gen_binary (AND, mode, x, result);
3176 : : }
3177 : : }
3178 : 3 : else if (GET_CODE (a) == AND)
3179 : : {
3180 : : /* Check for "if (X & C) x &= ~C". */
3181 : 0 : if (! rtx_equal_p (x, XEXP (a, 0))
3182 : 0 : || !CONST_INT_P (XEXP (a, 1))
3183 : 0 : || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3184 : 0 : != (~(HOST_WIDE_INT_1 << bitnum) & GET_MODE_MASK (mode)))
3185 : : return false;
3186 : :
3187 : : /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
3188 : : /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
3189 : 0 : result = (code == EQ) ? a : NULL_RTX;
3190 : : }
3191 : : else
3192 : : return false;
3193 : :
3194 : 0 : if (result)
3195 : : {
3196 : 0 : start_sequence ();
3197 : 0 : noce_emit_move_insn (x, result);
3198 : 0 : seq = end_ifcvt_sequence (if_info);
3199 : 0 : if (!seq)
3200 : : return false;
3201 : :
3202 : 0 : emit_insn_before_setloc (seq, if_info->jump,
3203 : 0 : INSN_LOCATION (if_info->insn_a));
3204 : : }
3205 : 0 : if_info->transform_name = "noce_try_bitop";
3206 : 0 : return true;
3207 : : }
3208 : :
3209 : :
3210 : : /* Similar to get_condition, only the resulting condition must be
3211 : : valid at JUMP, instead of at EARLIEST.
3212 : :
3213 : : If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
3214 : : THEN block of the caller, and we have to reverse the condition. */
3215 : :
3216 : : static rtx
3217 : 4383331 : noce_get_condition (rtx_insn *jump, rtx_insn **earliest,
3218 : : bool then_else_reversed)
3219 : : {
3220 : 4383331 : rtx cond, set, tmp;
3221 : 4383331 : bool reverse;
3222 : :
3223 : 4383331 : if (! any_condjump_p (jump))
3224 : : return NULL_RTX;
3225 : :
3226 : 4383331 : set = pc_set (jump);
3227 : :
3228 : : /* If this branches to JUMP_LABEL when the condition is false,
3229 : : reverse the condition. */
3230 : 8766662 : reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
3231 : 4383331 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
3232 : :
3233 : : /* We may have to reverse because the caller's if block is not canonical,
3234 : : i.e. the THEN block isn't the fallthrough block for the TEST block
3235 : : (see find_if_header). */
3236 : 4383331 : if (then_else_reversed)
3237 : 1894290 : reverse = !reverse;
3238 : :
3239 : : /* If the condition variable is a register and is MODE_INT, accept it. */
3240 : :
3241 : 4383331 : cond = XEXP (SET_SRC (set), 0);
3242 : 4383331 : tmp = XEXP (cond, 0);
3243 : 4381601 : if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
3244 : 4383373 : && (GET_MODE (tmp) != BImode
3245 : 0 : || !targetm.small_register_classes_for_mode_p (BImode)))
3246 : : {
3247 : 42 : *earliest = jump;
3248 : :
3249 : 42 : if (reverse)
3250 : 18 : cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
3251 : : GET_MODE (cond), tmp, XEXP (cond, 1));
3252 : 42 : return cond;
3253 : : }
3254 : :
3255 : : /* Otherwise, fall back on canonicalize_condition to do the dirty
3256 : : work of manipulating MODE_CC values and COMPARE rtx codes. */
3257 : 4383289 : tmp = canonicalize_condition (jump, cond, reverse, earliest,
3258 : : NULL_RTX, have_cbranchcc4, true);
3259 : :
3260 : : /* We don't handle side-effects in the condition, like handling
3261 : : REG_INC notes and making sure no duplicate conditions are emitted. */
3262 : 4383289 : if (tmp != NULL_RTX && side_effects_p (tmp))
3263 : : return NULL_RTX;
3264 : :
3265 : : return tmp;
3266 : : }
3267 : :
3268 : : /* Return true if OP is ok for if-then-else processing. */
3269 : :
3270 : : static bool
3271 : 9320371 : noce_operand_ok (const_rtx op)
3272 : : {
3273 : 9320371 : if (side_effects_p (op))
3274 : : return false;
3275 : :
3276 : : /* We special-case memories, so handle any of them with
3277 : : no address side effects. */
3278 : 9298497 : if (MEM_P (op))
3279 : 1611777 : return ! side_effects_p (XEXP (op, 0));
3280 : :
3281 : 7686720 : return ! may_trap_p (op);
3282 : : }
3283 : :
3284 : : /* Return true iff basic block TEST_BB is valid for noce if-conversion.
3285 : : The condition used in this if-conversion is in COND.
3286 : : In practice, check that TEST_BB ends with a single set
3287 : : x := a and all previous computations
3288 : : in TEST_BB don't produce any values that are live after TEST_BB.
3289 : : In other words, all the insns in TEST_BB are there only
3290 : : to compute a value for x. Add the rtx cost of the insns
3291 : : in TEST_BB to COST. Record whether TEST_BB is a single simple
3292 : : set instruction in SIMPLE_P. */
3293 : :
3294 : : static bool
3295 : 2158468 : bb_valid_for_noce_process_p (basic_block test_bb, rtx cond,
3296 : : unsigned int *cost, bool *simple_p)
3297 : : {
3298 : 2158468 : if (!test_bb)
3299 : : return false;
3300 : :
3301 : 2158468 : rtx_insn *last_insn = last_active_insn (test_bb, false);
3302 : 2158468 : rtx last_set = NULL_RTX;
3303 : :
3304 : 2158468 : rtx cc = cc_in_cond (cond);
3305 : :
3306 : 2158468 : if (!insn_valid_noce_process_p (last_insn, cc))
3307 : : return false;
3308 : :
3309 : : /* Punt on blocks ending with asm goto or jumps with other side-effects,
3310 : : last_active_insn ignores JUMP_INSNs. */
3311 : 1554768 : if (JUMP_P (BB_END (test_bb)) && !onlyjump_p (BB_END (test_bb)))
3312 : : return false;
3313 : :
3314 : 1554768 : last_set = single_set (last_insn);
3315 : :
3316 : 1554768 : rtx x = SET_DEST (last_set);
3317 : 1554768 : rtx_insn *first_insn = first_active_insn (test_bb);
3318 : 1554768 : rtx first_set = single_set (first_insn);
3319 : :
3320 : 1554768 : if (!first_set)
3321 : : return false;
3322 : :
3323 : : /* We have a single simple set, that's okay. */
3324 : 1542194 : bool speed_p = optimize_bb_for_speed_p (test_bb);
3325 : :
3326 : 1542194 : if (first_insn == last_insn)
3327 : : {
3328 : 547655 : *simple_p = noce_operand_ok (SET_DEST (first_set));
3329 : 547655 : *cost += pattern_cost (first_set, speed_p);
3330 : 547655 : return *simple_p;
3331 : : }
3332 : :
3333 : 994539 : rtx_insn *prev_last_insn = PREV_INSN (last_insn);
3334 : 994539 : gcc_assert (prev_last_insn);
3335 : :
3336 : : /* For now, disallow setting x multiple times in test_bb. */
3337 : 994539 : if (REG_P (x) && reg_set_between_p (x, first_insn, prev_last_insn))
3338 : : return false;
3339 : :
3340 : 842386 : bitmap test_bb_temps = BITMAP_ALLOC (®_obstack);
3341 : :
3342 : : /* The regs that are live out of test_bb. */
3343 : 842386 : bitmap test_bb_live_out = df_get_live_out (test_bb);
3344 : :
3345 : 842386 : int potential_cost = pattern_cost (last_set, speed_p);
3346 : 842386 : rtx_insn *insn;
3347 : 4360828 : FOR_BB_INSNS (test_bb, insn)
3348 : : {
3349 : 4248959 : if (insn != last_insn)
3350 : : {
3351 : 4137090 : if (!active_insn_p (insn))
3352 : 2597392 : continue;
3353 : :
3354 : 1539698 : if (!insn_valid_noce_process_p (insn, cc))
3355 : 220222 : goto free_bitmap_and_fail;
3356 : :
3357 : 1319476 : rtx sset = single_set (insn);
3358 : 1319476 : gcc_assert (sset);
3359 : 1319476 : rtx dest = SET_DEST (sset);
3360 : 1319476 : if (SUBREG_P (dest))
3361 : 17682 : dest = SUBREG_REG (dest);
3362 : :
3363 : 1319476 : if (contains_mem_rtx_p (SET_SRC (sset))
3364 : 953231 : || !REG_P (dest)
3365 : 2144868 : || reg_overlap_mentioned_p (dest, cond))
3366 : 510295 : goto free_bitmap_and_fail;
3367 : :
3368 : 809181 : potential_cost += pattern_cost (sset, speed_p);
3369 : 809181 : bitmap_set_bit (test_bb_temps, REGNO (dest));
3370 : : }
3371 : : }
3372 : :
3373 : : /* If any of the intermediate results in test_bb are live after test_bb
3374 : : then fail. */
3375 : 111869 : if (bitmap_intersect_p (test_bb_live_out, test_bb_temps))
3376 : 71325 : goto free_bitmap_and_fail;
3377 : :
3378 : 40544 : BITMAP_FREE (test_bb_temps);
3379 : 40544 : *cost += potential_cost;
3380 : 40544 : *simple_p = false;
3381 : 40544 : return true;
3382 : :
3383 : 801842 : free_bitmap_and_fail:
3384 : 801842 : BITMAP_FREE (test_bb_temps);
3385 : 801842 : return false;
3386 : : }
3387 : :
3388 : : /* Helper function to emit a cmov sequence encapsulated in
3389 : : start_sequence () and end_sequence (). If NEED_CMOV is true
3390 : : we call noce_emit_cmove to create a cmove sequence. Otherwise emit
3391 : : a simple move. If successful, store the first instruction of the
3392 : : sequence in TEMP_DEST and the sequence costs in SEQ_COST. */
3393 : :
3394 : : static rtx_insn*
3395 : 373906 : try_emit_cmove_seq (struct noce_if_info *if_info, rtx temp,
3396 : : rtx cond, rtx new_val, rtx old_val, bool need_cmov,
3397 : : unsigned *cost, rtx *temp_dest,
3398 : : rtx cc_cmp = NULL, rtx rev_cc_cmp = NULL)
3399 : : {
3400 : 373906 : rtx_insn *seq = NULL;
3401 : 373906 : *cost = 0;
3402 : :
3403 : 373906 : rtx x = XEXP (cond, 0);
3404 : 373906 : rtx y = XEXP (cond, 1);
3405 : 373906 : rtx_code cond_code = GET_CODE (cond);
3406 : :
3407 : 373906 : start_sequence ();
3408 : :
3409 : 373906 : if (need_cmov)
3410 : 290948 : *temp_dest = noce_emit_cmove (if_info, temp, cond_code,
3411 : : x, y, new_val, old_val, cc_cmp, rev_cc_cmp);
3412 : : else
3413 : : {
3414 : 82958 : *temp_dest = temp;
3415 : 82958 : if (if_info->then_else_reversed)
3416 : 2746 : noce_emit_move_insn (temp, old_val);
3417 : : else
3418 : 80212 : noce_emit_move_insn (temp, new_val);
3419 : : }
3420 : :
3421 : 373906 : if (*temp_dest != NULL_RTX)
3422 : : {
3423 : 367029 : seq = get_insns ();
3424 : 367029 : *cost = seq_cost (seq, if_info->speed_p);
3425 : : }
3426 : :
3427 : 373906 : end_sequence ();
3428 : :
3429 : 373906 : return seq;
3430 : : }
3431 : :
3432 : : /* We have something like:
3433 : :
3434 : : if (x > y)
3435 : : { i = EXPR_A; j = EXPR_B; k = EXPR_C; }
3436 : :
3437 : : Make it:
3438 : :
3439 : : tmp_i = (x > y) ? EXPR_A : i;
3440 : : tmp_j = (x > y) ? EXPR_B : j;
3441 : : tmp_k = (x > y) ? EXPR_C : k;
3442 : : i = tmp_i;
3443 : : j = tmp_j;
3444 : : k = tmp_k;
3445 : :
3446 : : Subsequent passes are expected to clean up the extra moves.
3447 : :
3448 : : Look for special cases such as writes to one register which are
3449 : : read back in another SET, as might occur in a swap idiom or
3450 : : similar.
3451 : :
3452 : : These look like:
3453 : :
3454 : : if (x > y)
3455 : : i = a;
3456 : : j = i;
3457 : :
3458 : : Which we want to rewrite to:
3459 : :
3460 : : tmp_i = (x > y) ? a : i;
3461 : : tmp_j = (x > y) ? tmp_i : j;
3462 : : i = tmp_i;
3463 : : j = tmp_j;
3464 : :
3465 : : We can catch these when looking at (SET x y) by keeping a list of the
3466 : : registers we would have targeted before if-conversion and looking back
3467 : : through it for an overlap with Y. If we find one, we rewire the
3468 : : conditional set to use the temporary we introduced earlier.
3469 : :
3470 : : IF_INFO contains the useful information about the block structure and
3471 : : jump instructions. */
3472 : :
3473 : : static bool
3474 : 39150 : noce_convert_multiple_sets (struct noce_if_info *if_info)
3475 : : {
3476 : 39150 : basic_block test_bb = if_info->test_bb;
3477 : 39150 : basic_block then_bb = if_info->then_bb;
3478 : 39150 : basic_block join_bb = if_info->join_bb;
3479 : 39150 : rtx_insn *jump = if_info->jump;
3480 : 39150 : rtx_insn *cond_earliest;
3481 : 39150 : rtx_insn *insn;
3482 : :
3483 : 39150 : start_sequence ();
3484 : :
3485 : : /* Decompose the condition attached to the jump. */
3486 : 39150 : rtx cond = noce_get_condition (jump, &cond_earliest, false);
3487 : 39150 : rtx x = XEXP (cond, 0);
3488 : 39150 : rtx y = XEXP (cond, 1);
3489 : :
3490 : 39150 : auto_delete_vec<noce_multiple_sets_info> insn_info;
3491 : 39150 : init_noce_multiple_sets_info (then_bb, insn_info);
3492 : :
3493 : 39150 : int last_needs_comparison = -1;
3494 : :
3495 : 39150 : bool ok = noce_convert_multiple_sets_1
3496 : 39150 : (if_info, insn_info, &last_needs_comparison);
3497 : 39150 : if (!ok)
3498 : : return false;
3499 : :
3500 : : /* If there are insns that overwrite part of the initial
3501 : : comparison, we can still omit creating temporaries for
3502 : : the last of them.
3503 : : As the second try will always create a less expensive,
3504 : : valid sequence, we do not need to compare and can discard
3505 : : the first one. */
3506 : 37685 : if (last_needs_comparison != -1)
3507 : : {
3508 : 37685 : end_sequence ();
3509 : 37685 : start_sequence ();
3510 : 37685 : ok = noce_convert_multiple_sets_1
3511 : 37685 : (if_info, insn_info, &last_needs_comparison);
3512 : : /* Actually we should not fail anymore if we reached here,
3513 : : but better still check. */
3514 : 37685 : if (!ok)
3515 : : return false;
3516 : : }
3517 : :
3518 : : /* We must have seen some sort of insn to insert, otherwise we were
3519 : : given an empty BB to convert, and we can't handle that. */
3520 : 37685 : gcc_assert (!insn_info.is_empty ());
3521 : :
3522 : : /* Now fixup the assignments.
3523 : : PR116405: Iterate in reverse order and keep track of the targets so that
3524 : : a move does not overwrite a subsequent value when multiple instructions
3525 : : have the same target. */
3526 : 37685 : unsigned i;
3527 : 37685 : noce_multiple_sets_info *info;
3528 : 37685 : bitmap set_targets = BITMAP_ALLOC (®_obstack);
3529 : 148816 : FOR_EACH_VEC_ELT_REVERSE (insn_info, i, info)
3530 : : {
3531 : 111131 : gcc_checking_assert (REG_P (info->target));
3532 : :
3533 : 111131 : if (info->target != info->temporary
3534 : 111131 : && !bitmap_bit_p (set_targets, REGNO (info->target)))
3535 : 3416 : noce_emit_move_insn (info->target, info->temporary);
3536 : :
3537 : 111131 : bitmap_set_bit (set_targets, REGNO (info->target));
3538 : : }
3539 : 37685 : BITMAP_FREE (set_targets);
3540 : :
3541 : : /* Actually emit the sequence if it isn't too expensive. */
3542 : 37685 : rtx_insn *seq = get_insns ();
3543 : :
3544 : 37685 : if (!targetm.noce_conversion_profitable_p (seq, if_info))
3545 : : {
3546 : 16751 : end_sequence ();
3547 : 16751 : return false;
3548 : : }
3549 : :
3550 : 113823 : for (insn = seq; insn; insn = NEXT_INSN (insn))
3551 : 92889 : set_used_flags (insn);
3552 : :
3553 : : /* Mark all our temporaries and targets as used. */
3554 : 69375 : for (unsigned i = 0; i < insn_info.length (); i++)
3555 : : {
3556 : 48441 : set_used_flags (insn_info[i]->temporary);
3557 : 48441 : set_used_flags (insn_info[i]->target);
3558 : : }
3559 : :
3560 : 20934 : set_used_flags (cond);
3561 : 20934 : set_used_flags (x);
3562 : 20934 : set_used_flags (y);
3563 : :
3564 : 20934 : unshare_all_rtl_in_chain (seq);
3565 : 20934 : end_sequence ();
3566 : :
3567 : 20934 : if (!seq)
3568 : : return false;
3569 : :
3570 : 113701 : for (insn = seq; insn; insn = NEXT_INSN (insn))
3571 : 92803 : if (JUMP_P (insn) || CALL_P (insn)
3572 : 92803 : || recog_memoized (insn) == -1)
3573 : 36 : return false;
3574 : :
3575 : 20898 : emit_insn_before_setloc (seq, if_info->jump,
3576 : 20898 : INSN_LOCATION (insn_info.last ()->unmodified_insn));
3577 : :
3578 : : /* Clean up THEN_BB and the edges in and out of it. */
3579 : 20898 : remove_edge (find_edge (test_bb, join_bb));
3580 : 20898 : remove_edge (find_edge (then_bb, join_bb));
3581 : 20898 : redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3582 : 20898 : delete_basic_block (then_bb);
3583 : 20898 : num_true_changes++;
3584 : :
3585 : : /* Maybe merge blocks now the jump is simple enough. */
3586 : 20898 : if (can_merge_blocks_p (test_bb, join_bb))
3587 : : {
3588 : 13655 : merge_blocks (test_bb, join_bb);
3589 : 13655 : num_true_changes++;
3590 : : }
3591 : :
3592 : 20898 : num_updated_if_blocks++;
3593 : 20898 : if_info->transform_name = "noce_convert_multiple_sets";
3594 : 20898 : return true;
3595 : 39150 : }
3596 : :
3597 : : /* This goes through all relevant insns of IF_INFO->then_bb and tries to create
3598 : : conditional moves. Information for the insns is kept in INSN_INFO. */
3599 : :
3600 : : static bool
3601 : 76835 : noce_convert_multiple_sets_1 (struct noce_if_info *if_info,
3602 : : auto_delete_vec<noce_multiple_sets_info> &insn_info,
3603 : : int *last_needs_comparison)
3604 : : {
3605 : 76835 : basic_block then_bb = if_info->then_bb;
3606 : 76835 : rtx_insn *jump = if_info->jump;
3607 : 76835 : rtx_insn *cond_earliest;
3608 : :
3609 : : /* Decompose the condition attached to the jump. */
3610 : 76835 : rtx cond = noce_get_condition (jump, &cond_earliest, false);
3611 : :
3612 : 76835 : rtx cc_cmp = cond_exec_get_condition (jump);
3613 : 76835 : if (cc_cmp)
3614 : 76835 : cc_cmp = copy_rtx (cc_cmp);
3615 : 76835 : rtx rev_cc_cmp = cond_exec_get_condition (jump, /* get_reversed */ true);
3616 : 76835 : if (rev_cc_cmp)
3617 : 76835 : rev_cc_cmp = copy_rtx (rev_cc_cmp);
3618 : :
3619 : 76835 : rtx_insn *insn;
3620 : 76835 : int count = 0;
3621 : 76835 : bool second_try = *last_needs_comparison != -1;
3622 : :
3623 : 472097 : FOR_BB_INSNS (then_bb, insn)
3624 : : {
3625 : : /* Skip over non-insns. */
3626 : 396727 : if (!active_insn_p (insn))
3627 : 170680 : continue;
3628 : :
3629 : 226047 : noce_multiple_sets_info *info = insn_info[count];
3630 : :
3631 : 226047 : rtx set = single_set (insn);
3632 : 226047 : gcc_checking_assert (set);
3633 : :
3634 : 226047 : rtx target = SET_DEST (set);
3635 : 226047 : rtx temp;
3636 : :
3637 : 226047 : rtx new_val = SET_SRC (set);
3638 : :
3639 : 226047 : int i, ii;
3640 : 289788 : FOR_EACH_VEC_ELT (info->rewired_src, i, ii)
3641 : 63741 : new_val = simplify_replace_rtx (new_val, insn_info[ii]->target,
3642 : 63741 : insn_info[ii]->temporary);
3643 : :
3644 : 226047 : rtx old_val = target;
3645 : :
3646 : : /* As we are transforming
3647 : : if (x > y)
3648 : : {
3649 : : a = b;
3650 : : c = d;
3651 : : }
3652 : : into
3653 : : a = (x > y) ...
3654 : : c = (x > y) ...
3655 : :
3656 : : we potentially check x > y before every set.
3657 : : Even though the check might be removed by subsequent passes, this means
3658 : : that we cannot transform
3659 : : if (x > y)
3660 : : {
3661 : : x = y;
3662 : : ...
3663 : : }
3664 : : into
3665 : : x = (x > y) ...
3666 : : ...
3667 : : since this would invalidate x and the following to-be-removed checks.
3668 : : Therefore we introduce a temporary every time we are about to
3669 : : overwrite a variable used in the check. Costing of a sequence with
3670 : : these is going to be inaccurate so only use temporaries when
3671 : : needed.
3672 : :
3673 : : If performing a second try, we know how many insns require a
3674 : : temporary. For the last of these, we can omit creating one. */
3675 : 226047 : if (reg_overlap_mentioned_p (target, cond)
3676 : 226047 : && (!second_try || count < *last_needs_comparison))
3677 : 25977 : temp = gen_reg_rtx (GET_MODE (target));
3678 : : else
3679 : : temp = target;
3680 : :
3681 : : /* We have identified swap-style idioms before. A normal
3682 : : set will need to be a cmov while the first instruction of a swap-style
3683 : : idiom can be a regular move. This helps with costing. */
3684 : 226047 : bool need_cmov = info->need_cmov;
3685 : :
3686 : : /* If we had a non-canonical conditional jump (i.e. one where
3687 : : the fallthrough is to the "else" case) we need to reverse
3688 : : the conditional select. */
3689 : 226047 : if (if_info->then_else_reversed)
3690 : 57158 : std::swap (old_val, new_val);
3691 : :
3692 : : /* Try emitting a conditional move passing the backend the
3693 : : canonicalized comparison. The backend is then able to
3694 : : recognize expressions like
3695 : :
3696 : : if (x > y)
3697 : : y = x;
3698 : :
3699 : : as min/max and emit an insn, accordingly. */
3700 : 226047 : unsigned cost1 = 0, cost2 = 0;
3701 : 226047 : rtx_insn *seq, *seq1, *seq2 = NULL;
3702 : 226047 : rtx temp_dest = NULL_RTX, temp_dest1 = NULL_RTX, temp_dest2 = NULL_RTX;
3703 : 226047 : bool read_comparison = false;
3704 : :
3705 : 226047 : seq1 = try_emit_cmove_seq (if_info, temp, cond,
3706 : : new_val, old_val, need_cmov,
3707 : : &cost1, &temp_dest1);
3708 : :
3709 : : /* Here, we try to pass the backend a non-canonicalized cc comparison
3710 : : as well. This allows the backend to emit a cmov directly without
3711 : : creating an additional compare for each. If successful, costing
3712 : : is easier and this sequence is usually preferred. */
3713 : 226047 : if (cc_cmp)
3714 : : {
3715 : 147859 : seq2 = try_emit_cmove_seq (if_info, temp, cond,
3716 : : new_val, old_val, need_cmov,
3717 : : &cost2, &temp_dest2, cc_cmp, rev_cc_cmp);
3718 : :
3719 : : /* The if_then_else in SEQ2 may be affected when cc_cmp/rev_cc_cmp is
3720 : : clobbered. We can't safely use the sequence in this case. */
3721 : 280944 : for (rtx_insn *iter = seq2; iter; iter = NEXT_INSN (iter))
3722 : 181514 : if (modified_in_p (cc_cmp, iter)
3723 : 181514 : || (rev_cc_cmp && modified_in_p (rev_cc_cmp, iter)))
3724 : : {
3725 : : seq2 = NULL;
3726 : : break;
3727 : : }
3728 : : }
3729 : :
3730 : : /* The backend might have created a sequence that uses the
3731 : : condition as a value. Check this. */
3732 : :
3733 : : /* We cannot handle anything more complex than a reg or constant. */
3734 : 226047 : if (!REG_P (XEXP (cond, 0)) && !CONSTANT_P (XEXP (cond, 0)))
3735 : 226047 : read_comparison = true;
3736 : :
3737 : 226047 : if (!REG_P (XEXP (cond, 1)) && !CONSTANT_P (XEXP (cond, 1)))
3738 : 226047 : read_comparison = true;
3739 : :
3740 : : rtx_insn *walk = seq2;
3741 : : int if_then_else_count = 0;
3742 : 352395 : while (walk && !read_comparison)
3743 : : {
3744 : 126348 : rtx exprs_to_check[2];
3745 : 126348 : unsigned int exprs_count = 0;
3746 : :
3747 : 126348 : rtx set = single_set (walk);
3748 : 126348 : if (set && XEXP (set, 1)
3749 : 126348 : && GET_CODE (XEXP (set, 1)) == IF_THEN_ELSE)
3750 : : {
3751 : : /* We assume that this is the cmove created by the backend that
3752 : : naturally uses the condition. */
3753 : 74867 : exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 1);
3754 : 74867 : exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 2);
3755 : 74867 : if_then_else_count++;
3756 : : }
3757 : 51481 : else if (NONDEBUG_INSN_P (walk))
3758 : 51481 : exprs_to_check[exprs_count++] = PATTERN (walk);
3759 : :
3760 : : /* Bail if we get more than one if_then_else because the assumption
3761 : : above may be incorrect. */
3762 : 126348 : if (if_then_else_count > 1)
3763 : : {
3764 : 0 : read_comparison = true;
3765 : 0 : break;
3766 : : }
3767 : :
3768 : 327563 : for (unsigned int i = 0; i < exprs_count; i++)
3769 : : {
3770 : 201215 : subrtx_iterator::array_type array;
3771 : 466315 : FOR_EACH_SUBRTX (iter, array, exprs_to_check[i], NONCONST)
3772 : 307134 : if (*iter != NULL_RTX
3773 : 307134 : && (reg_overlap_mentioned_p (XEXP (cond, 0), *iter)
3774 : 283286 : || reg_overlap_mentioned_p (XEXP (cond, 1), *iter)))
3775 : : {
3776 : : read_comparison = true;
3777 : : break;
3778 : : }
3779 : 201215 : }
3780 : :
3781 : 126348 : walk = NEXT_INSN (walk);
3782 : : }
3783 : :
3784 : : /* Check which version is less expensive. */
3785 : 226047 : if (seq1 != NULL_RTX && (cost1 <= cost2 || seq2 == NULL_RTX))
3786 : : {
3787 : 148082 : seq = seq1;
3788 : 148082 : temp_dest = temp_dest1;
3789 : 148082 : if (!second_try)
3790 : 75278 : *last_needs_comparison = count;
3791 : : }
3792 : 1511 : else if (seq2 != NULL_RTX)
3793 : : {
3794 : 76686 : seq = seq2;
3795 : 76686 : temp_dest = temp_dest2;
3796 : 76686 : if (!second_try && read_comparison)
3797 : 11635 : *last_needs_comparison = count;
3798 : : }
3799 : : else
3800 : : {
3801 : : /* Nothing worked, bail out. */
3802 : 1279 : end_sequence ();
3803 : 1465 : return false;
3804 : : }
3805 : :
3806 : : /* Although we use temporaries if there is register overlap of COND and
3807 : : TARGET, it is possible that SEQ modifies COND anyway. For example,
3808 : : COND may use the flags register and if INSN clobbers flags then
3809 : : we may be unable to emit a valid sequence (e.g. in x86 that would
3810 : : require saving and restoring the flags register). */
3811 : 151964 : if (!second_try)
3812 : 331457 : for (rtx_insn *iter = seq; iter; iter = NEXT_INSN (iter))
3813 : 218006 : if (modified_in_p (cond, iter))
3814 : : {
3815 : 186 : end_sequence ();
3816 : 186 : return false;
3817 : : }
3818 : :
3819 : 224582 : if (cc_cmp && seq == seq1)
3820 : : {
3821 : : /* Check if SEQ can clobber registers mentioned in cc_cmp/rev_cc_cmp.
3822 : : If yes, we need to use only SEQ1 from that point on.
3823 : : Only check when we use SEQ1 since we have already tested SEQ2. */
3824 : 96669 : for (rtx_insn *iter = seq; iter; iter = NEXT_INSN (iter))
3825 : 74113 : if (modified_in_p (cc_cmp, iter)
3826 : 74113 : || (rev_cc_cmp && modified_in_p (rev_cc_cmp, iter)))
3827 : : {
3828 : : cc_cmp = NULL_RTX;
3829 : : rev_cc_cmp = NULL_RTX;
3830 : : break;
3831 : : }
3832 : : }
3833 : :
3834 : : /* End the sub sequence and emit to the main sequence. */
3835 : 224582 : emit_insn (seq);
3836 : :
3837 : : /* Bookkeeping. */
3838 : 224582 : count++;
3839 : :
3840 : 224582 : info->target = target;
3841 : 224582 : info->temporary = temp_dest;
3842 : 224582 : info->unmodified_insn = insn;
3843 : : }
3844 : :
3845 : : /* Even if we did not actually need the comparison, we want to make sure
3846 : : to try a second time in order to get rid of the temporaries. */
3847 : 75370 : if (*last_needs_comparison == -1)
3848 : 1158 : *last_needs_comparison = 0;
3849 : :
3850 : : return true;
3851 : : }
3852 : :
3853 : : /* Find local swap-style idioms in BB and mark the first insn (1)
3854 : : that is only a temporary as not needing a conditional move as
3855 : : it is going to be dead afterwards anyway.
3856 : :
3857 : : (1) int tmp = a;
3858 : : a = b;
3859 : : b = tmp;
3860 : :
3861 : : ifcvt
3862 : : -->
3863 : :
3864 : : tmp = a;
3865 : : a = cond ? b : a_old;
3866 : : b = cond ? tmp : b_old;
3867 : :
3868 : : Additionally, store the index of insns like (2) when a subsequent
3869 : : SET reads from their destination.
3870 : :
3871 : : (2) int c = a;
3872 : : int d = c;
3873 : :
3874 : : ifcvt
3875 : : -->
3876 : :
3877 : : c = cond ? a : c_old;
3878 : : d = cond ? d : c; // Need to use c rather than c_old here.
3879 : : */
3880 : :
3881 : : static void
3882 : 39150 : init_noce_multiple_sets_info (basic_block bb,
3883 : : auto_delete_vec<noce_multiple_sets_info> &insn_info)
3884 : : {
3885 : 39150 : rtx_insn *insn;
3886 : 39150 : int count = 0;
3887 : 39150 : auto_vec<rtx> dests;
3888 : 39150 : bitmap bb_live_out = df_get_live_out (bb);
3889 : :
3890 : : /* Iterate over all SETs, storing the destinations in DEST.
3891 : : - If we encounter a previously changed register,
3892 : : rewire the read to the original source.
3893 : : - If we encounter a SET that writes to a destination
3894 : : that is not live after this block then the register
3895 : : does not need to be moved conditionally. */
3896 : 242552 : FOR_BB_INSNS (bb, insn)
3897 : : {
3898 : 203402 : if (!active_insn_p (insn))
3899 : 87888 : continue;
3900 : :
3901 : 115514 : noce_multiple_sets_info *info = new noce_multiple_sets_info;
3902 : 115514 : info->target = NULL_RTX;
3903 : 115514 : info->temporary = NULL_RTX;
3904 : 115514 : info->unmodified_insn = NULL;
3905 : 115514 : insn_info.safe_push (info);
3906 : :
3907 : 115514 : rtx set = single_set (insn);
3908 : 115514 : gcc_checking_assert (set);
3909 : :
3910 : 115514 : rtx src = SET_SRC (set);
3911 : 115514 : rtx dest = SET_DEST (set);
3912 : :
3913 : 115514 : gcc_checking_assert (REG_P (dest));
3914 : 115514 : info->need_cmov = bitmap_bit_p (bb_live_out, REGNO (dest));
3915 : :
3916 : : /* Check if the current SET's source is the same
3917 : : as any previously seen destination.
3918 : : This is quadratic but the number of insns in BB
3919 : : is bounded by PARAM_MAX_RTL_IF_CONVERSION_INSNS. */
3920 : 266997 : for (int i = count - 1; i >= 0; --i)
3921 : 151483 : if (reg_mentioned_p (dests[i], src))
3922 : 33121 : insn_info[count]->rewired_src.safe_push (i);
3923 : :
3924 : 115514 : dests.safe_push (dest);
3925 : 115514 : count++;
3926 : : }
3927 : 39150 : }
3928 : :
3929 : : /* Return true iff basic block TEST_BB is suitable for conversion to a
3930 : : series of conditional moves. Also check that we have more than one
3931 : : set (other routines can handle a single set better than we would),
3932 : : and fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets. While going
3933 : : through the insns store the sum of their potential costs in COST. */
3934 : :
3935 : : static bool
3936 : 1122534 : bb_ok_for_noce_convert_multiple_sets (basic_block test_bb, unsigned *cost)
3937 : : {
3938 : 1122534 : rtx_insn *insn;
3939 : 1122534 : unsigned count = 0;
3940 : 1122534 : unsigned param = param_max_rtl_if_conversion_insns;
3941 : 1122534 : bool speed_p = optimize_bb_for_speed_p (test_bb);
3942 : 1122534 : unsigned potential_cost = 0;
3943 : :
3944 : 7670535 : FOR_BB_INSNS (test_bb, insn)
3945 : : {
3946 : : /* Skip over notes etc. */
3947 : 7536853 : if (!active_insn_p (insn))
3948 : 5523938 : continue;
3949 : :
3950 : : /* We only handle SET insns. */
3951 : 2012915 : rtx set = single_set (insn);
3952 : 2012915 : if (set == NULL_RTX)
3953 : : return false;
3954 : :
3955 : 1799740 : rtx dest = SET_DEST (set);
3956 : 1799740 : rtx src = SET_SRC (set);
3957 : :
3958 : : /* Do not handle anything involving memory loads/stores since it might
3959 : : violate data-race-freedom guarantees. Make sure we can force SRC
3960 : : to a register as that may be needed in try_emit_cmove_seq. */
3961 : 1598836 : if (!REG_P (dest) || contains_mem_rtx_p (src)
3962 : 2880979 : || !noce_can_force_operand (src))
3963 : 728196 : return false;
3964 : :
3965 : : /* Destination and source must be appropriate. */
3966 : 1071544 : if (!noce_operand_ok (dest) || !noce_operand_ok (src))
3967 : 6160 : return false;
3968 : :
3969 : : /* We must be able to conditionally move in this mode. */
3970 : 1065384 : if (!can_conditionally_move_p (GET_MODE (dest)))
3971 : : return false;
3972 : :
3973 : 1024063 : potential_cost += insn_cost (insn, speed_p);
3974 : :
3975 : 1024063 : count++;
3976 : : }
3977 : :
3978 : 133682 : *cost += potential_cost;
3979 : :
3980 : : /* If we would only put out one conditional move, the other strategies
3981 : : this pass tries are better optimized and will be more appropriate.
3982 : : Some targets want to strictly limit the number of conditional moves
3983 : : that are emitted, they set this through PARAM, we need to respect
3984 : : that. */
3985 : 133682 : return count > 1 && count <= param;
3986 : : }
3987 : :
3988 : : /* Compute average of two given costs weighted by relative probabilities
3989 : : of respective basic blocks in an IF-THEN-ELSE. E is the IF-THEN edge.
3990 : : With P as the probability to take the IF-THEN branch, return
3991 : : P * THEN_COST + (1 - P) * ELSE_COST. */
3992 : : static unsigned
3993 : 259100 : average_cost (unsigned then_cost, unsigned else_cost, edge e)
3994 : : {
3995 : 259100 : return else_cost + e->probability.apply ((signed) (then_cost - else_cost));
3996 : : }
3997 : :
3998 : : /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
3999 : : it without using conditional execution. Return TRUE if we were successful
4000 : : at converting the block. */
4001 : :
4002 : : static bool
4003 : 1894290 : noce_process_if_block (struct noce_if_info *if_info)
4004 : : {
4005 : 1894290 : basic_block test_bb = if_info->test_bb; /* test block */
4006 : 1894290 : basic_block then_bb = if_info->then_bb; /* THEN */
4007 : 1894290 : basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
4008 : 1894290 : basic_block join_bb = if_info->join_bb; /* JOIN */
4009 : 1894290 : rtx_insn *jump = if_info->jump;
4010 : 1894290 : rtx cond = if_info->cond;
4011 : 1894290 : rtx_insn *insn_a, *insn_b;
4012 : 1894290 : rtx set_a, set_b;
4013 : 1894290 : rtx orig_x, x, a, b;
4014 : :
4015 : : /* We're looking for patterns of the form
4016 : :
4017 : : (1) if (...) x = a; else x = b;
4018 : : (2) x = b; if (...) x = a;
4019 : : (3) if (...) x = a; // as if with an initial x = x.
4020 : : (4) if (...) { x = a; y = b; z = c; } // Like 3, for multiple SETS.
4021 : : The later patterns require jumps to be more expensive.
4022 : : For the if (...) x = a; else x = b; case we allow multiple insns
4023 : : inside the then and else blocks as long as their only effect is
4024 : : to calculate a value for x.
4025 : : ??? For future expansion, further expand the "multiple X" rules. */
4026 : :
4027 : : /* First look for multiple SETS. The original costs already include
4028 : : a base cost of COSTS_N_INSNS (2): one instruction for the compare
4029 : : (which we will be needing either way) and one instruction for the
4030 : : branch. When comparing costs we want to use the branch instruction
4031 : : cost and the sets vs. the cmovs generated here. Therefore subtract
4032 : : the costs of the compare before checking.
4033 : : ??? Actually, instead of the branch instruction costs we might want
4034 : : to use COSTS_N_INSNS (BRANCH_COST ()) as in other places. */
4035 : :
4036 : 1894290 : unsigned potential_cost = if_info->original_cost - COSTS_N_INSNS (1);
4037 : 1894290 : unsigned old_cost = if_info->original_cost;
4038 : 1894290 : if (!else_bb
4039 : : && HAVE_conditional_move
4040 : 1894290 : && bb_ok_for_noce_convert_multiple_sets (then_bb, &potential_cost))
4041 : : {
4042 : : /* Temporarily set the original costs to what we estimated so
4043 : : we can determine if the transformation is worth it. */
4044 : 39150 : if_info->original_cost = potential_cost;
4045 : 39150 : if (noce_convert_multiple_sets (if_info))
4046 : : {
4047 : 20898 : if (dump_file && if_info->transform_name)
4048 : 1 : fprintf (dump_file, "if-conversion succeeded through %s\n",
4049 : : if_info->transform_name);
4050 : 20898 : return true;
4051 : : }
4052 : :
4053 : : /* Restore the original costs. */
4054 : 18252 : if_info->original_cost = old_cost;
4055 : : }
4056 : :
4057 : 1873392 : bool speed_p = optimize_bb_for_speed_p (test_bb);
4058 : 1873392 : unsigned int then_cost = 0, else_cost = 0;
4059 : 1873392 : if (!bb_valid_for_noce_process_p (then_bb, cond, &then_cost,
4060 : : &if_info->then_simple))
4061 : : return false;
4062 : :
4063 : 500022 : if (else_bb
4064 : 500022 : && !bb_valid_for_noce_process_p (else_bb, cond, &else_cost,
4065 : : &if_info->else_simple))
4066 : : return false;
4067 : :
4068 : 303123 : if (speed_p)
4069 : 259100 : if_info->original_cost += average_cost (then_cost, else_cost,
4070 : : find_edge (test_bb, then_bb));
4071 : : else
4072 : 44023 : if_info->original_cost += then_cost + else_cost;
4073 : :
4074 : 303123 : insn_a = last_active_insn (then_bb, false);
4075 : 303123 : set_a = single_set (insn_a);
4076 : 303123 : gcc_assert (set_a);
4077 : :
4078 : 303123 : x = SET_DEST (set_a);
4079 : 303123 : a = SET_SRC (set_a);
4080 : :
4081 : : /* Look for the other potential set. Make sure we've got equivalent
4082 : : destinations. */
4083 : : /* ??? This is overconservative. Storing to two different mems is
4084 : : as easy as conditionally computing the address. Storing to a
4085 : : single mem merely requires a scratch memory to use as one of the
4086 : : destination addresses; often the memory immediately below the
4087 : : stack pointer is available for this. */
4088 : 303123 : set_b = NULL_RTX;
4089 : 303123 : if (else_bb)
4090 : : {
4091 : 88177 : insn_b = last_active_insn (else_bb, false);
4092 : 88177 : set_b = single_set (insn_b);
4093 : 88177 : gcc_assert (set_b);
4094 : :
4095 : 88177 : if (!rtx_interchangeable_p (x, SET_DEST (set_b)))
4096 : : return false;
4097 : : }
4098 : : else
4099 : : {
4100 : 214946 : insn_b = if_info->cond_earliest;
4101 : 568924 : do
4102 : 568924 : insn_b = prev_nonnote_nondebug_insn (insn_b);
4103 : : while (insn_b
4104 : 566314 : && (BLOCK_FOR_INSN (insn_b)
4105 : 566314 : == BLOCK_FOR_INSN (if_info->cond_earliest))
4106 : 1263203 : && !modified_in_p (x, insn_b));
4107 : :
4108 : : /* We're going to be moving the evaluation of B down from above
4109 : : COND_EARLIEST to JUMP. Make sure the relevant data is still
4110 : : intact. */
4111 : 214946 : if (! insn_b
4112 : 212336 : || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
4113 : 125355 : || !NONJUMP_INSN_P (insn_b)
4114 : 119689 : || (set_b = single_set (insn_b)) == NULL_RTX
4115 : 118160 : || ! rtx_interchangeable_p (x, SET_DEST (set_b))
4116 : 105229 : || ! noce_operand_ok (SET_SRC (set_b))
4117 : 103000 : || reg_overlap_mentioned_p (x, SET_SRC (set_b))
4118 : 101196 : || modified_between_p (SET_SRC (set_b), insn_b, jump)
4119 : : /* Avoid extending the lifetime of hard registers on small
4120 : : register class machines. */
4121 : 98393 : || (REG_P (SET_SRC (set_b))
4122 : 27121 : && HARD_REGISTER_P (SET_SRC (set_b))
4123 : 4216 : && targetm.small_register_classes_for_mode_p
4124 : 4216 : (GET_MODE (SET_SRC (set_b))))
4125 : : /* Likewise with X. In particular this can happen when
4126 : : noce_get_condition looks farther back in the instruction
4127 : : stream than one might expect. */
4128 : 94177 : || reg_overlap_mentioned_p (x, cond)
4129 : 79722 : || reg_overlap_mentioned_p (x, a)
4130 : 285107 : || modified_between_p (x, insn_b, jump))
4131 : : {
4132 : : insn_b = NULL;
4133 : : set_b = NULL_RTX;
4134 : : }
4135 : : }
4136 : :
4137 : : /* If x has side effects then only the if-then-else form is safe to
4138 : : convert. But even in that case we would need to restore any notes
4139 : : (such as REG_INC) at then end. That can be tricky if
4140 : : noce_emit_move_insn expands to more than one insn, so disable the
4141 : : optimization entirely for now if there are side effects. */
4142 : 290265 : if (side_effects_p (x))
4143 : : return false;
4144 : :
4145 : 290265 : b = (set_b ? SET_SRC (set_b) : x);
4146 : :
4147 : : /* Only operate on register destinations, and even then avoid extending
4148 : : the lifetime of hard registers on small register class machines. */
4149 : 290265 : orig_x = x;
4150 : 290265 : if_info->orig_x = orig_x;
4151 : 290265 : if (!REG_P (x)
4152 : 290265 : || (HARD_REGISTER_P (x)
4153 : 2 : && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
4154 : : {
4155 : 62900 : if (GET_MODE (x) == BLKmode)
4156 : : return false;
4157 : :
4158 : 62573 : if (GET_CODE (x) == ZERO_EXTRACT
4159 : 3 : && (!CONST_INT_P (XEXP (x, 1))
4160 : 3 : || !CONST_INT_P (XEXP (x, 2))))
4161 : : return false;
4162 : :
4163 : 62573 : x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
4164 : : ? XEXP (x, 0) : x));
4165 : : }
4166 : :
4167 : : /* Don't operate on sources that may trap or are volatile. */
4168 : 289938 : if (! noce_operand_ok (a) || ! noce_operand_ok (b))
4169 : 0 : return false;
4170 : :
4171 : 338258 : retry:
4172 : : /* Set up the info block for our subroutines. */
4173 : 338258 : if_info->insn_a = insn_a;
4174 : 338258 : if_info->insn_b = insn_b;
4175 : 338258 : if_info->x = x;
4176 : 338258 : if_info->a = a;
4177 : 338258 : if_info->b = b;
4178 : :
4179 : : /* Try optimizations in some approximation of a useful order. */
4180 : : /* ??? Should first look to see if X is live incoming at all. If it
4181 : : isn't, we don't need anything but an unconditional set. */
4182 : :
4183 : : /* Look and see if A and B are really the same. Avoid creating silly
4184 : : cmove constructs that no one will fix up later. */
4185 : 338258 : if (noce_simple_bbs (if_info)
4186 : 310146 : && rtx_interchangeable_p (a, b))
4187 : : {
4188 : : /* If we have an INSN_B, we don't have to create any new rtl. Just
4189 : : move the instruction that we already have. If we don't have an
4190 : : INSN_B, that means that A == X, and we've got a noop move. In
4191 : : that case don't do anything and let the code below delete INSN_A. */
4192 : 488 : if (insn_b && else_bb)
4193 : : {
4194 : 473 : rtx note;
4195 : :
4196 : 473 : if (else_bb && insn_b == BB_END (else_bb))
4197 : 263 : BB_END (else_bb) = PREV_INSN (insn_b);
4198 : 473 : reorder_insns (insn_b, insn_b, PREV_INSN (jump));
4199 : :
4200 : : /* If there was a REG_EQUAL note, delete it since it may have been
4201 : : true due to this insn being after a jump. */
4202 : 473 : if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
4203 : 6 : remove_note (insn_b, note);
4204 : :
4205 : 488 : insn_b = NULL;
4206 : : }
4207 : : /* If we have "x = b; if (...) x = a;", and x has side-effects, then
4208 : : x must be executed twice. */
4209 : 15 : else if (insn_b && side_effects_p (orig_x))
4210 : : return false;
4211 : :
4212 : 488 : x = orig_x;
4213 : 488 : goto success;
4214 : : }
4215 : :
4216 : 337770 : if (!set_b && MEM_P (orig_x))
4217 : : /* We want to avoid store speculation to avoid cases like
4218 : : if (pthread_mutex_trylock(mutex))
4219 : : ++global_variable;
4220 : : Rather than go to much effort here, we rely on the SSA optimizers,
4221 : : which do a good enough job these days. */
4222 : : return false;
4223 : :
4224 : 276330 : if (noce_try_move (if_info))
4225 : 0 : goto success;
4226 : 276330 : if (noce_try_ifelse_collapse (if_info))
4227 : 2115 : goto success;
4228 : 274215 : if (noce_try_store_flag (if_info))
4229 : 16543 : goto success;
4230 : 257672 : if (noce_try_bitop (if_info))
4231 : 0 : goto success;
4232 : 257672 : if (noce_try_minmax (if_info))
4233 : 56 : goto success;
4234 : 257616 : if (noce_try_abs (if_info))
4235 : 24 : goto success;
4236 : 257592 : if (noce_try_inverse_constants (if_info))
4237 : 0 : goto success;
4238 : 257592 : if (!targetm.have_conditional_execution ()
4239 : 257592 : && noce_try_store_flag_constants (if_info))
4240 : 2384 : goto success;
4241 : 255208 : if (HAVE_conditional_move
4242 : 255208 : && noce_try_cmove (if_info))
4243 : 45901 : goto success;
4244 : 209307 : if (! targetm.have_conditional_execution ())
4245 : : {
4246 : 209307 : if (noce_try_addcc (if_info))
4247 : 7328 : goto success;
4248 : 201979 : if (noce_try_store_flag_mask (if_info))
4249 : 2 : goto success;
4250 : 201977 : if (HAVE_conditional_move
4251 : 201977 : && noce_try_cond_zero_arith (if_info))
4252 : 0 : goto success;
4253 : 201977 : if (HAVE_conditional_move
4254 : 201977 : && noce_try_cmove_arith (if_info))
4255 : 37705 : goto success;
4256 : 164272 : if (noce_try_sign_mask (if_info))
4257 : 0 : goto success;
4258 : : }
4259 : :
4260 : 164272 : if (!else_bb && set_b)
4261 : : {
4262 : 48320 : insn_b = NULL;
4263 : 48320 : set_b = NULL_RTX;
4264 : 48320 : b = orig_x;
4265 : 48320 : goto retry;
4266 : : }
4267 : :
4268 : : return false;
4269 : :
4270 : 112546 : success:
4271 : 112546 : if (dump_file && if_info->transform_name)
4272 : 5 : fprintf (dump_file, "if-conversion succeeded through %s\n",
4273 : : if_info->transform_name);
4274 : :
4275 : : /* If we used a temporary, fix it up now. */
4276 : 112546 : if (orig_x != x)
4277 : : {
4278 : 526 : rtx_insn *seq;
4279 : :
4280 : 526 : start_sequence ();
4281 : 526 : noce_emit_move_insn (orig_x, x);
4282 : 526 : seq = get_insns ();
4283 : 526 : set_used_flags (orig_x);
4284 : 526 : unshare_all_rtl_in_chain (seq);
4285 : 526 : end_sequence ();
4286 : :
4287 : 526 : emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
4288 : : }
4289 : :
4290 : : /* The original THEN and ELSE blocks may now be removed. The test block
4291 : : must now jump to the join block. If the test block and the join block
4292 : : can be merged, do so. */
4293 : 112546 : if (else_bb)
4294 : : {
4295 : 41444 : delete_basic_block (else_bb);
4296 : 41444 : num_true_changes++;
4297 : : }
4298 : : else
4299 : 71102 : remove_edge (find_edge (test_bb, join_bb));
4300 : :
4301 : 112546 : remove_edge (find_edge (then_bb, join_bb));
4302 : 112546 : redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
4303 : 112546 : delete_basic_block (then_bb);
4304 : 112546 : num_true_changes++;
4305 : :
4306 : 112546 : if (can_merge_blocks_p (test_bb, join_bb))
4307 : : {
4308 : 83549 : merge_blocks (test_bb, join_bb);
4309 : 83549 : num_true_changes++;
4310 : : }
4311 : :
4312 : 112546 : num_updated_if_blocks++;
4313 : 112546 : return true;
4314 : : }
4315 : :
4316 : : /* Check whether a block is suitable for conditional move conversion.
4317 : : Every insn must be a simple set of a register to a constant or a
4318 : : register. For each assignment, store the value in the pointer map
4319 : : VALS, keyed indexed by register pointer, then store the register
4320 : : pointer in REGS. COND is the condition we will test. */
4321 : :
4322 : : static bool
4323 : 1797971 : check_cond_move_block (basic_block bb,
4324 : : hash_map<rtx, rtx> *vals,
4325 : : vec<rtx> *regs,
4326 : : rtx cond)
4327 : : {
4328 : 1797971 : rtx_insn *insn;
4329 : 1797971 : rtx cc = cc_in_cond (cond);
4330 : :
4331 : : /* We can only handle simple jumps at the end of the basic block.
4332 : : It is almost impossible to update the CFG otherwise. */
4333 : 1797971 : insn = BB_END (bb);
4334 : 1797971 : if (JUMP_P (insn) && !onlyjump_p (insn))
4335 : : return false;
4336 : :
4337 : 8856875 : FOR_BB_INSNS (bb, insn)
4338 : : {
4339 : 8775435 : rtx set, dest, src;
4340 : :
4341 : 8775435 : if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
4342 : 6880370 : continue;
4343 : 1895065 : set = single_set (insn);
4344 : 1895065 : if (!set)
4345 : 1716484 : return false;
4346 : :
4347 : 1823895 : dest = SET_DEST (set);
4348 : 1823895 : src = SET_SRC (set);
4349 : 1823895 : if (!REG_P (dest)
4350 : 1823895 : || (HARD_REGISTER_P (dest)
4351 : 293931 : && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
4352 : 684879 : return false;
4353 : :
4354 : 1139016 : if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
4355 : : return false;
4356 : :
4357 : 204442 : if (side_effects_p (src) || side_effects_p (dest))
4358 : 0 : return false;
4359 : :
4360 : 204442 : if (may_trap_p (src) || may_trap_p (dest))
4361 : 0 : return false;
4362 : :
4363 : : /* Don't try to handle this if the source register was
4364 : : modified earlier in the block. */
4365 : 204442 : if ((REG_P (src)
4366 : 49748 : && vals->get (src))
4367 : 253922 : || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
4368 : 4258 : && vals->get (SUBREG_REG (src))))
4369 : 291 : return false;
4370 : :
4371 : : /* Don't try to handle this if the destination register was
4372 : : modified earlier in the block. */
4373 : 204151 : if (vals->get (dest))
4374 : : return false;
4375 : :
4376 : : /* Don't try to handle this if the condition uses the
4377 : : destination register. */
4378 : 204151 : if (reg_overlap_mentioned_p (dest, cond))
4379 : : return false;
4380 : :
4381 : : /* Don't try to handle this if the source register is modified
4382 : : later in the block. */
4383 : 182381 : if (!CONSTANT_P (src)
4384 : 182381 : && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
4385 : : return false;
4386 : :
4387 : : /* Skip it if the instruction to be moved might clobber CC. */
4388 : 178581 : if (cc && set_of (cc, insn))
4389 : : return false;
4390 : :
4391 : 178581 : vals->put (dest, src);
4392 : :
4393 : 178581 : regs->safe_push (dest);
4394 : : }
4395 : :
4396 : : return true;
4397 : : }
4398 : :
4399 : : /* Given a basic block BB suitable for conditional move conversion,
4400 : : a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
4401 : : the register values depending on COND, emit the insns in the block as
4402 : : conditional moves. If ELSE_BLOCK is true, THEN_BB was already
4403 : : processed. The caller has started a sequence for the conversion.
4404 : : Return true if successful, false if something goes wrong. */
4405 : :
4406 : : static bool
4407 : 59015 : cond_move_convert_if_block (struct noce_if_info *if_infop,
4408 : : basic_block bb, rtx cond,
4409 : : hash_map<rtx, rtx> *then_vals,
4410 : : hash_map<rtx, rtx> *else_vals,
4411 : : bool else_block_p)
4412 : : {
4413 : 59015 : enum rtx_code code;
4414 : 59015 : rtx_insn *insn;
4415 : 59015 : rtx cond_arg0, cond_arg1;
4416 : :
4417 : 59015 : code = GET_CODE (cond);
4418 : 59015 : cond_arg0 = XEXP (cond, 0);
4419 : 59015 : cond_arg1 = XEXP (cond, 1);
4420 : :
4421 : 234806 : FOR_BB_INSNS (bb, insn)
4422 : : {
4423 : 191241 : rtx set, target, dest, t, e;
4424 : :
4425 : : /* ??? Maybe emit conditional debug insn? */
4426 : 191241 : if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
4427 : 125568 : continue;
4428 : 101728 : set = single_set (insn);
4429 : 101728 : gcc_assert (set && REG_P (SET_DEST (set)));
4430 : :
4431 : 101728 : dest = SET_DEST (set);
4432 : :
4433 : 101728 : rtx *then_slot = then_vals->get (dest);
4434 : 101728 : rtx *else_slot = else_vals->get (dest);
4435 : 101728 : t = then_slot ? *then_slot : NULL_RTX;
4436 : 101728 : e = else_slot ? *else_slot : NULL_RTX;
4437 : :
4438 : 101728 : if (else_block_p)
4439 : : {
4440 : : /* If this register was set in the then block, we already
4441 : : handled this case there. */
4442 : 38138 : if (t)
4443 : 36055 : continue;
4444 : 2083 : t = dest;
4445 : 2083 : gcc_assert (e);
4446 : : }
4447 : : else
4448 : : {
4449 : 63590 : gcc_assert (t);
4450 : 63590 : if (!e)
4451 : 26591 : e = dest;
4452 : : }
4453 : :
4454 : 65673 : if (if_infop->cond_inverted)
4455 : 0 : std::swap (t, e);
4456 : :
4457 : 65673 : target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
4458 : : t, e);
4459 : 65673 : if (!target)
4460 : 15450 : return false;
4461 : :
4462 : 50223 : if (target != dest)
4463 : 0 : noce_emit_move_insn (dest, target);
4464 : : }
4465 : :
4466 : : return true;
4467 : : }
4468 : :
4469 : : /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
4470 : : it using only conditional moves. Return TRUE if we were successful at
4471 : : converting the block. */
4472 : :
4473 : : static bool
4474 : 1760846 : cond_move_process_if_block (struct noce_if_info *if_info)
4475 : : {
4476 : 1760846 : basic_block test_bb = if_info->test_bb;
4477 : 1760846 : basic_block then_bb = if_info->then_bb;
4478 : 1760846 : basic_block else_bb = if_info->else_bb;
4479 : 1760846 : basic_block join_bb = if_info->join_bb;
4480 : 1760846 : rtx_insn *jump = if_info->jump;
4481 : 1760846 : rtx cond = if_info->cond;
4482 : 1760846 : rtx_insn *seq, *loc_insn;
4483 : 1760846 : int c;
4484 : 1760846 : vec<rtx> then_regs = vNULL;
4485 : 1760846 : vec<rtx> else_regs = vNULL;
4486 : 1760846 : bool success_p = false;
4487 : 1760846 : int limit = param_max_rtl_if_conversion_insns;
4488 : :
4489 : : /* Build a mapping for each block to the value used for each
4490 : : register. */
4491 : 1760846 : hash_map<rtx, rtx> then_vals;
4492 : 1760846 : hash_map<rtx, rtx> else_vals;
4493 : :
4494 : : /* Make sure the blocks are suitable. */
4495 : 1760846 : if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
4496 : 1760846 : || (else_bb
4497 : 37125 : && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
4498 : 1716531 : goto done;
4499 : :
4500 : : /* Make sure the blocks can be used together. If the same register
4501 : : is set in both blocks, and is not set to a constant in both
4502 : : cases, then both blocks must set it to the same register. We
4503 : : have already verified that if it is set to a register, that the
4504 : : source register does not change after the assignment. Also count
4505 : : the number of registers set in only one of the blocks. */
4506 : 44315 : c = 0;
4507 : 197369 : for (rtx reg : then_regs)
4508 : : {
4509 : 67019 : rtx *then_slot = then_vals.get (reg);
4510 : 67019 : rtx *else_slot = else_vals.get (reg);
4511 : :
4512 : 67019 : gcc_checking_assert (then_slot);
4513 : 67019 : if (!else_slot)
4514 : 27232 : ++c;
4515 : : else
4516 : : {
4517 : 39787 : rtx then_val = *then_slot;
4518 : 39787 : rtx else_val = *else_slot;
4519 : 3358 : if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
4520 : 42244 : && !rtx_equal_p (then_val, else_val))
4521 : 2285 : goto done;
4522 : : }
4523 : : }
4524 : :
4525 : : /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
4526 : 117769 : for (rtx reg : else_regs)
4527 : : {
4528 : 39705 : gcc_checking_assert (else_vals.get (reg));
4529 : 39705 : if (!then_vals.get (reg))
4530 : 2217 : ++c;
4531 : : }
4532 : :
4533 : : /* Make sure it is reasonable to convert this block. What matters
4534 : : is the number of assignments currently made in only one of the
4535 : : branches, since if we convert we are going to always execute
4536 : : them. */
4537 : 42030 : if (c > MAX_CONDITIONAL_EXECUTE
4538 : 42030 : || c > limit)
4539 : 18 : goto done;
4540 : :
4541 : : /* Try to emit the conditional moves. First do the then block,
4542 : : then do anything left in the else blocks. */
4543 : 42012 : start_sequence ();
4544 : 42012 : if (!cond_move_convert_if_block (if_info, then_bb, cond,
4545 : : &then_vals, &else_vals, false)
4546 : 42012 : || (else_bb
4547 : 17003 : && !cond_move_convert_if_block (if_info, else_bb, cond,
4548 : : &then_vals, &else_vals, true)))
4549 : : {
4550 : 15450 : end_sequence ();
4551 : 15450 : goto done;
4552 : : }
4553 : 26562 : seq = end_ifcvt_sequence (if_info);
4554 : 26562 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
4555 : 22149 : goto done;
4556 : :
4557 : 4413 : loc_insn = first_active_insn (then_bb);
4558 : 4413 : if (!loc_insn)
4559 : : {
4560 : 4 : loc_insn = first_active_insn (else_bb);
4561 : 4 : gcc_assert (loc_insn);
4562 : : }
4563 : 4413 : emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
4564 : :
4565 : 4413 : if (else_bb)
4566 : : {
4567 : 4413 : delete_basic_block (else_bb);
4568 : 4413 : num_true_changes++;
4569 : : }
4570 : : else
4571 : 0 : remove_edge (find_edge (test_bb, join_bb));
4572 : :
4573 : 4413 : remove_edge (find_edge (then_bb, join_bb));
4574 : 4413 : redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
4575 : 4413 : delete_basic_block (then_bb);
4576 : 4413 : num_true_changes++;
4577 : :
4578 : 4413 : if (can_merge_blocks_p (test_bb, join_bb))
4579 : : {
4580 : 729 : merge_blocks (test_bb, join_bb);
4581 : 729 : num_true_changes++;
4582 : : }
4583 : :
4584 : 4413 : num_updated_if_blocks++;
4585 : 4413 : success_p = true;
4586 : :
4587 : 1760846 : done:
4588 : 1760846 : then_regs.release ();
4589 : 1760846 : else_regs.release ();
4590 : 1760846 : return success_p;
4591 : 1760846 : }
4592 : :
4593 : :
4594 : : /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
4595 : : IF-THEN-ELSE-JOIN block.
4596 : :
4597 : : If so, we'll try to convert the insns to not require the branch,
4598 : : using only transformations that do not require conditional execution.
4599 : :
4600 : : Return TRUE if we were successful at converting the block. */
4601 : :
4602 : : static bool
4603 : 9727901 : noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
4604 : : int pass)
4605 : : {
4606 : 9727901 : basic_block then_bb, else_bb, join_bb;
4607 : 9727901 : bool then_else_reversed = false;
4608 : 9727901 : rtx_insn *jump;
4609 : 9727901 : rtx_insn *cond_earliest;
4610 : 9727901 : struct noce_if_info if_info;
4611 : 9727901 : bool speed_p = optimize_bb_for_speed_p (test_bb);
4612 : :
4613 : : /* We only ever should get here before reload. */
4614 : 9727901 : gcc_assert (!reload_completed);
4615 : :
4616 : : /* Recognize an IF-THEN-ELSE-JOIN block. */
4617 : 9727901 : if (single_pred_p (then_edge->dest)
4618 : 7910634 : && single_succ_p (then_edge->dest)
4619 : 3435609 : && single_pred_p (else_edge->dest)
4620 : 10026749 : && single_succ_p (else_edge->dest)
4621 : 10860915 : && single_succ (then_edge->dest) == single_succ (else_edge->dest))
4622 : : {
4623 : : then_bb = then_edge->dest;
4624 : : else_bb = else_edge->dest;
4625 : : join_bb = single_succ (then_bb);
4626 : : }
4627 : : /* Recognize an IF-THEN-JOIN block. */
4628 : 8893735 : else if (single_pred_p (then_edge->dest)
4629 : 10510233 : && single_succ_p (then_edge->dest)
4630 : 11495178 : && single_succ (then_edge->dest) == else_edge->dest)
4631 : : {
4632 : : then_bb = then_edge->dest;
4633 : : else_bb = NULL_BLOCK;
4634 : : join_bb = else_edge->dest;
4635 : : }
4636 : : /* Recognize an IF-ELSE-JOIN block. We can have those because the order
4637 : : of basic blocks in cfglayout mode does not matter, so the fallthrough
4638 : : edge can go to any basic block (and not just to bb->next_bb, like in
4639 : : cfgrtl mode). */
4640 : 17498834 : else if (single_pred_p (else_edge->dest)
4641 : 11164948 : && single_succ_p (else_edge->dest)
4642 : 9483694 : && single_succ (else_edge->dest) == then_edge->dest)
4643 : : {
4644 : : /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
4645 : : To make this work, we have to invert the THEN and ELSE blocks
4646 : : and reverse the jump condition. */
4647 : : then_bb = else_edge->dest;
4648 : : else_bb = NULL_BLOCK;
4649 : : join_bb = single_succ (then_bb);
4650 : : then_else_reversed = true;
4651 : : }
4652 : : else
4653 : : /* Not a form we can handle. */
4654 : : return false;
4655 : :
4656 : : /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4657 : 1956750 : if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4658 : : return false;
4659 : 1904805 : if (else_bb
4660 : 1904805 : && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4661 : : return false;
4662 : :
4663 : 1894491 : num_possible_if_blocks++;
4664 : :
4665 : 1894491 : if (dump_file)
4666 : : {
4667 : 90 : fprintf (dump_file,
4668 : : "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
4669 : : (else_bb) ? "-ELSE" : "",
4670 : : pass, test_bb->index, then_bb->index);
4671 : :
4672 : 53 : if (else_bb)
4673 : 16 : fprintf (dump_file, ", else %d", else_bb->index);
4674 : :
4675 : 53 : fprintf (dump_file, ", join %d\n", join_bb->index);
4676 : : }
4677 : :
4678 : : /* If the conditional jump is more than just a conditional
4679 : : jump, then we cannot do if-conversion on this block. */
4680 : 1894491 : jump = BB_END (test_bb);
4681 : 1894491 : if (! onlyjump_p (jump))
4682 : : return false;
4683 : :
4684 : : /* Initialize an IF_INFO struct to pass around. */
4685 : 1894290 : memset (&if_info, 0, sizeof if_info);
4686 : 1894290 : if_info.test_bb = test_bb;
4687 : 1894290 : if_info.then_bb = then_bb;
4688 : 1894290 : if_info.else_bb = else_bb;
4689 : 1894290 : if_info.join_bb = join_bb;
4690 : 1894290 : if_info.cond = noce_get_condition (jump, &cond_earliest,
4691 : : then_else_reversed);
4692 : 1894290 : rtx_insn *rev_cond_earliest;
4693 : 3788580 : if_info.rev_cond = noce_get_condition (jump, &rev_cond_earliest,
4694 : 1894290 : !then_else_reversed);
4695 : 1894290 : if (!if_info.cond && !if_info.rev_cond)
4696 : : return false;
4697 : 1894290 : if (!if_info.cond)
4698 : : {
4699 : 0 : std::swap (if_info.cond, if_info.rev_cond);
4700 : 0 : std::swap (cond_earliest, rev_cond_earliest);
4701 : 0 : if_info.cond_inverted = true;
4702 : : }
4703 : : /* We must be comparing objects whose modes imply the size. */
4704 : 1894290 : if (GET_MODE (XEXP (if_info.cond, 0)) == BLKmode)
4705 : : return false;
4706 : 1894290 : gcc_assert (if_info.rev_cond == NULL_RTX
4707 : : || rev_cond_earliest == cond_earliest);
4708 : 1894290 : if_info.cond_earliest = cond_earliest;
4709 : 1894290 : if_info.jump = jump;
4710 : 1894290 : if_info.then_else_reversed = then_else_reversed;
4711 : 1894290 : if_info.speed_p = speed_p;
4712 : 1894290 : if_info.max_seq_cost
4713 : 1894290 : = targetm.max_noce_ifcvt_seq_cost (then_edge);
4714 : : /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
4715 : : that they are valid to transform. We can't easily get back to the insn
4716 : : for COND (and it may not exist if we had to canonicalize to get COND),
4717 : : and jump_insns are always given a cost of 1 by seq_cost, so treat
4718 : : both instructions as having cost COSTS_N_INSNS (1). */
4719 : 1894290 : if_info.original_cost = COSTS_N_INSNS (2);
4720 : :
4721 : :
4722 : : /* Do the real work. */
4723 : :
4724 : : /* ??? noce_process_if_block has not yet been updated to handle
4725 : : inverted conditions. */
4726 : 1894290 : if (!if_info.cond_inverted && noce_process_if_block (&if_info))
4727 : : return true;
4728 : :
4729 : 1760846 : if (HAVE_conditional_move
4730 : 1760846 : && cond_move_process_if_block (&if_info))
4731 : : return true;
4732 : :
4733 : : return false;
4734 : : }
4735 : :
4736 : :
4737 : : /* Merge the blocks and mark for local life update. */
4738 : :
4739 : : static void
4740 : 0 : merge_if_block (struct ce_if_block * ce_info)
4741 : : {
4742 : 0 : basic_block test_bb = ce_info->test_bb; /* last test block */
4743 : 0 : basic_block then_bb = ce_info->then_bb; /* THEN */
4744 : 0 : basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
4745 : 0 : basic_block join_bb = ce_info->join_bb; /* join block */
4746 : 0 : basic_block combo_bb;
4747 : :
4748 : : /* All block merging is done into the lower block numbers. */
4749 : :
4750 : 0 : combo_bb = test_bb;
4751 : 0 : df_set_bb_dirty (test_bb);
4752 : :
4753 : : /* Merge any basic blocks to handle && and || subtests. Each of
4754 : : the blocks are on the fallthru path from the predecessor block. */
4755 : 0 : if (ce_info->num_multiple_test_blocks > 0)
4756 : : {
4757 : 0 : basic_block bb = test_bb;
4758 : 0 : basic_block last_test_bb = ce_info->last_test_bb;
4759 : 0 : basic_block fallthru = block_fallthru (bb);
4760 : :
4761 : 0 : do
4762 : : {
4763 : 0 : bb = fallthru;
4764 : 0 : fallthru = block_fallthru (bb);
4765 : 0 : merge_blocks (combo_bb, bb);
4766 : 0 : num_true_changes++;
4767 : : }
4768 : 0 : while (bb != last_test_bb);
4769 : : }
4770 : :
4771 : : /* Merge TEST block into THEN block. Normally the THEN block won't have a
4772 : : label, but it might if there were || tests. That label's count should be
4773 : : zero, and it normally should be removed. */
4774 : :
4775 : 0 : if (then_bb)
4776 : : {
4777 : : /* If THEN_BB has no successors, then there's a BARRIER after it.
4778 : : If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
4779 : : is no longer needed, and in fact it is incorrect to leave it in
4780 : : the insn stream. */
4781 : 0 : if (EDGE_COUNT (then_bb->succs) == 0
4782 : 0 : && EDGE_COUNT (combo_bb->succs) > 1)
4783 : : {
4784 : 0 : rtx_insn *end = NEXT_INSN (BB_END (then_bb));
4785 : 0 : while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4786 : 0 : end = NEXT_INSN (end);
4787 : :
4788 : 0 : if (end && BARRIER_P (end))
4789 : 0 : delete_insn (end);
4790 : : }
4791 : 0 : merge_blocks (combo_bb, then_bb);
4792 : 0 : num_true_changes++;
4793 : : }
4794 : :
4795 : : /* The ELSE block, if it existed, had a label. That label count
4796 : : will almost always be zero, but odd things can happen when labels
4797 : : get their addresses taken. */
4798 : 0 : if (else_bb)
4799 : : {
4800 : : /* If ELSE_BB has no successors, then there's a BARRIER after it.
4801 : : If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
4802 : : is no longer needed, and in fact it is incorrect to leave it in
4803 : : the insn stream. */
4804 : 0 : if (EDGE_COUNT (else_bb->succs) == 0
4805 : 0 : && EDGE_COUNT (combo_bb->succs) > 1)
4806 : : {
4807 : 0 : rtx_insn *end = NEXT_INSN (BB_END (else_bb));
4808 : 0 : while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4809 : 0 : end = NEXT_INSN (end);
4810 : :
4811 : 0 : if (end && BARRIER_P (end))
4812 : 0 : delete_insn (end);
4813 : : }
4814 : 0 : merge_blocks (combo_bb, else_bb);
4815 : 0 : num_true_changes++;
4816 : : }
4817 : :
4818 : : /* If there was no join block reported, that means it was not adjacent
4819 : : to the others, and so we cannot merge them. */
4820 : :
4821 : 0 : if (! join_bb)
4822 : : {
4823 : 0 : rtx_insn *last = BB_END (combo_bb);
4824 : :
4825 : : /* The outgoing edge for the current COMBO block should already
4826 : : be correct. Verify this. */
4827 : 0 : if (EDGE_COUNT (combo_bb->succs) == 0)
4828 : 0 : gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
4829 : : || (NONJUMP_INSN_P (last)
4830 : : && GET_CODE (PATTERN (last)) == TRAP_IF
4831 : : && (TRAP_CONDITION (PATTERN (last))
4832 : : == const_true_rtx)));
4833 : :
4834 : : else
4835 : : /* There should still be something at the end of the THEN or ELSE
4836 : : blocks taking us to our final destination. */
4837 : 0 : gcc_assert (JUMP_P (last)
4838 : : || (EDGE_SUCC (combo_bb, 0)->dest
4839 : : == EXIT_BLOCK_PTR_FOR_FN (cfun)
4840 : : && CALL_P (last)
4841 : : && SIBLING_CALL_P (last))
4842 : : || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
4843 : : && can_throw_internal (last)));
4844 : : }
4845 : :
4846 : : /* The JOIN block may have had quite a number of other predecessors too.
4847 : : Since we've already merged the TEST, THEN and ELSE blocks, we should
4848 : : have only one remaining edge from our if-then-else diamond. If there
4849 : : is more than one remaining edge, it must come from elsewhere. There
4850 : : may be zero incoming edges if the THEN block didn't actually join
4851 : : back up (as with a call to a non-return function). */
4852 : 0 : else if (EDGE_COUNT (join_bb->preds) < 2
4853 : 0 : && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4854 : : {
4855 : : /* We can merge the JOIN cleanly and update the dataflow try
4856 : : again on this pass.*/
4857 : 0 : merge_blocks (combo_bb, join_bb);
4858 : 0 : num_true_changes++;
4859 : : }
4860 : : else
4861 : : {
4862 : : /* We cannot merge the JOIN. */
4863 : :
4864 : : /* The outgoing edge for the current COMBO block should already
4865 : : be correct. Verify this. */
4866 : 0 : gcc_assert (single_succ_p (combo_bb)
4867 : : && single_succ (combo_bb) == join_bb);
4868 : :
4869 : : /* Remove the jump and cruft from the end of the COMBO block. */
4870 : 0 : if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4871 : 0 : tidy_fallthru_edge (single_succ_edge (combo_bb));
4872 : : }
4873 : :
4874 : 0 : num_updated_if_blocks++;
4875 : 0 : }
4876 : :
4877 : : /* Find a block ending in a simple IF condition and try to transform it
4878 : : in some way. When converting a multi-block condition, put the new code
4879 : : in the first such block and delete the rest. Return a pointer to this
4880 : : first block if some transformation was done. Return NULL otherwise. */
4881 : :
4882 : : static basic_block
4883 : 39542807 : find_if_header (basic_block test_bb, int pass)
4884 : : {
4885 : 39542807 : ce_if_block ce_info;
4886 : 39542807 : edge then_edge;
4887 : 39542807 : edge else_edge;
4888 : :
4889 : : /* The kind of block we're looking for has exactly two successors. */
4890 : 59513258 : if (EDGE_COUNT (test_bb->succs) != 2)
4891 : : return NULL;
4892 : :
4893 : 20314848 : then_edge = EDGE_SUCC (test_bb, 0);
4894 : 20314848 : else_edge = EDGE_SUCC (test_bb, 1);
4895 : :
4896 : 20314848 : if (df_get_bb_dirty (then_edge->dest))
4897 : : return NULL;
4898 : 20291697 : if (df_get_bb_dirty (else_edge->dest))
4899 : : return NULL;
4900 : :
4901 : : /* Neither edge should be abnormal. */
4902 : 20243493 : if ((then_edge->flags & EDGE_COMPLEX)
4903 : 18436382 : || (else_edge->flags & EDGE_COMPLEX))
4904 : : return NULL;
4905 : :
4906 : : /* Nor exit the loop. */
4907 : 18010952 : if ((then_edge->flags & EDGE_LOOP_EXIT)
4908 : 16476634 : || (else_edge->flags & EDGE_LOOP_EXIT))
4909 : : return NULL;
4910 : :
4911 : : /* The THEN edge is canonically the one that falls through. */
4912 : 14240820 : if (then_edge->flags & EDGE_FALLTHRU)
4913 : : ;
4914 : 6907216 : else if (else_edge->flags & EDGE_FALLTHRU)
4915 : : std::swap (then_edge, else_edge);
4916 : : else
4917 : : /* Otherwise this must be a multiway branch of some sort. */
4918 : : return NULL;
4919 : :
4920 : 14240611 : memset (&ce_info, 0, sizeof (ce_info));
4921 : 14240611 : ce_info.test_bb = test_bb;
4922 : 14240611 : ce_info.then_bb = then_edge->dest;
4923 : 14240611 : ce_info.else_bb = else_edge->dest;
4924 : 14240611 : ce_info.pass = pass;
4925 : :
4926 : : #ifdef IFCVT_MACHDEP_INIT
4927 : : IFCVT_MACHDEP_INIT (&ce_info);
4928 : : #endif
4929 : :
4930 : 14240611 : if (!reload_completed
4931 : 14240611 : && noce_find_if_block (test_bb, then_edge, else_edge, pass))
4932 : 137857 : goto success;
4933 : :
4934 : 14102754 : if (reload_completed
4935 : 4512710 : && targetm.have_conditional_execution ()
4936 : 14102754 : && cond_exec_find_if_block (&ce_info))
4937 : 0 : goto success;
4938 : :
4939 : 14102754 : if (targetm.have_trap ()
4940 : 14102754 : && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
4941 : 14102754 : && find_cond_trap (test_bb, then_edge, else_edge))
4942 : 0 : goto success;
4943 : :
4944 : 14102754 : if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
4945 : 14102754 : && (reload_completed || !targetm.have_conditional_execution ()))
4946 : : {
4947 : 14102754 : if (find_if_case_1 (test_bb, then_edge, else_edge))
4948 : 19681 : goto success;
4949 : 14083073 : if (find_if_case_2 (test_bb, then_edge, else_edge))
4950 : 186859 : goto success;
4951 : : }
4952 : :
4953 : : return NULL;
4954 : :
4955 : 344397 : success:
4956 : 344397 : if (dump_file)
4957 : 21 : fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
4958 : : /* Set this so we continue looking. */
4959 : 344397 : cond_exec_changed_p = true;
4960 : 344397 : return ce_info.test_bb;
4961 : : }
4962 : :
4963 : : /* Return true if a block has two edges, one of which falls through to the next
4964 : : block, and the other jumps to a specific block, so that we can tell if the
4965 : : block is part of an && test or an || test. Returns either -1 or the number
4966 : : of non-note, non-jump, non-USE/CLOBBER insns in the block. */
4967 : :
4968 : : static int
4969 : 0 : block_jumps_and_fallthru (basic_block cur_bb, basic_block target_bb)
4970 : : {
4971 : 0 : edge cur_edge;
4972 : 0 : bool fallthru_p = false;
4973 : 0 : bool jump_p = false;
4974 : 0 : rtx_insn *insn;
4975 : 0 : rtx_insn *end;
4976 : 0 : int n_insns = 0;
4977 : 0 : edge_iterator ei;
4978 : :
4979 : 0 : if (!cur_bb || !target_bb)
4980 : : return -1;
4981 : :
4982 : : /* If no edges, obviously it doesn't jump or fallthru. */
4983 : 0 : if (EDGE_COUNT (cur_bb->succs) == 0)
4984 : : return 0;
4985 : :
4986 : 0 : FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
4987 : : {
4988 : 0 : if (cur_edge->flags & EDGE_COMPLEX)
4989 : : /* Anything complex isn't what we want. */
4990 : : return -1;
4991 : :
4992 : 0 : else if (cur_edge->flags & EDGE_FALLTHRU)
4993 : : fallthru_p = true;
4994 : :
4995 : 0 : else if (cur_edge->dest == target_bb)
4996 : : jump_p = true;
4997 : :
4998 : : else
4999 : : return -1;
5000 : : }
5001 : :
5002 : 0 : if ((jump_p & fallthru_p) == 0)
5003 : : return -1;
5004 : :
5005 : : /* Don't allow calls in the block, since this is used to group && and ||
5006 : : together for conditional execution support. ??? we should support
5007 : : conditional execution support across calls for IA-64 some day, but
5008 : : for now it makes the code simpler. */
5009 : 0 : end = BB_END (cur_bb);
5010 : 0 : insn = BB_HEAD (cur_bb);
5011 : :
5012 : 0 : while (insn != NULL_RTX)
5013 : : {
5014 : 0 : if (CALL_P (insn))
5015 : : return -1;
5016 : :
5017 : 0 : if (INSN_P (insn)
5018 : 0 : && !JUMP_P (insn)
5019 : 0 : && !DEBUG_INSN_P (insn)
5020 : 0 : && GET_CODE (PATTERN (insn)) != USE
5021 : 0 : && GET_CODE (PATTERN (insn)) != CLOBBER)
5022 : 0 : n_insns++;
5023 : :
5024 : 0 : if (insn == end)
5025 : : break;
5026 : :
5027 : 0 : insn = NEXT_INSN (insn);
5028 : : }
5029 : :
5030 : : return n_insns;
5031 : : }
5032 : :
5033 : : /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
5034 : : block. If so, we'll try to convert the insns to not require the branch.
5035 : : Return TRUE if we were successful at converting the block. */
5036 : :
5037 : : static bool
5038 : 0 : cond_exec_find_if_block (struct ce_if_block * ce_info)
5039 : : {
5040 : 0 : basic_block test_bb = ce_info->test_bb;
5041 : 0 : basic_block then_bb = ce_info->then_bb;
5042 : 0 : basic_block else_bb = ce_info->else_bb;
5043 : 0 : basic_block join_bb = NULL_BLOCK;
5044 : 0 : edge cur_edge;
5045 : 0 : basic_block next;
5046 : 0 : edge_iterator ei;
5047 : :
5048 : 0 : ce_info->last_test_bb = test_bb;
5049 : :
5050 : : /* We only ever should get here after reload,
5051 : : and if we have conditional execution. */
5052 : 0 : gcc_assert (reload_completed && targetm.have_conditional_execution ());
5053 : :
5054 : : /* Discover if any fall through predecessors of the current test basic block
5055 : : were && tests (which jump to the else block) or || tests (which jump to
5056 : : the then block). */
5057 : 0 : if (single_pred_p (test_bb)
5058 : 0 : && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
5059 : : {
5060 : 0 : basic_block bb = single_pred (test_bb);
5061 : 0 : basic_block target_bb;
5062 : 0 : int max_insns = MAX_CONDITIONAL_EXECUTE;
5063 : 0 : int n_insns;
5064 : :
5065 : : /* Determine if the preceding block is an && or || block. */
5066 : 0 : if ((n_insns = block_jumps_and_fallthru (bb, else_bb)) >= 0)
5067 : : {
5068 : 0 : ce_info->and_and_p = true;
5069 : 0 : target_bb = else_bb;
5070 : : }
5071 : 0 : else if ((n_insns = block_jumps_and_fallthru (bb, then_bb)) >= 0)
5072 : : {
5073 : 0 : ce_info->and_and_p = false;
5074 : 0 : target_bb = then_bb;
5075 : : }
5076 : : else
5077 : : target_bb = NULL_BLOCK;
5078 : :
5079 : 0 : if (target_bb && n_insns <= max_insns)
5080 : : {
5081 : 0 : int total_insns = 0;
5082 : 0 : int blocks = 0;
5083 : :
5084 : 0 : ce_info->last_test_bb = test_bb;
5085 : :
5086 : : /* Found at least one && or || block, look for more. */
5087 : 0 : do
5088 : : {
5089 : 0 : ce_info->test_bb = test_bb = bb;
5090 : 0 : total_insns += n_insns;
5091 : 0 : blocks++;
5092 : :
5093 : 0 : if (!single_pred_p (bb))
5094 : : break;
5095 : :
5096 : 0 : bb = single_pred (bb);
5097 : 0 : n_insns = block_jumps_and_fallthru (bb, target_bb);
5098 : : }
5099 : 0 : while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
5100 : :
5101 : 0 : ce_info->num_multiple_test_blocks = blocks;
5102 : 0 : ce_info->num_multiple_test_insns = total_insns;
5103 : :
5104 : 0 : if (ce_info->and_and_p)
5105 : 0 : ce_info->num_and_and_blocks = blocks;
5106 : : else
5107 : 0 : ce_info->num_or_or_blocks = blocks;
5108 : : }
5109 : : }
5110 : :
5111 : : /* The THEN block of an IF-THEN combo must have exactly one predecessor,
5112 : : other than any || blocks which jump to the THEN block. */
5113 : 0 : if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
5114 : : return false;
5115 : :
5116 : : /* The edges of the THEN and ELSE blocks cannot have complex edges. */
5117 : 0 : FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
5118 : : {
5119 : 0 : if (cur_edge->flags & EDGE_COMPLEX)
5120 : : return false;
5121 : : }
5122 : :
5123 : 0 : FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
5124 : : {
5125 : 0 : if (cur_edge->flags & EDGE_COMPLEX)
5126 : : return false;
5127 : : }
5128 : :
5129 : : /* The THEN block of an IF-THEN combo must have zero or one successors. */
5130 : 0 : if (EDGE_COUNT (then_bb->succs) > 0
5131 : 0 : && (!single_succ_p (then_bb)
5132 : 0 : || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
5133 : 0 : || (epilogue_completed
5134 : 0 : && tablejump_p (BB_END (then_bb), NULL, NULL))))
5135 : 0 : return false;
5136 : :
5137 : : /* If the THEN block has no successors, conditional execution can still
5138 : : make a conditional call. Don't do this unless the ELSE block has
5139 : : only one incoming edge -- the CFG manipulation is too ugly otherwise.
5140 : : Check for the last insn of the THEN block being an indirect jump, which
5141 : : is listed as not having any successors, but confuses the rest of the CE
5142 : : code processing. ??? we should fix this in the future. */
5143 : 0 : if (EDGE_COUNT (then_bb->succs) == 0)
5144 : : {
5145 : 0 : if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5146 : : {
5147 : 0 : rtx_insn *last_insn = BB_END (then_bb);
5148 : :
5149 : 0 : while (last_insn
5150 : 0 : && NOTE_P (last_insn)
5151 : 0 : && last_insn != BB_HEAD (then_bb))
5152 : 0 : last_insn = PREV_INSN (last_insn);
5153 : :
5154 : 0 : if (last_insn
5155 : 0 : && JUMP_P (last_insn)
5156 : 0 : && ! simplejump_p (last_insn))
5157 : : return false;
5158 : :
5159 : : join_bb = else_bb;
5160 : : else_bb = NULL_BLOCK;
5161 : : }
5162 : : else
5163 : : return false;
5164 : : }
5165 : :
5166 : : /* If the THEN block's successor is the other edge out of the TEST block,
5167 : : then we have an IF-THEN combo without an ELSE. */
5168 : 0 : else if (single_succ (then_bb) == else_bb)
5169 : : {
5170 : : join_bb = else_bb;
5171 : : else_bb = NULL_BLOCK;
5172 : : }
5173 : :
5174 : : /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
5175 : : has exactly one predecessor and one successor, and the outgoing edge
5176 : : is not complex, then we have an IF-THEN-ELSE combo. */
5177 : 0 : else if (single_succ_p (else_bb)
5178 : 0 : && single_succ (then_bb) == single_succ (else_bb)
5179 : 0 : && single_pred_p (else_bb)
5180 : 0 : && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
5181 : 0 : && !(epilogue_completed
5182 : 0 : && tablejump_p (BB_END (else_bb), NULL, NULL)))
5183 : 0 : join_bb = single_succ (else_bb);
5184 : :
5185 : : /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
5186 : : else
5187 : 0 : return false;
5188 : :
5189 : 0 : num_possible_if_blocks++;
5190 : :
5191 : 0 : if (dump_file)
5192 : : {
5193 : 0 : fprintf (dump_file,
5194 : : "\nIF-THEN%s block found, pass %d, start block %d "
5195 : : "[insn %d], then %d [%d]",
5196 : : (else_bb) ? "-ELSE" : "",
5197 : : ce_info->pass,
5198 : : test_bb->index,
5199 : 0 : BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
5200 : : then_bb->index,
5201 : 0 : BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
5202 : :
5203 : 0 : if (else_bb)
5204 : 0 : fprintf (dump_file, ", else %d [%d]",
5205 : : else_bb->index,
5206 : 0 : BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
5207 : :
5208 : 0 : fprintf (dump_file, ", join %d [%d]",
5209 : : join_bb->index,
5210 : 0 : BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
5211 : :
5212 : 0 : if (ce_info->num_multiple_test_blocks > 0)
5213 : 0 : fprintf (dump_file, ", %d %s block%s last test %d [%d]",
5214 : : ce_info->num_multiple_test_blocks,
5215 : 0 : (ce_info->and_and_p) ? "&&" : "||",
5216 : : (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
5217 : : ce_info->last_test_bb->index,
5218 : 0 : ((BB_HEAD (ce_info->last_test_bb))
5219 : 0 : ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
5220 : : : -1));
5221 : :
5222 : 0 : fputc ('\n', dump_file);
5223 : : }
5224 : :
5225 : : /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
5226 : : first condition for free, since we've already asserted that there's a
5227 : : fallthru edge from IF to THEN. Likewise for the && and || blocks, since
5228 : : we checked the FALLTHRU flag, those are already adjacent to the last IF
5229 : : block. */
5230 : : /* ??? As an enhancement, move the ELSE block. Have to deal with
5231 : : BLOCK notes, if by no other means than backing out the merge if they
5232 : : exist. Sticky enough I don't want to think about it now. */
5233 : 0 : next = then_bb;
5234 : 0 : if (else_bb && (next = next->next_bb) != else_bb)
5235 : : return false;
5236 : 0 : if ((next = next->next_bb) != join_bb
5237 : 0 : && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5238 : : {
5239 : 0 : if (else_bb)
5240 : : join_bb = NULL;
5241 : : else
5242 : : return false;
5243 : : }
5244 : :
5245 : : /* Do the real work. */
5246 : :
5247 : 0 : ce_info->else_bb = else_bb;
5248 : 0 : ce_info->join_bb = join_bb;
5249 : :
5250 : : /* If we have && and || tests, try to first handle combining the && and ||
5251 : : tests into the conditional code, and if that fails, go back and handle
5252 : : it without the && and ||, which at present handles the && case if there
5253 : : was no ELSE block. */
5254 : 0 : if (cond_exec_process_if_block (ce_info, true))
5255 : : return true;
5256 : :
5257 : 0 : if (ce_info->num_multiple_test_blocks)
5258 : : {
5259 : 0 : cancel_changes (0);
5260 : :
5261 : 0 : if (cond_exec_process_if_block (ce_info, false))
5262 : : return true;
5263 : : }
5264 : :
5265 : : return false;
5266 : : }
5267 : :
5268 : : /* Convert a branch over a trap, or a branch
5269 : : to a trap, into a conditional trap. */
5270 : :
5271 : : static bool
5272 : 0 : find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
5273 : : {
5274 : 0 : basic_block then_bb = then_edge->dest;
5275 : 0 : basic_block else_bb = else_edge->dest;
5276 : 0 : basic_block other_bb, trap_bb;
5277 : 0 : rtx_insn *trap, *jump;
5278 : 0 : rtx cond;
5279 : 0 : rtx_insn *cond_earliest;
5280 : :
5281 : : /* Locate the block with the trap instruction. */
5282 : : /* ??? While we look for no successors, we really ought to allow
5283 : : EH successors. Need to fix merge_if_block for that to work. */
5284 : 0 : if ((trap = block_has_only_trap (then_bb)) != NULL)
5285 : : trap_bb = then_bb, other_bb = else_bb;
5286 : 0 : else if ((trap = block_has_only_trap (else_bb)) != NULL)
5287 : : trap_bb = else_bb, other_bb = then_bb;
5288 : : else
5289 : : return false;
5290 : :
5291 : 0 : if (dump_file)
5292 : : {
5293 : 0 : fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
5294 : : test_bb->index, trap_bb->index);
5295 : : }
5296 : :
5297 : : /* If this is not a standard conditional jump, we can't parse it. */
5298 : 0 : jump = BB_END (test_bb);
5299 : 0 : cond = noce_get_condition (jump, &cond_earliest, then_bb == trap_bb);
5300 : 0 : if (! cond)
5301 : : return false;
5302 : :
5303 : : /* If the conditional jump is more than just a conditional jump, then
5304 : : we cannot do if-conversion on this block. Give up for returnjump_p,
5305 : : changing a conditional return followed by unconditional trap for
5306 : : conditional trap followed by unconditional return is likely not
5307 : : beneficial and harder to handle. */
5308 : 0 : if (! onlyjump_p (jump) || returnjump_p (jump))
5309 : 0 : return false;
5310 : :
5311 : : /* We must be comparing objects whose modes imply the size. */
5312 : 0 : if (GET_MODE (XEXP (cond, 0)) == BLKmode)
5313 : : return false;
5314 : :
5315 : : /* Attempt to generate the conditional trap. */
5316 : 0 : rtx_insn *seq = gen_cond_trap (GET_CODE (cond), copy_rtx (XEXP (cond, 0)),
5317 : : copy_rtx (XEXP (cond, 1)),
5318 : 0 : TRAP_CODE (PATTERN (trap)));
5319 : 0 : if (seq == NULL)
5320 : : return false;
5321 : :
5322 : : /* If that results in an invalid insn, back out. */
5323 : 0 : for (rtx_insn *x = seq; x; x = NEXT_INSN (x))
5324 : 0 : if (reload_completed
5325 : 0 : ? !valid_insn_p (x)
5326 : 0 : : recog_memoized (x) < 0)
5327 : : return false;
5328 : :
5329 : : /* Emit the new insns before cond_earliest. */
5330 : 0 : emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
5331 : :
5332 : : /* Delete the trap block if possible. */
5333 : 0 : remove_edge (trap_bb == then_bb ? then_edge : else_edge);
5334 : 0 : df_set_bb_dirty (test_bb);
5335 : 0 : df_set_bb_dirty (then_bb);
5336 : 0 : df_set_bb_dirty (else_bb);
5337 : :
5338 : 0 : if (EDGE_COUNT (trap_bb->preds) == 0)
5339 : : {
5340 : 0 : delete_basic_block (trap_bb);
5341 : 0 : num_true_changes++;
5342 : : }
5343 : :
5344 : : /* Wire together the blocks again. */
5345 : 0 : if (current_ir_type () == IR_RTL_CFGLAYOUT)
5346 : 0 : single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
5347 : 0 : else if (trap_bb == then_bb)
5348 : : {
5349 : 0 : rtx lab = JUMP_LABEL (jump);
5350 : 0 : rtx_insn *seq = targetm.gen_jump (lab);
5351 : 0 : rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
5352 : 0 : LABEL_NUSES (lab) += 1;
5353 : 0 : JUMP_LABEL (newjump) = lab;
5354 : 0 : emit_barrier_after (newjump);
5355 : : }
5356 : 0 : delete_insn (jump);
5357 : :
5358 : 0 : if (can_merge_blocks_p (test_bb, other_bb))
5359 : : {
5360 : 0 : merge_blocks (test_bb, other_bb);
5361 : 0 : num_true_changes++;
5362 : : }
5363 : :
5364 : 0 : num_updated_if_blocks++;
5365 : 0 : return true;
5366 : : }
5367 : :
5368 : : /* Subroutine of find_cond_trap: if BB contains only a trap insn,
5369 : : return it. */
5370 : :
5371 : : static rtx_insn *
5372 : 0 : block_has_only_trap (basic_block bb)
5373 : : {
5374 : 0 : rtx_insn *trap;
5375 : :
5376 : : /* We're not the exit block. */
5377 : 0 : if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5378 : : return NULL;
5379 : :
5380 : : /* The block must have no successors. */
5381 : 0 : if (EDGE_COUNT (bb->succs) > 0)
5382 : : return NULL;
5383 : :
5384 : : /* The only instruction in the THEN block must be the trap. */
5385 : 0 : trap = first_active_insn (bb);
5386 : 0 : if (! (trap == BB_END (bb)
5387 : 0 : && GET_CODE (PATTERN (trap)) == TRAP_IF
5388 : 0 : && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
5389 : 0 : return NULL;
5390 : :
5391 : : return trap;
5392 : : }
5393 : :
5394 : : /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
5395 : : transformable, but not necessarily the other. There need be no
5396 : : JOIN block.
5397 : :
5398 : : Return TRUE if we were successful at converting the block.
5399 : :
5400 : : Cases we'd like to look at:
5401 : :
5402 : : (1)
5403 : : if (test) goto over; // x not live
5404 : : x = a;
5405 : : goto label;
5406 : : over:
5407 : :
5408 : : becomes
5409 : :
5410 : : x = a;
5411 : : if (! test) goto label;
5412 : :
5413 : : (2)
5414 : : if (test) goto E; // x not live
5415 : : x = big();
5416 : : goto L;
5417 : : E:
5418 : : x = b;
5419 : : goto M;
5420 : :
5421 : : becomes
5422 : :
5423 : : x = b;
5424 : : if (test) goto M;
5425 : : x = big();
5426 : : goto L;
5427 : :
5428 : : (3) // This one's really only interesting for targets that can do
5429 : : // multiway branching, e.g. IA-64 BBB bundles. For other targets
5430 : : // it results in multiple branches on a cache line, which often
5431 : : // does not sit well with predictors.
5432 : :
5433 : : if (test1) goto E; // predicted not taken
5434 : : x = a;
5435 : : if (test2) goto F;
5436 : : ...
5437 : : E:
5438 : : x = b;
5439 : : J:
5440 : :
5441 : : becomes
5442 : :
5443 : : x = a;
5444 : : if (test1) goto E;
5445 : : if (test2) goto F;
5446 : :
5447 : : Notes:
5448 : :
5449 : : (A) Don't do (2) if the branch is predicted against the block we're
5450 : : eliminating. Do it anyway if we can eliminate a branch; this requires
5451 : : that the sole successor of the eliminated block postdominate the other
5452 : : side of the if.
5453 : :
5454 : : (B) With CE, on (3) we can steal from both sides of the if, creating
5455 : :
5456 : : if (test1) x = a;
5457 : : if (!test1) x = b;
5458 : : if (test1) goto J;
5459 : : if (test2) goto F;
5460 : : ...
5461 : : J:
5462 : :
5463 : : Again, this is most useful if J postdominates.
5464 : :
5465 : : (C) CE substitutes for helpful life information.
5466 : :
5467 : : (D) These heuristics need a lot of work. */
5468 : :
5469 : : /* Tests for case 1 above. */
5470 : :
5471 : : static bool
5472 : 14102754 : find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
5473 : : {
5474 : 14102754 : basic_block then_bb = then_edge->dest;
5475 : 14102754 : basic_block else_bb = else_edge->dest;
5476 : 14102754 : basic_block new_bb;
5477 : 14102754 : int then_bb_index;
5478 : 14102754 : profile_probability then_prob;
5479 : 14102754 : rtx else_target = NULL_RTX;
5480 : :
5481 : : /* If we are partitioning hot/cold basic blocks, we don't want to
5482 : : mess up unconditional or indirect jumps that cross between hot
5483 : : and cold sections.
5484 : :
5485 : : Basic block partitioning may result in some jumps that appear to
5486 : : be optimizable (or blocks that appear to be mergeable), but which really
5487 : : must be left untouched (they are required to make it safely across
5488 : : partition boundaries). See the comments at the top of
5489 : : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
5490 : :
5491 : 14102754 : if ((BB_END (then_bb)
5492 : 14102754 : && JUMP_P (BB_END (then_bb))
5493 : 7408056 : && CROSSING_JUMP_P (BB_END (then_bb)))
5494 : 13879159 : || (JUMP_P (BB_END (test_bb))
5495 : 13879159 : && CROSSING_JUMP_P (BB_END (test_bb)))
5496 : 27882816 : || (BB_END (else_bb)
5497 : 13780062 : && JUMP_P (BB_END (else_bb))
5498 : 6857225 : && CROSSING_JUMP_P (BB_END (else_bb))))
5499 : : return false;
5500 : :
5501 : : /* Verify test_bb ends in a conditional jump with no other side-effects. */
5502 : 13756138 : if (!onlyjump_p (BB_END (test_bb)))
5503 : : return false;
5504 : :
5505 : : /* THEN has one successor. */
5506 : 20289946 : if (!single_succ_p (then_bb))
5507 : : return false;
5508 : :
5509 : : /* THEN does not fall through, but is not strange either. */
5510 : 6206873 : if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
5511 : : return false;
5512 : :
5513 : : /* THEN has one predecessor. */
5514 : 15210314 : if (!single_pred_p (then_bb))
5515 : : return false;
5516 : :
5517 : : /* THEN must do something. */
5518 : 1127241 : if (forwarder_block_p (then_bb))
5519 : : return false;
5520 : :
5521 : 687623 : num_possible_if_blocks++;
5522 : 687623 : if (dump_file)
5523 : 12 : fprintf (dump_file,
5524 : : "\nIF-CASE-1 found, start %d, then %d\n",
5525 : : test_bb->index, then_bb->index);
5526 : :
5527 : 687623 : then_prob = then_edge->probability.invert ();
5528 : :
5529 : : /* We're speculating from the THEN path, we want to make sure the cost
5530 : : of speculation is within reason. */
5531 : 1303791 : if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
5532 : 1303791 : COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
5533 : : predictable_edge_p (then_edge)))))
5534 : : return false;
5535 : :
5536 : 138098 : if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5537 : : {
5538 : 0 : rtx_insn *jump = BB_END (else_edge->src);
5539 : 0 : gcc_assert (JUMP_P (jump));
5540 : 0 : else_target = JUMP_LABEL (jump);
5541 : : }
5542 : :
5543 : : /* Registers set are dead, or are predicable. */
5544 : 138098 : if (! dead_or_predicable (test_bb, then_bb, else_bb,
5545 : : single_succ_edge (then_bb), true))
5546 : : return false;
5547 : :
5548 : : /* Conversion went ok, including moving the insns and fixing up the
5549 : : jump. Adjust the CFG to match. */
5550 : :
5551 : : /* We can avoid creating a new basic block if then_bb is immediately
5552 : : followed by else_bb, i.e. deleting then_bb allows test_bb to fall
5553 : : through to else_bb. */
5554 : :
5555 : 19681 : if (then_bb->next_bb == else_bb
5556 : 8025 : && then_bb->prev_bb == test_bb
5557 : 8025 : && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5558 : : {
5559 : 8025 : redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
5560 : 8025 : new_bb = 0;
5561 : : }
5562 : 11656 : else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5563 : 0 : new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
5564 : : else_bb, else_target);
5565 : : else
5566 : 11656 : new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
5567 : : else_bb);
5568 : :
5569 : 19681 : df_set_bb_dirty (test_bb);
5570 : 19681 : df_set_bb_dirty (else_bb);
5571 : :
5572 : 19681 : then_bb_index = then_bb->index;
5573 : 19681 : delete_basic_block (then_bb);
5574 : :
5575 : : /* Make rest of code believe that the newly created block is the THEN_BB
5576 : : block we removed. */
5577 : 19681 : if (new_bb)
5578 : : {
5579 : 11656 : df_bb_replace (then_bb_index, new_bb);
5580 : : /* This should have been done above via force_nonfallthru_and_redirect
5581 : : (possibly called from redirect_edge_and_branch_force). */
5582 : 11656 : gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
5583 : : }
5584 : :
5585 : 19681 : num_true_changes++;
5586 : 19681 : num_updated_if_blocks++;
5587 : 19681 : return true;
5588 : : }
5589 : :
5590 : : /* Test for case 2 above. */
5591 : :
5592 : : static bool
5593 : 14083073 : find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
5594 : : {
5595 : 14083073 : basic_block then_bb = then_edge->dest;
5596 : 14083073 : basic_block else_bb = else_edge->dest;
5597 : 14083073 : edge else_succ;
5598 : 14083073 : profile_probability then_prob, else_prob;
5599 : :
5600 : : /* We do not want to speculate (empty) loop latches. */
5601 : 14083073 : if (current_loops
5602 : 5608585 : && else_bb->loop_father->latch == else_bb)
5603 : : return false;
5604 : :
5605 : : /* If we are partitioning hot/cold basic blocks, we don't want to
5606 : : mess up unconditional or indirect jumps that cross between hot
5607 : : and cold sections.
5608 : :
5609 : : Basic block partitioning may result in some jumps that appear to
5610 : : be optimizable (or blocks that appear to be mergeable), but which really
5611 : : must be left untouched (they are required to make it safely across
5612 : : partition boundaries). See the comments at the top of
5613 : : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
5614 : :
5615 : 13924608 : if ((BB_END (then_bb)
5616 : 13924608 : && JUMP_P (BB_END (then_bb))
5617 : 7312111 : && CROSSING_JUMP_P (BB_END (then_bb)))
5618 : 13701013 : || (JUMP_P (BB_END (test_bb))
5619 : 13701013 : && CROSSING_JUMP_P (BB_END (test_bb)))
5620 : 27526524 : || (BB_END (else_bb)
5621 : 13601916 : && JUMP_P (BB_END (else_bb))
5622 : 6709071 : && CROSSING_JUMP_P (BB_END (else_bb))))
5623 : : return false;
5624 : :
5625 : : /* Verify test_bb ends in a conditional jump with no other side-effects. */
5626 : 13577992 : if (!onlyjump_p (BB_END (test_bb)))
5627 : : return false;
5628 : :
5629 : : /* ELSE has one successor. */
5630 : 19707421 : if (!single_succ_p (else_bb))
5631 : : return false;
5632 : : else
5633 : 5811207 : else_succ = single_succ_edge (else_bb);
5634 : :
5635 : : /* ELSE outgoing edge is not complex. */
5636 : 5811207 : if (else_succ->flags & EDGE_COMPLEX)
5637 : : return false;
5638 : :
5639 : : /* ELSE has one predecessor. */
5640 : 17002297 : if (!single_pred_p (else_bb))
5641 : : return false;
5642 : :
5643 : : /* THEN is not EXIT. */
5644 : 3106083 : if (then_bb->index < NUM_FIXED_BLOCKS)
5645 : : return false;
5646 : :
5647 : 3106083 : else_prob = else_edge->probability;
5648 : 3106083 : then_prob = else_prob.invert ();
5649 : :
5650 : : /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
5651 : 3106083 : if (else_prob > then_prob)
5652 : : ;
5653 : 2098737 : else if (else_succ->dest->index < NUM_FIXED_BLOCKS
5654 : 2098737 : || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
5655 : : else_succ->dest))
5656 : : ;
5657 : : else
5658 : : return false;
5659 : :
5660 : 2348444 : num_possible_if_blocks++;
5661 : 2348444 : if (dump_file)
5662 : 46 : fprintf (dump_file,
5663 : : "\nIF-CASE-2 found, start %d, else %d\n",
5664 : : test_bb->index, else_bb->index);
5665 : :
5666 : : /* We're speculating from the ELSE path, we want to make sure the cost
5667 : : of speculation is within reason. */
5668 : 4490823 : if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
5669 : 4490823 : COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
5670 : : predictable_edge_p (else_edge)))))
5671 : : return false;
5672 : :
5673 : : /* Registers set are dead, or are predicable. */
5674 : 488845 : if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, false))
5675 : : return false;
5676 : :
5677 : : /* Conversion went ok, including moving the insns and fixing up the
5678 : : jump. Adjust the CFG to match. */
5679 : :
5680 : 186859 : df_set_bb_dirty (test_bb);
5681 : 186859 : df_set_bb_dirty (then_bb);
5682 : 186859 : delete_basic_block (else_bb);
5683 : :
5684 : 186859 : num_true_changes++;
5685 : 186859 : num_updated_if_blocks++;
5686 : :
5687 : : /* ??? We may now fallthru from one of THEN's successors into a join
5688 : : block. Rerun cleanup_cfg? Examine things manually? Wait? */
5689 : :
5690 : 186859 : return true;
5691 : : }
5692 : :
5693 : : /* Used by the code above to perform the actual rtl transformations.
5694 : : Return TRUE if successful.
5695 : :
5696 : : TEST_BB is the block containing the conditional branch. MERGE_BB
5697 : : is the block containing the code to manipulate. DEST_EDGE is an
5698 : : edge representing a jump to the join block; after the conversion,
5699 : : TEST_BB should be branching to its destination.
5700 : : REVERSEP is true if the sense of the branch should be reversed. */
5701 : :
5702 : : static bool
5703 : 626943 : dead_or_predicable (basic_block test_bb, basic_block merge_bb,
5704 : : basic_block other_bb, edge dest_edge, bool reversep)
5705 : : {
5706 : 626943 : basic_block new_dest = dest_edge->dest;
5707 : 626943 : rtx_insn *head, *end, *jump;
5708 : 626943 : rtx_insn *earliest = NULL;
5709 : 626943 : rtx old_dest;
5710 : 626943 : bitmap merge_set = NULL;
5711 : : /* Number of pending changes. */
5712 : 626943 : int n_validated_changes = 0;
5713 : 626943 : rtx new_dest_label = NULL_RTX;
5714 : :
5715 : 626943 : jump = BB_END (test_bb);
5716 : :
5717 : : /* Find the extent of the real code in the merge block. */
5718 : 626943 : head = BB_HEAD (merge_bb);
5719 : 626943 : end = BB_END (merge_bb);
5720 : :
5721 : 746380 : while (DEBUG_INSN_P (end) && end != head)
5722 : 119437 : end = PREV_INSN (end);
5723 : :
5724 : : /* If merge_bb ends with a tablejump, predicating/moving insn's
5725 : : into test_bb and then deleting merge_bb will result in the jumptable
5726 : : that follows merge_bb being removed along with merge_bb and then we
5727 : : get an unresolved reference to the jumptable. */
5728 : 626943 : if (tablejump_p (end, NULL, NULL))
5729 : : return false;
5730 : :
5731 : 626943 : if (LABEL_P (head))
5732 : 488867 : head = NEXT_INSN (head);
5733 : 626943 : while (DEBUG_INSN_P (head) && head != end)
5734 : 0 : head = NEXT_INSN (head);
5735 : 626943 : if (NOTE_P (head))
5736 : : {
5737 : 626943 : if (head == end)
5738 : : {
5739 : 99372 : head = end = NULL;
5740 : 99372 : goto no_body;
5741 : : }
5742 : 527571 : head = NEXT_INSN (head);
5743 : 1333178 : while (DEBUG_INSN_P (head) && head != end)
5744 : 278036 : head = NEXT_INSN (head);
5745 : : }
5746 : :
5747 : 527571 : if (JUMP_P (end))
5748 : : {
5749 : 190738 : if (!onlyjump_p (end))
5750 : : return false;
5751 : 150371 : if (head == end)
5752 : : {
5753 : 0 : head = end = NULL;
5754 : 0 : goto no_body;
5755 : : }
5756 : 150371 : end = PREV_INSN (end);
5757 : 370573 : while (DEBUG_INSN_P (end) && end != head)
5758 : 69831 : end = PREV_INSN (end);
5759 : : }
5760 : :
5761 : : /* Don't move frame-related insn across the conditional branch. This
5762 : : can lead to one of the paths of the branch having wrong unwind info. */
5763 : 487204 : if (epilogue_completed)
5764 : : {
5765 : : rtx_insn *insn = head;
5766 : 58276 : while (1)
5767 : : {
5768 : 258925 : if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
5769 : : return false;
5770 : 250487 : if (insn == end)
5771 : : break;
5772 : 58276 : insn = NEXT_INSN (insn);
5773 : 58276 : }
5774 : : }
5775 : :
5776 : : /* Disable handling dead code by conditional execution if the machine needs
5777 : : to do anything funny with the tests, etc. */
5778 : : #ifndef IFCVT_MODIFY_TESTS
5779 : 478766 : if (targetm.have_conditional_execution ())
5780 : : {
5781 : : /* In the conditional execution case, we have things easy. We know
5782 : : the condition is reversible. We don't have to check life info
5783 : : because we're going to conditionally execute the code anyway.
5784 : : All that's left is making sure the insns involved can actually
5785 : : be predicated. */
5786 : :
5787 : 0 : rtx cond;
5788 : :
5789 : : /* If the conditional jump is more than just a conditional jump,
5790 : : then we cannot do conditional execution conversion on this block. */
5791 : 0 : if (!onlyjump_p (jump))
5792 : 0 : goto nce;
5793 : :
5794 : 0 : cond = cond_exec_get_condition (jump);
5795 : 0 : if (! cond)
5796 : 0 : goto nce;
5797 : :
5798 : 0 : rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
5799 : 0 : profile_probability prob_val
5800 : 0 : = (note ? profile_probability::from_reg_br_prob_note (XINT (note, 0))
5801 : 0 : : profile_probability::uninitialized ());
5802 : :
5803 : 0 : if (reversep)
5804 : : {
5805 : 0 : enum rtx_code rev = reversed_comparison_code (cond, jump);
5806 : 0 : if (rev == UNKNOWN)
5807 : 0 : return false;
5808 : 0 : cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
5809 : : XEXP (cond, 1));
5810 : 0 : prob_val = prob_val.invert ();
5811 : : }
5812 : :
5813 : 0 : if (cond_exec_process_insns (NULL, head, end, cond, prob_val, false)
5814 : 0 : && verify_changes (0))
5815 : 0 : n_validated_changes = num_validated_changes ();
5816 : : else
5817 : 0 : cancel_changes (0);
5818 : :
5819 : 0 : earliest = jump;
5820 : : }
5821 : 478766 : nce:
5822 : : #endif
5823 : :
5824 : : /* If we allocated new pseudos (e.g. in the conditional move
5825 : : expander called from noce_emit_cmove), we must resize the
5826 : : array first. */
5827 : 478766 : if (max_regno < max_reg_num ())
5828 : 89076 : max_regno = max_reg_num ();
5829 : :
5830 : : /* Try the NCE path if the CE path did not result in any changes. */
5831 : 478766 : if (n_validated_changes == 0)
5832 : : {
5833 : 478766 : rtx cond;
5834 : 478766 : rtx_insn *insn;
5835 : 478766 : regset live;
5836 : 478766 : bool success;
5837 : :
5838 : : /* In the non-conditional execution case, we have to verify that there
5839 : : are no trapping operations, no calls, no references to memory, and
5840 : : that any registers modified are dead at the branch site. */
5841 : :
5842 : 478766 : if (!any_condjump_p (jump))
5843 : : return false;
5844 : :
5845 : : /* Find the extent of the conditional. */
5846 : 478766 : cond = noce_get_condition (jump, &earliest, false);
5847 : 478766 : if (!cond)
5848 : : return false;
5849 : :
5850 : 478766 : live = BITMAP_ALLOC (®_obstack);
5851 : 478766 : simulate_backwards_to_point (merge_bb, live, end);
5852 : 478766 : success = can_move_insns_across (head, end, earliest, jump,
5853 : : merge_bb, live,
5854 : : df_get_live_in (other_bb), NULL);
5855 : 478766 : BITMAP_FREE (live);
5856 : 478766 : if (!success)
5857 : : return false;
5858 : :
5859 : : /* Collect the set of registers set in MERGE_BB. */
5860 : 202902 : merge_set = BITMAP_ALLOC (®_obstack);
5861 : :
5862 : 953141 : FOR_BB_INSNS (merge_bb, insn)
5863 : 750239 : if (NONDEBUG_INSN_P (insn))
5864 : 253823 : df_simulate_find_defs (insn, merge_set);
5865 : :
5866 : : /* If shrink-wrapping, disable this optimization when test_bb is
5867 : : the first basic block and merge_bb exits. The idea is to not
5868 : : move code setting up a return register as that may clobber a
5869 : : register used to pass function parameters, which then must be
5870 : : saved in caller-saved regs. A caller-saved reg requires the
5871 : : prologue, killing a shrink-wrap opportunity. */
5872 : 202901 : if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
5873 : 173280 : && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
5874 : 28133 : && single_succ_p (new_dest)
5875 : 21925 : && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
5876 : 223967 : && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
5877 : : {
5878 : 21065 : regset return_regs;
5879 : 21065 : unsigned int i;
5880 : :
5881 : 21065 : return_regs = BITMAP_ALLOC (®_obstack);
5882 : :
5883 : : /* Start off with the intersection of regs used to pass
5884 : : params and regs used to return values. */
5885 : 1980110 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5886 : 1937980 : if (FUNCTION_ARG_REGNO_P (i)
5887 : 1937980 : && targetm.calls.function_value_regno_p (i))
5888 : 114208 : bitmap_set_bit (return_regs, INCOMING_REGNO (i));
5889 : :
5890 : 21065 : bitmap_and_into (return_regs,
5891 : 21065 : df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5892 : 21065 : bitmap_and_into (return_regs,
5893 : 21065 : df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
5894 : 21065 : if (!bitmap_empty_p (return_regs))
5895 : : {
5896 : 5511 : FOR_BB_INSNS_REVERSE (new_dest, insn)
5897 : 4821 : if (NONDEBUG_INSN_P (insn))
5898 : : {
5899 : 2536 : df_ref def;
5900 : :
5901 : : /* If this insn sets any reg in return_regs, add all
5902 : : reg uses to the set of regs we're interested in. */
5903 : 2961 : FOR_EACH_INSN_DEF (def, insn)
5904 : 1660 : if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
5905 : : {
5906 : 1235 : df_simulate_uses (insn, return_regs);
5907 : 1235 : break;
5908 : : }
5909 : : }
5910 : 690 : if (bitmap_intersect_p (merge_set, return_regs))
5911 : : {
5912 : 670 : BITMAP_FREE (return_regs);
5913 : 670 : BITMAP_FREE (merge_set);
5914 : 670 : return false;
5915 : : }
5916 : : }
5917 : 20395 : BITMAP_FREE (return_regs);
5918 : : }
5919 : : }
5920 : :
5921 : 301604 : no_body:
5922 : : /* We don't want to use normal invert_jump or redirect_jump because
5923 : : we don't want to delete_insn called. Also, we want to do our own
5924 : : change group management. */
5925 : :
5926 : 301604 : old_dest = JUMP_LABEL (jump);
5927 : 301604 : if (other_bb != new_dest)
5928 : : {
5929 : 301575 : if (!any_condjump_p (jump))
5930 : 0 : goto cancel;
5931 : :
5932 : 301575 : if (JUMP_P (BB_END (dest_edge->src)))
5933 : 20681 : new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
5934 : 280894 : else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5935 : 95064 : new_dest_label = ret_rtx;
5936 : : else
5937 : 185830 : new_dest_label = block_label (new_dest);
5938 : :
5939 : 301575 : rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
5940 : 301575 : if (reversep
5941 : 301575 : ? ! invert_jump_1 (jump_insn, new_dest_label)
5942 : 281894 : : ! redirect_jump_1 (jump_insn, new_dest_label))
5943 : 0 : goto cancel;
5944 : : }
5945 : :
5946 : 301604 : if (verify_changes (n_validated_changes))
5947 : 206540 : confirm_change_group ();
5948 : : else
5949 : 95064 : goto cancel;
5950 : :
5951 : 206540 : if (other_bb != new_dest)
5952 : : {
5953 : 206511 : redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
5954 : : 0, reversep);
5955 : :
5956 : 206511 : redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
5957 : 206511 : if (reversep)
5958 : : {
5959 : 19681 : std::swap (BRANCH_EDGE (test_bb)->probability,
5960 : 19681 : FALLTHRU_EDGE (test_bb)->probability);
5961 : 19681 : update_br_prob_note (test_bb);
5962 : : }
5963 : : }
5964 : :
5965 : : /* Move the insns out of MERGE_BB to before the branch. */
5966 : 206540 : if (head != NULL)
5967 : : {
5968 : 202232 : rtx_insn *insn;
5969 : :
5970 : 202232 : if (end == BB_END (merge_bb))
5971 : 163778 : BB_END (merge_bb) = PREV_INSN (head);
5972 : :
5973 : : /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
5974 : : notes being moved might become invalid. */
5975 : : insn = head;
5976 : 240701 : do
5977 : : {
5978 : 240701 : rtx note;
5979 : :
5980 : 240701 : if (! INSN_P (insn))
5981 : 3341 : continue;
5982 : 237360 : note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5983 : 237360 : if (! note)
5984 : 225793 : continue;
5985 : 11567 : remove_note (insn, note);
5986 : 279170 : } while (insn != end && (insn = NEXT_INSN (insn)));
5987 : :
5988 : : /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
5989 : : notes referring to the registers being set might become invalid. */
5990 : 202232 : if (merge_set)
5991 : : {
5992 : 202232 : unsigned i;
5993 : 202232 : bitmap_iterator bi;
5994 : :
5995 : 453161 : EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
5996 : 250929 : remove_reg_equal_equiv_notes_for_regno (i);
5997 : :
5998 : 202232 : BITMAP_FREE (merge_set);
5999 : : }
6000 : :
6001 : 202232 : reorder_insns (head, end, PREV_INSN (earliest));
6002 : : }
6003 : :
6004 : : /* Remove the jump and edge if we can. */
6005 : 206540 : if (other_bb == new_dest)
6006 : : {
6007 : 29 : delete_insn (jump);
6008 : 29 : remove_edge (BRANCH_EDGE (test_bb));
6009 : : /* ??? Can't merge blocks here, as then_bb is still in use.
6010 : : At minimum, the merge will get done just before bb-reorder. */
6011 : : }
6012 : :
6013 : : return true;
6014 : :
6015 : 95064 : cancel:
6016 : 95064 : cancel_changes (0);
6017 : :
6018 : 95064 : if (merge_set)
6019 : 0 : BITMAP_FREE (merge_set);
6020 : :
6021 : : return false;
6022 : : }
6023 : :
6024 : : /* Main entry point for all if-conversion. AFTER_COMBINE is true if
6025 : : we are after combine pass. */
6026 : :
6027 : : static void
6028 : 3063375 : if_convert (bool after_combine)
6029 : : {
6030 : 3063375 : basic_block bb;
6031 : 3063375 : int pass;
6032 : :
6033 : 3063375 : if (optimize == 1)
6034 : : {
6035 : 219679 : df_live_add_problem ();
6036 : 219679 : df_live_set_all_dirty ();
6037 : : }
6038 : :
6039 : : /* Record whether we are after combine pass. */
6040 : 3063375 : ifcvt_after_combine = after_combine;
6041 : 3063375 : have_cbranchcc4 = (direct_optab_handler (cbranch_optab, CCmode)
6042 : 3063375 : != CODE_FOR_nothing);
6043 : 3063375 : num_possible_if_blocks = 0;
6044 : 3063375 : num_updated_if_blocks = 0;
6045 : 3063375 : num_true_changes = 0;
6046 : :
6047 : 3063375 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
6048 : 3063375 : mark_loop_exit_edges ();
6049 : 3063375 : loop_optimizer_finalize ();
6050 : 3063375 : free_dominance_info (CDI_DOMINATORS);
6051 : :
6052 : : /* Compute postdominators. */
6053 : 3063375 : calculate_dominance_info (CDI_POST_DOMINATORS);
6054 : :
6055 : 3063375 : df_set_flags (DF_LR_RUN_DCE);
6056 : :
6057 : : /* Go through each of the basic blocks looking for things to convert. If we
6058 : : have conditional execution, we make multiple passes to allow us to handle
6059 : : IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
6060 : 3063375 : pass = 0;
6061 : 3250280 : do
6062 : : {
6063 : 3250280 : df_analyze ();
6064 : : /* Only need to do dce on the first pass. */
6065 : 3250280 : df_clear_flags (DF_LR_RUN_DCE);
6066 : 3250280 : cond_exec_changed_p = false;
6067 : 3250280 : pass++;
6068 : :
6069 : : #ifdef IFCVT_MULTIPLE_DUMPS
6070 : 3250280 : if (dump_file && pass > 1)
6071 : 21 : fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
6072 : : #endif
6073 : :
6074 : 43016076 : FOR_EACH_BB_FN (bb, cfun)
6075 : : {
6076 : : basic_block new_bb;
6077 : 40110193 : while (!df_get_bb_dirty (bb)
6078 : 79308603 : && (new_bb = find_if_header (bb, pass)) != NULL)
6079 : : bb = new_bb;
6080 : : }
6081 : :
6082 : : #ifdef IFCVT_MULTIPLE_DUMPS
6083 : 3250280 : if (dump_file && cond_exec_changed_p)
6084 : 21 : print_rtl_with_bb (dump_file, get_insns (), dump_flags);
6085 : : #endif
6086 : : }
6087 : : while (cond_exec_changed_p);
6088 : :
6089 : : #ifdef IFCVT_MULTIPLE_DUMPS
6090 : 3063375 : if (dump_file)
6091 : 85 : fprintf (dump_file, "\n\n========== no more changes\n");
6092 : : #endif
6093 : :
6094 : 3063375 : free_dominance_info (CDI_POST_DOMINATORS);
6095 : :
6096 : 3063375 : if (dump_file)
6097 : 85 : fflush (dump_file);
6098 : :
6099 : 3063375 : clear_aux_for_blocks ();
6100 : :
6101 : : /* If we allocated new pseudos, we must resize the array for sched1. */
6102 : 3063375 : if (max_regno < max_reg_num ())
6103 : 618672 : max_regno = max_reg_num ();
6104 : :
6105 : : /* Write the final stats. */
6106 : 3063375 : if (dump_file && num_possible_if_blocks > 0)
6107 : : {
6108 : 59 : fprintf (dump_file,
6109 : : "\n%d possible IF blocks searched.\n",
6110 : : num_possible_if_blocks);
6111 : 59 : fprintf (dump_file,
6112 : : "%d IF blocks converted.\n",
6113 : : num_updated_if_blocks);
6114 : 59 : fprintf (dump_file,
6115 : : "%d true changes made.\n\n\n",
6116 : : num_true_changes);
6117 : : }
6118 : :
6119 : 3063375 : if (optimize == 1)
6120 : 219679 : df_remove_problem (df_live);
6121 : :
6122 : : /* Some non-cold blocks may now be only reachable from cold blocks.
6123 : : Fix that up. */
6124 : 3063375 : fixup_partitions ();
6125 : :
6126 : 3063375 : checking_verify_flow_info ();
6127 : 3063375 : }
6128 : :
6129 : : /* If-conversion and CFG cleanup. */
6130 : : static void
6131 : 1023804 : rest_of_handle_if_conversion (void)
6132 : : {
6133 : 1023804 : int flags = 0;
6134 : :
6135 : 1023804 : if (flag_if_conversion)
6136 : : {
6137 : 1021123 : if (dump_file)
6138 : : {
6139 : 33 : dump_reg_info (dump_file);
6140 : 33 : dump_flow_info (dump_file, dump_flags);
6141 : : }
6142 : 1021123 : cleanup_cfg (CLEANUP_EXPENSIVE);
6143 : 1021123 : if_convert (false);
6144 : 1021123 : if (num_updated_if_blocks)
6145 : : /* Get rid of any dead CC-related instructions. */
6146 : 1023804 : flags |= CLEANUP_FORCE_FAST_DCE;
6147 : : }
6148 : :
6149 : 1023804 : cleanup_cfg (flags);
6150 : 1023804 : }
6151 : :
6152 : : namespace {
6153 : :
6154 : : const pass_data pass_data_rtl_ifcvt =
6155 : : {
6156 : : RTL_PASS, /* type */
6157 : : "ce1", /* name */
6158 : : OPTGROUP_NONE, /* optinfo_flags */
6159 : : TV_IFCVT, /* tv_id */
6160 : : 0, /* properties_required */
6161 : : 0, /* properties_provided */
6162 : : 0, /* properties_destroyed */
6163 : : 0, /* todo_flags_start */
6164 : : TODO_df_finish, /* todo_flags_finish */
6165 : : };
6166 : :
6167 : : class pass_rtl_ifcvt : public rtl_opt_pass
6168 : : {
6169 : : public:
6170 : 285081 : pass_rtl_ifcvt (gcc::context *ctxt)
6171 : 570162 : : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
6172 : : {}
6173 : :
6174 : : /* opt_pass methods: */
6175 : 1449863 : bool gate (function *) final override
6176 : : {
6177 : 1449863 : return (optimize > 0) && dbg_cnt (if_conversion);
6178 : : }
6179 : :
6180 : 1023804 : unsigned int execute (function *) final override
6181 : : {
6182 : 1023804 : rest_of_handle_if_conversion ();
6183 : 1023804 : return 0;
6184 : : }
6185 : :
6186 : : }; // class pass_rtl_ifcvt
6187 : :
6188 : : } // anon namespace
6189 : :
6190 : : rtl_opt_pass *
6191 : 285081 : make_pass_rtl_ifcvt (gcc::context *ctxt)
6192 : : {
6193 : 285081 : return new pass_rtl_ifcvt (ctxt);
6194 : : }
6195 : :
6196 : :
6197 : : /* Rerun if-conversion, as combine may have simplified things enough
6198 : : to now meet sequence length restrictions. */
6199 : :
6200 : : namespace {
6201 : :
6202 : : const pass_data pass_data_if_after_combine =
6203 : : {
6204 : : RTL_PASS, /* type */
6205 : : "ce2", /* name */
6206 : : OPTGROUP_NONE, /* optinfo_flags */
6207 : : TV_IFCVT, /* tv_id */
6208 : : 0, /* properties_required */
6209 : : 0, /* properties_provided */
6210 : : 0, /* properties_destroyed */
6211 : : 0, /* todo_flags_start */
6212 : : TODO_df_finish, /* todo_flags_finish */
6213 : : };
6214 : :
6215 : : class pass_if_after_combine : public rtl_opt_pass
6216 : : {
6217 : : public:
6218 : 285081 : pass_if_after_combine (gcc::context *ctxt)
6219 : 570162 : : rtl_opt_pass (pass_data_if_after_combine, ctxt)
6220 : : {}
6221 : :
6222 : : /* opt_pass methods: */
6223 : 1449863 : bool gate (function *) final override
6224 : : {
6225 : 1023805 : return optimize > 0 && flag_if_conversion
6226 : 2470987 : && dbg_cnt (if_after_combine);
6227 : : }
6228 : :
6229 : 1021123 : unsigned int execute (function *) final override
6230 : : {
6231 : 1021123 : if_convert (true);
6232 : 1021123 : return 0;
6233 : : }
6234 : :
6235 : : }; // class pass_if_after_combine
6236 : :
6237 : : } // anon namespace
6238 : :
6239 : : rtl_opt_pass *
6240 : 285081 : make_pass_if_after_combine (gcc::context *ctxt)
6241 : : {
6242 : 285081 : return new pass_if_after_combine (ctxt);
6243 : : }
6244 : :
6245 : :
6246 : : namespace {
6247 : :
6248 : : const pass_data pass_data_if_after_reload =
6249 : : {
6250 : : RTL_PASS, /* type */
6251 : : "ce3", /* name */
6252 : : OPTGROUP_NONE, /* optinfo_flags */
6253 : : TV_IFCVT2, /* tv_id */
6254 : : 0, /* properties_required */
6255 : : 0, /* properties_provided */
6256 : : 0, /* properties_destroyed */
6257 : : 0, /* todo_flags_start */
6258 : : TODO_df_finish, /* todo_flags_finish */
6259 : : };
6260 : :
6261 : : class pass_if_after_reload : public rtl_opt_pass
6262 : : {
6263 : : public:
6264 : 285081 : pass_if_after_reload (gcc::context *ctxt)
6265 : 570162 : : rtl_opt_pass (pass_data_if_after_reload, ctxt)
6266 : : {}
6267 : :
6268 : : /* opt_pass methods: */
6269 : 1449863 : bool gate (function *) final override
6270 : : {
6271 : 1023805 : return optimize > 0 && flag_if_conversion2
6272 : 2470993 : && dbg_cnt (if_after_reload);
6273 : : }
6274 : :
6275 : 1021129 : unsigned int execute (function *) final override
6276 : : {
6277 : 1021129 : if_convert (true);
6278 : 1021129 : return 0;
6279 : : }
6280 : :
6281 : : }; // class pass_if_after_reload
6282 : :
6283 : : } // anon namespace
6284 : :
6285 : : rtl_opt_pass *
6286 : 285081 : make_pass_if_after_reload (gcc::context *ctxt)
6287 : : {
6288 : 285081 : return new pass_if_after_reload (ctxt);
6289 : : }
|