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