2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Alexandre Oliva <aoliva@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "tree-inline.h"
34 #include "insn-config.h"
36 #include "langhooks.h"
37 #include "basic-block.h"
38 #include "tree-iterator.h"
41 #include "tree-mudflap.h"
42 #include "tree-flow.h"
45 #include "tree-flow.h"
46 #include "diagnostic.h"
49 #include "pointer-set.h"
51 #include "value-prof.h"
52 #include "tree-pass.h"
54 #include "integrate.h"
56 /* I'm not real happy about this, but we need to handle gimple and
60 /* Inlining, Cloning, Versioning, Parallelization
62 Inlining: a function body is duplicated, but the PARM_DECLs are
63 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
64 MODIFY_EXPRs that store to a dedicated returned-value variable.
65 The duplicated eh_region info of the copy will later be appended
66 to the info for the caller; the eh_region info in copied throwing
67 statements and RESX_EXPRs is adjusted accordingly.
69 Cloning: (only in C++) We have one body for a con/de/structor, and
70 multiple function decls, each with a unique parameter list.
71 Duplicate the body, using the given splay tree; some parameters
72 will become constants (like 0 or 1).
74 Versioning: a function body is duplicated and the result is a new
75 function rather than into blocks of an existing function as with
76 inlining. Some parameters will become constants.
78 Parallelization: a region of a function is duplicated resulting in
79 a new function. Variables may be replaced with complex expressions
80 to enable shared variable semantics.
82 All of these will simultaneously lookup any callgraph edges. If
83 we're going to inline the duplicated function body, and the given
84 function has some cloned callgraph nodes (one for each place this
85 function will be inlined) those callgraph edges will be duplicated.
86 If we're cloning the body, those callgraph edges will be
87 updated to point into the new body. (Note that the original
88 callgraph node and edge list will not be altered.)
90 See the CALL_EXPR handling case in copy_tree_body_r (). */
94 o In order to make inlining-on-trees work, we pessimized
95 function-local static constants. In particular, they are now
96 always output, even when not addressed. Fix this by treating
97 function-local static constants just like global static
98 constants; the back-end already knows not to output them if they
101 o Provide heuristics to clamp inlining of recursive template
105 /* Weights that estimate_num_insns uses for heuristics in inlining. */
107 eni_weights eni_inlining_weights;
109 /* Weights that estimate_num_insns uses to estimate the size of the
112 eni_weights eni_size_weights;
114 /* Weights that estimate_num_insns uses to estimate the time necessary
115 to execute the produced code. */
117 eni_weights eni_time_weights;
121 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
122 static void remap_block (tree *, copy_body_data *);
123 static void copy_bind_expr (tree *, int *, copy_body_data *);
124 static tree mark_local_for_remap_r (tree *, int *, void *);
125 static void unsave_expr_1 (tree);
126 static tree unsave_r (tree *, int *, void *);
127 static void declare_inline_vars (tree, tree);
128 static void remap_save_expr (tree *, void *, int *);
129 static void prepend_lexical_block (tree current_block, tree new_block);
130 static tree copy_decl_to_var (tree, copy_body_data *);
131 static tree copy_result_decl_to_var (tree, copy_body_data *);
132 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
133 static gimple remap_gimple_stmt (gimple, copy_body_data *);
134 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
136 /* Insert a tree->tree mapping for ID. Despite the name suggests
137 that the trees should be variables, it is used for more than that. */
140 insert_decl_map (copy_body_data *id, tree key, tree value)
142 *pointer_map_insert (id->decl_map, key) = value;
144 /* Always insert an identity map as well. If we see this same new
145 node again, we won't want to duplicate it a second time. */
147 *pointer_map_insert (id->decl_map, value) = value;
150 /* Construct new SSA name for old NAME. ID is the inline context. */
153 remap_ssa_name (tree name, copy_body_data *id)
158 gcc_assert (TREE_CODE (name) == SSA_NAME);
160 n = (tree *) pointer_map_contains (id->decl_map, name);
162 return unshare_expr (*n);
164 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
166 new_tree = remap_decl (SSA_NAME_VAR (name), id);
168 /* We might've substituted constant or another SSA_NAME for
171 Replace the SSA name representing RESULT_DECL by variable during
172 inlining: this saves us from need to introduce PHI node in a case
173 return value is just partly initialized. */
174 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
175 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
176 || !id->transform_return_to_modify))
178 new_tree = make_ssa_name (new_tree, NULL);
179 insert_decl_map (id, name, new_tree);
180 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
181 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
182 TREE_TYPE (new_tree) = TREE_TYPE (SSA_NAME_VAR (new_tree));
183 if (gimple_nop_p (SSA_NAME_DEF_STMT (name)))
185 /* By inlining function having uninitialized variable, we might
186 extend the lifetime (variable might get reused). This cause
187 ICE in the case we end up extending lifetime of SSA name across
188 abnormal edge, but also increase register pressure.
190 We simply initialize all uninitialized vars by 0 except
191 for case we are inlining to very first BB. We can avoid
192 this for all BBs that are not inside strongly connected
193 regions of the CFG, but this is expensive to test. */
195 && is_gimple_reg (SSA_NAME_VAR (name))
196 && TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL
197 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
198 || EDGE_COUNT (id->entry_bb->preds) != 1))
200 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
203 init_stmt = gimple_build_assign (new_tree,
204 fold_convert (TREE_TYPE (new_tree),
206 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
207 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
211 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
212 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name))
214 set_default_def (SSA_NAME_VAR (new_tree), new_tree);
219 insert_decl_map (id, name, new_tree);
223 /* Remap DECL during the copying of the BLOCK tree for the function. */
226 remap_decl (tree decl, copy_body_data *id)
231 /* We only remap local variables in the current function. */
234 /* See if we have remapped this declaration. */
236 n = (tree *) pointer_map_contains (id->decl_map, decl);
238 /* If we didn't already have an equivalent for this declaration,
242 /* Make a copy of the variable or label. */
243 tree t = id->copy_decl (decl, id);
245 /* Remember it, so that if we encounter this local entity again
246 we can reuse this copy. Do this early because remap_type may
247 need this decl for TYPE_STUB_DECL. */
248 insert_decl_map (id, decl, t);
253 /* Remap types, if necessary. */
254 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
255 if (TREE_CODE (t) == TYPE_DECL)
256 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
258 /* Remap sizes as necessary. */
259 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
260 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
262 /* If fields, do likewise for offset and qualifier. */
263 if (TREE_CODE (t) == FIELD_DECL)
265 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
266 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
267 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
270 if (cfun && gimple_in_ssa_p (cfun)
271 && (TREE_CODE (t) == VAR_DECL
272 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
274 tree def = gimple_default_def (id->src_cfun, decl);
276 if (TREE_CODE (decl) != PARM_DECL && def)
278 tree map = remap_ssa_name (def, id);
279 /* Watch out RESULT_DECLs whose SSA names map directly
281 if (TREE_CODE (map) == SSA_NAME
282 && gimple_nop_p (SSA_NAME_DEF_STMT (map)))
283 set_default_def (t, map);
285 add_referenced_var (t);
290 if (id->do_not_unshare)
293 return unshare_expr (*n);
297 remap_type_1 (tree type, copy_body_data *id)
301 /* We do need a copy. build and register it now. If this is a pointer or
302 reference type, remap the designated type and make a new pointer or
304 if (TREE_CODE (type) == POINTER_TYPE)
306 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
308 TYPE_REF_CAN_ALIAS_ALL (type));
309 insert_decl_map (id, type, new_tree);
312 else if (TREE_CODE (type) == REFERENCE_TYPE)
314 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
316 TYPE_REF_CAN_ALIAS_ALL (type));
317 insert_decl_map (id, type, new_tree);
321 new_tree = copy_node (type);
323 insert_decl_map (id, type, new_tree);
325 /* This is a new type, not a copy of an old type. Need to reassociate
326 variants. We can handle everything except the main variant lazily. */
327 t = TYPE_MAIN_VARIANT (type);
330 t = remap_type (t, id);
331 TYPE_MAIN_VARIANT (new_tree) = t;
332 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
333 TYPE_NEXT_VARIANT (t) = new_tree;
337 TYPE_MAIN_VARIANT (new_tree) = new_tree;
338 TYPE_NEXT_VARIANT (new_tree) = NULL;
341 if (TYPE_STUB_DECL (type))
342 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
344 /* Lazily create pointer and reference types. */
345 TYPE_POINTER_TO (new_tree) = NULL;
346 TYPE_REFERENCE_TO (new_tree) = NULL;
348 switch (TREE_CODE (new_tree))
352 case FIXED_POINT_TYPE:
355 t = TYPE_MIN_VALUE (new_tree);
356 if (t && TREE_CODE (t) != INTEGER_CST)
357 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
359 t = TYPE_MAX_VALUE (new_tree);
360 if (t && TREE_CODE (t) != INTEGER_CST)
361 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
365 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
366 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
370 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
371 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
376 case QUAL_UNION_TYPE:
380 for (f = TYPE_FIELDS (new_tree); f ; f = TREE_CHAIN (f))
382 t = remap_decl (f, id);
383 DECL_CONTEXT (t) = new_tree;
387 TYPE_FIELDS (new_tree) = nreverse (nf);
393 /* Shouldn't have been thought variable sized. */
397 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
398 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
404 remap_type (tree type, copy_body_data *id)
412 /* See if we have remapped this type. */
413 node = (tree *) pointer_map_contains (id->decl_map, type);
417 /* The type only needs remapping if it's variably modified. */
418 if (! variably_modified_type_p (type, id->src_fn))
420 insert_decl_map (id, type, type);
424 id->remapping_type_depth++;
425 tmp = remap_type_1 (type, id);
426 id->remapping_type_depth--;
431 /* Return previously remapped type of TYPE in ID. Return NULL if TYPE
432 is NULL or TYPE has not been remapped before. */
435 remapped_type (tree type, copy_body_data *id)
442 /* See if we have remapped this type. */
443 node = (tree *) pointer_map_contains (id->decl_map, type);
450 /* The type only needs remapping if it's variably modified. */
451 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
454 can_be_nonlocal (tree decl, copy_body_data *id)
456 /* We can not duplicate function decls. */
457 if (TREE_CODE (decl) == FUNCTION_DECL)
460 /* Local static vars must be non-local or we get multiple declaration
462 if (TREE_CODE (decl) == VAR_DECL
463 && !auto_var_in_fn_p (decl, id->src_fn))
466 /* At the moment dwarf2out can handle only these types of nodes. We
467 can support more later. */
468 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
471 /* We must use global type. We call remapped_type instead of
472 remap_type since we don't want to remap this type here if it
473 hasn't been remapped before. */
474 if (TREE_TYPE (decl) != remapped_type (TREE_TYPE (decl), id))
477 /* Wihtout SSA we can't tell if variable is used. */
478 if (!gimple_in_ssa_p (cfun))
481 /* Live variables must be copied so we can attach DECL_RTL. */
489 remap_decls (tree decls, VEC(tree,gc) **nonlocalized_list, copy_body_data *id)
492 tree new_decls = NULL_TREE;
494 /* Remap its variables. */
495 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
498 tree origin_var = DECL_ORIGIN (old_var);
500 if (can_be_nonlocal (old_var, id))
502 if (TREE_CODE (old_var) == VAR_DECL
503 && (var_ann (old_var) || !gimple_in_ssa_p (cfun)))
504 cfun->local_decls = tree_cons (NULL_TREE, old_var,
506 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
507 && !DECL_IGNORED_P (old_var)
508 && nonlocalized_list)
509 VEC_safe_push (tree, gc, *nonlocalized_list, origin_var);
513 /* Remap the variable. */
514 new_var = remap_decl (old_var, id);
516 /* If we didn't remap this variable, we can't mess with its
517 TREE_CHAIN. If we remapped this variable to the return slot, it's
518 already declared somewhere else, so don't declare it here. */
520 if (new_var == id->retvar)
524 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
525 && !DECL_IGNORED_P (old_var)
526 && nonlocalized_list)
527 VEC_safe_push (tree, gc, *nonlocalized_list, origin_var);
531 gcc_assert (DECL_P (new_var));
532 TREE_CHAIN (new_var) = new_decls;
537 return nreverse (new_decls);
540 /* Copy the BLOCK to contain remapped versions of the variables
541 therein. And hook the new block into the block-tree. */
544 remap_block (tree *block, copy_body_data *id)
550 /* Make the new block. */
552 new_block = make_node (BLOCK);
553 TREE_USED (new_block) = TREE_USED (old_block);
554 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
555 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
556 BLOCK_NONLOCALIZED_VARS (new_block)
557 = VEC_copy (tree, gc, BLOCK_NONLOCALIZED_VARS (old_block));
560 /* Remap its variables. */
561 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
562 &BLOCK_NONLOCALIZED_VARS (new_block),
567 if (id->transform_lang_insert_block)
568 id->transform_lang_insert_block (new_block);
570 /* Remember the remapped block. */
571 insert_decl_map (id, old_block, new_block);
574 /* Copy the whole block tree and root it in id->block. */
576 remap_blocks (tree block, copy_body_data *id)
579 tree new_tree = block;
584 remap_block (&new_tree, id);
585 gcc_assert (new_tree != block);
586 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
587 prepend_lexical_block (new_tree, remap_blocks (t, id));
588 /* Blocks are in arbitrary order, but make things slightly prettier and do
589 not swap order when producing a copy. */
590 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
595 copy_statement_list (tree *tp)
597 tree_stmt_iterator oi, ni;
600 new_tree = alloc_stmt_list ();
601 ni = tsi_start (new_tree);
602 oi = tsi_start (*tp);
605 for (; !tsi_end_p (oi); tsi_next (&oi))
606 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
610 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
612 tree block = BIND_EXPR_BLOCK (*tp);
613 /* Copy (and replace) the statement. */
614 copy_tree_r (tp, walk_subtrees, NULL);
617 remap_block (&block, id);
618 BIND_EXPR_BLOCK (*tp) = block;
621 if (BIND_EXPR_VARS (*tp))
622 /* This will remap a lot of the same decls again, but this should be
624 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
628 /* Create a new gimple_seq by remapping all the statements in BODY
629 using the inlining information in ID. */
632 remap_gimple_seq (gimple_seq body, copy_body_data *id)
634 gimple_stmt_iterator si;
635 gimple_seq new_body = NULL;
637 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
639 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
640 gimple_seq_add_stmt (&new_body, new_stmt);
647 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
648 block using the mapping information in ID. */
651 copy_gimple_bind (gimple stmt, copy_body_data *id)
654 tree new_block, new_vars;
655 gimple_seq body, new_body;
657 /* Copy the statement. Note that we purposely don't use copy_stmt
658 here because we need to remap statements as we copy. */
659 body = gimple_bind_body (stmt);
660 new_body = remap_gimple_seq (body, id);
662 new_block = gimple_bind_block (stmt);
664 remap_block (&new_block, id);
666 /* This will remap a lot of the same decls again, but this should be
668 new_vars = gimple_bind_vars (stmt);
670 new_vars = remap_decls (new_vars, NULL, id);
672 new_bind = gimple_build_bind (new_vars, new_body, new_block);
678 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
679 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
680 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
681 recursing into the children nodes of *TP. */
684 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
686 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
687 copy_body_data *id = (copy_body_data *) wi_p->info;
688 tree fn = id->src_fn;
690 if (TREE_CODE (*tp) == SSA_NAME)
692 *tp = remap_ssa_name (*tp, id);
696 else if (auto_var_in_fn_p (*tp, fn))
698 /* Local variables and labels need to be replaced by equivalent
699 variables. We don't want to copy static variables; there's
700 only one of those, no matter how many times we inline the
701 containing function. Similarly for globals from an outer
705 /* Remap the declaration. */
706 new_decl = remap_decl (*tp, id);
707 gcc_assert (new_decl);
708 /* Replace this variable with the copy. */
709 STRIP_TYPE_NOPS (new_decl);
710 /* ??? The C++ frontend uses void * pointer zero to initialize
711 any other type. This confuses the middle-end type verification.
712 As cloned bodies do not go through gimplification again the fixup
713 there doesn't trigger. */
714 if (TREE_CODE (new_decl) == INTEGER_CST
715 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
716 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
720 else if (TREE_CODE (*tp) == STATEMENT_LIST)
722 else if (TREE_CODE (*tp) == SAVE_EXPR)
724 else if (TREE_CODE (*tp) == LABEL_DECL
725 && (!DECL_CONTEXT (*tp)
726 || decl_function_context (*tp) == id->src_fn))
727 /* These may need to be remapped for EH handling. */
728 *tp = remap_decl (*tp, id);
729 else if (TYPE_P (*tp))
730 /* Types may need remapping as well. */
731 *tp = remap_type (*tp, id);
732 else if (CONSTANT_CLASS_P (*tp))
734 /* If this is a constant, we have to copy the node iff the type
735 will be remapped. copy_tree_r will not copy a constant. */
736 tree new_type = remap_type (TREE_TYPE (*tp), id);
738 if (new_type == TREE_TYPE (*tp))
741 else if (TREE_CODE (*tp) == INTEGER_CST)
742 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
743 TREE_INT_CST_HIGH (*tp));
746 *tp = copy_node (*tp);
747 TREE_TYPE (*tp) = new_type;
752 /* Otherwise, just copy the node. Note that copy_tree_r already
753 knows not to copy VAR_DECLs, etc., so this is safe. */
754 if (TREE_CODE (*tp) == INDIRECT_REF)
756 /* Get rid of *& from inline substitutions that can happen when a
757 pointer argument is an ADDR_EXPR. */
758 tree decl = TREE_OPERAND (*tp, 0);
761 n = (tree *) pointer_map_contains (id->decl_map, decl);
764 tree type, new_tree, old;
766 /* If we happen to get an ADDR_EXPR in n->value, strip
767 it manually here as we'll eventually get ADDR_EXPRs
768 which lie about their types pointed to. In this case
769 build_fold_indirect_ref wouldn't strip the
770 INDIRECT_REF, but we absolutely rely on that. As
771 fold_indirect_ref does other useful transformations,
772 try that first, though. */
773 type = TREE_TYPE (TREE_TYPE (*n));
774 new_tree = unshare_expr (*n);
776 *tp = gimple_fold_indirect_ref (new_tree);
779 if (TREE_CODE (new_tree) == ADDR_EXPR)
781 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
783 /* ??? We should either assert here or build
784 a VIEW_CONVERT_EXPR instead of blindly leaking
785 incompatible types to our IL. */
787 *tp = TREE_OPERAND (new_tree, 0);
791 *tp = build1 (INDIRECT_REF, type, new_tree);
792 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
793 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
801 /* Here is the "usual case". Copy this tree node, and then
802 tweak some special cases. */
803 copy_tree_r (tp, walk_subtrees, NULL);
805 /* Global variables we haven't seen yet need to go into referenced
806 vars. If not referenced from types only. */
807 if (gimple_in_ssa_p (cfun)
808 && TREE_CODE (*tp) == VAR_DECL
809 && id->remapping_type_depth == 0)
810 add_referenced_var (*tp);
812 /* We should never have TREE_BLOCK set on non-statements. */
814 gcc_assert (!TREE_BLOCK (*tp));
816 if (TREE_CODE (*tp) != OMP_CLAUSE)
817 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
819 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
821 /* The copied TARGET_EXPR has never been expanded, even if the
822 original node was expanded already. */
823 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
824 TREE_OPERAND (*tp, 3) = NULL_TREE;
826 else if (TREE_CODE (*tp) == ADDR_EXPR)
828 /* Variable substitution need not be simple. In particular,
829 the INDIRECT_REF substitution above. Make sure that
830 TREE_CONSTANT and friends are up-to-date. But make sure
831 to not improperly set TREE_BLOCK on some sub-expressions. */
832 int invariant = is_gimple_min_invariant (*tp);
833 tree block = id->block;
834 id->block = NULL_TREE;
835 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
838 /* Handle the case where we substituted an INDIRECT_REF
839 into the operand of the ADDR_EXPR. */
840 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
841 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
843 recompute_tree_invariant_for_addr_expr (*tp);
845 /* If this used to be invariant, but is not any longer,
846 then regimplification is probably needed. */
847 if (invariant && !is_gimple_min_invariant (*tp))
848 id->regimplify = true;
854 /* Keep iterating. */
859 /* Called from copy_body_id via walk_tree. DATA is really a
860 `copy_body_data *'. */
863 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
865 copy_body_data *id = (copy_body_data *) data;
866 tree fn = id->src_fn;
869 /* Begin by recognizing trees that we'll completely rewrite for the
870 inlining context. Our output for these trees is completely
871 different from out input (e.g. RETURN_EXPR is deleted, and morphs
872 into an edge). Further down, we'll handle trees that get
873 duplicated and/or tweaked. */
875 /* When requested, RETURN_EXPRs should be transformed to just the
876 contained MODIFY_EXPR. The branch semantics of the return will
877 be handled elsewhere by manipulating the CFG rather than a statement. */
878 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
880 tree assignment = TREE_OPERAND (*tp, 0);
882 /* If we're returning something, just turn that into an
883 assignment into the equivalent of the original RESULT_DECL.
884 If the "assignment" is just the result decl, the result
885 decl has already been set (e.g. a recent "foo (&result_decl,
886 ...)"); just toss the entire RETURN_EXPR. */
887 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
889 /* Replace the RETURN_EXPR with (a copy of) the
890 MODIFY_EXPR hanging underneath. */
891 *tp = copy_node (assignment);
893 else /* Else the RETURN_EXPR returns no value. */
896 return (tree) (void *)1;
899 else if (TREE_CODE (*tp) == SSA_NAME)
901 *tp = remap_ssa_name (*tp, id);
906 /* Local variables and labels need to be replaced by equivalent
907 variables. We don't want to copy static variables; there's only
908 one of those, no matter how many times we inline the containing
909 function. Similarly for globals from an outer function. */
910 else if (auto_var_in_fn_p (*tp, fn))
914 /* Remap the declaration. */
915 new_decl = remap_decl (*tp, id);
916 gcc_assert (new_decl);
917 /* Replace this variable with the copy. */
918 STRIP_TYPE_NOPS (new_decl);
922 else if (TREE_CODE (*tp) == STATEMENT_LIST)
923 copy_statement_list (tp);
924 else if (TREE_CODE (*tp) == SAVE_EXPR)
925 remap_save_expr (tp, id->decl_map, walk_subtrees);
926 else if (TREE_CODE (*tp) == LABEL_DECL
927 && (! DECL_CONTEXT (*tp)
928 || decl_function_context (*tp) == id->src_fn))
929 /* These may need to be remapped for EH handling. */
930 *tp = remap_decl (*tp, id);
931 else if (TREE_CODE (*tp) == BIND_EXPR)
932 copy_bind_expr (tp, walk_subtrees, id);
933 /* Types may need remapping as well. */
934 else if (TYPE_P (*tp))
935 *tp = remap_type (*tp, id);
937 /* If this is a constant, we have to copy the node iff the type will be
938 remapped. copy_tree_r will not copy a constant. */
939 else if (CONSTANT_CLASS_P (*tp))
941 tree new_type = remap_type (TREE_TYPE (*tp), id);
943 if (new_type == TREE_TYPE (*tp))
946 else if (TREE_CODE (*tp) == INTEGER_CST)
947 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
948 TREE_INT_CST_HIGH (*tp));
951 *tp = copy_node (*tp);
952 TREE_TYPE (*tp) = new_type;
956 /* Otherwise, just copy the node. Note that copy_tree_r already
957 knows not to copy VAR_DECLs, etc., so this is safe. */
960 /* Here we handle trees that are not completely rewritten.
961 First we detect some inlining-induced bogosities for
963 if (TREE_CODE (*tp) == MODIFY_EXPR
964 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
965 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
967 /* Some assignments VAR = VAR; don't generate any rtl code
968 and thus don't count as variable modification. Avoid
969 keeping bogosities like 0 = 0. */
970 tree decl = TREE_OPERAND (*tp, 0), value;
973 n = (tree *) pointer_map_contains (id->decl_map, decl);
977 STRIP_TYPE_NOPS (value);
978 if (TREE_CONSTANT (value) || TREE_READONLY (value))
980 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
981 return copy_tree_body_r (tp, walk_subtrees, data);
985 else if (TREE_CODE (*tp) == INDIRECT_REF)
987 /* Get rid of *& from inline substitutions that can happen when a
988 pointer argument is an ADDR_EXPR. */
989 tree decl = TREE_OPERAND (*tp, 0);
992 n = (tree *) pointer_map_contains (id->decl_map, decl);
997 /* If we happen to get an ADDR_EXPR in n->value, strip
998 it manually here as we'll eventually get ADDR_EXPRs
999 which lie about their types pointed to. In this case
1000 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1001 but we absolutely rely on that. As fold_indirect_ref
1002 does other useful transformations, try that first, though. */
1003 tree type = TREE_TYPE (TREE_TYPE (*n));
1004 if (id->do_not_unshare)
1007 new_tree = unshare_expr (*n);
1009 *tp = gimple_fold_indirect_ref (new_tree);
1012 if (TREE_CODE (new_tree) == ADDR_EXPR)
1014 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
1016 /* ??? We should either assert here or build
1017 a VIEW_CONVERT_EXPR instead of blindly leaking
1018 incompatible types to our IL. */
1020 *tp = TREE_OPERAND (new_tree, 0);
1024 *tp = build1 (INDIRECT_REF, type, new_tree);
1025 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1026 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1034 /* Here is the "usual case". Copy this tree node, and then
1035 tweak some special cases. */
1036 copy_tree_r (tp, walk_subtrees, NULL);
1038 /* Global variables we haven't seen yet needs to go into referenced
1039 vars. If not referenced from types only. */
1040 if (gimple_in_ssa_p (cfun)
1041 && TREE_CODE (*tp) == VAR_DECL
1042 && id->remapping_type_depth == 0)
1043 add_referenced_var (*tp);
1045 /* If EXPR has block defined, map it to newly constructed block.
1046 When inlining we want EXPRs without block appear in the block
1047 of function call. */
1050 new_block = id->block;
1051 if (TREE_BLOCK (*tp))
1054 n = (tree *) pointer_map_contains (id->decl_map,
1059 TREE_BLOCK (*tp) = new_block;
1062 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
1063 TREE_OPERAND (*tp, 0) =
1064 build_int_cst (NULL_TREE,
1065 id->eh_region_offset
1066 + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
1068 if (TREE_CODE (*tp) != OMP_CLAUSE)
1069 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1071 /* The copied TARGET_EXPR has never been expanded, even if the
1072 original node was expanded already. */
1073 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1075 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1076 TREE_OPERAND (*tp, 3) = NULL_TREE;
1079 /* Variable substitution need not be simple. In particular, the
1080 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1081 and friends are up-to-date. */
1082 else if (TREE_CODE (*tp) == ADDR_EXPR)
1084 int invariant = is_gimple_min_invariant (*tp);
1085 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1087 /* Handle the case where we substituted an INDIRECT_REF
1088 into the operand of the ADDR_EXPR. */
1089 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1090 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1092 recompute_tree_invariant_for_addr_expr (*tp);
1094 /* If this used to be invariant, but is not any longer,
1095 then regimplification is probably needed. */
1096 if (invariant && !is_gimple_min_invariant (*tp))
1097 id->regimplify = true;
1103 /* Keep iterating. */
1108 /* Helper for copy_bb. Remap statement STMT using the inlining
1109 information in ID. Return the new statement copy. */
1112 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1115 struct walk_stmt_info wi;
1117 bool skip_first = false;
1119 /* Begin by recognizing trees that we'll completely rewrite for the
1120 inlining context. Our output for these trees is completely
1121 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1122 into an edge). Further down, we'll handle trees that get
1123 duplicated and/or tweaked. */
1125 /* When requested, GIMPLE_RETURNs should be transformed to just the
1126 contained GIMPLE_ASSIGN. The branch semantics of the return will
1127 be handled elsewhere by manipulating the CFG rather than the
1129 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1131 tree retval = gimple_return_retval (stmt);
1133 /* If we're returning something, just turn that into an
1134 assignment into the equivalent of the original RESULT_DECL.
1135 If RETVAL is just the result decl, the result decl has
1136 already been set (e.g. a recent "foo (&result_decl, ...)");
1137 just toss the entire GIMPLE_RETURN. */
1138 if (retval && TREE_CODE (retval) != RESULT_DECL)
1140 copy = gimple_build_assign (id->retvar, retval);
1141 /* id->retvar is already substituted. Skip it on later remapping. */
1145 return gimple_build_nop ();
1147 else if (gimple_has_substatements (stmt))
1151 /* When cloning bodies from the C++ front end, we will be handed bodies
1152 in High GIMPLE form. Handle here all the High GIMPLE statements that
1153 have embedded statements. */
1154 switch (gimple_code (stmt))
1157 copy = copy_gimple_bind (stmt, id);
1161 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1162 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1165 case GIMPLE_EH_FILTER:
1166 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1167 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1171 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1172 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1173 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1176 case GIMPLE_WITH_CLEANUP_EXPR:
1177 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1178 copy = gimple_build_wce (s1);
1181 case GIMPLE_OMP_PARALLEL:
1182 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1183 copy = gimple_build_omp_parallel
1185 gimple_omp_parallel_clauses (stmt),
1186 gimple_omp_parallel_child_fn (stmt),
1187 gimple_omp_parallel_data_arg (stmt));
1190 case GIMPLE_OMP_TASK:
1191 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1192 copy = gimple_build_omp_task
1194 gimple_omp_task_clauses (stmt),
1195 gimple_omp_task_child_fn (stmt),
1196 gimple_omp_task_data_arg (stmt),
1197 gimple_omp_task_copy_fn (stmt),
1198 gimple_omp_task_arg_size (stmt),
1199 gimple_omp_task_arg_align (stmt));
1202 case GIMPLE_OMP_FOR:
1203 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1204 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1205 copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1206 gimple_omp_for_collapse (stmt), s2);
1209 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1211 gimple_omp_for_set_index (copy, i,
1212 gimple_omp_for_index (stmt, i));
1213 gimple_omp_for_set_initial (copy, i,
1214 gimple_omp_for_initial (stmt, i));
1215 gimple_omp_for_set_final (copy, i,
1216 gimple_omp_for_final (stmt, i));
1217 gimple_omp_for_set_incr (copy, i,
1218 gimple_omp_for_incr (stmt, i));
1219 gimple_omp_for_set_cond (copy, i,
1220 gimple_omp_for_cond (stmt, i));
1225 case GIMPLE_OMP_MASTER:
1226 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1227 copy = gimple_build_omp_master (s1);
1230 case GIMPLE_OMP_ORDERED:
1231 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1232 copy = gimple_build_omp_ordered (s1);
1235 case GIMPLE_OMP_SECTION:
1236 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1237 copy = gimple_build_omp_section (s1);
1240 case GIMPLE_OMP_SECTIONS:
1241 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1242 copy = gimple_build_omp_sections
1243 (s1, gimple_omp_sections_clauses (stmt));
1246 case GIMPLE_OMP_SINGLE:
1247 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1248 copy = gimple_build_omp_single
1249 (s1, gimple_omp_single_clauses (stmt));
1252 case GIMPLE_OMP_CRITICAL:
1253 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1255 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1264 if (gimple_assign_copy_p (stmt)
1265 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1266 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1268 /* Here we handle statements that are not completely rewritten.
1269 First we detect some inlining-induced bogosities for
1272 /* Some assignments VAR = VAR; don't generate any rtl code
1273 and thus don't count as variable modification. Avoid
1274 keeping bogosities like 0 = 0. */
1275 tree decl = gimple_assign_lhs (stmt), value;
1278 n = (tree *) pointer_map_contains (id->decl_map, decl);
1282 STRIP_TYPE_NOPS (value);
1283 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1284 return gimple_build_nop ();
1288 /* Create a new deep copy of the statement. */
1289 copy = gimple_copy (stmt);
1292 /* If STMT has a block defined, map it to the newly constructed
1293 block. When inlining we want statements without a block to
1294 appear in the block of the function call. */
1295 new_block = id->block;
1296 if (gimple_block (copy))
1299 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1304 gimple_set_block (copy, new_block);
1306 /* Remap all the operands in COPY. */
1307 memset (&wi, 0, sizeof (wi));
1310 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1312 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1314 /* Clear the copied virtual operands. We are not remapping them here
1315 but are going to recreate them from scratch. */
1316 if (gimple_has_mem_ops (copy))
1318 gimple_set_vdef (copy, NULL_TREE);
1319 gimple_set_vuse (copy, NULL_TREE);
1322 /* We have to handle EH region remapping of GIMPLE_RESX specially because
1323 the region number is not an operand. */
1324 if (gimple_code (stmt) == GIMPLE_RESX && id->eh_region_offset)
1326 gimple_resx_set_region (copy, gimple_resx_region (stmt) + id->eh_region_offset);
1332 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1336 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1337 gcov_type count_scale)
1339 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1340 basic_block copy_basic_block;
1343 /* create_basic_block() will append every new block to
1344 basic_block_info automatically. */
1345 copy_basic_block = create_basic_block (NULL, (void *) 0,
1346 (basic_block) bb->prev_bb->aux);
1347 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1349 /* We are going to rebuild frequencies from scratch. These values
1350 have just small importance to drive canonicalize_loop_headers. */
1351 copy_basic_block->frequency = ((gcov_type)bb->frequency
1352 * frequency_scale / REG_BR_PROB_BASE);
1354 if (copy_basic_block->frequency > BB_FREQ_MAX)
1355 copy_basic_block->frequency = BB_FREQ_MAX;
1357 copy_gsi = gsi_start_bb (copy_basic_block);
1359 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1361 gimple stmt = gsi_stmt (gsi);
1362 gimple orig_stmt = stmt;
1364 id->regimplify = false;
1365 stmt = remap_gimple_stmt (stmt, id);
1366 if (gimple_nop_p (stmt))
1369 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1372 /* With return slot optimization we can end up with
1373 non-gimple (foo *)&this->m, fix that here. */
1374 if (is_gimple_assign (stmt)
1375 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1376 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1379 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1380 gimple_assign_rhs1 (stmt),
1381 true, NULL, true, GSI_SAME_STMT);
1382 gimple_assign_set_rhs1 (stmt, new_rhs);
1383 id->regimplify = false;
1386 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1389 gimple_regimplify_operands (stmt, &seq_gsi);
1391 /* If copy_basic_block has been empty at the start of this iteration,
1392 call gsi_start_bb again to get at the newly added statements. */
1393 if (gsi_end_p (copy_gsi))
1394 copy_gsi = gsi_start_bb (copy_basic_block);
1396 gsi_next (©_gsi);
1398 /* Process the new statement. The call to gimple_regimplify_operands
1399 possibly turned the statement into multiple statements, we
1400 need to process all of them. */
1405 stmt = gsi_stmt (copy_gsi);
1406 if (is_gimple_call (stmt)
1407 && gimple_call_va_arg_pack_p (stmt)
1410 /* __builtin_va_arg_pack () should be replaced by
1411 all arguments corresponding to ... in the caller. */
1414 VEC(tree, heap) *argarray;
1415 size_t nargs = gimple_call_num_args (id->gimple_call);
1418 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1421 /* Create the new array of arguments. */
1422 n = nargs + gimple_call_num_args (stmt);
1423 argarray = VEC_alloc (tree, heap, n);
1424 VEC_safe_grow (tree, heap, argarray, n);
1426 /* Copy all the arguments before '...' */
1427 memcpy (VEC_address (tree, argarray),
1428 gimple_call_arg_ptr (stmt, 0),
1429 gimple_call_num_args (stmt) * sizeof (tree));
1431 /* Append the arguments passed in '...' */
1432 memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1433 gimple_call_arg_ptr (id->gimple_call, 0)
1434 + (gimple_call_num_args (id->gimple_call) - nargs),
1435 nargs * sizeof (tree));
1437 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1440 VEC_free (tree, heap, argarray);
1442 /* Copy all GIMPLE_CALL flags, location and block, except
1443 GF_CALL_VA_ARG_PACK. */
1444 gimple_call_copy_flags (new_call, stmt);
1445 gimple_call_set_va_arg_pack (new_call, false);
1446 gimple_set_location (new_call, gimple_location (stmt));
1447 gimple_set_block (new_call, gimple_block (stmt));
1448 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1450 gsi_replace (©_gsi, new_call, false);
1451 gimple_set_bb (stmt, NULL);
1454 else if (is_gimple_call (stmt)
1456 && (decl = gimple_call_fndecl (stmt))
1457 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1458 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1460 /* __builtin_va_arg_pack_len () should be replaced by
1461 the number of anonymous arguments. */
1462 size_t nargs = gimple_call_num_args (id->gimple_call);
1466 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1469 count = build_int_cst (integer_type_node, nargs);
1470 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1471 gsi_replace (©_gsi, new_stmt, false);
1475 /* Statements produced by inlining can be unfolded, especially
1476 when we constant propagated some operands. We can't fold
1477 them right now for two reasons:
1478 1) folding require SSA_NAME_DEF_STMTs to be correct
1479 2) we can't change function calls to builtins.
1480 So we just mark statement for later folding. We mark
1481 all new statements, instead just statements that has changed
1482 by some nontrivial substitution so even statements made
1483 foldable indirectly are updated. If this turns out to be
1484 expensive, copy_body can be told to watch for nontrivial
1486 if (id->statements_to_fold)
1487 pointer_set_insert (id->statements_to_fold, stmt);
1489 /* We're duplicating a CALL_EXPR. Find any corresponding
1490 callgraph edges and update or duplicate them. */
1491 if (is_gimple_call (stmt))
1493 struct cgraph_edge *edge = cgraph_edge (id->src_node, orig_stmt);
1496 switch (id->transform_call_graph_edges)
1498 case CB_CGE_DUPLICATE:
1500 cgraph_clone_edge (edge, id->dst_node, stmt,
1501 REG_BR_PROB_BASE, 1,
1502 edge->frequency, true);
1505 case CB_CGE_MOVE_CLONES:
1506 cgraph_set_call_stmt_including_clones (id->dst_node, orig_stmt, stmt);
1511 cgraph_set_call_stmt (edge, stmt);
1518 edge = cgraph_edge (id->src_node, orig_stmt);
1519 /* Constant propagation on argument done during inlining
1520 may create new direct call. Produce an edge for it. */
1522 || (edge->indirect_call
1523 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1524 && is_gimple_call (stmt)
1525 && (fn = gimple_call_fndecl (stmt)) != NULL)
1527 struct cgraph_node *dest = cgraph_node (fn);
1529 /* We have missing edge in the callgraph. This can happen in one case
1530 where previous inlining turned indirect call into direct call by
1531 constant propagating arguments. In all other cases we hit a bug
1532 (incorrect node sharing is most common reason for missing edges. */
1533 gcc_assert (dest->needed || !dest->analyzed);
1534 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1535 cgraph_create_edge_including_clones (id->dst_node, dest, stmt,
1537 compute_call_stmt_bb_frequency (id->dst_node->decl, bb),
1539 CIF_ORIGINALLY_INDIRECT_CALL);
1541 cgraph_create_edge (id->dst_node, dest, stmt,
1542 bb->count, CGRAPH_FREQ_BASE,
1543 bb->loop_depth)->inline_failed
1544 = CIF_ORIGINALLY_INDIRECT_CALL;
1547 fprintf (dump_file, "Created new direct edge to %s",
1548 cgraph_node_name (dest));
1552 flags = gimple_call_flags (stmt);
1554 if (flags & ECF_MAY_BE_ALLOCA)
1555 cfun->calls_alloca = true;
1556 if (flags & ECF_RETURNS_TWICE)
1557 cfun->calls_setjmp = true;
1560 /* If you think we can abort here, you are wrong.
1561 There is no region 0 in gimple. */
1562 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) != 0);
1564 if (stmt_could_throw_p (stmt)
1565 /* When we are cloning for inlining, we are supposed to
1566 construct a clone that calls precisely the same functions
1567 as original. However IPA optimizers might've proved
1568 earlier some function calls as non-trapping that might
1569 render some basic blocks dead that might become
1572 We can't update SSA with unreachable blocks in CFG and thus
1573 we prevent the scenario by preserving even the "dead" eh
1574 edges until the point they are later removed by
1576 || (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
1577 && lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) > 0))
1579 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
1581 /* Add an entry for the copied tree in the EH hashtable.
1582 When cloning or versioning, use the hashtable in
1583 cfun, and just copy the EH number. When inlining, use the
1584 hashtable in the caller, and adjust the region number. */
1586 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
1588 /* If this tree doesn't have a region associated with it,
1589 and there is a "current region,"
1590 then associate this tree with the current region
1591 and add edges associated with this region. */
1592 if (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) <= 0
1593 && id->eh_region > 0
1594 && stmt_could_throw_p (stmt))
1595 add_stmt_to_eh_region (stmt, id->eh_region);
1598 if (gimple_in_ssa_p (cfun))
1603 find_new_referenced_vars (gsi_stmt (copy_gsi));
1604 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1605 if (TREE_CODE (def) == SSA_NAME)
1606 SSA_NAME_DEF_STMT (def) = stmt;
1609 gsi_next (©_gsi);
1611 while (!gsi_end_p (copy_gsi));
1613 copy_gsi = gsi_last_bb (copy_basic_block);
1616 return copy_basic_block;
1619 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1620 form is quite easy, since dominator relationship for old basic blocks does
1623 There is however exception where inlining might change dominator relation
1624 across EH edges from basic block within inlined functions destinating
1625 to landing pads in function we inline into.
1627 The function fills in PHI_RESULTs of such PHI nodes if they refer
1628 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1629 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1630 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1631 set, and this means that there will be no overlapping live ranges
1632 for the underlying symbol.
1634 This might change in future if we allow redirecting of EH edges and
1635 we might want to change way build CFG pre-inlining to include
1636 all the possible edges then. */
1638 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1639 bool can_throw, bool nonlocal_goto)
1644 FOR_EACH_EDGE (e, ei, bb->succs)
1646 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1649 gimple_stmt_iterator si;
1652 gcc_assert (e->flags & EDGE_EH);
1655 gcc_assert (!(e->flags & EDGE_EH));
1657 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1661 phi = gsi_stmt (si);
1663 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1664 gcc_assert (!e->dest->aux);
1666 gcc_assert ((e->flags & EDGE_EH)
1667 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1669 if (!is_gimple_reg (PHI_RESULT (phi)))
1671 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1675 re = find_edge (ret_bb, e->dest);
1677 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1678 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1680 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1681 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1687 /* Copy edges from BB into its copy constructed earlier, scale profile
1688 accordingly. Edges will be taken care of later. Assume aux
1689 pointers to point to the copies of each BB. */
1692 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1694 basic_block new_bb = (basic_block) bb->aux;
1697 gimple_stmt_iterator si;
1700 /* Use the indices from the original blocks to create edges for the
1702 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1703 if (!(old_edge->flags & EDGE_EH))
1707 flags = old_edge->flags;
1709 /* Return edges do get a FALLTHRU flag when the get inlined. */
1710 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1711 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1712 flags |= EDGE_FALLTHRU;
1713 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1714 new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1715 new_edge->probability = old_edge->probability;
1718 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1721 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1724 bool can_throw, nonlocal_goto;
1726 copy_stmt = gsi_stmt (si);
1727 update_stmt (copy_stmt);
1728 if (gimple_in_ssa_p (cfun))
1729 mark_symbols_for_renaming (copy_stmt);
1731 /* Do this before the possible split_block. */
1734 /* If this tree could throw an exception, there are two
1735 cases where we need to add abnormal edge(s): the
1736 tree wasn't in a region and there is a "current
1737 region" in the caller; or the original tree had
1738 EH edges. In both cases split the block after the tree,
1739 and add abnormal edge(s) as needed; we need both
1740 those from the callee and the caller.
1741 We check whether the copy can throw, because the const
1742 propagation can change an INDIRECT_REF which throws
1743 into a COMPONENT_REF which doesn't. If the copy
1744 can throw, the original could also throw. */
1745 can_throw = stmt_can_throw_internal (copy_stmt);
1746 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1748 if (can_throw || nonlocal_goto)
1750 if (!gsi_end_p (si))
1751 /* Note that bb's predecessor edges aren't necessarily
1752 right at this point; split_block doesn't care. */
1754 edge e = split_block (new_bb, copy_stmt);
1757 new_bb->aux = e->src->aux;
1758 si = gsi_start_bb (new_bb);
1763 make_eh_edges (copy_stmt);
1766 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1768 if ((can_throw || nonlocal_goto)
1769 && gimple_in_ssa_p (cfun))
1770 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1771 can_throw, nonlocal_goto);
1775 /* Copy the PHIs. All blocks and edges are copied, some blocks
1776 was possibly split and new outgoing EH edges inserted.
1777 BB points to the block of original function and AUX pointers links
1778 the original and newly copied blocks. */
1781 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1783 basic_block const new_bb = (basic_block) bb->aux;
1786 gimple_stmt_iterator si;
1788 for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1794 phi = gsi_stmt (si);
1795 res = PHI_RESULT (phi);
1797 if (is_gimple_reg (res))
1799 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1800 SSA_NAME_DEF_STMT (new_res)
1801 = new_phi = create_phi_node (new_res, new_bb);
1802 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1805 = find_edge ((basic_block) new_edge->src->aux, bb);
1806 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1808 tree block = id->block;
1809 id->block = NULL_TREE;
1810 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1812 gcc_assert (new_arg);
1813 /* With return slot optimization we can end up with
1814 non-gimple (foo *)&this->m, fix that here. */
1815 if (TREE_CODE (new_arg) != SSA_NAME
1816 && TREE_CODE (new_arg) != FUNCTION_DECL
1817 && !is_gimple_val (new_arg))
1819 gimple_seq stmts = NULL;
1820 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1821 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1823 add_phi_arg (new_phi, new_arg, new_edge);
1830 /* Wrapper for remap_decl so it can be used as a callback. */
1833 remap_decl_1 (tree decl, void *data)
1835 return remap_decl (decl, (copy_body_data *) data);
1838 /* Build struct function and associated datastructures for the new clone
1839 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1842 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1845 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1846 gcov_type count_scale, frequency_scale;
1848 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1849 count_scale = (REG_BR_PROB_BASE * count
1850 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1854 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1855 frequency_scale = (REG_BR_PROB_BASE * frequency
1857 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1859 frequency_scale = count_scale;
1861 /* Register specific tree functions. */
1862 gimple_register_cfg_hooks ();
1864 /* Get clean struct function. */
1865 push_struct_function (new_fndecl);
1867 /* We will rebuild these, so just sanity check that they are empty. */
1868 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
1869 gcc_assert (cfun->local_decls == NULL);
1870 gcc_assert (cfun->cfg == NULL);
1871 gcc_assert (cfun->decl == new_fndecl);
1873 /* Copy items we preserve during clonning. */
1874 cfun->static_chain_decl = src_cfun->static_chain_decl;
1875 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
1876 cfun->function_end_locus = src_cfun->function_end_locus;
1877 cfun->curr_properties = src_cfun->curr_properties;
1878 cfun->last_verified = src_cfun->last_verified;
1879 if (src_cfun->ipa_transforms_to_apply)
1880 cfun->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
1881 src_cfun->ipa_transforms_to_apply);
1882 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
1883 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
1884 cfun->function_frequency = src_cfun->function_frequency;
1885 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
1886 cfun->stdarg = src_cfun->stdarg;
1887 cfun->dont_save_pending_sizes_p = src_cfun->dont_save_pending_sizes_p;
1888 cfun->after_inlining = src_cfun->after_inlining;
1889 cfun->returns_struct = src_cfun->returns_struct;
1890 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
1891 cfun->after_tree_profile = src_cfun->after_tree_profile;
1893 init_empty_tree_cfg ();
1895 ENTRY_BLOCK_PTR->count =
1896 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1898 ENTRY_BLOCK_PTR->frequency =
1899 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1900 frequency_scale / REG_BR_PROB_BASE);
1901 EXIT_BLOCK_PTR->count =
1902 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1904 EXIT_BLOCK_PTR->frequency =
1905 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1906 frequency_scale / REG_BR_PROB_BASE);
1908 init_eh_for_function ();
1910 if (src_cfun->gimple_df)
1912 init_tree_ssa (cfun);
1913 cfun->gimple_df->in_ssa_p = true;
1914 init_ssa_operands ();
1919 /* Make a copy of the body of FN so that it can be inserted inline in
1920 another function. Walks FN via CFG, returns new fndecl. */
1923 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1924 basic_block entry_block_map, basic_block exit_block_map)
1926 tree callee_fndecl = id->src_fn;
1927 /* Original cfun for the callee, doesn't change. */
1928 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1929 struct function *cfun_to_copy;
1931 tree new_fndecl = NULL;
1932 gcov_type count_scale, frequency_scale;
1935 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1936 count_scale = (REG_BR_PROB_BASE * count
1937 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1941 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1942 frequency_scale = (REG_BR_PROB_BASE * frequency
1944 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1946 frequency_scale = count_scale;
1948 /* Register specific tree functions. */
1949 gimple_register_cfg_hooks ();
1951 /* Must have a CFG here at this point. */
1952 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1953 (DECL_STRUCT_FUNCTION (callee_fndecl)));
1955 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1957 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1958 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1959 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1960 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1962 /* Duplicate any exception-handling regions. */
1965 id->eh_region_offset
1966 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1970 /* Use aux pointers to map the original blocks to copy. */
1971 FOR_EACH_BB_FN (bb, cfun_to_copy)
1973 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
1978 last = last_basic_block;
1980 /* Now that we've duplicated the blocks, duplicate their edges. */
1981 FOR_ALL_BB_FN (bb, cfun_to_copy)
1982 copy_edges_for_bb (bb, count_scale, exit_block_map);
1984 if (gimple_in_ssa_p (cfun))
1985 FOR_ALL_BB_FN (bb, cfun_to_copy)
1986 copy_phis_for_bb (bb, id);
1988 FOR_ALL_BB_FN (bb, cfun_to_copy)
1990 ((basic_block)bb->aux)->aux = NULL;
1994 /* Zero out AUX fields of newly created block during EH edge
1996 for (; last < last_basic_block; last++)
1997 BASIC_BLOCK (last)->aux = NULL;
1998 entry_block_map->aux = NULL;
1999 exit_block_map->aux = NULL;
2004 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2005 another function. */
2008 copy_tree_body (copy_body_data *id)
2010 tree fndecl = id->src_fn;
2011 tree body = DECL_SAVED_TREE (fndecl);
2013 walk_tree (&body, copy_tree_body_r, id, NULL);
2019 copy_body (copy_body_data *id, gcov_type count, int frequency,
2020 basic_block entry_block_map, basic_block exit_block_map)
2022 tree fndecl = id->src_fn;
2025 /* If this body has a CFG, walk CFG and copy. */
2026 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
2027 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
2032 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2033 defined in function FN, or of a data member thereof. */
2036 self_inlining_addr_expr (tree value, tree fn)
2040 if (TREE_CODE (value) != ADDR_EXPR)
2043 var = get_base_address (TREE_OPERAND (value, 0));
2045 return var && auto_var_in_fn_p (var, fn);
2049 insert_init_stmt (basic_block bb, gimple init_stmt)
2051 /* If VAR represents a zero-sized variable, it's possible that the
2052 assignment statement may result in no gimple statements. */
2055 gimple_stmt_iterator si = gsi_last_bb (bb);
2057 /* We can end up with init statements that store to a non-register
2058 from a rhs with a conversion. Handle that here by forcing the
2059 rhs into a temporary. gimple_regimplify_operands is not
2060 prepared to do this for us. */
2061 if (!is_gimple_reg (gimple_assign_lhs (init_stmt))
2062 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2063 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2065 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2066 gimple_expr_type (init_stmt),
2067 gimple_assign_rhs1 (init_stmt));
2068 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
2070 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
2071 gimple_assign_set_rhs1 (init_stmt, rhs);
2073 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2074 gimple_regimplify_operands (init_stmt, &si);
2075 mark_symbols_for_renaming (init_stmt);
2079 /* Initialize parameter P with VALUE. If needed, produce init statement
2080 at the end of BB. When BB is NULL, we return init statement to be
2083 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2084 basic_block bb, tree *vars)
2086 gimple init_stmt = NULL;
2089 tree def = (gimple_in_ssa_p (cfun)
2090 ? gimple_default_def (id->src_cfun, p) : NULL);
2093 && value != error_mark_node
2094 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
2096 if (fold_convertible_p (TREE_TYPE (p), value))
2097 rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
2099 /* ??? For valid (GIMPLE) programs we should not end up here.
2100 Still if something has gone wrong and we end up with truly
2101 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
2102 to not leak invalid GIMPLE to the following passes. */
2103 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2106 /* If the parameter is never assigned to, has no SSA_NAMEs created,
2107 we may not need to create a new variable here at all. Instead, we may
2108 be able to just use the argument value. */
2109 if (TREE_READONLY (p)
2110 && !TREE_ADDRESSABLE (p)
2111 && value && !TREE_SIDE_EFFECTS (value)
2114 /* We may produce non-gimple trees by adding NOPs or introduce
2115 invalid sharing when operand is not really constant.
2116 It is not big deal to prohibit constant propagation here as
2117 we will constant propagate in DOM1 pass anyway. */
2118 if (is_gimple_min_invariant (value)
2119 && useless_type_conversion_p (TREE_TYPE (p),
2121 /* We have to be very careful about ADDR_EXPR. Make sure
2122 the base variable isn't a local variable of the inlined
2123 function, e.g., when doing recursive inlining, direct or
2124 mutually-recursive or whatever, which is why we don't
2125 just test whether fn == current_function_decl. */
2126 && ! self_inlining_addr_expr (value, fn))
2128 insert_decl_map (id, p, value);
2133 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2134 here since the type of this decl must be visible to the calling
2136 var = copy_decl_to_var (p, id);
2137 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2140 add_referenced_var (var);
2143 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2144 that way, when the PARM_DECL is encountered, it will be
2145 automatically replaced by the VAR_DECL. */
2146 insert_decl_map (id, p, var);
2148 /* Declare this new variable. */
2149 TREE_CHAIN (var) = *vars;
2152 /* Make gimplifier happy about this variable. */
2153 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2155 /* Even if P was TREE_READONLY, the new VAR should not be.
2156 In the original code, we would have constructed a
2157 temporary, and then the function body would have never
2158 changed the value of P. However, now, we will be
2159 constructing VAR directly. The constructor body may
2160 change its value multiple times as it is being
2161 constructed. Therefore, it must not be TREE_READONLY;
2162 the back-end assumes that TREE_READONLY variable is
2163 assigned to only once. */
2164 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2165 TREE_READONLY (var) = 0;
2167 /* If there is no setup required and we are in SSA, take the easy route
2168 replacing all SSA names representing the function parameter by the
2169 SSA name passed to function.
2171 We need to construct map for the variable anyway as it might be used
2172 in different SSA names when parameter is set in function.
2174 Do replacement at -O0 for const arguments replaced by constant.
2175 This is important for builtin_constant_p and other construct requiring
2176 constant argument to be visible in inlined function body.
2178 FIXME: This usually kills the last connection in between inlined
2179 function parameter and the actual value in debug info. Can we do
2180 better here? If we just inserted the statement, copy propagation
2181 would kill it anyway as it always did in older versions of GCC.
2183 We might want to introduce a notion that single SSA_NAME might
2184 represent multiple variables for purposes of debugging. */
2185 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2187 || (TREE_READONLY (p)
2188 && is_gimple_min_invariant (rhs)))
2189 && (TREE_CODE (rhs) == SSA_NAME
2190 || is_gimple_min_invariant (rhs))
2191 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2193 insert_decl_map (id, def, rhs);
2197 /* If the value of argument is never used, don't care about initializing
2199 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2201 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2205 /* Initialize this VAR_DECL from the equivalent argument. Convert
2206 the argument to the proper type in case it was promoted. */
2209 if (rhs == error_mark_node)
2211 insert_decl_map (id, p, var);
2215 STRIP_USELESS_TYPE_CONVERSION (rhs);
2217 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2218 keep our trees in gimple form. */
2219 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2221 def = remap_ssa_name (def, id);
2222 init_stmt = gimple_build_assign (def, rhs);
2223 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2224 set_default_def (var, NULL);
2227 init_stmt = gimple_build_assign (var, rhs);
2229 if (bb && init_stmt)
2230 insert_init_stmt (bb, init_stmt);
2235 /* Generate code to initialize the parameters of the function at the
2236 top of the stack in ID from the GIMPLE_CALL STMT. */
2239 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2240 tree fn, basic_block bb)
2245 tree vars = NULL_TREE;
2246 tree static_chain = gimple_call_chain (stmt);
2248 /* Figure out what the parameters are. */
2249 parms = DECL_ARGUMENTS (fn);
2251 /* Loop through the parameter declarations, replacing each with an
2252 equivalent VAR_DECL, appropriately initialized. */
2253 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2256 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2257 setup_one_parameter (id, p, val, fn, bb, &vars);
2260 /* Initialize the static chain. */
2261 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2262 gcc_assert (fn != current_function_decl);
2265 /* No static chain? Seems like a bug in tree-nested.c. */
2266 gcc_assert (static_chain);
2268 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2271 declare_inline_vars (id->block, vars);
2275 /* Declare a return variable to replace the RESULT_DECL for the
2276 function we are calling. An appropriate DECL_STMT is returned.
2277 The USE_STMT is filled to contain a use of the declaration to
2278 indicate the return value of the function.
2280 RETURN_SLOT, if non-null is place where to store the result. It
2281 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2282 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2284 The return value is a (possibly null) value that is the result of the
2285 function as seen by the callee. *USE_P is a (possibly null) value that
2286 holds the result as seen by the caller. */
2289 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
2292 tree callee = id->src_fn;
2293 tree caller = id->dst_fn;
2294 tree result = DECL_RESULT (callee);
2295 tree callee_type = TREE_TYPE (result);
2296 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
2299 /* We don't need to do anything for functions that don't return
2301 if (!result || VOID_TYPE_P (callee_type))
2307 /* If there was a return slot, then the return value is the
2308 dereferenced address of that object. */
2311 /* The front end shouldn't have used both return_slot and
2312 a modify expression. */
2313 gcc_assert (!modify_dest);
2314 if (DECL_BY_REFERENCE (result))
2316 tree return_slot_addr = build_fold_addr_expr (return_slot);
2317 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2319 /* We are going to construct *&return_slot and we can't do that
2320 for variables believed to be not addressable.
2322 FIXME: This check possibly can match, because values returned
2323 via return slot optimization are not believed to have address
2324 taken by alias analysis. */
2325 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2326 if (gimple_in_ssa_p (cfun))
2328 HOST_WIDE_INT bitsize;
2329 HOST_WIDE_INT bitpos;
2331 enum machine_mode mode;
2335 base = get_inner_reference (return_slot, &bitsize, &bitpos,
2337 &mode, &unsignedp, &volatilep,
2339 if (TREE_CODE (base) == INDIRECT_REF)
2340 base = TREE_OPERAND (base, 0);
2341 if (TREE_CODE (base) == SSA_NAME)
2342 base = SSA_NAME_VAR (base);
2343 mark_sym_for_renaming (base);
2345 var = return_slot_addr;
2350 gcc_assert (TREE_CODE (var) != SSA_NAME);
2351 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2353 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2354 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2355 && !DECL_GIMPLE_REG_P (result)
2357 DECL_GIMPLE_REG_P (var) = 0;
2362 /* All types requiring non-trivial constructors should have been handled. */
2363 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2365 /* Attempt to avoid creating a new temporary variable. */
2367 && TREE_CODE (modify_dest) != SSA_NAME)
2369 bool use_it = false;
2371 /* We can't use MODIFY_DEST if there's type promotion involved. */
2372 if (!useless_type_conversion_p (callee_type, caller_type))
2375 /* ??? If we're assigning to a variable sized type, then we must
2376 reuse the destination variable, because we've no good way to
2377 create variable sized temporaries at this point. */
2378 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2381 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2382 reuse it as the result of the call directly. Don't do this if
2383 it would promote MODIFY_DEST to addressable. */
2384 else if (TREE_ADDRESSABLE (result))
2388 tree base_m = get_base_address (modify_dest);
2390 /* If the base isn't a decl, then it's a pointer, and we don't
2391 know where that's going to go. */
2392 if (!DECL_P (base_m))
2394 else if (is_global_var (base_m))
2396 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2397 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2398 && !DECL_GIMPLE_REG_P (result)
2399 && DECL_GIMPLE_REG_P (base_m))
2401 else if (!TREE_ADDRESSABLE (base_m))
2413 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2415 var = copy_result_decl_to_var (result, id);
2416 if (gimple_in_ssa_p (cfun))
2419 add_referenced_var (var);
2422 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2423 DECL_STRUCT_FUNCTION (caller)->local_decls
2424 = tree_cons (NULL_TREE, var,
2425 DECL_STRUCT_FUNCTION (caller)->local_decls);
2427 /* Do not have the rest of GCC warn about this variable as it should
2428 not be visible to the user. */
2429 TREE_NO_WARNING (var) = 1;
2431 declare_inline_vars (id->block, var);
2433 /* Build the use expr. If the return type of the function was
2434 promoted, convert it back to the expected type. */
2436 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2437 use = fold_convert (caller_type, var);
2439 STRIP_USELESS_TYPE_CONVERSION (use);
2441 if (DECL_BY_REFERENCE (result))
2443 TREE_ADDRESSABLE (var) = 1;
2444 var = build_fold_addr_expr (var);
2448 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2449 way, when the RESULT_DECL is encountered, it will be
2450 automatically replaced by the VAR_DECL. */
2451 insert_decl_map (id, result, var);
2453 /* Remember this so we can ignore it in remap_decls. */
2460 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
2461 to a local label. */
2464 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
2467 tree fn = (tree) fnp;
2469 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2478 /* Callback through walk_tree. Determine if we've got an aggregate
2479 type that we can't support; return non-null if so. */
2482 cannot_copy_type_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
2483 void *data ATTRIBUTE_UNUSED)
2485 tree t, node = *nodep;
2487 if (TREE_CODE (node) == RECORD_TYPE || TREE_CODE (node) == UNION_TYPE)
2489 /* We cannot inline a function of the form
2491 void F (int i) { struct S { int ar[i]; } s; }
2493 Attempting to do so produces a catch-22.
2494 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
2495 UNION_TYPE nodes, then it goes into infinite recursion on a
2496 structure containing a pointer to its own type. If it doesn't,
2497 then the type node for S doesn't get adjusted properly when
2500 ??? This is likely no longer true, but it's too late in the 4.0
2501 cycle to try to find out. This should be checked for 4.1. */
2502 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
2503 if (variably_modified_type_p (TREE_TYPE (t), NULL))
2511 /* Determine if the function can be copied. If so return NULL. If
2512 not return a string describng the reason for failure. */
2515 copy_forbidden (struct function *fun, tree fndecl)
2517 const char *reason = fun->cannot_be_copied_reason;
2520 /* Only examine the function once. */
2521 if (fun->cannot_be_copied_set)
2524 /* We cannot copy a function that receives a non-local goto
2525 because we cannot remap the destination label used in the
2526 function that is performing the non-local goto. */
2527 /* ??? Actually, this should be possible, if we work at it.
2528 No doubt there's just a handful of places that simply
2529 assume it doesn't happen and don't substitute properly. */
2530 if (fun->has_nonlocal_label)
2532 reason = G_("function %q+F can never be copied "
2533 "because it receives a non-local goto");
2537 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2539 tree decl = TREE_VALUE (step);
2541 if (TREE_CODE (decl) == VAR_DECL
2542 && TREE_STATIC (decl)
2543 && !DECL_EXTERNAL (decl)
2544 && DECL_INITIAL (decl)
2545 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
2546 has_label_address_in_static_1,
2549 reason = G_("function %q+F can never be copied because it saves "
2550 "address of local label in a static variable");
2554 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2555 && variably_modified_type_p (TREE_TYPE (decl), NULL)
2556 && walk_tree_without_duplicates (&TREE_TYPE (decl),
2557 cannot_copy_type_1, NULL))
2559 reason = G_("function %q+F can never be copied "
2560 "because it uses variable sized variables");
2566 fun->cannot_be_copied_reason = reason;
2567 fun->cannot_be_copied_set = true;
2572 static const char *inline_forbidden_reason;
2574 /* A callback for walk_gimple_seq to handle statements. Returns non-null
2575 iff a function can not be inlined. Also sets the reason why. */
2578 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2579 struct walk_stmt_info *wip)
2581 tree fn = (tree) wip->info;
2583 gimple stmt = gsi_stmt (*gsi);
2585 switch (gimple_code (stmt))
2588 /* Refuse to inline alloca call unless user explicitly forced so as
2589 this may change program's memory overhead drastically when the
2590 function using alloca is called in loop. In GCC present in
2591 SPEC2000 inlining into schedule_block cause it to require 2GB of
2592 RAM instead of 256MB. */
2593 if (gimple_alloca_call_p (stmt)
2594 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2596 inline_forbidden_reason
2597 = G_("function %q+F can never be inlined because it uses "
2598 "alloca (override using the always_inline attribute)");
2599 *handled_ops_p = true;
2603 t = gimple_call_fndecl (stmt);
2607 /* We cannot inline functions that call setjmp. */
2608 if (setjmp_call_p (t))
2610 inline_forbidden_reason
2611 = G_("function %q+F can never be inlined because it uses setjmp");
2612 *handled_ops_p = true;
2616 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
2617 switch (DECL_FUNCTION_CODE (t))
2619 /* We cannot inline functions that take a variable number of
2621 case BUILT_IN_VA_START:
2622 case BUILT_IN_NEXT_ARG:
2623 case BUILT_IN_VA_END:
2624 inline_forbidden_reason
2625 = G_("function %q+F can never be inlined because it "
2626 "uses variable argument lists");
2627 *handled_ops_p = true;
2630 case BUILT_IN_LONGJMP:
2631 /* We can't inline functions that call __builtin_longjmp at
2632 all. The non-local goto machinery really requires the
2633 destination be in a different function. If we allow the
2634 function calling __builtin_longjmp to be inlined into the
2635 function calling __builtin_setjmp, Things will Go Awry. */
2636 inline_forbidden_reason
2637 = G_("function %q+F can never be inlined because "
2638 "it uses setjmp-longjmp exception handling");
2639 *handled_ops_p = true;
2642 case BUILT_IN_NONLOCAL_GOTO:
2644 inline_forbidden_reason
2645 = G_("function %q+F can never be inlined because "
2646 "it uses non-local goto");
2647 *handled_ops_p = true;
2650 case BUILT_IN_RETURN:
2651 case BUILT_IN_APPLY_ARGS:
2652 /* If a __builtin_apply_args caller would be inlined,
2653 it would be saving arguments of the function it has
2654 been inlined into. Similarly __builtin_return would
2655 return from the function the inline has been inlined into. */
2656 inline_forbidden_reason
2657 = G_("function %q+F can never be inlined because "
2658 "it uses __builtin_return or __builtin_apply_args");
2659 *handled_ops_p = true;
2668 t = gimple_goto_dest (stmt);
2670 /* We will not inline a function which uses computed goto. The
2671 addresses of its local labels, which may be tucked into
2672 global storage, are of course not constant across
2673 instantiations, which causes unexpected behavior. */
2674 if (TREE_CODE (t) != LABEL_DECL)
2676 inline_forbidden_reason
2677 = G_("function %q+F can never be inlined "
2678 "because it contains a computed goto");
2679 *handled_ops_p = true;
2688 *handled_ops_p = false;
2692 /* Return true if FNDECL is a function that cannot be inlined into
2696 inline_forbidden_p (tree fndecl)
2698 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2699 struct walk_stmt_info wi;
2700 struct pointer_set_t *visited_nodes;
2702 bool forbidden_p = false;
2704 /* First check for shared reasons not to copy the code. */
2705 inline_forbidden_reason = copy_forbidden (fun, fndecl);
2706 if (inline_forbidden_reason != NULL)
2709 /* Next, walk the statements of the function looking for
2710 constraucts we can't handle, or are non-optimal for inlining. */
2711 visited_nodes = pointer_set_create ();
2712 memset (&wi, 0, sizeof (wi));
2713 wi.info = (void *) fndecl;
2714 wi.pset = visited_nodes;
2716 FOR_EACH_BB_FN (bb, fun)
2719 gimple_seq seq = bb_seq (bb);
2720 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
2721 forbidden_p = (ret != NULL);
2726 pointer_set_destroy (visited_nodes);
2730 /* Returns nonzero if FN is a function that does not have any
2731 fundamental inline blocking properties. */
2734 tree_inlinable_function_p (tree fn)
2736 bool inlinable = true;
2740 /* If we've already decided this function shouldn't be inlined,
2741 there's no need to check again. */
2742 if (DECL_UNINLINABLE (fn))
2745 /* We only warn for functions declared `inline' by the user. */
2746 do_warning = (warn_inline
2747 && DECL_DECLARED_INLINE_P (fn)
2748 && !DECL_NO_INLINE_WARNING_P (fn)
2749 && !DECL_IN_SYSTEM_HEADER (fn));
2751 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
2754 && always_inline == NULL)
2757 warning (OPT_Winline, "function %q+F can never be inlined because it "
2758 "is suppressed using -fno-inline", fn);
2762 /* Don't auto-inline anything that might not be bound within
2763 this unit of translation. */
2764 else if (!DECL_DECLARED_INLINE_P (fn)
2765 && DECL_REPLACEABLE_P (fn))
2768 else if (!function_attribute_inlinable_p (fn))
2771 warning (OPT_Winline, "function %q+F can never be inlined because it "
2772 "uses attributes conflicting with inlining", fn);
2776 else if (inline_forbidden_p (fn))
2778 /* See if we should warn about uninlinable functions. Previously,
2779 some of these warnings would be issued while trying to expand
2780 the function inline, but that would cause multiple warnings
2781 about functions that would for example call alloca. But since
2782 this a property of the function, just one warning is enough.
2783 As a bonus we can now give more details about the reason why a
2784 function is not inlinable. */
2786 sorry (inline_forbidden_reason, fn);
2787 else if (do_warning)
2788 warning (OPT_Winline, inline_forbidden_reason, fn);
2793 /* Squirrel away the result so that we don't have to check again. */
2794 DECL_UNINLINABLE (fn) = !inlinable;
2799 /* Estimate the cost of a memory move. Use machine dependent
2800 word size and take possible memcpy call into account. */
2803 estimate_move_cost (tree type)
2807 gcc_assert (!VOID_TYPE_P (type));
2809 size = int_size_in_bytes (type);
2811 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
2812 /* Cost of a memcpy call, 3 arguments and the call. */
2815 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
2818 /* Returns cost of operation CODE, according to WEIGHTS */
2821 estimate_operator_cost (enum tree_code code, eni_weights *weights,
2822 tree op1 ATTRIBUTE_UNUSED, tree op2)
2826 /* These are "free" conversions, or their presumed cost
2827 is folded into other operations. */
2834 /* Assign cost of 1 to usual operations.
2835 ??? We may consider mapping RTL costs to this. */
2840 case POINTER_PLUS_EXPR:
2844 case FIXED_CONVERT_EXPR:
2845 case FIX_TRUNC_EXPR:
2857 case VEC_LSHIFT_EXPR:
2858 case VEC_RSHIFT_EXPR:
2865 case TRUTH_ANDIF_EXPR:
2866 case TRUTH_ORIF_EXPR:
2867 case TRUTH_AND_EXPR:
2869 case TRUTH_XOR_EXPR:
2870 case TRUTH_NOT_EXPR:
2879 case UNORDERED_EXPR:
2890 case PREDECREMENT_EXPR:
2891 case PREINCREMENT_EXPR:
2892 case POSTDECREMENT_EXPR:
2893 case POSTINCREMENT_EXPR:
2895 case REALIGN_LOAD_EXPR:
2897 case REDUC_MAX_EXPR:
2898 case REDUC_MIN_EXPR:
2899 case REDUC_PLUS_EXPR:
2900 case WIDEN_SUM_EXPR:
2901 case WIDEN_MULT_EXPR:
2904 case VEC_WIDEN_MULT_HI_EXPR:
2905 case VEC_WIDEN_MULT_LO_EXPR:
2906 case VEC_UNPACK_HI_EXPR:
2907 case VEC_UNPACK_LO_EXPR:
2908 case VEC_UNPACK_FLOAT_HI_EXPR:
2909 case VEC_UNPACK_FLOAT_LO_EXPR:
2910 case VEC_PACK_TRUNC_EXPR:
2911 case VEC_PACK_SAT_EXPR:
2912 case VEC_PACK_FIX_TRUNC_EXPR:
2913 case VEC_EXTRACT_EVEN_EXPR:
2914 case VEC_EXTRACT_ODD_EXPR:
2915 case VEC_INTERLEAVE_HIGH_EXPR:
2916 case VEC_INTERLEAVE_LOW_EXPR:
2920 /* Few special cases of expensive operations. This is useful
2921 to avoid inlining on functions having too many of these. */
2922 case TRUNC_DIV_EXPR:
2924 case FLOOR_DIV_EXPR:
2925 case ROUND_DIV_EXPR:
2926 case EXACT_DIV_EXPR:
2927 case TRUNC_MOD_EXPR:
2929 case FLOOR_MOD_EXPR:
2930 case ROUND_MOD_EXPR:
2932 if (TREE_CODE (op2) != INTEGER_CST)
2933 return weights->div_mod_cost;
2937 /* We expect a copy assignment with no operator. */
2938 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
2944 /* Estimate number of instructions that will be created by expanding
2945 the statements in the statement sequence STMTS.
2946 WEIGHTS contains weights attributed to various constructs. */
2949 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
2952 gimple_stmt_iterator gsi;
2955 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2956 cost += estimate_num_insns (gsi_stmt (gsi), weights);
2962 /* Estimate number of instructions that will be created by expanding STMT.
2963 WEIGHTS contains weights attributed to various constructs. */
2966 estimate_num_insns (gimple stmt, eni_weights *weights)
2969 enum gimple_code code = gimple_code (stmt);
2976 /* Try to estimate the cost of assignments. We have three cases to
2978 1) Simple assignments to registers;
2979 2) Stores to things that must live in memory. This includes
2980 "normal" stores to scalars, but also assignments of large
2981 structures, or constructors of big arrays;
2983 Let us look at the first two cases, assuming we have "a = b + C":
2984 <GIMPLE_ASSIGN <var_decl "a">
2985 <plus_expr <var_decl "b"> <constant C>>
2986 If "a" is a GIMPLE register, the assignment to it is free on almost
2987 any target, because "a" usually ends up in a real register. Hence
2988 the only cost of this expression comes from the PLUS_EXPR, and we
2989 can ignore the GIMPLE_ASSIGN.
2990 If "a" is not a GIMPLE register, the assignment to "a" will most
2991 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
2992 of moving something into "a", which we compute using the function
2993 estimate_move_cost. */
2994 lhs = gimple_assign_lhs (stmt);
2995 rhs = gimple_assign_rhs1 (stmt);
2997 /* EH magic stuff is most probably going to be optimized out.
2998 We rarely really need to save EH info for unwinding
2999 nested exceptions. */
3000 if (TREE_CODE (lhs) == FILTER_EXPR
3001 || TREE_CODE (lhs) == EXC_PTR_EXPR
3002 || TREE_CODE (rhs) == FILTER_EXPR
3003 || TREE_CODE (rhs) == EXC_PTR_EXPR)
3005 if (is_gimple_reg (lhs))
3008 cost = estimate_move_cost (TREE_TYPE (lhs));
3010 if (!is_gimple_reg (rhs) && !is_gimple_min_invariant (rhs))
3011 cost += estimate_move_cost (TREE_TYPE (rhs));
3013 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3014 gimple_assign_rhs1 (stmt),
3015 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3016 == GIMPLE_BINARY_RHS
3017 ? gimple_assign_rhs2 (stmt) : NULL);
3021 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3022 gimple_op (stmt, 0),
3023 gimple_op (stmt, 1));
3027 /* Take into account cost of the switch + guess 2 conditional jumps for
3030 TODO: once the switch expansion logic is sufficiently separated, we can
3031 do better job on estimating cost of the switch. */
3032 if (weights->time_based)
3033 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3035 cost = gimple_switch_num_labels (stmt) * 2;
3040 tree decl = gimple_call_fndecl (stmt);
3041 tree addr = gimple_call_fn (stmt);
3042 tree funtype = TREE_TYPE (addr);
3044 if (POINTER_TYPE_P (funtype))
3045 funtype = TREE_TYPE (funtype);
3047 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
3048 cost = weights->target_builtin_call_cost;
3050 cost = weights->call_cost;
3052 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3053 switch (DECL_FUNCTION_CODE (decl))
3055 case BUILT_IN_CONSTANT_P:
3057 case BUILT_IN_EXPECT:
3060 /* Prefetch instruction is not expensive. */
3061 case BUILT_IN_PREFETCH:
3062 cost = weights->target_builtin_call_cost;
3070 funtype = TREE_TYPE (decl);
3072 if (!VOID_TYPE_P (TREE_TYPE (funtype)))
3073 cost += estimate_move_cost (TREE_TYPE (funtype));
3074 /* Our cost must be kept in sync with
3075 cgraph_estimate_size_after_inlining that does use function
3076 declaration to figure out the arguments. */
3077 if (decl && DECL_ARGUMENTS (decl))
3080 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
3081 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3082 cost += estimate_move_cost (TREE_TYPE (arg));
3084 else if (funtype && prototype_p (funtype))
3087 for (t = TYPE_ARG_TYPES (funtype); t && t != void_list_node;
3089 if (!VOID_TYPE_P (TREE_VALUE (t)))
3090 cost += estimate_move_cost (TREE_VALUE (t));
3094 for (i = 0; i < gimple_call_num_args (stmt); i++)
3096 tree arg = gimple_call_arg (stmt, i);
3097 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3098 cost += estimate_move_cost (TREE_TYPE (arg));
3110 case GIMPLE_PREDICT:
3118 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
3120 case GIMPLE_EH_FILTER:
3121 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
3124 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
3127 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
3128 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
3130 /* OpenMP directives are generally very expensive. */
3132 case GIMPLE_OMP_RETURN:
3133 case GIMPLE_OMP_SECTIONS_SWITCH:
3134 case GIMPLE_OMP_ATOMIC_STORE:
3135 case GIMPLE_OMP_CONTINUE:
3136 /* ...except these, which are cheap. */
3139 case GIMPLE_OMP_ATOMIC_LOAD:
3140 return weights->omp_cost;
3142 case GIMPLE_OMP_FOR:
3143 return (weights->omp_cost
3144 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
3145 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
3147 case GIMPLE_OMP_PARALLEL:
3148 case GIMPLE_OMP_TASK:
3149 case GIMPLE_OMP_CRITICAL:
3150 case GIMPLE_OMP_MASTER:
3151 case GIMPLE_OMP_ORDERED:
3152 case GIMPLE_OMP_SECTION:
3153 case GIMPLE_OMP_SECTIONS:
3154 case GIMPLE_OMP_SINGLE:
3155 return (weights->omp_cost
3156 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
3165 /* Estimate number of instructions that will be created by expanding
3166 function FNDECL. WEIGHTS contains weights attributed to various
3170 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
3172 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
3173 gimple_stmt_iterator bsi;
3177 gcc_assert (my_function && my_function->cfg);
3178 FOR_EACH_BB_FN (bb, my_function)
3180 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3181 n += estimate_num_insns (gsi_stmt (bsi), weights);
3188 /* Initializes weights used by estimate_num_insns. */
3191 init_inline_once (void)
3193 eni_size_weights.call_cost = 1;
3194 eni_size_weights.target_builtin_call_cost = 1;
3195 eni_size_weights.div_mod_cost = 1;
3196 eni_size_weights.omp_cost = 40;
3197 eni_size_weights.time_based = false;
3199 /* Estimating time for call is difficult, since we have no idea what the
3200 called function does. In the current uses of eni_time_weights,
3201 underestimating the cost does less harm than overestimating it, so
3202 we choose a rather small value here. */
3203 eni_time_weights.call_cost = 10;
3204 eni_time_weights.target_builtin_call_cost = 10;
3205 eni_time_weights.div_mod_cost = 10;
3206 eni_time_weights.omp_cost = 40;
3207 eni_time_weights.time_based = true;
3210 /* Estimate the number of instructions in a gimple_seq. */
3213 count_insns_seq (gimple_seq seq, eni_weights *weights)
3215 gimple_stmt_iterator gsi;
3217 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3218 n += estimate_num_insns (gsi_stmt (gsi), weights);
3224 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3227 prepend_lexical_block (tree current_block, tree new_block)
3229 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3230 BLOCK_SUBBLOCKS (current_block) = new_block;
3231 BLOCK_SUPERCONTEXT (new_block) = current_block;
3234 /* Fetch callee declaration from the call graph edge going from NODE and
3235 associated with STMR call statement. Return NULL_TREE if not found. */
3237 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3239 struct cgraph_edge *cs;
3241 cs = cgraph_edge (node, stmt);
3243 return cs->callee->decl;
3248 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3251 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3253 tree retvar, use_retvar;
3255 struct pointer_map_t *st;
3258 location_t saved_location;
3259 struct cgraph_edge *cg_edge;
3260 cgraph_inline_failed_t reason;
3261 basic_block return_block;
3263 gimple_stmt_iterator gsi, stmt_gsi;
3264 bool successfully_inlined = FALSE;
3265 bool purge_dead_abnormal_edges;
3269 /* Set input_location here so we get the right instantiation context
3270 if we call instantiate_decl from inlinable_function_p. */
3271 saved_location = input_location;
3272 if (gimple_has_location (stmt))
3273 input_location = gimple_location (stmt);
3275 /* From here on, we're only interested in CALL_EXPRs. */
3276 if (gimple_code (stmt) != GIMPLE_CALL)
3279 /* First, see if we can figure out what function is being called.
3280 If we cannot, then there is no hope of inlining the function. */
3281 fn = gimple_call_fndecl (stmt);
3284 fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3289 /* Turn forward declarations into real ones. */
3290 fn = cgraph_node (fn)->decl;
3292 /* If FN is a declaration of a function in a nested scope that was
3293 globally declared inline, we don't set its DECL_INITIAL.
3294 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3295 C++ front-end uses it for cdtors to refer to their internal
3296 declarations, that are not real functions. Fortunately those
3297 don't have trees to be saved, so we can tell by checking their
3299 if (!DECL_INITIAL (fn)
3300 && DECL_ABSTRACT_ORIGIN (fn)
3301 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3302 fn = DECL_ABSTRACT_ORIGIN (fn);
3304 /* Objective C and fortran still calls tree_rest_of_compilation directly.
3305 Kill this check once this is fixed. */
3306 if (!id->dst_node->analyzed)
3309 cg_edge = cgraph_edge (id->dst_node, stmt);
3311 /* Don't try to inline functions that are not well-suited to
3313 if (!cgraph_inline_p (cg_edge, &reason))
3315 /* If this call was originally indirect, we do not want to emit any
3316 inlining related warnings or sorry messages because there are no
3317 guarantees regarding those. */
3318 if (cg_edge->indirect_call)
3321 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3322 /* Avoid warnings during early inline pass. */
3323 && cgraph_global_info_ready)
3325 sorry ("inlining failed in call to %q+F: %s", fn,
3326 cgraph_inline_failed_string (reason));
3327 sorry ("called from here");
3329 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3330 && !DECL_IN_SYSTEM_HEADER (fn)
3331 && reason != CIF_UNSPECIFIED
3332 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3333 /* Avoid warnings during early inline pass. */
3334 && cgraph_global_info_ready)
3336 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3337 fn, cgraph_inline_failed_string (reason));
3338 warning (OPT_Winline, "called from here");
3342 fn = cg_edge->callee->decl;
3344 #ifdef ENABLE_CHECKING
3345 if (cg_edge->callee->decl != id->dst_node->decl)
3346 verify_cgraph_node (cg_edge->callee);
3349 /* We will be inlining this callee. */
3350 id->eh_region = lookup_stmt_eh_region (stmt);
3352 /* Split the block holding the GIMPLE_CALL. */
3353 e = split_block (bb, stmt);
3355 return_block = e->dest;
3358 /* split_block splits after the statement; work around this by
3359 moving the call into the second block manually. Not pretty,
3360 but seems easier than doing the CFG manipulation by hand
3361 when the GIMPLE_CALL is in the last statement of BB. */
3362 stmt_gsi = gsi_last_bb (bb);
3363 gsi_remove (&stmt_gsi, false);
3365 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3366 been the source of abnormal edges. In this case, schedule
3367 the removal of dead abnormal edges. */
3368 gsi = gsi_start_bb (return_block);
3369 if (gsi_end_p (gsi))
3371 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3372 purge_dead_abnormal_edges = true;
3376 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3377 purge_dead_abnormal_edges = false;
3380 stmt_gsi = gsi_start_bb (return_block);
3382 /* Build a block containing code to initialize the arguments, the
3383 actual inline expansion of the body, and a label for the return
3384 statements within the function to jump to. The type of the
3385 statement expression is the return type of the function call. */
3386 id->block = make_node (BLOCK);
3387 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3388 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3389 prepend_lexical_block (gimple_block (stmt), id->block);
3391 /* Local declarations will be replaced by their equivalents in this
3394 id->decl_map = pointer_map_create ();
3396 /* Record the function we are about to inline. */
3398 id->src_node = cg_edge->callee;
3399 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3400 id->gimple_call = stmt;
3402 gcc_assert (!id->src_cfun->after_inlining);
3405 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3407 gimple_stmt_iterator si = gsi_last_bb (bb);
3408 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3412 initialize_inlined_parameters (id, stmt, fn, bb);
3414 if (DECL_INITIAL (fn))
3415 prepend_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3417 /* Return statements in the function body will be replaced by jumps
3418 to the RET_LABEL. */
3419 gcc_assert (DECL_INITIAL (fn));
3420 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3422 /* Find the LHS to which the result of this call is assigned. */
3424 if (gimple_call_lhs (stmt))
3426 modify_dest = gimple_call_lhs (stmt);
3428 /* The function which we are inlining might not return a value,
3429 in which case we should issue a warning that the function
3430 does not return a value. In that case the optimizers will
3431 see that the variable to which the value is assigned was not
3432 initialized. We do not want to issue a warning about that
3433 uninitialized variable. */
3434 if (DECL_P (modify_dest))
3435 TREE_NO_WARNING (modify_dest) = 1;
3437 if (gimple_call_return_slot_opt_p (stmt))
3439 return_slot = modify_dest;
3446 /* If we are inlining a call to the C++ operator new, we don't want
3447 to use type based alias analysis on the return value. Otherwise
3448 we may get confused if the compiler sees that the inlined new
3449 function returns a pointer which was just deleted. See bug
3451 if (DECL_IS_OPERATOR_NEW (fn))
3457 /* Declare the return variable for the function. */
3458 retvar = declare_return_variable (id, return_slot, modify_dest, &use_retvar);
3460 /* Add local vars in this inlined callee to caller. */
3461 t_step = id->src_cfun->local_decls;
3462 for (; t_step; t_step = TREE_CHAIN (t_step))
3464 var = TREE_VALUE (t_step);
3465 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3467 if (var_ann (var) && add_referenced_var (var))
3468 cfun->local_decls = tree_cons (NULL_TREE, var,
3471 else if (!can_be_nonlocal (var, id))
3472 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3476 /* This is it. Duplicate the callee body. Assume callee is
3477 pre-gimplified. Note that we must not alter the caller
3478 function in any way before this point, as this CALL_EXPR may be
3479 a self-referential call; if we're calling ourselves, we need to
3480 duplicate our body before altering anything. */
3481 copy_body (id, bb->count, bb->frequency, bb, return_block);
3483 /* Reset the escaped and callused solutions. */
3484 if (cfun->gimple_df)
3486 pt_solution_reset (&cfun->gimple_df->escaped);
3487 pt_solution_reset (&cfun->gimple_df->callused);
3491 pointer_map_destroy (id->decl_map);
3494 /* Unlink the calls virtual operands before replacing it. */
3495 unlink_stmt_vdef (stmt);
3497 /* If the inlined function returns a result that we care about,
3498 substitute the GIMPLE_CALL with an assignment of the return
3499 variable to the LHS of the call. That is, if STMT was
3500 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
3501 if (use_retvar && gimple_call_lhs (stmt))
3503 gimple old_stmt = stmt;
3504 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3505 gsi_replace (&stmt_gsi, stmt, false);
3506 if (gimple_in_ssa_p (cfun))
3507 mark_symbols_for_renaming (stmt);
3508 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3512 /* Handle the case of inlining a function with no return
3513 statement, which causes the return value to become undefined. */
3514 if (gimple_call_lhs (stmt)
3515 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3517 tree name = gimple_call_lhs (stmt);
3518 tree var = SSA_NAME_VAR (name);
3519 tree def = gimple_default_def (cfun, var);
3523 /* If the variable is used undefined, make this name
3524 undefined via a move. */
3525 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3526 gsi_replace (&stmt_gsi, stmt, true);
3530 /* Otherwise make this variable undefined. */
3531 gsi_remove (&stmt_gsi, true);
3532 set_default_def (var, name);
3533 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
3537 gsi_remove (&stmt_gsi, true);
3540 if (purge_dead_abnormal_edges)
3541 gimple_purge_dead_abnormal_call_edges (return_block);
3543 /* If the value of the new expression is ignored, that's OK. We
3544 don't warn about this for CALL_EXPRs, so we shouldn't warn about
3545 the equivalent inlined version either. */
3546 if (is_gimple_assign (stmt))
3548 gcc_assert (gimple_assign_single_p (stmt)
3549 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
3550 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
3553 /* Output the inlining info for this abstract function, since it has been
3554 inlined. If we don't do this now, we can lose the information about the
3555 variables in the function when the blocks get blown away as soon as we
3556 remove the cgraph node. */
3557 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
3559 /* Update callgraph if needed. */
3560 cgraph_remove_node (cg_edge->callee);
3562 id->block = NULL_TREE;
3563 successfully_inlined = TRUE;
3566 input_location = saved_location;
3567 return successfully_inlined;
3570 /* Expand call statements reachable from STMT_P.
3571 We can only have CALL_EXPRs as the "toplevel" tree code or nested
3572 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
3573 unfortunately not use that function here because we need a pointer
3574 to the CALL_EXPR, not the tree itself. */
3577 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
3579 gimple_stmt_iterator gsi;
3581 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3583 gimple stmt = gsi_stmt (gsi);
3585 if (is_gimple_call (stmt)
3586 && expand_call_inline (bb, stmt, id))
3594 /* Walk all basic blocks created after FIRST and try to fold every statement
3595 in the STATEMENTS pointer set. */
3598 fold_marked_statements (int first, struct pointer_set_t *statements)
3600 for (; first < n_basic_blocks; first++)
3601 if (BASIC_BLOCK (first))
3603 gimple_stmt_iterator gsi;
3605 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
3608 if (pointer_set_contains (statements, gsi_stmt (gsi)))
3610 gimple old_stmt = gsi_stmt (gsi);
3611 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
3613 if (fold_stmt (&gsi))
3615 /* Re-read the statement from GSI as fold_stmt() may
3617 gimple new_stmt = gsi_stmt (gsi);
3618 update_stmt (new_stmt);
3620 if (is_gimple_call (old_stmt)
3621 || is_gimple_call (new_stmt))
3622 cgraph_update_edges_for_call_stmt (old_stmt, old_decl, new_stmt);
3624 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3625 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3631 /* Return true if BB has at least one abnormal outgoing edge. */
3634 has_abnormal_outgoing_edge_p (basic_block bb)
3639 FOR_EACH_EDGE (e, ei, bb->succs)
3640 if (e->flags & EDGE_ABNORMAL)
3646 /* Expand calls to inline functions in the body of FN. */
3649 optimize_inline_calls (tree fn)
3654 int last = n_basic_blocks;
3655 struct gimplify_ctx gctx;
3657 /* There is no point in performing inlining if errors have already
3658 occurred -- and we might crash if we try to inline invalid
3660 if (errorcount || sorrycount)
3664 memset (&id, 0, sizeof (id));
3666 id.src_node = id.dst_node = cgraph_node (fn);
3668 /* Or any functions that aren't finished yet. */
3669 prev_fn = NULL_TREE;
3670 if (current_function_decl)
3672 id.dst_fn = current_function_decl;
3673 prev_fn = current_function_decl;
3676 id.copy_decl = copy_decl_maybe_to_var;
3677 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3678 id.transform_new_cfg = false;
3679 id.transform_return_to_modify = true;
3680 id.transform_lang_insert_block = NULL;
3681 id.statements_to_fold = pointer_set_create ();
3683 push_gimplify_context (&gctx);
3685 /* We make no attempts to keep dominance info up-to-date. */
3686 free_dominance_info (CDI_DOMINATORS);
3687 free_dominance_info (CDI_POST_DOMINATORS);
3689 /* Register specific gimple functions. */
3690 gimple_register_cfg_hooks ();
3692 /* Reach the trees by walking over the CFG, and note the
3693 enclosing basic-blocks in the call edges. */
3694 /* We walk the blocks going forward, because inlined function bodies
3695 will split id->current_basic_block, and the new blocks will
3696 follow it; we'll trudge through them, processing their CALL_EXPRs
3699 gimple_expand_calls_inline (bb, &id);
3701 pop_gimplify_context (NULL);
3703 #ifdef ENABLE_CHECKING
3705 struct cgraph_edge *e;
3707 verify_cgraph_node (id.dst_node);
3709 /* Double check that we inlined everything we are supposed to inline. */
3710 for (e = id.dst_node->callees; e; e = e->next_callee)
3711 gcc_assert (e->inline_failed);
3715 /* Fold the statements before compacting/renumbering the basic blocks. */
3716 fold_marked_statements (last, id.statements_to_fold);
3717 pointer_set_destroy (id.statements_to_fold);
3719 /* Renumber the (code) basic_blocks consecutively. */
3721 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3724 fold_cond_expr_cond ();
3725 delete_unreachable_blocks_update_callgraph (&id);
3726 #ifdef ENABLE_CHECKING
3727 verify_cgraph_node (id.dst_node);
3730 /* It would be nice to check SSA/CFG/statement consistency here, but it is
3731 not possible yet - the IPA passes might make various functions to not
3732 throw and they don't care to proactively update local EH info. This is
3733 done later in fixup_cfg pass that also execute the verification. */
3734 return (TODO_update_ssa
3736 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
3737 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
3740 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
3743 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3745 enum tree_code code = TREE_CODE (*tp);
3746 enum tree_code_class cl = TREE_CODE_CLASS (code);
3748 /* We make copies of most nodes. */
3749 if (IS_EXPR_CODE_CLASS (cl)
3750 || code == TREE_LIST
3752 || code == TYPE_DECL
3753 || code == OMP_CLAUSE)
3755 /* Because the chain gets clobbered when we make a copy, we save it
3757 tree chain = NULL_TREE, new_tree;
3759 chain = TREE_CHAIN (*tp);
3761 /* Copy the node. */
3762 new_tree = copy_node (*tp);
3764 /* Propagate mudflap marked-ness. */
3765 if (flag_mudflap && mf_marked_p (*tp))
3770 /* Now, restore the chain, if appropriate. That will cause
3771 walk_tree to walk into the chain as well. */
3772 if (code == PARM_DECL
3773 || code == TREE_LIST
3774 || code == OMP_CLAUSE)
3775 TREE_CHAIN (*tp) = chain;
3777 /* For now, we don't update BLOCKs when we make copies. So, we
3778 have to nullify all BIND_EXPRs. */
3779 if (TREE_CODE (*tp) == BIND_EXPR)
3780 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
3782 else if (code == CONSTRUCTOR)
3784 /* CONSTRUCTOR nodes need special handling because
3785 we need to duplicate the vector of elements. */
3788 new_tree = copy_node (*tp);
3790 /* Propagate mudflap marked-ness. */
3791 if (flag_mudflap && mf_marked_p (*tp))
3794 CONSTRUCTOR_ELTS (new_tree) = VEC_copy (constructor_elt, gc,
3795 CONSTRUCTOR_ELTS (*tp));
3798 else if (TREE_CODE_CLASS (code) == tcc_type)
3800 else if (TREE_CODE_CLASS (code) == tcc_declaration)
3802 else if (TREE_CODE_CLASS (code) == tcc_constant)
3805 gcc_assert (code != STATEMENT_LIST);
3809 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
3810 information indicating to what new SAVE_EXPR this one should be mapped,
3811 use that one. Otherwise, create a new node and enter it in ST. FN is
3812 the function into which the copy will be placed. */
3815 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
3817 struct pointer_map_t *st = (struct pointer_map_t *) st_;
3821 /* See if we already encountered this SAVE_EXPR. */
3822 n = (tree *) pointer_map_contains (st, *tp);
3824 /* If we didn't already remap this SAVE_EXPR, do so now. */
3827 t = copy_node (*tp);
3829 /* Remember this SAVE_EXPR. */
3830 *pointer_map_insert (st, *tp) = t;
3831 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3832 *pointer_map_insert (st, t) = t;
3836 /* We've already walked into this SAVE_EXPR; don't do it again. */
3841 /* Replace this SAVE_EXPR with the copy. */
3845 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
3846 copies the declaration and enters it in the splay_tree in DATA (which is
3847 really an `copy_body_data *'). */
3850 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3853 copy_body_data *id = (copy_body_data *) data;
3855 /* Don't walk into types. */
3859 else if (TREE_CODE (*tp) == LABEL_EXPR)
3861 tree decl = TREE_OPERAND (*tp, 0);
3863 /* Copy the decl and remember the copy. */
3864 insert_decl_map (id, decl, id->copy_decl (decl, id));
3870 /* Perform any modifications to EXPR required when it is unsaved. Does
3871 not recurse into EXPR's subtrees. */
3874 unsave_expr_1 (tree expr)
3876 switch (TREE_CODE (expr))
3879 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3880 It's OK for this to happen if it was part of a subtree that
3881 isn't immediately expanded, such as operand 2 of another
3883 if (TREE_OPERAND (expr, 1))
3886 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3887 TREE_OPERAND (expr, 3) = NULL_TREE;
3895 /* Called via walk_tree when an expression is unsaved. Using the
3896 splay_tree pointed to by ST (which is really a `splay_tree'),
3897 remaps all local declarations to appropriate replacements. */
3900 unsave_r (tree *tp, int *walk_subtrees, void *data)
3902 copy_body_data *id = (copy_body_data *) data;
3903 struct pointer_map_t *st = id->decl_map;
3906 /* Only a local declaration (variable or label). */
3907 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
3908 || TREE_CODE (*tp) == LABEL_DECL)
3910 /* Lookup the declaration. */
3911 n = (tree *) pointer_map_contains (st, *tp);
3913 /* If it's there, remap it. */
3918 else if (TREE_CODE (*tp) == STATEMENT_LIST)
3920 else if (TREE_CODE (*tp) == BIND_EXPR)
3921 copy_bind_expr (tp, walk_subtrees, id);
3922 else if (TREE_CODE (*tp) == SAVE_EXPR)
3923 remap_save_expr (tp, st, walk_subtrees);
3926 copy_tree_r (tp, walk_subtrees, NULL);
3928 /* Do whatever unsaving is required. */
3929 unsave_expr_1 (*tp);
3932 /* Keep iterating. */
3936 /* Copies everything in EXPR and replaces variables, labels
3937 and SAVE_EXPRs local to EXPR. */
3940 unsave_expr_now (tree expr)
3944 /* There's nothing to do for NULL_TREE. */
3949 memset (&id, 0, sizeof (id));
3950 id.src_fn = current_function_decl;
3951 id.dst_fn = current_function_decl;
3952 id.decl_map = pointer_map_create ();
3954 id.copy_decl = copy_decl_no_change;
3955 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3956 id.transform_new_cfg = false;
3957 id.transform_return_to_modify = false;
3958 id.transform_lang_insert_block = NULL;
3960 /* Walk the tree once to find local labels. */
3961 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
3963 /* Walk the tree again, copying, remapping, and unsaving. */
3964 walk_tree (&expr, unsave_r, &id, NULL);
3967 pointer_map_destroy (id.decl_map);
3972 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
3973 label, copies the declaration and enters it in the splay_tree in DATA (which
3974 is really a 'copy_body_data *'. */
3977 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
3978 bool *handled_ops_p ATTRIBUTE_UNUSED,
3979 struct walk_stmt_info *wi)
3981 copy_body_data *id = (copy_body_data *) wi->info;
3982 gimple stmt = gsi_stmt (*gsip);
3984 if (gimple_code (stmt) == GIMPLE_LABEL)
3986 tree decl = gimple_label_label (stmt);
3988 /* Copy the decl and remember the copy. */
3989 insert_decl_map (id, decl, id->copy_decl (decl, id));
3996 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3997 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3998 remaps all local declarations to appropriate replacements in gimple
4002 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
4004 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
4005 copy_body_data *id = (copy_body_data *) wi->info;
4006 struct pointer_map_t *st = id->decl_map;
4010 /* Only a local declaration (variable or label). */
4011 if ((TREE_CODE (expr) == VAR_DECL
4012 && !TREE_STATIC (expr))
4013 || TREE_CODE (expr) == LABEL_DECL)
4015 /* Lookup the declaration. */
4016 n = (tree *) pointer_map_contains (st, expr);
4018 /* If it's there, remap it. */
4023 else if (TREE_CODE (expr) == STATEMENT_LIST
4024 || TREE_CODE (expr) == BIND_EXPR
4025 || TREE_CODE (expr) == SAVE_EXPR)
4027 else if (TREE_CODE (expr) == TARGET_EXPR)
4029 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4030 It's OK for this to happen if it was part of a subtree that
4031 isn't immediately expanded, such as operand 2 of another
4033 if (!TREE_OPERAND (expr, 1))
4035 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4036 TREE_OPERAND (expr, 3) = NULL_TREE;
4040 /* Keep iterating. */
4045 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4046 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4047 remaps all local declarations to appropriate replacements in gimple
4051 replace_locals_stmt (gimple_stmt_iterator *gsip,
4052 bool *handled_ops_p ATTRIBUTE_UNUSED,
4053 struct walk_stmt_info *wi)
4055 copy_body_data *id = (copy_body_data *) wi->info;
4056 gimple stmt = gsi_stmt (*gsip);
4058 if (gimple_code (stmt) == GIMPLE_BIND)
4060 tree block = gimple_bind_block (stmt);
4064 remap_block (&block, id);
4065 gimple_bind_set_block (stmt, block);
4068 /* This will remap a lot of the same decls again, but this should be
4070 if (gimple_bind_vars (stmt))
4071 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), NULL, id));
4074 /* Keep iterating. */
4079 /* Copies everything in SEQ and replaces variables and labels local to
4080 current_function_decl. */
4083 copy_gimple_seq_and_replace_locals (gimple_seq seq)
4086 struct walk_stmt_info wi;
4087 struct pointer_set_t *visited;
4090 /* There's nothing to do for NULL_TREE. */
4095 memset (&id, 0, sizeof (id));
4096 id.src_fn = current_function_decl;
4097 id.dst_fn = current_function_decl;
4098 id.decl_map = pointer_map_create ();
4100 id.copy_decl = copy_decl_no_change;
4101 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4102 id.transform_new_cfg = false;
4103 id.transform_return_to_modify = false;
4104 id.transform_lang_insert_block = NULL;
4106 /* Walk the tree once to find local labels. */
4107 memset (&wi, 0, sizeof (wi));
4108 visited = pointer_set_create ();
4111 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
4112 pointer_set_destroy (visited);
4114 copy = gimple_seq_copy (seq);
4116 /* Walk the copy, remapping decls. */
4117 memset (&wi, 0, sizeof (wi));
4119 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4122 pointer_map_destroy (id.decl_map);
4128 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
4131 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
4140 debug_find_tree (tree top, tree search)
4142 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4146 /* Declare the variables created by the inliner. Add all the variables in
4147 VARS to BIND_EXPR. */
4150 declare_inline_vars (tree block, tree vars)
4153 for (t = vars; t; t = TREE_CHAIN (t))
4155 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4156 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
4157 cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
4161 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4164 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
4165 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
4166 VAR_DECL translation. */
4169 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
4171 /* Don't generate debug information for the copy if we wouldn't have
4172 generated it for the copy either. */
4173 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4174 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4176 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4177 declaration inspired this copy. */
4178 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4180 /* The new variable/label has no RTL, yet. */
4181 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4182 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4183 SET_DECL_RTL (copy, NULL_RTX);
4185 /* These args would always appear unused, if not for this. */
4186 TREE_USED (copy) = 1;
4188 /* Set the context for the new declaration. */
4189 if (!DECL_CONTEXT (decl))
4190 /* Globals stay global. */
4192 else if (DECL_CONTEXT (decl) != id->src_fn)
4193 /* Things that weren't in the scope of the function we're inlining
4194 from aren't in the scope we're inlining to, either. */
4196 else if (TREE_STATIC (decl))
4197 /* Function-scoped static variables should stay in the original
4201 /* Ordinary automatic local variables are now in the scope of the
4203 DECL_CONTEXT (copy) = id->dst_fn;
4209 copy_decl_to_var (tree decl, copy_body_data *id)
4213 gcc_assert (TREE_CODE (decl) == PARM_DECL
4214 || TREE_CODE (decl) == RESULT_DECL);
4216 type = TREE_TYPE (decl);
4218 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4219 VAR_DECL, DECL_NAME (decl), type);
4220 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4221 TREE_READONLY (copy) = TREE_READONLY (decl);
4222 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4223 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4225 return copy_decl_for_dup_finish (id, decl, copy);
4228 /* Like copy_decl_to_var, but create a return slot object instead of a
4229 pointer variable for return by invisible reference. */
4232 copy_result_decl_to_var (tree decl, copy_body_data *id)
4236 gcc_assert (TREE_CODE (decl) == PARM_DECL
4237 || TREE_CODE (decl) == RESULT_DECL);
4239 type = TREE_TYPE (decl);
4240 if (DECL_BY_REFERENCE (decl))
4241 type = TREE_TYPE (type);
4243 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4244 VAR_DECL, DECL_NAME (decl), type);
4245 TREE_READONLY (copy) = TREE_READONLY (decl);
4246 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4247 if (!DECL_BY_REFERENCE (decl))
4249 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4250 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4253 return copy_decl_for_dup_finish (id, decl, copy);
4257 copy_decl_no_change (tree decl, copy_body_data *id)
4261 copy = copy_node (decl);
4263 /* The COPY is not abstract; it will be generated in DST_FN. */
4264 DECL_ABSTRACT (copy) = 0;
4265 lang_hooks.dup_lang_specific_decl (copy);
4267 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4268 been taken; it's for internal bookkeeping in expand_goto_internal. */
4269 if (TREE_CODE (copy) == LABEL_DECL)
4271 TREE_ADDRESSABLE (copy) = 0;
4272 LABEL_DECL_UID (copy) = -1;
4275 return copy_decl_for_dup_finish (id, decl, copy);
4279 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4281 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4282 return copy_decl_to_var (decl, id);
4284 return copy_decl_no_change (decl, id);
4287 /* Return a copy of the function's argument tree. */
4289 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4290 bitmap args_to_skip, tree *vars)
4293 tree new_parm = NULL;
4298 for (arg = orig_parm; arg; arg = TREE_CHAIN (arg), i++)
4299 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4301 tree new_tree = remap_decl (arg, id);
4302 lang_hooks.dup_lang_specific_decl (new_tree);
4304 parg = &TREE_CHAIN (new_tree);
4306 else if (!pointer_map_contains (id->decl_map, arg))
4308 /* Make an equivalent VAR_DECL. If the argument was used
4309 as temporary variable later in function, the uses will be
4310 replaced by local variable. */
4311 tree var = copy_decl_to_var (arg, id);
4313 add_referenced_var (var);
4314 insert_decl_map (id, arg, var);
4315 /* Declare this new variable. */
4316 TREE_CHAIN (var) = *vars;
4322 /* Return a copy of the function's static chain. */
4324 copy_static_chain (tree static_chain, copy_body_data * id)
4326 tree *chain_copy, *pvar;
4328 chain_copy = &static_chain;
4329 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4331 tree new_tree = remap_decl (*pvar, id);
4332 lang_hooks.dup_lang_specific_decl (new_tree);
4333 TREE_CHAIN (new_tree) = TREE_CHAIN (*pvar);
4336 return static_chain;
4339 /* Return true if the function is allowed to be versioned.
4340 This is a guard for the versioning functionality. */
4343 tree_versionable_function_p (tree fndecl)
4345 return copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL;
4348 /* Delete all unreachable basic blocks and update callgraph.
4349 Doing so is somewhat nontrivial because we need to update all clones and
4350 remove inline function that become unreachable. */
4353 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
4355 bool changed = false;
4356 basic_block b, next_bb;
4358 find_unreachable_blocks ();
4360 /* Delete all unreachable basic blocks. */
4362 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
4364 next_bb = b->next_bb;
4366 if (!(b->flags & BB_REACHABLE))
4368 gimple_stmt_iterator bsi;
4370 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
4371 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL)
4373 struct cgraph_edge *e;
4374 struct cgraph_node *node;
4376 if ((e = cgraph_edge (id->dst_node, gsi_stmt (bsi))) != NULL)
4378 if (!e->inline_failed)
4379 cgraph_remove_node_and_inline_clones (e->callee);
4381 cgraph_remove_edge (e);
4383 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
4384 && id->dst_node->clones)
4385 for (node = id->dst_node->clones; node != id->dst_node;)
4387 if ((e = cgraph_edge (node, gsi_stmt (bsi))) != NULL)
4389 if (!e->inline_failed)
4390 cgraph_remove_node_and_inline_clones (e->callee);
4392 cgraph_remove_edge (e);
4396 node = node->clones;
4397 else if (node->next_sibling_clone)
4398 node = node->next_sibling_clone;
4401 while (node != id->dst_node && !node->next_sibling_clone)
4402 node = node->clone_of;
4403 if (node != id->dst_node)
4404 node = node->next_sibling_clone;
4408 delete_basic_block (b);
4414 tidy_fallthru_edges ();
4415 #ifdef ENABLE_CHECKING0
4416 verify_cgraph_node (id->dst_node);
4417 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
4418 && id->dst_node->clones)
4420 struct cgraph_node *node;
4421 for (node = id->dst_node->clones; node != id->dst_node;)
4423 verify_cgraph_node (node);
4426 node = node->clones;
4427 else if (node->next_sibling_clone)
4428 node = node->next_sibling_clone;
4431 while (node != id->dst_node && !node->next_sibling_clone)
4432 node = node->clone_of;
4433 if (node != id->dst_node)
4434 node = node->next_sibling_clone;
4442 /* Create a copy of a function's tree.
4443 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4444 of the original function and the new copied function
4445 respectively. In case we want to replace a DECL
4446 tree with another tree while duplicating the function's
4447 body, TREE_MAP represents the mapping between these
4448 trees. If UPDATE_CLONES is set, the call_stmt fields
4449 of edges of clones of the function will be updated. */
4451 tree_function_versioning (tree old_decl, tree new_decl,
4452 VEC(ipa_replace_map_p,gc)* tree_map,
4453 bool update_clones, bitmap args_to_skip)
4455 struct cgraph_node *old_version_node;
4456 struct cgraph_node *new_version_node;
4460 struct ipa_replace_map *replace_info;
4461 basic_block old_entry_block;
4462 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
4465 tree old_current_function_decl = current_function_decl;
4466 tree vars = NULL_TREE;
4468 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
4469 && TREE_CODE (new_decl) == FUNCTION_DECL);
4470 DECL_POSSIBLY_INLINED (old_decl) = 1;
4472 old_version_node = cgraph_node (old_decl);
4473 new_version_node = cgraph_node (new_decl);
4475 /* Output the inlining info for this abstract function, since it has been
4476 inlined. If we don't do this now, we can lose the information about the
4477 variables in the function when the blocks get blown away as soon as we
4478 remove the cgraph node. */
4479 (*debug_hooks->outlining_inline_function) (old_decl);
4481 DECL_ARTIFICIAL (new_decl) = 1;
4482 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4484 /* Prepare the data structures for the tree copy. */
4485 memset (&id, 0, sizeof (id));
4487 /* Generate a new name for the new version. */
4488 id.statements_to_fold = pointer_set_create ();
4490 id.decl_map = pointer_map_create ();
4491 id.src_fn = old_decl;
4492 id.dst_fn = new_decl;
4493 id.src_node = old_version_node;
4494 id.dst_node = new_version_node;
4495 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4497 id.copy_decl = copy_decl_no_change;
4498 id.transform_call_graph_edges
4499 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4500 id.transform_new_cfg = true;
4501 id.transform_return_to_modify = false;
4502 id.transform_lang_insert_block = NULL;
4504 current_function_decl = new_decl;
4505 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4506 (DECL_STRUCT_FUNCTION (old_decl));
4507 initialize_cfun (new_decl, old_decl,
4508 old_entry_block->count,
4509 old_entry_block->frequency);
4510 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4512 /* Copy the function's static chain. */
4513 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4515 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4516 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4519 /* If there's a tree_map, prepare for substitution. */
4521 for (i = 0; i < VEC_length (ipa_replace_map_p, tree_map); i++)
4524 replace_info = VEC_index (ipa_replace_map_p, tree_map, i);
4525 if (replace_info->replace_p)
4527 tree op = replace_info->new_tree;
4531 if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
4532 op = TREE_OPERAND (op, 0);
4534 if (TREE_CODE (op) == ADDR_EXPR)
4536 op = TREE_OPERAND (op, 0);
4537 while (handled_component_p (op))
4538 op = TREE_OPERAND (op, 0);
4539 if (TREE_CODE (op) == VAR_DECL)
4540 add_referenced_var (op);
4542 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
4543 init = setup_one_parameter (&id, replace_info->old_tree,
4544 replace_info->new_tree, id.src_fn,
4548 VEC_safe_push (gimple, heap, init_stmts, init);
4551 /* Copy the function's arguments. */
4552 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4553 DECL_ARGUMENTS (new_decl) =
4554 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
4555 args_to_skip, &vars);
4557 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4559 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4560 number_blocks (id.dst_fn);
4562 declare_inline_vars (DECL_INITIAL (new_decl), vars);
4564 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4565 /* Add local vars. */
4566 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4567 t_step; t_step = TREE_CHAIN (t_step))
4569 tree var = TREE_VALUE (t_step);
4570 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
4571 cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
4572 else if (!can_be_nonlocal (var, &id))
4574 tree_cons (NULL_TREE, remap_decl (var, &id),
4578 /* Copy the Function's body. */
4579 copy_body (&id, old_entry_block->count, old_entry_block->frequency,
4580 ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4582 if (DECL_RESULT (old_decl) != NULL_TREE)
4584 tree *res_decl = &DECL_RESULT (old_decl);
4585 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4586 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4589 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4590 number_blocks (new_decl);
4592 if (VEC_length (gimple, init_stmts))
4594 basic_block bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
4595 while (VEC_length (gimple, init_stmts))
4596 insert_init_stmt (bb, VEC_pop (gimple, init_stmts));
4599 /* Remap the nonlocal_goto_save_area, if any. */
4600 if (cfun->nonlocal_goto_save_area)
4602 struct walk_stmt_info wi;
4604 memset (&wi, 0, sizeof (wi));
4606 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
4610 pointer_map_destroy (id.decl_map);
4611 free_dominance_info (CDI_DOMINATORS);
4612 free_dominance_info (CDI_POST_DOMINATORS);
4614 fold_marked_statements (0, id.statements_to_fold);
4615 pointer_set_destroy (id.statements_to_fold);
4616 fold_cond_expr_cond ();
4617 delete_unreachable_blocks_update_callgraph (&id);
4618 update_ssa (TODO_update_ssa);
4619 free_dominance_info (CDI_DOMINATORS);
4620 free_dominance_info (CDI_POST_DOMINATORS);
4622 VEC_free (gimple, heap, init_stmts);
4624 current_function_decl = old_current_function_decl;
4625 gcc_assert (!current_function_decl
4626 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4630 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
4631 the callee and return the inlined body on success. */
4634 maybe_inline_call_in_expr (tree exp)
4636 tree fn = get_callee_fndecl (exp);
4638 /* We can only try to inline "const" functions. */
4639 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
4641 struct pointer_map_t *decl_map = pointer_map_create ();
4642 call_expr_arg_iterator iter;
4646 /* Remap the parameters. */
4647 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
4649 param = TREE_CHAIN (param), arg = next_call_expr_arg (&iter))
4650 *pointer_map_insert (decl_map, param) = arg;
4652 memset (&id, 0, sizeof (id));
4654 id.dst_fn = current_function_decl;
4655 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
4656 id.decl_map = decl_map;
4658 id.copy_decl = copy_decl_no_change;
4659 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4660 id.transform_new_cfg = false;
4661 id.transform_return_to_modify = true;
4662 id.transform_lang_insert_block = false;
4664 /* Make sure not to unshare trees behind the front-end's back
4665 since front-end specific mechanisms may rely on sharing. */
4666 id.regimplify = false;
4667 id.do_not_unshare = true;
4669 /* We're not inside any EH region. */
4672 t = copy_tree_body (&id);
4673 pointer_map_destroy (decl_map);
4675 /* We can only return something suitable for use in a GENERIC
4677 if (TREE_CODE (t) == MODIFY_EXPR)
4678 return TREE_OPERAND (t, 1);
4684 /* Duplicate a type, fields and all. */
4687 build_duplicate_type (tree type)
4689 struct copy_body_data id;
4691 memset (&id, 0, sizeof (id));
4692 id.src_fn = current_function_decl;
4693 id.dst_fn = current_function_decl;
4695 id.decl_map = pointer_map_create ();
4696 id.copy_decl = copy_decl_no_change;
4698 type = remap_type_1 (type, &id);
4700 pointer_map_destroy (id.decl_map);
4702 TYPE_CANONICAL (type) = type;
4707 /* Return whether it is safe to inline a function because it used different
4708 target specific options or different optimization options. */
4710 tree_can_inline_p (tree caller, tree callee)
4713 /* This causes a regression in SPEC in that it prevents a cold function from
4714 inlining a hot function. Perhaps this should only apply to functions
4715 that the user declares hot/cold/optimize explicitly. */
4717 /* Don't inline a function with a higher optimization level than the
4718 caller, or with different space constraints (hot/cold functions). */
4719 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
4720 tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
4722 if (caller_tree != callee_tree)
4724 struct cl_optimization *caller_opt
4725 = TREE_OPTIMIZATION ((caller_tree)
4727 : optimization_default_node);
4729 struct cl_optimization *callee_opt
4730 = TREE_OPTIMIZATION ((callee_tree)
4732 : optimization_default_node);
4734 if ((caller_opt->optimize > callee_opt->optimize)
4735 || (caller_opt->optimize_size != callee_opt->optimize_size))
4740 /* Allow the backend to decide if inlining is ok. */
4741 return targetm.target_option.can_inline_p (caller, callee);