]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cfgcleanup.c
Add a function for getting the ABI of a call insn target
[thirdparty/gcc.git] / gcc / cfgcleanup.c
CommitLineData
402209ff 1/* Control flow optimization code for GNU compiler.
a5544970 2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
402209ff
JH
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
402209ff
JH
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
402209ff 19
1ea7e6ad 20/* This file contains optimizer of the control flow. The main entry point is
402209ff
JH
21 cleanup_cfg. Following optimizations are performed:
22
23 - Unreachable blocks removal
d1a6adeb 24 - Edge forwarding (edge to the forwarder block is forwarded to its
eaec9b3d 25 successor. Simplification of the branch instruction is performed by
402209ff 26 underlying infrastructure so branch can be converted to simplejump or
f5143c46 27 eliminated).
402209ff
JH
28 - Cross jumping (tail merging)
29 - Conditional jump-around-simplejump simplification
30 - Basic block merging. */
31
32#include "config.h"
33#include "system.h"
4977bab6 34#include "coretypes.h"
c7131fb2 35#include "backend.h"
957060b5 36#include "target.h"
402209ff 37#include "rtl.h"
957060b5
AM
38#include "tree.h"
39#include "cfghooks.h"
c7131fb2 40#include "df.h"
4d0cdd0c 41#include "memmodel.h"
957060b5 42#include "tm_p.h"
402209ff 43#include "insn-config.h"
957060b5 44#include "emit-rtl.h"
8ecba28a 45#include "cselib.h"
5f24e0dc 46#include "params.h"
ef330312
PB
47#include "tree-pass.h"
48#include "cfgloop.h"
60393bbc
AM
49#include "cfgrtl.h"
50#include "cfganal.h"
51#include "cfgbuild.h"
52#include "cfgcleanup.h"
c1e3e2d9 53#include "dce.h"
7d817ebc 54#include "dbgcnt.h"
a2250fe9 55#include "rtl-iter.h"
4b1a1d98 56#include "regs.h"
5a5a3bc5 57#include "function-abi.h"
402209ff 58
2dd2d53e 59#define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
c22cacf3 60
7cf240d5
JH
61/* Set to true when we are running first pass of try_optimize_cfg loop. */
62static bool first_pass;
c1e3e2d9 63
073a8998 64/* Set to true if crossjumps occurred in the latest run of try_optimize_cfg. */
bd2c6270 65static bool crossjumps_occurred;
c1e3e2d9 66
4ec5d4f5
BS
67/* Set to true if we couldn't run an optimization due to stale liveness
68 information; we should run df_analyze to enable more opportunities. */
69static bool block_was_dirty;
70
bf22920b 71static bool try_crossjump_to_edge (int, edge, edge, enum replace_direction);
d329e058 72static bool try_crossjump_bb (int, basic_block);
c2fc5456 73static bool outgoing_edges_match (int, basic_block, basic_block);
da5477a9 74static enum replace_direction old_insns_match_p (int, rtx_insn *, rtx_insn *);
d329e058 75
d329e058
AJ
76static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
77static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
d329e058
AJ
78static bool try_optimize_cfg (int);
79static bool try_simplify_condjump (basic_block);
80static bool try_forward_edges (int, basic_block);
6fb5fa3c 81static edge thread_jump (edge, basic_block);
d329e058
AJ
82static bool mark_effect (rtx, bitmap);
83static void notice_new_block (basic_block);
84static void update_forwarder_flag (basic_block);
c2fc5456 85static void merge_memattrs (rtx, rtx);
635559ab
JH
86\f
87/* Set flags for newly created block. */
88
89static void
d329e058 90notice_new_block (basic_block bb)
635559ab
JH
91{
92 if (!bb)
93 return;
5f0d2358 94
635559ab 95 if (forwarder_block_p (bb))
2dd2d53e 96 bb->flags |= BB_FORWARDER_BLOCK;
635559ab
JH
97}
98
99/* Recompute forwarder flag after block has been modified. */
100
101static void
d329e058 102update_forwarder_flag (basic_block bb)
635559ab
JH
103{
104 if (forwarder_block_p (bb))
2dd2d53e 105 bb->flags |= BB_FORWARDER_BLOCK;
635559ab 106 else
2dd2d53e 107 bb->flags &= ~BB_FORWARDER_BLOCK;
635559ab 108}
402209ff
JH
109\f
110/* Simplify a conditional jump around an unconditional jump.
111 Return true if something changed. */
112
113static bool
d329e058 114try_simplify_condjump (basic_block cbranch_block)
402209ff
JH
115{
116 basic_block jump_block, jump_dest_block, cbranch_dest_block;
117 edge cbranch_jump_edge, cbranch_fallthru_edge;
da5477a9 118 rtx_insn *cbranch_insn;
402209ff
JH
119
120 /* Verify that there are exactly two successors. */
628f6a4e 121 if (EDGE_COUNT (cbranch_block->succs) != 2)
402209ff
JH
122 return false;
123
124 /* Verify that we've got a normal conditional branch at the end
125 of the block. */
a813c111 126 cbranch_insn = BB_END (cbranch_block);
402209ff
JH
127 if (!any_condjump_p (cbranch_insn))
128 return false;
129
130 cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
131 cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
132
133 /* The next block must not have multiple predecessors, must not
134 be the last block in the function, and must contain just the
135 unconditional jump. */
136 jump_block = cbranch_fallthru_edge->dest;
c5cbcccf 137 if (!single_pred_p (jump_block)
fefa31b5 138 || jump_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
635559ab 139 || !FORWARDER_BLOCK_P (jump_block))
402209ff 140 return false;
c5cbcccf 141 jump_dest_block = single_succ (jump_block);
402209ff 142
750054a2
CT
143 /* If we are partitioning hot/cold basic blocks, we don't want to
144 mess up unconditional or indirect jumps that cross between hot
c22cacf3 145 and cold sections.
8e8d5162
CT
146
147 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
148 be optimizable (or blocks that appear to be mergeable), but which really
149 must be left untouched (they are required to make it safely across
150 partition boundaries). See the comments at the top of
8e8d5162 151 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
750054a2 152
87c8b4be
CT
153 if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
154 || (cbranch_jump_edge->flags & EDGE_CROSSING))
750054a2
CT
155 return false;
156
402209ff
JH
157 /* The conditional branch must target the block after the
158 unconditional branch. */
159 cbranch_dest_block = cbranch_jump_edge->dest;
160
fefa31b5 161 if (cbranch_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
1a8fb012 162 || jump_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
2f52c531 163 || !can_fallthru (jump_block, cbranch_dest_block))
402209ff
JH
164 return false;
165
ca6c03ca 166 /* Invert the conditional branch. */
1476d1bd
MM
167 if (!invert_jump (as_a <rtx_jump_insn *> (cbranch_insn),
168 block_label (jump_dest_block), 0))
ca6c03ca 169 return false;
402209ff 170
c263766c
RH
171 if (dump_file)
172 fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
a813c111 173 INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
402209ff
JH
174
175 /* Success. Update the CFG to match. Note that after this point
176 the edge variable names appear backwards; the redirection is done
177 this way to preserve edge profile data. */
178 cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
179 cbranch_dest_block);
180 cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
181 jump_dest_block);
182 cbranch_jump_edge->flags |= EDGE_FALLTHRU;
183 cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
b446e5a2 184 update_br_prob_note (cbranch_block);
402209ff
JH
185
186 /* Delete the block with the unconditional jump, and clean up the mess. */
f470c378
ZD
187 delete_basic_block (jump_block);
188 tidy_fallthru_edge (cbranch_jump_edge);
261139ce 189 update_forwarder_flag (cbranch_block);
402209ff
JH
190
191 return true;
192}
193\f
8ecba28a
JH
194/* Attempt to prove that operation is NOOP using CSElib or mark the effect
195 on register. Used by jump threading. */
5f0d2358 196
8ecba28a 197static bool
d329e058 198mark_effect (rtx exp, regset nonequal)
8ecba28a 199{
9f16e871 200 rtx dest;
8ecba28a
JH
201 switch (GET_CODE (exp))
202 {
203 /* In case we do clobber the register, mark it as equal, as we know the
c22cacf3 204 value is dead so it don't have to match. */
f87c27b4 205 case CLOBBER:
07a737f3
RS
206 dest = XEXP (exp, 0);
207 if (REG_P (dest))
208 bitmap_clear_range (nonequal, REGNO (dest), REG_NREGS (dest));
f87c27b4 209 return false;
5f0d2358 210
f87c27b4
KH
211 case SET:
212 if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
8ecba28a 213 return false;
f87c27b4
KH
214 dest = SET_DEST (exp);
215 if (dest == pc_rtx)
8ecba28a 216 return false;
f87c27b4
KH
217 if (!REG_P (dest))
218 return true;
07a737f3 219 bitmap_set_range (nonequal, REGNO (dest), REG_NREGS (dest));
f87c27b4
KH
220 return false;
221
222 default:
223 return false;
8ecba28a
JH
224 }
225}
fe477d8b 226
a2250fe9
RS
227/* Return true if X contains a register in NONEQUAL. */
228static bool
229mentions_nonequal_regs (const_rtx x, regset nonequal)
fe477d8b 230{
a2250fe9
RS
231 subrtx_iterator::array_type array;
232 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
fe477d8b 233 {
a2250fe9
RS
234 const_rtx x = *iter;
235 if (REG_P (x))
fe477d8b 236 {
53d1bae9
RS
237 unsigned int end_regno = END_REGNO (x);
238 for (unsigned int regno = REGNO (x); regno < end_regno; ++regno)
239 if (REGNO_REG_SET_P (nonequal, regno))
240 return true;
fe477d8b
JH
241 }
242 }
a2250fe9 243 return false;
fe477d8b 244}
a2250fe9 245
8ecba28a 246/* Attempt to prove that the basic block B will have no side effects and
95bd1dd7 247 always continues in the same edge if reached via E. Return the edge
8ecba28a
JH
248 if exist, NULL otherwise. */
249
250static edge
6fb5fa3c 251thread_jump (edge e, basic_block b)
8ecba28a 252{
da5477a9
DM
253 rtx set1, set2, cond1, cond2;
254 rtx_insn *insn;
8ecba28a
JH
255 enum rtx_code code1, code2, reversed_code2;
256 bool reverse1 = false;
3cd8c58a 257 unsigned i;
8ecba28a
JH
258 regset nonequal;
259 bool failed = false;
a2041967 260 reg_set_iterator rsi;
8ecba28a 261
2dd2d53e 262 if (b->flags & BB_NONTHREADABLE_BLOCK)
1540f9eb
JH
263 return NULL;
264
8ecba28a
JH
265 /* At the moment, we do handle only conditional jumps, but later we may
266 want to extend this code to tablejumps and others. */
628f6a4e 267 if (EDGE_COUNT (e->src->succs) != 2)
8ecba28a 268 return NULL;
628f6a4e 269 if (EDGE_COUNT (b->succs) != 2)
1540f9eb 270 {
2dd2d53e 271 b->flags |= BB_NONTHREADABLE_BLOCK;
1540f9eb
JH
272 return NULL;
273 }
8ecba28a
JH
274
275 /* Second branch must end with onlyjump, as we will eliminate the jump. */
a813c111 276 if (!any_condjump_p (BB_END (e->src)))
8ecba28a 277 return NULL;
f87c27b4 278
a813c111 279 if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
1540f9eb 280 {
2dd2d53e 281 b->flags |= BB_NONTHREADABLE_BLOCK;
1540f9eb
JH
282 return NULL;
283 }
8ecba28a 284
a813c111
SB
285 set1 = pc_set (BB_END (e->src));
286 set2 = pc_set (BB_END (b));
8ecba28a 287 if (((e->flags & EDGE_FALLTHRU) != 0)
68f3f6f1 288 != (XEXP (SET_SRC (set1), 1) == pc_rtx))
8ecba28a
JH
289 reverse1 = true;
290
291 cond1 = XEXP (SET_SRC (set1), 0);
292 cond2 = XEXP (SET_SRC (set2), 0);
293 if (reverse1)
a813c111 294 code1 = reversed_comparison_code (cond1, BB_END (e->src));
8ecba28a
JH
295 else
296 code1 = GET_CODE (cond1);
297
298 code2 = GET_CODE (cond2);
a813c111 299 reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
8ecba28a
JH
300
301 if (!comparison_dominates_p (code1, code2)
302 && !comparison_dominates_p (code1, reversed_code2))
303 return NULL;
304
305 /* Ensure that the comparison operators are equivalent.
95bd1dd7 306 ??? This is far too pessimistic. We should allow swapped operands,
8ecba28a
JH
307 different CCmodes, or for example comparisons for interval, that
308 dominate even when operands are not equivalent. */
309 if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
310 || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
311 return NULL;
312
23c64481
JJ
313 /* Punt if BB_END (e->src) is doloop-like conditional jump that modifies
314 the registers used in cond1. */
315 if (modified_in_p (cond1, BB_END (e->src)))
316 return NULL;
317
8ecba28a
JH
318 /* Short circuit cases where block B contains some side effects, as we can't
319 safely bypass it. */
a813c111 320 for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
8ecba28a
JH
321 insn = NEXT_INSN (insn))
322 if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
1540f9eb 323 {
2dd2d53e 324 b->flags |= BB_NONTHREADABLE_BLOCK;
1540f9eb
JH
325 return NULL;
326 }
8ecba28a 327
457eeaae 328 cselib_init (0);
8ecba28a
JH
329
330 /* First process all values computed in the source basic block. */
3cd8c58a
NS
331 for (insn = NEXT_INSN (BB_HEAD (e->src));
332 insn != NEXT_INSN (BB_END (e->src));
8ecba28a
JH
333 insn = NEXT_INSN (insn))
334 if (INSN_P (insn))
335 cselib_process_insn (insn);
336
8bdbfff5 337 nonequal = BITMAP_ALLOC (NULL);
8ecba28a 338 CLEAR_REG_SET (nonequal);
5f0d2358 339
8ecba28a
JH
340 /* Now assume that we've continued by the edge E to B and continue
341 processing as if it were same basic block.
8ecba28a 342 Our goal is to prove that whole block is an NOOP. */
5f0d2358 343
3cd8c58a
NS
344 for (insn = NEXT_INSN (BB_HEAD (b));
345 insn != NEXT_INSN (BB_END (b)) && !failed;
8ecba28a 346 insn = NEXT_INSN (insn))
f87c27b4 347 {
becba8a7
JJ
348 /* cond2 must not mention any register that is not equal to the
349 former block. Check this before processing that instruction,
350 as BB_END (b) could contain also clobbers. */
351 if (insn == BB_END (b)
352 && mentions_nonequal_regs (cond2, nonequal))
353 goto failed_exit;
354
f87c27b4
KH
355 if (INSN_P (insn))
356 {
357 rtx pat = PATTERN (insn);
358
359 if (GET_CODE (pat) == PARALLEL)
360 {
3cd8c58a 361 for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
f87c27b4
KH
362 failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
363 }
364 else
365 failed |= mark_effect (pat, nonequal);
366 }
5f0d2358 367
f87c27b4
KH
368 cselib_process_insn (insn);
369 }
8ecba28a
JH
370
371 /* Later we should clear nonequal of dead registers. So far we don't
372 have life information in cfg_cleanup. */
373 if (failed)
1540f9eb 374 {
2dd2d53e 375 b->flags |= BB_NONTHREADABLE_BLOCK;
1540f9eb
JH
376 goto failed_exit;
377 }
8ecba28a 378
a2041967
KH
379 EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
380 goto failed_exit;
8ecba28a 381
8bdbfff5 382 BITMAP_FREE (nonequal);
8ecba28a
JH
383 cselib_finish ();
384 if ((comparison_dominates_p (code1, code2) != 0)
4deaa2f8 385 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
8ecba28a
JH
386 return BRANCH_EDGE (b);
387 else
388 return FALLTHRU_EDGE (b);
389
390failed_exit:
8bdbfff5 391 BITMAP_FREE (nonequal);
8ecba28a
JH
392 cselib_finish ();
393 return NULL;
394}
395\f
402209ff 396/* Attempt to forward edges leaving basic block B.
eaec9b3d 397 Return true if successful. */
402209ff
JH
398
399static bool
d329e058 400try_forward_edges (int mode, basic_block b)
402209ff
JH
401{
402 bool changed = false;
628f6a4e
BE
403 edge_iterator ei;
404 edge e, *threaded_edges = NULL;
402209ff 405
628f6a4e 406 for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
402209ff
JH
407 {
408 basic_block target, first;
8a829274
EB
409 location_t goto_locus;
410 int counter;
8ecba28a 411 bool threaded = false;
bcb3bc6d 412 int nthreaded_edges = 0;
4ec5d4f5 413 bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
687d5dfe 414 bool new_target_threaded = false;
402209ff 415
402209ff
JH
416 /* Skip complex edges because we don't know how to update them.
417
c22cacf3
MS
418 Still handle fallthru edges, as we can succeed to forward fallthru
419 edge to the same place as the branch edge of conditional branch
420 and turn conditional branch to an unconditional branch. */
402209ff 421 if (e->flags & EDGE_COMPLEX)
628f6a4e
BE
422 {
423 ei_next (&ei);
424 continue;
425 }
402209ff
JH
426
427 target = first = e->dest;
24bd1a0b 428 counter = NUM_FIXED_BLOCKS;
7241571e 429 goto_locus = e->goto_locus;
402209ff 430
0cae8d31 431 while (counter < n_basic_blocks_for_fn (cfun))
402209ff 432 {
8ecba28a 433 basic_block new_target = NULL;
4ec5d4f5 434 may_thread |= (target->flags & BB_MODIFIED) != 0;
8ecba28a
JH
435
436 if (FORWARDER_BLOCK_P (target)
fefa31b5 437 && single_succ (target) != EXIT_BLOCK_PTR_FOR_FN (cfun))
8ecba28a
JH
438 {
439 /* Bypass trivial infinite loops. */
c5cbcccf
ZD
440 new_target = single_succ (target);
441 if (target == new_target)
0cae8d31 442 counter = n_basic_blocks_for_fn (cfun);
7241571e
JJ
443 else if (!optimize)
444 {
445 /* When not optimizing, ensure that edges or forwarder
446 blocks with different locus are not optimized out. */
8a829274
EB
447 location_t new_locus = single_succ_edge (target)->goto_locus;
448 location_t locus = goto_locus;
7241571e 449
ffa4602f
EB
450 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
451 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
5368224f 452 && new_locus != locus)
50a36e42
EB
453 new_target = NULL;
454 else
7241571e 455 {
ffa4602f 456 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
50a36e42 457 locus = new_locus;
7241571e 458
da5477a9 459 rtx_insn *last = BB_END (target);
11321111
AO
460 if (DEBUG_INSN_P (last))
461 last = prev_nondebug_insn (last);
ffa4602f
EB
462 if (last && INSN_P (last))
463 new_locus = INSN_LOCATION (last);
464 else
465 new_locus = UNKNOWN_LOCATION;
11321111 466
ffa4602f
EB
467 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
468 && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
5368224f 469 && new_locus != locus)
50a36e42
EB
470 new_target = NULL;
471 else
472 {
ffa4602f 473 if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
50a36e42
EB
474 locus = new_locus;
475
476 goto_locus = locus;
477 }
7241571e
JJ
478 }
479 }
8ecba28a 480 }
5f0d2358 481
8ecba28a
JH
482 /* Allow to thread only over one edge at time to simplify updating
483 of probabilities. */
7cf240d5 484 else if ((mode & CLEANUP_THREADING) && may_thread)
8ecba28a 485 {
6fb5fa3c 486 edge t = thread_jump (e, target);
1c570418 487 if (t)
8ecba28a 488 {
bcb3bc6d 489 if (!threaded_edges)
0cae8d31
DM
490 threaded_edges = XNEWVEC (edge,
491 n_basic_blocks_for_fn (cfun));
3b3b1e32
RH
492 else
493 {
494 int i;
495
496 /* Detect an infinite loop across blocks not
497 including the start block. */
498 for (i = 0; i < nthreaded_edges; ++i)
499 if (threaded_edges[i] == t)
500 break;
501 if (i < nthreaded_edges)
b90e45ae 502 {
0cae8d31 503 counter = n_basic_blocks_for_fn (cfun);
b90e45ae
JH
504 break;
505 }
3b3b1e32
RH
506 }
507
508 /* Detect an infinite loop across the start block. */
509 if (t->dest == b)
510 break;
511
0cae8d31
DM
512 gcc_assert (nthreaded_edges
513 < (n_basic_blocks_for_fn (cfun)
514 - NUM_FIXED_BLOCKS));
1c570418 515 threaded_edges[nthreaded_edges++] = t;
3b3b1e32
RH
516
517 new_target = t->dest;
518 new_target_threaded = true;
8ecba28a
JH
519 }
520 }
5f0d2358 521
8ecba28a
JH
522 if (!new_target)
523 break;
402209ff 524
8ecba28a 525 counter++;
687d5dfe
JH
526 /* Do not turn non-crossing jump to crossing. Depending on target
527 it may require different instruction pattern. */
528 if ((e->flags & EDGE_CROSSING)
529 || BB_PARTITION (first) == BB_PARTITION (new_target))
530 {
531 target = new_target;
532 threaded |= new_target_threaded;
533 }
f87c27b4 534 }
402209ff 535
0cae8d31 536 if (counter >= n_basic_blocks_for_fn (cfun))
402209ff 537 {
c263766c
RH
538 if (dump_file)
539 fprintf (dump_file, "Infinite loop in BB %i.\n",
0b17ab2f 540 target->index);
402209ff
JH
541 }
542 else if (target == first)
543 ; /* We didn't do anything. */
544 else
545 {
546 /* Save the values now, as the edge may get removed. */
ef30ab83 547 profile_count edge_count = e->count ();
1c570418 548 int n = 0;
402209ff 549
7241571e
JJ
550 e->goto_locus = goto_locus;
551
6ee3c8e4 552 /* Don't force if target is exit block. */
fefa31b5 553 if (threaded && target != EXIT_BLOCK_PTR_FOR_FN (cfun))
402209ff 554 {
8ecba28a 555 notice_new_block (redirect_edge_and_branch_force (e, target));
c263766c
RH
556 if (dump_file)
557 fprintf (dump_file, "Conditionals threaded.\n");
402209ff 558 }
8ecba28a 559 else if (!redirect_edge_and_branch (e, target))
402209ff 560 {
c263766c
RH
561 if (dump_file)
562 fprintf (dump_file,
5f0d2358 563 "Forwarding edge %i->%i to %i failed.\n",
0b17ab2f 564 b->index, e->dest->index, target->index);
628f6a4e 565 ei_next (&ei);
8ecba28a 566 continue;
402209ff 567 }
5f0d2358 568
8ecba28a
JH
569 /* We successfully forwarded the edge. Now update profile
570 data: for each edge we traversed in the chain, remove
571 the original edge's execution count. */
8ecba28a
JH
572 do
573 {
574 edge t;
5f0d2358 575
c5cbcccf 576 if (!single_succ_p (first))
3b3b1e32 577 {
341c100f 578 gcc_assert (n < nthreaded_edges);
3b3b1e32 579 t = threaded_edges [n++];
341c100f 580 gcc_assert (t->src == first);
e7a74006 581 update_bb_profile_for_threading (first, edge_count, t);
b446e5a2 582 update_br_prob_note (first);
3b3b1e32 583 }
8ecba28a 584 else
bcb3bc6d 585 {
15db5571 586 first->count -= edge_count;
bcb3bc6d
JH
587 /* It is possible that as the result of
588 threading we've removed edge as it is
589 threaded to the fallthru edge. Avoid
590 getting out of sync. */
591 if (n < nthreaded_edges
592 && first == threaded_edges [n]->src)
593 n++;
c5cbcccf 594 t = single_succ_edge (first);
f87c27b4 595 }
5f0d2358 596
8ecba28a
JH
597 first = t->dest;
598 }
599 while (first != target);
600
601 changed = true;
628f6a4e 602 continue;
402209ff 603 }
628f6a4e 604 ei_next (&ei);
402209ff
JH
605 }
606
04695783 607 free (threaded_edges);
402209ff
JH
608 return changed;
609}
610\f
402209ff
JH
611
612/* Blocks A and B are to be merged into a single block. A has no incoming
613 fallthru edge, so it can be moved before B without adding or modifying
614 any jumps (aside from the jump from A to B). */
615
4262e623 616static void
d329e058 617merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
402209ff 618{
da5477a9 619 rtx_insn *barrier;
402209ff 620
750054a2
CT
621 /* If we are partitioning hot/cold basic blocks, we don't want to
622 mess up unconditional or indirect jumps that cross between hot
8e8d5162 623 and cold sections.
c22cacf3 624
8e8d5162 625 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
626 be optimizable (or blocks that appear to be mergeable), but which really
627 must be left untouched (they are required to make it safely across
628 partition boundaries). See the comments at the top of
8e8d5162
CT
629 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
630
87c8b4be 631 if (BB_PARTITION (a) != BB_PARTITION (b))
750054a2
CT
632 return;
633
a813c111 634 barrier = next_nonnote_insn (BB_END (a));
341c100f 635 gcc_assert (BARRIER_P (barrier));
53c17031 636 delete_insn (barrier);
402209ff 637
402209ff 638 /* Scramble the insn chain. */
a813c111
SB
639 if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
640 reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
6fb5fa3c 641 df_set_bb_dirty (a);
402209ff 642
c263766c
RH
643 if (dump_file)
644 fprintf (dump_file, "Moved block %d before %d and merged.\n",
0b17ab2f 645 a->index, b->index);
402209ff 646
bf77398c 647 /* Swap the records for the two blocks around. */
402209ff 648
918ed612
ZD
649 unlink_block (a);
650 link_block (a, b->prev_bb);
651
402209ff 652 /* Now blocks A and B are contiguous. Merge them. */
bc35512f 653 merge_blocks (a, b);
402209ff
JH
654}
655
656/* Blocks A and B are to be merged into a single block. B has no outgoing
657 fallthru edge, so it can be moved after A without adding or modifying
658 any jumps (aside from the jump from A to B). */
659
4262e623 660static void
d329e058 661merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
402209ff 662{
da5477a9 663 rtx_insn *barrier, *real_b_end;
dfe08bc4 664 rtx_insn *label;
8942ee0f 665 rtx_jump_table_data *table;
402209ff 666
750054a2
CT
667 /* If we are partitioning hot/cold basic blocks, we don't want to
668 mess up unconditional or indirect jumps that cross between hot
c22cacf3
MS
669 and cold sections.
670
8e8d5162 671 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
672 be optimizable (or blocks that appear to be mergeable), but which really
673 must be left untouched (they are required to make it safely across
674 partition boundaries). See the comments at the top of
8e8d5162
CT
675 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
676
87c8b4be 677 if (BB_PARTITION (a) != BB_PARTITION (b))
750054a2
CT
678 return;
679
a813c111 680 real_b_end = BB_END (b);
402209ff 681
ee735eef
JZ
682 /* If there is a jump table following block B temporarily add the jump table
683 to block B so that it will also be moved to the correct location. */
a813c111 684 if (tablejump_p (BB_END (b), &label, &table)
dfe08bc4 685 && prev_active_insn (label) == BB_END (b))
402209ff 686 {
1130d5e3 687 BB_END (b) = table;
402209ff
JH
688 }
689
690 /* There had better have been a barrier there. Delete it. */
a813c111 691 barrier = NEXT_INSN (BB_END (b));
4b4bf941 692 if (barrier && BARRIER_P (barrier))
53c17031 693 delete_insn (barrier);
402209ff 694
402209ff
JH
695
696 /* Scramble the insn chain. */
a813c111 697 reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
402209ff 698
f62ce55b 699 /* Restore the real end of b. */
1130d5e3 700 BB_END (b) = real_b_end;
f62ce55b 701
c263766c
RH
702 if (dump_file)
703 fprintf (dump_file, "Moved block %d after %d and merged.\n",
0b17ab2f 704 b->index, a->index);
2150ad33
RH
705
706 /* Now blocks A and B are contiguous. Merge them. */
bc35512f 707 merge_blocks (a, b);
402209ff
JH
708}
709
710/* Attempt to merge basic blocks that are potentially non-adjacent.
ec3ae3da
JH
711 Return NULL iff the attempt failed, otherwise return basic block
712 where cleanup_cfg should continue. Because the merging commonly
713 moves basic block away or introduces another optimization
e0bb17a8 714 possibility, return basic block just before B so cleanup_cfg don't
ec3ae3da
JH
715 need to iterate.
716
717 It may be good idea to return basic block before C in the case
718 C has been moved after B and originally appeared earlier in the
4d6922ee 719 insn sequence, but we have no information available about the
ec3ae3da
JH
720 relative ordering of these two. Hopefully it is not too common. */
721
722static basic_block
bc35512f 723merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
402209ff 724{
ec3ae3da 725 basic_block next;
402209ff 726
750054a2
CT
727 /* If we are partitioning hot/cold basic blocks, we don't want to
728 mess up unconditional or indirect jumps that cross between hot
c22cacf3
MS
729 and cold sections.
730
8e8d5162 731 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
732 be optimizable (or blocks that appear to be mergeable), but which really
733 must be left untouched (they are required to make it safely across
734 partition boundaries). See the comments at the top of
8e8d5162
CT
735 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
736
87c8b4be 737 if (BB_PARTITION (b) != BB_PARTITION (c))
750054a2 738 return NULL;
c22cacf3 739
402209ff
JH
740 /* If B has a fallthru edge to C, no need to move anything. */
741 if (e->flags & EDGE_FALLTHRU)
742 {
0b17ab2f 743 int b_index = b->index, c_index = c->index;
7d776ee2
RG
744
745 /* Protect the loop latches. */
746 if (current_loops && c->loop_father->latch == c)
747 return NULL;
748
bc35512f 749 merge_blocks (b, c);
635559ab 750 update_forwarder_flag (b);
402209ff 751
c263766c
RH
752 if (dump_file)
753 fprintf (dump_file, "Merged %d and %d without moving.\n",
f87c27b4 754 b_index, c_index);
402209ff 755
fefa31b5 756 return b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? b : b->prev_bb;
402209ff 757 }
5f0d2358 758
402209ff
JH
759 /* Otherwise we will need to move code around. Do that only if expensive
760 transformations are allowed. */
761 else if (mode & CLEANUP_EXPENSIVE)
762 {
4262e623
JH
763 edge tmp_edge, b_fallthru_edge;
764 bool c_has_outgoing_fallthru;
765 bool b_has_incoming_fallthru;
402209ff
JH
766
767 /* Avoid overactive code motion, as the forwarder blocks should be
c22cacf3 768 eliminated by edge redirection instead. One exception might have
402209ff
JH
769 been if B is a forwarder block and C has no fallthru edge, but
770 that should be cleaned up by bb-reorder instead. */
635559ab 771 if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
ec3ae3da 772 return NULL;
402209ff
JH
773
774 /* We must make sure to not munge nesting of lexical blocks,
775 and loop notes. This is done by squeezing out all the notes
776 and leaving them there to lie. Not ideal, but functional. */
777
0fd4b31d 778 tmp_edge = find_fallthru_edge (c->succs);
402209ff 779 c_has_outgoing_fallthru = (tmp_edge != NULL);
402209ff 780
0fd4b31d 781 tmp_edge = find_fallthru_edge (b->preds);
402209ff 782 b_has_incoming_fallthru = (tmp_edge != NULL);
4262e623 783 b_fallthru_edge = tmp_edge;
ec3ae3da 784 next = b->prev_bb;
912b79e7
JH
785 if (next == c)
786 next = next->prev_bb;
4262e623
JH
787
788 /* Otherwise, we're going to try to move C after B. If C does
789 not have an outgoing fallthru, then it can be moved
790 immediately after B without introducing or modifying jumps. */
791 if (! c_has_outgoing_fallthru)
792 {
793 merge_blocks_move_successor_nojumps (b, c);
fefa31b5 794 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
4262e623 795 }
402209ff
JH
796
797 /* If B does not have an incoming fallthru, then it can be moved
798 immediately before C without introducing or modifying jumps.
799 C cannot be the first block, so we do not have to worry about
800 accessing a non-existent block. */
402209ff 801
4262e623
JH
802 if (b_has_incoming_fallthru)
803 {
473fb060 804 basic_block bb;
5f0d2358 805
fefa31b5 806 if (b_fallthru_edge->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
ec3ae3da 807 return NULL;
7dddfb65
JH
808 bb = force_nonfallthru (b_fallthru_edge);
809 if (bb)
810 notice_new_block (bb);
4262e623 811 }
5f0d2358 812
4262e623 813 merge_blocks_move_predecessor_nojumps (b, c);
fefa31b5 814 return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
402209ff 815 }
5f0d2358 816
10d6c0d0 817 return NULL;
402209ff
JH
818}
819\f
c2fc5456
R
820
821/* Removes the memory attributes of MEM expression
822 if they are not equal. */
823
893479de 824static void
c2fc5456
R
825merge_memattrs (rtx x, rtx y)
826{
827 int i;
828 int j;
829 enum rtx_code code;
830 const char *fmt;
831
832 if (x == y)
833 return;
834 if (x == 0 || y == 0)
835 return;
836
837 code = GET_CODE (x);
838
839 if (code != GET_CODE (y))
840 return;
841
842 if (GET_MODE (x) != GET_MODE (y))
843 return;
844
96b3c03f 845 if (code == MEM && !mem_attrs_eq_p (MEM_ATTRS (x), MEM_ATTRS (y)))
c2fc5456
R
846 {
847 if (! MEM_ATTRS (x))
848 MEM_ATTRS (y) = 0;
849 else if (! MEM_ATTRS (y))
850 MEM_ATTRS (x) = 0;
c22cacf3 851 else
c2fc5456 852 {
c2fc5456
R
853 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
854 {
855 set_mem_alias_set (x, 0);
856 set_mem_alias_set (y, 0);
857 }
c22cacf3 858
c2fc5456
R
859 if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
860 {
861 set_mem_expr (x, 0);
862 set_mem_expr (y, 0);
527210c4
RS
863 clear_mem_offset (x);
864 clear_mem_offset (y);
c2fc5456 865 }
527210c4
RS
866 else if (MEM_OFFSET_KNOWN_P (x) != MEM_OFFSET_KNOWN_P (y)
867 || (MEM_OFFSET_KNOWN_P (x)
d05d7551 868 && maybe_ne (MEM_OFFSET (x), MEM_OFFSET (y))))
c2fc5456 869 {
527210c4
RS
870 clear_mem_offset (x);
871 clear_mem_offset (y);
c2fc5456 872 }
c22cacf3 873
d05d7551
RS
874 if (!MEM_SIZE_KNOWN_P (x))
875 clear_mem_size (y);
876 else if (!MEM_SIZE_KNOWN_P (y))
877 clear_mem_size (x);
878 else if (known_le (MEM_SIZE (x), MEM_SIZE (y)))
879 set_mem_size (x, MEM_SIZE (y));
880 else if (known_le (MEM_SIZE (y), MEM_SIZE (x)))
881 set_mem_size (y, MEM_SIZE (x));
c2fc5456 882 else
f5541398 883 {
d05d7551 884 /* The sizes aren't ordered, so we can't merge them. */
f5541398
RS
885 clear_mem_size (x);
886 clear_mem_size (y);
887 }
c2fc5456
R
888
889 set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
890 set_mem_align (y, MEM_ALIGN (x));
891 }
892 }
84cf4ab6
JJ
893 if (code == MEM)
894 {
895 if (MEM_READONLY_P (x) != MEM_READONLY_P (y))
896 {
897 MEM_READONLY_P (x) = 0;
898 MEM_READONLY_P (y) = 0;
899 }
900 if (MEM_NOTRAP_P (x) != MEM_NOTRAP_P (y))
901 {
902 MEM_NOTRAP_P (x) = 0;
903 MEM_NOTRAP_P (y) = 0;
904 }
905 if (MEM_VOLATILE_P (x) != MEM_VOLATILE_P (y))
906 {
907 MEM_VOLATILE_P (x) = 1;
908 MEM_VOLATILE_P (y) = 1;
909 }
910 }
c22cacf3 911
c2fc5456
R
912 fmt = GET_RTX_FORMAT (code);
913 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
914 {
915 switch (fmt[i])
916 {
917 case 'E':
918 /* Two vectors must have the same length. */
919 if (XVECLEN (x, i) != XVECLEN (y, i))
920 return;
921
922 for (j = 0; j < XVECLEN (x, i); j++)
923 merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
924
925 break;
926
927 case 'e':
928 merge_memattrs (XEXP (x, i), XEXP (y, i));
929 }
930 }
931 return;
932}
933
934
472c95f5
TV
935 /* Checks if patterns P1 and P2 are equivalent, apart from the possibly
936 different single sets S1 and S2. */
c2fc5456
R
937
938static bool
472c95f5
TV
939equal_different_set_p (rtx p1, rtx s1, rtx p2, rtx s2)
940{
941 int i;
942 rtx e1, e2;
943
944 if (p1 == s1 && p2 == s2)
945 return true;
946
947 if (GET_CODE (p1) != PARALLEL || GET_CODE (p2) != PARALLEL)
948 return false;
949
950 if (XVECLEN (p1, 0) != XVECLEN (p2, 0))
951 return false;
952
953 for (i = 0; i < XVECLEN (p1, 0); i++)
954 {
955 e1 = XVECEXP (p1, 0, i);
956 e2 = XVECEXP (p2, 0, i);
957 if (e1 == s1 && e2 == s2)
958 continue;
959 if (reload_completed
960 ? rtx_renumbered_equal_p (e1, e2) : rtx_equal_p (e1, e2))
961 continue;
962
21c0a521 963 return false;
472c95f5
TV
964 }
965
966 return true;
967}
968
3e87f2d6
SC
969
970/* NOTE1 is the REG_EQUAL note, if any, attached to an insn
971 that is a single_set with a SET_SRC of SRC1. Similarly
972 for NOTE2/SRC2.
973
974 So effectively NOTE1/NOTE2 are an alternate form of
975 SRC1/SRC2 respectively.
976
977 Return nonzero if SRC1 or NOTE1 has the same constant
978 integer value as SRC2 or NOTE2. Else return zero. */
979static int
980values_equal_p (rtx note1, rtx note2, rtx src1, rtx src2)
981{
982 if (note1
983 && note2
984 && CONST_INT_P (XEXP (note1, 0))
985 && rtx_equal_p (XEXP (note1, 0), XEXP (note2, 0)))
986 return 1;
987
988 if (!note1
989 && !note2
990 && CONST_INT_P (src1)
991 && CONST_INT_P (src2)
992 && rtx_equal_p (src1, src2))
993 return 1;
994
995 if (note1
996 && CONST_INT_P (src2)
997 && rtx_equal_p (XEXP (note1, 0), src2))
998 return 1;
999
1000 if (note2
1001 && CONST_INT_P (src1)
1002 && rtx_equal_p (XEXP (note2, 0), src1))
1003 return 1;
1004
1005 return 0;
1006}
1007
472c95f5
TV
1008/* Examine register notes on I1 and I2 and return:
1009 - dir_forward if I1 can be replaced by I2, or
1010 - dir_backward if I2 can be replaced by I1, or
1011 - dir_both if both are the case. */
1012
1013static enum replace_direction
da5477a9 1014can_replace_by (rtx_insn *i1, rtx_insn *i2)
472c95f5
TV
1015{
1016 rtx s1, s2, d1, d2, src1, src2, note1, note2;
1017 bool c1, c2;
1018
1019 /* Check for 2 sets. */
1020 s1 = single_set (i1);
1021 s2 = single_set (i2);
1022 if (s1 == NULL_RTX || s2 == NULL_RTX)
1023 return dir_none;
1024
1025 /* Check that the 2 sets set the same dest. */
1026 d1 = SET_DEST (s1);
1027 d2 = SET_DEST (s2);
1028 if (!(reload_completed
1029 ? rtx_renumbered_equal_p (d1, d2) : rtx_equal_p (d1, d2)))
1030 return dir_none;
1031
1032 /* Find identical req_equiv or reg_equal note, which implies that the 2 sets
1033 set dest to the same value. */
1034 note1 = find_reg_equal_equiv_note (i1);
1035 note2 = find_reg_equal_equiv_note (i2);
3e87f2d6
SC
1036
1037 src1 = SET_SRC (s1);
1038 src2 = SET_SRC (s2);
1039
1040 if (!values_equal_p (note1, note2, src1, src2))
472c95f5
TV
1041 return dir_none;
1042
1043 if (!equal_different_set_p (PATTERN (i1), s1, PATTERN (i2), s2))
1044 return dir_none;
1045
1046 /* Although the 2 sets set dest to the same value, we cannot replace
1047 (set (dest) (const_int))
1048 by
1049 (set (dest) (reg))
1050 because we don't know if the reg is live and has the same value at the
1051 location of replacement. */
472c95f5
TV
1052 c1 = CONST_INT_P (src1);
1053 c2 = CONST_INT_P (src2);
1054 if (c1 && c2)
1055 return dir_both;
1056 else if (c2)
1057 return dir_forward;
1058 else if (c1)
1059 return dir_backward;
1060
1061 return dir_none;
1062}
1063
1064/* Merges directions A and B. */
1065
1066static enum replace_direction
1067merge_dir (enum replace_direction a, enum replace_direction b)
1068{
1069 /* Implements the following table:
1070 |bo fw bw no
1071 ---+-----------
1072 bo |bo fw bw no
1073 fw |-- fw no no
1074 bw |-- -- bw no
1075 no |-- -- -- no. */
1076
1077 if (a == b)
1078 return a;
1079
1080 if (a == dir_both)
1081 return b;
1082 if (b == dir_both)
1083 return a;
1084
1085 return dir_none;
1086}
1087
aade772d
JJ
1088/* Array of flags indexed by reg note kind, true if the given
1089 reg note is CFA related. */
1090static const bool reg_note_cfa_p[] = {
1091#undef REG_CFA_NOTE
1092#define DEF_REG_NOTE(NAME) false,
1093#define REG_CFA_NOTE(NAME) true,
1094#include "reg-notes.def"
1095#undef REG_CFA_NOTE
1096#undef DEF_REG_NOTE
1097 false
1098};
1099
1100/* Return true if I1 and I2 have identical CFA notes (the same order
1101 and equivalent content). */
1102
1103static bool
1104insns_have_identical_cfa_notes (rtx_insn *i1, rtx_insn *i2)
1105{
1106 rtx n1, n2;
1107 for (n1 = REG_NOTES (i1), n2 = REG_NOTES (i2); ;
1108 n1 = XEXP (n1, 1), n2 = XEXP (n2, 1))
1109 {
1110 /* Skip over reg notes not related to CFI information. */
1111 while (n1 && !reg_note_cfa_p[REG_NOTE_KIND (n1)])
1112 n1 = XEXP (n1, 1);
1113 while (n2 && !reg_note_cfa_p[REG_NOTE_KIND (n2)])
1114 n2 = XEXP (n2, 1);
1115 if (n1 == NULL_RTX && n2 == NULL_RTX)
1116 return true;
1117 if (n1 == NULL_RTX || n2 == NULL_RTX)
1118 return false;
1119 if (XEXP (n1, 0) == XEXP (n2, 0))
1120 ;
1121 else if (XEXP (n1, 0) == NULL_RTX || XEXP (n2, 0) == NULL_RTX)
1122 return false;
1123 else if (!(reload_completed
1124 ? rtx_renumbered_equal_p (XEXP (n1, 0), XEXP (n2, 0))
1125 : rtx_equal_p (XEXP (n1, 0), XEXP (n2, 0))))
1126 return false;
1127 }
1128}
1129
472c95f5
TV
1130/* Examine I1 and I2 and return:
1131 - dir_forward if I1 can be replaced by I2, or
1132 - dir_backward if I2 can be replaced by I1, or
1133 - dir_both if both are the case. */
1134
1135static enum replace_direction
da5477a9 1136old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx_insn *i1, rtx_insn *i2)
c2fc5456
R
1137{
1138 rtx p1, p2;
1139
1140 /* Verify that I1 and I2 are equivalent. */
1141 if (GET_CODE (i1) != GET_CODE (i2))
472c95f5 1142 return dir_none;
c2fc5456 1143
ba21aba3
DD
1144 /* __builtin_unreachable() may lead to empty blocks (ending with
1145 NOTE_INSN_BASIC_BLOCK). They may be crossjumped. */
1146 if (NOTE_INSN_BASIC_BLOCK_P (i1) && NOTE_INSN_BASIC_BLOCK_P (i2))
472c95f5 1147 return dir_both;
ba21aba3 1148
9a08d230
RH
1149 /* ??? Do not allow cross-jumping between different stack levels. */
1150 p1 = find_reg_note (i1, REG_ARGS_SIZE, NULL);
1151 p2 = find_reg_note (i2, REG_ARGS_SIZE, NULL);
42aa5124
RH
1152 if (p1 && p2)
1153 {
1154 p1 = XEXP (p1, 0);
1155 p2 = XEXP (p2, 0);
1156 if (!rtx_equal_p (p1, p2))
1157 return dir_none;
1158
1159 /* ??? Worse, this adjustment had better be constant lest we
1160 have differing incoming stack levels. */
1161 if (!frame_pointer_needed
68184180 1162 && known_eq (find_args_size_adjust (i1), HOST_WIDE_INT_MIN))
42aa5124
RH
1163 return dir_none;
1164 }
1165 else if (p1 || p2)
9a08d230
RH
1166 return dir_none;
1167
aade772d
JJ
1168 /* Do not allow cross-jumping between frame related insns and other
1169 insns. */
1170 if (RTX_FRAME_RELATED_P (i1) != RTX_FRAME_RELATED_P (i2))
1171 return dir_none;
1172
7752e522 1173 p1 = PATTERN (i1);
c2fc5456
R
1174 p2 = PATTERN (i2);
1175
1176 if (GET_CODE (p1) != GET_CODE (p2))
472c95f5 1177 return dir_none;
c2fc5456
R
1178
1179 /* If this is a CALL_INSN, compare register usage information.
1180 If we don't check this on stack register machines, the two
1181 CALL_INSNs might be merged leaving reg-stack.c with mismatching
1182 numbers of stack registers in the same basic block.
1183 If we don't check this on machines with delay slots, a delay slot may
1184 be filled that clobbers a parameter expected by the subroutine.
1185
1186 ??? We take the simple route for now and assume that if they're
31ce8a53 1187 equal, they were constructed identically.
c2fc5456 1188
31ce8a53
BS
1189 Also check for identical exception regions. */
1190
1191 if (CALL_P (i1))
1192 {
1193 /* Ensure the same EH region. */
1194 rtx n1 = find_reg_note (i1, REG_EH_REGION, 0);
1195 rtx n2 = find_reg_note (i2, REG_EH_REGION, 0);
1196
1197 if (!n1 && n2)
472c95f5 1198 return dir_none;
31ce8a53
BS
1199
1200 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
472c95f5 1201 return dir_none;
31ce8a53
BS
1202
1203 if (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
c22cacf3 1204 CALL_INSN_FUNCTION_USAGE (i2))
31ce8a53 1205 || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2))
472c95f5 1206 return dir_none;
68a9738a
JJ
1207
1208 /* For address sanitizer, never crossjump __asan_report_* builtins,
1209 otherwise errors might be reported on incorrect lines. */
de5a5fa1 1210 if (flag_sanitize & SANITIZE_ADDRESS)
68a9738a
JJ
1211 {
1212 rtx call = get_call_rtx_from (i1);
1213 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
1214 {
1215 rtx symbol = XEXP (XEXP (call, 0), 0);
1216 if (SYMBOL_REF_DECL (symbol)
1217 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
1218 {
1219 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
1220 == BUILT_IN_NORMAL)
1221 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
1222 >= BUILT_IN_ASAN_REPORT_LOAD1
1223 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
8946c29e 1224 <= BUILT_IN_ASAN_STOREN)
68a9738a
JJ
1225 return dir_none;
1226 }
1227 }
1228 }
4b1a1d98 1229
5a5a3bc5
RS
1230 HARD_REG_SET i1_used = insn_callee_abi (i1).full_reg_clobbers ();
1231 HARD_REG_SET i2_used = insn_callee_abi (i2).full_reg_clobbers ();
1232 /* ??? This preserves traditional behavior; it might not be needed. */
1233 i1_used |= fixed_reg_set;
1234 i2_used |= fixed_reg_set;
4b1a1d98 1235
a8579651 1236 if (i1_used != i2_used)
4b1a1d98 1237 return dir_none;
31ce8a53 1238 }
c2fc5456 1239
aade772d
JJ
1240 /* If both i1 and i2 are frame related, verify all the CFA notes
1241 in the same order and with the same content. */
1242 if (RTX_FRAME_RELATED_P (i1) && !insns_have_identical_cfa_notes (i1, i2))
1243 return dir_none;
1244
c2fc5456
R
1245#ifdef STACK_REGS
1246 /* If cross_jump_death_matters is not 0, the insn's mode
1247 indicates whether or not the insn contains any stack-like
1248 regs. */
1249
1250 if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
1251 {
1252 /* If register stack conversion has already been done, then
c22cacf3
MS
1253 death notes must also be compared before it is certain that
1254 the two instruction streams match. */
c2fc5456
R
1255
1256 rtx note;
1257 HARD_REG_SET i1_regset, i2_regset;
1258
1259 CLEAR_HARD_REG_SET (i1_regset);
1260 CLEAR_HARD_REG_SET (i2_regset);
1261
1262 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1263 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1264 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1265
1266 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1267 if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
1268 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1269
a8579651 1270 if (i1_regset != i2_regset)
472c95f5 1271 return dir_none;
c2fc5456
R
1272 }
1273#endif
1274
1275 if (reload_completed
1276 ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
472c95f5 1277 return dir_both;
c2fc5456 1278
472c95f5 1279 return can_replace_by (i1, i2);
c2fc5456
R
1280}
1281\f
31ce8a53
BS
1282/* When comparing insns I1 and I2 in flow_find_cross_jump or
1283 flow_find_head_matching_sequence, ensure the notes match. */
1284
1285static void
da5477a9 1286merge_notes (rtx_insn *i1, rtx_insn *i2)
31ce8a53
BS
1287{
1288 /* If the merged insns have different REG_EQUAL notes, then
1289 remove them. */
1290 rtx equiv1 = find_reg_equal_equiv_note (i1);
1291 rtx equiv2 = find_reg_equal_equiv_note (i2);
1292
1293 if (equiv1 && !equiv2)
1294 remove_note (i1, equiv1);
1295 else if (!equiv1 && equiv2)
1296 remove_note (i2, equiv2);
1297 else if (equiv1 && equiv2
1298 && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1299 {
1300 remove_note (i1, equiv1);
1301 remove_note (i2, equiv2);
1302 }
1303}
1304
823918ae
TV
1305 /* Walks from I1 in BB1 backward till the next non-debug insn, and returns the
1306 resulting insn in I1, and the corresponding bb in BB1. At the head of a
1307 bb, if there is a predecessor bb that reaches this bb via fallthru, and
1308 FOLLOW_FALLTHRU, walks further in the predecessor bb and registers this in
1309 DID_FALLTHRU. Otherwise, stops at the head of the bb. */
1310
1311static void
da5477a9 1312walk_to_nondebug_insn (rtx_insn **i1, basic_block *bb1, bool follow_fallthru,
823918ae
TV
1313 bool *did_fallthru)
1314{
1315 edge fallthru;
1316
1317 *did_fallthru = false;
1318
1319 /* Ignore notes. */
1320 while (!NONDEBUG_INSN_P (*i1))
1321 {
1322 if (*i1 != BB_HEAD (*bb1))
1323 {
1324 *i1 = PREV_INSN (*i1);
1325 continue;
1326 }
1327
1328 if (!follow_fallthru)
1329 return;
1330
1331 fallthru = find_fallthru_edge ((*bb1)->preds);
fefa31b5 1332 if (!fallthru || fallthru->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
823918ae
TV
1333 || !single_succ_p (fallthru->src))
1334 return;
1335
1336 *bb1 = fallthru->src;
1337 *i1 = BB_END (*bb1);
1338 *did_fallthru = true;
1339 }
1340}
1341
c2fc5456 1342/* Look through the insns at the end of BB1 and BB2 and find the longest
472c95f5
TV
1343 sequence that are either equivalent, or allow forward or backward
1344 replacement. Store the first insns for that sequence in *F1 and *F2 and
1345 return the sequence length.
1346
1347 DIR_P indicates the allowed replacement direction on function entry, and
1348 the actual replacement direction on function exit. If NULL, only equivalent
1349 sequences are allowed.
c2fc5456
R
1350
1351 To simplify callers of this function, if the blocks match exactly,
1352 store the head of the blocks in *F1 and *F2. */
1353
31ce8a53 1354int
da5477a9
DM
1355flow_find_cross_jump (basic_block bb1, basic_block bb2, rtx_insn **f1,
1356 rtx_insn **f2, enum replace_direction *dir_p)
c2fc5456 1357{
da5477a9 1358 rtx_insn *i1, *i2, *last1, *last2, *afterlast1, *afterlast2;
c2fc5456 1359 int ninsns = 0;
472c95f5 1360 enum replace_direction dir, last_dir, afterlast_dir;
823918ae 1361 bool follow_fallthru, did_fallthru;
472c95f5
TV
1362
1363 if (dir_p)
1364 dir = *dir_p;
1365 else
1366 dir = dir_both;
1367 afterlast_dir = dir;
1368 last_dir = afterlast_dir;
c2fc5456
R
1369
1370 /* Skip simple jumps at the end of the blocks. Complex jumps still
1371 need to be compared for equivalence, which we'll do below. */
1372
1373 i1 = BB_END (bb1);
da5477a9 1374 last1 = afterlast1 = last2 = afterlast2 = NULL;
c2fc5456
R
1375 if (onlyjump_p (i1)
1376 || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
1377 {
1378 last1 = i1;
1379 i1 = PREV_INSN (i1);
1380 }
1381
1382 i2 = BB_END (bb2);
1383 if (onlyjump_p (i2)
1384 || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
1385 {
1386 last2 = i2;
a0cbe71e
JJ
1387 /* Count everything except for unconditional jump as insn.
1388 Don't count any jumps if dir_p is NULL. */
1389 if (!simplejump_p (i2) && !returnjump_p (i2) && last1 && dir_p)
c2fc5456
R
1390 ninsns++;
1391 i2 = PREV_INSN (i2);
1392 }
1393
1394 while (true)
1395 {
823918ae
TV
1396 /* In the following example, we can replace all jumps to C by jumps to A.
1397
1398 This removes 4 duplicate insns.
1399 [bb A] insn1 [bb C] insn1
1400 insn2 insn2
1401 [bb B] insn3 insn3
1402 insn4 insn4
1403 jump_insn jump_insn
1404
1405 We could also replace all jumps to A by jumps to C, but that leaves B
1406 alive, and removes only 2 duplicate insns. In a subsequent crossjump
1407 step, all jumps to B would be replaced with jumps to the middle of C,
1408 achieving the same result with more effort.
1409 So we allow only the first possibility, which means that we don't allow
1410 fallthru in the block that's being replaced. */
1411
1412 follow_fallthru = dir_p && dir != dir_forward;
1413 walk_to_nondebug_insn (&i1, &bb1, follow_fallthru, &did_fallthru);
1414 if (did_fallthru)
1415 dir = dir_backward;
1416
1417 follow_fallthru = dir_p && dir != dir_backward;
1418 walk_to_nondebug_insn (&i2, &bb2, follow_fallthru, &did_fallthru);
1419 if (did_fallthru)
1420 dir = dir_forward;
c2fc5456
R
1421
1422 if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
1423 break;
1424
ba61fc53
JH
1425 /* Do not turn corssing edge to non-crossing or vice versa after
1426 reload. */
1427 if (BB_PARTITION (BLOCK_FOR_INSN (i1))
1428 != BB_PARTITION (BLOCK_FOR_INSN (i2))
1429 && reload_completed)
1430 break;
1431
472c95f5
TV
1432 dir = merge_dir (dir, old_insns_match_p (0, i1, i2));
1433 if (dir == dir_none || (!dir_p && dir != dir_both))
c2fc5456
R
1434 break;
1435
1436 merge_memattrs (i1, i2);
1437
1438 /* Don't begin a cross-jump with a NOTE insn. */
1439 if (INSN_P (i1))
1440 {
31ce8a53 1441 merge_notes (i1, i2);
c2fc5456
R
1442
1443 afterlast1 = last1, afterlast2 = last2;
1444 last1 = i1, last2 = i2;
472c95f5
TV
1445 afterlast_dir = last_dir;
1446 last_dir = dir;
a0cbe71e 1447 if (active_insn_p (i1))
2a562b0a 1448 ninsns++;
c2fc5456
R
1449 }
1450
1451 i1 = PREV_INSN (i1);
1452 i2 = PREV_INSN (i2);
1453 }
1454
c2fc5456
R
1455 /* Don't allow the insn after a compare to be shared by
1456 cross-jumping unless the compare is also shared. */
618f4073
TS
1457 if (HAVE_cc0 && ninsns && reg_mentioned_p (cc0_rtx, last1)
1458 && ! sets_cc0_p (last1))
472c95f5 1459 last1 = afterlast1, last2 = afterlast2, last_dir = afterlast_dir, ninsns--;
c2fc5456
R
1460
1461 /* Include preceding notes and labels in the cross-jump. One,
1462 this may bring us to the head of the blocks as requested above.
1463 Two, it keeps line number notes as matched as may be. */
1464 if (ninsns)
1465 {
823918ae 1466 bb1 = BLOCK_FOR_INSN (last1);
b5b8b0ac 1467 while (last1 != BB_HEAD (bb1) && !NONDEBUG_INSN_P (PREV_INSN (last1)))
c2fc5456
R
1468 last1 = PREV_INSN (last1);
1469
1470 if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
1471 last1 = PREV_INSN (last1);
1472
823918ae 1473 bb2 = BLOCK_FOR_INSN (last2);
b5b8b0ac 1474 while (last2 != BB_HEAD (bb2) && !NONDEBUG_INSN_P (PREV_INSN (last2)))
c2fc5456
R
1475 last2 = PREV_INSN (last2);
1476
1477 if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
1478 last2 = PREV_INSN (last2);
1479
1480 *f1 = last1;
1481 *f2 = last2;
1482 }
1483
472c95f5
TV
1484 if (dir_p)
1485 *dir_p = last_dir;
c2fc5456
R
1486 return ninsns;
1487}
1488
31ce8a53
BS
1489/* Like flow_find_cross_jump, except start looking for a matching sequence from
1490 the head of the two blocks. Do not include jumps at the end.
1491 If STOP_AFTER is nonzero, stop after finding that many matching
b59e0455
JJ
1492 instructions. If STOP_AFTER is zero, count all INSN_P insns, if it is
1493 non-zero, only count active insns. */
31ce8a53
BS
1494
1495int
da5477a9
DM
1496flow_find_head_matching_sequence (basic_block bb1, basic_block bb2, rtx_insn **f1,
1497 rtx_insn **f2, int stop_after)
31ce8a53 1498{
da5477a9 1499 rtx_insn *i1, *i2, *last1, *last2, *beforelast1, *beforelast2;
31ce8a53
BS
1500 int ninsns = 0;
1501 edge e;
1502 edge_iterator ei;
1503 int nehedges1 = 0, nehedges2 = 0;
1504
1505 FOR_EACH_EDGE (e, ei, bb1->succs)
1506 if (e->flags & EDGE_EH)
1507 nehedges1++;
1508 FOR_EACH_EDGE (e, ei, bb2->succs)
1509 if (e->flags & EDGE_EH)
1510 nehedges2++;
1511
1512 i1 = BB_HEAD (bb1);
1513 i2 = BB_HEAD (bb2);
da5477a9 1514 last1 = beforelast1 = last2 = beforelast2 = NULL;
31ce8a53
BS
1515
1516 while (true)
1517 {
4ec5d4f5 1518 /* Ignore notes, except NOTE_INSN_EPILOGUE_BEG. */
31ce8a53 1519 while (!NONDEBUG_INSN_P (i1) && i1 != BB_END (bb1))
4ec5d4f5
BS
1520 {
1521 if (NOTE_P (i1) && NOTE_KIND (i1) == NOTE_INSN_EPILOGUE_BEG)
1522 break;
1523 i1 = NEXT_INSN (i1);
1524 }
31ce8a53
BS
1525
1526 while (!NONDEBUG_INSN_P (i2) && i2 != BB_END (bb2))
4ec5d4f5
BS
1527 {
1528 if (NOTE_P (i2) && NOTE_KIND (i2) == NOTE_INSN_EPILOGUE_BEG)
1529 break;
1530 i2 = NEXT_INSN (i2);
1531 }
31ce8a53 1532
662592e1
BS
1533 if ((i1 == BB_END (bb1) && !NONDEBUG_INSN_P (i1))
1534 || (i2 == BB_END (bb2) && !NONDEBUG_INSN_P (i2)))
1535 break;
1536
31ce8a53
BS
1537 if (NOTE_P (i1) || NOTE_P (i2)
1538 || JUMP_P (i1) || JUMP_P (i2))
1539 break;
1540
1541 /* A sanity check to make sure we're not merging insns with different
1542 effects on EH. If only one of them ends a basic block, it shouldn't
1543 have an EH edge; if both end a basic block, there should be the same
1544 number of EH edges. */
1545 if ((i1 == BB_END (bb1) && i2 != BB_END (bb2)
1546 && nehedges1 > 0)
1547 || (i2 == BB_END (bb2) && i1 != BB_END (bb1)
1548 && nehedges2 > 0)
1549 || (i1 == BB_END (bb1) && i2 == BB_END (bb2)
1550 && nehedges1 != nehedges2))
1551 break;
1552
472c95f5 1553 if (old_insns_match_p (0, i1, i2) != dir_both)
31ce8a53
BS
1554 break;
1555
1556 merge_memattrs (i1, i2);
1557
1558 /* Don't begin a cross-jump with a NOTE insn. */
1559 if (INSN_P (i1))
1560 {
1561 merge_notes (i1, i2);
1562
1563 beforelast1 = last1, beforelast2 = last2;
1564 last1 = i1, last2 = i2;
b59e0455 1565 if (!stop_after || active_insn_p (i1))
a0cbe71e 1566 ninsns++;
31ce8a53
BS
1567 }
1568
1569 if (i1 == BB_END (bb1) || i2 == BB_END (bb2)
1570 || (stop_after > 0 && ninsns == stop_after))
1571 break;
1572
1573 i1 = NEXT_INSN (i1);
1574 i2 = NEXT_INSN (i2);
1575 }
1576
31ce8a53
BS
1577 /* Don't allow a compare to be shared by cross-jumping unless the insn
1578 after the compare is also shared. */
618f4073
TS
1579 if (HAVE_cc0 && ninsns && reg_mentioned_p (cc0_rtx, last1)
1580 && sets_cc0_p (last1))
31ce8a53 1581 last1 = beforelast1, last2 = beforelast2, ninsns--;
31ce8a53
BS
1582
1583 if (ninsns)
1584 {
1585 *f1 = last1;
1586 *f2 = last2;
1587 }
1588
1589 return ninsns;
1590}
1591
c2fc5456
R
1592/* Return true iff outgoing edges of BB1 and BB2 match, together with
1593 the branch instruction. This means that if we commonize the control
1594 flow before end of the basic block, the semantic remains unchanged.
402209ff
JH
1595
1596 We may assume that there exists one edge with a common destination. */
1597
1598static bool
c2fc5456 1599outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
402209ff 1600{
0dd0e980
JH
1601 int nehedges1 = 0, nehedges2 = 0;
1602 edge fallthru1 = 0, fallthru2 = 0;
1603 edge e1, e2;
628f6a4e 1604 edge_iterator ei;
0dd0e980 1605
6626665f 1606 /* If we performed shrink-wrapping, edges to the exit block can
484db665
BS
1607 only be distinguished for JUMP_INSNs. The two paths may differ in
1608 whether they went through the prologue. Sibcalls are fine, we know
1609 that we either didn't need or inserted an epilogue before them. */
1610 if (crtl->shrink_wrapped
fefa31b5
DM
1611 && single_succ_p (bb1)
1612 && single_succ (bb1) == EXIT_BLOCK_PTR_FOR_FN (cfun)
b532a785
JJ
1613 && (!JUMP_P (BB_END (bb1))
1614 /* Punt if the only successor is a fake edge to exit, the jump
1615 must be some weird one. */
1616 || (single_succ_edge (bb1)->flags & EDGE_FAKE) != 0)
484db665
BS
1617 && !(CALL_P (BB_END (bb1)) && SIBLING_CALL_P (BB_END (bb1))))
1618 return false;
b532a785 1619
c04cf67b
RH
1620 /* If BB1 has only one successor, we may be looking at either an
1621 unconditional jump, or a fake edge to exit. */
c5cbcccf
ZD
1622 if (single_succ_p (bb1)
1623 && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
4b4bf941 1624 && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
c5cbcccf
ZD
1625 return (single_succ_p (bb2)
1626 && (single_succ_edge (bb2)->flags
1627 & (EDGE_COMPLEX | EDGE_FAKE)) == 0
4b4bf941 1628 && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
402209ff
JH
1629
1630 /* Match conditional jumps - this may get tricky when fallthru and branch
1631 edges are crossed. */
628f6a4e 1632 if (EDGE_COUNT (bb1->succs) == 2
a813c111
SB
1633 && any_condjump_p (BB_END (bb1))
1634 && onlyjump_p (BB_END (bb1)))
402209ff 1635 {
c2fc5456
R
1636 edge b1, f1, b2, f2;
1637 bool reverse, match;
1638 rtx set1, set2, cond1, cond2;
1639 enum rtx_code code1, code2;
1640
628f6a4e 1641 if (EDGE_COUNT (bb2->succs) != 2
a813c111
SB
1642 || !any_condjump_p (BB_END (bb2))
1643 || !onlyjump_p (BB_END (bb2)))
0a2ed1f1 1644 return false;
c2fc5456
R
1645
1646 b1 = BRANCH_EDGE (bb1);
1647 b2 = BRANCH_EDGE (bb2);
1648 f1 = FALLTHRU_EDGE (bb1);
1649 f2 = FALLTHRU_EDGE (bb2);
1650
1651 /* Get around possible forwarders on fallthru edges. Other cases
c22cacf3 1652 should be optimized out already. */
c2fc5456
R
1653 if (FORWARDER_BLOCK_P (f1->dest))
1654 f1 = single_succ_edge (f1->dest);
1655
1656 if (FORWARDER_BLOCK_P (f2->dest))
1657 f2 = single_succ_edge (f2->dest);
1658
1659 /* To simplify use of this function, return false if there are
1660 unneeded forwarder blocks. These will get eliminated later
1661 during cleanup_cfg. */
1662 if (FORWARDER_BLOCK_P (f1->dest)
1663 || FORWARDER_BLOCK_P (f2->dest)
1664 || FORWARDER_BLOCK_P (b1->dest)
1665 || FORWARDER_BLOCK_P (b2->dest))
1666 return false;
1667
1668 if (f1->dest == f2->dest && b1->dest == b2->dest)
1669 reverse = false;
1670 else if (f1->dest == b2->dest && b1->dest == f2->dest)
1671 reverse = true;
1672 else
1673 return false;
1674
1675 set1 = pc_set (BB_END (bb1));
1676 set2 = pc_set (BB_END (bb2));
1677 if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
1678 != (XEXP (SET_SRC (set2), 1) == pc_rtx))
1679 reverse = !reverse;
1680
1681 cond1 = XEXP (SET_SRC (set1), 0);
1682 cond2 = XEXP (SET_SRC (set2), 0);
1683 code1 = GET_CODE (cond1);
1684 if (reverse)
1685 code2 = reversed_comparison_code (cond2, BB_END (bb2));
1686 else
1687 code2 = GET_CODE (cond2);
1688
1689 if (code2 == UNKNOWN)
1690 return false;
1691
1692 /* Verify codes and operands match. */
1693 match = ((code1 == code2
1694 && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
1695 && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
1696 || (code1 == swap_condition (code2)
1697 && rtx_renumbered_equal_p (XEXP (cond1, 1),
1698 XEXP (cond2, 0))
1699 && rtx_renumbered_equal_p (XEXP (cond1, 0),
1700 XEXP (cond2, 1))));
1701
1702 /* If we return true, we will join the blocks. Which means that
1703 we will only have one branch prediction bit to work with. Thus
1704 we require the existing branches to have probabilities that are
1705 roughly similar. */
1706 if (match
efd8f750
JH
1707 && optimize_bb_for_speed_p (bb1)
1708 && optimize_bb_for_speed_p (bb2))
c2fc5456 1709 {
357067f2 1710 profile_probability prob2;
c2fc5456
R
1711
1712 if (b1->dest == b2->dest)
1713 prob2 = b2->probability;
1714 else
1715 /* Do not use f2 probability as f2 may be forwarded. */
357067f2 1716 prob2 = b2->probability.invert ();
c2fc5456
R
1717
1718 /* Fail if the difference in probabilities is greater than 50%.
1719 This rules out two well-predicted branches with opposite
1720 outcomes. */
357067f2 1721 if (b1->probability.differs_lot_from_p (prob2))
c2fc5456
R
1722 {
1723 if (dump_file)
357067f2
JH
1724 {
1725 fprintf (dump_file,
1726 "Outcomes of branch in bb %i and %i differ too"
1727 " much (", bb1->index, bb2->index);
1728 b1->probability.dump (dump_file);
1729 prob2.dump (dump_file);
1730 fprintf (dump_file, ")\n");
1731 }
c2fc5456
R
1732 return false;
1733 }
1734 }
1735
1736 if (dump_file && match)
1737 fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
1738 bb1->index, bb2->index);
1739
1740 return match;
402209ff
JH
1741 }
1742
09da1532 1743 /* Generic case - we are seeing a computed jump, table jump or trapping
0dd0e980
JH
1744 instruction. */
1745
39811184
JZ
1746 /* Check whether there are tablejumps in the end of BB1 and BB2.
1747 Return true if they are identical. */
1748 {
dfe08bc4 1749 rtx_insn *label1, *label2;
8942ee0f 1750 rtx_jump_table_data *table1, *table2;
39811184 1751
a813c111
SB
1752 if (tablejump_p (BB_END (bb1), &label1, &table1)
1753 && tablejump_p (BB_END (bb2), &label2, &table2)
39811184
JZ
1754 && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
1755 {
1756 /* The labels should never be the same rtx. If they really are same
1757 the jump tables are same too. So disable crossjumping of blocks BB1
1758 and BB2 because when deleting the common insns in the end of BB1
6de9cd9a 1759 by delete_basic_block () the jump table would be deleted too. */
4af16369 1760 /* If LABEL2 is referenced in BB1->END do not do anything
39811184
JZ
1761 because we would loose information when replacing
1762 LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END. */
a813c111 1763 if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
39811184
JZ
1764 {
1765 /* Set IDENTICAL to true when the tables are identical. */
1766 bool identical = false;
1767 rtx p1, p2;
1768
1769 p1 = PATTERN (table1);
1770 p2 = PATTERN (table2);
1771 if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
1772 {
1773 identical = true;
1774 }
1775 else if (GET_CODE (p1) == ADDR_DIFF_VEC
1776 && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
1777 && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
1778 && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
1779 {
1780 int i;
1781
1782 identical = true;
1783 for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
1784 if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
1785 identical = false;
1786 }
1787
c2fc5456 1788 if (identical)
39811184 1789 {
39811184
JZ
1790 bool match;
1791
c2fc5456 1792 /* Temporarily replace references to LABEL1 with LABEL2
39811184 1793 in BB1->END so that we could compare the instructions. */
a2b7026c 1794 replace_label_in_insn (BB_END (bb1), label1, label2, false);
39811184 1795
472c95f5
TV
1796 match = (old_insns_match_p (mode, BB_END (bb1), BB_END (bb2))
1797 == dir_both);
c263766c
RH
1798 if (dump_file && match)
1799 fprintf (dump_file,
39811184
JZ
1800 "Tablejumps in bb %i and %i match.\n",
1801 bb1->index, bb2->index);
1802
c2fc5456
R
1803 /* Set the original label in BB1->END because when deleting
1804 a block whose end is a tablejump, the tablejump referenced
1805 from the instruction is deleted too. */
a2b7026c 1806 replace_label_in_insn (BB_END (bb1), label2, label1, false);
c2fc5456 1807
39811184
JZ
1808 return match;
1809 }
1810 }
1811 return false;
1812 }
1813 }
39811184 1814
d41d6122
TJ
1815 /* Find the last non-debug non-note instruction in each bb, except
1816 stop when we see the NOTE_INSN_BASIC_BLOCK, as old_insns_match_p
1817 handles that case specially. old_insns_match_p does not handle
1818 other types of instruction notes. */
da5477a9
DM
1819 rtx_insn *last1 = BB_END (bb1);
1820 rtx_insn *last2 = BB_END (bb2);
d41d6122
TJ
1821 while (!NOTE_INSN_BASIC_BLOCK_P (last1) &&
1822 (DEBUG_INSN_P (last1) || NOTE_P (last1)))
1823 last1 = PREV_INSN (last1);
1824 while (!NOTE_INSN_BASIC_BLOCK_P (last2) &&
1825 (DEBUG_INSN_P (last2) || NOTE_P (last2)))
1826 last2 = PREV_INSN (last2);
1827 gcc_assert (last1 && last2);
1828
0dd0e980 1829 /* First ensure that the instructions match. There may be many outgoing
39811184 1830 edges so this test is generally cheaper. */
206604dc 1831 if (old_insns_match_p (mode, last1, last2) != dir_both)
0dd0e980
JH
1832 return false;
1833
1834 /* Search the outgoing edges, ensure that the counts do match, find possible
1835 fallthru and exception handling edges since these needs more
1836 validation. */
628f6a4e
BE
1837 if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
1838 return false;
1839
206604dc 1840 bool nonfakeedges = false;
628f6a4e 1841 FOR_EACH_EDGE (e1, ei, bb1->succs)
0dd0e980 1842 {
628f6a4e 1843 e2 = EDGE_SUCC (bb2, ei.index);
c22cacf3 1844
206604dc
JJ
1845 if ((e1->flags & EDGE_FAKE) == 0)
1846 nonfakeedges = true;
1847
0dd0e980
JH
1848 if (e1->flags & EDGE_EH)
1849 nehedges1++;
5f0d2358 1850
0dd0e980
JH
1851 if (e2->flags & EDGE_EH)
1852 nehedges2++;
5f0d2358 1853
0dd0e980
JH
1854 if (e1->flags & EDGE_FALLTHRU)
1855 fallthru1 = e1;
1856 if (e2->flags & EDGE_FALLTHRU)
1857 fallthru2 = e2;
1858 }
5f0d2358 1859
0dd0e980 1860 /* If number of edges of various types does not match, fail. */
628f6a4e 1861 if (nehedges1 != nehedges2
5f0d2358 1862 || (fallthru1 != 0) != (fallthru2 != 0))
0dd0e980
JH
1863 return false;
1864
206604dc
JJ
1865 /* If !ACCUMULATE_OUTGOING_ARGS, bb1 (and bb2) have no successors
1866 and the last real insn doesn't have REG_ARGS_SIZE note, don't
1867 attempt to optimize, as the two basic blocks might have different
1868 REG_ARGS_SIZE depths. For noreturn calls and unconditional
1869 traps there should be REG_ARG_SIZE notes, they could be missing
1870 for __builtin_unreachable () uses though. */
1871 if (!nonfakeedges
1872 && !ACCUMULATE_OUTGOING_ARGS
1873 && (!INSN_P (last1)
1874 || !find_reg_note (last1, REG_ARGS_SIZE, NULL)))
1875 return false;
1876
0dd0e980
JH
1877 /* fallthru edges must be forwarded to the same destination. */
1878 if (fallthru1)
1879 {
1880 basic_block d1 = (forwarder_block_p (fallthru1->dest)
c5cbcccf 1881 ? single_succ (fallthru1->dest): fallthru1->dest);
0dd0e980 1882 basic_block d2 = (forwarder_block_p (fallthru2->dest)
c5cbcccf 1883 ? single_succ (fallthru2->dest): fallthru2->dest);
5f0d2358 1884
0dd0e980
JH
1885 if (d1 != d2)
1886 return false;
1887 }
5f0d2358 1888
5f77fbd4
JJ
1889 /* Ensure the same EH region. */
1890 {
a813c111
SB
1891 rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
1892 rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
5f0d2358 1893
5f77fbd4
JJ
1894 if (!n1 && n2)
1895 return false;
1896
1897 if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
1898 return false;
1899 }
5f0d2358 1900
38109dab
GL
1901 /* The same checks as in try_crossjump_to_edge. It is required for RTL
1902 version of sequence abstraction. */
1903 FOR_EACH_EDGE (e1, ei, bb2->succs)
1904 {
1905 edge e2;
1906 edge_iterator ei;
1907 basic_block d1 = e1->dest;
1908
1909 if (FORWARDER_BLOCK_P (d1))
1910 d1 = EDGE_SUCC (d1, 0)->dest;
1911
1912 FOR_EACH_EDGE (e2, ei, bb1->succs)
1913 {
1914 basic_block d2 = e2->dest;
1915 if (FORWARDER_BLOCK_P (d2))
1916 d2 = EDGE_SUCC (d2, 0)->dest;
1917 if (d1 == d2)
1918 break;
1919 }
1920
1921 if (!e2)
1922 return false;
1923 }
1924
0dd0e980 1925 return true;
402209ff
JH
1926}
1927
38109dab
GL
1928/* Returns true if BB basic block has a preserve label. */
1929
1930static bool
1931block_has_preserve_label (basic_block bb)
1932{
1933 return (bb
1934 && block_label (bb)
1935 && LABEL_PRESERVE_P (block_label (bb)));
1936}
1937
402209ff
JH
1938/* E1 and E2 are edges with the same destination block. Search their
1939 predecessors for common code. If found, redirect control flow from
bf22920b
TV
1940 (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC (dir_forward),
1941 or the other way around (dir_backward). DIR specifies the allowed
1942 replacement direction. */
402209ff
JH
1943
1944static bool
bf22920b
TV
1945try_crossjump_to_edge (int mode, edge e1, edge e2,
1946 enum replace_direction dir)
402209ff 1947{
c2fc5456 1948 int nmatch;
402209ff 1949 basic_block src1 = e1->src, src2 = e2->src;
39587bb9 1950 basic_block redirect_to, redirect_from, to_remove;
823918ae 1951 basic_block osrc1, osrc2, redirect_edges_to, tmp;
da5477a9 1952 rtx_insn *newpos1, *newpos2;
402209ff 1953 edge s;
628f6a4e 1954 edge_iterator ei;
c2fc5456 1955
da5477a9 1956 newpos1 = newpos2 = NULL;
6de9cd9a 1957
402209ff
JH
1958 /* Search backward through forwarder blocks. We don't need to worry
1959 about multiple entry or chained forwarders, as they will be optimized
1960 away. We do this to look past the unconditional jump following a
1961 conditional jump that is required due to the current CFG shape. */
c5cbcccf 1962 if (single_pred_p (src1)
635559ab 1963 && FORWARDER_BLOCK_P (src1))
c5cbcccf 1964 e1 = single_pred_edge (src1), src1 = e1->src;
5f0d2358 1965
c5cbcccf 1966 if (single_pred_p (src2)
635559ab 1967 && FORWARDER_BLOCK_P (src2))
c5cbcccf 1968 e2 = single_pred_edge (src2), src2 = e2->src;
402209ff
JH
1969
1970 /* Nothing to do if we reach ENTRY, or a common source block. */
fefa31b5
DM
1971 if (src1 == ENTRY_BLOCK_PTR_FOR_FN (cfun) || src2
1972 == ENTRY_BLOCK_PTR_FOR_FN (cfun))
402209ff
JH
1973 return false;
1974 if (src1 == src2)
1975 return false;
1976
1977 /* Seeing more than 1 forwarder blocks would confuse us later... */
635559ab 1978 if (FORWARDER_BLOCK_P (e1->dest)
c5cbcccf 1979 && FORWARDER_BLOCK_P (single_succ (e1->dest)))
402209ff 1980 return false;
5f0d2358 1981
635559ab 1982 if (FORWARDER_BLOCK_P (e2->dest)
c5cbcccf 1983 && FORWARDER_BLOCK_P (single_succ (e2->dest)))
402209ff
JH
1984 return false;
1985
1986 /* Likewise with dead code (possibly newly created by the other optimizations
1987 of cfg_cleanup). */
628f6a4e 1988 if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
402209ff
JH
1989 return false;
1990
ba61fc53
JH
1991 /* Do not turn corssing edge to non-crossing or vice versa after reload. */
1992 if (BB_PARTITION (src1) != BB_PARTITION (src2)
1993 && reload_completed)
1994 return false;
1995
402209ff 1996 /* Look for the common insn sequence, part the first ... */
c2fc5456 1997 if (!outgoing_edges_match (mode, src1, src2))
402209ff
JH
1998 return false;
1999
2000 /* ... and part the second. */
472c95f5 2001 nmatch = flow_find_cross_jump (src1, src2, &newpos1, &newpos2, &dir);
12183e0f 2002
823918ae
TV
2003 osrc1 = src1;
2004 osrc2 = src2;
2005 if (newpos1 != NULL_RTX)
2006 src1 = BLOCK_FOR_INSN (newpos1);
2007 if (newpos2 != NULL_RTX)
2008 src2 = BLOCK_FOR_INSN (newpos2);
2009
dd68669b
JL
2010 /* Check that SRC1 and SRC2 have preds again. They may have changed
2011 above due to the call to flow_find_cross_jump. */
2012 if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
2013 return false;
2014
bf22920b
TV
2015 if (dir == dir_backward)
2016 {
ba61fc53
JH
2017 std::swap (osrc1, osrc2);
2018 std::swap (src1, src2);
2019 std::swap (e1, e2);
2020 std::swap (newpos1, newpos2);
bf22920b
TV
2021 }
2022
12183e0f
PH
2023 /* Don't proceed with the crossjump unless we found a sufficient number
2024 of matching instructions or the 'from' block was totally matched
2025 (such that its predecessors will hopefully be redirected and the
2026 block removed). */
c2fc5456
R
2027 if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
2028 && (newpos1 != BB_HEAD (src1)))
7d22e898 2029 return false;
402209ff 2030
75c40d56 2031 /* Avoid deleting preserve label when redirecting ABNORMAL edges. */
38109dab
GL
2032 if (block_has_preserve_label (e1->dest)
2033 && (e1->flags & EDGE_ABNORMAL))
2034 return false;
2035
39811184
JZ
2036 /* Here we know that the insns in the end of SRC1 which are common with SRC2
2037 will be deleted.
2038 If we have tablejumps in the end of SRC1 and SRC2
2039 they have been already compared for equivalence in outgoing_edges_match ()
2040 so replace the references to TABLE1 by references to TABLE2. */
21c0a521 2041 {
dfe08bc4 2042 rtx_insn *label1, *label2;
8942ee0f 2043 rtx_jump_table_data *table1, *table2;
39811184 2044
823918ae
TV
2045 if (tablejump_p (BB_END (osrc1), &label1, &table1)
2046 && tablejump_p (BB_END (osrc2), &label2, &table2)
39811184
JZ
2047 && label1 != label2)
2048 {
da5477a9 2049 rtx_insn *insn;
39811184
JZ
2050
2051 /* Replace references to LABEL1 with LABEL2. */
39811184
JZ
2052 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2053 {
2054 /* Do not replace the label in SRC1->END because when deleting
2055 a block whose end is a tablejump, the tablejump referenced
2056 from the instruction is deleted too. */
823918ae 2057 if (insn != BB_END (osrc1))
a2b7026c 2058 replace_label_in_insn (insn, label1, label2, true);
39811184
JZ
2059 }
2060 }
21c0a521 2061 }
10d6c0d0 2062
b604fe9b
SB
2063 /* Avoid splitting if possible. We must always split when SRC2 has
2064 EH predecessor edges, or we may end up with basic blocks with both
2065 normal and EH predecessor edges. */
c2fc5456 2066 if (newpos2 == BB_HEAD (src2)
b604fe9b 2067 && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
402209ff
JH
2068 redirect_to = src2;
2069 else
2070 {
c2fc5456 2071 if (newpos2 == BB_HEAD (src2))
b604fe9b
SB
2072 {
2073 /* Skip possible basic block header. */
c2fc5456
R
2074 if (LABEL_P (newpos2))
2075 newpos2 = NEXT_INSN (newpos2);
b5b8b0ac
AO
2076 while (DEBUG_INSN_P (newpos2))
2077 newpos2 = NEXT_INSN (newpos2);
c2fc5456
R
2078 if (NOTE_P (newpos2))
2079 newpos2 = NEXT_INSN (newpos2);
b5b8b0ac
AO
2080 while (DEBUG_INSN_P (newpos2))
2081 newpos2 = NEXT_INSN (newpos2);
b604fe9b
SB
2082 }
2083
c263766c
RH
2084 if (dump_file)
2085 fprintf (dump_file, "Splitting bb %i before %i insns\n",
0b17ab2f 2086 src2->index, nmatch);
c2fc5456 2087 redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
402209ff
JH
2088 }
2089
c263766c 2090 if (dump_file)
c2fc5456
R
2091 fprintf (dump_file,
2092 "Cross jumping from bb %i to bb %i; %i common insns\n",
2093 src1->index, src2->index, nmatch);
402209ff 2094
6fc0bb99 2095 /* We may have some registers visible through the block. */
6fb5fa3c 2096 df_set_bb_dirty (redirect_to);
402209ff 2097
823918ae
TV
2098 if (osrc2 == src2)
2099 redirect_edges_to = redirect_to;
2100 else
2101 redirect_edges_to = osrc2;
2102
e7a74006 2103 /* Recompute the counts of destinations of outgoing edges. */
823918ae 2104 FOR_EACH_EDGE (s, ei, redirect_edges_to->succs)
402209ff
JH
2105 {
2106 edge s2;
628f6a4e 2107 edge_iterator ei;
402209ff
JH
2108 basic_block d = s->dest;
2109
635559ab 2110 if (FORWARDER_BLOCK_P (d))
c5cbcccf 2111 d = single_succ (d);
5f0d2358 2112
628f6a4e 2113 FOR_EACH_EDGE (s2, ei, src1->succs)
402209ff
JH
2114 {
2115 basic_block d2 = s2->dest;
635559ab 2116 if (FORWARDER_BLOCK_P (d2))
c5cbcccf 2117 d2 = single_succ (d2);
402209ff
JH
2118 if (d == d2)
2119 break;
2120 }
5f0d2358 2121
402209ff 2122 /* Take care to update possible forwarder blocks. We verified
c22cacf3
MS
2123 that there is no more than one in the chain, so we can't run
2124 into infinite loop. */
635559ab 2125 if (FORWARDER_BLOCK_P (s->dest))
e7a74006 2126 s->dest->count += s->count ();
5f0d2358 2127
635559ab 2128 if (FORWARDER_BLOCK_P (s2->dest))
e7a74006 2129 s2->dest->count -= s->count ();
5f0d2358 2130
97c07987
JH
2131 s->probability = s->probability.combine_with_count
2132 (redirect_edges_to->count,
2133 s2->probability, src1->count);
402209ff
JH
2134 }
2135
e7a74006 2136 /* Adjust count for the block. An earlier jump
52982a97
EB
2137 threading pass may have left the profile in an inconsistent
2138 state (see update_bb_profile_for_threading) so we must be
2139 prepared for overflows. */
823918ae
TV
2140 tmp = redirect_to;
2141 do
2142 {
2143 tmp->count += src1->count;
823918ae
TV
2144 if (tmp == redirect_edges_to)
2145 break;
2146 tmp = find_fallthru_edge (tmp->succs)->dest;
2147 }
2148 while (true);
2149 update_br_prob_note (redirect_edges_to);
402209ff
JH
2150
2151 /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
2152
c2fc5456
R
2153 /* Skip possible basic block header. */
2154 if (LABEL_P (newpos1))
2155 newpos1 = NEXT_INSN (newpos1);
b5b8b0ac
AO
2156
2157 while (DEBUG_INSN_P (newpos1))
2158 newpos1 = NEXT_INSN (newpos1);
2159
cd9c1ca8 2160 if (NOTE_INSN_BASIC_BLOCK_P (newpos1))
c2fc5456
R
2161 newpos1 = NEXT_INSN (newpos1);
2162
b5b8b0ac
AO
2163 while (DEBUG_INSN_P (newpos1))
2164 newpos1 = NEXT_INSN (newpos1);
2165
c2fc5456 2166 redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
c5cbcccf 2167 to_remove = single_succ (redirect_from);
402209ff 2168
c5cbcccf 2169 redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
f470c378 2170 delete_basic_block (to_remove);
402209ff 2171
39587bb9 2172 update_forwarder_flag (redirect_from);
7cbd12b8
JJ
2173 if (redirect_to != src2)
2174 update_forwarder_flag (src2);
635559ab 2175
402209ff
JH
2176 return true;
2177}
2178
2179/* Search the predecessors of BB for common insn sequences. When found,
2180 share code between them by redirecting control flow. Return true if
2181 any changes made. */
2182
2183static bool
d329e058 2184try_crossjump_bb (int mode, basic_block bb)
402209ff 2185{
628f6a4e 2186 edge e, e2, fallthru;
402209ff 2187 bool changed;
628f6a4e 2188 unsigned max, ix, ix2;
402209ff 2189
f63d1bf7 2190 /* Nothing to do if there is not at least two incoming edges. */
628f6a4e 2191 if (EDGE_COUNT (bb->preds) < 2)
402209ff
JH
2192 return false;
2193
bbcb0c05
SB
2194 /* Don't crossjump if this block ends in a computed jump,
2195 unless we are optimizing for size. */
efd8f750 2196 if (optimize_bb_for_size_p (bb)
fefa31b5 2197 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
bbcb0c05
SB
2198 && computed_jump_p (BB_END (bb)))
2199 return false;
2200
750054a2
CT
2201 /* If we are partitioning hot/cold basic blocks, we don't want to
2202 mess up unconditional or indirect jumps that cross between hot
c22cacf3
MS
2203 and cold sections.
2204
8e8d5162 2205 Basic block partitioning may result in some jumps that appear to
c22cacf3
MS
2206 be optimizable (or blocks that appear to be mergeable), but which really
2207 must be left untouched (they are required to make it safely across
2208 partition boundaries). See the comments at the top of
8e8d5162
CT
2209 bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
2210
c22cacf3
MS
2211 if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
2212 BB_PARTITION (EDGE_PRED (bb, 1)->src)
87c8b4be 2213 || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
750054a2
CT
2214 return false;
2215
402209ff
JH
2216 /* It is always cheapest to redirect a block that ends in a branch to
2217 a block that falls through into BB, as that adds no branches to the
2218 program. We'll try that combination first. */
5f24e0dc
RH
2219 fallthru = NULL;
2220 max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
628f6a4e
BE
2221
2222 if (EDGE_COUNT (bb->preds) > max)
2223 return false;
2224
0fd4b31d 2225 fallthru = find_fallthru_edge (bb->preds);
402209ff
JH
2226
2227 changed = false;
0248bceb 2228 for (ix = 0; ix < EDGE_COUNT (bb->preds);)
402209ff 2229 {
0248bceb 2230 e = EDGE_PRED (bb, ix);
628f6a4e 2231 ix++;
402209ff 2232
c1e3e2d9
SB
2233 /* As noted above, first try with the fallthru predecessor (or, a
2234 fallthru predecessor if we are in cfglayout mode). */
402209ff
JH
2235 if (fallthru)
2236 {
2237 /* Don't combine the fallthru edge into anything else.
2238 If there is a match, we'll do it the other way around. */
2239 if (e == fallthru)
2240 continue;
7cf240d5
JH
2241 /* If nothing changed since the last attempt, there is nothing
2242 we can do. */
2243 if (!first_pass
4ec5d4f5
BS
2244 && !((e->src->flags & BB_MODIFIED)
2245 || (fallthru->src->flags & BB_MODIFIED)))
7cf240d5 2246 continue;
402209ff 2247
bf22920b 2248 if (try_crossjump_to_edge (mode, e, fallthru, dir_forward))
402209ff
JH
2249 {
2250 changed = true;
628f6a4e 2251 ix = 0;
402209ff
JH
2252 continue;
2253 }
2254 }
2255
2256 /* Non-obvious work limiting check: Recognize that we're going
2257 to call try_crossjump_bb on every basic block. So if we have
2258 two blocks with lots of outgoing edges (a switch) and they
2259 share lots of common destinations, then we would do the
2260 cross-jump check once for each common destination.
2261
2262 Now, if the blocks actually are cross-jump candidates, then
2263 all of their destinations will be shared. Which means that
2264 we only need check them for cross-jump candidacy once. We
2265 can eliminate redundant checks of crossjump(A,B) by arbitrarily
2266 choosing to do the check from the block for which the edge
2267 in question is the first successor of A. */
628f6a4e 2268 if (EDGE_SUCC (e->src, 0) != e)
402209ff
JH
2269 continue;
2270
0248bceb 2271 for (ix2 = 0; ix2 < EDGE_COUNT (bb->preds); ix2++)
402209ff 2272 {
0248bceb 2273 e2 = EDGE_PRED (bb, ix2);
402209ff
JH
2274
2275 if (e2 == e)
2276 continue;
2277
2278 /* We've already checked the fallthru edge above. */
2279 if (e2 == fallthru)
2280 continue;
2281
402209ff
JH
2282 /* The "first successor" check above only prevents multiple
2283 checks of crossjump(A,B). In order to prevent redundant
2284 checks of crossjump(B,A), require that A be the block
2285 with the lowest index. */
0b17ab2f 2286 if (e->src->index > e2->src->index)
402209ff
JH
2287 continue;
2288
7cf240d5
JH
2289 /* If nothing changed since the last attempt, there is nothing
2290 we can do. */
2291 if (!first_pass
4ec5d4f5
BS
2292 && !((e->src->flags & BB_MODIFIED)
2293 || (e2->src->flags & BB_MODIFIED)))
7cf240d5
JH
2294 continue;
2295
bf22920b
TV
2296 /* Both e and e2 are not fallthru edges, so we can crossjump in either
2297 direction. */
2298 if (try_crossjump_to_edge (mode, e, e2, dir_both))
402209ff
JH
2299 {
2300 changed = true;
628f6a4e 2301 ix = 0;
402209ff
JH
2302 break;
2303 }
2304 }
2305 }
2306
c1e3e2d9 2307 if (changed)
bd2c6270 2308 crossjumps_occurred = true;
c1e3e2d9 2309
402209ff
JH
2310 return changed;
2311}
2312
4ec5d4f5
BS
2313/* Search the successors of BB for common insn sequences. When found,
2314 share code between them by moving it across the basic block
2315 boundary. Return true if any changes made. */
2316
2317static bool
2318try_head_merge_bb (basic_block bb)
2319{
2320 basic_block final_dest_bb = NULL;
2321 int max_match = INT_MAX;
2322 edge e0;
da5477a9 2323 rtx_insn **headptr, **currptr, **nextptr;
4ec5d4f5
BS
2324 bool changed, moveall;
2325 unsigned ix;
da5477a9 2326 rtx_insn *e0_last_head;
61aa0978
DM
2327 rtx cond;
2328 rtx_insn *move_before;
4ec5d4f5 2329 unsigned nedges = EDGE_COUNT (bb->succs);
da5477a9 2330 rtx_insn *jump = BB_END (bb);
4ec5d4f5
BS
2331 regset live, live_union;
2332
2333 /* Nothing to do if there is not at least two outgoing edges. */
2334 if (nedges < 2)
2335 return false;
2336
2337 /* Don't crossjump if this block ends in a computed jump,
2338 unless we are optimizing for size. */
2339 if (optimize_bb_for_size_p (bb)
fefa31b5 2340 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
4ec5d4f5
BS
2341 && computed_jump_p (BB_END (bb)))
2342 return false;
2343
2344 cond = get_condition (jump, &move_before, true, false);
2345 if (cond == NULL_RTX)
43052d45 2346 {
618f4073 2347 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
43052d45
BS
2348 move_before = prev_nonnote_nondebug_insn (jump);
2349 else
43052d45
BS
2350 move_before = jump;
2351 }
4ec5d4f5
BS
2352
2353 for (ix = 0; ix < nedges; ix++)
fefa31b5 2354 if (EDGE_SUCC (bb, ix)->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
4ec5d4f5
BS
2355 return false;
2356
2357 for (ix = 0; ix < nedges; ix++)
2358 {
2359 edge e = EDGE_SUCC (bb, ix);
2360 basic_block other_bb = e->dest;
2361
2362 if (df_get_bb_dirty (other_bb))
2363 {
2364 block_was_dirty = true;
2365 return false;
2366 }
2367
2368 if (e->flags & EDGE_ABNORMAL)
2369 return false;
2370
2371 /* Normally, all destination blocks must only be reachable from this
2372 block, i.e. they must have one incoming edge.
2373
2374 There is one special case we can handle, that of multiple consecutive
2375 jumps where the first jumps to one of the targets of the second jump.
2376 This happens frequently in switch statements for default labels.
2377 The structure is as follows:
2378 FINAL_DEST_BB
2379 ....
2380 if (cond) jump A;
2381 fall through
2382 BB
2383 jump with targets A, B, C, D...
2384 A
2385 has two incoming edges, from FINAL_DEST_BB and BB
2386
2387 In this case, we can try to move the insns through BB and into
2388 FINAL_DEST_BB. */
2389 if (EDGE_COUNT (other_bb->preds) != 1)
2390 {
2391 edge incoming_edge, incoming_bb_other_edge;
2392 edge_iterator ei;
2393
2394 if (final_dest_bb != NULL
2395 || EDGE_COUNT (other_bb->preds) != 2)
2396 return false;
2397
2398 /* We must be able to move the insns across the whole block. */
2399 move_before = BB_HEAD (bb);
2400 while (!NONDEBUG_INSN_P (move_before))
2401 move_before = NEXT_INSN (move_before);
2402
2403 if (EDGE_COUNT (bb->preds) != 1)
2404 return false;
2405 incoming_edge = EDGE_PRED (bb, 0);
2406 final_dest_bb = incoming_edge->src;
2407 if (EDGE_COUNT (final_dest_bb->succs) != 2)
2408 return false;
2409 FOR_EACH_EDGE (incoming_bb_other_edge, ei, final_dest_bb->succs)
2410 if (incoming_bb_other_edge != incoming_edge)
2411 break;
2412 if (incoming_bb_other_edge->dest != other_bb)
2413 return false;
2414 }
2415 }
2416
2417 e0 = EDGE_SUCC (bb, 0);
da5477a9 2418 e0_last_head = NULL;
4ec5d4f5
BS
2419 changed = false;
2420
2421 for (ix = 1; ix < nedges; ix++)
2422 {
2423 edge e = EDGE_SUCC (bb, ix);
da5477a9 2424 rtx_insn *e0_last, *e_last;
4ec5d4f5
BS
2425 int nmatch;
2426
2427 nmatch = flow_find_head_matching_sequence (e0->dest, e->dest,
2428 &e0_last, &e_last, 0);
2429 if (nmatch == 0)
2430 return false;
2431
2432 if (nmatch < max_match)
2433 {
2434 max_match = nmatch;
2435 e0_last_head = e0_last;
2436 }
2437 }
2438
2439 /* If we matched an entire block, we probably have to avoid moving the
2440 last insn. */
2441 if (max_match > 0
2442 && e0_last_head == BB_END (e0->dest)
2443 && (find_reg_note (e0_last_head, REG_EH_REGION, 0)
2444 || control_flow_insn_p (e0_last_head)))
2445 {
2446 max_match--;
2447 if (max_match == 0)
2448 return false;
ec2d7121 2449 e0_last_head = prev_real_nondebug_insn (e0_last_head);
4ec5d4f5
BS
2450 }
2451
2452 if (max_match == 0)
2453 return false;
2454
2455 /* We must find a union of the live registers at each of the end points. */
2456 live = BITMAP_ALLOC (NULL);
2457 live_union = BITMAP_ALLOC (NULL);
2458
da5477a9
DM
2459 currptr = XNEWVEC (rtx_insn *, nedges);
2460 headptr = XNEWVEC (rtx_insn *, nedges);
2461 nextptr = XNEWVEC (rtx_insn *, nedges);
4ec5d4f5
BS
2462
2463 for (ix = 0; ix < nedges; ix++)
2464 {
2465 int j;
2466 basic_block merge_bb = EDGE_SUCC (bb, ix)->dest;
da5477a9 2467 rtx_insn *head = BB_HEAD (merge_bb);
4ec5d4f5 2468
b59e0455
JJ
2469 while (!NONDEBUG_INSN_P (head))
2470 head = NEXT_INSN (head);
4ec5d4f5
BS
2471 headptr[ix] = head;
2472 currptr[ix] = head;
2473
2474 /* Compute the end point and live information */
2475 for (j = 1; j < max_match; j++)
b59e0455
JJ
2476 do
2477 head = NEXT_INSN (head);
2478 while (!NONDEBUG_INSN_P (head));
4ec5d4f5
BS
2479 simulate_backwards_to_point (merge_bb, live, head);
2480 IOR_REG_SET (live_union, live);
2481 }
2482
2483 /* If we're moving across two blocks, verify the validity of the
2484 first move, then adjust the target and let the loop below deal
2485 with the final move. */
2486 if (final_dest_bb != NULL)
2487 {
61aa0978 2488 rtx_insn *move_upto;
4ec5d4f5
BS
2489
2490 moveall = can_move_insns_across (currptr[0], e0_last_head, move_before,
2491 jump, e0->dest, live_union,
2492 NULL, &move_upto);
2493 if (!moveall)
2494 {
2495 if (move_upto == NULL_RTX)
2496 goto out;
2497
2498 while (e0_last_head != move_upto)
2499 {
2500 df_simulate_one_insn_backwards (e0->dest, e0_last_head,
2501 live_union);
2502 e0_last_head = PREV_INSN (e0_last_head);
2503 }
2504 }
2505 if (e0_last_head == NULL_RTX)
2506 goto out;
2507
2508 jump = BB_END (final_dest_bb);
2509 cond = get_condition (jump, &move_before, true, false);
2510 if (cond == NULL_RTX)
43052d45 2511 {
618f4073 2512 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
43052d45
BS
2513 move_before = prev_nonnote_nondebug_insn (jump);
2514 else
43052d45
BS
2515 move_before = jump;
2516 }
4ec5d4f5
BS
2517 }
2518
2519 do
2520 {
61aa0978 2521 rtx_insn *move_upto;
4ec5d4f5
BS
2522 moveall = can_move_insns_across (currptr[0], e0_last_head,
2523 move_before, jump, e0->dest, live_union,
2524 NULL, &move_upto);
2525 if (!moveall && move_upto == NULL_RTX)
2526 {
2527 if (jump == move_before)
2528 break;
2529
2530 /* Try again, using a different insertion point. */
2531 move_before = jump;
2532
4ec5d4f5
BS
2533 /* Don't try moving before a cc0 user, as that may invalidate
2534 the cc0. */
618f4073 2535 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
4ec5d4f5 2536 break;
4ec5d4f5
BS
2537
2538 continue;
2539 }
2540
2541 if (final_dest_bb && !moveall)
2542 /* We haven't checked whether a partial move would be OK for the first
2543 move, so we have to fail this case. */
2544 break;
2545
2546 changed = true;
2547 for (;;)
2548 {
2549 if (currptr[0] == move_upto)
2550 break;
2551 for (ix = 0; ix < nedges; ix++)
2552 {
da5477a9 2553 rtx_insn *curr = currptr[ix];
4ec5d4f5
BS
2554 do
2555 curr = NEXT_INSN (curr);
2556 while (!NONDEBUG_INSN_P (curr));
2557 currptr[ix] = curr;
2558 }
2559 }
2560
2561 /* If we can't currently move all of the identical insns, remember
2562 each insn after the range that we'll merge. */
2563 if (!moveall)
2564 for (ix = 0; ix < nedges; ix++)
2565 {
da5477a9 2566 rtx_insn *curr = currptr[ix];
4ec5d4f5
BS
2567 do
2568 curr = NEXT_INSN (curr);
2569 while (!NONDEBUG_INSN_P (curr));
2570 nextptr[ix] = curr;
2571 }
2572
2573 reorder_insns (headptr[0], currptr[0], PREV_INSN (move_before));
2574 df_set_bb_dirty (EDGE_SUCC (bb, 0)->dest);
2575 if (final_dest_bb != NULL)
2576 df_set_bb_dirty (final_dest_bb);
2577 df_set_bb_dirty (bb);
2578 for (ix = 1; ix < nedges; ix++)
2579 {
2580 df_set_bb_dirty (EDGE_SUCC (bb, ix)->dest);
2581 delete_insn_chain (headptr[ix], currptr[ix], false);
2582 }
2583 if (!moveall)
2584 {
2585 if (jump == move_before)
2586 break;
2587
2588 /* For the unmerged insns, try a different insertion point. */
2589 move_before = jump;
2590
4ec5d4f5
BS
2591 /* Don't try moving before a cc0 user, as that may invalidate
2592 the cc0. */
618f4073 2593 if (HAVE_cc0 && reg_mentioned_p (cc0_rtx, jump))
4ec5d4f5 2594 break;
4ec5d4f5
BS
2595
2596 for (ix = 0; ix < nedges; ix++)
2597 currptr[ix] = headptr[ix] = nextptr[ix];
2598 }
2599 }
2600 while (!moveall);
2601
2602 out:
2603 free (currptr);
2604 free (headptr);
2605 free (nextptr);
2606
bd2c6270 2607 crossjumps_occurred |= changed;
4ec5d4f5
BS
2608
2609 return changed;
2610}
2611
7752e522
JJ
2612/* Return true if BB contains just bb note, or bb note followed
2613 by only DEBUG_INSNs. */
2614
2615static bool
2616trivially_empty_bb_p (basic_block bb)
2617{
da5477a9 2618 rtx_insn *insn = BB_END (bb);
7752e522
JJ
2619
2620 while (1)
2621 {
2622 if (insn == BB_HEAD (bb))
2623 return true;
2624 if (!DEBUG_INSN_P (insn))
2625 return false;
2626 insn = PREV_INSN (insn);
2627 }
2628}
2629
45676a7c
SB
2630/* Return true if BB contains just a return and possibly a USE of the
2631 return value. Fill in *RET and *USE with the return and use insns
2ea0d750 2632 if any found, otherwise NULL. All CLOBBERs are ignored. */
45676a7c
SB
2633
2634static bool
2635bb_is_just_return (basic_block bb, rtx_insn **ret, rtx_insn **use)
2636{
2637 *ret = *use = NULL;
2638 rtx_insn *insn;
2639
2640 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
2641 return false;
2642
2643 FOR_BB_INSNS (bb, insn)
2644 if (NONDEBUG_INSN_P (insn))
2645 {
2ea0d750
SB
2646 rtx pat = PATTERN (insn);
2647
2648 if (!*ret && ANY_RETURN_P (pat))
45676a7c 2649 *ret = insn;
2ea0d750
SB
2650 else if (!*ret && !*use && GET_CODE (pat) == USE
2651 && REG_P (XEXP (pat, 0))
2652 && REG_FUNCTION_VALUE_P (XEXP (pat, 0)))
45676a7c 2653 *use = insn;
2ea0d750 2654 else if (GET_CODE (pat) != CLOBBER)
45676a7c
SB
2655 return false;
2656 }
2657
2658 return !!*ret;
2659}
2660
402209ff
JH
2661/* Do simple CFG optimizations - basic block merging, simplifying of jump
2662 instructions etc. Return nonzero if changes were made. */
2663
2664static bool
d329e058 2665try_optimize_cfg (int mode)
402209ff 2666{
402209ff
JH
2667 bool changed_overall = false;
2668 bool changed;
2669 int iterations = 0;
ec3ae3da 2670 basic_block bb, b, next;
402209ff 2671
6fb5fa3c 2672 if (mode & (CLEANUP_CROSSJUMP | CLEANUP_THREADING))
38c1593d
JH
2673 clear_bb_flags ();
2674
bd2c6270 2675 crossjumps_occurred = false;
c1e3e2d9 2676
11cd3bed 2677 FOR_EACH_BB_FN (bb, cfun)
2dd2d53e
SB
2678 update_forwarder_flag (bb);
2679
245f1bfa 2680 if (! targetm.cannot_modify_jumps_p ())
402209ff 2681 {
7cf240d5 2682 first_pass = true;
e4ec2cac
AO
2683 /* Attempt to merge blocks as made possible by edge removal. If
2684 a block has only one successor, and the successor has only
2685 one predecessor, they may be combined. */
2686 do
402209ff 2687 {
4ec5d4f5 2688 block_was_dirty = false;
e4ec2cac
AO
2689 changed = false;
2690 iterations++;
2691
c263766c
RH
2692 if (dump_file)
2693 fprintf (dump_file,
e4ec2cac
AO
2694 "\n\ntry_optimize_cfg iteration %i\n\n",
2695 iterations);
402209ff 2696
fefa31b5
DM
2697 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
2698 != EXIT_BLOCK_PTR_FOR_FN (cfun);)
402209ff 2699 {
e0082a72 2700 basic_block c;
e4ec2cac
AO
2701 edge s;
2702 bool changed_here = false;
5f0d2358 2703
468059bc
DD
2704 /* Delete trivially dead basic blocks. This is either
2705 blocks with no predecessors, or empty blocks with no
1e211590
DD
2706 successors. However if the empty block with no
2707 successors is the successor of the ENTRY_BLOCK, it is
2708 kept. This ensures that the ENTRY_BLOCK will have a
2709 successor which is a precondition for many RTL
2710 passes. Empty blocks may result from expanding
468059bc
DD
2711 __builtin_unreachable (). */
2712 if (EDGE_COUNT (b->preds) == 0
1e211590 2713 || (EDGE_COUNT (b->succs) == 0
7752e522 2714 && trivially_empty_bb_p (b)
fefa31b5
DM
2715 && single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->dest
2716 != b))
e4ec2cac 2717 {
f6366fc7 2718 c = b->prev_bb;
f1de5107 2719 if (EDGE_COUNT (b->preds) > 0)
3b5fda81
JJ
2720 {
2721 edge e;
2722 edge_iterator ei;
2723
f1de5107
JJ
2724 if (current_ir_type () == IR_RTL_CFGLAYOUT)
2725 {
c758576a
JJ
2726 rtx_insn *insn;
2727 for (insn = BB_FOOTER (b);
2728 insn; insn = NEXT_INSN (insn))
2729 if (BARRIER_P (insn))
2730 break;
2731 if (insn)
f1de5107 2732 FOR_EACH_EDGE (e, ei, b->preds)
c758576a 2733 if ((e->flags & EDGE_FALLTHRU))
f1de5107 2734 {
c758576a
JJ
2735 if (BB_FOOTER (b)
2736 && BB_FOOTER (e->src) == NULL)
f1de5107 2737 {
d8ce2eae
DM
2738 BB_FOOTER (e->src) = BB_FOOTER (b);
2739 BB_FOOTER (b) = NULL;
f1de5107
JJ
2740 }
2741 else
c758576a 2742 emit_barrier_after_bb (e->src);
f1de5107
JJ
2743 }
2744 }
2745 else
2746 {
da5477a9 2747 rtx_insn *last = get_last_bb_insn (b);
f1de5107
JJ
2748 if (last && BARRIER_P (last))
2749 FOR_EACH_EDGE (e, ei, b->preds)
2750 if ((e->flags & EDGE_FALLTHRU))
2751 emit_barrier_after (BB_END (e->src));
2752 }
3b5fda81 2753 }
f470c378 2754 delete_basic_block (b);
bef16e87 2755 changed = true;
6626665f 2756 /* Avoid trying to remove the exit block. */
fefa31b5 2757 b = (c == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? c->next_bb : c);
83bd032b 2758 continue;
e4ec2cac 2759 }
402209ff 2760
6ce2bcb7 2761 /* Remove code labels no longer used. */
c5cbcccf
ZD
2762 if (single_pred_p (b)
2763 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
2764 && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
4b4bf941 2765 && LABEL_P (BB_HEAD (b))
6c979aa1 2766 && !LABEL_PRESERVE_P (BB_HEAD (b))
e4ec2cac
AO
2767 /* If the previous block ends with a branch to this
2768 block, we can't delete the label. Normally this
2769 is a condjump that is yet to be simplified, but
2770 if CASE_DROPS_THRU, this can be a tablejump with
2771 some element going to the same place as the
2772 default (fallthru). */
fefa31b5 2773 && (single_pred (b) == ENTRY_BLOCK_PTR_FOR_FN (cfun)
c5cbcccf 2774 || !JUMP_P (BB_END (single_pred (b)))
a813c111 2775 || ! label_is_jump_target_p (BB_HEAD (b),
c5cbcccf 2776 BB_END (single_pred (b)))))
e4ec2cac 2777 {
03fbe718 2778 delete_insn (BB_HEAD (b));
c263766c
RH
2779 if (dump_file)
2780 fprintf (dump_file, "Deleted label in block %i.\n",
0b17ab2f 2781 b->index);
e4ec2cac 2782 }
402209ff 2783
e4ec2cac 2784 /* If we fall through an empty block, we can remove it. */
9be94227 2785 if (!(mode & (CLEANUP_CFGLAYOUT | CLEANUP_NO_INSN_DEL))
c5cbcccf
ZD
2786 && single_pred_p (b)
2787 && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
4b4bf941 2788 && !LABEL_P (BB_HEAD (b))
e4ec2cac
AO
2789 && FORWARDER_BLOCK_P (b)
2790 /* Note that forwarder_block_p true ensures that
2791 there is a successor for this block. */
c5cbcccf 2792 && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
0cae8d31 2793 && n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS + 1)
e4ec2cac 2794 {
c263766c
RH
2795 if (dump_file)
2796 fprintf (dump_file,
e4ec2cac 2797 "Deleting fallthru block %i.\n",
0b17ab2f 2798 b->index);
e4ec2cac 2799
fefa31b5
DM
2800 c = ((b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2801 ? b->next_bb : b->prev_bb);
c5cbcccf
ZD
2802 redirect_edge_succ_nodup (single_pred_edge (b),
2803 single_succ (b));
f470c378 2804 delete_basic_block (b);
e4ec2cac
AO
2805 changed = true;
2806 b = c;
1e211590 2807 continue;
e4ec2cac 2808 }
5f0d2358 2809
50a36e42 2810 /* Merge B with its single successor, if any. */
c5cbcccf
ZD
2811 if (single_succ_p (b)
2812 && (s = single_succ_edge (b))
ec3ae3da 2813 && !(s->flags & EDGE_COMPLEX)
fefa31b5 2814 && (c = s->dest) != EXIT_BLOCK_PTR_FOR_FN (cfun)
c5cbcccf 2815 && single_pred_p (c)
bc35512f
JH
2816 && b != c)
2817 {
2818 /* When not in cfg_layout mode use code aware of reordering
2819 INSN. This code possibly creates new basic blocks so it
2820 does not fit merge_blocks interface and is kept here in
2821 hope that it will become useless once more of compiler
2822 is transformed to use cfg_layout mode. */
c22cacf3 2823
bc35512f
JH
2824 if ((mode & CLEANUP_CFGLAYOUT)
2825 && can_merge_blocks_p (b, c))
2826 {
2827 merge_blocks (b, c);
2828 update_forwarder_flag (b);
2829 changed_here = true;
2830 }
2831 else if (!(mode & CLEANUP_CFGLAYOUT)
2832 /* If the jump insn has side effects,
2833 we can't kill the edge. */
4b4bf941 2834 && (!JUMP_P (BB_END (b))
e24e7211 2835 || (reload_completed
a813c111 2836 ? simplejump_p (BB_END (b))
e4efa971
JH
2837 : (onlyjump_p (BB_END (b))
2838 && !tablejump_p (BB_END (b),
2839 NULL, NULL))))
bc35512f
JH
2840 && (next = merge_blocks_move (s, b, c, mode)))
2841 {
2842 b = next;
2843 changed_here = true;
2844 }
ec3ae3da 2845 }
e4ec2cac 2846
45676a7c
SB
2847 /* Try to change a branch to a return to just that return. */
2848 rtx_insn *ret, *use;
2849 if (single_succ_p (b)
2850 && onlyjump_p (BB_END (b))
2851 && bb_is_just_return (single_succ (b), &ret, &use))
2852 {
2853 if (redirect_jump (as_a <rtx_jump_insn *> (BB_END (b)),
2854 PATTERN (ret), 0))
2855 {
2856 if (use)
2857 emit_insn_before (copy_insn (PATTERN (use)),
2858 BB_END (b));
2859 if (dump_file)
2860 fprintf (dump_file, "Changed jump %d->%d to return.\n",
2861 b->index, single_succ (b)->index);
2862 redirect_edge_succ (single_succ_edge (b),
2863 EXIT_BLOCK_PTR_FOR_FN (cfun));
2864 single_succ_edge (b)->flags &= ~EDGE_CROSSING;
2865 changed_here = true;
2866 }
2867 }
2868
2869 /* Try to change a conditional branch to a return to the
2870 respective conditional return. */
2871 if (EDGE_COUNT (b->succs) == 2
2872 && any_condjump_p (BB_END (b))
2873 && bb_is_just_return (BRANCH_EDGE (b)->dest, &ret, &use))
2874 {
2875 if (redirect_jump (as_a <rtx_jump_insn *> (BB_END (b)),
2876 PATTERN (ret), 0))
2877 {
2878 if (use)
2879 emit_insn_before (copy_insn (PATTERN (use)),
2880 BB_END (b));
2881 if (dump_file)
2882 fprintf (dump_file, "Changed conditional jump %d->%d "
2883 "to conditional return.\n",
2884 b->index, BRANCH_EDGE (b)->dest->index);
2885 redirect_edge_succ (BRANCH_EDGE (b),
2886 EXIT_BLOCK_PTR_FOR_FN (cfun));
2887 BRANCH_EDGE (b)->flags &= ~EDGE_CROSSING;
2888 changed_here = true;
2889 }
2890 }
2891
2892 /* Try to flip a conditional branch that falls through to
2893 a return so that it becomes a conditional return and a
2894 new jump to the original branch target. */
2895 if (EDGE_COUNT (b->succs) == 2
ac2a4c0d 2896 && BRANCH_EDGE (b)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
45676a7c
SB
2897 && any_condjump_p (BB_END (b))
2898 && bb_is_just_return (FALLTHRU_EDGE (b)->dest, &ret, &use))
2899 {
2900 if (invert_jump (as_a <rtx_jump_insn *> (BB_END (b)),
2901 JUMP_LABEL (BB_END (b)), 0))
2902 {
2903 basic_block new_ft = BRANCH_EDGE (b)->dest;
2904 if (redirect_jump (as_a <rtx_jump_insn *> (BB_END (b)),
2905 PATTERN (ret), 0))
2906 {
2907 if (use)
2908 emit_insn_before (copy_insn (PATTERN (use)),
2909 BB_END (b));
2910 if (dump_file)
2911 fprintf (dump_file, "Changed conditional jump "
2912 "%d->%d to conditional return, adding "
2913 "fall-through jump.\n",
2914 b->index, BRANCH_EDGE (b)->dest->index);
2915 redirect_edge_succ (BRANCH_EDGE (b),
2916 EXIT_BLOCK_PTR_FOR_FN (cfun));
2917 BRANCH_EDGE (b)->flags &= ~EDGE_CROSSING;
2918 std::swap (BRANCH_EDGE (b)->probability,
2919 FALLTHRU_EDGE (b)->probability);
2920 update_br_prob_note (b);
2921 basic_block jb = force_nonfallthru (FALLTHRU_EDGE (b));
2922 notice_new_block (jb);
2923 if (!redirect_jump (as_a <rtx_jump_insn *> (BB_END (jb)),
2924 block_label (new_ft), 0))
2925 gcc_unreachable ();
2926 redirect_edge_succ (single_succ_edge (jb), new_ft);
2927 changed_here = true;
2928 }
2929 else
2930 {
2931 /* Invert the jump back to what it was. This should
2932 never fail. */
2933 if (!invert_jump (as_a <rtx_jump_insn *> (BB_END (b)),
2934 JUMP_LABEL (BB_END (b)), 0))
2935 gcc_unreachable ();
2936 }
2937 }
2938 }
2939
e4ec2cac 2940 /* Simplify branch over branch. */
bc35512f
JH
2941 if ((mode & CLEANUP_EXPENSIVE)
2942 && !(mode & CLEANUP_CFGLAYOUT)
2943 && try_simplify_condjump (b))
38c1593d 2944 changed_here = true;
402209ff 2945
e4ec2cac
AO
2946 /* If B has a single outgoing edge, but uses a
2947 non-trivial jump instruction without side-effects, we
2948 can either delete the jump entirely, or replace it
3348b696 2949 with a simple unconditional jump. */
c5cbcccf 2950 if (single_succ_p (b)
fefa31b5 2951 && single_succ (b) != EXIT_BLOCK_PTR_FOR_FN (cfun)
a813c111 2952 && onlyjump_p (BB_END (b))
339ba33b 2953 && !CROSSING_JUMP_P (BB_END (b))
c5cbcccf
ZD
2954 && try_redirect_by_replacing_jump (single_succ_edge (b),
2955 single_succ (b),
20b4e8ae 2956 (mode & CLEANUP_CFGLAYOUT) != 0))
e4ec2cac 2957 {
e4ec2cac
AO
2958 update_forwarder_flag (b);
2959 changed_here = true;
2960 }
402209ff 2961
e4ec2cac
AO
2962 /* Simplify branch to branch. */
2963 if (try_forward_edges (mode, b))
afe8b6ec
EB
2964 {
2965 update_forwarder_flag (b);
2966 changed_here = true;
2967 }
402209ff 2968
e4ec2cac
AO
2969 /* Look for shared code between blocks. */
2970 if ((mode & CLEANUP_CROSSJUMP)
2971 && try_crossjump_bb (mode, b))
2972 changed_here = true;
402209ff 2973
4ec5d4f5
BS
2974 if ((mode & CLEANUP_CROSSJUMP)
2975 /* This can lengthen register lifetimes. Do it only after
2976 reload. */
2977 && reload_completed
2978 && try_head_merge_bb (b))
2979 changed_here = true;
2980
e4ec2cac
AO
2981 /* Don't get confused by the index shift caused by
2982 deleting blocks. */
2983 if (!changed_here)
e0082a72 2984 b = b->next_bb;
e4ec2cac
AO
2985 else
2986 changed = true;
2987 }
402209ff 2988
e4ec2cac 2989 if ((mode & CLEANUP_CROSSJUMP)
fefa31b5 2990 && try_crossjump_bb (mode, EXIT_BLOCK_PTR_FOR_FN (cfun)))
402209ff 2991 changed = true;
402209ff 2992
4ec5d4f5
BS
2993 if (block_was_dirty)
2994 {
2995 /* This should only be set by head-merging. */
2996 gcc_assert (mode & CLEANUP_CROSSJUMP);
2997 df_analyze ();
2998 }
2999
e4ec2cac 3000 if (changed)
600b5b1d
TJ
3001 {
3002 /* Edge forwarding in particular can cause hot blocks previously
3003 reached by both hot and cold blocks to become dominated only
3004 by cold blocks. This will cause the verification below to fail,
3005 and lead to now cold code in the hot section. This is not easy
3006 to detect and fix during edge forwarding, and in some cases
3007 is only visible after newly unreachable blocks are deleted,
3008 which will be done in fixup_partitions. */
3ff0dc17 3009 if ((mode & CLEANUP_NO_PARTITIONING) == 0)
3211aea2
JH
3010 {
3011 fixup_partitions ();
3012 checking_verify_flow_info ();
3013 }
600b5b1d 3014 }
402209ff 3015
e4ec2cac 3016 changed_overall |= changed;
7cf240d5 3017 first_pass = false;
e4ec2cac
AO
3018 }
3019 while (changed);
402209ff 3020 }
ca6c03ca 3021
04a90bec 3022 FOR_ALL_BB_FN (b, cfun)
2dd2d53e 3023 b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
635559ab 3024
402209ff
JH
3025 return changed_overall;
3026}
3027\f
6d2f8887 3028/* Delete all unreachable basic blocks. */
4262e623 3029
969d70ca 3030bool
d329e058 3031delete_unreachable_blocks (void)
402209ff 3032{
402209ff 3033 bool changed = false;
b5b8b0ac 3034 basic_block b, prev_bb;
402209ff
JH
3035
3036 find_unreachable_blocks ();
3037
65f4b875
AO
3038 /* When we're in GIMPLE mode and there may be debug bind insns, we
3039 should delete blocks in reverse dominator order, so as to get a
3040 chance to substitute all released DEFs into debug bind stmts. If
3041 we don't have dominators information, walking blocks backward
3042 gets us a better chance of retaining most debug information than
b5b8b0ac 3043 otherwise. */
65f4b875 3044 if (MAY_HAVE_DEBUG_BIND_INSNS && current_ir_type () == IR_GIMPLE
b5b8b0ac 3045 && dom_info_available_p (CDI_DOMINATORS))
402209ff 3046 {
fefa31b5
DM
3047 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
3048 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
b5b8b0ac
AO
3049 {
3050 prev_bb = b->prev_bb;
3051
3052 if (!(b->flags & BB_REACHABLE))
3053 {
3054 /* Speed up the removal of blocks that don't dominate
3055 others. Walking backwards, this should be the common
3056 case. */
3057 if (!first_dom_son (CDI_DOMINATORS, b))
3058 delete_basic_block (b);
3059 else
3060 {
9771b263 3061 vec<basic_block> h
b5b8b0ac
AO
3062 = get_all_dominated_blocks (CDI_DOMINATORS, b);
3063
9771b263 3064 while (h.length ())
b5b8b0ac 3065 {
9771b263 3066 b = h.pop ();
b5b8b0ac
AO
3067
3068 prev_bb = b->prev_bb;
0b17ab2f 3069
b5b8b0ac
AO
3070 gcc_assert (!(b->flags & BB_REACHABLE));
3071
3072 delete_basic_block (b);
3073 }
3074
9771b263 3075 h.release ();
b5b8b0ac
AO
3076 }
3077
3078 changed = true;
3079 }
3080 }
3081 }
3082 else
3083 {
fefa31b5
DM
3084 for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
3085 b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
6a58eee9 3086 {
b5b8b0ac
AO
3087 prev_bb = b->prev_bb;
3088
3089 if (!(b->flags & BB_REACHABLE))
3090 {
3091 delete_basic_block (b);
3092 changed = true;
3093 }
6a58eee9 3094 }
402209ff
JH
3095 }
3096
3097 if (changed)
3098 tidy_fallthru_edges ();
3099 return changed;
3100}
6fb5fa3c
DB
3101
3102/* Delete any jump tables never referenced. We can't delete them at the
29f3fd5b
SB
3103 time of removing tablejump insn as they are referenced by the preceding
3104 insns computing the destination, so we delay deleting and garbagecollect
3105 them once life information is computed. */
6fb5fa3c
DB
3106void
3107delete_dead_jumptables (void)
3108{
3109 basic_block bb;
3110
29f3fd5b
SB
3111 /* A dead jump table does not belong to any basic block. Scan insns
3112 between two adjacent basic blocks. */
11cd3bed 3113 FOR_EACH_BB_FN (bb, cfun)
6fb5fa3c 3114 {
da5477a9 3115 rtx_insn *insn, *next;
29f3fd5b
SB
3116
3117 for (insn = NEXT_INSN (BB_END (bb));
3118 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
3119 insn = next)
57d6c446 3120 {
29f3fd5b
SB
3121 next = NEXT_INSN (insn);
3122 if (LABEL_P (insn)
3123 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
3124 && JUMP_TABLE_DATA_P (next))
3125 {
da5477a9 3126 rtx_insn *label = insn, *jump = next;
29f3fd5b
SB
3127
3128 if (dump_file)
3129 fprintf (dump_file, "Dead jumptable %i removed\n",
3130 INSN_UID (insn));
3131
3132 next = NEXT_INSN (next);
3133 delete_insn (jump);
3134 delete_insn (label);
3135 }
6fb5fa3c
DB
3136 }
3137 }
3138}
3139
402209ff
JH
3140\f
3141/* Tidy the CFG by deleting unreachable code and whatnot. */
3142
3143bool
d329e058 3144cleanup_cfg (int mode)
402209ff 3145{
402209ff
JH
3146 bool changed = false;
3147
aeceeb06
SB
3148 /* Set the cfglayout mode flag here. We could update all the callers
3149 but that is just inconvenient, especially given that we eventually
3150 want to have cfglayout mode as the default. */
3151 if (current_ir_type () == IR_RTL_CFGLAYOUT)
3152 mode |= CLEANUP_CFGLAYOUT;
3153
402209ff 3154 timevar_push (TV_CLEANUP_CFG);
3dec4024
JH
3155 if (delete_unreachable_blocks ())
3156 {
3157 changed = true;
3158 /* We've possibly created trivially dead code. Cleanup it right
95bd1dd7 3159 now to introduce more opportunities for try_optimize_cfg. */
6fb5fa3c 3160 if (!(mode & (CLEANUP_NO_INSN_DEL))
3dec4024 3161 && !reload_completed)
62e5bf5d 3162 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3dec4024 3163 }
bf77398c
ZD
3164
3165 compact_blocks ();
3166
c1e3e2d9
SB
3167 /* To tail-merge blocks ending in the same noreturn function (e.g.
3168 a call to abort) we have to insert fake edges to exit. Do this
3169 here once. The fake edges do not interfere with any other CFG
3170 cleanups. */
3171 if (mode & CLEANUP_CROSSJUMP)
3172 add_noreturn_fake_exit_edges ();
3173
7d817ebc
DE
3174 if (!dbg_cnt (cfg_cleanup))
3175 return changed;
3176
3dec4024
JH
3177 while (try_optimize_cfg (mode))
3178 {
3179 delete_unreachable_blocks (), changed = true;
c1e3e2d9 3180 if (!(mode & CLEANUP_NO_INSN_DEL))
3dec4024 3181 {
c1e3e2d9
SB
3182 /* Try to remove some trivially dead insns when doing an expensive
3183 cleanup. But delete_trivially_dead_insns doesn't work after
3184 reload (it only handles pseudos) and run_fast_dce is too costly
3185 to run in every iteration.
3186
3187 For effective cross jumping, we really want to run a fast DCE to
3188 clean up any dead conditions, or they get in the way of performing
3189 useful tail merges.
3190
3191 Other transformations in cleanup_cfg are not so sensitive to dead
3192 code, so delete_trivially_dead_insns or even doing nothing at all
3193 is good enough. */
3194 if ((mode & CLEANUP_EXPENSIVE) && !reload_completed
3195 && !delete_trivially_dead_insns (get_insns (), max_reg_num ()))
3dec4024 3196 break;
bd2c6270 3197 if ((mode & CLEANUP_CROSSJUMP) && crossjumps_occurred)
f1f10541
RS
3198 {
3199 run_fast_dce ();
3200 mode &= ~CLEANUP_FORCE_FAST_DCE;
3201 }
3dec4024
JH
3202 }
3203 else
3204 break;
3dec4024 3205 }
402209ff 3206
c1e3e2d9
SB
3207 if (mode & CLEANUP_CROSSJUMP)
3208 remove_fake_exit_edges ();
3209
f1f10541
RS
3210 if (mode & CLEANUP_FORCE_FAST_DCE)
3211 run_fast_dce ();
3212
29f3fd5b
SB
3213 /* Don't call delete_dead_jumptables in cfglayout mode, because
3214 that function assumes that jump tables are in the insns stream.
3215 But we also don't _have_ to delete dead jumptables in cfglayout
3216 mode because we shouldn't even be looking at things that are
3217 not in a basic block. Dead jumptables are cleaned up when
3218 going out of cfglayout mode. */
3219 if (!(mode & CLEANUP_CFGLAYOUT))
6fb5fa3c
DB
3220 delete_dead_jumptables ();
3221
7d776ee2
RG
3222 /* ??? We probably do this way too often. */
3223 if (current_loops
3224 && (changed
3225 || (mode & CLEANUP_CFG_CHANGED)))
3226 {
7d776ee2
RG
3227 timevar_push (TV_REPAIR_LOOPS);
3228 /* The above doesn't preserve dominance info if available. */
3229 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
3230 calculate_dominance_info (CDI_DOMINATORS);
01cb1ef5 3231 fix_loop_structure (NULL);
7d776ee2
RG
3232 free_dominance_info (CDI_DOMINATORS);
3233 timevar_pop (TV_REPAIR_LOOPS);
3234 }
3235
402209ff
JH
3236 timevar_pop (TV_CLEANUP_CFG);
3237
402209ff
JH
3238 return changed;
3239}
ef330312 3240\f
27a4cd48
DM
3241namespace {
3242
3243const pass_data pass_data_jump =
11a687e7 3244{
27a4cd48
DM
3245 RTL_PASS, /* type */
3246 "jump", /* name */
3247 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
3248 TV_JUMP, /* tv_id */
3249 0, /* properties_required */
3250 0, /* properties_provided */
3251 0, /* properties_destroyed */
3252 0, /* todo_flags_start */
3bea341f 3253 0, /* todo_flags_finish */
11a687e7 3254};
27a4cd48
DM
3255
3256class pass_jump : public rtl_opt_pass
3257{
3258public:
c3284718
RS
3259 pass_jump (gcc::context *ctxt)
3260 : rtl_opt_pass (pass_data_jump, ctxt)
27a4cd48
DM
3261 {}
3262
3263 /* opt_pass methods: */
be55bfe6 3264 virtual unsigned int execute (function *);
27a4cd48
DM
3265
3266}; // class pass_jump
3267
be55bfe6
TS
3268unsigned int
3269pass_jump::execute (function *)
3270{
3271 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3272 if (dump_file)
3273 dump_flow_info (dump_file, dump_flags);
3274 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
3275 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
3276 return 0;
3277}
3278
27a4cd48
DM
3279} // anon namespace
3280
3281rtl_opt_pass *
3282make_pass_jump (gcc::context *ctxt)
3283{
3284 return new pass_jump (ctxt);
3285}
11a687e7 3286\f
27a4cd48
DM
3287namespace {
3288
98fe146e
IL
3289const pass_data pass_data_postreload_jump =
3290{
3291 RTL_PASS, /* type */
3292 "postreload_jump", /* name */
3293 OPTGROUP_NONE, /* optinfo_flags */
3294 TV_JUMP, /* tv_id */
3295 0, /* properties_required */
3296 0, /* properties_provided */
3297 0, /* properties_destroyed */
3298 0, /* todo_flags_start */
3299 0, /* todo_flags_finish */
3300};
3301
3302class pass_postreload_jump : public rtl_opt_pass
3303{
3304public:
3305 pass_postreload_jump (gcc::context *ctxt)
3306 : rtl_opt_pass (pass_data_postreload_jump, ctxt)
3307 {}
3308
3309 /* opt_pass methods: */
3310 virtual unsigned int execute (function *);
3311
3312}; // class pass_postreload_jump
3313
3314unsigned int
3315pass_postreload_jump::execute (function *)
3316{
3317 cleanup_cfg (flag_thread_jumps ? CLEANUP_THREADING : 0);
3318 return 0;
3319}
3320
3321} // anon namespace
3322
3323rtl_opt_pass *
3324make_pass_postreload_jump (gcc::context *ctxt)
3325{
3326 return new pass_postreload_jump (ctxt);
3327}
3328
3329namespace {
3330
27a4cd48 3331const pass_data pass_data_jump2 =
ef330312 3332{
27a4cd48
DM
3333 RTL_PASS, /* type */
3334 "jump2", /* name */
3335 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
3336 TV_JUMP, /* tv_id */
3337 0, /* properties_required */
3338 0, /* properties_provided */
3339 0, /* properties_destroyed */
3340 0, /* todo_flags_start */
3bea341f 3341 0, /* todo_flags_finish */
ef330312 3342};
27a4cd48
DM
3343
3344class pass_jump2 : public rtl_opt_pass
3345{
3346public:
c3284718
RS
3347 pass_jump2 (gcc::context *ctxt)
3348 : rtl_opt_pass (pass_data_jump2, ctxt)
27a4cd48
DM
3349 {}
3350
3351 /* opt_pass methods: */
be55bfe6
TS
3352 virtual unsigned int execute (function *)
3353 {
3354 cleanup_cfg (flag_crossjumping ? CLEANUP_CROSSJUMP : 0);
3355 return 0;
3356 }
27a4cd48
DM
3357
3358}; // class pass_jump2
3359
3360} // anon namespace
3361
3362rtl_opt_pass *
3363make_pass_jump2 (gcc::context *ctxt)
3364{
3365 return new pass_jump2 (ctxt);
3366}