* tree.c (find_tree_t, find_tree): Remove.
[official-gcc.git] / gcc / tree-inline.c
blob7030b92b89d331e15bbfaa7ed9af474d70034b64
1 /* Tree inlining.
2 Copyright 2001, 2002, 2003, 2004, 2005 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"
53 /* I'm not real happy about this, but we need to handle gimple and
54 non-gimple trees. */
55 #include "tree-gimple.h"
57 /* Inlining, Cloning, Versioning, Parallelization
59 Inlining: a function body is duplicated, but the PARM_DECLs are
60 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
61 MODIFY_EXPRs that store to a dedicated returned-value variable.
62 The duplicated eh_region info of the copy will later be appended
63 to the info for the caller; the eh_region info in copied throwing
64 statements and RESX_EXPRs is adjusted accordingly.
66 Cloning: (only in C++) We have one body for a con/de/structor, and
67 multiple function decls, each with a unique parameter list.
68 Duplicate the body, using the given splay tree; some parameters
69 will become constants (like 0 or 1).
71 Versioning: a function body is duplicated and the result is a new
72 function rather than into blocks of an existing function as with
73 inlining. Some parameters will become constants.
75 Parallelization: a region of a function is duplicated resulting in
76 a new function. Variables may be replaced with complex expressions
77 to enable shared variable semantics.
79 All of these will simultaneously lookup any callgraph edges. If
80 we're going to inline the duplicated function body, and the given
81 function has some cloned callgraph nodes (one for each place this
82 function will be inlined) those callgraph edges will be duplicated.
83 If we're cloning the body, those callgraph edges will be
84 updated to point into the new body. (Note that the original
85 callgraph node and edge list will not be altered.)
87 See the CALL_EXPR handling case in copy_body_r (). */
89 /* 0 if we should not perform inlining.
90 1 if we should expand functions calls inline at the tree level.
91 2 if we should consider *all* functions to be inline
92 candidates. */
94 int flag_inline_trees = 0;
96 /* To Do:
98 o In order to make inlining-on-trees work, we pessimized
99 function-local static constants. In particular, they are now
100 always output, even when not addressed. Fix this by treating
101 function-local static constants just like global static
102 constants; the back-end already knows not to output them if they
103 are not needed.
105 o Provide heuristics to clamp inlining of recursive template
106 calls? */
108 /* Prototypes. */
110 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
111 static tree copy_generic_body (copy_body_data *);
112 static bool inlinable_function_p (tree);
113 static void remap_block (tree *, copy_body_data *);
114 static tree remap_decls (tree, copy_body_data *);
115 static void copy_bind_expr (tree *, int *, copy_body_data *);
116 static tree mark_local_for_remap_r (tree *, int *, void *);
117 static void unsave_expr_1 (tree);
118 static tree unsave_r (tree *, int *, void *);
119 static void declare_inline_vars (tree, tree);
120 static void remap_save_expr (tree *, void *, int *);
121 static void add_lexical_block (tree current_block, tree new_block);
122 static tree copy_decl_to_var (tree, copy_body_data *);
123 static tree copy_decl_no_change (tree, copy_body_data *);
124 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
126 /* Insert a tree->tree mapping for ID. Despite the name suggests
127 that the trees should be variables, it is used for more than that. */
129 void
130 insert_decl_map (copy_body_data *id, tree key, tree value)
132 splay_tree_insert (id->decl_map, (splay_tree_key) key,
133 (splay_tree_value) value);
135 /* Always insert an identity map as well. If we see this same new
136 node again, we won't want to duplicate it a second time. */
137 if (key != value)
138 splay_tree_insert (id->decl_map, (splay_tree_key) value,
139 (splay_tree_value) value);
142 /* Remap DECL during the copying of the BLOCK tree for the function. */
144 tree
145 remap_decl (tree decl, copy_body_data *id)
147 splay_tree_node n;
148 tree fn;
150 /* We only remap local variables in the current function. */
151 fn = id->src_fn;
153 /* See if we have remapped this declaration. */
155 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
157 /* If we didn't already have an equivalent for this declaration,
158 create one now. */
159 if (!n)
161 /* Make a copy of the variable or label. */
162 tree t = id->copy_decl (decl, id);
164 /* Remember it, so that if we encounter this local entity again
165 we can reuse this copy. Do this early because remap_type may
166 need this decl for TYPE_STUB_DECL. */
167 insert_decl_map (id, decl, t);
169 if (!DECL_P (t))
170 return t;
172 /* Remap types, if necessary. */
173 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
174 if (TREE_CODE (t) == TYPE_DECL)
175 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
177 /* Remap sizes as necessary. */
178 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
179 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
181 /* If fields, do likewise for offset and qualifier. */
182 if (TREE_CODE (t) == FIELD_DECL)
184 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
185 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
186 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
189 return t;
192 return unshare_expr ((tree) n->value);
195 static tree
196 remap_type_1 (tree type, copy_body_data *id)
198 splay_tree_node node;
199 tree new, t;
201 if (type == NULL)
202 return type;
204 /* See if we have remapped this type. */
205 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
206 if (node)
207 return (tree) node->value;
209 /* The type only needs remapping if it's variably modified. */
210 if (! variably_modified_type_p (type, id->src_fn))
212 insert_decl_map (id, type, type);
213 return type;
216 /* We do need a copy. build and register it now. If this is a pointer or
217 reference type, remap the designated type and make a new pointer or
218 reference type. */
219 if (TREE_CODE (type) == POINTER_TYPE)
221 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
222 TYPE_MODE (type),
223 TYPE_REF_CAN_ALIAS_ALL (type));
224 insert_decl_map (id, type, new);
225 return new;
227 else if (TREE_CODE (type) == REFERENCE_TYPE)
229 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
230 TYPE_MODE (type),
231 TYPE_REF_CAN_ALIAS_ALL (type));
232 insert_decl_map (id, type, new);
233 return new;
235 else
236 new = copy_node (type);
238 insert_decl_map (id, type, new);
240 /* This is a new type, not a copy of an old type. Need to reassociate
241 variants. We can handle everything except the main variant lazily. */
242 t = TYPE_MAIN_VARIANT (type);
243 if (type != t)
245 t = remap_type (t, id);
246 TYPE_MAIN_VARIANT (new) = t;
247 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
248 TYPE_NEXT_VARIANT (t) = new;
250 else
252 TYPE_MAIN_VARIANT (new) = new;
253 TYPE_NEXT_VARIANT (new) = NULL;
256 if (TYPE_STUB_DECL (type))
257 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
259 /* Lazily create pointer and reference types. */
260 TYPE_POINTER_TO (new) = NULL;
261 TYPE_REFERENCE_TO (new) = NULL;
263 switch (TREE_CODE (new))
265 case INTEGER_TYPE:
266 case REAL_TYPE:
267 case ENUMERAL_TYPE:
268 case BOOLEAN_TYPE:
269 case CHAR_TYPE:
270 t = TYPE_MIN_VALUE (new);
271 if (t && TREE_CODE (t) != INTEGER_CST)
272 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
274 t = TYPE_MAX_VALUE (new);
275 if (t && TREE_CODE (t) != INTEGER_CST)
276 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
277 return new;
279 case FUNCTION_TYPE:
280 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
281 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
282 return new;
284 case ARRAY_TYPE:
285 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
286 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
287 break;
289 case RECORD_TYPE:
290 case UNION_TYPE:
291 case QUAL_UNION_TYPE:
293 tree f, nf = NULL;
295 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
297 t = remap_decl (f, id);
298 DECL_CONTEXT (t) = new;
299 TREE_CHAIN (t) = nf;
300 nf = t;
302 TYPE_FIELDS (new) = nreverse (nf);
304 break;
306 case OFFSET_TYPE:
307 default:
308 /* Shouldn't have been thought variable sized. */
309 gcc_unreachable ();
312 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
313 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
315 return new;
318 tree
319 remap_type (tree type, copy_body_data *id)
321 splay_tree_node node;
323 if (type == NULL)
324 return type;
326 /* See if we have remapped this type. */
327 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
328 if (node)
329 return (tree) node->value;
331 /* The type only needs remapping if it's variably modified. */
332 if (! variably_modified_type_p (type, id->src_fn))
334 insert_decl_map (id, type, type);
335 return type;
338 return remap_type_1 (type, id);
341 static tree
342 remap_decls (tree decls, copy_body_data *id)
344 tree old_var;
345 tree new_decls = NULL_TREE;
347 /* Remap its variables. */
348 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
350 tree new_var;
352 /* We can not chain the local static declarations into the unexpanded_var_list
353 as we can't duplicate them or break one decl rule. Go ahead and link
354 them into unexpanded_var_list. */
355 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
356 && !DECL_EXTERNAL (old_var))
358 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
359 cfun->unexpanded_var_list);
360 continue;
363 /* Remap the variable. */
364 new_var = remap_decl (old_var, id);
366 /* If we didn't remap this variable, so we can't mess with its
367 TREE_CHAIN. If we remapped this variable to the return slot, it's
368 already declared somewhere else, so don't declare it here. */
369 if (!new_var || new_var == id->retvar)
371 else
373 gcc_assert (DECL_P (new_var));
374 TREE_CHAIN (new_var) = new_decls;
375 new_decls = new_var;
379 return nreverse (new_decls);
382 /* Copy the BLOCK to contain remapped versions of the variables
383 therein. And hook the new block into the block-tree. */
385 static void
386 remap_block (tree *block, copy_body_data *id)
388 tree old_block;
389 tree new_block;
390 tree fn;
392 /* Make the new block. */
393 old_block = *block;
394 new_block = make_node (BLOCK);
395 TREE_USED (new_block) = TREE_USED (old_block);
396 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
397 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
398 *block = new_block;
400 /* Remap its variables. */
401 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
403 fn = id->dst_fn;
405 if (id->transform_lang_insert_block)
406 lang_hooks.decls.insert_block (new_block);
408 /* Remember the remapped block. */
409 insert_decl_map (id, old_block, new_block);
412 /* Copy the whole block tree and root it in id->block. */
413 static tree
414 remap_blocks (tree block, copy_body_data *id)
416 tree t;
417 tree new = block;
419 if (!block)
420 return NULL;
422 remap_block (&new, id);
423 gcc_assert (new != block);
424 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
425 add_lexical_block (new, remap_blocks (t, id));
426 return new;
429 static void
430 copy_statement_list (tree *tp)
432 tree_stmt_iterator oi, ni;
433 tree new;
435 new = alloc_stmt_list ();
436 ni = tsi_start (new);
437 oi = tsi_start (*tp);
438 *tp = new;
440 for (; !tsi_end_p (oi); tsi_next (&oi))
441 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
444 static void
445 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
447 tree block = BIND_EXPR_BLOCK (*tp);
448 /* Copy (and replace) the statement. */
449 copy_tree_r (tp, walk_subtrees, NULL);
450 if (block)
452 remap_block (&block, id);
453 BIND_EXPR_BLOCK (*tp) = block;
456 if (BIND_EXPR_VARS (*tp))
457 /* This will remap a lot of the same decls again, but this should be
458 harmless. */
459 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
462 /* Called from copy_body_id via walk_tree. DATA is really an
463 `copy_body_data *'. */
465 tree
466 copy_body_r (tree *tp, int *walk_subtrees, void *data)
468 copy_body_data *id = (copy_body_data *) data;
469 tree fn = id->src_fn;
470 tree new_block;
472 /* Begin by recognizing trees that we'll completely rewrite for the
473 inlining context. Our output for these trees is completely
474 different from out input (e.g. RETURN_EXPR is deleted, and morphs
475 into an edge). Further down, we'll handle trees that get
476 duplicated and/or tweaked. */
478 /* When requested, RETURN_EXPRs should be transformed to just the
479 contained MODIFY_EXPR. The branch semantics of the return will
480 be handled elsewhere by manipulating the CFG rather than a statement. */
481 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
483 tree assignment = TREE_OPERAND (*tp, 0);
485 /* If we're returning something, just turn that into an
486 assignment into the equivalent of the original RESULT_DECL.
487 If the "assignment" is just the result decl, the result
488 decl has already been set (e.g. a recent "foo (&result_decl,
489 ...)"); just toss the entire RETURN_EXPR. */
490 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
492 /* Replace the RETURN_EXPR with (a copy of) the
493 MODIFY_EXPR hanging underneath. */
494 *tp = copy_node (assignment);
496 else /* Else the RETURN_EXPR returns no value. */
498 *tp = NULL;
499 return (tree) (void *)1;
503 /* Local variables and labels need to be replaced by equivalent
504 variables. We don't want to copy static variables; there's only
505 one of those, no matter how many times we inline the containing
506 function. Similarly for globals from an outer function. */
507 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
509 tree new_decl;
511 /* Remap the declaration. */
512 new_decl = remap_decl (*tp, id);
513 gcc_assert (new_decl);
514 /* Replace this variable with the copy. */
515 STRIP_TYPE_NOPS (new_decl);
516 *tp = new_decl;
517 *walk_subtrees = 0;
519 else if (TREE_CODE (*tp) == STATEMENT_LIST)
520 copy_statement_list (tp);
521 else if (TREE_CODE (*tp) == SAVE_EXPR)
522 remap_save_expr (tp, id->decl_map, walk_subtrees);
523 else if (TREE_CODE (*tp) == LABEL_DECL
524 && (! DECL_CONTEXT (*tp)
525 || decl_function_context (*tp) == id->src_fn))
526 /* These may need to be remapped for EH handling. */
527 *tp = remap_decl (*tp, id);
528 else if (TREE_CODE (*tp) == BIND_EXPR)
529 copy_bind_expr (tp, walk_subtrees, id);
530 /* Types may need remapping as well. */
531 else if (TYPE_P (*tp))
532 *tp = remap_type (*tp, id);
534 /* If this is a constant, we have to copy the node iff the type will be
535 remapped. copy_tree_r will not copy a constant. */
536 else if (CONSTANT_CLASS_P (*tp))
538 tree new_type = remap_type (TREE_TYPE (*tp), id);
540 if (new_type == TREE_TYPE (*tp))
541 *walk_subtrees = 0;
543 else if (TREE_CODE (*tp) == INTEGER_CST)
544 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
545 TREE_INT_CST_HIGH (*tp));
546 else
548 *tp = copy_node (*tp);
549 TREE_TYPE (*tp) = new_type;
553 /* Otherwise, just copy the node. Note that copy_tree_r already
554 knows not to copy VAR_DECLs, etc., so this is safe. */
555 else
557 /* Here we handle trees that are not completely rewritten.
558 First we detect some inlining-induced bogosities for
559 discarding. */
560 if (TREE_CODE (*tp) == MODIFY_EXPR
561 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
562 && (lang_hooks.tree_inlining.auto_var_in_fn_p
563 (TREE_OPERAND (*tp, 0), fn)))
565 /* Some assignments VAR = VAR; don't generate any rtl code
566 and thus don't count as variable modification. Avoid
567 keeping bogosities like 0 = 0. */
568 tree decl = TREE_OPERAND (*tp, 0), value;
569 splay_tree_node n;
571 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
572 if (n)
574 value = (tree) n->value;
575 STRIP_TYPE_NOPS (value);
576 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
578 *tp = build_empty_stmt ();
579 return copy_body_r (tp, walk_subtrees, data);
583 else if (TREE_CODE (*tp) == INDIRECT_REF)
585 /* Get rid of *& from inline substitutions that can happen when a
586 pointer argument is an ADDR_EXPR. */
587 tree decl = TREE_OPERAND (*tp, 0);
588 splay_tree_node n;
590 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
591 if (n)
593 tree new;
594 /* If we happen to get an ADDR_EXPR in n->value, strip
595 it manually here as we'll eventually get ADDR_EXPRs
596 which lie about their types pointed to. In this case
597 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
598 but we absolutely rely on that. As fold_indirect_ref
599 does other useful transformations, try that first, though. */
600 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
601 new = unshare_expr ((tree)n->value);
602 *tp = fold_indirect_ref_1 (type, new);
603 if (! *tp)
605 if (TREE_CODE (new) == ADDR_EXPR)
606 *tp = TREE_OPERAND (new, 0);
607 else
608 *tp = build1 (INDIRECT_REF, type, new);
610 *walk_subtrees = 0;
611 return NULL;
615 /* Here is the "usual case". Copy this tree node, and then
616 tweak some special cases. */
617 copy_tree_r (tp, walk_subtrees, NULL);
619 /* If EXPR has block defined, map it to newly constructed block.
620 When inlining we want EXPRs without block appear in the block
621 of function call. */
622 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp))))
624 new_block = id->block;
625 if (TREE_BLOCK (*tp))
627 splay_tree_node n;
628 n = splay_tree_lookup (id->decl_map,
629 (splay_tree_key) TREE_BLOCK (*tp));
630 gcc_assert (n);
631 new_block = (tree) n->value;
633 TREE_BLOCK (*tp) = new_block;
636 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
637 TREE_OPERAND (*tp, 0) =
638 build_int_cst
639 (NULL_TREE,
640 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
642 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
644 /* The copied TARGET_EXPR has never been expanded, even if the
645 original node was expanded already. */
646 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
648 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
649 TREE_OPERAND (*tp, 3) = NULL_TREE;
652 /* Variable substitution need not be simple. In particular, the
653 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
654 and friends are up-to-date. */
655 else if (TREE_CODE (*tp) == ADDR_EXPR)
657 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
658 recompute_tree_invariant_for_addr_expr (*tp);
659 *walk_subtrees = 0;
663 /* Keep iterating. */
664 return NULL_TREE;
667 /* Copy basic block, scale profile accordingly. Edges will be taken care of
668 later */
670 static basic_block
671 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
673 block_stmt_iterator bsi, copy_bsi;
674 basic_block copy_basic_block;
676 /* create_basic_block() will append every new block to
677 basic_block_info automatically. */
678 copy_basic_block = create_basic_block (NULL, (void *) 0,
679 (basic_block) bb->prev_bb->aux);
680 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
681 copy_basic_block->frequency = (bb->frequency
682 * frequency_scale / REG_BR_PROB_BASE);
683 copy_bsi = bsi_start (copy_basic_block);
685 for (bsi = bsi_start (bb);
686 !bsi_end_p (bsi); bsi_next (&bsi))
688 tree stmt = bsi_stmt (bsi);
689 tree orig_stmt = stmt;
691 walk_tree (&stmt, copy_body_r, id, NULL);
693 /* RETURN_EXPR might be removed,
694 this is signalled by making stmt pointer NULL. */
695 if (stmt)
697 tree call, decl;
698 bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
699 call = get_call_expr_in (stmt);
700 /* We're duplicating a CALL_EXPR. Find any corresponding
701 callgraph edges and update or duplicate them. */
702 if (call && (decl = get_callee_fndecl (call)))
704 struct cgraph_node *node;
705 struct cgraph_edge *edge;
707 switch (id->transform_call_graph_edges)
709 case CB_CGE_DUPLICATE:
710 edge = cgraph_edge (id->src_node, orig_stmt);
711 if (edge)
712 cgraph_clone_edge (edge, id->dst_node, stmt,
713 REG_BR_PROB_BASE, 1, true);
714 break;
716 case CB_CGE_MOVE_CLONES:
717 for (node = id->dst_node->next_clone;
718 node;
719 node = node->next_clone)
721 edge = cgraph_edge (node, orig_stmt);
722 gcc_assert (edge);
723 edge->call_stmt = stmt;
725 /* FALLTHRU */
727 case CB_CGE_MOVE:
728 edge = cgraph_edge (id->dst_node, orig_stmt);
729 if (edge)
730 edge->call_stmt = stmt;
731 break;
733 default:
734 gcc_unreachable ();
737 /* If you think we can abort here, you are wrong.
738 There is no region 0 in tree land. */
739 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
740 != 0);
742 if (tree_could_throw_p (stmt))
744 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
745 /* Add an entry for the copied tree in the EH hashtable.
746 When cloning or versioning, use the hashtable in
747 cfun, and just copy the EH number. When inlining, use the
748 hashtable in the caller, and adjust the region number. */
749 if (region > 0)
750 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
752 /* If this tree doesn't have a region associated with it,
753 and there is a "current region,"
754 then associate this tree with the current region
755 and add edges associated with this region. */
756 if ((lookup_stmt_eh_region_fn (id->src_cfun,
757 orig_stmt) <= 0
758 && id->eh_region > 0)
759 && tree_could_throw_p (stmt))
760 add_stmt_to_eh_region (stmt, id->eh_region);
764 return copy_basic_block;
767 /* Copy edges from BB into its copy constructed earlier, scale profile
768 accordingly. Edges will be taken care of later. Assume aux
769 pointers to point to the copies of each BB. */
770 static void
771 copy_edges_for_bb (basic_block bb, int count_scale)
773 basic_block new_bb = (basic_block) bb->aux;
774 edge_iterator ei;
775 edge old_edge;
776 block_stmt_iterator bsi;
777 int flags;
779 /* Use the indices from the original blocks to create edges for the
780 new ones. */
781 FOR_EACH_EDGE (old_edge, ei, bb->succs)
782 if (!(old_edge->flags & EDGE_EH))
784 edge new;
786 flags = old_edge->flags;
788 /* Return edges do get a FALLTHRU flag when the get inlined. */
789 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
790 && old_edge->dest->aux != EXIT_BLOCK_PTR)
791 flags |= EDGE_FALLTHRU;
792 new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
793 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
794 new->probability = old_edge->probability;
797 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
798 return;
800 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
802 tree copy_stmt;
804 copy_stmt = bsi_stmt (bsi);
805 update_stmt (copy_stmt);
806 /* Do this before the possible split_block. */
807 bsi_next (&bsi);
809 /* If this tree could throw an exception, there are two
810 cases where we need to add abnormal edge(s): the
811 tree wasn't in a region and there is a "current
812 region" in the caller; or the original tree had
813 EH edges. In both cases split the block after the tree,
814 and add abnormal edge(s) as needed; we need both
815 those from the callee and the caller.
816 We check whether the copy can throw, because the const
817 propagation can change an INDIRECT_REF which throws
818 into a COMPONENT_REF which doesn't. If the copy
819 can throw, the original could also throw. */
821 if (tree_can_throw_internal (copy_stmt))
823 if (!bsi_end_p (bsi))
824 /* Note that bb's predecessor edges aren't necessarily
825 right at this point; split_block doesn't care. */
827 edge e = split_block (new_bb, copy_stmt);
828 new_bb = e->dest;
829 bsi = bsi_start (new_bb);
832 make_eh_edges (copy_stmt);
837 /* Wrapper for remap_decl so it can be used as a callback. */
838 static tree
839 remap_decl_1 (tree decl, void *data)
841 return remap_decl (decl, (copy_body_data *) data);
844 /* Make a copy of the body of FN so that it can be inserted inline in
845 another function. Walks FN via CFG, returns new fndecl. */
847 static tree
848 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
849 basic_block entry_block_map, basic_block exit_block_map)
851 tree callee_fndecl = id->src_fn;
852 /* Original cfun for the callee, doesn't change. */
853 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
854 /* Copy, built by this function. */
855 struct function *new_cfun;
856 /* Place to copy from; when a copy of the function was saved off earlier,
857 use that instead of the main copy. */
858 struct function *cfun_to_copy =
859 (struct function *) ggc_alloc_cleared (sizeof (struct function));
860 basic_block bb;
861 tree new_fndecl = NULL;
862 int count_scale, frequency_scale;
864 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
865 count_scale = (REG_BR_PROB_BASE * count
866 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
867 else
868 count_scale = 1;
870 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
871 frequency_scale = (REG_BR_PROB_BASE * frequency
873 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
874 else
875 frequency_scale = count_scale;
877 /* Register specific tree functions. */
878 tree_register_cfg_hooks ();
880 /* Must have a CFG here at this point. */
881 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
882 (DECL_STRUCT_FUNCTION (callee_fndecl)));
884 *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
886 id->src_cfun = cfun_to_copy;
888 /* If requested, create new basic_block_info and label_to_block_maps.
889 Otherwise, insert our new blocks and labels into the existing cfg. */
890 if (id->transform_new_cfg)
892 new_cfun =
893 (struct function *) ggc_alloc_cleared (sizeof (struct function));
894 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
895 new_cfun->cfg = NULL;
896 new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
897 new_cfun->ib_boundaries_block = (varray_type) 0;
898 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
899 push_cfun (new_cfun);
900 init_empty_tree_cfg ();
902 ENTRY_BLOCK_PTR->count =
903 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
904 REG_BR_PROB_BASE);
905 ENTRY_BLOCK_PTR->frequency =
906 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
907 frequency_scale / REG_BR_PROB_BASE);
908 EXIT_BLOCK_PTR->count =
909 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
910 REG_BR_PROB_BASE);
911 EXIT_BLOCK_PTR->frequency =
912 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
913 frequency_scale / REG_BR_PROB_BASE);
915 entry_block_map = ENTRY_BLOCK_PTR;
916 exit_block_map = EXIT_BLOCK_PTR;
919 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
920 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
922 /* Duplicate any exception-handling regions. */
923 if (cfun->eh)
925 if (id->transform_new_cfg)
926 init_eh_for_function ();
927 id->eh_region_offset
928 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id, id->eh_region);
930 /* Use aux pointers to map the original blocks to copy. */
931 FOR_EACH_BB_FN (bb, cfun_to_copy)
932 bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
933 /* Now that we've duplicated the blocks, duplicate their edges. */
934 FOR_ALL_BB_FN (bb, cfun_to_copy)
935 copy_edges_for_bb (bb, count_scale);
936 FOR_ALL_BB_FN (bb, cfun_to_copy)
937 bb->aux = NULL;
939 if (id->transform_new_cfg)
940 pop_cfun ();
942 return new_fndecl;
945 /* Make a copy of the body of FN so that it can be inserted inline in
946 another function. */
948 static tree
949 copy_generic_body (copy_body_data *id)
951 tree body;
952 tree fndecl = id->src_fn;
954 body = DECL_SAVED_TREE (fndecl);
955 walk_tree (&body, copy_body_r, id, NULL);
957 return body;
960 static tree
961 copy_body (copy_body_data *id, gcov_type count, int frequency,
962 basic_block entry_block_map, basic_block exit_block_map)
964 tree fndecl = id->src_fn;
965 tree body;
967 /* If this body has a CFG, walk CFG and copy. */
968 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
969 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
971 return body;
974 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
975 defined in function FN, or of a data member thereof. */
977 static bool
978 self_inlining_addr_expr (tree value, tree fn)
980 tree var;
982 if (TREE_CODE (value) != ADDR_EXPR)
983 return false;
985 var = get_base_address (TREE_OPERAND (value, 0));
987 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
990 static void
991 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
992 basic_block bb, tree *vars)
994 tree init_stmt;
995 tree var;
996 tree var_sub;
998 /* If the parameter is never assigned to, we may not need to
999 create a new variable here at all. Instead, we may be able
1000 to just use the argument value. */
1001 if (TREE_READONLY (p)
1002 && !TREE_ADDRESSABLE (p)
1003 && value && !TREE_SIDE_EFFECTS (value))
1005 /* We may produce non-gimple trees by adding NOPs or introduce
1006 invalid sharing when operand is not really constant.
1007 It is not big deal to prohibit constant propagation here as
1008 we will constant propagate in DOM1 pass anyway. */
1009 if (is_gimple_min_invariant (value)
1010 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1011 /* We have to be very careful about ADDR_EXPR. Make sure
1012 the base variable isn't a local variable of the inlined
1013 function, e.g., when doing recursive inlining, direct or
1014 mutually-recursive or whatever, which is why we don't
1015 just test whether fn == current_function_decl. */
1016 && ! self_inlining_addr_expr (value, fn))
1018 insert_decl_map (id, p, value);
1019 return;
1023 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1024 here since the type of this decl must be visible to the calling
1025 function. */
1026 var = copy_decl_to_var (p, id);
1028 /* See if the frontend wants to pass this by invisible reference. If
1029 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1030 replace uses of the PARM_DECL with dereferences. */
1031 if (TREE_TYPE (var) != TREE_TYPE (p)
1032 && POINTER_TYPE_P (TREE_TYPE (var))
1033 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1035 insert_decl_map (id, var, var);
1036 var_sub = build_fold_indirect_ref (var);
1038 else
1039 var_sub = var;
1041 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1042 that way, when the PARM_DECL is encountered, it will be
1043 automatically replaced by the VAR_DECL. */
1044 insert_decl_map (id, p, var_sub);
1046 /* Declare this new variable. */
1047 TREE_CHAIN (var) = *vars;
1048 *vars = var;
1050 /* Make gimplifier happy about this variable. */
1051 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1053 /* Even if P was TREE_READONLY, the new VAR should not be.
1054 In the original code, we would have constructed a
1055 temporary, and then the function body would have never
1056 changed the value of P. However, now, we will be
1057 constructing VAR directly. The constructor body may
1058 change its value multiple times as it is being
1059 constructed. Therefore, it must not be TREE_READONLY;
1060 the back-end assumes that TREE_READONLY variable is
1061 assigned to only once. */
1062 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1063 TREE_READONLY (var) = 0;
1065 /* Initialize this VAR_DECL from the equivalent argument. Convert
1066 the argument to the proper type in case it was promoted. */
1067 if (value)
1069 tree rhs = fold_convert (TREE_TYPE (var), value);
1070 block_stmt_iterator bsi = bsi_last (bb);
1072 if (rhs == error_mark_node)
1073 return;
1075 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1076 keep our trees in gimple form. */
1077 init_stmt = build2 (MODIFY_EXPR, TREE_TYPE (var), var, rhs);
1079 /* If we did not create a gimple value and we did not create a gimple
1080 cast of a gimple value, then we will need to gimplify INIT_STMTS
1081 at the end. Note that is_gimple_cast only checks the outer
1082 tree code, not its operand. Thus the explicit check that its
1083 operand is a gimple value. */
1084 if (!is_gimple_val (rhs)
1085 && (!is_gimple_cast (rhs)
1086 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1087 gimplify_stmt (&init_stmt);
1089 /* If VAR represents a zero-sized variable, it's possible that the
1090 assignment statment may result in no gimple statements. */
1091 if (init_stmt)
1092 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1096 /* Generate code to initialize the parameters of the function at the
1097 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1099 static void
1100 initialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
1101 tree fn, basic_block bb)
1103 tree parms;
1104 tree a;
1105 tree p;
1106 tree vars = NULL_TREE;
1107 int argnum = 0;
1109 /* Figure out what the parameters are. */
1110 parms = DECL_ARGUMENTS (fn);
1112 /* Loop through the parameter declarations, replacing each with an
1113 equivalent VAR_DECL, appropriately initialized. */
1114 for (p = parms, a = args; p;
1115 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1117 tree value;
1119 ++argnum;
1121 /* Find the initializer. */
1122 value = lang_hooks.tree_inlining.convert_parm_for_inlining
1123 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1125 setup_one_parameter (id, p, value, fn, bb, &vars);
1128 /* Initialize the static chain. */
1129 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1130 gcc_assert (fn != current_function_decl);
1131 if (p)
1133 /* No static chain? Seems like a bug in tree-nested.c. */
1134 gcc_assert (static_chain);
1136 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1139 declare_inline_vars (id->block, vars);
1142 /* Declare a return variable to replace the RESULT_DECL for the
1143 function we are calling. An appropriate DECL_STMT is returned.
1144 The USE_STMT is filled to contain a use of the declaration to
1145 indicate the return value of the function.
1147 RETURN_SLOT_ADDR, if non-null, was a fake parameter that
1148 took the address of the result. MODIFY_DEST, if non-null, was the LHS of
1149 the MODIFY_EXPR to which this call is the RHS.
1151 The return value is a (possibly null) value that is the result of the
1152 function as seen by the callee. *USE_P is a (possibly null) value that
1153 holds the result as seen by the caller. */
1155 static tree
1156 declare_return_variable (copy_body_data *id, tree return_slot_addr,
1157 tree modify_dest, tree *use_p)
1159 tree callee = id->src_fn;
1160 tree caller = id->dst_fn;
1161 tree result = DECL_RESULT (callee);
1162 tree callee_type = TREE_TYPE (result);
1163 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1164 tree var, use;
1166 /* We don't need to do anything for functions that don't return
1167 anything. */
1168 if (!result || VOID_TYPE_P (callee_type))
1170 *use_p = NULL_TREE;
1171 return NULL_TREE;
1174 /* If there was a return slot, then the return value is the
1175 dereferenced address of that object. */
1176 if (return_slot_addr)
1178 /* The front end shouldn't have used both return_slot_addr and
1179 a modify expression. */
1180 gcc_assert (!modify_dest);
1181 if (DECL_BY_REFERENCE (result))
1182 var = return_slot_addr;
1183 else
1184 var = build_fold_indirect_ref (return_slot_addr);
1185 if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1186 && !DECL_COMPLEX_GIMPLE_REG_P (result)
1187 && DECL_P (var))
1188 DECL_COMPLEX_GIMPLE_REG_P (var) = 0;
1189 use = NULL;
1190 goto done;
1193 /* All types requiring non-trivial constructors should have been handled. */
1194 gcc_assert (!TREE_ADDRESSABLE (callee_type));
1196 /* Attempt to avoid creating a new temporary variable. */
1197 if (modify_dest)
1199 bool use_it = false;
1201 /* We can't use MODIFY_DEST if there's type promotion involved. */
1202 if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1203 use_it = false;
1205 /* ??? If we're assigning to a variable sized type, then we must
1206 reuse the destination variable, because we've no good way to
1207 create variable sized temporaries at this point. */
1208 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1209 use_it = true;
1211 /* If the callee cannot possibly modify MODIFY_DEST, then we can
1212 reuse it as the result of the call directly. Don't do this if
1213 it would promote MODIFY_DEST to addressable. */
1214 else if (TREE_ADDRESSABLE (result))
1215 use_it = false;
1216 else
1218 tree base_m = get_base_address (modify_dest);
1220 /* If the base isn't a decl, then it's a pointer, and we don't
1221 know where that's going to go. */
1222 if (!DECL_P (base_m))
1223 use_it = false;
1224 else if (is_global_var (base_m))
1225 use_it = false;
1226 else if (!TREE_ADDRESSABLE (base_m))
1227 use_it = true;
1230 if (use_it)
1232 var = modify_dest;
1233 use = NULL;
1234 goto done;
1238 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1240 var = copy_decl_to_var (result, id);
1242 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1243 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1244 = tree_cons (NULL_TREE, var,
1245 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1247 /* Do not have the rest of GCC warn about this variable as it should
1248 not be visible to the user. */
1249 TREE_NO_WARNING (var) = 1;
1251 /* Build the use expr. If the return type of the function was
1252 promoted, convert it back to the expected type. */
1253 use = var;
1254 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1255 use = fold_convert (caller_type, var);
1257 done:
1258 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1259 way, when the RESULT_DECL is encountered, it will be
1260 automatically replaced by the VAR_DECL. */
1261 insert_decl_map (id, result, var);
1263 /* Remember this so we can ignore it in remap_decls. */
1264 id->retvar = var;
1266 *use_p = use;
1267 return var;
1270 /* Returns nonzero if a function can be inlined as a tree. */
1272 bool
1273 tree_inlinable_function_p (tree fn)
1275 return inlinable_function_p (fn);
1278 static const char *inline_forbidden_reason;
1280 static tree
1281 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1282 void *fnp)
1284 tree node = *nodep;
1285 tree fn = (tree) fnp;
1286 tree t;
1288 switch (TREE_CODE (node))
1290 case CALL_EXPR:
1291 /* Refuse to inline alloca call unless user explicitly forced so as
1292 this may change program's memory overhead drastically when the
1293 function using alloca is called in loop. In GCC present in
1294 SPEC2000 inlining into schedule_block cause it to require 2GB of
1295 RAM instead of 256MB. */
1296 if (alloca_call_p (node)
1297 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1299 inline_forbidden_reason
1300 = G_("function %q+F can never be inlined because it uses "
1301 "alloca (override using the always_inline attribute)");
1302 return node;
1304 t = get_callee_fndecl (node);
1305 if (! t)
1306 break;
1308 /* We cannot inline functions that call setjmp. */
1309 if (setjmp_call_p (t))
1311 inline_forbidden_reason
1312 = G_("function %q+F can never be inlined because it uses setjmp");
1313 return node;
1316 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1317 switch (DECL_FUNCTION_CODE (t))
1319 /* We cannot inline functions that take a variable number of
1320 arguments. */
1321 case BUILT_IN_VA_START:
1322 case BUILT_IN_STDARG_START:
1323 case BUILT_IN_NEXT_ARG:
1324 case BUILT_IN_VA_END:
1325 inline_forbidden_reason
1326 = G_("function %q+F can never be inlined because it "
1327 "uses variable argument lists");
1328 return node;
1330 case BUILT_IN_LONGJMP:
1331 /* We can't inline functions that call __builtin_longjmp at
1332 all. The non-local goto machinery really requires the
1333 destination be in a different function. If we allow the
1334 function calling __builtin_longjmp to be inlined into the
1335 function calling __builtin_setjmp, Things will Go Awry. */
1336 inline_forbidden_reason
1337 = G_("function %q+F can never be inlined because "
1338 "it uses setjmp-longjmp exception handling");
1339 return node;
1341 case BUILT_IN_NONLOCAL_GOTO:
1342 /* Similarly. */
1343 inline_forbidden_reason
1344 = G_("function %q+F can never be inlined because "
1345 "it uses non-local goto");
1346 return node;
1348 case BUILT_IN_RETURN:
1349 case BUILT_IN_APPLY_ARGS:
1350 /* If a __builtin_apply_args caller would be inlined,
1351 it would be saving arguments of the function it has
1352 been inlined into. Similarly __builtin_return would
1353 return from the function the inline has been inlined into. */
1354 inline_forbidden_reason
1355 = G_("function %q+F can never be inlined because "
1356 "it uses __builtin_return or __builtin_apply_args");
1357 return node;
1359 default:
1360 break;
1362 break;
1364 case GOTO_EXPR:
1365 t = TREE_OPERAND (node, 0);
1367 /* We will not inline a function which uses computed goto. The
1368 addresses of its local labels, which may be tucked into
1369 global storage, are of course not constant across
1370 instantiations, which causes unexpected behavior. */
1371 if (TREE_CODE (t) != LABEL_DECL)
1373 inline_forbidden_reason
1374 = G_("function %q+F can never be inlined "
1375 "because it contains a computed goto");
1376 return node;
1378 break;
1380 case LABEL_EXPR:
1381 t = TREE_OPERAND (node, 0);
1382 if (DECL_NONLOCAL (t))
1384 /* We cannot inline a function that receives a non-local goto
1385 because we cannot remap the destination label used in the
1386 function that is performing the non-local goto. */
1387 inline_forbidden_reason
1388 = G_("function %q+F can never be inlined "
1389 "because it receives a non-local goto");
1390 return node;
1392 break;
1394 case RECORD_TYPE:
1395 case UNION_TYPE:
1396 /* We cannot inline a function of the form
1398 void F (int i) { struct S { int ar[i]; } s; }
1400 Attempting to do so produces a catch-22.
1401 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1402 UNION_TYPE nodes, then it goes into infinite recursion on a
1403 structure containing a pointer to its own type. If it doesn't,
1404 then the type node for S doesn't get adjusted properly when
1405 F is inlined.
1407 ??? This is likely no longer true, but it's too late in the 4.0
1408 cycle to try to find out. This should be checked for 4.1. */
1409 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1410 if (variably_modified_type_p (TREE_TYPE (t), NULL))
1412 inline_forbidden_reason
1413 = G_("function %q+F can never be inlined "
1414 "because it uses variable sized variables");
1415 return node;
1418 default:
1419 break;
1422 return NULL_TREE;
1425 /* Return subexpression representing possible alloca call, if any. */
1426 static tree
1427 inline_forbidden_p (tree fndecl)
1429 location_t saved_loc = input_location;
1430 block_stmt_iterator bsi;
1431 basic_block bb;
1432 tree ret = NULL_TREE;
1434 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1435 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1437 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1438 inline_forbidden_p_1, fndecl);
1439 if (ret)
1440 goto egress;
1443 egress:
1444 input_location = saved_loc;
1445 return ret;
1448 /* Returns nonzero if FN is a function that does not have any
1449 fundamental inline blocking properties. */
1451 static bool
1452 inlinable_function_p (tree fn)
1454 bool inlinable = true;
1456 /* If we've already decided this function shouldn't be inlined,
1457 there's no need to check again. */
1458 if (DECL_UNINLINABLE (fn))
1459 return false;
1461 /* See if there is any language-specific reason it cannot be
1462 inlined. (It is important that this hook be called early because
1463 in C++ it may result in template instantiation.)
1464 If the function is not inlinable for language-specific reasons,
1465 it is left up to the langhook to explain why. */
1466 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1468 /* If we don't have the function body available, we can't inline it.
1469 However, this should not be recorded since we also get here for
1470 forward declared inline functions. Therefore, return at once. */
1471 if (!DECL_SAVED_TREE (fn))
1472 return false;
1474 /* If we're not inlining at all, then we cannot inline this function. */
1475 else if (!flag_inline_trees)
1476 inlinable = false;
1478 /* Only try to inline functions if DECL_INLINE is set. This should be
1479 true for all functions declared `inline', and for all other functions
1480 as well with -finline-functions.
1482 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1483 it's the front-end that must set DECL_INLINE in this case, because
1484 dwarf2out loses if a function that does not have DECL_INLINE set is
1485 inlined anyway. That is why we have both DECL_INLINE and
1486 DECL_DECLARED_INLINE_P. */
1487 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1488 here should be redundant. */
1489 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1490 inlinable = false;
1492 else if (inline_forbidden_p (fn))
1494 /* See if we should warn about uninlinable functions. Previously,
1495 some of these warnings would be issued while trying to expand
1496 the function inline, but that would cause multiple warnings
1497 about functions that would for example call alloca. But since
1498 this a property of the function, just one warning is enough.
1499 As a bonus we can now give more details about the reason why a
1500 function is not inlinable.
1501 We only warn for functions declared `inline' by the user. */
1502 bool do_warning = (warn_inline
1503 && DECL_INLINE (fn)
1504 && DECL_DECLARED_INLINE_P (fn)
1505 && !DECL_IN_SYSTEM_HEADER (fn));
1507 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1508 sorry (inline_forbidden_reason, fn);
1509 else if (do_warning)
1510 warning (OPT_Winline, inline_forbidden_reason, fn);
1512 inlinable = false;
1515 /* Squirrel away the result so that we don't have to check again. */
1516 DECL_UNINLINABLE (fn) = !inlinable;
1518 return inlinable;
1521 /* Estimate the cost of a memory move. Use machine dependent
1522 word size and take possible memcpy call into account. */
1525 estimate_move_cost (tree type)
1527 HOST_WIDE_INT size;
1529 size = int_size_in_bytes (type);
1531 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1532 /* Cost of a memcpy call, 3 arguments and the call. */
1533 return 4;
1534 else
1535 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1538 /* Used by estimate_num_insns. Estimate number of instructions seen
1539 by given statement. */
1541 static tree
1542 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1544 int *count = (int *) data;
1545 tree x = *tp;
1547 if (IS_TYPE_OR_DECL_P (x))
1549 *walk_subtrees = 0;
1550 return NULL;
1552 /* Assume that constants and references counts nothing. These should
1553 be majorized by amount of operations among them we count later
1554 and are common target of CSE and similar optimizations. */
1555 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1556 return NULL;
1558 switch (TREE_CODE (x))
1560 /* Containers have no cost. */
1561 case TREE_LIST:
1562 case TREE_VEC:
1563 case BLOCK:
1564 case COMPONENT_REF:
1565 case BIT_FIELD_REF:
1566 case INDIRECT_REF:
1567 case ALIGN_INDIRECT_REF:
1568 case MISALIGNED_INDIRECT_REF:
1569 case ARRAY_REF:
1570 case ARRAY_RANGE_REF:
1571 case OBJ_TYPE_REF:
1572 case EXC_PTR_EXPR: /* ??? */
1573 case FILTER_EXPR: /* ??? */
1574 case COMPOUND_EXPR:
1575 case BIND_EXPR:
1576 case WITH_CLEANUP_EXPR:
1577 case NOP_EXPR:
1578 case VIEW_CONVERT_EXPR:
1579 case SAVE_EXPR:
1580 case ADDR_EXPR:
1581 case COMPLEX_EXPR:
1582 case RANGE_EXPR:
1583 case CASE_LABEL_EXPR:
1584 case SSA_NAME:
1585 case CATCH_EXPR:
1586 case EH_FILTER_EXPR:
1587 case STATEMENT_LIST:
1588 case ERROR_MARK:
1589 case NON_LVALUE_EXPR:
1590 case FDESC_EXPR:
1591 case VA_ARG_EXPR:
1592 case TRY_CATCH_EXPR:
1593 case TRY_FINALLY_EXPR:
1594 case LABEL_EXPR:
1595 case GOTO_EXPR:
1596 case RETURN_EXPR:
1597 case EXIT_EXPR:
1598 case LOOP_EXPR:
1599 case PHI_NODE:
1600 case WITH_SIZE_EXPR:
1601 break;
1603 /* We don't account constants for now. Assume that the cost is amortized
1604 by operations that do use them. We may re-consider this decision once
1605 we are able to optimize the tree before estimating its size and break
1606 out static initializers. */
1607 case IDENTIFIER_NODE:
1608 case INTEGER_CST:
1609 case REAL_CST:
1610 case COMPLEX_CST:
1611 case VECTOR_CST:
1612 case STRING_CST:
1613 *walk_subtrees = 0;
1614 return NULL;
1616 /* Try to estimate the cost of assignments. We have three cases to
1617 deal with:
1618 1) Simple assignments to registers;
1619 2) Stores to things that must live in memory. This includes
1620 "normal" stores to scalars, but also assignments of large
1621 structures, or constructors of big arrays;
1622 3) TARGET_EXPRs.
1624 Let us look at the first two cases, assuming we have "a = b + C":
1625 <modify_expr <var_decl "a"> <plus_expr <var_decl "b"> <constant C>>
1626 If "a" is a GIMPLE register, the assignment to it is free on almost
1627 any target, because "a" usually ends up in a real register. Hence
1628 the only cost of this expression comes from the PLUS_EXPR, and we
1629 can ignore the MODIFY_EXPR.
1630 If "a" is not a GIMPLE register, the assignment to "a" will most
1631 likely be a real store, so the cost of the MODIFY_EXPR is the cost
1632 of moving something into "a", which we compute using the function
1633 estimate_move_cost.
1635 The third case deals with TARGET_EXPRs, for which the semantics are
1636 that a temporary is assigned, unless the TARGET_EXPR itself is being
1637 assigned to something else. In the latter case we do not need the
1638 temporary. E.g. in <modify_expr <var_decl "a"> <target_expr>>, the
1639 MODIFY_EXPR is free. */
1640 case INIT_EXPR:
1641 case MODIFY_EXPR:
1642 /* Is the right and side a TARGET_EXPR? */
1643 if (TREE_CODE (TREE_OPERAND (x, 1)) == TARGET_EXPR)
1644 break;
1645 /* ... fall through ... */
1647 case TARGET_EXPR:
1648 x = TREE_OPERAND (x, 0);
1649 /* Is this an assignments to a register? */
1650 if (is_gimple_reg (x))
1651 break;
1652 /* Otherwise it's a store, so fall through to compute the move cost. */
1654 case CONSTRUCTOR:
1655 *count += estimate_move_cost (TREE_TYPE (x));
1656 break;
1658 /* Assign cost of 1 to usual operations.
1659 ??? We may consider mapping RTL costs to this. */
1660 case COND_EXPR:
1661 case VEC_COND_EXPR:
1663 case PLUS_EXPR:
1664 case MINUS_EXPR:
1665 case MULT_EXPR:
1667 case FIX_TRUNC_EXPR:
1668 case FIX_CEIL_EXPR:
1669 case FIX_FLOOR_EXPR:
1670 case FIX_ROUND_EXPR:
1672 case NEGATE_EXPR:
1673 case FLOAT_EXPR:
1674 case MIN_EXPR:
1675 case MAX_EXPR:
1676 case ABS_EXPR:
1678 case LSHIFT_EXPR:
1679 case RSHIFT_EXPR:
1680 case LROTATE_EXPR:
1681 case RROTATE_EXPR:
1682 case VEC_LSHIFT_EXPR:
1683 case VEC_RSHIFT_EXPR:
1685 case BIT_IOR_EXPR:
1686 case BIT_XOR_EXPR:
1687 case BIT_AND_EXPR:
1688 case BIT_NOT_EXPR:
1690 case TRUTH_ANDIF_EXPR:
1691 case TRUTH_ORIF_EXPR:
1692 case TRUTH_AND_EXPR:
1693 case TRUTH_OR_EXPR:
1694 case TRUTH_XOR_EXPR:
1695 case TRUTH_NOT_EXPR:
1697 case LT_EXPR:
1698 case LE_EXPR:
1699 case GT_EXPR:
1700 case GE_EXPR:
1701 case EQ_EXPR:
1702 case NE_EXPR:
1703 case ORDERED_EXPR:
1704 case UNORDERED_EXPR:
1706 case UNLT_EXPR:
1707 case UNLE_EXPR:
1708 case UNGT_EXPR:
1709 case UNGE_EXPR:
1710 case UNEQ_EXPR:
1711 case LTGT_EXPR:
1713 case CONVERT_EXPR:
1715 case CONJ_EXPR:
1717 case PREDECREMENT_EXPR:
1718 case PREINCREMENT_EXPR:
1719 case POSTDECREMENT_EXPR:
1720 case POSTINCREMENT_EXPR:
1722 case SWITCH_EXPR:
1724 case ASM_EXPR:
1726 case REALIGN_LOAD_EXPR:
1728 case REDUC_MAX_EXPR:
1729 case REDUC_MIN_EXPR:
1730 case REDUC_PLUS_EXPR:
1732 case RESX_EXPR:
1733 *count += 1;
1734 break;
1736 /* Few special cases of expensive operations. This is useful
1737 to avoid inlining on functions having too many of these. */
1738 case TRUNC_DIV_EXPR:
1739 case CEIL_DIV_EXPR:
1740 case FLOOR_DIV_EXPR:
1741 case ROUND_DIV_EXPR:
1742 case EXACT_DIV_EXPR:
1743 case TRUNC_MOD_EXPR:
1744 case CEIL_MOD_EXPR:
1745 case FLOOR_MOD_EXPR:
1746 case ROUND_MOD_EXPR:
1747 case RDIV_EXPR:
1748 *count += 10;
1749 break;
1750 case CALL_EXPR:
1752 tree decl = get_callee_fndecl (x);
1753 tree arg;
1755 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
1756 switch (DECL_FUNCTION_CODE (decl))
1758 case BUILT_IN_CONSTANT_P:
1759 *walk_subtrees = 0;
1760 return NULL_TREE;
1761 case BUILT_IN_EXPECT:
1762 return NULL_TREE;
1763 default:
1764 break;
1767 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1768 that does use function declaration to figure out the arguments. */
1769 if (!decl)
1771 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
1772 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
1774 else
1776 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
1777 *count += estimate_move_cost (TREE_TYPE (arg));
1780 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
1781 break;
1783 default:
1784 gcc_unreachable ();
1786 return NULL;
1789 /* Estimate number of instructions that will be created by expanding EXPR. */
1792 estimate_num_insns (tree expr)
1794 int num = 0;
1795 struct pointer_set_t *visited_nodes;
1796 basic_block bb;
1797 block_stmt_iterator bsi;
1798 struct function *my_function;
1800 /* If we're given an entire function, walk the CFG. */
1801 if (TREE_CODE (expr) == FUNCTION_DECL)
1803 my_function = DECL_STRUCT_FUNCTION (expr);
1804 gcc_assert (my_function && my_function->cfg);
1805 visited_nodes = pointer_set_create ();
1806 FOR_EACH_BB_FN (bb, my_function)
1808 for (bsi = bsi_start (bb);
1809 !bsi_end_p (bsi);
1810 bsi_next (&bsi))
1812 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
1813 &num, visited_nodes);
1816 pointer_set_destroy (visited_nodes);
1818 else
1819 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
1821 return num;
1824 typedef struct function *function_p;
1826 DEF_VEC_P(function_p);
1827 DEF_VEC_ALLOC_P(function_p,heap);
1829 /* Initialized with NOGC, making this poisonous to the garbage collector. */
1830 static VEC(function_p,heap) *cfun_stack;
1832 void
1833 push_cfun (struct function *new_cfun)
1835 VEC_safe_push (function_p, heap, cfun_stack, cfun);
1836 cfun = new_cfun;
1839 void
1840 pop_cfun (void)
1842 cfun = VEC_pop (function_p, cfun_stack);
1845 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
1846 static void
1847 add_lexical_block (tree current_block, tree new_block)
1849 tree *blk_p;
1851 /* Walk to the last sub-block. */
1852 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
1853 *blk_p;
1854 blk_p = &TREE_CHAIN (*blk_p))
1856 *blk_p = new_block;
1857 BLOCK_SUPERCONTEXT (new_block) = current_block;
1860 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1862 static bool
1863 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
1865 copy_body_data *id;
1866 tree t;
1867 tree use_retvar;
1868 tree fn;
1869 splay_tree st;
1870 tree args;
1871 tree return_slot_addr;
1872 tree modify_dest;
1873 location_t saved_location;
1874 struct cgraph_edge *cg_edge;
1875 const char *reason;
1876 basic_block return_block;
1877 edge e;
1878 block_stmt_iterator bsi, stmt_bsi;
1879 bool successfully_inlined = FALSE;
1880 tree t_step;
1881 tree var;
1882 tree decl;
1884 /* See what we've got. */
1885 id = (copy_body_data *) data;
1886 t = *tp;
1888 /* Set input_location here so we get the right instantiation context
1889 if we call instantiate_decl from inlinable_function_p. */
1890 saved_location = input_location;
1891 if (EXPR_HAS_LOCATION (t))
1892 input_location = EXPR_LOCATION (t);
1894 /* From here on, we're only interested in CALL_EXPRs. */
1895 if (TREE_CODE (t) != CALL_EXPR)
1896 goto egress;
1898 /* First, see if we can figure out what function is being called.
1899 If we cannot, then there is no hope of inlining the function. */
1900 fn = get_callee_fndecl (t);
1901 if (!fn)
1902 goto egress;
1904 /* Turn forward declarations into real ones. */
1905 fn = cgraph_node (fn)->decl;
1907 /* If fn is a declaration of a function in a nested scope that was
1908 globally declared inline, we don't set its DECL_INITIAL.
1909 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1910 C++ front-end uses it for cdtors to refer to their internal
1911 declarations, that are not real functions. Fortunately those
1912 don't have trees to be saved, so we can tell by checking their
1913 DECL_SAVED_TREE. */
1914 if (! DECL_INITIAL (fn)
1915 && DECL_ABSTRACT_ORIGIN (fn)
1916 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1917 fn = DECL_ABSTRACT_ORIGIN (fn);
1919 /* Objective C and fortran still calls tree_rest_of_compilation directly.
1920 Kill this check once this is fixed. */
1921 if (!id->dst_node->analyzed)
1922 goto egress;
1924 cg_edge = cgraph_edge (id->dst_node, stmt);
1926 /* Constant propagation on argument done during previous inlining
1927 may create new direct call. Produce an edge for it. */
1928 if (!cg_edge)
1930 struct cgraph_node *dest = cgraph_node (fn);
1932 /* We have missing edge in the callgraph. This can happen in one case
1933 where previous inlining turned indirect call into direct call by
1934 constant propagating arguments. In all other cases we hit a bug
1935 (incorrect node sharing is most common reason for missing edges. */
1936 gcc_assert (dest->needed || !flag_unit_at_a_time);
1937 cgraph_create_edge (id->dst_node, dest, stmt,
1938 bb->count, bb->loop_depth)->inline_failed
1939 = N_("originally indirect function call not considered for inlining");
1940 goto egress;
1943 /* Don't try to inline functions that are not well-suited to
1944 inlining. */
1945 if (!cgraph_inline_p (cg_edge, &reason))
1947 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
1948 /* Avoid warnings during early inline pass. */
1949 && (!flag_unit_at_a_time || cgraph_global_info_ready))
1951 sorry ("inlining failed in call to %q+F: %s", fn, reason);
1952 sorry ("called from here");
1954 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
1955 && !DECL_IN_SYSTEM_HEADER (fn)
1956 && strlen (reason)
1957 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
1958 /* Avoid warnings during early inline pass. */
1959 && (!flag_unit_at_a_time || cgraph_global_info_ready))
1961 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
1962 fn, reason);
1963 warning (OPT_Winline, "called from here");
1965 goto egress;
1967 fn = cg_edge->callee->decl;
1969 #ifdef ENABLE_CHECKING
1970 if (cg_edge->callee->decl != id->dst_node->decl)
1971 verify_cgraph_node (cg_edge->callee);
1972 #endif
1974 /* We will be inlining this callee. */
1976 id->eh_region = lookup_stmt_eh_region (stmt);
1978 /* Split the block holding the CALL_EXPR. */
1980 e = split_block (bb, stmt);
1981 bb = e->src;
1982 return_block = e->dest;
1983 remove_edge (e);
1985 /* split_block splits before the statement, work around this by moving
1986 the call into the first half_bb. Not pretty, but seems easier than
1987 doing the CFG manipulation by hand when the CALL_EXPR is in the last
1988 statement in BB. */
1989 stmt_bsi = bsi_last (bb);
1990 bsi = bsi_start (return_block);
1991 if (!bsi_end_p (bsi))
1992 bsi_move_before (&stmt_bsi, &bsi);
1993 else
1995 tree stmt = bsi_stmt (stmt_bsi);
1996 bsi_remove (&stmt_bsi, false);
1997 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
1999 stmt_bsi = bsi_start (return_block);
2001 /* Build a block containing code to initialize the arguments, the
2002 actual inline expansion of the body, and a label for the return
2003 statements within the function to jump to. The type of the
2004 statement expression is the return type of the function call. */
2005 id->block = make_node (BLOCK);
2006 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2007 BLOCK_SOURCE_LOCATION (id->block) = input_location;
2008 add_lexical_block (TREE_BLOCK (stmt), id->block);
2010 /* Local declarations will be replaced by their equivalents in this
2011 map. */
2012 st = id->decl_map;
2013 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2014 NULL, NULL);
2016 /* Initialize the parameters. */
2017 args = TREE_OPERAND (t, 1);
2019 /* Record the function we are about to inline. */
2020 id->src_fn = fn;
2021 id->src_node = cg_edge->callee;
2023 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2025 if (DECL_INITIAL (fn))
2026 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
2028 /* Return statements in the function body will be replaced by jumps
2029 to the RET_LABEL. */
2031 gcc_assert (DECL_INITIAL (fn));
2032 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2034 /* Find the lhs to which the result of this call is assigned. */
2035 return_slot_addr = NULL;
2036 if (TREE_CODE (stmt) == MODIFY_EXPR)
2038 modify_dest = TREE_OPERAND (stmt, 0);
2040 /* The function which we are inlining might not return a value,
2041 in which case we should issue a warning that the function
2042 does not return a value. In that case the optimizers will
2043 see that the variable to which the value is assigned was not
2044 initialized. We do not want to issue a warning about that
2045 uninitialized variable. */
2046 if (DECL_P (modify_dest))
2047 TREE_NO_WARNING (modify_dest) = 1;
2048 if (CALL_EXPR_RETURN_SLOT_OPT (t))
2050 return_slot_addr = build_fold_addr_expr (modify_dest);
2051 modify_dest = NULL;
2054 else
2055 modify_dest = NULL;
2057 /* Declare the return variable for the function. */
2058 decl = declare_return_variable (id, return_slot_addr,
2059 modify_dest, &use_retvar);
2060 /* Do this only if declare_return_variable created a new one. */
2061 if (decl && !return_slot_addr && decl != modify_dest)
2062 declare_inline_vars (id->block, decl);
2064 /* This is it. Duplicate the callee body. Assume callee is
2065 pre-gimplified. Note that we must not alter the caller
2066 function in any way before this point, as this CALL_EXPR may be
2067 a self-referential call; if we're calling ourselves, we need to
2068 duplicate our body before altering anything. */
2069 copy_body (id, bb->count, bb->frequency, bb, return_block);
2071 /* Add local vars in this inlined callee to caller. */
2072 t_step = id->src_cfun->unexpanded_var_list;
2073 for (; t_step; t_step = TREE_CHAIN (t_step))
2075 var = TREE_VALUE (t_step);
2076 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2077 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2078 cfun->unexpanded_var_list);
2079 else
2080 cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2081 cfun->unexpanded_var_list);
2084 /* Clean up. */
2085 splay_tree_delete (id->decl_map);
2086 id->decl_map = st;
2088 /* If the inlined function returns a result that we care about,
2089 clobber the CALL_EXPR with a reference to the return variable. */
2090 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2092 *tp = use_retvar;
2093 maybe_clean_or_replace_eh_stmt (stmt, stmt);
2095 else
2096 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2097 tsi_delink() will leave the iterator in a sane state. */
2098 bsi_remove (&stmt_bsi, true);
2100 bsi_next (&bsi);
2101 if (bsi_end_p (bsi))
2102 tree_purge_dead_eh_edges (return_block);
2104 /* If the value of the new expression is ignored, that's OK. We
2105 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2106 the equivalent inlined version either. */
2107 TREE_USED (*tp) = 1;
2109 /* Output the inlining info for this abstract function, since it has been
2110 inlined. If we don't do this now, we can lose the information about the
2111 variables in the function when the blocks get blown away as soon as we
2112 remove the cgraph node. */
2113 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2115 /* Update callgraph if needed. */
2116 cgraph_remove_node (cg_edge->callee);
2118 /* Declare the 'auto' variables added with this inlined body. */
2119 record_vars (BLOCK_VARS (id->block));
2120 id->block = NULL_TREE;
2121 successfully_inlined = TRUE;
2123 egress:
2124 input_location = saved_location;
2125 return successfully_inlined;
2128 /* Expand call statements reachable from STMT_P.
2129 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2130 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
2131 unfortunately not use that function here because we need a pointer
2132 to the CALL_EXPR, not the tree itself. */
2134 static bool
2135 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
2137 block_stmt_iterator bsi;
2139 /* Register specific tree functions. */
2140 tree_register_cfg_hooks ();
2141 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2143 tree *expr_p = bsi_stmt_ptr (bsi);
2144 tree stmt = *expr_p;
2146 if (TREE_CODE (*expr_p) == MODIFY_EXPR)
2147 expr_p = &TREE_OPERAND (*expr_p, 1);
2148 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2149 expr_p = &TREE_OPERAND (*expr_p, 0);
2150 if (TREE_CODE (*expr_p) == CALL_EXPR)
2151 if (expand_call_inline (bb, stmt, expr_p, id))
2152 return true;
2154 return false;
2157 /* Expand calls to inline functions in the body of FN. */
2159 void
2160 optimize_inline_calls (tree fn)
2162 copy_body_data id;
2163 tree prev_fn;
2164 basic_block bb;
2165 /* There is no point in performing inlining if errors have already
2166 occurred -- and we might crash if we try to inline invalid
2167 code. */
2168 if (errorcount || sorrycount)
2169 return;
2171 /* Clear out ID. */
2172 memset (&id, 0, sizeof (id));
2174 id.src_node = id.dst_node = cgraph_node (fn);
2175 id.dst_fn = fn;
2176 /* Or any functions that aren't finished yet. */
2177 prev_fn = NULL_TREE;
2178 if (current_function_decl)
2180 id.dst_fn = current_function_decl;
2181 prev_fn = current_function_decl;
2184 id.copy_decl = copy_decl_maybe_to_var;
2185 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2186 id.transform_new_cfg = false;
2187 id.transform_return_to_modify = true;
2188 id.transform_lang_insert_block = false;
2190 push_gimplify_context ();
2192 /* Reach the trees by walking over the CFG, and note the
2193 enclosing basic-blocks in the call edges. */
2194 /* We walk the blocks going forward, because inlined function bodies
2195 will split id->current_basic_block, and the new blocks will
2196 follow it; we'll trudge through them, processing their CALL_EXPRs
2197 along the way. */
2198 FOR_EACH_BB (bb)
2199 gimple_expand_calls_inline (bb, &id);
2201 pop_gimplify_context (NULL);
2202 /* Renumber the (code) basic_blocks consecutively. */
2203 compact_blocks ();
2204 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2205 number_blocks (fn);
2207 #ifdef ENABLE_CHECKING
2209 struct cgraph_edge *e;
2211 verify_cgraph_node (id.dst_node);
2213 /* Double check that we inlined everything we are supposed to inline. */
2214 for (e = id.dst_node->callees; e; e = e->next_callee)
2215 gcc_assert (e->inline_failed);
2217 #endif
2218 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2219 as inlining loops might increase the maximum. */
2220 if (ENTRY_BLOCK_PTR->count)
2221 counts_to_freqs ();
2222 fold_cond_expr_cond ();
2225 /* FN is a function that has a complete body, and CLONE is a function whose
2226 body is to be set to a copy of FN, mapping argument declarations according
2227 to the ARG_MAP splay_tree. */
2229 void
2230 clone_body (tree clone, tree fn, void *arg_map)
2232 copy_body_data id;
2234 /* Clone the body, as if we were making an inline call. But, remap the
2235 parameters in the callee to the parameters of caller. */
2236 memset (&id, 0, sizeof (id));
2237 id.src_fn = fn;
2238 id.dst_fn = clone;
2239 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
2240 id.decl_map = (splay_tree)arg_map;
2242 id.copy_decl = copy_decl_no_change;
2243 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2244 id.transform_new_cfg = true;
2245 id.transform_return_to_modify = false;
2246 id.transform_lang_insert_block = true;
2248 /* We're not inside any EH region. */
2249 id.eh_region = -1;
2251 /* Actually copy the body. */
2252 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2255 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2257 tree
2258 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2260 enum tree_code code = TREE_CODE (*tp);
2262 /* We make copies of most nodes. */
2263 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
2264 || code == TREE_LIST
2265 || code == TREE_VEC
2266 || code == TYPE_DECL)
2268 /* Because the chain gets clobbered when we make a copy, we save it
2269 here. */
2270 tree chain = TREE_CHAIN (*tp);
2271 tree new;
2273 /* Copy the node. */
2274 new = copy_node (*tp);
2276 /* Propagate mudflap marked-ness. */
2277 if (flag_mudflap && mf_marked_p (*tp))
2278 mf_mark (new);
2280 *tp = new;
2282 /* Now, restore the chain, if appropriate. That will cause
2283 walk_tree to walk into the chain as well. */
2284 if (code == PARM_DECL || code == TREE_LIST)
2285 TREE_CHAIN (*tp) = chain;
2287 /* For now, we don't update BLOCKs when we make copies. So, we
2288 have to nullify all BIND_EXPRs. */
2289 if (TREE_CODE (*tp) == BIND_EXPR)
2290 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2292 else if (code == CONSTRUCTOR)
2294 /* CONSTRUCTOR nodes need special handling because
2295 we need to duplicate the vector of elements. */
2296 tree new;
2298 new = copy_node (*tp);
2300 /* Propagate mudflap marked-ness. */
2301 if (flag_mudflap && mf_marked_p (*tp))
2302 mf_mark (new);
2304 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2305 CONSTRUCTOR_ELTS (*tp));
2306 *tp = new;
2308 else if (TREE_CODE_CLASS (code) == tcc_type)
2309 *walk_subtrees = 0;
2310 else if (TREE_CODE_CLASS (code) == tcc_declaration)
2311 *walk_subtrees = 0;
2312 else if (TREE_CODE_CLASS (code) == tcc_constant)
2313 *walk_subtrees = 0;
2314 else
2315 gcc_assert (code != STATEMENT_LIST);
2316 return NULL_TREE;
2319 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2320 information indicating to what new SAVE_EXPR this one should be mapped,
2321 use that one. Otherwise, create a new node and enter it in ST. FN is
2322 the function into which the copy will be placed. */
2324 static void
2325 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2327 splay_tree st = (splay_tree) st_;
2328 splay_tree_node n;
2329 tree t;
2331 /* See if we already encountered this SAVE_EXPR. */
2332 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2334 /* If we didn't already remap this SAVE_EXPR, do so now. */
2335 if (!n)
2337 t = copy_node (*tp);
2339 /* Remember this SAVE_EXPR. */
2340 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2341 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2342 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2344 else
2346 /* We've already walked into this SAVE_EXPR; don't do it again. */
2347 *walk_subtrees = 0;
2348 t = (tree) n->value;
2351 /* Replace this SAVE_EXPR with the copy. */
2352 *tp = t;
2355 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2356 copies the declaration and enters it in the splay_tree in DATA (which is
2357 really an `copy_body_data *'). */
2359 static tree
2360 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2361 void *data)
2363 copy_body_data *id = (copy_body_data *) data;
2365 /* Don't walk into types. */
2366 if (TYPE_P (*tp))
2367 *walk_subtrees = 0;
2369 else if (TREE_CODE (*tp) == LABEL_EXPR)
2371 tree decl = TREE_OPERAND (*tp, 0);
2373 /* Copy the decl and remember the copy. */
2374 insert_decl_map (id, decl, id->copy_decl (decl, id));
2377 return NULL_TREE;
2380 /* Perform any modifications to EXPR required when it is unsaved. Does
2381 not recurse into EXPR's subtrees. */
2383 static void
2384 unsave_expr_1 (tree expr)
2386 switch (TREE_CODE (expr))
2388 case TARGET_EXPR:
2389 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2390 It's OK for this to happen if it was part of a subtree that
2391 isn't immediately expanded, such as operand 2 of another
2392 TARGET_EXPR. */
2393 if (TREE_OPERAND (expr, 1))
2394 break;
2396 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2397 TREE_OPERAND (expr, 3) = NULL_TREE;
2398 break;
2400 default:
2401 break;
2405 /* Called via walk_tree when an expression is unsaved. Using the
2406 splay_tree pointed to by ST (which is really a `splay_tree'),
2407 remaps all local declarations to appropriate replacements. */
2409 static tree
2410 unsave_r (tree *tp, int *walk_subtrees, void *data)
2412 copy_body_data *id = (copy_body_data *) data;
2413 splay_tree st = id->decl_map;
2414 splay_tree_node n;
2416 /* Only a local declaration (variable or label). */
2417 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2418 || TREE_CODE (*tp) == LABEL_DECL)
2420 /* Lookup the declaration. */
2421 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2423 /* If it's there, remap it. */
2424 if (n)
2425 *tp = (tree) n->value;
2428 else if (TREE_CODE (*tp) == STATEMENT_LIST)
2429 copy_statement_list (tp);
2430 else if (TREE_CODE (*tp) == BIND_EXPR)
2431 copy_bind_expr (tp, walk_subtrees, id);
2432 else if (TREE_CODE (*tp) == SAVE_EXPR)
2433 remap_save_expr (tp, st, walk_subtrees);
2434 else
2436 copy_tree_r (tp, walk_subtrees, NULL);
2438 /* Do whatever unsaving is required. */
2439 unsave_expr_1 (*tp);
2442 /* Keep iterating. */
2443 return NULL_TREE;
2446 /* Copies everything in EXPR and replaces variables, labels
2447 and SAVE_EXPRs local to EXPR. */
2449 tree
2450 unsave_expr_now (tree expr)
2452 copy_body_data id;
2454 /* There's nothing to do for NULL_TREE. */
2455 if (expr == 0)
2456 return expr;
2458 /* Set up ID. */
2459 memset (&id, 0, sizeof (id));
2460 id.src_fn = current_function_decl;
2461 id.dst_fn = current_function_decl;
2462 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2464 id.copy_decl = copy_decl_no_change;
2465 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2466 id.transform_new_cfg = false;
2467 id.transform_return_to_modify = false;
2468 id.transform_lang_insert_block = false;
2470 /* Walk the tree once to find local labels. */
2471 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2473 /* Walk the tree again, copying, remapping, and unsaving. */
2474 walk_tree (&expr, unsave_r, &id, NULL);
2476 /* Clean up. */
2477 splay_tree_delete (id.decl_map);
2479 return expr;
2482 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
2484 static tree
2485 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2487 if (*tp == data)
2488 return (tree) data;
2489 else
2490 return NULL;
2493 bool
2494 debug_find_tree (tree top, tree search)
2496 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2500 /* Declare the variables created by the inliner. Add all the variables in
2501 VARS to BIND_EXPR. */
2503 static void
2504 declare_inline_vars (tree block, tree vars)
2506 tree t;
2507 for (t = vars; t; t = TREE_CHAIN (t))
2508 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2510 if (block)
2511 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2515 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
2516 but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
2517 VAR_DECL translation. */
2519 static tree
2520 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
2522 /* Don't generate debug information for the copy if we wouldn't have
2523 generated it for the copy either. */
2524 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2525 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2527 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
2528 declaration inspired this copy. */
2529 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2531 /* The new variable/label has no RTL, yet. */
2532 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
2533 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
2534 SET_DECL_RTL (copy, NULL_RTX);
2536 /* These args would always appear unused, if not for this. */
2537 TREE_USED (copy) = 1;
2539 /* Set the context for the new declaration. */
2540 if (!DECL_CONTEXT (decl))
2541 /* Globals stay global. */
2543 else if (DECL_CONTEXT (decl) != id->src_fn)
2544 /* Things that weren't in the scope of the function we're inlining
2545 from aren't in the scope we're inlining to, either. */
2547 else if (TREE_STATIC (decl))
2548 /* Function-scoped static variables should stay in the original
2549 function. */
2551 else
2552 /* Ordinary automatic local variables are now in the scope of the
2553 new function. */
2554 DECL_CONTEXT (copy) = id->dst_fn;
2556 return copy;
2559 static tree
2560 copy_decl_to_var (tree decl, copy_body_data *id)
2562 tree copy, type;
2564 gcc_assert (TREE_CODE (decl) == PARM_DECL
2565 || TREE_CODE (decl) == RESULT_DECL);
2567 type = TREE_TYPE (decl);
2569 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
2570 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2571 TREE_READONLY (copy) = TREE_READONLY (decl);
2572 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2573 DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
2575 return copy_decl_for_dup_finish (id, decl, copy);
2578 static tree
2579 copy_decl_no_change (tree decl, copy_body_data *id)
2581 tree copy;
2583 copy = copy_node (decl);
2585 /* The COPY is not abstract; it will be generated in DST_FN. */
2586 DECL_ABSTRACT (copy) = 0;
2587 lang_hooks.dup_lang_specific_decl (copy);
2589 /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
2590 been taken; it's for internal bookkeeping in expand_goto_internal. */
2591 if (TREE_CODE (copy) == LABEL_DECL)
2593 TREE_ADDRESSABLE (copy) = 0;
2594 LABEL_DECL_UID (copy) = -1;
2597 return copy_decl_for_dup_finish (id, decl, copy);
2600 static tree
2601 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
2603 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
2604 return copy_decl_to_var (decl, id);
2605 else
2606 return copy_decl_no_change (decl, id);
2609 /* Return a copy of the function's argument tree. */
2610 static tree
2611 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
2613 tree *arg_copy, *parg;
2615 arg_copy = &orig_parm;
2616 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2618 tree new = remap_decl (*parg, id);
2619 lang_hooks.dup_lang_specific_decl (new);
2620 TREE_CHAIN (new) = TREE_CHAIN (*parg);
2621 *parg = new;
2623 return orig_parm;
2626 /* Return a copy of the function's static chain. */
2627 static tree
2628 copy_static_chain (tree static_chain, copy_body_data * id)
2630 tree *chain_copy, *pvar;
2632 chain_copy = &static_chain;
2633 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
2635 tree new = remap_decl (*pvar, id);
2636 lang_hooks.dup_lang_specific_decl (new);
2637 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
2638 *pvar = new;
2640 return static_chain;
2643 /* Return true if the function is allowed to be versioned.
2644 This is a guard for the versioning functionality. */
2645 bool
2646 tree_versionable_function_p (tree fndecl)
2648 if (fndecl == NULL_TREE)
2649 return false;
2650 /* ??? There are cases where a function is
2651 uninlinable but can be versioned. */
2652 if (!tree_inlinable_function_p (fndecl))
2653 return false;
2655 return true;
2658 /* Create a copy of a function's tree.
2659 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
2660 of the original function and the new copied function
2661 respectively. In case we want to replace a DECL
2662 tree with another tree while duplicating the function's
2663 body, TREE_MAP represents the mapping between these
2664 trees. If UPDATE_CLONES is set, the call_stmt fields
2665 of edges of clones of the function will be updated. */
2666 void
2667 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
2668 bool update_clones)
2670 struct cgraph_node *old_version_node;
2671 struct cgraph_node *new_version_node;
2672 copy_body_data id;
2673 tree p, new_fndecl;
2674 unsigned i;
2675 struct ipa_replace_map *replace_info;
2676 basic_block old_entry_block;
2677 tree t_step;
2679 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
2680 && TREE_CODE (new_decl) == FUNCTION_DECL);
2681 DECL_POSSIBLY_INLINED (old_decl) = 1;
2683 old_version_node = cgraph_node (old_decl);
2684 new_version_node = cgraph_node (new_decl);
2686 allocate_struct_function (new_decl);
2687 /* Cfun points to the new allocated function struct at this point. */
2688 cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
2690 DECL_ARTIFICIAL (new_decl) = 1;
2691 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
2693 /* Generate a new name for the new version. */
2694 if (!update_clones)
2695 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
2696 /* Create a new SYMBOL_REF rtx for the new name. */
2697 if (DECL_RTL (old_decl) != NULL)
2699 SET_DECL_RTL (new_decl, copy_rtx (DECL_RTL (old_decl)));
2700 XEXP (DECL_RTL (new_decl), 0) =
2701 gen_rtx_SYMBOL_REF (GET_MODE (XEXP (DECL_RTL (old_decl), 0)),
2702 IDENTIFIER_POINTER (DECL_NAME (new_decl)));
2705 /* Prepare the data structures for the tree copy. */
2706 memset (&id, 0, sizeof (id));
2708 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2709 id.src_fn = old_decl;
2710 id.dst_fn = new_decl;
2711 id.src_node = old_version_node;
2712 id.dst_node = new_version_node;
2713 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
2715 id.copy_decl = copy_decl_no_change;
2716 id.transform_call_graph_edges
2717 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
2718 id.transform_new_cfg = true;
2719 id.transform_return_to_modify = false;
2720 id.transform_lang_insert_block = false;
2722 current_function_decl = new_decl;
2724 /* Copy the function's static chain. */
2725 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
2726 if (p)
2727 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
2728 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
2729 &id);
2730 /* Copy the function's arguments. */
2731 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
2732 DECL_ARGUMENTS (new_decl) =
2733 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
2735 /* If there's a tree_map, prepare for substitution. */
2736 if (tree_map)
2737 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
2739 replace_info = VARRAY_GENERIC_PTR (tree_map, i);
2740 if (replace_info->replace_p)
2741 insert_decl_map (&id, replace_info->old_tree,
2742 replace_info->new_tree);
2745 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
2747 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2748 number_blocks (id.dst_fn);
2750 if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
2751 /* Add local vars. */
2752 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
2753 t_step; t_step = TREE_CHAIN (t_step))
2755 tree var = TREE_VALUE (t_step);
2756 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2757 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2758 cfun->unexpanded_var_list);
2759 else
2760 cfun->unexpanded_var_list =
2761 tree_cons (NULL_TREE, remap_decl (var, &id),
2762 cfun->unexpanded_var_list);
2765 /* Copy the Function's body. */
2766 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
2767 (DECL_STRUCT_FUNCTION (old_decl));
2768 new_fndecl = copy_body (&id,
2769 old_entry_block->count,
2770 old_entry_block->frequency, NULL, NULL);
2772 DECL_SAVED_TREE (new_decl) = DECL_SAVED_TREE (new_fndecl);
2774 DECL_STRUCT_FUNCTION (new_decl)->cfg =
2775 DECL_STRUCT_FUNCTION (new_fndecl)->cfg;
2776 DECL_STRUCT_FUNCTION (new_decl)->eh = DECL_STRUCT_FUNCTION (new_fndecl)->eh;
2777 DECL_STRUCT_FUNCTION (new_decl)->ib_boundaries_block =
2778 DECL_STRUCT_FUNCTION (new_fndecl)->ib_boundaries_block;
2779 DECL_STRUCT_FUNCTION (new_decl)->last_label_uid =
2780 DECL_STRUCT_FUNCTION (new_fndecl)->last_label_uid;
2782 if (DECL_RESULT (old_decl) != NULL_TREE)
2784 tree *res_decl = &DECL_RESULT (old_decl);
2785 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
2786 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
2789 current_function_decl = NULL;
2790 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2791 number_blocks (new_decl);
2793 /* Clean up. */
2794 splay_tree_delete (id.decl_map);
2795 fold_cond_expr_cond ();
2796 return;
2799 /* Duplicate a type, fields and all. */
2801 tree
2802 build_duplicate_type (tree type)
2804 struct copy_body_data id;
2806 memset (&id, 0, sizeof (id));
2807 id.src_fn = current_function_decl;
2808 id.dst_fn = current_function_decl;
2809 id.src_cfun = cfun;
2810 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2812 type = remap_type_1 (type, &id);
2814 splay_tree_delete (id.decl_map);
2816 return type;