Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / tree-inline.c
blob3b1c459128fb192974065b964c551971cf9dabc2
1 /* Tree inlining.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Alexandre Oliva <aoliva@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "flags.h"
30 #include "params.h"
31 #include "input.h"
32 #include "insn-config.h"
33 #include "hashtab.h"
34 #include "langhooks.h"
35 #include "basic-block.h"
36 #include "tree-iterator.h"
37 #include "cgraph.h"
38 #include "intl.h"
39 #include "tree-mudflap.h"
40 #include "tree-flow.h"
41 #include "function.h"
42 #include "tree-flow.h"
43 #include "tree-pretty-print.h"
44 #include "except.h"
45 #include "debug.h"
46 #include "pointer-set.h"
47 #include "ipa-prop.h"
48 #include "value-prof.h"
49 #include "tree-pass.h"
50 #include "target.h"
51 #include "integrate.h"
53 #include "rtl.h" /* FIXME: For asm_str_count. */
55 /* I'm not real happy about this, but we need to handle gimple and
56 non-gimple trees. */
57 #include "gimple.h"
59 /* Inlining, Cloning, Versioning, Parallelization
61 Inlining: a function body is duplicated, but the PARM_DECLs are
62 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
63 MODIFY_EXPRs that store to a dedicated returned-value variable.
64 The duplicated eh_region info of the copy will later be appended
65 to the info for the caller; the eh_region info in copied throwing
66 statements and RESX statements are adjusted accordingly.
68 Cloning: (only in C++) We have one body for a con/de/structor, and
69 multiple function decls, each with a unique parameter list.
70 Duplicate the body, using the given splay tree; some parameters
71 will become constants (like 0 or 1).
73 Versioning: a function body is duplicated and the result is a new
74 function rather than into blocks of an existing function as with
75 inlining. Some parameters will become constants.
77 Parallelization: a region of a function is duplicated resulting in
78 a new function. Variables may be replaced with complex expressions
79 to enable shared variable semantics.
81 All of these will simultaneously lookup any callgraph edges. If
82 we're going to inline the duplicated function body, and the given
83 function has some cloned callgraph nodes (one for each place this
84 function will be inlined) those callgraph edges will be duplicated.
85 If we're cloning the body, those callgraph edges will be
86 updated to point into the new body. (Note that the original
87 callgraph node and edge list will not be altered.)
89 See the CALL_EXPR handling case in copy_tree_body_r (). */
91 /* To Do:
93 o In order to make inlining-on-trees work, we pessimized
94 function-local static constants. In particular, they are now
95 always output, even when not addressed. Fix this by treating
96 function-local static constants just like global static
97 constants; the back-end already knows not to output them if they
98 are not needed.
100 o Provide heuristics to clamp inlining of recursive template
101 calls? */
104 /* Weights that estimate_num_insns uses to estimate the size of the
105 produced code. */
107 eni_weights eni_size_weights;
109 /* Weights that estimate_num_insns uses to estimate the time necessary
110 to execute the produced code. */
112 eni_weights eni_time_weights;
114 /* Prototypes. */
116 static tree declare_return_variable (copy_body_data *, tree, tree);
117 static void remap_block (tree *, copy_body_data *);
118 static void copy_bind_expr (tree *, int *, copy_body_data *);
119 static tree mark_local_for_remap_r (tree *, int *, void *);
120 static void unsave_expr_1 (tree);
121 static tree unsave_r (tree *, int *, void *);
122 static void declare_inline_vars (tree, tree);
123 static void remap_save_expr (tree *, void *, int *);
124 static void prepend_lexical_block (tree current_block, tree new_block);
125 static tree copy_decl_to_var (tree, copy_body_data *);
126 static tree copy_result_decl_to_var (tree, copy_body_data *);
127 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
128 static gimple remap_gimple_stmt (gimple, copy_body_data *);
129 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
131 /* Insert a tree->tree mapping for ID. Despite the name suggests
132 that the trees should be variables, it is used for more than that. */
134 void
135 insert_decl_map (copy_body_data *id, tree key, tree value)
137 *pointer_map_insert (id->decl_map, key) = value;
139 /* Always insert an identity map as well. If we see this same new
140 node again, we won't want to duplicate it a second time. */
141 if (key != value)
142 *pointer_map_insert (id->decl_map, value) = value;
145 /* Insert a tree->tree mapping for ID. This is only used for
146 variables. */
148 static void
149 insert_debug_decl_map (copy_body_data *id, tree key, tree value)
151 if (!gimple_in_ssa_p (id->src_cfun))
152 return;
154 if (!MAY_HAVE_DEBUG_STMTS)
155 return;
157 if (!target_for_debug_bind (key))
158 return;
160 gcc_assert (TREE_CODE (key) == PARM_DECL);
161 gcc_assert (TREE_CODE (value) == VAR_DECL);
163 if (!id->debug_map)
164 id->debug_map = pointer_map_create ();
166 *pointer_map_insert (id->debug_map, key) = value;
169 /* If nonzero, we're remapping the contents of inlined debug
170 statements. If negative, an error has occurred, such as a
171 reference to a variable that isn't available in the inlined
172 context. */
173 static int processing_debug_stmt = 0;
175 /* Construct new SSA name for old NAME. ID is the inline context. */
177 static tree
178 remap_ssa_name (tree name, copy_body_data *id)
180 tree new_tree;
181 tree *n;
183 gcc_assert (TREE_CODE (name) == SSA_NAME);
185 n = (tree *) pointer_map_contains (id->decl_map, name);
186 if (n)
187 return unshare_expr (*n);
189 if (processing_debug_stmt)
191 processing_debug_stmt = -1;
192 return name;
195 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
196 in copy_bb. */
197 new_tree = remap_decl (SSA_NAME_VAR (name), id);
199 /* We might've substituted constant or another SSA_NAME for
200 the variable.
202 Replace the SSA name representing RESULT_DECL by variable during
203 inlining: this saves us from need to introduce PHI node in a case
204 return value is just partly initialized. */
205 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
206 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
207 || !id->transform_return_to_modify))
209 struct ptr_info_def *pi;
210 new_tree = make_ssa_name (new_tree, NULL);
211 insert_decl_map (id, name, new_tree);
212 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
213 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
214 TREE_TYPE (new_tree) = TREE_TYPE (SSA_NAME_VAR (new_tree));
215 /* At least IPA points-to info can be directly transferred. */
216 if (id->src_cfun->gimple_df
217 && id->src_cfun->gimple_df->ipa_pta
218 && (pi = SSA_NAME_PTR_INFO (name))
219 && !pi->pt.anything)
221 struct ptr_info_def *new_pi = get_ptr_info (new_tree);
222 new_pi->pt = pi->pt;
224 if (gimple_nop_p (SSA_NAME_DEF_STMT (name)))
226 /* By inlining function having uninitialized variable, we might
227 extend the lifetime (variable might get reused). This cause
228 ICE in the case we end up extending lifetime of SSA name across
229 abnormal edge, but also increase register pressure.
231 We simply initialize all uninitialized vars by 0 except
232 for case we are inlining to very first BB. We can avoid
233 this for all BBs that are not inside strongly connected
234 regions of the CFG, but this is expensive to test. */
235 if (id->entry_bb
236 && is_gimple_reg (SSA_NAME_VAR (name))
237 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
238 && TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL
239 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
240 || EDGE_COUNT (id->entry_bb->preds) != 1))
242 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
243 gimple init_stmt;
245 init_stmt = gimple_build_assign (new_tree,
246 fold_convert (TREE_TYPE (new_tree),
247 integer_zero_node));
248 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
249 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
251 else
253 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
254 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name))
255 == name)
256 set_default_def (SSA_NAME_VAR (new_tree), new_tree);
260 else
261 insert_decl_map (id, name, new_tree);
262 return new_tree;
265 /* Remap DECL during the copying of the BLOCK tree for the function. */
267 tree
268 remap_decl (tree decl, copy_body_data *id)
270 tree *n;
272 /* We only remap local variables in the current function. */
274 /* See if we have remapped this declaration. */
276 n = (tree *) pointer_map_contains (id->decl_map, decl);
278 if (!n && processing_debug_stmt)
280 processing_debug_stmt = -1;
281 return decl;
284 /* If we didn't already have an equivalent for this declaration,
285 create one now. */
286 if (!n)
288 /* Make a copy of the variable or label. */
289 tree t = id->copy_decl (decl, id);
291 /* Remember it, so that if we encounter this local entity again
292 we can reuse this copy. Do this early because remap_type may
293 need this decl for TYPE_STUB_DECL. */
294 insert_decl_map (id, decl, t);
296 if (!DECL_P (t))
297 return t;
299 /* Remap types, if necessary. */
300 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
301 if (TREE_CODE (t) == TYPE_DECL)
302 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
304 /* Remap sizes as necessary. */
305 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
306 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
308 /* If fields, do likewise for offset and qualifier. */
309 if (TREE_CODE (t) == FIELD_DECL)
311 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
312 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
313 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
316 if (cfun && gimple_in_ssa_p (cfun)
317 && (TREE_CODE (t) == VAR_DECL
318 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
320 get_var_ann (t);
321 add_referenced_var (t);
323 return t;
326 if (id->do_not_unshare)
327 return *n;
328 else
329 return unshare_expr (*n);
332 static tree
333 remap_type_1 (tree type, copy_body_data *id)
335 tree new_tree, t;
337 /* We do need a copy. build and register it now. If this is a pointer or
338 reference type, remap the designated type and make a new pointer or
339 reference type. */
340 if (TREE_CODE (type) == POINTER_TYPE)
342 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
343 TYPE_MODE (type),
344 TYPE_REF_CAN_ALIAS_ALL (type));
345 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
346 new_tree = build_type_attribute_qual_variant (new_tree,
347 TYPE_ATTRIBUTES (type),
348 TYPE_QUALS (type));
349 insert_decl_map (id, type, new_tree);
350 return new_tree;
352 else if (TREE_CODE (type) == REFERENCE_TYPE)
354 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
355 TYPE_MODE (type),
356 TYPE_REF_CAN_ALIAS_ALL (type));
357 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
358 new_tree = build_type_attribute_qual_variant (new_tree,
359 TYPE_ATTRIBUTES (type),
360 TYPE_QUALS (type));
361 insert_decl_map (id, type, new_tree);
362 return new_tree;
364 else
365 new_tree = copy_node (type);
367 insert_decl_map (id, type, new_tree);
369 /* This is a new type, not a copy of an old type. Need to reassociate
370 variants. We can handle everything except the main variant lazily. */
371 t = TYPE_MAIN_VARIANT (type);
372 if (type != t)
374 t = remap_type (t, id);
375 TYPE_MAIN_VARIANT (new_tree) = t;
376 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
377 TYPE_NEXT_VARIANT (t) = new_tree;
379 else
381 TYPE_MAIN_VARIANT (new_tree) = new_tree;
382 TYPE_NEXT_VARIANT (new_tree) = NULL;
385 if (TYPE_STUB_DECL (type))
386 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
388 /* Lazily create pointer and reference types. */
389 TYPE_POINTER_TO (new_tree) = NULL;
390 TYPE_REFERENCE_TO (new_tree) = NULL;
392 switch (TREE_CODE (new_tree))
394 case INTEGER_TYPE:
395 case REAL_TYPE:
396 case FIXED_POINT_TYPE:
397 case ENUMERAL_TYPE:
398 case BOOLEAN_TYPE:
399 t = TYPE_MIN_VALUE (new_tree);
400 if (t && TREE_CODE (t) != INTEGER_CST)
401 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
403 t = TYPE_MAX_VALUE (new_tree);
404 if (t && TREE_CODE (t) != INTEGER_CST)
405 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
406 return new_tree;
408 case FUNCTION_TYPE:
409 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
410 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
411 return new_tree;
413 case ARRAY_TYPE:
414 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
415 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
416 break;
418 case RECORD_TYPE:
419 case UNION_TYPE:
420 case QUAL_UNION_TYPE:
422 tree f, nf = NULL;
424 for (f = TYPE_FIELDS (new_tree); f ; f = TREE_CHAIN (f))
426 t = remap_decl (f, id);
427 DECL_CONTEXT (t) = new_tree;
428 TREE_CHAIN (t) = nf;
429 nf = t;
431 TYPE_FIELDS (new_tree) = nreverse (nf);
433 break;
435 case OFFSET_TYPE:
436 default:
437 /* Shouldn't have been thought variable sized. */
438 gcc_unreachable ();
441 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
442 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
444 return new_tree;
447 tree
448 remap_type (tree type, copy_body_data *id)
450 tree *node;
451 tree tmp;
453 if (type == NULL)
454 return type;
456 /* See if we have remapped this type. */
457 node = (tree *) pointer_map_contains (id->decl_map, type);
458 if (node)
459 return *node;
461 /* The type only needs remapping if it's variably modified. */
462 if (! variably_modified_type_p (type, id->src_fn))
464 insert_decl_map (id, type, type);
465 return type;
468 id->remapping_type_depth++;
469 tmp = remap_type_1 (type, id);
470 id->remapping_type_depth--;
472 return tmp;
475 /* Return previously remapped type of TYPE in ID. Return NULL if TYPE
476 is NULL or TYPE has not been remapped before. */
478 static tree
479 remapped_type (tree type, copy_body_data *id)
481 tree *node;
483 if (type == NULL)
484 return type;
486 /* See if we have remapped this type. */
487 node = (tree *) pointer_map_contains (id->decl_map, type);
488 if (node)
489 return *node;
490 else
491 return NULL;
494 /* The type only needs remapping if it's variably modified. */
495 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
497 static bool
498 can_be_nonlocal (tree decl, copy_body_data *id)
500 /* We can not duplicate function decls. */
501 if (TREE_CODE (decl) == FUNCTION_DECL)
502 return true;
504 /* Local static vars must be non-local or we get multiple declaration
505 problems. */
506 if (TREE_CODE (decl) == VAR_DECL
507 && !auto_var_in_fn_p (decl, id->src_fn))
508 return true;
510 /* At the moment dwarf2out can handle only these types of nodes. We
511 can support more later. */
512 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
513 return false;
515 /* We must use global type. We call remapped_type instead of
516 remap_type since we don't want to remap this type here if it
517 hasn't been remapped before. */
518 if (TREE_TYPE (decl) != remapped_type (TREE_TYPE (decl), id))
519 return false;
521 /* Wihtout SSA we can't tell if variable is used. */
522 if (!gimple_in_ssa_p (cfun))
523 return false;
525 /* Live variables must be copied so we can attach DECL_RTL. */
526 if (var_ann (decl))
527 return false;
529 return true;
532 static tree
533 remap_decls (tree decls, VEC(tree,gc) **nonlocalized_list, copy_body_data *id)
535 tree old_var;
536 tree new_decls = NULL_TREE;
538 /* Remap its variables. */
539 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
541 tree new_var;
543 if (can_be_nonlocal (old_var, id))
545 if (TREE_CODE (old_var) == VAR_DECL
546 && ! DECL_EXTERNAL (old_var)
547 && (var_ann (old_var) || !gimple_in_ssa_p (cfun)))
548 cfun->local_decls = tree_cons (NULL_TREE, old_var,
549 cfun->local_decls);
550 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
551 && !DECL_IGNORED_P (old_var)
552 && nonlocalized_list)
553 VEC_safe_push (tree, gc, *nonlocalized_list, old_var);
554 continue;
557 /* Remap the variable. */
558 new_var = remap_decl (old_var, id);
560 /* If we didn't remap this variable, we can't mess with its
561 TREE_CHAIN. If we remapped this variable to the return slot, it's
562 already declared somewhere else, so don't declare it here. */
564 if (new_var == id->retvar)
566 else if (!new_var)
568 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
569 && !DECL_IGNORED_P (old_var)
570 && nonlocalized_list)
571 VEC_safe_push (tree, gc, *nonlocalized_list, old_var);
573 else
575 gcc_assert (DECL_P (new_var));
576 TREE_CHAIN (new_var) = new_decls;
577 new_decls = new_var;
579 /* Also copy value-expressions. */
580 if (TREE_CODE (new_var) == VAR_DECL
581 && DECL_HAS_VALUE_EXPR_P (new_var))
583 tree tem = DECL_VALUE_EXPR (new_var);
584 bool old_regimplify = id->regimplify;
585 id->remapping_type_depth++;
586 walk_tree (&tem, copy_tree_body_r, id, NULL);
587 id->remapping_type_depth--;
588 id->regimplify = old_regimplify;
589 SET_DECL_VALUE_EXPR (new_var, tem);
594 return nreverse (new_decls);
597 /* Copy the BLOCK to contain remapped versions of the variables
598 therein. And hook the new block into the block-tree. */
600 static void
601 remap_block (tree *block, copy_body_data *id)
603 tree old_block;
604 tree new_block;
606 /* Make the new block. */
607 old_block = *block;
608 new_block = make_node (BLOCK);
609 TREE_USED (new_block) = TREE_USED (old_block);
610 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
611 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
612 BLOCK_NONLOCALIZED_VARS (new_block)
613 = VEC_copy (tree, gc, BLOCK_NONLOCALIZED_VARS (old_block));
614 *block = new_block;
616 /* Remap its variables. */
617 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
618 &BLOCK_NONLOCALIZED_VARS (new_block),
619 id);
621 if (id->transform_lang_insert_block)
622 id->transform_lang_insert_block (new_block);
624 /* Remember the remapped block. */
625 insert_decl_map (id, old_block, new_block);
628 /* Copy the whole block tree and root it in id->block. */
629 static tree
630 remap_blocks (tree block, copy_body_data *id)
632 tree t;
633 tree new_tree = block;
635 if (!block)
636 return NULL;
638 remap_block (&new_tree, id);
639 gcc_assert (new_tree != block);
640 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
641 prepend_lexical_block (new_tree, remap_blocks (t, id));
642 /* Blocks are in arbitrary order, but make things slightly prettier and do
643 not swap order when producing a copy. */
644 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
645 return new_tree;
648 static void
649 copy_statement_list (tree *tp)
651 tree_stmt_iterator oi, ni;
652 tree new_tree;
654 new_tree = alloc_stmt_list ();
655 ni = tsi_start (new_tree);
656 oi = tsi_start (*tp);
657 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
658 *tp = new_tree;
660 for (; !tsi_end_p (oi); tsi_next (&oi))
662 tree stmt = tsi_stmt (oi);
663 if (TREE_CODE (stmt) == STATEMENT_LIST)
664 copy_statement_list (&stmt);
665 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
669 static void
670 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
672 tree block = BIND_EXPR_BLOCK (*tp);
673 /* Copy (and replace) the statement. */
674 copy_tree_r (tp, walk_subtrees, NULL);
675 if (block)
677 remap_block (&block, id);
678 BIND_EXPR_BLOCK (*tp) = block;
681 if (BIND_EXPR_VARS (*tp))
682 /* This will remap a lot of the same decls again, but this should be
683 harmless. */
684 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
688 /* Create a new gimple_seq by remapping all the statements in BODY
689 using the inlining information in ID. */
691 static gimple_seq
692 remap_gimple_seq (gimple_seq body, copy_body_data *id)
694 gimple_stmt_iterator si;
695 gimple_seq new_body = NULL;
697 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
699 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
700 gimple_seq_add_stmt (&new_body, new_stmt);
703 return new_body;
707 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
708 block using the mapping information in ID. */
710 static gimple
711 copy_gimple_bind (gimple stmt, copy_body_data *id)
713 gimple new_bind;
714 tree new_block, new_vars;
715 gimple_seq body, new_body;
717 /* Copy the statement. Note that we purposely don't use copy_stmt
718 here because we need to remap statements as we copy. */
719 body = gimple_bind_body (stmt);
720 new_body = remap_gimple_seq (body, id);
722 new_block = gimple_bind_block (stmt);
723 if (new_block)
724 remap_block (&new_block, id);
726 /* This will remap a lot of the same decls again, but this should be
727 harmless. */
728 new_vars = gimple_bind_vars (stmt);
729 if (new_vars)
730 new_vars = remap_decls (new_vars, NULL, id);
732 new_bind = gimple_build_bind (new_vars, new_body, new_block);
734 return new_bind;
738 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
739 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
740 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
741 recursing into the children nodes of *TP. */
743 static tree
744 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
746 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
747 copy_body_data *id = (copy_body_data *) wi_p->info;
748 tree fn = id->src_fn;
750 if (TREE_CODE (*tp) == SSA_NAME)
752 *tp = remap_ssa_name (*tp, id);
753 *walk_subtrees = 0;
754 return NULL;
756 else if (auto_var_in_fn_p (*tp, fn))
758 /* Local variables and labels need to be replaced by equivalent
759 variables. We don't want to copy static variables; there's
760 only one of those, no matter how many times we inline the
761 containing function. Similarly for globals from an outer
762 function. */
763 tree new_decl;
765 /* Remap the declaration. */
766 new_decl = remap_decl (*tp, id);
767 gcc_assert (new_decl);
768 /* Replace this variable with the copy. */
769 STRIP_TYPE_NOPS (new_decl);
770 /* ??? The C++ frontend uses void * pointer zero to initialize
771 any other type. This confuses the middle-end type verification.
772 As cloned bodies do not go through gimplification again the fixup
773 there doesn't trigger. */
774 if (TREE_CODE (new_decl) == INTEGER_CST
775 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
776 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
777 *tp = new_decl;
778 *walk_subtrees = 0;
780 else if (TREE_CODE (*tp) == STATEMENT_LIST)
781 gcc_unreachable ();
782 else if (TREE_CODE (*tp) == SAVE_EXPR)
783 gcc_unreachable ();
784 else if (TREE_CODE (*tp) == LABEL_DECL
785 && (!DECL_CONTEXT (*tp)
786 || decl_function_context (*tp) == id->src_fn))
787 /* These may need to be remapped for EH handling. */
788 *tp = remap_decl (*tp, id);
789 else if (TYPE_P (*tp))
790 /* Types may need remapping as well. */
791 *tp = remap_type (*tp, id);
792 else if (CONSTANT_CLASS_P (*tp))
794 /* If this is a constant, we have to copy the node iff the type
795 will be remapped. copy_tree_r will not copy a constant. */
796 tree new_type = remap_type (TREE_TYPE (*tp), id);
798 if (new_type == TREE_TYPE (*tp))
799 *walk_subtrees = 0;
801 else if (TREE_CODE (*tp) == INTEGER_CST)
802 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
803 TREE_INT_CST_HIGH (*tp));
804 else
806 *tp = copy_node (*tp);
807 TREE_TYPE (*tp) = new_type;
810 else
812 /* Otherwise, just copy the node. Note that copy_tree_r already
813 knows not to copy VAR_DECLs, etc., so this is safe. */
814 if (TREE_CODE (*tp) == MEM_REF)
816 /* We need to re-canonicalize MEM_REFs from inline substitutions
817 that can happen when a pointer argument is an ADDR_EXPR. */
818 tree decl = TREE_OPERAND (*tp, 0);
819 tree *n;
821 n = (tree *) pointer_map_contains (id->decl_map, decl);
822 if (n)
824 tree old = *tp;
825 tree ptr = unshare_expr (*n);
826 tree tem;
827 if ((tem = maybe_fold_offset_to_reference (EXPR_LOCATION (*tp),
828 ptr,
829 TREE_OPERAND (*tp, 1),
830 TREE_TYPE (*tp)))
831 && TREE_THIS_VOLATILE (tem) == TREE_THIS_VOLATILE (old))
833 tree *tem_basep = &tem;
834 while (handled_component_p (*tem_basep))
835 tem_basep = &TREE_OPERAND (*tem_basep, 0);
836 if (TREE_CODE (*tem_basep) == MEM_REF)
837 *tem_basep
838 = build2 (MEM_REF, TREE_TYPE (*tem_basep),
839 TREE_OPERAND (*tem_basep, 0),
840 fold_convert (TREE_TYPE (TREE_OPERAND (*tp, 1)),
841 TREE_OPERAND (*tem_basep, 1)));
842 else
843 *tem_basep
844 = build2 (MEM_REF, TREE_TYPE (*tem_basep),
845 build_fold_addr_expr (*tem_basep),
846 build_int_cst
847 (TREE_TYPE (TREE_OPERAND (*tp, 1)), 0));
848 *tp = tem;
850 else
852 *tp = fold_build2 (MEM_REF, TREE_TYPE (*tp),
853 ptr, TREE_OPERAND (*tp, 1));
854 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
856 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
857 *walk_subtrees = 0;
858 return NULL;
862 /* Here is the "usual case". Copy this tree node, and then
863 tweak some special cases. */
864 copy_tree_r (tp, walk_subtrees, NULL);
866 /* Global variables we haven't seen yet need to go into referenced
867 vars. If not referenced from types only. */
868 if (gimple_in_ssa_p (cfun)
869 && TREE_CODE (*tp) == VAR_DECL
870 && id->remapping_type_depth == 0
871 && !processing_debug_stmt)
872 add_referenced_var (*tp);
874 /* We should never have TREE_BLOCK set on non-statements. */
875 if (EXPR_P (*tp))
876 gcc_assert (!TREE_BLOCK (*tp));
878 if (TREE_CODE (*tp) != OMP_CLAUSE)
879 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
881 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
883 /* The copied TARGET_EXPR has never been expanded, even if the
884 original node was expanded already. */
885 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
886 TREE_OPERAND (*tp, 3) = NULL_TREE;
888 else if (TREE_CODE (*tp) == ADDR_EXPR)
890 /* Variable substitution need not be simple. In particular,
891 the MEM_REF substitution above. Make sure that
892 TREE_CONSTANT and friends are up-to-date. But make sure
893 to not improperly set TREE_BLOCK on some sub-expressions. */
894 int invariant = is_gimple_min_invariant (*tp);
895 tree block = id->block;
896 id->block = NULL_TREE;
897 walk_tree (&TREE_OPERAND (*tp, 0), remap_gimple_op_r, data, NULL);
898 id->block = block;
899 recompute_tree_invariant_for_addr_expr (*tp);
901 /* If this used to be invariant, but is not any longer,
902 then regimplification is probably needed. */
903 if (invariant && !is_gimple_min_invariant (*tp))
904 id->regimplify = true;
906 *walk_subtrees = 0;
910 /* Keep iterating. */
911 return NULL_TREE;
915 /* Called from copy_body_id via walk_tree. DATA is really a
916 `copy_body_data *'. */
918 tree
919 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
921 copy_body_data *id = (copy_body_data *) data;
922 tree fn = id->src_fn;
923 tree new_block;
925 /* Begin by recognizing trees that we'll completely rewrite for the
926 inlining context. Our output for these trees is completely
927 different from out input (e.g. RETURN_EXPR is deleted, and morphs
928 into an edge). Further down, we'll handle trees that get
929 duplicated and/or tweaked. */
931 /* When requested, RETURN_EXPRs should be transformed to just the
932 contained MODIFY_EXPR. The branch semantics of the return will
933 be handled elsewhere by manipulating the CFG rather than a statement. */
934 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
936 tree assignment = TREE_OPERAND (*tp, 0);
938 /* If we're returning something, just turn that into an
939 assignment into the equivalent of the original RESULT_DECL.
940 If the "assignment" is just the result decl, the result
941 decl has already been set (e.g. a recent "foo (&result_decl,
942 ...)"); just toss the entire RETURN_EXPR. */
943 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
945 /* Replace the RETURN_EXPR with (a copy of) the
946 MODIFY_EXPR hanging underneath. */
947 *tp = copy_node (assignment);
949 else /* Else the RETURN_EXPR returns no value. */
951 *tp = NULL;
952 return (tree) (void *)1;
955 else if (TREE_CODE (*tp) == SSA_NAME)
957 *tp = remap_ssa_name (*tp, id);
958 *walk_subtrees = 0;
959 return NULL;
962 /* Local variables and labels need to be replaced by equivalent
963 variables. We don't want to copy static variables; there's only
964 one of those, no matter how many times we inline the containing
965 function. Similarly for globals from an outer function. */
966 else if (auto_var_in_fn_p (*tp, fn))
968 tree new_decl;
970 /* Remap the declaration. */
971 new_decl = remap_decl (*tp, id);
972 gcc_assert (new_decl);
973 /* Replace this variable with the copy. */
974 STRIP_TYPE_NOPS (new_decl);
975 *tp = new_decl;
976 *walk_subtrees = 0;
978 else if (TREE_CODE (*tp) == STATEMENT_LIST)
979 copy_statement_list (tp);
980 else if (TREE_CODE (*tp) == SAVE_EXPR
981 || TREE_CODE (*tp) == TARGET_EXPR)
982 remap_save_expr (tp, id->decl_map, walk_subtrees);
983 else if (TREE_CODE (*tp) == LABEL_DECL
984 && (! DECL_CONTEXT (*tp)
985 || decl_function_context (*tp) == id->src_fn))
986 /* These may need to be remapped for EH handling. */
987 *tp = remap_decl (*tp, id);
988 else if (TREE_CODE (*tp) == BIND_EXPR)
989 copy_bind_expr (tp, walk_subtrees, id);
990 /* Types may need remapping as well. */
991 else if (TYPE_P (*tp))
992 *tp = remap_type (*tp, id);
994 /* If this is a constant, we have to copy the node iff the type will be
995 remapped. copy_tree_r will not copy a constant. */
996 else if (CONSTANT_CLASS_P (*tp))
998 tree new_type = remap_type (TREE_TYPE (*tp), id);
1000 if (new_type == TREE_TYPE (*tp))
1001 *walk_subtrees = 0;
1003 else if (TREE_CODE (*tp) == INTEGER_CST)
1004 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
1005 TREE_INT_CST_HIGH (*tp));
1006 else
1008 *tp = copy_node (*tp);
1009 TREE_TYPE (*tp) = new_type;
1013 /* Otherwise, just copy the node. Note that copy_tree_r already
1014 knows not to copy VAR_DECLs, etc., so this is safe. */
1015 else
1017 /* Here we handle trees that are not completely rewritten.
1018 First we detect some inlining-induced bogosities for
1019 discarding. */
1020 if (TREE_CODE (*tp) == MODIFY_EXPR
1021 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
1022 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
1024 /* Some assignments VAR = VAR; don't generate any rtl code
1025 and thus don't count as variable modification. Avoid
1026 keeping bogosities like 0 = 0. */
1027 tree decl = TREE_OPERAND (*tp, 0), value;
1028 tree *n;
1030 n = (tree *) pointer_map_contains (id->decl_map, decl);
1031 if (n)
1033 value = *n;
1034 STRIP_TYPE_NOPS (value);
1035 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1037 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
1038 return copy_tree_body_r (tp, walk_subtrees, data);
1042 else if (TREE_CODE (*tp) == INDIRECT_REF)
1044 /* Get rid of *& from inline substitutions that can happen when a
1045 pointer argument is an ADDR_EXPR. */
1046 tree decl = TREE_OPERAND (*tp, 0);
1047 tree *n;
1049 n = (tree *) pointer_map_contains (id->decl_map, decl);
1050 if (n)
1052 tree new_tree;
1053 tree old;
1054 /* If we happen to get an ADDR_EXPR in n->value, strip
1055 it manually here as we'll eventually get ADDR_EXPRs
1056 which lie about their types pointed to. In this case
1057 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
1058 but we absolutely rely on that. As fold_indirect_ref
1059 does other useful transformations, try that first, though. */
1060 tree type = TREE_TYPE (TREE_TYPE (*n));
1061 if (id->do_not_unshare)
1062 new_tree = *n;
1063 else
1064 new_tree = unshare_expr (*n);
1065 old = *tp;
1066 *tp = gimple_fold_indirect_ref (new_tree);
1067 if (! *tp)
1069 if (TREE_CODE (new_tree) == ADDR_EXPR)
1071 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
1072 type, new_tree);
1073 /* ??? We should either assert here or build
1074 a VIEW_CONVERT_EXPR instead of blindly leaking
1075 incompatible types to our IL. */
1076 if (! *tp)
1077 *tp = TREE_OPERAND (new_tree, 0);
1079 else
1081 *tp = build1 (INDIRECT_REF, type, new_tree);
1082 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1083 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
1086 *walk_subtrees = 0;
1087 return NULL;
1090 else if (TREE_CODE (*tp) == MEM_REF)
1092 /* We need to re-canonicalize MEM_REFs from inline substitutions
1093 that can happen when a pointer argument is an ADDR_EXPR. */
1094 tree decl = TREE_OPERAND (*tp, 0);
1095 tree *n;
1097 n = (tree *) pointer_map_contains (id->decl_map, decl);
1098 if (n)
1100 tree old = *tp;
1101 *tp = fold_build2 (MEM_REF, TREE_TYPE (*tp),
1102 unshare_expr (*n), TREE_OPERAND (*tp, 1));
1103 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
1104 TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
1105 *walk_subtrees = 0;
1106 return NULL;
1110 /* Here is the "usual case". Copy this tree node, and then
1111 tweak some special cases. */
1112 copy_tree_r (tp, walk_subtrees, NULL);
1114 /* Global variables we haven't seen yet needs to go into referenced
1115 vars. If not referenced from types or debug stmts only. */
1116 if (gimple_in_ssa_p (cfun)
1117 && TREE_CODE (*tp) == VAR_DECL
1118 && id->remapping_type_depth == 0
1119 && !processing_debug_stmt)
1120 add_referenced_var (*tp);
1122 /* If EXPR has block defined, map it to newly constructed block.
1123 When inlining we want EXPRs without block appear in the block
1124 of function call if we are not remapping a type. */
1125 if (EXPR_P (*tp))
1127 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1128 if (TREE_BLOCK (*tp))
1130 tree *n;
1131 n = (tree *) pointer_map_contains (id->decl_map,
1132 TREE_BLOCK (*tp));
1133 gcc_assert (n || id->remapping_type_depth != 0);
1134 if (n)
1135 new_block = *n;
1137 TREE_BLOCK (*tp) = new_block;
1140 if (TREE_CODE (*tp) != OMP_CLAUSE)
1141 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1143 /* The copied TARGET_EXPR has never been expanded, even if the
1144 original node was expanded already. */
1145 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1147 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1148 TREE_OPERAND (*tp, 3) = NULL_TREE;
1151 /* Variable substitution need not be simple. In particular, the
1152 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
1153 and friends are up-to-date. */
1154 else if (TREE_CODE (*tp) == ADDR_EXPR)
1156 int invariant = is_gimple_min_invariant (*tp);
1157 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1159 /* Handle the case where we substituted an INDIRECT_REF
1160 into the operand of the ADDR_EXPR. */
1161 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1162 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1163 else
1164 recompute_tree_invariant_for_addr_expr (*tp);
1166 /* If this used to be invariant, but is not any longer,
1167 then regimplification is probably needed. */
1168 if (invariant && !is_gimple_min_invariant (*tp))
1169 id->regimplify = true;
1171 *walk_subtrees = 0;
1175 /* Keep iterating. */
1176 return NULL_TREE;
1179 /* Helper for remap_gimple_stmt. Given an EH region number for the
1180 source function, map that to the duplicate EH region number in
1181 the destination function. */
1183 static int
1184 remap_eh_region_nr (int old_nr, copy_body_data *id)
1186 eh_region old_r, new_r;
1187 void **slot;
1189 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1190 slot = pointer_map_contains (id->eh_map, old_r);
1191 new_r = (eh_region) *slot;
1193 return new_r->index;
1196 /* Similar, but operate on INTEGER_CSTs. */
1198 static tree
1199 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1201 int old_nr, new_nr;
1203 old_nr = tree_low_cst (old_t_nr, 0);
1204 new_nr = remap_eh_region_nr (old_nr, id);
1206 return build_int_cst (NULL, new_nr);
1209 /* Helper for copy_bb. Remap statement STMT using the inlining
1210 information in ID. Return the new statement copy. */
1212 static gimple
1213 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1215 gimple copy = NULL;
1216 struct walk_stmt_info wi;
1217 tree new_block;
1218 bool skip_first = false;
1220 /* Begin by recognizing trees that we'll completely rewrite for the
1221 inlining context. Our output for these trees is completely
1222 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1223 into an edge). Further down, we'll handle trees that get
1224 duplicated and/or tweaked. */
1226 /* When requested, GIMPLE_RETURNs should be transformed to just the
1227 contained GIMPLE_ASSIGN. The branch semantics of the return will
1228 be handled elsewhere by manipulating the CFG rather than the
1229 statement. */
1230 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1232 tree retval = gimple_return_retval (stmt);
1234 /* If we're returning something, just turn that into an
1235 assignment into the equivalent of the original RESULT_DECL.
1236 If RETVAL is just the result decl, the result decl has
1237 already been set (e.g. a recent "foo (&result_decl, ...)");
1238 just toss the entire GIMPLE_RETURN. */
1239 if (retval && TREE_CODE (retval) != RESULT_DECL)
1241 copy = gimple_build_assign (id->retvar, retval);
1242 /* id->retvar is already substituted. Skip it on later remapping. */
1243 skip_first = true;
1245 else
1246 return gimple_build_nop ();
1248 else if (gimple_has_substatements (stmt))
1250 gimple_seq s1, s2;
1252 /* When cloning bodies from the C++ front end, we will be handed bodies
1253 in High GIMPLE form. Handle here all the High GIMPLE statements that
1254 have embedded statements. */
1255 switch (gimple_code (stmt))
1257 case GIMPLE_BIND:
1258 copy = copy_gimple_bind (stmt, id);
1259 break;
1261 case GIMPLE_CATCH:
1262 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1263 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1264 break;
1266 case GIMPLE_EH_FILTER:
1267 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1268 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1269 break;
1271 case GIMPLE_TRY:
1272 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1273 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1274 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1275 break;
1277 case GIMPLE_WITH_CLEANUP_EXPR:
1278 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1279 copy = gimple_build_wce (s1);
1280 break;
1282 case GIMPLE_OMP_PARALLEL:
1283 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1284 copy = gimple_build_omp_parallel
1285 (s1,
1286 gimple_omp_parallel_clauses (stmt),
1287 gimple_omp_parallel_child_fn (stmt),
1288 gimple_omp_parallel_data_arg (stmt));
1289 break;
1291 case GIMPLE_OMP_TASK:
1292 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1293 copy = gimple_build_omp_task
1294 (s1,
1295 gimple_omp_task_clauses (stmt),
1296 gimple_omp_task_child_fn (stmt),
1297 gimple_omp_task_data_arg (stmt),
1298 gimple_omp_task_copy_fn (stmt),
1299 gimple_omp_task_arg_size (stmt),
1300 gimple_omp_task_arg_align (stmt));
1301 break;
1303 case GIMPLE_OMP_FOR:
1304 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1305 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1306 copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1307 gimple_omp_for_collapse (stmt), s2);
1309 size_t i;
1310 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1312 gimple_omp_for_set_index (copy, i,
1313 gimple_omp_for_index (stmt, i));
1314 gimple_omp_for_set_initial (copy, i,
1315 gimple_omp_for_initial (stmt, i));
1316 gimple_omp_for_set_final (copy, i,
1317 gimple_omp_for_final (stmt, i));
1318 gimple_omp_for_set_incr (copy, i,
1319 gimple_omp_for_incr (stmt, i));
1320 gimple_omp_for_set_cond (copy, i,
1321 gimple_omp_for_cond (stmt, i));
1324 break;
1326 case GIMPLE_OMP_MASTER:
1327 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1328 copy = gimple_build_omp_master (s1);
1329 break;
1331 case GIMPLE_OMP_ORDERED:
1332 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1333 copy = gimple_build_omp_ordered (s1);
1334 break;
1336 case GIMPLE_OMP_SECTION:
1337 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1338 copy = gimple_build_omp_section (s1);
1339 break;
1341 case GIMPLE_OMP_SECTIONS:
1342 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1343 copy = gimple_build_omp_sections
1344 (s1, gimple_omp_sections_clauses (stmt));
1345 break;
1347 case GIMPLE_OMP_SINGLE:
1348 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1349 copy = gimple_build_omp_single
1350 (s1, gimple_omp_single_clauses (stmt));
1351 break;
1353 case GIMPLE_OMP_CRITICAL:
1354 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1355 copy
1356 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1357 break;
1359 default:
1360 gcc_unreachable ();
1363 else
1365 if (gimple_assign_copy_p (stmt)
1366 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1367 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1369 /* Here we handle statements that are not completely rewritten.
1370 First we detect some inlining-induced bogosities for
1371 discarding. */
1373 /* Some assignments VAR = VAR; don't generate any rtl code
1374 and thus don't count as variable modification. Avoid
1375 keeping bogosities like 0 = 0. */
1376 tree decl = gimple_assign_lhs (stmt), value;
1377 tree *n;
1379 n = (tree *) pointer_map_contains (id->decl_map, decl);
1380 if (n)
1382 value = *n;
1383 STRIP_TYPE_NOPS (value);
1384 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1385 return gimple_build_nop ();
1389 if (gimple_debug_bind_p (stmt))
1391 copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1392 gimple_debug_bind_get_value (stmt),
1393 stmt);
1394 VEC_safe_push (gimple, heap, id->debug_stmts, copy);
1395 return copy;
1398 /* Create a new deep copy of the statement. */
1399 copy = gimple_copy (stmt);
1401 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1402 RESX and EH_DISPATCH. */
1403 if (id->eh_map)
1404 switch (gimple_code (copy))
1406 case GIMPLE_CALL:
1408 tree r, fndecl = gimple_call_fndecl (copy);
1409 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1410 switch (DECL_FUNCTION_CODE (fndecl))
1412 case BUILT_IN_EH_COPY_VALUES:
1413 r = gimple_call_arg (copy, 1);
1414 r = remap_eh_region_tree_nr (r, id);
1415 gimple_call_set_arg (copy, 1, r);
1416 /* FALLTHRU */
1418 case BUILT_IN_EH_POINTER:
1419 case BUILT_IN_EH_FILTER:
1420 r = gimple_call_arg (copy, 0);
1421 r = remap_eh_region_tree_nr (r, id);
1422 gimple_call_set_arg (copy, 0, r);
1423 break;
1425 default:
1426 break;
1429 /* Reset alias info if we didn't apply measures to
1430 keep it valid over inlining by setting DECL_PT_UID. */
1431 if (!id->src_cfun->gimple_df
1432 || !id->src_cfun->gimple_df->ipa_pta)
1433 gimple_call_reset_alias_info (copy);
1435 break;
1437 case GIMPLE_RESX:
1439 int r = gimple_resx_region (copy);
1440 r = remap_eh_region_nr (r, id);
1441 gimple_resx_set_region (copy, r);
1443 break;
1445 case GIMPLE_EH_DISPATCH:
1447 int r = gimple_eh_dispatch_region (copy);
1448 r = remap_eh_region_nr (r, id);
1449 gimple_eh_dispatch_set_region (copy, r);
1451 break;
1453 default:
1454 break;
1458 /* If STMT has a block defined, map it to the newly constructed
1459 block. When inlining we want statements without a block to
1460 appear in the block of the function call. */
1461 new_block = id->block;
1462 if (gimple_block (copy))
1464 tree *n;
1465 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1466 gcc_assert (n);
1467 new_block = *n;
1470 gimple_set_block (copy, new_block);
1472 if (gimple_debug_bind_p (copy))
1473 return copy;
1475 /* Remap all the operands in COPY. */
1476 memset (&wi, 0, sizeof (wi));
1477 wi.info = id;
1478 if (skip_first)
1479 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1480 else
1481 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1483 /* Clear the copied virtual operands. We are not remapping them here
1484 but are going to recreate them from scratch. */
1485 if (gimple_has_mem_ops (copy))
1487 gimple_set_vdef (copy, NULL_TREE);
1488 gimple_set_vuse (copy, NULL_TREE);
1491 return copy;
1495 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1496 later */
1498 static basic_block
1499 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1500 gcov_type count_scale)
1502 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1503 basic_block copy_basic_block;
1504 tree decl;
1505 gcov_type freq;
1506 basic_block prev;
1508 /* Search for previous copied basic block. */
1509 prev = bb->prev_bb;
1510 while (!prev->aux)
1511 prev = prev->prev_bb;
1513 /* create_basic_block() will append every new block to
1514 basic_block_info automatically. */
1515 copy_basic_block = create_basic_block (NULL, (void *) 0,
1516 (basic_block) prev->aux);
1517 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1519 /* We are going to rebuild frequencies from scratch. These values
1520 have just small importance to drive canonicalize_loop_headers. */
1521 freq = ((gcov_type)bb->frequency * frequency_scale / REG_BR_PROB_BASE);
1523 /* We recompute frequencies after inlining, so this is quite safe. */
1524 if (freq > BB_FREQ_MAX)
1525 freq = BB_FREQ_MAX;
1526 copy_basic_block->frequency = freq;
1528 copy_gsi = gsi_start_bb (copy_basic_block);
1530 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1532 gimple stmt = gsi_stmt (gsi);
1533 gimple orig_stmt = stmt;
1535 id->regimplify = false;
1536 stmt = remap_gimple_stmt (stmt, id);
1537 if (gimple_nop_p (stmt))
1538 continue;
1540 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1541 seq_gsi = copy_gsi;
1543 /* With return slot optimization we can end up with
1544 non-gimple (foo *)&this->m, fix that here. */
1545 if (is_gimple_assign (stmt)
1546 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1547 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1549 tree new_rhs;
1550 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1551 gimple_assign_rhs1 (stmt),
1552 true, NULL, false, GSI_NEW_STMT);
1553 gimple_assign_set_rhs1 (stmt, new_rhs);
1554 id->regimplify = false;
1557 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1559 if (id->regimplify)
1560 gimple_regimplify_operands (stmt, &seq_gsi);
1562 /* If copy_basic_block has been empty at the start of this iteration,
1563 call gsi_start_bb again to get at the newly added statements. */
1564 if (gsi_end_p (copy_gsi))
1565 copy_gsi = gsi_start_bb (copy_basic_block);
1566 else
1567 gsi_next (&copy_gsi);
1569 /* Process the new statement. The call to gimple_regimplify_operands
1570 possibly turned the statement into multiple statements, we
1571 need to process all of them. */
1574 tree fn;
1576 stmt = gsi_stmt (copy_gsi);
1577 if (is_gimple_call (stmt)
1578 && gimple_call_va_arg_pack_p (stmt)
1579 && id->gimple_call)
1581 /* __builtin_va_arg_pack () should be replaced by
1582 all arguments corresponding to ... in the caller. */
1583 tree p;
1584 gimple new_call;
1585 VEC(tree, heap) *argarray;
1586 size_t nargs = gimple_call_num_args (id->gimple_call);
1587 size_t n;
1589 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1590 nargs--;
1592 /* Create the new array of arguments. */
1593 n = nargs + gimple_call_num_args (stmt);
1594 argarray = VEC_alloc (tree, heap, n);
1595 VEC_safe_grow (tree, heap, argarray, n);
1597 /* Copy all the arguments before '...' */
1598 memcpy (VEC_address (tree, argarray),
1599 gimple_call_arg_ptr (stmt, 0),
1600 gimple_call_num_args (stmt) * sizeof (tree));
1602 /* Append the arguments passed in '...' */
1603 memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1604 gimple_call_arg_ptr (id->gimple_call, 0)
1605 + (gimple_call_num_args (id->gimple_call) - nargs),
1606 nargs * sizeof (tree));
1608 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1609 argarray);
1611 VEC_free (tree, heap, argarray);
1613 /* Copy all GIMPLE_CALL flags, location and block, except
1614 GF_CALL_VA_ARG_PACK. */
1615 gimple_call_copy_flags (new_call, stmt);
1616 gimple_call_set_va_arg_pack (new_call, false);
1617 gimple_set_location (new_call, gimple_location (stmt));
1618 gimple_set_block (new_call, gimple_block (stmt));
1619 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1621 gsi_replace (&copy_gsi, new_call, false);
1622 stmt = new_call;
1624 else if (is_gimple_call (stmt)
1625 && id->gimple_call
1626 && (decl = gimple_call_fndecl (stmt))
1627 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1628 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1630 /* __builtin_va_arg_pack_len () should be replaced by
1631 the number of anonymous arguments. */
1632 size_t nargs = gimple_call_num_args (id->gimple_call);
1633 tree count, p;
1634 gimple new_stmt;
1636 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1637 nargs--;
1639 count = build_int_cst (integer_type_node, nargs);
1640 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1641 gsi_replace (&copy_gsi, new_stmt, false);
1642 stmt = new_stmt;
1645 /* Statements produced by inlining can be unfolded, especially
1646 when we constant propagated some operands. We can't fold
1647 them right now for two reasons:
1648 1) folding require SSA_NAME_DEF_STMTs to be correct
1649 2) we can't change function calls to builtins.
1650 So we just mark statement for later folding. We mark
1651 all new statements, instead just statements that has changed
1652 by some nontrivial substitution so even statements made
1653 foldable indirectly are updated. If this turns out to be
1654 expensive, copy_body can be told to watch for nontrivial
1655 changes. */
1656 if (id->statements_to_fold)
1657 pointer_set_insert (id->statements_to_fold, stmt);
1659 /* We're duplicating a CALL_EXPR. Find any corresponding
1660 callgraph edges and update or duplicate them. */
1661 if (is_gimple_call (stmt))
1663 struct cgraph_edge *edge;
1664 int flags;
1666 switch (id->transform_call_graph_edges)
1668 case CB_CGE_DUPLICATE:
1669 edge = cgraph_edge (id->src_node, orig_stmt);
1670 if (edge)
1672 int edge_freq = edge->frequency;
1673 edge = cgraph_clone_edge (edge, id->dst_node, stmt,
1674 gimple_uid (stmt),
1675 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1676 edge->frequency, true);
1677 /* We could also just rescale the frequency, but
1678 doing so would introduce roundoff errors and make
1679 verifier unhappy. */
1680 edge->frequency
1681 = compute_call_stmt_bb_frequency (id->dst_node->decl,
1682 copy_basic_block);
1683 if (dump_file
1684 && profile_status_for_function (cfun) != PROFILE_ABSENT
1685 && (edge_freq > edge->frequency + 10
1686 || edge_freq < edge->frequency - 10))
1688 fprintf (dump_file, "Edge frequency estimated by "
1689 "cgraph %i diverge from inliner's estimate %i\n",
1690 edge_freq,
1691 edge->frequency);
1692 fprintf (dump_file,
1693 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
1694 bb->index,
1695 bb->frequency,
1696 copy_basic_block->frequency);
1698 stmt = cgraph_redirect_edge_call_stmt_to_callee (edge);
1700 break;
1702 case CB_CGE_MOVE_CLONES:
1703 cgraph_set_call_stmt_including_clones (id->dst_node,
1704 orig_stmt, stmt);
1705 edge = cgraph_edge (id->dst_node, stmt);
1706 break;
1708 case CB_CGE_MOVE:
1709 edge = cgraph_edge (id->dst_node, orig_stmt);
1710 if (edge)
1711 cgraph_set_call_stmt (edge, stmt);
1712 break;
1714 default:
1715 gcc_unreachable ();
1718 /* Constant propagation on argument done during inlining
1719 may create new direct call. Produce an edge for it. */
1720 if ((!edge
1721 || (edge->indirect_inlining_edge
1722 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1723 && (fn = gimple_call_fndecl (stmt)) != NULL)
1725 struct cgraph_node *dest = cgraph_node (fn);
1727 /* We have missing edge in the callgraph. This can happen
1728 when previous inlining turned an indirect call into a
1729 direct call by constant propagating arguments or we are
1730 producing dead clone (for further cloning). In all
1731 other cases we hit a bug (incorrect node sharing is the
1732 most common reason for missing edges). */
1733 gcc_assert (dest->needed || !dest->analyzed
1734 || dest->address_taken
1735 || !id->src_node->analyzed);
1736 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1737 cgraph_create_edge_including_clones
1738 (id->dst_node, dest, orig_stmt, stmt, bb->count,
1739 compute_call_stmt_bb_frequency (id->dst_node->decl,
1740 copy_basic_block),
1741 bb->loop_depth, CIF_ORIGINALLY_INDIRECT_CALL);
1742 else
1743 cgraph_create_edge (id->dst_node, dest, stmt,
1744 bb->count,
1745 compute_call_stmt_bb_frequency
1746 (id->dst_node->decl, copy_basic_block),
1747 bb->loop_depth)->inline_failed
1748 = CIF_ORIGINALLY_INDIRECT_CALL;
1749 if (dump_file)
1751 fprintf (dump_file, "Created new direct edge to %s\n",
1752 cgraph_node_name (dest));
1756 flags = gimple_call_flags (stmt);
1757 if (flags & ECF_MAY_BE_ALLOCA)
1758 cfun->calls_alloca = true;
1759 if (flags & ECF_RETURNS_TWICE)
1760 cfun->calls_setjmp = true;
1763 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
1764 id->eh_map, id->eh_lp_nr);
1766 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
1768 ssa_op_iter i;
1769 tree def;
1771 find_new_referenced_vars (gsi_stmt (copy_gsi));
1772 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1773 if (TREE_CODE (def) == SSA_NAME)
1774 SSA_NAME_DEF_STMT (def) = stmt;
1777 gsi_next (&copy_gsi);
1779 while (!gsi_end_p (copy_gsi));
1781 copy_gsi = gsi_last_bb (copy_basic_block);
1784 return copy_basic_block;
1787 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1788 form is quite easy, since dominator relationship for old basic blocks does
1789 not change.
1791 There is however exception where inlining might change dominator relation
1792 across EH edges from basic block within inlined functions destinating
1793 to landing pads in function we inline into.
1795 The function fills in PHI_RESULTs of such PHI nodes if they refer
1796 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1797 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1798 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1799 set, and this means that there will be no overlapping live ranges
1800 for the underlying symbol.
1802 This might change in future if we allow redirecting of EH edges and
1803 we might want to change way build CFG pre-inlining to include
1804 all the possible edges then. */
1805 static void
1806 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1807 bool can_throw, bool nonlocal_goto)
1809 edge e;
1810 edge_iterator ei;
1812 FOR_EACH_EDGE (e, ei, bb->succs)
1813 if (!e->dest->aux
1814 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1816 gimple phi;
1817 gimple_stmt_iterator si;
1819 if (!nonlocal_goto)
1820 gcc_assert (e->flags & EDGE_EH);
1822 if (!can_throw)
1823 gcc_assert (!(e->flags & EDGE_EH));
1825 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1827 edge re;
1829 phi = gsi_stmt (si);
1831 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1832 gcc_assert (!e->dest->aux);
1834 gcc_assert ((e->flags & EDGE_EH)
1835 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1837 if (!is_gimple_reg (PHI_RESULT (phi)))
1839 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1840 continue;
1843 re = find_edge (ret_bb, e->dest);
1844 gcc_assert (re);
1845 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1846 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1848 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1849 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1855 /* Copy edges from BB into its copy constructed earlier, scale profile
1856 accordingly. Edges will be taken care of later. Assume aux
1857 pointers to point to the copies of each BB. Return true if any
1858 debug stmts are left after a statement that must end the basic block. */
1860 static bool
1861 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1863 basic_block new_bb = (basic_block) bb->aux;
1864 edge_iterator ei;
1865 edge old_edge;
1866 gimple_stmt_iterator si;
1867 int flags;
1868 bool need_debug_cleanup = false;
1870 /* Use the indices from the original blocks to create edges for the
1871 new ones. */
1872 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1873 if (!(old_edge->flags & EDGE_EH))
1875 edge new_edge;
1877 flags = old_edge->flags;
1879 /* Return edges do get a FALLTHRU flag when the get inlined. */
1880 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1881 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1882 flags |= EDGE_FALLTHRU;
1883 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1884 new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1885 new_edge->probability = old_edge->probability;
1888 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1889 return false;
1891 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1893 gimple copy_stmt;
1894 bool can_throw, nonlocal_goto;
1896 copy_stmt = gsi_stmt (si);
1897 if (!is_gimple_debug (copy_stmt))
1899 update_stmt (copy_stmt);
1900 if (gimple_in_ssa_p (cfun))
1901 mark_symbols_for_renaming (copy_stmt);
1904 /* Do this before the possible split_block. */
1905 gsi_next (&si);
1907 /* If this tree could throw an exception, there are two
1908 cases where we need to add abnormal edge(s): the
1909 tree wasn't in a region and there is a "current
1910 region" in the caller; or the original tree had
1911 EH edges. In both cases split the block after the tree,
1912 and add abnormal edge(s) as needed; we need both
1913 those from the callee and the caller.
1914 We check whether the copy can throw, because the const
1915 propagation can change an INDIRECT_REF which throws
1916 into a COMPONENT_REF which doesn't. If the copy
1917 can throw, the original could also throw. */
1918 can_throw = stmt_can_throw_internal (copy_stmt);
1919 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1921 if (can_throw || nonlocal_goto)
1923 if (!gsi_end_p (si))
1925 while (!gsi_end_p (si) && is_gimple_debug (gsi_stmt (si)))
1926 gsi_next (&si);
1927 if (gsi_end_p (si))
1928 need_debug_cleanup = true;
1930 if (!gsi_end_p (si))
1931 /* Note that bb's predecessor edges aren't necessarily
1932 right at this point; split_block doesn't care. */
1934 edge e = split_block (new_bb, copy_stmt);
1936 new_bb = e->dest;
1937 new_bb->aux = e->src->aux;
1938 si = gsi_start_bb (new_bb);
1942 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
1943 make_eh_dispatch_edges (copy_stmt);
1944 else if (can_throw)
1945 make_eh_edges (copy_stmt);
1947 if (nonlocal_goto)
1948 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1950 if ((can_throw || nonlocal_goto)
1951 && gimple_in_ssa_p (cfun))
1952 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1953 can_throw, nonlocal_goto);
1955 return need_debug_cleanup;
1958 /* Copy the PHIs. All blocks and edges are copied, some blocks
1959 was possibly split and new outgoing EH edges inserted.
1960 BB points to the block of original function and AUX pointers links
1961 the original and newly copied blocks. */
1963 static void
1964 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1966 basic_block const new_bb = (basic_block) bb->aux;
1967 edge_iterator ei;
1968 gimple phi;
1969 gimple_stmt_iterator si;
1971 for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1973 tree res, new_res;
1974 gimple new_phi;
1975 edge new_edge;
1977 phi = gsi_stmt (si);
1978 res = PHI_RESULT (phi);
1979 new_res = res;
1980 if (is_gimple_reg (res))
1982 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1983 SSA_NAME_DEF_STMT (new_res)
1984 = new_phi = create_phi_node (new_res, new_bb);
1985 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1987 edge old_edge = find_edge ((basic_block) new_edge->src->aux, bb);
1988 tree arg;
1989 tree new_arg;
1990 tree block = id->block;
1991 edge_iterator ei2;
1993 /* When doing partial cloning, we allow PHIs on the entry block
1994 as long as all the arguments are the same. Find any input
1995 edge to see argument to copy. */
1996 if (!old_edge)
1997 FOR_EACH_EDGE (old_edge, ei2, bb->preds)
1998 if (!old_edge->src->aux)
1999 break;
2001 arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
2002 new_arg = arg;
2003 id->block = NULL_TREE;
2004 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
2005 id->block = block;
2006 gcc_assert (new_arg);
2007 /* With return slot optimization we can end up with
2008 non-gimple (foo *)&this->m, fix that here. */
2009 if (TREE_CODE (new_arg) != SSA_NAME
2010 && TREE_CODE (new_arg) != FUNCTION_DECL
2011 && !is_gimple_val (new_arg))
2013 gimple_seq stmts = NULL;
2014 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
2015 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
2017 add_phi_arg (new_phi, new_arg, new_edge,
2018 gimple_phi_arg_location_from_edge (phi, old_edge));
2025 /* Wrapper for remap_decl so it can be used as a callback. */
2027 static tree
2028 remap_decl_1 (tree decl, void *data)
2030 return remap_decl (decl, (copy_body_data *) data);
2033 /* Build struct function and associated datastructures for the new clone
2034 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
2036 static void
2037 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
2039 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2040 gcov_type count_scale;
2042 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
2043 count_scale = (REG_BR_PROB_BASE * count
2044 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
2045 else
2046 count_scale = REG_BR_PROB_BASE;
2048 /* Register specific tree functions. */
2049 gimple_register_cfg_hooks ();
2051 /* Get clean struct function. */
2052 push_struct_function (new_fndecl);
2054 /* We will rebuild these, so just sanity check that they are empty. */
2055 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
2056 gcc_assert (cfun->local_decls == NULL);
2057 gcc_assert (cfun->cfg == NULL);
2058 gcc_assert (cfun->decl == new_fndecl);
2060 /* Copy items we preserve during cloning. */
2061 cfun->static_chain_decl = src_cfun->static_chain_decl;
2062 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
2063 cfun->function_end_locus = src_cfun->function_end_locus;
2064 cfun->curr_properties = src_cfun->curr_properties;
2065 cfun->last_verified = src_cfun->last_verified;
2066 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
2067 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
2068 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
2069 cfun->stdarg = src_cfun->stdarg;
2070 cfun->dont_save_pending_sizes_p = src_cfun->dont_save_pending_sizes_p;
2071 cfun->after_inlining = src_cfun->after_inlining;
2072 cfun->can_throw_non_call_exceptions
2073 = src_cfun->can_throw_non_call_exceptions;
2074 cfun->returns_struct = src_cfun->returns_struct;
2075 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
2076 cfun->after_tree_profile = src_cfun->after_tree_profile;
2078 init_empty_tree_cfg ();
2080 profile_status_for_function (cfun) = profile_status_for_function (src_cfun);
2081 ENTRY_BLOCK_PTR->count =
2082 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2083 REG_BR_PROB_BASE);
2084 ENTRY_BLOCK_PTR->frequency
2085 = ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2086 EXIT_BLOCK_PTR->count =
2087 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
2088 REG_BR_PROB_BASE);
2089 EXIT_BLOCK_PTR->frequency =
2090 EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
2091 if (src_cfun->eh)
2092 init_eh_for_function ();
2094 if (src_cfun->gimple_df)
2096 init_tree_ssa (cfun);
2097 cfun->gimple_df->in_ssa_p = true;
2098 init_ssa_operands ();
2100 pop_cfun ();
2103 /* Helper function for copy_cfg_body. Move debug stmts from the end
2104 of NEW_BB to the beginning of successor basic blocks when needed. If the
2105 successor has multiple predecessors, reset them, otherwise keep
2106 their value. */
2108 static void
2109 maybe_move_debug_stmts_to_successors (copy_body_data *id, basic_block new_bb)
2111 edge e;
2112 edge_iterator ei;
2113 gimple_stmt_iterator si = gsi_last_nondebug_bb (new_bb);
2115 if (gsi_end_p (si)
2116 || gsi_one_before_end_p (si)
2117 || !(stmt_can_throw_internal (gsi_stmt (si))
2118 || stmt_can_make_abnormal_goto (gsi_stmt (si))))
2119 return;
2121 FOR_EACH_EDGE (e, ei, new_bb->succs)
2123 gimple_stmt_iterator ssi = gsi_last_bb (new_bb);
2124 gimple_stmt_iterator dsi = gsi_after_labels (e->dest);
2125 while (is_gimple_debug (gsi_stmt (ssi)))
2127 gimple stmt = gsi_stmt (ssi), new_stmt;
2128 tree var;
2129 tree value;
2131 /* For the last edge move the debug stmts instead of copying
2132 them. */
2133 if (ei_one_before_end_p (ei))
2135 si = ssi;
2136 gsi_prev (&ssi);
2137 if (!single_pred_p (e->dest))
2138 gimple_debug_bind_reset_value (stmt);
2139 gsi_remove (&si, false);
2140 gsi_insert_before (&dsi, stmt, GSI_SAME_STMT);
2141 continue;
2144 var = gimple_debug_bind_get_var (stmt);
2145 if (single_pred_p (e->dest))
2147 value = gimple_debug_bind_get_value (stmt);
2148 value = unshare_expr (value);
2150 else
2151 value = NULL_TREE;
2152 new_stmt = gimple_build_debug_bind (var, value, stmt);
2153 gsi_insert_before (&dsi, new_stmt, GSI_SAME_STMT);
2154 VEC_safe_push (gimple, heap, id->debug_stmts, new_stmt);
2155 gsi_prev (&ssi);
2160 /* Make a copy of the body of FN so that it can be inserted inline in
2161 another function. Walks FN via CFG, returns new fndecl. */
2163 static tree
2164 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
2165 basic_block entry_block_map, basic_block exit_block_map,
2166 bitmap blocks_to_copy, basic_block new_entry)
2168 tree callee_fndecl = id->src_fn;
2169 /* Original cfun for the callee, doesn't change. */
2170 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2171 struct function *cfun_to_copy;
2172 basic_block bb;
2173 tree new_fndecl = NULL;
2174 bool need_debug_cleanup = false;
2175 gcov_type count_scale;
2176 int last;
2177 int incoming_frequency = 0;
2178 gcov_type incoming_count = 0;
2180 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
2181 count_scale = (REG_BR_PROB_BASE * count
2182 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
2183 else
2184 count_scale = REG_BR_PROB_BASE;
2186 /* Register specific tree functions. */
2187 gimple_register_cfg_hooks ();
2189 /* If we are inlining just region of the function, make sure to connect new entry
2190 to ENTRY_BLOCK_PTR. Since new entry can be part of loop, we must compute
2191 frequency and probability of ENTRY_BLOCK_PTR based on the frequencies and
2192 probabilities of edges incoming from nonduplicated region. */
2193 if (new_entry)
2195 edge e;
2196 edge_iterator ei;
2198 FOR_EACH_EDGE (e, ei, new_entry->preds)
2199 if (!e->src->aux)
2201 incoming_frequency += EDGE_FREQUENCY (e);
2202 incoming_count += e->count;
2204 incoming_count = incoming_count * count_scale / REG_BR_PROB_BASE;
2205 incoming_frequency
2206 = incoming_frequency * frequency_scale / REG_BR_PROB_BASE;
2207 ENTRY_BLOCK_PTR->count = incoming_count;
2208 ENTRY_BLOCK_PTR->frequency = incoming_frequency;
2211 /* Must have a CFG here at this point. */
2212 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
2213 (DECL_STRUCT_FUNCTION (callee_fndecl)));
2215 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
2217 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
2218 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
2219 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2220 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
2222 /* Duplicate any exception-handling regions. */
2223 if (cfun->eh)
2224 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
2225 remap_decl_1, id);
2227 /* Use aux pointers to map the original blocks to copy. */
2228 FOR_EACH_BB_FN (bb, cfun_to_copy)
2229 if (!blocks_to_copy || bitmap_bit_p (blocks_to_copy, bb->index))
2231 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
2232 bb->aux = new_bb;
2233 new_bb->aux = bb;
2236 last = last_basic_block;
2238 /* Now that we've duplicated the blocks, duplicate their edges. */
2239 FOR_ALL_BB_FN (bb, cfun_to_copy)
2240 if (!blocks_to_copy
2241 || (bb->index > 0 && bitmap_bit_p (blocks_to_copy, bb->index)))
2242 need_debug_cleanup |= copy_edges_for_bb (bb, count_scale, exit_block_map);
2244 if (new_entry)
2246 edge e = make_edge (entry_block_map, (basic_block)new_entry->aux, EDGE_FALLTHRU);
2247 e->probability = REG_BR_PROB_BASE;
2248 e->count = incoming_count;
2251 if (gimple_in_ssa_p (cfun))
2252 FOR_ALL_BB_FN (bb, cfun_to_copy)
2253 if (!blocks_to_copy
2254 || (bb->index > 0 && bitmap_bit_p (blocks_to_copy, bb->index)))
2255 copy_phis_for_bb (bb, id);
2257 FOR_ALL_BB_FN (bb, cfun_to_copy)
2258 if (bb->aux)
2260 if (need_debug_cleanup
2261 && bb->index != ENTRY_BLOCK
2262 && bb->index != EXIT_BLOCK)
2263 maybe_move_debug_stmts_to_successors (id, (basic_block) bb->aux);
2264 ((basic_block)bb->aux)->aux = NULL;
2265 bb->aux = NULL;
2268 /* Zero out AUX fields of newly created block during EH edge
2269 insertion. */
2270 for (; last < last_basic_block; last++)
2272 if (need_debug_cleanup)
2273 maybe_move_debug_stmts_to_successors (id, BASIC_BLOCK (last));
2274 BASIC_BLOCK (last)->aux = NULL;
2276 entry_block_map->aux = NULL;
2277 exit_block_map->aux = NULL;
2279 if (id->eh_map)
2281 pointer_map_destroy (id->eh_map);
2282 id->eh_map = NULL;
2285 return new_fndecl;
2288 /* Copy the debug STMT using ID. We deal with these statements in a
2289 special way: if any variable in their VALUE expression wasn't
2290 remapped yet, we won't remap it, because that would get decl uids
2291 out of sync, causing codegen differences between -g and -g0. If
2292 this arises, we drop the VALUE expression altogether. */
2294 static void
2295 copy_debug_stmt (gimple stmt, copy_body_data *id)
2297 tree t, *n;
2298 struct walk_stmt_info wi;
2300 t = id->block;
2301 if (gimple_block (stmt))
2303 tree *n;
2304 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (stmt));
2305 if (n)
2306 t = *n;
2308 gimple_set_block (stmt, t);
2310 /* Remap all the operands in COPY. */
2311 memset (&wi, 0, sizeof (wi));
2312 wi.info = id;
2314 processing_debug_stmt = 1;
2316 t = gimple_debug_bind_get_var (stmt);
2318 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2319 && (n = (tree *) pointer_map_contains (id->debug_map, t)))
2321 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2322 t = *n;
2324 else if (TREE_CODE (t) == VAR_DECL
2325 && !TREE_STATIC (t)
2326 && gimple_in_ssa_p (cfun)
2327 && !pointer_map_contains (id->decl_map, t)
2328 && !var_ann (t))
2329 /* T is a non-localized variable. */;
2330 else
2331 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2333 gimple_debug_bind_set_var (stmt, t);
2335 if (gimple_debug_bind_has_value_p (stmt))
2336 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2337 remap_gimple_op_r, &wi, NULL);
2339 /* Punt if any decl couldn't be remapped. */
2340 if (processing_debug_stmt < 0)
2341 gimple_debug_bind_reset_value (stmt);
2343 processing_debug_stmt = 0;
2345 update_stmt (stmt);
2346 if (gimple_in_ssa_p (cfun))
2347 mark_symbols_for_renaming (stmt);
2350 /* Process deferred debug stmts. In order to give values better odds
2351 of being successfully remapped, we delay the processing of debug
2352 stmts until all other stmts that might require remapping are
2353 processed. */
2355 static void
2356 copy_debug_stmts (copy_body_data *id)
2358 size_t i;
2359 gimple stmt;
2361 if (!id->debug_stmts)
2362 return;
2364 for (i = 0; VEC_iterate (gimple, id->debug_stmts, i, stmt); i++)
2365 copy_debug_stmt (stmt, id);
2367 VEC_free (gimple, heap, id->debug_stmts);
2370 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2371 another function. */
2373 static tree
2374 copy_tree_body (copy_body_data *id)
2376 tree fndecl = id->src_fn;
2377 tree body = DECL_SAVED_TREE (fndecl);
2379 walk_tree (&body, copy_tree_body_r, id, NULL);
2381 return body;
2384 /* Make a copy of the body of FN so that it can be inserted inline in
2385 another function. */
2387 static tree
2388 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
2389 basic_block entry_block_map, basic_block exit_block_map,
2390 bitmap blocks_to_copy, basic_block new_entry)
2392 tree fndecl = id->src_fn;
2393 tree body;
2395 /* If this body has a CFG, walk CFG and copy. */
2396 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
2397 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map,
2398 blocks_to_copy, new_entry);
2399 copy_debug_stmts (id);
2401 return body;
2404 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
2405 defined in function FN, or of a data member thereof. */
2407 static bool
2408 self_inlining_addr_expr (tree value, tree fn)
2410 tree var;
2412 if (TREE_CODE (value) != ADDR_EXPR)
2413 return false;
2415 var = get_base_address (TREE_OPERAND (value, 0));
2417 return var && auto_var_in_fn_p (var, fn);
2420 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2421 lexical block and line number information from base_stmt, if given,
2422 or from the last stmt of the block otherwise. */
2424 static gimple
2425 insert_init_debug_bind (copy_body_data *id,
2426 basic_block bb, tree var, tree value,
2427 gimple base_stmt)
2429 gimple note;
2430 gimple_stmt_iterator gsi;
2431 tree tracked_var;
2433 if (!gimple_in_ssa_p (id->src_cfun))
2434 return NULL;
2436 if (!MAY_HAVE_DEBUG_STMTS)
2437 return NULL;
2439 tracked_var = target_for_debug_bind (var);
2440 if (!tracked_var)
2441 return NULL;
2443 if (bb)
2445 gsi = gsi_last_bb (bb);
2446 if (!base_stmt && !gsi_end_p (gsi))
2447 base_stmt = gsi_stmt (gsi);
2450 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
2452 if (bb)
2454 if (!gsi_end_p (gsi))
2455 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
2456 else
2457 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
2460 return note;
2463 static void
2464 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
2466 /* If VAR represents a zero-sized variable, it's possible that the
2467 assignment statement may result in no gimple statements. */
2468 if (init_stmt)
2470 gimple_stmt_iterator si = gsi_last_bb (bb);
2472 /* We can end up with init statements that store to a non-register
2473 from a rhs with a conversion. Handle that here by forcing the
2474 rhs into a temporary. gimple_regimplify_operands is not
2475 prepared to do this for us. */
2476 if (!is_gimple_debug (init_stmt)
2477 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
2478 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2479 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2481 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2482 gimple_expr_type (init_stmt),
2483 gimple_assign_rhs1 (init_stmt));
2484 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
2485 GSI_NEW_STMT);
2486 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
2487 gimple_assign_set_rhs1 (init_stmt, rhs);
2489 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2490 gimple_regimplify_operands (init_stmt, &si);
2491 mark_symbols_for_renaming (init_stmt);
2493 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2495 tree var, def = gimple_assign_lhs (init_stmt);
2497 if (TREE_CODE (def) == SSA_NAME)
2498 var = SSA_NAME_VAR (def);
2499 else
2500 var = def;
2502 insert_init_debug_bind (id, bb, var, def, init_stmt);
2507 /* Initialize parameter P with VALUE. If needed, produce init statement
2508 at the end of BB. When BB is NULL, we return init statement to be
2509 output later. */
2510 static gimple
2511 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2512 basic_block bb, tree *vars)
2514 gimple init_stmt = NULL;
2515 tree var;
2516 tree rhs = value;
2517 tree def = (gimple_in_ssa_p (cfun)
2518 ? gimple_default_def (id->src_cfun, p) : NULL);
2520 if (value
2521 && value != error_mark_node
2522 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
2524 if (fold_convertible_p (TREE_TYPE (p), value))
2525 rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
2526 else
2527 /* ??? For valid (GIMPLE) programs we should not end up here.
2528 Still if something has gone wrong and we end up with truly
2529 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
2530 to not leak invalid GIMPLE to the following passes. */
2531 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2534 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2535 here since the type of this decl must be visible to the calling
2536 function. */
2537 var = copy_decl_to_var (p, id);
2539 /* We're actually using the newly-created var. */
2540 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2542 get_var_ann (var);
2543 add_referenced_var (var);
2546 /* Declare this new variable. */
2547 TREE_CHAIN (var) = *vars;
2548 *vars = var;
2550 /* Make gimplifier happy about this variable. */
2551 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2553 /* If the parameter is never assigned to, has no SSA_NAMEs created,
2554 we would not need to create a new variable here at all, if it
2555 weren't for debug info. Still, we can just use the argument
2556 value. */
2557 if (TREE_READONLY (p)
2558 && !TREE_ADDRESSABLE (p)
2559 && value && !TREE_SIDE_EFFECTS (value)
2560 && !def)
2562 /* We may produce non-gimple trees by adding NOPs or introduce
2563 invalid sharing when operand is not really constant.
2564 It is not big deal to prohibit constant propagation here as
2565 we will constant propagate in DOM1 pass anyway. */
2566 if (is_gimple_min_invariant (value)
2567 && useless_type_conversion_p (TREE_TYPE (p),
2568 TREE_TYPE (value))
2569 /* We have to be very careful about ADDR_EXPR. Make sure
2570 the base variable isn't a local variable of the inlined
2571 function, e.g., when doing recursive inlining, direct or
2572 mutually-recursive or whatever, which is why we don't
2573 just test whether fn == current_function_decl. */
2574 && ! self_inlining_addr_expr (value, fn))
2576 insert_decl_map (id, p, value);
2577 insert_debug_decl_map (id, p, var);
2578 return insert_init_debug_bind (id, bb, var, value, NULL);
2582 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2583 that way, when the PARM_DECL is encountered, it will be
2584 automatically replaced by the VAR_DECL. */
2585 insert_decl_map (id, p, var);
2587 /* Even if P was TREE_READONLY, the new VAR should not be.
2588 In the original code, we would have constructed a
2589 temporary, and then the function body would have never
2590 changed the value of P. However, now, we will be
2591 constructing VAR directly. The constructor body may
2592 change its value multiple times as it is being
2593 constructed. Therefore, it must not be TREE_READONLY;
2594 the back-end assumes that TREE_READONLY variable is
2595 assigned to only once. */
2596 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2597 TREE_READONLY (var) = 0;
2599 /* If there is no setup required and we are in SSA, take the easy route
2600 replacing all SSA names representing the function parameter by the
2601 SSA name passed to function.
2603 We need to construct map for the variable anyway as it might be used
2604 in different SSA names when parameter is set in function.
2606 Do replacement at -O0 for const arguments replaced by constant.
2607 This is important for builtin_constant_p and other construct requiring
2608 constant argument to be visible in inlined function body. */
2609 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2610 && (optimize
2611 || (TREE_READONLY (p)
2612 && is_gimple_min_invariant (rhs)))
2613 && (TREE_CODE (rhs) == SSA_NAME
2614 || is_gimple_min_invariant (rhs))
2615 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2617 insert_decl_map (id, def, rhs);
2618 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2621 /* If the value of argument is never used, don't care about initializing
2622 it. */
2623 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2625 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2626 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2629 /* Initialize this VAR_DECL from the equivalent argument. Convert
2630 the argument to the proper type in case it was promoted. */
2631 if (value)
2633 if (rhs == error_mark_node)
2635 insert_decl_map (id, p, var);
2636 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2639 STRIP_USELESS_TYPE_CONVERSION (rhs);
2641 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2642 keep our trees in gimple form. */
2643 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2645 def = remap_ssa_name (def, id);
2646 init_stmt = gimple_build_assign (def, rhs);
2647 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2648 set_default_def (var, NULL);
2650 else
2651 init_stmt = gimple_build_assign (var, rhs);
2653 if (bb && init_stmt)
2654 insert_init_stmt (id, bb, init_stmt);
2656 return init_stmt;
2659 /* Generate code to initialize the parameters of the function at the
2660 top of the stack in ID from the GIMPLE_CALL STMT. */
2662 static void
2663 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2664 tree fn, basic_block bb)
2666 tree parms;
2667 size_t i;
2668 tree p;
2669 tree vars = NULL_TREE;
2670 tree static_chain = gimple_call_chain (stmt);
2672 /* Figure out what the parameters are. */
2673 parms = DECL_ARGUMENTS (fn);
2675 /* Loop through the parameter declarations, replacing each with an
2676 equivalent VAR_DECL, appropriately initialized. */
2677 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2679 tree val;
2680 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2681 setup_one_parameter (id, p, val, fn, bb, &vars);
2683 /* After remapping parameters remap their types. This has to be done
2684 in a second loop over all parameters to appropriately remap
2685 variable sized arrays when the size is specified in a
2686 parameter following the array. */
2687 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2689 tree *varp = (tree *) pointer_map_contains (id->decl_map, p);
2690 if (varp
2691 && TREE_CODE (*varp) == VAR_DECL)
2693 tree def = (gimple_in_ssa_p (cfun)
2694 ? gimple_default_def (id->src_cfun, p) : NULL);
2695 TREE_TYPE (*varp) = remap_type (TREE_TYPE (*varp), id);
2696 /* Also remap the default definition if it was remapped
2697 to the default definition of the parameter replacement
2698 by the parameter setup. */
2699 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2701 tree *defp = (tree *) pointer_map_contains (id->decl_map, def);
2702 if (defp
2703 && TREE_CODE (*defp) == SSA_NAME
2704 && SSA_NAME_VAR (*defp) == *varp)
2705 TREE_TYPE (*defp) = TREE_TYPE (*varp);
2710 /* Initialize the static chain. */
2711 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2712 gcc_assert (fn != current_function_decl);
2713 if (p)
2715 /* No static chain? Seems like a bug in tree-nested.c. */
2716 gcc_assert (static_chain);
2718 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2721 declare_inline_vars (id->block, vars);
2725 /* Declare a return variable to replace the RESULT_DECL for the
2726 function we are calling. An appropriate DECL_STMT is returned.
2727 The USE_STMT is filled to contain a use of the declaration to
2728 indicate the return value of the function.
2730 RETURN_SLOT, if non-null is place where to store the result. It
2731 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2732 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2734 The return value is a (possibly null) value that holds the result
2735 as seen by the caller. */
2737 static tree
2738 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest)
2740 tree callee = id->src_fn;
2741 tree caller = id->dst_fn;
2742 tree result = DECL_RESULT (callee);
2743 tree callee_type = TREE_TYPE (result);
2744 tree caller_type;
2745 tree var, use;
2747 /* Handle type-mismatches in the function declaration return type
2748 vs. the call expression. */
2749 if (modify_dest)
2750 caller_type = TREE_TYPE (modify_dest);
2751 else
2752 caller_type = TREE_TYPE (TREE_TYPE (callee));
2754 /* We don't need to do anything for functions that don't return
2755 anything. */
2756 if (!result || VOID_TYPE_P (callee_type))
2757 return NULL_TREE;
2759 /* If there was a return slot, then the return value is the
2760 dereferenced address of that object. */
2761 if (return_slot)
2763 /* The front end shouldn't have used both return_slot and
2764 a modify expression. */
2765 gcc_assert (!modify_dest);
2766 if (DECL_BY_REFERENCE (result))
2768 tree return_slot_addr = build_fold_addr_expr (return_slot);
2769 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2771 /* We are going to construct *&return_slot and we can't do that
2772 for variables believed to be not addressable.
2774 FIXME: This check possibly can match, because values returned
2775 via return slot optimization are not believed to have address
2776 taken by alias analysis. */
2777 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2778 var = return_slot_addr;
2780 else
2782 var = return_slot;
2783 gcc_assert (TREE_CODE (var) != SSA_NAME);
2784 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2786 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2787 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2788 && !DECL_GIMPLE_REG_P (result)
2789 && DECL_P (var))
2790 DECL_GIMPLE_REG_P (var) = 0;
2791 use = NULL;
2792 goto done;
2795 /* All types requiring non-trivial constructors should have been handled. */
2796 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2798 /* Attempt to avoid creating a new temporary variable. */
2799 if (modify_dest
2800 && TREE_CODE (modify_dest) != SSA_NAME)
2802 bool use_it = false;
2804 /* We can't use MODIFY_DEST if there's type promotion involved. */
2805 if (!useless_type_conversion_p (callee_type, caller_type))
2806 use_it = false;
2808 /* ??? If we're assigning to a variable sized type, then we must
2809 reuse the destination variable, because we've no good way to
2810 create variable sized temporaries at this point. */
2811 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2812 use_it = true;
2814 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2815 reuse it as the result of the call directly. Don't do this if
2816 it would promote MODIFY_DEST to addressable. */
2817 else if (TREE_ADDRESSABLE (result))
2818 use_it = false;
2819 else
2821 tree base_m = get_base_address (modify_dest);
2823 /* If the base isn't a decl, then it's a pointer, and we don't
2824 know where that's going to go. */
2825 if (!DECL_P (base_m))
2826 use_it = false;
2827 else if (is_global_var (base_m))
2828 use_it = false;
2829 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2830 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2831 && !DECL_GIMPLE_REG_P (result)
2832 && DECL_GIMPLE_REG_P (base_m))
2833 use_it = false;
2834 else if (!TREE_ADDRESSABLE (base_m))
2835 use_it = true;
2838 if (use_it)
2840 var = modify_dest;
2841 use = NULL;
2842 goto done;
2846 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2848 var = copy_result_decl_to_var (result, id);
2849 if (gimple_in_ssa_p (cfun))
2851 get_var_ann (var);
2852 add_referenced_var (var);
2855 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2856 DECL_STRUCT_FUNCTION (caller)->local_decls
2857 = tree_cons (NULL_TREE, var,
2858 DECL_STRUCT_FUNCTION (caller)->local_decls);
2860 /* Do not have the rest of GCC warn about this variable as it should
2861 not be visible to the user. */
2862 TREE_NO_WARNING (var) = 1;
2864 declare_inline_vars (id->block, var);
2866 /* Build the use expr. If the return type of the function was
2867 promoted, convert it back to the expected type. */
2868 use = var;
2869 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2870 use = fold_convert (caller_type, var);
2872 STRIP_USELESS_TYPE_CONVERSION (use);
2874 if (DECL_BY_REFERENCE (result))
2876 TREE_ADDRESSABLE (var) = 1;
2877 var = build_fold_addr_expr (var);
2880 done:
2881 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2882 way, when the RESULT_DECL is encountered, it will be
2883 automatically replaced by the VAR_DECL. */
2884 insert_decl_map (id, result, var);
2886 /* Remember this so we can ignore it in remap_decls. */
2887 id->retvar = var;
2889 return use;
2892 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
2893 to a local label. */
2895 static tree
2896 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
2898 tree node = *nodep;
2899 tree fn = (tree) fnp;
2901 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2902 return node;
2904 if (TYPE_P (node))
2905 *walk_subtrees = 0;
2907 return NULL_TREE;
2910 /* Determine if the function can be copied. If so return NULL. If
2911 not return a string describng the reason for failure. */
2913 static const char *
2914 copy_forbidden (struct function *fun, tree fndecl)
2916 const char *reason = fun->cannot_be_copied_reason;
2917 tree step;
2919 /* Only examine the function once. */
2920 if (fun->cannot_be_copied_set)
2921 return reason;
2923 /* We cannot copy a function that receives a non-local goto
2924 because we cannot remap the destination label used in the
2925 function that is performing the non-local goto. */
2926 /* ??? Actually, this should be possible, if we work at it.
2927 No doubt there's just a handful of places that simply
2928 assume it doesn't happen and don't substitute properly. */
2929 if (fun->has_nonlocal_label)
2931 reason = G_("function %q+F can never be copied "
2932 "because it receives a non-local goto");
2933 goto fail;
2936 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2938 tree decl = TREE_VALUE (step);
2940 if (TREE_CODE (decl) == VAR_DECL
2941 && TREE_STATIC (decl)
2942 && !DECL_EXTERNAL (decl)
2943 && DECL_INITIAL (decl)
2944 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
2945 has_label_address_in_static_1,
2946 fndecl))
2948 reason = G_("function %q+F can never be copied because it saves "
2949 "address of local label in a static variable");
2950 goto fail;
2954 fail:
2955 fun->cannot_be_copied_reason = reason;
2956 fun->cannot_be_copied_set = true;
2957 return reason;
2961 static const char *inline_forbidden_reason;
2963 /* A callback for walk_gimple_seq to handle statements. Returns non-null
2964 iff a function can not be inlined. Also sets the reason why. */
2966 static tree
2967 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2968 struct walk_stmt_info *wip)
2970 tree fn = (tree) wip->info;
2971 tree t;
2972 gimple stmt = gsi_stmt (*gsi);
2974 switch (gimple_code (stmt))
2976 case GIMPLE_CALL:
2977 /* Refuse to inline alloca call unless user explicitly forced so as
2978 this may change program's memory overhead drastically when the
2979 function using alloca is called in loop. In GCC present in
2980 SPEC2000 inlining into schedule_block cause it to require 2GB of
2981 RAM instead of 256MB. */
2982 if (gimple_alloca_call_p (stmt)
2983 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2985 inline_forbidden_reason
2986 = G_("function %q+F can never be inlined because it uses "
2987 "alloca (override using the always_inline attribute)");
2988 *handled_ops_p = true;
2989 return fn;
2992 t = gimple_call_fndecl (stmt);
2993 if (t == NULL_TREE)
2994 break;
2996 /* We cannot inline functions that call setjmp. */
2997 if (setjmp_call_p (t))
2999 inline_forbidden_reason
3000 = G_("function %q+F can never be inlined because it uses setjmp");
3001 *handled_ops_p = true;
3002 return t;
3005 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
3006 switch (DECL_FUNCTION_CODE (t))
3008 /* We cannot inline functions that take a variable number of
3009 arguments. */
3010 case BUILT_IN_VA_START:
3011 case BUILT_IN_NEXT_ARG:
3012 case BUILT_IN_VA_END:
3013 inline_forbidden_reason
3014 = G_("function %q+F can never be inlined because it "
3015 "uses variable argument lists");
3016 *handled_ops_p = true;
3017 return t;
3019 case BUILT_IN_LONGJMP:
3020 /* We can't inline functions that call __builtin_longjmp at
3021 all. The non-local goto machinery really requires the
3022 destination be in a different function. If we allow the
3023 function calling __builtin_longjmp to be inlined into the
3024 function calling __builtin_setjmp, Things will Go Awry. */
3025 inline_forbidden_reason
3026 = G_("function %q+F can never be inlined because "
3027 "it uses setjmp-longjmp exception handling");
3028 *handled_ops_p = true;
3029 return t;
3031 case BUILT_IN_NONLOCAL_GOTO:
3032 /* Similarly. */
3033 inline_forbidden_reason
3034 = G_("function %q+F can never be inlined because "
3035 "it uses non-local goto");
3036 *handled_ops_p = true;
3037 return t;
3039 case BUILT_IN_RETURN:
3040 case BUILT_IN_APPLY_ARGS:
3041 /* If a __builtin_apply_args caller would be inlined,
3042 it would be saving arguments of the function it has
3043 been inlined into. Similarly __builtin_return would
3044 return from the function the inline has been inlined into. */
3045 inline_forbidden_reason
3046 = G_("function %q+F can never be inlined because "
3047 "it uses __builtin_return or __builtin_apply_args");
3048 *handled_ops_p = true;
3049 return t;
3051 default:
3052 break;
3054 break;
3056 case GIMPLE_GOTO:
3057 t = gimple_goto_dest (stmt);
3059 /* We will not inline a function which uses computed goto. The
3060 addresses of its local labels, which may be tucked into
3061 global storage, are of course not constant across
3062 instantiations, which causes unexpected behavior. */
3063 if (TREE_CODE (t) != LABEL_DECL)
3065 inline_forbidden_reason
3066 = G_("function %q+F can never be inlined "
3067 "because it contains a computed goto");
3068 *handled_ops_p = true;
3069 return t;
3071 break;
3073 default:
3074 break;
3077 *handled_ops_p = false;
3078 return NULL_TREE;
3081 /* Return true if FNDECL is a function that cannot be inlined into
3082 another one. */
3084 static bool
3085 inline_forbidden_p (tree fndecl)
3087 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
3088 struct walk_stmt_info wi;
3089 struct pointer_set_t *visited_nodes;
3090 basic_block bb;
3091 bool forbidden_p = false;
3093 /* First check for shared reasons not to copy the code. */
3094 inline_forbidden_reason = copy_forbidden (fun, fndecl);
3095 if (inline_forbidden_reason != NULL)
3096 return true;
3098 /* Next, walk the statements of the function looking for
3099 constraucts we can't handle, or are non-optimal for inlining. */
3100 visited_nodes = pointer_set_create ();
3101 memset (&wi, 0, sizeof (wi));
3102 wi.info = (void *) fndecl;
3103 wi.pset = visited_nodes;
3105 FOR_EACH_BB_FN (bb, fun)
3107 gimple ret;
3108 gimple_seq seq = bb_seq (bb);
3109 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
3110 forbidden_p = (ret != NULL);
3111 if (forbidden_p)
3112 break;
3115 pointer_set_destroy (visited_nodes);
3116 return forbidden_p;
3119 /* Return true if CALLEE cannot be inlined into CALLER. */
3121 static bool
3122 inline_forbidden_into_p (tree caller, tree callee)
3124 /* Don't inline if the functions have different EH personalities. */
3125 if (DECL_FUNCTION_PERSONALITY (caller)
3126 && DECL_FUNCTION_PERSONALITY (callee)
3127 && (DECL_FUNCTION_PERSONALITY (caller)
3128 != DECL_FUNCTION_PERSONALITY (callee)))
3129 return true;
3131 /* Don't inline if the callee can throw non-call exceptions but the
3132 caller cannot. */
3133 if (DECL_STRUCT_FUNCTION (callee)
3134 && DECL_STRUCT_FUNCTION (callee)->can_throw_non_call_exceptions
3135 && !(DECL_STRUCT_FUNCTION (caller)
3136 && DECL_STRUCT_FUNCTION (caller)->can_throw_non_call_exceptions))
3137 return true;
3139 return false;
3142 /* Returns nonzero if FN is a function that does not have any
3143 fundamental inline blocking properties. */
3145 bool
3146 tree_inlinable_function_p (tree fn)
3148 bool inlinable = true;
3149 bool do_warning;
3150 tree always_inline;
3152 /* If we've already decided this function shouldn't be inlined,
3153 there's no need to check again. */
3154 if (DECL_UNINLINABLE (fn))
3155 return false;
3157 /* We only warn for functions declared `inline' by the user. */
3158 do_warning = (warn_inline
3159 && DECL_DECLARED_INLINE_P (fn)
3160 && !DECL_NO_INLINE_WARNING_P (fn)
3161 && !DECL_IN_SYSTEM_HEADER (fn));
3163 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
3165 if (flag_no_inline
3166 && always_inline == NULL)
3168 if (do_warning)
3169 warning (OPT_Winline, "function %q+F can never be inlined because it "
3170 "is suppressed using -fno-inline", fn);
3171 inlinable = false;
3174 /* Don't auto-inline anything that might not be bound within
3175 this unit of translation. */
3176 else if (!DECL_DECLARED_INLINE_P (fn)
3177 && DECL_REPLACEABLE_P (fn))
3178 inlinable = false;
3180 else if (!function_attribute_inlinable_p (fn))
3182 if (do_warning)
3183 warning (OPT_Winline, "function %q+F can never be inlined because it "
3184 "uses attributes conflicting with inlining", fn);
3185 inlinable = false;
3188 else if (inline_forbidden_p (fn))
3190 /* See if we should warn about uninlinable functions. Previously,
3191 some of these warnings would be issued while trying to expand
3192 the function inline, but that would cause multiple warnings
3193 about functions that would for example call alloca. But since
3194 this a property of the function, just one warning is enough.
3195 As a bonus we can now give more details about the reason why a
3196 function is not inlinable. */
3197 if (always_inline)
3198 sorry (inline_forbidden_reason, fn);
3199 else if (do_warning)
3200 warning (OPT_Winline, inline_forbidden_reason, fn);
3202 inlinable = false;
3205 /* Squirrel away the result so that we don't have to check again. */
3206 DECL_UNINLINABLE (fn) = !inlinable;
3208 return inlinable;
3211 /* Estimate the cost of a memory move. Use machine dependent
3212 word size and take possible memcpy call into account. */
3215 estimate_move_cost (tree type)
3217 HOST_WIDE_INT size;
3219 gcc_assert (!VOID_TYPE_P (type));
3221 size = int_size_in_bytes (type);
3223 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
3224 /* Cost of a memcpy call, 3 arguments and the call. */
3225 return 4;
3226 else
3227 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
3230 /* Returns cost of operation CODE, according to WEIGHTS */
3232 static int
3233 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3234 tree op1 ATTRIBUTE_UNUSED, tree op2)
3236 switch (code)
3238 /* These are "free" conversions, or their presumed cost
3239 is folded into other operations. */
3240 case RANGE_EXPR:
3241 CASE_CONVERT:
3242 case COMPLEX_EXPR:
3243 case PAREN_EXPR:
3244 return 0;
3246 /* Assign cost of 1 to usual operations.
3247 ??? We may consider mapping RTL costs to this. */
3248 case COND_EXPR:
3249 case VEC_COND_EXPR:
3251 case PLUS_EXPR:
3252 case POINTER_PLUS_EXPR:
3253 case MINUS_EXPR:
3254 case MULT_EXPR:
3256 case ADDR_SPACE_CONVERT_EXPR:
3257 case FIXED_CONVERT_EXPR:
3258 case FIX_TRUNC_EXPR:
3260 case NEGATE_EXPR:
3261 case FLOAT_EXPR:
3262 case MIN_EXPR:
3263 case MAX_EXPR:
3264 case ABS_EXPR:
3266 case LSHIFT_EXPR:
3267 case RSHIFT_EXPR:
3268 case LROTATE_EXPR:
3269 case RROTATE_EXPR:
3270 case VEC_LSHIFT_EXPR:
3271 case VEC_RSHIFT_EXPR:
3273 case BIT_IOR_EXPR:
3274 case BIT_XOR_EXPR:
3275 case BIT_AND_EXPR:
3276 case BIT_NOT_EXPR:
3278 case TRUTH_ANDIF_EXPR:
3279 case TRUTH_ORIF_EXPR:
3280 case TRUTH_AND_EXPR:
3281 case TRUTH_OR_EXPR:
3282 case TRUTH_XOR_EXPR:
3283 case TRUTH_NOT_EXPR:
3285 case LT_EXPR:
3286 case LE_EXPR:
3287 case GT_EXPR:
3288 case GE_EXPR:
3289 case EQ_EXPR:
3290 case NE_EXPR:
3291 case ORDERED_EXPR:
3292 case UNORDERED_EXPR:
3294 case UNLT_EXPR:
3295 case UNLE_EXPR:
3296 case UNGT_EXPR:
3297 case UNGE_EXPR:
3298 case UNEQ_EXPR:
3299 case LTGT_EXPR:
3301 case CONJ_EXPR:
3303 case PREDECREMENT_EXPR:
3304 case PREINCREMENT_EXPR:
3305 case POSTDECREMENT_EXPR:
3306 case POSTINCREMENT_EXPR:
3308 case REALIGN_LOAD_EXPR:
3310 case REDUC_MAX_EXPR:
3311 case REDUC_MIN_EXPR:
3312 case REDUC_PLUS_EXPR:
3313 case WIDEN_SUM_EXPR:
3314 case WIDEN_MULT_EXPR:
3315 case DOT_PROD_EXPR:
3316 case WIDEN_MULT_PLUS_EXPR:
3317 case WIDEN_MULT_MINUS_EXPR:
3319 case VEC_WIDEN_MULT_HI_EXPR:
3320 case VEC_WIDEN_MULT_LO_EXPR:
3321 case VEC_UNPACK_HI_EXPR:
3322 case VEC_UNPACK_LO_EXPR:
3323 case VEC_UNPACK_FLOAT_HI_EXPR:
3324 case VEC_UNPACK_FLOAT_LO_EXPR:
3325 case VEC_PACK_TRUNC_EXPR:
3326 case VEC_PACK_SAT_EXPR:
3327 case VEC_PACK_FIX_TRUNC_EXPR:
3328 case VEC_EXTRACT_EVEN_EXPR:
3329 case VEC_EXTRACT_ODD_EXPR:
3330 case VEC_INTERLEAVE_HIGH_EXPR:
3331 case VEC_INTERLEAVE_LOW_EXPR:
3333 return 1;
3335 /* Few special cases of expensive operations. This is useful
3336 to avoid inlining on functions having too many of these. */
3337 case TRUNC_DIV_EXPR:
3338 case CEIL_DIV_EXPR:
3339 case FLOOR_DIV_EXPR:
3340 case ROUND_DIV_EXPR:
3341 case EXACT_DIV_EXPR:
3342 case TRUNC_MOD_EXPR:
3343 case CEIL_MOD_EXPR:
3344 case FLOOR_MOD_EXPR:
3345 case ROUND_MOD_EXPR:
3346 case RDIV_EXPR:
3347 if (TREE_CODE (op2) != INTEGER_CST)
3348 return weights->div_mod_cost;
3349 return 1;
3351 default:
3352 /* We expect a copy assignment with no operator. */
3353 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
3354 return 0;
3359 /* Estimate number of instructions that will be created by expanding
3360 the statements in the statement sequence STMTS.
3361 WEIGHTS contains weights attributed to various constructs. */
3363 static
3364 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
3366 int cost;
3367 gimple_stmt_iterator gsi;
3369 cost = 0;
3370 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
3371 cost += estimate_num_insns (gsi_stmt (gsi), weights);
3373 return cost;
3377 /* Estimate number of instructions that will be created by expanding STMT.
3378 WEIGHTS contains weights attributed to various constructs. */
3381 estimate_num_insns (gimple stmt, eni_weights *weights)
3383 unsigned cost, i;
3384 enum gimple_code code = gimple_code (stmt);
3385 tree lhs;
3386 tree rhs;
3388 switch (code)
3390 case GIMPLE_ASSIGN:
3391 /* Try to estimate the cost of assignments. We have three cases to
3392 deal with:
3393 1) Simple assignments to registers;
3394 2) Stores to things that must live in memory. This includes
3395 "normal" stores to scalars, but also assignments of large
3396 structures, or constructors of big arrays;
3398 Let us look at the first two cases, assuming we have "a = b + C":
3399 <GIMPLE_ASSIGN <var_decl "a">
3400 <plus_expr <var_decl "b"> <constant C>>
3401 If "a" is a GIMPLE register, the assignment to it is free on almost
3402 any target, because "a" usually ends up in a real register. Hence
3403 the only cost of this expression comes from the PLUS_EXPR, and we
3404 can ignore the GIMPLE_ASSIGN.
3405 If "a" is not a GIMPLE register, the assignment to "a" will most
3406 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
3407 of moving something into "a", which we compute using the function
3408 estimate_move_cost. */
3409 lhs = gimple_assign_lhs (stmt);
3410 rhs = gimple_assign_rhs1 (stmt);
3412 if (is_gimple_reg (lhs))
3413 cost = 0;
3414 else
3415 cost = estimate_move_cost (TREE_TYPE (lhs));
3417 if (!is_gimple_reg (rhs) && !is_gimple_min_invariant (rhs))
3418 cost += estimate_move_cost (TREE_TYPE (rhs));
3420 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3421 gimple_assign_rhs1 (stmt),
3422 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3423 == GIMPLE_BINARY_RHS
3424 ? gimple_assign_rhs2 (stmt) : NULL);
3425 break;
3427 case GIMPLE_COND:
3428 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3429 gimple_op (stmt, 0),
3430 gimple_op (stmt, 1));
3431 break;
3433 case GIMPLE_SWITCH:
3434 /* Take into account cost of the switch + guess 2 conditional jumps for
3435 each case label.
3437 TODO: once the switch expansion logic is sufficiently separated, we can
3438 do better job on estimating cost of the switch. */
3439 if (weights->time_based)
3440 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3441 else
3442 cost = gimple_switch_num_labels (stmt) * 2;
3443 break;
3445 case GIMPLE_CALL:
3447 tree decl = gimple_call_fndecl (stmt);
3448 tree addr = gimple_call_fn (stmt);
3449 tree funtype = TREE_TYPE (addr);
3450 bool stdarg = false;
3452 if (POINTER_TYPE_P (funtype))
3453 funtype = TREE_TYPE (funtype);
3455 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
3456 cost = weights->target_builtin_call_cost;
3457 else
3458 cost = weights->call_cost;
3460 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3461 switch (DECL_FUNCTION_CODE (decl))
3463 /* Builtins that expand to constants. */
3464 case BUILT_IN_CONSTANT_P:
3465 case BUILT_IN_EXPECT:
3466 case BUILT_IN_OBJECT_SIZE:
3467 case BUILT_IN_UNREACHABLE:
3468 /* Simple register moves or loads from stack. */
3469 case BUILT_IN_RETURN_ADDRESS:
3470 case BUILT_IN_EXTRACT_RETURN_ADDR:
3471 case BUILT_IN_FROB_RETURN_ADDR:
3472 case BUILT_IN_RETURN:
3473 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
3474 case BUILT_IN_FRAME_ADDRESS:
3475 case BUILT_IN_VA_END:
3476 case BUILT_IN_STACK_SAVE:
3477 case BUILT_IN_STACK_RESTORE:
3478 /* Exception state returns or moves registers around. */
3479 case BUILT_IN_EH_FILTER:
3480 case BUILT_IN_EH_POINTER:
3481 case BUILT_IN_EH_COPY_VALUES:
3482 return 0;
3484 /* builtins that are not expensive (that is they are most probably
3485 expanded inline into resonably simple code). */
3486 case BUILT_IN_ABS:
3487 case BUILT_IN_ALLOCA:
3488 case BUILT_IN_BSWAP32:
3489 case BUILT_IN_BSWAP64:
3490 case BUILT_IN_CLZ:
3491 case BUILT_IN_CLZIMAX:
3492 case BUILT_IN_CLZL:
3493 case BUILT_IN_CLZLL:
3494 case BUILT_IN_CTZ:
3495 case BUILT_IN_CTZIMAX:
3496 case BUILT_IN_CTZL:
3497 case BUILT_IN_CTZLL:
3498 case BUILT_IN_FFS:
3499 case BUILT_IN_FFSIMAX:
3500 case BUILT_IN_FFSL:
3501 case BUILT_IN_FFSLL:
3502 case BUILT_IN_IMAXABS:
3503 case BUILT_IN_FINITE:
3504 case BUILT_IN_FINITEF:
3505 case BUILT_IN_FINITEL:
3506 case BUILT_IN_FINITED32:
3507 case BUILT_IN_FINITED64:
3508 case BUILT_IN_FINITED128:
3509 case BUILT_IN_FPCLASSIFY:
3510 case BUILT_IN_ISFINITE:
3511 case BUILT_IN_ISINF_SIGN:
3512 case BUILT_IN_ISINF:
3513 case BUILT_IN_ISINFF:
3514 case BUILT_IN_ISINFL:
3515 case BUILT_IN_ISINFD32:
3516 case BUILT_IN_ISINFD64:
3517 case BUILT_IN_ISINFD128:
3518 case BUILT_IN_ISNAN:
3519 case BUILT_IN_ISNANF:
3520 case BUILT_IN_ISNANL:
3521 case BUILT_IN_ISNAND32:
3522 case BUILT_IN_ISNAND64:
3523 case BUILT_IN_ISNAND128:
3524 case BUILT_IN_ISNORMAL:
3525 case BUILT_IN_ISGREATER:
3526 case BUILT_IN_ISGREATEREQUAL:
3527 case BUILT_IN_ISLESS:
3528 case BUILT_IN_ISLESSEQUAL:
3529 case BUILT_IN_ISLESSGREATER:
3530 case BUILT_IN_ISUNORDERED:
3531 case BUILT_IN_VA_ARG_PACK:
3532 case BUILT_IN_VA_ARG_PACK_LEN:
3533 case BUILT_IN_VA_COPY:
3534 case BUILT_IN_TRAP:
3535 case BUILT_IN_SAVEREGS:
3536 case BUILT_IN_POPCOUNTL:
3537 case BUILT_IN_POPCOUNTLL:
3538 case BUILT_IN_POPCOUNTIMAX:
3539 case BUILT_IN_POPCOUNT:
3540 case BUILT_IN_PARITYL:
3541 case BUILT_IN_PARITYLL:
3542 case BUILT_IN_PARITYIMAX:
3543 case BUILT_IN_PARITY:
3544 case BUILT_IN_LABS:
3545 case BUILT_IN_LLABS:
3546 case BUILT_IN_PREFETCH:
3547 cost = weights->target_builtin_call_cost;
3548 break;
3550 default:
3551 break;
3554 if (decl)
3555 funtype = TREE_TYPE (decl);
3557 if (!VOID_TYPE_P (TREE_TYPE (funtype)))
3558 cost += estimate_move_cost (TREE_TYPE (funtype));
3560 if (funtype)
3561 stdarg = stdarg_p (funtype);
3563 /* Our cost must be kept in sync with
3564 cgraph_estimate_size_after_inlining that does use function
3565 declaration to figure out the arguments.
3567 For functions taking variable list of arguments we must
3568 look into call statement intself. This is safe because
3569 we will get only higher costs and in most cases we will
3570 not inline these anyway. */
3571 if (decl && DECL_ARGUMENTS (decl) && !stdarg)
3573 tree arg;
3574 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
3575 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3576 cost += estimate_move_cost (TREE_TYPE (arg));
3578 else if (funtype && prototype_p (funtype) && !stdarg)
3580 tree t;
3581 for (t = TYPE_ARG_TYPES (funtype); t && t != void_list_node;
3582 t = TREE_CHAIN (t))
3583 if (!VOID_TYPE_P (TREE_VALUE (t)))
3584 cost += estimate_move_cost (TREE_VALUE (t));
3586 else
3588 for (i = 0; i < gimple_call_num_args (stmt); i++)
3590 tree arg = gimple_call_arg (stmt, i);
3591 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3592 cost += estimate_move_cost (TREE_TYPE (arg));
3596 break;
3599 case GIMPLE_GOTO:
3600 case GIMPLE_LABEL:
3601 case GIMPLE_NOP:
3602 case GIMPLE_PHI:
3603 case GIMPLE_RETURN:
3604 case GIMPLE_PREDICT:
3605 case GIMPLE_DEBUG:
3606 return 0;
3608 case GIMPLE_ASM:
3609 return asm_str_count (gimple_asm_string (stmt));
3611 case GIMPLE_RESX:
3612 /* This is either going to be an external function call with one
3613 argument, or two register copy statements plus a goto. */
3614 return 2;
3616 case GIMPLE_EH_DISPATCH:
3617 /* ??? This is going to turn into a switch statement. Ideally
3618 we'd have a look at the eh region and estimate the number of
3619 edges involved. */
3620 return 10;
3622 case GIMPLE_BIND:
3623 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
3625 case GIMPLE_EH_FILTER:
3626 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
3628 case GIMPLE_CATCH:
3629 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
3631 case GIMPLE_TRY:
3632 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
3633 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
3635 /* OpenMP directives are generally very expensive. */
3637 case GIMPLE_OMP_RETURN:
3638 case GIMPLE_OMP_SECTIONS_SWITCH:
3639 case GIMPLE_OMP_ATOMIC_STORE:
3640 case GIMPLE_OMP_CONTINUE:
3641 /* ...except these, which are cheap. */
3642 return 0;
3644 case GIMPLE_OMP_ATOMIC_LOAD:
3645 return weights->omp_cost;
3647 case GIMPLE_OMP_FOR:
3648 return (weights->omp_cost
3649 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
3650 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
3652 case GIMPLE_OMP_PARALLEL:
3653 case GIMPLE_OMP_TASK:
3654 case GIMPLE_OMP_CRITICAL:
3655 case GIMPLE_OMP_MASTER:
3656 case GIMPLE_OMP_ORDERED:
3657 case GIMPLE_OMP_SECTION:
3658 case GIMPLE_OMP_SECTIONS:
3659 case GIMPLE_OMP_SINGLE:
3660 return (weights->omp_cost
3661 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
3663 default:
3664 gcc_unreachable ();
3667 return cost;
3670 /* Estimate number of instructions that will be created by expanding
3671 function FNDECL. WEIGHTS contains weights attributed to various
3672 constructs. */
3675 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
3677 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
3678 gimple_stmt_iterator bsi;
3679 basic_block bb;
3680 int n = 0;
3682 gcc_assert (my_function && my_function->cfg);
3683 FOR_EACH_BB_FN (bb, my_function)
3685 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3686 n += estimate_num_insns (gsi_stmt (bsi), weights);
3689 return n;
3693 /* Initializes weights used by estimate_num_insns. */
3695 void
3696 init_inline_once (void)
3698 eni_size_weights.call_cost = 1;
3699 eni_size_weights.target_builtin_call_cost = 1;
3700 eni_size_weights.div_mod_cost = 1;
3701 eni_size_weights.omp_cost = 40;
3702 eni_size_weights.time_based = false;
3704 /* Estimating time for call is difficult, since we have no idea what the
3705 called function does. In the current uses of eni_time_weights,
3706 underestimating the cost does less harm than overestimating it, so
3707 we choose a rather small value here. */
3708 eni_time_weights.call_cost = 10;
3709 eni_time_weights.target_builtin_call_cost = 10;
3710 eni_time_weights.div_mod_cost = 10;
3711 eni_time_weights.omp_cost = 40;
3712 eni_time_weights.time_based = true;
3715 /* Estimate the number of instructions in a gimple_seq. */
3718 count_insns_seq (gimple_seq seq, eni_weights *weights)
3720 gimple_stmt_iterator gsi;
3721 int n = 0;
3722 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3723 n += estimate_num_insns (gsi_stmt (gsi), weights);
3725 return n;
3729 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3731 static void
3732 prepend_lexical_block (tree current_block, tree new_block)
3734 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3735 BLOCK_SUBBLOCKS (current_block) = new_block;
3736 BLOCK_SUPERCONTEXT (new_block) = current_block;
3739 /* Fetch callee declaration from the call graph edge going from NODE and
3740 associated with STMR call statement. Return NULL_TREE if not found. */
3741 static tree
3742 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3744 struct cgraph_edge *cs;
3746 cs = cgraph_edge (node, stmt);
3747 if (cs && !cs->indirect_unknown_callee)
3748 return cs->callee->decl;
3750 return NULL_TREE;
3753 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3755 static bool
3756 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3758 tree use_retvar;
3759 tree fn;
3760 struct pointer_map_t *st, *dst;
3761 tree return_slot;
3762 tree modify_dest;
3763 location_t saved_location;
3764 struct cgraph_edge *cg_edge;
3765 cgraph_inline_failed_t reason;
3766 basic_block return_block;
3767 edge e;
3768 gimple_stmt_iterator gsi, stmt_gsi;
3769 bool successfully_inlined = FALSE;
3770 bool purge_dead_abnormal_edges;
3771 tree t_step;
3772 tree var;
3774 /* Set input_location here so we get the right instantiation context
3775 if we call instantiate_decl from inlinable_function_p. */
3776 saved_location = input_location;
3777 if (gimple_has_location (stmt))
3778 input_location = gimple_location (stmt);
3780 /* From here on, we're only interested in CALL_EXPRs. */
3781 if (gimple_code (stmt) != GIMPLE_CALL)
3782 goto egress;
3784 /* First, see if we can figure out what function is being called.
3785 If we cannot, then there is no hope of inlining the function. */
3786 fn = gimple_call_fndecl (stmt);
3787 if (!fn)
3789 fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3790 if (!fn)
3791 goto egress;
3794 /* Turn forward declarations into real ones. */
3795 fn = cgraph_node (fn)->decl;
3797 /* If FN is a declaration of a function in a nested scope that was
3798 globally declared inline, we don't set its DECL_INITIAL.
3799 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3800 C++ front-end uses it for cdtors to refer to their internal
3801 declarations, that are not real functions. Fortunately those
3802 don't have trees to be saved, so we can tell by checking their
3803 gimple_body. */
3804 if (!DECL_INITIAL (fn)
3805 && DECL_ABSTRACT_ORIGIN (fn)
3806 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3807 fn = DECL_ABSTRACT_ORIGIN (fn);
3809 /* Objective C and fortran still calls tree_rest_of_compilation directly.
3810 Kill this check once this is fixed. */
3811 if (!id->dst_node->analyzed)
3812 goto egress;
3814 cg_edge = cgraph_edge (id->dst_node, stmt);
3816 /* First check that inlining isn't simply forbidden in this case. */
3817 if (inline_forbidden_into_p (cg_edge->caller->decl, cg_edge->callee->decl))
3818 goto egress;
3820 /* Don't try to inline functions that are not well-suited to inlining. */
3821 if (!cgraph_inline_p (cg_edge, &reason))
3823 /* If this call was originally indirect, we do not want to emit any
3824 inlining related warnings or sorry messages because there are no
3825 guarantees regarding those. */
3826 if (cg_edge->indirect_inlining_edge)
3827 goto egress;
3829 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3830 /* Avoid warnings during early inline pass. */
3831 && cgraph_global_info_ready)
3833 sorry ("inlining failed in call to %q+F: %s", fn,
3834 _(cgraph_inline_failed_string (reason)));
3835 sorry ("called from here");
3837 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3838 && !DECL_IN_SYSTEM_HEADER (fn)
3839 && reason != CIF_UNSPECIFIED
3840 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3841 /* Avoid warnings during early inline pass. */
3842 && cgraph_global_info_ready)
3844 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3845 fn, _(cgraph_inline_failed_string (reason)));
3846 warning (OPT_Winline, "called from here");
3848 goto egress;
3850 fn = cg_edge->callee->decl;
3852 #ifdef ENABLE_CHECKING
3853 if (cg_edge->callee->decl != id->dst_node->decl)
3854 verify_cgraph_node (cg_edge->callee);
3855 #endif
3857 /* We will be inlining this callee. */
3858 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
3860 /* Update the callers EH personality. */
3861 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
3862 DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3863 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
3865 /* Split the block holding the GIMPLE_CALL. */
3866 e = split_block (bb, stmt);
3867 bb = e->src;
3868 return_block = e->dest;
3869 remove_edge (e);
3871 /* split_block splits after the statement; work around this by
3872 moving the call into the second block manually. Not pretty,
3873 but seems easier than doing the CFG manipulation by hand
3874 when the GIMPLE_CALL is in the last statement of BB. */
3875 stmt_gsi = gsi_last_bb (bb);
3876 gsi_remove (&stmt_gsi, false);
3878 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3879 been the source of abnormal edges. In this case, schedule
3880 the removal of dead abnormal edges. */
3881 gsi = gsi_start_bb (return_block);
3882 if (gsi_end_p (gsi))
3884 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3885 purge_dead_abnormal_edges = true;
3887 else
3889 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3890 purge_dead_abnormal_edges = false;
3893 stmt_gsi = gsi_start_bb (return_block);
3895 /* Build a block containing code to initialize the arguments, the
3896 actual inline expansion of the body, and a label for the return
3897 statements within the function to jump to. The type of the
3898 statement expression is the return type of the function call. */
3899 id->block = make_node (BLOCK);
3900 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3901 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3902 prepend_lexical_block (gimple_block (stmt), id->block);
3904 /* Local declarations will be replaced by their equivalents in this
3905 map. */
3906 st = id->decl_map;
3907 id->decl_map = pointer_map_create ();
3908 dst = id->debug_map;
3909 id->debug_map = NULL;
3911 /* Record the function we are about to inline. */
3912 id->src_fn = fn;
3913 id->src_node = cg_edge->callee;
3914 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3915 id->gimple_call = stmt;
3917 gcc_assert (!id->src_cfun->after_inlining);
3919 id->entry_bb = bb;
3920 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3922 gimple_stmt_iterator si = gsi_last_bb (bb);
3923 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3924 NOT_TAKEN),
3925 GSI_NEW_STMT);
3927 initialize_inlined_parameters (id, stmt, fn, bb);
3929 if (DECL_INITIAL (fn))
3930 prepend_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3932 /* Return statements in the function body will be replaced by jumps
3933 to the RET_LABEL. */
3934 gcc_assert (DECL_INITIAL (fn));
3935 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3937 /* Find the LHS to which the result of this call is assigned. */
3938 return_slot = NULL;
3939 if (gimple_call_lhs (stmt))
3941 modify_dest = gimple_call_lhs (stmt);
3943 /* The function which we are inlining might not return a value,
3944 in which case we should issue a warning that the function
3945 does not return a value. In that case the optimizers will
3946 see that the variable to which the value is assigned was not
3947 initialized. We do not want to issue a warning about that
3948 uninitialized variable. */
3949 if (DECL_P (modify_dest))
3950 TREE_NO_WARNING (modify_dest) = 1;
3952 if (gimple_call_return_slot_opt_p (stmt))
3954 return_slot = modify_dest;
3955 modify_dest = NULL;
3958 else
3959 modify_dest = NULL;
3961 /* If we are inlining a call to the C++ operator new, we don't want
3962 to use type based alias analysis on the return value. Otherwise
3963 we may get confused if the compiler sees that the inlined new
3964 function returns a pointer which was just deleted. See bug
3965 33407. */
3966 if (DECL_IS_OPERATOR_NEW (fn))
3968 return_slot = NULL;
3969 modify_dest = NULL;
3972 /* Declare the return variable for the function. */
3973 use_retvar = declare_return_variable (id, return_slot, modify_dest);
3975 /* Add local vars in this inlined callee to caller. */
3976 t_step = id->src_cfun->local_decls;
3977 for (; t_step; t_step = TREE_CHAIN (t_step))
3979 var = TREE_VALUE (t_step);
3980 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3982 if (var_ann (var) && add_referenced_var (var))
3983 cfun->local_decls = tree_cons (NULL_TREE, var,
3984 cfun->local_decls);
3986 else if (!can_be_nonlocal (var, id))
3987 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3988 cfun->local_decls);
3991 if (dump_file && (dump_flags & TDF_DETAILS))
3993 fprintf (dump_file, "Inlining ");
3994 print_generic_expr (dump_file, id->src_fn, 0);
3995 fprintf (dump_file, " to ");
3996 print_generic_expr (dump_file, id->dst_fn, 0);
3997 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
4000 /* This is it. Duplicate the callee body. Assume callee is
4001 pre-gimplified. Note that we must not alter the caller
4002 function in any way before this point, as this CALL_EXPR may be
4003 a self-referential call; if we're calling ourselves, we need to
4004 duplicate our body before altering anything. */
4005 copy_body (id, bb->count,
4006 cg_edge->frequency * REG_BR_PROB_BASE / CGRAPH_FREQ_BASE,
4007 bb, return_block, NULL, NULL);
4009 /* Reset the escaped solution. */
4010 if (cfun->gimple_df)
4011 pt_solution_reset (&cfun->gimple_df->escaped);
4013 /* Clean up. */
4014 if (id->debug_map)
4016 pointer_map_destroy (id->debug_map);
4017 id->debug_map = dst;
4019 pointer_map_destroy (id->decl_map);
4020 id->decl_map = st;
4022 /* Unlink the calls virtual operands before replacing it. */
4023 unlink_stmt_vdef (stmt);
4025 /* If the inlined function returns a result that we care about,
4026 substitute the GIMPLE_CALL with an assignment of the return
4027 variable to the LHS of the call. That is, if STMT was
4028 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
4029 if (use_retvar && gimple_call_lhs (stmt))
4031 gimple old_stmt = stmt;
4032 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
4033 gsi_replace (&stmt_gsi, stmt, false);
4034 if (gimple_in_ssa_p (cfun))
4035 mark_symbols_for_renaming (stmt);
4036 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
4038 else
4040 /* Handle the case of inlining a function with no return
4041 statement, which causes the return value to become undefined. */
4042 if (gimple_call_lhs (stmt)
4043 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
4045 tree name = gimple_call_lhs (stmt);
4046 tree var = SSA_NAME_VAR (name);
4047 tree def = gimple_default_def (cfun, var);
4049 if (def)
4051 /* If the variable is used undefined, make this name
4052 undefined via a move. */
4053 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
4054 gsi_replace (&stmt_gsi, stmt, true);
4056 else
4058 /* Otherwise make this variable undefined. */
4059 gsi_remove (&stmt_gsi, true);
4060 set_default_def (var, name);
4061 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
4064 else
4065 gsi_remove (&stmt_gsi, true);
4068 if (purge_dead_abnormal_edges)
4069 gimple_purge_dead_abnormal_call_edges (return_block);
4071 /* If the value of the new expression is ignored, that's OK. We
4072 don't warn about this for CALL_EXPRs, so we shouldn't warn about
4073 the equivalent inlined version either. */
4074 if (is_gimple_assign (stmt))
4076 gcc_assert (gimple_assign_single_p (stmt)
4077 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
4078 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
4081 /* Output the inlining info for this abstract function, since it has been
4082 inlined. If we don't do this now, we can lose the information about the
4083 variables in the function when the blocks get blown away as soon as we
4084 remove the cgraph node. */
4085 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
4087 /* Update callgraph if needed. */
4088 cgraph_remove_node (cg_edge->callee);
4090 id->block = NULL_TREE;
4091 successfully_inlined = TRUE;
4093 egress:
4094 input_location = saved_location;
4095 return successfully_inlined;
4098 /* Expand call statements reachable from STMT_P.
4099 We can only have CALL_EXPRs as the "toplevel" tree code or nested
4100 in a MODIFY_EXPR. See gimple.c:get_call_expr_in(). We can
4101 unfortunately not use that function here because we need a pointer
4102 to the CALL_EXPR, not the tree itself. */
4104 static bool
4105 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
4107 gimple_stmt_iterator gsi;
4109 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4111 gimple stmt = gsi_stmt (gsi);
4113 if (is_gimple_call (stmt)
4114 && expand_call_inline (bb, stmt, id))
4115 return true;
4118 return false;
4122 /* Walk all basic blocks created after FIRST and try to fold every statement
4123 in the STATEMENTS pointer set. */
4125 static void
4126 fold_marked_statements (int first, struct pointer_set_t *statements)
4128 for (; first < n_basic_blocks; first++)
4129 if (BASIC_BLOCK (first))
4131 gimple_stmt_iterator gsi;
4133 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
4134 !gsi_end_p (gsi);
4135 gsi_next (&gsi))
4136 if (pointer_set_contains (statements, gsi_stmt (gsi)))
4138 gimple old_stmt = gsi_stmt (gsi);
4139 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
4141 if (old_decl && DECL_BUILT_IN (old_decl))
4143 /* Folding builtins can create multiple instructions,
4144 we need to look at all of them. */
4145 gimple_stmt_iterator i2 = gsi;
4146 gsi_prev (&i2);
4147 if (fold_stmt (&gsi))
4149 gimple new_stmt;
4150 if (gsi_end_p (i2))
4151 i2 = gsi_start_bb (BASIC_BLOCK (first));
4152 else
4153 gsi_next (&i2);
4154 while (1)
4156 new_stmt = gsi_stmt (i2);
4157 update_stmt (new_stmt);
4158 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4159 new_stmt);
4161 if (new_stmt == gsi_stmt (gsi))
4163 /* It is okay to check only for the very last
4164 of these statements. If it is a throwing
4165 statement nothing will change. If it isn't
4166 this can remove EH edges. If that weren't
4167 correct then because some intermediate stmts
4168 throw, but not the last one. That would mean
4169 we'd have to split the block, which we can't
4170 here and we'd loose anyway. And as builtins
4171 probably never throw, this all
4172 is mood anyway. */
4173 if (maybe_clean_or_replace_eh_stmt (old_stmt,
4174 new_stmt))
4175 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
4176 break;
4178 gsi_next (&i2);
4182 else if (fold_stmt (&gsi))
4184 /* Re-read the statement from GSI as fold_stmt() may
4185 have changed it. */
4186 gimple new_stmt = gsi_stmt (gsi);
4187 update_stmt (new_stmt);
4189 if (is_gimple_call (old_stmt)
4190 || is_gimple_call (new_stmt))
4191 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
4192 new_stmt);
4194 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
4195 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
4201 /* Return true if BB has at least one abnormal outgoing edge. */
4203 static inline bool
4204 has_abnormal_outgoing_edge_p (basic_block bb)
4206 edge e;
4207 edge_iterator ei;
4209 FOR_EACH_EDGE (e, ei, bb->succs)
4210 if (e->flags & EDGE_ABNORMAL)
4211 return true;
4213 return false;
4216 /* Expand calls to inline functions in the body of FN. */
4218 unsigned int
4219 optimize_inline_calls (tree fn)
4221 copy_body_data id;
4222 basic_block bb;
4223 int last = n_basic_blocks;
4224 struct gimplify_ctx gctx;
4226 /* There is no point in performing inlining if errors have already
4227 occurred -- and we might crash if we try to inline invalid
4228 code. */
4229 if (seen_error ())
4230 return 0;
4232 /* Clear out ID. */
4233 memset (&id, 0, sizeof (id));
4235 id.src_node = id.dst_node = cgraph_node (fn);
4236 id.dst_fn = fn;
4237 /* Or any functions that aren't finished yet. */
4238 if (current_function_decl)
4239 id.dst_fn = current_function_decl;
4241 id.copy_decl = copy_decl_maybe_to_var;
4242 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4243 id.transform_new_cfg = false;
4244 id.transform_return_to_modify = true;
4245 id.transform_lang_insert_block = NULL;
4246 id.statements_to_fold = pointer_set_create ();
4248 push_gimplify_context (&gctx);
4250 /* We make no attempts to keep dominance info up-to-date. */
4251 free_dominance_info (CDI_DOMINATORS);
4252 free_dominance_info (CDI_POST_DOMINATORS);
4254 /* Register specific gimple functions. */
4255 gimple_register_cfg_hooks ();
4257 /* Reach the trees by walking over the CFG, and note the
4258 enclosing basic-blocks in the call edges. */
4259 /* We walk the blocks going forward, because inlined function bodies
4260 will split id->current_basic_block, and the new blocks will
4261 follow it; we'll trudge through them, processing their CALL_EXPRs
4262 along the way. */
4263 FOR_EACH_BB (bb)
4264 gimple_expand_calls_inline (bb, &id);
4266 pop_gimplify_context (NULL);
4268 #ifdef ENABLE_CHECKING
4270 struct cgraph_edge *e;
4272 verify_cgraph_node (id.dst_node);
4274 /* Double check that we inlined everything we are supposed to inline. */
4275 for (e = id.dst_node->callees; e; e = e->next_callee)
4276 gcc_assert (e->inline_failed);
4278 #endif
4280 /* Fold the statements before compacting/renumbering the basic blocks. */
4281 fold_marked_statements (last, id.statements_to_fold);
4282 pointer_set_destroy (id.statements_to_fold);
4284 gcc_assert (!id.debug_stmts);
4286 /* Renumber the (code) basic_blocks consecutively. */
4287 compact_blocks ();
4288 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4289 number_blocks (fn);
4291 fold_cond_expr_cond ();
4292 delete_unreachable_blocks_update_callgraph (&id);
4293 #ifdef ENABLE_CHECKING
4294 verify_cgraph_node (id.dst_node);
4295 #endif
4297 /* It would be nice to check SSA/CFG/statement consistency here, but it is
4298 not possible yet - the IPA passes might make various functions to not
4299 throw and they don't care to proactively update local EH info. This is
4300 done later in fixup_cfg pass that also execute the verification. */
4301 return (TODO_update_ssa
4302 | TODO_cleanup_cfg
4303 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
4304 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
4307 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
4309 tree
4310 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4312 enum tree_code code = TREE_CODE (*tp);
4313 enum tree_code_class cl = TREE_CODE_CLASS (code);
4315 /* We make copies of most nodes. */
4316 if (IS_EXPR_CODE_CLASS (cl)
4317 || code == TREE_LIST
4318 || code == TREE_VEC
4319 || code == TYPE_DECL
4320 || code == OMP_CLAUSE)
4322 /* Because the chain gets clobbered when we make a copy, we save it
4323 here. */
4324 tree chain = NULL_TREE, new_tree;
4326 chain = TREE_CHAIN (*tp);
4328 /* Copy the node. */
4329 new_tree = copy_node (*tp);
4331 /* Propagate mudflap marked-ness. */
4332 if (flag_mudflap && mf_marked_p (*tp))
4333 mf_mark (new_tree);
4335 *tp = new_tree;
4337 /* Now, restore the chain, if appropriate. That will cause
4338 walk_tree to walk into the chain as well. */
4339 if (code == PARM_DECL
4340 || code == TREE_LIST
4341 || code == OMP_CLAUSE)
4342 TREE_CHAIN (*tp) = chain;
4344 /* For now, we don't update BLOCKs when we make copies. So, we
4345 have to nullify all BIND_EXPRs. */
4346 if (TREE_CODE (*tp) == BIND_EXPR)
4347 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
4349 else if (code == CONSTRUCTOR)
4351 /* CONSTRUCTOR nodes need special handling because
4352 we need to duplicate the vector of elements. */
4353 tree new_tree;
4355 new_tree = copy_node (*tp);
4357 /* Propagate mudflap marked-ness. */
4358 if (flag_mudflap && mf_marked_p (*tp))
4359 mf_mark (new_tree);
4361 CONSTRUCTOR_ELTS (new_tree) = VEC_copy (constructor_elt, gc,
4362 CONSTRUCTOR_ELTS (*tp));
4363 *tp = new_tree;
4365 else if (TREE_CODE_CLASS (code) == tcc_type)
4366 *walk_subtrees = 0;
4367 else if (TREE_CODE_CLASS (code) == tcc_declaration)
4368 *walk_subtrees = 0;
4369 else if (TREE_CODE_CLASS (code) == tcc_constant)
4370 *walk_subtrees = 0;
4371 else
4372 gcc_assert (code != STATEMENT_LIST);
4373 return NULL_TREE;
4376 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
4377 information indicating to what new SAVE_EXPR this one should be mapped,
4378 use that one. Otherwise, create a new node and enter it in ST. FN is
4379 the function into which the copy will be placed. */
4381 static void
4382 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
4384 struct pointer_map_t *st = (struct pointer_map_t *) st_;
4385 tree *n;
4386 tree t;
4388 /* See if we already encountered this SAVE_EXPR. */
4389 n = (tree *) pointer_map_contains (st, *tp);
4391 /* If we didn't already remap this SAVE_EXPR, do so now. */
4392 if (!n)
4394 t = copy_node (*tp);
4396 /* Remember this SAVE_EXPR. */
4397 *pointer_map_insert (st, *tp) = t;
4398 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
4399 *pointer_map_insert (st, t) = t;
4401 else
4403 /* We've already walked into this SAVE_EXPR; don't do it again. */
4404 *walk_subtrees = 0;
4405 t = *n;
4408 /* Replace this SAVE_EXPR with the copy. */
4409 *tp = t;
4412 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
4413 copies the declaration and enters it in the splay_tree in DATA (which is
4414 really an `copy_body_data *'). */
4416 static tree
4417 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4418 void *data)
4420 copy_body_data *id = (copy_body_data *) data;
4422 /* Don't walk into types. */
4423 if (TYPE_P (*tp))
4424 *walk_subtrees = 0;
4426 else if (TREE_CODE (*tp) == LABEL_EXPR)
4428 tree decl = TREE_OPERAND (*tp, 0);
4430 /* Copy the decl and remember the copy. */
4431 insert_decl_map (id, decl, id->copy_decl (decl, id));
4434 return NULL_TREE;
4437 /* Perform any modifications to EXPR required when it is unsaved. Does
4438 not recurse into EXPR's subtrees. */
4440 static void
4441 unsave_expr_1 (tree expr)
4443 switch (TREE_CODE (expr))
4445 case TARGET_EXPR:
4446 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4447 It's OK for this to happen if it was part of a subtree that
4448 isn't immediately expanded, such as operand 2 of another
4449 TARGET_EXPR. */
4450 if (TREE_OPERAND (expr, 1))
4451 break;
4453 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4454 TREE_OPERAND (expr, 3) = NULL_TREE;
4455 break;
4457 default:
4458 break;
4462 /* Called via walk_tree when an expression is unsaved. Using the
4463 splay_tree pointed to by ST (which is really a `splay_tree'),
4464 remaps all local declarations to appropriate replacements. */
4466 static tree
4467 unsave_r (tree *tp, int *walk_subtrees, void *data)
4469 copy_body_data *id = (copy_body_data *) data;
4470 struct pointer_map_t *st = id->decl_map;
4471 tree *n;
4473 /* Only a local declaration (variable or label). */
4474 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
4475 || TREE_CODE (*tp) == LABEL_DECL)
4477 /* Lookup the declaration. */
4478 n = (tree *) pointer_map_contains (st, *tp);
4480 /* If it's there, remap it. */
4481 if (n)
4482 *tp = *n;
4485 else if (TREE_CODE (*tp) == STATEMENT_LIST)
4486 gcc_unreachable ();
4487 else if (TREE_CODE (*tp) == BIND_EXPR)
4488 copy_bind_expr (tp, walk_subtrees, id);
4489 else if (TREE_CODE (*tp) == SAVE_EXPR
4490 || TREE_CODE (*tp) == TARGET_EXPR)
4491 remap_save_expr (tp, st, walk_subtrees);
4492 else
4494 copy_tree_r (tp, walk_subtrees, NULL);
4496 /* Do whatever unsaving is required. */
4497 unsave_expr_1 (*tp);
4500 /* Keep iterating. */
4501 return NULL_TREE;
4504 /* Copies everything in EXPR and replaces variables, labels
4505 and SAVE_EXPRs local to EXPR. */
4507 tree
4508 unsave_expr_now (tree expr)
4510 copy_body_data id;
4512 /* There's nothing to do for NULL_TREE. */
4513 if (expr == 0)
4514 return expr;
4516 /* Set up ID. */
4517 memset (&id, 0, sizeof (id));
4518 id.src_fn = current_function_decl;
4519 id.dst_fn = current_function_decl;
4520 id.decl_map = pointer_map_create ();
4521 id.debug_map = NULL;
4523 id.copy_decl = copy_decl_no_change;
4524 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4525 id.transform_new_cfg = false;
4526 id.transform_return_to_modify = false;
4527 id.transform_lang_insert_block = NULL;
4529 /* Walk the tree once to find local labels. */
4530 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
4532 /* Walk the tree again, copying, remapping, and unsaving. */
4533 walk_tree (&expr, unsave_r, &id, NULL);
4535 /* Clean up. */
4536 pointer_map_destroy (id.decl_map);
4537 if (id.debug_map)
4538 pointer_map_destroy (id.debug_map);
4540 return expr;
4543 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
4544 label, copies the declaration and enters it in the splay_tree in DATA (which
4545 is really a 'copy_body_data *'. */
4547 static tree
4548 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
4549 bool *handled_ops_p ATTRIBUTE_UNUSED,
4550 struct walk_stmt_info *wi)
4552 copy_body_data *id = (copy_body_data *) wi->info;
4553 gimple stmt = gsi_stmt (*gsip);
4555 if (gimple_code (stmt) == GIMPLE_LABEL)
4557 tree decl = gimple_label_label (stmt);
4559 /* Copy the decl and remember the copy. */
4560 insert_decl_map (id, decl, id->copy_decl (decl, id));
4563 return NULL_TREE;
4567 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4568 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4569 remaps all local declarations to appropriate replacements in gimple
4570 operands. */
4572 static tree
4573 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
4575 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
4576 copy_body_data *id = (copy_body_data *) wi->info;
4577 struct pointer_map_t *st = id->decl_map;
4578 tree *n;
4579 tree expr = *tp;
4581 /* Only a local declaration (variable or label). */
4582 if ((TREE_CODE (expr) == VAR_DECL
4583 && !TREE_STATIC (expr))
4584 || TREE_CODE (expr) == LABEL_DECL)
4586 /* Lookup the declaration. */
4587 n = (tree *) pointer_map_contains (st, expr);
4589 /* If it's there, remap it. */
4590 if (n)
4591 *tp = *n;
4592 *walk_subtrees = 0;
4594 else if (TREE_CODE (expr) == STATEMENT_LIST
4595 || TREE_CODE (expr) == BIND_EXPR
4596 || TREE_CODE (expr) == SAVE_EXPR)
4597 gcc_unreachable ();
4598 else if (TREE_CODE (expr) == TARGET_EXPR)
4600 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
4601 It's OK for this to happen if it was part of a subtree that
4602 isn't immediately expanded, such as operand 2 of another
4603 TARGET_EXPR. */
4604 if (!TREE_OPERAND (expr, 1))
4606 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
4607 TREE_OPERAND (expr, 3) = NULL_TREE;
4611 /* Keep iterating. */
4612 return NULL_TREE;
4616 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
4617 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
4618 remaps all local declarations to appropriate replacements in gimple
4619 statements. */
4621 static tree
4622 replace_locals_stmt (gimple_stmt_iterator *gsip,
4623 bool *handled_ops_p ATTRIBUTE_UNUSED,
4624 struct walk_stmt_info *wi)
4626 copy_body_data *id = (copy_body_data *) wi->info;
4627 gimple stmt = gsi_stmt (*gsip);
4629 if (gimple_code (stmt) == GIMPLE_BIND)
4631 tree block = gimple_bind_block (stmt);
4633 if (block)
4635 remap_block (&block, id);
4636 gimple_bind_set_block (stmt, block);
4639 /* This will remap a lot of the same decls again, but this should be
4640 harmless. */
4641 if (gimple_bind_vars (stmt))
4642 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), NULL, id));
4645 /* Keep iterating. */
4646 return NULL_TREE;
4650 /* Copies everything in SEQ and replaces variables and labels local to
4651 current_function_decl. */
4653 gimple_seq
4654 copy_gimple_seq_and_replace_locals (gimple_seq seq)
4656 copy_body_data id;
4657 struct walk_stmt_info wi;
4658 struct pointer_set_t *visited;
4659 gimple_seq copy;
4661 /* There's nothing to do for NULL_TREE. */
4662 if (seq == NULL)
4663 return seq;
4665 /* Set up ID. */
4666 memset (&id, 0, sizeof (id));
4667 id.src_fn = current_function_decl;
4668 id.dst_fn = current_function_decl;
4669 id.decl_map = pointer_map_create ();
4670 id.debug_map = NULL;
4672 id.copy_decl = copy_decl_no_change;
4673 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4674 id.transform_new_cfg = false;
4675 id.transform_return_to_modify = false;
4676 id.transform_lang_insert_block = NULL;
4678 /* Walk the tree once to find local labels. */
4679 memset (&wi, 0, sizeof (wi));
4680 visited = pointer_set_create ();
4681 wi.info = &id;
4682 wi.pset = visited;
4683 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
4684 pointer_set_destroy (visited);
4686 copy = gimple_seq_copy (seq);
4688 /* Walk the copy, remapping decls. */
4689 memset (&wi, 0, sizeof (wi));
4690 wi.info = &id;
4691 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4693 /* Clean up. */
4694 pointer_map_destroy (id.decl_map);
4695 if (id.debug_map)
4696 pointer_map_destroy (id.debug_map);
4698 return copy;
4702 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
4704 static tree
4705 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
4707 if (*tp == data)
4708 return (tree) data;
4709 else
4710 return NULL;
4713 DEBUG_FUNCTION bool
4714 debug_find_tree (tree top, tree search)
4716 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4720 /* Declare the variables created by the inliner. Add all the variables in
4721 VARS to BIND_EXPR. */
4723 static void
4724 declare_inline_vars (tree block, tree vars)
4726 tree t;
4727 for (t = vars; t; t = TREE_CHAIN (t))
4729 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4730 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
4731 cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
4734 if (block)
4735 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4738 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
4739 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
4740 VAR_DECL translation. */
4742 static tree
4743 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
4745 /* Don't generate debug information for the copy if we wouldn't have
4746 generated it for the copy either. */
4747 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4748 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4750 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4751 declaration inspired this copy. */
4752 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4754 /* The new variable/label has no RTL, yet. */
4755 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4756 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4757 SET_DECL_RTL (copy, 0);
4759 /* These args would always appear unused, if not for this. */
4760 TREE_USED (copy) = 1;
4762 /* Set the context for the new declaration. */
4763 if (!DECL_CONTEXT (decl))
4764 /* Globals stay global. */
4766 else if (DECL_CONTEXT (decl) != id->src_fn)
4767 /* Things that weren't in the scope of the function we're inlining
4768 from aren't in the scope we're inlining to, either. */
4770 else if (TREE_STATIC (decl))
4771 /* Function-scoped static variables should stay in the original
4772 function. */
4774 else
4775 /* Ordinary automatic local variables are now in the scope of the
4776 new function. */
4777 DECL_CONTEXT (copy) = id->dst_fn;
4779 return copy;
4782 static tree
4783 copy_decl_to_var (tree decl, copy_body_data *id)
4785 tree copy, type;
4787 gcc_assert (TREE_CODE (decl) == PARM_DECL
4788 || TREE_CODE (decl) == RESULT_DECL);
4790 type = TREE_TYPE (decl);
4792 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4793 VAR_DECL, DECL_NAME (decl), type);
4794 if (DECL_PT_UID_SET_P (decl))
4795 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
4796 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4797 TREE_READONLY (copy) = TREE_READONLY (decl);
4798 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4799 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4801 return copy_decl_for_dup_finish (id, decl, copy);
4804 /* Like copy_decl_to_var, but create a return slot object instead of a
4805 pointer variable for return by invisible reference. */
4807 static tree
4808 copy_result_decl_to_var (tree decl, copy_body_data *id)
4810 tree copy, type;
4812 gcc_assert (TREE_CODE (decl) == PARM_DECL
4813 || TREE_CODE (decl) == RESULT_DECL);
4815 type = TREE_TYPE (decl);
4816 if (DECL_BY_REFERENCE (decl))
4817 type = TREE_TYPE (type);
4819 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4820 VAR_DECL, DECL_NAME (decl), type);
4821 if (DECL_PT_UID_SET_P (decl))
4822 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
4823 TREE_READONLY (copy) = TREE_READONLY (decl);
4824 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4825 if (!DECL_BY_REFERENCE (decl))
4827 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4828 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4831 return copy_decl_for_dup_finish (id, decl, copy);
4834 tree
4835 copy_decl_no_change (tree decl, copy_body_data *id)
4837 tree copy;
4839 copy = copy_node (decl);
4841 /* The COPY is not abstract; it will be generated in DST_FN. */
4842 DECL_ABSTRACT (copy) = 0;
4843 lang_hooks.dup_lang_specific_decl (copy);
4845 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4846 been taken; it's for internal bookkeeping in expand_goto_internal. */
4847 if (TREE_CODE (copy) == LABEL_DECL)
4849 TREE_ADDRESSABLE (copy) = 0;
4850 LABEL_DECL_UID (copy) = -1;
4853 return copy_decl_for_dup_finish (id, decl, copy);
4856 static tree
4857 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4859 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4860 return copy_decl_to_var (decl, id);
4861 else
4862 return copy_decl_no_change (decl, id);
4865 /* Return a copy of the function's argument tree. */
4866 static tree
4867 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4868 bitmap args_to_skip, tree *vars)
4870 tree arg, *parg;
4871 tree new_parm = NULL;
4872 int i = 0;
4874 parg = &new_parm;
4876 for (arg = orig_parm; arg; arg = TREE_CHAIN (arg), i++)
4877 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4879 tree new_tree = remap_decl (arg, id);
4880 lang_hooks.dup_lang_specific_decl (new_tree);
4881 *parg = new_tree;
4882 parg = &TREE_CHAIN (new_tree);
4884 else if (!pointer_map_contains (id->decl_map, arg))
4886 /* Make an equivalent VAR_DECL. If the argument was used
4887 as temporary variable later in function, the uses will be
4888 replaced by local variable. */
4889 tree var = copy_decl_to_var (arg, id);
4890 get_var_ann (var);
4891 add_referenced_var (var);
4892 insert_decl_map (id, arg, var);
4893 /* Declare this new variable. */
4894 TREE_CHAIN (var) = *vars;
4895 *vars = var;
4897 return new_parm;
4900 /* Return a copy of the function's static chain. */
4901 static tree
4902 copy_static_chain (tree static_chain, copy_body_data * id)
4904 tree *chain_copy, *pvar;
4906 chain_copy = &static_chain;
4907 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4909 tree new_tree = remap_decl (*pvar, id);
4910 lang_hooks.dup_lang_specific_decl (new_tree);
4911 TREE_CHAIN (new_tree) = TREE_CHAIN (*pvar);
4912 *pvar = new_tree;
4914 return static_chain;
4917 /* Return true if the function is allowed to be versioned.
4918 This is a guard for the versioning functionality. */
4920 bool
4921 tree_versionable_function_p (tree fndecl)
4923 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
4924 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
4927 /* Delete all unreachable basic blocks and update callgraph.
4928 Doing so is somewhat nontrivial because we need to update all clones and
4929 remove inline function that become unreachable. */
4931 static bool
4932 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
4934 bool changed = false;
4935 basic_block b, next_bb;
4937 find_unreachable_blocks ();
4939 /* Delete all unreachable basic blocks. */
4941 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
4943 next_bb = b->next_bb;
4945 if (!(b->flags & BB_REACHABLE))
4947 gimple_stmt_iterator bsi;
4949 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
4950 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL)
4952 struct cgraph_edge *e;
4953 struct cgraph_node *node;
4955 if ((e = cgraph_edge (id->dst_node, gsi_stmt (bsi))) != NULL)
4957 if (!e->inline_failed)
4958 cgraph_remove_node_and_inline_clones (e->callee);
4959 else
4960 cgraph_remove_edge (e);
4962 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
4963 && id->dst_node->clones)
4964 for (node = id->dst_node->clones; node != id->dst_node;)
4966 if ((e = cgraph_edge (node, gsi_stmt (bsi))) != NULL)
4968 if (!e->inline_failed)
4969 cgraph_remove_node_and_inline_clones (e->callee);
4970 else
4971 cgraph_remove_edge (e);
4974 if (node->clones)
4975 node = node->clones;
4976 else if (node->next_sibling_clone)
4977 node = node->next_sibling_clone;
4978 else
4980 while (node != id->dst_node && !node->next_sibling_clone)
4981 node = node->clone_of;
4982 if (node != id->dst_node)
4983 node = node->next_sibling_clone;
4987 delete_basic_block (b);
4988 changed = true;
4992 if (changed)
4993 tidy_fallthru_edges ();
4994 return changed;
4997 /* Update clone info after duplication. */
4999 static void
5000 update_clone_info (copy_body_data * id)
5002 struct cgraph_node *node;
5003 if (!id->dst_node->clones)
5004 return;
5005 for (node = id->dst_node->clones; node != id->dst_node;)
5007 /* First update replace maps to match the new body. */
5008 if (node->clone.tree_map)
5010 unsigned int i;
5011 for (i = 0; i < VEC_length (ipa_replace_map_p, node->clone.tree_map); i++)
5013 struct ipa_replace_map *replace_info;
5014 replace_info = VEC_index (ipa_replace_map_p, node->clone.tree_map, i);
5015 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
5016 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
5019 if (node->clones)
5020 node = node->clones;
5021 else if (node->next_sibling_clone)
5022 node = node->next_sibling_clone;
5023 else
5025 while (node != id->dst_node && !node->next_sibling_clone)
5026 node = node->clone_of;
5027 if (node != id->dst_node)
5028 node = node->next_sibling_clone;
5033 /* Create a copy of a function's tree.
5034 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
5035 of the original function and the new copied function
5036 respectively. In case we want to replace a DECL
5037 tree with another tree while duplicating the function's
5038 body, TREE_MAP represents the mapping between these
5039 trees. If UPDATE_CLONES is set, the call_stmt fields
5040 of edges of clones of the function will be updated.
5042 If non-NULL ARGS_TO_SKIP determine function parameters to remove
5043 from new version.
5044 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
5045 If non_NULL NEW_ENTRY determine new entry BB of the clone.
5047 void
5048 tree_function_versioning (tree old_decl, tree new_decl,
5049 VEC(ipa_replace_map_p,gc)* tree_map,
5050 bool update_clones, bitmap args_to_skip,
5051 bitmap blocks_to_copy, basic_block new_entry)
5053 struct cgraph_node *old_version_node;
5054 struct cgraph_node *new_version_node;
5055 copy_body_data id;
5056 tree p;
5057 unsigned i;
5058 struct ipa_replace_map *replace_info;
5059 basic_block old_entry_block, bb;
5060 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
5062 tree t_step;
5063 tree old_current_function_decl = current_function_decl;
5064 tree vars = NULL_TREE;
5066 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
5067 && TREE_CODE (new_decl) == FUNCTION_DECL);
5068 DECL_POSSIBLY_INLINED (old_decl) = 1;
5070 old_version_node = cgraph_node (old_decl);
5071 new_version_node = cgraph_node (new_decl);
5073 /* Output the inlining info for this abstract function, since it has been
5074 inlined. If we don't do this now, we can lose the information about the
5075 variables in the function when the blocks get blown away as soon as we
5076 remove the cgraph node. */
5077 (*debug_hooks->outlining_inline_function) (old_decl);
5079 DECL_ARTIFICIAL (new_decl) = 1;
5080 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
5081 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
5083 /* Prepare the data structures for the tree copy. */
5084 memset (&id, 0, sizeof (id));
5086 /* Generate a new name for the new version. */
5087 id.statements_to_fold = pointer_set_create ();
5089 id.decl_map = pointer_map_create ();
5090 id.debug_map = NULL;
5091 id.src_fn = old_decl;
5092 id.dst_fn = new_decl;
5093 id.src_node = old_version_node;
5094 id.dst_node = new_version_node;
5095 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
5096 if (id.src_node->ipa_transforms_to_apply)
5098 VEC(ipa_opt_pass,heap) * old_transforms_to_apply = id.dst_node->ipa_transforms_to_apply;
5099 unsigned int i;
5101 id.dst_node->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
5102 id.src_node->ipa_transforms_to_apply);
5103 for (i = 0; i < VEC_length (ipa_opt_pass, old_transforms_to_apply); i++)
5104 VEC_safe_push (ipa_opt_pass, heap, id.dst_node->ipa_transforms_to_apply,
5105 VEC_index (ipa_opt_pass,
5106 old_transforms_to_apply,
5107 i));
5110 id.copy_decl = copy_decl_no_change;
5111 id.transform_call_graph_edges
5112 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
5113 id.transform_new_cfg = true;
5114 id.transform_return_to_modify = false;
5115 id.transform_lang_insert_block = NULL;
5117 current_function_decl = new_decl;
5118 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
5119 (DECL_STRUCT_FUNCTION (old_decl));
5120 initialize_cfun (new_decl, old_decl,
5121 old_entry_block->count);
5122 DECL_STRUCT_FUNCTION (new_decl)->gimple_df->ipa_pta
5123 = id.src_cfun->gimple_df->ipa_pta;
5124 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
5126 /* Copy the function's static chain. */
5127 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
5128 if (p)
5129 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
5130 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
5131 &id);
5133 /* If there's a tree_map, prepare for substitution. */
5134 if (tree_map)
5135 for (i = 0; i < VEC_length (ipa_replace_map_p, tree_map); i++)
5137 gimple init;
5138 replace_info = VEC_index (ipa_replace_map_p, tree_map, i);
5139 if (replace_info->replace_p)
5141 tree op = replace_info->new_tree;
5142 if (!replace_info->old_tree)
5144 int i = replace_info->parm_num;
5145 tree parm;
5146 for (parm = DECL_ARGUMENTS (old_decl); i; parm = TREE_CHAIN (parm))
5147 i --;
5148 replace_info->old_tree = parm;
5152 STRIP_NOPS (op);
5154 if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
5155 op = TREE_OPERAND (op, 0);
5157 if (TREE_CODE (op) == ADDR_EXPR)
5159 op = TREE_OPERAND (op, 0);
5160 while (handled_component_p (op))
5161 op = TREE_OPERAND (op, 0);
5162 if (TREE_CODE (op) == VAR_DECL)
5163 add_referenced_var (op);
5165 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
5166 init = setup_one_parameter (&id, replace_info->old_tree,
5167 replace_info->new_tree, id.src_fn,
5168 NULL,
5169 &vars);
5170 if (init)
5171 VEC_safe_push (gimple, heap, init_stmts, init);
5174 /* Copy the function's arguments. */
5175 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
5176 DECL_ARGUMENTS (new_decl) =
5177 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
5178 args_to_skip, &vars);
5180 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
5182 /* Renumber the lexical scoping (non-code) blocks consecutively. */
5183 number_blocks (id.dst_fn);
5185 declare_inline_vars (DECL_INITIAL (new_decl), vars);
5187 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
5188 /* Add local vars. */
5189 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
5190 t_step; t_step = TREE_CHAIN (t_step))
5192 tree var = TREE_VALUE (t_step);
5193 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
5194 cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
5195 else if (!can_be_nonlocal (var, &id))
5196 cfun->local_decls =
5197 tree_cons (NULL_TREE, remap_decl (var, &id),
5198 cfun->local_decls);
5201 /* Copy the Function's body. */
5202 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
5203 ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, blocks_to_copy, new_entry);
5205 if (DECL_RESULT (old_decl) != NULL_TREE)
5207 tree *res_decl = &DECL_RESULT (old_decl);
5208 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
5209 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
5212 /* Renumber the lexical scoping (non-code) blocks consecutively. */
5213 number_blocks (new_decl);
5215 /* We want to create the BB unconditionally, so that the addition of
5216 debug stmts doesn't affect BB count, which may in the end cause
5217 codegen differences. */
5218 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
5219 while (VEC_length (gimple, init_stmts))
5220 insert_init_stmt (&id, bb, VEC_pop (gimple, init_stmts));
5221 update_clone_info (&id);
5223 /* Remap the nonlocal_goto_save_area, if any. */
5224 if (cfun->nonlocal_goto_save_area)
5226 struct walk_stmt_info wi;
5228 memset (&wi, 0, sizeof (wi));
5229 wi.info = &id;
5230 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
5233 /* Clean up. */
5234 pointer_map_destroy (id.decl_map);
5235 if (id.debug_map)
5236 pointer_map_destroy (id.debug_map);
5237 free_dominance_info (CDI_DOMINATORS);
5238 free_dominance_info (CDI_POST_DOMINATORS);
5240 fold_marked_statements (0, id.statements_to_fold);
5241 pointer_set_destroy (id.statements_to_fold);
5242 fold_cond_expr_cond ();
5243 delete_unreachable_blocks_update_callgraph (&id);
5244 if (id.dst_node->analyzed)
5245 cgraph_rebuild_references ();
5246 update_ssa (TODO_update_ssa);
5248 /* After partial cloning we need to rescale frequencies, so they are
5249 within proper range in the cloned function. */
5250 if (new_entry)
5252 struct cgraph_edge *e;
5253 rebuild_frequencies ();
5255 new_version_node->count = ENTRY_BLOCK_PTR->count;
5256 for (e = new_version_node->callees; e; e = e->next_callee)
5258 basic_block bb = gimple_bb (e->call_stmt);
5259 e->frequency = compute_call_stmt_bb_frequency (current_function_decl, bb);
5260 e->count = bb->count;
5264 free_dominance_info (CDI_DOMINATORS);
5265 free_dominance_info (CDI_POST_DOMINATORS);
5267 gcc_assert (!id.debug_stmts);
5268 VEC_free (gimple, heap, init_stmts);
5269 pop_cfun ();
5270 current_function_decl = old_current_function_decl;
5271 gcc_assert (!current_function_decl
5272 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
5273 return;
5276 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
5277 the callee and return the inlined body on success. */
5279 tree
5280 maybe_inline_call_in_expr (tree exp)
5282 tree fn = get_callee_fndecl (exp);
5284 /* We can only try to inline "const" functions. */
5285 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
5287 struct pointer_map_t *decl_map = pointer_map_create ();
5288 call_expr_arg_iterator iter;
5289 copy_body_data id;
5290 tree param, arg, t;
5292 /* Remap the parameters. */
5293 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
5294 param;
5295 param = TREE_CHAIN (param), arg = next_call_expr_arg (&iter))
5296 *pointer_map_insert (decl_map, param) = arg;
5298 memset (&id, 0, sizeof (id));
5299 id.src_fn = fn;
5300 id.dst_fn = current_function_decl;
5301 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
5302 id.decl_map = decl_map;
5304 id.copy_decl = copy_decl_no_change;
5305 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5306 id.transform_new_cfg = false;
5307 id.transform_return_to_modify = true;
5308 id.transform_lang_insert_block = false;
5310 /* Make sure not to unshare trees behind the front-end's back
5311 since front-end specific mechanisms may rely on sharing. */
5312 id.regimplify = false;
5313 id.do_not_unshare = true;
5315 /* We're not inside any EH region. */
5316 id.eh_lp_nr = 0;
5318 t = copy_tree_body (&id);
5319 pointer_map_destroy (decl_map);
5321 /* We can only return something suitable for use in a GENERIC
5322 expression tree. */
5323 if (TREE_CODE (t) == MODIFY_EXPR)
5324 return TREE_OPERAND (t, 1);
5327 return NULL_TREE;
5330 /* Duplicate a type, fields and all. */
5332 tree
5333 build_duplicate_type (tree type)
5335 struct copy_body_data id;
5337 memset (&id, 0, sizeof (id));
5338 id.src_fn = current_function_decl;
5339 id.dst_fn = current_function_decl;
5340 id.src_cfun = cfun;
5341 id.decl_map = pointer_map_create ();
5342 id.debug_map = NULL;
5343 id.copy_decl = copy_decl_no_change;
5345 type = remap_type_1 (type, &id);
5347 pointer_map_destroy (id.decl_map);
5348 if (id.debug_map)
5349 pointer_map_destroy (id.debug_map);
5351 TYPE_CANONICAL (type) = type;
5353 return type;
5356 /* Return whether it is safe to inline a function because it used different
5357 target specific options or call site actual types mismatch parameter types.
5358 E is the call edge to be checked. */
5359 bool
5360 tree_can_inline_p (struct cgraph_edge *e)
5362 #if 0
5363 /* This causes a regression in SPEC in that it prevents a cold function from
5364 inlining a hot function. Perhaps this should only apply to functions
5365 that the user declares hot/cold/optimize explicitly. */
5367 /* Don't inline a function with a higher optimization level than the
5368 caller, or with different space constraints (hot/cold functions). */
5369 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
5370 tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
5372 if (caller_tree != callee_tree)
5374 struct cl_optimization *caller_opt
5375 = TREE_OPTIMIZATION ((caller_tree)
5376 ? caller_tree
5377 : optimization_default_node);
5379 struct cl_optimization *callee_opt
5380 = TREE_OPTIMIZATION ((callee_tree)
5381 ? callee_tree
5382 : optimization_default_node);
5384 if ((caller_opt->optimize > callee_opt->optimize)
5385 || (caller_opt->optimize_size != callee_opt->optimize_size))
5386 return false;
5388 #endif
5389 tree caller, callee, lhs;
5391 caller = e->caller->decl;
5392 callee = e->callee->decl;
5394 /* First check that inlining isn't simply forbidden in this case. */
5395 if (inline_forbidden_into_p (caller, callee))
5397 e->inline_failed = CIF_UNSPECIFIED;
5398 gimple_call_set_cannot_inline (e->call_stmt, true);
5399 return false;
5402 /* Allow the backend to decide if inlining is ok. */
5403 if (!targetm.target_option.can_inline_p (caller, callee))
5405 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
5406 gimple_call_set_cannot_inline (e->call_stmt, true);
5407 e->call_stmt_cannot_inline_p = true;
5408 return false;
5411 /* Do not inline calls where we cannot triviall work around mismatches
5412 in argument or return types. */
5413 if (e->call_stmt
5414 && ((DECL_RESULT (callee)
5415 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
5416 && (lhs = gimple_call_lhs (e->call_stmt)) != NULL_TREE
5417 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
5418 TREE_TYPE (lhs))
5419 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
5420 || !gimple_check_call_args (e->call_stmt)))
5422 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
5423 gimple_call_set_cannot_inline (e->call_stmt, true);
5424 e->call_stmt_cannot_inline_p = true;
5425 return false;
5428 return true;