2 Copyright 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
28 #include "tree-inline.h"
34 #include "insn-config.h"
37 #include "splay-tree.h"
38 #include "langhooks.h"
39 #include "basic-block.h"
40 #include "tree-iterator.h"
43 #include "tree-mudflap.h"
44 #include "tree-flow.h"
47 #include "tree-flow.h"
48 #include "diagnostic.h"
51 #include "pointer-set.h"
52 #include "integrate.h"
54 /* I'm not real happy about this, but we need to handle gimple and
56 #include "tree-gimple.h"
58 /* Inlining, Saving, Cloning
60 Inlining: a function body is duplicated, but the PARM_DECLs are
61 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
62 MODIFY_EXPRs that store to a dedicated returned-value variable.
63 The duplicated eh_region info of the copy will later be appended
64 to the info for the caller; the eh_region info in copied throwing
65 statements and RESX_EXPRs is adjusted accordingly.
67 Saving: make a semantically-identical copy of the function body.
68 Necessary when we want to generate code for the body (a destructive
69 operation), but we expect to need this body in the future (e.g. for
70 inlining into another function).
72 Cloning: (only in C++) We have one body for a con/de/structor, and
73 multiple function decls, each with a unique parameter list.
74 Duplicate the body, using the given splay tree; some parameters
75 will become constants (like 0 or 1).
77 All of these will simultaneously lookup any callgraph edges. If
78 we're going to inline the duplicated function body, and the given
79 function has some cloned callgraph nodes (one for each place this
80 function will be inlined) those callgraph edges will be duplicated.
81 If we're saving or cloning the body, those callgraph edges will be
82 updated to point into the new body. (Note that the original
83 callgraph node and edge list will not be altered.)
85 See the CALL_EXPR handling case in copy_body_r (). */
87 /* 0 if we should not perform inlining.
88 1 if we should expand functions calls inline at the tree level.
89 2 if we should consider *all* functions to be inline
92 int flag_inline_trees
= 0;
96 o In order to make inlining-on-trees work, we pessimized
97 function-local static constants. In particular, they are now
98 always output, even when not addressed. Fix this by treating
99 function-local static constants just like global static
100 constants; the back-end already knows not to output them if they
103 o Provide heuristics to clamp inlining of recursive template
106 /* Data required for function inlining. */
108 typedef struct inline_data
110 /* FUNCTION_DECL for function being inlined. */
112 /* FUNCTION_DECL for function being inlined into. */
114 /* struct function for function being inlined. Usually this is the same
115 as DECL_STRUCT_FUNCTION (callee), but can be different if saved_cfg
116 and saved_eh are in use. */
117 struct function
*callee_cfun
;
118 /* The VAR_DECL for the return value. */
120 /* The map from local declarations in the inlined function to
121 equivalents in the function into which it is being inlined. */
123 /* We use the same mechanism to build clones that we do to perform
124 inlining. However, there are a few places where we need to
125 distinguish between those two situations. This flag is true if
126 we are cloning, rather than inlining. */
128 /* Similarly for saving function body. */
130 /* Callgraph node of function we are inlining into. */
131 struct cgraph_node
*node
;
132 /* Callgraph node of currently inlined function. */
133 struct cgraph_node
*current_node
;
136 /* Exception region the inlined call lie in. */
138 /* Take region number in the function being copied, add this value and
139 get eh region number of the duplicate in the function we inline into. */
140 int eh_region_offset
;
145 static tree
declare_return_variable (inline_data
*, tree
, tree
, tree
*);
146 static tree
copy_body_r (tree
*, int *, void *);
147 static tree
copy_generic_body (inline_data
*);
148 static bool inlinable_function_p (tree
);
149 static tree
remap_decl (tree
, inline_data
*);
150 static tree
remap_type (tree
, inline_data
*);
151 static void remap_block (tree
*, inline_data
*);
152 static tree
remap_decl (tree
, inline_data
*);
153 static tree
remap_decls (tree
, inline_data
*);
154 static void copy_bind_expr (tree
*, int *, inline_data
*);
155 static tree
mark_local_for_remap_r (tree
*, int *, void *);
156 static void unsave_expr_1 (tree
);
157 static tree
unsave_r (tree
*, int *, void *);
158 static void declare_inline_vars (tree
, tree
);
159 static void remap_save_expr (tree
*, void *, int *);
161 static inline bool inlining_p (inline_data
*id
);
163 /* Insert a tree->tree mapping for ID. Despite the name suggests
164 that the trees should be variables, it is used for more than that. */
167 insert_decl_map (inline_data
*id
, tree key
, tree value
)
169 splay_tree_insert (id
->decl_map
, (splay_tree_key
) key
,
170 (splay_tree_value
) value
);
172 /* Always insert an identity map as well. If we see this same new
173 node again, we won't want to duplicate it a second time. */
175 splay_tree_insert (id
->decl_map
, (splay_tree_key
) value
,
176 (splay_tree_value
) value
);
179 /* Remap DECL during the copying of the BLOCK tree for the function. */
182 remap_decl (tree decl
, inline_data
*id
)
187 /* We only remap local variables in the current function. */
190 /* See if we have remapped this declaration. */
192 n
= splay_tree_lookup (id
->decl_map
, (splay_tree_key
) decl
);
194 /* If we didn't already have an equivalent for this declaration,
198 /* Make a copy of the variable or label. */
200 t
= copy_decl_for_inlining (decl
, fn
, id
->caller
);
202 /* Remember it, so that if we encounter this local entity again
203 we can reuse this copy. Do this early because remap_type may
204 need this decl for TYPE_STUB_DECL. */
205 insert_decl_map (id
, decl
, t
);
207 /* Remap types, if necessary. */
208 TREE_TYPE (t
) = remap_type (TREE_TYPE (t
), id
);
209 if (TREE_CODE (t
) == TYPE_DECL
)
210 DECL_ORIGINAL_TYPE (t
) = remap_type (DECL_ORIGINAL_TYPE (t
), id
);
211 else if (TREE_CODE (t
) == PARM_DECL
)
212 DECL_ARG_TYPE_AS_WRITTEN (t
)
213 = remap_type (DECL_ARG_TYPE_AS_WRITTEN (t
), id
);
215 /* Remap sizes as necessary. */
216 walk_tree (&DECL_SIZE (t
), copy_body_r
, id
, NULL
);
217 walk_tree (&DECL_SIZE_UNIT (t
), copy_body_r
, id
, NULL
);
219 /* If fields, do likewise for offset and qualifier. */
220 if (TREE_CODE (t
) == FIELD_DECL
)
222 walk_tree (&DECL_FIELD_OFFSET (t
), copy_body_r
, id
, NULL
);
223 if (TREE_CODE (DECL_CONTEXT (t
)) == QUAL_UNION_TYPE
)
224 walk_tree (&DECL_QUALIFIER (t
), copy_body_r
, id
, NULL
);
228 /* FIXME handle anon aggrs. */
229 if (! DECL_NAME (t
) && TREE_TYPE (t
)
230 && lang_hooks
.tree_inlining
.anon_aggr_type_p (TREE_TYPE (t
)))
232 /* For a VAR_DECL of anonymous type, we must also copy the
233 member VAR_DECLS here and rechain the DECL_ANON_UNION_ELEMS. */
237 for (src
= DECL_ANON_UNION_ELEMS (t
); src
;
238 src
= TREE_CHAIN (src
))
240 tree member
= remap_decl (TREE_VALUE (src
), id
);
242 gcc_assert (!TREE_PURPOSE (src
));
243 members
= tree_cons (NULL
, member
, members
);
245 DECL_ANON_UNION_ELEMS (t
) = nreverse (members
);
249 /* If we are inlining and this is a variable (not a label), declare the
250 remapped variable in the callers' body. */
252 && (TREE_CODE (t
) == VAR_DECL
253 || TREE_CODE (t
) == PARM_DECL
))
254 declare_inline_vars (id
->block
, t
);
256 /* Remember it, so that if we encounter this local entity
257 again we can reuse this copy. */
258 insert_decl_map (id
, decl
, t
);
262 return unshare_expr ((tree
) n
->value
);
266 remap_type (tree type
, inline_data
*id
)
268 splay_tree_node node
;
274 /* See if we have remapped this type. */
275 node
= splay_tree_lookup (id
->decl_map
, (splay_tree_key
) type
);
277 return (tree
) node
->value
;
279 /* The type only needs remapping if it's variably modified. */
280 if (! variably_modified_type_p (type
, id
->callee
))
282 insert_decl_map (id
, type
, type
);
286 /* We do need a copy. build and register it now. If this is a pointer or
287 reference type, remap the designated type and make a new pointer or
289 if (TREE_CODE (type
) == POINTER_TYPE
)
291 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type
), id
),
293 TYPE_REF_CAN_ALIAS_ALL (type
));
294 insert_decl_map (id
, type
, new);
297 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
299 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type
), id
),
301 TYPE_REF_CAN_ALIAS_ALL (type
));
302 insert_decl_map (id
, type
, new);
306 new = copy_node (type
);
308 insert_decl_map (id
, type
, new);
310 /* This is a new type, not a copy of an old type. Need to reassociate
311 variants. We can handle everything except the main variant lazily. */
312 t
= TYPE_MAIN_VARIANT (type
);
315 t
= remap_type (t
, id
);
316 TYPE_MAIN_VARIANT (new) = t
;
317 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t
);
318 TYPE_NEXT_VARIANT (t
) = new;
322 TYPE_MAIN_VARIANT (new) = new;
323 TYPE_NEXT_VARIANT (new) = NULL
;
326 if (TYPE_STUB_DECL (type
))
327 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type
), id
);
329 /* Lazily create pointer and reference types. */
330 TYPE_POINTER_TO (new) = NULL
;
331 TYPE_REFERENCE_TO (new) = NULL
;
333 switch (TREE_CODE (new))
340 t
= TYPE_MIN_VALUE (new);
341 if (t
&& TREE_CODE (t
) != INTEGER_CST
)
342 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r
, id
, NULL
);
344 t
= TYPE_MAX_VALUE (new);
345 if (t
&& TREE_CODE (t
) != INTEGER_CST
)
346 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r
, id
, NULL
);
350 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id
);
351 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r
, id
, NULL
);
355 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id
);
356 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id
);
361 case QUAL_UNION_TYPE
:
362 walk_tree (&TYPE_FIELDS (new), copy_body_r
, id
, NULL
);
367 /* Shouldn't have been thought variable sized. */
371 walk_tree (&TYPE_SIZE (new), copy_body_r
, id
, NULL
);
372 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r
, id
, NULL
);
378 remap_decls (tree decls
, inline_data
*id
)
381 tree new_decls
= NULL_TREE
;
383 /* Remap its variables. */
384 for (old_var
= decls
; old_var
; old_var
= TREE_CHAIN (old_var
))
388 /* We can not chain the local static declarations into the unexpanded_var_list
389 as we can't duplicate them or break one decl rule. Go ahead and link
390 them into unexpanded_var_list. */
391 if (!lang_hooks
.tree_inlining
.auto_var_in_fn_p (old_var
, id
->callee
)
392 && !DECL_EXTERNAL (old_var
))
394 cfun
->unexpanded_var_list
= tree_cons (NULL_TREE
, old_var
,
395 cfun
->unexpanded_var_list
);
399 /* Remap the variable. */
400 new_var
= remap_decl (old_var
, id
);
402 /* If we didn't remap this variable, so we can't mess with its
403 TREE_CHAIN. If we remapped this variable to the return slot, it's
404 already declared somewhere else, so don't declare it here. */
405 if (!new_var
|| new_var
== id
->retvar
)
409 gcc_assert (DECL_P (new_var
));
410 TREE_CHAIN (new_var
) = new_decls
;
415 return nreverse (new_decls
);
418 /* Copy the BLOCK to contain remapped versions of the variables
419 therein. And hook the new block into the block-tree. */
422 remap_block (tree
*block
, inline_data
*id
)
428 /* Make the new block. */
430 new_block
= make_node (BLOCK
);
431 TREE_USED (new_block
) = TREE_USED (old_block
);
432 BLOCK_ABSTRACT_ORIGIN (new_block
) = old_block
;
435 /* Remap its variables. */
436 BLOCK_VARS (new_block
) = remap_decls (BLOCK_VARS (old_block
), id
);
440 /* FIXME! It shouldn't be so hard to manage blocks. Rebuilding them in
441 rest_of_compilation is a good start. */
443 /* We're building a clone; DECL_INITIAL is still
444 error_mark_node, and current_binding_level is the parm
446 lang_hooks
.decls
.insert_block (new_block
);
449 /* Attach this new block after the DECL_INITIAL block for the
450 function into which this block is being inlined. In
451 rest_of_compilation we will straighten out the BLOCK tree. */
453 if (DECL_INITIAL (fn
))
454 first_block
= &BLOCK_CHAIN (DECL_INITIAL (fn
));
456 first_block
= &DECL_INITIAL (fn
);
457 BLOCK_CHAIN (new_block
) = *first_block
;
458 *first_block
= new_block
;
461 /* Remember the remapped block. */
462 insert_decl_map (id
, old_block
, new_block
);
466 copy_statement_list (tree
*tp
)
468 tree_stmt_iterator oi
, ni
;
471 new = alloc_stmt_list ();
472 ni
= tsi_start (new);
473 oi
= tsi_start (*tp
);
476 for (; !tsi_end_p (oi
); tsi_next (&oi
))
477 tsi_link_after (&ni
, tsi_stmt (oi
), TSI_NEW_STMT
);
481 copy_bind_expr (tree
*tp
, int *walk_subtrees
, inline_data
*id
)
483 tree block
= BIND_EXPR_BLOCK (*tp
);
484 /* Copy (and replace) the statement. */
485 copy_tree_r (tp
, walk_subtrees
, NULL
);
488 remap_block (&block
, id
);
489 BIND_EXPR_BLOCK (*tp
) = block
;
492 if (BIND_EXPR_VARS (*tp
))
493 /* This will remap a lot of the same decls again, but this should be
495 BIND_EXPR_VARS (*tp
) = remap_decls (BIND_EXPR_VARS (*tp
), id
);
498 /* Called from copy_body_id via walk_tree. DATA is really an
502 copy_body_r (tree
*tp
, int *walk_subtrees
, void *data
)
504 inline_data
*id
= (inline_data
*) data
;
505 tree fn
= id
->callee
;
507 /* Begin by recognizing trees that we'll completely rewrite for the
508 inlining context. Our output for these trees is completely
509 different from out input (e.g. RETURN_EXPR is deleted, and morphs
510 into an edge). Further down, we'll handle trees that get
511 duplicated and/or tweaked. */
513 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
514 GOTO_STMT with the RET_LABEL as its target. */
515 if (TREE_CODE (*tp
) == RETURN_EXPR
&& inlining_p (id
))
517 tree assignment
= TREE_OPERAND (*tp
, 0);
519 /* If we're returning something, just turn that into an
520 assignment into the equivalent of the original RESULT_DECL.
521 If the "assignment" is just the result decl, the result
522 decl has already been set (e.g. a recent "foo (&result_decl,
523 ...)"); just toss the entire RETURN_EXPR. */
524 if (assignment
&& TREE_CODE (assignment
) == MODIFY_EXPR
)
526 /* Replace the RETURN_EXPR with (a copy of) the
527 MODIFY_EXPR hanging underneath. */
528 *tp
= copy_node (assignment
);
530 else /* Else the RETURN_EXPR returns no value. */
537 /* Local variables and labels need to be replaced by equivalent
538 variables. We don't want to copy static variables; there's only
539 one of those, no matter how many times we inline the containing
540 function. Similarly for globals from an outer function. */
541 else if (lang_hooks
.tree_inlining
.auto_var_in_fn_p (*tp
, fn
))
545 /* Remap the declaration. */
546 new_decl
= remap_decl (*tp
, id
);
547 gcc_assert (new_decl
);
548 /* Replace this variable with the copy. */
549 STRIP_TYPE_NOPS (new_decl
);
553 else if (TREE_CODE (*tp
) == STATEMENT_LIST
)
554 copy_statement_list (tp
);
555 else if (TREE_CODE (*tp
) == SAVE_EXPR
)
556 remap_save_expr (tp
, id
->decl_map
, walk_subtrees
);
557 else if (TREE_CODE (*tp
) == LABEL_DECL
558 && (! DECL_CONTEXT (*tp
)
559 || decl_function_context (*tp
) == id
->callee
))
560 /* These may need to be remapped for EH handling. */
561 *tp
= remap_decl (*tp
, id
);
562 else if (TREE_CODE (*tp
) == BIND_EXPR
)
563 copy_bind_expr (tp
, walk_subtrees
, id
);
564 /* Types may need remapping as well. */
565 else if (TYPE_P (*tp
))
566 *tp
= remap_type (*tp
, id
);
568 /* If this is a constant, we have to copy the node iff the type will be
569 remapped. copy_tree_r will not copy a constant. */
570 else if (CONSTANT_CLASS_P (*tp
))
572 tree new_type
= remap_type (TREE_TYPE (*tp
), id
);
574 if (new_type
== TREE_TYPE (*tp
))
577 else if (TREE_CODE (*tp
) == INTEGER_CST
)
578 *tp
= build_int_cst_wide (new_type
, TREE_INT_CST_LOW (*tp
),
579 TREE_INT_CST_HIGH (*tp
));
582 *tp
= copy_node (*tp
);
583 TREE_TYPE (*tp
) = new_type
;
587 /* Otherwise, just copy the node. Note that copy_tree_r already
588 knows not to copy VAR_DECLs, etc., so this is safe. */
591 /* Here we handle trees that are not completely rewritten.
592 First we detect some inlining-induced bogosities for
594 if (TREE_CODE (*tp
) == MODIFY_EXPR
595 && TREE_OPERAND (*tp
, 0) == TREE_OPERAND (*tp
, 1)
596 && (lang_hooks
.tree_inlining
.auto_var_in_fn_p
597 (TREE_OPERAND (*tp
, 0), fn
)))
599 /* Some assignments VAR = VAR; don't generate any rtl code
600 and thus don't count as variable modification. Avoid
601 keeping bogosities like 0 = 0. */
602 tree decl
= TREE_OPERAND (*tp
, 0), value
;
605 n
= splay_tree_lookup (id
->decl_map
, (splay_tree_key
) decl
);
608 value
= (tree
) n
->value
;
609 STRIP_TYPE_NOPS (value
);
610 if (TREE_CONSTANT (value
) || TREE_READONLY_DECL_P (value
))
612 *tp
= build_empty_stmt ();
613 return copy_body_r (tp
, walk_subtrees
, data
);
617 else if (TREE_CODE (*tp
) == INDIRECT_REF
)
619 /* Get rid of *& from inline substitutions that can happen when a
620 pointer argument is an ADDR_EXPR. */
621 tree decl
= TREE_OPERAND (*tp
, 0);
624 n
= splay_tree_lookup (id
->decl_map
, (splay_tree_key
) decl
);
627 /* If we happen to get an ADDR_EXPR in n->value, strip
628 it manually here as we'll eventually get ADDR_EXPRs
629 which lie about their types pointed to. In this case
630 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
631 but we absolutely rely on that. As fold_indirect_ref
632 does other useful transformations, try that first, though. */
633 tree type
= TREE_TYPE (TREE_TYPE ((tree
)n
->value
));
634 *tp
= fold_indirect_ref_1 (type
, (tree
)n
->value
);
637 if (TREE_CODE ((tree
)n
->value
) == ADDR_EXPR
)
638 *tp
= TREE_OPERAND ((tree
)n
->value
, 0);
640 *tp
= build1 (INDIRECT_REF
, type
, (tree
)n
->value
);
647 /* Here is the "usual case". Copy this tree node, and then
648 tweak some special cases. */
649 copy_tree_r (tp
, walk_subtrees
, NULL
);
651 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp
))))
652 TREE_BLOCK (*tp
) = id
->block
;
654 if (TREE_CODE (*tp
) == RESX_EXPR
&& id
->eh_region_offset
)
655 TREE_OPERAND (*tp
, 0) =
658 id
->eh_region_offset
+ TREE_INT_CST_LOW (TREE_OPERAND (*tp
, 0)));
660 TREE_TYPE (*tp
) = remap_type (TREE_TYPE (*tp
), id
);
662 /* The copied TARGET_EXPR has never been expanded, even if the
663 original node was expanded already. */
664 if (TREE_CODE (*tp
) == TARGET_EXPR
&& TREE_OPERAND (*tp
, 3))
666 TREE_OPERAND (*tp
, 1) = TREE_OPERAND (*tp
, 3);
667 TREE_OPERAND (*tp
, 3) = NULL_TREE
;
670 /* Variable substitution need not be simple. In particular, the
671 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
672 and friends are up-to-date. */
673 else if (TREE_CODE (*tp
) == ADDR_EXPR
)
675 walk_tree (&TREE_OPERAND (*tp
, 0), copy_body_r
, id
, NULL
);
676 recompute_tree_invarant_for_addr_expr (*tp
);
681 /* Keep iterating. */
685 /* Copy basic block, scale profile accordingly. Edges will be taken care of
689 copy_bb (inline_data
*id
, basic_block bb
, int frequency_scale
, int count_scale
)
691 block_stmt_iterator bsi
, copy_bsi
;
692 basic_block copy_basic_block
;
694 /* create_basic_block() will append every new block to
695 basic_block_info automatically. */
696 copy_basic_block
= create_basic_block (NULL
, (void *) 0, bb
->prev_bb
->aux
);
697 copy_basic_block
->count
= bb
->count
* count_scale
/ REG_BR_PROB_BASE
;
698 copy_basic_block
->frequency
= (bb
->frequency
699 * frequency_scale
/ REG_BR_PROB_BASE
);
700 copy_bsi
= bsi_start (copy_basic_block
);
702 for (bsi
= bsi_start (bb
);
703 !bsi_end_p (bsi
); bsi_next (&bsi
))
705 tree stmt
= bsi_stmt (bsi
);
706 tree orig_stmt
= stmt
;
708 walk_tree (&stmt
, copy_body_r
, id
, NULL
);
710 /* RETURN_EXPR might be removed,
711 this is signalled by making stmt pointer NULL. */
715 bsi_insert_after (©_bsi
, stmt
, BSI_NEW_STMT
);
716 call
= get_call_expr_in (stmt
);
717 /* We're duplicating a CALL_EXPR. Find any corresponding
718 callgraph edges and update or duplicate them. */
719 if (call
&& (decl
= get_callee_fndecl (call
)))
723 struct cgraph_node
*node
;
724 struct cgraph_edge
*edge
;
726 /* We're saving a copy of the body, so we'll update the
727 callgraph nodes in place. Note that we avoid
728 altering the original callgraph node; we begin with
730 for (node
= id
->node
->next_clone
;
732 node
= node
->next_clone
)
734 edge
= cgraph_edge (node
, orig_stmt
);
736 edge
->call_stmt
= stmt
;
741 struct cgraph_edge
*edge
;
743 /* We're cloning or inlining this body; duplicate the
744 associate callgraph nodes. */
745 edge
= cgraph_edge (id
->current_node
, orig_stmt
);
747 cgraph_clone_edge (edge
, id
->node
, stmt
,
748 REG_BR_PROB_BASE
, 1);
751 /* If you think we can abort here, you are wrong.
752 There is no region 0 in tree land. */
753 gcc_assert (lookup_stmt_eh_region_fn (id
->callee_cfun
, orig_stmt
)
756 if (tree_could_throw_p (stmt
))
758 int region
= lookup_stmt_eh_region_fn (id
->callee_cfun
, orig_stmt
);
759 /* Add an entry for the copied tree in the EH hashtable.
760 When saving or cloning or versioning, use the hashtable in
761 cfun, and just copy the EH number. When inlining, use the
762 hashtable in the caller, and adjust the region number. */
764 add_stmt_to_eh_region (stmt
, region
+ id
->eh_region_offset
);
766 /* If this tree doesn't have a region associated with it,
767 and there is a "current region,"
768 then associate this tree with the current region
769 and add edges associated with this region. */
770 if ((lookup_stmt_eh_region_fn (id
->callee_cfun
,
772 && id
->eh_region
> 0)
773 && tree_could_throw_p (stmt
))
774 add_stmt_to_eh_region (stmt
, id
->eh_region
);
778 return copy_basic_block
;
781 /* Copy edges from BB into its copy constructed earlier, scale profile
782 accordingly. Edges will be taken care of later. Assume aux
783 pointers to point to the copies of each BB. */
785 copy_edges_for_bb (basic_block bb
, int count_scale
)
787 basic_block new_bb
= bb
->aux
;
790 block_stmt_iterator bsi
;
793 /* Use the indices from the original blocks to create edges for the
795 FOR_EACH_EDGE (old_edge
, ei
, bb
->succs
)
796 if (!(old_edge
->flags
& EDGE_EH
))
800 flags
= old_edge
->flags
;
802 /* Return edges do get a FALLTHRU flag when the get inlined. */
803 if (old_edge
->dest
->index
== EXIT_BLOCK
&& !old_edge
->flags
804 && old_edge
->dest
->aux
!= EXIT_BLOCK_PTR
)
805 flags
|= EDGE_FALLTHRU
;
806 new = make_edge (new_bb
, old_edge
->dest
->aux
, flags
);
807 new->count
= old_edge
->count
* count_scale
/ REG_BR_PROB_BASE
;
808 new->probability
= old_edge
->probability
;
811 if (bb
->index
== ENTRY_BLOCK
|| bb
->index
== EXIT_BLOCK
)
814 for (bsi
= bsi_start (new_bb
); !bsi_end_p (bsi
);)
818 copy_stmt
= bsi_stmt (bsi
);
819 update_stmt (copy_stmt
);
820 /* Do this before the possible split_block. */
823 /* If this tree could throw an exception, there are two
824 cases where we need to add abnormal edge(s): the
825 tree wasn't in a region and there is a "current
826 region" in the caller; or the original tree had
827 EH edges. In both cases split the block after the tree,
828 and add abnormal edge(s) as needed; we need both
829 those from the callee and the caller.
830 We check whether the copy can throw, because the const
831 propagation can change an INDIRECT_REF which throws
832 into a COMPONENT_REF which doesn't. If the copy
833 can throw, the original could also throw. */
835 if (tree_can_throw_internal (copy_stmt
))
837 if (!bsi_end_p (bsi
))
838 /* Note that bb's predecessor edges aren't necessarily
839 right at this point; split_block doesn't care. */
841 edge e
= split_block (new_bb
, copy_stmt
);
843 bsi
= bsi_start (new_bb
);
846 make_eh_edges (copy_stmt
);
851 /* Wrapper for remap_decl so it can be used as a callback. */
853 remap_decl_1 (tree decl
, void *data
)
855 return remap_decl (decl
, data
);
858 /* Make a copy of the body of FN so that it can be inserted inline in
859 another function. Walks FN via CFG, returns new fndecl. */
862 copy_cfg_body (inline_data
* id
, gcov_type count
, int frequency
,
863 basic_block entry_block_map
, basic_block exit_block_map
)
865 tree callee_fndecl
= id
->callee
;
866 /* Original cfun for the callee, doesn't change. */
867 struct function
*callee_cfun
= DECL_STRUCT_FUNCTION (callee_fndecl
);
868 /* Copy, built by this function. */
869 struct function
*new_cfun
;
870 /* Place to copy from; when a copy of the function was saved off earlier,
871 use that instead of the main copy. */
872 struct function
*cfun_to_copy
=
873 (struct function
*) ggc_alloc_cleared (sizeof (struct function
));
875 tree new_fndecl
= NULL
;
876 bool saving_or_cloning
;
877 int count_scale
, frequency_scale
;
879 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->count
)
880 count_scale
= (REG_BR_PROB_BASE
* count
881 / ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->count
);
885 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->frequency
)
886 frequency_scale
= (REG_BR_PROB_BASE
* frequency
888 ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->frequency
);
890 frequency_scale
= count_scale
;
892 /* Register specific tree functions. */
893 tree_register_cfg_hooks ();
895 /* Must have a CFG here at this point. */
896 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
897 (DECL_STRUCT_FUNCTION (callee_fndecl
)));
899 *cfun_to_copy
= *DECL_STRUCT_FUNCTION (callee_fndecl
);
901 /* If there is a saved_cfg+saved_args lurking in the
902 struct function, a copy of the callee body was saved there, and
903 the 'struct cgraph edge' nodes have been fudged to point into the
904 saved body. Accordingly, we want to copy that saved body so the
905 callgraph edges will be recognized and cloned properly. */
906 if (cfun_to_copy
->saved_cfg
)
908 cfun_to_copy
->cfg
= cfun_to_copy
->saved_cfg
;
909 cfun_to_copy
->eh
= cfun_to_copy
->saved_eh
;
911 id
->callee_cfun
= cfun_to_copy
;
913 /* If saving or cloning a function body, create new basic_block_info
914 and label_to_block_maps. Otherwise, we're duplicating a function
915 body for inlining; insert our new blocks and labels into the
917 saving_or_cloning
= (id
->saving_p
|| id
->cloning_p
);
918 if (saving_or_cloning
)
921 (struct function
*) ggc_alloc_cleared (sizeof (struct function
));
922 *new_cfun
= *DECL_STRUCT_FUNCTION (callee_fndecl
);
923 new_cfun
->cfg
= NULL
;
924 new_cfun
->decl
= new_fndecl
= copy_node (callee_fndecl
);
925 new_cfun
->ib_boundaries_block
= (varray_type
) 0;
926 DECL_STRUCT_FUNCTION (new_fndecl
) = new_cfun
;
927 push_cfun (new_cfun
);
928 init_empty_tree_cfg ();
930 ENTRY_BLOCK_PTR
->count
=
931 (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->count
* count_scale
/
933 ENTRY_BLOCK_PTR
->frequency
=
934 (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->frequency
*
935 frequency_scale
/ REG_BR_PROB_BASE
);
936 EXIT_BLOCK_PTR
->count
=
937 (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->count
* count_scale
/
939 EXIT_BLOCK_PTR
->frequency
=
940 (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun
)->frequency
*
941 frequency_scale
/ REG_BR_PROB_BASE
);
943 entry_block_map
= ENTRY_BLOCK_PTR
;
944 exit_block_map
= EXIT_BLOCK_PTR
;
947 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy
)->aux
= entry_block_map
;
948 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy
)->aux
= exit_block_map
;
951 /* Duplicate any exception-handling regions. */
954 if (saving_or_cloning
)
955 init_eh_for_function ();
956 id
->eh_region_offset
= duplicate_eh_regions (cfun_to_copy
,
959 gcc_assert (inlining_p (id
) || !id
->eh_region_offset
);
961 /* Use aux pointers to map the original blocks to copy. */
962 FOR_EACH_BB_FN (bb
, cfun_to_copy
)
963 bb
->aux
= copy_bb (id
, bb
, frequency_scale
, count_scale
);
964 /* Now that we've duplicated the blocks, duplicate their edges. */
965 FOR_ALL_BB_FN (bb
, cfun_to_copy
)
966 copy_edges_for_bb (bb
, count_scale
);
967 FOR_ALL_BB_FN (bb
, cfun_to_copy
)
970 if (saving_or_cloning
)
976 /* Make a copy of the body of FN so that it can be inserted inline in
980 copy_generic_body (inline_data
*id
)
983 tree fndecl
= id
->callee
;
985 body
= DECL_SAVED_TREE (fndecl
);
986 walk_tree (&body
, copy_body_r
, id
, NULL
);
992 copy_body (inline_data
*id
, gcov_type count
, int frequency
,
993 basic_block entry_block_map
, basic_block exit_block_map
)
995 tree fndecl
= id
->callee
;
998 /* If this body has a CFG, walk CFG and copy. */
999 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl
)));
1000 body
= copy_cfg_body (id
, count
, frequency
, entry_block_map
, exit_block_map
);
1005 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1006 defined in function FN, or of a data member thereof. */
1009 self_inlining_addr_expr (tree value
, tree fn
)
1013 if (TREE_CODE (value
) != ADDR_EXPR
)
1016 var
= get_base_address (TREE_OPERAND (value
, 0));
1018 return var
&& lang_hooks
.tree_inlining
.auto_var_in_fn_p (var
, fn
);
1022 setup_one_parameter (inline_data
*id
, tree p
, tree value
, tree fn
,
1023 basic_block bb
, tree
*vars
)
1029 /* If the parameter is never assigned to, we may not need to
1030 create a new variable here at all. Instead, we may be able
1031 to just use the argument value. */
1032 if (TREE_READONLY (p
)
1033 && !TREE_ADDRESSABLE (p
)
1034 && value
&& !TREE_SIDE_EFFECTS (value
))
1036 /* We may produce non-gimple trees by adding NOPs or introduce
1037 invalid sharing when operand is not really constant.
1038 It is not big deal to prohibit constant propagation here as
1039 we will constant propagate in DOM1 pass anyway. */
1040 if (is_gimple_min_invariant (value
)
1041 && lang_hooks
.types_compatible_p (TREE_TYPE (value
), TREE_TYPE (p
))
1042 /* We have to be very careful about ADDR_EXPR. Make sure
1043 the base variable isn't a local variable of the inlined
1044 function, e.g., when doing recursive inlining, direct or
1045 mutually-recursive or whatever, which is why we don't
1046 just test whether fn == current_function_decl. */
1047 && ! self_inlining_addr_expr (value
, fn
))
1049 insert_decl_map (id
, p
, value
);
1054 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1055 here since the type of this decl must be visible to the calling
1057 var
= copy_decl_for_inlining (p
, fn
, id
->caller
);
1059 /* See if the frontend wants to pass this by invisible reference. If
1060 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1061 replace uses of the PARM_DECL with dereferences. */
1062 if (TREE_TYPE (var
) != TREE_TYPE (p
)
1063 && POINTER_TYPE_P (TREE_TYPE (var
))
1064 && TREE_TYPE (TREE_TYPE (var
)) == TREE_TYPE (p
))
1066 insert_decl_map (id
, var
, var
);
1067 var_sub
= build_fold_indirect_ref (var
);
1072 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1073 that way, when the PARM_DECL is encountered, it will be
1074 automatically replaced by the VAR_DECL. */
1075 insert_decl_map (id
, p
, var_sub
);
1077 /* Declare this new variable. */
1078 TREE_CHAIN (var
) = *vars
;
1081 /* Make gimplifier happy about this variable. */
1082 DECL_SEEN_IN_BIND_EXPR_P (var
) = 1;
1084 /* Even if P was TREE_READONLY, the new VAR should not be.
1085 In the original code, we would have constructed a
1086 temporary, and then the function body would have never
1087 changed the value of P. However, now, we will be
1088 constructing VAR directly. The constructor body may
1089 change its value multiple times as it is being
1090 constructed. Therefore, it must not be TREE_READONLY;
1091 the back-end assumes that TREE_READONLY variable is
1092 assigned to only once. */
1093 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p
)))
1094 TREE_READONLY (var
) = 0;
1096 /* Initialize this VAR_DECL from the equivalent argument. Convert
1097 the argument to the proper type in case it was promoted. */
1100 tree rhs
= fold_convert (TREE_TYPE (var
), value
);
1101 block_stmt_iterator bsi
= bsi_last (bb
);
1103 if (rhs
== error_mark_node
)
1106 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1107 keep our trees in gimple form. */
1108 init_stmt
= build (MODIFY_EXPR
, TREE_TYPE (var
), var
, rhs
);
1110 /* If we did not create a gimple value and we did not create a gimple
1111 cast of a gimple value, then we will need to gimplify INIT_STMTS
1112 at the end. Note that is_gimple_cast only checks the outer
1113 tree code, not its operand. Thus the explicit check that its
1114 operand is a gimple value. */
1115 if (!is_gimple_val (rhs
)
1116 && (!is_gimple_cast (rhs
)
1117 || !is_gimple_val (TREE_OPERAND (rhs
, 0))))
1118 gimplify_stmt (&init_stmt
);
1119 bsi_insert_after (&bsi
, init_stmt
, BSI_NEW_STMT
);
1123 /* Generate code to initialize the parameters of the function at the
1124 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1127 initialize_inlined_parameters (inline_data
*id
, tree args
, tree static_chain
,
1128 tree fn
, basic_block bb
)
1133 tree vars
= NULL_TREE
;
1136 /* Figure out what the parameters are. */
1137 parms
= DECL_ARGUMENTS (fn
);
1138 if (fn
== current_function_decl
)
1139 parms
= cfun
->saved_args
;
1141 /* Loop through the parameter declarations, replacing each with an
1142 equivalent VAR_DECL, appropriately initialized. */
1143 for (p
= parms
, a
= args
; p
;
1144 a
= a
? TREE_CHAIN (a
) : a
, p
= TREE_CHAIN (p
))
1150 /* Find the initializer. */
1151 value
= lang_hooks
.tree_inlining
.convert_parm_for_inlining
1152 (p
, a
? TREE_VALUE (a
) : NULL_TREE
, fn
, argnum
);
1154 setup_one_parameter (id
, p
, value
, fn
, bb
, &vars
);
1157 /* Initialize the static chain. */
1158 p
= DECL_STRUCT_FUNCTION (fn
)->static_chain_decl
;
1159 if (fn
== current_function_decl
)
1160 p
= DECL_STRUCT_FUNCTION (fn
)->saved_static_chain_decl
;
1163 /* No static chain? Seems like a bug in tree-nested.c. */
1164 gcc_assert (static_chain
);
1166 setup_one_parameter (id
, p
, static_chain
, fn
, bb
, &vars
);
1169 declare_inline_vars (id
->block
, vars
);
1172 /* Declare a return variable to replace the RESULT_DECL for the
1173 function we are calling. An appropriate DECL_STMT is returned.
1174 The USE_STMT is filled to contain a use of the declaration to
1175 indicate the return value of the function.
1177 RETURN_SLOT_ADDR, if non-null, was a fake parameter that
1178 took the address of the result. MODIFY_DEST, if non-null, was the LHS of
1179 the MODIFY_EXPR to which this call is the RHS.
1181 The return value is a (possibly null) value that is the result of the
1182 function as seen by the callee. *USE_P is a (possibly null) value that
1183 holds the result as seen by the caller. */
1186 declare_return_variable (inline_data
*id
, tree return_slot_addr
,
1187 tree modify_dest
, tree
*use_p
)
1189 tree callee
= id
->callee
;
1190 tree caller
= id
->caller
;
1191 tree result
= DECL_RESULT (callee
);
1192 tree callee_type
= TREE_TYPE (result
);
1193 tree caller_type
= TREE_TYPE (TREE_TYPE (callee
));
1196 /* We don't need to do anything for functions that don't return
1198 if (!result
|| VOID_TYPE_P (callee_type
))
1204 /* If there was a return slot, then the return value is the
1205 dereferenced address of that object. */
1206 if (return_slot_addr
)
1208 /* The front end shouldn't have used both return_slot_addr and
1209 a modify expression. */
1210 gcc_assert (!modify_dest
);
1211 if (DECL_BY_REFERENCE (result
))
1212 var
= return_slot_addr
;
1214 var
= build_fold_indirect_ref (return_slot_addr
);
1219 /* All types requiring non-trivial constructors should have been handled. */
1220 gcc_assert (!TREE_ADDRESSABLE (callee_type
));
1222 /* Attempt to avoid creating a new temporary variable. */
1225 bool use_it
= false;
1227 /* We can't use MODIFY_DEST if there's type promotion involved. */
1228 if (!lang_hooks
.types_compatible_p (caller_type
, callee_type
))
1231 /* ??? If we're assigning to a variable sized type, then we must
1232 reuse the destination variable, because we've no good way to
1233 create variable sized temporaries at this point. */
1234 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type
)) != INTEGER_CST
)
1237 /* If the callee cannot possibly modify MODIFY_DEST, then we can
1238 reuse it as the result of the call directly. Don't do this if
1239 it would promote MODIFY_DEST to addressable. */
1240 else if (!TREE_STATIC (modify_dest
)
1241 && !TREE_ADDRESSABLE (modify_dest
)
1242 && !TREE_ADDRESSABLE (result
))
1253 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type
)) == INTEGER_CST
);
1255 var
= copy_decl_for_inlining (result
, callee
, caller
);
1257 DECL_SEEN_IN_BIND_EXPR_P (var
) = 1;
1258 DECL_STRUCT_FUNCTION (caller
)->unexpanded_var_list
1259 = tree_cons (NULL_TREE
, var
,
1260 DECL_STRUCT_FUNCTION (caller
)->unexpanded_var_list
);
1262 /* Do not have the rest of GCC warn about this variable as it should
1263 not be visible to the user. */
1264 TREE_NO_WARNING (var
) = 1;
1266 /* Build the use expr. If the return type of the function was
1267 promoted, convert it back to the expected type. */
1269 if (!lang_hooks
.types_compatible_p (TREE_TYPE (var
), caller_type
))
1270 use
= fold_convert (caller_type
, var
);
1273 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1274 way, when the RESULT_DECL is encountered, it will be
1275 automatically replaced by the VAR_DECL. */
1276 insert_decl_map (id
, result
, var
);
1278 /* Remember this so we can ignore it in remap_decls. */
1285 /* Returns nonzero if a function can be inlined as a tree. */
1288 tree_inlinable_function_p (tree fn
)
1290 return inlinable_function_p (fn
);
1293 static const char *inline_forbidden_reason
;
1296 inline_forbidden_p_1 (tree
*nodep
, int *walk_subtrees ATTRIBUTE_UNUSED
,
1300 tree fn
= (tree
) fnp
;
1303 switch (TREE_CODE (node
))
1306 /* Refuse to inline alloca call unless user explicitly forced so as
1307 this may change program's memory overhead drastically when the
1308 function using alloca is called in loop. In GCC present in
1309 SPEC2000 inlining into schedule_block cause it to require 2GB of
1310 RAM instead of 256MB. */
1311 if (alloca_call_p (node
)
1312 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn
)))
1314 inline_forbidden_reason
1315 = G_("%Jfunction %qF can never be inlined because it uses "
1316 "alloca (override using the always_inline attribute)");
1319 t
= get_callee_fndecl (node
);
1323 /* We cannot inline functions that call setjmp. */
1324 if (setjmp_call_p (t
))
1326 inline_forbidden_reason
1327 = G_("%Jfunction %qF can never be inlined because it uses setjmp");
1331 if (DECL_BUILT_IN_CLASS (t
) == BUILT_IN_NORMAL
)
1332 switch (DECL_FUNCTION_CODE (t
))
1334 /* We cannot inline functions that take a variable number of
1336 case BUILT_IN_VA_START
:
1337 case BUILT_IN_STDARG_START
:
1338 case BUILT_IN_NEXT_ARG
:
1339 case BUILT_IN_VA_END
:
1340 inline_forbidden_reason
1341 = G_("%Jfunction %qF can never be inlined because it "
1342 "uses variable argument lists");
1345 case BUILT_IN_LONGJMP
:
1346 /* We can't inline functions that call __builtin_longjmp at
1347 all. The non-local goto machinery really requires the
1348 destination be in a different function. If we allow the
1349 function calling __builtin_longjmp to be inlined into the
1350 function calling __builtin_setjmp, Things will Go Awry. */
1351 inline_forbidden_reason
1352 = G_("%Jfunction %qF can never be inlined because "
1353 "it uses setjmp-longjmp exception handling");
1356 case BUILT_IN_NONLOCAL_GOTO
:
1358 inline_forbidden_reason
1359 = G_("%Jfunction %qF can never be inlined because "
1360 "it uses non-local goto");
1363 case BUILT_IN_RETURN
:
1364 case BUILT_IN_APPLY_ARGS
:
1365 /* If a __builtin_apply_args caller would be inlined,
1366 it would be saving arguments of the function it has
1367 been inlined into. Similarly __builtin_return would
1368 return from the function the inline has been inlined into. */
1369 inline_forbidden_reason
1370 = G_("%Jfunction %qF can never be inlined because "
1371 "it uses __builtin_return or __builtin_apply_args");
1380 t
= TREE_OPERAND (node
, 0);
1382 /* We will not inline a function which uses computed goto. The
1383 addresses of its local labels, which may be tucked into
1384 global storage, are of course not constant across
1385 instantiations, which causes unexpected behavior. */
1386 if (TREE_CODE (t
) != LABEL_DECL
)
1388 inline_forbidden_reason
1389 = G_("%Jfunction %qF can never be inlined "
1390 "because it contains a computed goto");
1396 t
= TREE_OPERAND (node
, 0);
1397 if (DECL_NONLOCAL (t
))
1399 /* We cannot inline a function that receives a non-local goto
1400 because we cannot remap the destination label used in the
1401 function that is performing the non-local goto. */
1402 inline_forbidden_reason
1403 = G_("%Jfunction %qF can never be inlined "
1404 "because it receives a non-local goto");
1411 /* We cannot inline a function of the form
1413 void F (int i) { struct S { int ar[i]; } s; }
1415 Attempting to do so produces a catch-22.
1416 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1417 UNION_TYPE nodes, then it goes into infinite recursion on a
1418 structure containing a pointer to its own type. If it doesn't,
1419 then the type node for S doesn't get adjusted properly when
1422 ??? This is likely no longer true, but it's too late in the 4.0
1423 cycle to try to find out. This should be checked for 4.1. */
1424 for (t
= TYPE_FIELDS (node
); t
; t
= TREE_CHAIN (t
))
1425 if (variably_modified_type_p (TREE_TYPE (t
), NULL
))
1427 inline_forbidden_reason
1428 = G_("%Jfunction %qF can never be inlined "
1429 "because it uses variable sized variables");
1440 /* Return subexpression representing possible alloca call, if any. */
1442 inline_forbidden_p (tree fndecl
)
1444 location_t saved_loc
= input_location
;
1445 block_stmt_iterator bsi
;
1447 tree ret
= NULL_TREE
;
1449 FOR_EACH_BB_FN (bb
, DECL_STRUCT_FUNCTION (fndecl
))
1450 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1452 ret
= walk_tree_without_duplicates (bsi_stmt_ptr (bsi
),
1453 inline_forbidden_p_1
, fndecl
);
1459 input_location
= saved_loc
;
1463 /* Returns nonzero if FN is a function that does not have any
1464 fundamental inline blocking properties. */
1467 inlinable_function_p (tree fn
)
1469 bool inlinable
= true;
1471 /* If we've already decided this function shouldn't be inlined,
1472 there's no need to check again. */
1473 if (DECL_UNINLINABLE (fn
))
1476 /* See if there is any language-specific reason it cannot be
1477 inlined. (It is important that this hook be called early because
1478 in C++ it may result in template instantiation.)
1479 If the function is not inlinable for language-specific reasons,
1480 it is left up to the langhook to explain why. */
1481 inlinable
= !lang_hooks
.tree_inlining
.cannot_inline_tree_fn (&fn
);
1483 /* If we don't have the function body available, we can't inline it.
1484 However, this should not be recorded since we also get here for
1485 forward declared inline functions. Therefore, return at once. */
1486 if (!DECL_SAVED_TREE (fn
))
1489 /* If we're not inlining at all, then we cannot inline this function. */
1490 else if (!flag_inline_trees
)
1493 /* Only try to inline functions if DECL_INLINE is set. This should be
1494 true for all functions declared `inline', and for all other functions
1495 as well with -finline-functions.
1497 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1498 it's the front-end that must set DECL_INLINE in this case, because
1499 dwarf2out loses if a function that does not have DECL_INLINE set is
1500 inlined anyway. That is why we have both DECL_INLINE and
1501 DECL_DECLARED_INLINE_P. */
1502 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1503 here should be redundant. */
1504 else if (!DECL_INLINE (fn
) && !flag_unit_at_a_time
)
1507 else if (inline_forbidden_p (fn
))
1509 /* See if we should warn about uninlinable functions. Previously,
1510 some of these warnings would be issued while trying to expand
1511 the function inline, but that would cause multiple warnings
1512 about functions that would for example call alloca. But since
1513 this a property of the function, just one warning is enough.
1514 As a bonus we can now give more details about the reason why a
1515 function is not inlinable.
1516 We only warn for functions declared `inline' by the user. */
1517 bool do_warning
= (warn_inline
1519 && DECL_DECLARED_INLINE_P (fn
)
1520 && !DECL_IN_SYSTEM_HEADER (fn
));
1522 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn
)))
1523 sorry (inline_forbidden_reason
, fn
, fn
);
1524 else if (do_warning
)
1525 warning (0, inline_forbidden_reason
, fn
, fn
);
1530 /* Squirrel away the result so that we don't have to check again. */
1531 DECL_UNINLINABLE (fn
) = !inlinable
;
1536 /* Estimate the cost of a memory move. Use machine dependent
1537 word size and take possible memcpy call into account. */
1540 estimate_move_cost (tree type
)
1544 size
= int_size_in_bytes (type
);
1546 if (size
< 0 || size
> MOVE_MAX_PIECES
* MOVE_RATIO
)
1547 /* Cost of a memcpy call, 3 arguments and the call. */
1550 return ((size
+ MOVE_MAX_PIECES
- 1) / MOVE_MAX_PIECES
);
1553 /* Used by estimate_num_insns. Estimate number of instructions seen
1554 by given statement. */
1557 estimate_num_insns_1 (tree
*tp
, int *walk_subtrees
, void *data
)
1562 if (IS_TYPE_OR_DECL_P (x
))
1567 /* Assume that constants and references counts nothing. These should
1568 be majorized by amount of operations among them we count later
1569 and are common target of CSE and similar optimizations. */
1570 else if (CONSTANT_CLASS_P (x
) || REFERENCE_CLASS_P (x
))
1573 switch (TREE_CODE (x
))
1575 /* Containers have no cost. */
1582 case ALIGN_INDIRECT_REF
:
1583 case MISALIGNED_INDIRECT_REF
:
1585 case ARRAY_RANGE_REF
:
1587 case EXC_PTR_EXPR
: /* ??? */
1588 case FILTER_EXPR
: /* ??? */
1591 case WITH_CLEANUP_EXPR
:
1593 case VIEW_CONVERT_EXPR
:
1598 case CASE_LABEL_EXPR
:
1601 case EH_FILTER_EXPR
:
1602 case STATEMENT_LIST
:
1604 case NON_LVALUE_EXPR
:
1607 case TRY_CATCH_EXPR
:
1608 case TRY_FINALLY_EXPR
:
1615 case WITH_SIZE_EXPR
:
1618 /* We don't account constants for now. Assume that the cost is amortized
1619 by operations that do use them. We may re-consider this decision once
1620 we are able to optimize the tree before estimating its size and break
1621 out static initializers. */
1622 case IDENTIFIER_NODE
:
1631 /* Try to estimate the cost of assignments. We have three cases to
1633 1) Simple assignments to registers;
1634 2) Stores to things that must live in memory. This includes
1635 "normal" stores to scalars, but also assignments of large
1636 structures, or constructors of big arrays;
1639 Let us look at the first two cases, assuming we have "a = b + C":
1640 <modify_expr <var_decl "a"> <plus_expr <var_decl "b"> <constant C>>
1641 If "a" is a GIMPLE register, the assignment to it is free on almost
1642 any target, because "a" usually ends up in a real register. Hence
1643 the only cost of this expression comes from the PLUS_EXPR, and we
1644 can ignore the MODIFY_EXPR.
1645 If "a" is not a GIMPLE register, the assignment to "a" will most
1646 likely be a real store, so the cost of the MODIFY_EXPR is the cost
1647 of moving something into "a", which we compute using the function
1650 The third case deals with TARGET_EXPRs, for which the semantics are
1651 that a temporary is assigned, unless the TARGET_EXPR itself is being
1652 assigned to something else. In the latter case we do not need the
1653 temporary. E.g. in <modify_expr <var_decl "a"> <target_expr>>, the
1654 MODIFY_EXPR is free. */
1657 /* Is the right and side a TARGET_EXPR? */
1658 if (TREE_CODE (TREE_OPERAND (x
, 1)) == TARGET_EXPR
)
1660 /* ... fall through ... */
1663 x
= TREE_OPERAND (x
, 0);
1664 /* Is this an assignments to a register? */
1665 if (is_gimple_reg (x
))
1667 /* Otherwise it's a store, so fall through to compute the move cost. */
1670 *count
+= estimate_move_cost (TREE_TYPE (x
));
1673 /* Assign cost of 1 to usual operations.
1674 ??? We may consider mapping RTL costs to this. */
1682 case FIX_TRUNC_EXPR
:
1684 case FIX_FLOOR_EXPR
:
1685 case FIX_ROUND_EXPR
:
1697 case VEC_LSHIFT_EXPR
:
1698 case VEC_RSHIFT_EXPR
:
1705 case TRUTH_ANDIF_EXPR
:
1706 case TRUTH_ORIF_EXPR
:
1707 case TRUTH_AND_EXPR
:
1709 case TRUTH_XOR_EXPR
:
1710 case TRUTH_NOT_EXPR
:
1719 case UNORDERED_EXPR
:
1732 case PREDECREMENT_EXPR
:
1733 case PREINCREMENT_EXPR
:
1734 case POSTDECREMENT_EXPR
:
1735 case POSTINCREMENT_EXPR
:
1741 case REALIGN_LOAD_EXPR
:
1743 case REDUC_MAX_EXPR
:
1744 case REDUC_MIN_EXPR
:
1745 case REDUC_PLUS_EXPR
:
1751 /* Few special cases of expensive operations. This is useful
1752 to avoid inlining on functions having too many of these. */
1753 case TRUNC_DIV_EXPR
:
1755 case FLOOR_DIV_EXPR
:
1756 case ROUND_DIV_EXPR
:
1757 case EXACT_DIV_EXPR
:
1758 case TRUNC_MOD_EXPR
:
1760 case FLOOR_MOD_EXPR
:
1761 case ROUND_MOD_EXPR
:
1767 tree decl
= get_callee_fndecl (x
);
1770 if (decl
&& DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
1771 switch (DECL_FUNCTION_CODE (decl
))
1773 case BUILT_IN_CONSTANT_P
:
1776 case BUILT_IN_EXPECT
:
1782 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1783 that does use function declaration to figure out the arguments. */
1786 for (arg
= TREE_OPERAND (x
, 1); arg
; arg
= TREE_CHAIN (arg
))
1787 *count
+= estimate_move_cost (TREE_TYPE (TREE_VALUE (arg
)));
1791 for (arg
= DECL_ARGUMENTS (decl
); arg
; arg
= TREE_CHAIN (arg
))
1792 *count
+= estimate_move_cost (TREE_TYPE (arg
));
1795 *count
+= PARAM_VALUE (PARAM_INLINE_CALL_COST
);
1804 /* Estimate number of instructions that will be created by expanding EXPR. */
1807 estimate_num_insns (tree expr
)
1810 struct pointer_set_t
*visited_nodes
;
1812 block_stmt_iterator bsi
;
1813 struct function
*my_function
;
1815 /* If we're given an entire function, walk the CFG. */
1816 if (TREE_CODE (expr
) == FUNCTION_DECL
)
1818 my_function
= DECL_STRUCT_FUNCTION (expr
);
1819 gcc_assert (my_function
&& my_function
->cfg
);
1820 visited_nodes
= pointer_set_create ();
1821 FOR_EACH_BB_FN (bb
, my_function
)
1823 for (bsi
= bsi_start (bb
);
1827 walk_tree (bsi_stmt_ptr (bsi
), estimate_num_insns_1
,
1828 &num
, visited_nodes
);
1831 pointer_set_destroy (visited_nodes
);
1834 walk_tree_without_duplicates (&expr
, estimate_num_insns_1
, &num
);
1839 typedef struct function
*function_p
;
1841 DEF_VEC_P(function_p
);
1842 DEF_VEC_ALLOC_P(function_p
,heap
);
1844 /* Initialized with NOGC, making this poisonous to the garbage collector. */
1845 static VEC(function_p
,heap
) *cfun_stack
;
1848 push_cfun (struct function
*new_cfun
)
1850 VEC_safe_push (function_p
, heap
, cfun_stack
, cfun
);
1857 cfun
= VEC_pop (function_p
, cfun_stack
);
1860 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
1862 add_lexical_block (tree current_block
, tree new_block
)
1866 /* Walk to the last sub-block. */
1867 for (blk_p
= &BLOCK_SUBBLOCKS (current_block
);
1869 blk_p
= &TREE_CHAIN (*blk_p
))
1872 BLOCK_SUPERCONTEXT (new_block
) = current_block
;
1873 BLOCK_SUBBLOCKS (new_block
) = NULL_TREE
;
1876 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1879 expand_call_inline (basic_block bb
, tree stmt
, tree
*tp
, void *data
)
1887 tree return_slot_addr
;
1889 location_t saved_location
;
1890 struct cgraph_edge
*cg_edge
;
1892 basic_block return_block
;
1894 block_stmt_iterator bsi
, stmt_bsi
;
1895 bool successfully_inlined
= FALSE
;
1898 struct cgraph_node
*old_node
;
1901 /* See what we've got. */
1902 id
= (inline_data
*) data
;
1905 /* Set input_location here so we get the right instantiation context
1906 if we call instantiate_decl from inlinable_function_p. */
1907 saved_location
= input_location
;
1908 if (EXPR_HAS_LOCATION (t
))
1909 input_location
= EXPR_LOCATION (t
);
1911 /* From here on, we're only interested in CALL_EXPRs. */
1912 if (TREE_CODE (t
) != CALL_EXPR
)
1915 /* First, see if we can figure out what function is being called.
1916 If we cannot, then there is no hope of inlining the function. */
1917 fn
= get_callee_fndecl (t
);
1921 /* Turn forward declarations into real ones. */
1922 fn
= cgraph_node (fn
)->decl
;
1924 /* If fn is a declaration of a function in a nested scope that was
1925 globally declared inline, we don't set its DECL_INITIAL.
1926 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1927 C++ front-end uses it for cdtors to refer to their internal
1928 declarations, that are not real functions. Fortunately those
1929 don't have trees to be saved, so we can tell by checking their
1931 if (! DECL_INITIAL (fn
)
1932 && DECL_ABSTRACT_ORIGIN (fn
)
1933 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn
)))
1934 fn
= DECL_ABSTRACT_ORIGIN (fn
);
1936 /* Objective C and fortran still calls tree_rest_of_compilation directly.
1937 Kill this check once this is fixed. */
1938 if (!id
->current_node
->analyzed
)
1941 cg_edge
= cgraph_edge (id
->current_node
, stmt
);
1943 /* Constant propagation on argument done during previous inlining
1944 may create new direct call. Produce an edge for it. */
1947 struct cgraph_node
*dest
= cgraph_node (fn
);
1949 /* We have missing edge in the callgraph. This can happen in one case
1950 where previous inlining turned indirect call into direct call by
1951 constant propagating arguments. In all other cases we hit a bug
1952 (incorrect node sharing is most common reason for missing edges. */
1953 gcc_assert (dest
->needed
|| !flag_unit_at_a_time
);
1954 cgraph_create_edge (id
->node
, dest
, stmt
,
1955 bb
->count
, bb
->loop_depth
)->inline_failed
1956 = N_("originally indirect function call not considered for inlining");
1960 /* Don't try to inline functions that are not well-suited to
1962 if (!cgraph_inline_p (cg_edge
, &reason
))
1964 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn
)))
1966 sorry ("%Jinlining failed in call to %qF: %s", fn
, fn
, reason
);
1967 sorry ("called from here");
1969 else if (warn_inline
&& DECL_DECLARED_INLINE_P (fn
)
1970 && !DECL_IN_SYSTEM_HEADER (fn
)
1972 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn
))
1973 /* Avoid warnings during early inline pass. */
1974 && (!flag_unit_at_a_time
|| cgraph_global_info_ready
))
1976 warning (OPT_Winline
, "%Jinlining failed in call to %qF: %s",
1978 warning (OPT_Winline
, "called from here");
1983 #ifdef ENABLE_CHECKING
1984 if (cg_edge
->callee
->decl
!= id
->node
->decl
)
1985 verify_cgraph_node (cg_edge
->callee
);
1988 /* We will be inlining this callee. */
1990 id
->eh_region
= lookup_stmt_eh_region (stmt
);
1992 /* Split the block holding the CALL_EXPR. */
1994 e
= split_block (bb
, stmt
);
1996 return_block
= e
->dest
;
1999 /* split_block splits before the statement, work around this by moving
2000 the call into the first half_bb. Not pretty, but seems easier than
2001 doing the CFG manipulation by hand when the CALL_EXPR is in the last
2003 stmt_bsi
= bsi_last (bb
);
2004 bsi
= bsi_start (return_block
);
2005 if (!bsi_end_p (bsi
))
2006 bsi_move_before (&stmt_bsi
, &bsi
);
2009 tree stmt
= bsi_stmt (stmt_bsi
);
2010 bsi_remove (&stmt_bsi
);
2011 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
2013 stmt_bsi
= bsi_start (return_block
);
2015 /* Build a block containing code to initialize the arguments, the
2016 actual inline expansion of the body, and a label for the return
2017 statements within the function to jump to. The type of the
2018 statement expression is the return type of the function call. */
2019 id
->block
= make_node (BLOCK
);
2020 BLOCK_ABSTRACT_ORIGIN (id
->block
) = fn
;
2021 add_lexical_block (TREE_BLOCK (stmt
), id
->block
);
2024 /* Local declarations will be replaced by their equivalents in this
2027 id
->decl_map
= splay_tree_new (splay_tree_compare_pointers
,
2030 /* Initialize the parameters. */
2031 args
= TREE_OPERAND (t
, 1);
2033 initialize_inlined_parameters (id
, args
, TREE_OPERAND (t
, 2), fn
, bb
);
2035 /* Record the function we are about to inline. */
2038 /* Return statements in the function body will be replaced by jumps
2039 to the RET_LABEL. */
2041 gcc_assert (DECL_INITIAL (fn
));
2042 gcc_assert (TREE_CODE (DECL_INITIAL (fn
)) == BLOCK
);
2044 /* Find the lhs to which the result of this call is assigned. */
2045 return_slot_addr
= NULL
;
2046 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
2048 modify_dest
= TREE_OPERAND (stmt
, 0);
2050 /* The function which we are inlining might not return a value,
2051 in which case we should issue a warning that the function
2052 does not return a value. In that case the optimizers will
2053 see that the variable to which the value is assigned was not
2054 initialized. We do not want to issue a warning about that
2055 uninitialized variable. */
2056 if (DECL_P (modify_dest
))
2057 TREE_NO_WARNING (modify_dest
) = 1;
2058 if (CALL_EXPR_RETURN_SLOT_OPT (t
))
2060 return_slot_addr
= build_fold_addr_expr (modify_dest
);
2067 /* Declare the return variable for the function. */
2068 decl
= declare_return_variable (id
, return_slot_addr
,
2069 modify_dest
, &use_retvar
);
2070 /* Do this only if declare_return_variable created a new one. */
2071 if (decl
&& !return_slot_addr
&& decl
!= modify_dest
)
2072 declare_inline_vars (id
->block
, decl
);
2074 /* After we've initialized the parameters, we insert the body of the
2076 old_node
= id
->current_node
;
2078 /* Anoint the callee-to-be-duplicated as the "current_node." When
2079 CALL_EXPRs within callee are duplicated, the edges from callee to
2080 callee's callees (caller's grandchildren) will be cloned. */
2081 id
->current_node
= cg_edge
->callee
;
2083 /* This is it. Duplicate the callee body. Assume callee is
2084 pre-gimplified. Note that we must not alter the caller
2085 function in any way before this point, as this CALL_EXPR may be
2086 a self-referential call; if we're calling ourselves, we need to
2087 duplicate our body before altering anything. */
2088 copy_body (id
, bb
->count
, bb
->frequency
, bb
, return_block
);
2089 id
->current_node
= old_node
;
2092 splay_tree_delete (id
->decl_map
);
2095 /* If the inlined function returns a result that we care about,
2096 clobber the CALL_EXPR with a reference to the return variable. */
2097 if (use_retvar
&& (TREE_CODE (bsi_stmt (stmt_bsi
)) != CALL_EXPR
))
2100 maybe_clean_or_replace_eh_stmt (stmt
, stmt
);
2103 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2104 tsi_delink() will leave the iterator in a sane state. */
2105 bsi_remove (&stmt_bsi
);
2108 if (bsi_end_p (bsi
))
2109 tree_purge_dead_eh_edges (return_block
);
2111 /* If the value of the new expression is ignored, that's OK. We
2112 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2113 the equivalent inlined version either. */
2114 TREE_USED (*tp
) = 1;
2116 /* Output the inlining info for this abstract function, since it has been
2117 inlined. If we don't do this now, we can lose the information about the
2118 variables in the function when the blocks get blown away as soon as we
2119 remove the cgraph node. */
2120 (*debug_hooks
->outlining_inline_function
) (cg_edge
->callee
->decl
);
2122 /* Update callgraph if needed. */
2123 cgraph_remove_node (cg_edge
->callee
);
2125 /* Declare the 'auto' variables added with this inlined body. */
2126 record_vars (BLOCK_VARS (id
->block
));
2127 id
->block
= NULL_TREE
;
2129 /* Add local static vars in this inlined callee to caller. */
2130 for (t_step
= id
->callee_cfun
->unexpanded_var_list
;
2132 t_step
= TREE_CHAIN (t_step
))
2134 var
= TREE_VALUE (t_step
);
2135 if (TREE_STATIC (var
) && !TREE_ASM_WRITTEN (var
))
2138 successfully_inlined
= TRUE
;
2141 input_location
= saved_location
;
2142 return successfully_inlined
;
2145 /* Expand call statements reachable from STMT_P.
2146 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2147 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
2148 unfortunately not use that function here because we need a pointer
2149 to the CALL_EXPR, not the tree itself. */
2152 gimple_expand_calls_inline (basic_block bb
, inline_data
*id
)
2154 block_stmt_iterator bsi
;
2156 /* Register specific tree functions. */
2157 tree_register_cfg_hooks ();
2158 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
2160 tree
*expr_p
= bsi_stmt_ptr (bsi
);
2161 tree stmt
= *expr_p
;
2163 if (TREE_CODE (*expr_p
) == MODIFY_EXPR
)
2164 expr_p
= &TREE_OPERAND (*expr_p
, 1);
2165 if (TREE_CODE (*expr_p
) == WITH_SIZE_EXPR
)
2166 expr_p
= &TREE_OPERAND (*expr_p
, 0);
2167 if (TREE_CODE (*expr_p
) == CALL_EXPR
)
2168 if (expand_call_inline (bb
, stmt
, expr_p
, id
))
2174 /* Expand calls to inline functions in the body of FN. */
2177 optimize_inline_calls (tree fn
)
2182 /* There is no point in performing inlining if errors have already
2183 occurred -- and we might crash if we try to inline invalid
2185 if (errorcount
|| sorrycount
)
2189 memset (&id
, 0, sizeof (id
));
2191 id
.current_node
= id
.node
= cgraph_node (fn
);
2193 /* Or any functions that aren't finished yet. */
2194 prev_fn
= NULL_TREE
;
2195 if (current_function_decl
)
2197 id
.caller
= current_function_decl
;
2198 prev_fn
= current_function_decl
;
2200 push_gimplify_context ();
2202 /* Reach the trees by walking over the CFG, and note the
2203 enclosing basic-blocks in the call edges. */
2204 /* We walk the blocks going forward, because inlined function bodies
2205 will split id->current_basic_block, and the new blocks will
2206 follow it; we'll trudge through them, processing their CALL_EXPRs
2209 gimple_expand_calls_inline (bb
, &id
);
2212 pop_gimplify_context (NULL
);
2213 /* Renumber the (code) basic_blocks consecutively. */
2215 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2218 #ifdef ENABLE_CHECKING
2220 struct cgraph_edge
*e
;
2222 verify_cgraph_node (id
.node
);
2224 /* Double check that we inlined everything we are supposed to inline. */
2225 for (e
= id
.node
->callees
; e
; e
= e
->next_callee
)
2226 gcc_assert (e
->inline_failed
);
2229 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2230 as inlining loops might increase the maximum. */
2231 if (ENTRY_BLOCK_PTR
->count
)
2233 fold_cond_expr_cond ();
2236 /* FN is a function that has a complete body, and CLONE is a function whose
2237 body is to be set to a copy of FN, mapping argument declarations according
2238 to the ARG_MAP splay_tree. */
2241 clone_body (tree clone
, tree fn
, void *arg_map
)
2245 /* Clone the body, as if we were making an inline call. But, remap the
2246 parameters in the callee to the parameters of caller. */
2247 memset (&id
, 0, sizeof (id
));
2250 id
.callee_cfun
= DECL_STRUCT_FUNCTION (fn
);
2251 id
.decl_map
= (splay_tree
)arg_map
;
2253 /* Cloning is treated slightly differently from inlining. Set
2254 CLONING_P so that it's clear which operation we're performing. */
2255 id
.cloning_p
= true;
2257 /* We're not inside any EH region. */
2260 /* Actually copy the body. */
2261 append_to_statement_list_force (copy_generic_body (&id
), &DECL_SAVED_TREE (clone
));
2264 /* Save duplicate body in FN. MAP is used to pass around splay tree
2265 used to update arguments in restore_body. */
2267 /* Make and return duplicate of body in FN. Put copies of DECL_ARGUMENTS
2268 in *arg_copy and of the static chain, if any, in *sc_copy. */
2271 save_body (tree fn
, tree
*arg_copy
, tree
*sc_copy
)
2274 tree newdecl
, *parg
;
2275 basic_block fn_entry_block
;
2277 memset (&id
, 0, sizeof (id
));
2279 id
.callee_cfun
= DECL_STRUCT_FUNCTION (fn
);
2281 id
.node
= cgraph_node (fn
);
2283 id
.decl_map
= splay_tree_new (splay_tree_compare_pointers
, NULL
, NULL
);
2284 *arg_copy
= DECL_ARGUMENTS (fn
);
2286 for (parg
= arg_copy
; *parg
; parg
= &TREE_CHAIN (*parg
))
2288 tree
new = copy_node (*parg
);
2290 lang_hooks
.dup_lang_specific_decl (new);
2291 DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*parg
);
2292 insert_decl_map (&id
, *parg
, new);
2293 TREE_CHAIN (new) = TREE_CHAIN (*parg
);
2297 *sc_copy
= DECL_STRUCT_FUNCTION (fn
)->static_chain_decl
;
2300 tree
new = copy_node (*sc_copy
);
2302 lang_hooks
.dup_lang_specific_decl (new);
2303 DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*sc_copy
);
2304 insert_decl_map (&id
, *sc_copy
, new);
2305 TREE_CHAIN (new) = TREE_CHAIN (*sc_copy
);
2309 /* We're not inside any EH region. */
2312 insert_decl_map (&id
, DECL_RESULT (fn
), DECL_RESULT (fn
));
2314 /* Actually copy the body, including a new (struct function *) and CFG.
2315 EH info is also duplicated so its labels point into the copied
2316 CFG, not the original. */
2317 fn_entry_block
= ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fn
));
2318 newdecl
= copy_body (&id
, fn_entry_block
->count
, fn_entry_block
->frequency
, NULL
, NULL
);
2319 DECL_STRUCT_FUNCTION (fn
)->saved_cfg
= DECL_STRUCT_FUNCTION (newdecl
)->cfg
;
2320 DECL_STRUCT_FUNCTION (fn
)->saved_eh
= DECL_STRUCT_FUNCTION (newdecl
)->eh
;
2323 splay_tree_delete (id
.decl_map
);
2326 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2329 copy_tree_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
2331 enum tree_code code
= TREE_CODE (*tp
);
2333 /* We make copies of most nodes. */
2334 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
))
2335 || code
== TREE_LIST
2337 || code
== TYPE_DECL
)
2339 /* Because the chain gets clobbered when we make a copy, we save it
2341 tree chain
= TREE_CHAIN (*tp
);
2344 /* Copy the node. */
2345 new = copy_node (*tp
);
2347 /* Propagate mudflap marked-ness. */
2348 if (flag_mudflap
&& mf_marked_p (*tp
))
2353 /* Now, restore the chain, if appropriate. That will cause
2354 walk_tree to walk into the chain as well. */
2355 if (code
== PARM_DECL
|| code
== TREE_LIST
)
2356 TREE_CHAIN (*tp
) = chain
;
2358 /* For now, we don't update BLOCKs when we make copies. So, we
2359 have to nullify all BIND_EXPRs. */
2360 if (TREE_CODE (*tp
) == BIND_EXPR
)
2361 BIND_EXPR_BLOCK (*tp
) = NULL_TREE
;
2364 else if (TREE_CODE_CLASS (code
) == tcc_type
)
2366 else if (TREE_CODE_CLASS (code
) == tcc_declaration
)
2368 else if (TREE_CODE_CLASS (code
) == tcc_constant
)
2371 gcc_assert (code
!= STATEMENT_LIST
);
2375 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2376 information indicating to what new SAVE_EXPR this one should be mapped,
2377 use that one. Otherwise, create a new node and enter it in ST. FN is
2378 the function into which the copy will be placed. */
2381 remap_save_expr (tree
*tp
, void *st_
, int *walk_subtrees
)
2383 splay_tree st
= (splay_tree
) st_
;
2387 /* See if we already encountered this SAVE_EXPR. */
2388 n
= splay_tree_lookup (st
, (splay_tree_key
) *tp
);
2390 /* If we didn't already remap this SAVE_EXPR, do so now. */
2393 t
= copy_node (*tp
);
2395 /* Remember this SAVE_EXPR. */
2396 splay_tree_insert (st
, (splay_tree_key
) *tp
, (splay_tree_value
) t
);
2397 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2398 splay_tree_insert (st
, (splay_tree_key
) t
, (splay_tree_value
) t
);
2402 /* We've already walked into this SAVE_EXPR; don't do it again. */
2404 t
= (tree
) n
->value
;
2407 /* Replace this SAVE_EXPR with the copy. */
2411 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2412 copies the declaration and enters it in the splay_tree in DATA (which is
2413 really an `inline_data *'). */
2416 mark_local_for_remap_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
2419 inline_data
*id
= (inline_data
*) data
;
2421 /* Don't walk into types. */
2425 else if (TREE_CODE (*tp
) == LABEL_EXPR
)
2427 tree decl
= TREE_OPERAND (*tp
, 0);
2429 /* Copy the decl and remember the copy. */
2430 insert_decl_map (id
, decl
,
2431 copy_decl_for_inlining (decl
, DECL_CONTEXT (decl
),
2432 DECL_CONTEXT (decl
)));
2438 /* Perform any modifications to EXPR required when it is unsaved. Does
2439 not recurse into EXPR's subtrees. */
2442 unsave_expr_1 (tree expr
)
2444 switch (TREE_CODE (expr
))
2447 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2448 It's OK for this to happen if it was part of a subtree that
2449 isn't immediately expanded, such as operand 2 of another
2451 if (TREE_OPERAND (expr
, 1))
2454 TREE_OPERAND (expr
, 1) = TREE_OPERAND (expr
, 3);
2455 TREE_OPERAND (expr
, 3) = NULL_TREE
;
2463 /* Called via walk_tree when an expression is unsaved. Using the
2464 splay_tree pointed to by ST (which is really a `splay_tree'),
2465 remaps all local declarations to appropriate replacements. */
2468 unsave_r (tree
*tp
, int *walk_subtrees
, void *data
)
2470 inline_data
*id
= (inline_data
*) data
;
2471 splay_tree st
= id
->decl_map
;
2474 /* Only a local declaration (variable or label). */
2475 if ((TREE_CODE (*tp
) == VAR_DECL
&& !TREE_STATIC (*tp
))
2476 || TREE_CODE (*tp
) == LABEL_DECL
)
2478 /* Lookup the declaration. */
2479 n
= splay_tree_lookup (st
, (splay_tree_key
) *tp
);
2481 /* If it's there, remap it. */
2483 *tp
= (tree
) n
->value
;
2486 else if (TREE_CODE (*tp
) == STATEMENT_LIST
)
2487 copy_statement_list (tp
);
2488 else if (TREE_CODE (*tp
) == BIND_EXPR
)
2489 copy_bind_expr (tp
, walk_subtrees
, id
);
2490 else if (TREE_CODE (*tp
) == SAVE_EXPR
)
2491 remap_save_expr (tp
, st
, walk_subtrees
);
2494 copy_tree_r (tp
, walk_subtrees
, NULL
);
2496 /* Do whatever unsaving is required. */
2497 unsave_expr_1 (*tp
);
2500 /* Keep iterating. */
2504 /* Copies everything in EXPR and replaces variables, labels
2505 and SAVE_EXPRs local to EXPR. */
2508 unsave_expr_now (tree expr
)
2512 /* There's nothing to do for NULL_TREE. */
2517 memset (&id
, 0, sizeof (id
));
2518 id
.callee
= current_function_decl
;
2519 id
.caller
= current_function_decl
;
2520 id
.decl_map
= splay_tree_new (splay_tree_compare_pointers
, NULL
, NULL
);
2522 /* Walk the tree once to find local labels. */
2523 walk_tree_without_duplicates (&expr
, mark_local_for_remap_r
, &id
);
2525 /* Walk the tree again, copying, remapping, and unsaving. */
2526 walk_tree (&expr
, unsave_r
, &id
, NULL
);
2529 splay_tree_delete (id
.decl_map
);
2534 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
2537 debug_find_tree_1 (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
, void *data
)
2546 debug_find_tree (tree top
, tree search
)
2548 return walk_tree_without_duplicates (&top
, debug_find_tree_1
, search
) != 0;
2552 /* Declare the variables created by the inliner. Add all the variables in
2553 VARS to BIND_EXPR. */
2556 declare_inline_vars (tree block
, tree vars
)
2559 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
2560 DECL_SEEN_IN_BIND_EXPR_P (t
) = 1;
2563 BLOCK_VARS (block
) = chainon (BLOCK_VARS (block
), vars
);
2566 /* Returns true if we're inlining. */
2568 inlining_p (inline_data
*id
)
2570 return (!id
->saving_p
&& !id
->cloning_p
);