OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / ipa-inline.c
index 7640fb7..6d74c1f 100644 (file)
@@ -1,12 +1,12 @@
 /* Inlining decision heuristics.
-   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
+Software Foundation; either version 3, or (at your option) any later
 version.
 
 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -15,9 +15,8 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 /*  Inlining decision heuristics
 
@@ -91,11 +90,11 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
      maintained by pass manager).  The functions after inlining are early
      optimized so the early inliner sees unoptimized function itself, but
      all considered callees are already optimized allowing it to unfold
-     abstraction penalty on C++ effectivly and cheaply.
+     abstraction penalty on C++ effectively and cheaply.
 
    pass_ipa_early_inlining
 
-     With profiling, the early inlining is also neccesary to reduce
+     With profiling, the early inlining is also necessary to reduce
      instrumentation costs on program with high abstraction penalty (doing
      many redundant calls).  This can't happen in parallel with early
      optimization and profile instrumentation, because we would end up
@@ -139,6 +138,7 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 #include "coverage.h"
 #include "ggc.h"
 #include "tree-flow.h"
+#include "rtl.h"
 
 /* Mode incremental inliner operate on:
 
@@ -172,6 +172,12 @@ static int nfunctions_inlined;
 static int overall_insns;
 static gcov_type max_count;
 
+static inline struct inline_summary *
+inline_summary (struct cgraph_node *node)
+{
+  return &node->local.inline_summary;
+}
+
 /* Estimate size of the function after inlining WHAT into TO.  */
 
 static int
@@ -204,6 +210,7 @@ cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate, bool update_o
         In that case just go ahead and re-use it.  */
       if (!e->callee->callers->next_caller
          && !e->callee->needed
+         && !cgraph_new_nodes
          && flag_unit_at_a_time)
        {
          gcc_assert (!e->callee->global.inlined_to);
@@ -214,7 +221,7 @@ cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate, bool update_o
       else
        {
          struct cgraph_node *n;
-         n = cgraph_clone_node (e->callee, e->count, e->loop_nest, 
+         n = cgraph_clone_node (e->callee, e->count, e->frequency, e->loop_nest, 
                                 update_original);
          cgraph_redirect_edge_callee (e, n);
        }
@@ -225,8 +232,10 @@ cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate, bool update_o
   else
     e->callee->global.inlined_to = e->caller;
   e->callee->global.stack_frame_offset
-    = e->caller->global.stack_frame_offset + e->caller->local.estimated_self_stack_size;
-  peak = e->callee->global.stack_frame_offset + e->callee->local.estimated_self_stack_size;
+    = e->caller->global.stack_frame_offset
+      + inline_summary (e->caller)->estimated_self_stack_size;
+  peak = e->callee->global.stack_frame_offset
+      + inline_summary (e->callee)->estimated_self_stack_size;
   if (e->callee->global.inlined_to->global.estimated_stack_size < peak)
     e->callee->global.inlined_to->global.estimated_stack_size = peak;
 
@@ -286,8 +295,8 @@ cgraph_mark_inline (struct cgraph_edge *edge)
   struct cgraph_node *to = edge->caller;
   struct cgraph_node *what = edge->callee;
   struct cgraph_edge *e, *next;
-  int times = 0;
 
+  gcc_assert (!CALL_STMT_CANNOT_INLINE_P (edge->call_stmt));
   /* Look for all calls, mark them inline and clone recursively
      all inlined functions.  */
   for (e = what->callers; e; e = next)
@@ -298,10 +307,9 @@ cgraph_mark_inline (struct cgraph_edge *edge)
           cgraph_mark_inline_edge (e, true);
          if (e == edge)
            edge = next;
-         times++;
        }
     }
-  gcc_assert (times);
+
   return edge;
 }
 
@@ -359,10 +367,10 @@ cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
 
   /* When inlining large function body called once into small function,
      take the inlined function as base for limiting the growth.  */
-  if (to->local.self_insns > what->local.self_insns)
-    limit = to->local.self_insns;
+  if (inline_summary (to)->self_insns > inline_summary(what)->self_insns)
+    limit = inline_summary (to)->self_insns;
   else
-    limit = what->local.self_insns;
+    limit = inline_summary (what)->self_insns;
 
   limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
 
@@ -378,12 +386,12 @@ cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
       return false;
     }
 
