]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ipa-utils.cc
Fix wrong code issues with ipa-sra
[thirdparty/gcc.git] / gcc / ipa-utils.cc
1 /* Utilities for ipa analysis.
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4
5 This file is part of GCC.
6
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
10 version.
11
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
15 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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "predict.h"
28 #include "alloc-pool.h"
29 #include "cgraph.h"
30 #include "lto-streamer.h"
31 #include "dumpfile.h"
32 #include "splay-tree.h"
33 #include "ipa-utils.h"
34 #include "symbol-summary.h"
35 #include "tree-vrp.h"
36 #include "ipa-prop.h"
37 #include "ipa-fnsummary.h"
38 #include "tree-eh.h"
39 #include "gimple-iterator.h"
40 #include "ipa-modref-tree.h"
41 #include "ipa-modref.h"
42 #include "tree-ssa-loop-niter.h"
43
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. */
47
48 void
49 ipa_print_order (FILE* out,
50 const char * note,
51 struct cgraph_node** order,
52 int count)
53 {
54 int i;
55 fprintf (out, "\n\n ordered call graph: %s\n", note);
56
57 for (i = count - 1; i >= 0; i--)
58 order[i]->dump (out);
59 fprintf (out, "\n");
60 fflush (out);
61 }
62
63
64 struct searchc_env {
65 struct cgraph_node **stack;
66 struct cgraph_node **result;
67 int stack_size;
68 int order_pos;
69 splay_tree nodes_marked_new;
70 bool reduce;
71 int count;
72 };
73
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
82 searching from. */
83
84 static void
85 searchc (struct searchc_env* env, struct cgraph_node *v,
86 bool (*ignore_edge) (struct cgraph_edge *))
87 {
88 struct cgraph_edge *edge;
89 struct ipa_dfs_info *v_info = (struct ipa_dfs_info *) v->aux;
90
91 /* mark node as old */
92 v_info->new_node = false;
93 splay_tree_remove (env->nodes_marked_new, v->get_uid ());
94
95 v_info->dfn_number = env->count;
96 v_info->low_link = env->count;
97 env->count++;
98 env->stack[(env->stack_size)++] = v;
99 v_info->on_stack = true;
100
101 for (edge = v->callees; edge; edge = edge->next_callee)
102 {
103 struct ipa_dfs_info * w_info;
104 enum availability avail;
105 struct cgraph_node *w = edge->callee->ultimate_alias_target (&avail);
106
107 if (!w || (ignore_edge && ignore_edge (edge)))
108 continue;
109
110 if (w->aux
111 && (avail >= AVAIL_INTERPOSABLE))
112 {
113 w_info = (struct ipa_dfs_info *) w->aux;
114 if (w_info->new_node)
115 {
116 searchc (env, w, ignore_edge);
117 v_info->low_link =
118 (v_info->low_link < w_info->low_link) ?
119 v_info->low_link : w_info->low_link;
120 }
121 else
122 if ((w_info->dfn_number < v_info->dfn_number)
123 && (w_info->on_stack))
124 v_info->low_link =
125 (w_info->dfn_number < v_info->low_link) ?
126 w_info->dfn_number : v_info->low_link;
127 }
128 }
129
130
131 if (v_info->low_link == v_info->dfn_number)
132 {
133 struct cgraph_node *last = NULL;
134 struct cgraph_node *x;
135 struct ipa_dfs_info *x_info;
136 do {
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;
141
142 if (env->reduce)
143 {
144 x_info->next_cycle = last;
145 last = x;
146 }
147 else
148 env->result[env->order_pos++] = x;
149 }
150 while (v != x);
151 if (env->reduce)
152 env->result[env->order_pos++] = v;
153 }
154 }
155
156 /* Topsort the call graph by caller relation. Put the result in ORDER.
157
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.
161
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. */
165
166 int
167 ipa_reduced_postorder (struct cgraph_node **order,
168 bool reduce,
169 bool (*ignore_edge) (struct cgraph_edge *))
170 {
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);
175 env.stack_size = 0;
176 env.result = order;
177 env.order_pos = 0;
178 env.nodes_marked_new = splay_tree_new (splay_tree_compare_ints, 0, 0);
179 env.count = 1;
180 env.reduce = reduce;
181
182 FOR_EACH_DEFINED_FUNCTION (node)
183 {
184 enum availability avail = node->get_availability ();
185
186 if (avail > AVAIL_INTERPOSABLE
187 || avail == AVAIL_INTERPOSABLE)
188 {
189 /* Reuse the info if it is already there. */
190 struct ipa_dfs_info *info = (struct ipa_dfs_info *) node->aux;
191 if (!info)
192 info = XCNEW (struct ipa_dfs_info);
193 info->new_node = true;
194 info->on_stack = false;
195 info->next_cycle = NULL;
196 node->aux = info;
197
198 splay_tree_insert (env.nodes_marked_new,
199 (splay_tree_key)node->get_uid (),
200 (splay_tree_value)node);
201 }
202 else
203 node->aux = NULL;
204 }
205 result = splay_tree_min (env.nodes_marked_new);
206 while (result)
207 {
208 node = (struct cgraph_node *)result->value;
209 searchc (&env, node, ignore_edge);
210 result = splay_tree_min (env.nodes_marked_new);
211 }
212 splay_tree_delete (env.nodes_marked_new);
213 free (env.stack);
214
215 return env.order_pos;
216 }
217
218 /* Deallocate all ipa_dfs_info structures pointed to by the aux pointer of call
219 graph nodes. */
220
221 void
222 ipa_free_postorder_info (void)
223 {
224 struct cgraph_node *node;
225 FOR_EACH_DEFINED_FUNCTION (node)
226 {
227 /* Get rid of the aux information. */
228 if (node->aux)
229 {
230 free (node->aux);
231 node->aux = NULL;
232 }
233 }
234 }
235
236 /* Get the set of nodes for the cycle in the reduced call graph starting
237 from NODE. */
238
239 vec<cgraph_node *>
240 ipa_get_nodes_in_cycle (struct cgraph_node *node)
241 {
242 vec<cgraph_node *> v = vNULL;
243 struct ipa_dfs_info *node_dfs_info;
244 while (node)
245 {
246 v.safe_push (node);
247 node_dfs_info = (struct ipa_dfs_info *) node->aux;
248 node = node_dfs_info->next_cycle;
249 }
250 return v;
251 }
252
253 /* Return true iff the CS is an edge within a strongly connected component as
254 computed by ipa_reduced_postorder. */
255
256 bool
257 ipa_edge_within_scc (struct cgraph_edge *cs)
258 {
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 ();
262
263 callee_dfs = (struct ipa_dfs_info *) callee->aux;
264 return (caller_dfs
265 && callee_dfs
266 && caller_dfs->scc_no == callee_dfs->scc_no);
267 }
268
269 struct postorder_stack
270 {
271 struct cgraph_node *node;
272 struct cgraph_edge *edge;
273 int ref;
274 };
275
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. */
279
280 int
281 ipa_reverse_postorder (struct cgraph_node **order)
282 {
283 struct cgraph_node *node, *node2;
284 int stack_size = 0;
285 int order_pos = 0;
286 struct cgraph_edge *edge;
287 int pass;
288 struct ipa_ref *ref = NULL;
289
290 struct postorder_stack *stack =
291 XCNEWVEC (struct postorder_stack, symtab->cgraph_count);
292
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)
298 node->aux = NULL;
299 for (pass = 0; pass < 2; pass++)
300 FOR_EACH_FUNCTION (node)
301 if (!node->aux
302 && (pass
303 || (!node->address_taken
304 && !node->inlined_to
305 && !node->alias && !node->thunk
306 && !node->only_called_directly_p ())))
307 {
308 stack_size = 0;
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)
314 {
315 while (true)
316 {
317 node2 = NULL;
318 while (stack[stack_size].edge && !node2)
319 {
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))
329 node2 = NULL;
330 }
331 for (; stack[stack_size].node->iterate_referring (
332 stack[stack_size].ref,
333 ref) && !node2;
334 stack[stack_size].ref++)
335 {
336 if (ref->use == IPA_REF_ALIAS)
337 node2 = dyn_cast <cgraph_node *> (ref->referring);
338 }
339 if (!node2)
340 break;
341 if (!node2->aux)
342 {
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;
347 }
348 }
349 order[order_pos++] = stack[stack_size--].node;
350 }
351 }
352 free (stack);
353 FOR_EACH_FUNCTION (node)
354 node->aux = NULL;
355 return order_pos;
356 }
357
358
359
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
362 INDIRECT_REFS. */
363
364 tree
365 get_base_var (tree t)
366 {
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)
373 {
374 t = TREE_OPERAND (t, 0);
375 }
376 return t;
377 }
378
379 /* Scale function of calls in NODE by ratio ORIG_COUNT/NODE->count. */
380
381 void
382 scale_ipa_profile_for_fn (struct cgraph_node *node, profile_count orig_count)
383 {
384 profile_count to = node->count;
385 profile_count::adjust_for_ipa_scaling (&to, &orig_count);
386 struct cgraph_edge *e;
387
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);
392 }
393
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. */
397
398 void
399 ipa_merge_profiles (struct cgraph_node *dst,
400 struct cgraph_node *src,
401 bool preserve_body)
402 {
403 tree oldsrcdecl = src->decl;
404 struct function *srccfun, *dstcfun;
405 bool match = true;
406 bool copy_counts = false;
407
408 if (!src->definition
409 || !dst->definition)
410 return;
411
412 if (src->frequency < dst->frequency)
413 src->frequency = dst->frequency;
414
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;
418
419 if (src->profile_id && !dst->profile_id)
420 dst->profile_id = src->profile_id;
421
422 /* Merging zero profile to dst is no-op. */
423 if (src->count.ipa () == profile_count::zero ())
424 return;
425
426 /* FIXME when we merge in unknown profile, we ought to set counts as
427 unsafe. */
428 if (!src->count.initialized_p ()
429 || !(src->count.ipa () == src->count))
430 return;
431 profile_count orig_count = dst->count;
432
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). */
436
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 ())
443 ;
444 else if (src->count.ipa ().initialized_p ())
445 {
446 copy_counts = true;
447 dst->count = src->count.ipa ();
448 }
449
450 /* If no updating needed return early. */
451 if (dst->count == orig_count)
452 return;
453
454 if (symtab->dump_file)
455 {
456 fprintf (symtab->dump_file, "Merging profiles of %s count:",
457 src->dump_name ());
458 src->count.dump (symtab->dump_file);
459 fprintf (symtab->dump_file, " to %s count:",
460 dst->dump_name ());
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");
465 }
466
467 /* First handle functions with no gimple body. */
468 if (dst->thunk || dst->alias
469 || src->thunk || src->alias)
470 {
471 scale_ipa_profile_for_fn (dst, orig_count);
472 return;
473 }
474
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)
480 {
481 struct lto_in_decl_state temp;
482 struct lto_in_decl_state *state;
483
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,
489 NO_INSERT);
490 state = *slot;
491 src->lto_file_data->function_decl_states->clear_slot (slot);
492 gcc_assert (state);
493
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;
500
501 /* Associate the decl state with new declaration, so LTO streamer
502 can look it up. */
503 state->fn_decl = src->decl;
504 slot
505 = src->lto_file_data->function_decl_states->find_slot (state, INSERT);
506 gcc_assert (!*slot);
507 *slot = state;
508 }
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))
515 {
516 if (symtab->dump_file)
517 fprintf (symtab->dump_file,
518 "Giving up; number of basic block mismatch.\n");
519 match = false;
520 }
521 else if (last_basic_block_for_fn (srccfun)
522 != last_basic_block_for_fn (dstcfun))
523 {
524 if (symtab->dump_file)
525 fprintf (symtab->dump_file,
526 "Giving up; last block mismatch.\n");
527 match = false;
528 }
529 else
530 {
531 basic_block srcbb, dstbb;
532 struct cgraph_edge *e, *e2;
533
534 for (e = dst->callees, e2 = src->callees; e && e2 && match;
535 e2 = e2->next_callee, e = e->next_callee)
536 {
537 if (gimple_bb (e->call_stmt)->index
538 != gimple_bb (e2->call_stmt)->index)
539 {
540 if (symtab->dump_file)
541 fprintf (symtab->dump_file,
542 "Giving up; call stmt mismatch.\n");
543 match = false;
544 }
545 }
546 if (e || e2)
547 {
548 if (symtab->dump_file)
549 fprintf (symtab->dump_file,
550 "Giving up; number of calls differs.\n");
551 match = false;
552 }
553 for (e = dst->indirect_calls, e2 = src->indirect_calls; e && e2 && match;
554 e2 = e2->next_callee, e = e->next_callee)
555 {
556 if (gimple_bb (e->call_stmt)->index
557 != gimple_bb (e2->call_stmt)->index)
558 {
559 if (symtab->dump_file)
560 fprintf (symtab->dump_file,
561 "Giving up; indirect call stmt mismatch.\n");
562 match = false;
563 }
564 }
565 if (e || e2)
566 {
567 if (symtab->dump_file)
568 fprintf (symtab->dump_file,
569 "Giving up; number of indirect calls differs.\n");
570 match=false;
571 }
572
573 if (match)
574 FOR_ALL_BB_FN (srcbb, srccfun)
575 {
576 unsigned int i;
577
578 dstbb = BASIC_BLOCK_FOR_FN (dstcfun, srcbb->index);
579 if (dstbb == NULL)
580 {
581 if (symtab->dump_file)
582 fprintf (symtab->dump_file,
583 "No matching block for bb %i.\n",
584 srcbb->index);
585 match = false;
586 break;
587 }
588 if (EDGE_COUNT (srcbb->succs) != EDGE_COUNT (dstbb->succs))
589 {
590 if (symtab->dump_file)
591 fprintf (symtab->dump_file,
592 "Edge count mismatch for bb %i.\n",
593 srcbb->index);
594 match = false;
595 break;
596 }
597 for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
598 {
599 edge srce = EDGE_SUCC (srcbb, i);
600 edge dste = EDGE_SUCC (dstbb, i);
601 if (srce->dest->index != dste->dest->index)
602 {
603 if (symtab->dump_file)
604 fprintf (symtab->dump_file,
605 "Succ edge mismatch for bb %i.\n",
606 srce->dest->index);
607 match = false;
608 break;
609 }
610 }
611 }
612 }
613 if (match)
614 {
615 struct cgraph_edge *e, *e2;
616 basic_block srcbb, dstbb;
617
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);
627
628 /* TODO: merge also statement histograms. */
629 FOR_ALL_BB_FN (srcbb, srccfun)
630 {
631 unsigned int i;
632
633 dstbb = BASIC_BLOCK_FOR_FN (dstcfun, srcbb->index);
634
635 profile_count srccount = srcbb->count;
636 if (srcscale)
637 srccount = srccount.apply_scale (srcnum, srcden);
638 if (dstscale)
639 dstbb->count = dstbb->count.apply_scale (dstnum, dstden);
640
641 if (copy_counts)
642 {
643 dstbb->count = srccount;
644 for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
645 {
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;
650 }
651 }
652 else
653 {
654 for (i = 0; i < EDGE_COUNT (srcbb->succs); i++)
655 {
656 edge srce = EDGE_SUCC (srcbb, i);
657 edge dste = EDGE_SUCC (dstbb, i);
658 dste->probability =
659 dste->probability * dstbb->count.ipa ().probability_in
660 (dstbb->count.ipa ()
661 + srccount.ipa ())
662 + srce->probability * srcbb->count.ipa ().probability_in
663 (dstbb->count.ipa ()
664 + srccount.ipa ());
665 }
666 dstbb->count = dstbb->count.ipa () + srccount.ipa ();
667 }
668 }
669 push_cfun (dstcfun);
670 update_max_bb_count ();
671 compute_function_frequency ();
672 pop_cfun ();
673 for (e = dst->callees; e; e = e->next_callee)
674 {
675 if (e->speculative)
676 continue;
677 e->count = gimple_bb (e->call_stmt)->count;
678 }
679 for (e = dst->indirect_calls, e2 = src->indirect_calls; e;
680 e2 = (e2 ? e2->next_callee : NULL), e = e->next_callee)
681 {
682 if (!e->speculative && !e2->speculative)
683 {
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;
688 continue;
689 }
690
691 /* When copying just remove all speuclations on dst and then copy
692 one from src. */
693 if (copy_counts)
694 {
695 while (e->speculative)
696 cgraph_edge::resolve_speculation (e, NULL);
697 e->count = gimple_bb (e->call_stmt)->count;
698 if (e2->speculative)
699 {
700 for (cgraph_edge *e3 = e2->first_speculative_call_target ();
701 e3;
702 e3 = e3->next_speculative_call_target ())
703 {
704 cgraph_edge *ns;
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);
712 }
713 }
714 continue;
715 }
716
717 /* Iterate all speculations in SRC, see if corresponding ones exist
718 int DST and if so, sum the counts. Otherwise create new
719 speculation. */
720 int max_spec = 0;
721 for (cgraph_edge *e3 = e->first_speculative_call_target ();
722 e3;
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 ();
727 e3;
728 e3 = e3->next_speculative_call_target ())
729 {
730 cgraph_edge *te
731 = e->speculative_call_for_target
732 (dyn_cast <cgraph_node *>
733 (e3->speculative_call_target_ref ()->referred));
734 if (te)
735 te->count = te->count + e3->count;
736 else
737 {
738 e->count = e->count + e3->count;
739 cgraph_edge *ns;
740 ns = e->make_speculative
741 (dyn_cast <cgraph_node *>
742 (e3->speculative_call_target_ref ()
743 ->referred),
744 e3->count,
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);
749 }
750 }
751 }
752 if (!preserve_body)
753 src->release_body ();
754 /* Update summary. */
755 compute_fn_summary (dst, 0);
756 }
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. */
759 else
760 scale_ipa_profile_for_fn (dst, orig_count);
761 src->decl = oldsrcdecl;
762 }
763
764 /* Return true if call to DEST is known to be self-recusive
765 call withing FUNC. */
766
767 bool
768 recursive_call_p (tree func, tree dest)
769 {
770 struct cgraph_node *dest_node = cgraph_node::get_create (dest);
771 struct cgraph_node *cnode = cgraph_node::get_create (func);
772 ipa_ref *alias;
773 enum availability avail;
774
775 gcc_assert (!cnode->alias);
776 if (cnode != dest_node->ultimate_alias_target (&avail))
777 return false;
778 if (avail >= AVAIL_AVAILABLE)
779 return true;
780 if (!dest_node->semantically_equivalent_p (cnode))
781 return false;
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))
786 return false;
787 return true;
788 }
789
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). */
793
794 bool
795 stmt_may_terminate_function_p (function *fun, gimple *stmt, bool assume_return_or_eh)
796 {
797 if (stmt_can_throw_external (fun, stmt))
798 return true;
799 gasm *astmt = dyn_cast <gasm *> (stmt);
800 if (astmt && gimple_asm_volatile_p (astmt))
801 return true;
802 if (assume_return_or_eh)
803 return false;
804 if (gimple_could_trap_p (stmt))
805 return true;
806 if (gcall *call = dyn_cast <gcall *> (stmt))
807 {
808 int flags = gimple_call_flags (call);
809 if (flags & (ECF_PURE | ECF_CONST) && ! (flags & ECF_LOOPING_CONST_OR_PURE))
810 return false;
811 modref_summary *s = get_modref_function_summary (call, NULL);
812 if (s && !s->side_effects)
813 return false;
814 return true;
815 }
816 return false;
817 }
818
819 /* Return bitmap of all basic blocks whose first statements are known to
820 execute on every invocation of the function.
821
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.
825
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
828 loop or trap. */
829 bitmap
830 find_always_executed_bbs (function *fun, bool assume_return_or_eh)
831 {
832 auto_vec<basic_block, 20> stack;
833 auto_vec<basic_block, 20> terminating_bbs;
834 hash_set<basic_block> visited;
835 edge e;
836 edge_iterator ei;
837
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
840 each invocation. */
841 stack.safe_push (ENTRY_BLOCK_PTR_FOR_FN (fun));
842 while (!stack.is_empty ())
843 {
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)))
848 found = true;
849 FOR_EACH_EDGE (e, ei, bb->succs)
850 {
851 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (fun))
852 {
853 found_exit = true;
854 break;
855 }
856 /* Watch for infinite loops. */
857 if (!found && (assume_return_or_eh & EDGE_DFS_BACK)
858 && !finite_loop_p (e->src->loop_father))
859 found = true;
860 }
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))
864 {
865 found = true;
866 break;
867 }
868 if (found)
869 {
870 visited.add (EXIT_BLOCK_PTR_FOR_FN (fun));
871 if (!found_exit)
872 terminating_bbs.safe_push (bb);
873 }
874 else
875 FOR_EACH_EDGE (e, ei, bb->succs)
876 if (!visited.add (e->dest))
877 stack.safe_push (e->dest);
878 }
879
880 /* Next walk from exit block and find all articulations in the CFG.
881 Add all terminating basic blocks as "fake" predecessors of the
882 exit block. */
883
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 ())
888 {
889 bitmap_set_bit (ret,
890 single_succ_edge
891 (ENTRY_BLOCK_PTR_FOR_FN (fun))->dest->index);
892 return ret;
893 }
894
895 struct astate
896 {
897 unsigned int dfs_preorder;
898 unsigned int dfs_postorder;
899
900 unsigned int low, high;
901 };
902
903 struct worklist
904 {
905 basic_block bb;
906 astate *cstate;
907 };
908
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;
914
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.
918
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.
924
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 ())
930 {
931 worklist &w = worklist_vec.last ();
932 basic_block bb = w.bb;
933 astate *cstate = w.cstate;
934
935 if (!cstate)
936 {
937 astate **slot = &state.get_or_insert (bb);
938
939 cstate = *slot;
940 /* Already processed by DFS? */
941 if (cstate)
942 {
943 worklist_vec.pop ();
944 continue;
945 }
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++;
949 w.cstate = cstate;
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)
953 {
954 worklist we = {bb2, NULL};
955 worklist_vec.safe_push (we);
956 }
957 FOR_EACH_EDGE (e, ei, bb->preds)
958 if (visited.contains (e->src))
959 {
960 worklist we = {e->src, NULL};
961 worklist_vec.safe_push (we);
962 }
963 /* Keep BB on worklist so we process it last time. */
964 continue;
965 }
966 /* We are finished with processing reachable BBs, see if we have articulation. */
967 worklist_vec.pop ();
968 cstate->high = cstate->dfs_postorder = next_dfs_num++;
969 stack.safe_push (bb);
970 }
971 /* This is the final postorder walk. Determine low and high values and mark
972 always executed blocks. */
973 for (basic_block bb : stack)
974 {
975 astate *cstate = *state.get (bb);
976 FOR_EACH_EDGE (e, ei, bb->preds)
977 {
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 */
981 if (!cstate2)
982 {
983 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (fun))
984 cstate->low = 0;
985 continue;
986 }
987 cstate->low = MIN (cstate->low, (*cstate2)->low);
988 cstate->high = MAX (cstate->high, (*cstate2)->high);
989 }
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)
994 {
995 astate **cstate2 = state.get (e->dest);
996 if (!cstate2)
997 continue;
998 cstate->low = MIN (cstate->low, (*cstate2)->low);
999 cstate->high = MAX (cstate->high, (*cstate2)->high);
1000 }
1001 }
1002 obstack_free (&state_obstack, NULL);
1003
1004 return ret;
1005 }