1 /* Control and data flow functions for trees.
2 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
28 #include "tree-inline.h"
34 #include "insn-config.h"
35 #include "integrate.h"
38 #include "splay-tree.h"
39 #include "langhooks.h"
44 /* This should be eventually be generalized to other languages, but
45 this would require a shared function-as-trees infrastructure. */
46 #ifndef INLINER_FOR_JAVA
48 #else /* INLINER_FOR_JAVA */
50 #include "java-tree.h"
51 #endif /* INLINER_FOR_JAVA */
53 /* 0 if we should not perform inlining.
54 1 if we should expand functions calls inline at the tree level.
55 2 if we should consider *all* functions to be inline
58 int flag_inline_trees
= 0;
62 o In order to make inlining-on-trees work, we pessimized
63 function-local static constants. In particular, they are now
64 always output, even when not addressed. Fix this by treating
65 function-local static constants just like global static
66 constants; the back-end already knows not to output them if they
69 o Provide heuristics to clamp inlining of recursive template
72 /* Data required for function inlining. */
74 typedef struct inline_data
76 /* A stack of the functions we are inlining. For example, if we are
77 compiling `f', which calls `g', which calls `h', and we are
78 inlining the body of `h', the stack will contain, `h', followed
79 by `g', followed by `f'. The first few elements of the stack may
80 contain other functions that we know we should not recurse into,
81 even though they are not directly being inlined. */
83 /* The index of the first element of FNS that really represents an
85 unsigned first_inlined_fn
;
86 /* The label to jump to when a return statement is encountered. If
87 this value is NULL, then return statements will simply be
88 remapped as return statements, rather than as jumps. */
90 /* The map from local declarations in the inlined function to
91 equivalents in the function into which it is being inlined. */
93 /* Nonzero if we are currently within the cleanup for a
95 int in_target_cleanup_p
;
96 /* A list of the functions current function has inlined. */
97 varray_type inlined_fns
;
98 /* The approximate number of instructions we have inlined in the
99 current call stack. */
101 /* We use the same mechanism to build clones that we do to perform
102 inlining. However, there are a few places where we need to
103 distinguish between those two situations. This flag is true if
104 we are cloning, rather than inlining. */
106 /* Hash table used to prevent walk_tree from visiting the same node
107 umpteen million times. */
109 /* Decl of function we are inlining into. */
116 static tree
declare_return_variable (inline_data
*, tree
, tree
*);
117 static tree
copy_body_r (tree
*, int *, void *);
118 static tree
copy_body (inline_data
*);
119 static tree
expand_call_inline (tree
*, int *, void *);
120 static void expand_calls_inline (tree
*, inline_data
*);
121 static bool inlinable_function_p (tree
);
122 static int limits_allow_inlining (tree
, inline_data
*);
123 static tree
remap_decl (tree
, inline_data
*);
124 #ifndef INLINER_FOR_JAVA
125 static tree
initialize_inlined_parameters (inline_data
*, tree
, tree
);
126 static void remap_block (tree
, tree
, inline_data
*);
127 static void copy_scope_stmt (tree
*, int *, inline_data
*);
128 #else /* INLINER_FOR_JAVA */
129 static tree
initialize_inlined_parameters (inline_data
*, tree
, tree
, tree
);
130 static void remap_block (tree
*, tree
, inline_data
*);
131 static tree
add_stmt_to_compound (tree
, tree
, tree
);
132 #endif /* INLINER_FOR_JAVA */
134 /* Remap DECL during the copying of the BLOCK tree for the function. */
137 remap_decl (tree decl
, inline_data
*id
)
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
))
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,
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. */
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
))
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 */
216 #ifndef INLINER_FOR_JAVA
217 remap_block (tree scope_stmt
, tree decls
, inline_data
*id
)
218 #else /* INLINER_FOR_JAVA */
219 remap_block (tree
*block
, tree decls
, inline_data
*id
)
220 #endif /* INLINER_FOR_JAVA */
222 #ifndef INLINER_FOR_JAVA
223 /* We cannot do this in the cleanup for a TARGET_EXPR since we do
224 not know whether or not expand_expr will actually write out the
225 code we put there. If it does not, then we'll have more BLOCKs
226 than block-notes, and things will go awry. At some point, we
227 should make the back-end handle BLOCK notes in a tidier way,
228 without requiring a strict correspondence to the block-tree; then
229 this check can go. */
230 if (id
->in_target_cleanup_p
)
232 SCOPE_STMT_BLOCK (scope_stmt
) = NULL_TREE
;
236 /* If this is the beginning of a scope, remap the associated BLOCK. */
237 if (SCOPE_BEGIN_P (scope_stmt
) && SCOPE_STMT_BLOCK (scope_stmt
))
244 /* Make the new block. */
245 old_block
= SCOPE_STMT_BLOCK (scope_stmt
);
246 new_block
= make_node (BLOCK
);
247 TREE_USED (new_block
) = TREE_USED (old_block
);
248 BLOCK_ABSTRACT_ORIGIN (new_block
) = old_block
;
249 SCOPE_STMT_BLOCK (scope_stmt
) = new_block
;
251 /* Remap its variables. */
252 for (old_var
= decls
? decls
: BLOCK_VARS (old_block
);
254 old_var
= TREE_CHAIN (old_var
))
258 /* Remap the variable. */
259 new_var
= remap_decl (old_var
, id
);
260 /* If we didn't remap this variable, so we can't mess with
261 its TREE_CHAIN. If we remapped this variable to
262 something other than a declaration (say, if we mapped it
263 to a constant), then we must similarly omit any mention
265 if (!new_var
|| !DECL_P (new_var
))
269 TREE_CHAIN (new_var
) = BLOCK_VARS (new_block
);
270 BLOCK_VARS (new_block
) = new_var
;
273 /* We put the BLOCK_VARS in reverse order; fix that now. */
274 BLOCK_VARS (new_block
) = nreverse (BLOCK_VARS (new_block
));
275 fn
= VARRAY_TREE (id
->fns
, 0);
277 /* We're building a clone; DECL_INITIAL is still
278 error_mark_node, and current_binding_level is the parm
280 (*lang_hooks
.decls
.insert_block
) (new_block
);
283 /* Attach this new block after the DECL_INITIAL block for the
284 function into which this block is being inlined. In
285 rest_of_compilation we will straighten out the BLOCK tree. */
287 if (DECL_INITIAL (fn
))
288 first_block
= &BLOCK_CHAIN (DECL_INITIAL (fn
));
290 first_block
= &DECL_INITIAL (fn
);
291 BLOCK_CHAIN (new_block
) = *first_block
;
292 *first_block
= new_block
;
294 /* Remember the remapped block. */
295 splay_tree_insert (id
->decl_map
,
296 (splay_tree_key
) old_block
,
297 (splay_tree_value
) new_block
);
299 /* If this is the end of a scope, set the SCOPE_STMT_BLOCK to be the
301 else if (SCOPE_END_P (scope_stmt
) && SCOPE_STMT_BLOCK (scope_stmt
))
305 /* Find this block in the table of remapped things. */
306 n
= splay_tree_lookup (id
->decl_map
,
307 (splay_tree_key
) SCOPE_STMT_BLOCK (scope_stmt
));
310 SCOPE_STMT_BLOCK (scope_stmt
) = (tree
) n
->value
;
312 #else /* INLINER_FOR_JAVA */
318 /* Make the new block. */
320 new_block
= make_node (BLOCK
);
321 TREE_USED (new_block
) = TREE_USED (old_block
);
322 BLOCK_ABSTRACT_ORIGIN (new_block
) = old_block
;
323 BLOCK_SUBBLOCKS (new_block
) = BLOCK_SUBBLOCKS (old_block
);
324 TREE_SIDE_EFFECTS (new_block
) = TREE_SIDE_EFFECTS (old_block
);
325 TREE_TYPE (new_block
) = TREE_TYPE (old_block
);
328 /* Remap its variables. */
329 for (old_var
= decls
? decls
: BLOCK_VARS (old_block
);
331 old_var
= TREE_CHAIN (old_var
))
335 /* All local class initialization flags go in the outermost
337 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (old_var
))
339 /* We may already have one. */
340 if (! splay_tree_lookup (id
->decl_map
, (splay_tree_key
) old_var
))
342 tree outermost_block
;
343 new_var
= remap_decl (old_var
, id
);
344 DECL_ABSTRACT_ORIGIN (new_var
) = NULL
;
345 outermost_block
= DECL_SAVED_TREE (current_function_decl
);
346 TREE_CHAIN (new_var
) = BLOCK_VARS (outermost_block
);
347 BLOCK_VARS (outermost_block
) = new_var
;
352 /* Remap the variable. */
353 new_var
= remap_decl (old_var
, id
);
354 /* If we didn't remap this variable, so we can't mess with
355 its TREE_CHAIN. If we remapped this variable to
356 something other than a declaration (say, if we mapped it
357 to a constant), then we must similarly omit any mention
359 if (!new_var
|| !DECL_P (new_var
))
363 TREE_CHAIN (new_var
) = BLOCK_VARS (new_block
);
364 BLOCK_VARS (new_block
) = new_var
;
367 /* We put the BLOCK_VARS in reverse order; fix that now. */
368 BLOCK_VARS (new_block
) = nreverse (BLOCK_VARS (new_block
));
369 fn
= VARRAY_TREE (id
->fns
, 0);
370 /* Remember the remapped block. */
371 splay_tree_insert (id
->decl_map
,
372 (splay_tree_key
) old_block
,
373 (splay_tree_value
) new_block
);
374 #endif /* INLINER_FOR_JAVA */
377 #ifndef INLINER_FOR_JAVA
378 /* Copy the SCOPE_STMT pointed to by TP. */
381 copy_scope_stmt (tree
*tp
, int *walk_subtrees
, inline_data
*id
)
385 /* Remember whether or not this statement was nullified. When
386 making a copy, copy_tree_r always sets SCOPE_NULLIFIED_P (and
387 doesn't copy the SCOPE_STMT_BLOCK) to free callers from having to
388 deal with copying BLOCKs if they do not wish to do so. */
389 block
= SCOPE_STMT_BLOCK (*tp
);
390 /* Copy (and replace) the statement. */
391 copy_tree_r (tp
, walk_subtrees
, NULL
);
392 /* Restore the SCOPE_STMT_BLOCK. */
393 SCOPE_STMT_BLOCK (*tp
) = block
;
395 /* Remap the associated block. */
396 remap_block (*tp
, NULL_TREE
, id
);
398 #endif /* not INLINER_FOR_JAVA */
400 /* Called from copy_body via walk_tree. DATA is really an
403 copy_body_r (tree
*tp
, int *walk_subtrees
, void *data
)
409 id
= (inline_data
*) data
;
410 fn
= VARRAY_TOP_TREE (id
->fns
);
413 /* All automatic variables should have a DECL_CONTEXT indicating
414 what function they come from. */
415 if ((TREE_CODE (*tp
) == VAR_DECL
|| TREE_CODE (*tp
) == LABEL_DECL
)
416 && DECL_NAMESPACE_SCOPE_P (*tp
))
417 if (! DECL_EXTERNAL (*tp
) && ! TREE_STATIC (*tp
))
421 #ifdef INLINER_FOR_JAVA
422 if (TREE_CODE (*tp
) == BLOCK
)
423 remap_block (tp
, NULL_TREE
, id
);
426 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
427 GOTO_STMT with the RET_LABEL as its target. */
428 #ifndef INLINER_FOR_JAVA
429 if (TREE_CODE (*tp
) == RETURN_STMT
&& id
->ret_label
)
430 #else /* INLINER_FOR_JAVA */
431 if (TREE_CODE (*tp
) == RETURN_EXPR
&& id
->ret_label
)
432 #endif /* INLINER_FOR_JAVA */
434 tree return_stmt
= *tp
;
437 /* Build the GOTO_STMT. */
438 #ifndef INLINER_FOR_JAVA
439 goto_stmt
= build_stmt (GOTO_STMT
, id
->ret_label
);
440 TREE_CHAIN (goto_stmt
) = TREE_CHAIN (return_stmt
);
441 GOTO_FAKE_P (goto_stmt
) = 1;
442 #else /* INLINER_FOR_JAVA */
443 tree assignment
= TREE_OPERAND (return_stmt
, 0);
444 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, id
->ret_label
);
445 TREE_SIDE_EFFECTS (goto_stmt
) = 1;
446 #endif /* INLINER_FOR_JAVA */
448 /* If we're returning something, just turn that into an
449 assignment into the equivalent of the original
451 #ifndef INLINER_FOR_JAVA
452 if (RETURN_STMT_EXPR (return_stmt
))
454 *tp
= build_stmt (EXPR_STMT
,
455 RETURN_STMT_EXPR (return_stmt
));
456 STMT_IS_FULL_EXPR_P (*tp
) = 1;
457 /* And then jump to the end of the function. */
458 TREE_CHAIN (*tp
) = goto_stmt
;
460 #else /* INLINER_FOR_JAVA */
463 copy_body_r (&assignment
, walk_subtrees
, data
);
464 *tp
= build (COMPOUND_EXPR
, void_type_node
, assignment
, goto_stmt
);
465 TREE_SIDE_EFFECTS (*tp
) = 1;
467 #endif /* INLINER_FOR_JAVA */
468 /* If we're not returning anything just do the jump. */
472 /* Local variables and labels need to be replaced by equivalent
473 variables. We don't want to copy static variables; there's only
474 one of those, no matter how many times we inline the containing
476 else if ((*lang_hooks
.tree_inlining
.auto_var_in_fn_p
) (*tp
, fn
))
480 /* Remap the declaration. */
481 new_decl
= remap_decl (*tp
, id
);
484 /* Replace this variable with the copy. */
485 STRIP_TYPE_NOPS (new_decl
);
489 else if (nonstatic_local_decl_p (*tp
)
490 && DECL_CONTEXT (*tp
) != VARRAY_TREE (id
->fns
, 0))
493 else if (TREE_CODE (*tp
) == SAVE_EXPR
)
494 remap_save_expr (tp
, id
->decl_map
, VARRAY_TREE (id
->fns
, 0),
496 else if (TREE_CODE (*tp
) == UNSAVE_EXPR
)
497 /* UNSAVE_EXPRs should not be generated until expansion time. */
499 #ifndef INLINER_FOR_JAVA
500 /* For a SCOPE_STMT, we must copy the associated block so that we
501 can write out debugging information for the inlined variables. */
502 else if (TREE_CODE (*tp
) == SCOPE_STMT
&& !id
->in_target_cleanup_p
)
503 copy_scope_stmt (tp
, walk_subtrees
, id
);
504 #else /* INLINER_FOR_JAVA */
505 else if (TREE_CODE (*tp
) == LABELED_BLOCK_EXPR
)
507 /* We need a new copy of this labeled block; the EXIT_BLOCK_EXPR
508 will refer to it, so save a copy ready for remapping. We
509 save it in the decl_map, although it isn't a decl. */
510 tree new_block
= copy_node (*tp
);
511 splay_tree_insert (id
->decl_map
,
512 (splay_tree_key
) *tp
,
513 (splay_tree_value
) new_block
);
516 else if (TREE_CODE (*tp
) == EXIT_BLOCK_EXPR
)
519 = splay_tree_lookup (id
->decl_map
,
520 (splay_tree_key
) TREE_OPERAND (*tp
, 0));
521 /* We _must_ have seen the enclosing LABELED_BLOCK_EXPR. */
524 *tp
= copy_node (*tp
);
525 TREE_OPERAND (*tp
, 0) = (tree
) n
->value
;
527 #endif /* INLINER_FOR_JAVA */
528 /* Otherwise, just copy the node. Note that copy_tree_r already
529 knows not to copy VAR_DECLs, etc., so this is safe. */
532 if (TREE_CODE (*tp
) == MODIFY_EXPR
533 && TREE_OPERAND (*tp
, 0) == TREE_OPERAND (*tp
, 1)
534 && ((*lang_hooks
.tree_inlining
.auto_var_in_fn_p
)
535 (TREE_OPERAND (*tp
, 0), fn
)))
537 /* Some assignments VAR = VAR; don't generate any rtl code
538 and thus don't count as variable modification. Avoid
539 keeping bogosities like 0 = 0. */
540 tree decl
= TREE_OPERAND (*tp
, 0), value
;
543 n
= splay_tree_lookup (id
->decl_map
, (splay_tree_key
) decl
);
546 value
= (tree
) n
->value
;
547 STRIP_TYPE_NOPS (value
);
548 if (TREE_CONSTANT (value
) || TREE_READONLY_DECL_P (value
))
551 return copy_body_r (tp
, walk_subtrees
, data
);
555 else if (TREE_CODE (*tp
) == ADDR_EXPR
556 && ((*lang_hooks
.tree_inlining
.auto_var_in_fn_p
)
557 (TREE_OPERAND (*tp
, 0), fn
)))
559 /* Get rid of &* from inline substitutions. It can occur when
560 someone takes the address of a parm or return slot passed by
561 invisible reference. */
562 tree decl
= TREE_OPERAND (*tp
, 0), value
;
565 n
= splay_tree_lookup (id
->decl_map
, (splay_tree_key
) decl
);
568 value
= (tree
) n
->value
;
569 if (TREE_CODE (value
) == INDIRECT_REF
)
571 *tp
= convert (TREE_TYPE (*tp
), TREE_OPERAND (value
, 0));
572 return copy_body_r (tp
, walk_subtrees
, data
);
577 copy_tree_r (tp
, walk_subtrees
, NULL
);
579 /* The copied TARGET_EXPR has never been expanded, even if the
580 original node was expanded already. */
581 if (TREE_CODE (*tp
) == TARGET_EXPR
&& TREE_OPERAND (*tp
, 3))
583 TREE_OPERAND (*tp
, 1) = TREE_OPERAND (*tp
, 3);
584 TREE_OPERAND (*tp
, 3) = NULL_TREE
;
588 /* Keep iterating. */
592 /* Make a copy of the body of FN so that it can be inserted inline in
596 copy_body (inline_data
*id
)
600 body
= DECL_SAVED_TREE (VARRAY_TOP_TREE (id
->fns
));
601 walk_tree (&body
, copy_body_r
, id
, NULL
);
606 /* Generate code to initialize the parameters of the function at the
607 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
610 #ifndef INLINER_FOR_JAVA
611 initialize_inlined_parameters (inline_data
*id
, tree args
, tree fn
)
612 #else /* INLINER_FOR_JAVA */
613 initialize_inlined_parameters (inline_data
*id
, tree args
, tree fn
, tree block
)
614 #endif /* INLINER_FOR_JAVA */
620 #ifdef INLINER_FOR_JAVA
621 tree vars
= NULL_TREE
;
622 #endif /* INLINER_FOR_JAVA */
624 /* Figure out what the parameters are. */
625 parms
= DECL_ARGUMENTS (fn
);
627 /* Start with no initializations whatsoever. */
628 init_stmts
= NULL_TREE
;
630 /* Loop through the parameter declarations, replacing each with an
631 equivalent VAR_DECL, appropriately initialized. */
632 for (p
= parms
, a
= args
; p
;
633 a
= a
? TREE_CHAIN (a
) : a
, p
= TREE_CHAIN (p
))
635 #ifndef INLINER_FOR_JAVA
638 #endif /* not INLINER_FOR_JAVA */
643 /* Find the initializer. */
644 value
= (*lang_hooks
.tree_inlining
.convert_parm_for_inlining
)
645 (p
, a
? TREE_VALUE (a
) : NULL_TREE
, fn
);
647 /* If the parameter is never assigned to, we may not need to
648 create a new variable here at all. Instead, we may be able
649 to just use the argument value. */
650 if (TREE_READONLY (p
)
651 && !TREE_ADDRESSABLE (p
)
652 && value
&& !TREE_SIDE_EFFECTS (value
))
654 /* Simplify the value, if possible. */
655 value
= fold (DECL_P (value
) ? decl_constant_value (value
) : value
);
657 /* We can't risk substituting complex expressions. They
658 might contain variables that will be assigned to later.
659 Theoretically, we could check the expression to see if
660 all of the variables that determine its value are
661 read-only, but we don't bother. */
662 if (TREE_CONSTANT (value
) || TREE_READONLY_DECL_P (value
))
664 /* If this is a declaration, wrap it a NOP_EXPR so that
665 we don't try to put the VALUE on the list of
668 value
= build1 (NOP_EXPR
, TREE_TYPE (value
), value
);
670 /* If this is a constant, make sure it has the right type. */
671 else if (TREE_TYPE (value
) != TREE_TYPE (p
))
672 value
= fold (build1 (NOP_EXPR
, TREE_TYPE (p
), value
));
674 splay_tree_insert (id
->decl_map
,
676 (splay_tree_value
) value
);
681 /* Make an equivalent VAR_DECL. */
682 var
= copy_decl_for_inlining (p
, fn
, VARRAY_TREE (id
->fns
, 0));
684 /* See if the frontend wants to pass this by invisible reference. If
685 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
686 replace uses of the PARM_DECL with dereferences. */
687 if (TREE_TYPE (var
) != TREE_TYPE (p
)
688 && POINTER_TYPE_P (TREE_TYPE (var
))
689 && TREE_TYPE (TREE_TYPE (var
)) == TREE_TYPE (p
))
690 var_sub
= build1 (INDIRECT_REF
, TREE_TYPE (p
), var
);
694 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
695 that way, when the PARM_DECL is encountered, it will be
696 automatically replaced by the VAR_DECL. */
697 splay_tree_insert (id
->decl_map
,
699 (splay_tree_value
) var_sub
);
701 /* Declare this new variable. */
702 #ifndef INLINER_FOR_JAVA
703 init_stmt
= build_stmt (DECL_STMT
, var
);
704 TREE_CHAIN (init_stmt
) = init_stmts
;
705 init_stmts
= init_stmt
;
706 #else /* INLINER_FOR_JAVA */
707 TREE_CHAIN (var
) = vars
;
709 #endif /* INLINER_FOR_JAVA */
711 /* Initialize this VAR_DECL from the equivalent argument. If
712 the argument is an object, created via a constructor or copy,
713 this will not result in an extra copy: the TARGET_EXPR
714 representing the argument will be bound to VAR, and the
715 object will be constructed in VAR. */
716 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p
)))
717 #ifndef INLINER_FOR_JAVA
718 DECL_INITIAL (var
) = value
;
721 /* Even if P was TREE_READONLY, the new VAR should not be.
722 In the original code, we would have constructed a
723 temporary, and then the function body would have never
724 changed the value of P. However, now, we will be
725 constructing VAR directly. The constructor body may
726 change its value multiple times as it is being
727 constructed. Therefore, it must not be TREE_READONLY;
728 the back-end assumes that TREE_READONLY variable is
729 assigned to only once. */
730 TREE_READONLY (var
) = 0;
732 /* Build a run-time initialization. */
733 init_stmt
= build_stmt (EXPR_STMT
,
734 build (INIT_EXPR
, TREE_TYPE (p
),
736 /* Add this initialization to the list. Note that we want the
737 declaration *after* the initialization because we are going
738 to reverse all the initialization statements below. */
739 TREE_CHAIN (init_stmt
) = init_stmts
;
740 init_stmts
= init_stmt
;
743 /* See if we need to clean up the declaration. */
744 cleanup
= (*lang_hooks
.maybe_build_cleanup
) (var
);
748 /* Build the cleanup statement. */
749 cleanup_stmt
= build_stmt (CLEANUP_STMT
, var
, cleanup
);
750 /* Add it to the *front* of the list; the list will be
752 TREE_CHAIN (cleanup_stmt
) = init_stmts
;
753 init_stmts
= cleanup_stmt
;
755 #else /* INLINER_FOR_JAVA */
757 tree assignment
= build (MODIFY_EXPR
, TREE_TYPE (p
), var
, value
);
758 init_stmts
= add_stmt_to_compound (init_stmts
, TREE_TYPE (p
),
763 /* Java objects don't ever need constructing when being
764 passed as arguments because only call by reference is
768 #endif /* INLINER_FOR_JAVA */
771 #ifndef INLINER_FOR_JAVA
772 /* Evaluate trailing arguments. */
773 for (; a
; a
= TREE_CHAIN (a
))
776 tree value
= TREE_VALUE (a
);
778 if (! value
|| ! TREE_SIDE_EFFECTS (value
))
781 init_stmt
= build_stmt (EXPR_STMT
, value
);
782 TREE_CHAIN (init_stmt
) = init_stmts
;
783 init_stmts
= init_stmt
;
786 /* The initialization statements have been built up in reverse
787 order. Straighten them out now. */
788 return nreverse (init_stmts
);
789 #else /* INLINER_FOR_JAVA */
790 BLOCK_VARS (block
) = nreverse (vars
);
792 #endif /* INLINER_FOR_JAVA */
795 /* Declare a return variable to replace the RESULT_DECL for the
796 function we are calling. An appropriate DECL_STMT is returned.
797 The USE_STMT is filled in to contain a use of the declaration to
798 indicate the return value of the function. */
800 #ifndef INLINER_FOR_JAVA
802 declare_return_variable (struct inline_data
*id
, tree return_slot_addr
,
804 #else /* INLINER_FOR_JAVA */
806 declare_return_variable (struct inline_data
*id
, tree return_slot_addr
,
808 #endif /* INLINER_FOR_JAVA */
810 tree fn
= VARRAY_TOP_TREE (id
->fns
);
811 tree result
= DECL_RESULT (fn
);
812 #ifndef INLINER_FOR_JAVA
814 #endif /* not INLINER_FOR_JAVA */
815 int need_return_decl
= 1;
817 /* We don't need to do anything for functions that don't return
819 if (!result
|| VOID_TYPE_P (TREE_TYPE (result
)))
821 #ifndef INLINER_FOR_JAVA
822 *use_stmt
= NULL_TREE
;
823 #else /* INLINER_FOR_JAVA */
825 #endif /* INLINER_FOR_JAVA */
829 #ifndef INLINER_FOR_JAVA
830 var
= ((*lang_hooks
.tree_inlining
.copy_res_decl_for_inlining
)
831 (result
, fn
, VARRAY_TREE (id
->fns
, 0), id
->decl_map
,
832 &need_return_decl
, return_slot_addr
));
834 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
835 way, when the RESULT_DECL is encountered, it will be
836 automatically replaced by the VAR_DECL. */
837 splay_tree_insert (id
->decl_map
,
838 (splay_tree_key
) result
,
839 (splay_tree_value
) var
);
841 /* Build the USE_STMT. If the return type of the function was
842 promoted, convert it back to the expected type. */
843 if (TREE_TYPE (var
) == TREE_TYPE (TREE_TYPE (fn
)))
844 *use_stmt
= build_stmt (EXPR_STMT
, var
);
846 *use_stmt
= build_stmt (EXPR_STMT
,
847 build1 (NOP_EXPR
, TREE_TYPE (TREE_TYPE (fn
)),
849 TREE_ADDRESSABLE (*use_stmt
) = 1;
851 /* Build the declaration statement if FN does not return an
853 if (need_return_decl
)
854 return build_stmt (DECL_STMT
, var
);
855 #else /* INLINER_FOR_JAVA */
856 *var
= ((*lang_hooks
.tree_inlining
.copy_res_decl_for_inlining
)
857 (result
, fn
, VARRAY_TREE (id
->fns
, 0), id
->decl_map
,
858 &need_return_decl
, return_slot_addr
));
860 splay_tree_insert (id
->decl_map
,
861 (splay_tree_key
) result
,
862 (splay_tree_value
) *var
);
863 DECL_IGNORED_P (*var
) = 1;
864 if (need_return_decl
)
866 #endif /* INLINER_FOR_JAVA */
867 /* If FN does return an aggregate, there's no need to declare the
868 return variable; we're using a variable in our caller's frame. */
873 /* Returns nonzero if a function can be inlined as a tree. */
876 tree_inlinable_function_p (tree fn
)
878 return inlinable_function_p (fn
);
881 static const char *inline_forbidden_reason
;
884 inline_forbidden_p_1 (tree
*nodep
, int *walk_subtrees ATTRIBUTE_UNUSED
,
888 tree fn
= (tree
) fnp
;
891 switch (TREE_CODE (node
))
894 /* Refuse to inline alloca call unless user explicitly forced so as this
895 may change program's memory overhead drastically when the function
896 using alloca is called in loop. In GCC present in SPEC2000 inlining
897 into schedule_block cause it to require 2GB of ram instead of 256MB. */
898 if (alloca_call_p (node
)
899 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn
)))
901 inline_forbidden_reason
902 = N_("%Jfunction '%F' can never be inlined because it uses "
903 "alloca (override using the always_inline attribute)");
906 t
= get_callee_fndecl (node
);
911 /* We cannot inline functions that call setjmp. */
912 if (setjmp_call_p (t
))
914 inline_forbidden_reason
915 = N_("%Jfunction '%F' can never be inlined because it uses setjmp");
919 switch (DECL_FUNCTION_CODE (t
))
921 /* We cannot inline functions that take a variable number of
923 case BUILT_IN_VA_START
:
924 case BUILT_IN_STDARG_START
:
925 case BUILT_IN_NEXT_ARG
:
926 case BUILT_IN_VA_END
:
928 inline_forbidden_reason
929 = N_("%Jfunction '%F' can never be inlined because it "
930 "uses variable argument lists");
933 case BUILT_IN_LONGJMP
:
935 /* We can't inline functions that call __builtin_longjmp at all.
936 The non-local goto machinery really requires the destination
937 be in a different function. If we allow the function calling
938 __builtin_longjmp to be inlined into the function calling
939 __builtin_setjmp, Things will Go Awry. */
940 /* ??? Need front end help to identify "regular" non-local goto. */
941 if (DECL_BUILT_IN_CLASS (t
) == BUILT_IN_NORMAL
)
943 inline_forbidden_reason
944 = N_("%Jfunction '%F' can never be inlined "
945 "because it uses setjmp-longjmp exception handling");
955 #ifndef INLINER_FOR_JAVA
957 /* We cannot inline functions that contain other functions. */
958 if (TREE_CODE (TREE_OPERAND (node
, 0)) == FUNCTION_DECL
959 && DECL_INITIAL (TREE_OPERAND (node
, 0)))
961 inline_forbidden_reason
962 = N_("%Jfunction '%F' can never be inlined "
963 "because it contains a nested function");
970 t
= TREE_OPERAND (node
, 0);
972 /* We will not inline a function which uses computed goto. The
973 addresses of its local labels, which may be tucked into
974 global storage, are of course not constant across
975 instantiations, which causes unexpected behavior. */
976 if (TREE_CODE (t
) != LABEL_DECL
)
978 inline_forbidden_reason
979 = N_("%Jfunction '%F' can never be inlined "
980 "because it contains a computed goto");
984 /* We cannot inline a nested function that jumps to a nonlocal
986 if (TREE_CODE (t
) == LABEL_DECL
&& DECL_CONTEXT (t
) != fn
)
988 inline_forbidden_reason
989 = N_("%Jfunction '%F' can never be inlined "
990 "because it contains a nonlocal goto");
998 /* We cannot inline a function of the form
1000 void F (int i) { struct S { int ar[i]; } s; }
1002 Attempting to do so produces a catch-22.
1003 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1004 UNION_TYPE nodes, then it goes into infinite recursion on a
1005 structure containing a pointer to its own type. If it doesn't,
1006 then the type node for S doesn't get adjusted properly when
1007 F is inlined, and we abort in find_function_data. */
1008 for (t
= TYPE_FIELDS (node
); t
; t
= TREE_CHAIN (t
))
1009 if (variably_modified_type_p (TREE_TYPE (t
)))
1011 inline_forbidden_reason
1012 = N_("%Jfunction '%F' can never be inlined "
1013 "because it uses variable sized variables");
1024 /* Return subexpression representing possible alloca call, if any. */
1026 inline_forbidden_p (tree fndecl
)
1028 location_t saved_loc
= input_location
;
1029 tree ret
= walk_tree_without_duplicates
1030 (&DECL_SAVED_TREE (fndecl
), inline_forbidden_p_1
, fndecl
);
1031 input_location
= saved_loc
;
1035 /* Returns nonzero if FN is a function that does not have any
1036 fundamental inline blocking properties. */
1039 inlinable_function_p (tree fn
)
1041 bool inlinable
= true;
1043 /* If we've already decided this function shouldn't be inlined,
1044 there's no need to check again. */
1045 if (DECL_UNINLINABLE (fn
))
1048 /* See if there is any language-specific reason it cannot be
1049 inlined. (It is important that this hook be called early because
1050 in C++ it may result in template instantiation.)
1051 If the function is not inlinable for language-specific reasons,
1052 it is left up to the langhook to explain why. */
1053 inlinable
= !(*lang_hooks
.tree_inlining
.cannot_inline_tree_fn
) (&fn
);
1055 /* If we don't have the function body available, we can't inline it.
1056 However, this should not be recorded since we also get here for
1057 forward declared inline functions. Therefore, return at once. */
1058 if (!DECL_SAVED_TREE (fn
))
1061 /* If we're not inlining at all, then we cannot inline this function. */
1062 else if (!flag_inline_trees
)
1065 /* Only try to inline functions if DECL_INLINE is set. This should be
1066 true for all functions declared `inline', and for all other functions
1067 as well with -finline-functions.
1069 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1070 it's the front-end that must set DECL_INLINE in this case, because
1071 dwarf2out loses if a function that does not have DECL_INLINE set is
1072 inlined anyway. That is why we have both DECL_INLINE and
1073 DECL_DECLARED_INLINE_P. */
1074 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1075 here should be redundant. */
1076 else if (!DECL_INLINE (fn
) && !flag_unit_at_a_time
)
1079 #ifdef INLINER_FOR_JAVA
1080 /* Synchronized methods can't be inlined. This is a bug. */
1081 else if (METHOD_SYNCHRONIZED (fn
))
1083 #endif /* INLINER_FOR_JAVA */
1085 else if (inline_forbidden_p (fn
))
1087 /* See if we should warn about uninlinable functions. Previously,
1088 some of these warnings would be issued while trying to expand
1089 the function inline, but that would cause multiple warnings
1090 about functions that would for example call alloca. But since
1091 this a property of the function, just one warning is enough.
1092 As a bonus we can now give more details about the reason why a
1093 function is not inlinable.
1094 We only warn for functions declared `inline' by the user. */
1095 bool do_warning
= (warn_inline
1097 && DECL_DECLARED_INLINE_P (fn
)
1098 && !DECL_IN_SYSTEM_HEADER (fn
));
1101 warning (inline_forbidden_reason
, fn
, fn
);
1106 /* Squirrel away the result so that we don't have to check again. */
1107 DECL_UNINLINABLE (fn
) = !inlinable
;
1112 /* We can't inline functions that are too big. Only allow a single
1113 function to be of MAX_INLINE_INSNS_SINGLE size. Make special
1114 allowance for extern inline functions, though.
1116 Return nonzero if the function FN can be inlined into the inlining
1120 limits_allow_inlining (tree fn
, inline_data
*id
)
1122 int estimated_insns
= 0;
1125 /* Don't even bother if the function is not inlinable. */
1126 if (!inlinable_function_p (fn
))
1129 /* Investigate the size of the function. Return at once
1130 if the function body size is too large. */
1131 if (!(*lang_hooks
.tree_inlining
.disregard_inline_limits
) (fn
))
1133 int currfn_max_inline_insns
;
1135 /* If we haven't already done so, get an estimate of the number of
1136 instructions that will be produces when expanding this function. */
1137 if (!DECL_ESTIMATED_INSNS (fn
))
1138 DECL_ESTIMATED_INSNS (fn
)
1139 = (*lang_hooks
.tree_inlining
.estimate_num_insns
) (fn
);
1140 estimated_insns
= DECL_ESTIMATED_INSNS (fn
);
1142 /* We may be here either because fn is declared inline or because
1143 we use -finline-functions. For the second case, we are more
1146 FIXME: -finline-functions should imply -funit-at-a-time, it's
1147 about equally expensive but unit-at-a-time produces
1149 currfn_max_inline_insns
= DECL_DECLARED_INLINE_P (fn
) ?
1150 MAX_INLINE_INSNS_SINGLE
: MAX_INLINE_INSNS_AUTO
;
1152 /* If the function is too big to be inlined, adieu. */
1153 if (estimated_insns
> currfn_max_inline_insns
)
1156 /* We now know that we don't disregard the inlining limits and that
1157 we basically should be able to inline this function.
1158 We always allow inlining functions if we estimate that they are
1159 smaller than MIN_INLINE_INSNS. Otherwise, investigate further. */
1160 if (estimated_insns
> MIN_INLINE_INSNS
)
1162 int sum_insns
= (id
? id
->inlined_insns
: 0) + estimated_insns
;
1164 /* In the extreme case that we have exceeded the recursive inlining
1165 limit by a huge factor (128), we just say no.
1167 FIXME: Should not happen in real life, but people have reported
1168 that it actually does!? */
1169 if (sum_insns
> MAX_INLINE_INSNS
* 128)
1172 /* If we did not hit the extreme limit, we use a linear function
1173 with slope -1/MAX_INLINE_SLOPE to exceedingly decrease the
1175 else if (sum_insns
> MAX_INLINE_INSNS
)
1177 if (estimated_insns
> currfn_max_inline_insns
1178 - (sum_insns
- MAX_INLINE_INSNS
) / MAX_INLINE_SLOPE
)
1184 /* Don't allow recursive inlining. */
1185 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (id
->fns
); ++i
)
1186 if (VARRAY_TREE (id
->fns
, i
) == fn
)
1189 if (DECL_INLINED_FNS (fn
))
1192 tree inlined_fns
= DECL_INLINED_FNS (fn
);
1194 for (j
= 0; j
< TREE_VEC_LENGTH (inlined_fns
); ++j
)
1195 if (TREE_VEC_ELT (inlined_fns
, j
) == VARRAY_TREE (id
->fns
, 0))
1199 /* Go ahead, this function can be inlined. */
1203 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1206 expand_call_inline (tree
*tp
, int *walk_subtrees
, void *data
)
1212 #ifndef INLINER_FOR_JAVA
1216 #else /* INLINER_FOR_JAVA */
1218 #endif /* INLINER_FOR_JAVA */
1224 tree return_slot_addr
;
1226 /* See what we've got. */
1227 id
= (inline_data
*) data
;
1230 /* Recurse, but letting recursive invocations know that we are
1231 inside the body of a TARGET_EXPR. */
1232 if (TREE_CODE (*tp
) == TARGET_EXPR
)
1234 #ifndef INLINER_FOR_JAVA
1235 int i
, len
= first_rtl_op (TARGET_EXPR
);
1237 /* We're walking our own subtrees. */
1240 /* Actually walk over them. This loop is the body of
1241 walk_trees, omitting the case where the TARGET_EXPR
1242 itself is handled. */
1243 for (i
= 0; i
< len
; ++i
)
1246 ++id
->in_target_cleanup_p
;
1247 walk_tree (&TREE_OPERAND (*tp
, i
), expand_call_inline
, data
,
1250 --id
->in_target_cleanup_p
;
1254 #else /* INLINER_FOR_JAVA */
1256 #endif /* INLINER_FOR_JAVA */
1258 else if (TREE_CODE (t
) == EXPR_WITH_FILE_LOCATION
)
1260 /* We're walking the subtree directly. */
1262 /* Update the source position. */
1263 push_srcloc (EXPR_WFL_FILENAME (t
), EXPR_WFL_LINENO (t
));
1264 walk_tree (&EXPR_WFL_NODE (t
), expand_call_inline
, data
,
1266 /* Restore the original source position. */
1273 /* Because types were not copied in copy_body, CALL_EXPRs beneath
1274 them should not be expanded. This can happen if the type is a
1275 dynamic array type, for example. */
1278 /* From here on, we're only interested in CALL_EXPRs. */
1279 if (TREE_CODE (t
) != CALL_EXPR
)
1282 /* First, see if we can figure out what function is being called.
1283 If we cannot, then there is no hope of inlining the function. */
1284 fn
= get_callee_fndecl (t
);
1288 /* Turn forward declarations into real ones. */
1289 if (flag_unit_at_a_time
)
1290 fn
= cgraph_node (fn
)->decl
;
1292 /* If fn is a declaration of a function in a nested scope that was
1293 globally declared inline, we don't set its DECL_INITIAL.
1294 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1295 C++ front-end uses it for cdtors to refer to their internal
1296 declarations, that are not real functions. Fortunately those
1297 don't have trees to be saved, so we can tell by checking their
1299 if (! DECL_INITIAL (fn
)
1300 && DECL_ABSTRACT_ORIGIN (fn
)
1301 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn
)))
1302 fn
= DECL_ABSTRACT_ORIGIN (fn
);
1304 /* Don't try to inline functions that are not well-suited to
1306 if ((flag_unit_at_a_time
1307 && (!DECL_SAVED_TREE (fn
) || !cgraph_inline_p (id
->current_decl
, fn
)))
1308 || (!flag_unit_at_a_time
&& !limits_allow_inlining (fn
, id
)))
1310 if (warn_inline
&& DECL_INLINE (fn
) && DECL_DECLARED_INLINE_P (fn
)
1311 && !DECL_IN_SYSTEM_HEADER (fn
))
1313 warning ("%Jinlining failed in call to '%F'", fn
, fn
);
1314 warning ("called from here");
1319 if (! (*lang_hooks
.tree_inlining
.start_inlining
) (fn
))
1322 /* Set the current filename and line number to the function we are
1323 inlining so that when we create new _STMT nodes here they get
1324 line numbers corresponding to the function we are calling. We
1325 wrap the whole inlined body in an EXPR_WITH_FILE_AND_LINE as well
1326 because individual statements don't record the filename. */
1327 push_srcloc (DECL_SOURCE_FILE (fn
), DECL_SOURCE_LINE (fn
));
1329 #ifndef INLINER_FOR_JAVA
1330 /* Build a statement-expression containing code to initialize the
1331 arguments, the actual inline expansion of the body, and a label
1332 for the return statements within the function to jump to. The
1333 type of the statement expression is the return type of the
1335 expr
= build1 (STMT_EXPR
, TREE_TYPE (TREE_TYPE (fn
)), make_node (COMPOUND_STMT
));
1336 /* There is no scope associated with the statement-expression. */
1337 STMT_EXPR_NO_SCOPE (expr
) = 1;
1338 if (lookup_attribute ("warn_unused_result",
1339 TYPE_ATTRIBUTES (TREE_TYPE (fn
))))
1340 STMT_EXPR_WARN_UNUSED_RESULT (expr
) = 1;
1341 stmt
= STMT_EXPR_STMT (expr
);
1342 #else /* INLINER_FOR_JAVA */
1343 /* Build a block containing code to initialize the arguments, the
1344 actual inline expansion of the body, and a label for the return
1345 statements within the function to jump to. The type of the
1346 statement expression is the return type of the function call. */
1348 expr
= build (BLOCK
, TREE_TYPE (TREE_TYPE (fn
)), stmt
);
1349 #endif /* INLINER_FOR_JAVA */
1351 /* Local declarations will be replaced by their equivalents in this
1354 id
->decl_map
= splay_tree_new (splay_tree_compare_pointers
,
1357 /* Initialize the parameters. */
1358 args
= TREE_OPERAND (t
, 1);
1359 return_slot_addr
= NULL_TREE
;
1360 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (t
))
1362 return_slot_addr
= TREE_VALUE (args
);
1363 args
= TREE_CHAIN (args
);
1366 #ifndef INLINER_FOR_JAVA
1367 arg_inits
= initialize_inlined_parameters (id
, args
, fn
);
1368 /* Expand any inlined calls in the initializers. Do this before we
1369 push FN on the stack of functions we are inlining; we want to
1370 inline calls to FN that appear in the initializers for the
1372 expand_calls_inline (&arg_inits
, id
);
1373 /* And add them to the tree. */
1374 COMPOUND_BODY (stmt
) = chainon (COMPOUND_BODY (stmt
), arg_inits
);
1375 #else /* INLINER_FOR_JAVA */
1376 arg_inits
= initialize_inlined_parameters (id
, args
, fn
, expr
);
1379 /* Expand any inlined calls in the initializers. Do this before we
1380 push FN on the stack of functions we are inlining; we want to
1381 inline calls to FN that appear in the initializers for the
1383 expand_calls_inline (&arg_inits
, id
);
1385 /* And add them to the tree. */
1386 BLOCK_EXPR_BODY (expr
) = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
),
1387 TREE_TYPE (arg_inits
),
1390 #endif /* INLINER_FOR_JAVA */
1392 /* Record the function we are about to inline so that we can avoid
1393 recursing into it. */
1394 VARRAY_PUSH_TREE (id
->fns
, fn
);
1396 /* Record the function we are about to inline if optimize_function
1397 has not been called on it yet and we don't have it in the list. */
1398 if (! DECL_INLINED_FNS (fn
))
1402 for (i
= VARRAY_ACTIVE_SIZE (id
->inlined_fns
) - 1; i
>= 0; i
--)
1403 if (VARRAY_TREE (id
->inlined_fns
, i
) == fn
)
1406 VARRAY_PUSH_TREE (id
->inlined_fns
, fn
);
1409 /* Return statements in the function body will be replaced by jumps
1410 to the RET_LABEL. */
1411 id
->ret_label
= build_decl (LABEL_DECL
, NULL_TREE
, NULL_TREE
);
1412 DECL_CONTEXT (id
->ret_label
) = VARRAY_TREE (id
->fns
, 0);
1414 if (! DECL_INITIAL (fn
)
1415 || TREE_CODE (DECL_INITIAL (fn
)) != BLOCK
)
1418 #ifndef INLINER_FOR_JAVA
1419 /* Create a block to put the parameters in. We have to do this
1420 after the parameters have been remapped because remapping
1421 parameters is different from remapping ordinary variables. */
1422 scope_stmt
= build_stmt (SCOPE_STMT
, DECL_INITIAL (fn
));
1423 SCOPE_BEGIN_P (scope_stmt
) = 1;
1424 SCOPE_NO_CLEANUPS_P (scope_stmt
) = 1;
1425 remap_block (scope_stmt
, DECL_ARGUMENTS (fn
), id
);
1426 TREE_CHAIN (scope_stmt
) = COMPOUND_BODY (stmt
);
1427 COMPOUND_BODY (stmt
) = scope_stmt
;
1429 /* Tell the debugging backends that this block represents the
1430 outermost scope of the inlined function. */
1431 if (SCOPE_STMT_BLOCK (scope_stmt
))
1432 BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope_stmt
)) = DECL_ORIGIN (fn
);
1434 /* Declare the return variable for the function. */
1435 COMPOUND_BODY (stmt
)
1436 = chainon (COMPOUND_BODY (stmt
),
1437 declare_return_variable (id
, return_slot_addr
, &use_stmt
));
1438 #else /* INLINER_FOR_JAVA */
1440 /* Declare the return variable for the function. */
1441 tree decl
= declare_return_variable (id
, return_slot_addr
, &retvar
);
1444 tree
*next
= &BLOCK_VARS (expr
);
1446 next
= &TREE_CHAIN (*next
);
1450 #endif /* INLINER_FOR_JAVA */
1452 /* After we've initialized the parameters, we insert the body of the
1454 #ifndef INLINER_FOR_JAVA
1455 inlined_body
= &COMPOUND_BODY (stmt
);
1456 while (*inlined_body
)
1457 inlined_body
= &TREE_CHAIN (*inlined_body
);
1458 *inlined_body
= copy_body (id
);
1459 #else /* INLINER_FOR_JAVA */
1462 java_inlining_map_static_initializers (fn
, id
->decl_map
);
1463 new_body
= copy_body (id
);
1464 TREE_TYPE (new_body
) = TREE_TYPE (TREE_TYPE (fn
));
1465 BLOCK_EXPR_BODY (expr
)
1466 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
),
1467 TREE_TYPE (new_body
), new_body
);
1468 inlined_body
= &BLOCK_EXPR_BODY (expr
);
1470 #endif /* INLINER_FOR_JAVA */
1472 /* After the body of the function comes the RET_LABEL. This must come
1473 before we evaluate the returned value below, because that evaluation
1474 may cause RTL to be generated. */
1475 #ifndef INLINER_FOR_JAVA
1476 COMPOUND_BODY (stmt
)
1477 = chainon (COMPOUND_BODY (stmt
),
1478 build_stmt (LABEL_STMT
, id
->ret_label
));
1479 #else /* INLINER_FOR_JAVA */
1481 tree label
= build1 (LABEL_EXPR
, void_type_node
, id
->ret_label
);
1482 BLOCK_EXPR_BODY (expr
)
1483 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
), void_type_node
, label
);
1484 TREE_SIDE_EFFECTS (label
) = TREE_SIDE_EFFECTS (t
);
1486 #endif /* INLINER_FOR_JAVA */
1488 /* Finally, mention the returned value so that the value of the
1489 statement-expression is the returned value of the function. */
1490 #ifndef INLINER_FOR_JAVA
1491 COMPOUND_BODY (stmt
) = chainon (COMPOUND_BODY (stmt
), use_stmt
);
1493 /* Close the block for the parameters. */
1494 scope_stmt
= build_stmt (SCOPE_STMT
, DECL_INITIAL (fn
));
1495 SCOPE_NO_CLEANUPS_P (scope_stmt
) = 1;
1496 remap_block (scope_stmt
, NULL_TREE
, id
);
1497 COMPOUND_BODY (stmt
)
1498 = chainon (COMPOUND_BODY (stmt
), scope_stmt
);
1499 #else /* INLINER_FOR_JAVA */
1502 /* Mention the retvar. If the return type of the function was
1503 promoted, convert it back to the expected type. */
1504 if (TREE_TYPE (TREE_TYPE (fn
)) != TREE_TYPE (retvar
))
1505 retvar
= build1 (NOP_EXPR
, TREE_TYPE (TREE_TYPE (fn
)), retvar
);
1506 BLOCK_EXPR_BODY (expr
)
1507 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
),
1508 TREE_TYPE (retvar
), retvar
);
1511 java_inlining_merge_static_initializers (fn
, id
->decl_map
);
1512 #endif /* INLINER_FOR_JAVA */
1515 splay_tree_delete (id
->decl_map
);
1518 /* The new expression has side-effects if the old one did. */
1519 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (t
);
1521 /* Replace the call by the inlined body. Wrap it in an
1522 EXPR_WITH_FILE_LOCATION so that we'll get debugging line notes
1523 pointing to the right place. */
1524 #ifndef INLINER_FOR_JAVA
1525 chain
= TREE_CHAIN (*tp
);
1526 #endif /* INLINER_FOR_JAVA */
1527 *tp
= build_expr_wfl (expr
, DECL_SOURCE_FILE (fn
), DECL_SOURCE_LINE (fn
),
1529 EXPR_WFL_EMIT_LINE_NOTE (*tp
) = 1;
1530 #ifndef INLINER_FOR_JAVA
1531 TREE_CHAIN (*tp
) = chain
;
1532 #endif /* not INLINER_FOR_JAVA */
1535 /* If the value of the new expression is ignored, that's OK. We
1536 don't warn about this for CALL_EXPRs, so we shouldn't warn about
1537 the equivalent inlined version either. */
1538 TREE_USED (*tp
) = 1;
1540 /* Our function now has more statements than it did before. */
1541 DECL_ESTIMATED_INSNS (VARRAY_TREE (id
->fns
, 0)) += DECL_ESTIMATED_INSNS (fn
);
1542 /* For accounting, subtract one for the saved call/ret. */
1543 id
->inlined_insns
+= DECL_ESTIMATED_INSNS (fn
) - 1;
1545 /* Update callgraph if needed. */
1546 if (id
->decl
&& flag_unit_at_a_time
)
1548 cgraph_remove_call (id
->decl
, fn
);
1549 cgraph_create_edges (id
->decl
, *inlined_body
);
1552 /* Recurse into the body of the just inlined function. */
1554 tree old_decl
= id
->current_decl
;
1555 id
->current_decl
= fn
;
1556 expand_calls_inline (inlined_body
, id
);
1557 id
->current_decl
= old_decl
;
1559 VARRAY_POP (id
->fns
);
1561 /* If we've returned to the top level, clear out the record of how
1562 much inlining has been done. */
1563 if (VARRAY_ACTIVE_SIZE (id
->fns
) == id
->first_inlined_fn
)
1564 id
->inlined_insns
= 0;
1566 /* Don't walk into subtrees. We've already handled them above. */
1569 (*lang_hooks
.tree_inlining
.end_inlining
) (fn
);
1571 /* Keep iterating. */
1574 /* Walk over the entire tree *TP, replacing CALL_EXPRs with inline
1575 expansions as appropriate. */
1578 expand_calls_inline (tree
*tp
, inline_data
*id
)
1580 /* Search through *TP, replacing all calls to inline functions by
1581 appropriate equivalents. Use walk_tree in no-duplicates mode
1582 to avoid exponential time complexity. (We can't just use
1583 walk_tree_without_duplicates, because of the special TARGET_EXPR
1584 handling in expand_calls. The hash table is set up in
1585 optimize_function. */
1586 walk_tree (tp
, expand_call_inline
, id
, id
->tree_pruner
);
1589 /* Expand calls to inline functions in the body of FN. */
1592 optimize_inline_calls (tree fn
)
1598 memset (&id
, 0, sizeof (id
));
1601 id
.current_decl
= fn
;
1602 /* Don't allow recursion into FN. */
1603 VARRAY_TREE_INIT (id
.fns
, 32, "fns");
1604 VARRAY_PUSH_TREE (id
.fns
, fn
);
1605 if (!DECL_ESTIMATED_INSNS (fn
))
1606 DECL_ESTIMATED_INSNS (fn
)
1607 = (*lang_hooks
.tree_inlining
.estimate_num_insns
) (fn
);
1608 /* Or any functions that aren't finished yet. */
1609 prev_fn
= NULL_TREE
;
1610 if (current_function_decl
)
1612 VARRAY_PUSH_TREE (id
.fns
, current_function_decl
);
1613 prev_fn
= current_function_decl
;
1616 prev_fn
= ((*lang_hooks
.tree_inlining
.add_pending_fn_decls
)
1617 (&id
.fns
, prev_fn
));
1619 /* Create the list of functions this call will inline. */
1620 VARRAY_TREE_INIT (id
.inlined_fns
, 32, "inlined_fns");
1622 /* Keep track of the low-water mark, i.e., the point where the first
1623 real inlining is represented in ID.FNS. */
1624 id
.first_inlined_fn
= VARRAY_ACTIVE_SIZE (id
.fns
);
1626 /* Replace all calls to inline functions with the bodies of those
1628 id
.tree_pruner
= htab_create (37, htab_hash_pointer
,
1629 htab_eq_pointer
, NULL
);
1630 expand_calls_inline (&DECL_SAVED_TREE (fn
), &id
);
1633 htab_delete (id
.tree_pruner
);
1634 if (DECL_LANG_SPECIFIC (fn
))
1636 tree ifn
= make_tree_vec (VARRAY_ACTIVE_SIZE (id
.inlined_fns
));
1638 if (VARRAY_ACTIVE_SIZE (id
.inlined_fns
))
1639 memcpy (&TREE_VEC_ELT (ifn
, 0), &VARRAY_TREE (id
.inlined_fns
, 0),
1640 VARRAY_ACTIVE_SIZE (id
.inlined_fns
) * sizeof (tree
));
1641 DECL_INLINED_FNS (fn
) = ifn
;
1645 /* FN is a function that has a complete body, and CLONE is a function
1646 whose body is to be set to a copy of FN, mapping argument
1647 declarations according to the ARG_MAP splay_tree. */
1650 clone_body (tree clone
, tree fn
, void *arg_map
)
1654 /* Clone the body, as if we were making an inline call. But, remap
1655 the parameters in the callee to the parameters of caller. If
1656 there's an in-charge parameter, map it to an appropriate
1658 memset (&id
, 0, sizeof (id
));
1659 VARRAY_TREE_INIT (id
.fns
, 2, "fns");
1660 VARRAY_PUSH_TREE (id
.fns
, clone
);
1661 VARRAY_PUSH_TREE (id
.fns
, fn
);
1662 id
.decl_map
= (splay_tree
)arg_map
;
1664 /* Cloning is treated slightly differently from inlining. Set
1665 CLONING_P so that it's clear which operation we're performing. */
1666 id
.cloning_p
= true;
1668 /* Actually copy the body. */
1669 TREE_CHAIN (DECL_SAVED_TREE (clone
)) = copy_body (&id
);
1672 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1673 FUNC is called with the DATA and the address of each sub-tree. If
1674 FUNC returns a non-NULL value, the traversal is aborted, and the
1675 value returned by FUNC is returned. If HTAB is non-NULL it is used
1676 to record the nodes visited, and to avoid visiting a node more than
1680 walk_tree (tree
*tp
, walk_tree_fn func
, void *data
, void *htab_
)
1682 htab_t htab
= (htab_t
) htab_
;
1683 enum tree_code code
;
1687 #define WALK_SUBTREE(NODE) \
1690 result = walk_tree (&(NODE), func, data, htab); \
1696 #define WALK_SUBTREE_TAIL(NODE) \
1700 goto tail_recurse; \
1705 /* Skip empty subtrees. */
1713 /* Don't walk the same tree twice, if the user has requested
1714 that we avoid doing so. */
1715 slot
= htab_find_slot (htab
, *tp
, INSERT
);
1721 /* Call the function. */
1723 result
= (*func
) (tp
, &walk_subtrees
, data
);
1725 /* If we found something, return it. */
1729 code
= TREE_CODE (*tp
);
1731 #ifndef INLINER_FOR_JAVA
1732 /* Even if we didn't, FUNC may have decided that there was nothing
1733 interesting below this point in the tree. */
1736 if (STATEMENT_CODE_P (code
) || code
== TREE_LIST
1737 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
))
1738 /* But we still need to check our siblings. */
1739 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp
));
1744 /* Handle common cases up front. */
1745 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
1746 #else /* INLINER_FOR_JAVA */
1747 if (code
!= EXIT_BLOCK_EXPR
1748 && code
!= SAVE_EXPR
1749 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
1750 #endif /* INLINER_FOR_JAVA */
1754 #ifndef INLINER_FOR_JAVA
1755 /* Set lineno here so we get the right instantiation context
1756 if we call instantiate_decl from inlinable_function_p. */
1757 if (STATEMENT_CODE_P (code
) && !STMT_LINENO_FOR_FN_P (*tp
))
1758 input_line
= STMT_LINENO (*tp
);
1759 #endif /* not INLINER_FOR_JAVA */
1761 /* Walk over all the sub-trees of this operand. */
1762 len
= first_rtl_op (code
);
1763 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1764 But, we only want to walk once. */
1765 if (code
== TARGET_EXPR
1766 && TREE_OPERAND (*tp
, 3) == TREE_OPERAND (*tp
, 1))
1768 /* Go through the subtrees. We need to do this in forward order so
1769 that the scope of a FOR_EXPR is handled properly. */
1770 for (i
= 0; i
< len
; ++i
)
1771 WALK_SUBTREE (TREE_OPERAND (*tp
, i
));
1773 #ifndef INLINER_FOR_JAVA
1774 /* For statements, we also walk the chain so that we cover the
1775 entire statement tree. */
1776 if (STATEMENT_CODE_P (code
))
1778 if (code
== DECL_STMT
1779 && DECL_STMT_DECL (*tp
)
1780 && DECL_P (DECL_STMT_DECL (*tp
)))
1782 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
1783 into declarations that are just mentioned, rather than
1784 declared; they don't really belong to this part of the tree.
1785 And, we can see cycles: the initializer for a declaration can
1786 refer to the declaration itself. */
1787 WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp
)));
1788 WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp
)));
1789 WALK_SUBTREE (DECL_SIZE_UNIT (DECL_STMT_DECL (*tp
)));
1792 /* This can be tail-recursion optimized if we write it this way. */
1793 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp
));
1796 #endif /* not INLINER_FOR_JAVA */
1797 /* We didn't find what we were looking for. */
1800 else if (TREE_CODE_CLASS (code
) == 'd')
1802 WALK_SUBTREE_TAIL (TREE_TYPE (*tp
));
1804 else if (TREE_CODE_CLASS (code
) == 't')
1806 WALK_SUBTREE (TYPE_SIZE (*tp
));
1807 WALK_SUBTREE (TYPE_SIZE_UNIT (*tp
));
1808 /* Also examine various special fields, below. */
1811 result
= (*lang_hooks
.tree_inlining
.walk_subtrees
) (tp
, &walk_subtrees
, func
,
1813 if (result
|| ! walk_subtrees
)
1816 /* Not one of the easy cases. We must explicitly go through the
1821 case IDENTIFIER_NODE
:
1836 /* None of thse have subtrees other than those already walked
1841 case REFERENCE_TYPE
:
1842 WALK_SUBTREE_TAIL (TREE_TYPE (*tp
));
1846 WALK_SUBTREE (TREE_VALUE (*tp
));
1847 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp
));
1852 int len
= TREE_VEC_LENGTH (*tp
);
1857 /* Walk all elements but the first. */
1859 WALK_SUBTREE (TREE_VEC_ELT (*tp
, len
));
1861 /* Now walk the first one as a tail call. */
1862 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp
, 0));
1866 WALK_SUBTREE (TREE_REALPART (*tp
));
1867 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp
));
1870 WALK_SUBTREE_TAIL (CONSTRUCTOR_ELTS (*tp
));
1873 WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp
));
1877 WALK_SUBTREE (TREE_TYPE (*tp
));
1879 tree arg
= TYPE_ARG_TYPES (*tp
);
1881 /* We never want to walk into default arguments. */
1882 for (; arg
; arg
= TREE_CHAIN (arg
))
1883 WALK_SUBTREE (TREE_VALUE (arg
));
1888 WALK_SUBTREE (TREE_TYPE (*tp
));
1889 WALK_SUBTREE_TAIL (TYPE_DOMAIN (*tp
));
1892 WALK_SUBTREE (TYPE_MIN_VALUE (*tp
));
1893 WALK_SUBTREE_TAIL (TYPE_MAX_VALUE (*tp
));
1896 WALK_SUBTREE (TREE_TYPE (*tp
));
1897 WALK_SUBTREE_TAIL (TYPE_OFFSET_BASETYPE (*tp
));
1899 #ifdef INLINER_FOR_JAVA
1900 case EXIT_BLOCK_EXPR
:
1901 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp
, 1));
1904 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp
, 0));
1905 #endif /* INLINER_FOR_JAVA */
1911 /* We didn't find what we were looking for. */
1915 #undef WALK_SUBTREE_TAIL
1918 /* Like walk_tree, but does not walk duplicate nodes more than
1922 walk_tree_without_duplicates (tree
*tp
, walk_tree_fn func
, void *data
)
1927 htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1928 result
= walk_tree (tp
, func
, data
, htab
);
1933 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
1936 copy_tree_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
1938 enum tree_code code
= TREE_CODE (*tp
);
1940 /* We make copies of most nodes. */
1941 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
1942 || TREE_CODE_CLASS (code
) == 'c'
1943 || code
== TREE_LIST
1945 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
))
1947 /* Because the chain gets clobbered when we make a copy, we save it
1949 tree chain
= TREE_CHAIN (*tp
);
1951 /* Copy the node. */
1952 *tp
= copy_node (*tp
);
1954 /* Now, restore the chain, if appropriate. That will cause
1955 walk_tree to walk into the chain as well. */
1956 if (code
== PARM_DECL
|| code
== TREE_LIST
1957 #ifndef INLINER_FOR_JAVA
1958 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
)
1959 || STATEMENT_CODE_P (code
))
1960 TREE_CHAIN (*tp
) = chain
;
1962 /* For now, we don't update BLOCKs when we make copies. So, we
1963 have to nullify all scope-statements. */
1964 if (TREE_CODE (*tp
) == SCOPE_STMT
)
1965 SCOPE_STMT_BLOCK (*tp
) = NULL_TREE
;
1966 #else /* INLINER_FOR_JAVA */
1967 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
))
1968 TREE_CHAIN (*tp
) = chain
;
1969 #endif /* INLINER_FOR_JAVA */
1971 else if (TREE_CODE_CLASS (code
) == 't' && !variably_modified_type_p (*tp
))
1972 /* Types only need to be copied if they are variably modified. */
1978 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
1979 information indicating to what new SAVE_EXPR this one should be
1980 mapped, use that one. Otherwise, create a new node and enter it in
1981 ST. FN is the function into which the copy will be placed. */
1984 remap_save_expr (tree
*tp
, void *st_
, tree fn
, int *walk_subtrees
)
1986 splay_tree st
= (splay_tree
) st_
;
1989 /* See if we already encountered this SAVE_EXPR. */
1990 n
= splay_tree_lookup (st
, (splay_tree_key
) *tp
);
1992 /* If we didn't already remap this SAVE_EXPR, do so now. */
1995 tree t
= copy_node (*tp
);
1997 /* The SAVE_EXPR is now part of the function into which we
1998 are inlining this body. */
1999 SAVE_EXPR_CONTEXT (t
) = fn
;
2000 /* And we haven't evaluated it yet. */
2001 SAVE_EXPR_RTL (t
) = NULL_RTX
;
2002 /* Remember this SAVE_EXPR. */
2003 n
= splay_tree_insert (st
,
2004 (splay_tree_key
) *tp
,
2005 (splay_tree_value
) t
);
2006 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2007 splay_tree_insert (st
, (splay_tree_key
) t
, (splay_tree_value
) t
);
2010 /* We've already walked into this SAVE_EXPR, so we needn't do it
2014 /* Replace this SAVE_EXPR with the copy. */
2015 *tp
= (tree
) n
->value
;
2018 #ifdef INLINER_FOR_JAVA
2019 /* Add STMT to EXISTING if possible, otherwise create a new
2020 COMPOUND_EXPR and add STMT to it. */
2023 add_stmt_to_compound (tree existing
, tree type
, tree stmt
)
2028 return build (COMPOUND_EXPR
, type
, existing
, stmt
);
2033 #endif /* INLINER_FOR_JAVA */