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