2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
44 #include "tree-flow.h"
45 #include "diagnostic.h"
48 #include "pointer-set.h"
50 #include "value-prof.h"
51 #include "tree-pass.h"
53 #include "integrate.h"
55 /* I'm not real happy about this, but we need to handle gimple and
59 /* Inlining, Cloning, Versioning, Parallelization
61 Inlining: a function body is duplicated, but the PARM_DECLs are
62 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
63 MODIFY_EXPRs that store to a dedicated returned-value variable.
64 The duplicated eh_region info of the copy will later be appended
65 to the info for the caller; the eh_region info in copied throwing
66 statements and RESX statements are adjusted accordingly.
68 Cloning: (only in C++) We have one body for a con/de/structor, and
69 multiple function decls, each with a unique parameter list.
70 Duplicate the body, using the given splay tree; some parameters
71 will become constants (like 0 or 1).
73 Versioning: a function body is duplicated and the result is a new
74 function rather than into blocks of an existing function as with
75 inlining. Some parameters will become constants.
77 Parallelization: a region of a function is duplicated resulting in
78 a new function. Variables may be replaced with complex expressions
79 to enable shared variable semantics.
81 All of these will simultaneously lookup any callgraph edges. If
82 we're going to inline the duplicated function body, and the given
83 function has some cloned callgraph nodes (one for each place this
84 function will be inlined) those callgraph edges will be duplicated.
85 If we're cloning the body, those callgraph edges will be
86 updated to point into the new body. (Note that the original
87 callgraph node and edge list will not be altered.)
89 See the CALL_EXPR handling case in copy_tree_body_r (). */
93 o In order to make inlining-on-trees work, we pessimized
94 function-local static constants. In particular, they are now
95 always output, even when not addressed. Fix this by treating
96 function-local static constants just like global static
97 constants; the back-end already knows not to output them if they
100 o Provide heuristics to clamp inlining of recursive template
104 /* Weights that estimate_num_insns uses for heuristics in inlining. */
106 eni_weights eni_inlining_weights;
108 /* Weights that estimate_num_insns uses to estimate the size of the
111 eni_weights eni_size_weights;
113 /* Weights that estimate_num_insns uses to estimate the time necessary
114 to execute the produced code. */
116 eni_weights eni_time_weights;
120 static tree declare_return_variable (copy_body_data *, tree, tree);
121 static void remap_block (tree *, copy_body_data *);
122 static void copy_bind_expr (tree *, int *, copy_body_data *);
123 static tree mark_local_for_remap_r (tree *, int *, void *);
124 static void unsave_expr_1 (tree);
125 static tree unsave_r (tree *, int *, void *);
126 static void declare_inline_vars (tree, tree);
127 static void remap_save_expr (tree *, void *, int *);
128 static void prepend_lexical_block (tree current_block, tree new_block);
129 static tree copy_decl_to_var (tree, copy_body_data *);
130 static tree copy_result_decl_to_var (tree, copy_body_data *);
131 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
132 static gimple remap_gimple_stmt (gimple, copy_body_data *);
133 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
135 /* Insert a tree->tree mapping for ID. Despite the name suggests
136 that the trees should be variables, it is used for more than that. */
139 insert_decl_map (copy_body_data *id, tree key, tree value)
141 *pointer_map_insert (id->decl_map, key) = value;
143 /* Always insert an identity map as well. If we see this same new
144 node again, we won't want to duplicate it a second time. */
146 *pointer_map_insert (id->decl_map, value) = value;
149 /* Insert a tree->tree mapping for ID. This is only used for
153 insert_debug_decl_map (copy_body_data *id, tree key, tree value)
155 if (!gimple_in_ssa_p (id->src_cfun))
158 if (!MAY_HAVE_DEBUG_STMTS)
161 if (!target_for_debug_bind (key))
164 gcc_assert (TREE_CODE (key) == PARM_DECL);
165 gcc_assert (TREE_CODE (value) == VAR_DECL);
168 id->debug_map = pointer_map_create ();
170 *pointer_map_insert (id->debug_map, key) = value;
173 /* If nonzero, we're remapping the contents of inlined debug
174 statements. If negative, an error has occurred, such as a
175 reference to a variable that isn't available in the inlined
177 static int processing_debug_stmt = 0;
179 /* Construct new SSA name for old NAME. ID is the inline context. */
182 remap_ssa_name (tree name, copy_body_data *id)
187 gcc_assert (TREE_CODE (name) == SSA_NAME);
189 n = (tree *) pointer_map_contains (id->decl_map, name);
191 return unshare_expr (*n);
193 if (processing_debug_stmt)
195 processing_debug_stmt = -1;
199 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
201 new_tree = remap_decl (SSA_NAME_VAR (name), id);
203 /* We might've substituted constant or another SSA_NAME for
206 Replace the SSA name representing RESULT_DECL by variable during
207 inlining: this saves us from need to introduce PHI node in a case
208 return value is just partly initialized. */
209 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
210 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
211 || !id->transform_return_to_modify))
213 struct ptr_info_def *pi;
214 new_tree = make_ssa_name (new_tree, NULL);
215 insert_decl_map (id, name, new_tree);
216 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
217 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
218 TREE_TYPE (new_tree) = TREE_TYPE (SSA_NAME_VAR (new_tree));
219 /* At least IPA points-to info can be directly transferred. */
220 if (id->src_cfun->gimple_df
221 && id->src_cfun->gimple_df->ipa_pta
222 && (pi = SSA_NAME_PTR_INFO (name))
225 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
228 if (gimple_nop_p (SSA_NAME_DEF_STMT (name)))
230 /* By inlining function having uninitialized variable, we might
231 extend the lifetime (variable might get reused). This cause
232 ICE in the case we end up extending lifetime of SSA name across
233 abnormal edge, but also increase register pressure.
235 We simply initialize all uninitialized vars by 0 except
236 for case we are inlining to very first BB. We can avoid
237 this for all BBs that are not inside strongly connected
238 regions of the CFG, but this is expensive to test. */
240 && is_gimple_reg (SSA_NAME_VAR (name))
241 && TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL
242 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
243 || EDGE_COUNT (id->entry_bb->preds) != 1))
245 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
248 init_stmt = gimple_build_assign (new_tree,
249 fold_convert (TREE_TYPE (new_tree),
251 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
252 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
256 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
257 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name))
259 set_default_def (SSA_NAME_VAR (new_tree), new_tree);
264 insert_decl_map (id, name, new_tree);
268 /* Remap DECL during the copying of the BLOCK tree for the function. */
271 remap_decl (tree decl, copy_body_data *id)
275 /* We only remap local variables in the current function. */
277 /* See if we have remapped this declaration. */
279 n = (tree *) pointer_map_contains (id->decl_map, decl);
281 if (!n && processing_debug_stmt)
283 processing_debug_stmt = -1;
287 /* If we didn't already have an equivalent for this declaration,
291 /* Make a copy of the variable or label. */
292 tree t = id->copy_decl (decl, id);
294 /* Remember it, so that if we encounter this local entity again
295 we can reuse this copy. Do this early because remap_type may
296 need this decl for TYPE_STUB_DECL. */
297 insert_decl_map (id, decl, t);
302 /* Remap types, if necessary. */
303 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
304 if (TREE_CODE (t) == TYPE_DECL)
305 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
307 /* Remap sizes as necessary. */
308 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
309 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
311 /* If fields, do likewise for offset and qualifier. */
312 if (TREE_CODE (t) == FIELD_DECL)
314 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
315 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
316 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
319 if (cfun && gimple_in_ssa_p (cfun)
320 && (TREE_CODE (t) == VAR_DECL
321 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
324 add_referenced_var (t);
329 if (id->do_not_unshare)
332 return unshare_expr (*n);
336 remap_type_1 (tree type, copy_body_data *id)
340 /* We do need a copy. build and register it now. If this is a pointer or
341 reference type, remap the designated type and make a new pointer or
343 if (TREE_CODE (type) == POINTER_TYPE)
345 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
347 TYPE_REF_CAN_ALIAS_ALL (type));
348 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
349 new_tree = build_type_attribute_qual_variant (new_tree,
350 TYPE_ATTRIBUTES (type),
352 insert_decl_map (id, type, new_tree);
355 else if (TREE_CODE (type) == REFERENCE_TYPE)
357 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
359 TYPE_REF_CAN_ALIAS_ALL (type));
360 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
361 new_tree = build_type_attribute_qual_variant (new_tree,
362 TYPE_ATTRIBUTES (type),
364 insert_decl_map (id, type, new_tree);
368 new_tree = copy_node (type);
370 insert_decl_map (id, type, new_tree);
372 /* This is a new type, not a copy of an old type. Need to reassociate
373 variants. We can handle everything except the main variant lazily. */
374 t = TYPE_MAIN_VARIANT (type);
377 t = remap_type (t, id);
378 TYPE_MAIN_VARIANT (new_tree) = t;
379 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
380 TYPE_NEXT_VARIANT (t) = new_tree;
384 TYPE_MAIN_VARIANT (new_tree) = new_tree;
385 TYPE_NEXT_VARIANT (new_tree) = NULL;
388 if (TYPE_STUB_DECL (type))
389 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
391 /* Lazily create pointer and reference types. */
392 TYPE_POINTER_TO (new_tree) = NULL;
393 TYPE_REFERENCE_TO (new_tree) = NULL;
395 switch (TREE_CODE (new_tree))
399 case FIXED_POINT_TYPE:
402 t = TYPE_MIN_VALUE (new_tree);
403 if (t && TREE_CODE (t) != INTEGER_CST)
404 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
406 t = TYPE_MAX_VALUE (new_tree);
407 if (t && TREE_CODE (t) != INTEGER_CST)
408 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
412 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
413 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
417 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
418 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
423 case QUAL_UNION_TYPE:
427 for (f = TYPE_FIELDS (new_tree); f ; f = TREE_CHAIN (f))
429 t = remap_decl (f, id);
430 DECL_CONTEXT (t) = new_tree;
434 TYPE_FIELDS (new_tree) = nreverse (nf);
440 /* Shouldn't have been thought variable sized. */
444 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
445 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
451 remap_type (tree type, copy_body_data *id)
459 /* See if we have remapped this type. */
460 node = (tree *) pointer_map_contains (id->decl_map, type);
464 /* The type only needs remapping if it's variably modified. */
465 if (! variably_modified_type_p (type, id->src_fn))
467 insert_decl_map (id, type, type);
471 id->remapping_type_depth++;
472 tmp = remap_type_1 (type, id);
473 id->remapping_type_depth--;
478 /* Return previously remapped type of TYPE in ID. Return NULL if TYPE
479 is NULL or TYPE has not been remapped before. */
482 remapped_type (tree type, copy_body_data *id)
489 /* See if we have remapped this type. */
490 node = (tree *) pointer_map_contains (id->decl_map, type);
497 /* The type only needs remapping if it's variably modified. */
498 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
501 can_be_nonlocal (tree decl, copy_body_data *id)
503 /* We can not duplicate function decls. */
504 if (TREE_CODE (decl) == FUNCTION_DECL)
507 /* Local static vars must be non-local or we get multiple declaration
509 if (TREE_CODE (decl) == VAR_DECL
510 && !auto_var_in_fn_p (decl, id->src_fn))
513 /* At the moment dwarf2out can handle only these types of nodes. We
514 can support more later. */
515 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
518 /* We must use global type. We call remapped_type instead of
519 remap_type since we don't want to remap this type here if it
520 hasn't been remapped before. */
521 if (TREE_TYPE (decl) != remapped_type (TREE_TYPE (decl), id))
524 /* Wihtout SSA we can't tell if variable is used. */
525 if (!gimple_in_ssa_p (cfun))
528 /* Live variables must be copied so we can attach DECL_RTL. */
536 remap_decls (tree decls, VEC(tree,gc) **nonlocalized_list, copy_body_data *id)
539 tree new_decls = NULL_TREE;
541 /* Remap its variables. */
542 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
546 if (can_be_nonlocal (old_var, id))
548 if (TREE_CODE (old_var) == VAR_DECL
549 && ! DECL_EXTERNAL (old_var)
550 && (var_ann (old_var) || !gimple_in_ssa_p (cfun)))
551 cfun->local_decls = tree_cons (NULL_TREE, old_var,
553 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
554 && !DECL_IGNORED_P (old_var)
555 && nonlocalized_list)
556 VEC_safe_push (tree, gc, *nonlocalized_list, old_var);
560 /* Remap the variable. */
561 new_var = remap_decl (old_var, id);
563 /* If we didn't remap this variable, we can't mess with its
564 TREE_CHAIN. If we remapped this variable to the return slot, it's
565 already declared somewhere else, so don't declare it here. */
567 if (new_var == id->retvar)
571 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
572 && !DECL_IGNORED_P (old_var)
573 && nonlocalized_list)
574 VEC_safe_push (tree, gc, *nonlocalized_list, old_var);
578 gcc_assert (DECL_P (new_var));
579 TREE_CHAIN (new_var) = new_decls;
584 return nreverse (new_decls);
587 /* Copy the BLOCK to contain remapped versions of the variables
588 therein. And hook the new block into the block-tree. */
591 remap_block (tree *block, copy_body_data *id)
596 /* Make the new block. */
598 new_block = make_node (BLOCK);
599 TREE_USED (new_block) = TREE_USED (old_block);
600 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
601 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
602 BLOCK_NONLOCALIZED_VARS (new_block)
603 = VEC_copy (tree, gc, BLOCK_NONLOCALIZED_VARS (old_block));
606 /* Remap its variables. */
607 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
608 &BLOCK_NONLOCALIZED_VARS (new_block),
611 if (id->transform_lang_insert_block)
612 id->transform_lang_insert_block (new_block);
614 /* Remember the remapped block. */
615 insert_decl_map (id, old_block, new_block);
618 /* Copy the whole block tree and root it in id->block. */
620 remap_blocks (tree block, copy_body_data *id)
623 tree new_tree = block;
628 remap_block (&new_tree, id);
629 gcc_assert (new_tree != block);
630 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
631 prepend_lexical_block (new_tree, remap_blocks (t, id));
632 /* Blocks are in arbitrary order, but make things slightly prettier and do
633 not swap order when producing a copy. */
634 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
639 copy_statement_list (tree *tp)
641 tree_stmt_iterator oi, ni;
644 new_tree = alloc_stmt_list ();
645 ni = tsi_start (new_tree);
646 oi = tsi_start (*tp);
647 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
650 for (; !tsi_end_p (oi); tsi_next (&oi))
652 tree stmt = tsi_stmt (oi);
653 if (TREE_CODE (stmt) == STATEMENT_LIST)
654 copy_statement_list (&stmt);
655 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
660 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
662 tree block = BIND_EXPR_BLOCK (*tp);
663 /* Copy (and replace) the statement. */
664 copy_tree_r (tp, walk_subtrees, NULL);
667 remap_block (&block, id);
668 BIND_EXPR_BLOCK (*tp) = block;
671 if (BIND_EXPR_VARS (*tp))
672 /* This will remap a lot of the same decls again, but this should be
674 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
678 /* Create a new gimple_seq by remapping all the statements in BODY
679 using the inlining information in ID. */
682 remap_gimple_seq (gimple_seq body, copy_body_data *id)
684 gimple_stmt_iterator si;
685 gimple_seq new_body = NULL;
687 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
689 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
690 gimple_seq_add_stmt (&new_body, new_stmt);
697 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
698 block using the mapping information in ID. */
701 copy_gimple_bind (gimple stmt, copy_body_data *id)
704 tree new_block, new_vars;
705 gimple_seq body, new_body;
707 /* Copy the statement. Note that we purposely don't use copy_stmt
708 here because we need to remap statements as we copy. */
709 body = gimple_bind_body (stmt);
710 new_body = remap_gimple_seq (body, id);
712 new_block = gimple_bind_block (stmt);
714 remap_block (&new_block, id);
716 /* This will remap a lot of the same decls again, but this should be
718 new_vars = gimple_bind_vars (stmt);
720 new_vars = remap_decls (new_vars, NULL, id);
722 new_bind = gimple_build_bind (new_vars, new_body, new_block);
728 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
729 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
730 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
731 recursing into the children nodes of *TP. */
734 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
736 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
737 copy_body_data *id = (copy_body_data *) wi_p->info;
738 tree fn = id->src_fn;
740 if (TREE_CODE (*tp) == SSA_NAME)
742 *tp = remap_ssa_name (*tp, id);
746 else if (auto_var_in_fn_p (*tp, fn))
748 /* Local variables and labels need to be replaced by equivalent
749 variables. We don't want to copy static variables; there's
750 only one of those, no matter how many times we inline the
751 containing function. Similarly for globals from an outer
755 /* Remap the declaration. */
756 new_decl = remap_decl (*tp, id);
757 gcc_assert (new_decl);
758 /* Replace this variable with the copy. */
759 STRIP_TYPE_NOPS (new_decl);
760 /* ??? The C++ frontend uses void * pointer zero to initialize
761 any other type. This confuses the middle-end type verification.
762 As cloned bodies do not go through gimplification again the fixup
763 there doesn't trigger. */
764 if (TREE_CODE (new_decl) == INTEGER_CST
765 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
766 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
770 else if (TREE_CODE (*tp) == STATEMENT_LIST)
772 else if (TREE_CODE (*tp) == SAVE_EXPR)
774 else if (TREE_CODE (*tp) == LABEL_DECL
775 && (!DECL_CONTEXT (*tp)
776 || decl_function_context (*tp) == id->src_fn))
777 /* These may need to be remapped for EH handling. */
778 *tp = remap_decl (*tp, id);
779 else if (TYPE_P (*tp))
780 /* Types may need remapping as well. */
781 *tp = remap_type (*tp, id);
782 else if (CONSTANT_CLASS_P (*tp))
784 /* If this is a constant, we have to copy the node iff the type
785 will be remapped. copy_tree_r will not copy a constant. */
786 tree new_type = remap_type (TREE_TYPE (*tp), id);
788 if (new_type == TREE_TYPE (*tp))
791 else if (TREE_CODE (*tp) == INTEGER_CST)
792 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
793 TREE_INT_CST_HIGH (*tp));
796 *tp = copy_node (*tp);
797 TREE_TYPE (*tp) = new_type;
802 /* Otherwise, just copy the node. Note that copy_tree_r already
803 knows not to copy VAR_DECLs, etc., so this is safe. */
804 if (TREE_CODE (*tp) == INDIRECT_REF)
806 /* Get rid of *& from inline substitutions that can happen when a
807 pointer argument is an ADDR_EXPR. */
808 tree decl = TREE_OPERAND (*tp, 0);
811 n = (tree *) pointer_map_contains (id->decl_map, decl);
814 tree type, new_tree, old;
816 /* If we happen to get an ADDR_EXPR in n->value, strip
817 it manually here as we'll eventually get ADDR_EXPRs
818 which lie about their types pointed to. In this case
819 build_fold_indirect_ref wouldn't strip the
820 INDIRECT_REF, but we absolutely rely on that. As
821 fold_indirect_ref does other useful transformations,
822 try that first, though. */
823 type = TREE_TYPE (TREE_TYPE (*n));
824 new_tree = unshare_expr (*n);
826 *tp = gimple_fold_indirect_ref (new_tree);
829 if (TREE_CODE (new_tree) == ADDR_EXPR)
831 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
833 /* ??? We should either assert here or build
834 a VIEW_CONVERT_EXPR instead of blindly leaking
835 incompatible types to our IL. */
837 *tp = TREE_OPERAND (new_tree, 0);
841 *tp = build1 (INDIRECT_REF, type, new_tree);
842 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
843 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
851 /* Here is the "usual case". Copy this tree node, and then
852 tweak some special cases. */
853 copy_tree_r (tp, walk_subtrees, NULL);
855 /* Global variables we haven't seen yet need to go into referenced
856 vars. If not referenced from types only. */
857 if (gimple_in_ssa_p (cfun)
858 && TREE_CODE (*tp) == VAR_DECL
859 && id->remapping_type_depth == 0
860 && !processing_debug_stmt)
861 add_referenced_var (*tp);
863 /* We should never have TREE_BLOCK set on non-statements. */
865 gcc_assert (!TREE_BLOCK (*tp));
867 if (TREE_CODE (*tp) != OMP_CLAUSE)
868 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
870 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
872 /* The copied TARGET_EXPR has never been expanded, even if the
873 original node was expanded already. */
874 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
875 TREE_OPERAND (*tp, 3) = NULL_TREE;
877 else if (TREE_CODE (*tp) == ADDR_EXPR)
879 /* Variable substitution need not be simple. In particular,
880 the INDIRECT_REF substitution above. Make sure that
881 TREE_CONSTANT and friends are up-to-date. But make sure
882 to not improperly set TREE_BLOCK on some sub-expressions. */
883 int invariant = is_gimple_min_invariant (*tp);
884 tree block = id->block;
885 id->block = NULL_TREE;
886 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
889 /* Handle the case where we substituted an INDIRECT_REF
890 into the operand of the ADDR_EXPR. */
891 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
892 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
894 recompute_tree_invariant_for_addr_expr (*tp);
896 /* If this used to be invariant, but is not any longer,
897 then regimplification is probably needed. */
898 if (invariant && !is_gimple_min_invariant (*tp))
899 id->regimplify = true;
905 /* Keep iterating. */
910 /* Called from copy_body_id via walk_tree. DATA is really a
911 `copy_body_data *'. */
914 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
916 copy_body_data *id = (copy_body_data *) data;
917 tree fn = id->src_fn;
920 /* Begin by recognizing trees that we'll completely rewrite for the
921 inlining context. Our output for these trees is completely
922 different from out input (e.g. RETURN_EXPR is deleted, and morphs
923 into an edge). Further down, we'll handle trees that get
924 duplicated and/or tweaked. */
926 /* When requested, RETURN_EXPRs should be transformed to just the
927 contained MODIFY_EXPR. The branch semantics of the return will
928 be handled elsewhere by manipulating the CFG rather than a statement. */
929 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
931 tree assignment = TREE_OPERAND (*tp, 0);
933 /* If we're returning something, just turn that into an
934 assignment into the equivalent of the original RESULT_DECL.
935 If the "assignment" is just the result decl, the result
936 decl has already been set (e.g. a recent "foo (&result_decl,
937 ...)"); just toss the entire RETURN_EXPR. */
938 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
940 /* Replace the RETURN_EXPR with (a copy of) the
941 MODIFY_EXPR hanging underneath. */
942 *tp = copy_node (assignment);
944 else /* Else the RETURN_EXPR returns no value. */
947 return (tree) (void *)1;
950 else if (TREE_CODE (*tp) == SSA_NAME)
952 *tp = remap_ssa_name (*tp, id);
957 /* Local variables and labels need to be replaced by equivalent
958 variables. We don't want to copy static variables; there's only
959 one of those, no matter how many times we inline the containing
960 function. Similarly for globals from an outer function. */
961 else if (auto_var_in_fn_p (*tp, fn))
965 /* Remap the declaration. */
966 new_decl = remap_decl (*tp, id);
967 gcc_assert (new_decl);
968 /* Replace this variable with the copy. */
969 STRIP_TYPE_NOPS (new_decl);
973 else if (TREE_CODE (*tp) == STATEMENT_LIST)
974 copy_statement_list (tp);
975 else if (TREE_CODE (*tp) == SAVE_EXPR
976 || TREE_CODE (*tp) == TARGET_EXPR)
977 remap_save_expr (tp, id->decl_map, walk_subtrees);
978 else if (TREE_CODE (*tp) == LABEL_DECL
979 && (! DECL_CONTEXT (*tp)
980 || decl_function_context (*tp) == id->src_fn))
981 /* These may need to be remapped for EH handling. */
982 *tp = remap_decl (*tp, id);
983 else if (TREE_CODE (*tp) == BIND_EXPR)
984 copy_bind_expr (tp, walk_subtrees, id);
985 /* Types may need remapping as well. */
986 else if (TYPE_P (*tp))
987 *tp = remap_type (*tp, id);
989 /* If this is a constant, we have to copy the node iff the type will be
990 remapped. copy_tree_r will not copy a constant. */
991 else if (CONSTANT_CLASS_P (*tp))
993 tree new_type = remap_type (TREE_TYPE (*tp), id);
995 if (new_type == TREE_TYPE (*tp))
998 else if (TREE_CODE (*tp) == INTEGER_CST)
999 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
1000 TREE_INT_CST_HIGH (*tp));
1003 *tp = copy_node (*tp);
1004 TREE_TYPE (*tp) = new_type;
1008 /* Otherwise, just copy the node. Note that copy_tree_r already
1009 knows not to copy VAR_DECLs, etc., so this is safe. */
1012 /* Here we handle trees that are not completely rewritten.
1013 First we detect some inlining-induced bogosities for
1015 if (TREE_CODE (*tp) == MODIFY_EXPR
1016 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
1017 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
1019 /* Some assignments VAR = VAR; don't generate any rtl code
1020 and thus don't count as variable modification. Avoid
1021 keeping bogosities like 0 = 0. */
1022 tree decl = TREE_OPERAND (*tp, 0), value;
1025 n = (tree *) pointer_map_contains (id->decl_map, decl);
1029 STRIP_TYPE_NOPS (value);
1030 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1032 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
1033 return copy_tree_body_r (tp, walk_subtrees, data);
1037 else if (TREE_CODE (*tp) == INDIRECT_REF)
1039 /* Get rid of *& from inline substitutions that can happen when a
1040 pointer argument is an ADDR_EXPR. */
1041 tree decl = TREE_OPERAND (*tp, 0);
1044 n = (tree *) pointer_map_contains (id->decl_map, decl);
1049 /* If we happen to get an ADDR_EXPR in n->value, strip
1050 it manually here as we'll eventually get ADDR_EXPRs
1051 which lie about their types pointed to. In this case
1052 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1053 but we absolutely rely on that. As fold_indirect_ref
1054 does other useful transformations, try that first, though. */
1055 tree type = TREE_TYPE (TREE_TYPE (*n));
1056 if (id->do_not_unshare)
1059 new_tree = unshare_expr (*n);
1061 *tp = gimple_fold_indirect_ref (new_tree);
1064 if (TREE_CODE (new_tree) == ADDR_EXPR)
1066 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
1068 /* ??? We should either assert here or build
1069 a VIEW_CONVERT_EXPR instead of blindly leaking
1070 incompatible types to our IL. */
1072 *tp = TREE_OPERAND (new_tree, 0);
1076 *tp = build1 (INDIRECT_REF, type, new_tree);
1077 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1078 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1086 /* Here is the "usual case". Copy this tree node, and then
1087 tweak some special cases. */
1088 copy_tree_r (tp, walk_subtrees, NULL);
1090 /* Global variables we haven't seen yet needs to go into referenced
1091 vars. If not referenced from types or debug stmts only. */
1092 if (gimple_in_ssa_p (cfun)
1093 && TREE_CODE (*tp) == VAR_DECL
1094 && id->remapping_type_depth == 0
1095 && !processing_debug_stmt)
1096 add_referenced_var (*tp);
1098 /* If EXPR has block defined, map it to newly constructed block.
1099 When inlining we want EXPRs without block appear in the block
1100 of function call if we are not remapping a type. */
1103 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1104 if (TREE_BLOCK (*tp))
1107 n = (tree *) pointer_map_contains (id->decl_map,
1112 TREE_BLOCK (*tp) = new_block;
1115 if (TREE_CODE (*tp) != OMP_CLAUSE)
1116 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1118 /* The copied TARGET_EXPR has never been expanded, even if the
1119 original node was expanded already. */
1120 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1122 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1123 TREE_OPERAND (*tp, 3) = NULL_TREE;
1126 /* Variable substitution need not be simple. In particular, the
1127 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1128 and friends are up-to-date. */
1129 else if (TREE_CODE (*tp) == ADDR_EXPR)
1131 int invariant = is_gimple_min_invariant (*tp);
1132 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1134 /* Handle the case where we substituted an INDIRECT_REF
1135 into the operand of the ADDR_EXPR. */
1136 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1137 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1139 recompute_tree_invariant_for_addr_expr (*tp);
1141 /* If this used to be invariant, but is not any longer,
1142 then regimplification is probably needed. */
1143 if (invariant && !is_gimple_min_invariant (*tp))
1144 id->regimplify = true;
1150 /* Keep iterating. */
1154 /* Helper for remap_gimple_stmt. Given an EH region number for the
1155 source function, map that to the duplicate EH region number in
1156 the destination function. */
1159 remap_eh_region_nr (int old_nr, copy_body_data *id)
1161 eh_region old_r, new_r;
1164 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1165 slot = pointer_map_contains (id->eh_map, old_r);
1166 new_r = (eh_region) *slot;
1168 return new_r->index;
1171 /* Similar, but operate on INTEGER_CSTs. */
1174 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1178 old_nr = tree_low_cst (old_t_nr, 0);
1179 new_nr = remap_eh_region_nr (old_nr, id);
1181 return build_int_cst (NULL, new_nr);
1184 /* Helper for copy_bb. Remap statement STMT using the inlining
1185 information in ID. Return the new statement copy. */
1188 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1191 struct walk_stmt_info wi;
1193 bool skip_first = false;
1195 /* Begin by recognizing trees that we'll completely rewrite for the
1196 inlining context. Our output for these trees is completely
1197 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1198 into an edge). Further down, we'll handle trees that get
1199 duplicated and/or tweaked. */
1201 /* When requested, GIMPLE_RETURNs should be transformed to just the
1202 contained GIMPLE_ASSIGN. The branch semantics of the return will
1203 be handled elsewhere by manipulating the CFG rather than the
1205 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1207 tree retval = gimple_return_retval (stmt);
1209 /* If we're returning something, just turn that into an
1210 assignment into the equivalent of the original RESULT_DECL.
1211 If RETVAL is just the result decl, the result decl has
1212 already been set (e.g. a recent "foo (&result_decl, ...)");
1213 just toss the entire GIMPLE_RETURN. */
1214 if (retval && TREE_CODE (retval) != RESULT_DECL)
1216 copy = gimple_build_assign (id->retvar, retval);
1217 /* id->retvar is already substituted. Skip it on later remapping. */
1221 return gimple_build_nop ();
1223 else if (gimple_has_substatements (stmt))
1227 /* When cloning bodies from the C++ front end, we will be handed bodies
1228 in High GIMPLE form. Handle here all the High GIMPLE statements that
1229 have embedded statements. */
1230 switch (gimple_code (stmt))
1233 copy = copy_gimple_bind (stmt, id);
1237 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1238 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1241 case GIMPLE_EH_FILTER:
1242 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1243 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1247 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1248 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1249 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1252 case GIMPLE_WITH_CLEANUP_EXPR:
1253 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1254 copy = gimple_build_wce (s1);
1257 case GIMPLE_OMP_PARALLEL:
1258 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1259 copy = gimple_build_omp_parallel
1261 gimple_omp_parallel_clauses (stmt),
1262 gimple_omp_parallel_child_fn (stmt),
1263 gimple_omp_parallel_data_arg (stmt));
1266 case GIMPLE_OMP_TASK:
1267 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1268 copy = gimple_build_omp_task
1270 gimple_omp_task_clauses (stmt),
1271 gimple_omp_task_child_fn (stmt),
1272 gimple_omp_task_data_arg (stmt),
1273 gimple_omp_task_copy_fn (stmt),
1274 gimple_omp_task_arg_size (stmt),
1275 gimple_omp_task_arg_align (stmt));
1278 case GIMPLE_OMP_FOR:
1279 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1280 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1281 copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1282 gimple_omp_for_collapse (stmt), s2);
1285 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1287 gimple_omp_for_set_index (copy, i,
1288 gimple_omp_for_index (stmt, i));
1289 gimple_omp_for_set_initial (copy, i,
1290 gimple_omp_for_initial (stmt, i));
1291 gimple_omp_for_set_final (copy, i,
1292 gimple_omp_for_final (stmt, i));
1293 gimple_omp_for_set_incr (copy, i,
1294 gimple_omp_for_incr (stmt, i));
1295 gimple_omp_for_set_cond (copy, i,
1296 gimple_omp_for_cond (stmt, i));
1301 case GIMPLE_OMP_MASTER:
1302 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1303 copy = gimple_build_omp_master (s1);
1306 case GIMPLE_OMP_ORDERED:
1307 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1308 copy = gimple_build_omp_ordered (s1);
1311 case GIMPLE_OMP_SECTION:
1312 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1313 copy = gimple_build_omp_section (s1);
1316 case GIMPLE_OMP_SECTIONS:
1317 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1318 copy = gimple_build_omp_sections
1319 (s1, gimple_omp_sections_clauses (stmt));
1322 case GIMPLE_OMP_SINGLE:
1323 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1324 copy = gimple_build_omp_single
1325 (s1, gimple_omp_single_clauses (stmt));
1328 case GIMPLE_OMP_CRITICAL:
1329 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1331 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1340 if (gimple_assign_copy_p (stmt)
1341 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1342 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1344 /* Here we handle statements that are not completely rewritten.
1345 First we detect some inlining-induced bogosities for
1348 /* Some assignments VAR = VAR; don't generate any rtl code
1349 and thus don't count as variable modification. Avoid
1350 keeping bogosities like 0 = 0. */
1351 tree decl = gimple_assign_lhs (stmt), value;
1354 n = (tree *) pointer_map_contains (id->decl_map, decl);
1358 STRIP_TYPE_NOPS (value);
1359 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1360 return gimple_build_nop ();
1364 if (gimple_debug_bind_p (stmt))
1366 copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1367 gimple_debug_bind_get_value (stmt),
1369 VEC_safe_push (gimple, heap, id->debug_stmts, copy);
1373 /* Create a new deep copy of the statement. */
1374 copy = gimple_copy (stmt);
1376 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1377 RESX and EH_DISPATCH. */
1379 switch (gimple_code (copy))
1383 tree r, fndecl = gimple_call_fndecl (copy);
1384 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1385 switch (DECL_FUNCTION_CODE (fndecl))
1387 case BUILT_IN_EH_COPY_VALUES:
1388 r = gimple_call_arg (copy, 1);
1389 r = remap_eh_region_tree_nr (r, id);
1390 gimple_call_set_arg (copy, 1, r);
1393 case BUILT_IN_EH_POINTER:
1394 case BUILT_IN_EH_FILTER:
1395 r = gimple_call_arg (copy, 0);
1396 r = remap_eh_region_tree_nr (r, id);
1397 gimple_call_set_arg (copy, 0, r);
1404 /* Reset alias info if we didn't apply measures to
1405 keep it valid over inlining by setting DECL_PT_UID. */
1406 if (!id->src_cfun->gimple_df
1407 || !id->src_cfun->gimple_df->ipa_pta)
1408 gimple_call_reset_alias_info (copy);
1414 int r = gimple_resx_region (copy);
1415 r = remap_eh_region_nr (r, id);
1416 gimple_resx_set_region (copy, r);
1420 case GIMPLE_EH_DISPATCH:
1422 int r = gimple_eh_dispatch_region (copy);
1423 r = remap_eh_region_nr (r, id);
1424 gimple_eh_dispatch_set_region (copy, r);
1433 /* If STMT has a block defined, map it to the newly constructed
1434 block. When inlining we want statements without a block to
1435 appear in the block of the function call. */
1436 new_block = id->block;
1437 if (gimple_block (copy))
1440 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1445 gimple_set_block (copy, new_block);
1447 if (gimple_debug_bind_p (copy))
1450 /* Remap all the operands in COPY. */
1451 memset (&wi, 0, sizeof (wi));
1454 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1456 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1458 /* Clear the copied virtual operands. We are not remapping them here
1459 but are going to recreate them from scratch. */
1460 if (gimple_has_mem_ops (copy))
1462 gimple_set_vdef (copy, NULL_TREE);
1463 gimple_set_vuse (copy, NULL_TREE);
1470 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1474 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1475 gcov_type count_scale)
1477 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1478 basic_block copy_basic_block;
1482 /* create_basic_block() will append every new block to
1483 basic_block_info automatically. */
1484 copy_basic_block = create_basic_block (NULL, (void *) 0,
1485 (basic_block) bb->prev_bb->aux);
1486 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1488 /* We are going to rebuild frequencies from scratch. These values
1489 have just small importance to drive canonicalize_loop_headers. */
1490 freq = ((gcov_type)bb->frequency * frequency_scale / REG_BR_PROB_BASE);
1492 /* We recompute frequencies after inlining, so this is quite safe. */
1493 if (freq > BB_FREQ_MAX)
1495 copy_basic_block->frequency = freq;
1497 copy_gsi = gsi_start_bb (copy_basic_block);
1499 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1501 gimple stmt = gsi_stmt (gsi);
1502 gimple orig_stmt = stmt;
1504 id->regimplify = false;
1505 stmt = remap_gimple_stmt (stmt, id);
1506 if (gimple_nop_p (stmt))
1509 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1512 /* With return slot optimization we can end up with
1513 non-gimple (foo *)&this->m, fix that here. */
1514 if (is_gimple_assign (stmt)
1515 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1516 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1519 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1520 gimple_assign_rhs1 (stmt),
1521 true, NULL, false, GSI_NEW_STMT);
1522 gimple_assign_set_rhs1 (stmt, new_rhs);
1523 id->regimplify = false;
1526 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1529 gimple_regimplify_operands (stmt, &seq_gsi);
1531 /* If copy_basic_block has been empty at the start of this iteration,
1532 call gsi_start_bb again to get at the newly added statements. */
1533 if (gsi_end_p (copy_gsi))
1534 copy_gsi = gsi_start_bb (copy_basic_block);
1536 gsi_next (©_gsi);
1538 /* Process the new statement. The call to gimple_regimplify_operands
1539 possibly turned the statement into multiple statements, we
1540 need to process all of them. */
1545 stmt = gsi_stmt (copy_gsi);
1546 if (is_gimple_call (stmt)
1547 && gimple_call_va_arg_pack_p (stmt)
1550 /* __builtin_va_arg_pack () should be replaced by
1551 all arguments corresponding to ... in the caller. */
1554 VEC(tree, heap) *argarray;
1555 size_t nargs = gimple_call_num_args (id->gimple_call);
1558 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1561 /* Create the new array of arguments. */
1562 n = nargs + gimple_call_num_args (stmt);
1563 argarray = VEC_alloc (tree, heap, n);
1564 VEC_safe_grow (tree, heap, argarray, n);
1566 /* Copy all the arguments before '...' */
1567 memcpy (VEC_address (tree, argarray),
1568 gimple_call_arg_ptr (stmt, 0),
1569 gimple_call_num_args (stmt) * sizeof (tree));
1571 /* Append the arguments passed in '...' */
1572 memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1573 gimple_call_arg_ptr (id->gimple_call, 0)
1574 + (gimple_call_num_args (id->gimple_call) - nargs),
1575 nargs * sizeof (tree));
1577 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1580 VEC_free (tree, heap, argarray);
1582 /* Copy all GIMPLE_CALL flags, location and block, except
1583 GF_CALL_VA_ARG_PACK. */
1584 gimple_call_copy_flags (new_call, stmt);
1585 gimple_call_set_va_arg_pack (new_call, false);
1586 gimple_set_location (new_call, gimple_location (stmt));
1587 gimple_set_block (new_call, gimple_block (stmt));
1588 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1590 gsi_replace (©_gsi, new_call, false);
1591 gimple_set_bb (stmt, NULL);
1594 else if (is_gimple_call (stmt)
1596 && (decl = gimple_call_fndecl (stmt))
1597 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1598 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1600 /* __builtin_va_arg_pack_len () should be replaced by
1601 the number of anonymous arguments. */
1602 size_t nargs = gimple_call_num_args (id->gimple_call);
1606 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1609 count = build_int_cst (integer_type_node, nargs);
1610 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1611 gsi_replace (©_gsi, new_stmt, false);
1615 /* Statements produced by inlining can be unfolded, especially
1616 when we constant propagated some operands. We can't fold
1617 them right now for two reasons:
1618 1) folding require SSA_NAME_DEF_STMTs to be correct
1619 2) we can't change function calls to builtins.
1620 So we just mark statement for later folding. We mark
1621 all new statements, instead just statements that has changed
1622 by some nontrivial substitution so even statements made
1623 foldable indirectly are updated. If this turns out to be
1624 expensive, copy_body can be told to watch for nontrivial
1626 if (id->statements_to_fold)
1627 pointer_set_insert (id->statements_to_fold, stmt);
1629 /* We're duplicating a CALL_EXPR. Find any corresponding
1630 callgraph edges and update or duplicate them. */
1631 if (is_gimple_call (stmt))
1633 struct cgraph_edge *edge;
1636 switch (id->transform_call_graph_edges)
1638 case CB_CGE_DUPLICATE:
1639 edge = cgraph_edge (id->src_node, orig_stmt);
1642 int edge_freq = edge->frequency;
1643 edge = cgraph_clone_edge (edge, id->dst_node, stmt,
1645 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1646 edge->frequency, true);
1647 /* We could also just rescale the frequency, but
1648 doing so would introduce roundoff errors and make
1649 verifier unhappy. */
1651 = compute_call_stmt_bb_frequency (id->dst_node->decl,
1654 && profile_status_for_function (cfun) != PROFILE_ABSENT
1655 && (edge_freq > edge->frequency + 10
1656 || edge_freq < edge->frequency - 10))
1658 fprintf (dump_file, "Edge frequency estimated by "
1659 "cgraph %i diverge from inliner's estimate %i\n",
1663 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
1666 copy_basic_block->frequency);
1668 stmt = cgraph_redirect_edge_call_stmt_to_callee (edge);
1672 case CB_CGE_MOVE_CLONES:
1673 cgraph_set_call_stmt_including_clones (id->dst_node,
1675 edge = cgraph_edge (id->dst_node, stmt);
1679 edge = cgraph_edge (id->dst_node, orig_stmt);
1681 cgraph_set_call_stmt (edge, stmt);
1688 /* Constant propagation on argument done during inlining
1689 may create new direct call. Produce an edge for it. */
1691 || (edge->indirect_call
1692 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1693 && is_gimple_call (stmt)
1694 && (fn = gimple_call_fndecl (stmt)) != NULL)
1696 struct cgraph_node *dest = cgraph_node (fn);
1698 /* We have missing edge in the callgraph. This can happen
1699 when previous inlining turned an indirect call into a
1700 direct call by constant propagating arguments or we are
1701 producing dead clone (for further clonning). In all
1702 other cases we hit a bug (incorrect node sharing is the
1703 most common reason for missing edges). */
1704 gcc_assert (dest->needed || !dest->analyzed
1705 || !id->src_node->analyzed);
1706 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1707 cgraph_create_edge_including_clones
1708 (id->dst_node, dest, orig_stmt, stmt, bb->count,
1709 compute_call_stmt_bb_frequency (id->dst_node->decl,
1711 bb->loop_depth, CIF_ORIGINALLY_INDIRECT_CALL);
1713 cgraph_create_edge (id->dst_node, dest, stmt,
1715 compute_call_stmt_bb_frequency
1716 (id->dst_node->decl, copy_basic_block),
1717 bb->loop_depth)->inline_failed
1718 = CIF_ORIGINALLY_INDIRECT_CALL;
1721 fprintf (dump_file, "Created new direct edge to %s",
1722 cgraph_node_name (dest));
1726 flags = gimple_call_flags (stmt);
1727 if (flags & ECF_MAY_BE_ALLOCA)
1728 cfun->calls_alloca = true;
1729 if (flags & ECF_RETURNS_TWICE)
1730 cfun->calls_setjmp = true;
1733 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
1734 id->eh_map, id->eh_lp_nr);
1736 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
1741 find_new_referenced_vars (gsi_stmt (copy_gsi));
1742 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1743 if (TREE_CODE (def) == SSA_NAME)
1744 SSA_NAME_DEF_STMT (def) = stmt;
1747 gsi_next (©_gsi);
1749 while (!gsi_end_p (copy_gsi));
1751 copy_gsi = gsi_last_bb (copy_basic_block);
1754 return copy_basic_block;
1757 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1758 form is quite easy, since dominator relationship for old basic blocks does
1761 There is however exception where inlining might change dominator relation
1762 across EH edges from basic block within inlined functions destinating
1763 to landing pads in function we inline into.
1765 The function fills in PHI_RESULTs of such PHI nodes if they refer
1766 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1767 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1768 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1769 set, and this means that there will be no overlapping live ranges
1770 for the underlying symbol.
1772 This might change in future if we allow redirecting of EH edges and
1773 we might want to change way build CFG pre-inlining to include
1774 all the possible edges then. */
1776 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1777 bool can_throw, bool nonlocal_goto)
1782 FOR_EACH_EDGE (e, ei, bb->succs)
1784 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1787 gimple_stmt_iterator si;
1790 gcc_assert (e->flags & EDGE_EH);
1793 gcc_assert (!(e->flags & EDGE_EH));
1795 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1799 phi = gsi_stmt (si);
1801 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1802 gcc_assert (!e->dest->aux);
1804 gcc_assert ((e->flags & EDGE_EH)
1805 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1807 if (!is_gimple_reg (PHI_RESULT (phi)))
1809 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1813 re = find_edge (ret_bb, e->dest);
1815 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1816 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1818 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1819 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1825 /* Copy edges from BB into its copy constructed earlier, scale profile
1826 accordingly. Edges will be taken care of later. Assume aux
1827 pointers to point to the copies of each BB. */
1830 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1832 basic_block new_bb = (basic_block) bb->aux;
1835 gimple_stmt_iterator si;
1838 /* Use the indices from the original blocks to create edges for the
1840 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1841 if (!(old_edge->flags & EDGE_EH))
1845 flags = old_edge->flags;
1847 /* Return edges do get a FALLTHRU flag when the get inlined. */
1848 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1849 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1850 flags |= EDGE_FALLTHRU;
1851 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1852 new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1853 new_edge->probability = old_edge->probability;
1856 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1859 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1862 bool can_throw, nonlocal_goto;
1864 copy_stmt = gsi_stmt (si);
1865 if (!is_gimple_debug (copy_stmt))
1867 update_stmt (copy_stmt);
1868 if (gimple_in_ssa_p (cfun))
1869 mark_symbols_for_renaming (copy_stmt);
1872 /* Do this before the possible split_block. */
1875 /* If this tree could throw an exception, there are two
1876 cases where we need to add abnormal edge(s): the
1877 tree wasn't in a region and there is a "current
1878 region" in the caller; or the original tree had
1879 EH edges. In both cases split the block after the tree,
1880 and add abnormal edge(s) as needed; we need both
1881 those from the callee and the caller.
1882 We check whether the copy can throw, because the const
1883 propagation can change an INDIRECT_REF which throws
1884 into a COMPONENT_REF which doesn't. If the copy
1885 can throw, the original could also throw. */
1886 can_throw = stmt_can_throw_internal (copy_stmt);
1887 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1889 if (can_throw || nonlocal_goto)
1891 if (!gsi_end_p (si))
1892 /* Note that bb's predecessor edges aren't necessarily
1893 right at this point; split_block doesn't care. */
1895 edge e = split_block (new_bb, copy_stmt);
1898 new_bb->aux = e->src->aux;
1899 si = gsi_start_bb (new_bb);
1903 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
1904 make_eh_dispatch_edges (copy_stmt);
1906 make_eh_edges (copy_stmt);
1909 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1911 if ((can_throw || nonlocal_goto)
1912 && gimple_in_ssa_p (cfun))
1913 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1914 can_throw, nonlocal_goto);
1918 /* Copy the PHIs. All blocks and edges are copied, some blocks
1919 was possibly split and new outgoing EH edges inserted.
1920 BB points to the block of original function and AUX pointers links
1921 the original and newly copied blocks. */
1924 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1926 basic_block const new_bb = (basic_block) bb->aux;
1929 gimple_stmt_iterator si;
1931 for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1937 phi = gsi_stmt (si);
1938 res = PHI_RESULT (phi);
1940 if (is_gimple_reg (res))
1942 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1943 SSA_NAME_DEF_STMT (new_res)
1944 = new_phi = create_phi_node (new_res, new_bb);
1945 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1948 = find_edge ((basic_block) new_edge->src->aux, bb);
1949 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1951 tree block = id->block;
1952 id->block = NULL_TREE;
1953 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1955 gcc_assert (new_arg);
1956 /* With return slot optimization we can end up with
1957 non-gimple (foo *)&this->m, fix that here. */
1958 if (TREE_CODE (new_arg) != SSA_NAME
1959 && TREE_CODE (new_arg) != FUNCTION_DECL
1960 && !is_gimple_val (new_arg))
1962 gimple_seq stmts = NULL;
1963 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1964 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1966 add_phi_arg (new_phi, new_arg, new_edge,
1967 gimple_phi_arg_location_from_edge (phi, old_edge));
1974 /* Wrapper for remap_decl so it can be used as a callback. */
1977 remap_decl_1 (tree decl, void *data)
1979 return remap_decl (decl, (copy_body_data *) data);
1982 /* Build struct function and associated datastructures for the new clone
1983 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1986 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
1988 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1989 gcov_type count_scale;
1991 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1992 count_scale = (REG_BR_PROB_BASE * count
1993 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1995 count_scale = REG_BR_PROB_BASE;
1997 /* Register specific tree functions. */
1998 gimple_register_cfg_hooks ();
2000 /* Get clean struct function. */
2001 push_struct_function (new_fndecl);
2003 /* We will rebuild these, so just sanity check that they are empty. */
2004 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
2005 gcc_assert (cfun->local_decls == NULL);
2006 gcc_assert (cfun->cfg == NULL);
2007 gcc_assert (cfun->decl == new_fndecl);
2009 /* Copy items we preserve during clonning. */
2010 cfun->static_chain_decl = src_cfun->static_chain_decl;
2011 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
2012 cfun->function_end_locus = src_cfun->function_end_locus;
2013 cfun->curr_properties = src_cfun->curr_properties;
2014 cfun->last_verified = src_cfun->last_verified;
2015 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2016 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
2017 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2018 cfun->stdarg = src_cfun->stdarg;
2019 cfun->dont_save_pending_sizes_p = src_cfun->dont_save_pending_sizes_p;
2020 cfun->after_inlining = src_cfun->after_inlining;
2021 cfun->returns_struct = src_cfun->returns_struct;
2022 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
2023 cfun->after_tree_profile = src_cfun->after_tree_profile;
2025 init_empty_tree_cfg ();
2027 profile_status_for_function (cfun) = profile_status_for_function (src_cfun);
2028 ENTRY_BLOCK_PTR->count =
2029 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2031 ENTRY_BLOCK_PTR->frequency
2032 = ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2033 EXIT_BLOCK_PTR->count =
2034 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2036 EXIT_BLOCK_PTR->frequency =
2037 EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2039 init_eh_for_function ();
2041 if (src_cfun->gimple_df)
2043 init_tree_ssa (cfun);
2044 cfun->gimple_df->in_ssa_p = true;
2045 init_ssa_operands ();
2050 /* Make a copy of the body of FN so that it can be inserted inline in
2051 another function. Walks FN via CFG, returns new fndecl. */
2054 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
2055 basic_block entry_block_map, basic_block exit_block_map)
2057 tree callee_fndecl = id->src_fn;
2058 /* Original cfun for the callee, doesn't change. */
2059 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2060 struct function *cfun_to_copy;
2062 tree new_fndecl = NULL;
2063 gcov_type count_scale;
2066 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
2067 count_scale = (REG_BR_PROB_BASE * count
2068 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
2070 count_scale = REG_BR_PROB_BASE;
2072 /* Register specific tree functions. */
2073 gimple_register_cfg_hooks ();
2075 /* Must have a CFG here at this point. */
2076 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
2077 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2079 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2081 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
2082 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
2083 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2084 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2086 /* Duplicate any exception-handling regions. */
2088 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2091 /* Use aux pointers to map the original blocks to copy. */
2092 FOR_EACH_BB_FN (bb, cfun_to_copy)
2094 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2099 last = last_basic_block;
2101 /* Now that we've duplicated the blocks, duplicate their edges. */
2102 FOR_ALL_BB_FN (bb, cfun_to_copy)
2103 copy_edges_for_bb (bb, count_scale, exit_block_map);
2105 if (gimple_in_ssa_p (cfun))
2106 FOR_ALL_BB_FN (bb, cfun_to_copy)
2107 copy_phis_for_bb (bb, id);
2109 FOR_ALL_BB_FN (bb, cfun_to_copy)
2111 ((basic_block)bb->aux)->aux = NULL;
2115 /* Zero out AUX fields of newly created block during EH edge
2117 for (; last < last_basic_block; last++)
2118 BASIC_BLOCK (last)->aux = NULL;
2119 entry_block_map->aux = NULL;
2120 exit_block_map->aux = NULL;
2124 pointer_map_destroy (id->eh_map);
2131 /* Copy the debug STMT using ID. We deal with these statements in a
2132 special way: if any variable in their VALUE expression wasn't
2133 remapped yet, we won't remap it, because that would get decl uids
2134 out of sync, causing codegen differences between -g and -g0. If
2135 this arises, we drop the VALUE expression altogether. */
2138 copy_debug_stmt (gimple stmt, copy_body_data *id)
2141 struct walk_stmt_info wi;
2144 if (gimple_block (stmt))
2147 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (stmt));
2151 gimple_set_block (stmt, t);
2153 /* Remap all the operands in COPY. */
2154 memset (&wi, 0, sizeof (wi));
2157 processing_debug_stmt = 1;
2159 t = gimple_debug_bind_get_var (stmt);
2161 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2162 && (n = (tree *) pointer_map_contains (id->debug_map, t)))
2164 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2167 else if (TREE_CODE (t) == VAR_DECL
2169 && gimple_in_ssa_p (cfun)
2170 && !pointer_map_contains (id->decl_map, t)
2172 /* T is a non-localized variable. */;
2174 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2176 gimple_debug_bind_set_var (stmt, t);
2178 if (gimple_debug_bind_has_value_p (stmt))
2179 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2180 remap_gimple_op_r, &wi, NULL);
2182 /* Punt if any decl couldn't be remapped. */
2183 if (processing_debug_stmt < 0)
2184 gimple_debug_bind_reset_value (stmt);
2186 processing_debug_stmt = 0;
2189 if (gimple_in_ssa_p (cfun))
2190 mark_symbols_for_renaming (stmt);
2193 /* Process deferred debug stmts. In order to give values better odds
2194 of being successfully remapped, we delay the processing of debug
2195 stmts until all other stmts that might require remapping are
2199 copy_debug_stmts (copy_body_data *id)
2204 if (!id->debug_stmts)
2207 for (i = 0; VEC_iterate (gimple, id->debug_stmts, i, stmt); i++)
2208 copy_debug_stmt (stmt, id);
2210 VEC_free (gimple, heap, id->debug_stmts);
2213 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2214 another function. */
2217 copy_tree_body (copy_body_data *id)
2219 tree fndecl = id->src_fn;
2220 tree body = DECL_SAVED_TREE (fndecl);
2222 walk_tree (&body, copy_tree_body_r, id, NULL);
2227 /* Make a copy of the body of FN so that it can be inserted inline in
2228 another function. */
2231 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
2232 basic_block entry_block_map, basic_block exit_block_map)
2234 tree fndecl = id->src_fn;
2237 /* If this body has a CFG, walk CFG and copy. */
2238 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
2239 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map);
2240 copy_debug_stmts (id);
2245 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2246 defined in function FN, or of a data member thereof. */
2249 self_inlining_addr_expr (tree value, tree fn)
2253 if (TREE_CODE (value) != ADDR_EXPR)
2256 var = get_base_address (TREE_OPERAND (value, 0));
2258 return var && auto_var_in_fn_p (var, fn);
2261 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2262 lexical block and line number information from base_stmt, if given,
2263 or from the last stmt of the block otherwise. */
2266 insert_init_debug_bind (copy_body_data *id,
2267 basic_block bb, tree var, tree value,
2271 gimple_stmt_iterator gsi;
2274 if (!gimple_in_ssa_p (id->src_cfun))
2277 if (!MAY_HAVE_DEBUG_STMTS)
2280 tracked_var = target_for_debug_bind (var);
2286 gsi = gsi_last_bb (bb);
2287 if (!base_stmt && !gsi_end_p (gsi))
2288 base_stmt = gsi_stmt (gsi);
2291 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
2295 if (!gsi_end_p (gsi))
2296 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
2298 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
2305 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
2307 /* If VAR represents a zero-sized variable, it's possible that the
2308 assignment statement may result in no gimple statements. */
2311 gimple_stmt_iterator si = gsi_last_bb (bb);
2313 /* We can end up with init statements that store to a non-register
2314 from a rhs with a conversion. Handle that here by forcing the
2315 rhs into a temporary. gimple_regimplify_operands is not
2316 prepared to do this for us. */
2317 if (!is_gimple_debug (init_stmt)
2318 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
2319 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2320 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2322 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2323 gimple_expr_type (init_stmt),
2324 gimple_assign_rhs1 (init_stmt));
2325 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
2327 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
2328 gimple_assign_set_rhs1 (init_stmt, rhs);
2330 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2331 gimple_regimplify_operands (init_stmt, &si);
2332 mark_symbols_for_renaming (init_stmt);
2334 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2336 tree var, def = gimple_assign_lhs (init_stmt);
2338 if (TREE_CODE (def) == SSA_NAME)
2339 var = SSA_NAME_VAR (def);
2343 insert_init_debug_bind (id, bb, var, def, init_stmt);
2348 /* Initialize parameter P with VALUE. If needed, produce init statement
2349 at the end of BB. When BB is NULL, we return init statement to be
2352 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2353 basic_block bb, tree *vars)
2355 gimple init_stmt = NULL;
2358 tree def = (gimple_in_ssa_p (cfun)
2359 ? gimple_default_def (id->src_cfun, p) : NULL);
2362 && value != error_mark_node
2363 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
2365 if (fold_convertible_p (TREE_TYPE (p), value))
2366 rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
2368 /* ??? For valid (GIMPLE) programs we should not end up here.
2369 Still if something has gone wrong and we end up with truly
2370 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
2371 to not leak invalid GIMPLE to the following passes. */
2372 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2375 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2376 here since the type of this decl must be visible to the calling
2378 var = copy_decl_to_var (p, id);
2380 /* We're actually using the newly-created var. */
2381 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2384 add_referenced_var (var);
2387 /* Declare this new variable. */
2388 TREE_CHAIN (var) = *vars;
2391 /* Make gimplifier happy about this variable. */
2392 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2394 /* If the parameter is never assigned to, has no SSA_NAMEs created,
2395 we would not need to create a new variable here at all, if it
2396 weren't for debug info. Still, we can just use the argument
2398 if (TREE_READONLY (p)
2399 && !TREE_ADDRESSABLE (p)
2400 && value && !TREE_SIDE_EFFECTS (value)
2403 /* We may produce non-gimple trees by adding NOPs or introduce
2404 invalid sharing when operand is not really constant.
2405 It is not big deal to prohibit constant propagation here as
2406 we will constant propagate in DOM1 pass anyway. */
2407 if (is_gimple_min_invariant (value)
2408 && useless_type_conversion_p (TREE_TYPE (p),
2410 /* We have to be very careful about ADDR_EXPR. Make sure
2411 the base variable isn't a local variable of the inlined
2412 function, e.g., when doing recursive inlining, direct or
2413 mutually-recursive or whatever, which is why we don't
2414 just test whether fn == current_function_decl. */
2415 && ! self_inlining_addr_expr (value, fn))
2417 insert_decl_map (id, p, value);
2418 insert_debug_decl_map (id, p, var);
2419 return insert_init_debug_bind (id, bb, var, value, NULL);
2423 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2424 that way, when the PARM_DECL is encountered, it will be
2425 automatically replaced by the VAR_DECL. */
2426 insert_decl_map (id, p, var);
2428 /* Even if P was TREE_READONLY, the new VAR should not be.
2429 In the original code, we would have constructed a
2430 temporary, and then the function body would have never
2431 changed the value of P. However, now, we will be
2432 constructing VAR directly. The constructor body may
2433 change its value multiple times as it is being
2434 constructed. Therefore, it must not be TREE_READONLY;
2435 the back-end assumes that TREE_READONLY variable is
2436 assigned to only once. */
2437 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2438 TREE_READONLY (var) = 0;
2440 /* If there is no setup required and we are in SSA, take the easy route
2441 replacing all SSA names representing the function parameter by the
2442 SSA name passed to function.
2444 We need to construct map for the variable anyway as it might be used
2445 in different SSA names when parameter is set in function.
2447 Do replacement at -O0 for const arguments replaced by constant.
2448 This is important for builtin_constant_p and other construct requiring
2449 constant argument to be visible in inlined function body. */
2450 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2452 || (TREE_READONLY (p)
2453 && is_gimple_min_invariant (rhs)))
2454 && (TREE_CODE (rhs) == SSA_NAME
2455 || is_gimple_min_invariant (rhs))
2456 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2458 insert_decl_map (id, def, rhs);
2459 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2462 /* If the value of argument is never used, don't care about initializing
2464 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2466 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2467 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2470 /* Initialize this VAR_DECL from the equivalent argument. Convert
2471 the argument to the proper type in case it was promoted. */
2474 if (rhs == error_mark_node)
2476 insert_decl_map (id, p, var);
2477 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2480 STRIP_USELESS_TYPE_CONVERSION (rhs);
2482 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2483 keep our trees in gimple form. */
2484 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2486 def = remap_ssa_name (def, id);
2487 init_stmt = gimple_build_assign (def, rhs);
2488 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2489 set_default_def (var, NULL);
2492 init_stmt = gimple_build_assign (var, rhs);
2494 if (bb && init_stmt)
2495 insert_init_stmt (id, bb, init_stmt);
2500 /* Generate code to initialize the parameters of the function at the
2501 top of the stack in ID from the GIMPLE_CALL STMT. */
2504 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2505 tree fn, basic_block bb)
2510 tree vars = NULL_TREE;
2511 tree static_chain = gimple_call_chain (stmt);
2513 /* Figure out what the parameters are. */
2514 parms = DECL_ARGUMENTS (fn);
2516 /* Loop through the parameter declarations, replacing each with an
2517 equivalent VAR_DECL, appropriately initialized. */
2518 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2521 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2522 setup_one_parameter (id, p, val, fn, bb, &vars);
2525 /* Initialize the static chain. */
2526 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2527 gcc_assert (fn != current_function_decl);
2530 /* No static chain? Seems like a bug in tree-nested.c. */
2531 gcc_assert (static_chain);
2533 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2536 declare_inline_vars (id->block, vars);
2540 /* Declare a return variable to replace the RESULT_DECL for the
2541 function we are calling. An appropriate DECL_STMT is returned.
2542 The USE_STMT is filled to contain a use of the declaration to
2543 indicate the return value of the function.
2545 RETURN_SLOT, if non-null is place where to store the result. It
2546 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2547 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2549 The return value is a (possibly null) value that holds the result
2550 as seen by the caller. */
2553 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest)
2555 tree callee = id->src_fn;
2556 tree caller = id->dst_fn;
2557 tree result = DECL_RESULT (callee);
2558 tree callee_type = TREE_TYPE (result);
2562 /* Handle type-mismatches in the function declaration return type
2563 vs. the call expression. */
2565 caller_type = TREE_TYPE (modify_dest);
2567 caller_type = TREE_TYPE (TREE_TYPE (callee));
2569 /* We don't need to do anything for functions that don't return
2571 if (!result || VOID_TYPE_P (callee_type))
2574 /* If there was a return slot, then the return value is the
2575 dereferenced address of that object. */
2578 /* The front end shouldn't have used both return_slot and
2579 a modify expression. */
2580 gcc_assert (!modify_dest);
2581 if (DECL_BY_REFERENCE (result))
2583 tree return_slot_addr = build_fold_addr_expr (return_slot);
2584 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2586 /* We are going to construct *&return_slot and we can't do that
2587 for variables believed to be not addressable.
2589 FIXME: This check possibly can match, because values returned
2590 via return slot optimization are not believed to have address
2591 taken by alias analysis. */
2592 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2593 if (gimple_in_ssa_p (cfun))
2595 HOST_WIDE_INT bitsize;
2596 HOST_WIDE_INT bitpos;
2598 enum machine_mode mode;
2602 base = get_inner_reference (return_slot, &bitsize, &bitpos,
2604 &mode, &unsignedp, &volatilep,
2606 if (TREE_CODE (base) == INDIRECT_REF)
2607 base = TREE_OPERAND (base, 0);
2608 if (TREE_CODE (base) == SSA_NAME)
2609 base = SSA_NAME_VAR (base);
2610 mark_sym_for_renaming (base);
2612 var = return_slot_addr;
2617 gcc_assert (TREE_CODE (var) != SSA_NAME);
2618 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2620 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2621 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2622 && !DECL_GIMPLE_REG_P (result)
2624 DECL_GIMPLE_REG_P (var) = 0;
2629 /* All types requiring non-trivial constructors should have been handled. */
2630 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2632 /* Attempt to avoid creating a new temporary variable. */
2634 && TREE_CODE (modify_dest) != SSA_NAME)
2636 bool use_it = false;
2638 /* We can't use MODIFY_DEST if there's type promotion involved. */
2639 if (!useless_type_conversion_p (callee_type, caller_type))
2642 /* ??? If we're assigning to a variable sized type, then we must
2643 reuse the destination variable, because we've no good way to
2644 create variable sized temporaries at this point. */
2645 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2648 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2649 reuse it as the result of the call directly. Don't do this if
2650 it would promote MODIFY_DEST to addressable. */
2651 else if (TREE_ADDRESSABLE (result))
2655 tree base_m = get_base_address (modify_dest);
2657 /* If the base isn't a decl, then it's a pointer, and we don't
2658 know where that's going to go. */
2659 if (!DECL_P (base_m))
2661 else if (is_global_var (base_m))
2663 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2664 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2665 && !DECL_GIMPLE_REG_P (result)
2666 && DECL_GIMPLE_REG_P (base_m))
2668 else if (!TREE_ADDRESSABLE (base_m))
2680 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2682 var = copy_result_decl_to_var (result, id);
2683 if (gimple_in_ssa_p (cfun))
2686 add_referenced_var (var);
2689 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2690 DECL_STRUCT_FUNCTION (caller)->local_decls
2691 = tree_cons (NULL_TREE, var,
2692 DECL_STRUCT_FUNCTION (caller)->local_decls);
2694 /* Do not have the rest of GCC warn about this variable as it should
2695 not be visible to the user. */
2696 TREE_NO_WARNING (var) = 1;
2698 declare_inline_vars (id->block, var);
2700 /* Build the use expr. If the return type of the function was
2701 promoted, convert it back to the expected type. */
2703 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2704 use = fold_convert (caller_type, var);
2706 STRIP_USELESS_TYPE_CONVERSION (use);
2708 if (DECL_BY_REFERENCE (result))
2710 TREE_ADDRESSABLE (var) = 1;
2711 var = build_fold_addr_expr (var);
2715 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2716 way, when the RESULT_DECL is encountered, it will be
2717 automatically replaced by the VAR_DECL. */
2718 insert_decl_map (id, result, var);
2720 /* Remember this so we can ignore it in remap_decls. */
2726 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
2727 to a local label. */
2730 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
2733 tree fn = (tree) fnp;
2735 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2744 /* Determine if the function can be copied. If so return NULL. If
2745 not return a string describng the reason for failure. */
2748 copy_forbidden (struct function *fun, tree fndecl)
2750 const char *reason = fun->cannot_be_copied_reason;
2753 /* Only examine the function once. */
2754 if (fun->cannot_be_copied_set)
2757 /* We cannot copy a function that receives a non-local goto
2758 because we cannot remap the destination label used in the
2759 function that is performing the non-local goto. */
2760 /* ??? Actually, this should be possible, if we work at it.
2761 No doubt there's just a handful of places that simply
2762 assume it doesn't happen and don't substitute properly. */
2763 if (fun->has_nonlocal_label)
2765 reason = G_("function %q+F can never be copied "
2766 "because it receives a non-local goto");
2770 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2772 tree decl = TREE_VALUE (step);
2774 if (TREE_CODE (decl) == VAR_DECL
2775 && TREE_STATIC (decl)
2776 && !DECL_EXTERNAL (decl)
2777 && DECL_INITIAL (decl)
2778 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
2779 has_label_address_in_static_1,
2782 reason = G_("function %q+F can never be copied because it saves "
2783 "address of local label in a static variable");
2789 fun->cannot_be_copied_reason = reason;
2790 fun->cannot_be_copied_set = true;
2795 static const char *inline_forbidden_reason;
2797 /* A callback for walk_gimple_seq to handle statements. Returns non-null
2798 iff a function can not be inlined. Also sets the reason why. */
2801 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2802 struct walk_stmt_info *wip)
2804 tree fn = (tree) wip->info;
2806 gimple stmt = gsi_stmt (*gsi);
2808 switch (gimple_code (stmt))
2811 /* Refuse to inline alloca call unless user explicitly forced so as
2812 this may change program's memory overhead drastically when the
2813 function using alloca is called in loop. In GCC present in
2814 SPEC2000 inlining into schedule_block cause it to require 2GB of
2815 RAM instead of 256MB. */
2816 if (gimple_alloca_call_p (stmt)
2817 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2819 inline_forbidden_reason
2820 = G_("function %q+F can never be inlined because it uses "
2821 "alloca (override using the always_inline attribute)");
2822 *handled_ops_p = true;
2826 t = gimple_call_fndecl (stmt);
2830 /* We cannot inline functions that call setjmp. */
2831 if (setjmp_call_p (t))
2833 inline_forbidden_reason
2834 = G_("function %q+F can never be inlined because it uses setjmp");
2835 *handled_ops_p = true;
2839 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
2840 switch (DECL_FUNCTION_CODE (t))
2842 /* We cannot inline functions that take a variable number of
2844 case BUILT_IN_VA_START:
2845 case BUILT_IN_NEXT_ARG:
2846 case BUILT_IN_VA_END:
2847 inline_forbidden_reason
2848 = G_("function %q+F can never be inlined because it "
2849 "uses variable argument lists");
2850 *handled_ops_p = true;
2853 case BUILT_IN_LONGJMP:
2854 /* We can't inline functions that call __builtin_longjmp at
2855 all. The non-local goto machinery really requires the
2856 destination be in a different function. If we allow the
2857 function calling __builtin_longjmp to be inlined into the
2858 function calling __builtin_setjmp, Things will Go Awry. */
2859 inline_forbidden_reason
2860 = G_("function %q+F can never be inlined because "
2861 "it uses setjmp-longjmp exception handling");
2862 *handled_ops_p = true;
2865 case BUILT_IN_NONLOCAL_GOTO:
2867 inline_forbidden_reason
2868 = G_("function %q+F can never be inlined because "
2869 "it uses non-local goto");
2870 *handled_ops_p = true;
2873 case BUILT_IN_RETURN:
2874 case BUILT_IN_APPLY_ARGS:
2875 /* If a __builtin_apply_args caller would be inlined,
2876 it would be saving arguments of the function it has
2877 been inlined into. Similarly __builtin_return would
2878 return from the function the inline has been inlined into. */
2879 inline_forbidden_reason
2880 = G_("function %q+F can never be inlined because "
2881 "it uses __builtin_return or __builtin_apply_args");
2882 *handled_ops_p = true;
2891 t = gimple_goto_dest (stmt);
2893 /* We will not inline a function which uses computed goto. The
2894 addresses of its local labels, which may be tucked into
2895 global storage, are of course not constant across
2896 instantiations, which causes unexpected behavior. */
2897 if (TREE_CODE (t) != LABEL_DECL)
2899 inline_forbidden_reason
2900 = G_("function %q+F can never be inlined "
2901 "because it contains a computed goto");
2902 *handled_ops_p = true;
2911 *handled_ops_p = false;
2915 /* Return true if FNDECL is a function that cannot be inlined into
2919 inline_forbidden_p (tree fndecl)
2921 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2922 struct walk_stmt_info wi;
2923 struct pointer_set_t *visited_nodes;
2925 bool forbidden_p = false;
2927 /* First check for shared reasons not to copy the code. */
2928 inline_forbidden_reason = copy_forbidden (fun, fndecl);
2929 if (inline_forbidden_reason != NULL)
2932 /* Next, walk the statements of the function looking for
2933 constraucts we can't handle, or are non-optimal for inlining. */
2934 visited_nodes = pointer_set_create ();
2935 memset (&wi, 0, sizeof (wi));
2936 wi.info = (void *) fndecl;
2937 wi.pset = visited_nodes;
2939 FOR_EACH_BB_FN (bb, fun)
2942 gimple_seq seq = bb_seq (bb);
2943 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
2944 forbidden_p = (ret != NULL);
2949 pointer_set_destroy (visited_nodes);
2953 /* Returns nonzero if FN is a function that does not have any
2954 fundamental inline blocking properties. */
2957 tree_inlinable_function_p (tree fn)
2959 bool inlinable = true;
2963 /* If we've already decided this function shouldn't be inlined,
2964 there's no need to check again. */
2965 if (DECL_UNINLINABLE (fn))
2968 /* We only warn for functions declared `inline' by the user. */
2969 do_warning = (warn_inline
2970 && DECL_DECLARED_INLINE_P (fn)
2971 && !DECL_NO_INLINE_WARNING_P (fn)
2972 && !DECL_IN_SYSTEM_HEADER (fn));
2974 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
2977 && always_inline == NULL)
2980 warning (OPT_Winline, "function %q+F can never be inlined because it "
2981 "is suppressed using -fno-inline", fn);
2985 /* Don't auto-inline anything that might not be bound within
2986 this unit of translation. */
2987 else if (!DECL_DECLARED_INLINE_P (fn)
2988 && DECL_REPLACEABLE_P (fn))
2991 else if (!function_attribute_inlinable_p (fn))
2994 warning (OPT_Winline, "function %q+F can never be inlined because it "
2995 "uses attributes conflicting with inlining", fn);
2999 else if (inline_forbidden_p (fn))
3001 /* See if we should warn about uninlinable functions. Previously,
3002 some of these warnings would be issued while trying to expand
3003 the function inline, but that would cause multiple warnings
3004 about functions that would for example call alloca. But since
3005 this a property of the function, just one warning is enough.
3006 As a bonus we can now give more details about the reason why a
3007 function is not inlinable. */
3009 sorry (inline_forbidden_reason, fn);
3010 else if (do_warning)
3011 warning (OPT_Winline, inline_forbidden_reason, fn);
3016 /* Squirrel away the result so that we don't have to check again. */
3017 DECL_UNINLINABLE (fn) = !inlinable;
3022 /* Estimate the cost of a memory move. Use machine dependent
3023 word size and take possible memcpy call into account. */
3026 estimate_move_cost (tree type)
3030 gcc_assert (!VOID_TYPE_P (type));
3032 size = int_size_in_bytes (type);
3034 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
3035 /* Cost of a memcpy call, 3 arguments and the call. */
3038 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
3041 /* Returns cost of operation CODE, according to WEIGHTS */
3044 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3045 tree op1 ATTRIBUTE_UNUSED, tree op2)
3049 /* These are "free" conversions, or their presumed cost
3050 is folded into other operations. */
3057 /* Assign cost of 1 to usual operations.
3058 ??? We may consider mapping RTL costs to this. */
3063 case POINTER_PLUS_EXPR:
3067 case ADDR_SPACE_CONVERT_EXPR:
3068 case FIXED_CONVERT_EXPR:
3069 case FIX_TRUNC_EXPR:
3081 case VEC_LSHIFT_EXPR:
3082 case VEC_RSHIFT_EXPR:
3089 case TRUTH_ANDIF_EXPR:
3090 case TRUTH_ORIF_EXPR:
3091 case TRUTH_AND_EXPR:
3093 case TRUTH_XOR_EXPR:
3094 case TRUTH_NOT_EXPR:
3103 case UNORDERED_EXPR:
3114 case PREDECREMENT_EXPR:
3115 case PREINCREMENT_EXPR:
3116 case POSTDECREMENT_EXPR:
3117 case POSTINCREMENT_EXPR:
3119 case REALIGN_LOAD_EXPR:
3121 case REDUC_MAX_EXPR:
3122 case REDUC_MIN_EXPR:
3123 case REDUC_PLUS_EXPR:
3124 case WIDEN_SUM_EXPR:
3125 case WIDEN_MULT_EXPR:
3128 case VEC_WIDEN_MULT_HI_EXPR:
3129 case VEC_WIDEN_MULT_LO_EXPR:
3130 case VEC_UNPACK_HI_EXPR:
3131 case VEC_UNPACK_LO_EXPR:
3132 case VEC_UNPACK_FLOAT_HI_EXPR:
3133 case VEC_UNPACK_FLOAT_LO_EXPR:
3134 case VEC_PACK_TRUNC_EXPR:
3135 case VEC_PACK_SAT_EXPR:
3136 case VEC_PACK_FIX_TRUNC_EXPR:
3137 case VEC_EXTRACT_EVEN_EXPR:
3138 case VEC_EXTRACT_ODD_EXPR:
3139 case VEC_INTERLEAVE_HIGH_EXPR:
3140 case VEC_INTERLEAVE_LOW_EXPR:
3144 /* Few special cases of expensive operations. This is useful
3145 to avoid inlining on functions having too many of these. */
3146 case TRUNC_DIV_EXPR:
3148 case FLOOR_DIV_EXPR:
3149 case ROUND_DIV_EXPR:
3150 case EXACT_DIV_EXPR:
3151 case TRUNC_MOD_EXPR:
3153 case FLOOR_MOD_EXPR:
3154 case ROUND_MOD_EXPR:
3156 if (TREE_CODE (op2) != INTEGER_CST)
3157 return weights->div_mod_cost;
3161 /* We expect a copy assignment with no operator. */
3162 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
3168 /* Estimate number of instructions that will be created by expanding
3169 the statements in the statement sequence STMTS.
3170 WEIGHTS contains weights attributed to various constructs. */
3173 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
3176 gimple_stmt_iterator gsi;
3179 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
3180 cost += estimate_num_insns (gsi_stmt (gsi), weights);
3186 /* Estimate number of instructions that will be created by expanding STMT.
3187 WEIGHTS contains weights attributed to various constructs. */
3190 estimate_num_insns (gimple stmt, eni_weights *weights)
3193 enum gimple_code code = gimple_code (stmt);
3200 /* Try to estimate the cost of assignments. We have three cases to
3202 1) Simple assignments to registers;
3203 2) Stores to things that must live in memory. This includes
3204 "normal" stores to scalars, but also assignments of large
3205 structures, or constructors of big arrays;
3207 Let us look at the first two cases, assuming we have "a = b + C":
3208 <GIMPLE_ASSIGN <var_decl "a">
3209 <plus_expr <var_decl "b"> <constant C>>
3210 If "a" is a GIMPLE register, the assignment to it is free on almost
3211 any target, because "a" usually ends up in a real register. Hence
3212 the only cost of this expression comes from the PLUS_EXPR, and we
3213 can ignore the GIMPLE_ASSIGN.
3214 If "a" is not a GIMPLE register, the assignment to "a" will most
3215 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
3216 of moving something into "a", which we compute using the function
3217 estimate_move_cost. */
3218 lhs = gimple_assign_lhs (stmt);
3219 rhs = gimple_assign_rhs1 (stmt);
3221 if (is_gimple_reg (lhs))
3224 cost = estimate_move_cost (TREE_TYPE (lhs));
3226 if (!is_gimple_reg (rhs) && !is_gimple_min_invariant (rhs))
3227 cost += estimate_move_cost (TREE_TYPE (rhs));
3229 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3230 gimple_assign_rhs1 (stmt),
3231 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3232 == GIMPLE_BINARY_RHS
3233 ? gimple_assign_rhs2 (stmt) : NULL);
3237 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3238 gimple_op (stmt, 0),
3239 gimple_op (stmt, 1));
3243 /* Take into account cost of the switch + guess 2 conditional jumps for
3246 TODO: once the switch expansion logic is sufficiently separated, we can
3247 do better job on estimating cost of the switch. */
3248 if (weights->time_based)
3249 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3251 cost = gimple_switch_num_labels (stmt) * 2;
3256 tree decl = gimple_call_fndecl (stmt);
3257 tree addr = gimple_call_fn (stmt);
3258 tree funtype = TREE_TYPE (addr);
3260 if (POINTER_TYPE_P (funtype))
3261 funtype = TREE_TYPE (funtype);
3263 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
3264 cost = weights->target_builtin_call_cost;
3266 cost = weights->call_cost;
3268 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3269 switch (DECL_FUNCTION_CODE (decl))
3271 case BUILT_IN_CONSTANT_P:
3273 case BUILT_IN_EXPECT:
3276 /* Prefetch instruction is not expensive. */
3277 case BUILT_IN_PREFETCH:
3278 cost = weights->target_builtin_call_cost;
3281 /* Exception state returns or moves registers around. */
3282 case BUILT_IN_EH_FILTER:
3283 case BUILT_IN_EH_POINTER:
3284 case BUILT_IN_EH_COPY_VALUES:
3292 funtype = TREE_TYPE (decl);
3294 if (!VOID_TYPE_P (TREE_TYPE (funtype)))
3295 cost += estimate_move_cost (TREE_TYPE (funtype));
3296 /* Our cost must be kept in sync with
3297 cgraph_estimate_size_after_inlining that does use function
3298 declaration to figure out the arguments. */
3299 if (decl && DECL_ARGUMENTS (decl))
3302 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
3303 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3304 cost += estimate_move_cost (TREE_TYPE (arg));
3306 else if (funtype && prototype_p (funtype))
3309 for (t = TYPE_ARG_TYPES (funtype); t && t != void_list_node;
3311 if (!VOID_TYPE_P (TREE_VALUE (t)))
3312 cost += estimate_move_cost (TREE_VALUE (t));
3316 for (i = 0; i < gimple_call_num_args (stmt); i++)
3318 tree arg = gimple_call_arg (stmt, i);
3319 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3320 cost += estimate_move_cost (TREE_TYPE (arg));
3332 case GIMPLE_PREDICT:
3337 return asm_str_count (gimple_asm_string (stmt));
3340 /* This is either going to be an external function call with one
3341 argument, or two register copy statements plus a goto. */
3344 case GIMPLE_EH_DISPATCH:
3345 /* ??? This is going to turn into a switch statement. Ideally
3346 we'd have a look at the eh region and estimate the number of
3351 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
3353 case GIMPLE_EH_FILTER:
3354 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
3357 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
3360 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
3361 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
3363 /* OpenMP directives are generally very expensive. */
3365 case GIMPLE_OMP_RETURN:
3366 case GIMPLE_OMP_SECTIONS_SWITCH:
3367 case GIMPLE_OMP_ATOMIC_STORE:
3368 case GIMPLE_OMP_CONTINUE:
3369 /* ...except these, which are cheap. */
3372 case GIMPLE_OMP_ATOMIC_LOAD:
3373 return weights->omp_cost;
3375 case GIMPLE_OMP_FOR:
3376 return (weights->omp_cost
3377 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
3378 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
3380 case GIMPLE_OMP_PARALLEL:
3381 case GIMPLE_OMP_TASK:
3382 case GIMPLE_OMP_CRITICAL:
3383 case GIMPLE_OMP_MASTER:
3384 case GIMPLE_OMP_ORDERED:
3385 case GIMPLE_OMP_SECTION:
3386 case GIMPLE_OMP_SECTIONS:
3387 case GIMPLE_OMP_SINGLE:
3388 return (weights->omp_cost
3389 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
3398 /* Estimate number of instructions that will be created by expanding
3399 function FNDECL. WEIGHTS contains weights attributed to various
3403 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
3405 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
3406 gimple_stmt_iterator bsi;
3410 gcc_assert (my_function && my_function->cfg);
3411 FOR_EACH_BB_FN (bb, my_function)
3413 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3414 n += estimate_num_insns (gsi_stmt (bsi), weights);
3421 /* Initializes weights used by estimate_num_insns. */
3424 init_inline_once (void)
3426 eni_size_weights.call_cost = 1;
3427 eni_size_weights.target_builtin_call_cost = 1;
3428 eni_size_weights.div_mod_cost = 1;
3429 eni_size_weights.omp_cost = 40;
3430 eni_size_weights.time_based = false;
3432 /* Estimating time for call is difficult, since we have no idea what the
3433 called function does. In the current uses of eni_time_weights,
3434 underestimating the cost does less harm than overestimating it, so
3435 we choose a rather small value here. */
3436 eni_time_weights.call_cost = 10;
3437 eni_time_weights.target_builtin_call_cost = 10;
3438 eni_time_weights.div_mod_cost = 10;
3439 eni_time_weights.omp_cost = 40;
3440 eni_time_weights.time_based = true;
3443 /* Estimate the number of instructions in a gimple_seq. */
3446 count_insns_seq (gimple_seq seq, eni_weights *weights)
3448 gimple_stmt_iterator gsi;
3450 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3451 n += estimate_num_insns (gsi_stmt (gsi), weights);
3457 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3460 prepend_lexical_block (tree current_block, tree new_block)
3462 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3463 BLOCK_SUBBLOCKS (current_block) = new_block;
3464 BLOCK_SUPERCONTEXT (new_block) = current_block;
3467 /* Fetch callee declaration from the call graph edge going from NODE and
3468 associated with STMR call statement. Return NULL_TREE if not found. */
3470 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3472 struct cgraph_edge *cs;
3474 cs = cgraph_edge (node, stmt);
3476 return cs->callee->decl;
3481 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3484 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3488 struct pointer_map_t *st, *dst;
3491 location_t saved_location;
3492 struct cgraph_edge *cg_edge;
3493 cgraph_inline_failed_t reason;
3494 basic_block return_block;
3496 gimple_stmt_iterator gsi, stmt_gsi;
3497 bool successfully_inlined = FALSE;
3498 bool purge_dead_abnormal_edges;
3502 /* Set input_location here so we get the right instantiation context
3503 if we call instantiate_decl from inlinable_function_p. */
3504 saved_location = input_location;
3505 if (gimple_has_location (stmt))
3506 input_location = gimple_location (stmt);
3508 /* From here on, we're only interested in CALL_EXPRs. */
3509 if (gimple_code (stmt) != GIMPLE_CALL)
3512 /* First, see if we can figure out what function is being called.
3513 If we cannot, then there is no hope of inlining the function. */
3514 fn = gimple_call_fndecl (stmt);
3517 fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3522 /* Turn forward declarations into real ones. */
3523 fn = cgraph_node (fn)->decl;
3525 /* If FN is a declaration of a function in a nested scope that was
3526 globally declared inline, we don't set its DECL_INITIAL.
3527 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3528 C++ front-end uses it for cdtors to refer to their internal
3529 declarations, that are not real functions. Fortunately those
3530 don't have trees to be saved, so we can tell by checking their
3532 if (!DECL_INITIAL (fn)
3533 && DECL_ABSTRACT_ORIGIN (fn)
3534 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3535 fn = DECL_ABSTRACT_ORIGIN (fn);
3537 /* Objective C and fortran still calls tree_rest_of_compilation directly.
3538 Kill this check once this is fixed. */
3539 if (!id->dst_node->analyzed)
3542 cg_edge = cgraph_edge (id->dst_node, stmt);
3544 /* Don't inline functions with different EH personalities. */
3545 if (DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3546 && DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl)
3547 && (DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3548 != DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl)))
3551 /* Don't try to inline functions that are not well-suited to
3553 if (!cgraph_inline_p (cg_edge, &reason))
3555 /* If this call was originally indirect, we do not want to emit any
3556 inlining related warnings or sorry messages because there are no
3557 guarantees regarding those. */
3558 if (cg_edge->indirect_call)
3561 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3562 /* Avoid warnings during early inline pass. */
3563 && cgraph_global_info_ready)
3565 sorry ("inlining failed in call to %q+F: %s", fn,
3566 cgraph_inline_failed_string (reason));
3567 sorry ("called from here");
3569 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3570 && !DECL_IN_SYSTEM_HEADER (fn)
3571 && reason != CIF_UNSPECIFIED
3572 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3573 /* Avoid warnings during early inline pass. */
3574 && cgraph_global_info_ready)
3576 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3577 fn, cgraph_inline_failed_string (reason));
3578 warning (OPT_Winline, "called from here");
3582 fn = cg_edge->callee->decl;
3584 #ifdef ENABLE_CHECKING
3585 if (cg_edge->callee->decl != id->dst_node->decl)
3586 verify_cgraph_node (cg_edge->callee);
3589 /* We will be inlining this callee. */
3590 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
3592 /* Update the callers EH personality. */
3593 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
3594 DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3595 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
3597 /* Split the block holding the GIMPLE_CALL. */
3598 e = split_block (bb, stmt);
3600 return_block = e->dest;
3603 /* split_block splits after the statement; work around this by
3604 moving the call into the second block manually. Not pretty,
3605 but seems easier than doing the CFG manipulation by hand
3606 when the GIMPLE_CALL is in the last statement of BB. */
3607 stmt_gsi = gsi_last_bb (bb);
3608 gsi_remove (&stmt_gsi, false);
3610 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3611 been the source of abnormal edges. In this case, schedule
3612 the removal of dead abnormal edges. */
3613 gsi = gsi_start_bb (return_block);
3614 if (gsi_end_p (gsi))
3616 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3617 purge_dead_abnormal_edges = true;
3621 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3622 purge_dead_abnormal_edges = false;
3625 stmt_gsi = gsi_start_bb (return_block);
3627 /* Build a block containing code to initialize the arguments, the
3628 actual inline expansion of the body, and a label for the return
3629 statements within the function to jump to. The type of the
3630 statement expression is the return type of the function call. */
3631 id->block = make_node (BLOCK);
3632 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3633 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3634 prepend_lexical_block (gimple_block (stmt), id->block);
3636 /* Local declarations will be replaced by their equivalents in this
3639 id->decl_map = pointer_map_create ();
3640 dst = id->debug_map;
3641 id->debug_map = NULL;
3643 /* Record the function we are about to inline. */
3645 id->src_node = cg_edge->callee;
3646 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3647 id->gimple_call = stmt;
3649 gcc_assert (!id->src_cfun->after_inlining);
3652 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3654 gimple_stmt_iterator si = gsi_last_bb (bb);
3655 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3659 initialize_inlined_parameters (id, stmt, fn, bb);
3661 if (DECL_INITIAL (fn))
3662 prepend_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3664 /* Return statements in the function body will be replaced by jumps
3665 to the RET_LABEL. */
3666 gcc_assert (DECL_INITIAL (fn));
3667 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3669 /* Find the LHS to which the result of this call is assigned. */
3671 if (gimple_call_lhs (stmt))
3673 modify_dest = gimple_call_lhs (stmt);
3675 /* The function which we are inlining might not return a value,
3676 in which case we should issue a warning that the function
3677 does not return a value. In that case the optimizers will
3678 see that the variable to which the value is assigned was not
3679 initialized. We do not want to issue a warning about that
3680 uninitialized variable. */
3681 if (DECL_P (modify_dest))
3682 TREE_NO_WARNING (modify_dest) = 1;
3684 if (gimple_call_return_slot_opt_p (stmt))
3686 return_slot = modify_dest;
3693 /* If we are inlining a call to the C++ operator new, we don't want
3694 to use type based alias analysis on the return value. Otherwise
3695 we may get confused if the compiler sees that the inlined new
3696 function returns a pointer which was just deleted. See bug
3698 if (DECL_IS_OPERATOR_NEW (fn))
3704 /* Declare the return variable for the function. */
3705 use_retvar = declare_return_variable (id, return_slot, modify_dest);
3707 /* Add local vars in this inlined callee to caller. */
3708 t_step = id->src_cfun->local_decls;
3709 for (; t_step; t_step = TREE_CHAIN (t_step))
3711 var = TREE_VALUE (t_step);
3712 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3714 if (var_ann (var) && add_referenced_var (var))
3715 cfun->local_decls = tree_cons (NULL_TREE, var,
3718 else if (!can_be_nonlocal (var, id))
3719 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3723 if (dump_file && (dump_flags & TDF_DETAILS))
3725 fprintf (dump_file, "Inlining ");
3726 print_generic_expr (dump_file, id->src_fn, 0);
3727 fprintf (dump_file, " to ");
3728 print_generic_expr (dump_file, id->dst_fn, 0);
3729 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
3732 /* This is it. Duplicate the callee body. Assume callee is
3733 pre-gimplified. Note that we must not alter the caller
3734 function in any way before this point, as this CALL_EXPR may be
3735 a self-referential call; if we're calling ourselves, we need to
3736 duplicate our body before altering anything. */
3737 copy_body (id, bb->count,
3738 cg_edge->frequency * REG_BR_PROB_BASE / CGRAPH_FREQ_BASE,
3741 /* Reset the escaped solution. */
3742 if (cfun->gimple_df)
3743 pt_solution_reset (&cfun->gimple_df->escaped);
3748 pointer_map_destroy (id->debug_map);
3749 id->debug_map = dst;
3751 pointer_map_destroy (id->decl_map);
3754 /* Unlink the calls virtual operands before replacing it. */
3755 unlink_stmt_vdef (stmt);
3757 /* If the inlined function returns a result that we care about,
3758 substitute the GIMPLE_CALL with an assignment of the return
3759 variable to the LHS of the call. That is, if STMT was
3760 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
3761 if (use_retvar && gimple_call_lhs (stmt))
3763 gimple old_stmt = stmt;
3764 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3765 gsi_replace (&stmt_gsi, stmt, false);
3766 if (gimple_in_ssa_p (cfun))
3767 mark_symbols_for_renaming (stmt);
3768 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3772 /* Handle the case of inlining a function with no return
3773 statement, which causes the return value to become undefined. */
3774 if (gimple_call_lhs (stmt)
3775 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3777 tree name = gimple_call_lhs (stmt);
3778 tree var = SSA_NAME_VAR (name);
3779 tree def = gimple_default_def (cfun, var);
3783 /* If the variable is used undefined, make this name
3784 undefined via a move. */
3785 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3786 gsi_replace (&stmt_gsi, stmt, true);
3790 /* Otherwise make this variable undefined. */
3791 gsi_remove (&stmt_gsi, true);
3792 set_default_def (var, name);
3793 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
3797 gsi_remove (&stmt_gsi, true);
3800 if (purge_dead_abnormal_edges)
3801 gimple_purge_dead_abnormal_call_edges (return_block);
3803 /* If the value of the new expression is ignored, that's OK. We
3804 don't warn about this for CALL_EXPRs, so we shouldn't warn about
3805 the equivalent inlined version either. */
3806 if (is_gimple_assign (stmt))
3808 gcc_assert (gimple_assign_single_p (stmt)
3809 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
3810 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
3813 /* Output the inlining info for this abstract function, since it has been
3814 inlined. If we don't do this now, we can lose the information about the
3815 variables in the function when the blocks get blown away as soon as we
3816 remove the cgraph node. */
3817 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
3819 /* Update callgraph if needed. */
3820 cgraph_remove_node (cg_edge->callee);
3822 id->block = NULL_TREE;
3823 successfully_inlined = TRUE;
3826 input_location = saved_location;
3827 return successfully_inlined;
3830 /* Expand call statements reachable from STMT_P.
3831 We can only have CALL_EXPRs as the "toplevel" tree code or nested
3832 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
3833 unfortunately not use that function here because we need a pointer
3834 to the CALL_EXPR, not the tree itself. */
3837 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
3839 gimple_stmt_iterator gsi;
3841 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3843 gimple stmt = gsi_stmt (gsi);
3845 if (is_gimple_call (stmt)
3846 && expand_call_inline (bb, stmt, id))
3854 /* Walk all basic blocks created after FIRST and try to fold every statement
3855 in the STATEMENTS pointer set. */
3858 fold_marked_statements (int first, struct pointer_set_t *statements)
3860 for (; first < n_basic_blocks; first++)
3861 if (BASIC_BLOCK (first))
3863 gimple_stmt_iterator gsi;
3865 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
3868 if (pointer_set_contains (statements, gsi_stmt (gsi)))
3870 gimple old_stmt = gsi_stmt (gsi);
3871 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
3873 if (old_decl && DECL_BUILT_IN (old_decl))
3875 /* Folding builtins can create multiple instructions,
3876 we need to look at all of them. */
3877 gimple_stmt_iterator i2 = gsi;
3879 if (fold_stmt (&gsi))
3883 i2 = gsi_start_bb (BASIC_BLOCK (first));
3888 new_stmt = gsi_stmt (i2);
3889 update_stmt (new_stmt);
3890 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
3893 if (new_stmt == gsi_stmt (gsi))
3895 /* It is okay to check only for the very last
3896 of these statements. If it is a throwing
3897 statement nothing will change. If it isn't
3898 this can remove EH edges. If that weren't
3899 correct then because some intermediate stmts
3900 throw, but not the last one. That would mean
3901 we'd have to split the block, which we can't
3902 here and we'd loose anyway. And as builtins
3903 probably never throw, this all
3905 if (maybe_clean_or_replace_eh_stmt (old_stmt,
3907 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3914 else if (fold_stmt (&gsi))
3916 /* Re-read the statement from GSI as fold_stmt() may
3918 gimple new_stmt = gsi_stmt (gsi);
3919 update_stmt (new_stmt);
3921 if (is_gimple_call (old_stmt)
3922 || is_gimple_call (new_stmt))
3923 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
3926 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3927 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3933 /* Return true if BB has at least one abnormal outgoing edge. */
3936 has_abnormal_outgoing_edge_p (basic_block bb)
3941 FOR_EACH_EDGE (e, ei, bb->succs)
3942 if (e->flags & EDGE_ABNORMAL)
3948 /* Expand calls to inline functions in the body of FN. */
3951 optimize_inline_calls (tree fn)
3955 int last = n_basic_blocks;
3956 struct gimplify_ctx gctx;
3958 /* There is no point in performing inlining if errors have already
3959 occurred -- and we might crash if we try to inline invalid
3961 if (errorcount || sorrycount)
3965 memset (&id, 0, sizeof (id));
3967 id.src_node = id.dst_node = cgraph_node (fn);
3969 /* Or any functions that aren't finished yet. */
3970 if (current_function_decl)
3971 id.dst_fn = current_function_decl;
3973 id.copy_decl = copy_decl_maybe_to_var;
3974 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3975 id.transform_new_cfg = false;
3976 id.transform_return_to_modify = true;
3977 id.transform_lang_insert_block = NULL;
3978 id.statements_to_fold = pointer_set_create ();
3980 push_gimplify_context (&gctx);
3982 /* We make no attempts to keep dominance info up-to-date. */
3983 free_dominance_info (CDI_DOMINATORS);
3984 free_dominance_info (CDI_POST_DOMINATORS);
3986 /* Register specific gimple functions. */
3987 gimple_register_cfg_hooks ();
3989 /* Reach the trees by walking over the CFG, and note the
3990 enclosing basic-blocks in the call edges. */
3991 /* We walk the blocks going forward, because inlined function bodies
3992 will split id->current_basic_block, and the new blocks will
3993 follow it; we'll trudge through them, processing their CALL_EXPRs
3996 gimple_expand_calls_inline (bb, &id);
3998 pop_gimplify_context (NULL);
4000 #ifdef ENABLE_CHECKING
4002 struct cgraph_edge *e;
4004 verify_cgraph_node (id.dst_node);
4006 /* Double check that we inlined everything we are supposed to inline. */
4007 for (e = id.dst_node->callees; e; e = e->next_callee)
4008 gcc_assert (e->inline_failed);
4012 /* Fold the statements before compacting/renumbering the basic blocks. */
4013 fold_marked_statements (last, id.statements_to_fold);
4014 pointer_set_destroy (id.statements_to_fold);
4016 gcc_assert (!id.debug_stmts);
4018 /* Renumber the (code) basic_blocks consecutively. */
4020 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4023 fold_cond_expr_cond ();
4024 delete_unreachable_blocks_update_callgraph (&id);
4025 #ifdef ENABLE_CHECKING
4026 verify_cgraph_node (id.dst_node);
4029 /* It would be nice to check SSA/CFG/statement consistency here, but it is
4030 not possible yet - the IPA passes might make various functions to not
4031 throw and they don't care to proactively update local EH info. This is
4032 done later in fixup_cfg pass that also execute the verification. */
4033 return (TODO_update_ssa
4035 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
4036 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
4039 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4042 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4044 enum tree_code code = TREE_CODE (*tp);
4045 enum tree_code_class cl = TREE_CODE_CLASS (code);
4047 /* We make copies of most nodes. */
4048 if (IS_EXPR_CODE_CLASS (cl)
4049 || code == TREE_LIST
4051 || code == TYPE_DECL
4052 || code == OMP_CLAUSE)
4054 /* Because the chain gets clobbered when we make a copy, we save it
4056 tree chain = NULL_TREE, new_tree;
4058 chain = TREE_CHAIN (*tp);
4060 /* Copy the node. */
4061 new_tree = copy_node (*tp);
4063 /* Propagate mudflap marked-ness. */
4064 if (flag_mudflap && mf_marked_p (*tp))
4069 /* Now, restore the chain, if appropriate. That will cause
4070 walk_tree to walk into the chain as well. */
4071 if (code == PARM_DECL
4072 || code == TREE_LIST
4073 || code == OMP_CLAUSE)
4074 TREE_CHAIN (*tp) = chain;
4076 /* For now, we don't update BLOCKs when we make copies. So, we
4077 have to nullify all BIND_EXPRs. */
4078 if (TREE_CODE (*tp) == BIND_EXPR)
4079 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
4081 else if (code == CONSTRUCTOR)
4083 /* CONSTRUCTOR nodes need special handling because
4084 we need to duplicate the vector of elements. */
4087 new_tree = copy_node (*tp);
4089 /* Propagate mudflap marked-ness. */
4090 if (flag_mudflap && mf_marked_p (*tp))
4093 CONSTRUCTOR_ELTS (new_tree) = VEC_copy (constructor_elt, gc,
4094 CONSTRUCTOR_ELTS (*tp));
4097 else if (TREE_CODE_CLASS (code) == tcc_type)
4099 else if (TREE_CODE_CLASS (code) == tcc_declaration)
4101 else if (TREE_CODE_CLASS (code) == tcc_constant)
4104 gcc_assert (code != STATEMENT_LIST);
4108 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
4109 information indicating to what new SAVE_EXPR this one should be mapped,
4110 use that one. Otherwise, create a new node and enter it in ST. FN is
4111 the function into which the copy will be placed. */
4114 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
4116 struct pointer_map_t *st = (struct pointer_map_t *) st_;
4120 /* See if we already encountered this SAVE_EXPR. */
4121 n = (tree *) pointer_map_contains (st, *tp);
4123 /* If we didn't already remap this SAVE_EXPR, do so now. */
4126 t = copy_node (*tp);
4128 /* Remember this SAVE_EXPR. */
4129 *pointer_map_insert (st, *tp) = t;
4130 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
4131 *pointer_map_insert (st, t) = t;
4135 /* We've already walked into this SAVE_EXPR; don't do it again. */
4140 /* Replace this SAVE_EXPR with the copy. */
4144 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
4145 copies the declaration and enters it in the splay_tree in DATA (which is
4146 really an `copy_body_data *'). */
4149 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4152 copy_body_data *id = (copy_body_data *) data;
4154 /* Don't walk into types. */
4158 else if (TREE_CODE (*tp) == LABEL_EXPR)
4160 tree decl = TREE_OPERAND (*tp, 0);
4162 /* Copy the decl and remember the copy. */
4163 insert_decl_map (id, decl, id->copy_decl (decl, id));
4169 /* Perform any modifications to EXPR required when it is unsaved. Does
4170 not recurse into EXPR's subtrees. */
4173 unsave_expr_1 (tree expr)
4175 switch (TREE_CODE (expr))
4178 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4179 It's OK for this to happen if it was part of a subtree that
4180 isn't immediately expanded, such as operand 2 of another
4182 if (TREE_OPERAND (expr, 1))
4185 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4186 TREE_OPERAND (expr, 3) = NULL_TREE;
4194 /* Called via walk_tree when an expression is unsaved. Using the
4195 splay_tree pointed to by ST (which is really a `splay_tree'),
4196 remaps all local declarations to appropriate replacements. */
4199 unsave_r (tree *tp, int *walk_subtrees, void *data)
4201 copy_body_data *id = (copy_body_data *) data;
4202 struct pointer_map_t *st = id->decl_map;
4205 /* Only a local declaration (variable or label). */
4206 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
4207 || TREE_CODE (*tp) == LABEL_DECL)
4209 /* Lookup the declaration. */
4210 n = (tree *) pointer_map_contains (st, *tp);
4212 /* If it's there, remap it. */
4217 else if (TREE_CODE (*tp) == STATEMENT_LIST)
4219 else if (TREE_CODE (*tp) == BIND_EXPR)
4220 copy_bind_expr (tp, walk_subtrees, id);
4221 else if (TREE_CODE (*tp) == SAVE_EXPR
4222 || TREE_CODE (*tp) == TARGET_EXPR)
4223 remap_save_expr (tp, st, walk_subtrees);
4226 copy_tree_r (tp, walk_subtrees, NULL);
4228 /* Do whatever unsaving is required. */
4229 unsave_expr_1 (*tp);
4232 /* Keep iterating. */
4236 /* Copies everything in EXPR and replaces variables, labels
4237 and SAVE_EXPRs local to EXPR. */
4240 unsave_expr_now (tree expr)
4244 /* There's nothing to do for NULL_TREE. */
4249 memset (&id, 0, sizeof (id));
4250 id.src_fn = current_function_decl;
4251 id.dst_fn = current_function_decl;
4252 id.decl_map = pointer_map_create ();
4253 id.debug_map = NULL;
4255 id.copy_decl = copy_decl_no_change;
4256 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4257 id.transform_new_cfg = false;
4258 id.transform_return_to_modify = false;
4259 id.transform_lang_insert_block = NULL;
4261 /* Walk the tree once to find local labels. */
4262 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
4264 /* Walk the tree again, copying, remapping, and unsaving. */
4265 walk_tree (&expr, unsave_r, &id, NULL);
4268 pointer_map_destroy (id.decl_map);
4270 pointer_map_destroy (id.debug_map);
4275 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
4276 label, copies the declaration and enters it in the splay_tree in DATA (which
4277 is really a 'copy_body_data *'. */
4280 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
4281 bool *handled_ops_p ATTRIBUTE_UNUSED,
4282 struct walk_stmt_info *wi)
4284 copy_body_data *id = (copy_body_data *) wi->info;
4285 gimple stmt = gsi_stmt (*gsip);
4287 if (gimple_code (stmt) == GIMPLE_LABEL)
4289 tree decl = gimple_label_label (stmt);
4291 /* Copy the decl and remember the copy. */
4292 insert_decl_map (id, decl, id->copy_decl (decl, id));
4299 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4300 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4301 remaps all local declarations to appropriate replacements in gimple
4305 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
4307 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
4308 copy_body_data *id = (copy_body_data *) wi->info;
4309 struct pointer_map_t *st = id->decl_map;
4313 /* Only a local declaration (variable or label). */
4314 if ((TREE_CODE (expr) == VAR_DECL
4315 && !TREE_STATIC (expr))
4316 || TREE_CODE (expr) == LABEL_DECL)
4318 /* Lookup the declaration. */
4319 n = (tree *) pointer_map_contains (st, expr);
4321 /* If it's there, remap it. */
4326 else if (TREE_CODE (expr) == STATEMENT_LIST
4327 || TREE_CODE (expr) == BIND_EXPR
4328 || TREE_CODE (expr) == SAVE_EXPR)
4330 else if (TREE_CODE (expr) == TARGET_EXPR)
4332 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4333 It's OK for this to happen if it was part of a subtree that
4334 isn't immediately expanded, such as operand 2 of another
4336 if (!TREE_OPERAND (expr, 1))
4338 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4339 TREE_OPERAND (expr, 3) = NULL_TREE;
4343 /* Keep iterating. */
4348 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4349 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4350 remaps all local declarations to appropriate replacements in gimple
4354 replace_locals_stmt (gimple_stmt_iterator *gsip,
4355 bool *handled_ops_p ATTRIBUTE_UNUSED,
4356 struct walk_stmt_info *wi)
4358 copy_body_data *id = (copy_body_data *) wi->info;
4359 gimple stmt = gsi_stmt (*gsip);
4361 if (gimple_code (stmt) == GIMPLE_BIND)
4363 tree block = gimple_bind_block (stmt);
4367 remap_block (&block, id);
4368 gimple_bind_set_block (stmt, block);
4371 /* This will remap a lot of the same decls again, but this should be
4373 if (gimple_bind_vars (stmt))
4374 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), NULL, id));
4377 /* Keep iterating. */
4382 /* Copies everything in SEQ and replaces variables and labels local to
4383 current_function_decl. */
4386 copy_gimple_seq_and_replace_locals (gimple_seq seq)
4389 struct walk_stmt_info wi;
4390 struct pointer_set_t *visited;
4393 /* There's nothing to do for NULL_TREE. */
4398 memset (&id, 0, sizeof (id));
4399 id.src_fn = current_function_decl;
4400 id.dst_fn = current_function_decl;
4401 id.decl_map = pointer_map_create ();
4402 id.debug_map = NULL;
4404 id.copy_decl = copy_decl_no_change;
4405 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4406 id.transform_new_cfg = false;
4407 id.transform_return_to_modify = false;
4408 id.transform_lang_insert_block = NULL;
4410 /* Walk the tree once to find local labels. */
4411 memset (&wi, 0, sizeof (wi));
4412 visited = pointer_set_create ();
4415 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
4416 pointer_set_destroy (visited);
4418 copy = gimple_seq_copy (seq);
4420 /* Walk the copy, remapping decls. */
4421 memset (&wi, 0, sizeof (wi));
4423 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4426 pointer_map_destroy (id.decl_map);
4428 pointer_map_destroy (id.debug_map);
4434 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
4437 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
4446 debug_find_tree (tree top, tree search)
4448 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4452 /* Declare the variables created by the inliner. Add all the variables in
4453 VARS to BIND_EXPR. */
4456 declare_inline_vars (tree block, tree vars)
4459 for (t = vars; t; t = TREE_CHAIN (t))
4461 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4462 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
4463 cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
4467 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4470 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
4471 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
4472 VAR_DECL translation. */
4475 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
4477 /* Don't generate debug information for the copy if we wouldn't have
4478 generated it for the copy either. */
4479 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4480 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4482 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4483 declaration inspired this copy. */
4484 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4486 /* The new variable/label has no RTL, yet. */
4487 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4488 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4489 SET_DECL_RTL (copy, NULL_RTX);
4491 /* These args would always appear unused, if not for this. */
4492 TREE_USED (copy) = 1;
4494 /* Set the context for the new declaration. */
4495 if (!DECL_CONTEXT (decl))
4496 /* Globals stay global. */
4498 else if (DECL_CONTEXT (decl) != id->src_fn)
4499 /* Things that weren't in the scope of the function we're inlining
4500 from aren't in the scope we're inlining to, either. */
4502 else if (TREE_STATIC (decl))
4503 /* Function-scoped static variables should stay in the original
4507 /* Ordinary automatic local variables are now in the scope of the
4509 DECL_CONTEXT (copy) = id->dst_fn;
4515 copy_decl_to_var (tree decl, copy_body_data *id)
4519 gcc_assert (TREE_CODE (decl) == PARM_DECL
4520 || TREE_CODE (decl) == RESULT_DECL);
4522 type = TREE_TYPE (decl);
4524 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4525 VAR_DECL, DECL_NAME (decl), type);
4526 if (DECL_PT_UID_SET_P (decl))
4527 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
4528 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4529 TREE_READONLY (copy) = TREE_READONLY (decl);
4530 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4531 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4533 return copy_decl_for_dup_finish (id, decl, copy);
4536 /* Like copy_decl_to_var, but create a return slot object instead of a
4537 pointer variable for return by invisible reference. */
4540 copy_result_decl_to_var (tree decl, copy_body_data *id)
4544 gcc_assert (TREE_CODE (decl) == PARM_DECL
4545 || TREE_CODE (decl) == RESULT_DECL);
4547 type = TREE_TYPE (decl);
4548 if (DECL_BY_REFERENCE (decl))
4549 type = TREE_TYPE (type);
4551 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4552 VAR_DECL, DECL_NAME (decl), type);
4553 if (DECL_PT_UID_SET_P (decl))
4554 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
4555 TREE_READONLY (copy) = TREE_READONLY (decl);
4556 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4557 if (!DECL_BY_REFERENCE (decl))
4559 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4560 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4563 return copy_decl_for_dup_finish (id, decl, copy);
4567 copy_decl_no_change (tree decl, copy_body_data *id)
4571 copy = copy_node (decl);
4573 /* The COPY is not abstract; it will be generated in DST_FN. */
4574 DECL_ABSTRACT (copy) = 0;
4575 lang_hooks.dup_lang_specific_decl (copy);
4577 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4578 been taken; it's for internal bookkeeping in expand_goto_internal. */
4579 if (TREE_CODE (copy) == LABEL_DECL)
4581 TREE_ADDRESSABLE (copy) = 0;
4582 LABEL_DECL_UID (copy) = -1;
4585 return copy_decl_for_dup_finish (id, decl, copy);
4589 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4591 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4592 return copy_decl_to_var (decl, id);
4594 return copy_decl_no_change (decl, id);
4597 /* Return a copy of the function's argument tree. */
4599 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4600 bitmap args_to_skip, tree *vars)
4603 tree new_parm = NULL;
4608 for (arg = orig_parm; arg; arg = TREE_CHAIN (arg), i++)
4609 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4611 tree new_tree = remap_decl (arg, id);
4612 lang_hooks.dup_lang_specific_decl (new_tree);
4614 parg = &TREE_CHAIN (new_tree);
4616 else if (!pointer_map_contains (id->decl_map, arg))
4618 /* Make an equivalent VAR_DECL. If the argument was used
4619 as temporary variable later in function, the uses will be
4620 replaced by local variable. */
4621 tree var = copy_decl_to_var (arg, id);
4623 add_referenced_var (var);
4624 insert_decl_map (id, arg, var);
4625 /* Declare this new variable. */
4626 TREE_CHAIN (var) = *vars;
4632 /* Return a copy of the function's static chain. */
4634 copy_static_chain (tree static_chain, copy_body_data * id)
4636 tree *chain_copy, *pvar;
4638 chain_copy = &static_chain;
4639 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4641 tree new_tree = remap_decl (*pvar, id);
4642 lang_hooks.dup_lang_specific_decl (new_tree);
4643 TREE_CHAIN (new_tree) = TREE_CHAIN (*pvar);
4646 return static_chain;
4649 /* Return true if the function is allowed to be versioned.
4650 This is a guard for the versioning functionality. */
4653 tree_versionable_function_p (tree fndecl)
4655 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
4656 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
4659 /* Delete all unreachable basic blocks and update callgraph.
4660 Doing so is somewhat nontrivial because we need to update all clones and
4661 remove inline function that become unreachable. */
4664 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
4666 bool changed = false;
4667 basic_block b, next_bb;
4669 find_unreachable_blocks ();
4671 /* Delete all unreachable basic blocks. */
4673 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
4675 next_bb = b->next_bb;
4677 if (!(b->flags & BB_REACHABLE))
4679 gimple_stmt_iterator bsi;
4681 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
4682 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL)
4684 struct cgraph_edge *e;
4685 struct cgraph_node *node;
4687 if ((e = cgraph_edge (id->dst_node, gsi_stmt (bsi))) != NULL)
4689 if (!e->inline_failed)
4690 cgraph_remove_node_and_inline_clones (e->callee);
4692 cgraph_remove_edge (e);
4694 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
4695 && id->dst_node->clones)
4696 for (node = id->dst_node->clones; node != id->dst_node;)
4698 if ((e = cgraph_edge (node, gsi_stmt (bsi))) != NULL)
4700 if (!e->inline_failed)
4701 cgraph_remove_node_and_inline_clones (e->callee);
4703 cgraph_remove_edge (e);
4707 node = node->clones;
4708 else if (node->next_sibling_clone)
4709 node = node->next_sibling_clone;
4712 while (node != id->dst_node && !node->next_sibling_clone)
4713 node = node->clone_of;
4714 if (node != id->dst_node)
4715 node = node->next_sibling_clone;
4719 delete_basic_block (b);
4725 tidy_fallthru_edges ();
4729 /* Update clone info after duplication. */
4732 update_clone_info (copy_body_data * id)
4734 struct cgraph_node *node;
4735 if (!id->dst_node->clones)
4737 for (node = id->dst_node->clones; node != id->dst_node;)
4739 /* First update replace maps to match the new body. */
4740 if (node->clone.tree_map)
4743 for (i = 0; i < VEC_length (ipa_replace_map_p, node->clone.tree_map); i++)
4745 struct ipa_replace_map *replace_info;
4746 replace_info = VEC_index (ipa_replace_map_p, node->clone.tree_map, i);
4747 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
4748 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
4752 node = node->clones;
4753 else if (node->next_sibling_clone)
4754 node = node->next_sibling_clone;
4757 while (node != id->dst_node && !node->next_sibling_clone)
4758 node = node->clone_of;
4759 if (node != id->dst_node)
4760 node = node->next_sibling_clone;
4765 /* Create a copy of a function's tree.
4766 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4767 of the original function and the new copied function
4768 respectively. In case we want to replace a DECL
4769 tree with another tree while duplicating the function's
4770 body, TREE_MAP represents the mapping between these
4771 trees. If UPDATE_CLONES is set, the call_stmt fields
4772 of edges of clones of the function will be updated. */
4774 tree_function_versioning (tree old_decl, tree new_decl,
4775 VEC(ipa_replace_map_p,gc)* tree_map,
4776 bool update_clones, bitmap args_to_skip)
4778 struct cgraph_node *old_version_node;
4779 struct cgraph_node *new_version_node;
4783 struct ipa_replace_map *replace_info;
4784 basic_block old_entry_block, bb;
4785 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
4788 tree old_current_function_decl = current_function_decl;
4789 tree vars = NULL_TREE;
4791 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
4792 && TREE_CODE (new_decl) == FUNCTION_DECL);
4793 DECL_POSSIBLY_INLINED (old_decl) = 1;
4795 old_version_node = cgraph_node (old_decl);
4796 new_version_node = cgraph_node (new_decl);
4798 /* Output the inlining info for this abstract function, since it has been
4799 inlined. If we don't do this now, we can lose the information about the
4800 variables in the function when the blocks get blown away as soon as we
4801 remove the cgraph node. */
4802 (*debug_hooks->outlining_inline_function) (old_decl);
4804 DECL_ARTIFICIAL (new_decl) = 1;
4805 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4806 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
4808 /* Prepare the data structures for the tree copy. */
4809 memset (&id, 0, sizeof (id));
4811 /* Generate a new name for the new version. */
4812 id.statements_to_fold = pointer_set_create ();
4814 id.decl_map = pointer_map_create ();
4815 id.debug_map = NULL;
4816 id.src_fn = old_decl;
4817 id.dst_fn = new_decl;
4818 id.src_node = old_version_node;
4819 id.dst_node = new_version_node;
4820 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4821 if (id.src_node->ipa_transforms_to_apply)
4823 VEC(ipa_opt_pass,heap) * old_transforms_to_apply = id.dst_node->ipa_transforms_to_apply;
4826 id.dst_node->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
4827 id.src_node->ipa_transforms_to_apply);
4828 for (i = 0; i < VEC_length (ipa_opt_pass, old_transforms_to_apply); i++)
4829 VEC_safe_push (ipa_opt_pass, heap, id.dst_node->ipa_transforms_to_apply,
4830 VEC_index (ipa_opt_pass,
4831 old_transforms_to_apply,
4835 id.copy_decl = copy_decl_no_change;
4836 id.transform_call_graph_edges
4837 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4838 id.transform_new_cfg = true;
4839 id.transform_return_to_modify = false;
4840 id.transform_lang_insert_block = NULL;
4842 current_function_decl = new_decl;
4843 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4844 (DECL_STRUCT_FUNCTION (old_decl));
4845 initialize_cfun (new_decl, old_decl,
4846 old_entry_block->count);
4847 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4849 /* Copy the function's static chain. */
4850 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4852 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4853 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4856 /* If there's a tree_map, prepare for substitution. */
4858 for (i = 0; i < VEC_length (ipa_replace_map_p, tree_map); i++)
4861 replace_info = VEC_index (ipa_replace_map_p, tree_map, i);
4862 if (replace_info->replace_p)
4864 tree op = replace_info->new_tree;
4868 if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
4869 op = TREE_OPERAND (op, 0);
4871 if (TREE_CODE (op) == ADDR_EXPR)
4873 op = TREE_OPERAND (op, 0);
4874 while (handled_component_p (op))
4875 op = TREE_OPERAND (op, 0);
4876 if (TREE_CODE (op) == VAR_DECL)
4877 add_referenced_var (op);
4879 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
4880 init = setup_one_parameter (&id, replace_info->old_tree,
4881 replace_info->new_tree, id.src_fn,
4885 VEC_safe_push (gimple, heap, init_stmts, init);
4888 /* Copy the function's arguments. */
4889 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4890 DECL_ARGUMENTS (new_decl) =
4891 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
4892 args_to_skip, &vars);
4894 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4896 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4897 number_blocks (id.dst_fn);
4899 declare_inline_vars (DECL_INITIAL (new_decl), vars);
4901 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4902 /* Add local vars. */
4903 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4904 t_step; t_step = TREE_CHAIN (t_step))
4906 tree var = TREE_VALUE (t_step);
4907 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
4908 cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
4909 else if (!can_be_nonlocal (var, &id))
4911 tree_cons (NULL_TREE, remap_decl (var, &id),
4915 /* Copy the Function's body. */
4916 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
4917 ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4919 if (DECL_RESULT (old_decl) != NULL_TREE)
4921 tree *res_decl = &DECL_RESULT (old_decl);
4922 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4923 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4926 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4927 number_blocks (new_decl);
4929 /* We want to create the BB unconditionally, so that the addition of
4930 debug stmts doesn't affect BB count, which may in the end cause
4931 codegen differences. */
4932 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
4933 while (VEC_length (gimple, init_stmts))
4934 insert_init_stmt (&id, bb, VEC_pop (gimple, init_stmts));
4935 update_clone_info (&id);
4937 /* Remap the nonlocal_goto_save_area, if any. */
4938 if (cfun->nonlocal_goto_save_area)
4940 struct walk_stmt_info wi;
4942 memset (&wi, 0, sizeof (wi));
4944 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
4948 pointer_map_destroy (id.decl_map);
4950 pointer_map_destroy (id.debug_map);
4951 free_dominance_info (CDI_DOMINATORS);
4952 free_dominance_info (CDI_POST_DOMINATORS);
4954 fold_marked_statements (0, id.statements_to_fold);
4955 pointer_set_destroy (id.statements_to_fold);
4956 fold_cond_expr_cond ();
4957 delete_unreachable_blocks_update_callgraph (&id);
4958 update_ssa (TODO_update_ssa);
4959 free_dominance_info (CDI_DOMINATORS);
4960 free_dominance_info (CDI_POST_DOMINATORS);
4962 gcc_assert (!id.debug_stmts);
4963 VEC_free (gimple, heap, init_stmts);
4965 current_function_decl = old_current_function_decl;
4966 gcc_assert (!current_function_decl
4967 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4971 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
4972 the callee and return the inlined body on success. */
4975 maybe_inline_call_in_expr (tree exp)
4977 tree fn = get_callee_fndecl (exp);
4979 /* We can only try to inline "const" functions. */
4980 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
4982 struct pointer_map_t *decl_map = pointer_map_create ();
4983 call_expr_arg_iterator iter;
4987 /* Remap the parameters. */
4988 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
4990 param = TREE_CHAIN (param), arg = next_call_expr_arg (&iter))
4991 *pointer_map_insert (decl_map, param) = arg;
4993 memset (&id, 0, sizeof (id));
4995 id.dst_fn = current_function_decl;
4996 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
4997 id.decl_map = decl_map;
4999 id.copy_decl = copy_decl_no_change;
5000 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5001 id.transform_new_cfg = false;
5002 id.transform_return_to_modify = true;
5003 id.transform_lang_insert_block = false;
5005 /* Make sure not to unshare trees behind the front-end's back
5006 since front-end specific mechanisms may rely on sharing. */
5007 id.regimplify = false;
5008 id.do_not_unshare = true;
5010 /* We're not inside any EH region. */
5013 t = copy_tree_body (&id);
5014 pointer_map_destroy (decl_map);
5016 /* We can only return something suitable for use in a GENERIC
5018 if (TREE_CODE (t) == MODIFY_EXPR)
5019 return TREE_OPERAND (t, 1);
5025 /* Duplicate a type, fields and all. */
5028 build_duplicate_type (tree type)
5030 struct copy_body_data id;
5032 memset (&id, 0, sizeof (id));
5033 id.src_fn = current_function_decl;
5034 id.dst_fn = current_function_decl;
5036 id.decl_map = pointer_map_create ();
5037 id.debug_map = NULL;
5038 id.copy_decl = copy_decl_no_change;
5040 type = remap_type_1 (type, &id);
5042 pointer_map_destroy (id.decl_map);
5044 pointer_map_destroy (id.debug_map);
5046 TYPE_CANONICAL (type) = type;
5051 /* Return whether it is safe to inline a function because it used different
5052 target specific options or call site actual types mismatch parameter types.
5053 E is the call edge to be checked. */
5055 tree_can_inline_p (struct cgraph_edge *e)
5058 /* This causes a regression in SPEC in that it prevents a cold function from
5059 inlining a hot function. Perhaps this should only apply to functions
5060 that the user declares hot/cold/optimize explicitly. */
5062 /* Don't inline a function with a higher optimization level than the
5063 caller, or with different space constraints (hot/cold functions). */
5064 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
5065 tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
5067 if (caller_tree != callee_tree)
5069 struct cl_optimization *caller_opt
5070 = TREE_OPTIMIZATION ((caller_tree)
5072 : optimization_default_node);
5074 struct cl_optimization *callee_opt
5075 = TREE_OPTIMIZATION ((callee_tree)
5077 : optimization_default_node);
5079 if ((caller_opt->optimize > callee_opt->optimize)
5080 || (caller_opt->optimize_size != callee_opt->optimize_size))
5084 tree caller, callee, lhs;
5086 caller = e->caller->decl;
5087 callee = e->callee->decl;
5089 /* We cannot inline a function that uses a different EH personality
5091 if (DECL_FUNCTION_PERSONALITY (caller)
5092 && DECL_FUNCTION_PERSONALITY (callee)
5093 && (DECL_FUNCTION_PERSONALITY (caller)
5094 != DECL_FUNCTION_PERSONALITY (callee)))
5096 e->inline_failed = CIF_UNSPECIFIED;
5097 gimple_call_set_cannot_inline (e->call_stmt, true);
5101 /* Allow the backend to decide if inlining is ok. */
5102 if (!targetm.target_option.can_inline_p (caller, callee))
5104 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
5105 gimple_call_set_cannot_inline (e->call_stmt, true);
5106 e->call_stmt_cannot_inline_p = true;
5110 /* Do not inline calls where we cannot triviall work around mismatches
5111 in argument or return types. */
5113 && ((DECL_RESULT (callee)
5114 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
5115 && (lhs = gimple_call_lhs (e->call_stmt)) != NULL_TREE
5116 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
5118 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
5119 || !gimple_check_call_args (e->call_stmt)))
5121 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
5122 gimple_call_set_cannot_inline (e->call_stmt, true);
5123 e->call_stmt_cannot_inline_p = true;