2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-ssa-tail-merge.c
blob317fe4c997def15f81818359329bb19b7c41a2c8
1 /* Tail merging for gimple.
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 Contributed by Tom de Vries (tom@codesourcery.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Pass overview.
24 MOTIVATIONAL EXAMPLE
26 gimple representation of gcc/testsuite/gcc.dg/pr43864.c at
28 hprofStartupp (charD.1 * outputFileNameD.2600, charD.1 * ctxD.2601)
30 struct FILED.1638 * fpD.2605;
31 charD.1 fileNameD.2604[1000];
32 intD.0 D.3915;
33 const charD.1 * restrict outputFileName.0D.3914;
35 # BLOCK 2 freq:10000
36 # PRED: ENTRY [100.0%] (fallthru,exec)
37 # PT = nonlocal { D.3926 } (restr)
38 outputFileName.0D.3914_3
39 = (const charD.1 * restrict) outputFileNameD.2600_2(D);
40 # .MEMD.3923_13 = VDEF <.MEMD.3923_12(D)>
41 # USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
42 # CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
43 sprintfD.759 (&fileNameD.2604, outputFileName.0D.3914_3);
44 # .MEMD.3923_14 = VDEF <.MEMD.3923_13>
45 # USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
46 # CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
47 D.3915_4 = accessD.2606 (&fileNameD.2604, 1);
48 if (D.3915_4 == 0)
49 goto <bb 3>;
50 else
51 goto <bb 4>;
52 # SUCC: 3 [10.0%] (true,exec) 4 [90.0%] (false,exec)
54 # BLOCK 3 freq:1000
55 # PRED: 2 [10.0%] (true,exec)
56 # .MEMD.3923_15 = VDEF <.MEMD.3923_14>
57 # USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
58 # CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
59 freeD.898 (ctxD.2601_5(D));
60 goto <bb 7>;
61 # SUCC: 7 [100.0%] (fallthru,exec)
63 # BLOCK 4 freq:9000
64 # PRED: 2 [90.0%] (false,exec)
65 # .MEMD.3923_16 = VDEF <.MEMD.3923_14>
66 # PT = nonlocal escaped
67 # USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
68 # CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
69 fpD.2605_8 = fopenD.1805 (&fileNameD.2604[0], 0B);
70 if (fpD.2605_8 == 0B)
71 goto <bb 5>;
72 else
73 goto <bb 6>;
74 # SUCC: 5 [1.9%] (true,exec) 6 [98.1%] (false,exec)
76 # BLOCK 5 freq:173
77 # PRED: 4 [1.9%] (true,exec)
78 # .MEMD.3923_17 = VDEF <.MEMD.3923_16>
79 # USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
80 # CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
81 freeD.898 (ctxD.2601_5(D));
82 goto <bb 7>;
83 # SUCC: 7 [100.0%] (fallthru,exec)
85 # BLOCK 6 freq:8827
86 # PRED: 4 [98.1%] (false,exec)
87 # .MEMD.3923_18 = VDEF <.MEMD.3923_16>
88 # USE = nonlocal null { fileNameD.2604 D.3926 } (restr)
89 # CLB = nonlocal null { fileNameD.2604 D.3926 } (restr)
90 fooD.2599 (outputFileNameD.2600_2(D), fpD.2605_8);
91 # SUCC: 7 [100.0%] (fallthru,exec)
93 # BLOCK 7 freq:10000
94 # PRED: 3 [100.0%] (fallthru,exec) 5 [100.0%] (fallthru,exec)
95 6 [100.0%] (fallthru,exec)
96 # PT = nonlocal null
98 # ctxD.2601_1 = PHI <0B(3), 0B(5), ctxD.2601_5(D)(6)>
99 # .MEMD.3923_11 = PHI <.MEMD.3923_15(3), .MEMD.3923_17(5),
100 .MEMD.3923_18(6)>
101 # VUSE <.MEMD.3923_11>
102 return ctxD.2601_1;
103 # SUCC: EXIT [100.0%]
106 bb 3 and bb 5 can be merged. The blocks have different predecessors, but the
107 same successors, and the same operations.
110 CONTEXT
112 A technique called tail merging (or cross jumping) can fix the example
113 above. For a block, we look for common code at the end (the tail) of the
114 predecessor blocks, and insert jumps from one block to the other.
115 The example is a special case for tail merging, in that 2 whole blocks
116 can be merged, rather than just the end parts of it.
117 We currently only focus on whole block merging, so in that sense
118 calling this pass tail merge is a bit of a misnomer.
120 We distinguish 2 kinds of situations in which blocks can be merged:
121 - same operations, same predecessors. The successor edges coming from one
122 block are redirected to come from the other block.
123 - same operations, same successors. The predecessor edges entering one block
124 are redirected to enter the other block. Note that this operation might
125 involve introducing phi operations.
127 For efficient implementation, we would like to value numbers the blocks, and
128 have a comparison operator that tells us whether the blocks are equal.
129 Besides being runtime efficient, block value numbering should also abstract
130 from irrelevant differences in order of operations, much like normal value
131 numbering abstracts from irrelevant order of operations.
133 For the first situation (same_operations, same predecessors), normal value
134 numbering fits well. We can calculate a block value number based on the
135 value numbers of the defs and vdefs.
137 For the second situation (same operations, same successors), this approach
138 doesn't work so well. We can illustrate this using the example. The calls
139 to free use different vdefs: MEMD.3923_16 and MEMD.3923_14, and these will
140 remain different in value numbering, since they represent different memory
141 states. So the resulting vdefs of the frees will be different in value
142 numbering, so the block value numbers will be different.
144 The reason why we call the blocks equal is not because they define the same
145 values, but because uses in the blocks use (possibly different) defs in the
146 same way. To be able to detect this efficiently, we need to do some kind of
147 reverse value numbering, meaning number the uses rather than the defs, and
148 calculate a block value number based on the value number of the uses.
149 Ideally, a block comparison operator will also indicate which phis are needed
150 to merge the blocks.
152 For the moment, we don't do block value numbering, but we do insn-by-insn
153 matching, using scc value numbers to match operations with results, and
154 structural comparison otherwise, while ignoring vop mismatches.
157 IMPLEMENTATION
159 1. The pass first determines all groups of blocks with the same successor
160 blocks.
161 2. Within each group, it tries to determine clusters of equal basic blocks.
162 3. The clusters are applied.
163 4. The same successor groups are updated.
164 5. This process is repeated from 2 onwards, until no more changes.
167 LIMITATIONS/TODO
169 - block only
170 - handles only 'same operations, same successors'.
171 It handles same predecessors as a special subcase though.
172 - does not implement the reverse value numbering and block value numbering.
173 - improve memory allocation: use garbage collected memory, obstacks,
174 allocpools where appropriate.
175 - no insertion of gimple_reg phis, We only introduce vop-phis.
176 - handle blocks with gimple_reg phi_nodes.
179 PASS PLACEMENT
180 This 'pass' is not a stand-alone gimple pass, but runs as part of
181 pass_pre, in order to share the value numbering.
184 SWITCHES
186 - ftree-tail-merge. On at -O2. We may have to enable it only at -Os. */
188 #include "config.h"
189 #include "system.h"
190 #include "coretypes.h"
191 #include "tm.h"
192 #include "tree.h"
193 #include "tm_p.h"
194 #include "basic-block.h"
195 #include "flags.h"
196 #include "function.h"
197 #include "tree-flow.h"
198 #include "bitmap.h"
199 #include "tree-ssa-alias.h"
200 #include "params.h"
201 #include "hash-table.h"
202 #include "gimple-pretty-print.h"
203 #include "tree-ssa-sccvn.h"
204 #include "tree-dump.h"
205 #include "cfgloop.h"
206 #include "tree-pass.h"
208 /* Describes a group of bbs with the same successors. The successor bbs are
209 cached in succs, and the successor edge flags are cached in succ_flags.
210 If a bb has the EDGE_TRUE/VALSE_VALUE flags swapped compared to succ_flags,
211 it's marked in inverse.
212 Additionally, the hash value for the struct is cached in hashval, and
213 in_worklist indicates whether it's currently part of worklist. */
215 struct same_succ_def
217 /* The bbs that have the same successor bbs. */
218 bitmap bbs;
219 /* The successor bbs. */
220 bitmap succs;
221 /* Indicates whether the EDGE_TRUE/FALSE_VALUEs of succ_flags are swapped for
222 bb. */
223 bitmap inverse;
224 /* The edge flags for each of the successor bbs. */
225 vec<int> succ_flags;
226 /* Indicates whether the struct is currently in the worklist. */
227 bool in_worklist;
228 /* The hash value of the struct. */
229 hashval_t hashval;
231 /* hash_table support. */
232 typedef same_succ_def value_type;
233 typedef same_succ_def compare_type;
234 static inline hashval_t hash (const value_type *);
235 static int equal (const value_type *, const compare_type *);
236 static void remove (value_type *);
238 typedef struct same_succ_def *same_succ;
239 typedef const struct same_succ_def *const_same_succ;
241 /* hash routine for hash_table support, returns hashval of E. */
243 inline hashval_t
244 same_succ_def::hash (const value_type *e)
246 return e->hashval;
249 /* A group of bbs where 1 bb from bbs can replace the other bbs. */
251 struct bb_cluster_def
253 /* The bbs in the cluster. */
254 bitmap bbs;
255 /* The preds of the bbs in the cluster. */
256 bitmap preds;
257 /* Index in all_clusters vector. */
258 int index;
259 /* The bb to replace the cluster with. */
260 basic_block rep_bb;
262 typedef struct bb_cluster_def *bb_cluster;
263 typedef const struct bb_cluster_def *const_bb_cluster;
265 /* Per bb-info. */
267 struct aux_bb_info
269 /* The number of non-debug statements in the bb. */
270 int size;
271 /* The same_succ that this bb is a member of. */
272 same_succ bb_same_succ;
273 /* The cluster that this bb is a member of. */
274 bb_cluster cluster;
275 /* The vop state at the exit of a bb. This is shortlived data, used to
276 communicate data between update_block_by and update_vuses. */
277 tree vop_at_exit;
278 /* The bb that either contains or is dominated by the dependencies of the
279 bb. */
280 basic_block dep_bb;
283 /* Macros to access the fields of struct aux_bb_info. */
285 #define BB_SIZE(bb) (((struct aux_bb_info *)bb->aux)->size)
286 #define BB_SAME_SUCC(bb) (((struct aux_bb_info *)bb->aux)->bb_same_succ)
287 #define BB_CLUSTER(bb) (((struct aux_bb_info *)bb->aux)->cluster)
288 #define BB_VOP_AT_EXIT(bb) (((struct aux_bb_info *)bb->aux)->vop_at_exit)
289 #define BB_DEP_BB(bb) (((struct aux_bb_info *)bb->aux)->dep_bb)
291 /* Returns true if the only effect a statement STMT has, is to define locally
292 used SSA_NAMEs. */
294 static bool
295 stmt_local_def (gimple stmt)
297 basic_block bb, def_bb;
298 imm_use_iterator iter;
299 use_operand_p use_p;
300 tree val;
301 def_operand_p def_p;
303 if (gimple_has_side_effects (stmt))
304 return false;
306 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
307 if (def_p == NULL)
308 return false;
310 val = DEF_FROM_PTR (def_p);
311 if (val == NULL_TREE || TREE_CODE (val) != SSA_NAME)
312 return false;
314 def_bb = gimple_bb (stmt);
316 FOR_EACH_IMM_USE_FAST (use_p, iter, val)
318 if (is_gimple_debug (USE_STMT (use_p)))
319 continue;
320 bb = gimple_bb (USE_STMT (use_p));
321 if (bb == def_bb)
322 continue;
324 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI
325 && EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src == def_bb)
326 continue;
328 return false;
331 return true;
334 /* Let GSI skip forwards over local defs. */
336 static void
337 gsi_advance_fw_nondebug_nonlocal (gimple_stmt_iterator *gsi)
339 gimple stmt;
341 while (true)
343 if (gsi_end_p (*gsi))
344 return;
345 stmt = gsi_stmt (*gsi);
346 if (!stmt_local_def (stmt))
347 return;
348 gsi_next_nondebug (gsi);
352 /* VAL1 and VAL2 are either:
353 - uses in BB1 and BB2, or
354 - phi alternatives for BB1 and BB2.
355 Return true if the uses have the same gvn value. */
357 static bool
358 gvn_uses_equal (tree val1, tree val2)
360 gcc_checking_assert (val1 != NULL_TREE && val2 != NULL_TREE);
362 if (val1 == val2)
363 return true;
365 if (vn_valueize (val1) != vn_valueize (val2))
366 return false;
368 return ((TREE_CODE (val1) == SSA_NAME || CONSTANT_CLASS_P (val1))
369 && (TREE_CODE (val2) == SSA_NAME || CONSTANT_CLASS_P (val2)));
372 /* Prints E to FILE. */
374 static void
375 same_succ_print (FILE *file, const same_succ e)
377 unsigned int i;
378 bitmap_print (file, e->bbs, "bbs:", "\n");
379 bitmap_print (file, e->succs, "succs:", "\n");
380 bitmap_print (file, e->inverse, "inverse:", "\n");
381 fprintf (file, "flags:");
382 for (i = 0; i < e->succ_flags.length (); ++i)
383 fprintf (file, " %x", e->succ_flags[i]);
384 fprintf (file, "\n");
387 /* Prints same_succ VE to VFILE. */
389 inline int
390 ssa_same_succ_print_traverse (same_succ *pe, FILE *file)
392 const same_succ e = *pe;
393 same_succ_print (file, e);
394 return 1;
397 /* Update BB_DEP_BB (USE_BB), given a use of VAL in USE_BB. */
399 static void
400 update_dep_bb (basic_block use_bb, tree val)
402 basic_block dep_bb;
404 /* Not a dep. */
405 if (TREE_CODE (val) != SSA_NAME)
406 return;
408 /* Skip use of global def. */
409 if (SSA_NAME_IS_DEFAULT_DEF (val))
410 return;
412 /* Skip use of local def. */
413 dep_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
414 if (dep_bb == use_bb)
415 return;
417 if (BB_DEP_BB (use_bb) == NULL
418 || dominated_by_p (CDI_DOMINATORS, dep_bb, BB_DEP_BB (use_bb)))
419 BB_DEP_BB (use_bb) = dep_bb;
422 /* Update BB_DEP_BB, given the dependencies in STMT. */
424 static void
425 stmt_update_dep_bb (gimple stmt)
427 ssa_op_iter iter;
428 use_operand_p use;
430 FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
431 update_dep_bb (gimple_bb (stmt), USE_FROM_PTR (use));
434 /* Calculates hash value for same_succ VE. */
436 static hashval_t
437 same_succ_hash (const_same_succ e)
439 hashval_t hashval = bitmap_hash (e->succs);
440 int flags;
441 unsigned int i;
442 unsigned int first = bitmap_first_set_bit (e->bbs);
443 basic_block bb = BASIC_BLOCK (first);
444 int size = 0;
445 gimple_stmt_iterator gsi;
446 gimple stmt;
447 tree arg;
448 unsigned int s;
449 bitmap_iterator bs;
451 for (gsi = gsi_start_nondebug_bb (bb);
452 !gsi_end_p (gsi); gsi_next_nondebug (&gsi))
454 stmt = gsi_stmt (gsi);
455 stmt_update_dep_bb (stmt);
456 if (stmt_local_def (stmt))
457 continue;
458 size++;
460 hashval = iterative_hash_hashval_t (gimple_code (stmt), hashval);
461 if (is_gimple_assign (stmt))
462 hashval = iterative_hash_hashval_t (gimple_assign_rhs_code (stmt),
463 hashval);
464 if (!is_gimple_call (stmt))
465 continue;
466 if (gimple_call_internal_p (stmt))
467 hashval = iterative_hash_hashval_t
468 ((hashval_t) gimple_call_internal_fn (stmt), hashval);
469 else
470 hashval = iterative_hash_expr (gimple_call_fn (stmt), hashval);
471 for (i = 0; i < gimple_call_num_args (stmt); i++)
473 arg = gimple_call_arg (stmt, i);
474 arg = vn_valueize (arg);
475 hashval = iterative_hash_expr (arg, hashval);
479 hashval = iterative_hash_hashval_t (size, hashval);
480 BB_SIZE (bb) = size;
482 for (i = 0; i < e->succ_flags.length (); ++i)
484 flags = e->succ_flags[i];
485 flags = flags & ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
486 hashval = iterative_hash_hashval_t (flags, hashval);
489 EXECUTE_IF_SET_IN_BITMAP (e->succs, 0, s, bs)
491 int n = find_edge (bb, BASIC_BLOCK (s))->dest_idx;
492 for (gsi = gsi_start_phis (BASIC_BLOCK (s)); !gsi_end_p (gsi);
493 gsi_next (&gsi))
495 gimple phi = gsi_stmt (gsi);
496 tree lhs = gimple_phi_result (phi);
497 tree val = gimple_phi_arg_def (phi, n);
499 if (virtual_operand_p (lhs))
500 continue;
501 update_dep_bb (bb, val);
505 return hashval;
508 /* Returns true if E1 and E2 have 2 successors, and if the successor flags
509 are inverse for the EDGE_TRUE_VALUE and EDGE_FALSE_VALUE flags, and equal for
510 the other edge flags. */
512 static bool
513 inverse_flags (const_same_succ e1, const_same_succ e2)
515 int f1a, f1b, f2a, f2b;
516 int mask = ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
518 if (e1->succ_flags.length () != 2)
519 return false;
521 f1a = e1->succ_flags[0];
522 f1b = e1->succ_flags[1];
523 f2a = e2->succ_flags[0];
524 f2b = e2->succ_flags[1];
526 if (f1a == f2a && f1b == f2b)
527 return false;
529 return (f1a & mask) == (f2a & mask) && (f1b & mask) == (f2b & mask);
532 /* Compares SAME_SUCCs E1 and E2. */
535 same_succ_def::equal (const value_type *e1, const compare_type *e2)
537 unsigned int i, first1, first2;
538 gimple_stmt_iterator gsi1, gsi2;
539 gimple s1, s2;
540 basic_block bb1, bb2;
542 if (e1->hashval != e2->hashval)
543 return 0;
545 if (e1->succ_flags.length () != e2->succ_flags.length ())
546 return 0;
548 if (!bitmap_equal_p (e1->succs, e2->succs))
549 return 0;
551 if (!inverse_flags (e1, e2))
553 for (i = 0; i < e1->succ_flags.length (); ++i)
554 if (e1->succ_flags[i] != e1->succ_flags[i])
555 return 0;
558 first1 = bitmap_first_set_bit (e1->bbs);
559 first2 = bitmap_first_set_bit (e2->bbs);
561 bb1 = BASIC_BLOCK (first1);
562 bb2 = BASIC_BLOCK (first2);
564 if (BB_SIZE (bb1) != BB_SIZE (bb2))
565 return 0;
567 gsi1 = gsi_start_nondebug_bb (bb1);
568 gsi2 = gsi_start_nondebug_bb (bb2);
569 gsi_advance_fw_nondebug_nonlocal (&gsi1);
570 gsi_advance_fw_nondebug_nonlocal (&gsi2);
571 while (!(gsi_end_p (gsi1) || gsi_end_p (gsi2)))
573 s1 = gsi_stmt (gsi1);
574 s2 = gsi_stmt (gsi2);
575 if (gimple_code (s1) != gimple_code (s2))
576 return 0;
577 if (is_gimple_call (s1) && !gimple_call_same_target_p (s1, s2))
578 return 0;
579 gsi_next_nondebug (&gsi1);
580 gsi_next_nondebug (&gsi2);
581 gsi_advance_fw_nondebug_nonlocal (&gsi1);
582 gsi_advance_fw_nondebug_nonlocal (&gsi2);
585 return 1;
588 /* Alloc and init a new SAME_SUCC. */
590 static same_succ
591 same_succ_alloc (void)
593 same_succ same = XNEW (struct same_succ_def);
595 same->bbs = BITMAP_ALLOC (NULL);
596 same->succs = BITMAP_ALLOC (NULL);
597 same->inverse = BITMAP_ALLOC (NULL);
598 same->succ_flags.create (10);
599 same->in_worklist = false;
601 return same;
604 /* Delete same_succ E. */
606 void
607 same_succ_def::remove (same_succ e)
609 BITMAP_FREE (e->bbs);
610 BITMAP_FREE (e->succs);
611 BITMAP_FREE (e->inverse);
612 e->succ_flags.release ();
614 XDELETE (e);
617 /* Reset same_succ SAME. */
619 static void
620 same_succ_reset (same_succ same)
622 bitmap_clear (same->bbs);
623 bitmap_clear (same->succs);
624 bitmap_clear (same->inverse);
625 same->succ_flags.truncate (0);
628 static hash_table <same_succ_def> same_succ_htab;
630 /* Array that is used to store the edge flags for a successor. */
632 static int *same_succ_edge_flags;
634 /* Bitmap that is used to mark bbs that are recently deleted. */
636 static bitmap deleted_bbs;
638 /* Bitmap that is used to mark predecessors of bbs that are
639 deleted. */
641 static bitmap deleted_bb_preds;
643 /* Prints same_succ_htab to stderr. */
645 extern void debug_same_succ (void);
646 DEBUG_FUNCTION void
647 debug_same_succ ( void)
649 same_succ_htab.traverse <FILE *, ssa_same_succ_print_traverse> (stderr);
653 /* Vector of bbs to process. */
655 static vec<same_succ> worklist;
657 /* Prints worklist to FILE. */
659 static void
660 print_worklist (FILE *file)
662 unsigned int i;
663 for (i = 0; i < worklist.length (); ++i)
664 same_succ_print (file, worklist[i]);
667 /* Adds SAME to worklist. */
669 static void
670 add_to_worklist (same_succ same)
672 if (same->in_worklist)
673 return;
675 if (bitmap_count_bits (same->bbs) < 2)
676 return;
678 same->in_worklist = true;
679 worklist.safe_push (same);
682 /* Add BB to same_succ_htab. */
684 static void
685 find_same_succ_bb (basic_block bb, same_succ *same_p)
687 unsigned int j;
688 bitmap_iterator bj;
689 same_succ same = *same_p;
690 same_succ *slot;
691 edge_iterator ei;
692 edge e;
694 if (bb == NULL
695 /* Be conservative with loop structure. It's not evident that this test
696 is sufficient. Before tail-merge, we've just called
697 loop_optimizer_finalize, and LOOPS_MAY_HAVE_MULTIPLE_LATCHES is now
698 set, so there's no guarantee that the loop->latch value is still valid.
699 But we assume that, since we've forced LOOPS_HAVE_SIMPLE_LATCHES at the
700 start of pre, we've kept that property intact throughout pre, and are
701 keeping it throughout tail-merge using this test. */
702 || bb->loop_father->latch == bb)
703 return;
704 bitmap_set_bit (same->bbs, bb->index);
705 FOR_EACH_EDGE (e, ei, bb->succs)
707 int index = e->dest->index;
708 bitmap_set_bit (same->succs, index);
709 same_succ_edge_flags[index] = e->flags;
711 EXECUTE_IF_SET_IN_BITMAP (same->succs, 0, j, bj)
712 same->succ_flags.safe_push (same_succ_edge_flags[j]);
714 same->hashval = same_succ_hash (same);
716 slot = same_succ_htab.find_slot_with_hash (same, same->hashval, INSERT);
717 if (*slot == NULL)
719 *slot = same;
720 BB_SAME_SUCC (bb) = same;
721 add_to_worklist (same);
722 *same_p = NULL;
724 else
726 bitmap_set_bit ((*slot)->bbs, bb->index);
727 BB_SAME_SUCC (bb) = *slot;
728 add_to_worklist (*slot);
729 if (inverse_flags (same, *slot))
730 bitmap_set_bit ((*slot)->inverse, bb->index);
731 same_succ_reset (same);
735 /* Find bbs with same successors. */
737 static void
738 find_same_succ (void)
740 same_succ same = same_succ_alloc ();
741 basic_block bb;
743 FOR_EACH_BB (bb)
745 find_same_succ_bb (bb, &same);
746 if (same == NULL)
747 same = same_succ_alloc ();
750 same_succ_def::remove (same);
753 /* Initializes worklist administration. */
755 static void
756 init_worklist (void)
758 alloc_aux_for_blocks (sizeof (struct aux_bb_info));
759 same_succ_htab.create (n_basic_blocks);
760 same_succ_edge_flags = XCNEWVEC (int, last_basic_block);
761 deleted_bbs = BITMAP_ALLOC (NULL);
762 deleted_bb_preds = BITMAP_ALLOC (NULL);
763 worklist.create (n_basic_blocks);
764 find_same_succ ();
766 if (dump_file && (dump_flags & TDF_DETAILS))
768 fprintf (dump_file, "initial worklist:\n");
769 print_worklist (dump_file);
773 /* Deletes worklist administration. */
775 static void
776 delete_worklist (void)
778 free_aux_for_blocks ();
779 same_succ_htab.dispose ();
780 XDELETEVEC (same_succ_edge_flags);
781 same_succ_edge_flags = NULL;
782 BITMAP_FREE (deleted_bbs);
783 BITMAP_FREE (deleted_bb_preds);
784 worklist.release ();
787 /* Mark BB as deleted, and mark its predecessors. */
789 static void
790 mark_basic_block_deleted (basic_block bb)
792 edge e;
793 edge_iterator ei;
795 bitmap_set_bit (deleted_bbs, bb->index);
797 FOR_EACH_EDGE (e, ei, bb->preds)
798 bitmap_set_bit (deleted_bb_preds, e->src->index);
801 /* Removes BB from its corresponding same_succ. */
803 static void
804 same_succ_flush_bb (basic_block bb)
806 same_succ same = BB_SAME_SUCC (bb);
807 BB_SAME_SUCC (bb) = NULL;
808 if (bitmap_single_bit_set_p (same->bbs))
809 same_succ_htab.remove_elt_with_hash (same, same->hashval);
810 else
811 bitmap_clear_bit (same->bbs, bb->index);
814 /* Removes all bbs in BBS from their corresponding same_succ. */
816 static void
817 same_succ_flush_bbs (bitmap bbs)
819 unsigned int i;
820 bitmap_iterator bi;
822 EXECUTE_IF_SET_IN_BITMAP (bbs, 0, i, bi)
823 same_succ_flush_bb (BASIC_BLOCK (i));
826 /* Release the last vdef in BB, either normal or phi result. */
828 static void
829 release_last_vdef (basic_block bb)
831 gimple_stmt_iterator i;
833 for (i = gsi_last_bb (bb); !gsi_end_p (i); gsi_prev_nondebug (&i))
835 gimple stmt = gsi_stmt (i);
836 if (gimple_vdef (stmt) == NULL_TREE)
837 continue;
839 mark_virtual_operand_for_renaming (gimple_vdef (stmt));
840 return;
843 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
845 gimple phi = gsi_stmt (i);
846 tree res = gimple_phi_result (phi);
848 if (!virtual_operand_p (res))
849 continue;
851 mark_virtual_phi_result_for_renaming (phi);
852 return;
857 /* For deleted_bb_preds, find bbs with same successors. */
859 static void
860 update_worklist (void)
862 unsigned int i;
863 bitmap_iterator bi;
864 basic_block bb;
865 same_succ same;
867 bitmap_and_compl_into (deleted_bb_preds, deleted_bbs);
868 bitmap_clear (deleted_bbs);
870 bitmap_clear_bit (deleted_bb_preds, ENTRY_BLOCK);
871 same_succ_flush_bbs (deleted_bb_preds);
873 same = same_succ_alloc ();
874 EXECUTE_IF_SET_IN_BITMAP (deleted_bb_preds, 0, i, bi)
876 bb = BASIC_BLOCK (i);
877 gcc_assert (bb != NULL);
878 find_same_succ_bb (bb, &same);
879 if (same == NULL)
880 same = same_succ_alloc ();
882 same_succ_def::remove (same);
883 bitmap_clear (deleted_bb_preds);
886 /* Prints cluster C to FILE. */
888 static void
889 print_cluster (FILE *file, bb_cluster c)
891 if (c == NULL)
892 return;
893 bitmap_print (file, c->bbs, "bbs:", "\n");
894 bitmap_print (file, c->preds, "preds:", "\n");
897 /* Prints cluster C to stderr. */
899 extern void debug_cluster (bb_cluster);
900 DEBUG_FUNCTION void
901 debug_cluster (bb_cluster c)
903 print_cluster (stderr, c);
906 /* Update C->rep_bb, given that BB is added to the cluster. */
908 static void
909 update_rep_bb (bb_cluster c, basic_block bb)
911 /* Initial. */
912 if (c->rep_bb == NULL)
914 c->rep_bb = bb;
915 return;
918 /* Current needs no deps, keep it. */
919 if (BB_DEP_BB (c->rep_bb) == NULL)
920 return;
922 /* Bb needs no deps, change rep_bb. */
923 if (BB_DEP_BB (bb) == NULL)
925 c->rep_bb = bb;
926 return;
929 /* Bb needs last deps earlier than current, change rep_bb. A potential
930 problem with this, is that the first deps might also be earlier, which
931 would mean we prefer longer lifetimes for the deps. To be able to check
932 for this, we would have to trace BB_FIRST_DEP_BB as well, besides
933 BB_DEP_BB, which is really BB_LAST_DEP_BB.
934 The benefit of choosing the bb with last deps earlier, is that it can
935 potentially be used as replacement for more bbs. */
936 if (dominated_by_p (CDI_DOMINATORS, BB_DEP_BB (c->rep_bb), BB_DEP_BB (bb)))
937 c->rep_bb = bb;
940 /* Add BB to cluster C. Sets BB in C->bbs, and preds of BB in C->preds. */
942 static void
943 add_bb_to_cluster (bb_cluster c, basic_block bb)
945 edge e;
946 edge_iterator ei;
948 bitmap_set_bit (c->bbs, bb->index);
950 FOR_EACH_EDGE (e, ei, bb->preds)
951 bitmap_set_bit (c->preds, e->src->index);
953 update_rep_bb (c, bb);
956 /* Allocate and init new cluster. */
958 static bb_cluster
959 new_cluster (void)
961 bb_cluster c;
962 c = XCNEW (struct bb_cluster_def);
963 c->bbs = BITMAP_ALLOC (NULL);
964 c->preds = BITMAP_ALLOC (NULL);
965 c->rep_bb = NULL;
966 return c;
969 /* Delete clusters. */
971 static void
972 delete_cluster (bb_cluster c)
974 if (c == NULL)
975 return;
976 BITMAP_FREE (c->bbs);
977 BITMAP_FREE (c->preds);
978 XDELETE (c);
982 /* Array that contains all clusters. */
984 static vec<bb_cluster> all_clusters;
986 /* Allocate all cluster vectors. */
988 static void
989 alloc_cluster_vectors (void)
991 all_clusters.create (n_basic_blocks);
994 /* Reset all cluster vectors. */
996 static void
997 reset_cluster_vectors (void)
999 unsigned int i;
1000 basic_block bb;
1001 for (i = 0; i < all_clusters.length (); ++i)
1002 delete_cluster (all_clusters[i]);
1003 all_clusters.truncate (0);
1004 FOR_EACH_BB (bb)
1005 BB_CLUSTER (bb) = NULL;
1008 /* Delete all cluster vectors. */
1010 static void
1011 delete_cluster_vectors (void)
1013 unsigned int i;
1014 for (i = 0; i < all_clusters.length (); ++i)
1015 delete_cluster (all_clusters[i]);
1016 all_clusters.release ();
1019 /* Merge cluster C2 into C1. */
1021 static void
1022 merge_clusters (bb_cluster c1, bb_cluster c2)
1024 bitmap_ior_into (c1->bbs, c2->bbs);
1025 bitmap_ior_into (c1->preds, c2->preds);
1028 /* Register equivalence of BB1 and BB2 (members of cluster C). Store c in
1029 all_clusters, or merge c with existing cluster. */
1031 static void
1032 set_cluster (basic_block bb1, basic_block bb2)
1034 basic_block merge_bb, other_bb;
1035 bb_cluster merge, old, c;
1037 if (BB_CLUSTER (bb1) == NULL && BB_CLUSTER (bb2) == NULL)
1039 c = new_cluster ();
1040 add_bb_to_cluster (c, bb1);
1041 add_bb_to_cluster (c, bb2);
1042 BB_CLUSTER (bb1) = c;
1043 BB_CLUSTER (bb2) = c;
1044 c->index = all_clusters.length ();
1045 all_clusters.safe_push (c);
1047 else if (BB_CLUSTER (bb1) == NULL || BB_CLUSTER (bb2) == NULL)
1049 merge_bb = BB_CLUSTER (bb1) == NULL ? bb2 : bb1;
1050 other_bb = BB_CLUSTER (bb1) == NULL ? bb1 : bb2;
1051 merge = BB_CLUSTER (merge_bb);
1052 add_bb_to_cluster (merge, other_bb);
1053 BB_CLUSTER (other_bb) = merge;
1055 else if (BB_CLUSTER (bb1) != BB_CLUSTER (bb2))
1057 unsigned int i;
1058 bitmap_iterator bi;
1060 old = BB_CLUSTER (bb2);
1061 merge = BB_CLUSTER (bb1);
1062 merge_clusters (merge, old);
1063 EXECUTE_IF_SET_IN_BITMAP (old->bbs, 0, i, bi)
1064 BB_CLUSTER (BASIC_BLOCK (i)) = merge;
1065 all_clusters[old->index] = NULL;
1066 update_rep_bb (merge, old->rep_bb);
1067 delete_cluster (old);
1069 else
1070 gcc_unreachable ();
1073 /* Return true if gimple statements S1 and S2 are equal. Gimple_bb (s1) and
1074 gimple_bb (s2) are members of SAME_SUCC. */
1076 static bool
1077 gimple_equal_p (same_succ same_succ, gimple s1, gimple s2)
1079 unsigned int i;
1080 tree lhs1, lhs2;
1081 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
1082 tree t1, t2;
1083 bool equal, inv_cond;
1084 enum tree_code code1, code2;
1086 if (gimple_code (s1) != gimple_code (s2))
1087 return false;
1089 switch (gimple_code (s1))
1091 case GIMPLE_CALL:
1092 if (gimple_call_num_args (s1) != gimple_call_num_args (s2))
1093 return false;
1094 if (!gimple_call_same_target_p (s1, s2))
1095 return false;
1097 /* Eventually, we'll significantly complicate the CFG by adding
1098 back edges to properly model the effects of transaction restart.
1099 For the bulk of optimization this does not matter, but what we
1100 cannot recover from is tail merging blocks between two separate
1101 transactions. Avoid that by making commit not match. */
1102 if (gimple_call_builtin_p (s1, BUILT_IN_TM_COMMIT))
1103 return false;
1105 equal = true;
1106 for (i = 0; i < gimple_call_num_args (s1); ++i)
1108 t1 = gimple_call_arg (s1, i);
1109 t2 = gimple_call_arg (s2, i);
1110 if (operand_equal_p (t1, t2, 0))
1111 continue;
1112 if (gvn_uses_equal (t1, t2))
1113 continue;
1114 equal = false;
1115 break;
1117 if (!equal)
1118 return false;
1120 lhs1 = gimple_get_lhs (s1);
1121 lhs2 = gimple_get_lhs (s2);
1122 if (lhs1 == NULL_TREE && lhs2 == NULL_TREE)
1123 return true;
1124 if (lhs1 == NULL_TREE || lhs2 == NULL_TREE)
1125 return false;
1126 if (TREE_CODE (lhs1) == SSA_NAME && TREE_CODE (lhs2) == SSA_NAME)
1127 return vn_valueize (lhs1) == vn_valueize (lhs2);
1128 return operand_equal_p (lhs1, lhs2, 0);
1130 case GIMPLE_ASSIGN:
1131 lhs1 = gimple_get_lhs (s1);
1132 lhs2 = gimple_get_lhs (s2);
1133 if (TREE_CODE (lhs1) != SSA_NAME
1134 && TREE_CODE (lhs2) != SSA_NAME)
1135 return (vn_valueize (gimple_vdef (s1))
1136 == vn_valueize (gimple_vdef (s2)));
1137 else if (TREE_CODE (lhs1) == SSA_NAME
1138 && TREE_CODE (lhs2) == SSA_NAME)
1139 return vn_valueize (lhs1) == vn_valueize (lhs2);
1140 return false;
1142 case GIMPLE_COND:
1143 t1 = gimple_cond_lhs (s1);
1144 t2 = gimple_cond_lhs (s2);
1145 if (!operand_equal_p (t1, t2, 0)
1146 && !gvn_uses_equal (t1, t2))
1147 return false;
1149 t1 = gimple_cond_rhs (s1);
1150 t2 = gimple_cond_rhs (s2);
1151 if (!operand_equal_p (t1, t2, 0)
1152 && !gvn_uses_equal (t1, t2))
1153 return false;
1155 code1 = gimple_expr_code (s1);
1156 code2 = gimple_expr_code (s2);
1157 inv_cond = (bitmap_bit_p (same_succ->inverse, bb1->index)
1158 != bitmap_bit_p (same_succ->inverse, bb2->index));
1159 if (inv_cond)
1161 bool honor_nans
1162 = HONOR_NANS (TYPE_MODE (TREE_TYPE (gimple_cond_lhs (s1))));
1163 code2 = invert_tree_comparison (code2, honor_nans);
1165 return code1 == code2;
1167 default:
1168 return false;
1172 /* Let GSI skip backwards over local defs. Return the earliest vuse in VUSE.
1173 Return true in VUSE_ESCAPED if the vuse influenced a SSA_OP_DEF of one of the
1174 processed statements. */
1176 static void
1177 gsi_advance_bw_nondebug_nonlocal (gimple_stmt_iterator *gsi, tree *vuse,
1178 bool *vuse_escaped)
1180 gimple stmt;
1181 tree lvuse;
1183 while (true)
1185 if (gsi_end_p (*gsi))
1186 return;
1187 stmt = gsi_stmt (*gsi);
1189 lvuse = gimple_vuse (stmt);
1190 if (lvuse != NULL_TREE)
1192 *vuse = lvuse;
1193 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_DEF))
1194 *vuse_escaped = true;
1197 if (!stmt_local_def (stmt))
1198 return;
1199 gsi_prev_nondebug (gsi);
1203 /* Determines whether BB1 and BB2 (members of same_succ) are duplicates. If so,
1204 clusters them. */
1206 static void
1207 find_duplicate (same_succ same_succ, basic_block bb1, basic_block bb2)
1209 gimple_stmt_iterator gsi1 = gsi_last_nondebug_bb (bb1);
1210 gimple_stmt_iterator gsi2 = gsi_last_nondebug_bb (bb2);
1211 tree vuse1 = NULL_TREE, vuse2 = NULL_TREE;
1212 bool vuse_escaped = false;
1214 gsi_advance_bw_nondebug_nonlocal (&gsi1, &vuse1, &vuse_escaped);
1215 gsi_advance_bw_nondebug_nonlocal (&gsi2, &vuse2, &vuse_escaped);
1217 while (!gsi_end_p (gsi1) && !gsi_end_p (gsi2))
1219 gimple stmt1 = gsi_stmt (gsi1);
1220 gimple stmt2 = gsi_stmt (gsi2);
1222 if (!gimple_equal_p (same_succ, stmt1, stmt2))
1223 return;
1225 // We cannot tail-merge the builtins that end transactions.
1226 // ??? The alternative being unsharing of BBs in the tm_init pass.
1227 if (flag_tm
1228 && is_gimple_call (stmt1)
1229 && (gimple_call_flags (stmt1) & ECF_TM_BUILTIN)
1230 && is_tm_ending_fndecl (gimple_call_fndecl (stmt1)))
1231 return;
1233 gsi_prev_nondebug (&gsi1);
1234 gsi_prev_nondebug (&gsi2);
1235 gsi_advance_bw_nondebug_nonlocal (&gsi1, &vuse1, &vuse_escaped);
1236 gsi_advance_bw_nondebug_nonlocal (&gsi2, &vuse2, &vuse_escaped);
1239 if (!(gsi_end_p (gsi1) && gsi_end_p (gsi2)))
1240 return;
1242 /* If the incoming vuses are not the same, and the vuse escaped into an
1243 SSA_OP_DEF, then merging the 2 blocks will change the value of the def,
1244 which potentially means the semantics of one of the blocks will be changed.
1245 TODO: make this check more precise. */
1246 if (vuse_escaped && vuse1 != vuse2)
1247 return;
1249 if (dump_file)
1250 fprintf (dump_file, "find_duplicates: <bb %d> duplicate of <bb %d>\n",
1251 bb1->index, bb2->index);
1253 set_cluster (bb1, bb2);
1256 /* Returns whether for all phis in DEST the phi alternatives for E1 and
1257 E2 are equal. */
1259 static bool
1260 same_phi_alternatives_1 (basic_block dest, edge e1, edge e2)
1262 int n1 = e1->dest_idx, n2 = e2->dest_idx;
1263 gimple_stmt_iterator gsi;
1265 for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
1267 gimple phi = gsi_stmt (gsi);
1268 tree lhs = gimple_phi_result (phi);
1269 tree val1 = gimple_phi_arg_def (phi, n1);
1270 tree val2 = gimple_phi_arg_def (phi, n2);
1272 if (virtual_operand_p (lhs))
1273 continue;
1275 if (operand_equal_for_phi_arg_p (val1, val2))
1276 continue;
1277 if (gvn_uses_equal (val1, val2))
1278 continue;
1280 return false;
1283 return true;
1286 /* Returns whether for all successors of BB1 and BB2 (members of SAME_SUCC), the
1287 phi alternatives for BB1 and BB2 are equal. */
1289 static bool
1290 same_phi_alternatives (same_succ same_succ, basic_block bb1, basic_block bb2)
1292 unsigned int s;
1293 bitmap_iterator bs;
1294 edge e1, e2;
1295 basic_block succ;
1297 EXECUTE_IF_SET_IN_BITMAP (same_succ->succs, 0, s, bs)
1299 succ = BASIC_BLOCK (s);
1300 e1 = find_edge (bb1, succ);
1301 e2 = find_edge (bb2, succ);
1302 if (e1->flags & EDGE_COMPLEX
1303 || e2->flags & EDGE_COMPLEX)
1304 return false;
1306 /* For all phis in bb, the phi alternatives for e1 and e2 need to have
1307 the same value. */
1308 if (!same_phi_alternatives_1 (succ, e1, e2))
1309 return false;
1312 return true;
1315 /* Return true if BB has non-vop phis. */
1317 static bool
1318 bb_has_non_vop_phi (basic_block bb)
1320 gimple_seq phis = phi_nodes (bb);
1321 gimple phi;
1323 if (phis == NULL)
1324 return false;
1326 if (!gimple_seq_singleton_p (phis))
1327 return true;
1329 phi = gimple_seq_first_stmt (phis);
1330 return !virtual_operand_p (gimple_phi_result (phi));
1333 /* Returns true if redirecting the incoming edges of FROM to TO maintains the
1334 invariant that uses in FROM are dominates by their defs. */
1336 static bool
1337 deps_ok_for_redirect_from_bb_to_bb (basic_block from, basic_block to)
1339 basic_block cd, dep_bb = BB_DEP_BB (to);
1340 edge_iterator ei;
1341 edge e;
1342 bitmap from_preds = BITMAP_ALLOC (NULL);
1344 if (dep_bb == NULL)
1345 return true;
1347 FOR_EACH_EDGE (e, ei, from->preds)
1348 bitmap_set_bit (from_preds, e->src->index);
1349 cd = nearest_common_dominator_for_set (CDI_DOMINATORS, from_preds);
1350 BITMAP_FREE (from_preds);
1352 return dominated_by_p (CDI_DOMINATORS, dep_bb, cd);
1355 /* Returns true if replacing BB1 (or its replacement bb) by BB2 (or its
1356 replacement bb) and vice versa maintains the invariant that uses in the
1357 replacement are dominates by their defs. */
1359 static bool
1360 deps_ok_for_redirect (basic_block bb1, basic_block bb2)
1362 if (BB_CLUSTER (bb1) != NULL)
1363 bb1 = BB_CLUSTER (bb1)->rep_bb;
1365 if (BB_CLUSTER (bb2) != NULL)
1366 bb2 = BB_CLUSTER (bb2)->rep_bb;
1368 return (deps_ok_for_redirect_from_bb_to_bb (bb1, bb2)
1369 && deps_ok_for_redirect_from_bb_to_bb (bb2, bb1));
1372 /* Within SAME_SUCC->bbs, find clusters of bbs which can be merged. */
1374 static void
1375 find_clusters_1 (same_succ same_succ)
1377 basic_block bb1, bb2;
1378 unsigned int i, j;
1379 bitmap_iterator bi, bj;
1380 int nr_comparisons;
1381 int max_comparisons = PARAM_VALUE (PARAM_MAX_TAIL_MERGE_COMPARISONS);
1383 EXECUTE_IF_SET_IN_BITMAP (same_succ->bbs, 0, i, bi)
1385 bb1 = BASIC_BLOCK (i);
1387 /* TODO: handle blocks with phi-nodes. We'll have to find corresponding
1388 phi-nodes in bb1 and bb2, with the same alternatives for the same
1389 preds. */
1390 if (bb_has_non_vop_phi (bb1))
1391 continue;
1393 nr_comparisons = 0;
1394 EXECUTE_IF_SET_IN_BITMAP (same_succ->bbs, i + 1, j, bj)
1396 bb2 = BASIC_BLOCK (j);
1398 if (bb_has_non_vop_phi (bb2))
1399 continue;
1401 if (BB_CLUSTER (bb1) != NULL && BB_CLUSTER (bb1) == BB_CLUSTER (bb2))
1402 continue;
1404 /* Limit quadratic behaviour. */
1405 nr_comparisons++;
1406 if (nr_comparisons > max_comparisons)
1407 break;
1409 /* This is a conservative dependency check. We could test more
1410 precise for allowed replacement direction. */
1411 if (!deps_ok_for_redirect (bb1, bb2))
1412 continue;
1414 if (!(same_phi_alternatives (same_succ, bb1, bb2)))
1415 continue;
1417 find_duplicate (same_succ, bb1, bb2);
1422 /* Find clusters of bbs which can be merged. */
1424 static void
1425 find_clusters (void)
1427 same_succ same;
1429 while (!worklist.is_empty ())
1431 same = worklist.pop ();
1432 same->in_worklist = false;
1433 if (dump_file && (dump_flags & TDF_DETAILS))
1435 fprintf (dump_file, "processing worklist entry\n");
1436 same_succ_print (dump_file, same);
1438 find_clusters_1 (same);
1442 /* Returns the vop phi of BB, if any. */
1444 static gimple
1445 vop_phi (basic_block bb)
1447 gimple stmt;
1448 gimple_stmt_iterator gsi;
1449 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1451 stmt = gsi_stmt (gsi);
1452 if (! virtual_operand_p (gimple_phi_result (stmt)))
1453 continue;
1454 return stmt;
1456 return NULL;
1459 /* Redirect all edges from BB1 to BB2, removes BB1 and marks it as removed. */
1461 static void
1462 replace_block_by (basic_block bb1, basic_block bb2)
1464 edge pred_edge;
1465 unsigned int i;
1466 gimple bb2_phi;
1468 bb2_phi = vop_phi (bb2);
1470 /* Mark the basic block as deleted. */
1471 mark_basic_block_deleted (bb1);
1473 /* Redirect the incoming edges of bb1 to bb2. */
1474 for (i = EDGE_COUNT (bb1->preds); i > 0 ; --i)
1476 pred_edge = EDGE_PRED (bb1, i - 1);
1477 pred_edge = redirect_edge_and_branch (pred_edge, bb2);
1478 gcc_assert (pred_edge != NULL);
1480 if (bb2_phi == NULL)
1481 continue;
1483 /* The phi might have run out of capacity when the redirect added an
1484 argument, which means it could have been replaced. Refresh it. */
1485 bb2_phi = vop_phi (bb2);
1487 add_phi_arg (bb2_phi, SSA_NAME_VAR (gimple_phi_result (bb2_phi)),
1488 pred_edge, UNKNOWN_LOCATION);
1491 bb2->frequency += bb1->frequency;
1492 if (bb2->frequency > BB_FREQ_MAX)
1493 bb2->frequency = BB_FREQ_MAX;
1495 bb2->count += bb1->count;
1497 /* Do updates that use bb1, before deleting bb1. */
1498 release_last_vdef (bb1);
1499 same_succ_flush_bb (bb1);
1501 delete_basic_block (bb1);
1504 /* Bbs for which update_debug_stmt need to be called. */
1506 static bitmap update_bbs;
1508 /* For each cluster in all_clusters, merge all cluster->bbs. Returns
1509 number of bbs removed. */
1511 static int
1512 apply_clusters (void)
1514 basic_block bb1, bb2;
1515 bb_cluster c;
1516 unsigned int i, j;
1517 bitmap_iterator bj;
1518 int nr_bbs_removed = 0;
1520 for (i = 0; i < all_clusters.length (); ++i)
1522 c = all_clusters[i];
1523 if (c == NULL)
1524 continue;
1526 bb2 = c->rep_bb;
1527 bitmap_set_bit (update_bbs, bb2->index);
1529 bitmap_clear_bit (c->bbs, bb2->index);
1530 EXECUTE_IF_SET_IN_BITMAP (c->bbs, 0, j, bj)
1532 bb1 = BASIC_BLOCK (j);
1533 bitmap_clear_bit (update_bbs, bb1->index);
1535 replace_block_by (bb1, bb2);
1536 nr_bbs_removed++;
1540 return nr_bbs_removed;
1543 /* Resets debug statement STMT if it has uses that are not dominated by their
1544 defs. */
1546 static void
1547 update_debug_stmt (gimple stmt)
1549 use_operand_p use_p;
1550 ssa_op_iter oi;
1551 basic_block bbdef, bbuse;
1552 gimple def_stmt;
1553 tree name;
1555 if (!gimple_debug_bind_p (stmt))
1556 return;
1558 bbuse = gimple_bb (stmt);
1559 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, oi, SSA_OP_USE)
1561 name = USE_FROM_PTR (use_p);
1562 gcc_assert (TREE_CODE (name) == SSA_NAME);
1564 def_stmt = SSA_NAME_DEF_STMT (name);
1565 gcc_assert (def_stmt != NULL);
1567 bbdef = gimple_bb (def_stmt);
1568 if (bbdef == NULL || bbuse == bbdef
1569 || dominated_by_p (CDI_DOMINATORS, bbuse, bbdef))
1570 continue;
1572 gimple_debug_bind_reset_value (stmt);
1573 update_stmt (stmt);
1577 /* Resets all debug statements that have uses that are not
1578 dominated by their defs. */
1580 static void
1581 update_debug_stmts (void)
1583 basic_block bb;
1584 bitmap_iterator bi;
1585 unsigned int i;
1587 EXECUTE_IF_SET_IN_BITMAP (update_bbs, 0, i, bi)
1589 gimple stmt;
1590 gimple_stmt_iterator gsi;
1592 bb = BASIC_BLOCK (i);
1593 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1595 stmt = gsi_stmt (gsi);
1596 if (!is_gimple_debug (stmt))
1597 continue;
1598 update_debug_stmt (stmt);
1603 /* Runs tail merge optimization. */
1605 unsigned int
1606 tail_merge_optimize (unsigned int todo)
1608 int nr_bbs_removed_total = 0;
1609 int nr_bbs_removed;
1610 bool loop_entered = false;
1611 int iteration_nr = 0;
1612 int max_iterations = PARAM_VALUE (PARAM_MAX_TAIL_MERGE_ITERATIONS);
1614 if (!flag_tree_tail_merge
1615 || max_iterations == 0
1616 /* We try to be conservative with respect to loop structure, since:
1617 - the cases where tail-merging could both affect loop structure and be
1618 benificial are rare,
1619 - it prevents us from having to fixup the loops using
1620 loops_state_set (LOOPS_NEED_FIXUP), and
1621 - keeping loop structure may allow us to simplify the pass.
1622 In order to be conservative, we need loop information. In rare cases
1623 (about 7 test-cases in the g++ testsuite) there is none (because
1624 loop_optimizer_finalize has been called before tail-merge, and
1625 PROP_loops is not set), so we bail out. */
1626 || current_loops == NULL)
1627 return 0;
1629 timevar_push (TV_TREE_TAIL_MERGE);
1631 if (!dom_info_available_p (CDI_DOMINATORS))
1633 /* PRE can leave us with unreachable blocks, remove them now. */
1634 delete_unreachable_blocks ();
1635 calculate_dominance_info (CDI_DOMINATORS);
1637 init_worklist ();
1639 while (!worklist.is_empty ())
1641 if (!loop_entered)
1643 loop_entered = true;
1644 alloc_cluster_vectors ();
1645 update_bbs = BITMAP_ALLOC (NULL);
1647 else
1648 reset_cluster_vectors ();
1650 iteration_nr++;
1651 if (dump_file && (dump_flags & TDF_DETAILS))
1652 fprintf (dump_file, "worklist iteration #%d\n", iteration_nr);
1654 find_clusters ();
1655 gcc_assert (worklist.is_empty ());
1656 if (all_clusters.is_empty ())
1657 break;
1659 nr_bbs_removed = apply_clusters ();
1660 nr_bbs_removed_total += nr_bbs_removed;
1661 if (nr_bbs_removed == 0)
1662 break;
1664 free_dominance_info (CDI_DOMINATORS);
1666 if (iteration_nr == max_iterations)
1667 break;
1669 calculate_dominance_info (CDI_DOMINATORS);
1670 update_worklist ();
1673 if (dump_file && (dump_flags & TDF_DETAILS))
1674 fprintf (dump_file, "htab collision / search: %f\n",
1675 same_succ_htab.collisions ());
1677 if (nr_bbs_removed_total > 0)
1679 if (MAY_HAVE_DEBUG_STMTS)
1681 calculate_dominance_info (CDI_DOMINATORS);
1682 update_debug_stmts ();
1685 if (dump_file && (dump_flags & TDF_DETAILS))
1687 fprintf (dump_file, "Before TODOs.\n");
1688 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1691 todo |= (TODO_verify_ssa | TODO_verify_stmts | TODO_verify_flow);
1692 mark_virtual_operands_for_renaming (cfun);
1695 delete_worklist ();
1696 if (loop_entered)
1698 delete_cluster_vectors ();
1699 BITMAP_FREE (update_bbs);
1702 timevar_pop (TV_TREE_TAIL_MERGE);
1704 return todo;