2 Copyright 2001, 2002, 2003, 2004, 2005, 2006 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 "langhooks.h"
38 #include "basic-block.h"
39 #include "tree-iterator.h"
42 #include "tree-mudflap.h"
43 #include "tree-flow.h"
46 #include "tree-flow.h"
47 #include "diagnostic.h"
50 #include "pointer-set.h"
52 #include "value-prof.h"
54 /* I'm not real happy about this, but we need to handle gimple and
56 #include "tree-gimple.h"
58 /* Inlining, Cloning, Versioning, Parallelization
60 Inlining: a function body is duplicated, but the PARM_DECLs are
61 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
62 GIMPLE_MODIFY_STMTs 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 Cloning: (only in C++) We have one body for a con/de/structor, and
68 multiple function decls, each with a unique parameter list.
69 Duplicate the body, using the given splay tree; some parameters
70 will become constants (like 0 or 1).
72 Versioning: a function body is duplicated and the result is a new
73 function rather than into blocks of an existing function as with
74 inlining. Some parameters will become constants.
76 Parallelization: a region of a function is duplicated resulting in
77 a new function. Variables may be replaced with complex expressions
78 to enable shared variable semantics.
80 All of these will simultaneously lookup any callgraph edges. If
81 we're going to inline the duplicated function body, and the given
82 function has some cloned callgraph nodes (one for each place this
83 function will be inlined) those callgraph edges will be duplicated.
84 If we're cloning the body, those callgraph edges will be
85 updated to point into the new body. (Note that the original
86 callgraph node and edge list will not be altered.)
88 See the CALL_EXPR handling case in copy_body_r (). */
90 /* 0 if we should not perform inlining.
91 1 if we should expand functions calls inline at the tree level.
92 2 if we should consider *all* functions to be inline
95 int flag_inline_trees = 0;
99 o In order to make inlining-on-trees work, we pessimized
100 function-local static constants. In particular, they are now
101 always output, even when not addressed. Fix this by treating
102 function-local static constants just like global static
103 constants; the back-end already knows not to output them if they
106 o Provide heuristics to clamp inlining of recursive template
111 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
112 static tree copy_generic_body (copy_body_data *);
113 static bool inlinable_function_p (tree);
114 static void remap_block (tree *, copy_body_data *);
115 static tree remap_decls (tree, copy_body_data *);
116 static void copy_bind_expr (tree *, int *, copy_body_data *);
117 static tree mark_local_for_remap_r (tree *, int *, void *);
118 static void unsave_expr_1 (tree);
119 static tree unsave_r (tree *, int *, void *);
120 static void declare_inline_vars (tree, tree);
121 static void remap_save_expr (tree *, void *, int *);
122 static void add_lexical_block (tree current_block, tree new_block);
123 static tree copy_decl_to_var (tree, copy_body_data *);
124 static tree copy_result_decl_to_var (tree, copy_body_data *);
125 static tree copy_decl_no_change (tree, copy_body_data *);
126 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
128 /* Insert a tree->tree mapping for ID. Despite the name suggests
129 that the trees should be variables, it is used for more than that. */
132 insert_decl_map (copy_body_data *id, tree key, tree value)
134 splay_tree_insert (id->decl_map, (splay_tree_key) key,
135 (splay_tree_value) value);
137 /* Always insert an identity map as well. If we see this same new
138 node again, we won't want to duplicate it a second time. */
140 splay_tree_insert (id->decl_map, (splay_tree_key) value,
141 (splay_tree_value) value);
144 /* Remap DECL during the copying of the BLOCK tree for the function. */
147 remap_decl (tree decl, copy_body_data *id)
152 /* We only remap local variables in the current function. */
155 /* See if we have remapped this declaration. */
157 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
159 /* If we didn't already have an equivalent for this declaration,
163 /* Make a copy of the variable or label. */
164 tree t = id->copy_decl (decl, id);
166 /* Remember it, so that if we encounter this local entity again
167 we can reuse this copy. Do this early because remap_type may
168 need this decl for TYPE_STUB_DECL. */
169 insert_decl_map (id, decl, t);
174 /* Remap types, if necessary. */
175 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
176 if (TREE_CODE (t) == TYPE_DECL)
177 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
179 /* Remap sizes as necessary. */
180 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
181 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
183 /* If fields, do likewise for offset and qualifier. */
184 if (TREE_CODE (t) == FIELD_DECL)
186 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
187 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
188 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
194 return unshare_expr ((tree) n->value);
198 remap_type_1 (tree type, copy_body_data *id)
200 splay_tree_node node;
206 /* See if we have remapped this type. */
207 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
209 return (tree) node->value;
211 /* The type only needs remapping if it's variably modified. */
212 if (! variably_modified_type_p (type, id->src_fn))
214 insert_decl_map (id, type, type);
218 /* We do need a copy. build and register it now. If this is a pointer or
219 reference type, remap the designated type and make a new pointer or
221 if (TREE_CODE (type) == POINTER_TYPE)
223 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
225 TYPE_REF_CAN_ALIAS_ALL (type));
226 insert_decl_map (id, type, new);
229 else if (TREE_CODE (type) == REFERENCE_TYPE)
231 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
233 TYPE_REF_CAN_ALIAS_ALL (type));
234 insert_decl_map (id, type, new);
238 new = copy_node (type);
240 insert_decl_map (id, type, new);
242 /* This is a new type, not a copy of an old type. Need to reassociate
243 variants. We can handle everything except the main variant lazily. */
244 t = TYPE_MAIN_VARIANT (type);
247 t = remap_type (t, id);
248 TYPE_MAIN_VARIANT (new) = t;
249 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
250 TYPE_NEXT_VARIANT (t) = new;
254 TYPE_MAIN_VARIANT (new) = new;
255 TYPE_NEXT_VARIANT (new) = NULL;
258 if (TYPE_STUB_DECL (type))
259 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
261 /* Lazily create pointer and reference types. */
262 TYPE_POINTER_TO (new) = NULL;
263 TYPE_REFERENCE_TO (new) = NULL;
265 switch (TREE_CODE (new))
271 t = TYPE_MIN_VALUE (new);
272 if (t && TREE_CODE (t) != INTEGER_CST)
273 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
275 t = TYPE_MAX_VALUE (new);
276 if (t && TREE_CODE (t) != INTEGER_CST)
277 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
281 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
282 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
286 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
287 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
292 case QUAL_UNION_TYPE:
296 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
298 t = remap_decl (f, id);
299 DECL_CONTEXT (t) = new;
303 TYPE_FIELDS (new) = nreverse (nf);
309 /* Shouldn't have been thought variable sized. */
313 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
314 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
320 remap_type (tree type, copy_body_data *id)
322 splay_tree_node node;
327 /* See if we have remapped this type. */
328 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
330 return (tree) node->value;
332 /* The type only needs remapping if it's variably modified. */
333 if (! variably_modified_type_p (type, id->src_fn))
335 insert_decl_map (id, type, type);
339 return remap_type_1 (type, id);
343 remap_decls (tree decls, copy_body_data *id)
346 tree new_decls = NULL_TREE;
348 /* Remap its variables. */
349 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
353 /* We can not chain the local static declarations into the unexpanded_var_list
354 as we can't duplicate them or break one decl rule. Go ahead and link
355 them into unexpanded_var_list. */
356 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
357 && !DECL_EXTERNAL (old_var))
359 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
360 cfun->unexpanded_var_list);
364 /* Remap the variable. */
365 new_var = remap_decl (old_var, id);
367 /* If we didn't remap this variable, so we can't mess with its
368 TREE_CHAIN. If we remapped this variable to the return slot, it's
369 already declared somewhere else, so don't declare it here. */
370 if (!new_var || new_var == id->retvar)
374 gcc_assert (DECL_P (new_var));
375 TREE_CHAIN (new_var) = new_decls;
380 return nreverse (new_decls);
383 /* Copy the BLOCK to contain remapped versions of the variables
384 therein. And hook the new block into the block-tree. */
387 remap_block (tree *block, copy_body_data *id)
393 /* Make the new block. */
395 new_block = make_node (BLOCK);
396 TREE_USED (new_block) = TREE_USED (old_block);
397 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
398 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
401 /* Remap its variables. */
402 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
406 if (id->transform_lang_insert_block)
407 lang_hooks.decls.insert_block (new_block);
409 /* Remember the remapped block. */
410 insert_decl_map (id, old_block, new_block);
413 /* Copy the whole block tree and root it in id->block. */
415 remap_blocks (tree block, copy_body_data *id)
423 remap_block (&new, id);
424 gcc_assert (new != block);
425 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
426 add_lexical_block (new, remap_blocks (t, id));
431 copy_statement_list (tree *tp)
433 tree_stmt_iterator oi, ni;
436 new = alloc_stmt_list ();
437 ni = tsi_start (new);
438 oi = tsi_start (*tp);
441 for (; !tsi_end_p (oi); tsi_next (&oi))
442 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
446 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
448 tree block = BIND_EXPR_BLOCK (*tp);
449 /* Copy (and replace) the statement. */
450 copy_tree_r (tp, walk_subtrees, NULL);
453 remap_block (&block, id);
454 BIND_EXPR_BLOCK (*tp) = block;
457 if (BIND_EXPR_VARS (*tp))
458 /* This will remap a lot of the same decls again, but this should be
460 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
463 /* Called from copy_body_id via walk_tree. DATA is really an
464 `copy_body_data *'. */
467 copy_body_r (tree *tp, int *walk_subtrees, void *data)
469 copy_body_data *id = (copy_body_data *) data;
470 tree fn = id->src_fn;
473 /* Begin by recognizing trees that we'll completely rewrite for the
474 inlining context. Our output for these trees is completely
475 different from out input (e.g. RETURN_EXPR is deleted, and morphs
476 into an edge). Further down, we'll handle trees that get
477 duplicated and/or tweaked. */
479 /* When requested, RETURN_EXPRs should be transformed to just the
480 contained GIMPLE_MODIFY_STMT. The branch semantics of the return will
481 be handled elsewhere by manipulating the CFG rather than a statement. */
482 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
484 tree assignment = TREE_OPERAND (*tp, 0);
486 /* If we're returning something, just turn that into an
487 assignment into the equivalent of the original RESULT_DECL.
488 If the "assignment" is just the result decl, the result
489 decl has already been set (e.g. a recent "foo (&result_decl,
490 ...)"); just toss the entire RETURN_EXPR. */
491 if (assignment && TREE_CODE (assignment) == GIMPLE_MODIFY_STMT)
493 /* Replace the RETURN_EXPR with (a copy of) the
494 GIMPLE_MODIFY_STMT hanging underneath. */
495 *tp = copy_node (assignment);
497 else /* Else the RETURN_EXPR returns no value. */
500 return (tree) (void *)1;
504 /* Local variables and labels need to be replaced by equivalent
505 variables. We don't want to copy static variables; there's only
506 one of those, no matter how many times we inline the containing
507 function. Similarly for globals from an outer function. */
508 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
512 /* Remap the declaration. */
513 new_decl = remap_decl (*tp, id);
514 gcc_assert (new_decl);
515 /* Replace this variable with the copy. */
516 STRIP_TYPE_NOPS (new_decl);
520 else if (TREE_CODE (*tp) == STATEMENT_LIST)
521 copy_statement_list (tp);
522 else if (TREE_CODE (*tp) == SAVE_EXPR)
523 remap_save_expr (tp, id->decl_map, walk_subtrees);
524 else if (TREE_CODE (*tp) == LABEL_DECL
525 && (! DECL_CONTEXT (*tp)
526 || decl_function_context (*tp) == id->src_fn))
527 /* These may need to be remapped for EH handling. */
528 *tp = remap_decl (*tp, id);
529 else if (TREE_CODE (*tp) == BIND_EXPR)
530 copy_bind_expr (tp, walk_subtrees, id);
531 /* Types may need remapping as well. */
532 else if (TYPE_P (*tp))
533 *tp = remap_type (*tp, id);
535 /* If this is a constant, we have to copy the node iff the type will be
536 remapped. copy_tree_r will not copy a constant. */
537 else if (CONSTANT_CLASS_P (*tp))
539 tree new_type = remap_type (TREE_TYPE (*tp), id);
541 if (new_type == TREE_TYPE (*tp))
544 else if (TREE_CODE (*tp) == INTEGER_CST)
545 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
546 TREE_INT_CST_HIGH (*tp));
549 *tp = copy_node (*tp);
550 TREE_TYPE (*tp) = new_type;
554 /* Otherwise, just copy the node. Note that copy_tree_r already
555 knows not to copy VAR_DECLs, etc., so this is safe. */
558 /* Here we handle trees that are not completely rewritten.
559 First we detect some inlining-induced bogosities for
561 if (TREE_CODE (*tp) == GIMPLE_MODIFY_STMT
562 && GIMPLE_STMT_OPERAND (*tp, 0) == GIMPLE_STMT_OPERAND (*tp, 1)
563 && (lang_hooks.tree_inlining.auto_var_in_fn_p
564 (GIMPLE_STMT_OPERAND (*tp, 0), fn)))
566 /* Some assignments VAR = VAR; don't generate any rtl code
567 and thus don't count as variable modification. Avoid
568 keeping bogosities like 0 = 0. */
569 tree decl = GIMPLE_STMT_OPERAND (*tp, 0), value;
572 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
575 value = (tree) n->value;
576 STRIP_TYPE_NOPS (value);
577 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
579 *tp = build_empty_stmt ();
580 return copy_body_r (tp, walk_subtrees, data);
584 else if (TREE_CODE (*tp) == INDIRECT_REF)
586 /* Get rid of *& from inline substitutions that can happen when a
587 pointer argument is an ADDR_EXPR. */
588 tree decl = TREE_OPERAND (*tp, 0);
591 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
596 /* If we happen to get an ADDR_EXPR in n->value, strip
597 it manually here as we'll eventually get ADDR_EXPRs
598 which lie about their types pointed to. In this case
599 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
600 but we absolutely rely on that. As fold_indirect_ref
601 does other useful transformations, try that first, though. */
602 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
603 new = unshare_expr ((tree)n->value);
605 *tp = fold_indirect_ref_1 (type, new);
608 if (TREE_CODE (new) == ADDR_EXPR)
609 *tp = TREE_OPERAND (new, 0);
612 *tp = build1 (INDIRECT_REF, type, new);
613 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
621 /* Here is the "usual case". Copy this tree node, and then
622 tweak some special cases. */
623 copy_tree_r (tp, walk_subtrees, NULL);
625 /* If EXPR has block defined, map it to newly constructed block.
626 When inlining we want EXPRs without block appear in the block
628 if (EXPR_P (*tp) || GIMPLE_STMT_P (*tp))
630 new_block = id->block;
631 if (TREE_BLOCK (*tp))
634 n = splay_tree_lookup (id->decl_map,
635 (splay_tree_key) TREE_BLOCK (*tp));
637 new_block = (tree) n->value;
639 TREE_BLOCK (*tp) = new_block;
642 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
643 TREE_OPERAND (*tp, 0) =
646 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
648 if (!GIMPLE_TUPLE_P (*tp))
649 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
651 /* The copied TARGET_EXPR has never been expanded, even if the
652 original node was expanded already. */
653 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
655 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
656 TREE_OPERAND (*tp, 3) = NULL_TREE;
659 /* Variable substitution need not be simple. In particular, the
660 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
661 and friends are up-to-date. */
662 else if (TREE_CODE (*tp) == ADDR_EXPR)
664 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
665 /* Handle the case where we substituted an INDIRECT_REF
666 into the operand of the ADDR_EXPR. */
667 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
668 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
670 recompute_tree_invariant_for_addr_expr (*tp);
675 /* Keep iterating. */
679 /* Copy basic block, scale profile accordingly. Edges will be taken care of
683 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
685 block_stmt_iterator bsi, copy_bsi;
686 basic_block copy_basic_block;
688 /* create_basic_block() will append every new block to
689 basic_block_info automatically. */
690 copy_basic_block = create_basic_block (NULL, (void *) 0,
691 (basic_block) bb->prev_bb->aux);
692 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
693 copy_basic_block->frequency = (bb->frequency
694 * frequency_scale / REG_BR_PROB_BASE);
695 copy_bsi = bsi_start (copy_basic_block);
697 for (bsi = bsi_start (bb);
698 !bsi_end_p (bsi); bsi_next (&bsi))
700 tree stmt = bsi_stmt (bsi);
701 tree orig_stmt = stmt;
703 walk_tree (&stmt, copy_body_r, id, NULL);
705 /* RETURN_EXPR might be removed,
706 this is signalled by making stmt pointer NULL. */
711 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
713 /* With return slot optimization we can end up with
714 non-gimple (foo *)&this->m, fix that here. */
715 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
716 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == NOP_EXPR
717 && !is_gimple_val (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0)))
718 gimplify_stmt (&stmt);
720 bsi_insert_after (©_bsi, stmt, BSI_NEW_STMT);
721 call = get_call_expr_in (stmt);
722 /* We're duplicating a CALL_EXPR. Find any corresponding
723 callgraph edges and update or duplicate them. */
724 if (call && (decl = get_callee_fndecl (call)))
726 struct cgraph_node *node;
727 struct cgraph_edge *edge;
729 switch (id->transform_call_graph_edges)
731 case CB_CGE_DUPLICATE:
732 edge = cgraph_edge (id->src_node, orig_stmt);
734 cgraph_clone_edge (edge, id->dst_node, stmt,
735 REG_BR_PROB_BASE, 1, true);
738 case CB_CGE_MOVE_CLONES:
739 for (node = id->dst_node->next_clone;
741 node = node->next_clone)
743 edge = cgraph_edge (node, orig_stmt);
745 cgraph_set_call_stmt (edge, stmt);
750 edge = cgraph_edge (id->dst_node, orig_stmt);
752 cgraph_set_call_stmt (edge, stmt);
759 /* If you think we can abort here, you are wrong.
760 There is no region 0 in tree land. */
761 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
764 if (tree_could_throw_p (stmt))
766 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
767 /* Add an entry for the copied tree in the EH hashtable.
768 When cloning or versioning, use the hashtable in
769 cfun, and just copy the EH number. When inlining, use the
770 hashtable in the caller, and adjust the region number. */
772 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
774 /* If this tree doesn't have a region associated with it,
775 and there is a "current region,"
776 then associate this tree with the current region
777 and add edges associated with this region. */
778 if ((lookup_stmt_eh_region_fn (id->src_cfun,
780 && id->eh_region > 0)
781 && tree_could_throw_p (stmt))
782 add_stmt_to_eh_region (stmt, id->eh_region);
786 return copy_basic_block;
789 /* Copy edges from BB into its copy constructed earlier, scale profile
790 accordingly. Edges will be taken care of later. Assume aux
791 pointers to point to the copies of each BB. */
793 copy_edges_for_bb (basic_block bb, int count_scale)
795 basic_block new_bb = (basic_block) bb->aux;
798 block_stmt_iterator bsi;
801 /* Use the indices from the original blocks to create edges for the
803 FOR_EACH_EDGE (old_edge, ei, bb->succs)
804 if (!(old_edge->flags & EDGE_EH))
808 flags = old_edge->flags;
810 /* Return edges do get a FALLTHRU flag when the get inlined. */
811 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
812 && old_edge->dest->aux != EXIT_BLOCK_PTR)
813 flags |= EDGE_FALLTHRU;
814 new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
815 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
816 new->probability = old_edge->probability;
819 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
822 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
826 copy_stmt = bsi_stmt (bsi);
827 update_stmt (copy_stmt);
828 /* Do this before the possible split_block. */
831 /* If this tree could throw an exception, there are two
832 cases where we need to add abnormal edge(s): the
833 tree wasn't in a region and there is a "current
834 region" in the caller; or the original tree had
835 EH edges. In both cases split the block after the tree,
836 and add abnormal edge(s) as needed; we need both
837 those from the callee and the caller.
838 We check whether the copy can throw, because the const
839 propagation can change an INDIRECT_REF which throws
840 into a COMPONENT_REF which doesn't. If the copy
841 can throw, the original could also throw. */
843 if (tree_can_throw_internal (copy_stmt))
845 if (!bsi_end_p (bsi))
846 /* Note that bb's predecessor edges aren't necessarily
847 right at this point; split_block doesn't care. */
849 edge e = split_block (new_bb, copy_stmt);
851 bsi = bsi_start (new_bb);
854 make_eh_edges (copy_stmt);
859 /* Wrapper for remap_decl so it can be used as a callback. */
861 remap_decl_1 (tree decl, void *data)
863 return remap_decl (decl, (copy_body_data *) data);
866 /* Make a copy of the body of FN so that it can be inserted inline in
867 another function. Walks FN via CFG, returns new fndecl. */
870 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
871 basic_block entry_block_map, basic_block exit_block_map)
873 tree callee_fndecl = id->src_fn;
874 /* Original cfun for the callee, doesn't change. */
875 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
876 /* Copy, built by this function. */
877 struct function *new_cfun;
878 /* Place to copy from; when a copy of the function was saved off earlier,
879 use that instead of the main copy. */
880 struct function *cfun_to_copy =
881 (struct function *) ggc_alloc_cleared (sizeof (struct function));
883 tree new_fndecl = NULL;
884 int count_scale, frequency_scale;
886 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
887 count_scale = (REG_BR_PROB_BASE * count
888 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
892 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
893 frequency_scale = (REG_BR_PROB_BASE * frequency
895 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
897 frequency_scale = count_scale;
899 /* Register specific tree functions. */
900 tree_register_cfg_hooks ();
902 /* Must have a CFG here at this point. */
903 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
904 (DECL_STRUCT_FUNCTION (callee_fndecl)));
906 *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
908 id->src_cfun = cfun_to_copy;
910 /* If requested, create new basic_block_info and label_to_block_maps.
911 Otherwise, insert our new blocks and labels into the existing cfg. */
912 if (id->transform_new_cfg)
915 (struct function *) ggc_alloc_cleared (sizeof (struct function));
916 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
917 new_cfun->cfg = NULL;
918 new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
919 new_cfun->ib_boundaries_block = NULL;
920 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
921 push_cfun (new_cfun);
922 init_empty_tree_cfg ();
924 ENTRY_BLOCK_PTR->count =
925 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
927 ENTRY_BLOCK_PTR->frequency =
928 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
929 frequency_scale / REG_BR_PROB_BASE);
930 EXIT_BLOCK_PTR->count =
931 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
933 EXIT_BLOCK_PTR->frequency =
934 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
935 frequency_scale / REG_BR_PROB_BASE);
937 entry_block_map = ENTRY_BLOCK_PTR;
938 exit_block_map = EXIT_BLOCK_PTR;
941 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
942 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
944 /* Duplicate any exception-handling regions. */
947 if (id->transform_new_cfg)
948 init_eh_for_function ();
950 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
953 /* Use aux pointers to map the original blocks to copy. */
954 FOR_EACH_BB_FN (bb, cfun_to_copy)
955 bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
956 /* Now that we've duplicated the blocks, duplicate their edges. */
957 FOR_ALL_BB_FN (bb, cfun_to_copy)
958 copy_edges_for_bb (bb, count_scale);
959 FOR_ALL_BB_FN (bb, cfun_to_copy)
962 if (id->transform_new_cfg)
968 /* Make a copy of the body of FN so that it can be inserted inline in
972 copy_generic_body (copy_body_data *id)
975 tree fndecl = id->src_fn;
977 body = DECL_SAVED_TREE (fndecl);
978 walk_tree (&body, copy_body_r, id, NULL);
984 copy_body (copy_body_data *id, gcov_type count, int frequency,
985 basic_block entry_block_map, basic_block exit_block_map)
987 tree fndecl = id->src_fn;
990 /* If this body has a CFG, walk CFG and copy. */
991 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
992 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
997 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
998 defined in function FN, or of a data member thereof. */
1001 self_inlining_addr_expr (tree value, tree fn)
1005 if (TREE_CODE (value) != ADDR_EXPR)
1008 var = get_base_address (TREE_OPERAND (value, 0));
1010 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
1014 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1015 basic_block bb, tree *vars)
1021 /* If the parameter is never assigned to, we may not need to
1022 create a new variable here at all. Instead, we may be able
1023 to just use the argument value. */
1024 if (TREE_READONLY (p)
1025 && !TREE_ADDRESSABLE (p)
1026 && value && !TREE_SIDE_EFFECTS (value))
1028 /* We may produce non-gimple trees by adding NOPs or introduce
1029 invalid sharing when operand is not really constant.
1030 It is not big deal to prohibit constant propagation here as
1031 we will constant propagate in DOM1 pass anyway. */
1032 if (is_gimple_min_invariant (value)
1033 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1034 /* We have to be very careful about ADDR_EXPR. Make sure
1035 the base variable isn't a local variable of the inlined
1036 function, e.g., when doing recursive inlining, direct or
1037 mutually-recursive or whatever, which is why we don't
1038 just test whether fn == current_function_decl. */
1039 && ! self_inlining_addr_expr (value, fn))
1041 insert_decl_map (id, p, value);
1046 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1047 here since the type of this decl must be visible to the calling
1049 var = copy_decl_to_var (p, id);
1051 /* See if the frontend wants to pass this by invisible reference. If
1052 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1053 replace uses of the PARM_DECL with dereferences. */
1054 if (TREE_TYPE (var) != TREE_TYPE (p)
1055 && POINTER_TYPE_P (TREE_TYPE (var))
1056 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1058 insert_decl_map (id, var, var);
1059 var_sub = build_fold_indirect_ref (var);
1064 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1065 that way, when the PARM_DECL is encountered, it will be
1066 automatically replaced by the VAR_DECL. */
1067 insert_decl_map (id, p, var_sub);
1069 /* Declare this new variable. */
1070 TREE_CHAIN (var) = *vars;
1073 /* Make gimplifier happy about this variable. */
1074 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1076 /* Even if P was TREE_READONLY, the new VAR should not be.
1077 In the original code, we would have constructed a
1078 temporary, and then the function body would have never
1079 changed the value of P. However, now, we will be
1080 constructing VAR directly. The constructor body may
1081 change its value multiple times as it is being
1082 constructed. Therefore, it must not be TREE_READONLY;
1083 the back-end assumes that TREE_READONLY variable is
1084 assigned to only once. */
1085 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1086 TREE_READONLY (var) = 0;
1088 /* Initialize this VAR_DECL from the equivalent argument. Convert
1089 the argument to the proper type in case it was promoted. */
1092 tree rhs = fold_convert (TREE_TYPE (var), value);
1093 block_stmt_iterator bsi = bsi_last (bb);
1095 if (rhs == error_mark_node)
1098 STRIP_USELESS_TYPE_CONVERSION (rhs);
1100 /* We want to use GIMPLE_MODIFY_STMT, not INIT_EXPR here so that we
1101 keep our trees in gimple form. */
1102 init_stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, rhs);
1104 /* If we did not create a gimple value and we did not create a gimple
1105 cast of a gimple value, then we will need to gimplify INIT_STMTS
1106 at the end. Note that is_gimple_cast only checks the outer
1107 tree code, not its operand. Thus the explicit check that its
1108 operand is a gimple value. */
1109 if (!is_gimple_val (rhs)
1110 && (!is_gimple_cast (rhs)
1111 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1112 gimplify_stmt (&init_stmt);
1114 /* If VAR represents a zero-sized variable, it's possible that the
1115 assignment statment may result in no gimple statements. */
1117 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1121 /* Generate code to initialize the parameters of the function at the
1122 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1125 initialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
1126 tree fn, basic_block bb)
1131 tree vars = NULL_TREE;
1134 /* Figure out what the parameters are. */
1135 parms = DECL_ARGUMENTS (fn);
1137 /* Loop through the parameter declarations, replacing each with an
1138 equivalent VAR_DECL, appropriately initialized. */
1139 for (p = parms, a = args; p;
1140 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1146 /* Find the initializer. */
1147 value = lang_hooks.tree_inlining.convert_parm_for_inlining
1148 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1150 setup_one_parameter (id, p, value, fn, bb, &vars);
1153 /* Initialize the static chain. */
1154 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1155 gcc_assert (fn != current_function_decl);
1158 /* No static chain? Seems like a bug in tree-nested.c. */
1159 gcc_assert (static_chain);
1161 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1164 declare_inline_vars (id->block, vars);
1167 /* Declare a return variable to replace the RESULT_DECL for the
1168 function we are calling. An appropriate DECL_STMT is returned.
1169 The USE_STMT is filled to contain a use of the declaration to
1170 indicate the return value of the function.
1172 RETURN_SLOT_ADDR, if non-null, was a fake parameter that
1173 took the address of the result. MODIFY_DEST, if non-null, was the LHS of
1174 the GIMPLE_MODIFY_STMT to which this call is the RHS.
1176 The return value is a (possibly null) value that is the result of the
1177 function as seen by the callee. *USE_P is a (possibly null) value that
1178 holds the result as seen by the caller. */
1181 declare_return_variable (copy_body_data *id, tree return_slot_addr,
1182 tree modify_dest, tree *use_p)
1184 tree callee = id->src_fn;
1185 tree caller = id->dst_fn;
1186 tree result = DECL_RESULT (callee);
1187 tree callee_type = TREE_TYPE (result);
1188 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1191 /* We don't need to do anything for functions that don't return
1193 if (!result || VOID_TYPE_P (callee_type))
1199 /* If there was a return slot, then the return value is the
1200 dereferenced address of that object. */
1201 if (return_slot_addr)
1203 /* The front end shouldn't have used both return_slot_addr and
1204 a modify expression. */
1205 gcc_assert (!modify_dest);
1206 if (DECL_BY_REFERENCE (result))
1207 var = return_slot_addr;
1209 var = build_fold_indirect_ref (return_slot_addr);
1210 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1211 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1212 && !DECL_GIMPLE_REG_P (result)
1214 DECL_GIMPLE_REG_P (var) = 0;
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_ADDRESSABLE (result))
1244 tree base_m = get_base_address (modify_dest);
1246 /* If the base isn't a decl, then it's a pointer, and we don't
1247 know where that's going to go. */
1248 if (!DECL_P (base_m))
1250 else if (is_global_var (base_m))
1252 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1253 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1254 && !DECL_GIMPLE_REG_P (result)
1255 && DECL_GIMPLE_REG_P (base_m))
1257 else if (!TREE_ADDRESSABLE (base_m))
1269 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1271 var = copy_result_decl_to_var (result, id);
1273 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1274 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1275 = tree_cons (NULL_TREE, var,
1276 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1278 /* Do not have the rest of GCC warn about this variable as it should
1279 not be visible to the user. */
1280 TREE_NO_WARNING (var) = 1;
1282 declare_inline_vars (id->block, var);
1284 /* Build the use expr. If the return type of the function was
1285 promoted, convert it back to the expected type. */
1287 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1288 use = fold_convert (caller_type, var);
1290 STRIP_USELESS_TYPE_CONVERSION (use);
1292 if (DECL_BY_REFERENCE (result))
1293 var = build_fold_addr_expr (var);
1296 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1297 way, when the RESULT_DECL is encountered, it will be
1298 automatically replaced by the VAR_DECL. */
1299 insert_decl_map (id, result, var);
1301 /* Remember this so we can ignore it in remap_decls. */
1308 /* Returns nonzero if a function can be inlined as a tree. */
1311 tree_inlinable_function_p (tree fn)
1313 return inlinable_function_p (fn);
1316 static const char *inline_forbidden_reason;
1319 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1323 tree fn = (tree) fnp;
1326 switch (TREE_CODE (node))
1329 /* Refuse to inline alloca call unless user explicitly forced so as
1330 this may change program's memory overhead drastically when the
1331 function using alloca is called in loop. In GCC present in
1332 SPEC2000 inlining into schedule_block cause it to require 2GB of
1333 RAM instead of 256MB. */
1334 if (alloca_call_p (node)
1335 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1337 inline_forbidden_reason
1338 = G_("function %q+F can never be inlined because it uses "
1339 "alloca (override using the always_inline attribute)");
1342 t = get_callee_fndecl (node);
1346 /* We cannot inline functions that call setjmp. */
1347 if (setjmp_call_p (t))
1349 inline_forbidden_reason
1350 = G_("function %q+F can never be inlined because it uses setjmp");
1354 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1355 switch (DECL_FUNCTION_CODE (t))
1357 /* We cannot inline functions that take a variable number of
1359 case BUILT_IN_VA_START:
1360 case BUILT_IN_STDARG_START:
1361 case BUILT_IN_NEXT_ARG:
1362 case BUILT_IN_VA_END:
1363 inline_forbidden_reason
1364 = G_("function %q+F can never be inlined because it "
1365 "uses variable argument lists");
1368 case BUILT_IN_LONGJMP:
1369 /* We can't inline functions that call __builtin_longjmp at
1370 all. The non-local goto machinery really requires the
1371 destination be in a different function. If we allow the
1372 function calling __builtin_longjmp to be inlined into the
1373 function calling __builtin_setjmp, Things will Go Awry. */
1374 inline_forbidden_reason
1375 = G_("function %q+F can never be inlined because "
1376 "it uses setjmp-longjmp exception handling");
1379 case BUILT_IN_NONLOCAL_GOTO:
1381 inline_forbidden_reason
1382 = G_("function %q+F can never be inlined because "
1383 "it uses non-local goto");
1386 case BUILT_IN_RETURN:
1387 case BUILT_IN_APPLY_ARGS:
1388 /* If a __builtin_apply_args caller would be inlined,
1389 it would be saving arguments of the function it has
1390 been inlined into. Similarly __builtin_return would
1391 return from the function the inline has been inlined into. */
1392 inline_forbidden_reason
1393 = G_("function %q+F can never be inlined because "
1394 "it uses __builtin_return or __builtin_apply_args");
1403 t = TREE_OPERAND (node, 0);
1405 /* We will not inline a function which uses computed goto. The
1406 addresses of its local labels, which may be tucked into
1407 global storage, are of course not constant across
1408 instantiations, which causes unexpected behavior. */
1409 if (TREE_CODE (t) != LABEL_DECL)
1411 inline_forbidden_reason
1412 = G_("function %q+F can never be inlined "
1413 "because it contains a computed goto");
1419 t = TREE_OPERAND (node, 0);
1420 if (DECL_NONLOCAL (t))
1422 /* We cannot inline a function that receives a non-local goto
1423 because we cannot remap the destination label used in the
1424 function that is performing the non-local goto. */
1425 inline_forbidden_reason
1426 = G_("function %q+F can never be inlined "
1427 "because it receives a non-local goto");
1434 /* We cannot inline a function of the form
1436 void F (int i) { struct S { int ar[i]; } s; }
1438 Attempting to do so produces a catch-22.
1439 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1440 UNION_TYPE nodes, then it goes into infinite recursion on a
1441 structure containing a pointer to its own type. If it doesn't,
1442 then the type node for S doesn't get adjusted properly when
1445 ??? This is likely no longer true, but it's too late in the 4.0
1446 cycle to try to find out. This should be checked for 4.1. */
1447 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1448 if (variably_modified_type_p (TREE_TYPE (t), NULL))
1450 inline_forbidden_reason
1451 = G_("function %q+F can never be inlined "
1452 "because it uses variable sized variables");
1463 /* Return subexpression representing possible alloca call, if any. */
1465 inline_forbidden_p (tree fndecl)
1467 location_t saved_loc = input_location;
1468 block_stmt_iterator bsi;
1470 tree ret = NULL_TREE;
1472 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1473 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1475 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1476 inline_forbidden_p_1, fndecl);
1482 input_location = saved_loc;
1486 /* Returns nonzero if FN is a function that does not have any
1487 fundamental inline blocking properties. */
1490 inlinable_function_p (tree fn)
1492 bool inlinable = true;
1494 /* If we've already decided this function shouldn't be inlined,
1495 there's no need to check again. */
1496 if (DECL_UNINLINABLE (fn))
1499 /* See if there is any language-specific reason it cannot be
1500 inlined. (It is important that this hook be called early because
1501 in C++ it may result in template instantiation.)
1502 If the function is not inlinable for language-specific reasons,
1503 it is left up to the langhook to explain why. */
1504 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1506 /* If we don't have the function body available, we can't inline it.
1507 However, this should not be recorded since we also get here for
1508 forward declared inline functions. Therefore, return at once. */
1509 if (!DECL_SAVED_TREE (fn))
1512 /* If we're not inlining at all, then we cannot inline this function. */
1513 else if (!flag_inline_trees)
1516 /* Only try to inline functions if DECL_INLINE is set. This should be
1517 true for all functions declared `inline', and for all other functions
1518 as well with -finline-functions.
1520 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1521 it's the front-end that must set DECL_INLINE in this case, because
1522 dwarf2out loses if a function that does not have DECL_INLINE set is
1523 inlined anyway. That is why we have both DECL_INLINE and
1524 DECL_DECLARED_INLINE_P. */
1525 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1526 here should be redundant. */
1527 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1530 else if (inline_forbidden_p (fn))
1532 /* See if we should warn about uninlinable functions. Previously,
1533 some of these warnings would be issued while trying to expand
1534 the function inline, but that would cause multiple warnings
1535 about functions that would for example call alloca. But since
1536 this a property of the function, just one warning is enough.
1537 As a bonus we can now give more details about the reason why a
1538 function is not inlinable.
1539 We only warn for functions declared `inline' by the user. */
1540 bool do_warning = (warn_inline
1542 && DECL_DECLARED_INLINE_P (fn)
1543 && !DECL_IN_SYSTEM_HEADER (fn));
1545 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1546 sorry (inline_forbidden_reason, fn);
1547 else if (do_warning)
1548 warning (OPT_Winline, inline_forbidden_reason, fn);
1553 /* Squirrel away the result so that we don't have to check again. */
1554 DECL_UNINLINABLE (fn) = !inlinable;
1559 /* Estimate the cost of a memory move. Use machine dependent
1560 word size and take possible memcpy call into account. */
1563 estimate_move_cost (tree type)
1567 size = int_size_in_bytes (type);
1569 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1570 /* Cost of a memcpy call, 3 arguments and the call. */
1573 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1576 /* Used by estimate_num_insns. Estimate number of instructions seen
1577 by given statement. */
1580 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1582 int *count = (int *) data;
1585 if (IS_TYPE_OR_DECL_P (x))
1590 /* Assume that constants and references counts nothing. These should
1591 be majorized by amount of operations among them we count later
1592 and are common target of CSE and similar optimizations. */
1593 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1596 switch (TREE_CODE (x))
1598 /* Containers have no cost. */
1605 case ALIGN_INDIRECT_REF:
1606 case MISALIGNED_INDIRECT_REF:
1608 case ARRAY_RANGE_REF:
1610 case EXC_PTR_EXPR: /* ??? */
1611 case FILTER_EXPR: /* ??? */
1614 case WITH_CLEANUP_EXPR:
1616 case VIEW_CONVERT_EXPR:
1621 case CASE_LABEL_EXPR:
1624 case EH_FILTER_EXPR:
1625 case STATEMENT_LIST:
1627 case NON_LVALUE_EXPR:
1630 case TRY_CATCH_EXPR:
1631 case TRY_FINALLY_EXPR:
1638 case WITH_SIZE_EXPR:
1644 /* We don't account constants for now. Assume that the cost is amortized
1645 by operations that do use them. We may re-consider this decision once
1646 we are able to optimize the tree before estimating its size and break
1647 out static initializers. */
1648 case IDENTIFIER_NODE:
1657 /* Try to estimate the cost of assignments. We have three cases to
1659 1) Simple assignments to registers;
1660 2) Stores to things that must live in memory. This includes
1661 "normal" stores to scalars, but also assignments of large
1662 structures, or constructors of big arrays;
1665 Let us look at the first two cases, assuming we have "a = b + C":
1666 <GIMPLE_MODIFY_STMT <var_decl "a">
1667 <plus_expr <var_decl "b"> <constant C>>
1668 If "a" is a GIMPLE register, the assignment to it is free on almost
1669 any target, because "a" usually ends up in a real register. Hence
1670 the only cost of this expression comes from the PLUS_EXPR, and we
1671 can ignore the GIMPLE_MODIFY_STMT.
1672 If "a" is not a GIMPLE register, the assignment to "a" will most
1673 likely be a real store, so the cost of the GIMPLE_MODIFY_STMT is the cost
1674 of moving something into "a", which we compute using the function
1677 The third case deals with TARGET_EXPRs, for which the semantics are
1678 that a temporary is assigned, unless the TARGET_EXPR itself is being
1679 assigned to something else. In the latter case we do not need the
1681 <GIMPLE_MODIFY_STMT <var_decl "a"> <target_expr>>, the
1682 GIMPLE_MODIFY_STMT is free. */
1684 case GIMPLE_MODIFY_STMT:
1685 /* Is the right and side a TARGET_EXPR? */
1686 if (TREE_CODE (GENERIC_TREE_OPERAND (x, 1)) == TARGET_EXPR)
1688 /* ... fall through ... */
1691 x = GENERIC_TREE_OPERAND (x, 0);
1692 /* Is this an assignments to a register? */
1693 if (is_gimple_reg (x))
1695 /* Otherwise it's a store, so fall through to compute the move cost. */
1698 *count += estimate_move_cost (TREE_TYPE (x));
1701 /* Assign cost of 1 to usual operations.
1702 ??? We may consider mapping RTL costs to this. */
1710 case FIX_TRUNC_EXPR:
1722 case VEC_LSHIFT_EXPR:
1723 case VEC_RSHIFT_EXPR:
1730 case TRUTH_ANDIF_EXPR:
1731 case TRUTH_ORIF_EXPR:
1732 case TRUTH_AND_EXPR:
1734 case TRUTH_XOR_EXPR:
1735 case TRUTH_NOT_EXPR:
1744 case UNORDERED_EXPR:
1757 case PREDECREMENT_EXPR:
1758 case PREINCREMENT_EXPR:
1759 case POSTDECREMENT_EXPR:
1760 case POSTINCREMENT_EXPR:
1766 case REALIGN_LOAD_EXPR:
1768 case REDUC_MAX_EXPR:
1769 case REDUC_MIN_EXPR:
1770 case REDUC_PLUS_EXPR:
1771 case WIDEN_SUM_EXPR:
1773 case VEC_WIDEN_MULT_HI_EXPR:
1774 case VEC_WIDEN_MULT_LO_EXPR:
1775 case VEC_UNPACK_HI_EXPR:
1776 case VEC_UNPACK_LO_EXPR:
1777 case VEC_PACK_MOD_EXPR:
1778 case VEC_PACK_SAT_EXPR:
1780 case WIDEN_MULT_EXPR:
1782 case VEC_EXTRACT_EVEN_EXPR:
1783 case VEC_EXTRACT_ODD_EXPR:
1784 case VEC_INTERLEAVE_HIGH_EXPR:
1785 case VEC_INTERLEAVE_LOW_EXPR:
1791 /* Few special cases of expensive operations. This is useful
1792 to avoid inlining on functions having too many of these. */
1793 case TRUNC_DIV_EXPR:
1795 case FLOOR_DIV_EXPR:
1796 case ROUND_DIV_EXPR:
1797 case EXACT_DIV_EXPR:
1798 case TRUNC_MOD_EXPR:
1800 case FLOOR_MOD_EXPR:
1801 case ROUND_MOD_EXPR:
1807 tree decl = get_callee_fndecl (x);
1810 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
1811 switch (DECL_FUNCTION_CODE (decl))
1813 case BUILT_IN_CONSTANT_P:
1816 case BUILT_IN_EXPECT:
1822 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1823 that does use function declaration to figure out the arguments. */
1826 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
1827 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
1831 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
1832 *count += estimate_move_cost (TREE_TYPE (arg));
1835 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
1848 /* OpenMP directives are generally very expensive. */
1858 /* Estimate number of instructions that will be created by expanding EXPR. */
1861 estimate_num_insns (tree expr)
1864 struct pointer_set_t *visited_nodes;
1866 block_stmt_iterator bsi;
1867 struct function *my_function;
1869 /* If we're given an entire function, walk the CFG. */
1870 if (TREE_CODE (expr) == FUNCTION_DECL)
1872 my_function = DECL_STRUCT_FUNCTION (expr);
1873 gcc_assert (my_function && my_function->cfg);
1874 visited_nodes = pointer_set_create ();
1875 FOR_EACH_BB_FN (bb, my_function)
1877 for (bsi = bsi_start (bb);
1881 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
1882 &num, visited_nodes);
1885 pointer_set_destroy (visited_nodes);
1888 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
1893 typedef struct function *function_p;
1895 DEF_VEC_P(function_p);
1896 DEF_VEC_ALLOC_P(function_p,heap);
1898 /* Initialized with NOGC, making this poisonous to the garbage collector. */
1899 static VEC(function_p,heap) *cfun_stack;
1902 push_cfun (struct function *new_cfun)
1904 VEC_safe_push (function_p, heap, cfun_stack, cfun);
1911 cfun = VEC_pop (function_p, cfun_stack);
1914 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
1916 add_lexical_block (tree current_block, tree new_block)
1920 /* Walk to the last sub-block. */
1921 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
1923 blk_p = &TREE_CHAIN (*blk_p))
1926 BLOCK_SUPERCONTEXT (new_block) = current_block;
1929 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1932 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
1940 tree return_slot_addr;
1942 location_t saved_location;
1943 struct cgraph_edge *cg_edge;
1945 basic_block return_block;
1947 block_stmt_iterator bsi, stmt_bsi;
1948 bool successfully_inlined = FALSE;
1949 bool purge_dead_abnormal_edges;
1953 /* See what we've got. */
1954 id = (copy_body_data *) data;
1957 /* Set input_location here so we get the right instantiation context
1958 if we call instantiate_decl from inlinable_function_p. */
1959 saved_location = input_location;
1960 if (EXPR_HAS_LOCATION (t))
1961 input_location = EXPR_LOCATION (t);
1963 /* From here on, we're only interested in CALL_EXPRs. */
1964 if (TREE_CODE (t) != CALL_EXPR)
1967 /* First, see if we can figure out what function is being called.
1968 If we cannot, then there is no hope of inlining the function. */
1969 fn = get_callee_fndecl (t);
1973 /* Turn forward declarations into real ones. */
1974 fn = cgraph_node (fn)->decl;
1976 /* If fn is a declaration of a function in a nested scope that was
1977 globally declared inline, we don't set its DECL_INITIAL.
1978 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1979 C++ front-end uses it for cdtors to refer to their internal
1980 declarations, that are not real functions. Fortunately those
1981 don't have trees to be saved, so we can tell by checking their
1983 if (! DECL_INITIAL (fn)
1984 && DECL_ABSTRACT_ORIGIN (fn)
1985 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1986 fn = DECL_ABSTRACT_ORIGIN (fn);
1988 /* Objective C and fortran still calls tree_rest_of_compilation directly.
1989 Kill this check once this is fixed. */
1990 if (!id->dst_node->analyzed)
1993 cg_edge = cgraph_edge (id->dst_node, stmt);
1995 /* Constant propagation on argument done during previous inlining
1996 may create new direct call. Produce an edge for it. */
1999 struct cgraph_node *dest = cgraph_node (fn);
2001 /* We have missing edge in the callgraph. This can happen in one case
2002 where previous inlining turned indirect call into direct call by
2003 constant propagating arguments. In all other cases we hit a bug
2004 (incorrect node sharing is most common reason for missing edges. */
2005 gcc_assert (dest->needed || !flag_unit_at_a_time);
2006 cgraph_create_edge (id->dst_node, dest, stmt,
2007 bb->count, bb->loop_depth)->inline_failed
2008 = N_("originally indirect function call not considered for inlining");
2012 /* Don't try to inline functions that are not well-suited to
2014 if (!cgraph_inline_p (cg_edge, &reason))
2016 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
2017 /* Avoid warnings during early inline pass. */
2018 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2020 sorry ("inlining failed in call to %q+F: %s", fn, reason);
2021 sorry ("called from here");
2023 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
2024 && !DECL_IN_SYSTEM_HEADER (fn)
2026 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
2027 /* Avoid warnings during early inline pass. */
2028 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2030 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
2032 warning (OPT_Winline, "called from here");
2036 fn = cg_edge->callee->decl;
2038 #ifdef ENABLE_CHECKING
2039 if (cg_edge->callee->decl != id->dst_node->decl)
2040 verify_cgraph_node (cg_edge->callee);
2043 /* We will be inlining this callee. */
2044 id->eh_region = lookup_stmt_eh_region (stmt);
2046 /* Split the block holding the CALL_EXPR. */
2047 e = split_block (bb, stmt);
2049 return_block = e->dest;
2052 /* split_block splits after the statement; work around this by
2053 moving the call into the second block manually. Not pretty,
2054 but seems easier than doing the CFG manipulation by hand
2055 when the CALL_EXPR is in the last statement of BB. */
2056 stmt_bsi = bsi_last (bb);
2057 bsi_remove (&stmt_bsi, false);
2059 /* If the CALL_EXPR was in the last statement of BB, it may have
2060 been the source of abnormal edges. In this case, schedule
2061 the removal of dead abnormal edges. */
2062 bsi = bsi_start (return_block);
2063 if (bsi_end_p (bsi))
2065 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2066 purge_dead_abnormal_edges = true;
2070 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2071 purge_dead_abnormal_edges = false;
2074 stmt_bsi = bsi_start (return_block);
2076 /* Build a block containing code to initialize the arguments, the
2077 actual inline expansion of the body, and a label for the return
2078 statements within the function to jump to. The type of the
2079 statement expression is the return type of the function call. */
2080 id->block = make_node (BLOCK);
2081 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2082 BLOCK_SOURCE_LOCATION (id->block) = input_location;
2083 add_lexical_block (TREE_BLOCK (stmt), id->block);
2085 /* Local declarations will be replaced by their equivalents in this
2088 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2091 /* Initialize the parameters. */
2092 args = TREE_OPERAND (t, 1);
2094 /* Record the function we are about to inline. */
2096 id->src_node = cg_edge->callee;
2098 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2100 if (DECL_INITIAL (fn))
2101 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
2103 /* Return statements in the function body will be replaced by jumps
2104 to the RET_LABEL. */
2106 gcc_assert (DECL_INITIAL (fn));
2107 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2109 /* Find the lhs to which the result of this call is assigned. */
2110 return_slot_addr = NULL;
2111 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2113 modify_dest = GIMPLE_STMT_OPERAND (stmt, 0);
2115 /* The function which we are inlining might not return a value,
2116 in which case we should issue a warning that the function
2117 does not return a value. In that case the optimizers will
2118 see that the variable to which the value is assigned was not
2119 initialized. We do not want to issue a warning about that
2120 uninitialized variable. */
2121 if (DECL_P (modify_dest))
2122 TREE_NO_WARNING (modify_dest) = 1;
2123 if (CALL_EXPR_RETURN_SLOT_OPT (t))
2125 return_slot_addr = build_fold_addr_expr (modify_dest);
2126 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2133 /* Declare the return variable for the function. */
2134 declare_return_variable (id, return_slot_addr,
2135 modify_dest, &use_retvar);
2137 /* This is it. Duplicate the callee body. Assume callee is
2138 pre-gimplified. Note that we must not alter the caller
2139 function in any way before this point, as this CALL_EXPR may be
2140 a self-referential call; if we're calling ourselves, we need to
2141 duplicate our body before altering anything. */
2142 copy_body (id, bb->count, bb->frequency, bb, return_block);
2144 /* Add local vars in this inlined callee to caller. */
2145 t_step = id->src_cfun->unexpanded_var_list;
2146 for (; t_step; t_step = TREE_CHAIN (t_step))
2148 var = TREE_VALUE (t_step);
2149 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2150 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2151 cfun->unexpanded_var_list);
2153 cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2154 cfun->unexpanded_var_list);
2158 splay_tree_delete (id->decl_map);
2161 /* If the inlined function returns a result that we care about,
2162 clobber the CALL_EXPR with a reference to the return variable. */
2163 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2166 maybe_clean_or_replace_eh_stmt (stmt, stmt);
2169 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2170 tsi_delink() will leave the iterator in a sane state. */
2171 bsi_remove (&stmt_bsi, true);
2173 if (purge_dead_abnormal_edges)
2174 tree_purge_dead_abnormal_call_edges (return_block);
2176 /* If the value of the new expression is ignored, that's OK. We
2177 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2178 the equivalent inlined version either. */
2179 TREE_USED (*tp) = 1;
2181 /* Output the inlining info for this abstract function, since it has been
2182 inlined. If we don't do this now, we can lose the information about the
2183 variables in the function when the blocks get blown away as soon as we
2184 remove the cgraph node. */
2185 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2187 /* Update callgraph if needed. */
2188 cgraph_remove_node (cg_edge->callee);
2190 id->block = NULL_TREE;
2191 successfully_inlined = TRUE;
2194 input_location = saved_location;
2195 return successfully_inlined;
2198 /* Expand call statements reachable from STMT_P.
2199 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2200 in a GIMPLE_MODIFY_STMT. See tree-gimple.c:get_call_expr_in(). We can
2201 unfortunately not use that function here because we need a pointer
2202 to the CALL_EXPR, not the tree itself. */
2205 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
2207 block_stmt_iterator bsi;
2209 /* Register specific tree functions. */
2210 tree_register_cfg_hooks ();
2211 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2213 tree *expr_p = bsi_stmt_ptr (bsi);
2214 tree stmt = *expr_p;
2216 if (TREE_CODE (*expr_p) == GIMPLE_MODIFY_STMT)
2217 expr_p = &GIMPLE_STMT_OPERAND (*expr_p, 1);
2218 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2219 expr_p = &TREE_OPERAND (*expr_p, 0);
2220 if (TREE_CODE (*expr_p) == CALL_EXPR)
2221 if (expand_call_inline (bb, stmt, expr_p, id))
2227 /* Expand calls to inline functions in the body of FN. */
2230 optimize_inline_calls (tree fn)
2235 /* There is no point in performing inlining if errors have already
2236 occurred -- and we might crash if we try to inline invalid
2238 if (errorcount || sorrycount)
2242 memset (&id, 0, sizeof (id));
2244 id.src_node = id.dst_node = cgraph_node (fn);
2246 /* Or any functions that aren't finished yet. */
2247 prev_fn = NULL_TREE;
2248 if (current_function_decl)
2250 id.dst_fn = current_function_decl;
2251 prev_fn = current_function_decl;
2254 id.copy_decl = copy_decl_maybe_to_var;
2255 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2256 id.transform_new_cfg = false;
2257 id.transform_return_to_modify = true;
2258 id.transform_lang_insert_block = false;
2260 push_gimplify_context ();
2262 /* Reach the trees by walking over the CFG, and note the
2263 enclosing basic-blocks in the call edges. */
2264 /* We walk the blocks going forward, because inlined function bodies
2265 will split id->current_basic_block, and the new blocks will
2266 follow it; we'll trudge through them, processing their CALL_EXPRs
2269 gimple_expand_calls_inline (bb, &id);
2271 pop_gimplify_context (NULL);
2272 /* Renumber the (code) basic_blocks consecutively. */
2274 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2277 #ifdef ENABLE_CHECKING
2279 struct cgraph_edge *e;
2281 verify_cgraph_node (id.dst_node);
2283 /* Double check that we inlined everything we are supposed to inline. */
2284 for (e = id.dst_node->callees; e; e = e->next_callee)
2285 gcc_assert (e->inline_failed);
2288 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2289 as inlining loops might increase the maximum. */
2290 if (ENTRY_BLOCK_PTR->count)
2292 fold_cond_expr_cond ();
2295 /* FN is a function that has a complete body, and CLONE is a function whose
2296 body is to be set to a copy of FN, mapping argument declarations according
2297 to the ARG_MAP splay_tree. */
2300 clone_body (tree clone, tree fn, void *arg_map)
2304 /* Clone the body, as if we were making an inline call. But, remap the
2305 parameters in the callee to the parameters of caller. */
2306 memset (&id, 0, sizeof (id));
2309 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
2310 id.decl_map = (splay_tree)arg_map;
2312 id.copy_decl = copy_decl_no_change;
2313 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2314 id.transform_new_cfg = true;
2315 id.transform_return_to_modify = false;
2316 id.transform_lang_insert_block = true;
2318 /* We're not inside any EH region. */
2321 /* Actually copy the body. */
2322 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2325 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2328 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2330 enum tree_code code = TREE_CODE (*tp);
2331 enum tree_code_class cl = TREE_CODE_CLASS (code);
2333 /* We make copies of most nodes. */
2334 if (IS_EXPR_CODE_CLASS (cl)
2335 || IS_GIMPLE_STMT_CODE_CLASS (cl)
2336 || code == TREE_LIST
2338 || code == TYPE_DECL
2339 || code == OMP_CLAUSE)
2341 /* Because the chain gets clobbered when we make a copy, we save it
2343 tree chain = NULL_TREE, new;
2345 if (!GIMPLE_TUPLE_P (*tp))
2346 chain = TREE_CHAIN (*tp);
2348 /* Copy the node. */
2349 new = copy_node (*tp);
2351 /* Propagate mudflap marked-ness. */
2352 if (flag_mudflap && mf_marked_p (*tp))
2357 /* Now, restore the chain, if appropriate. That will cause
2358 walk_tree to walk into the chain as well. */
2359 if (code == PARM_DECL
2360 || code == TREE_LIST
2361 || code == OMP_CLAUSE)
2362 TREE_CHAIN (*tp) = chain;
2364 /* For now, we don't update BLOCKs when we make copies. So, we
2365 have to nullify all BIND_EXPRs. */
2366 if (TREE_CODE (*tp) == BIND_EXPR)
2367 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2369 else if (code == CONSTRUCTOR)
2371 /* CONSTRUCTOR nodes need special handling because
2372 we need to duplicate the vector of elements. */
2375 new = copy_node (*tp);
2377 /* Propagate mudflap marked-ness. */
2378 if (flag_mudflap && mf_marked_p (*tp))
2381 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2382 CONSTRUCTOR_ELTS (*tp));
2385 else if (TREE_CODE_CLASS (code) == tcc_type)
2387 else if (TREE_CODE_CLASS (code) == tcc_declaration)
2389 else if (TREE_CODE_CLASS (code) == tcc_constant)
2392 gcc_assert (code != STATEMENT_LIST);
2396 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2397 information indicating to what new SAVE_EXPR this one should be mapped,
2398 use that one. Otherwise, create a new node and enter it in ST. FN is
2399 the function into which the copy will be placed. */
2402 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2404 splay_tree st = (splay_tree) st_;
2408 /* See if we already encountered this SAVE_EXPR. */
2409 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2411 /* If we didn't already remap this SAVE_EXPR, do so now. */
2414 t = copy_node (*tp);
2416 /* Remember this SAVE_EXPR. */
2417 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2418 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2419 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2423 /* We've already walked into this SAVE_EXPR; don't do it again. */
2425 t = (tree) n->value;
2428 /* Replace this SAVE_EXPR with the copy. */
2432 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2433 copies the declaration and enters it in the splay_tree in DATA (which is
2434 really an `copy_body_data *'). */
2437 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2440 copy_body_data *id = (copy_body_data *) data;
2442 /* Don't walk into types. */
2446 else if (TREE_CODE (*tp) == LABEL_EXPR)
2448 tree decl = TREE_OPERAND (*tp, 0);
2450 /* Copy the decl and remember the copy. */
2451 insert_decl_map (id, decl, id->copy_decl (decl, id));
2457 /* Perform any modifications to EXPR required when it is unsaved. Does
2458 not recurse into EXPR's subtrees. */
2461 unsave_expr_1 (tree expr)
2463 switch (TREE_CODE (expr))
2466 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2467 It's OK for this to happen if it was part of a subtree that
2468 isn't immediately expanded, such as operand 2 of another
2470 if (TREE_OPERAND (expr, 1))
2473 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2474 TREE_OPERAND (expr, 3) = NULL_TREE;
2482 /* Called via walk_tree when an expression is unsaved. Using the
2483 splay_tree pointed to by ST (which is really a `splay_tree'),
2484 remaps all local declarations to appropriate replacements. */
2487 unsave_r (tree *tp, int *walk_subtrees, void *data)
2489 copy_body_data *id = (copy_body_data *) data;
2490 splay_tree st = id->decl_map;
2493 /* Only a local declaration (variable or label). */
2494 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2495 || TREE_CODE (*tp) == LABEL_DECL)
2497 /* Lookup the declaration. */
2498 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2500 /* If it's there, remap it. */
2502 *tp = (tree) n->value;
2505 else if (TREE_CODE (*tp) == STATEMENT_LIST)
2506 copy_statement_list (tp);
2507 else if (TREE_CODE (*tp) == BIND_EXPR)
2508 copy_bind_expr (tp, walk_subtrees, id);
2509 else if (TREE_CODE (*tp) == SAVE_EXPR)
2510 remap_save_expr (tp, st, walk_subtrees);
2513 copy_tree_r (tp, walk_subtrees, NULL);
2515 /* Do whatever unsaving is required. */
2516 unsave_expr_1 (*tp);
2519 /* Keep iterating. */
2523 /* Copies everything in EXPR and replaces variables, labels
2524 and SAVE_EXPRs local to EXPR. */
2527 unsave_expr_now (tree expr)
2531 /* There's nothing to do for NULL_TREE. */
2536 memset (&id, 0, sizeof (id));
2537 id.src_fn = current_function_decl;
2538 id.dst_fn = current_function_decl;
2539 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2541 id.copy_decl = copy_decl_no_change;
2542 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2543 id.transform_new_cfg = false;
2544 id.transform_return_to_modify = false;
2545 id.transform_lang_insert_block = false;
2547 /* Walk the tree once to find local labels. */
2548 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2550 /* Walk the tree again, copying, remapping, and unsaving. */
2551 walk_tree (&expr, unsave_r, &id, NULL);
2554 splay_tree_delete (id.decl_map);
2559 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
2562 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2571 debug_find_tree (tree top, tree search)
2573 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2577 /* Declare the variables created by the inliner. Add all the variables in
2578 VARS to BIND_EXPR. */
2581 declare_inline_vars (tree block, tree vars)
2584 for (t = vars; t; t = TREE_CHAIN (t))
2586 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2587 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
2588 cfun->unexpanded_var_list =
2589 tree_cons (NULL_TREE, t,
2590 cfun->unexpanded_var_list);
2594 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2598 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
2599 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
2600 VAR_DECL translation. */
2603 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
2605 /* Don't generate debug information for the copy if we wouldn't have
2606 generated it for the copy either. */
2607 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2608 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2610 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
2611 declaration inspired this copy. */
2612 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2614 /* The new variable/label has no RTL, yet. */
2615 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
2616 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
2617 SET_DECL_RTL (copy, NULL_RTX);
2619 /* These args would always appear unused, if not for this. */
2620 TREE_USED (copy) = 1;
2622 /* Set the context for the new declaration. */
2623 if (!DECL_CONTEXT (decl))
2624 /* Globals stay global. */
2626 else if (DECL_CONTEXT (decl) != id->src_fn)
2627 /* Things that weren't in the scope of the function we're inlining
2628 from aren't in the scope we're inlining to, either. */
2630 else if (TREE_STATIC (decl))
2631 /* Function-scoped static variables should stay in the original
2635 /* Ordinary automatic local variables are now in the scope of the
2637 DECL_CONTEXT (copy) = id->dst_fn;
2643 copy_decl_to_var (tree decl, copy_body_data *id)
2647 gcc_assert (TREE_CODE (decl) == PARM_DECL
2648 || TREE_CODE (decl) == RESULT_DECL);
2650 type = TREE_TYPE (decl);
2652 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
2653 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2654 TREE_READONLY (copy) = TREE_READONLY (decl);
2655 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2656 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
2658 return copy_decl_for_dup_finish (id, decl, copy);
2661 /* Like copy_decl_to_var, but create a return slot object instead of a
2662 pointer variable for return by invisible reference. */
2665 copy_result_decl_to_var (tree decl, copy_body_data *id)
2669 gcc_assert (TREE_CODE (decl) == PARM_DECL
2670 || TREE_CODE (decl) == RESULT_DECL);
2672 type = TREE_TYPE (decl);
2673 if (DECL_BY_REFERENCE (decl))
2674 type = TREE_TYPE (type);
2676 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
2677 TREE_READONLY (copy) = TREE_READONLY (decl);
2678 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2679 if (!DECL_BY_REFERENCE (decl))
2681 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2682 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
2685 return copy_decl_for_dup_finish (id, decl, copy);
2690 copy_decl_no_change (tree decl, copy_body_data *id)
2694 copy = copy_node (decl);
2696 /* The COPY is not abstract; it will be generated in DST_FN. */
2697 DECL_ABSTRACT (copy) = 0;
2698 lang_hooks.dup_lang_specific_decl (copy);
2700 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
2701 been taken; it's for internal bookkeeping in expand_goto_internal. */
2702 if (TREE_CODE (copy) == LABEL_DECL)
2704 TREE_ADDRESSABLE (copy) = 0;
2705 LABEL_DECL_UID (copy) = -1;
2708 return copy_decl_for_dup_finish (id, decl, copy);
2712 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
2714 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
2715 return copy_decl_to_var (decl, id);
2717 return copy_decl_no_change (decl, id);
2720 /* Return a copy of the function's argument tree. */
2722 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
2724 tree *arg_copy, *parg;
2726 arg_copy = &orig_parm;
2727 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2729 tree new = remap_decl (*parg, id);
2730 lang_hooks.dup_lang_specific_decl (new);
2731 TREE_CHAIN (new) = TREE_CHAIN (*parg);
2737 /* Return a copy of the function's static chain. */
2739 copy_static_chain (tree static_chain, copy_body_data * id)
2741 tree *chain_copy, *pvar;
2743 chain_copy = &static_chain;
2744 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
2746 tree new = remap_decl (*pvar, id);
2747 lang_hooks.dup_lang_specific_decl (new);
2748 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
2751 return static_chain;
2754 /* Return true if the function is allowed to be versioned.
2755 This is a guard for the versioning functionality. */
2757 tree_versionable_function_p (tree fndecl)
2759 if (fndecl == NULL_TREE)
2761 /* ??? There are cases where a function is
2762 uninlinable but can be versioned. */
2763 if (!tree_inlinable_function_p (fndecl))
2769 /* Create a copy of a function's tree.
2770 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
2771 of the original function and the new copied function
2772 respectively. In case we want to replace a DECL
2773 tree with another tree while duplicating the function's
2774 body, TREE_MAP represents the mapping between these
2775 trees. If UPDATE_CLONES is set, the call_stmt fields
2776 of edges of clones of the function will be updated. */
2778 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
2781 struct cgraph_node *old_version_node;
2782 struct cgraph_node *new_version_node;
2786 struct ipa_replace_map *replace_info;
2787 basic_block old_entry_block;
2790 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
2791 && TREE_CODE (new_decl) == FUNCTION_DECL);
2792 DECL_POSSIBLY_INLINED (old_decl) = 1;
2794 old_version_node = cgraph_node (old_decl);
2795 new_version_node = cgraph_node (new_decl);
2797 allocate_struct_function (new_decl);
2798 /* Cfun points to the new allocated function struct at this point. */
2799 cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
2801 DECL_ARTIFICIAL (new_decl) = 1;
2802 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
2804 /* Generate a new name for the new version. */
2807 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
2808 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2809 SET_DECL_RTL (new_decl, NULL_RTX);
2812 /* Prepare the data structures for the tree copy. */
2813 memset (&id, 0, sizeof (id));
2815 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2816 id.src_fn = old_decl;
2817 id.dst_fn = new_decl;
2818 id.src_node = old_version_node;
2819 id.dst_node = new_version_node;
2820 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
2822 id.copy_decl = copy_decl_no_change;
2823 id.transform_call_graph_edges
2824 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
2825 id.transform_new_cfg = true;
2826 id.transform_return_to_modify = false;
2827 id.transform_lang_insert_block = false;
2829 current_function_decl = new_decl;
2831 /* Copy the function's static chain. */
2832 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
2834 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
2835 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
2837 /* Copy the function's arguments. */
2838 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
2839 DECL_ARGUMENTS (new_decl) =
2840 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
2842 /* If there's a tree_map, prepare for substitution. */
2844 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
2846 replace_info = VARRAY_GENERIC_PTR (tree_map, i);
2847 if (replace_info->replace_p)
2848 insert_decl_map (&id, replace_info->old_tree,
2849 replace_info->new_tree);
2852 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
2854 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2855 number_blocks (id.dst_fn);
2857 if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
2858 /* Add local vars. */
2859 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
2860 t_step; t_step = TREE_CHAIN (t_step))
2862 tree var = TREE_VALUE (t_step);
2863 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2864 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2865 cfun->unexpanded_var_list);
2867 cfun->unexpanded_var_list =
2868 tree_cons (NULL_TREE, remap_decl (var, &id),
2869 cfun->unexpanded_var_list);
2872 /* Copy the Function's body. */
2873 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
2874 (DECL_STRUCT_FUNCTION (old_decl));
2875 new_fndecl = copy_body (&id,
2876 old_entry_block->count,
2877 old_entry_block->frequency, NULL, NULL);
2879 DECL_SAVED_TREE (new_decl) = DECL_SAVED_TREE (new_fndecl);
2881 DECL_STRUCT_FUNCTION (new_decl)->cfg =
2882 DECL_STRUCT_FUNCTION (new_fndecl)->cfg;
2883 DECL_STRUCT_FUNCTION (new_decl)->eh = DECL_STRUCT_FUNCTION (new_fndecl)->eh;
2884 DECL_STRUCT_FUNCTION (new_decl)->ib_boundaries_block =
2885 DECL_STRUCT_FUNCTION (new_fndecl)->ib_boundaries_block;
2886 DECL_STRUCT_FUNCTION (new_decl)->last_label_uid =
2887 DECL_STRUCT_FUNCTION (new_fndecl)->last_label_uid;
2889 if (DECL_RESULT (old_decl) != NULL_TREE)
2891 tree *res_decl = &DECL_RESULT (old_decl);
2892 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
2893 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
2896 current_function_decl = NULL;
2897 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2898 number_blocks (new_decl);
2901 splay_tree_delete (id.decl_map);
2902 fold_cond_expr_cond ();
2906 /* Duplicate a type, fields and all. */
2909 build_duplicate_type (tree type)
2911 struct copy_body_data id;
2913 memset (&id, 0, sizeof (id));
2914 id.src_fn = current_function_decl;
2915 id.dst_fn = current_function_decl;
2917 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2919 type = remap_type_1 (type, &id);
2921 splay_tree_delete (id.decl_map);