* config/alpha/alpha.md, arm/arm.c, darwin.c, frv/frv.md,
[official-gcc.git] / gcc / tree-inline.c
blob20bfc735b2a7bf36f4d00f69dd9da27f67694b14
1 /* Tree inlining.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
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"
55 /* I'm not real happy about this, but we need to handle gimple and
56 non-gimple trees. */
57 #include "tree-gimple.h"
59 /* Inlining, Cloning, Versioning, Parallelization
61 Inlining: a function body is duplicated, but the PARM_DECLs are
62 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
63 GIMPLE_MODIFY_STMTs that store to a dedicated returned-value variable.
64 The duplicated eh_region info of the copy will later be appended
65 to the info for the caller; the eh_region info in copied throwing
66 statements and RESX_EXPRs is adjusted accordingly.
68 Cloning: (only in C++) We have one body for a con/de/structor, and
69 multiple function decls, each with a unique parameter list.
70 Duplicate the body, using the given splay tree; some parameters
71 will become constants (like 0 or 1).
73 Versioning: a function body is duplicated and the result is a new
74 function rather than into blocks of an existing function as with
75 inlining. Some parameters will become constants.
77 Parallelization: a region of a function is duplicated resulting in
78 a new function. Variables may be replaced with complex expressions
79 to enable shared variable semantics.
81 All of these will simultaneously lookup any callgraph edges. If
82 we're going to inline the duplicated function body, and the given
83 function has some cloned callgraph nodes (one for each place this
84 function will be inlined) those callgraph edges will be duplicated.
85 If we're cloning the body, those callgraph edges will be
86 updated to point into the new body. (Note that the original
87 callgraph node and edge list will not be altered.)
89 See the CALL_EXPR handling case in copy_body_r (). */
91 /* 0 if we should not perform inlining.
92 1 if we should expand functions calls inline at the tree level.
93 2 if we should consider *all* functions to be inline
94 candidates. */
96 int flag_inline_trees = 0;
98 /* To Do:
100 o In order to make inlining-on-trees work, we pessimized
101 function-local static constants. In particular, they are now
102 always output, even when not addressed. Fix this by treating
103 function-local static constants just like global static
104 constants; the back-end already knows not to output them if they
105 are not needed.
107 o Provide heuristics to clamp inlining of recursive template
108 calls? */
110 /* Prototypes. */
112 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
113 static tree copy_generic_body (copy_body_data *);
114 static bool inlinable_function_p (tree);
115 static void remap_block (tree *, copy_body_data *);
116 static tree remap_decls (tree, copy_body_data *);
117 static void copy_bind_expr (tree *, int *, copy_body_data *);
118 static tree mark_local_for_remap_r (tree *, int *, void *);
119 static void unsave_expr_1 (tree);
120 static tree unsave_r (tree *, int *, void *);
121 static void declare_inline_vars (tree, tree);
122 static void remap_save_expr (tree *, void *, int *);
123 static void add_lexical_block (tree current_block, tree new_block);
124 static tree copy_decl_to_var (tree, copy_body_data *);
125 static tree copy_result_decl_to_var (tree, copy_body_data *);
126 static tree copy_decl_no_change (tree, copy_body_data *);
127 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
129 /* Insert a tree->tree mapping for ID. Despite the name suggests
130 that the trees should be variables, it is used for more than that. */
132 void
133 insert_decl_map (copy_body_data *id, tree key, tree value)
135 splay_tree_insert (id->decl_map, (splay_tree_key) key,
136 (splay_tree_value) value);
138 /* Always insert an identity map as well. If we see this same new
139 node again, we won't want to duplicate it a second time. */
140 if (key != value)
141 splay_tree_insert (id->decl_map, (splay_tree_key) value,
142 (splay_tree_value) value);
145 /* Construct new SSA name for old NAME. ID is the inline context. */
147 static tree
148 remap_ssa_name (tree name, copy_body_data *id)
150 tree new;
151 splay_tree_node n;
153 gcc_assert (TREE_CODE (name) == SSA_NAME);
155 n = splay_tree_lookup (id->decl_map, (splay_tree_key) name);
156 if (n)
157 return (tree) n->value;
159 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
160 in copy_bb. */
161 new = remap_decl (SSA_NAME_VAR (name), id);
162 /* We might've substituted constant or another SSA_NAME for
163 the variable.
165 Replace the SSA name representing RESULT_DECL by variable during
166 inlining: this saves us from need to introduce PHI node in a case
167 return value is just partly initialized. */
168 if ((TREE_CODE (new) == VAR_DECL || TREE_CODE (new) == PARM_DECL)
169 && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
170 || !id->transform_return_to_modify))
172 new = make_ssa_name (new, NULL);
173 insert_decl_map (id, name, new);
174 if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (name)))
176 SSA_NAME_DEF_STMT (new) = build_empty_stmt ();
177 if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name)) == name)
178 set_default_def (SSA_NAME_VAR (new), new);
180 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new)
181 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
182 TREE_TYPE (new) = TREE_TYPE (SSA_NAME_VAR (new));
184 else
185 insert_decl_map (id, name, new);
186 return new;
189 /* Remap DECL during the copying of the BLOCK tree for the function. */
191 tree
192 remap_decl (tree decl, copy_body_data *id)
194 splay_tree_node n;
195 tree fn;
197 /* We only remap local variables in the current function. */
198 fn = id->src_fn;
200 /* See if we have remapped this declaration. */
202 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
204 /* If we didn't already have an equivalent for this declaration,
205 create one now. */
206 if (!n)
208 /* Make a copy of the variable or label. */
209 tree t = id->copy_decl (decl, id);
211 /* Remember it, so that if we encounter this local entity again
212 we can reuse this copy. Do this early because remap_type may
213 need this decl for TYPE_STUB_DECL. */
214 insert_decl_map (id, decl, t);
216 if (!DECL_P (t))
217 return t;
219 /* Remap types, if necessary. */
220 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
221 if (TREE_CODE (t) == TYPE_DECL)
222 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
224 /* Remap sizes as necessary. */
225 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
226 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
228 /* If fields, do likewise for offset and qualifier. */
229 if (TREE_CODE (t) == FIELD_DECL)
231 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
232 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
233 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
236 if (cfun && gimple_in_ssa_p (cfun)
237 && (TREE_CODE (t) == VAR_DECL
238 || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
240 tree def = gimple_default_def (id->src_cfun, decl);
241 get_var_ann (t);
242 if (TREE_CODE (decl) != PARM_DECL && def)
244 tree map = remap_ssa_name (def, id);
245 /* Watch out RESULT_DECLs whose SSA names map directly
246 to them. */
247 if (TREE_CODE (map) == SSA_NAME)
248 set_default_def (t, map);
250 add_referenced_var (t);
252 return t;
255 return unshare_expr ((tree) n->value);
258 static tree
259 remap_type_1 (tree type, copy_body_data *id)
261 splay_tree_node node;
262 tree new, t;
264 if (type == NULL)
265 return type;
267 /* See if we have remapped this type. */
268 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
269 if (node)
270 return (tree) node->value;
272 /* The type only needs remapping if it's variably modified. */
273 if (! variably_modified_type_p (type, id->src_fn))
275 insert_decl_map (id, type, type);
276 return type;
279 /* We do need a copy. build and register it now. If this is a pointer or
280 reference type, remap the designated type and make a new pointer or
281 reference type. */
282 if (TREE_CODE (type) == POINTER_TYPE)
284 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
285 TYPE_MODE (type),
286 TYPE_REF_CAN_ALIAS_ALL (type));
287 insert_decl_map (id, type, new);
288 return new;
290 else if (TREE_CODE (type) == REFERENCE_TYPE)
292 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
293 TYPE_MODE (type),
294 TYPE_REF_CAN_ALIAS_ALL (type));
295 insert_decl_map (id, type, new);
296 return new;
298 else
299 new = copy_node (type);
301 insert_decl_map (id, type, new);
303 /* This is a new type, not a copy of an old type. Need to reassociate
304 variants. We can handle everything except the main variant lazily. */
305 t = TYPE_MAIN_VARIANT (type);
306 if (type != t)
308 t = remap_type (t, id);
309 TYPE_MAIN_VARIANT (new) = t;
310 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
311 TYPE_NEXT_VARIANT (t) = new;
313 else
315 TYPE_MAIN_VARIANT (new) = new;
316 TYPE_NEXT_VARIANT (new) = NULL;
319 if (TYPE_STUB_DECL (type))
320 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
322 /* Lazily create pointer and reference types. */
323 TYPE_POINTER_TO (new) = NULL;
324 TYPE_REFERENCE_TO (new) = NULL;
326 switch (TREE_CODE (new))
328 case INTEGER_TYPE:
329 case REAL_TYPE:
330 case ENUMERAL_TYPE:
331 case BOOLEAN_TYPE:
332 t = TYPE_MIN_VALUE (new);
333 if (t && TREE_CODE (t) != INTEGER_CST)
334 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
336 t = TYPE_MAX_VALUE (new);
337 if (t && TREE_CODE (t) != INTEGER_CST)
338 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
339 return new;
341 case FUNCTION_TYPE:
342 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
343 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
344 return new;
346 case ARRAY_TYPE:
347 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
348 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
349 break;
351 case RECORD_TYPE:
352 case UNION_TYPE:
353 case QUAL_UNION_TYPE:
355 tree f, nf = NULL;
357 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
359 t = remap_decl (f, id);
360 DECL_CONTEXT (t) = new;
361 TREE_CHAIN (t) = nf;
362 nf = t;
364 TYPE_FIELDS (new) = nreverse (nf);
366 break;
368 case OFFSET_TYPE:
369 default:
370 /* Shouldn't have been thought variable sized. */
371 gcc_unreachable ();
374 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
375 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
377 return new;
380 tree
381 remap_type (tree type, copy_body_data *id)
383 splay_tree_node node;
385 if (type == NULL)
386 return type;
388 /* See if we have remapped this type. */
389 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
390 if (node)
391 return (tree) node->value;
393 /* The type only needs remapping if it's variably modified. */
394 if (! variably_modified_type_p (type, id->src_fn))
396 insert_decl_map (id, type, type);
397 return type;
400 return remap_type_1 (type, id);
403 static tree
404 remap_decls (tree decls, copy_body_data *id)
406 tree old_var;
407 tree new_decls = NULL_TREE;
409 /* Remap its variables. */
410 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
412 tree new_var;
414 /* We can not chain the local static declarations into the unexpanded_var_list
415 as we can't duplicate them or break one decl rule. Go ahead and link
416 them into unexpanded_var_list. */
417 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
418 && !DECL_EXTERNAL (old_var))
420 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
421 cfun->unexpanded_var_list);
422 continue;
425 /* Remap the variable. */
426 new_var = remap_decl (old_var, id);
428 /* If we didn't remap this variable, so we can't mess with its
429 TREE_CHAIN. If we remapped this variable to the return slot, it's
430 already declared somewhere else, so don't declare it here. */
431 if (!new_var || new_var == id->retvar)
433 else
435 gcc_assert (DECL_P (new_var));
436 TREE_CHAIN (new_var) = new_decls;
437 new_decls = new_var;
441 return nreverse (new_decls);
444 /* Copy the BLOCK to contain remapped versions of the variables
445 therein. And hook the new block into the block-tree. */
447 static void
448 remap_block (tree *block, copy_body_data *id)
450 tree old_block;
451 tree new_block;
452 tree fn;
454 /* Make the new block. */
455 old_block = *block;
456 new_block = make_node (BLOCK);
457 TREE_USED (new_block) = TREE_USED (old_block);
458 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
459 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
460 *block = new_block;
462 /* Remap its variables. */
463 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
465 fn = id->dst_fn;
467 if (id->transform_lang_insert_block)
468 lang_hooks.decls.insert_block (new_block);
470 /* Remember the remapped block. */
471 insert_decl_map (id, old_block, new_block);
474 /* Copy the whole block tree and root it in id->block. */
475 static tree
476 remap_blocks (tree block, copy_body_data *id)
478 tree t;
479 tree new = block;
481 if (!block)
482 return NULL;
484 remap_block (&new, id);
485 gcc_assert (new != block);
486 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
487 add_lexical_block (new, remap_blocks (t, id));
488 return new;
491 static void
492 copy_statement_list (tree *tp)
494 tree_stmt_iterator oi, ni;
495 tree new;
497 new = alloc_stmt_list ();
498 ni = tsi_start (new);
499 oi = tsi_start (*tp);
500 *tp = new;
502 for (; !tsi_end_p (oi); tsi_next (&oi))
503 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
506 static void
507 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
509 tree block = BIND_EXPR_BLOCK (*tp);
510 /* Copy (and replace) the statement. */
511 copy_tree_r (tp, walk_subtrees, NULL);
512 if (block)
514 remap_block (&block, id);
515 BIND_EXPR_BLOCK (*tp) = block;
518 if (BIND_EXPR_VARS (*tp))
519 /* This will remap a lot of the same decls again, but this should be
520 harmless. */
521 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
524 /* Called from copy_body_id via walk_tree. DATA is really an
525 `copy_body_data *'. */
527 tree
528 copy_body_r (tree *tp, int *walk_subtrees, void *data)
530 copy_body_data *id = (copy_body_data *) data;
531 tree fn = id->src_fn;
532 tree new_block;
534 /* Begin by recognizing trees that we'll completely rewrite for the
535 inlining context. Our output for these trees is completely
536 different from out input (e.g. RETURN_EXPR is deleted, and morphs
537 into an edge). Further down, we'll handle trees that get
538 duplicated and/or tweaked. */
540 /* When requested, RETURN_EXPRs should be transformed to just the
541 contained GIMPLE_MODIFY_STMT. The branch semantics of the return will
542 be handled elsewhere by manipulating the CFG rather than a statement. */
543 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
545 tree assignment = TREE_OPERAND (*tp, 0);
547 /* If we're returning something, just turn that into an
548 assignment into the equivalent of the original RESULT_DECL.
549 If the "assignment" is just the result decl, the result
550 decl has already been set (e.g. a recent "foo (&result_decl,
551 ...)"); just toss the entire RETURN_EXPR. */
552 if (assignment && TREE_CODE (assignment) == GIMPLE_MODIFY_STMT)
554 /* Replace the RETURN_EXPR with (a copy of) the
555 GIMPLE_MODIFY_STMT hanging underneath. */
556 *tp = copy_node (assignment);
558 else /* Else the RETURN_EXPR returns no value. */
560 *tp = NULL;
561 return (tree) (void *)1;
564 else if (TREE_CODE (*tp) == SSA_NAME)
566 *tp = remap_ssa_name (*tp, id);
567 *walk_subtrees = 0;
568 return NULL;
571 /* Local variables and labels need to be replaced by equivalent
572 variables. We don't want to copy static variables; there's only
573 one of those, no matter how many times we inline the containing
574 function. Similarly for globals from an outer function. */
575 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
577 tree new_decl;
579 /* Remap the declaration. */
580 new_decl = remap_decl (*tp, id);
581 gcc_assert (new_decl);
582 /* Replace this variable with the copy. */
583 STRIP_TYPE_NOPS (new_decl);
584 *tp = new_decl;
585 *walk_subtrees = 0;
587 else if (TREE_CODE (*tp) == STATEMENT_LIST)
588 copy_statement_list (tp);
589 else if (TREE_CODE (*tp) == SAVE_EXPR)
590 remap_save_expr (tp, id->decl_map, walk_subtrees);
591 else if (TREE_CODE (*tp) == LABEL_DECL
592 && (! DECL_CONTEXT (*tp)
593 || decl_function_context (*tp) == id->src_fn))
594 /* These may need to be remapped for EH handling. */
595 *tp = remap_decl (*tp, id);
596 else if (TREE_CODE (*tp) == BIND_EXPR)
597 copy_bind_expr (tp, walk_subtrees, id);
598 /* Types may need remapping as well. */
599 else if (TYPE_P (*tp))
600 *tp = remap_type (*tp, id);
602 /* If this is a constant, we have to copy the node iff the type will be
603 remapped. copy_tree_r will not copy a constant. */
604 else if (CONSTANT_CLASS_P (*tp))
606 tree new_type = remap_type (TREE_TYPE (*tp), id);
608 if (new_type == TREE_TYPE (*tp))
609 *walk_subtrees = 0;
611 else if (TREE_CODE (*tp) == INTEGER_CST)
612 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
613 TREE_INT_CST_HIGH (*tp));
614 else
616 *tp = copy_node (*tp);
617 TREE_TYPE (*tp) = new_type;
621 /* Otherwise, just copy the node. Note that copy_tree_r already
622 knows not to copy VAR_DECLs, etc., so this is safe. */
623 else
625 /* Here we handle trees that are not completely rewritten.
626 First we detect some inlining-induced bogosities for
627 discarding. */
628 if (TREE_CODE (*tp) == GIMPLE_MODIFY_STMT
629 && GIMPLE_STMT_OPERAND (*tp, 0) == GIMPLE_STMT_OPERAND (*tp, 1)
630 && (lang_hooks.tree_inlining.auto_var_in_fn_p
631 (GIMPLE_STMT_OPERAND (*tp, 0), fn)))
633 /* Some assignments VAR = VAR; don't generate any rtl code
634 and thus don't count as variable modification. Avoid
635 keeping bogosities like 0 = 0. */
636 tree decl = GIMPLE_STMT_OPERAND (*tp, 0), value;
637 splay_tree_node n;
639 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
640 if (n)
642 value = (tree) n->value;
643 STRIP_TYPE_NOPS (value);
644 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
646 *tp = build_empty_stmt ();
647 return copy_body_r (tp, walk_subtrees, data);
651 else if (TREE_CODE (*tp) == INDIRECT_REF)
653 /* Get rid of *& from inline substitutions that can happen when a
654 pointer argument is an ADDR_EXPR. */
655 tree decl = TREE_OPERAND (*tp, 0);
656 splay_tree_node n;
658 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
659 if (n)
661 tree new;
662 tree old;
663 /* If we happen to get an ADDR_EXPR in n->value, strip
664 it manually here as we'll eventually get ADDR_EXPRs
665 which lie about their types pointed to. In this case
666 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
667 but we absolutely rely on that. As fold_indirect_ref
668 does other useful transformations, try that first, though. */
669 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
670 new = unshare_expr ((tree)n->value);
671 old = *tp;
672 *tp = fold_indirect_ref_1 (type, new);
673 if (! *tp)
675 if (TREE_CODE (new) == ADDR_EXPR)
676 *tp = TREE_OPERAND (new, 0);
677 else
679 *tp = build1 (INDIRECT_REF, type, new);
680 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
683 *walk_subtrees = 0;
684 return NULL;
688 /* Here is the "usual case". Copy this tree node, and then
689 tweak some special cases. */
690 copy_tree_r (tp, walk_subtrees, NULL);
692 /* Global variables we didn't seen yet needs to go into referenced
693 vars. */
694 if (gimple_in_ssa_p (cfun) && TREE_CODE (*tp) == VAR_DECL)
695 add_referenced_var (*tp);
697 /* If EXPR has block defined, map it to newly constructed block.
698 When inlining we want EXPRs without block appear in the block
699 of function call. */
700 if (EXPR_P (*tp) || GIMPLE_STMT_P (*tp))
702 new_block = id->block;
703 if (TREE_BLOCK (*tp))
705 splay_tree_node n;
706 n = splay_tree_lookup (id->decl_map,
707 (splay_tree_key) TREE_BLOCK (*tp));
708 gcc_assert (n);
709 new_block = (tree) n->value;
711 TREE_BLOCK (*tp) = new_block;
714 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
715 TREE_OPERAND (*tp, 0) =
716 build_int_cst
717 (NULL_TREE,
718 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
720 if (!GIMPLE_TUPLE_P (*tp))
721 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
723 /* The copied TARGET_EXPR has never been expanded, even if the
724 original node was expanded already. */
725 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
727 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
728 TREE_OPERAND (*tp, 3) = NULL_TREE;
731 /* Variable substitution need not be simple. In particular, the
732 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
733 and friends are up-to-date. */
734 else if (TREE_CODE (*tp) == ADDR_EXPR)
736 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
737 /* Handle the case where we substituted an INDIRECT_REF
738 into the operand of the ADDR_EXPR. */
739 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
740 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
741 else
742 recompute_tree_invariant_for_addr_expr (*tp);
743 *walk_subtrees = 0;
747 /* Keep iterating. */
748 return NULL_TREE;
751 /* Copy basic block, scale profile accordingly. Edges will be taken care of
752 later */
754 static basic_block
755 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
757 block_stmt_iterator bsi, copy_bsi;
758 basic_block copy_basic_block;
760 /* create_basic_block() will append every new block to
761 basic_block_info automatically. */
762 copy_basic_block = create_basic_block (NULL, (void *) 0,
763 (basic_block) bb->prev_bb->aux);
764 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
765 copy_basic_block->frequency = (bb->frequency
766 * frequency_scale / REG_BR_PROB_BASE);
767 copy_bsi = bsi_start (copy_basic_block);
769 for (bsi = bsi_start (bb);
770 !bsi_end_p (bsi); bsi_next (&bsi))
772 tree stmt = bsi_stmt (bsi);
773 tree orig_stmt = stmt;
775 walk_tree (&stmt, copy_body_r, id, NULL);
777 /* RETURN_EXPR might be removed,
778 this is signalled by making stmt pointer NULL. */
779 if (stmt)
781 tree call, decl;
783 gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
785 /* With return slot optimization we can end up with
786 non-gimple (foo *)&this->m, fix that here. */
787 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
788 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == NOP_EXPR
789 && !is_gimple_val (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0)))
790 gimplify_stmt (&stmt);
792 bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
794 /* Process new statement. gimplify_stmt possibly turned statement
795 into multiple statements, we need to process all of them. */
796 while (!bsi_end_p (copy_bsi))
798 stmt = bsi_stmt (copy_bsi);
799 call = get_call_expr_in (stmt);
800 /* We're duplicating a CALL_EXPR. Find any corresponding
801 callgraph edges and update or duplicate them. */
802 if (call && (decl = get_callee_fndecl (call)))
804 struct cgraph_node *node;
805 struct cgraph_edge *edge;
807 switch (id->transform_call_graph_edges)
809 case CB_CGE_DUPLICATE:
810 edge = cgraph_edge (id->src_node, orig_stmt);
811 if (edge)
812 cgraph_clone_edge (edge, id->dst_node, stmt,
813 REG_BR_PROB_BASE, 1, true);
814 break;
816 case CB_CGE_MOVE_CLONES:
817 for (node = id->dst_node->next_clone;
818 node;
819 node = node->next_clone)
821 edge = cgraph_edge (node, orig_stmt);
822 gcc_assert (edge);
823 cgraph_set_call_stmt (edge, stmt);
825 /* FALLTHRU */
827 case CB_CGE_MOVE:
828 edge = cgraph_edge (id->dst_node, orig_stmt);
829 if (edge)
830 cgraph_set_call_stmt (edge, stmt);
831 break;
833 default:
834 gcc_unreachable ();
837 /* If you think we can abort here, you are wrong.
838 There is no region 0 in tree land. */
839 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
840 != 0);
842 if (tree_could_throw_p (stmt))
844 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
845 /* Add an entry for the copied tree in the EH hashtable.
846 When cloning or versioning, use the hashtable in
847 cfun, and just copy the EH number. When inlining, use the
848 hashtable in the caller, and adjust the region number. */
849 if (region > 0)
850 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
852 /* If this tree doesn't have a region associated with it,
853 and there is a "current region,"
854 then associate this tree with the current region
855 and add edges associated with this region. */
856 if ((lookup_stmt_eh_region_fn (id->src_cfun,
857 orig_stmt) <= 0
858 && id->eh_region > 0)
859 && tree_could_throw_p (stmt))
860 add_stmt_to_eh_region (stmt, id->eh_region);
862 if (gimple_in_ssa_p (cfun))
864 ssa_op_iter i;
865 tree def;
867 find_new_referenced_vars (bsi_stmt_ptr (copy_bsi));
868 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
869 if (TREE_CODE (def) == SSA_NAME)
870 SSA_NAME_DEF_STMT (def) = stmt;
872 bsi_next (&copy_bsi);
874 copy_bsi = bsi_last (copy_basic_block);
877 return copy_basic_block;
880 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
881 form is quite easy, since dominator relationship for old basic blocks does
882 not change.
884 There is however exception where inlining might change dominator relation
885 across EH edges from basic block within inlined functions destinating
886 to landing pads in function we inline into.
888 The function mark PHI_RESULT of such PHI nodes for renaming; it is
889 safe the EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI
890 must be set. This means, that there will be no overlapping live ranges
891 for the underlying symbol.
893 This might change in future if we allow redirecting of EH edges and
894 we might want to change way build CFG pre-inlining to include
895 all the possible edges then. */
896 static void
897 update_ssa_across_eh_edges (basic_block bb)
899 edge e;
900 edge_iterator ei;
902 FOR_EACH_EDGE (e, ei, bb->succs)
903 if (!e->dest->aux
904 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
906 tree phi;
908 gcc_assert (e->flags & EDGE_EH);
909 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
911 gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI
912 (PHI_RESULT (phi)));
913 mark_sym_for_renaming
914 (SSA_NAME_VAR (PHI_RESULT (phi)));
919 /* Copy edges from BB into its copy constructed earlier, scale profile
920 accordingly. Edges will be taken care of later. Assume aux
921 pointers to point to the copies of each BB. */
922 static void
923 copy_edges_for_bb (basic_block bb, int count_scale)
925 basic_block new_bb = (basic_block) bb->aux;
926 edge_iterator ei;
927 edge old_edge;
928 block_stmt_iterator bsi;
929 int flags;
931 /* Use the indices from the original blocks to create edges for the
932 new ones. */
933 FOR_EACH_EDGE (old_edge, ei, bb->succs)
934 if (!(old_edge->flags & EDGE_EH))
936 edge new;
938 flags = old_edge->flags;
940 /* Return edges do get a FALLTHRU flag when the get inlined. */
941 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
942 && old_edge->dest->aux != EXIT_BLOCK_PTR)
943 flags |= EDGE_FALLTHRU;
944 new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
945 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
946 new->probability = old_edge->probability;
949 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
950 return;
952 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
954 tree copy_stmt;
956 copy_stmt = bsi_stmt (bsi);
957 update_stmt (copy_stmt);
958 if (gimple_in_ssa_p (cfun))
959 mark_symbols_for_renaming (copy_stmt);
960 /* Do this before the possible split_block. */
961 bsi_next (&bsi);
963 /* If this tree could throw an exception, there are two
964 cases where we need to add abnormal edge(s): the
965 tree wasn't in a region and there is a "current
966 region" in the caller; or the original tree had
967 EH edges. In both cases split the block after the tree,
968 and add abnormal edge(s) as needed; we need both
969 those from the callee and the caller.
970 We check whether the copy can throw, because the const
971 propagation can change an INDIRECT_REF which throws
972 into a COMPONENT_REF which doesn't. If the copy
973 can throw, the original could also throw. */
975 if (tree_can_throw_internal (copy_stmt))
977 if (!bsi_end_p (bsi))
978 /* Note that bb's predecessor edges aren't necessarily
979 right at this point; split_block doesn't care. */
981 edge e = split_block (new_bb, copy_stmt);
983 new_bb = e->dest;
984 new_bb->aux = e->src->aux;
985 bsi = bsi_start (new_bb);
988 make_eh_edges (copy_stmt);
990 if (gimple_in_ssa_p (cfun))
991 update_ssa_across_eh_edges (bb_for_stmt (copy_stmt));
996 /* Copy the PHIs. All blocks and edges are copied, some blocks
997 was possibly split and new outgoing EH edges inserted.
998 BB points to the block of original function and AUX pointers links
999 the original and newly copied blocks. */
1001 static void
1002 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1004 basic_block new_bb = bb->aux;
1005 edge_iterator ei;
1006 tree phi;
1008 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1010 tree res = PHI_RESULT (phi);
1011 tree new_res = res;
1012 tree new_phi;
1013 edge new_edge;
1015 if (is_gimple_reg (res))
1017 walk_tree (&new_res, copy_body_r, id, NULL);
1018 SSA_NAME_DEF_STMT (new_res)
1019 = new_phi = create_phi_node (new_res, new_bb);
1020 FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1022 edge old_edge = find_edge (new_edge->src->aux, bb);
1023 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1024 tree new_arg = arg;
1026 walk_tree (&new_arg, copy_body_r, id, NULL);
1027 gcc_assert (new_arg);
1028 add_phi_arg (new_phi, new_arg, new_edge);
1034 /* Wrapper for remap_decl so it can be used as a callback. */
1035 static tree
1036 remap_decl_1 (tree decl, void *data)
1038 return remap_decl (decl, (copy_body_data *) data);
1041 /* Build struct function and associated datastructures for the new clone
1042 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1044 static void
1045 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1046 int frequency)
1048 struct function *new_cfun
1049 = (struct function *) ggc_alloc_cleared (sizeof (struct function));
1050 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1051 int count_scale, frequency_scale;
1053 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1054 count_scale = (REG_BR_PROB_BASE * count
1055 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1056 else
1057 count_scale = 1;
1059 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1060 frequency_scale = (REG_BR_PROB_BASE * frequency
1062 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1063 else
1064 frequency_scale = count_scale;
1066 /* Register specific tree functions. */
1067 tree_register_cfg_hooks ();
1068 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
1069 VALUE_HISTOGRAMS (new_cfun) = NULL;
1070 new_cfun->unexpanded_var_list = NULL;
1071 new_cfun->cfg = NULL;
1072 new_cfun->decl = new_fndecl /*= copy_node (callee_fndecl)*/;
1073 new_cfun->ib_boundaries_block = NULL;
1074 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
1075 push_cfun (new_cfun);
1076 init_empty_tree_cfg ();
1078 ENTRY_BLOCK_PTR->count =
1079 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1080 REG_BR_PROB_BASE);
1081 ENTRY_BLOCK_PTR->frequency =
1082 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1083 frequency_scale / REG_BR_PROB_BASE);
1084 EXIT_BLOCK_PTR->count =
1085 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1086 REG_BR_PROB_BASE);
1087 EXIT_BLOCK_PTR->frequency =
1088 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1089 frequency_scale / REG_BR_PROB_BASE);
1090 if (src_cfun->eh)
1091 init_eh_for_function ();
1093 if (src_cfun->gimple_df)
1095 init_tree_ssa ();
1096 cfun->gimple_df->in_ssa_p = true;
1097 init_ssa_operands ();
1099 pop_cfun ();
1102 /* Make a copy of the body of FN so that it can be inserted inline in
1103 another function. Walks FN via CFG, returns new fndecl. */
1105 static tree
1106 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1107 basic_block entry_block_map, basic_block exit_block_map)
1109 tree callee_fndecl = id->src_fn;
1110 /* Original cfun for the callee, doesn't change. */
1111 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1112 struct function *cfun_to_copy;
1113 basic_block bb;
1114 tree new_fndecl = NULL;
1115 int count_scale, frequency_scale;
1116 int last;
1118 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1119 count_scale = (REG_BR_PROB_BASE * count
1120 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1121 else
1122 count_scale = 1;
1124 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1125 frequency_scale = (REG_BR_PROB_BASE * frequency
1127 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1128 else
1129 frequency_scale = count_scale;
1131 /* Register specific tree functions. */
1132 tree_register_cfg_hooks ();
1134 /* Must have a CFG here at this point. */
1135 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1136 (DECL_STRUCT_FUNCTION (callee_fndecl)));
1138 cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1141 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1142 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1143 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1144 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1146 /* Duplicate any exception-handling regions. */
1147 if (cfun->eh)
1149 id->eh_region_offset
1150 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1151 0, id->eh_region);
1153 /* Use aux pointers to map the original blocks to copy. */
1154 FOR_EACH_BB_FN (bb, cfun_to_copy)
1156 basic_block new = copy_bb (id, bb, frequency_scale, count_scale);
1157 bb->aux = new;
1158 new->aux = bb;
1161 last = n_basic_blocks;
1162 /* Now that we've duplicated the blocks, duplicate their edges. */
1163 FOR_ALL_BB_FN (bb, cfun_to_copy)
1164 copy_edges_for_bb (bb, count_scale);
1165 if (gimple_in_ssa_p (cfun))
1166 FOR_ALL_BB_FN (bb, cfun_to_copy)
1167 copy_phis_for_bb (bb, id);
1168 FOR_ALL_BB_FN (bb, cfun_to_copy)
1170 ((basic_block)bb->aux)->aux = NULL;
1171 bb->aux = NULL;
1173 /* Zero out AUX fields of newly created block during EH edge
1174 insertion. */
1175 for (; last < n_basic_blocks; last++)
1176 BASIC_BLOCK (last)->aux = NULL;
1177 entry_block_map->aux = NULL;
1178 exit_block_map->aux = NULL;
1180 return new_fndecl;
1183 /* Make a copy of the body of FN so that it can be inserted inline in
1184 another function. */
1186 static tree
1187 copy_generic_body (copy_body_data *id)
1189 tree body;
1190 tree fndecl = id->src_fn;
1192 body = DECL_SAVED_TREE (fndecl);
1193 walk_tree (&body, copy_body_r, id, NULL);
1195 return body;
1198 static tree
1199 copy_body (copy_body_data *id, gcov_type count, int frequency,
1200 basic_block entry_block_map, basic_block exit_block_map)
1202 tree fndecl = id->src_fn;
1203 tree body;
1205 /* If this body has a CFG, walk CFG and copy. */
1206 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1207 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1209 return body;
1212 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1213 defined in function FN, or of a data member thereof. */
1215 static bool
1216 self_inlining_addr_expr (tree value, tree fn)
1218 tree var;
1220 if (TREE_CODE (value) != ADDR_EXPR)
1221 return false;
1223 var = get_base_address (TREE_OPERAND (value, 0));
1225 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
1228 static void
1229 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1230 basic_block bb, tree *vars)
1232 tree init_stmt;
1233 tree var;
1234 tree var_sub;
1235 tree rhs = value ? fold_convert (TREE_TYPE (p), value) : NULL;
1236 tree def = (gimple_in_ssa_p (cfun)
1237 ? gimple_default_def (id->src_cfun, p) : NULL);
1239 /* If the parameter is never assigned to, has no SSA_NAMEs created,
1240 we may not need to create a new variable here at all. Instead, we may
1241 be able to just use the argument value. */
1242 if (TREE_READONLY (p)
1243 && !TREE_ADDRESSABLE (p)
1244 && value && !TREE_SIDE_EFFECTS (value)
1245 && !def)
1247 /* We may produce non-gimple trees by adding NOPs or introduce
1248 invalid sharing when operand is not really constant.
1249 It is not big deal to prohibit constant propagation here as
1250 we will constant propagate in DOM1 pass anyway. */
1251 if (is_gimple_min_invariant (value)
1252 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1253 /* We have to be very careful about ADDR_EXPR. Make sure
1254 the base variable isn't a local variable of the inlined
1255 function, e.g., when doing recursive inlining, direct or
1256 mutually-recursive or whatever, which is why we don't
1257 just test whether fn == current_function_decl. */
1258 && ! self_inlining_addr_expr (value, fn))
1260 insert_decl_map (id, p, value);
1261 return;
1265 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1266 here since the type of this decl must be visible to the calling
1267 function. */
1268 var = copy_decl_to_var (p, id);
1269 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
1271 get_var_ann (var);
1272 add_referenced_var (var);
1275 /* See if the frontend wants to pass this by invisible reference. If
1276 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1277 replace uses of the PARM_DECL with dereferences. */
1278 if (TREE_TYPE (var) != TREE_TYPE (p)
1279 && POINTER_TYPE_P (TREE_TYPE (var))
1280 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1282 insert_decl_map (id, var, var);
1283 var_sub = build_fold_indirect_ref (var);
1285 else
1286 var_sub = var;
1288 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1289 that way, when the PARM_DECL is encountered, it will be
1290 automatically replaced by the VAR_DECL. */
1291 insert_decl_map (id, p, var_sub);
1293 /* Declare this new variable. */
1294 TREE_CHAIN (var) = *vars;
1295 *vars = var;
1297 /* Make gimplifier happy about this variable. */
1298 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1300 /* Even if P was TREE_READONLY, the new VAR should not be.
1301 In the original code, we would have constructed a
1302 temporary, and then the function body would have never
1303 changed the value of P. However, now, we will be
1304 constructing VAR directly. The constructor body may
1305 change its value multiple times as it is being
1306 constructed. Therefore, it must not be TREE_READONLY;
1307 the back-end assumes that TREE_READONLY variable is
1308 assigned to only once. */
1309 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1310 TREE_READONLY (var) = 0;
1312 /* If there is no setup required and we are in SSA, take the easy route
1313 replacing all SSA names representing the function parameter by the
1314 SSA name passed to function.
1316 We need to construct map for the variable anyway as it might be used
1317 in different SSA names when parameter is set in function.
1319 FIXME: This usually kills the last connection in between inlined
1320 function parameter and the actual value in debug info. Can we do
1321 better here? If we just inserted the statement, copy propagation
1322 would kill it anyway as it always did in older versions of GCC.
1324 We might want to introduce a notion that single SSA_NAME might
1325 represent multiple variables for purposes of debugging. */
1326 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
1327 && (TREE_CODE (rhs) == SSA_NAME
1328 || is_gimple_min_invariant (rhs)))
1330 insert_decl_map (id, def, rhs);
1331 return;
1334 /* Initialize this VAR_DECL from the equivalent argument. Convert
1335 the argument to the proper type in case it was promoted. */
1336 if (value)
1338 block_stmt_iterator bsi = bsi_last (bb);
1340 if (rhs == error_mark_node)
1342 insert_decl_map (id, p, var_sub);
1343 return;
1346 STRIP_USELESS_TYPE_CONVERSION (rhs);
1348 /* We want to use GIMPLE_MODIFY_STMT, not INIT_EXPR here so that we
1349 keep our trees in gimple form. */
1350 if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
1352 def = remap_ssa_name (def, id);
1353 init_stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), def, rhs);
1354 SSA_NAME_DEF_STMT (def) = init_stmt;
1355 SSA_NAME_IS_DEFAULT_DEF (def) = 0;
1356 set_default_def (var, NULL);
1358 else
1359 init_stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, rhs);
1361 /* If we did not create a gimple value and we did not create a gimple
1362 cast of a gimple value, then we will need to gimplify INIT_STMTS
1363 at the end. Note that is_gimple_cast only checks the outer
1364 tree code, not its operand. Thus the explicit check that its
1365 operand is a gimple value. */
1366 if ((!is_gimple_val (rhs)
1367 && (!is_gimple_cast (rhs)
1368 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1369 || !is_gimple_reg (var))
1371 tree_stmt_iterator i;
1373 push_gimplify_context ();
1374 gimplify_stmt (&init_stmt);
1375 if (gimple_in_ssa_p (cfun)
1376 && init_stmt && TREE_CODE (init_stmt) == STATEMENT_LIST)
1378 /* The replacement can expose previously unreferenced
1379 variables. */
1380 for (i = tsi_start (init_stmt); !tsi_end_p (i); tsi_next (&i))
1381 find_new_referenced_vars (tsi_stmt_ptr (i));
1383 pop_gimplify_context (NULL);
1386 /* If VAR represents a zero-sized variable, it's possible that the
1387 assignment statment may result in no gimple statements. */
1388 if (init_stmt)
1389 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1390 if (gimple_in_ssa_p (cfun))
1391 for (;!bsi_end_p (bsi); bsi_next (&bsi))
1392 mark_symbols_for_renaming (bsi_stmt (bsi));
1396 /* Generate code to initialize the parameters of the function at the
1397 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1399 static void
1400 initialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
1401 tree fn, basic_block bb)
1403 tree parms;
1404 tree a;
1405 tree p;
1406 tree vars = NULL_TREE;
1407 int argnum = 0;
1409 /* Figure out what the parameters are. */
1410 parms = DECL_ARGUMENTS (fn);
1412 /* Loop through the parameter declarations, replacing each with an
1413 equivalent VAR_DECL, appropriately initialized. */
1414 for (p = parms, a = args; p;
1415 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1417 tree value;
1419 ++argnum;
1421 /* Find the initializer. */
1422 value = lang_hooks.tree_inlining.convert_parm_for_inlining
1423 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1425 setup_one_parameter (id, p, value, fn, bb, &vars);
1428 /* Initialize the static chain. */
1429 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1430 gcc_assert (fn != current_function_decl);
1431 if (p)
1433 /* No static chain? Seems like a bug in tree-nested.c. */
1434 gcc_assert (static_chain);
1436 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1439 declare_inline_vars (id->block, vars);
1442 /* Declare a return variable to replace the RESULT_DECL for the
1443 function we are calling. An appropriate DECL_STMT is returned.
1444 The USE_STMT is filled to contain a use of the declaration to
1445 indicate the return value of the function.
1447 RETURN_SLOT, if non-null is place where to store the result. It
1448 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
1449 was the LHS of the GIMPLE_MODIFY_STMT to which this call is the RHS.
1451 The return value is a (possibly null) value that is the result of the
1452 function as seen by the callee. *USE_P is a (possibly null) value that
1453 holds the result as seen by the caller. */
1455 static tree
1456 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
1457 tree *use_p)
1459 tree callee = id->src_fn;
1460 tree caller = id->dst_fn;
1461 tree result = DECL_RESULT (callee);
1462 tree callee_type = TREE_TYPE (result);
1463 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1464 tree var, use;
1466 /* We don't need to do anything for functions that don't return
1467 anything. */
1468 if (!result || VOID_TYPE_P (callee_type))
1470 *use_p = NULL_TREE;
1471 return NULL_TREE;
1474 /* If there was a return slot, then the return value is the
1475 dereferenced address of that object. */
1476 if (return_slot)
1478 /* The front end shouldn't have used both return_slot and
1479 a modify expression. */
1480 gcc_assert (!modify_dest);
1481 if (DECL_BY_REFERENCE (result))
1483 tree return_slot_addr = build_fold_addr_expr (return_slot);
1484 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
1486 /* We are going to construct *&return_slot and we can't do that
1487 for variables believed to be not addressable.
1489 FIXME: This check possibly can match, because values returned
1490 via return slot optimization are not believed to have address
1491 taken by alias analysis. */
1492 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
1493 if (gimple_in_ssa_p (cfun))
1495 HOST_WIDE_INT bitsize;
1496 HOST_WIDE_INT bitpos;
1497 tree offset;
1498 enum machine_mode mode;
1499 int unsignedp;
1500 int volatilep;
1501 tree base;
1502 base = get_inner_reference (return_slot, &bitsize, &bitpos,
1503 &offset,
1504 &mode, &unsignedp, &volatilep,
1505 false);
1506 if (TREE_CODE (base) == INDIRECT_REF)
1507 base = TREE_OPERAND (base, 0);
1508 if (TREE_CODE (base) == SSA_NAME)
1509 base = SSA_NAME_VAR (base);
1510 mark_sym_for_renaming (base);
1512 var = return_slot_addr;
1514 else
1516 var = return_slot;
1517 gcc_assert (TREE_CODE (var) != SSA_NAME);
1519 if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1520 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1521 && !DECL_GIMPLE_REG_P (result)
1522 && DECL_P (var))
1523 DECL_GIMPLE_REG_P (var) = 0;
1524 use = NULL;
1525 goto done;
1528 /* All types requiring non-trivial constructors should have been handled. */
1529 gcc_assert (!TREE_ADDRESSABLE (callee_type));
1531 /* Attempt to avoid creating a new temporary variable. */
1532 if (modify_dest
1533 && TREE_CODE (modify_dest) != SSA_NAME)
1535 bool use_it = false;
1537 /* We can't use MODIFY_DEST if there's type promotion involved. */
1538 if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1539 use_it = false;
1541 /* ??? If we're assigning to a variable sized type, then we must
1542 reuse the destination variable, because we've no good way to
1543 create variable sized temporaries at this point. */
1544 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1545 use_it = true;
1547 /* If the callee cannot possibly modify MODIFY_DEST, then we can
1548 reuse it as the result of the call directly. Don't do this if
1549 it would promote MODIFY_DEST to addressable. */
1550 else if (TREE_ADDRESSABLE (result))
1551 use_it = false;
1552 else
1554 tree base_m = get_base_address (modify_dest);
1556 /* If the base isn't a decl, then it's a pointer, and we don't
1557 know where that's going to go. */
1558 if (!DECL_P (base_m))
1559 use_it = false;
1560 else if (is_global_var (base_m))
1561 use_it = false;
1562 else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1563 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1564 && !DECL_GIMPLE_REG_P (result)
1565 && DECL_GIMPLE_REG_P (base_m))
1566 use_it = false;
1567 else if (!TREE_ADDRESSABLE (base_m))
1568 use_it = true;
1571 if (use_it)
1573 var = modify_dest;
1574 use = NULL;
1575 goto done;
1579 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1581 var = copy_result_decl_to_var (result, id);
1582 if (gimple_in_ssa_p (cfun))
1584 get_var_ann (var);
1585 add_referenced_var (var);
1588 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1589 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1590 = tree_cons (NULL_TREE, var,
1591 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1593 /* Do not have the rest of GCC warn about this variable as it should
1594 not be visible to the user. */
1595 TREE_NO_WARNING (var) = 1;
1597 declare_inline_vars (id->block, var);
1599 /* Build the use expr. If the return type of the function was
1600 promoted, convert it back to the expected type. */
1601 use = var;
1602 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1603 use = fold_convert (caller_type, var);
1605 STRIP_USELESS_TYPE_CONVERSION (use);
1607 if (DECL_BY_REFERENCE (result))
1608 var = build_fold_addr_expr (var);
1610 done:
1611 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1612 way, when the RESULT_DECL is encountered, it will be
1613 automatically replaced by the VAR_DECL. */
1614 insert_decl_map (id, result, var);
1616 /* Remember this so we can ignore it in remap_decls. */
1617 id->retvar = var;
1619 *use_p = use;
1620 return var;
1623 /* Returns nonzero if a function can be inlined as a tree. */
1625 bool
1626 tree_inlinable_function_p (tree fn)
1628 return inlinable_function_p (fn);
1631 static const char *inline_forbidden_reason;
1633 static tree
1634 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1635 void *fnp)
1637 tree node = *nodep;
1638 tree fn = (tree) fnp;
1639 tree t;
1641 switch (TREE_CODE (node))
1643 case CALL_EXPR:
1644 /* Refuse to inline alloca call unless user explicitly forced so as
1645 this may change program's memory overhead drastically when the
1646 function using alloca is called in loop. In GCC present in
1647 SPEC2000 inlining into schedule_block cause it to require 2GB of
1648 RAM instead of 256MB. */
1649 if (alloca_call_p (node)
1650 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1652 inline_forbidden_reason
1653 = G_("function %q+F can never be inlined because it uses "
1654 "alloca (override using the always_inline attribute)");
1655 return node;
1657 t = get_callee_fndecl (node);
1658 if (! t)
1659 break;
1661 /* We cannot inline functions that call setjmp. */
1662 if (setjmp_call_p (t))
1664 inline_forbidden_reason
1665 = G_("function %q+F can never be inlined because it uses setjmp");
1666 return node;
1669 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1670 switch (DECL_FUNCTION_CODE (t))
1672 /* We cannot inline functions that take a variable number of
1673 arguments. */
1674 case BUILT_IN_VA_START:
1675 case BUILT_IN_STDARG_START:
1676 case BUILT_IN_NEXT_ARG:
1677 case BUILT_IN_VA_END:
1678 inline_forbidden_reason
1679 = G_("function %q+F can never be inlined because it "
1680 "uses variable argument lists");
1681 return node;
1683 case BUILT_IN_LONGJMP:
1684 /* We can't inline functions that call __builtin_longjmp at
1685 all. The non-local goto machinery really requires the
1686 destination be in a different function. If we allow the
1687 function calling __builtin_longjmp to be inlined into the
1688 function calling __builtin_setjmp, Things will Go Awry. */
1689 inline_forbidden_reason
1690 = G_("function %q+F can never be inlined because "
1691 "it uses setjmp-longjmp exception handling");
1692 return node;
1694 case BUILT_IN_NONLOCAL_GOTO:
1695 /* Similarly. */
1696 inline_forbidden_reason
1697 = G_("function %q+F can never be inlined because "
1698 "it uses non-local goto");
1699 return node;
1701 case BUILT_IN_RETURN:
1702 case BUILT_IN_APPLY_ARGS:
1703 /* If a __builtin_apply_args caller would be inlined,
1704 it would be saving arguments of the function it has
1705 been inlined into. Similarly __builtin_return would
1706 return from the function the inline has been inlined into. */
1707 inline_forbidden_reason
1708 = G_("function %q+F can never be inlined because "
1709 "it uses __builtin_return or __builtin_apply_args");
1710 return node;
1712 default:
1713 break;
1715 break;
1717 case GOTO_EXPR:
1718 t = TREE_OPERAND (node, 0);
1720 /* We will not inline a function which uses computed goto. The
1721 addresses of its local labels, which may be tucked into
1722 global storage, are of course not constant across
1723 instantiations, which causes unexpected behavior. */
1724 if (TREE_CODE (t) != LABEL_DECL)
1726 inline_forbidden_reason
1727 = G_("function %q+F can never be inlined "
1728 "because it contains a computed goto");
1729 return node;
1731 break;
1733 case LABEL_EXPR:
1734 t = TREE_OPERAND (node, 0);
1735 if (DECL_NONLOCAL (t))
1737 /* We cannot inline a function that receives a non-local goto
1738 because we cannot remap the destination label used in the
1739 function that is performing the non-local goto. */
1740 inline_forbidden_reason
1741 = G_("function %q+F can never be inlined "
1742 "because it receives a non-local goto");
1743 return node;
1745 break;
1747 case RECORD_TYPE:
1748 case UNION_TYPE:
1749 /* We cannot inline a function of the form
1751 void F (int i) { struct S { int ar[i]; } s; }
1753 Attempting to do so produces a catch-22.
1754 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1755 UNION_TYPE nodes, then it goes into infinite recursion on a
1756 structure containing a pointer to its own type. If it doesn't,
1757 then the type node for S doesn't get adjusted properly when
1758 F is inlined.
1760 ??? This is likely no longer true, but it's too late in the 4.0
1761 cycle to try to find out. This should be checked for 4.1. */
1762 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1763 if (variably_modified_type_p (TREE_TYPE (t), NULL))
1765 inline_forbidden_reason
1766 = G_("function %q+F can never be inlined "
1767 "because it uses variable sized variables");
1768 return node;
1771 default:
1772 break;
1775 return NULL_TREE;
1778 /* Return subexpression representing possible alloca call, if any. */
1779 static tree
1780 inline_forbidden_p (tree fndecl)
1782 location_t saved_loc = input_location;
1783 block_stmt_iterator bsi;
1784 basic_block bb;
1785 tree ret = NULL_TREE;
1787 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1788 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1790 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1791 inline_forbidden_p_1, fndecl);
1792 if (ret)
1793 goto egress;
1796 egress:
1797 input_location = saved_loc;
1798 return ret;
1801 /* Returns nonzero if FN is a function that does not have any
1802 fundamental inline blocking properties. */
1804 static bool
1805 inlinable_function_p (tree fn)
1807 bool inlinable = true;
1809 /* If we've already decided this function shouldn't be inlined,
1810 there's no need to check again. */
1811 if (DECL_UNINLINABLE (fn))
1812 return false;
1814 /* See if there is any language-specific reason it cannot be
1815 inlined. (It is important that this hook be called early because
1816 in C++ it may result in template instantiation.)
1817 If the function is not inlinable for language-specific reasons,
1818 it is left up to the langhook to explain why. */
1819 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1821 /* If we don't have the function body available, we can't inline it.
1822 However, this should not be recorded since we also get here for
1823 forward declared inline functions. Therefore, return at once. */
1824 if (!DECL_SAVED_TREE (fn))
1825 return false;
1827 /* If we're not inlining at all, then we cannot inline this function. */
1828 else if (!flag_inline_trees)
1829 inlinable = false;
1831 /* Only try to inline functions if DECL_INLINE is set. This should be
1832 true for all functions declared `inline', and for all other functions
1833 as well with -finline-functions.
1835 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1836 it's the front-end that must set DECL_INLINE in this case, because
1837 dwarf2out loses if a function that does not have DECL_INLINE set is
1838 inlined anyway. That is why we have both DECL_INLINE and
1839 DECL_DECLARED_INLINE_P. */
1840 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1841 here should be redundant. */
1842 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1843 inlinable = false;
1845 else if (inline_forbidden_p (fn))
1847 /* See if we should warn about uninlinable functions. Previously,
1848 some of these warnings would be issued while trying to expand
1849 the function inline, but that would cause multiple warnings
1850 about functions that would for example call alloca. But since
1851 this a property of the function, just one warning is enough.
1852 As a bonus we can now give more details about the reason why a
1853 function is not inlinable.
1854 We only warn for functions declared `inline' by the user. */
1855 bool do_warning = (warn_inline
1856 && DECL_INLINE (fn)
1857 && DECL_DECLARED_INLINE_P (fn)
1858 && !DECL_IN_SYSTEM_HEADER (fn));
1860 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1861 sorry (inline_forbidden_reason, fn);
1862 else if (do_warning)
1863 warning (OPT_Winline, inline_forbidden_reason, fn);
1865 inlinable = false;
1868 /* Squirrel away the result so that we don't have to check again. */
1869 DECL_UNINLINABLE (fn) = !inlinable;
1871 return inlinable;
1874 /* Estimate the cost of a memory move. Use machine dependent
1875 word size and take possible memcpy call into account. */
1878 estimate_move_cost (tree type)
1880 HOST_WIDE_INT size;
1882 size = int_size_in_bytes (type);
1884 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1885 /* Cost of a memcpy call, 3 arguments and the call. */
1886 return 4;
1887 else
1888 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1891 /* Used by estimate_num_insns. Estimate number of instructions seen
1892 by given statement. */
1894 static tree
1895 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1897 int *count = (int *) data;
1898 tree x = *tp;
1900 if (IS_TYPE_OR_DECL_P (x))
1902 *walk_subtrees = 0;
1903 return NULL;
1905 /* Assume that constants and references counts nothing. These should
1906 be majorized by amount of operations among them we count later
1907 and are common target of CSE and similar optimizations. */
1908 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1909 return NULL;
1911 switch (TREE_CODE (x))
1913 /* Containers have no cost. */
1914 case TREE_LIST:
1915 case TREE_VEC:
1916 case BLOCK:
1917 case COMPONENT_REF:
1918 case BIT_FIELD_REF:
1919 case INDIRECT_REF:
1920 case ALIGN_INDIRECT_REF:
1921 case MISALIGNED_INDIRECT_REF:
1922 case ARRAY_REF:
1923 case ARRAY_RANGE_REF:
1924 case OBJ_TYPE_REF:
1925 case EXC_PTR_EXPR: /* ??? */
1926 case FILTER_EXPR: /* ??? */
1927 case COMPOUND_EXPR:
1928 case BIND_EXPR:
1929 case WITH_CLEANUP_EXPR:
1930 case NOP_EXPR:
1931 case VIEW_CONVERT_EXPR:
1932 case SAVE_EXPR:
1933 case ADDR_EXPR:
1934 case COMPLEX_EXPR:
1935 case RANGE_EXPR:
1936 case CASE_LABEL_EXPR:
1937 case SSA_NAME:
1938 case CATCH_EXPR:
1939 case EH_FILTER_EXPR:
1940 case STATEMENT_LIST:
1941 case ERROR_MARK:
1942 case NON_LVALUE_EXPR:
1943 case FDESC_EXPR:
1944 case VA_ARG_EXPR:
1945 case TRY_CATCH_EXPR:
1946 case TRY_FINALLY_EXPR:
1947 case LABEL_EXPR:
1948 case GOTO_EXPR:
1949 case RETURN_EXPR:
1950 case EXIT_EXPR:
1951 case LOOP_EXPR:
1952 case PHI_NODE:
1953 case WITH_SIZE_EXPR:
1954 case OMP_CLAUSE:
1955 case OMP_RETURN:
1956 case OMP_CONTINUE:
1957 break;
1959 /* We don't account constants for now. Assume that the cost is amortized
1960 by operations that do use them. We may re-consider this decision once
1961 we are able to optimize the tree before estimating its size and break
1962 out static initializers. */
1963 case IDENTIFIER_NODE:
1964 case INTEGER_CST:
1965 case REAL_CST:
1966 case COMPLEX_CST:
1967 case VECTOR_CST:
1968 case STRING_CST:
1969 *walk_subtrees = 0;
1970 return NULL;
1972 /* Try to estimate the cost of assignments. We have three cases to
1973 deal with:
1974 1) Simple assignments to registers;
1975 2) Stores to things that must live in memory. This includes
1976 "normal" stores to scalars, but also assignments of large
1977 structures, or constructors of big arrays;
1978 3) TARGET_EXPRs.
1980 Let us look at the first two cases, assuming we have "a = b + C":
1981 <GIMPLE_MODIFY_STMT <var_decl "a">
1982 <plus_expr <var_decl "b"> <constant C>>
1983 If "a" is a GIMPLE register, the assignment to it is free on almost
1984 any target, because "a" usually ends up in a real register. Hence
1985 the only cost of this expression comes from the PLUS_EXPR, and we
1986 can ignore the GIMPLE_MODIFY_STMT.
1987 If "a" is not a GIMPLE register, the assignment to "a" will most
1988 likely be a real store, so the cost of the GIMPLE_MODIFY_STMT is the cost
1989 of moving something into "a", which we compute using the function
1990 estimate_move_cost.
1992 The third case deals with TARGET_EXPRs, for which the semantics are
1993 that a temporary is assigned, unless the TARGET_EXPR itself is being
1994 assigned to something else. In the latter case we do not need the
1995 temporary. E.g. in:
1996 <GIMPLE_MODIFY_STMT <var_decl "a"> <target_expr>>, the
1997 GIMPLE_MODIFY_STMT is free. */
1998 case INIT_EXPR:
1999 case GIMPLE_MODIFY_STMT:
2000 /* Is the right and side a TARGET_EXPR? */
2001 if (TREE_CODE (GENERIC_TREE_OPERAND (x, 1)) == TARGET_EXPR)
2002 break;
2003 /* ... fall through ... */
2005 case TARGET_EXPR:
2006 x = GENERIC_TREE_OPERAND (x, 0);
2007 /* Is this an assignments to a register? */
2008 if (is_gimple_reg (x))
2009 break;
2010 /* Otherwise it's a store, so fall through to compute the move cost. */
2012 case CONSTRUCTOR:
2013 *count += estimate_move_cost (TREE_TYPE (x));
2014 break;
2016 /* Assign cost of 1 to usual operations.
2017 ??? We may consider mapping RTL costs to this. */
2018 case COND_EXPR:
2019 case VEC_COND_EXPR:
2021 case PLUS_EXPR:
2022 case MINUS_EXPR:
2023 case MULT_EXPR:
2025 case FIX_TRUNC_EXPR:
2027 case NEGATE_EXPR:
2028 case FLOAT_EXPR:
2029 case MIN_EXPR:
2030 case MAX_EXPR:
2031 case ABS_EXPR:
2033 case LSHIFT_EXPR:
2034 case RSHIFT_EXPR:
2035 case LROTATE_EXPR:
2036 case RROTATE_EXPR:
2037 case VEC_LSHIFT_EXPR:
2038 case VEC_RSHIFT_EXPR:
2040 case BIT_IOR_EXPR:
2041 case BIT_XOR_EXPR:
2042 case BIT_AND_EXPR:
2043 case BIT_NOT_EXPR:
2045 case TRUTH_ANDIF_EXPR:
2046 case TRUTH_ORIF_EXPR:
2047 case TRUTH_AND_EXPR:
2048 case TRUTH_OR_EXPR:
2049 case TRUTH_XOR_EXPR:
2050 case TRUTH_NOT_EXPR:
2052 case LT_EXPR:
2053 case LE_EXPR:
2054 case GT_EXPR:
2055 case GE_EXPR:
2056 case EQ_EXPR:
2057 case NE_EXPR:
2058 case ORDERED_EXPR:
2059 case UNORDERED_EXPR:
2061 case UNLT_EXPR:
2062 case UNLE_EXPR:
2063 case UNGT_EXPR:
2064 case UNGE_EXPR:
2065 case UNEQ_EXPR:
2066 case LTGT_EXPR:
2068 case CONVERT_EXPR:
2070 case CONJ_EXPR:
2072 case PREDECREMENT_EXPR:
2073 case PREINCREMENT_EXPR:
2074 case POSTDECREMENT_EXPR:
2075 case POSTINCREMENT_EXPR:
2077 case SWITCH_EXPR:
2079 case ASM_EXPR:
2081 case REALIGN_LOAD_EXPR:
2083 case REDUC_MAX_EXPR:
2084 case REDUC_MIN_EXPR:
2085 case REDUC_PLUS_EXPR:
2086 case WIDEN_SUM_EXPR:
2087 case DOT_PROD_EXPR:
2088 case VEC_WIDEN_MULT_HI_EXPR:
2089 case VEC_WIDEN_MULT_LO_EXPR:
2090 case VEC_UNPACK_HI_EXPR:
2091 case VEC_UNPACK_LO_EXPR:
2092 case VEC_PACK_MOD_EXPR:
2093 case VEC_PACK_SAT_EXPR:
2095 case WIDEN_MULT_EXPR:
2097 case VEC_EXTRACT_EVEN_EXPR:
2098 case VEC_EXTRACT_ODD_EXPR:
2099 case VEC_INTERLEAVE_HIGH_EXPR:
2100 case VEC_INTERLEAVE_LOW_EXPR:
2102 case RESX_EXPR:
2103 *count += 1;
2104 break;
2106 /* Few special cases of expensive operations. This is useful
2107 to avoid inlining on functions having too many of these. */
2108 case TRUNC_DIV_EXPR:
2109 case CEIL_DIV_EXPR:
2110 case FLOOR_DIV_EXPR:
2111 case ROUND_DIV_EXPR:
2112 case EXACT_DIV_EXPR:
2113 case TRUNC_MOD_EXPR:
2114 case CEIL_MOD_EXPR:
2115 case FLOOR_MOD_EXPR:
2116 case ROUND_MOD_EXPR:
2117 case RDIV_EXPR:
2118 *count += 10;
2119 break;
2120 case CALL_EXPR:
2122 tree decl = get_callee_fndecl (x);
2123 tree arg;
2125 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2126 switch (DECL_FUNCTION_CODE (decl))
2128 case BUILT_IN_CONSTANT_P:
2129 *walk_subtrees = 0;
2130 return NULL_TREE;
2131 case BUILT_IN_EXPECT:
2132 return NULL_TREE;
2133 default:
2134 break;
2137 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
2138 that does use function declaration to figure out the arguments. */
2139 if (!decl)
2141 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
2142 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
2144 else
2146 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2147 *count += estimate_move_cost (TREE_TYPE (arg));
2150 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
2151 break;
2154 case OMP_PARALLEL:
2155 case OMP_FOR:
2156 case OMP_SECTIONS:
2157 case OMP_SINGLE:
2158 case OMP_SECTION:
2159 case OMP_MASTER:
2160 case OMP_ORDERED:
2161 case OMP_CRITICAL:
2162 case OMP_ATOMIC:
2163 /* OpenMP directives are generally very expensive. */
2164 *count += 40;
2165 break;
2167 default:
2168 gcc_unreachable ();
2170 return NULL;
2173 /* Estimate number of instructions that will be created by expanding EXPR. */
2176 estimate_num_insns (tree expr)
2178 int num = 0;
2179 struct pointer_set_t *visited_nodes;
2180 basic_block bb;
2181 block_stmt_iterator bsi;
2182 struct function *my_function;
2184 /* If we're given an entire function, walk the CFG. */
2185 if (TREE_CODE (expr) == FUNCTION_DECL)
2187 my_function = DECL_STRUCT_FUNCTION (expr);
2188 gcc_assert (my_function && my_function->cfg);
2189 visited_nodes = pointer_set_create ();
2190 FOR_EACH_BB_FN (bb, my_function)
2192 for (bsi = bsi_start (bb);
2193 !bsi_end_p (bsi);
2194 bsi_next (&bsi))
2196 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
2197 &num, visited_nodes);
2200 pointer_set_destroy (visited_nodes);
2202 else
2203 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
2205 return num;
2208 typedef struct function *function_p;
2210 DEF_VEC_P(function_p);
2211 DEF_VEC_ALLOC_P(function_p,heap);
2213 /* Initialized with NOGC, making this poisonous to the garbage collector. */
2214 static VEC(function_p,heap) *cfun_stack;
2216 void
2217 push_cfun (struct function *new_cfun)
2219 VEC_safe_push (function_p, heap, cfun_stack, cfun);
2220 cfun = new_cfun;
2223 void
2224 pop_cfun (void)
2226 cfun = VEC_pop (function_p, cfun_stack);
2229 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
2230 static void
2231 add_lexical_block (tree current_block, tree new_block)
2233 tree *blk_p;
2235 /* Walk to the last sub-block. */
2236 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
2237 *blk_p;
2238 blk_p = &TREE_CHAIN (*blk_p))
2240 *blk_p = new_block;
2241 BLOCK_SUPERCONTEXT (new_block) = current_block;
2244 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
2246 static bool
2247 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
2249 copy_body_data *id;
2250 tree t;
2251 tree use_retvar;
2252 tree fn;
2253 splay_tree st;
2254 tree args;
2255 tree return_slot;
2256 tree modify_dest;
2257 location_t saved_location;
2258 struct cgraph_edge *cg_edge;
2259 const char *reason;
2260 basic_block return_block;
2261 edge e;
2262 block_stmt_iterator bsi, stmt_bsi;
2263 bool successfully_inlined = FALSE;
2264 bool purge_dead_abnormal_edges;
2265 tree t_step;
2266 tree var;
2268 /* See what we've got. */
2269 id = (copy_body_data *) data;
2270 t = *tp;
2272 /* Set input_location here so we get the right instantiation context
2273 if we call instantiate_decl from inlinable_function_p. */
2274 saved_location = input_location;
2275 if (EXPR_HAS_LOCATION (t))
2276 input_location = EXPR_LOCATION (t);
2278 /* From here on, we're only interested in CALL_EXPRs. */
2279 if (TREE_CODE (t) != CALL_EXPR)
2280 goto egress;
2282 /* First, see if we can figure out what function is being called.
2283 If we cannot, then there is no hope of inlining the function. */
2284 fn = get_callee_fndecl (t);
2285 if (!fn)
2286 goto egress;
2288 /* Turn forward declarations into real ones. */
2289 fn = cgraph_node (fn)->decl;
2291 /* If fn is a declaration of a function in a nested scope that was
2292 globally declared inline, we don't set its DECL_INITIAL.
2293 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
2294 C++ front-end uses it for cdtors to refer to their internal
2295 declarations, that are not real functions. Fortunately those
2296 don't have trees to be saved, so we can tell by checking their
2297 DECL_SAVED_TREE. */
2298 if (! DECL_INITIAL (fn)
2299 && DECL_ABSTRACT_ORIGIN (fn)
2300 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
2301 fn = DECL_ABSTRACT_ORIGIN (fn);
2303 /* Objective C and fortran still calls tree_rest_of_compilation directly.
2304 Kill this check once this is fixed. */
2305 if (!id->dst_node->analyzed)
2306 goto egress;
2308 cg_edge = cgraph_edge (id->dst_node, stmt);
2310 /* Constant propagation on argument done during previous inlining
2311 may create new direct call. Produce an edge for it. */
2312 if (!cg_edge)
2314 struct cgraph_node *dest = cgraph_node (fn);
2316 /* We have missing edge in the callgraph. This can happen in one case
2317 where previous inlining turned indirect call into direct call by
2318 constant propagating arguments. In all other cases we hit a bug
2319 (incorrect node sharing is most common reason for missing edges. */
2320 gcc_assert (dest->needed || !flag_unit_at_a_time);
2321 cgraph_create_edge (id->dst_node, dest, stmt,
2322 bb->count, bb->loop_depth)->inline_failed
2323 = N_("originally indirect function call not considered for inlining");
2324 goto egress;
2327 /* Don't try to inline functions that are not well-suited to
2328 inlining. */
2329 if (!cgraph_inline_p (cg_edge, &reason))
2331 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
2332 /* Avoid warnings during early inline pass. */
2333 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2335 sorry ("inlining failed in call to %q+F: %s", fn, reason);
2336 sorry ("called from here");
2338 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
2339 && !DECL_IN_SYSTEM_HEADER (fn)
2340 && strlen (reason)
2341 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
2342 /* Avoid warnings during early inline pass. */
2343 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2345 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
2346 fn, reason);
2347 warning (OPT_Winline, "called from here");
2349 goto egress;
2351 fn = cg_edge->callee->decl;
2353 #ifdef ENABLE_CHECKING
2354 if (cg_edge->callee->decl != id->dst_node->decl)
2355 verify_cgraph_node (cg_edge->callee);
2356 #endif
2358 /* We will be inlining this callee. */
2359 id->eh_region = lookup_stmt_eh_region (stmt);
2361 /* Split the block holding the CALL_EXPR. */
2362 e = split_block (bb, stmt);
2363 bb = e->src;
2364 return_block = e->dest;
2365 remove_edge (e);
2367 /* split_block splits after the statement; work around this by
2368 moving the call into the second block manually. Not pretty,
2369 but seems easier than doing the CFG manipulation by hand
2370 when the CALL_EXPR is in the last statement of BB. */
2371 stmt_bsi = bsi_last (bb);
2372 bsi_remove (&stmt_bsi, false);
2374 /* If the CALL_EXPR was in the last statement of BB, it may have
2375 been the source of abnormal edges. In this case, schedule
2376 the removal of dead abnormal edges. */
2377 bsi = bsi_start (return_block);
2378 if (bsi_end_p (bsi))
2380 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2381 purge_dead_abnormal_edges = true;
2383 else
2385 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2386 purge_dead_abnormal_edges = false;
2389 stmt_bsi = bsi_start (return_block);
2391 /* Build a block containing code to initialize the arguments, the
2392 actual inline expansion of the body, and a label for the return
2393 statements within the function to jump to. The type of the
2394 statement expression is the return type of the function call. */
2395 id->block = make_node (BLOCK);
2396 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2397 BLOCK_SOURCE_LOCATION (id->block) = input_location;
2398 add_lexical_block (TREE_BLOCK (stmt), id->block);
2400 /* Local declarations will be replaced by their equivalents in this
2401 map. */
2402 st = id->decl_map;
2403 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2404 NULL, NULL);
2406 /* Initialize the parameters. */
2407 args = TREE_OPERAND (t, 1);
2409 /* Record the function we are about to inline. */
2410 id->src_fn = fn;
2411 id->src_node = cg_edge->callee;
2412 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
2414 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2416 if (DECL_INITIAL (fn))
2417 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
2419 /* Return statements in the function body will be replaced by jumps
2420 to the RET_LABEL. */
2422 gcc_assert (DECL_INITIAL (fn));
2423 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2425 /* Find the lhs to which the result of this call is assigned. */
2426 return_slot = NULL;
2427 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2429 modify_dest = GIMPLE_STMT_OPERAND (stmt, 0);
2431 /* The function which we are inlining might not return a value,
2432 in which case we should issue a warning that the function
2433 does not return a value. In that case the optimizers will
2434 see that the variable to which the value is assigned was not
2435 initialized. We do not want to issue a warning about that
2436 uninitialized variable. */
2437 if (DECL_P (modify_dest))
2438 TREE_NO_WARNING (modify_dest) = 1;
2439 if (CALL_EXPR_RETURN_SLOT_OPT (t))
2441 return_slot = modify_dest;
2442 modify_dest = NULL;
2445 else
2446 modify_dest = NULL;
2448 /* Declare the return variable for the function. */
2449 declare_return_variable (id, return_slot,
2450 modify_dest, &use_retvar);
2452 /* This is it. Duplicate the callee body. Assume callee is
2453 pre-gimplified. Note that we must not alter the caller
2454 function in any way before this point, as this CALL_EXPR may be
2455 a self-referential call; if we're calling ourselves, we need to
2456 duplicate our body before altering anything. */
2457 copy_body (id, bb->count, bb->frequency, bb, return_block);
2459 /* Add local vars in this inlined callee to caller. */
2460 t_step = id->src_cfun->unexpanded_var_list;
2461 for (; t_step; t_step = TREE_CHAIN (t_step))
2463 var = TREE_VALUE (t_step);
2464 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2465 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2466 cfun->unexpanded_var_list);
2467 else
2468 cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2469 cfun->unexpanded_var_list);
2472 /* Clean up. */
2473 splay_tree_delete (id->decl_map);
2474 id->decl_map = st;
2476 /* If the inlined function returns a result that we care about,
2477 clobber the CALL_EXPR with a reference to the return variable. */
2478 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2480 *tp = use_retvar;
2481 if (gimple_in_ssa_p (cfun))
2483 update_stmt (stmt);
2484 mark_symbols_for_renaming (stmt);
2486 maybe_clean_or_replace_eh_stmt (stmt, stmt);
2488 else
2489 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2490 tsi_delink() will leave the iterator in a sane state. */
2492 /* Handle case of inlining function that miss return statement so
2493 return value becomes undefined. */
2494 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
2495 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME)
2497 tree name = TREE_OPERAND (stmt, 0);
2498 tree var = SSA_NAME_VAR (TREE_OPERAND (stmt, 0));
2499 tree def = gimple_default_def (cfun, var);
2501 /* If the variable is used undefined, make this name undefined via
2502 move. */
2503 if (def)
2505 TREE_OPERAND (stmt, 1) = def;
2506 update_stmt (stmt);
2508 /* Otherwise make this variable undefined. */
2509 else
2511 bsi_remove (&stmt_bsi, true);
2512 set_default_def (var, name);
2513 SSA_NAME_DEF_STMT (name) = build_empty_stmt ();
2516 else
2517 bsi_remove (&stmt_bsi, true);
2520 if (purge_dead_abnormal_edges)
2521 tree_purge_dead_abnormal_call_edges (return_block);
2523 /* If the value of the new expression is ignored, that's OK. We
2524 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2525 the equivalent inlined version either. */
2526 TREE_USED (*tp) = 1;
2528 /* Output the inlining info for this abstract function, since it has been
2529 inlined. If we don't do this now, we can lose the information about the
2530 variables in the function when the blocks get blown away as soon as we
2531 remove the cgraph node. */
2532 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2534 /* Update callgraph if needed. */
2535 cgraph_remove_node (cg_edge->callee);
2537 id->block = NULL_TREE;
2538 successfully_inlined = TRUE;
2540 egress:
2541 input_location = saved_location;
2542 return successfully_inlined;
2545 /* Expand call statements reachable from STMT_P.
2546 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2547 in a GIMPLE_MODIFY_STMT. See tree-gimple.c:get_call_expr_in(). We can
2548 unfortunately not use that function here because we need a pointer
2549 to the CALL_EXPR, not the tree itself. */
2551 static bool
2552 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
2554 block_stmt_iterator bsi;
2556 /* Register specific tree functions. */
2557 tree_register_cfg_hooks ();
2558 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2560 tree *expr_p = bsi_stmt_ptr (bsi);
2561 tree stmt = *expr_p;
2563 if (TREE_CODE (*expr_p) == GIMPLE_MODIFY_STMT)
2564 expr_p = &GIMPLE_STMT_OPERAND (*expr_p, 1);
2565 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2566 expr_p = &TREE_OPERAND (*expr_p, 0);
2567 if (TREE_CODE (*expr_p) == CALL_EXPR)
2568 if (expand_call_inline (bb, stmt, expr_p, id))
2569 return true;
2571 return false;
2574 /* Expand calls to inline functions in the body of FN. */
2576 void
2577 optimize_inline_calls (tree fn)
2579 copy_body_data id;
2580 tree prev_fn;
2581 basic_block bb;
2582 /* There is no point in performing inlining if errors have already
2583 occurred -- and we might crash if we try to inline invalid
2584 code. */
2585 if (errorcount || sorrycount)
2586 return;
2588 /* Clear out ID. */
2589 memset (&id, 0, sizeof (id));
2591 id.src_node = id.dst_node = cgraph_node (fn);
2592 id.dst_fn = fn;
2593 /* Or any functions that aren't finished yet. */
2594 prev_fn = NULL_TREE;
2595 if (current_function_decl)
2597 id.dst_fn = current_function_decl;
2598 prev_fn = current_function_decl;
2601 id.copy_decl = copy_decl_maybe_to_var;
2602 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2603 id.transform_new_cfg = false;
2604 id.transform_return_to_modify = true;
2605 id.transform_lang_insert_block = false;
2607 push_gimplify_context ();
2609 /* Reach the trees by walking over the CFG, and note the
2610 enclosing basic-blocks in the call edges. */
2611 /* We walk the blocks going forward, because inlined function bodies
2612 will split id->current_basic_block, and the new blocks will
2613 follow it; we'll trudge through them, processing their CALL_EXPRs
2614 along the way. */
2615 FOR_EACH_BB (bb)
2616 gimple_expand_calls_inline (bb, &id);
2618 pop_gimplify_context (NULL);
2619 /* Renumber the (code) basic_blocks consecutively. */
2620 compact_blocks ();
2621 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2622 number_blocks (fn);
2624 #ifdef ENABLE_CHECKING
2626 struct cgraph_edge *e;
2628 verify_cgraph_node (id.dst_node);
2630 /* Double check that we inlined everything we are supposed to inline. */
2631 for (e = id.dst_node->callees; e; e = e->next_callee)
2632 gcc_assert (e->inline_failed);
2634 #endif
2635 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2636 as inlining loops might increase the maximum. */
2637 if (ENTRY_BLOCK_PTR->count)
2638 counts_to_freqs ();
2639 if (gimple_in_ssa_p (cfun))
2641 /* We make no attempts to keep dominance info up-to-date. */
2642 free_dominance_info (CDI_DOMINATORS);
2643 free_dominance_info (CDI_POST_DOMINATORS);
2644 delete_unreachable_blocks ();
2645 update_ssa (TODO_update_ssa);
2646 fold_cond_expr_cond ();
2647 if (need_ssa_update_p ())
2648 update_ssa (TODO_update_ssa);
2650 else
2651 fold_cond_expr_cond ();
2652 /* It would be nice to check SSA/CFG/statement consistency here, but it is
2653 not possible yet - the IPA passes might make various functions to not
2654 throw and they don't care to proactively update local EH info. This is
2655 done later in fixup_cfg pass that also execute the verification. */
2658 /* FN is a function that has a complete body, and CLONE is a function whose
2659 body is to be set to a copy of FN, mapping argument declarations according
2660 to the ARG_MAP splay_tree. */
2662 void
2663 clone_body (tree clone, tree fn, void *arg_map)
2665 copy_body_data id;
2667 /* Clone the body, as if we were making an inline call. But, remap the
2668 parameters in the callee to the parameters of caller. */
2669 memset (&id, 0, sizeof (id));
2670 id.src_fn = fn;
2671 id.dst_fn = clone;
2672 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
2673 id.decl_map = (splay_tree)arg_map;
2675 id.copy_decl = copy_decl_no_change;
2676 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2677 id.transform_new_cfg = true;
2678 id.transform_return_to_modify = false;
2679 id.transform_lang_insert_block = true;
2681 /* We're not inside any EH region. */
2682 id.eh_region = -1;
2684 /* Actually copy the body. */
2685 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2688 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2690 tree
2691 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2693 enum tree_code code = TREE_CODE (*tp);
2694 enum tree_code_class cl = TREE_CODE_CLASS (code);
2696 /* We make copies of most nodes. */
2697 if (IS_EXPR_CODE_CLASS (cl)
2698 || IS_GIMPLE_STMT_CODE_CLASS (cl)
2699 || code == TREE_LIST
2700 || code == TREE_VEC
2701 || code == TYPE_DECL
2702 || code == OMP_CLAUSE)
2704 /* Because the chain gets clobbered when we make a copy, we save it
2705 here. */
2706 tree chain = NULL_TREE, new;
2708 if (!GIMPLE_TUPLE_P (*tp))
2709 chain = TREE_CHAIN (*tp);
2711 /* Copy the node. */
2712 new = copy_node (*tp);
2714 /* Propagate mudflap marked-ness. */
2715 if (flag_mudflap && mf_marked_p (*tp))
2716 mf_mark (new);
2718 *tp = new;
2720 /* Now, restore the chain, if appropriate. That will cause
2721 walk_tree to walk into the chain as well. */
2722 if (code == PARM_DECL
2723 || code == TREE_LIST
2724 || code == OMP_CLAUSE)
2725 TREE_CHAIN (*tp) = chain;
2727 /* For now, we don't update BLOCKs when we make copies. So, we
2728 have to nullify all BIND_EXPRs. */
2729 if (TREE_CODE (*tp) == BIND_EXPR)
2730 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2732 else if (code == CONSTRUCTOR)
2734 /* CONSTRUCTOR nodes need special handling because
2735 we need to duplicate the vector of elements. */
2736 tree new;
2738 new = copy_node (*tp);
2740 /* Propagate mudflap marked-ness. */
2741 if (flag_mudflap && mf_marked_p (*tp))
2742 mf_mark (new);
2744 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2745 CONSTRUCTOR_ELTS (*tp));
2746 *tp = new;
2748 else if (TREE_CODE_CLASS (code) == tcc_type)
2749 *walk_subtrees = 0;
2750 else if (TREE_CODE_CLASS (code) == tcc_declaration)
2751 *walk_subtrees = 0;
2752 else if (TREE_CODE_CLASS (code) == tcc_constant)
2753 *walk_subtrees = 0;
2754 else
2755 gcc_assert (code != STATEMENT_LIST);
2756 return NULL_TREE;
2759 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2760 information indicating to what new SAVE_EXPR this one should be mapped,
2761 use that one. Otherwise, create a new node and enter it in ST. FN is
2762 the function into which the copy will be placed. */
2764 static void
2765 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2767 splay_tree st = (splay_tree) st_;
2768 splay_tree_node n;
2769 tree t;
2771 /* See if we already encountered this SAVE_EXPR. */
2772 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2774 /* If we didn't already remap this SAVE_EXPR, do so now. */
2775 if (!n)
2777 t = copy_node (*tp);
2779 /* Remember this SAVE_EXPR. */
2780 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2781 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2782 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2784 else
2786 /* We've already walked into this SAVE_EXPR; don't do it again. */
2787 *walk_subtrees = 0;
2788 t = (tree) n->value;
2791 /* Replace this SAVE_EXPR with the copy. */
2792 *tp = t;
2795 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2796 copies the declaration and enters it in the splay_tree in DATA (which is
2797 really an `copy_body_data *'). */
2799 static tree
2800 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2801 void *data)
2803 copy_body_data *id = (copy_body_data *) data;
2805 /* Don't walk into types. */
2806 if (TYPE_P (*tp))
2807 *walk_subtrees = 0;
2809 else if (TREE_CODE (*tp) == LABEL_EXPR)
2811 tree decl = TREE_OPERAND (*tp, 0);
2813 /* Copy the decl and remember the copy. */
2814 insert_decl_map (id, decl, id->copy_decl (decl, id));
2817 return NULL_TREE;
2820 /* Perform any modifications to EXPR required when it is unsaved. Does
2821 not recurse into EXPR's subtrees. */
2823 static void
2824 unsave_expr_1 (tree expr)
2826 switch (TREE_CODE (expr))
2828 case TARGET_EXPR:
2829 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2830 It's OK for this to happen if it was part of a subtree that
2831 isn't immediately expanded, such as operand 2 of another
2832 TARGET_EXPR. */
2833 if (TREE_OPERAND (expr, 1))
2834 break;
2836 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2837 TREE_OPERAND (expr, 3) = NULL_TREE;
2838 break;
2840 default:
2841 break;
2845 /* Called via walk_tree when an expression is unsaved. Using the
2846 splay_tree pointed to by ST (which is really a `splay_tree'),
2847 remaps all local declarations to appropriate replacements. */
2849 static tree
2850 unsave_r (tree *tp, int *walk_subtrees, void *data)
2852 copy_body_data *id = (copy_body_data *) data;
2853 splay_tree st = id->decl_map;
2854 splay_tree_node n;
2856 /* Only a local declaration (variable or label). */
2857 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2858 || TREE_CODE (*tp) == LABEL_DECL)
2860 /* Lookup the declaration. */
2861 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2863 /* If it's there, remap it. */
2864 if (n)
2865 *tp = (tree) n->value;
2868 else if (TREE_CODE (*tp) == STATEMENT_LIST)
2869 copy_statement_list (tp);
2870 else if (TREE_CODE (*tp) == BIND_EXPR)
2871 copy_bind_expr (tp, walk_subtrees, id);
2872 else if (TREE_CODE (*tp) == SAVE_EXPR)
2873 remap_save_expr (tp, st, walk_subtrees);
2874 else
2876 copy_tree_r (tp, walk_subtrees, NULL);
2878 /* Do whatever unsaving is required. */
2879 unsave_expr_1 (*tp);
2882 /* Keep iterating. */
2883 return NULL_TREE;
2886 /* Copies everything in EXPR and replaces variables, labels
2887 and SAVE_EXPRs local to EXPR. */
2889 tree
2890 unsave_expr_now (tree expr)
2892 copy_body_data id;
2894 /* There's nothing to do for NULL_TREE. */
2895 if (expr == 0)
2896 return expr;
2898 /* Set up ID. */
2899 memset (&id, 0, sizeof (id));
2900 id.src_fn = current_function_decl;
2901 id.dst_fn = current_function_decl;
2902 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2904 id.copy_decl = copy_decl_no_change;
2905 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2906 id.transform_new_cfg = false;
2907 id.transform_return_to_modify = false;
2908 id.transform_lang_insert_block = false;
2910 /* Walk the tree once to find local labels. */
2911 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2913 /* Walk the tree again, copying, remapping, and unsaving. */
2914 walk_tree (&expr, unsave_r, &id, NULL);
2916 /* Clean up. */
2917 splay_tree_delete (id.decl_map);
2919 return expr;
2922 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
2924 static tree
2925 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2927 if (*tp == data)
2928 return (tree) data;
2929 else
2930 return NULL;
2933 bool
2934 debug_find_tree (tree top, tree search)
2936 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2940 /* Declare the variables created by the inliner. Add all the variables in
2941 VARS to BIND_EXPR. */
2943 static void
2944 declare_inline_vars (tree block, tree vars)
2946 tree t;
2947 for (t = vars; t; t = TREE_CHAIN (t))
2949 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2950 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
2951 cfun->unexpanded_var_list =
2952 tree_cons (NULL_TREE, t,
2953 cfun->unexpanded_var_list);
2956 if (block)
2957 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2961 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
2962 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
2963 VAR_DECL translation. */
2965 static tree
2966 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
2968 /* Don't generate debug information for the copy if we wouldn't have
2969 generated it for the copy either. */
2970 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2971 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2973 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
2974 declaration inspired this copy. */
2975 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2977 /* The new variable/label has no RTL, yet. */
2978 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
2979 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
2980 SET_DECL_RTL (copy, NULL_RTX);
2982 /* These args would always appear unused, if not for this. */
2983 TREE_USED (copy) = 1;
2985 /* Set the context for the new declaration. */
2986 if (!DECL_CONTEXT (decl))
2987 /* Globals stay global. */
2989 else if (DECL_CONTEXT (decl) != id->src_fn)
2990 /* Things that weren't in the scope of the function we're inlining
2991 from aren't in the scope we're inlining to, either. */
2993 else if (TREE_STATIC (decl))
2994 /* Function-scoped static variables should stay in the original
2995 function. */
2997 else
2998 /* Ordinary automatic local variables are now in the scope of the
2999 new function. */
3000 DECL_CONTEXT (copy) = id->dst_fn;
3002 return copy;
3005 static tree
3006 copy_decl_to_var (tree decl, copy_body_data *id)
3008 tree copy, type;
3010 gcc_assert (TREE_CODE (decl) == PARM_DECL
3011 || TREE_CODE (decl) == RESULT_DECL);
3013 type = TREE_TYPE (decl);
3015 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
3016 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
3017 TREE_READONLY (copy) = TREE_READONLY (decl);
3018 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
3019 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
3021 return copy_decl_for_dup_finish (id, decl, copy);
3024 /* Like copy_decl_to_var, but create a return slot object instead of a
3025 pointer variable for return by invisible reference. */
3027 static tree
3028 copy_result_decl_to_var (tree decl, copy_body_data *id)
3030 tree copy, type;
3032 gcc_assert (TREE_CODE (decl) == PARM_DECL
3033 || TREE_CODE (decl) == RESULT_DECL);
3035 type = TREE_TYPE (decl);
3036 if (DECL_BY_REFERENCE (decl))
3037 type = TREE_TYPE (type);
3039 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
3040 TREE_READONLY (copy) = TREE_READONLY (decl);
3041 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
3042 if (!DECL_BY_REFERENCE (decl))
3044 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
3045 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
3048 return copy_decl_for_dup_finish (id, decl, copy);
3052 static tree
3053 copy_decl_no_change (tree decl, copy_body_data *id)
3055 tree copy;
3057 copy = copy_node (decl);
3059 /* The COPY is not abstract; it will be generated in DST_FN. */
3060 DECL_ABSTRACT (copy) = 0;
3061 lang_hooks.dup_lang_specific_decl (copy);
3063 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
3064 been taken; it's for internal bookkeeping in expand_goto_internal. */
3065 if (TREE_CODE (copy) == LABEL_DECL)
3067 TREE_ADDRESSABLE (copy) = 0;
3068 LABEL_DECL_UID (copy) = -1;
3071 return copy_decl_for_dup_finish (id, decl, copy);
3074 static tree
3075 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
3077 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
3078 return copy_decl_to_var (decl, id);
3079 else
3080 return copy_decl_no_change (decl, id);
3083 /* Return a copy of the function's argument tree. */
3084 static tree
3085 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
3087 tree *arg_copy, *parg;
3089 arg_copy = &orig_parm;
3090 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
3092 tree new = remap_decl (*parg, id);
3093 lang_hooks.dup_lang_specific_decl (new);
3094 TREE_CHAIN (new) = TREE_CHAIN (*parg);
3095 *parg = new;
3097 return orig_parm;
3100 /* Return a copy of the function's static chain. */
3101 static tree
3102 copy_static_chain (tree static_chain, copy_body_data * id)
3104 tree *chain_copy, *pvar;
3106 chain_copy = &static_chain;
3107 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
3109 tree new = remap_decl (*pvar, id);
3110 lang_hooks.dup_lang_specific_decl (new);
3111 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
3112 *pvar = new;
3114 return static_chain;
3117 /* Return true if the function is allowed to be versioned.
3118 This is a guard for the versioning functionality. */
3119 bool
3120 tree_versionable_function_p (tree fndecl)
3122 if (fndecl == NULL_TREE)
3123 return false;
3124 /* ??? There are cases where a function is
3125 uninlinable but can be versioned. */
3126 if (!tree_inlinable_function_p (fndecl))
3127 return false;
3129 return true;
3132 /* Create a copy of a function's tree.
3133 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
3134 of the original function and the new copied function
3135 respectively. In case we want to replace a DECL
3136 tree with another tree while duplicating the function's
3137 body, TREE_MAP represents the mapping between these
3138 trees. If UPDATE_CLONES is set, the call_stmt fields
3139 of edges of clones of the function will be updated. */
3140 void
3141 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
3142 bool update_clones)
3144 struct cgraph_node *old_version_node;
3145 struct cgraph_node *new_version_node;
3146 copy_body_data id;
3147 tree p;
3148 unsigned i;
3149 struct ipa_replace_map *replace_info;
3150 basic_block old_entry_block;
3151 tree t_step;
3153 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
3154 && TREE_CODE (new_decl) == FUNCTION_DECL);
3155 DECL_POSSIBLY_INLINED (old_decl) = 1;
3157 old_version_node = cgraph_node (old_decl);
3158 new_version_node = cgraph_node (new_decl);
3160 allocate_struct_function (new_decl);
3161 /* Cfun points to the new allocated function struct at this point. */
3162 cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
3164 DECL_ARTIFICIAL (new_decl) = 1;
3165 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
3167 /* Generate a new name for the new version. */
3168 if (!update_clones)
3170 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
3171 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
3172 SET_DECL_RTL (new_decl, NULL_RTX);
3175 /* Prepare the data structures for the tree copy. */
3176 memset (&id, 0, sizeof (id));
3178 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
3179 id.src_fn = old_decl;
3180 id.dst_fn = new_decl;
3181 id.src_node = old_version_node;
3182 id.dst_node = new_version_node;
3183 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
3185 id.copy_decl = copy_decl_no_change;
3186 id.transform_call_graph_edges
3187 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
3188 id.transform_new_cfg = true;
3189 id.transform_return_to_modify = false;
3190 id.transform_lang_insert_block = false;
3192 current_function_decl = new_decl;
3193 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
3194 (DECL_STRUCT_FUNCTION (old_decl));
3195 initialize_cfun (new_decl, old_decl,
3196 old_entry_block->count,
3197 old_entry_block->frequency);
3198 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
3200 /* Copy the function's static chain. */
3201 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
3202 if (p)
3203 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
3204 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
3205 &id);
3206 /* Copy the function's arguments. */
3207 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
3208 DECL_ARGUMENTS (new_decl) =
3209 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
3211 /* If there's a tree_map, prepare for substitution. */
3212 if (tree_map)
3213 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
3215 replace_info = VARRAY_GENERIC_PTR (tree_map, i);
3216 if (replace_info->replace_p)
3217 insert_decl_map (&id, replace_info->old_tree,
3218 replace_info->new_tree);
3221 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
3223 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3224 number_blocks (id.dst_fn);
3226 if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
3227 /* Add local vars. */
3228 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
3229 t_step; t_step = TREE_CHAIN (t_step))
3231 tree var = TREE_VALUE (t_step);
3232 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3233 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
3234 cfun->unexpanded_var_list);
3235 else
3236 cfun->unexpanded_var_list =
3237 tree_cons (NULL_TREE, remap_decl (var, &id),
3238 cfun->unexpanded_var_list);
3241 /* Copy the Function's body. */
3242 copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
3244 if (DECL_RESULT (old_decl) != NULL_TREE)
3246 tree *res_decl = &DECL_RESULT (old_decl);
3247 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
3248 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
3251 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3252 number_blocks (new_decl);
3254 /* Clean up. */
3255 splay_tree_delete (id.decl_map);
3256 fold_cond_expr_cond ();
3257 if (gimple_in_ssa_p (cfun))
3259 update_ssa (TODO_update_ssa);
3261 free_dominance_info (CDI_DOMINATORS);
3262 free_dominance_info (CDI_POST_DOMINATORS);
3263 pop_cfun ();
3264 current_function_decl = NULL;
3265 return;
3268 /* Duplicate a type, fields and all. */
3270 tree
3271 build_duplicate_type (tree type)
3273 struct copy_body_data id;
3275 memset (&id, 0, sizeof (id));
3276 id.src_fn = current_function_decl;
3277 id.dst_fn = current_function_decl;
3278 id.src_cfun = cfun;
3279 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
3281 type = remap_type_1 (type, &id);
3283 splay_tree_delete (id.decl_map);
3285 return type;