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