OSDN Git Service

* pt.c (convert_template_argument): Revert this change:
[pf3gnuchains/gcc-fork.git] / gcc / tree-inline.c
1 /* Control and data flow functions for trees.
2    Copyright 2001, 2002 Free Software Foundation, Inc.
3    Contributed by Alexandre Oliva <aoliva@redhat.com>
4
5 This file is part of GNU CC.
6
7 GNU CC 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 GNU CC 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 GNU CC; 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 "toplev.h"
25 #include "tree.h"
26 #include "tree-inline.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "insn-config.h"
33 #include "integrate.h"
34 #include "varray.h"
35 #include "hashtab.h"
36 #include "splay-tree.h"
37 #include "langhooks.h"
38
39 /* This should be eventually be generalized to other languages, but
40    this would require a shared function-as-trees infrastructure.  */
41 #ifndef INLINER_FOR_JAVA
42 #include "c-common.h"
43 #else /* INLINER_FOR_JAVA */
44 #include "parse.h"
45 #include "java-tree.h"
46 #endif /* INLINER_FOR_JAVA */
47
48 /* 0 if we should not perform inlining.
49    1 if we should expand functions calls inline at the tree level.
50    2 if we should consider *all* functions to be inline
51    candidates.  */
52
53 int flag_inline_trees = 0;
54
55 /* To Do:
56
57    o In order to make inlining-on-trees work, we pessimized
58      function-local static constants.  In particular, they are now
59      always output, even when not addressed.  Fix this by treating
60      function-local static constants just like global static
61      constants; the back-end already knows not to output them if they
62      are not needed.
63
64    o Provide heuristics to clamp inlining of recursive template
65      calls?  */
66
67 /* Data required for function inlining.  */
68
69 typedef struct inline_data
70 {
71   /* A stack of the functions we are inlining.  For example, if we are
72      compiling `f', which calls `g', which calls `h', and we are
73      inlining the body of `h', the stack will contain, `h', followed
74      by `g', followed by `f'.  The first few elements of the stack may
75      contain other functions that we know we should not recurse into,
76      even though they are not directly being inlined.  */
77   varray_type fns;
78   /* The index of the first element of FNS that really represents an
79      inlined function.  */
80   unsigned first_inlined_fn;
81   /* The label to jump to when a return statement is encountered.  If
82      this value is NULL, then return statements will simply be
83      remapped as return statements, rather than as jumps.  */
84   tree ret_label;
85   /* The map from local declarations in the inlined function to
86      equivalents in the function into which it is being inlined.  */
87   splay_tree decl_map;
88   /* Nonzero if we are currently within the cleanup for a
89      TARGET_EXPR.  */
90   int in_target_cleanup_p;
91   /* A stack of the TARGET_EXPRs that we are currently processing.  */
92   varray_type target_exprs;
93   /* A list of the functions current function has inlined.  */
94   varray_type inlined_fns;
95   /* The approximate number of statements we have inlined in the
96      current call stack.  */
97   int inlined_stmts;
98   /* We use the same mechanism to build clones that we do to perform
99      inlining.  However, there are a few places where we need to
100      distinguish between those two situations.  This flag is true if
101      we are cloning, rather than inlining.  */
102   bool cloning_p;
103   /* Hash table used to prevent walk_tree from visiting the same node
104      umpteen million times.  */
105   htab_t tree_pruner;
106 } inline_data;
107
108 /* Prototypes.  */
109
110 static tree declare_return_variable PARAMS ((inline_data *, tree *));
111 static tree copy_body_r PARAMS ((tree *, int *, void *));
112 static tree copy_body PARAMS ((inline_data *));
113 static tree expand_call_inline PARAMS ((tree *, int *, void *));
114 static void expand_calls_inline PARAMS ((tree *, inline_data *));
115 static int inlinable_function_p PARAMS ((tree, inline_data *));
116 static tree remap_decl PARAMS ((tree, inline_data *));
117 #ifndef INLINER_FOR_JAVA
118 static tree initialize_inlined_parameters PARAMS ((inline_data *, tree, tree));
119 static void remap_block PARAMS ((tree, tree, inline_data *));
120 static void copy_scope_stmt PARAMS ((tree *, int *, inline_data *));
121 #else /* INLINER_FOR_JAVA */
122 static tree initialize_inlined_parameters PARAMS ((inline_data *, tree, tree, tree));
123 static void remap_block PARAMS ((tree *, tree, inline_data *));
124 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
125 #endif /* INLINER_FOR_JAVA */
126
127 /* The approximate number of instructions per statement.  This number
128    need not be particularly accurate; it is used only to make
129    decisions about when a function is too big to inline.  */
130 #define INSNS_PER_STMT (10)
131
132 /* Remap DECL during the copying of the BLOCK tree for the function.  */
133
134 static tree
135 remap_decl (decl, id)
136      tree decl;
137      inline_data *id;
138 {
139   splay_tree_node n;
140   tree fn;
141
142   /* We only remap local variables in the current function.  */
143   fn = VARRAY_TOP_TREE (id->fns);
144   if (! (*lang_hooks.tree_inlining.auto_var_in_fn_p) (decl, fn))
145     return NULL_TREE;
146
147   /* See if we have remapped this declaration.  */
148   n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
149   /* If we didn't already have an equivalent for this declaration,
150      create one now.  */
151   if (!n)
152     {
153       tree t;
154
155       /* Make a copy of the variable or label.  */
156       t = copy_decl_for_inlining (decl, fn,
157                                   VARRAY_TREE (id->fns, 0));
158
159       /* The decl T could be a dynamic array or other variable size type,
160          in which case some fields need to be remapped because they may
161          contain SAVE_EXPRs.  */
162       if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
163           && TYPE_DOMAIN (TREE_TYPE (t)))
164         {
165           TREE_TYPE (t) = copy_node (TREE_TYPE (t));
166           TYPE_DOMAIN (TREE_TYPE (t))
167             = copy_node (TYPE_DOMAIN (TREE_TYPE (t)));
168           walk_tree (&TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))),
169                      copy_body_r, id, NULL);
170         }
171
172 #ifndef INLINER_FOR_JAVA
173       if (! DECL_NAME (t) && TREE_TYPE (t)
174           && (*lang_hooks.tree_inlining.anon_aggr_type_p) (TREE_TYPE (t)))
175         {
176           /* For a VAR_DECL of anonymous type, we must also copy the
177              member VAR_DECLS here and rechain the
178              DECL_ANON_UNION_ELEMS.  */
179           tree members = NULL;
180           tree src;
181
182           for (src = DECL_ANON_UNION_ELEMS (t); src;
183                src = TREE_CHAIN (src))
184             {
185               tree member = remap_decl (TREE_VALUE (src), id);
186
187               if (TREE_PURPOSE (src))
188                 abort ();
189               members = tree_cons (NULL, member, members);
190             }
191           DECL_ANON_UNION_ELEMS (t) = nreverse (members);
192         }
193 #endif /* not INLINER_FOR_JAVA */
194
195       /* Remember it, so that if we encounter this local entity
196          again we can reuse this copy.  */
197       n = splay_tree_insert (id->decl_map,
198                              (splay_tree_key) decl,
199                              (splay_tree_value) t);
200     }
201
202   return (tree) n->value;
203 }
204
205 #ifndef INLINER_FOR_JAVA
206 /* Copy the SCOPE_STMT_BLOCK associated with SCOPE_STMT to contain
207    remapped versions of the variables therein.  And hook the new block
208    into the block-tree.  If non-NULL, the DECLS are declarations to
209    add to use instead of the BLOCK_VARS in the old block.  */
210 #else /* INLINER_FOR_JAVA */
211 /* Copy the BLOCK to contain remapped versions of the variables
212    therein.  And hook the new block into the block-tree.  */
213 #endif /* INLINER_FOR_JAVA */
214
215 static void
216 #ifndef INLINER_FOR_JAVA
217 remap_block (scope_stmt, decls, id)
218      tree scope_stmt;
219 #else /* INLINER_FOR_JAVA */
220 remap_block (block, decls, id)
221      tree *block;
222 #endif /* INLINER_FOR_JAVA */
223      tree decls;
224      inline_data *id;
225 {
226 #ifndef INLINER_FOR_JAVA
227   /* We cannot do this in the cleanup for a TARGET_EXPR since we do
228      not know whether or not expand_expr will actually write out the
229      code we put there.  If it does not, then we'll have more BLOCKs
230      than block-notes, and things will go awry.  At some point, we
231      should make the back-end handle BLOCK notes in a tidier way,
232      without requiring a strict correspondence to the block-tree; then
233      this check can go.  */
234   if (id->in_target_cleanup_p)
235     {
236       SCOPE_STMT_BLOCK (scope_stmt) = NULL_TREE;
237       return;
238     }
239
240   /* If this is the beginning of a scope, remap the associated BLOCK.  */
241   if (SCOPE_BEGIN_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
242     {
243       tree old_block;
244       tree new_block;
245       tree old_var;
246       tree fn;
247
248       /* Make the new block.  */
249       old_block = SCOPE_STMT_BLOCK (scope_stmt);
250       new_block = make_node (BLOCK);
251       TREE_USED (new_block) = TREE_USED (old_block);
252       BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
253       SCOPE_STMT_BLOCK (scope_stmt) = new_block;
254
255       /* Remap its variables.  */
256       for (old_var = decls ? decls : BLOCK_VARS (old_block);
257            old_var;
258            old_var = TREE_CHAIN (old_var))
259         {
260           tree new_var;
261
262           /* Remap the variable.  */
263           new_var = remap_decl (old_var, id);
264           /* If we didn't remap this variable, so we can't mess with
265              its TREE_CHAIN.  If we remapped this variable to
266              something other than a declaration (say, if we mapped it
267              to a constant), then we must similarly omit any mention
268              of it here.  */
269           if (!new_var || !DECL_P (new_var))
270             ;
271           else
272             {
273               TREE_CHAIN (new_var) = BLOCK_VARS (new_block);
274               BLOCK_VARS (new_block) = new_var;
275             }
276         }
277       /* We put the BLOCK_VARS in reverse order; fix that now.  */
278       BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block));
279       fn = VARRAY_TREE (id->fns, 0);
280       if (id->cloning_p)
281         /* We're building a clone; DECL_INITIAL is still
282            error_mark_node, and current_binding_level is the parm
283            binding level.  */
284         (*lang_hooks.decls.insert_block) (new_block);
285       else
286         {
287           /* Attach this new block after the DECL_INITIAL block for the
288              function into which this block is being inlined.  In
289              rest_of_compilation we will straighten out the BLOCK tree.  */
290           tree *first_block;
291           if (DECL_INITIAL (fn))
292             first_block = &BLOCK_CHAIN (DECL_INITIAL (fn));
293           else
294             first_block = &DECL_INITIAL (fn);
295           BLOCK_CHAIN (new_block) = *first_block;
296           *first_block = new_block;
297         }
298       /* Remember the remapped block.  */
299       splay_tree_insert (id->decl_map,
300                          (splay_tree_key) old_block,
301                          (splay_tree_value) new_block);
302     }
303   /* If this is the end of a scope, set the SCOPE_STMT_BLOCK to be the
304      remapped block.  */
305   else if (SCOPE_END_P (scope_stmt) && SCOPE_STMT_BLOCK (scope_stmt))
306     {
307       splay_tree_node n;
308
309       /* Find this block in the table of remapped things.  */
310       n = splay_tree_lookup (id->decl_map,
311                              (splay_tree_key) SCOPE_STMT_BLOCK (scope_stmt));
312       if (! n)
313         abort ();
314       SCOPE_STMT_BLOCK (scope_stmt) = (tree) n->value;
315     }
316 #else /* INLINER_FOR_JAVA */
317   tree old_block;
318   tree new_block;
319   tree old_var;
320   tree fn;
321
322   /* Make the new block.  */
323   old_block = *block;
324   new_block = make_node (BLOCK);
325   TREE_USED (new_block) = TREE_USED (old_block);
326   BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
327   BLOCK_SUBBLOCKS (new_block) = BLOCK_SUBBLOCKS (old_block);
328   TREE_SIDE_EFFECTS (new_block) = TREE_SIDE_EFFECTS (old_block);
329   TREE_TYPE (new_block) = TREE_TYPE (old_block);
330   *block = new_block;
331
332   /* Remap its variables.  */
333   for (old_var = decls ? decls : BLOCK_VARS (old_block);
334        old_var;
335        old_var = TREE_CHAIN (old_var))
336     {
337       tree new_var;
338
339       /* All local class initialization flags go in the outermost
340          scope.  */
341       if (LOCAL_CLASS_INITIALIZATION_FLAG_P (old_var))
342         {
343           /* We may already have one.  */
344           if (! splay_tree_lookup (id->decl_map, (splay_tree_key) old_var))
345             {
346               tree outermost_block;
347               new_var = remap_decl (old_var, id);
348               DECL_ABSTRACT_ORIGIN (new_var) = NULL;
349               outermost_block = DECL_SAVED_TREE (current_function_decl);
350               TREE_CHAIN (new_var) = BLOCK_VARS (outermost_block);
351               BLOCK_VARS (outermost_block) = new_var;
352             }
353           continue;
354         }
355
356       /* Remap the variable.  */
357       new_var = remap_decl (old_var, id);
358       /* If we didn't remap this variable, so we can't mess with
359          its TREE_CHAIN.  If we remapped this variable to
360          something other than a declaration (say, if we mapped it
361          to a constant), then we must similarly omit any mention
362          of it here.  */
363       if (!new_var || !DECL_P (new_var))
364         ;
365       else
366         {
367           TREE_CHAIN (new_var) = BLOCK_VARS (new_block);
368           BLOCK_VARS (new_block) = new_var;
369         }
370     }
371   /* We put the BLOCK_VARS in reverse order; fix that now.  */
372   BLOCK_VARS (new_block) = nreverse (BLOCK_VARS (new_block));
373   fn = VARRAY_TREE (id->fns, 0);
374   /* Remember the remapped block.  */
375   splay_tree_insert (id->decl_map,
376                      (splay_tree_key) old_block,
377                      (splay_tree_value) new_block);
378 #endif /* INLINER_FOR_JAVA */
379 }
380
381 #ifndef INLINER_FOR_JAVA
382 /* Copy the SCOPE_STMT pointed to by TP.  */
383
384 static void
385 copy_scope_stmt (tp, walk_subtrees, id)
386      tree *tp;
387      int *walk_subtrees;
388      inline_data *id;
389 {
390   tree block;
391
392   /* Remember whether or not this statement was nullified.  When
393      making a copy, copy_tree_r always sets SCOPE_NULLIFIED_P (and
394      doesn't copy the SCOPE_STMT_BLOCK) to free callers from having to
395      deal with copying BLOCKs if they do not wish to do so.  */
396   block = SCOPE_STMT_BLOCK (*tp);
397   /* Copy (and replace) the statement.  */
398   copy_tree_r (tp, walk_subtrees, NULL);
399   /* Restore the SCOPE_STMT_BLOCK.  */
400   SCOPE_STMT_BLOCK (*tp) = block;
401
402   /* Remap the associated block.  */
403   remap_block (*tp, NULL_TREE, id);
404 }
405 #endif /* not INLINER_FOR_JAVA */
406
407 /* Called from copy_body via walk_tree.  DATA is really an
408    `inline_data *'.  */
409 static tree
410 copy_body_r (tp, walk_subtrees, data)
411      tree *tp;
412      int *walk_subtrees;
413      void *data;
414 {
415   inline_data* id;
416   tree fn;
417
418   /* Set up.  */
419   id = (inline_data *) data;
420   fn = VARRAY_TOP_TREE (id->fns);
421
422 #if 0
423   /* All automatic variables should have a DECL_CONTEXT indicating
424      what function they come from.  */
425   if ((TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == LABEL_DECL)
426       && DECL_NAMESPACE_SCOPE_P (*tp))
427     if (! DECL_EXTERNAL (*tp) && ! TREE_STATIC (*tp))
428       abort ();
429 #endif
430
431 #ifdef INLINER_FOR_JAVA
432   if (TREE_CODE (*tp) == BLOCK)
433     remap_block (tp, NULL_TREE, id);
434 #endif
435
436   /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
437      GOTO_STMT with the RET_LABEL as its target.  */
438 #ifndef INLINER_FOR_JAVA
439   if (TREE_CODE (*tp) == RETURN_STMT && id->ret_label)
440 #else /* INLINER_FOR_JAVA */
441   if (TREE_CODE (*tp) == RETURN_EXPR && id->ret_label)
442 #endif /* INLINER_FOR_JAVA */
443     {
444       tree return_stmt = *tp;
445       tree goto_stmt;
446
447       /* Build the GOTO_STMT.  */
448 #ifndef INLINER_FOR_JAVA
449       goto_stmt = build_stmt (GOTO_STMT, id->ret_label);
450       TREE_CHAIN (goto_stmt) = TREE_CHAIN (return_stmt);
451       GOTO_FAKE_P (goto_stmt) = 1;
452 #else /* INLINER_FOR_JAVA */
453       tree assignment = TREE_OPERAND (return_stmt, 0);
454       goto_stmt = build1 (GOTO_EXPR, void_type_node, id->ret_label);
455       TREE_SIDE_EFFECTS (goto_stmt) = 1;
456 #endif /* INLINER_FOR_JAVA */
457
458       /* If we're returning something, just turn that into an
459          assignment into the equivalent of the original
460          RESULT_DECL.  */
461 #ifndef INLINER_FOR_JAVA
462       if (RETURN_STMT_EXPR (return_stmt))
463         {
464           *tp = build_stmt (EXPR_STMT,
465                             RETURN_STMT_EXPR (return_stmt));
466           STMT_IS_FULL_EXPR_P (*tp) = 1;
467           /* And then jump to the end of the function.  */
468           TREE_CHAIN (*tp) = goto_stmt;
469         }
470 #else /* INLINER_FOR_JAVA */
471       if (assignment)
472         {
473           copy_body_r (&assignment, walk_subtrees, data);
474           *tp = build (COMPOUND_EXPR, void_type_node, assignment, goto_stmt);
475           TREE_SIDE_EFFECTS (*tp) = 1;      
476         }
477 #endif /* INLINER_FOR_JAVA */
478       /* If we're not returning anything just do the jump.  */
479       else
480         *tp = goto_stmt;
481     }
482   /* Local variables and labels need to be replaced by equivalent
483      variables.  We don't want to copy static variables; there's only
484      one of those, no matter how many times we inline the containing
485      function.  */
486   else if ((*lang_hooks.tree_inlining.auto_var_in_fn_p) (*tp, fn))
487     {
488       tree new_decl;
489
490       /* Remap the declaration.  */
491       new_decl = remap_decl (*tp, id);
492       if (! new_decl)
493         abort ();
494       /* Replace this variable with the copy.  */
495       STRIP_TYPE_NOPS (new_decl);
496       *tp = new_decl;
497     }
498 #if 0
499   else if (nonstatic_local_decl_p (*tp)
500            && DECL_CONTEXT (*tp) != VARRAY_TREE (id->fns, 0))
501     abort ();
502 #endif
503   else if (TREE_CODE (*tp) == SAVE_EXPR)
504     remap_save_expr (tp, id->decl_map, VARRAY_TREE (id->fns, 0),
505                      walk_subtrees);
506   else if (TREE_CODE (*tp) == UNSAVE_EXPR)
507     /* UNSAVE_EXPRs should not be generated until expansion time.  */
508     abort ();
509 #ifndef INLINER_FOR_JAVA
510   /* For a SCOPE_STMT, we must copy the associated block so that we
511      can write out debugging information for the inlined variables.  */
512   else if (TREE_CODE (*tp) == SCOPE_STMT && !id->in_target_cleanup_p)
513     copy_scope_stmt (tp, walk_subtrees, id);
514 #else /* INLINER_FOR_JAVA */
515   else if (TREE_CODE (*tp) == LABELED_BLOCK_EXPR)
516     {
517       /* We need a new copy of this labeled block; the EXIT_BLOCK_EXPR
518          will refer to it, so save a copy ready for remapping.  We
519          save it in the decl_map, although it isn't a decl.  */
520       tree new_block = copy_node (*tp);
521       splay_tree_insert (id->decl_map,
522                          (splay_tree_key) *tp,
523                          (splay_tree_value) new_block);
524       *tp = new_block;
525     }
526   else if (TREE_CODE (*tp) == EXIT_BLOCK_EXPR)
527     {
528       splay_tree_node n 
529         = splay_tree_lookup (id->decl_map, 
530                              (splay_tree_key) TREE_OPERAND (*tp, 0));
531       /* We _must_ have seen the enclosing LABELED_BLOCK_EXPR.  */
532       if (! n)
533         abort ();
534       *tp = copy_node (*tp);
535       TREE_OPERAND (*tp, 0) = (tree) n->value;
536     }
537 #endif /* INLINER_FOR_JAVA */
538   /* Otherwise, just copy the node.  Note that copy_tree_r already
539      knows not to copy VAR_DECLs, etc., so this is safe.  */
540   else
541     {
542       copy_tree_r (tp, walk_subtrees, NULL);
543
544       /* The copied TARGET_EXPR has never been expanded, even if the
545          original node was expanded already.  */
546       if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
547         {
548           TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
549           TREE_OPERAND (*tp, 3) = NULL_TREE;
550         }
551       else if (TREE_CODE (*tp) == MODIFY_EXPR
552                && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
553                && ((*lang_hooks.tree_inlining.auto_var_in_fn_p)
554                    (TREE_OPERAND (*tp, 0), fn)))
555         {
556           /* Some assignments VAR = VAR; don't generate any rtl code
557              and thus don't count as variable modification.  Avoid
558              keeping bogosities like 0 = 0.  */
559           tree decl = TREE_OPERAND (*tp, 0), value;
560           splay_tree_node n;
561
562           n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
563           if (n)
564             {
565               value = (tree) n->value;
566               STRIP_TYPE_NOPS (value);
567               if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
568                 *tp = value;
569             }
570         }
571     }
572
573   /* Keep iterating.  */
574   return NULL_TREE;
575 }
576
577 /* Make a copy of the body of FN so that it can be inserted inline in
578    another function.  */
579
580 static tree
581 copy_body (id)
582      inline_data *id;
583 {
584   tree body;
585
586   body = DECL_SAVED_TREE (VARRAY_TOP_TREE (id->fns));
587   walk_tree (&body, copy_body_r, id, NULL);
588
589   return body;
590 }
591
592 /* Generate code to initialize the parameters of the function at the
593    top of the stack in ID from the ARGS (presented as a TREE_LIST).  */
594
595 static tree
596 #ifndef INLINER_FOR_JAVA
597 initialize_inlined_parameters (id, args, fn)
598 #else /* INLINER_FOR_JAVA */
599 initialize_inlined_parameters (id, args, fn, block)
600 #endif /* INLINER_FOR_JAVA */
601      inline_data *id;
602      tree args;
603      tree fn;
604 #ifdef INLINER_FOR_JAVA
605      tree block;
606 #endif /* INLINER_FOR_JAVA */
607 {
608   tree init_stmts;
609   tree parms;
610   tree a;
611   tree p;
612 #ifdef INLINER_FOR_JAVA
613   tree vars = NULL_TREE;
614 #endif /* INLINER_FOR_JAVA */
615
616   /* Figure out what the parameters are.  */
617   parms = DECL_ARGUMENTS (fn);
618
619   /* Start with no initializations whatsoever.  */
620   init_stmts = NULL_TREE;
621
622   /* Loop through the parameter declarations, replacing each with an
623      equivalent VAR_DECL, appropriately initialized.  */
624   for (p = parms, a = args; p;
625        a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
626     {
627 #ifndef INLINER_FOR_JAVA
628       tree init_stmt;
629       tree cleanup;
630 #endif /* not INLINER_FOR_JAVA */
631       tree var;
632       tree value;
633
634       /* Find the initializer.  */
635       value = (*lang_hooks.tree_inlining.convert_parm_for_inlining)
636               (p, a ? TREE_VALUE (a) : NULL_TREE, fn);
637
638       /* If the parameter is never assigned to, we may not need to
639          create a new variable here at all.  Instead, we may be able
640          to just use the argument value.  */
641       if (TREE_READONLY (p)
642           && !TREE_ADDRESSABLE (p)
643           && value && !TREE_SIDE_EFFECTS (value))
644         {
645           /* Simplify the value, if possible.  */
646           value = fold (DECL_P (value) ? decl_constant_value (value) : value);
647
648           /* We can't risk substituting complex expressions.  They
649              might contain variables that will be assigned to later.
650              Theoretically, we could check the expression to see if
651              all of the variables that determine its value are
652              read-only, but we don't bother.  */
653           if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
654             {
655               /* If this is a declaration, wrap it a NOP_EXPR so that
656                  we don't try to put the VALUE on the list of
657                  BLOCK_VARS.  */
658               if (DECL_P (value))
659                 value = build1 (NOP_EXPR, TREE_TYPE (value), value);
660
661               splay_tree_insert (id->decl_map,
662                                  (splay_tree_key) p,
663                                  (splay_tree_value) value);
664               continue;
665             }
666         }
667
668       /* Make an equivalent VAR_DECL.  */
669       var = copy_decl_for_inlining (p, fn, VARRAY_TREE (id->fns, 0));
670       /* Register the VAR_DECL as the equivalent for the PARM_DECL;
671          that way, when the PARM_DECL is encountered, it will be
672          automatically replaced by the VAR_DECL.  */
673       splay_tree_insert (id->decl_map,
674                          (splay_tree_key) p,
675                          (splay_tree_value) var);
676
677       /* Declare this new variable.  */
678 #ifndef INLINER_FOR_JAVA
679       init_stmt = build_stmt (DECL_STMT, var);
680       TREE_CHAIN (init_stmt) = init_stmts;
681       init_stmts = init_stmt;
682 #else /* INLINER_FOR_JAVA */
683       TREE_CHAIN (var) = vars;
684       vars = var;
685 #endif /* INLINER_FOR_JAVA */
686
687       /* Initialize this VAR_DECL from the equivalent argument.  If
688          the argument is an object, created via a constructor or copy,
689          this will not result in an extra copy: the TARGET_EXPR
690          representing the argument will be bound to VAR, and the
691          object will be constructed in VAR.  */
692       if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
693 #ifndef INLINER_FOR_JAVA
694         DECL_INITIAL (var) = value;
695       else
696         {
697           /* Even if P was TREE_READONLY, the new VAR should not be.
698              In the original code, we would have constructed a
699              temporary, and then the function body would have never
700              changed the value of P.  However, now, we will be
701              constructing VAR directly.  The constructor body may
702              change its value multiple times as it is being
703              constructed.  Therefore, it must not be TREE_READONLY;
704              the back-end assumes that TREE_READONLY variable is
705              assigned to only once.  */
706           TREE_READONLY (var) = 0;
707
708           /* Build a run-time initialization.  */
709           init_stmt = build_stmt (EXPR_STMT,
710                                   build (INIT_EXPR, TREE_TYPE (p),
711                                          var, value));
712           /* Add this initialization to the list.  Note that we want the
713              declaration *after* the initialization because we are going
714              to reverse all the initialization statements below.  */
715           TREE_CHAIN (init_stmt) = init_stmts;
716           init_stmts = init_stmt;
717         }
718
719       /* See if we need to clean up the declaration.  */
720       cleanup = (*lang_hooks.maybe_build_cleanup) (var);
721       if (cleanup)
722         {
723           tree cleanup_stmt;
724           /* Build the cleanup statement.  */
725           cleanup_stmt = build_stmt (CLEANUP_STMT, var, cleanup);
726           /* Add it to the *front* of the list; the list will be
727              reversed below.  */
728           TREE_CHAIN (cleanup_stmt) = init_stmts;
729           init_stmts = cleanup_stmt;
730         }
731 #else /* INLINER_FOR_JAVA */
732         {
733           tree assignment = build (MODIFY_EXPR, TREE_TYPE (p), var, value);
734           init_stmts = add_stmt_to_compound (init_stmts, TREE_TYPE (p), 
735                                              assignment);
736         }
737       else
738         {
739           /* Java objects don't ever need constructing when being
740              passed as arguments because only call by reference is
741              supported.  */
742           abort ();
743         }
744 #endif /* INLINER_FOR_JAVA */
745     }
746
747 #ifndef INLINER_FOR_JAVA
748   /* Evaluate trailing arguments.  */
749   for (; a; a = TREE_CHAIN (a))
750     {
751       tree init_stmt;
752       tree value = TREE_VALUE (a);
753
754       if (! value || ! TREE_SIDE_EFFECTS (value))
755         continue;
756
757       init_stmt = build_stmt (EXPR_STMT, value);
758       TREE_CHAIN (init_stmt) = init_stmts;
759       init_stmts = init_stmt;
760     }
761
762   /* The initialization statements have been built up in reverse
763      order.  Straighten them out now.  */
764   return nreverse (init_stmts);
765 #else /* INLINER_FOR_JAVA */
766   BLOCK_VARS (block) = nreverse (vars);
767   return init_stmts;
768 #endif /* INLINER_FOR_JAVA */
769 }
770
771 /* Declare a return variable to replace the RESULT_DECL for the
772    function we are calling.  An appropriate DECL_STMT is returned.
773    The USE_STMT is filled in to contain a use of the declaration to
774    indicate the return value of the function.  */
775
776 #ifndef INLINER_FOR_JAVA
777 static tree
778 declare_return_variable (id, use_stmt)
779      struct inline_data *id;
780      tree *use_stmt;
781 #else /* INLINER_FOR_JAVA */
782 static tree
783 declare_return_variable (id, var)
784      struct inline_data *id;
785      tree *var;
786 #endif /* INLINER_FOR_JAVA */
787 {
788   tree fn = VARRAY_TOP_TREE (id->fns);
789   tree result = DECL_RESULT (fn);
790 #ifndef INLINER_FOR_JAVA
791   tree var;
792 #endif /* not INLINER_FOR_JAVA */
793   int need_return_decl = 1;
794
795   /* We don't need to do anything for functions that don't return
796      anything.  */
797   if (!result || VOID_TYPE_P (TREE_TYPE (result)))
798     {
799 #ifndef INLINER_FOR_JAVA
800       *use_stmt = NULL_TREE;
801 #else /* INLINER_FOR_JAVA */
802       *var = NULL_TREE;
803 #endif /* INLINER_FOR_JAVA */
804       return NULL_TREE;
805     }
806
807 #ifndef INLINER_FOR_JAVA
808   var = ((*lang_hooks.tree_inlining.copy_res_decl_for_inlining)
809          (result, fn, VARRAY_TREE (id->fns, 0), id->decl_map,
810           &need_return_decl, &id->target_exprs));
811
812   /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
813      way, when the RESULT_DECL is encountered, it will be
814      automatically replaced by the VAR_DECL.  */
815   splay_tree_insert (id->decl_map,
816                      (splay_tree_key) result,
817                      (splay_tree_value) var);
818
819   /* Build the USE_STMT.  If the return type of the function was
820      promoted, convert it back to the expected type.  */
821   if (TREE_TYPE (var) == TREE_TYPE (TREE_TYPE (fn)))
822     *use_stmt = build_stmt (EXPR_STMT, var);
823   else
824     *use_stmt = build_stmt (EXPR_STMT,
825                             build1 (NOP_EXPR, TREE_TYPE (TREE_TYPE (fn)),
826                                     var));
827   TREE_ADDRESSABLE (*use_stmt) = 1;
828
829   /* Build the declaration statement if FN does not return an
830      aggregate.  */
831   if (need_return_decl)
832     return build_stmt (DECL_STMT, var);
833 #else /* INLINER_FOR_JAVA */
834   *var = ((*lang_hooks.tree_inlining.copy_res_decl_for_inlining)
835          (result, fn, VARRAY_TREE (id->fns, 0), id->decl_map,
836           &need_return_decl, NULL_TREE));
837
838   splay_tree_insert (id->decl_map,
839                      (splay_tree_key) result,
840                      (splay_tree_value) *var);
841   DECL_IGNORED_P (*var) = 1;
842   if (need_return_decl)
843     return *var;
844 #endif /* INLINER_FOR_JAVA */
845   /* If FN does return an aggregate, there's no need to declare the
846      return variable; we're using a variable in our caller's frame.  */
847   else
848     return NULL_TREE;
849 }
850
851 /* Returns nonzero if a function can be inlined as a tree.  */
852
853 int
854 tree_inlinable_function_p (fn)
855      tree fn;
856 {
857   return inlinable_function_p (fn, NULL);
858 }
859
860 /* Returns nonzero if FN is a function that can be inlined into the
861    inlining context ID_.  If ID_ is NULL, check whether the function
862    can be inlined at all.  */
863
864 static int
865 inlinable_function_p (fn, id)
866      tree fn;
867      inline_data *id;
868 {
869   int inlinable;
870   int currfn_insns;
871
872   /* If we've already decided this function shouldn't be inlined,
873      there's no need to check again.  */
874   if (DECL_UNINLINABLE (fn))
875     return 0;
876
877   /* Assume it is not inlinable.  */
878   inlinable = 0;
879
880   /* The number of instructions (estimated) of current function.  */
881   currfn_insns = DECL_NUM_STMTS (fn) * INSNS_PER_STMT;
882
883   /* If we're not inlining things, then nothing is inlinable.  */
884   if (! flag_inline_trees)
885     ;
886   /* If we're not inlining all functions and the function was not
887      declared `inline', we don't inline it.  Don't think of
888      disregarding DECL_INLINE when flag_inline_trees == 2; it's the
889      front-end that must set DECL_INLINE in this case, because
890      dwarf2out loses if a function is inlined that doesn't have
891      DECL_INLINE set.  */
892   else if (! DECL_INLINE (fn))
893     ;
894   /* We can't inline functions that are too big.  Only allow a single
895      function to be of MAX_INLINE_INSNS_SINGLE size.  Make special
896      allowance for extern inline functions, though.  */
897   else if (! (*lang_hooks.tree_inlining.disregard_inline_limits) (fn)
898            && currfn_insns > MAX_INLINE_INSNS_SINGLE)
899     ;
900   /* All is well.  We can inline this function.  Traditionally, GCC
901      has refused to inline functions using alloca, or functions whose
902      values are returned in a PARALLEL, and a few other such obscure
903      conditions.  We are not equally constrained at the tree level.  */
904   else
905     inlinable = 1;
906
907   /* Squirrel away the result so that we don't have to check again.  */
908   DECL_UNINLINABLE (fn) = ! inlinable;
909
910   /* In case we don't disregard the inlining limits and we basically
911      can inline this function, investigate further.  */
912   if (! (*lang_hooks.tree_inlining.disregard_inline_limits) (fn)
913       && inlinable)
914     {
915       int sum_insns = (id ? id->inlined_stmts : 0) * INSNS_PER_STMT
916                      + currfn_insns;
917       /* In the extreme case that we have exceeded the recursive inlining
918          limit by a huge factor (128), we just say no. Should not happen
919          in real life.  */
920       if (sum_insns > MAX_INLINE_INSNS * 128)
921          inlinable = 0;
922       /* If we did not hit the extreme limit, we use a linear function
923          with slope -1/MAX_INLINE_SLOPE to exceedingly decrease the
924          allowable size. We always allow a size of MIN_INLINE_INSNS
925          though.  */
926       else if ((sum_insns > MAX_INLINE_INSNS)
927                && (currfn_insns > MIN_INLINE_INSNS))
928         {
929           int max_curr = MAX_INLINE_INSNS_SINGLE
930                         - (sum_insns - MAX_INLINE_INSNS) / MAX_INLINE_SLOPE;
931           if (currfn_insns > max_curr)
932             inlinable = 0;
933         }
934     }
935
936   if (inlinable && (*lang_hooks.tree_inlining.cannot_inline_tree_fn) (&fn))
937     inlinable = 0;
938
939   /* If we don't have the function body available, we can't inline
940      it.  */
941   if (! DECL_SAVED_TREE (fn))
942     inlinable = 0;
943
944   /* Check again, language hooks may have modified it.  */
945   if (! inlinable || DECL_UNINLINABLE (fn))
946     return 0;
947
948   /* Don't do recursive inlining, either.  We don't record this in
949      DECL_UNINLINABLE; we may be able to inline this function later.  */
950   if (id)
951     {
952       size_t i;
953
954       for (i = 0; i < VARRAY_ACTIVE_SIZE (id->fns); ++i)
955         if (VARRAY_TREE (id->fns, i) == fn)
956           return 0;
957
958       if (DECL_INLINED_FNS (fn))
959         {
960           int j;
961           tree inlined_fns = DECL_INLINED_FNS (fn);
962
963           for (j = 0; j < TREE_VEC_LENGTH (inlined_fns); ++j)
964             if (TREE_VEC_ELT (inlined_fns, j) == VARRAY_TREE (id->fns, 0))
965               return 0;
966         }
967     }
968
969   /* Return the result.  */
970   return inlinable;
971 }
972
973 /* If *TP is a CALL_EXPR, replace it with its inline expansion.  */
974
975 static tree
976 expand_call_inline (tp, walk_subtrees, data)
977      tree *tp;
978      int *walk_subtrees;
979      void *data;
980 {
981   inline_data *id;
982   tree t;
983   tree expr;
984   tree stmt;
985 #ifndef INLINER_FOR_JAVA
986   tree chain;
987   tree scope_stmt;
988   tree use_stmt;
989 #else /* INLINER_FOR_JAVA */
990   tree retvar;
991 #endif /* INLINER_FOR_JAVA */
992   tree fn;
993   tree arg_inits;
994   tree *inlined_body;
995   splay_tree st;
996
997   /* See what we've got.  */
998   id = (inline_data *) data;
999   t = *tp;
1000
1001   /* Recurse, but letting recursive invocations know that we are
1002      inside the body of a TARGET_EXPR.  */
1003   if (TREE_CODE (*tp) == TARGET_EXPR)
1004     {
1005 #ifndef INLINER_FOR_JAVA
1006       int i, len = first_rtl_op (TARGET_EXPR);
1007
1008       /* We're walking our own subtrees.  */
1009       *walk_subtrees = 0;
1010
1011       /* Push *TP on the stack of pending TARGET_EXPRs.  */
1012       VARRAY_PUSH_TREE (id->target_exprs, *tp);
1013
1014       /* Actually walk over them.  This loop is the body of
1015          walk_trees, omitting the case where the TARGET_EXPR
1016          itself is handled.  */
1017       for (i = 0; i < len; ++i)
1018         {
1019           if (i == 2)
1020             ++id->in_target_cleanup_p;
1021           walk_tree (&TREE_OPERAND (*tp, i), expand_call_inline, data,
1022                      id->tree_pruner);
1023           if (i == 2)
1024             --id->in_target_cleanup_p;
1025         }
1026
1027       /* We're done with this TARGET_EXPR now.  */
1028       VARRAY_POP (id->target_exprs);
1029
1030       return NULL_TREE;
1031 #else /* INLINER_FOR_JAVA */
1032       abort ();
1033 #endif /* INLINER_FOR_JAVA */
1034     }
1035
1036   if (TYPE_P (t))
1037     /* Because types were not copied in copy_body, CALL_EXPRs beneath
1038        them should not be expanded.  This can happen if the type is a
1039        dynamic array type, for example.  */
1040     *walk_subtrees = 0;
1041
1042   /* From here on, we're only interested in CALL_EXPRs.  */
1043   if (TREE_CODE (t) != CALL_EXPR)
1044     return NULL_TREE;
1045
1046   /* First, see if we can figure out what function is being called.
1047      If we cannot, then there is no hope of inlining the function.  */
1048   fn = get_callee_fndecl (t);
1049   if (!fn)
1050     return NULL_TREE;
1051
1052   /* If fn is a declaration of a function in a nested scope that was
1053      globally declared inline, we don't set its DECL_INITIAL.
1054      However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1055      C++ front-end uses it for cdtors to refer to their internal
1056      declarations, that are not real functions.  Fortunately those
1057      don't have trees to be saved, so we can tell by checking their
1058      DECL_SAVED_TREE.  */
1059   if (! DECL_INITIAL (fn)
1060       && DECL_ABSTRACT_ORIGIN (fn)
1061       && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1062     fn = DECL_ABSTRACT_ORIGIN (fn);
1063
1064   /* Don't try to inline functions that are not well-suited to
1065      inlining.  */
1066   if (!inlinable_function_p (fn, id))
1067     return NULL_TREE;
1068
1069   if (! (*lang_hooks.tree_inlining.start_inlining) (fn))
1070     return NULL_TREE;
1071
1072   /* Set the current filename and line number to the function we are
1073      inlining so that when we create new _STMT nodes here they get
1074      line numbers corresponding to the function we are calling.  We
1075      wrap the whole inlined body in an EXPR_WITH_FILE_AND_LINE as well
1076      because individual statements don't record the filename.  */
1077   push_srcloc (DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
1078
1079 #ifndef INLINER_FOR_JAVA
1080   /* Build a statement-expression containing code to initialize the
1081      arguments, the actual inline expansion of the body, and a label
1082      for the return statements within the function to jump to.  The
1083      type of the statement expression is the return type of the
1084      function call.  */
1085   expr = build1 (STMT_EXPR, TREE_TYPE (TREE_TYPE (fn)), make_node (COMPOUND_STMT));
1086   /* There is no scope associated with the statement-expression.  */
1087   STMT_EXPR_NO_SCOPE (expr) = 1;
1088   stmt = STMT_EXPR_STMT (expr);
1089 #else /* INLINER_FOR_JAVA */
1090   /* Build a block containing code to initialize the arguments, the
1091      actual inline expansion of the body, and a label for the return
1092      statements within the function to jump to.  The type of the
1093      statement expression is the return type of the function call.  */
1094   stmt = NULL;
1095   expr = build (BLOCK, TREE_TYPE (TREE_TYPE (fn)), stmt);
1096 #endif /* INLINER_FOR_JAVA */
1097
1098   /* Local declarations will be replaced by their equivalents in this
1099      map.  */
1100   st = id->decl_map;
1101   id->decl_map = splay_tree_new (splay_tree_compare_pointers,
1102                                  NULL, NULL);
1103
1104   /* Initialize the parameters.  */
1105 #ifndef INLINER_FOR_JAVA
1106   arg_inits = initialize_inlined_parameters (id, TREE_OPERAND (t, 1), fn);
1107   /* Expand any inlined calls in the initializers.  Do this before we
1108      push FN on the stack of functions we are inlining; we want to
1109      inline calls to FN that appear in the initializers for the
1110      parameters.  */
1111   expand_calls_inline (&arg_inits, id);
1112   /* And add them to the tree.  */
1113   COMPOUND_BODY (stmt) = chainon (COMPOUND_BODY (stmt), arg_inits);
1114 #else /* INLINER_FOR_JAVA */
1115   arg_inits = initialize_inlined_parameters (id, TREE_OPERAND (t, 1), fn, expr);
1116   if (arg_inits)
1117     {
1118       /* Expand any inlined calls in the initializers.  Do this before we
1119          push FN on the stack of functions we are inlining; we want to
1120          inline calls to FN that appear in the initializers for the
1121          parameters.  */
1122       expand_calls_inline (&arg_inits, id);
1123       
1124       /* And add them to the tree.  */
1125       BLOCK_EXPR_BODY (expr) = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), 
1126                                                      TREE_TYPE (arg_inits), 
1127                                                      arg_inits);
1128     }
1129 #endif /* INLINER_FOR_JAVA */
1130
1131   /* Record the function we are about to inline so that we can avoid
1132      recursing into it.  */
1133   VARRAY_PUSH_TREE (id->fns, fn);
1134
1135   /* Record the function we are about to inline if optimize_function
1136      has not been called on it yet and we don't have it in the list.  */
1137   if (! DECL_INLINED_FNS (fn))
1138     {
1139       int i;
1140
1141       for (i = VARRAY_ACTIVE_SIZE (id->inlined_fns) - 1; i >= 0; i--)
1142         if (VARRAY_TREE (id->inlined_fns, i) == fn)
1143           break;
1144       if (i < 0)
1145         VARRAY_PUSH_TREE (id->inlined_fns, fn);
1146     }
1147
1148   /* Return statements in the function body will be replaced by jumps
1149      to the RET_LABEL.  */
1150   id->ret_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
1151   DECL_CONTEXT (id->ret_label) = VARRAY_TREE (id->fns, 0);
1152
1153   if (! DECL_INITIAL (fn)
1154       || TREE_CODE (DECL_INITIAL (fn)) != BLOCK)
1155     abort ();
1156
1157 #ifndef INLINER_FOR_JAVA
1158   /* Create a block to put the parameters in.  We have to do this
1159      after the parameters have been remapped because remapping
1160      parameters is different from remapping ordinary variables.  */
1161   scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
1162   SCOPE_BEGIN_P (scope_stmt) = 1;
1163   SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
1164   remap_block (scope_stmt, DECL_ARGUMENTS (fn), id);
1165   TREE_CHAIN (scope_stmt) = COMPOUND_BODY (stmt);
1166   COMPOUND_BODY (stmt) = scope_stmt;
1167
1168   /* Tell the debugging backends that this block represents the
1169      outermost scope of the inlined function.  */
1170   if (SCOPE_STMT_BLOCK (scope_stmt))
1171     BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope_stmt)) = DECL_ORIGIN (fn);
1172
1173   /* Declare the return variable for the function.  */
1174   COMPOUND_BODY (stmt)
1175     = chainon (COMPOUND_BODY (stmt),
1176                declare_return_variable (id, &use_stmt));
1177 #else /* INLINER_FOR_JAVA */
1178   {
1179     /* Declare the return variable for the function.  */
1180     tree decl = declare_return_variable (id, &retvar);
1181     if (retvar)
1182       {
1183         tree *next = &BLOCK_VARS (expr);
1184         while (*next)
1185           next = &TREE_CHAIN (*next);   
1186         *next = decl;
1187       }
1188   }
1189 #endif /* INLINER_FOR_JAVA */
1190
1191   /* After we've initialized the parameters, we insert the body of the
1192      function itself.  */
1193 #ifndef INLINER_FOR_JAVA
1194   inlined_body = &COMPOUND_BODY (stmt);
1195   while (*inlined_body)
1196     inlined_body = &TREE_CHAIN (*inlined_body);
1197   *inlined_body = copy_body (id);
1198 #else /* INLINER_FOR_JAVA */
1199   {
1200     tree new_body;
1201     java_inlining_map_static_initializers (fn, id->decl_map);
1202     new_body = copy_body (id);
1203     TREE_TYPE (new_body) = TREE_TYPE (TREE_TYPE (fn));
1204     BLOCK_EXPR_BODY (expr)
1205       = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), 
1206                               TREE_TYPE (new_body), new_body);
1207     inlined_body = &BLOCK_EXPR_BODY (expr);
1208   }
1209 #endif /* INLINER_FOR_JAVA */
1210
1211   /* After the body of the function comes the RET_LABEL.  This must come
1212      before we evaluate the returned value below, because that evalulation
1213      may cause RTL to be generated.  */
1214 #ifndef INLINER_FOR_JAVA
1215   COMPOUND_BODY (stmt)
1216     = chainon (COMPOUND_BODY (stmt),
1217                build_stmt (LABEL_STMT, id->ret_label));
1218 #else /* INLINER_FOR_JAVA */
1219   {
1220     tree label = build1 (LABEL_EXPR, void_type_node, id->ret_label);
1221     BLOCK_EXPR_BODY (expr)
1222       = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), void_type_node, label);
1223     TREE_SIDE_EFFECTS (label) = TREE_SIDE_EFFECTS (t);
1224   }
1225 #endif /* INLINER_FOR_JAVA */
1226
1227   /* Finally, mention the returned value so that the value of the
1228      statement-expression is the returned value of the function.  */
1229 #ifndef INLINER_FOR_JAVA
1230   COMPOUND_BODY (stmt) = chainon (COMPOUND_BODY (stmt), use_stmt);
1231   
1232   /* Close the block for the parameters.  */
1233   scope_stmt = build_stmt (SCOPE_STMT, DECL_INITIAL (fn));
1234   SCOPE_NO_CLEANUPS_P (scope_stmt) = 1;
1235   remap_block (scope_stmt, NULL_TREE, id);
1236   COMPOUND_BODY (stmt)
1237     = chainon (COMPOUND_BODY (stmt), scope_stmt);
1238 #else /* INLINER_FOR_JAVA */
1239   if (retvar)
1240     {
1241       /* Mention the retvar.  If the return type of the function was
1242          promoted, convert it back to the expected type.  */
1243       if (TREE_TYPE (TREE_TYPE (fn)) != TREE_TYPE (retvar))
1244         retvar = build1 (NOP_EXPR, TREE_TYPE (TREE_TYPE (fn)), retvar);
1245       BLOCK_EXPR_BODY (expr) 
1246         = add_stmt_to_compound (BLOCK_EXPR_BODY (expr), 
1247                                 TREE_TYPE (retvar), retvar);
1248     }
1249   
1250   java_inlining_merge_static_initializers (fn, id->decl_map);
1251 #endif /* INLINER_FOR_JAVA */
1252
1253   /* Clean up.  */
1254   splay_tree_delete (id->decl_map);
1255   id->decl_map = st;
1256
1257   /* The new expression has side-effects if the old one did.  */
1258   TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (t);
1259
1260   /* Replace the call by the inlined body.  Wrap it in an
1261      EXPR_WITH_FILE_LOCATION so that we'll get debugging line notes
1262      pointing to the right place.  */
1263 #ifndef INLINER_FOR_JAVA
1264   chain = TREE_CHAIN (*tp);
1265   *tp = build_expr_wfl (expr, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn),
1266                         /*col=*/0);
1267 #else /* INLINER_FOR_JAVA */
1268   *tp = build_expr_wfl (expr, DECL_SOURCE_FILE (fn), 
1269                         DECL_SOURCE_LINE_FIRST(fn),
1270                         /*col=*/0);
1271 #endif /* INLINER_FOR_JAVA */
1272   EXPR_WFL_EMIT_LINE_NOTE (*tp) = 1;
1273 #ifndef INLINER_FOR_JAVA
1274   TREE_CHAIN (*tp) = chain;
1275 #endif /* not INLINER_FOR_JAVA */
1276   pop_srcloc ();
1277
1278   /* If the value of the new expression is ignored, that's OK.  We
1279      don't warn about this for CALL_EXPRs, so we shouldn't warn about
1280      the equivalent inlined version either.  */
1281   TREE_USED (*tp) = 1;
1282
1283   /* Our function now has more statements than it did before.  */
1284   DECL_NUM_STMTS (VARRAY_TREE (id->fns, 0)) += DECL_NUM_STMTS (fn);
1285   /* For accounting, subtract one for the saved call/ret.  */
1286   id->inlined_stmts += DECL_NUM_STMTS (fn) - 1;
1287
1288   /* Recurse into the body of the just inlined function.  */
1289   expand_calls_inline (inlined_body, id);
1290   VARRAY_POP (id->fns);
1291
1292   /* If we've returned to the top level, clear out the record of how
1293      much inlining has been done.  */
1294   if (VARRAY_ACTIVE_SIZE (id->fns) == id->first_inlined_fn)
1295     id->inlined_stmts = 0;
1296
1297   /* Don't walk into subtrees.  We've already handled them above.  */
1298   *walk_subtrees = 0;
1299
1300   (*lang_hooks.tree_inlining.end_inlining) (fn);
1301
1302   /* Keep iterating.  */
1303   return NULL_TREE;
1304 }
1305 /* Walk over the entire tree *TP, replacing CALL_EXPRs with inline
1306    expansions as appropriate.  */
1307
1308 static void
1309 expand_calls_inline (tp, id)
1310      tree *tp;
1311      inline_data *id;
1312 {
1313   /* Search through *TP, replacing all calls to inline functions by
1314      appropriate equivalents.  Use walk_tree in no-duplicates mode
1315      to avoid exponential time complexity.  (We can't just use
1316      walk_tree_without_duplicates, because of the special TARGET_EXPR
1317      handling in expand_calls.  The hash table is set up in
1318      optimize_function.  */
1319   walk_tree (tp, expand_call_inline, id, id->tree_pruner);
1320 }
1321
1322 /* Expand calls to inline functions in the body of FN.  */
1323
1324 void
1325 optimize_inline_calls (fn)
1326      tree fn;
1327 {
1328   inline_data id;
1329   tree prev_fn;
1330
1331   /* Clear out ID.  */
1332   memset (&id, 0, sizeof (id));
1333
1334   /* Don't allow recursion into FN.  */
1335   VARRAY_TREE_INIT (id.fns, 32, "fns");
1336   VARRAY_PUSH_TREE (id.fns, fn);
1337   /* Or any functions that aren't finished yet.  */
1338   prev_fn = NULL_TREE;
1339   if (current_function_decl)
1340     {
1341       VARRAY_PUSH_TREE (id.fns, current_function_decl);
1342       prev_fn = current_function_decl;
1343     }
1344
1345   prev_fn = ((*lang_hooks.tree_inlining.add_pending_fn_decls)
1346              (&id.fns, prev_fn));
1347
1348   /* Create the stack of TARGET_EXPRs.  */
1349   VARRAY_TREE_INIT (id.target_exprs, 32, "target_exprs");
1350
1351   /* Create the list of functions this call will inline.  */
1352   VARRAY_TREE_INIT (id.inlined_fns, 32, "inlined_fns");
1353
1354   /* Keep track of the low-water mark, i.e., the point where the first
1355      real inlining is represented in ID.FNS.  */
1356   id.first_inlined_fn = VARRAY_ACTIVE_SIZE (id.fns);
1357
1358   /* Replace all calls to inline functions with the bodies of those
1359      functions.  */
1360   id.tree_pruner = htab_create (37, htab_hash_pointer,
1361                                 htab_eq_pointer, NULL);
1362   expand_calls_inline (&DECL_SAVED_TREE (fn), &id);
1363
1364   /* Clean up.  */
1365   htab_delete (id.tree_pruner);
1366   if (DECL_LANG_SPECIFIC (fn))
1367     {
1368       tree ifn = make_tree_vec (VARRAY_ACTIVE_SIZE (id.inlined_fns));
1369
1370       memcpy (&TREE_VEC_ELT (ifn, 0), &VARRAY_TREE (id.inlined_fns, 0),
1371               VARRAY_ACTIVE_SIZE (id.inlined_fns) * sizeof (tree));
1372       DECL_INLINED_FNS (fn) = ifn;
1373     }
1374 }
1375
1376 /* FN is a function that has a complete body, and CLONE is a function
1377    whose body is to be set to a copy of FN, mapping argument
1378    declarations according to the ARG_MAP splay_tree.  */
1379
1380 void
1381 clone_body (clone, fn, arg_map)
1382      tree clone, fn;
1383      void *arg_map;
1384 {
1385   inline_data id;
1386
1387   /* Clone the body, as if we were making an inline call.  But, remap
1388      the parameters in the callee to the parameters of caller.  If
1389      there's an in-charge parameter, map it to an appropriate
1390      constant.  */
1391   memset (&id, 0, sizeof (id));
1392   VARRAY_TREE_INIT (id.fns, 2, "fns");
1393   VARRAY_PUSH_TREE (id.fns, clone);
1394   VARRAY_PUSH_TREE (id.fns, fn);
1395   id.decl_map = (splay_tree)arg_map;
1396
1397   /* Cloning is treated slightly differently from inlining.  Set
1398      CLONING_P so that it's clear which operation we're performing.  */
1399   id.cloning_p = true;
1400
1401   /* Actually copy the body.  */
1402   TREE_CHAIN (DECL_SAVED_TREE (clone)) = copy_body (&id);
1403 }
1404
1405 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1406    FUNC is called with the DATA and the address of each sub-tree.  If
1407    FUNC returns a non-NULL value, the traversal is aborted, and the
1408    value returned by FUNC is returned.  If HTAB is non-NULL it is used
1409    to record the nodes visited, and to avoid visiting a node more than
1410    once.  */
1411
1412 tree
1413 walk_tree (tp, func, data, htab_)
1414      tree *tp;
1415      walk_tree_fn func;
1416      void *data;
1417      void *htab_;
1418 {
1419   htab_t htab = (htab_t) htab_;
1420   enum tree_code code;
1421   int walk_subtrees;
1422   tree result;
1423
1424 #define WALK_SUBTREE(NODE)                              \
1425   do                                                    \
1426     {                                                   \
1427       result = walk_tree (&(NODE), func, data, htab);   \
1428       if (result)                                       \
1429         return result;                                  \
1430     }                                                   \
1431   while (0)
1432
1433 #define WALK_SUBTREE_TAIL(NODE)                         \
1434   do                                                    \
1435     {                                                   \
1436        tp = & (NODE);                                   \
1437        goto tail_recurse;                               \
1438     }                                                   \
1439   while (0)
1440
1441  tail_recurse:
1442   /* Skip empty subtrees.  */
1443   if (!*tp)
1444     return NULL_TREE;
1445
1446   if (htab)
1447     {
1448       void **slot;
1449
1450       /* Don't walk the same tree twice, if the user has requested
1451          that we avoid doing so.  */
1452       if (htab_find (htab, *tp))
1453         return NULL_TREE;
1454       /* If we haven't already seen this node, add it to the table.  */
1455       slot = htab_find_slot (htab, *tp, INSERT);
1456       *slot = *tp;
1457     }
1458
1459   /* Call the function.  */
1460   walk_subtrees = 1;
1461   result = (*func) (tp, &walk_subtrees, data);
1462
1463   /* If we found something, return it.  */
1464   if (result)
1465     return result;
1466
1467   code = TREE_CODE (*tp);
1468
1469 #ifndef INLINER_FOR_JAVA
1470   /* Even if we didn't, FUNC may have decided that there was nothing
1471      interesting below this point in the tree.  */
1472   if (!walk_subtrees)
1473     {
1474       if (statement_code_p (code) || code == TREE_LIST
1475           || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp))
1476         /* But we still need to check our siblings.  */
1477         WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1478       else
1479         return NULL_TREE;
1480     }
1481
1482   /* Handle common cases up front.  */
1483   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1484       || TREE_CODE_CLASS (code) == 'r'
1485       || TREE_CODE_CLASS (code) == 's')
1486 #else /* INLINER_FOR_JAVA */
1487   if (code != EXIT_BLOCK_EXPR
1488       && code != SAVE_EXPR
1489       && (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1490           || TREE_CODE_CLASS (code) == 'r'
1491           || TREE_CODE_CLASS (code) == 's'))
1492 #endif /* INLINER_FOR_JAVA */
1493     {
1494       int i, len;
1495
1496 #ifndef INLINER_FOR_JAVA
1497       /* Set lineno here so we get the right instantiation context
1498          if we call instantiate_decl from inlinable_function_p.  */
1499       if (statement_code_p (code) && !STMT_LINENO_FOR_FN_P (*tp))
1500         lineno = STMT_LINENO (*tp);
1501 #endif /* not INLINER_FOR_JAVA */
1502
1503       /* Walk over all the sub-trees of this operand.  */
1504       len = first_rtl_op (code);
1505       /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1506          But, we only want to walk once.  */
1507       if (code == TARGET_EXPR
1508           && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1))
1509         --len;
1510       /* Go through the subtrees.  We need to do this in forward order so
1511          that the scope of a FOR_EXPR is handled properly.  */
1512       for (i = 0; i < len; ++i)
1513         WALK_SUBTREE (TREE_OPERAND (*tp, i));
1514
1515 #ifndef INLINER_FOR_JAVA
1516       /* For statements, we also walk the chain so that we cover the
1517          entire statement tree.  */
1518       if (statement_code_p (code))
1519         {
1520           if (code == DECL_STMT
1521               && DECL_STMT_DECL (*tp)
1522               && DECL_P (DECL_STMT_DECL (*tp)))
1523             {
1524               /* Walk the DECL_INITIAL and DECL_SIZE.  We don't want to walk
1525                  into declarations that are just mentioned, rather than
1526                  declared; they don't really belong to this part of the tree.
1527                  And, we can see cycles: the initializer for a declaration can
1528                  refer to the declaration itself.  */
1529               WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp)));
1530               WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp)));
1531               WALK_SUBTREE (DECL_SIZE_UNIT (DECL_STMT_DECL (*tp)));
1532             }
1533
1534           /* This can be tail-recursion optimized if we write it this way.  */
1535           WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1536         }
1537
1538 #endif /* not INLINER_FOR_JAVA */
1539       /* We didn't find what we were looking for.  */
1540       return NULL_TREE;
1541     }
1542   else if (TREE_CODE_CLASS (code) == 'd')
1543     {
1544       WALK_SUBTREE_TAIL (TREE_TYPE (*tp));
1545     }
1546
1547   result = (*lang_hooks.tree_inlining.walk_subtrees) (tp, &walk_subtrees, func,
1548                                                       data, htab);
1549   if (result || ! walk_subtrees)
1550     return result;
1551
1552   /* Not one of the easy cases.  We must explicitly go through the
1553      children.  */
1554   switch (code)
1555     {
1556     case ERROR_MARK:
1557     case IDENTIFIER_NODE:
1558     case INTEGER_CST:
1559     case REAL_CST:
1560     case VECTOR_CST:
1561     case STRING_CST:
1562     case REAL_TYPE:
1563     case COMPLEX_TYPE:
1564     case VECTOR_TYPE:
1565     case VOID_TYPE:
1566     case BOOLEAN_TYPE:
1567     case UNION_TYPE:
1568     case ENUMERAL_TYPE:
1569     case BLOCK:
1570     case RECORD_TYPE:
1571       /* None of thse have subtrees other than those already walked
1572          above.  */
1573       break;
1574
1575     case POINTER_TYPE:
1576     case REFERENCE_TYPE:
1577       WALK_SUBTREE_TAIL (TREE_TYPE (*tp));
1578       break;
1579
1580     case TREE_LIST:
1581       WALK_SUBTREE (TREE_VALUE (*tp));
1582       WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
1583       break;
1584
1585     case TREE_VEC:
1586       {
1587         int len = TREE_VEC_LENGTH (*tp);
1588
1589         if (len == 0)
1590           break;
1591
1592         /* Walk all elements but the first.  */
1593         while (--len)
1594           WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
1595
1596         /* Now walk the first one as a tail call.  */
1597         WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
1598       }
1599
1600     case COMPLEX_CST:
1601       WALK_SUBTREE (TREE_REALPART (*tp));
1602       WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
1603
1604     case CONSTRUCTOR:
1605       WALK_SUBTREE_TAIL (CONSTRUCTOR_ELTS (*tp));
1606
1607     case METHOD_TYPE:
1608       WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp));
1609       /* Fall through.  */
1610
1611     case FUNCTION_TYPE:
1612       WALK_SUBTREE (TREE_TYPE (*tp));
1613       {
1614         tree arg = TYPE_ARG_TYPES (*tp);
1615
1616         /* We never want to walk into default arguments.  */
1617         for (; arg; arg = TREE_CHAIN (arg))
1618           WALK_SUBTREE (TREE_VALUE (arg));
1619       }
1620       break;
1621
1622     case ARRAY_TYPE:
1623       WALK_SUBTREE (TREE_TYPE (*tp));
1624       WALK_SUBTREE_TAIL (TYPE_DOMAIN (*tp));
1625
1626     case INTEGER_TYPE:
1627       WALK_SUBTREE (TYPE_MIN_VALUE (*tp));
1628       WALK_SUBTREE_TAIL (TYPE_MAX_VALUE (*tp));
1629
1630     case OFFSET_TYPE:
1631       WALK_SUBTREE (TREE_TYPE (*tp));
1632       WALK_SUBTREE_TAIL (TYPE_OFFSET_BASETYPE (*tp));
1633
1634 #ifdef INLINER_FOR_JAVA
1635     case EXIT_BLOCK_EXPR:
1636       WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 1));
1637
1638     case SAVE_EXPR:
1639       WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
1640 #endif /* INLINER_FOR_JAVA */
1641
1642     default:
1643       abort ();
1644     }
1645
1646   /* We didn't find what we were looking for.  */
1647   return NULL_TREE;
1648
1649 #undef WALK_SUBTREE
1650 }
1651
1652 /* Like walk_tree, but does not walk duplicate nodes more than
1653    once.  */
1654
1655 tree
1656 walk_tree_without_duplicates (tp, func, data)
1657      tree *tp;
1658      walk_tree_fn func;
1659      void *data;
1660 {
1661   tree result;
1662   htab_t htab;
1663
1664   htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1665   result = walk_tree (tp, func, data, htab);
1666   htab_delete (htab);
1667   return result;
1668 }
1669
1670 /* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
1671
1672 tree
1673 copy_tree_r (tp, walk_subtrees, data)
1674      tree *tp;
1675      int *walk_subtrees;
1676      void *data ATTRIBUTE_UNUSED;
1677 {
1678   enum tree_code code = TREE_CODE (*tp);
1679
1680   /* We make copies of most nodes.  */
1681   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1682       || TREE_CODE_CLASS (code) == 'r'
1683       || TREE_CODE_CLASS (code) == 'c'
1684       || TREE_CODE_CLASS (code) == 's'
1685       || code == TREE_LIST
1686       || code == TREE_VEC
1687       || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp))
1688     {
1689       /* Because the chain gets clobbered when we make a copy, we save it
1690          here.  */
1691       tree chain = TREE_CHAIN (*tp);
1692
1693       /* Copy the node.  */
1694       *tp = copy_node (*tp);
1695
1696       /* Now, restore the chain, if appropriate.  That will cause
1697          walk_tree to walk into the chain as well.  */
1698       if (code == PARM_DECL || code == TREE_LIST
1699 #ifndef INLINER_FOR_JAVA
1700           || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp)
1701           || statement_code_p (code))
1702         TREE_CHAIN (*tp) = chain;
1703
1704       /* For now, we don't update BLOCKs when we make copies.  So, we
1705          have to nullify all scope-statements.  */
1706       if (TREE_CODE (*tp) == SCOPE_STMT)
1707         SCOPE_STMT_BLOCK (*tp) = NULL_TREE;
1708 #else /* INLINER_FOR_JAVA */
1709           || (*lang_hooks.tree_inlining.tree_chain_matters_p) (*tp))
1710         TREE_CHAIN (*tp) = chain;
1711 #endif /* INLINER_FOR_JAVA */
1712     }
1713   else if (TREE_CODE_CLASS (code) == 't')
1714     /* There's no need to copy types, or anything beneath them.  */
1715     *walk_subtrees = 0;
1716
1717   return NULL_TREE;
1718 }
1719
1720 /* The SAVE_EXPR pointed to by TP is being copied.  If ST contains
1721    information indicating to what new SAVE_EXPR this one should be
1722    mapped, use that one.  Otherwise, create a new node and enter it in
1723    ST.  FN is the function into which the copy will be placed.  */
1724
1725 void
1726 remap_save_expr (tp, st_, fn, walk_subtrees)
1727      tree *tp;
1728      void *st_;
1729      tree fn;
1730      int *walk_subtrees;
1731 {
1732   splay_tree st = (splay_tree) st_;
1733   splay_tree_node n;
1734
1735   /* See if we already encountered this SAVE_EXPR.  */
1736   n = splay_tree_lookup (st, (splay_tree_key) *tp);
1737
1738   /* If we didn't already remap this SAVE_EXPR, do so now.  */
1739   if (!n)
1740     {
1741       tree t = copy_node (*tp);
1742
1743       /* The SAVE_EXPR is now part of the function into which we
1744          are inlining this body.  */
1745       SAVE_EXPR_CONTEXT (t) = fn;
1746       /* And we haven't evaluated it yet.  */
1747       SAVE_EXPR_RTL (t) = NULL_RTX;
1748       /* Remember this SAVE_EXPR.  */
1749       n = splay_tree_insert (st,
1750                              (splay_tree_key) *tp,
1751                              (splay_tree_value) t);
1752       /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
1753       splay_tree_insert (st, (splay_tree_key) t,
1754                          (splay_tree_value) error_mark_node);
1755     }
1756   else
1757     /* We've already walked into this SAVE_EXPR, so we needn't do it
1758        again.  */
1759     *walk_subtrees = 0;
1760
1761   /* Replace this SAVE_EXPR with the copy.  */
1762   *tp = (tree) n->value;
1763 }
1764
1765 #ifdef INLINER_FOR_JAVA
1766 /* Add STMT to EXISTING if possible, otherwise create a new
1767    COMPOUND_EXPR and add STMT to it.  */
1768
1769 static tree
1770 add_stmt_to_compound (existing, type, stmt)
1771      tree existing, type, stmt;
1772 {
1773   if (!stmt)
1774     return existing;
1775   else if (existing)
1776     return build (COMPOUND_EXPR, type, existing, stmt);
1777   else
1778     return stmt;
1779 }
1780
1781 #endif /* INLINER_FOR_JAVA */