2014-12-18 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / tree-inline.c
blob59f2dab8d0866f505b1633f7a41a10942d5a61e0
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));
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);
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 (gbind *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 dependence CLIQUE from the source to the destination function
853 as specified in ID. */
855 static unsigned short
856 remap_dependence_clique (copy_body_data *id, unsigned short clique)
858 if (clique == 0)
859 return 0;
860 if (!id->dependence_map)
861 id->dependence_map
862 = new hash_map<unsigned short, unsigned short, dependence_hasher>;
863 bool existed;
864 unsigned short &newc = id->dependence_map->get_or_insert (clique, &existed);
865 if (!existed)
866 newc = ++cfun->last_clique;
867 return newc;
870 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
871 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
872 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
873 recursing into the children nodes of *TP. */
875 static tree
876 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
878 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
879 copy_body_data *id = (copy_body_data *) wi_p->info;
880 tree fn = id->src_fn;
882 if (TREE_CODE (*tp) == SSA_NAME)
884 *tp = remap_ssa_name (*tp, id);
885 *walk_subtrees = 0;
886 return NULL;
888 else if (auto_var_in_fn_p (*tp, fn))
890 /* Local variables and labels need to be replaced by equivalent
891 variables. We don't want to copy static variables; there's
892 only one of those, no matter how many times we inline the
893 containing function. Similarly for globals from an outer
894 function. */
895 tree new_decl;
897 /* Remap the declaration. */
898 new_decl = remap_decl (*tp, id);
899 gcc_assert (new_decl);
900 /* Replace this variable with the copy. */
901 STRIP_TYPE_NOPS (new_decl);
902 /* ??? The C++ frontend uses void * pointer zero to initialize
903 any other type. This confuses the middle-end type verification.
904 As cloned bodies do not go through gimplification again the fixup
905 there doesn't trigger. */
906 if (TREE_CODE (new_decl) == INTEGER_CST
907 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
908 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
909 *tp = new_decl;
910 *walk_subtrees = 0;
912 else if (TREE_CODE (*tp) == STATEMENT_LIST)
913 gcc_unreachable ();
914 else if (TREE_CODE (*tp) == SAVE_EXPR)
915 gcc_unreachable ();
916 else if (TREE_CODE (*tp) == LABEL_DECL
917 && (!DECL_CONTEXT (*tp)
918 || decl_function_context (*tp) == id->src_fn))
919 /* These may need to be remapped for EH handling. */
920 *tp = remap_decl (*tp, id);
921 else if (TREE_CODE (*tp) == FIELD_DECL)
923 /* If the enclosing record type is variably_modified_type_p, the field
924 has already been remapped. Otherwise, it need not be. */
925 tree *n = id->decl_map->get (*tp);
926 if (n)
927 *tp = *n;
928 *walk_subtrees = 0;
930 else if (TYPE_P (*tp))
931 /* Types may need remapping as well. */
932 *tp = remap_type (*tp, id);
933 else if (CONSTANT_CLASS_P (*tp))
935 /* If this is a constant, we have to copy the node iff the type
936 will be remapped. copy_tree_r will not copy a constant. */
937 tree new_type = remap_type (TREE_TYPE (*tp), id);
939 if (new_type == TREE_TYPE (*tp))
940 *walk_subtrees = 0;
942 else if (TREE_CODE (*tp) == INTEGER_CST)
943 *tp = wide_int_to_tree (new_type, *tp);
944 else
946 *tp = copy_node (*tp);
947 TREE_TYPE (*tp) = new_type;
950 else
952 /* Otherwise, just copy the node. Note that copy_tree_r already
953 knows not to copy VAR_DECLs, etc., so this is safe. */
955 if (TREE_CODE (*tp) == MEM_REF)
957 /* We need to re-canonicalize MEM_REFs from inline substitutions
958 that can happen when a pointer argument is an ADDR_EXPR.
959 Recurse here manually to allow that. */
960 tree ptr = TREE_OPERAND (*tp, 0);
961 tree type = remap_type (TREE_TYPE (*tp), id);
962 tree old = *tp;
963 walk_tree (&ptr, remap_gimple_op_r, data, NULL);
964 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
965 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
966 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
967 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
968 if (MR_DEPENDENCE_CLIQUE (old) != 0)
970 MR_DEPENDENCE_CLIQUE (*tp)
971 = remap_dependence_clique (id, MR_DEPENDENCE_CLIQUE (old));
972 MR_DEPENDENCE_BASE (*tp) = MR_DEPENDENCE_BASE (old);
974 /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
975 remapped a parameter as the property might be valid only
976 for the parameter itself. */
977 if (TREE_THIS_NOTRAP (old)
978 && (!is_parm (TREE_OPERAND (old, 0))
979 || (!id->transform_parameter && is_parm (ptr))))
980 TREE_THIS_NOTRAP (*tp) = 1;
981 *walk_subtrees = 0;
982 return NULL;
985 /* Here is the "usual case". Copy this tree node, and then
986 tweak some special cases. */
987 copy_tree_r (tp, walk_subtrees, NULL);
989 if (TREE_CODE (*tp) != OMP_CLAUSE)
990 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
992 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
994 /* The copied TARGET_EXPR has never been expanded, even if the
995 original node was expanded already. */
996 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
997 TREE_OPERAND (*tp, 3) = NULL_TREE;
999 else if (TREE_CODE (*tp) == ADDR_EXPR)
1001 /* Variable substitution need not be simple. In particular,
1002 the MEM_REF substitution above. Make sure that
1003 TREE_CONSTANT and friends are up-to-date. */
1004 int invariant = is_gimple_min_invariant (*tp);
1005 walk_tree (&TREE_OPERAND (*tp, 0), remap_gimple_op_r, data, NULL);
1006 recompute_tree_invariant_for_addr_expr (*tp);
1008 /* If this used to be invariant, but is not any longer,
1009 then regimplification is probably needed. */
1010 if (invariant && !is_gimple_min_invariant (*tp))
1011 id->regimplify = true;
1013 *walk_subtrees = 0;
1017 /* Update the TREE_BLOCK for the cloned expr. */
1018 if (EXPR_P (*tp))
1020 tree new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1021 tree old_block = TREE_BLOCK (*tp);
1022 if (old_block)
1024 tree *n;
1025 n = id->decl_map->get (TREE_BLOCK (*tp));
1026 if (n)
1027 new_block = *n;
1029 TREE_SET_BLOCK (*tp, new_block);
1032 /* Keep iterating. */
1033 return NULL_TREE;
1037 /* Called from copy_body_id via walk_tree. DATA is really a
1038 `copy_body_data *'. */
1040 tree
1041 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
1043 copy_body_data *id = (copy_body_data *) data;
1044 tree fn = id->src_fn;
1045 tree new_block;
1047 /* Begin by recognizing trees that we'll completely rewrite for the
1048 inlining context. Our output for these trees is completely
1049 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1050 into an edge). Further down, we'll handle trees that get
1051 duplicated and/or tweaked. */
1053 /* When requested, RETURN_EXPRs should be transformed to just the
1054 contained MODIFY_EXPR. The branch semantics of the return will
1055 be handled elsewhere by manipulating the CFG rather than a statement. */
1056 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
1058 tree assignment = TREE_OPERAND (*tp, 0);
1060 /* If we're returning something, just turn that into an
1061 assignment into the equivalent of the original RESULT_DECL.
1062 If the "assignment" is just the result decl, the result
1063 decl has already been set (e.g. a recent "foo (&result_decl,
1064 ...)"); just toss the entire RETURN_EXPR. */
1065 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
1067 /* Replace the RETURN_EXPR with (a copy of) the
1068 MODIFY_EXPR hanging underneath. */
1069 *tp = copy_node (assignment);
1071 else /* Else the RETURN_EXPR returns no value. */
1073 *tp = NULL;
1074 return (tree) (void *)1;
1077 else if (TREE_CODE (*tp) == SSA_NAME)
1079 *tp = remap_ssa_name (*tp, id);
1080 *walk_subtrees = 0;
1081 return NULL;
1084 /* Local variables and labels need to be replaced by equivalent
1085 variables. We don't want to copy static variables; there's only
1086 one of those, no matter how many times we inline the containing
1087 function. Similarly for globals from an outer function. */
1088 else if (auto_var_in_fn_p (*tp, fn))
1090 tree new_decl;
1092 /* Remap the declaration. */
1093 new_decl = remap_decl (*tp, id);
1094 gcc_assert (new_decl);
1095 /* Replace this variable with the copy. */
1096 STRIP_TYPE_NOPS (new_decl);
1097 *tp = new_decl;
1098 *walk_subtrees = 0;
1100 else if (TREE_CODE (*tp) == STATEMENT_LIST)
1101 copy_statement_list (tp);
1102 else if (TREE_CODE (*tp) == SAVE_EXPR
1103 || TREE_CODE (*tp) == TARGET_EXPR)
1104 remap_save_expr (tp, id->decl_map, walk_subtrees);
1105 else if (TREE_CODE (*tp) == LABEL_DECL
1106 && (! DECL_CONTEXT (*tp)
1107 || decl_function_context (*tp) == id->src_fn))
1108 /* These may need to be remapped for EH handling. */
1109 *tp = remap_decl (*tp, id);
1110 else if (TREE_CODE (*tp) == BIND_EXPR)
1111 copy_bind_expr (tp, walk_subtrees, id);
1112 /* Types may need remapping as well. */
1113 else if (TYPE_P (*tp))
1114 *tp = remap_type (*tp, id);
1116 /* If this is a constant, we have to copy the node iff the type will be
1117 remapped. copy_tree_r will not copy a constant. */
1118 else if (CONSTANT_CLASS_P (*tp))
1120 tree new_type = remap_type (TREE_TYPE (*tp), id);
1122 if (new_type == TREE_TYPE (*tp))
1123 *walk_subtrees = 0;
1125 else if (TREE_CODE (*tp) == INTEGER_CST)
1126 *tp = wide_int_to_tree (new_type, *tp);
1127 else
1129 *tp = copy_node (*tp);
1130 TREE_TYPE (*tp) = new_type;
1134 /* Otherwise, just copy the node. Note that copy_tree_r already
1135 knows not to copy VAR_DECLs, etc., so this is safe. */
1136 else
1138 /* Here we handle trees that are not completely rewritten.
1139 First we detect some inlining-induced bogosities for
1140 discarding. */
1141 if (TREE_CODE (*tp) == MODIFY_EXPR
1142 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
1143 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
1145 /* Some assignments VAR = VAR; don't generate any rtl code
1146 and thus don't count as variable modification. Avoid
1147 keeping bogosities like 0 = 0. */
1148 tree decl = TREE_OPERAND (*tp, 0), value;
1149 tree *n;
1151 n = id->decl_map->get (decl);
1152 if (n)
1154 value = *n;
1155 STRIP_TYPE_NOPS (value);
1156 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1158 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
1159 return copy_tree_body_r (tp, walk_subtrees, data);
1163 else if (TREE_CODE (*tp) == INDIRECT_REF)
1165 /* Get rid of *& from inline substitutions that can happen when a
1166 pointer argument is an ADDR_EXPR. */
1167 tree decl = TREE_OPERAND (*tp, 0);
1168 tree *n = id->decl_map->get (decl);
1169 if (n)
1171 /* If we happen to get an ADDR_EXPR in n->value, strip
1172 it manually here as we'll eventually get ADDR_EXPRs
1173 which lie about their types pointed to. In this case
1174 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1175 but we absolutely rely on that. As fold_indirect_ref
1176 does other useful transformations, try that first, though. */
1177 tree type = TREE_TYPE (*tp);
1178 tree ptr = id->do_not_unshare ? *n : unshare_expr (*n);
1179 tree old = *tp;
1180 *tp = gimple_fold_indirect_ref (ptr);
1181 if (! *tp)
1183 if (TREE_CODE (ptr) == ADDR_EXPR)
1186 = fold_indirect_ref_1 (EXPR_LOCATION (ptr), type, ptr);
1187 /* ??? We should either assert here or build
1188 a VIEW_CONVERT_EXPR instead of blindly leaking
1189 incompatible types to our IL. */
1190 if (! *tp)
1191 *tp = TREE_OPERAND (ptr, 0);
1193 else
1195 *tp = build1 (INDIRECT_REF, type, ptr);
1196 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1197 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1198 TREE_READONLY (*tp) = TREE_READONLY (old);
1199 /* We cannot propagate the TREE_THIS_NOTRAP flag if we
1200 have remapped a parameter as the property might be
1201 valid only for the parameter itself. */
1202 if (TREE_THIS_NOTRAP (old)
1203 && (!is_parm (TREE_OPERAND (old, 0))
1204 || (!id->transform_parameter && is_parm (ptr))))
1205 TREE_THIS_NOTRAP (*tp) = 1;
1208 *walk_subtrees = 0;
1209 return NULL;
1212 else if (TREE_CODE (*tp) == MEM_REF)
1214 /* We need to re-canonicalize MEM_REFs from inline substitutions
1215 that can happen when a pointer argument is an ADDR_EXPR.
1216 Recurse here manually to allow that. */
1217 tree ptr = TREE_OPERAND (*tp, 0);
1218 tree type = remap_type (TREE_TYPE (*tp), id);
1219 tree old = *tp;
1220 walk_tree (&ptr, copy_tree_body_r, data, NULL);
1221 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
1222 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1223 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1224 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
1225 if (MR_DEPENDENCE_CLIQUE (old) != 0)
1227 MR_DEPENDENCE_CLIQUE (*tp)
1228 = remap_dependence_clique (id, MR_DEPENDENCE_CLIQUE (old));
1229 MR_DEPENDENCE_BASE (*tp) = MR_DEPENDENCE_BASE (old);
1231 /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
1232 remapped a parameter as the property might be valid only
1233 for the parameter itself. */
1234 if (TREE_THIS_NOTRAP (old)
1235 && (!is_parm (TREE_OPERAND (old, 0))
1236 || (!id->transform_parameter && is_parm (ptr))))
1237 TREE_THIS_NOTRAP (*tp) = 1;
1238 *walk_subtrees = 0;
1239 return NULL;
1242 /* Here is the "usual case". Copy this tree node, and then
1243 tweak some special cases. */
1244 copy_tree_r (tp, walk_subtrees, NULL);
1246 /* If EXPR has block defined, map it to newly constructed block.
1247 When inlining we want EXPRs without block appear in the block
1248 of function call if we are not remapping a type. */
1249 if (EXPR_P (*tp))
1251 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1252 if (TREE_BLOCK (*tp))
1254 tree *n;
1255 n = id->decl_map->get (TREE_BLOCK (*tp));
1256 if (n)
1257 new_block = *n;
1259 TREE_SET_BLOCK (*tp, new_block);
1262 if (TREE_CODE (*tp) != OMP_CLAUSE)
1263 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1265 /* The copied TARGET_EXPR has never been expanded, even if the
1266 original node was expanded already. */
1267 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1269 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1270 TREE_OPERAND (*tp, 3) = NULL_TREE;
1273 /* Variable substitution need not be simple. In particular, the
1274 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1275 and friends are up-to-date. */
1276 else if (TREE_CODE (*tp) == ADDR_EXPR)
1278 int invariant = is_gimple_min_invariant (*tp);
1279 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1281 /* Handle the case where we substituted an INDIRECT_REF
1282 into the operand of the ADDR_EXPR. */
1283 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1284 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1285 else
1286 recompute_tree_invariant_for_addr_expr (*tp);
1288 /* If this used to be invariant, but is not any longer,
1289 then regimplification is probably needed. */
1290 if (invariant && !is_gimple_min_invariant (*tp))
1291 id->regimplify = true;
1293 *walk_subtrees = 0;
1297 /* Keep iterating. */
1298 return NULL_TREE;
1301 /* Helper for remap_gimple_stmt. Given an EH region number for the
1302 source function, map that to the duplicate EH region number in
1303 the destination function. */
1305 static int
1306 remap_eh_region_nr (int old_nr, copy_body_data *id)
1308 eh_region old_r, new_r;
1310 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1311 new_r = static_cast<eh_region> (*id->eh_map->get (old_r));
1313 return new_r->index;
1316 /* Similar, but operate on INTEGER_CSTs. */
1318 static tree
1319 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1321 int old_nr, new_nr;
1323 old_nr = tree_to_shwi (old_t_nr);
1324 new_nr = remap_eh_region_nr (old_nr, id);
1326 return build_int_cst (integer_type_node, new_nr);
1329 /* Helper for copy_bb. Remap statement STMT using the inlining
1330 information in ID. Return the new statement copy. */
1332 static gimple_seq
1333 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1335 gimple copy = NULL;
1336 struct walk_stmt_info wi;
1337 bool skip_first = false;
1338 gimple_seq stmts = NULL;
1340 /* Begin by recognizing trees that we'll completely rewrite for the
1341 inlining context. Our output for these trees is completely
1342 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1343 into an edge). Further down, we'll handle trees that get
1344 duplicated and/or tweaked. */
1346 /* When requested, GIMPLE_RETURNs should be transformed to just the
1347 contained GIMPLE_ASSIGN. The branch semantics of the return will
1348 be handled elsewhere by manipulating the CFG rather than the
1349 statement. */
1350 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1352 tree retval = gimple_return_retval (as_a <greturn *> (stmt));
1353 tree retbnd = gimple_return_retbnd (stmt);
1354 tree bndslot = id->retbnd;
1356 if (retbnd && bndslot)
1358 gimple bndcopy = gimple_build_assign (bndslot, retbnd);
1359 memset (&wi, 0, sizeof (wi));
1360 wi.info = id;
1361 walk_gimple_op (bndcopy, remap_gimple_op_r, &wi);
1362 gimple_seq_add_stmt (&stmts, bndcopy);
1365 /* If we're returning something, just turn that into an
1366 assignment into the equivalent of the original RESULT_DECL.
1367 If RETVAL is just the result decl, the result decl has
1368 already been set (e.g. a recent "foo (&result_decl, ...)");
1369 just toss the entire GIMPLE_RETURN. */
1370 if (retval
1371 && (TREE_CODE (retval) != RESULT_DECL
1372 && (TREE_CODE (retval) != SSA_NAME
1373 || ! SSA_NAME_VAR (retval)
1374 || TREE_CODE (SSA_NAME_VAR (retval)) != RESULT_DECL)))
1376 copy = gimple_build_assign (id->do_not_unshare
1377 ? id->retvar : unshare_expr (id->retvar),
1378 retval);
1379 /* id->retvar is already substituted. Skip it on later remapping. */
1380 skip_first = true;
1382 /* We need to copy bounds if return structure with pointers into
1383 instrumented function. */
1384 if (chkp_function_instrumented_p (id->dst_fn)
1385 && !bndslot
1386 && !BOUNDED_P (id->retvar)
1387 && chkp_type_has_pointer (TREE_TYPE (id->retvar)))
1388 id->assign_stmts.safe_push (copy);
1391 else
1392 return stmts;
1394 else if (gimple_has_substatements (stmt))
1396 gimple_seq s1, s2;
1398 /* When cloning bodies from the C++ front end, we will be handed bodies
1399 in High GIMPLE form. Handle here all the High GIMPLE statements that
1400 have embedded statements. */
1401 switch (gimple_code (stmt))
1403 case GIMPLE_BIND:
1404 copy = copy_gimple_bind (as_a <gbind *> (stmt), id);
1405 break;
1407 case GIMPLE_CATCH:
1409 gcatch *catch_stmt = as_a <gcatch *> (stmt);
1410 s1 = remap_gimple_seq (gimple_catch_handler (catch_stmt), id);
1411 copy = gimple_build_catch (gimple_catch_types (catch_stmt), s1);
1413 break;
1415 case GIMPLE_EH_FILTER:
1416 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1417 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1418 break;
1420 case GIMPLE_TRY:
1421 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1422 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1423 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1424 break;
1426 case GIMPLE_WITH_CLEANUP_EXPR:
1427 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1428 copy = gimple_build_wce (s1);
1429 break;
1431 case GIMPLE_OMP_PARALLEL:
1433 gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
1434 s1 = remap_gimple_seq (gimple_omp_body (omp_par_stmt), id);
1435 copy = gimple_build_omp_parallel
1436 (s1,
1437 gimple_omp_parallel_clauses (omp_par_stmt),
1438 gimple_omp_parallel_child_fn (omp_par_stmt),
1439 gimple_omp_parallel_data_arg (omp_par_stmt));
1441 break;
1443 case GIMPLE_OMP_TASK:
1444 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1445 copy = gimple_build_omp_task
1446 (s1,
1447 gimple_omp_task_clauses (stmt),
1448 gimple_omp_task_child_fn (stmt),
1449 gimple_omp_task_data_arg (stmt),
1450 gimple_omp_task_copy_fn (stmt),
1451 gimple_omp_task_arg_size (stmt),
1452 gimple_omp_task_arg_align (stmt));
1453 break;
1455 case GIMPLE_OMP_FOR:
1456 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1457 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1458 copy = gimple_build_omp_for (s1, gimple_omp_for_kind (stmt),
1459 gimple_omp_for_clauses (stmt),
1460 gimple_omp_for_collapse (stmt), s2);
1462 size_t i;
1463 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1465 gimple_omp_for_set_index (copy, i,
1466 gimple_omp_for_index (stmt, i));
1467 gimple_omp_for_set_initial (copy, i,
1468 gimple_omp_for_initial (stmt, i));
1469 gimple_omp_for_set_final (copy, i,
1470 gimple_omp_for_final (stmt, i));
1471 gimple_omp_for_set_incr (copy, i,
1472 gimple_omp_for_incr (stmt, i));
1473 gimple_omp_for_set_cond (copy, i,
1474 gimple_omp_for_cond (stmt, i));
1477 break;
1479 case GIMPLE_OMP_MASTER:
1480 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1481 copy = gimple_build_omp_master (s1);
1482 break;
1484 case GIMPLE_OMP_TASKGROUP:
1485 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1486 copy = gimple_build_omp_taskgroup (s1);
1487 break;
1489 case GIMPLE_OMP_ORDERED:
1490 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1491 copy = gimple_build_omp_ordered (s1);
1492 break;
1494 case GIMPLE_OMP_SECTION:
1495 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1496 copy = gimple_build_omp_section (s1);
1497 break;
1499 case GIMPLE_OMP_SECTIONS:
1500 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1501 copy = gimple_build_omp_sections
1502 (s1, gimple_omp_sections_clauses (stmt));
1503 break;
1505 case GIMPLE_OMP_SINGLE:
1506 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1507 copy = gimple_build_omp_single
1508 (s1, gimple_omp_single_clauses (stmt));
1509 break;
1511 case GIMPLE_OMP_TARGET:
1512 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1513 copy = gimple_build_omp_target
1514 (s1, gimple_omp_target_kind (stmt),
1515 gimple_omp_target_clauses (stmt));
1516 break;
1518 case GIMPLE_OMP_TEAMS:
1519 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1520 copy = gimple_build_omp_teams
1521 (s1, gimple_omp_teams_clauses (stmt));
1522 break;
1524 case GIMPLE_OMP_CRITICAL:
1525 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1526 copy = gimple_build_omp_critical (s1,
1527 gimple_omp_critical_name (
1528 as_a <gomp_critical *> (stmt)));
1529 break;
1531 case GIMPLE_TRANSACTION:
1533 gtransaction *old_trans_stmt = as_a <gtransaction *> (stmt);
1534 gtransaction *new_trans_stmt;
1535 s1 = remap_gimple_seq (gimple_transaction_body (old_trans_stmt),
1536 id);
1537 copy = new_trans_stmt
1538 = gimple_build_transaction (
1540 gimple_transaction_label (old_trans_stmt));
1541 gimple_transaction_set_subcode (
1542 new_trans_stmt,
1543 gimple_transaction_subcode (old_trans_stmt));
1545 break;
1547 default:
1548 gcc_unreachable ();
1551 else
1553 if (gimple_assign_copy_p (stmt)
1554 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1555 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1557 /* Here we handle statements that are not completely rewritten.
1558 First we detect some inlining-induced bogosities for
1559 discarding. */
1561 /* Some assignments VAR = VAR; don't generate any rtl code
1562 and thus don't count as variable modification. Avoid
1563 keeping bogosities like 0 = 0. */
1564 tree decl = gimple_assign_lhs (stmt), value;
1565 tree *n;
1567 n = id->decl_map->get (decl);
1568 if (n)
1570 value = *n;
1571 STRIP_TYPE_NOPS (value);
1572 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1573 return NULL;
1577 /* For *ptr_N ={v} {CLOBBER}, if ptr_N is SSA_NAME defined
1578 in a block that we aren't copying during tree_function_versioning,
1579 just drop the clobber stmt. */
1580 if (id->blocks_to_copy && gimple_clobber_p (stmt))
1582 tree lhs = gimple_assign_lhs (stmt);
1583 if (TREE_CODE (lhs) == MEM_REF
1584 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
1586 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0));
1587 if (gimple_bb (def_stmt)
1588 && !bitmap_bit_p (id->blocks_to_copy,
1589 gimple_bb (def_stmt)->index))
1590 return NULL;
1594 if (gimple_debug_bind_p (stmt))
1596 gdebug *copy
1597 = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1598 gimple_debug_bind_get_value (stmt),
1599 stmt);
1600 id->debug_stmts.safe_push (copy);
1601 gimple_seq_add_stmt (&stmts, copy);
1602 return stmts;
1604 if (gimple_debug_source_bind_p (stmt))
1606 gdebug *copy = gimple_build_debug_source_bind
1607 (gimple_debug_source_bind_get_var (stmt),
1608 gimple_debug_source_bind_get_value (stmt),
1609 stmt);
1610 id->debug_stmts.safe_push (copy);
1611 gimple_seq_add_stmt (&stmts, copy);
1612 return stmts;
1615 /* Create a new deep copy of the statement. */
1616 copy = gimple_copy (stmt);
1618 /* Clear flags that need revisiting. */
1619 if (gcall *call_stmt = dyn_cast <gcall *> (copy))
1621 if (gimple_call_tail_p (call_stmt))
1622 gimple_call_set_tail (call_stmt, false);
1623 if (gimple_call_from_thunk_p (call_stmt))
1624 gimple_call_set_from_thunk (call_stmt, false);
1627 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1628 RESX and EH_DISPATCH. */
1629 if (id->eh_map)
1630 switch (gimple_code (copy))
1632 case GIMPLE_CALL:
1634 tree r, fndecl = gimple_call_fndecl (copy);
1635 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1636 switch (DECL_FUNCTION_CODE (fndecl))
1638 case BUILT_IN_EH_COPY_VALUES:
1639 r = gimple_call_arg (copy, 1);
1640 r = remap_eh_region_tree_nr (r, id);
1641 gimple_call_set_arg (copy, 1, r);
1642 /* FALLTHRU */
1644 case BUILT_IN_EH_POINTER:
1645 case BUILT_IN_EH_FILTER:
1646 r = gimple_call_arg (copy, 0);
1647 r = remap_eh_region_tree_nr (r, id);
1648 gimple_call_set_arg (copy, 0, r);
1649 break;
1651 default:
1652 break;
1655 /* Reset alias info if we didn't apply measures to
1656 keep it valid over inlining by setting DECL_PT_UID. */
1657 if (!id->src_cfun->gimple_df
1658 || !id->src_cfun->gimple_df->ipa_pta)
1659 gimple_call_reset_alias_info (as_a <gcall *> (copy));
1661 break;
1663 case GIMPLE_RESX:
1665 gresx *resx_stmt = as_a <gresx *> (copy);
1666 int r = gimple_resx_region (resx_stmt);
1667 r = remap_eh_region_nr (r, id);
1668 gimple_resx_set_region (resx_stmt, r);
1670 break;
1672 case GIMPLE_EH_DISPATCH:
1674 geh_dispatch *eh_dispatch = as_a <geh_dispatch *> (copy);
1675 int r = gimple_eh_dispatch_region (eh_dispatch);
1676 r = remap_eh_region_nr (r, id);
1677 gimple_eh_dispatch_set_region (eh_dispatch, r);
1679 break;
1681 default:
1682 break;
1686 /* If STMT has a block defined, map it to the newly constructed
1687 block. */
1688 if (gimple_block (copy))
1690 tree *n;
1691 n = id->decl_map->get (gimple_block (copy));
1692 gcc_assert (n);
1693 gimple_set_block (copy, *n);
1696 if (gimple_debug_bind_p (copy) || gimple_debug_source_bind_p (copy))
1698 gimple_seq_add_stmt (&stmts, copy);
1699 return stmts;
1702 /* Remap all the operands in COPY. */
1703 memset (&wi, 0, sizeof (wi));
1704 wi.info = id;
1705 if (skip_first)
1706 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1707 else
1708 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1710 /* Clear the copied virtual operands. We are not remapping them here
1711 but are going to recreate them from scratch. */
1712 if (gimple_has_mem_ops (copy))
1714 gimple_set_vdef (copy, NULL_TREE);
1715 gimple_set_vuse (copy, NULL_TREE);
1718 gimple_seq_add_stmt (&stmts, copy);
1719 return stmts;
1723 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1724 later */
1726 static basic_block
1727 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1728 gcov_type count_scale)
1730 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1731 basic_block copy_basic_block;
1732 tree decl;
1733 gcov_type freq;
1734 basic_block prev;
1736 /* Search for previous copied basic block. */
1737 prev = bb->prev_bb;
1738 while (!prev->aux)
1739 prev = prev->prev_bb;
1741 /* create_basic_block() will append every new block to
1742 basic_block_info automatically. */
1743 copy_basic_block = create_basic_block (NULL, (void *) 0,
1744 (basic_block) prev->aux);
1745 copy_basic_block->count = apply_scale (bb->count, count_scale);
1747 /* We are going to rebuild frequencies from scratch. These values
1748 have just small importance to drive canonicalize_loop_headers. */
1749 freq = apply_scale ((gcov_type)bb->frequency, frequency_scale);
1751 /* We recompute frequencies after inlining, so this is quite safe. */
1752 if (freq > BB_FREQ_MAX)
1753 freq = BB_FREQ_MAX;
1754 copy_basic_block->frequency = freq;
1756 copy_gsi = gsi_start_bb (copy_basic_block);
1758 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1760 gimple_seq stmts;
1761 gimple stmt = gsi_stmt (gsi);
1762 gimple orig_stmt = stmt;
1763 gimple_stmt_iterator stmts_gsi;
1764 bool stmt_added = false;
1766 id->regimplify = false;
1767 stmts = remap_gimple_stmt (stmt, id);
1769 if (gimple_seq_empty_p (stmts))
1770 continue;
1772 seq_gsi = copy_gsi;
1774 for (stmts_gsi = gsi_start (stmts);
1775 !gsi_end_p (stmts_gsi); )
1777 stmt = gsi_stmt (stmts_gsi);
1779 /* Advance iterator now before stmt is moved to seq_gsi. */
1780 gsi_next (&stmts_gsi);
1782 if (gimple_nop_p (stmt))
1783 continue;
1785 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun,
1786 orig_stmt);
1788 /* With return slot optimization we can end up with
1789 non-gimple (foo *)&this->m, fix that here. */
1790 if (is_gimple_assign (stmt)
1791 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
1792 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1794 tree new_rhs;
1795 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1796 gimple_assign_rhs1 (stmt),
1797 true, NULL, false,
1798 GSI_CONTINUE_LINKING);
1799 gimple_assign_set_rhs1 (stmt, new_rhs);
1800 id->regimplify = false;
1803 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1805 if (id->regimplify)
1806 gimple_regimplify_operands (stmt, &seq_gsi);
1808 stmt_added = true;
1811 if (!stmt_added)
1812 continue;
1814 /* If copy_basic_block has been empty at the start of this iteration,
1815 call gsi_start_bb again to get at the newly added statements. */
1816 if (gsi_end_p (copy_gsi))
1817 copy_gsi = gsi_start_bb (copy_basic_block);
1818 else
1819 gsi_next (&copy_gsi);
1821 /* Process the new statement. The call to gimple_regimplify_operands
1822 possibly turned the statement into multiple statements, we
1823 need to process all of them. */
1826 tree fn;
1827 gcall *call_stmt;
1829 stmt = gsi_stmt (copy_gsi);
1830 call_stmt = dyn_cast <gcall *> (stmt);
1831 if (call_stmt
1832 && gimple_call_va_arg_pack_p (call_stmt)
1833 && id->call_stmt)
1835 /* __builtin_va_arg_pack () should be replaced by
1836 all arguments corresponding to ... in the caller. */
1837 tree p;
1838 gcall *new_call;
1839 vec<tree> argarray;
1840 size_t nargs = gimple_call_num_args (id->call_stmt);
1841 size_t n, i, nargs_to_copy;
1842 bool remove_bounds = false;
1844 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1845 nargs--;
1847 /* Bounds should be removed from arg pack in case
1848 we handle not instrumented call in instrumented
1849 function. */
1850 nargs_to_copy = nargs;
1851 if (gimple_call_with_bounds_p (id->call_stmt)
1852 && !gimple_call_with_bounds_p (stmt))
1854 for (i = gimple_call_num_args (id->call_stmt) - nargs;
1855 i < gimple_call_num_args (id->call_stmt);
1856 i++)
1857 if (POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
1858 nargs_to_copy--;
1859 remove_bounds = true;
1862 /* Create the new array of arguments. */
1863 n = nargs_to_copy + gimple_call_num_args (call_stmt);
1864 argarray.create (n);
1865 argarray.safe_grow_cleared (n);
1867 /* Copy all the arguments before '...' */
1868 memcpy (argarray.address (),
1869 gimple_call_arg_ptr (call_stmt, 0),
1870 gimple_call_num_args (call_stmt) * sizeof (tree));
1872 if (remove_bounds)
1874 /* Append the rest of arguments removing bounds. */
1875 unsigned cur = gimple_call_num_args (call_stmt);
1876 i = gimple_call_num_args (id->call_stmt) - nargs;
1877 for (i = gimple_call_num_args (id->call_stmt) - nargs;
1878 i < gimple_call_num_args (id->call_stmt);
1879 i++)
1880 if (!POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
1881 argarray[cur++] = gimple_call_arg (id->call_stmt, i);
1882 gcc_assert (cur == n);
1884 else
1886 /* Append the arguments passed in '...' */
1887 memcpy (argarray.address () + gimple_call_num_args (call_stmt),
1888 gimple_call_arg_ptr (id->call_stmt, 0)
1889 + (gimple_call_num_args (id->call_stmt) - nargs),
1890 nargs * sizeof (tree));
1893 new_call = gimple_build_call_vec (gimple_call_fn (call_stmt),
1894 argarray);
1896 argarray.release ();
1898 /* Copy all GIMPLE_CALL flags, location and block, except
1899 GF_CALL_VA_ARG_PACK. */
1900 gimple_call_copy_flags (new_call, call_stmt);
1901 gimple_call_set_va_arg_pack (new_call, false);
1902 gimple_set_location (new_call, gimple_location (stmt));
1903 gimple_set_block (new_call, gimple_block (stmt));
1904 gimple_call_set_lhs (new_call, gimple_call_lhs (call_stmt));
1906 gsi_replace (&copy_gsi, new_call, false);
1907 stmt = new_call;
1909 else if (is_gimple_call (stmt)
1910 && id->call_stmt
1911 && (decl = gimple_call_fndecl (stmt))
1912 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1913 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1915 /* __builtin_va_arg_pack_len () should be replaced by
1916 the number of anonymous arguments. */
1917 size_t nargs = gimple_call_num_args (id->call_stmt), i;
1918 tree count, p;
1919 gimple new_stmt;
1921 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1922 nargs--;
1924 /* For instrumented calls we should ignore bounds. */
1925 for (i = gimple_call_num_args (id->call_stmt) - nargs;
1926 i < gimple_call_num_args (id->call_stmt);
1927 i++)
1928 if (POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
1929 nargs--;
1931 count = build_int_cst (integer_type_node, nargs);
1932 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1933 gsi_replace (&copy_gsi, new_stmt, false);
1934 stmt = new_stmt;
1937 /* Statements produced by inlining can be unfolded, especially
1938 when we constant propagated some operands. We can't fold
1939 them right now for two reasons:
1940 1) folding require SSA_NAME_DEF_STMTs to be correct
1941 2) we can't change function calls to builtins.
1942 So we just mark statement for later folding. We mark
1943 all new statements, instead just statements that has changed
1944 by some nontrivial substitution so even statements made
1945 foldable indirectly are updated. If this turns out to be
1946 expensive, copy_body can be told to watch for nontrivial
1947 changes. */
1948 if (id->statements_to_fold)
1949 id->statements_to_fold->add (stmt);
1951 /* We're duplicating a CALL_EXPR. Find any corresponding
1952 callgraph edges and update or duplicate them. */
1953 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
1955 struct cgraph_edge *edge;
1957 switch (id->transform_call_graph_edges)
1959 case CB_CGE_DUPLICATE:
1960 edge = id->src_node->get_edge (orig_stmt);
1961 if (edge)
1963 int edge_freq = edge->frequency;
1964 int new_freq;
1965 struct cgraph_edge *old_edge = edge;
1966 edge = edge->clone (id->dst_node, call_stmt,
1967 gimple_uid (stmt),
1968 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1969 true);
1970 /* We could also just rescale the frequency, but
1971 doing so would introduce roundoff errors and make
1972 verifier unhappy. */
1973 new_freq = compute_call_stmt_bb_frequency (id->dst_node->decl,
1974 copy_basic_block);
1976 /* Speculative calls consist of two edges - direct and indirect.
1977 Duplicate the whole thing and distribute frequencies accordingly. */
1978 if (edge->speculative)
1980 struct cgraph_edge *direct, *indirect;
1981 struct ipa_ref *ref;
1983 gcc_assert (!edge->indirect_unknown_callee);
1984 old_edge->speculative_call_info (direct, indirect, ref);
1985 indirect = indirect->clone (id->dst_node, call_stmt,
1986 gimple_uid (stmt),
1987 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1988 true);
1989 if (old_edge->frequency + indirect->frequency)
1991 edge->frequency = MIN (RDIV ((gcov_type)new_freq * old_edge->frequency,
1992 (old_edge->frequency + indirect->frequency)),
1993 CGRAPH_FREQ_MAX);
1994 indirect->frequency = MIN (RDIV ((gcov_type)new_freq * indirect->frequency,
1995 (old_edge->frequency + indirect->frequency)),
1996 CGRAPH_FREQ_MAX);
1998 id->dst_node->clone_reference (ref, stmt);
2000 else
2002 edge->frequency = new_freq;
2003 if (dump_file
2004 && profile_status_for_fn (cfun) != PROFILE_ABSENT
2005 && (edge_freq > edge->frequency + 10
2006 || edge_freq < edge->frequency - 10))
2008 fprintf (dump_file, "Edge frequency estimated by "
2009 "cgraph %i diverge from inliner's estimate %i\n",
2010 edge_freq,
2011 edge->frequency);
2012 fprintf (dump_file,
2013 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
2014 bb->index,
2015 bb->frequency,
2016 copy_basic_block->frequency);
2020 break;
2022 case CB_CGE_MOVE_CLONES:
2023 id->dst_node->set_call_stmt_including_clones (orig_stmt,
2024 call_stmt);
2025 edge = id->dst_node->get_edge (stmt);
2026 break;
2028 case CB_CGE_MOVE:
2029 edge = id->dst_node->get_edge (orig_stmt);
2030 if (edge)
2031 edge->set_call_stmt (call_stmt);
2032 break;
2034 default:
2035 gcc_unreachable ();
2038 /* Constant propagation on argument done during inlining
2039 may create new direct call. Produce an edge for it. */
2040 if ((!edge
2041 || (edge->indirect_inlining_edge
2042 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
2043 && id->dst_node->definition
2044 && (fn = gimple_call_fndecl (stmt)) != NULL)
2046 struct cgraph_node *dest = cgraph_node::get (fn);
2048 /* We have missing edge in the callgraph. This can happen
2049 when previous inlining turned an indirect call into a
2050 direct call by constant propagating arguments or we are
2051 producing dead clone (for further cloning). In all
2052 other cases we hit a bug (incorrect node sharing is the
2053 most common reason for missing edges). */
2054 gcc_assert (!dest->definition
2055 || dest->address_taken
2056 || !id->src_node->definition
2057 || !id->dst_node->definition);
2058 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
2059 id->dst_node->create_edge_including_clones
2060 (dest, orig_stmt, call_stmt, bb->count,
2061 compute_call_stmt_bb_frequency (id->dst_node->decl,
2062 copy_basic_block),
2063 CIF_ORIGINALLY_INDIRECT_CALL);
2064 else
2065 id->dst_node->create_edge (dest, call_stmt,
2066 bb->count,
2067 compute_call_stmt_bb_frequency
2068 (id->dst_node->decl,
2069 copy_basic_block))->inline_failed
2070 = CIF_ORIGINALLY_INDIRECT_CALL;
2071 if (dump_file)
2073 fprintf (dump_file, "Created new direct edge to %s\n",
2074 dest->name ());
2078 notice_special_calls (as_a <gcall *> (stmt));
2081 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
2082 id->eh_map, id->eh_lp_nr);
2084 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
2086 ssa_op_iter i;
2087 tree def;
2089 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
2090 if (TREE_CODE (def) == SSA_NAME)
2091 SSA_NAME_DEF_STMT (def) = stmt;
2094 gsi_next (&copy_gsi);
2096 while (!gsi_end_p (copy_gsi));
2098 copy_gsi = gsi_last_bb (copy_basic_block);
2101 return copy_basic_block;
2104 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
2105 form is quite easy, since dominator relationship for old basic blocks does
2106 not change.
2108 There is however exception where inlining might change dominator relation
2109 across EH edges from basic block within inlined functions destinating
2110 to landing pads in function we inline into.
2112 The function fills in PHI_RESULTs of such PHI nodes if they refer
2113 to gimple regs. Otherwise, the function mark PHI_RESULT of such
2114 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
2115 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
2116 set, and this means that there will be no overlapping live ranges
2117 for the underlying symbol.
2119 This might change in future if we allow redirecting of EH edges and
2120 we might want to change way build CFG pre-inlining to include
2121 all the possible edges then. */
2122 static void
2123 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
2124 bool can_throw, bool nonlocal_goto)
2126 edge e;
2127 edge_iterator ei;
2129 FOR_EACH_EDGE (e, ei, bb->succs)
2130 if (!e->dest->aux
2131 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
2133 gphi *phi;
2134 gphi_iterator si;
2136 if (!nonlocal_goto)
2137 gcc_assert (e->flags & EDGE_EH);
2139 if (!can_throw)
2140 gcc_assert (!(e->flags & EDGE_EH));
2142 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
2144 edge re;
2146 phi = si.phi ();
2148 /* For abnormal goto/call edges the receiver can be the
2149 ENTRY_BLOCK. Do not assert this cannot happen. */
2151 gcc_assert ((e->flags & EDGE_EH)
2152 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
2154 re = find_edge (ret_bb, e->dest);
2155 gcc_checking_assert (re);
2156 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
2157 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
2159 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
2160 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
2166 /* Copy edges from BB into its copy constructed earlier, scale profile
2167 accordingly. Edges will be taken care of later. Assume aux
2168 pointers to point to the copies of each BB. Return true if any
2169 debug stmts are left after a statement that must end the basic block. */
2171 static bool
2172 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb,
2173 basic_block abnormal_goto_dest)
2175 basic_block new_bb = (basic_block) bb->aux;
2176 edge_iterator ei;
2177 edge old_edge;
2178 gimple_stmt_iterator si;
2179 int flags;
2180 bool need_debug_cleanup = false;
2182 /* Use the indices from the original blocks to create edges for the
2183 new ones. */
2184 FOR_EACH_EDGE (old_edge, ei, bb->succs)
2185 if (!(old_edge->flags & EDGE_EH))
2187 edge new_edge;
2189 flags = old_edge->flags;
2191 /* Return edges do get a FALLTHRU flag when the get inlined. */
2192 if (old_edge->dest->index == EXIT_BLOCK
2193 && !(old_edge->flags & (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE|EDGE_FAKE))
2194 && old_edge->dest->aux != EXIT_BLOCK_PTR_FOR_FN (cfun))
2195 flags |= EDGE_FALLTHRU;
2196 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
2197 new_edge->count = apply_scale (old_edge->count, count_scale);
2198 new_edge->probability = old_edge->probability;
2201 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
2202 return false;
2204 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
2206 gimple copy_stmt;
2207 bool can_throw, nonlocal_goto;
2209 copy_stmt = gsi_stmt (si);
2210 if (!is_gimple_debug (copy_stmt))
2211 update_stmt (copy_stmt);
2213 /* Do this before the possible split_block. */
2214 gsi_next (&si);
2216 /* If this tree could throw an exception, there are two
2217 cases where we need to add abnormal edge(s): the
2218 tree wasn't in a region and there is a "current
2219 region" in the caller; or the original tree had
2220 EH edges. In both cases split the block after the tree,
2221 and add abnormal edge(s) as needed; we need both
2222 those from the callee and the caller.
2223 We check whether the copy can throw, because the const
2224 propagation can change an INDIRECT_REF which throws
2225 into a COMPONENT_REF which doesn't. If the copy
2226 can throw, the original could also throw. */
2227 can_throw = stmt_can_throw_internal (copy_stmt);
2228 nonlocal_goto
2229 = (stmt_can_make_abnormal_goto (copy_stmt)
2230 && !computed_goto_p (copy_stmt));
2232 if (can_throw || nonlocal_goto)
2234 if (!gsi_end_p (si))
2236 while (!gsi_end_p (si) && is_gimple_debug (gsi_stmt (si)))
2237 gsi_next (&si);
2238 if (gsi_end_p (si))
2239 need_debug_cleanup = true;
2241 if (!gsi_end_p (si))
2242 /* Note that bb's predecessor edges aren't necessarily
2243 right at this point; split_block doesn't care. */
2245 edge e = split_block (new_bb, copy_stmt);
2247 new_bb = e->dest;
2248 new_bb->aux = e->src->aux;
2249 si = gsi_start_bb (new_bb);
2253 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
2254 make_eh_dispatch_edges (as_a <geh_dispatch *> (copy_stmt));
2255 else if (can_throw)
2256 make_eh_edges (copy_stmt);
2258 /* If the call we inline cannot make abnormal goto do not add
2259 additional abnormal edges but only retain those already present
2260 in the original function body. */
2261 if (abnormal_goto_dest == NULL)
2262 nonlocal_goto = false;
2263 if (nonlocal_goto)
2265 basic_block copy_stmt_bb = gimple_bb (copy_stmt);
2267 if (get_abnormal_succ_dispatcher (copy_stmt_bb))
2268 nonlocal_goto = false;
2269 /* ABNORMAL_DISPATCHER (1) is for longjmp/setjmp or nonlocal gotos
2270 in OpenMP regions which aren't allowed to be left abnormally.
2271 So, no need to add abnormal edge in that case. */
2272 else if (is_gimple_call (copy_stmt)
2273 && gimple_call_internal_p (copy_stmt)
2274 && (gimple_call_internal_fn (copy_stmt)
2275 == IFN_ABNORMAL_DISPATCHER)
2276 && gimple_call_arg (copy_stmt, 0) == boolean_true_node)
2277 nonlocal_goto = false;
2278 else
2279 make_edge (copy_stmt_bb, abnormal_goto_dest, EDGE_ABNORMAL);
2282 if ((can_throw || nonlocal_goto)
2283 && gimple_in_ssa_p (cfun))
2284 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
2285 can_throw, nonlocal_goto);
2287 return need_debug_cleanup;
2290 /* Copy the PHIs. All blocks and edges are copied, some blocks
2291 was possibly split and new outgoing EH edges inserted.
2292 BB points to the block of original function and AUX pointers links
2293 the original and newly copied blocks. */
2295 static void
2296 copy_phis_for_bb (basic_block bb, copy_body_data *id)
2298 basic_block const new_bb = (basic_block) bb->aux;
2299 edge_iterator ei;
2300 gphi *phi;
2301 gphi_iterator si;
2302 edge new_edge;
2303 bool inserted = false;
2305 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
2307 tree res, new_res;
2308 gphi *new_phi;
2310 phi = si.phi ();
2311 res = PHI_RESULT (phi);
2312 new_res = res;
2313 if (!virtual_operand_p (res))
2315 walk_tree (&new_res, copy_tree_body_r, id, NULL);
2316 new_phi = create_phi_node (new_res, new_bb);
2317 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2319 edge old_edge = find_edge ((basic_block) new_edge->src->aux, bb);
2320 tree arg;
2321 tree new_arg;
2322 edge_iterator ei2;
2323 location_t locus;
2325 /* When doing partial cloning, we allow PHIs on the entry block
2326 as long as all the arguments are the same. Find any input
2327 edge to see argument to copy. */
2328 if (!old_edge)
2329 FOR_EACH_EDGE (old_edge, ei2, bb->preds)
2330 if (!old_edge->src->aux)
2331 break;
2333 arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
2334 new_arg = arg;
2335 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
2336 gcc_assert (new_arg);
2337 /* With return slot optimization we can end up with
2338 non-gimple (foo *)&this->m, fix that here. */
2339 if (TREE_CODE (new_arg) != SSA_NAME
2340 && TREE_CODE (new_arg) != FUNCTION_DECL
2341 && !is_gimple_val (new_arg))
2343 gimple_seq stmts = NULL;
2344 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
2345 gsi_insert_seq_on_edge (new_edge, stmts);
2346 inserted = true;
2348 locus = gimple_phi_arg_location_from_edge (phi, old_edge);
2349 if (LOCATION_BLOCK (locus))
2351 tree *n;
2352 n = id->decl_map->get (LOCATION_BLOCK (locus));
2353 gcc_assert (n);
2354 if (*n)
2355 locus = COMBINE_LOCATION_DATA (line_table, locus, *n);
2356 else
2357 locus = LOCATION_LOCUS (locus);
2359 else
2360 locus = LOCATION_LOCUS (locus);
2362 add_phi_arg (new_phi, new_arg, new_edge, locus);
2367 /* Commit the delayed edge insertions. */
2368 if (inserted)
2369 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2370 gsi_commit_one_edge_insert (new_edge, NULL);
2374 /* Wrapper for remap_decl so it can be used as a callback. */
2376 static tree
2377 remap_decl_1 (tree decl, void *data)
2379 return remap_decl (decl, (copy_body_data *) data);
2382 /* Build struct function and associated datastructures for the new clone
2383 NEW_FNDECL to be build. CALLEE_FNDECL is the original. Function changes
2384 the cfun to the function of new_fndecl (and current_function_decl too). */
2386 static void
2387 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
2389 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2390 gcov_type count_scale;
2392 if (!DECL_ARGUMENTS (new_fndecl))
2393 DECL_ARGUMENTS (new_fndecl) = DECL_ARGUMENTS (callee_fndecl);
2394 if (!DECL_RESULT (new_fndecl))
2395 DECL_RESULT (new_fndecl) = DECL_RESULT (callee_fndecl);
2397 if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
2398 count_scale
2399 = GCOV_COMPUTE_SCALE (count,
2400 ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
2401 else
2402 count_scale = REG_BR_PROB_BASE;
2404 /* Register specific tree functions. */
2405 gimple_register_cfg_hooks ();
2407 /* Get clean struct function. */
2408 push_struct_function (new_fndecl);
2410 /* We will rebuild these, so just sanity check that they are empty. */
2411 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
2412 gcc_assert (cfun->local_decls == NULL);
2413 gcc_assert (cfun->cfg == NULL);
2414 gcc_assert (cfun->decl == new_fndecl);
2416 /* Copy items we preserve during cloning. */
2417 cfun->static_chain_decl = src_cfun->static_chain_decl;
2418 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
2419 cfun->function_end_locus = src_cfun->function_end_locus;
2420 cfun->curr_properties = src_cfun->curr_properties;
2421 cfun->last_verified = src_cfun->last_verified;
2422 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2423 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
2424 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2425 cfun->stdarg = src_cfun->stdarg;
2426 cfun->after_inlining = src_cfun->after_inlining;
2427 cfun->can_throw_non_call_exceptions
2428 = src_cfun->can_throw_non_call_exceptions;
2429 cfun->can_delete_dead_exceptions = src_cfun->can_delete_dead_exceptions;
2430 cfun->returns_struct = src_cfun->returns_struct;
2431 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
2433 init_empty_tree_cfg ();
2435 profile_status_for_fn (cfun) = profile_status_for_fn (src_cfun);
2436 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
2437 (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
2438 REG_BR_PROB_BASE);
2439 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
2440 = ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
2441 EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
2442 (EXIT_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
2443 REG_BR_PROB_BASE);
2444 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency =
2445 EXIT_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
2446 if (src_cfun->eh)
2447 init_eh_for_function ();
2449 if (src_cfun->gimple_df)
2451 init_tree_ssa (cfun);
2452 cfun->gimple_df->in_ssa_p = true;
2453 init_ssa_operands (cfun);
2457 /* Helper function for copy_cfg_body. Move debug stmts from the end
2458 of NEW_BB to the beginning of successor basic blocks when needed. If the
2459 successor has multiple predecessors, reset them, otherwise keep
2460 their value. */
2462 static void
2463 maybe_move_debug_stmts_to_successors (copy_body_data *id, basic_block new_bb)
2465 edge e;
2466 edge_iterator ei;
2467 gimple_stmt_iterator si = gsi_last_nondebug_bb (new_bb);
2469 if (gsi_end_p (si)
2470 || gsi_one_before_end_p (si)
2471 || !(stmt_can_throw_internal (gsi_stmt (si))
2472 || stmt_can_make_abnormal_goto (gsi_stmt (si))))
2473 return;
2475 FOR_EACH_EDGE (e, ei, new_bb->succs)
2477 gimple_stmt_iterator ssi = gsi_last_bb (new_bb);
2478 gimple_stmt_iterator dsi = gsi_after_labels (e->dest);
2479 while (is_gimple_debug (gsi_stmt (ssi)))
2481 gimple stmt = gsi_stmt (ssi);
2482 gdebug *new_stmt;
2483 tree var;
2484 tree value;
2486 /* For the last edge move the debug stmts instead of copying
2487 them. */
2488 if (ei_one_before_end_p (ei))
2490 si = ssi;
2491 gsi_prev (&ssi);
2492 if (!single_pred_p (e->dest) && gimple_debug_bind_p (stmt))
2493 gimple_debug_bind_reset_value (stmt);
2494 gsi_remove (&si, false);
2495 gsi_insert_before (&dsi, stmt, GSI_SAME_STMT);
2496 continue;
2499 if (gimple_debug_bind_p (stmt))
2501 var = gimple_debug_bind_get_var (stmt);
2502 if (single_pred_p (e->dest))
2504 value = gimple_debug_bind_get_value (stmt);
2505 value = unshare_expr (value);
2507 else
2508 value = NULL_TREE;
2509 new_stmt = gimple_build_debug_bind (var, value, stmt);
2511 else if (gimple_debug_source_bind_p (stmt))
2513 var = gimple_debug_source_bind_get_var (stmt);
2514 value = gimple_debug_source_bind_get_value (stmt);
2515 new_stmt = gimple_build_debug_source_bind (var, value, stmt);
2517 else
2518 gcc_unreachable ();
2519 gsi_insert_before (&dsi, new_stmt, GSI_SAME_STMT);
2520 id->debug_stmts.safe_push (new_stmt);
2521 gsi_prev (&ssi);
2526 /* Make a copy of the sub-loops of SRC_PARENT and place them
2527 as siblings of DEST_PARENT. */
2529 static void
2530 copy_loops (copy_body_data *id,
2531 struct loop *dest_parent, struct loop *src_parent)
2533 struct loop *src_loop = src_parent->inner;
2534 while (src_loop)
2536 if (!id->blocks_to_copy
2537 || bitmap_bit_p (id->blocks_to_copy, src_loop->header->index))
2539 struct loop *dest_loop = alloc_loop ();
2541 /* Assign the new loop its header and latch and associate
2542 those with the new loop. */
2543 dest_loop->header = (basic_block)src_loop->header->aux;
2544 dest_loop->header->loop_father = dest_loop;
2545 if (src_loop->latch != NULL)
2547 dest_loop->latch = (basic_block)src_loop->latch->aux;
2548 dest_loop->latch->loop_father = dest_loop;
2551 /* Copy loop meta-data. */
2552 copy_loop_info (src_loop, dest_loop);
2554 /* Finally place it into the loop array and the loop tree. */
2555 place_new_loop (cfun, dest_loop);
2556 flow_loop_tree_node_add (dest_parent, dest_loop);
2558 dest_loop->safelen = src_loop->safelen;
2559 dest_loop->dont_vectorize = src_loop->dont_vectorize;
2560 if (src_loop->force_vectorize)
2562 dest_loop->force_vectorize = true;
2563 cfun->has_force_vectorize_loops = true;
2565 if (src_loop->simduid)
2567 dest_loop->simduid = remap_decl (src_loop->simduid, id);
2568 cfun->has_simduid_loops = true;
2571 /* Recurse. */
2572 copy_loops (id, dest_loop, src_loop);
2574 src_loop = src_loop->next;
2578 /* Call cgraph_redirect_edge_call_stmt_to_callee on all calls in BB */
2580 void
2581 redirect_all_calls (copy_body_data * id, basic_block bb)
2583 gimple_stmt_iterator si;
2584 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2586 if (is_gimple_call (gsi_stmt (si)))
2588 struct cgraph_edge *edge = id->dst_node->get_edge (gsi_stmt (si));
2589 if (edge)
2590 edge->redirect_call_stmt_to_callee ();
2595 /* Convert estimated frequencies into counts for NODE, scaling COUNT
2596 with each bb's frequency. Used when NODE has a 0-weight entry
2597 but we are about to inline it into a non-zero count call bb.
2598 See the comments for handle_missing_profiles() in predict.c for
2599 when this can happen for COMDATs. */
2601 void
2602 freqs_to_counts (struct cgraph_node *node, gcov_type count)
2604 basic_block bb;
2605 edge_iterator ei;
2606 edge e;
2607 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2609 FOR_ALL_BB_FN(bb, fn)
2611 bb->count = apply_scale (count,
2612 GCOV_COMPUTE_SCALE (bb->frequency, BB_FREQ_MAX));
2613 FOR_EACH_EDGE (e, ei, bb->succs)
2614 e->count = apply_probability (e->src->count, e->probability);
2618 /* Make a copy of the body of FN so that it can be inserted inline in
2619 another function. Walks FN via CFG, returns new fndecl. */
2621 static tree
2622 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
2623 basic_block entry_block_map, basic_block exit_block_map,
2624 basic_block new_entry)
2626 tree callee_fndecl = id->src_fn;
2627 /* Original cfun for the callee, doesn't change. */
2628 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2629 struct function *cfun_to_copy;
2630 basic_block bb;
2631 tree new_fndecl = NULL;
2632 bool need_debug_cleanup = false;
2633 gcov_type count_scale;
2634 int last;
2635 int incoming_frequency = 0;
2636 gcov_type incoming_count = 0;
2638 /* This can happen for COMDAT routines that end up with 0 counts
2639 despite being called (see the comments for handle_missing_profiles()
2640 in predict.c as to why). Apply counts to the blocks in the callee
2641 before inlining, using the guessed edge frequencies, so that we don't
2642 end up with a 0-count inline body which can confuse downstream
2643 optimizations such as function splitting. */
2644 if (!ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count && count)
2646 /* Apply the larger of the call bb count and the total incoming
2647 call edge count to the callee. */
2648 gcov_type in_count = 0;
2649 struct cgraph_edge *in_edge;
2650 for (in_edge = id->src_node->callers; in_edge;
2651 in_edge = in_edge->next_caller)
2652 in_count += in_edge->count;
2653 freqs_to_counts (id->src_node, count > in_count ? count : in_count);
2656 if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
2657 count_scale
2658 = GCOV_COMPUTE_SCALE (count,
2659 ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
2660 else
2661 count_scale = REG_BR_PROB_BASE;
2663 /* Register specific tree functions. */
2664 gimple_register_cfg_hooks ();
2666 /* If we are inlining just region of the function, make sure to connect
2667 new entry to ENTRY_BLOCK_PTR_FOR_FN (cfun). Since new entry can be
2668 part of loop, we must compute frequency and probability of
2669 ENTRY_BLOCK_PTR_FOR_FN (cfun) based on the frequencies and
2670 probabilities of edges incoming from nonduplicated region. */
2671 if (new_entry)
2673 edge e;
2674 edge_iterator ei;
2676 FOR_EACH_EDGE (e, ei, new_entry->preds)
2677 if (!e->src->aux)
2679 incoming_frequency += EDGE_FREQUENCY (e);
2680 incoming_count += e->count;
2682 incoming_count = apply_scale (incoming_count, count_scale);
2683 incoming_frequency
2684 = apply_scale ((gcov_type)incoming_frequency, frequency_scale);
2685 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = incoming_count;
2686 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = incoming_frequency;
2689 /* Must have a CFG here at this point. */
2690 gcc_assert (ENTRY_BLOCK_PTR_FOR_FN
2691 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2693 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2695 ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = entry_block_map;
2696 EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = exit_block_map;
2697 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy);
2698 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy);
2700 /* Duplicate any exception-handling regions. */
2701 if (cfun->eh)
2702 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2703 remap_decl_1, id);
2705 /* Use aux pointers to map the original blocks to copy. */
2706 FOR_EACH_BB_FN (bb, cfun_to_copy)
2707 if (!id->blocks_to_copy || bitmap_bit_p (id->blocks_to_copy, bb->index))
2709 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2710 bb->aux = new_bb;
2711 new_bb->aux = bb;
2712 new_bb->loop_father = entry_block_map->loop_father;
2715 last = last_basic_block_for_fn (cfun);
2717 /* Now that we've duplicated the blocks, duplicate their edges. */
2718 basic_block abnormal_goto_dest = NULL;
2719 if (id->call_stmt
2720 && stmt_can_make_abnormal_goto (id->call_stmt))
2722 gimple_stmt_iterator gsi = gsi_for_stmt (id->call_stmt);
2724 bb = gimple_bb (id->call_stmt);
2725 gsi_next (&gsi);
2726 if (gsi_end_p (gsi))
2727 abnormal_goto_dest = get_abnormal_succ_dispatcher (bb);
2729 FOR_ALL_BB_FN (bb, cfun_to_copy)
2730 if (!id->blocks_to_copy
2731 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
2732 need_debug_cleanup |= copy_edges_for_bb (bb, count_scale, exit_block_map,
2733 abnormal_goto_dest);
2735 if (new_entry)
2737 edge e = make_edge (entry_block_map, (basic_block)new_entry->aux, EDGE_FALLTHRU);
2738 e->probability = REG_BR_PROB_BASE;
2739 e->count = incoming_count;
2742 /* Duplicate the loop tree, if available and wanted. */
2743 if (loops_for_fn (src_cfun) != NULL
2744 && current_loops != NULL)
2746 copy_loops (id, entry_block_map->loop_father,
2747 get_loop (src_cfun, 0));
2748 /* Defer to cfgcleanup to update loop-father fields of basic-blocks. */
2749 loops_state_set (LOOPS_NEED_FIXUP);
2752 /* If the loop tree in the source function needed fixup, mark the
2753 destination loop tree for fixup, too. */
2754 if (loops_for_fn (src_cfun)->state & LOOPS_NEED_FIXUP)
2755 loops_state_set (LOOPS_NEED_FIXUP);
2757 if (gimple_in_ssa_p (cfun))
2758 FOR_ALL_BB_FN (bb, cfun_to_copy)
2759 if (!id->blocks_to_copy
2760 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
2761 copy_phis_for_bb (bb, id);
2763 FOR_ALL_BB_FN (bb, cfun_to_copy)
2764 if (bb->aux)
2766 if (need_debug_cleanup
2767 && bb->index != ENTRY_BLOCK
2768 && bb->index != EXIT_BLOCK)
2769 maybe_move_debug_stmts_to_successors (id, (basic_block) bb->aux);
2770 /* Update call edge destinations. This can not be done before loop
2771 info is updated, because we may split basic blocks. */
2772 if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
2773 redirect_all_calls (id, (basic_block)bb->aux);
2774 ((basic_block)bb->aux)->aux = NULL;
2775 bb->aux = NULL;
2778 /* Zero out AUX fields of newly created block during EH edge
2779 insertion. */
2780 for (; last < last_basic_block_for_fn (cfun); last++)
2782 if (need_debug_cleanup)
2783 maybe_move_debug_stmts_to_successors (id,
2784 BASIC_BLOCK_FOR_FN (cfun, last));
2785 BASIC_BLOCK_FOR_FN (cfun, last)->aux = NULL;
2786 /* Update call edge destinations. This can not be done before loop
2787 info is updated, because we may split basic blocks. */
2788 if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
2789 redirect_all_calls (id, BASIC_BLOCK_FOR_FN (cfun, last));
2791 entry_block_map->aux = NULL;
2792 exit_block_map->aux = NULL;
2794 if (id->eh_map)
2796 delete id->eh_map;
2797 id->eh_map = NULL;
2799 if (id->dependence_map)
2801 delete id->dependence_map;
2802 id->dependence_map = NULL;
2805 return new_fndecl;
2808 /* Copy the debug STMT using ID. We deal with these statements in a
2809 special way: if any variable in their VALUE expression wasn't
2810 remapped yet, we won't remap it, because that would get decl uids
2811 out of sync, causing codegen differences between -g and -g0. If
2812 this arises, we drop the VALUE expression altogether. */
2814 static void
2815 copy_debug_stmt (gdebug *stmt, copy_body_data *id)
2817 tree t, *n;
2818 struct walk_stmt_info wi;
2820 if (gimple_block (stmt))
2822 n = id->decl_map->get (gimple_block (stmt));
2823 gimple_set_block (stmt, n ? *n : id->block);
2826 /* Remap all the operands in COPY. */
2827 memset (&wi, 0, sizeof (wi));
2828 wi.info = id;
2830 processing_debug_stmt = 1;
2832 if (gimple_debug_source_bind_p (stmt))
2833 t = gimple_debug_source_bind_get_var (stmt);
2834 else
2835 t = gimple_debug_bind_get_var (stmt);
2837 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2838 && (n = id->debug_map->get (t)))
2840 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2841 t = *n;
2843 else if (TREE_CODE (t) == VAR_DECL
2844 && !is_global_var (t)
2845 && !id->decl_map->get (t))
2846 /* T is a non-localized variable. */;
2847 else
2848 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2850 if (gimple_debug_bind_p (stmt))
2852 gimple_debug_bind_set_var (stmt, t);
2854 if (gimple_debug_bind_has_value_p (stmt))
2855 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2856 remap_gimple_op_r, &wi, NULL);
2858 /* Punt if any decl couldn't be remapped. */
2859 if (processing_debug_stmt < 0)
2860 gimple_debug_bind_reset_value (stmt);
2862 else if (gimple_debug_source_bind_p (stmt))
2864 gimple_debug_source_bind_set_var (stmt, t);
2865 walk_tree (gimple_debug_source_bind_get_value_ptr (stmt),
2866 remap_gimple_op_r, &wi, NULL);
2867 /* When inlining and source bind refers to one of the optimized
2868 away parameters, change the source bind into normal debug bind
2869 referring to the corresponding DEBUG_EXPR_DECL that should have
2870 been bound before the call stmt. */
2871 t = gimple_debug_source_bind_get_value (stmt);
2872 if (t != NULL_TREE
2873 && TREE_CODE (t) == PARM_DECL
2874 && id->call_stmt)
2876 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (id->src_fn);
2877 unsigned int i;
2878 if (debug_args != NULL)
2880 for (i = 0; i < vec_safe_length (*debug_args); i += 2)
2881 if ((**debug_args)[i] == DECL_ORIGIN (t)
2882 && TREE_CODE ((**debug_args)[i + 1]) == DEBUG_EXPR_DECL)
2884 t = (**debug_args)[i + 1];
2885 stmt->subcode = GIMPLE_DEBUG_BIND;
2886 gimple_debug_bind_set_value (stmt, t);
2887 break;
2893 processing_debug_stmt = 0;
2895 update_stmt (stmt);
2898 /* Process deferred debug stmts. In order to give values better odds
2899 of being successfully remapped, we delay the processing of debug
2900 stmts until all other stmts that might require remapping are
2901 processed. */
2903 static void
2904 copy_debug_stmts (copy_body_data *id)
2906 size_t i;
2907 gdebug *stmt;
2909 if (!id->debug_stmts.exists ())
2910 return;
2912 FOR_EACH_VEC_ELT (id->debug_stmts, i, stmt)
2913 copy_debug_stmt (stmt, id);
2915 id->debug_stmts.release ();
2918 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2919 another function. */
2921 static tree
2922 copy_tree_body (copy_body_data *id)
2924 tree fndecl = id->src_fn;
2925 tree body = DECL_SAVED_TREE (fndecl);
2927 walk_tree (&body, copy_tree_body_r, id, NULL);
2929 return body;
2932 /* Make a copy of the body of FN so that it can be inserted inline in
2933 another function. */
2935 static tree
2936 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
2937 basic_block entry_block_map, basic_block exit_block_map,
2938 basic_block new_entry)
2940 tree fndecl = id->src_fn;
2941 tree body;
2943 /* If this body has a CFG, walk CFG and copy. */
2944 gcc_assert (ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (fndecl)));
2945 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map,
2946 new_entry);
2947 copy_debug_stmts (id);
2949 return body;
2952 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2953 defined in function FN, or of a data member thereof. */
2955 static bool
2956 self_inlining_addr_expr (tree value, tree fn)
2958 tree var;
2960 if (TREE_CODE (value) != ADDR_EXPR)
2961 return false;
2963 var = get_base_address (TREE_OPERAND (value, 0));
2965 return var && auto_var_in_fn_p (var, fn);
2968 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2969 lexical block and line number information from base_stmt, if given,
2970 or from the last stmt of the block otherwise. */
2972 static gimple
2973 insert_init_debug_bind (copy_body_data *id,
2974 basic_block bb, tree var, tree value,
2975 gimple base_stmt)
2977 gimple note;
2978 gimple_stmt_iterator gsi;
2979 tree tracked_var;
2981 if (!gimple_in_ssa_p (id->src_cfun))
2982 return NULL;
2984 if (!MAY_HAVE_DEBUG_STMTS)
2985 return NULL;
2987 tracked_var = target_for_debug_bind (var);
2988 if (!tracked_var)
2989 return NULL;
2991 if (bb)
2993 gsi = gsi_last_bb (bb);
2994 if (!base_stmt && !gsi_end_p (gsi))
2995 base_stmt = gsi_stmt (gsi);
2998 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
3000 if (bb)
3002 if (!gsi_end_p (gsi))
3003 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
3004 else
3005 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
3008 return note;
3011 static void
3012 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
3014 /* If VAR represents a zero-sized variable, it's possible that the
3015 assignment statement may result in no gimple statements. */
3016 if (init_stmt)
3018 gimple_stmt_iterator si = gsi_last_bb (bb);
3020 /* We can end up with init statements that store to a non-register
3021 from a rhs with a conversion. Handle that here by forcing the
3022 rhs into a temporary. gimple_regimplify_operands is not
3023 prepared to do this for us. */
3024 if (!is_gimple_debug (init_stmt)
3025 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
3026 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
3027 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
3029 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
3030 gimple_expr_type (init_stmt),
3031 gimple_assign_rhs1 (init_stmt));
3032 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
3033 GSI_NEW_STMT);
3034 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
3035 gimple_assign_set_rhs1 (init_stmt, rhs);
3037 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
3038 gimple_regimplify_operands (init_stmt, &si);
3040 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
3042 tree def = gimple_assign_lhs (init_stmt);
3043 insert_init_debug_bind (id, bb, def, def, init_stmt);
3048 /* Initialize parameter P with VALUE. If needed, produce init statement
3049 at the end of BB. When BB is NULL, we return init statement to be
3050 output later. */
3051 static gimple
3052 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
3053 basic_block bb, tree *vars)
3055 gimple init_stmt = NULL;
3056 tree var;
3057 tree rhs = value;
3058 tree def = (gimple_in_ssa_p (cfun)
3059 ? ssa_default_def (id->src_cfun, p) : NULL);
3061 if (value
3062 && value != error_mark_node
3063 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
3065 /* If we can match up types by promotion/demotion do so. */
3066 if (fold_convertible_p (TREE_TYPE (p), value))
3067 rhs = fold_convert (TREE_TYPE (p), value);
3068 else
3070 /* ??? For valid programs we should not end up here.
3071 Still if we end up with truly mismatched types here, fall back
3072 to using a VIEW_CONVERT_EXPR or a literal zero to not leak invalid
3073 GIMPLE to the following passes. */
3074 if (!is_gimple_reg_type (TREE_TYPE (value))
3075 || TYPE_SIZE (TREE_TYPE (p)) == TYPE_SIZE (TREE_TYPE (value)))
3076 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
3077 else
3078 rhs = build_zero_cst (TREE_TYPE (p));
3082 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
3083 here since the type of this decl must be visible to the calling
3084 function. */
3085 var = copy_decl_to_var (p, id);
3087 /* Declare this new variable. */
3088 DECL_CHAIN (var) = *vars;
3089 *vars = var;
3091 /* Make gimplifier happy about this variable. */
3092 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
3094 /* If the parameter is never assigned to, has no SSA_NAMEs created,
3095 we would not need to create a new variable here at all, if it
3096 weren't for debug info. Still, we can just use the argument
3097 value. */
3098 if (TREE_READONLY (p)
3099 && !TREE_ADDRESSABLE (p)
3100 && value && !TREE_SIDE_EFFECTS (value)
3101 && !def)
3103 /* We may produce non-gimple trees by adding NOPs or introduce
3104 invalid sharing when operand is not really constant.
3105 It is not big deal to prohibit constant propagation here as
3106 we will constant propagate in DOM1 pass anyway. */
3107 if (is_gimple_min_invariant (value)
3108 && useless_type_conversion_p (TREE_TYPE (p),
3109 TREE_TYPE (value))
3110 /* We have to be very careful about ADDR_EXPR. Make sure
3111 the base variable isn't a local variable of the inlined
3112 function, e.g., when doing recursive inlining, direct or
3113 mutually-recursive or whatever, which is why we don't
3114 just test whether fn == current_function_decl. */
3115 && ! self_inlining_addr_expr (value, fn))
3117 insert_decl_map (id, p, value);
3118 insert_debug_decl_map (id, p, var);
3119 return insert_init_debug_bind (id, bb, var, value, NULL);
3123 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
3124 that way, when the PARM_DECL is encountered, it will be
3125 automatically replaced by the VAR_DECL. */
3126 insert_decl_map (id, p, var);
3128 /* Even if P was TREE_READONLY, the new VAR should not be.
3129 In the original code, we would have constructed a
3130 temporary, and then the function body would have never
3131 changed the value of P. However, now, we will be
3132 constructing VAR directly. The constructor body may
3133 change its value multiple times as it is being
3134 constructed. Therefore, it must not be TREE_READONLY;
3135 the back-end assumes that TREE_READONLY variable is
3136 assigned to only once. */
3137 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
3138 TREE_READONLY (var) = 0;
3140 /* If there is no setup required and we are in SSA, take the easy route
3141 replacing all SSA names representing the function parameter by the
3142 SSA name passed to function.
3144 We need to construct map for the variable anyway as it might be used
3145 in different SSA names when parameter is set in function.
3147 Do replacement at -O0 for const arguments replaced by constant.
3148 This is important for builtin_constant_p and other construct requiring
3149 constant argument to be visible in inlined function body. */
3150 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
3151 && (optimize
3152 || (TREE_READONLY (p)
3153 && is_gimple_min_invariant (rhs)))
3154 && (TREE_CODE (rhs) == SSA_NAME
3155 || is_gimple_min_invariant (rhs))
3156 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
3158 insert_decl_map (id, def, rhs);
3159 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3162 /* If the value of argument is never used, don't care about initializing
3163 it. */
3164 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
3166 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
3167 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3170 /* Initialize this VAR_DECL from the equivalent argument. Convert
3171 the argument to the proper type in case it was promoted. */
3172 if (value)
3174 if (rhs == error_mark_node)
3176 insert_decl_map (id, p, var);
3177 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3180 STRIP_USELESS_TYPE_CONVERSION (rhs);
3182 /* If we are in SSA form properly remap the default definition
3183 or assign to a dummy SSA name if the parameter is unused and
3184 we are not optimizing. */
3185 if (gimple_in_ssa_p (cfun) && is_gimple_reg (p))
3187 if (def)
3189 def = remap_ssa_name (def, id);
3190 init_stmt = gimple_build_assign (def, rhs);
3191 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
3192 set_ssa_default_def (cfun, var, NULL);
3194 else if (!optimize)
3196 def = make_ssa_name (var);
3197 init_stmt = gimple_build_assign (def, rhs);
3200 else
3201 init_stmt = gimple_build_assign (var, rhs);
3203 if (bb && init_stmt)
3204 insert_init_stmt (id, bb, init_stmt);
3206 return init_stmt;
3209 /* Generate code to initialize the parameters of the function at the
3210 top of the stack in ID from the GIMPLE_CALL STMT. */
3212 static void
3213 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
3214 tree fn, basic_block bb)
3216 tree parms;
3217 size_t i;
3218 tree p;
3219 tree vars = NULL_TREE;
3220 tree static_chain = gimple_call_chain (stmt);
3222 /* Figure out what the parameters are. */
3223 parms = DECL_ARGUMENTS (fn);
3225 /* Loop through the parameter declarations, replacing each with an
3226 equivalent VAR_DECL, appropriately initialized. */
3227 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
3229 tree val;
3230 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
3231 setup_one_parameter (id, p, val, fn, bb, &vars);
3233 /* After remapping parameters remap their types. This has to be done
3234 in a second loop over all parameters to appropriately remap
3235 variable sized arrays when the size is specified in a
3236 parameter following the array. */
3237 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
3239 tree *varp = id->decl_map->get (p);
3240 if (varp
3241 && TREE_CODE (*varp) == VAR_DECL)
3243 tree def = (gimple_in_ssa_p (cfun) && is_gimple_reg (p)
3244 ? ssa_default_def (id->src_cfun, p) : NULL);
3245 tree var = *varp;
3246 TREE_TYPE (var) = remap_type (TREE_TYPE (var), id);
3247 /* Also remap the default definition if it was remapped
3248 to the default definition of the parameter replacement
3249 by the parameter setup. */
3250 if (def)
3252 tree *defp = id->decl_map->get (def);
3253 if (defp
3254 && TREE_CODE (*defp) == SSA_NAME
3255 && SSA_NAME_VAR (*defp) == var)
3256 TREE_TYPE (*defp) = TREE_TYPE (var);
3261 /* Initialize the static chain. */
3262 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
3263 gcc_assert (fn != current_function_decl);
3264 if (p)
3266 /* No static chain? Seems like a bug in tree-nested.c. */
3267 gcc_assert (static_chain);
3269 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
3272 declare_inline_vars (id->block, vars);
3276 /* Declare a return variable to replace the RESULT_DECL for the
3277 function we are calling. An appropriate DECL_STMT is returned.
3278 The USE_STMT is filled to contain a use of the declaration to
3279 indicate the return value of the function.
3281 RETURN_SLOT, if non-null is place where to store the result. It
3282 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
3283 was the LHS of the MODIFY_EXPR to which this call is the RHS.
3285 RETURN_BOUNDS holds a destination for returned bounds.
3287 The return value is a (possibly null) value that holds the result
3288 as seen by the caller. */
3290 static tree
3291 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
3292 tree return_bounds, basic_block entry_bb)
3294 tree callee = id->src_fn;
3295 tree result = DECL_RESULT (callee);
3296 tree callee_type = TREE_TYPE (result);
3297 tree caller_type;
3298 tree var, use;
3300 /* Handle type-mismatches in the function declaration return type
3301 vs. the call expression. */
3302 if (modify_dest)
3303 caller_type = TREE_TYPE (modify_dest);
3304 else
3305 caller_type = TREE_TYPE (TREE_TYPE (callee));
3307 /* We don't need to do anything for functions that don't return anything. */
3308 if (VOID_TYPE_P (callee_type))
3309 return NULL_TREE;
3311 /* If there was a return slot, then the return value is the
3312 dereferenced address of that object. */
3313 if (return_slot)
3315 /* The front end shouldn't have used both return_slot and
3316 a modify expression. */
3317 gcc_assert (!modify_dest);
3318 if (DECL_BY_REFERENCE (result))
3320 tree return_slot_addr = build_fold_addr_expr (return_slot);
3321 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
3323 /* We are going to construct *&return_slot and we can't do that
3324 for variables believed to be not addressable.
3326 FIXME: This check possibly can match, because values returned
3327 via return slot optimization are not believed to have address
3328 taken by alias analysis. */
3329 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
3330 var = return_slot_addr;
3332 else
3334 var = return_slot;
3335 gcc_assert (TREE_CODE (var) != SSA_NAME);
3336 if (TREE_ADDRESSABLE (result))
3337 mark_addressable (var);
3339 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
3340 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
3341 && !DECL_GIMPLE_REG_P (result)
3342 && DECL_P (var))
3343 DECL_GIMPLE_REG_P (var) = 0;
3344 use = NULL;
3345 goto done;
3348 /* All types requiring non-trivial constructors should have been handled. */
3349 gcc_assert (!TREE_ADDRESSABLE (callee_type));
3351 /* Attempt to avoid creating a new temporary variable. */
3352 if (modify_dest
3353 && TREE_CODE (modify_dest) != SSA_NAME)
3355 bool use_it = false;
3357 /* We can't use MODIFY_DEST if there's type promotion involved. */
3358 if (!useless_type_conversion_p (callee_type, caller_type))
3359 use_it = false;
3361 /* ??? If we're assigning to a variable sized type, then we must
3362 reuse the destination variable, because we've no good way to
3363 create variable sized temporaries at this point. */
3364 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
3365 use_it = true;
3367 /* If the callee cannot possibly modify MODIFY_DEST, then we can
3368 reuse it as the result of the call directly. Don't do this if
3369 it would promote MODIFY_DEST to addressable. */
3370 else if (TREE_ADDRESSABLE (result))
3371 use_it = false;
3372 else
3374 tree base_m = get_base_address (modify_dest);
3376 /* If the base isn't a decl, then it's a pointer, and we don't
3377 know where that's going to go. */
3378 if (!DECL_P (base_m))
3379 use_it = false;
3380 else if (is_global_var (base_m))
3381 use_it = false;
3382 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
3383 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
3384 && !DECL_GIMPLE_REG_P (result)
3385 && DECL_GIMPLE_REG_P (base_m))
3386 use_it = false;
3387 else if (!TREE_ADDRESSABLE (base_m))
3388 use_it = true;
3391 if (use_it)
3393 var = modify_dest;
3394 use = NULL;
3395 goto done;
3399 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
3401 var = copy_result_decl_to_var (result, id);
3402 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
3404 /* Do not have the rest of GCC warn about this variable as it should
3405 not be visible to the user. */
3406 TREE_NO_WARNING (var) = 1;
3408 declare_inline_vars (id->block, var);
3410 /* Build the use expr. If the return type of the function was
3411 promoted, convert it back to the expected type. */
3412 use = var;
3413 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
3415 /* If we can match up types by promotion/demotion do so. */
3416 if (fold_convertible_p (caller_type, var))
3417 use = fold_convert (caller_type, var);
3418 else
3420 /* ??? For valid programs we should not end up here.
3421 Still if we end up with truly mismatched types here, fall back
3422 to using a MEM_REF to not leak invalid GIMPLE to the following
3423 passes. */
3424 /* Prevent var from being written into SSA form. */
3425 if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE
3426 || TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE)
3427 DECL_GIMPLE_REG_P (var) = false;
3428 else if (is_gimple_reg_type (TREE_TYPE (var)))
3429 TREE_ADDRESSABLE (var) = true;
3430 use = fold_build2 (MEM_REF, caller_type,
3431 build_fold_addr_expr (var),
3432 build_int_cst (ptr_type_node, 0));
3436 STRIP_USELESS_TYPE_CONVERSION (use);
3438 if (DECL_BY_REFERENCE (result))
3440 TREE_ADDRESSABLE (var) = 1;
3441 var = build_fold_addr_expr (var);
3444 done:
3445 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
3446 way, when the RESULT_DECL is encountered, it will be
3447 automatically replaced by the VAR_DECL.
3449 When returning by reference, ensure that RESULT_DECL remaps to
3450 gimple_val. */
3451 if (DECL_BY_REFERENCE (result)
3452 && !is_gimple_val (var))
3454 tree temp = create_tmp_var (TREE_TYPE (result), "retvalptr");
3455 insert_decl_map (id, result, temp);
3456 /* When RESULT_DECL is in SSA form, we need to remap and initialize
3457 it's default_def SSA_NAME. */
3458 if (gimple_in_ssa_p (id->src_cfun)
3459 && is_gimple_reg (result))
3461 temp = make_ssa_name (temp);
3462 insert_decl_map (id, ssa_default_def (id->src_cfun, result), temp);
3464 insert_init_stmt (id, entry_bb, gimple_build_assign (temp, var));
3466 else
3467 insert_decl_map (id, result, var);
3469 /* Remember this so we can ignore it in remap_decls. */
3470 id->retvar = var;
3472 /* If returned bounds are used, then make var for them. */
3473 if (return_bounds)
3475 tree bndtemp = create_tmp_var (pointer_bounds_type_node, "retbnd");
3476 DECL_SEEN_IN_BIND_EXPR_P (bndtemp) = 1;
3477 TREE_NO_WARNING (bndtemp) = 1;
3478 declare_inline_vars (id->block, bndtemp);
3480 id->retbnd = bndtemp;
3481 insert_init_stmt (id, entry_bb,
3482 gimple_build_assign (bndtemp, chkp_get_zero_bounds_var ()));
3485 return use;
3488 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
3489 to a local label. */
3491 static tree
3492 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
3494 tree node = *nodep;
3495 tree fn = (tree) fnp;
3497 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
3498 return node;
3500 if (TYPE_P (node))
3501 *walk_subtrees = 0;
3503 return NULL_TREE;
3506 /* Determine if the function can be copied. If so return NULL. If
3507 not return a string describng the reason for failure. */
3509 static const char *
3510 copy_forbidden (struct function *fun, tree fndecl)
3512 const char *reason = fun->cannot_be_copied_reason;
3513 tree decl;
3514 unsigned ix;
3516 /* Only examine the function once. */
3517 if (fun->cannot_be_copied_set)
3518 return reason;
3520 /* We cannot copy a function that receives a non-local goto
3521 because we cannot remap the destination label used in the
3522 function that is performing the non-local goto. */
3523 /* ??? Actually, this should be possible, if we work at it.
3524 No doubt there's just a handful of places that simply
3525 assume it doesn't happen and don't substitute properly. */
3526 if (fun->has_nonlocal_label)
3528 reason = G_("function %q+F can never be copied "
3529 "because it receives a non-local goto");
3530 goto fail;
3533 FOR_EACH_LOCAL_DECL (fun, ix, decl)
3534 if (TREE_CODE (decl) == VAR_DECL
3535 && TREE_STATIC (decl)
3536 && !DECL_EXTERNAL (decl)
3537 && DECL_INITIAL (decl)
3538 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
3539 has_label_address_in_static_1,
3540 fndecl))
3542 reason = G_("function %q+F can never be copied because it saves "
3543 "address of local label in a static variable");
3544 goto fail;
3547 fail:
3548 fun->cannot_be_copied_reason = reason;
3549 fun->cannot_be_copied_set = true;
3550 return reason;
3554 static const char *inline_forbidden_reason;
3556 /* A callback for walk_gimple_seq to handle statements. Returns non-null
3557 iff a function can not be inlined. Also sets the reason why. */
3559 static tree
3560 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
3561 struct walk_stmt_info *wip)
3563 tree fn = (tree) wip->info;
3564 tree t;
3565 gimple stmt = gsi_stmt (*gsi);
3567 switch (gimple_code (stmt))
3569 case GIMPLE_CALL:
3570 /* Refuse to inline alloca call unless user explicitly forced so as
3571 this may change program's memory overhead drastically when the
3572 function using alloca is called in loop. In GCC present in
3573 SPEC2000 inlining into schedule_block cause it to require 2GB of
3574 RAM instead of 256MB. Don't do so for alloca calls emitted for
3575 VLA objects as those can't cause unbounded growth (they're always
3576 wrapped inside stack_save/stack_restore regions. */
3577 if (gimple_alloca_call_p (stmt)
3578 && !gimple_call_alloca_for_var_p (as_a <gcall *> (stmt))
3579 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
3581 inline_forbidden_reason
3582 = G_("function %q+F can never be inlined because it uses "
3583 "alloca (override using the always_inline attribute)");
3584 *handled_ops_p = true;
3585 return fn;
3588 t = gimple_call_fndecl (stmt);
3589 if (t == NULL_TREE)
3590 break;
3592 /* We cannot inline functions that call setjmp. */
3593 if (setjmp_call_p (t))
3595 inline_forbidden_reason
3596 = G_("function %q+F can never be inlined because it uses setjmp");
3597 *handled_ops_p = true;
3598 return t;
3601 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
3602 switch (DECL_FUNCTION_CODE (t))
3604 /* We cannot inline functions that take a variable number of
3605 arguments. */
3606 case BUILT_IN_VA_START:
3607 case BUILT_IN_NEXT_ARG:
3608 case BUILT_IN_VA_END:
3609 inline_forbidden_reason
3610 = G_("function %q+F can never be inlined because it "
3611 "uses variable argument lists");
3612 *handled_ops_p = true;
3613 return t;
3615 case BUILT_IN_LONGJMP:
3616 /* We can't inline functions that call __builtin_longjmp at
3617 all. The non-local goto machinery really requires the
3618 destination be in a different function. If we allow the
3619 function calling __builtin_longjmp to be inlined into the
3620 function calling __builtin_setjmp, Things will Go Awry. */
3621 inline_forbidden_reason
3622 = G_("function %q+F can never be inlined because "
3623 "it uses setjmp-longjmp exception handling");
3624 *handled_ops_p = true;
3625 return t;
3627 case BUILT_IN_NONLOCAL_GOTO:
3628 /* Similarly. */
3629 inline_forbidden_reason
3630 = G_("function %q+F can never be inlined because "
3631 "it uses non-local goto");
3632 *handled_ops_p = true;
3633 return t;
3635 case BUILT_IN_RETURN:
3636 case BUILT_IN_APPLY_ARGS:
3637 /* If a __builtin_apply_args caller would be inlined,
3638 it would be saving arguments of the function it has
3639 been inlined into. Similarly __builtin_return would
3640 return from the function the inline has been inlined into. */
3641 inline_forbidden_reason
3642 = G_("function %q+F can never be inlined because "
3643 "it uses __builtin_return or __builtin_apply_args");
3644 *handled_ops_p = true;
3645 return t;
3647 default:
3648 break;
3650 break;
3652 case GIMPLE_GOTO:
3653 t = gimple_goto_dest (stmt);
3655 /* We will not inline a function which uses computed goto. The
3656 addresses of its local labels, which may be tucked into
3657 global storage, are of course not constant across
3658 instantiations, which causes unexpected behavior. */
3659 if (TREE_CODE (t) != LABEL_DECL)
3661 inline_forbidden_reason
3662 = G_("function %q+F can never be inlined "
3663 "because it contains a computed goto");
3664 *handled_ops_p = true;
3665 return t;
3667 break;
3669 default:
3670 break;
3673 *handled_ops_p = false;
3674 return NULL_TREE;
3677 /* Return true if FNDECL is a function that cannot be inlined into
3678 another one. */
3680 static bool
3681 inline_forbidden_p (tree fndecl)
3683 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
3684 struct walk_stmt_info wi;
3685 basic_block bb;
3686 bool forbidden_p = false;
3688 /* First check for shared reasons not to copy the code. */
3689 inline_forbidden_reason = copy_forbidden (fun, fndecl);
3690 if (inline_forbidden_reason != NULL)
3691 return true;
3693 /* Next, walk the statements of the function looking for
3694 constraucts we can't handle, or are non-optimal for inlining. */
3695 hash_set<tree> visited_nodes;
3696 memset (&wi, 0, sizeof (wi));
3697 wi.info = (void *) fndecl;
3698 wi.pset = &visited_nodes;
3700 FOR_EACH_BB_FN (bb, fun)
3702 gimple ret;
3703 gimple_seq seq = bb_seq (bb);
3704 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
3705 forbidden_p = (ret != NULL);
3706 if (forbidden_p)
3707 break;
3710 return forbidden_p;
3713 /* Return false if the function FNDECL cannot be inlined on account of its
3714 attributes, true otherwise. */
3715 static bool
3716 function_attribute_inlinable_p (const_tree fndecl)
3718 if (targetm.attribute_table)
3720 const_tree a;
3722 for (a = DECL_ATTRIBUTES (fndecl); a; a = TREE_CHAIN (a))
3724 const_tree name = TREE_PURPOSE (a);
3725 int i;
3727 for (i = 0; targetm.attribute_table[i].name != NULL; i++)
3728 if (is_attribute_p (targetm.attribute_table[i].name, name))
3729 return targetm.function_attribute_inlinable_p (fndecl);
3733 return true;
3736 /* Returns nonzero if FN is a function that does not have any
3737 fundamental inline blocking properties. */
3739 bool
3740 tree_inlinable_function_p (tree fn)
3742 bool inlinable = true;
3743 bool do_warning;
3744 tree always_inline;
3746 /* If we've already decided this function shouldn't be inlined,
3747 there's no need to check again. */
3748 if (DECL_UNINLINABLE (fn))
3749 return false;
3751 /* We only warn for functions declared `inline' by the user. */
3752 do_warning = (warn_inline
3753 && DECL_DECLARED_INLINE_P (fn)
3754 && !DECL_NO_INLINE_WARNING_P (fn)
3755 && !DECL_IN_SYSTEM_HEADER (fn));
3757 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
3759 if (flag_no_inline
3760 && always_inline == NULL)
3762 if (do_warning)
3763 warning (OPT_Winline, "function %q+F can never be inlined because it "
3764 "is suppressed using -fno-inline", fn);
3765 inlinable = false;
3768 else if (!function_attribute_inlinable_p (fn))
3770 if (do_warning)
3771 warning (OPT_Winline, "function %q+F can never be inlined because it "
3772 "uses attributes conflicting with inlining", fn);
3773 inlinable = false;
3776 else if (inline_forbidden_p (fn))
3778 /* See if we should warn about uninlinable functions. Previously,
3779 some of these warnings would be issued while trying to expand
3780 the function inline, but that would cause multiple warnings
3781 about functions that would for example call alloca. But since
3782 this a property of the function, just one warning is enough.
3783 As a bonus we can now give more details about the reason why a
3784 function is not inlinable. */
3785 if (always_inline)
3786 error (inline_forbidden_reason, fn);
3787 else if (do_warning)
3788 warning (OPT_Winline, inline_forbidden_reason, fn);
3790 inlinable = false;
3793 /* Squirrel away the result so that we don't have to check again. */
3794 DECL_UNINLINABLE (fn) = !inlinable;
3796 return inlinable;
3799 /* Estimate the cost of a memory move of type TYPE. Use machine dependent
3800 word size and take possible memcpy call into account and return
3801 cost based on whether optimizing for size or speed according to SPEED_P. */
3804 estimate_move_cost (tree type, bool ARG_UNUSED (speed_p))
3806 HOST_WIDE_INT size;
3808 gcc_assert (!VOID_TYPE_P (type));
3810 if (TREE_CODE (type) == VECTOR_TYPE)
3812 machine_mode inner = TYPE_MODE (TREE_TYPE (type));
3813 machine_mode simd
3814 = targetm.vectorize.preferred_simd_mode (inner);
3815 int simd_mode_size = GET_MODE_SIZE (simd);
3816 return ((GET_MODE_SIZE (TYPE_MODE (type)) + simd_mode_size - 1)
3817 / simd_mode_size);
3820 size = int_size_in_bytes (type);
3822 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (speed_p))
3823 /* Cost of a memcpy call, 3 arguments and the call. */
3824 return 4;
3825 else
3826 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
3829 /* Returns cost of operation CODE, according to WEIGHTS */
3831 static int
3832 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3833 tree op1 ATTRIBUTE_UNUSED, tree op2)
3835 switch (code)
3837 /* These are "free" conversions, or their presumed cost
3838 is folded into other operations. */
3839 case RANGE_EXPR:
3840 CASE_CONVERT:
3841 case COMPLEX_EXPR:
3842 case PAREN_EXPR:
3843 case VIEW_CONVERT_EXPR:
3844 return 0;
3846 /* Assign cost of 1 to usual operations.
3847 ??? We may consider mapping RTL costs to this. */
3848 case COND_EXPR:
3849 case VEC_COND_EXPR:
3850 case VEC_PERM_EXPR:
3852 case PLUS_EXPR:
3853 case POINTER_PLUS_EXPR:
3854 case MINUS_EXPR:
3855 case MULT_EXPR:
3856 case MULT_HIGHPART_EXPR:
3857 case FMA_EXPR:
3859 case ADDR_SPACE_CONVERT_EXPR:
3860 case FIXED_CONVERT_EXPR:
3861 case FIX_TRUNC_EXPR:
3863 case NEGATE_EXPR:
3864 case FLOAT_EXPR:
3865 case MIN_EXPR:
3866 case MAX_EXPR:
3867 case ABS_EXPR:
3869 case LSHIFT_EXPR:
3870 case RSHIFT_EXPR:
3871 case LROTATE_EXPR:
3872 case RROTATE_EXPR:
3874 case BIT_IOR_EXPR:
3875 case BIT_XOR_EXPR:
3876 case BIT_AND_EXPR:
3877 case BIT_NOT_EXPR:
3879 case TRUTH_ANDIF_EXPR:
3880 case TRUTH_ORIF_EXPR:
3881 case TRUTH_AND_EXPR:
3882 case TRUTH_OR_EXPR:
3883 case TRUTH_XOR_EXPR:
3884 case TRUTH_NOT_EXPR:
3886 case LT_EXPR:
3887 case LE_EXPR:
3888 case GT_EXPR:
3889 case GE_EXPR:
3890 case EQ_EXPR:
3891 case NE_EXPR:
3892 case ORDERED_EXPR:
3893 case UNORDERED_EXPR:
3895 case UNLT_EXPR:
3896 case UNLE_EXPR:
3897 case UNGT_EXPR:
3898 case UNGE_EXPR:
3899 case UNEQ_EXPR:
3900 case LTGT_EXPR:
3902 case CONJ_EXPR:
3904 case PREDECREMENT_EXPR:
3905 case PREINCREMENT_EXPR:
3906 case POSTDECREMENT_EXPR:
3907 case POSTINCREMENT_EXPR:
3909 case REALIGN_LOAD_EXPR:
3911 case REDUC_MAX_EXPR:
3912 case REDUC_MIN_EXPR:
3913 case REDUC_PLUS_EXPR:
3914 case WIDEN_SUM_EXPR:
3915 case WIDEN_MULT_EXPR:
3916 case DOT_PROD_EXPR:
3917 case SAD_EXPR:
3918 case WIDEN_MULT_PLUS_EXPR:
3919 case WIDEN_MULT_MINUS_EXPR:
3920 case WIDEN_LSHIFT_EXPR:
3922 case VEC_WIDEN_MULT_HI_EXPR:
3923 case VEC_WIDEN_MULT_LO_EXPR:
3924 case VEC_WIDEN_MULT_EVEN_EXPR:
3925 case VEC_WIDEN_MULT_ODD_EXPR:
3926 case VEC_UNPACK_HI_EXPR:
3927 case VEC_UNPACK_LO_EXPR:
3928 case VEC_UNPACK_FLOAT_HI_EXPR:
3929 case VEC_UNPACK_FLOAT_LO_EXPR:
3930 case VEC_PACK_TRUNC_EXPR:
3931 case VEC_PACK_SAT_EXPR:
3932 case VEC_PACK_FIX_TRUNC_EXPR:
3933 case VEC_WIDEN_LSHIFT_HI_EXPR:
3934 case VEC_WIDEN_LSHIFT_LO_EXPR:
3936 return 1;
3938 /* Few special cases of expensive operations. This is useful
3939 to avoid inlining on functions having too many of these. */
3940 case TRUNC_DIV_EXPR:
3941 case CEIL_DIV_EXPR:
3942 case FLOOR_DIV_EXPR:
3943 case ROUND_DIV_EXPR:
3944 case EXACT_DIV_EXPR:
3945 case TRUNC_MOD_EXPR:
3946 case CEIL_MOD_EXPR:
3947 case FLOOR_MOD_EXPR:
3948 case ROUND_MOD_EXPR:
3949 case RDIV_EXPR:
3950 if (TREE_CODE (op2) != INTEGER_CST)
3951 return weights->div_mod_cost;
3952 return 1;
3954 default:
3955 /* We expect a copy assignment with no operator. */
3956 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
3957 return 0;
3962 /* Estimate number of instructions that will be created by expanding
3963 the statements in the statement sequence STMTS.
3964 WEIGHTS contains weights attributed to various constructs. */
3966 static
3967 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
3969 int cost;
3970 gimple_stmt_iterator gsi;
3972 cost = 0;
3973 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
3974 cost += estimate_num_insns (gsi_stmt (gsi), weights);
3976 return cost;
3980 /* Estimate number of instructions that will be created by expanding STMT.
3981 WEIGHTS contains weights attributed to various constructs. */
3984 estimate_num_insns (gimple stmt, eni_weights *weights)
3986 unsigned cost, i;
3987 enum gimple_code code = gimple_code (stmt);
3988 tree lhs;
3989 tree rhs;
3991 switch (code)
3993 case GIMPLE_ASSIGN:
3994 /* Try to estimate the cost of assignments. We have three cases to
3995 deal with:
3996 1) Simple assignments to registers;
3997 2) Stores to things that must live in memory. This includes
3998 "normal" stores to scalars, but also assignments of large
3999 structures, or constructors of big arrays;
4001 Let us look at the first two cases, assuming we have "a = b + C":
4002 <GIMPLE_ASSIGN <var_decl "a">
4003 <plus_expr <var_decl "b"> <constant C>>
4004 If "a" is a GIMPLE register, the assignment to it is free on almost
4005 any target, because "a" usually ends up in a real register. Hence
4006 the only cost of this expression comes from the PLUS_EXPR, and we
4007 can ignore the GIMPLE_ASSIGN.
4008 If "a" is not a GIMPLE register, the assignment to "a" will most
4009 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
4010 of moving something into "a", which we compute using the function
4011 estimate_move_cost. */
4012 if (gimple_clobber_p (stmt))
4013 return 0; /* ={v} {CLOBBER} stmt expands to nothing. */
4015 lhs = gimple_assign_lhs (stmt);
4016 rhs = gimple_assign_rhs1 (stmt);
4018 cost = 0;
4020 /* Account for the cost of moving to / from memory. */
4021 if (gimple_store_p (stmt))
4022 cost += estimate_move_cost (TREE_TYPE (lhs), weights->time_based);
4023 if (gimple_assign_load_p (stmt))
4024 cost += estimate_move_cost (TREE_TYPE (rhs), weights->time_based);
4026 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
4027 gimple_assign_rhs1 (stmt),
4028 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
4029 == GIMPLE_BINARY_RHS
4030 ? gimple_assign_rhs2 (stmt) : NULL);
4031 break;
4033 case GIMPLE_COND:
4034 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
4035 gimple_op (stmt, 0),
4036 gimple_op (stmt, 1));
4037 break;
4039 case GIMPLE_SWITCH:
4041 gswitch *switch_stmt = as_a <gswitch *> (stmt);
4042 /* Take into account cost of the switch + guess 2 conditional jumps for
4043 each case label.
4045 TODO: once the switch expansion logic is sufficiently separated, we can
4046 do better job on estimating cost of the switch. */
4047 if (weights->time_based)
4048 cost = floor_log2 (gimple_switch_num_labels (switch_stmt)) * 2;
4049 else
4050 cost = gimple_switch_num_labels (switch_stmt) * 2;
4052 break;
4054 case GIMPLE_CALL:
4056 tree decl;
4058 if (gimple_call_internal_p (stmt))
4059 return 0;
4060 else if ((decl = gimple_call_fndecl (stmt))
4061 && DECL_BUILT_IN (decl))
4063 /* Do not special case builtins where we see the body.
4064 This just confuse inliner. */
4065 struct cgraph_node *node;
4066 if (!(node = cgraph_node::get (decl))
4067 || node->definition)
4069 /* For buitins that are likely expanded to nothing or
4070 inlined do not account operand costs. */
4071 else if (is_simple_builtin (decl))
4072 return 0;
4073 else if (is_inexpensive_builtin (decl))
4074 return weights->target_builtin_call_cost;
4075 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4077 /* We canonicalize x * x to pow (x, 2.0) with -ffast-math, so
4078 specialize the cheap expansion we do here.
4079 ??? This asks for a more general solution. */
4080 switch (DECL_FUNCTION_CODE (decl))
4082 case BUILT_IN_POW:
4083 case BUILT_IN_POWF:
4084 case BUILT_IN_POWL:
4085 if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
4086 && REAL_VALUES_EQUAL
4087 (TREE_REAL_CST (gimple_call_arg (stmt, 1)), dconst2))
4088 return estimate_operator_cost
4089 (MULT_EXPR, weights, gimple_call_arg (stmt, 0),
4090 gimple_call_arg (stmt, 0));
4091 break;
4093 default:
4094 break;
4099 cost = decl ? weights->call_cost : weights->indirect_call_cost;
4100 if (gimple_call_lhs (stmt))
4101 cost += estimate_move_cost (TREE_TYPE (gimple_call_lhs (stmt)),
4102 weights->time_based);
4103 for (i = 0; i < gimple_call_num_args (stmt); i++)
4105 tree arg = gimple_call_arg (stmt, i);
4106 cost += estimate_move_cost (TREE_TYPE (arg),
4107 weights->time_based);
4109 break;
4112 case GIMPLE_RETURN:
4113 return weights->return_cost;
4115 case GIMPLE_GOTO:
4116 case GIMPLE_LABEL:
4117 case GIMPLE_NOP:
4118 case GIMPLE_PHI:
4119 case GIMPLE_PREDICT:
4120 case GIMPLE_DEBUG:
4121 return 0;
4123 case GIMPLE_ASM:
4125 int count = asm_str_count (gimple_asm_string (as_a <gasm *> (stmt)));
4126 /* 1000 means infinity. This avoids overflows later
4127 with very long asm statements. */
4128 if (count > 1000)
4129 count = 1000;
4130 return count;
4133 case GIMPLE_RESX:
4134 /* This is either going to be an external function call with one
4135 argument, or two register copy statements plus a goto. */
4136 return 2;
4138 case GIMPLE_EH_DISPATCH:
4139 /* ??? This is going to turn into a switch statement. Ideally
4140 we'd have a look at the eh region and estimate the number of
4141 edges involved. */
4142 return 10;
4144 case GIMPLE_BIND:
4145 return estimate_num_insns_seq (
4146 gimple_bind_body (as_a <gbind *> (stmt)),
4147 weights);
4149 case GIMPLE_EH_FILTER:
4150 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
4152 case GIMPLE_CATCH:
4153 return estimate_num_insns_seq (gimple_catch_handler (
4154 as_a <gcatch *> (stmt)),
4155 weights);
4157 case GIMPLE_TRY:
4158 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
4159 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
4161 /* OpenMP directives are generally very expensive. */
4163 case GIMPLE_OMP_RETURN:
4164 case GIMPLE_OMP_SECTIONS_SWITCH:
4165 case GIMPLE_OMP_ATOMIC_STORE:
4166 case GIMPLE_OMP_CONTINUE:
4167 /* ...except these, which are cheap. */
4168 return 0;
4170 case GIMPLE_OMP_ATOMIC_LOAD:
4171 return weights->omp_cost;
4173 case GIMPLE_OMP_FOR:
4174 return (weights->omp_cost
4175 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
4176 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
4178 case GIMPLE_OMP_PARALLEL:
4179 case GIMPLE_OMP_TASK:
4180 case GIMPLE_OMP_CRITICAL:
4181 case GIMPLE_OMP_MASTER:
4182 case GIMPLE_OMP_TASKGROUP:
4183 case GIMPLE_OMP_ORDERED:
4184 case GIMPLE_OMP_SECTION:
4185 case GIMPLE_OMP_SECTIONS:
4186 case GIMPLE_OMP_SINGLE:
4187 case GIMPLE_OMP_TARGET:
4188 case GIMPLE_OMP_TEAMS:
4189 return (weights->omp_cost
4190 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
4192 case GIMPLE_TRANSACTION:
4193 return (weights->tm_cost
4194 + estimate_num_insns_seq (gimple_transaction_body (
4195 as_a <gtransaction *> (stmt)),
4196 weights));
4198 default:
4199 gcc_unreachable ();
4202 return cost;
4205 /* Estimate number of instructions that will be created by expanding
4206 function FNDECL. WEIGHTS contains weights attributed to various
4207 constructs. */
4210 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
4212 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
4213 gimple_stmt_iterator bsi;
4214 basic_block bb;
4215 int n = 0;
4217 gcc_assert (my_function && my_function->cfg);
4218 FOR_EACH_BB_FN (bb, my_function)
4220 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4221 n += estimate_num_insns (gsi_stmt (bsi), weights);
4224 return n;
4228 /* Initializes weights used by estimate_num_insns. */
4230 void
4231 init_inline_once (void)
4233 eni_size_weights.call_cost = 1;
4234 eni_size_weights.indirect_call_cost = 3;
4235 eni_size_weights.target_builtin_call_cost = 1;
4236 eni_size_weights.div_mod_cost = 1;
4237 eni_size_weights.omp_cost = 40;
4238 eni_size_weights.tm_cost = 10;
4239 eni_size_weights.time_based = false;
4240 eni_size_weights.return_cost = 1;
4242 /* Estimating time for call is difficult, since we have no idea what the
4243 called function does. In the current uses of eni_time_weights,
4244 underestimating the cost does less harm than overestimating it, so
4245 we choose a rather small value here. */
4246 eni_time_weights.call_cost = 10;
4247 eni_time_weights.indirect_call_cost = 15;
4248 eni_time_weights.target_builtin_call_cost = 1;
4249 eni_time_weights.div_mod_cost = 10;
4250 eni_time_weights.omp_cost = 40;
4251 eni_time_weights.tm_cost = 40;
4252 eni_time_weights.time_based = true;
4253 eni_time_weights.return_cost = 2;
4256 /* Estimate the number of instructions in a gimple_seq. */
4259 count_insns_seq (gimple_seq seq, eni_weights *weights)
4261 gimple_stmt_iterator gsi;
4262 int n = 0;
4263 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
4264 n += estimate_num_insns (gsi_stmt (gsi), weights);
4266 return n;
4270 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
4272 static void
4273 prepend_lexical_block (tree current_block, tree new_block)
4275 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
4276 BLOCK_SUBBLOCKS (current_block) = new_block;
4277 BLOCK_SUPERCONTEXT (new_block) = current_block;
4280 /* Add local variables from CALLEE to CALLER. */
4282 static inline void
4283 add_local_variables (struct function *callee, struct function *caller,
4284 copy_body_data *id)
4286 tree var;
4287 unsigned ix;
4289 FOR_EACH_LOCAL_DECL (callee, ix, var)
4290 if (!can_be_nonlocal (var, id))
4292 tree new_var = remap_decl (var, id);
4294 /* Remap debug-expressions. */
4295 if (TREE_CODE (new_var) == VAR_DECL
4296 && DECL_HAS_DEBUG_EXPR_P (var)
4297 && new_var != var)
4299 tree tem = DECL_DEBUG_EXPR (var);
4300 bool old_regimplify = id->regimplify;
4301 id->remapping_type_depth++;
4302 walk_tree (&tem, copy_tree_body_r, id, NULL);
4303 id->remapping_type_depth--;
4304 id->regimplify = old_regimplify;
4305 SET_DECL_DEBUG_EXPR (new_var, tem);
4306 DECL_HAS_DEBUG_EXPR_P (new_var) = 1;
4308 add_local_decl (caller, new_var);
4312 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
4314 static bool
4315 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
4317 tree use_retvar;
4318 tree fn;
4319 hash_map<tree, tree> *dst;
4320 hash_map<tree, tree> *st = NULL;
4321 tree return_slot;
4322 tree modify_dest;
4323 tree return_bounds = NULL;
4324 location_t saved_location;
4325 struct cgraph_edge *cg_edge;
4326 cgraph_inline_failed_t reason;
4327 basic_block return_block;
4328 edge e;
4329 gimple_stmt_iterator gsi, stmt_gsi;
4330 bool successfully_inlined = FALSE;
4331 bool purge_dead_abnormal_edges;
4332 gcall *call_stmt;
4333 unsigned int i;
4335 /* Set input_location here so we get the right instantiation context
4336 if we call instantiate_decl from inlinable_function_p. */
4337 /* FIXME: instantiate_decl isn't called by inlinable_function_p. */
4338 saved_location = input_location;
4339 input_location = gimple_location (stmt);
4341 /* From here on, we're only interested in CALL_EXPRs. */
4342 call_stmt = dyn_cast <gcall *> (stmt);
4343 if (!call_stmt)
4344 goto egress;
4346 cg_edge = id->dst_node->get_edge (stmt);
4347 gcc_checking_assert (cg_edge);
4348 /* First, see if we can figure out what function is being called.
4349 If we cannot, then there is no hope of inlining the function. */
4350 if (cg_edge->indirect_unknown_callee)
4351 goto egress;
4352 fn = cg_edge->callee->decl;
4353 gcc_checking_assert (fn);
4355 /* If FN is a declaration of a function in a nested scope that was
4356 globally declared inline, we don't set its DECL_INITIAL.
4357 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
4358 C++ front-end uses it for cdtors to refer to their internal
4359 declarations, that are not real functions. Fortunately those
4360 don't have trees to be saved, so we can tell by checking their
4361 gimple_body. */
4362 if (!DECL_INITIAL (fn)
4363 && DECL_ABSTRACT_ORIGIN (fn)
4364 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
4365 fn = DECL_ABSTRACT_ORIGIN (fn);
4367 /* Don't try to inline functions that are not well-suited to inlining. */
4368 if (cg_edge->inline_failed)
4370 reason = cg_edge->inline_failed;
4371 /* If this call was originally indirect, we do not want to emit any
4372 inlining related warnings or sorry messages because there are no
4373 guarantees regarding those. */
4374 if (cg_edge->indirect_inlining_edge)
4375 goto egress;
4377 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
4378 /* For extern inline functions that get redefined we always
4379 silently ignored always_inline flag. Better behaviour would
4380 be to be able to keep both bodies and use extern inline body
4381 for inlining, but we can't do that because frontends overwrite
4382 the body. */
4383 && !cg_edge->callee->local.redefined_extern_inline
4384 /* During early inline pass, report only when optimization is
4385 not turned on. */
4386 && (symtab->global_info_ready
4387 || !optimize
4388 || cgraph_inline_failed_type (reason) == CIF_FINAL_ERROR)
4389 /* PR 20090218-1_0.c. Body can be provided by another module. */
4390 && (reason != CIF_BODY_NOT_AVAILABLE || !flag_generate_lto))
4392 error ("inlining failed in call to always_inline %q+F: %s", fn,
4393 cgraph_inline_failed_string (reason));
4394 error ("called from here");
4396 else if (warn_inline
4397 && DECL_DECLARED_INLINE_P (fn)
4398 && !DECL_NO_INLINE_WARNING_P (fn)
4399 && !DECL_IN_SYSTEM_HEADER (fn)
4400 && reason != CIF_UNSPECIFIED
4401 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
4402 /* Do not warn about not inlined recursive calls. */
4403 && !cg_edge->recursive_p ()
4404 /* Avoid warnings during early inline pass. */
4405 && symtab->global_info_ready)
4407 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
4408 fn, _(cgraph_inline_failed_string (reason)));
4409 warning (OPT_Winline, "called from here");
4411 goto egress;
4413 fn = cg_edge->callee->decl;
4414 cg_edge->callee->get_untransformed_body ();
4416 #ifdef ENABLE_CHECKING
4417 if (cg_edge->callee->decl != id->dst_node->decl)
4418 cg_edge->callee->verify ();
4419 #endif
4421 /* We will be inlining this callee. */
4422 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
4423 id->assign_stmts.create (0);
4425 /* Update the callers EH personality. */
4426 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
4427 DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
4428 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
4430 /* Split the block holding the GIMPLE_CALL. */
4431 e = split_block (bb, stmt);
4432 bb = e->src;
4433 return_block = e->dest;
4434 remove_edge (e);
4436 /* split_block splits after the statement; work around this by
4437 moving the call into the second block manually. Not pretty,
4438 but seems easier than doing the CFG manipulation by hand
4439 when the GIMPLE_CALL is in the last statement of BB. */
4440 stmt_gsi = gsi_last_bb (bb);
4441 gsi_remove (&stmt_gsi, false);
4443 /* If the GIMPLE_CALL was in the last statement of BB, it may have
4444 been the source of abnormal edges. In this case, schedule
4445 the removal of dead abnormal edges. */
4446 gsi = gsi_start_bb (return_block);
4447 if (gsi_end_p (gsi))
4449 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
4450 purge_dead_abnormal_edges = true;
4452 else
4454 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
4455 purge_dead_abnormal_edges = false;
4458 stmt_gsi = gsi_start_bb (return_block);
4460 /* Build a block containing code to initialize the arguments, the
4461 actual inline expansion of the body, and a label for the return
4462 statements within the function to jump to. The type of the
4463 statement expression is the return type of the function call.
4464 ??? If the call does not have an associated block then we will
4465 remap all callee blocks to NULL, effectively dropping most of
4466 its debug information. This should only happen for calls to
4467 artificial decls inserted by the compiler itself. We need to
4468 either link the inlined blocks into the caller block tree or
4469 not refer to them in any way to not break GC for locations. */
4470 if (gimple_block (stmt))
4472 id->block = make_node (BLOCK);
4473 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
4474 BLOCK_SOURCE_LOCATION (id->block) = LOCATION_LOCUS (input_location);
4475 prepend_lexical_block (gimple_block (stmt), id->block);
4478 /* Local declarations will be replaced by their equivalents in this
4479 map. */
4480 st = id->decl_map;
4481 id->decl_map = new hash_map<tree, tree>;
4482 dst = id->debug_map;
4483 id->debug_map = NULL;
4485 /* Record the function we are about to inline. */
4486 id->src_fn = fn;
4487 id->src_node = cg_edge->callee;
4488 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
4489 id->call_stmt = stmt;
4491 gcc_assert (!id->src_cfun->after_inlining);
4493 id->entry_bb = bb;
4494 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
4496 gimple_stmt_iterator si = gsi_last_bb (bb);
4497 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
4498 NOT_TAKEN),
4499 GSI_NEW_STMT);
4501 initialize_inlined_parameters (id, stmt, fn, bb);
4503 if (DECL_INITIAL (fn))
4505 if (gimple_block (stmt))
4507 tree *var;
4509 prepend_lexical_block (id->block,
4510 remap_blocks (DECL_INITIAL (fn), id));
4511 gcc_checking_assert (BLOCK_SUBBLOCKS (id->block)
4512 && (BLOCK_CHAIN (BLOCK_SUBBLOCKS (id->block))
4513 == NULL_TREE));
4514 /* Move vars for PARM_DECLs from DECL_INITIAL block to id->block,
4515 otherwise for DWARF DW_TAG_formal_parameter will not be children of
4516 DW_TAG_inlined_subroutine, but of a DW_TAG_lexical_block
4517 under it. The parameters can be then evaluated in the debugger,
4518 but don't show in backtraces. */
4519 for (var = &BLOCK_VARS (BLOCK_SUBBLOCKS (id->block)); *var; )
4520 if (TREE_CODE (DECL_ORIGIN (*var)) == PARM_DECL)
4522 tree v = *var;
4523 *var = TREE_CHAIN (v);
4524 TREE_CHAIN (v) = BLOCK_VARS (id->block);
4525 BLOCK_VARS (id->block) = v;
4527 else
4528 var = &TREE_CHAIN (*var);
4530 else
4531 remap_blocks_to_null (DECL_INITIAL (fn), id);
4534 /* Return statements in the function body will be replaced by jumps
4535 to the RET_LABEL. */
4536 gcc_assert (DECL_INITIAL (fn));
4537 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
4539 /* Find the LHS to which the result of this call is assigned. */
4540 return_slot = NULL;
4541 if (gimple_call_lhs (stmt))
4543 modify_dest = gimple_call_lhs (stmt);
4545 /* Remember where to copy returned bounds. */
4546 if (gimple_call_with_bounds_p (stmt)
4547 && TREE_CODE (modify_dest) == SSA_NAME)
4549 gcall *retbnd = chkp_retbnd_call_by_val (modify_dest);
4550 if (retbnd)
4552 return_bounds = gimple_call_lhs (retbnd);
4553 /* If returned bounds are not used then just
4554 remove unused call. */
4555 if (!return_bounds)
4557 gimple_stmt_iterator iter = gsi_for_stmt (retbnd);
4558 gsi_remove (&iter, true);
4563 /* The function which we are inlining might not return a value,
4564 in which case we should issue a warning that the function
4565 does not return a value. In that case the optimizers will
4566 see that the variable to which the value is assigned was not
4567 initialized. We do not want to issue a warning about that
4568 uninitialized variable. */
4569 if (DECL_P (modify_dest))
4570 TREE_NO_WARNING (modify_dest) = 1;
4572 if (gimple_call_return_slot_opt_p (call_stmt))
4574 return_slot = modify_dest;
4575 modify_dest = NULL;
4578 else
4579 modify_dest = NULL;
4581 /* If we are inlining a call to the C++ operator new, we don't want
4582 to use type based alias analysis on the return value. Otherwise
4583 we may get confused if the compiler sees that the inlined new
4584 function returns a pointer which was just deleted. See bug
4585 33407. */
4586 if (DECL_IS_OPERATOR_NEW (fn))
4588 return_slot = NULL;
4589 modify_dest = NULL;
4592 /* Declare the return variable for the function. */
4593 use_retvar = declare_return_variable (id, return_slot, modify_dest,
4594 return_bounds, bb);
4596 /* Add local vars in this inlined callee to caller. */
4597 add_local_variables (id->src_cfun, cfun, id);
4599 if (dump_file && (dump_flags & TDF_DETAILS))
4601 fprintf (dump_file, "Inlining ");
4602 print_generic_expr (dump_file, id->src_fn, 0);
4603 fprintf (dump_file, " to ");
4604 print_generic_expr (dump_file, id->dst_fn, 0);
4605 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
4608 /* This is it. Duplicate the callee body. Assume callee is
4609 pre-gimplified. Note that we must not alter the caller
4610 function in any way before this point, as this CALL_EXPR may be
4611 a self-referential call; if we're calling ourselves, we need to
4612 duplicate our body before altering anything. */
4613 copy_body (id, cg_edge->callee->count,
4614 GCOV_COMPUTE_SCALE (cg_edge->frequency, CGRAPH_FREQ_BASE),
4615 bb, return_block, NULL);
4617 /* Reset the escaped solution. */
4618 if (cfun->gimple_df)
4619 pt_solution_reset (&cfun->gimple_df->escaped);
4621 /* Clean up. */
4622 if (id->debug_map)
4624 delete id->debug_map;
4625 id->debug_map = dst;
4627 delete id->decl_map;
4628 id->decl_map = st;
4630 /* Unlink the calls virtual operands before replacing it. */
4631 unlink_stmt_vdef (stmt);
4632 if (gimple_vdef (stmt)
4633 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
4634 release_ssa_name (gimple_vdef (stmt));
4636 /* If the inlined function returns a result that we care about,
4637 substitute the GIMPLE_CALL with an assignment of the return
4638 variable to the LHS of the call. That is, if STMT was
4639 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
4640 if (use_retvar && gimple_call_lhs (stmt))
4642 gimple old_stmt = stmt;
4643 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
4644 gsi_replace (&stmt_gsi, stmt, false);
4645 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
4647 /* Copy bounds if we copy structure with bounds. */
4648 if (chkp_function_instrumented_p (id->dst_fn)
4649 && !BOUNDED_P (use_retvar)
4650 && chkp_type_has_pointer (TREE_TYPE (use_retvar)))
4651 id->assign_stmts.safe_push (stmt);
4653 else
4655 /* Handle the case of inlining a function with no return
4656 statement, which causes the return value to become undefined. */
4657 if (gimple_call_lhs (stmt)
4658 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
4660 tree name = gimple_call_lhs (stmt);
4661 tree var = SSA_NAME_VAR (name);
4662 tree def = ssa_default_def (cfun, var);
4664 if (def)
4666 /* If the variable is used undefined, make this name
4667 undefined via a move. */
4668 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
4669 gsi_replace (&stmt_gsi, stmt, true);
4671 else
4673 /* Otherwise make this variable undefined. */
4674 gsi_remove (&stmt_gsi, true);
4675 set_ssa_default_def (cfun, var, name);
4676 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
4679 else
4680 gsi_remove (&stmt_gsi, true);
4683 /* Put returned bounds into the correct place if required. */
4684 if (return_bounds)
4686 gimple old_stmt = SSA_NAME_DEF_STMT (return_bounds);
4687 gimple new_stmt = gimple_build_assign (return_bounds, id->retbnd);
4688 gimple_stmt_iterator bnd_gsi = gsi_for_stmt (old_stmt);
4689 unlink_stmt_vdef (old_stmt);
4690 gsi_replace (&bnd_gsi, new_stmt, false);
4691 maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt);
4692 cgraph_update_edges_for_call_stmt (old_stmt,
4693 gimple_call_fndecl (old_stmt),
4694 new_stmt);
4697 if (purge_dead_abnormal_edges)
4699 gimple_purge_dead_eh_edges (return_block);
4700 gimple_purge_dead_abnormal_call_edges (return_block);
4703 /* If the value of the new expression is ignored, that's OK. We
4704 don't warn about this for CALL_EXPRs, so we shouldn't warn about
4705 the equivalent inlined version either. */
4706 if (is_gimple_assign (stmt))
4708 gcc_assert (gimple_assign_single_p (stmt)
4709 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
4710 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
4713 /* Copy bounds for all generated assigns that need it. */
4714 for (i = 0; i < id->assign_stmts.length (); i++)
4715 chkp_copy_bounds_for_assign (id->assign_stmts[i], cg_edge);
4716 id->assign_stmts.release ();
4718 /* Output the inlining info for this abstract function, since it has been
4719 inlined. If we don't do this now, we can lose the information about the
4720 variables in the function when the blocks get blown away as soon as we
4721 remove the cgraph node. */
4722 if (gimple_block (stmt))
4723 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
4725 /* Update callgraph if needed. */
4726 cg_edge->callee->remove ();
4728 id->block = NULL_TREE;
4729 successfully_inlined = TRUE;
4731 egress:
4732 input_location = saved_location;
4733 return successfully_inlined;
4736 /* Expand call statements reachable from STMT_P.
4737 We can only have CALL_EXPRs as the "toplevel" tree code or nested
4738 in a MODIFY_EXPR. */
4740 static bool
4741 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
4743 gimple_stmt_iterator gsi;
4745 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4747 gimple stmt = gsi_stmt (gsi);
4749 if (is_gimple_call (stmt)
4750 && !gimple_call_internal_p (stmt)
4751 && expand_call_inline (bb, stmt, id))
4752 return true;
4755 return false;
4759 /* Walk all basic blocks created after FIRST and try to fold every statement
4760 in the STATEMENTS pointer set. */
4762 static void
4763 fold_marked_statements (int first, hash_set<gimple> *statements)
4765 for (; first < n_basic_blocks_for_fn (cfun); first++)
4766 if (BASIC_BLOCK_FOR_FN (cfun, first))
4768 gimple_stmt_iterator gsi;
4770 for (gsi = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
4771 !gsi_end_p (gsi);
4772 gsi_next (&gsi))
4773 if (statements->contains (gsi_stmt (gsi)))
4775 gimple old_stmt = gsi_stmt (gsi);
4776 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
4778 if (old_decl && DECL_BUILT_IN (old_decl))
4780 /* Folding builtins can create multiple instructions,
4781 we need to look at all of them. */
4782 gimple_stmt_iterator i2 = gsi;
4783 gsi_prev (&i2);
4784 if (fold_stmt (&gsi))
4786 gimple new_stmt;
4787 /* If a builtin at the end of a bb folded into nothing,
4788 the following loop won't work. */
4789 if (gsi_end_p (gsi))
4791 cgraph_update_edges_for_call_stmt (old_stmt,
4792 old_decl, NULL);
4793 break;
4795 if (gsi_end_p (i2))
4796 i2 = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
4797 else
4798 gsi_next (&i2);
4799 while (1)
4801 new_stmt = gsi_stmt (i2);
4802 update_stmt (new_stmt);
4803 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4804 new_stmt);
4806 if (new_stmt == gsi_stmt (gsi))
4808 /* It is okay to check only for the very last
4809 of these statements. If it is a throwing
4810 statement nothing will change. If it isn't
4811 this can remove EH edges. If that weren't
4812 correct then because some intermediate stmts
4813 throw, but not the last one. That would mean
4814 we'd have to split the block, which we can't
4815 here and we'd loose anyway. And as builtins
4816 probably never throw, this all
4817 is mood anyway. */
4818 if (maybe_clean_or_replace_eh_stmt (old_stmt,
4819 new_stmt))
4820 gimple_purge_dead_eh_edges (
4821 BASIC_BLOCK_FOR_FN (cfun, first));
4822 break;
4824 gsi_next (&i2);
4828 else if (fold_stmt (&gsi))
4830 /* Re-read the statement from GSI as fold_stmt() may
4831 have changed it. */
4832 gimple new_stmt = gsi_stmt (gsi);
4833 update_stmt (new_stmt);
4835 if (is_gimple_call (old_stmt)
4836 || is_gimple_call (new_stmt))
4837 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4838 new_stmt);
4840 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
4841 gimple_purge_dead_eh_edges (BASIC_BLOCK_FOR_FN (cfun,
4842 first));
4848 /* Expand calls to inline functions in the body of FN. */
4850 unsigned int
4851 optimize_inline_calls (tree fn)
4853 copy_body_data id;
4854 basic_block bb;
4855 int last = n_basic_blocks_for_fn (cfun);
4856 bool inlined_p = false;
4858 /* Clear out ID. */
4859 memset (&id, 0, sizeof (id));
4861 id.src_node = id.dst_node = cgraph_node::get (fn);
4862 gcc_assert (id.dst_node->definition);
4863 id.dst_fn = fn;
4864 /* Or any functions that aren't finished yet. */
4865 if (current_function_decl)
4866 id.dst_fn = current_function_decl;
4868 id.copy_decl = copy_decl_maybe_to_var;
4869 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4870 id.transform_new_cfg = false;
4871 id.transform_return_to_modify = true;
4872 id.transform_parameter = true;
4873 id.transform_lang_insert_block = NULL;
4874 id.statements_to_fold = new hash_set<gimple>;
4876 push_gimplify_context ();
4878 /* We make no attempts to keep dominance info up-to-date. */
4879 free_dominance_info (CDI_DOMINATORS);
4880 free_dominance_info (CDI_POST_DOMINATORS);
4882 /* Register specific gimple functions. */
4883 gimple_register_cfg_hooks ();
4885 /* Reach the trees by walking over the CFG, and note the
4886 enclosing basic-blocks in the call edges. */
4887 /* We walk the blocks going forward, because inlined function bodies
4888 will split id->current_basic_block, and the new blocks will
4889 follow it; we'll trudge through them, processing their CALL_EXPRs
4890 along the way. */
4891 FOR_EACH_BB_FN (bb, cfun)
4892 inlined_p |= gimple_expand_calls_inline (bb, &id);
4894 pop_gimplify_context (NULL);
4896 #ifdef ENABLE_CHECKING
4898 struct cgraph_edge *e;
4900 id.dst_node->verify ();
4902 /* Double check that we inlined everything we are supposed to inline. */
4903 for (e = id.dst_node->callees; e; e = e->next_callee)
4904 gcc_assert (e->inline_failed);
4906 #endif
4908 /* Fold queued statements. */
4909 fold_marked_statements (last, id.statements_to_fold);
4910 delete id.statements_to_fold;
4912 gcc_assert (!id.debug_stmts.exists ());
4914 /* If we didn't inline into the function there is nothing to do. */
4915 if (!inlined_p)
4916 return 0;
4918 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4919 number_blocks (fn);
4921 delete_unreachable_blocks_update_callgraph (&id);
4922 #ifdef ENABLE_CHECKING
4923 id.dst_node->verify ();
4924 #endif
4926 /* It would be nice to check SSA/CFG/statement consistency here, but it is
4927 not possible yet - the IPA passes might make various functions to not
4928 throw and they don't care to proactively update local EH info. This is
4929 done later in fixup_cfg pass that also execute the verification. */
4930 return (TODO_update_ssa
4931 | TODO_cleanup_cfg
4932 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
4933 | (gimple_in_ssa_p (cfun) ? TODO_update_address_taken : 0)
4934 | (profile_status_for_fn (cfun) != PROFILE_ABSENT
4935 ? TODO_rebuild_frequencies : 0));
4938 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4940 tree
4941 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4943 enum tree_code code = TREE_CODE (*tp);
4944 enum tree_code_class cl = TREE_CODE_CLASS (code);
4946 /* We make copies of most nodes. */
4947 if (IS_EXPR_CODE_CLASS (cl)
4948 || code == TREE_LIST
4949 || code == TREE_VEC
4950 || code == TYPE_DECL
4951 || code == OMP_CLAUSE)
4953 /* Because the chain gets clobbered when we make a copy, we save it
4954 here. */
4955 tree chain = NULL_TREE, new_tree;
4957 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
4958 chain = TREE_CHAIN (*tp);
4960 /* Copy the node. */
4961 new_tree = copy_node (*tp);
4963 *tp = new_tree;
4965 /* Now, restore the chain, if appropriate. That will cause
4966 walk_tree to walk into the chain as well. */
4967 if (code == PARM_DECL
4968 || code == TREE_LIST
4969 || code == OMP_CLAUSE)
4970 TREE_CHAIN (*tp) = chain;
4972 /* For now, we don't update BLOCKs when we make copies. So, we
4973 have to nullify all BIND_EXPRs. */
4974 if (TREE_CODE (*tp) == BIND_EXPR)
4975 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
4977 else if (code == CONSTRUCTOR)
4979 /* CONSTRUCTOR nodes need special handling because
4980 we need to duplicate the vector of elements. */
4981 tree new_tree;
4983 new_tree = copy_node (*tp);
4984 CONSTRUCTOR_ELTS (new_tree) = vec_safe_copy (CONSTRUCTOR_ELTS (*tp));
4985 *tp = new_tree;
4987 else if (code == STATEMENT_LIST)
4988 /* We used to just abort on STATEMENT_LIST, but we can run into them
4989 with statement-expressions (c++/40975). */
4990 copy_statement_list (tp);
4991 else if (TREE_CODE_CLASS (code) == tcc_type)
4992 *walk_subtrees = 0;
4993 else if (TREE_CODE_CLASS (code) == tcc_declaration)
4994 *walk_subtrees = 0;
4995 else if (TREE_CODE_CLASS (code) == tcc_constant)
4996 *walk_subtrees = 0;
4997 return NULL_TREE;
5000 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
5001 information indicating to what new SAVE_EXPR this one should be mapped,
5002 use that one. Otherwise, create a new node and enter it in ST. FN is
5003 the function into which the copy will be placed. */
5005 static void
5006 remap_save_expr (tree *tp, hash_map<tree, tree> *st, int *walk_subtrees)
5008 tree *n;
5009 tree t;
5011 /* See if we already encountered this SAVE_EXPR. */
5012 n = st->get (*tp);
5014 /* If we didn't already remap this SAVE_EXPR, do so now. */
5015 if (!n)
5017 t = copy_node (*tp);
5019 /* Remember this SAVE_EXPR. */
5020 st->put (*tp, t);
5021 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
5022 st->put (t, t);
5024 else
5026 /* We've already walked into this SAVE_EXPR; don't do it again. */
5027 *walk_subtrees = 0;
5028 t = *n;
5031 /* Replace this SAVE_EXPR with the copy. */
5032 *tp = t;
5035 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
5036 label, copies the declaration and enters it in the splay_tree in DATA (which
5037 is really a 'copy_body_data *'. */
5039 static tree
5040 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
5041 bool *handled_ops_p ATTRIBUTE_UNUSED,
5042 struct walk_stmt_info *wi)
5044 copy_body_data *id = (copy_body_data *) wi->info;
5045 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsip));
5047 if (stmt)
5049 tree decl = gimple_label_label (stmt);
5051 /* Copy the decl and remember the copy. */
5052 insert_decl_map (id, decl, id->copy_decl (decl, id));
5055 return NULL_TREE;
5059 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
5060 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
5061 remaps all local declarations to appropriate replacements in gimple
5062 operands. */
5064 static tree
5065 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
5067 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
5068 copy_body_data *id = (copy_body_data *) wi->info;
5069 hash_map<tree, tree> *st = id->decl_map;
5070 tree *n;
5071 tree expr = *tp;
5073 /* Only a local declaration (variable or label). */
5074 if ((TREE_CODE (expr) == VAR_DECL
5075 && !TREE_STATIC (expr))
5076 || TREE_CODE (expr) == LABEL_DECL)
5078 /* Lookup the declaration. */
5079 n = st->get (expr);
5081 /* If it's there, remap it. */
5082 if (n)
5083 *tp = *n;
5084 *walk_subtrees = 0;
5086 else if (TREE_CODE (expr) == STATEMENT_LIST
5087 || TREE_CODE (expr) == BIND_EXPR
5088 || TREE_CODE (expr) == SAVE_EXPR)
5089 gcc_unreachable ();
5090 else if (TREE_CODE (expr) == TARGET_EXPR)
5092 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
5093 It's OK for this to happen if it was part of a subtree that
5094 isn't immediately expanded, such as operand 2 of another
5095 TARGET_EXPR. */
5096 if (!TREE_OPERAND (expr, 1))
5098 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
5099 TREE_OPERAND (expr, 3) = NULL_TREE;
5103 /* Keep iterating. */
5104 return NULL_TREE;
5108 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
5109 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
5110 remaps all local declarations to appropriate replacements in gimple
5111 statements. */
5113 static tree
5114 replace_locals_stmt (gimple_stmt_iterator *gsip,
5115 bool *handled_ops_p ATTRIBUTE_UNUSED,
5116 struct walk_stmt_info *wi)
5118 copy_body_data *id = (copy_body_data *) wi->info;
5119 gimple gs = gsi_stmt (*gsip);
5121 if (gbind *stmt = dyn_cast <gbind *> (gs))
5123 tree block = gimple_bind_block (stmt);
5125 if (block)
5127 remap_block (&block, id);
5128 gimple_bind_set_block (stmt, block);
5131 /* This will remap a lot of the same decls again, but this should be
5132 harmless. */
5133 if (gimple_bind_vars (stmt))
5134 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt),
5135 NULL, id));
5138 /* Keep iterating. */
5139 return NULL_TREE;
5143 /* Copies everything in SEQ and replaces variables and labels local to
5144 current_function_decl. */
5146 gimple_seq
5147 copy_gimple_seq_and_replace_locals (gimple_seq seq)
5149 copy_body_data id;
5150 struct walk_stmt_info wi;
5151 gimple_seq copy;
5153 /* There's nothing to do for NULL_TREE. */
5154 if (seq == NULL)
5155 return seq;
5157 /* Set up ID. */
5158 memset (&id, 0, sizeof (id));
5159 id.src_fn = current_function_decl;
5160 id.dst_fn = current_function_decl;
5161 id.decl_map = new hash_map<tree, tree>;
5162 id.debug_map = NULL;
5164 id.copy_decl = copy_decl_no_change;
5165 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5166 id.transform_new_cfg = false;
5167 id.transform_return_to_modify = false;
5168 id.transform_parameter = false;
5169 id.transform_lang_insert_block = NULL;
5171 /* Walk the tree once to find local labels. */
5172 memset (&wi, 0, sizeof (wi));
5173 hash_set<tree> visited;
5174 wi.info = &id;
5175 wi.pset = &visited;
5176 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
5178 copy = gimple_seq_copy (seq);
5180 /* Walk the copy, remapping decls. */
5181 memset (&wi, 0, sizeof (wi));
5182 wi.info = &id;
5183 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
5185 /* Clean up. */
5186 delete id.decl_map;
5187 if (id.debug_map)
5188 delete id.debug_map;
5189 if (id.dependence_map)
5191 delete id.dependence_map;
5192 id.dependence_map = NULL;
5195 return copy;
5199 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
5201 static tree
5202 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
5204 if (*tp == data)
5205 return (tree) data;
5206 else
5207 return NULL;
5210 DEBUG_FUNCTION bool
5211 debug_find_tree (tree top, tree search)
5213 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
5217 /* Declare the variables created by the inliner. Add all the variables in
5218 VARS to BIND_EXPR. */
5220 static void
5221 declare_inline_vars (tree block, tree vars)
5223 tree t;
5224 for (t = vars; t; t = DECL_CHAIN (t))
5226 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
5227 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
5228 add_local_decl (cfun, t);
5231 if (block)
5232 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
5235 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
5236 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
5237 VAR_DECL translation. */
5239 static tree
5240 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
5242 /* Don't generate debug information for the copy if we wouldn't have
5243 generated it for the copy either. */
5244 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
5245 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
5247 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
5248 declaration inspired this copy. */
5249 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
5251 /* The new variable/label has no RTL, yet. */
5252 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
5253 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
5254 SET_DECL_RTL (copy, 0);
5256 /* These args would always appear unused, if not for this. */
5257 TREE_USED (copy) = 1;
5259 /* Set the context for the new declaration. */
5260 if (!DECL_CONTEXT (decl))
5261 /* Globals stay global. */
5263 else if (DECL_CONTEXT (decl) != id->src_fn)
5264 /* Things that weren't in the scope of the function we're inlining
5265 from aren't in the scope we're inlining to, either. */
5267 else if (TREE_STATIC (decl))
5268 /* Function-scoped static variables should stay in the original
5269 function. */
5271 else
5272 /* Ordinary automatic local variables are now in the scope of the
5273 new function. */
5274 DECL_CONTEXT (copy) = id->dst_fn;
5276 return copy;
5279 static tree
5280 copy_decl_to_var (tree decl, copy_body_data *id)
5282 tree copy, type;
5284 gcc_assert (TREE_CODE (decl) == PARM_DECL
5285 || TREE_CODE (decl) == RESULT_DECL);
5287 type = TREE_TYPE (decl);
5289 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
5290 VAR_DECL, DECL_NAME (decl), type);
5291 if (DECL_PT_UID_SET_P (decl))
5292 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
5293 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
5294 TREE_READONLY (copy) = TREE_READONLY (decl);
5295 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
5296 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
5298 return copy_decl_for_dup_finish (id, decl, copy);
5301 /* Like copy_decl_to_var, but create a return slot object instead of a
5302 pointer variable for return by invisible reference. */
5304 static tree
5305 copy_result_decl_to_var (tree decl, copy_body_data *id)
5307 tree copy, type;
5309 gcc_assert (TREE_CODE (decl) == PARM_DECL
5310 || TREE_CODE (decl) == RESULT_DECL);
5312 type = TREE_TYPE (decl);
5313 if (DECL_BY_REFERENCE (decl))
5314 type = TREE_TYPE (type);
5316 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
5317 VAR_DECL, DECL_NAME (decl), type);
5318 if (DECL_PT_UID_SET_P (decl))
5319 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
5320 TREE_READONLY (copy) = TREE_READONLY (decl);
5321 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
5322 if (!DECL_BY_REFERENCE (decl))
5324 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
5325 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
5328 return copy_decl_for_dup_finish (id, decl, copy);
5331 tree
5332 copy_decl_no_change (tree decl, copy_body_data *id)
5334 tree copy;
5336 copy = copy_node (decl);
5338 /* The COPY is not abstract; it will be generated in DST_FN. */
5339 DECL_ABSTRACT_P (copy) = false;
5340 lang_hooks.dup_lang_specific_decl (copy);
5342 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
5343 been taken; it's for internal bookkeeping in expand_goto_internal. */
5344 if (TREE_CODE (copy) == LABEL_DECL)
5346 TREE_ADDRESSABLE (copy) = 0;
5347 LABEL_DECL_UID (copy) = -1;
5350 return copy_decl_for_dup_finish (id, decl, copy);
5353 static tree
5354 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
5356 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
5357 return copy_decl_to_var (decl, id);
5358 else
5359 return copy_decl_no_change (decl, id);
5362 /* Return a copy of the function's argument tree. */
5363 static tree
5364 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
5365 bitmap args_to_skip, tree *vars)
5367 tree arg, *parg;
5368 tree new_parm = NULL;
5369 int i = 0;
5371 parg = &new_parm;
5373 for (arg = orig_parm; arg; arg = DECL_CHAIN (arg), i++)
5374 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
5376 tree new_tree = remap_decl (arg, id);
5377 if (TREE_CODE (new_tree) != PARM_DECL)
5378 new_tree = id->copy_decl (arg, id);
5379 lang_hooks.dup_lang_specific_decl (new_tree);
5380 *parg = new_tree;
5381 parg = &DECL_CHAIN (new_tree);
5383 else if (!id->decl_map->get (arg))
5385 /* Make an equivalent VAR_DECL. If the argument was used
5386 as temporary variable later in function, the uses will be
5387 replaced by local variable. */
5388 tree var = copy_decl_to_var (arg, id);
5389 insert_decl_map (id, arg, var);
5390 /* Declare this new variable. */
5391 DECL_CHAIN (var) = *vars;
5392 *vars = var;
5394 return new_parm;
5397 /* Return a copy of the function's static chain. */
5398 static tree
5399 copy_static_chain (tree static_chain, copy_body_data * id)
5401 tree *chain_copy, *pvar;
5403 chain_copy = &static_chain;
5404 for (pvar = chain_copy; *pvar; pvar = &DECL_CHAIN (*pvar))
5406 tree new_tree = remap_decl (*pvar, id);
5407 lang_hooks.dup_lang_specific_decl (new_tree);
5408 DECL_CHAIN (new_tree) = DECL_CHAIN (*pvar);
5409 *pvar = new_tree;
5411 return static_chain;
5414 /* Return true if the function is allowed to be versioned.
5415 This is a guard for the versioning functionality. */
5417 bool
5418 tree_versionable_function_p (tree fndecl)
5420 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
5421 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
5424 /* Delete all unreachable basic blocks and update callgraph.
5425 Doing so is somewhat nontrivial because we need to update all clones and
5426 remove inline function that become unreachable. */
5428 static bool
5429 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
5431 bool changed = false;
5432 basic_block b, next_bb;
5434 find_unreachable_blocks ();
5436 /* Delete all unreachable basic blocks. */
5438 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
5439 != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
5441 next_bb = b->next_bb;
5443 if (!(b->flags & BB_REACHABLE))
5445 gimple_stmt_iterator bsi;
5447 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
5449 struct cgraph_edge *e;
5450 struct cgraph_node *node;
5452 id->dst_node->remove_stmt_references (gsi_stmt (bsi));
5454 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
5455 &&(e = id->dst_node->get_edge (gsi_stmt (bsi))) != NULL)
5457 if (!e->inline_failed)
5458 e->callee->remove_symbol_and_inline_clones (id->dst_node);
5459 else
5460 e->remove ();
5462 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
5463 && id->dst_node->clones)
5464 for (node = id->dst_node->clones; node != id->dst_node;)
5466 node->remove_stmt_references (gsi_stmt (bsi));
5467 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
5468 && (e = node->get_edge (gsi_stmt (bsi))) != NULL)
5470 if (!e->inline_failed)
5471 e->callee->remove_symbol_and_inline_clones (id->dst_node);
5472 else
5473 e->remove ();
5476 if (node->clones)
5477 node = node->clones;
5478 else if (node->next_sibling_clone)
5479 node = node->next_sibling_clone;
5480 else
5482 while (node != id->dst_node && !node->next_sibling_clone)
5483 node = node->clone_of;
5484 if (node != id->dst_node)
5485 node = node->next_sibling_clone;
5489 delete_basic_block (b);
5490 changed = true;
5494 return changed;
5497 /* Update clone info after duplication. */
5499 static void
5500 update_clone_info (copy_body_data * id)
5502 struct cgraph_node *node;
5503 if (!id->dst_node->clones)
5504 return;
5505 for (node = id->dst_node->clones; node != id->dst_node;)
5507 /* First update replace maps to match the new body. */
5508 if (node->clone.tree_map)
5510 unsigned int i;
5511 for (i = 0; i < vec_safe_length (node->clone.tree_map); i++)
5513 struct ipa_replace_map *replace_info;
5514 replace_info = (*node->clone.tree_map)[i];
5515 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
5516 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
5519 if (node->clones)
5520 node = node->clones;
5521 else if (node->next_sibling_clone)
5522 node = node->next_sibling_clone;
5523 else
5525 while (node != id->dst_node && !node->next_sibling_clone)
5526 node = node->clone_of;
5527 if (node != id->dst_node)
5528 node = node->next_sibling_clone;
5533 /* Create a copy of a function's tree.
5534 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
5535 of the original function and the new copied function
5536 respectively. In case we want to replace a DECL
5537 tree with another tree while duplicating the function's
5538 body, TREE_MAP represents the mapping between these
5539 trees. If UPDATE_CLONES is set, the call_stmt fields
5540 of edges of clones of the function will be updated.
5542 If non-NULL ARGS_TO_SKIP determine function parameters to remove
5543 from new version.
5544 If SKIP_RETURN is true, the new version will return void.
5545 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
5546 If non_NULL NEW_ENTRY determine new entry BB of the clone.
5548 void
5549 tree_function_versioning (tree old_decl, tree new_decl,
5550 vec<ipa_replace_map *, va_gc> *tree_map,
5551 bool update_clones, bitmap args_to_skip,
5552 bool skip_return, bitmap blocks_to_copy,
5553 basic_block new_entry)
5555 struct cgraph_node *old_version_node;
5556 struct cgraph_node *new_version_node;
5557 copy_body_data id;
5558 tree p;
5559 unsigned i;
5560 struct ipa_replace_map *replace_info;
5561 basic_block old_entry_block, bb;
5562 auto_vec<gimple, 10> init_stmts;
5563 tree vars = NULL_TREE;
5565 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
5566 && TREE_CODE (new_decl) == FUNCTION_DECL);
5567 DECL_POSSIBLY_INLINED (old_decl) = 1;
5569 old_version_node = cgraph_node::get (old_decl);
5570 gcc_checking_assert (old_version_node);
5571 new_version_node = cgraph_node::get (new_decl);
5572 gcc_checking_assert (new_version_node);
5574 /* Copy over debug args. */
5575 if (DECL_HAS_DEBUG_ARGS_P (old_decl))
5577 vec<tree, va_gc> **new_debug_args, **old_debug_args;
5578 gcc_checking_assert (decl_debug_args_lookup (new_decl) == NULL);
5579 DECL_HAS_DEBUG_ARGS_P (new_decl) = 0;
5580 old_debug_args = decl_debug_args_lookup (old_decl);
5581 if (old_debug_args)
5583 new_debug_args = decl_debug_args_insert (new_decl);
5584 *new_debug_args = vec_safe_copy (*old_debug_args);
5588 /* Output the inlining info for this abstract function, since it has been
5589 inlined. If we don't do this now, we can lose the information about the
5590 variables in the function when the blocks get blown away as soon as we
5591 remove the cgraph node. */
5592 (*debug_hooks->outlining_inline_function) (old_decl);
5594 DECL_ARTIFICIAL (new_decl) = 1;
5595 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
5596 if (DECL_ORIGIN (old_decl) == old_decl)
5597 old_version_node->used_as_abstract_origin = true;
5598 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
5600 /* Prepare the data structures for the tree copy. */
5601 memset (&id, 0, sizeof (id));
5603 /* Generate a new name for the new version. */
5604 id.statements_to_fold = new hash_set<gimple>;
5606 id.decl_map = new hash_map<tree, tree>;
5607 id.debug_map = NULL;
5608 id.src_fn = old_decl;
5609 id.dst_fn = new_decl;
5610 id.src_node = old_version_node;
5611 id.dst_node = new_version_node;
5612 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
5613 id.blocks_to_copy = blocks_to_copy;
5615 id.copy_decl = copy_decl_no_change;
5616 id.transform_call_graph_edges
5617 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
5618 id.transform_new_cfg = true;
5619 id.transform_return_to_modify = false;
5620 id.transform_parameter = false;
5621 id.transform_lang_insert_block = NULL;
5623 old_entry_block = ENTRY_BLOCK_PTR_FOR_FN
5624 (DECL_STRUCT_FUNCTION (old_decl));
5625 DECL_RESULT (new_decl) = DECL_RESULT (old_decl);
5626 DECL_ARGUMENTS (new_decl) = DECL_ARGUMENTS (old_decl);
5627 initialize_cfun (new_decl, old_decl,
5628 old_entry_block->count);
5629 if (DECL_STRUCT_FUNCTION (new_decl)->gimple_df)
5630 DECL_STRUCT_FUNCTION (new_decl)->gimple_df->ipa_pta
5631 = id.src_cfun->gimple_df->ipa_pta;
5633 /* Copy the function's static chain. */
5634 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
5635 if (p)
5636 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
5637 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
5638 &id);
5640 /* If there's a tree_map, prepare for substitution. */
5641 if (tree_map)
5642 for (i = 0; i < tree_map->length (); i++)
5644 gimple init;
5645 replace_info = (*tree_map)[i];
5646 if (replace_info->replace_p)
5648 if (!replace_info->old_tree)
5650 int i = replace_info->parm_num;
5651 tree parm;
5652 tree req_type;
5654 for (parm = DECL_ARGUMENTS (old_decl); i; parm = DECL_CHAIN (parm))
5655 i --;
5656 replace_info->old_tree = parm;
5657 req_type = TREE_TYPE (parm);
5658 if (!useless_type_conversion_p (req_type, TREE_TYPE (replace_info->new_tree)))
5660 if (fold_convertible_p (req_type, replace_info->new_tree))
5661 replace_info->new_tree = fold_build1 (NOP_EXPR, req_type, replace_info->new_tree);
5662 else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (replace_info->new_tree)))
5663 replace_info->new_tree = fold_build1 (VIEW_CONVERT_EXPR, req_type, replace_info->new_tree);
5664 else
5666 if (dump_file)
5668 fprintf (dump_file, " const ");
5669 print_generic_expr (dump_file, replace_info->new_tree, 0);
5670 fprintf (dump_file, " can't be converted to param ");
5671 print_generic_expr (dump_file, parm, 0);
5672 fprintf (dump_file, "\n");
5674 replace_info->old_tree = NULL;
5678 else
5679 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
5680 if (replace_info->old_tree)
5682 init = setup_one_parameter (&id, replace_info->old_tree,
5683 replace_info->new_tree, id.src_fn,
5684 NULL,
5685 &vars);
5686 if (init)
5687 init_stmts.safe_push (init);
5691 /* Copy the function's arguments. */
5692 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
5693 DECL_ARGUMENTS (new_decl) =
5694 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
5695 args_to_skip, &vars);
5697 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
5698 BLOCK_SUPERCONTEXT (DECL_INITIAL (new_decl)) = new_decl;
5700 declare_inline_vars (DECL_INITIAL (new_decl), vars);
5702 if (!vec_safe_is_empty (DECL_STRUCT_FUNCTION (old_decl)->local_decls))
5703 /* Add local vars. */
5704 add_local_variables (DECL_STRUCT_FUNCTION (old_decl), cfun, &id);
5706 if (DECL_RESULT (old_decl) == NULL_TREE)
5708 else if (skip_return && !VOID_TYPE_P (TREE_TYPE (DECL_RESULT (old_decl))))
5710 DECL_RESULT (new_decl)
5711 = build_decl (DECL_SOURCE_LOCATION (DECL_RESULT (old_decl)),
5712 RESULT_DECL, NULL_TREE, void_type_node);
5713 DECL_CONTEXT (DECL_RESULT (new_decl)) = new_decl;
5714 cfun->returns_struct = 0;
5715 cfun->returns_pcc_struct = 0;
5717 else
5719 tree old_name;
5720 DECL_RESULT (new_decl) = remap_decl (DECL_RESULT (old_decl), &id);
5721 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
5722 if (gimple_in_ssa_p (id.src_cfun)
5723 && DECL_BY_REFERENCE (DECL_RESULT (old_decl))
5724 && (old_name = ssa_default_def (id.src_cfun, DECL_RESULT (old_decl))))
5726 tree new_name = make_ssa_name (DECL_RESULT (new_decl));
5727 insert_decl_map (&id, old_name, new_name);
5728 SSA_NAME_DEF_STMT (new_name) = gimple_build_nop ();
5729 set_ssa_default_def (cfun, DECL_RESULT (new_decl), new_name);
5733 /* Set up the destination functions loop tree. */
5734 if (loops_for_fn (DECL_STRUCT_FUNCTION (old_decl)) != NULL)
5736 cfun->curr_properties &= ~PROP_loops;
5737 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5738 cfun->curr_properties |= PROP_loops;
5741 /* Copy the Function's body. */
5742 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
5743 ENTRY_BLOCK_PTR_FOR_FN (cfun), EXIT_BLOCK_PTR_FOR_FN (cfun),
5744 new_entry);
5746 /* Renumber the lexical scoping (non-code) blocks consecutively. */
5747 number_blocks (new_decl);
5749 /* We want to create the BB unconditionally, so that the addition of
5750 debug stmts doesn't affect BB count, which may in the end cause
5751 codegen differences. */
5752 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5753 while (init_stmts.length ())
5754 insert_init_stmt (&id, bb, init_stmts.pop ());
5755 update_clone_info (&id);
5757 /* Remap the nonlocal_goto_save_area, if any. */
5758 if (cfun->nonlocal_goto_save_area)
5760 struct walk_stmt_info wi;
5762 memset (&wi, 0, sizeof (wi));
5763 wi.info = &id;
5764 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
5767 /* Clean up. */
5768 delete id.decl_map;
5769 if (id.debug_map)
5770 delete id.debug_map;
5771 free_dominance_info (CDI_DOMINATORS);
5772 free_dominance_info (CDI_POST_DOMINATORS);
5774 fold_marked_statements (0, id.statements_to_fold);
5775 delete id.statements_to_fold;
5776 fold_cond_expr_cond ();
5777 delete_unreachable_blocks_update_callgraph (&id);
5778 if (id.dst_node->definition)
5779 cgraph_edge::rebuild_references ();
5780 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
5782 calculate_dominance_info (CDI_DOMINATORS);
5783 fix_loop_structure (NULL);
5785 update_ssa (TODO_update_ssa);
5787 /* After partial cloning we need to rescale frequencies, so they are
5788 within proper range in the cloned function. */
5789 if (new_entry)
5791 struct cgraph_edge *e;
5792 rebuild_frequencies ();
5794 new_version_node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5795 for (e = new_version_node->callees; e; e = e->next_callee)
5797 basic_block bb = gimple_bb (e->call_stmt);
5798 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5799 bb);
5800 e->count = bb->count;
5802 for (e = new_version_node->indirect_calls; e; e = e->next_callee)
5804 basic_block bb = gimple_bb (e->call_stmt);
5805 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5806 bb);
5807 e->count = bb->count;
5811 free_dominance_info (CDI_DOMINATORS);
5812 free_dominance_info (CDI_POST_DOMINATORS);
5814 gcc_assert (!id.debug_stmts.exists ());
5815 pop_cfun ();
5816 return;
5819 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
5820 the callee and return the inlined body on success. */
5822 tree
5823 maybe_inline_call_in_expr (tree exp)
5825 tree fn = get_callee_fndecl (exp);
5827 /* We can only try to inline "const" functions. */
5828 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
5830 call_expr_arg_iterator iter;
5831 copy_body_data id;
5832 tree param, arg, t;
5833 hash_map<tree, tree> decl_map;
5835 /* Remap the parameters. */
5836 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
5837 param;
5838 param = DECL_CHAIN (param), arg = next_call_expr_arg (&iter))
5839 decl_map.put (param, arg);
5841 memset (&id, 0, sizeof (id));
5842 id.src_fn = fn;
5843 id.dst_fn = current_function_decl;
5844 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
5845 id.decl_map = &decl_map;
5847 id.copy_decl = copy_decl_no_change;
5848 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5849 id.transform_new_cfg = false;
5850 id.transform_return_to_modify = true;
5851 id.transform_parameter = true;
5852 id.transform_lang_insert_block = NULL;
5854 /* Make sure not to unshare trees behind the front-end's back
5855 since front-end specific mechanisms may rely on sharing. */
5856 id.regimplify = false;
5857 id.do_not_unshare = true;
5859 /* We're not inside any EH region. */
5860 id.eh_lp_nr = 0;
5862 t = copy_tree_body (&id);
5864 /* We can only return something suitable for use in a GENERIC
5865 expression tree. */
5866 if (TREE_CODE (t) == MODIFY_EXPR)
5867 return TREE_OPERAND (t, 1);
5870 return NULL_TREE;
5873 /* Duplicate a type, fields and all. */
5875 tree
5876 build_duplicate_type (tree type)
5878 struct copy_body_data id;
5880 memset (&id, 0, sizeof (id));
5881 id.src_fn = current_function_decl;
5882 id.dst_fn = current_function_decl;
5883 id.src_cfun = cfun;
5884 id.decl_map = new hash_map<tree, tree>;
5885 id.debug_map = NULL;
5886 id.copy_decl = copy_decl_no_change;
5888 type = remap_type_1 (type, &id);
5890 delete id.decl_map;
5891 if (id.debug_map)
5892 delete id.debug_map;
5894 TYPE_CANONICAL (type) = type;
5896 return type;
5899 /* Unshare the entire DECL_SAVED_TREE of FN and return the remapped
5900 parameters and RESULT_DECL in PARMS and RESULT. Used by C++ constexpr
5901 evaluation. */
5903 tree
5904 copy_fn (tree fn, tree& parms, tree& result)
5906 copy_body_data id;
5907 tree param;
5908 hash_map<tree, tree> decl_map;
5910 tree *p = &parms;
5911 *p = NULL_TREE;
5913 memset (&id, 0, sizeof (id));
5914 id.src_fn = fn;
5915 id.dst_fn = current_function_decl;
5916 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
5917 id.decl_map = &decl_map;
5919 id.copy_decl = copy_decl_no_change;
5920 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5921 id.transform_new_cfg = false;
5922 id.transform_return_to_modify = false;
5923 id.transform_parameter = true;
5924 id.transform_lang_insert_block = NULL;
5926 /* Make sure not to unshare trees behind the front-end's back
5927 since front-end specific mechanisms may rely on sharing. */
5928 id.regimplify = false;
5929 id.do_not_unshare = true;
5931 /* We're not inside any EH region. */
5932 id.eh_lp_nr = 0;
5934 /* Remap the parameters and result and return them to the caller. */
5935 for (param = DECL_ARGUMENTS (fn);
5936 param;
5937 param = DECL_CHAIN (param))
5939 *p = remap_decl (param, &id);
5940 p = &DECL_CHAIN (*p);
5943 if (DECL_RESULT (fn))
5944 result = remap_decl (DECL_RESULT (fn), &id);
5945 else
5946 result = NULL_TREE;
5948 return copy_tree_body (&id);