OSDN Git Service

PR objc/21641
[pf3gnuchains/gcc-fork.git] / gcc / tree-inline.c
1 /* Tree inlining.
2    Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Alexandre Oliva <aoliva@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "rtl.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "insn-config.h"
35 #include "varray.h"
36 #include "hashtab.h"
37 #include "splay-tree.h"
38 #include "langhooks.h"
39 #include "basic-block.h"
40 #include "tree-iterator.h"
41 #include "cgraph.h"
42 #include "intl.h"
43 #include "tree-mudflap.h"
44 #include "tree-flow.h"
45 #include "function.h"
46 #include "ggc.h"
47 #include "tree-flow.h"
48 #include "diagnostic.h"
49 #include "except.h"
50 #include "debug.h"
51 #include "pointer-set.h"
52 #include "integrate.h"
53
54 /* I'm not real happy about this, but we need to handle gimple and
55    non-gimple trees.  */
56 #include "tree-gimple.h"
57
58 /* Inlining, Saving, Cloning
59
60    Inlining: a function body is duplicated, but the PARM_DECLs are
61    remapped into VAR_DECLs, and non-void RETURN_EXPRs become
62    MODIFY_EXPRs that store to a dedicated returned-value variable.
63    The duplicated eh_region info of the copy will later be appended
64    to the info for the caller; the eh_region info in copied throwing
65    statements and RESX_EXPRs is adjusted accordingly.
66
67    Saving: make a semantically-identical copy of the function body.
68    Necessary when we want to generate code for the body (a destructive
69    operation), but we expect to need this body in the future (e.g. for
70    inlining into another function).
71
72    Cloning: (only in C++) We have one body for a con/de/structor, and
73    multiple function decls, each with a unique parameter list.
74    Duplicate the body, using the given splay tree; some parameters
75    will become constants (like 0 or 1).
76
77    All of these will simultaneously lookup any callgraph edges.  If
78    we're going to inline the duplicated function body, and the given
79    function has some cloned callgraph nodes (one for each place this
80    function will be inlined) those callgraph edges will be duplicated.
81    If we're saving or cloning the body, those callgraph edges will be
82    updated to point into the new body.  (Note that the original
83    callgraph node and edge list will not be altered.)
84
85    See the CALL_EXPR handling case in copy_body_r ().  */
86
87 /* 0 if we should not perform inlining.
88    1 if we should expand functions calls inline at the tree level.
89    2 if we should consider *all* functions to be inline
90    candidates.  */
91
92 int flag_inline_trees = 0;
93
94 /* To Do:
95
96    o In order to make inlining-on-trees work, we pessimized
97      function-local static constants.  In particular, they are now
98      always output, even when not addressed.  Fix this by treating
99      function-local static constants just like global static
100      constants; the back-end already knows not to output them if they
101      are not needed.
102
103    o Provide heuristics to clamp inlining of recursive template
104      calls?  */
105
106 /* Data required for function inlining.  */
107
108 typedef struct inline_data
109 {
110   /* FUNCTION_DECL for function being inlined.  */
111   tree callee;
112   /* FUNCTION_DECL for function being inlined into.  */
113   tree caller;
114   /* struct function for function being inlined.  Usually this is the same
115      as DECL_STRUCT_FUNCTION (callee), but can be different if saved_cfg
116      and saved_eh are in use.  */
117   struct function *callee_cfun;
118   /* The VAR_DECL for the return value.  */
119   tree retvar;
120   /* The map from local declarations in the inlined function to
121      equivalents in the function into which it is being inlined.  */
122   splay_tree decl_map;
123   /* We use the same mechanism to build clones that we do to perform
124      inlining.  However, there are a few places where we need to
125      distinguish between those two situations.  This flag is true if
126      we are cloning, rather than inlining.  */
127   bool cloning_p;
128   /* Similarly for saving function body.  */
129   bool saving_p;
130   /* Callgraph node of function we are inlining into.  */
131   struct cgraph_node *node;
132   /* Callgraph node of currently inlined function.  */
133   struct cgraph_node *current_node;
134   /* Current BLOCK.  */
135   tree block;
136   /* Exception region the inlined call lie in.  */
137   int eh_region;
138   /* Take region number in the function being copied, add this value and
139      get eh region number of the duplicate in the function we inline into.  */
140   int eh_region_offset;
141 } inline_data;
142
143 /* Prototypes.  */
144
145 static tree declare_return_variable (inline_data *, tree, tree, tree *);
146 static tree copy_body_r (tree *, int *, void *);
147 static tree copy_generic_body (inline_data *);
148 static bool inlinable_function_p (tree);
149 static tree remap_decl (tree, inline_data *);
150 static tree remap_type (tree, inline_data *);
151 static void remap_block (tree *, inline_data *);
152 static tree remap_decl (tree, inline_data *);
153 static tree remap_decls (tree, inline_data *);
154 static void copy_bind_expr (tree *, int *, inline_data *);
155 static tree mark_local_for_remap_r (tree *, int *, void *);
156 static void unsave_expr_1 (tree);
157 static tree unsave_r (tree *, int *, void *);
158 static void declare_inline_vars (tree, tree);
159 static void remap_save_expr (tree *, void *, int *);
160
161 static inline bool inlining_p (inline_data *id);
162
163 /* Insert a tree->tree mapping for ID.  Despite the name suggests
164    that the trees should be variables, it is used for more than that.  */
165
166 static void
167 insert_decl_map (inline_data *id, tree key, tree value)
168 {
169   splay_tree_insert (id->decl_map, (splay_tree_key) key,
170                      (splay_tree_value) value);
171
172   /* Always insert an identity map as well.  If we see this same new
173      node again, we won't want to duplicate it a second time.  */
174   if (key != value)
175     splay_tree_insert (id->decl_map, (splay_tree_key) value,
176                        (splay_tree_value) value);
177 }
178
179 /* Remap DECL during the copying of the BLOCK tree for the function.  */
180
181 static tree
182 remap_decl (tree decl, inline_data *id)
183 {
184   splay_tree_node n;
185   tree fn;
186
187   /* We only remap local variables in the current function.  */
188   fn = id->callee;
189
190   /* See if we have remapped this declaration.  */
191
192   n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
193
194   /* If we didn't already have an equivalent for this declaration,
195      create one now.  */
196   if (!n)
197     {
198       /* Make a copy of the variable or label.  */
199       tree t;
200       t = copy_decl_for_inlining (decl, fn, id->caller);
201
202       /* Remember it, so that if we encounter this local entity again
203          we can reuse this copy.  Do this early because remap_type may
204          need this decl for TYPE_STUB_DECL.  */
205       insert_decl_map (id, decl, t);
206
207       /* Remap types, if necessary.  */
208       TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
209       if (TREE_CODE (t) == TYPE_DECL)
210         DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
211       else if (TREE_CODE (t) == PARM_DECL)
212         DECL_ARG_TYPE_AS_WRITTEN (t)
213           = remap_type (DECL_ARG_TYPE_AS_WRITTEN (t), id);
214
215       /* Remap sizes as necessary.  */
216       walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
217       walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
218
219       /* If fields, do likewise for offset and qualifier.  */
220       if (TREE_CODE (t) == FIELD_DECL)
221         {
222           walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
223           if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
224             walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
225         }
226
227 #if 0
228       /* FIXME handle anon aggrs.  */
229       if (! DECL_NAME (t) && TREE_TYPE (t)
230           && lang_hooks.tree_inlining.anon_aggr_type_p (TREE_TYPE (t)))
231         {
232           /* For a VAR_DECL of anonymous type, we must also copy the
233              member VAR_DECLS here and rechain the DECL_ANON_UNION_ELEMS.  */
234           tree members = NULL;
235           tree src;
236
237           for (src = DECL_ANON_UNION_ELEMS (t); src;
238                src = TREE_CHAIN (src))
239             {
240               tree member = remap_decl (TREE_VALUE (src), id);
241
242               gcc_assert (!TREE_PURPOSE (src));
243               members = tree_cons (NULL, member, members);
244             }
245           DECL_ANON_UNION_ELEMS (t) = nreverse (members);
246         }
247 #endif
248
249       /* If we are inlining and this is a variable (not a label), declare the
250          remapped variable in the callers' body.  */
251       if (inlining_p (id)
252           && (TREE_CODE (t) == VAR_DECL
253               || TREE_CODE (t) == PARM_DECL))
254         declare_inline_vars (id->block, t);
255
256       /* Remember it, so that if we encounter this local entity
257          again we can reuse this copy.  */
258       insert_decl_map (id, decl, t);
259       return t;
260     }
261
262   return unshare_expr ((tree) n->value);
263 }
264
265 static tree
266 remap_type (tree type, inline_data *id)
267 {
268   splay_tree_node node;
269   tree new, t;
270
271   if (type == NULL)
272     return type;
273
274   /* See if we have remapped this type.  */
275   node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
276   if (node)
277     return (tree) node->value;
278
279   /* The type only needs remapping if it's variably modified.  */
280   if (! variably_modified_type_p (type, id->callee))
281     {
282       insert_decl_map (id, type, type);
283       return type;
284     }
285
286   /* We do need a copy.  build and register it now.  If this is a pointer or
287      reference type, remap the designated type and make a new pointer or
288      reference type.  */
289   if (TREE_CODE (type) == POINTER_TYPE)
290     {
291       new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
292                                          TYPE_MODE (type),
293                                          TYPE_REF_CAN_ALIAS_ALL (type));
294       insert_decl_map (id, type, new);
295       return new;
296     }
297   else if (TREE_CODE (type) == REFERENCE_TYPE)
298     {
299       new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
300                                             TYPE_MODE (type),
301                                             TYPE_REF_CAN_ALIAS_ALL (type));
302       insert_decl_map (id, type, new);
303       return new;
304     }
305   else
306     new = copy_node (type);
307
308   insert_decl_map (id, type, new);
309
310   /* This is a new type, not a copy of an old type.  Need to reassociate
311      variants.  We can handle everything except the main variant lazily.  */
312   t = TYPE_MAIN_VARIANT (type);
313   if (type != t)
314     {
315       t = remap_type (t, id);
316       TYPE_MAIN_VARIANT (new) = t;
317       TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
318       TYPE_NEXT_VARIANT (t) = new;
319     }
320   else
321     {
322       TYPE_MAIN_VARIANT (new) = new;
323       TYPE_NEXT_VARIANT (new) = NULL;
324     }
325
326   if (TYPE_STUB_DECL (type))
327     TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
328
329   /* Lazily create pointer and reference types.  */
330   TYPE_POINTER_TO (new) = NULL;
331   TYPE_REFERENCE_TO (new) = NULL;
332
333   switch (TREE_CODE (new))
334     {
335     case INTEGER_TYPE:
336     case REAL_TYPE:
337     case ENUMERAL_TYPE:
338     case BOOLEAN_TYPE:
339     case CHAR_TYPE:
340       t = TYPE_MIN_VALUE (new);
341       if (t && TREE_CODE (t) != INTEGER_CST)
342         walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
343
344       t = TYPE_MAX_VALUE (new);
345       if (t && TREE_CODE (t) != INTEGER_CST)
346         walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
347       return new;
348
349     case FUNCTION_TYPE:
350       TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
351       walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
352       return new;
353
354     case ARRAY_TYPE:
355       TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
356       TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
357       break;
358
359     case RECORD_TYPE:
360     case UNION_TYPE:
361     case QUAL_UNION_TYPE:
362       walk_tree (&TYPE_FIELDS (new), copy_body_r, id, NULL);
363       break;
364
365     case OFFSET_TYPE:
366     default:
367       /* Shouldn't have been thought variable sized.  */
368       gcc_unreachable ();
369     }
370
371   walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
372   walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
373
374   return new;
375 }
376
377 static tree
378 remap_decls (tree decls, inline_data *id)
379 {
380   tree old_var;
381   tree new_decls = NULL_TREE;
382
383   /* Remap its variables.  */
384   for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
385     {
386       tree new_var;
387
388       /* Remap the variable.  */
389       new_var = remap_decl (old_var, id);
390
391       /* If we didn't remap this variable, so we can't mess with its
392          TREE_CHAIN.  If we remapped this variable to the return slot, it's
393          already declared somewhere else, so don't declare it here.  */
394       if (!new_var || new_var == id->retvar)
395         ;
396       else
397         {
398           gcc_assert (DECL_P (new_var));
399           TREE_CHAIN (new_var) = new_decls;
400           new_decls = new_var;
401         }
402     }
403
404   return nreverse (new_decls);
405 }
406
407 /* Copy the BLOCK to contain remapped versions of the variables
408    therein.  And hook the new block into the block-tree.  */
409
410 static void
411 remap_block (tree *block, inline_data *id)
412 {
413   tree old_block;
414   tree new_block;
415   tree fn;
416
417   /* Make the new block.  */
418   old_block = *block;
419   new_block = make_node (BLOCK);
420   TREE_USED (new_block) = TREE_USED (old_block);
421   BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
422   *block = new_block;
423
424   /* Remap its variables.  */
425   BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
426
427   fn = id->caller;
428 #if 1
429   /* FIXME!  It shouldn't be so hard to manage blocks.  Rebuilding them in
430      rest_of_compilation is a good start.  */
431   if (id->cloning_p)
432     /* We're building a clone; DECL_INITIAL is still
433        error_mark_node, and current_binding_level is the parm
434        binding level.  */
435     lang_hooks.decls.insert_block (new_block);
436   else
437     {
438       /* Attach this new block after the DECL_INITIAL block for the
439          function into which this block is being inlined.  In
440          rest_of_compilation we will straighten out the BLOCK tree.  */
441       tree *first_block;
442       if (DECL_INITIAL (fn))
443         first_block = &BLOCK_CHAIN (DECL_INITIAL (fn));
444       else
445         first_block = &DECL_INITIAL (fn);
446       BLOCK_CHAIN (new_block) = *first_block;
447       *first_block = new_block;
448     }
449 #endif
450   /* Remember the remapped block.  */
451   insert_decl_map (id, old_block, new_block);
452 }
453
454 static void
455 copy_statement_list (tree *tp)
456 {
457   tree_stmt_iterator oi, ni;
458   tree new;
459
460   new = alloc_stmt_list ();
461   ni = tsi_start (new);
462   oi = tsi_start (*tp);
463   *tp = new;
464
465   for (; !tsi_end_p (oi); tsi_next (&oi))
466     tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
467 }
468
469 static void
470 copy_bind_expr (tree *tp, int *walk_subtrees, inline_data *id)
471 {
472   tree block = BIND_EXPR_BLOCK (*tp);
473   /* Copy (and replace) the statement.  */
474   copy_tree_r (tp, walk_subtrees, NULL);
475   if (block)
476     {
477       remap_block (&block, id);
478       BIND_EXPR_BLOCK (*tp) = block;
479     }
480
481   if (BIND_EXPR_VARS (*tp))
482     /* This will remap a lot of the same decls again, but this should be
483        harmless.  */
484     BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
485 }
486
487 /* Called from copy_body_id via walk_tree.  DATA is really an
488    `inline_data *'.  */
489
490 static tree
491 copy_body_r (tree *tp, int *walk_subtrees, void *data)
492 {
493   inline_data *id = (inline_data *) data;
494   tree fn = id->callee;
495
496   /* Begin by recognizing trees that we'll completely rewrite for the
497      inlining context.  Our output for these trees is completely
498      different from out input (e.g. RETURN_EXPR is deleted, and morphs
499      into an edge).  Further down, we'll handle trees that get
500      duplicated and/or tweaked.  */
501
502   /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
503      GOTO_STMT with the RET_LABEL as its target.  */
504   if (TREE_CODE (*tp) == RETURN_EXPR && inlining_p (id))
505     {
506       tree assignment = TREE_OPERAND (*tp, 0);
507
508       /* If we're returning something, just turn that into an
509          assignment into the equivalent of the original RESULT_DECL.
510          If the "assignment" is just the result decl, the result
511          decl has already been set (e.g. a recent "foo (&result_decl,
512          ...)"); just toss the entire RETURN_EXPR.  */
513       if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
514         {
515           /* Replace the RETURN_EXPR with (a copy of) the
516              MODIFY_EXPR hangning underneath.  */
517           *tp = copy_node (assignment);
518         }
519       else /* Else the RETURN_EXPR returns no value.  */
520         {
521           *tp = NULL;
522           return (void *)1;
523         }
524     }
525
526   /* Local variables and labels need to be replaced by equivalent
527      variables.  We don't want to copy static variables; there's only
528      one of those, no matter how many times we inline the containing
529      function.  Similarly for globals from an outer function.  */
530   else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
531     {
532       tree new_decl;
533
534       /* Remap the declaration.  */
535       new_decl = remap_decl (*tp, id);
536       gcc_assert (new_decl);
537       /* Replace this variable with the copy.  */
538       STRIP_TYPE_NOPS (new_decl);
539       *tp = new_decl;
540       *walk_subtrees = 0;
541     }
542   else if (TREE_CODE (*tp) == STATEMENT_LIST)
543     copy_statement_list (tp);
544   else if (TREE_CODE (*tp) == SAVE_EXPR)
545     remap_save_expr (tp, id->decl_map, walk_subtrees);
546   else if (TREE_CODE (*tp) == LABEL_DECL)
547     /* These may need to be remapped for EH handling.  */
548     remap_decl (*tp, id);
549   else if (TREE_CODE (*tp) == BIND_EXPR)
550     copy_bind_expr (tp, walk_subtrees, id);
551   /* Types may need remapping as well.  */
552   else if (TYPE_P (*tp))
553     *tp = remap_type (*tp, id);
554
555   /* If this is a constant, we have to copy the node iff the type will be
556      remapped.  copy_tree_r will not copy a constant.  */
557   else if (CONSTANT_CLASS_P (*tp))
558     {
559       tree new_type = remap_type (TREE_TYPE (*tp), id);
560
561       if (new_type == TREE_TYPE (*tp))
562         *walk_subtrees = 0;
563
564       else if (TREE_CODE (*tp) == INTEGER_CST)
565         *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
566                                   TREE_INT_CST_HIGH (*tp));
567       else
568         {
569           *tp = copy_node (*tp);
570           TREE_TYPE (*tp) = new_type;
571         }
572     }
573
574   /* Otherwise, just copy the node.  Note that copy_tree_r already
575      knows not to copy VAR_DECLs, etc., so this is safe.  */
576   else
577     {
578       tree old_node = *tp;
579
580       /* Here we handle trees that are not completely rewritten.
581          First we detect some inlining-induced bogosities for
582          discarding.  */
583       if (TREE_CODE (*tp) == MODIFY_EXPR
584           && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
585           && (lang_hooks.tree_inlining.auto_var_in_fn_p
586               (TREE_OPERAND (*tp, 0), fn)))
587         {
588           /* Some assignments VAR = VAR; don't generate any rtl code
589              and thus don't count as variable modification.  Avoid
590              keeping bogosities like 0 = 0.  */
591           tree decl = TREE_OPERAND (*tp, 0), value;
592           splay_tree_node n;
593
594           n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
595           if (n)
596             {
597               value = (tree) n->value;
598               STRIP_TYPE_NOPS (value);
599               if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
600                 {
601                   *tp = build_empty_stmt ();
602                   return copy_body_r (tp, walk_subtrees, data);
603                 }
604             }
605         }
606       else if (TREE_CODE (*tp) == INDIRECT_REF)
607         {
608           /* Get rid of *& from inline substitutions that can happen when a
609              pointer argument is an ADDR_EXPR.  */
610           tree decl = TREE_OPERAND (*tp, 0);
611           splay_tree_node n;
612
613           n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
614           if (n)
615             {
616               *tp = build_fold_indirect_ref ((tree)n->value);
617               *walk_subtrees = 0;
618               return NULL;
619             }
620         }
621
622       /* Here is the "usual case".  Copy this tree node, and then
623          tweak some special cases.  */
624       copy_tree_r (tp, walk_subtrees, NULL);
625       if (id->block
626           && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp))))
627         TREE_BLOCK (*tp) = id->block;
628
629       /* We're duplicationg a CALL_EXPR.  Find any corresponding
630          callgraph edges and update or duplicate them.  */
631       if (TREE_CODE (*tp) == CALL_EXPR && id->node && get_callee_fndecl (*tp))
632         {
633           if (id->saving_p)
634             {
635               struct cgraph_node *node;
636               struct cgraph_edge *edge;
637
638               /* We're saving a copy of the body, so we'll update the
639                  callgraph nodes in place.  Note that we avoid
640                  altering the original callgraph node; we begin with
641                  the first clone.  */
642               for (node = id->node->next_clone;
643                    node;
644                    node = node->next_clone)
645                 {
646                   edge = cgraph_edge (node, old_node);
647                   gcc_assert (edge);
648                   edge->call_expr = *tp;
649                 }
650             }
651           else
652             {
653               struct cgraph_edge *edge;
654
655               /* We're cloning or inlining this body; duplicate the
656                  associate callgraph nodes.  */
657               edge = cgraph_edge (id->current_node, old_node);
658               if (edge)
659                  cgraph_clone_edge (edge, id->node, *tp);
660             }
661         }
662       else if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
663         TREE_OPERAND (*tp, 0) =
664           build_int_cst
665             (NULL_TREE,
666              id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
667
668       TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
669
670       /* The copied TARGET_EXPR has never been expanded, even if the
671          original node was expanded already.  */
672       if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
673         {
674           TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
675           TREE_OPERAND (*tp, 3) = NULL_TREE;
676         }
677
678       /* Variable substitution need not be simple.  In particular, the
679          INDIRECT_REF substitution above.  Make sure that TREE_CONSTANT
680          and friends are up-to-date.  */
681       else if (TREE_CODE (*tp) == ADDR_EXPR)
682         {
683           walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
684           recompute_tree_invarant_for_addr_expr (*tp);
685           *walk_subtrees = 0;
686         }
687     }
688
689   /* Keep iterating.  */
690   return NULL_TREE;
691 }
692
693 /* Copy basic block, scale profile accordingly.  Edges will be taken care of
694    later  */
695
696 static basic_block
697 copy_bb (inline_data *id, basic_block bb, int frequency_scale, int count_scale)
698 {
699   block_stmt_iterator bsi, copy_bsi;
700   basic_block copy_basic_block;
701
702   /* create_basic_block() will append every new block to
703      basic_block_info automatically.  */
704   copy_basic_block = create_basic_block (NULL, (void *) 0, bb->prev_bb->aux);
705   copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
706   copy_basic_block->frequency = (bb->frequency
707                                      * frequency_scale / REG_BR_PROB_BASE);
708   copy_bsi = bsi_start (copy_basic_block);
709
710   for (bsi = bsi_start (bb);
711        !bsi_end_p (bsi); bsi_next (&bsi))
712     {
713       tree stmt = bsi_stmt (bsi);
714       tree orig_stmt = stmt;
715
716       walk_tree (&stmt, copy_body_r, id, NULL);
717
718       /* RETURN_EXPR might be removed,
719          this is signalled by making stmt pointer NULL.  */
720       if (stmt)
721         {
722           bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
723           /* If you think we can abort here, you are wrong.
724              There is no region 0 in tree land.  */
725           gcc_assert (lookup_stmt_eh_region_fn (id->callee_cfun, orig_stmt)
726                       != 0);
727
728           if (tree_could_throw_p (stmt))
729             {
730               int region = lookup_stmt_eh_region_fn (id->callee_cfun, orig_stmt);
731               /* Add an entry for the copied tree in the EH hashtable.
732                  When saving or cloning or versioning, use the hashtable in
733                  cfun, and just copy the EH number.  When inlining, use the
734                  hashtable in the caller, and adjust the region number.  */
735               if (region > 0)
736                 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
737
738               /* If this tree doesn't have a region associated with it,
739                  and there is a "current region,"
740                  then associate this tree with the current region
741                  and add edges associated with this region.  */
742               if ((lookup_stmt_eh_region_fn (id->callee_cfun,
743                                              orig_stmt) <= 0
744                    && id->eh_region > 0)
745                   && tree_could_throw_p (stmt))
746                 add_stmt_to_eh_region (stmt, id->eh_region);
747             }
748         }
749     }
750   return copy_basic_block;
751 }
752
753 /* Copy edges from BB into it's copy constructed ealier, scale profile
754    accordingly.  Edges will be taken care of
755    later.  Assume aux pointers to point to the copies of each BB  */
756 static void
757 copy_edges_for_bb (basic_block bb, int count_scale)
758 {
759   basic_block new_bb = bb->aux;
760   edge_iterator ei;
761   edge old_edge;
762   block_stmt_iterator bsi;
763   int flags;
764
765   /* Use the indices from the original blocks to create edges for the
766      new ones.  */
767   FOR_EACH_EDGE (old_edge, ei, bb->succs)
768     {
769       edge new;
770
771       flags = old_edge->flags;
772
773       /* Return edges do get a FALLTHU flag when the get inlined.  */
774       if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
775           && old_edge->dest->aux != EXIT_BLOCK_PTR)
776         flags |= EDGE_FALLTHRU;
777       new = make_edge (new_bb, old_edge->dest->aux, flags);
778       new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
779       new->probability = old_edge->probability;
780     }
781
782   if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
783     return;
784
785   tree_purge_dead_eh_edges (new_bb);
786   for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
787     {
788       tree copy_stmt;
789
790       copy_stmt = bsi_stmt (bsi);
791       update_stmt (copy_stmt);
792       /* Do this before the possible split_block.  */
793       bsi_next (&bsi);
794
795       /* If this tree could throw an exception, there are two
796          cases where we need to add abnormal edge(s): the
797          tree wasn't in a region and there is a "current
798          region" in the caller; or the original tree had
799          EH edges.  In both cases split the block after the tree,
800          and add abnormal edge(s) as needed; we need both
801          those from the callee and the caller.
802          We check whether the copy can throw, because the const
803          propagation can change an INDIRECT_REF which throws
804          into a COMPONENT_REF which doesn't.  If the copy
805          can throw, the original could also throw.  */
806
807       if (TREE_CODE (copy_stmt) == RESX_EXPR
808           || (tree_could_throw_p (copy_stmt)
809               && lookup_stmt_eh_region (copy_stmt) > 0))
810         {
811           if (!bsi_end_p (bsi))
812             /* Note that bb's predecessor edges aren't necessarily
813                right at this point; split_block doesn't care.  */
814             {
815               edge e = split_block (new_bb, copy_stmt);
816               new_bb = e->dest;
817               bsi = bsi_start (new_bb);
818             }
819
820            make_eh_edges (copy_stmt);
821         }
822     }
823 }
824
825 /* Wrapper for remap_decl so it can be used as a callback.  */
826 static tree
827 remap_decl_1 (tree decl, void *data)
828 {
829   return remap_decl (decl, data);
830 }
831
832 /* Make a copy of the body of FN so that it can be inserted inline in
833    another function.  Walks FN via CFG, returns new fndecl.  */
834
835 static tree
836 copy_cfg_body (inline_data * id, gcov_type count, int frequency,
837                basic_block entry_block_map, basic_block exit_block_map)
838 {
839   tree callee_fndecl = id->callee;
840   /* Original cfun for the callee, doesn't change.  */
841   struct function *callee_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
842   /* Copy, built by this function.  */
843   struct function *new_cfun;
844   /* Place to copy from; when a copy of the function was saved off earlier,
845      use that instead of the main copy.  */
846   struct function *cfun_to_copy =
847     (struct function *) ggc_alloc_cleared (sizeof (struct function));
848   basic_block bb;
849   tree new_fndecl = NULL;
850   bool saving_or_cloning;
851   int count_scale, frequency_scale;
852
853   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count)
854     count_scale = (REG_BR_PROB_BASE * count
855                    / ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count);
856   else
857     count_scale = 1;
858
859   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency)
860     frequency_scale = (REG_BR_PROB_BASE * frequency
861                        /
862                        ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency);
863   else
864     frequency_scale = count_scale;
865
866   /* Register specific tree functions.  */
867   tree_register_cfg_hooks ();
868
869   /* Must have a CFG here at this point.  */
870   gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
871               (DECL_STRUCT_FUNCTION (callee_fndecl)));
872
873   *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
874
875   /* If there is a saved_cfg+saved_args lurking in the
876      struct function, a copy of the callee body was saved there, and
877      the 'struct cgraph edge' nodes have been fudged to point into the
878      saved body.  Accordingly, we want to copy that saved body so the
879      callgraph edges will be recognized and cloned properly.  */
880   if (cfun_to_copy->saved_cfg)
881     {
882       cfun_to_copy->cfg = cfun_to_copy->saved_cfg;
883       cfun_to_copy->eh = cfun_to_copy->saved_eh;
884     }
885   id->callee_cfun = cfun_to_copy;
886
887   /* If saving or cloning a function body, create new basic_block_info
888      and label_to_block_maps.  Otherwise, we're duplicaing a function
889      body for inlining; insert our new blocks and labels into the
890      existing varrays.  */
891   saving_or_cloning = (id->saving_p || id->cloning_p);
892   if (saving_or_cloning)
893     {
894       new_cfun =
895         (struct function *) ggc_alloc_cleared (sizeof (struct function));
896       *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
897       new_cfun->cfg = NULL;
898       new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
899       new_cfun->ib_boundaries_block = (varray_type) 0;
900       DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
901       push_cfun (new_cfun);
902       init_empty_tree_cfg ();
903
904       ENTRY_BLOCK_PTR->count =
905         (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count * count_scale /
906          REG_BR_PROB_BASE);
907       ENTRY_BLOCK_PTR->frequency =
908         (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency *
909          frequency_scale / REG_BR_PROB_BASE);
910       EXIT_BLOCK_PTR->count =
911         (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count * count_scale /
912          REG_BR_PROB_BASE);
913       EXIT_BLOCK_PTR->frequency =
914         (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency *
915          frequency_scale / REG_BR_PROB_BASE);
916
917       entry_block_map = ENTRY_BLOCK_PTR;
918       exit_block_map = EXIT_BLOCK_PTR;
919     }
920
921   ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
922   EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
923
924
925   /* Duplicate any exception-handling regions.  */
926   if (cfun->eh)
927     {
928       if (saving_or_cloning)
929         init_eh_for_function ();
930       id->eh_region_offset = duplicate_eh_regions (cfun_to_copy,
931                                                    remap_decl_1,
932                                                    id, id->eh_region);
933       gcc_assert (inlining_p (id) || !id->eh_region_offset);
934     }
935   /* Use aux pointers to map the original blocks to copy.  */
936   FOR_EACH_BB_FN (bb, cfun_to_copy)
937     bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
938   /* Now that we've duplicated the blocks, duplicate their edges.  */
939   FOR_ALL_BB_FN (bb, cfun_to_copy)
940     copy_edges_for_bb (bb, count_scale);
941   FOR_ALL_BB_FN (bb, cfun_to_copy)
942     bb->aux = NULL;
943
944   if (saving_or_cloning)
945     pop_cfun ();
946
947   return new_fndecl;
948 }
949
950 /* Make a copy of the body of FN so that it can be inserted inline in
951    another function.  */
952
953 static tree
954 copy_generic_body (inline_data *id)
955 {
956   tree body;
957   tree fndecl = id->callee;
958
959   body = DECL_SAVED_TREE (fndecl);
960   walk_tree (&body, copy_body_r, id, NULL);
961
962   return body;
963 }
964
965 static tree
966 copy_body (inline_data *id, gcov_type count, int frequency,
967            basic_block entry_block_map, basic_block exit_block_map)
968 {
969   tree fndecl = id->callee;
970   tree body;
971
972   /* If this body has a CFG, walk CFG and copy.  */
973   gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
974   body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
975
976   return body;
977 }
978
979 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
980    defined in function FN, or of a data member thereof.  */
981
982 static bool
983 self_inlining_addr_expr (tree value, tree fn)
984 {
985   tree var;
986
987   if (TREE_CODE (value) != ADDR_EXPR)
988     return false;
989
990   var = get_base_address (TREE_OPERAND (value, 0));
991
992   return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
993 }
994
995 static void
996 setup_one_parameter (inline_data *id, tree p, tree value, tree fn,
997                      basic_block bb, tree *vars)
998 {
999   tree init_stmt;
1000   tree var;
1001   tree var_sub;
1002
1003   /* If the parameter is never assigned to, we may not need to
1004      create a new variable here at all.  Instead, we may be able
1005      to just use the argument value.  */
1006   if (TREE_READONLY (p)
1007       && !TREE_ADDRESSABLE (p)
1008       && value && !TREE_SIDE_EFFECTS (value))
1009     {
1010       /* We may produce non-gimple trees by adding NOPs or introduce
1011          invalid sharing when operand is not really constant.
1012          It is not big deal to prohibit constant propagation here as
1013          we will constant propagate in DOM1 pass anyway.  */
1014       if (is_gimple_min_invariant (value)
1015           && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1016           /* We have to be very careful about ADDR_EXPR.  Make sure
1017              the base variable isn't a local variable of the inlined
1018              function, e.g., when doing recursive inlining, direct or
1019              mutually-recursive or whatever, which is why we don't
1020              just test whether fn == current_function_decl.  */
1021           && ! self_inlining_addr_expr (value, fn))
1022         {
1023           insert_decl_map (id, p, value);
1024           return;
1025         }
1026     }
1027
1028   /* Make an equivalent VAR_DECL.  Note that we must NOT remap the type
1029      here since the type of this decl must be visible to the calling
1030      function.  */
1031   var = copy_decl_for_inlining (p, fn, id->caller);
1032
1033   /* See if the frontend wants to pass this by invisible reference.  If
1034      so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1035      replace uses of the PARM_DECL with dereferences.  */
1036   if (TREE_TYPE (var) != TREE_TYPE (p)
1037       && POINTER_TYPE_P (TREE_TYPE (var))
1038       && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1039     {
1040       insert_decl_map (id, var, var);
1041       var_sub = build_fold_indirect_ref (var);
1042     }
1043   else
1044     var_sub = var;
1045
1046   /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1047      that way, when the PARM_DECL is encountered, it will be
1048      automatically replaced by the VAR_DECL.  */
1049   insert_decl_map (id, p, var_sub);
1050
1051   /* Declare this new variable.  */
1052   TREE_CHAIN (var) = *vars;
1053   *vars = var;
1054
1055   /* Make gimplifier happy about this variable.  */
1056   DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1057
1058   /* Even if P was TREE_READONLY, the new VAR should not be.
1059      In the original code, we would have constructed a
1060      temporary, and then the function body would have never
1061      changed the value of P.  However, now, we will be
1062      constructing VAR directly.  The constructor body may
1063      change its value multiple times as it is being
1064      constructed.  Therefore, it must not be TREE_READONLY;
1065      the back-end assumes that TREE_READONLY variable is
1066      assigned to only once.  */
1067   if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1068     TREE_READONLY (var) = 0;
1069
1070   /* Initialize this VAR_DECL from the equivalent argument.  Convert
1071      the argument to the proper type in case it was promoted.  */
1072   if (value)
1073     {
1074       tree rhs = fold_convert (TREE_TYPE (var), value);
1075       block_stmt_iterator bsi = bsi_last (bb);
1076
1077       if (rhs == error_mark_node)
1078         return;
1079
1080       /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1081          keep our trees in gimple form.  */
1082       init_stmt = build (MODIFY_EXPR, TREE_TYPE (var), var, rhs);
1083
1084       /* If we did not create a gimple value and we did not create a gimple
1085          cast of a gimple value, then we will need to gimplify INIT_STMTS
1086          at the end.  Note that is_gimple_cast only checks the outer
1087          tree code, not its operand.  Thus the explicit check that it's
1088          operand is a gimple value.  */
1089       if (!is_gimple_val (rhs)
1090           && (!is_gimple_cast (rhs)
1091               || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1092         gimplify_stmt (&init_stmt);
1093       bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1094     }
1095 }
1096
1097 /* Generate code to initialize the parameters of the function at the
1098    top of the stack in ID from the ARGS (presented as a TREE_LIST).  */
1099
1100 static void
1101 initialize_inlined_parameters (inline_data *id, tree args, tree static_chain,
1102                                tree fn, basic_block bb)
1103 {
1104   tree parms;
1105   tree a;
1106   tree p;
1107   tree vars = NULL_TREE;
1108   int argnum = 0;
1109
1110   /* Figure out what the parameters are.  */
1111   parms = DECL_ARGUMENTS (fn);
1112   if (fn == current_function_decl)
1113     parms = cfun->saved_args;
1114
1115   /* Loop through the parameter declarations, replacing each with an
1116      equivalent VAR_DECL, appropriately initialized.  */
1117   for (p = parms, a = args; p;
1118        a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1119     {
1120       tree value;
1121
1122       ++argnum;
1123
1124       /* Find the initializer.  */
1125       value = lang_hooks.tree_inlining.convert_parm_for_inlining
1126               (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1127
1128       setup_one_parameter (id, p, value, fn, bb, &vars);
1129     }
1130
1131   /* Initialize the static chain.  */
1132   p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1133   if (fn == current_function_decl)
1134     p = DECL_STRUCT_FUNCTION (fn)->saved_static_chain_decl;
1135   if (p)
1136     {
1137       /* No static chain?  Seems like a bug in tree-nested.c.  */
1138       gcc_assert (static_chain);
1139
1140       setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1141     }
1142
1143   declare_inline_vars (id->block, vars);
1144 }
1145
1146 /* Declare a return variable to replace the RESULT_DECL for the
1147    function we are calling.  An appropriate DECL_STMT is returned.
1148    The USE_STMT is filled to contain a use of the declaration to
1149    indicate the return value of the function.
1150
1151    RETURN_SLOT_ADDR, if non-null, was a fake parameter that
1152    took the address of the result.  MODIFY_DEST, if non-null, was the LHS of
1153    the MODIFY_EXPR to which this call is the RHS.
1154
1155    The return value is a (possibly null) value that is the result of the
1156    function as seen by the callee.  *USE_P is a (possibly null) value that
1157    holds the result as seen by the caller.  */
1158
1159 static tree
1160 declare_return_variable (inline_data *id, tree return_slot_addr,
1161                          tree modify_dest, tree *use_p)
1162 {
1163   tree callee = id->callee;
1164   tree caller = id->caller;
1165   tree result = DECL_RESULT (callee);
1166   tree callee_type = TREE_TYPE (result);
1167   tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1168   tree var, use;
1169
1170   /* We don't need to do anything for functions that don't return
1171      anything.  */
1172   if (!result || VOID_TYPE_P (callee_type))
1173     {
1174       *use_p = NULL_TREE;
1175       return NULL_TREE;
1176     }
1177
1178   /* If there was a return slot, then the return value is the
1179      dereferenced address of that object.  */
1180   if (return_slot_addr)
1181     {
1182       /* The front end shouldn't have used both return_slot_addr and
1183          a modify expression.  */
1184       gcc_assert (!modify_dest);
1185       if (DECL_BY_REFERENCE (result))
1186         var = return_slot_addr;
1187       else
1188         var = build_fold_indirect_ref (return_slot_addr);
1189       use = NULL;
1190       goto done;
1191     }
1192
1193   /* All types requiring non-trivial constructors should have been handled.  */
1194   gcc_assert (!TREE_ADDRESSABLE (callee_type));
1195
1196   /* Attempt to avoid creating a new temporary variable.  */
1197   if (modify_dest)
1198     {
1199       bool use_it = false;
1200
1201       /* We can't use MODIFY_DEST if there's type promotion involved.  */
1202       if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1203         use_it = false;
1204
1205       /* ??? If we're assigning to a variable sized type, then we must
1206          reuse the destination variable, because we've no good way to
1207          create variable sized temporaries at this point.  */
1208       else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1209         use_it = true;
1210
1211       /* If the callee cannot possibly modify MODIFY_DEST, then we can
1212          reuse it as the result of the call directly.  Don't do this if
1213          it would promote MODIFY_DEST to addressable.  */
1214       else if (!TREE_STATIC (modify_dest)
1215                && !TREE_ADDRESSABLE (modify_dest)
1216                && !TREE_ADDRESSABLE (result))
1217         use_it = true;
1218
1219       if (use_it)
1220         {
1221           var = modify_dest;
1222           use = NULL;
1223           goto done;
1224         }
1225     }
1226
1227   gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1228
1229   var = copy_decl_for_inlining (result, callee, caller);
1230
1231   DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1232   DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1233     = tree_cons (NULL_TREE, var,
1234                  DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1235
1236   /* Do not have the rest of GCC warn about this variable as it should
1237      not be visible to the user.  */
1238   TREE_NO_WARNING (var) = 1;
1239
1240   /* Build the use expr.  If the return type of the function was
1241      promoted, convert it back to the expected type.  */
1242   use = var;
1243   if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1244     use = fold_convert (caller_type, var);
1245
1246  done:
1247   /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1248      way, when the RESULT_DECL is encountered, it will be
1249      automatically replaced by the VAR_DECL.  */
1250   insert_decl_map (id, result, var);
1251
1252   /* Remember this so we can ignore it in remap_decls.  */
1253   id->retvar = var;
1254
1255   *use_p = use;
1256   return var;
1257 }
1258
1259 /* Returns nonzero if a function can be inlined as a tree.  */
1260
1261 bool
1262 tree_inlinable_function_p (tree fn)
1263 {
1264   return inlinable_function_p (fn);
1265 }
1266
1267 static const char *inline_forbidden_reason;
1268
1269 static tree
1270 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1271                       void *fnp)
1272 {
1273   tree node = *nodep;
1274   tree fn = (tree) fnp;
1275   tree t;
1276
1277   switch (TREE_CODE (node))
1278     {
1279     case CALL_EXPR:
1280       /* Refuse to inline alloca call unless user explicitly forced so as
1281          this may change program's memory overhead drastically when the
1282          function using alloca is called in loop.  In GCC present in
1283          SPEC2000 inlining into schedule_block cause it to require 2GB of
1284          RAM instead of 256MB.  */
1285       if (alloca_call_p (node)
1286           && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1287         {
1288           inline_forbidden_reason
1289             = N_("%Jfunction %qF can never be inlined because it uses "
1290                  "alloca (override using the always_inline attribute)");
1291           return node;
1292         }
1293       t = get_callee_fndecl (node);
1294       if (! t)
1295         break;
1296
1297       /* We cannot inline functions that call setjmp.  */
1298       if (setjmp_call_p (t))
1299         {
1300           inline_forbidden_reason
1301             = N_("%Jfunction %qF can never be inlined because it uses setjmp");
1302           return node;
1303         }
1304
1305       if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1306         switch (DECL_FUNCTION_CODE (t))
1307           {
1308             /* We cannot inline functions that take a variable number of
1309                arguments.  */
1310           case BUILT_IN_VA_START:
1311           case BUILT_IN_STDARG_START:
1312           case BUILT_IN_NEXT_ARG:
1313           case BUILT_IN_VA_END:
1314             inline_forbidden_reason
1315               = N_("%Jfunction %qF can never be inlined because it "
1316                    "uses variable argument lists");
1317             return node;
1318
1319           case BUILT_IN_LONGJMP:
1320             /* We can't inline functions that call __builtin_longjmp at
1321                all.  The non-local goto machinery really requires the
1322                destination be in a different function.  If we allow the
1323                function calling __builtin_longjmp to be inlined into the
1324                function calling __builtin_setjmp, Things will Go Awry.  */
1325             inline_forbidden_reason
1326               = N_("%Jfunction %qF can never be inlined because "
1327                    "it uses setjmp-longjmp exception handling");
1328             return node;
1329
1330           case BUILT_IN_NONLOCAL_GOTO:
1331             /* Similarly.  */
1332             inline_forbidden_reason
1333               = N_("%Jfunction %qF can never be inlined because "
1334                    "it uses non-local goto");
1335             return node;
1336
1337           case BUILT_IN_RETURN:
1338           case BUILT_IN_APPLY_ARGS:
1339             /* If a __builtin_apply_args caller would be inlined,
1340                it would be saving arguments of the function it has
1341                been inlined into.  Similarly __builtin_return would
1342                return from the function the inline has been inlined into.  */
1343             inline_forbidden_reason
1344               = N_("%Jfunction %qF can never be inlined because "
1345                    "it uses __builtin_return or __builtin_apply_args");
1346             return node;
1347
1348           default:
1349             break;
1350           }
1351       break;
1352
1353     case GOTO_EXPR:
1354       t = TREE_OPERAND (node, 0);
1355
1356       /* We will not inline a function which uses computed goto.  The
1357          addresses of its local labels, which may be tucked into
1358          global storage, are of course not constant across
1359          instantiations, which causes unexpected behavior.  */
1360       if (TREE_CODE (t) != LABEL_DECL)
1361         {
1362           inline_forbidden_reason
1363             = N_("%Jfunction %qF can never be inlined "
1364                  "because it contains a computed goto");
1365           return node;
1366         }
1367       break;
1368
1369     case LABEL_EXPR:
1370       t = TREE_OPERAND (node, 0);
1371       if (DECL_NONLOCAL (t))
1372         {
1373           /* We cannot inline a function that receives a non-local goto
1374              because we cannot remap the destination label used in the
1375              function that is performing the non-local goto.  */
1376           inline_forbidden_reason
1377             = N_("%Jfunction %qF can never be inlined "
1378                  "because it receives a non-local goto");
1379           return node;
1380         }
1381       break;
1382
1383     case RECORD_TYPE:
1384     case UNION_TYPE:
1385       /* We cannot inline a function of the form
1386
1387            void F (int i) { struct S { int ar[i]; } s; }
1388
1389          Attempting to do so produces a catch-22.
1390          If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1391          UNION_TYPE nodes, then it goes into infinite recursion on a
1392          structure containing a pointer to its own type.  If it doesn't,
1393          then the type node for S doesn't get adjusted properly when
1394          F is inlined. 
1395
1396          ??? This is likely no longer true, but it's too late in the 4.0
1397          cycle to try to find out.  This should be checked for 4.1.  */
1398       for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1399         if (variably_modified_type_p (TREE_TYPE (t), NULL))
1400           {
1401             inline_forbidden_reason
1402               = N_("%Jfunction %qF can never be inlined "
1403                    "because it uses variable sized variables");
1404             return node;
1405           }
1406
1407     default:
1408       break;
1409     }
1410
1411   return NULL_TREE;
1412 }
1413
1414 /* Return subexpression representing possible alloca call, if any.  */
1415 static tree
1416 inline_forbidden_p (tree fndecl)
1417 {
1418   location_t saved_loc = input_location;
1419   block_stmt_iterator bsi;
1420   basic_block bb;
1421   tree ret = NULL_TREE;
1422
1423   FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1424     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1425       {
1426         ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1427                                     inline_forbidden_p_1, fndecl);
1428         if (ret)
1429           goto egress;
1430       }
1431
1432 egress:
1433   input_location = saved_loc;
1434   return ret;
1435 }
1436
1437 /* Returns nonzero if FN is a function that does not have any
1438    fundamental inline blocking properties.  */
1439
1440 static bool
1441 inlinable_function_p (tree fn)
1442 {
1443   bool inlinable = true;
1444
1445   /* If we've already decided this function shouldn't be inlined,
1446      there's no need to check again.  */
1447   if (DECL_UNINLINABLE (fn))
1448     return false;
1449
1450   /* See if there is any language-specific reason it cannot be
1451      inlined.  (It is important that this hook be called early because
1452      in C++ it may result in template instantiation.)
1453      If the function is not inlinable for language-specific reasons,
1454      it is left up to the langhook to explain why.  */
1455   inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1456
1457   /* If we don't have the function body available, we can't inline it.
1458      However, this should not be recorded since we also get here for
1459      forward declared inline functions.  Therefore, return at once.  */
1460   if (!DECL_SAVED_TREE (fn))
1461     return false;
1462
1463   /* If we're not inlining at all, then we cannot inline this function.  */
1464   else if (!flag_inline_trees)
1465     inlinable = false;
1466
1467   /* Only try to inline functions if DECL_INLINE is set.  This should be
1468      true for all functions declared `inline', and for all other functions
1469      as well with -finline-functions.
1470
1471      Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1472      it's the front-end that must set DECL_INLINE in this case, because
1473      dwarf2out loses if a function that does not have DECL_INLINE set is
1474      inlined anyway.  That is why we have both DECL_INLINE and
1475      DECL_DECLARED_INLINE_P.  */
1476   /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1477             here should be redundant.  */
1478   else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1479     inlinable = false;
1480
1481   else if (inline_forbidden_p (fn))
1482     {
1483       /* See if we should warn about uninlinable functions.  Previously,
1484          some of these warnings would be issued while trying to expand
1485          the function inline, but that would cause multiple warnings
1486          about functions that would for example call alloca.  But since
1487          this a property of the function, just one warning is enough.
1488          As a bonus we can now give more details about the reason why a
1489          function is not inlinable.
1490          We only warn for functions declared `inline' by the user.  */
1491       bool do_warning = (warn_inline
1492                          && DECL_INLINE (fn)
1493                          && DECL_DECLARED_INLINE_P (fn)
1494                          && !DECL_IN_SYSTEM_HEADER (fn));
1495
1496       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1497         sorry (inline_forbidden_reason, fn, fn);
1498       else if (do_warning)
1499         warning (0, inline_forbidden_reason, fn, fn);
1500
1501       inlinable = false;
1502     }
1503
1504   /* Squirrel away the result so that we don't have to check again.  */
1505   DECL_UNINLINABLE (fn) = !inlinable;
1506
1507   return inlinable;
1508 }
1509
1510 /* Estimate the cost of a memory move.  Use machine dependent
1511    word size and take possible memcpy call into account.  */
1512
1513 int
1514 estimate_move_cost (tree type)
1515 {
1516   HOST_WIDE_INT size;
1517
1518   size = int_size_in_bytes (type);
1519
1520   if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1521     /* Cost of a memcpy call, 3 arguments and the call.  */
1522     return 4;
1523   else
1524     return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1525 }
1526
1527 /* Used by estimate_num_insns.  Estimate number of instructions seen
1528    by given statement.  */
1529
1530 static tree
1531 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1532 {
1533   int *count = data;
1534   tree x = *tp;
1535
1536   if (IS_TYPE_OR_DECL_P (x))
1537     {
1538       *walk_subtrees = 0;
1539       return NULL;
1540     }
1541   /* Assume that constants and references counts nothing.  These should
1542      be majorized by amount of operations among them we count later
1543      and are common target of CSE and similar optimizations.  */
1544   else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1545     return NULL;
1546
1547   switch (TREE_CODE (x))
1548     {
1549     /* Containers have no cost.  */
1550     case TREE_LIST:
1551     case TREE_VEC:
1552     case BLOCK:
1553     case COMPONENT_REF:
1554     case BIT_FIELD_REF:
1555     case INDIRECT_REF:
1556     case ALIGN_INDIRECT_REF:
1557     case MISALIGNED_INDIRECT_REF:
1558     case ARRAY_REF:
1559     case ARRAY_RANGE_REF:
1560     case OBJ_TYPE_REF:
1561     case EXC_PTR_EXPR: /* ??? */
1562     case FILTER_EXPR: /* ??? */
1563     case COMPOUND_EXPR:
1564     case BIND_EXPR:
1565     case WITH_CLEANUP_EXPR:
1566     case NOP_EXPR:
1567     case VIEW_CONVERT_EXPR:
1568     case SAVE_EXPR:
1569     case ADDR_EXPR:
1570     case COMPLEX_EXPR:
1571     case RANGE_EXPR:
1572     case CASE_LABEL_EXPR:
1573     case SSA_NAME:
1574     case CATCH_EXPR:
1575     case EH_FILTER_EXPR:
1576     case STATEMENT_LIST:
1577     case ERROR_MARK:
1578     case NON_LVALUE_EXPR:
1579     case FDESC_EXPR:
1580     case VA_ARG_EXPR:
1581     case TRY_CATCH_EXPR:
1582     case TRY_FINALLY_EXPR:
1583     case LABEL_EXPR:
1584     case GOTO_EXPR:
1585     case RETURN_EXPR:
1586     case EXIT_EXPR:
1587     case LOOP_EXPR:
1588     case PHI_NODE:
1589     case WITH_SIZE_EXPR:
1590       break;
1591
1592     /* We don't account constants for now.  Assume that the cost is amortized
1593        by operations that do use them.  We may re-consider this decision once
1594        we are able to optimize the tree before estimating it's size and break
1595        out static initializers.  */
1596     case IDENTIFIER_NODE:
1597     case INTEGER_CST:
1598     case REAL_CST:
1599     case COMPLEX_CST:
1600     case VECTOR_CST:
1601     case STRING_CST:
1602       *walk_subtrees = 0;
1603       return NULL;
1604
1605     /* Try to estimate the cost of assignments.  We have three cases to
1606        deal with:
1607         1) Simple assignments to registers;
1608         2) Stores to things that must live in memory.  This includes
1609            "normal" stores to scalars, but also assignments of large
1610            structures, or constructors of big arrays;
1611         3) TARGET_EXPRs.
1612
1613        Let us look at the first two cases, assuming we have "a = b + C":
1614        <modify_expr <var_decl "a"> <plus_expr <var_decl "b"> <constant C>>
1615        If "a" is a GIMPLE register, the assignment to it is free on almost
1616        any target, because "a" usually ends up in a real register.  Hence
1617        the only cost of this expression comes from the PLUS_EXPR, and we
1618        can ignore the MODIFY_EXPR.
1619        If "a" is not a GIMPLE register, the assignment to "a" will most
1620        likely be a real store, so the cost of the MODIFY_EXPR is the cost
1621        of moving something into "a", which we compute using the function
1622        estimate_move_cost.
1623
1624        The third case deals with TARGET_EXPRs, for which the semantics are
1625        that a temporary is assigned, unless the TARGET_EXPR itself is being
1626        assigned to something else.  In the latter case we do not need the
1627        temporary.  E.g. in <modify_expr <var_decl "a"> <target_expr>>, the
1628        MODIFY_EXPR is free.  */
1629     case INIT_EXPR:
1630     case MODIFY_EXPR:
1631       /* Is the right and side a TARGET_EXPR?  */
1632       if (TREE_CODE (TREE_OPERAND (x, 1)) == TARGET_EXPR)
1633         break;
1634       /* ... fall through ...  */
1635
1636     case TARGET_EXPR:
1637       x = TREE_OPERAND (x, 0);
1638       /* Is this an assignments to a register?  */
1639       if (is_gimple_reg (x))
1640         break;
1641       /* Otherwise it's a store, so fall through to compute the move cost.  */
1642
1643     case CONSTRUCTOR:
1644       *count += estimate_move_cost (TREE_TYPE (x));
1645       break;
1646
1647     /* Assign cost of 1 to usual operations.
1648        ??? We may consider mapping RTL costs to this.  */
1649     case COND_EXPR:
1650     case VEC_COND_EXPR:
1651
1652     case PLUS_EXPR:
1653     case MINUS_EXPR:
1654     case MULT_EXPR:
1655
1656     case FIX_TRUNC_EXPR:
1657     case FIX_CEIL_EXPR:
1658     case FIX_FLOOR_EXPR:
1659     case FIX_ROUND_EXPR:
1660
1661     case NEGATE_EXPR:
1662     case FLOAT_EXPR:
1663     case MIN_EXPR:
1664     case MAX_EXPR:
1665     case ABS_EXPR:
1666
1667     case LSHIFT_EXPR:
1668     case RSHIFT_EXPR:
1669     case LROTATE_EXPR:
1670     case RROTATE_EXPR:
1671
1672     case BIT_IOR_EXPR:
1673     case BIT_XOR_EXPR:
1674     case BIT_AND_EXPR:
1675     case BIT_NOT_EXPR:
1676
1677     case TRUTH_ANDIF_EXPR:
1678     case TRUTH_ORIF_EXPR:
1679     case TRUTH_AND_EXPR:
1680     case TRUTH_OR_EXPR:
1681     case TRUTH_XOR_EXPR:
1682     case TRUTH_NOT_EXPR:
1683
1684     case LT_EXPR:
1685     case LE_EXPR:
1686     case GT_EXPR:
1687     case GE_EXPR:
1688     case EQ_EXPR:
1689     case NE_EXPR:
1690     case ORDERED_EXPR:
1691     case UNORDERED_EXPR:
1692
1693     case UNLT_EXPR:
1694     case UNLE_EXPR:
1695     case UNGT_EXPR:
1696     case UNGE_EXPR:
1697     case UNEQ_EXPR:
1698     case LTGT_EXPR:
1699
1700     case CONVERT_EXPR:
1701
1702     case CONJ_EXPR:
1703
1704     case PREDECREMENT_EXPR:
1705     case PREINCREMENT_EXPR:
1706     case POSTDECREMENT_EXPR:
1707     case POSTINCREMENT_EXPR:
1708
1709     case SWITCH_EXPR:
1710
1711     case ASM_EXPR:
1712
1713     case REALIGN_LOAD_EXPR:
1714
1715     case RESX_EXPR:
1716       *count += 1;
1717       break;
1718
1719     /* Few special cases of expensive operations.  This is useful
1720        to avoid inlining on functions having too many of these.  */
1721     case TRUNC_DIV_EXPR:
1722     case CEIL_DIV_EXPR:
1723     case FLOOR_DIV_EXPR:
1724     case ROUND_DIV_EXPR:
1725     case EXACT_DIV_EXPR:
1726     case TRUNC_MOD_EXPR:
1727     case CEIL_MOD_EXPR:
1728     case FLOOR_MOD_EXPR:
1729     case ROUND_MOD_EXPR:
1730     case RDIV_EXPR:
1731       *count += 10;
1732       break;
1733     case CALL_EXPR:
1734       {
1735         tree decl = get_callee_fndecl (x);
1736         tree arg;
1737
1738         if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
1739           switch (DECL_FUNCTION_CODE (decl))
1740             {
1741             case BUILT_IN_CONSTANT_P:
1742               *walk_subtrees = 0;
1743               return NULL_TREE;
1744             case BUILT_IN_EXPECT:
1745               return NULL_TREE;
1746             default:
1747               break;
1748             }
1749
1750         /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1751            that does use function declaration to figure out the arguments.  */
1752         if (!decl)
1753           {
1754             for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
1755               *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
1756           }
1757         else
1758           {
1759             for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
1760               *count += estimate_move_cost (TREE_TYPE (arg));
1761           }
1762
1763         *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
1764         break;
1765       }
1766     default:
1767       gcc_unreachable ();
1768     }
1769   return NULL;
1770 }
1771
1772 /* Estimate number of instructions that will be created by expanding EXPR.  */
1773
1774 int
1775 estimate_num_insns (tree expr)
1776 {
1777   int num = 0;
1778   struct pointer_set_t *visited_nodes;
1779   basic_block bb;
1780   block_stmt_iterator bsi;
1781   struct function *my_function;
1782
1783   /* If we're given an entire function, walk the CFG.  */
1784   if (TREE_CODE (expr) == FUNCTION_DECL)
1785     {
1786       my_function = DECL_STRUCT_FUNCTION (expr);
1787       gcc_assert (my_function && my_function->cfg);
1788       visited_nodes = pointer_set_create ();
1789       FOR_EACH_BB_FN (bb, my_function)
1790         {
1791           for (bsi = bsi_start (bb);
1792                !bsi_end_p (bsi);
1793                bsi_next (&bsi))
1794             {
1795               walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
1796                          &num, visited_nodes);
1797             }
1798         }
1799       pointer_set_destroy (visited_nodes);
1800     }
1801   else
1802     walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
1803
1804   return num;
1805 }
1806
1807 /* Initialized with NOGC, making this poisonous to the garbage collector.  */
1808 static varray_type cfun_stack;
1809
1810 void
1811 push_cfun (struct function *new_cfun)
1812 {
1813   static bool initialized = false;
1814
1815   if (!initialized)
1816     {
1817       VARRAY_GENERIC_PTR_NOGC_INIT (cfun_stack, 20, "cfun_stack");
1818       initialized = true;
1819     }
1820   VARRAY_PUSH_GENERIC_PTR (cfun_stack, cfun);
1821   cfun = new_cfun;
1822 }
1823
1824 void
1825 pop_cfun (void)
1826 {
1827   cfun = (struct function *)VARRAY_TOP_GENERIC_PTR (cfun_stack);
1828   VARRAY_POP (cfun_stack);
1829 }
1830
1831 /* Install new lexical TREE_BLOCK underneath 'current_block'.  */
1832 static void
1833 add_lexical_block (tree current_block, tree new_block)
1834 {
1835   tree *blk_p;
1836
1837   /* Walk to the last sub-block.  */
1838   for (blk_p = &BLOCK_SUBBLOCKS (current_block);
1839        *blk_p;
1840        blk_p = &TREE_CHAIN (*blk_p))
1841     ;
1842   *blk_p = new_block;
1843   BLOCK_SUPERCONTEXT (new_block) = current_block;
1844   BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
1845 }
1846
1847 /* If *TP is a CALL_EXPR, replace it with its inline expansion.  */
1848
1849 static bool
1850 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
1851 {
1852   inline_data *id;
1853   tree t;
1854   tree use_retvar;
1855   tree fn;
1856   splay_tree st;
1857   tree args;
1858   tree return_slot_addr;
1859   tree modify_dest;
1860   location_t saved_location;
1861   struct cgraph_edge *cg_edge;
1862   const char *reason;
1863   basic_block return_block;
1864   edge e;
1865   block_stmt_iterator bsi, stmt_bsi;
1866   bool successfully_inlined = FALSE;
1867   tree t_step;
1868   tree var;
1869   struct cgraph_node *old_node;
1870   tree decl;
1871
1872   /* See what we've got.  */
1873   id = (inline_data *) data;
1874   t = *tp;
1875
1876   /* Set input_location here so we get the right instantiation context
1877      if we call instantiate_decl from inlinable_function_p.  */
1878   saved_location = input_location;
1879   if (EXPR_HAS_LOCATION (t))
1880     input_location = EXPR_LOCATION (t);
1881
1882   /* From here on, we're only interested in CALL_EXPRs.  */
1883   if (TREE_CODE (t) != CALL_EXPR)
1884     goto egress;
1885
1886   /* First, see if we can figure out what function is being called.
1887      If we cannot, then there is no hope of inlining the function.  */
1888   fn = get_callee_fndecl (t);
1889   if (!fn)
1890     goto egress;
1891
1892   /* Turn forward declarations into real ones.  */
1893   fn = cgraph_node (fn)->decl;
1894
1895   /* If fn is a declaration of a function in a nested scope that was
1896      globally declared inline, we don't set its DECL_INITIAL.
1897      However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1898      C++ front-end uses it for cdtors to refer to their internal
1899      declarations, that are not real functions.  Fortunately those
1900      don't have trees to be saved, so we can tell by checking their
1901      DECL_SAVED_TREE.  */
1902   if (! DECL_INITIAL (fn)
1903       && DECL_ABSTRACT_ORIGIN (fn)
1904       && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1905     fn = DECL_ABSTRACT_ORIGIN (fn);
1906
1907   /* Objective C and fortran still calls tree_rest_of_compilation directly.
1908      Kill this check once this is fixed.  */
1909   if (!id->current_node->analyzed)
1910     goto egress;
1911
1912   cg_edge = cgraph_edge (id->current_node, t);
1913
1914   /* Constant propagation on argument done during previous inlining
1915      may create new direct call.  Produce an edge for it.  */
1916   if (!cg_edge)
1917     {
1918       struct cgraph_node *dest = cgraph_node (fn);
1919
1920       /* We have missing edge in the callgraph.  This can happen in one case
1921          where previous inlining turned indirect call into direct call by
1922          constant propagating arguments.  In all other cases we hit a bug
1923          (incorrect node sharing is most common reason for missing edges.  */
1924       gcc_assert (dest->needed || !flag_unit_at_a_time);
1925       cgraph_create_edge (id->node, dest, t)->inline_failed
1926         = N_("originally indirect function call not considered for inlining");
1927       goto egress;
1928     }
1929
1930   /* Don't try to inline functions that are not well-suited to
1931      inlining.  */
1932   if (!cgraph_inline_p (cg_edge, &reason))
1933     {
1934       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1935         {
1936           sorry ("%Jinlining failed in call to %qF: %s", fn, fn, reason);
1937           sorry ("called from here");
1938         }
1939       else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
1940                && !DECL_IN_SYSTEM_HEADER (fn)
1941                && strlen (reason)
1942                && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn)))
1943         {
1944           warning (0, "%Jinlining failed in call to %qF: %s", fn, fn, reason);
1945           warning (0, "called from here");
1946         }
1947       goto egress;
1948     }
1949
1950 #ifdef ENABLE_CHECKING
1951   if (cg_edge->callee->decl != id->node->decl)
1952     verify_cgraph_node (cg_edge->callee);
1953 #endif
1954
1955   /* We will be inlining this callee.  */
1956
1957   id->eh_region = lookup_stmt_eh_region (stmt);
1958
1959   /* Split the block holding the CALL_EXPR.  */
1960
1961   e = split_block (bb, stmt);
1962   bb = e->src;
1963   return_block = e->dest;
1964   remove_edge (e);
1965
1966   /* split_block splits before the statement, work around this by moving
1967      the call into the first half_bb.  Not pretty, but seems easier than
1968      doing the CFG manipulation by hand when the CALL_EXPR is in the last
1969      statement in BB.  */
1970   stmt_bsi = bsi_last (bb);
1971   bsi = bsi_start (return_block);
1972   if (!bsi_end_p (bsi))
1973     bsi_move_before (&stmt_bsi, &bsi);
1974   else
1975     {
1976       tree stmt = bsi_stmt (stmt_bsi);
1977       bsi_remove (&stmt_bsi);
1978       bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
1979     }
1980   stmt_bsi = bsi_start (return_block);
1981
1982   /* Build a block containing code to initialize the arguments, the
1983      actual inline expansion of the body, and a label for the return
1984      statements within the function to jump to.  The type of the
1985      statement expression is the return type of the function call.  */
1986   id->block = make_node (BLOCK);
1987   BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
1988   add_lexical_block (TREE_BLOCK (stmt), id->block);
1989
1990
1991   /* Local declarations will be replaced by their equivalents in this
1992      map.  */
1993   st = id->decl_map;
1994   id->decl_map = splay_tree_new (splay_tree_compare_pointers,
1995                                  NULL, NULL);
1996
1997   /* Initialize the parameters.  */
1998   args = TREE_OPERAND (t, 1);
1999   if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (t))
2000     {
2001       return_slot_addr = TREE_VALUE (args);
2002       args = TREE_CHAIN (args);
2003     }
2004   else
2005     return_slot_addr = NULL_TREE;
2006
2007   initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2008
2009   /* Record the function we are about to inline.  */
2010   id->callee = fn;
2011
2012   /* Return statements in the function body will be replaced by jumps
2013      to the RET_LABEL.  */
2014
2015   gcc_assert (DECL_INITIAL (fn));
2016   gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2017
2018   /* Find the lhs to which the result of this call is assigned.  */
2019   modify_dest = stmt;
2020   if (TREE_CODE (modify_dest) == MODIFY_EXPR)
2021     {
2022       modify_dest = TREE_OPERAND (modify_dest, 0);
2023
2024       /* The function which we are inlining might not return a value,
2025          in which case we should issue a warning that the function
2026          does not return a value.  In that case the optimizers will
2027          see that the variable to which the value is assigned was not
2028          initialized.  We do not want to issue a warning about that
2029          uninitialized variable.  */
2030       if (DECL_P (modify_dest))
2031         TREE_NO_WARNING (modify_dest) = 1;
2032     }
2033   else
2034     modify_dest = NULL;
2035
2036   /* Declare the return variable for the function.  */
2037   decl = declare_return_variable (id, return_slot_addr,
2038                                   modify_dest, &use_retvar);
2039   /* Do this only if declare_return_variable created a new one.  */
2040   if (decl && !return_slot_addr && decl != modify_dest)
2041     declare_inline_vars (id->block, decl);
2042
2043   /* After we've initialized the parameters, we insert the body of the
2044      function itself.  */
2045   old_node = id->current_node;
2046
2047   /* Anoint the callee-to-be-duplicated as the "current_node."  When
2048      CALL_EXPRs within callee are duplicated, the edges from callee to
2049      callee's callees (caller's grandchildren) will be cloned.  */
2050   id->current_node = cg_edge->callee;
2051
2052   /* This is it.  Duplicate the callee body.  Assume callee is
2053      pre-gimplified.  Note that we must not alter the caller
2054      function in any way before this point, as this CALL_EXPR may be
2055      a self-referential call; if we're calling ourselves, we need to
2056      duplicate our body before altering anything.  */
2057   copy_body (id, bb->count, bb->frequency, bb, return_block);
2058   id->current_node = old_node;
2059
2060   /* Clean up.  */
2061   splay_tree_delete (id->decl_map);
2062   id->decl_map = st;
2063
2064   /* If the inlined function returns a result that we care about,
2065      clobber the CALL_EXPR with a reference to the return variable.  */
2066   if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2067     {
2068       *tp = use_retvar;
2069       maybe_clean_or_replace_eh_stmt (stmt, stmt);
2070     }
2071   else
2072     /* We're modifying a TSI owned by gimple_expand_calls_inline();
2073        tsi_delink() will leave the iterator in a sane state.  */
2074     bsi_remove (&stmt_bsi);
2075
2076   bsi_next (&bsi);
2077   if (bsi_end_p (bsi))
2078     tree_purge_dead_eh_edges (return_block);
2079
2080   /* If the value of the new expression is ignored, that's OK.  We
2081      don't warn about this for CALL_EXPRs, so we shouldn't warn about
2082      the equivalent inlined version either.  */
2083   TREE_USED (*tp) = 1;
2084
2085   /* Output the inlining info for this abstract function, since it has been
2086      inlined.  If we don't do this now, we can lose the information about the
2087      variables in the function when the blocks get blown away as soon as we
2088      remove the cgraph node.  */
2089   (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2090
2091   /* Update callgraph if needed.  */
2092   cgraph_remove_node (cg_edge->callee);
2093
2094   /* Declare the 'auto' variables added with this inlined body.  */
2095   record_vars (BLOCK_VARS (id->block));
2096   id->block = NULL_TREE;
2097
2098   /* Add local static vars in this inlined callee to caller.  */
2099   for (t_step = id->callee_cfun->unexpanded_var_list;
2100        t_step;
2101        t_step = TREE_CHAIN (t_step))
2102     {
2103       var = TREE_VALUE (t_step);
2104       if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2105         record_vars (var);
2106     }
2107   successfully_inlined = TRUE;
2108
2109  egress:
2110   input_location = saved_location;
2111   return successfully_inlined;
2112 }
2113
2114 /* Expand call statements reachable from STMT_P.
2115    We can only have CALL_EXPRs as the "toplevel" tree code or nested
2116    in a MODIFY_EXPR.  See tree-gimple.c:get_call_expr_in().  We can
2117    unfortunately not use that function here because we need a pointer
2118    to the CALL_EXPR, not the tree itself.  */
2119
2120 static bool
2121 gimple_expand_calls_inline (basic_block bb, inline_data *id)
2122 {
2123   block_stmt_iterator bsi;
2124
2125   /* Register specific tree functions.  */
2126   tree_register_cfg_hooks ();
2127   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2128     {
2129       tree *expr_p = bsi_stmt_ptr (bsi);
2130       tree stmt = *expr_p;
2131
2132       if (TREE_CODE (*expr_p) == MODIFY_EXPR)
2133         expr_p = &TREE_OPERAND (*expr_p, 1);
2134       if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2135         expr_p = &TREE_OPERAND (*expr_p, 0);
2136       if (TREE_CODE (*expr_p) == CALL_EXPR)
2137         if (expand_call_inline (bb, stmt, expr_p, id))
2138           return true;
2139     }
2140   return false;
2141 }
2142
2143 /* Expand calls to inline functions in the body of FN.  */
2144
2145 void
2146 optimize_inline_calls (tree fn)
2147 {
2148   inline_data id;
2149   tree prev_fn;
2150   basic_block bb;
2151   /* There is no point in performing inlining if errors have already
2152      occurred -- and we might crash if we try to inline invalid
2153      code.  */
2154   if (errorcount || sorrycount)
2155     return;
2156
2157   /* Clear out ID.  */
2158   memset (&id, 0, sizeof (id));
2159
2160   id.current_node = id.node = cgraph_node (fn);
2161   id.caller = fn;
2162   /* Or any functions that aren't finished yet.  */
2163   prev_fn = NULL_TREE;
2164   if (current_function_decl)
2165     {
2166       id.caller = current_function_decl;
2167       prev_fn = current_function_decl;
2168     }
2169   push_gimplify_context ();
2170
2171   /* Reach the trees by walking over the CFG, and note the
2172      enclosing basic-blocks in the call edges.  */
2173   /* We walk the blocks going forward, because inlined function bodies
2174      will split id->current_basic_block, and the new blocks will
2175      follow it; we'll trudge through them, processing their CALL_EXPRs
2176      along the way.  */
2177   FOR_EACH_BB (bb)
2178     gimple_expand_calls_inline (bb, &id);
2179
2180
2181   pop_gimplify_context (NULL);
2182   /* Renumber the (code) basic_blocks consecutively.  */
2183   compact_blocks ();
2184   /* Renumber the lexical scoping (non-code) blocks consecutively.  */
2185   number_blocks (fn);
2186
2187 #ifdef ENABLE_CHECKING
2188     {
2189       struct cgraph_edge *e;
2190
2191       verify_cgraph_node (id.node);
2192
2193       /* Double check that we inlined everything we are supposed to inline.  */
2194       for (e = id.node->callees; e; e = e->next_callee)
2195         gcc_assert (e->inline_failed);
2196     }
2197 #endif
2198   /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2199      as inlining loops might increase the maximum.  */
2200   if (ENTRY_BLOCK_PTR->count)
2201     counts_to_freqs ();
2202   fold_cond_expr_cond ();
2203 }
2204
2205 /* FN is a function that has a complete body, and CLONE is a function whose
2206    body is to be set to a copy of FN, mapping argument declarations according
2207    to the ARG_MAP splay_tree.  */
2208
2209 void
2210 clone_body (tree clone, tree fn, void *arg_map)
2211 {
2212   inline_data id;
2213
2214   /* Clone the body, as if we were making an inline call.  But, remap the
2215      parameters in the callee to the parameters of caller.  */
2216   memset (&id, 0, sizeof (id));
2217   id.caller = clone;
2218   id.callee = fn;
2219   id.callee_cfun = DECL_STRUCT_FUNCTION (fn);
2220   id.decl_map = (splay_tree)arg_map;
2221
2222   /* Cloning is treated slightly differently from inlining.  Set
2223      CLONING_P so that it's clear which operation we're performing.  */
2224   id.cloning_p = true;
2225
2226   /* We're not inside any EH region.  */
2227   id.eh_region = -1;
2228
2229   /* Actually copy the body.  */
2230   append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2231 }
2232
2233 /* Save duplicate body in FN.  MAP is used to pass around splay tree
2234    used to update argments in restore_body.  */
2235
2236 /* Make and return duplicate of body in FN.  Put copies of DECL_ARGUMENTS
2237    in *arg_copy and of the static chain, if any, in *sc_copy.  */
2238
2239 void
2240 save_body (tree fn, tree *arg_copy, tree *sc_copy)
2241 {
2242   inline_data id;
2243   tree newdecl, *parg;
2244   basic_block fn_entry_block;
2245
2246   memset (&id, 0, sizeof (id));
2247   id.callee = fn;
2248   id.callee_cfun = DECL_STRUCT_FUNCTION (fn);
2249   id.caller = fn;
2250   id.node = cgraph_node (fn);
2251   id.saving_p = true;
2252   id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2253   *arg_copy = DECL_ARGUMENTS (fn);
2254
2255   for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2256     {
2257       tree new = copy_node (*parg);
2258
2259       lang_hooks.dup_lang_specific_decl (new);
2260       DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*parg);
2261       insert_decl_map (&id, *parg, new);
2262       TREE_CHAIN (new) = TREE_CHAIN (*parg);
2263       *parg = new;
2264     }
2265
2266   *sc_copy = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2267   if (*sc_copy)
2268     {
2269       tree new = copy_node (*sc_copy);
2270
2271       lang_hooks.dup_lang_specific_decl (new);
2272       DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*sc_copy);
2273       insert_decl_map (&id, *sc_copy, new);
2274       TREE_CHAIN (new) = TREE_CHAIN (*sc_copy);
2275       *sc_copy = new;
2276     }
2277
2278   /* We're not inside any EH region.  */
2279   id.eh_region = -1;
2280
2281   insert_decl_map (&id, DECL_RESULT (fn), DECL_RESULT (fn));
2282
2283   /* Actually copy the body, including a new (struct function *) and CFG.
2284      EH info is also duplicated so its labels point into the copied
2285      CFG, not the original.  */
2286   fn_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fn));
2287   newdecl = copy_body (&id, fn_entry_block->count, fn_entry_block->frequency, NULL, NULL);
2288   DECL_STRUCT_FUNCTION (fn)->saved_cfg = DECL_STRUCT_FUNCTION (newdecl)->cfg;
2289   DECL_STRUCT_FUNCTION (fn)->saved_eh = DECL_STRUCT_FUNCTION (newdecl)->eh;
2290
2291   /* Clean up.  */
2292   splay_tree_delete (id.decl_map);
2293 }
2294
2295 /* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
2296
2297 tree
2298 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2299 {
2300   enum tree_code code = TREE_CODE (*tp);
2301
2302   /* We make copies of most nodes.  */
2303   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
2304       || code == TREE_LIST
2305       || code == TREE_VEC
2306       || code == TYPE_DECL)
2307     {
2308       /* Because the chain gets clobbered when we make a copy, we save it
2309          here.  */
2310       tree chain = TREE_CHAIN (*tp);
2311       tree new;
2312
2313       /* Copy the node.  */
2314       new = copy_node (*tp);
2315
2316       /* Propagate mudflap marked-ness.  */
2317       if (flag_mudflap && mf_marked_p (*tp))
2318         mf_mark (new);
2319
2320       *tp = new;
2321
2322       /* Now, restore the chain, if appropriate.  That will cause
2323          walk_tree to walk into the chain as well.  */
2324       if (code == PARM_DECL || code == TREE_LIST)
2325         TREE_CHAIN (*tp) = chain;
2326
2327       /* For now, we don't update BLOCKs when we make copies.  So, we
2328          have to nullify all BIND_EXPRs.  */
2329       if (TREE_CODE (*tp) == BIND_EXPR)
2330         BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2331     }
2332
2333   else if (TREE_CODE_CLASS (code) == tcc_type)
2334     *walk_subtrees = 0;
2335   else if (TREE_CODE_CLASS (code) == tcc_declaration)
2336     *walk_subtrees = 0;
2337   else if (TREE_CODE_CLASS (code) == tcc_constant)
2338     *walk_subtrees = 0;
2339   else
2340     gcc_assert (code != STATEMENT_LIST);
2341   return NULL_TREE;
2342 }
2343
2344 /* The SAVE_EXPR pointed to by TP is being copied.  If ST contains
2345    information indicating to what new SAVE_EXPR this one should be mapped,
2346    use that one.  Otherwise, create a new node and enter it in ST.  FN is
2347    the function into which the copy will be placed.  */
2348
2349 static void
2350 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2351 {
2352   splay_tree st = (splay_tree) st_;
2353   splay_tree_node n;
2354   tree t;
2355
2356   /* See if we already encountered this SAVE_EXPR.  */
2357   n = splay_tree_lookup (st, (splay_tree_key) *tp);
2358
2359   /* If we didn't already remap this SAVE_EXPR, do so now.  */
2360   if (!n)
2361     {
2362       t = copy_node (*tp);
2363
2364       /* Remember this SAVE_EXPR.  */
2365       splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2366       /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
2367       splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2368     }
2369   else
2370     {
2371       /* We've already walked into this SAVE_EXPR; don't do it again.  */
2372       *walk_subtrees = 0;
2373       t = (tree) n->value;
2374     }
2375
2376   /* Replace this SAVE_EXPR with the copy.  */
2377   *tp = t;
2378 }
2379
2380 /* Called via walk_tree.  If *TP points to a DECL_STMT for a local label,
2381    copies the declaration and enters it in the splay_tree in DATA (which is
2382    really an `inline_data *').  */
2383
2384 static tree
2385 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2386                         void *data)
2387 {
2388   inline_data *id = (inline_data *) data;
2389
2390   /* Don't walk into types.  */
2391   if (TYPE_P (*tp))
2392     *walk_subtrees = 0;
2393
2394   else if (TREE_CODE (*tp) == LABEL_EXPR)
2395     {
2396       tree decl = TREE_OPERAND (*tp, 0);
2397
2398       /* Copy the decl and remember the copy.  */
2399       insert_decl_map (id, decl,
2400                        copy_decl_for_inlining (decl, DECL_CONTEXT (decl),
2401                                                DECL_CONTEXT (decl)));
2402     }
2403
2404   return NULL_TREE;
2405 }
2406
2407 /* Perform any modifications to EXPR required when it is unsaved.  Does
2408    not recurse into EXPR's subtrees.  */
2409
2410 static void
2411 unsave_expr_1 (tree expr)
2412 {
2413   switch (TREE_CODE (expr))
2414     {
2415     case TARGET_EXPR:
2416       /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2417          It's OK for this to happen if it was part of a subtree that
2418          isn't immediately expanded, such as operand 2 of another
2419          TARGET_EXPR.  */
2420       if (TREE_OPERAND (expr, 1))
2421         break;
2422
2423       TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2424       TREE_OPERAND (expr, 3) = NULL_TREE;
2425       break;
2426
2427     default:
2428       break;
2429     }
2430 }
2431
2432 /* Called via walk_tree when an expression is unsaved.  Using the
2433    splay_tree pointed to by ST (which is really a `splay_tree'),
2434    remaps all local declarations to appropriate replacements.  */
2435
2436 static tree
2437 unsave_r (tree *tp, int *walk_subtrees, void *data)
2438 {
2439   inline_data *id = (inline_data *) data;
2440   splay_tree st = id->decl_map;
2441   splay_tree_node n;
2442
2443   /* Only a local declaration (variable or label).  */
2444   if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2445       || TREE_CODE (*tp) == LABEL_DECL)
2446     {
2447       /* Lookup the declaration.  */
2448       n = splay_tree_lookup (st, (splay_tree_key) *tp);
2449
2450       /* If it's there, remap it.  */
2451       if (n)
2452         *tp = (tree) n->value;
2453     }
2454
2455   else if (TREE_CODE (*tp) == STATEMENT_LIST)
2456     copy_statement_list (tp);
2457   else if (TREE_CODE (*tp) == BIND_EXPR)
2458     copy_bind_expr (tp, walk_subtrees, id);
2459   else if (TREE_CODE (*tp) == SAVE_EXPR)
2460     remap_save_expr (tp, st, walk_subtrees);
2461   else
2462     {
2463       copy_tree_r (tp, walk_subtrees, NULL);
2464
2465       /* Do whatever unsaving is required.  */
2466       unsave_expr_1 (*tp);
2467     }
2468
2469   /* Keep iterating.  */
2470   return NULL_TREE;
2471 }
2472
2473 /* Copies everything in EXPR and replaces variables, labels
2474    and SAVE_EXPRs local to EXPR.  */
2475
2476 tree
2477 unsave_expr_now (tree expr)
2478 {
2479   inline_data id;
2480
2481   /* There's nothing to do for NULL_TREE.  */
2482   if (expr == 0)
2483     return expr;
2484
2485   /* Set up ID.  */
2486   memset (&id, 0, sizeof (id));
2487   id.callee = current_function_decl;
2488   id.caller = current_function_decl;
2489   id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2490
2491   /* Walk the tree once to find local labels.  */
2492   walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2493
2494   /* Walk the tree again, copying, remapping, and unsaving.  */
2495   walk_tree (&expr, unsave_r, &id, NULL);
2496
2497   /* Clean up.  */
2498   splay_tree_delete (id.decl_map);
2499
2500   return expr;
2501 }
2502
2503 /* Allow someone to determine if SEARCH is a child of TOP from gdb.  */
2504
2505 static tree
2506 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2507 {
2508   if (*tp == data)
2509     return (tree) data;
2510   else
2511     return NULL;
2512 }
2513
2514 bool
2515 debug_find_tree (tree top, tree search)
2516 {
2517   return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2518 }
2519
2520
2521 /* Declare the variables created by the inliner.  Add all the variables in
2522    VARS to BIND_EXPR.  */
2523
2524 static void
2525 declare_inline_vars (tree block, tree vars)
2526 {
2527   tree t;
2528   for (t = vars; t; t = TREE_CHAIN (t))
2529     DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2530
2531   if (block)
2532     BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2533 }
2534
2535 /* Returns true if we're inlining.  */
2536 static inline bool
2537 inlining_p (inline_data *id)
2538 {
2539   return (!id->saving_p && !id->cloning_p);
2540 }