-  stack_size_limit = to->local.estimated_self_stack_size;
+  stack_size_limit = inline_summary (to)->estimated_self_stack_size;
 
   stack_size_limit += stack_size_limit * PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) / 100;
 
   inlined_stack = (to->global.stack_frame_offset
-                  + to->local.estimated_self_stack_size
+                  + inline_summary (to)->estimated_self_stack_size
                   + what->global.estimated_stack_size);
   if (inlined_stack  > stack_size_limit
       && inlined_stack > PARAM_VALUE (PARAM_LARGE_STACK_FRAME))
@@ -404,10 +412,10 @@ cgraph_default_inline_p (struct cgraph_node *n, const char **reason)
 
   if (n->inline_decl)
     decl = n->inline_decl;
-  if (!DECL_INLINE (decl))
+  if (!flag_inline_small_functions && !DECL_DECLARED_INLINE_P (decl))
     {
       if (reason)
-       *reason = N_("function not inlinable");
+       *reason = N_("function not inline candidate");
       return false;
     }
 
@@ -470,6 +478,15 @@ cgraph_maybe_hot_edge_p (struct cgraph_edge *edge)
       && (edge->count
          <= profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
     return false;
+  if (lookup_attribute ("cold", DECL_ATTRIBUTES (edge->callee->decl))
+      || lookup_attribute ("cold", DECL_ATTRIBUTES (edge->caller->decl)))
+    return false;
+  if (lookup_attribute ("hot", DECL_ATTRIBUTES (edge->caller->decl)))
+    return true;
+  if (flag_guess_branch_prob
+      && edge->frequency < (CGRAPH_FREQ_MAX
+                           / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
+    return false;
   return true;
 }
 
@@ -477,44 +494,75 @@ cgraph_maybe_hot_edge_p (struct cgraph_edge *edge)
    smallest badness are inlined first.  After each inlining is performed
    the costs of all caller edges of nodes affected are recomputed so the
    metrics may accurately depend on values such as number of inlinable callers
-   of the function or function body size.
-
-   With profiling we use number of executions of each edge to drive the cost.
-   We also should distinguish hot and cold calls where the cold calls are
-   inlined into only when code size is overall improved.  
-   */
+   of the function or function body size.  */
 
 static int
 cgraph_edge_badness (struct cgraph_edge *edge)
 {
-  if (max_count)
+  int badness;
+  int growth =
+    cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
+
+  growth -= edge->caller->global.insns;
+
+  /* Always prefer inlining saving code size.  */
+  if (growth <= 0)
+    badness = INT_MIN - growth;
+
+  /* When profiling is available, base priorities -(#calls / growth).
+     So we optimize for overall number of "executed" inlined calls.  */
+  else if (max_count)
+    badness = ((int)((double)edge->count * INT_MIN / max_count)) / growth;
+
+  /* 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.
+
+     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).  */
+  else if (flag_guess_branch_prob)
     {
+      int div = edge->frequency * 100 / CGRAPH_FREQ_BASE;
       int growth =
        cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
       growth -= edge->caller->global.insns;
+      badness = growth * 256;
+
+      /* 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 < 1)
+       div = 1;
+      if (badness > 0)
+       badness /= div;
+      badness += cgraph_estimate_growth (edge->callee);
+    }
+  /* When function local profile is not available or it does not give
+     useful information (ie frequency is zero), base the cost on
+     loop nest and overall size growth, so we optimize for overall number
+     of functions fully inlined in program.  */
+  else
+    {
+      int nest = MIN (edge->loop_nest, 8);
+      badness = cgraph_estimate_growth (edge->callee) * 256;
 
-      /* Always prefer inlining saving code size.  */
-      if (growth <= 0)
-       return INT_MIN - growth;
-      return ((int)((double)edge->count * INT_MIN / max_count)) / growth;
+      /* Decrease badness if call is nested.  */
+      if (badness > 0)    
+       badness >>= nest;
+      else
+        {
+         badness <<= nest;
+        }
     }
+  /* Make recursive inlining happen always after other inlining is done.  */
+  if (cgraph_recursive_inlining_p (edge->caller, edge->callee, NULL))
+    return badness + 1;
   else
-  {
-    int nest = MIN (edge->loop_nest, 8);
-    int badness = cgraph_estimate_growth (edge->callee) * 256;
-
-    /* Decrease badness if call is nested.  */
-    if (badness > 0)    
-      badness >>= nest;
-    else
-      badness <<= nest;
-
-    /* Make recursive inlining happen always after other inlining is done.  */
-    if (cgraph_recursive_inlining_p (edge->caller, edge->callee, NULL))
-      return badness + 1;
-    else
-      return badness;
-  }
+    return badness;
 }
 
 /* Recompute heap nodes for each of caller edge.  */
@@ -542,7 +590,7 @@ update_caller_keys (fibheap_t heap, struct cgraph_node *node,
       for (edge = node->callers; edge; edge = edge->next_caller)
        if (edge->aux)
          {
-           fibheap_delete_node (heap, edge->aux);
+           fibheap_delete_node (heap, (fibnode_t) edge->aux);
            edge->aux = NULL;
            if (edge->inline_failed)
              edge->inline_failed = failed_reason;
@@ -556,7 +604,7 @@ update_caller_keys (fibheap_t heap, struct cgraph_node *node,
        int badness = cgraph_edge_badness (edge);
        if (edge->aux)
          {
-           fibnode_t n = edge->aux;
+           fibnode_t n = (fibnode_t) edge->aux;
            gcc_assert (n->data == edge);
            if (n->key == badness)
              continue;
@@ -564,7 +612,7 @@ update_caller_keys (fibheap_t heap, struct cgraph_node *node,
            /* fibheap_replace_key only increase the keys.  */
            if (fibheap_replace_key (heap, n, badness))
              continue;
-           fibheap_delete_node (heap, edge->aux);
+           fibheap_delete_node (heap, (fibnode_t) edge->aux);
          }
        edge->aux = fibheap_insert (heap, badness, edge);
       }
@@ -626,6 +674,10 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node)
   int depth = 0;
   int n = 0;
 
+  if (optimize_size
+      || (!flag_inline_functions && !DECL_DECLARED_INLINE_P (node->decl)))
+    return false;
+
   if (DECL_DECLARED_INLINE_P (node->decl))
     {
       limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
@@ -650,7 +702,7 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node)
             cgraph_node_name (node));
 
   /* We need original clone to copy around.  */
-  master_clone = cgraph_clone_node (node, node->count, 1, false);
+  master_clone = cgraph_clone_node (node, node->count, CGRAPH_FREQ_BASE, 1, false);
   master_clone->needed = true;
   for (e = master_clone->callees; e; e = e->next_callee)
     if (!e->inline_failed)
@@ -661,7 +713,8 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node)
         && (cgraph_estimate_size_after_inlining (1, node, master_clone)
             <= limit))
     {
-      struct cgraph_edge *curr = fibheap_extract_min (heap);
+      struct cgraph_edge *curr
+       = (struct cgraph_edge *) fibheap_extract_min (heap);
       struct cgraph_node *cnode;
 
       depth = 1;
@@ -673,7 +726,7 @@ cgraph_decide_recursive_inlining (struct cgraph_node *node)
        {
           if (dump_file)
            fprintf (dump_file, 
-                    "   maxmal depth reached\n");
+                    "   maximal depth reached\n");
          continue;
        }
 
@@ -751,7 +804,7 @@ cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
       e->inline_failed = reason;
 }
 
-/* Given whole compilation unit esitmate of INSNS, compute how large we can
+/* Given whole compilation unit estimate of INSNS, compute how large we can
    allow the unit to grow.  */
 static int
 compute_max_insns (int insns)
@@ -812,12 +865,14 @@ cgraph_decide_inlining_of_small_functions (void)
   max_insns = compute_max_insns (overall_insns);
   min_insns = overall_insns;
 
-  while (overall_insns <= max_insns && (edge = fibheap_extract_min (heap)))
+  while (overall_insns <= max_insns
+        && (edge = (struct cgraph_edge *) fibheap_extract_min (heap)))
     {
       int old_insns = overall_insns;
       struct cgraph_node *where;
       int growth =
        cgraph_estimate_size_after_inlining (1, edge->caller, edge->callee);
+      const char *not_good = NULL;
 
       growth -= edge->caller->global.insns;
 
@@ -830,10 +885,11 @@ cgraph_decide_inlining_of_small_functions (void)
          fprintf (dump_file, 
                   " to be inlined into %s\n"
                   " Estimated growth after inlined into all callees is %+i insns.\n"
-                  " Estimated badness is %i.\n",
+                  " Estimated badness is %i, frequency %.2f.\n",
                   cgraph_node_name (edge->caller),
                   cgraph_estimate_growth (edge->callee),
-                  cgraph_edge_badness (edge));
+                  cgraph_edge_badness (edge),
+                  edge->frequency / (double)CGRAPH_FREQ_BASE);
          if (edge->count)
            fprintf (dump_file," Called "HOST_WIDEST_INT_PRINT_DEC"x\n", edge->count);
        }
