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