OSDN Git Service

2014-05-07 Richard Biener <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / ipa-inline.c
index ecad8fa..b4a32c1 100644 (file)
@@ -23,18 +23,6 @@ along with GCC; see the file COPYING3.  If not see
 
     The implementation of inliner is organized as follows:
 
-    Transformation of callgraph to represent inlining decisions.
-
-      The inline decisions are stored in callgraph in "inline plan" and
-      all applied later.
-
-      To mark given call inline, use cgraph_mark_inline function.
-      The function marks the edge inlinable and, if neccesary, produces
-      virtual clone in the callgraph representing the new copy of callee's
-      function body.
-
-      The inline plan is applied on given function body by inline_transform. 
-
     inlining heuristics limits
 
       can_inline_edge_p allow to check that particular inlining is allowed
@@ -60,10 +48,10 @@ along with GCC; see the file COPYING3.  If not see
         (reverse postorder) on the callgraph. Functions are converted into SSA
         form just before this pass and optimized subsequently. As a result, the
         callees of the function seen by the early inliner was already optimized
-        and results of early inlining adds a lot of optimization oppurtunities
+        and results of early inlining adds a lot of optimization opportunities
         for the local optimization.
 
-        The pass handle the obvious inlining decisions within the copmilation
+        The pass handle the obvious inlining decisions within the compilation
         unit - inlining auto inline functions, inlining for size and
         flattening.
 
@@ -75,8 +63,8 @@ along with GCC; see the file COPYING3.  If not see
         Because of lack of whole unit knowledge, the pass can not really make
         good code size/performance tradeoffs.  It however does very simple
         speculative inlining allowing code size to grow by
-        EARLY_INLINING_INSNS when calee is leaf function.  In this case the
-        optimizations perfomed later are very likely to eliminate the cost.
+        EARLY_INLINING_INSNS when callee is leaf function.  In this case the
+        optimizations performed later are very likely to eliminate the cost.
 
        pass_ipa_inline
 
@@ -126,170 +114,17 @@ along with GCC; see the file COPYING3.  If not see
 #include "except.h"
 #include "target.h"
 #include "ipa-inline.h"
+#include "ipa-utils.h"
 
 /* Statistics we collect about inlining algorithm.  */
-static int ncalls_inlined;
-static int nfunctions_inlined;
 static int overall_size;
-static gcov_type max_count, max_benefit;
-
-/* 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
-   clones or re-using node originally representing out-of-line function call.
-   */
-void
-cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate,
-                           bool update_original)
-{
-  HOST_WIDE_INT peak;
-  struct inline_summary *caller_info, *callee_info;
-
-  if (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
-         /* Recursive inlining never wants the master clone to
-            be overwritten.  */
-         && update_original
-         /* FIXME: When address is taken of DECL_EXTERNAL function we still
-            can remove its offline copy, but we would need to keep unanalyzed
-            node in the callgraph so references can point to it.  */
-         && !e->callee->address_taken
-         && cgraph_can_remove_if_no_direct_calls_p (e->callee)
-         /* Inlining might enable more devirtualizing, so we want to remove
-            those only after all devirtualizable virtual calls are processed.
-            Lacking may edges in callgraph we just preserve them post
-            inlining.  */
-         && (!DECL_VIRTUAL_P (e->callee->decl)
-             || (!DECL_COMDAT (e->callee->decl)
-                 && !DECL_EXTERNAL (e->callee->decl)))
-         /* 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);
-         if (e->callee->analyzed && !DECL_EXTERNAL (e->callee->decl))
-           {
-             overall_size -= inline_summary (e->callee)->size;
-             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->callee->decl,
-                                e->count, e->frequency, e->loop_nest,
-                                update_original, NULL);
-         cgraph_redirect_edge_callee (e, n);
-       }
-    }
-
-  callee_info = inline_summary (e->callee);
-  caller_info = inline_summary (e->caller);
-
-  if (e->caller->global.inlined_to)
-    e->callee->global.inlined_to = e->caller->global.inlined_to;
-  else
-    e->callee->global.inlined_to = e->caller;
-  callee_info->stack_frame_offset
-    = caller_info->stack_frame_offset
-      + caller_info->estimated_self_stack_size;
-  peak = callee_info->stack_frame_offset
-      + callee_info->estimated_self_stack_size;
-  if (inline_summary (e->callee->global.inlined_to)->estimated_stack_size
-      < peak)
-    inline_summary (e->callee->global.inlined_to)->estimated_stack_size = peak;
-  cgraph_propagate_frequency (e->callee);
-
-  /* Recursively clone all bodies.  */
-  for (e = e->callee->callees; e; e = e->next_callee)
-    if (!e->inline_failed)
-      cgraph_clone_inlined_nodes (e, duplicate, update_original);
-}
-
-/* Mark edge E as inlined and update callgraph accordingly.  UPDATE_ORIGINAL
-   specify whether profile of original function should be updated.  If any new
-   indirect edges are discovered in the process, add them to NEW_EDGES, unless
-   it is NULL.  Return true iff any new callgraph edges were discovered as a
-   result of inlining.  */
-
-static bool
-cgraph_mark_inline_edge (struct cgraph_edge *e, bool update_original,
-                        VEC (cgraph_edge_p, heap) **new_edges)
-{
-  int old_size = 0, new_size = 0;
-  struct cgraph_node *to = NULL;
-  struct cgraph_edge *curr = e;
-  struct inline_summary *info;
-
-  /* Don't inline inlined edges.  */
-  gcc_assert (e->inline_failed);
-  /* Don't even think of inlining inline clone.  */
-  gcc_assert (!e->callee->global.inlined_to);
-
-  e->inline_failed = CIF_OK;
-  DECL_POSSIBLY_INLINED (e->callee->decl) = true;
-
-  cgraph_clone_inlined_nodes (e, true, update_original);
-
-  /* Now update size of caller and all functions caller is inlined into.  */
-  for (;e && !e->inline_failed; e = e->caller->callers)
-    {
-      to = e->caller;
-      info = inline_summary (to);
-      old_size = info->size;
-      new_size = estimate_size_after_inlining (to, curr);
-      info->size = new_size;
-      info->time = estimate_time_after_inlining (to, curr);
-    }
-  gcc_assert (curr->callee->global.inlined_to == to);
-  if (new_size > old_size)
-    overall_size += new_size - old_size;
-  ncalls_inlined++;
-
-  /* FIXME: We should remove the optimize check after we ensure we never run
-     IPA passes when not optimizing.  */
-  if (flag_indirect_inlining && optimize)
-    return ipa_propagate_indirect_call_infos (curr, new_edges);
-  else
-    return false;
-}
+static gcov_type max_count;
 
 /* Return false when inlining edge E would lead to violating
    limits on function unit growth or stack usage growth.  
 
    The relative function body growth limit is present generally
-   to avoid problems with non-linear behaviour of the compiler.
+   to avoid problems with non-linear behavior of the compiler.
    To allow inlining huge functions into tiny wrapper, the limit
    is always based on the bigger of the two functions considered.
 
@@ -302,7 +137,7 @@ static bool
 caller_growth_limits (struct cgraph_edge *e)
 {
   struct cgraph_node *to = e->caller;
-  struct cgraph_node *what = e->callee;
+  struct cgraph_node *what = cgraph_function_or_thunk_node (e->callee, NULL);
   int newsize;
   int limit = 0;
   HOST_WIDE_INT stack_size_limit = 0, inlined_stack;
@@ -316,7 +151,7 @@ caller_growth_limits (struct cgraph_edge *e)
      we immediately inline to.  This is the most relaxed
      interpretation of the rule "do not grow large functions
      too much in order to prevent compiler from exploding".  */
-  do
+  while (true)
     {
       info = inline_summary (to);
       if (limit < info->self_size)
@@ -325,8 +160,9 @@ caller_growth_limits (struct cgraph_edge *e)
        stack_size_limit = info->estimated_self_stack_size;
       if (to->global.inlined_to)
         to = to->callers->caller;
+      else
+       break;
     }
-  while (to->global.inlined_to);
 
   what_info = inline_summary (what);
 
@@ -346,12 +182,15 @@ caller_growth_limits (struct cgraph_edge *e)
       return false;
     }
 
-  /* FIXME: Stack size limit often prevents inlining in fortran programs
-     due to large i/o datastructures used by the fortran frontend.
+  if (!what_info->estimated_stack_size)
+    return true;
+
+  /* FIXME: Stack size limit often prevents inlining in Fortran programs
+     due to large i/o datastructures used by the Fortran front-end.
      We ought to ignore this limit when we know that the edge is executed
      on every invocation of the caller (i.e. its call statement dominates
      exit block).  We do not track this information, yet.  */
