OSDN Git Service

2010-04-12 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / ipa-inline.c
index 17e0501..e9ba04b 100644 (file)
@@ -1,5 +1,6 @@
 /* Inlining decision heuristics.
-   Copyright (C) 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
+   Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -155,12 +156,14 @@ along with GCC; see the file COPYING3.  If not see
 enum inlining_mode {
   INLINE_NONE = 0,
   INLINE_ALWAYS_INLINE,
+  INLINE_SIZE_NORECURSIVE,
   INLINE_SIZE,
   INLINE_ALL
 };
+
 static bool
-cgraph_decide_inlining_incrementally (struct cgraph_node *, enum inlining_mode,
-                                     int);
+cgraph_decide_inlining_incrementally (struct cgraph_node *, enum inlining_mode);
+static void cgraph_flatten (struct cgraph_node *node);
 
 
 /* Statistics we collect about inlining algorithm.  */
@@ -184,8 +187,8 @@ static int
 cgraph_estimate_time_after_inlining (int frequency, struct cgraph_node *to,
                                     struct cgraph_node *what)
 {
-  gcov_type time = (((gcov_type)what->global.time - inline_summary
-                    (what)->time_inlining_benefit)
+  gcov_type time = (((gcov_type)what->global.time
+                    - inline_summary (what)->time_inlining_benefit)
                    * frequency + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE
                    + to->global.time;
   if (time < 0)
@@ -206,6 +209,29 @@ cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
   return size;
 }
 
+/* Scale frequency of NODE edges by FREQ_SCALE and increase loop nest
+   by NEST.  */
+
+static void
+update_noncloned_frequencies (struct cgraph_node *node,
+                             int freq_scale, int nest)
+{
+  struct cgraph_edge *e;
+
+  /* We do not want to ignore high loop nest after freq drops to 0.  */
+  if (!freq_scale)
+    freq_scale = 1;
+  for (e = node->callees; e; e = e->next_callee)
+    {
+      e->loop_nest += nest;
+      e->frequency = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
+      if (e->frequency > CGRAPH_FREQ_MAX)
+        e->frequency = CGRAPH_FREQ_MAX;
+      if (!e->inline_failed)
+        update_noncloned_frequencies (e->callee, freq_scale, nest);
+    }
+}
+
 /* E is expected to be an edge being inlined.  Clone destination node of
    the edge and redirect it to the new clone.
    DUPLICATE is used for bookkeeping on whether we are actually creating new
@@ -222,7 +248,11 @@ cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
       /* We may eliminate the need for out-of-line copy to be output.
         In that case just go ahead and re-use it.  */
       if (!e->callee->callers->next_caller
-         && !e->callee->needed
+         && cgraph_can_remove_if_no_direct_calls_p (e->callee)
+         /* Don't reuse if more than one function shares a comdat group.
+            If the other function(s) are needed, we need to emit even
+            this function out of line.  */
+         && !e->callee->same_comdat_group
          && !cgraph_new_nodes)
        {
          gcc_assert (!e->callee->global.inlined_to);
@@ -232,12 +262,14 @@ cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
              nfunctions_inlined++;
            }
          duplicate = false;
+         e->callee->local.externally_visible = false;
+          update_noncloned_frequencies (e->callee, e->frequency, e->loop_nest);
        }
       else
        {
          struct cgraph_node *n;
-         n = cgraph_clone_node (e->callee, e->count, e->frequency, e->loop_nest, 
-                                update_original);
+         n = cgraph_clone_node (e->callee, e->count, e->frequency, e->loop_nest,
+                                update_original, NULL);
          cgraph_redirect_edge_callee (e, n);
        }
     }
@@ -273,9 +305,9 @@ cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
   int old_size = 0, new_size = 0;
   struct cgraph_node *to = NULL, *what;
   struct cgraph_edge *curr = e;
-
-  if (e->callee->inline_decl)
-    cgraph_redirect_edge_callee (e, cgraph_node (e->callee->inline_decl));
+  int freq;
+  bool duplicate = false;
+  int orig_size = e->callee->global.size;
 
   gcc_assert (e->inline_failed);
   e->inline_failed = CIF_OK;
@@ -284,10 +316,15 @@ cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
     DECL_POSSIBLY_INLINED (e->callee->decl) = true;
   e->callee->global.inlined = true;
 
+  if (e->callee->callers->next_caller
+      || !cgraph_can_remove_if_no_direct_calls_p (e->callee)
+      || e->callee->same_comdat_group)
+    duplicate = true;
   cgraph_clone_inlined_nodes (e, true, update_original);
 
   what = e->callee;
 
+  freq = e->frequency;
   /* Now update size of caller and all functions caller is inlined into.  */
   for (;e && !e->inline_failed; e = e->caller->callers)
     {
@@ -295,11 +332,13 @@ cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
       old_size = e->caller->global.size;
       new_size = cgraph_estimate_size_after_inlining (1, to, what);
       to->global.size = new_size;
-      to->global.time = cgraph_estimate_time_after_inlining (e->frequency, to, what);
+      to->global.time = cgraph_estimate_time_after_inlining (freq, to, what);
     }
   gcc_assert (what->global.inlined_to == to);
   if (new_size > old_size)
     overall_size += new_size - old_size;
+  if (!duplicate)
+    overall_size -= orig_size;
   ncalls_inlined++;
 
   if (flag_indirect_inlining)
@@ -308,18 +347,16 @@ cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
     return false;
 }
 
-/* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
-   Return following unredirected edge in the list of callers
-   of EDGE->CALLEE  */
+/* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.  */
 
-static struct cgraph_edge *
+static void
 cgraph_mark_inline (struct cgraph_edge *edge)
 {
   struct cgraph_node *to = edge->caller;
   struct cgraph_node *what = edge->callee;
   struct cgraph_edge *e, *next;
 
-  gcc_assert (!gimple_call_cannot_inline_p (edge->call_stmt));
+  gcc_assert (!edge->call_stmt_cannot_inline_p);
   /* Look for all calls, mark them inline and clone recursively
      all inlined functions.  */
   for (e = what->callers; e; e = next)
@@ -332,8 +369,6 @@ cgraph_mark_inline (struct cgraph_edge *edge)
            edge = next;
        }
     }
