Line data Source code
1 : /* Tail merging for gimple.
2 : Copyright (C) 2011-2026 Free Software Foundation, Inc.
3 : Contributed by Tom de Vries (tom@codesourcery.com)
4 :
5 : This file is part of GCC.
6 :
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.
11 :
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.
16 :
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/>. */
20 :
21 : /* Pass overview.
22 :
23 :
24 : MOTIVATIONAL EXAMPLE
25 :
26 : gimple representation of gcc/testsuite/gcc.dg/pr43864.c at
27 :
28 : hprofStartupp (charD.1 * outputFileNameD.2600, charD.1 * ctxD.2601)
29 : {
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;
34 :
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)
53 :
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)
62 :
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)
75 :
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)
84 :
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)
92 :
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
97 :
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%]
104 : }
105 :
106 : bb 3 and bb 5 can be merged. The blocks have different predecessors, but the
107 : same successors, and the same operations.
108 :
109 :
110 : CONTEXT
111 :
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.
119 :
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.
126 :
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.
132 :
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.
136 :
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.
143 :
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.
151 :
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.
155 :
156 :
157 : IMPLEMENTATION
158 :
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.
165 :
166 :
167 : LIMITATIONS/TODO
168 :
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.
177 :
178 :
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.
182 :
183 :
184 : SWITCHES
185 :
186 : - ftree-tail-merge. On at -O2. We may have to enable it only at -Os. */
187 :
188 : #include "config.h"
189 : #include "system.h"
190 : #include "coretypes.h"
191 : #include "backend.h"
192 : #include "tree.h"
193 : #include "gimple.h"
194 : #include "cfghooks.h"
195 : #include "tree-pass.h"
196 : #include "ssa.h"
197 : #include "fold-const.h"
198 : #include "trans-mem.h"
199 : #include "cfganal.h"
200 : #include "cfgcleanup.h"
201 : #include "gimple-iterator.h"
202 : #include "tree-cfg.h"
203 : #include "tree-into-ssa.h"
204 : #include "tree-ssa-sccvn.h"
205 : #include "tree-ssa-ifcombine.h"
206 : #include "cfgloop.h"
207 : #include "tree-eh.h"
208 : #include "tree-cfgcleanup.h"
209 : #include "tree-ssa.h"
210 : #include "gimple-match.h"
211 :
212 : const int ignore_edge_flags = EDGE_DFS_BACK | EDGE_EXECUTABLE;
213 :
214 : /* Describes a group of bbs with the same successors. The successor bbs are
215 : cached in succs, and the successor edge flags are cached in succ_flags.
216 : If a bb has the EDGE_TRUE/FALSE_VALUE flags swapped compared to succ_flags,
217 : it's marked in inverse.
218 : Additionally, the hash value for the struct is cached in hashval, and
219 : in_worklist indicates whether it's currently part of worklist. */
220 :
221 : struct same_succ : pointer_hash <same_succ>
222 : {
223 : /* The bbs that have the same successor bbs. */
224 : bitmap bbs;
225 : /* The successor bbs. */
226 : bitmap succs;
227 : /* Indicates whether the EDGE_TRUE/FALSE_VALUEs of succ_flags are swapped for
228 : bb. */
229 : bitmap inverse;
230 : /* The edge flags for each of the successor bbs. */
231 : vec<int> succ_flags;
232 : /* Indicates whether the struct is currently in the worklist. */
233 : bool in_worklist;
234 : /* The hash value of the struct. */
235 : hashval_t hashval;
236 :
237 : /* hash_table support. */
238 : static inline hashval_t hash (const same_succ *);
239 : static int equal (const same_succ *, const same_succ *);
240 : static void remove (same_succ *);
241 : };
242 :
243 : /* hash routine for hash_table support, returns hashval of E. */
244 :
245 : inline hashval_t
246 44972400 : same_succ::hash (const same_succ *e)
247 : {
248 44972400 : return e->hashval;
249 : }
250 :
251 : /* A group of bbs where 1 bb from bbs can replace the other bbs. */
252 :
253 : struct bb_cluster
254 : {
255 : /* The bbs in the cluster. */
256 : bitmap bbs;
257 : /* The preds of the bbs in the cluster. */
258 : bitmap preds;
259 : /* Index in all_clusters vector. */
260 : int index;
261 : /* The bb to replace the cluster with. */
262 : basic_block rep_bb;
263 : };
264 :
265 : /* Per bb-info. */
266 :
267 : struct aux_bb_info
268 : {
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 bb that either contains or is dominated by the dependencies of the
276 : bb. */
277 : basic_block dep_bb;
278 : };
279 :
280 : /* Macros to access the fields of struct aux_bb_info. */
281 :
282 : #define BB_SIZE(bb) (((struct aux_bb_info *)bb->aux)->size)
283 : #define BB_SAME_SUCC(bb) (((struct aux_bb_info *)bb->aux)->bb_same_succ)
284 : #define BB_CLUSTER(bb) (((struct aux_bb_info *)bb->aux)->cluster)
285 : #define BB_DEP_BB(bb) (((struct aux_bb_info *)bb->aux)->dep_bb)
286 :
287 : /* Valueization helper querying the VN lattice. */
288 :
289 : static tree
290 14100489 : tail_merge_valueize (tree name)
291 : {
292 14100489 : if (TREE_CODE (name) == SSA_NAME
293 14100489 : && has_VN_INFO (name))
294 : {
295 5221274 : tree tem = VN_INFO (name)->valnum;
296 5221274 : if (tem != VN_TOP)
297 5221274 : return tem;
298 : }
299 : return name;
300 : }
301 :
302 : /* Returns true if the only effect a statement STMT has, is to define locally
303 : used SSA_NAMEs. */
304 :
305 : static bool
306 40962637 : stmt_local_def (gimple *stmt)
307 : {
308 40962637 : basic_block bb, def_bb;
309 40962637 : imm_use_iterator iter;
310 40962637 : use_operand_p use_p;
311 40962637 : tree val;
312 40962637 : def_operand_p def_p;
313 :
314 40962637 : if (gimple_vdef (stmt) != NULL_TREE
315 26132595 : || gimple_has_side_effects (stmt)
316 24993172 : || gimple_could_trap_p_1 (stmt, false, false)
317 24147325 : || gimple_vuse (stmt) != NULL_TREE
318 : /* Copied from tree-ssa-ifcombine.cc:bb_no_side_effects_p():
319 : const calls don't match any of the above, yet they could
320 : still have some side-effects - they could contain
321 : gimple_could_trap_p statements, like floating point
322 : exceptions or integer division by zero. See PR70586.
323 : FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
324 : should handle this. */
325 49763559 : || is_gimple_call (stmt))
326 : return false;
327 :
328 16430823 : def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
329 16430823 : if (def_p == NULL)
330 : return false;
331 :
332 8592437 : val = DEF_FROM_PTR (def_p);
333 8592437 : if (val == NULL_TREE || TREE_CODE (val) != SSA_NAME)
334 : return false;
335 :
336 8592437 : def_bb = gimple_bb (stmt);
337 :
338 8592437 : bool any_use = false;
339 18311349 : FOR_EACH_IMM_USE_FAST (use_p, iter, val)
340 : {
341 11263765 : if (is_gimple_debug (USE_STMT (use_p)))
342 1567127 : continue;
343 :
344 9696638 : any_use = true;
345 9696638 : bb = gimple_bb (USE_STMT (use_p));
346 9696638 : if (bb == def_bb)
347 7358651 : continue;
348 :
349 3131121 : if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI
350 2337987 : && EDGE_PRED (bb, PHI_ARG_INDEX_FROM_USE (use_p))->src == def_bb)
351 793134 : continue;
352 :
353 1544853 : return false;
354 1544853 : }
355 :
356 : /* When there is no use avoid making the stmt live on other paths.
357 : This can happen with DCE disabled or not done as seen in PR98845. */
358 7047584 : if (!any_use)
359 : return false;
360 :
361 : return true;
362 : }
363 :
364 : /* Let GSI skip forwards over local defs. */
365 :
366 : static void
367 7883744 : gsi_advance_fw_nondebug_nonlocal (gimple_stmt_iterator *gsi)
368 : {
369 8421276 : gimple *stmt;
370 :
371 8958808 : while (true)
372 : {
373 8421276 : if (gsi_end_p (*gsi))
374 : return;
375 3209472 : stmt = gsi_stmt (*gsi);
376 3209472 : if (!stmt_local_def (stmt))
377 : return;
378 537532 : gsi_next_nondebug (gsi);
379 : }
380 : }
381 :
382 : /* VAL1 and VAL2 are either:
383 : - uses in BB1 and BB2, or
384 : - phi alternatives for BB1 and BB2.
385 : Return true if the uses have the same gvn value. */
386 :
387 : static bool
388 1319740 : gvn_uses_equal (tree val1, tree val2)
389 : {
390 1319740 : gcc_checking_assert (val1 != NULL_TREE && val2 != NULL_TREE);
391 :
392 1319740 : if (val1 == val2)
393 : return true;
394 :
395 1319740 : if (tail_merge_valueize (val1) != tail_merge_valueize (val2))
396 : return false;
397 :
398 0 : return ((TREE_CODE (val1) == SSA_NAME || CONSTANT_CLASS_P (val1))
399 20097 : && (TREE_CODE (val2) == SSA_NAME || CONSTANT_CLASS_P (val2)));
400 : }
401 :
402 : /* Prints E to FILE. */
403 :
404 : static void
405 16 : same_succ_print (FILE *file, const same_succ *e)
406 : {
407 16 : unsigned int i;
408 16 : bitmap_print (file, e->bbs, "bbs:", "\n");
409 16 : bitmap_print (file, e->succs, "succs:", "\n");
410 16 : bitmap_print (file, e->inverse, "inverse:", "\n");
411 16 : fprintf (file, "flags:");
412 48 : for (i = 0; i < e->succ_flags.length (); ++i)
413 16 : fprintf (file, " %x", e->succ_flags[i]);
414 16 : fprintf (file, "\n");
415 16 : }
416 :
417 : /* Prints same_succ VE to VFILE. */
418 :
419 : inline int
420 0 : ssa_same_succ_print_traverse (same_succ **pe, FILE *file)
421 : {
422 0 : const same_succ *e = *pe;
423 0 : same_succ_print (file, e);
424 0 : return 1;
425 : }
426 :
427 : /* Update BB_DEP_BB (USE_BB), given a use of VAL in USE_BB. */
428 :
429 : static void
430 37266848 : update_dep_bb (basic_block use_bb, tree val)
431 : {
432 37266848 : basic_block dep_bb;
433 :
434 : /* Not a dep. */
435 37266848 : if (TREE_CODE (val) != SSA_NAME)
436 : return;
437 :
438 : /* Skip use of global def. */
439 35631339 : if (SSA_NAME_IS_DEFAULT_DEF (val))
440 : return;
441 :
442 : /* Skip use of local def. */
443 31158183 : dep_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
444 31158183 : if (dep_bb == use_bb)
445 : return;
446 :
447 11720362 : if (BB_DEP_BB (use_bb) == NULL
448 11720362 : || dominated_by_p (CDI_DOMINATORS, dep_bb, BB_DEP_BB (use_bb)))
449 9965019 : BB_DEP_BB (use_bb) = dep_bb;
450 : }
451 :
452 : /* Update BB_DEP_BB, given the dependencies in STMT. */
453 :
454 : static void
455 36587507 : stmt_update_dep_bb (gimple *stmt)
456 : {
457 36587507 : ssa_op_iter iter;
458 36587507 : use_operand_p use;
459 :
460 67723893 : FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
461 31136386 : update_dep_bb (gimple_bb (stmt), USE_FROM_PTR (use));
462 36587507 : }
463 :
464 : /* Calculates hash value for same_succ VE. */
465 :
466 : static hashval_t
467 14653209 : same_succ_hash (const same_succ *e)
468 : {
469 14653209 : inchash::hash hstate (bitmap_hash (e->succs));
470 14653209 : int flags;
471 14653209 : unsigned int i;
472 14653209 : unsigned int first = bitmap_first_set_bit (e->bbs);
473 14653209 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, first);
474 14653209 : int size = 0;
475 14653209 : gimple *stmt;
476 14653209 : tree arg;
477 14653209 : unsigned int s;
478 14653209 : bitmap_iterator bs;
479 :
480 14653209 : for (gimple_stmt_iterator gsi = gsi_start_nondebug_bb (bb);
481 51240716 : !gsi_end_p (gsi); gsi_next_nondebug (&gsi))
482 : {
483 36587507 : stmt = gsi_stmt (gsi);
484 36587507 : if (is_gimple_debug (stmt))
485 0 : continue;
486 :
487 36587507 : stmt_update_dep_bb (stmt);
488 36587507 : if (stmt_local_def (stmt))
489 6478562 : continue;
490 30108945 : size++;
491 :
492 30108945 : hstate.add_int (gimple_code (stmt));
493 30108945 : if (is_gimple_assign (stmt))
494 16707377 : hstate.add_int (gimple_assign_rhs_code (stmt));
495 30108945 : if (!is_gimple_call (stmt))
496 24310450 : continue;
497 5798495 : if (gimple_call_internal_p (stmt))
498 117792 : hstate.add_int (gimple_call_internal_fn (stmt));
499 : else
500 : {
501 5680703 : inchash::add_expr (gimple_call_fn (stmt), hstate);
502 5680703 : if (gimple_call_chain (stmt))
503 30409 : inchash::add_expr (gimple_call_chain (stmt), hstate);
504 : }
505 17252252 : for (i = 0; i < gimple_call_num_args (stmt); i++)
506 : {
507 11453757 : arg = gimple_call_arg (stmt, i);
508 11453757 : arg = tail_merge_valueize (arg);
509 11453757 : inchash::add_expr (arg, hstate);
510 : }
511 : }
512 :
513 14653209 : hstate.add_int (size);
514 14653209 : BB_SIZE (bb) = size;
515 :
516 14653209 : hstate.add_int (bb->loop_father->num);
517 :
518 34496161 : for (i = 0; i < e->succ_flags.length (); ++i)
519 : {
520 19842952 : flags = e->succ_flags[i];
521 19842952 : flags = flags & ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
522 19842952 : hstate.add_int (flags);
523 : }
524 :
525 34496161 : EXECUTE_IF_SET_IN_BITMAP (e->succs, 0, s, bs)
526 : {
527 19842952 : int n = find_edge (bb, BASIC_BLOCK_FOR_FN (cfun, s))->dest_idx;
528 19842952 : for (gphi_iterator gsi = gsi_start_phis (BASIC_BLOCK_FOR_FN (cfun, s));
529 30664356 : !gsi_end_p (gsi);
530 10821404 : gsi_next (&gsi))
531 : {
532 10821404 : gphi *phi = gsi.phi ();
533 10821404 : tree lhs = gimple_phi_result (phi);
534 10821404 : tree val = gimple_phi_arg_def (phi, n);
535 :
536 21642808 : if (virtual_operand_p (lhs))
537 4690942 : continue;
538 6130462 : update_dep_bb (bb, val);
539 : }
540 : }
541 :
542 14653209 : return hstate.end ();
543 : }
544 :
545 : /* Returns true if E1 and E2 have 2 successors, and if the successor flags
546 : are inverse for the EDGE_TRUE_VALUE and EDGE_FALSE_VALUE flags, and equal for
547 : the other edge flags. */
548 :
549 : static bool
550 5211930 : inverse_flags (const same_succ *e1, const same_succ *e2)
551 : {
552 5211930 : int f1a, f1b, f2a, f2b;
553 5211930 : int mask = ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
554 :
555 5228452 : if (e1->succ_flags.length () != 2)
556 : return false;
557 :
558 18318 : f1a = e1->succ_flags[0];
559 18318 : f1b = e1->succ_flags[1];
560 18318 : f2a = e2->succ_flags[0];
561 18318 : f2b = e2->succ_flags[1];
562 :
563 18318 : if (f1a == f2a && f1b == f2b)
564 : return false;
565 :
566 1796 : return (f1a & mask) == (f2a & mask) && (f1b & mask) == (f2b & mask);
567 : }
568 :
569 : /* Compares SAME_SUCCs E1 and E2. */
570 :
571 : int
572 53766985 : same_succ::equal (const same_succ *e1, const same_succ *e2)
573 : {
574 53766985 : unsigned int i, first1, first2;
575 53766985 : gimple_stmt_iterator gsi1, gsi2;
576 53766985 : gimple *s1, *s2;
577 53766985 : basic_block bb1, bb2;
578 :
579 53766985 : if (e1 == e2)
580 : return 1;
581 :
582 52570760 : if (e1->hashval != e2->hashval)
583 : return 0;
584 :
585 8189919 : if (e1->succ_flags.length () != e2->succ_flags.length ())
586 : return 0;
587 :
588 2729973 : if (!bitmap_equal_p (e1->succs, e2->succs))
589 : return 0;
590 :
591 2606028 : if (!inverse_flags (e1, e2))
592 : {
593 4833437 : for (i = 0; i < e1->succ_flags.length (); ++i)
594 2228307 : if (e1->succ_flags[i] != e2->succ_flags[i])
595 : return 0;
596 : }
597 :
598 2606028 : first1 = bitmap_first_set_bit (e1->bbs);
599 2606028 : first2 = bitmap_first_set_bit (e2->bbs);
600 :
601 2606028 : bb1 = BASIC_BLOCK_FOR_FN (cfun, first1);
602 2606028 : bb2 = BASIC_BLOCK_FOR_FN (cfun, first2);
603 :
604 2606028 : if (BB_SIZE (bb1) != BB_SIZE (bb2))
605 : return 0;
606 :
607 2606028 : if (bb1->loop_father != bb2->loop_father)
608 : return 0;
609 :
610 2606028 : gsi1 = gsi_start_nondebug_bb (bb1);
611 2606028 : gsi2 = gsi_start_nondebug_bb (bb2);
612 2606028 : gsi_advance_fw_nondebug_nonlocal (&gsi1);
613 2606028 : gsi_advance_fw_nondebug_nonlocal (&gsi2);
614 6547900 : while (!(gsi_end_p (gsi1) || gsi_end_p (gsi2)))
615 : {
616 1335970 : s1 = gsi_stmt (gsi1);
617 1335970 : s2 = gsi_stmt (gsi2);
618 1335970 : if (gimple_code (s1) != gimple_code (s2))
619 : return 0;
620 1335970 : if (is_gimple_call (s1) && !gimple_call_same_target_p (s1, s2))
621 : return 0;
622 1335844 : gsi_next_nondebug (&gsi1);
623 1335844 : gsi_next_nondebug (&gsi2);
624 1335844 : gsi_advance_fw_nondebug_nonlocal (&gsi1);
625 1335844 : gsi_advance_fw_nondebug_nonlocal (&gsi2);
626 : }
627 :
628 : return 1;
629 : }
630 :
631 : /* Alloc and init a new SAME_SUCC. */
632 :
633 : static same_succ *
634 13248215 : same_succ_alloc (void)
635 : {
636 13248215 : same_succ *same = XNEW (struct same_succ);
637 :
638 13248215 : same->bbs = BITMAP_ALLOC (NULL);
639 13248215 : same->succs = BITMAP_ALLOC (NULL);
640 13248215 : same->inverse = BITMAP_ALLOC (NULL);
641 13248215 : same->succ_flags.create (10);
642 13248215 : same->in_worklist = false;
643 :
644 13248215 : return same;
645 : }
646 :
647 : /* Delete same_succ E. */
648 :
649 : void
650 13248215 : same_succ::remove (same_succ *e)
651 : {
652 13248215 : BITMAP_FREE (e->bbs);
653 13248215 : BITMAP_FREE (e->succs);
654 13248215 : BITMAP_FREE (e->inverse);
655 13248215 : e->succ_flags.release ();
656 :
657 13248215 : XDELETE (e);
658 13248215 : }
659 :
660 : /* Reset same_succ SAME. */
661 :
662 : static void
663 2605902 : same_succ_reset (same_succ *same)
664 : {
665 2605902 : bitmap_clear (same->bbs);
666 2605902 : bitmap_clear (same->succs);
667 2605902 : bitmap_clear (same->inverse);
668 2605902 : same->succ_flags.truncate (0);
669 2605902 : }
670 :
671 : static hash_table<same_succ> *same_succ_htab;
672 :
673 : /* Array that is used to store the edge flags for a successor. */
674 :
675 : static int *same_succ_edge_flags;
676 :
677 : /* Bitmap that is used to mark bbs that are recently deleted. */
678 :
679 : static bitmap deleted_bbs;
680 :
681 : /* Bitmap that is used to mark predecessors of bbs that are
682 : deleted. */
683 :
684 : static bitmap deleted_bb_preds;
685 :
686 : /* Prints same_succ_htab to stderr. */
687 :
688 : extern void debug_same_succ (void);
689 : DEBUG_FUNCTION void
690 0 : debug_same_succ ( void)
691 : {
692 0 : same_succ_htab->traverse <FILE *, ssa_same_succ_print_traverse> (stderr);
693 0 : }
694 :
695 :
696 : /* Vector of bbs to process. */
697 :
698 : static vec<same_succ *> worklist;
699 :
700 : /* Prints worklist to FILE. */
701 :
702 : static void
703 14 : print_worklist (FILE *file)
704 : {
705 14 : unsigned int i;
706 22 : for (i = 0; i < worklist.length (); ++i)
707 8 : same_succ_print (file, worklist[i]);
708 14 : }
709 :
710 : /* Adds SAME to worklist. */
711 :
712 : static void
713 14653209 : add_to_worklist (same_succ *same)
714 : {
715 14653209 : if (same->in_worklist)
716 : return;
717 :
718 13198375 : if (bitmap_count_bits (same->bbs) < 2)
719 : return;
720 :
721 1151068 : same->in_worklist = true;
722 1151068 : worklist.safe_push (same);
723 : }
724 :
725 : /* Add BB to same_succ_htab. */
726 :
727 : static void
728 14653209 : find_same_succ_bb (basic_block bb, same_succ **same_p)
729 : {
730 14653209 : unsigned int j;
731 14653209 : bitmap_iterator bj;
732 14653209 : same_succ *same = *same_p;
733 14653209 : same_succ **slot;
734 14653209 : edge_iterator ei;
735 14653209 : edge e;
736 :
737 14653209 : if (bb == NULL)
738 0 : return;
739 14653209 : bitmap_set_bit (same->bbs, bb->index);
740 34496161 : FOR_EACH_EDGE (e, ei, bb->succs)
741 : {
742 19842952 : int index = e->dest->index;
743 19842952 : bitmap_set_bit (same->succs, index);
744 19842952 : same_succ_edge_flags[index] = (e->flags & ~ignore_edge_flags);
745 : }
746 34496161 : EXECUTE_IF_SET_IN_BITMAP (same->succs, 0, j, bj)
747 19842952 : same->succ_flags.safe_push (same_succ_edge_flags[j]);
748 :
749 14653209 : same->hashval = same_succ_hash (same);
750 :
751 14653209 : slot = same_succ_htab->find_slot_with_hash (same, same->hashval, INSERT);
752 14653209 : if (*slot == NULL)
753 : {
754 12047307 : *slot = same;
755 12047307 : BB_SAME_SUCC (bb) = same;
756 12047307 : add_to_worklist (same);
757 12047307 : *same_p = NULL;
758 : }
759 : else
760 : {
761 2605902 : bitmap_set_bit ((*slot)->bbs, bb->index);
762 2605902 : BB_SAME_SUCC (bb) = *slot;
763 2605902 : add_to_worklist (*slot);
764 2605902 : if (inverse_flags (same, *slot))
765 898 : bitmap_set_bit ((*slot)->inverse, bb->index);
766 2605902 : same_succ_reset (same);
767 : }
768 : }
769 :
770 : /* Find bbs with same successors. */
771 :
772 : static void
773 987688 : find_same_succ (void)
774 : {
775 987688 : same_succ *same = same_succ_alloc ();
776 987688 : basic_block bb;
777 :
778 14444240 : FOR_EACH_BB_FN (bb, cfun)
779 : {
780 13456552 : find_same_succ_bb (bb, &same);
781 13456552 : if (same == NULL)
782 10903528 : same = same_succ_alloc ();
783 : }
784 :
785 987688 : same_succ::remove (same);
786 987688 : }
787 :
788 : /* Initializes worklist administration. */
789 :
790 : static void
791 987688 : init_worklist (void)
792 : {
793 987688 : alloc_aux_for_blocks (sizeof (struct aux_bb_info));
794 987688 : same_succ_htab = new hash_table<same_succ> (n_basic_blocks_for_fn (cfun));
795 987688 : same_succ_edge_flags = XCNEWVEC (int, last_basic_block_for_fn (cfun));
796 987688 : deleted_bbs = BITMAP_ALLOC (NULL);
797 987688 : deleted_bb_preds = BITMAP_ALLOC (NULL);
798 987688 : worklist.create (n_basic_blocks_for_fn (cfun));
799 987688 : find_same_succ ();
800 :
801 987688 : if (dump_file && (dump_flags & TDF_DETAILS))
802 : {
803 14 : fprintf (dump_file, "initial worklist:\n");
804 14 : print_worklist (dump_file);
805 : }
806 987688 : }
807 :
808 : /* Deletes worklist administration. */
809 :
810 : static void
811 987688 : delete_worklist (void)
812 : {
813 987688 : free_aux_for_blocks ();
814 987688 : delete same_succ_htab;
815 987688 : same_succ_htab = NULL;
816 987688 : XDELETEVEC (same_succ_edge_flags);
817 987688 : same_succ_edge_flags = NULL;
818 987688 : BITMAP_FREE (deleted_bbs);
819 987688 : BITMAP_FREE (deleted_bb_preds);
820 987688 : worklist.release ();
821 987688 : }
822 :
823 : /* Mark BB as deleted, and mark its predecessors. */
824 :
825 : static void
826 1308597 : mark_basic_block_deleted (basic_block bb)
827 : {
828 1308597 : edge e;
829 1308597 : edge_iterator ei;
830 :
831 1308597 : bitmap_set_bit (deleted_bbs, bb->index);
832 :
833 2741537 : FOR_EACH_EDGE (e, ei, bb->preds)
834 1432940 : bitmap_set_bit (deleted_bb_preds, e->src->index);
835 1308597 : }
836 :
837 : /* Removes BB from its corresponding same_succ. */
838 :
839 : static void
840 2505254 : same_succ_flush_bb (basic_block bb)
841 : {
842 2505254 : same_succ *same = BB_SAME_SUCC (bb);
843 2505254 : if (! same)
844 0 : return;
845 :
846 2505254 : BB_SAME_SUCC (bb) = NULL;
847 2505254 : if (bitmap_single_bit_set_p (same->bbs))
848 1196225 : same_succ_htab->remove_elt_with_hash (same, same->hashval);
849 : else
850 1309029 : bitmap_clear_bit (same->bbs, bb->index);
851 : }
852 :
853 : /* Removes all bbs in BBS from their corresponding same_succ. */
854 :
855 : static void
856 213220 : same_succ_flush_bbs (bitmap bbs)
857 : {
858 213220 : unsigned int i;
859 213220 : bitmap_iterator bi;
860 :
861 1409877 : EXECUTE_IF_SET_IN_BITMAP (bbs, 0, i, bi)
862 1196657 : same_succ_flush_bb (BASIC_BLOCK_FOR_FN (cfun, i));
863 213220 : }
864 :
865 : /* Release the last vdef in BB, either normal or phi result. */
866 :
867 : static void
868 1308597 : release_last_vdef (basic_block bb)
869 : {
870 2776067 : for (gimple_stmt_iterator i = gsi_last_bb (bb); !gsi_end_p (i);
871 158873 : gsi_prev_nondebug (&i))
872 : {
873 438852 : gimple *stmt = gsi_stmt (i);
874 858985 : if (gimple_vdef (stmt) == NULL_TREE)
875 158873 : continue;
876 :
877 279979 : mark_virtual_operand_for_renaming (gimple_vdef (stmt));
878 279979 : return;
879 : }
880 :
881 1028618 : for (gphi_iterator i = gsi_start_phis (bb); !gsi_end_p (i);
882 0 : gsi_next (&i))
883 : {
884 80 : gphi *phi = i.phi ();
885 80 : tree res = gimple_phi_result (phi);
886 :
887 160 : if (!virtual_operand_p (res))
888 0 : continue;
889 :
890 80 : mark_virtual_phi_result_for_renaming (phi);
891 80 : return;
892 : }
893 : }
894 :
895 : /* For deleted_bb_preds, find bbs with same successors. */
896 :
897 : static void
898 213220 : update_worklist (void)
899 : {
900 213220 : unsigned int i;
901 213220 : bitmap_iterator bi;
902 213220 : basic_block bb;
903 213220 : same_succ *same;
904 :
905 213220 : bitmap_and_compl_into (deleted_bb_preds, deleted_bbs);
906 213220 : bitmap_clear (deleted_bbs);
907 :
908 213220 : bitmap_clear_bit (deleted_bb_preds, ENTRY_BLOCK);
909 213220 : same_succ_flush_bbs (deleted_bb_preds);
910 :
911 213220 : same = same_succ_alloc ();
912 1409877 : EXECUTE_IF_SET_IN_BITMAP (deleted_bb_preds, 0, i, bi)
913 : {
914 1196657 : bb = BASIC_BLOCK_FOR_FN (cfun, i);
915 1196657 : gcc_assert (bb != NULL);
916 1196657 : find_same_succ_bb (bb, &same);
917 1196657 : if (same == NULL)
918 1143779 : same = same_succ_alloc ();
919 : }
920 213220 : same_succ::remove (same);
921 213220 : bitmap_clear (deleted_bb_preds);
922 213220 : }
923 :
924 : /* Prints cluster C to FILE. */
925 :
926 : static void
927 0 : print_cluster (FILE *file, bb_cluster *c)
928 : {
929 0 : if (c == NULL)
930 : return;
931 0 : bitmap_print (file, c->bbs, "bbs:", "\n");
932 0 : bitmap_print (file, c->preds, "preds:", "\n");
933 : }
934 :
935 : /* Prints cluster C to stderr. */
936 :
937 : extern void debug_cluster (bb_cluster *);
938 : DEBUG_FUNCTION void
939 0 : debug_cluster (bb_cluster *c)
940 : {
941 0 : print_cluster (stderr, c);
942 0 : }
943 :
944 : /* Update C->rep_bb, given that BB is added to the cluster. */
945 :
946 : static void
947 1943877 : update_rep_bb (bb_cluster *c, basic_block bb)
948 : {
949 : /* Initial. */
950 1943877 : if (c->rep_bb == NULL)
951 : {
952 635280 : c->rep_bb = bb;
953 635280 : return;
954 : }
955 :
956 : /* Current needs no deps, keep it. */
957 1308597 : if (BB_DEP_BB (c->rep_bb) == NULL)
958 : return;
959 :
960 : /* Bb needs no deps, change rep_bb. */
961 19869 : if (BB_DEP_BB (bb) == NULL)
962 : {
963 103 : c->rep_bb = bb;
964 103 : return;
965 : }
966 :
967 : /* Bb needs last deps earlier than current, change rep_bb. A potential
968 : problem with this, is that the first deps might also be earlier, which
969 : would mean we prefer longer lifetimes for the deps. To be able to check
970 : for this, we would have to trace BB_FIRST_DEP_BB as well, besides
971 : BB_DEP_BB, which is really BB_LAST_DEP_BB.
972 : The benefit of choosing the bb with last deps earlier, is that it can
973 : potentially be used as replacement for more bbs. */
974 19766 : if (dominated_by_p (CDI_DOMINATORS, BB_DEP_BB (c->rep_bb), BB_DEP_BB (bb)))
975 19486 : c->rep_bb = bb;
976 : }
977 :
978 : /* Add BB to cluster C. Sets BB in C->bbs, and preds of BB in C->preds. */
979 :
980 : static void
981 1943877 : add_bb_to_cluster (bb_cluster *c, basic_block bb)
982 : {
983 1943877 : edge e;
984 1943877 : edge_iterator ei;
985 :
986 1943877 : bitmap_set_bit (c->bbs, bb->index);
987 :
988 4162764 : FOR_EACH_EDGE (e, ei, bb->preds)
989 2218887 : bitmap_set_bit (c->preds, e->src->index);
990 :
991 1943877 : update_rep_bb (c, bb);
992 1943877 : }
993 :
994 : /* Allocate and init new cluster. */
995 :
996 : static bb_cluster *
997 635280 : new_cluster (void)
998 : {
999 635280 : bb_cluster *c;
1000 635280 : c = XCNEW (bb_cluster);
1001 635280 : c->bbs = BITMAP_ALLOC (NULL);
1002 635280 : c->preds = BITMAP_ALLOC (NULL);
1003 635280 : c->rep_bb = NULL;
1004 635280 : return c;
1005 : }
1006 :
1007 : /* Delete clusters. */
1008 :
1009 : static void
1010 635280 : delete_cluster (bb_cluster *c)
1011 : {
1012 635280 : if (c == NULL)
1013 : return;
1014 635280 : BITMAP_FREE (c->bbs);
1015 635280 : BITMAP_FREE (c->preds);
1016 635280 : XDELETE (c);
1017 : }
1018 :
1019 :
1020 : /* Array that contains all clusters. */
1021 :
1022 : static vec<bb_cluster *> all_clusters;
1023 :
1024 : /* Allocate all cluster vectors. */
1025 :
1026 : static void
1027 272599 : alloc_cluster_vectors (void)
1028 : {
1029 272599 : all_clusters.create (n_basic_blocks_for_fn (cfun));
1030 272599 : }
1031 :
1032 : /* Reset all cluster vectors. */
1033 :
1034 : static void
1035 13107 : reset_cluster_vectors (void)
1036 : {
1037 13107 : unsigned int i;
1038 13107 : basic_block bb;
1039 142048 : for (i = 0; i < all_clusters.length (); ++i)
1040 128941 : delete_cluster (all_clusters[i]);
1041 13107 : all_clusters.truncate (0);
1042 1413168 : FOR_EACH_BB_FN (bb, cfun)
1043 1400061 : BB_CLUSTER (bb) = NULL;
1044 13107 : }
1045 :
1046 : /* Delete all cluster vectors. */
1047 :
1048 : static void
1049 272599 : delete_cluster_vectors (void)
1050 : {
1051 272599 : unsigned int i;
1052 778938 : for (i = 0; i < all_clusters.length (); ++i)
1053 506339 : delete_cluster (all_clusters[i]);
1054 272599 : all_clusters.release ();
1055 272599 : }
1056 :
1057 : /* Merge cluster C2 into C1. */
1058 :
1059 : static void
1060 0 : merge_clusters (bb_cluster *c1, bb_cluster *c2)
1061 : {
1062 0 : bitmap_ior_into (c1->bbs, c2->bbs);
1063 0 : bitmap_ior_into (c1->preds, c2->preds);
1064 0 : }
1065 :
1066 : /* Register equivalence of BB1 and BB2 (members of cluster C). Store c in
1067 : all_clusters, or merge c with existing cluster. */
1068 :
1069 : static void
1070 1308597 : set_cluster (basic_block bb1, basic_block bb2)
1071 : {
1072 1308597 : basic_block merge_bb, other_bb;
1073 1308597 : bb_cluster *merge, *old, *c;
1074 :
1075 1308597 : if (BB_CLUSTER (bb1) == NULL && BB_CLUSTER (bb2) == NULL)
1076 : {
1077 635280 : c = new_cluster ();
1078 635280 : add_bb_to_cluster (c, bb1);
1079 635280 : add_bb_to_cluster (c, bb2);
1080 635280 : BB_CLUSTER (bb1) = c;
1081 635280 : BB_CLUSTER (bb2) = c;
1082 635280 : c->index = all_clusters.length ();
1083 635280 : all_clusters.safe_push (c);
1084 : }
1085 673317 : else if (BB_CLUSTER (bb1) == NULL || BB_CLUSTER (bb2) == NULL)
1086 : {
1087 673317 : merge_bb = BB_CLUSTER (bb1) == NULL ? bb2 : bb1;
1088 673317 : other_bb = BB_CLUSTER (bb1) == NULL ? bb1 : bb2;
1089 673317 : merge = BB_CLUSTER (merge_bb);
1090 673317 : add_bb_to_cluster (merge, other_bb);
1091 673317 : BB_CLUSTER (other_bb) = merge;
1092 : }
1093 0 : else if (BB_CLUSTER (bb1) != BB_CLUSTER (bb2))
1094 : {
1095 0 : unsigned int i;
1096 0 : bitmap_iterator bi;
1097 :
1098 0 : old = BB_CLUSTER (bb2);
1099 0 : merge = BB_CLUSTER (bb1);
1100 0 : merge_clusters (merge, old);
1101 0 : EXECUTE_IF_SET_IN_BITMAP (old->bbs, 0, i, bi)
1102 0 : BB_CLUSTER (BASIC_BLOCK_FOR_FN (cfun, i)) = merge;
1103 0 : all_clusters[old->index] = NULL;
1104 0 : update_rep_bb (merge, old->rep_bb);
1105 0 : delete_cluster (old);
1106 : }
1107 : else
1108 0 : gcc_unreachable ();
1109 1308597 : }
1110 :
1111 : /* Return true if gimple operands T1 and T2 have the same value. */
1112 :
1113 : static bool
1114 808013 : gimple_operand_equal_value_p (tree t1, tree t2)
1115 : {
1116 808013 : if (t1 == t2)
1117 : return true;
1118 :
1119 155757 : if (t1 == NULL_TREE
1120 155757 : || t2 == NULL_TREE)
1121 : return false;
1122 :
1123 155757 : if (operand_equal_p (t1, t2, OEP_MATCH_SIDE_EFFECTS))
1124 : return true;
1125 :
1126 54430 : return gvn_uses_equal (t1, t2);
1127 : }
1128 :
1129 : /* Return true if gimple statements S1 and S2 are equal. Gimple_bb (s1) and
1130 : gimple_bb (s2) are members of SAME_SUCC. */
1131 :
1132 : static bool
1133 563393 : gimple_equal_p (same_succ *same_succ, gimple *s1, gimple *s2)
1134 : {
1135 563393 : unsigned int i;
1136 563393 : tree lhs1, lhs2;
1137 563393 : basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
1138 563393 : tree t1, t2;
1139 563393 : bool inv_cond;
1140 563393 : enum tree_code code1, code2;
1141 :
1142 563393 : if (gimple_code (s1) != gimple_code (s2))
1143 : return false;
1144 :
1145 563393 : switch (gimple_code (s1))
1146 : {
1147 438253 : case GIMPLE_CALL:
1148 438253 : if (!gimple_call_same_target_p (s1, s2))
1149 : return false;
1150 :
1151 438253 : t1 = gimple_call_chain (s1);
1152 438253 : t2 = gimple_call_chain (s2);
1153 438253 : if (!gimple_operand_equal_value_p (t1, t2))
1154 : return false;
1155 :
1156 438253 : if (gimple_call_num_args (s1) != gimple_call_num_args (s2))
1157 : return false;
1158 :
1159 703540 : for (i = 0; i < gimple_call_num_args (s1); ++i)
1160 : {
1161 265293 : t1 = gimple_call_arg (s1, i);
1162 265293 : t2 = gimple_call_arg (s2, i);
1163 265293 : if (!gimple_operand_equal_value_p (t1, t2))
1164 : return false;
1165 : }
1166 :
1167 438247 : lhs1 = gimple_get_lhs (s1);
1168 438247 : lhs2 = gimple_get_lhs (s2);
1169 438247 : if (lhs1 == NULL_TREE && lhs2 == NULL_TREE)
1170 : return true;
1171 3983 : if (lhs1 == NULL_TREE || lhs2 == NULL_TREE)
1172 : return false;
1173 3983 : if (TREE_CODE (lhs1) == SSA_NAME && TREE_CODE (lhs2) == SSA_NAME)
1174 3626 : return tail_merge_valueize (lhs1) == tail_merge_valueize (lhs2);
1175 357 : return operand_equal_p (lhs1, lhs2, 0);
1176 :
1177 110635 : case GIMPLE_ASSIGN:
1178 110635 : if (gimple_assign_rhs_code (s1) != gimple_assign_rhs_code (s2))
1179 : return false;
1180 :
1181 110635 : lhs1 = gimple_get_lhs (s1);
1182 110635 : lhs2 = gimple_get_lhs (s2);
1183 110635 : if (TREE_CODE (lhs1) != SSA_NAME
1184 99422 : && TREE_CODE (lhs2) != SSA_NAME)
1185 99422 : return (operand_equal_p (lhs1, lhs2, 0)
1186 99422 : && gimple_operand_equal_value_p (gimple_assign_rhs1 (s1),
1187 : gimple_assign_rhs1 (s2)));
1188 :
1189 11213 : if (TREE_CODE (lhs1) != SSA_NAME
1190 11213 : || TREE_CODE (lhs2) != SSA_NAME)
1191 : return false;
1192 :
1193 11131 : gcc_checking_assert (gimple_num_args (s1) == gimple_num_args (s2));
1194 21867 : for (i = 0; i < gimple_num_args (s1); ++i)
1195 : {
1196 11200 : t1 = gimple_arg (s1, i);
1197 11200 : t2 = gimple_arg (s2, i);
1198 25272 : while (handled_component_p (t1) && handled_component_p (t2))
1199 : {
1200 3243 : if (TREE_CODE (t1) != TREE_CODE (t2)
1201 3243 : || TREE_THIS_VOLATILE (t1) != TREE_THIS_VOLATILE (t2))
1202 : return false;
1203 3243 : switch (TREE_CODE (t1))
1204 : {
1205 3144 : case COMPONENT_REF:
1206 3144 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)
1207 5918 : || !gimple_operand_equal_value_p (TREE_OPERAND (t1, 2),
1208 2774 : TREE_OPERAND (t2, 2)))
1209 : return false;
1210 : break;
1211 71 : case ARRAY_REF:
1212 71 : case ARRAY_RANGE_REF:
1213 71 : if (!gimple_operand_equal_value_p (TREE_OPERAND (t1, 3),
1214 71 : TREE_OPERAND (t2, 3)))
1215 : return false;
1216 : /* Fallthru. */
1217 99 : case BIT_FIELD_REF:
1218 99 : if (!gimple_operand_equal_value_p (TREE_OPERAND (t1, 1),
1219 99 : TREE_OPERAND (t2, 1))
1220 197 : || !gimple_operand_equal_value_p (TREE_OPERAND (t1, 2),
1221 98 : TREE_OPERAND (t2, 2)))
1222 : return false;
1223 : break;
1224 : case REALPART_EXPR:
1225 : case IMAGPART_EXPR:
1226 : case VIEW_CONVERT_EXPR:
1227 : break;
1228 : default:
1229 : gcc_unreachable ();
1230 : }
1231 2872 : t1 = TREE_OPERAND (t1, 0);
1232 2872 : t2 = TREE_OPERAND (t2, 0);
1233 : }
1234 10829 : if (TREE_CODE (t1) == MEM_REF && TREE_CODE (t2) == MEM_REF)
1235 : {
1236 6332 : if (TREE_THIS_VOLATILE (t1) != TREE_THIS_VOLATILE (t2)
1237 6332 : || TYPE_ALIGN (TREE_TYPE (t1)) != TYPE_ALIGN (TREE_TYPE (t2))
1238 6332 : || !gimple_operand_equal_value_p (TREE_OPERAND (t1, 0),
1239 6332 : TREE_OPERAND (t2, 0))
1240 12664 : || TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
1241 : return false;
1242 : }
1243 4497 : else if (!gimple_operand_equal_value_p (t1, t2))
1244 : return false;
1245 : }
1246 : return true;
1247 :
1248 4430 : case GIMPLE_COND:
1249 4430 : t1 = gimple_cond_lhs (s1);
1250 4430 : t2 = gimple_cond_lhs (s2);
1251 4430 : if (!gimple_operand_equal_value_p (t1, t2))
1252 : return false;
1253 :
1254 1544 : t1 = gimple_cond_rhs (s1);
1255 1544 : t2 = gimple_cond_rhs (s2);
1256 1544 : if (!gimple_operand_equal_value_p (t1, t2))
1257 : return false;
1258 :
1259 1067 : code1 = gimple_cond_code (s1);
1260 1067 : code2 = gimple_cond_code (s2);
1261 1067 : inv_cond = (bitmap_bit_p (same_succ->inverse, bb1->index)
1262 1067 : != bitmap_bit_p (same_succ->inverse, bb2->index));
1263 1067 : if (inv_cond)
1264 : {
1265 35 : bool honor_nans = HONOR_NANS (t1);
1266 35 : code2 = invert_tree_comparison (code2, honor_nans);
1267 : }
1268 1067 : return code1 == code2;
1269 :
1270 : default:
1271 : return false;
1272 : }
1273 : }
1274 :
1275 : /* Let GSI skip backwards over local defs. Return the earliest vuse in VUSE.
1276 : Return true in VUSE_ESCAPED if the vuse influenced a SSA_OP_DEF of one of the
1277 : processed statements. */
1278 :
1279 : static void
1280 3749734 : gsi_advance_bw_nondebug_nonlocal (gimple_stmt_iterator *gsi, tree *vuse,
1281 : bool *vuse_escaped)
1282 : {
1283 3757596 : gimple *stmt;
1284 3757596 : tree lvuse;
1285 :
1286 3765458 : while (true)
1287 : {
1288 3757596 : if (gsi_end_p (*gsi))
1289 : return;
1290 1165658 : stmt = gsi_stmt (*gsi);
1291 :
1292 1165658 : lvuse = gimple_vuse (stmt);
1293 1110134 : if (lvuse != NULL_TREE)
1294 : {
1295 845534 : *vuse = lvuse;
1296 845534 : if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_DEF))
1297 29212 : *vuse_escaped = true;
1298 : }
1299 :
1300 1165658 : if (!stmt_local_def (stmt))
1301 : return;
1302 7862 : gsi_prev_nondebug (gsi);
1303 : }
1304 : }
1305 :
1306 : /* Return true if equal (in the sense of gimple_equal_p) statements STMT1 and
1307 : STMT2 are allowed to be merged. */
1308 :
1309 : static bool
1310 490937 : merge_stmts_p (gimple *stmt1, gimple *stmt2)
1311 : {
1312 : /* What could be better than this here is to blacklist the bb
1313 : containing the stmt, when encountering the stmt f.i. in
1314 : same_succ_hash. */
1315 490937 : if (is_tm_ending (stmt1))
1316 : return false;
1317 :
1318 : /* Verify EH landing pads. */
1319 490885 : if (lookup_stmt_eh_lp_fn (cfun, stmt1) != lookup_stmt_eh_lp_fn (cfun, stmt2))
1320 : return false;
1321 :
1322 483397 : if (is_gimple_call (stmt1)
1323 483397 : && gimple_call_internal_p (stmt1))
1324 : {
1325 150 : location_t locs[2];
1326 150 : locs[0] = gimple_location (stmt1);
1327 150 : locs[1] = gimple_location (stmt2);
1328 :
1329 150 : if (!factor_operation_ok (gimple_call_internal_fn (stmt1),
1330 : -2, nullptr, locs, 2, true, true))
1331 0 : return false;
1332 : }
1333 :
1334 : return true;
1335 : }
1336 :
1337 : /* Determines whether BB1 and BB2 (members of same_succ) are duplicates. If so,
1338 : clusters them. */
1339 :
1340 : static void
1341 1391470 : find_duplicate (same_succ *same_succ, basic_block bb1, basic_block bb2)
1342 : {
1343 1391470 : gimple_stmt_iterator gsi1 = gsi_last_nondebug_bb (bb1);
1344 1391470 : gimple_stmt_iterator gsi2 = gsi_last_nondebug_bb (bb2);
1345 1391470 : tree vuse1 = NULL_TREE, vuse2 = NULL_TREE;
1346 1391470 : bool vuse_escaped = false;
1347 :
1348 1391470 : gsi_advance_bw_nondebug_nonlocal (&gsi1, &vuse1, &vuse_escaped);
1349 1391470 : gsi_advance_bw_nondebug_nonlocal (&gsi2, &vuse2, &vuse_escaped);
1350 :
1351 3266337 : while (!gsi_end_p (gsi1) && !gsi_end_p (gsi2))
1352 : {
1353 578898 : gimple *stmt1 = gsi_stmt (gsi1);
1354 578898 : gimple *stmt2 = gsi_stmt (gsi2);
1355 :
1356 578898 : if (gimple_code (stmt1) == GIMPLE_LABEL
1357 578898 : && gimple_code (stmt2) == GIMPLE_LABEL)
1358 : break;
1359 :
1360 563393 : if (!gimple_equal_p (same_succ, stmt1, stmt2))
1361 82873 : return;
1362 :
1363 490937 : if (!merge_stmts_p (stmt1, stmt2))
1364 : return;
1365 :
1366 483397 : gsi_prev_nondebug (&gsi1);
1367 483397 : gsi_prev_nondebug (&gsi2);
1368 483397 : gsi_advance_bw_nondebug_nonlocal (&gsi1, &vuse1, &vuse_escaped);
1369 483397 : gsi_advance_bw_nondebug_nonlocal (&gsi2, &vuse2, &vuse_escaped);
1370 : }
1371 :
1372 1326875 : while (!gsi_end_p (gsi1) && gimple_code (gsi_stmt (gsi1)) == GIMPLE_LABEL)
1373 : {
1374 15506 : tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi1)));
1375 31012 : if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
1376 : return;
1377 30801 : gsi_prev (&gsi1);
1378 : }
1379 1326770 : while (!gsi_end_p (gsi2) && gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL)
1380 : {
1381 15401 : tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)));
1382 30802 : if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
1383 : return;
1384 30801 : gsi_prev (&gsi2);
1385 : }
1386 1311369 : if (!(gsi_end_p (gsi1) && gsi_end_p (gsi2)))
1387 : return;
1388 :
1389 : /* If the incoming vuses are not the same, and the vuse escaped into an
1390 : SSA_OP_DEF, then merging the 2 blocks will change the value of the def,
1391 : which potentially means the semantics of one of the blocks will be changed.
1392 : TODO: make this check more precise. */
1393 1311369 : if (vuse_escaped && vuse1 != vuse2)
1394 : return;
1395 :
1396 1308597 : if (dump_file)
1397 30 : fprintf (dump_file, "find_duplicates: <bb %d> duplicate of <bb %d>\n",
1398 : bb1->index, bb2->index);
1399 :
1400 1308597 : set_cluster (bb1, bb2);
1401 : }
1402 :
1403 : /* Returns whether for all phis in DEST the phi alternatives for E1 and
1404 : E2 are equal. */
1405 :
1406 : static bool
1407 2254988 : same_phi_alternatives_1 (basic_block dest, edge e1, edge e2)
1408 : {
1409 2254988 : int n1 = e1->dest_idx, n2 = e2->dest_idx;
1410 2254988 : gphi_iterator gsi;
1411 :
1412 3097590 : for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
1413 : {
1414 2095768 : gphi *phi = gsi.phi ();
1415 2095768 : tree lhs = gimple_phi_result (phi);
1416 2095768 : tree val1 = gimple_phi_arg_def (phi, n1);
1417 2095768 : tree val2 = gimple_phi_arg_def (phi, n2);
1418 :
1419 4191536 : if (virtual_operand_p (lhs))
1420 595493 : continue;
1421 :
1422 1500275 : if (operand_equal_for_phi_arg_p (val1, val2))
1423 234965 : continue;
1424 1265310 : if (gvn_uses_equal (val1, val2))
1425 12144 : continue;
1426 :
1427 : return false;
1428 : }
1429 :
1430 : return true;
1431 : }
1432 :
1433 : /* Returns whether for all successors of BB1 and BB2 (members of SAME_SUCC), the
1434 : phi alternatives for BB1 and BB2 are equal. */
1435 :
1436 : static bool
1437 2644636 : same_phi_alternatives (same_succ *same_succ, basic_block bb1, basic_block bb2)
1438 : {
1439 2644636 : unsigned int s;
1440 2644636 : bitmap_iterator bs;
1441 2644636 : edge e1, e2;
1442 2644636 : basic_block succ;
1443 :
1444 3646458 : EXECUTE_IF_SET_IN_BITMAP (same_succ->succs, 0, s, bs)
1445 : {
1446 2254988 : succ = BASIC_BLOCK_FOR_FN (cfun, s);
1447 2254988 : e1 = find_edge (bb1, succ);
1448 2254988 : e2 = find_edge (bb2, succ);
1449 2254988 : if (e1->flags & EDGE_COMPLEX
1450 2254988 : || e2->flags & EDGE_COMPLEX)
1451 : return false;
1452 :
1453 : /* For all phis in bb, the phi alternatives for e1 and e2 need to have
1454 : the same value. */
1455 2254988 : if (!same_phi_alternatives_1 (succ, e1, e2))
1456 : return false;
1457 : }
1458 :
1459 : return true;
1460 : }
1461 :
1462 : /* Return true if BB has non-vop phis. */
1463 :
1464 : static bool
1465 21372873 : bb_has_non_vop_phi (basic_block bb)
1466 : {
1467 21372873 : gimple_seq phis = phi_nodes (bb);
1468 21372873 : gimple *phi;
1469 :
1470 21372873 : if (phis == NULL)
1471 : return false;
1472 :
1473 147125 : if (!gimple_seq_singleton_p (phis))
1474 : return true;
1475 :
1476 118309 : phi = gimple_seq_first_stmt (phis);
1477 236618 : return !virtual_operand_p (gimple_phi_result (phi));
1478 : }
1479 :
1480 : /* Returns true if redirecting the incoming edges of FROM to TO maintains the
1481 : invariant that uses in FROM are dominates by their defs. */
1482 :
1483 : static bool
1484 4311426 : deps_ok_for_redirect_from_bb_to_bb (basic_block from, basic_block to)
1485 : {
1486 4311426 : basic_block cd, dep_bb = BB_DEP_BB (to);
1487 4311426 : edge_iterator ei;
1488 4311426 : edge e;
1489 :
1490 4311426 : if (dep_bb == NULL)
1491 : return true;
1492 :
1493 2019422 : bitmap from_preds = BITMAP_ALLOC (NULL);
1494 4093029 : FOR_EACH_EDGE (e, ei, from->preds)
1495 2073607 : bitmap_set_bit (from_preds, e->src->index);
1496 2019422 : cd = nearest_common_dominator_for_set (CDI_DOMINATORS, from_preds);
1497 2019422 : BITMAP_FREE (from_preds);
1498 :
1499 2019422 : return dominated_by_p (CDI_DOMINATORS, dep_bb, cd);
1500 : }
1501 :
1502 : /* Returns true if replacing BB1 (or its replacement bb) by BB2 (or its
1503 : replacement bb) and vice versa maintains the invariant that uses in the
1504 : replacement are dominates by their defs. */
1505 :
1506 : static bool
1507 3441527 : deps_ok_for_redirect (basic_block &bb1, basic_block &bb2)
1508 : {
1509 3441527 : basic_block b1 = bb1;
1510 3441527 : basic_block b2 = bb2;
1511 3441527 : if (BB_CLUSTER (b1) != NULL)
1512 976456 : b1 = BB_CLUSTER (b1)->rep_bb;
1513 :
1514 3441527 : if (BB_CLUSTER (b2) != NULL)
1515 78341 : b2 = BB_CLUSTER (b2)->rep_bb;
1516 :
1517 3441527 : if (deps_ok_for_redirect_from_bb_to_bb (b1, b2))
1518 : return true;
1519 869899 : if (deps_ok_for_redirect_from_bb_to_bb (b2, b1))
1520 : {
1521 73008 : std::swap (bb1, bb2);
1522 73008 : return true;
1523 : }
1524 : return false;
1525 : }
1526 :
1527 : /* Within SAME_SUCC->bbs, find clusters of bbs which can be merged. */
1528 :
1529 : static void
1530 1151068 : find_clusters_1 (same_succ *same_succ)
1531 : {
1532 1151068 : basic_block bb1, bb2;
1533 1151068 : unsigned int i, j;
1534 1151068 : bitmap_iterator bi, bj;
1535 1151068 : int nr_comparisons;
1536 1151068 : int max_comparisons = param_max_tail_merge_comparisons;
1537 :
1538 4908454 : EXECUTE_IF_SET_IN_BITMAP (same_succ->bbs, 0, i, bi)
1539 : {
1540 3757386 : bb1 = BASIC_BLOCK_FOR_FN (cfun, i);
1541 :
1542 : /* TODO: handle blocks with phi-nodes. We'll have to find corresponding
1543 : phi-nodes in bb1 and bb2, with the same alternatives for the same
1544 : preds. */
1545 7487617 : if (bb_has_non_vop_phi (bb1) || bb_has_eh_pred (bb1)
1546 6852007 : || bb_has_abnormal_pred (bb1))
1547 662957 : continue;
1548 :
1549 3094429 : nr_comparisons = 0;
1550 20596120 : EXECUTE_IF_SET_IN_BITMAP (same_succ->bbs, i + 1, j, bj)
1551 : {
1552 17615487 : bb2 = BASIC_BLOCK_FOR_FN (cfun, j);
1553 :
1554 35213856 : if (bb_has_non_vop_phi (bb2) || bb_has_eh_pred (bb2)
1555 35213492 : || bb_has_abnormal_pred (bb2))
1556 17482 : continue;
1557 :
1558 17598005 : if (BB_CLUSTER (bb1) != NULL && BB_CLUSTER (bb1) == BB_CLUSTER (bb2))
1559 14042682 : continue;
1560 :
1561 : /* Limit quadratic behavior. */
1562 3555323 : nr_comparisons++;
1563 3555323 : if (nr_comparisons > max_comparisons)
1564 : break;
1565 :
1566 : /* This is a conservative dependency check. We could test more
1567 : precise for allowed replacement direction. */
1568 3441527 : if (!deps_ok_for_redirect (bb1, bb2))
1569 796891 : continue;
1570 :
1571 2644636 : if (!(same_phi_alternatives (same_succ, bb1, bb2)))
1572 1253166 : continue;
1573 :
1574 1391470 : find_duplicate (same_succ, bb1, bb2);
1575 : }
1576 : }
1577 1151068 : }
1578 :
1579 : /* Find clusters of bbs which can be merged. */
1580 :
1581 : static void
1582 285706 : find_clusters (void)
1583 : {
1584 285706 : same_succ *same;
1585 :
1586 1436774 : while (!worklist.is_empty ())
1587 : {
1588 1151068 : same = worklist.pop ();
1589 1151068 : same->in_worklist = false;
1590 88 : if (dump_file && (dump_flags & TDF_DETAILS))
1591 : {
1592 8 : fprintf (dump_file, "processing worklist entry\n");
1593 8 : same_succ_print (dump_file, same);
1594 : }
1595 1151068 : find_clusters_1 (same);
1596 : }
1597 285706 : }
1598 :
1599 : /* Returns the vop phi of BB, if any. */
1600 :
1601 : static gphi *
1602 1327906 : vop_phi (basic_block bb)
1603 : {
1604 1327906 : gphi *stmt;
1605 1327906 : gphi_iterator gsi;
1606 1327906 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1607 : {
1608 32097 : stmt = gsi.phi ();
1609 64194 : if (! virtual_operand_p (gimple_phi_result (stmt)))
1610 0 : continue;
1611 : return stmt;
1612 : }
1613 : return NULL;
1614 : }
1615 :
1616 : /* Redirect all edges from BB1 to BB2, removes BB1 and marks it as removed. */
1617 :
1618 : static void
1619 1308597 : replace_block_by (basic_block bb1, basic_block bb2)
1620 : {
1621 1308597 : edge pred_edge;
1622 1308597 : unsigned int i;
1623 1308597 : gphi *bb2_phi;
1624 :
1625 1308597 : bb2_phi = vop_phi (bb2);
1626 :
1627 : /* Mark the basic block as deleted. */
1628 1308597 : mark_basic_block_deleted (bb1);
1629 :
1630 : /* Redirect the incoming edges of bb1 to bb2. */
1631 4050134 : for (i = EDGE_COUNT (bb1->preds); i > 0 ; --i)
1632 : {
1633 1432940 : pred_edge = EDGE_PRED (bb1, i - 1);
1634 1432940 : pred_edge = redirect_edge_and_branch (pred_edge, bb2);
1635 1432940 : gcc_assert (pred_edge != NULL);
1636 :
1637 1432940 : if (bb2_phi == NULL)
1638 1413631 : continue;
1639 :
1640 : /* The phi might have run out of capacity when the redirect added an
1641 : argument, which means it could have been replaced. Refresh it. */
1642 19309 : bb2_phi = vop_phi (bb2);
1643 :
1644 19309 : add_phi_arg (bb2_phi, SSA_NAME_VAR (gimple_phi_result (bb2_phi)),
1645 : pred_edge, UNKNOWN_LOCATION);
1646 : }
1647 :
1648 :
1649 : /* Merge the outgoing edge counts from bb1 onto bb2. */
1650 1308597 : edge e1, e2;
1651 1308597 : edge_iterator ei;
1652 :
1653 1308597 : if (bb2->count.initialized_p ())
1654 2241663 : FOR_EACH_EDGE (e1, ei, bb1->succs)
1655 : {
1656 933171 : e2 = find_edge (bb2, e1->dest);
1657 933171 : gcc_assert (e2);
1658 :
1659 : /* If probabilities are same, we are done.
1660 : If counts are nonzero we can distribute accordingly. In remaining
1661 : cases just average the values and hope for the best. */
1662 933171 : e2->probability = e1->probability.combine_with_count
1663 933171 : (bb1->count, e2->probability, bb2->count);
1664 : }
1665 1308597 : bb2->count += bb1->count;
1666 :
1667 : /* Move over any user labels from bb1 after the bb2 labels. */
1668 1308597 : gimple_stmt_iterator gsi1 = gsi_start_bb (bb1);
1669 1308597 : if (!gsi_end_p (gsi1) && gimple_code (gsi_stmt (gsi1)) == GIMPLE_LABEL)
1670 : {
1671 15370 : gimple_stmt_iterator gsi2 = gsi_after_labels (bb2);
1672 30741 : while (!gsi_end_p (gsi1)
1673 30741 : && gimple_code (gsi_stmt (gsi1)) == GIMPLE_LABEL)
1674 : {
1675 15371 : tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi1)));
1676 30742 : gcc_assert (!DECL_NONLOCAL (label) && !FORCED_LABEL (label));
1677 15371 : if (DECL_ARTIFICIAL (label))
1678 15198 : gsi_next (&gsi1);
1679 : else
1680 173 : gsi_move_before (&gsi1, &gsi2);
1681 : }
1682 : }
1683 :
1684 : /* Clear range info from all stmts in BB2 -- this transformation
1685 : could make them out of date. */
1686 1308597 : reset_flow_sensitive_info_in_bb (bb2);
1687 :
1688 : /* Do updates that use bb1, before deleting bb1. */
1689 1308597 : release_last_vdef (bb1);
1690 1308597 : same_succ_flush_bb (bb1);
1691 :
1692 1308597 : delete_basic_block (bb1);
1693 1308597 : }
1694 :
1695 : /* Bbs for which update_debug_stmt need to be called. */
1696 :
1697 : static bitmap update_bbs;
1698 :
1699 : static bitmap ifcombine_candidate_bbs;
1700 :
1701 : /* For each cluster in all_clusters, merge all cluster->bbs. Returns
1702 : number of bbs removed. */
1703 :
1704 : static int
1705 220564 : apply_clusters (void)
1706 : {
1707 220564 : basic_block bb1, bb2;
1708 220564 : bb_cluster *c;
1709 220564 : unsigned int i, j;
1710 220564 : bitmap_iterator bj;
1711 220564 : int nr_bbs_removed = 0;
1712 :
1713 855844 : for (i = 0; i < all_clusters.length (); ++i)
1714 : {
1715 635280 : c = all_clusters[i];
1716 635280 : if (c == NULL)
1717 0 : continue;
1718 :
1719 635280 : bb2 = c->rep_bb;
1720 635280 : bitmap_set_bit (update_bbs, bb2->index);
1721 :
1722 635280 : bitmap_clear_bit (c->bbs, bb2->index);
1723 1943877 : EXECUTE_IF_SET_IN_BITMAP (c->bbs, 0, j, bj)
1724 : {
1725 1308597 : bb1 = BASIC_BLOCK_FOR_FN (cfun, j);
1726 1308597 : bitmap_clear_bit (update_bbs, bb1->index);
1727 :
1728 1308597 : replace_block_by (bb1, bb2);
1729 :
1730 1308597 : basic_block imm_dominator
1731 1308597 : = get_immediate_dominator (CDI_DOMINATORS, bb2);
1732 :
1733 : /* Find conditions in if-statements that lead to bb2. */
1734 1308597 : edge e;
1735 1308597 : edge_iterator ei;
1736 21966766 : FOR_EACH_EDGE (e, ei, bb2->preds)
1737 : {
1738 : /* The immediate dominator's condition cannot be combined; skip it
1739 : before the more expensive recognize_if_then_else check. */
1740 20658169 : if (e->src == imm_dominator)
1741 1252682 : continue;
1742 :
1743 19405487 : basic_block then_tmp = NULL;
1744 19405487 : basic_block else_tmp = NULL;
1745 19405487 : if (recognize_if_then_else (e->src, &bb2, &else_tmp)
1746 19405487 : || recognize_if_then_else (e->src, &then_tmp, &bb2))
1747 : {
1748 : /* A recognized if-then-else always ends in a gcond. */
1749 33950332 : gcc_assert (safe_dyn_cast <gcond *> (*gsi_last_bb (e->src)));
1750 16975166 : bitmap_set_bit (ifcombine_candidate_bbs, e->src->index);
1751 : }
1752 : }
1753 :
1754 1308597 : nr_bbs_removed++;
1755 : }
1756 : }
1757 :
1758 220564 : return nr_bbs_removed;
1759 : }
1760 :
1761 : /* Resets debug statement STMT if it has uses that are not dominated by their
1762 : defs. */
1763 :
1764 : static void
1765 135956 : update_debug_stmt (gimple *stmt)
1766 : {
1767 135956 : use_operand_p use_p;
1768 135956 : ssa_op_iter oi;
1769 135956 : basic_block bbuse;
1770 :
1771 135956 : if (!gimple_debug_bind_p (stmt))
1772 22721 : return;
1773 :
1774 113235 : bbuse = gimple_bb (stmt);
1775 118592 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, oi, SSA_OP_USE)
1776 : {
1777 5785 : tree name = USE_FROM_PTR (use_p);
1778 5785 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1779 5785 : basic_block bbdef = gimple_bb (def_stmt);
1780 11142 : if (bbdef == NULL || bbuse == bbdef
1781 5785 : || dominated_by_p (CDI_DOMINATORS, bbuse, bbdef))
1782 5357 : continue;
1783 :
1784 428 : gimple_debug_bind_reset_value (stmt);
1785 428 : update_stmt (stmt);
1786 428 : break;
1787 : }
1788 : }
1789 :
1790 : /* Resets all debug statements that have uses that are not
1791 : dominated by their defs. */
1792 :
1793 : static void
1794 142161 : update_debug_stmts (void)
1795 : {
1796 142161 : basic_block bb;
1797 142161 : bitmap_iterator bi;
1798 142161 : unsigned int i;
1799 :
1800 592913 : EXECUTE_IF_SET_IN_BITMAP (update_bbs, 0, i, bi)
1801 : {
1802 450752 : gimple *stmt;
1803 450752 : gimple_stmt_iterator gsi;
1804 :
1805 450752 : bb = BASIC_BLOCK_FOR_FN (cfun, i);
1806 1117509 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1807 : {
1808 216005 : stmt = gsi_stmt (gsi);
1809 216005 : if (!is_gimple_debug (stmt))
1810 80049 : continue;
1811 135956 : update_debug_stmt (stmt);
1812 : }
1813 : }
1814 142161 : }
1815 :
1816 : /* Runs tail merge optimization. */
1817 :
1818 : unsigned int
1819 987733 : tail_merge_optimize (bool need_crit_edge_split)
1820 : {
1821 987733 : int nr_bbs_removed_total = 0;
1822 987733 : int nr_bbs_removed;
1823 987733 : bool loop_entered = false;
1824 987733 : int iteration_nr = 0;
1825 987733 : int max_iterations = param_max_tail_merge_iterations;
1826 987733 : unsigned int todo = 0;
1827 :
1828 987733 : if (!flag_tree_tail_merge
1829 987688 : || max_iterations == 0)
1830 : return 0;
1831 :
1832 987688 : timevar_push (TV_TREE_TAIL_MERGE);
1833 :
1834 : /* Re-split critical edges when PRE did a CFG cleanup. */
1835 987688 : if (need_crit_edge_split)
1836 141498 : split_edges_for_insertion ();
1837 :
1838 987688 : if (!dom_info_available_p (CDI_DOMINATORS))
1839 : {
1840 : /* PRE can leave us with unreachable blocks, remove them now. */
1841 0 : delete_unreachable_blocks ();
1842 0 : calculate_dominance_info (CDI_DOMINATORS);
1843 : }
1844 987688 : init_worklist ();
1845 :
1846 2188596 : while (!worklist.is_empty ())
1847 : {
1848 285706 : if (!loop_entered)
1849 : {
1850 272599 : loop_entered = true;
1851 272599 : alloc_cluster_vectors ();
1852 272599 : update_bbs = BITMAP_ALLOC (NULL);
1853 272599 : ifcombine_candidate_bbs = BITMAP_ALLOC (NULL);
1854 : }
1855 : else
1856 13107 : reset_cluster_vectors ();
1857 :
1858 285706 : iteration_nr++;
1859 285706 : if (dump_file && (dump_flags & TDF_DETAILS))
1860 7 : fprintf (dump_file, "worklist iteration #%d\n", iteration_nr);
1861 :
1862 285706 : find_clusters ();
1863 285706 : gcc_assert (worklist.is_empty ());
1864 285706 : if (all_clusters.is_empty ())
1865 : break;
1866 :
1867 220564 : nr_bbs_removed = apply_clusters ();
1868 220564 : nr_bbs_removed_total += nr_bbs_removed;
1869 220564 : if (nr_bbs_removed == 0)
1870 : break;
1871 :
1872 220564 : free_dominance_info (CDI_DOMINATORS);
1873 :
1874 220564 : if (iteration_nr == max_iterations)
1875 : break;
1876 :
1877 213220 : calculate_dominance_info (CDI_DOMINATORS);
1878 213220 : update_worklist ();
1879 : }
1880 :
1881 987688 : if (dump_file && (dump_flags & TDF_DETAILS))
1882 28 : fprintf (dump_file, "htab collision / search: %f\n",
1883 : same_succ_htab->collisions ());
1884 :
1885 987688 : if (nr_bbs_removed_total > 0)
1886 : {
1887 213220 : bool need_dominance
1888 213220 : = MAY_HAVE_DEBUG_BIND_STMTS
1889 213220 : || !bitmap_empty_p (ifcombine_candidate_bbs);
1890 :
1891 : if (need_dominance)
1892 212663 : calculate_dominance_info (CDI_DOMINATORS);
1893 :
1894 213220 : if (MAY_HAVE_DEBUG_BIND_STMTS)
1895 142161 : update_debug_stmts ();
1896 :
1897 213220 : unsigned int i;
1898 213220 : bitmap_iterator bi;
1899 213220 : bool cfg_changed = false;
1900 : /* Try to combine conditions of blocks that were made to branch to the
1901 : same successor by tail merging. */
1902 213220 : if (!bitmap_empty_p (ifcombine_candidate_bbs))
1903 : {
1904 206342 : mark_ssa_maybe_undefs ();
1905 1294388 : EXECUTE_IF_SET_IN_BITMAP (ifcombine_candidate_bbs, 0, i, bi)
1906 : {
1907 1088046 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1908 1088046 : if (bb)
1909 1087635 : cfg_changed |= tree_ssa_ifcombine_bb (bb);
1910 : }
1911 : }
1912 :
1913 213220 : if (dump_file && (dump_flags & TDF_DETAILS))
1914 : {
1915 2 : fprintf (dump_file, "Before TODOs.\n");
1916 2 : dump_function_to_file (current_function_decl, dump_file, dump_flags);
1917 : }
1918 :
1919 213220 : mark_virtual_operands_for_renaming (cfun);
1920 :
1921 417725 : todo |= cfg_changed ? TODO_cleanup_cfg : 0;
1922 : }
1923 :
1924 987688 : delete_worklist ();
1925 987688 : if (loop_entered)
1926 : {
1927 272599 : delete_cluster_vectors ();
1928 272599 : BITMAP_FREE (update_bbs);
1929 272599 : BITMAP_FREE (ifcombine_candidate_bbs);
1930 : }
1931 :
1932 987688 : timevar_pop (TV_TREE_TAIL_MERGE);
1933 :
1934 987688 : return todo;
1935 : }
|