-  stack_size_limit += (stack_size_limit
+  stack_size_limit += ((gcov_type)stack_size_limit
                       * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100);
 
   inlined_stack = (outer_info->stack_frame_offset
@@ -360,7 +199,7 @@ caller_growth_limits (struct cgraph_edge *e)
   /* Check new stack consumption with stack consumption at the place
      stack is used.  */
   if (inlined_stack > stack_size_limit
-      /* If function already has large stack usage from sibbling
+      /* If function already has large stack usage from sibling
         inline call, we can inline, too.
         This bit overoptimistically assume that we are good at stack
         packing.  */
@@ -381,8 +220,8 @@ report_inline_failed_reason (struct cgraph_edge *e)
   if (dump_file)
     {
       fprintf (dump_file, "  not inlinable: %s/%i -> %s/%i, %s\n",
-              cgraph_node_name (e->caller), e->caller->uid,
-              cgraph_node_name (e->callee), e->callee->uid,
+              xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
+              xstrdup (cgraph_node_name (e->callee)), e->callee->uid,
               cgraph_inline_failed_string (e->inline_failed));
     }
 }
@@ -398,22 +237,35 @@ static bool
 can_inline_edge_p (struct cgraph_edge *e, bool report)
 {
   bool inlinable = true;
+  enum availability avail;
+  struct cgraph_node *callee
+    = cgraph_function_or_thunk_node (e->callee, &avail);
   tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (e->caller->decl);
-  tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (e->callee->decl);
+  tree callee_tree
+    = callee ? DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee->decl) : NULL;
+  struct function *caller_cfun = DECL_STRUCT_FUNCTION (e->caller->decl);
+  struct function *callee_cfun
+    = callee ? DECL_STRUCT_FUNCTION (callee->decl) : NULL;
+
+  if (!caller_cfun && e->caller->clone_of)
+    caller_cfun = DECL_STRUCT_FUNCTION (e->caller->clone_of->decl);
+
+  if (!callee_cfun && callee && callee->clone_of)
+    callee_cfun = DECL_STRUCT_FUNCTION (callee->clone_of->decl);
 
   gcc_assert (e->inline_failed);
 
-  if (!e->callee->analyzed)
+  if (!callee || !callee->analyzed)
     {
       e->inline_failed = CIF_BODY_NOT_AVAILABLE;
       inlinable = false;
     }
-  else if (!inline_summary (e->callee)->inlinable)
+  else if (!inline_summary (callee)->inlinable)
     {
       e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
       inlinable = false;
     }
-  else if (cgraph_function_body_availability (e->callee) <= AVAIL_OVERWRITABLE)
+  else if (avail <= AVAIL_OVERWRITABLE)
     {
       e->inline_failed = CIF_OVERWRITABLE;
       return false;
@@ -425,34 +277,45 @@ can_inline_edge_p (struct cgraph_edge *e, bool report)
     }
   /* Don't inline if the functions have different EH personalities.  */
   else if (DECL_FUNCTION_PERSONALITY (e->caller->decl)
-          && DECL_FUNCTION_PERSONALITY (e->callee->decl)
+          && DECL_FUNCTION_PERSONALITY (callee->decl)
           && (DECL_FUNCTION_PERSONALITY (e->caller->decl)
-              != DECL_FUNCTION_PERSONALITY (e->callee->decl)))
+              != DECL_FUNCTION_PERSONALITY (callee->decl)))
     {
       e->inline_failed = CIF_EH_PERSONALITY;
       inlinable = false;
     }
+  /* TM pure functions should not be inlined into non-TM_pure
+     functions.  */
+  else if (is_tm_pure (callee->decl)
+          && !is_tm_pure (e->caller->decl))
+    {
+      e->inline_failed = CIF_UNSPECIFIED;
+      inlinable = false;
+    }
   /* Don't inline if the callee can throw non-call exceptions but the
      caller cannot.
      FIXME: this is obviously wrong for LTO where STRUCT_FUNCTION is missing.
      Move the flag into cgraph node or mirror it in the inline summary.  */
-  else if (DECL_STRUCT_FUNCTION (e->callee->decl)
-          && DECL_STRUCT_FUNCTION (e->callee->decl)->can_throw_non_call_exceptions
-          && !(DECL_STRUCT_FUNCTION (e->caller->decl)
-               && DECL_STRUCT_FUNCTION (e->caller->decl)->can_throw_non_call_exceptions))
+  else if (callee_cfun && callee_cfun->can_throw_non_call_exceptions
+          && !(caller_cfun && caller_cfun->can_throw_non_call_exceptions))
     {
       e->inline_failed = CIF_NON_CALL_EXCEPTIONS;
       inlinable = false;
     }
-  /* Check compatibility of target optimizatio noptions.  */
+  /* Check compatibility of target optimizatiooptions.  */
   else if (!targetm.target_option.can_inline_p (e->caller->decl,
-                                               e->callee->decl))
+                                               callee->decl))
     {
       e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
       inlinable = false;
     }
   /* Check if caller growth allows the inlining.  */
-  else if (!DECL_DISREGARD_INLINE_LIMITS (e->callee->decl)
+  else if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl)
+          && !lookup_attribute ("flatten",
+                                DECL_ATTRIBUTES
+                                  (e->caller->global.inlined_to
+                                   ? e->caller->global.inlined_to->decl
+                                   : e->caller->decl))
            && !caller_growth_limits (e))
     inlinable = false;
   /* Don't inline a function with a higher optimization level than the
@@ -470,22 +333,16 @@ can_inline_edge_p (struct cgraph_edge *e, bool report)
                             ? callee_tree
                             : optimization_default_node);
 
-      if ((caller_opt->x_optimize > callee_opt->x_optimize)
-         || (caller_opt->x_optimize_size != callee_opt->x_optimize_size))
+      if (((caller_opt->x_optimize > callee_opt->x_optimize)
+          || (caller_opt->x_optimize_size != callee_opt->x_optimize_size))
+         /* gcc.dg/pr43564.c.  Look at forced inline even in -O0.  */
+         && !DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
        {
-          e->inline_failed = CIF_TARGET_OPTIMIZATION_MISMATCH;
+         e->inline_failed = CIF_OPTIMIZATION_MISMATCH;
          inlinable = false;
        }
     }
 
-  /* Be sure that the cannot_inline_p flag is up to date.  */
-  gcc_checking_assert (!e->call_stmt
-                      || (gimple_call_cannot_inline_p (e->call_stmt)
-                          == e->call_stmt_cannot_inline_p)
-                      /* In -flto-partition=none mode we really keep things out of
-                         sync because call_stmt_cannot_inline_p is set at cgraph
-                         merging when function bodies are not there yet.  */
-                      || (in_lto_p && !gimple_call_cannot_inline_p (e->call_stmt)));
   if (!inlinable && report)
     report_inline_failed_reason (e);
   return inlinable;
@@ -497,10 +354,12 @@ can_inline_edge_p (struct cgraph_edge *e, bool report)
 static bool
 can_early_inline_edge_p (struct cgraph_edge *e)
 {
+  struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee,
+                                                             NULL);
   /* Early inliner might get called at WPA stage when IPA pass adds new
      function.  In this case we can not really do any of early inlining
      because function bodies are missing.  */
-  if (!gimple_has_body_p (e->callee->decl))
+  if (!gimple_has_body_p (callee->decl))
     {
       e->inline_failed = CIF_BODY_NOT_AVAILABLE;
       return false;
@@ -510,7 +369,7 @@ can_early_inline_edge_p (struct cgraph_edge *e)
      the callee by early inliner, yet).  We don't have CIF code for this
      case; later we will re-do the decision in the real inliner.  */
   if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->caller->decl))
