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 : 2894302 : cheap_bb_rtx_cost_p (const_basic_block bb,
137 : : profile_probability prob, int max_cost)
138 : : {
139 : 2894302 : int count = 0;
140 : 2894302 : rtx_insn *insn = BB_HEAD (bb);
141 : 2894302 : bool speed = optimize_bb_for_speed_p (bb);
142 : 2894302 : int scale = prob.initialized_p () ? prob.to_reg_br_prob_base ()
143 : 2894302 : : 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 : 2894302 : 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 : 2844260 : scale += REG_BR_PROB_BASE / 8;
161 : :
162 : :
163 : 2894302 : max_cost *= scale;
164 : :
165 : 10841033 : while (1)
166 : : {
167 : 13735335 : 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 : 4305279 : if (asm_noperands (PATTERN (insn)) >= 0)
174 : : return false;
175 : :
176 : 4302299 : int cost = insn_cost (insn, speed) * REG_BR_PROB_BASE;
177 : 4302299 : 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 : 4253565 : {
189 : 4253565 : rtx set = single_set (insn);
190 : 4253565 : if (set && STACK_REG_P (SET_DEST (set)))
191 : : return false;
192 : : }
193 : : #endif
194 : :
195 : 4249735 : count += cost;
196 : 4249735 : if (count >= max_cost)
197 : : return false;
198 : : }
199 : 9430056 : else if (CALL_P (insn))
200 : : return false;
201 : :
202 : 11439261 : if (insn == BB_END (bb))
203 : : break;
204 : 10841033 : insn = NEXT_INSN (insn);
205 : 10841033 : }
206 : :
207 : : return true;
208 : : }
209 : :
210 : : /* Return the first non-jump active insn in the basic block. */
211 : :
212 : : static rtx_insn *
213 : 1487354 : first_active_insn (basic_block bb)
214 : : {
215 : 1487354 : rtx_insn *insn = BB_HEAD (bb);
216 : :
217 : 1487354 : if (LABEL_P (insn))
218 : : {
219 : 270227 : if (insn == BB_END (bb))
220 : : return NULL;
221 : 270227 : insn = NEXT_INSN (insn);
222 : : }
223 : :
224 : 4489592 : while (NOTE_P (insn) || DEBUG_INSN_P (insn))
225 : : {
226 : 3002242 : if (insn == BB_END (bb))
227 : : return NULL;
228 : 3002238 : insn = NEXT_INSN (insn);
229 : : }
230 : :
231 : 1487350 : 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 : 2446295 : last_active_insn (basic_block bb, bool skip_use_p)
241 : : {
242 : 2446295 : rtx_insn *insn = BB_END (bb);
243 : 2446295 : rtx_insn *head = BB_HEAD (bb);
244 : :
245 : 2446295 : while (NOTE_P (insn)
246 : : || JUMP_P (insn)
247 : : || DEBUG_INSN_P (insn)
248 : 5499646 : || (skip_use_p
249 : 0 : && NONJUMP_INSN_P (insn)
250 : 0 : && GET_CODE (PATTERN (insn)) == USE))
251 : : {
252 : 3053685 : if (insn == head)
253 : : return NULL;
254 : 3053351 : insn = PREV_INSN (insn);
255 : : }
256 : :
257 : 2445961 : if (LABEL_P (insn))
258 : 97 : 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 : 492782 : rtx_interchangeable_p (const_rtx a, const_rtx b)
319 : : {
320 : 492782 : if (!rtx_equal_p (a, b))
321 : : return false;
322 : :
323 : 173426 : 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 : 2491 : 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 : 153836 : cond_exec_get_condition (rtx_insn *jump, bool get_reversed = false)
440 : : {
441 : 153836 : rtx test_if, cond;
442 : :
443 : 153836 : if (any_condjump_p (jump))
444 : 153836 : test_if = SET_SRC (pc_set (jump));
445 : : else
446 : : return NULL_RTX;
447 : 153836 : cond = XEXP (test_if, 0);
448 : :
449 : : /* If this branches to JUMP_LABEL when the condition is false,
450 : : reverse the condition. */
451 : 153836 : if (get_reversed
452 : 153836 : || (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
453 : 0 : && label_ref_label (XEXP (test_if, 2))
454 : 0 : == JUMP_LABEL (jump)))
455 : : {
456 : 76918 : enum rtx_code rev = reversed_comparison_code (cond, jump);
457 : 76918 : if (rev == UNKNOWN)
458 : : return NULL_RTX;
459 : :
460 : 76918 : 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 : 352382 : noce_reversed_cond_code (struct noce_if_info *if_info)
800 : : {
801 : 352382 : if (if_info->rev_cond)
802 : 352382 : 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 : 199779 : default_noce_conversion_profitable_p (rtx_insn *seq,
813 : : struct noce_if_info *if_info)
814 : : {
815 : 199779 : bool speed_p = if_info->speed_p;
816 : :
817 : : /* Cost up the new sequence. */
818 : 199779 : unsigned int cost = seq_cost (seq, speed_p);
819 : :
820 : 199779 : 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 : 169730 : return speed_p && cost <= if_info->max_seq_cost;
826 : : }
827 : :
828 : : /* Helper function for noce_try_store_flag*. */
829 : :
830 : : static rtx
831 : 45612 : noce_emit_store_flag (struct noce_if_info *if_info, rtx x, bool reversep,
832 : : int normalize)
833 : : {
834 : 45612 : rtx cond = if_info->cond;
835 : 45612 : bool cond_complex;
836 : 45612 : enum rtx_code code;
837 : :
838 : 45612 : cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
839 : 45612 : || ! 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 : 45612 : if (cond_complex)
845 : : {
846 : 23365 : rtx set = pc_set (if_info->jump);
847 : 23365 : cond = XEXP (SET_SRC (set), 0);
848 : 23365 : if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
849 : 23365 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump))
850 : 0 : reversep = !reversep;
851 : 23365 : if (if_info->then_else_reversed)
852 : 20831 : reversep = !reversep;
853 : : }
854 : 22247 : else if (reversep
855 : 15599 : && if_info->rev_cond
856 : 15599 : && general_operand (XEXP (if_info->rev_cond, 0), VOIDmode)
857 : 37846 : && general_operand (XEXP (if_info->rev_cond, 1), VOIDmode))
858 : : {
859 : 15599 : cond = if_info->rev_cond;
860 : 15599 : reversep = false;
861 : : }
862 : :
863 : 45612 : if (reversep)
864 : 4345 : code = reversed_comparison_code (cond, if_info->jump);
865 : : else
866 : 41267 : code = GET_CODE (cond);
867 : :
868 : 45612 : if ((if_info->cond_earliest == if_info->jump || cond_complex)
869 : 23365 : && (normalize == 0 || STORE_FLAG_VALUE == normalize))
870 : : {
871 : 17837 : rtx src = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
872 : : XEXP (cond, 1));
873 : 17837 : rtx set = gen_rtx_SET (x, src);
874 : :
875 : 17837 : start_sequence ();
876 : 17837 : rtx_insn *insn = emit_insn (set);
877 : :
878 : 17837 : if (recog_memoized (insn) >= 0)
879 : : {
880 : 7507 : rtx_insn *seq = get_insns ();
881 : 7507 : end_sequence ();
882 : 7507 : emit_insn (seq);
883 : :
884 : 7507 : if_info->cond_earliest = if_info->jump;
885 : :
886 : 7507 : return x;
887 : : }
888 : :
889 : 10330 : end_sequence ();
890 : : }
891 : :
892 : : /* Don't even try if the comparison operands or the mode of X are weird. */
893 : 38105 : if (cond_complex || !SCALAR_INT_MODE_P (GET_MODE (x)))
894 : 15858 : return NULL_RTX;
895 : :
896 : 22247 : return emit_store_flag (x, code, XEXP (cond, 0),
897 : : XEXP (cond, 1), VOIDmode,
898 : 22247 : (code == LTU || code == LEU
899 : 44494 : || code == GEU || code == GTU), normalize);
900 : : }
901 : :
902 : : /* Return true if X can be safely forced into a register by copy_to_mode_reg
903 : : / force_operand. */
904 : :
905 : : static bool
906 : 1798063 : noce_can_force_operand (rtx x)
907 : : {
908 : 1798063 : if (general_operand (x, VOIDmode))
909 : : return true;
910 : 393108 : if (SUBREG_P (x))
911 : : {
912 : 1178 : if (!noce_can_force_operand (SUBREG_REG (x)))
913 : : return false;
914 : : return true;
915 : : }
916 : 391930 : if (ARITHMETIC_P (x))
917 : : {
918 : 336616 : if (!noce_can_force_operand (XEXP (x, 0))
919 : 336616 : || !noce_can_force_operand (XEXP (x, 1)))
920 : 4041 : return false;
921 : 332575 : switch (GET_CODE (x))
922 : : {
923 : : case MULT:
924 : : case DIV:
925 : : case MOD:
926 : : case UDIV:
927 : : case UMOD:
928 : : return true;
929 : 324503 : default:
930 : 324503 : return code_to_optab (GET_CODE (x));
931 : : }
932 : : }
933 : 55314 : if (UNARY_P (x))
934 : : {
935 : 48564 : if (!noce_can_force_operand (XEXP (x, 0)))
936 : : return false;
937 : 48552 : switch (GET_CODE (x))
938 : : {
939 : : case ZERO_EXTEND:
940 : : case SIGN_EXTEND:
941 : : case TRUNCATE:
942 : : case FLOAT_EXTEND:
943 : : case FLOAT_TRUNCATE:
944 : : case FIX:
945 : : case UNSIGNED_FIX:
946 : : case FLOAT:
947 : : case UNSIGNED_FLOAT:
948 : : return true;
949 : 8585 : default:
950 : 8585 : return code_to_optab (GET_CODE (x));
951 : : }
952 : : }
953 : : return false;
954 : : }
955 : :
956 : : /* Emit instruction to move an rtx, possibly into STRICT_LOW_PART.
957 : : X is the destination/target and Y is the value to copy. */
958 : :
959 : : static void
960 : 111184 : noce_emit_move_insn (rtx x, rtx y)
961 : : {
962 : 111184 : machine_mode outmode;
963 : 111184 : rtx outer, inner;
964 : 111184 : poly_int64 bitpos;
965 : :
966 : 111184 : if (GET_CODE (x) != STRICT_LOW_PART)
967 : : {
968 : 111184 : rtx_insn *seq, *insn;
969 : 111184 : rtx target;
970 : 111184 : optab ot;
971 : :
972 : 111184 : start_sequence ();
973 : : /* Check that the SET_SRC is reasonable before calling emit_move_insn,
974 : : otherwise construct a suitable SET pattern ourselves. */
975 : 84752 : insn = (OBJECT_P (y) || CONSTANT_P (y) || GET_CODE (y) == SUBREG)
976 : 111245 : ? emit_move_insn (x, y)
977 : 84691 : : emit_insn (gen_rtx_SET (x, y));
978 : 111184 : seq = get_insns ();
979 : 111184 : end_sequence ();
980 : :
981 : 111184 : if (recog_memoized (insn) <= 0)
982 : : {
983 : 55178 : if (GET_CODE (x) == ZERO_EXTRACT)
984 : : {
985 : 0 : rtx op = XEXP (x, 0);
986 : 0 : unsigned HOST_WIDE_INT size = INTVAL (XEXP (x, 1));
987 : 0 : unsigned HOST_WIDE_INT start = INTVAL (XEXP (x, 2));
988 : :
989 : : /* store_bit_field expects START to be relative to
990 : : BYTES_BIG_ENDIAN and adjusts this value for machines with
991 : : BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN. In order to be able to
992 : : invoke store_bit_field again it is necessary to have the START
993 : : value from the first call. */
994 : 0 : if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
995 : : {
996 : : if (MEM_P (op))
997 : : start = BITS_PER_UNIT - start - size;
998 : : else
999 : : {
1000 : : gcc_assert (REG_P (op));
1001 : : start = BITS_PER_WORD - start - size;
1002 : : }
1003 : : }
1004 : :
1005 : 0 : gcc_assert (start < (MEM_P (op) ? BITS_PER_UNIT : BITS_PER_WORD));
1006 : 0 : store_bit_field (op, size, start, 0, 0, GET_MODE (x), y, false,
1007 : : false);
1008 : 0 : return;
1009 : : }
1010 : :
1011 : 55178 : switch (GET_RTX_CLASS (GET_CODE (y)))
1012 : : {
1013 : 1122 : case RTX_UNARY:
1014 : 1122 : ot = code_to_optab (GET_CODE (y));
1015 : 1122 : if (ot && noce_can_force_operand (XEXP (y, 0)))
1016 : : {
1017 : 848 : start_sequence ();
1018 : 848 : target = expand_unop (GET_MODE (y), ot, XEXP (y, 0), x, 0);
1019 : 848 : if (target != NULL_RTX)
1020 : : {
1021 : 848 : if (target != x)
1022 : 0 : emit_move_insn (x, target);
1023 : 848 : seq = get_insns ();
1024 : : }
1025 : 848 : end_sequence ();
1026 : : }
1027 : : break;
1028 : :
1029 : 33084 : case RTX_BIN_ARITH:
1030 : 33084 : case RTX_COMM_ARITH:
1031 : 33084 : ot = code_to_optab (GET_CODE (y));
1032 : 33084 : if (ot
1033 : 33084 : && noce_can_force_operand (XEXP (y, 0))
1034 : 66168 : && noce_can_force_operand (XEXP (y, 1)))
1035 : : {
1036 : 33084 : start_sequence ();
1037 : 33084 : target = expand_binop (GET_MODE (y), ot,
1038 : : XEXP (y, 0), XEXP (y, 1),
1039 : : x, 0, OPTAB_DIRECT);
1040 : 33084 : if (target != NULL_RTX)
1041 : : {
1042 : 33056 : if (target != x)
1043 : 0 : emit_move_insn (x, target);
1044 : 33056 : seq = get_insns ();
1045 : : }
1046 : 33084 : end_sequence ();
1047 : : }
1048 : : break;
1049 : :
1050 : : default:
1051 : : break;
1052 : : }
1053 : : }
1054 : :
1055 : 111184 : emit_insn (seq);
1056 : 111184 : return;
1057 : : }
1058 : :
1059 : 0 : outer = XEXP (x, 0);
1060 : 0 : inner = XEXP (outer, 0);
1061 : 0 : outmode = GET_MODE (outer);
1062 : 0 : bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
1063 : 0 : store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos,
1064 : 0 : 0, 0, outmode, y, false, false);
1065 : : }
1066 : :
1067 : : /* Return the CC reg if it is used in COND. */
1068 : :
1069 : : static rtx
1070 : 3992664 : cc_in_cond (rtx cond)
1071 : : {
1072 : 3992664 : if (have_cbranchcc4 && cond
1073 : 3992664 : && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_CC)
1074 : 71183 : return XEXP (cond, 0);
1075 : :
1076 : : return NULL_RTX;
1077 : : }
1078 : :
1079 : : /* Return sequence of instructions generated by if conversion. This
1080 : : function calls end_sequence() to end the current stream, ensures
1081 : : that the instructions are unshared, recognizable non-jump insns.
1082 : : On failure, this function returns a NULL_RTX. */
1083 : :
1084 : : static rtx_insn *
1085 : 229619 : end_ifcvt_sequence (struct noce_if_info *if_info)
1086 : : {
1087 : 229619 : rtx_insn *insn;
1088 : 229619 : rtx_insn *seq = get_insns ();
1089 : 229619 : rtx cc = cc_in_cond (if_info->cond);
1090 : :
1091 : 229619 : set_used_flags (if_info->x);
1092 : 229619 : set_used_flags (if_info->cond);
1093 : 229619 : set_used_flags (if_info->a);
1094 : 229619 : set_used_flags (if_info->b);
1095 : :
1096 : 1284183 : for (insn = seq; insn; insn = NEXT_INSN (insn))
1097 : 824945 : set_used_flags (insn);
1098 : :
1099 : 229619 : unshare_all_rtl_in_chain (seq);
1100 : 229619 : end_sequence ();
1101 : :
1102 : : /* Make sure that all of the instructions emitted are recognizable,
1103 : : and that we haven't introduced a new jump instruction.
1104 : : As an exercise for the reader, build a general mechanism that
1105 : : allows proper placement of required clobbers. */
1106 : 1259705 : for (insn = seq; insn; insn = NEXT_INSN (insn))
1107 : 822221 : if (JUMP_P (insn)
1108 : 822221 : || recog_memoized (insn) == -1
1109 : : /* Make sure new generated code does not clobber CC. */
1110 : 1622764 : || (cc && set_of (cc, insn)))
1111 : 21754 : return NULL;
1112 : :
1113 : : return seq;
1114 : : }
1115 : :
1116 : : /* Return true iff the then and else basic block (if it exists)
1117 : : consist of a single simple set instruction. */
1118 : :
1119 : : static bool
1120 : 3220069 : noce_simple_bbs (struct noce_if_info *if_info)
1121 : : {
1122 : 0 : if (!if_info->then_simple)
1123 : : return false;
1124 : :
1125 : 2959680 : if (if_info->else_bb)
1126 : 0 : return if_info->else_simple;
1127 : :
1128 : : return true;
1129 : : }
1130 : :
1131 : : /* Convert "if (a != b) x = a; else x = b" into "x = a" and
1132 : : "if (a == b) x = a; else x = b" into "x = b". */
1133 : :
1134 : : static bool
1135 : 264526 : noce_try_move (struct noce_if_info *if_info)
1136 : : {
1137 : 264526 : rtx cond = if_info->cond;
1138 : 264526 : enum rtx_code code = GET_CODE (cond);
1139 : 264526 : rtx y;
1140 : 264526 : rtx_insn *seq;
1141 : :
1142 : 264526 : if (code != NE && code != EQ)
1143 : : return false;
1144 : :
1145 : 502902 : if (!noce_simple_bbs (if_info))
1146 : : return false;
1147 : :
1148 : : /* This optimization isn't valid if either A or B could be a NaN
1149 : : or a signed zero. */
1150 : 187764 : if (HONOR_NANS (if_info->x)
1151 : 187764 : || HONOR_SIGNED_ZEROS (if_info->x))
1152 : 54080 : return false;
1153 : :
1154 : : /* Check whether the operands of the comparison are A and in
1155 : : either order. */
1156 : 133684 : if ((rtx_equal_p (if_info->a, XEXP (cond, 0))
1157 : 2045 : && rtx_equal_p (if_info->b, XEXP (cond, 1)))
1158 : 135718 : || (rtx_equal_p (if_info->a, XEXP (cond, 1))
1159 : 2013 : && rtx_equal_p (if_info->b, XEXP (cond, 0))))
1160 : : {
1161 : 22 : if (!rtx_interchangeable_p (if_info->a, if_info->b))
1162 : : return false;
1163 : :
1164 : 0 : y = (code == EQ) ? if_info->a : if_info->b;
1165 : :
1166 : : /* Avoid generating the move if the source is the destination. */
1167 : 0 : if (! rtx_equal_p (if_info->x, y))
1168 : : {
1169 : 0 : start_sequence ();
1170 : 0 : noce_emit_move_insn (if_info->x, y);
1171 : 0 : seq = end_ifcvt_sequence (if_info);
1172 : 0 : if (!seq)
1173 : : return false;
1174 : :
1175 : 0 : emit_insn_before_setloc (seq, if_info->jump,
1176 : 0 : INSN_LOCATION (if_info->insn_a));
1177 : : }
1178 : 0 : if_info->transform_name = "noce_try_move";
1179 : 0 : return true;
1180 : : }
1181 : : return false;
1182 : : }
1183 : :
1184 : : /* Try forming an IF_THEN_ELSE (cond, b, a) and collapsing that
1185 : : through simplify_rtx. Sometimes that can eliminate the IF_THEN_ELSE.
1186 : : If that is the case, emit the result into x. */
1187 : :
1188 : : static bool
1189 : 264526 : noce_try_ifelse_collapse (struct noce_if_info * if_info)
1190 : : {
1191 : 565741 : if (!noce_simple_bbs (if_info))
1192 : : return false;
1193 : :
1194 : 241753 : machine_mode mode = GET_MODE (if_info->x);
1195 : 241753 : rtx if_then_else = simplify_gen_ternary (IF_THEN_ELSE, mode, mode,
1196 : : if_info->cond, if_info->b,
1197 : : if_info->a);
1198 : :
1199 : 241753 : if (GET_CODE (if_then_else) == IF_THEN_ELSE)
1200 : : return false;
1201 : :
1202 : 23060 : rtx_insn *seq;
1203 : 23060 : start_sequence ();
1204 : 23060 : noce_emit_move_insn (if_info->x, if_then_else);
1205 : 23060 : seq = end_ifcvt_sequence (if_info);
1206 : 23060 : if (!seq)
1207 : : return false;
1208 : :
1209 : 2088 : emit_insn_before_setloc (seq, if_info->jump,
1210 : 2088 : INSN_LOCATION (if_info->insn_a));
1211 : :
1212 : 2088 : if_info->transform_name = "noce_try_ifelse_collapse";
1213 : 2088 : return true;
1214 : : }
1215 : :
1216 : :
1217 : : /* Convert "if (test) x = 1; else x = 0".
1218 : :
1219 : : Only try 0 and STORE_FLAG_VALUE here. Other combinations will be
1220 : : tried in noce_try_store_flag_constants after noce_try_cmove has had
1221 : : a go at the conversion. */
1222 : :
1223 : : static bool
1224 : 262438 : noce_try_store_flag (struct noce_if_info *if_info)
1225 : : {
1226 : 262438 : bool reversep;
1227 : 262438 : rtx target;
1228 : 262438 : rtx_insn *seq;
1229 : :
1230 : 547389 : if (!noce_simple_bbs (if_info))
1231 : : return false;
1232 : :
1233 : 239665 : if (CONST_INT_P (if_info->b)
1234 : 65662 : && INTVAL (if_info->b) == STORE_FLAG_VALUE
1235 : 3763 : && if_info->a == const0_rtx)
1236 : : reversep = false;
1237 : 238511 : else if (if_info->b == const0_rtx
1238 : 33466 : && CONST_INT_P (if_info->a)
1239 : 20331 : && INTVAL (if_info->a) == STORE_FLAG_VALUE
1240 : 258330 : && noce_reversed_cond_code (if_info) != UNKNOWN)
1241 : : reversep = true;
1242 : : else
1243 : 218692 : return false;
1244 : :
1245 : 20973 : start_sequence ();
1246 : :
1247 : 20973 : target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
1248 : 20973 : if (target)
1249 : : {
1250 : 16229 : if (target != if_info->x)
1251 : 3036 : noce_emit_move_insn (if_info->x, target);
1252 : :
1253 : 16229 : seq = end_ifcvt_sequence (if_info);
1254 : 16229 : if (! seq)
1255 : : return false;
1256 : :
1257 : 16227 : emit_insn_before_setloc (seq, if_info->jump,
1258 : 16227 : INSN_LOCATION (if_info->insn_a));
1259 : 16227 : if_info->transform_name = "noce_try_store_flag";
1260 : 16227 : return true;
1261 : : }
1262 : : else
1263 : : {
1264 : 4744 : end_sequence ();
1265 : 4744 : return false;
1266 : : }
1267 : : }
1268 : :
1269 : :
1270 : : /* Convert "if (test) x = -A; else x = A" into
1271 : : x = A; if (test) x = -x if the machine can do the
1272 : : conditional negate form of this cheaply.
1273 : : Try this before noce_try_cmove that will just load the
1274 : : immediates into two registers and do a conditional select
1275 : : between them. If the target has a conditional negate or
1276 : : conditional invert operation we can save a potentially
1277 : : expensive constant synthesis. */
1278 : :
1279 : : static bool
1280 : 246128 : noce_try_inverse_constants (struct noce_if_info *if_info)
1281 : : {
1282 : 530608 : if (!noce_simple_bbs (if_info))
1283 : : return false;
1284 : :
1285 : 223355 : if (!CONST_INT_P (if_info->a)
1286 : 49981 : || !CONST_INT_P (if_info->b)
1287 : 19320 : || !REG_P (if_info->x))
1288 : : return false;
1289 : :
1290 : 19320 : machine_mode mode = GET_MODE (if_info->x);
1291 : :
1292 : 19320 : HOST_WIDE_INT val_a = INTVAL (if_info->a);
1293 : 19320 : HOST_WIDE_INT val_b = INTVAL (if_info->b);
1294 : :
1295 : 19320 : rtx cond = if_info->cond;
1296 : :
1297 : 19320 : rtx x = if_info->x;
1298 : 19320 : rtx target;
1299 : :
1300 : 19320 : start_sequence ();
1301 : :
1302 : 19320 : rtx_code code;
1303 : 19320 : if (val_b != HOST_WIDE_INT_MIN && val_a == -val_b)
1304 : : code = NEG;
1305 : 18336 : else if (val_a == ~val_b)
1306 : : code = NOT;
1307 : : else
1308 : : {
1309 : 18218 : end_sequence ();
1310 : 18218 : return false;
1311 : : }
1312 : :
1313 : 1102 : rtx tmp = gen_reg_rtx (mode);
1314 : 1102 : noce_emit_move_insn (tmp, if_info->a);
1315 : :
1316 : 1102 : target = emit_conditional_neg_or_complement (x, code, mode, cond, tmp, tmp);
1317 : :
1318 : 1102 : if (target)
1319 : : {
1320 : 0 : rtx_insn *seq = get_insns ();
1321 : :
1322 : 0 : if (!seq)
1323 : : {
1324 : 0 : end_sequence ();
1325 : 0 : return false;
1326 : : }
1327 : :
1328 : 0 : if (target != if_info->x)
1329 : 0 : noce_emit_move_insn (if_info->x, target);
1330 : :
1331 : 0 : seq = end_ifcvt_sequence (if_info);
1332 : :
1333 : 0 : if (!seq)
1334 : : return false;
1335 : :
1336 : 0 : emit_insn_before_setloc (seq, if_info->jump,
1337 : 0 : INSN_LOCATION (if_info->insn_a));
1338 : 0 : if_info->transform_name = "noce_try_inverse_constants";
1339 : 0 : return true;
1340 : : }
1341 : :
1342 : 1102 : end_sequence ();
1343 : 1102 : return false;
1344 : : }
1345 : :
1346 : :
1347 : : /* Convert "if (test) x = a; else x = b", for A and B constant.
1348 : : Also allow A = y + c1, B = y + c2, with a common y between A
1349 : : and B. */
1350 : :
1351 : : static bool
1352 : 246128 : noce_try_store_flag_constants (struct noce_if_info *if_info)
1353 : : {
1354 : 246128 : rtx target;
1355 : 246128 : rtx_insn *seq;
1356 : 246128 : bool reversep;
1357 : 246128 : HOST_WIDE_INT itrue, ifalse, diff, tmp;
1358 : 246128 : int normalize;
1359 : 246128 : bool can_reverse;
1360 : 246128 : machine_mode mode = GET_MODE (if_info->x);
1361 : 246128 : rtx common = NULL_RTX;
1362 : :
1363 : 246128 : rtx a = if_info->a;
1364 : 246128 : rtx b = if_info->b;
1365 : :
1366 : : /* Handle cases like x := test ? y + 3 : y + 4. */
1367 : 246128 : if (GET_CODE (a) == PLUS
1368 : 29073 : && GET_CODE (b) == PLUS
1369 : 1758 : && CONST_INT_P (XEXP (a, 1))
1370 : 729 : && CONST_INT_P (XEXP (b, 1))
1371 : 638 : && rtx_equal_p (XEXP (a, 0), XEXP (b, 0))
1372 : : /* Allow expressions that are not using the result or plain
1373 : : registers where we handle overlap below. */
1374 : 246670 : && (REG_P (XEXP (a, 0))
1375 : 13 : || (noce_operand_ok (XEXP (a, 0))
1376 : 13 : && ! reg_overlap_mentioned_p (if_info->x, XEXP (a, 0)))))
1377 : : {
1378 : 542 : common = XEXP (a, 0);
1379 : 542 : a = XEXP (a, 1);
1380 : 542 : b = XEXP (b, 1);
1381 : : }
1382 : :
1383 : 307253 : if (!noce_simple_bbs (if_info))
1384 : : return false;
1385 : :
1386 : 223355 : if (CONST_INT_P (a)
1387 : 50511 : && CONST_INT_P (b))
1388 : : {
1389 : 19850 : ifalse = INTVAL (a);
1390 : 19850 : itrue = INTVAL (b);
1391 : 19850 : bool subtract_flag_p = false;
1392 : :
1393 : 19850 : diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1394 : : /* Make sure we can represent the difference between the two values. */
1395 : 19850 : if ((diff > 0)
1396 : 19850 : != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1397 : : return false;
1398 : :
1399 : 19825 : diff = trunc_int_for_mode (diff, mode);
1400 : :
1401 : 19825 : can_reverse = noce_reversed_cond_code (if_info) != UNKNOWN;
1402 : 19825 : reversep = false;
1403 : 19825 : if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1404 : : {
1405 : 11246 : normalize = 0;
1406 : : /* We could collapse these cases but it is easier to follow the
1407 : : diff/STORE_FLAG_VALUE combinations when they are listed
1408 : : explicitly. */
1409 : :
1410 : : /* test ? 3 : 4
1411 : : => 4 + (test != 0). */
1412 : 11246 : if (diff < 0 && STORE_FLAG_VALUE < 0)
1413 : : reversep = false;
1414 : : /* test ? 4 : 3
1415 : : => can_reverse | 4 + (test == 0)
1416 : : !can_reverse | 3 - (test != 0). */
1417 : 11246 : else if (diff > 0 && STORE_FLAG_VALUE < 0)
1418 : : {
1419 : : reversep = can_reverse;
1420 : : subtract_flag_p = !can_reverse;
1421 : : /* If we need to subtract the flag and we have PLUS-immediate
1422 : : A and B then it is unlikely to be beneficial to play tricks
1423 : : here. */
1424 : : if (subtract_flag_p && common)
1425 : : return false;
1426 : : }
1427 : : /* test ? 3 : 4
1428 : : => can_reverse | 3 + (test == 0)
1429 : : !can_reverse | 4 - (test != 0). */
1430 : 11246 : else if (diff < 0 && STORE_FLAG_VALUE > 0)
1431 : : {
1432 : 5468 : reversep = can_reverse;
1433 : 5468 : subtract_flag_p = !can_reverse;
1434 : : /* If we need to subtract the flag and we have PLUS-immediate
1435 : : A and B then it is unlikely to be beneficial to play tricks
1436 : : here. */
1437 : 5468 : if (subtract_flag_p && common)
1438 : : return false;
1439 : : }
1440 : : /* test ? 4 : 3
1441 : : => 4 + (test != 0). */
1442 : : else if (diff > 0 && STORE_FLAG_VALUE > 0)
1443 : : reversep = false;
1444 : : else
1445 : : gcc_unreachable ();
1446 : : }
1447 : : /* Is this (cond) ? 2^n : 0? */
1448 : 1340 : else if (ifalse == 0 && pow2p_hwi (itrue)
1449 : 8579 : && STORE_FLAG_VALUE == 1)
1450 : : normalize = 1;
1451 : : /* Is this (cond) ? 0 : 2^n? */
1452 : 244415 : else if (itrue == 0 && pow2p_hwi (ifalse) && can_reverse
1453 : 8573 : && STORE_FLAG_VALUE == 1)
1454 : : {
1455 : : normalize = 1;
1456 : : reversep = true;
1457 : : }
1458 : : /* Is this (cond) ? -1 : x? */
1459 : : else if (itrue == -1
1460 : : && STORE_FLAG_VALUE == -1)
1461 : : normalize = -1;
1462 : : /* Is this (cond) ? x : -1? */
1463 : : else if (ifalse == -1 && can_reverse
1464 : : && STORE_FLAG_VALUE == -1)
1465 : : {
1466 : : normalize = -1;
1467 : : reversep = true;
1468 : : }
1469 : : else
1470 : : return false;
1471 : :
1472 : 5468 : if (reversep)
1473 : : {
1474 : 5469 : std::swap (itrue, ifalse);
1475 : 5469 : diff = trunc_int_for_mode (-(unsigned HOST_WIDE_INT) diff, mode);
1476 : : }
1477 : :
1478 : 11254 : start_sequence ();
1479 : :
1480 : : /* If we have x := test ? x + 3 : x + 4 then move the original
1481 : : x out of the way while we store flags. */
1482 : 11254 : if (common && rtx_equal_p (common, if_info->x))
1483 : : {
1484 : 11 : common = gen_reg_rtx (mode);
1485 : 11 : noce_emit_move_insn (common, if_info->x);
1486 : : }
1487 : :
1488 : 11254 : target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
1489 : 11254 : if (! target)
1490 : : {
1491 : 4744 : end_sequence ();
1492 : 4744 : return false;
1493 : : }
1494 : :
1495 : : /* if (test) x = 3; else x = 4;
1496 : : => x = 3 + (test == 0); */
1497 : 6510 : if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
1498 : : {
1499 : : /* Add the common part now. This may allow combine to merge this
1500 : : with the store flag operation earlier into some sort of conditional
1501 : : increment/decrement if the target allows it. */
1502 : 6502 : if (common)
1503 : 94 : target = expand_simple_binop (mode, PLUS,
1504 : : target, common,
1505 : : target, 0, OPTAB_WIDEN);
1506 : :
1507 : : /* Always use ifalse here. It should have been swapped with itrue
1508 : : when appropriate when reversep is true. */
1509 : 13004 : target = expand_simple_binop (mode, subtract_flag_p ? MINUS : PLUS,
1510 : 6502 : gen_int_mode (ifalse, mode), target,
1511 : : if_info->x, 0, OPTAB_WIDEN);
1512 : : }
1513 : : /* Other cases are not beneficial when the original A and B are PLUS
1514 : : expressions. */
1515 : 8 : else if (common)
1516 : : {
1517 : 0 : end_sequence ();
1518 : 0 : return false;
1519 : : }
1520 : : /* if (test) x = 8; else x = 0;
1521 : : => x = (test != 0) << 3; */
1522 : 8 : else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
1523 : : {
1524 : 8 : target = expand_simple_binop (mode, ASHIFT,
1525 : : target, GEN_INT (tmp), if_info->x, 0,
1526 : : OPTAB_WIDEN);
1527 : : }
1528 : :
1529 : : /* if (test) x = -1; else x = b;
1530 : : => x = -(test != 0) | b; */
1531 : 0 : else if (itrue == -1)
1532 : : {
1533 : 0 : target = expand_simple_binop (mode, IOR,
1534 : 0 : target, gen_int_mode (ifalse, mode),
1535 : : if_info->x, 0, OPTAB_WIDEN);
1536 : : }
1537 : : else
1538 : : {
1539 : 0 : end_sequence ();
1540 : 0 : return false;
1541 : : }
1542 : :
1543 : 6510 : if (! target)
1544 : : {
1545 : 0 : end_sequence ();
1546 : 0 : return false;
1547 : : }
1548 : :
1549 : 6510 : if (target != if_info->x)
1550 : 0 : noce_emit_move_insn (if_info->x, target);
1551 : :
1552 : 6510 : seq = end_ifcvt_sequence (if_info);
1553 : 6510 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1554 : 4400 : return false;
1555 : :
1556 : 2110 : emit_insn_before_setloc (seq, if_info->jump,
1557 : 2110 : INSN_LOCATION (if_info->insn_a));
1558 : 2110 : if_info->transform_name = "noce_try_store_flag_constants";
1559 : :
1560 : 2110 : return true;
1561 : : }
1562 : :
1563 : : return false;
1564 : : }
1565 : :
1566 : : /* Convert "if (test) foo++" into "foo += (test != 0)", and
1567 : : similarly for "foo--". */
1568 : :
1569 : : static bool
1570 : 201984 : noce_try_addcc (struct noce_if_info *if_info)
1571 : : {
1572 : 201984 : rtx target;
1573 : 201984 : rtx_insn *seq;
1574 : 201984 : bool subtract;
1575 : 201984 : int normalize;
1576 : :
1577 : 419034 : if (!noce_simple_bbs (if_info))
1578 : : return false;
1579 : :
1580 : 179211 : if (GET_CODE (if_info->a) == PLUS
1581 : 24413 : && rtx_equal_p (XEXP (if_info->a, 0), if_info->b)
1582 : 198496 : && noce_reversed_cond_code (if_info) != UNKNOWN)
1583 : : {
1584 : 19285 : rtx cond = if_info->rev_cond;
1585 : 19285 : enum rtx_code code;
1586 : :
1587 : 19285 : if (cond == NULL_RTX)
1588 : : {
1589 : 0 : cond = if_info->cond;
1590 : 0 : code = reversed_comparison_code (cond, if_info->jump);
1591 : : }
1592 : : else
1593 : 19285 : code = GET_CODE (cond);
1594 : :
1595 : : /* First try to use addcc pattern. */
1596 : 19285 : if (general_operand (XEXP (cond, 0), VOIDmode)
1597 : 19285 : && general_operand (XEXP (cond, 1), VOIDmode))
1598 : : {
1599 : 17632 : start_sequence ();
1600 : 52896 : target = emit_conditional_add (if_info->x, code,
1601 : : XEXP (cond, 0),
1602 : : XEXP (cond, 1),
1603 : : VOIDmode,
1604 : : if_info->b,
1605 : 17632 : XEXP (if_info->a, 1),
1606 : 17632 : GET_MODE (if_info->x),
1607 : 17632 : (code == LTU || code == GEU
1608 : 17632 : || code == LEU || code == GTU));
1609 : 17632 : if (target)
1610 : : {
1611 : 912 : if (target != if_info->x)
1612 : 0 : noce_emit_move_insn (if_info->x, target);
1613 : :
1614 : 912 : seq = end_ifcvt_sequence (if_info);
1615 : 912 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1616 : 64 : return false;
1617 : :
1618 : 848 : emit_insn_before_setloc (seq, if_info->jump,
1619 : 848 : INSN_LOCATION (if_info->insn_a));
1620 : 848 : if_info->transform_name = "noce_try_addcc";
1621 : :
1622 : 848 : return true;
1623 : : }
1624 : 16720 : end_sequence ();
1625 : : }
1626 : :
1627 : : /* If that fails, construct conditional increment or decrement using
1628 : : setcc. We're changing a branch and an increment to a comparison and
1629 : : an ADD/SUB. */
1630 : 18373 : if (XEXP (if_info->a, 1) == const1_rtx
1631 : 12802 : || XEXP (if_info->a, 1) == constm1_rtx)
1632 : : {
1633 : 6945 : start_sequence ();
1634 : 6945 : if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1635 : : subtract = false, normalize = 0;
1636 : 1374 : else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
1637 : : subtract = true, normalize = 0;
1638 : : else
1639 : 0 : subtract = false, normalize = INTVAL (XEXP (if_info->a, 1));
1640 : :
1641 : :
1642 : 6945 : target = noce_emit_store_flag (if_info,
1643 : 6945 : gen_reg_rtx (GET_MODE (if_info->x)),
1644 : : true, normalize);
1645 : :
1646 : 6945 : if (target)
1647 : 10832 : target = expand_simple_binop (GET_MODE (if_info->x),
1648 : : subtract ? MINUS : PLUS,
1649 : : if_info->b, target, if_info->x,
1650 : : 0, OPTAB_WIDEN);
1651 : 6103 : if (target)
1652 : : {
1653 : 6103 : if (target != if_info->x)
1654 : 0 : noce_emit_move_insn (if_info->x, target);
1655 : :
1656 : 6103 : seq = end_ifcvt_sequence (if_info);
1657 : 6103 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1658 : 1295 : return false;
1659 : :
1660 : 4808 : emit_insn_before_setloc (seq, if_info->jump,
1661 : 4808 : INSN_LOCATION (if_info->insn_a));
1662 : 4808 : if_info->transform_name = "noce_try_addcc";
1663 : 4808 : return true;
1664 : : }
1665 : 842 : end_sequence ();
1666 : : }
1667 : : }
1668 : :
1669 : : return false;
1670 : : }
1671 : :
1672 : : /* Convert "if (test) x = 0;" to "x &= -(test == 0);" */
1673 : :
1674 : : static bool
1675 : 196328 : noce_try_store_flag_mask (struct noce_if_info *if_info)
1676 : : {
1677 : 196328 : rtx target;
1678 : 196328 : rtx_insn *seq;
1679 : 196328 : bool reversep;
1680 : :
1681 : 413284 : if (!noce_simple_bbs (if_info))
1682 : : return false;
1683 : :
1684 : 173555 : reversep = false;
1685 : :
1686 : 173555 : if ((if_info->a == const0_rtx
1687 : 2775 : && (REG_P (if_info->b) || rtx_equal_p (if_info->b, if_info->x)))
1688 : 174945 : || ((reversep = (noce_reversed_cond_code (if_info) != UNKNOWN))
1689 : 172170 : && if_info->b == const0_rtx
1690 : 17051 : && (REG_P (if_info->a) || rtx_equal_p (if_info->a, if_info->x))))
1691 : : {
1692 : 1395 : start_sequence ();
1693 : 1395 : target = noce_emit_store_flag (if_info,
1694 : 1395 : gen_reg_rtx (GET_MODE (if_info->x)),
1695 : : reversep, -1);
1696 : 1395 : if (target)
1697 : 628 : target = expand_simple_binop (GET_MODE (if_info->x), AND,
1698 : : reversep ? if_info->a : if_info->b,
1699 : : target, if_info->x, 0,
1700 : : OPTAB_WIDEN);
1701 : :
1702 : 628 : if (target)
1703 : : {
1704 : 628 : if (target != if_info->x)
1705 : 0 : noce_emit_move_insn (if_info->x, target);
1706 : :
1707 : 628 : seq = end_ifcvt_sequence (if_info);
1708 : 628 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1709 : 626 : return false;
1710 : :
1711 : 2 : emit_insn_before_setloc (seq, if_info->jump,
1712 : 2 : INSN_LOCATION (if_info->insn_a));
1713 : 2 : if_info->transform_name = "noce_try_store_flag_mask";
1714 : :
1715 : 2 : return true;
1716 : : }
1717 : :
1718 : 767 : end_sequence ();
1719 : : }
1720 : :
1721 : : return false;
1722 : : }
1723 : :
1724 : : /* Helper function for noce_try_cmove and noce_try_cmove_arith. */
1725 : :
1726 : : static rtx
1727 : 561158 : noce_emit_cmove (struct noce_if_info *if_info, rtx x, enum rtx_code code,
1728 : : rtx cmp_a, rtx cmp_b, rtx vfalse, rtx vtrue, rtx cc_cmp,
1729 : : rtx rev_cc_cmp)
1730 : : {
1731 : 561158 : rtx target ATTRIBUTE_UNUSED;
1732 : 561158 : bool unsignedp ATTRIBUTE_UNUSED;
1733 : :
1734 : : /* If earliest == jump, try to build the cmove insn directly.
1735 : : This is helpful when combine has created some complex condition
1736 : : (like for alpha's cmovlbs) that we can't hope to regenerate
1737 : : through the normal interface. */
1738 : :
1739 : 561158 : if (if_info->cond_earliest == if_info->jump)
1740 : : {
1741 : 4588 : rtx cond = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
1742 : 4588 : rtx if_then_else = gen_rtx_IF_THEN_ELSE (GET_MODE (x),
1743 : : cond, vtrue, vfalse);
1744 : 4588 : rtx set = gen_rtx_SET (x, if_then_else);
1745 : :
1746 : 4588 : start_sequence ();
1747 : 4588 : rtx_insn *insn = emit_insn (set);
1748 : :
1749 : 4588 : if (recog_memoized (insn) >= 0)
1750 : : {
1751 : 1253 : rtx_insn *seq = get_insns ();
1752 : 1253 : end_sequence ();
1753 : 1253 : emit_insn (seq);
1754 : :
1755 : 1253 : return x;
1756 : : }
1757 : :
1758 : 3335 : end_sequence ();
1759 : : }
1760 : :
1761 : 1119810 : unsignedp = (code == LTU || code == GEU
1762 : 559905 : || code == LEU || code == GTU);
1763 : :
1764 : 559905 : if (cc_cmp != NULL_RTX && rev_cc_cmp != NULL_RTX)
1765 : 119785 : target = emit_conditional_move (x, cc_cmp, rev_cc_cmp,
1766 : 119785 : vtrue, vfalse, GET_MODE (x));
1767 : : else
1768 : : {
1769 : : /* Don't even try if the comparison operands are weird
1770 : : except that the target supports cbranchcc4. */
1771 : 440120 : if (! general_operand (cmp_a, GET_MODE (cmp_a))
1772 : 440120 : || ! general_operand (cmp_b, GET_MODE (cmp_b)))
1773 : : {
1774 : 40056 : if (!have_cbranchcc4
1775 : 40056 : || GET_MODE_CLASS (GET_MODE (cmp_a)) != MODE_CC
1776 : 2824 : || cmp_b != const0_rtx)
1777 : : return NULL_RTX;
1778 : : }
1779 : :
1780 : 402888 : target = emit_conditional_move (x, { code, cmp_a, cmp_b, VOIDmode },
1781 : 402888 : vtrue, vfalse, GET_MODE (x),
1782 : : unsignedp);
1783 : : }
1784 : :
1785 : 522673 : if (target)
1786 : : return target;
1787 : :
1788 : : /* We might be faced with a situation like:
1789 : :
1790 : : x = (reg:M TARGET)
1791 : : vtrue = (subreg:M (reg:N VTRUE) BYTE)
1792 : : vfalse = (subreg:M (reg:N VFALSE) BYTE)
1793 : :
1794 : : We can't do a conditional move in mode M, but it's possible that we
1795 : : could do a conditional move in mode N instead and take a subreg of
1796 : : the result.
1797 : :
1798 : : If we can't create new pseudos, though, don't bother. */
1799 : 42388 : if (reload_completed)
1800 : : return NULL_RTX;
1801 : :
1802 : 42388 : if (GET_CODE (vtrue) == SUBREG && GET_CODE (vfalse) == SUBREG)
1803 : : {
1804 : 30 : rtx reg_vtrue = SUBREG_REG (vtrue);
1805 : 30 : rtx reg_vfalse = SUBREG_REG (vfalse);
1806 : 30 : poly_uint64 byte_vtrue = SUBREG_BYTE (vtrue);
1807 : 30 : poly_uint64 byte_vfalse = SUBREG_BYTE (vfalse);
1808 : 30 : rtx promoted_target;
1809 : :
1810 : 30 : if (GET_MODE (reg_vtrue) != GET_MODE (reg_vfalse)
1811 : 30 : || maybe_ne (byte_vtrue, byte_vfalse)
1812 : 30 : || (SUBREG_PROMOTED_VAR_P (vtrue)
1813 : 30 : != SUBREG_PROMOTED_VAR_P (vfalse))
1814 : 30 : || (SUBREG_PROMOTED_GET (vtrue)
1815 : 30 : != SUBREG_PROMOTED_GET (vfalse)))
1816 : : return NULL_RTX;
1817 : :
1818 : 30 : promoted_target = gen_reg_rtx (GET_MODE (reg_vtrue));
1819 : :
1820 : 60 : target = emit_conditional_move (promoted_target,
1821 : : { code, cmp_a, cmp_b, VOIDmode },
1822 : : reg_vtrue, reg_vfalse,
1823 : 30 : GET_MODE (reg_vtrue), unsignedp);
1824 : : /* Nope, couldn't do it in that mode either. */
1825 : 30 : if (!target)
1826 : : return NULL_RTX;
1827 : :
1828 : 7 : target = gen_rtx_SUBREG (GET_MODE (vtrue), promoted_target, byte_vtrue);
1829 : 7 : SUBREG_PROMOTED_VAR_P (target) = SUBREG_PROMOTED_VAR_P (vtrue);
1830 : 7 : SUBREG_PROMOTED_SET (target, SUBREG_PROMOTED_GET (vtrue));
1831 : 7 : emit_move_insn (x, target);
1832 : 7 : return x;
1833 : : }
1834 : : else
1835 : : return NULL_RTX;
1836 : : }
1837 : :
1838 : : /* Emit a conditional zero, returning TARGET or NULL_RTX upon failure.
1839 : : IF_INFO describes the if-conversion scenario under consideration.
1840 : : CZERO_CODE selects the condition (EQ/NE).
1841 : : NON_ZERO_OP is the nonzero operand of the conditional move
1842 : : TARGET is the desired output register. */
1843 : :
1844 : : static rtx
1845 : 99 : noce_emit_czero (struct noce_if_info *if_info, enum rtx_code czero_code,
1846 : : rtx non_zero_op, rtx target)
1847 : : {
1848 : 99 : machine_mode mode = GET_MODE (target);
1849 : 99 : rtx cond_op0 = XEXP (if_info->cond, 0);
1850 : 99 : rtx czero_cond
1851 : 99 : = gen_rtx_fmt_ee (czero_code, GET_MODE (cond_op0), cond_op0, const0_rtx);
1852 : 99 : rtx if_then_else
1853 : 99 : = gen_rtx_IF_THEN_ELSE (mode, czero_cond, const0_rtx, non_zero_op);
1854 : 99 : rtx set = gen_rtx_SET (target, if_then_else);
1855 : :
1856 : 99 : rtx_insn *insn = make_insn_raw (set);
1857 : :
1858 : 99 : if (recog_memoized (insn) >= 0)
1859 : : {
1860 : 0 : add_insn (insn);
1861 : 0 : return target;
1862 : : }
1863 : :
1864 : : return NULL_RTX;
1865 : : }
1866 : :
1867 : : /* Try only simple constants and registers here. More complex cases
1868 : : are handled in noce_try_cmove_arith after noce_try_store_flag_arith
1869 : : has had a go at it. */
1870 : :
1871 : : static bool
1872 : 244018 : noce_try_cmove (struct noce_if_info *if_info)
1873 : : {
1874 : 244018 : enum rtx_code code;
1875 : 244018 : rtx target;
1876 : 244018 : rtx_insn *seq;
1877 : :
1878 : 482265 : if (!noce_simple_bbs (if_info))
1879 : : return false;
1880 : :
1881 : 161136 : if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
1882 : 254602 : && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
1883 : : {
1884 : 87480 : start_sequence ();
1885 : :
1886 : 87480 : code = GET_CODE (if_info->cond);
1887 : 87480 : target = noce_emit_cmove (if_info, if_info->x, code,
1888 : : XEXP (if_info->cond, 0),
1889 : : XEXP (if_info->cond, 1),
1890 : : if_info->a, if_info->b);
1891 : :
1892 : 87480 : if (target)
1893 : : {
1894 : 60038 : if (target != if_info->x)
1895 : 0 : noce_emit_move_insn (if_info->x, target);
1896 : :
1897 : 60038 : seq = end_ifcvt_sequence (if_info);
1898 : 60038 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1899 : 18029 : return false;
1900 : :
1901 : 42009 : emit_insn_before_setloc (seq, if_info->jump,
1902 : 42009 : INSN_LOCATION (if_info->insn_a));
1903 : 42009 : if_info->transform_name = "noce_try_cmove";
1904 : :
1905 : 42009 : return true;
1906 : : }
1907 : : /* If both a and b are constants try a last-ditch transformation:
1908 : : if (test) x = a; else x = b;
1909 : : => x = (-(test != 0) & (b - a)) + a;
1910 : : Try this only if the target-specific expansion above has failed.
1911 : : The target-specific expander may want to generate sequences that
1912 : : we don't know about, so give them a chance before trying this
1913 : : approach. */
1914 : 27442 : else if (!targetm.have_conditional_execution ()
1915 : 27442 : && CONST_INT_P (if_info->a) && CONST_INT_P (if_info->b))
1916 : : {
1917 : 5045 : machine_mode mode = GET_MODE (if_info->x);
1918 : 5045 : HOST_WIDE_INT ifalse = INTVAL (if_info->a);
1919 : 5045 : HOST_WIDE_INT itrue = INTVAL (if_info->b);
1920 : 5045 : rtx target = noce_emit_store_flag (if_info, if_info->x, false, -1);
1921 : 5045 : if (!target)
1922 : : {
1923 : 4761 : end_sequence ();
1924 : 4761 : return false;
1925 : : }
1926 : :
1927 : 284 : HOST_WIDE_INT diff = (unsigned HOST_WIDE_INT) itrue - ifalse;
1928 : : /* Make sure we can represent the difference
1929 : : between the two values. */
1930 : 284 : if ((diff > 0)
1931 : 284 : != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
1932 : : {
1933 : 9 : end_sequence ();
1934 : 9 : return false;
1935 : : }
1936 : :
1937 : 275 : diff = trunc_int_for_mode (diff, mode);
1938 : 275 : target = expand_simple_binop (mode, AND,
1939 : 275 : target, gen_int_mode (diff, mode),
1940 : : if_info->x, 0, OPTAB_WIDEN);
1941 : 275 : if (target)
1942 : 275 : target = expand_simple_binop (mode, PLUS,
1943 : 275 : target, gen_int_mode (ifalse, mode),
1944 : : if_info->x, 0, OPTAB_WIDEN);
1945 : 275 : if (target)
1946 : : {
1947 : 275 : if (target != if_info->x)
1948 : 0 : noce_emit_move_insn (if_info->x, target);
1949 : :
1950 : 275 : seq = end_ifcvt_sequence (if_info);
1951 : 275 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
1952 : 250 : return false;
1953 : :
1954 : 25 : emit_insn_before_setloc (seq, if_info->jump,
1955 : 25 : INSN_LOCATION (if_info->insn_a));
1956 : 25 : if_info->transform_name = "noce_try_cmove";
1957 : 25 : return true;
1958 : : }
1959 : : else
1960 : : {
1961 : 0 : end_sequence ();
1962 : 0 : return false;
1963 : : }
1964 : : }
1965 : : else
1966 : 22397 : end_sequence ();
1967 : : }
1968 : :
1969 : : return false;
1970 : : }
1971 : :
1972 : : /* Return true if X contains a conditional code mode rtx. */
1973 : :
1974 : : static bool
1975 : 2813109 : contains_ccmode_rtx_p (rtx x)
1976 : : {
1977 : 2813109 : subrtx_iterator::array_type array;
1978 : 7040665 : FOR_EACH_SUBRTX (iter, array, x, ALL)
1979 : 4278545 : if (GET_MODE_CLASS (GET_MODE (*iter)) == MODE_CC)
1980 : 50989 : return true;
1981 : :
1982 : 2762120 : return false;
1983 : 2813109 : }
1984 : :
1985 : : /* Helper for bb_valid_for_noce_process_p. Validate that
1986 : : the rtx insn INSN is a single set that does not set
1987 : : the conditional register CC and is in general valid for
1988 : : if-conversion. */
1989 : :
1990 : : static bool
1991 : 3471937 : insn_valid_noce_process_p (rtx_insn *insn, rtx cc)
1992 : : {
1993 : 3471937 : if (!insn
1994 : 3471506 : || !NONJUMP_INSN_P (insn)
1995 : 6311099 : || (cc && set_of (cc, insn)))
1996 : 642135 : return false;
1997 : :
1998 : 2829802 : rtx sset = single_set (insn);
1999 : :
2000 : : /* Currently support only simple single sets in test_bb. */
2001 : 2829802 : if (!sset
2002 : 2826402 : || !noce_operand_ok (SET_DEST (sset))
2003 : 2813109 : || contains_ccmode_rtx_p (SET_DEST (sset))
2004 : 5591922 : || !noce_operand_ok (SET_SRC (sset)))
2005 : 130709 : return false;
2006 : :
2007 : : return true;
2008 : : }
2009 : :
2010 : :
2011 : : /* Return true iff the registers that the insns in BB_A set do not get
2012 : : used in BB_B. If TO_RENAME is non-NULL then it is a location that will be
2013 : : renamed later by the caller and so conflicts on it should be ignored
2014 : : in this function. */
2015 : :
2016 : : static bool
2017 : 71880 : bbs_ok_for_cmove_arith (basic_block bb_a, basic_block bb_b, rtx to_rename)
2018 : : {
2019 : 71880 : rtx_insn *a_insn;
2020 : 71880 : bitmap bba_sets = BITMAP_ALLOC (®_obstack);
2021 : :
2022 : 71880 : df_ref def;
2023 : 71880 : df_ref use;
2024 : :
2025 : 359910 : FOR_BB_INSNS (bb_a, a_insn)
2026 : : {
2027 : 288030 : if (!active_insn_p (a_insn))
2028 : 189632 : continue;
2029 : :
2030 : 98398 : rtx sset_a = single_set (a_insn);
2031 : :
2032 : 98398 : if (!sset_a)
2033 : : {
2034 : 0 : BITMAP_FREE (bba_sets);
2035 : 0 : return false;
2036 : : }
2037 : : /* Record all registers that BB_A sets. */
2038 : 238979 : FOR_EACH_INSN_DEF (def, a_insn)
2039 : 140581 : if (!(to_rename && DF_REF_REG (def) == to_rename))
2040 : 69513 : bitmap_set_bit (bba_sets, DF_REF_REGNO (def));
2041 : : }
2042 : :
2043 : 71880 : rtx_insn *b_insn;
2044 : :
2045 : 359875 : FOR_BB_INSNS (bb_b, b_insn)
2046 : : {
2047 : 288105 : if (!active_insn_p (b_insn))
2048 : 189703 : continue;
2049 : :
2050 : 98402 : rtx sset_b = single_set (b_insn);
2051 : :
2052 : 98402 : if (!sset_b)
2053 : : {
2054 : 0 : BITMAP_FREE (bba_sets);
2055 : 0 : return false;
2056 : : }
2057 : :
2058 : : /* Make sure this is a REG and not some instance
2059 : : of ZERO_EXTRACT or non-paradoxical SUBREG or other dangerous stuff.
2060 : : If we have a memory destination then we have a pair of simple
2061 : : basic blocks performing an operation of the form [addr] = c ? a : b.
2062 : : bb_valid_for_noce_process_p will have ensured that these are
2063 : : the only stores present. In that case [addr] should be the location
2064 : : to be renamed. Assert that the callers set this up properly. */
2065 : 98402 : if (MEM_P (SET_DEST (sset_b)))
2066 : 800 : gcc_assert (rtx_equal_p (SET_DEST (sset_b), to_rename));
2067 : 97602 : else if (!REG_P (SET_DEST (sset_b))
2068 : 97602 : && !paradoxical_subreg_p (SET_DEST (sset_b)))
2069 : : {
2070 : 47 : BITMAP_FREE (bba_sets);
2071 : 47 : return false;
2072 : : }
2073 : :
2074 : : /* If the insn uses a reg set in BB_A return false. */
2075 : 190261 : FOR_EACH_INSN_USE (use, b_insn)
2076 : : {
2077 : 91969 : if (bitmap_bit_p (bba_sets, DF_REF_REGNO (use)))
2078 : : {
2079 : 63 : BITMAP_FREE (bba_sets);
2080 : 63 : return false;
2081 : : }
2082 : : }
2083 : :
2084 : : }
2085 : :
2086 : 71770 : BITMAP_FREE (bba_sets);
2087 : 71770 : return true;
2088 : : }
2089 : :
2090 : : /* Emit copies of all the active instructions in BB except the last.
2091 : : This is a helper for noce_try_cmove_arith. */
2092 : :
2093 : : static void
2094 : 19840 : noce_emit_all_but_last (basic_block bb)
2095 : : {
2096 : 19840 : rtx_insn *last = last_active_insn (bb, false);
2097 : 19840 : rtx_insn *insn;
2098 : 167961 : FOR_BB_INSNS (bb, insn)
2099 : : {
2100 : 128281 : if (insn != last && active_insn_p (insn))
2101 : : {
2102 : 49263 : rtx_insn *to_emit = as_a <rtx_insn *> (copy_rtx (insn));
2103 : :
2104 : 49263 : emit_insn (PATTERN (to_emit));
2105 : : }
2106 : : }
2107 : 19840 : }
2108 : :
2109 : : /* Helper for noce_try_cmove_arith. Emit the pattern TO_EMIT and return
2110 : : the resulting insn or NULL if it's not a valid insn. */
2111 : :
2112 : : static rtx_insn *
2113 : 240030 : noce_emit_insn (rtx to_emit)
2114 : : {
2115 : 240030 : gcc_assert (to_emit);
2116 : 240030 : rtx_insn *insn = emit_insn (to_emit);
2117 : :
2118 : 240030 : if (recog_memoized (insn) < 0)
2119 : 215 : return NULL;
2120 : :
2121 : : return insn;
2122 : : }
2123 : :
2124 : : /* Helper for noce_try_cmove_arith. Emit a copy of the insns up to
2125 : : and including the penultimate one in BB if it is not simple
2126 : : (as indicated by SIMPLE). Then emit LAST_INSN as the last
2127 : : insn in the block. The reason for that is that LAST_INSN may
2128 : : have been modified by the preparation in noce_try_cmove_arith. */
2129 : :
2130 : : static bool
2131 : 242273 : noce_emit_bb (rtx last_insn, basic_block bb, bool simple)
2132 : : {
2133 : 242273 : if (bb && !simple)
2134 : 19840 : noce_emit_all_but_last (bb);
2135 : :
2136 : 242273 : if (last_insn && !noce_emit_insn (last_insn))
2137 : : return false;
2138 : :
2139 : : return true;
2140 : : }
2141 : :
2142 : : /* Try more complex cases involving conditional_move. */
2143 : :
2144 : : static bool
2145 : 196326 : noce_try_cmove_arith (struct noce_if_info *if_info)
2146 : : {
2147 : 196326 : rtx a = if_info->a;
2148 : 196326 : rtx b = if_info->b;
2149 : 196326 : rtx x = if_info->x;
2150 : 196326 : rtx orig_a, orig_b;
2151 : 196326 : rtx_insn *insn_a, *insn_b;
2152 : 196326 : bool a_simple = if_info->then_simple;
2153 : 196326 : bool b_simple = if_info->else_simple;
2154 : 196326 : basic_block then_bb = if_info->then_bb;
2155 : 196326 : basic_block else_bb = if_info->else_bb;
2156 : 196326 : rtx target;
2157 : 196326 : bool is_mem = false;
2158 : 196326 : enum rtx_code code;
2159 : 196326 : rtx cond = if_info->cond;
2160 : 196326 : rtx_insn *ifcvt_seq;
2161 : :
2162 : : /* A conditional move from two memory sources is equivalent to a
2163 : : conditional on their addresses followed by a load. Don't do this
2164 : : early because it'll screw alias analysis. Note that we've
2165 : : already checked for no side effects. */
2166 : 196326 : if (cse_not_expected
2167 : 67500 : && MEM_P (a) && MEM_P (b)
2168 : 200713 : && MEM_ADDR_SPACE (a) == MEM_ADDR_SPACE (b))
2169 : : {
2170 : 4387 : machine_mode address_mode = get_address_mode (a);
2171 : :
2172 : 4387 : a = XEXP (a, 0);
2173 : 4387 : b = XEXP (b, 0);
2174 : 4387 : x = gen_reg_rtx (address_mode);
2175 : 4387 : is_mem = true;
2176 : : }
2177 : :
2178 : : /* ??? We could handle this if we knew that a load from A or B could
2179 : : not trap or fault. This is also true if we've already loaded
2180 : : from the address along the path from ENTRY. */
2181 : 191939 : else if (may_trap_or_fault_p (a) || may_trap_or_fault_p (b))
2182 : 55910 : return false;
2183 : :
2184 : : /* if (test) x = a + b; else x = c - d;
2185 : : => y = a + b;
2186 : : x = c - d;
2187 : : if (test)
2188 : : x = y;
2189 : : */
2190 : :
2191 : 140416 : code = GET_CODE (cond);
2192 : 140416 : insn_a = if_info->insn_a;
2193 : 140416 : insn_b = if_info->insn_b;
2194 : :
2195 : 140416 : machine_mode x_mode = GET_MODE (x);
2196 : :
2197 : 140416 : if (!can_conditionally_move_p (x_mode))
2198 : : return false;
2199 : :
2200 : : /* Possibly rearrange operands to make things come out more natural. */
2201 : 121251 : if (noce_reversed_cond_code (if_info) != UNKNOWN)
2202 : : {
2203 : 121251 : bool reversep = false;
2204 : 121251 : if (rtx_equal_p (b, x))
2205 : : reversep = true;
2206 : 65596 : else if (general_operand (b, GET_MODE (b)))
2207 : : reversep = true;
2208 : :
2209 : : if (reversep)
2210 : : {
2211 : 111642 : if (if_info->rev_cond)
2212 : : {
2213 : 111642 : cond = if_info->rev_cond;
2214 : 111642 : code = GET_CODE (cond);
2215 : : }
2216 : : else
2217 : 0 : code = reversed_comparison_code (cond, if_info->jump);
2218 : : std::swap (a, b);
2219 : : std::swap (insn_a, insn_b);
2220 : : std::swap (a_simple, b_simple);
2221 : : std::swap (then_bb, else_bb);
2222 : : }
2223 : : }
2224 : :
2225 : 37097 : if (then_bb && else_bb
2226 : 157246 : && (!bbs_ok_for_cmove_arith (then_bb, else_bb, if_info->orig_x)
2227 : 35885 : || !bbs_ok_for_cmove_arith (else_bb, then_bb, if_info->orig_x)))
2228 : 110 : return false;
2229 : :
2230 : 121141 : start_sequence ();
2231 : :
2232 : : /* If one of the blocks is empty then the corresponding B or A value
2233 : : came from the test block. The non-empty complex block that we will
2234 : : emit might clobber the register used by B or A, so move it to a pseudo
2235 : : first. */
2236 : :
2237 : 121141 : rtx tmp_a = NULL_RTX;
2238 : 121141 : rtx tmp_b = NULL_RTX;
2239 : :
2240 : 121141 : if (b_simple || !else_bb)
2241 : 104800 : tmp_b = gen_reg_rtx (x_mode);
2242 : :
2243 : 121141 : if (a_simple || !then_bb)
2244 : 117642 : tmp_a = gen_reg_rtx (x_mode);
2245 : :
2246 : 121141 : orig_a = a;
2247 : 121141 : orig_b = b;
2248 : :
2249 : 121141 : rtx emit_a = NULL_RTX;
2250 : 121141 : rtx emit_b = NULL_RTX;
2251 : 121141 : rtx_insn *tmp_insn = NULL;
2252 : 121141 : bool modified_in_a = false;
2253 : 121141 : bool modified_in_b = false;
2254 : : /* If either operand is complex, load it into a register first.
2255 : : The best way to do this is to copy the original insn. In this
2256 : : way we preserve any clobbers etc that the insn may have had.
2257 : : This is of course not possible in the IS_MEM case. */
2258 : :
2259 : 121141 : if (! general_operand (a, GET_MODE (a)) || tmp_a)
2260 : : {
2261 : :
2262 : 120277 : if (is_mem)
2263 : : {
2264 : 4387 : rtx reg = gen_reg_rtx (GET_MODE (a));
2265 : 4387 : emit_a = gen_rtx_SET (reg, a);
2266 : : }
2267 : : else
2268 : : {
2269 : 115890 : if (insn_a)
2270 : : {
2271 : 60193 : a = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2272 : :
2273 : 60193 : rtx_insn *copy_of_a = as_a <rtx_insn *> (copy_rtx (insn_a));
2274 : 60193 : rtx set = single_set (copy_of_a);
2275 : 60193 : SET_DEST (set) = a;
2276 : :
2277 : 60193 : emit_a = PATTERN (copy_of_a);
2278 : : }
2279 : : else
2280 : : {
2281 : 55697 : rtx tmp_reg = tmp_a ? tmp_a : gen_reg_rtx (GET_MODE (a));
2282 : 55697 : emit_a = gen_rtx_SET (tmp_reg, a);
2283 : 55697 : a = tmp_reg;
2284 : : }
2285 : : }
2286 : : }
2287 : :
2288 : 121141 : if (! general_operand (b, GET_MODE (b)) || tmp_b)
2289 : : {
2290 : 119762 : if (is_mem)
2291 : : {
2292 : 4387 : rtx reg = gen_reg_rtx (GET_MODE (b));
2293 : 4387 : emit_b = gen_rtx_SET (reg, b);
2294 : : }
2295 : : else
2296 : : {
2297 : 115375 : if (insn_b)
2298 : : {
2299 : 115188 : b = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2300 : 115188 : rtx_insn *copy_of_b = as_a <rtx_insn *> (copy_rtx (insn_b));
2301 : 115188 : rtx set = single_set (copy_of_b);
2302 : :
2303 : 115188 : SET_DEST (set) = b;
2304 : 115188 : emit_b = PATTERN (copy_of_b);
2305 : : }
2306 : : else
2307 : : {
2308 : 187 : rtx tmp_reg = tmp_b ? tmp_b : gen_reg_rtx (GET_MODE (b));
2309 : 187 : emit_b = gen_rtx_SET (tmp_reg, b);
2310 : 187 : b = tmp_reg;
2311 : : }
2312 : : }
2313 : : }
2314 : :
2315 : 121141 : modified_in_a = emit_a != NULL_RTX && modified_in_p (orig_b, emit_a);
2316 : 121141 : if (tmp_b && then_bb)
2317 : : {
2318 : 115284 : FOR_BB_INSNS (then_bb, tmp_insn)
2319 : : /* Don't check inside insn_a. We will have changed it to emit_a
2320 : : with a destination that doesn't conflict. */
2321 : 88596 : if (!(insn_a && tmp_insn == insn_a)
2322 : 150504 : && modified_in_p (orig_b, tmp_insn))
2323 : : {
2324 : : modified_in_a = true;
2325 : : break;
2326 : : }
2327 : :
2328 : : }
2329 : :
2330 : 121141 : modified_in_b = emit_b != NULL_RTX && modified_in_p (orig_a, emit_b);
2331 : 121141 : if (tmp_a && else_bb)
2332 : : {
2333 : 483826 : FOR_BB_INSNS (else_bb, tmp_insn)
2334 : : /* Don't check inside insn_b. We will have changed it to emit_b
2335 : : with a destination that doesn't conflict. */
2336 : 367215 : if (!(insn_b && tmp_insn == insn_b)
2337 : 617819 : && modified_in_p (orig_a, tmp_insn))
2338 : : {
2339 : : modified_in_b = true;
2340 : : break;
2341 : : }
2342 : : }
2343 : :
2344 : : /* If insn to set up A clobbers any registers B depends on, try to
2345 : : swap insn that sets up A with the one that sets up B. If even
2346 : : that doesn't help, punt. */
2347 : 121141 : if (modified_in_a && !modified_in_b)
2348 : : {
2349 : 0 : if (!noce_emit_bb (emit_b, else_bb, b_simple))
2350 : 0 : goto end_seq_and_fail;
2351 : :
2352 : 0 : if (!noce_emit_bb (emit_a, then_bb, a_simple))
2353 : 0 : goto end_seq_and_fail;
2354 : : }
2355 : 121141 : else if (!modified_in_a)
2356 : : {
2357 : 121141 : if (!noce_emit_bb (emit_a, then_bb, a_simple))
2358 : 9 : goto end_seq_and_fail;
2359 : :
2360 : 121132 : if (!noce_emit_bb (emit_b, else_bb, b_simple))
2361 : 206 : goto end_seq_and_fail;
2362 : : }
2363 : : else
2364 : 0 : goto end_seq_and_fail;
2365 : :
2366 : 120926 : target = noce_emit_cmove (if_info, x, code, XEXP (cond, 0), XEXP (cond, 1),
2367 : : a, b);
2368 : :
2369 : 120926 : if (! target)
2370 : 29531 : goto end_seq_and_fail;
2371 : :
2372 : : /* If we're handling a memory for above, emit the load now. */
2373 : 91395 : if (is_mem)
2374 : : {
2375 : 2271 : rtx mem = gen_rtx_MEM (GET_MODE (if_info->x), target);
2376 : :
2377 : : /* Copy over flags as appropriate. */
2378 : 2271 : if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
2379 : 0 : MEM_VOLATILE_P (mem) = 1;
2380 : 2271 : if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
2381 : 2241 : set_mem_alias_set (mem, MEM_ALIAS_SET (if_info->a));
2382 : 4542 : set_mem_align (mem,
2383 : 2271 : MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
2384 : :
2385 : 2271 : gcc_assert (MEM_ADDR_SPACE (if_info->a) == MEM_ADDR_SPACE (if_info->b));
2386 : 2271 : set_mem_addr_space (mem, MEM_ADDR_SPACE (if_info->a));
2387 : :
2388 : 2271 : noce_emit_move_insn (if_info->x, mem);
2389 : : }
2390 : 89124 : else if (target != x)
2391 : 0 : noce_emit_move_insn (x, target);
2392 : :
2393 : 91395 : ifcvt_seq = end_ifcvt_sequence (if_info);
2394 : 91395 : if (!ifcvt_seq || !targetm.noce_conversion_profitable_p (ifcvt_seq, if_info))
2395 : 55773 : return false;
2396 : :
2397 : 35622 : emit_insn_before_setloc (ifcvt_seq, if_info->jump,
2398 : 35622 : INSN_LOCATION (if_info->insn_a));
2399 : 35622 : if_info->transform_name = "noce_try_cmove_arith";
2400 : 35622 : return true;
2401 : :
2402 : 29746 : end_seq_and_fail:
2403 : 29746 : end_sequence ();
2404 : 29746 : return false;
2405 : : }
2406 : :
2407 : : /* For most cases, the simplified condition we found is the best
2408 : : choice, but this is not the case for the min/max/abs transforms.
2409 : : For these we wish to know that it is A or B in the condition. */
2410 : :
2411 : : static rtx
2412 : 166126 : noce_get_alt_condition (struct noce_if_info *if_info, rtx target,
2413 : : rtx_insn **earliest)
2414 : : {
2415 : 166126 : rtx cond, set;
2416 : 166126 : rtx_insn *insn;
2417 : 166126 : bool reverse;
2418 : :
2419 : : /* If target is already mentioned in the known condition, return it. */
2420 : 166126 : if (reg_mentioned_p (target, if_info->cond))
2421 : : {
2422 : 8131 : *earliest = if_info->cond_earliest;
2423 : 8131 : return if_info->cond;
2424 : : }
2425 : :
2426 : 157995 : set = pc_set (if_info->jump);
2427 : 157995 : cond = XEXP (SET_SRC (set), 0);
2428 : 157995 : reverse
2429 : 315990 : = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
2430 : 157995 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (if_info->jump);
2431 : 157995 : if (if_info->then_else_reversed)
2432 : 25135 : reverse = !reverse;
2433 : :
2434 : : /* If we're looking for a constant, try to make the conditional
2435 : : have that constant in it. There are two reasons why it may
2436 : : not have the constant we want:
2437 : :
2438 : : 1. GCC may have needed to put the constant in a register, because
2439 : : the target can't compare directly against that constant. For
2440 : : this case, we look for a SET immediately before the comparison
2441 : : that puts a constant in that register.
2442 : :
2443 : : 2. GCC may have canonicalized the conditional, for example
2444 : : replacing "if x < 4" with "if x <= 3". We can undo that (or
2445 : : make equivalent types of changes) to get the constants we need
2446 : : if they're off by one in the right direction. */
2447 : :
2448 : 157995 : if (CONST_INT_P (target))
2449 : : {
2450 : 44775 : enum rtx_code code = GET_CODE (if_info->cond);
2451 : 44775 : rtx op_a = XEXP (if_info->cond, 0);
2452 : 44775 : rtx op_b = XEXP (if_info->cond, 1);
2453 : 44775 : rtx_insn *prev_insn;
2454 : :
2455 : : /* First, look to see if we put a constant in a register. */
2456 : 44775 : prev_insn = prev_nonnote_nondebug_insn (if_info->cond_earliest);
2457 : 44775 : if (prev_insn
2458 : 44671 : && BLOCK_FOR_INSN (prev_insn)
2459 : 44671 : == BLOCK_FOR_INSN (if_info->cond_earliest)
2460 : 36028 : && INSN_P (prev_insn)
2461 : 76441 : && GET_CODE (PATTERN (prev_insn)) == SET)
2462 : : {
2463 : 24892 : rtx src = find_reg_equal_equiv_note (prev_insn);
2464 : 24892 : if (!src)
2465 : 24784 : src = SET_SRC (PATTERN (prev_insn));
2466 : 24892 : if (CONST_INT_P (src))
2467 : : {
2468 : 13070 : if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
2469 : : op_a = src;
2470 : 12821 : else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
2471 : 1073 : op_b = src;
2472 : :
2473 : 13070 : if (CONST_INT_P (op_a))
2474 : : {
2475 : 249 : std::swap (op_a, op_b);
2476 : 249 : code = swap_condition (code);
2477 : : }
2478 : : }
2479 : : }
2480 : :
2481 : : /* Now, look to see if we can get the right constant by
2482 : : adjusting the conditional. */
2483 : 44775 : if (CONST_INT_P (op_b))
2484 : : {
2485 : 22063 : HOST_WIDE_INT desired_val = INTVAL (target);
2486 : 22063 : HOST_WIDE_INT actual_val = INTVAL (op_b);
2487 : :
2488 : 22063 : switch (code)
2489 : : {
2490 : 847 : case LT:
2491 : 847 : if (desired_val != HOST_WIDE_INT_MAX
2492 : 829 : && actual_val == desired_val + 1)
2493 : : {
2494 : 156 : code = LE;
2495 : 156 : op_b = GEN_INT (desired_val);
2496 : : }
2497 : : break;
2498 : 3 : case LE:
2499 : 3 : if (desired_val != HOST_WIDE_INT_MIN
2500 : 3 : && actual_val == desired_val - 1)
2501 : : {
2502 : 0 : code = LT;
2503 : 0 : op_b = GEN_INT (desired_val);
2504 : : }
2505 : : break;
2506 : 1563 : case GT:
2507 : 1563 : if (desired_val != HOST_WIDE_INT_MIN
2508 : 1563 : && actual_val == desired_val - 1)
2509 : : {
2510 : 241 : code = GE;
2511 : 241 : op_b = GEN_INT (desired_val);
2512 : : }
2513 : : break;
2514 : 39 : case GE:
2515 : 39 : if (desired_val != HOST_WIDE_INT_MAX
2516 : 24 : && actual_val == desired_val + 1)
2517 : : {
2518 : 3 : code = GT;
2519 : 3 : op_b = GEN_INT (desired_val);
2520 : : }
2521 : : break;
2522 : : default:
2523 : : break;
2524 : : }
2525 : : }
2526 : :
2527 : : /* If we made any changes, generate a new conditional that is
2528 : : equivalent to what we started with, but has the right
2529 : : constants in it. */
2530 : 44775 : if (code != GET_CODE (if_info->cond)
2531 : 44126 : || op_a != XEXP (if_info->cond, 0)
2532 : 44126 : || op_b != XEXP (if_info->cond, 1))
2533 : : {
2534 : 1722 : cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
2535 : 1722 : *earliest = if_info->cond_earliest;
2536 : 1722 : return cond;
2537 : : }
2538 : : }
2539 : :
2540 : 156273 : cond = canonicalize_condition (if_info->jump, cond, reverse,
2541 : : earliest, target, have_cbranchcc4, true);
2542 : 156273 : if (! cond || ! reg_mentioned_p (target, cond))
2543 : 156273 : return NULL;
2544 : :
2545 : : /* We almost certainly searched back to a different place.
2546 : : Need to re-verify correct lifetimes. */
2547 : :
2548 : : /* X may not be mentioned in the range (cond_earliest, jump]. */
2549 : 0 : for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
2550 : 0 : if (INSN_P (insn) && reg_overlap_mentioned_p (if_info->x, PATTERN (insn)))
2551 : : return NULL;
2552 : :
2553 : : /* A and B may not be modified in the range [cond_earliest, jump). */
2554 : 0 : for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
2555 : 0 : if (INSN_P (insn)
2556 : 0 : && (modified_in_p (if_info->a, insn)
2557 : 0 : || modified_in_p (if_info->b, insn)))
2558 : 0 : return NULL;
2559 : :
2560 : : return cond;
2561 : : }
2562 : :
2563 : : /* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc. */
2564 : :
2565 : : static bool
2566 : 246211 : noce_try_minmax (struct noce_if_info *if_info)
2567 : : {
2568 : 246211 : rtx cond, target;
2569 : 246211 : rtx_insn *earliest, *seq;
2570 : 246211 : enum rtx_code code, op;
2571 : 246211 : bool unsignedp;
2572 : :
2573 : 530747 : if (!noce_simple_bbs (if_info))
2574 : : return false;
2575 : :
2576 : : /* ??? Reject modes with NaNs or signed zeros since we don't know how
2577 : : they will be resolved with an SMIN/SMAX. It wouldn't be too hard
2578 : : to get the target to tell us... */
2579 : 223438 : if (HONOR_SIGNED_ZEROS (if_info->x)
2580 : 223438 : || HONOR_NANS (if_info->x))
2581 : 58444 : return false;
2582 : :
2583 : 164994 : cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
2584 : 164994 : if (!cond)
2585 : : return false;
2586 : :
2587 : : /* Verify the condition is of the form we expect, and canonicalize
2588 : : the comparison code. */
2589 : 9822 : code = GET_CODE (cond);
2590 : 9822 : if (rtx_equal_p (XEXP (cond, 0), if_info->a))
2591 : : {
2592 : 2280 : if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
2593 : : return false;
2594 : : }
2595 : 7542 : else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
2596 : : {
2597 : 5146 : if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
2598 : : return false;
2599 : 40 : code = swap_condition (code);
2600 : : }
2601 : : else
2602 : : return false;
2603 : :
2604 : : /* Determine what sort of operation this is. Note that the code is for
2605 : : a taken branch, so the code->operation mapping appears backwards. */
2606 : 59 : switch (code)
2607 : : {
2608 : : case LT:
2609 : : case LE:
2610 : : case UNLT:
2611 : : case UNLE:
2612 : : op = SMAX;
2613 : : unsignedp = false;
2614 : : break;
2615 : 8 : case GT:
2616 : 8 : case GE:
2617 : 8 : case UNGT:
2618 : 8 : case UNGE:
2619 : 8 : op = SMIN;
2620 : 8 : unsignedp = false;
2621 : 8 : break;
2622 : 3 : case LTU:
2623 : 3 : case LEU:
2624 : 3 : op = UMAX;
2625 : 3 : unsignedp = true;
2626 : 3 : break;
2627 : 22 : case GTU:
2628 : 22 : case GEU:
2629 : 22 : op = UMIN;
2630 : 22 : unsignedp = true;
2631 : 22 : break;
2632 : : default:
2633 : : return false;
2634 : : }
2635 : :
2636 : 59 : start_sequence ();
2637 : :
2638 : 59 : target = expand_simple_binop (GET_MODE (if_info->x), op,
2639 : : if_info->a, if_info->b,
2640 : : if_info->x, unsignedp, OPTAB_WIDEN);
2641 : 59 : if (! target)
2642 : : {
2643 : 0 : end_sequence ();
2644 : 0 : return false;
2645 : : }
2646 : 59 : if (target != if_info->x)
2647 : 0 : noce_emit_move_insn (if_info->x, target);
2648 : :
2649 : 59 : seq = end_ifcvt_sequence (if_info);
2650 : 59 : if (!seq)
2651 : : return false;
2652 : :
2653 : 59 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2654 : 59 : if_info->cond = cond;
2655 : 59 : if_info->cond_earliest = earliest;
2656 : 59 : if_info->rev_cond = NULL_RTX;
2657 : 59 : if_info->transform_name = "noce_try_minmax";
2658 : :
2659 : 59 : return true;
2660 : : }
2661 : :
2662 : : /* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);",
2663 : : "if (a < 0) x = ~a; else x = a;" to "x = one_cmpl_abs(a);",
2664 : : etc. */
2665 : :
2666 : : static bool
2667 : 246152 : noce_try_abs (struct noce_if_info *if_info)
2668 : : {
2669 : 246152 : rtx cond, target, a, b, c;
2670 : 246152 : rtx_insn *earliest, *seq;
2671 : 246152 : bool negate;
2672 : 246152 : bool one_cmpl = false;
2673 : :
2674 : 530650 : if (!noce_simple_bbs (if_info))
2675 : : return false;
2676 : :
2677 : : /* Reject modes with signed zeros. */
2678 : 223379 : if (HONOR_SIGNED_ZEROS (if_info->x))
2679 : : return false;
2680 : :
2681 : : /* Recognize A and B as constituting an ABS or NABS. The canonical
2682 : : form is a branch around the negation, taken when the object is the
2683 : : first operand of a comparison against 0 that evaluates to true. */
2684 : 164944 : a = if_info->a;
2685 : 164944 : b = if_info->b;
2686 : 164944 : if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
2687 : : negate = false;
2688 : 163828 : else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
2689 : : {
2690 : : std::swap (a, b);
2691 : : negate = true;
2692 : : }
2693 : 163812 : else if (GET_CODE (a) == NOT && rtx_equal_p (XEXP (a, 0), b))
2694 : : {
2695 : : negate = false;
2696 : : one_cmpl = true;
2697 : : }
2698 : 163812 : else if (GET_CODE (b) == NOT && rtx_equal_p (XEXP (b, 0), a))
2699 : : {
2700 : : std::swap (a, b);
2701 : : negate = true;
2702 : : one_cmpl = true;
2703 : : }
2704 : : else
2705 : 163812 : return false;
2706 : :
2707 : 1132 : cond = noce_get_alt_condition (if_info, b, &earliest);
2708 : 1132 : if (!cond)
2709 : : return false;
2710 : :
2711 : : /* Verify the condition is of the form we expect. */
2712 : 31 : if (rtx_equal_p (XEXP (cond, 0), b))
2713 : 31 : c = XEXP (cond, 1);
2714 : 0 : else if (rtx_equal_p (XEXP (cond, 1), b))
2715 : : {
2716 : 0 : c = XEXP (cond, 0);
2717 : 0 : negate = !negate;
2718 : : }
2719 : : else
2720 : : return false;
2721 : :
2722 : : /* Verify that C is zero. Search one step backward for a
2723 : : REG_EQUAL note or a simple source if necessary. */
2724 : 31 : if (REG_P (c))
2725 : : {
2726 : 0 : rtx set;
2727 : 0 : rtx_insn *insn = prev_nonnote_nondebug_insn (earliest);
2728 : 0 : if (insn
2729 : 0 : && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (earliest)
2730 : 0 : && (set = single_set (insn))
2731 : 0 : && rtx_equal_p (SET_DEST (set), c))
2732 : : {
2733 : 0 : rtx note = find_reg_equal_equiv_note (insn);
2734 : 0 : if (note)
2735 : 0 : c = XEXP (note, 0);
2736 : : else
2737 : 0 : c = SET_SRC (set);
2738 : : }
2739 : : else
2740 : 0 : return false;
2741 : : }
2742 : 31 : if (MEM_P (c)
2743 : 0 : && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
2744 : 31 : && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
2745 : 0 : c = get_pool_constant (XEXP (c, 0));
2746 : :
2747 : : /* Work around funny ideas get_condition has wrt canonicalization.
2748 : : Note that these rtx constants are known to be CONST_INT, and
2749 : : therefore imply integer comparisons.
2750 : : The one_cmpl case is more complicated, as we want to handle
2751 : : only x < 0 ? ~x : x or x >= 0 ? x : ~x to one_cmpl_abs (x)
2752 : : and x < 0 ? x : ~x or x >= 0 ? ~x : x to ~one_cmpl_abs (x),
2753 : : but not other cases (x > -1 is equivalent of x >= 0). */
2754 : 31 : if (c == constm1_rtx && GET_CODE (cond) == GT)
2755 : : ;
2756 : 7 : else if (c == const1_rtx && GET_CODE (cond) == LT)
2757 : : {
2758 : 0 : if (one_cmpl)
2759 : : return false;
2760 : : }
2761 : 7 : else if (c == CONST0_RTX (GET_MODE (b)))
2762 : : {
2763 : 0 : if (one_cmpl
2764 : 0 : && GET_CODE (cond) != GE
2765 : 0 : && GET_CODE (cond) != LT)
2766 : : return false;
2767 : : }
2768 : : else
2769 : : return false;
2770 : :
2771 : : /* Determine what sort of operation this is. */
2772 : 24 : switch (GET_CODE (cond))
2773 : : {
2774 : 0 : case LT:
2775 : 0 : case LE:
2776 : 0 : case UNLT:
2777 : 0 : case UNLE:
2778 : 0 : negate = !negate;
2779 : 0 : break;
2780 : : case GT:
2781 : : case GE:
2782 : : case UNGT:
2783 : : case UNGE:
2784 : : break;
2785 : : default:
2786 : : return false;
2787 : : }
2788 : :
2789 : 24 : start_sequence ();
2790 : 24 : if (one_cmpl)
2791 : 0 : target = expand_one_cmpl_abs_nojump (GET_MODE (if_info->x), b,
2792 : : if_info->x);
2793 : : else
2794 : 24 : target = expand_abs_nojump (GET_MODE (if_info->x), b, if_info->x, 1);
2795 : :
2796 : : /* ??? It's a quandary whether cmove would be better here, especially
2797 : : for integers. Perhaps combine will clean things up. */
2798 : 24 : if (target && negate)
2799 : : {
2800 : 0 : if (one_cmpl)
2801 : 0 : target = expand_simple_unop (GET_MODE (target), NOT, target,
2802 : : if_info->x, 0);
2803 : : else
2804 : 0 : target = expand_simple_unop (GET_MODE (target), NEG, target,
2805 : : if_info->x, 0);
2806 : : }
2807 : :
2808 : 24 : if (! target)
2809 : : {
2810 : 0 : end_sequence ();
2811 : 0 : return false;
2812 : : }
2813 : :
2814 : 24 : if (target != if_info->x)
2815 : 0 : noce_emit_move_insn (if_info->x, target);
2816 : :
2817 : 24 : seq = end_ifcvt_sequence (if_info);
2818 : 24 : if (!seq)
2819 : : return false;
2820 : :
2821 : 24 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2822 : 24 : if_info->cond = cond;
2823 : 24 : if_info->cond_earliest = earliest;
2824 : 24 : if_info->rev_cond = NULL_RTX;
2825 : 24 : if_info->transform_name = "noce_try_abs";
2826 : :
2827 : 24 : return true;
2828 : : }
2829 : :
2830 : : /* Convert "if (m < 0) x = b; else x = 0;" to "x = (m >> C) & b;". */
2831 : :
2832 : : static bool
2833 : 160704 : noce_try_sign_mask (struct noce_if_info *if_info)
2834 : : {
2835 : 160704 : rtx cond, t, m, c;
2836 : 160704 : rtx_insn *seq;
2837 : 160704 : machine_mode mode;
2838 : 160704 : enum rtx_code code;
2839 : 160704 : bool t_unconditional;
2840 : :
2841 : 331934 : if (!noce_simple_bbs (if_info))
2842 : : return false;
2843 : :
2844 : 143946 : cond = if_info->cond;
2845 : 143946 : code = GET_CODE (cond);
2846 : 143946 : m = XEXP (cond, 0);
2847 : 143946 : c = XEXP (cond, 1);
2848 : :
2849 : 143946 : t = NULL_RTX;
2850 : 143946 : if (if_info->a == const0_rtx)
2851 : : {
2852 : 2601 : if ((code == LT && c == const0_rtx)
2853 : 2566 : || (code == LE && c == constm1_rtx))
2854 : 35 : t = if_info->b;
2855 : : }
2856 : 141345 : else if (if_info->b == const0_rtx)
2857 : : {
2858 : 13854 : if ((code == GE && c == const0_rtx)
2859 : 13854 : || (code == GT && c == constm1_rtx))
2860 : : t = if_info->a;
2861 : : }
2862 : :
2863 : 136 : if (! t || side_effects_p (t))
2864 : 143810 : return false;
2865 : :
2866 : : /* We currently don't handle different modes. */
2867 : 136 : mode = GET_MODE (t);
2868 : 136 : if (GET_MODE (m) != mode)
2869 : : return false;
2870 : :
2871 : : /* This is only profitable if T is unconditionally executed/evaluated in the
2872 : : original insn sequence or T is cheap and can't trap or fault. The former
2873 : : happens if B is the non-zero (T) value and if INSN_B was taken from
2874 : : TEST_BB, or there was no INSN_B which can happen for e.g. conditional
2875 : : stores to memory. For the cost computation use the block TEST_BB where
2876 : : the evaluation will end up after the transformation. */
2877 : 64 : t_unconditional
2878 : 149 : = (t == if_info->b
2879 : 64 : && (if_info->insn_b == NULL_RTX
2880 : 0 : || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb));
2881 : 128 : if (!(t_unconditional
2882 : 64 : || ((set_src_cost (t, mode, if_info->speed_p)
2883 : : < COSTS_N_INSNS (2))
2884 : 58 : && !may_trap_or_fault_p (t))))
2885 : 43 : return false;
2886 : :
2887 : 21 : if (!noce_can_force_operand (t))
2888 : : return false;
2889 : :
2890 : 0 : start_sequence ();
2891 : : /* Use emit_store_flag to generate "m < 0 ? -1 : 0" instead of expanding
2892 : : "(signed) m >> 31" directly. This benefits targets with specialized
2893 : : insns to obtain the signmask, but still uses ashr_optab otherwise. */
2894 : 0 : m = emit_store_flag (gen_reg_rtx (mode), LT, m, const0_rtx, mode, 0, -1);
2895 : 0 : t = m ? expand_binop (mode, and_optab, m, t, NULL_RTX, 0, OPTAB_DIRECT)
2896 : : : NULL_RTX;
2897 : :
2898 : 0 : if (!t)
2899 : : {
2900 : 0 : end_sequence ();
2901 : 0 : return false;
2902 : : }
2903 : :
2904 : 0 : noce_emit_move_insn (if_info->x, t);
2905 : :
2906 : 0 : seq = end_ifcvt_sequence (if_info);
2907 : 0 : if (!seq)
2908 : : return false;
2909 : :
2910 : 0 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
2911 : 0 : if_info->transform_name = "noce_try_sign_mask";
2912 : :
2913 : 0 : return true;
2914 : : }
2915 : :
2916 : : /* Check if OP is supported by conditional zero based if conversion,
2917 : : returning TRUE if satisfied otherwise FALSE.
2918 : :
2919 : : OP is the operation to check. */
2920 : :
2921 : : static bool
2922 : 90546 : noce_cond_zero_binary_op_supported (rtx op)
2923 : : {
2924 : 90546 : enum rtx_code opcode = GET_CODE (op);
2925 : :
2926 : 90546 : if (opcode == PLUS || opcode == MINUS || opcode == IOR || opcode == XOR
2927 : : || opcode == ASHIFT || opcode == ASHIFTRT || opcode == LSHIFTRT
2928 : : || opcode == ROTATE || opcode == ROTATERT || opcode == AND)
2929 : 4258 : return true;
2930 : :
2931 : : return false;
2932 : : }
2933 : :
2934 : : /* Helper function to return REG itself,
2935 : : otherwise NULL_RTX for other RTX_CODE. */
2936 : :
2937 : : static rtx
2938 : 4112 : get_base_reg (rtx exp)
2939 : : {
2940 : 0 : if (REG_P (exp))
2941 : 0 : return exp;
2942 : :
2943 : : return NULL_RTX;
2944 : : }
2945 : :
2946 : : /* Check if IF-BB and THEN-BB satisfy the condition for conditional zero
2947 : : based if conversion, returning TRUE if satisfied otherwise FALSE.
2948 : :
2949 : : IF_INFO describes the if-conversion scenario under consideration.
2950 : : COMMON_PTR points to the common REG of canonicalized IF_INFO->A and
2951 : : IF_INFO->B.
2952 : : CZERO_CODE_PTR points to the comparison code to use in czero RTX.
2953 : : A_PTR points to the A expression of canonicalized IF_INFO->A.
2954 : : TO_REPLACE points to the RTX to be replaced by czero RTX destnation. */
2955 : :
2956 : : static bool
2957 : 196326 : noce_bbs_ok_for_cond_zero_arith (struct noce_if_info *if_info, rtx *common_ptr,
2958 : : rtx *bin_exp_ptr,
2959 : : enum rtx_code *czero_code_ptr, rtx *a_ptr,
2960 : : rtx **to_replace)
2961 : : {
2962 : 196326 : rtx common = NULL_RTX;
2963 : 196326 : rtx cond = if_info->cond;
2964 : 196326 : rtx a = copy_rtx (if_info->a);
2965 : 196326 : rtx b = copy_rtx (if_info->b);
2966 : 196326 : rtx bin_op1 = NULL_RTX;
2967 : 196326 : enum rtx_code czero_code = UNKNOWN;
2968 : 196326 : bool reverse = false;
2969 : 196326 : rtx op0, op1, bin_exp;
2970 : :
2971 : 413183 : if (!noce_simple_bbs (if_info))
2972 : : return false;
2973 : :
2974 : : /* COND must be EQ or NE comparision of a reg and 0. */
2975 : 173553 : if (GET_CODE (cond) != NE && GET_CODE (cond) != EQ)
2976 : : return false;
2977 : 142186 : if (!REG_P (XEXP (cond, 0)) || !rtx_equal_p (XEXP (cond, 1), const0_rtx))
2978 : 62636 : return false;
2979 : :
2980 : : /* Canonicalize x = y : (y op z) to x = (y op z) : y. */
2981 : 79550 : if (REG_P (a) && noce_cond_zero_binary_op_supported (b))
2982 : : {
2983 : : std::swap (a, b);
2984 : : reverse = !reverse;
2985 : : }
2986 : :
2987 : : /* Check if x = (y op z) : y is supported by czero based ifcvt. */
2988 : 79550 : if (!(noce_cond_zero_binary_op_supported (a) && REG_P (b)))
2989 : : return false;
2990 : :
2991 : 2056 : bin_exp = a;
2992 : :
2993 : : /* Canonicalize x = (z op y) : y to x = (y op z) : y */
2994 : 2056 : op1 = get_base_reg (XEXP (bin_exp, 1));
2995 : 149 : if (op1 && rtx_equal_p (op1, b) && COMMUTATIVE_ARITH_P (bin_exp))
2996 : 1 : std::swap (XEXP (bin_exp, 0), XEXP (bin_exp, 1));
2997 : :
2998 : 2056 : op0 = get_base_reg (XEXP (bin_exp, 0));
2999 : 1962 : if (op0 && rtx_equal_p (op0, b))
3000 : : {
3001 : 1432 : common = b;
3002 : 1432 : bin_op1 = XEXP (bin_exp, 1);
3003 : 2864 : czero_code = (reverse ^ (GET_CODE (bin_exp) == AND))
3004 : 1432 : ? noce_reversed_cond_code (if_info)
3005 : 1400 : : GET_CODE (cond);
3006 : : }
3007 : : else
3008 : 624 : return false;
3009 : :
3010 : 1432 : if (czero_code == UNKNOWN)
3011 : : return false;
3012 : :
3013 : 1432 : if (REG_P (bin_op1))
3014 : 99 : *to_replace = &XEXP (bin_exp, 1);
3015 : : else
3016 : : return false;
3017 : :
3018 : 99 : *common_ptr = common;
3019 : 99 : *bin_exp_ptr = bin_exp;
3020 : 99 : *czero_code_ptr = czero_code;
3021 : 99 : *a_ptr = a;
3022 : :
3023 : 99 : return true;
3024 : : }
3025 : :
3026 : : /* Try to covert if-then-else with conditional zero,
3027 : : returning TURE on success or FALSE on failure.
3028 : : IF_INFO describes the if-conversion scenario under consideration. */
3029 : :
3030 : : static int
3031 : 196326 : noce_try_cond_zero_arith (struct noce_if_info *if_info)
3032 : : {
3033 : 196326 : rtx target, rtmp, a;
3034 : 196326 : rtx_insn *seq;
3035 : 196326 : machine_mode mode = GET_MODE (if_info->x);
3036 : 196326 : rtx common = NULL_RTX;
3037 : 196326 : enum rtx_code czero_code = UNKNOWN;
3038 : 196326 : rtx bin_exp = NULL_RTX;
3039 : 196326 : enum rtx_code bin_code = UNKNOWN;
3040 : 196326 : rtx non_zero_op = NULL_RTX;
3041 : 196326 : rtx *to_replace = NULL;
3042 : :
3043 : 196326 : if (!noce_bbs_ok_for_cond_zero_arith (if_info, &common, &bin_exp, &czero_code,
3044 : : &a, &to_replace))
3045 : : return false;
3046 : :
3047 : 99 : start_sequence ();
3048 : :
3049 : 99 : bin_code = GET_CODE (bin_exp);
3050 : :
3051 : 99 : if (bin_code == AND)
3052 : : {
3053 : 2 : rtmp = gen_reg_rtx (mode);
3054 : 2 : noce_emit_move_insn (rtmp, a);
3055 : :
3056 : 2 : target = noce_emit_czero (if_info, czero_code, common, if_info->x);
3057 : 2 : if (!target)
3058 : : {
3059 : 2 : end_sequence ();
3060 : 2 : return false;
3061 : : }
3062 : :
3063 : 0 : target = expand_simple_binop (mode, IOR, rtmp, target, if_info->x, 0,
3064 : : OPTAB_WIDEN);
3065 : 0 : if (!target)
3066 : : {
3067 : 0 : end_sequence ();
3068 : 0 : return false;
3069 : : }
3070 : :
3071 : 0 : if (target != if_info->x)
3072 : 0 : noce_emit_move_insn (if_info->x, target);
3073 : : }
3074 : : else
3075 : : {
3076 : 97 : non_zero_op = *to_replace;
3077 : : /* If x is used in both input and out like x = c ? x + z : x,
3078 : : use a new reg to avoid modifying x */
3079 : 97 : if (common && rtx_equal_p (common, if_info->x))
3080 : 65 : target = gen_reg_rtx (mode);
3081 : : else
3082 : 32 : target = if_info->x;
3083 : :
3084 : 97 : target = noce_emit_czero (if_info, czero_code, non_zero_op, target);
3085 : 97 : if (!target || !to_replace)
3086 : : {
3087 : 97 : end_sequence ();
3088 : 97 : return false;
3089 : : }
3090 : :
3091 : 0 : *to_replace = target;
3092 : 0 : noce_emit_move_insn (if_info->x, a);
3093 : : }
3094 : :
3095 : 0 : seq = end_ifcvt_sequence (if_info);
3096 : 0 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
3097 : 0 : return false;
3098 : :
3099 : 0 : emit_insn_before_setloc (seq, if_info->jump, INSN_LOCATION (if_info->insn_a));
3100 : 0 : if_info->transform_name = "noce_try_cond_zero_arith";
3101 : 0 : return true;
3102 : : }
3103 : :
3104 : : /* Optimize away "if (x & C) x |= C" and similar bit manipulation
3105 : : transformations. */
3106 : :
3107 : : static bool
3108 : 246211 : noce_try_bitop (struct noce_if_info *if_info)
3109 : : {
3110 : 246211 : rtx cond, x, a, result;
3111 : 246211 : rtx_insn *seq;
3112 : 246211 : scalar_int_mode mode;
3113 : 246211 : enum rtx_code code;
3114 : 246211 : int bitnum;
3115 : :
3116 : 246211 : x = if_info->x;
3117 : 246211 : cond = if_info->cond;
3118 : 246211 : code = GET_CODE (cond);
3119 : :
3120 : : /* Check for an integer operation. */
3121 : 246211 : if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
3122 : : return false;
3123 : :
3124 : 465844 : if (!noce_simple_bbs (if_info))
3125 : : return false;
3126 : :
3127 : : /* Check for no else condition. */
3128 : 163658 : if (! rtx_equal_p (x, if_info->b))
3129 : : return false;
3130 : :
3131 : : /* Check for a suitable condition. */
3132 : 89458 : if (code != NE && code != EQ)
3133 : : return false;
3134 : 62988 : if (XEXP (cond, 1) != const0_rtx)
3135 : : return false;
3136 : 43086 : cond = XEXP (cond, 0);
3137 : :
3138 : : /* ??? We could also handle AND here. */
3139 : 43086 : if (GET_CODE (cond) == ZERO_EXTRACT)
3140 : : {
3141 : 412 : if (XEXP (cond, 1) != const1_rtx
3142 : 192 : || !CONST_INT_P (XEXP (cond, 2))
3143 : 604 : || ! rtx_equal_p (x, XEXP (cond, 0)))
3144 : 409 : return false;
3145 : 3 : bitnum = INTVAL (XEXP (cond, 2));
3146 : 3 : if (BITS_BIG_ENDIAN)
3147 : : bitnum = GET_MODE_BITSIZE (mode) - 1 - bitnum;
3148 : 3 : if (bitnum < 0 || bitnum >= HOST_BITS_PER_WIDE_INT)
3149 : : return false;
3150 : : }
3151 : : else
3152 : : return false;
3153 : :
3154 : 3 : a = if_info->a;
3155 : 3 : if (GET_CODE (a) == IOR || GET_CODE (a) == XOR)
3156 : : {
3157 : : /* Check for "if (X & C) x = x op C". */
3158 : 0 : if (! rtx_equal_p (x, XEXP (a, 0))
3159 : 0 : || !CONST_INT_P (XEXP (a, 1))
3160 : 0 : || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3161 : 0 : != HOST_WIDE_INT_1U << bitnum)
3162 : : return false;
3163 : :
3164 : : /* if ((x & C) == 0) x |= C; is transformed to x |= C. */
3165 : : /* if ((x & C) != 0) x |= C; is transformed to nothing. */
3166 : 0 : if (GET_CODE (a) == IOR)
3167 : 0 : result = (code == NE) ? a : NULL_RTX;
3168 : 0 : else if (code == NE)
3169 : : {
3170 : : /* if ((x & C) == 0) x ^= C; is transformed to x |= C. */
3171 : 0 : result = gen_int_mode (HOST_WIDE_INT_1 << bitnum, mode);
3172 : 0 : result = simplify_gen_binary (IOR, mode, x, result);
3173 : : }
3174 : : else
3175 : : {
3176 : : /* if ((x & C) != 0) x ^= C; is transformed to x &= ~C. */
3177 : 0 : result = gen_int_mode (~(HOST_WIDE_INT_1 << bitnum), mode);
3178 : 0 : result = simplify_gen_binary (AND, mode, x, result);
3179 : : }
3180 : : }
3181 : 3 : else if (GET_CODE (a) == AND)
3182 : : {
3183 : : /* Check for "if (X & C) x &= ~C". */
3184 : 0 : if (! rtx_equal_p (x, XEXP (a, 0))
3185 : 0 : || !CONST_INT_P (XEXP (a, 1))
3186 : 0 : || (INTVAL (XEXP (a, 1)) & GET_MODE_MASK (mode))
3187 : 0 : != (~(HOST_WIDE_INT_1 << bitnum) & GET_MODE_MASK (mode)))
3188 : : return false;
3189 : :
3190 : : /* if ((x & C) == 0) x &= ~C; is transformed to nothing. */
3191 : : /* if ((x & C) != 0) x &= ~C; is transformed to x &= ~C. */
3192 : 0 : result = (code == EQ) ? a : NULL_RTX;
3193 : : }
3194 : : else
3195 : : return false;
3196 : :
3197 : 0 : if (result)
3198 : : {
3199 : 0 : start_sequence ();
3200 : 0 : noce_emit_move_insn (x, result);
3201 : 0 : seq = end_ifcvt_sequence (if_info);
3202 : 0 : if (!seq)
3203 : : return false;
3204 : :
3205 : 0 : emit_insn_before_setloc (seq, if_info->jump,
3206 : 0 : INSN_LOCATION (if_info->insn_a));
3207 : : }
3208 : 0 : if_info->transform_name = "noce_try_bitop";
3209 : 0 : return true;
3210 : : }
3211 : :
3212 : :
3213 : : /* Similar to get_condition, only the resulting condition must be
3214 : : valid at JUMP, instead of at EARLIEST.
3215 : :
3216 : : If THEN_ELSE_REVERSED is true, the fallthrough does not go to the
3217 : : THEN block of the caller, and we have to reverse the condition. */
3218 : :
3219 : : static rtx
3220 : 4176809 : noce_get_condition (rtx_insn *jump, rtx_insn **earliest,
3221 : : bool then_else_reversed)
3222 : : {
3223 : 4176809 : rtx cond, set, tmp;
3224 : 4176809 : bool reverse;
3225 : :
3226 : 4176809 : if (! any_condjump_p (jump))
3227 : : return NULL_RTX;
3228 : :
3229 : 4176809 : set = pc_set (jump);
3230 : :
3231 : : /* If this branches to JUMP_LABEL when the condition is false,
3232 : : reverse the condition. */
3233 : 8353618 : reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
3234 : 4176809 : && label_ref_label (XEXP (SET_SRC (set), 2)) == JUMP_LABEL (jump));
3235 : :
3236 : : /* We may have to reverse because the caller's if block is not canonical,
3237 : : i.e. the THEN block isn't the fallthrough block for the TEST block
3238 : : (see find_if_header). */
3239 : 4176809 : if (then_else_reversed)
3240 : 1800825 : reverse = !reverse;
3241 : :
3242 : : /* If the condition variable is a register and is MODE_INT, accept it. */
3243 : :
3244 : 4176809 : cond = XEXP (SET_SRC (set), 0);
3245 : 4176809 : tmp = XEXP (cond, 0);
3246 : 4175659 : if (REG_P (tmp) && GET_MODE_CLASS (GET_MODE (tmp)) == MODE_INT
3247 : 4176841 : && (GET_MODE (tmp) != BImode
3248 : 0 : || !targetm.small_register_classes_for_mode_p (BImode)))
3249 : : {
3250 : 32 : *earliest = jump;
3251 : :
3252 : 32 : if (reverse)
3253 : 14 : cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
3254 : : GET_MODE (cond), tmp, XEXP (cond, 1));
3255 : 32 : return cond;
3256 : : }
3257 : :
3258 : : /* Otherwise, fall back on canonicalize_condition to do the dirty
3259 : : work of manipulating MODE_CC values and COMPARE rtx codes. */
3260 : 4176777 : tmp = canonicalize_condition (jump, cond, reverse, earliest,
3261 : : NULL_RTX, have_cbranchcc4, true);
3262 : :
3263 : : /* We don't handle side-effects in the condition, like handling
3264 : : REG_INC notes and making sure no duplicate conditions are emitted. */
3265 : 4176777 : if (tmp != NULL_RTX && side_effects_p (tmp))
3266 : : return NULL_RTX;
3267 : :
3268 : : return tmp;
3269 : : }
3270 : :
3271 : : /* Return true if OP is ok for if-then-else processing. */
3272 : :
3273 : : static bool
3274 : 8768813 : noce_operand_ok (const_rtx op)
3275 : : {
3276 : 8768813 : if (side_effects_p (op))
3277 : : return false;
3278 : :
3279 : : /* We special-case memories, so handle any of them with
3280 : : no address side effects. */
3281 : 8747083 : if (MEM_P (op))
3282 : 1534699 : return ! side_effects_p (XEXP (op, 0));
3283 : :
3284 : 7212384 : return ! may_trap_p (op);
3285 : : }
3286 : :
3287 : : /* Return true iff basic block TEST_BB is valid for noce if-conversion.
3288 : : The condition used in this if-conversion is in COND.
3289 : : In practice, check that TEST_BB ends with a single set
3290 : : x := a and all previous computations
3291 : : in TEST_BB don't produce any values that are live after TEST_BB.
3292 : : In other words, all the insns in TEST_BB are there only
3293 : : to compute a value for x. Add the rtx cost of the insns
3294 : : in TEST_BB to COST. Record whether TEST_BB is a single simple
3295 : : set instruction in SIMPLE_P. */
3296 : :
3297 : : static bool
3298 : 2052170 : bb_valid_for_noce_process_p (basic_block test_bb, rtx cond,
3299 : : unsigned int *cost, bool *simple_p)
3300 : : {
3301 : 2052170 : if (!test_bb)
3302 : : return false;
3303 : :
3304 : 2052170 : rtx_insn *last_insn = last_active_insn (test_bb, false);
3305 : 2052170 : rtx last_set = NULL_RTX;
3306 : :
3307 : 2052170 : rtx cc = cc_in_cond (cond);
3308 : :
3309 : 2052170 : if (!insn_valid_noce_process_p (last_insn, cc))
3310 : : return false;
3311 : :
3312 : : /* Punt on blocks ending with asm goto or jumps with other side-effects,
3313 : : last_active_insn ignores JUMP_INSNs. */
3314 : 1483554 : if (JUMP_P (BB_END (test_bb)) && !onlyjump_p (BB_END (test_bb)))
3315 : : return false;
3316 : :
3317 : 1483552 : last_set = single_set (last_insn);
3318 : :
3319 : 1483552 : rtx x = SET_DEST (last_set);
3320 : 1483552 : rtx_insn *first_insn = first_active_insn (test_bb);
3321 : 1483552 : rtx first_set = single_set (first_insn);
3322 : :
3323 : 1483552 : if (!first_set)
3324 : : return false;
3325 : :
3326 : : /* We have a single simple set, that's okay. */
3327 : 1470843 : bool speed_p = optimize_bb_for_speed_p (test_bb);
3328 : :
3329 : 1470843 : if (first_insn == last_insn)
3330 : : {
3331 : 523487 : *simple_p = noce_operand_ok (SET_DEST (first_set));
3332 : 523487 : *cost += pattern_cost (first_set, speed_p);
3333 : 523487 : return *simple_p;
3334 : : }
3335 : :
3336 : 947356 : rtx_insn *prev_last_insn = PREV_INSN (last_insn);
3337 : 947356 : gcc_assert (prev_last_insn);
3338 : :
3339 : : /* For now, disallow setting x multiple times in test_bb. */
3340 : 947356 : if (REG_P (x) && reg_set_between_p (x, first_insn, prev_last_insn))
3341 : : return false;
3342 : :
3343 : 797554 : bitmap test_bb_temps = BITMAP_ALLOC (®_obstack);
3344 : :
3345 : : /* The regs that are live out of test_bb. */
3346 : 797554 : bitmap test_bb_live_out = df_get_live_out (test_bb);
3347 : :
3348 : 797554 : int potential_cost = pattern_cost (last_set, speed_p);
3349 : 797554 : rtx_insn *insn;
3350 : 3906729 : FOR_BB_INSNS (test_bb, insn)
3351 : : {
3352 : 3804074 : if (insn != last_insn)
3353 : : {
3354 : 3701419 : if (!active_insn_p (insn))
3355 : 2281652 : continue;
3356 : :
3357 : 1419767 : if (!insn_valid_noce_process_p (insn, cc))
3358 : 204228 : goto free_bitmap_and_fail;
3359 : :
3360 : 1215539 : rtx sset = single_set (insn);
3361 : 1215539 : gcc_assert (sset);
3362 : 1215539 : rtx dest = SET_DEST (sset);
3363 : 1215539 : if (SUBREG_P (dest))
3364 : 24025 : dest = SUBREG_REG (dest);
3365 : :
3366 : 1215539 : if (contains_mem_rtx_p (SET_SRC (sset))
3367 : 828329 : || !REG_P (dest)
3368 : 1956659 : || reg_overlap_mentioned_p (dest, cond))
3369 : 490671 : goto free_bitmap_and_fail;
3370 : :
3371 : 724868 : potential_cost += pattern_cost (sset, speed_p);
3372 : 724868 : bitmap_set_bit (test_bb_temps, REGNO (dest));
3373 : : }
3374 : : }
3375 : :
3376 : : /* If any of the intermediate results in test_bb are live after test_bb
3377 : : then fail. */
3378 : 102655 : if (bitmap_intersect_p (test_bb_live_out, test_bb_temps))
3379 : 64302 : goto free_bitmap_and_fail;
3380 : :
3381 : 38353 : BITMAP_FREE (test_bb_temps);
3382 : 38353 : *cost += potential_cost;
3383 : 38353 : *simple_p = false;
3384 : 38353 : return true;
3385 : :
3386 : 759201 : free_bitmap_and_fail:
3387 : 759201 : BITMAP_FREE (test_bb_temps);
3388 : 759201 : return false;
3389 : : }
3390 : :
3391 : : /* Helper function to emit a cmov sequence encapsulated in
3392 : : start_sequence () and end_sequence (). If NEED_CMOV is true
3393 : : we call noce_emit_cmove to create a cmove sequence. Otherwise emit
3394 : : a simple move. If successful, store the first instruction of the
3395 : : sequence in TEMP_DEST and the sequence costs in SEQ_COST. */
3396 : :
3397 : : static rtx_insn*
3398 : 371500 : try_emit_cmove_seq (struct noce_if_info *if_info, rtx temp,
3399 : : rtx cond, rtx new_val, rtx old_val, bool need_cmov,
3400 : : unsigned *cost, rtx *temp_dest,
3401 : : rtx cc_cmp = NULL, rtx rev_cc_cmp = NULL)
3402 : : {
3403 : 371500 : rtx_insn *seq = NULL;
3404 : 371500 : *cost = 0;
3405 : :
3406 : 371500 : rtx x = XEXP (cond, 0);
3407 : 371500 : rtx y = XEXP (cond, 1);
3408 : 371500 : rtx_code cond_code = GET_CODE (cond);
3409 : :
3410 : 371500 : start_sequence ();
3411 : :
3412 : 371500 : if (need_cmov)
3413 : 294223 : *temp_dest = noce_emit_cmove (if_info, temp, cond_code,
3414 : : x, y, new_val, old_val, cc_cmp, rev_cc_cmp);
3415 : : else
3416 : : {
3417 : 77277 : *temp_dest = temp;
3418 : 77277 : if (if_info->then_else_reversed)
3419 : 2808 : noce_emit_move_insn (temp, old_val);
3420 : : else
3421 : 74469 : noce_emit_move_insn (temp, new_val);
3422 : : }
3423 : :
3424 : 371500 : if (*temp_dest != NULL_RTX)
3425 : : {
3426 : 364248 : seq = get_insns ();
3427 : 364248 : *cost = seq_cost (seq, if_info->speed_p);
3428 : : }
3429 : :
3430 : 371500 : end_sequence ();
3431 : :
3432 : 371500 : return seq;
3433 : : }
3434 : :
3435 : : /* We have something like:
3436 : :
3437 : : if (x > y)
3438 : : { i = EXPR_A; j = EXPR_B; k = EXPR_C; }
3439 : :
3440 : : Make it:
3441 : :
3442 : : tmp_i = (x > y) ? EXPR_A : i;
3443 : : tmp_j = (x > y) ? EXPR_B : j;
3444 : : tmp_k = (x > y) ? EXPR_C : k;
3445 : : i = tmp_i;
3446 : : j = tmp_j;
3447 : : k = tmp_k;
3448 : :
3449 : : Subsequent passes are expected to clean up the extra moves.
3450 : :
3451 : : Look for special cases such as writes to one register which are
3452 : : read back in another SET, as might occur in a swap idiom or
3453 : : similar.
3454 : :
3455 : : These look like:
3456 : :
3457 : : if (x > y)
3458 : : i = a;
3459 : : j = i;
3460 : :
3461 : : Which we want to rewrite to:
3462 : :
3463 : : tmp_i = (x > y) ? a : i;
3464 : : tmp_j = (x > y) ? tmp_i : j;
3465 : : i = tmp_i;
3466 : : j = tmp_j;
3467 : :
3468 : : We can catch these when looking at (SET x y) by keeping a list of the
3469 : : registers we would have targeted before if-conversion and looking back
3470 : : through it for an overlap with Y. If we find one, we rewire the
3471 : : conditional set to use the temporary we introduced earlier.
3472 : :
3473 : : IF_INFO contains the useful information about the block structure and
3474 : : jump instructions. */
3475 : :
3476 : : static bool
3477 : 39259 : noce_convert_multiple_sets (struct noce_if_info *if_info)
3478 : : {
3479 : 39259 : basic_block test_bb = if_info->test_bb;
3480 : 39259 : basic_block then_bb = if_info->then_bb;
3481 : 39259 : basic_block join_bb = if_info->join_bb;
3482 : 39259 : rtx_insn *jump = if_info->jump;
3483 : 39259 : rtx_insn *cond_earliest;
3484 : 39259 : rtx_insn *insn;
3485 : :
3486 : 39259 : start_sequence ();
3487 : :
3488 : : /* Decompose the condition attached to the jump. */
3489 : 39259 : rtx cond = noce_get_condition (jump, &cond_earliest, false);
3490 : 39259 : rtx x = XEXP (cond, 0);
3491 : 39259 : rtx y = XEXP (cond, 1);
3492 : :
3493 : 39259 : auto_delete_vec<noce_multiple_sets_info> insn_info;
3494 : 39259 : init_noce_multiple_sets_info (then_bb, insn_info);
3495 : :
3496 : 39259 : int last_needs_comparison = -1;
3497 : :
3498 : 39259 : bool ok = noce_convert_multiple_sets_1
3499 : 39259 : (if_info, insn_info, &last_needs_comparison);
3500 : 39259 : if (!ok)
3501 : : return false;
3502 : :
3503 : : /* If there are insns that overwrite part of the initial
3504 : : comparison, we can still omit creating temporaries for
3505 : : the last of them.
3506 : : As the second try will always create a less expensive,
3507 : : valid sequence, we do not need to compare and can discard
3508 : : the first one. */
3509 : 37659 : if (last_needs_comparison != -1)
3510 : : {
3511 : 37659 : end_sequence ();
3512 : 37659 : start_sequence ();
3513 : 37659 : ok = noce_convert_multiple_sets_1
3514 : 37659 : (if_info, insn_info, &last_needs_comparison);
3515 : : /* Actually we should not fail anymore if we reached here,
3516 : : but better still check. */
3517 : 37659 : if (!ok)
3518 : : return false;
3519 : : }
3520 : :
3521 : : /* We must have seen some sort of insn to insert, otherwise we were
3522 : : given an empty BB to convert, and we can't handle that. */
3523 : 37659 : gcc_assert (!insn_info.is_empty ());
3524 : :
3525 : : /* Now fixup the assignments.
3526 : : PR116405: Iterate in reverse order and keep track of the targets so that
3527 : : a move does not overwrite a subsequent value when multiple instructions
3528 : : have the same target. */
3529 : 37659 : unsigned i;
3530 : 37659 : noce_multiple_sets_info *info;
3531 : 37659 : bitmap set_targets = BITMAP_ALLOC (®_obstack);
3532 : 147166 : FOR_EACH_VEC_ELT_REVERSE (insn_info, i, info)
3533 : : {
3534 : 109507 : gcc_checking_assert (REG_P (info->target));
3535 : :
3536 : 109507 : if (info->target != info->temporary
3537 : 109507 : && !bitmap_bit_p (set_targets, REGNO (info->target)))
3538 : 3675 : noce_emit_move_insn (info->target, info->temporary);
3539 : :
3540 : 109507 : bitmap_set_bit (set_targets, REGNO (info->target));
3541 : : }
3542 : 37659 : BITMAP_FREE (set_targets);
3543 : :
3544 : : /* Actually emit the sequence if it isn't too expensive. */
3545 : 37659 : rtx_insn *seq = get_insns ();
3546 : :
3547 : 37659 : if (!targetm.noce_conversion_profitable_p (seq, if_info))
3548 : : {
3549 : 17289 : end_sequence ();
3550 : 17289 : return false;
3551 : : }
3552 : :
3553 : 110729 : for (insn = seq; insn; insn = NEXT_INSN (insn))
3554 : 90359 : set_used_flags (insn);
3555 : :
3556 : : /* Mark all our temporaries and targets as used. */
3557 : 67242 : for (unsigned i = 0; i < insn_info.length (); i++)
3558 : : {
3559 : 46872 : set_used_flags (insn_info[i]->temporary);
3560 : 46872 : set_used_flags (insn_info[i]->target);
3561 : : }
3562 : :
3563 : 20370 : set_used_flags (cond);
3564 : 20370 : set_used_flags (x);
3565 : 20370 : set_used_flags (y);
3566 : :
3567 : 20370 : unshare_all_rtl_in_chain (seq);
3568 : 20370 : end_sequence ();
3569 : :
3570 : 20370 : if (!seq)
3571 : : return false;
3572 : :
3573 : 110707 : for (insn = seq; insn; insn = NEXT_INSN (insn))
3574 : 90348 : if (JUMP_P (insn) || CALL_P (insn)
3575 : 90348 : || recog_memoized (insn) == -1)
3576 : 11 : return false;
3577 : :
3578 : 20359 : emit_insn_before_setloc (seq, if_info->jump,
3579 : 20359 : INSN_LOCATION (insn_info.last ()->unmodified_insn));
3580 : :
3581 : : /* Clean up THEN_BB and the edges in and out of it. */
3582 : 20359 : remove_edge (find_edge (test_bb, join_bb));
3583 : 20359 : remove_edge (find_edge (then_bb, join_bb));
3584 : 20359 : redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
3585 : 20359 : delete_basic_block (then_bb);
3586 : 20359 : num_true_changes++;
3587 : :
3588 : : /* Maybe merge blocks now the jump is simple enough. */
3589 : 20359 : if (can_merge_blocks_p (test_bb, join_bb))
3590 : : {
3591 : 13703 : merge_blocks (test_bb, join_bb);
3592 : 13703 : num_true_changes++;
3593 : : }
3594 : :
3595 : 20359 : num_updated_if_blocks++;
3596 : 20359 : if_info->transform_name = "noce_convert_multiple_sets";
3597 : 20359 : return true;
3598 : 39259 : }
3599 : :
3600 : : /* This goes through all relevant insns of IF_INFO->then_bb and tries to create
3601 : : conditional moves. Information for the insns is kept in INSN_INFO. */
3602 : :
3603 : : static bool
3604 : 76918 : noce_convert_multiple_sets_1 (struct noce_if_info *if_info,
3605 : : auto_delete_vec<noce_multiple_sets_info> &insn_info,
3606 : : int *last_needs_comparison)
3607 : : {
3608 : 76918 : basic_block then_bb = if_info->then_bb;
3609 : 76918 : rtx_insn *jump = if_info->jump;
3610 : 76918 : rtx_insn *cond_earliest;
3611 : :
3612 : : /* Decompose the condition attached to the jump. */
3613 : 76918 : rtx cond = noce_get_condition (jump, &cond_earliest, false);
3614 : :
3615 : 76918 : rtx cc_cmp = cond_exec_get_condition (jump);
3616 : 76918 : if (cc_cmp)
3617 : 76918 : cc_cmp = copy_rtx (cc_cmp);
3618 : 76918 : rtx rev_cc_cmp = cond_exec_get_condition (jump, /* get_reversed */ true);
3619 : 76918 : if (rev_cc_cmp)
3620 : 76918 : rev_cc_cmp = copy_rtx (rev_cc_cmp);
3621 : :
3622 : 76918 : rtx_insn *insn;
3623 : 76918 : int count = 0;
3624 : 76918 : bool second_try = *last_needs_comparison != -1;
3625 : :
3626 : 481285 : FOR_BB_INSNS (then_bb, insn)
3627 : : {
3628 : : /* Skip over non-insns. */
3629 : 405967 : if (!active_insn_p (insn))
3630 : 182945 : continue;
3631 : :
3632 : 223022 : noce_multiple_sets_info *info = insn_info[count];
3633 : :
3634 : 223022 : rtx set = single_set (insn);
3635 : 223022 : gcc_checking_assert (set);
3636 : :
3637 : 223022 : rtx target = SET_DEST (set);
3638 : 223022 : rtx temp;
3639 : :
3640 : 223022 : rtx new_val = SET_SRC (set);
3641 : :
3642 : 223022 : int i, ii;
3643 : 281146 : FOR_EACH_VEC_ELT (info->rewired_src, i, ii)
3644 : 58124 : new_val = simplify_replace_rtx (new_val, insn_info[ii]->target,
3645 : 58124 : insn_info[ii]->temporary);
3646 : :
3647 : 223022 : rtx old_val = target;
3648 : :
3649 : : /* As we are transforming
3650 : : if (x > y)
3651 : : {
3652 : : a = b;
3653 : : c = d;
3654 : : }
3655 : : into
3656 : : a = (x > y) ...
3657 : : c = (x > y) ...
3658 : :
3659 : : we potentially check x > y before every set.
3660 : : Even though the check might be removed by subsequent passes, this means
3661 : : that we cannot transform
3662 : : if (x > y)
3663 : : {
3664 : : x = y;
3665 : : ...
3666 : : }
3667 : : into
3668 : : x = (x > y) ...
3669 : : ...
3670 : : since this would invalidate x and the following to-be-removed checks.
3671 : : Therefore we introduce a temporary every time we are about to
3672 : : overwrite a variable used in the check. Costing of a sequence with
3673 : : these is going to be inaccurate so only use temporaries when
3674 : : needed.
3675 : :
3676 : : If performing a second try, we know how many insns require a
3677 : : temporary. For the last of these, we can omit creating one. */
3678 : 223022 : if (reg_overlap_mentioned_p (target, cond)
3679 : 223022 : && (!second_try || count < *last_needs_comparison))
3680 : 26154 : temp = gen_reg_rtx (GET_MODE (target));
3681 : : else
3682 : : temp = target;
3683 : :
3684 : : /* We have identified swap-style idioms before. A normal
3685 : : set will need to be a cmov while the first instruction of a swap-style
3686 : : idiom can be a regular move. This helps with costing. */
3687 : 223022 : bool need_cmov = info->need_cmov;
3688 : :
3689 : : /* If we had a non-canonical conditional jump (i.e. one where
3690 : : the fallthrough is to the "else" case) we need to reverse
3691 : : the conditional select. */
3692 : 223022 : if (if_info->then_else_reversed)
3693 : 57187 : std::swap (old_val, new_val);
3694 : :
3695 : : /* Try emitting a conditional move passing the backend the
3696 : : canonicalized comparison. The backend is then able to
3697 : : recognize expressions like
3698 : :
3699 : : if (x > y)
3700 : : y = x;
3701 : :
3702 : : as min/max and emit an insn, accordingly. */
3703 : 223022 : unsigned cost1 = 0, cost2 = 0;
3704 : 223022 : rtx_insn *seq, *seq1, *seq2 = NULL;
3705 : 223022 : rtx temp_dest = NULL_RTX, temp_dest1 = NULL_RTX, temp_dest2 = NULL_RTX;
3706 : 223022 : bool read_comparison = false;
3707 : :
3708 : 223022 : seq1 = try_emit_cmove_seq (if_info, temp, cond,
3709 : : new_val, old_val, need_cmov,
3710 : : &cost1, &temp_dest1);
3711 : :
3712 : : /* Here, we try to pass the backend a non-canonicalized cc comparison
3713 : : as well. This allows the backend to emit a cmov directly without
3714 : : creating an additional compare for each. If successful, costing
3715 : : is easier and this sequence is usually preferred. */
3716 : 223022 : if (cc_cmp)
3717 : : {
3718 : 148478 : seq2 = try_emit_cmove_seq (if_info, temp, cond,
3719 : : new_val, old_val, need_cmov,
3720 : : &cost2, &temp_dest2, cc_cmp, rev_cc_cmp);
3721 : :
3722 : : /* The if_then_else in SEQ2 may be affected when cc_cmp/rev_cc_cmp is
3723 : : clobbered. We can't safely use the sequence in this case. */
3724 : 282976 : for (rtx_insn *iter = seq2; iter; iter = NEXT_INSN (iter))
3725 : 183011 : if (modified_in_p (cc_cmp, iter)
3726 : 183011 : || (rev_cc_cmp && modified_in_p (rev_cc_cmp, iter)))
3727 : : {
3728 : : seq2 = NULL;
3729 : : break;
3730 : : }
3731 : : }
3732 : :
3733 : : /* The backend might have created a sequence that uses the
3734 : : condition as a value. Check this. */
3735 : :
3736 : : /* We cannot handle anything more complex than a reg or constant. */
3737 : 223022 : if (!REG_P (XEXP (cond, 0)) && !CONSTANT_P (XEXP (cond, 0)))
3738 : 223022 : read_comparison = true;
3739 : :
3740 : 223022 : if (!REG_P (XEXP (cond, 1)) && !CONSTANT_P (XEXP (cond, 1)))
3741 : 223022 : read_comparison = true;
3742 : :
3743 : : rtx_insn *walk = seq2;
3744 : : int if_then_else_count = 0;
3745 : 348237 : while (walk && !read_comparison)
3746 : : {
3747 : 125215 : rtx exprs_to_check[2];
3748 : 125215 : unsigned int exprs_count = 0;
3749 : :
3750 : 125215 : rtx set = single_set (walk);
3751 : 125215 : if (set && XEXP (set, 1)
3752 : 125215 : && GET_CODE (XEXP (set, 1)) == IF_THEN_ELSE)
3753 : : {
3754 : : /* We assume that this is the cmove created by the backend that
3755 : : naturally uses the condition. */
3756 : 74453 : exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 1);
3757 : 74453 : exprs_to_check[exprs_count++] = XEXP (XEXP (set, 1), 2);
3758 : 74453 : if_then_else_count++;
3759 : : }
3760 : 50762 : else if (NONDEBUG_INSN_P (walk))
3761 : 50762 : exprs_to_check[exprs_count++] = PATTERN (walk);
3762 : :
3763 : : /* Bail if we get more than one if_then_else because the assumption
3764 : : above may be incorrect. */
3765 : 125215 : if (if_then_else_count > 1)
3766 : : {
3767 : 0 : read_comparison = true;
3768 : 0 : break;
3769 : : }
3770 : :
3771 : 324883 : for (unsigned int i = 0; i < exprs_count; i++)
3772 : : {
3773 : 199668 : subrtx_iterator::array_type array;
3774 : 461846 : FOR_EACH_SUBRTX (iter, array, exprs_to_check[i], NONCONST)
3775 : 302240 : if (*iter != NULL_RTX
3776 : 302240 : && (reg_overlap_mentioned_p (XEXP (cond, 0), *iter)
3777 : 279798 : || reg_overlap_mentioned_p (XEXP (cond, 1), *iter)))
3778 : : {
3779 : : read_comparison = true;
3780 : : break;
3781 : : }
3782 : 199668 : }
3783 : :
3784 : 125215 : walk = NEXT_INSN (walk);
3785 : : }
3786 : :
3787 : : /* Check which version is less expensive. */
3788 : 223022 : if (seq1 != NULL_RTX && (cost1 <= cost2 || seq2 == NULL_RTX))
3789 : : {
3790 : 144291 : seq = seq1;
3791 : 144291 : temp_dest = temp_dest1;
3792 : 144291 : if (!second_try)
3793 : 73428 : *last_needs_comparison = count;
3794 : : }
3795 : 1671 : else if (seq2 != NULL_RTX)
3796 : : {
3797 : 77321 : seq = seq2;
3798 : 77321 : temp_dest = temp_dest2;
3799 : 77321 : if (!second_try && read_comparison)
3800 : 11485 : *last_needs_comparison = count;
3801 : : }
3802 : : else
3803 : : {
3804 : : /* Nothing worked, bail out. */
3805 : 1410 : end_sequence ();
3806 : 1600 : return false;
3807 : : }
3808 : :
3809 : : /* Although we use temporaries if there is register overlap of COND and
3810 : : TARGET, it is possible that SEQ modifies COND anyway. For example,
3811 : : COND may use the flags register and if INSN clobbers flags then
3812 : : we may be unable to emit a valid sequence (e.g. in x86 that would
3813 : : require saving and restoring the flags register). */
3814 : 150749 : if (!second_try)
3815 : 329959 : for (rtx_insn *iter = seq; iter; iter = NEXT_INSN (iter))
3816 : 218044 : if (modified_in_p (cond, iter))
3817 : : {
3818 : 190 : end_sequence ();
3819 : 190 : return false;
3820 : : }
3821 : :
3822 : 221422 : if (cc_cmp && seq == seq1)
3823 : : {
3824 : : /* Check if SEQ can clobber registers mentioned in cc_cmp/rev_cc_cmp.
3825 : : If yes, we need to use only SEQ1 from that point on.
3826 : : Only check when we use SEQ1 since we have already tested SEQ2. */
3827 : 96706 : for (rtx_insn *iter = seq; iter; iter = NEXT_INSN (iter))
3828 : 74241 : if (modified_in_p (cc_cmp, iter)
3829 : 74241 : || (rev_cc_cmp && modified_in_p (rev_cc_cmp, iter)))
3830 : : {
3831 : : cc_cmp = NULL_RTX;
3832 : : rev_cc_cmp = NULL_RTX;
3833 : : break;
3834 : : }
3835 : : }
3836 : :
3837 : : /* End the sub sequence and emit to the main sequence. */
3838 : 221422 : emit_insn (seq);
3839 : :
3840 : : /* Bookkeeping. */
3841 : 221422 : count++;
3842 : :
3843 : 221422 : info->target = target;
3844 : 221422 : info->temporary = temp_dest;
3845 : 221422 : info->unmodified_insn = insn;
3846 : : }
3847 : :
3848 : : /* Even if we did not actually need the comparison, we want to make sure
3849 : : to try a second time in order to get rid of the temporaries. */
3850 : 75318 : if (*last_needs_comparison == -1)
3851 : 1220 : *last_needs_comparison = 0;
3852 : :
3853 : : return true;
3854 : : }
3855 : :
3856 : : /* Find local swap-style idioms in BB and mark the first insn (1)
3857 : : that is only a temporary as not needing a conditional move as
3858 : : it is going to be dead afterwards anyway.
3859 : :
3860 : : (1) int tmp = a;
3861 : : a = b;
3862 : : b = tmp;
3863 : :
3864 : : ifcvt
3865 : : -->
3866 : :
3867 : : tmp = a;
3868 : : a = cond ? b : a_old;
3869 : : b = cond ? tmp : b_old;
3870 : :
3871 : : Additionally, store the index of insns like (2) when a subsequent
3872 : : SET reads from their destination.
3873 : :
3874 : : (2) int c = a;
3875 : : int d = c;
3876 : :
3877 : : ifcvt
3878 : : -->
3879 : :
3880 : : c = cond ? a : c_old;
3881 : : d = cond ? d : c; // Need to use c rather than c_old here.
3882 : : */
3883 : :
3884 : : static void
3885 : 39259 : init_noce_multiple_sets_info (basic_block bb,
3886 : : auto_delete_vec<noce_multiple_sets_info> &insn_info)
3887 : : {
3888 : 39259 : rtx_insn *insn;
3889 : 39259 : int count = 0;
3890 : 39259 : auto_vec<rtx> dests;
3891 : 39259 : bitmap bb_live_out = df_get_live_out (bb);
3892 : :
3893 : : /* Iterate over all SETs, storing the destinations in DEST.
3894 : : - If we encounter a previously changed register,
3895 : : rewire the read to the original source.
3896 : : - If we encounter a SET that writes to a destination
3897 : : that is not live after this block then the register
3898 : : does not need to be moved conditionally. */
3899 : 252037 : FOR_BB_INSNS (bb, insn)
3900 : : {
3901 : 212778 : if (!active_insn_p (insn))
3902 : 98533 : continue;
3903 : :
3904 : 114245 : noce_multiple_sets_info *info = new noce_multiple_sets_info;
3905 : 114245 : info->target = NULL_RTX;
3906 : 114245 : info->temporary = NULL_RTX;
3907 : 114245 : info->unmodified_insn = NULL;
3908 : 114245 : insn_info.safe_push (info);
3909 : :
3910 : 114245 : rtx set = single_set (insn);
3911 : 114245 : gcc_checking_assert (set);
3912 : :
3913 : 114245 : rtx src = SET_SRC (set);
3914 : 114245 : rtx dest = SET_DEST (set);
3915 : :
3916 : 114245 : gcc_checking_assert (REG_P (dest));
3917 : 114245 : info->need_cmov = bitmap_bit_p (bb_live_out, REGNO (dest));
3918 : :
3919 : : /* Check if the current SET's source is the same
3920 : : as any previously seen destination.
3921 : : This is quadratic but the number of insns in BB
3922 : : is bounded by PARAM_MAX_RTL_IF_CONVERSION_INSNS. */
3923 : 257996 : for (int i = count - 1; i >= 0; --i)
3924 : 143751 : if (reg_mentioned_p (dests[i], src))
3925 : 30356 : insn_info[count]->rewired_src.safe_push (i);
3926 : :
3927 : 114245 : dests.safe_push (dest);
3928 : 114245 : count++;
3929 : : }
3930 : 39259 : }
3931 : :
3932 : : /* Return true iff basic block TEST_BB is suitable for conversion to a
3933 : : series of conditional moves. Also check that we have more than one
3934 : : set (other routines can handle a single set better than we would),
3935 : : and fewer than PARAM_MAX_RTL_IF_CONVERSION_INSNS sets. While going
3936 : : through the insns store the sum of their potential costs in COST. */
3937 : :
3938 : : static bool
3939 : 1061347 : bb_ok_for_noce_convert_multiple_sets (basic_block test_bb, unsigned *cost)
3940 : : {
3941 : 1061347 : rtx_insn *insn;
3942 : 1061347 : unsigned count = 0;
3943 : 1061347 : unsigned param = param_max_rtl_if_conversion_insns;
3944 : 1061347 : bool speed_p = optimize_bb_for_speed_p (test_bb);
3945 : 1061347 : unsigned potential_cost = 0;
3946 : :
3947 : 7145202 : FOR_BB_INSNS (test_bb, insn)
3948 : : {
3949 : : /* Skip over notes etc. */
3950 : 7017005 : if (!active_insn_p (insn))
3951 : 5130286 : continue;
3952 : :
3953 : : /* We only handle SET insns. */
3954 : 1886719 : rtx set = single_set (insn);
3955 : 1886719 : if (set == NULL_RTX)
3956 : : return false;
3957 : :
3958 : 1691701 : rtx dest = SET_DEST (set);
3959 : 1691701 : rtx src = SET_SRC (set);
3960 : :
3961 : : /* Do not handle anything involving memory loads/stores since it might
3962 : : violate data-race-freedom guarantees. Make sure we can force SRC
3963 : : to a register as that may be needed in try_emit_cmove_seq. */
3964 : 1496964 : if (!REG_P (dest) || contains_mem_rtx_p (src)
3965 : 2701418 : || !noce_can_force_operand (src))
3966 : 690232 : return false;
3967 : :
3968 : : /* Destination and source must be appropriate. */
3969 : 1001469 : if (!noce_operand_ok (dest) || !noce_operand_ok (src))
3970 : 6194 : return false;
3971 : :
3972 : : /* We must be able to conditionally move in this mode. */
3973 : 995275 : if (!can_conditionally_move_p (GET_MODE (dest)))
3974 : : return false;
3975 : :
3976 : 953569 : potential_cost += insn_cost (insn, speed_p);
3977 : :
3978 : 953569 : count++;
3979 : : }
3980 : :
3981 : 128197 : *cost += potential_cost;
3982 : :
3983 : : /* If we would only put out one conditional move, the other strategies
3984 : : this pass tries are better optimized and will be more appropriate.
3985 : : Some targets want to strictly limit the number of conditional moves
3986 : : that are emitted, they set this through PARAM, we need to respect
3987 : : that. */
3988 : 128197 : return count > 1 && count <= param;
3989 : : }
3990 : :
3991 : : /* Compute average of two given costs weighted by relative probabilities
3992 : : of respective basic blocks in an IF-THEN-ELSE. E is the IF-THEN edge.
3993 : : With P as the probability to take the IF-THEN branch, return
3994 : : P * THEN_COST + (1 - P) * ELSE_COST. */
3995 : : static unsigned
3996 : 246364 : average_cost (unsigned then_cost, unsigned else_cost, edge e)
3997 : : {
3998 : 246364 : return else_cost + e->probability.apply ((signed) (then_cost - else_cost));
3999 : : }
4000 : :
4001 : : /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
4002 : : it without using conditional execution. Return TRUE if we were successful
4003 : : at converting the block. */
4004 : :
4005 : : static bool
4006 : 1800825 : noce_process_if_block (struct noce_if_info *if_info)
4007 : : {
4008 : 1800825 : basic_block test_bb = if_info->test_bb; /* test block */
4009 : 1800825 : basic_block then_bb = if_info->then_bb; /* THEN */
4010 : 1800825 : basic_block else_bb = if_info->else_bb; /* ELSE or NULL */
4011 : 1800825 : basic_block join_bb = if_info->join_bb; /* JOIN */
4012 : 1800825 : rtx_insn *jump = if_info->jump;
4013 : 1800825 : rtx cond = if_info->cond;
4014 : 1800825 : rtx_insn *insn_a, *insn_b;
4015 : 1800825 : rtx set_a, set_b;
4016 : 1800825 : rtx orig_x, x, a, b;
4017 : :
4018 : : /* We're looking for patterns of the form
4019 : :
4020 : : (1) if (...) x = a; else x = b;
4021 : : (2) x = b; if (...) x = a;
4022 : : (3) if (...) x = a; // as if with an initial x = x.
4023 : : (4) if (...) { x = a; y = b; z = c; } // Like 3, for multiple SETS.
4024 : : The later patterns require jumps to be more expensive.
4025 : : For the if (...) x = a; else x = b; case we allow multiple insns
4026 : : inside the then and else blocks as long as their only effect is
4027 : : to calculate a value for x.
4028 : : ??? For future expansion, further expand the "multiple X" rules. */
4029 : :
4030 : : /* First look for multiple SETS. The original costs already include
4031 : : a base cost of COSTS_N_INSNS (2): one instruction for the compare
4032 : : (which we will be needing either way) and one instruction for the
4033 : : branch. When comparing costs we want to use the branch instruction
4034 : : cost and the sets vs. the cmovs generated here. Therefore subtract
4035 : : the costs of the compare before checking.
4036 : : ??? Actually, instead of the branch instruction costs we might want
4037 : : to use COSTS_N_INSNS (BRANCH_COST ()) as in other places. */
4038 : :
4039 : 1800825 : unsigned potential_cost = if_info->original_cost - COSTS_N_INSNS (1);
4040 : 1800825 : unsigned old_cost = if_info->original_cost;
4041 : 1800825 : if (!else_bb
4042 : : && HAVE_conditional_move
4043 : 1800825 : && bb_ok_for_noce_convert_multiple_sets (then_bb, &potential_cost))
4044 : : {
4045 : : /* Temporarily set the original costs to what we estimated so
4046 : : we can determine if the transformation is worth it. */
4047 : 39259 : if_info->original_cost = potential_cost;
4048 : 39259 : if (noce_convert_multiple_sets (if_info))
4049 : : {
4050 : 20359 : if (dump_file && if_info->transform_name)
4051 : 1 : fprintf (dump_file, "if-conversion succeeded through %s\n",
4052 : : if_info->transform_name);
4053 : 20359 : return true;
4054 : : }
4055 : :
4056 : : /* Restore the original costs. */
4057 : 18900 : if_info->original_cost = old_cost;
4058 : : }
4059 : :
4060 : 1780466 : bool speed_p = optimize_bb_for_speed_p (test_bb);
4061 : 1780466 : unsigned int then_cost = 0, else_cost = 0;
4062 : 1780466 : if (!bb_valid_for_noce_process_p (then_bb, cond, &then_cost,
4063 : : &if_info->then_simple))
4064 : : return false;
4065 : :
4066 : 477691 : if (else_bb
4067 : 477691 : && !bb_valid_for_noce_process_p (else_bb, cond, &else_cost,
4068 : : &if_info->else_simple))
4069 : : return false;
4070 : :
4071 : 290136 : if (speed_p)
4072 : 246364 : if_info->original_cost += average_cost (then_cost, else_cost,
4073 : : find_edge (test_bb, then_bb));
4074 : : else
4075 : 43772 : if_info->original_cost += then_cost + else_cost;
4076 : :
4077 : 290136 : insn_a = last_active_insn (then_bb, false);
4078 : 290136 : set_a = single_set (insn_a);
4079 : 290136 : gcc_assert (set_a);
4080 : :
4081 : 290136 : x = SET_DEST (set_a);
4082 : 290136 : a = SET_SRC (set_a);
4083 : :
4084 : : /* Look for the other potential set. Make sure we've got equivalent
4085 : : destinations. */
4086 : : /* ??? This is overconservative. Storing to two different mems is
4087 : : as easy as conditionally computing the address. Storing to a
4088 : : single mem merely requires a scratch memory to use as one of the
4089 : : destination addresses; often the memory immediately below the
4090 : : stack pointer is available for this. */
4091 : 290136 : set_b = NULL_RTX;
4092 : 290136 : if (else_bb)
4093 : : {
4094 : 84149 : insn_b = last_active_insn (else_bb, false);
4095 : 84149 : set_b = single_set (insn_b);
4096 : 84149 : gcc_assert (set_b);
4097 : :
4098 : 84149 : if (!rtx_interchangeable_p (x, SET_DEST (set_b)))
4099 : : return false;
4100 : : }
4101 : : else
4102 : : {
4103 : 205987 : insn_b = if_info->cond_earliest;
4104 : 541002 : do
4105 : 541002 : insn_b = prev_nonnote_nondebug_insn (insn_b);
4106 : : while (insn_b
4107 : 538448 : && (BLOCK_FOR_INSN (insn_b)
4108 : 538448 : == BLOCK_FOR_INSN (if_info->cond_earliest))
4109 : 1202012 : && !modified_in_p (x, insn_b));
4110 : :
4111 : : /* We're going to be moving the evaluation of B down from above
4112 : : COND_EARLIEST to JUMP. Make sure the relevant data is still
4113 : : intact. */
4114 : 205987 : if (! insn_b
4115 : 203433 : || BLOCK_FOR_INSN (insn_b) != BLOCK_FOR_INSN (if_info->cond_earliest)
4116 : 120008 : || !NONJUMP_INSN_P (insn_b)
4117 : 114464 : || (set_b = single_set (insn_b)) == NULL_RTX
4118 : 112921 : || ! rtx_interchangeable_p (x, SET_DEST (set_b))
4119 : 100811 : || ! noce_operand_ok (SET_SRC (set_b))
4120 : 98637 : || reg_overlap_mentioned_p (x, SET_SRC (set_b))
4121 : 96830 : || modified_between_p (SET_SRC (set_b), insn_b, jump)
4122 : : /* Avoid extending the lifetime of hard registers on small
4123 : : register class machines. */
4124 : 94104 : || (REG_P (SET_SRC (set_b))
4125 : 26312 : && HARD_REGISTER_P (SET_SRC (set_b))
4126 : 3578 : && targetm.small_register_classes_for_mode_p
4127 : 3578 : (GET_MODE (SET_SRC (set_b))))
4128 : : /* Likewise with X. In particular this can happen when
4129 : : noce_get_condition looks farther back in the instruction
4130 : : stream than one might expect. */
4131 : 90526 : || reg_overlap_mentioned_p (x, cond)
4132 : 77082 : || reg_overlap_mentioned_p (x, a)
4133 : 275461 : || modified_between_p (x, insn_b, jump))
4134 : : {
4135 : : insn_b = NULL;
4136 : : set_b = NULL_RTX;
4137 : : }
4138 : : }
4139 : :
4140 : : /* If x has side effects then only the if-then-else form is safe to
4141 : : convert. But even in that case we would need to restore any notes
4142 : : (such as REG_INC) at then end. That can be tricky if
4143 : : noce_emit_move_insn expands to more than one insn, so disable the
4144 : : optimization entirely for now if there are side effects. */
4145 : 276834 : if (side_effects_p (x))
4146 : : return false;
4147 : :
4148 : 276834 : b = (set_b ? SET_SRC (set_b) : x);
4149 : :
4150 : : /* Only operate on register destinations, and even then avoid extending
4151 : : the lifetime of hard registers on small register class machines. */
4152 : 276834 : orig_x = x;
4153 : 276834 : if_info->orig_x = orig_x;
4154 : 276834 : if (!REG_P (x)
4155 : 276834 : || (HARD_REGISTER_P (x)
4156 : 2 : && targetm.small_register_classes_for_mode_p (GET_MODE (x))))
4157 : : {
4158 : 61336 : if (GET_MODE (x) == BLKmode)
4159 : : return false;
4160 : :
4161 : 61023 : if (GET_CODE (x) == ZERO_EXTRACT
4162 : 3 : && (!CONST_INT_P (XEXP (x, 1))
4163 : 3 : || !CONST_INT_P (XEXP (x, 2))))
4164 : : return false;
4165 : :
4166 : 61023 : x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
4167 : : ? XEXP (x, 0) : x));
4168 : : }
4169 : :
4170 : : /* Don't operate on sources that may trap or are volatile. */
4171 : 276521 : if (! noce_operand_ok (a) || ! noce_operand_ok (b))
4172 : 0 : return false;
4173 : :
4174 : 324543 : retry:
4175 : : /* Set up the info block for our subroutines. */
4176 : 324543 : if_info->insn_a = insn_a;
4177 : 324543 : if_info->insn_b = insn_b;
4178 : 324543 : if_info->x = x;
4179 : 324543 : if_info->a = a;
4180 : 324543 : if_info->b = b;
4181 : :
4182 : : /* Try optimizations in some approximation of a useful order. */
4183 : : /* ??? Should first look to see if X is live incoming at all. If it
4184 : : isn't, we don't need anything but an unconditional set. */
4185 : :
4186 : : /* Look and see if A and B are really the same. Avoid creating silly
4187 : : cmove constructs that no one will fix up later. */
4188 : 324543 : if (noce_simple_bbs (if_info)
4189 : 299202 : && rtx_interchangeable_p (a, b))
4190 : : {
4191 : : /* If we have an INSN_B, we don't have to create any new rtl. Just
4192 : : move the instruction that we already have. If we don't have an
4193 : : INSN_B, that means that A == X, and we've got a noop move. In
4194 : : that case don't do anything and let the code below delete INSN_A. */
4195 : 584 : if (insn_b && else_bb)
4196 : : {
4197 : 570 : rtx note;
4198 : :
4199 : 570 : if (else_bb && insn_b == BB_END (else_bb))
4200 : 276 : BB_END (else_bb) = PREV_INSN (insn_b);
4201 : 570 : reorder_insns (insn_b, insn_b, PREV_INSN (jump));
4202 : :
4203 : : /* If there was a REG_EQUAL note, delete it since it may have been
4204 : : true due to this insn being after a jump. */
4205 : 570 : if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
4206 : 6 : remove_note (insn_b, note);
4207 : :
4208 : 584 : insn_b = NULL;
4209 : : }
4210 : : /* If we have "x = b; if (...) x = a;", and x has side-effects, then
4211 : : x must be executed twice. */
4212 : 14 : else if (insn_b && side_effects_p (orig_x))
4213 : : return false;
4214 : :
4215 : 584 : x = orig_x;
4216 : 584 : goto success;
4217 : : }
4218 : :
4219 : 323959 : if (!set_b && MEM_P (orig_x))
4220 : : /* We want to avoid store speculation to avoid cases like
4221 : : if (pthread_mutex_trylock(mutex))
4222 : : ++global_variable;
4223 : : Rather than go to much effort here, we rely on the SSA optimizers,
4224 : : which do a good enough job these days. */
4225 : : return false;
4226 : :
4227 : 264526 : if (noce_try_move (if_info))
4228 : 0 : goto success;
4229 : 264526 : if (noce_try_ifelse_collapse (if_info))
4230 : 2088 : goto success;
4231 : 262438 : if (noce_try_store_flag (if_info))
4232 : 16227 : goto success;
4233 : 246211 : if (noce_try_bitop (if_info))
4234 : 0 : goto success;
4235 : 246211 : if (noce_try_minmax (if_info))
4236 : 59 : goto success;
4237 : 246152 : if (noce_try_abs (if_info))
4238 : 24 : goto success;
4239 : 246128 : if (noce_try_inverse_constants (if_info))
4240 : 0 : goto success;
4241 : 246128 : if (!targetm.have_conditional_execution ()
4242 : 246128 : && noce_try_store_flag_constants (if_info))
4243 : 2110 : goto success;
4244 : 244018 : if (HAVE_conditional_move
4245 : 244018 : && noce_try_cmove (if_info))
4246 : 42034 : goto success;
4247 : 201984 : if (! targetm.have_conditional_execution ())
4248 : : {
4249 : 201984 : if (noce_try_addcc (if_info))
4250 : 5656 : goto success;
4251 : 196328 : if (noce_try_store_flag_mask (if_info))
4252 : 2 : goto success;
4253 : 196326 : if (HAVE_conditional_move
4254 : 196326 : && noce_try_cond_zero_arith (if_info))
4255 : 0 : goto success;
4256 : 196326 : if (HAVE_conditional_move
4257 : 196326 : && noce_try_cmove_arith (if_info))
4258 : 35622 : goto success;
4259 : 160704 : if (noce_try_sign_mask (if_info))
4260 : 0 : goto success;
4261 : : }
4262 : :
4263 : 160704 : if (!else_bb && set_b)
4264 : : {
4265 : 48022 : insn_b = NULL;
4266 : 48022 : set_b = NULL_RTX;
4267 : 48022 : b = orig_x;
4268 : 48022 : goto retry;
4269 : : }
4270 : :
4271 : : return false;
4272 : :
4273 : 104406 : success:
4274 : 104406 : if (dump_file && if_info->transform_name)
4275 : 5 : fprintf (dump_file, "if-conversion succeeded through %s\n",
4276 : : if_info->transform_name);
4277 : :
4278 : : /* If we used a temporary, fix it up now. */
4279 : 104406 : if (orig_x != x)
4280 : : {
4281 : 750 : rtx_insn *seq;
4282 : :
4283 : 750 : start_sequence ();
4284 : 750 : noce_emit_move_insn (orig_x, x);
4285 : 750 : seq = get_insns ();
4286 : 750 : set_used_flags (orig_x);
4287 : 750 : unshare_all_rtl_in_chain (seq);
4288 : 750 : end_sequence ();
4289 : :
4290 : 750 : emit_insn_before_setloc (seq, BB_END (test_bb), INSN_LOCATION (insn_a));
4291 : : }
4292 : :
4293 : : /* The original THEN and ELSE blocks may now be removed. The test block
4294 : : must now jump to the join block. If the test block and the join block
4295 : : can be merged, do so. */
4296 : 104406 : if (else_bb)
4297 : : {
4298 : 38630 : delete_basic_block (else_bb);
4299 : 38630 : num_true_changes++;
4300 : : }
4301 : : else
4302 : 65776 : remove_edge (find_edge (test_bb, join_bb));
4303 : :
4304 : 104406 : remove_edge (find_edge (then_bb, join_bb));
4305 : 104406 : redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
4306 : 104406 : delete_basic_block (then_bb);
4307 : 104406 : num_true_changes++;
4308 : :
4309 : 104406 : if (can_merge_blocks_p (test_bb, join_bb))
4310 : : {
4311 : 76955 : merge_blocks (test_bb, join_bb);
4312 : 76955 : num_true_changes++;
4313 : : }
4314 : :
4315 : 104406 : num_updated_if_blocks++;
4316 : 104406 : return true;
4317 : : }
4318 : :
4319 : : /* Check whether a block is suitable for conditional move conversion.
4320 : : Every insn must be a simple set of a register to a constant or a
4321 : : register. For each assignment, store the value in the pointer map
4322 : : VALS, keyed indexed by register pointer, then store the register
4323 : : pointer in REGS. COND is the condition we will test. */
4324 : :
4325 : : static bool
4326 : 1710875 : check_cond_move_block (basic_block bb,
4327 : : hash_map<rtx, rtx> *vals,
4328 : : vec<rtx> *regs,
4329 : : rtx cond)
4330 : : {
4331 : 1710875 : rtx_insn *insn;
4332 : 1710875 : rtx cc = cc_in_cond (cond);
4333 : :
4334 : : /* We can only handle simple jumps at the end of the basic block.
4335 : : It is almost impossible to update the CFG otherwise. */
4336 : 1710875 : insn = BB_END (bb);
4337 : 1710875 : if (JUMP_P (insn) && !onlyjump_p (insn))
4338 : : return false;
4339 : :
4340 : 8206344 : FOR_BB_INSNS (bb, insn)
4341 : : {
4342 : 8129245 : rtx set, dest, src;
4343 : :
4344 : 8129245 : if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
4345 : 6333236 : continue;
4346 : 1796009 : set = single_set (insn);
4347 : 1796009 : if (!set)
4348 : 1633729 : return false;
4349 : :
4350 : 1724739 : dest = SET_DEST (set);
4351 : 1724739 : src = SET_SRC (set);
4352 : 1724739 : if (!REG_P (dest)
4353 : 1724739 : || (HARD_REGISTER_P (dest)
4354 : 269982 : && targetm.small_register_classes_for_mode_p (GET_MODE (dest))))
4355 : 618422 : return false;
4356 : :
4357 : 1106317 : if (!CONSTANT_P (src) && !register_operand (src, VOIDmode))
4358 : : return false;
4359 : :
4360 : 187936 : if (side_effects_p (src) || side_effects_p (dest))
4361 : 0 : return false;
4362 : :
4363 : 187936 : if (may_trap_p (src) || may_trap_p (dest))
4364 : 0 : return false;
4365 : :
4366 : : /* Don't try to handle this if the source register was
4367 : : modified earlier in the block. */
4368 : 187936 : if ((REG_P (src)
4369 : 49388 : && vals->get (src))
4370 : 237064 : || (GET_CODE (src) == SUBREG && REG_P (SUBREG_REG (src))
4371 : 3886 : && vals->get (SUBREG_REG (src))))
4372 : 261 : return false;
4373 : :
4374 : : /* Don't try to handle this if the destination register was
4375 : : modified earlier in the block. */
4376 : 187675 : if (vals->get (dest))
4377 : : return false;
4378 : :
4379 : : /* Don't try to handle this if the condition uses the
4380 : : destination register. */
4381 : 187675 : if (reg_overlap_mentioned_p (dest, cond))
4382 : : return false;
4383 : :
4384 : : /* Don't try to handle this if the source register is modified
4385 : : later in the block. */
4386 : 165510 : if (!CONSTANT_P (src)
4387 : 165510 : && modified_between_p (src, insn, NEXT_INSN (BB_END (bb))))
4388 : : return false;
4389 : :
4390 : : /* Skip it if the instruction to be moved might clobber CC. */
4391 : 162280 : if (cc && set_of (cc, insn))
4392 : : return false;
4393 : :
4394 : 162280 : vals->put (dest, src);
4395 : :
4396 : 162280 : regs->safe_push (dest);
4397 : : }
4398 : :
4399 : : return true;
4400 : : }
4401 : :
4402 : : /* Given a basic block BB suitable for conditional move conversion,
4403 : : a condition COND, and pointer maps THEN_VALS and ELSE_VALS containing
4404 : : the register values depending on COND, emit the insns in the block as
4405 : : conditional moves. If ELSE_BLOCK is true, THEN_BB was already
4406 : : processed. The caller has started a sequence for the conversion.
4407 : : Return true if successful, false if something goes wrong. */
4408 : :
4409 : : static bool
4410 : 54544 : cond_move_convert_if_block (struct noce_if_info *if_infop,
4411 : : basic_block bb, rtx cond,
4412 : : hash_map<rtx, rtx> *then_vals,
4413 : : hash_map<rtx, rtx> *else_vals,
4414 : : bool else_block_p)
4415 : : {
4416 : 54544 : enum rtx_code code;
4417 : 54544 : rtx_insn *insn;
4418 : 54544 : rtx cond_arg0, cond_arg1;
4419 : :
4420 : 54544 : code = GET_CODE (cond);
4421 : 54544 : cond_arg0 = XEXP (cond, 0);
4422 : 54544 : cond_arg1 = XEXP (cond, 1);
4423 : :
4424 : 209048 : FOR_BB_INSNS (bb, insn)
4425 : : {
4426 : 169892 : rtx set, target, dest, t, e;
4427 : :
4428 : : /* ??? Maybe emit conditional debug insn? */
4429 : 169892 : if (!NONDEBUG_INSN_P (insn) || JUMP_P (insn))
4430 : 111363 : continue;
4431 : 87364 : set = single_set (insn);
4432 : 87364 : gcc_assert (set && REG_P (SET_DEST (set)));
4433 : :
4434 : 87364 : dest = SET_DEST (set);
4435 : :
4436 : 87364 : rtx *then_slot = then_vals->get (dest);
4437 : 87364 : rtx *else_slot = else_vals->get (dest);
4438 : 87364 : t = then_slot ? *then_slot : NULL_RTX;
4439 : 87364 : e = else_slot ? *else_slot : NULL_RTX;
4440 : :
4441 : 87364 : if (else_block_p)
4442 : : {
4443 : : /* If this register was set in the then block, we already
4444 : : handled this case there. */
4445 : 30942 : if (t)
4446 : 28835 : continue;
4447 : 2107 : t = dest;
4448 : 2107 : gcc_assert (e);
4449 : : }
4450 : : else
4451 : : {
4452 : 56422 : gcc_assert (t);
4453 : 56422 : if (!e)
4454 : 26738 : e = dest;
4455 : : }
4456 : :
4457 : 58529 : if (if_infop->cond_inverted)
4458 : 0 : std::swap (t, e);
4459 : :
4460 : 58529 : target = noce_emit_cmove (if_infop, dest, code, cond_arg0, cond_arg1,
4461 : : t, e);
4462 : 58529 : if (!target)
4463 : 15388 : return false;
4464 : :
4465 : 43141 : if (target != dest)
4466 : 0 : noce_emit_move_insn (dest, target);
4467 : : }
4468 : :
4469 : : return true;
4470 : : }
4471 : :
4472 : : /* Given a simple IF-THEN-JOIN or IF-THEN-ELSE-JOIN block, attempt to convert
4473 : : it using only conditional moves. Return TRUE if we were successful at
4474 : : converting the block. */
4475 : :
4476 : : static bool
4477 : 1676060 : cond_move_process_if_block (struct noce_if_info *if_info)
4478 : : {
4479 : 1676060 : basic_block test_bb = if_info->test_bb;
4480 : 1676060 : basic_block then_bb = if_info->then_bb;
4481 : 1676060 : basic_block else_bb = if_info->else_bb;
4482 : 1676060 : basic_block join_bb = if_info->join_bb;
4483 : 1676060 : rtx_insn *jump = if_info->jump;
4484 : 1676060 : rtx cond = if_info->cond;
4485 : 1676060 : rtx_insn *seq, *loc_insn;
4486 : 1676060 : int c;
4487 : 1676060 : vec<rtx> then_regs = vNULL;
4488 : 1676060 : vec<rtx> else_regs = vNULL;
4489 : 1676060 : bool success_p = false;
4490 : 1676060 : int limit = param_max_rtl_if_conversion_insns;
4491 : :
4492 : : /* Build a mapping for each block to the value used for each
4493 : : register. */
4494 : 1676060 : hash_map<rtx, rtx> then_vals;
4495 : 1676060 : hash_map<rtx, rtx> else_vals;
4496 : :
4497 : : /* Make sure the blocks are suitable. */
4498 : 1676060 : if (!check_cond_move_block (then_bb, &then_vals, &then_regs, cond)
4499 : 1676060 : || (else_bb
4500 : 34815 : && !check_cond_move_block (else_bb, &else_vals, &else_regs, cond)))
4501 : 1633776 : goto done;
4502 : :
4503 : : /* Make sure the blocks can be used together. If the same register
4504 : : is set in both blocks, and is not set to a constant in both
4505 : : cases, then both blocks must set it to the same register. We
4506 : : have already verified that if it is set to a register, that the
4507 : : source register does not change after the assignment. Also count
4508 : : the number of registers set in only one of the blocks. */
4509 : 42284 : c = 0;
4510 : 183933 : for (rtx reg : then_regs)
4511 : : {
4512 : 59907 : rtx *then_slot = then_vals.get (reg);
4513 : 59907 : rtx *else_slot = else_vals.get (reg);
4514 : :
4515 : 59907 : gcc_checking_assert (then_slot);
4516 : 59907 : if (!else_slot)
4517 : 27335 : ++c;
4518 : : else
4519 : : {
4520 : 32572 : rtx then_val = *then_slot;
4521 : 32572 : rtx else_val = *else_slot;
4522 : 3357 : if (!CONSTANT_P (then_val) && !CONSTANT_P (else_val)
4523 : 35192 : && !rtx_equal_p (then_val, else_val))
4524 : 2490 : goto done;
4525 : : }
4526 : : }
4527 : :
4528 : : /* Finish off c for MAX_CONDITIONAL_EXECUTE. */
4529 : 103477 : for (rtx reg : else_regs)
4530 : : {
4531 : 32307 : gcc_checking_assert (else_vals.get (reg));
4532 : 32307 : if (!then_vals.get (reg))
4533 : 2237 : ++c;
4534 : : }
4535 : :
4536 : : /* Make sure it is reasonable to convert this block. What matters
4537 : : is the number of assignments currently made in only one of the
4538 : : branches, since if we convert we are going to always execute
4539 : : them. */
4540 : 39794 : if (c > MAX_CONDITIONAL_EXECUTE
4541 : 39794 : || c > limit)
4542 : 20 : goto done;
4543 : :
4544 : : /* Try to emit the conditional moves. First do the then block,
4545 : : then do anything left in the else blocks. */
4546 : 39774 : start_sequence ();
4547 : 39774 : if (!cond_move_convert_if_block (if_info, then_bb, cond,
4548 : : &then_vals, &else_vals, false)
4549 : 39774 : || (else_bb
4550 : 14770 : && !cond_move_convert_if_block (if_info, else_bb, cond,
4551 : : &then_vals, &else_vals, true)))
4552 : : {
4553 : 15388 : end_sequence ();
4554 : 15388 : goto done;
4555 : : }
4556 : 24386 : seq = end_ifcvt_sequence (if_info);
4557 : 24386 : if (!seq || !targetm.noce_conversion_profitable_p (seq, if_info))
4558 : 20588 : goto done;
4559 : :
4560 : 3798 : loc_insn = first_active_insn (then_bb);
4561 : 3798 : if (!loc_insn)
4562 : : {
4563 : 4 : loc_insn = first_active_insn (else_bb);
4564 : 4 : gcc_assert (loc_insn);
4565 : : }
4566 : 3798 : emit_insn_before_setloc (seq, jump, INSN_LOCATION (loc_insn));
4567 : :
4568 : 3798 : if (else_bb)
4569 : : {
4570 : 3798 : delete_basic_block (else_bb);
4571 : 3798 : num_true_changes++;
4572 : : }
4573 : : else
4574 : 0 : remove_edge (find_edge (test_bb, join_bb));
4575 : :
4576 : 3798 : remove_edge (find_edge (then_bb, join_bb));
4577 : 3798 : redirect_edge_and_branch_force (single_succ_edge (test_bb), join_bb);
4578 : 3798 : delete_basic_block (then_bb);
4579 : 3798 : num_true_changes++;
4580 : :
4581 : 3798 : if (can_merge_blocks_p (test_bb, join_bb))
4582 : : {
4583 : 696 : merge_blocks (test_bb, join_bb);
4584 : 696 : num_true_changes++;
4585 : : }
4586 : :
4587 : 3798 : num_updated_if_blocks++;
4588 : 3798 : success_p = true;
4589 : :
4590 : 1676060 : done:
4591 : 1676060 : then_regs.release ();
4592 : 1676060 : else_regs.release ();
4593 : 1676060 : return success_p;
4594 : 1676060 : }
4595 : :
4596 : :
4597 : : /* Determine if a given basic block heads a simple IF-THEN-JOIN or an
4598 : : IF-THEN-ELSE-JOIN block.
4599 : :
4600 : : If so, we'll try to convert the insns to not require the branch,
4601 : : using only transformations that do not require conditional execution.
4602 : :
4603 : : Return TRUE if we were successful at converting the block. */
4604 : :
4605 : : static bool
4606 : 9204961 : noce_find_if_block (basic_block test_bb, edge then_edge, edge else_edge,
4607 : : int pass)
4608 : : {
4609 : 9204961 : basic_block then_bb, else_bb, join_bb;
4610 : 9204961 : bool then_else_reversed = false;
4611 : 9204961 : rtx_insn *jump;
4612 : 9204961 : rtx_insn *cond_earliest;
4613 : 9204961 : struct noce_if_info if_info;
4614 : 9204961 : bool speed_p = optimize_bb_for_speed_p (test_bb);
4615 : :
4616 : : /* We only ever should get here before reload. */
4617 : 9204961 : gcc_assert (!reload_completed);
4618 : :
4619 : : /* Recognize an IF-THEN-ELSE-JOIN block. */
4620 : 9204961 : if (single_pred_p (then_edge->dest)
4621 : 7497204 : && single_succ_p (then_edge->dest)
4622 : 3223157 : && single_pred_p (else_edge->dest)
4623 : 9478875 : && single_succ_p (else_edge->dest)
4624 : 10280635 : && single_succ (then_edge->dest) == single_succ (else_edge->dest))
4625 : : {
4626 : : then_bb = then_edge->dest;
4627 : : else_bb = else_edge->dest;
4628 : : join_bb = single_succ (then_bb);
4629 : : }
4630 : : /* Recognize an IF-THEN-JOIN block. */
4631 : 8403201 : else if (single_pred_p (then_edge->dest)
4632 : 9897043 : && single_succ_p (then_edge->dest)
4633 : 10824598 : && single_succ (then_edge->dest) == else_edge->dest)
4634 : : {
4635 : : then_bb = then_edge->dest;
4636 : : else_bb = NULL_BLOCK;
4637 : : join_bb = else_edge->dest;
4638 : : }
4639 : : /* Recognize an IF-ELSE-JOIN block. We can have those because the order
4640 : : of basic blocks in cfglayout mode does not matter, so the fallthrough
4641 : : edge can go to any basic block (and not just to bb->next_bb, like in
4642 : : cfgrtl mode). */
4643 : 16552044 : else if (single_pred_p (else_edge->dest)
4644 : 10567461 : && single_succ_p (else_edge->dest)
4645 : 8966709 : && single_succ (else_edge->dest) == then_edge->dest)
4646 : : {
4647 : : /* The noce transformations do not apply to IF-ELSE-JOIN blocks.
4648 : : To make this work, we have to invert the THEN and ELSE blocks
4649 : : and reverse the jump condition. */
4650 : : then_bb = else_edge->dest;
4651 : : else_bb = NULL_BLOCK;
4652 : : join_bb = single_succ (then_bb);
4653 : : then_else_reversed = true;
4654 : : }
4655 : : else
4656 : : /* Not a form we can handle. */
4657 : : return false;
4658 : :
4659 : : /* The edges of the THEN and ELSE blocks cannot have complex edges. */
4660 : 1863157 : if (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
4661 : : return false;
4662 : 1811339 : if (else_bb
4663 : 1811339 : && single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
4664 : : return false;
4665 : :
4666 : 1801026 : num_possible_if_blocks++;
4667 : :
4668 : 1801026 : if (dump_file)
4669 : : {
4670 : 90 : fprintf (dump_file,
4671 : : "\nIF-THEN%s-JOIN block found, pass %d, test %d, then %d",
4672 : : (else_bb) ? "-ELSE" : "",
4673 : : pass, test_bb->index, then_bb->index);
4674 : :
4675 : 53 : if (else_bb)
4676 : 16 : fprintf (dump_file, ", else %d", else_bb->index);
4677 : :
4678 : 53 : fprintf (dump_file, ", join %d\n", join_bb->index);
4679 : : }
4680 : :
4681 : : /* If the conditional jump is more than just a conditional
4682 : : jump, then we cannot do if-conversion on this block. */
4683 : 1801026 : jump = BB_END (test_bb);
4684 : 1801026 : if (! onlyjump_p (jump))
4685 : : return false;
4686 : :
4687 : : /* Initialize an IF_INFO struct to pass around. */
4688 : 1800825 : memset (&if_info, 0, sizeof if_info);
4689 : 1800825 : if_info.test_bb = test_bb;
4690 : 1800825 : if_info.then_bb = then_bb;
4691 : 1800825 : if_info.else_bb = else_bb;
4692 : 1800825 : if_info.join_bb = join_bb;
4693 : 1800825 : if_info.cond = noce_get_condition (jump, &cond_earliest,
4694 : : then_else_reversed);
4695 : 1800825 : rtx_insn *rev_cond_earliest;
4696 : 3601650 : if_info.rev_cond = noce_get_condition (jump, &rev_cond_earliest,
4697 : 1800825 : !then_else_reversed);
4698 : 1800825 : if (!if_info.cond && !if_info.rev_cond)
4699 : : return false;
4700 : 1800825 : if (!if_info.cond)
4701 : : {
4702 : 0 : std::swap (if_info.cond, if_info.rev_cond);
4703 : 0 : std::swap (cond_earliest, rev_cond_earliest);
4704 : 0 : if_info.cond_inverted = true;
4705 : : }
4706 : : /* We must be comparing objects whose modes imply the size. */
4707 : 1800825 : if (GET_MODE (XEXP (if_info.cond, 0)) == BLKmode)
4708 : : return false;
4709 : 1800825 : gcc_assert (if_info.rev_cond == NULL_RTX
4710 : : || rev_cond_earliest == cond_earliest);
4711 : 1800825 : if_info.cond_earliest = cond_earliest;
4712 : 1800825 : if_info.jump = jump;
4713 : 1800825 : if_info.then_else_reversed = then_else_reversed;
4714 : 1800825 : if_info.speed_p = speed_p;
4715 : 1800825 : if_info.max_seq_cost
4716 : 1800825 : = targetm.max_noce_ifcvt_seq_cost (then_edge);
4717 : : /* We'll add in the cost of THEN_BB and ELSE_BB later, when we check
4718 : : that they are valid to transform. We can't easily get back to the insn
4719 : : for COND (and it may not exist if we had to canonicalize to get COND),
4720 : : and jump_insns are always given a cost of 1 by seq_cost, so treat
4721 : : both instructions as having cost COSTS_N_INSNS (1). */
4722 : 1800825 : if_info.original_cost = COSTS_N_INSNS (2);
4723 : :
4724 : :
4725 : : /* Do the real work. */
4726 : :
4727 : : /* ??? noce_process_if_block has not yet been updated to handle
4728 : : inverted conditions. */
4729 : 1800825 : if (!if_info.cond_inverted && noce_process_if_block (&if_info))
4730 : : return true;
4731 : :
4732 : 1676060 : if (HAVE_conditional_move
4733 : 1676060 : && cond_move_process_if_block (&if_info))
4734 : : return true;
4735 : :
4736 : : return false;
4737 : : }
4738 : :
4739 : :
4740 : : /* Merge the blocks and mark for local life update. */
4741 : :
4742 : : static void
4743 : 0 : merge_if_block (struct ce_if_block * ce_info)
4744 : : {
4745 : 0 : basic_block test_bb = ce_info->test_bb; /* last test block */
4746 : 0 : basic_block then_bb = ce_info->then_bb; /* THEN */
4747 : 0 : basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
4748 : 0 : basic_block join_bb = ce_info->join_bb; /* join block */
4749 : 0 : basic_block combo_bb;
4750 : :
4751 : : /* All block merging is done into the lower block numbers. */
4752 : :
4753 : 0 : combo_bb = test_bb;
4754 : 0 : df_set_bb_dirty (test_bb);
4755 : :
4756 : : /* Merge any basic blocks to handle && and || subtests. Each of
4757 : : the blocks are on the fallthru path from the predecessor block. */
4758 : 0 : if (ce_info->num_multiple_test_blocks > 0)
4759 : : {
4760 : 0 : basic_block bb = test_bb;
4761 : 0 : basic_block last_test_bb = ce_info->last_test_bb;
4762 : 0 : basic_block fallthru = block_fallthru (bb);
4763 : :
4764 : 0 : do
4765 : : {
4766 : 0 : bb = fallthru;
4767 : 0 : fallthru = block_fallthru (bb);
4768 : 0 : merge_blocks (combo_bb, bb);
4769 : 0 : num_true_changes++;
4770 : : }
4771 : 0 : while (bb != last_test_bb);
4772 : : }
4773 : :
4774 : : /* Merge TEST block into THEN block. Normally the THEN block won't have a
4775 : : label, but it might if there were || tests. That label's count should be
4776 : : zero, and it normally should be removed. */
4777 : :
4778 : 0 : if (then_bb)
4779 : : {
4780 : : /* If THEN_BB has no successors, then there's a BARRIER after it.
4781 : : If COMBO_BB has more than one successor (THEN_BB), then that BARRIER
4782 : : is no longer needed, and in fact it is incorrect to leave it in
4783 : : the insn stream. */
4784 : 0 : if (EDGE_COUNT (then_bb->succs) == 0
4785 : 0 : && EDGE_COUNT (combo_bb->succs) > 1)
4786 : : {
4787 : 0 : rtx_insn *end = NEXT_INSN (BB_END (then_bb));
4788 : 0 : while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4789 : 0 : end = NEXT_INSN (end);
4790 : :
4791 : 0 : if (end && BARRIER_P (end))
4792 : 0 : delete_insn (end);
4793 : : }
4794 : 0 : merge_blocks (combo_bb, then_bb);
4795 : 0 : num_true_changes++;
4796 : : }
4797 : :
4798 : : /* The ELSE block, if it existed, had a label. That label count
4799 : : will almost always be zero, but odd things can happen when labels
4800 : : get their addresses taken. */
4801 : 0 : if (else_bb)
4802 : : {
4803 : : /* If ELSE_BB has no successors, then there's a BARRIER after it.
4804 : : If COMBO_BB has more than one successor (ELSE_BB), then that BARRIER
4805 : : is no longer needed, and in fact it is incorrect to leave it in
4806 : : the insn stream. */
4807 : 0 : if (EDGE_COUNT (else_bb->succs) == 0
4808 : 0 : && EDGE_COUNT (combo_bb->succs) > 1)
4809 : : {
4810 : 0 : rtx_insn *end = NEXT_INSN (BB_END (else_bb));
4811 : 0 : while (end && NOTE_P (end) && !NOTE_INSN_BASIC_BLOCK_P (end))
4812 : 0 : end = NEXT_INSN (end);
4813 : :
4814 : 0 : if (end && BARRIER_P (end))
4815 : 0 : delete_insn (end);
4816 : : }
4817 : 0 : merge_blocks (combo_bb, else_bb);
4818 : 0 : num_true_changes++;
4819 : : }
4820 : :
4821 : : /* If there was no join block reported, that means it was not adjacent
4822 : : to the others, and so we cannot merge them. */
4823 : :
4824 : 0 : if (! join_bb)
4825 : : {
4826 : 0 : rtx_insn *last = BB_END (combo_bb);
4827 : :
4828 : : /* The outgoing edge for the current COMBO block should already
4829 : : be correct. Verify this. */
4830 : 0 : if (EDGE_COUNT (combo_bb->succs) == 0)
4831 : 0 : gcc_assert (find_reg_note (last, REG_NORETURN, NULL)
4832 : : || (NONJUMP_INSN_P (last)
4833 : : && GET_CODE (PATTERN (last)) == TRAP_IF
4834 : : && (TRAP_CONDITION (PATTERN (last))
4835 : : == const_true_rtx)));
4836 : :
4837 : : else
4838 : : /* There should still be something at the end of the THEN or ELSE
4839 : : blocks taking us to our final destination. */
4840 : 0 : gcc_assert (JUMP_P (last)
4841 : : || (EDGE_SUCC (combo_bb, 0)->dest
4842 : : == EXIT_BLOCK_PTR_FOR_FN (cfun)
4843 : : && CALL_P (last)
4844 : : && SIBLING_CALL_P (last))
4845 : : || ((EDGE_SUCC (combo_bb, 0)->flags & EDGE_EH)
4846 : : && can_throw_internal (last)));
4847 : : }
4848 : :
4849 : : /* The JOIN block may have had quite a number of other predecessors too.
4850 : : Since we've already merged the TEST, THEN and ELSE blocks, we should
4851 : : have only one remaining edge from our if-then-else diamond. If there
4852 : : is more than one remaining edge, it must come from elsewhere. There
4853 : : may be zero incoming edges if the THEN block didn't actually join
4854 : : back up (as with a call to a non-return function). */
4855 : 0 : else if (EDGE_COUNT (join_bb->preds) < 2
4856 : 0 : && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4857 : : {
4858 : : /* We can merge the JOIN cleanly and update the dataflow try
4859 : : again on this pass.*/
4860 : 0 : merge_blocks (combo_bb, join_bb);
4861 : 0 : num_true_changes++;
4862 : : }
4863 : : else
4864 : : {
4865 : : /* We cannot merge the JOIN. */
4866 : :
4867 : : /* The outgoing edge for the current COMBO block should already
4868 : : be correct. Verify this. */
4869 : 0 : gcc_assert (single_succ_p (combo_bb)
4870 : : && single_succ (combo_bb) == join_bb);
4871 : :
4872 : : /* Remove the jump and cruft from the end of the COMBO block. */
4873 : 0 : if (join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
4874 : 0 : tidy_fallthru_edge (single_succ_edge (combo_bb));
4875 : : }
4876 : :
4877 : 0 : num_updated_if_blocks++;
4878 : 0 : }
4879 : :
4880 : : /* Find a block ending in a simple IF condition and try to transform it
4881 : : in some way. When converting a multi-block condition, put the new code
4882 : : in the first such block and delete the rest. Return a pointer to this
4883 : : first block if some transformation was done. Return NULL otherwise. */
4884 : :
4885 : : static basic_block
4886 : 37498073 : find_if_header (basic_block test_bb, int pass)
4887 : : {
4888 : 37498073 : ce_if_block ce_info;
4889 : 37498073 : edge then_edge;
4890 : 37498073 : edge else_edge;
4891 : :
4892 : : /* The kind of block we're looking for has exactly two successors. */
4893 : 56446810 : if (EDGE_COUNT (test_bb->succs) != 2)
4894 : : return NULL;
4895 : :
4896 : 19280311 : then_edge = EDGE_SUCC (test_bb, 0);
4897 : 19280311 : else_edge = EDGE_SUCC (test_bb, 1);
4898 : :
4899 : 19280311 : if (df_get_bb_dirty (then_edge->dest))
4900 : : return NULL;
4901 : 19258815 : if (df_get_bb_dirty (else_edge->dest))
4902 : : return NULL;
4903 : :
4904 : : /* Neither edge should be abnormal. */
4905 : 19209391 : if ((then_edge->flags & EDGE_COMPLEX)
4906 : 17482929 : || (else_edge->flags & EDGE_COMPLEX))
4907 : : return NULL;
4908 : :
4909 : : /* Nor exit the loop. */
4910 : 17044494 : if ((then_edge->flags & EDGE_LOOP_EXIT)
4911 : 15599234 : || (else_edge->flags & EDGE_LOOP_EXIT))
4912 : : return NULL;
4913 : :
4914 : : /* The THEN edge is canonically the one that falls through. */
4915 : 13529115 : if (then_edge->flags & EDGE_FALLTHRU)
4916 : : ;
4917 : 6687581 : else if (else_edge->flags & EDGE_FALLTHRU)
4918 : : std::swap (then_edge, else_edge);
4919 : : else
4920 : : /* Otherwise this must be a multiway branch of some sort. */
4921 : : return NULL;
4922 : :
4923 : 13528937 : memset (&ce_info, 0, sizeof (ce_info));
4924 : 13528937 : ce_info.test_bb = test_bb;
4925 : 13528937 : ce_info.then_bb = then_edge->dest;
4926 : 13528937 : ce_info.else_bb = else_edge->dest;
4927 : 13528937 : ce_info.pass = pass;
4928 : :
4929 : : #ifdef IFCVT_MACHDEP_INIT
4930 : : IFCVT_MACHDEP_INIT (&ce_info);
4931 : : #endif
4932 : :
4933 : 13528937 : if (!reload_completed
4934 : 13528937 : && noce_find_if_block (test_bb, then_edge, else_edge, pass))
4935 : 128563 : goto success;
4936 : :
4937 : 13400374 : if (reload_completed
4938 : 4323976 : && targetm.have_conditional_execution ()
4939 : 13400374 : && cond_exec_find_if_block (&ce_info))
4940 : 0 : goto success;
4941 : :
4942 : 13400374 : if (targetm.have_trap ()
4943 : 13400374 : && optab_handler (ctrap_optab, word_mode) != CODE_FOR_nothing
4944 : 13400374 : && find_cond_trap (test_bb, then_edge, else_edge))
4945 : 0 : goto success;
4946 : :
4947 : 13400374 : if (dom_info_state (CDI_POST_DOMINATORS) >= DOM_NO_FAST_QUERY
4948 : 13400374 : && (reload_completed || !targetm.have_conditional_execution ()))
4949 : : {
4950 : 13400374 : if (find_if_case_1 (test_bb, then_edge, else_edge))
4951 : 17857 : goto success;
4952 : 13382517 : if (find_if_case_2 (test_bb, then_edge, else_edge))
4953 : 185154 : goto success;
4954 : : }
4955 : :
4956 : : return NULL;
4957 : :
4958 : 331574 : success:
4959 : 331574 : if (dump_file)
4960 : 17 : fprintf (dump_file, "Conversion succeeded on pass %d.\n", pass);
4961 : : /* Set this so we continue looking. */
4962 : 331574 : cond_exec_changed_p = true;
4963 : 331574 : return ce_info.test_bb;
4964 : : }
4965 : :
4966 : : /* Return true if a block has two edges, one of which falls through to the next
4967 : : block, and the other jumps to a specific block, so that we can tell if the
4968 : : block is part of an && test or an || test. Returns either -1 or the number
4969 : : of non-note, non-jump, non-USE/CLOBBER insns in the block. */
4970 : :
4971 : : static int
4972 : 0 : block_jumps_and_fallthru (basic_block cur_bb, basic_block target_bb)
4973 : : {
4974 : 0 : edge cur_edge;
4975 : 0 : bool fallthru_p = false;
4976 : 0 : bool jump_p = false;
4977 : 0 : rtx_insn *insn;
4978 : 0 : rtx_insn *end;
4979 : 0 : int n_insns = 0;
4980 : 0 : edge_iterator ei;
4981 : :
4982 : 0 : if (!cur_bb || !target_bb)
4983 : : return -1;
4984 : :
4985 : : /* If no edges, obviously it doesn't jump or fallthru. */
4986 : 0 : if (EDGE_COUNT (cur_bb->succs) == 0)
4987 : : return 0;
4988 : :
4989 : 0 : FOR_EACH_EDGE (cur_edge, ei, cur_bb->succs)
4990 : : {
4991 : 0 : if (cur_edge->flags & EDGE_COMPLEX)
4992 : : /* Anything complex isn't what we want. */
4993 : : return -1;
4994 : :
4995 : 0 : else if (cur_edge->flags & EDGE_FALLTHRU)
4996 : : fallthru_p = true;
4997 : :
4998 : 0 : else if (cur_edge->dest == target_bb)
4999 : : jump_p = true;
5000 : :
5001 : : else
5002 : : return -1;
5003 : : }
5004 : :
5005 : 0 : if ((jump_p & fallthru_p) == 0)
5006 : : return -1;
5007 : :
5008 : : /* Don't allow calls in the block, since this is used to group && and ||
5009 : : together for conditional execution support. ??? we should support
5010 : : conditional execution support across calls for IA-64 some day, but
5011 : : for now it makes the code simpler. */
5012 : 0 : end = BB_END (cur_bb);
5013 : 0 : insn = BB_HEAD (cur_bb);
5014 : :
5015 : 0 : while (insn != NULL_RTX)
5016 : : {
5017 : 0 : if (CALL_P (insn))
5018 : : return -1;
5019 : :
5020 : 0 : if (INSN_P (insn)
5021 : 0 : && !JUMP_P (insn)
5022 : 0 : && !DEBUG_INSN_P (insn)
5023 : 0 : && GET_CODE (PATTERN (insn)) != USE
5024 : 0 : && GET_CODE (PATTERN (insn)) != CLOBBER)
5025 : 0 : n_insns++;
5026 : :
5027 : 0 : if (insn == end)
5028 : : break;
5029 : :
5030 : 0 : insn = NEXT_INSN (insn);
5031 : : }
5032 : :
5033 : : return n_insns;
5034 : : }
5035 : :
5036 : : /* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
5037 : : block. If so, we'll try to convert the insns to not require the branch.
5038 : : Return TRUE if we were successful at converting the block. */
5039 : :
5040 : : static bool
5041 : 0 : cond_exec_find_if_block (struct ce_if_block * ce_info)
5042 : : {
5043 : 0 : basic_block test_bb = ce_info->test_bb;
5044 : 0 : basic_block then_bb = ce_info->then_bb;
5045 : 0 : basic_block else_bb = ce_info->else_bb;
5046 : 0 : basic_block join_bb = NULL_BLOCK;
5047 : 0 : edge cur_edge;
5048 : 0 : basic_block next;
5049 : 0 : edge_iterator ei;
5050 : :
5051 : 0 : ce_info->last_test_bb = test_bb;
5052 : :
5053 : : /* We only ever should get here after reload,
5054 : : and if we have conditional execution. */
5055 : 0 : gcc_assert (reload_completed && targetm.have_conditional_execution ());
5056 : :
5057 : : /* Discover if any fall through predecessors of the current test basic block
5058 : : were && tests (which jump to the else block) or || tests (which jump to
5059 : : the then block). */
5060 : 0 : if (single_pred_p (test_bb)
5061 : 0 : && single_pred_edge (test_bb)->flags == EDGE_FALLTHRU)
5062 : : {
5063 : 0 : basic_block bb = single_pred (test_bb);
5064 : 0 : basic_block target_bb;
5065 : 0 : int max_insns = MAX_CONDITIONAL_EXECUTE;
5066 : 0 : int n_insns;
5067 : :
5068 : : /* Determine if the preceding block is an && or || block. */
5069 : 0 : if ((n_insns = block_jumps_and_fallthru (bb, else_bb)) >= 0)
5070 : : {
5071 : 0 : ce_info->and_and_p = true;
5072 : 0 : target_bb = else_bb;
5073 : : }
5074 : 0 : else if ((n_insns = block_jumps_and_fallthru (bb, then_bb)) >= 0)
5075 : : {
5076 : 0 : ce_info->and_and_p = false;
5077 : 0 : target_bb = then_bb;
5078 : : }
5079 : : else
5080 : : target_bb = NULL_BLOCK;
5081 : :
5082 : 0 : if (target_bb && n_insns <= max_insns)
5083 : : {
5084 : 0 : int total_insns = 0;
5085 : 0 : int blocks = 0;
5086 : :
5087 : 0 : ce_info->last_test_bb = test_bb;
5088 : :
5089 : : /* Found at least one && or || block, look for more. */
5090 : 0 : do
5091 : : {
5092 : 0 : ce_info->test_bb = test_bb = bb;
5093 : 0 : total_insns += n_insns;
5094 : 0 : blocks++;
5095 : :
5096 : 0 : if (!single_pred_p (bb))
5097 : : break;
5098 : :
5099 : 0 : bb = single_pred (bb);
5100 : 0 : n_insns = block_jumps_and_fallthru (bb, target_bb);
5101 : : }
5102 : 0 : while (n_insns >= 0 && (total_insns + n_insns) <= max_insns);
5103 : :
5104 : 0 : ce_info->num_multiple_test_blocks = blocks;
5105 : 0 : ce_info->num_multiple_test_insns = total_insns;
5106 : :
5107 : 0 : if (ce_info->and_and_p)
5108 : 0 : ce_info->num_and_and_blocks = blocks;
5109 : : else
5110 : 0 : ce_info->num_or_or_blocks = blocks;
5111 : : }
5112 : : }
5113 : :
5114 : : /* The THEN block of an IF-THEN combo must have exactly one predecessor,
5115 : : other than any || blocks which jump to the THEN block. */
5116 : 0 : if ((EDGE_COUNT (then_bb->preds) - ce_info->num_or_or_blocks) != 1)
5117 : : return false;
5118 : :
5119 : : /* The edges of the THEN and ELSE blocks cannot have complex edges. */
5120 : 0 : FOR_EACH_EDGE (cur_edge, ei, then_bb->preds)
5121 : : {
5122 : 0 : if (cur_edge->flags & EDGE_COMPLEX)
5123 : : return false;
5124 : : }
5125 : :
5126 : 0 : FOR_EACH_EDGE (cur_edge, ei, else_bb->preds)
5127 : : {
5128 : 0 : if (cur_edge->flags & EDGE_COMPLEX)
5129 : : return false;
5130 : : }
5131 : :
5132 : : /* The THEN block of an IF-THEN combo must have zero or one successors. */
5133 : 0 : if (EDGE_COUNT (then_bb->succs) > 0
5134 : 0 : && (!single_succ_p (then_bb)
5135 : 0 : || (single_succ_edge (then_bb)->flags & EDGE_COMPLEX)
5136 : 0 : || (epilogue_completed
5137 : 0 : && tablejump_p (BB_END (then_bb), NULL, NULL))))
5138 : 0 : return false;
5139 : :
5140 : : /* If the THEN block has no successors, conditional execution can still
5141 : : make a conditional call. Don't do this unless the ELSE block has
5142 : : only one incoming edge -- the CFG manipulation is too ugly otherwise.
5143 : : Check for the last insn of the THEN block being an indirect jump, which
5144 : : is listed as not having any successors, but confuses the rest of the CE
5145 : : code processing. ??? we should fix this in the future. */
5146 : 0 : if (EDGE_COUNT (then_bb->succs) == 0)
5147 : : {
5148 : 0 : if (single_pred_p (else_bb) && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5149 : : {
5150 : 0 : rtx_insn *last_insn = BB_END (then_bb);
5151 : :
5152 : 0 : while (last_insn
5153 : 0 : && NOTE_P (last_insn)
5154 : 0 : && last_insn != BB_HEAD (then_bb))
5155 : 0 : last_insn = PREV_INSN (last_insn);
5156 : :
5157 : 0 : if (last_insn
5158 : 0 : && JUMP_P (last_insn)
5159 : 0 : && ! simplejump_p (last_insn))
5160 : : return false;
5161 : :
5162 : : join_bb = else_bb;
5163 : : else_bb = NULL_BLOCK;
5164 : : }
5165 : : else
5166 : : return false;
5167 : : }
5168 : :
5169 : : /* If the THEN block's successor is the other edge out of the TEST block,
5170 : : then we have an IF-THEN combo without an ELSE. */
5171 : 0 : else if (single_succ (then_bb) == else_bb)
5172 : : {
5173 : : join_bb = else_bb;
5174 : : else_bb = NULL_BLOCK;
5175 : : }
5176 : :
5177 : : /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
5178 : : has exactly one predecessor and one successor, and the outgoing edge
5179 : : is not complex, then we have an IF-THEN-ELSE combo. */
5180 : 0 : else if (single_succ_p (else_bb)
5181 : 0 : && single_succ (then_bb) == single_succ (else_bb)
5182 : 0 : && single_pred_p (else_bb)
5183 : 0 : && !(single_succ_edge (else_bb)->flags & EDGE_COMPLEX)
5184 : 0 : && !(epilogue_completed
5185 : 0 : && tablejump_p (BB_END (else_bb), NULL, NULL)))
5186 : 0 : join_bb = single_succ (else_bb);
5187 : :
5188 : : /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination. */
5189 : : else
5190 : 0 : return false;
5191 : :
5192 : 0 : num_possible_if_blocks++;
5193 : :
5194 : 0 : if (dump_file)
5195 : : {
5196 : 0 : fprintf (dump_file,
5197 : : "\nIF-THEN%s block found, pass %d, start block %d "
5198 : : "[insn %d], then %d [%d]",
5199 : : (else_bb) ? "-ELSE" : "",
5200 : : ce_info->pass,
5201 : : test_bb->index,
5202 : 0 : BB_HEAD (test_bb) ? (int)INSN_UID (BB_HEAD (test_bb)) : -1,
5203 : : then_bb->index,
5204 : 0 : BB_HEAD (then_bb) ? (int)INSN_UID (BB_HEAD (then_bb)) : -1);
5205 : :
5206 : 0 : if (else_bb)
5207 : 0 : fprintf (dump_file, ", else %d [%d]",
5208 : : else_bb->index,
5209 : 0 : BB_HEAD (else_bb) ? (int)INSN_UID (BB_HEAD (else_bb)) : -1);
5210 : :
5211 : 0 : fprintf (dump_file, ", join %d [%d]",
5212 : : join_bb->index,
5213 : 0 : BB_HEAD (join_bb) ? (int)INSN_UID (BB_HEAD (join_bb)) : -1);
5214 : :
5215 : 0 : if (ce_info->num_multiple_test_blocks > 0)
5216 : 0 : fprintf (dump_file, ", %d %s block%s last test %d [%d]",
5217 : : ce_info->num_multiple_test_blocks,
5218 : 0 : (ce_info->and_and_p) ? "&&" : "||",
5219 : : (ce_info->num_multiple_test_blocks == 1) ? "" : "s",
5220 : : ce_info->last_test_bb->index,
5221 : 0 : ((BB_HEAD (ce_info->last_test_bb))
5222 : 0 : ? (int)INSN_UID (BB_HEAD (ce_info->last_test_bb))
5223 : : : -1));
5224 : :
5225 : 0 : fputc ('\n', dump_file);
5226 : : }
5227 : :
5228 : : /* Make sure IF, THEN, and ELSE, blocks are adjacent. Actually, we get the
5229 : : first condition for free, since we've already asserted that there's a
5230 : : fallthru edge from IF to THEN. Likewise for the && and || blocks, since
5231 : : we checked the FALLTHRU flag, those are already adjacent to the last IF
5232 : : block. */
5233 : : /* ??? As an enhancement, move the ELSE block. Have to deal with
5234 : : BLOCK notes, if by no other means than backing out the merge if they
5235 : : exist. Sticky enough I don't want to think about it now. */
5236 : 0 : next = then_bb;
5237 : 0 : if (else_bb && (next = next->next_bb) != else_bb)
5238 : : return false;
5239 : 0 : if ((next = next->next_bb) != join_bb
5240 : 0 : && join_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5241 : : {
5242 : 0 : if (else_bb)
5243 : : join_bb = NULL;
5244 : : else
5245 : : return false;
5246 : : }
5247 : :
5248 : : /* Do the real work. */
5249 : :
5250 : 0 : ce_info->else_bb = else_bb;
5251 : 0 : ce_info->join_bb = join_bb;
5252 : :
5253 : : /* If we have && and || tests, try to first handle combining the && and ||
5254 : : tests into the conditional code, and if that fails, go back and handle
5255 : : it without the && and ||, which at present handles the && case if there
5256 : : was no ELSE block. */
5257 : 0 : if (cond_exec_process_if_block (ce_info, true))
5258 : : return true;
5259 : :
5260 : 0 : if (ce_info->num_multiple_test_blocks)
5261 : : {
5262 : 0 : cancel_changes (0);
5263 : :
5264 : 0 : if (cond_exec_process_if_block (ce_info, false))
5265 : : return true;
5266 : : }
5267 : :
5268 : : return false;
5269 : : }
5270 : :
5271 : : /* Convert a branch over a trap, or a branch
5272 : : to a trap, into a conditional trap. */
5273 : :
5274 : : static bool
5275 : 0 : find_cond_trap (basic_block test_bb, edge then_edge, edge else_edge)
5276 : : {
5277 : 0 : basic_block then_bb = then_edge->dest;
5278 : 0 : basic_block else_bb = else_edge->dest;
5279 : 0 : basic_block other_bb, trap_bb;
5280 : 0 : rtx_insn *trap, *jump;
5281 : 0 : rtx cond;
5282 : 0 : rtx_insn *cond_earliest;
5283 : :
5284 : : /* Locate the block with the trap instruction. */
5285 : : /* ??? While we look for no successors, we really ought to allow
5286 : : EH successors. Need to fix merge_if_block for that to work. */
5287 : 0 : if ((trap = block_has_only_trap (then_bb)) != NULL)
5288 : : trap_bb = then_bb, other_bb = else_bb;
5289 : 0 : else if ((trap = block_has_only_trap (else_bb)) != NULL)
5290 : : trap_bb = else_bb, other_bb = then_bb;
5291 : : else
5292 : : return false;
5293 : :
5294 : 0 : if (dump_file)
5295 : : {
5296 : 0 : fprintf (dump_file, "\nTRAP-IF block found, start %d, trap %d\n",
5297 : : test_bb->index, trap_bb->index);
5298 : : }
5299 : :
5300 : : /* If this is not a standard conditional jump, we can't parse it. */
5301 : 0 : jump = BB_END (test_bb);
5302 : 0 : cond = noce_get_condition (jump, &cond_earliest, then_bb == trap_bb);
5303 : 0 : if (! cond)
5304 : : return false;
5305 : :
5306 : : /* If the conditional jump is more than just a conditional jump, then
5307 : : we cannot do if-conversion on this block. Give up for returnjump_p,
5308 : : changing a conditional return followed by unconditional trap for
5309 : : conditional trap followed by unconditional return is likely not
5310 : : beneficial and harder to handle. */
5311 : 0 : if (! onlyjump_p (jump) || returnjump_p (jump))
5312 : 0 : return false;
5313 : :
5314 : : /* We must be comparing objects whose modes imply the size. */
5315 : 0 : if (GET_MODE (XEXP (cond, 0)) == BLKmode)
5316 : : return false;
5317 : :
5318 : : /* Attempt to generate the conditional trap. */
5319 : 0 : rtx_insn *seq = gen_cond_trap (GET_CODE (cond), copy_rtx (XEXP (cond, 0)),
5320 : : copy_rtx (XEXP (cond, 1)),
5321 : 0 : TRAP_CODE (PATTERN (trap)));
5322 : 0 : if (seq == NULL)
5323 : : return false;
5324 : :
5325 : : /* If that results in an invalid insn, back out. */
5326 : 0 : for (rtx_insn *x = seq; x; x = NEXT_INSN (x))
5327 : 0 : if (reload_completed
5328 : 0 : ? !valid_insn_p (x)
5329 : 0 : : recog_memoized (x) < 0)
5330 : : return false;
5331 : :
5332 : : /* Emit the new insns before cond_earliest. */
5333 : 0 : emit_insn_before_setloc (seq, cond_earliest, INSN_LOCATION (trap));
5334 : :
5335 : : /* Delete the trap block if possible. */
5336 : 0 : remove_edge (trap_bb == then_bb ? then_edge : else_edge);
5337 : 0 : df_set_bb_dirty (test_bb);
5338 : 0 : df_set_bb_dirty (then_bb);
5339 : 0 : df_set_bb_dirty (else_bb);
5340 : :
5341 : 0 : if (EDGE_COUNT (trap_bb->preds) == 0)
5342 : : {
5343 : 0 : delete_basic_block (trap_bb);
5344 : 0 : num_true_changes++;
5345 : : }
5346 : :
5347 : : /* Wire together the blocks again. */
5348 : 0 : if (current_ir_type () == IR_RTL_CFGLAYOUT)
5349 : 0 : single_succ_edge (test_bb)->flags |= EDGE_FALLTHRU;
5350 : 0 : else if (trap_bb == then_bb)
5351 : : {
5352 : 0 : rtx lab = JUMP_LABEL (jump);
5353 : 0 : rtx_insn *seq = targetm.gen_jump (lab);
5354 : 0 : rtx_jump_insn *newjump = emit_jump_insn_after (seq, jump);
5355 : 0 : LABEL_NUSES (lab) += 1;
5356 : 0 : JUMP_LABEL (newjump) = lab;
5357 : 0 : emit_barrier_after (newjump);
5358 : : }
5359 : 0 : delete_insn (jump);
5360 : :
5361 : 0 : if (can_merge_blocks_p (test_bb, other_bb))
5362 : : {
5363 : 0 : merge_blocks (test_bb, other_bb);
5364 : 0 : num_true_changes++;
5365 : : }
5366 : :
5367 : 0 : num_updated_if_blocks++;
5368 : 0 : return true;
5369 : : }
5370 : :
5371 : : /* Subroutine of find_cond_trap: if BB contains only a trap insn,
5372 : : return it. */
5373 : :
5374 : : static rtx_insn *
5375 : 0 : block_has_only_trap (basic_block bb)
5376 : : {
5377 : 0 : rtx_insn *trap;
5378 : :
5379 : : /* We're not the exit block. */
5380 : 0 : if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5381 : : return NULL;
5382 : :
5383 : : /* The block must have no successors. */
5384 : 0 : if (EDGE_COUNT (bb->succs) > 0)
5385 : : return NULL;
5386 : :
5387 : : /* The only instruction in the THEN block must be the trap. */
5388 : 0 : trap = first_active_insn (bb);
5389 : 0 : if (! (trap == BB_END (bb)
5390 : 0 : && GET_CODE (PATTERN (trap)) == TRAP_IF
5391 : 0 : && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
5392 : 0 : return NULL;
5393 : :
5394 : : return trap;
5395 : : }
5396 : :
5397 : : /* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
5398 : : transformable, but not necessarily the other. There need be no
5399 : : JOIN block.
5400 : :
5401 : : Return TRUE if we were successful at converting the block.
5402 : :
5403 : : Cases we'd like to look at:
5404 : :
5405 : : (1)
5406 : : if (test) goto over; // x not live
5407 : : x = a;
5408 : : goto label;
5409 : : over:
5410 : :
5411 : : becomes
5412 : :
5413 : : x = a;
5414 : : if (! test) goto label;
5415 : :
5416 : : (2)
5417 : : if (test) goto E; // x not live
5418 : : x = big();
5419 : : goto L;
5420 : : E:
5421 : : x = b;
5422 : : goto M;
5423 : :
5424 : : becomes
5425 : :
5426 : : x = b;
5427 : : if (test) goto M;
5428 : : x = big();
5429 : : goto L;
5430 : :
5431 : : (3) // This one's really only interesting for targets that can do
5432 : : // multiway branching, e.g. IA-64 BBB bundles. For other targets
5433 : : // it results in multiple branches on a cache line, which often
5434 : : // does not sit well with predictors.
5435 : :
5436 : : if (test1) goto E; // predicted not taken
5437 : : x = a;
5438 : : if (test2) goto F;
5439 : : ...
5440 : : E:
5441 : : x = b;
5442 : : J:
5443 : :
5444 : : becomes
5445 : :
5446 : : x = a;
5447 : : if (test1) goto E;
5448 : : if (test2) goto F;
5449 : :
5450 : : Notes:
5451 : :
5452 : : (A) Don't do (2) if the branch is predicted against the block we're
5453 : : eliminating. Do it anyway if we can eliminate a branch; this requires
5454 : : that the sole successor of the eliminated block postdominate the other
5455 : : side of the if.
5456 : :
5457 : : (B) With CE, on (3) we can steal from both sides of the if, creating
5458 : :
5459 : : if (test1) x = a;
5460 : : if (!test1) x = b;
5461 : : if (test1) goto J;
5462 : : if (test2) goto F;
5463 : : ...
5464 : : J:
5465 : :
5466 : : Again, this is most useful if J postdominates.
5467 : :
5468 : : (C) CE substitutes for helpful life information.
5469 : :
5470 : : (D) These heuristics need a lot of work. */
5471 : :
5472 : : /* Tests for case 1 above. */
5473 : :
5474 : : static bool
5475 : 13400374 : find_if_case_1 (basic_block test_bb, edge then_edge, edge else_edge)
5476 : : {
5477 : 13400374 : basic_block then_bb = then_edge->dest;
5478 : 13400374 : basic_block else_bb = else_edge->dest;
5479 : 13400374 : basic_block new_bb;
5480 : 13400374 : int then_bb_index;
5481 : 13400374 : profile_probability then_prob;
5482 : 13400374 : rtx else_target = NULL_RTX;
5483 : :
5484 : : /* If we are partitioning hot/cold basic blocks, we don't want to
5485 : : mess up unconditional or indirect jumps that cross between hot
5486 : : and cold sections.
5487 : :
5488 : : Basic block partitioning may result in some jumps that appear to
5489 : : be optimizable (or blocks that appear to be mergeable), but which really
5490 : : must be left untouched (they are required to make it safely across
5491 : : partition boundaries). See the comments at the top of
5492 : : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
5493 : :
5494 : 13400374 : if ((BB_END (then_bb)
5495 : 13400374 : && JUMP_P (BB_END (then_bb))
5496 : 7056432 : && CROSSING_JUMP_P (BB_END (then_bb)))
5497 : 13172954 : || (JUMP_P (BB_END (test_bb))
5498 : 13172954 : && CROSSING_JUMP_P (BB_END (test_bb)))
5499 : 26473703 : || (BB_END (else_bb)
5500 : 13073329 : && JUMP_P (BB_END (else_bb))
5501 : 6492533 : && CROSSING_JUMP_P (BB_END (else_bb))))
5502 : : return false;
5503 : :
5504 : : /* Verify test_bb ends in a conditional jump with no other side-effects. */
5505 : 13055137 : if (!onlyjump_p (BB_END (test_bb)))
5506 : : return false;
5507 : :
5508 : : /* THEN has one successor. */
5509 : 19228796 : if (!single_succ_p (then_bb))
5510 : : return false;
5511 : :
5512 : : /* THEN does not fall through, but is not strange either. */
5513 : 5846279 : if (single_succ_edge (then_bb)->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
5514 : : return false;
5515 : :
5516 : : /* THEN has one predecessor. */
5517 : 14444226 : if (!single_pred_p (then_bb))
5518 : : return false;
5519 : :
5520 : : /* THEN must do something. */
5521 : 1061709 : if (forwarder_block_p (then_bb))
5522 : : return false;
5523 : :
5524 : 640017 : num_possible_if_blocks++;
5525 : 640017 : if (dump_file)
5526 : 4 : fprintf (dump_file,
5527 : : "\nIF-CASE-1 found, start %d, then %d\n",
5528 : : test_bb->index, then_bb->index);
5529 : :
5530 : 640017 : then_prob = then_edge->probability.invert ();
5531 : :
5532 : : /* We're speculating from the THEN path, we want to make sure the cost
5533 : : of speculation is within reason. */
5534 : 1212749 : if (! cheap_bb_rtx_cost_p (then_bb, then_prob,
5535 : 1212749 : COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (then_edge->src),
5536 : : predictable_edge_p (then_edge)))))
5537 : : return false;
5538 : :
5539 : 126117 : if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5540 : : {
5541 : 0 : rtx_insn *jump = BB_END (else_edge->src);
5542 : 0 : gcc_assert (JUMP_P (jump));
5543 : 0 : else_target = JUMP_LABEL (jump);
5544 : : }
5545 : :
5546 : : /* Registers set are dead, or are predicable. */
5547 : 126117 : if (! dead_or_predicable (test_bb, then_bb, else_bb,
5548 : : single_succ_edge (then_bb), true))
5549 : : return false;
5550 : :
5551 : : /* Conversion went ok, including moving the insns and fixing up the
5552 : : jump. Adjust the CFG to match. */
5553 : :
5554 : : /* We can avoid creating a new basic block if then_bb is immediately
5555 : : followed by else_bb, i.e. deleting then_bb allows test_bb to fall
5556 : : through to else_bb. */
5557 : :
5558 : 17857 : if (then_bb->next_bb == else_bb
5559 : 7410 : && then_bb->prev_bb == test_bb
5560 : 7410 : && else_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
5561 : : {
5562 : 7410 : redirect_edge_succ (FALLTHRU_EDGE (test_bb), else_bb);
5563 : 7410 : new_bb = 0;
5564 : : }
5565 : 10447 : else if (else_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
5566 : 0 : new_bb = force_nonfallthru_and_redirect (FALLTHRU_EDGE (test_bb),
5567 : : else_bb, else_target);
5568 : : else
5569 : 10447 : new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb),
5570 : : else_bb);
5571 : :
5572 : 17857 : df_set_bb_dirty (test_bb);
5573 : 17857 : df_set_bb_dirty (else_bb);
5574 : :
5575 : 17857 : then_bb_index = then_bb->index;
5576 : 17857 : delete_basic_block (then_bb);
5577 : :
5578 : : /* Make rest of code believe that the newly created block is the THEN_BB
5579 : : block we removed. */
5580 : 17857 : if (new_bb)
5581 : : {
5582 : 10447 : df_bb_replace (then_bb_index, new_bb);
5583 : : /* This should have been done above via force_nonfallthru_and_redirect
5584 : : (possibly called from redirect_edge_and_branch_force). */
5585 : 10447 : gcc_checking_assert (BB_PARTITION (new_bb) == BB_PARTITION (test_bb));
5586 : : }
5587 : :
5588 : 17857 : num_true_changes++;
5589 : 17857 : num_updated_if_blocks++;
5590 : 17857 : return true;
5591 : : }
5592 : :
5593 : : /* Test for case 2 above. */
5594 : :
5595 : : static bool
5596 : 13382517 : find_if_case_2 (basic_block test_bb, edge then_edge, edge else_edge)
5597 : : {
5598 : 13382517 : basic_block then_bb = then_edge->dest;
5599 : 13382517 : basic_block else_bb = else_edge->dest;
5600 : 13382517 : edge else_succ;
5601 : 13382517 : profile_probability then_prob, else_prob;
5602 : :
5603 : : /* We do not want to speculate (empty) loop latches. */
5604 : 13382517 : if (current_loops
5605 : 5289271 : && else_bb->loop_father->latch == else_bb)
5606 : : return false;
5607 : :
5608 : : /* If we are partitioning hot/cold basic blocks, we don't want to
5609 : : mess up unconditional or indirect jumps that cross between hot
5610 : : and cold sections.
5611 : :
5612 : : Basic block partitioning may result in some jumps that appear to
5613 : : be optimizable (or blocks that appear to be mergeable), but which really
5614 : : must be left untouched (they are required to make it safely across
5615 : : partition boundaries). See the comments at the top of
5616 : : bb-reorder.cc:partition_hot_cold_basic_blocks for complete details. */
5617 : :
5618 : 13229510 : if ((BB_END (then_bb)
5619 : 13229510 : && JUMP_P (BB_END (then_bb))
5620 : 6963870 : && CROSSING_JUMP_P (BB_END (then_bb)))
5621 : 13002090 : || (JUMP_P (BB_END (test_bb))
5622 : 13002090 : && CROSSING_JUMP_P (BB_END (test_bb)))
5623 : 26131975 : || (BB_END (else_bb)
5624 : 12902465 : && JUMP_P (BB_END (else_bb))
5625 : 6350611 : && CROSSING_JUMP_P (BB_END (else_bb))))
5626 : : return false;
5627 : :
5628 : : /* Verify test_bb ends in a conditional jump with no other side-effects. */
5629 : 12884273 : if (!onlyjump_p (BB_END (test_bb)))
5630 : : return false;
5631 : :
5632 : : /* ELSE has one successor. */
5633 : 18720684 : if (!single_succ_p (else_bb))
5634 : : return false;
5635 : : else
5636 : 5523321 : else_succ = single_succ_edge (else_bb);
5637 : :
5638 : : /* ELSE outgoing edge is not complex. */
5639 : 5523321 : if (else_succ->flags & EDGE_COMPLEX)
5640 : : return false;
5641 : :
5642 : : /* ELSE has one predecessor. */
5643 : 16137050 : if (!single_pred_p (else_bb))
5644 : : return false;
5645 : :
5646 : : /* THEN is not EXIT. */
5647 : 2939687 : if (then_bb->index < NUM_FIXED_BLOCKS)
5648 : : return false;
5649 : :
5650 : 2939687 : else_prob = else_edge->probability;
5651 : 2939687 : then_prob = else_prob.invert ();
5652 : :
5653 : : /* ELSE is predicted or SUCC(ELSE) postdominates THEN. */
5654 : 2939687 : if (else_prob > then_prob)
5655 : : ;
5656 : 1980736 : else if (else_succ->dest->index < NUM_FIXED_BLOCKS
5657 : 1980736 : || dominated_by_p (CDI_POST_DOMINATORS, then_bb,
5658 : : else_succ->dest))
5659 : : ;
5660 : : else
5661 : : return false;
5662 : :
5663 : 2254285 : num_possible_if_blocks++;
5664 : 2254285 : if (dump_file)
5665 : 42 : fprintf (dump_file,
5666 : : "\nIF-CASE-2 found, start %d, else %d\n",
5667 : : test_bb->index, else_bb->index);
5668 : :
5669 : : /* We're speculating from the ELSE path, we want to make sure the cost
5670 : : of speculation is within reason. */
5671 : 4308505 : if (! cheap_bb_rtx_cost_p (else_bb, else_prob,
5672 : 4308505 : COSTS_N_INSNS (BRANCH_COST (optimize_bb_for_speed_p (else_edge->src),
5673 : : predictable_edge_p (else_edge)))))
5674 : : return false;
5675 : :
5676 : : /* Registers set are dead, or are predicable. */
5677 : 472111 : if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ, false))
5678 : : return false;
5679 : :
5680 : : /* Conversion went ok, including moving the insns and fixing up the
5681 : : jump. Adjust the CFG to match. */
5682 : :
5683 : 185154 : df_set_bb_dirty (test_bb);
5684 : 185154 : df_set_bb_dirty (then_bb);
5685 : 185154 : delete_basic_block (else_bb);
5686 : :
5687 : 185154 : num_true_changes++;
5688 : 185154 : num_updated_if_blocks++;
5689 : :
5690 : : /* ??? We may now fallthru from one of THEN's successors into a join
5691 : : block. Rerun cleanup_cfg? Examine things manually? Wait? */
5692 : :
5693 : 185154 : return true;
5694 : : }
5695 : :
5696 : : /* Used by the code above to perform the actual rtl transformations.
5697 : : Return TRUE if successful.
5698 : :
5699 : : TEST_BB is the block containing the conditional branch. MERGE_BB
5700 : : is the block containing the code to manipulate. DEST_EDGE is an
5701 : : edge representing a jump to the join block; after the conversion,
5702 : : TEST_BB should be branching to its destination.
5703 : : REVERSEP is true if the sense of the branch should be reversed. */
5704 : :
5705 : : static bool
5706 : 598228 : dead_or_predicable (basic_block test_bb, basic_block merge_bb,
5707 : : basic_block other_bb, edge dest_edge, bool reversep)
5708 : : {
5709 : 598228 : basic_block new_dest = dest_edge->dest;
5710 : 598228 : rtx_insn *head, *end, *jump;
5711 : 598228 : rtx_insn *earliest = NULL;
5712 : 598228 : rtx old_dest;
5713 : 598228 : bitmap merge_set = NULL;
5714 : : /* Number of pending changes. */
5715 : 598228 : int n_validated_changes = 0;
5716 : 598228 : rtx new_dest_label = NULL_RTX;
5717 : :
5718 : 598228 : jump = BB_END (test_bb);
5719 : :
5720 : : /* Find the extent of the real code in the merge block. */
5721 : 598228 : head = BB_HEAD (merge_bb);
5722 : 598228 : end = BB_END (merge_bb);
5723 : :
5724 : 711698 : while (DEBUG_INSN_P (end) && end != head)
5725 : 113470 : end = PREV_INSN (end);
5726 : :
5727 : : /* If merge_bb ends with a tablejump, predicating/moving insn's
5728 : : into test_bb and then deleting merge_bb will result in the jumptable
5729 : : that follows merge_bb being removed along with merge_bb and then we
5730 : : get an unresolved reference to the jumptable. */
5731 : 598228 : if (tablejump_p (end, NULL, NULL))
5732 : : return false;
5733 : :
5734 : 598228 : if (LABEL_P (head))
5735 : 472145 : head = NEXT_INSN (head);
5736 : 598228 : while (DEBUG_INSN_P (head) && head != end)
5737 : 0 : head = NEXT_INSN (head);
5738 : 598228 : if (NOTE_P (head))
5739 : : {
5740 : 598228 : if (head == end)
5741 : : {
5742 : 98172 : head = end = NULL;
5743 : 98172 : goto no_body;
5744 : : }
5745 : 500056 : head = NEXT_INSN (head);
5746 : 1264467 : while (DEBUG_INSN_P (head) && head != end)
5747 : 264355 : head = NEXT_INSN (head);
5748 : : }
5749 : :
5750 : 500056 : if (JUMP_P (end))
5751 : : {
5752 : 178839 : if (!onlyjump_p (end))
5753 : : return false;
5754 : 138407 : if (head == end)
5755 : : {
5756 : 2 : head = end = NULL;
5757 : 2 : goto no_body;
5758 : : }
5759 : 138405 : end = PREV_INSN (end);
5760 : 338314 : while (DEBUG_INSN_P (end) && end != head)
5761 : 61504 : end = PREV_INSN (end);
5762 : : }
5763 : :
5764 : : /* Don't move frame-related insn across the conditional branch. This
5765 : : can lead to one of the paths of the branch having wrong unwind info. */
5766 : 459622 : if (epilogue_completed)
5767 : : {
5768 : : rtx_insn *insn = head;
5769 : 53905 : while (1)
5770 : : {
5771 : 235072 : if (INSN_P (insn) && RTX_FRAME_RELATED_P (insn))
5772 : : return false;
5773 : 234432 : if (insn == end)
5774 : : break;
5775 : 53905 : insn = NEXT_INSN (insn);
5776 : 53905 : }
5777 : : }
5778 : :
5779 : : /* Disable handling dead code by conditional execution if the machine needs
5780 : : to do anything funny with the tests, etc. */
5781 : : #ifndef IFCVT_MODIFY_TESTS
5782 : 458982 : if (targetm.have_conditional_execution ())
5783 : : {
5784 : : /* In the conditional execution case, we have things easy. We know
5785 : : the condition is reversible. We don't have to check life info
5786 : : because we're going to conditionally execute the code anyway.
5787 : : All that's left is making sure the insns involved can actually
5788 : : be predicated. */
5789 : :
5790 : 0 : rtx cond;
5791 : :
5792 : : /* If the conditional jump is more than just a conditional jump,
5793 : : then we cannot do conditional execution conversion on this block. */
5794 : 0 : if (!onlyjump_p (jump))
5795 : 0 : goto nce;
5796 : :
5797 : 0 : cond = cond_exec_get_condition (jump);
5798 : 0 : if (! cond)
5799 : 0 : goto nce;
5800 : :
5801 : 0 : rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
5802 : 0 : profile_probability prob_val
5803 : 0 : = (note ? profile_probability::from_reg_br_prob_note (XINT (note, 0))
5804 : 0 : : profile_probability::uninitialized ());
5805 : :
5806 : 0 : if (reversep)
5807 : : {
5808 : 0 : enum rtx_code rev = reversed_comparison_code (cond, jump);
5809 : 0 : if (rev == UNKNOWN)
5810 : 0 : return false;
5811 : 0 : cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
5812 : : XEXP (cond, 1));
5813 : 0 : prob_val = prob_val.invert ();
5814 : : }
5815 : :
5816 : 0 : if (cond_exec_process_insns (NULL, head, end, cond, prob_val, false)
5817 : 0 : && verify_changes (0))
5818 : 0 : n_validated_changes = num_validated_changes ();
5819 : : else
5820 : 0 : cancel_changes (0);
5821 : :
5822 : 0 : earliest = jump;
5823 : : }
5824 : 458982 : nce:
5825 : : #endif
5826 : :
5827 : : /* If we allocated new pseudos (e.g. in the conditional move
5828 : : expander called from noce_emit_cmove), we must resize the
5829 : : array first. */
5830 : 458982 : if (max_regno < max_reg_num ())
5831 : 86098 : max_regno = max_reg_num ();
5832 : :
5833 : : /* Try the NCE path if the CE path did not result in any changes. */
5834 : 458982 : if (n_validated_changes == 0)
5835 : : {
5836 : 458982 : rtx cond;
5837 : 458982 : rtx_insn *insn;
5838 : 458982 : regset live;
5839 : 458982 : bool success;
5840 : :
5841 : : /* In the non-conditional execution case, we have to verify that there
5842 : : are no trapping operations, no calls, no references to memory, and
5843 : : that any registers modified are dead at the branch site. */
5844 : :
5845 : 458982 : if (!any_condjump_p (jump))
5846 : : return false;
5847 : :
5848 : : /* Find the extent of the conditional. */
5849 : 458982 : cond = noce_get_condition (jump, &earliest, false);
5850 : 458982 : if (!cond)
5851 : : return false;
5852 : :
5853 : 458982 : live = BITMAP_ALLOC (®_obstack);
5854 : 458982 : simulate_backwards_to_point (merge_bb, live, end);
5855 : 458982 : success = can_move_insns_across (head, end, earliest, jump,
5856 : : merge_bb, live,
5857 : : df_get_live_in (other_bb), NULL);
5858 : 458982 : BITMAP_FREE (live);
5859 : 458982 : if (!success)
5860 : : return false;
5861 : :
5862 : : /* Collect the set of registers set in MERGE_BB. */
5863 : 200338 : merge_set = BITMAP_ALLOC (®_obstack);
5864 : :
5865 : 936534 : FOR_BB_INSNS (merge_bb, insn)
5866 : 736196 : if (NONDEBUG_INSN_P (insn))
5867 : 249310 : df_simulate_find_defs (insn, merge_set);
5868 : :
5869 : : /* If shrink-wrapping, disable this optimization when test_bb is
5870 : : the first basic block and merge_bb exits. The idea is to not
5871 : : move code setting up a return register as that may clobber a
5872 : : register used to pass function parameters, which then must be
5873 : : saved in caller-saved regs. A caller-saved reg requires the
5874 : : prologue, killing a shrink-wrap opportunity. */
5875 : 200338 : if ((SHRINK_WRAPPING_ENABLED && !epilogue_completed)
5876 : 171434 : && ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == test_bb
5877 : 28684 : && single_succ_p (new_dest)
5878 : 22488 : && single_succ (new_dest) == EXIT_BLOCK_PTR_FOR_FN (cfun)
5879 : 221985 : && bitmap_intersect_p (df_get_live_in (new_dest), merge_set))
5880 : : {
5881 : 21647 : regset return_regs;
5882 : 21647 : unsigned int i;
5883 : :
5884 : 21647 : return_regs = BITMAP_ALLOC (®_obstack);
5885 : :
5886 : : /* Start off with the intersection of regs used to pass
5887 : : params and regs used to return values. */
5888 : 2034818 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5889 : 1991524 : if (FUNCTION_ARG_REGNO_P (i)
5890 : 1991524 : && targetm.calls.function_value_regno_p (i))
5891 : 117357 : bitmap_set_bit (return_regs, INCOMING_REGNO (i));
5892 : :
5893 : 21647 : bitmap_and_into (return_regs,
5894 : 21647 : df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5895 : 21647 : bitmap_and_into (return_regs,
5896 : 21647 : df_get_live_in (EXIT_BLOCK_PTR_FOR_FN (cfun)));
5897 : 21647 : if (!bitmap_empty_p (return_regs))
5898 : : {
5899 : 6230 : FOR_BB_INSNS_REVERSE (new_dest, insn)
5900 : 5509 : if (NONDEBUG_INSN_P (insn))
5901 : : {
5902 : 2739 : df_ref def;
5903 : :
5904 : : /* If this insn sets any reg in return_regs, add all
5905 : : reg uses to the set of regs we're interested in. */
5906 : 3172 : FOR_EACH_INSN_DEF (def, insn)
5907 : 1822 : if (bitmap_bit_p (return_regs, DF_REF_REGNO (def)))
5908 : : {
5909 : 1389 : df_simulate_uses (insn, return_regs);
5910 : 1389 : break;
5911 : : }
5912 : : }
5913 : 721 : if (bitmap_intersect_p (merge_set, return_regs))
5914 : : {
5915 : 687 : BITMAP_FREE (return_regs);
5916 : 687 : BITMAP_FREE (merge_set);
5917 : 687 : return false;
5918 : : }
5919 : : }
5920 : 20960 : BITMAP_FREE (return_regs);
5921 : : }
5922 : : }
5923 : :
5924 : 297825 : no_body:
5925 : : /* We don't want to use normal invert_jump or redirect_jump because
5926 : : we don't want to delete_insn called. Also, we want to do our own
5927 : : change group management. */
5928 : :
5929 : 297825 : old_dest = JUMP_LABEL (jump);
5930 : 297825 : if (other_bb != new_dest)
5931 : : {
5932 : 297795 : if (!any_condjump_p (jump))
5933 : 0 : goto cancel;
5934 : :
5935 : 297795 : if (JUMP_P (BB_END (dest_edge->src)))
5936 : 18814 : new_dest_label = JUMP_LABEL (BB_END (dest_edge->src));
5937 : 278981 : else if (new_dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
5938 : 94814 : new_dest_label = ret_rtx;
5939 : : else
5940 : 184167 : new_dest_label = block_label (new_dest);
5941 : :
5942 : 297795 : rtx_jump_insn *jump_insn = as_a <rtx_jump_insn *> (jump);
5943 : 297795 : if (reversep
5944 : 577733 : ? ! invert_jump_1 (jump_insn, new_dest_label)
5945 : 279938 : : ! redirect_jump_1 (jump_insn, new_dest_label))
5946 : 0 : goto cancel;
5947 : : }
5948 : :
5949 : 297825 : if (verify_changes (n_validated_changes))
5950 : 203011 : confirm_change_group ();
5951 : : else
5952 : 94814 : goto cancel;
5953 : :
5954 : 203011 : if (other_bb != new_dest)
5955 : : {
5956 : 202981 : redirect_jump_2 (as_a <rtx_jump_insn *> (jump), old_dest, new_dest_label,
5957 : : 0, reversep);
5958 : :
5959 : 202981 : redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
5960 : 202981 : if (reversep)
5961 : : {
5962 : 17857 : std::swap (BRANCH_EDGE (test_bb)->probability,
5963 : 17857 : FALLTHRU_EDGE (test_bb)->probability);
5964 : 17857 : update_br_prob_note (test_bb);
5965 : : }
5966 : : }
5967 : :
5968 : : /* Move the insns out of MERGE_BB to before the branch. */
5969 : 203011 : if (head != NULL)
5970 : : {
5971 : 199651 : rtx_insn *insn;
5972 : :
5973 : 199651 : if (end == BB_END (merge_bb))
5974 : 163133 : BB_END (merge_bb) = PREV_INSN (head);
5975 : :
5976 : : /* PR 21767: when moving insns above a conditional branch, the REG_EQUAL
5977 : : notes being moved might become invalid. */
5978 : : insn = head;
5979 : 236674 : do
5980 : : {
5981 : 236674 : rtx note;
5982 : :
5983 : 236674 : if (! INSN_P (insn))
5984 : 2851 : continue;
5985 : 233823 : note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5986 : 233823 : if (! note)
5987 : 223637 : continue;
5988 : 10186 : remove_note (insn, note);
5989 : 273697 : } while (insn != end && (insn = NEXT_INSN (insn)));
5990 : :
5991 : : /* PR46315: when moving insns above a conditional branch, the REG_EQUAL
5992 : : notes referring to the registers being set might become invalid. */
5993 : 199651 : if (merge_set)
5994 : : {
5995 : 199651 : unsigned i;
5996 : 199651 : bitmap_iterator bi;
5997 : :
5998 : 446710 : EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
5999 : 247059 : remove_reg_equal_equiv_notes_for_regno (i);
6000 : :
6001 : 199651 : BITMAP_FREE (merge_set);
6002 : : }
6003 : :
6004 : 199651 : reorder_insns (head, end, PREV_INSN (earliest));
6005 : : }
6006 : :
6007 : : /* Remove the jump and edge if we can. */
6008 : 203011 : if (other_bb == new_dest)
6009 : : {
6010 : 30 : delete_insn (jump);
6011 : 30 : remove_edge (BRANCH_EDGE (test_bb));
6012 : : /* ??? Can't merge blocks here, as then_bb is still in use.
6013 : : At minimum, the merge will get done just before bb-reorder. */
6014 : : }
6015 : :
6016 : : return true;
6017 : :
6018 : 94814 : cancel:
6019 : 94814 : cancel_changes (0);
6020 : :
6021 : 94814 : if (merge_set)
6022 : 0 : BITMAP_FREE (merge_set);
6023 : :
6024 : : return false;
6025 : : }
6026 : :
6027 : : /* Main entry point for all if-conversion. AFTER_COMBINE is true if
6028 : : we are after combine pass. */
6029 : :
6030 : : static void
6031 : 3017697 : if_convert (bool after_combine)
6032 : : {
6033 : 3017697 : basic_block bb;
6034 : 3017697 : int pass;
6035 : :
6036 : 3017697 : if (optimize == 1)
6037 : : {
6038 : 218632 : df_live_add_problem ();
6039 : 218632 : df_live_set_all_dirty ();
6040 : : }
6041 : :
6042 : : /* Record whether we are after combine pass. */
6043 : 3017697 : ifcvt_after_combine = after_combine;
6044 : 3017697 : have_cbranchcc4 = (direct_optab_handler (cbranch_optab, CCmode)
6045 : 3017697 : != CODE_FOR_nothing);
6046 : 3017697 : num_possible_if_blocks = 0;
6047 : 3017697 : num_updated_if_blocks = 0;
6048 : 3017697 : num_true_changes = 0;
6049 : :
6050 : 3017697 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
6051 : 3017697 : mark_loop_exit_edges ();
6052 : 3017697 : loop_optimizer_finalize ();
6053 : 3017697 : free_dominance_info (CDI_DOMINATORS);
6054 : :
6055 : : /* Compute postdominators. */
6056 : 3017697 : calculate_dominance_info (CDI_POST_DOMINATORS);
6057 : :
6058 : 3017697 : df_set_flags (DF_LR_RUN_DCE);
6059 : :
6060 : : /* Go through each of the basic blocks looking for things to convert. If we
6061 : : have conditional execution, we make multiple passes to allow us to handle
6062 : : IF-THEN{-ELSE} blocks within other IF-THEN{-ELSE} blocks. */
6063 : 3017697 : pass = 0;
6064 : 3199377 : do
6065 : : {
6066 : 3199377 : df_analyze ();
6067 : : /* Only need to do dce on the first pass. */
6068 : 3199377 : df_clear_flags (DF_LR_RUN_DCE);
6069 : 3199377 : cond_exec_changed_p = false;
6070 : 3199377 : pass++;
6071 : :
6072 : : #ifdef IFCVT_MULTIPLE_DUMPS
6073 : 3199377 : if (dump_file && pass > 1)
6074 : 17 : fprintf (dump_file, "\n\n========== Pass %d ==========\n", pass);
6075 : : #endif
6076 : :
6077 : 40915309 : FOR_EACH_BB_FN (bb, cfun)
6078 : : {
6079 : : basic_block new_bb;
6080 : 38047506 : while (!df_get_bb_dirty (bb)
6081 : 75214005 : && (new_bb = find_if_header (bb, pass)) != NULL)
6082 : : bb = new_bb;
6083 : : }
6084 : :
6085 : : #ifdef IFCVT_MULTIPLE_DUMPS
6086 : 3199377 : if (dump_file && cond_exec_changed_p)
6087 : 17 : print_rtl_with_bb (dump_file, get_insns (), dump_flags);
6088 : : #endif
6089 : : }
6090 : : while (cond_exec_changed_p);
6091 : :
6092 : : #ifdef IFCVT_MULTIPLE_DUMPS
6093 : 3017697 : if (dump_file)
6094 : 85 : fprintf (dump_file, "\n\n========== no more changes\n");
6095 : : #endif
6096 : :
6097 : 3017697 : free_dominance_info (CDI_POST_DOMINATORS);
6098 : :
6099 : 3017697 : if (dump_file)
6100 : 85 : fflush (dump_file);
6101 : :
6102 : 3017697 : clear_aux_for_blocks ();
6103 : :
6104 : : /* If we allocated new pseudos, we must resize the array for sched1. */
6105 : 3017697 : if (max_regno < max_reg_num ())
6106 : 616366 : max_regno = max_reg_num ();
6107 : :
6108 : : /* Write the final stats. */
6109 : 3017697 : if (dump_file && num_possible_if_blocks > 0)
6110 : : {
6111 : 59 : fprintf (dump_file,
6112 : : "\n%d possible IF blocks searched.\n",
6113 : : num_possible_if_blocks);
6114 : 59 : fprintf (dump_file,
6115 : : "%d IF blocks converted.\n",
6116 : : num_updated_if_blocks);
6117 : 59 : fprintf (dump_file,
6118 : : "%d true changes made.\n\n\n",
6119 : : num_true_changes);
6120 : : }
6121 : :
6122 : 3017697 : if (optimize == 1)
6123 : 218632 : df_remove_problem (df_live);
6124 : :
6125 : : /* Some non-cold blocks may now be only reachable from cold blocks.
6126 : : Fix that up. */
6127 : 3017697 : fixup_partitions ();
6128 : :
6129 : 3017697 : checking_verify_flow_info ();
6130 : 3017697 : }
6131 : :
6132 : : /* If-conversion and CFG cleanup. */
6133 : : static void
6134 : 1008570 : rest_of_handle_if_conversion (void)
6135 : : {
6136 : 1008570 : int flags = 0;
6137 : :
6138 : 1008570 : if (flag_if_conversion)
6139 : : {
6140 : 1005897 : if (dump_file)
6141 : : {
6142 : 33 : dump_reg_info (dump_file);
6143 : 33 : dump_flow_info (dump_file, dump_flags);
6144 : : }
6145 : 1005897 : cleanup_cfg (CLEANUP_EXPENSIVE);
6146 : 1005897 : if_convert (false);
6147 : 1005897 : if (num_updated_if_blocks)
6148 : : /* Get rid of any dead CC-related instructions. */
6149 : 1008570 : flags |= CLEANUP_FORCE_FAST_DCE;
6150 : : }
6151 : :
6152 : 1008570 : cleanup_cfg (flags);
6153 : 1008570 : }
6154 : :
6155 : : namespace {
6156 : :
6157 : : const pass_data pass_data_rtl_ifcvt =
6158 : : {
6159 : : RTL_PASS, /* type */
6160 : : "ce1", /* name */
6161 : : OPTGROUP_NONE, /* optinfo_flags */
6162 : : TV_IFCVT, /* tv_id */
6163 : : 0, /* properties_required */
6164 : : 0, /* properties_provided */
6165 : : 0, /* properties_destroyed */
6166 : : 0, /* todo_flags_start */
6167 : : TODO_df_finish, /* todo_flags_finish */
6168 : : };
6169 : :
6170 : : class pass_rtl_ifcvt : public rtl_opt_pass
6171 : : {
6172 : : public:
6173 : 283157 : pass_rtl_ifcvt (gcc::context *ctxt)
6174 : 566314 : : rtl_opt_pass (pass_data_rtl_ifcvt, ctxt)
6175 : : {}
6176 : :
6177 : : /* opt_pass methods: */
6178 : 1435849 : bool gate (function *) final override
6179 : : {
6180 : 1435849 : return (optimize > 0) && dbg_cnt (if_conversion);
6181 : : }
6182 : :
6183 : 1008570 : unsigned int execute (function *) final override
6184 : : {
6185 : 1008570 : rest_of_handle_if_conversion ();
6186 : 1008570 : return 0;
6187 : : }
6188 : :
6189 : : }; // class pass_rtl_ifcvt
6190 : :
6191 : : } // anon namespace
6192 : :
6193 : : rtl_opt_pass *
6194 : 283157 : make_pass_rtl_ifcvt (gcc::context *ctxt)
6195 : : {
6196 : 283157 : return new pass_rtl_ifcvt (ctxt);
6197 : : }
6198 : :
6199 : :
6200 : : /* Rerun if-conversion, as combine may have simplified things enough
6201 : : to now meet sequence length restrictions. */
6202 : :
6203 : : namespace {
6204 : :
6205 : : const pass_data pass_data_if_after_combine =
6206 : : {
6207 : : RTL_PASS, /* type */
6208 : : "ce2", /* name */
6209 : : OPTGROUP_NONE, /* optinfo_flags */
6210 : : TV_IFCVT, /* tv_id */
6211 : : 0, /* properties_required */
6212 : : 0, /* properties_provided */
6213 : : 0, /* properties_destroyed */
6214 : : 0, /* todo_flags_start */
6215 : : TODO_df_finish, /* todo_flags_finish */
6216 : : };
6217 : :
6218 : : class pass_if_after_combine : public rtl_opt_pass
6219 : : {
6220 : : public:
6221 : 283157 : pass_if_after_combine (gcc::context *ctxt)
6222 : 566314 : : rtl_opt_pass (pass_data_if_after_combine, ctxt)
6223 : : {}
6224 : :
6225 : : /* opt_pass methods: */
6226 : 1435849 : bool gate (function *) final override
6227 : : {
6228 : 1008571 : return optimize > 0 && flag_if_conversion
6229 : 2441747 : && dbg_cnt (if_after_combine);
6230 : : }
6231 : :
6232 : 1005897 : unsigned int execute (function *) final override
6233 : : {
6234 : 1005897 : if_convert (true);
6235 : 1005897 : return 0;
6236 : : }
6237 : :
6238 : : }; // class pass_if_after_combine
6239 : :
6240 : : } // anon namespace
6241 : :
6242 : : rtl_opt_pass *
6243 : 283157 : make_pass_if_after_combine (gcc::context *ctxt)
6244 : : {
6245 : 283157 : return new pass_if_after_combine (ctxt);
6246 : : }
6247 : :
6248 : :
6249 : : namespace {
6250 : :
6251 : : const pass_data pass_data_if_after_reload =
6252 : : {
6253 : : RTL_PASS, /* type */
6254 : : "ce3", /* name */
6255 : : OPTGROUP_NONE, /* optinfo_flags */
6256 : : TV_IFCVT2, /* tv_id */
6257 : : 0, /* properties_required */
6258 : : 0, /* properties_provided */
6259 : : 0, /* properties_destroyed */
6260 : : 0, /* todo_flags_start */
6261 : : TODO_df_finish, /* todo_flags_finish */
6262 : : };
6263 : :
6264 : : class pass_if_after_reload : public rtl_opt_pass
6265 : : {
6266 : : public:
6267 : 283157 : pass_if_after_reload (gcc::context *ctxt)
6268 : 566314 : : rtl_opt_pass (pass_data_if_after_reload, ctxt)
6269 : : {}
6270 : :
6271 : : /* opt_pass methods: */
6272 : 1435849 : bool gate (function *) final override
6273 : : {
6274 : 1008571 : return optimize > 0 && flag_if_conversion2
6275 : 2441753 : && dbg_cnt (if_after_reload);
6276 : : }
6277 : :
6278 : 1005903 : unsigned int execute (function *) final override
6279 : : {
6280 : 1005903 : if_convert (true);
6281 : 1005903 : return 0;
6282 : : }
6283 : :
6284 : : }; // class pass_if_after_reload
6285 : :
6286 : : } // anon namespace
6287 : :
6288 : : rtl_opt_pass *
6289 : 283157 : make_pass_if_after_reload (gcc::context *ctxt)
6290 : : {
6291 : 283157 : return new pass_if_after_reload (ctxt);
6292 : : }
|