OSDN Git Service

* cgraph.c: Add overall comment.
[pf3gnuchains/gcc-fork.git] / gcc / tree-inline.c
1 /* Control and data flow functions for trees.
2    Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3    Contributed by Alexandre Oliva <aoliva@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "rtl.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "insn-config.h"
35 #include "integrate.h"
36 #include "varray.h"
37 #include "hashtab.h"
38 #include "splay-tree.h"
39 #include "langhooks.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "diagnostic.h"
43 #include "function.h"
44
45 /* This should be eventually be generalized to other languages, but
46    this would require a shared function-as-trees infrastructure.  */
47 #ifndef INLINER_FOR_JAVA
48 #include "c-common.h"
49 #else /* INLINER_FOR_JAVA */
50 #include "parse.h"
51 #include "java-tree.h"
52 #endif /* INLINER_FOR_JAVA */
53
54 /* 0 if we should not perform inlining.
55    1 if we should expand functions calls inline at the tree level.
56    2 if we should consider *all* functions to be inline
57    candidates.  */
58
59 int flag_inline_trees = 0;
60
61 /* To Do:
62
63    o In order to make inlining-on-trees work, we pessimized
64      function-local static constants.  In particular, they are now
65      always output, even when not addressed.  Fix this by treating
66      function-local static constants just like global static
67      constants; the back-end already knows not to output them if they
68      are not needed.
69
70    o Provide heuristics to clamp inlining of recursive template
71      calls?  */
72
73 /* Data required for function inlining.  */
74
75 typedef struct inline_data
76 {
77   /* A stack of the functions we are inlining.  For example, if we are
78      compiling `f', which calls `g', which calls `h', and we are
79      inlining the body of `h', the stack will contain, `h', followed
80      by `g', followed by `f'.  The first few elements of the stack may
81      contain other functions that we know we should not recurse into,
82      even though they are not directly being inlined.  */
83   varray_type fns;
84   /* The index of the first element of FNS that really represents an
85      inlined function.  */
86   unsigned first_inlined_fn;
87   /* The label to jump to when a return statement is encountered.  If
88      this value is NULL, then return statements will simply be
89      remapped as return statements, rather than as jumps.  */
90   tree ret_label;
91   /* The map from local declarations in the inlined function to
92      equivalents in the function into which it is being inlined.  */
93   splay_tree decl_map;
94   /* Nonzero if we are currently within the cleanup for a
95      TARGET_EXPR.  */
96   int in_target_cleanup_p;
97   /* A list of the functions current function has inlined.  */
98   varray_type inlined_fns;
99   /* We use the same mechanism to build clones that we do to perform
100      inlining.  However, there are a few places where we need to
101      distinguish between those two situations.  This flag is true if
102      we are cloning, rather than inlining.  */
103   bool cloning_p;
104   /* Similarly for saving function body.  */
105   bool saving_p;
106   /* Hash table used to prevent walk_tree from visiting the same node
107      umpteen million times.  */
108   htab_t tree_pruner;
109   /* Callgraph node of function we are inlining into.  */
110   struct cgraph_node *node;
111   /* Callgraph node of currently inlined function.  */
112   struct cgraph_node *current_node;
113 } inline_data;
114
115 /* Prototypes.  */
116
117 static tree declare_return_variable (inline_data *, tree, tree *);
118 static tree copy_body_r (tree *, int *, void *);
119 static tree copy_body (inline_data *);
120 static tree expand_call_inline (tree *, int *, void *);
121 static void expand_calls_inline (tree *, inline_data *);
122 static bool inlinable_function_p (tree);
123 static tree remap_decl (tree, inline_data *);
124 static tree remap_type (tree, inline_data *);
125 #ifndef INLINER_FOR_JAVA
126 static tree initialize_inlined_parameters (inline_data *, tree, tree);
127 static void remap_block (tree, tree, inline_data *);
128 static void copy_scope_stmt (tree *, int *, inline_data *);
129 #else /* INLINER_FOR_JAVA */
130 static tree initialize_inlined_parameters (inline_data *, tree, tree, tree);
131 static void remap_block (tree *, tree, inline_data *);
132 static tree add_stmt_to_compound (tree, tree, tree);
133 #endif /* INLINER_FOR_JAVA */
134
135 /* Insert a tree->tree mapping for ID.  Despite the name suggests
136    that the trees should be variables, it is used for more than that.  */
137
138 static void
139 insert_decl_map (inline_data *id, tree key, tree value)
140 {
141   splay_tree_insert (id->decl_map, (splay_tree_key) key,
142                      (splay_tree_value) value);
143
144   /* Always insert an identity map as well.  If we see this same new
145      node again, we won't want to duplicate it a second time.  */
146   if (key != value)
147     splay_tree_insert (id->decl_map, (splay_tree_key) value,
148                        (splay_tree_value) value);
149 }
150
151 /* Remap DECL during the copying of the BLOCK tree for the function.  */
152
153 static tree
154 remap_decl (tree decl, inline_data *id)
155 {
156   splay_tree_node n;
157   tree fn;
158
159   /* We only remap local variables in the current function.  */
160   fn = VARRAY_TOP_TREE (id->fns);
161   if (! lang_hooks.tree_inlining.auto_var_in_fn_p (decl, fn))
162     return NULL_TREE;
163
164   /* See if we have remapped this declaration.  */
165   n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
166
167   /* If we didn't already have an equivalent for this declaration,
168      create one now.  */
169   if (!n)
170     {
171       tree t;
172
173       /* Make a copy of the variable or label.  */
174       t = copy_decl_for_inlining (decl, fn, VARRAY_TREE (id->fns, 0));
175
176       /* Remap types, if necessary.  */
177       TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
178       if (TREE_CODE (t) == TYPE_DECL)
179         DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
180       else if (TREE_CODE (t) == PARM_DECL)
181         DECL_ARG_TYPE_AS_WRITTEN (t)
182           = remap_type (DECL_ARG_TYPE_AS_WRITTEN (t), id);
183
184       /* Remap sizes as necessary.  */
185       walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
186       walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
187
188 #ifndef INLINER_FOR_JAVA
189       if (! DECL_NAME (t) && TREE_TYPE (t)
190           && lang_hooks.tree_inlining.anon_aggr_type_p (TREE_TYPE (t)))
191         {
192           /* For a VAR_DECL of anonymous type, we must also copy the
193              member VAR_DECLS here and rechain the DECL_ANON_UNION_ELEMS.  */
194           tree members = NULL;
195           tree src;
196
197           for (src = DECL_ANON_UNION_ELEMS (t); src;
198                src = TREE_CHAIN (src))
199             {
200               tree member = remap_decl (TREE_VALUE (src), id);
201
202               if (TREE_PURPOSE (src))
203                 abort ();
204               members = tree_cons (NULL, member, members);
205             }
206           DECL_ANON_UNION_ELEMS (t) = nreverse (members);
207         }
208 #endif /* not INLINER_FOR_JAVA */
209
210       /* Remember it, so that if we encounter this local entity
211          again we can reuse this copy.  */
212       insert_decl_map (id, decl, t);
213       return t;
214     }
215
216   return (tree) n->value;
217 }
218
219 static tree
220 remap_type (tree type, inline_data *id)
221 {
222   splay_tree_node node;
223   tree new, t;
224
225   if (type == NULL)
226     return type;
227
228   /* See if we have remapped this type.  */
229   node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
230   if (node)
231     return (tree) node->value;
232
233   /* The type only needs remapping if it's variably modified.  */
234   if (! variably_modified_type_p (type))
235     {
236       insert_decl_map (id, type, type);
237       return type;
238     }
239   
240   /* We do need a copy.  build and register it now.  */
241   new = copy_node (type);
242   insert_decl_map (id, type, new);
243
244   /* This is a new type, not a copy of an old type.  Need to reassociate
245      variants.  We can handle everything except the main variant lazily.  */
246   t = TYPE_MAIN_VARIANT (type);
247   if (type != t)
248     {
249       t = remap_type (t, id);
250       TYPE_MAIN_VARIANT (new) = t;
251       TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
252       TYPE_NEXT_VARIANT (t) = new;
253     }
254   else
255     {
256       TYPE_MAIN_VARIANT (new) = new;
257       TYPE_NEXT_VARIANT (new) = NULL;
258     }
259
260   /* Lazily create pointer and reference types.  */
261   TYPE_POINTER_TO (new) = NULL;
262   TYPE_REFERENCE_TO (new) = NULL;
263
264   switch (TREE_CODE (new))
265     {
266     case INTEGER_TYPE:
267     case REAL_TYPE:
268     case ENUMERAL_TYPE:
269     case BOOLEAN_TYPE:
270     case CHAR_TYPE:
271       t = TYPE_MIN_VALUE (new);
272       if (t && TREE_CODE (t) != INTEGER_CST)
273         walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
274       t = TYPE_MAX_VALUE (new);
275       if (t && TREE_CODE (t) != INTEGER_CST)
276         walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
277       return new;
278     
279     case POINTER_TYPE:
280       TREE_TYPE (new) = t = remap_type (TREE_TYPE (new), id);
281       if (TYPE_MODE (new) == ptr_mode)
282         TYPE_POINTER_TO (t) = new;
283       return new;
284
285     case REFERENCE_TYPE:
286       TREE_TYPE (new) = t = remap_type (TREE_TYPE (new), id);
287       if (TYPE_MODE (new) == ptr_mode)
288         TYPE_REFERENCE_TO (t) = new;
289       return new;
290
291     case METHOD_TYPE:
292     case FUNCTION_TYPE:
293       TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
294       walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
295       return new;
296
297     case ARRAY_TYPE:
298       TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
299       TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
300       break;
301
302     case RECORD_TYPE:
303     case UNION_TYPE:
304     case QUAL_UNION_TYPE:
305       walk_tree (&TYPE_FIELDS (new), copy_body_r, id, NULL);
306       break;
307
308     case FILE_TYPE:
309     case SET_TYPE:
310     case OFFSET_TYPE:
311     default:
312       /* Shouldn't have been thought variable sized.  */
313       abort ();
314     }
315
316   walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
317   walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
318
319   return new;
320 }
321
322 #ifndef INLINER_FOR_JAVA
323 /* Copy the SCOPE_STMT_BLOCK associated with SCOPE_STMT to contain
324    remapped versions of the variables therein.  And hook the new block
325    into the block-tree.  If non-NULL, the DECLS are declarations to
326    add to use instead of the BLOCK_VARS in the old block.  */
327 #else /* INLINER_FOR_JAVA */
328 /* Copy the BLOCK to contain remapped versions of the variables
329    therein.  And hook the new block into the block-tree.  */
330 #endif /* INLINER_FOR_JAVA */
331
332 static void
333 #ifndef INLINER_FOR_JAVA
334 remap_block (tree scope_stmt, tree decls, inline_data *id)
335 #else /* INLINER_FOR_JAVA */
336 remap_block (tree *block, tree decls, inline_data *id)
337 #endif /* INLINER_FOR_JAVA */
338 {
339 #ifndef INLINER_FOR_JAVA
340   /* We cannot do this in the cleanup for a TARGET_EXPR since we do
341      not know whether or not expand_expr will actually write out the
342      code we put there.  If it does not, then we'll have more BLOCKs
343      than block-notes, and things will go awry.  At some point, we
344      should make the back-end handle BLOCK notes in a tidier way,
345      without requiring a strict correspondence to the block-tree; then
346      this check can go.  */
347   if (id->in_target_cleanup_p)
348     {
349       SCOPE_STMT_BLOCK (scope_stmt) = NULL_TREE;
350       return;
351     }
352
353   /* If this is the beginning of a scope, remap the associated BLOCK.  */
354   if (SCOPE_BEGIN_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
355     {
356       tree old_block;
357       tree new_block;
358       tree old_var;
359       tree fn;
360
361       /* Make the new block.  */
362       old_block = SCOPE_STMT_BLOCK (scope_stmt);
363       new_block = make_node (BLOCK);
364       TREE_USED (new_block) = TREE_USED (old_block);
365       BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
366       SCOPE_STMT_BLOCK (scope_stmt) = new_block;
367
368       /* Remap its variables.  */
369       for (old_var = decls ? decls : BLOCK_VARS (old_block);
370            old_var;
371            old_var = TREE_CHAIN (old_var))
372         {
373           tree new_var;
374
375           /* Remap the variable.  */
376           new_var = remap_decl (old_var, id);
377           /* If we didn't remap this variable, so we can't mess with
378              its TREE_CHAIN.  If we remapped this variable to
379              something other than a declaration (say, if we mapped it
380              to a constant), then we must similarly omit any mention
381              of it here.  */
382           if (!new_var || !DECL_P (new_var))
383             ;
384           else
385             {
386               TREE_CHAIN (new_var) = BLOCK_VARS (new_block);
387               BLOCK_VARS (new_block) = new_var;
388             }
389         }
390       /* We put the BLOCK_VARS in reverse order; fix that now.  */
391       BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block));
392       fn = VARRAY_TREE (id->fns, 0);
393       if (id->cloning_p)
394         /* We're building a clone; DECL_INITIAL is still
395            error_mark_node, and current_binding_level is the parm
396            binding level.  */
397         lang_hooks.decls.insert_block (new_block);
398       else
399         {
400           /* Attach this new block after the DECL_INITIAL block for the
401              function into which this block is being inlined.  In
402              rest_of_compilation we will straighten out the BLOCK tree.  */
403           tree *first_block;
404           if (DECL_INITIAL (fn))
405             first_block = &BLOCK_CHAIN (DECL_INITIAL (fn));
406           else
407             first_block = &DECL_INITIAL (fn);
408           BLOCK_CHAIN (new_block) = *first_block;
409           *first_block = new_block;
410         }
411       /* Remember the remapped block.  */
412       insert_decl_map (id, old_block, new_block);
413     }
414   /* If this is the end of a scope, set the SCOPE_STMT_BLOCK to be the
415      remapped block.  */
416   else if (SCOPE_END_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
417     {
418       splay_tree_node n;
419
420       /* Find this block in the table of remapped things.  */
421       n = splay_tree_lookup (id->decl_map,
422                              (splay_tree_key) SCOPE_STMT_BLOCK (scope_stmt));
423       if (! n)
424         abort ();
425       SCOPE_STMT_BLOCK (scope_stmt) = (tree) n->value;
426     }
427 #else /* INLINER_FOR_JAVA */
428   tree old_block;
429   tree new_block;
430   tree old_var;
431   tree fn;
432
433   /* Make the new block.  */
434   old_block = *block;
435   new_block = make_node (BLOCK);
436   TREE_USED (new_block) = TREE_USED (old_block);
437   BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
438   BLOCK_SUBBLOCKS (new_block) = BLOCK_SUBBLOCKS (old_block);
439   TREE_SIDE_EFFECTS (new_block) = TREE_SIDE_EFFECTS (old_block);
440   TREE_TYPE (new_block) = TREE_TYPE (old_block);
441   *block = new_block;
442
443   /* Remap its variables.  */
444   for (old_var = decls ? decls : BLOCK_VARS (old_block);
445        old_var;
446        old_var = TREE_CHAIN (old_var))
447     {
448       tree new_var;
449
450       /* All local class initialization flags go in the outermost
451          scope.  */
452       if (LOCAL_CLASS_INITIALIZATION_FLAG_P (old_var))
453         {
454           /* We may already have one.  */
455           if (! splay_tree_lookup (id->decl_map, (splay_tree_key) old_var))
456             {
457               tree outermost_block;
458               new_var = remap_decl (old_var, id);
459               DECL_ABSTRACT_ORIGIN (new_var) = NULL;
460               outermost_block = DECL_SAVED_TREE (current_function_decl);
461               TREE_CHAIN (new_var) = BLOCK_VARS (outermost_block);
462               BLOCK_VARS (outermost_block) = new_var;
463             }
464           continue;
465         }
466
467       /* Remap the variable.  */
468       new_var = remap_decl (old_var, id);
469       /* If we didn't remap this variable, so we can't mess with
470          its TREE_CHAIN.  If we remapped this variable to
471          something other than a declaration (say, if we mapped it
472          to a constant), then we must similarly omit any mention
473          of it here.  */
474       if (!new_var || !DECL_P (new_var))
475         ;
476       else
477         {
478           TREE_CHAIN (new_var) = BLOCK_VARS (new_block);
479           BLOCK_VARS (new_block) = new_var;
480         }
481     }
482   /* We put the BLOCK_VARS in reverse order; fix that now.  */
483   BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block));
484   fn = VARRAY_TREE (id->fns, 0);
485   /* Remember the remapped block.  */
486   splay_tree_insert (id->decl_map,
487                      (splay_tree_key) old_block,
488                      (splay_tree_value) new_block);
489 #endif /* INLINER_FOR_JAVA */
490 }
491
492 #ifndef INLINER_FOR_JAVA
493 /* Copy the SCOPE_STMT pointed to by TP.  */
494
495 static void
496 copy_scope_stmt (tree *tp, int *walk_subtrees, inline_data *id)
497 {
498   tree block;
499
500   /* Remember whether or not this statement was nullified.  When
501      making a copy, copy_tree_r always sets SCOPE_NULLIFIED_P (and
502      doesn't copy the SCOPE_STMT_BLOCK) to free callers from having to
503      deal with copying BLOCKs if they do not wish to do so.  */
504   block = SCOPE_STMT_BLOCK (*tp);
505   /* Copy (and replace) the statement.  */
506   copy_tree_r (tp, walk_subtrees, NULL);
507   /* Restore the SCOPE_STMT_BLOCK.  */
508   SCOPE_STMT_BLOCK (*tp) = block;
509
510   /* Remap the associated block.  */
511   remap_block (*tp, NULL_TREE, id);
512 }
513 #endif /* not INLINER_FOR_JAVA */
514
515 /* Called from copy_body via walk_tree.  DATA is really an
516    `inline_data *'.  */
517 static tree
518 copy_body_r (tree *tp, int *walk_subtrees, void *data)
519 {
520   inline_data* id;
521   tree fn;
522
523   /* Set up.  */
524   id = (inline_data *) data;
525   fn = VARRAY_TOP_TREE (id->fns);
526
527 #if 0
528   /* All automatic variables should have a DECL_CONTEXT indicating
529      what function they come from.  */
530   if ((TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == LABEL_DECL)
531       && DECL_NAMESPACE_SCOPE_P (*tp))
532     if (! DECL_EXTERNAL (*tp) && ! TREE_STATIC (*tp))
533       abort ();
534 #endif
535
536 #ifdef INLINER_FOR_JAVA
537   if (TREE_CODE (*tp) == BLOCK)
538     remap_block (tp, NULL_TREE, id);
539 #endif
540
541   /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
542      GOTO_STMT with the RET_LABEL as its target.  */
543 #ifndef INLINER_FOR_JAVA
544   if (TREE_CODE (*tp) == RETURN_STMT && id->ret_label && !id->saving_p)
545 #else /* INLINER_FOR_JAVA */
546   if (TREE_CODE (*tp) == RETURN_EXPR && id->ret_label && !id->saving_p)
547 #endif /* INLINER_FOR_JAVA */
548     {
549       tree return_stmt = *tp;
550       tree goto_stmt;
551
552       /* Build the GOTO_STMT.  */
553 #ifndef INLINER_FOR_JAVA
554       goto_stmt = build_stmt (GOTO_STMT, id->ret_label);
555       TREE_CHAIN (goto_stmt) = TREE_CHAIN (return_stmt);
556       GOTO_FAKE_P (goto_stmt) = 1;
557 #else /* INLINER_FOR_JAVA */
558       tree assignment = TREE_OPERAND (return_stmt, 0);
559       goto_stmt = build1 (GOTO_EXPR, void_type_node, id->ret_label);
560       TREE_SIDE_EFFECTS (goto_stmt) = 1;
561 #endif /* INLINER_FOR_JAVA */
562
563       /* If we're returning something, just turn that into an
564          assignment into the equivalent of the original
565          RESULT_DECL.  */
566 #ifndef INLINER_FOR_JAVA
567       if (RETURN_STMT_EXPR (return_stmt))
568         {
569           *tp = build_stmt (EXPR_STMT,
570                             RETURN_STMT_EXPR (return_stmt));
571           STMT_IS_FULL_EXPR_P (*tp) = 1;
572           /* And then jump to the end of the function.  */
573           TREE_CHAIN (*tp) = goto_stmt;
574         }
575 #else /* INLINER_FOR_JAVA */
576       if (assignment)
577         {
578           copy_body_r (&assignment, walk_subtrees, data);
579           *tp = build (COMPOUND_EXPR, void_type_node, assignment, goto_stmt);
580           TREE_SIDE_EFFECTS (*tp) = 1;
581         }
582 #endif /* INLINER_FOR_JAVA */
583       /* If we're not returning anything just do the jump.  */
584       else
585         *tp = goto_stmt;
586
587       /* We can't replace return label while inlining function
588          because it is in the outer function.  */
589       insert_decl_map (id, id->ret_label, id->ret_label);
590     }
591   /* Local variables and labels need to be replaced by equivalent
592      variables.  We don't want to copy static variables; there's only
593      one of those, no matter how many times we inline the containing
594      function.  */
595   else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
596     {
597       tree new_decl;
598
599       /* Remap the declaration.  */
600       new_decl = remap_decl (*tp, id);
601       if (! new_decl)
602         abort ();
603       /* Replace this variable with the copy.  */
604       STRIP_TYPE_NOPS (new_decl);
605       *tp = new_decl;
606     }
607 #if 0
608   else if (nonstatic_local_decl_p (*tp)
609            && DECL_CONTEXT (*tp) != VARRAY_TREE (id->fns, 0))
610     abort ();
611 #endif
612   else if (TREE_CODE (*tp) == SAVE_EXPR)
613     remap_save_expr (tp, id->decl_map, VARRAY_TREE (id->fns, 0),
614                      walk_subtrees);
615   else if (TREE_CODE (*tp) == UNSAVE_EXPR)
616     /* UNSAVE_EXPRs should not be generated until expansion time.  */
617     abort ();
618 #ifndef INLINER_FOR_JAVA
619   /* For a SCOPE_STMT, we must copy the associated block so that we
620      can write out debugging information for the inlined variables.  */
621   else if (TREE_CODE (*tp) == SCOPE_STMT && !id->in_target_cleanup_p)
622     copy_scope_stmt (tp, walk_subtrees, id);
623 #else /* INLINER_FOR_JAVA */
624   else if (TREE_CODE (*tp) == LABELED_BLOCK_EXPR)
625     {
626       /* We need a new copy of this labeled block; the EXIT_BLOCK_EXPR
627          will refer to it, so save a copy ready for remapping.  We
628          save it in the decl_map, although it isn't a decl.  */
629       tree new_block = copy_node (*tp);
630       insert_decl_map (id, *tp, new_block);
631       *tp = new_block;
632     }
633   else if (TREE_CODE (*tp) == EXIT_BLOCK_EXPR)
634     {
635       splay_tree_node n
636         = splay_tree_lookup (id->decl_map,
637                              (splay_tree_key) TREE_OPERAND (*tp, 0));
638       /* We _must_ have seen the enclosing LABELED_BLOCK_EXPR.  */
639       if (! n)
640         abort ();
641       *tp = copy_node (*tp);
642       TREE_OPERAND (*tp, 0) = (tree) n->value;
643     }
644 #endif /* INLINER_FOR_JAVA */
645   /* Types may need remapping as well.  */
646   else if (TYPE_P (*tp))
647     *tp = remap_type (*tp, id);
648
649   /* Otherwise, just copy the node.  Note that copy_tree_r already
650      knows not to copy VAR_DECLs, etc., so this is safe.  */
651   else
652     {
653       tree old_node = *tp;
654
655       if (TREE_CODE (*tp) == MODIFY_EXPR
656           && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
657           && (lang_hooks.tree_inlining.auto_var_in_fn_p
658               (TREE_OPERAND (*tp, 0), fn)))
659         {
660           /* Some assignments VAR = VAR; don't generate any rtl code
661              and thus don't count as variable modification.  Avoid
662              keeping bogosities like 0 = 0.  */
663           tree decl = TREE_OPERAND (*tp, 0), value;
664           splay_tree_node n;
665
666           n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
667           if (n)
668             {
669               value = (tree) n->value;
670               STRIP_TYPE_NOPS (value);
671               if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
672                 {
673                   *tp = value;
674                   return copy_body_r (tp, walk_subtrees, data);
675                 }
676             }
677         }
678       else if (TREE_CODE (*tp) == ADDR_EXPR
679                && (lang_hooks.tree_inlining.auto_var_in_fn_p
680                    (TREE_OPERAND (*tp, 0), fn)))
681         {
682           /* Get rid of &* from inline substitutions.  It can occur when
683              someone takes the address of a parm or return slot passed by
684              invisible reference.  */
685           tree decl = TREE_OPERAND (*tp, 0), value;
686           splay_tree_node n;
687
688           n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
689           if (n)
690             {
691               value = (tree) n->value;
692               if (TREE_CODE (value) == INDIRECT_REF)
693                 {
694                   *tp = convert (TREE_TYPE (*tp), TREE_OPERAND (value, 0));
695                   return copy_body_r (tp, walk_subtrees, data);
696                 }
697             }
698         }
699
700       copy_tree_r (tp, walk_subtrees, NULL);
701
702       if (TREE_CODE (*tp) == CALL_EXPR && id->node && get_callee_fndecl (*tp))
703         {
704           if (id->saving_p)
705             {
706               struct cgraph_node *node;
707               struct cgraph_edge *edge;
708
709               for (node = id->node->next_clone; node; node = node->next_clone)
710                 {
711                   edge = cgraph_edge (node, old_node);
712                   if (edge)
713                     edge->call_expr = *tp;
714                   else
715                     abort ();
716                 }
717             }
718           else if (!id->cloning_p)
719             {
720               struct cgraph_edge *edge;
721
722               edge = cgraph_edge (id->current_node, old_node);
723               if (edge)
724                 cgraph_clone_edge (edge, id->node, *tp);
725             }
726         }
727
728       TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
729
730       /* The copied TARGET_EXPR has never been expanded, even if the
731          original node was expanded already.  */
732       if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
733         {
734           TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
735           TREE_OPERAND (*tp, 3) = NULL_TREE;
736         }
737     }
738
739   /* Keep iterating.  */
740   return NULL_TREE;
741 }
742
743 /* Make a copy of the body of FN so that it can be inserted inline in
744    another function.  */
745
746 static tree
747 copy_body (inline_data *id)
748 {
749   tree body;
750   tree fndecl = VARRAY_TOP_TREE (id->fns);
751
752   if (fndecl == current_function_decl
753       && cfun->saved_tree)
754     body = cfun->saved_tree;
755   else
756     body = DECL_SAVED_TREE (fndecl);
757   walk_tree (&body, copy_body_r, id, NULL);
758
759   return body;
760 }
761
762 /* Generate code to initialize the parameters of the function at the
763    top of the stack in ID from the ARGS (presented as a TREE_LIST).  */
764
765 static tree
766 #ifndef INLINER_FOR_JAVA
767 initialize_inlined_parameters (inline_data *id, tree args, tree fn)
768 #else /* INLINER_FOR_JAVA */
769 initialize_inlined_parameters (inline_data *id, tree args, tree fn, tree block)
770 #endif /* INLINER_FOR_JAVA */
771 {
772   tree init_stmts;
773   tree parms;
774   tree a;
775   tree p;
776 #ifdef INLINER_FOR_JAVA
777   tree vars = NULL_TREE;
778 #endif /* INLINER_FOR_JAVA */
779   int argnum = 0;
780
781   /* Figure out what the parameters are.  */
782   parms = DECL_ARGUMENTS (fn);
783   if (fn == current_function_decl && cfun->saved_args)
784     parms = cfun->saved_args;
785
786   /* Start with no initializations whatsoever.  */
787   init_stmts = NULL_TREE;
788
789   /* Loop through the parameter declarations, replacing each with an
790      equivalent VAR_DECL, appropriately initialized.  */
791   for (p = parms, a = args; p;
792        a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
793     {
794 #ifndef INLINER_FOR_JAVA
795       tree init_stmt;
796       tree cleanup;
797 #endif /* not INLINER_FOR_JAVA */
798       tree var;
799       tree value;
800       tree var_sub;
801
802       ++argnum;
803
804       /* Find the initializer.  */
805       value = lang_hooks.tree_inlining.convert_parm_for_inlining
806               (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
807
808       /* If the parameter is never assigned to, we may not need to
809          create a new variable here at all.  Instead, we may be able
810          to just use the argument value.  */
811       if (TREE_READONLY (p)
812           && !TREE_ADDRESSABLE (p)
813           && value && !TREE_SIDE_EFFECTS (value))
814         {
815           /* Simplify the value, if possible.  */
816           value = fold (DECL_P (value) ? decl_constant_value (value) : value);
817
818           /* We can't risk substituting complex expressions.  They
819              might contain variables that will be assigned to later.
820              Theoretically, we could check the expression to see if
821              all of the variables that determine its value are
822              read-only, but we don't bother.  */
823           if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
824             {
825               /* If this is a declaration, wrap it a NOP_EXPR so that
826                  we don't try to put the VALUE on the list of
827                  BLOCK_VARS.  */
828               if (DECL_P (value))
829                 value = build1 (NOP_EXPR, TREE_TYPE (value), value);
830
831               /* If this is a constant, make sure it has the right type.  */
832               else if (TREE_TYPE (value) != TREE_TYPE (p))
833                 value = fold (build1 (NOP_EXPR, TREE_TYPE (p), value));
834
835               insert_decl_map (id, p, value);
836               continue;
837             }
838         }
839
840       /* Make an equivalent VAR_DECL.  */
841       var = copy_decl_for_inlining (p, fn, VARRAY_TREE (id->fns, 0));
842
843       /* See if the frontend wants to pass this by invisible reference.  If
844          so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
845          replace uses of the PARM_DECL with dereferences.  */
846       if (TREE_TYPE (var) != TREE_TYPE (p)
847           && POINTER_TYPE_P (TREE_TYPE (var))
848           && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
849         var_sub = build1 (INDIRECT_REF, TREE_TYPE (p), var);
850       else
851         var_sub = var;
852
853       /* Register the VAR_DECL as the equivalent for the PARM_DECL;
854          that way, when the PARM_DECL is encountered, it will be
855          automatically replaced by the VAR_DECL.  */
856       insert_decl_map (id, p, var_sub);
857
858       /* Declare this new variable.  */
859 #ifndef INLINER_FOR_JAVA
860       init_stmt = build_stmt (DECL_STMT, var);
861       TREE_CHAIN (init_stmt) = init_stmts;
862       init_stmts = init_stmt;
863 #else /* INLINER_FOR_JAVA */
864       TREE_CHAIN (var) = vars;
865       vars = var;
866 #endif /* INLINER_FOR_JAVA */
867
868       /* Initialize this VAR_DECL from the equivalent argument.  If
869          the argument is an object, created via a constructor or copy,
870          this will not result in an extra copy: the TARGET_EXPR
871          representing the argument will be bound to VAR, and the
872          object will be constructed in VAR.  */
873       if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
874 #ifndef INLINER_FOR_JAVA
875         DECL_INITIAL (var) = value;
876       else
877         {
878           /* Even if P was TREE_READONLY, the new VAR should not be.
879              In the original code, we would have constructed a
880              temporary, and then the function body would have never
881              changed the value of P.  However, now, we will be
882              constructing VAR directly.  The constructor body may
883              change its value multiple times as it is being
884              constructed.  Therefore, it must not be TREE_READONLY;
885              the back-end assumes that TREE_READONLY variable is
886              assigned to only once.  */
887           TREE_READONLY (var) = 0;
888
889           /* Build a run-time initialization.  */
890           init_stmt = build_stmt (EXPR_STMT,
891                                   build (INIT_EXPR, TREE_TYPE (p),
892                                          var, value));
893           /* Add this initialization to the list.  Note that we want the
894              declaration *after* the initialization because we are going
895              to reverse all the initialization statements below.  */
896           TREE_CHAIN (init_stmt) = init_stmts;
897           init_stmts = init_stmt;
898         }
899
900       /* See if we need to clean up the declaration.  */
901       cleanup = lang_hooks.maybe_build_cleanup (var);
902       if (cleanup)
903         {
904           tree cleanup_stmt;
905           /* Build the cleanup statement.  */
906           cleanup_stmt = build_stmt (CLEANUP_STMT, var, cleanup);
907           /* Add it to the *front* of the list; the list will be
908              reversed below.  */
909           TREE_CHAIN (cleanup_stmt) = init_stmts;
910           init_stmts = cleanup_stmt;
911         }
912 #else /* INLINER_FOR_JAVA */
913         {
914           tree assignment = build (MODIFY_EXPR, TREE_TYPE (p), var, value);
915           init_stmts = add_stmt_to_compound (init_stmts, TREE_TYPE (p),
916                                              assignment);
917         }
918       else
919         {
920           /* Java objects don't ever need constructing when being
921              passed as arguments because only call by reference is
922              supported.  */
923           abort ();
924         }
925 #endif /* INLINER_FOR_JAVA */
926     }
927
928 #ifndef INLINER_FOR_JAVA
929   /* Evaluate trailing arguments.  */
930   for (; a; a = TREE_CHAIN (a))
931     {
932       tree init_stmt;
933       tree value = TREE_VALUE (a);
934
935       if (! value || ! TREE_SIDE_EFFECTS (value))
936         continue;
937
938       init_stmt = build_stmt (EXPR_STMT, value);
939       TREE_CHAIN (init_stmt) = init_stmts;
940       init_stmts = init_stmt;
941     }
942
943   /* The initialization statements have been built up in reverse
944      order.  Straighten them out now.  */
945   return nreverse (init_stmts);
946 #else /* INLINER_FOR_JAVA */
947   BLOCK_VARS (block) = nreverse (vars);
948   return init_stmts;
949 #endif /* INLINER_FOR_JAVA */
950 }
951
952 /* Declare a return variable to replace the RESULT_DECL for the
953    function we are calling.  An appropriate DECL_STMT is returned.
954    The USE_STMT is filled in to contain a use of the declaration to
955    indicate the return value of the function.  */
956
957 #ifndef INLINER_FOR_JAVA
958 static tree
959 declare_return_variable (struct inline_data *id, tree return_slot_addr,
960                          tree *use_stmt)
961 #else /* INLINER_FOR_JAVA */
962 static tree
963 declare_return_variable (struct inline_data *id, tree return_slot_addr,
964                          tree *var)
965 #endif /* INLINER_FOR_JAVA */
966 {
967   tree fn = VARRAY_TOP_TREE (id->fns);
968   tree result = DECL_RESULT (fn);
969 #ifndef INLINER_FOR_JAVA
970   tree var;
971 #endif /* not INLINER_FOR_JAVA */
972   int need_return_decl = 1;
973
974   /* We don't need to do anything for functions that don't return
975      anything.  */
976   if (!result || VOID_TYPE_P (TREE_TYPE (result)))
977     {
978 #ifndef INLINER_FOR_JAVA
979       *use_stmt = NULL_TREE;
980 #else /* INLINER_FOR_JAVA */
981       *var = NULL_TREE;
982 #endif /* INLINER_FOR_JAVA */
983       return NULL_TREE;
984     }
985
986 #ifndef INLINER_FOR_JAVA
987   var = (lang_hooks.tree_inlining.copy_res_decl_for_inlining
988          (result, fn, VARRAY_TREE (id->fns, 0), id->decl_map,
989           &need_return_decl, return_slot_addr));
990
991   /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
992      way, when the RESULT_DECL is encountered, it will be
993      automatically replaced by the VAR_DECL.  */
994   insert_decl_map (id, result, var);
995
996   /* Build the USE_STMT.  If the return type of the function was
997      promoted, convert it back to the expected type.  */
998   if (TREE_TYPE (var) == TREE_TYPE (TREE_TYPE (fn)))
999     *use_stmt = build_stmt (EXPR_STMT, var);
1000   else
1001     *use_stmt = build_stmt (EXPR_STMT,
1002                             build1 (NOP_EXPR, TREE_TYPE (TREE_TYPE (fn)),
1003                                     var));
1004   TREE_ADDRESSABLE (*use_stmt) = 1;
1005
1006   /* Build the declaration statement if FN does not return an
1007      aggregate.  */
1008   if (need_return_decl)
1009     return build_stmt (DECL_STMT, var);
1010 #else /* INLINER_FOR_JAVA */
1011   *var = (lang_hooks.tree_inlining.copy_res_decl_for_inlining
1012          (result, fn, VARRAY_TREE (id->fns, 0), id->decl_map,
1013           &need_return_decl, return_slot_addr));
1014
1015   splay_tree_insert (id->decl_map,
1016                      (splay_tree_key) result,
1017                      (splay_tree_value) *var);
1018   DECL_IGNORED_P (*var) = 1;
1019   if (need_return_decl)
1020     return *var;
1021 #endif /* INLINER_FOR_JAVA */
1022   /* If FN does return an aggregate, there's no need to declare the
1023      return variable; we're using a variable in our caller's frame.  */
1024   else
1025     return NULL_TREE;
1026 }
1027
1028 /* Returns nonzero if a function can be inlined as a tree.  */
1029
1030 bool
1031 tree_inlinable_function_p (tree fn)
1032 {
1033   return inlinable_function_p (fn);
1034 }
1035
1036 static const char *inline_forbidden_reason;
1037
1038 static tree
1039 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1040                       void *fnp)
1041 {
1042   tree node = *nodep;
1043   tree fn = (tree) fnp;
1044   tree t;
1045
1046   switch (TREE_CODE (node))
1047     {
1048     case CALL_EXPR:
1049       /* Refuse to inline alloca call unless user explicitly forced so as
1050          this may change program's memory overhead drastically when the
1051          function using alloca is called in loop.  In GCC present in
1052          SPEC2000 inlining into schedule_block cause it to require 2GB of
1053          RAM instead of 256MB.  */
1054       if (alloca_call_p (node)
1055           && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1056         {
1057           inline_forbidden_reason
1058             = N_("%Jfunction '%F' can never be inlined because it uses "
1059                  "alloca (override using the always_inline attribute)");
1060           return node;
1061         }
1062       t = get_callee_fndecl (node);
1063       if (! t)
1064         break;
1065
1066
1067       /* We cannot inline functions that call setjmp.  */
1068       if (setjmp_call_p (t))
1069         {
1070           inline_forbidden_reason
1071             = N_("%Jfunction '%F' can never be inlined because it uses setjmp");
1072           return node;
1073         }
1074
1075       if (DECL_BUILT_IN (t))
1076         switch (DECL_FUNCTION_CODE (t))
1077           {
1078             /* We cannot inline functions that take a variable number of
1079                arguments.  */
1080           case BUILT_IN_VA_START:
1081           case BUILT_IN_STDARG_START:
1082           case BUILT_IN_NEXT_ARG:
1083           case BUILT_IN_VA_END:
1084             {
1085               inline_forbidden_reason
1086                 = N_("%Jfunction '%F' can never be inlined because it "
1087                      "uses variable argument lists");
1088               return node;
1089             }
1090           case BUILT_IN_LONGJMP:
1091             {
1092               /* We can't inline functions that call __builtin_longjmp at
1093                  all.  The non-local goto machinery really requires the
1094                  destination be in a different function.  If we allow the
1095                  function calling __builtin_longjmp to be inlined into the
1096                  function calling __builtin_setjmp, Things will Go Awry.  */
1097               /* ??? Need front end help to identify "regular" non-local
1098                  goto.  */
1099               if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1100                 {
1101                   inline_forbidden_reason
1102                     = N_("%Jfunction '%F' can never be inlined because "
1103                          "it uses setjmp-longjmp exception handling");
1104                   return node;
1105                 }
1106             }
1107
1108           default:
1109             break;
1110           }
1111       break;
1112
1113 #ifndef INLINER_FOR_JAVA
1114     case DECL_STMT:
1115       /* We cannot inline functions that contain other functions.  */
1116       if (TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
1117           && DECL_INITIAL (TREE_OPERAND (node, 0)))
1118         {
1119           inline_forbidden_reason
1120             = N_("%Jfunction '%F' can never be inlined "
1121                  "because it contains a nested function");
1122           return node;
1123         }
1124       break;
1125
1126     case GOTO_STMT:
1127     case GOTO_EXPR:
1128       t = TREE_OPERAND (node, 0);
1129
1130       /* We will not inline a function which uses computed goto.  The
1131          addresses of its local labels, which may be tucked into
1132          global storage, are of course not constant across
1133          instantiations, which causes unexpected behavior.  */
1134       if (TREE_CODE (t) != LABEL_DECL)
1135         {
1136           inline_forbidden_reason
1137             = N_("%Jfunction '%F' can never be inlined "
1138                  "because it contains a computed goto");
1139           return node;
1140         }
1141
1142       /* We cannot inline a nested function that jumps to a nonlocal
1143          label.  */
1144       if (TREE_CODE (t) == LABEL_DECL && DECL_CONTEXT (t) != fn)
1145         {
1146           inline_forbidden_reason
1147             = N_("%Jfunction '%F' can never be inlined "
1148                  "because it contains a nonlocal goto");
1149           return node;
1150         }
1151
1152       break;
1153
1154     case RECORD_TYPE:
1155     case UNION_TYPE:
1156       /* We cannot inline a function of the form
1157
1158            void F (int i) { struct S { int ar[i]; } s; }
1159
1160          Attempting to do so produces a catch-22.
1161          If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1162          UNION_TYPE nodes, then it goes into infinite recursion on a
1163          structure containing a pointer to its own type.  If it doesn't,
1164          then the type node for S doesn't get adjusted properly when
1165          F is inlined, and we abort in find_function_data.  */
1166       for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1167         if (variably_modified_type_p (TREE_TYPE (t)))
1168           {
1169             inline_forbidden_reason
1170               = N_("%Jfunction '%F' can never be inlined "
1171                    "because it uses variable sized variables");
1172             return node;
1173           }
1174 #endif
1175     default:
1176       break;
1177     }
1178
1179   return NULL_TREE;
1180 }
1181
1182 /* Return subexpression representing possible alloca call, if any.  */
1183 static tree
1184 inline_forbidden_p (tree fndecl)
1185 {
1186   location_t saved_loc = input_location;
1187   tree ret = walk_tree_without_duplicates
1188                 (&DECL_SAVED_TREE (fndecl), inline_forbidden_p_1, fndecl);
1189   input_location = saved_loc;
1190   return ret;
1191 }
1192
1193 /* Returns nonzero if FN is a function that does not have any
1194    fundamental inline blocking properties.  */
1195
1196 static bool
1197 inlinable_function_p (tree fn)
1198 {
1199   bool inlinable = true;
1200
1201   /* If we've already decided this function shouldn't be inlined,
1202      there's no need to check again.  */
1203   if (DECL_UNINLINABLE (fn))
1204     return false;
1205
1206   /* See if there is any language-specific reason it cannot be
1207      inlined.  (It is important that this hook be called early because
1208      in C++ it may result in template instantiation.)
1209      If the function is not inlinable for language-specific reasons,
1210      it is left up to the langhook to explain why.  */
1211   inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1212
1213   /* If we don't have the function body available, we can't inline it.
1214      However, this should not be recorded since we also get here for
1215      forward declared inline functions.  Therefore, return at once.  */
1216   if (!DECL_SAVED_TREE (fn))
1217     return false;
1218
1219   /* If we're not inlining at all, then we cannot inline this function.  */
1220   else if (!flag_inline_trees)
1221     inlinable = false;
1222
1223   /* Only try to inline functions if DECL_INLINE is set.  This should be
1224      true for all functions declared `inline', and for all other functions
1225      as well with -finline-functions.
1226
1227      Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1228      it's the front-end that must set DECL_INLINE in this case, because
1229      dwarf2out loses if a function that does not have DECL_INLINE set is
1230      inlined anyway.  That is why we have both DECL_INLINE and
1231      DECL_DECLARED_INLINE_P.  */
1232   /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1233             here should be redundant.  */
1234   else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1235     inlinable = false;
1236
1237 #ifdef INLINER_FOR_JAVA
1238   /* Synchronized methods can't be inlined.  This is a bug.  */
1239   else if (METHOD_SYNCHRONIZED (fn))
1240     inlinable = false;
1241 #endif /* INLINER_FOR_JAVA */
1242
1243   else if (inline_forbidden_p (fn))
1244     {
1245       /* See if we should warn about uninlinable functions.  Previously,
1246          some of these warnings would be issued while trying to expand
1247          the function inline, but that would cause multiple warnings
1248          about functions that would for example call alloca.  But since
1249          this a property of the function, just one warning is enough.
1250          As a bonus we can now give more details about the reason why a
1251          function is not inlinable.
1252          We only warn for functions declared `inline' by the user.  */
1253       bool do_warning = (warn_inline
1254                          && DECL_INLINE (fn)
1255                          && DECL_DECLARED_INLINE_P (fn)
1256                          && !DECL_IN_SYSTEM_HEADER (fn));
1257
1258       if (lookup_attribute ("always_inline",
1259                             DECL_ATTRIBUTES (fn)))
1260         sorry (inline_forbidden_reason, fn, fn);
1261       else if (do_warning)
1262         warning (inline_forbidden_reason, fn, fn);
1263
1264       inlinable = false;
1265     }
1266
1267   /* Squirrel away the result so that we don't have to check again.  */
1268   DECL_UNINLINABLE (fn) = !inlinable;
1269
1270   return inlinable;
1271 }
1272
1273 /* If *TP is a CALL_EXPR, replace it with its inline expansion.  */
1274
1275 static tree
1276 expand_call_inline (tree *tp, int *walk_subtrees, void *data)
1277 {
1278   inline_data *id;
1279   tree t;
1280   tree expr;
1281   tree stmt;
1282 #ifndef INLINER_FOR_JAVA
1283   tree chain;
1284   tree scope_stmt;
1285   tree use_stmt;
1286 #else /* INLINER_FOR_JAVA */
1287   tree retvar;
1288 #endif /* INLINER_FOR_JAVA */
1289   tree fn;
1290   tree arg_inits;
1291   tree *inlined_body;
1292   splay_tree st;
1293   tree args;
1294   tree return_slot_addr;
1295   struct cgraph_edge *edge;
1296   const char *reason;
1297
1298   /* See what we've got.  */
1299   id = (inline_data *) data;
1300   t = *tp;
1301
1302   /* Recurse, but letting recursive invocations know that we are
1303      inside the body of a TARGET_EXPR.  */
1304   if (TREE_CODE (*tp) == TARGET_EXPR)
1305     {
1306 #ifndef INLINER_FOR_JAVA
1307       int i, len = first_rtl_op (TARGET_EXPR);
1308
1309       /* We're walking our own subtrees.  */
1310       *walk_subtrees = 0;
1311
1312       /* Actually walk over them.  This loop is the body of
1313          walk_trees, omitting the case where the TARGET_EXPR
1314          itself is handled.  */
1315       for (i = 0; i < len; ++i)
1316         {
1317           if (i == 2)
1318             ++id->in_target_cleanup_p;
1319           walk_tree (&TREE_OPERAND (*tp, i), expand_call_inline, data,
1320                      id->tree_pruner);
1321           if (i == 2)
1322             --id->in_target_cleanup_p;
1323         }
1324
1325       return NULL_TREE;
1326 #else /* INLINER_FOR_JAVA */
1327       abort ();
1328 #endif /* INLINER_FOR_JAVA */
1329     }
1330   else if (TREE_CODE (t) == EXPR_WITH_FILE_LOCATION)
1331     {
1332       /* We're walking the subtree directly.  */
1333       *walk_subtrees = 0;
1334       /* Update the source position.  */
1335       push_srcloc (EXPR_WFL_FILENAME (t), EXPR_WFL_LINENO (t));
1336       walk_tree (&EXPR_WFL_NODE (t), expand_call_inline, data,
1337                  id->tree_pruner);
1338       /* Restore the original source position.  */
1339       pop_srcloc ();
1340
1341       return NULL_TREE;
1342     }
1343
1344   if (TYPE_P (t))
1345     /* Because types were not copied in copy_body, CALL_EXPRs beneath
1346        them should not be expanded.  This can happen if the type is a
1347        dynamic array type, for example.  */
1348     *walk_subtrees = 0;
1349
1350   /* From here on, we're only interested in CALL_EXPRs.  */
1351   if (TREE_CODE (t) != CALL_EXPR)
1352     return NULL_TREE;
1353
1354   /* First, see if we can figure out what function is being called.
1355      If we cannot, then there is no hope of inlining the function.  */
1356   fn = get_callee_fndecl (t);
1357   if (!fn)
1358     return NULL_TREE;
1359
1360   /* Turn forward declarations into real ones.  */
1361   fn = cgraph_node (fn)->decl;
1362
1363   /* If fn is a declaration of a function in a nested scope that was
1364      globally declared inline, we don't set its DECL_INITIAL.
1365      However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1366      C++ front-end uses it for cdtors to refer to their internal
1367      declarations, that are not real functions.  Fortunately those
1368      don't have trees to be saved, so we can tell by checking their
1369      DECL_SAVED_TREE.  */
1370   if (! DECL_INITIAL (fn)
1371       && DECL_ABSTRACT_ORIGIN (fn)
1372       && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1373     fn = DECL_ABSTRACT_ORIGIN (fn);
1374
1375   /* Objective C and fortran still calls tree_rest_of_compilation directly.
1376      Kill this check once this is fixed.  */
1377   if (!id->current_node->analyzed)
1378     return NULL_TREE;
1379
1380   edge = cgraph_edge (id->current_node, t);
1381
1382   /* Constant propagation on argument done during previous inlining
1383      may create new direct call.  Produce an edge for it.  */
1384   if (!edge)
1385     {
1386       struct cgraph_node *dest = cgraph_node (fn);
1387
1388       /* FN must have address taken so it can be passed as argument.  */
1389       if (!dest->needed)
1390         abort ();
1391       cgraph_create_edge (id->node, dest, t)->inline_failed
1392         = N_("originally indirect function call not considered for inlining");
1393       return NULL_TREE;
1394     }
1395
1396   /* Don't try to inline functions that are not well-suited to
1397      inlining.  */
1398   if (!cgraph_inline_p (edge, &reason))
1399     {
1400       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1401         {
1402           sorry ("%Jinlining failed in call to '%F': %s", fn, fn, reason);
1403           sorry ("called from here");
1404         }
1405       else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
1406                && !DECL_IN_SYSTEM_HEADER (fn)
1407                && strlen (reason))
1408         {
1409           warning ("%Jinlining failed in call to '%F': %s", fn, fn, reason);
1410           warning ("called from here");
1411         }
1412       return NULL_TREE;
1413     }
1414
1415 #ifdef ENABLE_CHECKING
1416   if (edge->callee->decl != id->node->decl)
1417     verify_cgraph_node (edge->callee);
1418 #endif
1419
1420   if (! lang_hooks.tree_inlining.start_inlining (fn))
1421     return NULL_TREE;
1422
1423   /* Set the current filename and line number to the function we are
1424      inlining so that when we create new _STMT nodes here they get
1425      line numbers corresponding to the function we are calling.  We
1426      wrap the whole inlined body in an EXPR_WITH_FILE_AND_LINE as well
1427      because individual statements don't record the filename.  */
1428   push_srcloc (DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
1429
1430 #ifndef INLINER_FOR_JAVA
1431   /* Build a statement-expression containing code to initialize the
1432      arguments, the actual inline expansion of the body, and a label
1433      for the return statements within the function to jump to.  The
1434      type of the statement expression is the return type of the
1435      function call.  */
1436   expr = build1 (STMT_EXPR, TREE_TYPE (TREE_TYPE (fn)), make_node (COMPOUND_STMT));
1437   /* There is no scope associated with the statement-expression.  */
1438   STMT_EXPR_NO_SCOPE (expr) = 1;
1439   if (lookup_attribute ("warn_unused_result",
1440                         TYPE_ATTRIBUTES (TREE_TYPE (fn))))
1441     STMT_EXPR_WARN_UNUSED_RESULT (expr) = 1;
1442   stmt = STMT_EXPR_STMT (expr);
1443 #else /* INLINER_FOR_JAVA */
1444   /* Build a block containing code to initialize the arguments, the
1445      actual inline expansion of the body, and a label for the return
1446      statements within the function to jump to.  The type of the
1447      statement expression is the return type of the function call.  */
1448   stmt = NULL;
1449   expr = build (BLOCK, TREE_TYPE (TREE_TYPE (fn)));
1450 #endif /* INLINER_FOR_JAVA */
1451
1452   /* Local declarations will be replaced by their equivalents in this
1453      map.  */
1454   st = id->decl_map;
1455   id->decl_map = splay_tree_new (splay_tree_compare_pointers,
1456                                  NULL, NULL);
1457
1458   /* Initialize the parameters.  */
1459   args = TREE_OPERAND (t, 1);
1460   return_slot_addr = NULL_TREE;
1461   if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (t))
1462     {
1463       return_slot_addr = TREE_VALUE (args);
1464       args = TREE_CHAIN (args);
1465     }
1466
1467 #ifndef INLINER_FOR_JAVA
1468   arg_inits = initialize_inlined_parameters (id, args, fn);
1469   /* Expand any inlined calls in the initializers.  Do this before we
1470      push FN on the stack of functions we are inlining; we want to
1471      inline calls to FN that appear in the initializers for the
1472      parameters.  */
1473   expand_calls_inline (&arg_inits, id);
1474   /* And add them to the tree.  */
1475   COMPOUND_BODY (stmt) = chainon (COMPOUND_BODY (stmt), arg_inits);
1476 #else /* INLINER_FOR_JAVA */
1477   arg_inits = initialize_inlined_parameters (id, args, fn, expr);
1478   if (arg_inits)
1479     {
1480       /* Expand any inlined calls in the initializers.  Do this before we
1481          push FN on the stack of functions we are inlining; we want to
1482          inline calls to FN that appear in the initializers for the
1483          parameters.  */
1484       expand_calls_inline (&arg_inits, id);
1485
1486       /* And add them to the tree.  */
1487       BLOCK_EXPR_BODY (expr) = add_stmt_to_compound (BLOCK_EXPR_BODY (expr),
1488                                                      TREE_TYPE (arg_inits),
1489                                                      arg_inits);
1490     }
1491 #endif /* INLINER_FOR_JAVA */
1492
1493   /* Record the function we are about to inline so that we can avoid
1494      recursing into it.  */
1495   VARRAY_PUSH_TREE (id->fns, fn);
1496
1497   /* Record the function we are about to inline if optimize_function
1498      has not been called on it yet and we don't have it in the list.  */
1499   if (! DECL_INLINED_FNS (fn))
1500     {
1501       int i;
1502
1503       for (i = VARRAY_ACTIVE_SIZE (id->inlined_fns) - 1; i >= 0; i--)
1504         if (VARRAY_TREE (id->inlined_fns, i) == fn)
1505           break;
1506       if (i < 0)
1507         VARRAY_PUSH_TREE (id->inlined_fns, fn);
1508     }
1509
1510   /* Return statements in the function body will be replaced by jumps
1511      to the RET_LABEL.  */
1512   id->ret_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
1513   DECL_CONTEXT (id->ret_label) = VARRAY_TREE (id->fns, 0);
1514
1515   if (! DECL_INITIAL (fn)
1516       || TREE_CODE (DECL_INITIAL (fn)) != BLOCK)
1517     abort ();
1518
1519 #ifndef INLINER_FOR_JAVA
1520   /* Create a block to put the parameters in.  We have to do this
1521      after the parameters have been remapped because remapping
1522      parameters is different from remapping ordinary variables.  */
1523   scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
1524   SCOPE_BEGIN_P (scope_stmt) = 1;
1525   SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
1526   remap_block (scope_stmt, DECL_ARGUMENTS (fn), id);
1527   TREE_CHAIN (scope_stmt) = COMPOUND_BODY (stmt);
1528   COMPOUND_BODY (stmt) = scope_stmt;
1529
1530   /* Tell the debugging backends that this block represents the
1531      outermost scope of the inlined function.  */
1532   if (SCOPE_STMT_BLOCK (scope_stmt))
1533     BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope_stmt)) = DECL_ORIGIN (fn);
1534
1535   /* Declare the return variable for the function.  */
1536   COMPOUND_BODY (stmt)
1537     = chainon (COMPOUND_BODY (stmt),
1538                declare_return_variable (id, return_slot_addr, &use_stmt));
1539 #else /* INLINER_FOR_JAVA */
1540   {
1541     /* Declare the return variable for the function.  */
1542     tree decl = declare_return_variable (id, return_slot_addr, &retvar);
1543     if (retvar)
1544       {
1545         tree *next = &BLOCK_VARS (expr);
1546         while (*next)
1547           next = &TREE_CHAIN (*next);
1548         *next = decl;
1549       }
1550   }
1551 #endif /* INLINER_FOR_JAVA */
1552
1553   /* After we've initialized the parameters, we insert the body of the
1554      function itself.  */
1555   {
1556     struct cgraph_node *old_node = id->current_node;
1557
1558     id->current_node = edge->callee;
1559 #ifndef INLINER_FOR_JAVA
1560     inlined_body = &COMPOUND_BODY (stmt);
1561     while (*inlined_body)
1562       inlined_body = &TREE_CHAIN (*inlined_body);
1563     *inlined_body = copy_body (id);
1564 #else /* INLINER_FOR_JAVA */
1565     {
1566       tree new_body;
1567       java_inlining_map_static_initializers (fn, id->decl_map);
1568       new_body = copy_body (id);
1569       TREE_TYPE (new_body) = TREE_TYPE (TREE_TYPE (fn));
1570       BLOCK_EXPR_BODY (expr)
1571         = add_stmt_to_compound (BLOCK_EXPR_BODY (expr),
1572                                 TREE_TYPE (new_body), new_body);
1573       inlined_body = &BLOCK_EXPR_BODY (expr);
1574     }
1575 #endif /* INLINER_FOR_JAVA */
1576     id->current_node = old_node;
1577   }
1578
1579   /* After the body of the function comes the RET_LABEL.  This must come
1580      before we evaluate the returned value below, because that evaluation
1581      may cause RTL to be generated.  */
1582 #ifndef INLINER_FOR_JAVA
1583   COMPOUND_BODY (stmt)
1584     = chainon (COMPOUND_BODY (stmt),
1585                build_stmt (LABEL_STMT, id->ret_label));
1586 #else /* INLINER_FOR_JAVA */
1587   {
1588     tree label = build1 (LABEL_EXPR, void_type_node, id->ret_label);
1589     BLOCK_EXPR_BODY (expr)
1590       = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), void_type_node, label);
1591     TREE_SIDE_EFFECTS (label) = TREE_SIDE_EFFECTS (t);
1592   }
1593 #endif /* INLINER_FOR_JAVA */
1594
1595   /* Finally, mention the returned value so that the value of the
1596      statement-expression is the returned value of the function.  */
1597 #ifndef INLINER_FOR_JAVA
1598   COMPOUND_BODY (stmt) = chainon (COMPOUND_BODY (stmt), use_stmt);
1599
1600   /* Close the block for the parameters.  */
1601   scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
1602   SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
1603   remap_block (scope_stmt, NULL_TREE, id);
1604   COMPOUND_BODY (stmt)
1605     = chainon (COMPOUND_BODY (stmt), scope_stmt);
1606 #else /* INLINER_FOR_JAVA */
1607   if (retvar)
1608     {
1609       /* Mention the retvar.  If the return type of the function was
1610          promoted, convert it back to the expected type.  */
1611       if (TREE_TYPE (TREE_TYPE (fn)) != TREE_TYPE (retvar))
1612         retvar = build1 (NOP_EXPR, TREE_TYPE (TREE_TYPE (fn)), retvar);
1613       BLOCK_EXPR_BODY (expr)
1614         = add_stmt_to_compound (BLOCK_EXPR_BODY (expr),
1615                                 TREE_TYPE (retvar), retvar);
1616     }
1617
1618   java_inlining_merge_static_initializers (fn, id->decl_map);
1619 #endif /* INLINER_FOR_JAVA */
1620
1621   /* Clean up.  */
1622   splay_tree_delete (id->decl_map);
1623   id->decl_map = st;
1624
1625   /* The new expression has side-effects if the old one did.  */
1626   TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (t);
1627
1628   /* Replace the call by the inlined body.  Wrap it in an
1629      EXPR_WITH_FILE_LOCATION so that we'll get debugging line notes
1630      pointing to the right place.  */
1631 #ifndef INLINER_FOR_JAVA
1632   chain = TREE_CHAIN (*tp);
1633 #endif /* INLINER_FOR_JAVA */
1634   *tp = build_expr_wfl (expr, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn),
1635                         /*col=*/0);
1636   EXPR_WFL_EMIT_LINE_NOTE (*tp) = 1;
1637 #ifndef INLINER_FOR_JAVA
1638   TREE_CHAIN (*tp) = chain;
1639 #endif /* not INLINER_FOR_JAVA */
1640   pop_srcloc ();
1641
1642   /* If the value of the new expression is ignored, that's OK.  We
1643      don't warn about this for CALL_EXPRs, so we shouldn't warn about
1644      the equivalent inlined version either.  */
1645   TREE_USED (*tp) = 1;
1646
1647   /* Update callgraph if needed.  */
1648   cgraph_remove_node (edge->callee);
1649
1650   /* Recurse into the body of the just inlined function.  */
1651   expand_calls_inline (inlined_body, id);
1652   VARRAY_POP (id->fns);
1653
1654   /* Don't walk into subtrees.  We've already handled them above.  */
1655   *walk_subtrees = 0;
1656
1657   lang_hooks.tree_inlining.end_inlining (fn);
1658
1659   /* Keep iterating.  */
1660   return NULL_TREE;
1661 }
1662 /* Walk over the entire tree *TP, replacing CALL_EXPRs with inline
1663    expansions as appropriate.  */
1664
1665 static void
1666 expand_calls_inline (tree *tp, inline_data *id)
1667 {
1668   /* Search through *TP, replacing all calls to inline functions by
1669      appropriate equivalents.  Use walk_tree in no-duplicates mode
1670      to avoid exponential time complexity.  (We can't just use
1671      walk_tree_without_duplicates, because of the special TARGET_EXPR
1672      handling in expand_calls.  The hash table is set up in
1673      optimize_function.  */
1674   walk_tree (tp, expand_call_inline, id, id->tree_pruner);
1675 }
1676
1677 /* Expand calls to inline functions in the body of FN.  */
1678
1679 void
1680 optimize_inline_calls (tree fn)
1681 {
1682   inline_data id;
1683   tree prev_fn;
1684
1685   /* There is no point in performing inlining if errors have already
1686      occurred -- and we might crash if we try to inline invalid
1687      code.  */
1688   if (errorcount || sorrycount)
1689     return;
1690
1691   /* Clear out ID.  */
1692   memset (&id, 0, sizeof (id));
1693
1694   id.current_node = id.node = cgraph_node (fn);
1695   /* Don't allow recursion into FN.  */
1696   VARRAY_TREE_INIT (id.fns, 32, "fns");
1697   VARRAY_PUSH_TREE (id.fns, fn);
1698   /* Or any functions that aren't finished yet.  */
1699   prev_fn = NULL_TREE;
1700   if (current_function_decl)
1701     {
1702       VARRAY_PUSH_TREE (id.fns, current_function_decl);
1703       prev_fn = current_function_decl;
1704     }
1705
1706   prev_fn = (lang_hooks.tree_inlining.add_pending_fn_decls
1707              (&id.fns, prev_fn));
1708
1709   /* Create the list of functions this call will inline.  */
1710   VARRAY_TREE_INIT (id.inlined_fns, 32, "inlined_fns");
1711
1712   /* Keep track of the low-water mark, i.e., the point where the first
1713      real inlining is represented in ID.FNS.  */
1714   id.first_inlined_fn = VARRAY_ACTIVE_SIZE (id.fns);
1715
1716   /* Replace all calls to inline functions with the bodies of those
1717      functions.  */
1718   id.tree_pruner = htab_create (37, htab_hash_pointer,
1719                                 htab_eq_pointer, NULL);
1720   expand_calls_inline (&DECL_SAVED_TREE (fn), &id);
1721
1722   /* Clean up.  */
1723   htab_delete (id.tree_pruner);
1724   if (DECL_LANG_SPECIFIC (fn))
1725     {
1726       tree ifn = make_tree_vec (VARRAY_ACTIVE_SIZE (id.inlined_fns));
1727
1728       if (VARRAY_ACTIVE_SIZE (id.inlined_fns))
1729         memcpy (&TREE_VEC_ELT (ifn, 0), &VARRAY_TREE (id.inlined_fns, 0),
1730                 VARRAY_ACTIVE_SIZE (id.inlined_fns) * sizeof (tree));
1731       DECL_INLINED_FNS (fn) = ifn;
1732     }
1733 #ifdef ENABLE_CHECKING
1734     {
1735       struct cgraph_edge *e;
1736
1737       verify_cgraph_node (id.node);
1738
1739       /* Double check that we inlined everything we are supposed to inline.  */
1740       for (e = id.node->callees; e; e = e->next_callee)
1741         if (!e->inline_failed)
1742           abort ();
1743     }
1744 #endif
1745 }
1746
1747 /* FN is a function that has a complete body, and CLONE is a function
1748    whose body is to be set to a copy of FN, mapping argument
1749    declarations according to the ARG_MAP splay_tree.  */
1750
1751 void
1752 clone_body (tree clone, tree fn, void *arg_map)
1753 {
1754   inline_data id;
1755
1756   /* Clone the body, as if we were making an inline call.  But, remap
1757      the parameters in the callee to the parameters of caller.  If
1758      there's an in-charge parameter, map it to an appropriate
1759      constant.  */
1760   memset (&id, 0, sizeof (id));
1761   VARRAY_TREE_INIT (id.fns, 2, "fns");
1762   VARRAY_PUSH_TREE (id.fns, clone);
1763   VARRAY_PUSH_TREE (id.fns, fn);
1764   id.decl_map = (splay_tree)arg_map;
1765
1766   /* Cloning is treated slightly differently from inlining.  Set
1767      CLONING_P so that it's clear which operation we're performing.  */
1768   id.cloning_p = true;
1769
1770   /* Actually copy the body.  */
1771   TREE_CHAIN (DECL_SAVED_TREE (clone)) = copy_body (&id);
1772 }
1773
1774 /* Save duplicate of body in FN.  MAP is used to pass around splay tree
1775    used to update arguments in restore_body.  */
1776 tree
1777 save_body (tree fn, tree *arg_copy)
1778 {
1779   inline_data id;
1780   tree body, *parg;
1781
1782   memset (&id, 0, sizeof (id));
1783   VARRAY_TREE_INIT (id.fns, 1, "fns");
1784   VARRAY_PUSH_TREE (id.fns, fn);
1785   id.node = cgraph_node (fn);
1786   id.saving_p = true;
1787   id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
1788   *arg_copy = DECL_ARGUMENTS (fn);
1789   for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
1790     {
1791       tree new = copy_node (*parg);
1792       (*lang_hooks.dup_lang_specific_decl) (new);
1793       DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*parg);
1794       insert_decl_map (&id, *parg, new);
1795       TREE_CHAIN (new) = TREE_CHAIN (*parg);
1796       *parg = new;
1797     }
1798   insert_decl_map (&id, DECL_RESULT (fn), DECL_RESULT (fn));
1799
1800   /* Actually copy the body.  */
1801   body = copy_body (&id);
1802   if (lang_hooks.update_decl_after_saving)
1803     lang_hooks.update_decl_after_saving (fn, id.decl_map);
1804
1805   /* Clean up.  */
1806   splay_tree_delete (id.decl_map);
1807   return body;
1808 }
1809
1810 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1811    FUNC is called with the DATA and the address of each sub-tree.  If
1812    FUNC returns a non-NULL value, the traversal is aborted, and the
1813    value returned by FUNC is returned.  If HTAB is non-NULL it is used
1814    to record the nodes visited, and to avoid visiting a node more than
1815    once.  */
1816
1817 tree
1818 walk_tree (tree *tp, walk_tree_fn func, void *data, void *htab_)
1819 {
1820   htab_t htab = (htab_t) htab_;
1821   enum tree_code code;
1822   int walk_subtrees;
1823   tree result;
1824
1825 #define WALK_SUBTREE(NODE)                              \
1826   do                                                    \
1827     {                                                   \
1828       result = walk_tree (&(NODE), func, data, htab);   \
1829       if (result)                                       \
1830         return result;                                  \
1831     }                                                   \
1832   while (0)
1833
1834 #define WALK_SUBTREE_TAIL(NODE)                         \
1835   do                                                    \
1836     {                                                   \
1837        tp = & (NODE);                                   \
1838        goto tail_recurse;                               \
1839     }                                                   \
1840   while (0)
1841
1842  tail_recurse:
1843   /* Skip empty subtrees.  */
1844   if (!*tp)
1845     return NULL_TREE;
1846
1847   if (htab)
1848     {
1849       void **slot;
1850
1851       /* Don't walk the same tree twice, if the user has requested
1852          that we avoid doing so.  */
1853       slot = htab_find_slot (htab, *tp, INSERT);
1854       if (*slot)
1855         return NULL_TREE;
1856       *slot = *tp;
1857     }
1858
1859   /* Call the function.  */
1860   walk_subtrees = 1;
1861   result = (*func) (tp, &walk_subtrees, data);
1862
1863   /* If we found something, return it.  */
1864   if (result)
1865     return result;
1866
1867   code = TREE_CODE (*tp);
1868
1869 #ifndef INLINER_FOR_JAVA
1870   /* Even if we didn't, FUNC may have decided that there was nothing
1871      interesting below this point in the tree.  */
1872   if (!walk_subtrees)
1873     {
1874       if (STATEMENT_CODE_P (code) || code == TREE_LIST
1875           || lang_hooks.tree_inlining.tree_chain_matters_p (*tp))
1876         /* But we still need to check our siblings.  */
1877         WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1878       else
1879         return NULL_TREE;
1880     }
1881
1882   /* Handle common cases up front.  */
1883   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1884 #else /* INLINER_FOR_JAVA */
1885   if (code != EXIT_BLOCK_EXPR
1886       && code != SAVE_EXPR
1887       && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1888 #endif /* INLINER_FOR_JAVA */
1889     {
1890       int i, len;
1891
1892 #ifndef INLINER_FOR_JAVA
1893       /* Set lineno here so we get the right instantiation context
1894          if we call instantiate_decl from inlinable_function_p.  */
1895       if (STATEMENT_CODE_P (code) && !STMT_LINENO_FOR_FN_P (*tp))
1896         input_line = STMT_LINENO (*tp);
1897 #endif /* not INLINER_FOR_JAVA */
1898
1899       /* Walk over all the sub-trees of this operand.  */
1900       len = first_rtl_op (code);
1901       /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1902          But, we only want to walk once.  */
1903       if (code == TARGET_EXPR
1904           && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1))
1905         --len;
1906       /* Go through the subtrees.  We need to do this in forward order so
1907          that the scope of a FOR_EXPR is handled properly.  */
1908       for (i = 0; i < len; ++i)
1909         WALK_SUBTREE (TREE_OPERAND (*tp, i));
1910
1911 #ifndef INLINER_FOR_JAVA
1912       /* For statements, we also walk the chain so that we cover the
1913          entire statement tree.  */
1914       if (STATEMENT_CODE_P (code))
1915         {
1916           if (code == DECL_STMT
1917               && DECL_STMT_DECL (*tp)
1918               && DECL_P (DECL_STMT_DECL (*tp)))
1919             {
1920               /* Walk the DECL_INITIAL and DECL_SIZE.  We don't want to walk
1921                  into declarations that are just mentioned, rather than
1922                  declared; they don't really belong to this part of the tree.
1923                  And, we can see cycles: the initializer for a declaration can
1924                  refer to the declaration itself.  */
1925               WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp)));
1926               WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp)));
1927               WALK_SUBTREE (DECL_SIZE_UNIT (DECL_STMT_DECL (*tp)));
1928               WALK_SUBTREE (TREE_TYPE (*tp));
1929             }
1930
1931           /* This can be tail-recursion optimized if we write it this way.  */
1932           WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1933         }
1934
1935 #endif /* not INLINER_FOR_JAVA */
1936       /* We didn't find what we were looking for.  */
1937       return NULL_TREE;
1938     }
1939   else if (TREE_CODE_CLASS (code) == 'd')
1940     {
1941       WALK_SUBTREE_TAIL (TREE_TYPE (*tp));
1942     }
1943   else if (TREE_CODE_CLASS (code) == 't')
1944     {
1945       WALK_SUBTREE (TYPE_SIZE (*tp));
1946       WALK_SUBTREE (TYPE_SIZE_UNIT (*tp));
1947       /* Also examine various special fields, below.  */
1948     }
1949
1950   result = lang_hooks.tree_inlining.walk_subtrees (tp, &walk_subtrees, func,
1951                                                    data, htab);
1952   if (result || ! walk_subtrees)
1953     return result;
1954
1955   /* Not one of the easy cases.  We must explicitly go through the
1956      children.  */
1957   switch (code)
1958     {
1959     case ERROR_MARK:
1960     case IDENTIFIER_NODE:
1961     case INTEGER_CST:
1962     case REAL_CST:
1963     case VECTOR_CST:
1964     case STRING_CST:
1965     case REAL_TYPE:
1966     case COMPLEX_TYPE:
1967     case VECTOR_TYPE:
1968     case VOID_TYPE:
1969     case BOOLEAN_TYPE:
1970     case UNION_TYPE:
1971     case ENUMERAL_TYPE:
1972     case BLOCK:
1973     case RECORD_TYPE:
1974     case CHAR_TYPE:
1975     case PLACEHOLDER_EXPR:
1976       /* None of these have subtrees other than those already walked
1977          above.  */
1978       break;
1979
1980     case POINTER_TYPE:
1981     case REFERENCE_TYPE:
1982       WALK_SUBTREE_TAIL (TREE_TYPE (*tp));
1983       break;
1984
1985     case TREE_LIST:
1986       WALK_SUBTREE (TREE_VALUE (*tp));
1987       WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1988       break;
1989
1990     case TREE_VEC:
1991       {
1992         int len = TREE_VEC_LENGTH (*tp);
1993
1994         if (len == 0)
1995           break;
1996
1997         /* Walk all elements but the first.  */
1998         while (--len)
1999           WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
2000
2001         /* Now walk the first one as a tail call.  */
2002         WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
2003       }
2004
2005     case COMPLEX_CST:
2006       WALK_SUBTREE (TREE_REALPART (*tp));
2007       WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
2008
2009     case CONSTRUCTOR:
2010       WALK_SUBTREE_TAIL (CONSTRUCTOR_ELTS (*tp));
2011
2012     case METHOD_TYPE:
2013       WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp));
2014       /* Fall through.  */
2015
2016     case FUNCTION_TYPE:
2017       WALK_SUBTREE (TREE_TYPE (*tp));
2018       {
2019         tree arg = TYPE_ARG_TYPES (*tp);
2020
2021         /* We never want to walk into default arguments.  */
2022         for (; arg; arg = TREE_CHAIN (arg))
2023           WALK_SUBTREE (TREE_VALUE (arg));
2024       }
2025       break;
2026
2027     case ARRAY_TYPE:
2028       WALK_SUBTREE (TREE_TYPE (*tp));
2029       WALK_SUBTREE_TAIL (TYPE_DOMAIN (*tp));
2030
2031     case INTEGER_TYPE:
2032       WALK_SUBTREE (TYPE_MIN_VALUE (*tp));
2033       WALK_SUBTREE_TAIL (TYPE_MAX_VALUE (*tp));
2034
2035     case OFFSET_TYPE:
2036       WALK_SUBTREE (TREE_TYPE (*tp));
2037       WALK_SUBTREE_TAIL (TYPE_OFFSET_BASETYPE (*tp));
2038
2039 #ifdef INLINER_FOR_JAVA
2040     case EXIT_BLOCK_EXPR:
2041       WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 1));
2042
2043     case SAVE_EXPR:
2044       WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
2045 #endif /* INLINER_FOR_JAVA */
2046
2047     default:
2048       abort ();
2049     }
2050
2051   /* We didn't find what we were looking for.  */
2052   return NULL_TREE;
2053
2054 #undef WALK_SUBTREE
2055 #undef WALK_SUBTREE_TAIL
2056 }
2057
2058 /* Like walk_tree, but does not walk duplicate nodes more than
2059    once.  */
2060
2061 tree
2062 walk_tree_without_duplicates (tree *tp, walk_tree_fn func, void *data)
2063 {
2064   tree result;
2065   htab_t htab;
2066
2067   htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
2068   result = walk_tree (tp, func, data, htab);
2069   htab_delete (htab);
2070   return result;
2071 }
2072
2073 /* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
2074
2075 tree
2076 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2077 {
2078   enum tree_code code = TREE_CODE (*tp);
2079
2080   /* We make copies of most nodes.  */
2081   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
2082       || TREE_CODE_CLASS (code) == 'c'
2083       || code == TREE_LIST
2084       || code == TREE_VEC
2085       || lang_hooks.tree_inlining.tree_chain_matters_p (*tp))
2086     {
2087       /* Because the chain gets clobbered when we make a copy, we save it
2088          here.  */
2089       tree chain = TREE_CHAIN (*tp);
2090
2091       /* Copy the node.  */
2092       *tp = copy_node (*tp);
2093
2094       /* Now, restore the chain, if appropriate.  That will cause
2095          walk_tree to walk into the chain as well.  */
2096       if (code == PARM_DECL || code == TREE_LIST
2097 #ifndef INLINER_FOR_JAVA
2098           || lang_hooks.tree_inlining.tree_chain_matters_p (*tp)
2099           || STATEMENT_CODE_P (code))
2100         TREE_CHAIN (*tp) = chain;
2101
2102       /* For now, we don't update BLOCKs when we make copies.  So, we
2103          have to nullify all scope-statements.  */
2104       if (TREE_CODE (*tp) == SCOPE_STMT)
2105         SCOPE_STMT_BLOCK (*tp) = NULL_TREE;
2106 #else /* INLINER_FOR_JAVA */
2107           || lang_hooks.tree_inlining.tree_chain_matters_p (*tp))
2108         TREE_CHAIN (*tp) = chain;
2109 #endif /* INLINER_FOR_JAVA */
2110     }
2111   else if (TREE_CODE_CLASS (code) == 't')
2112     *walk_subtrees = 0;
2113
2114   return NULL_TREE;
2115 }
2116
2117 /* The SAVE_EXPR pointed to by TP is being copied.  If ST contains
2118    information indicating to what new SAVE_EXPR this one should be
2119    mapped, use that one.  Otherwise, create a new node and enter it in
2120    ST.  FN is the function into which the copy will be placed.  */
2121
2122 void
2123 remap_save_expr (tree *tp, void *st_, tree fn, int *walk_subtrees)
2124 {
2125   splay_tree st = (splay_tree) st_;
2126   splay_tree_node n;
2127   tree t;
2128
2129   /* See if we already encountered this SAVE_EXPR.  */
2130   n = splay_tree_lookup (st, (splay_tree_key) *tp);
2131
2132   /* If we didn't already remap this SAVE_EXPR, do so now.  */
2133   if (!n)
2134     {
2135       t = copy_node (*tp);
2136
2137       /* The SAVE_EXPR is now part of the function into which we
2138          are inlining this body.  */
2139       SAVE_EXPR_CONTEXT (t) = fn;
2140       /* And we haven't evaluated it yet.  */
2141       SAVE_EXPR_RTL (t) = NULL_RTX;
2142       /* Remember this SAVE_EXPR.  */
2143       splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2144       /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
2145       splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2146     }
2147   else
2148     {
2149       /* We've already walked into this SAVE_EXPR; don't do it again.  */
2150       *walk_subtrees = 0;
2151       t = (tree) n->value;
2152     }
2153
2154   /* Replace this SAVE_EXPR with the copy.  */
2155   *tp = t;
2156 }
2157
2158 #ifdef INLINER_FOR_JAVA
2159 /* Add STMT to EXISTING if possible, otherwise create a new
2160    COMPOUND_EXPR and add STMT to it.  */
2161
2162 static tree
2163 add_stmt_to_compound (tree existing, tree type, tree stmt)
2164 {
2165   if (!stmt)
2166     return existing;
2167   else if (existing)
2168     return build (COMPOUND_EXPR, type, existing, stmt);
2169   else
2170     return stmt;
2171 }
2172
2173 #endif /* INLINER_FOR_JAVA */