-
-  return edge;
 }
 
 /* Estimate the growth caused by inlining NODE into all callees.  */
@@ -361,7 +396,8 @@ cgraph_estimate_growth (struct cgraph_node *node)
      we decide to not inline for different reasons, but it is not big deal
      as in that case we will keep the body around, but we will also avoid
      some inlining.  */
-  if (!node->needed && !DECL_EXTERNAL (node->decl) && !self_recursive)
+  if (cgraph_only_called_directly_p (node)
+      && !DECL_EXTERNAL (node->decl) && !self_recursive)
     growth -= node->global.size;
 
   node->global.estimated_growth = growth;
@@ -369,7 +405,7 @@ cgraph_estimate_growth (struct cgraph_node *node)
 }
 
 /* Return false when inlining WHAT into TO is not good idea
-   as it would cause too large growth of function bodies.  
+   as it would cause too large growth of function bodies.
    When ONE_ONLY is true, assume that only one call site is going
    to be inlined, otherwise figure out how many call sites in
    TO calls WHAT and verify that all can be inlined.
@@ -440,8 +476,9 @@ cgraph_default_inline_p (struct cgraph_node *n, cgraph_inline_failed_t *reason)
 {
   tree decl = n->decl;
 
-  if (n->inline_decl)
-    decl = n->inline_decl;
+  if (n->local.disregard_inline_limits)
+    return true;
+
   if (!flag_inline_small_functions && !DECL_DECLARED_INLINE_P (decl))
     {
       if (reason)
@@ -509,7 +546,7 @@ cgraph_recursive_inlining_p (struct cgraph_node *to,
 static int
 cgraph_edge_badness (struct cgraph_edge *edge)
 {
-  int badness;
+  gcov_type badness;
   int growth =
     cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
 
@@ -537,20 +574,22 @@ cgraph_edge_badness (struct cgraph_edge *edge)
   else if (flag_guess_branch_prob)
     {
       int div = edge->frequency * 100 / CGRAPH_FREQ_BASE + 1;
-      badness = growth * 256;
+      badness = growth * 10000;
       div *= MIN (100 * inline_summary (edge->callee)->time_inlining_benefit
                  / (edge->callee->global.time + 1) + 1, 100);
-      
+
 
       /* Decrease badness if call is nested.  */
       /* Compress the range so we don't overflow.  */
-      if (div > 256)
-       div = 256 + ceil_log2 (div) - 8;
+      if (div > 10000)
+       div = 10000 + ceil_log2 (div) - 8;
       if (div < 1)
        div = 1;
       if (badness > 0)
        badness /= div;
       badness += cgraph_estimate_growth (edge->callee);
+      if (badness > INT_MAX)
+        badness = INT_MAX;
     }
   /* When function local profile is not available or it does not give
      useful information (ie frequency is zero), base the cost on
@@ -562,7 +601,7 @@ cgraph_edge_badness (struct cgraph_edge *edge)
       badness = cgraph_estimate_growth (edge->callee) * 256;
 
       /* Decrease badness if call is nested.  */
-      if (badness > 0)    
+      if (badness > 0)
        badness >>= nest;
       else
         {
@@ -688,6 +727,12 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node,
   int depth = 0;
   int n = 0;
 
+  /* It does not make sense to recursively inline always-inline functions
+     as we are going to sorry() on the remaining calls anyway.  */
+  if (node->local.disregard_inline_limits
+      && lookup_attribute ("always_inline", DECL_ATTRIBUTES (node->decl)))
+    return false;
+
   if (optimize_function_for_size_p (DECL_STRUCT_FUNCTION (node->decl))
       || (!flag_inline_functions && !DECL_DECLARED_INLINE_P (node->decl)))
     return false;
@@ -711,12 +756,13 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node,
     }
 
   if (dump_file)
