svn merge -r 216846:217483 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / tree-inline.c
blob54b351421cab31a1893db798f691184cfbd4ad00
1 /* Tree inlining.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "tree-inline.h"
30 #include "flags.h"
31 #include "params.h"
32 #include "input.h"
33 #include "insn-config.h"
34 #include "hashtab.h"
35 #include "langhooks.h"
36 #include "predict.h"
37 #include "vec.h"
38 #include "hash-set.h"
39 #include "machmode.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfganal.h"
45 #include "basic-block.h"
46 #include "tree-iterator.h"
47 #include "intl.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "gimple-fold.h"
51 #include "tree-eh.h"
52 #include "gimple-expr.h"
53 #include "is-a.h"
54 #include "gimple.h"
55 #include "gimplify.h"
56 #include "gimple-iterator.h"
57 #include "gimplify-me.h"
58 #include "gimple-walk.h"
59 #include "gimple-ssa.h"
60 #include "tree-cfg.h"
61 #include "tree-phinodes.h"
62 #include "ssa-iterators.h"
63 #include "stringpool.h"
64 #include "tree-ssanames.h"
65 #include "tree-into-ssa.h"
66 #include "expr.h"
67 #include "tree-dfa.h"
68 #include "tree-ssa.h"
69 #include "tree-pretty-print.h"
70 #include "except.h"
71 #include "debug.h"
72 #include "hash-map.h"
73 #include "plugin-api.h"
74 #include "ipa-ref.h"
75 #include "cgraph.h"
76 #include "alloc-pool.h"
77 #include "ipa-prop.h"
78 #include "value-prof.h"
79 #include "tree-pass.h"
80 #include "target.h"
81 #include "cfgloop.h"
82 #include "builtins.h"
83 #include "tree-chkp.h"
85 #include "rtl.h" /* FIXME: For asm_str_count. */
87 /* I'm not real happy about this, but we need to handle gimple and
88 non-gimple trees. */
90 /* Inlining, Cloning, Versioning, Parallelization
92 Inlining: a function body is duplicated, but the PARM_DECLs are
93 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
94 MODIFY_EXPRs that store to a dedicated returned-value variable.
95 The duplicated eh_region info of the copy will later be appended
96 to the info for the caller; the eh_region info in copied throwing
97 statements and RESX statements are adjusted accordingly.
99 Cloning: (only in C++) We have one body for a con/de/structor, and
100 multiple function decls, each with a unique parameter list.
101 Duplicate the body, using the given splay tree; some parameters
102 will become constants (like 0 or 1).
104 Versioning: a function body is duplicated and the result is a new
105 function rather than into blocks of an existing function as with
106 inlining. Some parameters will become constants.
108 Parallelization: a region of a function is duplicated resulting in
109 a new function. Variables may be replaced with complex expressions
110 to enable shared variable semantics.
112 All of these will simultaneously lookup any callgraph edges. If
113 we're going to inline the duplicated function body, and the given
114 function has some cloned callgraph nodes (one for each place this
115 function will be inlined) those callgraph edges will be duplicated.
116 If we're cloning the body, those callgraph edges will be
117 updated to point into the new body. (Note that the original
118 callgraph node and edge list will not be altered.)
120 See the CALL_EXPR handling case in copy_tree_body_r (). */
122 /* To Do:
124 o In order to make inlining-on-trees work, we pessimized
125 function-local static constants. In particular, they are now
126 always output, even when not addressed. Fix this by treating
127 function-local static constants just like global static
128 constants; the back-end already knows not to output them if they
129 are not needed.
131 o Provide heuristics to clamp inlining of recursive template
132 calls? */
135 /* Weights that estimate_num_insns uses to estimate the size of the
136 produced code. */
138 eni_weights eni_size_weights;
140 /* Weights that estimate_num_insns uses to estimate the time necessary
141 to execute the produced code. */
143 eni_weights eni_time_weights;
145 /* Prototypes. */
147 static tree declare_return_variable (copy_body_data *, tree, tree, tree,
148 basic_block);
149 static void remap_block (tree *, copy_body_data *);
150 static void copy_bind_expr (tree *, int *, copy_body_data *);
151 static void declare_inline_vars (tree, tree);
152 static void remap_save_expr (tree *, hash_map<tree, tree> *, int *);
153 static void prepend_lexical_block (tree current_block, tree new_block);
154 static tree copy_decl_to_var (tree, copy_body_data *);
155 static tree copy_result_decl_to_var (tree, copy_body_data *);
156 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
157 static gimple_seq remap_gimple_stmt (gimple, copy_body_data *);
158 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
159 static void insert_init_stmt (copy_body_data *, basic_block, gimple);
161 /* Insert a tree->tree mapping for ID. Despite the name suggests
162 that the trees should be variables, it is used for more than that. */
164 void
165 insert_decl_map (copy_body_data *id, tree key, tree value)
167 id->decl_map->put (key, value);
169 /* Always insert an identity map as well. If we see this same new
170 node again, we won't want to duplicate it a second time. */
171 if (key != value)
172 id->decl_map->put (value, value);
175 /* Insert a tree->tree mapping for ID. This is only used for
176 variables. */
178 static void
179 insert_debug_decl_map (copy_body_data *id, tree key, tree value)
181 if (!gimple_in_ssa_p (id->src_cfun))
182 return;
184 if (!MAY_HAVE_DEBUG_STMTS)
185 return;
187 if (!target_for_debug_bind (key))
188 return;
190 gcc_assert (TREE_CODE (key) == PARM_DECL);
191 gcc_assert (TREE_CODE (value) == VAR_DECL);
193 if (!id->debug_map)
194 id->debug_map = new hash_map<tree, tree>;
196 id->debug_map->put (key, value);
199 /* If nonzero, we're remapping the contents of inlined debug
200 statements. If negative, an error has occurred, such as a
201 reference to a variable that isn't available in the inlined
202 context. */
203 static int processing_debug_stmt = 0;
205 /* Construct new SSA name for old NAME. ID is the inline context. */
207 static tree
208 remap_ssa_name (tree name, copy_body_data *id)
210 tree new_tree, var;
211 tree *n;
213 gcc_assert (TREE_CODE (name) == SSA_NAME);
215 n = id->decl_map->get (name);
216 if (n)
217 return unshare_expr (*n);
219 if (processing_debug_stmt)
221 if (SSA_NAME_IS_DEFAULT_DEF (name)
222 && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
223 && id->entry_bb == NULL
224 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
226 tree vexpr = make_node (DEBUG_EXPR_DECL);
227 gimple def_temp;
228 gimple_stmt_iterator gsi;
229 tree val = SSA_NAME_VAR (name);
231 n = id->decl_map->get (val);
232 if (n != NULL)
233 val = *n;
234 if (TREE_CODE (val) != PARM_DECL)
236 processing_debug_stmt = -1;
237 return name;
239 def_temp = gimple_build_debug_source_bind (vexpr, val, NULL);
240 DECL_ARTIFICIAL (vexpr) = 1;
241 TREE_TYPE (vexpr) = TREE_TYPE (name);
242 DECL_MODE (vexpr) = DECL_MODE (SSA_NAME_VAR (name));
243 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
244 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
245 return vexpr;
248 processing_debug_stmt = -1;
249 return name;
252 /* Remap anonymous SSA names or SSA names of anonymous decls. */
253 var = SSA_NAME_VAR (name);
254 if (!var
255 || (!SSA_NAME_IS_DEFAULT_DEF (name)
256 && TREE_CODE (var) == VAR_DECL
257 && !VAR_DECL_IS_VIRTUAL_OPERAND (var)
258 && DECL_ARTIFICIAL (var)
259 && DECL_IGNORED_P (var)
260 && !DECL_NAME (var)))
262 struct ptr_info_def *pi;
263 new_tree = make_ssa_name (remap_type (TREE_TYPE (name), id), NULL);
264 if (!var && SSA_NAME_IDENTIFIER (name))
265 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_tree, SSA_NAME_IDENTIFIER (name));
266 insert_decl_map (id, name, new_tree);
267 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
268 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
269 /* At least IPA points-to info can be directly transferred. */
270 if (id->src_cfun->gimple_df
271 && id->src_cfun->gimple_df->ipa_pta
272 && (pi = SSA_NAME_PTR_INFO (name))
273 && !pi->pt.anything)
275 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
276 new_pi->pt = pi->pt;
278 return new_tree;
281 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
282 in copy_bb. */
283 new_tree = remap_decl (var, id);
285 /* We might've substituted constant or another SSA_NAME for
286 the variable.
288 Replace the SSA name representing RESULT_DECL by variable during
289 inlining: this saves us from need to introduce PHI node in a case
290 return value is just partly initialized. */
291 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
292 && (!SSA_NAME_VAR (name)
293 || TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
294 || !id->transform_return_to_modify))
296 struct ptr_info_def *pi;
297 new_tree = make_ssa_name (new_tree, NULL);
298 insert_decl_map (id, name, new_tree);
299 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
300 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
301 /* At least IPA points-to info can be directly transferred. */
302 if (id->src_cfun->gimple_df
303 && id->src_cfun->gimple_df->ipa_pta
304 && (pi = SSA_NAME_PTR_INFO (name))
305 && !pi->pt.anything)
307 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
308 new_pi->pt = pi->pt;
310 if (SSA_NAME_IS_DEFAULT_DEF (name))
312 /* By inlining function having uninitialized variable, we might
313 extend the lifetime (variable might get reused). This cause
314 ICE in the case we end up extending lifetime of SSA name across
315 abnormal edge, but also increase register pressure.
317 We simply initialize all uninitialized vars by 0 except
318 for case we are inlining to very first BB. We can avoid
319 this for all BBs that are not inside strongly connected
320 regions of the CFG, but this is expensive to test. */
321 if (id->entry_bb
322 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
323 && (!SSA_NAME_VAR (name)
324 || TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL)
325 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun),
326 0)->dest
327 || EDGE_COUNT (id->entry_bb->preds) != 1))
329 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
330 gimple init_stmt;
331 tree zero = build_zero_cst (TREE_TYPE (new_tree));
333 init_stmt = gimple_build_assign (new_tree, zero);
334 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
335 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
337 else
339 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
340 set_ssa_default_def (cfun, SSA_NAME_VAR (new_tree), new_tree);
344 else
345 insert_decl_map (id, name, new_tree);
346 return new_tree;
349 /* Remap DECL during the copying of the BLOCK tree for the function. */
351 tree
352 remap_decl (tree decl, copy_body_data *id)
354 tree *n;
356 /* We only remap local variables in the current function. */
358 /* See if we have remapped this declaration. */
360 n = id->decl_map->get (decl);
362 if (!n && processing_debug_stmt)
364 processing_debug_stmt = -1;
365 return decl;
368 /* If we didn't already have an equivalent for this declaration,
369 create one now. */
370 if (!n)
372 /* Make a copy of the variable or label. */
373 tree t = id->copy_decl (decl, id);
375 /* Remember it, so that if we encounter this local entity again
376 we can reuse this copy. Do this early because remap_type may
377 need this decl for TYPE_STUB_DECL. */
378 insert_decl_map (id, decl, t);
380 if (!DECL_P (t))
381 return t;
383 /* Remap types, if necessary. */
384 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
385 if (TREE_CODE (t) == TYPE_DECL)
386 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
388 /* Remap sizes as necessary. */
389 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
390 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
392 /* If fields, do likewise for offset and qualifier. */
393 if (TREE_CODE (t) == FIELD_DECL)
395 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
396 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
397 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
400 return t;
403 if (id->do_not_unshare)
404 return *n;
405 else
406 return unshare_expr (*n);
409 static tree
410 remap_type_1 (tree type, copy_body_data *id)
412 tree new_tree, t;
414 /* We do need a copy. build and register it now. If this is a pointer or
415 reference type, remap the designated type and make a new pointer or
416 reference type. */
417 if (TREE_CODE (type) == POINTER_TYPE)
419 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
420 TYPE_MODE (type),
421 TYPE_REF_CAN_ALIAS_ALL (type));
422 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
423 new_tree = build_type_attribute_qual_variant (new_tree,
424 TYPE_ATTRIBUTES (type),
425 TYPE_QUALS (type));
426 insert_decl_map (id, type, new_tree);
427 return new_tree;
429 else if (TREE_CODE (type) == REFERENCE_TYPE)
431 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
432 TYPE_MODE (type),
433 TYPE_REF_CAN_ALIAS_ALL (type));
434 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
435 new_tree = build_type_attribute_qual_variant (new_tree,
436 TYPE_ATTRIBUTES (type),
437 TYPE_QUALS (type));
438 insert_decl_map (id, type, new_tree);
439 return new_tree;
441 else
442 new_tree = copy_node (type);
444 insert_decl_map (id, type, new_tree);
446 /* This is a new type, not a copy of an old type. Need to reassociate
447 variants. We can handle everything except the main variant lazily. */
448 t = TYPE_MAIN_VARIANT (type);
449 if (type != t)
451 t = remap_type (t, id);
452 TYPE_MAIN_VARIANT (new_tree) = t;
453 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
454 TYPE_NEXT_VARIANT (t) = new_tree;
456 else
458 TYPE_MAIN_VARIANT (new_tree) = new_tree;
459 TYPE_NEXT_VARIANT (new_tree) = NULL;
462 if (TYPE_STUB_DECL (type))
463 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
465 /* Lazily create pointer and reference types. */
466 TYPE_POINTER_TO (new_tree) = NULL;
467 TYPE_REFERENCE_TO (new_tree) = NULL;
469 /* Copy all types that may contain references to local variables; be sure to
470 preserve sharing in between type and its main variant when possible. */
471 switch (TREE_CODE (new_tree))
473 case INTEGER_TYPE:
474 case REAL_TYPE:
475 case FIXED_POINT_TYPE:
476 case ENUMERAL_TYPE:
477 case BOOLEAN_TYPE:
478 if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
480 gcc_checking_assert (TYPE_MIN_VALUE (type) == TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (type)));
481 gcc_checking_assert (TYPE_MAX_VALUE (type) == TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (type)));
483 TYPE_MIN_VALUE (new_tree) = TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (new_tree));
484 TYPE_MAX_VALUE (new_tree) = TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (new_tree));
486 else
488 t = TYPE_MIN_VALUE (new_tree);
489 if (t && TREE_CODE (t) != INTEGER_CST)
490 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
492 t = TYPE_MAX_VALUE (new_tree);
493 if (t && TREE_CODE (t) != INTEGER_CST)
494 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
496 return new_tree;
498 case FUNCTION_TYPE:
499 if (TYPE_MAIN_VARIANT (new_tree) != new_tree
500 && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
501 TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
502 else
503 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
504 if (TYPE_MAIN_VARIANT (new_tree) != new_tree
505 && TYPE_ARG_TYPES (type) == TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (type)))
506 TYPE_ARG_TYPES (new_tree) = TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (new_tree));
507 else
508 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
509 return new_tree;
511 case ARRAY_TYPE:
512 if (TYPE_MAIN_VARIANT (new_tree) != new_tree
513 && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
514 TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
515 else
516 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
518 if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
520 gcc_checking_assert (TYPE_DOMAIN (type) == TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)));
521 TYPE_DOMAIN (new_tree) = TYPE_DOMAIN (TYPE_MAIN_VARIANT (new_tree));
523 else
524 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
525 break;
527 case RECORD_TYPE:
528 case UNION_TYPE:
529 case QUAL_UNION_TYPE:
530 if (TYPE_MAIN_VARIANT (type) != type
531 && TYPE_FIELDS (type) == TYPE_FIELDS (TYPE_MAIN_VARIANT (type)))
532 TYPE_FIELDS (new_tree) = TYPE_FIELDS (TYPE_MAIN_VARIANT (new_tree));
533 else
535 tree f, nf = NULL;
537 for (f = TYPE_FIELDS (new_tree); f ; f = DECL_CHAIN (f))
539 t = remap_decl (f, id);
540 DECL_CONTEXT (t) = new_tree;
541 DECL_CHAIN (t) = nf;
542 nf = t;
544 TYPE_FIELDS (new_tree) = nreverse (nf);
546 break;
548 case OFFSET_TYPE:
549 default:
550 /* Shouldn't have been thought variable sized. */
551 gcc_unreachable ();
554 /* All variants of type share the same size, so use the already remaped data. */
555 if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
557 gcc_checking_assert (TYPE_SIZE (type) == TYPE_SIZE (TYPE_MAIN_VARIANT (type)));
558 gcc_checking_assert (TYPE_SIZE_UNIT (type) == TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type)));
560 TYPE_SIZE (new_tree) = TYPE_SIZE (TYPE_MAIN_VARIANT (new_tree));
561 TYPE_SIZE_UNIT (new_tree) = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (new_tree));
563 else
565 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
566 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
569 return new_tree;
572 tree
573 remap_type (tree type, copy_body_data *id)
575 tree *node;
576 tree tmp;
578 if (type == NULL)
579 return type;
581 /* See if we have remapped this type. */
582 node = id->decl_map->get (type);
583 if (node)
584 return *node;
586 /* The type only needs remapping if it's variably modified. */
587 if (! variably_modified_type_p (type, id->src_fn))
589 insert_decl_map (id, type, type);
590 return type;
593 id->remapping_type_depth++;
594 tmp = remap_type_1 (type, id);
595 id->remapping_type_depth--;
597 return tmp;
600 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
602 static bool
603 can_be_nonlocal (tree decl, copy_body_data *id)
605 /* We can not duplicate function decls. */
606 if (TREE_CODE (decl) == FUNCTION_DECL)
607 return true;
609 /* Local static vars must be non-local or we get multiple declaration
610 problems. */
611 if (TREE_CODE (decl) == VAR_DECL
612 && !auto_var_in_fn_p (decl, id->src_fn))
613 return true;
615 return false;
618 static tree
619 remap_decls (tree decls, vec<tree, va_gc> **nonlocalized_list,
620 copy_body_data *id)
622 tree old_var;
623 tree new_decls = NULL_TREE;
625 /* Remap its variables. */
626 for (old_var = decls; old_var; old_var = DECL_CHAIN (old_var))
628 tree new_var;
630 if (can_be_nonlocal (old_var, id))
632 /* We need to add this variable to the local decls as otherwise
633 nothing else will do so. */
634 if (TREE_CODE (old_var) == VAR_DECL
635 && ! DECL_EXTERNAL (old_var))
636 add_local_decl (cfun, old_var);
637 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
638 && !DECL_IGNORED_P (old_var)
639 && nonlocalized_list)
640 vec_safe_push (*nonlocalized_list, old_var);
641 continue;
644 /* Remap the variable. */
645 new_var = remap_decl (old_var, id);
647 /* If we didn't remap this variable, we can't mess with its
648 TREE_CHAIN. If we remapped this variable to the return slot, it's
649 already declared somewhere else, so don't declare it here. */
651 if (new_var == id->retvar)
653 else if (!new_var)
655 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
656 && !DECL_IGNORED_P (old_var)
657 && nonlocalized_list)
658 vec_safe_push (*nonlocalized_list, old_var);
660 else
662 gcc_assert (DECL_P (new_var));
663 DECL_CHAIN (new_var) = new_decls;
664 new_decls = new_var;
666 /* Also copy value-expressions. */
667 if (TREE_CODE (new_var) == VAR_DECL
668 && DECL_HAS_VALUE_EXPR_P (new_var))
670 tree tem = DECL_VALUE_EXPR (new_var);
671 bool old_regimplify = id->regimplify;
672 id->remapping_type_depth++;
673 walk_tree (&tem, copy_tree_body_r, id, NULL);
674 id->remapping_type_depth--;
675 id->regimplify = old_regimplify;
676 SET_DECL_VALUE_EXPR (new_var, tem);
681 return nreverse (new_decls);
684 /* Copy the BLOCK to contain remapped versions of the variables
685 therein. And hook the new block into the block-tree. */
687 static void
688 remap_block (tree *block, copy_body_data *id)
690 tree old_block;
691 tree new_block;
693 /* Make the new block. */
694 old_block = *block;
695 new_block = make_node (BLOCK);
696 TREE_USED (new_block) = TREE_USED (old_block);
697 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
698 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
699 BLOCK_NONLOCALIZED_VARS (new_block)
700 = vec_safe_copy (BLOCK_NONLOCALIZED_VARS (old_block));
701 *block = new_block;
703 /* Remap its variables. */
704 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
705 &BLOCK_NONLOCALIZED_VARS (new_block),
706 id);
708 if (id->transform_lang_insert_block)
709 id->transform_lang_insert_block (new_block);
711 /* Remember the remapped block. */
712 insert_decl_map (id, old_block, new_block);
715 /* Copy the whole block tree and root it in id->block. */
716 static tree
717 remap_blocks (tree block, copy_body_data *id)
719 tree t;
720 tree new_tree = block;
722 if (!block)
723 return NULL;
725 remap_block (&new_tree, id);
726 gcc_assert (new_tree != block);
727 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
728 prepend_lexical_block (new_tree, remap_blocks (t, id));
729 /* Blocks are in arbitrary order, but make things slightly prettier and do
730 not swap order when producing a copy. */
731 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
732 return new_tree;
735 /* Remap the block tree rooted at BLOCK to nothing. */
736 static void
737 remap_blocks_to_null (tree block, copy_body_data *id)
739 tree t;
740 insert_decl_map (id, block, NULL_TREE);
741 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
742 remap_blocks_to_null (t, id);
745 static void
746 copy_statement_list (tree *tp)
748 tree_stmt_iterator oi, ni;
749 tree new_tree;
751 new_tree = alloc_stmt_list ();
752 ni = tsi_start (new_tree);
753 oi = tsi_start (*tp);
754 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
755 *tp = new_tree;
757 for (; !tsi_end_p (oi); tsi_next (&oi))
759 tree stmt = tsi_stmt (oi);
760 if (TREE_CODE (stmt) == STATEMENT_LIST)
761 /* This copy is not redundant; tsi_link_after will smash this
762 STATEMENT_LIST into the end of the one we're building, and we
763 don't want to do that with the original. */
764 copy_statement_list (&stmt);
765 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
769 static void
770 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
772 tree block = BIND_EXPR_BLOCK (*tp);
773 /* Copy (and replace) the statement. */
774 copy_tree_r (tp, walk_subtrees, NULL);
775 if (block)
777 remap_block (&block, id);
778 BIND_EXPR_BLOCK (*tp) = block;
781 if (BIND_EXPR_VARS (*tp))
782 /* This will remap a lot of the same decls again, but this should be
783 harmless. */
784 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
788 /* Create a new gimple_seq by remapping all the statements in BODY
789 using the inlining information in ID. */
791 static gimple_seq
792 remap_gimple_seq (gimple_seq body, copy_body_data *id)
794 gimple_stmt_iterator si;
795 gimple_seq new_body = NULL;
797 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
799 gimple_seq new_stmts = remap_gimple_stmt (gsi_stmt (si), id);
800 gimple_seq_add_seq (&new_body, new_stmts);
803 return new_body;
807 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
808 block using the mapping information in ID. */
810 static gimple
811 copy_gimple_bind (gimple stmt, copy_body_data *id)
813 gimple new_bind;
814 tree new_block, new_vars;
815 gimple_seq body, new_body;
817 /* Copy the statement. Note that we purposely don't use copy_stmt
818 here because we need to remap statements as we copy. */
819 body = gimple_bind_body (stmt);
820 new_body = remap_gimple_seq (body, id);
822 new_block = gimple_bind_block (stmt);
823 if (new_block)
824 remap_block (&new_block, id);
826 /* This will remap a lot of the same decls again, but this should be
827 harmless. */
828 new_vars = gimple_bind_vars (stmt);
829 if (new_vars)
830 new_vars = remap_decls (new_vars, NULL, id);
832 new_bind = gimple_build_bind (new_vars, new_body, new_block);
834 return new_bind;
837 /* Return true if DECL is a parameter or a SSA_NAME for a parameter. */
839 static bool
840 is_parm (tree decl)
842 if (TREE_CODE (decl) == SSA_NAME)
844 decl = SSA_NAME_VAR (decl);
845 if (!decl)
846 return false;
849 return (TREE_CODE (decl) == PARM_DECL);
852 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
853 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
854 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
855 recursing into the children nodes of *TP. */
857 static tree
858 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
860 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
861 copy_body_data *id = (copy_body_data *) wi_p->info;
862 tree fn = id->src_fn;
864 if (TREE_CODE (*tp) == SSA_NAME)
866 *tp = remap_ssa_name (*tp, id);
867 *walk_subtrees = 0;
868 return NULL;
870 else if (auto_var_in_fn_p (*tp, fn))
872 /* Local variables and labels need to be replaced by equivalent
873 variables. We don't want to copy static variables; there's
874 only one of those, no matter how many times we inline the
875 containing function. Similarly for globals from an outer
876 function. */
877 tree new_decl;
879 /* Remap the declaration. */
880 new_decl = remap_decl (*tp, id);
881 gcc_assert (new_decl);
882 /* Replace this variable with the copy. */
883 STRIP_TYPE_NOPS (new_decl);
884 /* ??? The C++ frontend uses void * pointer zero to initialize
885 any other type. This confuses the middle-end type verification.
886 As cloned bodies do not go through gimplification again the fixup
887 there doesn't trigger. */
888 if (TREE_CODE (new_decl) == INTEGER_CST
889 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
890 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
891 *tp = new_decl;
892 *walk_subtrees = 0;
894 else if (TREE_CODE (*tp) == STATEMENT_LIST)
895 gcc_unreachable ();
896 else if (TREE_CODE (*tp) == SAVE_EXPR)
897 gcc_unreachable ();
898 else if (TREE_CODE (*tp) == LABEL_DECL
899 && (!DECL_CONTEXT (*tp)
900 || decl_function_context (*tp) == id->src_fn))
901 /* These may need to be remapped for EH handling. */
902 *tp = remap_decl (*tp, id);
903 else if (TREE_CODE (*tp) == FIELD_DECL)
905 /* If the enclosing record type is variably_modified_type_p, the field
906 has already been remapped. Otherwise, it need not be. */
907 tree *n = id->decl_map->get (*tp);
908 if (n)
909 *tp = *n;
910 *walk_subtrees = 0;
912 else if (TYPE_P (*tp))
913 /* Types may need remapping as well. */
914 *tp = remap_type (*tp, id);
915 else if (CONSTANT_CLASS_P (*tp))
917 /* If this is a constant, we have to copy the node iff the type
918 will be remapped. copy_tree_r will not copy a constant. */
919 tree new_type = remap_type (TREE_TYPE (*tp), id);
921 if (new_type == TREE_TYPE (*tp))
922 *walk_subtrees = 0;
924 else if (TREE_CODE (*tp) == INTEGER_CST)
925 *tp = wide_int_to_tree (new_type, *tp);
926 else
928 *tp = copy_node (*tp);
929 TREE_TYPE (*tp) = new_type;
932 else
934 /* Otherwise, just copy the node. Note that copy_tree_r already
935 knows not to copy VAR_DECLs, etc., so this is safe. */
937 if (TREE_CODE (*tp) == MEM_REF)
939 /* We need to re-canonicalize MEM_REFs from inline substitutions
940 that can happen when a pointer argument is an ADDR_EXPR.
941 Recurse here manually to allow that. */
942 tree ptr = TREE_OPERAND (*tp, 0);
943 tree type = remap_type (TREE_TYPE (*tp), id);
944 tree old = *tp;
945 walk_tree (&ptr, remap_gimple_op_r, data, NULL);
946 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
947 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
948 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
949 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
950 /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
951 remapped a parameter as the property might be valid only
952 for the parameter itself. */
953 if (TREE_THIS_NOTRAP (old)
954 && (!is_parm (TREE_OPERAND (old, 0))
955 || (!id->transform_parameter && is_parm (ptr))))
956 TREE_THIS_NOTRAP (*tp) = 1;
957 *walk_subtrees = 0;
958 return NULL;
961 /* Here is the "usual case". Copy this tree node, and then
962 tweak some special cases. */
963 copy_tree_r (tp, walk_subtrees, NULL);
965 if (TREE_CODE (*tp) != OMP_CLAUSE)
966 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
968 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
970 /* The copied TARGET_EXPR has never been expanded, even if the
971 original node was expanded already. */
972 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
973 TREE_OPERAND (*tp, 3) = NULL_TREE;
975 else if (TREE_CODE (*tp) == ADDR_EXPR)
977 /* Variable substitution need not be simple. In particular,
978 the MEM_REF substitution above. Make sure that
979 TREE_CONSTANT and friends are up-to-date. */
980 int invariant = is_gimple_min_invariant (*tp);
981 walk_tree (&TREE_OPERAND (*tp, 0), remap_gimple_op_r, data, NULL);
982 recompute_tree_invariant_for_addr_expr (*tp);
984 /* If this used to be invariant, but is not any longer,
985 then regimplification is probably needed. */
986 if (invariant && !is_gimple_min_invariant (*tp))
987 id->regimplify = true;
989 *walk_subtrees = 0;
993 /* Update the TREE_BLOCK for the cloned expr. */
994 if (EXPR_P (*tp))
996 tree new_block = id->remapping_type_depth == 0 ? id->block : NULL;
997 tree old_block = TREE_BLOCK (*tp);
998 if (old_block)
1000 tree *n;
1001 n = id->decl_map->get (TREE_BLOCK (*tp));
1002 if (n)
1003 new_block = *n;
1005 TREE_SET_BLOCK (*tp, new_block);
1008 /* Keep iterating. */
1009 return NULL_TREE;
1013 /* Called from copy_body_id via walk_tree. DATA is really a
1014 `copy_body_data *'. */
1016 tree
1017 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
1019 copy_body_data *id = (copy_body_data *) data;
1020 tree fn = id->src_fn;
1021 tree new_block;
1023 /* Begin by recognizing trees that we'll completely rewrite for the
1024 inlining context. Our output for these trees is completely
1025 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1026 into an edge). Further down, we'll handle trees that get
1027 duplicated and/or tweaked. */
1029 /* When requested, RETURN_EXPRs should be transformed to just the
1030 contained MODIFY_EXPR. The branch semantics of the return will
1031 be handled elsewhere by manipulating the CFG rather than a statement. */
1032 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
1034 tree assignment = TREE_OPERAND (*tp, 0);
1036 /* If we're returning something, just turn that into an
1037 assignment into the equivalent of the original RESULT_DECL.
1038 If the "assignment" is just the result decl, the result
1039 decl has already been set (e.g. a recent "foo (&result_decl,
1040 ...)"); just toss the entire RETURN_EXPR. */
1041 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
1043 /* Replace the RETURN_EXPR with (a copy of) the
1044 MODIFY_EXPR hanging underneath. */
1045 *tp = copy_node (assignment);
1047 else /* Else the RETURN_EXPR returns no value. */
1049 *tp = NULL;
1050 return (tree) (void *)1;
1053 else if (TREE_CODE (*tp) == SSA_NAME)
1055 *tp = remap_ssa_name (*tp, id);
1056 *walk_subtrees = 0;
1057 return NULL;
1060 /* Local variables and labels need to be replaced by equivalent
1061 variables. We don't want to copy static variables; there's only
1062 one of those, no matter how many times we inline the containing
1063 function. Similarly for globals from an outer function. */
1064 else if (auto_var_in_fn_p (*tp, fn))
1066 tree new_decl;
1068 /* Remap the declaration. */
1069 new_decl = remap_decl (*tp, id);
1070 gcc_assert (new_decl);
1071 /* Replace this variable with the copy. */
1072 STRIP_TYPE_NOPS (new_decl);
1073 *tp = new_decl;
1074 *walk_subtrees = 0;
1076 else if (TREE_CODE (*tp) == STATEMENT_LIST)
1077 copy_statement_list (tp);
1078 else if (TREE_CODE (*tp) == SAVE_EXPR
1079 || TREE_CODE (*tp) == TARGET_EXPR)
1080 remap_save_expr (tp, id->decl_map, walk_subtrees);
1081 else if (TREE_CODE (*tp) == LABEL_DECL
1082 && (! DECL_CONTEXT (*tp)
1083 || decl_function_context (*tp) == id->src_fn))
1084 /* These may need to be remapped for EH handling. */
1085 *tp = remap_decl (*tp, id);
1086 else if (TREE_CODE (*tp) == BIND_EXPR)
1087 copy_bind_expr (tp, walk_subtrees, id);
1088 /* Types may need remapping as well. */
1089 else if (TYPE_P (*tp))
1090 *tp = remap_type (*tp, id);
1092 /* If this is a constant, we have to copy the node iff the type will be
1093 remapped. copy_tree_r will not copy a constant. */
1094 else if (CONSTANT_CLASS_P (*tp))
1096 tree new_type = remap_type (TREE_TYPE (*tp), id);
1098 if (new_type == TREE_TYPE (*tp))
1099 *walk_subtrees = 0;
1101 else if (TREE_CODE (*tp) == INTEGER_CST)
1102 *tp = wide_int_to_tree (new_type, *tp);
1103 else
1105 *tp = copy_node (*tp);
1106 TREE_TYPE (*tp) = new_type;
1110 /* Otherwise, just copy the node. Note that copy_tree_r already
1111 knows not to copy VAR_DECLs, etc., so this is safe. */
1112 else
1114 /* Here we handle trees that are not completely rewritten.
1115 First we detect some inlining-induced bogosities for
1116 discarding. */
1117 if (TREE_CODE (*tp) == MODIFY_EXPR
1118 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
1119 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
1121 /* Some assignments VAR = VAR; don't generate any rtl code
1122 and thus don't count as variable modification. Avoid
1123 keeping bogosities like 0 = 0. */
1124 tree decl = TREE_OPERAND (*tp, 0), value;
1125 tree *n;
1127 n = id->decl_map->get (decl);
1128 if (n)
1130 value = *n;
1131 STRIP_TYPE_NOPS (value);
1132 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1134 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
1135 return copy_tree_body_r (tp, walk_subtrees, data);
1139 else if (TREE_CODE (*tp) == INDIRECT_REF)
1141 /* Get rid of *& from inline substitutions that can happen when a
1142 pointer argument is an ADDR_EXPR. */
1143 tree decl = TREE_OPERAND (*tp, 0);
1144 tree *n = id->decl_map->get (decl);
1145 if (n)
1147 /* If we happen to get an ADDR_EXPR in n->value, strip
1148 it manually here as we'll eventually get ADDR_EXPRs
1149 which lie about their types pointed to. In this case
1150 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1151 but we absolutely rely on that. As fold_indirect_ref
1152 does other useful transformations, try that first, though. */
1153 tree type = TREE_TYPE (*tp);
1154 tree ptr = id->do_not_unshare ? *n : unshare_expr (*n);
1155 tree old = *tp;
1156 *tp = gimple_fold_indirect_ref (ptr);
1157 if (! *tp)
1159 if (TREE_CODE (ptr) == ADDR_EXPR)
1162 = fold_indirect_ref_1 (EXPR_LOCATION (ptr), type, ptr);
1163 /* ??? We should either assert here or build
1164 a VIEW_CONVERT_EXPR instead of blindly leaking
1165 incompatible types to our IL. */
1166 if (! *tp)
1167 *tp = TREE_OPERAND (ptr, 0);
1169 else
1171 *tp = build1 (INDIRECT_REF, type, ptr);
1172 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1173 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1174 TREE_READONLY (*tp) = TREE_READONLY (old);
1175 /* We cannot propagate the TREE_THIS_NOTRAP flag if we
1176 have remapped a parameter as the property might be
1177 valid only for the parameter itself. */
1178 if (TREE_THIS_NOTRAP (old)
1179 && (!is_parm (TREE_OPERAND (old, 0))
1180 || (!id->transform_parameter && is_parm (ptr))))
1181 TREE_THIS_NOTRAP (*tp) = 1;
1184 *walk_subtrees = 0;
1185 return NULL;
1188 else if (TREE_CODE (*tp) == MEM_REF)
1190 /* We need to re-canonicalize MEM_REFs from inline substitutions
1191 that can happen when a pointer argument is an ADDR_EXPR.
1192 Recurse here manually to allow that. */
1193 tree ptr = TREE_OPERAND (*tp, 0);
1194 tree type = remap_type (TREE_TYPE (*tp), id);
1195 tree old = *tp;
1196 walk_tree (&ptr, copy_tree_body_r, data, NULL);
1197 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
1198 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1199 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1200 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
1201 /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
1202 remapped a parameter as the property might be valid only
1203 for the parameter itself. */
1204 if (TREE_THIS_NOTRAP (old)
1205 && (!is_parm (TREE_OPERAND (old, 0))
1206 || (!id->transform_parameter && is_parm (ptr))))
1207 TREE_THIS_NOTRAP (*tp) = 1;
1208 *walk_subtrees = 0;
1209 return NULL;
1212 /* Here is the "usual case". Copy this tree node, and then
1213 tweak some special cases. */
1214 copy_tree_r (tp, walk_subtrees, NULL);
1216 /* If EXPR has block defined, map it to newly constructed block.
1217 When inlining we want EXPRs without block appear in the block
1218 of function call if we are not remapping a type. */
1219 if (EXPR_P (*tp))
1221 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1222 if (TREE_BLOCK (*tp))
1224 tree *n;
1225 n = id->decl_map->get (TREE_BLOCK (*tp));
1226 if (n)
1227 new_block = *n;
1229 TREE_SET_BLOCK (*tp, new_block);
1232 if (TREE_CODE (*tp) != OMP_CLAUSE)
1233 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1235 /* The copied TARGET_EXPR has never been expanded, even if the
1236 original node was expanded already. */
1237 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1239 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1240 TREE_OPERAND (*tp, 3) = NULL_TREE;
1243 /* Variable substitution need not be simple. In particular, the
1244 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1245 and friends are up-to-date. */
1246 else if (TREE_CODE (*tp) == ADDR_EXPR)
1248 int invariant = is_gimple_min_invariant (*tp);
1249 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1251 /* Handle the case where we substituted an INDIRECT_REF
1252 into the operand of the ADDR_EXPR. */
1253 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1254 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1255 else
1256 recompute_tree_invariant_for_addr_expr (*tp);
1258 /* If this used to be invariant, but is not any longer,
1259 then regimplification is probably needed. */
1260 if (invariant && !is_gimple_min_invariant (*tp))
1261 id->regimplify = true;
1263 *walk_subtrees = 0;
1267 /* Keep iterating. */
1268 return NULL_TREE;
1271 /* Helper for remap_gimple_stmt. Given an EH region number for the
1272 source function, map that to the duplicate EH region number in
1273 the destination function. */
1275 static int
1276 remap_eh_region_nr (int old_nr, copy_body_data *id)
1278 eh_region old_r, new_r;
1280 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1281 new_r = static_cast<eh_region> (*id->eh_map->get (old_r));
1283 return new_r->index;
1286 /* Similar, but operate on INTEGER_CSTs. */
1288 static tree
1289 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1291 int old_nr, new_nr;
1293 old_nr = tree_to_shwi (old_t_nr);
1294 new_nr = remap_eh_region_nr (old_nr, id);
1296 return build_int_cst (integer_type_node, new_nr);
1299 /* Helper for copy_bb. Remap statement STMT using the inlining
1300 information in ID. Return the new statement copy. */
1302 static gimple_seq
1303 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1305 gimple copy = NULL;
1306 struct walk_stmt_info wi;
1307 bool skip_first = false;
1308 gimple_seq stmts = NULL;
1310 /* Begin by recognizing trees that we'll completely rewrite for the
1311 inlining context. Our output for these trees is completely
1312 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1313 into an edge). Further down, we'll handle trees that get
1314 duplicated and/or tweaked. */
1316 /* When requested, GIMPLE_RETURNs should be transformed to just the
1317 contained GIMPLE_ASSIGN. The branch semantics of the return will
1318 be handled elsewhere by manipulating the CFG rather than the
1319 statement. */
1320 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1322 tree retval = gimple_return_retval (stmt);
1323 tree retbnd = gimple_return_retbnd (stmt);
1324 tree bndslot = id->retbnd;
1326 if (retbnd && bndslot)
1328 gimple bndcopy = gimple_build_assign (bndslot, retbnd);
1329 memset (&wi, 0, sizeof (wi));
1330 wi.info = id;
1331 walk_gimple_op (bndcopy, remap_gimple_op_r, &wi);
1332 gimple_seq_add_stmt (&stmts, bndcopy);
1335 /* If we're returning something, just turn that into an
1336 assignment into the equivalent of the original RESULT_DECL.
1337 If RETVAL is just the result decl, the result decl has
1338 already been set (e.g. a recent "foo (&result_decl, ...)");
1339 just toss the entire GIMPLE_RETURN. */
1340 if (retval
1341 && (TREE_CODE (retval) != RESULT_DECL
1342 && (TREE_CODE (retval) != SSA_NAME
1343 || ! SSA_NAME_VAR (retval)
1344 || TREE_CODE (SSA_NAME_VAR (retval)) != RESULT_DECL)))
1346 copy = gimple_build_assign (id->do_not_unshare
1347 ? id->retvar : unshare_expr (id->retvar),
1348 retval);
1349 /* id->retvar is already substituted. Skip it on later remapping. */
1350 skip_first = true;
1352 /* We need to copy bounds if return structure with pointers into
1353 instrumented function. */
1354 if (chkp_function_instrumented_p (id->dst_fn)
1355 && !bndslot
1356 && !BOUNDED_P (id->retvar)
1357 && chkp_type_has_pointer (TREE_TYPE (id->retvar)))
1358 id->assign_stmts.safe_push (copy);
1361 else
1362 return stmts;
1364 else if (gimple_has_substatements (stmt))
1366 gimple_seq s1, s2;
1368 /* When cloning bodies from the C++ front end, we will be handed bodies
1369 in High GIMPLE form. Handle here all the High GIMPLE statements that
1370 have embedded statements. */
1371 switch (gimple_code (stmt))
1373 case GIMPLE_BIND:
1374 copy = copy_gimple_bind (stmt, id);
1375 break;
1377 case GIMPLE_CATCH:
1378 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1379 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1380 break;
1382 case GIMPLE_EH_FILTER:
1383 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1384 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1385 break;
1387 case GIMPLE_TRY:
1388 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1389 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1390 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1391 break;
1393 case GIMPLE_WITH_CLEANUP_EXPR:
1394 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1395 copy = gimple_build_wce (s1);
1396 break;
1398 case GIMPLE_OACC_KERNELS:
1399 case GIMPLE_OACC_PARALLEL:
1400 gcc_unreachable ();
1402 case GIMPLE_OMP_PARALLEL:
1403 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1404 copy = gimple_build_omp_parallel
1405 (s1,
1406 gimple_omp_parallel_clauses (stmt),
1407 gimple_omp_parallel_child_fn (stmt),
1408 gimple_omp_parallel_data_arg (stmt));
1409 break;
1411 case GIMPLE_OMP_TASK:
1412 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1413 copy = gimple_build_omp_task
1414 (s1,
1415 gimple_omp_task_clauses (stmt),
1416 gimple_omp_task_child_fn (stmt),
1417 gimple_omp_task_data_arg (stmt),
1418 gimple_omp_task_copy_fn (stmt),
1419 gimple_omp_task_arg_size (stmt),
1420 gimple_omp_task_arg_align (stmt));
1421 break;
1423 case GIMPLE_OMP_FOR:
1424 gcc_assert (!is_gimple_omp_oacc_specifically (stmt));
1425 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1426 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1427 copy = gimple_build_omp_for (s1, gimple_omp_for_kind (stmt),
1428 gimple_omp_for_clauses (stmt),
1429 gimple_omp_for_collapse (stmt), s2);
1431 size_t i;
1432 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1434 gimple_omp_for_set_index (copy, i,
1435 gimple_omp_for_index (stmt, i));
1436 gimple_omp_for_set_initial (copy, i,
1437 gimple_omp_for_initial (stmt, i));
1438 gimple_omp_for_set_final (copy, i,
1439 gimple_omp_for_final (stmt, i));
1440 gimple_omp_for_set_incr (copy, i,
1441 gimple_omp_for_incr (stmt, i));
1442 gimple_omp_for_set_cond (copy, i,
1443 gimple_omp_for_cond (stmt, i));
1446 break;
1448 case GIMPLE_OMP_MASTER:
1449 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1450 copy = gimple_build_omp_master (s1);
1451 break;
1453 case GIMPLE_OMP_TASKGROUP:
1454 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1455 copy = gimple_build_omp_taskgroup (s1);
1456 break;
1458 case GIMPLE_OMP_ORDERED:
1459 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1460 copy = gimple_build_omp_ordered (s1);
1461 break;
1463 case GIMPLE_OMP_SECTION:
1464 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1465 copy = gimple_build_omp_section (s1);
1466 break;
1468 case GIMPLE_OMP_SECTIONS:
1469 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1470 copy = gimple_build_omp_sections
1471 (s1, gimple_omp_sections_clauses (stmt));
1472 break;
1474 case GIMPLE_OMP_SINGLE:
1475 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1476 copy = gimple_build_omp_single
1477 (s1, gimple_omp_single_clauses (stmt));
1478 break;
1480 case GIMPLE_OMP_TARGET:
1481 gcc_assert (!is_gimple_omp_oacc_specifically (stmt));
1482 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1483 copy = gimple_build_omp_target
1484 (s1, gimple_omp_target_kind (stmt),
1485 gimple_omp_target_clauses (stmt));
1486 break;
1488 case GIMPLE_OMP_TEAMS:
1489 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1490 copy = gimple_build_omp_teams
1491 (s1, gimple_omp_teams_clauses (stmt));
1492 break;
1494 case GIMPLE_OMP_CRITICAL:
1495 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1496 copy
1497 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1498 break;
1500 case GIMPLE_TRANSACTION:
1501 s1 = remap_gimple_seq (gimple_transaction_body (stmt), id);
1502 copy = gimple_build_transaction (s1, gimple_transaction_label (stmt));
1503 gimple_transaction_set_subcode (copy, gimple_transaction_subcode (stmt));
1504 break;
1506 default:
1507 gcc_unreachable ();
1510 else
1512 if (gimple_assign_copy_p (stmt)
1513 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1514 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1516 /* Here we handle statements that are not completely rewritten.
1517 First we detect some inlining-induced bogosities for
1518 discarding. */
1520 /* Some assignments VAR = VAR; don't generate any rtl code
1521 and thus don't count as variable modification. Avoid
1522 keeping bogosities like 0 = 0. */
1523 tree decl = gimple_assign_lhs (stmt), value;
1524 tree *n;
1526 n = id->decl_map->get (decl);
1527 if (n)
1529 value = *n;
1530 STRIP_TYPE_NOPS (value);
1531 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1532 return NULL;
1536 /* For *ptr_N ={v} {CLOBBER}, if ptr_N is SSA_NAME defined
1537 in a block that we aren't copying during tree_function_versioning,
1538 just drop the clobber stmt. */
1539 if (id->blocks_to_copy && gimple_clobber_p (stmt))
1541 tree lhs = gimple_assign_lhs (stmt);
1542 if (TREE_CODE (lhs) == MEM_REF
1543 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
1545 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0));
1546 if (gimple_bb (def_stmt)
1547 && !bitmap_bit_p (id->blocks_to_copy,
1548 gimple_bb (def_stmt)->index))
1549 return NULL;
1553 if (gimple_debug_bind_p (stmt))
1555 copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1556 gimple_debug_bind_get_value (stmt),
1557 stmt);
1558 id->debug_stmts.safe_push (copy);
1559 gimple_seq_add_stmt (&stmts, copy);
1560 return stmts;
1562 if (gimple_debug_source_bind_p (stmt))
1564 copy = gimple_build_debug_source_bind
1565 (gimple_debug_source_bind_get_var (stmt),
1566 gimple_debug_source_bind_get_value (stmt), stmt);
1567 id->debug_stmts.safe_push (copy);
1568 gimple_seq_add_stmt (&stmts, copy);
1569 return stmts;
1572 /* Create a new deep copy of the statement. */
1573 copy = gimple_copy (stmt);
1575 /* Clear flags that need revisiting. */
1576 if (is_gimple_call (copy)
1577 && gimple_call_tail_p (copy))
1578 gimple_call_set_tail (copy, false);
1580 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1581 RESX and EH_DISPATCH. */
1582 if (id->eh_map)
1583 switch (gimple_code (copy))
1585 case GIMPLE_CALL:
1587 tree r, fndecl = gimple_call_fndecl (copy);
1588 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1589 switch (DECL_FUNCTION_CODE (fndecl))
1591 case BUILT_IN_EH_COPY_VALUES:
1592 r = gimple_call_arg (copy, 1);
1593 r = remap_eh_region_tree_nr (r, id);
1594 gimple_call_set_arg (copy, 1, r);
1595 /* FALLTHRU */
1597 case BUILT_IN_EH_POINTER:
1598 case BUILT_IN_EH_FILTER:
1599 r = gimple_call_arg (copy, 0);
1600 r = remap_eh_region_tree_nr (r, id);
1601 gimple_call_set_arg (copy, 0, r);
1602 break;
1604 default:
1605 break;
1608 /* Reset alias info if we didn't apply measures to
1609 keep it valid over inlining by setting DECL_PT_UID. */
1610 if (!id->src_cfun->gimple_df
1611 || !id->src_cfun->gimple_df->ipa_pta)
1612 gimple_call_reset_alias_info (copy);
1614 break;
1616 case GIMPLE_RESX:
1618 int r = gimple_resx_region (copy);
1619 r = remap_eh_region_nr (r, id);
1620 gimple_resx_set_region (copy, r);
1622 break;
1624 case GIMPLE_EH_DISPATCH:
1626 int r = gimple_eh_dispatch_region (copy);
1627 r = remap_eh_region_nr (r, id);
1628 gimple_eh_dispatch_set_region (copy, r);
1630 break;
1632 default:
1633 break;
1637 /* If STMT has a block defined, map it to the newly constructed
1638 block. */
1639 if (gimple_block (copy))
1641 tree *n;
1642 n = id->decl_map->get (gimple_block (copy));
1643 gcc_assert (n);
1644 gimple_set_block (copy, *n);
1647 if (gimple_debug_bind_p (copy) || gimple_debug_source_bind_p (copy))
1649 gimple_seq_add_stmt (&stmts, copy);
1650 return stmts;
1653 /* Remap all the operands in COPY. */
1654 memset (&wi, 0, sizeof (wi));
1655 wi.info = id;
1656 if (skip_first)
1657 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1658 else
1659 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1661 /* Clear the copied virtual operands. We are not remapping them here
1662 but are going to recreate them from scratch. */
1663 if (gimple_has_mem_ops (copy))
1665 gimple_set_vdef (copy, NULL_TREE);
1666 gimple_set_vuse (copy, NULL_TREE);
1669 gimple_seq_add_stmt (&stmts, copy);
1670 return stmts;
1674 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1675 later */
1677 static basic_block
1678 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1679 gcov_type count_scale)
1681 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1682 basic_block copy_basic_block;
1683 tree decl;
1684 gcov_type freq;
1685 basic_block prev;
1687 /* Search for previous copied basic block. */
1688 prev = bb->prev_bb;
1689 while (!prev->aux)
1690 prev = prev->prev_bb;
1692 /* create_basic_block() will append every new block to
1693 basic_block_info automatically. */
1694 copy_basic_block = create_basic_block (NULL, (void *) 0,
1695 (basic_block) prev->aux);
1696 copy_basic_block->count = apply_scale (bb->count, count_scale);
1698 /* We are going to rebuild frequencies from scratch. These values
1699 have just small importance to drive canonicalize_loop_headers. */
1700 freq = apply_scale ((gcov_type)bb->frequency, frequency_scale);
1702 /* We recompute frequencies after inlining, so this is quite safe. */
1703 if (freq > BB_FREQ_MAX)
1704 freq = BB_FREQ_MAX;
1705 copy_basic_block->frequency = freq;
1707 copy_gsi = gsi_start_bb (copy_basic_block);
1709 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1711 gimple_seq stmts;
1712 gimple stmt = gsi_stmt (gsi);
1713 gimple orig_stmt = stmt;
1714 gimple_stmt_iterator stmts_gsi;
1715 bool stmt_added = false;
1717 id->regimplify = false;
1718 stmts = remap_gimple_stmt (stmt, id);
1720 if (gimple_seq_empty_p (stmts))
1721 continue;
1723 seq_gsi = copy_gsi;
1725 for (stmts_gsi = gsi_start (stmts);
1726 !gsi_end_p (stmts_gsi); )
1728 stmt = gsi_stmt (stmts_gsi);
1730 /* Advance iterator now before stmt is moved to seq_gsi. */
1731 gsi_next (&stmts_gsi);
1733 if (gimple_nop_p (stmt))
1734 continue;
1736 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun,
1737 orig_stmt);
1739 /* With return slot optimization we can end up with
1740 non-gimple (foo *)&this->m, fix that here. */
1741 if (is_gimple_assign (stmt)
1742 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
1743 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1745 tree new_rhs;
1746 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1747 gimple_assign_rhs1 (stmt),
1748 true, NULL, false,
1749 GSI_CONTINUE_LINKING);
1750 gimple_assign_set_rhs1 (stmt, new_rhs);
1751 id->regimplify = false;
1754 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1756 if (id->regimplify)
1757 gimple_regimplify_operands (stmt, &seq_gsi);
1759 stmt_added = true;
1762 if (!stmt_added)
1763 continue;
1765 /* If copy_basic_block has been empty at the start of this iteration,
1766 call gsi_start_bb again to get at the newly added statements. */
1767 if (gsi_end_p (copy_gsi))
1768 copy_gsi = gsi_start_bb (copy_basic_block);
1769 else
1770 gsi_next (&copy_gsi);
1772 /* Process the new statement. The call to gimple_regimplify_operands
1773 possibly turned the statement into multiple statements, we
1774 need to process all of them. */
1777 tree fn;
1779 stmt = gsi_stmt (copy_gsi);
1780 if (is_gimple_call (stmt)
1781 && gimple_call_va_arg_pack_p (stmt)
1782 && id->gimple_call)
1784 /* __builtin_va_arg_pack () should be replaced by
1785 all arguments corresponding to ... in the caller. */
1786 tree p;
1787 gimple new_call;
1788 vec<tree> argarray;
1789 size_t nargs = gimple_call_num_args (id->gimple_call);
1790 size_t n, i, nargs_to_copy;
1791 bool remove_bounds = false;
1793 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1794 nargs--;
1796 /* Bounds should be removed from arg pack in case
1797 we handle not instrumented call in instrumented
1798 function. */
1799 nargs_to_copy = nargs;
1800 if (gimple_call_with_bounds_p (id->gimple_call)
1801 && !gimple_call_with_bounds_p (stmt))
1803 for (i = gimple_call_num_args (id->gimple_call) - nargs;
1804 i < gimple_call_num_args (id->gimple_call);
1805 i++)
1806 if (POINTER_BOUNDS_P (gimple_call_arg (id->gimple_call, i)))
1807 nargs_to_copy--;
1808 remove_bounds = true;
1811 /* Create the new array of arguments. */
1812 n = nargs_to_copy + gimple_call_num_args (stmt);
1813 argarray.create (n);
1814 argarray.safe_grow_cleared (n);
1816 /* Copy all the arguments before '...' */
1817 memcpy (argarray.address (),
1818 gimple_call_arg_ptr (stmt, 0),
1819 gimple_call_num_args (stmt) * sizeof (tree));
1821 if (remove_bounds)
1823 /* Append the rest of arguments removing bounds. */
1824 unsigned cur = gimple_call_num_args (stmt);
1825 i = gimple_call_num_args (id->gimple_call) - nargs;
1826 for (i = gimple_call_num_args (id->gimple_call) - nargs;
1827 i < gimple_call_num_args (id->gimple_call);
1828 i++)
1829 if (!POINTER_BOUNDS_P (gimple_call_arg (id->gimple_call, i)))
1830 argarray[cur++] = gimple_call_arg (id->gimple_call, i);
1831 gcc_assert (cur == n);
1833 else
1835 /* Append the arguments passed in '...' */
1836 memcpy (argarray.address () + gimple_call_num_args (stmt),
1837 gimple_call_arg_ptr (id->gimple_call, 0)
1838 + (gimple_call_num_args (id->gimple_call) - nargs),
1839 nargs * sizeof (tree));
1842 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1843 argarray);
1845 argarray.release ();
1847 /* Copy all GIMPLE_CALL flags, location and block, except
1848 GF_CALL_VA_ARG_PACK. */
1849 gimple_call_copy_flags (new_call, stmt);
1850 gimple_call_set_va_arg_pack (new_call, false);
1851 gimple_set_location (new_call, gimple_location (stmt));
1852 gimple_set_block (new_call, gimple_block (stmt));
1853 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1855 gsi_replace (&copy_gsi, new_call, false);
1856 stmt = new_call;
1858 else if (is_gimple_call (stmt)
1859 && id->gimple_call
1860 && (decl = gimple_call_fndecl (stmt))
1861 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1862 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1864 /* __builtin_va_arg_pack_len () should be replaced by
1865 the number of anonymous arguments. */
1866 size_t nargs = gimple_call_num_args (id->gimple_call), i;
1867 tree count, p;
1868 gimple new_stmt;
1870 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1871 nargs--;
1873 /* For instrumented calls we should ignore bounds. */
1874 for (i = gimple_call_num_args (id->gimple_call) - nargs;
1875 i < gimple_call_num_args (id->gimple_call);
1876 i++)
1877 if (POINTER_BOUNDS_P (gimple_call_arg (id->gimple_call, i)))
1878 nargs--;
1880 count = build_int_cst (integer_type_node, nargs);
1881 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1882 gsi_replace (&copy_gsi, new_stmt, false);
1883 stmt = new_stmt;
1886 /* Statements produced by inlining can be unfolded, especially
1887 when we constant propagated some operands. We can't fold
1888 them right now for two reasons:
1889 1) folding require SSA_NAME_DEF_STMTs to be correct
1890 2) we can't change function calls to builtins.
1891 So we just mark statement for later folding. We mark
1892 all new statements, instead just statements that has changed
1893 by some nontrivial substitution so even statements made
1894 foldable indirectly are updated. If this turns out to be
1895 expensive, copy_body can be told to watch for nontrivial
1896 changes. */
1897 if (id->statements_to_fold)
1898 id->statements_to_fold->add (stmt);
1900 /* We're duplicating a CALL_EXPR. Find any corresponding
1901 callgraph edges and update or duplicate them. */
1902 if (is_gimple_call (stmt))
1904 struct cgraph_edge *edge;
1906 switch (id->transform_call_graph_edges)
1908 case CB_CGE_DUPLICATE:
1909 edge = id->src_node->get_edge (orig_stmt);
1910 if (edge)
1912 int edge_freq = edge->frequency;
1913 int new_freq;
1914 struct cgraph_edge *old_edge = edge;
1915 edge = edge->clone (id->dst_node, stmt,
1916 gimple_uid (stmt),
1917 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1918 true);
1919 /* We could also just rescale the frequency, but
1920 doing so would introduce roundoff errors and make
1921 verifier unhappy. */
1922 new_freq = compute_call_stmt_bb_frequency (id->dst_node->decl,
1923 copy_basic_block);
1925 /* Speculative calls consist of two edges - direct and indirect.
1926 Duplicate the whole thing and distribute frequencies accordingly. */
1927 if (edge->speculative)
1929 struct cgraph_edge *direct, *indirect;
1930 struct ipa_ref *ref;
1932 gcc_assert (!edge->indirect_unknown_callee);
1933 old_edge->speculative_call_info (direct, indirect, ref);
1934 indirect = indirect->clone (id->dst_node, stmt,
1935 gimple_uid (stmt),
1936 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1937 true);
1938 if (old_edge->frequency + indirect->frequency)
1940 edge->frequency = MIN (RDIV ((gcov_type)new_freq * old_edge->frequency,
1941 (old_edge->frequency + indirect->frequency)),
1942 CGRAPH_FREQ_MAX);
1943 indirect->frequency = MIN (RDIV ((gcov_type)new_freq * indirect->frequency,
1944 (old_edge->frequency + indirect->frequency)),
1945 CGRAPH_FREQ_MAX);
1947 id->dst_node->clone_reference (ref, stmt);
1949 else
1951 edge->frequency = new_freq;
1952 if (dump_file
1953 && profile_status_for_fn (cfun) != PROFILE_ABSENT
1954 && (edge_freq > edge->frequency + 10
1955 || edge_freq < edge->frequency - 10))
1957 fprintf (dump_file, "Edge frequency estimated by "
1958 "cgraph %i diverge from inliner's estimate %i\n",
1959 edge_freq,
1960 edge->frequency);
1961 fprintf (dump_file,
1962 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
1963 bb->index,
1964 bb->frequency,
1965 copy_basic_block->frequency);
1969 break;
1971 case CB_CGE_MOVE_CLONES:
1972 id->dst_node->set_call_stmt_including_clones (orig_stmt,
1973 stmt);
1974 edge = id->dst_node->get_edge (stmt);
1975 break;
1977 case CB_CGE_MOVE:
1978 edge = id->dst_node->get_edge (orig_stmt);
1979 if (edge)
1980 edge->set_call_stmt (stmt);
1981 break;
1983 default:
1984 gcc_unreachable ();
1987 /* Constant propagation on argument done during inlining
1988 may create new direct call. Produce an edge for it. */
1989 if ((!edge
1990 || (edge->indirect_inlining_edge
1991 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1992 && id->dst_node->definition
1993 && (fn = gimple_call_fndecl (stmt)) != NULL)
1995 struct cgraph_node *dest = cgraph_node::get (fn);
1997 /* We have missing edge in the callgraph. This can happen
1998 when previous inlining turned an indirect call into a
1999 direct call by constant propagating arguments or we are
2000 producing dead clone (for further cloning). In all
2001 other cases we hit a bug (incorrect node sharing is the
2002 most common reason for missing edges). */
2003 gcc_assert (!dest->definition
2004 || dest->address_taken
2005 || !id->src_node->definition
2006 || !id->dst_node->definition);
2007 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
2008 id->dst_node->create_edge_including_clones
2009 (dest, orig_stmt, stmt, bb->count,
2010 compute_call_stmt_bb_frequency (id->dst_node->decl,
2011 copy_basic_block),
2012 CIF_ORIGINALLY_INDIRECT_CALL);
2013 else
2014 id->dst_node->create_edge (dest, stmt,
2015 bb->count,
2016 compute_call_stmt_bb_frequency
2017 (id->dst_node->decl,
2018 copy_basic_block))->inline_failed
2019 = CIF_ORIGINALLY_INDIRECT_CALL;
2020 if (dump_file)
2022 fprintf (dump_file, "Created new direct edge to %s\n",
2023 dest->name ());
2027 notice_special_calls (stmt);
2030 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
2031 id->eh_map, id->eh_lp_nr);
2033 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
2035 ssa_op_iter i;
2036 tree def;
2038 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
2039 if (TREE_CODE (def) == SSA_NAME)
2040 SSA_NAME_DEF_STMT (def) = stmt;
2043 gsi_next (&copy_gsi);
2045 while (!gsi_end_p (copy_gsi));
2047 copy_gsi = gsi_last_bb (copy_basic_block);
2050 return copy_basic_block;
2053 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
2054 form is quite easy, since dominator relationship for old basic blocks does
2055 not change.
2057 There is however exception where inlining might change dominator relation
2058 across EH edges from basic block within inlined functions destinating
2059 to landing pads in function we inline into.
2061 The function fills in PHI_RESULTs of such PHI nodes if they refer
2062 to gimple regs. Otherwise, the function mark PHI_RESULT of such
2063 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
2064 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
2065 set, and this means that there will be no overlapping live ranges
2066 for the underlying symbol.
2068 This might change in future if we allow redirecting of EH edges and
2069 we might want to change way build CFG pre-inlining to include
2070 all the possible edges then. */
2071 static void
2072 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
2073 bool can_throw, bool nonlocal_goto)
2075 edge e;
2076 edge_iterator ei;
2078 FOR_EACH_EDGE (e, ei, bb->succs)
2079 if (!e->dest->aux
2080 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
2082 gimple phi;
2083 gimple_stmt_iterator si;
2085 if (!nonlocal_goto)
2086 gcc_assert (e->flags & EDGE_EH);
2088 if (!can_throw)
2089 gcc_assert (!(e->flags & EDGE_EH));
2091 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
2093 edge re;
2095 phi = gsi_stmt (si);
2097 /* For abnormal goto/call edges the receiver can be the
2098 ENTRY_BLOCK. Do not assert this cannot happen. */
2100 gcc_assert ((e->flags & EDGE_EH)
2101 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
2103 re = find_edge (ret_bb, e->dest);
2104 gcc_checking_assert (re);
2105 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
2106 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
2108 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
2109 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
2115 /* Copy edges from BB into its copy constructed earlier, scale profile
2116 accordingly. Edges will be taken care of later. Assume aux
2117 pointers to point to the copies of each BB. Return true if any
2118 debug stmts are left after a statement that must end the basic block. */
2120 static bool
2121 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb,
2122 basic_block abnormal_goto_dest)
2124 basic_block new_bb = (basic_block) bb->aux;
2125 edge_iterator ei;
2126 edge old_edge;
2127 gimple_stmt_iterator si;
2128 int flags;
2129 bool need_debug_cleanup = false;
2131 /* Use the indices from the original blocks to create edges for the
2132 new ones. */
2133 FOR_EACH_EDGE (old_edge, ei, bb->succs)
2134 if (!(old_edge->flags & EDGE_EH))
2136 edge new_edge;
2138 flags = old_edge->flags;
2140 /* Return edges do get a FALLTHRU flag when the get inlined. */
2141 if (old_edge->dest->index == EXIT_BLOCK
2142 && !(old_edge->flags & (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE|EDGE_FAKE))
2143 && old_edge->dest->aux != EXIT_BLOCK_PTR_FOR_FN (cfun))
2144 flags |= EDGE_FALLTHRU;
2145 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
2146 new_edge->count = apply_scale (old_edge->count, count_scale);
2147 new_edge->probability = old_edge->probability;
2150 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
2151 return false;
2153 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
2155 gimple copy_stmt;
2156 bool can_throw, nonlocal_goto;
2158 copy_stmt = gsi_stmt (si);
2159 if (!is_gimple_debug (copy_stmt))
2160 update_stmt (copy_stmt);
2162 /* Do this before the possible split_block. */
2163 gsi_next (&si);
2165 /* If this tree could throw an exception, there are two
2166 cases where we need to add abnormal edge(s): the
2167 tree wasn't in a region and there is a "current
2168 region" in the caller; or the original tree had
2169 EH edges. In both cases split the block after the tree,
2170 and add abnormal edge(s) as needed; we need both
2171 those from the callee and the caller.
2172 We check whether the copy can throw, because the const
2173 propagation can change an INDIRECT_REF which throws
2174 into a COMPONENT_REF which doesn't. If the copy
2175 can throw, the original could also throw. */
2176 can_throw = stmt_can_throw_internal (copy_stmt);
2177 nonlocal_goto
2178 = (stmt_can_make_abnormal_goto (copy_stmt)
2179 && !computed_goto_p (copy_stmt));
2181 if (can_throw || nonlocal_goto)
2183 if (!gsi_end_p (si))
2185 while (!gsi_end_p (si) && is_gimple_debug (gsi_stmt (si)))
2186 gsi_next (&si);
2187 if (gsi_end_p (si))
2188 need_debug_cleanup = true;
2190 if (!gsi_end_p (si))
2191 /* Note that bb's predecessor edges aren't necessarily
2192 right at this point; split_block doesn't care. */
2194 edge e = split_block (new_bb, copy_stmt);
2196 new_bb = e->dest;
2197 new_bb->aux = e->src->aux;
2198 si = gsi_start_bb (new_bb);
2202 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
2203 make_eh_dispatch_edges (copy_stmt);
2204 else if (can_throw)
2205 make_eh_edges (copy_stmt);
2207 /* If the call we inline cannot make abnormal goto do not add
2208 additional abnormal edges but only retain those already present
2209 in the original function body. */
2210 if (abnormal_goto_dest == NULL)
2211 nonlocal_goto = false;
2212 if (nonlocal_goto)
2214 basic_block copy_stmt_bb = gimple_bb (copy_stmt);
2216 if (get_abnormal_succ_dispatcher (copy_stmt_bb))
2217 nonlocal_goto = false;
2218 /* ABNORMAL_DISPATCHER (1) is for longjmp/setjmp or nonlocal gotos
2219 in OpenMP regions which aren't allowed to be left abnormally.
2220 So, no need to add abnormal edge in that case. */
2221 else if (is_gimple_call (copy_stmt)
2222 && gimple_call_internal_p (copy_stmt)
2223 && (gimple_call_internal_fn (copy_stmt)
2224 == IFN_ABNORMAL_DISPATCHER)
2225 && gimple_call_arg (copy_stmt, 0) == boolean_true_node)
2226 nonlocal_goto = false;
2227 else
2228 make_edge (copy_stmt_bb, abnormal_goto_dest, EDGE_ABNORMAL);
2231 if ((can_throw || nonlocal_goto)
2232 && gimple_in_ssa_p (cfun))
2233 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
2234 can_throw, nonlocal_goto);
2236 return need_debug_cleanup;
2239 /* Copy the PHIs. All blocks and edges are copied, some blocks
2240 was possibly split and new outgoing EH edges inserted.
2241 BB points to the block of original function and AUX pointers links
2242 the original and newly copied blocks. */
2244 static void
2245 copy_phis_for_bb (basic_block bb, copy_body_data *id)
2247 basic_block const new_bb = (basic_block) bb->aux;
2248 edge_iterator ei;
2249 gimple phi;
2250 gimple_stmt_iterator si;
2251 edge new_edge;
2252 bool inserted = false;
2254 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
2256 tree res, new_res;
2257 gimple new_phi;
2259 phi = gsi_stmt (si);
2260 res = PHI_RESULT (phi);
2261 new_res = res;
2262 if (!virtual_operand_p (res))
2264 walk_tree (&new_res, copy_tree_body_r, id, NULL);
2265 new_phi = create_phi_node (new_res, new_bb);
2266 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2268 edge old_edge = find_edge ((basic_block) new_edge->src->aux, bb);
2269 tree arg;
2270 tree new_arg;
2271 edge_iterator ei2;
2272 location_t locus;
2274 /* When doing partial cloning, we allow PHIs on the entry block
2275 as long as all the arguments are the same. Find any input
2276 edge to see argument to copy. */
2277 if (!old_edge)
2278 FOR_EACH_EDGE (old_edge, ei2, bb->preds)
2279 if (!old_edge->src->aux)
2280 break;
2282 arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
2283 new_arg = arg;
2284 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
2285 gcc_assert (new_arg);
2286 /* With return slot optimization we can end up with
2287 non-gimple (foo *)&this->m, fix that here. */
2288 if (TREE_CODE (new_arg) != SSA_NAME
2289 && TREE_CODE (new_arg) != FUNCTION_DECL
2290 && !is_gimple_val (new_arg))
2292 gimple_seq stmts = NULL;
2293 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
2294 gsi_insert_seq_on_edge (new_edge, stmts);
2295 inserted = true;
2297 locus = gimple_phi_arg_location_from_edge (phi, old_edge);
2298 if (LOCATION_BLOCK (locus))
2300 tree *n;
2301 n = id->decl_map->get (LOCATION_BLOCK (locus));
2302 gcc_assert (n);
2303 if (*n)
2304 locus = COMBINE_LOCATION_DATA (line_table, locus, *n);
2305 else
2306 locus = LOCATION_LOCUS (locus);
2308 else
2309 locus = LOCATION_LOCUS (locus);
2311 add_phi_arg (new_phi, new_arg, new_edge, locus);
2316 /* Commit the delayed edge insertions. */
2317 if (inserted)
2318 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2319 gsi_commit_one_edge_insert (new_edge, NULL);
2323 /* Wrapper for remap_decl so it can be used as a callback. */
2325 static tree
2326 remap_decl_1 (tree decl, void *data)
2328 return remap_decl (decl, (copy_body_data *) data);
2331 /* Build struct function and associated datastructures for the new clone
2332 NEW_FNDECL to be build. CALLEE_FNDECL is the original. Function changes
2333 the cfun to the function of new_fndecl (and current_function_decl too). */
2335 static void
2336 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
2338 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2339 gcov_type count_scale;
2341 if (!DECL_ARGUMENTS (new_fndecl))
2342 DECL_ARGUMENTS (new_fndecl) = DECL_ARGUMENTS (callee_fndecl);
2343 if (!DECL_RESULT (new_fndecl))
2344 DECL_RESULT (new_fndecl) = DECL_RESULT (callee_fndecl);
2346 if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
2347 count_scale
2348 = GCOV_COMPUTE_SCALE (count,
2349 ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
2350 else
2351 count_scale = REG_BR_PROB_BASE;
2353 /* Register specific tree functions. */
2354 gimple_register_cfg_hooks ();
2356 /* Get clean struct function. */
2357 push_struct_function (new_fndecl);
2359 /* We will rebuild these, so just sanity check that they are empty. */
2360 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
2361 gcc_assert (cfun->local_decls == NULL);
2362 gcc_assert (cfun->cfg == NULL);
2363 gcc_assert (cfun->decl == new_fndecl);
2365 /* Copy items we preserve during cloning. */
2366 cfun->static_chain_decl = src_cfun->static_chain_decl;
2367 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
2368 cfun->function_end_locus = src_cfun->function_end_locus;
2369 cfun->curr_properties = src_cfun->curr_properties;
2370 cfun->last_verified = src_cfun->last_verified;
2371 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2372 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
2373 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2374 cfun->stdarg = src_cfun->stdarg;
2375 cfun->after_inlining = src_cfun->after_inlining;
2376 cfun->can_throw_non_call_exceptions
2377 = src_cfun->can_throw_non_call_exceptions;
2378 cfun->can_delete_dead_exceptions = src_cfun->can_delete_dead_exceptions;
2379 cfun->returns_struct = src_cfun->returns_struct;
2380 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
2382 init_empty_tree_cfg ();
2384 profile_status_for_fn (cfun) = profile_status_for_fn (src_cfun);
2385 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
2386 (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
2387 REG_BR_PROB_BASE);
2388 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
2389 = ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
2390 EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
2391 (EXIT_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
2392 REG_BR_PROB_BASE);
2393 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency =
2394 EXIT_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
2395 if (src_cfun->eh)
2396 init_eh_for_function ();
2398 if (src_cfun->gimple_df)
2400 init_tree_ssa (cfun);
2401 cfun->gimple_df->in_ssa_p = true;
2402 init_ssa_operands (cfun);
2406 /* Helper function for copy_cfg_body. Move debug stmts from the end
2407 of NEW_BB to the beginning of successor basic blocks when needed. If the
2408 successor has multiple predecessors, reset them, otherwise keep
2409 their value. */
2411 static void
2412 maybe_move_debug_stmts_to_successors (copy_body_data *id, basic_block new_bb)
2414 edge e;
2415 edge_iterator ei;
2416 gimple_stmt_iterator si = gsi_last_nondebug_bb (new_bb);
2418 if (gsi_end_p (si)
2419 || gsi_one_before_end_p (si)
2420 || !(stmt_can_throw_internal (gsi_stmt (si))
2421 || stmt_can_make_abnormal_goto (gsi_stmt (si))))
2422 return;
2424 FOR_EACH_EDGE (e, ei, new_bb->succs)
2426 gimple_stmt_iterator ssi = gsi_last_bb (new_bb);
2427 gimple_stmt_iterator dsi = gsi_after_labels (e->dest);
2428 while (is_gimple_debug (gsi_stmt (ssi)))
2430 gimple stmt = gsi_stmt (ssi), new_stmt;
2431 tree var;
2432 tree value;
2434 /* For the last edge move the debug stmts instead of copying
2435 them. */
2436 if (ei_one_before_end_p (ei))
2438 si = ssi;
2439 gsi_prev (&ssi);
2440 if (!single_pred_p (e->dest) && gimple_debug_bind_p (stmt))
2441 gimple_debug_bind_reset_value (stmt);
2442 gsi_remove (&si, false);
2443 gsi_insert_before (&dsi, stmt, GSI_SAME_STMT);
2444 continue;
2447 if (gimple_debug_bind_p (stmt))
2449 var = gimple_debug_bind_get_var (stmt);
2450 if (single_pred_p (e->dest))
2452 value = gimple_debug_bind_get_value (stmt);
2453 value = unshare_expr (value);
2455 else
2456 value = NULL_TREE;
2457 new_stmt = gimple_build_debug_bind (var, value, stmt);
2459 else if (gimple_debug_source_bind_p (stmt))
2461 var = gimple_debug_source_bind_get_var (stmt);
2462 value = gimple_debug_source_bind_get_value (stmt);
2463 new_stmt = gimple_build_debug_source_bind (var, value, stmt);
2465 else
2466 gcc_unreachable ();
2467 gsi_insert_before (&dsi, new_stmt, GSI_SAME_STMT);
2468 id->debug_stmts.safe_push (new_stmt);
2469 gsi_prev (&ssi);
2474 /* Make a copy of the sub-loops of SRC_PARENT and place them
2475 as siblings of DEST_PARENT. */
2477 static void
2478 copy_loops (copy_body_data *id,
2479 struct loop *dest_parent, struct loop *src_parent)
2481 struct loop *src_loop = src_parent->inner;
2482 while (src_loop)
2484 if (!id->blocks_to_copy
2485 || bitmap_bit_p (id->blocks_to_copy, src_loop->header->index))
2487 struct loop *dest_loop = alloc_loop ();
2489 /* Assign the new loop its header and latch and associate
2490 those with the new loop. */
2491 dest_loop->header = (basic_block)src_loop->header->aux;
2492 dest_loop->header->loop_father = dest_loop;
2493 if (src_loop->latch != NULL)
2495 dest_loop->latch = (basic_block)src_loop->latch->aux;
2496 dest_loop->latch->loop_father = dest_loop;
2499 /* Copy loop meta-data. */
2500 copy_loop_info (src_loop, dest_loop);
2502 /* Finally place it into the loop array and the loop tree. */
2503 place_new_loop (cfun, dest_loop);
2504 flow_loop_tree_node_add (dest_parent, dest_loop);
2506 dest_loop->safelen = src_loop->safelen;
2507 dest_loop->dont_vectorize = src_loop->dont_vectorize;
2508 if (src_loop->force_vectorize)
2510 dest_loop->force_vectorize = true;
2511 cfun->has_force_vectorize_loops = true;
2513 if (src_loop->simduid)
2515 dest_loop->simduid = remap_decl (src_loop->simduid, id);
2516 cfun->has_simduid_loops = true;
2519 /* Recurse. */
2520 copy_loops (id, dest_loop, src_loop);
2522 src_loop = src_loop->next;
2526 /* Call cgraph_redirect_edge_call_stmt_to_callee on all calls in BB */
2528 void
2529 redirect_all_calls (copy_body_data * id, basic_block bb)
2531 gimple_stmt_iterator si;
2532 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2534 if (is_gimple_call (gsi_stmt (si)))
2536 struct cgraph_edge *edge = id->dst_node->get_edge (gsi_stmt (si));
2537 if (edge)
2538 edge->redirect_call_stmt_to_callee ();
2543 /* Convert estimated frequencies into counts for NODE, scaling COUNT
2544 with each bb's frequency. Used when NODE has a 0-weight entry
2545 but we are about to inline it into a non-zero count call bb.
2546 See the comments for handle_missing_profiles() in predict.c for
2547 when this can happen for COMDATs. */
2549 void
2550 freqs_to_counts (struct cgraph_node *node, gcov_type count)
2552 basic_block bb;
2553 edge_iterator ei;
2554 edge e;
2555 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2557 FOR_ALL_BB_FN(bb, fn)
2559 bb->count = apply_scale (count,
2560 GCOV_COMPUTE_SCALE (bb->frequency, BB_FREQ_MAX));
2561 FOR_EACH_EDGE (e, ei, bb->succs)
2562 e->count = apply_probability (e->src->count, e->probability);
2566 /* Make a copy of the body of FN so that it can be inserted inline in
2567 another function. Walks FN via CFG, returns new fndecl. */
2569 static tree
2570 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
2571 basic_block entry_block_map, basic_block exit_block_map,
2572 basic_block new_entry)
2574 tree callee_fndecl = id->src_fn;
2575 /* Original cfun for the callee, doesn't change. */
2576 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2577 struct function *cfun_to_copy;
2578 basic_block bb;
2579 tree new_fndecl = NULL;
2580 bool need_debug_cleanup = false;
2581 gcov_type count_scale;
2582 int last;
2583 int incoming_frequency = 0;
2584 gcov_type incoming_count = 0;
2586 /* This can happen for COMDAT routines that end up with 0 counts
2587 despite being called (see the comments for handle_missing_profiles()
2588 in predict.c as to why). Apply counts to the blocks in the callee
2589 before inlining, using the guessed edge frequencies, so that we don't
2590 end up with a 0-count inline body which can confuse downstream
2591 optimizations such as function splitting. */
2592 if (!ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count && count)
2594 /* Apply the larger of the call bb count and the total incoming
2595 call edge count to the callee. */
2596 gcov_type in_count = 0;
2597 struct cgraph_edge *in_edge;
2598 for (in_edge = id->src_node->callers; in_edge;
2599 in_edge = in_edge->next_caller)
2600 in_count += in_edge->count;
2601 freqs_to_counts (id->src_node, count > in_count ? count : in_count);
2604 if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
2605 count_scale
2606 = GCOV_COMPUTE_SCALE (count,
2607 ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
2608 else
2609 count_scale = REG_BR_PROB_BASE;
2611 /* Register specific tree functions. */
2612 gimple_register_cfg_hooks ();
2614 /* If we are inlining just region of the function, make sure to connect
2615 new entry to ENTRY_BLOCK_PTR_FOR_FN (cfun). Since new entry can be
2616 part of loop, we must compute frequency and probability of
2617 ENTRY_BLOCK_PTR_FOR_FN (cfun) based on the frequencies and
2618 probabilities of edges incoming from nonduplicated region. */
2619 if (new_entry)
2621 edge e;
2622 edge_iterator ei;
2624 FOR_EACH_EDGE (e, ei, new_entry->preds)
2625 if (!e->src->aux)
2627 incoming_frequency += EDGE_FREQUENCY (e);
2628 incoming_count += e->count;
2630 incoming_count = apply_scale (incoming_count, count_scale);
2631 incoming_frequency
2632 = apply_scale ((gcov_type)incoming_frequency, frequency_scale);
2633 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = incoming_count;
2634 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = incoming_frequency;
2637 /* Must have a CFG here at this point. */
2638 gcc_assert (ENTRY_BLOCK_PTR_FOR_FN
2639 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2641 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2643 ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = entry_block_map;
2644 EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = exit_block_map;
2645 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy);
2646 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy);
2648 /* Duplicate any exception-handling regions. */
2649 if (cfun->eh)
2650 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2651 remap_decl_1, id);
2653 /* Use aux pointers to map the original blocks to copy. */
2654 FOR_EACH_BB_FN (bb, cfun_to_copy)
2655 if (!id->blocks_to_copy || bitmap_bit_p (id->blocks_to_copy, bb->index))
2657 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2658 bb->aux = new_bb;
2659 new_bb->aux = bb;
2660 new_bb->loop_father = entry_block_map->loop_father;
2663 last = last_basic_block_for_fn (cfun);
2665 /* Now that we've duplicated the blocks, duplicate their edges. */
2666 basic_block abnormal_goto_dest = NULL;
2667 if (id->gimple_call
2668 && stmt_can_make_abnormal_goto (id->gimple_call))
2670 gimple_stmt_iterator gsi = gsi_for_stmt (id->gimple_call);
2672 bb = gimple_bb (id->gimple_call);
2673 gsi_next (&gsi);
2674 if (gsi_end_p (gsi))
2675 abnormal_goto_dest = get_abnormal_succ_dispatcher (bb);
2677 FOR_ALL_BB_FN (bb, cfun_to_copy)
2678 if (!id->blocks_to_copy
2679 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
2680 need_debug_cleanup |= copy_edges_for_bb (bb, count_scale, exit_block_map,
2681 abnormal_goto_dest);
2683 if (new_entry)
2685 edge e = make_edge (entry_block_map, (basic_block)new_entry->aux, EDGE_FALLTHRU);
2686 e->probability = REG_BR_PROB_BASE;
2687 e->count = incoming_count;
2690 /* Duplicate the loop tree, if available and wanted. */
2691 if (loops_for_fn (src_cfun) != NULL
2692 && current_loops != NULL)
2694 copy_loops (id, entry_block_map->loop_father,
2695 get_loop (src_cfun, 0));
2696 /* Defer to cfgcleanup to update loop-father fields of basic-blocks. */
2697 loops_state_set (LOOPS_NEED_FIXUP);
2700 /* If the loop tree in the source function needed fixup, mark the
2701 destination loop tree for fixup, too. */
2702 if (loops_for_fn (src_cfun)->state & LOOPS_NEED_FIXUP)
2703 loops_state_set (LOOPS_NEED_FIXUP);
2705 if (gimple_in_ssa_p (cfun))
2706 FOR_ALL_BB_FN (bb, cfun_to_copy)
2707 if (!id->blocks_to_copy
2708 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
2709 copy_phis_for_bb (bb, id);
2711 FOR_ALL_BB_FN (bb, cfun_to_copy)
2712 if (bb->aux)
2714 if (need_debug_cleanup
2715 && bb->index != ENTRY_BLOCK
2716 && bb->index != EXIT_BLOCK)
2717 maybe_move_debug_stmts_to_successors (id, (basic_block) bb->aux);
2718 /* Update call edge destinations. This can not be done before loop
2719 info is updated, because we may split basic blocks. */
2720 if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
2721 redirect_all_calls (id, (basic_block)bb->aux);
2722 ((basic_block)bb->aux)->aux = NULL;
2723 bb->aux = NULL;
2726 /* Zero out AUX fields of newly created block during EH edge
2727 insertion. */
2728 for (; last < last_basic_block_for_fn (cfun); last++)
2730 if (need_debug_cleanup)
2731 maybe_move_debug_stmts_to_successors (id,
2732 BASIC_BLOCK_FOR_FN (cfun, last));
2733 BASIC_BLOCK_FOR_FN (cfun, last)->aux = NULL;
2734 /* Update call edge destinations. This can not be done before loop
2735 info is updated, because we may split basic blocks. */
2736 if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
2737 redirect_all_calls (id, BASIC_BLOCK_FOR_FN (cfun, last));
2739 entry_block_map->aux = NULL;
2740 exit_block_map->aux = NULL;
2742 if (id->eh_map)
2744 delete id->eh_map;
2745 id->eh_map = NULL;
2748 return new_fndecl;
2751 /* Copy the debug STMT using ID. We deal with these statements in a
2752 special way: if any variable in their VALUE expression wasn't
2753 remapped yet, we won't remap it, because that would get decl uids
2754 out of sync, causing codegen differences between -g and -g0. If
2755 this arises, we drop the VALUE expression altogether. */
2757 static void
2758 copy_debug_stmt (gimple stmt, copy_body_data *id)
2760 tree t, *n;
2761 struct walk_stmt_info wi;
2763 if (gimple_block (stmt))
2765 n = id->decl_map->get (gimple_block (stmt));
2766 gimple_set_block (stmt, n ? *n : id->block);
2769 /* Remap all the operands in COPY. */
2770 memset (&wi, 0, sizeof (wi));
2771 wi.info = id;
2773 processing_debug_stmt = 1;
2775 if (gimple_debug_source_bind_p (stmt))
2776 t = gimple_debug_source_bind_get_var (stmt);
2777 else
2778 t = gimple_debug_bind_get_var (stmt);
2780 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2781 && (n = id->debug_map->get (t)))
2783 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2784 t = *n;
2786 else if (TREE_CODE (t) == VAR_DECL
2787 && !is_global_var (t)
2788 && !id->decl_map->get (t))
2789 /* T is a non-localized variable. */;
2790 else
2791 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2793 if (gimple_debug_bind_p (stmt))
2795 gimple_debug_bind_set_var (stmt, t);
2797 if (gimple_debug_bind_has_value_p (stmt))
2798 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2799 remap_gimple_op_r, &wi, NULL);
2801 /* Punt if any decl couldn't be remapped. */
2802 if (processing_debug_stmt < 0)
2803 gimple_debug_bind_reset_value (stmt);
2805 else if (gimple_debug_source_bind_p (stmt))
2807 gimple_debug_source_bind_set_var (stmt, t);
2808 walk_tree (gimple_debug_source_bind_get_value_ptr (stmt),
2809 remap_gimple_op_r, &wi, NULL);
2810 /* When inlining and source bind refers to one of the optimized
2811 away parameters, change the source bind into normal debug bind
2812 referring to the corresponding DEBUG_EXPR_DECL that should have
2813 been bound before the call stmt. */
2814 t = gimple_debug_source_bind_get_value (stmt);
2815 if (t != NULL_TREE
2816 && TREE_CODE (t) == PARM_DECL
2817 && id->gimple_call)
2819 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (id->src_fn);
2820 unsigned int i;
2821 if (debug_args != NULL)
2823 for (i = 0; i < vec_safe_length (*debug_args); i += 2)
2824 if ((**debug_args)[i] == DECL_ORIGIN (t)
2825 && TREE_CODE ((**debug_args)[i + 1]) == DEBUG_EXPR_DECL)
2827 t = (**debug_args)[i + 1];
2828 stmt->subcode = GIMPLE_DEBUG_BIND;
2829 gimple_debug_bind_set_value (stmt, t);
2830 break;
2836 processing_debug_stmt = 0;
2838 update_stmt (stmt);
2841 /* Process deferred debug stmts. In order to give values better odds
2842 of being successfully remapped, we delay the processing of debug
2843 stmts until all other stmts that might require remapping are
2844 processed. */
2846 static void
2847 copy_debug_stmts (copy_body_data *id)
2849 size_t i;
2850 gimple stmt;
2852 if (!id->debug_stmts.exists ())
2853 return;
2855 FOR_EACH_VEC_ELT (id->debug_stmts, i, stmt)
2856 copy_debug_stmt (stmt, id);
2858 id->debug_stmts.release ();
2861 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2862 another function. */
2864 static tree
2865 copy_tree_body (copy_body_data *id)
2867 tree fndecl = id->src_fn;
2868 tree body = DECL_SAVED_TREE (fndecl);
2870 walk_tree (&body, copy_tree_body_r, id, NULL);
2872 return body;
2875 /* Make a copy of the body of FN so that it can be inserted inline in
2876 another function. */
2878 static tree
2879 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
2880 basic_block entry_block_map, basic_block exit_block_map,
2881 basic_block new_entry)
2883 tree fndecl = id->src_fn;
2884 tree body;
2886 /* If this body has a CFG, walk CFG and copy. */
2887 gcc_assert (ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (fndecl)));
2888 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map,
2889 new_entry);
2890 copy_debug_stmts (id);
2892 return body;
2895 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2896 defined in function FN, or of a data member thereof. */
2898 static bool
2899 self_inlining_addr_expr (tree value, tree fn)
2901 tree var;
2903 if (TREE_CODE (value) != ADDR_EXPR)
2904 return false;
2906 var = get_base_address (TREE_OPERAND (value, 0));
2908 return var && auto_var_in_fn_p (var, fn);
2911 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2912 lexical block and line number information from base_stmt, if given,
2913 or from the last stmt of the block otherwise. */
2915 static gimple
2916 insert_init_debug_bind (copy_body_data *id,
2917 basic_block bb, tree var, tree value,
2918 gimple base_stmt)
2920 gimple note;
2921 gimple_stmt_iterator gsi;
2922 tree tracked_var;
2924 if (!gimple_in_ssa_p (id->src_cfun))
2925 return NULL;
2927 if (!MAY_HAVE_DEBUG_STMTS)
2928 return NULL;
2930 tracked_var = target_for_debug_bind (var);
2931 if (!tracked_var)
2932 return NULL;
2934 if (bb)
2936 gsi = gsi_last_bb (bb);
2937 if (!base_stmt && !gsi_end_p (gsi))
2938 base_stmt = gsi_stmt (gsi);
2941 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
2943 if (bb)
2945 if (!gsi_end_p (gsi))
2946 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
2947 else
2948 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
2951 return note;
2954 static void
2955 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
2957 /* If VAR represents a zero-sized variable, it's possible that the
2958 assignment statement may result in no gimple statements. */
2959 if (init_stmt)
2961 gimple_stmt_iterator si = gsi_last_bb (bb);
2963 /* We can end up with init statements that store to a non-register
2964 from a rhs with a conversion. Handle that here by forcing the
2965 rhs into a temporary. gimple_regimplify_operands is not
2966 prepared to do this for us. */
2967 if (!is_gimple_debug (init_stmt)
2968 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
2969 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2970 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2972 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2973 gimple_expr_type (init_stmt),
2974 gimple_assign_rhs1 (init_stmt));
2975 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
2976 GSI_NEW_STMT);
2977 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
2978 gimple_assign_set_rhs1 (init_stmt, rhs);
2980 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2981 gimple_regimplify_operands (init_stmt, &si);
2983 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2985 tree def = gimple_assign_lhs (init_stmt);
2986 insert_init_debug_bind (id, bb, def, def, init_stmt);
2991 /* Initialize parameter P with VALUE. If needed, produce init statement
2992 at the end of BB. When BB is NULL, we return init statement to be
2993 output later. */
2994 static gimple
2995 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2996 basic_block bb, tree *vars)
2998 gimple init_stmt = NULL;
2999 tree var;
3000 tree rhs = value;
3001 tree def = (gimple_in_ssa_p (cfun)
3002 ? ssa_default_def (id->src_cfun, p) : NULL);
3004 if (value
3005 && value != error_mark_node
3006 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
3008 /* If we can match up types by promotion/demotion do so. */
3009 if (fold_convertible_p (TREE_TYPE (p), value))
3010 rhs = fold_convert (TREE_TYPE (p), value);
3011 else
3013 /* ??? For valid programs we should not end up here.
3014 Still if we end up with truly mismatched types here, fall back
3015 to using a VIEW_CONVERT_EXPR or a literal zero to not leak invalid
3016 GIMPLE to the following passes. */
3017 if (!is_gimple_reg_type (TREE_TYPE (value))
3018 || TYPE_SIZE (TREE_TYPE (p)) == TYPE_SIZE (TREE_TYPE (value)))
3019 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
3020 else
3021 rhs = build_zero_cst (TREE_TYPE (p));
3025 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
3026 here since the type of this decl must be visible to the calling
3027 function. */
3028 var = copy_decl_to_var (p, id);
3030 /* Declare this new variable. */
3031 DECL_CHAIN (var) = *vars;
3032 *vars = var;
3034 /* Make gimplifier happy about this variable. */
3035 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
3037 /* If the parameter is never assigned to, has no SSA_NAMEs created,
3038 we would not need to create a new variable here at all, if it
3039 weren't for debug info. Still, we can just use the argument
3040 value. */
3041 if (TREE_READONLY (p)
3042 && !TREE_ADDRESSABLE (p)
3043 && value && !TREE_SIDE_EFFECTS (value)
3044 && !def)
3046 /* We may produce non-gimple trees by adding NOPs or introduce
3047 invalid sharing when operand is not really constant.
3048 It is not big deal to prohibit constant propagation here as
3049 we will constant propagate in DOM1 pass anyway. */
3050 if (is_gimple_min_invariant (value)
3051 && useless_type_conversion_p (TREE_TYPE (p),
3052 TREE_TYPE (value))
3053 /* We have to be very careful about ADDR_EXPR. Make sure
3054 the base variable isn't a local variable of the inlined
3055 function, e.g., when doing recursive inlining, direct or
3056 mutually-recursive or whatever, which is why we don't
3057 just test whether fn == current_function_decl. */
3058 && ! self_inlining_addr_expr (value, fn))
3060 insert_decl_map (id, p, value);
3061 insert_debug_decl_map (id, p, var);
3062 return insert_init_debug_bind (id, bb, var, value, NULL);
3066 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
3067 that way, when the PARM_DECL is encountered, it will be
3068 automatically replaced by the VAR_DECL. */
3069 insert_decl_map (id, p, var);
3071 /* Even if P was TREE_READONLY, the new VAR should not be.
3072 In the original code, we would have constructed a
3073 temporary, and then the function body would have never
3074 changed the value of P. However, now, we will be
3075 constructing VAR directly. The constructor body may
3076 change its value multiple times as it is being
3077 constructed. Therefore, it must not be TREE_READONLY;
3078 the back-end assumes that TREE_READONLY variable is
3079 assigned to only once. */
3080 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
3081 TREE_READONLY (var) = 0;
3083 /* If there is no setup required and we are in SSA, take the easy route
3084 replacing all SSA names representing the function parameter by the
3085 SSA name passed to function.
3087 We need to construct map for the variable anyway as it might be used
3088 in different SSA names when parameter is set in function.
3090 Do replacement at -O0 for const arguments replaced by constant.
3091 This is important for builtin_constant_p and other construct requiring
3092 constant argument to be visible in inlined function body. */
3093 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
3094 && (optimize
3095 || (TREE_READONLY (p)
3096 && is_gimple_min_invariant (rhs)))
3097 && (TREE_CODE (rhs) == SSA_NAME
3098 || is_gimple_min_invariant (rhs))
3099 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
3101 insert_decl_map (id, def, rhs);
3102 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3105 /* If the value of argument is never used, don't care about initializing
3106 it. */
3107 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
3109 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
3110 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3113 /* Initialize this VAR_DECL from the equivalent argument. Convert
3114 the argument to the proper type in case it was promoted. */
3115 if (value)
3117 if (rhs == error_mark_node)
3119 insert_decl_map (id, p, var);
3120 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3123 STRIP_USELESS_TYPE_CONVERSION (rhs);
3125 /* If we are in SSA form properly remap the default definition
3126 or assign to a dummy SSA name if the parameter is unused and
3127 we are not optimizing. */
3128 if (gimple_in_ssa_p (cfun) && is_gimple_reg (p))
3130 if (def)
3132 def = remap_ssa_name (def, id);
3133 init_stmt = gimple_build_assign (def, rhs);
3134 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
3135 set_ssa_default_def (cfun, var, NULL);
3137 else if (!optimize)
3139 def = make_ssa_name (var, NULL);
3140 init_stmt = gimple_build_assign (def, rhs);
3143 else
3144 init_stmt = gimple_build_assign (var, rhs);
3146 if (bb && init_stmt)
3147 insert_init_stmt (id, bb, init_stmt);
3149 return init_stmt;
3152 /* Generate code to initialize the parameters of the function at the
3153 top of the stack in ID from the GIMPLE_CALL STMT. */
3155 static void
3156 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
3157 tree fn, basic_block bb)
3159 tree parms;
3160 size_t i;
3161 tree p;
3162 tree vars = NULL_TREE;
3163 tree static_chain = gimple_call_chain (stmt);
3165 /* Figure out what the parameters are. */
3166 parms = DECL_ARGUMENTS (fn);
3168 /* Loop through the parameter declarations, replacing each with an
3169 equivalent VAR_DECL, appropriately initialized. */
3170 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
3172 tree val;
3173 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
3174 setup_one_parameter (id, p, val, fn, bb, &vars);
3176 /* After remapping parameters remap their types. This has to be done
3177 in a second loop over all parameters to appropriately remap
3178 variable sized arrays when the size is specified in a
3179 parameter following the array. */
3180 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
3182 tree *varp = id->decl_map->get (p);
3183 if (varp
3184 && TREE_CODE (*varp) == VAR_DECL)
3186 tree def = (gimple_in_ssa_p (cfun) && is_gimple_reg (p)
3187 ? ssa_default_def (id->src_cfun, p) : NULL);
3188 tree var = *varp;
3189 TREE_TYPE (var) = remap_type (TREE_TYPE (var), id);
3190 /* Also remap the default definition if it was remapped
3191 to the default definition of the parameter replacement
3192 by the parameter setup. */
3193 if (def)
3195 tree *defp = id->decl_map->get (def);
3196 if (defp
3197 && TREE_CODE (*defp) == SSA_NAME
3198 && SSA_NAME_VAR (*defp) == var)
3199 TREE_TYPE (*defp) = TREE_TYPE (var);
3204 /* Initialize the static chain. */
3205 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
3206 gcc_assert (fn != current_function_decl);
3207 if (p)
3209 /* No static chain? Seems like a bug in tree-nested.c. */
3210 gcc_assert (static_chain);
3212 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
3215 declare_inline_vars (id->block, vars);
3219 /* Declare a return variable to replace the RESULT_DECL for the
3220 function we are calling. An appropriate DECL_STMT is returned.
3221 The USE_STMT is filled to contain a use of the declaration to
3222 indicate the return value of the function.
3224 RETURN_SLOT, if non-null is place where to store the result. It
3225 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
3226 was the LHS of the MODIFY_EXPR to which this call is the RHS.
3228 RETURN_BOUNDS holds a destination for returned bounds.
3230 The return value is a (possibly null) value that holds the result
3231 as seen by the caller. */
3233 static tree
3234 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
3235 tree return_bounds, basic_block entry_bb)
3237 tree callee = id->src_fn;
3238 tree result = DECL_RESULT (callee);
3239 tree callee_type = TREE_TYPE (result);
3240 tree caller_type;
3241 tree var, use;
3243 /* Handle type-mismatches in the function declaration return type
3244 vs. the call expression. */
3245 if (modify_dest)
3246 caller_type = TREE_TYPE (modify_dest);
3247 else
3248 caller_type = TREE_TYPE (TREE_TYPE (callee));
3250 /* We don't need to do anything for functions that don't return anything. */
3251 if (VOID_TYPE_P (callee_type))
3252 return NULL_TREE;
3254 /* If there was a return slot, then the return value is the
3255 dereferenced address of that object. */
3256 if (return_slot)
3258 /* The front end shouldn't have used both return_slot and
3259 a modify expression. */
3260 gcc_assert (!modify_dest);
3261 if (DECL_BY_REFERENCE (result))
3263 tree return_slot_addr = build_fold_addr_expr (return_slot);
3264 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
3266 /* We are going to construct *&return_slot and we can't do that
3267 for variables believed to be not addressable.
3269 FIXME: This check possibly can match, because values returned
3270 via return slot optimization are not believed to have address
3271 taken by alias analysis. */
3272 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
3273 var = return_slot_addr;
3275 else
3277 var = return_slot;
3278 gcc_assert (TREE_CODE (var) != SSA_NAME);
3279 if (TREE_ADDRESSABLE (result))
3280 mark_addressable (var);
3282 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
3283 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
3284 && !DECL_GIMPLE_REG_P (result)
3285 && DECL_P (var))
3286 DECL_GIMPLE_REG_P (var) = 0;
3287 use = NULL;
3288 goto done;
3291 /* All types requiring non-trivial constructors should have been handled. */
3292 gcc_assert (!TREE_ADDRESSABLE (callee_type));
3294 /* Attempt to avoid creating a new temporary variable. */
3295 if (modify_dest
3296 && TREE_CODE (modify_dest) != SSA_NAME)
3298 bool use_it = false;
3300 /* We can't use MODIFY_DEST if there's type promotion involved. */
3301 if (!useless_type_conversion_p (callee_type, caller_type))
3302 use_it = false;
3304 /* ??? If we're assigning to a variable sized type, then we must
3305 reuse the destination variable, because we've no good way to
3306 create variable sized temporaries at this point. */
3307 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
3308 use_it = true;
3310 /* If the callee cannot possibly modify MODIFY_DEST, then we can
3311 reuse it as the result of the call directly. Don't do this if
3312 it would promote MODIFY_DEST to addressable. */
3313 else if (TREE_ADDRESSABLE (result))
3314 use_it = false;
3315 else
3317 tree base_m = get_base_address (modify_dest);
3319 /* If the base isn't a decl, then it's a pointer, and we don't
3320 know where that's going to go. */
3321 if (!DECL_P (base_m))
3322 use_it = false;
3323 else if (is_global_var (base_m))
3324 use_it = false;
3325 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
3326 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
3327 && !DECL_GIMPLE_REG_P (result)
3328 && DECL_GIMPLE_REG_P (base_m))
3329 use_it = false;
3330 else if (!TREE_ADDRESSABLE (base_m))
3331 use_it = true;
3334 if (use_it)
3336 var = modify_dest;
3337 use = NULL;
3338 goto done;
3342 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
3344 var = copy_result_decl_to_var (result, id);
3345 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
3347 /* Do not have the rest of GCC warn about this variable as it should
3348 not be visible to the user. */
3349 TREE_NO_WARNING (var) = 1;
3351 declare_inline_vars (id->block, var);
3353 /* Build the use expr. If the return type of the function was
3354 promoted, convert it back to the expected type. */
3355 use = var;
3356 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
3358 /* If we can match up types by promotion/demotion do so. */
3359 if (fold_convertible_p (caller_type, var))
3360 use = fold_convert (caller_type, var);
3361 else
3363 /* ??? For valid programs we should not end up here.
3364 Still if we end up with truly mismatched types here, fall back
3365 to using a MEM_REF to not leak invalid GIMPLE to the following
3366 passes. */
3367 /* Prevent var from being written into SSA form. */
3368 if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE
3369 || TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE)
3370 DECL_GIMPLE_REG_P (var) = false;
3371 else if (is_gimple_reg_type (TREE_TYPE (var)))
3372 TREE_ADDRESSABLE (var) = true;
3373 use = fold_build2 (MEM_REF, caller_type,
3374 build_fold_addr_expr (var),
3375 build_int_cst (ptr_type_node, 0));
3379 STRIP_USELESS_TYPE_CONVERSION (use);
3381 if (DECL_BY_REFERENCE (result))
3383 TREE_ADDRESSABLE (var) = 1;
3384 var = build_fold_addr_expr (var);
3387 done:
3388 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
3389 way, when the RESULT_DECL is encountered, it will be
3390 automatically replaced by the VAR_DECL.
3392 When returning by reference, ensure that RESULT_DECL remaps to
3393 gimple_val. */
3394 if (DECL_BY_REFERENCE (result)
3395 && !is_gimple_val (var))
3397 tree temp = create_tmp_var (TREE_TYPE (result), "retvalptr");
3398 insert_decl_map (id, result, temp);
3399 /* When RESULT_DECL is in SSA form, we need to remap and initialize
3400 it's default_def SSA_NAME. */
3401 if (gimple_in_ssa_p (id->src_cfun)
3402 && is_gimple_reg (result))
3404 temp = make_ssa_name (temp, NULL);
3405 insert_decl_map (id, ssa_default_def (id->src_cfun, result), temp);
3407 insert_init_stmt (id, entry_bb, gimple_build_assign (temp, var));
3409 else
3410 insert_decl_map (id, result, var);
3412 /* Remember this so we can ignore it in remap_decls. */
3413 id->retvar = var;
3415 /* If returned bounds are used, then make var for them. */
3416 if (return_bounds)
3418 tree bndtemp = create_tmp_var (pointer_bounds_type_node, "retbnd");
3419 DECL_SEEN_IN_BIND_EXPR_P (bndtemp) = 1;
3420 TREE_NO_WARNING (bndtemp) = 1;
3421 declare_inline_vars (id->block, bndtemp);
3423 id->retbnd = bndtemp;
3424 insert_init_stmt (id, entry_bb,
3425 gimple_build_assign (bndtemp, chkp_get_zero_bounds_var ()));
3428 return use;
3431 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
3432 to a local label. */
3434 static tree
3435 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
3437 tree node = *nodep;
3438 tree fn = (tree) fnp;
3440 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
3441 return node;
3443 if (TYPE_P (node))
3444 *walk_subtrees = 0;
3446 return NULL_TREE;
3449 /* Determine if the function can be copied. If so return NULL. If
3450 not return a string describng the reason for failure. */
3452 static const char *
3453 copy_forbidden (struct function *fun, tree fndecl)
3455 const char *reason = fun->cannot_be_copied_reason;
3456 tree decl;
3457 unsigned ix;
3459 /* Only examine the function once. */
3460 if (fun->cannot_be_copied_set)
3461 return reason;
3463 /* We cannot copy a function that receives a non-local goto
3464 because we cannot remap the destination label used in the
3465 function that is performing the non-local goto. */
3466 /* ??? Actually, this should be possible, if we work at it.
3467 No doubt there's just a handful of places that simply
3468 assume it doesn't happen and don't substitute properly. */
3469 if (fun->has_nonlocal_label)
3471 reason = G_("function %q+F can never be copied "
3472 "because it receives a non-local goto");
3473 goto fail;
3476 FOR_EACH_LOCAL_DECL (fun, ix, decl)
3477 if (TREE_CODE (decl) == VAR_DECL
3478 && TREE_STATIC (decl)
3479 && !DECL_EXTERNAL (decl)
3480 && DECL_INITIAL (decl)
3481 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
3482 has_label_address_in_static_1,
3483 fndecl))
3485 reason = G_("function %q+F can never be copied because it saves "
3486 "address of local label in a static variable");
3487 goto fail;
3490 fail:
3491 fun->cannot_be_copied_reason = reason;
3492 fun->cannot_be_copied_set = true;
3493 return reason;
3497 static const char *inline_forbidden_reason;
3499 /* A callback for walk_gimple_seq to handle statements. Returns non-null
3500 iff a function can not be inlined. Also sets the reason why. */
3502 static tree
3503 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
3504 struct walk_stmt_info *wip)
3506 tree fn = (tree) wip->info;
3507 tree t;
3508 gimple stmt = gsi_stmt (*gsi);
3510 switch (gimple_code (stmt))
3512 case GIMPLE_CALL:
3513 /* Refuse to inline alloca call unless user explicitly forced so as
3514 this may change program's memory overhead drastically when the
3515 function using alloca is called in loop. In GCC present in
3516 SPEC2000 inlining into schedule_block cause it to require 2GB of
3517 RAM instead of 256MB. Don't do so for alloca calls emitted for
3518 VLA objects as those can't cause unbounded growth (they're always
3519 wrapped inside stack_save/stack_restore regions. */
3520 if (gimple_alloca_call_p (stmt)
3521 && !gimple_call_alloca_for_var_p (stmt)
3522 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
3524 inline_forbidden_reason
3525 = G_("function %q+F can never be inlined because it uses "
3526 "alloca (override using the always_inline attribute)");
3527 *handled_ops_p = true;
3528 return fn;
3531 t = gimple_call_fndecl (stmt);
3532 if (t == NULL_TREE)
3533 break;
3535 /* We cannot inline functions that call setjmp. */
3536 if (setjmp_call_p (t))
3538 inline_forbidden_reason
3539 = G_("function %q+F can never be inlined because it uses setjmp");
3540 *handled_ops_p = true;
3541 return t;
3544 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
3545 switch (DECL_FUNCTION_CODE (t))
3547 /* We cannot inline functions that take a variable number of
3548 arguments. */
3549 case BUILT_IN_VA_START:
3550 case BUILT_IN_NEXT_ARG:
3551 case BUILT_IN_VA_END:
3552 inline_forbidden_reason
3553 = G_("function %q+F can never be inlined because it "
3554 "uses variable argument lists");
3555 *handled_ops_p = true;
3556 return t;
3558 case BUILT_IN_LONGJMP:
3559 /* We can't inline functions that call __builtin_longjmp at
3560 all. The non-local goto machinery really requires the
3561 destination be in a different function. If we allow the
3562 function calling __builtin_longjmp to be inlined into the
3563 function calling __builtin_setjmp, Things will Go Awry. */
3564 inline_forbidden_reason
3565 = G_("function %q+F can never be inlined because "
3566 "it uses setjmp-longjmp exception handling");
3567 *handled_ops_p = true;
3568 return t;
3570 case BUILT_IN_NONLOCAL_GOTO:
3571 /* Similarly. */
3572 inline_forbidden_reason
3573 = G_("function %q+F can never be inlined because "
3574 "it uses non-local goto");
3575 *handled_ops_p = true;
3576 return t;
3578 case BUILT_IN_RETURN:
3579 case BUILT_IN_APPLY_ARGS:
3580 /* If a __builtin_apply_args caller would be inlined,
3581 it would be saving arguments of the function it has
3582 been inlined into. Similarly __builtin_return would
3583 return from the function the inline has been inlined into. */
3584 inline_forbidden_reason
3585 = G_("function %q+F can never be inlined because "
3586 "it uses __builtin_return or __builtin_apply_args");
3587 *handled_ops_p = true;
3588 return t;
3590 default:
3591 break;
3593 break;
3595 case GIMPLE_GOTO:
3596 t = gimple_goto_dest (stmt);
3598 /* We will not inline a function which uses computed goto. The
3599 addresses of its local labels, which may be tucked into
3600 global storage, are of course not constant across
3601 instantiations, which causes unexpected behavior. */
3602 if (TREE_CODE (t) != LABEL_DECL)
3604 inline_forbidden_reason
3605 = G_("function %q+F can never be inlined "
3606 "because it contains a computed goto");
3607 *handled_ops_p = true;
3608 return t;
3610 break;
3612 default:
3613 break;
3616 *handled_ops_p = false;
3617 return NULL_TREE;
3620 /* Return true if FNDECL is a function that cannot be inlined into
3621 another one. */
3623 static bool
3624 inline_forbidden_p (tree fndecl)
3626 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
3627 struct walk_stmt_info wi;
3628 basic_block bb;
3629 bool forbidden_p = false;
3631 /* First check for shared reasons not to copy the code. */
3632 inline_forbidden_reason = copy_forbidden (fun, fndecl);
3633 if (inline_forbidden_reason != NULL)
3634 return true;
3636 /* Next, walk the statements of the function looking for
3637 constraucts we can't handle, or are non-optimal for inlining. */
3638 hash_set<tree> visited_nodes;
3639 memset (&wi, 0, sizeof (wi));
3640 wi.info = (void *) fndecl;
3641 wi.pset = &visited_nodes;
3643 FOR_EACH_BB_FN (bb, fun)
3645 gimple ret;
3646 gimple_seq seq = bb_seq (bb);
3647 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
3648 forbidden_p = (ret != NULL);
3649 if (forbidden_p)
3650 break;
3653 return forbidden_p;
3656 /* Return false if the function FNDECL cannot be inlined on account of its
3657 attributes, true otherwise. */
3658 static bool
3659 function_attribute_inlinable_p (const_tree fndecl)
3661 if (targetm.attribute_table)
3663 const_tree a;
3665 for (a = DECL_ATTRIBUTES (fndecl); a; a = TREE_CHAIN (a))
3667 const_tree name = TREE_PURPOSE (a);
3668 int i;
3670 for (i = 0; targetm.attribute_table[i].name != NULL; i++)
3671 if (is_attribute_p (targetm.attribute_table[i].name, name))
3672 return targetm.function_attribute_inlinable_p (fndecl);
3676 return true;
3679 /* Returns nonzero if FN is a function that does not have any
3680 fundamental inline blocking properties. */
3682 bool
3683 tree_inlinable_function_p (tree fn)
3685 bool inlinable = true;
3686 bool do_warning;
3687 tree always_inline;
3689 /* If we've already decided this function shouldn't be inlined,
3690 there's no need to check again. */
3691 if (DECL_UNINLINABLE (fn))
3692 return false;
3694 /* We only warn for functions declared `inline' by the user. */
3695 do_warning = (warn_inline
3696 && DECL_DECLARED_INLINE_P (fn)
3697 && !DECL_NO_INLINE_WARNING_P (fn)
3698 && !DECL_IN_SYSTEM_HEADER (fn));
3700 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
3702 if (flag_no_inline
3703 && always_inline == NULL)
3705 if (do_warning)
3706 warning (OPT_Winline, "function %q+F can never be inlined because it "
3707 "is suppressed using -fno-inline", fn);
3708 inlinable = false;
3711 else if (!function_attribute_inlinable_p (fn))
3713 if (do_warning)
3714 warning (OPT_Winline, "function %q+F can never be inlined because it "
3715 "uses attributes conflicting with inlining", fn);
3716 inlinable = false;
3719 else if (inline_forbidden_p (fn))
3721 /* See if we should warn about uninlinable functions. Previously,
3722 some of these warnings would be issued while trying to expand
3723 the function inline, but that would cause multiple warnings
3724 about functions that would for example call alloca. But since
3725 this a property of the function, just one warning is enough.
3726 As a bonus we can now give more details about the reason why a
3727 function is not inlinable. */
3728 if (always_inline)
3729 error (inline_forbidden_reason, fn);
3730 else if (do_warning)
3731 warning (OPT_Winline, inline_forbidden_reason, fn);
3733 inlinable = false;
3736 /* Squirrel away the result so that we don't have to check again. */
3737 DECL_UNINLINABLE (fn) = !inlinable;
3739 return inlinable;
3742 /* Estimate the cost of a memory move of type TYPE. Use machine dependent
3743 word size and take possible memcpy call into account and return
3744 cost based on whether optimizing for size or speed according to SPEED_P. */
3747 estimate_move_cost (tree type, bool ARG_UNUSED (speed_p))
3749 HOST_WIDE_INT size;
3751 gcc_assert (!VOID_TYPE_P (type));
3753 if (TREE_CODE (type) == VECTOR_TYPE)
3755 machine_mode inner = TYPE_MODE (TREE_TYPE (type));
3756 machine_mode simd
3757 = targetm.vectorize.preferred_simd_mode (inner);
3758 int simd_mode_size = GET_MODE_SIZE (simd);
3759 return ((GET_MODE_SIZE (TYPE_MODE (type)) + simd_mode_size - 1)
3760 / simd_mode_size);
3763 size = int_size_in_bytes (type);
3765 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (speed_p))
3766 /* Cost of a memcpy call, 3 arguments and the call. */
3767 return 4;
3768 else
3769 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
3772 /* Returns cost of operation CODE, according to WEIGHTS */
3774 static int
3775 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3776 tree op1 ATTRIBUTE_UNUSED, tree op2)
3778 switch (code)
3780 /* These are "free" conversions, or their presumed cost
3781 is folded into other operations. */
3782 case RANGE_EXPR:
3783 CASE_CONVERT:
3784 case COMPLEX_EXPR:
3785 case PAREN_EXPR:
3786 case VIEW_CONVERT_EXPR:
3787 return 0;
3789 /* Assign cost of 1 to usual operations.
3790 ??? We may consider mapping RTL costs to this. */
3791 case COND_EXPR:
3792 case VEC_COND_EXPR:
3793 case VEC_PERM_EXPR:
3795 case PLUS_EXPR:
3796 case POINTER_PLUS_EXPR:
3797 case MINUS_EXPR:
3798 case MULT_EXPR:
3799 case MULT_HIGHPART_EXPR:
3800 case FMA_EXPR:
3802 case ADDR_SPACE_CONVERT_EXPR:
3803 case FIXED_CONVERT_EXPR:
3804 case FIX_TRUNC_EXPR:
3806 case NEGATE_EXPR:
3807 case FLOAT_EXPR:
3808 case MIN_EXPR:
3809 case MAX_EXPR:
3810 case ABS_EXPR:
3812 case LSHIFT_EXPR:
3813 case RSHIFT_EXPR:
3814 case LROTATE_EXPR:
3815 case RROTATE_EXPR:
3816 case VEC_RSHIFT_EXPR:
3818 case BIT_IOR_EXPR:
3819 case BIT_XOR_EXPR:
3820 case BIT_AND_EXPR:
3821 case BIT_NOT_EXPR:
3823 case TRUTH_ANDIF_EXPR:
3824 case TRUTH_ORIF_EXPR:
3825 case TRUTH_AND_EXPR:
3826 case TRUTH_OR_EXPR:
3827 case TRUTH_XOR_EXPR:
3828 case TRUTH_NOT_EXPR:
3830 case LT_EXPR:
3831 case LE_EXPR:
3832 case GT_EXPR:
3833 case GE_EXPR:
3834 case EQ_EXPR:
3835 case NE_EXPR:
3836 case ORDERED_EXPR:
3837 case UNORDERED_EXPR:
3839 case UNLT_EXPR:
3840 case UNLE_EXPR:
3841 case UNGT_EXPR:
3842 case UNGE_EXPR:
3843 case UNEQ_EXPR:
3844 case LTGT_EXPR:
3846 case CONJ_EXPR:
3848 case PREDECREMENT_EXPR:
3849 case PREINCREMENT_EXPR:
3850 case POSTDECREMENT_EXPR:
3851 case POSTINCREMENT_EXPR:
3853 case REALIGN_LOAD_EXPR:
3855 case REDUC_MAX_EXPR:
3856 case REDUC_MIN_EXPR:
3857 case REDUC_PLUS_EXPR:
3858 case WIDEN_SUM_EXPR:
3859 case WIDEN_MULT_EXPR:
3860 case DOT_PROD_EXPR:
3861 case SAD_EXPR:
3862 case WIDEN_MULT_PLUS_EXPR:
3863 case WIDEN_MULT_MINUS_EXPR:
3864 case WIDEN_LSHIFT_EXPR:
3866 case VEC_WIDEN_MULT_HI_EXPR:
3867 case VEC_WIDEN_MULT_LO_EXPR:
3868 case VEC_WIDEN_MULT_EVEN_EXPR:
3869 case VEC_WIDEN_MULT_ODD_EXPR:
3870 case VEC_UNPACK_HI_EXPR:
3871 case VEC_UNPACK_LO_EXPR:
3872 case VEC_UNPACK_FLOAT_HI_EXPR:
3873 case VEC_UNPACK_FLOAT_LO_EXPR:
3874 case VEC_PACK_TRUNC_EXPR:
3875 case VEC_PACK_SAT_EXPR:
3876 case VEC_PACK_FIX_TRUNC_EXPR:
3877 case VEC_WIDEN_LSHIFT_HI_EXPR:
3878 case VEC_WIDEN_LSHIFT_LO_EXPR:
3880 return 1;
3882 /* Few special cases of expensive operations. This is useful
3883 to avoid inlining on functions having too many of these. */
3884 case TRUNC_DIV_EXPR:
3885 case CEIL_DIV_EXPR:
3886 case FLOOR_DIV_EXPR:
3887 case ROUND_DIV_EXPR:
3888 case EXACT_DIV_EXPR:
3889 case TRUNC_MOD_EXPR:
3890 case CEIL_MOD_EXPR:
3891 case FLOOR_MOD_EXPR:
3892 case ROUND_MOD_EXPR:
3893 case RDIV_EXPR:
3894 if (TREE_CODE (op2) != INTEGER_CST)
3895 return weights->div_mod_cost;
3896 return 1;
3898 default:
3899 /* We expect a copy assignment with no operator. */
3900 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
3901 return 0;
3906 /* Estimate number of instructions that will be created by expanding
3907 the statements in the statement sequence STMTS.
3908 WEIGHTS contains weights attributed to various constructs. */
3910 static
3911 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
3913 int cost;
3914 gimple_stmt_iterator gsi;
3916 cost = 0;
3917 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
3918 cost += estimate_num_insns (gsi_stmt (gsi), weights);
3920 return cost;
3924 /* Estimate number of instructions that will be created by expanding STMT.
3925 WEIGHTS contains weights attributed to various constructs. */
3928 estimate_num_insns (gimple stmt, eni_weights *weights)
3930 unsigned cost, i;
3931 enum gimple_code code = gimple_code (stmt);
3932 tree lhs;
3933 tree rhs;
3935 switch (code)
3937 case GIMPLE_ASSIGN:
3938 /* Try to estimate the cost of assignments. We have three cases to
3939 deal with:
3940 1) Simple assignments to registers;
3941 2) Stores to things that must live in memory. This includes
3942 "normal" stores to scalars, but also assignments of large
3943 structures, or constructors of big arrays;
3945 Let us look at the first two cases, assuming we have "a = b + C":
3946 <GIMPLE_ASSIGN <var_decl "a">
3947 <plus_expr <var_decl "b"> <constant C>>
3948 If "a" is a GIMPLE register, the assignment to it is free on almost
3949 any target, because "a" usually ends up in a real register. Hence
3950 the only cost of this expression comes from the PLUS_EXPR, and we
3951 can ignore the GIMPLE_ASSIGN.
3952 If "a" is not a GIMPLE register, the assignment to "a" will most
3953 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
3954 of moving something into "a", which we compute using the function
3955 estimate_move_cost. */
3956 if (gimple_clobber_p (stmt))
3957 return 0; /* ={v} {CLOBBER} stmt expands to nothing. */
3959 lhs = gimple_assign_lhs (stmt);
3960 rhs = gimple_assign_rhs1 (stmt);
3962 cost = 0;
3964 /* Account for the cost of moving to / from memory. */
3965 if (gimple_store_p (stmt))
3966 cost += estimate_move_cost (TREE_TYPE (lhs), weights->time_based);
3967 if (gimple_assign_load_p (stmt))
3968 cost += estimate_move_cost (TREE_TYPE (rhs), weights->time_based);
3970 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3971 gimple_assign_rhs1 (stmt),
3972 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3973 == GIMPLE_BINARY_RHS
3974 ? gimple_assign_rhs2 (stmt) : NULL);
3975 break;
3977 case GIMPLE_COND:
3978 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3979 gimple_op (stmt, 0),
3980 gimple_op (stmt, 1));
3981 break;
3983 case GIMPLE_SWITCH:
3984 /* Take into account cost of the switch + guess 2 conditional jumps for
3985 each case label.
3987 TODO: once the switch expansion logic is sufficiently separated, we can
3988 do better job on estimating cost of the switch. */
3989 if (weights->time_based)
3990 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3991 else
3992 cost = gimple_switch_num_labels (stmt) * 2;
3993 break;
3995 case GIMPLE_CALL:
3997 tree decl;
3999 if (gimple_call_internal_p (stmt))
4000 return 0;
4001 else if ((decl = gimple_call_fndecl (stmt))
4002 && DECL_BUILT_IN (decl))
4004 /* Do not special case builtins where we see the body.
4005 This just confuse inliner. */
4006 struct cgraph_node *node;
4007 if (!(node = cgraph_node::get (decl))
4008 || node->definition)
4010 /* For buitins that are likely expanded to nothing or
4011 inlined do not account operand costs. */
4012 else if (is_simple_builtin (decl))
4013 return 0;
4014 else if (is_inexpensive_builtin (decl))
4015 return weights->target_builtin_call_cost;
4016 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4018 /* We canonicalize x * x to pow (x, 2.0) with -ffast-math, so
4019 specialize the cheap expansion we do here.
4020 ??? This asks for a more general solution. */
4021 switch (DECL_FUNCTION_CODE (decl))
4023 case BUILT_IN_POW:
4024 case BUILT_IN_POWF:
4025 case BUILT_IN_POWL:
4026 if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
4027 && REAL_VALUES_EQUAL
4028 (TREE_REAL_CST (gimple_call_arg (stmt, 1)), dconst2))
4029 return estimate_operator_cost
4030 (MULT_EXPR, weights, gimple_call_arg (stmt, 0),
4031 gimple_call_arg (stmt, 0));
4032 break;
4034 default:
4035 break;
4040 cost = decl ? weights->call_cost : weights->indirect_call_cost;
4041 if (gimple_call_lhs (stmt))
4042 cost += estimate_move_cost (TREE_TYPE (gimple_call_lhs (stmt)),
4043 weights->time_based);
4044 for (i = 0; i < gimple_call_num_args (stmt); i++)
4046 tree arg = gimple_call_arg (stmt, i);
4047 cost += estimate_move_cost (TREE_TYPE (arg),
4048 weights->time_based);
4050 break;
4053 case GIMPLE_RETURN:
4054 return weights->return_cost;
4056 case GIMPLE_GOTO:
4057 case GIMPLE_LABEL:
4058 case GIMPLE_NOP:
4059 case GIMPLE_PHI:
4060 case GIMPLE_PREDICT:
4061 case GIMPLE_DEBUG:
4062 return 0;
4064 case GIMPLE_ASM:
4066 int count = asm_str_count (gimple_asm_string (stmt));
4067 /* 1000 means infinity. This avoids overflows later
4068 with very long asm statements. */
4069 if (count > 1000)
4070 count = 1000;
4071 return count;
4074 case GIMPLE_RESX:
4075 /* This is either going to be an external function call with one
4076 argument, or two register copy statements plus a goto. */
4077 return 2;
4079 case GIMPLE_EH_DISPATCH:
4080 /* ??? This is going to turn into a switch statement. Ideally
4081 we'd have a look at the eh region and estimate the number of
4082 edges involved. */
4083 return 10;
4085 case GIMPLE_BIND:
4086 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
4088 case GIMPLE_EH_FILTER:
4089 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
4091 case GIMPLE_CATCH:
4092 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
4094 case GIMPLE_TRY:
4095 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
4096 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
4098 /* OpenMP directives are generally very expensive. */
4100 case GIMPLE_OMP_RETURN:
4101 case GIMPLE_OMP_SECTIONS_SWITCH:
4102 case GIMPLE_OMP_ATOMIC_STORE:
4103 case GIMPLE_OMP_CONTINUE:
4104 /* ...except these, which are cheap. */
4105 return 0;
4107 case GIMPLE_OMP_ATOMIC_LOAD:
4108 return weights->omp_cost;
4110 case GIMPLE_OMP_FOR:
4111 return (weights->omp_cost
4112 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
4113 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
4115 case GIMPLE_OACC_KERNELS:
4116 case GIMPLE_OACC_PARALLEL:
4117 case GIMPLE_OMP_PARALLEL:
4118 case GIMPLE_OMP_TASK:
4119 case GIMPLE_OMP_CRITICAL:
4120 case GIMPLE_OMP_MASTER:
4121 case GIMPLE_OMP_TASKGROUP:
4122 case GIMPLE_OMP_ORDERED:
4123 case GIMPLE_OMP_SECTION:
4124 case GIMPLE_OMP_SECTIONS:
4125 case GIMPLE_OMP_SINGLE:
4126 case GIMPLE_OMP_TARGET:
4127 case GIMPLE_OMP_TEAMS:
4128 return (weights->omp_cost
4129 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
4131 case GIMPLE_TRANSACTION:
4132 return (weights->tm_cost
4133 + estimate_num_insns_seq (gimple_transaction_body (stmt),
4134 weights));
4136 default:
4137 gcc_unreachable ();
4140 return cost;
4143 /* Estimate number of instructions that will be created by expanding
4144 function FNDECL. WEIGHTS contains weights attributed to various
4145 constructs. */
4148 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
4150 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
4151 gimple_stmt_iterator bsi;
4152 basic_block bb;
4153 int n = 0;
4155 gcc_assert (my_function && my_function->cfg);
4156 FOR_EACH_BB_FN (bb, my_function)
4158 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4159 n += estimate_num_insns (gsi_stmt (bsi), weights);
4162 return n;
4166 /* Initializes weights used by estimate_num_insns. */
4168 void
4169 init_inline_once (void)
4171 eni_size_weights.call_cost = 1;
4172 eni_size_weights.indirect_call_cost = 3;
4173 eni_size_weights.target_builtin_call_cost = 1;
4174 eni_size_weights.div_mod_cost = 1;
4175 eni_size_weights.omp_cost = 40;
4176 eni_size_weights.tm_cost = 10;
4177 eni_size_weights.time_based = false;
4178 eni_size_weights.return_cost = 1;
4180 /* Estimating time for call is difficult, since we have no idea what the
4181 called function does. In the current uses of eni_time_weights,
4182 underestimating the cost does less harm than overestimating it, so
4183 we choose a rather small value here. */
4184 eni_time_weights.call_cost = 10;
4185 eni_time_weights.indirect_call_cost = 15;
4186 eni_time_weights.target_builtin_call_cost = 1;
4187 eni_time_weights.div_mod_cost = 10;
4188 eni_time_weights.omp_cost = 40;
4189 eni_time_weights.tm_cost = 40;
4190 eni_time_weights.time_based = true;
4191 eni_time_weights.return_cost = 2;
4194 /* Estimate the number of instructions in a gimple_seq. */
4197 count_insns_seq (gimple_seq seq, eni_weights *weights)
4199 gimple_stmt_iterator gsi;
4200 int n = 0;
4201 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
4202 n += estimate_num_insns (gsi_stmt (gsi), weights);
4204 return n;
4208 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
4210 static void
4211 prepend_lexical_block (tree current_block, tree new_block)
4213 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
4214 BLOCK_SUBBLOCKS (current_block) = new_block;
4215 BLOCK_SUPERCONTEXT (new_block) = current_block;
4218 /* Add local variables from CALLEE to CALLER. */
4220 static inline void
4221 add_local_variables (struct function *callee, struct function *caller,
4222 copy_body_data *id)
4224 tree var;
4225 unsigned ix;
4227 FOR_EACH_LOCAL_DECL (callee, ix, var)
4228 if (!can_be_nonlocal (var, id))
4230 tree new_var = remap_decl (var, id);
4232 /* Remap debug-expressions. */
4233 if (TREE_CODE (new_var) == VAR_DECL
4234 && DECL_HAS_DEBUG_EXPR_P (var)
4235 && new_var != var)
4237 tree tem = DECL_DEBUG_EXPR (var);
4238 bool old_regimplify = id->regimplify;
4239 id->remapping_type_depth++;
4240 walk_tree (&tem, copy_tree_body_r, id, NULL);
4241 id->remapping_type_depth--;
4242 id->regimplify = old_regimplify;
4243 SET_DECL_DEBUG_EXPR (new_var, tem);
4244 DECL_HAS_DEBUG_EXPR_P (new_var) = 1;
4246 add_local_decl (caller, new_var);
4250 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
4252 static bool
4253 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
4255 tree use_retvar;
4256 tree fn;
4257 hash_map<tree, tree> *dst;
4258 hash_map<tree, tree> *st = NULL;
4259 tree return_slot;
4260 tree modify_dest;
4261 tree return_bounds = NULL;
4262 location_t saved_location;
4263 struct cgraph_edge *cg_edge;
4264 cgraph_inline_failed_t reason;
4265 basic_block return_block;
4266 edge e;
4267 gimple_stmt_iterator gsi, stmt_gsi;
4268 bool successfully_inlined = FALSE;
4269 bool purge_dead_abnormal_edges;
4270 unsigned int i;
4272 /* Set input_location here so we get the right instantiation context
4273 if we call instantiate_decl from inlinable_function_p. */
4274 /* FIXME: instantiate_decl isn't called by inlinable_function_p. */
4275 saved_location = input_location;
4276 input_location = gimple_location (stmt);
4278 /* From here on, we're only interested in CALL_EXPRs. */
4279 if (gimple_code (stmt) != GIMPLE_CALL)
4280 goto egress;
4282 cg_edge = id->dst_node->get_edge (stmt);
4283 gcc_checking_assert (cg_edge);
4284 /* First, see if we can figure out what function is being called.
4285 If we cannot, then there is no hope of inlining the function. */
4286 if (cg_edge->indirect_unknown_callee)
4287 goto egress;
4288 fn = cg_edge->callee->decl;
4289 gcc_checking_assert (fn);
4291 /* If FN is a declaration of a function in a nested scope that was
4292 globally declared inline, we don't set its DECL_INITIAL.
4293 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
4294 C++ front-end uses it for cdtors to refer to their internal
4295 declarations, that are not real functions. Fortunately those
4296 don't have trees to be saved, so we can tell by checking their
4297 gimple_body. */
4298 if (!DECL_INITIAL (fn)
4299 && DECL_ABSTRACT_ORIGIN (fn)
4300 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
4301 fn = DECL_ABSTRACT_ORIGIN (fn);
4303 /* Don't try to inline functions that are not well-suited to inlining. */
4304 if (cg_edge->inline_failed)
4306 reason = cg_edge->inline_failed;
4307 /* If this call was originally indirect, we do not want to emit any
4308 inlining related warnings or sorry messages because there are no
4309 guarantees regarding those. */
4310 if (cg_edge->indirect_inlining_edge)
4311 goto egress;
4313 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
4314 /* For extern inline functions that get redefined we always
4315 silently ignored always_inline flag. Better behaviour would
4316 be to be able to keep both bodies and use extern inline body
4317 for inlining, but we can't do that because frontends overwrite
4318 the body. */
4319 && !cg_edge->callee->local.redefined_extern_inline
4320 /* During early inline pass, report only when optimization is
4321 not turned on. */
4322 && (symtab->global_info_ready
4323 || !optimize
4324 || cgraph_inline_failed_type (reason) == CIF_FINAL_ERROR)
4325 /* PR 20090218-1_0.c. Body can be provided by another module. */
4326 && (reason != CIF_BODY_NOT_AVAILABLE || !flag_generate_lto))
4328 error ("inlining failed in call to always_inline %q+F: %s", fn,
4329 cgraph_inline_failed_string (reason));
4330 error ("called from here");
4332 else if (warn_inline
4333 && DECL_DECLARED_INLINE_P (fn)
4334 && !DECL_NO_INLINE_WARNING_P (fn)
4335 && !DECL_IN_SYSTEM_HEADER (fn)
4336 && reason != CIF_UNSPECIFIED
4337 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
4338 /* Do not warn about not inlined recursive calls. */
4339 && !cg_edge->recursive_p ()
4340 /* Avoid warnings during early inline pass. */
4341 && symtab->global_info_ready)
4343 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
4344 fn, _(cgraph_inline_failed_string (reason)));
4345 warning (OPT_Winline, "called from here");
4347 goto egress;
4349 fn = cg_edge->callee->decl;
4350 cg_edge->callee->get_body ();
4352 #ifdef ENABLE_CHECKING
4353 if (cg_edge->callee->decl != id->dst_node->decl)
4354 cg_edge->callee->verify ();
4355 #endif
4357 /* We will be inlining this callee. */
4358 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
4359 id->assign_stmts.create (0);
4361 /* Update the callers EH personality. */
4362 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
4363 DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
4364 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
4366 /* Split the block holding the GIMPLE_CALL. */
4367 e = split_block (bb, stmt);
4368 bb = e->src;
4369 return_block = e->dest;
4370 remove_edge (e);
4372 /* split_block splits after the statement; work around this by
4373 moving the call into the second block manually. Not pretty,
4374 but seems easier than doing the CFG manipulation by hand
4375 when the GIMPLE_CALL is in the last statement of BB. */
4376 stmt_gsi = gsi_last_bb (bb);
4377 gsi_remove (&stmt_gsi, false);
4379 /* If the GIMPLE_CALL was in the last statement of BB, it may have
4380 been the source of abnormal edges. In this case, schedule
4381 the removal of dead abnormal edges. */
4382 gsi = gsi_start_bb (return_block);
4383 if (gsi_end_p (gsi))
4385 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
4386 purge_dead_abnormal_edges = true;
4388 else
4390 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
4391 purge_dead_abnormal_edges = false;
4394 stmt_gsi = gsi_start_bb (return_block);
4396 /* Build a block containing code to initialize the arguments, the
4397 actual inline expansion of the body, and a label for the return
4398 statements within the function to jump to. The type of the
4399 statement expression is the return type of the function call.
4400 ??? If the call does not have an associated block then we will
4401 remap all callee blocks to NULL, effectively dropping most of
4402 its debug information. This should only happen for calls to
4403 artificial decls inserted by the compiler itself. We need to
4404 either link the inlined blocks into the caller block tree or
4405 not refer to them in any way to not break GC for locations. */
4406 if (gimple_block (stmt))
4408 id->block = make_node (BLOCK);
4409 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
4410 BLOCK_SOURCE_LOCATION (id->block) = LOCATION_LOCUS (input_location);
4411 prepend_lexical_block (gimple_block (stmt), id->block);
4414 /* Local declarations will be replaced by their equivalents in this
4415 map. */
4416 st = id->decl_map;
4417 id->decl_map = new hash_map<tree, tree>;
4418 dst = id->debug_map;
4419 id->debug_map = NULL;
4421 /* Record the function we are about to inline. */
4422 id->src_fn = fn;
4423 id->src_node = cg_edge->callee;
4424 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
4425 id->gimple_call = stmt;
4427 gcc_assert (!id->src_cfun->after_inlining);
4429 id->entry_bb = bb;
4430 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
4432 gimple_stmt_iterator si = gsi_last_bb (bb);
4433 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
4434 NOT_TAKEN),
4435 GSI_NEW_STMT);
4437 initialize_inlined_parameters (id, stmt, fn, bb);
4439 if (DECL_INITIAL (fn))
4441 if (gimple_block (stmt))
4443 tree *var;
4445 prepend_lexical_block (id->block,
4446 remap_blocks (DECL_INITIAL (fn), id));
4447 gcc_checking_assert (BLOCK_SUBBLOCKS (id->block)
4448 && (BLOCK_CHAIN (BLOCK_SUBBLOCKS (id->block))
4449 == NULL_TREE));
4450 /* Move vars for PARM_DECLs from DECL_INITIAL block to id->block,
4451 otherwise for DWARF DW_TAG_formal_parameter will not be children of
4452 DW_TAG_inlined_subroutine, but of a DW_TAG_lexical_block
4453 under it. The parameters can be then evaluated in the debugger,
4454 but don't show in backtraces. */
4455 for (var = &BLOCK_VARS (BLOCK_SUBBLOCKS (id->block)); *var; )
4456 if (TREE_CODE (DECL_ORIGIN (*var)) == PARM_DECL)
4458 tree v = *var;
4459 *var = TREE_CHAIN (v);
4460 TREE_CHAIN (v) = BLOCK_VARS (id->block);
4461 BLOCK_VARS (id->block) = v;
4463 else
4464 var = &TREE_CHAIN (*var);
4466 else
4467 remap_blocks_to_null (DECL_INITIAL (fn), id);
4470 /* Return statements in the function body will be replaced by jumps
4471 to the RET_LABEL. */
4472 gcc_assert (DECL_INITIAL (fn));
4473 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
4475 /* Find the LHS to which the result of this call is assigned. */
4476 return_slot = NULL;
4477 if (gimple_call_lhs (stmt))
4479 modify_dest = gimple_call_lhs (stmt);
4481 /* Remember where to copy returned bounds. */
4482 if (gimple_call_with_bounds_p (stmt)
4483 && TREE_CODE (modify_dest) == SSA_NAME)
4485 gimple retbnd = chkp_retbnd_call_by_val (modify_dest);
4486 if (retbnd)
4488 return_bounds = gimple_call_lhs (retbnd);
4489 /* If returned bounds are not used then just
4490 remove unused call. */
4491 if (!return_bounds)
4493 gimple_stmt_iterator iter = gsi_for_stmt (retbnd);
4494 gsi_remove (&iter, true);
4499 /* The function which we are inlining might not return a value,
4500 in which case we should issue a warning that the function
4501 does not return a value. In that case the optimizers will
4502 see that the variable to which the value is assigned was not
4503 initialized. We do not want to issue a warning about that
4504 uninitialized variable. */
4505 if (DECL_P (modify_dest))
4506 TREE_NO_WARNING (modify_dest) = 1;
4508 if (gimple_call_return_slot_opt_p (stmt))
4510 return_slot = modify_dest;
4511 modify_dest = NULL;
4514 else
4515 modify_dest = NULL;
4517 /* If we are inlining a call to the C++ operator new, we don't want
4518 to use type based alias analysis on the return value. Otherwise
4519 we may get confused if the compiler sees that the inlined new
4520 function returns a pointer which was just deleted. See bug
4521 33407. */
4522 if (DECL_IS_OPERATOR_NEW (fn))
4524 return_slot = NULL;
4525 modify_dest = NULL;
4528 /* Declare the return variable for the function. */
4529 use_retvar = declare_return_variable (id, return_slot, modify_dest,
4530 return_bounds, bb);
4532 /* Add local vars in this inlined callee to caller. */
4533 add_local_variables (id->src_cfun, cfun, id);
4535 if (dump_file && (dump_flags & TDF_DETAILS))
4537 fprintf (dump_file, "Inlining ");
4538 print_generic_expr (dump_file, id->src_fn, 0);
4539 fprintf (dump_file, " to ");
4540 print_generic_expr (dump_file, id->dst_fn, 0);
4541 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
4544 /* This is it. Duplicate the callee body. Assume callee is
4545 pre-gimplified. Note that we must not alter the caller
4546 function in any way before this point, as this CALL_EXPR may be
4547 a self-referential call; if we're calling ourselves, we need to
4548 duplicate our body before altering anything. */
4549 copy_body (id, cg_edge->callee->count,
4550 GCOV_COMPUTE_SCALE (cg_edge->frequency, CGRAPH_FREQ_BASE),
4551 bb, return_block, NULL);
4553 /* Reset the escaped solution. */
4554 if (cfun->gimple_df)
4555 pt_solution_reset (&cfun->gimple_df->escaped);
4557 /* Clean up. */
4558 if (id->debug_map)
4560 delete id->debug_map;
4561 id->debug_map = dst;
4563 delete id->decl_map;
4564 id->decl_map = st;
4566 /* Unlink the calls virtual operands before replacing it. */
4567 unlink_stmt_vdef (stmt);
4568 if (gimple_vdef (stmt)
4569 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
4570 release_ssa_name (gimple_vdef (stmt));
4572 /* If the inlined function returns a result that we care about,
4573 substitute the GIMPLE_CALL with an assignment of the return
4574 variable to the LHS of the call. That is, if STMT was
4575 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
4576 if (use_retvar && gimple_call_lhs (stmt))
4578 gimple old_stmt = stmt;
4579 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
4580 gsi_replace (&stmt_gsi, stmt, false);
4581 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
4583 /* Copy bounds if we copy structure with bounds. */
4584 if (chkp_function_instrumented_p (id->dst_fn)
4585 && !BOUNDED_P (use_retvar)
4586 && chkp_type_has_pointer (TREE_TYPE (use_retvar)))
4587 id->assign_stmts.safe_push (stmt);
4589 else
4591 /* Handle the case of inlining a function with no return
4592 statement, which causes the return value to become undefined. */
4593 if (gimple_call_lhs (stmt)
4594 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
4596 tree name = gimple_call_lhs (stmt);
4597 tree var = SSA_NAME_VAR (name);
4598 tree def = ssa_default_def (cfun, var);
4600 if (def)
4602 /* If the variable is used undefined, make this name
4603 undefined via a move. */
4604 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
4605 gsi_replace (&stmt_gsi, stmt, true);
4607 else
4609 /* Otherwise make this variable undefined. */
4610 gsi_remove (&stmt_gsi, true);
4611 set_ssa_default_def (cfun, var, name);
4612 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
4615 else
4616 gsi_remove (&stmt_gsi, true);
4619 /* Put returned bounds into the correct place if required. */
4620 if (return_bounds)
4622 gimple old_stmt = SSA_NAME_DEF_STMT (return_bounds);
4623 gimple new_stmt = gimple_build_assign (return_bounds, id->retbnd);
4624 gimple_stmt_iterator bnd_gsi = gsi_for_stmt (old_stmt);
4625 unlink_stmt_vdef (old_stmt);
4626 gsi_replace (&bnd_gsi, new_stmt, false);
4627 maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt);
4628 cgraph_update_edges_for_call_stmt (old_stmt,
4629 gimple_call_fndecl (old_stmt),
4630 new_stmt);
4633 if (purge_dead_abnormal_edges)
4635 gimple_purge_dead_eh_edges (return_block);
4636 gimple_purge_dead_abnormal_call_edges (return_block);
4639 /* If the value of the new expression is ignored, that's OK. We
4640 don't warn about this for CALL_EXPRs, so we shouldn't warn about
4641 the equivalent inlined version either. */
4642 if (is_gimple_assign (stmt))
4644 gcc_assert (gimple_assign_single_p (stmt)
4645 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
4646 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
4649 /* Copy bounds for all generated assigns that need it. */
4650 for (i = 0; i < id->assign_stmts.length (); i++)
4651 chkp_copy_bounds_for_assign (id->assign_stmts[i], cg_edge);
4652 id->assign_stmts.release ();
4654 /* Output the inlining info for this abstract function, since it has been
4655 inlined. If we don't do this now, we can lose the information about the
4656 variables in the function when the blocks get blown away as soon as we
4657 remove the cgraph node. */
4658 if (gimple_block (stmt))
4659 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
4661 /* Update callgraph if needed. */
4662 cg_edge->callee->remove ();
4664 id->block = NULL_TREE;
4665 successfully_inlined = TRUE;
4667 egress:
4668 input_location = saved_location;
4669 return successfully_inlined;
4672 /* Expand call statements reachable from STMT_P.
4673 We can only have CALL_EXPRs as the "toplevel" tree code or nested
4674 in a MODIFY_EXPR. */
4676 static bool
4677 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
4679 gimple_stmt_iterator gsi;
4681 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4683 gimple stmt = gsi_stmt (gsi);
4685 if (is_gimple_call (stmt)
4686 && !gimple_call_internal_p (stmt)
4687 && expand_call_inline (bb, stmt, id))
4688 return true;
4691 return false;
4695 /* Walk all basic blocks created after FIRST and try to fold every statement
4696 in the STATEMENTS pointer set. */
4698 static void
4699 fold_marked_statements (int first, hash_set<gimple> *statements)
4701 for (; first < n_basic_blocks_for_fn (cfun); first++)
4702 if (BASIC_BLOCK_FOR_FN (cfun, first))
4704 gimple_stmt_iterator gsi;
4706 for (gsi = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
4707 !gsi_end_p (gsi);
4708 gsi_next (&gsi))
4709 if (statements->contains (gsi_stmt (gsi)))
4711 gimple old_stmt = gsi_stmt (gsi);
4712 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
4714 if (old_decl && DECL_BUILT_IN (old_decl))
4716 /* Folding builtins can create multiple instructions,
4717 we need to look at all of them. */
4718 gimple_stmt_iterator i2 = gsi;
4719 gsi_prev (&i2);
4720 if (fold_stmt (&gsi))
4722 gimple new_stmt;
4723 /* If a builtin at the end of a bb folded into nothing,
4724 the following loop won't work. */
4725 if (gsi_end_p (gsi))
4727 cgraph_update_edges_for_call_stmt (old_stmt,
4728 old_decl, NULL);
4729 break;
4731 if (gsi_end_p (i2))
4732 i2 = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
4733 else
4734 gsi_next (&i2);
4735 while (1)
4737 new_stmt = gsi_stmt (i2);
4738 update_stmt (new_stmt);
4739 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4740 new_stmt);
4742 if (new_stmt == gsi_stmt (gsi))
4744 /* It is okay to check only for the very last
4745 of these statements. If it is a throwing
4746 statement nothing will change. If it isn't
4747 this can remove EH edges. If that weren't
4748 correct then because some intermediate stmts
4749 throw, but not the last one. That would mean
4750 we'd have to split the block, which we can't
4751 here and we'd loose anyway. And as builtins
4752 probably never throw, this all
4753 is mood anyway. */
4754 if (maybe_clean_or_replace_eh_stmt (old_stmt,
4755 new_stmt))
4756 gimple_purge_dead_eh_edges (
4757 BASIC_BLOCK_FOR_FN (cfun, first));
4758 break;
4760 gsi_next (&i2);
4764 else if (fold_stmt (&gsi))
4766 /* Re-read the statement from GSI as fold_stmt() may
4767 have changed it. */
4768 gimple new_stmt = gsi_stmt (gsi);
4769 update_stmt (new_stmt);
4771 if (is_gimple_call (old_stmt)
4772 || is_gimple_call (new_stmt))
4773 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4774 new_stmt);
4776 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
4777 gimple_purge_dead_eh_edges (BASIC_BLOCK_FOR_FN (cfun,
4778 first));
4784 /* Expand calls to inline functions in the body of FN. */
4786 unsigned int
4787 optimize_inline_calls (tree fn)
4789 copy_body_data id;
4790 basic_block bb;
4791 int last = n_basic_blocks_for_fn (cfun);
4792 bool inlined_p = false;
4794 /* Clear out ID. */
4795 memset (&id, 0, sizeof (id));
4797 id.src_node = id.dst_node = cgraph_node::get (fn);
4798 gcc_assert (id.dst_node->definition);
4799 id.dst_fn = fn;
4800 /* Or any functions that aren't finished yet. */
4801 if (current_function_decl)
4802 id.dst_fn = current_function_decl;
4804 id.copy_decl = copy_decl_maybe_to_var;
4805 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4806 id.transform_new_cfg = false;
4807 id.transform_return_to_modify = true;
4808 id.transform_parameter = true;
4809 id.transform_lang_insert_block = NULL;
4810 id.statements_to_fold = new hash_set<gimple>;
4812 push_gimplify_context ();
4814 /* We make no attempts to keep dominance info up-to-date. */
4815 free_dominance_info (CDI_DOMINATORS);
4816 free_dominance_info (CDI_POST_DOMINATORS);
4818 /* Register specific gimple functions. */
4819 gimple_register_cfg_hooks ();
4821 /* Reach the trees by walking over the CFG, and note the
4822 enclosing basic-blocks in the call edges. */
4823 /* We walk the blocks going forward, because inlined function bodies
4824 will split id->current_basic_block, and the new blocks will
4825 follow it; we'll trudge through them, processing their CALL_EXPRs
4826 along the way. */
4827 FOR_EACH_BB_FN (bb, cfun)
4828 inlined_p |= gimple_expand_calls_inline (bb, &id);
4830 pop_gimplify_context (NULL);
4832 #ifdef ENABLE_CHECKING
4834 struct cgraph_edge *e;
4836 id.dst_node->verify ();
4838 /* Double check that we inlined everything we are supposed to inline. */
4839 for (e = id.dst_node->callees; e; e = e->next_callee)
4840 gcc_assert (e->inline_failed);
4842 #endif
4844 /* Fold queued statements. */
4845 fold_marked_statements (last, id.statements_to_fold);
4846 delete id.statements_to_fold;
4848 gcc_assert (!id.debug_stmts.exists ());
4850 /* If we didn't inline into the function there is nothing to do. */
4851 if (!inlined_p)
4852 return 0;
4854 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4855 number_blocks (fn);
4857 delete_unreachable_blocks_update_callgraph (&id);
4858 #ifdef ENABLE_CHECKING
4859 id.dst_node->verify ();
4860 #endif
4862 /* It would be nice to check SSA/CFG/statement consistency here, but it is
4863 not possible yet - the IPA passes might make various functions to not
4864 throw and they don't care to proactively update local EH info. This is
4865 done later in fixup_cfg pass that also execute the verification. */
4866 return (TODO_update_ssa
4867 | TODO_cleanup_cfg
4868 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
4869 | (gimple_in_ssa_p (cfun) ? TODO_update_address_taken : 0)
4870 | (profile_status_for_fn (cfun) != PROFILE_ABSENT
4871 ? TODO_rebuild_frequencies : 0));
4874 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4876 tree
4877 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4879 enum tree_code code = TREE_CODE (*tp);
4880 enum tree_code_class cl = TREE_CODE_CLASS (code);
4882 /* We make copies of most nodes. */
4883 if (IS_EXPR_CODE_CLASS (cl)
4884 || code == TREE_LIST
4885 || code == TREE_VEC
4886 || code == TYPE_DECL
4887 || code == OMP_CLAUSE)
4889 /* Because the chain gets clobbered when we make a copy, we save it
4890 here. */
4891 tree chain = NULL_TREE, new_tree;
4893 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
4894 chain = TREE_CHAIN (*tp);
4896 /* Copy the node. */
4897 new_tree = copy_node (*tp);
4899 *tp = new_tree;
4901 /* Now, restore the chain, if appropriate. That will cause
4902 walk_tree to walk into the chain as well. */
4903 if (code == PARM_DECL
4904 || code == TREE_LIST
4905 || code == OMP_CLAUSE)
4906 TREE_CHAIN (*tp) = chain;
4908 /* For now, we don't update BLOCKs when we make copies. So, we
4909 have to nullify all BIND_EXPRs. */
4910 if (TREE_CODE (*tp) == BIND_EXPR)
4911 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
4913 else if (code == CONSTRUCTOR)
4915 /* CONSTRUCTOR nodes need special handling because
4916 we need to duplicate the vector of elements. */
4917 tree new_tree;
4919 new_tree = copy_node (*tp);
4920 CONSTRUCTOR_ELTS (new_tree) = vec_safe_copy (CONSTRUCTOR_ELTS (*tp));
4921 *tp = new_tree;
4923 else if (code == STATEMENT_LIST)
4924 /* We used to just abort on STATEMENT_LIST, but we can run into them
4925 with statement-expressions (c++/40975). */
4926 copy_statement_list (tp);
4927 else if (TREE_CODE_CLASS (code) == tcc_type)
4928 *walk_subtrees = 0;
4929 else if (TREE_CODE_CLASS (code) == tcc_declaration)
4930 *walk_subtrees = 0;
4931 else if (TREE_CODE_CLASS (code) == tcc_constant)
4932 *walk_subtrees = 0;
4933 return NULL_TREE;
4936 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
4937 information indicating to what new SAVE_EXPR this one should be mapped,
4938 use that one. Otherwise, create a new node and enter it in ST. FN is
4939 the function into which the copy will be placed. */
4941 static void
4942 remap_save_expr (tree *tp, hash_map<tree, tree> *st, int *walk_subtrees)
4944 tree *n;
4945 tree t;
4947 /* See if we already encountered this SAVE_EXPR. */
4948 n = st->get (*tp);
4950 /* If we didn't already remap this SAVE_EXPR, do so now. */
4951 if (!n)
4953 t = copy_node (*tp);
4955 /* Remember this SAVE_EXPR. */
4956 st->put (*tp, t);
4957 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
4958 st->put (t, t);
4960 else
4962 /* We've already walked into this SAVE_EXPR; don't do it again. */
4963 *walk_subtrees = 0;
4964 t = *n;
4967 /* Replace this SAVE_EXPR with the copy. */
4968 *tp = t;
4971 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
4972 label, copies the declaration and enters it in the splay_tree in DATA (which
4973 is really a 'copy_body_data *'. */
4975 static tree
4976 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
4977 bool *handled_ops_p ATTRIBUTE_UNUSED,
4978 struct walk_stmt_info *wi)
4980 copy_body_data *id = (copy_body_data *) wi->info;
4981 gimple stmt = gsi_stmt (*gsip);
4983 if (gimple_code (stmt) == GIMPLE_LABEL)
4985 tree decl = gimple_label_label (stmt);
4987 /* Copy the decl and remember the copy. */
4988 insert_decl_map (id, decl, id->copy_decl (decl, id));
4991 return NULL_TREE;
4995 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4996 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4997 remaps all local declarations to appropriate replacements in gimple
4998 operands. */
5000 static tree
5001 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
5003 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
5004 copy_body_data *id = (copy_body_data *) wi->info;
5005 hash_map<tree, tree> *st = id->decl_map;
5006 tree *n;
5007 tree expr = *tp;
5009 /* Only a local declaration (variable or label). */
5010 if ((TREE_CODE (expr) == VAR_DECL
5011 && !TREE_STATIC (expr))
5012 || TREE_CODE (expr) == LABEL_DECL)
5014 /* Lookup the declaration. */
5015 n = st->get (expr);
5017 /* If it's there, remap it. */
5018 if (n)
5019 *tp = *n;
5020 *walk_subtrees = 0;
5022 else if (TREE_CODE (expr) == STATEMENT_LIST
5023 || TREE_CODE (expr) == BIND_EXPR
5024 || TREE_CODE (expr) == SAVE_EXPR)
5025 gcc_unreachable ();
5026 else if (TREE_CODE (expr) == TARGET_EXPR)
5028 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
5029 It's OK for this to happen if it was part of a subtree that
5030 isn't immediately expanded, such as operand 2 of another
5031 TARGET_EXPR. */
5032 if (!TREE_OPERAND (expr, 1))
5034 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
5035 TREE_OPERAND (expr, 3) = NULL_TREE;
5039 /* Keep iterating. */
5040 return NULL_TREE;
5044 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
5045 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
5046 remaps all local declarations to appropriate replacements in gimple
5047 statements. */
5049 static tree
5050 replace_locals_stmt (gimple_stmt_iterator *gsip,
5051 bool *handled_ops_p ATTRIBUTE_UNUSED,
5052 struct walk_stmt_info *wi)
5054 copy_body_data *id = (copy_body_data *) wi->info;
5055 gimple stmt = gsi_stmt (*gsip);
5057 if (gimple_code (stmt) == GIMPLE_BIND)
5059 tree block = gimple_bind_block (stmt);
5061 if (block)
5063 remap_block (&block, id);
5064 gimple_bind_set_block (stmt, block);
5067 /* This will remap a lot of the same decls again, but this should be
5068 harmless. */
5069 if (gimple_bind_vars (stmt))
5070 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt),
5071 NULL, id));
5074 /* Keep iterating. */
5075 return NULL_TREE;
5079 /* Copies everything in SEQ and replaces variables and labels local to
5080 current_function_decl. */
5082 gimple_seq
5083 copy_gimple_seq_and_replace_locals (gimple_seq seq)
5085 copy_body_data id;
5086 struct walk_stmt_info wi;
5087 gimple_seq copy;
5089 /* There's nothing to do for NULL_TREE. */
5090 if (seq == NULL)
5091 return seq;
5093 /* Set up ID. */
5094 memset (&id, 0, sizeof (id));
5095 id.src_fn = current_function_decl;
5096 id.dst_fn = current_function_decl;
5097 id.decl_map = new hash_map<tree, tree>;
5098 id.debug_map = NULL;
5100 id.copy_decl = copy_decl_no_change;
5101 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5102 id.transform_new_cfg = false;
5103 id.transform_return_to_modify = false;
5104 id.transform_parameter = false;
5105 id.transform_lang_insert_block = NULL;
5107 /* Walk the tree once to find local labels. */
5108 memset (&wi, 0, sizeof (wi));
5109 hash_set<tree> visited;
5110 wi.info = &id;
5111 wi.pset = &visited;
5112 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
5114 copy = gimple_seq_copy (seq);
5116 /* Walk the copy, remapping decls. */
5117 memset (&wi, 0, sizeof (wi));
5118 wi.info = &id;
5119 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
5121 /* Clean up. */
5122 delete id.decl_map;
5123 if (id.debug_map)
5124 delete id.debug_map;
5126 return copy;
5130 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
5132 static tree
5133 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
5135 if (*tp == data)
5136 return (tree) data;
5137 else
5138 return NULL;
5141 DEBUG_FUNCTION bool
5142 debug_find_tree (tree top, tree search)
5144 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
5148 /* Declare the variables created by the inliner. Add all the variables in
5149 VARS to BIND_EXPR. */
5151 static void
5152 declare_inline_vars (tree block, tree vars)
5154 tree t;
5155 for (t = vars; t; t = DECL_CHAIN (t))
5157 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
5158 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
5159 add_local_decl (cfun, t);
5162 if (block)
5163 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
5166 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
5167 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
5168 VAR_DECL translation. */
5170 static tree
5171 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
5173 /* Don't generate debug information for the copy if we wouldn't have
5174 generated it for the copy either. */
5175 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
5176 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
5178 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
5179 declaration inspired this copy. */
5180 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
5182 /* The new variable/label has no RTL, yet. */
5183 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
5184 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
5185 SET_DECL_RTL (copy, 0);
5187 /* These args would always appear unused, if not for this. */
5188 TREE_USED (copy) = 1;
5190 /* Set the context for the new declaration. */
5191 if (!DECL_CONTEXT (decl))
5192 /* Globals stay global. */
5194 else if (DECL_CONTEXT (decl) != id->src_fn)
5195 /* Things that weren't in the scope of the function we're inlining
5196 from aren't in the scope we're inlining to, either. */
5198 else if (TREE_STATIC (decl))
5199 /* Function-scoped static variables should stay in the original
5200 function. */
5202 else
5203 /* Ordinary automatic local variables are now in the scope of the
5204 new function. */
5205 DECL_CONTEXT (copy) = id->dst_fn;
5207 return copy;
5210 static tree
5211 copy_decl_to_var (tree decl, copy_body_data *id)
5213 tree copy, type;
5215 gcc_assert (TREE_CODE (decl) == PARM_DECL
5216 || TREE_CODE (decl) == RESULT_DECL);
5218 type = TREE_TYPE (decl);
5220 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
5221 VAR_DECL, DECL_NAME (decl), type);
5222 if (DECL_PT_UID_SET_P (decl))
5223 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
5224 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
5225 TREE_READONLY (copy) = TREE_READONLY (decl);
5226 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
5227 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
5229 return copy_decl_for_dup_finish (id, decl, copy);
5232 /* Like copy_decl_to_var, but create a return slot object instead of a
5233 pointer variable for return by invisible reference. */
5235 static tree
5236 copy_result_decl_to_var (tree decl, copy_body_data *id)
5238 tree copy, type;
5240 gcc_assert (TREE_CODE (decl) == PARM_DECL
5241 || TREE_CODE (decl) == RESULT_DECL);
5243 type = TREE_TYPE (decl);
5244 if (DECL_BY_REFERENCE (decl))
5245 type = TREE_TYPE (type);
5247 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
5248 VAR_DECL, DECL_NAME (decl), type);
5249 if (DECL_PT_UID_SET_P (decl))
5250 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
5251 TREE_READONLY (copy) = TREE_READONLY (decl);
5252 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
5253 if (!DECL_BY_REFERENCE (decl))
5255 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
5256 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
5259 return copy_decl_for_dup_finish (id, decl, copy);
5262 tree
5263 copy_decl_no_change (tree decl, copy_body_data *id)
5265 tree copy;
5267 copy = copy_node (decl);
5269 /* The COPY is not abstract; it will be generated in DST_FN. */
5270 DECL_ABSTRACT_P (copy) = false;
5271 lang_hooks.dup_lang_specific_decl (copy);
5273 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
5274 been taken; it's for internal bookkeeping in expand_goto_internal. */
5275 if (TREE_CODE (copy) == LABEL_DECL)
5277 TREE_ADDRESSABLE (copy) = 0;
5278 LABEL_DECL_UID (copy) = -1;
5281 return copy_decl_for_dup_finish (id, decl, copy);
5284 static tree
5285 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
5287 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
5288 return copy_decl_to_var (decl, id);
5289 else
5290 return copy_decl_no_change (decl, id);
5293 /* Return a copy of the function's argument tree. */
5294 static tree
5295 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
5296 bitmap args_to_skip, tree *vars)
5298 tree arg, *parg;
5299 tree new_parm = NULL;
5300 int i = 0;
5302 parg = &new_parm;
5304 for (arg = orig_parm; arg; arg = DECL_CHAIN (arg), i++)
5305 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
5307 tree new_tree = remap_decl (arg, id);
5308 if (TREE_CODE (new_tree) != PARM_DECL)
5309 new_tree = id->copy_decl (arg, id);
5310 lang_hooks.dup_lang_specific_decl (new_tree);
5311 *parg = new_tree;
5312 parg = &DECL_CHAIN (new_tree);
5314 else if (!id->decl_map->get (arg))
5316 /* Make an equivalent VAR_DECL. If the argument was used
5317 as temporary variable later in function, the uses will be
5318 replaced by local variable. */
5319 tree var = copy_decl_to_var (arg, id);
5320 insert_decl_map (id, arg, var);
5321 /* Declare this new variable. */
5322 DECL_CHAIN (var) = *vars;
5323 *vars = var;
5325 return new_parm;
5328 /* Return a copy of the function's static chain. */
5329 static tree
5330 copy_static_chain (tree static_chain, copy_body_data * id)
5332 tree *chain_copy, *pvar;
5334 chain_copy = &static_chain;
5335 for (pvar = chain_copy; *pvar; pvar = &DECL_CHAIN (*pvar))
5337 tree new_tree = remap_decl (*pvar, id);
5338 lang_hooks.dup_lang_specific_decl (new_tree);
5339 DECL_CHAIN (new_tree) = DECL_CHAIN (*pvar);
5340 *pvar = new_tree;
5342 return static_chain;
5345 /* Return true if the function is allowed to be versioned.
5346 This is a guard for the versioning functionality. */
5348 bool
5349 tree_versionable_function_p (tree fndecl)
5351 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
5352 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
5355 /* Delete all unreachable basic blocks and update callgraph.
5356 Doing so is somewhat nontrivial because we need to update all clones and
5357 remove inline function that become unreachable. */
5359 static bool
5360 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
5362 bool changed = false;
5363 basic_block b, next_bb;
5365 find_unreachable_blocks ();
5367 /* Delete all unreachable basic blocks. */
5369 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
5370 != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
5372 next_bb = b->next_bb;
5374 if (!(b->flags & BB_REACHABLE))
5376 gimple_stmt_iterator bsi;
5378 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
5380 struct cgraph_edge *e;
5381 struct cgraph_node *node;
5383 id->dst_node->remove_stmt_references (gsi_stmt (bsi));
5385 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
5386 &&(e = id->dst_node->get_edge (gsi_stmt (bsi))) != NULL)
5388 if (!e->inline_failed)
5389 e->callee->remove_symbol_and_inline_clones (id->dst_node);
5390 else
5391 e->remove ();
5393 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
5394 && id->dst_node->clones)
5395 for (node = id->dst_node->clones; node != id->dst_node;)
5397 node->remove_stmt_references (gsi_stmt (bsi));
5398 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
5399 && (e = node->get_edge (gsi_stmt (bsi))) != NULL)
5401 if (!e->inline_failed)
5402 e->callee->remove_symbol_and_inline_clones (id->dst_node);
5403 else
5404 e->remove ();
5407 if (node->clones)
5408 node = node->clones;
5409 else if (node->next_sibling_clone)
5410 node = node->next_sibling_clone;
5411 else
5413 while (node != id->dst_node && !node->next_sibling_clone)
5414 node = node->clone_of;
5415 if (node != id->dst_node)
5416 node = node->next_sibling_clone;
5420 delete_basic_block (b);
5421 changed = true;
5425 return changed;
5428 /* Update clone info after duplication. */
5430 static void
5431 update_clone_info (copy_body_data * id)
5433 struct cgraph_node *node;
5434 if (!id->dst_node->clones)
5435 return;
5436 for (node = id->dst_node->clones; node != id->dst_node;)
5438 /* First update replace maps to match the new body. */
5439 if (node->clone.tree_map)
5441 unsigned int i;
5442 for (i = 0; i < vec_safe_length (node->clone.tree_map); i++)
5444 struct ipa_replace_map *replace_info;
5445 replace_info = (*node->clone.tree_map)[i];
5446 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
5447 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
5450 if (node->clones)
5451 node = node->clones;
5452 else if (node->next_sibling_clone)
5453 node = node->next_sibling_clone;
5454 else
5456 while (node != id->dst_node && !node->next_sibling_clone)
5457 node = node->clone_of;
5458 if (node != id->dst_node)
5459 node = node->next_sibling_clone;
5464 /* Create a copy of a function's tree.
5465 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
5466 of the original function and the new copied function
5467 respectively. In case we want to replace a DECL
5468 tree with another tree while duplicating the function's
5469 body, TREE_MAP represents the mapping between these
5470 trees. If UPDATE_CLONES is set, the call_stmt fields
5471 of edges of clones of the function will be updated.
5473 If non-NULL ARGS_TO_SKIP determine function parameters to remove
5474 from new version.
5475 If SKIP_RETURN is true, the new version will return void.
5476 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
5477 If non_NULL NEW_ENTRY determine new entry BB of the clone.
5479 void
5480 tree_function_versioning (tree old_decl, tree new_decl,
5481 vec<ipa_replace_map *, va_gc> *tree_map,
5482 bool update_clones, bitmap args_to_skip,
5483 bool skip_return, bitmap blocks_to_copy,
5484 basic_block new_entry)
5486 struct cgraph_node *old_version_node;
5487 struct cgraph_node *new_version_node;
5488 copy_body_data id;
5489 tree p;
5490 unsigned i;
5491 struct ipa_replace_map *replace_info;
5492 basic_block old_entry_block, bb;
5493 auto_vec<gimple, 10> init_stmts;
5494 tree vars = NULL_TREE;
5496 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
5497 && TREE_CODE (new_decl) == FUNCTION_DECL);
5498 DECL_POSSIBLY_INLINED (old_decl) = 1;
5500 old_version_node = cgraph_node::get (old_decl);
5501 gcc_checking_assert (old_version_node);
5502 new_version_node = cgraph_node::get (new_decl);
5503 gcc_checking_assert (new_version_node);
5505 /* Copy over debug args. */
5506 if (DECL_HAS_DEBUG_ARGS_P (old_decl))
5508 vec<tree, va_gc> **new_debug_args, **old_debug_args;
5509 gcc_checking_assert (decl_debug_args_lookup (new_decl) == NULL);
5510 DECL_HAS_DEBUG_ARGS_P (new_decl) = 0;
5511 old_debug_args = decl_debug_args_lookup (old_decl);
5512 if (old_debug_args)
5514 new_debug_args = decl_debug_args_insert (new_decl);
5515 *new_debug_args = vec_safe_copy (*old_debug_args);
5519 /* Output the inlining info for this abstract function, since it has been
5520 inlined. If we don't do this now, we can lose the information about the
5521 variables in the function when the blocks get blown away as soon as we
5522 remove the cgraph node. */
5523 (*debug_hooks->outlining_inline_function) (old_decl);
5525 DECL_ARTIFICIAL (new_decl) = 1;
5526 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
5527 if (DECL_ORIGIN (old_decl) == old_decl)
5528 old_version_node->used_as_abstract_origin = true;
5529 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
5531 /* Prepare the data structures for the tree copy. */
5532 memset (&id, 0, sizeof (id));
5534 /* Generate a new name for the new version. */
5535 id.statements_to_fold = new hash_set<gimple>;
5537 id.decl_map = new hash_map<tree, tree>;
5538 id.debug_map = NULL;
5539 id.src_fn = old_decl;
5540 id.dst_fn = new_decl;
5541 id.src_node = old_version_node;
5542 id.dst_node = new_version_node;
5543 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
5544 id.blocks_to_copy = blocks_to_copy;
5546 id.copy_decl = copy_decl_no_change;
5547 id.transform_call_graph_edges
5548 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
5549 id.transform_new_cfg = true;
5550 id.transform_return_to_modify = false;
5551 id.transform_parameter = false;
5552 id.transform_lang_insert_block = NULL;
5554 old_entry_block = ENTRY_BLOCK_PTR_FOR_FN
5555 (DECL_STRUCT_FUNCTION (old_decl));
5556 DECL_RESULT (new_decl) = DECL_RESULT (old_decl);
5557 DECL_ARGUMENTS (new_decl) = DECL_ARGUMENTS (old_decl);
5558 initialize_cfun (new_decl, old_decl,
5559 old_entry_block->count);
5560 if (DECL_STRUCT_FUNCTION (new_decl)->gimple_df)
5561 DECL_STRUCT_FUNCTION (new_decl)->gimple_df->ipa_pta
5562 = id.src_cfun->gimple_df->ipa_pta;
5564 /* Copy the function's static chain. */
5565 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
5566 if (p)
5567 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
5568 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
5569 &id);
5571 /* If there's a tree_map, prepare for substitution. */
5572 if (tree_map)
5573 for (i = 0; i < tree_map->length (); i++)
5575 gimple init;
5576 replace_info = (*tree_map)[i];
5577 if (replace_info->replace_p)
5579 if (!replace_info->old_tree)
5581 int i = replace_info->parm_num;
5582 tree parm;
5583 tree req_type;
5585 for (parm = DECL_ARGUMENTS (old_decl); i; parm = DECL_CHAIN (parm))
5586 i --;
5587 replace_info->old_tree = parm;
5588 req_type = TREE_TYPE (parm);
5589 if (!useless_type_conversion_p (req_type, TREE_TYPE (replace_info->new_tree)))
5591 if (fold_convertible_p (req_type, replace_info->new_tree))
5592 replace_info->new_tree = fold_build1 (NOP_EXPR, req_type, replace_info->new_tree);
5593 else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (replace_info->new_tree)))
5594 replace_info->new_tree = fold_build1 (VIEW_CONVERT_EXPR, req_type, replace_info->new_tree);
5595 else
5597 if (dump_file)
5599 fprintf (dump_file, " const ");
5600 print_generic_expr (dump_file, replace_info->new_tree, 0);
5601 fprintf (dump_file, " can't be converted to param ");
5602 print_generic_expr (dump_file, parm, 0);
5603 fprintf (dump_file, "\n");
5605 replace_info->old_tree = NULL;
5609 else
5610 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
5611 if (replace_info->old_tree)
5613 init = setup_one_parameter (&id, replace_info->old_tree,
5614 replace_info->new_tree, id.src_fn,
5615 NULL,
5616 &vars);
5617 if (init)
5618 init_stmts.safe_push (init);
5622 /* Copy the function's arguments. */
5623 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
5624 DECL_ARGUMENTS (new_decl) =
5625 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
5626 args_to_skip, &vars);
5628 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
5629 BLOCK_SUPERCONTEXT (DECL_INITIAL (new_decl)) = new_decl;
5631 declare_inline_vars (DECL_INITIAL (new_decl), vars);
5633 if (!vec_safe_is_empty (DECL_STRUCT_FUNCTION (old_decl)->local_decls))
5634 /* Add local vars. */
5635 add_local_variables (DECL_STRUCT_FUNCTION (old_decl), cfun, &id);
5637 if (DECL_RESULT (old_decl) == NULL_TREE)
5639 else if (skip_return && !VOID_TYPE_P (TREE_TYPE (DECL_RESULT (old_decl))))
5641 DECL_RESULT (new_decl)
5642 = build_decl (DECL_SOURCE_LOCATION (DECL_RESULT (old_decl)),
5643 RESULT_DECL, NULL_TREE, void_type_node);
5644 DECL_CONTEXT (DECL_RESULT (new_decl)) = new_decl;
5645 cfun->returns_struct = 0;
5646 cfun->returns_pcc_struct = 0;
5648 else
5650 tree old_name;
5651 DECL_RESULT (new_decl) = remap_decl (DECL_RESULT (old_decl), &id);
5652 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
5653 if (gimple_in_ssa_p (id.src_cfun)
5654 && DECL_BY_REFERENCE (DECL_RESULT (old_decl))
5655 && (old_name = ssa_default_def (id.src_cfun, DECL_RESULT (old_decl))))
5657 tree new_name = make_ssa_name (DECL_RESULT (new_decl), NULL);
5658 insert_decl_map (&id, old_name, new_name);
5659 SSA_NAME_DEF_STMT (new_name) = gimple_build_nop ();
5660 set_ssa_default_def (cfun, DECL_RESULT (new_decl), new_name);
5664 /* Set up the destination functions loop tree. */
5665 if (loops_for_fn (DECL_STRUCT_FUNCTION (old_decl)) != NULL)
5667 cfun->curr_properties &= ~PROP_loops;
5668 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5669 cfun->curr_properties |= PROP_loops;
5672 /* Copy the Function's body. */
5673 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
5674 ENTRY_BLOCK_PTR_FOR_FN (cfun), EXIT_BLOCK_PTR_FOR_FN (cfun),
5675 new_entry);
5677 /* Renumber the lexical scoping (non-code) blocks consecutively. */
5678 number_blocks (new_decl);
5680 /* We want to create the BB unconditionally, so that the addition of
5681 debug stmts doesn't affect BB count, which may in the end cause
5682 codegen differences. */
5683 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5684 while (init_stmts.length ())
5685 insert_init_stmt (&id, bb, init_stmts.pop ());
5686 update_clone_info (&id);
5688 /* Remap the nonlocal_goto_save_area, if any. */
5689 if (cfun->nonlocal_goto_save_area)
5691 struct walk_stmt_info wi;
5693 memset (&wi, 0, sizeof (wi));
5694 wi.info = &id;
5695 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
5698 /* Clean up. */
5699 delete id.decl_map;
5700 if (id.debug_map)
5701 delete id.debug_map;
5702 free_dominance_info (CDI_DOMINATORS);
5703 free_dominance_info (CDI_POST_DOMINATORS);
5705 fold_marked_statements (0, id.statements_to_fold);
5706 delete id.statements_to_fold;
5707 fold_cond_expr_cond ();
5708 delete_unreachable_blocks_update_callgraph (&id);
5709 if (id.dst_node->definition)
5710 cgraph_edge::rebuild_references ();
5711 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
5713 calculate_dominance_info (CDI_DOMINATORS);
5714 fix_loop_structure (NULL);
5716 update_ssa (TODO_update_ssa);
5718 /* After partial cloning we need to rescale frequencies, so they are
5719 within proper range in the cloned function. */
5720 if (new_entry)
5722 struct cgraph_edge *e;
5723 rebuild_frequencies ();
5725 new_version_node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5726 for (e = new_version_node->callees; e; e = e->next_callee)
5728 basic_block bb = gimple_bb (e->call_stmt);
5729 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5730 bb);
5731 e->count = bb->count;
5733 for (e = new_version_node->indirect_calls; e; e = e->next_callee)
5735 basic_block bb = gimple_bb (e->call_stmt);
5736 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5737 bb);
5738 e->count = bb->count;
5742 free_dominance_info (CDI_DOMINATORS);
5743 free_dominance_info (CDI_POST_DOMINATORS);
5745 gcc_assert (!id.debug_stmts.exists ());
5746 pop_cfun ();
5747 return;
5750 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
5751 the callee and return the inlined body on success. */
5753 tree
5754 maybe_inline_call_in_expr (tree exp)
5756 tree fn = get_callee_fndecl (exp);
5758 /* We can only try to inline "const" functions. */
5759 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
5761 call_expr_arg_iterator iter;
5762 copy_body_data id;
5763 tree param, arg, t;
5764 hash_map<tree, tree> decl_map;
5766 /* Remap the parameters. */
5767 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
5768 param;
5769 param = DECL_CHAIN (param), arg = next_call_expr_arg (&iter))
5770 decl_map.put (param, arg);
5772 memset (&id, 0, sizeof (id));
5773 id.src_fn = fn;
5774 id.dst_fn = current_function_decl;
5775 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
5776 id.decl_map = &decl_map;
5778 id.copy_decl = copy_decl_no_change;
5779 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5780 id.transform_new_cfg = false;
5781 id.transform_return_to_modify = true;
5782 id.transform_parameter = true;
5783 id.transform_lang_insert_block = NULL;
5785 /* Make sure not to unshare trees behind the front-end's back
5786 since front-end specific mechanisms may rely on sharing. */
5787 id.regimplify = false;
5788 id.do_not_unshare = true;
5790 /* We're not inside any EH region. */
5791 id.eh_lp_nr = 0;
5793 t = copy_tree_body (&id);
5795 /* We can only return something suitable for use in a GENERIC
5796 expression tree. */
5797 if (TREE_CODE (t) == MODIFY_EXPR)
5798 return TREE_OPERAND (t, 1);
5801 return NULL_TREE;
5804 /* Duplicate a type, fields and all. */
5806 tree
5807 build_duplicate_type (tree type)
5809 struct copy_body_data id;
5811 memset (&id, 0, sizeof (id));
5812 id.src_fn = current_function_decl;
5813 id.dst_fn = current_function_decl;
5814 id.src_cfun = cfun;
5815 id.decl_map = new hash_map<tree, tree>;
5816 id.debug_map = NULL;
5817 id.copy_decl = copy_decl_no_change;
5819 type = remap_type_1 (type, &id);
5821 delete id.decl_map;
5822 if (id.debug_map)
5823 delete id.debug_map;
5825 TYPE_CANONICAL (type) = type;
5827 return type;