re PR c++/62127 (ICE with VLA in constructor)
[official-gcc.git] / gcc / tree-inline.c
bloba817989200e7fa2f4a0e0acdfc318bf9ddb37a59
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 "basic-block.h"
37 #include "tree-iterator.h"
38 #include "intl.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimplify.h"
47 #include "gimple-iterator.h"
48 #include "gimplify-me.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
51 #include "tree-cfg.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-into-ssa.h"
57 #include "expr.h"
58 #include "tree-dfa.h"
59 #include "tree-ssa.h"
60 #include "function.h"
61 #include "tree-pretty-print.h"
62 #include "except.h"
63 #include "debug.h"
64 #include "ipa-prop.h"
65 #include "value-prof.h"
66 #include "tree-pass.h"
67 #include "target.h"
68 #include "cfgloop.h"
69 #include "builtins.h"
71 #include "rtl.h" /* FIXME: For asm_str_count. */
73 /* I'm not real happy about this, but we need to handle gimple and
74 non-gimple trees. */
76 /* Inlining, Cloning, Versioning, Parallelization
78 Inlining: a function body is duplicated, but the PARM_DECLs are
79 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
80 MODIFY_EXPRs that store to a dedicated returned-value variable.
81 The duplicated eh_region info of the copy will later be appended
82 to the info for the caller; the eh_region info in copied throwing
83 statements and RESX statements are adjusted accordingly.
85 Cloning: (only in C++) We have one body for a con/de/structor, and
86 multiple function decls, each with a unique parameter list.
87 Duplicate the body, using the given splay tree; some parameters
88 will become constants (like 0 or 1).
90 Versioning: a function body is duplicated and the result is a new
91 function rather than into blocks of an existing function as with
92 inlining. Some parameters will become constants.
94 Parallelization: a region of a function is duplicated resulting in
95 a new function. Variables may be replaced with complex expressions
96 to enable shared variable semantics.
98 All of these will simultaneously lookup any callgraph edges. If
99 we're going to inline the duplicated function body, and the given
100 function has some cloned callgraph nodes (one for each place this
101 function will be inlined) those callgraph edges will be duplicated.
102 If we're cloning the body, those callgraph edges will be
103 updated to point into the new body. (Note that the original
104 callgraph node and edge list will not be altered.)
106 See the CALL_EXPR handling case in copy_tree_body_r (). */
108 /* To Do:
110 o In order to make inlining-on-trees work, we pessimized
111 function-local static constants. In particular, they are now
112 always output, even when not addressed. Fix this by treating
113 function-local static constants just like global static
114 constants; the back-end already knows not to output them if they
115 are not needed.
117 o Provide heuristics to clamp inlining of recursive template
118 calls? */
121 /* Weights that estimate_num_insns uses to estimate the size of the
122 produced code. */
124 eni_weights eni_size_weights;
126 /* Weights that estimate_num_insns uses to estimate the time necessary
127 to execute the produced code. */
129 eni_weights eni_time_weights;
131 /* Prototypes. */
133 static tree declare_return_variable (copy_body_data *, tree, tree, basic_block);
134 static void remap_block (tree *, copy_body_data *);
135 static void copy_bind_expr (tree *, int *, copy_body_data *);
136 static void declare_inline_vars (tree, tree);
137 static void remap_save_expr (tree *, hash_map<tree, tree> *, int *);
138 static void prepend_lexical_block (tree current_block, tree new_block);
139 static tree copy_decl_to_var (tree, copy_body_data *);
140 static tree copy_result_decl_to_var (tree, copy_body_data *);
141 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
142 static gimple remap_gimple_stmt (gimple, copy_body_data *);
143 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
145 /* Insert a tree->tree mapping for ID. Despite the name suggests
146 that the trees should be variables, it is used for more than that. */
148 void
149 insert_decl_map (copy_body_data *id, tree key, tree value)
151 id->decl_map->put (key, value);
153 /* Always insert an identity map as well. If we see this same new
154 node again, we won't want to duplicate it a second time. */
155 if (key != value)
156 id->decl_map->put (value, value);
159 /* Insert a tree->tree mapping for ID. This is only used for
160 variables. */
162 static void
163 insert_debug_decl_map (copy_body_data *id, tree key, tree value)
165 if (!gimple_in_ssa_p (id->src_cfun))
166 return;
168 if (!MAY_HAVE_DEBUG_STMTS)
169 return;
171 if (!target_for_debug_bind (key))
172 return;
174 gcc_assert (TREE_CODE (key) == PARM_DECL);
175 gcc_assert (TREE_CODE (value) == VAR_DECL);
177 if (!id->debug_map)
178 id->debug_map = new hash_map<tree, tree>;
180 id->debug_map->put (key, value);
183 /* If nonzero, we're remapping the contents of inlined debug
184 statements. If negative, an error has occurred, such as a
185 reference to a variable that isn't available in the inlined
186 context. */
187 static int processing_debug_stmt = 0;
189 /* Construct new SSA name for old NAME. ID is the inline context. */
191 static tree
192 remap_ssa_name (tree name, copy_body_data *id)
194 tree new_tree, var;
195 tree *n;
197 gcc_assert (TREE_CODE (name) == SSA_NAME);
199 n = id->decl_map->get (name);
200 if (n)
201 return unshare_expr (*n);
203 if (processing_debug_stmt)
205 if (SSA_NAME_IS_DEFAULT_DEF (name)
206 && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
207 && id->entry_bb == NULL
208 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
210 tree vexpr = make_node (DEBUG_EXPR_DECL);
211 gimple def_temp;
212 gimple_stmt_iterator gsi;
213 tree val = SSA_NAME_VAR (name);
215 n = id->decl_map->get (val);
216 if (n != NULL)
217 val = *n;
218 if (TREE_CODE (val) != PARM_DECL)
220 processing_debug_stmt = -1;
221 return name;
223 def_temp = gimple_build_debug_source_bind (vexpr, val, NULL);
224 DECL_ARTIFICIAL (vexpr) = 1;
225 TREE_TYPE (vexpr) = TREE_TYPE (name);
226 DECL_MODE (vexpr) = DECL_MODE (SSA_NAME_VAR (name));
227 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
228 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
229 return vexpr;
232 processing_debug_stmt = -1;
233 return name;
236 /* Remap anonymous SSA names or SSA names of anonymous decls. */
237 var = SSA_NAME_VAR (name);
238 if (!var
239 || (!SSA_NAME_IS_DEFAULT_DEF (name)
240 && TREE_CODE (var) == VAR_DECL
241 && !VAR_DECL_IS_VIRTUAL_OPERAND (var)
242 && DECL_ARTIFICIAL (var)
243 && DECL_IGNORED_P (var)
244 && !DECL_NAME (var)))
246 struct ptr_info_def *pi;
247 new_tree = make_ssa_name (remap_type (TREE_TYPE (name), id), NULL);
248 if (!var && SSA_NAME_IDENTIFIER (name))
249 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_tree, SSA_NAME_IDENTIFIER (name));
250 insert_decl_map (id, name, new_tree);
251 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
252 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
253 /* At least IPA points-to info can be directly transferred. */
254 if (id->src_cfun->gimple_df
255 && id->src_cfun->gimple_df->ipa_pta
256 && (pi = SSA_NAME_PTR_INFO (name))
257 && !pi->pt.anything)
259 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
260 new_pi->pt = pi->pt;
262 return new_tree;
265 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
266 in copy_bb. */
267 new_tree = remap_decl (var, id);
269 /* We might've substituted constant or another SSA_NAME for
270 the variable.
272 Replace the SSA name representing RESULT_DECL by variable during
273 inlining: this saves us from need to introduce PHI node in a case
274 return value is just partly initialized. */
275 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
276 && (!SSA_NAME_VAR (name)
277 || TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
278 || !id->transform_return_to_modify))
280 struct ptr_info_def *pi;
281 new_tree = make_ssa_name (new_tree, NULL);
282 insert_decl_map (id, name, new_tree);
283 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
284 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
285 /* At least IPA points-to info can be directly transferred. */
286 if (id->src_cfun->gimple_df
287 && id->src_cfun->gimple_df->ipa_pta
288 && (pi = SSA_NAME_PTR_INFO (name))
289 && !pi->pt.anything)
291 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
292 new_pi->pt = pi->pt;
294 if (SSA_NAME_IS_DEFAULT_DEF (name))
296 /* By inlining function having uninitialized variable, we might
297 extend the lifetime (variable might get reused). This cause
298 ICE in the case we end up extending lifetime of SSA name across
299 abnormal edge, but also increase register pressure.
301 We simply initialize all uninitialized vars by 0 except
302 for case we are inlining to very first BB. We can avoid
303 this for all BBs that are not inside strongly connected
304 regions of the CFG, but this is expensive to test. */
305 if (id->entry_bb
306 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
307 && (!SSA_NAME_VAR (name)
308 || TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL)
309 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun),
310 0)->dest
311 || EDGE_COUNT (id->entry_bb->preds) != 1))
313 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
314 gimple init_stmt;
315 tree zero = build_zero_cst (TREE_TYPE (new_tree));
317 init_stmt = gimple_build_assign (new_tree, zero);
318 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
319 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
321 else
323 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
324 set_ssa_default_def (cfun, SSA_NAME_VAR (new_tree), new_tree);
328 else
329 insert_decl_map (id, name, new_tree);
330 return new_tree;
333 /* Remap DECL during the copying of the BLOCK tree for the function. */
335 tree
336 remap_decl (tree decl, copy_body_data *id)
338 tree *n;
340 /* We only remap local variables in the current function. */
342 /* See if we have remapped this declaration. */
344 n = id->decl_map->get (decl);
346 if (!n && processing_debug_stmt)
348 processing_debug_stmt = -1;
349 return decl;
352 /* If we didn't already have an equivalent for this declaration,
353 create one now. */
354 if (!n)
356 /* Make a copy of the variable or label. */
357 tree t = id->copy_decl (decl, id);
359 /* Remember it, so that if we encounter this local entity again
360 we can reuse this copy. Do this early because remap_type may
361 need this decl for TYPE_STUB_DECL. */
362 insert_decl_map (id, decl, t);
364 if (!DECL_P (t))
365 return t;
367 /* Remap types, if necessary. */
368 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
369 if (TREE_CODE (t) == TYPE_DECL)
370 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
372 /* Remap sizes as necessary. */
373 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
374 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
376 /* If fields, do likewise for offset and qualifier. */
377 if (TREE_CODE (t) == FIELD_DECL)
379 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
380 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
381 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
384 return t;
387 if (id->do_not_unshare)
388 return *n;
389 else
390 return unshare_expr (*n);
393 static tree
394 remap_type_1 (tree type, copy_body_data *id)
396 tree new_tree, t;
398 /* We do need a copy. build and register it now. If this is a pointer or
399 reference type, remap the designated type and make a new pointer or
400 reference type. */
401 if (TREE_CODE (type) == POINTER_TYPE)
403 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
404 TYPE_MODE (type),
405 TYPE_REF_CAN_ALIAS_ALL (type));
406 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
407 new_tree = build_type_attribute_qual_variant (new_tree,
408 TYPE_ATTRIBUTES (type),
409 TYPE_QUALS (type));
410 insert_decl_map (id, type, new_tree);
411 return new_tree;
413 else if (TREE_CODE (type) == REFERENCE_TYPE)
415 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
416 TYPE_MODE (type),
417 TYPE_REF_CAN_ALIAS_ALL (type));
418 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
419 new_tree = build_type_attribute_qual_variant (new_tree,
420 TYPE_ATTRIBUTES (type),
421 TYPE_QUALS (type));
422 insert_decl_map (id, type, new_tree);
423 return new_tree;
425 else
426 new_tree = copy_node (type);
428 insert_decl_map (id, type, new_tree);
430 /* This is a new type, not a copy of an old type. Need to reassociate
431 variants. We can handle everything except the main variant lazily. */
432 t = TYPE_MAIN_VARIANT (type);
433 if (type != t)
435 t = remap_type (t, id);
436 TYPE_MAIN_VARIANT (new_tree) = t;
437 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
438 TYPE_NEXT_VARIANT (t) = new_tree;
440 else
442 TYPE_MAIN_VARIANT (new_tree) = new_tree;
443 TYPE_NEXT_VARIANT (new_tree) = NULL;
446 if (TYPE_STUB_DECL (type))
447 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
449 /* Lazily create pointer and reference types. */
450 TYPE_POINTER_TO (new_tree) = NULL;
451 TYPE_REFERENCE_TO (new_tree) = NULL;
453 /* Copy all types that may contain references to local variables; be sure to
454 preserve sharing in between type and its main variant when possible. */
455 switch (TREE_CODE (new_tree))
457 case INTEGER_TYPE:
458 case REAL_TYPE:
459 case FIXED_POINT_TYPE:
460 case ENUMERAL_TYPE:
461 case BOOLEAN_TYPE:
462 if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
464 gcc_checking_assert (TYPE_MIN_VALUE (type) == TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (type)));
465 gcc_checking_assert (TYPE_MAX_VALUE (type) == TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (type)));
467 TYPE_MIN_VALUE (new_tree) = TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (new_tree));
468 TYPE_MAX_VALUE (new_tree) = TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (new_tree));
470 else
472 t = TYPE_MIN_VALUE (new_tree);
473 if (t && TREE_CODE (t) != INTEGER_CST)
474 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
476 t = TYPE_MAX_VALUE (new_tree);
477 if (t && TREE_CODE (t) != INTEGER_CST)
478 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
480 return new_tree;
482 case FUNCTION_TYPE:
483 if (TYPE_MAIN_VARIANT (new_tree) != new_tree
484 && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
485 TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
486 else
487 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
488 if (TYPE_MAIN_VARIANT (new_tree) != new_tree
489 && TYPE_ARG_TYPES (type) == TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (type)))
490 TYPE_ARG_TYPES (new_tree) = TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (new_tree));
491 else
492 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
493 return new_tree;
495 case ARRAY_TYPE:
496 if (TYPE_MAIN_VARIANT (new_tree) != new_tree
497 && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
498 TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
499 else
500 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
502 if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
504 gcc_checking_assert (TYPE_DOMAIN (type) == TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)));
505 TYPE_DOMAIN (new_tree) = TYPE_DOMAIN (TYPE_MAIN_VARIANT (new_tree));
507 else
508 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
509 break;
511 case RECORD_TYPE:
512 case UNION_TYPE:
513 case QUAL_UNION_TYPE:
514 if (TYPE_MAIN_VARIANT (type) != type
515 && TYPE_FIELDS (type) == TYPE_FIELDS (TYPE_MAIN_VARIANT (type)))
516 TYPE_FIELDS (new_tree) = TYPE_FIELDS (TYPE_MAIN_VARIANT (new_tree));
517 else
519 tree f, nf = NULL;
521 for (f = TYPE_FIELDS (new_tree); f ; f = DECL_CHAIN (f))
523 t = remap_decl (f, id);
524 DECL_CONTEXT (t) = new_tree;
525 DECL_CHAIN (t) = nf;
526 nf = t;
528 TYPE_FIELDS (new_tree) = nreverse (nf);
530 break;
532 case OFFSET_TYPE:
533 default:
534 /* Shouldn't have been thought variable sized. */
535 gcc_unreachable ();
538 /* All variants of type share the same size, so use the already remaped data. */
539 if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
541 gcc_checking_assert (TYPE_SIZE (type) == TYPE_SIZE (TYPE_MAIN_VARIANT (type)));
542 gcc_checking_assert (TYPE_SIZE_UNIT (type) == TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type)));
544 TYPE_SIZE (new_tree) = TYPE_SIZE (TYPE_MAIN_VARIANT (new_tree));
545 TYPE_SIZE_UNIT (new_tree) = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (new_tree));
547 else
549 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
550 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
553 return new_tree;
556 tree
557 remap_type (tree type, copy_body_data *id)
559 tree *node;
560 tree tmp;
562 if (type == NULL)
563 return type;
565 /* See if we have remapped this type. */
566 node = id->decl_map->get (type);
567 if (node)
568 return *node;
570 /* The type only needs remapping if it's variably modified. */
571 if (! variably_modified_type_p (type, id->src_fn))
573 insert_decl_map (id, type, type);
574 return type;
577 id->remapping_type_depth++;
578 tmp = remap_type_1 (type, id);
579 id->remapping_type_depth--;
581 return tmp;
584 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
586 static bool
587 can_be_nonlocal (tree decl, copy_body_data *id)
589 /* We can not duplicate function decls. */
590 if (TREE_CODE (decl) == FUNCTION_DECL)
591 return true;
593 /* Local static vars must be non-local or we get multiple declaration
594 problems. */
595 if (TREE_CODE (decl) == VAR_DECL
596 && !auto_var_in_fn_p (decl, id->src_fn))
597 return true;
599 return false;
602 static tree
603 remap_decls (tree decls, vec<tree, va_gc> **nonlocalized_list,
604 copy_body_data *id)
606 tree old_var;
607 tree new_decls = NULL_TREE;
609 /* Remap its variables. */
610 for (old_var = decls; old_var; old_var = DECL_CHAIN (old_var))
612 tree new_var;
614 if (can_be_nonlocal (old_var, id))
616 /* We need to add this variable to the local decls as otherwise
617 nothing else will do so. */
618 if (TREE_CODE (old_var) == VAR_DECL
619 && ! DECL_EXTERNAL (old_var))
620 add_local_decl (cfun, old_var);
621 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
622 && !DECL_IGNORED_P (old_var)
623 && nonlocalized_list)
624 vec_safe_push (*nonlocalized_list, old_var);
625 continue;
628 /* Remap the variable. */
629 new_var = remap_decl (old_var, id);
631 /* If we didn't remap this variable, we can't mess with its
632 TREE_CHAIN. If we remapped this variable to the return slot, it's
633 already declared somewhere else, so don't declare it here. */
635 if (new_var == id->retvar)
637 else if (!new_var)
639 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
640 && !DECL_IGNORED_P (old_var)
641 && nonlocalized_list)
642 vec_safe_push (*nonlocalized_list, old_var);
644 else
646 gcc_assert (DECL_P (new_var));
647 DECL_CHAIN (new_var) = new_decls;
648 new_decls = new_var;
650 /* Also copy value-expressions. */
651 if (TREE_CODE (new_var) == VAR_DECL
652 && DECL_HAS_VALUE_EXPR_P (new_var))
654 tree tem = DECL_VALUE_EXPR (new_var);
655 bool old_regimplify = id->regimplify;
656 id->remapping_type_depth++;
657 walk_tree (&tem, copy_tree_body_r, id, NULL);
658 id->remapping_type_depth--;
659 id->regimplify = old_regimplify;
660 SET_DECL_VALUE_EXPR (new_var, tem);
665 return nreverse (new_decls);
668 /* Copy the BLOCK to contain remapped versions of the variables
669 therein. And hook the new block into the block-tree. */
671 static void
672 remap_block (tree *block, copy_body_data *id)
674 tree old_block;
675 tree new_block;
677 /* Make the new block. */
678 old_block = *block;
679 new_block = make_node (BLOCK);
680 TREE_USED (new_block) = TREE_USED (old_block);
681 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
682 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
683 BLOCK_NONLOCALIZED_VARS (new_block)
684 = vec_safe_copy (BLOCK_NONLOCALIZED_VARS (old_block));
685 *block = new_block;
687 /* Remap its variables. */
688 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
689 &BLOCK_NONLOCALIZED_VARS (new_block),
690 id);
692 if (id->transform_lang_insert_block)
693 id->transform_lang_insert_block (new_block);
695 /* Remember the remapped block. */
696 insert_decl_map (id, old_block, new_block);
699 /* Copy the whole block tree and root it in id->block. */
700 static tree
701 remap_blocks (tree block, copy_body_data *id)
703 tree t;
704 tree new_tree = block;
706 if (!block)
707 return NULL;
709 remap_block (&new_tree, id);
710 gcc_assert (new_tree != block);
711 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
712 prepend_lexical_block (new_tree, remap_blocks (t, id));
713 /* Blocks are in arbitrary order, but make things slightly prettier and do
714 not swap order when producing a copy. */
715 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
716 return new_tree;
719 /* Remap the block tree rooted at BLOCK to nothing. */
720 static void
721 remap_blocks_to_null (tree block, copy_body_data *id)
723 tree t;
724 insert_decl_map (id, block, NULL_TREE);
725 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
726 remap_blocks_to_null (t, id);
729 static void
730 copy_statement_list (tree *tp)
732 tree_stmt_iterator oi, ni;
733 tree new_tree;
735 new_tree = alloc_stmt_list ();
736 ni = tsi_start (new_tree);
737 oi = tsi_start (*tp);
738 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
739 *tp = new_tree;
741 for (; !tsi_end_p (oi); tsi_next (&oi))
743 tree stmt = tsi_stmt (oi);
744 if (TREE_CODE (stmt) == STATEMENT_LIST)
745 /* This copy is not redundant; tsi_link_after will smash this
746 STATEMENT_LIST into the end of the one we're building, and we
747 don't want to do that with the original. */
748 copy_statement_list (&stmt);
749 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
753 static void
754 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
756 tree block = BIND_EXPR_BLOCK (*tp);
757 /* Copy (and replace) the statement. */
758 copy_tree_r (tp, walk_subtrees, NULL);
759 if (block)
761 remap_block (&block, id);
762 BIND_EXPR_BLOCK (*tp) = block;
765 if (BIND_EXPR_VARS (*tp))
766 /* This will remap a lot of the same decls again, but this should be
767 harmless. */
768 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
772 /* Create a new gimple_seq by remapping all the statements in BODY
773 using the inlining information in ID. */
775 static gimple_seq
776 remap_gimple_seq (gimple_seq body, copy_body_data *id)
778 gimple_stmt_iterator si;
779 gimple_seq new_body = NULL;
781 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
783 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
784 gimple_seq_add_stmt (&new_body, new_stmt);
787 return new_body;
791 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
792 block using the mapping information in ID. */
794 static gimple
795 copy_gimple_bind (gimple stmt, copy_body_data *id)
797 gimple new_bind;
798 tree new_block, new_vars;
799 gimple_seq body, new_body;
801 /* Copy the statement. Note that we purposely don't use copy_stmt
802 here because we need to remap statements as we copy. */
803 body = gimple_bind_body (stmt);
804 new_body = remap_gimple_seq (body, id);
806 new_block = gimple_bind_block (stmt);
807 if (new_block)
808 remap_block (&new_block, id);
810 /* This will remap a lot of the same decls again, but this should be
811 harmless. */
812 new_vars = gimple_bind_vars (stmt);
813 if (new_vars)
814 new_vars = remap_decls (new_vars, NULL, id);
816 new_bind = gimple_build_bind (new_vars, new_body, new_block);
818 return new_bind;
821 /* Return true if DECL is a parameter or a SSA_NAME for a parameter. */
823 static bool
824 is_parm (tree decl)
826 if (TREE_CODE (decl) == SSA_NAME)
828 decl = SSA_NAME_VAR (decl);
829 if (!decl)
830 return false;
833 return (TREE_CODE (decl) == PARM_DECL);
836 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
837 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
838 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
839 recursing into the children nodes of *TP. */
841 static tree
842 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
844 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
845 copy_body_data *id = (copy_body_data *) wi_p->info;
846 tree fn = id->src_fn;
848 if (TREE_CODE (*tp) == SSA_NAME)
850 *tp = remap_ssa_name (*tp, id);
851 *walk_subtrees = 0;
852 return NULL;
854 else if (auto_var_in_fn_p (*tp, fn))
856 /* Local variables and labels need to be replaced by equivalent
857 variables. We don't want to copy static variables; there's
858 only one of those, no matter how many times we inline the
859 containing function. Similarly for globals from an outer
860 function. */
861 tree new_decl;
863 /* Remap the declaration. */
864 new_decl = remap_decl (*tp, id);
865 gcc_assert (new_decl);
866 /* Replace this variable with the copy. */
867 STRIP_TYPE_NOPS (new_decl);
868 /* ??? The C++ frontend uses void * pointer zero to initialize
869 any other type. This confuses the middle-end type verification.
870 As cloned bodies do not go through gimplification again the fixup
871 there doesn't trigger. */
872 if (TREE_CODE (new_decl) == INTEGER_CST
873 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
874 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
875 *tp = new_decl;
876 *walk_subtrees = 0;
878 else if (TREE_CODE (*tp) == STATEMENT_LIST)
879 gcc_unreachable ();
880 else if (TREE_CODE (*tp) == SAVE_EXPR)
881 gcc_unreachable ();
882 else if (TREE_CODE (*tp) == LABEL_DECL
883 && (!DECL_CONTEXT (*tp)
884 || decl_function_context (*tp) == id->src_fn))
885 /* These may need to be remapped for EH handling. */
886 *tp = remap_decl (*tp, id);
887 else if (TREE_CODE (*tp) == FIELD_DECL)
889 /* If the enclosing record type is variably_modified_type_p, the field
890 has already been remapped. Otherwise, it need not be. */
891 tree *n = id->decl_map->get (*tp);
892 if (n)
893 *tp = *n;
894 *walk_subtrees = 0;
896 else if (TYPE_P (*tp))
897 /* Types may need remapping as well. */
898 *tp = remap_type (*tp, id);
899 else if (CONSTANT_CLASS_P (*tp))
901 /* If this is a constant, we have to copy the node iff the type
902 will be remapped. copy_tree_r will not copy a constant. */
903 tree new_type = remap_type (TREE_TYPE (*tp), id);
905 if (new_type == TREE_TYPE (*tp))
906 *walk_subtrees = 0;
908 else if (TREE_CODE (*tp) == INTEGER_CST)
909 *tp = wide_int_to_tree (new_type, *tp);
910 else
912 *tp = copy_node (*tp);
913 TREE_TYPE (*tp) = new_type;
916 else
918 /* Otherwise, just copy the node. Note that copy_tree_r already
919 knows not to copy VAR_DECLs, etc., so this is safe. */
921 if (TREE_CODE (*tp) == MEM_REF)
923 /* We need to re-canonicalize MEM_REFs from inline substitutions
924 that can happen when a pointer argument is an ADDR_EXPR.
925 Recurse here manually to allow that. */
926 tree ptr = TREE_OPERAND (*tp, 0);
927 tree type = remap_type (TREE_TYPE (*tp), id);
928 tree old = *tp;
929 walk_tree (&ptr, remap_gimple_op_r, data, NULL);
930 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
931 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
932 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
933 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
934 /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
935 remapped a parameter as the property might be valid only
936 for the parameter itself. */
937 if (TREE_THIS_NOTRAP (old)
938 && (!is_parm (TREE_OPERAND (old, 0))
939 || (!id->transform_parameter && is_parm (ptr))))
940 TREE_THIS_NOTRAP (*tp) = 1;
941 *walk_subtrees = 0;
942 return NULL;
945 /* Here is the "usual case". Copy this tree node, and then
946 tweak some special cases. */
947 copy_tree_r (tp, walk_subtrees, NULL);
949 if (TREE_CODE (*tp) != OMP_CLAUSE)
950 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
952 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
954 /* The copied TARGET_EXPR has never been expanded, even if the
955 original node was expanded already. */
956 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
957 TREE_OPERAND (*tp, 3) = NULL_TREE;
959 else if (TREE_CODE (*tp) == ADDR_EXPR)
961 /* Variable substitution need not be simple. In particular,
962 the MEM_REF substitution above. Make sure that
963 TREE_CONSTANT and friends are up-to-date. */
964 int invariant = is_gimple_min_invariant (*tp);
965 walk_tree (&TREE_OPERAND (*tp, 0), remap_gimple_op_r, data, NULL);
966 recompute_tree_invariant_for_addr_expr (*tp);
968 /* If this used to be invariant, but is not any longer,
969 then regimplification is probably needed. */
970 if (invariant && !is_gimple_min_invariant (*tp))
971 id->regimplify = true;
973 *walk_subtrees = 0;
977 /* Update the TREE_BLOCK for the cloned expr. */
978 if (EXPR_P (*tp))
980 tree new_block = id->remapping_type_depth == 0 ? id->block : NULL;
981 tree old_block = TREE_BLOCK (*tp);
982 if (old_block)
984 tree *n;
985 n = id->decl_map->get (TREE_BLOCK (*tp));
986 if (n)
987 new_block = *n;
989 TREE_SET_BLOCK (*tp, new_block);
992 /* Keep iterating. */
993 return NULL_TREE;
997 /* Called from copy_body_id via walk_tree. DATA is really a
998 `copy_body_data *'. */
1000 tree
1001 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
1003 copy_body_data *id = (copy_body_data *) data;
1004 tree fn = id->src_fn;
1005 tree new_block;
1007 /* Begin by recognizing trees that we'll completely rewrite for the
1008 inlining context. Our output for these trees is completely
1009 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1010 into an edge). Further down, we'll handle trees that get
1011 duplicated and/or tweaked. */
1013 /* When requested, RETURN_EXPRs should be transformed to just the
1014 contained MODIFY_EXPR. The branch semantics of the return will
1015 be handled elsewhere by manipulating the CFG rather than a statement. */
1016 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
1018 tree assignment = TREE_OPERAND (*tp, 0);
1020 /* If we're returning something, just turn that into an
1021 assignment into the equivalent of the original RESULT_DECL.
1022 If the "assignment" is just the result decl, the result
1023 decl has already been set (e.g. a recent "foo (&result_decl,
1024 ...)"); just toss the entire RETURN_EXPR. */
1025 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
1027 /* Replace the RETURN_EXPR with (a copy of) the
1028 MODIFY_EXPR hanging underneath. */
1029 *tp = copy_node (assignment);
1031 else /* Else the RETURN_EXPR returns no value. */
1033 *tp = NULL;
1034 return (tree) (void *)1;
1037 else if (TREE_CODE (*tp) == SSA_NAME)
1039 *tp = remap_ssa_name (*tp, id);
1040 *walk_subtrees = 0;
1041 return NULL;
1044 /* Local variables and labels need to be replaced by equivalent
1045 variables. We don't want to copy static variables; there's only
1046 one of those, no matter how many times we inline the containing
1047 function. Similarly for globals from an outer function. */
1048 else if (auto_var_in_fn_p (*tp, fn))
1050 tree new_decl;
1052 /* Remap the declaration. */
1053 new_decl = remap_decl (*tp, id);
1054 gcc_assert (new_decl);
1055 /* Replace this variable with the copy. */
1056 STRIP_TYPE_NOPS (new_decl);
1057 *tp = new_decl;
1058 *walk_subtrees = 0;
1060 else if (TREE_CODE (*tp) == STATEMENT_LIST)
1061 copy_statement_list (tp);
1062 else if (TREE_CODE (*tp) == SAVE_EXPR
1063 || TREE_CODE (*tp) == TARGET_EXPR)
1064 remap_save_expr (tp, id->decl_map, walk_subtrees);
1065 else if (TREE_CODE (*tp) == LABEL_DECL
1066 && (! DECL_CONTEXT (*tp)
1067 || decl_function_context (*tp) == id->src_fn))
1068 /* These may need to be remapped for EH handling. */
1069 *tp = remap_decl (*tp, id);
1070 else if (TREE_CODE (*tp) == BIND_EXPR)
1071 copy_bind_expr (tp, walk_subtrees, id);
1072 /* Types may need remapping as well. */
1073 else if (TYPE_P (*tp))
1074 *tp = remap_type (*tp, id);
1076 /* If this is a constant, we have to copy the node iff the type will be
1077 remapped. copy_tree_r will not copy a constant. */
1078 else if (CONSTANT_CLASS_P (*tp))
1080 tree new_type = remap_type (TREE_TYPE (*tp), id);
1082 if (new_type == TREE_TYPE (*tp))
1083 *walk_subtrees = 0;
1085 else if (TREE_CODE (*tp) == INTEGER_CST)
1086 *tp = wide_int_to_tree (new_type, *tp);
1087 else
1089 *tp = copy_node (*tp);
1090 TREE_TYPE (*tp) = new_type;
1094 /* Otherwise, just copy the node. Note that copy_tree_r already
1095 knows not to copy VAR_DECLs, etc., so this is safe. */
1096 else
1098 /* Here we handle trees that are not completely rewritten.
1099 First we detect some inlining-induced bogosities for
1100 discarding. */
1101 if (TREE_CODE (*tp) == MODIFY_EXPR
1102 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
1103 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
1105 /* Some assignments VAR = VAR; don't generate any rtl code
1106 and thus don't count as variable modification. Avoid
1107 keeping bogosities like 0 = 0. */
1108 tree decl = TREE_OPERAND (*tp, 0), value;
1109 tree *n;
1111 n = id->decl_map->get (decl);
1112 if (n)
1114 value = *n;
1115 STRIP_TYPE_NOPS (value);
1116 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1118 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
1119 return copy_tree_body_r (tp, walk_subtrees, data);
1123 else if (TREE_CODE (*tp) == INDIRECT_REF)
1125 /* Get rid of *& from inline substitutions that can happen when a
1126 pointer argument is an ADDR_EXPR. */
1127 tree decl = TREE_OPERAND (*tp, 0);
1128 tree *n = id->decl_map->get (decl);
1129 if (n)
1131 /* If we happen to get an ADDR_EXPR in n->value, strip
1132 it manually here as we'll eventually get ADDR_EXPRs
1133 which lie about their types pointed to. In this case
1134 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1135 but we absolutely rely on that. As fold_indirect_ref
1136 does other useful transformations, try that first, though. */
1137 tree type = TREE_TYPE (*tp);
1138 tree ptr = id->do_not_unshare ? *n : unshare_expr (*n);
1139 tree old = *tp;
1140 *tp = gimple_fold_indirect_ref (ptr);
1141 if (! *tp)
1143 if (TREE_CODE (ptr) == ADDR_EXPR)
1146 = fold_indirect_ref_1 (EXPR_LOCATION (ptr), type, ptr);
1147 /* ??? We should either assert here or build
1148 a VIEW_CONVERT_EXPR instead of blindly leaking
1149 incompatible types to our IL. */
1150 if (! *tp)
1151 *tp = TREE_OPERAND (ptr, 0);
1153 else
1155 *tp = build1 (INDIRECT_REF, type, ptr);
1156 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1157 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1158 TREE_READONLY (*tp) = TREE_READONLY (old);
1159 /* We cannot propagate the TREE_THIS_NOTRAP flag if we
1160 have remapped a parameter as the property might be
1161 valid only for the parameter itself. */
1162 if (TREE_THIS_NOTRAP (old)
1163 && (!is_parm (TREE_OPERAND (old, 0))
1164 || (!id->transform_parameter && is_parm (ptr))))
1165 TREE_THIS_NOTRAP (*tp) = 1;
1168 *walk_subtrees = 0;
1169 return NULL;
1172 else if (TREE_CODE (*tp) == MEM_REF)
1174 /* We need to re-canonicalize MEM_REFs from inline substitutions
1175 that can happen when a pointer argument is an ADDR_EXPR.
1176 Recurse here manually to allow that. */
1177 tree ptr = TREE_OPERAND (*tp, 0);
1178 tree type = remap_type (TREE_TYPE (*tp), id);
1179 tree old = *tp;
1180 walk_tree (&ptr, copy_tree_body_r, data, NULL);
1181 *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
1182 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1183 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1184 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
1185 /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
1186 remapped a parameter as the property might be valid only
1187 for the parameter itself. */
1188 if (TREE_THIS_NOTRAP (old)
1189 && (!is_parm (TREE_OPERAND (old, 0))
1190 || (!id->transform_parameter && is_parm (ptr))))
1191 TREE_THIS_NOTRAP (*tp) = 1;
1192 *walk_subtrees = 0;
1193 return NULL;
1196 /* Here is the "usual case". Copy this tree node, and then
1197 tweak some special cases. */
1198 copy_tree_r (tp, walk_subtrees, NULL);
1200 /* If EXPR has block defined, map it to newly constructed block.
1201 When inlining we want EXPRs without block appear in the block
1202 of function call if we are not remapping a type. */
1203 if (EXPR_P (*tp))
1205 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1206 if (TREE_BLOCK (*tp))
1208 tree *n;
1209 n = id->decl_map->get (TREE_BLOCK (*tp));
1210 if (n)
1211 new_block = *n;
1213 TREE_SET_BLOCK (*tp, new_block);
1216 if (TREE_CODE (*tp) != OMP_CLAUSE)
1217 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1219 /* The copied TARGET_EXPR has never been expanded, even if the
1220 original node was expanded already. */
1221 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1223 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1224 TREE_OPERAND (*tp, 3) = NULL_TREE;
1227 /* Variable substitution need not be simple. In particular, the
1228 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1229 and friends are up-to-date. */
1230 else if (TREE_CODE (*tp) == ADDR_EXPR)
1232 int invariant = is_gimple_min_invariant (*tp);
1233 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1235 /* Handle the case where we substituted an INDIRECT_REF
1236 into the operand of the ADDR_EXPR. */
1237 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1238 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1239 else
1240 recompute_tree_invariant_for_addr_expr (*tp);
1242 /* If this used to be invariant, but is not any longer,
1243 then regimplification is probably needed. */
1244 if (invariant && !is_gimple_min_invariant (*tp))
1245 id->regimplify = true;
1247 *walk_subtrees = 0;
1251 /* Keep iterating. */
1252 return NULL_TREE;
1255 /* Helper for remap_gimple_stmt. Given an EH region number for the
1256 source function, map that to the duplicate EH region number in
1257 the destination function. */
1259 static int
1260 remap_eh_region_nr (int old_nr, copy_body_data *id)
1262 eh_region old_r, new_r;
1264 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1265 new_r = static_cast<eh_region> (*id->eh_map->get (old_r));
1267 return new_r->index;
1270 /* Similar, but operate on INTEGER_CSTs. */
1272 static tree
1273 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1275 int old_nr, new_nr;
1277 old_nr = tree_to_shwi (old_t_nr);
1278 new_nr = remap_eh_region_nr (old_nr, id);
1280 return build_int_cst (integer_type_node, new_nr);
1283 /* Helper for copy_bb. Remap statement STMT using the inlining
1284 information in ID. Return the new statement copy. */
1286 static gimple
1287 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1289 gimple copy = NULL;
1290 struct walk_stmt_info wi;
1291 bool skip_first = false;
1293 /* Begin by recognizing trees that we'll completely rewrite for the
1294 inlining context. Our output for these trees is completely
1295 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1296 into an edge). Further down, we'll handle trees that get
1297 duplicated and/or tweaked. */
1299 /* When requested, GIMPLE_RETURNs should be transformed to just the
1300 contained GIMPLE_ASSIGN. The branch semantics of the return will
1301 be handled elsewhere by manipulating the CFG rather than the
1302 statement. */
1303 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1305 tree retval = gimple_return_retval (stmt);
1307 /* If we're returning something, just turn that into an
1308 assignment into the equivalent of the original RESULT_DECL.
1309 If RETVAL is just the result decl, the result decl has
1310 already been set (e.g. a recent "foo (&result_decl, ...)");
1311 just toss the entire GIMPLE_RETURN. */
1312 if (retval
1313 && (TREE_CODE (retval) != RESULT_DECL
1314 && (TREE_CODE (retval) != SSA_NAME
1315 || ! SSA_NAME_VAR (retval)
1316 || TREE_CODE (SSA_NAME_VAR (retval)) != RESULT_DECL)))
1318 copy = gimple_build_assign (id->do_not_unshare
1319 ? id->retvar : unshare_expr (id->retvar),
1320 retval);
1321 /* id->retvar is already substituted. Skip it on later remapping. */
1322 skip_first = true;
1324 else
1325 return gimple_build_nop ();
1327 else if (gimple_has_substatements (stmt))
1329 gimple_seq s1, s2;
1331 /* When cloning bodies from the C++ front end, we will be handed bodies
1332 in High GIMPLE form. Handle here all the High GIMPLE statements that
1333 have embedded statements. */
1334 switch (gimple_code (stmt))
1336 case GIMPLE_BIND:
1337 copy = copy_gimple_bind (stmt, id);
1338 break;
1340 case GIMPLE_CATCH:
1341 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1342 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1343 break;
1345 case GIMPLE_EH_FILTER:
1346 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1347 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1348 break;
1350 case GIMPLE_TRY:
1351 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1352 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1353 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1354 break;
1356 case GIMPLE_WITH_CLEANUP_EXPR:
1357 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1358 copy = gimple_build_wce (s1);
1359 break;
1361 case GIMPLE_OMP_PARALLEL:
1362 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1363 copy = gimple_build_omp_parallel
1364 (s1,
1365 gimple_omp_parallel_clauses (stmt),
1366 gimple_omp_parallel_child_fn (stmt),
1367 gimple_omp_parallel_data_arg (stmt));
1368 break;
1370 case GIMPLE_OMP_TASK:
1371 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1372 copy = gimple_build_omp_task
1373 (s1,
1374 gimple_omp_task_clauses (stmt),
1375 gimple_omp_task_child_fn (stmt),
1376 gimple_omp_task_data_arg (stmt),
1377 gimple_omp_task_copy_fn (stmt),
1378 gimple_omp_task_arg_size (stmt),
1379 gimple_omp_task_arg_align (stmt));
1380 break;
1382 case GIMPLE_OMP_FOR:
1383 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1384 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1385 copy = gimple_build_omp_for (s1, gimple_omp_for_kind (stmt),
1386 gimple_omp_for_clauses (stmt),
1387 gimple_omp_for_collapse (stmt), s2);
1389 size_t i;
1390 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1392 gimple_omp_for_set_index (copy, i,
1393 gimple_omp_for_index (stmt, i));
1394 gimple_omp_for_set_initial (copy, i,
1395 gimple_omp_for_initial (stmt, i));
1396 gimple_omp_for_set_final (copy, i,
1397 gimple_omp_for_final (stmt, i));
1398 gimple_omp_for_set_incr (copy, i,
1399 gimple_omp_for_incr (stmt, i));
1400 gimple_omp_for_set_cond (copy, i,
1401 gimple_omp_for_cond (stmt, i));
1404 break;
1406 case GIMPLE_OMP_MASTER:
1407 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1408 copy = gimple_build_omp_master (s1);
1409 break;
1411 case GIMPLE_OMP_TASKGROUP:
1412 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1413 copy = gimple_build_omp_taskgroup (s1);
1414 break;
1416 case GIMPLE_OMP_ORDERED:
1417 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1418 copy = gimple_build_omp_ordered (s1);
1419 break;
1421 case GIMPLE_OMP_SECTION:
1422 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1423 copy = gimple_build_omp_section (s1);
1424 break;
1426 case GIMPLE_OMP_SECTIONS:
1427 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1428 copy = gimple_build_omp_sections
1429 (s1, gimple_omp_sections_clauses (stmt));
1430 break;
1432 case GIMPLE_OMP_SINGLE:
1433 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1434 copy = gimple_build_omp_single
1435 (s1, gimple_omp_single_clauses (stmt));
1436 break;
1438 case GIMPLE_OMP_TARGET:
1439 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1440 copy = gimple_build_omp_target
1441 (s1, gimple_omp_target_kind (stmt),
1442 gimple_omp_target_clauses (stmt));
1443 break;
1445 case GIMPLE_OMP_TEAMS:
1446 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1447 copy = gimple_build_omp_teams
1448 (s1, gimple_omp_teams_clauses (stmt));
1449 break;
1451 case GIMPLE_OMP_CRITICAL:
1452 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1453 copy
1454 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1455 break;
1457 case GIMPLE_TRANSACTION:
1458 s1 = remap_gimple_seq (gimple_transaction_body (stmt), id);
1459 copy = gimple_build_transaction (s1, gimple_transaction_label (stmt));
1460 gimple_transaction_set_subcode (copy, gimple_transaction_subcode (stmt));
1461 break;
1463 default:
1464 gcc_unreachable ();
1467 else
1469 if (gimple_assign_copy_p (stmt)
1470 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1471 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1473 /* Here we handle statements that are not completely rewritten.
1474 First we detect some inlining-induced bogosities for
1475 discarding. */
1477 /* Some assignments VAR = VAR; don't generate any rtl code
1478 and thus don't count as variable modification. Avoid
1479 keeping bogosities like 0 = 0. */
1480 tree decl = gimple_assign_lhs (stmt), value;
1481 tree *n;
1483 n = id->decl_map->get (decl);
1484 if (n)
1486 value = *n;
1487 STRIP_TYPE_NOPS (value);
1488 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1489 return gimple_build_nop ();
1493 /* For *ptr_N ={v} {CLOBBER}, if ptr_N is SSA_NAME defined
1494 in a block that we aren't copying during tree_function_versioning,
1495 just drop the clobber stmt. */
1496 if (id->blocks_to_copy && gimple_clobber_p (stmt))
1498 tree lhs = gimple_assign_lhs (stmt);
1499 if (TREE_CODE (lhs) == MEM_REF
1500 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
1502 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0));
1503 if (gimple_bb (def_stmt)
1504 && !bitmap_bit_p (id->blocks_to_copy,
1505 gimple_bb (def_stmt)->index))
1506 return gimple_build_nop ();
1510 if (gimple_debug_bind_p (stmt))
1512 copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1513 gimple_debug_bind_get_value (stmt),
1514 stmt);
1515 id->debug_stmts.safe_push (copy);
1516 return copy;
1518 if (gimple_debug_source_bind_p (stmt))
1520 copy = gimple_build_debug_source_bind
1521 (gimple_debug_source_bind_get_var (stmt),
1522 gimple_debug_source_bind_get_value (stmt), stmt);
1523 id->debug_stmts.safe_push (copy);
1524 return copy;
1527 /* Create a new deep copy of the statement. */
1528 copy = gimple_copy (stmt);
1530 /* Clear flags that need revisiting. */
1531 if (is_gimple_call (copy)
1532 && gimple_call_tail_p (copy))
1533 gimple_call_set_tail (copy, false);
1535 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1536 RESX and EH_DISPATCH. */
1537 if (id->eh_map)
1538 switch (gimple_code (copy))
1540 case GIMPLE_CALL:
1542 tree r, fndecl = gimple_call_fndecl (copy);
1543 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1544 switch (DECL_FUNCTION_CODE (fndecl))
1546 case BUILT_IN_EH_COPY_VALUES:
1547 r = gimple_call_arg (copy, 1);
1548 r = remap_eh_region_tree_nr (r, id);
1549 gimple_call_set_arg (copy, 1, r);
1550 /* FALLTHRU */
1552 case BUILT_IN_EH_POINTER:
1553 case BUILT_IN_EH_FILTER:
1554 r = gimple_call_arg (copy, 0);
1555 r = remap_eh_region_tree_nr (r, id);
1556 gimple_call_set_arg (copy, 0, r);
1557 break;
1559 default:
1560 break;
1563 /* Reset alias info if we didn't apply measures to
1564 keep it valid over inlining by setting DECL_PT_UID. */
1565 if (!id->src_cfun->gimple_df
1566 || !id->src_cfun->gimple_df->ipa_pta)
1567 gimple_call_reset_alias_info (copy);
1569 break;
1571 case GIMPLE_RESX:
1573 int r = gimple_resx_region (copy);
1574 r = remap_eh_region_nr (r, id);
1575 gimple_resx_set_region (copy, r);
1577 break;
1579 case GIMPLE_EH_DISPATCH:
1581 int r = gimple_eh_dispatch_region (copy);
1582 r = remap_eh_region_nr (r, id);
1583 gimple_eh_dispatch_set_region (copy, r);
1585 break;
1587 default:
1588 break;
1592 /* If STMT has a block defined, map it to the newly constructed
1593 block. */
1594 if (gimple_block (copy))
1596 tree *n;
1597 n = id->decl_map->get (gimple_block (copy));
1598 gcc_assert (n);
1599 gimple_set_block (copy, *n);
1602 if (gimple_debug_bind_p (copy) || gimple_debug_source_bind_p (copy))
1603 return copy;
1605 /* Remap all the operands in COPY. */
1606 memset (&wi, 0, sizeof (wi));
1607 wi.info = id;
1608 if (skip_first)
1609 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1610 else
1611 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1613 /* Clear the copied virtual operands. We are not remapping them here
1614 but are going to recreate them from scratch. */
1615 if (gimple_has_mem_ops (copy))
1617 gimple_set_vdef (copy, NULL_TREE);
1618 gimple_set_vuse (copy, NULL_TREE);
1621 return copy;
1625 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1626 later */
1628 static basic_block
1629 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1630 gcov_type count_scale)
1632 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1633 basic_block copy_basic_block;
1634 tree decl;
1635 gcov_type freq;
1636 basic_block prev;
1638 /* Search for previous copied basic block. */
1639 prev = bb->prev_bb;
1640 while (!prev->aux)
1641 prev = prev->prev_bb;
1643 /* create_basic_block() will append every new block to
1644 basic_block_info automatically. */
1645 copy_basic_block = create_basic_block (NULL, (void *) 0,
1646 (basic_block) prev->aux);
1647 copy_basic_block->count = apply_scale (bb->count, count_scale);
1649 /* We are going to rebuild frequencies from scratch. These values
1650 have just small importance to drive canonicalize_loop_headers. */
1651 freq = apply_scale ((gcov_type)bb->frequency, frequency_scale);
1653 /* We recompute frequencies after inlining, so this is quite safe. */
1654 if (freq > BB_FREQ_MAX)
1655 freq = BB_FREQ_MAX;
1656 copy_basic_block->frequency = freq;
1658 copy_gsi = gsi_start_bb (copy_basic_block);
1660 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1662 gimple stmt = gsi_stmt (gsi);
1663 gimple orig_stmt = stmt;
1665 id->regimplify = false;
1666 stmt = remap_gimple_stmt (stmt, id);
1667 if (gimple_nop_p (stmt))
1668 continue;
1670 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1671 seq_gsi = copy_gsi;
1673 /* With return slot optimization we can end up with
1674 non-gimple (foo *)&this->m, fix that here. */
1675 if (is_gimple_assign (stmt)
1676 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1677 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1679 tree new_rhs;
1680 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1681 gimple_assign_rhs1 (stmt),
1682 true, NULL, false,
1683 GSI_CONTINUE_LINKING);
1684 gimple_assign_set_rhs1 (stmt, new_rhs);
1685 id->regimplify = false;
1688 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1690 if (id->regimplify)
1691 gimple_regimplify_operands (stmt, &seq_gsi);
1693 /* If copy_basic_block has been empty at the start of this iteration,
1694 call gsi_start_bb again to get at the newly added statements. */
1695 if (gsi_end_p (copy_gsi))
1696 copy_gsi = gsi_start_bb (copy_basic_block);
1697 else
1698 gsi_next (&copy_gsi);
1700 /* Process the new statement. The call to gimple_regimplify_operands
1701 possibly turned the statement into multiple statements, we
1702 need to process all of them. */
1705 tree fn;
1707 stmt = gsi_stmt (copy_gsi);
1708 if (is_gimple_call (stmt)
1709 && gimple_call_va_arg_pack_p (stmt)
1710 && id->gimple_call)
1712 /* __builtin_va_arg_pack () should be replaced by
1713 all arguments corresponding to ... in the caller. */
1714 tree p;
1715 gimple new_call;
1716 vec<tree> argarray;
1717 size_t nargs = gimple_call_num_args (id->gimple_call);
1718 size_t n;
1720 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1721 nargs--;
1723 /* Create the new array of arguments. */
1724 n = nargs + gimple_call_num_args (stmt);
1725 argarray.create (n);
1726 argarray.safe_grow_cleared (n);
1728 /* Copy all the arguments before '...' */
1729 memcpy (argarray.address (),
1730 gimple_call_arg_ptr (stmt, 0),
1731 gimple_call_num_args (stmt) * sizeof (tree));
1733 /* Append the arguments passed in '...' */
1734 memcpy (argarray.address () + gimple_call_num_args (stmt),
1735 gimple_call_arg_ptr (id->gimple_call, 0)
1736 + (gimple_call_num_args (id->gimple_call) - nargs),
1737 nargs * sizeof (tree));
1739 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1740 argarray);
1742 argarray.release ();
1744 /* Copy all GIMPLE_CALL flags, location and block, except
1745 GF_CALL_VA_ARG_PACK. */
1746 gimple_call_copy_flags (new_call, stmt);
1747 gimple_call_set_va_arg_pack (new_call, false);
1748 gimple_set_location (new_call, gimple_location (stmt));
1749 gimple_set_block (new_call, gimple_block (stmt));
1750 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1752 gsi_replace (&copy_gsi, new_call, false);
1753 stmt = new_call;
1755 else if (is_gimple_call (stmt)
1756 && id->gimple_call
1757 && (decl = gimple_call_fndecl (stmt))
1758 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1759 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1761 /* __builtin_va_arg_pack_len () should be replaced by
1762 the number of anonymous arguments. */
1763 size_t nargs = gimple_call_num_args (id->gimple_call);
1764 tree count, p;
1765 gimple new_stmt;
1767 for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
1768 nargs--;
1770 count = build_int_cst (integer_type_node, nargs);
1771 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1772 gsi_replace (&copy_gsi, new_stmt, false);
1773 stmt = new_stmt;
1776 /* Statements produced by inlining can be unfolded, especially
1777 when we constant propagated some operands. We can't fold
1778 them right now for two reasons:
1779 1) folding require SSA_NAME_DEF_STMTs to be correct
1780 2) we can't change function calls to builtins.
1781 So we just mark statement for later folding. We mark
1782 all new statements, instead just statements that has changed
1783 by some nontrivial substitution so even statements made
1784 foldable indirectly are updated. If this turns out to be
1785 expensive, copy_body can be told to watch for nontrivial
1786 changes. */
1787 if (id->statements_to_fold)
1788 id->statements_to_fold->add (stmt);
1790 /* We're duplicating a CALL_EXPR. Find any corresponding
1791 callgraph edges and update or duplicate them. */
1792 if (is_gimple_call (stmt))
1794 struct cgraph_edge *edge;
1796 switch (id->transform_call_graph_edges)
1798 case CB_CGE_DUPLICATE:
1799 edge = id->src_node->get_edge (orig_stmt);
1800 if (edge)
1802 int edge_freq = edge->frequency;
1803 int new_freq;
1804 struct cgraph_edge *old_edge = edge;
1805 edge = edge->clone (id->dst_node, stmt,
1806 gimple_uid (stmt),
1807 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1808 true);
1809 /* We could also just rescale the frequency, but
1810 doing so would introduce roundoff errors and make
1811 verifier unhappy. */
1812 new_freq = compute_call_stmt_bb_frequency (id->dst_node->decl,
1813 copy_basic_block);
1815 /* Speculative calls consist of two edges - direct and indirect.
1816 Duplicate the whole thing and distribute frequencies accordingly. */
1817 if (edge->speculative)
1819 struct cgraph_edge *direct, *indirect;
1820 struct ipa_ref *ref;
1822 gcc_assert (!edge->indirect_unknown_callee);
1823 old_edge->speculative_call_info (direct, indirect, ref);
1824 indirect = indirect->clone (id->dst_node, stmt,
1825 gimple_uid (stmt),
1826 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1827 true);
1828 if (old_edge->frequency + indirect->frequency)
1830 edge->frequency = MIN (RDIV ((gcov_type)new_freq * old_edge->frequency,
1831 (old_edge->frequency + indirect->frequency)),
1832 CGRAPH_FREQ_MAX);
1833 indirect->frequency = MIN (RDIV ((gcov_type)new_freq * indirect->frequency,
1834 (old_edge->frequency + indirect->frequency)),
1835 CGRAPH_FREQ_MAX);
1837 id->dst_node->clone_reference (ref, stmt);
1839 else
1841 edge->frequency = new_freq;
1842 if (dump_file
1843 && profile_status_for_fn (cfun) != PROFILE_ABSENT
1844 && (edge_freq > edge->frequency + 10
1845 || edge_freq < edge->frequency - 10))
1847 fprintf (dump_file, "Edge frequency estimated by "
1848 "cgraph %i diverge from inliner's estimate %i\n",
1849 edge_freq,
1850 edge->frequency);
1851 fprintf (dump_file,
1852 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
1853 bb->index,
1854 bb->frequency,
1855 copy_basic_block->frequency);
1859 break;
1861 case CB_CGE_MOVE_CLONES:
1862 id->dst_node->set_call_stmt_including_clones (orig_stmt,
1863 stmt);
1864 edge = id->dst_node->get_edge (stmt);
1865 break;
1867 case CB_CGE_MOVE:
1868 edge = id->dst_node->get_edge (orig_stmt);
1869 if (edge)
1870 edge->set_call_stmt (stmt);
1871 break;
1873 default:
1874 gcc_unreachable ();
1877 /* Constant propagation on argument done during inlining
1878 may create new direct call. Produce an edge for it. */
1879 if ((!edge
1880 || (edge->indirect_inlining_edge
1881 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1882 && id->dst_node->definition
1883 && (fn = gimple_call_fndecl (stmt)) != NULL)
1885 struct cgraph_node *dest = cgraph_node::get (fn);
1887 /* We have missing edge in the callgraph. This can happen
1888 when previous inlining turned an indirect call into a
1889 direct call by constant propagating arguments or we are
1890 producing dead clone (for further cloning). In all
1891 other cases we hit a bug (incorrect node sharing is the
1892 most common reason for missing edges). */
1893 gcc_assert (!dest->definition
1894 || dest->address_taken
1895 || !id->src_node->definition
1896 || !id->dst_node->definition);
1897 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1898 id->dst_node->create_edge_including_clones
1899 (dest, orig_stmt, stmt, bb->count,
1900 compute_call_stmt_bb_frequency (id->dst_node->decl,
1901 copy_basic_block),
1902 CIF_ORIGINALLY_INDIRECT_CALL);
1903 else
1904 id->dst_node->create_edge (dest, stmt,
1905 bb->count,
1906 compute_call_stmt_bb_frequency
1907 (id->dst_node->decl,
1908 copy_basic_block))->inline_failed
1909 = CIF_ORIGINALLY_INDIRECT_CALL;
1910 if (dump_file)
1912 fprintf (dump_file, "Created new direct edge to %s\n",
1913 dest->name ());
1917 notice_special_calls (stmt);
1920 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
1921 id->eh_map, id->eh_lp_nr);
1923 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
1925 ssa_op_iter i;
1926 tree def;
1928 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1929 if (TREE_CODE (def) == SSA_NAME)
1930 SSA_NAME_DEF_STMT (def) = stmt;
1933 gsi_next (&copy_gsi);
1935 while (!gsi_end_p (copy_gsi));
1937 copy_gsi = gsi_last_bb (copy_basic_block);
1940 return copy_basic_block;
1943 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1944 form is quite easy, since dominator relationship for old basic blocks does
1945 not change.
1947 There is however exception where inlining might change dominator relation
1948 across EH edges from basic block within inlined functions destinating
1949 to landing pads in function we inline into.
1951 The function fills in PHI_RESULTs of such PHI nodes if they refer
1952 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1953 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1954 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1955 set, and this means that there will be no overlapping live ranges
1956 for the underlying symbol.
1958 This might change in future if we allow redirecting of EH edges and
1959 we might want to change way build CFG pre-inlining to include
1960 all the possible edges then. */
1961 static void
1962 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1963 bool can_throw, bool nonlocal_goto)
1965 edge e;
1966 edge_iterator ei;
1968 FOR_EACH_EDGE (e, ei, bb->succs)
1969 if (!e->dest->aux
1970 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1972 gimple phi;
1973 gimple_stmt_iterator si;
1975 if (!nonlocal_goto)
1976 gcc_assert (e->flags & EDGE_EH);
1978 if (!can_throw)
1979 gcc_assert (!(e->flags & EDGE_EH));
1981 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1983 edge re;
1985 phi = gsi_stmt (si);
1987 /* For abnormal goto/call edges the receiver can be the
1988 ENTRY_BLOCK. Do not assert this cannot happen. */
1990 gcc_assert ((e->flags & EDGE_EH)
1991 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1993 re = find_edge (ret_bb, e->dest);
1994 gcc_checking_assert (re);
1995 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1996 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1998 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1999 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
2005 /* Copy edges from BB into its copy constructed earlier, scale profile
2006 accordingly. Edges will be taken care of later. Assume aux
2007 pointers to point to the copies of each BB. Return true if any
2008 debug stmts are left after a statement that must end the basic block. */
2010 static bool
2011 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb,
2012 basic_block abnormal_goto_dest)
2014 basic_block new_bb = (basic_block) bb->aux;
2015 edge_iterator ei;
2016 edge old_edge;
2017 gimple_stmt_iterator si;
2018 int flags;
2019 bool need_debug_cleanup = false;
2021 /* Use the indices from the original blocks to create edges for the
2022 new ones. */
2023 FOR_EACH_EDGE (old_edge, ei, bb->succs)
2024 if (!(old_edge->flags & EDGE_EH))
2026 edge new_edge;
2028 flags = old_edge->flags;
2030 /* Return edges do get a FALLTHRU flag when the get inlined. */
2031 if (old_edge->dest->index == EXIT_BLOCK
2032 && !(old_edge->flags & (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE|EDGE_FAKE))
2033 && old_edge->dest->aux != EXIT_BLOCK_PTR_FOR_FN (cfun))
2034 flags |= EDGE_FALLTHRU;
2035 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
2036 new_edge->count = apply_scale (old_edge->count, count_scale);
2037 new_edge->probability = old_edge->probability;
2040 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
2041 return false;
2043 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
2045 gimple copy_stmt;
2046 bool can_throw, nonlocal_goto;
2048 copy_stmt = gsi_stmt (si);
2049 if (!is_gimple_debug (copy_stmt))
2050 update_stmt (copy_stmt);
2052 /* Do this before the possible split_block. */
2053 gsi_next (&si);
2055 /* If this tree could throw an exception, there are two
2056 cases where we need to add abnormal edge(s): the
2057 tree wasn't in a region and there is a "current
2058 region" in the caller; or the original tree had
2059 EH edges. In both cases split the block after the tree,
2060 and add abnormal edge(s) as needed; we need both
2061 those from the callee and the caller.
2062 We check whether the copy can throw, because the const
2063 propagation can change an INDIRECT_REF which throws
2064 into a COMPONENT_REF which doesn't. If the copy
2065 can throw, the original could also throw. */
2066 can_throw = stmt_can_throw_internal (copy_stmt);
2067 nonlocal_goto
2068 = (stmt_can_make_abnormal_goto (copy_stmt)
2069 && !computed_goto_p (copy_stmt));
2071 if (can_throw || nonlocal_goto)
2073 if (!gsi_end_p (si))
2075 while (!gsi_end_p (si) && is_gimple_debug (gsi_stmt (si)))
2076 gsi_next (&si);
2077 if (gsi_end_p (si))
2078 need_debug_cleanup = true;
2080 if (!gsi_end_p (si))
2081 /* Note that bb's predecessor edges aren't necessarily
2082 right at this point; split_block doesn't care. */
2084 edge e = split_block (new_bb, copy_stmt);
2086 new_bb = e->dest;
2087 new_bb->aux = e->src->aux;
2088 si = gsi_start_bb (new_bb);
2092 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
2093 make_eh_dispatch_edges (copy_stmt);
2094 else if (can_throw)
2095 make_eh_edges (copy_stmt);
2097 /* If the call we inline cannot make abnormal goto do not add
2098 additional abnormal edges but only retain those already present
2099 in the original function body. */
2100 if (abnormal_goto_dest == NULL)
2101 nonlocal_goto = false;
2102 if (nonlocal_goto)
2104 basic_block copy_stmt_bb = gimple_bb (copy_stmt);
2106 if (get_abnormal_succ_dispatcher (copy_stmt_bb))
2107 nonlocal_goto = false;
2108 /* ABNORMAL_DISPATCHER (1) is for longjmp/setjmp or nonlocal gotos
2109 in OpenMP regions which aren't allowed to be left abnormally.
2110 So, no need to add abnormal edge in that case. */
2111 else if (is_gimple_call (copy_stmt)
2112 && gimple_call_internal_p (copy_stmt)
2113 && (gimple_call_internal_fn (copy_stmt)
2114 == IFN_ABNORMAL_DISPATCHER)
2115 && gimple_call_arg (copy_stmt, 0) == boolean_true_node)
2116 nonlocal_goto = false;
2117 else
2118 make_edge (copy_stmt_bb, abnormal_goto_dest, EDGE_ABNORMAL);
2121 if ((can_throw || nonlocal_goto)
2122 && gimple_in_ssa_p (cfun))
2123 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
2124 can_throw, nonlocal_goto);
2126 return need_debug_cleanup;
2129 /* Copy the PHIs. All blocks and edges are copied, some blocks
2130 was possibly split and new outgoing EH edges inserted.
2131 BB points to the block of original function and AUX pointers links
2132 the original and newly copied blocks. */
2134 static void
2135 copy_phis_for_bb (basic_block bb, copy_body_data *id)
2137 basic_block const new_bb = (basic_block) bb->aux;
2138 edge_iterator ei;
2139 gimple phi;
2140 gimple_stmt_iterator si;
2141 edge new_edge;
2142 bool inserted = false;
2144 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
2146 tree res, new_res;
2147 gimple new_phi;
2149 phi = gsi_stmt (si);
2150 res = PHI_RESULT (phi);
2151 new_res = res;
2152 if (!virtual_operand_p (res))
2154 walk_tree (&new_res, copy_tree_body_r, id, NULL);
2155 new_phi = create_phi_node (new_res, new_bb);
2156 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2158 edge old_edge = find_edge ((basic_block) new_edge->src->aux, bb);
2159 tree arg;
2160 tree new_arg;
2161 edge_iterator ei2;
2162 location_t locus;
2164 /* When doing partial cloning, we allow PHIs on the entry block
2165 as long as all the arguments are the same. Find any input
2166 edge to see argument to copy. */
2167 if (!old_edge)
2168 FOR_EACH_EDGE (old_edge, ei2, bb->preds)
2169 if (!old_edge->src->aux)
2170 break;
2172 arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
2173 new_arg = arg;
2174 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
2175 gcc_assert (new_arg);
2176 /* With return slot optimization we can end up with
2177 non-gimple (foo *)&this->m, fix that here. */
2178 if (TREE_CODE (new_arg) != SSA_NAME
2179 && TREE_CODE (new_arg) != FUNCTION_DECL
2180 && !is_gimple_val (new_arg))
2182 gimple_seq stmts = NULL;
2183 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
2184 gsi_insert_seq_on_edge (new_edge, stmts);
2185 inserted = true;
2187 locus = gimple_phi_arg_location_from_edge (phi, old_edge);
2188 if (LOCATION_BLOCK (locus))
2190 tree *n;
2191 n = id->decl_map->get (LOCATION_BLOCK (locus));
2192 gcc_assert (n);
2193 if (*n)
2194 locus = COMBINE_LOCATION_DATA (line_table, locus, *n);
2195 else
2196 locus = LOCATION_LOCUS (locus);
2198 else
2199 locus = LOCATION_LOCUS (locus);
2201 add_phi_arg (new_phi, new_arg, new_edge, locus);
2206 /* Commit the delayed edge insertions. */
2207 if (inserted)
2208 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
2209 gsi_commit_one_edge_insert (new_edge, NULL);
2213 /* Wrapper for remap_decl so it can be used as a callback. */
2215 static tree
2216 remap_decl_1 (tree decl, void *data)
2218 return remap_decl (decl, (copy_body_data *) data);
2221 /* Build struct function and associated datastructures for the new clone
2222 NEW_FNDECL to be build. CALLEE_FNDECL is the original. Function changes
2223 the cfun to the function of new_fndecl (and current_function_decl too). */
2225 static void
2226 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
2228 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2229 gcov_type count_scale;
2231 if (!DECL_ARGUMENTS (new_fndecl))
2232 DECL_ARGUMENTS (new_fndecl) = DECL_ARGUMENTS (callee_fndecl);
2233 if (!DECL_RESULT (new_fndecl))
2234 DECL_RESULT (new_fndecl) = DECL_RESULT (callee_fndecl);
2236 if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
2237 count_scale
2238 = GCOV_COMPUTE_SCALE (count,
2239 ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
2240 else
2241 count_scale = REG_BR_PROB_BASE;
2243 /* Register specific tree functions. */
2244 gimple_register_cfg_hooks ();
2246 /* Get clean struct function. */
2247 push_struct_function (new_fndecl);
2249 /* We will rebuild these, so just sanity check that they are empty. */
2250 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
2251 gcc_assert (cfun->local_decls == NULL);
2252 gcc_assert (cfun->cfg == NULL);
2253 gcc_assert (cfun->decl == new_fndecl);
2255 /* Copy items we preserve during cloning. */
2256 cfun->static_chain_decl = src_cfun->static_chain_decl;
2257 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
2258 cfun->function_end_locus = src_cfun->function_end_locus;
2259 cfun->curr_properties = src_cfun->curr_properties;
2260 cfun->last_verified = src_cfun->last_verified;
2261 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2262 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
2263 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2264 cfun->stdarg = src_cfun->stdarg;
2265 cfun->after_inlining = src_cfun->after_inlining;
2266 cfun->can_throw_non_call_exceptions
2267 = src_cfun->can_throw_non_call_exceptions;
2268 cfun->can_delete_dead_exceptions = src_cfun->can_delete_dead_exceptions;
2269 cfun->returns_struct = src_cfun->returns_struct;
2270 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
2272 init_empty_tree_cfg ();
2274 profile_status_for_fn (cfun) = profile_status_for_fn (src_cfun);
2275 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
2276 (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
2277 REG_BR_PROB_BASE);
2278 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
2279 = ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
2280 EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
2281 (EXIT_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
2282 REG_BR_PROB_BASE);
2283 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency =
2284 EXIT_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
2285 if (src_cfun->eh)
2286 init_eh_for_function ();
2288 if (src_cfun->gimple_df)
2290 init_tree_ssa (cfun);
2291 cfun->gimple_df->in_ssa_p = true;
2292 init_ssa_operands (cfun);
2296 /* Helper function for copy_cfg_body. Move debug stmts from the end
2297 of NEW_BB to the beginning of successor basic blocks when needed. If the
2298 successor has multiple predecessors, reset them, otherwise keep
2299 their value. */
2301 static void
2302 maybe_move_debug_stmts_to_successors (copy_body_data *id, basic_block new_bb)
2304 edge e;
2305 edge_iterator ei;
2306 gimple_stmt_iterator si = gsi_last_nondebug_bb (new_bb);
2308 if (gsi_end_p (si)
2309 || gsi_one_before_end_p (si)
2310 || !(stmt_can_throw_internal (gsi_stmt (si))
2311 || stmt_can_make_abnormal_goto (gsi_stmt (si))))
2312 return;
2314 FOR_EACH_EDGE (e, ei, new_bb->succs)
2316 gimple_stmt_iterator ssi = gsi_last_bb (new_bb);
2317 gimple_stmt_iterator dsi = gsi_after_labels (e->dest);
2318 while (is_gimple_debug (gsi_stmt (ssi)))
2320 gimple stmt = gsi_stmt (ssi), new_stmt;
2321 tree var;
2322 tree value;
2324 /* For the last edge move the debug stmts instead of copying
2325 them. */
2326 if (ei_one_before_end_p (ei))
2328 si = ssi;
2329 gsi_prev (&ssi);
2330 if (!single_pred_p (e->dest) && gimple_debug_bind_p (stmt))
2331 gimple_debug_bind_reset_value (stmt);
2332 gsi_remove (&si, false);
2333 gsi_insert_before (&dsi, stmt, GSI_SAME_STMT);
2334 continue;
2337 if (gimple_debug_bind_p (stmt))
2339 var = gimple_debug_bind_get_var (stmt);
2340 if (single_pred_p (e->dest))
2342 value = gimple_debug_bind_get_value (stmt);
2343 value = unshare_expr (value);
2345 else
2346 value = NULL_TREE;
2347 new_stmt = gimple_build_debug_bind (var, value, stmt);
2349 else if (gimple_debug_source_bind_p (stmt))
2351 var = gimple_debug_source_bind_get_var (stmt);
2352 value = gimple_debug_source_bind_get_value (stmt);
2353 new_stmt = gimple_build_debug_source_bind (var, value, stmt);
2355 else
2356 gcc_unreachable ();
2357 gsi_insert_before (&dsi, new_stmt, GSI_SAME_STMT);
2358 id->debug_stmts.safe_push (new_stmt);
2359 gsi_prev (&ssi);
2364 /* Make a copy of the sub-loops of SRC_PARENT and place them
2365 as siblings of DEST_PARENT. */
2367 static void
2368 copy_loops (copy_body_data *id,
2369 struct loop *dest_parent, struct loop *src_parent)
2371 struct loop *src_loop = src_parent->inner;
2372 while (src_loop)
2374 if (!id->blocks_to_copy
2375 || bitmap_bit_p (id->blocks_to_copy, src_loop->header->index))
2377 struct loop *dest_loop = alloc_loop ();
2379 /* Assign the new loop its header and latch and associate
2380 those with the new loop. */
2381 dest_loop->header = (basic_block)src_loop->header->aux;
2382 dest_loop->header->loop_father = dest_loop;
2383 if (src_loop->latch != NULL)
2385 dest_loop->latch = (basic_block)src_loop->latch->aux;
2386 dest_loop->latch->loop_father = dest_loop;
2389 /* Copy loop meta-data. */
2390 copy_loop_info (src_loop, dest_loop);
2392 /* Finally place it into the loop array and the loop tree. */
2393 place_new_loop (cfun, dest_loop);
2394 flow_loop_tree_node_add (dest_parent, dest_loop);
2396 dest_loop->safelen = src_loop->safelen;
2397 dest_loop->dont_vectorize = src_loop->dont_vectorize;
2398 if (src_loop->force_vectorize)
2400 dest_loop->force_vectorize = true;
2401 cfun->has_force_vectorize_loops = true;
2403 if (src_loop->simduid)
2405 dest_loop->simduid = remap_decl (src_loop->simduid, id);
2406 cfun->has_simduid_loops = true;
2409 /* Recurse. */
2410 copy_loops (id, dest_loop, src_loop);
2412 src_loop = src_loop->next;
2416 /* Call cgraph_redirect_edge_call_stmt_to_callee on all calls in BB */
2418 void
2419 redirect_all_calls (copy_body_data * id, basic_block bb)
2421 gimple_stmt_iterator si;
2422 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2424 if (is_gimple_call (gsi_stmt (si)))
2426 struct cgraph_edge *edge = id->dst_node->get_edge (gsi_stmt (si));
2427 if (edge)
2428 edge->redirect_call_stmt_to_callee ();
2433 /* Convert estimated frequencies into counts for NODE, scaling COUNT
2434 with each bb's frequency. Used when NODE has a 0-weight entry
2435 but we are about to inline it into a non-zero count call bb.
2436 See the comments for handle_missing_profiles() in predict.c for
2437 when this can happen for COMDATs. */
2439 void
2440 freqs_to_counts (struct cgraph_node *node, gcov_type count)
2442 basic_block bb;
2443 edge_iterator ei;
2444 edge e;
2445 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2447 FOR_ALL_BB_FN(bb, fn)
2449 bb->count = apply_scale (count,
2450 GCOV_COMPUTE_SCALE (bb->frequency, BB_FREQ_MAX));
2451 FOR_EACH_EDGE (e, ei, bb->succs)
2452 e->count = apply_probability (e->src->count, e->probability);
2456 /* Make a copy of the body of FN so that it can be inserted inline in
2457 another function. Walks FN via CFG, returns new fndecl. */
2459 static tree
2460 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
2461 basic_block entry_block_map, basic_block exit_block_map,
2462 basic_block new_entry)
2464 tree callee_fndecl = id->src_fn;
2465 /* Original cfun for the callee, doesn't change. */
2466 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2467 struct function *cfun_to_copy;
2468 basic_block bb;
2469 tree new_fndecl = NULL;
2470 bool need_debug_cleanup = false;
2471 gcov_type count_scale;
2472 int last;
2473 int incoming_frequency = 0;
2474 gcov_type incoming_count = 0;
2476 /* This can happen for COMDAT routines that end up with 0 counts
2477 despite being called (see the comments for handle_missing_profiles()
2478 in predict.c as to why). Apply counts to the blocks in the callee
2479 before inlining, using the guessed edge frequencies, so that we don't
2480 end up with a 0-count inline body which can confuse downstream
2481 optimizations such as function splitting. */
2482 if (!ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count && count)
2484 /* Apply the larger of the call bb count and the total incoming
2485 call edge count to the callee. */
2486 gcov_type in_count = 0;
2487 struct cgraph_edge *in_edge;
2488 for (in_edge = id->src_node->callers; in_edge;
2489 in_edge = in_edge->next_caller)
2490 in_count += in_edge->count;
2491 freqs_to_counts (id->src_node, count > in_count ? count : in_count);
2494 if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
2495 count_scale
2496 = GCOV_COMPUTE_SCALE (count,
2497 ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
2498 else
2499 count_scale = REG_BR_PROB_BASE;
2501 /* Register specific tree functions. */
2502 gimple_register_cfg_hooks ();
2504 /* If we are inlining just region of the function, make sure to connect
2505 new entry to ENTRY_BLOCK_PTR_FOR_FN (cfun). Since new entry can be
2506 part of loop, we must compute frequency and probability of
2507 ENTRY_BLOCK_PTR_FOR_FN (cfun) based on the frequencies and
2508 probabilities of edges incoming from nonduplicated region. */
2509 if (new_entry)
2511 edge e;
2512 edge_iterator ei;
2514 FOR_EACH_EDGE (e, ei, new_entry->preds)
2515 if (!e->src->aux)
2517 incoming_frequency += EDGE_FREQUENCY (e);
2518 incoming_count += e->count;
2520 incoming_count = apply_scale (incoming_count, count_scale);
2521 incoming_frequency
2522 = apply_scale ((gcov_type)incoming_frequency, frequency_scale);
2523 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = incoming_count;
2524 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = incoming_frequency;
2527 /* Must have a CFG here at this point. */
2528 gcc_assert (ENTRY_BLOCK_PTR_FOR_FN
2529 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2531 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2533 ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = entry_block_map;
2534 EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = exit_block_map;
2535 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy);
2536 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy);
2538 /* Duplicate any exception-handling regions. */
2539 if (cfun->eh)
2540 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2541 remap_decl_1, id);
2543 /* Use aux pointers to map the original blocks to copy. */
2544 FOR_EACH_BB_FN (bb, cfun_to_copy)
2545 if (!id->blocks_to_copy || bitmap_bit_p (id->blocks_to_copy, bb->index))
2547 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2548 bb->aux = new_bb;
2549 new_bb->aux = bb;
2550 new_bb->loop_father = entry_block_map->loop_father;
2553 last = last_basic_block_for_fn (cfun);
2555 /* Now that we've duplicated the blocks, duplicate their edges. */
2556 basic_block abnormal_goto_dest = NULL;
2557 if (id->gimple_call
2558 && stmt_can_make_abnormal_goto (id->gimple_call))
2560 gimple_stmt_iterator gsi = gsi_for_stmt (id->gimple_call);
2562 bb = gimple_bb (id->gimple_call);
2563 gsi_next (&gsi);
2564 if (gsi_end_p (gsi))
2565 abnormal_goto_dest = get_abnormal_succ_dispatcher (bb);
2567 FOR_ALL_BB_FN (bb, cfun_to_copy)
2568 if (!id->blocks_to_copy
2569 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
2570 need_debug_cleanup |= copy_edges_for_bb (bb, count_scale, exit_block_map,
2571 abnormal_goto_dest);
2573 if (new_entry)
2575 edge e = make_edge (entry_block_map, (basic_block)new_entry->aux, EDGE_FALLTHRU);
2576 e->probability = REG_BR_PROB_BASE;
2577 e->count = incoming_count;
2580 /* Duplicate the loop tree, if available and wanted. */
2581 if (loops_for_fn (src_cfun) != NULL
2582 && current_loops != NULL)
2584 copy_loops (id, entry_block_map->loop_father,
2585 get_loop (src_cfun, 0));
2586 /* Defer to cfgcleanup to update loop-father fields of basic-blocks. */
2587 loops_state_set (LOOPS_NEED_FIXUP);
2590 /* If the loop tree in the source function needed fixup, mark the
2591 destination loop tree for fixup, too. */
2592 if (loops_for_fn (src_cfun)->state & LOOPS_NEED_FIXUP)
2593 loops_state_set (LOOPS_NEED_FIXUP);
2595 if (gimple_in_ssa_p (cfun))
2596 FOR_ALL_BB_FN (bb, cfun_to_copy)
2597 if (!id->blocks_to_copy
2598 || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
2599 copy_phis_for_bb (bb, id);
2601 FOR_ALL_BB_FN (bb, cfun_to_copy)
2602 if (bb->aux)
2604 if (need_debug_cleanup
2605 && bb->index != ENTRY_BLOCK
2606 && bb->index != EXIT_BLOCK)
2607 maybe_move_debug_stmts_to_successors (id, (basic_block) bb->aux);
2608 /* Update call edge destinations. This can not be done before loop
2609 info is updated, because we may split basic blocks. */
2610 if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
2611 redirect_all_calls (id, (basic_block)bb->aux);
2612 ((basic_block)bb->aux)->aux = NULL;
2613 bb->aux = NULL;
2616 /* Zero out AUX fields of newly created block during EH edge
2617 insertion. */
2618 for (; last < last_basic_block_for_fn (cfun); last++)
2620 if (need_debug_cleanup)
2621 maybe_move_debug_stmts_to_successors (id,
2622 BASIC_BLOCK_FOR_FN (cfun, last));
2623 BASIC_BLOCK_FOR_FN (cfun, last)->aux = NULL;
2624 /* Update call edge destinations. This can not be done before loop
2625 info is updated, because we may split basic blocks. */
2626 if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
2627 redirect_all_calls (id, BASIC_BLOCK_FOR_FN (cfun, last));
2629 entry_block_map->aux = NULL;
2630 exit_block_map->aux = NULL;
2632 if (id->eh_map)
2634 delete id->eh_map;
2635 id->eh_map = NULL;
2638 return new_fndecl;
2641 /* Copy the debug STMT using ID. We deal with these statements in a
2642 special way: if any variable in their VALUE expression wasn't
2643 remapped yet, we won't remap it, because that would get decl uids
2644 out of sync, causing codegen differences between -g and -g0. If
2645 this arises, we drop the VALUE expression altogether. */
2647 static void
2648 copy_debug_stmt (gimple stmt, copy_body_data *id)
2650 tree t, *n;
2651 struct walk_stmt_info wi;
2653 if (gimple_block (stmt))
2655 n = id->decl_map->get (gimple_block (stmt));
2656 gimple_set_block (stmt, n ? *n : id->block);
2659 /* Remap all the operands in COPY. */
2660 memset (&wi, 0, sizeof (wi));
2661 wi.info = id;
2663 processing_debug_stmt = 1;
2665 if (gimple_debug_source_bind_p (stmt))
2666 t = gimple_debug_source_bind_get_var (stmt);
2667 else
2668 t = gimple_debug_bind_get_var (stmt);
2670 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2671 && (n = id->debug_map->get (t)))
2673 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2674 t = *n;
2676 else if (TREE_CODE (t) == VAR_DECL
2677 && !is_global_var (t)
2678 && !id->decl_map->get (t))
2679 /* T is a non-localized variable. */;
2680 else
2681 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2683 if (gimple_debug_bind_p (stmt))
2685 gimple_debug_bind_set_var (stmt, t);
2687 if (gimple_debug_bind_has_value_p (stmt))
2688 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2689 remap_gimple_op_r, &wi, NULL);
2691 /* Punt if any decl couldn't be remapped. */
2692 if (processing_debug_stmt < 0)
2693 gimple_debug_bind_reset_value (stmt);
2695 else if (gimple_debug_source_bind_p (stmt))
2697 gimple_debug_source_bind_set_var (stmt, t);
2698 walk_tree (gimple_debug_source_bind_get_value_ptr (stmt),
2699 remap_gimple_op_r, &wi, NULL);
2700 /* When inlining and source bind refers to one of the optimized
2701 away parameters, change the source bind into normal debug bind
2702 referring to the corresponding DEBUG_EXPR_DECL that should have
2703 been bound before the call stmt. */
2704 t = gimple_debug_source_bind_get_value (stmt);
2705 if (t != NULL_TREE
2706 && TREE_CODE (t) == PARM_DECL
2707 && id->gimple_call)
2709 vec<tree, va_gc> **debug_args = decl_debug_args_lookup (id->src_fn);
2710 unsigned int i;
2711 if (debug_args != NULL)
2713 for (i = 0; i < vec_safe_length (*debug_args); i += 2)
2714 if ((**debug_args)[i] == DECL_ORIGIN (t)
2715 && TREE_CODE ((**debug_args)[i + 1]) == DEBUG_EXPR_DECL)
2717 t = (**debug_args)[i + 1];
2718 stmt->subcode = GIMPLE_DEBUG_BIND;
2719 gimple_debug_bind_set_value (stmt, t);
2720 break;
2726 processing_debug_stmt = 0;
2728 update_stmt (stmt);
2731 /* Process deferred debug stmts. In order to give values better odds
2732 of being successfully remapped, we delay the processing of debug
2733 stmts until all other stmts that might require remapping are
2734 processed. */
2736 static void
2737 copy_debug_stmts (copy_body_data *id)
2739 size_t i;
2740 gimple stmt;
2742 if (!id->debug_stmts.exists ())
2743 return;
2745 FOR_EACH_VEC_ELT (id->debug_stmts, i, stmt)
2746 copy_debug_stmt (stmt, id);
2748 id->debug_stmts.release ();
2751 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2752 another function. */
2754 static tree
2755 copy_tree_body (copy_body_data *id)
2757 tree fndecl = id->src_fn;
2758 tree body = DECL_SAVED_TREE (fndecl);
2760 walk_tree (&body, copy_tree_body_r, id, NULL);
2762 return body;
2765 /* Make a copy of the body of FN so that it can be inserted inline in
2766 another function. */
2768 static tree
2769 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
2770 basic_block entry_block_map, basic_block exit_block_map,
2771 basic_block new_entry)
2773 tree fndecl = id->src_fn;
2774 tree body;
2776 /* If this body has a CFG, walk CFG and copy. */
2777 gcc_assert (ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (fndecl)));
2778 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map,
2779 new_entry);
2780 copy_debug_stmts (id);
2782 return body;
2785 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2786 defined in function FN, or of a data member thereof. */
2788 static bool
2789 self_inlining_addr_expr (tree value, tree fn)
2791 tree var;
2793 if (TREE_CODE (value) != ADDR_EXPR)
2794 return false;
2796 var = get_base_address (TREE_OPERAND (value, 0));
2798 return var && auto_var_in_fn_p (var, fn);
2801 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2802 lexical block and line number information from base_stmt, if given,
2803 or from the last stmt of the block otherwise. */
2805 static gimple
2806 insert_init_debug_bind (copy_body_data *id,
2807 basic_block bb, tree var, tree value,
2808 gimple base_stmt)
2810 gimple note;
2811 gimple_stmt_iterator gsi;
2812 tree tracked_var;
2814 if (!gimple_in_ssa_p (id->src_cfun))
2815 return NULL;
2817 if (!MAY_HAVE_DEBUG_STMTS)
2818 return NULL;
2820 tracked_var = target_for_debug_bind (var);
2821 if (!tracked_var)
2822 return NULL;
2824 if (bb)
2826 gsi = gsi_last_bb (bb);
2827 if (!base_stmt && !gsi_end_p (gsi))
2828 base_stmt = gsi_stmt (gsi);
2831 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
2833 if (bb)
2835 if (!gsi_end_p (gsi))
2836 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
2837 else
2838 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
2841 return note;
2844 static void
2845 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
2847 /* If VAR represents a zero-sized variable, it's possible that the
2848 assignment statement may result in no gimple statements. */
2849 if (init_stmt)
2851 gimple_stmt_iterator si = gsi_last_bb (bb);
2853 /* We can end up with init statements that store to a non-register
2854 from a rhs with a conversion. Handle that here by forcing the
2855 rhs into a temporary. gimple_regimplify_operands is not
2856 prepared to do this for us. */
2857 if (!is_gimple_debug (init_stmt)
2858 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
2859 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2860 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2862 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2863 gimple_expr_type (init_stmt),
2864 gimple_assign_rhs1 (init_stmt));
2865 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
2866 GSI_NEW_STMT);
2867 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
2868 gimple_assign_set_rhs1 (init_stmt, rhs);
2870 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2871 gimple_regimplify_operands (init_stmt, &si);
2873 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2875 tree def = gimple_assign_lhs (init_stmt);
2876 insert_init_debug_bind (id, bb, def, def, init_stmt);
2881 /* Initialize parameter P with VALUE. If needed, produce init statement
2882 at the end of BB. When BB is NULL, we return init statement to be
2883 output later. */
2884 static gimple
2885 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2886 basic_block bb, tree *vars)
2888 gimple init_stmt = NULL;
2889 tree var;
2890 tree rhs = value;
2891 tree def = (gimple_in_ssa_p (cfun)
2892 ? ssa_default_def (id->src_cfun, p) : NULL);
2894 if (value
2895 && value != error_mark_node
2896 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
2898 /* If we can match up types by promotion/demotion do so. */
2899 if (fold_convertible_p (TREE_TYPE (p), value))
2900 rhs = fold_convert (TREE_TYPE (p), value);
2901 else
2903 /* ??? For valid programs we should not end up here.
2904 Still if we end up with truly mismatched types here, fall back
2905 to using a VIEW_CONVERT_EXPR or a literal zero to not leak invalid
2906 GIMPLE to the following passes. */
2907 if (!is_gimple_reg_type (TREE_TYPE (value))
2908 || TYPE_SIZE (TREE_TYPE (p)) == TYPE_SIZE (TREE_TYPE (value)))
2909 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2910 else
2911 rhs = build_zero_cst (TREE_TYPE (p));
2915 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2916 here since the type of this decl must be visible to the calling
2917 function. */
2918 var = copy_decl_to_var (p, id);
2920 /* Declare this new variable. */
2921 DECL_CHAIN (var) = *vars;
2922 *vars = var;
2924 /* Make gimplifier happy about this variable. */
2925 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2927 /* If the parameter is never assigned to, has no SSA_NAMEs created,
2928 we would not need to create a new variable here at all, if it
2929 weren't for debug info. Still, we can just use the argument
2930 value. */
2931 if (TREE_READONLY (p)
2932 && !TREE_ADDRESSABLE (p)
2933 && value && !TREE_SIDE_EFFECTS (value)
2934 && !def)
2936 /* We may produce non-gimple trees by adding NOPs or introduce
2937 invalid sharing when operand is not really constant.
2938 It is not big deal to prohibit constant propagation here as
2939 we will constant propagate in DOM1 pass anyway. */
2940 if (is_gimple_min_invariant (value)
2941 && useless_type_conversion_p (TREE_TYPE (p),
2942 TREE_TYPE (value))
2943 /* We have to be very careful about ADDR_EXPR. Make sure
2944 the base variable isn't a local variable of the inlined
2945 function, e.g., when doing recursive inlining, direct or
2946 mutually-recursive or whatever, which is why we don't
2947 just test whether fn == current_function_decl. */
2948 && ! self_inlining_addr_expr (value, fn))
2950 insert_decl_map (id, p, value);
2951 insert_debug_decl_map (id, p, var);
2952 return insert_init_debug_bind (id, bb, var, value, NULL);
2956 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2957 that way, when the PARM_DECL is encountered, it will be
2958 automatically replaced by the VAR_DECL. */
2959 insert_decl_map (id, p, var);
2961 /* Even if P was TREE_READONLY, the new VAR should not be.
2962 In the original code, we would have constructed a
2963 temporary, and then the function body would have never
2964 changed the value of P. However, now, we will be
2965 constructing VAR directly. The constructor body may
2966 change its value multiple times as it is being
2967 constructed. Therefore, it must not be TREE_READONLY;
2968 the back-end assumes that TREE_READONLY variable is
2969 assigned to only once. */
2970 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2971 TREE_READONLY (var) = 0;
2973 /* If there is no setup required and we are in SSA, take the easy route
2974 replacing all SSA names representing the function parameter by the
2975 SSA name passed to function.
2977 We need to construct map for the variable anyway as it might be used
2978 in different SSA names when parameter is set in function.
2980 Do replacement at -O0 for const arguments replaced by constant.
2981 This is important for builtin_constant_p and other construct requiring
2982 constant argument to be visible in inlined function body. */
2983 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2984 && (optimize
2985 || (TREE_READONLY (p)
2986 && is_gimple_min_invariant (rhs)))
2987 && (TREE_CODE (rhs) == SSA_NAME
2988 || is_gimple_min_invariant (rhs))
2989 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2991 insert_decl_map (id, def, rhs);
2992 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2995 /* If the value of argument is never used, don't care about initializing
2996 it. */
2997 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2999 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
3000 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3003 /* Initialize this VAR_DECL from the equivalent argument. Convert
3004 the argument to the proper type in case it was promoted. */
3005 if (value)
3007 if (rhs == error_mark_node)
3009 insert_decl_map (id, p, var);
3010 return insert_init_debug_bind (id, bb, var, rhs, NULL);
3013 STRIP_USELESS_TYPE_CONVERSION (rhs);
3015 /* If we are in SSA form properly remap the default definition
3016 or assign to a dummy SSA name if the parameter is unused and
3017 we are not optimizing. */
3018 if (gimple_in_ssa_p (cfun) && is_gimple_reg (p))
3020 if (def)
3022 def = remap_ssa_name (def, id);
3023 init_stmt = gimple_build_assign (def, rhs);
3024 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
3025 set_ssa_default_def (cfun, var, NULL);
3027 else if (!optimize)
3029 def = make_ssa_name (var, NULL);
3030 init_stmt = gimple_build_assign (def, rhs);
3033 else
3034 init_stmt = gimple_build_assign (var, rhs);
3036 if (bb && init_stmt)
3037 insert_init_stmt (id, bb, init_stmt);
3039 return init_stmt;
3042 /* Generate code to initialize the parameters of the function at the
3043 top of the stack in ID from the GIMPLE_CALL STMT. */
3045 static void
3046 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
3047 tree fn, basic_block bb)
3049 tree parms;
3050 size_t i;
3051 tree p;
3052 tree vars = NULL_TREE;
3053 tree static_chain = gimple_call_chain (stmt);
3055 /* Figure out what the parameters are. */
3056 parms = DECL_ARGUMENTS (fn);
3058 /* Loop through the parameter declarations, replacing each with an
3059 equivalent VAR_DECL, appropriately initialized. */
3060 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
3062 tree val;
3063 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
3064 setup_one_parameter (id, p, val, fn, bb, &vars);
3066 /* After remapping parameters remap their types. This has to be done
3067 in a second loop over all parameters to appropriately remap
3068 variable sized arrays when the size is specified in a
3069 parameter following the array. */
3070 for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
3072 tree *varp = id->decl_map->get (p);
3073 if (varp
3074 && TREE_CODE (*varp) == VAR_DECL)
3076 tree def = (gimple_in_ssa_p (cfun) && is_gimple_reg (p)
3077 ? ssa_default_def (id->src_cfun, p) : NULL);
3078 tree var = *varp;
3079 TREE_TYPE (var) = remap_type (TREE_TYPE (var), id);
3080 /* Also remap the default definition if it was remapped
3081 to the default definition of the parameter replacement
3082 by the parameter setup. */
3083 if (def)
3085 tree *defp = id->decl_map->get (def);
3086 if (defp
3087 && TREE_CODE (*defp) == SSA_NAME
3088 && SSA_NAME_VAR (*defp) == var)
3089 TREE_TYPE (*defp) = TREE_TYPE (var);
3094 /* Initialize the static chain. */
3095 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
3096 gcc_assert (fn != current_function_decl);
3097 if (p)
3099 /* No static chain? Seems like a bug in tree-nested.c. */
3100 gcc_assert (static_chain);
3102 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
3105 declare_inline_vars (id->block, vars);
3109 /* Declare a return variable to replace the RESULT_DECL for the
3110 function we are calling. An appropriate DECL_STMT is returned.
3111 The USE_STMT is filled to contain a use of the declaration to
3112 indicate the return value of the function.
3114 RETURN_SLOT, if non-null is place where to store the result. It
3115 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
3116 was the LHS of the MODIFY_EXPR to which this call is the RHS.
3118 The return value is a (possibly null) value that holds the result
3119 as seen by the caller. */
3121 static tree
3122 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
3123 basic_block entry_bb)
3125 tree callee = id->src_fn;
3126 tree result = DECL_RESULT (callee);
3127 tree callee_type = TREE_TYPE (result);
3128 tree caller_type;
3129 tree var, use;
3131 /* Handle type-mismatches in the function declaration return type
3132 vs. the call expression. */
3133 if (modify_dest)
3134 caller_type = TREE_TYPE (modify_dest);
3135 else
3136 caller_type = TREE_TYPE (TREE_TYPE (callee));
3138 /* We don't need to do anything for functions that don't return anything. */
3139 if (VOID_TYPE_P (callee_type))
3140 return NULL_TREE;
3142 /* If there was a return slot, then the return value is the
3143 dereferenced address of that object. */
3144 if (return_slot)
3146 /* The front end shouldn't have used both return_slot and
3147 a modify expression. */
3148 gcc_assert (!modify_dest);
3149 if (DECL_BY_REFERENCE (result))
3151 tree return_slot_addr = build_fold_addr_expr (return_slot);
3152 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
3154 /* We are going to construct *&return_slot and we can't do that
3155 for variables believed to be not addressable.
3157 FIXME: This check possibly can match, because values returned
3158 via return slot optimization are not believed to have address
3159 taken by alias analysis. */
3160 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
3161 var = return_slot_addr;
3163 else
3165 var = return_slot;
3166 gcc_assert (TREE_CODE (var) != SSA_NAME);
3167 if (TREE_ADDRESSABLE (result))
3168 mark_addressable (var);
3170 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
3171 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
3172 && !DECL_GIMPLE_REG_P (result)
3173 && DECL_P (var))
3174 DECL_GIMPLE_REG_P (var) = 0;
3175 use = NULL;
3176 goto done;
3179 /* All types requiring non-trivial constructors should have been handled. */
3180 gcc_assert (!TREE_ADDRESSABLE (callee_type));
3182 /* Attempt to avoid creating a new temporary variable. */
3183 if (modify_dest
3184 && TREE_CODE (modify_dest) != SSA_NAME)
3186 bool use_it = false;
3188 /* We can't use MODIFY_DEST if there's type promotion involved. */
3189 if (!useless_type_conversion_p (callee_type, caller_type))
3190 use_it = false;
3192 /* ??? If we're assigning to a variable sized type, then we must
3193 reuse the destination variable, because we've no good way to
3194 create variable sized temporaries at this point. */
3195 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
3196 use_it = true;
3198 /* If the callee cannot possibly modify MODIFY_DEST, then we can
3199 reuse it as the result of the call directly. Don't do this if
3200 it would promote MODIFY_DEST to addressable. */
3201 else if (TREE_ADDRESSABLE (result))
3202 use_it = false;
3203 else
3205 tree base_m = get_base_address (modify_dest);
3207 /* If the base isn't a decl, then it's a pointer, and we don't
3208 know where that's going to go. */
3209 if (!DECL_P (base_m))
3210 use_it = false;
3211 else if (is_global_var (base_m))
3212 use_it = false;
3213 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
3214 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
3215 && !DECL_GIMPLE_REG_P (result)
3216 && DECL_GIMPLE_REG_P (base_m))
3217 use_it = false;
3218 else if (!TREE_ADDRESSABLE (base_m))
3219 use_it = true;
3222 if (use_it)
3224 var = modify_dest;
3225 use = NULL;
3226 goto done;
3230 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
3232 var = copy_result_decl_to_var (result, id);
3233 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
3235 /* Do not have the rest of GCC warn about this variable as it should
3236 not be visible to the user. */
3237 TREE_NO_WARNING (var) = 1;
3239 declare_inline_vars (id->block, var);
3241 /* Build the use expr. If the return type of the function was
3242 promoted, convert it back to the expected type. */
3243 use = var;
3244 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
3246 /* If we can match up types by promotion/demotion do so. */
3247 if (fold_convertible_p (caller_type, var))
3248 use = fold_convert (caller_type, var);
3249 else
3251 /* ??? For valid programs we should not end up here.
3252 Still if we end up with truly mismatched types here, fall back
3253 to using a MEM_REF to not leak invalid GIMPLE to the following
3254 passes. */
3255 /* Prevent var from being written into SSA form. */
3256 if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE
3257 || TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE)
3258 DECL_GIMPLE_REG_P (var) = false;
3259 else if (is_gimple_reg_type (TREE_TYPE (var)))
3260 TREE_ADDRESSABLE (var) = true;
3261 use = fold_build2 (MEM_REF, caller_type,
3262 build_fold_addr_expr (var),
3263 build_int_cst (ptr_type_node, 0));
3267 STRIP_USELESS_TYPE_CONVERSION (use);
3269 if (DECL_BY_REFERENCE (result))
3271 TREE_ADDRESSABLE (var) = 1;
3272 var = build_fold_addr_expr (var);
3275 done:
3276 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
3277 way, when the RESULT_DECL is encountered, it will be
3278 automatically replaced by the VAR_DECL.
3280 When returning by reference, ensure that RESULT_DECL remaps to
3281 gimple_val. */
3282 if (DECL_BY_REFERENCE (result)
3283 && !is_gimple_val (var))
3285 tree temp = create_tmp_var (TREE_TYPE (result), "retvalptr");
3286 insert_decl_map (id, result, temp);
3287 /* When RESULT_DECL is in SSA form, we need to remap and initialize
3288 it's default_def SSA_NAME. */
3289 if (gimple_in_ssa_p (id->src_cfun)
3290 && is_gimple_reg (result))
3292 temp = make_ssa_name (temp, NULL);
3293 insert_decl_map (id, ssa_default_def (id->src_cfun, result), temp);
3295 insert_init_stmt (id, entry_bb, gimple_build_assign (temp, var));
3297 else
3298 insert_decl_map (id, result, var);
3300 /* Remember this so we can ignore it in remap_decls. */
3301 id->retvar = var;
3303 return use;
3306 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
3307 to a local label. */
3309 static tree
3310 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
3312 tree node = *nodep;
3313 tree fn = (tree) fnp;
3315 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
3316 return node;
3318 if (TYPE_P (node))
3319 *walk_subtrees = 0;
3321 return NULL_TREE;
3324 /* Determine if the function can be copied. If so return NULL. If
3325 not return a string describng the reason for failure. */
3327 static const char *
3328 copy_forbidden (struct function *fun, tree fndecl)
3330 const char *reason = fun->cannot_be_copied_reason;
3331 tree decl;
3332 unsigned ix;
3334 /* Only examine the function once. */
3335 if (fun->cannot_be_copied_set)
3336 return reason;
3338 /* We cannot copy a function that receives a non-local goto
3339 because we cannot remap the destination label used in the
3340 function that is performing the non-local goto. */
3341 /* ??? Actually, this should be possible, if we work at it.
3342 No doubt there's just a handful of places that simply
3343 assume it doesn't happen and don't substitute properly. */
3344 if (fun->has_nonlocal_label)
3346 reason = G_("function %q+F can never be copied "
3347 "because it receives a non-local goto");
3348 goto fail;
3351 FOR_EACH_LOCAL_DECL (fun, ix, decl)
3352 if (TREE_CODE (decl) == VAR_DECL
3353 && TREE_STATIC (decl)
3354 && !DECL_EXTERNAL (decl)
3355 && DECL_INITIAL (decl)
3356 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
3357 has_label_address_in_static_1,
3358 fndecl))
3360 reason = G_("function %q+F can never be copied because it saves "
3361 "address of local label in a static variable");
3362 goto fail;
3365 fail:
3366 fun->cannot_be_copied_reason = reason;
3367 fun->cannot_be_copied_set = true;
3368 return reason;
3372 static const char *inline_forbidden_reason;
3374 /* A callback for walk_gimple_seq to handle statements. Returns non-null
3375 iff a function can not be inlined. Also sets the reason why. */
3377 static tree
3378 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
3379 struct walk_stmt_info *wip)
3381 tree fn = (tree) wip->info;
3382 tree t;
3383 gimple stmt = gsi_stmt (*gsi);
3385 switch (gimple_code (stmt))
3387 case GIMPLE_CALL:
3388 /* Refuse to inline alloca call unless user explicitly forced so as
3389 this may change program's memory overhead drastically when the
3390 function using alloca is called in loop. In GCC present in
3391 SPEC2000 inlining into schedule_block cause it to require 2GB of
3392 RAM instead of 256MB. Don't do so for alloca calls emitted for
3393 VLA objects as those can't cause unbounded growth (they're always
3394 wrapped inside stack_save/stack_restore regions. */
3395 if (gimple_alloca_call_p (stmt)
3396 && !gimple_call_alloca_for_var_p (stmt)
3397 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
3399 inline_forbidden_reason
3400 = G_("function %q+F can never be inlined because it uses "
3401 "alloca (override using the always_inline attribute)");
3402 *handled_ops_p = true;
3403 return fn;
3406 t = gimple_call_fndecl (stmt);
3407 if (t == NULL_TREE)
3408 break;
3410 /* We cannot inline functions that call setjmp. */
3411 if (setjmp_call_p (t))
3413 inline_forbidden_reason
3414 = G_("function %q+F can never be inlined because it uses setjmp");
3415 *handled_ops_p = true;
3416 return t;
3419 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
3420 switch (DECL_FUNCTION_CODE (t))
3422 /* We cannot inline functions that take a variable number of
3423 arguments. */
3424 case BUILT_IN_VA_START:
3425 case BUILT_IN_NEXT_ARG:
3426 case BUILT_IN_VA_END:
3427 inline_forbidden_reason
3428 = G_("function %q+F can never be inlined because it "
3429 "uses variable argument lists");
3430 *handled_ops_p = true;
3431 return t;
3433 case BUILT_IN_LONGJMP:
3434 /* We can't inline functions that call __builtin_longjmp at
3435 all. The non-local goto machinery really requires the
3436 destination be in a different function. If we allow the
3437 function calling __builtin_longjmp to be inlined into the
3438 function calling __builtin_setjmp, Things will Go Awry. */
3439 inline_forbidden_reason
3440 = G_("function %q+F can never be inlined because "
3441 "it uses setjmp-longjmp exception handling");
3442 *handled_ops_p = true;
3443 return t;
3445 case BUILT_IN_NONLOCAL_GOTO:
3446 /* Similarly. */
3447 inline_forbidden_reason
3448 = G_("function %q+F can never be inlined because "
3449 "it uses non-local goto");
3450 *handled_ops_p = true;
3451 return t;
3453 case BUILT_IN_RETURN:
3454 case BUILT_IN_APPLY_ARGS:
3455 /* If a __builtin_apply_args caller would be inlined,
3456 it would be saving arguments of the function it has
3457 been inlined into. Similarly __builtin_return would
3458 return from the function the inline has been inlined into. */
3459 inline_forbidden_reason
3460 = G_("function %q+F can never be inlined because "
3461 "it uses __builtin_return or __builtin_apply_args");
3462 *handled_ops_p = true;
3463 return t;
3465 default:
3466 break;
3468 break;
3470 case GIMPLE_GOTO:
3471 t = gimple_goto_dest (stmt);
3473 /* We will not inline a function which uses computed goto. The
3474 addresses of its local labels, which may be tucked into
3475 global storage, are of course not constant across
3476 instantiations, which causes unexpected behavior. */
3477 if (TREE_CODE (t) != LABEL_DECL)
3479 inline_forbidden_reason
3480 = G_("function %q+F can never be inlined "
3481 "because it contains a computed goto");
3482 *handled_ops_p = true;
3483 return t;
3485 break;
3487 default:
3488 break;
3491 *handled_ops_p = false;
3492 return NULL_TREE;
3495 /* Return true if FNDECL is a function that cannot be inlined into
3496 another one. */
3498 static bool
3499 inline_forbidden_p (tree fndecl)
3501 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
3502 struct walk_stmt_info wi;
3503 basic_block bb;
3504 bool forbidden_p = false;
3506 /* First check for shared reasons not to copy the code. */
3507 inline_forbidden_reason = copy_forbidden (fun, fndecl);
3508 if (inline_forbidden_reason != NULL)
3509 return true;
3511 /* Next, walk the statements of the function looking for
3512 constraucts we can't handle, or are non-optimal for inlining. */
3513 hash_set<tree> visited_nodes;
3514 memset (&wi, 0, sizeof (wi));
3515 wi.info = (void *) fndecl;
3516 wi.pset = &visited_nodes;
3518 FOR_EACH_BB_FN (bb, fun)
3520 gimple ret;
3521 gimple_seq seq = bb_seq (bb);
3522 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
3523 forbidden_p = (ret != NULL);
3524 if (forbidden_p)
3525 break;
3528 return forbidden_p;
3531 /* Return false if the function FNDECL cannot be inlined on account of its
3532 attributes, true otherwise. */
3533 static bool
3534 function_attribute_inlinable_p (const_tree fndecl)
3536 if (targetm.attribute_table)
3538 const_tree a;
3540 for (a = DECL_ATTRIBUTES (fndecl); a; a = TREE_CHAIN (a))
3542 const_tree name = TREE_PURPOSE (a);
3543 int i;
3545 for (i = 0; targetm.attribute_table[i].name != NULL; i++)
3546 if (is_attribute_p (targetm.attribute_table[i].name, name))
3547 return targetm.function_attribute_inlinable_p (fndecl);
3551 return true;
3554 /* Returns nonzero if FN is a function that does not have any
3555 fundamental inline blocking properties. */
3557 bool
3558 tree_inlinable_function_p (tree fn)
3560 bool inlinable = true;
3561 bool do_warning;
3562 tree always_inline;
3564 /* If we've already decided this function shouldn't be inlined,
3565 there's no need to check again. */
3566 if (DECL_UNINLINABLE (fn))
3567 return false;
3569 /* We only warn for functions declared `inline' by the user. */
3570 do_warning = (warn_inline
3571 && DECL_DECLARED_INLINE_P (fn)
3572 && !DECL_NO_INLINE_WARNING_P (fn)
3573 && !DECL_IN_SYSTEM_HEADER (fn));
3575 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
3577 if (flag_no_inline
3578 && always_inline == NULL)
3580 if (do_warning)
3581 warning (OPT_Winline, "function %q+F can never be inlined because it "
3582 "is suppressed using -fno-inline", fn);
3583 inlinable = false;
3586 else if (!function_attribute_inlinable_p (fn))
3588 if (do_warning)
3589 warning (OPT_Winline, "function %q+F can never be inlined because it "
3590 "uses attributes conflicting with inlining", fn);
3591 inlinable = false;
3594 else if (inline_forbidden_p (fn))
3596 /* See if we should warn about uninlinable functions. Previously,
3597 some of these warnings would be issued while trying to expand
3598 the function inline, but that would cause multiple warnings
3599 about functions that would for example call alloca. But since
3600 this a property of the function, just one warning is enough.
3601 As a bonus we can now give more details about the reason why a
3602 function is not inlinable. */
3603 if (always_inline)
3604 error (inline_forbidden_reason, fn);
3605 else if (do_warning)
3606 warning (OPT_Winline, inline_forbidden_reason, fn);
3608 inlinable = false;
3611 /* Squirrel away the result so that we don't have to check again. */
3612 DECL_UNINLINABLE (fn) = !inlinable;
3614 return inlinable;
3617 /* Estimate the cost of a memory move of type TYPE. Use machine dependent
3618 word size and take possible memcpy call into account and return
3619 cost based on whether optimizing for size or speed according to SPEED_P. */
3622 estimate_move_cost (tree type, bool ARG_UNUSED (speed_p))
3624 HOST_WIDE_INT size;
3626 gcc_assert (!VOID_TYPE_P (type));
3628 if (TREE_CODE (type) == VECTOR_TYPE)
3630 enum machine_mode inner = TYPE_MODE (TREE_TYPE (type));
3631 enum machine_mode simd
3632 = targetm.vectorize.preferred_simd_mode (inner);
3633 int simd_mode_size = GET_MODE_SIZE (simd);
3634 return ((GET_MODE_SIZE (TYPE_MODE (type)) + simd_mode_size - 1)
3635 / simd_mode_size);
3638 size = int_size_in_bytes (type);
3640 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (speed_p))
3641 /* Cost of a memcpy call, 3 arguments and the call. */
3642 return 4;
3643 else
3644 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
3647 /* Returns cost of operation CODE, according to WEIGHTS */
3649 static int
3650 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3651 tree op1 ATTRIBUTE_UNUSED, tree op2)
3653 switch (code)
3655 /* These are "free" conversions, or their presumed cost
3656 is folded into other operations. */
3657 case RANGE_EXPR:
3658 CASE_CONVERT:
3659 case COMPLEX_EXPR:
3660 case PAREN_EXPR:
3661 case VIEW_CONVERT_EXPR:
3662 return 0;
3664 /* Assign cost of 1 to usual operations.
3665 ??? We may consider mapping RTL costs to this. */
3666 case COND_EXPR:
3667 case VEC_COND_EXPR:
3668 case VEC_PERM_EXPR:
3670 case PLUS_EXPR:
3671 case POINTER_PLUS_EXPR:
3672 case MINUS_EXPR:
3673 case MULT_EXPR:
3674 case MULT_HIGHPART_EXPR:
3675 case FMA_EXPR:
3677 case ADDR_SPACE_CONVERT_EXPR:
3678 case FIXED_CONVERT_EXPR:
3679 case FIX_TRUNC_EXPR:
3681 case NEGATE_EXPR:
3682 case FLOAT_EXPR:
3683 case MIN_EXPR:
3684 case MAX_EXPR:
3685 case ABS_EXPR:
3687 case LSHIFT_EXPR:
3688 case RSHIFT_EXPR:
3689 case LROTATE_EXPR:
3690 case RROTATE_EXPR:
3691 case VEC_LSHIFT_EXPR:
3692 case VEC_RSHIFT_EXPR:
3694 case BIT_IOR_EXPR:
3695 case BIT_XOR_EXPR:
3696 case BIT_AND_EXPR:
3697 case BIT_NOT_EXPR:
3699 case TRUTH_ANDIF_EXPR:
3700 case TRUTH_ORIF_EXPR:
3701 case TRUTH_AND_EXPR:
3702 case TRUTH_OR_EXPR:
3703 case TRUTH_XOR_EXPR:
3704 case TRUTH_NOT_EXPR:
3706 case LT_EXPR:
3707 case LE_EXPR:
3708 case GT_EXPR:
3709 case GE_EXPR:
3710 case EQ_EXPR:
3711 case NE_EXPR:
3712 case ORDERED_EXPR:
3713 case UNORDERED_EXPR:
3715 case UNLT_EXPR:
3716 case UNLE_EXPR:
3717 case UNGT_EXPR:
3718 case UNGE_EXPR:
3719 case UNEQ_EXPR:
3720 case LTGT_EXPR:
3722 case CONJ_EXPR:
3724 case PREDECREMENT_EXPR:
3725 case PREINCREMENT_EXPR:
3726 case POSTDECREMENT_EXPR:
3727 case POSTINCREMENT_EXPR:
3729 case REALIGN_LOAD_EXPR:
3731 case REDUC_MAX_EXPR:
3732 case REDUC_MIN_EXPR:
3733 case REDUC_PLUS_EXPR:
3734 case WIDEN_SUM_EXPR:
3735 case WIDEN_MULT_EXPR:
3736 case DOT_PROD_EXPR:
3737 case SAD_EXPR:
3738 case WIDEN_MULT_PLUS_EXPR:
3739 case WIDEN_MULT_MINUS_EXPR:
3740 case WIDEN_LSHIFT_EXPR:
3742 case VEC_WIDEN_MULT_HI_EXPR:
3743 case VEC_WIDEN_MULT_LO_EXPR:
3744 case VEC_WIDEN_MULT_EVEN_EXPR:
3745 case VEC_WIDEN_MULT_ODD_EXPR:
3746 case VEC_UNPACK_HI_EXPR:
3747 case VEC_UNPACK_LO_EXPR:
3748 case VEC_UNPACK_FLOAT_HI_EXPR:
3749 case VEC_UNPACK_FLOAT_LO_EXPR:
3750 case VEC_PACK_TRUNC_EXPR:
3751 case VEC_PACK_SAT_EXPR:
3752 case VEC_PACK_FIX_TRUNC_EXPR:
3753 case VEC_WIDEN_LSHIFT_HI_EXPR:
3754 case VEC_WIDEN_LSHIFT_LO_EXPR:
3756 return 1;
3758 /* Few special cases of expensive operations. This is useful
3759 to avoid inlining on functions having too many of these. */
3760 case TRUNC_DIV_EXPR:
3761 case CEIL_DIV_EXPR:
3762 case FLOOR_DIV_EXPR:
3763 case ROUND_DIV_EXPR:
3764 case EXACT_DIV_EXPR:
3765 case TRUNC_MOD_EXPR:
3766 case CEIL_MOD_EXPR:
3767 case FLOOR_MOD_EXPR:
3768 case ROUND_MOD_EXPR:
3769 case RDIV_EXPR:
3770 if (TREE_CODE (op2) != INTEGER_CST)
3771 return weights->div_mod_cost;
3772 return 1;
3774 default:
3775 /* We expect a copy assignment with no operator. */
3776 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
3777 return 0;
3782 /* Estimate number of instructions that will be created by expanding
3783 the statements in the statement sequence STMTS.
3784 WEIGHTS contains weights attributed to various constructs. */
3786 static
3787 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
3789 int cost;
3790 gimple_stmt_iterator gsi;
3792 cost = 0;
3793 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
3794 cost += estimate_num_insns (gsi_stmt (gsi), weights);
3796 return cost;
3800 /* Estimate number of instructions that will be created by expanding STMT.
3801 WEIGHTS contains weights attributed to various constructs. */
3804 estimate_num_insns (gimple stmt, eni_weights *weights)
3806 unsigned cost, i;
3807 enum gimple_code code = gimple_code (stmt);
3808 tree lhs;
3809 tree rhs;
3811 switch (code)
3813 case GIMPLE_ASSIGN:
3814 /* Try to estimate the cost of assignments. We have three cases to
3815 deal with:
3816 1) Simple assignments to registers;
3817 2) Stores to things that must live in memory. This includes
3818 "normal" stores to scalars, but also assignments of large
3819 structures, or constructors of big arrays;
3821 Let us look at the first two cases, assuming we have "a = b + C":
3822 <GIMPLE_ASSIGN <var_decl "a">
3823 <plus_expr <var_decl "b"> <constant C>>
3824 If "a" is a GIMPLE register, the assignment to it is free on almost
3825 any target, because "a" usually ends up in a real register. Hence
3826 the only cost of this expression comes from the PLUS_EXPR, and we
3827 can ignore the GIMPLE_ASSIGN.
3828 If "a" is not a GIMPLE register, the assignment to "a" will most
3829 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
3830 of moving something into "a", which we compute using the function
3831 estimate_move_cost. */
3832 if (gimple_clobber_p (stmt))
3833 return 0; /* ={v} {CLOBBER} stmt expands to nothing. */
3835 lhs = gimple_assign_lhs (stmt);
3836 rhs = gimple_assign_rhs1 (stmt);
3838 cost = 0;
3840 /* Account for the cost of moving to / from memory. */
3841 if (gimple_store_p (stmt))
3842 cost += estimate_move_cost (TREE_TYPE (lhs), weights->time_based);
3843 if (gimple_assign_load_p (stmt))
3844 cost += estimate_move_cost (TREE_TYPE (rhs), weights->time_based);
3846 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3847 gimple_assign_rhs1 (stmt),
3848 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3849 == GIMPLE_BINARY_RHS
3850 ? gimple_assign_rhs2 (stmt) : NULL);
3851 break;
3853 case GIMPLE_COND:
3854 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3855 gimple_op (stmt, 0),
3856 gimple_op (stmt, 1));
3857 break;
3859 case GIMPLE_SWITCH:
3860 /* Take into account cost of the switch + guess 2 conditional jumps for
3861 each case label.
3863 TODO: once the switch expansion logic is sufficiently separated, we can
3864 do better job on estimating cost of the switch. */
3865 if (weights->time_based)
3866 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3867 else
3868 cost = gimple_switch_num_labels (stmt) * 2;
3869 break;
3871 case GIMPLE_CALL:
3873 tree decl;
3875 if (gimple_call_internal_p (stmt))
3876 return 0;
3877 else if ((decl = gimple_call_fndecl (stmt))
3878 && DECL_BUILT_IN (decl))
3880 /* Do not special case builtins where we see the body.
3881 This just confuse inliner. */
3882 struct cgraph_node *node;
3883 if (!(node = cgraph_node::get (decl))
3884 || node->definition)
3886 /* For buitins that are likely expanded to nothing or
3887 inlined do not account operand costs. */
3888 else if (is_simple_builtin (decl))
3889 return 0;
3890 else if (is_inexpensive_builtin (decl))
3891 return weights->target_builtin_call_cost;
3892 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3894 /* We canonicalize x * x to pow (x, 2.0) with -ffast-math, so
3895 specialize the cheap expansion we do here.
3896 ??? This asks for a more general solution. */
3897 switch (DECL_FUNCTION_CODE (decl))
3899 case BUILT_IN_POW:
3900 case BUILT_IN_POWF:
3901 case BUILT_IN_POWL:
3902 if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
3903 && REAL_VALUES_EQUAL
3904 (TREE_REAL_CST (gimple_call_arg (stmt, 1)), dconst2))
3905 return estimate_operator_cost
3906 (MULT_EXPR, weights, gimple_call_arg (stmt, 0),
3907 gimple_call_arg (stmt, 0));
3908 break;
3910 default:
3911 break;
3916 cost = decl ? weights->call_cost : weights->indirect_call_cost;
3917 if (gimple_call_lhs (stmt))
3918 cost += estimate_move_cost (TREE_TYPE (gimple_call_lhs (stmt)),
3919 weights->time_based);
3920 for (i = 0; i < gimple_call_num_args (stmt); i++)
3922 tree arg = gimple_call_arg (stmt, i);
3923 cost += estimate_move_cost (TREE_TYPE (arg),
3924 weights->time_based);
3926 break;
3929 case GIMPLE_RETURN:
3930 return weights->return_cost;
3932 case GIMPLE_GOTO:
3933 case GIMPLE_LABEL:
3934 case GIMPLE_NOP:
3935 case GIMPLE_PHI:
3936 case GIMPLE_PREDICT:
3937 case GIMPLE_DEBUG:
3938 return 0;
3940 case GIMPLE_ASM:
3942 int count = asm_str_count (gimple_asm_string (stmt));
3943 /* 1000 means infinity. This avoids overflows later
3944 with very long asm statements. */
3945 if (count > 1000)
3946 count = 1000;
3947 return count;
3950 case GIMPLE_RESX:
3951 /* This is either going to be an external function call with one
3952 argument, or two register copy statements plus a goto. */
3953 return 2;
3955 case GIMPLE_EH_DISPATCH:
3956 /* ??? This is going to turn into a switch statement. Ideally
3957 we'd have a look at the eh region and estimate the number of
3958 edges involved. */
3959 return 10;
3961 case GIMPLE_BIND:
3962 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
3964 case GIMPLE_EH_FILTER:
3965 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
3967 case GIMPLE_CATCH:
3968 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
3970 case GIMPLE_TRY:
3971 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
3972 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
3974 /* OpenMP directives are generally very expensive. */
3976 case GIMPLE_OMP_RETURN:
3977 case GIMPLE_OMP_SECTIONS_SWITCH:
3978 case GIMPLE_OMP_ATOMIC_STORE:
3979 case GIMPLE_OMP_CONTINUE:
3980 /* ...except these, which are cheap. */
3981 return 0;
3983 case GIMPLE_OMP_ATOMIC_LOAD:
3984 return weights->omp_cost;
3986 case GIMPLE_OMP_FOR:
3987 return (weights->omp_cost
3988 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
3989 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
3991 case GIMPLE_OMP_PARALLEL:
3992 case GIMPLE_OMP_TASK:
3993 case GIMPLE_OMP_CRITICAL:
3994 case GIMPLE_OMP_MASTER:
3995 case GIMPLE_OMP_TASKGROUP:
3996 case GIMPLE_OMP_ORDERED:
3997 case GIMPLE_OMP_SECTION:
3998 case GIMPLE_OMP_SECTIONS:
3999 case GIMPLE_OMP_SINGLE:
4000 case GIMPLE_OMP_TARGET:
4001 case GIMPLE_OMP_TEAMS:
4002 return (weights->omp_cost
4003 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
4005 case GIMPLE_TRANSACTION:
4006 return (weights->tm_cost
4007 + estimate_num_insns_seq (gimple_transaction_body (stmt),
4008 weights));
4010 default:
4011 gcc_unreachable ();
4014 return cost;
4017 /* Estimate number of instructions that will be created by expanding
4018 function FNDECL. WEIGHTS contains weights attributed to various
4019 constructs. */
4022 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
4024 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
4025 gimple_stmt_iterator bsi;
4026 basic_block bb;
4027 int n = 0;
4029 gcc_assert (my_function && my_function->cfg);
4030 FOR_EACH_BB_FN (bb, my_function)
4032 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
4033 n += estimate_num_insns (gsi_stmt (bsi), weights);
4036 return n;
4040 /* Initializes weights used by estimate_num_insns. */
4042 void
4043 init_inline_once (void)
4045 eni_size_weights.call_cost = 1;
4046 eni_size_weights.indirect_call_cost = 3;
4047 eni_size_weights.target_builtin_call_cost = 1;
4048 eni_size_weights.div_mod_cost = 1;
4049 eni_size_weights.omp_cost = 40;
4050 eni_size_weights.tm_cost = 10;
4051 eni_size_weights.time_based = false;
4052 eni_size_weights.return_cost = 1;
4054 /* Estimating time for call is difficult, since we have no idea what the
4055 called function does. In the current uses of eni_time_weights,
4056 underestimating the cost does less harm than overestimating it, so
4057 we choose a rather small value here. */
4058 eni_time_weights.call_cost = 10;
4059 eni_time_weights.indirect_call_cost = 15;
4060 eni_time_weights.target_builtin_call_cost = 1;
4061 eni_time_weights.div_mod_cost = 10;
4062 eni_time_weights.omp_cost = 40;
4063 eni_time_weights.tm_cost = 40;
4064 eni_time_weights.time_based = true;
4065 eni_time_weights.return_cost = 2;
4068 /* Estimate the number of instructions in a gimple_seq. */
4071 count_insns_seq (gimple_seq seq, eni_weights *weights)
4073 gimple_stmt_iterator gsi;
4074 int n = 0;
4075 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
4076 n += estimate_num_insns (gsi_stmt (gsi), weights);
4078 return n;
4082 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
4084 static void
4085 prepend_lexical_block (tree current_block, tree new_block)
4087 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
4088 BLOCK_SUBBLOCKS (current_block) = new_block;
4089 BLOCK_SUPERCONTEXT (new_block) = current_block;
4092 /* Add local variables from CALLEE to CALLER. */
4094 static inline void
4095 add_local_variables (struct function *callee, struct function *caller,
4096 copy_body_data *id)
4098 tree var;
4099 unsigned ix;
4101 FOR_EACH_LOCAL_DECL (callee, ix, var)
4102 if (!can_be_nonlocal (var, id))
4104 tree new_var = remap_decl (var, id);
4106 /* Remap debug-expressions. */
4107 if (TREE_CODE (new_var) == VAR_DECL
4108 && DECL_HAS_DEBUG_EXPR_P (var)
4109 && new_var != var)
4111 tree tem = DECL_DEBUG_EXPR (var);
4112 bool old_regimplify = id->regimplify;
4113 id->remapping_type_depth++;
4114 walk_tree (&tem, copy_tree_body_r, id, NULL);
4115 id->remapping_type_depth--;
4116 id->regimplify = old_regimplify;
4117 SET_DECL_DEBUG_EXPR (new_var, tem);
4118 DECL_HAS_DEBUG_EXPR_P (new_var) = 1;
4120 add_local_decl (caller, new_var);
4124 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
4126 static bool
4127 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
4129 tree use_retvar;
4130 tree fn;
4131 hash_map<tree, tree> *dst;
4132 hash_map<tree, tree> *st = NULL;
4133 tree return_slot;
4134 tree modify_dest;
4135 location_t saved_location;
4136 struct cgraph_edge *cg_edge;
4137 cgraph_inline_failed_t reason;
4138 basic_block return_block;
4139 edge e;
4140 gimple_stmt_iterator gsi, stmt_gsi;
4141 bool successfully_inlined = FALSE;
4142 bool purge_dead_abnormal_edges;
4144 /* Set input_location here so we get the right instantiation context
4145 if we call instantiate_decl from inlinable_function_p. */
4146 /* FIXME: instantiate_decl isn't called by inlinable_function_p. */
4147 saved_location = input_location;
4148 input_location = gimple_location (stmt);
4150 /* From here on, we're only interested in CALL_EXPRs. */
4151 if (gimple_code (stmt) != GIMPLE_CALL)
4152 goto egress;
4154 cg_edge = id->dst_node->get_edge (stmt);
4155 gcc_checking_assert (cg_edge);
4156 /* First, see if we can figure out what function is being called.
4157 If we cannot, then there is no hope of inlining the function. */
4158 if (cg_edge->indirect_unknown_callee)
4159 goto egress;
4160 fn = cg_edge->callee->decl;
4161 gcc_checking_assert (fn);
4163 /* If FN is a declaration of a function in a nested scope that was
4164 globally declared inline, we don't set its DECL_INITIAL.
4165 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
4166 C++ front-end uses it for cdtors to refer to their internal
4167 declarations, that are not real functions. Fortunately those
4168 don't have trees to be saved, so we can tell by checking their
4169 gimple_body. */
4170 if (!DECL_INITIAL (fn)
4171 && DECL_ABSTRACT_ORIGIN (fn)
4172 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
4173 fn = DECL_ABSTRACT_ORIGIN (fn);
4175 /* Don't try to inline functions that are not well-suited to inlining. */
4176 if (cg_edge->inline_failed)
4178 reason = cg_edge->inline_failed;
4179 /* If this call was originally indirect, we do not want to emit any
4180 inlining related warnings or sorry messages because there are no
4181 guarantees regarding those. */
4182 if (cg_edge->indirect_inlining_edge)
4183 goto egress;
4185 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
4186 /* For extern inline functions that get redefined we always
4187 silently ignored always_inline flag. Better behaviour would
4188 be to be able to keep both bodies and use extern inline body
4189 for inlining, but we can't do that because frontends overwrite
4190 the body. */
4191 && !cg_edge->callee->local.redefined_extern_inline
4192 /* During early inline pass, report only when optimization is
4193 not turned on. */
4194 && (symtab->global_info_ready
4195 || !optimize
4196 || cgraph_inline_failed_type (reason) == CIF_FINAL_ERROR)
4197 /* PR 20090218-1_0.c. Body can be provided by another module. */
4198 && (reason != CIF_BODY_NOT_AVAILABLE || !flag_generate_lto))
4200 error ("inlining failed in call to always_inline %q+F: %s", fn,
4201 cgraph_inline_failed_string (reason));
4202 error ("called from here");
4204 else if (warn_inline
4205 && DECL_DECLARED_INLINE_P (fn)
4206 && !DECL_NO_INLINE_WARNING_P (fn)
4207 && !DECL_IN_SYSTEM_HEADER (fn)
4208 && reason != CIF_UNSPECIFIED
4209 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
4210 /* Do not warn about not inlined recursive calls. */
4211 && !cg_edge->recursive_p ()
4212 /* Avoid warnings during early inline pass. */
4213 && symtab->global_info_ready)
4215 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
4216 fn, _(cgraph_inline_failed_string (reason)));
4217 warning (OPT_Winline, "called from here");
4219 goto egress;
4221 fn = cg_edge->callee->decl;
4222 cg_edge->callee->get_body ();
4224 #ifdef ENABLE_CHECKING
4225 if (cg_edge->callee->decl != id->dst_node->decl)
4226 cg_edge->callee->verify ();
4227 #endif
4229 /* We will be inlining this callee. */
4230 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
4232 /* Update the callers EH personality. */
4233 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
4234 DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
4235 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
4237 /* Split the block holding the GIMPLE_CALL. */
4238 e = split_block (bb, stmt);
4239 bb = e->src;
4240 return_block = e->dest;
4241 remove_edge (e);
4243 /* split_block splits after the statement; work around this by
4244 moving the call into the second block manually. Not pretty,
4245 but seems easier than doing the CFG manipulation by hand
4246 when the GIMPLE_CALL is in the last statement of BB. */
4247 stmt_gsi = gsi_last_bb (bb);
4248 gsi_remove (&stmt_gsi, false);
4250 /* If the GIMPLE_CALL was in the last statement of BB, it may have
4251 been the source of abnormal edges. In this case, schedule
4252 the removal of dead abnormal edges. */
4253 gsi = gsi_start_bb (return_block);
4254 if (gsi_end_p (gsi))
4256 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
4257 purge_dead_abnormal_edges = true;
4259 else
4261 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
4262 purge_dead_abnormal_edges = false;
4265 stmt_gsi = gsi_start_bb (return_block);
4267 /* Build a block containing code to initialize the arguments, the
4268 actual inline expansion of the body, and a label for the return
4269 statements within the function to jump to. The type of the
4270 statement expression is the return type of the function call.
4271 ??? If the call does not have an associated block then we will
4272 remap all callee blocks to NULL, effectively dropping most of
4273 its debug information. This should only happen for calls to
4274 artificial decls inserted by the compiler itself. We need to
4275 either link the inlined blocks into the caller block tree or
4276 not refer to them in any way to not break GC for locations. */
4277 if (gimple_block (stmt))
4279 id->block = make_node (BLOCK);
4280 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
4281 BLOCK_SOURCE_LOCATION (id->block) = LOCATION_LOCUS (input_location);
4282 prepend_lexical_block (gimple_block (stmt), id->block);
4285 /* Local declarations will be replaced by their equivalents in this
4286 map. */
4287 st = id->decl_map;
4288 id->decl_map = new hash_map<tree, tree>;
4289 dst = id->debug_map;
4290 id->debug_map = NULL;
4292 /* Record the function we are about to inline. */
4293 id->src_fn = fn;
4294 id->src_node = cg_edge->callee;
4295 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
4296 id->gimple_call = stmt;
4298 gcc_assert (!id->src_cfun->after_inlining);
4300 id->entry_bb = bb;
4301 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
4303 gimple_stmt_iterator si = gsi_last_bb (bb);
4304 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
4305 NOT_TAKEN),
4306 GSI_NEW_STMT);
4308 initialize_inlined_parameters (id, stmt, fn, bb);
4310 if (DECL_INITIAL (fn))
4312 if (gimple_block (stmt))
4314 tree *var;
4316 prepend_lexical_block (id->block,
4317 remap_blocks (DECL_INITIAL (fn), id));
4318 gcc_checking_assert (BLOCK_SUBBLOCKS (id->block)
4319 && (BLOCK_CHAIN (BLOCK_SUBBLOCKS (id->block))
4320 == NULL_TREE));
4321 /* Move vars for PARM_DECLs from DECL_INITIAL block to id->block,
4322 otherwise for DWARF DW_TAG_formal_parameter will not be children of
4323 DW_TAG_inlined_subroutine, but of a DW_TAG_lexical_block
4324 under it. The parameters can be then evaluated in the debugger,
4325 but don't show in backtraces. */
4326 for (var = &BLOCK_VARS (BLOCK_SUBBLOCKS (id->block)); *var; )
4327 if (TREE_CODE (DECL_ORIGIN (*var)) == PARM_DECL)
4329 tree v = *var;
4330 *var = TREE_CHAIN (v);
4331 TREE_CHAIN (v) = BLOCK_VARS (id->block);
4332 BLOCK_VARS (id->block) = v;
4334 else
4335 var = &TREE_CHAIN (*var);
4337 else
4338 remap_blocks_to_null (DECL_INITIAL (fn), id);
4341 /* Return statements in the function body will be replaced by jumps
4342 to the RET_LABEL. */
4343 gcc_assert (DECL_INITIAL (fn));
4344 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
4346 /* Find the LHS to which the result of this call is assigned. */
4347 return_slot = NULL;
4348 if (gimple_call_lhs (stmt))
4350 modify_dest = gimple_call_lhs (stmt);
4352 /* The function which we are inlining might not return a value,
4353 in which case we should issue a warning that the function
4354 does not return a value. In that case the optimizers will
4355 see that the variable to which the value is assigned was not
4356 initialized. We do not want to issue a warning about that
4357 uninitialized variable. */
4358 if (DECL_P (modify_dest))
4359 TREE_NO_WARNING (modify_dest) = 1;
4361 if (gimple_call_return_slot_opt_p (stmt))
4363 return_slot = modify_dest;
4364 modify_dest = NULL;
4367 else
4368 modify_dest = NULL;
4370 /* If we are inlining a call to the C++ operator new, we don't want
4371 to use type based alias analysis on the return value. Otherwise
4372 we may get confused if the compiler sees that the inlined new
4373 function returns a pointer which was just deleted. See bug
4374 33407. */
4375 if (DECL_IS_OPERATOR_NEW (fn))
4377 return_slot = NULL;
4378 modify_dest = NULL;
4381 /* Declare the return variable for the function. */
4382 use_retvar = declare_return_variable (id, return_slot, modify_dest, bb);
4384 /* Add local vars in this inlined callee to caller. */
4385 add_local_variables (id->src_cfun, cfun, id);
4387 if (dump_file && (dump_flags & TDF_DETAILS))
4389 fprintf (dump_file, "Inlining ");
4390 print_generic_expr (dump_file, id->src_fn, 0);
4391 fprintf (dump_file, " to ");
4392 print_generic_expr (dump_file, id->dst_fn, 0);
4393 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
4396 /* This is it. Duplicate the callee body. Assume callee is
4397 pre-gimplified. Note that we must not alter the caller
4398 function in any way before this point, as this CALL_EXPR may be
4399 a self-referential call; if we're calling ourselves, we need to
4400 duplicate our body before altering anything. */
4401 copy_body (id, cg_edge->callee->count,
4402 GCOV_COMPUTE_SCALE (cg_edge->frequency, CGRAPH_FREQ_BASE),
4403 bb, return_block, NULL);
4405 /* Reset the escaped solution. */
4406 if (cfun->gimple_df)
4407 pt_solution_reset (&cfun->gimple_df->escaped);
4409 /* Clean up. */
4410 if (id->debug_map)
4412 delete id->debug_map;
4413 id->debug_map = dst;
4415 delete id->decl_map;
4416 id->decl_map = st;
4418 /* Unlink the calls virtual operands before replacing it. */
4419 unlink_stmt_vdef (stmt);
4420 if (gimple_vdef (stmt)
4421 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
4422 release_ssa_name (gimple_vdef (stmt));
4424 /* If the inlined function returns a result that we care about,
4425 substitute the GIMPLE_CALL with an assignment of the return
4426 variable to the LHS of the call. That is, if STMT was
4427 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
4428 if (use_retvar && gimple_call_lhs (stmt))
4430 gimple old_stmt = stmt;
4431 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
4432 gsi_replace (&stmt_gsi, stmt, false);
4433 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
4435 else
4437 /* Handle the case of inlining a function with no return
4438 statement, which causes the return value to become undefined. */
4439 if (gimple_call_lhs (stmt)
4440 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
4442 tree name = gimple_call_lhs (stmt);
4443 tree var = SSA_NAME_VAR (name);
4444 tree def = ssa_default_def (cfun, var);
4446 if (def)
4448 /* If the variable is used undefined, make this name
4449 undefined via a move. */
4450 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
4451 gsi_replace (&stmt_gsi, stmt, true);
4453 else
4455 /* Otherwise make this variable undefined. */
4456 gsi_remove (&stmt_gsi, true);
4457 set_ssa_default_def (cfun, var, name);
4458 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
4461 else
4462 gsi_remove (&stmt_gsi, true);
4465 if (purge_dead_abnormal_edges)
4467 gimple_purge_dead_eh_edges (return_block);
4468 gimple_purge_dead_abnormal_call_edges (return_block);
4471 /* If the value of the new expression is ignored, that's OK. We
4472 don't warn about this for CALL_EXPRs, so we shouldn't warn about
4473 the equivalent inlined version either. */
4474 if (is_gimple_assign (stmt))
4476 gcc_assert (gimple_assign_single_p (stmt)
4477 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
4478 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
4481 /* Output the inlining info for this abstract function, since it has been
4482 inlined. If we don't do this now, we can lose the information about the
4483 variables in the function when the blocks get blown away as soon as we
4484 remove the cgraph node. */
4485 if (gimple_block (stmt))
4486 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
4488 /* Update callgraph if needed. */
4489 cg_edge->callee->remove ();
4491 id->block = NULL_TREE;
4492 successfully_inlined = TRUE;
4494 egress:
4495 input_location = saved_location;
4496 return successfully_inlined;
4499 /* Expand call statements reachable from STMT_P.
4500 We can only have CALL_EXPRs as the "toplevel" tree code or nested
4501 in a MODIFY_EXPR. */
4503 static bool
4504 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
4506 gimple_stmt_iterator gsi;
4508 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4510 gimple stmt = gsi_stmt (gsi);
4512 if (is_gimple_call (stmt)
4513 && !gimple_call_internal_p (stmt)
4514 && expand_call_inline (bb, stmt, id))
4515 return true;
4518 return false;
4522 /* Walk all basic blocks created after FIRST and try to fold every statement
4523 in the STATEMENTS pointer set. */
4525 static void
4526 fold_marked_statements (int first, hash_set<gimple> *statements)
4528 for (; first < n_basic_blocks_for_fn (cfun); first++)
4529 if (BASIC_BLOCK_FOR_FN (cfun, first))
4531 gimple_stmt_iterator gsi;
4533 for (gsi = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
4534 !gsi_end_p (gsi);
4535 gsi_next (&gsi))
4536 if (statements->contains (gsi_stmt (gsi)))
4538 gimple old_stmt = gsi_stmt (gsi);
4539 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
4541 if (old_decl && DECL_BUILT_IN (old_decl))
4543 /* Folding builtins can create multiple instructions,
4544 we need to look at all of them. */
4545 gimple_stmt_iterator i2 = gsi;
4546 gsi_prev (&i2);
4547 if (fold_stmt (&gsi))
4549 gimple new_stmt;
4550 /* If a builtin at the end of a bb folded into nothing,
4551 the following loop won't work. */
4552 if (gsi_end_p (gsi))
4554 cgraph_update_edges_for_call_stmt (old_stmt,
4555 old_decl, NULL);
4556 break;
4558 if (gsi_end_p (i2))
4559 i2 = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
4560 else
4561 gsi_next (&i2);
4562 while (1)
4564 new_stmt = gsi_stmt (i2);
4565 update_stmt (new_stmt);
4566 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4567 new_stmt);
4569 if (new_stmt == gsi_stmt (gsi))
4571 /* It is okay to check only for the very last
4572 of these statements. If it is a throwing
4573 statement nothing will change. If it isn't
4574 this can remove EH edges. If that weren't
4575 correct then because some intermediate stmts
4576 throw, but not the last one. That would mean
4577 we'd have to split the block, which we can't
4578 here and we'd loose anyway. And as builtins
4579 probably never throw, this all
4580 is mood anyway. */
4581 if (maybe_clean_or_replace_eh_stmt (old_stmt,
4582 new_stmt))
4583 gimple_purge_dead_eh_edges (
4584 BASIC_BLOCK_FOR_FN (cfun, first));
4585 break;
4587 gsi_next (&i2);
4591 else if (fold_stmt (&gsi))
4593 /* Re-read the statement from GSI as fold_stmt() may
4594 have changed it. */
4595 gimple new_stmt = gsi_stmt (gsi);
4596 update_stmt (new_stmt);
4598 if (is_gimple_call (old_stmt)
4599 || is_gimple_call (new_stmt))
4600 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4601 new_stmt);
4603 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
4604 gimple_purge_dead_eh_edges (BASIC_BLOCK_FOR_FN (cfun,
4605 first));
4611 /* Expand calls to inline functions in the body of FN. */
4613 unsigned int
4614 optimize_inline_calls (tree fn)
4616 copy_body_data id;
4617 basic_block bb;
4618 int last = n_basic_blocks_for_fn (cfun);
4619 bool inlined_p = false;
4621 /* Clear out ID. */
4622 memset (&id, 0, sizeof (id));
4624 id.src_node = id.dst_node = cgraph_node::get (fn);
4625 gcc_assert (id.dst_node->definition);
4626 id.dst_fn = fn;
4627 /* Or any functions that aren't finished yet. */
4628 if (current_function_decl)
4629 id.dst_fn = current_function_decl;
4631 id.copy_decl = copy_decl_maybe_to_var;
4632 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4633 id.transform_new_cfg = false;
4634 id.transform_return_to_modify = true;
4635 id.transform_parameter = true;
4636 id.transform_lang_insert_block = NULL;
4637 id.statements_to_fold = new hash_set<gimple>;
4639 push_gimplify_context ();
4641 /* We make no attempts to keep dominance info up-to-date. */
4642 free_dominance_info (CDI_DOMINATORS);
4643 free_dominance_info (CDI_POST_DOMINATORS);
4645 /* Register specific gimple functions. */
4646 gimple_register_cfg_hooks ();
4648 /* Reach the trees by walking over the CFG, and note the
4649 enclosing basic-blocks in the call edges. */
4650 /* We walk the blocks going forward, because inlined function bodies
4651 will split id->current_basic_block, and the new blocks will
4652 follow it; we'll trudge through them, processing their CALL_EXPRs
4653 along the way. */
4654 FOR_EACH_BB_FN (bb, cfun)
4655 inlined_p |= gimple_expand_calls_inline (bb, &id);
4657 pop_gimplify_context (NULL);
4659 #ifdef ENABLE_CHECKING
4661 struct cgraph_edge *e;
4663 id.dst_node->verify ();
4665 /* Double check that we inlined everything we are supposed to inline. */
4666 for (e = id.dst_node->callees; e; e = e->next_callee)
4667 gcc_assert (e->inline_failed);
4669 #endif
4671 /* Fold queued statements. */
4672 fold_marked_statements (last, id.statements_to_fold);
4673 delete id.statements_to_fold;
4675 gcc_assert (!id.debug_stmts.exists ());
4677 /* If we didn't inline into the function there is nothing to do. */
4678 if (!inlined_p)
4679 return 0;
4681 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4682 number_blocks (fn);
4684 delete_unreachable_blocks_update_callgraph (&id);
4685 #ifdef ENABLE_CHECKING
4686 id.dst_node->verify ();
4687 #endif
4689 /* It would be nice to check SSA/CFG/statement consistency here, but it is
4690 not possible yet - the IPA passes might make various functions to not
4691 throw and they don't care to proactively update local EH info. This is
4692 done later in fixup_cfg pass that also execute the verification. */
4693 return (TODO_update_ssa
4694 | TODO_cleanup_cfg
4695 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
4696 | (gimple_in_ssa_p (cfun) ? TODO_update_address_taken : 0)
4697 | (profile_status_for_fn (cfun) != PROFILE_ABSENT
4698 ? TODO_rebuild_frequencies : 0));
4701 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4703 tree
4704 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4706 enum tree_code code = TREE_CODE (*tp);
4707 enum tree_code_class cl = TREE_CODE_CLASS (code);
4709 /* We make copies of most nodes. */
4710 if (IS_EXPR_CODE_CLASS (cl)
4711 || code == TREE_LIST
4712 || code == TREE_VEC
4713 || code == TYPE_DECL
4714 || code == OMP_CLAUSE)
4716 /* Because the chain gets clobbered when we make a copy, we save it
4717 here. */
4718 tree chain = NULL_TREE, new_tree;
4720 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
4721 chain = TREE_CHAIN (*tp);
4723 /* Copy the node. */
4724 new_tree = copy_node (*tp);
4726 *tp = new_tree;
4728 /* Now, restore the chain, if appropriate. That will cause
4729 walk_tree to walk into the chain as well. */
4730 if (code == PARM_DECL
4731 || code == TREE_LIST
4732 || code == OMP_CLAUSE)
4733 TREE_CHAIN (*tp) = chain;
4735 /* For now, we don't update BLOCKs when we make copies. So, we
4736 have to nullify all BIND_EXPRs. */
4737 if (TREE_CODE (*tp) == BIND_EXPR)
4738 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
4740 else if (code == CONSTRUCTOR)
4742 /* CONSTRUCTOR nodes need special handling because
4743 we need to duplicate the vector of elements. */
4744 tree new_tree;
4746 new_tree = copy_node (*tp);
4747 CONSTRUCTOR_ELTS (new_tree) = vec_safe_copy (CONSTRUCTOR_ELTS (*tp));
4748 *tp = new_tree;
4750 else if (code == STATEMENT_LIST)
4751 /* We used to just abort on STATEMENT_LIST, but we can run into them
4752 with statement-expressions (c++/40975). */
4753 copy_statement_list (tp);
4754 else if (TREE_CODE_CLASS (code) == tcc_type)
4755 *walk_subtrees = 0;
4756 else if (TREE_CODE_CLASS (code) == tcc_declaration)
4757 *walk_subtrees = 0;
4758 else if (TREE_CODE_CLASS (code) == tcc_constant)
4759 *walk_subtrees = 0;
4760 return NULL_TREE;
4763 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
4764 information indicating to what new SAVE_EXPR this one should be mapped,
4765 use that one. Otherwise, create a new node and enter it in ST. FN is
4766 the function into which the copy will be placed. */
4768 static void
4769 remap_save_expr (tree *tp, hash_map<tree, tree> *st, int *walk_subtrees)
4771 tree *n;
4772 tree t;
4774 /* See if we already encountered this SAVE_EXPR. */
4775 n = st->get (*tp);
4777 /* If we didn't already remap this SAVE_EXPR, do so now. */
4778 if (!n)
4780 t = copy_node (*tp);
4782 /* Remember this SAVE_EXPR. */
4783 st->put (*tp, t);
4784 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
4785 st->put (t, t);
4787 else
4789 /* We've already walked into this SAVE_EXPR; don't do it again. */
4790 *walk_subtrees = 0;
4791 t = *n;
4794 /* Replace this SAVE_EXPR with the copy. */
4795 *tp = t;
4798 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
4799 label, copies the declaration and enters it in the splay_tree in DATA (which
4800 is really a 'copy_body_data *'. */
4802 static tree
4803 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
4804 bool *handled_ops_p ATTRIBUTE_UNUSED,
4805 struct walk_stmt_info *wi)
4807 copy_body_data *id = (copy_body_data *) wi->info;
4808 gimple stmt = gsi_stmt (*gsip);
4810 if (gimple_code (stmt) == GIMPLE_LABEL)
4812 tree decl = gimple_label_label (stmt);
4814 /* Copy the decl and remember the copy. */
4815 insert_decl_map (id, decl, id->copy_decl (decl, id));
4818 return NULL_TREE;
4822 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4823 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4824 remaps all local declarations to appropriate replacements in gimple
4825 operands. */
4827 static tree
4828 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
4830 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
4831 copy_body_data *id = (copy_body_data *) wi->info;
4832 hash_map<tree, tree> *st = id->decl_map;
4833 tree *n;
4834 tree expr = *tp;
4836 /* Only a local declaration (variable or label). */
4837 if ((TREE_CODE (expr) == VAR_DECL
4838 && !TREE_STATIC (expr))
4839 || TREE_CODE (expr) == LABEL_DECL)
4841 /* Lookup the declaration. */
4842 n = st->get (expr);
4844 /* If it's there, remap it. */
4845 if (n)
4846 *tp = *n;
4847 *walk_subtrees = 0;
4849 else if (TREE_CODE (expr) == STATEMENT_LIST
4850 || TREE_CODE (expr) == BIND_EXPR
4851 || TREE_CODE (expr) == SAVE_EXPR)
4852 gcc_unreachable ();
4853 else if (TREE_CODE (expr) == TARGET_EXPR)
4855 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4856 It's OK for this to happen if it was part of a subtree that
4857 isn't immediately expanded, such as operand 2 of another
4858 TARGET_EXPR. */
4859 if (!TREE_OPERAND (expr, 1))
4861 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4862 TREE_OPERAND (expr, 3) = NULL_TREE;
4866 /* Keep iterating. */
4867 return NULL_TREE;
4871 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4872 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4873 remaps all local declarations to appropriate replacements in gimple
4874 statements. */
4876 static tree
4877 replace_locals_stmt (gimple_stmt_iterator *gsip,
4878 bool *handled_ops_p ATTRIBUTE_UNUSED,
4879 struct walk_stmt_info *wi)
4881 copy_body_data *id = (copy_body_data *) wi->info;
4882 gimple stmt = gsi_stmt (*gsip);
4884 if (gimple_code (stmt) == GIMPLE_BIND)
4886 tree block = gimple_bind_block (stmt);
4888 if (block)
4890 remap_block (&block, id);
4891 gimple_bind_set_block (stmt, block);
4894 /* This will remap a lot of the same decls again, but this should be
4895 harmless. */
4896 if (gimple_bind_vars (stmt))
4897 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt),
4898 NULL, id));
4901 /* Keep iterating. */
4902 return NULL_TREE;
4906 /* Copies everything in SEQ and replaces variables and labels local to
4907 current_function_decl. */
4909 gimple_seq
4910 copy_gimple_seq_and_replace_locals (gimple_seq seq)
4912 copy_body_data id;
4913 struct walk_stmt_info wi;
4914 gimple_seq copy;
4916 /* There's nothing to do for NULL_TREE. */
4917 if (seq == NULL)
4918 return seq;
4920 /* Set up ID. */
4921 memset (&id, 0, sizeof (id));
4922 id.src_fn = current_function_decl;
4923 id.dst_fn = current_function_decl;
4924 id.decl_map = new hash_map<tree, tree>;
4925 id.debug_map = NULL;
4927 id.copy_decl = copy_decl_no_change;
4928 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4929 id.transform_new_cfg = false;
4930 id.transform_return_to_modify = false;
4931 id.transform_parameter = false;
4932 id.transform_lang_insert_block = NULL;
4934 /* Walk the tree once to find local labels. */
4935 memset (&wi, 0, sizeof (wi));
4936 hash_set<tree> visited;
4937 wi.info = &id;
4938 wi.pset = &visited;
4939 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
4941 copy = gimple_seq_copy (seq);
4943 /* Walk the copy, remapping decls. */
4944 memset (&wi, 0, sizeof (wi));
4945 wi.info = &id;
4946 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4948 /* Clean up. */
4949 delete id.decl_map;
4950 if (id.debug_map)
4951 delete id.debug_map;
4953 return copy;
4957 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
4959 static tree
4960 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
4962 if (*tp == data)
4963 return (tree) data;
4964 else
4965 return NULL;
4968 DEBUG_FUNCTION bool
4969 debug_find_tree (tree top, tree search)
4971 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4975 /* Declare the variables created by the inliner. Add all the variables in
4976 VARS to BIND_EXPR. */
4978 static void
4979 declare_inline_vars (tree block, tree vars)
4981 tree t;
4982 for (t = vars; t; t = DECL_CHAIN (t))
4984 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4985 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
4986 add_local_decl (cfun, t);
4989 if (block)
4990 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4993 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
4994 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
4995 VAR_DECL translation. */
4997 static tree
4998 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
5000 /* Don't generate debug information for the copy if we wouldn't have
5001 generated it for the copy either. */
5002 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
5003 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
5005 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
5006 declaration inspired this copy. */
5007 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
5009 /* The new variable/label has no RTL, yet. */
5010 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
5011 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
5012 SET_DECL_RTL (copy, 0);
5014 /* These args would always appear unused, if not for this. */
5015 TREE_USED (copy) = 1;
5017 /* Set the context for the new declaration. */
5018 if (!DECL_CONTEXT (decl))
5019 /* Globals stay global. */
5021 else if (DECL_CONTEXT (decl) != id->src_fn)
5022 /* Things that weren't in the scope of the function we're inlining
5023 from aren't in the scope we're inlining to, either. */
5025 else if (TREE_STATIC (decl))
5026 /* Function-scoped static variables should stay in the original
5027 function. */
5029 else
5030 /* Ordinary automatic local variables are now in the scope of the
5031 new function. */
5032 DECL_CONTEXT (copy) = id->dst_fn;
5034 return copy;
5037 static tree
5038 copy_decl_to_var (tree decl, copy_body_data *id)
5040 tree copy, type;
5042 gcc_assert (TREE_CODE (decl) == PARM_DECL
5043 || TREE_CODE (decl) == RESULT_DECL);
5045 type = TREE_TYPE (decl);
5047 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
5048 VAR_DECL, DECL_NAME (decl), type);
5049 if (DECL_PT_UID_SET_P (decl))
5050 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
5051 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
5052 TREE_READONLY (copy) = TREE_READONLY (decl);
5053 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
5054 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
5056 return copy_decl_for_dup_finish (id, decl, copy);
5059 /* Like copy_decl_to_var, but create a return slot object instead of a
5060 pointer variable for return by invisible reference. */
5062 static tree
5063 copy_result_decl_to_var (tree decl, copy_body_data *id)
5065 tree copy, type;
5067 gcc_assert (TREE_CODE (decl) == PARM_DECL
5068 || TREE_CODE (decl) == RESULT_DECL);
5070 type = TREE_TYPE (decl);
5071 if (DECL_BY_REFERENCE (decl))
5072 type = TREE_TYPE (type);
5074 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
5075 VAR_DECL, DECL_NAME (decl), type);
5076 if (DECL_PT_UID_SET_P (decl))
5077 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
5078 TREE_READONLY (copy) = TREE_READONLY (decl);
5079 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
5080 if (!DECL_BY_REFERENCE (decl))
5082 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
5083 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
5086 return copy_decl_for_dup_finish (id, decl, copy);
5089 tree
5090 copy_decl_no_change (tree decl, copy_body_data *id)
5092 tree copy;
5094 copy = copy_node (decl);
5096 /* The COPY is not abstract; it will be generated in DST_FN. */
5097 DECL_ABSTRACT_P (copy) = false;
5098 lang_hooks.dup_lang_specific_decl (copy);
5100 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
5101 been taken; it's for internal bookkeeping in expand_goto_internal. */
5102 if (TREE_CODE (copy) == LABEL_DECL)
5104 TREE_ADDRESSABLE (copy) = 0;
5105 LABEL_DECL_UID (copy) = -1;
5108 return copy_decl_for_dup_finish (id, decl, copy);
5111 static tree
5112 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
5114 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
5115 return copy_decl_to_var (decl, id);
5116 else
5117 return copy_decl_no_change (decl, id);
5120 /* Return a copy of the function's argument tree. */
5121 static tree
5122 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
5123 bitmap args_to_skip, tree *vars)
5125 tree arg, *parg;
5126 tree new_parm = NULL;
5127 int i = 0;
5129 parg = &new_parm;
5131 for (arg = orig_parm; arg; arg = DECL_CHAIN (arg), i++)
5132 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
5134 tree new_tree = remap_decl (arg, id);
5135 if (TREE_CODE (new_tree) != PARM_DECL)
5136 new_tree = id->copy_decl (arg, id);
5137 lang_hooks.dup_lang_specific_decl (new_tree);
5138 *parg = new_tree;
5139 parg = &DECL_CHAIN (new_tree);
5141 else if (!id->decl_map->get (arg))
5143 /* Make an equivalent VAR_DECL. If the argument was used
5144 as temporary variable later in function, the uses will be
5145 replaced by local variable. */
5146 tree var = copy_decl_to_var (arg, id);
5147 insert_decl_map (id, arg, var);
5148 /* Declare this new variable. */
5149 DECL_CHAIN (var) = *vars;
5150 *vars = var;
5152 return new_parm;
5155 /* Return a copy of the function's static chain. */
5156 static tree
5157 copy_static_chain (tree static_chain, copy_body_data * id)
5159 tree *chain_copy, *pvar;
5161 chain_copy = &static_chain;
5162 for (pvar = chain_copy; *pvar; pvar = &DECL_CHAIN (*pvar))
5164 tree new_tree = remap_decl (*pvar, id);
5165 lang_hooks.dup_lang_specific_decl (new_tree);
5166 DECL_CHAIN (new_tree) = DECL_CHAIN (*pvar);
5167 *pvar = new_tree;
5169 return static_chain;
5172 /* Return true if the function is allowed to be versioned.
5173 This is a guard for the versioning functionality. */
5175 bool
5176 tree_versionable_function_p (tree fndecl)
5178 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
5179 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
5182 /* Delete all unreachable basic blocks and update callgraph.
5183 Doing so is somewhat nontrivial because we need to update all clones and
5184 remove inline function that become unreachable. */
5186 static bool
5187 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
5189 bool changed = false;
5190 basic_block b, next_bb;
5192 find_unreachable_blocks ();
5194 /* Delete all unreachable basic blocks. */
5196 for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
5197 != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
5199 next_bb = b->next_bb;
5201 if (!(b->flags & BB_REACHABLE))
5203 gimple_stmt_iterator bsi;
5205 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
5207 struct cgraph_edge *e;
5208 struct cgraph_node *node;
5210 id->dst_node->remove_stmt_references (gsi_stmt (bsi));
5212 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
5213 &&(e = id->dst_node->get_edge (gsi_stmt (bsi))) != NULL)
5215 if (!e->inline_failed)
5216 e->callee->remove_symbol_and_inline_clones (id->dst_node);
5217 else
5218 e->remove ();
5220 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
5221 && id->dst_node->clones)
5222 for (node = id->dst_node->clones; node != id->dst_node;)
5224 node->remove_stmt_references (gsi_stmt (bsi));
5225 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
5226 && (e = node->get_edge (gsi_stmt (bsi))) != NULL)
5228 if (!e->inline_failed)
5229 e->callee->remove_symbol_and_inline_clones (id->dst_node);
5230 else
5231 e->remove ();
5234 if (node->clones)
5235 node = node->clones;
5236 else if (node->next_sibling_clone)
5237 node = node->next_sibling_clone;
5238 else
5240 while (node != id->dst_node && !node->next_sibling_clone)
5241 node = node->clone_of;
5242 if (node != id->dst_node)
5243 node = node->next_sibling_clone;
5247 delete_basic_block (b);
5248 changed = true;
5252 return changed;
5255 /* Update clone info after duplication. */
5257 static void
5258 update_clone_info (copy_body_data * id)
5260 struct cgraph_node *node;
5261 if (!id->dst_node->clones)
5262 return;
5263 for (node = id->dst_node->clones; node != id->dst_node;)
5265 /* First update replace maps to match the new body. */
5266 if (node->clone.tree_map)
5268 unsigned int i;
5269 for (i = 0; i < vec_safe_length (node->clone.tree_map); i++)
5271 struct ipa_replace_map *replace_info;
5272 replace_info = (*node->clone.tree_map)[i];
5273 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
5274 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
5277 if (node->clones)
5278 node = node->clones;
5279 else if (node->next_sibling_clone)
5280 node = node->next_sibling_clone;
5281 else
5283 while (node != id->dst_node && !node->next_sibling_clone)
5284 node = node->clone_of;
5285 if (node != id->dst_node)
5286 node = node->next_sibling_clone;
5291 /* Create a copy of a function's tree.
5292 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
5293 of the original function and the new copied function
5294 respectively. In case we want to replace a DECL
5295 tree with another tree while duplicating the function's
5296 body, TREE_MAP represents the mapping between these
5297 trees. If UPDATE_CLONES is set, the call_stmt fields
5298 of edges of clones of the function will be updated.
5300 If non-NULL ARGS_TO_SKIP determine function parameters to remove
5301 from new version.
5302 If SKIP_RETURN is true, the new version will return void.
5303 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
5304 If non_NULL NEW_ENTRY determine new entry BB of the clone.
5306 void
5307 tree_function_versioning (tree old_decl, tree new_decl,
5308 vec<ipa_replace_map *, va_gc> *tree_map,
5309 bool update_clones, bitmap args_to_skip,
5310 bool skip_return, bitmap blocks_to_copy,
5311 basic_block new_entry)
5313 struct cgraph_node *old_version_node;
5314 struct cgraph_node *new_version_node;
5315 copy_body_data id;
5316 tree p;
5317 unsigned i;
5318 struct ipa_replace_map *replace_info;
5319 basic_block old_entry_block, bb;
5320 auto_vec<gimple, 10> init_stmts;
5321 tree vars = NULL_TREE;
5323 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
5324 && TREE_CODE (new_decl) == FUNCTION_DECL);
5325 DECL_POSSIBLY_INLINED (old_decl) = 1;
5327 old_version_node = cgraph_node::get (old_decl);
5328 gcc_checking_assert (old_version_node);
5329 new_version_node = cgraph_node::get (new_decl);
5330 gcc_checking_assert (new_version_node);
5332 /* Copy over debug args. */
5333 if (DECL_HAS_DEBUG_ARGS_P (old_decl))
5335 vec<tree, va_gc> **new_debug_args, **old_debug_args;
5336 gcc_checking_assert (decl_debug_args_lookup (new_decl) == NULL);
5337 DECL_HAS_DEBUG_ARGS_P (new_decl) = 0;
5338 old_debug_args = decl_debug_args_lookup (old_decl);
5339 if (old_debug_args)
5341 new_debug_args = decl_debug_args_insert (new_decl);
5342 *new_debug_args = vec_safe_copy (*old_debug_args);
5346 /* Output the inlining info for this abstract function, since it has been
5347 inlined. If we don't do this now, we can lose the information about the
5348 variables in the function when the blocks get blown away as soon as we
5349 remove the cgraph node. */
5350 (*debug_hooks->outlining_inline_function) (old_decl);
5352 DECL_ARTIFICIAL (new_decl) = 1;
5353 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
5354 if (DECL_ORIGIN (old_decl) == old_decl)
5355 old_version_node->used_as_abstract_origin = true;
5356 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
5358 /* Prepare the data structures for the tree copy. */
5359 memset (&id, 0, sizeof (id));
5361 /* Generate a new name for the new version. */
5362 id.statements_to_fold = new hash_set<gimple>;
5364 id.decl_map = new hash_map<tree, tree>;
5365 id.debug_map = NULL;
5366 id.src_fn = old_decl;
5367 id.dst_fn = new_decl;
5368 id.src_node = old_version_node;
5369 id.dst_node = new_version_node;
5370 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
5371 id.blocks_to_copy = blocks_to_copy;
5373 id.copy_decl = copy_decl_no_change;
5374 id.transform_call_graph_edges
5375 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
5376 id.transform_new_cfg = true;
5377 id.transform_return_to_modify = false;
5378 id.transform_parameter = false;
5379 id.transform_lang_insert_block = NULL;
5381 old_entry_block = ENTRY_BLOCK_PTR_FOR_FN
5382 (DECL_STRUCT_FUNCTION (old_decl));
5383 DECL_RESULT (new_decl) = DECL_RESULT (old_decl);
5384 DECL_ARGUMENTS (new_decl) = DECL_ARGUMENTS (old_decl);
5385 initialize_cfun (new_decl, old_decl,
5386 old_entry_block->count);
5387 if (DECL_STRUCT_FUNCTION (new_decl)->gimple_df)
5388 DECL_STRUCT_FUNCTION (new_decl)->gimple_df->ipa_pta
5389 = id.src_cfun->gimple_df->ipa_pta;
5391 /* Copy the function's static chain. */
5392 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
5393 if (p)
5394 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
5395 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
5396 &id);
5398 /* If there's a tree_map, prepare for substitution. */
5399 if (tree_map)
5400 for (i = 0; i < tree_map->length (); i++)
5402 gimple init;
5403 replace_info = (*tree_map)[i];
5404 if (replace_info->replace_p)
5406 if (!replace_info->old_tree)
5408 int i = replace_info->parm_num;
5409 tree parm;
5410 tree req_type;
5412 for (parm = DECL_ARGUMENTS (old_decl); i; parm = DECL_CHAIN (parm))
5413 i --;
5414 replace_info->old_tree = parm;
5415 req_type = TREE_TYPE (parm);
5416 if (!useless_type_conversion_p (req_type, TREE_TYPE (replace_info->new_tree)))
5418 if (fold_convertible_p (req_type, replace_info->new_tree))
5419 replace_info->new_tree = fold_build1 (NOP_EXPR, req_type, replace_info->new_tree);
5420 else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (replace_info->new_tree)))
5421 replace_info->new_tree = fold_build1 (VIEW_CONVERT_EXPR, req_type, replace_info->new_tree);
5422 else
5424 if (dump_file)
5426 fprintf (dump_file, " const ");
5427 print_generic_expr (dump_file, replace_info->new_tree, 0);
5428 fprintf (dump_file, " can't be converted to param ");
5429 print_generic_expr (dump_file, parm, 0);
5430 fprintf (dump_file, "\n");
5432 replace_info->old_tree = NULL;
5436 else
5437 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
5438 if (replace_info->old_tree)
5440 init = setup_one_parameter (&id, replace_info->old_tree,
5441 replace_info->new_tree, id.src_fn,
5442 NULL,
5443 &vars);
5444 if (init)
5445 init_stmts.safe_push (init);
5449 /* Copy the function's arguments. */
5450 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
5451 DECL_ARGUMENTS (new_decl) =
5452 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
5453 args_to_skip, &vars);
5455 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
5456 BLOCK_SUPERCONTEXT (DECL_INITIAL (new_decl)) = new_decl;
5458 declare_inline_vars (DECL_INITIAL (new_decl), vars);
5460 if (!vec_safe_is_empty (DECL_STRUCT_FUNCTION (old_decl)->local_decls))
5461 /* Add local vars. */
5462 add_local_variables (DECL_STRUCT_FUNCTION (old_decl), cfun, &id);
5464 if (DECL_RESULT (old_decl) == NULL_TREE)
5466 else if (skip_return && !VOID_TYPE_P (TREE_TYPE (DECL_RESULT (old_decl))))
5468 DECL_RESULT (new_decl)
5469 = build_decl (DECL_SOURCE_LOCATION (DECL_RESULT (old_decl)),
5470 RESULT_DECL, NULL_TREE, void_type_node);
5471 DECL_CONTEXT (DECL_RESULT (new_decl)) = new_decl;
5472 cfun->returns_struct = 0;
5473 cfun->returns_pcc_struct = 0;
5475 else
5477 tree old_name;
5478 DECL_RESULT (new_decl) = remap_decl (DECL_RESULT (old_decl), &id);
5479 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
5480 if (gimple_in_ssa_p (id.src_cfun)
5481 && DECL_BY_REFERENCE (DECL_RESULT (old_decl))
5482 && (old_name = ssa_default_def (id.src_cfun, DECL_RESULT (old_decl))))
5484 tree new_name = make_ssa_name (DECL_RESULT (new_decl), NULL);
5485 insert_decl_map (&id, old_name, new_name);
5486 SSA_NAME_DEF_STMT (new_name) = gimple_build_nop ();
5487 set_ssa_default_def (cfun, DECL_RESULT (new_decl), new_name);
5491 /* Set up the destination functions loop tree. */
5492 if (loops_for_fn (DECL_STRUCT_FUNCTION (old_decl)) != NULL)
5494 cfun->curr_properties &= ~PROP_loops;
5495 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5496 cfun->curr_properties |= PROP_loops;
5499 /* Copy the Function's body. */
5500 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
5501 ENTRY_BLOCK_PTR_FOR_FN (cfun), EXIT_BLOCK_PTR_FOR_FN (cfun),
5502 new_entry);
5504 /* Renumber the lexical scoping (non-code) blocks consecutively. */
5505 number_blocks (new_decl);
5507 /* We want to create the BB unconditionally, so that the addition of
5508 debug stmts doesn't affect BB count, which may in the end cause
5509 codegen differences. */
5510 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
5511 while (init_stmts.length ())
5512 insert_init_stmt (&id, bb, init_stmts.pop ());
5513 update_clone_info (&id);
5515 /* Remap the nonlocal_goto_save_area, if any. */
5516 if (cfun->nonlocal_goto_save_area)
5518 struct walk_stmt_info wi;
5520 memset (&wi, 0, sizeof (wi));
5521 wi.info = &id;
5522 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
5525 /* Clean up. */
5526 delete id.decl_map;
5527 if (id.debug_map)
5528 delete id.debug_map;
5529 free_dominance_info (CDI_DOMINATORS);
5530 free_dominance_info (CDI_POST_DOMINATORS);
5532 fold_marked_statements (0, id.statements_to_fold);
5533 delete id.statements_to_fold;
5534 fold_cond_expr_cond ();
5535 delete_unreachable_blocks_update_callgraph (&id);
5536 if (id.dst_node->definition)
5537 cgraph_edge::rebuild_references ();
5538 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
5540 calculate_dominance_info (CDI_DOMINATORS);
5541 fix_loop_structure (NULL);
5543 update_ssa (TODO_update_ssa);
5545 /* After partial cloning we need to rescale frequencies, so they are
5546 within proper range in the cloned function. */
5547 if (new_entry)
5549 struct cgraph_edge *e;
5550 rebuild_frequencies ();
5552 new_version_node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
5553 for (e = new_version_node->callees; e; e = e->next_callee)
5555 basic_block bb = gimple_bb (e->call_stmt);
5556 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5557 bb);
5558 e->count = bb->count;
5560 for (e = new_version_node->indirect_calls; e; e = e->next_callee)
5562 basic_block bb = gimple_bb (e->call_stmt);
5563 e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
5564 bb);
5565 e->count = bb->count;
5569 free_dominance_info (CDI_DOMINATORS);
5570 free_dominance_info (CDI_POST_DOMINATORS);
5572 gcc_assert (!id.debug_stmts.exists ());
5573 pop_cfun ();
5574 return;
5577 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
5578 the callee and return the inlined body on success. */
5580 tree
5581 maybe_inline_call_in_expr (tree exp)
5583 tree fn = get_callee_fndecl (exp);
5585 /* We can only try to inline "const" functions. */
5586 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
5588 call_expr_arg_iterator iter;
5589 copy_body_data id;
5590 tree param, arg, t;
5591 hash_map<tree, tree> decl_map;
5593 /* Remap the parameters. */
5594 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
5595 param;
5596 param = DECL_CHAIN (param), arg = next_call_expr_arg (&iter))
5597 decl_map.put (param, arg);
5599 memset (&id, 0, sizeof (id));
5600 id.src_fn = fn;
5601 id.dst_fn = current_function_decl;
5602 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
5603 id.decl_map = &decl_map;
5605 id.copy_decl = copy_decl_no_change;
5606 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5607 id.transform_new_cfg = false;
5608 id.transform_return_to_modify = true;
5609 id.transform_parameter = true;
5610 id.transform_lang_insert_block = NULL;
5612 /* Make sure not to unshare trees behind the front-end's back
5613 since front-end specific mechanisms may rely on sharing. */
5614 id.regimplify = false;
5615 id.do_not_unshare = true;
5617 /* We're not inside any EH region. */
5618 id.eh_lp_nr = 0;
5620 t = copy_tree_body (&id);
5622 /* We can only return something suitable for use in a GENERIC
5623 expression tree. */
5624 if (TREE_CODE (t) == MODIFY_EXPR)
5625 return TREE_OPERAND (t, 1);
5628 return NULL_TREE;
5631 /* Duplicate a type, fields and all. */
5633 tree
5634 build_duplicate_type (tree type)
5636 struct copy_body_data id;
5638 memset (&id, 0, sizeof (id));
5639 id.src_fn = current_function_decl;
5640 id.dst_fn = current_function_decl;
5641 id.src_cfun = cfun;
5642 id.decl_map = new hash_map<tree, tree>;
5643 id.debug_map = NULL;
5644 id.copy_decl = copy_decl_no_change;
5646 type = remap_type_1 (type, &id);
5648 delete id.decl_map;
5649 if (id.debug_map)
5650 delete id.debug_map;
5652 TYPE_CANONICAL (type) = type;
5654 return type;