1 /* Utilities for ipa analysis.
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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/>. */
23 #include "coretypes.h"
28 #include "alloc-pool.h"
30 #include "lto-streamer.h"
32 #include "splay-tree.h"
33 #include "ipa-utils.h"
34 #include "symbol-summary.h"
37 #include "ipa-fnsummary.h"
39 #include "gimple-iterator.h"
40 #include "ipa-modref-tree.h"
41 #include "ipa-modref.h"
42 #include "tree-ssa-loop-niter.h"
44 /* Debugging function for postorder and inorder code. NOTE is a string
45 that is printed before the nodes are printed. ORDER is an array of
46 cgraph_nodes that has COUNT useful nodes in it. */
49 ipa_print_order (FILE* out
,
51 struct cgraph_node
** order
,
55 fprintf (out
, "\n\n ordered call graph: %s\n", note
);
57 for (i
= count
- 1; i
>= 0; i
--)
65 struct cgraph_node
**stack
;
66 struct cgraph_node
**result
;
69 splay_tree nodes_marked_new
;
74 /* This is an implementation of Tarjan's strongly connected region
75 finder as reprinted in Aho Hopcraft and Ullman's The Design and
76 Analysis of Computer Programs (1975) pages 192-193. This version
77 has been customized for cgraph_nodes. The env parameter is because
78 it is recursive and there are no nested functions here. This
79 function should only be called from itself or
80 ipa_reduced_postorder. ENV is a stack env and would be
81 unnecessary if C had nested functions. V is the node to start
85 searchc (struct searchc_env
* env
, struct cgraph_node
*v
,
86 bool (*ignore_edge
) (struct cgraph_edge
*))
88 struct cgraph_edge
*edge
;
89 struct ipa_dfs_info
*v_info
= (struct ipa_dfs_info
*) v
->aux
;
91 /* mark node as old */
92 v_info
->new_node
= false;
93 splay_tree_remove (env
->nodes_marked_new
, v
->get_uid ());
95 v_info
->dfn_number
= env
->count
;
96 v_info
->low_link
= env
->count
;
98 env
->stack
[(env
->stack_size
)++] = v
;
99 v_info
->on_stack
= true;
101 for (edge
= v
->callees
; edge
; edge
= edge
->next_callee
)
103 struct ipa_dfs_info
* w_info
;
104 enum availability avail
;
105 struct cgraph_node
*w
= edge
->callee
->ultimate_alias_target (&avail
);
107 if (!w
|| (ignore_edge
&& ignore_edge (edge
)))
111 && (avail
>= AVAIL_INTERPOSABLE
))
113 w_info
= (struct ipa_dfs_info
*) w
->aux
;
114 if (w_info
->new_node
)
116 searchc (env
, w
, ignore_edge
);
118 (v_info
->low_link
< w_info
->low_link
) ?
119 v_info
->low_link
: w_info
->low_link
;
122 if ((w_info
->dfn_number
< v_info
->dfn_number
)
123 && (w_info
->on_stack
))
125 (w_info
->dfn_number
< v_info
->low_link
) ?
126 w_info
->dfn_number
: v_info
->low_link
;
131 if (v_info
->low_link
== v_info
->dfn_number
)
133 struct cgraph_node
*last
= NULL
;
134 struct cgraph_node
*x
;
135 struct ipa_dfs_info
*x_info
;
137 x
= env
->stack
[--(env
->stack_size
)];
138 x_info
= (struct ipa_dfs_info
*) x
->aux
;
139 x_info
->on_stack
= false;
140 x_info
->scc_no
= v_info
->dfn_number
;
144 x_info
->next_cycle
= last
;
148 env
->result
[env
->order_pos
++] = x
;
152 env
->result
[env
->order_pos
++] = v
;
156 /* Topsort the call graph by caller relation. Put the result in ORDER.
158 The REDUCE flag is true if you want the cycles reduced to single nodes.
159 You can use ipa_get_nodes_in_cycle to obtain a vector containing all real
160 call graph nodes in a reduced node.
162 Set ALLOW_OVERWRITABLE if nodes with such availability should be included.
163 IGNORE_EDGE, if non-NULL is a hook that may make some edges insignificant
164 for the topological sort. */
167 ipa_reduced_postorder (struct cgraph_node
**order
,
169 bool (*ignore_edge
) (struct cgraph_edge
*))
171 struct cgraph_node
*node
;
172 struct searchc_env env
;
173 splay_tree_node result
;
174 env
.stack
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
178 env
.nodes_marked_new
= splay_tree_new (splay_tree_compare_ints
, 0, 0);
182 FOR_EACH_DEFINED_FUNCTION (node
)
184 enum availability avail
= node
->get_availability ();
186 if (avail
> AVAIL_INTERPOSABLE
187 || avail
== AVAIL_INTERPOSABLE
)
189 /* Reuse the info if it is already there. */
190 struct ipa_dfs_info
*info
= (struct ipa_dfs_info
*) node
->aux
;
192 info
= XCNEW (struct ipa_dfs_info
);
193 info
->new_node
= true;
194 info
->on_stack
= false;
195 info
->next_cycle
= NULL
;
198 splay_tree_insert (env
.nodes_marked_new
,
199 (splay_tree_key
)node
->get_uid (),
200 (splay_tree_value
)node
);
205 result
= splay_tree_min (env
.nodes_marked_new
);
208 node
= (struct cgraph_node
*)result
->value
;
209 searchc (&env
, node
, ignore_edge
);
210 result
= splay_tree_min (env
.nodes_marked_new
);
212 splay_tree_delete (env
.nodes_marked_new
);
215 return env
.order_pos
;
218 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
222 ipa_free_postorder_info (void)
224 struct cgraph_node
*node
;
225 FOR_EACH_DEFINED_FUNCTION (node
)
227 /* Get rid of the aux information. */
236 /* Get the set of nodes for the cycle in the reduced call graph starting
240 ipa_get_nodes_in_cycle (struct cgraph_node
*node
)
242 vec
<cgraph_node
*> v
= vNULL
;
243 struct ipa_dfs_info
*node_dfs_info
;
247 node_dfs_info
= (struct ipa_dfs_info
*) node
->aux
;
248 node
= node_dfs_info
->next_cycle
;
253 /* Return true iff the CS is an edge within a strongly connected component as
254 computed by ipa_reduced_postorder. */
257 ipa_edge_within_scc (struct cgraph_edge
*cs
)
259 struct ipa_dfs_info
*caller_dfs
= (struct ipa_dfs_info
*) cs
->caller
->aux
;
260 struct ipa_dfs_info
*callee_dfs
;
261 struct cgraph_node
*callee
= cs
->callee
->function_symbol ();
263 callee_dfs
= (struct ipa_dfs_info
*) callee
->aux
;
266 && caller_dfs
->scc_no
== callee_dfs
->scc_no
);
269 struct postorder_stack
271 struct cgraph_node
*node
;
272 struct cgraph_edge
*edge
;
276 /* Fill array order with all nodes with output flag set in the reverse
277 topological order. Return the number of elements in the array.
278 FIXME: While walking, consider aliases, too. */
281 ipa_reverse_postorder (struct cgraph_node
**order
)
283 struct cgraph_node
*node
, *node2
;
286 struct cgraph_edge
*edge
;
288 struct ipa_ref
*ref
= NULL
;
290 struct postorder_stack
*stack
=
291 XCNEWVEC (struct postorder_stack
, symtab
->cgraph_count
);
293 /* We have to deal with cycles nicely, so use a depth first traversal
294 output algorithm. Ignore the fact that some functions won't need
295 to be output and put them into order as well, so we get dependencies
296 right through inline functions. */
297 FOR_EACH_FUNCTION (node
)
299 for (pass
= 0; pass
< 2; pass
++)
300 FOR_EACH_FUNCTION (node
)
303 || (!node
->address_taken
305 && !node
->alias
&& !node
->thunk
306 && !node
->only_called_directly_p ())))
309 stack
[stack_size
].node
= node
;
310 stack
[stack_size
].edge
= node
->callers
;
311 stack
[stack_size
].ref
= 0;
312 node
->aux
= (void *)(size_t)1;
313 while (stack_size
>= 0)
318 while (stack
[stack_size
].edge
&& !node2
)
320 edge
= stack
[stack_size
].edge
;
321 node2
= edge
->caller
;
322 stack
[stack_size
].edge
= edge
->next_caller
;
323 /* Break possible cycles involving always-inline
324 functions by ignoring edges from always-inline
325 functions to non-always-inline functions. */
326 if (DECL_DISREGARD_INLINE_LIMITS (edge
->caller
->decl
)
327 && !DECL_DISREGARD_INLINE_LIMITS
328 (edge
->callee
->function_symbol ()->decl
))
331 for (; stack
[stack_size
].node
->iterate_referring (
332 stack
[stack_size
].ref
,
334 stack
[stack_size
].ref
++)
336 if (ref
->use
== IPA_REF_ALIAS
)
337 node2
= dyn_cast
<cgraph_node
*> (ref
->referring
);
343 stack
[++stack_size
].node
= node2
;
344 stack
[stack_size
].edge
= node2
->callers
;
345 stack
[stack_size
].ref
= 0;
346 node2
->aux
= (void *)(size_t)1;
349 order
[order_pos
++] = stack
[stack_size
--].node
;
353 FOR_EACH_FUNCTION (node
)
360 /* Given a memory reference T, will return the variable at the bottom
361 of the access. Unlike get_base_address, this will recurse through
365 get_base_var (tree t
)
367 while (!SSA_VAR_P (t
)
368 && (!CONSTANT_CLASS_P (t
))
369 && TREE_CODE (t
) != LABEL_DECL
370 && TREE_CODE (t
) != FUNCTION_DECL
371 && TREE_CODE (t
) != CONST_DECL
372 && TREE_CODE (t
) != CONSTRUCTOR
)
374 t
= TREE_OPERAND (t
, 0);
379 /* Scale function of calls in NODE by ratio ORIG_COUNT/NODE->count. */
382 scale_ipa_profile_for_fn (struct cgraph_node
*node
, profile_count orig_count
)
384 profile_count to
= node
->count
;
385 profile_count::adjust_for_ipa_scaling (&to
, &orig_count
);
386 struct cgraph_edge
*e
;
388 for (e
= node
->callees
; e
; e
= e
->next_callee
)
389 e
->count
= e
->count
.apply_scale (to
, orig_count
);
390 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
391 e
->count
= e
->count
.apply_scale (to
, orig_count
);
394 /* SRC and DST are going to be merged. Take SRC's profile and merge it into
395 DST so it is not going to be lost. Possibly destroy SRC's body on the way
396 unless PRESERVE_BODY is set. */
399 ipa_merge_profiles (struct cgraph_node
*dst
,
400 struct cgraph_node
*src
,
403 tree oldsrcdecl
= src
->decl
;
404 struct function
*srccfun
, *dstcfun
;
406 bool copy_counts
= false;
412 if (src
->frequency
< dst
->frequency
)
413 src
->frequency
= dst
->frequency
;
415 /* Time profiles are merged. */
416 if (dst
->tp_first_run
> src
->tp_first_run
&& src
->tp_first_run
)
417 dst
->tp_first_run
= src
->tp_first_run
;
419 if (src
->profile_id
&& !dst
->profile_id
)
420 dst
->profile_id
= src
->profile_id
;
422 /* Merging zero profile to dst is no-op. */
423 if (src
->count
.ipa () == profile_count::zero ())
426 /* FIXME when we merge in unknown profile, we ought to set counts as
428 if (!src
->count
.initialized_p ()
429 || !(src
->count
.ipa () == src
->count
))
431 profile_count orig_count
= dst
->count
;
433 /* Either sum the profiles if both are IPA and not global0, or
434 pick more informative one (that is nonzero IPA if other is
435 uninitialized, guessed or global0). */
437 if ((dst
->count
.ipa ().nonzero_p ()
438 || src
->count
.ipa ().nonzero_p ())
439 && dst
->count
.ipa ().initialized_p ()
440 && src
->count
.ipa ().initialized_p ())
441 dst
->count
= dst
->count
.ipa () + src
->count
.ipa ();
442 else if (dst
->count
.ipa ().initialized_p ())
444 else if (src
->count
.ipa ().initialized_p ())
447 dst
->count
= src
->count
.ipa ();
450 /* If no updating needed return early. */
451 if (dst
->count
== orig_count
)
454 if (symtab
->dump_file
)
456 fprintf (symtab
->dump_file
, "Merging profiles of %s count:",
458 src
->count
.dump (symtab
->dump_file
);
459 fprintf (symtab
->dump_file
, " to %s count:",
461 orig_count
.dump (symtab
->dump_file
);
462 fprintf (symtab
->dump_file
, " resulting count:");
463 dst
->count
.dump (symtab
->dump_file
);
464 fprintf (symtab
->dump_file
, "\n");
467 /* First handle functions with no gimple body. */
468 if (dst
->thunk
|| dst
->alias
469 || src
->thunk
|| src
->alias
)
471 scale_ipa_profile_for_fn (dst
, orig_count
);
475 /* This is ugly. We need to get both function bodies into memory.
476 If declaration is merged, we need to duplicate it to be able
477 to load body that is being replaced. This makes symbol table
478 temporarily inconsistent. */
479 if (src
->decl
== dst
->decl
)
481 struct lto_in_decl_state temp
;
482 struct lto_in_decl_state
*state
;
484 /* We are going to move the decl, we want to remove its file decl data.
485 and link these with the new decl. */
486 temp
.fn_decl
= src
->decl
;
487 lto_in_decl_state
**slot
488 = src
->lto_file_data
->function_decl_states
->find_slot (&temp
,
491 src
->lto_file_data
->function_decl_states
->clear_slot (slot
);
494 /* Duplicate the decl and be sure it does not link into body of DST. */
495 src
->decl
= copy_node (src
->decl
);
496 DECL_STRUCT_FUNCTION (src
->decl
) = NULL
;
497 DECL_ARGUMENTS (src
->decl
) = NULL
;
498 DECL_INITIAL (src
->decl
) = NULL
;
499 DECL_RESULT (src
->decl
) = NULL
;
501 /* Associate the decl state with new declaration, so LTO streamer
503 state
->fn_decl
= src
->decl
;
505 = src
->lto_file_data
->function_decl_states
->find_slot (state
, INSERT
);
509 src
->get_untransformed_body ();
510 dst
->get_untransformed_body ();
511 srccfun
= DECL_STRUCT_FUNCTION (src
->decl
);
512 dstcfun
= DECL_STRUCT_FUNCTION (dst
->decl
);
513 if (n_basic_blocks_for_fn (srccfun
)
514 != n_basic_blocks_for_fn (dstcfun
))
516 if (symtab
->dump_file
)
517 fprintf (symtab
->dump_file
,
518 "Giving up; number of basic block mismatch.\n");
521 else if (last_basic_block_for_fn (srccfun
)
522 != last_basic_block_for_fn (dstcfun
))
524 if (symtab
->dump_file
)
525 fprintf (symtab
->dump_file
,
526 "Giving up; last block mismatch.\n");
531 basic_block srcbb
, dstbb
;
532 struct cgraph_edge
*e
, *e2
;
534 for (e
= dst
->callees
, e2
= src
->callees
; e
&& e2
&& match
;
535 e2
= e2
->next_callee
, e
= e
->next_callee
)
537 if (gimple_bb (e
->call_stmt
)->index
538 != gimple_bb (e2
->call_stmt
)->index
)
540 if (symtab
->dump_file
)
541 fprintf (symtab
->dump_file
,
542 "Giving up; call stmt mismatch.\n");
548 if (symtab
->dump_file
)
549 fprintf (symtab
->dump_file
,
550 "Giving up; number of calls differs.\n");
553 for (e
= dst
->indirect_calls
, e2
= src
->indirect_calls
; e
&& e2
&& match
;
554 e2
= e2
->next_callee
, e
= e
->next_callee
)
556 if (gimple_bb (e
->call_stmt
)->index
557 != gimple_bb (e2
->call_stmt
)->index
)
559 if (symtab
->dump_file
)
560 fprintf (symtab
->dump_file
,
561 "Giving up; indirect call stmt mismatch.\n");
567 if (symtab
->dump_file
)
568 fprintf (symtab
->dump_file
,
569 "Giving up; number of indirect calls differs.\n");
574 FOR_ALL_BB_FN (srcbb
, srccfun
)
578 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
581 if (symtab
->dump_file
)
582 fprintf (symtab
->dump_file
,
583 "No matching block for bb %i.\n",
588 if (EDGE_COUNT (srcbb
->succs
) != EDGE_COUNT (dstbb
->succs
))
590 if (symtab
->dump_file
)
591 fprintf (symtab
->dump_file
,
592 "Edge count mismatch for bb %i.\n",
597 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
599 edge srce
= EDGE_SUCC (srcbb
, i
);
600 edge dste
= EDGE_SUCC (dstbb
, i
);
601 if (srce
->dest
->index
!= dste
->dest
->index
)
603 if (symtab
->dump_file
)
604 fprintf (symtab
->dump_file
,
605 "Succ edge mismatch for bb %i.\n",
615 struct cgraph_edge
*e
, *e2
;
616 basic_block srcbb
, dstbb
;
618 /* Function and global profile may be out of sync. First scale it same
619 way as fixup_cfg would. */
620 profile_count srcnum
= src
->count
;
621 profile_count srcden
= ENTRY_BLOCK_PTR_FOR_FN (srccfun
)->count
;
622 bool srcscale
= srcnum
.initialized_p () && !(srcnum
== srcden
);
623 profile_count dstnum
= orig_count
;
624 profile_count dstden
= ENTRY_BLOCK_PTR_FOR_FN (dstcfun
)->count
;
625 bool dstscale
= !copy_counts
626 && dstnum
.initialized_p () && !(dstnum
== dstden
);
628 /* TODO: merge also statement histograms. */
629 FOR_ALL_BB_FN (srcbb
, srccfun
)
633 dstbb
= BASIC_BLOCK_FOR_FN (dstcfun
, srcbb
->index
);
635 profile_count srccount
= srcbb
->count
;
637 srccount
= srccount
.apply_scale (srcnum
, srcden
);
639 dstbb
->count
= dstbb
->count
.apply_scale (dstnum
, dstden
);
643 dstbb
->count
= srccount
;
644 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
646 edge srce
= EDGE_SUCC (srcbb
, i
);
647 edge dste
= EDGE_SUCC (dstbb
, i
);
648 if (srce
->probability
.initialized_p ())
649 dste
->probability
= srce
->probability
;
654 for (i
= 0; i
< EDGE_COUNT (srcbb
->succs
); i
++)
656 edge srce
= EDGE_SUCC (srcbb
, i
);
657 edge dste
= EDGE_SUCC (dstbb
, i
);
659 dste
->probability
* dstbb
->count
.ipa ().probability_in
662 + srce
->probability
* srcbb
->count
.ipa ().probability_in
666 dstbb
->count
= dstbb
->count
.ipa () + srccount
.ipa ();
670 update_max_bb_count ();
671 compute_function_frequency ();
673 for (e
= dst
->callees
; e
; e
= e
->next_callee
)
677 e
->count
= gimple_bb (e
->call_stmt
)->count
;
679 for (e
= dst
->indirect_calls
, e2
= src
->indirect_calls
; e
;
680 e2
= (e2
? e2
->next_callee
: NULL
), e
= e
->next_callee
)
682 if (!e
->speculative
&& !e2
->speculative
)
684 /* FIXME: we need to also merge ipa-profile histograms
685 because with LTO merging happens from lto-symtab before
686 these are converted to indirect edges. */
687 e
->count
= gimple_bb (e
->call_stmt
)->count
;
691 /* When copying just remove all speuclations on dst and then copy
695 while (e
->speculative
)
696 cgraph_edge::resolve_speculation (e
, NULL
);
697 e
->count
= gimple_bb (e
->call_stmt
)->count
;
700 for (cgraph_edge
*e3
= e2
->first_speculative_call_target ();
702 e3
= e3
->next_speculative_call_target ())
705 ns
= e
->make_speculative
706 (dyn_cast
<cgraph_node
*>
707 (e3
->speculative_call_target_ref ()->referred
),
708 e3
->count
, e3
->speculative_id
);
709 /* Target may differ from ref (for example it may be
710 redirected to local alias. */
711 ns
->redirect_callee (e3
->callee
);
717 /* Iterate all speculations in SRC, see if corresponding ones exist
718 int DST and if so, sum the counts. Otherwise create new
721 for (cgraph_edge
*e3
= e
->first_speculative_call_target ();
723 e3
= e3
->next_speculative_call_target ())
724 if (e3
->speculative_id
> max_spec
)
725 max_spec
= e3
->speculative_id
;
726 for (cgraph_edge
*e3
= e2
->first_speculative_call_target ();
728 e3
= e3
->next_speculative_call_target ())
731 = e
->speculative_call_for_target
732 (dyn_cast
<cgraph_node
*>
733 (e3
->speculative_call_target_ref ()->referred
));
735 te
->count
= te
->count
+ e3
->count
;
738 e
->count
= e
->count
+ e3
->count
;
740 ns
= e
->make_speculative
741 (dyn_cast
<cgraph_node
*>
742 (e3
->speculative_call_target_ref ()
745 e3
->speculative_id
+ max_spec
+ 1);
746 /* Target may differ from ref (for example it may be
747 redirected to local alias. */
748 ns
->redirect_callee (e3
->callee
);
753 src
->release_body ();
754 /* Update summary. */
755 compute_fn_summary (dst
, 0);
757 /* We can't update CFG profile, but we can scale IPA profile. CFG
758 will be scaled according to dst->count after IPA passes. */
760 scale_ipa_profile_for_fn (dst
, orig_count
);
761 src
->decl
= oldsrcdecl
;
764 /* Return true if call to DEST is known to be self-recusive
765 call withing FUNC. */
768 recursive_call_p (tree func
, tree dest
)
770 struct cgraph_node
*dest_node
= cgraph_node::get_create (dest
);
771 struct cgraph_node
*cnode
= cgraph_node::get_create (func
);
773 enum availability avail
;
775 gcc_assert (!cnode
->alias
);
776 if (cnode
!= dest_node
->ultimate_alias_target (&avail
))
778 if (avail
>= AVAIL_AVAILABLE
)
780 if (!dest_node
->semantically_equivalent_p (cnode
))
782 /* If there is only one way to call the fuction or we know all of them
783 are semantically equivalent, we still can consider call recursive. */
784 FOR_EACH_ALIAS (cnode
, alias
)
785 if (!dest_node
->semantically_equivalent_p (alias
->referring
))
790 /* Return true if stmt may terminate execution of function.
791 If assume_return_or_eh we can further assume that the function ends
792 either by retrn statement or EH (no trapping or infinite loops). */
795 stmt_may_terminate_function_p (function
*fun
, gimple
*stmt
, bool assume_return_or_eh
)
797 if (stmt_can_throw_external (fun
, stmt
))
799 gasm
*astmt
= dyn_cast
<gasm
*> (stmt
);
800 if (astmt
&& gimple_asm_volatile_p (astmt
))
802 if (assume_return_or_eh
)
804 if (gimple_could_trap_p (stmt
))
806 if (gcall
*call
= dyn_cast
<gcall
*> (stmt
))
808 int flags
= gimple_call_flags (call
);
809 if (flags
& (ECF_PURE
| ECF_CONST
) && ! (flags
& ECF_LOOPING_CONST_OR_PURE
))
811 modref_summary
*s
= get_modref_function_summary (call
, NULL
);
812 if (s
&& !s
->side_effects
)
819 /* Return bitmap of all basic blocks whose first statements are known to
820 execute on every invocation of the function.
822 If assume_return_or_eh we can further assume that the function ends
823 either by retrn statement or EH (no trapping or infinite loops).
824 This is useful when sumarizing function in passes like ipa-modref.
826 Seeing assume_return_or_eh to false is used to prove that given
827 statmeent will be executed even if the function gets into infinite
830 find_always_executed_bbs (function
*fun
, bool assume_return_or_eh
)
832 auto_vec
<basic_block
, 20> stack
;
833 auto_vec
<basic_block
, 20> terminating_bbs
;
834 hash_set
<basic_block
> visited
;
838 /* First walk all BBs reachable from entry stopping on statements that may
839 terminate execution. Everything past this statement is not going to be executed
841 stack
.safe_push (ENTRY_BLOCK_PTR_FOR_FN (fun
));
842 while (!stack
.is_empty ())
844 basic_block bb
= stack
.pop ();
845 bool found
= false, found_exit
= false;
846 if (!assume_return_or_eh
847 && (EDGE_COUNT (bb
->succs
) == 0 || (bb
->flags
& BB_IRREDUCIBLE_LOOP
)))
849 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
851 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (fun
))
856 /* Watch for infinite loops. */
857 if (!found
&& (assume_return_or_eh
& EDGE_DFS_BACK
)
858 && !finite_loop_p (e
->src
->loop_father
))
861 for (gimple_stmt_iterator si
= gsi_start_nondebug_after_labels_bb (bb
);
862 !gsi_end_p (si
) && !found
; gsi_next_nondebug (&si
))
863 if (stmt_may_terminate_function_p (fun
, gsi_stmt (si
), assume_return_or_eh
))
870 visited
.add (EXIT_BLOCK_PTR_FOR_FN (fun
));
872 terminating_bbs
.safe_push (bb
);
875 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
876 if (!visited
.add (e
->dest
))
877 stack
.safe_push (e
->dest
);
880 /* Next walk from exit block and find all articulations in the CFG.
881 Add all terminating basic blocks as "fake" predecessors of the
884 bitmap ret
= BITMAP_ALLOC (NULL
);
885 /* A degenerated case when there is no path to exit. */
886 if (!visited
.contains (EXIT_BLOCK_PTR_FOR_FN (fun
))
887 && terminating_bbs
.is_empty ())
891 (ENTRY_BLOCK_PTR_FOR_FN (fun
))->dest
->index
);
897 unsigned int dfs_preorder
;
898 unsigned int dfs_postorder
;
900 unsigned int low
, high
;
909 struct obstack state_obstack
;
910 gcc_obstack_init (&state_obstack
);
911 hash_map
<basic_block
, astate
*> state
;
912 auto_vec
<worklist
, 32> worklist_vec
;
913 unsigned int next_dfs_num
= 1;
915 /* Always executed blocks are blocks that are on every path from entry to exit.
916 We proceed in two steps. First we do backward DFS walk (so we know that entry
917 is always reached) and record preorder and postorder visiting times.
919 In second step we proceed in postorder and for every block A we compute
920 minimal preorder (A.low) and maximal postorder (A.high) of block reachable
921 from the BBs in DFS subtree of A. If A is always executed there are no
922 edges out of this subtree. This can be tested by checking that A.low == A.preorder
923 and B.high == A.postorder.
925 This is first step. Do backward DFS walk and record preorder, postorder
926 and predecessor info. Initialize stack in postorder. */
927 worklist we
= {EXIT_BLOCK_PTR_FOR_FN (fun
), NULL
};
928 worklist_vec
.safe_push (we
);
929 while (!worklist_vec
.is_empty ())
931 worklist
&w
= worklist_vec
.last ();
932 basic_block bb
= w
.bb
;
933 astate
*cstate
= w
.cstate
;
937 astate
**slot
= &state
.get_or_insert (bb
);
940 /* Already processed by DFS? */
946 /* DFS is visiting BB for first time. */
947 *slot
= cstate
= XOBNEW (&state_obstack
, struct astate
);
948 cstate
->low
= cstate
->dfs_preorder
= next_dfs_num
++;
950 /* Exit block is special; process all fake edges we identified. */
951 if (bb
== EXIT_BLOCK_PTR_FOR_FN (fun
))
952 for (basic_block bb2
: terminating_bbs
)
954 worklist we
= {bb2
, NULL
};
955 worklist_vec
.safe_push (we
);
957 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
958 if (visited
.contains (e
->src
))
960 worklist we
= {e
->src
, NULL
};
961 worklist_vec
.safe_push (we
);
963 /* Keep BB on worklist so we process it last time. */
966 /* We are finished with processing reachable BBs, see if we have articulation. */
968 cstate
->high
= cstate
->dfs_postorder
= next_dfs_num
++;
969 stack
.safe_push (bb
);
971 /* This is the final postorder walk. Determine low and high values and mark
972 always executed blocks. */
973 for (basic_block bb
: stack
)
975 astate
*cstate
= *state
.get (bb
);
976 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
978 astate
**cstate2
= state
.get (e
->src
);
979 /* We skip walking part of CFG reached only after first edge to exit.
980 No BB reachable from the skipped part is always executed */
983 if (e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (fun
))
987 cstate
->low
= MIN (cstate
->low
, (*cstate2
)->low
);
988 cstate
->high
= MAX (cstate
->high
, (*cstate2
)->high
);
990 if (cstate
->low
== cstate
->dfs_preorder
&& cstate
->high
== cstate
->dfs_postorder
991 && bb
!= EXIT_BLOCK_PTR_FOR_FN (fun
))
992 bitmap_set_bit (ret
, bb
->index
);
993 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
995 astate
**cstate2
= state
.get (e
->dest
);
998 cstate
->low
= MIN (cstate
->low
, (*cstate2
)->low
);
999 cstate
->high
= MAX (cstate
->high
, (*cstate2
)->high
);
1002 obstack_free (&state_obstack
, NULL
);