-      || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
+      || !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
     {
       if (dump_file)
        fprintf (dump_file, "  edge not inlinable: not in SSA form\n");
@@ -542,10 +401,11 @@ static bool
 want_early_inline_function_p (struct cgraph_edge *e)
 {
   bool want_inline = true;
+  struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
 
-  if (DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
+  if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
     ;
-  else if (!DECL_DECLARED_INLINE_P (e->callee->decl)
+  else if (!DECL_DECLARED_INLINE_P (callee->decl)
           && !flag_inline_small_functions)
     {
       e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
@@ -563,19 +423,19 @@ want_early_inline_function_p (struct cgraph_edge *e)
          if (dump_file)
            fprintf (dump_file, "  will not early inline: %s/%i->%s/%i, "
                     "call is cold and code would grow by %i\n",
-                    cgraph_node_name (e->caller), e->caller->uid,
-                    cgraph_node_name (e->callee), e->callee->uid,
+                    xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
+                    xstrdup (cgraph_node_name (callee)), callee->uid,
                     growth);
          want_inline = false;
        }
-      else if (!leaf_node_p (e->callee)
+      else if (!leaf_node_p (callee)
               && growth > 0)
        {
          if (dump_file)
            fprintf (dump_file, "  will not early inline: %s/%i->%s/%i, "
                     "callee is not leaf and code would grow by %i\n",
-                    cgraph_node_name (e->caller), e->caller->uid,
-                    cgraph_node_name (e->callee), e->callee->uid,
+                    xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
+                    xstrdup (cgraph_node_name (callee)), callee->uid,
                     growth);
          want_inline = false;
        }
@@ -584,8 +444,8 @@ want_early_inline_function_p (struct cgraph_edge *e)
          if (dump_file)
            fprintf (dump_file, "  will not early inline: %s/%i->%s/%i, "
                     "growth %i exceeds --param early-inlining-insns\n",
-                    cgraph_node_name (e->caller), e->caller->uid,
-                    cgraph_node_name (e->callee), e->callee->uid,
+                    xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
+                    xstrdup (cgraph_node_name (callee)), callee->uid,
                     growth);
          want_inline = false;
        }
@@ -600,10 +460,11 @@ static bool
 want_inline_small_function_p (struct cgraph_edge *e, bool report)
 {
   bool want_inline = true;
+  struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
 
-  if (DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
+  if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
     ;
-  else if (!DECL_DECLARED_INLINE_P (e->callee->decl)
+  else if (!DECL_DECLARED_INLINE_P (callee->decl)
           && !flag_inline_small_functions)
     {
       e->inline_failed = CIF_FUNCTION_NOT_INLINE_CANDIDATE;
@@ -615,26 +476,62 @@ want_inline_small_function_p (struct cgraph_edge *e, bool report)
 
       if (growth <= 0)
        ;
-      else if (DECL_DECLARED_INLINE_P (e->callee->decl)
+      else if (DECL_DECLARED_INLINE_P (callee->decl)
               && growth >= MAX_INLINE_INSNS_SINGLE)
        {
           e->inline_failed = CIF_MAX_INLINE_INSNS_SINGLE_LIMIT;
          want_inline = false;
        }
-      else if (!DECL_DECLARED_INLINE_P (e->callee->decl)
+      /* Before giving up based on fact that caller size will grow, allow
+         functions that are called few times and eliminating the offline
+        copy will lead to overall code size reduction.
+        Not all of these will be handled by subsequent inlining of functions
+        called once: in particular weak functions are not handled or funcitons
+        that inline to multiple calls but a lot of bodies is optimized out.
+        Finally we want to inline earlier to allow inlining of callbacks.
+
+        This is slightly wrong on aggressive side:  it is entirely possible
+        that function is called many times with a context where inlining
+        reduces code size and few times with a context where inlining increase
+        code size.  Resoluting growth estimate will be negative even if it
+        would make more sense to keep offline copy and do not inline into the
+        call sites that makes the code size grow.  
+
+        When badness orders the calls in a way that code reducing calls come
+        first, this situation is not a problem at all: after inlining all
+        "good" calls, we will realize that keeping the function around is
+        better.  */
+      else if (growth <= MAX_INLINE_INSNS_SINGLE
+              /* Unlike for functions called once, we play unsafe with
+                 COMDATs.  We can allow that since we know functions
+                 in consideration are small (and thus risk is small) and
+                 moreover grow estimates already accounts that COMDAT
+                 functions may or may not disappear when eliminated from
+                 current unit. With good probability making aggressive
+                 choice in all units is going to make overall program
+                 smaller.
+
+                 Consequently we ask cgraph_can_remove_if_no_direct_calls_p
+                 instead of
+                 cgraph_will_be_removed_from_program_if_no_direct_calls  */
+               && !DECL_EXTERNAL (callee->decl)
+               && cgraph_can_remove_if_no_direct_calls_p (callee)
+               && estimate_growth (callee) <= 0)
+       ;
+      else if (!DECL_DECLARED_INLINE_P (callee->decl)
               && !flag_inline_functions)
        {
           e->inline_failed = CIF_NOT_DECLARED_INLINED;
          want_inline = false;
        }
-      else if (!DECL_DECLARED_INLINE_P (e->callee->decl)
+      else if (!DECL_DECLARED_INLINE_P (callee->decl)
               && growth >= MAX_INLINE_INSNS_AUTO)
        {
           e->inline_failed = CIF_MAX_INLINE_INSNS_AUTO_LIMIT;
          want_inline = false;
        }
-      else if (!cgraph_maybe_hot_edge_p (e)
-              && estimate_growth (e->callee) > 0)
+      /* If call is cold, do not inline when function body would grow. */
+      else if (!cgraph_maybe_hot_edge_p (e))
        {
           e->inline_failed = CIF_UNLIKELY_CALL;
          want_inline = false;
@@ -655,6 +552,7 @@ want_inline_small_function_p (struct cgraph_edge *e, bool report)
 
    In both cases we want to be extra selective since
    inlining the call will just introduce new recursive calls to appear.  */
+
 static bool
 want_inline_self_recursive_call_p (struct cgraph_edge *edge,
                                   struct cgraph_node *outer_node,
@@ -666,7 +564,7 @@ want_inline_self_recursive_call_p (struct cgraph_edge *edge,
   int caller_freq = CGRAPH_FREQ_BASE;
   int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
 
-  if (DECL_DECLARED_INLINE_P (edge->callee->decl))
+  if (DECL_DECLARED_INLINE_P (edge->caller->decl))
     max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
 
   if (!cgraph_maybe_hot_edge_p (edge))
@@ -693,12 +591,12 @@ want_inline_self_recursive_call_p (struct cgraph_edge *edge,
   /* Inlining of self recursive function into copy of itself within other function
      is transformation similar to loop peeling.
 
-     Peeling is profitable if we can inline enough copies to make probablility
+     Peeling is profitable if we can inline enough copies to make probability
      of actual call to the self recursive function very small.  Be sure that
      the probability of recursion is small.
 
-     We ensure that the frequency of recusing is at most 1 - (1/max_depth).
-     This way the expected number of recusion is at most max_depth.  */
+     We ensure that the frequency of recursing is at most 1 - (1/max_depth).
+     This way the expected number of recision is at most max_depth.  */
   else if (peeling)
     {
       int max_prob = CGRAPH_FREQ_BASE - ((CGRAPH_FREQ_BASE + max_depth - 1)
@@ -721,7 +619,7 @@ want_inline_self_recursive_call_p (struct cgraph_edge *edge,
          want_inline = false;
        }
     }
-  /* Recusive inlining, i.e. equivalent of unrolling, is profitable if recusion
+  /* Recursive inlining, i.e. equivalent of unrolling, is profitable if recursion
      depth is large.  We reduce function call overhead and increase chances that
      things fit in hardware return predictor.
 
@@ -729,7 +627,7 @@ want_inline_self_recursive_call_p (struct cgraph_edge *edge,
      actually slowing down functions whose recursion tree is wide rather than
      deep.
 
-     Deciding reliably on when to do recursive inlining withthout profile feedback
+     Deciding reliably on when to do recursive inlining without profile feedback
      is tricky.  For now we disable recursive inlining when probability of self
      recursion is low. 
 
@@ -759,6 +657,79 @@ want_inline_self_recursive_call_p (struct cgraph_edge *edge,
   return want_inline;
 }
 
+/* Return true when NODE has caller other than EDGE. 
+   Worker for cgraph_for_node_and_aliases.  */
+
+static bool
+check_caller_edge (struct cgraph_node *node, void *edge)
+{
+  return (node->callers
+          && node->callers != edge);
+}
+
+
+/* Decide if NODE is called once inlining it would eliminate need
+   for the offline copy of function.  */
+
+static bool
+want_inline_function_called_once_p (struct cgraph_node *node)
+{
+   struct cgraph_node *function = cgraph_function_or_thunk_node (node, NULL);
+   /* Already inlined?  */
+   if (function->global.inlined_to)
+     return false;
+   /* Zero or more then one callers?  */
+   if (!node->callers
+       || node->callers->next_caller)
+     return false;
+   /* Maybe other aliases has more direct calls.  */
+   if (cgraph_for_node_and_aliases (node, check_caller_edge, node->callers, true))
+     return false;
+   /* Recursive call makes no sense to inline.  */
+   if (cgraph_edge_recursive_p (node->callers))
+     return false;
+   /* External functions are not really in the unit, so inlining
+      them when called once would just increase the program size.  */
+   if (DECL_EXTERNAL (function->decl))
+     return false;
+   /* Offline body must be optimized out.  */
+   if (!cgraph_will_be_removed_from_program_if_no_direct_calls (function))
+     return false;
+   if (!can_inline_edge_p (node->callers, true))
+     return false;
+   return true;
+}
+
+
+/* Return relative time improvement for inlining EDGE in range
+   1...2^9.  */
+
+static inline int
+relative_time_benefit (struct inline_summary *callee_info,
+                      struct cgraph_edge *edge,
+                      int time_growth)
+{
+  int relbenefit;
+  gcov_type uninlined_call_time;
+
+  uninlined_call_time =
+    ((gcov_type)
+     (callee_info->time
+      + inline_edge_summary (edge)->call_stmt_time) * edge->frequency
+     + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
+  /* Compute relative time benefit, i.e. how much the call becomes faster.
+     ??? perhaps computing how much the caller+calle together become faster
+     would lead to more realistic results.  */
+  if (!uninlined_call_time)
+    uninlined_call_time = 1;
+  relbenefit =
+    (uninlined_call_time - time_growth) * 256 / (uninlined_call_time);
+  relbenefit = MIN (relbenefit, 512);
+  relbenefit = MAX (relbenefit, 1);
+  return relbenefit;
+}
+
+
 /* A cost model driving the inlining heuristics in a way so the edges with
    smallest badness are inlined first.  After each inlining is performed
    the costs of all caller edges of nodes affected are recomputed so the
@@ -769,47 +740,57 @@ static int
 edge_badness (struct cgraph_edge *edge, bool dump)
 {
   gcov_type badness;
-  int growth;
-  struct inline_summary *callee_info = inline_summary (edge->callee);
+  int growth, time_growth;
+  struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee,
+                                                             NULL);
+  struct inline_summary *callee_info = inline_summary (callee);
 
-  if (DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl))
+  if (DECL_DISREGARD_INLINE_LIMITS (callee->decl))
     return INT_MIN;
 
   growth = estimate_edge_growth (edge);
+  time_growth = estimate_edge_time (edge);
 
   if (dump)
     {
       fprintf (dump_file, "    Badness calculation for %s -> %s\n",
-              cgraph_node_name (edge->caller),
-              cgraph_node_name (edge->callee));
-      fprintf (dump_file, "      growth %i, time %i-%i, size %i-%i\n",
+              xstrdup (cgraph_node_name (edge->caller)),
+              xstrdup (cgraph_node_name (callee)));
+      fprintf (dump_file, "      size growth %i, time growth %i\n",
               growth,
-              callee_info->time,
-              callee_info->time_inlining_benefit
-              + edge->call_stmt_time,
-              callee_info->size,
-              callee_info->size_inlining_benefit
-              + edge->call_stmt_size);
+              time_growth);
     }
 
   /* Always prefer inlining saving code size.  */
   if (growth <= 0)
     {
-      badness = INT_MIN - growth;
+      badness = INT_MIN / 2 + growth;
       if (dump)
-       fprintf (dump_file, "      %i: Growth %i < 0\n", (int) badness,
+       fprintf (dump_file, "      %i: Growth %i <= 0\n", (int) badness,
                 growth);
     }
 
-  /* When profiling is available, base priorities -(#calls / growth).
-     So we optimize for overall number of "executed" inlined calls.  */
+  /* When profiling is available, compute badness as:
+
+               relative_edge_count * relative_time_benefit
+     goodness = -------------------------------------------
+               edge_growth
+     badness = -goodness  
+
+    The fraction is upside down, becuase on edge counts and time beneits
+    the bounds are known. Edge growth is essentially unlimited.  */
+
   else if (max_count)
     {
+      int relbenefit = relative_time_benefit (callee_info, edge, time_growth);
       badness =
        ((int)
-        ((double) edge->count * INT_MIN / max_count / (max_benefit + 1)) *
-        (callee_info->time_inlining_benefit
-         + edge->call_stmt_time + 1)) / growth;
+        ((double) edge->count * INT_MIN / 2 / max_count / 512) *
+        relative_time_benefit (callee_info, edge, time_growth)) / growth;
+      
+      /* Be sure that insanity of the profile won't lead to increasing counts
+        in the scalling and thus to overflow in the computation above.  */
+      gcc_assert (max_count >= edge->count);
       if (dump)
        {
          fprintf (dump_file,
@@ -817,53 +798,65 @@ edge_badness (struct cgraph_edge *edge, bool dump)
                   " * Relative benefit %f\n",
                   (int) badness, (double) badness / INT_MIN,
                   (double) edge->count / max_count,
-                  (double) (inline_summary (edge->callee)->
-                            time_inlining_benefit
-                            + edge->call_stmt_time + 1) / (max_benefit + 1));
+                  relbenefit * 100 / 256.0);
        }
     }
 
-  /* When function local profile is available, base priorities on
-     growth / frequency, so we optimize for overall frequency of inlined
-     calls.  This is not too accurate since while the call might be frequent
-     within function, the function itself is infrequent.
+  /* When function local profile is available. Compute badness as:
 
-     Other objective to optimize for is number of different calls inlined.
-     We add the estimated growth after inlining all functions to bias the
-     priorities slightly in this direction (so fewer times called functions
-     of the same size gets priority).  */
+     
+               growth_of_callee
+     badness = -------------------------------------- + growth_for-all
+              relative_time_benefit * edge_frequency
+
+  */
   else if (flag_guess_branch_prob)
     {
-      int div = edge->frequency * 100 / CGRAPH_FREQ_BASE + 1;
-      int benefitperc;
-      int growth_for_all;
-      badness = growth * 10000;
-      benefitperc =
-       100 * (callee_info->time_inlining_benefit
-              + edge->call_stmt_time)
-           / (callee_info->time + 1) + 1;
-      benefitperc = MIN (benefitperc, 100);
-      div *= benefitperc;
-
-      /* Decrease badness if call is nested.  */
-      /* Compress the range so we don't overflow.  */
-      if (div > 10000)
-       div = 10000 + ceil_log2 (div) - 8;
-      if (div < 1)
-       div = 1;
-      if (badness > 0)
-       badness /= div;
-      growth_for_all = estimate_growth (edge->callee);
-      badness += growth_for_all;
-      if (badness > INT_MAX)
-       badness = INT_MAX;
+      int div = edge->frequency * (1<<10) / CGRAPH_FREQ_MAX;
+
+      div = MAX (div, 1);
+      gcc_checking_assert (edge->frequency <= CGRAPH_FREQ_MAX);
+      div *= relative_time_benefit (callee_info, edge, time_growth);
+
+      /* frequency is normalized in range 1...2^10.
+         relbenefit in range 1...2^9
+        DIV should be in range 1....2^19.  */
+      gcc_checking_assert (div >= 1 && div <= (1<<19));
+
+      /* Result must be integer in range 0...INT_MAX.
+        Set the base of fixed point calculation so we don't lose much of
+        precision for small bandesses (those are interesting) yet we don't
+        overflow for growths that are still in interesting range.
+
+        Fixed point arithmetic with point at 8th bit. */
+      badness = ((gcov_type)growth) * (1<<(19+8));
+      badness = (badness + div / 2) / div;
+
+      /* Overall growth of inlining all calls of function matters: we want to
+        inline so offline copy of function is no longer needed.
+
+        Additionally functions that can be fully inlined without much of
+        effort are better inline candidates than functions that can be fully
+        inlined only after noticeable overall unit growths. The latter
+        are better in a sense compressing of code size by factoring out common
+        code into separate function shared by multiple code paths.
+
+        We might mix the valud into the fraction by taking into account
+        relative growth of the unit, but for now just add the number
+        into resulting fraction.  */
+      if (badness > INT_MAX / 2)
+       {
+         badness = INT_MAX / 2;
+         if (dump)
+           fprintf (dump_file, "Badness overflow\n");
+       }
       if (dump)
        {
          fprintf (dump_file,
-                  "      %i: guessed profile. frequency %i, overall growth %i,"
-                  " benefit %i%%, divisor %i\n",
-                  (int) badness, edge->frequency, growth_for_all,
-                  benefitperc, div);
+                  "      %i: guessed profile. frequency %f,"
+                  " benefit %f%%, divisor %i\n",
+                  (int) badness, (double)edge->frequency / CGRAPH_FREQ_BASE,
+                  relative_time_benefit (callee_info, edge, time_growth) * 100 / 256.0, div);
        }
     }
   /* When function local profile is not available or it does not give
@@ -872,8 +865,8 @@ edge_badness (struct cgraph_edge *edge, bool dump)
      of functions fully inlined in program.  */
   else
     {
-      int nest = MIN (edge->loop_nest, 8);
-      badness = estimate_growth (edge->callee) * 256;
+      int nest = MIN (inline_edge_summary (edge)->loop_depth, 8);
+      badness = growth * 256;
 
       /* Decrease badness if call is nested.  */
       if (badness > 0)
@@ -913,16 +906,18 @@ update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
         a minimum of heap.  */
       if (badness < n->key)
        {
-         fibheap_replace_key (heap, n, badness);
          if (dump_file && (dump_flags & TDF_DETAILS))
            {
              fprintf (dump_file,
                       "  decreasing badness %s/%i -> %s/%i, %i to %i\n",
-                      cgraph_node_name (edge->caller), edge->caller->uid,
-                      cgraph_node_name (edge->callee), edge->callee->uid,
+                      xstrdup (cgraph_node_name (edge->caller)),
+                      edge->caller->uid,
+                      xstrdup (cgraph_node_name (edge->callee)),
+                      edge->callee->uid,
                       (int)n->key,
                       badness);
            }
+         fibheap_replace_key (heap, n, badness);
          gcc_checking_assert (n->key == badness);
        }
     }
@@ -932,53 +927,120 @@ update_edge_key (fibheap_t heap, struct cgraph_edge *edge)
         {
           fprintf (dump_file,
                    "  enqueuing call %s/%i -> %s/%i, badness %i\n",
-                   cgraph_node_name (edge->caller), edge->caller->uid,
-                   cgraph_node_name (edge->callee), edge->callee->uid,
+                   xstrdup (cgraph_node_name (edge->caller)),
+                   edge->caller->uid,
+                   xstrdup (cgraph_node_name (edge->callee)),
+                   edge->callee->uid,
                    badness);
         }
       edge->aux = fibheap_insert (heap, badness, edge);
     }
 }
 
-/* Recompute heap nodes for each of caller edge.  */
+
+/* NODE was inlined.
+   All caller edges needs to be resetted because
+   size estimates change. Similarly callees needs reset
+   because better context may be known.  */
+
+static void
+reset_edge_caches (struct cgraph_node *node)
+{
+  struct cgraph_edge *edge;
+  struct cgraph_edge *e = node->callees;
+  struct cgraph_node *where = node;
+  int i;
+  struct ipa_ref *ref;
+
+  if (where->global.inlined_to)
+    where = where->global.inlined_to;
+
+  /* WHERE body size has changed, the cached growth is invalid.  */
+  reset_node_growth_cache (where);
+
+  for (edge = where->callers; edge; edge = edge->next_caller)
+    if (edge->inline_failed)
+      reset_edge_growth_cache (edge);
+  for (i = 0; ipa_ref_list_refering_iterate (&where->ref_list, i, ref); i++)
+    if (ref->use == IPA_REF_ALIAS)
+      reset_edge_caches (ipa_ref_refering_node (ref));
+
+  if (!e)
+    return;
+
+  while (true)
+    if (!e->inline_failed && e->callee->callees)
+      e = e->callee->callees;
+    else
+      {
+       if (e->inline_failed)
+         reset_edge_growth_cache (e);
+       if (e->next_callee)
+         e = e->next_callee;
+       else
+         {
+           do
+             {
+               if (e->caller == node)
+                 return;
+               e = e->caller->callers;
+             }
+           while (!e->next_callee);
+           e = e->next_callee;
+         }
+      }
+}
+
+/* Recompute HEAP nodes for each of caller of NODE.
+   UPDATED_NODES track nodes we already visited, to avoid redundant work.
+   When CHECK_INLINABLITY_FOR is set, re-check for specified edge that
+   it is inlinable. Otherwise check all edges.  */
 
 static void
 update_caller_keys (fibheap_t heap, struct cgraph_node *node,
-                   bitmap updated_nodes)
+                   bitmap updated_nodes,
+                   struct cgraph_edge *check_inlinablity_for)
 {
   struct cgraph_edge *edge;
+  int i;
+  struct ipa_ref *ref;
 
-  if (!inline_summary (node)->inlinable
+  if ((!node->alias && !inline_summary (node)->inlinable)
       || cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE
       || node->global.inlined_to)
     return;
   if (!bitmap_set_bit (updated_nodes, node->uid))
     return;
-  inline_summary (node)->estimated_growth = INT_MIN;
 
-  /* See if there is something to do.  */
-  for (edge = node->callers; edge; edge = edge->next_caller)
-    if (edge->inline_failed)
-      break;
-  if (!edge)
-    return;
+  for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
+    if (ref->use == IPA_REF_ALIAS)
+      {
+       struct cgraph_node *alias = ipa_ref_refering_node (ref);
+        update_caller_keys (heap, alias, updated_nodes, check_inlinablity_for);
+      }
 
-  for (; edge; edge = edge->next_caller)
+  for (edge = node->callers; edge; edge = edge->next_caller)
     if (edge->inline_failed)
       {
-       if (can_inline_edge_p (edge, false)
-           && want_inline_small_function_p (edge, false))
-          update_edge_key (heap, edge);
-       else if (edge->aux)
+        if (!check_inlinablity_for
+           || check_inlinablity_for == edge)
          {
-           report_inline_failed_reason (edge);
-           fibheap_delete_node (heap, (fibnode_t) edge->aux);
-           edge->aux = NULL;
+           if (can_inline_edge_p (edge, false)
+               && want_inline_small_function_p (edge, false))
+             update_edge_key (heap, edge);
+           else if (edge->aux)
+             {
+               report_inline_failed_reason (edge);
+               fibheap_delete_node (heap, (fibnode_t) edge->aux);
+               edge->aux = NULL;
+             }
          }
+       else if (edge->aux)
+         update_edge_key (heap, edge);
       }
 }
 
-/* Recompute heap nodes for each uninlined call.
+/* Recompute HEAP nodes for each uninlined call in NODE.
    This is used when we know that edge badnesses are going only to increase
    (we introduced new call site) and thus all we need is to insert newly
    created edges into heap.  */
@@ -988,7 +1050,6 @@ update_callee_keys (fibheap_t heap, struct cgraph_node *node,
                    bitmap updated_nodes)
 {
   struct cgraph_edge *e = node->callees;
-  inline_summary (node)->estimated_growth = INT_MIN;
 
   if (!e)
     return;
@@ -997,13 +1058,26 @@ update_callee_keys (fibheap_t heap, struct cgraph_node *node,
       e = e->callee->callees;
     else
       {
+       enum availability avail;
+       struct cgraph_node *callee;
+       /* We do not reset callee growth cache here.  Since we added a new call,
+          growth chould have just increased and consequentely badness metric
+           don't need updating.  */
        if (e->inline_failed
-           && inline_summary (e->callee)->inlinable
-           && cgraph_function_body_availability (e->callee) >= AVAIL_AVAILABLE
-           && !bitmap_bit_p (updated_nodes, e->callee->uid))
+           && (callee = cgraph_function_or_thunk_node (e->callee, &avail))
+           && inline_summary (callee)->inlinable
+           && cgraph_function_body_availability (callee) >= AVAIL_AVAILABLE
+           && !bitmap_bit_p (updated_nodes, callee->uid))
          {
-           inline_summary (node)->estimated_growth = INT_MIN;
-           update_edge_key (heap, e);
+           if (can_inline_edge_p (e, false)
+               && want_inline_small_function_p (e, false))
+             update_edge_key (heap, e);
+           else if (e->aux)
+             {
+               report_inline_failed_reason (e);
+               fibheap_delete_node (heap, (fibnode_t) e->aux);
+               e->aux = NULL;
+             }
          }
        if (e->next_callee)
          e = e->next_callee;
@@ -1029,8 +1103,6 @@ update_all_callee_keys (fibheap_t heap, struct cgraph_node *node,
                        bitmap updated_nodes)
 {
   struct cgraph_edge *e = node->callees;
-  inline_summary (node)->estimated_growth = INT_MIN;
-
   if (!e)
     return;
   while (true)
@@ -1038,8 +1110,14 @@ update_all_callee_keys (fibheap_t heap, struct cgraph_node *node,
       e = e->callee->callees;
     else
       {
+       struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee,
+                                                                   NULL);
+
+       /* We inlined and thus callees might have different number of calls.
+          Reset their caches  */
+        reset_node_growth_cache (callee);
        if (e->inline_failed)
-         update_caller_keys (heap, e->callee, updated_nodes);
+         update_caller_keys (heap, callee, updated_nodes, e);
        if (e->next_callee)
          e = e->next_callee;
        else
@@ -1063,16 +1141,18 @@ static void
 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
                        fibheap_t heap)
 {
-  static int priority;
   struct cgraph_edge *e;
+  enum availability avail;
+
   for (e = where->callees; e; e = e->next_callee)
-    if (e->callee == node)
+    if (e->callee == node
+       || (cgraph_function_or_thunk_node (e->callee, &avail) == node
+           && avail > AVAIL_OVERWRITABLE))
       {
        /* When profile feedback is available, prioritize by expected number
-          of calls.  Without profile feedback we maintain simple queue
-          to order candidates via recursive depths.  */
+          of calls.  */
         fibheap_insert (heap,
-                       !max_count ? priority++
+                       !max_count ? -e->frequency
                        : -(e->count / ((max_count + (1<<24) - 1) / (1<<24))),
                        e);
       }
@@ -1137,8 +1217,9 @@ recursive_inlining (struct cgraph_edge *edge,
       depth = 1;
       for (cnode = curr->caller;
           cnode->global.inlined_to; cnode = cnode->callers->caller)
-       if (node->decl == curr->callee->decl)
-         depth++;
+       if (node->decl
+           == cgraph_function_or_thunk_node (curr->callee, NULL)->decl)
+          depth++;
 
       if (!want_inline_self_recursive_call_p (curr, node, false, depth))
        continue;
@@ -1158,15 +1239,15 @@ recursive_inlining (struct cgraph_edge *edge,
        {
          /* We need original clone to copy around.  */
          master_clone = cgraph_clone_node (node, node->decl,
-                                           node->count, CGRAPH_FREQ_BASE, 1,
-                                           false, NULL);
+                                           node->count, CGRAPH_FREQ_BASE,
+                                           false, NULL, true);
          for (e = master_clone->callees; e; e = e->next_callee)
            if (!e->inline_failed)
-             cgraph_clone_inlined_nodes (e, true, false);
+             clone_inlined_nodes (e, true, false, NULL);
        }
 
       cgraph_redirect_edge_callee (curr, master_clone);
-      cgraph_mark_inline_edge (curr, false, new_edges);
+      inline_call (curr, false, new_edges, &overall_size);
       lookup_recursive_calls (node, curr->callee, heap);
       n++;
     }
@@ -1199,8 +1280,10 @@ recursive_inlining (struct cgraph_edge *edge,
   return true;
 }
 
+
 /* Given whole compilation unit estimate of INSNS, compute how large we can
    allow the unit to grow.  */
+
 static int
 compute_max_insns (int insns)
 {
@@ -1212,7 +1295,9 @@ compute_max_insns (int insns)
          * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
 }
 
+
 /* Compute badness of all edges in NEW_EDGES and add them to the HEAP.  */
+
 static void
 add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
 {
@@ -1221,8 +1306,7 @@ add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
       struct cgraph_edge *edge = VEC_pop (cgraph_edge_p, new_edges);
 
       gcc_assert (!edge->aux);
-      if (inline_summary (edge->callee)->inlinable
-         && edge->inline_failed
+      if (edge->inline_failed
          && can_inline_edge_p (edge, true)
          && want_inline_small_function_p (edge, true))
         edge->aux = fibheap_insert (heap, edge_badness (edge, false), edge);
@@ -1231,11 +1315,10 @@ add_new_edges_to_heap (fibheap_t heap, VEC (cgraph_edge_p, heap) *new_edges)
 
 
 /* We use greedy algorithm for inlining of small functions:
-   All inline candidates are put into prioritized heap based on estimated
-   growth of the overall number of instructions and then update the estimates.
+   All inline candidates are put into prioritized heap ordered in
+   increasing badness.
 
-   INLINED and INLINED_CALLEES are just pointers to arrays large enough
-   to be passed to cgraph_inlined_into and cgraph_inlined_callees.  */
+   The inlining of small functions is bounded by unit growth parameters.  */
 
 static void
 inline_small_functions (void)
@@ -1246,26 +1329,52 @@ inline_small_functions (void)
   bitmap updated_nodes = BITMAP_ALLOC (NULL);
   int min_size, max_size;
   VEC (cgraph_edge_p, heap) *new_indirect_edges = NULL;
+  int initial_size = 0;
 
   if (flag_indirect_inlining)
     new_indirect_edges = VEC_alloc (cgraph_edge_p, heap, 8);
 
   if (dump_file)
-    fprintf (dump_file, "\nDeciding on smaller functions:\n");
+    fprintf (dump_file,
+            "\nDeciding on inlining of small functions.  Starting with size %i.\n",
+            initial_size);
 
-  /* Put all inline candidates into the heap.  */
+  /* Compute overall unit size and other global parameters used by badness
+     metrics.  */
 
-  for (node = cgraph_nodes; node; node = node->next)
-    if (node->analyzed)
+  max_count = 0;
+  initialize_growth_caches ();
+
+  FOR_EACH_DEFINED_FUNCTION (node)
+    if (!node->global.inlined_to)
       {
-       struct inline_summary *info = inline_summary (node);
+       if (cgraph_function_with_gimple_body_p (node)
+           || node->thunk.thunk_p)
+         {
+           struct inline_summary *info = inline_summary (node);
+
+           if (!DECL_EXTERNAL (node->decl))
+             initial_size += info->size;
+         }
 
+       for (edge = node->callers; edge; edge = edge->next_caller)
+         if (max_count < edge->count)
+           max_count = edge->count;
+      }
+
+  overall_size = initial_size;
+  max_size = compute_max_insns (overall_size);
+  min_size = overall_size;
+
+  /* Populate the heeap with all edges we might inline.  */
+
+  FOR_EACH_DEFINED_FUNCTION (node)
+    if (!node->global.inlined_to)
+      {
        if (dump_file)
          fprintf (dump_file, "Enqueueing calls of %s/%i.\n",
                   cgraph_node_name (node), node->uid);
 
-       info->estimated_growth = INT_MIN;
-
        for (edge = node->callers; edge; edge = edge->next_caller)
          if (edge->inline_failed
              && can_inline_edge_p (edge, true)
@@ -1277,16 +1386,17 @@ inline_small_functions (void)
            }
       }
 
-  max_size = compute_max_insns (overall_size);
-  min_size = overall_size;
+  gcc_assert (in_lto_p
+             || !max_count
+             || (profile_info && flag_branch_probabilities));
 
-  while (overall_size <= max_size
-        && !fibheap_empty (heap))
+  while (!fibheap_empty (heap))
     {
       int old_size = overall_size;
       struct cgraph_node *where, *callee;
       int badness = fibheap_min_key (heap);
       int current_badness;
+      int cached_badness;
       int growth;
 
       edge = (struct cgraph_edge *) fibheap_extract_min (heap);
@@ -1295,10 +1405,18 @@ inline_small_functions (void)
       if (!edge->inline_failed)
        continue;
 
+      /* Be sure that caches are maintained consistent.  
+         We can not make this ENABLE_CHECKING only because it cause differnt
+         updates of the fibheap queue.  */
+      cached_badness = edge_badness (edge, false);
+      reset_edge_growth_cache (edge);
+      reset_node_growth_cache (edge->callee);
+
       /* When updating the edge costs, we only decrease badness in the keys.
-        When the badness increase, we keep the heap as it is and re-insert
-        key now.  */
+        Increases of badness are handled lazilly; when we see key with out
+        of date value on it, we re-insert it now.  */
       current_badness = edge_badness (edge, false);
+      gcc_assert (cached_badness == current_badness);
       gcc_assert (current_badness >= badness);
       if (current_badness != badness)
        {
@@ -1309,14 +1427,14 @@ inline_small_functions (void)
       if (!can_inline_edge_p (edge, true))
        continue;
       
-      callee = edge->callee;
+      callee = cgraph_function_or_thunk_node (edge->callee, NULL);
       growth = estimate_edge_growth (edge);
       if (dump_file)
        {
          fprintf (dump_file,
                   "\nConsidering %s with %i size\n",
-                  cgraph_node_name (edge->callee),
-                  inline_summary (edge->callee)->size);
+                  cgraph_node_name (callee),
+                  inline_summary (callee)->size);
          fprintf (dump_file,
                   " to be inlined into %s in %s:%i\n"
                   " Estimated growth after inlined into all is %+i insns.\n"
@@ -1326,7 +1444,7 @@ inline_small_functions (void)
                   : gimple_filename ((const_gimple) edge->call_stmt),
                   flag_wpa ? -1
                   : gimple_lineno ((const_gimple) edge->call_stmt),
-                  estimate_growth (edge->callee),
+                  estimate_growth (callee),
                   badness,
                   edge->frequency / (double)CGRAPH_FREQ_BASE);
          if (edge->count)
@@ -1337,7 +1455,7 @@ inline_small_functions (void)
        }
 
       if (overall_size + growth > max_size
-         && !DECL_DISREGARD_INLINE_LIMITS (edge->callee->decl))
+         && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
        {
          edge->inline_failed = CIF_INLINE_UNIT_GROWTH_LIMIT;
          report_inline_failed_reason (edge);
@@ -1345,12 +1463,12 @@ inline_small_functions (void)
        }
 
       if (!want_inline_small_function_p (edge, true))
-       {
-         if (dump_file)
-           fprintf (dump_file, " inline_failed:%s.\n",
-                    cgraph_inline_failed_string (edge->inline_failed));
-         continue;
-       }
+       continue;
+
+      /* Heuristics for inlining small functions works poorly for
+        recursive calls where we do efect similar to loop unrolling.
+        When inliing such edge seems profitable, leave decision on
+        specific inliner.  */
       if (cgraph_edge_recursive_p (edge))
        {
          where = edge->caller;
@@ -1363,13 +1481,15 @@ inline_small_functions (void)
              edge->inline_failed = CIF_RECURSIVE_INLINING;
              continue;
            }
+         reset_edge_caches (where);
+         /* Recursive inliner inlines all recursive calls of the function
+            at once. Consequently we need to update all callee keys.  */
          if (flag_indirect_inlining)
            add_new_edges_to_heap (heap, new_indirect_edges);
           update_all_callee_keys (heap, where, updated_nodes);
        }
       else
        {
-         struct cgraph_node *callee;
          struct cgraph_node *outer_node = NULL;
          int depth = 0;
 
@@ -1381,7 +1501,7 @@ inline_small_functions (void)
          where = edge->caller;
          while (where->global.inlined_to)
            {
-             if (where->decl == edge->callee->decl)
+             if (where->decl == callee->decl)
                outer_node = where, depth++;
              where = where->callers->caller;
            }
@@ -1397,16 +1517,23 @@ inline_small_functions (void)
          else if (depth && dump_file)
            fprintf (dump_file, " Peeling recursion with depth %i\n", depth);
 
-         callee = edge->callee;
          gcc_checking_assert (!callee->global.inlined_to);
-         cgraph_mark_inline_edge (edge, true, &new_indirect_edges);
+         inline_call (edge, true, &new_indirect_edges, &overall_size);
          if (flag_indirect_inlining)
            add_new_edges_to_heap (heap, new_indirect_edges);
 
+         reset_edge_caches (edge->callee);
+          reset_node_growth_cache (callee);
+
          /* We inlined last offline copy to the body.  This might lead
             to callees of function having fewer call sites and thus they
-            may need updating.  */
-         if (callee->global.inlined_to)
+            may need updating. 
+
+            FIXME: the callee size could also shrink because more information
+            is propagated from caller.  We don't track when this happen and
+            thus we need to recompute everything all the time.  Once this is
+            solved, "|| 1" should go away.  */
+         if (callee->global.inlined_to || 1)
            update_all_callee_keys (heap, callee, updated_nodes);
          else
            update_callee_keys (heap, edge->callee, updated_nodes);
@@ -1421,12 +1548,12 @@ inline_small_functions (void)
         inlined into (since it's body size changed) and for the functions
         called by function we inlined (since number of it inlinable callers
         might change).  */
-      update_caller_keys (heap, where, updated_nodes);
+      update_caller_keys (heap, where, updated_nodes, NULL);
 
       /* We removed one call of the function we just inlined.  If offline
         copy is still needed, be sure to update the keys.  */
       if (callee != where && !callee->global.inlined_to)
-        update_caller_keys (heap, callee, updated_nodes);
+        update_caller_keys (heap, callee, updated_nodes, NULL);
       bitmap_clear (updated_nodes);
 
       if (dump_file)
@@ -1449,16 +1576,23 @@ inline_small_functions (void)
        }
     }
 
+  free_growth_caches ();
   if (new_indirect_edges)
     VEC_free (cgraph_edge_p, heap, new_indirect_edges);
   fibheap_delete (heap);
+  if (dump_file)
+    fprintf (dump_file,
+            "Unit growth for small function inlining: %i->%i (%i%%)\n",
+            initial_size, overall_size,
+            initial_size ? overall_size * 100 / (initial_size) - 100: 0);
   BITMAP_FREE (updated_nodes);
 }
 
-/* Flatten NODE from the IPA inliner.  */
+/* Flatten NODE.  Performed both during early inlining and
+   at IPA inlining time.  */
 
 static void
-flatten_function (struct cgraph_node *node)
+flatten_function (struct cgraph_node *node, bool early)
 {
   struct cgraph_edge *e;
 
@@ -1470,15 +1604,16 @@ flatten_function (struct cgraph_node *node)
   for (e = node->callees; e; e = e->next_callee)
     {
       struct cgraph_node *orig_callee;
+      struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
 
       /* We've hit cycle?  It is time to give up.  */
-      if (e->callee->aux)
+      if (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));
+                    xstrdup (cgraph_node_name (callee)),
+                    xstrdup (cgraph_node_name (e->caller)));
          e->inline_failed = CIF_RECURSIVE_INLINING;
          continue;
        }
@@ -1487,14 +1622,14 @@ flatten_function (struct cgraph_node *node)
         it in order to fully flatten the leaves.  */
       if (!e->inline_failed)
        {
-         flatten_function (e->callee);
+         flatten_function (callee, early);
          continue;
        }
 
       /* Flatten attribute needs to be processed during late inlining. For
         extra code quality we however do flattening during early optimization,
         too.  */
-      if (cgraph_state != CGRAPH_STATE_IPA_SSA
+      if (!early
          ? !can_inline_edge_p (e, true)
          : !can_early_inline_edge_p (e))
        continue;
@@ -1507,7 +1642,7 @@ flatten_function (struct cgraph_node *node)
        }
 
       if (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
-         != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
+         != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (callee->decl)))
        {
          if (dump_file)
            fprintf (dump_file, "Not inlining: SSA form does not match.\n");
@@ -1518,13 +1653,13 @@ flatten_function (struct cgraph_node *node)
          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);
+                xstrdup (cgraph_node_name (callee)),
+                xstrdup (cgraph_node_name (e->caller)));
+      orig_callee = callee;
+      inline_call (e, true, NULL, NULL);
       if (e->callee != orig_callee)
        orig_callee->aux = (void *) node;
-      flatten_function (e->callee);
+      flatten_function (e->callee, early);
       if (e->callee != orig_callee)
        orig_callee->aux = NULL;
     }
@@ -1542,50 +1677,15 @@ ipa_inline (void)
   int nnodes;
   struct cgraph_node **order =
     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
-  int old_size = 0;
   int i;
-  int initial_size = 0;
 
-  if (in_lto_p && flag_indirect_inlining)
+  if (in_lto_p && optimize)
     ipa_update_after_lto_read ();
-  if (flag_indirect_inlining)
-    ipa_create_all_structures_for_iinln ();
-
-  max_count = 0;
-  max_benefit = 0;
-  for (node = cgraph_nodes; node; node = node->next)
-    if (node->analyzed)
-      {
-       struct cgraph_edge *e;
-       struct inline_summary *info = inline_summary (node);
-
-       gcc_assert (info->self_size == info->size);
-       if (!DECL_EXTERNAL (node->decl))
-         initial_size += info->size;
-       for (e = node->callees; e; e = e->next_callee)
-         {
-           int benefit = (info->time_inlining_benefit
-                          + e->call_stmt_time);
-           if (max_count < e->count)
-             max_count = e->count;
-           if (max_benefit < benefit)
-             max_benefit = benefit;
-         }
-      }
 
   if (dump_file)
     dump_inline_summaries (dump_file);
-  gcc_assert (in_lto_p
-             || !max_count
-             || (profile_info && flag_branch_probabilities));
-  overall_size = initial_size;
 
-  nnodes = cgraph_postorder (order);
-
-  if (dump_file)
-    fprintf (dump_file,
-            "\nDeciding on inlining.  Starting with size %i.\n",
-            initial_size);
+  nnodes = ipa_reverse_postorder (order);
 
   for (node = cgraph_nodes; node; node = node->next)
     node->aux = 0;
@@ -1599,10 +1699,7 @@ ipa_inline (void)
     {
       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.
+      /* Handle nodes to be flattened.
         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
@@ -1613,7 +1710,7 @@ ipa_inline (void)
          if (dump_file)
            fprintf (dump_file,
                     "Flattening %s\n", cgraph_node_name (node));
-         flatten_function (node);
+         flatten_function (node, false);
        }
     }
 
@@ -1626,60 +1723,69 @@ ipa_inline (void)
      we still might do a quick check that nothing new is found.  */
   if (flag_inline_functions_called_once)
     {
+      int cold;
       if (dump_file)
        fprintf (dump_file, "\nDeciding on functions called once:\n");
 
-      /* And finally decide what functions are called once.  */
-      for (node = cgraph_nodes; node; node = node->next)
+      /* Inlining one function called once has good chance of preventing
+        inlining other function into the same callee.  Ideally we should
+        work in priority order, but probably inlining hot functions first
+        is good cut without the extra pain of maintaining the queue.
+
+        ??? this is not really fitting the bill perfectly: inlining function
+        into callee often leads to better optimization of callee due to
+        increased context for optimization.
+        For example if main() function calls a function that outputs help
+        and then function that does the main optmization, we should inline
+        the second with priority even if both calls are cold by themselves.
+
+        We probably want to implement new predicate replacing our use of
+        maybe_hot_edge interpreted as maybe_hot_edge || callee is known
+        to be hot.  */
+      for (cold = 0; cold <= 1; cold ++)
        {
-         if (node->callers
-             && !node->callers->next_caller
-             && !node->global.inlined_to
-             && node->callers->inline_failed
-             && node->callers->caller != node
-             && node->callers->caller->global.inlined_to != node
-             && cgraph_will_be_removed_from_program_if_no_direct_calls (node)
-             && inline_summary (node)->inlinable
-             && cgraph_function_body_availability (node) >= AVAIL_AVAILABLE
-             && !DECL_EXTERNAL (node->decl)
-             && can_inline_edge_p (node->callers, true))
+         for (node = cgraph_nodes; node; node = node->next)
            {
-             struct cgraph_node *caller = node->callers->caller;
-
-             old_size = overall_size;
-             if (dump_file)
+             if (want_inline_function_called_once_p (node)
+                 && (cold
+                     || cgraph_maybe_hot_edge_p (node->callers)))
                {
-                 fprintf (dump_file,
-                          "\nInlining %s size %i.\n",
-                          cgraph_node_name (node), inline_summary (node)->size);
-                 fprintf (dump_file,
-                          " Called once from %s %i insns.\n",
-                          cgraph_node_name (node->callers->caller),
-                          inline_summary (node->callers->caller)->size);
+                 struct cgraph_node *caller = node->callers->caller;
+
+                 if (dump_file)
+                   {
+                     fprintf (dump_file,
+                              "\nInlining %s size %i.\n",
+                              cgraph_node_name (node),
+                              inline_summary (node)->size);
+                     fprintf (dump_file,
+                              " Called once from %s %i insns.\n",
+                              cgraph_node_name (node->callers->caller),
+                              inline_summary (node->callers->caller)->size);
+                   }
+
+                 inline_call (node->callers, true, NULL, NULL);
+                 if (dump_file)
+                   fprintf (dump_file,
+                            " Inlined into %s which now has %i size\n",
+                            cgraph_node_name (caller),
+                            inline_summary (caller)->size);
                }
-
-             cgraph_mark_inline_edge (node->callers, true, NULL);
-             if (dump_file)
-               fprintf (dump_file,
-                        " Inlined into %s which now has %i size"
-                        " for a net change of %+i size.\n",
-                        cgraph_node_name (caller),
-                        inline_summary (caller)->size,
-                        overall_size - old_size);
            }
        }
     }
 
   /* Free ipa-prop structures if they are no longer needed.  */
-  if (flag_indirect_inlining)
+  if (optimize)
     ipa_free_all_structures_after_iinln ();
 
   if (dump_file)
     fprintf (dump_file,
-            "\nInlined %i calls, eliminated %i functions, "
-            "size %i turned to %i size.\n\n",
-            ncalls_inlined, nfunctions_inlined, initial_size,
-            overall_size);
+            "\nInlined %i calls, eliminated %i functions\n\n",
+            ncalls_inlined, nfunctions_inlined);
+
+  if (dump_file)
+    dump_inline_summaries (dump_file);
   /* In WPA we use inline summaries for partitioning process.  */
   if (!flag_wpa)
     inline_free_summary ();
@@ -1696,7 +1802,8 @@ inline_always_inline_functions (struct cgraph_node *node)
 
   for (e = node->callees; e; e = e->next_callee)
     {
-      if (!DECL_DISREGARD_INLINE_LIMITS (e->callee->decl))
+      struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
+      if (!DECL_DISREGARD_INLINE_LIMITS (callee->decl))
        continue;
 
       if (cgraph_edge_recursive_p (e))
@@ -1713,9 +1820,9 @@ inline_always_inline_functions (struct cgraph_node *node)
 
       if (dump_file)
        fprintf (dump_file, "  Inlining %s into %s (always_inline).\n",
-                cgraph_node_name (e->callee),
-                cgraph_node_name (e->caller));
-      cgraph_mark_inline_edge (e, true, NULL);
+                xstrdup (cgraph_node_name (e->callee)),
+                xstrdup (cgraph_node_name (e->caller)));
+      inline_call (e, true, NULL, NULL);
       inlined = true;
     }
 
@@ -1733,19 +1840,20 @@ early_inline_small_functions (struct cgraph_node *node)
 
   for (e = node->callees; e; e = e->next_callee)
     {
-      if (!inline_summary (e->callee)->inlinable
+      struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
+      if (!inline_summary (callee)->inlinable
          || !e->inline_failed)
        continue;
 
       /* Do not consider functions not declared inline.  */
-      if (!DECL_DECLARED_INLINE_P (e->callee->decl)
+      if (!DECL_DECLARED_INLINE_P (callee->decl)
          && !flag_inline_small_functions
          && !flag_inline_functions)
        continue;
 
       if (dump_file)
        fprintf (dump_file, "Considering inline candidate %s.\n",
-                cgraph_node_name (e->callee));
+                cgraph_node_name (callee));
 
       if (!can_early_inline_edge_p (e))
        continue;
@@ -1762,21 +1870,15 @@ early_inline_small_functions (struct cgraph_node *node)
 
       if (dump_file)
        fprintf (dump_file, " Inlining %s into %s.\n",
-                cgraph_node_name (e->callee),
-                cgraph_node_name (e->caller));
-      cgraph_mark_inline_edge (e, true, NULL);
+                xstrdup (cgraph_node_name (callee)),
+                xstrdup (cgraph_node_name (e->caller)));
+      inline_call (e, true, NULL, NULL);
       inlined = true;
     }
 
   return inlined;
 }
 
-/* Because inlining might remove no-longer reachable nodes, we need to
-   keep the array visible to garbage collector to avoid reading collected
-   out nodes.  */
-static int nnodes;
-static GTY ((length ("nnodes"))) struct cgraph_node **order;
-
 /* Do inlining of small functions.  Doing so early helps profiling and other
    passes to be somewhat more effective and avoids some code duplication in
    later real inlining pass for testcases with very many function calls.  */
@@ -1792,6 +1894,15 @@ early_inliner (void)
   if (seen_error ())
     return 0;
 
+  /* Do nothing if datastructures for ipa-inliner are already computed.  This
+     happens when some pass decides to construct new function and
+     cgraph_add_new_function calls lowering passes and early optimization on
+     it.  This may confuse ourself when early inliner decide to inline call to
+     function clone, because function clones don't have parameter list in
+     ipa-prop matching their signature.  */
+  if (ipa_node_params_vector)
+    return 0;
+
 #ifdef ENABLE_CHECKING
   verify_cgraph_node (node);
 #endif
@@ -1807,7 +1918,7 @@ early_inliner (void)
         during incremental inlining.  This sucks as functions calling
         always inline functions will get less optimized, but at the
         same time inlining of functions calling always inline
-        functoin into an always inline function might introduce
+        function into an always inline function might introduce
         cycles of edges to be always inlined in the callgraph.
 
         We might want to be smarter and just avoid this type of inlining.  */
@@ -1821,7 +1932,7 @@ early_inliner (void)
       if (dump_file)
        fprintf (dump_file,
                 "Flattening %s\n", cgraph_node_name (node));
-      flatten_function (node);
+      flatten_function (node, true);
       inlined = true;
     }
   else
@@ -1840,10 +1951,15 @@ early_inliner (void)
             info that might be cleared out for newly discovered edges.  */
          for (edge = node->callees; edge; edge = edge->next_callee)
            {
-             edge->call_stmt_size
+             struct inline_edge_summary *es = inline_edge_summary (edge);
+             es->call_stmt_size
                = estimate_num_insns (edge->call_stmt, &eni_size_weights);
-             edge->call_stmt_time
+             es->call_stmt_time
                = estimate_num_insns (edge->call_stmt, &eni_time_weights);
+             if (edge->callee->decl
+                 && !gimple_check_call_matching_types (edge->call_stmt,
+                                                       edge->callee->decl))
+               edge->call_stmt_cannot_inline_p = true;
            }
          timevar_pop (TV_INTEGRATION);
          iterations++;
@@ -1880,59 +1996,21 @@ struct gimple_opt_pass pass_early_inline =
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  TODO_dump_func                       /* todo_flags_finish */
+  0                                    /* todo_flags_finish */
  }
 };
 
 
-/* Apply inline plan to function.  */
-static unsigned int
-inline_transform (struct cgraph_node *node)
-{
-  unsigned int todo = 0;
-  struct cgraph_edge *e;
-  bool inline_p = false;
-
-  /* FIXME: Currently the pass manager 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))
-    save_inline_function_body (node);
-
-  for (e = node->callees; e; e = e->next_callee)
-    {
-      cgraph_redirect_edge_call_stmt_to_callee (e);
-      if (!e->inline_failed || warn_inline)
-        inline_p = true;
-    }
-
-  if (inline_p)
-    {
-      timevar_push (TV_INTEGRATION);
-      todo = optimize_inline_calls (current_function_decl);
-      timevar_pop (TV_INTEGRATION);
-    }
-  cfun->always_inline_functions_inlined = true;
-  cfun->after_inlining = true;
-  return todo | execute_fixup_cfg ();
-}
-
 /* When to run IPA inlining.  Inlining of always-inline functions
-   happens during early inlining.  */
+   happens during early inlining.
+
+   Enable inlining unconditoinally at -flto.  We need size estimates to
+   drive partitioning.  */
 
 static bool
 gate_ipa_inline (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;
+  return optimize || flag_lto || flag_wpa;
 }
 
 struct ipa_opt_pass_d pass_ipa_inline =
@@ -1950,7 +2028,7 @@ struct ipa_opt_pass_d pass_ipa_inline =
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
   TODO_remove_functions,               /* todo_flags_finish */
-  TODO_dump_cgraph | TODO_dump_func
+  TODO_dump_cgraph 
   | TODO_remove_functions | TODO_ggc_collect   /* todo_flags_finish */
  },
  inline_generate_summary,              /* generate_summary */
@@ -1963,6 +2041,3 @@ struct ipa_opt_pass_d pass_ipa_inline =
  inline_transform,                     /* function_transform */
  NULL,                                 /* variable_transform */
 };
-
-
-#include "gt-ipa-inline.h"