* config/mips/mips.md (rdhwr): Rename to rdhwr_synci_step_<mode>.
[official-gcc.git] / gcc / tree-inline.c
blob33ccafdb78c9abaa12e34b28570f718e3d0b1ff4
1 /* Tree inlining.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Alexandre Oliva <aoliva@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "rtl.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "insn-config.h"
35 #include "varray.h"
36 #include "hashtab.h"
37 #include "langhooks.h"
38 #include "basic-block.h"
39 #include "tree-iterator.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "tree-mudflap.h"
43 #include "tree-flow.h"
44 #include "function.h"
45 #include "ggc.h"
46 #include "tree-flow.h"
47 #include "diagnostic.h"
48 #include "except.h"
49 #include "debug.h"
50 #include "pointer-set.h"
51 #include "ipa-prop.h"
52 #include "value-prof.h"
53 #include "tree-pass.h"
54 #include "target.h"
55 #include "integrate.h"
57 /* I'm not real happy about this, but we need to handle gimple and
58 non-gimple trees. */
59 #include "gimple.h"
61 /* Inlining, Cloning, Versioning, Parallelization
63 Inlining: a function body is duplicated, but the PARM_DECLs are
64 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
65 MODIFY_EXPRs that store to a dedicated returned-value variable.
66 The duplicated eh_region info of the copy will later be appended
67 to the info for the caller; the eh_region info in copied throwing
68 statements and RESX_EXPRs is adjusted accordingly.
70 Cloning: (only in C++) We have one body for a con/de/structor, and
71 multiple function decls, each with a unique parameter list.
72 Duplicate the body, using the given splay tree; some parameters
73 will become constants (like 0 or 1).
75 Versioning: a function body is duplicated and the result is a new
76 function rather than into blocks of an existing function as with
77 inlining. Some parameters will become constants.
79 Parallelization: a region of a function is duplicated resulting in
80 a new function. Variables may be replaced with complex expressions
81 to enable shared variable semantics.
83 All of these will simultaneously lookup any callgraph edges. If
84 we're going to inline the duplicated function body, and the given
85 function has some cloned callgraph nodes (one for each place this
86 function will be inlined) those callgraph edges will be duplicated.
87 If we're cloning the body, those callgraph edges will be
88 updated to point into the new body. (Note that the original
89 callgraph node and edge list will not be altered.)
91 See the CALL_EXPR handling case in copy_tree_body_r (). */
93 /* To Do:
95 o In order to make inlining-on-trees work, we pessimized
96 function-local static constants. In particular, they are now
97 always output, even when not addressed. Fix this by treating
98 function-local static constants just like global static
99 constants; the back-end already knows not to output them if they
100 are not needed.
102 o Provide heuristics to clamp inlining of recursive template
103 calls? */
106 /* Weights that estimate_num_insns uses for heuristics in inlining. */
108 eni_weights eni_inlining_weights;
110 /* Weights that estimate_num_insns uses to estimate the size of the
111 produced code. */
113 eni_weights eni_size_weights;
115 /* Weights that estimate_num_insns uses to estimate the time necessary
116 to execute the produced code. */
118 eni_weights eni_time_weights;
120 /* Prototypes. */
122 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
123 static bool inlinable_function_p (tree);
124 static void remap_block (tree *, copy_body_data *);
125 static tree remap_decls (tree, copy_body_data *);
126 static void copy_bind_expr (tree *, int *, copy_body_data *);
127 static tree mark_local_for_remap_r (tree *, int *, void *);
128 static void unsave_expr_1 (tree);
129 static tree unsave_r (tree *, int *, void *);
130 static void declare_inline_vars (tree, tree);
131 static void remap_save_expr (tree *, void *, int *);
132 static void prepend_lexical_block (tree current_block, tree new_block);
133 static tree copy_decl_to_var (tree, copy_body_data *);
134 static tree copy_result_decl_to_var (tree, copy_body_data *);
135 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
136 static gimple remap_gimple_stmt (gimple, copy_body_data *);
138 /* Insert a tree->tree mapping for ID. Despite the name suggests
139 that the trees should be variables, it is used for more than that. */
141 void
142 insert_decl_map (copy_body_data *id, tree key, tree value)
144 *pointer_map_insert (id->decl_map, key) = value;
146 /* Always insert an identity map as well. If we see this same new
147 node again, we won't want to duplicate it a second time. */
148 if (key != value)
149 *pointer_map_insert (id->decl_map, value) = value;
152 /* Construct new SSA name for old NAME. ID is the inline context. */
154 static tree
155 remap_ssa_name (tree name, copy_body_data *id)
157 tree new_tree;
158 tree *n;
160 gcc_assert (TREE_CODE (name) == SSA_NAME);
162 n = (tree *) pointer_map_contains (id->decl_map, name);
163 if (n)
164 return unshare_expr (*n);
166 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
167 in copy_bb. */
168 new_tree = remap_decl (SSA_NAME_VAR (name), id);
170 /* We might've substituted constant or another SSA_NAME for
171 the variable.
173 Replace the SSA name representing RESULT_DECL by variable during
174 inlining: this saves us from need to introduce PHI node in a case
175 return value is just partly initialized. */
176 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
177 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
178 || !id->transform_return_to_modify))
180 new_tree = make_ssa_name (new_tree, NULL);
181 insert_decl_map (id, name, new_tree);
182 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
183 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
184 TREE_TYPE (new_tree) = TREE_TYPE (SSA_NAME_VAR (new_tree));
185 if (gimple_nop_p (SSA_NAME_DEF_STMT (name)))
187 /* By inlining function having uninitialized variable, we might
188 extend the lifetime (variable might get reused). This cause
189 ICE in the case we end up extending lifetime of SSA name across
190 abnormal edge, but also increase register pressure.
192 We simply initialize all uninitialized vars by 0 except
193 for case we are inlining to very first BB. We can avoid
194 this for all BBs that are not inside strongly connected
195 regions of the CFG, but this is expensive to test. */
196 if (id->entry_bb
197 && is_gimple_reg (SSA_NAME_VAR (name))
198 && TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL
199 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
200 || EDGE_COUNT (id->entry_bb->preds) != 1))
202 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
203 gimple init_stmt;
205 init_stmt = gimple_build_assign (new_tree,
206 fold_convert (TREE_TYPE (new_tree),
207 integer_zero_node));
208 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
209 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
211 else
213 SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
214 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name))
215 == name)
216 set_default_def (SSA_NAME_VAR (new_tree), new_tree);
220 else
221 insert_decl_map (id, name, new_tree);
222 return new_tree;
225 /* Remap DECL during the copying of the BLOCK tree for the function. */
227 tree
228 remap_decl (tree decl, copy_body_data *id)
230 tree *n;
231 tree fn;
233 /* We only remap local variables in the current function. */
234 fn = id->src_fn;
236 /* See if we have remapped this declaration. */
238 n = (tree *) pointer_map_contains (id->decl_map, decl);
240 /* If we didn't already have an equivalent for this declaration,
241 create one now. */
242 if (!n)
244 /* Make a copy of the variable or label. */
245 tree t = id->copy_decl (decl, id);
247 /* Remember it, so that if we encounter this local entity again
248 we can reuse this copy. Do this early because remap_type may
249 need this decl for TYPE_STUB_DECL. */
250 insert_decl_map (id, decl, t);
252 if (!DECL_P (t))
253 return t;
255 /* Remap types, if necessary. */
256 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
257 if (TREE_CODE (t) == TYPE_DECL)
258 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
260 /* Remap sizes as necessary. */
261 walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
262 walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
264 /* If fields, do likewise for offset and qualifier. */
265 if (TREE_CODE (t) == FIELD_DECL)
267 walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
268 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
269 walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
272 if (cfun && gimple_in_ssa_p (cfun)
273 && (TREE_CODE (t) == VAR_DECL
274 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
276 tree def = gimple_default_def (id->src_cfun, decl);
277 get_var_ann (t);
278 if (TREE_CODE (decl) != PARM_DECL && def)
280 tree map = remap_ssa_name (def, id);
281 /* Watch out RESULT_DECLs whose SSA names map directly
282 to them. */
283 if (TREE_CODE (map) == SSA_NAME
284 && gimple_nop_p (SSA_NAME_DEF_STMT (map)))
285 set_default_def (t, map);
287 add_referenced_var (t);
289 return t;
292 return unshare_expr (*n);
295 static tree
296 remap_type_1 (tree type, copy_body_data *id)
298 tree new_tree, t;
300 /* We do need a copy. build and register it now. If this is a pointer or
301 reference type, remap the designated type and make a new pointer or
302 reference type. */
303 if (TREE_CODE (type) == POINTER_TYPE)
305 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
306 TYPE_MODE (type),
307 TYPE_REF_CAN_ALIAS_ALL (type));
308 insert_decl_map (id, type, new_tree);
309 return new_tree;
311 else if (TREE_CODE (type) == REFERENCE_TYPE)
313 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
314 TYPE_MODE (type),
315 TYPE_REF_CAN_ALIAS_ALL (type));
316 insert_decl_map (id, type, new_tree);
317 return new_tree;
319 else
320 new_tree = copy_node (type);
322 insert_decl_map (id, type, new_tree);
324 /* This is a new type, not a copy of an old type. Need to reassociate
325 variants. We can handle everything except the main variant lazily. */
326 t = TYPE_MAIN_VARIANT (type);
327 if (type != t)
329 t = remap_type (t, id);
330 TYPE_MAIN_VARIANT (new_tree) = t;
331 TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
332 TYPE_NEXT_VARIANT (t) = new_tree;
334 else
336 TYPE_MAIN_VARIANT (new_tree) = new_tree;
337 TYPE_NEXT_VARIANT (new_tree) = NULL;
340 if (TYPE_STUB_DECL (type))
341 TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
343 /* Lazily create pointer and reference types. */
344 TYPE_POINTER_TO (new_tree) = NULL;
345 TYPE_REFERENCE_TO (new_tree) = NULL;
347 switch (TREE_CODE (new_tree))
349 case INTEGER_TYPE:
350 case REAL_TYPE:
351 case FIXED_POINT_TYPE:
352 case ENUMERAL_TYPE:
353 case BOOLEAN_TYPE:
354 t = TYPE_MIN_VALUE (new_tree);
355 if (t && TREE_CODE (t) != INTEGER_CST)
356 walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
358 t = TYPE_MAX_VALUE (new_tree);
359 if (t && TREE_CODE (t) != INTEGER_CST)
360 walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
361 return new_tree;
363 case FUNCTION_TYPE:
364 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
365 walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
366 return new_tree;
368 case ARRAY_TYPE:
369 TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
370 TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
371 break;
373 case RECORD_TYPE:
374 case UNION_TYPE:
375 case QUAL_UNION_TYPE:
377 tree f, nf = NULL;
379 for (f = TYPE_FIELDS (new_tree); f ; f = TREE_CHAIN (f))
381 t = remap_decl (f, id);
382 DECL_CONTEXT (t) = new_tree;
383 TREE_CHAIN (t) = nf;
384 nf = t;
386 TYPE_FIELDS (new_tree) = nreverse (nf);
388 break;
390 case OFFSET_TYPE:
391 default:
392 /* Shouldn't have been thought variable sized. */
393 gcc_unreachable ();
396 walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
397 walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
399 return new_tree;
402 tree
403 remap_type (tree type, copy_body_data *id)
405 tree *node;
406 tree tmp;
408 if (type == NULL)
409 return type;
411 /* See if we have remapped this type. */
412 node = (tree *) pointer_map_contains (id->decl_map, type);
413 if (node)
414 return *node;
416 /* The type only needs remapping if it's variably modified. */
417 if (! variably_modified_type_p (type, id->src_fn))
419 insert_decl_map (id, type, type);
420 return type;
423 id->remapping_type_depth++;
424 tmp = remap_type_1 (type, id);
425 id->remapping_type_depth--;
427 return tmp;
430 static tree
431 remap_decls (tree decls, copy_body_data *id)
433 tree old_var;
434 tree new_decls = NULL_TREE;
436 /* Remap its variables. */
437 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
439 tree new_var;
441 /* We cannot chain the local static declarations into the local_decls
442 as we can't duplicate them or break one decl rule. Go ahead
443 and link them into local_decls. */
445 if (!auto_var_in_fn_p (old_var, id->src_fn)
446 && !DECL_EXTERNAL (old_var))
448 cfun->local_decls = tree_cons (NULL_TREE, old_var,
449 cfun->local_decls);
450 continue;
453 /* Remap the variable. */
454 new_var = remap_decl (old_var, id);
456 /* If we didn't remap this variable, we can't mess with its
457 TREE_CHAIN. If we remapped this variable to the return slot, it's
458 already declared somewhere else, so don't declare it here. */
459 if (!new_var || new_var == id->retvar)
461 else
463 gcc_assert (DECL_P (new_var));
464 TREE_CHAIN (new_var) = new_decls;
465 new_decls = new_var;
469 return nreverse (new_decls);
472 /* Copy the BLOCK to contain remapped versions of the variables
473 therein. And hook the new block into the block-tree. */
475 static void
476 remap_block (tree *block, copy_body_data *id)
478 tree old_block;
479 tree new_block;
480 tree fn;
482 /* Make the new block. */
483 old_block = *block;
484 new_block = make_node (BLOCK);
485 TREE_USED (new_block) = TREE_USED (old_block);
486 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
487 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
488 *block = new_block;
490 /* Remap its variables. */
491 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
493 fn = id->dst_fn;
495 if (id->transform_lang_insert_block)
496 id->transform_lang_insert_block (new_block);
498 /* Remember the remapped block. */
499 insert_decl_map (id, old_block, new_block);
502 /* Copy the whole block tree and root it in id->block. */
503 static tree
504 remap_blocks (tree block, copy_body_data *id)
506 tree t;
507 tree new_tree = block;
509 if (!block)
510 return NULL;
512 remap_block (&new_tree, id);
513 gcc_assert (new_tree != block);
514 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
515 prepend_lexical_block (new_tree, remap_blocks (t, id));
516 /* Blocks are in arbitrary order, but make things slightly prettier and do
517 not swap order when producing a copy. */
518 BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
519 return new_tree;
522 static void
523 copy_statement_list (tree *tp)
525 tree_stmt_iterator oi, ni;
526 tree new_tree;
528 new_tree = alloc_stmt_list ();
529 ni = tsi_start (new_tree);
530 oi = tsi_start (*tp);
531 *tp = new_tree;
533 for (; !tsi_end_p (oi); tsi_next (&oi))
534 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
537 static void
538 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
540 tree block = BIND_EXPR_BLOCK (*tp);
541 /* Copy (and replace) the statement. */
542 copy_tree_r (tp, walk_subtrees, NULL);
543 if (block)
545 remap_block (&block, id);
546 BIND_EXPR_BLOCK (*tp) = block;
549 if (BIND_EXPR_VARS (*tp))
550 /* This will remap a lot of the same decls again, but this should be
551 harmless. */
552 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
556 /* Create a new gimple_seq by remapping all the statements in BODY
557 using the inlining information in ID. */
559 gimple_seq
560 remap_gimple_seq (gimple_seq body, copy_body_data *id)
562 gimple_stmt_iterator si;
563 gimple_seq new_body = NULL;
565 for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
567 gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
568 gimple_seq_add_stmt (&new_body, new_stmt);
571 return new_body;
575 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
576 block using the mapping information in ID. */
578 static gimple
579 copy_gimple_bind (gimple stmt, copy_body_data *id)
581 gimple new_bind;
582 tree new_block, new_vars;
583 gimple_seq body, new_body;
585 /* Copy the statement. Note that we purposely don't use copy_stmt
586 here because we need to remap statements as we copy. */
587 body = gimple_bind_body (stmt);
588 new_body = remap_gimple_seq (body, id);
590 new_block = gimple_bind_block (stmt);
591 if (new_block)
592 remap_block (&new_block, id);
594 /* This will remap a lot of the same decls again, but this should be
595 harmless. */
596 new_vars = gimple_bind_vars (stmt);
597 if (new_vars)
598 new_vars = remap_decls (new_vars, id);
600 new_bind = gimple_build_bind (new_vars, new_body, new_block);
602 return new_bind;
606 /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
607 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
608 WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
609 recursing into the children nodes of *TP. */
611 static tree
612 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
614 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
615 copy_body_data *id = (copy_body_data *) wi_p->info;
616 tree fn = id->src_fn;
618 if (TREE_CODE (*tp) == SSA_NAME)
620 *tp = remap_ssa_name (*tp, id);
621 *walk_subtrees = 0;
622 return NULL;
624 else if (auto_var_in_fn_p (*tp, fn))
626 /* Local variables and labels need to be replaced by equivalent
627 variables. We don't want to copy static variables; there's
628 only one of those, no matter how many times we inline the
629 containing function. Similarly for globals from an outer
630 function. */
631 tree new_decl;
633 /* Remap the declaration. */
634 new_decl = remap_decl (*tp, id);
635 gcc_assert (new_decl);
636 /* Replace this variable with the copy. */
637 STRIP_TYPE_NOPS (new_decl);
638 *tp = new_decl;
639 *walk_subtrees = 0;
641 else if (TREE_CODE (*tp) == STATEMENT_LIST)
642 gcc_unreachable ();
643 else if (TREE_CODE (*tp) == SAVE_EXPR)
644 gcc_unreachable ();
645 else if (TREE_CODE (*tp) == LABEL_DECL
646 && (!DECL_CONTEXT (*tp)
647 || decl_function_context (*tp) == id->src_fn))
648 /* These may need to be remapped for EH handling. */
649 *tp = remap_decl (*tp, id);
650 else if (TYPE_P (*tp))
651 /* Types may need remapping as well. */
652 *tp = remap_type (*tp, id);
653 else if (CONSTANT_CLASS_P (*tp))
655 /* If this is a constant, we have to copy the node iff the type
656 will be remapped. copy_tree_r will not copy a constant. */
657 tree new_type = remap_type (TREE_TYPE (*tp), id);
659 if (new_type == TREE_TYPE (*tp))
660 *walk_subtrees = 0;
662 else if (TREE_CODE (*tp) == INTEGER_CST)
663 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
664 TREE_INT_CST_HIGH (*tp));
665 else
667 *tp = copy_node (*tp);
668 TREE_TYPE (*tp) = new_type;
671 else
673 /* Otherwise, just copy the node. Note that copy_tree_r already
674 knows not to copy VAR_DECLs, etc., so this is safe. */
675 if (TREE_CODE (*tp) == INDIRECT_REF)
677 /* Get rid of *& from inline substitutions that can happen when a
678 pointer argument is an ADDR_EXPR. */
679 tree decl = TREE_OPERAND (*tp, 0);
680 tree *n;
682 n = (tree *) pointer_map_contains (id->decl_map, decl);
683 if (n)
685 tree type, new_tree, old;
687 /* If we happen to get an ADDR_EXPR in n->value, strip
688 it manually here as we'll eventually get ADDR_EXPRs
689 which lie about their types pointed to. In this case
690 build_fold_indirect_ref wouldn't strip the
691 INDIRECT_REF, but we absolutely rely on that. As
692 fold_indirect_ref does other useful transformations,
693 try that first, though. */
694 type = TREE_TYPE (TREE_TYPE (*n));
695 new_tree = unshare_expr (*n);
696 old = *tp;
697 *tp = gimple_fold_indirect_ref (new_tree);
698 if (!*tp)
700 if (TREE_CODE (new_tree) == ADDR_EXPR)
702 *tp = fold_indirect_ref_1 (type, new_tree);
703 /* ??? We should either assert here or build
704 a VIEW_CONVERT_EXPR instead of blindly leaking
705 incompatible types to our IL. */
706 if (! *tp)
707 *tp = TREE_OPERAND (new_tree, 0);
709 else
711 *tp = build1 (INDIRECT_REF, type, new_tree);
712 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
715 *walk_subtrees = 0;
716 return NULL;
720 /* Here is the "usual case". Copy this tree node, and then
721 tweak some special cases. */
722 copy_tree_r (tp, walk_subtrees, NULL);
724 /* Global variables we haven't seen yet need to go into referenced
725 vars. If not referenced from types only. */
726 if (gimple_in_ssa_p (cfun)
727 && TREE_CODE (*tp) == VAR_DECL
728 && id->remapping_type_depth == 0)
729 add_referenced_var (*tp);
731 /* We should never have TREE_BLOCK set on non-statements. */
732 if (EXPR_P (*tp))
733 gcc_assert (!TREE_BLOCK (*tp));
735 if (TREE_CODE (*tp) != OMP_CLAUSE)
736 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
738 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
740 /* The copied TARGET_EXPR has never been expanded, even if the
741 original node was expanded already. */
742 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
743 TREE_OPERAND (*tp, 3) = NULL_TREE;
745 else if (TREE_CODE (*tp) == ADDR_EXPR)
747 /* Variable substitution need not be simple. In particular,
748 the INDIRECT_REF substitution above. Make sure that
749 TREE_CONSTANT and friends are up-to-date. But make sure
750 to not improperly set TREE_BLOCK on some sub-expressions. */
751 int invariant = is_gimple_min_invariant (*tp);
752 tree block = id->block;
753 id->block = NULL_TREE;
754 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
755 id->block = block;
757 /* Handle the case where we substituted an INDIRECT_REF
758 into the operand of the ADDR_EXPR. */
759 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
760 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
761 else
762 recompute_tree_invariant_for_addr_expr (*tp);
764 /* If this used to be invariant, but is not any longer,
765 then regimplification is probably needed. */
766 if (invariant && !is_gimple_min_invariant (*tp))
767 id->regimplify = true;
769 *walk_subtrees = 0;
773 /* Keep iterating. */
774 return NULL_TREE;
778 /* Called from copy_body_id via walk_tree. DATA is really a
779 `copy_body_data *'. */
781 tree
782 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
784 copy_body_data *id = (copy_body_data *) data;
785 tree fn = id->src_fn;
786 tree new_block;
788 /* Begin by recognizing trees that we'll completely rewrite for the
789 inlining context. Our output for these trees is completely
790 different from out input (e.g. RETURN_EXPR is deleted, and morphs
791 into an edge). Further down, we'll handle trees that get
792 duplicated and/or tweaked. */
794 /* When requested, RETURN_EXPRs should be transformed to just the
795 contained MODIFY_EXPR. The branch semantics of the return will
796 be handled elsewhere by manipulating the CFG rather than a statement. */
797 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
799 tree assignment = TREE_OPERAND (*tp, 0);
801 /* If we're returning something, just turn that into an
802 assignment into the equivalent of the original RESULT_DECL.
803 If the "assignment" is just the result decl, the result
804 decl has already been set (e.g. a recent "foo (&result_decl,
805 ...)"); just toss the entire RETURN_EXPR. */
806 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
808 /* Replace the RETURN_EXPR with (a copy of) the
809 MODIFY_EXPR hanging underneath. */
810 *tp = copy_node (assignment);
812 else /* Else the RETURN_EXPR returns no value. */
814 *tp = NULL;
815 return (tree) (void *)1;
818 else if (TREE_CODE (*tp) == SSA_NAME)
820 *tp = remap_ssa_name (*tp, id);
821 *walk_subtrees = 0;
822 return NULL;
825 /* Local variables and labels need to be replaced by equivalent
826 variables. We don't want to copy static variables; there's only
827 one of those, no matter how many times we inline the containing
828 function. Similarly for globals from an outer function. */
829 else if (auto_var_in_fn_p (*tp, fn))
831 tree new_decl;
833 /* Remap the declaration. */
834 new_decl = remap_decl (*tp, id);
835 gcc_assert (new_decl);
836 /* Replace this variable with the copy. */
837 STRIP_TYPE_NOPS (new_decl);
838 *tp = new_decl;
839 *walk_subtrees = 0;
841 else if (TREE_CODE (*tp) == STATEMENT_LIST)
842 copy_statement_list (tp);
843 else if (TREE_CODE (*tp) == SAVE_EXPR)
844 remap_save_expr (tp, id->decl_map, walk_subtrees);
845 else if (TREE_CODE (*tp) == LABEL_DECL
846 && (! DECL_CONTEXT (*tp)
847 || decl_function_context (*tp) == id->src_fn))
848 /* These may need to be remapped for EH handling. */
849 *tp = remap_decl (*tp, id);
850 else if (TREE_CODE (*tp) == BIND_EXPR)
851 copy_bind_expr (tp, walk_subtrees, id);
852 /* Types may need remapping as well. */
853 else if (TYPE_P (*tp))
854 *tp = remap_type (*tp, id);
856 /* If this is a constant, we have to copy the node iff the type will be
857 remapped. copy_tree_r will not copy a constant. */
858 else if (CONSTANT_CLASS_P (*tp))
860 tree new_type = remap_type (TREE_TYPE (*tp), id);
862 if (new_type == TREE_TYPE (*tp))
863 *walk_subtrees = 0;
865 else if (TREE_CODE (*tp) == INTEGER_CST)
866 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
867 TREE_INT_CST_HIGH (*tp));
868 else
870 *tp = copy_node (*tp);
871 TREE_TYPE (*tp) = new_type;
875 /* Otherwise, just copy the node. Note that copy_tree_r already
876 knows not to copy VAR_DECLs, etc., so this is safe. */
877 else
879 /* Here we handle trees that are not completely rewritten.
880 First we detect some inlining-induced bogosities for
881 discarding. */
882 if (TREE_CODE (*tp) == MODIFY_EXPR
883 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
884 && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
886 /* Some assignments VAR = VAR; don't generate any rtl code
887 and thus don't count as variable modification. Avoid
888 keeping bogosities like 0 = 0. */
889 tree decl = TREE_OPERAND (*tp, 0), value;
890 tree *n;
892 n = (tree *) pointer_map_contains (id->decl_map, decl);
893 if (n)
895 value = *n;
896 STRIP_TYPE_NOPS (value);
897 if (TREE_CONSTANT (value) || TREE_READONLY (value))
899 *tp = build_empty_stmt ();
900 return copy_tree_body_r (tp, walk_subtrees, data);
904 else if (TREE_CODE (*tp) == INDIRECT_REF)
906 /* Get rid of *& from inline substitutions that can happen when a
907 pointer argument is an ADDR_EXPR. */
908 tree decl = TREE_OPERAND (*tp, 0);
909 tree *n;
911 n = (tree *) pointer_map_contains (id->decl_map, decl);
912 if (n)
914 tree new_tree;
915 tree old;
916 /* If we happen to get an ADDR_EXPR in n->value, strip
917 it manually here as we'll eventually get ADDR_EXPRs
918 which lie about their types pointed to. In this case
919 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
920 but we absolutely rely on that. As fold_indirect_ref
921 does other useful transformations, try that first, though. */
922 tree type = TREE_TYPE (TREE_TYPE (*n));
923 new_tree = unshare_expr (*n);
924 old = *tp;
925 *tp = gimple_fold_indirect_ref (new_tree);
926 if (! *tp)
928 if (TREE_CODE (new_tree) == ADDR_EXPR)
930 *tp = fold_indirect_ref_1 (type, new_tree);
931 /* ??? We should either assert here or build
932 a VIEW_CONVERT_EXPR instead of blindly leaking
933 incompatible types to our IL. */
934 if (! *tp)
935 *tp = TREE_OPERAND (new_tree, 0);
937 else
939 *tp = build1 (INDIRECT_REF, type, new_tree);
940 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
941 TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
944 *walk_subtrees = 0;
945 return NULL;
949 /* Here is the "usual case". Copy this tree node, and then
950 tweak some special cases. */
951 copy_tree_r (tp, walk_subtrees, NULL);
953 /* Global variables we haven't seen yet needs to go into referenced
954 vars. If not referenced from types only. */
955 if (gimple_in_ssa_p (cfun)
956 && TREE_CODE (*tp) == VAR_DECL
957 && id->remapping_type_depth == 0)
958 add_referenced_var (*tp);
960 /* If EXPR has block defined, map it to newly constructed block.
961 When inlining we want EXPRs without block appear in the block
962 of function call. */
963 if (EXPR_P (*tp))
965 new_block = id->block;
966 if (TREE_BLOCK (*tp))
968 tree *n;
969 n = (tree *) pointer_map_contains (id->decl_map,
970 TREE_BLOCK (*tp));
971 gcc_assert (n);
972 new_block = *n;
974 TREE_BLOCK (*tp) = new_block;
977 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
978 TREE_OPERAND (*tp, 0) =
979 build_int_cst (NULL_TREE,
980 id->eh_region_offset
981 + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
983 if (TREE_CODE (*tp) != OMP_CLAUSE)
984 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
986 /* The copied TARGET_EXPR has never been expanded, even if the
987 original node was expanded already. */
988 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
990 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
991 TREE_OPERAND (*tp, 3) = NULL_TREE;
994 /* Variable substitution need not be simple. In particular, the
995 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
996 and friends are up-to-date. */
997 else if (TREE_CODE (*tp) == ADDR_EXPR)
999 int invariant = is_gimple_min_invariant (*tp);
1000 walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1002 /* Handle the case where we substituted an INDIRECT_REF
1003 into the operand of the ADDR_EXPR. */
1004 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1005 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1006 else
1007 recompute_tree_invariant_for_addr_expr (*tp);
1009 /* If this used to be invariant, but is not any longer,
1010 then regimplification is probably needed. */
1011 if (invariant && !is_gimple_min_invariant (*tp))
1012 id->regimplify = true;
1014 *walk_subtrees = 0;
1018 /* Keep iterating. */
1019 return NULL_TREE;
1023 /* Helper for copy_bb. Remap statement STMT using the inlining
1024 information in ID. Return the new statement copy. */
1026 static gimple
1027 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1029 gimple copy = NULL;
1030 struct walk_stmt_info wi;
1031 tree new_block;
1032 bool skip_first = false;
1034 /* Begin by recognizing trees that we'll completely rewrite for the
1035 inlining context. Our output for these trees is completely
1036 different from out input (e.g. RETURN_EXPR is deleted, and morphs
1037 into an edge). Further down, we'll handle trees that get
1038 duplicated and/or tweaked. */
1040 /* When requested, GIMPLE_RETURNs should be transformed to just the
1041 contained GIMPLE_ASSIGN. The branch semantics of the return will
1042 be handled elsewhere by manipulating the CFG rather than the
1043 statement. */
1044 if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1046 tree retval = gimple_return_retval (stmt);
1048 /* If we're returning something, just turn that into an
1049 assignment into the equivalent of the original RESULT_DECL.
1050 If RETVAL is just the result decl, the result decl has
1051 already been set (e.g. a recent "foo (&result_decl, ...)");
1052 just toss the entire GIMPLE_RETURN. */
1053 if (retval && TREE_CODE (retval) != RESULT_DECL)
1055 copy = gimple_build_assign (id->retvar, retval);
1056 /* id->retvar is already substituted. Skip it on later remapping. */
1057 skip_first = true;
1059 else
1060 return gimple_build_nop ();
1062 else if (gimple_has_substatements (stmt))
1064 gimple_seq s1, s2;
1066 /* When cloning bodies from the C++ front end, we will be handed bodies
1067 in High GIMPLE form. Handle here all the High GIMPLE statements that
1068 have embedded statements. */
1069 switch (gimple_code (stmt))
1071 case GIMPLE_BIND:
1072 copy = copy_gimple_bind (stmt, id);
1073 break;
1075 case GIMPLE_CATCH:
1076 s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1077 copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1078 break;
1080 case GIMPLE_EH_FILTER:
1081 s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1082 copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1083 break;
1085 case GIMPLE_TRY:
1086 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1087 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1088 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1089 break;
1091 case GIMPLE_WITH_CLEANUP_EXPR:
1092 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1093 copy = gimple_build_wce (s1);
1094 break;
1096 case GIMPLE_OMP_PARALLEL:
1097 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1098 copy = gimple_build_omp_parallel
1099 (s1,
1100 gimple_omp_parallel_clauses (stmt),
1101 gimple_omp_parallel_child_fn (stmt),
1102 gimple_omp_parallel_data_arg (stmt));
1103 break;
1105 case GIMPLE_OMP_TASK:
1106 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1107 copy = gimple_build_omp_task
1108 (s1,
1109 gimple_omp_task_clauses (stmt),
1110 gimple_omp_task_child_fn (stmt),
1111 gimple_omp_task_data_arg (stmt),
1112 gimple_omp_task_copy_fn (stmt),
1113 gimple_omp_task_arg_size (stmt),
1114 gimple_omp_task_arg_align (stmt));
1115 break;
1117 case GIMPLE_OMP_FOR:
1118 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1119 s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1120 copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1121 gimple_omp_for_collapse (stmt), s2);
1123 size_t i;
1124 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1126 gimple_omp_for_set_index (copy, i,
1127 gimple_omp_for_index (stmt, i));
1128 gimple_omp_for_set_initial (copy, i,
1129 gimple_omp_for_initial (stmt, i));
1130 gimple_omp_for_set_final (copy, i,
1131 gimple_omp_for_final (stmt, i));
1132 gimple_omp_for_set_incr (copy, i,
1133 gimple_omp_for_incr (stmt, i));
1134 gimple_omp_for_set_cond (copy, i,
1135 gimple_omp_for_cond (stmt, i));
1138 break;
1140 case GIMPLE_OMP_MASTER:
1141 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1142 copy = gimple_build_omp_master (s1);
1143 break;
1145 case GIMPLE_OMP_ORDERED:
1146 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1147 copy = gimple_build_omp_ordered (s1);
1148 break;
1150 case GIMPLE_OMP_SECTION:
1151 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1152 copy = gimple_build_omp_section (s1);
1153 break;
1155 case GIMPLE_OMP_SECTIONS:
1156 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1157 copy = gimple_build_omp_sections
1158 (s1, gimple_omp_sections_clauses (stmt));
1159 break;
1161 case GIMPLE_OMP_SINGLE:
1162 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1163 copy = gimple_build_omp_single
1164 (s1, gimple_omp_single_clauses (stmt));
1165 break;
1167 case GIMPLE_OMP_CRITICAL:
1168 s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1169 copy
1170 = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1171 break;
1173 default:
1174 gcc_unreachable ();
1177 else
1179 if (gimple_assign_copy_p (stmt)
1180 && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1181 && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1183 /* Here we handle statements that are not completely rewritten.
1184 First we detect some inlining-induced bogosities for
1185 discarding. */
1187 /* Some assignments VAR = VAR; don't generate any rtl code
1188 and thus don't count as variable modification. Avoid
1189 keeping bogosities like 0 = 0. */
1190 tree decl = gimple_assign_lhs (stmt), value;
1191 tree *n;
1193 n = (tree *) pointer_map_contains (id->decl_map, decl);
1194 if (n)
1196 value = *n;
1197 STRIP_TYPE_NOPS (value);
1198 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1199 return gimple_build_nop ();
1203 /* Create a new deep copy of the statement. */
1204 copy = gimple_copy (stmt);
1207 /* If STMT has a block defined, map it to the newly constructed
1208 block. When inlining we want statements without a block to
1209 appear in the block of the function call. */
1210 new_block = id->block;
1211 if (gimple_block (copy))
1213 tree *n;
1214 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1215 gcc_assert (n);
1216 new_block = *n;
1219 gimple_set_block (copy, new_block);
1221 /* Remap all the operands in COPY. */
1222 memset (&wi, 0, sizeof (wi));
1223 wi.info = id;
1224 if (skip_first)
1225 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1226 else
1227 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1229 /* We have to handle EH region remapping of GIMPLE_RESX specially because
1230 the region number is not an operand. */
1231 if (gimple_code (stmt) == GIMPLE_RESX && id->eh_region_offset)
1233 gimple_resx_set_region (copy, gimple_resx_region (stmt) + id->eh_region_offset);
1235 return copy;
1239 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1240 later */
1242 static basic_block
1243 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1244 gcov_type count_scale)
1246 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1247 basic_block copy_basic_block;
1248 tree decl;
1250 /* create_basic_block() will append every new block to
1251 basic_block_info automatically. */
1252 copy_basic_block = create_basic_block (NULL, (void *) 0,
1253 (basic_block) bb->prev_bb->aux);
1254 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1256 /* We are going to rebuild frequencies from scratch. These values
1257 have just small importance to drive canonicalize_loop_headers. */
1258 copy_basic_block->frequency = ((gcov_type)bb->frequency
1259 * frequency_scale / REG_BR_PROB_BASE);
1261 if (copy_basic_block->frequency > BB_FREQ_MAX)
1262 copy_basic_block->frequency = BB_FREQ_MAX;
1264 copy_gsi = gsi_start_bb (copy_basic_block);
1266 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1268 gimple stmt = gsi_stmt (gsi);
1269 gimple orig_stmt = stmt;
1271 id->regimplify = false;
1272 stmt = remap_gimple_stmt (stmt, id);
1273 if (gimple_nop_p (stmt))
1274 continue;
1276 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1277 seq_gsi = copy_gsi;
1279 /* With return slot optimization we can end up with
1280 non-gimple (foo *)&this->m, fix that here. */
1281 if (is_gimple_assign (stmt)
1282 && gimple_assign_rhs_code (stmt) == NOP_EXPR
1283 && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1285 tree new_rhs;
1286 new_rhs = force_gimple_operand_gsi (&seq_gsi,
1287 gimple_assign_rhs1 (stmt),
1288 true, NULL, true, GSI_SAME_STMT);
1289 gimple_assign_set_rhs1 (stmt, new_rhs);
1290 id->regimplify = false;
1293 gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1295 if (id->regimplify)
1296 gimple_regimplify_operands (stmt, &seq_gsi);
1298 /* If copy_basic_block has been empty at the start of this iteration,
1299 call gsi_start_bb again to get at the newly added statements. */
1300 if (gsi_end_p (copy_gsi))
1301 copy_gsi = gsi_start_bb (copy_basic_block);
1302 else
1303 gsi_next (&copy_gsi);
1305 /* Process the new statement. The call to gimple_regimplify_operands
1306 possibly turned the statement into multiple statements, we
1307 need to process all of them. */
1310 stmt = gsi_stmt (copy_gsi);
1311 if (is_gimple_call (stmt)
1312 && gimple_call_va_arg_pack_p (stmt)
1313 && id->gimple_call)
1315 /* __builtin_va_arg_pack () should be replaced by
1316 all arguments corresponding to ... in the caller. */
1317 tree p;
1318 gimple new_call;
1319 VEC(tree, heap) *argarray;
1320 size_t nargs = gimple_call_num_args (id->gimple_call);
1321 size_t n;
1323 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1324 nargs--;
1326 /* Create the new array of arguments. */
1327 n = nargs + gimple_call_num_args (stmt);
1328 argarray = VEC_alloc (tree, heap, n);
1329 VEC_safe_grow (tree, heap, argarray, n);
1331 /* Copy all the arguments before '...' */
1332 memcpy (VEC_address (tree, argarray),
1333 gimple_call_arg_ptr (stmt, 0),
1334 gimple_call_num_args (stmt) * sizeof (tree));
1336 /* Append the arguments passed in '...' */
1337 memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1338 gimple_call_arg_ptr (id->gimple_call, 0)
1339 + (gimple_call_num_args (id->gimple_call) - nargs),
1340 nargs * sizeof (tree));
1342 new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1343 argarray);
1345 VEC_free (tree, heap, argarray);
1347 /* Copy all GIMPLE_CALL flags, location and block, except
1348 GF_CALL_VA_ARG_PACK. */
1349 gimple_call_copy_flags (new_call, stmt);
1350 gimple_call_set_va_arg_pack (new_call, false);
1351 gimple_set_location (new_call, gimple_location (stmt));
1352 gimple_set_block (new_call, gimple_block (stmt));
1353 gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1355 gsi_replace (&copy_gsi, new_call, false);
1356 gimple_set_bb (stmt, NULL);
1357 stmt = new_call;
1359 else if (is_gimple_call (stmt)
1360 && id->gimple_call
1361 && (decl = gimple_call_fndecl (stmt))
1362 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1363 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1365 /* __builtin_va_arg_pack_len () should be replaced by
1366 the number of anonymous arguments. */
1367 size_t nargs = gimple_call_num_args (id->gimple_call);
1368 tree count, p;
1369 gimple new_stmt;
1371 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1372 nargs--;
1374 count = build_int_cst (integer_type_node, nargs);
1375 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1376 gsi_replace (&copy_gsi, new_stmt, false);
1377 stmt = new_stmt;
1380 /* Statements produced by inlining can be unfolded, especially
1381 when we constant propagated some operands. We can't fold
1382 them right now for two reasons:
1383 1) folding require SSA_NAME_DEF_STMTs to be correct
1384 2) we can't change function calls to builtins.
1385 So we just mark statement for later folding. We mark
1386 all new statements, instead just statements that has changed
1387 by some nontrivial substitution so even statements made
1388 foldable indirectly are updated. If this turns out to be
1389 expensive, copy_body can be told to watch for nontrivial
1390 changes. */
1391 if (id->statements_to_fold)
1392 pointer_set_insert (id->statements_to_fold, stmt);
1394 /* We're duplicating a CALL_EXPR. Find any corresponding
1395 callgraph edges and update or duplicate them. */
1396 if (is_gimple_call (stmt))
1398 struct cgraph_node *node;
1399 struct cgraph_edge *edge;
1401 switch (id->transform_call_graph_edges)
1403 case CB_CGE_DUPLICATE:
1404 edge = cgraph_edge (id->src_node, orig_stmt);
1405 if (edge)
1406 cgraph_clone_edge (edge, id->dst_node, stmt,
1407 REG_BR_PROB_BASE, 1,
1408 edge->frequency, true);
1409 break;
1411 case CB_CGE_MOVE_CLONES:
1412 for (node = id->dst_node->next_clone;
1413 node;
1414 node = node->next_clone)
1416 edge = cgraph_edge (node, orig_stmt);
1417 if (edge)
1418 cgraph_set_call_stmt (edge, stmt);
1420 /* FALLTHRU */
1422 case CB_CGE_MOVE:
1423 edge = cgraph_edge (id->dst_node, orig_stmt);
1424 if (edge)
1425 cgraph_set_call_stmt (edge, stmt);
1426 break;
1428 default:
1429 gcc_unreachable ();
1433 /* If you think we can abort here, you are wrong.
1434 There is no region 0 in gimple. */
1435 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) != 0);
1437 if (stmt_could_throw_p (stmt)
1438 /* When we are cloning for inlining, we are supposed to
1439 construct a clone that calls precisely the same functions
1440 as original. However IPA optimizers might've proved
1441 earlier some function calls as non-trapping that might
1442 render some basic blocks dead that might become
1443 unreachable.
1445 We can't update SSA with unreachable blocks in CFG and thus
1446 we prevent the scenario by preserving even the "dead" eh
1447 edges until the point they are later removed by
1448 fixup_cfg pass. */
1449 || (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
1450 && lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) > 0))
1452 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
1454 /* Add an entry for the copied tree in the EH hashtable.
1455 When cloning or versioning, use the hashtable in
1456 cfun, and just copy the EH number. When inlining, use the
1457 hashtable in the caller, and adjust the region number. */
1458 if (region > 0)
1459 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
1461 /* If this tree doesn't have a region associated with it,
1462 and there is a "current region,"
1463 then associate this tree with the current region
1464 and add edges associated with this region. */
1465 if (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) <= 0
1466 && id->eh_region > 0
1467 && stmt_could_throw_p (stmt))
1468 add_stmt_to_eh_region (stmt, id->eh_region);
1471 if (gimple_in_ssa_p (cfun))
1473 ssa_op_iter i;
1474 tree def;
1476 find_new_referenced_vars (gsi_stmt (copy_gsi));
1477 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1478 if (TREE_CODE (def) == SSA_NAME)
1479 SSA_NAME_DEF_STMT (def) = stmt;
1482 gsi_next (&copy_gsi);
1484 while (!gsi_end_p (copy_gsi));
1486 copy_gsi = gsi_last_bb (copy_basic_block);
1489 return copy_basic_block;
1492 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1493 form is quite easy, since dominator relationship for old basic blocks does
1494 not change.
1496 There is however exception where inlining might change dominator relation
1497 across EH edges from basic block within inlined functions destinating
1498 to landing pads in function we inline into.
1500 The function fills in PHI_RESULTs of such PHI nodes if they refer
1501 to gimple regs. Otherwise, the function mark PHI_RESULT of such
1502 PHI nodes for renaming. For non-gimple regs, renaming is safe: the
1503 EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1504 set, and this means that there will be no overlapping live ranges
1505 for the underlying symbol.
1507 This might change in future if we allow redirecting of EH edges and
1508 we might want to change way build CFG pre-inlining to include
1509 all the possible edges then. */
1510 static void
1511 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1512 bool can_throw, bool nonlocal_goto)
1514 edge e;
1515 edge_iterator ei;
1517 FOR_EACH_EDGE (e, ei, bb->succs)
1518 if (!e->dest->aux
1519 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1521 gimple phi;
1522 gimple_stmt_iterator si;
1524 gcc_assert (e->flags & EDGE_ABNORMAL);
1526 if (!nonlocal_goto)
1527 gcc_assert (e->flags & EDGE_EH);
1529 if (!can_throw)
1530 gcc_assert (!(e->flags & EDGE_EH));
1532 for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1534 edge re;
1536 phi = gsi_stmt (si);
1538 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1539 gcc_assert (!e->dest->aux);
1541 gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1543 if (!is_gimple_reg (PHI_RESULT (phi)))
1545 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1546 continue;
1549 re = find_edge (ret_bb, e->dest);
1550 gcc_assert (re);
1551 gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1552 == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1554 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1555 USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1561 /* Copy edges from BB into its copy constructed earlier, scale profile
1562 accordingly. Edges will be taken care of later. Assume aux
1563 pointers to point to the copies of each BB. */
1565 static void
1566 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1568 basic_block new_bb = (basic_block) bb->aux;
1569 edge_iterator ei;
1570 edge old_edge;
1571 gimple_stmt_iterator si;
1572 int flags;
1574 /* Use the indices from the original blocks to create edges for the
1575 new ones. */
1576 FOR_EACH_EDGE (old_edge, ei, bb->succs)
1577 if (!(old_edge->flags & EDGE_EH))
1579 edge new_edge;
1581 flags = old_edge->flags;
1583 /* Return edges do get a FALLTHRU flag when the get inlined. */
1584 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1585 && old_edge->dest->aux != EXIT_BLOCK_PTR)
1586 flags |= EDGE_FALLTHRU;
1587 new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1588 new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1589 new_edge->probability = old_edge->probability;
1592 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1593 return;
1595 for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1597 gimple copy_stmt;
1598 bool can_throw, nonlocal_goto;
1600 copy_stmt = gsi_stmt (si);
1601 update_stmt (copy_stmt);
1602 if (gimple_in_ssa_p (cfun))
1603 mark_symbols_for_renaming (copy_stmt);
1605 /* Do this before the possible split_block. */
1606 gsi_next (&si);
1608 /* If this tree could throw an exception, there are two
1609 cases where we need to add abnormal edge(s): the
1610 tree wasn't in a region and there is a "current
1611 region" in the caller; or the original tree had
1612 EH edges. In both cases split the block after the tree,
1613 and add abnormal edge(s) as needed; we need both
1614 those from the callee and the caller.
1615 We check whether the copy can throw, because the const
1616 propagation can change an INDIRECT_REF which throws
1617 into a COMPONENT_REF which doesn't. If the copy
1618 can throw, the original could also throw. */
1619 can_throw = stmt_can_throw_internal (copy_stmt);
1620 nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1622 if (can_throw || nonlocal_goto)
1624 if (!gsi_end_p (si))
1625 /* Note that bb's predecessor edges aren't necessarily
1626 right at this point; split_block doesn't care. */
1628 edge e = split_block (new_bb, copy_stmt);
1630 new_bb = e->dest;
1631 new_bb->aux = e->src->aux;
1632 si = gsi_start_bb (new_bb);
1636 if (can_throw)
1637 make_eh_edges (copy_stmt);
1639 if (nonlocal_goto)
1640 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1642 if ((can_throw || nonlocal_goto)
1643 && gimple_in_ssa_p (cfun))
1644 update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1645 can_throw, nonlocal_goto);
1649 /* Copy the PHIs. All blocks and edges are copied, some blocks
1650 was possibly split and new outgoing EH edges inserted.
1651 BB points to the block of original function and AUX pointers links
1652 the original and newly copied blocks. */
1654 static void
1655 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1657 basic_block const new_bb = (basic_block) bb->aux;
1658 edge_iterator ei;
1659 gimple phi;
1660 gimple_stmt_iterator si;
1662 for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1664 tree res, new_res;
1665 gimple new_phi;
1666 edge new_edge;
1668 phi = gsi_stmt (si);
1669 res = PHI_RESULT (phi);
1670 new_res = res;
1671 if (is_gimple_reg (res))
1673 walk_tree (&new_res, copy_tree_body_r, id, NULL);
1674 SSA_NAME_DEF_STMT (new_res)
1675 = new_phi = create_phi_node (new_res, new_bb);
1676 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1678 edge const old_edge
1679 = find_edge ((basic_block) new_edge->src->aux, bb);
1680 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1681 tree new_arg = arg;
1682 tree block = id->block;
1683 id->block = NULL_TREE;
1684 walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1685 id->block = block;
1686 gcc_assert (new_arg);
1687 /* With return slot optimization we can end up with
1688 non-gimple (foo *)&this->m, fix that here. */
1689 if (TREE_CODE (new_arg) != SSA_NAME
1690 && TREE_CODE (new_arg) != FUNCTION_DECL
1691 && !is_gimple_val (new_arg))
1693 gimple_seq stmts = NULL;
1694 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1695 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1697 add_phi_arg (new_phi, new_arg, new_edge);
1704 /* Wrapper for remap_decl so it can be used as a callback. */
1706 static tree
1707 remap_decl_1 (tree decl, void *data)
1709 return remap_decl (decl, (copy_body_data *) data);
1712 /* Build struct function and associated datastructures for the new clone
1713 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1715 static void
1716 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1717 int frequency)
1719 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1720 gcov_type count_scale, frequency_scale;
1722 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1723 count_scale = (REG_BR_PROB_BASE * count
1724 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1725 else
1726 count_scale = 1;
1728 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1729 frequency_scale = (REG_BR_PROB_BASE * frequency
1731 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1732 else
1733 frequency_scale = count_scale;
1735 /* Register specific tree functions. */
1736 gimple_register_cfg_hooks ();
1738 /* Get clean struct function. */
1739 push_struct_function (new_fndecl);
1741 /* We will rebuild these, so just sanity check that they are empty. */
1742 gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
1743 gcc_assert (cfun->local_decls == NULL);
1744 gcc_assert (cfun->cfg == NULL);
1745 gcc_assert (cfun->decl == new_fndecl);
1747 /* No need to copy; this is initialized later in compilation. */
1748 gcc_assert (!src_cfun->calls_setjmp);
1749 gcc_assert (!src_cfun->calls_alloca);
1751 /* Copy items we preserve during clonning. */
1752 cfun->static_chain_decl = src_cfun->static_chain_decl;
1753 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
1754 cfun->function_end_locus = src_cfun->function_end_locus;
1755 cfun->curr_properties = src_cfun->curr_properties;
1756 cfun->last_verified = src_cfun->last_verified;
1757 if (src_cfun->ipa_transforms_to_apply)
1758 cfun->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
1759 src_cfun->ipa_transforms_to_apply);
1760 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
1761 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
1762 cfun->function_frequency = src_cfun->function_frequency;
1763 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
1764 cfun->stdarg = src_cfun->stdarg;
1765 cfun->dont_save_pending_sizes_p = src_cfun->dont_save_pending_sizes_p;
1766 cfun->after_inlining = src_cfun->after_inlining;
1767 cfun->returns_struct = src_cfun->returns_struct;
1768 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
1769 cfun->after_tree_profile = src_cfun->after_tree_profile;
1771 init_empty_tree_cfg ();
1773 ENTRY_BLOCK_PTR->count =
1774 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1775 REG_BR_PROB_BASE);
1776 ENTRY_BLOCK_PTR->frequency =
1777 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1778 frequency_scale / REG_BR_PROB_BASE);
1779 EXIT_BLOCK_PTR->count =
1780 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1781 REG_BR_PROB_BASE);
1782 EXIT_BLOCK_PTR->frequency =
1783 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1784 frequency_scale / REG_BR_PROB_BASE);
1785 if (src_cfun->eh)
1786 init_eh_for_function ();
1788 if (src_cfun->gimple_df)
1790 init_tree_ssa (cfun);
1791 cfun->gimple_df->in_ssa_p = true;
1792 init_ssa_operands ();
1794 pop_cfun ();
1797 /* Make a copy of the body of FN so that it can be inserted inline in
1798 another function. Walks FN via CFG, returns new fndecl. */
1800 static tree
1801 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1802 basic_block entry_block_map, basic_block exit_block_map)
1804 tree callee_fndecl = id->src_fn;
1805 /* Original cfun for the callee, doesn't change. */
1806 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1807 struct function *cfun_to_copy;
1808 basic_block bb;
1809 tree new_fndecl = NULL;
1810 gcov_type count_scale, frequency_scale;
1811 int last;
1813 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1814 count_scale = (REG_BR_PROB_BASE * count
1815 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1816 else
1817 count_scale = 1;
1819 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1820 frequency_scale = (REG_BR_PROB_BASE * frequency
1822 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1823 else
1824 frequency_scale = count_scale;
1826 /* Register specific tree functions. */
1827 gimple_register_cfg_hooks ();
1829 /* Must have a CFG here at this point. */
1830 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1831 (DECL_STRUCT_FUNCTION (callee_fndecl)));
1833 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1835 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1836 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1837 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1838 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1840 /* Duplicate any exception-handling regions. */
1841 if (cfun->eh)
1843 id->eh_region_offset
1844 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1845 0, id->eh_region);
1848 /* Use aux pointers to map the original blocks to copy. */
1849 FOR_EACH_BB_FN (bb, cfun_to_copy)
1851 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
1852 bb->aux = new_bb;
1853 new_bb->aux = bb;
1856 last = last_basic_block;
1858 /* Now that we've duplicated the blocks, duplicate their edges. */
1859 FOR_ALL_BB_FN (bb, cfun_to_copy)
1860 copy_edges_for_bb (bb, count_scale, exit_block_map);
1862 if (gimple_in_ssa_p (cfun))
1863 FOR_ALL_BB_FN (bb, cfun_to_copy)
1864 copy_phis_for_bb (bb, id);
1866 FOR_ALL_BB_FN (bb, cfun_to_copy)
1868 ((basic_block)bb->aux)->aux = NULL;
1869 bb->aux = NULL;
1872 /* Zero out AUX fields of newly created block during EH edge
1873 insertion. */
1874 for (; last < last_basic_block; last++)
1875 BASIC_BLOCK (last)->aux = NULL;
1876 entry_block_map->aux = NULL;
1877 exit_block_map->aux = NULL;
1879 return new_fndecl;
1882 static tree
1883 copy_body (copy_body_data *id, gcov_type count, int frequency,
1884 basic_block entry_block_map, basic_block exit_block_map)
1886 tree fndecl = id->src_fn;
1887 tree body;
1889 /* If this body has a CFG, walk CFG and copy. */
1890 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1891 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1893 return body;
1896 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1897 defined in function FN, or of a data member thereof. */
1899 static bool
1900 self_inlining_addr_expr (tree value, tree fn)
1902 tree var;
1904 if (TREE_CODE (value) != ADDR_EXPR)
1905 return false;
1907 var = get_base_address (TREE_OPERAND (value, 0));
1909 return var && auto_var_in_fn_p (var, fn);
1912 static void
1913 insert_init_stmt (basic_block bb, gimple init_stmt)
1915 /* If VAR represents a zero-sized variable, it's possible that the
1916 assignment statement may result in no gimple statements. */
1917 if (init_stmt)
1919 gimple_stmt_iterator si = gsi_last_bb (bb);
1921 /* We can end up with init statements that store to a non-register
1922 from a rhs with a conversion. Handle that here by forcing the
1923 rhs into a temporary. gimple_regimplify_operands is not
1924 prepared to do this for us. */
1925 if (!is_gimple_reg (gimple_assign_lhs (init_stmt))
1926 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
1927 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
1929 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
1930 gimple_expr_type (init_stmt),
1931 gimple_assign_rhs1 (init_stmt));
1932 rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
1933 GSI_NEW_STMT);
1934 gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
1935 gimple_assign_set_rhs1 (init_stmt, rhs);
1937 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
1938 gimple_regimplify_operands (init_stmt, &si);
1939 mark_symbols_for_renaming (init_stmt);
1943 /* Initialize parameter P with VALUE. If needed, produce init statement
1944 at the end of BB. When BB is NULL, we return init statement to be
1945 output later. */
1946 static gimple
1947 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1948 basic_block bb, tree *vars)
1950 gimple init_stmt = NULL;
1951 tree var;
1952 tree rhs = value;
1953 tree def = (gimple_in_ssa_p (cfun)
1954 ? gimple_default_def (id->src_cfun, p) : NULL);
1956 if (value
1957 && value != error_mark_node
1958 && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
1960 if (fold_convertible_p (TREE_TYPE (p), value))
1961 rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
1962 else
1963 /* ??? For valid (GIMPLE) programs we should not end up here.
1964 Still if something has gone wrong and we end up with truly
1965 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
1966 to not leak invalid GIMPLE to the following passes. */
1967 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
1970 /* If the parameter is never assigned to, has no SSA_NAMEs created,
1971 we may not need to create a new variable here at all. Instead, we may
1972 be able to just use the argument value. */
1973 if (TREE_READONLY (p)
1974 && !TREE_ADDRESSABLE (p)
1975 && value && !TREE_SIDE_EFFECTS (value)
1976 && !def)
1978 /* We may produce non-gimple trees by adding NOPs or introduce
1979 invalid sharing when operand is not really constant.
1980 It is not big deal to prohibit constant propagation here as
1981 we will constant propagate in DOM1 pass anyway. */
1982 if (is_gimple_min_invariant (value)
1983 && useless_type_conversion_p (TREE_TYPE (p),
1984 TREE_TYPE (value))
1985 /* We have to be very careful about ADDR_EXPR. Make sure
1986 the base variable isn't a local variable of the inlined
1987 function, e.g., when doing recursive inlining, direct or
1988 mutually-recursive or whatever, which is why we don't
1989 just test whether fn == current_function_decl. */
1990 && ! self_inlining_addr_expr (value, fn))
1992 insert_decl_map (id, p, value);
1993 return NULL;
1997 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1998 here since the type of this decl must be visible to the calling
1999 function. */
2000 var = copy_decl_to_var (p, id);
2001 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2003 get_var_ann (var);
2004 add_referenced_var (var);
2007 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2008 that way, when the PARM_DECL is encountered, it will be
2009 automatically replaced by the VAR_DECL. */
2010 insert_decl_map (id, p, var);
2012 /* Declare this new variable. */
2013 TREE_CHAIN (var) = *vars;
2014 *vars = var;
2016 /* Make gimplifier happy about this variable. */
2017 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2019 /* Even if P was TREE_READONLY, the new VAR should not be.
2020 In the original code, we would have constructed a
2021 temporary, and then the function body would have never
2022 changed the value of P. However, now, we will be
2023 constructing VAR directly. The constructor body may
2024 change its value multiple times as it is being
2025 constructed. Therefore, it must not be TREE_READONLY;
2026 the back-end assumes that TREE_READONLY variable is
2027 assigned to only once. */
2028 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2029 TREE_READONLY (var) = 0;
2031 /* If there is no setup required and we are in SSA, take the easy route
2032 replacing all SSA names representing the function parameter by the
2033 SSA name passed to function.
2035 We need to construct map for the variable anyway as it might be used
2036 in different SSA names when parameter is set in function.
2038 FIXME: This usually kills the last connection in between inlined
2039 function parameter and the actual value in debug info. Can we do
2040 better here? If we just inserted the statement, copy propagation
2041 would kill it anyway as it always did in older versions of GCC.
2043 We might want to introduce a notion that single SSA_NAME might
2044 represent multiple variables for purposes of debugging. */
2045 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2046 && (TREE_CODE (rhs) == SSA_NAME
2047 || is_gimple_min_invariant (rhs))
2048 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2050 insert_decl_map (id, def, rhs);
2051 return NULL;
2054 /* If the value of argument is never used, don't care about initializing
2055 it. */
2056 if (gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2058 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2059 return NULL;
2062 /* Initialize this VAR_DECL from the equivalent argument. Convert
2063 the argument to the proper type in case it was promoted. */
2064 if (value)
2066 if (rhs == error_mark_node)
2068 insert_decl_map (id, p, var);
2069 return NULL;
2072 STRIP_USELESS_TYPE_CONVERSION (rhs);
2074 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2075 keep our trees in gimple form. */
2076 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2078 def = remap_ssa_name (def, id);
2079 init_stmt = gimple_build_assign (def, rhs);
2080 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2081 set_default_def (var, NULL);
2083 else
2084 init_stmt = gimple_build_assign (var, rhs);
2086 if (bb && init_stmt)
2087 insert_init_stmt (bb, init_stmt);
2089 return init_stmt;
2092 /* Generate code to initialize the parameters of the function at the
2093 top of the stack in ID from the GIMPLE_CALL STMT. */
2095 static void
2096 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2097 tree fn, basic_block bb)
2099 tree parms;
2100 size_t i;
2101 tree p;
2102 tree vars = NULL_TREE;
2103 tree static_chain = gimple_call_chain (stmt);
2105 /* Figure out what the parameters are. */
2106 parms = DECL_ARGUMENTS (fn);
2108 /* Loop through the parameter declarations, replacing each with an
2109 equivalent VAR_DECL, appropriately initialized. */
2110 for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2112 tree val;
2113 val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2114 setup_one_parameter (id, p, val, fn, bb, &vars);
2117 /* Initialize the static chain. */
2118 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2119 gcc_assert (fn != current_function_decl);
2120 if (p)
2122 /* No static chain? Seems like a bug in tree-nested.c. */
2123 gcc_assert (static_chain);
2125 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2128 declare_inline_vars (id->block, vars);
2132 /* Declare a return variable to replace the RESULT_DECL for the
2133 function we are calling. An appropriate DECL_STMT is returned.
2134 The USE_STMT is filled to contain a use of the declaration to
2135 indicate the return value of the function.
2137 RETURN_SLOT, if non-null is place where to store the result. It
2138 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2139 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2141 The return value is a (possibly null) value that is the result of the
2142 function as seen by the callee. *USE_P is a (possibly null) value that
2143 holds the result as seen by the caller. */
2145 static tree
2146 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
2147 tree *use_p)
2149 tree callee = id->src_fn;
2150 tree caller = id->dst_fn;
2151 tree result = DECL_RESULT (callee);
2152 tree callee_type = TREE_TYPE (result);
2153 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
2154 tree var, use;
2156 /* We don't need to do anything for functions that don't return
2157 anything. */
2158 if (!result || VOID_TYPE_P (callee_type))
2160 *use_p = NULL_TREE;
2161 return NULL_TREE;
2164 /* If there was a return slot, then the return value is the
2165 dereferenced address of that object. */
2166 if (return_slot)
2168 /* The front end shouldn't have used both return_slot and
2169 a modify expression. */
2170 gcc_assert (!modify_dest);
2171 if (DECL_BY_REFERENCE (result))
2173 tree return_slot_addr = build_fold_addr_expr (return_slot);
2174 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2176 /* We are going to construct *&return_slot and we can't do that
2177 for variables believed to be not addressable.
2179 FIXME: This check possibly can match, because values returned
2180 via return slot optimization are not believed to have address
2181 taken by alias analysis. */
2182 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2183 if (gimple_in_ssa_p (cfun))
2185 HOST_WIDE_INT bitsize;
2186 HOST_WIDE_INT bitpos;
2187 tree offset;
2188 enum machine_mode mode;
2189 int unsignedp;
2190 int volatilep;
2191 tree base;
2192 base = get_inner_reference (return_slot, &bitsize, &bitpos,
2193 &offset,
2194 &mode, &unsignedp, &volatilep,
2195 false);
2196 if (TREE_CODE (base) == INDIRECT_REF)
2197 base = TREE_OPERAND (base, 0);
2198 if (TREE_CODE (base) == SSA_NAME)
2199 base = SSA_NAME_VAR (base);
2200 mark_sym_for_renaming (base);
2202 var = return_slot_addr;
2204 else
2206 var = return_slot;
2207 gcc_assert (TREE_CODE (var) != SSA_NAME);
2208 TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2210 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2211 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2212 && !DECL_GIMPLE_REG_P (result)
2213 && DECL_P (var))
2214 DECL_GIMPLE_REG_P (var) = 0;
2215 use = NULL;
2216 goto done;
2219 /* All types requiring non-trivial constructors should have been handled. */
2220 gcc_assert (!TREE_ADDRESSABLE (callee_type));
2222 /* Attempt to avoid creating a new temporary variable. */
2223 if (modify_dest
2224 && TREE_CODE (modify_dest) != SSA_NAME)
2226 bool use_it = false;
2228 /* We can't use MODIFY_DEST if there's type promotion involved. */
2229 if (!useless_type_conversion_p (callee_type, caller_type))
2230 use_it = false;
2232 /* ??? If we're assigning to a variable sized type, then we must
2233 reuse the destination variable, because we've no good way to
2234 create variable sized temporaries at this point. */
2235 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2236 use_it = true;
2238 /* If the callee cannot possibly modify MODIFY_DEST, then we can
2239 reuse it as the result of the call directly. Don't do this if
2240 it would promote MODIFY_DEST to addressable. */
2241 else if (TREE_ADDRESSABLE (result))
2242 use_it = false;
2243 else
2245 tree base_m = get_base_address (modify_dest);
2247 /* If the base isn't a decl, then it's a pointer, and we don't
2248 know where that's going to go. */
2249 if (!DECL_P (base_m))
2250 use_it = false;
2251 else if (is_global_var (base_m))
2252 use_it = false;
2253 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2254 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2255 && !DECL_GIMPLE_REG_P (result)
2256 && DECL_GIMPLE_REG_P (base_m))
2257 use_it = false;
2258 else if (!TREE_ADDRESSABLE (base_m))
2259 use_it = true;
2262 if (use_it)
2264 var = modify_dest;
2265 use = NULL;
2266 goto done;
2270 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2272 var = copy_result_decl_to_var (result, id);
2273 if (gimple_in_ssa_p (cfun))
2275 get_var_ann (var);
2276 add_referenced_var (var);
2279 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2280 DECL_STRUCT_FUNCTION (caller)->local_decls
2281 = tree_cons (NULL_TREE, var,
2282 DECL_STRUCT_FUNCTION (caller)->local_decls);
2284 /* Do not have the rest of GCC warn about this variable as it should
2285 not be visible to the user. */
2286 TREE_NO_WARNING (var) = 1;
2288 declare_inline_vars (id->block, var);
2290 /* Build the use expr. If the return type of the function was
2291 promoted, convert it back to the expected type. */
2292 use = var;
2293 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2294 use = fold_convert (caller_type, var);
2296 STRIP_USELESS_TYPE_CONVERSION (use);
2298 if (DECL_BY_REFERENCE (result))
2299 var = build_fold_addr_expr (var);
2301 done:
2302 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2303 way, when the RESULT_DECL is encountered, it will be
2304 automatically replaced by the VAR_DECL. */
2305 insert_decl_map (id, result, var);
2307 /* Remember this so we can ignore it in remap_decls. */
2308 id->retvar = var;
2310 *use_p = use;
2311 return var;
2314 /* Returns nonzero if a function can be inlined as a tree. */
2316 bool
2317 tree_inlinable_function_p (tree fn)
2319 return inlinable_function_p (fn);
2322 static const char *inline_forbidden_reason;
2324 /* A callback for walk_gimple_seq to handle tree operands. Returns
2325 NULL_TREE if a function can be inlined, otherwise sets the reason
2326 why not and returns a tree representing the offending operand. */
2328 static tree
2329 inline_forbidden_p_op (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
2330 void *fnp ATTRIBUTE_UNUSED)
2332 tree node = *nodep;
2333 tree t;
2335 if (TREE_CODE (node) == RECORD_TYPE || TREE_CODE (node) == UNION_TYPE)
2337 /* We cannot inline a function of the form
2339 void F (int i) { struct S { int ar[i]; } s; }
2341 Attempting to do so produces a catch-22.
2342 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
2343 UNION_TYPE nodes, then it goes into infinite recursion on a
2344 structure containing a pointer to its own type. If it doesn't,
2345 then the type node for S doesn't get adjusted properly when
2346 F is inlined.
2348 ??? This is likely no longer true, but it's too late in the 4.0
2349 cycle to try to find out. This should be checked for 4.1. */
2350 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
2351 if (variably_modified_type_p (TREE_TYPE (t), NULL))
2353 inline_forbidden_reason
2354 = G_("function %q+F can never be inlined "
2355 "because it uses variable sized variables");
2356 return node;
2360 return NULL_TREE;
2364 /* A callback for walk_gimple_seq to handle statements. Returns
2365 non-NULL iff a function can not be inlined. Also sets the reason
2366 why. */
2368 static tree
2369 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2370 struct walk_stmt_info *wip)
2372 tree fn = (tree) wip->info;
2373 tree t;
2374 gimple stmt = gsi_stmt (*gsi);
2376 switch (gimple_code (stmt))
2378 case GIMPLE_CALL:
2379 /* Refuse to inline alloca call unless user explicitly forced so as
2380 this may change program's memory overhead drastically when the
2381 function using alloca is called in loop. In GCC present in
2382 SPEC2000 inlining into schedule_block cause it to require 2GB of
2383 RAM instead of 256MB. */
2384 if (gimple_alloca_call_p (stmt)
2385 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2387 inline_forbidden_reason
2388 = G_("function %q+F can never be inlined because it uses "
2389 "alloca (override using the always_inline attribute)");
2390 *handled_ops_p = true;
2391 return fn;
2394 t = gimple_call_fndecl (stmt);
2395 if (t == NULL_TREE)
2396 break;
2398 /* We cannot inline functions that call setjmp. */
2399 if (setjmp_call_p (t))
2401 inline_forbidden_reason
2402 = G_("function %q+F can never be inlined because it uses setjmp");
2403 *handled_ops_p = true;
2404 return t;
2407 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
2408 switch (DECL_FUNCTION_CODE (t))
2410 /* We cannot inline functions that take a variable number of
2411 arguments. */
2412 case BUILT_IN_VA_START:
2413 case BUILT_IN_NEXT_ARG:
2414 case BUILT_IN_VA_END:
2415 inline_forbidden_reason
2416 = G_("function %q+F can never be inlined because it "
2417 "uses variable argument lists");
2418 *handled_ops_p = true;
2419 return t;
2421 case BUILT_IN_LONGJMP:
2422 /* We can't inline functions that call __builtin_longjmp at
2423 all. The non-local goto machinery really requires the
2424 destination be in a different function. If we allow the
2425 function calling __builtin_longjmp to be inlined into the
2426 function calling __builtin_setjmp, Things will Go Awry. */
2427 inline_forbidden_reason
2428 = G_("function %q+F can never be inlined because "
2429 "it uses setjmp-longjmp exception handling");
2430 *handled_ops_p = true;
2431 return t;
2433 case BUILT_IN_NONLOCAL_GOTO:
2434 /* Similarly. */
2435 inline_forbidden_reason
2436 = G_("function %q+F can never be inlined because "
2437 "it uses non-local goto");
2438 *handled_ops_p = true;
2439 return t;
2441 case BUILT_IN_RETURN:
2442 case BUILT_IN_APPLY_ARGS:
2443 /* If a __builtin_apply_args caller would be inlined,
2444 it would be saving arguments of the function it has
2445 been inlined into. Similarly __builtin_return would
2446 return from the function the inline has been inlined into. */
2447 inline_forbidden_reason
2448 = G_("function %q+F can never be inlined because "
2449 "it uses __builtin_return or __builtin_apply_args");
2450 *handled_ops_p = true;
2451 return t;
2453 default:
2454 break;
2456 break;
2458 case GIMPLE_GOTO:
2459 t = gimple_goto_dest (stmt);
2461 /* We will not inline a function which uses computed goto. The
2462 addresses of its local labels, which may be tucked into
2463 global storage, are of course not constant across
2464 instantiations, which causes unexpected behavior. */
2465 if (TREE_CODE (t) != LABEL_DECL)
2467 inline_forbidden_reason
2468 = G_("function %q+F can never be inlined "
2469 "because it contains a computed goto");
2470 *handled_ops_p = true;
2471 return t;
2473 break;
2475 case GIMPLE_LABEL:
2476 t = gimple_label_label (stmt);
2477 if (DECL_NONLOCAL (t))
2479 /* We cannot inline a function that receives a non-local goto
2480 because we cannot remap the destination label used in the
2481 function that is performing the non-local goto. */
2482 inline_forbidden_reason
2483 = G_("function %q+F can never be inlined "
2484 "because it receives a non-local goto");
2485 *handled_ops_p = true;
2486 return t;
2488 break;
2490 default:
2491 break;
2494 *handled_ops_p = false;
2495 return NULL_TREE;
2499 static tree
2500 inline_forbidden_p_2 (tree *nodep, int *walk_subtrees,
2501 void *fnp)
2503 tree node = *nodep;
2504 tree fn = (tree) fnp;
2506 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2508 inline_forbidden_reason
2509 = G_("function %q+F can never be inlined "
2510 "because it saves address of local label in a static variable");
2511 return node;
2514 if (TYPE_P (node))
2515 *walk_subtrees = 0;
2517 return NULL_TREE;
2520 /* Return true if FNDECL is a function that cannot be inlined into
2521 another one. */
2523 static bool
2524 inline_forbidden_p (tree fndecl)
2526 location_t saved_loc = input_location;
2527 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2528 tree step;
2529 struct walk_stmt_info wi;
2530 struct pointer_set_t *visited_nodes;
2531 basic_block bb;
2532 bool forbidden_p = false;
2534 visited_nodes = pointer_set_create ();
2535 memset (&wi, 0, sizeof (wi));
2536 wi.info = (void *) fndecl;
2537 wi.pset = visited_nodes;
2539 FOR_EACH_BB_FN (bb, fun)
2541 gimple ret;
2542 gimple_seq seq = bb_seq (bb);
2543 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt,
2544 inline_forbidden_p_op, &wi);
2545 forbidden_p = (ret != NULL);
2546 if (forbidden_p)
2547 goto egress;
2550 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2552 tree decl = TREE_VALUE (step);
2553 if (TREE_CODE (decl) == VAR_DECL
2554 && TREE_STATIC (decl)
2555 && !DECL_EXTERNAL (decl)
2556 && DECL_INITIAL (decl))
2558 tree ret;
2559 ret = walk_tree_without_duplicates (&DECL_INITIAL (decl),
2560 inline_forbidden_p_2, fndecl);
2561 forbidden_p = (ret != NULL);
2562 if (forbidden_p)
2563 goto egress;
2567 egress:
2568 pointer_set_destroy (visited_nodes);
2569 input_location = saved_loc;
2570 return forbidden_p;
2573 /* Returns nonzero if FN is a function that does not have any
2574 fundamental inline blocking properties. */
2576 static bool
2577 inlinable_function_p (tree fn)
2579 bool inlinable = true;
2580 bool do_warning;
2581 tree always_inline;
2583 /* If we've already decided this function shouldn't be inlined,
2584 there's no need to check again. */
2585 if (DECL_UNINLINABLE (fn))
2586 return false;
2588 /* We only warn for functions declared `inline' by the user. */
2589 do_warning = (warn_inline
2590 && DECL_DECLARED_INLINE_P (fn)
2591 && !DECL_NO_INLINE_WARNING_P (fn)
2592 && !DECL_IN_SYSTEM_HEADER (fn));
2594 always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
2596 if (flag_no_inline
2597 && always_inline == NULL)
2599 if (do_warning)
2600 warning (OPT_Winline, "function %q+F can never be inlined because it "
2601 "is suppressed using -fno-inline", fn);
2602 inlinable = false;
2605 /* Don't auto-inline anything that might not be bound within
2606 this unit of translation. */
2607 else if (!DECL_DECLARED_INLINE_P (fn)
2608 && DECL_REPLACEABLE_P (fn))
2609 inlinable = false;
2611 else if (!function_attribute_inlinable_p (fn))
2613 if (do_warning)
2614 warning (OPT_Winline, "function %q+F can never be inlined because it "
2615 "uses attributes conflicting with inlining", fn);
2616 inlinable = false;
2619 else if (inline_forbidden_p (fn))
2621 /* See if we should warn about uninlinable functions. Previously,
2622 some of these warnings would be issued while trying to expand
2623 the function inline, but that would cause multiple warnings
2624 about functions that would for example call alloca. But since
2625 this a property of the function, just one warning is enough.
2626 As a bonus we can now give more details about the reason why a
2627 function is not inlinable. */
2628 if (always_inline)
2629 sorry (inline_forbidden_reason, fn);
2630 else if (do_warning)
2631 warning (OPT_Winline, inline_forbidden_reason, fn);
2633 inlinable = false;
2636 /* Squirrel away the result so that we don't have to check again. */
2637 DECL_UNINLINABLE (fn) = !inlinable;
2639 return inlinable;
2642 /* Estimate the cost of a memory move. Use machine dependent
2643 word size and take possible memcpy call into account. */
2646 estimate_move_cost (tree type)
2648 HOST_WIDE_INT size;
2650 size = int_size_in_bytes (type);
2652 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
2653 /* Cost of a memcpy call, 3 arguments and the call. */
2654 return 4;
2655 else
2656 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
2659 /* Returns cost of operation CODE, according to WEIGHTS */
2661 static int
2662 estimate_operator_cost (enum tree_code code, eni_weights *weights)
2664 switch (code)
2666 /* These are "free" conversions, or their presumed cost
2667 is folded into other operations. */
2668 case RANGE_EXPR:
2669 CASE_CONVERT:
2670 case COMPLEX_EXPR:
2671 case PAREN_EXPR:
2672 return 0;
2674 /* Assign cost of 1 to usual operations.
2675 ??? We may consider mapping RTL costs to this. */
2676 case COND_EXPR:
2677 case VEC_COND_EXPR:
2679 case PLUS_EXPR:
2680 case POINTER_PLUS_EXPR:
2681 case MINUS_EXPR:
2682 case MULT_EXPR:
2684 case FIXED_CONVERT_EXPR:
2685 case FIX_TRUNC_EXPR:
2687 case NEGATE_EXPR:
2688 case FLOAT_EXPR:
2689 case MIN_EXPR:
2690 case MAX_EXPR:
2691 case ABS_EXPR:
2693 case LSHIFT_EXPR:
2694 case RSHIFT_EXPR:
2695 case LROTATE_EXPR:
2696 case RROTATE_EXPR:
2697 case VEC_LSHIFT_EXPR:
2698 case VEC_RSHIFT_EXPR:
2700 case BIT_IOR_EXPR:
2701 case BIT_XOR_EXPR:
2702 case BIT_AND_EXPR:
2703 case BIT_NOT_EXPR:
2705 case TRUTH_ANDIF_EXPR:
2706 case TRUTH_ORIF_EXPR:
2707 case TRUTH_AND_EXPR:
2708 case TRUTH_OR_EXPR:
2709 case TRUTH_XOR_EXPR:
2710 case TRUTH_NOT_EXPR:
2712 case LT_EXPR:
2713 case LE_EXPR:
2714 case GT_EXPR:
2715 case GE_EXPR:
2716 case EQ_EXPR:
2717 case NE_EXPR:
2718 case ORDERED_EXPR:
2719 case UNORDERED_EXPR:
2721 case UNLT_EXPR:
2722 case UNLE_EXPR:
2723 case UNGT_EXPR:
2724 case UNGE_EXPR:
2725 case UNEQ_EXPR:
2726 case LTGT_EXPR:
2728 case CONJ_EXPR:
2730 case PREDECREMENT_EXPR:
2731 case PREINCREMENT_EXPR:
2732 case POSTDECREMENT_EXPR:
2733 case POSTINCREMENT_EXPR:
2735 case REALIGN_LOAD_EXPR:
2737 case REDUC_MAX_EXPR:
2738 case REDUC_MIN_EXPR:
2739 case REDUC_PLUS_EXPR:
2740 case WIDEN_SUM_EXPR:
2741 case WIDEN_MULT_EXPR:
2742 case DOT_PROD_EXPR:
2744 case VEC_WIDEN_MULT_HI_EXPR:
2745 case VEC_WIDEN_MULT_LO_EXPR:
2746 case VEC_UNPACK_HI_EXPR:
2747 case VEC_UNPACK_LO_EXPR:
2748 case VEC_UNPACK_FLOAT_HI_EXPR:
2749 case VEC_UNPACK_FLOAT_LO_EXPR:
2750 case VEC_PACK_TRUNC_EXPR:
2751 case VEC_PACK_SAT_EXPR:
2752 case VEC_PACK_FIX_TRUNC_EXPR:
2753 case VEC_EXTRACT_EVEN_EXPR:
2754 case VEC_EXTRACT_ODD_EXPR:
2755 case VEC_INTERLEAVE_HIGH_EXPR:
2756 case VEC_INTERLEAVE_LOW_EXPR:
2758 return 1;
2760 /* Few special cases of expensive operations. This is useful
2761 to avoid inlining on functions having too many of these. */
2762 case TRUNC_DIV_EXPR:
2763 case CEIL_DIV_EXPR:
2764 case FLOOR_DIV_EXPR:
2765 case ROUND_DIV_EXPR:
2766 case EXACT_DIV_EXPR:
2767 case TRUNC_MOD_EXPR:
2768 case CEIL_MOD_EXPR:
2769 case FLOOR_MOD_EXPR:
2770 case ROUND_MOD_EXPR:
2771 case RDIV_EXPR:
2772 return weights->div_mod_cost;
2774 default:
2775 /* We expect a copy assignment with no operator. */
2776 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
2777 return 0;
2782 /* Estimate number of instructions that will be created by expanding
2783 the statements in the statement sequence STMTS.
2784 WEIGHTS contains weights attributed to various constructs. */
2786 static
2787 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
2789 int cost;
2790 gimple_stmt_iterator gsi;
2792 cost = 0;
2793 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2794 cost += estimate_num_insns (gsi_stmt (gsi), weights);
2796 return cost;
2800 /* Estimate number of instructions that will be created by expanding STMT.
2801 WEIGHTS contains weights attributed to various constructs. */
2804 estimate_num_insns (gimple stmt, eni_weights *weights)
2806 unsigned cost, i;
2807 enum gimple_code code = gimple_code (stmt);
2808 tree lhs;
2810 switch (code)
2812 case GIMPLE_ASSIGN:
2813 /* Try to estimate the cost of assignments. We have three cases to
2814 deal with:
2815 1) Simple assignments to registers;
2816 2) Stores to things that must live in memory. This includes
2817 "normal" stores to scalars, but also assignments of large
2818 structures, or constructors of big arrays;
2820 Let us look at the first two cases, assuming we have "a = b + C":
2821 <GIMPLE_ASSIGN <var_decl "a">
2822 <plus_expr <var_decl "b"> <constant C>>
2823 If "a" is a GIMPLE register, the assignment to it is free on almost
2824 any target, because "a" usually ends up in a real register. Hence
2825 the only cost of this expression comes from the PLUS_EXPR, and we
2826 can ignore the GIMPLE_ASSIGN.
2827 If "a" is not a GIMPLE register, the assignment to "a" will most
2828 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
2829 of moving something into "a", which we compute using the function
2830 estimate_move_cost. */
2831 lhs = gimple_assign_lhs (stmt);
2832 if (is_gimple_reg (lhs))
2833 cost = 0;
2834 else
2835 cost = estimate_move_cost (TREE_TYPE (lhs));
2837 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights);
2838 break;
2840 case GIMPLE_COND:
2841 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights);
2842 break;
2844 case GIMPLE_SWITCH:
2845 /* Take into account cost of the switch + guess 2 conditional jumps for
2846 each case label.
2848 TODO: once the switch expansion logic is sufficiently separated, we can
2849 do better job on estimating cost of the switch. */
2850 cost = gimple_switch_num_labels (stmt) * 2;
2851 break;
2853 case GIMPLE_CALL:
2855 tree decl = gimple_call_fndecl (stmt);
2856 tree addr = gimple_call_fn (stmt);
2857 tree funtype = TREE_TYPE (addr);
2859 if (POINTER_TYPE_P (funtype))
2860 funtype = TREE_TYPE (funtype);
2862 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
2863 cost = weights->target_builtin_call_cost;
2864 else
2865 cost = weights->call_cost;
2867 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2868 switch (DECL_FUNCTION_CODE (decl))
2870 case BUILT_IN_CONSTANT_P:
2871 return 0;
2872 case BUILT_IN_EXPECT:
2873 cost = 0;
2874 break;
2876 /* Prefetch instruction is not expensive. */
2877 case BUILT_IN_PREFETCH:
2878 cost = weights->target_builtin_call_cost;
2879 break;
2881 default:
2882 break;
2885 if (decl)
2886 funtype = TREE_TYPE (decl);
2888 /* Our cost must be kept in sync with
2889 cgraph_estimate_size_after_inlining that does use function
2890 declaration to figure out the arguments. */
2891 if (decl && DECL_ARGUMENTS (decl))
2893 tree arg;
2894 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2895 cost += estimate_move_cost (TREE_TYPE (arg));
2897 else if (funtype && prototype_p (funtype))
2899 tree t;
2900 for (t = TYPE_ARG_TYPES (funtype); t; t = TREE_CHAIN (t))
2901 cost += estimate_move_cost (TREE_VALUE (t));
2903 else
2905 for (i = 0; i < gimple_call_num_args (stmt); i++)
2907 tree arg = gimple_call_arg (stmt, i);
2908 cost += estimate_move_cost (TREE_TYPE (arg));
2912 break;
2915 case GIMPLE_GOTO:
2916 case GIMPLE_LABEL:
2917 case GIMPLE_NOP:
2918 case GIMPLE_PHI:
2919 case GIMPLE_RETURN:
2920 case GIMPLE_CHANGE_DYNAMIC_TYPE:
2921 case GIMPLE_PREDICT:
2922 return 0;
2924 case GIMPLE_ASM:
2925 case GIMPLE_RESX:
2926 return 1;
2928 case GIMPLE_BIND:
2929 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
2931 case GIMPLE_EH_FILTER:
2932 return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
2934 case GIMPLE_CATCH:
2935 return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
2937 case GIMPLE_TRY:
2938 return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
2939 + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
2941 /* OpenMP directives are generally very expensive. */
2943 case GIMPLE_OMP_RETURN:
2944 case GIMPLE_OMP_SECTIONS_SWITCH:
2945 case GIMPLE_OMP_ATOMIC_STORE:
2946 case GIMPLE_OMP_CONTINUE:
2947 /* ...except these, which are cheap. */
2948 return 0;
2950 case GIMPLE_OMP_ATOMIC_LOAD:
2951 return weights->omp_cost;
2953 case GIMPLE_OMP_FOR:
2954 return (weights->omp_cost
2955 + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
2956 + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
2958 case GIMPLE_OMP_PARALLEL:
2959 case GIMPLE_OMP_TASK:
2960 case GIMPLE_OMP_CRITICAL:
2961 case GIMPLE_OMP_MASTER:
2962 case GIMPLE_OMP_ORDERED:
2963 case GIMPLE_OMP_SECTION:
2964 case GIMPLE_OMP_SECTIONS:
2965 case GIMPLE_OMP_SINGLE:
2966 return (weights->omp_cost
2967 + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
2969 default:
2970 gcc_unreachable ();
2973 return cost;
2976 /* Estimate number of instructions that will be created by expanding
2977 function FNDECL. WEIGHTS contains weights attributed to various
2978 constructs. */
2981 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
2983 struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
2984 gimple_stmt_iterator bsi;
2985 basic_block bb;
2986 int n = 0;
2988 gcc_assert (my_function && my_function->cfg);
2989 FOR_EACH_BB_FN (bb, my_function)
2991 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2992 n += estimate_num_insns (gsi_stmt (bsi), weights);
2995 return n;
2999 /* Initializes weights used by estimate_num_insns. */
3001 void
3002 init_inline_once (void)
3004 eni_inlining_weights.call_cost = PARAM_VALUE (PARAM_INLINE_CALL_COST);
3005 eni_inlining_weights.target_builtin_call_cost = 1;
3006 eni_inlining_weights.div_mod_cost = 10;
3007 eni_inlining_weights.omp_cost = 40;
3009 eni_size_weights.call_cost = 1;
3010 eni_size_weights.target_builtin_call_cost = 1;
3011 eni_size_weights.div_mod_cost = 1;
3012 eni_size_weights.omp_cost = 40;
3014 /* Estimating time for call is difficult, since we have no idea what the
3015 called function does. In the current uses of eni_time_weights,
3016 underestimating the cost does less harm than overestimating it, so
3017 we choose a rather small value here. */
3018 eni_time_weights.call_cost = 10;
3019 eni_time_weights.target_builtin_call_cost = 10;
3020 eni_time_weights.div_mod_cost = 10;
3021 eni_time_weights.omp_cost = 40;
3024 /* Estimate the number of instructions in a gimple_seq. */
3027 count_insns_seq (gimple_seq seq, eni_weights *weights)
3029 gimple_stmt_iterator gsi;
3030 int n = 0;
3031 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3032 n += estimate_num_insns (gsi_stmt (gsi), weights);
3034 return n;
3038 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
3040 static void
3041 prepend_lexical_block (tree current_block, tree new_block)
3043 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3044 BLOCK_SUBBLOCKS (current_block) = new_block;
3045 BLOCK_SUPERCONTEXT (new_block) = current_block;
3048 /* Fetch callee declaration from the call graph edge going from NODE and
3049 associated with STMR call statement. Return NULL_TREE if not found. */
3050 static tree
3051 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3053 struct cgraph_edge *cs;
3055 cs = cgraph_edge (node, stmt);
3056 if (cs)
3057 return cs->callee->decl;
3059 return NULL_TREE;
3062 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3064 static bool
3065 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3067 tree retvar, use_retvar;
3068 tree fn;
3069 struct pointer_map_t *st;
3070 tree return_slot;
3071 tree modify_dest;
3072 location_t saved_location;
3073 struct cgraph_edge *cg_edge;
3074 const char *reason;
3075 basic_block return_block;
3076 edge e;
3077 gimple_stmt_iterator gsi, stmt_gsi;
3078 bool successfully_inlined = FALSE;
3079 bool purge_dead_abnormal_edges;
3080 tree t_step;
3081 tree var;
3083 /* Set input_location here so we get the right instantiation context
3084 if we call instantiate_decl from inlinable_function_p. */
3085 saved_location = input_location;
3086 if (gimple_has_location (stmt))
3087 input_location = gimple_location (stmt);
3089 /* From here on, we're only interested in CALL_EXPRs. */
3090 if (gimple_code (stmt) != GIMPLE_CALL)
3091 goto egress;
3093 /* First, see if we can figure out what function is being called.
3094 If we cannot, then there is no hope of inlining the function. */
3095 fn = gimple_call_fndecl (stmt);
3096 if (!fn)
3098 fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3099 if (!fn)
3100 goto egress;
3103 /* Turn forward declarations into real ones. */
3104 fn = cgraph_node (fn)->decl;
3106 /* If FN is a declaration of a function in a nested scope that was
3107 globally declared inline, we don't set its DECL_INITIAL.
3108 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3109 C++ front-end uses it for cdtors to refer to their internal
3110 declarations, that are not real functions. Fortunately those
3111 don't have trees to be saved, so we can tell by checking their
3112 gimple_body. */
3113 if (!DECL_INITIAL (fn)
3114 && DECL_ABSTRACT_ORIGIN (fn)
3115 && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3116 fn = DECL_ABSTRACT_ORIGIN (fn);
3118 /* Objective C and fortran still calls tree_rest_of_compilation directly.
3119 Kill this check once this is fixed. */
3120 if (!id->dst_node->analyzed)
3121 goto egress;
3123 cg_edge = cgraph_edge (id->dst_node, stmt);
3125 /* Constant propagation on argument done during previous inlining
3126 may create new direct call. Produce an edge for it. */
3127 if (!cg_edge)
3129 struct cgraph_node *dest = cgraph_node (fn);
3131 /* We have missing edge in the callgraph. This can happen in one case
3132 where previous inlining turned indirect call into direct call by
3133 constant propagating arguments. In all other cases we hit a bug
3134 (incorrect node sharing is most common reason for missing edges. */
3135 gcc_assert (dest->needed);
3136 cgraph_create_edge (id->dst_node, dest, stmt,
3137 bb->count, CGRAPH_FREQ_BASE,
3138 bb->loop_depth)->inline_failed
3139 = N_("originally indirect function call not considered for inlining");
3140 if (dump_file)
3142 fprintf (dump_file, "Created new direct edge to %s",
3143 cgraph_node_name (dest));
3145 goto egress;
3148 /* Don't try to inline functions that are not well-suited to
3149 inlining. */
3150 if (!cgraph_inline_p (cg_edge, &reason))
3152 /* If this call was originally indirect, we do not want to emit any
3153 inlining related warnings or sorry messages because there are no
3154 guarantees regarding those. */
3155 if (cg_edge->indirect_call)
3156 goto egress;
3158 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3159 /* Avoid warnings during early inline pass. */
3160 && cgraph_global_info_ready)
3162 sorry ("inlining failed in call to %q+F: %s", fn, reason);
3163 sorry ("called from here");
3165 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3166 && !DECL_IN_SYSTEM_HEADER (fn)
3167 && strlen (reason)
3168 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3169 /* Avoid warnings during early inline pass. */
3170 && cgraph_global_info_ready)
3172 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3173 fn, reason);
3174 warning (OPT_Winline, "called from here");
3176 goto egress;
3178 fn = cg_edge->callee->decl;
3180 #ifdef ENABLE_CHECKING
3181 if (cg_edge->callee->decl != id->dst_node->decl)
3182 verify_cgraph_node (cg_edge->callee);
3183 #endif
3185 /* We will be inlining this callee. */
3186 id->eh_region = lookup_stmt_eh_region (stmt);
3188 /* Split the block holding the GIMPLE_CALL. */
3189 e = split_block (bb, stmt);
3190 bb = e->src;
3191 return_block = e->dest;
3192 remove_edge (e);
3194 /* split_block splits after the statement; work around this by
3195 moving the call into the second block manually. Not pretty,
3196 but seems easier than doing the CFG manipulation by hand
3197 when the GIMPLE_CALL is in the last statement of BB. */
3198 stmt_gsi = gsi_last_bb (bb);
3199 gsi_remove (&stmt_gsi, false);
3201 /* If the GIMPLE_CALL was in the last statement of BB, it may have
3202 been the source of abnormal edges. In this case, schedule
3203 the removal of dead abnormal edges. */
3204 gsi = gsi_start_bb (return_block);
3205 if (gsi_end_p (gsi))
3207 gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3208 purge_dead_abnormal_edges = true;
3210 else
3212 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3213 purge_dead_abnormal_edges = false;
3216 stmt_gsi = gsi_start_bb (return_block);
3218 /* Build a block containing code to initialize the arguments, the
3219 actual inline expansion of the body, and a label for the return
3220 statements within the function to jump to. The type of the
3221 statement expression is the return type of the function call. */
3222 id->block = make_node (BLOCK);
3223 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3224 BLOCK_SOURCE_LOCATION (id->block) = input_location;
3225 prepend_lexical_block (gimple_block (stmt), id->block);
3227 /* Local declarations will be replaced by their equivalents in this
3228 map. */
3229 st = id->decl_map;
3230 id->decl_map = pointer_map_create ();
3232 /* Record the function we are about to inline. */
3233 id->src_fn = fn;
3234 id->src_node = cg_edge->callee;
3235 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3236 id->gimple_call = stmt;
3238 gcc_assert (!id->src_cfun->after_inlining);
3240 id->entry_bb = bb;
3241 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3243 gimple_stmt_iterator si = gsi_last_bb (bb);
3244 gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3245 NOT_TAKEN),
3246 GSI_NEW_STMT);
3248 initialize_inlined_parameters (id, stmt, fn, bb);
3250 if (DECL_INITIAL (fn))
3251 prepend_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3253 /* Return statements in the function body will be replaced by jumps
3254 to the RET_LABEL. */
3255 gcc_assert (DECL_INITIAL (fn));
3256 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3258 /* Find the LHS to which the result of this call is assigned. */
3259 return_slot = NULL;
3260 if (gimple_call_lhs (stmt))
3262 modify_dest = gimple_call_lhs (stmt);
3264 /* The function which we are inlining might not return a value,
3265 in which case we should issue a warning that the function
3266 does not return a value. In that case the optimizers will
3267 see that the variable to which the value is assigned was not
3268 initialized. We do not want to issue a warning about that
3269 uninitialized variable. */
3270 if (DECL_P (modify_dest))
3271 TREE_NO_WARNING (modify_dest) = 1;
3273 if (gimple_call_return_slot_opt_p (stmt))
3275 return_slot = modify_dest;
3276 modify_dest = NULL;
3279 else
3280 modify_dest = NULL;
3282 /* If we are inlining a call to the C++ operator new, we don't want
3283 to use type based alias analysis on the return value. Otherwise
3284 we may get confused if the compiler sees that the inlined new
3285 function returns a pointer which was just deleted. See bug
3286 33407. */
3287 if (DECL_IS_OPERATOR_NEW (fn))
3289 return_slot = NULL;
3290 modify_dest = NULL;
3293 /* Declare the return variable for the function. */
3294 retvar = declare_return_variable (id, return_slot, modify_dest, &use_retvar);
3296 if (DECL_IS_OPERATOR_NEW (fn))
3298 gcc_assert (TREE_CODE (retvar) == VAR_DECL
3299 && POINTER_TYPE_P (TREE_TYPE (retvar)));
3300 DECL_NO_TBAA_P (retvar) = 1;
3303 /* This is it. Duplicate the callee body. Assume callee is
3304 pre-gimplified. Note that we must not alter the caller
3305 function in any way before this point, as this CALL_EXPR may be
3306 a self-referential call; if we're calling ourselves, we need to
3307 duplicate our body before altering anything. */
3308 copy_body (id, bb->count, bb->frequency, bb, return_block);
3310 /* Add local vars in this inlined callee to caller. */
3311 t_step = id->src_cfun->local_decls;
3312 for (; t_step; t_step = TREE_CHAIN (t_step))
3314 var = TREE_VALUE (t_step);
3315 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3316 cfun->local_decls = tree_cons (NULL_TREE, var,
3317 cfun->local_decls);
3318 else
3319 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3320 cfun->local_decls);
3323 /* Clean up. */
3324 pointer_map_destroy (id->decl_map);
3325 id->decl_map = st;
3327 /* If the inlined function returns a result that we care about,
3328 substitute the GIMPLE_CALL with an assignment of the return
3329 variable to the LHS of the call. That is, if STMT was
3330 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
3331 if (use_retvar && gimple_call_lhs (stmt))
3333 gimple old_stmt = stmt;
3334 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3335 gsi_replace (&stmt_gsi, stmt, false);
3336 if (gimple_in_ssa_p (cfun))
3338 update_stmt (stmt);
3339 mark_symbols_for_renaming (stmt);
3341 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3343 else
3345 /* Handle the case of inlining a function with no return
3346 statement, which causes the return value to become undefined. */
3347 if (gimple_call_lhs (stmt)
3348 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3350 tree name = gimple_call_lhs (stmt);
3351 tree var = SSA_NAME_VAR (name);
3352 tree def = gimple_default_def (cfun, var);
3354 if (def)
3356 /* If the variable is used undefined, make this name
3357 undefined via a move. */
3358 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3359 gsi_replace (&stmt_gsi, stmt, true);
3360 update_stmt (stmt);
3362 else
3364 /* Otherwise make this variable undefined. */
3365 gsi_remove (&stmt_gsi, true);
3366 set_default_def (var, name);
3367 SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
3370 else
3371 gsi_remove (&stmt_gsi, true);
3374 if (purge_dead_abnormal_edges)
3375 gimple_purge_dead_abnormal_call_edges (return_block);
3377 /* If the value of the new expression is ignored, that's OK. We
3378 don't warn about this for CALL_EXPRs, so we shouldn't warn about
3379 the equivalent inlined version either. */
3380 if (is_gimple_assign (stmt))
3382 gcc_assert (gimple_assign_single_p (stmt)
3383 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
3384 TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
3387 /* Output the inlining info for this abstract function, since it has been
3388 inlined. If we don't do this now, we can lose the information about the
3389 variables in the function when the blocks get blown away as soon as we
3390 remove the cgraph node. */
3391 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
3393 /* Update callgraph if needed. */
3394 cgraph_remove_node (cg_edge->callee);
3396 id->block = NULL_TREE;
3397 successfully_inlined = TRUE;
3399 egress:
3400 input_location = saved_location;
3401 return successfully_inlined;
3404 /* Expand call statements reachable from STMT_P.
3405 We can only have CALL_EXPRs as the "toplevel" tree code or nested
3406 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
3407 unfortunately not use that function here because we need a pointer
3408 to the CALL_EXPR, not the tree itself. */
3410 static bool
3411 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
3413 gimple_stmt_iterator gsi;
3415 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3417 gimple stmt = gsi_stmt (gsi);
3419 if (is_gimple_call (stmt)
3420 && expand_call_inline (bb, stmt, id))
3421 return true;
3424 return false;
3428 /* Walk all basic blocks created after FIRST and try to fold every statement
3429 in the STATEMENTS pointer set. */
3431 static void
3432 fold_marked_statements (int first, struct pointer_set_t *statements)
3434 for (; first < n_basic_blocks; first++)
3435 if (BASIC_BLOCK (first))
3437 gimple_stmt_iterator gsi;
3439 for (gsi = gsi_start_bb (BASIC_BLOCK (first));
3440 !gsi_end_p (gsi);
3441 gsi_next (&gsi))
3442 if (pointer_set_contains (statements, gsi_stmt (gsi)))
3444 gimple old_stmt = gsi_stmt (gsi);
3446 if (fold_stmt (&gsi))
3448 /* Re-read the statement from GSI as fold_stmt() may
3449 have changed it. */
3450 gimple new_stmt = gsi_stmt (gsi);
3451 update_stmt (new_stmt);
3453 if (is_gimple_call (old_stmt))
3454 cgraph_update_edges_for_call_stmt (old_stmt, new_stmt);
3456 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3457 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3463 /* Return true if BB has at least one abnormal outgoing edge. */
3465 static inline bool
3466 has_abnormal_outgoing_edge_p (basic_block bb)
3468 edge e;
3469 edge_iterator ei;
3471 FOR_EACH_EDGE (e, ei, bb->succs)
3472 if (e->flags & EDGE_ABNORMAL)
3473 return true;
3475 return false;
3478 /* Expand calls to inline functions in the body of FN. */
3480 unsigned int
3481 optimize_inline_calls (tree fn)
3483 copy_body_data id;
3484 tree prev_fn;
3485 basic_block bb;
3486 int last = n_basic_blocks;
3487 struct gimplify_ctx gctx;
3489 /* There is no point in performing inlining if errors have already
3490 occurred -- and we might crash if we try to inline invalid
3491 code. */
3492 if (errorcount || sorrycount)
3493 return 0;
3495 /* Clear out ID. */
3496 memset (&id, 0, sizeof (id));
3498 id.src_node = id.dst_node = cgraph_node (fn);
3499 id.dst_fn = fn;
3500 /* Or any functions that aren't finished yet. */
3501 prev_fn = NULL_TREE;
3502 if (current_function_decl)
3504 id.dst_fn = current_function_decl;
3505 prev_fn = current_function_decl;
3508 id.copy_decl = copy_decl_maybe_to_var;
3509 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3510 id.transform_new_cfg = false;
3511 id.transform_return_to_modify = true;
3512 id.transform_lang_insert_block = NULL;
3513 id.statements_to_fold = pointer_set_create ();
3515 push_gimplify_context (&gctx);
3517 /* We make no attempts to keep dominance info up-to-date. */
3518 free_dominance_info (CDI_DOMINATORS);
3519 free_dominance_info (CDI_POST_DOMINATORS);
3521 /* Register specific gimple functions. */
3522 gimple_register_cfg_hooks ();
3524 /* Reach the trees by walking over the CFG, and note the
3525 enclosing basic-blocks in the call edges. */
3526 /* We walk the blocks going forward, because inlined function bodies
3527 will split id->current_basic_block, and the new blocks will
3528 follow it; we'll trudge through them, processing their CALL_EXPRs
3529 along the way. */
3530 FOR_EACH_BB (bb)
3531 gimple_expand_calls_inline (bb, &id);
3533 pop_gimplify_context (NULL);
3535 #ifdef ENABLE_CHECKING
3537 struct cgraph_edge *e;
3539 verify_cgraph_node (id.dst_node);
3541 /* Double check that we inlined everything we are supposed to inline. */
3542 for (e = id.dst_node->callees; e; e = e->next_callee)
3543 gcc_assert (e->inline_failed);
3545 #endif
3547 /* Fold the statements before compacting/renumbering the basic blocks. */
3548 fold_marked_statements (last, id.statements_to_fold);
3549 pointer_set_destroy (id.statements_to_fold);
3551 /* Renumber the (code) basic_blocks consecutively. */
3552 compact_blocks ();
3553 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3554 number_blocks (fn);
3556 /* We are not going to maintain the cgraph edges up to date.
3557 Kill it so it won't confuse us. */
3558 cgraph_node_remove_callees (id.dst_node);
3560 fold_cond_expr_cond ();
3562 /* It would be nice to check SSA/CFG/statement consistency here, but it is
3563 not possible yet - the IPA passes might make various functions to not
3564 throw and they don't care to proactively update local EH info. This is
3565 done later in fixup_cfg pass that also execute the verification. */
3566 return (TODO_update_ssa
3567 | TODO_cleanup_cfg
3568 | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
3569 | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
3572 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
3574 tree
3575 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3577 enum tree_code code = TREE_CODE (*tp);
3578 enum tree_code_class cl = TREE_CODE_CLASS (code);
3580 /* We make copies of most nodes. */
3581 if (IS_EXPR_CODE_CLASS (cl)
3582 || code == TREE_LIST
3583 || code == TREE_VEC
3584 || code == TYPE_DECL
3585 || code == OMP_CLAUSE)
3587 /* Because the chain gets clobbered when we make a copy, we save it
3588 here. */
3589 tree chain = NULL_TREE, new_tree;
3591 chain = TREE_CHAIN (*tp);
3593 /* Copy the node. */
3594 new_tree = copy_node (*tp);
3596 /* Propagate mudflap marked-ness. */
3597 if (flag_mudflap && mf_marked_p (*tp))
3598 mf_mark (new_tree);
3600 *tp = new_tree;
3602 /* Now, restore the chain, if appropriate. That will cause
3603 walk_tree to walk into the chain as well. */
3604 if (code == PARM_DECL
3605 || code == TREE_LIST
3606 || code == OMP_CLAUSE)
3607 TREE_CHAIN (*tp) = chain;
3609 /* For now, we don't update BLOCKs when we make copies. So, we
3610 have to nullify all BIND_EXPRs. */
3611 if (TREE_CODE (*tp) == BIND_EXPR)
3612 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
3614 else if (code == CONSTRUCTOR)
3616 /* CONSTRUCTOR nodes need special handling because
3617 we need to duplicate the vector of elements. */
3618 tree new_tree;
3620 new_tree = copy_node (*tp);
3622 /* Propagate mudflap marked-ness. */
3623 if (flag_mudflap && mf_marked_p (*tp))
3624 mf_mark (new_tree);
3626 CONSTRUCTOR_ELTS (new_tree) = VEC_copy (constructor_elt, gc,
3627 CONSTRUCTOR_ELTS (*tp));
3628 *tp = new_tree;
3630 else if (TREE_CODE_CLASS (code) == tcc_type)
3631 *walk_subtrees = 0;
3632 else if (TREE_CODE_CLASS (code) == tcc_declaration)
3633 *walk_subtrees = 0;
3634 else if (TREE_CODE_CLASS (code) == tcc_constant)
3635 *walk_subtrees = 0;
3636 else
3637 gcc_assert (code != STATEMENT_LIST);
3638 return NULL_TREE;
3641 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
3642 information indicating to what new SAVE_EXPR this one should be mapped,
3643 use that one. Otherwise, create a new node and enter it in ST. FN is
3644 the function into which the copy will be placed. */
3646 static void
3647 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
3649 struct pointer_map_t *st = (struct pointer_map_t *) st_;
3650 tree *n;
3651 tree t;
3653 /* See if we already encountered this SAVE_EXPR. */
3654 n = (tree *) pointer_map_contains (st, *tp);
3656 /* If we didn't already remap this SAVE_EXPR, do so now. */
3657 if (!n)
3659 t = copy_node (*tp);
3661 /* Remember this SAVE_EXPR. */
3662 *pointer_map_insert (st, *tp) = t;
3663 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3664 *pointer_map_insert (st, t) = t;
3666 else
3668 /* We've already walked into this SAVE_EXPR; don't do it again. */
3669 *walk_subtrees = 0;
3670 t = *n;
3673 /* Replace this SAVE_EXPR with the copy. */
3674 *tp = t;
3677 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
3678 copies the declaration and enters it in the splay_tree in DATA (which is
3679 really an `copy_body_data *'). */
3681 static tree
3682 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3683 void *data)
3685 copy_body_data *id = (copy_body_data *) data;
3687 /* Don't walk into types. */
3688 if (TYPE_P (*tp))
3689 *walk_subtrees = 0;
3691 else if (TREE_CODE (*tp) == LABEL_EXPR)
3693 tree decl = TREE_OPERAND (*tp, 0);
3695 /* Copy the decl and remember the copy. */
3696 insert_decl_map (id, decl, id->copy_decl (decl, id));
3699 return NULL_TREE;
3702 /* Perform any modifications to EXPR required when it is unsaved. Does
3703 not recurse into EXPR's subtrees. */
3705 static void
3706 unsave_expr_1 (tree expr)
3708 switch (TREE_CODE (expr))
3710 case TARGET_EXPR:
3711 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3712 It's OK for this to happen if it was part of a subtree that
3713 isn't immediately expanded, such as operand 2 of another
3714 TARGET_EXPR. */
3715 if (TREE_OPERAND (expr, 1))
3716 break;
3718 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3719 TREE_OPERAND (expr, 3) = NULL_TREE;
3720 break;
3722 default:
3723 break;
3727 /* Called via walk_tree when an expression is unsaved. Using the
3728 splay_tree pointed to by ST (which is really a `splay_tree'),
3729 remaps all local declarations to appropriate replacements. */
3731 static tree
3732 unsave_r (tree *tp, int *walk_subtrees, void *data)
3734 copy_body_data *id = (copy_body_data *) data;
3735 struct pointer_map_t *st = id->decl_map;
3736 tree *n;
3738 /* Only a local declaration (variable or label). */
3739 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
3740 || TREE_CODE (*tp) == LABEL_DECL)
3742 /* Lookup the declaration. */
3743 n = (tree *) pointer_map_contains (st, *tp);
3745 /* If it's there, remap it. */
3746 if (n)
3747 *tp = *n;
3750 else if (TREE_CODE (*tp) == STATEMENT_LIST)
3751 gcc_unreachable ();
3752 else if (TREE_CODE (*tp) == BIND_EXPR)
3753 copy_bind_expr (tp, walk_subtrees, id);
3754 else if (TREE_CODE (*tp) == SAVE_EXPR)
3755 remap_save_expr (tp, st, walk_subtrees);
3756 else
3758 copy_tree_r (tp, walk_subtrees, NULL);
3760 /* Do whatever unsaving is required. */
3761 unsave_expr_1 (*tp);
3764 /* Keep iterating. */
3765 return NULL_TREE;
3768 /* Copies everything in EXPR and replaces variables, labels
3769 and SAVE_EXPRs local to EXPR. */
3771 tree
3772 unsave_expr_now (tree expr)
3774 copy_body_data id;
3776 /* There's nothing to do for NULL_TREE. */
3777 if (expr == 0)
3778 return expr;
3780 /* Set up ID. */
3781 memset (&id, 0, sizeof (id));
3782 id.src_fn = current_function_decl;
3783 id.dst_fn = current_function_decl;
3784 id.decl_map = pointer_map_create ();
3786 id.copy_decl = copy_decl_no_change;
3787 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3788 id.transform_new_cfg = false;
3789 id.transform_return_to_modify = false;
3790 id.transform_lang_insert_block = NULL;
3792 /* Walk the tree once to find local labels. */
3793 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
3795 /* Walk the tree again, copying, remapping, and unsaving. */
3796 walk_tree (&expr, unsave_r, &id, NULL);
3798 /* Clean up. */
3799 pointer_map_destroy (id.decl_map);
3801 return expr;
3804 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
3805 label, copies the declaration and enters it in the splay_tree in DATA (which
3806 is really a 'copy_body_data *'. */
3808 static tree
3809 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
3810 bool *handled_ops_p ATTRIBUTE_UNUSED,
3811 struct walk_stmt_info *wi)
3813 copy_body_data *id = (copy_body_data *) wi->info;
3814 gimple stmt = gsi_stmt (*gsip);
3816 if (gimple_code (stmt) == GIMPLE_LABEL)
3818 tree decl = gimple_label_label (stmt);
3820 /* Copy the decl and remember the copy. */
3821 insert_decl_map (id, decl, id->copy_decl (decl, id));
3824 return NULL_TREE;
3828 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3829 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3830 remaps all local declarations to appropriate replacements in gimple
3831 operands. */
3833 static tree
3834 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
3836 struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
3837 copy_body_data *id = (copy_body_data *) wi->info;
3838 struct pointer_map_t *st = id->decl_map;
3839 tree *n;
3840 tree expr = *tp;
3842 /* Only a local declaration (variable or label). */
3843 if ((TREE_CODE (expr) == VAR_DECL
3844 && !TREE_STATIC (expr))
3845 || TREE_CODE (expr) == LABEL_DECL)
3847 /* Lookup the declaration. */
3848 n = (tree *) pointer_map_contains (st, expr);
3850 /* If it's there, remap it. */
3851 if (n)
3852 *tp = *n;
3853 *walk_subtrees = 0;
3855 else if (TREE_CODE (expr) == STATEMENT_LIST
3856 || TREE_CODE (expr) == BIND_EXPR
3857 || TREE_CODE (expr) == SAVE_EXPR)
3858 gcc_unreachable ();
3859 else if (TREE_CODE (expr) == TARGET_EXPR)
3861 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3862 It's OK for this to happen if it was part of a subtree that
3863 isn't immediately expanded, such as operand 2 of another
3864 TARGET_EXPR. */
3865 if (!TREE_OPERAND (expr, 1))
3867 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3868 TREE_OPERAND (expr, 3) = NULL_TREE;
3872 /* Keep iterating. */
3873 return NULL_TREE;
3877 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3878 Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3879 remaps all local declarations to appropriate replacements in gimple
3880 statements. */
3882 static tree
3883 replace_locals_stmt (gimple_stmt_iterator *gsip,
3884 bool *handled_ops_p ATTRIBUTE_UNUSED,
3885 struct walk_stmt_info *wi)
3887 copy_body_data *id = (copy_body_data *) wi->info;
3888 gimple stmt = gsi_stmt (*gsip);
3890 if (gimple_code (stmt) == GIMPLE_BIND)
3892 tree block = gimple_bind_block (stmt);
3894 if (block)
3896 remap_block (&block, id);
3897 gimple_bind_set_block (stmt, block);
3900 /* This will remap a lot of the same decls again, but this should be
3901 harmless. */
3902 if (gimple_bind_vars (stmt))
3903 gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), id));
3906 /* Keep iterating. */
3907 return NULL_TREE;
3911 /* Copies everything in SEQ and replaces variables and labels local to
3912 current_function_decl. */
3914 gimple_seq
3915 copy_gimple_seq_and_replace_locals (gimple_seq seq)
3917 copy_body_data id;
3918 struct walk_stmt_info wi;
3919 struct pointer_set_t *visited;
3920 gimple_seq copy;
3922 /* There's nothing to do for NULL_TREE. */
3923 if (seq == NULL)
3924 return seq;
3926 /* Set up ID. */
3927 memset (&id, 0, sizeof (id));
3928 id.src_fn = current_function_decl;
3929 id.dst_fn = current_function_decl;
3930 id.decl_map = pointer_map_create ();
3932 id.copy_decl = copy_decl_no_change;
3933 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3934 id.transform_new_cfg = false;
3935 id.transform_return_to_modify = false;
3936 id.transform_lang_insert_block = NULL;
3938 /* Walk the tree once to find local labels. */
3939 memset (&wi, 0, sizeof (wi));
3940 visited = pointer_set_create ();
3941 wi.info = &id;
3942 wi.pset = visited;
3943 walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
3944 pointer_set_destroy (visited);
3946 copy = gimple_seq_copy (seq);
3948 /* Walk the copy, remapping decls. */
3949 memset (&wi, 0, sizeof (wi));
3950 wi.info = &id;
3951 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
3953 /* Clean up. */
3954 pointer_map_destroy (id.decl_map);
3956 return copy;
3960 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
3962 static tree
3963 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
3965 if (*tp == data)
3966 return (tree) data;
3967 else
3968 return NULL;
3971 bool
3972 debug_find_tree (tree top, tree search)
3974 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
3978 /* Declare the variables created by the inliner. Add all the variables in
3979 VARS to BIND_EXPR. */
3981 static void
3982 declare_inline_vars (tree block, tree vars)
3984 tree t;
3985 for (t = vars; t; t = TREE_CHAIN (t))
3987 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
3988 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
3989 cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
3992 if (block)
3993 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
3996 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
3997 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
3998 VAR_DECL translation. */
4000 static tree
4001 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
4003 /* Don't generate debug information for the copy if we wouldn't have
4004 generated it for the copy either. */
4005 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4006 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4008 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4009 declaration inspired this copy. */
4010 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4012 /* The new variable/label has no RTL, yet. */
4013 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4014 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4015 SET_DECL_RTL (copy, NULL_RTX);
4017 /* These args would always appear unused, if not for this. */
4018 TREE_USED (copy) = 1;
4020 /* Set the context for the new declaration. */
4021 if (!DECL_CONTEXT (decl))
4022 /* Globals stay global. */
4024 else if (DECL_CONTEXT (decl) != id->src_fn)
4025 /* Things that weren't in the scope of the function we're inlining
4026 from aren't in the scope we're inlining to, either. */
4028 else if (TREE_STATIC (decl))
4029 /* Function-scoped static variables should stay in the original
4030 function. */
4032 else
4033 /* Ordinary automatic local variables are now in the scope of the
4034 new function. */
4035 DECL_CONTEXT (copy) = id->dst_fn;
4037 return copy;
4040 static tree
4041 copy_decl_to_var (tree decl, copy_body_data *id)
4043 tree copy, type;
4045 gcc_assert (TREE_CODE (decl) == PARM_DECL
4046 || TREE_CODE (decl) == RESULT_DECL);
4048 type = TREE_TYPE (decl);
4050 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4051 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4052 TREE_READONLY (copy) = TREE_READONLY (decl);
4053 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4054 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4055 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4057 return copy_decl_for_dup_finish (id, decl, copy);
4060 /* Like copy_decl_to_var, but create a return slot object instead of a
4061 pointer variable for return by invisible reference. */
4063 static tree
4064 copy_result_decl_to_var (tree decl, copy_body_data *id)
4066 tree copy, type;
4068 gcc_assert (TREE_CODE (decl) == PARM_DECL
4069 || TREE_CODE (decl) == RESULT_DECL);
4071 type = TREE_TYPE (decl);
4072 if (DECL_BY_REFERENCE (decl))
4073 type = TREE_TYPE (type);
4075 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4076 TREE_READONLY (copy) = TREE_READONLY (decl);
4077 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4078 if (!DECL_BY_REFERENCE (decl))
4080 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4081 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4082 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4085 return copy_decl_for_dup_finish (id, decl, copy);
4088 tree
4089 copy_decl_no_change (tree decl, copy_body_data *id)
4091 tree copy;
4093 copy = copy_node (decl);
4095 /* The COPY is not abstract; it will be generated in DST_FN. */
4096 DECL_ABSTRACT (copy) = 0;
4097 lang_hooks.dup_lang_specific_decl (copy);
4099 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4100 been taken; it's for internal bookkeeping in expand_goto_internal. */
4101 if (TREE_CODE (copy) == LABEL_DECL)
4103 TREE_ADDRESSABLE (copy) = 0;
4104 LABEL_DECL_UID (copy) = -1;
4107 return copy_decl_for_dup_finish (id, decl, copy);
4110 static tree
4111 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4113 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4114 return copy_decl_to_var (decl, id);
4115 else
4116 return copy_decl_no_change (decl, id);
4119 /* Return a copy of the function's argument tree. */
4120 static tree
4121 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4122 bitmap args_to_skip, tree *vars)
4124 tree arg, *parg;
4125 tree new_parm = NULL;
4126 int i = 0;
4128 parg = &new_parm;
4130 for (arg = orig_parm; arg; arg = TREE_CHAIN (arg), i++)
4131 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4133 tree new_tree = remap_decl (arg, id);
4134 lang_hooks.dup_lang_specific_decl (new_tree);
4135 *parg = new_tree;
4136 parg = &TREE_CHAIN (new_tree);
4138 else
4140 /* Make an equivalent VAR_DECL. If the argument was used
4141 as temporary variable later in function, the uses will be
4142 replaced by local variable. */
4143 tree var = copy_decl_to_var (arg, id);
4144 get_var_ann (var);
4145 add_referenced_var (var);
4146 insert_decl_map (id, arg, var);
4147 /* Declare this new variable. */
4148 TREE_CHAIN (var) = *vars;
4149 *vars = var;
4151 return new_parm;
4154 /* Return a copy of the function's static chain. */
4155 static tree
4156 copy_static_chain (tree static_chain, copy_body_data * id)
4158 tree *chain_copy, *pvar;
4160 chain_copy = &static_chain;
4161 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4163 tree new_tree = remap_decl (*pvar, id);
4164 lang_hooks.dup_lang_specific_decl (new_tree);
4165 TREE_CHAIN (new_tree) = TREE_CHAIN (*pvar);
4166 *pvar = new_tree;
4168 return static_chain;
4171 /* Return true if the function is allowed to be versioned.
4172 This is a guard for the versioning functionality. */
4173 bool
4174 tree_versionable_function_p (tree fndecl)
4176 if (fndecl == NULL_TREE)
4177 return false;
4178 /* ??? There are cases where a function is
4179 uninlinable but can be versioned. */
4180 if (!tree_inlinable_function_p (fndecl))
4181 return false;
4183 return true;
4186 /* Create a copy of a function's tree.
4187 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4188 of the original function and the new copied function
4189 respectively. In case we want to replace a DECL
4190 tree with another tree while duplicating the function's
4191 body, TREE_MAP represents the mapping between these
4192 trees. If UPDATE_CLONES is set, the call_stmt fields
4193 of edges of clones of the function will be updated. */
4194 void
4195 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
4196 bool update_clones, bitmap args_to_skip)
4198 struct cgraph_node *old_version_node;
4199 struct cgraph_node *new_version_node;
4200 copy_body_data id;
4201 tree p;
4202 unsigned i;
4203 struct ipa_replace_map *replace_info;
4204 basic_block old_entry_block;
4205 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
4207 tree t_step;
4208 tree old_current_function_decl = current_function_decl;
4209 tree vars = NULL_TREE;
4211 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
4212 && TREE_CODE (new_decl) == FUNCTION_DECL);
4213 DECL_POSSIBLY_INLINED (old_decl) = 1;
4215 old_version_node = cgraph_node (old_decl);
4216 new_version_node = cgraph_node (new_decl);
4218 DECL_ARTIFICIAL (new_decl) = 1;
4219 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4221 /* Prepare the data structures for the tree copy. */
4222 memset (&id, 0, sizeof (id));
4224 /* Generate a new name for the new version. */
4225 if (!update_clones)
4227 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
4228 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
4229 SET_DECL_RTL (new_decl, NULL_RTX);
4230 id.statements_to_fold = pointer_set_create ();
4233 id.decl_map = pointer_map_create ();
4234 id.src_fn = old_decl;
4235 id.dst_fn = new_decl;
4236 id.src_node = old_version_node;
4237 id.dst_node = new_version_node;
4238 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4240 id.copy_decl = copy_decl_no_change;
4241 id.transform_call_graph_edges
4242 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4243 id.transform_new_cfg = true;
4244 id.transform_return_to_modify = false;
4245 id.transform_lang_insert_block = NULL;
4247 current_function_decl = new_decl;
4248 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4249 (DECL_STRUCT_FUNCTION (old_decl));
4250 initialize_cfun (new_decl, old_decl,
4251 old_entry_block->count,
4252 old_entry_block->frequency);
4253 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4255 /* Copy the function's static chain. */
4256 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4257 if (p)
4258 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4259 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4260 &id);
4261 /* Copy the function's arguments. */
4262 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4263 DECL_ARGUMENTS (new_decl) =
4264 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
4265 args_to_skip, &vars);
4267 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4269 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4270 number_blocks (id.dst_fn);
4272 /* If there's a tree_map, prepare for substitution. */
4273 if (tree_map)
4274 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
4276 gimple init;
4277 replace_info
4278 = (struct ipa_replace_map *) VARRAY_GENERIC_PTR (tree_map, i);
4279 if (replace_info->replace_p)
4281 tree op = replace_info->new_tree;
4283 STRIP_NOPS (op);
4285 if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
4286 op = TREE_OPERAND (op, 0);
4288 if (TREE_CODE (op) == ADDR_EXPR)
4290 op = TREE_OPERAND (op, 0);
4291 while (handled_component_p (op))
4292 op = TREE_OPERAND (op, 0);
4293 if (TREE_CODE (op) == VAR_DECL)
4294 add_referenced_var (op);
4296 gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
4297 init = setup_one_parameter (&id, replace_info->old_tree,
4298 replace_info->new_tree, id.src_fn,
4299 NULL,
4300 &vars);
4301 if (init)
4302 VEC_safe_push (gimple, heap, init_stmts, init);
4306 declare_inline_vars (DECL_INITIAL (new_decl), vars);
4307 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4308 /* Add local vars. */
4309 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4310 t_step; t_step = TREE_CHAIN (t_step))
4312 tree var = TREE_VALUE (t_step);
4313 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
4314 cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
4315 else
4316 cfun->local_decls =
4317 tree_cons (NULL_TREE, remap_decl (var, &id),
4318 cfun->local_decls);
4321 /* Copy the Function's body. */
4322 copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4324 if (DECL_RESULT (old_decl) != NULL_TREE)
4326 tree *res_decl = &DECL_RESULT (old_decl);
4327 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4328 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4331 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4332 number_blocks (new_decl);
4334 if (VEC_length (gimple, init_stmts))
4336 basic_block bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
4337 while (VEC_length (gimple, init_stmts))
4338 insert_init_stmt (bb, VEC_pop (gimple, init_stmts));
4341 /* Clean up. */
4342 pointer_map_destroy (id.decl_map);
4343 if (!update_clones)
4345 fold_marked_statements (0, id.statements_to_fold);
4346 pointer_set_destroy (id.statements_to_fold);
4347 fold_cond_expr_cond ();
4349 if (gimple_in_ssa_p (cfun))
4351 free_dominance_info (CDI_DOMINATORS);
4352 free_dominance_info (CDI_POST_DOMINATORS);
4353 if (!update_clones)
4354 delete_unreachable_blocks ();
4355 update_ssa (TODO_update_ssa);
4356 if (!update_clones)
4358 fold_cond_expr_cond ();
4359 if (need_ssa_update_p ())
4360 update_ssa (TODO_update_ssa);
4363 free_dominance_info (CDI_DOMINATORS);
4364 free_dominance_info (CDI_POST_DOMINATORS);
4365 VEC_free (gimple, heap, init_stmts);
4366 pop_cfun ();
4367 current_function_decl = old_current_function_decl;
4368 gcc_assert (!current_function_decl
4369 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4370 return;
4373 /* Duplicate a type, fields and all. */
4375 tree
4376 build_duplicate_type (tree type)
4378 struct copy_body_data id;
4380 memset (&id, 0, sizeof (id));
4381 id.src_fn = current_function_decl;
4382 id.dst_fn = current_function_decl;
4383 id.src_cfun = cfun;
4384 id.decl_map = pointer_map_create ();
4385 id.copy_decl = copy_decl_no_change;
4387 type = remap_type_1 (type, &id);
4389 pointer_map_destroy (id.decl_map);
4391 TYPE_CANONICAL (type) = type;
4393 return type;
4396 /* Return whether it is safe to inline a function because it used different
4397 target specific options or different optimization options. */
4398 bool
4399 tree_can_inline_p (tree caller, tree callee)
4401 #if 0
4402 /* This causes a regression in SPEC in that it prevents a cold function from
4403 inlining a hot function. Perhaps this should only apply to functions
4404 that the user declares hot/cold/optimize explicitly. */
4406 /* Don't inline a function with a higher optimization level than the
4407 caller, or with different space constraints (hot/cold functions). */
4408 tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
4409 tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
4411 if (caller_tree != callee_tree)
4413 struct cl_optimization *caller_opt
4414 = TREE_OPTIMIZATION ((caller_tree)
4415 ? caller_tree
4416 : optimization_default_node);
4418 struct cl_optimization *callee_opt
4419 = TREE_OPTIMIZATION ((callee_tree)
4420 ? callee_tree
4421 : optimization_default_node);
4423 if ((caller_opt->optimize > callee_opt->optimize)
4424 || (caller_opt->optimize_size != callee_opt->optimize_size))
4425 return false;
4427 #endif
4429 /* Allow the backend to decide if inlining is ok. */
4430 return targetm.target_option.can_inline_p (caller, callee);