OSDN Git Service

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