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