Release 0.3
[delight.git] / gcc-4.1.2 / gcc / tree-inline.c
blobe6c8c89539778d840a0936070834946149e0afca
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 "splay-tree.h"
38 #include "langhooks.h"
39 #include "basic-block.h"
40 #include "tree-iterator.h"
41 #include "cgraph.h"
42 #include "intl.h"
43 #include "tree-mudflap.h"
44 #include "tree-flow.h"
45 #include "function.h"
46 #include "ggc.h"
47 #include "tree-flow.h"
48 #include "diagnostic.h"
49 #include "except.h"
50 #include "debug.h"
51 #include "pointer-set.h"
52 #include "ipa-prop.h"
54 /* I'm not real happy about this, but we need to handle gimple and
55 non-gimple trees. */
56 #include "tree-gimple.h"
58 /* Inlining, Saving, Cloning
60 Inlining: a function body is duplicated, but the PARM_DECLs are
61 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
62 MODIFY_EXPRs that store to a dedicated returned-value variable.
63 The duplicated eh_region info of the copy will later be appended
64 to the info for the caller; the eh_region info in copied throwing
65 statements and RESX_EXPRs is adjusted accordingly.
67 Saving: make a semantically-identical copy of the function body.
68 Necessary when we want to generate code for the body (a destructive
69 operation), but we expect to need this body in the future (e.g. for
70 inlining into another function).
72 Cloning: (only in C++) We have one body for a con/de/structor, and
73 multiple function decls, each with a unique parameter list.
74 Duplicate the body, using the given splay tree; some parameters
75 will become constants (like 0 or 1).
77 All of these will simultaneously lookup any callgraph edges. If
78 we're going to inline the duplicated function body, and the given
79 function has some cloned callgraph nodes (one for each place this
80 function will be inlined) those callgraph edges will be duplicated.
81 If we're saving or cloning the body, those callgraph edges will be
82 updated to point into the new body. (Note that the original
83 callgraph node and edge list will not be altered.)
85 See the CALL_EXPR handling case in copy_body_r (). */
87 /* 0 if we should not perform inlining.
88 1 if we should expand functions calls inline at the tree level.
89 2 if we should consider *all* functions to be inline
90 candidates. */
92 int flag_inline_trees = 0;
94 /* To Do:
96 o In order to make inlining-on-trees work, we pessimized
97 function-local static constants. In particular, they are now
98 always output, even when not addressed. Fix this by treating
99 function-local static constants just like global static
100 constants; the back-end already knows not to output them if they
101 are not needed.
103 o Provide heuristics to clamp inlining of recursive template
104 calls? */
106 /* Data required for function inlining. */
108 typedef struct inline_data
110 /* FUNCTION_DECL for function being inlined. */
111 tree callee;
112 /* FUNCTION_DECL for function being inlined into. */
113 tree caller;
114 /* struct function for function being inlined. Usually this is the same
115 as DECL_STRUCT_FUNCTION (callee), but can be different if saved_cfg
116 and saved_eh are in use. */
117 struct function *callee_cfun;
118 /* The VAR_DECL for the return value. */
119 tree retvar;
120 /* The map from local declarations in the inlined function to
121 equivalents in the function into which it is being inlined. */
122 splay_tree decl_map;
123 /* We use the same mechanism to build clones that we do to perform
124 inlining. However, there are a few places where we need to
125 distinguish between those two situations. This flag is true if
126 we are cloning, rather than inlining. */
127 bool cloning_p;
128 /* Similarly for saving function body. */
129 bool saving_p;
130 /* Versioning function is slightly different from inlining. */
131 bool versioning_p;
132 /* Callgraph node of function we are inlining into. */
133 struct cgraph_node *node;
134 /* Callgraph node of currently inlined function. */
135 struct cgraph_node *current_node;
136 /* Current BLOCK. */
137 tree block;
138 varray_type ipa_info;
139 /* Exception region the inlined call lie in. */
140 int eh_region;
141 /* Take region number in the function being copied, add this value and
142 get eh region number of the duplicate in the function we inline into. */
143 int eh_region_offset;
144 } inline_data;
146 /* Prototypes. */
148 static tree declare_return_variable (inline_data *, tree, tree, tree *);
149 static tree copy_body_r (tree *, int *, void *);
150 static tree copy_generic_body (inline_data *);
151 static bool inlinable_function_p (tree);
152 static tree remap_decl (tree, inline_data *);
153 static tree remap_type (tree, inline_data *);
154 static void remap_block (tree *, inline_data *);
155 static tree remap_decl (tree, inline_data *);
156 static tree remap_decls (tree, inline_data *);
157 static void copy_bind_expr (tree *, int *, inline_data *);
158 static tree mark_local_for_remap_r (tree *, int *, void *);
159 static void unsave_expr_1 (tree);
160 static tree unsave_r (tree *, int *, void *);
161 static void declare_inline_vars (tree, tree);
162 static void remap_save_expr (tree *, void *, int *);
163 static bool replace_ref_tree (inline_data *, tree *);
164 static inline bool inlining_p (inline_data *);
165 static void add_lexical_block (tree current_block, tree new_block);
167 /* Insert a tree->tree mapping for ID. Despite the name suggests
168 that the trees should be variables, it is used for more than that. */
170 static void
171 insert_decl_map (inline_data *id, tree key, tree value)
173 splay_tree_insert (id->decl_map, (splay_tree_key) key,
174 (splay_tree_value) value);
176 /* Always insert an identity map as well. If we see this same new
177 node again, we won't want to duplicate it a second time. */
178 if (key != value)
179 splay_tree_insert (id->decl_map, (splay_tree_key) value,
180 (splay_tree_value) value);
183 /* Remap DECL during the copying of the BLOCK tree for the function. */
185 static tree
186 remap_decl (tree decl, inline_data *id)
188 splay_tree_node n;
189 tree fn;
191 /* We only remap local variables in the current function. */
192 fn = id->callee;
194 /* See if we have remapped this declaration. */
196 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
198 /* If we didn't already have an equivalent for this declaration,
199 create one now. */
200 if (!n)
202 /* Make a copy of the variable or label. */
203 tree t;
204 t = copy_decl_for_dup (decl, fn, id->caller, id->versioning_p);
206 /* Remember it, so that if we encounter this local entity again
207 we can reuse this copy. Do this early because remap_type may
208 need this decl for TYPE_STUB_DECL. */
209 insert_decl_map (id, decl, t);
211 /* Remap types, if necessary. */
212 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
213 if (TREE_CODE (t) == TYPE_DECL)
214 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
216 /* Remap sizes as necessary. */
217 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
218 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
220 /* If fields, do likewise for offset and qualifier. */
221 if (TREE_CODE (t) == FIELD_DECL)
223 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
224 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
225 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
228 #if 0
229 /* FIXME handle anon aggrs. */
230 if (! DECL_NAME (t) && TREE_TYPE (t)
231 && lang_hooks.tree_inlining.anon_aggr_type_p (TREE_TYPE (t)))
233 /* For a VAR_DECL of anonymous type, we must also copy the
234 member VAR_DECLS here and rechain the DECL_ANON_UNION_ELEMS. */
235 tree members = NULL;
236 tree src;
238 for (src = DECL_ANON_UNION_ELEMS (t); src;
239 src = TREE_CHAIN (src))
241 tree member = remap_decl (TREE_VALUE (src), id);
243 gcc_assert (!TREE_PURPOSE (src));
244 members = tree_cons (NULL, member, members);
246 DECL_ANON_UNION_ELEMS (t) = nreverse (members);
248 #endif
250 /* Remember it, so that if we encounter this local entity
251 again we can reuse this copy. */
252 insert_decl_map (id, decl, t);
253 return t;
256 return unshare_expr ((tree) n->value);
259 static tree
260 remap_type_1 (tree type, inline_data *id)
262 tree new, t;
264 /* We do need a copy. build and register it now. If this is a pointer or
265 reference type, remap the designated type and make a new pointer or
266 reference type. */
267 if (TREE_CODE (type) == POINTER_TYPE)
269 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
270 TYPE_MODE (type),
271 TYPE_REF_CAN_ALIAS_ALL (type));
272 insert_decl_map (id, type, new);
273 return new;
275 else if (TREE_CODE (type) == REFERENCE_TYPE)
277 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
278 TYPE_MODE (type),
279 TYPE_REF_CAN_ALIAS_ALL (type));
280 insert_decl_map (id, type, new);
281 return new;
283 else
284 new = copy_node (type);
286 insert_decl_map (id, type, new);
288 /* This is a new type, not a copy of an old type. Need to reassociate
289 variants. We can handle everything except the main variant lazily. */
290 t = TYPE_MAIN_VARIANT (type);
291 if (type != t)
293 t = remap_type (t, id);
294 TYPE_MAIN_VARIANT (new) = t;
295 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
296 TYPE_NEXT_VARIANT (t) = new;
298 else
300 TYPE_MAIN_VARIANT (new) = new;
301 TYPE_NEXT_VARIANT (new) = NULL;
304 if (TYPE_STUB_DECL (type))
305 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
307 /* Lazily create pointer and reference types. */
308 TYPE_POINTER_TO (new) = NULL;
309 TYPE_REFERENCE_TO (new) = NULL;
311 switch (TREE_CODE (new))
313 case INTEGER_TYPE:
314 case REAL_TYPE:
315 case ENUMERAL_TYPE:
316 case BOOLEAN_TYPE:
317 case CHAR_TYPE:
318 t = TYPE_MIN_VALUE (new);
319 if (t && TREE_CODE (t) != INTEGER_CST)
320 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
322 t = TYPE_MAX_VALUE (new);
323 if (t && TREE_CODE (t) != INTEGER_CST)
324 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
325 return new;
327 case FUNCTION_TYPE:
328 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
329 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
330 return new;
332 case ARRAY_TYPE:
333 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
334 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
335 break;
337 case RECORD_TYPE:
338 case UNION_TYPE:
339 case QUAL_UNION_TYPE:
341 tree f, nf = NULL;
343 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
345 t = remap_decl (f, id);
346 DECL_CONTEXT (t) = new;
347 TREE_CHAIN (t) = nf;
348 nf = t;
350 TYPE_FIELDS (new) = nreverse (nf);
352 break;
354 case OFFSET_TYPE:
355 default:
356 /* Shouldn't have been thought variable sized. */
357 gcc_unreachable ();
360 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
361 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
363 return new;
366 static tree
367 remap_type (tree type, inline_data *id)
369 splay_tree_node node;
371 if (type == NULL)
372 return type;
374 /* See if we have remapped this type. */
375 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
376 if (node)
377 return (tree) node->value;
379 /* The type only needs remapping if it's variably modified. */
380 if (! variably_modified_type_p (type, id->callee))
382 insert_decl_map (id, type, type);
383 return type;
386 return remap_type_1 (type, id);
389 static tree
390 remap_decls (tree decls, inline_data *id)
392 tree old_var;
393 tree new_decls = NULL_TREE;
395 /* Remap its variables. */
396 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
398 tree new_var;
400 /* We can not chain the local static declarations into the unexpanded_var_list
401 as we can't duplicate them or break one decl rule. Go ahead and link
402 them into unexpanded_var_list. */
403 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->callee)
404 && !DECL_EXTERNAL (old_var))
406 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
407 cfun->unexpanded_var_list);
408 continue;
411 /* Remap the variable. */
412 new_var = remap_decl (old_var, id);
414 /* If we didn't remap this variable, so we can't mess with its
415 TREE_CHAIN. If we remapped this variable to the return slot, it's
416 already declared somewhere else, so don't declare it here. */
417 if (!new_var || new_var == id->retvar)
419 else
421 gcc_assert (DECL_P (new_var));
422 TREE_CHAIN (new_var) = new_decls;
423 new_decls = new_var;
427 return nreverse (new_decls);
430 /* Copy the BLOCK to contain remapped versions of the variables
431 therein. And hook the new block into the block-tree. */
433 static void
434 remap_block (tree *block, inline_data *id)
436 tree old_block;
437 tree new_block;
438 tree fn;
440 /* Make the new block. */
441 old_block = *block;
442 new_block = make_node (BLOCK);
443 TREE_USED (new_block) = TREE_USED (old_block);
444 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
445 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
446 *block = new_block;
448 /* Remap its variables. */
449 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
451 fn = id->caller;
452 if (id->cloning_p)
453 /* We're building a clone; DECL_INITIAL is still
454 error_mark_node, and current_binding_level is the parm
455 binding level. */
456 lang_hooks.decls.insert_block (new_block);
457 /* Remember the remapped block. */
458 insert_decl_map (id, old_block, new_block);
461 /* Copy the whole block tree and root it in id->block. */
462 static tree
463 remap_blocks (tree block, inline_data *id)
465 tree t;
466 tree new = block;
468 if (!block)
469 return NULL;
471 remap_block (&new, id);
472 gcc_assert (new != block);
473 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
474 add_lexical_block (new, remap_blocks (t, id));
475 return new;
478 static void
479 copy_statement_list (tree *tp)
481 tree_stmt_iterator oi, ni;
482 tree new;
484 new = alloc_stmt_list ();
485 ni = tsi_start (new);
486 oi = tsi_start (*tp);
487 *tp = new;
489 for (; !tsi_end_p (oi); tsi_next (&oi))
490 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
493 static void
494 copy_bind_expr (tree *tp, int *walk_subtrees, inline_data *id)
496 tree block = BIND_EXPR_BLOCK (*tp);
497 /* Copy (and replace) the statement. */
498 copy_tree_r (tp, walk_subtrees, NULL);
499 if (block)
501 remap_block (&block, id);
502 BIND_EXPR_BLOCK (*tp) = block;
505 if (BIND_EXPR_VARS (*tp))
506 /* This will remap a lot of the same decls again, but this should be
507 harmless. */
508 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
511 /* Called from copy_body_id via walk_tree. DATA is really an
512 `inline_data *'. */
514 static tree
515 copy_body_r (tree *tp, int *walk_subtrees, void *data)
517 inline_data *id = (inline_data *) data;
518 tree fn = id->callee;
519 tree new_block;
521 /* Begin by recognizing trees that we'll completely rewrite for the
522 inlining context. Our output for these trees is completely
523 different from out input (e.g. RETURN_EXPR is deleted, and morphs
524 into an edge). Further down, we'll handle trees that get
525 duplicated and/or tweaked. */
527 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
528 GOTO_STMT with the RET_LABEL as its target. */
529 if (TREE_CODE (*tp) == RETURN_EXPR && inlining_p (id))
531 tree assignment = TREE_OPERAND (*tp, 0);
533 /* If we're returning something, just turn that into an
534 assignment into the equivalent of the original RESULT_DECL.
535 If the "assignment" is just the result decl, the result
536 decl has already been set (e.g. a recent "foo (&result_decl,
537 ...)"); just toss the entire RETURN_EXPR. */
538 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
540 /* Replace the RETURN_EXPR with (a copy of) the
541 MODIFY_EXPR hanging underneath. */
542 *tp = copy_node (assignment);
544 else /* Else the RETURN_EXPR returns no value. */
546 *tp = NULL;
547 return (void *)1;
551 /* Local variables and labels need to be replaced by equivalent
552 variables. We don't want to copy static variables; there's only
553 one of those, no matter how many times we inline the containing
554 function. Similarly for globals from an outer function. */
555 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
557 tree new_decl;
559 /* Remap the declaration. */
560 new_decl = remap_decl (*tp, id);
561 gcc_assert (new_decl);
562 /* Replace this variable with the copy. */
563 STRIP_TYPE_NOPS (new_decl);
564 *tp = new_decl;
565 *walk_subtrees = 0;
567 else if (TREE_CODE (*tp) == STATEMENT_LIST)
568 copy_statement_list (tp);
569 else if (TREE_CODE (*tp) == SAVE_EXPR)
570 remap_save_expr (tp, id->decl_map, walk_subtrees);
571 else if (TREE_CODE (*tp) == LABEL_DECL
572 && (! DECL_CONTEXT (*tp)
573 || decl_function_context (*tp) == id->callee))
574 /* These may need to be remapped for EH handling. */
575 *tp = remap_decl (*tp, id);
576 else if (TREE_CODE (*tp) == BIND_EXPR)
577 copy_bind_expr (tp, walk_subtrees, id);
578 /* Types may need remapping as well. */
579 else if (TYPE_P (*tp))
580 *tp = remap_type (*tp, id);
582 /* If this is a constant, we have to copy the node iff the type will be
583 remapped. copy_tree_r will not copy a constant. */
584 else if (CONSTANT_CLASS_P (*tp))
586 tree new_type = remap_type (TREE_TYPE (*tp), id);
588 if (new_type == TREE_TYPE (*tp))
589 *walk_subtrees = 0;
591 else if (TREE_CODE (*tp) == INTEGER_CST)
592 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
593 TREE_INT_CST_HIGH (*tp));
594 else
596 *tp = copy_node (*tp);
597 TREE_TYPE (*tp) = new_type;
601 /* Otherwise, just copy the node. Note that copy_tree_r already
602 knows not to copy VAR_DECLs, etc., so this is safe. */
603 else
605 if (! id->cloning_p && ! id->saving_p &&
606 TREE_CODE (*tp) == MODIFY_EXPR &&
607 TREE_OPERAND (*tp, 0) ==
608 DECL_STRUCT_FUNCTION (fn)->static_chain_decl)
610 /* Don't use special methods to initialize the static chain
611 if expanding inline. If this code could somehow be
612 expanded in expand_start_function, it would not be
613 necessary to deal with it here. */
614 *tp = build_empty_stmt ();
616 /* Here we handle trees that are not completely rewritten.
617 First we detect some inlining-induced bogosities for
618 discarding. */
619 else if (TREE_CODE (*tp) == MODIFY_EXPR
620 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
621 && (lang_hooks.tree_inlining.auto_var_in_fn_p
622 (TREE_OPERAND (*tp, 0), fn)))
624 /* Some assignments VAR = VAR; don't generate any rtl code
625 and thus don't count as variable modification. Avoid
626 keeping bogosities like 0 = 0. */
627 tree decl = TREE_OPERAND (*tp, 0), value;
628 splay_tree_node n;
630 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
631 if (n)
633 value = (tree) n->value;
634 STRIP_TYPE_NOPS (value);
635 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
637 *tp = build_empty_stmt ();
638 return copy_body_r (tp, walk_subtrees, data);
642 else if (TREE_CODE (*tp) == INDIRECT_REF
643 && !id->versioning_p)
645 /* Get rid of *& from inline substitutions that can happen when a
646 pointer argument is an ADDR_EXPR. */
647 tree decl = TREE_OPERAND (*tp, 0);
648 splay_tree_node n;
650 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
651 if (n)
653 tree new;
654 tree old;
655 /* If we happen to get an ADDR_EXPR in n->value, strip
656 it manually here as we'll eventually get ADDR_EXPRs
657 which lie about their types pointed to. In this case
658 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
659 but we absolutely rely on that. As fold_indirect_ref
660 does other useful transformations, try that first, though. */
661 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
662 new = unshare_expr ((tree)n->value);
663 old = *tp;
664 *tp = fold_indirect_ref_1 (type, new);
665 if (! *tp)
667 if (TREE_CODE (new) == ADDR_EXPR)
668 *tp = TREE_OPERAND (new, 0);
669 else
671 *tp = build1 (INDIRECT_REF, type, new);
672 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
675 *walk_subtrees = 0;
676 return NULL;
680 /* Here is the "usual case". Copy this tree node, and then
681 tweak some special cases. */
682 copy_tree_r (tp, walk_subtrees, id->versioning_p ? data : NULL);
684 /* If EXPR has block defined, map it to newly constructed block.
685 When inlining we want EXPRs without block appear in the block
686 of function call. */
687 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp))))
689 new_block = id->block;
690 if (TREE_BLOCK (*tp))
692 splay_tree_node n;
693 n = splay_tree_lookup (id->decl_map,
694 (splay_tree_key) TREE_BLOCK (*tp));
695 gcc_assert (n);
696 new_block = (tree) n->value;
698 TREE_BLOCK (*tp) = new_block;
701 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
702 TREE_OPERAND (*tp, 0) =
703 build_int_cst
704 (NULL_TREE,
705 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
707 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
709 /* The copied TARGET_EXPR has never been expanded, even if the
710 original node was expanded already. */
711 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
713 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
714 TREE_OPERAND (*tp, 3) = NULL_TREE;
717 /* Variable substitution need not be simple. In particular, the
718 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
719 and friends are up-to-date. */
720 else if (TREE_CODE (*tp) == ADDR_EXPR)
722 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
723 /* Handle the case where we substituted an INDIRECT_REF
724 into the operand of the ADDR_EXPR. */
725 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
726 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
727 else
729 recompute_tree_invarant_for_addr_expr (*tp);
730 if (DECL_P (TREE_OPERAND (*tp, 0)))
731 TREE_ADDRESSABLE (TREE_OPERAND (*tp, 0)) = 1;
733 *walk_subtrees = 0;
737 /* Keep iterating. */
738 return NULL_TREE;
741 /* Copy basic block, scale profile accordingly. Edges will be taken care of
742 later */
744 static basic_block
745 copy_bb (inline_data *id, basic_block bb, int frequency_scale, int count_scale)
747 block_stmt_iterator bsi, copy_bsi;
748 basic_block copy_basic_block;
750 /* create_basic_block() will append every new block to
751 basic_block_info automatically. */
752 copy_basic_block = create_basic_block (NULL, (void *) 0, bb->prev_bb->aux);
753 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
754 copy_basic_block->frequency = (bb->frequency
755 * frequency_scale / REG_BR_PROB_BASE);
756 copy_bsi = bsi_start (copy_basic_block);
758 for (bsi = bsi_start (bb);
759 !bsi_end_p (bsi); bsi_next (&bsi))
761 tree stmt = bsi_stmt (bsi);
762 tree orig_stmt = stmt;
764 walk_tree (&stmt, copy_body_r, id, NULL);
766 /* RETURN_EXPR might be removed,
767 this is signalled by making stmt pointer NULL. */
768 if (stmt)
770 tree call, decl;
772 /* With return slot optimization we can end up with
773 non-gimple (foo *)&this->m, fix that here. */
774 if (TREE_CODE (stmt) == MODIFY_EXPR
775 && TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
776 && !is_gimple_val (TREE_OPERAND (TREE_OPERAND (stmt, 1), 0)))
777 gimplify_stmt (&stmt);
779 bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
780 call = get_call_expr_in (stmt);
781 /* We're duplicating a CALL_EXPR. Find any corresponding
782 callgraph edges and update or duplicate them. */
783 if (call && (decl = get_callee_fndecl (call)))
785 if (id->saving_p)
787 struct cgraph_node *node;
788 struct cgraph_edge *edge;
790 /* We're saving a copy of the body, so we'll update the
791 callgraph nodes in place. Note that we avoid
792 altering the original callgraph node; we begin with
793 the first clone. */
794 for (node = id->node->next_clone;
795 node;
796 node = node->next_clone)
798 edge = cgraph_edge (node, orig_stmt);
799 gcc_assert (edge);
800 edge->call_stmt = stmt;
803 else
805 struct cgraph_edge *edge;
807 /* We're cloning or inlining this body; duplicate the
808 associate callgraph nodes. */
809 if (!id->versioning_p)
811 edge = cgraph_edge (id->current_node, orig_stmt);
812 if (edge)
813 cgraph_clone_edge (edge, id->node, stmt,
814 REG_BR_PROB_BASE, 1, true);
817 if (id->versioning_p)
819 /* Update the call_expr on the edges from the new version
820 to its callees. */
821 struct cgraph_edge *edge;
822 edge = cgraph_edge (id->node, orig_stmt);
823 if (edge)
824 edge->call_stmt = stmt;
827 /* If you think we can abort here, you are wrong.
828 There is no region 0 in tree land. */
829 gcc_assert (lookup_stmt_eh_region_fn (id->callee_cfun, orig_stmt)
830 != 0);
832 if (tree_could_throw_p (stmt))
834 int region = lookup_stmt_eh_region_fn (id->callee_cfun, orig_stmt);
835 /* Add an entry for the copied tree in the EH hashtable.
836 When saving or cloning or versioning, use the hashtable in
837 cfun, and just copy the EH number. When inlining, use the
838 hashtable in the caller, and adjust the region number. */
839 if (region > 0)
840 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
842 /* If this tree doesn't have a region associated with it,
843 and there is a "current region,"
844 then associate this tree with the current region
845 and add edges associated with this region. */
846 if ((lookup_stmt_eh_region_fn (id->callee_cfun,
847 orig_stmt) <= 0
848 && id->eh_region > 0)
849 && tree_could_throw_p (stmt))
850 add_stmt_to_eh_region (stmt, id->eh_region);
854 return copy_basic_block;
857 /* Copy edges from BB into its copy constructed earlier, scale profile
858 accordingly. Edges will be taken care of later. Assume aux
859 pointers to point to the copies of each BB. */
860 static void
861 copy_edges_for_bb (basic_block bb, int count_scale)
863 basic_block new_bb = bb->aux;
864 edge_iterator ei;
865 edge old_edge;
866 block_stmt_iterator bsi;
867 int flags;
869 /* Use the indices from the original blocks to create edges for the
870 new ones. */
871 FOR_EACH_EDGE (old_edge, ei, bb->succs)
872 if (!(old_edge->flags & EDGE_EH))
874 edge new;
876 flags = old_edge->flags;
878 /* Return edges do get a FALLTHRU flag when the get inlined. */
879 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
880 && old_edge->dest->aux != EXIT_BLOCK_PTR)
881 flags |= EDGE_FALLTHRU;
882 new = make_edge (new_bb, old_edge->dest->aux, flags);
883 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
884 new->probability = old_edge->probability;
887 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
888 return;
890 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
892 tree copy_stmt;
894 copy_stmt = bsi_stmt (bsi);
895 update_stmt (copy_stmt);
896 /* Do this before the possible split_block. */
897 bsi_next (&bsi);
899 /* If this tree could throw an exception, there are two
900 cases where we need to add abnormal edge(s): the
901 tree wasn't in a region and there is a "current
902 region" in the caller; or the original tree had
903 EH edges. In both cases split the block after the tree,
904 and add abnormal edge(s) as needed; we need both
905 those from the callee and the caller.
906 We check whether the copy can throw, because the const
907 propagation can change an INDIRECT_REF which throws
908 into a COMPONENT_REF which doesn't. If the copy
909 can throw, the original could also throw. */
911 if (tree_can_throw_internal (copy_stmt))
913 if (!bsi_end_p (bsi))
914 /* Note that bb's predecessor edges aren't necessarily
915 right at this point; split_block doesn't care. */
917 edge e = split_block (new_bb, copy_stmt);
918 new_bb = e->dest;
919 bsi = bsi_start (new_bb);
922 make_eh_edges (copy_stmt);
927 /* Wrapper for remap_decl so it can be used as a callback. */
928 static tree
929 remap_decl_1 (tree decl, void *data)
931 return remap_decl (decl, data);
934 /* Make a copy of the body of FN so that it can be inserted inline in
935 another function. Walks FN via CFG, returns new fndecl. */
937 static tree
938 copy_cfg_body (inline_data * id, gcov_type count, int frequency,
939 basic_block entry_block_map, basic_block exit_block_map)
941 tree callee_fndecl = id->callee;
942 /* Original cfun for the callee, doesn't change. */
943 struct function *callee_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
944 /* Copy, built by this function. */
945 struct function *new_cfun;
946 /* Place to copy from; when a copy of the function was saved off earlier,
947 use that instead of the main copy. */
948 struct function *cfun_to_copy =
949 (struct function *) ggc_alloc_cleared (sizeof (struct function));
950 basic_block bb;
951 tree new_fndecl = NULL;
952 bool saving_or_cloning;
953 int count_scale, frequency_scale;
955 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count)
956 count_scale = (REG_BR_PROB_BASE * count
957 / ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count);
958 else
959 count_scale = 1;
961 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency)
962 frequency_scale = (REG_BR_PROB_BASE * frequency
964 ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency);
965 else
966 frequency_scale = count_scale;
968 /* Register specific tree functions. */
969 tree_register_cfg_hooks ();
971 /* Must have a CFG here at this point. */
972 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
973 (DECL_STRUCT_FUNCTION (callee_fndecl)));
975 *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
977 /* If there is a saved_cfg+saved_args lurking in the
978 struct function, a copy of the callee body was saved there, and
979 the 'struct cgraph edge' nodes have been fudged to point into the
980 saved body. Accordingly, we want to copy that saved body so the
981 callgraph edges will be recognized and cloned properly. */
982 if (cfun_to_copy->saved_cfg)
984 cfun_to_copy->cfg = cfun_to_copy->saved_cfg;
985 cfun_to_copy->eh = cfun_to_copy->saved_eh;
987 id->callee_cfun = cfun_to_copy;
989 /* If saving or cloning a function body, create new basic_block_info
990 and label_to_block_maps. Otherwise, we're duplicating a function
991 body for inlining; insert our new blocks and labels into the
992 existing varrays. */
993 saving_or_cloning = (id->saving_p || id->cloning_p || id->versioning_p);
994 if (saving_or_cloning)
996 new_cfun =
997 (struct function *) ggc_alloc_cleared (sizeof (struct function));
998 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
999 new_cfun->cfg = NULL;
1000 new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
1001 new_cfun->ib_boundaries_block = (varray_type) 0;
1002 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
1003 push_cfun (new_cfun);
1004 init_empty_tree_cfg ();
1006 ENTRY_BLOCK_PTR->count =
1007 (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count * count_scale /
1008 REG_BR_PROB_BASE);
1009 ENTRY_BLOCK_PTR->frequency =
1010 (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency *
1011 frequency_scale / REG_BR_PROB_BASE);
1012 EXIT_BLOCK_PTR->count =
1013 (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count * count_scale /
1014 REG_BR_PROB_BASE);
1015 EXIT_BLOCK_PTR->frequency =
1016 (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency *
1017 frequency_scale / REG_BR_PROB_BASE);
1019 entry_block_map = ENTRY_BLOCK_PTR;
1020 exit_block_map = EXIT_BLOCK_PTR;
1023 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1024 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1027 /* Duplicate any exception-handling regions. */
1028 if (cfun->eh)
1030 if (saving_or_cloning)
1031 init_eh_for_function ();
1032 id->eh_region_offset = duplicate_eh_regions (cfun_to_copy,
1033 remap_decl_1,
1034 id, id->eh_region);
1035 gcc_assert (inlining_p (id) || !id->eh_region_offset);
1037 /* Use aux pointers to map the original blocks to copy. */
1038 FOR_EACH_BB_FN (bb, cfun_to_copy)
1039 bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
1040 /* Now that we've duplicated the blocks, duplicate their edges. */
1041 FOR_ALL_BB_FN (bb, cfun_to_copy)
1042 copy_edges_for_bb (bb, count_scale);
1043 FOR_ALL_BB_FN (bb, cfun_to_copy)
1044 bb->aux = NULL;
1046 if (saving_or_cloning)
1047 pop_cfun ();
1049 return new_fndecl;
1052 /* Make a copy of the body of FN so that it can be inserted inline in
1053 another function. */
1055 static tree
1056 copy_generic_body (inline_data *id)
1058 tree body;
1059 tree fndecl = id->callee;
1061 body = DECL_SAVED_TREE (fndecl);
1062 walk_tree (&body, copy_body_r, id, NULL);
1064 return body;
1067 static tree
1068 copy_body (inline_data *id, gcov_type count, int frequency,
1069 basic_block entry_block_map, basic_block exit_block_map)
1071 tree fndecl = id->callee;
1072 tree body;
1074 /* If this body has a CFG, walk CFG and copy. */
1075 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1076 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1078 return body;
1081 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1082 defined in function FN, or of a data member thereof. */
1084 static bool
1085 self_inlining_addr_expr (tree value, tree fn)
1087 tree var;
1089 if (TREE_CODE (value) != ADDR_EXPR)
1090 return false;
1092 var = get_base_address (TREE_OPERAND (value, 0));
1094 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
1097 static void
1098 setup_one_parameter (inline_data *id, tree p, tree value, tree fn,
1099 basic_block bb, tree *vars)
1101 tree init_stmt;
1102 tree var;
1103 tree var_sub;
1105 /* If the parameter is never assigned to, we may not need to
1106 create a new variable here at all. Instead, we may be able
1107 to just use the argument value. */
1108 if (TREE_READONLY (p)
1109 && !TREE_ADDRESSABLE (p)
1110 && value && !TREE_SIDE_EFFECTS (value))
1112 /* We may produce non-gimple trees by adding NOPs or introduce
1113 invalid sharing when operand is not really constant.
1114 It is not big deal to prohibit constant propagation here as
1115 we will constant propagate in DOM1 pass anyway. */
1116 if (is_gimple_min_invariant (value)
1117 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1118 /* We have to be very careful about ADDR_EXPR. Make sure
1119 the base variable isn't a local variable of the inlined
1120 function, e.g., when doing recursive inlining, direct or
1121 mutually-recursive or whatever, which is why we don't
1122 just test whether fn == current_function_decl. */
1123 && ! self_inlining_addr_expr (value, fn))
1125 insert_decl_map (id, p, value);
1126 return;
1130 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1131 here since the type of this decl must be visible to the calling
1132 function. */
1133 var = copy_decl_for_dup (p, fn, id->caller, /*versioning=*/false);
1135 /* See if the frontend wants to pass this by invisible reference. If
1136 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1137 replace uses of the PARM_DECL with dereferences. */
1138 if (TREE_TYPE (var) != TREE_TYPE (p)
1139 && POINTER_TYPE_P (TREE_TYPE (var))
1140 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1142 insert_decl_map (id, var, var);
1143 var_sub = build_fold_indirect_ref (var);
1145 else
1146 var_sub = var;
1148 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1149 that way, when the PARM_DECL is encountered, it will be
1150 automatically replaced by the VAR_DECL. */
1151 insert_decl_map (id, p, var_sub);
1153 /* Declare this new variable. */
1154 TREE_CHAIN (var) = *vars;
1155 *vars = var;
1157 /* Make gimplifier happy about this variable. */
1158 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1160 /* Even if P was TREE_READONLY, the new VAR should not be.
1161 In the original code, we would have constructed a
1162 temporary, and then the function body would have never
1163 changed the value of P. However, now, we will be
1164 constructing VAR directly. The constructor body may
1165 change its value multiple times as it is being
1166 constructed. Therefore, it must not be TREE_READONLY;
1167 the back-end assumes that TREE_READONLY variable is
1168 assigned to only once. */
1169 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1170 TREE_READONLY (var) = 0;
1172 /* Initialize this VAR_DECL from the equivalent argument. Convert
1173 the argument to the proper type in case it was promoted. */
1174 if (value)
1176 tree rhs = fold_convert (TREE_TYPE (var), value);
1177 block_stmt_iterator bsi = bsi_last (bb);
1179 if (rhs == error_mark_node)
1180 return;
1182 STRIP_USELESS_TYPE_CONVERSION (rhs);
1184 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1185 keep our trees in gimple form. */
1186 init_stmt = build (MODIFY_EXPR, TREE_TYPE (var), var, rhs);
1188 /* If we did not create a gimple value and we did not create a gimple
1189 cast of a gimple value, then we will need to gimplify INIT_STMTS
1190 at the end. Note that is_gimple_cast only checks the outer
1191 tree code, not its operand. Thus the explicit check that its
1192 operand is a gimple value. */
1193 if (!is_gimple_val (rhs)
1194 && (!is_gimple_cast (rhs)
1195 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1196 gimplify_stmt (&init_stmt);
1198 /* If VAR represents a zero-sized variable, it's possible that the
1199 assignment statment may result in no gimple statements. */
1200 if (init_stmt)
1201 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1205 /* Generate code to initialize the parameters of the function at the
1206 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1208 static void
1209 initialize_inlined_parameters (inline_data *id, tree args, tree static_chain,
1210 tree fn, basic_block bb)
1212 tree parms;
1213 tree a;
1214 tree p;
1215 tree vars = NULL_TREE;
1216 int argnum = 0;
1218 /* Figure out what the parameters are. */
1219 parms = DECL_ARGUMENTS (fn);
1220 if (fn == current_function_decl)
1221 parms = cfun->saved_args;
1223 /* Loop through the parameter declarations, replacing each with an
1224 equivalent VAR_DECL, appropriately initialized. */
1225 for (p = parms, a = args; p;
1226 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1228 tree value;
1230 ++argnum;
1232 /* Find the initializer. */
1233 value = lang_hooks.tree_inlining.convert_parm_for_inlining
1234 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1236 setup_one_parameter (id, p, value, fn, bb, &vars);
1239 /* Initialize the static chain. */
1240 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1241 if (fn == current_function_decl)
1242 p = DECL_STRUCT_FUNCTION (fn)->saved_static_chain_decl;
1243 if (p)
1245 /* No static chain? Seems like a bug in tree-nested.c. */
1246 gcc_assert (static_chain);
1248 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1251 declare_inline_vars (id->block, vars);
1254 /* Declare a return variable to replace the RESULT_DECL for the
1255 function we are calling. An appropriate DECL_STMT is returned.
1256 The USE_STMT is filled to contain a use of the declaration to
1257 indicate the return value of the function.
1259 RETURN_SLOT_ADDR, if non-null, was a fake parameter that
1260 took the address of the result. MODIFY_DEST, if non-null, was the LHS of
1261 the MODIFY_EXPR to which this call is the RHS.
1263 The return value is a (possibly null) value that is the result of the
1264 function as seen by the callee. *USE_P is a (possibly null) value that
1265 holds the result as seen by the caller. */
1267 static tree
1268 declare_return_variable (inline_data *id, tree return_slot_addr,
1269 tree modify_dest, tree *use_p)
1271 tree callee = id->callee;
1272 tree caller = id->caller;
1273 tree result = DECL_RESULT (callee);
1274 tree callee_type = TREE_TYPE (result);
1275 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1276 tree var, use;
1278 /* We don't need to do anything for functions that don't return
1279 anything. */
1280 if (!result || VOID_TYPE_P (callee_type))
1282 *use_p = NULL_TREE;
1283 return NULL_TREE;
1286 /* If there was a return slot, then the return value is the
1287 dereferenced address of that object. */
1288 if (return_slot_addr)
1290 /* The front end shouldn't have used both return_slot_addr and
1291 a modify expression. */
1292 gcc_assert (!modify_dest);
1293 if (DECL_BY_REFERENCE (result))
1294 var = return_slot_addr;
1295 else
1296 var = build_fold_indirect_ref (return_slot_addr);
1297 if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1298 && !DECL_COMPLEX_GIMPLE_REG_P (result)
1299 && DECL_P (var))
1300 DECL_COMPLEX_GIMPLE_REG_P (var) = 0;
1301 use = NULL;
1302 goto done;
1305 /* All types requiring non-trivial constructors should have been handled. */
1306 gcc_assert (!TREE_ADDRESSABLE (callee_type));
1308 /* Attempt to avoid creating a new temporary variable. */
1309 if (modify_dest)
1311 bool use_it = false;
1313 /* We can't use MODIFY_DEST if there's type promotion involved. */
1314 if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1315 use_it = false;
1317 /* ??? If we're assigning to a variable sized type, then we must
1318 reuse the destination variable, because we've no good way to
1319 create variable sized temporaries at this point. */
1320 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1321 use_it = true;
1323 /* If the callee cannot possibly modify MODIFY_DEST, then we can
1324 reuse it as the result of the call directly. Don't do this if
1325 it would promote MODIFY_DEST to addressable. */
1326 else if (TREE_ADDRESSABLE (result))
1327 use_it = false;
1328 else
1330 tree base_m = get_base_address (modify_dest);
1332 /* If the base isn't a decl, then it's a pointer, and we don't
1333 know where that's going to go. */
1334 if (!DECL_P (base_m))
1335 use_it = false;
1336 else if (is_global_var (base_m))
1337 use_it = false;
1338 else if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1339 && !DECL_COMPLEX_GIMPLE_REG_P (result)
1340 && DECL_COMPLEX_GIMPLE_REG_P (base_m))
1341 use_it = false;
1342 else if (!TREE_ADDRESSABLE (base_m))
1343 use_it = true;
1346 if (use_it)
1348 var = modify_dest;
1349 use = NULL;
1350 goto done;
1354 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1356 var = copy_decl_for_dup (result, callee, caller, /*versioning=*/false);
1358 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1359 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1360 = tree_cons (NULL_TREE, var,
1361 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1363 /* Do not have the rest of GCC warn about this variable as it should
1364 not be visible to the user. */
1365 TREE_NO_WARNING (var) = 1;
1367 /* Build the use expr. If the return type of the function was
1368 promoted, convert it back to the expected type. */
1369 use = var;
1370 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1371 use = fold_convert (caller_type, var);
1373 STRIP_USELESS_TYPE_CONVERSION (use);
1375 done:
1376 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1377 way, when the RESULT_DECL is encountered, it will be
1378 automatically replaced by the VAR_DECL. */
1379 insert_decl_map (id, result, var);
1381 /* Remember this so we can ignore it in remap_decls. */
1382 id->retvar = var;
1384 *use_p = use;
1385 return var;
1388 /* Returns nonzero if a function can be inlined as a tree. */
1390 bool
1391 tree_inlinable_function_p (tree fn)
1393 return inlinable_function_p (fn);
1396 static const char *inline_forbidden_reason;
1398 static tree
1399 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1400 void *fnp)
1402 tree node = *nodep;
1403 tree fn = (tree) fnp;
1404 tree t;
1406 switch (TREE_CODE (node))
1408 case CALL_EXPR:
1409 /* Refuse to inline alloca call unless user explicitly forced so as
1410 this may change program's memory overhead drastically when the
1411 function using alloca is called in loop. In GCC present in
1412 SPEC2000 inlining into schedule_block cause it to require 2GB of
1413 RAM instead of 256MB. */
1414 if (alloca_call_p (node)
1415 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1417 inline_forbidden_reason
1418 = G_("function %q+F can never be inlined because it uses "
1419 "alloca (override using the always_inline attribute)");
1420 return node;
1422 t = get_callee_fndecl (node);
1423 if (! t)
1424 break;
1426 /* We cannot inline functions that call setjmp. */
1427 if (setjmp_call_p (t))
1429 inline_forbidden_reason
1430 = G_("function %q+F can never be inlined because it uses setjmp");
1431 return node;
1434 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1435 switch (DECL_FUNCTION_CODE (t))
1437 /* We cannot inline functions that take a variable number of
1438 arguments. */
1439 case BUILT_IN_VA_START:
1440 case BUILT_IN_STDARG_START:
1441 case BUILT_IN_NEXT_ARG:
1442 case BUILT_IN_VA_END:
1443 inline_forbidden_reason
1444 = G_("function %q+F can never be inlined because it "
1445 "uses variable argument lists");
1446 return node;
1448 case BUILT_IN_LONGJMP:
1449 /* We can't inline functions that call __builtin_longjmp at
1450 all. The non-local goto machinery really requires the
1451 destination be in a different function. If we allow the
1452 function calling __builtin_longjmp to be inlined into the
1453 function calling __builtin_setjmp, Things will Go Awry. */
1454 inline_forbidden_reason
1455 = G_("function %q+F can never be inlined because "
1456 "it uses setjmp-longjmp exception handling");
1457 return node;
1459 case BUILT_IN_NONLOCAL_GOTO:
1460 /* Similarly. */
1461 inline_forbidden_reason
1462 = G_("function %q+F can never be inlined because "
1463 "it uses non-local goto");
1464 return node;
1466 case BUILT_IN_RETURN:
1467 case BUILT_IN_APPLY_ARGS:
1468 /* If a __builtin_apply_args caller would be inlined,
1469 it would be saving arguments of the function it has
1470 been inlined into. Similarly __builtin_return would
1471 return from the function the inline has been inlined into. */
1472 inline_forbidden_reason
1473 = G_("function %q+F can never be inlined because "
1474 "it uses __builtin_return or __builtin_apply_args");
1475 return node;
1477 default:
1478 break;
1480 break;
1482 case GOTO_EXPR:
1483 t = TREE_OPERAND (node, 0);
1485 /* We will not inline a function which uses computed goto. The
1486 addresses of its local labels, which may be tucked into
1487 global storage, are of course not constant across
1488 instantiations, which causes unexpected behavior. */
1489 if (TREE_CODE (t) != LABEL_DECL)
1491 inline_forbidden_reason
1492 = G_("function %q+F can never be inlined "
1493 "because it contains a computed goto");
1494 return node;
1496 break;
1498 case LABEL_EXPR:
1499 t = TREE_OPERAND (node, 0);
1500 if (DECL_NONLOCAL (t))
1502 /* We cannot inline a function that receives a non-local goto
1503 because we cannot remap the destination label used in the
1504 function that is performing the non-local goto. */
1505 inline_forbidden_reason
1506 = G_("function %q+F can never be inlined "
1507 "because it receives a non-local goto");
1508 return node;
1510 break;
1512 case RECORD_TYPE:
1513 case UNION_TYPE:
1514 /* We cannot inline a function of the form
1516 void F (int i) { struct S { int ar[i]; } s; }
1518 Attempting to do so produces a catch-22.
1519 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1520 UNION_TYPE nodes, then it goes into infinite recursion on a
1521 structure containing a pointer to its own type. If it doesn't,
1522 then the type node for S doesn't get adjusted properly when
1523 F is inlined.
1525 ??? This is likely no longer true, but it's too late in the 4.0
1526 cycle to try to find out. This should be checked for 4.1. */
1527 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1528 if (variably_modified_type_p (TREE_TYPE (t), NULL))
1530 inline_forbidden_reason
1531 = G_("function %q+F can never be inlined "
1532 "because it uses variable sized variables");
1533 return node;
1536 default:
1537 break;
1540 return NULL_TREE;
1543 /* Return subexpression representing possible alloca call, if any. */
1544 static tree
1545 inline_forbidden_p (tree fndecl)
1547 location_t saved_loc = input_location;
1548 block_stmt_iterator bsi;
1549 basic_block bb;
1550 tree ret = NULL_TREE;
1552 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1553 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1555 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1556 inline_forbidden_p_1, fndecl);
1557 if (ret)
1558 goto egress;
1561 egress:
1562 input_location = saved_loc;
1563 return ret;
1566 /* Returns nonzero if FN is a function that does not have any
1567 fundamental inline blocking properties. */
1569 static bool
1570 inlinable_function_p (tree fn)
1572 bool inlinable = true;
1574 /* If we've already decided this function shouldn't be inlined,
1575 there's no need to check again. */
1576 if (DECL_UNINLINABLE (fn))
1577 return false;
1579 /* See if there is any language-specific reason it cannot be
1580 inlined. (It is important that this hook be called early because
1581 in C++ it may result in template instantiation.)
1582 If the function is not inlinable for language-specific reasons,
1583 it is left up to the langhook to explain why. */
1584 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1586 /* If we don't have the function body available, we can't inline it.
1587 However, this should not be recorded since we also get here for
1588 forward declared inline functions. Therefore, return at once. */
1589 if (!DECL_SAVED_TREE (fn))
1590 return false;
1592 /* If we're not inlining at all, then we cannot inline this function. */
1593 else if (!flag_inline_trees)
1594 inlinable = false;
1596 /* Only try to inline functions if DECL_INLINE is set. This should be
1597 true for all functions declared `inline', and for all other functions
1598 as well with -finline-functions.
1600 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1601 it's the front-end that must set DECL_INLINE in this case, because
1602 dwarf2out loses if a function that does not have DECL_INLINE set is
1603 inlined anyway. That is why we have both DECL_INLINE and
1604 DECL_DECLARED_INLINE_P. */
1605 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1606 here should be redundant. */
1607 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1608 inlinable = false;
1610 else if (inline_forbidden_p (fn))
1612 /* See if we should warn about uninlinable functions. Previously,
1613 some of these warnings would be issued while trying to expand
1614 the function inline, but that would cause multiple warnings
1615 about functions that would for example call alloca. But since
1616 this a property of the function, just one warning is enough.
1617 As a bonus we can now give more details about the reason why a
1618 function is not inlinable.
1619 We only warn for functions declared `inline' by the user. */
1620 bool do_warning = (warn_inline
1621 && DECL_INLINE (fn)
1622 && DECL_DECLARED_INLINE_P (fn)
1623 && !DECL_IN_SYSTEM_HEADER (fn));
1625 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1626 sorry (inline_forbidden_reason, fn);
1627 else if (do_warning)
1628 warning (OPT_Winline, inline_forbidden_reason, fn);
1630 inlinable = false;
1633 /* Squirrel away the result so that we don't have to check again. */
1634 DECL_UNINLINABLE (fn) = !inlinable;
1636 return inlinable;
1639 /* Estimate the cost of a memory move. Use machine dependent
1640 word size and take possible memcpy call into account. */
1643 estimate_move_cost (tree type)
1645 HOST_WIDE_INT size;
1647 size = int_size_in_bytes (type);
1649 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1650 /* Cost of a memcpy call, 3 arguments and the call. */
1651 return 4;
1652 else
1653 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1656 /* Used by estimate_num_insns. Estimate number of instructions seen
1657 by given statement. */
1659 static tree
1660 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1662 int *count = data;
1663 tree x = *tp;
1665 if (IS_TYPE_OR_DECL_P (x))
1667 *walk_subtrees = 0;
1668 return NULL;
1670 /* Assume that constants and references counts nothing. These should
1671 be majorized by amount of operations among them we count later
1672 and are common target of CSE and similar optimizations. */
1673 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1674 return NULL;
1676 switch (TREE_CODE (x))
1678 /* Containers have no cost. */
1679 case TREE_LIST:
1680 case TREE_VEC:
1681 case BLOCK:
1682 case COMPONENT_REF:
1683 case BIT_FIELD_REF:
1684 case INDIRECT_REF:
1685 case ALIGN_INDIRECT_REF:
1686 case MISALIGNED_INDIRECT_REF:
1687 case ARRAY_REF:
1688 case ARRAY_RANGE_REF:
1689 case OBJ_TYPE_REF:
1690 case EXC_PTR_EXPR: /* ??? */
1691 case FILTER_EXPR: /* ??? */
1692 case COMPOUND_EXPR:
1693 case BIND_EXPR:
1694 case WITH_CLEANUP_EXPR:
1695 case NOP_EXPR:
1696 case VIEW_CONVERT_EXPR:
1697 case SAVE_EXPR:
1698 case ADDR_EXPR:
1699 case COMPLEX_EXPR:
1700 case RANGE_EXPR:
1701 case CASE_LABEL_EXPR:
1702 case SSA_NAME:
1703 case CATCH_EXPR:
1704 case EH_FILTER_EXPR:
1705 case STATEMENT_LIST:
1706 case ERROR_MARK:
1707 case NON_LVALUE_EXPR:
1708 case FDESC_EXPR:
1709 case VA_ARG_EXPR:
1710 case TRY_CATCH_EXPR:
1711 case TRY_FINALLY_EXPR:
1712 case LABEL_EXPR:
1713 case GOTO_EXPR:
1714 case RETURN_EXPR:
1715 case EXIT_EXPR:
1716 case LOOP_EXPR:
1717 case PHI_NODE:
1718 case WITH_SIZE_EXPR:
1719 break;
1721 /* We don't account constants for now. Assume that the cost is amortized
1722 by operations that do use them. We may re-consider this decision once
1723 we are able to optimize the tree before estimating its size and break
1724 out static initializers. */
1725 case IDENTIFIER_NODE:
1726 case INTEGER_CST:
1727 case REAL_CST:
1728 case COMPLEX_CST:
1729 case VECTOR_CST:
1730 case STRING_CST:
1731 *walk_subtrees = 0;
1732 return NULL;
1734 /* Try to estimate the cost of assignments. We have three cases to
1735 deal with:
1736 1) Simple assignments to registers;
1737 2) Stores to things that must live in memory. This includes
1738 "normal" stores to scalars, but also assignments of large
1739 structures, or constructors of big arrays;
1740 3) TARGET_EXPRs.
1742 Let us look at the first two cases, assuming we have "a = b + C":
1743 <modify_expr <var_decl "a"> <plus_expr <var_decl "b"> <constant C>>
1744 If "a" is a GIMPLE register, the assignment to it is free on almost
1745 any target, because "a" usually ends up in a real register. Hence
1746 the only cost of this expression comes from the PLUS_EXPR, and we
1747 can ignore the MODIFY_EXPR.
1748 If "a" is not a GIMPLE register, the assignment to "a" will most
1749 likely be a real store, so the cost of the MODIFY_EXPR is the cost
1750 of moving something into "a", which we compute using the function
1751 estimate_move_cost.
1753 The third case deals with TARGET_EXPRs, for which the semantics are
1754 that a temporary is assigned, unless the TARGET_EXPR itself is being
1755 assigned to something else. In the latter case we do not need the
1756 temporary. E.g. in <modify_expr <var_decl "a"> <target_expr>>, the
1757 MODIFY_EXPR is free. */
1758 case INIT_EXPR:
1759 case MODIFY_EXPR:
1760 /* Is the right and side a TARGET_EXPR? */
1761 if (TREE_CODE (TREE_OPERAND (x, 1)) == TARGET_EXPR)
1762 break;
1763 /* ... fall through ... */
1765 case TARGET_EXPR:
1766 x = TREE_OPERAND (x, 0);
1767 /* Is this an assignments to a register? */
1768 if (is_gimple_reg (x))
1769 break;
1770 /* Otherwise it's a store, so fall through to compute the move cost. */
1772 case CONSTRUCTOR:
1773 *count += estimate_move_cost (TREE_TYPE (x));
1774 break;
1776 /* Assign cost of 1 to usual operations.
1777 ??? We may consider mapping RTL costs to this. */
1778 case COND_EXPR:
1779 case VEC_COND_EXPR:
1781 case PLUS_EXPR:
1782 case MINUS_EXPR:
1783 case MULT_EXPR:
1785 case FIX_TRUNC_EXPR:
1786 case FIX_CEIL_EXPR:
1787 case FIX_FLOOR_EXPR:
1788 case FIX_ROUND_EXPR:
1790 case NEGATE_EXPR:
1791 case FLOAT_EXPR:
1792 case MIN_EXPR:
1793 case MAX_EXPR:
1794 case ABS_EXPR:
1796 case LSHIFT_EXPR:
1797 case RSHIFT_EXPR:
1798 case LROTATE_EXPR:
1799 case RROTATE_EXPR:
1800 case VEC_LSHIFT_EXPR:
1801 case VEC_RSHIFT_EXPR:
1803 case BIT_IOR_EXPR:
1804 case BIT_XOR_EXPR:
1805 case BIT_AND_EXPR:
1806 case BIT_NOT_EXPR:
1808 case TRUTH_ANDIF_EXPR:
1809 case TRUTH_ORIF_EXPR:
1810 case TRUTH_AND_EXPR:
1811 case TRUTH_OR_EXPR:
1812 case TRUTH_XOR_EXPR:
1813 case TRUTH_NOT_EXPR:
1815 case LT_EXPR:
1816 case LE_EXPR:
1817 case GT_EXPR:
1818 case GE_EXPR:
1819 case EQ_EXPR:
1820 case NE_EXPR:
1821 case ORDERED_EXPR:
1822 case UNORDERED_EXPR:
1824 case UNLT_EXPR:
1825 case UNLE_EXPR:
1826 case UNGT_EXPR:
1827 case UNGE_EXPR:
1828 case UNEQ_EXPR:
1829 case LTGT_EXPR:
1831 case CONVERT_EXPR:
1833 case CONJ_EXPR:
1835 case PREDECREMENT_EXPR:
1836 case PREINCREMENT_EXPR:
1837 case POSTDECREMENT_EXPR:
1838 case POSTINCREMENT_EXPR:
1840 case SWITCH_EXPR:
1842 case ASM_EXPR:
1844 case REALIGN_LOAD_EXPR:
1846 case REDUC_MAX_EXPR:
1847 case REDUC_MIN_EXPR:
1848 case REDUC_PLUS_EXPR:
1850 case RESX_EXPR:
1851 *count += 1;
1852 break;
1854 /* Few special cases of expensive operations. This is useful
1855 to avoid inlining on functions having too many of these. */
1856 case TRUNC_DIV_EXPR:
1857 case CEIL_DIV_EXPR:
1858 case FLOOR_DIV_EXPR:
1859 case ROUND_DIV_EXPR:
1860 case EXACT_DIV_EXPR:
1861 case TRUNC_MOD_EXPR:
1862 case CEIL_MOD_EXPR:
1863 case FLOOR_MOD_EXPR:
1864 case ROUND_MOD_EXPR:
1865 case RDIV_EXPR:
1866 *count += 10;
1867 break;
1868 case CALL_EXPR:
1870 tree decl = get_callee_fndecl (x);
1871 tree arg;
1873 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
1874 switch (DECL_FUNCTION_CODE (decl))
1876 case BUILT_IN_CONSTANT_P:
1877 *walk_subtrees = 0;
1878 return NULL_TREE;
1879 case BUILT_IN_EXPECT:
1880 return NULL_TREE;
1881 default:
1882 break;
1885 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1886 that does use function declaration to figure out the arguments. */
1887 if (!decl)
1889 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
1890 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
1892 else
1894 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
1895 *count += estimate_move_cost (TREE_TYPE (arg));
1898 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
1899 break;
1901 default:
1902 gcc_unreachable ();
1904 return NULL;
1907 /* Estimate number of instructions that will be created by expanding EXPR. */
1910 estimate_num_insns (tree expr)
1912 int num = 0;
1913 struct pointer_set_t *visited_nodes;
1914 basic_block bb;
1915 block_stmt_iterator bsi;
1916 struct function *my_function;
1918 /* If we're given an entire function, walk the CFG. */
1919 if (TREE_CODE (expr) == FUNCTION_DECL)
1921 my_function = DECL_STRUCT_FUNCTION (expr);
1922 gcc_assert (my_function && my_function->cfg);
1923 visited_nodes = pointer_set_create ();
1924 FOR_EACH_BB_FN (bb, my_function)
1926 for (bsi = bsi_start (bb);
1927 !bsi_end_p (bsi);
1928 bsi_next (&bsi))
1930 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
1931 &num, visited_nodes);
1934 pointer_set_destroy (visited_nodes);
1936 else
1937 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
1939 return num;
1942 typedef struct function *function_p;
1944 DEF_VEC_P(function_p);
1945 DEF_VEC_ALLOC_P(function_p,heap);
1947 /* Initialized with NOGC, making this poisonous to the garbage collector. */
1948 static VEC(function_p,heap) *cfun_stack;
1950 void
1951 push_cfun (struct function *new_cfun)
1953 VEC_safe_push (function_p, heap, cfun_stack, cfun);
1954 cfun = new_cfun;
1957 void
1958 pop_cfun (void)
1960 cfun = VEC_pop (function_p, cfun_stack);
1963 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
1964 static void
1965 add_lexical_block (tree current_block, tree new_block)
1967 tree *blk_p;
1969 /* Walk to the last sub-block. */
1970 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
1971 *blk_p;
1972 blk_p = &TREE_CHAIN (*blk_p))
1974 *blk_p = new_block;
1975 BLOCK_SUPERCONTEXT (new_block) = current_block;
1978 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1980 static bool
1981 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
1983 inline_data *id;
1984 tree t;
1985 tree use_retvar;
1986 tree fn;
1987 splay_tree st;
1988 tree args;
1989 tree return_slot_addr;
1990 tree modify_dest;
1991 location_t saved_location;
1992 struct cgraph_edge *cg_edge;
1993 const char *reason;
1994 basic_block return_block;
1995 edge e;
1996 block_stmt_iterator bsi, stmt_bsi;
1997 bool successfully_inlined = FALSE;
1998 tree t_step;
1999 tree var;
2000 struct cgraph_node *old_node;
2001 tree decl;
2003 /* See what we've got. */
2004 id = (inline_data *) data;
2005 t = *tp;
2007 /* Set input_location here so we get the right instantiation context
2008 if we call instantiate_decl from inlinable_function_p. */
2009 saved_location = input_location;
2010 if (EXPR_HAS_LOCATION (t))
2011 input_location = EXPR_LOCATION (t);
2013 /* From here on, we're only interested in CALL_EXPRs. */
2014 if (TREE_CODE (t) != CALL_EXPR)
2015 goto egress;
2017 /* First, see if we can figure out what function is being called.
2018 If we cannot, then there is no hope of inlining the function. */
2019 fn = get_callee_fndecl (t);
2020 if (!fn)
2021 goto egress;
2023 /* Turn forward declarations into real ones. */
2024 fn = cgraph_node (fn)->decl;
2026 /* If fn is a declaration of a function in a nested scope that was
2027 globally declared inline, we don't set its DECL_INITIAL.
2028 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
2029 C++ front-end uses it for cdtors to refer to their internal
2030 declarations, that are not real functions. Fortunately those
2031 don't have trees to be saved, so we can tell by checking their
2032 DECL_SAVED_TREE. */
2033 if (! DECL_INITIAL (fn)
2034 && DECL_ABSTRACT_ORIGIN (fn)
2035 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
2036 fn = DECL_ABSTRACT_ORIGIN (fn);
2038 /* Objective C and fortran still calls tree_rest_of_compilation directly.
2039 Kill this check once this is fixed. */
2040 if (!id->current_node->analyzed)
2041 goto egress;
2043 cg_edge = cgraph_edge (id->current_node, stmt);
2045 /* Constant propagation on argument done during previous inlining
2046 may create new direct call. Produce an edge for it. */
2047 if (!cg_edge)
2049 struct cgraph_node *dest = cgraph_node (fn);
2051 /* We have missing edge in the callgraph. This can happen in one case
2052 where previous inlining turned indirect call into direct call by
2053 constant propagating arguments. In all other cases we hit a bug
2054 (incorrect node sharing is most common reason for missing edges. */
2055 gcc_assert (dest->needed || !flag_unit_at_a_time);
2056 cgraph_create_edge (id->node, dest, stmt,
2057 bb->count, bb->loop_depth)->inline_failed
2058 = N_("originally indirect function call not considered for inlining");
2059 goto egress;
2062 /* Don't try to inline functions that are not well-suited to
2063 inlining. */
2064 if (!cgraph_inline_p (cg_edge, &reason))
2066 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
2067 /* Avoid warnings during early inline pass. */
2068 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2070 sorry ("inlining failed in call to %q+F: %s", fn, reason);
2071 sorry ("called from here");
2073 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
2074 && !DECL_IN_SYSTEM_HEADER (fn)
2075 && strlen (reason)
2076 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
2077 /* Avoid warnings during early inline pass. */
2078 && (!flag_unit_at_a_time || cgraph_global_info_ready))
2080 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
2081 fn, reason);
2082 warning (OPT_Winline, "called from here");
2084 goto egress;
2087 #ifdef ENABLE_CHECKING
2088 if (cg_edge->callee->decl != id->node->decl)
2089 verify_cgraph_node (cg_edge->callee);
2090 #endif
2092 /* We will be inlining this callee. */
2094 id->eh_region = lookup_stmt_eh_region (stmt);
2096 /* Split the block holding the CALL_EXPR. */
2098 e = split_block (bb, stmt);
2099 bb = e->src;
2100 return_block = e->dest;
2101 remove_edge (e);
2103 /* split_block splits before the statement, work around this by moving
2104 the call into the first half_bb. Not pretty, but seems easier than
2105 doing the CFG manipulation by hand when the CALL_EXPR is in the last
2106 statement in BB. */
2107 stmt_bsi = bsi_last (bb);
2108 bsi = bsi_start (return_block);
2109 if (!bsi_end_p (bsi))
2110 bsi_move_before (&stmt_bsi, &bsi);
2111 else
2113 tree stmt = bsi_stmt (stmt_bsi);
2114 bsi_remove (&stmt_bsi);
2115 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2117 stmt_bsi = bsi_start (return_block);
2119 /* Build a block containing code to initialize the arguments, the
2120 actual inline expansion of the body, and a label for the return
2121 statements within the function to jump to. The type of the
2122 statement expression is the return type of the function call. */
2123 id->block = make_node (BLOCK);
2124 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2125 BLOCK_SOURCE_LOCATION (id->block) = input_location;
2126 add_lexical_block (TREE_BLOCK (stmt), id->block);
2128 /* Local declarations will be replaced by their equivalents in this
2129 map. */
2130 st = id->decl_map;
2131 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2132 NULL, NULL);
2134 /* Initialize the parameters. */
2135 args = TREE_OPERAND (t, 1);
2137 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2139 /* Record the function we are about to inline. */
2140 id->callee = fn;
2142 if (DECL_STRUCT_FUNCTION (fn)->saved_blocks)
2143 add_lexical_block (id->block, remap_blocks (DECL_STRUCT_FUNCTION (fn)->saved_blocks, id));
2144 else if (DECL_INITIAL (fn))
2145 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
2147 /* Return statements in the function body will be replaced by jumps
2148 to the RET_LABEL. */
2150 gcc_assert (DECL_INITIAL (fn));
2151 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2153 /* Find the lhs to which the result of this call is assigned. */
2154 return_slot_addr = NULL;
2155 if (TREE_CODE (stmt) == MODIFY_EXPR)
2157 modify_dest = TREE_OPERAND (stmt, 0);
2159 /* The function which we are inlining might not return a value,
2160 in which case we should issue a warning that the function
2161 does not return a value. In that case the optimizers will
2162 see that the variable to which the value is assigned was not
2163 initialized. We do not want to issue a warning about that
2164 uninitialized variable. */
2165 if (DECL_P (modify_dest))
2166 TREE_NO_WARNING (modify_dest) = 1;
2167 if (CALL_EXPR_RETURN_SLOT_OPT (t))
2169 return_slot_addr = build_fold_addr_expr (modify_dest);
2170 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2171 modify_dest = NULL;
2174 else
2175 modify_dest = NULL;
2177 /* Declare the return variable for the function. */
2178 decl = declare_return_variable (id, return_slot_addr,
2179 modify_dest, &use_retvar);
2180 /* Do this only if declare_return_variable created a new one. */
2181 if (decl && !return_slot_addr && decl != modify_dest)
2182 declare_inline_vars (id->block, decl);
2184 /* After we've initialized the parameters, we insert the body of the
2185 function itself. */
2186 old_node = id->current_node;
2188 /* Anoint the callee-to-be-duplicated as the "current_node." When
2189 CALL_EXPRs within callee are duplicated, the edges from callee to
2190 callee's callees (caller's grandchildren) will be cloned. */
2191 id->current_node = cg_edge->callee;
2193 /* This is it. Duplicate the callee body. Assume callee is
2194 pre-gimplified. Note that we must not alter the caller
2195 function in any way before this point, as this CALL_EXPR may be
2196 a self-referential call; if we're calling ourselves, we need to
2197 duplicate our body before altering anything. */
2198 copy_body (id, bb->count, bb->frequency, bb, return_block);
2199 id->current_node = old_node;
2201 /* Add local vars in this inlined callee to caller. */
2202 t_step = id->callee_cfun->unexpanded_var_list;
2203 if (id->callee_cfun->saved_unexpanded_var_list)
2204 t_step = id->callee_cfun->saved_unexpanded_var_list;
2205 for (; t_step; t_step = TREE_CHAIN (t_step))
2207 var = TREE_VALUE (t_step);
2208 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2209 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2210 cfun->unexpanded_var_list);
2211 else
2212 cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2213 cfun->unexpanded_var_list);
2216 /* Clean up. */
2217 splay_tree_delete (id->decl_map);
2218 id->decl_map = st;
2220 /* If the inlined function returns a result that we care about,
2221 clobber the CALL_EXPR with a reference to the return variable. */
2222 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2224 *tp = use_retvar;
2225 maybe_clean_or_replace_eh_stmt (stmt, stmt);
2227 else
2228 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2229 tsi_delink() will leave the iterator in a sane state. */
2230 bsi_remove (&stmt_bsi);
2232 bsi_next (&bsi);
2233 if (bsi_end_p (bsi))
2234 tree_purge_dead_eh_edges (return_block);
2236 /* If the value of the new expression is ignored, that's OK. We
2237 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2238 the equivalent inlined version either. */
2239 TREE_USED (*tp) = 1;
2241 /* Output the inlining info for this abstract function, since it has been
2242 inlined. If we don't do this now, we can lose the information about the
2243 variables in the function when the blocks get blown away as soon as we
2244 remove the cgraph node. */
2245 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2247 /* Update callgraph if needed. */
2248 cgraph_remove_node (cg_edge->callee);
2250 /* Declare the 'auto' variables added with this inlined body. */
2251 record_vars (BLOCK_VARS (id->block));
2252 id->block = NULL_TREE;
2253 successfully_inlined = TRUE;
2255 egress:
2256 input_location = saved_location;
2257 return successfully_inlined;
2260 /* Expand call statements reachable from STMT_P.
2261 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2262 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
2263 unfortunately not use that function here because we need a pointer
2264 to the CALL_EXPR, not the tree itself. */
2266 static bool
2267 gimple_expand_calls_inline (basic_block bb, inline_data *id)
2269 block_stmt_iterator bsi;
2271 /* Register specific tree functions. */
2272 tree_register_cfg_hooks ();
2273 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2275 tree *expr_p = bsi_stmt_ptr (bsi);
2276 tree stmt = *expr_p;
2278 if (TREE_CODE (*expr_p) == MODIFY_EXPR)
2279 expr_p = &TREE_OPERAND (*expr_p, 1);
2280 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2281 expr_p = &TREE_OPERAND (*expr_p, 0);
2282 if (TREE_CODE (*expr_p) == CALL_EXPR)
2283 if (expand_call_inline (bb, stmt, expr_p, id))
2284 return true;
2286 return false;
2289 /* Expand calls to inline functions in the body of FN. */
2291 void
2292 optimize_inline_calls (tree fn)
2294 inline_data id;
2295 tree prev_fn;
2296 basic_block bb;
2297 /* There is no point in performing inlining if errors have already
2298 occurred -- and we might crash if we try to inline invalid
2299 code. */
2300 if (errorcount || sorrycount)
2301 return;
2303 /* Clear out ID. */
2304 memset (&id, 0, sizeof (id));
2306 id.current_node = id.node = cgraph_node (fn);
2307 id.caller = fn;
2308 /* Or any functions that aren't finished yet. */
2309 prev_fn = NULL_TREE;
2310 if (current_function_decl)
2312 id.caller = current_function_decl;
2313 prev_fn = current_function_decl;
2315 push_gimplify_context ();
2317 /* Reach the trees by walking over the CFG, and note the
2318 enclosing basic-blocks in the call edges. */
2319 /* We walk the blocks going forward, because inlined function bodies
2320 will split id->current_basic_block, and the new blocks will
2321 follow it; we'll trudge through them, processing their CALL_EXPRs
2322 along the way. */
2323 FOR_EACH_BB (bb)
2324 gimple_expand_calls_inline (bb, &id);
2327 pop_gimplify_context (NULL);
2328 /* Renumber the (code) basic_blocks consecutively. */
2329 compact_blocks ();
2330 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2331 number_blocks (fn);
2333 #ifdef ENABLE_CHECKING
2335 struct cgraph_edge *e;
2337 verify_cgraph_node (id.node);
2339 /* Double check that we inlined everything we are supposed to inline. */
2340 for (e = id.node->callees; e; e = e->next_callee)
2341 gcc_assert (e->inline_failed);
2343 #endif
2344 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2345 as inlining loops might increase the maximum. */
2346 if (ENTRY_BLOCK_PTR->count)
2347 counts_to_freqs ();
2348 fold_cond_expr_cond ();
2351 /* FN is a function that has a complete body, and CLONE is a function whose
2352 body is to be set to a copy of FN, mapping argument declarations according
2353 to the ARG_MAP splay_tree. */
2355 void
2356 clone_body (tree clone, tree fn, void *arg_map)
2358 inline_data id;
2360 /* Clone the body, as if we were making an inline call. But, remap the
2361 parameters in the callee to the parameters of caller. */
2362 memset (&id, 0, sizeof (id));
2363 id.caller = clone;
2364 id.callee = fn;
2365 id.callee_cfun = DECL_STRUCT_FUNCTION (fn);
2366 id.decl_map = (splay_tree)arg_map;
2368 /* Cloning is treated slightly differently from inlining. Set
2369 CLONING_P so that it's clear which operation we're performing. */
2370 id.cloning_p = true;
2372 /* We're not inside any EH region. */
2373 id.eh_region = -1;
2375 /* Actually copy the body. */
2376 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2379 /* Save duplicate body in FN. MAP is used to pass around splay tree
2380 used to update arguments in restore_body. */
2382 /* Make and return duplicate of body in FN. Put copies of DECL_ARGUMENTS
2383 in *arg_copy and of the static chain, if any, in *sc_copy. */
2385 void
2386 save_body (tree fn, tree *arg_copy, tree *sc_copy)
2388 inline_data id;
2389 tree newdecl, *parg;
2390 basic_block fn_entry_block;
2391 tree t_step;
2393 memset (&id, 0, sizeof (id));
2394 id.callee = fn;
2395 id.callee_cfun = DECL_STRUCT_FUNCTION (fn);
2396 id.caller = fn;
2397 id.node = cgraph_node (fn);
2398 id.saving_p = true;
2399 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2400 *arg_copy = DECL_ARGUMENTS (fn);
2402 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2404 tree new = copy_node (*parg);
2406 lang_hooks.dup_lang_specific_decl (new);
2407 DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*parg);
2408 insert_decl_map (&id, *parg, new);
2409 TREE_CHAIN (new) = TREE_CHAIN (*parg);
2410 *parg = new;
2413 *sc_copy = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2414 if (*sc_copy)
2416 tree new = copy_node (*sc_copy);
2418 lang_hooks.dup_lang_specific_decl (new);
2419 DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*sc_copy);
2420 insert_decl_map (&id, *sc_copy, new);
2421 TREE_CHAIN (new) = TREE_CHAIN (*sc_copy);
2422 *sc_copy = new;
2425 /* We're not inside any EH region. */
2426 id.eh_region = -1;
2428 insert_decl_map (&id, DECL_RESULT (fn), DECL_RESULT (fn));
2430 DECL_STRUCT_FUNCTION (fn)->saved_blocks
2431 = remap_blocks (DECL_INITIAL (fn), &id);
2432 for (t_step = id.callee_cfun->unexpanded_var_list;
2433 t_step;
2434 t_step = TREE_CHAIN (t_step))
2436 tree var = TREE_VALUE (t_step);
2437 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2438 cfun->saved_unexpanded_var_list
2439 = tree_cons (NULL_TREE, var, cfun->saved_unexpanded_var_list);
2440 else
2441 cfun->saved_unexpanded_var_list
2442 = tree_cons (NULL_TREE, remap_decl (var, &id),
2443 cfun->saved_unexpanded_var_list);
2446 /* Actually copy the body, including a new (struct function *) and CFG.
2447 EH info is also duplicated so its labels point into the copied
2448 CFG, not the original. */
2449 fn_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fn));
2450 newdecl = copy_body (&id, fn_entry_block->count, fn_entry_block->frequency,
2451 NULL, NULL);
2452 DECL_STRUCT_FUNCTION (fn)->saved_cfg = DECL_STRUCT_FUNCTION (newdecl)->cfg;
2453 DECL_STRUCT_FUNCTION (fn)->saved_eh = DECL_STRUCT_FUNCTION (newdecl)->eh;
2455 /* Clean up. */
2456 splay_tree_delete (id.decl_map);
2459 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2461 tree
2462 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2464 enum tree_code code = TREE_CODE (*tp);
2465 inline_data *id = (inline_data *) data;
2467 /* We make copies of most nodes. */
2468 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
2469 || code == TREE_LIST
2470 || code == TREE_VEC
2471 || code == TYPE_DECL)
2473 /* Because the chain gets clobbered when we make a copy, we save it
2474 here. */
2475 tree chain = TREE_CHAIN (*tp);
2476 tree new;
2478 if (id && id->versioning_p && replace_ref_tree (id, tp))
2480 *walk_subtrees = 0;
2481 return NULL_TREE;
2483 /* Copy the node. */
2484 new = copy_node (*tp);
2486 /* Propagate mudflap marked-ness. */
2487 if (flag_mudflap && mf_marked_p (*tp))
2488 mf_mark (new);
2490 *tp = new;
2492 /* Now, restore the chain, if appropriate. That will cause
2493 walk_tree to walk into the chain as well. */
2494 if (code == PARM_DECL || code == TREE_LIST)
2495 TREE_CHAIN (*tp) = chain;
2497 /* For now, we don't update BLOCKs when we make copies. So, we
2498 have to nullify all BIND_EXPRs. */
2499 if (TREE_CODE (*tp) == BIND_EXPR)
2500 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2502 else if (code == CONSTRUCTOR)
2504 /* CONSTRUCTOR nodes need special handling because
2505 we need to duplicate the vector of elements. */
2506 tree new;
2508 new = copy_node (*tp);
2510 /* Propagate mudflap marked-ness. */
2511 if (flag_mudflap && mf_marked_p (*tp))
2512 mf_mark (new);
2514 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2515 CONSTRUCTOR_ELTS (*tp));
2516 *tp = new;
2518 else if (TREE_CODE_CLASS (code) == tcc_type)
2519 *walk_subtrees = 0;
2520 else if (TREE_CODE_CLASS (code) == tcc_declaration)
2521 *walk_subtrees = 0;
2522 else if (TREE_CODE_CLASS (code) == tcc_constant)
2523 *walk_subtrees = 0;
2524 else
2525 gcc_assert (code != STATEMENT_LIST);
2526 return NULL_TREE;
2529 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2530 information indicating to what new SAVE_EXPR this one should be mapped,
2531 use that one. Otherwise, create a new node and enter it in ST. FN is
2532 the function into which the copy will be placed. */
2534 static void
2535 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2537 splay_tree st = (splay_tree) st_;
2538 splay_tree_node n;
2539 tree t;
2541 /* See if we already encountered this SAVE_EXPR. */
2542 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2544 /* If we didn't already remap this SAVE_EXPR, do so now. */
2545 if (!n)
2547 t = copy_node (*tp);
2549 /* Remember this SAVE_EXPR. */
2550 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2551 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2552 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2554 else
2556 /* We've already walked into this SAVE_EXPR; don't do it again. */
2557 *walk_subtrees = 0;
2558 t = (tree) n->value;
2561 /* Replace this SAVE_EXPR with the copy. */
2562 *tp = t;
2565 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2566 copies the declaration and enters it in the splay_tree in DATA (which is
2567 really an `inline_data *'). */
2569 static tree
2570 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2571 void *data)
2573 inline_data *id = (inline_data *) data;
2575 /* Don't walk into types. */
2576 if (TYPE_P (*tp))
2577 *walk_subtrees = 0;
2579 else if (TREE_CODE (*tp) == LABEL_EXPR)
2581 tree decl = TREE_OPERAND (*tp, 0);
2583 /* Copy the decl and remember the copy. */
2584 insert_decl_map (id, decl,
2585 copy_decl_for_dup (decl, DECL_CONTEXT (decl),
2586 DECL_CONTEXT (decl), /*versioning=*/false));
2589 return NULL_TREE;
2592 /* Perform any modifications to EXPR required when it is unsaved. Does
2593 not recurse into EXPR's subtrees. */
2595 static void
2596 unsave_expr_1 (tree expr)
2598 switch (TREE_CODE (expr))
2600 case TARGET_EXPR:
2601 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2602 It's OK for this to happen if it was part of a subtree that
2603 isn't immediately expanded, such as operand 2 of another
2604 TARGET_EXPR. */
2605 if (TREE_OPERAND (expr, 1))
2606 break;
2608 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2609 TREE_OPERAND (expr, 3) = NULL_TREE;
2610 break;
2612 default:
2613 break;
2617 /* Called via walk_tree when an expression is unsaved. Using the
2618 splay_tree pointed to by ST (which is really a `splay_tree'),
2619 remaps all local declarations to appropriate replacements. */
2621 static tree
2622 unsave_r (tree *tp, int *walk_subtrees, void *data)
2624 inline_data *id = (inline_data *) data;
2625 splay_tree st = id->decl_map;
2626 splay_tree_node n;
2628 /* Only a local declaration (variable or label). */
2629 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2630 || TREE_CODE (*tp) == LABEL_DECL)
2632 /* Lookup the declaration. */
2633 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2635 /* If it's there, remap it. */
2636 if (n)
2637 *tp = (tree) n->value;
2640 else if (TREE_CODE (*tp) == STATEMENT_LIST)
2641 copy_statement_list (tp);
2642 else if (TREE_CODE (*tp) == BIND_EXPR)
2643 copy_bind_expr (tp, walk_subtrees, id);
2644 else if (TREE_CODE (*tp) == SAVE_EXPR)
2645 remap_save_expr (tp, st, walk_subtrees);
2646 else
2648 copy_tree_r (tp, walk_subtrees, NULL);
2650 /* Do whatever unsaving is required. */
2651 unsave_expr_1 (*tp);
2654 /* Keep iterating. */
2655 return NULL_TREE;
2658 /* Copies everything in EXPR and replaces variables, labels
2659 and SAVE_EXPRs local to EXPR. */
2661 tree
2662 unsave_expr_now (tree expr)
2664 inline_data id;
2666 /* There's nothing to do for NULL_TREE. */
2667 if (expr == 0)
2668 return expr;
2670 /* Set up ID. */
2671 memset (&id, 0, sizeof (id));
2672 id.callee = current_function_decl;
2673 id.caller = current_function_decl;
2674 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2676 /* Walk the tree once to find local labels. */
2677 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2679 /* Walk the tree again, copying, remapping, and unsaving. */
2680 walk_tree (&expr, unsave_r, &id, NULL);
2682 /* Clean up. */
2683 splay_tree_delete (id.decl_map);
2685 return expr;
2688 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
2690 static tree
2691 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2693 if (*tp == data)
2694 return (tree) data;
2695 else
2696 return NULL;
2699 bool
2700 debug_find_tree (tree top, tree search)
2702 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2706 /* Declare the variables created by the inliner. Add all the variables in
2707 VARS to BIND_EXPR. */
2709 static void
2710 declare_inline_vars (tree block, tree vars)
2712 tree t;
2713 for (t = vars; t; t = TREE_CHAIN (t))
2714 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2716 if (block)
2717 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2721 /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
2722 but now it will be in the TO_FN. VERSIONING means that this function
2723 is used by the versioning utility (not inlining or cloning). */
2725 tree
2726 copy_decl_for_dup (tree decl, tree from_fn, tree to_fn, bool versioning)
2728 tree copy;
2730 gcc_assert (DECL_P (decl));
2731 /* Copy the declaration. */
2732 if (!versioning
2733 && (TREE_CODE (decl) == PARM_DECL
2734 || TREE_CODE (decl) == RESULT_DECL))
2736 tree type = TREE_TYPE (decl);
2738 /* For a parameter or result, we must make an equivalent VAR_DECL,
2739 not a new PARM_DECL. */
2740 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
2741 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2742 TREE_READONLY (copy) = TREE_READONLY (decl);
2743 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2744 DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
2746 else
2748 copy = copy_node (decl);
2749 /* The COPY is not abstract; it will be generated in TO_FN. */
2750 DECL_ABSTRACT (copy) = 0;
2751 lang_hooks.dup_lang_specific_decl (copy);
2753 /* TREE_ADDRESSABLE isn't used to indicate that a label's
2754 address has been taken; it's for internal bookkeeping in
2755 expand_goto_internal. */
2756 if (TREE_CODE (copy) == LABEL_DECL)
2758 TREE_ADDRESSABLE (copy) = 0;
2759 LABEL_DECL_UID (copy) = -1;
2763 /* Don't generate debug information for the copy if we wouldn't have
2764 generated it for the copy either. */
2765 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2766 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2768 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
2769 declaration inspired this copy. */
2770 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2772 /* The new variable/label has no RTL, yet. */
2773 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
2774 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
2775 SET_DECL_RTL (copy, NULL_RTX);
2777 /* These args would always appear unused, if not for this. */
2778 TREE_USED (copy) = 1;
2780 /* Set the context for the new declaration. */
2781 if (!DECL_CONTEXT (decl))
2782 /* Globals stay global. */
2784 else if (DECL_CONTEXT (decl) != from_fn)
2785 /* Things that weren't in the scope of the function we're inlining
2786 from aren't in the scope we're inlining to, either. */
2788 else if (TREE_STATIC (decl))
2789 /* Function-scoped static variables should stay in the original
2790 function. */
2792 else
2793 /* Ordinary automatic local variables are now in the scope of the
2794 new function. */
2795 DECL_CONTEXT (copy) = to_fn;
2797 return copy;
2800 /* Return a copy of the function's argument tree. */
2801 static tree
2802 copy_arguments_for_versioning (tree orig_parm, inline_data * id)
2804 tree *arg_copy, *parg;
2806 arg_copy = &orig_parm;
2807 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2809 tree new = remap_decl (*parg, id);
2810 lang_hooks.dup_lang_specific_decl (new);
2811 TREE_CHAIN (new) = TREE_CHAIN (*parg);
2812 *parg = new;
2814 return orig_parm;
2817 /* Return a copy of the function's static chain. */
2818 static tree
2819 copy_static_chain (tree static_chain, inline_data * id)
2821 tree *chain_copy, *pvar;
2823 chain_copy = &static_chain;
2824 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
2826 tree new = remap_decl (*pvar, id);
2827 lang_hooks.dup_lang_specific_decl (new);
2828 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
2829 *pvar = new;
2831 return static_chain;
2834 /* Return true if the function is allowed to be versioned.
2835 This is a guard for the versioning functionality. */
2836 bool
2837 tree_versionable_function_p (tree fndecl)
2839 if (fndecl == NULL_TREE)
2840 return false;
2841 /* ??? There are cases where a function is
2842 uninlinable but can be versioned. */
2843 if (!tree_inlinable_function_p (fndecl))
2844 return false;
2846 return true;
2849 /* Create a copy of a function's tree.
2850 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
2851 of the original function and the new copied function
2852 respectively. In case we want to replace a DECL
2853 tree with another tree while duplicating the function's
2854 body, TREE_MAP represents the mapping between these
2855 trees. */
2856 void
2857 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map)
2859 struct cgraph_node *old_version_node;
2860 struct cgraph_node *new_version_node;
2861 inline_data id;
2862 tree p, new_fndecl;
2863 unsigned i;
2864 struct ipa_replace_map *replace_info;
2865 basic_block old_entry_block;
2866 tree t_step;
2868 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
2869 && TREE_CODE (new_decl) == FUNCTION_DECL);
2870 DECL_POSSIBLY_INLINED (old_decl) = 1;
2872 old_version_node = cgraph_node (old_decl);
2873 new_version_node = cgraph_node (new_decl);
2875 allocate_struct_function (new_decl);
2876 /* Cfun points to the new allocated function struct at this point. */
2877 cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
2879 DECL_ARTIFICIAL (new_decl) = 1;
2880 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
2882 /* Generate a new name for the new version. */
2883 DECL_NAME (new_decl) =
2884 create_tmp_var_name (NULL);
2885 /* Create a new SYMBOL_REF rtx for the new name. */
2886 if (DECL_RTL (old_decl) != NULL)
2888 SET_DECL_RTL (new_decl, copy_rtx (DECL_RTL (old_decl)));
2889 XEXP (DECL_RTL (new_decl), 0) =
2890 gen_rtx_SYMBOL_REF (GET_MODE (XEXP (DECL_RTL (old_decl), 0)),
2891 IDENTIFIER_POINTER (DECL_NAME (new_decl)));
2894 /* Prepare the data structures for the tree copy. */
2895 memset (&id, 0, sizeof (id));
2897 /* The new version. */
2898 id.node = new_version_node;
2900 /* The old version. */
2901 id.current_node = cgraph_node (old_decl);
2903 id.versioning_p = true;
2904 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2905 id.caller = new_decl;
2906 id.callee = old_decl;
2907 id.callee_cfun = DECL_STRUCT_FUNCTION (old_decl);
2909 current_function_decl = new_decl;
2911 /* Copy the function's static chain. */
2912 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
2913 if (p)
2914 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
2915 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
2916 &id);
2917 /* Copy the function's arguments. */
2918 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
2919 DECL_ARGUMENTS (new_decl) =
2920 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
2922 /* If there's a tree_map, prepare for substitution. */
2923 if (tree_map)
2924 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
2926 replace_info = VARRAY_GENERIC_PTR (tree_map, i);
2927 if (replace_info->replace_p && !replace_info->ref_p)
2928 insert_decl_map (&id, replace_info->old_tree,
2929 replace_info->new_tree);
2930 else if (replace_info->replace_p && replace_info->ref_p)
2931 id.ipa_info = tree_map;
2934 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.callee), &id);
2936 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2937 number_blocks (id.caller);
2939 if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
2940 /* Add local vars. */
2941 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
2942 t_step; t_step = TREE_CHAIN (t_step))
2944 tree var = TREE_VALUE (t_step);
2945 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2946 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2947 cfun->unexpanded_var_list);
2948 else
2949 cfun->unexpanded_var_list =
2950 tree_cons (NULL_TREE, remap_decl (var, &id),
2951 cfun->unexpanded_var_list);
2954 /* Copy the Function's body. */
2955 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
2956 (DECL_STRUCT_FUNCTION (old_decl));
2957 new_fndecl = copy_body (&id,
2958 old_entry_block->count,
2959 old_entry_block->frequency, NULL, NULL);
2961 DECL_SAVED_TREE (new_decl) = DECL_SAVED_TREE (new_fndecl);
2963 DECL_STRUCT_FUNCTION (new_decl)->cfg =
2964 DECL_STRUCT_FUNCTION (new_fndecl)->cfg;
2965 DECL_STRUCT_FUNCTION (new_decl)->eh = DECL_STRUCT_FUNCTION (new_fndecl)->eh;
2966 DECL_STRUCT_FUNCTION (new_decl)->ib_boundaries_block =
2967 DECL_STRUCT_FUNCTION (new_fndecl)->ib_boundaries_block;
2968 DECL_STRUCT_FUNCTION (new_decl)->last_label_uid =
2969 DECL_STRUCT_FUNCTION (new_fndecl)->last_label_uid;
2971 if (DECL_RESULT (old_decl) != NULL_TREE)
2973 tree *res_decl = &DECL_RESULT (old_decl);
2974 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
2975 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
2978 current_function_decl = NULL;
2979 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2980 number_blocks (new_decl);
2982 /* Clean up. */
2983 splay_tree_delete (id.decl_map);
2984 fold_cond_expr_cond ();
2985 return;
2988 /* Replace an INDIRECT_REF tree of a given DECL tree with a new
2989 given tree.
2990 ID->ipa_info keeps the old tree and the new tree.
2991 TP points to the INDIRECT REF tree. Return true if
2992 the trees were replaced. */
2993 static bool
2994 replace_ref_tree (inline_data * id, tree * tp)
2996 bool replaced = false;
2997 tree new;
2999 if (id->ipa_info && VARRAY_ACTIVE_SIZE (id->ipa_info) > 0)
3001 unsigned i;
3003 for (i = 0; i < VARRAY_ACTIVE_SIZE (id->ipa_info); i++)
3005 struct ipa_replace_map *replace_info;
3006 replace_info = VARRAY_GENERIC_PTR (id->ipa_info, i);
3008 if (replace_info->replace_p && replace_info->ref_p)
3010 tree old_tree = replace_info->old_tree;
3011 tree new_tree = replace_info->new_tree;
3013 if (TREE_CODE (*tp) == INDIRECT_REF
3014 && TREE_OPERAND (*tp, 0) == old_tree)
3016 new = copy_node (new_tree);
3017 *tp = new;
3018 replaced = true;
3023 return replaced;
3026 /* Return true if we are inlining. */
3027 static inline bool
3028 inlining_p (inline_data * id)
3030 return (!id->saving_p && !id->cloning_p && !id->versioning_p);
3033 /* Duplicate a type, fields and all. */
3035 tree
3036 build_duplicate_type (tree type)
3038 inline_data id;
3040 memset (&id, 0, sizeof (id));
3041 id.callee = current_function_decl;
3042 id.caller = current_function_decl;
3043 id.callee_cfun = cfun;
3044 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
3046 type = remap_type_1 (type, &id);
3048 splay_tree_delete (id.decl_map);
3050 return type;