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