-    fprintf (dump_file, 
+    fprintf (dump_file,
             "  Performing recursive inlining on %s\n",
             cgraph_node_name (node));
 
   /* We need original clone to copy around.  */
-  master_clone = cgraph_clone_node (node, node->count, CGRAPH_FREQ_BASE, 1, false);
+  master_clone = cgraph_clone_node (node, node->count, CGRAPH_FREQ_BASE, 1,
+                                   false, NULL);
   master_clone->needed = true;
   for (e = master_clone->callees; e; e = e->next_callee)
     if (!e->inline_failed)
@@ -739,7 +785,7 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node,
       if (depth > max_depth)
        {
           if (dump_file)
-           fprintf (dump_file, 
+           fprintf (dump_file,
                     "   maximal depth reached\n");
          continue;
        }
@@ -755,7 +801,7 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node,
           if (curr->count * 100 / node->count < probability)
            {
              if (dump_file)
-               fprintf (dump_file, 
+               fprintf (dump_file,
                         "   Probability of edge is too small\n");
              continue;
            }
@@ -763,7 +809,7 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node,
 
       if (dump_file)
        {
-         fprintf (dump_file, 
+         fprintf (dump_file,
                   "   Inlining call of depth %i", depth);
          if (node->count)
            {
@@ -782,7 +828,7 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node,
 
   fibheap_delete (heap);
   if (dump_file)
-    fprintf (dump_file, 
+    fprintf (dump_file,
             "\n   Inlined %i times, body grown from size %i to %i, time %i to %i\n", n,
             master_clone->global.size, node->global.size,
             master_clone->global.time, node->global.time);
@@ -876,8 +922,7 @@ cgraph_decide_inlining_of_small_functions (void)
 
   for (node = cgraph_nodes; node; node = node->next)
     {
-      if (!node->local.inlinable || !node->callers
-         || node->local.disregard_inline_limits)
+      if (!node->local.inlinable || !node->callers)
        continue;
       if (dump_file)
        fprintf (dump_file, "Considering inline candidate %s.\n", cgraph_node_name (node));
@@ -913,11 +958,11 @@ cgraph_decide_inlining_of_small_functions (void)
 
       if (dump_file)
        {
-         fprintf (dump_file, 
+         fprintf (dump_file,
                   "\nConsidering %s with %i size\n",
                   cgraph_node_name (edge->callee),
                   edge->callee->global.size);
-         fprintf (dump_file, 
+         fprintf (dump_file,
                   " to be inlined into %s in %s:%i\n"
                   " Estimated growth after inlined into all callees is %+i insns.\n"
                   " Estimated badness is %i, frequency %.2f.\n",
@@ -999,10 +1044,8 @@ cgraph_decide_inlining_of_small_functions (void)
            }
          continue;
        }
-      if (!tree_can_inline_p (edge->caller->decl, edge->callee->decl))
+      if (!tree_can_inline_p (edge))
        {
-         gimple_call_set_cannot_inline (edge->call_stmt, true);
-         edge->inline_failed = CIF_TARGET_OPTION_MISMATCH;
          if (dump_file)
            fprintf (dump_file, " inline_failed:%s.\n",
                     cgraph_inline_failed_string (edge->inline_failed));
@@ -1025,7 +1068,7 @@ cgraph_decide_inlining_of_small_functions (void)
       else
        {
          struct cgraph_node *callee;
-         if (gimple_call_cannot_inline_p (edge->call_stmt)
+         if (edge->call_stmt_cannot_inline_p
              || !cgraph_check_inline_limits (edge->caller, edge->callee,
                                              &edge->inline_failed, true))
            {
@@ -1057,8 +1100,8 @@ cgraph_decide_inlining_of_small_functions (void)
 
       if (dump_file)
        {
-         fprintf (dump_file, 
-                  " Inlined into %s which now has sie %i and self time %i,"
+         fprintf (dump_file,
+                  " Inlined into %s which now has size %i and self time %i,"
                   "net change of %+i.\n",
                   cgraph_node_name (edge->caller),
                   edge->caller->global.time,
@@ -1090,6 +1133,86 @@ cgraph_decide_inlining_of_small_functions (void)
   BITMAP_FREE (updated_nodes);
 }
 
+/* Flatten NODE from the IPA inliner.  */
+
+static void
+cgraph_flatten (struct cgraph_node *node)
+{
+  struct cgraph_edge *e;
+
+  /* We shouldn't be called recursively when we are being processed.  */
+  gcc_assert (node->aux == NULL);
+
+  node->aux = (void *)(size_t) INLINE_ALL;
+
+  for (e = node->callees; e; e = e->next_callee)
+    {
+      struct cgraph_node *orig_callee;
+
+      if (e->call_stmt_cannot_inline_p)
+       continue;
+
+      if (!e->callee->analyzed)
+       {
+         if (dump_file)
+           fprintf (dump_file,
+                    "Not inlining: Function body not available.\n");
+         continue;
+       }
+
+      /* We've hit cycle?  It is time to give up.  */
+      if (e->callee->aux)
+       {
+         if (dump_file)
+           fprintf (dump_file,
+                    "Not inlining %s into %s to avoid cycle.\n",
+                    cgraph_node_name (e->callee),
+                    cgraph_node_name (e->caller));
+         e->inline_failed = CIF_RECURSIVE_INLINING;
+         continue;
+       }
+
+      /* When the edge is already inlined, we just need to recurse into
+        it in order to fully flatten the leaves.  */
+      if (!e->inline_failed)
+       {
+         cgraph_flatten (e->callee);
+         continue;
+       }
+
+      if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
+       {
+         if (dump_file)
+           fprintf (dump_file, "Not inlining: recursive call.\n");
+         continue;
+       }
+
+      if (!tree_can_inline_p (e))
+       {
+         if (dump_file)
+           fprintf (dump_file, "Not inlining: %s",
+                    cgraph_inline_failed_string (e->inline_failed));
+         continue;
+       }
+
+      /* Inline the edge and flatten the inline clone.  Avoid
+         recursing through the original node if the node was cloned.  */
+      if (dump_file)
+       fprintf (dump_file, " Inlining %s into %s.\n",
+                cgraph_node_name (e->callee),
+                cgraph_node_name (e->caller));
+      orig_callee = e->callee;
+      cgraph_mark_inline_edge (e, true, NULL);
+      if (e->callee != orig_callee)
+       orig_callee->aux = (void *)(size_t) INLINE_ALL;
+      cgraph_flatten (e->callee);
+      if (e->callee != orig_callee)
+       orig_callee->aux = NULL;
+    }
+
+  node->aux = NULL;
+}
+
 /* Decide on the inlining.  We do so in the topological order to avoid
    expenses on updating data structures.  */
 
@@ -1102,10 +1225,11 @@ cgraph_decide_inlining (void)
     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
   int old_size = 0;
   int i;
-  bool redo_always_inline = true;
   int initial_size = 0;
 
   cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
+  if (in_lto_p && flag_indirect_inlining)
+    ipa_update_after_lto_read ();
 
   max_count = 0;
   max_benefit = 0;
@@ -1115,7 +1239,6 @@ cgraph_decide_inlining (void)
        struct cgraph_edge *e;
 
        gcc_assert (inline_summary (node)->self_size == node->global.size);
-       gcc_assert (node->needed || node->reachable);
        initial_size += node->global.size;
        for (e = node->callees; e; e = e->next_callee)
          if (max_count < e->count)
@@ -1123,7 +1246,9 @@ cgraph_decide_inlining (void)
        if (max_benefit < inline_summary (node)->time_inlining_benefit)
          max_benefit = inline_summary (node)->time_inlining_benefit;
       }
-  gcc_assert (!max_count || (profile_info && flag_branch_probabilities));
+  gcc_assert (in_lto_p
+             || !max_count
+             || (profile_info && flag_branch_probabilities));
   overall_size = initial_size;
 
   nnodes = cgraph_postorder (order);
@@ -1137,69 +1262,29 @@ cgraph_decide_inlining (void)
     node->aux = 0;
 
   if (dump_file)
-    fprintf (dump_file, "\nInlining always_inline functions:\n");
+    fprintf (dump_file, "\nFlattening functions:\n");
 
-  /* In the first pass mark all always_inline edges.  Do this with a priority
-     so none of our later choices will make this impossible.  */
-  while (redo_always_inline)
+  /* In the first pass handle functions to be flattened.  Do this with
+     a priority so none of our later choices will make this impossible.  */
+  for (i = nnodes - 1; i >= 0; i--)
     {
-      redo_always_inline = false;
-      for (i = nnodes - 1; i >= 0; i--)
+      node = order[i];
+
+      /* Handle nodes to be flattened, but don't update overall unit
+        size.  Calling the incremental inliner here is lame,
+        a simple worklist should be enough.  What should be left
+        here from the early inliner (if it runs) is cyclic cases.
+        Ideally when processing callees we stop inlining at the
+        entry of cycles, possibly cloning that entry point and
+        try to flatten itself turning it into a self-recursive
+        function.  */
+      if (lookup_attribute ("flatten",
+                           DECL_ATTRIBUTES (node->decl)) != NULL)
        {
-         struct cgraph_edge *e, *next;
-
-         node = order[i];
-
-         /* Handle nodes to be flattened, but don't update overall unit
-            size.  */
-         if (lookup_attribute ("flatten",
-                               DECL_ATTRIBUTES (node->decl)) != NULL)
-           {
-             if (dump_file)
-               fprintf (dump_file,
-                        "Flattening %s\n", cgraph_node_name (node));
-             cgraph_decide_inlining_incrementally (node, INLINE_ALL, 0);
-           }
-
-         if (!node->local.disregard_inline_limits)
-           continue;
          if (dump_file)
            fprintf (dump_file,
-                    "\nConsidering %s size:%i (always inline)\n",
-                    cgraph_node_name (node), node->global.size);
-         old_size = overall_size;
-         for (e = node->callers; e; e = next)
-           {
-             next = e->next_caller;
-             if (!e->inline_failed
-                 || gimple_call_cannot_inline_p (e->call_stmt))
-               continue;
-             if (cgraph_recursive_inlining_p (e->caller, e->callee,
-                                              &e->inline_failed))
-               continue;
-             if (!tree_can_inline_p (e->caller->decl, e->callee->decl))
-               {
-                 gimple_call_set_cannot_inline (e->call_stmt, true);
-                 continue;
-               }
-             if (cgraph_mark_inline_edge (e, true, NULL))
-               redo_always_inline = true;
-             if (dump_file)
-               fprintf (dump_file,
-                        " Inlined into %s which now has size %i.\n",
-                        cgraph_node_name (e->caller),
-                        e->caller->global.size);
-           }
-         /* Inlining self recursive function might introduce new calls to
-            themselves we didn't see in the loop above.  Fill in the proper
-            reason why inline failed.  */
-         for (e = node->callers; e; e = e->next_caller)
-           if (e->inline_failed)
-             e->inline_failed = CIF_RECURSIVE_INLINING;
-         if (dump_file)
-           fprintf (dump_file, 
-                    " Inlined for a net change of %+i size.\n",
-                    overall_size - old_size);
+                    "Flattening %s\n", cgraph_node_name (node));
+         cgraph_flatten (node);
        }
     }
 
@@ -1217,13 +1302,17 @@ cgraph_decide_inlining (void)
 
          if (node->callers
              && !node->callers->next_caller
-             && !node->needed
+             && cgraph_only_called_directly_p (node)
              && node->local.inlinable
              && node->callers->inline_failed
-             && !gimple_call_cannot_inline_p (node->callers->call_stmt)
+             && node->callers->caller != node
+             && node->callers->caller->global.inlined_to != node
+             && !node->callers->call_stmt_cannot_inline_p
              && !DECL_EXTERNAL (node->decl)
              && !DECL_COMDAT (node->decl))
            {
+             cgraph_inline_failed_t reason;
+             old_size = overall_size;
              if (dump_file)
                {
                  fprintf (dump_file,
@@ -1236,7 +1325,7 @@ cgraph_decide_inlining (void)
                }
 
              if (cgraph_check_inline_limits (node->callers->caller, node,
-                                             NULL, false))
+                                             &reason, false))
                {
                  cgraph_mark_inline (node->callers);
                  if (dump_file)
@@ -1251,7 +1340,8 @@ cgraph_decide_inlining (void)
                {
                  if (dump_file)
                    fprintf (dump_file,
-                            " Inline limit reached, not inlined.\n");
+                            " Not inlining: %s.\n",
+                             cgraph_inline_failed_string (reason));
                }
            }
        }
@@ -1271,275 +1361,188 @@ cgraph_decide_inlining (void)
   return 0;
 }
 
-/* Try to inline edge E from incremental inliner.  MODE specifies mode
-   of inliner.
-
-   We are detecting cycles by storing mode of inliner into cgraph_node last
-   time we visited it in the recursion.  In general when mode is set, we have
-   recursive inlining, but as an special case, we want to try harder inline
-   ALWAYS_INLINE functions: consider callgraph a->b->c->b, with a being
-   flatten, b being always inline.  Flattening 'a' will collapse
-   a->b->c before hitting cycle.  To accommodate always inline, we however
-   need to inline a->b->c->b.
-
-   So after hitting cycle first time, we switch into ALWAYS_INLINE mode and
-   stop inlining only after hitting ALWAYS_INLINE in ALWAY_INLINE mode.  */
+/* Return true when N is leaf function.  Accept cheap (pure&const) builtins
+   in leaf functions.  */
 static bool
-try_inline (struct cgraph_edge *e, enum inlining_mode mode, int depth)
+leaf_node_p (struct cgraph_node *n)
 {
-  struct cgraph_node *callee = e->callee;
-  enum inlining_mode callee_mode = (enum inlining_mode) (size_t) callee->aux;
-  bool always_inline = e->callee->local.disregard_inline_limits;
-
-  /* We've hit cycle?  */
-  if (callee_mode)
-    {
-      /* It is first time we see it and we are not in ALWAY_INLINE only
-        mode yet.  and the function in question is always_inline.  */
-      if (always_inline && mode != INLINE_ALWAYS_INLINE)
-       {
-         if (dump_file)
-           {
-             indent_to (dump_file, depth);
-             fprintf (dump_file,
-                      "Hit cycle in %s, switching to always inline only.\n",
-                      cgraph_node_name (callee));
-           }
-         mode = INLINE_ALWAYS_INLINE;
-       }
-      /* Otherwise it is time to give up.  */
-      else
-       {
-         if (dump_file)
-           {
-             indent_to (dump_file, depth);
-             fprintf (dump_file,
-                      "Not inlining %s into %s to avoid cycle.\n",
-                      cgraph_node_name (callee),
-                      cgraph_node_name (e->caller));
-           }
-         e->inline_failed = (e->callee->local.disregard_inline_limits
-                             ? CIF_RECURSIVE_INLINING : CIF_UNSPECIFIED);
-          return false;
-       }
-    }
-      
-  callee->aux = (void *)(size_t) mode;
-  if (dump_file)
-    {
-      indent_to (dump_file, depth);
-      fprintf (dump_file, " Inlining %s into %s.\n",
-              cgraph_node_name (e->callee),
-              cgraph_node_name (e->caller));
-    }
-  if (e->inline_failed)
-    {
-      cgraph_mark_inline (e);
-
-      /* In order to fully inline always_inline functions, we need to
-        recurse here, since the inlined functions might not be processed by
-        incremental inlining at all yet.  
-
-        Also flattening needs to be done recursively.  */
-
-      if (mode == INLINE_ALL || always_inline)
-       cgraph_decide_inlining_incrementally (e->callee, mode, depth + 1);
-    }
-  callee->aux = (void *)(size_t) callee_mode;
+  struct cgraph_edge *e;
+  for (e = n->callees; e; e = e->next_callee)
+    if (!DECL_BUILT_IN (e->callee->decl)
+       || (!TREE_READONLY (e->callee->decl)
+           || DECL_PURE_P (e->callee->decl)))
+      return false;
   return true;
 }
 
 /* Decide on the inlining.  We do so in the topological order to avoid
-   expenses on updating data structures.  
-   DEPTH is depth of recursion, used only for debug output.  */
+   expenses on updating data structures.  */
 
 static bool
 cgraph_decide_inlining_incrementally (struct cgraph_node *node,
-                                     enum inlining_mode mode,
-                                     int depth)
+                                     enum inlining_mode mode)
 {
   struct cgraph_edge *e;
   bool inlined = false;
   cgraph_inline_failed_t failed_reason;
-  enum inlining_mode old_mode;
 
 #ifdef ENABLE_CHECKING
   verify_cgraph_node (node);
 #endif
 
-  old_mode = (enum inlining_mode) (size_t)node->aux;
-
-  if (mode != INLINE_ALWAYS_INLINE
+  if (mode != INLINE_ALWAYS_INLINE && mode != INLINE_SIZE_NORECURSIVE
       && lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) != NULL)
     {
       if (dump_file)
-       {
-         indent_to (dump_file, depth);
-         fprintf (dump_file, "Flattening %s\n", cgraph_node_name (node));
-       }
+       fprintf (dump_file, "Incrementally flattening %s\n",
+                cgraph_node_name (node));
       mode = INLINE_ALL;
     }
 
-  node->aux = (void *)(size_t) mode;
-
   /* First of all look for always inline functions.  */
-  for (e = node->callees; e; e = e->next_callee)
-    {
-      if (!e->callee->local.disregard_inline_limits
-         && (mode != INLINE_ALL || !e->callee->local.inlinable))
-       continue;
-      if (gimple_call_cannot_inline_p (e->call_stmt))
-       continue;
-      /* When the edge is already inlined, we just need to recurse into
-        it in order to fully flatten the leaves.  */
-      if (!e->inline_failed && mode == INLINE_ALL)
-       {
-          inlined |= try_inline (e, mode, depth);
-         continue;
-       }
-      if (dump_file)
-       {
-         indent_to (dump_file, depth);
-         fprintf (dump_file,
-                  "Considering to always inline inline candidate %s.\n",
-                  cgraph_node_name (e->callee));
-       }
-      if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
-       {
-         if (dump_file)
-           {
-             indent_to (dump_file, depth);
-             fprintf (dump_file, "Not inlining: recursive call.\n");
-           }
-         continue;
-       }
-      if (!tree_can_inline_p (node->decl, e->callee->decl))
-       {
-         gimple_call_set_cannot_inline (e->call_stmt, true);
-         if (dump_file)
-           {
-             indent_to (dump_file, depth);
-             fprintf (dump_file,
-                      "Not inlining: Target specific option mismatch.\n");
-           }
-         continue;
-       }
-      if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
-         != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
-       {
-         if (dump_file)
-           {
-             indent_to (dump_file, depth);
-             fprintf (dump_file, "Not inlining: SSA form does not match.\n");
-           }
-         continue;
-       }
-      if (!e->callee->analyzed && !e->callee->inline_decl)
-       {
-         if (dump_file)
-           {
-             indent_to (dump_file, depth);
-             fprintf (dump_file,
-                      "Not inlining: Function body no longer available.\n");
-           }
-         continue;
-       }
-      inlined |= try_inline (e, mode, depth);
-    }
-
-  /* Now do the automatic inlining.  */
-  if (mode != INLINE_ALL && mode != INLINE_ALWAYS_INLINE)
+  if (mode != INLINE_SIZE_NORECURSIVE)
     for (e = node->callees; e; e = e->next_callee)
       {
-        int allowed_growth = 0;
-       if (!e->callee->local.inlinable
-           || !e->inline_failed
-           || e->callee->local.disregard_inline_limits)
+       if (!e->callee->local.disregard_inline_limits
+           && (mode != INLINE_ALL || !e->callee->local.inlinable))
+         continue;
+       if (e->call_stmt_cannot_inline_p)
          continue;
        if (dump_file)
-         fprintf (dump_file, "Considering inline candidate %s.\n",
+         fprintf (dump_file,
+                  "Considering to always inline inline candidate %s.\n",
                   cgraph_node_name (e->callee));
        if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
          {
            if (dump_file)
-             {
-               indent_to (dump_file, depth);
-               fprintf (dump_file, "Not inlining: recursive call.\n");
-             }
+             fprintf (dump_file, "Not inlining: recursive call.\n");
+           continue;
+         }
+       if (!tree_can_inline_p (e))
+         {
+           if (dump_file)
+             fprintf (dump_file,
+                      "Not inlining: %s",
+                      cgraph_inline_failed_string (e->inline_failed));
            continue;
          }
        if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
            != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
          {
            if (dump_file)
-             {
-               indent_to (dump_file, depth);
-               fprintf (dump_file, "Not inlining: SSA form does not match.\n");
-             }
+             fprintf (dump_file, "Not inlining: SSA form does not match.\n");
            continue;
          }
-
-       if (cgraph_maybe_hot_edge_p (e))
-         allowed_growth = PARAM_VALUE (PARAM_EARLY_INLINING_INSNS);
-
-       /* When the function body would grow and inlining the function won't
-          eliminate the need for offline copy of the function, don't inline.
-        */
-       if ((mode == INLINE_SIZE
-            || (!flag_inline_functions
-                && !DECL_DECLARED_INLINE_P (e->callee->decl)))
-           && (cgraph_estimate_size_after_inlining (1, e->caller, e->callee)
-               >= e->caller->global.size + allowed_growth)
-           && cgraph_estimate_growth (e->callee) >= allowed_growth)
+       if (!e->callee->analyzed)
          {
            if (dump_file)
-             {
-               indent_to (dump_file, depth);
+             fprintf (dump_file,
+                      "Not inlining: Function body no longer available.\n");
+           continue;
+         }
+
+       if (dump_file)
+         fprintf (dump_file, " Inlining %s into %s.\n",
+                  cgraph_node_name (e->callee),
+                  cgraph_node_name (e->caller));
+       cgraph_mark_inline (e);
+       inlined = true;
+      }
+
+  /* Now do the automatic inlining.  */
+  if (mode != INLINE_ALL && mode != INLINE_ALWAYS_INLINE
+      /* Never inline regular functions into always-inline functions
+        during incremental inlining.  */
+      && !node->local.disregard_inline_limits)
+    {
+      bitmap visited = BITMAP_ALLOC (NULL);
+      for (e = node->callees; e; e = e->next_callee)
+       {
+         int allowed_growth = 0;
+         if (!e->callee->local.inlinable
+             || !e->inline_failed
+             || e->callee->local.disregard_inline_limits)
+           continue;
+         /* We are inlining a function to all call-sites in node
+            or to none.  So visit each candidate only once.  */
+         if (!bitmap_set_bit (visited, e->callee->uid))
+           continue;
+         if (dump_file)
+           fprintf (dump_file, "Considering inline candidate %s.\n",
+                    cgraph_node_name (e->callee));
+         if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
+           {
+             if (dump_file)
+               fprintf (dump_file, "Not inlining: recursive call.\n");
+             continue;
+           }
+         if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
+             != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
+           {
+             if (dump_file)
+               fprintf (dump_file,
+                        "Not inlining: SSA form does not match.\n");
+             continue;
+           }
+
+         if (cgraph_maybe_hot_edge_p (e) && leaf_node_p (e->callee)
+             && optimize_function_for_speed_p (cfun))
+           allowed_growth = PARAM_VALUE (PARAM_EARLY_INLINING_INSNS);
+
+         /* When the function body would grow and inlining the function
+            won't eliminate the need for offline copy of the function,
+            don't inline.  */
+         if (((mode == INLINE_SIZE || mode == INLINE_SIZE_NORECURSIVE)
+              || (!flag_inline_functions
+                  && !DECL_DECLARED_INLINE_P (e->callee->decl)))
+             && (cgraph_estimate_size_after_inlining (1, e->caller, e->callee)
+                 > e->caller->global.size + allowed_growth)
+             && cgraph_estimate_growth (e->callee) > allowed_growth)
+           {
+             if (dump_file)
                fprintf (dump_file,
                         "Not inlining: code size would grow by %i.\n",
                         cgraph_estimate_size_after_inlining (1, e->caller,
                                                              e->callee)
                         - e->caller->global.size);
-             }
-           continue;
-         }
-       if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed,
-                                       false)
-           || gimple_call_cannot_inline_p (e->call_stmt))
-         {
-           if (dump_file)
-             {
-               indent_to (dump_file, depth);
+             continue;
+           }
+         if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed,
+                                          false)
+             || e->call_stmt_cannot_inline_p)
+           {
+             if (dump_file)
                fprintf (dump_file, "Not inlining: %s.\n",
                         cgraph_inline_failed_string (e->inline_failed));
-             }
-           continue;
-         }
-       if (!e->callee->analyzed && !e->callee->inline_decl)
-         {
-           if (dump_file)
-             {
-               indent_to (dump_file, depth);
+             continue;
+           }
+         if (!e->callee->analyzed)
+           {
+             if (dump_file)
                fprintf (dump_file,
                         "Not inlining: Function body no longer available.\n");
-             }
-           continue;
-         }
-       if (!tree_can_inline_p (node->decl, e->callee->decl))
-         {
-           gimple_call_set_cannot_inline (e->call_stmt, true);
-           if (dump_file)
-             {
-               indent_to (dump_file, depth);
+             continue;
+           }
+         if (!tree_can_inline_p (e))
+           {
+             if (dump_file)
                fprintf (dump_file,
-                        "Not inlining: Target specific option mismatch.\n");
-             }
-           continue;
-         }
-       if (cgraph_default_inline_p (e->callee, &failed_reason))
-         inlined |= try_inline (e, mode, depth);
-      }
-  node->aux = (void *)(size_t) old_mode;
+                        "Not inlining: %s.",
+                        cgraph_inline_failed_string (e->inline_failed));
+             continue;
+           }
+         if (cgraph_default_inline_p (e->callee, &failed_reason))
+           {
+             if (dump_file)
+               fprintf (dump_file, " Inlining %s into %s.\n",
+                        cgraph_node_name (e->callee),
+                        cgraph_node_name (e->caller));
+             cgraph_mark_inline (e);
+             inlined = true;
+           }
+       }
+      BITMAP_FREE (visited);
+    }
   return inlined;
 }
 
@@ -1557,32 +1560,52 @@ cgraph_early_inlining (void)
 {
   struct cgraph_node *node = cgraph_node (current_function_decl);
   unsigned int todo = 0;
+  int iterations = 0;
 
   if (sorrycount || errorcount)
     return 0;
-  if (cgraph_decide_inlining_incrementally (node, INLINE_SIZE, 0))
+
+  if (!optimize
+      || flag_no_inline
+      || !flag_early_inlining)
     {
+      /* When not optimizing or not inlining inline only always-inline
+        functions.  */
+      cgraph_decide_inlining_incrementally (node, INLINE_ALWAYS_INLINE);
       timevar_push (TV_INTEGRATION);
-      todo = optimize_inline_calls (current_function_decl);
+      todo |= optimize_inline_calls (current_function_decl);
       timevar_pop (TV_INTEGRATION);
     }
+  else
+    {
+      /* We iterate incremental inlining to get trivial cases of indirect
+        inlining.  */
+      while (iterations < PARAM_VALUE (PARAM_EARLY_INLINER_MAX_ITERATIONS)
+            && cgraph_decide_inlining_incrementally (node,
+                                                     iterations
+                                                     ? INLINE_SIZE_NORECURSIVE
+                                                     : INLINE_SIZE))
+       {
+         timevar_push (TV_INTEGRATION);
+         todo |= optimize_inline_calls (current_function_decl);
+         iterations++;
+         timevar_pop (TV_INTEGRATION);
+       }
+      if (dump_file)
+       fprintf (dump_file, "Iterations: %i\n", iterations);
+    }
+
   cfun->always_inline_functions_inlined = true;
-  return todo;
-}
 
-/* When inlining shall be performed.  */
-static bool
-cgraph_gate_early_inlining (void)
-{
-  return flag_early_inlining;
+  return todo;
 }
 
-struct gimple_opt_pass pass_early_inline = 
+struct gimple_opt_pass pass_early_inline =
 {
  {
   GIMPLE_PASS,
   "einline",                           /* name */
-  cgraph_gate_early_inlining,          /* gate */
+  NULL,                                        /* gate */
   cgraph_early_inlining,               /* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
@@ -1601,13 +1624,14 @@ static bool
 cgraph_gate_ipa_early_inlining (void)
 {
   return (flag_early_inlining
+         && !in_lto_p
          && (flag_branch_probabilities || flag_test_coverage
              || profile_arc_flag));
 }
 
 /* IPA pass wrapper for early inlining pass.  We need to run early inlining
    before tree profiling so we have stand alone IPA pass for doing so.  */
-struct simple_ipa_opt_pass pass_ipa_early_inline = 
+struct simple_ipa_opt_pass pass_ipa_early_inline =
 {
  {
   SIMPLE_IPA_PASS,
@@ -1661,7 +1685,7 @@ likely_eliminated_by_inlining_p (gimple stmt)
            while (handled_component_p (inner_rhs)
                   || TREE_CODE (inner_rhs) == ADDR_EXPR || TREE_CODE (inner_rhs) == INDIRECT_REF)
              inner_rhs = TREE_OPERAND (inner_rhs, 0);
-               
+
 
            if (TREE_CODE (inner_rhs) == PARM_DECL
                || (TREE_CODE (inner_rhs) == SSA_NAME
@@ -1705,52 +1729,43 @@ estimate_function_body_sizes (struct cgraph_node *node)
   tree arg;
   int freq;
   tree funtype = TREE_TYPE (node->decl);
-  bitmap must_not_throw = must_not_throw_labels ();
 
-  if (dump_file)
+  if (node->local.disregard_inline_limits)
     {
-      fprintf (dump_file, "Analyzing function body size: %s\n", cgraph_node_name (node));
+      inline_summary (node)->self_time = 0;
+      inline_summary (node)->self_size = 0;
+      inline_summary (node)->time_inlining_benefit = 0;
+      inline_summary (node)->size_inlining_benefit = 0;
     }
 
+  if (dump_file)
+    fprintf (dump_file, "Analyzing function body size: %s\n",
+            cgraph_node_name (node));
+
   gcc_assert (my_function && my_function->cfg);
   FOR_EACH_BB_FN (bb, my_function)
     {
       freq = compute_call_stmt_bb_frequency (node->decl, bb);
       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
        {
-         int this_size = estimate_num_insns (gsi_stmt (bsi), &eni_size_weights);
-         int this_time = estimate_num_insns (gsi_stmt (bsi), &eni_time_weights);
-
-         /* MUST_NOT_THROW is usually handled by runtime calling terminate and stopping
-            stacking unwinding.  However when there is local cleanup that can resume
-            to MUST_NOT_THROW then we generate explicit handler containing
-            std::terminate () call.
-            
-            Because inlining of function can introduce new cleanup region, prior
-            inlining we keep std::terinate () calls for every MUST_NOT_THROW containing
-            function call.  Wast majority of these will be eliminated after inlining
-            and crossjumping will inify possible duplicated calls.  So ignore
-            the handlers for function body estimates.  */
-         if (gimple_code (gsi_stmt (bsi)) == GIMPLE_LABEL
-             && bitmap_bit_p (must_not_throw, 
-                              LABEL_DECL_UID (gimple_label_label (gsi_stmt (bsi)))))
-           {
-             if (dump_file)
-               fprintf (dump_file, "  MUST_NOT_THROW landing pad.  Ignoring whole BB.\n");
-           }
-         if (dump_file)
+         gimple stmt = gsi_stmt (bsi);
+         int this_size = estimate_num_insns (stmt, &eni_size_weights);
+         int this_time = estimate_num_insns (stmt, &eni_time_weights);
+
+         if (dump_file && (dump_flags & TDF_DETAILS))
            {
-             fprintf (dump_file, "  freq:%6i size:%3i time:%3i ", freq, this_size, this_time);
-             print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
+             fprintf (dump_file, "  freq:%6i size:%3i time:%3i ",
+                      freq, this_size, this_time);
+             print_gimple_stmt (dump_file, stmt, 0, 0);
            }
          this_time *= freq;
          time += this_time;
          size += this_size;
-         if (likely_eliminated_by_inlining_p (gsi_stmt (bsi)))
+         if (likely_eliminated_by_inlining_p (stmt))
            {
              size_inlining_benefit += this_size;
              time_inlining_benefit += this_time;
-             if (dump_file)
+             if (dump_file && (dump_flags & TDF_DETAILS))
                fprintf (dump_file, "    Likely eliminated\n");
            }
          gcc_assert (time >= 0);
@@ -1761,11 +1776,9 @@ estimate_function_body_sizes (struct cgraph_node *node)
   time_inlining_benefit = ((time_inlining_benefit + CGRAPH_FREQ_BASE / 2)
                           / CGRAPH_FREQ_BASE);
   if (dump_file)
-    {
-      fprintf (dump_file, "Overall function body time: %i-%i size: %i-%i\n",
-               (int)time, (int)time_inlining_benefit,
-              size, size_inlining_benefit);
-    }
+    fprintf (dump_file, "Overall function body time: %i-%i size: %i-%i\n",
+            (int)time, (int)time_inlining_benefit,
+            size, size_inlining_benefit);
   time_inlining_benefit += eni_time_weights.call_cost;
   size_inlining_benefit += eni_size_weights.call_cost;
   if (!VOID_TYPE_P (TREE_TYPE (funtype)))
@@ -1775,11 +1788,12 @@ estimate_function_body_sizes (struct cgraph_node *node)
       size_inlining_benefit += cost;
     }
   for (arg = DECL_ARGUMENTS (node->decl); arg; arg = TREE_CHAIN (arg))
-    {
-      int cost = estimate_move_cost (TREE_TYPE (arg));
-      time_inlining_benefit += cost;
-      size_inlining_benefit += cost;
-    }
+    if (!VOID_TYPE_P (TREE_TYPE (arg)))
+      {
+        int cost = estimate_move_cost (TREE_TYPE (arg));
+        time_inlining_benefit += cost;
+        size_inlining_benefit += cost;
+      }
   if (time_inlining_benefit > MAX_TIME)
     time_inlining_benefit = MAX_TIME;
   if (time > MAX_TIME)
@@ -1787,14 +1801,11 @@ estimate_function_body_sizes (struct cgraph_node *node)
   inline_summary (node)->self_time = time;
   inline_summary (node)->self_size = size;
   if (dump_file)
-    {
-      fprintf (dump_file, "With function call overhead time: %i-%i size: %i-%i\n",
-               (int)time, (int)time_inlining_benefit,
-              size, size_inlining_benefit);
-    }
+    fprintf (dump_file, "With function call overhead time: %i-%i size: %i-%i\n",
+            (int)time, (int)time_inlining_benefit,
+            size, size_inlining_benefit);
   inline_summary (node)->time_inlining_benefit = time_inlining_benefit;
   inline_summary (node)->size_inlining_benefit = size_inlining_benefit;
-  BITMAP_FREE (must_not_throw);
 }
 
 /* Compute parameters of functions used by inliner.  */
@@ -1813,10 +1824,10 @@ compute_inline_parameters (struct cgraph_node *node)
   node->global.stack_frame_offset = 0;
 
   /* Can this function be inlined at all?  */
-  node->local.inlinable = tree_inlinable_function_p (current_function_decl);
+  node->local.inlinable = tree_inlinable_function_p (node->decl);
   if (node->local.inlinable && !node->local.disregard_inline_limits)
     node->local.disregard_inline_limits
-      = DECL_DISREGARD_INLINE_LIMITS (current_function_decl);
+      = DECL_DISREGARD_INLINE_LIMITS (node->decl);
   estimate_function_body_sizes (node);
   /* Inlining characteristics are maintained by the cgraph_mark_inline.  */
   node->global.time = inline_summary (node)->self_time;
@@ -1834,7 +1845,7 @@ compute_inline_parameters_for_current (void)
   return 0;
 }
 
-struct gimple_opt_pass pass_inline_parameters = 
+struct gimple_opt_pass pass_inline_parameters =
 {
  {
   GIMPLE_PASS,
@@ -1922,7 +1933,7 @@ inline_generate_summary (void)
   for (node = cgraph_nodes; node; node = node->next)
     if (node->analyzed)
       analyze_function (node);
-  
+
   return;
 }
 
@@ -1933,6 +1944,11 @@ inline_transform (struct cgraph_node *node)
   unsigned int todo = 0;
   struct cgraph_edge *e;
 
+  /* FIXME: Currently the passmanager is adding inline transform more than once to some
+     clones.  This needs revisiting after WPA cleanups.  */
+  if (cfun->after_inlining)
+    return 0;
+
   /* We might need the body of this function so that we can expand
      it inline somewhere else.  */
   if (cgraph_preserve_function_body_p (node->decl))
@@ -1953,12 +1969,54 @@ inline_transform (struct cgraph_node *node)
   return todo | execute_fixup_cfg ();
 }
 
-struct ipa_opt_pass pass_ipa_inline = 
+/* Read inline summary.  Jump functions are shared among ipa-cp
+   and inliner, so when ipa-cp is active, we don't need to write them
+   twice.  */
+
+static void
+inline_read_summary (void)
+{
+  if (flag_indirect_inlining)
+    {
+      ipa_register_cgraph_hooks ();
+      if (!flag_ipa_cp)
+        ipa_prop_read_jump_functions ();
+    }
+  function_insertion_hook_holder =
+      cgraph_add_function_insertion_hook (&add_new_function, NULL);
+}
+
+/* Write inline summary for node in SET.
+   Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
+   active, we don't need to write them twice.  */
+
+static void
+inline_write_summary (cgraph_node_set set)
+{
+  if (flag_indirect_inlining && !flag_ipa_cp)
+    ipa_prop_write_jump_functions (set);
+}
+
+/* When to run IPA inlining.  Inlining of always-inline functions
+   happens during early inlining.  */
+
+static bool
+gate_cgraph_decide_inlining (void)
+{
+  /* ???  We'd like to skip this if not optimizing or not inlining as
+     all always-inline functions have been processed by early
+     inlining already.  But this at least breaks EH with C++ as
+     we need to unconditionally run fixup_cfg even at -O0.
+     So leave it on unconditionally for now.  */
+  return 1;
+}
+
+struct ipa_opt_pass_d pass_ipa_inline =
 {
  {
   IPA_PASS,
   "inline",                            /* name */
-  NULL,                                        /* gate */
+  gate_cgraph_decide_inlining,         /* gate */
   cgraph_decide_inlining,              /* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
@@ -1972,9 +2030,10 @@ struct ipa_opt_pass pass_ipa_inline =
   | TODO_remove_functions              /* todo_flags_finish */
  },
  inline_generate_summary,              /* generate_summary */
NULL,                                 /* write_summary */
NULL,                                 /* read_summary */
inline_write_summary,                 /* write_summary */
inline_read_summary,                  /* read_summary */
  NULL,                                 /* function_read_summary */
+ lto_ipa_fixup_call_notes,             /* stmt_fixup */
  0,                                    /* TODOs */
  inline_transform,                     /* function_transform */
  NULL,                                 /* variable_transform */