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)
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. */
26 #include "tree-inline.h"
32 #include "insn-config.h"
33 #include "integrate.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
43 #else /* INLINER_FOR_JAVA */
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
53 int flag_inline_trees
= 0;
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
64 o Provide heuristics to clamp inlining of recursive template
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. */
78 /* The index of the first element of FNS that really represents an
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. */
85 /* The map from local declarations in the inlined function to
86 equivalents in the function into which it is being inlined. */
88 /* Nonzero if we are currently within the cleanup for a
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. */
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. */
103 /* Hash table used to prevent walk_tree from visiting the same node
104 umpteen million times. */
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. */
135 remap_decl (decl
, 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 (scope_stmt
, decls
, id
)
219 #else /* INLINER_FOR_JAVA */
220 remap_block (block
, decls
, id
)
222 #endif /* INLINER_FOR_JAVA */
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
;
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
))
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
);
258 old_var
= TREE_CHAIN (old_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
269 if (!new_var
|| !DECL_P (new_var
))
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);
281 /* We're building a clone; DECL_INITIAL is still
282 error_mark_node, and current_binding_level is the parm
284 (*lang_hooks
.decls
.insert_block
) (new_block
);
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. */
291 if (DECL_INITIAL (fn
))
292 first_block
= &BLOCK_CHAIN (DECL_INITIAL (fn
));
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
305 else if (SCOPE_END_P (scope_stmt
) && SCOPE_STMT_BLOCK (scope_stmt
))
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
));
314 SCOPE_STMT_BLOCK (scope_stmt
) = (tree
) n
->value
;
316 #else /* INLINER_FOR_JAVA */
322 /* Make the new 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
);
332 /* Remap its variables. */
333 for (old_var
= decls
? decls
: BLOCK_VARS (old_block
);
335 old_var
= TREE_CHAIN (old_var
))
339 /* All local class initialization flags go in the outermost
341 if (LOCAL_CLASS_INITIALIZATION_FLAG_P (old_var
))
343 /* We may already have one. */
344 if (! splay_tree_lookup (id
->decl_map
, (splay_tree_key
) old_var
))
346 tree outermost_block
;
347 new_var
= remap_decl (old_var
, id
);
348 DECL_ABSTRACT_ORIGIN (new_var
) = NULL
;
349 outermost_block
= DECL_SAVED_TREE (current_function_decl
);
350 TREE_CHAIN (new_var
) = BLOCK_VARS (outermost_block
);
351 BLOCK_VARS (outermost_block
) = new_var
;
356 /* Remap the variable. */
357 new_var
= remap_decl (old_var
, id
);
358 /* If we didn't remap this variable, so we can't mess with
359 its TREE_CHAIN. If we remapped this variable to
360 something other than a declaration (say, if we mapped it
361 to a constant), then we must similarly omit any mention
363 if (!new_var
|| !DECL_P (new_var
))
367 TREE_CHAIN (new_var
) = BLOCK_VARS (new_block
);
368 BLOCK_VARS (new_block
) = new_var
;
371 /* We put the BLOCK_VARS in reverse order; fix that now. */
372 BLOCK_VARS (new_block
) = nreverse (BLOCK_VARS (new_block
));
373 fn
= VARRAY_TREE (id
->fns
, 0);
374 /* Remember the remapped block. */
375 splay_tree_insert (id
->decl_map
,
376 (splay_tree_key
) old_block
,
377 (splay_tree_value
) new_block
);
378 #endif /* INLINER_FOR_JAVA */
381 #ifndef INLINER_FOR_JAVA
382 /* Copy the SCOPE_STMT pointed to by TP. */
385 copy_scope_stmt (tp
, walk_subtrees
, id
)
392 /* Remember whether or not this statement was nullified. When
393 making a copy, copy_tree_r always sets SCOPE_NULLIFIED_P (and
394 doesn't copy the SCOPE_STMT_BLOCK) to free callers from having to
395 deal with copying BLOCKs if they do not wish to do so. */
396 block
= SCOPE_STMT_BLOCK (*tp
);
397 /* Copy (and replace) the statement. */
398 copy_tree_r (tp
, walk_subtrees
, NULL
);
399 /* Restore the SCOPE_STMT_BLOCK. */
400 SCOPE_STMT_BLOCK (*tp
) = block
;
402 /* Remap the associated block. */
403 remap_block (*tp
, NULL_TREE
, id
);
405 #endif /* not INLINER_FOR_JAVA */
407 /* Called from copy_body via walk_tree. DATA is really an
410 copy_body_r (tp
, walk_subtrees
, data
)
419 id
= (inline_data
*) data
;
420 fn
= VARRAY_TOP_TREE (id
->fns
);
423 /* All automatic variables should have a DECL_CONTEXT indicating
424 what function they come from. */
425 if ((TREE_CODE (*tp
) == VAR_DECL
|| TREE_CODE (*tp
) == LABEL_DECL
)
426 && DECL_NAMESPACE_SCOPE_P (*tp
))
427 if (! DECL_EXTERNAL (*tp
) && ! TREE_STATIC (*tp
))
431 #ifdef INLINER_FOR_JAVA
432 if (TREE_CODE (*tp
) == BLOCK
)
433 remap_block (tp
, NULL_TREE
, id
);
436 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
437 GOTO_STMT with the RET_LABEL as its target. */
438 #ifndef INLINER_FOR_JAVA
439 if (TREE_CODE (*tp
) == RETURN_STMT
&& id
->ret_label
)
440 #else /* INLINER_FOR_JAVA */
441 if (TREE_CODE (*tp
) == RETURN_EXPR
&& id
->ret_label
)
442 #endif /* INLINER_FOR_JAVA */
444 tree return_stmt
= *tp
;
447 /* Build the GOTO_STMT. */
448 #ifndef INLINER_FOR_JAVA
449 goto_stmt
= build_stmt (GOTO_STMT
, id
->ret_label
);
450 TREE_CHAIN (goto_stmt
) = TREE_CHAIN (return_stmt
);
451 GOTO_FAKE_P (goto_stmt
) = 1;
452 #else /* INLINER_FOR_JAVA */
453 tree assignment
= TREE_OPERAND (return_stmt
, 0);
454 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, id
->ret_label
);
455 TREE_SIDE_EFFECTS (goto_stmt
) = 1;
456 #endif /* INLINER_FOR_JAVA */
458 /* If we're returning something, just turn that into an
459 assignment into the equivalent of the original
461 #ifndef INLINER_FOR_JAVA
462 if (RETURN_STMT_EXPR (return_stmt
))
464 *tp
= build_stmt (EXPR_STMT
,
465 RETURN_STMT_EXPR (return_stmt
));
466 STMT_IS_FULL_EXPR_P (*tp
) = 1;
467 /* And then jump to the end of the function. */
468 TREE_CHAIN (*tp
) = goto_stmt
;
470 #else /* INLINER_FOR_JAVA */
473 copy_body_r (&assignment
, walk_subtrees
, data
);
474 *tp
= build (COMPOUND_EXPR
, void_type_node
, assignment
, goto_stmt
);
475 TREE_SIDE_EFFECTS (*tp
) = 1;
477 #endif /* INLINER_FOR_JAVA */
478 /* If we're not returning anything just do the jump. */
482 /* Local variables and labels need to be replaced by equivalent
483 variables. We don't want to copy static variables; there's only
484 one of those, no matter how many times we inline the containing
486 else if ((*lang_hooks
.tree_inlining
.auto_var_in_fn_p
) (*tp
, fn
))
490 /* Remap the declaration. */
491 new_decl
= remap_decl (*tp
, id
);
494 /* Replace this variable with the copy. */
495 STRIP_TYPE_NOPS (new_decl
);
499 else if (nonstatic_local_decl_p (*tp
)
500 && DECL_CONTEXT (*tp
) != VARRAY_TREE (id
->fns
, 0))
503 else if (TREE_CODE (*tp
) == SAVE_EXPR
)
504 remap_save_expr (tp
, id
->decl_map
, VARRAY_TREE (id
->fns
, 0),
506 else if (TREE_CODE (*tp
) == UNSAVE_EXPR
)
507 /* UNSAVE_EXPRs should not be generated until expansion time. */
509 #ifndef INLINER_FOR_JAVA
510 /* For a SCOPE_STMT, we must copy the associated block so that we
511 can write out debugging information for the inlined variables. */
512 else if (TREE_CODE (*tp
) == SCOPE_STMT
&& !id
->in_target_cleanup_p
)
513 copy_scope_stmt (tp
, walk_subtrees
, id
);
514 #else /* INLINER_FOR_JAVA */
515 else if (TREE_CODE (*tp
) == LABELED_BLOCK_EXPR
)
517 /* We need a new copy of this labeled block; the EXIT_BLOCK_EXPR
518 will refer to it, so save a copy ready for remapping. We
519 save it in the decl_map, although it isn't a decl. */
520 tree new_block
= copy_node (*tp
);
521 splay_tree_insert (id
->decl_map
,
522 (splay_tree_key
) *tp
,
523 (splay_tree_value
) new_block
);
526 else if (TREE_CODE (*tp
) == EXIT_BLOCK_EXPR
)
529 = splay_tree_lookup (id
->decl_map
,
530 (splay_tree_key
) TREE_OPERAND (*tp
, 0));
531 /* We _must_ have seen the enclosing LABELED_BLOCK_EXPR. */
534 *tp
= copy_node (*tp
);
535 TREE_OPERAND (*tp
, 0) = (tree
) n
->value
;
537 #endif /* INLINER_FOR_JAVA */
538 /* Otherwise, just copy the node. Note that copy_tree_r already
539 knows not to copy VAR_DECLs, etc., so this is safe. */
542 copy_tree_r (tp
, walk_subtrees
, NULL
);
544 /* The copied TARGET_EXPR has never been expanded, even if the
545 original node was expanded already. */
546 if (TREE_CODE (*tp
) == TARGET_EXPR
&& TREE_OPERAND (*tp
, 3))
548 TREE_OPERAND (*tp
, 1) = TREE_OPERAND (*tp
, 3);
549 TREE_OPERAND (*tp
, 3) = NULL_TREE
;
551 else if (TREE_CODE (*tp
) == MODIFY_EXPR
552 && TREE_OPERAND (*tp
, 0) == TREE_OPERAND (*tp
, 1)
553 && ((*lang_hooks
.tree_inlining
.auto_var_in_fn_p
)
554 (TREE_OPERAND (*tp
, 0), fn
)))
556 /* Some assignments VAR = VAR; don't generate any rtl code
557 and thus don't count as variable modification. Avoid
558 keeping bogosities like 0 = 0. */
559 tree decl
= TREE_OPERAND (*tp
, 0), value
;
562 n
= splay_tree_lookup (id
->decl_map
, (splay_tree_key
) decl
);
565 value
= (tree
) n
->value
;
566 STRIP_TYPE_NOPS (value
);
567 if (TREE_CONSTANT (value
) || TREE_READONLY_DECL_P (value
))
573 /* Keep iterating. */
577 /* Make a copy of the body of FN so that it can be inserted inline in
586 body
= DECL_SAVED_TREE (VARRAY_TOP_TREE (id
->fns
));
587 walk_tree (&body
, copy_body_r
, id
, NULL
);
592 /* Generate code to initialize the parameters of the function at the
593 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
596 #ifndef INLINER_FOR_JAVA
597 initialize_inlined_parameters (id
, args
, fn
)
598 #else /* INLINER_FOR_JAVA */
599 initialize_inlined_parameters (id
, args
, fn
, block
)
600 #endif /* INLINER_FOR_JAVA */
604 #ifdef INLINER_FOR_JAVA
606 #endif /* INLINER_FOR_JAVA */
612 #ifdef INLINER_FOR_JAVA
613 tree vars
= NULL_TREE
;
614 #endif /* INLINER_FOR_JAVA */
616 /* Figure out what the parameters are. */
617 parms
= DECL_ARGUMENTS (fn
);
619 /* Start with no initializations whatsoever. */
620 init_stmts
= NULL_TREE
;
622 /* Loop through the parameter declarations, replacing each with an
623 equivalent VAR_DECL, appropriately initialized. */
624 for (p
= parms
, a
= args
; p
;
625 a
= a
? TREE_CHAIN (a
) : a
, p
= TREE_CHAIN (p
))
627 #ifndef INLINER_FOR_JAVA
630 #endif /* not INLINER_FOR_JAVA */
634 /* Find the initializer. */
635 value
= (*lang_hooks
.tree_inlining
.convert_parm_for_inlining
)
636 (p
, a
? TREE_VALUE (a
) : NULL_TREE
, fn
);
638 /* If the parameter is never assigned to, we may not need to
639 create a new variable here at all. Instead, we may be able
640 to just use the argument value. */
641 if (TREE_READONLY (p
)
642 && !TREE_ADDRESSABLE (p
)
643 && value
&& !TREE_SIDE_EFFECTS (value
))
645 /* Simplify the value, if possible. */
646 value
= fold (DECL_P (value
) ? decl_constant_value (value
) : value
);
648 /* We can't risk substituting complex expressions. They
649 might contain variables that will be assigned to later.
650 Theoretically, we could check the expression to see if
651 all of the variables that determine its value are
652 read-only, but we don't bother. */
653 if (TREE_CONSTANT (value
) || TREE_READONLY_DECL_P (value
))
655 /* If this is a declaration, wrap it a NOP_EXPR so that
656 we don't try to put the VALUE on the list of
659 value
= build1 (NOP_EXPR
, TREE_TYPE (value
), value
);
661 splay_tree_insert (id
->decl_map
,
663 (splay_tree_value
) value
);
668 /* Make an equivalent VAR_DECL. */
669 var
= copy_decl_for_inlining (p
, fn
, VARRAY_TREE (id
->fns
, 0));
670 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
671 that way, when the PARM_DECL is encountered, it will be
672 automatically replaced by the VAR_DECL. */
673 splay_tree_insert (id
->decl_map
,
675 (splay_tree_value
) var
);
677 /* Declare this new variable. */
678 #ifndef INLINER_FOR_JAVA
679 init_stmt
= build_stmt (DECL_STMT
, var
);
680 TREE_CHAIN (init_stmt
) = init_stmts
;
681 init_stmts
= init_stmt
;
682 #else /* INLINER_FOR_JAVA */
683 TREE_CHAIN (var
) = vars
;
685 #endif /* INLINER_FOR_JAVA */
687 /* Initialize this VAR_DECL from the equivalent argument. If
688 the argument is an object, created via a constructor or copy,
689 this will not result in an extra copy: the TARGET_EXPR
690 representing the argument will be bound to VAR, and the
691 object will be constructed in VAR. */
692 if (! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p
)))
693 #ifndef INLINER_FOR_JAVA
694 DECL_INITIAL (var
) = value
;
697 /* Even if P was TREE_READONLY, the new VAR should not be.
698 In the original code, we would have constructed a
699 temporary, and then the function body would have never
700 changed the value of P. However, now, we will be
701 constructing VAR directly. The constructor body may
702 change its value multiple times as it is being
703 constructed. Therefore, it must not be TREE_READONLY;
704 the back-end assumes that TREE_READONLY variable is
705 assigned to only once. */
706 TREE_READONLY (var
) = 0;
708 /* Build a run-time initialization. */
709 init_stmt
= build_stmt (EXPR_STMT
,
710 build (INIT_EXPR
, TREE_TYPE (p
),
712 /* Add this initialization to the list. Note that we want the
713 declaration *after* the initialization because we are going
714 to reverse all the initialization statements below. */
715 TREE_CHAIN (init_stmt
) = init_stmts
;
716 init_stmts
= init_stmt
;
719 /* See if we need to clean up the declaration. */
720 cleanup
= (*lang_hooks
.maybe_build_cleanup
) (var
);
724 /* Build the cleanup statement. */
725 cleanup_stmt
= build_stmt (CLEANUP_STMT
, var
, cleanup
);
726 /* Add it to the *front* of the list; the list will be
728 TREE_CHAIN (cleanup_stmt
) = init_stmts
;
729 init_stmts
= cleanup_stmt
;
731 #else /* INLINER_FOR_JAVA */
733 tree assignment
= build (MODIFY_EXPR
, TREE_TYPE (p
), var
, value
);
734 init_stmts
= add_stmt_to_compound (init_stmts
, TREE_TYPE (p
),
739 /* Java objects don't ever need constructing when being
740 passed as arguments because only call by reference is
744 #endif /* INLINER_FOR_JAVA */
747 #ifndef INLINER_FOR_JAVA
748 /* Evaluate trailing arguments. */
749 for (; a
; a
= TREE_CHAIN (a
))
752 tree value
= TREE_VALUE (a
);
754 if (! value
|| ! TREE_SIDE_EFFECTS (value
))
757 init_stmt
= build_stmt (EXPR_STMT
, value
);
758 TREE_CHAIN (init_stmt
) = init_stmts
;
759 init_stmts
= init_stmt
;
762 /* The initialization statements have been built up in reverse
763 order. Straighten them out now. */
764 return nreverse (init_stmts
);
765 #else /* INLINER_FOR_JAVA */
766 BLOCK_VARS (block
) = nreverse (vars
);
768 #endif /* INLINER_FOR_JAVA */
771 /* Declare a return variable to replace the RESULT_DECL for the
772 function we are calling. An appropriate DECL_STMT is returned.
773 The USE_STMT is filled in to contain a use of the declaration to
774 indicate the return value of the function. */
776 #ifndef INLINER_FOR_JAVA
778 declare_return_variable (id
, use_stmt
)
779 struct inline_data
*id
;
781 #else /* INLINER_FOR_JAVA */
783 declare_return_variable (id
, var
)
784 struct inline_data
*id
;
786 #endif /* INLINER_FOR_JAVA */
788 tree fn
= VARRAY_TOP_TREE (id
->fns
);
789 tree result
= DECL_RESULT (fn
);
790 #ifndef INLINER_FOR_JAVA
792 #endif /* not INLINER_FOR_JAVA */
793 int need_return_decl
= 1;
795 /* We don't need to do anything for functions that don't return
797 if (!result
|| VOID_TYPE_P (TREE_TYPE (result
)))
799 #ifndef INLINER_FOR_JAVA
800 *use_stmt
= NULL_TREE
;
801 #else /* INLINER_FOR_JAVA */
803 #endif /* INLINER_FOR_JAVA */
807 #ifndef INLINER_FOR_JAVA
808 var
= ((*lang_hooks
.tree_inlining
.copy_res_decl_for_inlining
)
809 (result
, fn
, VARRAY_TREE (id
->fns
, 0), id
->decl_map
,
810 &need_return_decl
, &id
->target_exprs
));
812 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
813 way, when the RESULT_DECL is encountered, it will be
814 automatically replaced by the VAR_DECL. */
815 splay_tree_insert (id
->decl_map
,
816 (splay_tree_key
) result
,
817 (splay_tree_value
) var
);
819 /* Build the USE_STMT. If the return type of the function was
820 promoted, convert it back to the expected type. */
821 if (TREE_TYPE (var
) == TREE_TYPE (TREE_TYPE (fn
)))
822 *use_stmt
= build_stmt (EXPR_STMT
, var
);
824 *use_stmt
= build_stmt (EXPR_STMT
,
825 build1 (NOP_EXPR
, TREE_TYPE (TREE_TYPE (fn
)),
827 TREE_ADDRESSABLE (*use_stmt
) = 1;
829 /* Build the declaration statement if FN does not return an
831 if (need_return_decl
)
832 return build_stmt (DECL_STMT
, var
);
833 #else /* INLINER_FOR_JAVA */
834 *var
= ((*lang_hooks
.tree_inlining
.copy_res_decl_for_inlining
)
835 (result
, fn
, VARRAY_TREE (id
->fns
, 0), id
->decl_map
,
836 &need_return_decl
, NULL_TREE
));
838 splay_tree_insert (id
->decl_map
,
839 (splay_tree_key
) result
,
840 (splay_tree_value
) *var
);
841 DECL_IGNORED_P (*var
) = 1;
842 if (need_return_decl
)
844 #endif /* INLINER_FOR_JAVA */
845 /* If FN does return an aggregate, there's no need to declare the
846 return variable; we're using a variable in our caller's frame. */
851 /* Returns nonzero if a function can be inlined as a tree. */
854 tree_inlinable_function_p (fn
)
857 return inlinable_function_p (fn
, NULL
);
860 /* Returns nonzero if FN is a function that can be inlined into the
861 inlining context ID_. If ID_ is NULL, check whether the function
862 can be inlined at all. */
865 inlinable_function_p (fn
, id
)
872 /* If we've already decided this function shouldn't be inlined,
873 there's no need to check again. */
874 if (DECL_UNINLINABLE (fn
))
877 /* Assume it is not inlinable. */
880 /* The number of instructions (estimated) of current function. */
881 currfn_insns
= DECL_NUM_STMTS (fn
) * INSNS_PER_STMT
;
883 /* If we're not inlining things, then nothing is inlinable. */
884 if (! flag_inline_trees
)
886 /* If we're not inlining all functions and the function was not
887 declared `inline', we don't inline it. Don't think of
888 disregarding DECL_INLINE when flag_inline_trees == 2; it's the
889 front-end that must set DECL_INLINE in this case, because
890 dwarf2out loses if a function is inlined that doesn't have
892 else if (! DECL_INLINE (fn
))
894 /* We can't inline functions that are too big. Only allow a single
895 function to be of MAX_INLINE_INSNS_SINGLE size. Make special
896 allowance for extern inline functions, though. */
897 else if (! (*lang_hooks
.tree_inlining
.disregard_inline_limits
) (fn
)
898 && currfn_insns
> MAX_INLINE_INSNS_SINGLE
)
900 /* All is well. We can inline this function. Traditionally, GCC
901 has refused to inline functions using alloca, or functions whose
902 values are returned in a PARALLEL, and a few other such obscure
903 conditions. We are not equally constrained at the tree level. */
907 /* Squirrel away the result so that we don't have to check again. */
908 DECL_UNINLINABLE (fn
) = ! inlinable
;
910 /* In case we don't disregard the inlining limits and we basically
911 can inline this function, investigate further. */
912 if (! (*lang_hooks
.tree_inlining
.disregard_inline_limits
) (fn
)
915 int sum_insns
= (id
? id
->inlined_stmts
: 0) * INSNS_PER_STMT
917 /* In the extreme case that we have exceeded the recursive inlining
918 limit by a huge factor (128), we just say no. Should not happen
920 if (sum_insns
> MAX_INLINE_INSNS
* 128)
922 /* If we did not hit the extreme limit, we use a linear function
923 with slope -1/MAX_INLINE_SLOPE to exceedingly decrease the
924 allowable size. We always allow a size of MIN_INLINE_INSNS
926 else if ((sum_insns
> MAX_INLINE_INSNS
)
927 && (currfn_insns
> MIN_INLINE_INSNS
))
929 int max_curr
= MAX_INLINE_INSNS_SINGLE
930 - (sum_insns
- MAX_INLINE_INSNS
) / MAX_INLINE_SLOPE
;
931 if (currfn_insns
> max_curr
)
936 if (inlinable
&& (*lang_hooks
.tree_inlining
.cannot_inline_tree_fn
) (&fn
))
939 /* If we don't have the function body available, we can't inline
941 if (! DECL_SAVED_TREE (fn
))
944 /* Check again, language hooks may have modified it. */
945 if (! inlinable
|| DECL_UNINLINABLE (fn
))
948 /* Don't do recursive inlining, either. We don't record this in
949 DECL_UNINLINABLE; we may be able to inline this function later. */
954 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (id
->fns
); ++i
)
955 if (VARRAY_TREE (id
->fns
, i
) == fn
)
958 if (DECL_INLINED_FNS (fn
))
961 tree inlined_fns
= DECL_INLINED_FNS (fn
);
963 for (j
= 0; j
< TREE_VEC_LENGTH (inlined_fns
); ++j
)
964 if (TREE_VEC_ELT (inlined_fns
, j
) == VARRAY_TREE (id
->fns
, 0))
969 /* Return the result. */
973 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
976 expand_call_inline (tp
, walk_subtrees
, data
)
985 #ifndef INLINER_FOR_JAVA
989 #else /* INLINER_FOR_JAVA */
991 #endif /* INLINER_FOR_JAVA */
997 /* See what we've got. */
998 id
= (inline_data
*) data
;
1001 /* Recurse, but letting recursive invocations know that we are
1002 inside the body of a TARGET_EXPR. */
1003 if (TREE_CODE (*tp
) == TARGET_EXPR
)
1005 #ifndef INLINER_FOR_JAVA
1006 int i
, len
= first_rtl_op (TARGET_EXPR
);
1008 /* We're walking our own subtrees. */
1011 /* Push *TP on the stack of pending TARGET_EXPRs. */
1012 VARRAY_PUSH_TREE (id
->target_exprs
, *tp
);
1014 /* Actually walk over them. This loop is the body of
1015 walk_trees, omitting the case where the TARGET_EXPR
1016 itself is handled. */
1017 for (i
= 0; i
< len
; ++i
)
1020 ++id
->in_target_cleanup_p
;
1021 walk_tree (&TREE_OPERAND (*tp
, i
), expand_call_inline
, data
,
1024 --id
->in_target_cleanup_p
;
1027 /* We're done with this TARGET_EXPR now. */
1028 VARRAY_POP (id
->target_exprs
);
1031 #else /* INLINER_FOR_JAVA */
1033 #endif /* INLINER_FOR_JAVA */
1037 /* Because types were not copied in copy_body, CALL_EXPRs beneath
1038 them should not be expanded. This can happen if the type is a
1039 dynamic array type, for example. */
1042 /* From here on, we're only interested in CALL_EXPRs. */
1043 if (TREE_CODE (t
) != CALL_EXPR
)
1046 /* First, see if we can figure out what function is being called.
1047 If we cannot, then there is no hope of inlining the function. */
1048 fn
= get_callee_fndecl (t
);
1052 /* If fn is a declaration of a function in a nested scope that was
1053 globally declared inline, we don't set its DECL_INITIAL.
1054 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1055 C++ front-end uses it for cdtors to refer to their internal
1056 declarations, that are not real functions. Fortunately those
1057 don't have trees to be saved, so we can tell by checking their
1059 if (! DECL_INITIAL (fn
)
1060 && DECL_ABSTRACT_ORIGIN (fn
)
1061 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn
)))
1062 fn
= DECL_ABSTRACT_ORIGIN (fn
);
1064 /* Don't try to inline functions that are not well-suited to
1066 if (!inlinable_function_p (fn
, id
))
1069 if (! (*lang_hooks
.tree_inlining
.start_inlining
) (fn
))
1072 /* Set the current filename and line number to the function we are
1073 inlining so that when we create new _STMT nodes here they get
1074 line numbers corresponding to the function we are calling. We
1075 wrap the whole inlined body in an EXPR_WITH_FILE_AND_LINE as well
1076 because individual statements don't record the filename. */
1077 push_srcloc (DECL_SOURCE_FILE (fn
), DECL_SOURCE_LINE (fn
));
1079 #ifndef INLINER_FOR_JAVA
1080 /* Build a statement-expression containing code to initialize the
1081 arguments, the actual inline expansion of the body, and a label
1082 for the return statements within the function to jump to. The
1083 type of the statement expression is the return type of the
1085 expr
= build1 (STMT_EXPR
, TREE_TYPE (TREE_TYPE (fn
)), make_node (COMPOUND_STMT
));
1086 /* There is no scope associated with the statement-expression. */
1087 STMT_EXPR_NO_SCOPE (expr
) = 1;
1088 stmt
= STMT_EXPR_STMT (expr
);
1089 #else /* INLINER_FOR_JAVA */
1090 /* Build a block containing code to initialize the arguments, the
1091 actual inline expansion of the body, and a label for the return
1092 statements within the function to jump to. The type of the
1093 statement expression is the return type of the function call. */
1095 expr
= build (BLOCK
, TREE_TYPE (TREE_TYPE (fn
)), stmt
);
1096 #endif /* INLINER_FOR_JAVA */
1098 /* Local declarations will be replaced by their equivalents in this
1101 id
->decl_map
= splay_tree_new (splay_tree_compare_pointers
,
1104 /* Initialize the parameters. */
1105 #ifndef INLINER_FOR_JAVA
1106 arg_inits
= initialize_inlined_parameters (id
, TREE_OPERAND (t
, 1), fn
);
1107 /* Expand any inlined calls in the initializers. Do this before we
1108 push FN on the stack of functions we are inlining; we want to
1109 inline calls to FN that appear in the initializers for the
1111 expand_calls_inline (&arg_inits
, id
);
1112 /* And add them to the tree. */
1113 COMPOUND_BODY (stmt
) = chainon (COMPOUND_BODY (stmt
), arg_inits
);
1114 #else /* INLINER_FOR_JAVA */
1115 arg_inits
= initialize_inlined_parameters (id
, TREE_OPERAND (t
, 1), fn
, expr
);
1118 /* Expand any inlined calls in the initializers. Do this before we
1119 push FN on the stack of functions we are inlining; we want to
1120 inline calls to FN that appear in the initializers for the
1122 expand_calls_inline (&arg_inits
, id
);
1124 /* And add them to the tree. */
1125 BLOCK_EXPR_BODY (expr
) = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
),
1126 TREE_TYPE (arg_inits
),
1129 #endif /* INLINER_FOR_JAVA */
1131 /* Record the function we are about to inline so that we can avoid
1132 recursing into it. */
1133 VARRAY_PUSH_TREE (id
->fns
, fn
);
1135 /* Record the function we are about to inline if optimize_function
1136 has not been called on it yet and we don't have it in the list. */
1137 if (! DECL_INLINED_FNS (fn
))
1141 for (i
= VARRAY_ACTIVE_SIZE (id
->inlined_fns
) - 1; i
>= 0; i
--)
1142 if (VARRAY_TREE (id
->inlined_fns
, i
) == fn
)
1145 VARRAY_PUSH_TREE (id
->inlined_fns
, fn
);
1148 /* Return statements in the function body will be replaced by jumps
1149 to the RET_LABEL. */
1150 id
->ret_label
= build_decl (LABEL_DECL
, NULL_TREE
, NULL_TREE
);
1151 DECL_CONTEXT (id
->ret_label
) = VARRAY_TREE (id
->fns
, 0);
1153 if (! DECL_INITIAL (fn
)
1154 || TREE_CODE (DECL_INITIAL (fn
)) != BLOCK
)
1157 #ifndef INLINER_FOR_JAVA
1158 /* Create a block to put the parameters in. We have to do this
1159 after the parameters have been remapped because remapping
1160 parameters is different from remapping ordinary variables. */
1161 scope_stmt
= build_stmt (SCOPE_STMT
, DECL_INITIAL (fn
));
1162 SCOPE_BEGIN_P (scope_stmt
) = 1;
1163 SCOPE_NO_CLEANUPS_P (scope_stmt
) = 1;
1164 remap_block (scope_stmt
, DECL_ARGUMENTS (fn
), id
);
1165 TREE_CHAIN (scope_stmt
) = COMPOUND_BODY (stmt
);
1166 COMPOUND_BODY (stmt
) = scope_stmt
;
1168 /* Tell the debugging backends that this block represents the
1169 outermost scope of the inlined function. */
1170 if (SCOPE_STMT_BLOCK (scope_stmt
))
1171 BLOCK_ABSTRACT_ORIGIN (SCOPE_STMT_BLOCK (scope_stmt
)) = DECL_ORIGIN (fn
);
1173 /* Declare the return variable for the function. */
1174 COMPOUND_BODY (stmt
)
1175 = chainon (COMPOUND_BODY (stmt
),
1176 declare_return_variable (id
, &use_stmt
));
1177 #else /* INLINER_FOR_JAVA */
1179 /* Declare the return variable for the function. */
1180 tree decl
= declare_return_variable (id
, &retvar
);
1183 tree
*next
= &BLOCK_VARS (expr
);
1185 next
= &TREE_CHAIN (*next
);
1189 #endif /* INLINER_FOR_JAVA */
1191 /* After we've initialized the parameters, we insert the body of the
1193 #ifndef INLINER_FOR_JAVA
1194 inlined_body
= &COMPOUND_BODY (stmt
);
1195 while (*inlined_body
)
1196 inlined_body
= &TREE_CHAIN (*inlined_body
);
1197 *inlined_body
= copy_body (id
);
1198 #else /* INLINER_FOR_JAVA */
1201 java_inlining_map_static_initializers (fn
, id
->decl_map
);
1202 new_body
= copy_body (id
);
1203 TREE_TYPE (new_body
) = TREE_TYPE (TREE_TYPE (fn
));
1204 BLOCK_EXPR_BODY (expr
)
1205 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
),
1206 TREE_TYPE (new_body
), new_body
);
1207 inlined_body
= &BLOCK_EXPR_BODY (expr
);
1209 #endif /* INLINER_FOR_JAVA */
1211 /* After the body of the function comes the RET_LABEL. This must come
1212 before we evaluate the returned value below, because that evalulation
1213 may cause RTL to be generated. */
1214 #ifndef INLINER_FOR_JAVA
1215 COMPOUND_BODY (stmt
)
1216 = chainon (COMPOUND_BODY (stmt
),
1217 build_stmt (LABEL_STMT
, id
->ret_label
));
1218 #else /* INLINER_FOR_JAVA */
1220 tree label
= build1 (LABEL_EXPR
, void_type_node
, id
->ret_label
);
1221 BLOCK_EXPR_BODY (expr
)
1222 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
), void_type_node
, label
);
1223 TREE_SIDE_EFFECTS (label
) = TREE_SIDE_EFFECTS (t
);
1225 #endif /* INLINER_FOR_JAVA */
1227 /* Finally, mention the returned value so that the value of the
1228 statement-expression is the returned value of the function. */
1229 #ifndef INLINER_FOR_JAVA
1230 COMPOUND_BODY (stmt
) = chainon (COMPOUND_BODY (stmt
), use_stmt
);
1232 /* Close the block for the parameters. */
1233 scope_stmt
= build_stmt (SCOPE_STMT
, DECL_INITIAL (fn
));
1234 SCOPE_NO_CLEANUPS_P (scope_stmt
) = 1;
1235 remap_block (scope_stmt
, NULL_TREE
, id
);
1236 COMPOUND_BODY (stmt
)
1237 = chainon (COMPOUND_BODY (stmt
), scope_stmt
);
1238 #else /* INLINER_FOR_JAVA */
1241 /* Mention the retvar. If the return type of the function was
1242 promoted, convert it back to the expected type. */
1243 if (TREE_TYPE (TREE_TYPE (fn
)) != TREE_TYPE (retvar
))
1244 retvar
= build1 (NOP_EXPR
, TREE_TYPE (TREE_TYPE (fn
)), retvar
);
1245 BLOCK_EXPR_BODY (expr
)
1246 = add_stmt_to_compound (BLOCK_EXPR_BODY (expr
),
1247 TREE_TYPE (retvar
), retvar
);
1250 java_inlining_merge_static_initializers (fn
, id
->decl_map
);
1251 #endif /* INLINER_FOR_JAVA */
1254 splay_tree_delete (id
->decl_map
);
1257 /* The new expression has side-effects if the old one did. */
1258 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (t
);
1260 /* Replace the call by the inlined body. Wrap it in an
1261 EXPR_WITH_FILE_LOCATION so that we'll get debugging line notes
1262 pointing to the right place. */
1263 #ifndef INLINER_FOR_JAVA
1264 chain
= TREE_CHAIN (*tp
);
1265 *tp
= build_expr_wfl (expr
, DECL_SOURCE_FILE (fn
), DECL_SOURCE_LINE (fn
),
1267 #else /* INLINER_FOR_JAVA */
1268 *tp
= build_expr_wfl (expr
, DECL_SOURCE_FILE (fn
),
1269 DECL_SOURCE_LINE_FIRST(fn
),
1271 #endif /* INLINER_FOR_JAVA */
1272 EXPR_WFL_EMIT_LINE_NOTE (*tp
) = 1;
1273 #ifndef INLINER_FOR_JAVA
1274 TREE_CHAIN (*tp
) = chain
;
1275 #endif /* not INLINER_FOR_JAVA */
1278 /* If the value of the new expression is ignored, that's OK. We
1279 don't warn about this for CALL_EXPRs, so we shouldn't warn about
1280 the equivalent inlined version either. */
1281 TREE_USED (*tp
) = 1;
1283 /* Our function now has more statements than it did before. */
1284 DECL_NUM_STMTS (VARRAY_TREE (id
->fns
, 0)) += DECL_NUM_STMTS (fn
);
1285 /* For accounting, subtract one for the saved call/ret. */
1286 id
->inlined_stmts
+= DECL_NUM_STMTS (fn
) - 1;
1288 /* Recurse into the body of the just inlined function. */
1289 expand_calls_inline (inlined_body
, id
);
1290 VARRAY_POP (id
->fns
);
1292 /* If we've returned to the top level, clear out the record of how
1293 much inlining has been done. */
1294 if (VARRAY_ACTIVE_SIZE (id
->fns
) == id
->first_inlined_fn
)
1295 id
->inlined_stmts
= 0;
1297 /* Don't walk into subtrees. We've already handled them above. */
1300 (*lang_hooks
.tree_inlining
.end_inlining
) (fn
);
1302 /* Keep iterating. */
1305 /* Walk over the entire tree *TP, replacing CALL_EXPRs with inline
1306 expansions as appropriate. */
1309 expand_calls_inline (tp
, id
)
1313 /* Search through *TP, replacing all calls to inline functions by
1314 appropriate equivalents. Use walk_tree in no-duplicates mode
1315 to avoid exponential time complexity. (We can't just use
1316 walk_tree_without_duplicates, because of the special TARGET_EXPR
1317 handling in expand_calls. The hash table is set up in
1318 optimize_function. */
1319 walk_tree (tp
, expand_call_inline
, id
, id
->tree_pruner
);
1322 /* Expand calls to inline functions in the body of FN. */
1325 optimize_inline_calls (fn
)
1332 memset (&id
, 0, sizeof (id
));
1334 /* Don't allow recursion into FN. */
1335 VARRAY_TREE_INIT (id
.fns
, 32, "fns");
1336 VARRAY_PUSH_TREE (id
.fns
, fn
);
1337 /* Or any functions that aren't finished yet. */
1338 prev_fn
= NULL_TREE
;
1339 if (current_function_decl
)
1341 VARRAY_PUSH_TREE (id
.fns
, current_function_decl
);
1342 prev_fn
= current_function_decl
;
1345 prev_fn
= ((*lang_hooks
.tree_inlining
.add_pending_fn_decls
)
1346 (&id
.fns
, prev_fn
));
1348 /* Create the stack of TARGET_EXPRs. */
1349 VARRAY_TREE_INIT (id
.target_exprs
, 32, "target_exprs");
1351 /* Create the list of functions this call will inline. */
1352 VARRAY_TREE_INIT (id
.inlined_fns
, 32, "inlined_fns");
1354 /* Keep track of the low-water mark, i.e., the point where the first
1355 real inlining is represented in ID.FNS. */
1356 id
.first_inlined_fn
= VARRAY_ACTIVE_SIZE (id
.fns
);
1358 /* Replace all calls to inline functions with the bodies of those
1360 id
.tree_pruner
= htab_create (37, htab_hash_pointer
,
1361 htab_eq_pointer
, NULL
);
1362 expand_calls_inline (&DECL_SAVED_TREE (fn
), &id
);
1365 htab_delete (id
.tree_pruner
);
1366 if (DECL_LANG_SPECIFIC (fn
))
1368 tree ifn
= make_tree_vec (VARRAY_ACTIVE_SIZE (id
.inlined_fns
));
1370 memcpy (&TREE_VEC_ELT (ifn
, 0), &VARRAY_TREE (id
.inlined_fns
, 0),
1371 VARRAY_ACTIVE_SIZE (id
.inlined_fns
) * sizeof (tree
));
1372 DECL_INLINED_FNS (fn
) = ifn
;
1376 /* FN is a function that has a complete body, and CLONE is a function
1377 whose body is to be set to a copy of FN, mapping argument
1378 declarations according to the ARG_MAP splay_tree. */
1381 clone_body (clone
, fn
, arg_map
)
1387 /* Clone the body, as if we were making an inline call. But, remap
1388 the parameters in the callee to the parameters of caller. If
1389 there's an in-charge parameter, map it to an appropriate
1391 memset (&id
, 0, sizeof (id
));
1392 VARRAY_TREE_INIT (id
.fns
, 2, "fns");
1393 VARRAY_PUSH_TREE (id
.fns
, clone
);
1394 VARRAY_PUSH_TREE (id
.fns
, fn
);
1395 id
.decl_map
= (splay_tree
)arg_map
;
1397 /* Cloning is treated slightly differently from inlining. Set
1398 CLONING_P so that it's clear which operation we're performing. */
1399 id
.cloning_p
= true;
1401 /* Actually copy the body. */
1402 TREE_CHAIN (DECL_SAVED_TREE (clone
)) = copy_body (&id
);
1405 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1406 FUNC is called with the DATA and the address of each sub-tree. If
1407 FUNC returns a non-NULL value, the traversal is aborted, and the
1408 value returned by FUNC is returned. If HTAB is non-NULL it is used
1409 to record the nodes visited, and to avoid visiting a node more than
1413 walk_tree (tp
, func
, data
, htab_
)
1419 htab_t htab
= (htab_t
) htab_
;
1420 enum tree_code code
;
1424 #define WALK_SUBTREE(NODE) \
1427 result = walk_tree (&(NODE), func, data, htab); \
1433 #define WALK_SUBTREE_TAIL(NODE) \
1437 goto tail_recurse; \
1442 /* Skip empty subtrees. */
1450 /* Don't walk the same tree twice, if the user has requested
1451 that we avoid doing so. */
1452 if (htab_find (htab
, *tp
))
1454 /* If we haven't already seen this node, add it to the table. */
1455 slot
= htab_find_slot (htab
, *tp
, INSERT
);
1459 /* Call the function. */
1461 result
= (*func
) (tp
, &walk_subtrees
, data
);
1463 /* If we found something, return it. */
1467 code
= TREE_CODE (*tp
);
1469 #ifndef INLINER_FOR_JAVA
1470 /* Even if we didn't, FUNC may have decided that there was nothing
1471 interesting below this point in the tree. */
1474 if (statement_code_p (code
) || code
== TREE_LIST
1475 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
))
1476 /* But we still need to check our siblings. */
1477 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp
));
1482 /* Handle common cases up front. */
1483 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
1484 || TREE_CODE_CLASS (code
) == 'r'
1485 || TREE_CODE_CLASS (code
) == 's')
1486 #else /* INLINER_FOR_JAVA */
1487 if (code
!= EXIT_BLOCK_EXPR
1488 && code
!= SAVE_EXPR
1489 && (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
1490 || TREE_CODE_CLASS (code
) == 'r'
1491 || TREE_CODE_CLASS (code
) == 's'))
1492 #endif /* INLINER_FOR_JAVA */
1496 #ifndef INLINER_FOR_JAVA
1497 /* Set lineno here so we get the right instantiation context
1498 if we call instantiate_decl from inlinable_function_p. */
1499 if (statement_code_p (code
) && !STMT_LINENO_FOR_FN_P (*tp
))
1500 lineno
= STMT_LINENO (*tp
);
1501 #endif /* not INLINER_FOR_JAVA */
1503 /* Walk over all the sub-trees of this operand. */
1504 len
= first_rtl_op (code
);
1505 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1506 But, we only want to walk once. */
1507 if (code
== TARGET_EXPR
1508 && TREE_OPERAND (*tp
, 3) == TREE_OPERAND (*tp
, 1))
1510 /* Go through the subtrees. We need to do this in forward order so
1511 that the scope of a FOR_EXPR is handled properly. */
1512 for (i
= 0; i
< len
; ++i
)
1513 WALK_SUBTREE (TREE_OPERAND (*tp
, i
));
1515 #ifndef INLINER_FOR_JAVA
1516 /* For statements, we also walk the chain so that we cover the
1517 entire statement tree. */
1518 if (statement_code_p (code
))
1520 if (code
== DECL_STMT
1521 && DECL_STMT_DECL (*tp
)
1522 && DECL_P (DECL_STMT_DECL (*tp
)))
1524 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
1525 into declarations that are just mentioned, rather than
1526 declared; they don't really belong to this part of the tree.
1527 And, we can see cycles: the initializer for a declaration can
1528 refer to the declaration itself. */
1529 WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp
)));
1530 WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp
)));
1531 WALK_SUBTREE (DECL_SIZE_UNIT (DECL_STMT_DECL (*tp
)));
1534 /* This can be tail-recursion optimized if we write it this way. */
1535 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp
));
1538 #endif /* not INLINER_FOR_JAVA */
1539 /* We didn't find what we were looking for. */
1542 else if (TREE_CODE_CLASS (code
) == 'd')
1544 WALK_SUBTREE_TAIL (TREE_TYPE (*tp
));
1546 else if (TREE_CODE_CLASS (code
) == 't')
1548 WALK_SUBTREE (TYPE_SIZE (*tp
));
1549 WALK_SUBTREE (TYPE_SIZE_UNIT (*tp
));
1550 /* Also examine various special fields, below. */
1553 result
= (*lang_hooks
.tree_inlining
.walk_subtrees
) (tp
, &walk_subtrees
, func
,
1555 if (result
|| ! walk_subtrees
)
1558 /* Not one of the easy cases. We must explicitly go through the
1563 case IDENTIFIER_NODE
:
1577 /* None of thse have subtrees other than those already walked
1582 case REFERENCE_TYPE
:
1583 WALK_SUBTREE_TAIL (TREE_TYPE (*tp
));
1587 WALK_SUBTREE (TREE_VALUE (*tp
));
1588 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp
));
1593 int len
= TREE_VEC_LENGTH (*tp
);
1598 /* Walk all elements but the first. */
1600 WALK_SUBTREE (TREE_VEC_ELT (*tp
, len
));
1602 /* Now walk the first one as a tail call. */
1603 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp
, 0));
1607 WALK_SUBTREE (TREE_REALPART (*tp
));
1608 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp
));
1611 WALK_SUBTREE_TAIL (CONSTRUCTOR_ELTS (*tp
));
1614 WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp
));
1618 WALK_SUBTREE (TREE_TYPE (*tp
));
1620 tree arg
= TYPE_ARG_TYPES (*tp
);
1622 /* We never want to walk into default arguments. */
1623 for (; arg
; arg
= TREE_CHAIN (arg
))
1624 WALK_SUBTREE (TREE_VALUE (arg
));
1629 WALK_SUBTREE (TREE_TYPE (*tp
));
1630 WALK_SUBTREE_TAIL (TYPE_DOMAIN (*tp
));
1633 WALK_SUBTREE (TYPE_MIN_VALUE (*tp
));
1634 WALK_SUBTREE_TAIL (TYPE_MAX_VALUE (*tp
));
1637 WALK_SUBTREE (TREE_TYPE (*tp
));
1638 WALK_SUBTREE_TAIL (TYPE_OFFSET_BASETYPE (*tp
));
1640 #ifdef INLINER_FOR_JAVA
1641 case EXIT_BLOCK_EXPR
:
1642 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp
, 1));
1645 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp
, 0));
1646 #endif /* INLINER_FOR_JAVA */
1652 /* We didn't find what we were looking for. */
1656 #undef WALK_SUBTREE_TAIL
1659 /* Like walk_tree, but does not walk duplicate nodes more than
1663 walk_tree_without_duplicates (tp
, func
, data
)
1671 htab
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1672 result
= walk_tree (tp
, func
, data
, htab
);
1677 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
1680 copy_tree_r (tp
, walk_subtrees
, data
)
1683 void *data ATTRIBUTE_UNUSED
;
1685 enum tree_code code
= TREE_CODE (*tp
);
1687 /* We make copies of most nodes. */
1688 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
1689 || TREE_CODE_CLASS (code
) == 'r'
1690 || TREE_CODE_CLASS (code
) == 'c'
1691 || TREE_CODE_CLASS (code
) == 's'
1692 || code
== TREE_LIST
1694 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
))
1696 /* Because the chain gets clobbered when we make a copy, we save it
1698 tree chain
= TREE_CHAIN (*tp
);
1700 /* Copy the node. */
1701 *tp
= copy_node (*tp
);
1703 /* Now, restore the chain, if appropriate. That will cause
1704 walk_tree to walk into the chain as well. */
1705 if (code
== PARM_DECL
|| code
== TREE_LIST
1706 #ifndef INLINER_FOR_JAVA
1707 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
)
1708 || statement_code_p (code
))
1709 TREE_CHAIN (*tp
) = chain
;
1711 /* For now, we don't update BLOCKs when we make copies. So, we
1712 have to nullify all scope-statements. */
1713 if (TREE_CODE (*tp
) == SCOPE_STMT
)
1714 SCOPE_STMT_BLOCK (*tp
) = NULL_TREE
;
1715 #else /* INLINER_FOR_JAVA */
1716 || (*lang_hooks
.tree_inlining
.tree_chain_matters_p
) (*tp
))
1717 TREE_CHAIN (*tp
) = chain
;
1718 #endif /* INLINER_FOR_JAVA */
1720 else if (TREE_CODE_CLASS (code
) == 't' && !variably_modified_type_p (*tp
))
1721 /* Types only need to be copied if they are variably modified. */
1727 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
1728 information indicating to what new SAVE_EXPR this one should be
1729 mapped, use that one. Otherwise, create a new node and enter it in
1730 ST. FN is the function into which the copy will be placed. */
1733 remap_save_expr (tp
, st_
, fn
, walk_subtrees
)
1739 splay_tree st
= (splay_tree
) st_
;
1742 /* See if we already encountered this SAVE_EXPR. */
1743 n
= splay_tree_lookup (st
, (splay_tree_key
) *tp
);
1745 /* If we didn't already remap this SAVE_EXPR, do so now. */
1748 tree t
= copy_node (*tp
);
1750 /* The SAVE_EXPR is now part of the function into which we
1751 are inlining this body. */
1752 SAVE_EXPR_CONTEXT (t
) = fn
;
1753 /* And we haven't evaluated it yet. */
1754 SAVE_EXPR_RTL (t
) = NULL_RTX
;
1755 /* Remember this SAVE_EXPR. */
1756 n
= splay_tree_insert (st
,
1757 (splay_tree_key
) *tp
,
1758 (splay_tree_value
) t
);
1759 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
1760 splay_tree_insert (st
, (splay_tree_key
) t
,
1761 (splay_tree_value
) error_mark_node
);
1764 /* We've already walked into this SAVE_EXPR, so we needn't do it
1768 /* Replace this SAVE_EXPR with the copy. */
1769 *tp
= (tree
) n
->value
;
1772 #ifdef INLINER_FOR_JAVA
1773 /* Add STMT to EXISTING if possible, otherwise create a new
1774 COMPOUND_EXPR and add STMT to it. */
1777 add_stmt_to_compound (existing
, type
, stmt
)
1778 tree existing
, type
, stmt
;
1783 return build (COMPOUND_EXPR
, type
, existing
, stmt
);
1788 #endif /* INLINER_FOR_JAVA */