@@ -870,13 +926,19 @@ cgraph_decide_inlining_of_small_functions (void)
            }
        }
 
-      if (!cgraph_maybe_hot_edge_p (edge) && growth > 0)
+      if (!cgraph_maybe_hot_edge_p (edge))
+       not_good = N_("call is unlikely and code size would grow");
+      if (!flag_inline_functions
+         && !DECL_DECLARED_INLINE_P (edge->callee->decl))
+       not_good = N_("function not declared inline and code size would grow");
+      if (optimize_size)
+       not_good = N_("optimizing for size and code size would grow");
+      if (not_good && growth > 0 && cgraph_estimate_growth (edge->callee) > 0)
        {
           if (!cgraph_recursive_inlining_p (edge->caller, edge->callee,
                                            &edge->inline_failed))
            {
-             edge->inline_failed = 
-               N_("call is unlikely");
+             edge->inline_failed = not_good;
              if (dump_file)
                fprintf (dump_file, " inline_failed:%s.\n", edge->inline_failed);
            }
@@ -905,8 +967,9 @@ cgraph_decide_inlining_of_small_functions (void)
       else
        {
          struct cgraph_node *callee;
-         if (!cgraph_check_inline_limits (edge->caller, edge->callee,
-                                          &edge->inline_failed, true))
+         if (CALL_STMT_CANNOT_INLINE_P (edge->call_stmt)
+             || !cgraph_check_inline_limits (edge->caller, edge->callee,
+                                             &edge->inline_failed, true))
            {
              if (dump_file)
                fprintf (dump_file, " Not inlining into %s:%s.\n",
@@ -948,7 +1011,7 @@ cgraph_decide_inlining_of_small_functions (void)
            fprintf (dump_file, "New minimal insns reached: %i\n", min_insns);
        }
     }
-  while ((edge = fibheap_extract_min (heap)) != NULL)
+  while ((edge = (struct cgraph_edge *) fibheap_extract_min (heap)) != NULL)
     {
       gcc_assert (edge->aux);
       edge->aux = NULL;
@@ -981,8 +1044,8 @@ cgraph_decide_inlining (void)
       {
        struct cgraph_edge *e;
 
-       initial_insns += node->local.self_insns;
-       gcc_assert (node->local.self_insns == node->global.insns);
+       initial_insns += inline_summary (node)->self_insns;
+       gcc_assert (inline_summary (node)->self_insns == node->global.insns);
        for (e = node->callees; e; e = e->next_callee)
          if (max_count < e->count)
            max_count = e->count;
@@ -1030,7 +1093,7 @@ cgraph_decide_inlining (void)
       for (e = node->callers; e; e = next)
        {
          next = e->next_caller;
-         if (!e->inline_failed)
+         if (!e->inline_failed || CALL_STMT_CANNOT_INLINE_P (e->call_stmt))
            continue;
          if (cgraph_recursive_inlining_p (e->caller, e->callee,
                                           &e->inline_failed))
@@ -1043,7 +1106,7 @@ cgraph_decide_inlining (void)
                     e->caller->global.insns);
        }
       /* Inlining self recursive function might introduce new calls to
-        thsemselves we didn't see in the loop above.  Fill in the proper
+        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)
@@ -1071,6 +1134,7 @@ cgraph_decide_inlining (void)
 
          if (node->callers && !node->callers->next_caller && !node->needed
              && node->local.inlinable && node->callers->inline_failed
+             && !CALL_STMT_CANNOT_INLINE_P (node->callers->call_stmt)
              && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
            {
              if (dump_file)
@@ -1126,7 +1190,7 @@ cgraph_decide_inlining (void)
    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 accomondate always inline, we however
+   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
@@ -1135,7 +1199,7 @@ static bool
 try_inline (struct cgraph_edge *e, enum inlining_mode mode, int depth)
 {
   struct cgraph_node *callee = e->callee;
-  enum inlining_mode callee_mode = (size_t) callee->aux;
+  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?  */
@@ -1144,8 +1208,17 @@ try_inline (struct cgraph_edge *e, enum inlining_mode mode, int depth)
       /* 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)
-       mode = INLINE_ALWAYS_INLINE;
-      /* Otheriwse it is time to give up.  */
+       {
+         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)
@@ -1170,7 +1243,8 @@ try_inline (struct cgraph_edge *e, enum inlining_mode mode, int depth)
               cgraph_node_name (e->callee),
               cgraph_node_name (e->caller));
     }
-  cgraph_mark_inline (e);
+  if (e->inline_failed)
+    cgraph_mark_inline (e);
 
   /* In order to fully inline always_inline functions at -O0, we need to
      recurse here, since the inlined functions might not be processed by
@@ -1189,7 +1263,8 @@ try_inline (struct cgraph_edge *e, enum inlining_mode mode, int depth)
    DEPTH is depth of recursion, used only for debug output.  */
 
 static bool
-cgraph_decide_inlining_incrementally (struct cgraph_node *node, enum inlining_mode mode,
+cgraph_decide_inlining_incrementally (struct cgraph_node *node,
+                                     enum inlining_mode mode,
                                      int depth)
 {
   struct cgraph_edge *e;
@@ -1201,13 +1276,16 @@ cgraph_decide_inlining_incrementally (struct cgraph_node *node, enum inlining_mo
   verify_cgraph_node (node);
 #endif
 
-  old_mode = (size_t)node->aux;
+  old_mode = (enum inlining_mode) (size_t)node->aux;
 
   if (mode != INLINE_ALWAYS_INLINE
       && lookup_attribute ("flatten", DECL_ATTRIBUTES (node->decl)) != NULL)
     {
       if (dump_file)
-       fprintf (dump_file, " Flattening %s\n", cgraph_node_name (node));
+       {
+         indent_to (dump_file, depth);
+         fprintf (dump_file, "Flattening %s\n", cgraph_node_name (node));
+       }
       mode = INLINE_ALL;
     }
 
@@ -1216,49 +1294,135 @@ cgraph_decide_inlining_incrementally (struct cgraph_node *node, enum inlining_mo
   /* First of all look for always inline functions.  */
   for (e = node->callees; e; e = e->next_callee)
     {
-      if (dump_file && e->callee->local.inlinable
-         && (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
-             != gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl))))
+      if (!e->callee->local.disregard_inline_limits
+         && (mode != INLINE_ALL || !e->callee->local.inlinable))
+       continue;
+      if (CALL_STMT_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)
        {
-         fprintf (dump_file, "  Ignoring %s: SSA form not computed yet.\n",
+         indent_to (dump_file, depth);
+         fprintf (dump_file,
+                  "Considering to always inline inline candidate %s.\n",
                   cgraph_node_name (e->callee));
        }
-      if ((e->callee->local.disregard_inline_limits
-          || (mode == INLINE_ALL && e->callee->local.inlinable))
-         && e->inline_failed
-         && (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
-             == gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
-         && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
-         /* ??? It is possible that renaming variable removed the function body
-            in duplicate_decls. See gcc.c-torture/compile/20011119-2.c  */
-         && (DECL_SAVED_TREE (e->callee->decl) || e->callee->inline_decl))
+      if (cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed))
        {
-         inlined |= try_inline (e, mode, depth);
+         if (dump_file)
+           {
+             indent_to (dump_file, depth);
+             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)
+           {
+             indent_to (dump_file, depth);
+             fprintf (dump_file, "Not inlining: SSA form does not match.\n");
+           }
+         continue;
+       }
+      if (!DECL_SAVED_TREE (e->callee->decl) && !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 (!flag_really_no_inline && mode != INLINE_ALL
       && mode != INLINE_ALWAYS_INLINE)
     for (e = node->callees; e; e = e->next_callee)
-      if (e->callee->local.inlinable
-         && e->inline_failed
-         && !e->callee->local.disregard_inline_limits
-         && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
-         && (gimple_in_ssa_p (DECL_STRUCT_FUNCTION (node->decl))
-             == gimple_in_ssa_p (DECL_STRUCT_FUNCTION (e->callee->decl)))
-         && (mode != INLINE_SIZE
-             || (cgraph_estimate_size_after_inlining (1, e->caller, e->callee)
-                 <= e->caller->global.insns))
-         && cgraph_check_inline_limits (node, e->callee, &e->inline_failed,
-                                        false)
-         && (DECL_SAVED_TREE (e->callee->decl) || e->callee->inline_decl))
-       {
-         if (cgraph_default_inline_p (e->callee, &failed_reason))
-           inlined |= try_inline (e, mode, depth);
-         else if (!flag_unit_at_a_time)
-           e->inline_failed = failed_reason;
-       }
+      {
+       if (!e->callee->local.inlinable
+           || !e->inline_failed
+           || e->callee->local.disregard_inline_limits)
+         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)
+             {
+               indent_to (dump_file, depth);
+               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)
+             {
+               indent_to (dump_file, depth);
+               fprintf (dump_file, "Not inlining: SSA form does not match.\n");
+             }
+           continue;
+         }
+       /* 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.insns)
+           && cgraph_estimate_growth (e->callee) > 0)
+         {
+           if (dump_file)
+             {
+               indent_to (dump_file, depth);
+               fprintf (dump_file,
+                        "Not inlining: code size would grow by %i insns.\n",
+                        cgraph_estimate_size_after_inlining (1, e->caller,
+                                                             e->callee)
+                        - e->caller->global.insns);
+             }
+           continue;
+         }
+       if (!cgraph_check_inline_limits (node, e->callee, &e->inline_failed,
+                                       false)
+           || CALL_STMT_CANNOT_INLINE_P (e->call_stmt))
+         {
+           if (dump_file)
+             {
+               indent_to (dump_file, depth);
+               fprintf (dump_file, "Not inlining: %s.\n", e->inline_failed);
+             }
+           continue;
+         }
+       if (!DECL_SAVED_TREE (e->callee->decl) && !e->callee->inline_decl)
+         {
+           if (dump_file)
+             {
+               indent_to (dump_file, depth);
+               fprintf (dump_file,
+                        "Not inlining: Function body no longer available.\n");
+             }
+           continue;
+         }
+       if (cgraph_default_inline_p (e->callee, &failed_reason))
+         inlined |= try_inline (e, mode, depth);
+       else if (!flag_unit_at_a_time)
+         e->inline_failed = failed_reason;
+      }
   node->aux = (void *)(size_t) old_mode;
   return inlined;
 }
@@ -1270,24 +1434,6 @@ cgraph_gate_inlining (void)
   return flag_inline_trees;
 }
 
-struct tree_opt_pass pass_ipa_inline = 
-{
-  "inline",                            /* name */
-  cgraph_gate_inlining,                        /* gate */
-  cgraph_decide_inlining,              /* execute */
-  NULL,                                        /* sub */
-  NULL,                                        /* next */
-  0,                                   /* static_pass_number */
-  TV_INLINE_HEURISTICS,                        /* tv_id */
-  0,                                   /* properties_required */
-  PROP_cfg,                            /* properties_provided */
-  0,                                   /* properties_destroyed */
-  TODO_remove_functions,               /* todo_flags_finish */
-  TODO_dump_cgraph | TODO_dump_func
-  | TODO_remove_functions,             /* todo_flags_finish */
-  0                                    /* letter */
-};
-
 /* Because inlining might remove no-longer reachable nodes, we need to
    keep the array visible to garbage collector to avoid reading collected
    out nodes.  */
@@ -1306,7 +1452,7 @@ cgraph_early_inlining (void)
   if (sorrycount || errorcount)
     return 0;
   if (cgraph_decide_inlining_incrementally (node,
-                                           flag_unit_at_a_time
+                                           flag_unit_at_a_time || optimize_size
                                            ? INLINE_SIZE : INLINE_SPEED, 0))
     {
       timevar_push (TV_INTEGRATION);
@@ -1323,8 +1469,10 @@ cgraph_gate_early_inlining (void)
   return flag_inline_trees && flag_early_inlining;
 }
 
-struct tree_opt_pass pass_early_inline = 
+struct gimple_opt_pass pass_early_inline = 
 {
+ {
+  GIMPLE_PASS,
   "einline",                           /* name */
   cgraph_gate_early_inlining,          /* gate */
   cgraph_early_inlining,               /* execute */
@@ -1336,8 +1484,8 @@ struct tree_opt_pass pass_early_inline =
   PROP_cfg,                            /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  TODO_dump_func,                      /* todo_flags_finish */
-  0                                    /* letter */
+  TODO_dump_func                       /* todo_flags_finish */
+ }
 };
 
 /* When inlining shall be performed.  */
@@ -1351,8 +1499,10 @@ cgraph_gate_ipa_early_inlining (void)
 
 /* 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 tree_opt_pass pass_ipa_early_inline = 
+struct simple_ipa_opt_pass pass_ipa_early_inline = 
 {
+ {
+  SIMPLE_IPA_PASS,
   "einline_ipa",                       /* name */
   cgraph_gate_ipa_early_inlining,      /* gate */
   NULL,                                        /* execute */
@@ -1364,30 +1514,40 @@ struct tree_opt_pass pass_ipa_early_inline =
   PROP_cfg,                            /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  TODO_dump_cgraph,                    /* todo_flags_finish */
-  0                                    /* letter */
+  TODO_dump_cgraph                     /* todo_flags_finish */
+ }
 };
 
 /* Compute parameters of functions used by inliner.  */
-static unsigned int
-compute_inline_parameters (void)
+unsigned int
+compute_inline_parameters (struct cgraph_node *node)
 {
-  struct cgraph_node *node = cgraph_node (current_function_decl);
-
   gcc_assert (!node->global.inlined_to);
-  node->local.estimated_self_stack_size = estimated_stack_frame_size ();
-  node->global.estimated_stack_size = node->local.estimated_self_stack_size;
+  inline_summary (node)->estimated_self_stack_size
+    = estimated_stack_frame_size ();
+  node->global.estimated_stack_size
+    = inline_summary (node)->estimated_self_stack_size;
   node->global.stack_frame_offset = 0;
   node->local.inlinable = tree_inlinable_function_p (current_function_decl);
-  node->local.self_insns = estimate_num_insns (current_function_decl,
-                                              &eni_inlining_weights);
-  if (node->local.inlinable)
+  inline_summary (node)->self_insns = estimate_num_insns (current_function_decl,
+                                                         &eni_inlining_weights);
+  if (node->local.inlinable && !node->local.disregard_inline_limits)
     node->local.disregard_inline_limits
-      = lang_hooks.tree_inlining.disregard_inline_limits (current_function_decl);
+      = DECL_DISREGARD_INLINE_LIMITS (current_function_decl);
   if (flag_really_no_inline && !node->local.disregard_inline_limits)
     node->local.inlinable = 0;
   /* Inlining characteristics are maintained by the cgraph_mark_inline.  */
-  node->global.insns = node->local.self_insns;
+  node->global.insns = inline_summary (node)->self_insns;
+  return 0;
+}
+
+
+/* Compute parameters of functions used by inliner using
+   current_function_decl.  */
+static unsigned int
+compute_inline_parameters_for_current (void)
+{
+  compute_inline_parameters (cgraph_node (current_function_decl));
   return 0;
 }
 
@@ -1398,11 +1558,13 @@ gate_inline_passes (void)
   return flag_inline_trees;
 }
 
-struct tree_opt_pass pass_inline_parameters = 
+struct gimple_opt_pass pass_inline_parameters = 
 {
+ {
+  GIMPLE_PASS,
   NULL,                                        /* name */
   gate_inline_passes,                  /* gate */
-  compute_inline_parameters,           /* execute */
+  compute_inline_parameters_for_current,/* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
@@ -1411,22 +1573,45 @@ struct tree_opt_pass pass_inline_parameters =
   PROP_cfg,                            /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  0,                                   /* todo_flags_finish */
-  0                                    /* letter */
+  0                                    /* todo_flags_finish */
+ }
 };
 
-/* Apply inline plan to the function.  */
+/* Note function body size.  */
+static void
+inline_generate_summary (void)
+{
+  struct cgraph_node **order =
+    XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
+  int nnodes = cgraph_postorder (order);
+  int i;
+
+  for (i = nnodes - 1; i >= 0; i--)
+    {
+      struct cgraph_node *node = order[i];
+      
+      /* Allow possibly removed nodes to be garbage collected.  */
+      order[i] = NULL;
+      if (node->analyzed && (node->needed || node->reachable))
+       {
+         push_cfun (DECL_STRUCT_FUNCTION (node->decl));
+         current_function_decl = node->decl;
+         compute_inline_parameters (node);
+         pop_cfun ();
+       }
+    }
+  
+  current_function_decl = NULL;
+  free (order);
+  return;
+}
+
+/* Apply inline plan to function.  */
 static unsigned int
-apply_inline (void)
+inline_transform (struct cgraph_node *node)
 {
   unsigned int todo = 0;
   struct cgraph_edge *e;
-  struct cgraph_node *node = cgraph_node (current_function_decl);
-
-  /* Even when not optimizing, ensure that always_inline functions get inlined.
-   */
-  if (!optimize)
-   cgraph_decide_inlining_incrementally (node, INLINE_SPEED, 0);
 
   /* We might need the body of this function so that we can expand
      it inline somewhere else.  */
@@ -1442,6 +1627,64 @@ apply_inline (void)
       todo = optimize_inline_calls (current_function_decl);
       timevar_pop (TV_INTEGRATION);
     }
+  return todo | execute_fixup_cfg ();
+}
+
+struct ipa_opt_pass pass_ipa_inline = 
+{
+ {
+  IPA_PASS,
+  "inline",                            /* name */
+  cgraph_gate_inlining,                        /* gate */
+  cgraph_decide_inlining,              /* execute */
+  NULL,                                        /* sub */
+  NULL,                                        /* next */
+  0,                                   /* static_pass_number */
+  TV_INLINE_HEURISTICS,                        /* tv_id */
+  0,                                   /* properties_required */
+  PROP_cfg,                            /* properties_provided */
+  0,                                   /* properties_destroyed */
+  TODO_remove_functions,               /* todo_flags_finish */
+  TODO_dump_cgraph | TODO_dump_func
+  | TODO_remove_functions              /* todo_flags_finish */
+ },
+ inline_generate_summary,              /* generate_summary */
+ NULL,                                 /* write_summary */
+ NULL,                                 /* read_summary */
+ NULL,                                 /* function_read_summary */
+ 0,                                    /* TODOs */
+ inline_transform,                     /* function_transform */
+ NULL,                                 /* variable_transform */
+};
+
+
+/* When inlining shall be performed.  */
+static bool
+cgraph_gate_O0_always_inline (void)
+{
+  return !flag_unit_at_a_time || !flag_inline_trees;
+}
+
+static unsigned int
+cgraph_O0_always_inline (void)
+{
+  struct cgraph_node *node = cgraph_node (current_function_decl);
+  unsigned int todo = 0;
+  bool inlined;
+
+  if (sorrycount || errorcount)
+    return 0;
+  inlined = cgraph_decide_inlining_incrementally (node, INLINE_SPEED, 0);
+  /* We might need the body of this function so that we can expand
+     it inline somewhere else.  */
+  if (cgraph_preserve_function_body_p (current_function_decl))
+    save_inline_function_body (node);
+  if (inlined || warn_inline)
+    {
+      timevar_push (TV_INTEGRATION);
+      todo = optimize_inline_calls (current_function_decl);
+      timevar_pop (TV_INTEGRATION);
+    }
   /* In non-unit-at-a-time we must mark all referenced functions as needed.  */
   if (!flag_unit_at_a_time)
     {
@@ -1453,11 +1696,13 @@ apply_inline (void)
   return todo | execute_fixup_cfg ();
 }
 
-struct tree_opt_pass pass_apply_inline = 
+struct gimple_opt_pass pass_O0_always_inline = 
 {
-  "apply_inline",                      /* name */
-  NULL,                                        /* gate */
-  apply_inline,                                /* execute */
+ {
+  GIMPLE_PASS,
+  "always_inline",                     /* name */
+  cgraph_gate_O0_always_inline,                /* gate */
+  cgraph_O0_always_inline,             /* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
@@ -1467,8 +1712,8 @@ struct tree_opt_pass pass_apply_inline =
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
   TODO_dump_func | TODO_verify_flow
-  | TODO_verify_stmts,                 /* todo_flags_finish */
-  0                                    /* letter */
+  | TODO_verify_stmts                  /* todo_flags_finish */
+ }
 };
 
 #include "gt-ipa-inline.h"