2010-05-19 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / tree-inline.c
blobd7c7991fbeec328f881315d7a5df3ff3afb7550d
1 /* Tree inlining.
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)
11 any later version.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "rtl.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "insn-config.h"
35 #include "hashtab.h"
36 #include "langhooks.h"
37 #include "basic-block.h"
38 #include "tree-iterator.h"
39 #include "cgraph.h"
40 #include "intl.h"
41 #include "tree-mudflap.h"
42 #include "tree-flow.h"
43 #include "function.h"
44 #include "ggc.h"
45 #include "tree-flow.h"
46 #include "diagnostic.h"
47 #include "except.h"
48 #include "debug.h"
49 #include "pointer-set.h"
50 #include "ipa-prop.h"
51 #include "value-prof.h"
52 #include "tree-pass.h"
53 #include "target.h"
54 #include "integrate.h"
56 /* I'm not real happy about this, but we need to handle gimple and
57 non-gimple trees. */
58 #include "gimple.h"
60 /* Inlining, Cloning, Versioning, Parallelization
62 Inlining: a function body is duplicated, but the PARM_DECLs are
63 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
64 MODIFY_EXPRs that store to a dedicated returned-value variable.
65 The duplicated eh_region info of the copy will later be appended
66 to the info for the caller; the eh_region info in copied throwing
67 statements and RESX statements are adjusted accordingly.
69 Cloning: (only in C++) We have one body for a con/de/structor, and
70 multiple function decls, each with a unique parameter list.
71 Duplicate the body, using the given splay tree; some parameters
72 will become constants (like 0 or 1).
74 Versioning: a function body is duplicated and the result is a new
75 function rather than into blocks of an existing function as with
76 inlining. Some parameters will become constants.
78 Parallelization: a region of a function is duplicated resulting in
79 a new function. Variables may be replaced with complex expressions
80 to enable shared variable semantics.
82 All of these will simultaneously lookup any callgraph edges. If
83 we're going to inline the duplicated function body, and the given
84 function has some cloned callgraph nodes (one for each place this
85 function will be inlined) those callgraph edges will be duplicated.
86 If we're cloning the body, those callgraph edges will be
87 updated to point into the new body. (Note that the original
88 callgraph node and edge list will not be altered.)
90 See the CALL_EXPR handling case in copy_tree_body_r (). */
92 /* To Do:
94 o In order to make inlining-on-trees work, we pessimized
95 function-local static constants. In particular, they are now
96 always output, even when not addressed. Fix this by treating
97 function-local static constants just like global static
98 constants; the back-end already knows not to output them if they
99 are not needed.
101 o Provide heuristics to clamp inlining of recursive template
102 calls? */
105 /* Weights that estimate_num_insns uses for heuristics in inlining. */
107 eni_weights eni_inlining_weights;
109 /* Weights that estimate_num_insns uses to estimate the size of the
110 produced code. */
112 eni_weights eni_size_weights;
114 /* Weights that estimate_num_insns uses to estimate the time necessary
115 to execute the produced code. */
117 eni_weights eni_time_weights;
119 /* Prototypes. */
121 static tree declare_return_variable (copy_body_data *, tree, tree);
122 static void remap_block (tree *, copy_body_data *);
123 static void copy_bind_expr (tree *, int *, copy_body_data *);
124 static tree mark_local_for_remap_r (tree *, int *, void *);
125 static void unsave_expr_1 (tree);
126 static tree unsave_r (tree *, int *, void *);
127 static void declare_inline_vars (tree, tree);
128 static void remap_save_expr (tree *, void *, int *);
129 static void prepend_lexical_block (tree current_block, tree new_block);
130 static tree copy_decl_to_var (tree, copy_body_data *);
131 static tree copy_result_decl_to_var (tree, copy_body_data *);
132 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
133 static gimple remap_gimple_stmt (gimple, copy_body_data *);
134 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
136 /* Insert a tree->tree mapping for ID. Despite the name suggests
137 that the trees should be variables, it is used for more than that. */
139 void
140 insert_decl_map (copy_body_data *id, tree key, tree value)
142 *pointer_map_insert (id->decl_map, key) = value;
144 /* Always insert an identity map as well. If we see this same new
145 node again, we won't want to duplicate it a second time. */
146 if (key != value)
147 *pointer_map_insert (id->decl_map, value) = value;
150 /* Insert a tree->tree mapping for ID. This is only used for
151 variables. */
153 static void
154 insert_debug_decl_map (copy_body_data *id, tree key, tree value)
156 if (!gimple_in_ssa_p (id->src_cfun))
157 return;
159 if (!MAY_HAVE_DEBUG_STMTS)
160 return;
162 if (!target_for_debug_bind (key))
163 return;
165 gcc_assert (TREE_CODE (key) == PARM_DECL);
166 gcc_assert (TREE_CODE (value) == VAR_DECL);
168 if (!id->debug_map)
169 id->debug_map = pointer_map_create ();
171 *pointer_map_insert (id->debug_map, key) = value;
174 /* If nonzero, we're remapping the contents of inlined debug
175 statements. If negative, an error has occurred, such as a
176 reference to a variable that isn't available in the inlined
177 context. */
178 static int processing_debug_stmt = 0;
180 /* Construct new SSA name for old NAME. ID is the inline context. */
182 static tree
183 remap_ssa_name (tree name, copy_body_data *id)
185 tree new_tree;
186 tree *n;
188 gcc_assert (TREE_CODE (name) == SSA_NAME);
190 n = (tree *) pointer_map_contains (id->decl_map, name);
191 if (n)
192 return unshare_expr (*n);
194 if (processing_debug_stmt)
196 processing_debug_stmt = -1;
197 return name;
200 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
201 in copy_bb. */
202 new_tree = remap_decl (SSA_NAME_VAR (name), id);
204 /* We might've substituted constant or another SSA_NAME for
205 the variable.
207 Replace the SSA name representing RESULT_DECL by variable during
208 inlining: this saves us from need to introduce PHI node in a case
209 return value is just partly initialized. */
210 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
211 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
212 || !id->transform_return_to_modify))
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 if (gimple_nop_p (SSA_NAME_DEF_STMT (name)))
221 /* By inlining function having uninitialized variable, we might
222 extend the lifetime (variable might get reused). This cause
223 ICE in the case we end up extending lifetime of SSA name across
224 abnormal edge, but also increase register pressure.
226 We simply initialize all uninitialized vars by 0 except
227 for case we are inlining to very first BB. We can avoid
228 this for all BBs that are not inside strongly connected
229 regions of the CFG, but this is expensive to test. */
230 if (id->entry_bb
231 && is_gimple_reg (SSA_NAME_VAR (name))
232 && TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL
233 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
234 || EDGE_COUNT (id->entry_bb->preds) != 1))
236 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
237 gimple init_stmt;
239 init_stmt = gimple_build_assign (new_tree,
240 fold_convert (TREE_TYPE (new_tree),
241 integer_zero_node));
242 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
243 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
245 else
247 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
248 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name))
249 == name)
250 set_default_def (SSA_NAME_VAR (new_tree), new_tree);
254 else
255 insert_decl_map (id, name, new_tree);
256 return new_tree;
259 /* Remap DECL during the copying of the BLOCK tree for the function. */
261 tree
262 remap_decl (tree decl, copy_body_data *id)
264 tree *n;
266 /* We only remap local variables in the current function. */
268 /* See if we have remapped this declaration. */
270 n = (tree *) pointer_map_contains (id->decl_map, decl);
272 if (!n && processing_debug_stmt)
274 processing_debug_stmt = -1;
275 return decl;
278 /* If we didn't already have an equivalent for this declaration,
279 create one now. */
280 if (!n)
282 /* Make a copy of the variable or label. */
283 tree t = id->copy_decl (decl, id);
285 /* Remember it, so that if we encounter this local entity again
286 we can reuse this copy. Do this early because remap_type may
287 need this decl for TYPE_STUB_DECL. */
288 insert_decl_map (id, decl, t);
290 if (!DECL_P (t))
291 return t;
293 /* Remap types, if necessary. */
294 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
295 if (TREE_CODE (t) == TYPE_DECL)
296 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
298 /* Remap sizes as necessary. */
299 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
300 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
302 /* If fields, do likewise for offset and qualifier. */
303 if (TREE_CODE (t) == FIELD_DECL)
305 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
306 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
307 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
310 if (cfun && gimple_in_ssa_p (cfun)
311 && (TREE_CODE (t) == VAR_DECL
312 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
314 get_var_ann (t);
315 add_referenced_var (t);
317 return t;
320 if (id->do_not_unshare)
321 return *n;
322 else
323 return unshare_expr (*n);
326 static tree
327 remap_type_1 (tree type, copy_body_data *id)
329 tree new_tree, t;
331 /* We do need a copy. build and register it now. If this is a pointer or
332 reference type, remap the designated type and make a new pointer or
333 reference type. */
334 if (TREE_CODE (type) == POINTER_TYPE)
336 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
337 TYPE_MODE (type),
338 TYPE_REF_CAN_ALIAS_ALL (type));
339 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
340 new_tree = build_type_attribute_qual_variant (new_tree,
341 TYPE_ATTRIBUTES (type),
342 TYPE_QUALS (type));
343 insert_decl_map (id, type, new_tree);
344 return new_tree;
346 else if (TREE_CODE (type) == REFERENCE_TYPE)
348 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
349 TYPE_MODE (type),
350 TYPE_REF_CAN_ALIAS_ALL (type));
351 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
352 new_tree = build_type_attribute_qual_variant (new_tree,
353 TYPE_ATTRIBUTES (type),
354 TYPE_QUALS (type));
355 insert_decl_map (id, type, new_tree);
356 return new_tree;
358 else
359 new_tree = copy_node (type);
361 insert_decl_map (id, type, new_tree);
363 /* This is a new type, not a copy of an old type. Need to reassociate
364 variants. We can handle everything except the main variant lazily. */
365 t = TYPE_MAIN_VARIANT (type);
366 if (type != t)
368 t = remap_type (t, id);
369 TYPE_MAIN_VARIANT (new_tree) = t;
370 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
371 TYPE_NEXT_VARIANT (t) = new_tree;
373 else
375 TYPE_MAIN_VARIANT (new_tree) = new_tree;
376 TYPE_NEXT_VARIANT (new_tree) = NULL;
379 if (TYPE_STUB_DECL (type))
380 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
382 /* Lazily create pointer and reference types. */
383 TYPE_POINTER_TO (new_tree) = NULL;
384 TYPE_REFERENCE_TO (new_tree) = NULL;
386 switch (TREE_CODE (new_tree))
388 case INTEGER_TYPE:
389 case REAL_TYPE:
390 case FIXED_POINT_TYPE:
391 case ENUMERAL_TYPE:
392 case BOOLEAN_TYPE:
393 t = TYPE_MIN_VALUE (new_tree);
394 if (t && TREE_CODE (t) != INTEGER_CST)
395 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
397 t = TYPE_MAX_VALUE (new_tree);
398 if (t && TREE_CODE (t) != INTEGER_CST)
399 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
400 return new_tree;
402 case FUNCTION_TYPE:
403 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
404 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
405 return new_tree;
407 case ARRAY_TYPE:
408 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
409 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
410 break;
412 case RECORD_TYPE:
413 case UNION_TYPE:
414 case QUAL_UNION_TYPE:
416 tree f, nf = NULL;
418 for (f = TYPE_FIELDS (new_tree); f ; f = TREE_CHAIN (f))
420 t = remap_decl (f, id);
421 DECL_CONTEXT (t) = new_tree;
422 TREE_CHAIN (t) = nf;
423 nf = t;
425 TYPE_FIELDS (new_tree) = nreverse (nf);
427 break;
429 case OFFSET_TYPE:
430 default:
431 /* Shouldn't have been thought variable sized. */
432 gcc_unreachable ();
435 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
436 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
438 return new_tree;
441 tree
442 remap_type (tree type, copy_body_data *id)
444 tree *node;
445 tree tmp;
447 if (type == NULL)
448 return type;
450 /* See if we have remapped this type. */
451 node = (tree *) pointer_map_contains (id->decl_map, type);
452 if (node)
453 return *node;
455 /* The type only needs remapping if it's variably modified. */
456 if (! variably_modified_type_p (type, id->src_fn))
458 insert_decl_map (id, type, type);
459 return type;
462 id->remapping_type_depth++;
463 tmp = remap_type_1 (type, id);
464 id->remapping_type_depth--;
466 return tmp;
469 /* Return previously remapped type of TYPE in ID. Return NULL if TYPE
470 is NULL or TYPE has not been remapped before. */
472 static tree
473 remapped_type (tree type, copy_body_data *id)
475 tree *node;
477 if (type == NULL)
478 return type;
480 /* See if we have remapped this type. */
481 node = (tree *) pointer_map_contains (id->decl_map, type);
482 if (node)
483 return *node;
484 else
485 return NULL;
488 /* The type only needs remapping if it's variably modified. */
489 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
491 static bool
492 can_be_nonlocal (tree decl, copy_body_data *id)
494 /* We can not duplicate function decls. */
495 if (TREE_CODE (decl) == FUNCTION_DECL)
496 return true;
498 /* Local static vars must be non-local or we get multiple declaration
499 problems. */
500 if (TREE_CODE (decl) == VAR_DECL
501 && !auto_var_in_fn_p (decl, id->src_fn))
502 return true;
504 /* At the moment dwarf2out can handle only these types of nodes. We
505 can support more later. */
506 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
507 return false;
509 /* We must use global type. We call remapped_type instead of
510 remap_type since we don't want to remap this type here if it
511 hasn't been remapped before. */
512 if (TREE_TYPE (decl) != remapped_type (TREE_TYPE (decl), id))
513 return false;
515 /* Wihtout SSA we can't tell if variable is used. */
516 if (!gimple_in_ssa_p (cfun))
517 return false;
519 /* Live variables must be copied so we can attach DECL_RTL. */
520 if (var_ann (decl))
521 return false;
523 return true;
526 static tree
527 remap_decls (tree decls, VEC(tree,gc) **nonlocalized_list, copy_body_data *id)
529 tree old_var;
530 tree new_decls = NULL_TREE;
532 /* Remap its variables. */
533 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
535 tree new_var;
537 if (can_be_nonlocal (old_var, id))
539 if (TREE_CODE (old_var) == VAR_DECL
540 && ! DECL_EXTERNAL (old_var)
541 && (var_ann (old_var) || !gimple_in_ssa_p (cfun)))
542 cfun->local_decls = tree_cons (NULL_TREE, old_var,
543 cfun->local_decls);
544 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
545 && !DECL_IGNORED_P (old_var)
546 && nonlocalized_list)
547 VEC_safe_push (tree, gc, *nonlocalized_list, old_var);
548 continue;
551 /* Remap the variable. */
552 new_var = remap_decl (old_var, id);
554 /* If we didn't remap this variable, we can't mess with its
555 TREE_CHAIN. If we remapped this variable to the return slot, it's
556 already declared somewhere else, so don't declare it here. */
558 if (new_var == id->retvar)
560 else if (!new_var)
562 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
563 && !DECL_IGNORED_P (old_var)
564 && nonlocalized_list)
565 VEC_safe_push (tree, gc, *nonlocalized_list, old_var);
567 else
569 gcc_assert (DECL_P (new_var));
570 TREE_CHAIN (new_var) = new_decls;
571 new_decls = new_var;
575 return nreverse (new_decls);
578 /* Copy the BLOCK to contain remapped versions of the variables
579 therein. And hook the new block into the block-tree. */
581 static void
582 remap_block (tree *block, copy_body_data *id)
584 tree old_block;
585 tree new_block;
587 /* Make the new block. */
588 old_block = *block;
589 new_block = make_node (BLOCK);
590 TREE_USED (new_block) = TREE_USED (old_block);
591 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
592 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
593 BLOCK_NONLOCALIZED_VARS (new_block)
594 = VEC_copy (tree, gc, BLOCK_NONLOCALIZED_VARS (old_block));
595 *block = new_block;
597 /* Remap its variables. */
598 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
599 &BLOCK_NONLOCALIZED_VARS (new_block),
600 id);
602 if (id->transform_lang_insert_block)
603 id->transform_lang_insert_block (new_block);
605 /* Remember the remapped block. */
606 insert_decl_map (id, old_block, new_block);
609 /* Copy the whole block tree and root it in id->block. */
610 static tree
611 remap_blocks (tree block, copy_body_data *id)
613 tree t;
614 tree new_tree = block;
616 if (!block)
617 return NULL;
619 remap_block (&new_tree, id);
620 gcc_assert (new_tree != block);
621 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
622 prepend_lexical_block (new_tree, remap_blocks (t, id));
623 /* Blocks are in arbitrary order, but make things slightly prettier and do
624 not swap order when producing a copy. */
625 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
626 return new_tree;
629 static void
630 copy_statement_list (tree *tp)
632 tree_stmt_iterator oi, ni;
633 tree new_tree;
635 new_tree = alloc_stmt_list ();
636 ni = tsi_start (new_tree);
637 oi = tsi_start (*tp);
638 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
639 *tp = new_tree;
641 for (; !tsi_end_p (oi); tsi_next (&oi))
643 tree stmt = tsi_stmt (oi);
644 if (TREE_CODE (stmt) == STATEMENT_LIST)
645 copy_statement_list (&stmt);
646 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
650 static void
651 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
653 tree block = BIND_EXPR_BLOCK (*tp);
654 tree t;
655 /* Copy (and replace) the statement. */
656 copy_tree_r (tp, walk_subtrees, NULL);
657 if (block)
659 remap_block (&block, id);
660 BIND_EXPR_BLOCK (*tp) = block;
663 if (BIND_EXPR_VARS (*tp))
665 /* This will remap a lot of the same decls again, but this should be
666 harmless. */
667 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
669 /* Also copy value-expressions. */
670 for (t = BIND_EXPR_VARS (*tp); t; t = TREE_CHAIN (t))
671 if (TREE_CODE (t) == VAR_DECL
672 && DECL_HAS_VALUE_EXPR_P (t))
674 tree tem = DECL_VALUE_EXPR (t);
675 walk_tree (&tem, copy_tree_body_r, id, NULL);
676 SET_DECL_VALUE_EXPR (t, tem);
682 /* Create a new gimple_seq by remapping all the statements in BODY
683 using the inlining information in ID. */
685 gimple_seq
686 remap_gimple_seq (gimple_seq body, copy_body_data *id)
688 gimple_stmt_iterator si;
689 gimple_seq new_body = NULL;
691 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
693 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
694 gimple_seq_add_stmt (&new_body, new_stmt);
697 return new_body;
701 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
702 block using the mapping information in ID. */
704 static gimple
705 copy_gimple_bind (gimple stmt, copy_body_data *id)
707 gimple new_bind;
708 tree new_block, new_vars;
709 gimple_seq body, new_body;
711 /* Copy the statement. Note that we purposely don't use copy_stmt
712 here because we need to remap statements as we copy. */
713 body = gimple_bind_body (stmt);
714 new_body = remap_gimple_seq (body, id);
716 new_block = gimple_bind_block (stmt);
717 if (new_block)
718 remap_block (&new_block, id);
720 /* This will remap a lot of the same decls again, but this should be
721 harmless. */
722 new_vars = gimple_bind_vars (stmt);
723 if (new_vars)
724 new_vars = remap_decls (new_vars, NULL, id);
726 new_bind = gimple_build_bind (new_vars, new_body, new_block);
728 return new_bind;
732 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
733 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
734 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
735 recursing into the children nodes of *TP. */
737 static tree
738 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
740 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
741 copy_body_data *id = (copy_body_data *) wi_p->info;
742 tree fn = id->src_fn;
744 if (TREE_CODE (*tp) == SSA_NAME)
746 *tp = remap_ssa_name (*tp, id);
747 *walk_subtrees = 0;
748 return NULL;
750 else if (auto_var_in_fn_p (*tp, fn))
752 /* Local variables and labels need to be replaced by equivalent
753 variables. We don't want to copy static variables; there's
754 only one of those, no matter how many times we inline the
755 containing function. Similarly for globals from an outer
756 function. */
757 tree new_decl;
759 /* Remap the declaration. */
760 new_decl = remap_decl (*tp, id);
761 gcc_assert (new_decl);
762 /* Replace this variable with the copy. */
763 STRIP_TYPE_NOPS (new_decl);
764 /* ??? The C++ frontend uses void * pointer zero to initialize
765 any other type. This confuses the middle-end type verification.
766 As cloned bodies do not go through gimplification again the fixup
767 there doesn't trigger. */
768 if (TREE_CODE (new_decl) == INTEGER_CST
769 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
770 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
771 *tp = new_decl;
772 *walk_subtrees = 0;
774 else if (TREE_CODE (*tp) == STATEMENT_LIST)
775 gcc_unreachable ();
776 else if (TREE_CODE (*tp) == SAVE_EXPR)
777 gcc_unreachable ();
778 else if (TREE_CODE (*tp) == LABEL_DECL
779 && (!DECL_CONTEXT (*tp)
780 || decl_function_context (*tp) == id->src_fn))
781 /* These may need to be remapped for EH handling. */
782 *tp = remap_decl (*tp, id);
783 else if (TYPE_P (*tp))
784 /* Types may need remapping as well. */
785 *tp = remap_type (*tp, id);
786 else if (CONSTANT_CLASS_P (*tp))
788 /* If this is a constant, we have to copy the node iff the type
789 will be remapped. copy_tree_r will not copy a constant. */
790 tree new_type = remap_type (TREE_TYPE (*tp), id);
792 if (new_type == TREE_TYPE (*tp))
793 *walk_subtrees = 0;
795 else if (TREE_CODE (*tp) == INTEGER_CST)
796 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
797 TREE_INT_CST_HIGH (*tp));
798 else
800 *tp = copy_node (*tp);
801 TREE_TYPE (*tp) = new_type;
804 else
806 /* Otherwise, just copy the node. Note that copy_tree_r already
807 knows not to copy VAR_DECLs, etc., so this is safe. */
808 if (TREE_CODE (*tp) == INDIRECT_REF)
810 /* Get rid of *& from inline substitutions that can happen when a
811 pointer argument is an ADDR_EXPR. */
812 tree decl = TREE_OPERAND (*tp, 0);
813 tree *n;
815 n = (tree *) pointer_map_contains (id->decl_map, decl);
816 if (n)
818 tree type, new_tree, old;
820 /* If we happen to get an ADDR_EXPR in n->value, strip
821 it manually here as we'll eventually get ADDR_EXPRs
822 which lie about their types pointed to. In this case
823 build_fold_indirect_ref wouldn't strip the
824 INDIRECT_REF, but we absolutely rely on that. As
825 fold_indirect_ref does other useful transformations,
826 try that first, though. */
827 type = TREE_TYPE (TREE_TYPE (*n));
828 new_tree = unshare_expr (*n);
829 old = *tp;
830 *tp = gimple_fold_indirect_ref (new_tree);
831 if (!*tp)
833 if (TREE_CODE (new_tree) == ADDR_EXPR)
835 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
836 type, new_tree);
837 /* ??? We should either assert here or build
838 a VIEW_CONVERT_EXPR instead of blindly leaking
839 incompatible types to our IL. */
840 if (! *tp)
841 *tp = TREE_OPERAND (new_tree, 0);
843 else
845 *tp = build1 (INDIRECT_REF, type, new_tree);
846 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
847 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
850 *walk_subtrees = 0;
851 return NULL;
855 /* Here is the "usual case". Copy this tree node, and then
856 tweak some special cases. */
857 copy_tree_r (tp, walk_subtrees, NULL);
859 /* Global variables we haven't seen yet need to go into referenced
860 vars. If not referenced from types only. */
861 if (gimple_in_ssa_p (cfun)
862 && TREE_CODE (*tp) == VAR_DECL
863 && id->remapping_type_depth == 0
864 && !processing_debug_stmt)
865 add_referenced_var (*tp);
867 /* We should never have TREE_BLOCK set on non-statements. */
868 if (EXPR_P (*tp))
869 gcc_assert (!TREE_BLOCK (*tp));
871 if (TREE_CODE (*tp) != OMP_CLAUSE)
872 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
874 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
876 /* The copied TARGET_EXPR has never been expanded, even if the
877 original node was expanded already. */
878 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
879 TREE_OPERAND (*tp, 3) = NULL_TREE;
881 else if (TREE_CODE (*tp) == ADDR_EXPR)
883 /* Variable substitution need not be simple. In particular,
884 the INDIRECT_REF substitution above. Make sure that
885 TREE_CONSTANT and friends are up-to-date. But make sure
886 to not improperly set TREE_BLOCK on some sub-expressions. */
887 int invariant = is_gimple_min_invariant (*tp);
888 tree block = id->block;
889 id->block = NULL_TREE;
890 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
891 id->block = block;
893 /* Handle the case where we substituted an INDIRECT_REF
894 into the operand of the ADDR_EXPR. */
895 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
896 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
897 else
898 recompute_tree_invariant_for_addr_expr (*tp);
900 /* If this used to be invariant, but is not any longer,
901 then regimplification is probably needed. */
902 if (invariant && !is_gimple_min_invariant (*tp))
903 id->regimplify = true;
905 *walk_subtrees = 0;
909 /* Keep iterating. */
910 return NULL_TREE;
914 /* Called from copy_body_id via walk_tree. DATA is really a
915 `copy_body_data *'. */
917 tree
918 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
920 copy_body_data *id = (copy_body_data *) data;
921 tree fn = id->src_fn;
922 tree new_block;
924 /* Begin by recognizing trees that we'll completely rewrite for the
925 inlining context. Our output for these trees is completely
926 different from out input (e.g. RETURN_EXPR is deleted, and morphs
927 into an edge). Further down, we'll handle trees that get
928 duplicated and/or tweaked. */
930 /* When requested, RETURN_EXPRs should be transformed to just the
931 contained MODIFY_EXPR. The branch semantics of the return will
932 be handled elsewhere by manipulating the CFG rather than a statement. */
933 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
935 tree assignment = TREE_OPERAND (*tp, 0);
937 /* If we're returning something, just turn that into an
938 assignment into the equivalent of the original RESULT_DECL.
939 If the "assignment" is just the result decl, the result
940 decl has already been set (e.g. a recent "foo (&result_decl,
941 ...)"); just toss the entire RETURN_EXPR. */
942 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
944 /* Replace the RETURN_EXPR with (a copy of) the
945 MODIFY_EXPR hanging underneath. */
946 *tp = copy_node (assignment);
948 else /* Else the RETURN_EXPR returns no value. */
950 *tp = NULL;
951 return (tree) (void *)1;
954 else if (TREE_CODE (*tp) == SSA_NAME)
956 *tp = remap_ssa_name (*tp, id);
957 *walk_subtrees = 0;
958 return NULL;
961 /* Local variables and labels need to be replaced by equivalent
962 variables. We don't want to copy static variables; there's only
963 one of those, no matter how many times we inline the containing
964 function. Similarly for globals from an outer function. */
965 else if (auto_var_in_fn_p (*tp, fn))
967 tree new_decl;
969 /* Remap the declaration. */
970 new_decl = remap_decl (*tp, id);
971 gcc_assert (new_decl);
972 /* Replace this variable with the copy. */
973 STRIP_TYPE_NOPS (new_decl);
974 *tp = new_decl;
975 *walk_subtrees = 0;
977 else if (TREE_CODE (*tp) == STATEMENT_LIST)
978 copy_statement_list (tp);
979 else if (TREE_CODE (*tp) == SAVE_EXPR
980 || TREE_CODE (*tp) == TARGET_EXPR)
981 remap_save_expr (tp, id->decl_map, walk_subtrees);
982 else if (TREE_CODE (*tp) == LABEL_DECL
983 && (! DECL_CONTEXT (*tp)
984 || decl_function_context (*tp) == id->src_fn))
985 /* These may need to be remapped for EH handling. */
986 *tp = remap_decl (*tp, id);
987 else if (TREE_CODE (*tp) == BIND_EXPR)
988 copy_bind_expr (tp, walk_subtrees, id);
989 /* Types may need remapping as well. */
990 else if (TYPE_P (*tp))
991 *tp = remap_type (*tp, id);
993 /* If this is a constant, we have to copy the node iff the type will be
994 remapped. copy_tree_r will not copy a constant. */
995 else if (CONSTANT_CLASS_P (*tp))
997 tree new_type = remap_type (TREE_TYPE (*tp), id);
999 if (new_type == TREE_TYPE (*tp))
1000 *walk_subtrees = 0;
1002 else if (TREE_CODE (*tp) == INTEGER_CST)
1003 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
1004 TREE_INT_CST_HIGH (*tp));
1005 else
1007 *tp = copy_node (*tp);
1008 TREE_TYPE (*tp) = new_type;
1012 /* Otherwise, just copy the node. Note that copy_tree_r already
1013 knows not to copy VAR_DECLs, etc., so this is safe. */
1014 else
1016 /* Here we handle trees that are not completely rewritten.
1017 First we detect some inlining-induced bogosities for
1018 discarding. */
1019 if (TREE_CODE (*tp) == MODIFY_EXPR
1020 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
1021 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
1023 /* Some assignments VAR = VAR; don't generate any rtl code
1024 and thus don't count as variable modification. Avoid
1025 keeping bogosities like 0 = 0. */
1026 tree decl = TREE_OPERAND (*tp, 0), value;
1027 tree *n;
1029 n = (tree *) pointer_map_contains (id->decl_map, decl);
1030 if (n)
1032 value = *n;
1033 STRIP_TYPE_NOPS (value);
1034 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1036 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
1037 return copy_tree_body_r (tp, walk_subtrees, data);
1041 else if (TREE_CODE (*tp) == INDIRECT_REF)
1043 /* Get rid of *& from inline substitutions that can happen when a
1044 pointer argument is an ADDR_EXPR. */
1045 tree decl = TREE_OPERAND (*tp, 0);
1046 tree *n;
1048 n = (tree *) pointer_map_contains (id->decl_map, decl);
1049 if (n)
1051 tree new_tree;
1052 tree old;
1053 /* If we happen to get an ADDR_EXPR in n->value, strip
1054 it manually here as we'll eventually get ADDR_EXPRs
1055 which lie about their types pointed to. In this case
1056 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1057 but we absolutely rely on that. As fold_indirect_ref
1058 does other useful transformations, try that first, though. */
1059 tree type = TREE_TYPE (TREE_TYPE (*n));
1060 if (id->do_not_unshare)
1061 new_tree = *n;
1062 else
1063 new_tree = unshare_expr (*n);
1064 old = *tp;
1065 *tp = gimple_fold_indirect_ref (new_tree);
1066 if (! *tp)
1068 if (TREE_CODE (new_tree) == ADDR_EXPR)
1070 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
1071 type, new_tree);
1072 /* ??? We should either assert here or build
1073 a VIEW_CONVERT_EXPR instead of blindly leaking
1074 incompatible types to our IL. */
1075 if (! *tp)
1076 *tp = TREE_OPERAND (new_tree, 0);
1078 else
1080 *tp = build1 (INDIRECT_REF, type, new_tree);
1081 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1082 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1085 *walk_subtrees = 0;
1086 return NULL;
1090 /* Here is the "usual case". Copy this tree node, and then
1091 tweak some special cases. */
1092 copy_tree_r (tp, walk_subtrees, NULL);
1094 /* Global variables we haven't seen yet needs to go into referenced
1095 vars. If not referenced from types or debug stmts only. */
1096 if (gimple_in_ssa_p (cfun)
1097 && TREE_CODE (*tp) == VAR_DECL
1098 && id->remapping_type_depth == 0
1099 && !processing_debug_stmt)
1100 add_referenced_var (*tp);
1102 /* If EXPR has block defined, map it to newly constructed block.
1103 When inlining we want EXPRs without block appear in the block
1104 of function call if we are not remapping a type. */
1105 if (EXPR_P (*tp))
1107 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1108 if (TREE_BLOCK (*tp))
1110 tree *n;
1111 n = (tree *) pointer_map_contains (id->decl_map,
1112 TREE_BLOCK (*tp));
1113 gcc_assert (n);
1114 new_block = *n;
1116 TREE_BLOCK (*tp) = new_block;
1119 if (TREE_CODE (*tp) != OMP_CLAUSE)
1120 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1122 /* The copied TARGET_EXPR has never been expanded, even if the
1123 original node was expanded already. */
1124 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1126 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1127 TREE_OPERAND (*tp, 3) = NULL_TREE;
1130 /* Variable substitution need not be simple. In particular, the
1131 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1132 and friends are up-to-date. */
1133 else if (TREE_CODE (*tp) == ADDR_EXPR)
1135 int invariant = is_gimple_min_invariant (*tp);
1136 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1138 /* Handle the case where we substituted an INDIRECT_REF
1139 into the operand of the ADDR_EXPR. */
1140 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1141 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1142 else
1143 recompute_tree_invariant_for_addr_expr (*tp);
1145 /* If this used to be invariant, but is not any longer,
1146 then regimplification is probably needed. */
1147 if (invariant && !is_gimple_min_invariant (*tp))
1148 id->regimplify = true;
1150 *walk_subtrees = 0;
1154 /* Keep iterating. */
1155 return NULL_TREE;
1158 /* Helper for remap_gimple_stmt. Given an EH region number for the
1159 source function, map that to the duplicate EH region number in
1160 the destination function. */
1162 static int
1163 remap_eh_region_nr (int old_nr, copy_body_data *id)
1165 eh_region old_r, new_r;
1166 void **slot;
1168 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1169 slot = pointer_map_contains (id->eh_map, old_r);
1170 new_r = (eh_region) *slot;
1172 return new_r->index;
1175 /* Similar, but operate on INTEGER_CSTs. */
1177 static tree
1178 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1180 int old_nr, new_nr;
1182 old_nr = tree_low_cst (old_t_nr, 0);
1183 new_nr = remap_eh_region_nr (old_nr, id);
1185 return build_int_cst (NULL, new_nr);
1188 /* Helper for copy_bb. Remap statement STMT using the inlining
1189 information in ID. Return the new statement copy. */
1191 static gimple
1192 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1194 gimple copy = NULL;
1195 struct walk_stmt_info wi;
1196 tree new_block;
1197 bool skip_first = false;
1199 /* Begin by recognizing trees that we'll completely rewrite for the
1200 inlining context. Our output for these trees is completely
1201 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1202 into an edge). Further down, we'll handle trees that get
1203 duplicated and/or tweaked. */
1205 /* When requested, GIMPLE_RETURNs should be transformed to just the
1206 contained GIMPLE_ASSIGN. The branch semantics of the return will
1207 be handled elsewhere by manipulating the CFG rather than the
1208 statement. */
1209 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1211 tree retval = gimple_return_retval (stmt);
1213 /* If we're returning something, just turn that into an
1214 assignment into the equivalent of the original RESULT_DECL.
1215 If RETVAL is just the result decl, the result decl has
1216 already been set (e.g. a recent "foo (&result_decl, ...)");
1217 just toss the entire GIMPLE_RETURN. */
1218 if (retval && TREE_CODE (retval) != RESULT_DECL)
1220 copy = gimple_build_assign (id->retvar, retval);
1221 /* id->retvar is already substituted. Skip it on later remapping. */
1222 skip_first = true;
1224 else
1225 return gimple_build_nop ();
1227 else if (gimple_has_substatements (stmt))
1229 gimple_seq s1, s2;
1231 /* When cloning bodies from the C++ front end, we will be handed bodies
1232 in High GIMPLE form. Handle here all the High GIMPLE statements that
1233 have embedded statements. */
1234 switch (gimple_code (stmt))
1236 case GIMPLE_BIND:
1237 copy = copy_gimple_bind (stmt, id);
1238 break;
1240 case GIMPLE_CATCH:
1241 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1242 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1243 break;
1245 case GIMPLE_EH_FILTER:
1246 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1247 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1248 break;
1250 case GIMPLE_TRY:
1251 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1252 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1253 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1254 break;
1256 case GIMPLE_WITH_CLEANUP_EXPR:
1257 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1258 copy = gimple_build_wce (s1);
1259 break;
1261 case GIMPLE_OMP_PARALLEL:
1262 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1263 copy = gimple_build_omp_parallel
1264 (s1,
1265 gimple_omp_parallel_clauses (stmt),
1266 gimple_omp_parallel_child_fn (stmt),
1267 gimple_omp_parallel_data_arg (stmt));
1268 break;
1270 case GIMPLE_OMP_TASK:
1271 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1272 copy = gimple_build_omp_task
1273 (s1,
1274 gimple_omp_task_clauses (stmt),
1275 gimple_omp_task_child_fn (stmt),
1276 gimple_omp_task_data_arg (stmt),
1277 gimple_omp_task_copy_fn (stmt),
1278 gimple_omp_task_arg_size (stmt),
1279 gimple_omp_task_arg_align (stmt));
1280 break;
1282 case GIMPLE_OMP_FOR:
1283 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1284 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1285 copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1286 gimple_omp_for_collapse (stmt), s2);
1288 size_t i;
1289 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1291 gimple_omp_for_set_index (copy, i,
1292 gimple_omp_for_index (stmt, i));
1293 gimple_omp_for_set_initial (copy, i,
1294 gimple_omp_for_initial (stmt, i));
1295 gimple_omp_for_set_final (copy, i,
1296 gimple_omp_for_final (stmt, i));
1297 gimple_omp_for_set_incr (copy, i,
1298 gimple_omp_for_incr (stmt, i));
1299 gimple_omp_for_set_cond (copy, i,
1300 gimple_omp_for_cond (stmt, i));
1303 break;
1305 case GIMPLE_OMP_MASTER:
1306 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1307 copy = gimple_build_omp_master (s1);
1308 break;
1310 case GIMPLE_OMP_ORDERED:
1311 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1312 copy = gimple_build_omp_ordered (s1);
1313 break;
1315 case GIMPLE_OMP_SECTION:
1316 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1317 copy = gimple_build_omp_section (s1);
1318 break;
1320 case GIMPLE_OMP_SECTIONS:
1321 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1322 copy = gimple_build_omp_sections
1323 (s1, gimple_omp_sections_clauses (stmt));
1324 break;
1326 case GIMPLE_OMP_SINGLE:
1327 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1328 copy = gimple_build_omp_single
1329 (s1, gimple_omp_single_clauses (stmt));
1330 break;
1332 case GIMPLE_OMP_CRITICAL:
1333 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1334 copy
1335 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1336 break;
1338 default:
1339 gcc_unreachable ();
1342 else
1344 if (gimple_assign_copy_p (stmt)
1345 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1346 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1348 /* Here we handle statements that are not completely rewritten.
1349 First we detect some inlining-induced bogosities for
1350 discarding. */
1352 /* Some assignments VAR = VAR; don't generate any rtl code
1353 and thus don't count as variable modification. Avoid
1354 keeping bogosities like 0 = 0. */
1355 tree decl = gimple_assign_lhs (stmt), value;
1356 tree *n;
1358 n = (tree *) pointer_map_contains (id->decl_map, decl);
1359 if (n)
1361 value = *n;
1362 STRIP_TYPE_NOPS (value);
1363 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1364 return gimple_build_nop ();
1368 if (gimple_debug_bind_p (stmt))
1370 copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1371 gimple_debug_bind_get_value (stmt),
1372 stmt);
1373 VEC_safe_push (gimple, heap, id->debug_stmts, copy);
1374 return copy;
1377 /* Create a new deep copy of the statement. */
1378 copy = gimple_copy (stmt);
1380 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1381 RESX and EH_DISPATCH. */
1382 if (id->eh_map)
1383 switch (gimple_code (copy))
1385 case GIMPLE_CALL:
1387 tree r, fndecl = gimple_call_fndecl (copy);
1388 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1389 switch (DECL_FUNCTION_CODE (fndecl))
1391 case BUILT_IN_EH_COPY_VALUES:
1392 r = gimple_call_arg (copy, 1);
1393 r = remap_eh_region_tree_nr (r, id);
1394 gimple_call_set_arg (copy, 1, r);
1395 /* FALLTHRU */
1397 case BUILT_IN_EH_POINTER:
1398 case BUILT_IN_EH_FILTER:
1399 r = gimple_call_arg (copy, 0);
1400 r = remap_eh_region_tree_nr (r, id);
1401 gimple_call_set_arg (copy, 0, r);
1402 break;
1404 default:
1405 break;
1408 break;
1410 case GIMPLE_RESX:
1412 int r = gimple_resx_region (copy);
1413 r = remap_eh_region_nr (r, id);
1414 gimple_resx_set_region (copy, r);
1416 break;
1418 case GIMPLE_EH_DISPATCH:
1420 int r = gimple_eh_dispatch_region (copy);
1421 r = remap_eh_region_nr (r, id);
1422 gimple_eh_dispatch_set_region (copy, r);
1424 break;
1426 default:
1427 break;
1431 /* If STMT has a block defined, map it to the newly constructed
1432 block. When inlining we want statements without a block to
1433 appear in the block of the function call. */
1434 new_block = id->block;
1435 if (gimple_block (copy))
1437 tree *n;
1438 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1439 gcc_assert (n);
1440 new_block = *n;
1443 gimple_set_block (copy, new_block);
1445 if (gimple_debug_bind_p (copy))
1446 return copy;
1448 /* Remap all the operands in COPY. */
1449 memset (&wi, 0, sizeof (wi));
1450 wi.info = id;
1451 if (skip_first)
1452 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1453 else
1454 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1456 /* Clear the copied virtual operands. We are not remapping them here
1457 but are going to recreate them from scratch. */
1458 if (gimple_has_mem_ops (copy))
1460 gimple_set_vdef (copy, NULL_TREE);
1461 gimple_set_vuse (copy, NULL_TREE);
1464 return copy;
1468 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1469 later */
1471 static basic_block
1472 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1473 gcov_type count_scale)
1475 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1476 basic_block copy_basic_block;
1477 tree decl;
1478 gcov_type freq;
1480 /* create_basic_block() will append every new block to
1481 basic_block_info automatically. */
1482 copy_basic_block = create_basic_block (NULL, (void *) 0,
1483 (basic_block) bb->prev_bb->aux);
1484 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1486 /* We are going to rebuild frequencies from scratch. These values
1487 have just small importance to drive canonicalize_loop_headers. */
1488 freq = ((gcov_type)bb->frequency * frequency_scale / REG_BR_PROB_BASE);
1490 /* We recompute frequencies after inlining, so this is quite safe. */
1491 if (freq > BB_FREQ_MAX)
1492 freq = BB_FREQ_MAX;
1493 copy_basic_block->frequency = freq;
1495 copy_gsi = gsi_start_bb (copy_basic_block);
1497 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1499 gimple stmt = gsi_stmt (gsi);
1500 gimple orig_stmt = stmt;
1502 id->regimplify = false;
1503 stmt = remap_gimple_stmt (stmt, id);
1504 if (gimple_nop_p (stmt))
1505 continue;
1507 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1508 seq_gsi = copy_gsi;
1510 /* With return slot optimization we can end up with
1511 non-gimple (foo *)&this->m, fix that here. */
1512 if (is_gimple_assign (stmt)
1513 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1514 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1516 tree new_rhs;
1517 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1518 gimple_assign_rhs1 (stmt),
1519 true, NULL, false, GSI_NEW_STMT);
1520 gimple_assign_set_rhs1 (stmt, new_rhs);
1521 id->regimplify = false;
1524 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1526 if (id->regimplify)
1527 gimple_regimplify_operands (stmt, &seq_gsi);
1529 /* If copy_basic_block has been empty at the start of this iteration,
1530 call gsi_start_bb again to get at the newly added statements. */
1531 if (gsi_end_p (copy_gsi))
1532 copy_gsi = gsi_start_bb (copy_basic_block);
1533 else
1534 gsi_next (&copy_gsi);
1536 /* Process the new statement. The call to gimple_regimplify_operands
1537 possibly turned the statement into multiple statements, we
1538 need to process all of them. */
1541 tree fn;
1543 stmt = gsi_stmt (copy_gsi);
1544 if (is_gimple_call (stmt)
1545 && gimple_call_va_arg_pack_p (stmt)
1546 && id->gimple_call)
1548 /* __builtin_va_arg_pack () should be replaced by
1549 all arguments corresponding to ... in the caller. */
1550 tree p;
1551 gimple new_call;
1552 VEC(tree, heap) *argarray;
1553 size_t nargs = gimple_call_num_args (id->gimple_call);
1554 size_t n;
1556 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1557 nargs--;
1559 /* Create the new array of arguments. */
1560 n = nargs + gimple_call_num_args (stmt);
1561 argarray = VEC_alloc (tree, heap, n);
1562 VEC_safe_grow (tree, heap, argarray, n);
1564 /* Copy all the arguments before '...' */
1565 memcpy (VEC_address (tree, argarray),
1566 gimple_call_arg_ptr (stmt, 0),
1567 gimple_call_num_args (stmt) * sizeof (tree));
1569 /* Append the arguments passed in '...' */
1570 memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1571 gimple_call_arg_ptr (id->gimple_call, 0)
1572 + (gimple_call_num_args (id->gimple_call) - nargs),
1573 nargs * sizeof (tree));
1575 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1576 argarray);
1578 VEC_free (tree, heap, argarray);
1580 /* Copy all GIMPLE_CALL flags, location and block, except
1581 GF_CALL_VA_ARG_PACK. */
1582 gimple_call_copy_flags (new_call, stmt);
1583 gimple_call_set_va_arg_pack (new_call, false);
1584 gimple_set_location (new_call, gimple_location (stmt));
1585 gimple_set_block (new_call, gimple_block (stmt));
1586 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1588 gsi_replace (&copy_gsi, new_call, false);
1589 gimple_set_bb (stmt, NULL);
1590 stmt = new_call;
1592 else if (is_gimple_call (stmt)
1593 && id->gimple_call
1594 && (decl = gimple_call_fndecl (stmt))
1595 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1596 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1598 /* __builtin_va_arg_pack_len () should be replaced by
1599 the number of anonymous arguments. */
1600 size_t nargs = gimple_call_num_args (id->gimple_call);
1601 tree count, p;
1602 gimple new_stmt;
1604 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1605 nargs--;
1607 count = build_int_cst (integer_type_node, nargs);
1608 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1609 gsi_replace (&copy_gsi, new_stmt, false);
1610 stmt = new_stmt;
1613 /* Statements produced by inlining can be unfolded, especially
1614 when we constant propagated some operands. We can't fold
1615 them right now for two reasons:
1616 1) folding require SSA_NAME_DEF_STMTs to be correct
1617 2) we can't change function calls to builtins.
1618 So we just mark statement for later folding. We mark
1619 all new statements, instead just statements that has changed
1620 by some nontrivial substitution so even statements made
1621 foldable indirectly are updated. If this turns out to be
1622 expensive, copy_body can be told to watch for nontrivial
1623 changes. */
1624 if (id->statements_to_fold)
1625 pointer_set_insert (id->statements_to_fold, stmt);
1627 /* We're duplicating a CALL_EXPR. Find any corresponding
1628 callgraph edges and update or duplicate them. */
1629 if (is_gimple_call (stmt))
1631 struct cgraph_edge *edge;
1632 int flags;
1634 switch (id->transform_call_graph_edges)
1636 case CB_CGE_DUPLICATE:
1637 edge = cgraph_edge (id->src_node, orig_stmt);
1638 if (edge)
1640 int edge_freq = edge->frequency;
1641 edge = cgraph_clone_edge (edge, id->dst_node, stmt,
1642 gimple_uid (stmt),
1643 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1644 edge->frequency, true);
1645 /* We could also just rescale the frequency, but
1646 doing so would introduce roundoff errors and make
1647 verifier unhappy. */
1648 edge->frequency
1649 = compute_call_stmt_bb_frequency (id->dst_node->decl,
1650 copy_basic_block);
1651 if (dump_file
1652 && profile_status_for_function (cfun) != PROFILE_ABSENT
1653 && (edge_freq > edge->frequency + 10
1654 || edge_freq < edge->frequency - 10))
1656 fprintf (dump_file, "Edge frequency estimated by "
1657 "cgraph %i diverge from inliner's estimate %i\n",
1658 edge_freq,
1659 edge->frequency);
1660 fprintf (dump_file,
1661 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
1662 bb->index,
1663 bb->frequency,
1664 copy_basic_block->frequency);
1666 stmt = cgraph_redirect_edge_call_stmt_to_callee (edge);
1668 break;
1670 case CB_CGE_MOVE_CLONES:
1671 cgraph_set_call_stmt_including_clones (id->dst_node,
1672 orig_stmt, stmt);
1673 edge = cgraph_edge (id->dst_node, stmt);
1674 break;
1676 case CB_CGE_MOVE:
1677 edge = cgraph_edge (id->dst_node, orig_stmt);
1678 if (edge)
1679 cgraph_set_call_stmt (edge, stmt);
1680 break;
1682 default:
1683 gcc_unreachable ();
1686 /* Constant propagation on argument done during inlining
1687 may create new direct call. Produce an edge for it. */
1688 if ((!edge
1689 || (edge->indirect_call
1690 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1691 && is_gimple_call (stmt)
1692 && (fn = gimple_call_fndecl (stmt)) != NULL)
1694 struct cgraph_node *dest = cgraph_node (fn);
1696 /* We have missing edge in the callgraph. This can happen
1697 when previous inlining turned an indirect call into a
1698 direct call by constant propagating arguments or we are
1699 producing dead clone (for further clonning). In all
1700 other cases we hit a bug (incorrect node sharing is the
1701 most common reason for missing edges). */
1702 gcc_assert (dest->needed || !dest->analyzed
1703 || !id->src_node->analyzed);
1704 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1705 cgraph_create_edge_including_clones
1706 (id->dst_node, dest, orig_stmt, stmt, bb->count,
1707 compute_call_stmt_bb_frequency (id->dst_node->decl,
1708 copy_basic_block),
1709 bb->loop_depth, CIF_ORIGINALLY_INDIRECT_CALL);
1710 else
1711 cgraph_create_edge (id->dst_node, dest, stmt,
1712 bb->count,
1713 compute_call_stmt_bb_frequency
1714 (id->dst_node->decl, copy_basic_block),
1715 bb->loop_depth)->inline_failed
1716 = CIF_ORIGINALLY_INDIRECT_CALL;
1717 if (dump_file)
1719 fprintf (dump_file, "Created new direct edge to %s",
1720 cgraph_node_name (dest));
1724 flags = gimple_call_flags (stmt);
1725 if (flags & ECF_MAY_BE_ALLOCA)
1726 cfun->calls_alloca = true;
1727 if (flags & ECF_RETURNS_TWICE)
1728 cfun->calls_setjmp = true;
1731 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
1732 id->eh_map, id->eh_lp_nr);
1734 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
1736 ssa_op_iter i;
1737 tree def;
1739 find_new_referenced_vars (gsi_stmt (copy_gsi));
1740 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1741 if (TREE_CODE (def) == SSA_NAME)
1742 SSA_NAME_DEF_STMT (def) = stmt;
1745 gsi_next (&copy_gsi);
1747 while (!gsi_end_p (copy_gsi));
1749 copy_gsi = gsi_last_bb (copy_basic_block);
1752 return copy_basic_block;
1755 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1756 form is quite easy, since dominator relationship for old basic blocks does
1757 not change.
1759 There is however exception where inlining might change dominator relation
1760 across EH edges from basic block within inlined functions destinating
1761 to landing pads in function we inline into.
1763 The function fills in PHI_RESULTs of such PHI nodes if they refer
1764 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1765 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1766 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1767 set, and this means that there will be no overlapping live ranges
1768 for the underlying symbol.
1770 This might change in future if we allow redirecting of EH edges and
1771 we might want to change way build CFG pre-inlining to include
1772 all the possible edges then. */
1773 static void
1774 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1775 bool can_throw, bool nonlocal_goto)
1777 edge e;
1778 edge_iterator ei;
1780 FOR_EACH_EDGE (e, ei, bb->succs)
1781 if (!e->dest->aux
1782 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1784 gimple phi;
1785 gimple_stmt_iterator si;
1787 if (!nonlocal_goto)
1788 gcc_assert (e->flags & EDGE_EH);
1790 if (!can_throw)
1791 gcc_assert (!(e->flags & EDGE_EH));
1793 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1795 edge re;
1797 phi = gsi_stmt (si);
1799 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1800 gcc_assert (!e->dest->aux);
1802 gcc_assert ((e->flags & EDGE_EH)
1803 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1805 if (!is_gimple_reg (PHI_RESULT (phi)))
1807 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1808 continue;
1811 re = find_edge (ret_bb, e->dest);
1812 gcc_assert (re);
1813 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1814 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1816 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1817 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1823 /* Copy edges from BB into its copy constructed earlier, scale profile
1824 accordingly. Edges will be taken care of later. Assume aux
1825 pointers to point to the copies of each BB. */
1827 static void
1828 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1830 basic_block new_bb = (basic_block) bb->aux;
1831 edge_iterator ei;
1832 edge old_edge;
1833 gimple_stmt_iterator si;
1834 int flags;
1836 /* Use the indices from the original blocks to create edges for the
1837 new ones. */
1838 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1839 if (!(old_edge->flags & EDGE_EH))
1841 edge new_edge;
1843 flags = old_edge->flags;
1845 /* Return edges do get a FALLTHRU flag when the get inlined. */
1846 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1847 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1848 flags |= EDGE_FALLTHRU;
1849 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1850 new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1851 new_edge->probability = old_edge->probability;
1854 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1855 return;
1857 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1859 gimple copy_stmt;
1860 bool can_throw, nonlocal_goto;
1862 copy_stmt = gsi_stmt (si);
1863 if (!is_gimple_debug (copy_stmt))
1865 update_stmt (copy_stmt);
1866 if (gimple_in_ssa_p (cfun))
1867 mark_symbols_for_renaming (copy_stmt);
1870 /* Do this before the possible split_block. */
1871 gsi_next (&si);
1873 /* If this tree could throw an exception, there are two
1874 cases where we need to add abnormal edge(s): the
1875 tree wasn't in a region and there is a "current
1876 region" in the caller; or the original tree had
1877 EH edges. In both cases split the block after the tree,
1878 and add abnormal edge(s) as needed; we need both
1879 those from the callee and the caller.
1880 We check whether the copy can throw, because the const
1881 propagation can change an INDIRECT_REF which throws
1882 into a COMPONENT_REF which doesn't. If the copy
1883 can throw, the original could also throw. */
1884 can_throw = stmt_can_throw_internal (copy_stmt);
1885 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1887 if (can_throw || nonlocal_goto)
1889 if (!gsi_end_p (si))
1890 /* Note that bb's predecessor edges aren't necessarily
1891 right at this point; split_block doesn't care. */
1893 edge e = split_block (new_bb, copy_stmt);
1895 new_bb = e->dest;
1896 new_bb->aux = e->src->aux;
1897 si = gsi_start_bb (new_bb);
1901 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
1902 make_eh_dispatch_edges (copy_stmt);
1903 else if (can_throw)
1904 make_eh_edges (copy_stmt);
1906 if (nonlocal_goto)
1907 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1909 if ((can_throw || nonlocal_goto)
1910 && gimple_in_ssa_p (cfun))
1911 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1912 can_throw, nonlocal_goto);
1916 /* Copy the PHIs. All blocks and edges are copied, some blocks
1917 was possibly split and new outgoing EH edges inserted.
1918 BB points to the block of original function and AUX pointers links
1919 the original and newly copied blocks. */
1921 static void
1922 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1924 basic_block const new_bb = (basic_block) bb->aux;
1925 edge_iterator ei;
1926 gimple phi;
1927 gimple_stmt_iterator si;
1929 for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1931 tree res, new_res;
1932 gimple new_phi;
1933 edge new_edge;
1935 phi = gsi_stmt (si);
1936 res = PHI_RESULT (phi);
1937 new_res = res;
1938 if (is_gimple_reg (res))
1940 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1941 SSA_NAME_DEF_STMT (new_res)
1942 = new_phi = create_phi_node (new_res, new_bb);
1943 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1945 edge const old_edge
1946 = find_edge ((basic_block) new_edge->src->aux, bb);
1947 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1948 tree new_arg = arg;
1949 tree block = id->block;
1950 id->block = NULL_TREE;
1951 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1952 id->block = block;
1953 gcc_assert (new_arg);
1954 /* With return slot optimization we can end up with
1955 non-gimple (foo *)&this->m, fix that here. */
1956 if (TREE_CODE (new_arg) != SSA_NAME
1957 && TREE_CODE (new_arg) != FUNCTION_DECL
1958 && !is_gimple_val (new_arg))
1960 gimple_seq stmts = NULL;
1961 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1962 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1964 add_phi_arg (new_phi, new_arg, new_edge,
1965 gimple_phi_arg_location_from_edge (phi, old_edge));
1972 /* Wrapper for remap_decl so it can be used as a callback. */
1974 static tree
1975 remap_decl_1 (tree decl, void *data)
1977 return remap_decl (decl, (copy_body_data *) data);
1980 /* Build struct function and associated datastructures for the new clone
1981 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1983 static void
1984 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
1986 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1987 gcov_type count_scale;
1989 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1990 count_scale = (REG_BR_PROB_BASE * count
1991 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1992 else
1993 count_scale = REG_BR_PROB_BASE;
1995 /* Register specific tree functions. */
1996 gimple_register_cfg_hooks ();
1998 /* Get clean struct function. */
1999 push_struct_function (new_fndecl);
2001 /* We will rebuild these, so just sanity check that they are empty. */
2002 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
2003 gcc_assert (cfun->local_decls == NULL);
2004 gcc_assert (cfun->cfg == NULL);
2005 gcc_assert (cfun->decl == new_fndecl);
2007 /* Copy items we preserve during clonning. */
2008 cfun->static_chain_decl = src_cfun->static_chain_decl;
2009 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
2010 cfun->function_end_locus = src_cfun->function_end_locus;
2011 cfun->curr_properties = src_cfun->curr_properties;
2012 cfun->last_verified = src_cfun->last_verified;
2013 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2014 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
2015 cfun->function_frequency = src_cfun->function_frequency;
2016 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2017 cfun->stdarg = src_cfun->stdarg;
2018 cfun->dont_save_pending_sizes_p = src_cfun->dont_save_pending_sizes_p;
2019 cfun->after_inlining = src_cfun->after_inlining;
2020 cfun->returns_struct = src_cfun->returns_struct;
2021 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
2022 cfun->after_tree_profile = src_cfun->after_tree_profile;
2024 init_empty_tree_cfg ();
2026 profile_status_for_function (cfun) = profile_status_for_function (src_cfun);
2027 ENTRY_BLOCK_PTR->count =
2028 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2029 REG_BR_PROB_BASE);
2030 ENTRY_BLOCK_PTR->frequency
2031 = ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2032 EXIT_BLOCK_PTR->count =
2033 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2034 REG_BR_PROB_BASE);
2035 EXIT_BLOCK_PTR->frequency =
2036 EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2037 if (src_cfun->eh)
2038 init_eh_for_function ();
2040 if (src_cfun->gimple_df)
2042 init_tree_ssa (cfun);
2043 cfun->gimple_df->in_ssa_p = true;
2044 init_ssa_operands ();
2046 pop_cfun ();
2049 /* Make a copy of the body of FN so that it can be inserted inline in
2050 another function. Walks FN via CFG, returns new fndecl. */
2052 static tree
2053 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
2054 basic_block entry_block_map, basic_block exit_block_map)
2056 tree callee_fndecl = id->src_fn;
2057 /* Original cfun for the callee, doesn't change. */
2058 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2059 struct function *cfun_to_copy;
2060 basic_block bb;
2061 tree new_fndecl = NULL;
2062 gcov_type count_scale;
2063 int last;
2065 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
2066 count_scale = (REG_BR_PROB_BASE * count
2067 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
2068 else
2069 count_scale = REG_BR_PROB_BASE;
2071 /* Register specific tree functions. */
2072 gimple_register_cfg_hooks ();
2074 /* Must have a CFG here at this point. */
2075 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
2076 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2078 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2080 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
2081 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
2082 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2083 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2085 /* Duplicate any exception-handling regions. */
2086 if (cfun->eh)
2087 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2088 remap_decl_1, id);
2090 /* Use aux pointers to map the original blocks to copy. */
2091 FOR_EACH_BB_FN (bb, cfun_to_copy)
2093 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2094 bb->aux = new_bb;
2095 new_bb->aux = bb;
2098 last = last_basic_block;
2100 /* Now that we've duplicated the blocks, duplicate their edges. */
2101 FOR_ALL_BB_FN (bb, cfun_to_copy)
2102 copy_edges_for_bb (bb, count_scale, exit_block_map);
2104 if (gimple_in_ssa_p (cfun))
2105 FOR_ALL_BB_FN (bb, cfun_to_copy)
2106 copy_phis_for_bb (bb, id);
2108 FOR_ALL_BB_FN (bb, cfun_to_copy)
2110 ((basic_block)bb->aux)->aux = NULL;
2111 bb->aux = NULL;
2114 /* Zero out AUX fields of newly created block during EH edge
2115 insertion. */
2116 for (; last < last_basic_block; last++)
2117 BASIC_BLOCK (last)->aux = NULL;
2118 entry_block_map->aux = NULL;
2119 exit_block_map->aux = NULL;
2121 if (id->eh_map)
2123 pointer_map_destroy (id->eh_map);
2124 id->eh_map = NULL;
2127 return new_fndecl;
2130 /* Copy the debug STMT using ID. We deal with these statements in a
2131 special way: if any variable in their VALUE expression wasn't
2132 remapped yet, we won't remap it, because that would get decl uids
2133 out of sync, causing codegen differences between -g and -g0. If
2134 this arises, we drop the VALUE expression altogether. */
2136 static void
2137 copy_debug_stmt (gimple stmt, copy_body_data *id)
2139 tree t, *n;
2140 struct walk_stmt_info wi;
2142 t = id->block;
2143 if (gimple_block (stmt))
2145 tree *n;
2146 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (stmt));
2147 if (n)
2148 t = *n;
2150 gimple_set_block (stmt, t);
2152 /* Remap all the operands in COPY. */
2153 memset (&wi, 0, sizeof (wi));
2154 wi.info = id;
2156 processing_debug_stmt = 1;
2158 t = gimple_debug_bind_get_var (stmt);
2160 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2161 && (n = (tree *) pointer_map_contains (id->debug_map, t)))
2163 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2164 t = *n;
2166 else if (TREE_CODE (t) == VAR_DECL
2167 && !TREE_STATIC (t)
2168 && gimple_in_ssa_p (cfun)
2169 && !pointer_map_contains (id->decl_map, t)
2170 && !var_ann (t))
2171 /* T is a non-localized variable. */;
2172 else
2173 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2175 gimple_debug_bind_set_var (stmt, t);
2177 if (gimple_debug_bind_has_value_p (stmt))
2178 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2179 remap_gimple_op_r, &wi, NULL);
2181 /* Punt if any decl couldn't be remapped. */
2182 if (processing_debug_stmt < 0)
2183 gimple_debug_bind_reset_value (stmt);
2185 processing_debug_stmt = 0;
2187 update_stmt (stmt);
2188 if (gimple_in_ssa_p (cfun))
2189 mark_symbols_for_renaming (stmt);
2192 /* Process deferred debug stmts. In order to give values better odds
2193 of being successfully remapped, we delay the processing of debug
2194 stmts until all other stmts that might require remapping are
2195 processed. */
2197 static void
2198 copy_debug_stmts (copy_body_data *id)
2200 size_t i;
2201 gimple stmt;
2203 if (!id->debug_stmts)
2204 return;
2206 for (i = 0; VEC_iterate (gimple, id->debug_stmts, i, stmt); i++)
2207 copy_debug_stmt (stmt, id);
2209 VEC_free (gimple, heap, id->debug_stmts);
2212 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2213 another function. */
2215 static tree
2216 copy_tree_body (copy_body_data *id)
2218 tree fndecl = id->src_fn;
2219 tree body = DECL_SAVED_TREE (fndecl);
2221 walk_tree (&body, copy_tree_body_r, id, NULL);
2223 return body;
2226 /* Make a copy of the body of FN so that it can be inserted inline in
2227 another function. */
2229 static tree
2230 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
2231 basic_block entry_block_map, basic_block exit_block_map)
2233 tree fndecl = id->src_fn;
2234 tree body;
2236 /* If this body has a CFG, walk CFG and copy. */
2237 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
2238 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map);
2239 copy_debug_stmts (id);
2241 return body;
2244 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2245 defined in function FN, or of a data member thereof. */
2247 static bool
2248 self_inlining_addr_expr (tree value, tree fn)
2250 tree var;
2252 if (TREE_CODE (value) != ADDR_EXPR)
2253 return false;
2255 var = get_base_address (TREE_OPERAND (value, 0));
2257 return var && auto_var_in_fn_p (var, fn);
2260 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2261 lexical block and line number information from base_stmt, if given,
2262 or from the last stmt of the block otherwise. */
2264 static gimple
2265 insert_init_debug_bind (copy_body_data *id,
2266 basic_block bb, tree var, tree value,
2267 gimple base_stmt)
2269 gimple note;
2270 gimple_stmt_iterator gsi;
2271 tree tracked_var;
2273 if (!gimple_in_ssa_p (id->src_cfun))
2274 return NULL;
2276 if (!MAY_HAVE_DEBUG_STMTS)
2277 return NULL;
2279 tracked_var = target_for_debug_bind (var);
2280 if (!tracked_var)
2281 return NULL;
2283 if (bb)
2285 gsi = gsi_last_bb (bb);
2286 if (!base_stmt && !gsi_end_p (gsi))
2287 base_stmt = gsi_stmt (gsi);
2290 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
2292 if (bb)
2294 if (!gsi_end_p (gsi))
2295 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
2296 else
2297 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
2300 return note;
2303 static void
2304 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
2306 /* If VAR represents a zero-sized variable, it's possible that the
2307 assignment statement may result in no gimple statements. */
2308 if (init_stmt)
2310 gimple_stmt_iterator si = gsi_last_bb (bb);
2312 /* We can end up with init statements that store to a non-register
2313 from a rhs with a conversion. Handle that here by forcing the
2314 rhs into a temporary. gimple_regimplify_operands is not
2315 prepared to do this for us. */
2316 if (!is_gimple_debug (init_stmt)
2317 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
2318 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2319 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2321 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2322 gimple_expr_type (init_stmt),
2323 gimple_assign_rhs1 (init_stmt));
2324 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
2325 GSI_NEW_STMT);
2326 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
2327 gimple_assign_set_rhs1 (init_stmt, rhs);
2329 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2330 gimple_regimplify_operands (init_stmt, &si);
2331 mark_symbols_for_renaming (init_stmt);
2333 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2335 tree var, def = gimple_assign_lhs (init_stmt);
2337 if (TREE_CODE (def) == SSA_NAME)
2338 var = SSA_NAME_VAR (def);
2339 else
2340 var = def;
2342 insert_init_debug_bind (id, bb, var, def, init_stmt);
2347 /* Initialize parameter P with VALUE. If needed, produce init statement
2348 at the end of BB. When BB is NULL, we return init statement to be
2349 output later. */
2350 static gimple
2351 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2352 basic_block bb, tree *vars)
2354 gimple init_stmt = NULL;
2355 tree var;
2356 tree rhs = value;
2357 tree def = (gimple_in_ssa_p (cfun)
2358 ? gimple_default_def (id->src_cfun, p) : NULL);
2360 if (value
2361 && value != error_mark_node
2362 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
2364 if (fold_convertible_p (TREE_TYPE (p), value))
2365 rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
2366 else
2367 /* ??? For valid (GIMPLE) programs we should not end up here.
2368 Still if something has gone wrong and we end up with truly
2369 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
2370 to not leak invalid GIMPLE to the following passes. */
2371 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2374 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2375 here since the type of this decl must be visible to the calling
2376 function. */
2377 var = copy_decl_to_var (p, id);
2379 /* We're actually using the newly-created var. */
2380 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2382 get_var_ann (var);
2383 add_referenced_var (var);
2386 /* Declare this new variable. */
2387 TREE_CHAIN (var) = *vars;
2388 *vars = var;
2390 /* Make gimplifier happy about this variable. */
2391 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2393 /* If the parameter is never assigned to, has no SSA_NAMEs created,
2394 we would not need to create a new variable here at all, if it
2395 weren't for debug info. Still, we can just use the argument
2396 value. */
2397 if (TREE_READONLY (p)
2398 && !TREE_ADDRESSABLE (p)
2399 && value && !TREE_SIDE_EFFECTS (value)
2400 && !def)
2402 /* We may produce non-gimple trees by adding NOPs or introduce
2403 invalid sharing when operand is not really constant.
2404 It is not big deal to prohibit constant propagation here as
2405 we will constant propagate in DOM1 pass anyway. */
2406 if (is_gimple_min_invariant (value)
2407 && useless_type_conversion_p (TREE_TYPE (p),
2408 TREE_TYPE (value))
2409 /* We have to be very careful about ADDR_EXPR. Make sure
2410 the base variable isn't a local variable of the inlined
2411 function, e.g., when doing recursive inlining, direct or
2412 mutually-recursive or whatever, which is why we don't
2413 just test whether fn == current_function_decl. */
2414 && ! self_inlining_addr_expr (value, fn))
2416 insert_decl_map (id, p, value);
2417 insert_debug_decl_map (id, p, var);
2418 return insert_init_debug_bind (id, bb, var, value, NULL);
2422 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2423 that way, when the PARM_DECL is encountered, it will be
2424 automatically replaced by the VAR_DECL. */
2425 insert_decl_map (id, p, var);
2427 /* Even if P was TREE_READONLY, the new VAR should not be.
2428 In the original code, we would have constructed a
2429 temporary, and then the function body would have never
2430 changed the value of P. However, now, we will be
2431 constructing VAR directly. The constructor body may
2432 change its value multiple times as it is being
2433 constructed. Therefore, it must not be TREE_READONLY;
2434 the back-end assumes that TREE_READONLY variable is
2435 assigned to only once. */
2436 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2437 TREE_READONLY (var) = 0;
2439 /* If there is no setup required and we are in SSA, take the easy route
2440 replacing all SSA names representing the function parameter by the
2441 SSA name passed to function.
2443 We need to construct map for the variable anyway as it might be used
2444 in different SSA names when parameter is set in function.
2446 Do replacement at -O0 for const arguments replaced by constant.
2447 This is important for builtin_constant_p and other construct requiring
2448 constant argument to be visible in inlined function body. */
2449 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2450 && (optimize
2451 || (TREE_READONLY (p)
2452 && is_gimple_min_invariant (rhs)))
2453 && (TREE_CODE (rhs) == SSA_NAME
2454 || is_gimple_min_invariant (rhs))
2455 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2457 insert_decl_map (id, def, rhs);
2458 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2461 /* If the value of argument is never used, don't care about initializing
2462 it. */
2463 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2465 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2466 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2469 /* Initialize this VAR_DECL from the equivalent argument. Convert
2470 the argument to the proper type in case it was promoted. */
2471 if (value)
2473 if (rhs == error_mark_node)
2475 insert_decl_map (id, p, var);
2476 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2479 STRIP_USELESS_TYPE_CONVERSION (rhs);
2481 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2482 keep our trees in gimple form. */
2483 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2485 def = remap_ssa_name (def, id);
2486 init_stmt = gimple_build_assign (def, rhs);
2487 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2488 set_default_def (var, NULL);
2490 else
2491 init_stmt = gimple_build_assign (var, rhs);
2493 if (bb && init_stmt)
2494 insert_init_stmt (id, bb, init_stmt);
2496 return init_stmt;
2499 /* Generate code to initialize the parameters of the function at the
2500 top of the stack in ID from the GIMPLE_CALL STMT. */
2502 static void
2503 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2504 tree fn, basic_block bb)
2506 tree parms;
2507 size_t i;
2508 tree p;
2509 tree vars = NULL_TREE;
2510 tree static_chain = gimple_call_chain (stmt);
2512 /* Figure out what the parameters are. */
2513 parms = DECL_ARGUMENTS (fn);
2515 /* Loop through the parameter declarations, replacing each with an
2516 equivalent VAR_DECL, appropriately initialized. */
2517 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2519 tree val;
2520 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2521 setup_one_parameter (id, p, val, fn, bb, &vars);
2524 /* Initialize the static chain. */
2525 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2526 gcc_assert (fn != current_function_decl);
2527 if (p)
2529 /* No static chain? Seems like a bug in tree-nested.c. */
2530 gcc_assert (static_chain);
2532 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2535 declare_inline_vars (id->block, vars);
2539 /* Declare a return variable to replace the RESULT_DECL for the
2540 function we are calling. An appropriate DECL_STMT is returned.
2541 The USE_STMT is filled to contain a use of the declaration to
2542 indicate the return value of the function.
2544 RETURN_SLOT, if non-null is place where to store the result. It
2545 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2546 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2548 The return value is a (possibly null) value that holds the result
2549 as seen by the caller. */
2551 static tree
2552 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest)
2554 tree callee = id->src_fn;
2555 tree caller = id->dst_fn;
2556 tree result = DECL_RESULT (callee);
2557 tree callee_type = TREE_TYPE (result);
2558 tree caller_type;
2559 tree var, use;
2561 /* Handle type-mismatches in the function declaration return type
2562 vs. the call expression. */
2563 if (modify_dest)
2564 caller_type = TREE_TYPE (modify_dest);
2565 else
2566 caller_type = TREE_TYPE (TREE_TYPE (callee));
2568 /* We don't need to do anything for functions that don't return
2569 anything. */
2570 if (!result || VOID_TYPE_P (callee_type))
2571 return NULL_TREE;
2573 /* If there was a return slot, then the return value is the
2574 dereferenced address of that object. */
2575 if (return_slot)
2577 /* The front end shouldn't have used both return_slot and
2578 a modify expression. */
2579 gcc_assert (!modify_dest);
2580 if (DECL_BY_REFERENCE (result))
2582 tree return_slot_addr = build_fold_addr_expr (return_slot);
2583 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2585 /* We are going to construct *&return_slot and we can't do that
2586 for variables believed to be not addressable.
2588 FIXME: This check possibly can match, because values returned
2589 via return slot optimization are not believed to have address
2590 taken by alias analysis. */
2591 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2592 if (gimple_in_ssa_p (cfun))
2594 HOST_WIDE_INT bitsize;
2595 HOST_WIDE_INT bitpos;
2596 tree offset;
2597 enum machine_mode mode;
2598 int unsignedp;
2599 int volatilep;
2600 tree base;
2601 base = get_inner_reference (return_slot, &bitsize, &bitpos,
2602 &offset,
2603 &mode, &unsignedp, &volatilep,
2604 false);
2605 if (TREE_CODE (base) == INDIRECT_REF)
2606 base = TREE_OPERAND (base, 0);
2607 if (TREE_CODE (base) == SSA_NAME)
2608 base = SSA_NAME_VAR (base);
2609 mark_sym_for_renaming (base);
2611 var = return_slot_addr;
2613 else
2615 var = return_slot;
2616 gcc_assert (TREE_CODE (var) != SSA_NAME);
2617 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2619 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2620 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2621 && !DECL_GIMPLE_REG_P (result)
2622 && DECL_P (var))
2623 DECL_GIMPLE_REG_P (var) = 0;
2624 use = NULL;
2625 goto done;
2628 /* All types requiring non-trivial constructors should have been handled. */
2629 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2631 /* Attempt to avoid creating a new temporary variable. */
2632 if (modify_dest
2633 && TREE_CODE (modify_dest) != SSA_NAME)
2635 bool use_it = false;
2637 /* We can't use MODIFY_DEST if there's type promotion involved. */
2638 if (!useless_type_conversion_p (callee_type, caller_type))
2639 use_it = false;
2641 /* ??? If we're assigning to a variable sized type, then we must
2642 reuse the destination variable, because we've no good way to
2643 create variable sized temporaries at this point. */
2644 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2645 use_it = true;
2647 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2648 reuse it as the result of the call directly. Don't do this if
2649 it would promote MODIFY_DEST to addressable. */
2650 else if (TREE_ADDRESSABLE (result))
2651 use_it = false;
2652 else
2654 tree base_m = get_base_address (modify_dest);
2656 /* If the base isn't a decl, then it's a pointer, and we don't
2657 know where that's going to go. */
2658 if (!DECL_P (base_m))
2659 use_it = false;
2660 else if (is_global_var (base_m))
2661 use_it = false;
2662 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2663 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2664 && !DECL_GIMPLE_REG_P (result)
2665 && DECL_GIMPLE_REG_P (base_m))
2666 use_it = false;
2667 else if (!TREE_ADDRESSABLE (base_m))
2668 use_it = true;
2671 if (use_it)
2673 var = modify_dest;
2674 use = NULL;
2675 goto done;
2679 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2681 var = copy_result_decl_to_var (result, id);
2682 if (gimple_in_ssa_p (cfun))
2684 get_var_ann (var);
2685 add_referenced_var (var);
2688 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2689 DECL_STRUCT_FUNCTION (caller)->local_decls
2690 = tree_cons (NULL_TREE, var,
2691 DECL_STRUCT_FUNCTION (caller)->local_decls);
2693 /* Do not have the rest of GCC warn about this variable as it should
2694 not be visible to the user. */
2695 TREE_NO_WARNING (var) = 1;
2697 declare_inline_vars (id->block, var);
2699 /* Build the use expr. If the return type of the function was
2700 promoted, convert it back to the expected type. */
2701 use = var;
2702 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2703 use = fold_convert (caller_type, var);
2705 STRIP_USELESS_TYPE_CONVERSION (use);
2707 if (DECL_BY_REFERENCE (result))
2709 TREE_ADDRESSABLE (var) = 1;
2710 var = build_fold_addr_expr (var);
2713 done:
2714 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2715 way, when the RESULT_DECL is encountered, it will be
2716 automatically replaced by the VAR_DECL. */
2717 insert_decl_map (id, result, var);
2719 /* Remember this so we can ignore it in remap_decls. */
2720 id->retvar = var;
2722 return use;
2725 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
2726 to a local label. */
2728 static tree
2729 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
2731 tree node = *nodep;
2732 tree fn = (tree) fnp;
2734 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2735 return node;
2737 if (TYPE_P (node))
2738 *walk_subtrees = 0;
2740 return NULL_TREE;
2743 /* Determine if the function can be copied. If so return NULL. If
2744 not return a string describng the reason for failure. */
2746 static const char *
2747 copy_forbidden (struct function *fun, tree fndecl)
2749 const char *reason = fun->cannot_be_copied_reason;
2750 tree step;
2752 /* Only examine the function once. */
2753 if (fun->cannot_be_copied_set)
2754 return reason;
2756 /* We cannot copy a function that receives a non-local goto
2757 because we cannot remap the destination label used in the
2758 function that is performing the non-local goto. */
2759 /* ??? Actually, this should be possible, if we work at it.
2760 No doubt there's just a handful of places that simply
2761 assume it doesn't happen and don't substitute properly. */
2762 if (fun->has_nonlocal_label)
2764 reason = G_("function %q+F can never be copied "
2765 "because it receives a non-local goto");
2766 goto fail;
2769 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2771 tree decl = TREE_VALUE (step);
2773 if (TREE_CODE (decl) == VAR_DECL
2774 && TREE_STATIC (decl)
2775 && !DECL_EXTERNAL (decl)
2776 && DECL_INITIAL (decl)
2777 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
2778 has_label_address_in_static_1,
2779 fndecl))
2781 reason = G_("function %q+F can never be copied because it saves "
2782 "address of local label in a static variable");
2783 goto fail;
2787 fail:
2788 fun->cannot_be_copied_reason = reason;
2789 fun->cannot_be_copied_set = true;
2790 return reason;
2794 static const char *inline_forbidden_reason;
2796 /* A callback for walk_gimple_seq to handle statements. Returns non-null
2797 iff a function can not be inlined. Also sets the reason why. */
2799 static tree
2800 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2801 struct walk_stmt_info *wip)
2803 tree fn = (tree) wip->info;
2804 tree t;
2805 gimple stmt = gsi_stmt (*gsi);
2807 switch (gimple_code (stmt))
2809 case GIMPLE_CALL:
2810 /* Refuse to inline alloca call unless user explicitly forced so as
2811 this may change program's memory overhead drastically when the
2812 function using alloca is called in loop. In GCC present in
2813 SPEC2000 inlining into schedule_block cause it to require 2GB of
2814 RAM instead of 256MB. */
2815 if (gimple_alloca_call_p (stmt)
2816 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2818 inline_forbidden_reason
2819 = G_("function %q+F can never be inlined because it uses "
2820 "alloca (override using the always_inline attribute)");
2821 *handled_ops_p = true;
2822 return fn;
2825 t = gimple_call_fndecl (stmt);
2826 if (t == NULL_TREE)
2827 break;
2829 /* We cannot inline functions that call setjmp. */
2830 if (setjmp_call_p (t))
2832 inline_forbidden_reason
2833 = G_("function %q+F can never be inlined because it uses setjmp");
2834 *handled_ops_p = true;
2835 return t;
2838 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
2839 switch (DECL_FUNCTION_CODE (t))
2841 /* We cannot inline functions that take a variable number of
2842 arguments. */
2843 case BUILT_IN_VA_START:
2844 case BUILT_IN_NEXT_ARG:
2845 case BUILT_IN_VA_END:
2846 inline_forbidden_reason
2847 = G_("function %q+F can never be inlined because it "
2848 "uses variable argument lists");
2849 *handled_ops_p = true;
2850 return t;
2852 case BUILT_IN_LONGJMP:
2853 /* We can't inline functions that call __builtin_longjmp at
2854 all. The non-local goto machinery really requires the
2855 destination be in a different function. If we allow the
2856 function calling __builtin_longjmp to be inlined into the
2857 function calling __builtin_setjmp, Things will Go Awry. */
2858 inline_forbidden_reason
2859 = G_("function %q+F can never be inlined because "
2860 "it uses setjmp-longjmp exception handling");
2861 *handled_ops_p = true;
2862 return t;
2864 case BUILT_IN_NONLOCAL_GOTO:
2865 /* Similarly. */
2866 inline_forbidden_reason
2867 = G_("function %q+F can never be inlined because "
2868 "it uses non-local goto");
2869 *handled_ops_p = true;
2870 return t;
2872 case BUILT_IN_RETURN:
2873 case BUILT_IN_APPLY_ARGS:
2874 /* If a __builtin_apply_args caller would be inlined,
2875 it would be saving arguments of the function it has
2876 been inlined into. Similarly __builtin_return would
2877 return from the function the inline has been inlined into. */
2878 inline_forbidden_reason
2879 = G_("function %q+F can never be inlined because "
2880 "it uses __builtin_return or __builtin_apply_args");
2881 *handled_ops_p = true;
2882 return t;
2884 default:
2885 break;
2887 break;
2889 case GIMPLE_GOTO:
2890 t = gimple_goto_dest (stmt);
2892 /* We will not inline a function which uses computed goto. The
2893 addresses of its local labels, which may be tucked into
2894 global storage, are of course not constant across
2895 instantiations, which causes unexpected behavior. */
2896 if (TREE_CODE (t) != LABEL_DECL)
2898 inline_forbidden_reason
2899 = G_("function %q+F can never be inlined "
2900 "because it contains a computed goto");
2901 *handled_ops_p = true;
2902 return t;
2904 break;
2906 default:
2907 break;
2910 *handled_ops_p = false;
2911 return NULL_TREE;
2914 /* Return true if FNDECL is a function that cannot be inlined into
2915 another one. */
2917 static bool
2918 inline_forbidden_p (tree fndecl)
2920 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2921 struct walk_stmt_info wi;
2922 struct pointer_set_t *visited_nodes;
2923 basic_block bb;
2924 bool forbidden_p = false;
2926 /* First check for shared reasons not to copy the code. */
2927 inline_forbidden_reason = copy_forbidden (fun, fndecl);
2928 if (inline_forbidden_reason != NULL)
2929 return true;
2931 /* Next, walk the statements of the function looking for
2932 constraucts we can't handle, or are non-optimal for inlining. */
2933 visited_nodes = pointer_set_create ();
2934 memset (&wi, 0, sizeof (wi));
2935 wi.info = (void *) fndecl;
2936 wi.pset = visited_nodes;
2938 FOR_EACH_BB_FN (bb, fun)
2940 gimple ret;
2941 gimple_seq seq = bb_seq (bb);
2942 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
2943 forbidden_p = (ret != NULL);
2944 if (forbidden_p)
2945 break;
2948 pointer_set_destroy (visited_nodes);
2949 return forbidden_p;
2952 /* Returns nonzero if FN is a function that does not have any
2953 fundamental inline blocking properties. */
2955 bool
2956 tree_inlinable_function_p (tree fn)
2958 bool inlinable = true;
2959 bool do_warning;
2960 tree always_inline;
2962 /* If we've already decided this function shouldn't be inlined,
2963 there's no need to check again. */
2964 if (DECL_UNINLINABLE (fn))
2965 return false;
2967 /* We only warn for functions declared `inline' by the user. */
2968 do_warning = (warn_inline
2969 && DECL_DECLARED_INLINE_P (fn)
2970 && !DECL_NO_INLINE_WARNING_P (fn)
2971 && !DECL_IN_SYSTEM_HEADER (fn));
2973 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
2975 if (flag_no_inline
2976 && always_inline == NULL)
2978 if (do_warning)
2979 warning (OPT_Winline, "function %q+F can never be inlined because it "
2980 "is suppressed using -fno-inline", fn);
2981 inlinable = false;
2984 /* Don't auto-inline anything that might not be bound within
2985 this unit of translation. */
2986 else if (!DECL_DECLARED_INLINE_P (fn)
2987 && DECL_REPLACEABLE_P (fn))
2988 inlinable = false;
2990 else if (!function_attribute_inlinable_p (fn))
2992 if (do_warning)
2993 warning (OPT_Winline, "function %q+F can never be inlined because it "
2994 "uses attributes conflicting with inlining", fn);
2995 inlinable = false;
2998 else if (inline_forbidden_p (fn))
3000 /* See if we should warn about uninlinable functions. Previously,
3001 some of these warnings would be issued while trying to expand
3002 the function inline, but that would cause multiple warnings
3003 about functions that would for example call alloca. But since
3004 this a property of the function, just one warning is enough.
3005 As a bonus we can now give more details about the reason why a
3006 function is not inlinable. */
3007 if (always_inline)
3008 sorry (inline_forbidden_reason, fn);
3009 else if (do_warning)
3010 warning (OPT_Winline, inline_forbidden_reason, fn);
3012 inlinable = false;
3015 /* Squirrel away the result so that we don't have to check again. */
3016 DECL_UNINLINABLE (fn) = !inlinable;
3018 return inlinable;
3021 /* Estimate the cost of a memory move. Use machine dependent
3022 word size and take possible memcpy call into account. */
3025 estimate_move_cost (tree type)
3027 HOST_WIDE_INT size;
3029 gcc_assert (!VOID_TYPE_P (type));
3031 size = int_size_in_bytes (type);
3033 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
3034 /* Cost of a memcpy call, 3 arguments and the call. */
3035 return 4;
3036 else
3037 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
3040 /* Returns cost of operation CODE, according to WEIGHTS */
3042 static int
3043 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3044 tree op1 ATTRIBUTE_UNUSED, tree op2)
3046 switch (code)
3048 /* These are "free" conversions, or their presumed cost
3049 is folded into other operations. */
3050 case RANGE_EXPR:
3051 CASE_CONVERT:
3052 case COMPLEX_EXPR:
3053 case PAREN_EXPR:
3054 return 0;
3056 /* Assign cost of 1 to usual operations.
3057 ??? We may consider mapping RTL costs to this. */
3058 case COND_EXPR:
3059 case VEC_COND_EXPR:
3061 case PLUS_EXPR:
3062 case POINTER_PLUS_EXPR:
3063 case MINUS_EXPR:
3064 case MULT_EXPR:
3066 case ADDR_SPACE_CONVERT_EXPR:
3067 case FIXED_CONVERT_EXPR:
3068 case FIX_TRUNC_EXPR:
3070 case NEGATE_EXPR:
3071 case FLOAT_EXPR:
3072 case MIN_EXPR:
3073 case MAX_EXPR:
3074 case ABS_EXPR:
3076 case LSHIFT_EXPR:
3077 case RSHIFT_EXPR:
3078 case LROTATE_EXPR:
3079 case RROTATE_EXPR:
3080 case VEC_LSHIFT_EXPR:
3081 case VEC_RSHIFT_EXPR:
3083 case BIT_IOR_EXPR:
3084 case BIT_XOR_EXPR:
3085 case BIT_AND_EXPR:
3086 case BIT_NOT_EXPR:
3088 case TRUTH_ANDIF_EXPR:
3089 case TRUTH_ORIF_EXPR:
3090 case TRUTH_AND_EXPR:
3091 case TRUTH_OR_EXPR:
3092 case TRUTH_XOR_EXPR:
3093 case TRUTH_NOT_EXPR:
3095 case LT_EXPR:
3096 case LE_EXPR:
3097 case GT_EXPR:
3098 case GE_EXPR:
3099 case EQ_EXPR:
3100 case NE_EXPR:
3101 case ORDERED_EXPR:
3102 case UNORDERED_EXPR:
3104 case UNLT_EXPR:
3105 case UNLE_EXPR:
3106 case UNGT_EXPR:
3107 case UNGE_EXPR:
3108 case UNEQ_EXPR:
3109 case LTGT_EXPR:
3111 case CONJ_EXPR:
3113 case PREDECREMENT_EXPR:
3114 case PREINCREMENT_EXPR:
3115 case POSTDECREMENT_EXPR:
3116 case POSTINCREMENT_EXPR:
3118 case REALIGN_LOAD_EXPR:
3120 case REDUC_MAX_EXPR:
3121 case REDUC_MIN_EXPR:
3122 case REDUC_PLUS_EXPR:
3123 case WIDEN_SUM_EXPR:
3124 case WIDEN_MULT_EXPR:
3125 case DOT_PROD_EXPR:
3127 case VEC_WIDEN_MULT_HI_EXPR:
3128 case VEC_WIDEN_MULT_LO_EXPR:
3129 case VEC_UNPACK_HI_EXPR:
3130 case VEC_UNPACK_LO_EXPR:
3131 case VEC_UNPACK_FLOAT_HI_EXPR:
3132 case VEC_UNPACK_FLOAT_LO_EXPR:
3133 case VEC_PACK_TRUNC_EXPR:
3134 case VEC_PACK_SAT_EXPR:
3135 case VEC_PACK_FIX_TRUNC_EXPR:
3136 case VEC_EXTRACT_EVEN_EXPR:
3137 case VEC_EXTRACT_ODD_EXPR:
3138 case VEC_INTERLEAVE_HIGH_EXPR:
3139 case VEC_INTERLEAVE_LOW_EXPR:
3141 return 1;
3143 /* Few special cases of expensive operations. This is useful
3144 to avoid inlining on functions having too many of these. */
3145 case TRUNC_DIV_EXPR:
3146 case CEIL_DIV_EXPR:
3147 case FLOOR_DIV_EXPR:
3148 case ROUND_DIV_EXPR:
3149 case EXACT_DIV_EXPR:
3150 case TRUNC_MOD_EXPR:
3151 case CEIL_MOD_EXPR:
3152 case FLOOR_MOD_EXPR:
3153 case ROUND_MOD_EXPR:
3154 case RDIV_EXPR:
3155 if (TREE_CODE (op2) != INTEGER_CST)
3156 return weights->div_mod_cost;
3157 return 1;
3159 default:
3160 /* We expect a copy assignment with no operator. */
3161 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
3162 return 0;
3167 /* Estimate number of instructions that will be created by expanding
3168 the statements in the statement sequence STMTS.
3169 WEIGHTS contains weights attributed to various constructs. */
3171 static
3172 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
3174 int cost;
3175 gimple_stmt_iterator gsi;
3177 cost = 0;
3178 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
3179 cost += estimate_num_insns (gsi_stmt (gsi), weights);
3181 return cost;
3185 /* Estimate number of instructions that will be created by expanding STMT.
3186 WEIGHTS contains weights attributed to various constructs. */
3189 estimate_num_insns (gimple stmt, eni_weights *weights)
3191 unsigned cost, i;
3192 enum gimple_code code = gimple_code (stmt);
3193 tree lhs;
3194 tree rhs;
3196 switch (code)
3198 case GIMPLE_ASSIGN:
3199 /* Try to estimate the cost of assignments. We have three cases to
3200 deal with:
3201 1) Simple assignments to registers;
3202 2) Stores to things that must live in memory. This includes
3203 "normal" stores to scalars, but also assignments of large
3204 structures, or constructors of big arrays;
3206 Let us look at the first two cases, assuming we have "a = b + C":
3207 <GIMPLE_ASSIGN <var_decl "a">
3208 <plus_expr <var_decl "b"> <constant C>>
3209 If "a" is a GIMPLE register, the assignment to it is free on almost
3210 any target, because "a" usually ends up in a real register. Hence
3211 the only cost of this expression comes from the PLUS_EXPR, and we
3212 can ignore the GIMPLE_ASSIGN.
3213 If "a" is not a GIMPLE register, the assignment to "a" will most
3214 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
3215 of moving something into "a", which we compute using the function
3216 estimate_move_cost. */
3217 lhs = gimple_assign_lhs (stmt);
3218 rhs = gimple_assign_rhs1 (stmt);
3220 if (is_gimple_reg (lhs))
3221 cost = 0;
3222 else
3223 cost = estimate_move_cost (TREE_TYPE (lhs));
3225 if (!is_gimple_reg (rhs) && !is_gimple_min_invariant (rhs))
3226 cost += estimate_move_cost (TREE_TYPE (rhs));
3228 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3229 gimple_assign_rhs1 (stmt),
3230 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3231 == GIMPLE_BINARY_RHS
3232 ? gimple_assign_rhs2 (stmt) : NULL);
3233 break;
3235 case GIMPLE_COND:
3236 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3237 gimple_op (stmt, 0),
3238 gimple_op (stmt, 1));
3239 break;
3241 case GIMPLE_SWITCH:
3242 /* Take into account cost of the switch + guess 2 conditional jumps for
3243 each case label.
3245 TODO: once the switch expansion logic is sufficiently separated, we can
3246 do better job on estimating cost of the switch. */
3247 if (weights->time_based)
3248 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3249 else
3250 cost = gimple_switch_num_labels (stmt) * 2;
3251 break;
3253 case GIMPLE_CALL:
3255 tree decl = gimple_call_fndecl (stmt);
3256 tree addr = gimple_call_fn (stmt);
3257 tree funtype = TREE_TYPE (addr);
3259 if (POINTER_TYPE_P (funtype))
3260 funtype = TREE_TYPE (funtype);
3262 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
3263 cost = weights->target_builtin_call_cost;
3264 else
3265 cost = weights->call_cost;
3267 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3268 switch (DECL_FUNCTION_CODE (decl))
3270 case BUILT_IN_CONSTANT_P:
3271 return 0;
3272 case BUILT_IN_EXPECT:
3273 return 0;
3275 /* Prefetch instruction is not expensive. */
3276 case BUILT_IN_PREFETCH:
3277 cost = weights->target_builtin_call_cost;
3278 break;
3280 /* Exception state returns or moves registers around. */
3281 case BUILT_IN_EH_FILTER:
3282 case BUILT_IN_EH_POINTER:
3283 case BUILT_IN_EH_COPY_VALUES:
3284 return 0;
3286 default:
3287 break;
3290 if (decl)
3291 funtype = TREE_TYPE (decl);
3293 if (!VOID_TYPE_P (TREE_TYPE (funtype)))
3294 cost += estimate_move_cost (TREE_TYPE (funtype));
3295 /* Our cost must be kept in sync with
3296 cgraph_estimate_size_after_inlining that does use function
3297 declaration to figure out the arguments. */
3298 if (decl && DECL_ARGUMENTS (decl))
3300 tree arg;
3301 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
3302 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3303 cost += estimate_move_cost (TREE_TYPE (arg));
3305 else if (funtype && prototype_p (funtype))
3307 tree t;
3308 for (t = TYPE_ARG_TYPES (funtype); t && t != void_list_node;
3309 t = TREE_CHAIN (t))
3310 if (!VOID_TYPE_P (TREE_VALUE (t)))
3311 cost += estimate_move_cost (TREE_VALUE (t));
3313 else
3315 for (i = 0; i < gimple_call_num_args (stmt); i++)
3317 tree arg = gimple_call_arg (stmt, i);
3318 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3319 cost += estimate_move_cost (TREE_TYPE (arg));
3323 break;
3326 case GIMPLE_GOTO:
3327 case GIMPLE_LABEL:
3328 case GIMPLE_NOP:
3329 case GIMPLE_PHI:
3330 case GIMPLE_RETURN:
3331 case GIMPLE_PREDICT:
3332 case GIMPLE_DEBUG:
3333 return 0;
3335 case GIMPLE_ASM:
3336 return asm_str_count (gimple_asm_string (stmt));
3338 case GIMPLE_RESX:
3339 /* This is either going to be an external function call with one
3340 argument, or two register copy statements plus a goto. */
3341 return 2;
3343 case GIMPLE_EH_DISPATCH:
3344 /* ??? This is going to turn into a switch statement. Ideally
3345 we'd have a look at the eh region and estimate the number of
3346 edges involved. */
3347 return 10;
3349 case GIMPLE_BIND:
3350 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
3352 case GIMPLE_EH_FILTER:
3353 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
3355 case GIMPLE_CATCH:
3356 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
3358 case GIMPLE_TRY:
3359 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
3360 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
3362 /* OpenMP directives are generally very expensive. */
3364 case GIMPLE_OMP_RETURN:
3365 case GIMPLE_OMP_SECTIONS_SWITCH:
3366 case GIMPLE_OMP_ATOMIC_STORE:
3367 case GIMPLE_OMP_CONTINUE:
3368 /* ...except these, which are cheap. */
3369 return 0;
3371 case GIMPLE_OMP_ATOMIC_LOAD:
3372 return weights->omp_cost;
3374 case GIMPLE_OMP_FOR:
3375 return (weights->omp_cost
3376 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
3377 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
3379 case GIMPLE_OMP_PARALLEL:
3380 case GIMPLE_OMP_TASK:
3381 case GIMPLE_OMP_CRITICAL:
3382 case GIMPLE_OMP_MASTER:
3383 case GIMPLE_OMP_ORDERED:
3384 case GIMPLE_OMP_SECTION:
3385 case GIMPLE_OMP_SECTIONS:
3386 case GIMPLE_OMP_SINGLE:
3387 return (weights->omp_cost
3388 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
3390 default:
3391 gcc_unreachable ();
3394 return cost;
3397 /* Estimate number of instructions that will be created by expanding
3398 function FNDECL. WEIGHTS contains weights attributed to various
3399 constructs. */
3402 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
3404 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
3405 gimple_stmt_iterator bsi;
3406 basic_block bb;
3407 int n = 0;
3409 gcc_assert (my_function && my_function->cfg);
3410 FOR_EACH_BB_FN (bb, my_function)
3412 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3413 n += estimate_num_insns (gsi_stmt (bsi), weights);
3416 return n;
3420 /* Initializes weights used by estimate_num_insns. */
3422 void
3423 init_inline_once (void)
3425 eni_size_weights.call_cost = 1;
3426 eni_size_weights.target_builtin_call_cost = 1;
3427 eni_size_weights.div_mod_cost = 1;
3428 eni_size_weights.omp_cost = 40;
3429 eni_size_weights.time_based = false;
3431 /* Estimating time for call is difficult, since we have no idea what the
3432 called function does. In the current uses of eni_time_weights,
3433 underestimating the cost does less harm than overestimating it, so
3434 we choose a rather small value here. */
3435 eni_time_weights.call_cost = 10;
3436 eni_time_weights.target_builtin_call_cost = 10;
3437 eni_time_weights.div_mod_cost = 10;
3438 eni_time_weights.omp_cost = 40;
3439 eni_time_weights.time_based = true;
3442 /* Estimate the number of instructions in a gimple_seq. */
3445 count_insns_seq (gimple_seq seq, eni_weights *weights)
3447 gimple_stmt_iterator gsi;
3448 int n = 0;
3449 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3450 n += estimate_num_insns (gsi_stmt (gsi), weights);
3452 return n;
3456 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3458 static void
3459 prepend_lexical_block (tree current_block, tree new_block)
3461 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3462 BLOCK_SUBBLOCKS (current_block) = new_block;
3463 BLOCK_SUPERCONTEXT (new_block) = current_block;
3466 /* Fetch callee declaration from the call graph edge going from NODE and
3467 associated with STMR call statement. Return NULL_TREE if not found. */
3468 static tree
3469 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3471 struct cgraph_edge *cs;
3473 cs = cgraph_edge (node, stmt);
3474 if (cs)
3475 return cs->callee->decl;
3477 return NULL_TREE;
3480 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3482 static bool
3483 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3485 tree use_retvar;
3486 tree fn;
3487 struct pointer_map_t *st, *dst;
3488 tree return_slot;
3489 tree modify_dest;
3490 location_t saved_location;
3491 struct cgraph_edge *cg_edge;
3492 cgraph_inline_failed_t reason;
3493 basic_block return_block;
3494 edge e;
3495 gimple_stmt_iterator gsi, stmt_gsi;
3496 bool successfully_inlined = FALSE;
3497 bool purge_dead_abnormal_edges;
3498 tree t_step;
3499 tree var;
3501 /* Set input_location here so we get the right instantiation context
3502 if we call instantiate_decl from inlinable_function_p. */
3503 saved_location = input_location;
3504 if (gimple_has_location (stmt))
3505 input_location = gimple_location (stmt);
3507 /* From here on, we're only interested in CALL_EXPRs. */
3508 if (gimple_code (stmt) != GIMPLE_CALL)
3509 goto egress;
3511 /* First, see if we can figure out what function is being called.
3512 If we cannot, then there is no hope of inlining the function. */
3513 fn = gimple_call_fndecl (stmt);
3514 if (!fn)
3516 fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3517 if (!fn)
3518 goto egress;
3521 /* Turn forward declarations into real ones. */
3522 fn = cgraph_node (fn)->decl;
3524 /* If FN is a declaration of a function in a nested scope that was
3525 globally declared inline, we don't set its DECL_INITIAL.
3526 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3527 C++ front-end uses it for cdtors to refer to their internal
3528 declarations, that are not real functions. Fortunately those
3529 don't have trees to be saved, so we can tell by checking their
3530 gimple_body. */
3531 if (!DECL_INITIAL (fn)
3532 && DECL_ABSTRACT_ORIGIN (fn)
3533 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3534 fn = DECL_ABSTRACT_ORIGIN (fn);
3536 /* Objective C and fortran still calls tree_rest_of_compilation directly.
3537 Kill this check once this is fixed. */
3538 if (!id->dst_node->analyzed)
3539 goto egress;
3541 cg_edge = cgraph_edge (id->dst_node, stmt);
3543 /* Don't inline functions with different EH personalities. */
3544 if (DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3545 && DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl)
3546 && (DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3547 != DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl)))
3548 goto egress;
3550 /* Don't try to inline functions that are not well-suited to
3551 inlining. */
3552 if (!cgraph_inline_p (cg_edge, &reason))
3554 /* If this call was originally indirect, we do not want to emit any
3555 inlining related warnings or sorry messages because there are no
3556 guarantees regarding those. */
3557 if (cg_edge->indirect_call)
3558 goto egress;
3560 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3561 /* Avoid warnings during early inline pass. */
3562 && cgraph_global_info_ready)
3564 sorry ("inlining failed in call to %q+F: %s", fn,
3565 cgraph_inline_failed_string (reason));
3566 sorry ("called from here");
3568 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3569 && !DECL_IN_SYSTEM_HEADER (fn)
3570 && reason != CIF_UNSPECIFIED
3571 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3572 /* Avoid warnings during early inline pass. */
3573 && cgraph_global_info_ready)
3575 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3576 fn, cgraph_inline_failed_string (reason));
3577 warning (OPT_Winline, "called from here");
3579 goto egress;
3581 fn = cg_edge->callee->decl;
3583 #ifdef ENABLE_CHECKING
3584 if (cg_edge->callee->decl != id->dst_node->decl)
3585 verify_cgraph_node (cg_edge->callee);
3586 #endif
3588 /* We will be inlining this callee. */
3589 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
3591 /* Update the callers EH personality. */
3592 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
3593 DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3594 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
3596 /* Split the block holding the GIMPLE_CALL. */
3597 e = split_block (bb, stmt);
3598 bb = e->src;
3599 return_block = e->dest;
3600 remove_edge (e);
3602 /* split_block splits after the statement; work around this by
3603 moving the call into the second block manually. Not pretty,
3604 but seems easier than doing the CFG manipulation by hand
3605 when the GIMPLE_CALL is in the last statement of BB. */
3606 stmt_gsi = gsi_last_bb (bb);
3607 gsi_remove (&stmt_gsi, false);
3609 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3610 been the source of abnormal edges. In this case, schedule
3611 the removal of dead abnormal edges. */
3612 gsi = gsi_start_bb (return_block);
3613 if (gsi_end_p (gsi))
3615 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3616 purge_dead_abnormal_edges = true;
3618 else
3620 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3621 purge_dead_abnormal_edges = false;
3624 stmt_gsi = gsi_start_bb (return_block);
3626 /* Build a block containing code to initialize the arguments, the
3627 actual inline expansion of the body, and a label for the return
3628 statements within the function to jump to. The type of the
3629 statement expression is the return type of the function call. */
3630 id->block = make_node (BLOCK);
3631 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3632 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3633 prepend_lexical_block (gimple_block (stmt), id->block);
3635 /* Local declarations will be replaced by their equivalents in this
3636 map. */
3637 st = id->decl_map;
3638 id->decl_map = pointer_map_create ();
3639 dst = id->debug_map;
3640 id->debug_map = NULL;
3642 /* Record the function we are about to inline. */
3643 id->src_fn = fn;
3644 id->src_node = cg_edge->callee;
3645 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3646 id->gimple_call = stmt;
3648 gcc_assert (!id->src_cfun->after_inlining);
3650 id->entry_bb = bb;
3651 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3653 gimple_stmt_iterator si = gsi_last_bb (bb);
3654 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3655 NOT_TAKEN),
3656 GSI_NEW_STMT);
3658 initialize_inlined_parameters (id, stmt, fn, bb);
3660 if (DECL_INITIAL (fn))
3661 prepend_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3663 /* Return statements in the function body will be replaced by jumps
3664 to the RET_LABEL. */
3665 gcc_assert (DECL_INITIAL (fn));
3666 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3668 /* Find the LHS to which the result of this call is assigned. */
3669 return_slot = NULL;
3670 if (gimple_call_lhs (stmt))
3672 modify_dest = gimple_call_lhs (stmt);
3674 /* The function which we are inlining might not return a value,
3675 in which case we should issue a warning that the function
3676 does not return a value. In that case the optimizers will
3677 see that the variable to which the value is assigned was not
3678 initialized. We do not want to issue a warning about that
3679 uninitialized variable. */
3680 if (DECL_P (modify_dest))
3681 TREE_NO_WARNING (modify_dest) = 1;
3683 if (gimple_call_return_slot_opt_p (stmt))
3685 return_slot = modify_dest;
3686 modify_dest = NULL;
3689 else
3690 modify_dest = NULL;
3692 /* If we are inlining a call to the C++ operator new, we don't want
3693 to use type based alias analysis on the return value. Otherwise
3694 we may get confused if the compiler sees that the inlined new
3695 function returns a pointer which was just deleted. See bug
3696 33407. */
3697 if (DECL_IS_OPERATOR_NEW (fn))
3699 return_slot = NULL;
3700 modify_dest = NULL;
3703 /* Declare the return variable for the function. */
3704 use_retvar = declare_return_variable (id, return_slot, modify_dest);
3706 /* Add local vars in this inlined callee to caller. */
3707 t_step = id->src_cfun->local_decls;
3708 for (; t_step; t_step = TREE_CHAIN (t_step))
3710 var = TREE_VALUE (t_step);
3711 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3713 if (var_ann (var) && add_referenced_var (var))
3714 cfun->local_decls = tree_cons (NULL_TREE, var,
3715 cfun->local_decls);
3717 else if (!can_be_nonlocal (var, id))
3718 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3719 cfun->local_decls);
3722 if (dump_file && (dump_flags & TDF_DETAILS))
3724 fprintf (dump_file, "Inlining ");
3725 print_generic_expr (dump_file, id->src_fn, 0);
3726 fprintf (dump_file, " to ");
3727 print_generic_expr (dump_file, id->dst_fn, 0);
3728 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
3731 /* This is it. Duplicate the callee body. Assume callee is
3732 pre-gimplified. Note that we must not alter the caller
3733 function in any way before this point, as this CALL_EXPR may be
3734 a self-referential call; if we're calling ourselves, we need to
3735 duplicate our body before altering anything. */
3736 copy_body (id, bb->count,
3737 cg_edge->frequency * REG_BR_PROB_BASE / CGRAPH_FREQ_BASE,
3738 bb, return_block);
3740 /* Reset the escaped and callused solutions. */
3741 if (cfun->gimple_df)
3743 pt_solution_reset (&cfun->gimple_df->escaped);
3744 pt_solution_reset (&cfun->gimple_df->callused);
3747 /* Clean up. */
3748 if (id->debug_map)
3750 pointer_map_destroy (id->debug_map);
3751 id->debug_map = dst;
3753 pointer_map_destroy (id->decl_map);
3754 id->decl_map = st;
3756 /* Unlink the calls virtual operands before replacing it. */
3757 unlink_stmt_vdef (stmt);
3759 /* If the inlined function returns a result that we care about,
3760 substitute the GIMPLE_CALL with an assignment of the return
3761 variable to the LHS of the call. That is, if STMT was
3762 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
3763 if (use_retvar && gimple_call_lhs (stmt))
3765 gimple old_stmt = stmt;
3766 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3767 gsi_replace (&stmt_gsi, stmt, false);
3768 if (gimple_in_ssa_p (cfun))
3769 mark_symbols_for_renaming (stmt);
3770 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3772 else
3774 /* Handle the case of inlining a function with no return
3775 statement, which causes the return value to become undefined. */
3776 if (gimple_call_lhs (stmt)
3777 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3779 tree name = gimple_call_lhs (stmt);
3780 tree var = SSA_NAME_VAR (name);
3781 tree def = gimple_default_def (cfun, var);
3783 if (def)
3785 /* If the variable is used undefined, make this name
3786 undefined via a move. */
3787 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3788 gsi_replace (&stmt_gsi, stmt, true);
3790 else
3792 /* Otherwise make this variable undefined. */
3793 gsi_remove (&stmt_gsi, true);
3794 set_default_def (var, name);
3795 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
3798 else
3799 gsi_remove (&stmt_gsi, true);
3802 if (purge_dead_abnormal_edges)
3803 gimple_purge_dead_abnormal_call_edges (return_block);
3805 /* If the value of the new expression is ignored, that's OK. We
3806 don't warn about this for CALL_EXPRs, so we shouldn't warn about
3807 the equivalent inlined version either. */
3808 if (is_gimple_assign (stmt))
3810 gcc_assert (gimple_assign_single_p (stmt)
3811 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
3812 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
3815 /* Output the inlining info for this abstract function, since it has been
3816 inlined. If we don't do this now, we can lose the information about the
3817 variables in the function when the blocks get blown away as soon as we
3818 remove the cgraph node. */
3819 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
3821 /* Update callgraph if needed. */
3822 cgraph_remove_node (cg_edge->callee);
3824 id->block = NULL_TREE;
3825 successfully_inlined = TRUE;
3827 egress:
3828 input_location = saved_location;
3829 return successfully_inlined;
3832 /* Expand call statements reachable from STMT_P.
3833 We can only have CALL_EXPRs as the "toplevel" tree code or nested
3834 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
3835 unfortunately not use that function here because we need a pointer
3836 to the CALL_EXPR, not the tree itself. */
3838 static bool
3839 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
3841 gimple_stmt_iterator gsi;
3843 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3845 gimple stmt = gsi_stmt (gsi);
3847 if (is_gimple_call (stmt)
3848 && expand_call_inline (bb, stmt, id))
3849 return true;
3852 return false;
3856 /* Walk all basic blocks created after FIRST and try to fold every statement
3857 in the STATEMENTS pointer set. */
3859 static void
3860 fold_marked_statements (int first, struct pointer_set_t *statements)
3862 for (; first < n_basic_blocks; first++)
3863 if (BASIC_BLOCK (first))
3865 gimple_stmt_iterator gsi;
3867 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
3868 !gsi_end_p (gsi);
3869 gsi_next (&gsi))
3870 if (pointer_set_contains (statements, gsi_stmt (gsi)))
3872 gimple old_stmt = gsi_stmt (gsi);
3873 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
3875 if (old_decl && DECL_BUILT_IN (old_decl))
3877 /* Folding builtins can create multiple instructions,
3878 we need to look at all of them. */
3879 gimple_stmt_iterator i2 = gsi;
3880 gsi_prev (&i2);
3881 if (fold_stmt (&gsi))
3883 gimple new_stmt;
3884 if (gsi_end_p (i2))
3885 i2 = gsi_start_bb (BASIC_BLOCK (first));
3886 else
3887 gsi_next (&i2);
3888 while (1)
3890 new_stmt = gsi_stmt (i2);
3891 update_stmt (new_stmt);
3892 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
3893 new_stmt);
3895 if (new_stmt == gsi_stmt (gsi))
3897 /* It is okay to check only for the very last
3898 of these statements. If it is a throwing
3899 statement nothing will change. If it isn't
3900 this can remove EH edges. If that weren't
3901 correct then because some intermediate stmts
3902 throw, but not the last one. That would mean
3903 we'd have to split the block, which we can't
3904 here and we'd loose anyway. And as builtins
3905 probably never throw, this all
3906 is mood anyway. */
3907 if (maybe_clean_or_replace_eh_stmt (old_stmt,
3908 new_stmt))
3909 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3910 break;
3912 gsi_next (&i2);
3916 else if (fold_stmt (&gsi))
3918 /* Re-read the statement from GSI as fold_stmt() may
3919 have changed it. */
3920 gimple new_stmt = gsi_stmt (gsi);
3921 update_stmt (new_stmt);
3923 if (is_gimple_call (old_stmt)
3924 || is_gimple_call (new_stmt))
3925 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
3926 new_stmt);
3928 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3929 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3935 /* Return true if BB has at least one abnormal outgoing edge. */
3937 static inline bool
3938 has_abnormal_outgoing_edge_p (basic_block bb)
3940 edge e;
3941 edge_iterator ei;
3943 FOR_EACH_EDGE (e, ei, bb->succs)
3944 if (e->flags & EDGE_ABNORMAL)
3945 return true;
3947 return false;
3950 /* Expand calls to inline functions in the body of FN. */
3952 unsigned int
3953 optimize_inline_calls (tree fn)
3955 copy_body_data id;
3956 basic_block bb;
3957 int last = n_basic_blocks;
3958 struct gimplify_ctx gctx;
3960 /* There is no point in performing inlining if errors have already
3961 occurred -- and we might crash if we try to inline invalid
3962 code. */
3963 if (errorcount || sorrycount)
3964 return 0;
3966 /* Clear out ID. */
3967 memset (&id, 0, sizeof (id));
3969 id.src_node = id.dst_node = cgraph_node (fn);
3970 id.dst_fn = fn;
3971 /* Or any functions that aren't finished yet. */
3972 if (current_function_decl)
3973 id.dst_fn = current_function_decl;
3975 id.copy_decl = copy_decl_maybe_to_var;
3976 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3977 id.transform_new_cfg = false;
3978 id.transform_return_to_modify = true;
3979 id.transform_lang_insert_block = NULL;
3980 id.statements_to_fold = pointer_set_create ();
3982 push_gimplify_context (&gctx);
3984 /* We make no attempts to keep dominance info up-to-date. */
3985 free_dominance_info (CDI_DOMINATORS);
3986 free_dominance_info (CDI_POST_DOMINATORS);
3988 /* Register specific gimple functions. */
3989 gimple_register_cfg_hooks ();
3991 /* Reach the trees by walking over the CFG, and note the
3992 enclosing basic-blocks in the call edges. */
3993 /* We walk the blocks going forward, because inlined function bodies
3994 will split id->current_basic_block, and the new blocks will
3995 follow it; we'll trudge through them, processing their CALL_EXPRs
3996 along the way. */
3997 FOR_EACH_BB (bb)
3998 gimple_expand_calls_inline (bb, &id);
4000 pop_gimplify_context (NULL);
4002 #ifdef ENABLE_CHECKING
4004 struct cgraph_edge *e;
4006 verify_cgraph_node (id.dst_node);
4008 /* Double check that we inlined everything we are supposed to inline. */
4009 for (e = id.dst_node->callees; e; e = e->next_callee)
4010 gcc_assert (e->inline_failed);
4012 #endif
4014 /* Fold the statements before compacting/renumbering the basic blocks. */
4015 fold_marked_statements (last, id.statements_to_fold);
4016 pointer_set_destroy (id.statements_to_fold);
4018 gcc_assert (!id.debug_stmts);
4020 /* Renumber the (code) basic_blocks consecutively. */
4021 compact_blocks ();
4022 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4023 number_blocks (fn);
4025 fold_cond_expr_cond ();
4026 delete_unreachable_blocks_update_callgraph (&id);
4027 #ifdef ENABLE_CHECKING
4028 verify_cgraph_node (id.dst_node);
4029 #endif
4031 /* It would be nice to check SSA/CFG/statement consistency here, but it is
4032 not possible yet - the IPA passes might make various functions to not
4033 throw and they don't care to proactively update local EH info. This is
4034 done later in fixup_cfg pass that also execute the verification. */
4035 return (TODO_update_ssa
4036 | TODO_cleanup_cfg
4037 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
4038 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
4041 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4043 tree
4044 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4046 enum tree_code code = TREE_CODE (*tp);
4047 enum tree_code_class cl = TREE_CODE_CLASS (code);
4049 /* We make copies of most nodes. */
4050 if (IS_EXPR_CODE_CLASS (cl)
4051 || code == TREE_LIST
4052 || code == TREE_VEC
4053 || code == TYPE_DECL
4054 || code == OMP_CLAUSE)
4056 /* Because the chain gets clobbered when we make a copy, we save it
4057 here. */
4058 tree chain = NULL_TREE, new_tree;
4060 chain = TREE_CHAIN (*tp);
4062 /* Copy the node. */
4063 new_tree = copy_node (*tp);
4065 /* Propagate mudflap marked-ness. */
4066 if (flag_mudflap && mf_marked_p (*tp))
4067 mf_mark (new_tree);
4069 *tp = new_tree;
4071 /* Now, restore the chain, if appropriate. That will cause
4072 walk_tree to walk into the chain as well. */
4073 if (code == PARM_DECL
4074 || code == TREE_LIST
4075 || code == OMP_CLAUSE)
4076 TREE_CHAIN (*tp) = chain;
4078 /* For now, we don't update BLOCKs when we make copies. So, we
4079 have to nullify all BIND_EXPRs. */
4080 if (TREE_CODE (*tp) == BIND_EXPR)
4081 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
4083 else if (code == CONSTRUCTOR)
4085 /* CONSTRUCTOR nodes need special handling because
4086 we need to duplicate the vector of elements. */
4087 tree new_tree;
4089 new_tree = copy_node (*tp);
4091 /* Propagate mudflap marked-ness. */
4092 if (flag_mudflap && mf_marked_p (*tp))
4093 mf_mark (new_tree);
4095 CONSTRUCTOR_ELTS (new_tree) = VEC_copy (constructor_elt, gc,
4096 CONSTRUCTOR_ELTS (*tp));
4097 *tp = new_tree;
4099 else if (TREE_CODE_CLASS (code) == tcc_type)
4100 *walk_subtrees = 0;
4101 else if (TREE_CODE_CLASS (code) == tcc_declaration)
4102 *walk_subtrees = 0;
4103 else if (TREE_CODE_CLASS (code) == tcc_constant)
4104 *walk_subtrees = 0;
4105 else
4106 gcc_assert (code != STATEMENT_LIST);
4107 return NULL_TREE;
4110 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
4111 information indicating to what new SAVE_EXPR this one should be mapped,
4112 use that one. Otherwise, create a new node and enter it in ST. FN is
4113 the function into which the copy will be placed. */
4115 static void
4116 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
4118 struct pointer_map_t *st = (struct pointer_map_t *) st_;
4119 tree *n;
4120 tree t;
4122 /* See if we already encountered this SAVE_EXPR. */
4123 n = (tree *) pointer_map_contains (st, *tp);
4125 /* If we didn't already remap this SAVE_EXPR, do so now. */
4126 if (!n)
4128 t = copy_node (*tp);
4130 /* Remember this SAVE_EXPR. */
4131 *pointer_map_insert (st, *tp) = t;
4132 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
4133 *pointer_map_insert (st, t) = t;
4135 else
4137 /* We've already walked into this SAVE_EXPR; don't do it again. */
4138 *walk_subtrees = 0;
4139 t = *n;
4142 /* Replace this SAVE_EXPR with the copy. */
4143 *tp = t;
4146 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
4147 copies the declaration and enters it in the splay_tree in DATA (which is
4148 really an `copy_body_data *'). */
4150 static tree
4151 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4152 void *data)
4154 copy_body_data *id = (copy_body_data *) data;
4156 /* Don't walk into types. */
4157 if (TYPE_P (*tp))
4158 *walk_subtrees = 0;
4160 else if (TREE_CODE (*tp) == LABEL_EXPR)
4162 tree decl = TREE_OPERAND (*tp, 0);
4164 /* Copy the decl and remember the copy. */
4165 insert_decl_map (id, decl, id->copy_decl (decl, id));
4168 return NULL_TREE;
4171 /* Perform any modifications to EXPR required when it is unsaved. Does
4172 not recurse into EXPR's subtrees. */
4174 static void
4175 unsave_expr_1 (tree expr)
4177 switch (TREE_CODE (expr))
4179 case TARGET_EXPR:
4180 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4181 It's OK for this to happen if it was part of a subtree that
4182 isn't immediately expanded, such as operand 2 of another
4183 TARGET_EXPR. */
4184 if (TREE_OPERAND (expr, 1))
4185 break;
4187 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4188 TREE_OPERAND (expr, 3) = NULL_TREE;
4189 break;
4191 default:
4192 break;
4196 /* Called via walk_tree when an expression is unsaved. Using the
4197 splay_tree pointed to by ST (which is really a `splay_tree'),
4198 remaps all local declarations to appropriate replacements. */
4200 static tree
4201 unsave_r (tree *tp, int *walk_subtrees, void *data)
4203 copy_body_data *id = (copy_body_data *) data;
4204 struct pointer_map_t *st = id->decl_map;
4205 tree *n;
4207 /* Only a local declaration (variable or label). */
4208 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
4209 || TREE_CODE (*tp) == LABEL_DECL)
4211 /* Lookup the declaration. */
4212 n = (tree *) pointer_map_contains (st, *tp);
4214 /* If it's there, remap it. */
4215 if (n)
4216 *tp = *n;
4219 else if (TREE_CODE (*tp) == STATEMENT_LIST)
4220 gcc_unreachable ();
4221 else if (TREE_CODE (*tp) == BIND_EXPR)
4222 copy_bind_expr (tp, walk_subtrees, id);
4223 else if (TREE_CODE (*tp) == SAVE_EXPR
4224 || TREE_CODE (*tp) == TARGET_EXPR)
4225 remap_save_expr (tp, st, walk_subtrees);
4226 else
4228 copy_tree_r (tp, walk_subtrees, NULL);
4230 /* Do whatever unsaving is required. */
4231 unsave_expr_1 (*tp);
4234 /* Keep iterating. */
4235 return NULL_TREE;
4238 /* Copies everything in EXPR and replaces variables, labels
4239 and SAVE_EXPRs local to EXPR. */
4241 tree
4242 unsave_expr_now (tree expr)
4244 copy_body_data id;
4246 /* There's nothing to do for NULL_TREE. */
4247 if (expr == 0)
4248 return expr;
4250 /* Set up ID. */
4251 memset (&id, 0, sizeof (id));
4252 id.src_fn = current_function_decl;
4253 id.dst_fn = current_function_decl;
4254 id.decl_map = pointer_map_create ();
4255 id.debug_map = NULL;
4257 id.copy_decl = copy_decl_no_change;
4258 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4259 id.transform_new_cfg = false;
4260 id.transform_return_to_modify = false;
4261 id.transform_lang_insert_block = NULL;
4263 /* Walk the tree once to find local labels. */
4264 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
4266 /* Walk the tree again, copying, remapping, and unsaving. */
4267 walk_tree (&expr, unsave_r, &id, NULL);
4269 /* Clean up. */
4270 pointer_map_destroy (id.decl_map);
4271 if (id.debug_map)
4272 pointer_map_destroy (id.debug_map);
4274 return expr;
4277 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
4278 label, copies the declaration and enters it in the splay_tree in DATA (which
4279 is really a 'copy_body_data *'. */
4281 static tree
4282 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
4283 bool *handled_ops_p ATTRIBUTE_UNUSED,
4284 struct walk_stmt_info *wi)
4286 copy_body_data *id = (copy_body_data *) wi->info;
4287 gimple stmt = gsi_stmt (*gsip);
4289 if (gimple_code (stmt) == GIMPLE_LABEL)
4291 tree decl = gimple_label_label (stmt);
4293 /* Copy the decl and remember the copy. */
4294 insert_decl_map (id, decl, id->copy_decl (decl, id));
4297 return NULL_TREE;
4301 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4302 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4303 remaps all local declarations to appropriate replacements in gimple
4304 operands. */
4306 static tree
4307 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
4309 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
4310 copy_body_data *id = (copy_body_data *) wi->info;
4311 struct pointer_map_t *st = id->decl_map;
4312 tree *n;
4313 tree expr = *tp;
4315 /* Only a local declaration (variable or label). */
4316 if ((TREE_CODE (expr) == VAR_DECL
4317 && !TREE_STATIC (expr))
4318 || TREE_CODE (expr) == LABEL_DECL)
4320 /* Lookup the declaration. */
4321 n = (tree *) pointer_map_contains (st, expr);
4323 /* If it's there, remap it. */
4324 if (n)
4325 *tp = *n;
4326 *walk_subtrees = 0;
4328 else if (TREE_CODE (expr) == STATEMENT_LIST
4329 || TREE_CODE (expr) == BIND_EXPR
4330 || TREE_CODE (expr) == SAVE_EXPR)
4331 gcc_unreachable ();
4332 else if (TREE_CODE (expr) == TARGET_EXPR)
4334 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4335 It's OK for this to happen if it was part of a subtree that
4336 isn't immediately expanded, such as operand 2 of another
4337 TARGET_EXPR. */
4338 if (!TREE_OPERAND (expr, 1))
4340 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4341 TREE_OPERAND (expr, 3) = NULL_TREE;
4345 /* Keep iterating. */
4346 return NULL_TREE;
4350 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4351 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4352 remaps all local declarations to appropriate replacements in gimple
4353 statements. */
4355 static tree
4356 replace_locals_stmt (gimple_stmt_iterator *gsip,
4357 bool *handled_ops_p ATTRIBUTE_UNUSED,
4358 struct walk_stmt_info *wi)
4360 copy_body_data *id = (copy_body_data *) wi->info;
4361 gimple stmt = gsi_stmt (*gsip);
4363 if (gimple_code (stmt) == GIMPLE_BIND)
4365 tree block = gimple_bind_block (stmt);
4367 if (block)
4369 remap_block (&block, id);
4370 gimple_bind_set_block (stmt, block);
4373 /* This will remap a lot of the same decls again, but this should be
4374 harmless. */
4375 if (gimple_bind_vars (stmt))
4376 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), NULL, id));
4379 /* Keep iterating. */
4380 return NULL_TREE;
4384 /* Copies everything in SEQ and replaces variables and labels local to
4385 current_function_decl. */
4387 gimple_seq
4388 copy_gimple_seq_and_replace_locals (gimple_seq seq)
4390 copy_body_data id;
4391 struct walk_stmt_info wi;
4392 struct pointer_set_t *visited;
4393 gimple_seq copy;
4395 /* There's nothing to do for NULL_TREE. */
4396 if (seq == NULL)
4397 return seq;
4399 /* Set up ID. */
4400 memset (&id, 0, sizeof (id));
4401 id.src_fn = current_function_decl;
4402 id.dst_fn = current_function_decl;
4403 id.decl_map = pointer_map_create ();
4404 id.debug_map = NULL;
4406 id.copy_decl = copy_decl_no_change;
4407 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4408 id.transform_new_cfg = false;
4409 id.transform_return_to_modify = false;
4410 id.transform_lang_insert_block = NULL;
4412 /* Walk the tree once to find local labels. */
4413 memset (&wi, 0, sizeof (wi));
4414 visited = pointer_set_create ();
4415 wi.info = &id;
4416 wi.pset = visited;
4417 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
4418 pointer_set_destroy (visited);
4420 copy = gimple_seq_copy (seq);
4422 /* Walk the copy, remapping decls. */
4423 memset (&wi, 0, sizeof (wi));
4424 wi.info = &id;
4425 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4427 /* Clean up. */
4428 pointer_map_destroy (id.decl_map);
4429 if (id.debug_map)
4430 pointer_map_destroy (id.debug_map);
4432 return copy;
4436 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
4438 static tree
4439 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
4441 if (*tp == data)
4442 return (tree) data;
4443 else
4444 return NULL;
4447 bool
4448 debug_find_tree (tree top, tree search)
4450 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4454 /* Declare the variables created by the inliner. Add all the variables in
4455 VARS to BIND_EXPR. */
4457 static void
4458 declare_inline_vars (tree block, tree vars)
4460 tree t;
4461 for (t = vars; t; t = TREE_CHAIN (t))
4463 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4464 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
4465 cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
4468 if (block)
4469 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4472 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
4473 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
4474 VAR_DECL translation. */
4476 static tree
4477 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
4479 /* Don't generate debug information for the copy if we wouldn't have
4480 generated it for the copy either. */
4481 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4482 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4484 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4485 declaration inspired this copy. */
4486 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4488 /* The new variable/label has no RTL, yet. */
4489 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4490 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4491 SET_DECL_RTL (copy, NULL_RTX);
4493 /* These args would always appear unused, if not for this. */
4494 TREE_USED (copy) = 1;
4496 /* Set the context for the new declaration. */
4497 if (!DECL_CONTEXT (decl))
4498 /* Globals stay global. */
4500 else if (DECL_CONTEXT (decl) != id->src_fn)
4501 /* Things that weren't in the scope of the function we're inlining
4502 from aren't in the scope we're inlining to, either. */
4504 else if (TREE_STATIC (decl))
4505 /* Function-scoped static variables should stay in the original
4506 function. */
4508 else
4509 /* Ordinary automatic local variables are now in the scope of the
4510 new function. */
4511 DECL_CONTEXT (copy) = id->dst_fn;
4513 return copy;
4516 static tree
4517 copy_decl_to_var (tree decl, copy_body_data *id)
4519 tree copy, type;
4521 gcc_assert (TREE_CODE (decl) == PARM_DECL
4522 || TREE_CODE (decl) == RESULT_DECL);
4524 type = TREE_TYPE (decl);
4526 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4527 VAR_DECL, DECL_NAME (decl), type);
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. */
4539 static tree
4540 copy_result_decl_to_var (tree decl, copy_body_data *id)
4542 tree copy, type;
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 TREE_READONLY (copy) = TREE_READONLY (decl);
4554 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4555 if (!DECL_BY_REFERENCE (decl))
4557 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4558 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4561 return copy_decl_for_dup_finish (id, decl, copy);
4564 tree
4565 copy_decl_no_change (tree decl, copy_body_data *id)
4567 tree copy;
4569 copy = copy_node (decl);
4571 /* The COPY is not abstract; it will be generated in DST_FN. */
4572 DECL_ABSTRACT (copy) = 0;
4573 lang_hooks.dup_lang_specific_decl (copy);
4575 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4576 been taken; it's for internal bookkeeping in expand_goto_internal. */
4577 if (TREE_CODE (copy) == LABEL_DECL)
4579 TREE_ADDRESSABLE (copy) = 0;
4580 LABEL_DECL_UID (copy) = -1;
4583 return copy_decl_for_dup_finish (id, decl, copy);
4586 static tree
4587 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4589 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4590 return copy_decl_to_var (decl, id);
4591 else
4592 return copy_decl_no_change (decl, id);
4595 /* Return a copy of the function's argument tree. */
4596 static tree
4597 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4598 bitmap args_to_skip, tree *vars)
4600 tree arg, *parg;
4601 tree new_parm = NULL;
4602 int i = 0;
4604 parg = &new_parm;
4606 for (arg = orig_parm; arg; arg = TREE_CHAIN (arg), i++)
4607 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4609 tree new_tree = remap_decl (arg, id);
4610 lang_hooks.dup_lang_specific_decl (new_tree);
4611 *parg = new_tree;
4612 parg = &TREE_CHAIN (new_tree);
4614 else if (!pointer_map_contains (id->decl_map, arg))
4616 /* Make an equivalent VAR_DECL. If the argument was used
4617 as temporary variable later in function, the uses will be
4618 replaced by local variable. */
4619 tree var = copy_decl_to_var (arg, id);
4620 get_var_ann (var);
4621 add_referenced_var (var);
4622 insert_decl_map (id, arg, var);
4623 /* Declare this new variable. */
4624 TREE_CHAIN (var) = *vars;
4625 *vars = var;
4627 return new_parm;
4630 /* Return a copy of the function's static chain. */
4631 static tree
4632 copy_static_chain (tree static_chain, copy_body_data * id)
4634 tree *chain_copy, *pvar;
4636 chain_copy = &static_chain;
4637 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4639 tree new_tree = remap_decl (*pvar, id);
4640 lang_hooks.dup_lang_specific_decl (new_tree);
4641 TREE_CHAIN (new_tree) = TREE_CHAIN (*pvar);
4642 *pvar = new_tree;
4644 return static_chain;
4647 /* Return true if the function is allowed to be versioned.
4648 This is a guard for the versioning functionality. */
4650 bool
4651 tree_versionable_function_p (tree fndecl)
4653 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
4654 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
4657 /* Delete all unreachable basic blocks and update callgraph.
4658 Doing so is somewhat nontrivial because we need to update all clones and
4659 remove inline function that become unreachable. */
4661 static bool
4662 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
4664 bool changed = false;
4665 basic_block b, next_bb;
4667 find_unreachable_blocks ();
4669 /* Delete all unreachable basic blocks. */
4671 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
4673 next_bb = b->next_bb;
4675 if (!(b->flags & BB_REACHABLE))
4677 gimple_stmt_iterator bsi;
4679 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
4680 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL)
4682 struct cgraph_edge *e;
4683 struct cgraph_node *node;
4685 if ((e = cgraph_edge (id->dst_node, gsi_stmt (bsi))) != NULL)
4687 if (!e->inline_failed)
4688 cgraph_remove_node_and_inline_clones (e->callee);
4689 else
4690 cgraph_remove_edge (e);
4692 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
4693 && id->dst_node->clones)
4694 for (node = id->dst_node->clones; node != id->dst_node;)
4696 if ((e = cgraph_edge (node, gsi_stmt (bsi))) != NULL)
4698 if (!e->inline_failed)
4699 cgraph_remove_node_and_inline_clones (e->callee);
4700 else
4701 cgraph_remove_edge (e);
4704 if (node->clones)
4705 node = node->clones;
4706 else if (node->next_sibling_clone)
4707 node = node->next_sibling_clone;
4708 else
4710 while (node != id->dst_node && !node->next_sibling_clone)
4711 node = node->clone_of;
4712 if (node != id->dst_node)
4713 node = node->next_sibling_clone;
4717 delete_basic_block (b);
4718 changed = true;
4722 if (changed)
4723 tidy_fallthru_edges ();
4724 return changed;
4727 /* Update clone info after duplication. */
4729 static void
4730 update_clone_info (copy_body_data * id)
4732 struct cgraph_node *node;
4733 if (!id->dst_node->clones)
4734 return;
4735 for (node = id->dst_node->clones; node != id->dst_node;)
4737 /* First update replace maps to match the new body. */
4738 if (node->clone.tree_map)
4740 unsigned int i;
4741 for (i = 0; i < VEC_length (ipa_replace_map_p, node->clone.tree_map); i++)
4743 struct ipa_replace_map *replace_info;
4744 replace_info = VEC_index (ipa_replace_map_p, node->clone.tree_map, i);
4745 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
4746 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
4749 if (node->clones)
4750 node = node->clones;
4751 else if (node->next_sibling_clone)
4752 node = node->next_sibling_clone;
4753 else
4755 while (node != id->dst_node && !node->next_sibling_clone)
4756 node = node->clone_of;
4757 if (node != id->dst_node)
4758 node = node->next_sibling_clone;
4763 /* Create a copy of a function's tree.
4764 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4765 of the original function and the new copied function
4766 respectively. In case we want to replace a DECL
4767 tree with another tree while duplicating the function's
4768 body, TREE_MAP represents the mapping between these
4769 trees. If UPDATE_CLONES is set, the call_stmt fields
4770 of edges of clones of the function will be updated. */
4771 void
4772 tree_function_versioning (tree old_decl, tree new_decl,
4773 VEC(ipa_replace_map_p,gc)* tree_map,
4774 bool update_clones, bitmap args_to_skip)
4776 struct cgraph_node *old_version_node;
4777 struct cgraph_node *new_version_node;
4778 copy_body_data id;
4779 tree p;
4780 unsigned i;
4781 struct ipa_replace_map *replace_info;
4782 basic_block old_entry_block, bb;
4783 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
4785 tree t_step;
4786 tree old_current_function_decl = current_function_decl;
4787 tree vars = NULL_TREE;
4789 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
4790 && TREE_CODE (new_decl) == FUNCTION_DECL);
4791 DECL_POSSIBLY_INLINED (old_decl) = 1;
4793 old_version_node = cgraph_node (old_decl);
4794 new_version_node = cgraph_node (new_decl);
4796 /* Output the inlining info for this abstract function, since it has been
4797 inlined. If we don't do this now, we can lose the information about the
4798 variables in the function when the blocks get blown away as soon as we
4799 remove the cgraph node. */
4800 (*debug_hooks->outlining_inline_function) (old_decl);
4802 DECL_ARTIFICIAL (new_decl) = 1;
4803 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4804 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
4806 /* Prepare the data structures for the tree copy. */
4807 memset (&id, 0, sizeof (id));
4809 /* Generate a new name for the new version. */
4810 id.statements_to_fold = pointer_set_create ();
4812 id.decl_map = pointer_map_create ();
4813 id.debug_map = NULL;
4814 id.src_fn = old_decl;
4815 id.dst_fn = new_decl;
4816 id.src_node = old_version_node;
4817 id.dst_node = new_version_node;
4818 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4819 if (id.src_node->ipa_transforms_to_apply)
4821 VEC(ipa_opt_pass,heap) * old_transforms_to_apply = id.dst_node->ipa_transforms_to_apply;
4822 unsigned int i;
4824 id.dst_node->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
4825 id.src_node->ipa_transforms_to_apply);
4826 for (i = 0; i < VEC_length (ipa_opt_pass, old_transforms_to_apply); i++)
4827 VEC_safe_push (ipa_opt_pass, heap, id.dst_node->ipa_transforms_to_apply,
4828 VEC_index (ipa_opt_pass,
4829 old_transforms_to_apply,
4830 i));
4833 id.copy_decl = copy_decl_no_change;
4834 id.transform_call_graph_edges
4835 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4836 id.transform_new_cfg = true;
4837 id.transform_return_to_modify = false;
4838 id.transform_lang_insert_block = NULL;
4840 current_function_decl = new_decl;
4841 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4842 (DECL_STRUCT_FUNCTION (old_decl));
4843 initialize_cfun (new_decl, old_decl,
4844 old_entry_block->count);
4845 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4847 /* Copy the function's static chain. */
4848 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4849 if (p)
4850 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4851 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4852 &id);
4854 /* If there's a tree_map, prepare for substitution. */
4855 if (tree_map)
4856 for (i = 0; i < VEC_length (ipa_replace_map_p, tree_map); i++)
4858 gimple init;
4859 replace_info = VEC_index (ipa_replace_map_p, tree_map, i);
4860 if (replace_info->replace_p)
4862 tree op = replace_info->new_tree;
4864 STRIP_NOPS (op);
4866 if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
4867 op = TREE_OPERAND (op, 0);
4869 if (TREE_CODE (op) == ADDR_EXPR)
4871 op = TREE_OPERAND (op, 0);
4872 while (handled_component_p (op))
4873 op = TREE_OPERAND (op, 0);
4874 if (TREE_CODE (op) == VAR_DECL)
4875 add_referenced_var (op);
4877 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
4878 init = setup_one_parameter (&id, replace_info->old_tree,
4879 replace_info->new_tree, id.src_fn,
4880 NULL,
4881 &vars);
4882 if (init)
4883 VEC_safe_push (gimple, heap, init_stmts, init);
4886 /* Copy the function's arguments. */
4887 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4888 DECL_ARGUMENTS (new_decl) =
4889 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
4890 args_to_skip, &vars);
4892 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4894 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4895 number_blocks (id.dst_fn);
4897 declare_inline_vars (DECL_INITIAL (new_decl), vars);
4899 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4900 /* Add local vars. */
4901 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4902 t_step; t_step = TREE_CHAIN (t_step))
4904 tree var = TREE_VALUE (t_step);
4905 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
4906 cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
4907 else if (!can_be_nonlocal (var, &id))
4908 cfun->local_decls =
4909 tree_cons (NULL_TREE, remap_decl (var, &id),
4910 cfun->local_decls);
4913 /* Copy the Function's body. */
4914 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
4915 ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4917 if (DECL_RESULT (old_decl) != NULL_TREE)
4919 tree *res_decl = &DECL_RESULT (old_decl);
4920 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4921 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4924 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4925 number_blocks (new_decl);
4927 /* We want to create the BB unconditionally, so that the addition of
4928 debug stmts doesn't affect BB count, which may in the end cause
4929 codegen differences. */
4930 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
4931 while (VEC_length (gimple, init_stmts))
4932 insert_init_stmt (&id, bb, VEC_pop (gimple, init_stmts));
4933 update_clone_info (&id);
4935 /* Remap the nonlocal_goto_save_area, if any. */
4936 if (cfun->nonlocal_goto_save_area)
4938 struct walk_stmt_info wi;
4940 memset (&wi, 0, sizeof (wi));
4941 wi.info = &id;
4942 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
4945 /* Clean up. */
4946 pointer_map_destroy (id.decl_map);
4947 if (id.debug_map)
4948 pointer_map_destroy (id.debug_map);
4949 free_dominance_info (CDI_DOMINATORS);
4950 free_dominance_info (CDI_POST_DOMINATORS);
4952 fold_marked_statements (0, id.statements_to_fold);
4953 pointer_set_destroy (id.statements_to_fold);
4954 fold_cond_expr_cond ();
4955 delete_unreachable_blocks_update_callgraph (&id);
4956 update_ssa (TODO_update_ssa);
4957 free_dominance_info (CDI_DOMINATORS);
4958 free_dominance_info (CDI_POST_DOMINATORS);
4960 gcc_assert (!id.debug_stmts);
4961 VEC_free (gimple, heap, init_stmts);
4962 pop_cfun ();
4963 current_function_decl = old_current_function_decl;
4964 gcc_assert (!current_function_decl
4965 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4966 return;
4969 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
4970 the callee and return the inlined body on success. */
4972 tree
4973 maybe_inline_call_in_expr (tree exp)
4975 tree fn = get_callee_fndecl (exp);
4977 /* We can only try to inline "const" functions. */
4978 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
4980 struct pointer_map_t *decl_map = pointer_map_create ();
4981 call_expr_arg_iterator iter;
4982 copy_body_data id;
4983 tree param, arg, t;
4985 /* Remap the parameters. */
4986 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
4987 param;
4988 param = TREE_CHAIN (param), arg = next_call_expr_arg (&iter))
4989 *pointer_map_insert (decl_map, param) = arg;
4991 memset (&id, 0, sizeof (id));
4992 id.src_fn = fn;
4993 id.dst_fn = current_function_decl;
4994 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
4995 id.decl_map = decl_map;
4997 id.copy_decl = copy_decl_no_change;
4998 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4999 id.transform_new_cfg = false;
5000 id.transform_return_to_modify = true;
5001 id.transform_lang_insert_block = false;
5003 /* Make sure not to unshare trees behind the front-end's back
5004 since front-end specific mechanisms may rely on sharing. */
5005 id.regimplify = false;
5006 id.do_not_unshare = true;
5008 /* We're not inside any EH region. */
5009 id.eh_lp_nr = 0;
5011 t = copy_tree_body (&id);
5012 pointer_map_destroy (decl_map);
5014 /* We can only return something suitable for use in a GENERIC
5015 expression tree. */
5016 if (TREE_CODE (t) == MODIFY_EXPR)
5017 return TREE_OPERAND (t, 1);
5020 return NULL_TREE;
5023 /* Duplicate a type, fields and all. */
5025 tree
5026 build_duplicate_type (tree type)
5028 struct copy_body_data id;
5030 memset (&id, 0, sizeof (id));
5031 id.src_fn = current_function_decl;
5032 id.dst_fn = current_function_decl;
5033 id.src_cfun = cfun;
5034 id.decl_map = pointer_map_create ();
5035 id.debug_map = NULL;
5036 id.copy_decl = copy_decl_no_change;
5038 type = remap_type_1 (type, &id);
5040 pointer_map_destroy (id.decl_map);
5041 if (id.debug_map)
5042 pointer_map_destroy (id.debug_map);
5044 TYPE_CANONICAL (type) = type;
5046 return type;
5049 /* Return whether it is safe to inline a function because it used different
5050 target specific options or call site actual types mismatch parameter types.
5051 E is the call edge to be checked. */
5052 bool
5053 tree_can_inline_p (struct cgraph_edge *e)
5055 #if 0
5056 /* This causes a regression in SPEC in that it prevents a cold function from
5057 inlining a hot function. Perhaps this should only apply to functions
5058 that the user declares hot/cold/optimize explicitly. */
5060 /* Don't inline a function with a higher optimization level than the
5061 caller, or with different space constraints (hot/cold functions). */
5062 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
5063 tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
5065 if (caller_tree != callee_tree)
5067 struct cl_optimization *caller_opt
5068 = TREE_OPTIMIZATION ((caller_tree)
5069 ? caller_tree
5070 : optimization_default_node);
5072 struct cl_optimization *callee_opt
5073 = TREE_OPTIMIZATION ((callee_tree)
5074 ? callee_tree
5075 : optimization_default_node);
5077 if ((caller_opt->optimize > callee_opt->optimize)
5078 || (caller_opt->optimize_size != callee_opt->optimize_size))
5079 return false;
5081 #endif
5082 tree caller, callee, lhs;
5084 caller = e->caller->decl;
5085 callee = e->callee->decl;
5087 /* We cannot inline a function that uses a different EH personality
5088 than the caller. */
5089 if (DECL_FUNCTION_PERSONALITY (caller)
5090 && DECL_FUNCTION_PERSONALITY (callee)
5091 && (DECL_FUNCTION_PERSONALITY (caller)
5092 != DECL_FUNCTION_PERSONALITY (callee)))
5094 e->inline_failed = CIF_UNSPECIFIED;
5095 gimple_call_set_cannot_inline (e->call_stmt, true);
5096 return false;
5099 /* Allow the backend to decide if inlining is ok. */
5100 if (!targetm.target_option.can_inline_p (caller, callee))
5102 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
5103 gimple_call_set_cannot_inline (e->call_stmt, true);
5104 e->call_stmt_cannot_inline_p = true;
5105 return false;
5108 /* Do not inline calls where we cannot triviall work around mismatches
5109 in argument or return types. */
5110 if (e->call_stmt
5111 && ((DECL_RESULT (callee)
5112 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
5113 && (lhs = gimple_call_lhs (e->call_stmt)) != NULL_TREE
5114 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
5115 TREE_TYPE (lhs))
5116 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
5117 || !gimple_check_call_args (e->call_stmt)))
5119 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
5120 gimple_call_set_cannot_inline (e->call_stmt, true);
5121 e->call_stmt_cannot_inline_p = true;
5122 return false;
5125 return true;