2005-06-22 Kelley Cook <kcook@gcc.gnu.org>
[official-gcc.git] / gcc / tree-inline.c
blobee30ccc92b089598cbc83373f7520a8ab0eaee30
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 "integrate.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 /* Callgraph node of function we are inlining into. */
131 struct cgraph_node *node;
132 /* Callgraph node of currently inlined function. */
133 struct cgraph_node *current_node;
134 /* Current BLOCK. */
135 tree block;
136 /* Exception region the inlined call lie in. */
137 int eh_region;
138 /* Take region number in the function being copied, add this value and
139 get eh region number of the duplicate in the function we inline into. */
140 int eh_region_offset;
141 } inline_data;
143 /* Prototypes. */
145 static tree declare_return_variable (inline_data *, tree, tree, tree *);
146 static tree copy_body_r (tree *, int *, void *);
147 static tree copy_generic_body (inline_data *);
148 static bool inlinable_function_p (tree);
149 static tree remap_decl (tree, inline_data *);
150 static tree remap_type (tree, inline_data *);
151 static void remap_block (tree *, inline_data *);
152 static tree remap_decl (tree, inline_data *);
153 static tree remap_decls (tree, inline_data *);
154 static void copy_bind_expr (tree *, int *, inline_data *);
155 static tree mark_local_for_remap_r (tree *, int *, void *);
156 static void unsave_expr_1 (tree);
157 static tree unsave_r (tree *, int *, void *);
158 static void declare_inline_vars (tree, tree);
159 static void remap_save_expr (tree *, void *, int *);
161 static inline bool inlining_p (inline_data *id);
163 /* Insert a tree->tree mapping for ID. Despite the name suggests
164 that the trees should be variables, it is used for more than that. */
166 static void
167 insert_decl_map (inline_data *id, tree key, tree value)
169 splay_tree_insert (id->decl_map, (splay_tree_key) key,
170 (splay_tree_value) value);
172 /* Always insert an identity map as well. If we see this same new
173 node again, we won't want to duplicate it a second time. */
174 if (key != value)
175 splay_tree_insert (id->decl_map, (splay_tree_key) value,
176 (splay_tree_value) value);
179 /* Remap DECL during the copying of the BLOCK tree for the function. */
181 static tree
182 remap_decl (tree decl, inline_data *id)
184 splay_tree_node n;
185 tree fn;
187 /* We only remap local variables in the current function. */
188 fn = id->callee;
190 /* See if we have remapped this declaration. */
192 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
194 /* If we didn't already have an equivalent for this declaration,
195 create one now. */
196 if (!n)
198 /* Make a copy of the variable or label. */
199 tree t;
200 t = copy_decl_for_inlining (decl, fn, id->caller);
202 /* Remember it, so that if we encounter this local entity again
203 we can reuse this copy. Do this early because remap_type may
204 need this decl for TYPE_STUB_DECL. */
205 insert_decl_map (id, decl, t);
207 /* Remap types, if necessary. */
208 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
209 if (TREE_CODE (t) == TYPE_DECL)
210 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
211 else if (TREE_CODE (t) == PARM_DECL)
212 DECL_ARG_TYPE_AS_WRITTEN (t)
213 = remap_type (DECL_ARG_TYPE_AS_WRITTEN (t), id);
215 /* Remap sizes as necessary. */
216 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
217 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
219 /* If fields, do likewise for offset and qualifier. */
220 if (TREE_CODE (t) == FIELD_DECL)
222 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
223 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
224 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
227 #if 0
228 /* FIXME handle anon aggrs. */
229 if (! DECL_NAME (t) && TREE_TYPE (t)
230 && lang_hooks.tree_inlining.anon_aggr_type_p (TREE_TYPE (t)))
232 /* For a VAR_DECL of anonymous type, we must also copy the
233 member VAR_DECLS here and rechain the DECL_ANON_UNION_ELEMS. */
234 tree members = NULL;
235 tree src;
237 for (src = DECL_ANON_UNION_ELEMS (t); src;
238 src = TREE_CHAIN (src))
240 tree member = remap_decl (TREE_VALUE (src), id);
242 gcc_assert (!TREE_PURPOSE (src));
243 members = tree_cons (NULL, member, members);
245 DECL_ANON_UNION_ELEMS (t) = nreverse (members);
247 #endif
249 /* If we are inlining and this is a variable (not a label), declare the
250 remapped variable in the callers' body. */
251 if (inlining_p (id)
252 && (TREE_CODE (t) == VAR_DECL
253 || TREE_CODE (t) == PARM_DECL))
254 declare_inline_vars (id->block, t);
256 /* Remember it, so that if we encounter this local entity
257 again we can reuse this copy. */
258 insert_decl_map (id, decl, t);
259 return t;
262 return unshare_expr ((tree) n->value);
265 static tree
266 remap_type (tree type, inline_data *id)
268 splay_tree_node node;
269 tree new, t;
271 if (type == NULL)
272 return type;
274 /* See if we have remapped this type. */
275 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
276 if (node)
277 return (tree) node->value;
279 /* The type only needs remapping if it's variably modified. */
280 if (! variably_modified_type_p (type, id->callee))
282 insert_decl_map (id, type, type);
283 return type;
286 /* We do need a copy. build and register it now. If this is a pointer or
287 reference type, remap the designated type and make a new pointer or
288 reference type. */
289 if (TREE_CODE (type) == POINTER_TYPE)
291 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
292 TYPE_MODE (type),
293 TYPE_REF_CAN_ALIAS_ALL (type));
294 insert_decl_map (id, type, new);
295 return new;
297 else if (TREE_CODE (type) == REFERENCE_TYPE)
299 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
300 TYPE_MODE (type),
301 TYPE_REF_CAN_ALIAS_ALL (type));
302 insert_decl_map (id, type, new);
303 return new;
305 else
306 new = copy_node (type);
308 insert_decl_map (id, type, new);
310 /* This is a new type, not a copy of an old type. Need to reassociate
311 variants. We can handle everything except the main variant lazily. */
312 t = TYPE_MAIN_VARIANT (type);
313 if (type != t)
315 t = remap_type (t, id);
316 TYPE_MAIN_VARIANT (new) = t;
317 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
318 TYPE_NEXT_VARIANT (t) = new;
320 else
322 TYPE_MAIN_VARIANT (new) = new;
323 TYPE_NEXT_VARIANT (new) = NULL;
326 if (TYPE_STUB_DECL (type))
327 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
329 /* Lazily create pointer and reference types. */
330 TYPE_POINTER_TO (new) = NULL;
331 TYPE_REFERENCE_TO (new) = NULL;
333 switch (TREE_CODE (new))
335 case INTEGER_TYPE:
336 case REAL_TYPE:
337 case ENUMERAL_TYPE:
338 case BOOLEAN_TYPE:
339 case CHAR_TYPE:
340 t = TYPE_MIN_VALUE (new);
341 if (t && TREE_CODE (t) != INTEGER_CST)
342 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
344 t = TYPE_MAX_VALUE (new);
345 if (t && TREE_CODE (t) != INTEGER_CST)
346 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
347 return new;
349 case FUNCTION_TYPE:
350 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
351 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
352 return new;
354 case ARRAY_TYPE:
355 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
356 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
357 break;
359 case RECORD_TYPE:
360 case UNION_TYPE:
361 case QUAL_UNION_TYPE:
362 walk_tree (&TYPE_FIELDS (new), copy_body_r, id, NULL);
363 break;
365 case OFFSET_TYPE:
366 default:
367 /* Shouldn't have been thought variable sized. */
368 gcc_unreachable ();
371 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
372 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
374 return new;
377 static tree
378 remap_decls (tree decls, inline_data *id)
380 tree old_var;
381 tree new_decls = NULL_TREE;
383 /* Remap its variables. */
384 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
386 tree new_var;
388 /* We can not chain the local static declarations into the unexpanded_var_list
389 as we can't duplicate them or break one decl rule. Go ahead and link
390 them into unexpanded_var_list. */
391 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->callee)
392 && !DECL_EXTERNAL (old_var))
394 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
395 cfun->unexpanded_var_list);
396 continue;
399 /* Remap the variable. */
400 new_var = remap_decl (old_var, id);
402 /* If we didn't remap this variable, so we can't mess with its
403 TREE_CHAIN. If we remapped this variable to the return slot, it's
404 already declared somewhere else, so don't declare it here. */
405 if (!new_var || new_var == id->retvar)
407 else
409 gcc_assert (DECL_P (new_var));
410 TREE_CHAIN (new_var) = new_decls;
411 new_decls = new_var;
415 return nreverse (new_decls);
418 /* Copy the BLOCK to contain remapped versions of the variables
419 therein. And hook the new block into the block-tree. */
421 static void
422 remap_block (tree *block, inline_data *id)
424 tree old_block;
425 tree new_block;
426 tree fn;
428 /* Make the new block. */
429 old_block = *block;
430 new_block = make_node (BLOCK);
431 TREE_USED (new_block) = TREE_USED (old_block);
432 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
433 *block = new_block;
435 /* Remap its variables. */
436 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
438 fn = id->caller;
439 #if 1
440 /* FIXME! It shouldn't be so hard to manage blocks. Rebuilding them in
441 rest_of_compilation is a good start. */
442 if (id->cloning_p)
443 /* We're building a clone; DECL_INITIAL is still
444 error_mark_node, and current_binding_level is the parm
445 binding level. */
446 lang_hooks.decls.insert_block (new_block);
447 else
449 /* Attach this new block after the DECL_INITIAL block for the
450 function into which this block is being inlined. In
451 rest_of_compilation we will straighten out the BLOCK tree. */
452 tree *first_block;
453 if (DECL_INITIAL (fn))
454 first_block = &BLOCK_CHAIN (DECL_INITIAL (fn));
455 else
456 first_block = &DECL_INITIAL (fn);
457 BLOCK_CHAIN (new_block) = *first_block;
458 *first_block = new_block;
460 #endif
461 /* Remember the remapped block. */
462 insert_decl_map (id, old_block, new_block);
465 static void
466 copy_statement_list (tree *tp)
468 tree_stmt_iterator oi, ni;
469 tree new;
471 new = alloc_stmt_list ();
472 ni = tsi_start (new);
473 oi = tsi_start (*tp);
474 *tp = new;
476 for (; !tsi_end_p (oi); tsi_next (&oi))
477 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
480 static void
481 copy_bind_expr (tree *tp, int *walk_subtrees, inline_data *id)
483 tree block = BIND_EXPR_BLOCK (*tp);
484 /* Copy (and replace) the statement. */
485 copy_tree_r (tp, walk_subtrees, NULL);
486 if (block)
488 remap_block (&block, id);
489 BIND_EXPR_BLOCK (*tp) = block;
492 if (BIND_EXPR_VARS (*tp))
493 /* This will remap a lot of the same decls again, but this should be
494 harmless. */
495 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
498 /* Called from copy_body_id via walk_tree. DATA is really an
499 `inline_data *'. */
501 static tree
502 copy_body_r (tree *tp, int *walk_subtrees, void *data)
504 inline_data *id = (inline_data *) data;
505 tree fn = id->callee;
507 /* Begin by recognizing trees that we'll completely rewrite for the
508 inlining context. Our output for these trees is completely
509 different from out input (e.g. RETURN_EXPR is deleted, and morphs
510 into an edge). Further down, we'll handle trees that get
511 duplicated and/or tweaked. */
513 /* If this is a RETURN_STMT, change it into an EXPR_STMT and a
514 GOTO_STMT with the RET_LABEL as its target. */
515 if (TREE_CODE (*tp) == RETURN_EXPR && inlining_p (id))
517 tree assignment = TREE_OPERAND (*tp, 0);
519 /* If we're returning something, just turn that into an
520 assignment into the equivalent of the original RESULT_DECL.
521 If the "assignment" is just the result decl, the result
522 decl has already been set (e.g. a recent "foo (&result_decl,
523 ...)"); just toss the entire RETURN_EXPR. */
524 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
526 /* Replace the RETURN_EXPR with (a copy of) the
527 MODIFY_EXPR hanging underneath. */
528 *tp = copy_node (assignment);
530 else /* Else the RETURN_EXPR returns no value. */
532 *tp = NULL;
533 return (void *)1;
537 /* Local variables and labels need to be replaced by equivalent
538 variables. We don't want to copy static variables; there's only
539 one of those, no matter how many times we inline the containing
540 function. Similarly for globals from an outer function. */
541 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
543 tree new_decl;
545 /* Remap the declaration. */
546 new_decl = remap_decl (*tp, id);
547 gcc_assert (new_decl);
548 /* Replace this variable with the copy. */
549 STRIP_TYPE_NOPS (new_decl);
550 *tp = new_decl;
551 *walk_subtrees = 0;
553 else if (TREE_CODE (*tp) == STATEMENT_LIST)
554 copy_statement_list (tp);
555 else if (TREE_CODE (*tp) == SAVE_EXPR)
556 remap_save_expr (tp, id->decl_map, walk_subtrees);
557 else if (TREE_CODE (*tp) == LABEL_DECL)
558 /* These may need to be remapped for EH handling. */
559 remap_decl (*tp, id);
560 else if (TREE_CODE (*tp) == BIND_EXPR)
561 copy_bind_expr (tp, walk_subtrees, id);
562 /* Types may need remapping as well. */
563 else if (TYPE_P (*tp))
564 *tp = remap_type (*tp, id);
566 /* If this is a constant, we have to copy the node iff the type will be
567 remapped. copy_tree_r will not copy a constant. */
568 else if (CONSTANT_CLASS_P (*tp))
570 tree new_type = remap_type (TREE_TYPE (*tp), id);
572 if (new_type == TREE_TYPE (*tp))
573 *walk_subtrees = 0;
575 else if (TREE_CODE (*tp) == INTEGER_CST)
576 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
577 TREE_INT_CST_HIGH (*tp));
578 else
580 *tp = copy_node (*tp);
581 TREE_TYPE (*tp) = new_type;
585 /* Otherwise, just copy the node. Note that copy_tree_r already
586 knows not to copy VAR_DECLs, etc., so this is safe. */
587 else
589 /* Here we handle trees that are not completely rewritten.
590 First we detect some inlining-induced bogosities for
591 discarding. */
592 if (TREE_CODE (*tp) == MODIFY_EXPR
593 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
594 && (lang_hooks.tree_inlining.auto_var_in_fn_p
595 (TREE_OPERAND (*tp, 0), fn)))
597 /* Some assignments VAR = VAR; don't generate any rtl code
598 and thus don't count as variable modification. Avoid
599 keeping bogosities like 0 = 0. */
600 tree decl = TREE_OPERAND (*tp, 0), value;
601 splay_tree_node n;
603 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
604 if (n)
606 value = (tree) n->value;
607 STRIP_TYPE_NOPS (value);
608 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
610 *tp = build_empty_stmt ();
611 return copy_body_r (tp, walk_subtrees, data);
615 else if (TREE_CODE (*tp) == INDIRECT_REF)
617 /* Get rid of *& from inline substitutions that can happen when a
618 pointer argument is an ADDR_EXPR. */
619 tree decl = TREE_OPERAND (*tp, 0);
620 splay_tree_node n;
622 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
623 if (n)
625 /* If we happen to get an ADDR_EXPR in n->value, strip
626 it manually here as we'll eventually get ADDR_EXPRs
627 which lie about their types pointed to. In this case
628 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
629 but we absolutely rely on that. As fold_indirect_ref
630 does other useful transformations, try that first, though. */
631 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
632 *tp = fold_indirect_ref_1 (type, (tree)n->value);
633 if (! *tp)
635 if (TREE_CODE ((tree)n->value) == ADDR_EXPR)
636 *tp = TREE_OPERAND ((tree)n->value, 0);
637 else
638 *tp = build1 (INDIRECT_REF, type, (tree)n->value);
640 *walk_subtrees = 0;
641 return NULL;
645 /* Here is the "usual case". Copy this tree node, and then
646 tweak some special cases. */
647 copy_tree_r (tp, walk_subtrees, NULL);
648 if (id->block
649 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp))))
650 TREE_BLOCK (*tp) = id->block;
652 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
653 TREE_OPERAND (*tp, 0) =
654 build_int_cst
655 (NULL_TREE,
656 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
658 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
660 /* The copied TARGET_EXPR has never been expanded, even if the
661 original node was expanded already. */
662 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
664 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
665 TREE_OPERAND (*tp, 3) = NULL_TREE;
668 /* Variable substitution need not be simple. In particular, the
669 INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
670 and friends are up-to-date. */
671 else if (TREE_CODE (*tp) == ADDR_EXPR)
673 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
674 recompute_tree_invarant_for_addr_expr (*tp);
675 *walk_subtrees = 0;
679 /* Keep iterating. */
680 return NULL_TREE;
683 /* Copy basic block, scale profile accordingly. Edges will be taken care of
684 later */
686 static basic_block
687 copy_bb (inline_data *id, basic_block bb, int frequency_scale, int count_scale)
689 block_stmt_iterator bsi, copy_bsi;
690 basic_block copy_basic_block;
692 /* create_basic_block() will append every new block to
693 basic_block_info automatically. */
694 copy_basic_block = create_basic_block (NULL, (void *) 0, bb->prev_bb->aux);
695 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
696 copy_basic_block->frequency = (bb->frequency
697 * frequency_scale / REG_BR_PROB_BASE);
698 copy_bsi = bsi_start (copy_basic_block);
700 for (bsi = bsi_start (bb);
701 !bsi_end_p (bsi); bsi_next (&bsi))
703 tree stmt = bsi_stmt (bsi);
704 tree orig_stmt = stmt;
706 walk_tree (&stmt, copy_body_r, id, NULL);
708 /* RETURN_EXPR might be removed,
709 this is signalled by making stmt pointer NULL. */
710 if (stmt)
712 tree call, decl;
713 bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
714 call = get_call_expr_in (stmt);
715 /* We're duplicating a CALL_EXPR. Find any corresponding
716 callgraph edges and update or duplicate them. */
717 if (call && (decl = get_callee_fndecl (call)))
719 if (id->saving_p)
721 struct cgraph_node *node;
722 struct cgraph_edge *edge;
724 /* We're saving a copy of the body, so we'll update the
725 callgraph nodes in place. Note that we avoid
726 altering the original callgraph node; we begin with
727 the first clone. */
728 for (node = id->node->next_clone;
729 node;
730 node = node->next_clone)
732 edge = cgraph_edge (node, orig_stmt);
733 gcc_assert (edge);
734 edge->call_stmt = stmt;
737 else
739 struct cgraph_edge *edge;
741 /* We're cloning or inlining this body; duplicate the
742 associate callgraph nodes. */
743 edge = cgraph_edge (id->current_node, orig_stmt);
744 if (edge)
745 cgraph_clone_edge (edge, id->node, stmt,
746 REG_BR_PROB_BASE, 1);
749 /* If you think we can abort here, you are wrong.
750 There is no region 0 in tree land. */
751 gcc_assert (lookup_stmt_eh_region_fn (id->callee_cfun, orig_stmt)
752 != 0);
754 if (tree_could_throw_p (stmt))
756 int region = lookup_stmt_eh_region_fn (id->callee_cfun, orig_stmt);
757 /* Add an entry for the copied tree in the EH hashtable.
758 When saving or cloning or versioning, use the hashtable in
759 cfun, and just copy the EH number. When inlining, use the
760 hashtable in the caller, and adjust the region number. */
761 if (region > 0)
762 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
764 /* If this tree doesn't have a region associated with it,
765 and there is a "current region,"
766 then associate this tree with the current region
767 and add edges associated with this region. */
768 if ((lookup_stmt_eh_region_fn (id->callee_cfun,
769 orig_stmt) <= 0
770 && id->eh_region > 0)
771 && tree_could_throw_p (stmt))
772 add_stmt_to_eh_region (stmt, id->eh_region);
776 return copy_basic_block;
779 /* Copy edges from BB into its copy constructed earlier, scale profile
780 accordingly. Edges will be taken care of later. Assume aux
781 pointers to point to the copies of each BB. */
782 static void
783 copy_edges_for_bb (basic_block bb, int count_scale)
785 basic_block new_bb = bb->aux;
786 edge_iterator ei;
787 edge old_edge;
788 block_stmt_iterator bsi;
789 int flags;
791 /* Use the indices from the original blocks to create edges for the
792 new ones. */
793 FOR_EACH_EDGE (old_edge, ei, bb->succs)
794 if (!(old_edge->flags & EDGE_EH))
796 edge new;
798 flags = old_edge->flags;
800 /* Return edges do get a FALLTHRU flag when the get inlined. */
801 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
802 && old_edge->dest->aux != EXIT_BLOCK_PTR)
803 flags |= EDGE_FALLTHRU;
804 new = make_edge (new_bb, old_edge->dest->aux, flags);
805 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
806 new->probability = old_edge->probability;
809 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
810 return;
812 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
814 tree copy_stmt;
816 copy_stmt = bsi_stmt (bsi);
817 update_stmt (copy_stmt);
818 /* Do this before the possible split_block. */
819 bsi_next (&bsi);
821 /* If this tree could throw an exception, there are two
822 cases where we need to add abnormal edge(s): the
823 tree wasn't in a region and there is a "current
824 region" in the caller; or the original tree had
825 EH edges. In both cases split the block after the tree,
826 and add abnormal edge(s) as needed; we need both
827 those from the callee and the caller.
828 We check whether the copy can throw, because the const
829 propagation can change an INDIRECT_REF which throws
830 into a COMPONENT_REF which doesn't. If the copy
831 can throw, the original could also throw. */
833 if (tree_can_throw_internal (copy_stmt))
835 if (!bsi_end_p (bsi))
836 /* Note that bb's predecessor edges aren't necessarily
837 right at this point; split_block doesn't care. */
839 edge e = split_block (new_bb, copy_stmt);
840 new_bb = e->dest;
841 bsi = bsi_start (new_bb);
844 make_eh_edges (copy_stmt);
849 /* Wrapper for remap_decl so it can be used as a callback. */
850 static tree
851 remap_decl_1 (tree decl, void *data)
853 return remap_decl (decl, data);
856 /* Make a copy of the body of FN so that it can be inserted inline in
857 another function. Walks FN via CFG, returns new fndecl. */
859 static tree
860 copy_cfg_body (inline_data * id, gcov_type count, int frequency,
861 basic_block entry_block_map, basic_block exit_block_map)
863 tree callee_fndecl = id->callee;
864 /* Original cfun for the callee, doesn't change. */
865 struct function *callee_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
866 /* Copy, built by this function. */
867 struct function *new_cfun;
868 /* Place to copy from; when a copy of the function was saved off earlier,
869 use that instead of the main copy. */
870 struct function *cfun_to_copy =
871 (struct function *) ggc_alloc_cleared (sizeof (struct function));
872 basic_block bb;
873 tree new_fndecl = NULL;
874 bool saving_or_cloning;
875 int count_scale, frequency_scale;
877 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count)
878 count_scale = (REG_BR_PROB_BASE * count
879 / ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count);
880 else
881 count_scale = 1;
883 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency)
884 frequency_scale = (REG_BR_PROB_BASE * frequency
886 ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency);
887 else
888 frequency_scale = count_scale;
890 /* Register specific tree functions. */
891 tree_register_cfg_hooks ();
893 /* Must have a CFG here at this point. */
894 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
895 (DECL_STRUCT_FUNCTION (callee_fndecl)));
897 *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
899 /* If there is a saved_cfg+saved_args lurking in the
900 struct function, a copy of the callee body was saved there, and
901 the 'struct cgraph edge' nodes have been fudged to point into the
902 saved body. Accordingly, we want to copy that saved body so the
903 callgraph edges will be recognized and cloned properly. */
904 if (cfun_to_copy->saved_cfg)
906 cfun_to_copy->cfg = cfun_to_copy->saved_cfg;
907 cfun_to_copy->eh = cfun_to_copy->saved_eh;
909 id->callee_cfun = cfun_to_copy;
911 /* If saving or cloning a function body, create new basic_block_info
912 and label_to_block_maps. Otherwise, we're duplicating a function
913 body for inlining; insert our new blocks and labels into the
914 existing varrays. */
915 saving_or_cloning = (id->saving_p || id->cloning_p);
916 if (saving_or_cloning)
918 new_cfun =
919 (struct function *) ggc_alloc_cleared (sizeof (struct function));
920 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
921 new_cfun->cfg = NULL;
922 new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
923 new_cfun->ib_boundaries_block = (varray_type) 0;
924 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
925 push_cfun (new_cfun);
926 init_empty_tree_cfg ();
928 ENTRY_BLOCK_PTR->count =
929 (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count * count_scale /
930 REG_BR_PROB_BASE);
931 ENTRY_BLOCK_PTR->frequency =
932 (ENTRY_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency *
933 frequency_scale / REG_BR_PROB_BASE);
934 EXIT_BLOCK_PTR->count =
935 (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->count * count_scale /
936 REG_BR_PROB_BASE);
937 EXIT_BLOCK_PTR->frequency =
938 (EXIT_BLOCK_PTR_FOR_FUNCTION (callee_cfun)->frequency *
939 frequency_scale / REG_BR_PROB_BASE);
941 entry_block_map = ENTRY_BLOCK_PTR;
942 exit_block_map = EXIT_BLOCK_PTR;
945 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
946 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
949 /* Duplicate any exception-handling regions. */
950 if (cfun->eh)
952 if (saving_or_cloning)
953 init_eh_for_function ();
954 id->eh_region_offset = duplicate_eh_regions (cfun_to_copy,
955 remap_decl_1,
956 id, id->eh_region);
957 gcc_assert (inlining_p (id) || !id->eh_region_offset);
959 /* Use aux pointers to map the original blocks to copy. */
960 FOR_EACH_BB_FN (bb, cfun_to_copy)
961 bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
962 /* Now that we've duplicated the blocks, duplicate their edges. */
963 FOR_ALL_BB_FN (bb, cfun_to_copy)
964 copy_edges_for_bb (bb, count_scale);
965 FOR_ALL_BB_FN (bb, cfun_to_copy)
966 bb->aux = NULL;
968 if (saving_or_cloning)
969 pop_cfun ();
971 return new_fndecl;
974 /* Make a copy of the body of FN so that it can be inserted inline in
975 another function. */
977 static tree
978 copy_generic_body (inline_data *id)
980 tree body;
981 tree fndecl = id->callee;
983 body = DECL_SAVED_TREE (fndecl);
984 walk_tree (&body, copy_body_r, id, NULL);
986 return body;
989 static tree
990 copy_body (inline_data *id, gcov_type count, int frequency,
991 basic_block entry_block_map, basic_block exit_block_map)
993 tree fndecl = id->callee;
994 tree body;
996 /* If this body has a CFG, walk CFG and copy. */
997 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
998 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1000 return body;
1003 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1004 defined in function FN, or of a data member thereof. */
1006 static bool
1007 self_inlining_addr_expr (tree value, tree fn)
1009 tree var;
1011 if (TREE_CODE (value) != ADDR_EXPR)
1012 return false;
1014 var = get_base_address (TREE_OPERAND (value, 0));
1016 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
1019 static void
1020 setup_one_parameter (inline_data *id, tree p, tree value, tree fn,
1021 basic_block bb, tree *vars)
1023 tree init_stmt;
1024 tree var;
1025 tree var_sub;
1027 /* If the parameter is never assigned to, we may not need to
1028 create a new variable here at all. Instead, we may be able
1029 to just use the argument value. */
1030 if (TREE_READONLY (p)
1031 && !TREE_ADDRESSABLE (p)
1032 && value && !TREE_SIDE_EFFECTS (value))
1034 /* We may produce non-gimple trees by adding NOPs or introduce
1035 invalid sharing when operand is not really constant.
1036 It is not big deal to prohibit constant propagation here as
1037 we will constant propagate in DOM1 pass anyway. */
1038 if (is_gimple_min_invariant (value)
1039 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1040 /* We have to be very careful about ADDR_EXPR. Make sure
1041 the base variable isn't a local variable of the inlined
1042 function, e.g., when doing recursive inlining, direct or
1043 mutually-recursive or whatever, which is why we don't
1044 just test whether fn == current_function_decl. */
1045 && ! self_inlining_addr_expr (value, fn))
1047 insert_decl_map (id, p, value);
1048 return;
1052 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
1053 here since the type of this decl must be visible to the calling
1054 function. */
1055 var = copy_decl_for_inlining (p, fn, id->caller);
1057 /* See if the frontend wants to pass this by invisible reference. If
1058 so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1059 replace uses of the PARM_DECL with dereferences. */
1060 if (TREE_TYPE (var) != TREE_TYPE (p)
1061 && POINTER_TYPE_P (TREE_TYPE (var))
1062 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1064 insert_decl_map (id, var, var);
1065 var_sub = build_fold_indirect_ref (var);
1067 else
1068 var_sub = var;
1070 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1071 that way, when the PARM_DECL is encountered, it will be
1072 automatically replaced by the VAR_DECL. */
1073 insert_decl_map (id, p, var_sub);
1075 /* Declare this new variable. */
1076 TREE_CHAIN (var) = *vars;
1077 *vars = var;
1079 /* Make gimplifier happy about this variable. */
1080 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1082 /* Even if P was TREE_READONLY, the new VAR should not be.
1083 In the original code, we would have constructed a
1084 temporary, and then the function body would have never
1085 changed the value of P. However, now, we will be
1086 constructing VAR directly. The constructor body may
1087 change its value multiple times as it is being
1088 constructed. Therefore, it must not be TREE_READONLY;
1089 the back-end assumes that TREE_READONLY variable is
1090 assigned to only once. */
1091 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1092 TREE_READONLY (var) = 0;
1094 /* Initialize this VAR_DECL from the equivalent argument. Convert
1095 the argument to the proper type in case it was promoted. */
1096 if (value)
1098 tree rhs = fold_convert (TREE_TYPE (var), value);
1099 block_stmt_iterator bsi = bsi_last (bb);
1101 if (rhs == error_mark_node)
1102 return;
1104 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1105 keep our trees in gimple form. */
1106 init_stmt = build (MODIFY_EXPR, TREE_TYPE (var), var, rhs);
1108 /* If we did not create a gimple value and we did not create a gimple
1109 cast of a gimple value, then we will need to gimplify INIT_STMTS
1110 at the end. Note that is_gimple_cast only checks the outer
1111 tree code, not its operand. Thus the explicit check that its
1112 operand is a gimple value. */
1113 if (!is_gimple_val (rhs)
1114 && (!is_gimple_cast (rhs)
1115 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1116 gimplify_stmt (&init_stmt);
1117 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1121 /* Generate code to initialize the parameters of the function at the
1122 top of the stack in ID from the ARGS (presented as a TREE_LIST). */
1124 static void
1125 initialize_inlined_parameters (inline_data *id, tree args, tree static_chain,
1126 tree fn, basic_block bb)
1128 tree parms;
1129 tree a;
1130 tree p;
1131 tree vars = NULL_TREE;
1132 int argnum = 0;
1134 /* Figure out what the parameters are. */
1135 parms = DECL_ARGUMENTS (fn);
1136 if (fn == current_function_decl)
1137 parms = cfun->saved_args;
1139 /* Loop through the parameter declarations, replacing each with an
1140 equivalent VAR_DECL, appropriately initialized. */
1141 for (p = parms, a = args; p;
1142 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1144 tree value;
1146 ++argnum;
1148 /* Find the initializer. */
1149 value = lang_hooks.tree_inlining.convert_parm_for_inlining
1150 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1152 setup_one_parameter (id, p, value, fn, bb, &vars);
1155 /* Initialize the static chain. */
1156 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1157 if (fn == current_function_decl)
1158 p = DECL_STRUCT_FUNCTION (fn)->saved_static_chain_decl;
1159 if (p)
1161 /* No static chain? Seems like a bug in tree-nested.c. */
1162 gcc_assert (static_chain);
1164 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1167 declare_inline_vars (id->block, vars);
1170 /* Declare a return variable to replace the RESULT_DECL for the
1171 function we are calling. An appropriate DECL_STMT is returned.
1172 The USE_STMT is filled to contain a use of the declaration to
1173 indicate the return value of the function.
1175 RETURN_SLOT_ADDR, if non-null, was a fake parameter that
1176 took the address of the result. MODIFY_DEST, if non-null, was the LHS of
1177 the MODIFY_EXPR to which this call is the RHS.
1179 The return value is a (possibly null) value that is the result of the
1180 function as seen by the callee. *USE_P is a (possibly null) value that
1181 holds the result as seen by the caller. */
1183 static tree
1184 declare_return_variable (inline_data *id, tree return_slot_addr,
1185 tree modify_dest, tree *use_p)
1187 tree callee = id->callee;
1188 tree caller = id->caller;
1189 tree result = DECL_RESULT (callee);
1190 tree callee_type = TREE_TYPE (result);
1191 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1192 tree var, use;
1194 /* We don't need to do anything for functions that don't return
1195 anything. */
1196 if (!result || VOID_TYPE_P (callee_type))
1198 *use_p = NULL_TREE;
1199 return NULL_TREE;
1202 /* If there was a return slot, then the return value is the
1203 dereferenced address of that object. */
1204 if (return_slot_addr)
1206 /* The front end shouldn't have used both return_slot_addr and
1207 a modify expression. */
1208 gcc_assert (!modify_dest);
1209 if (DECL_BY_REFERENCE (result))
1210 var = return_slot_addr;
1211 else
1212 var = build_fold_indirect_ref (return_slot_addr);
1213 use = NULL;
1214 goto done;
1217 /* All types requiring non-trivial constructors should have been handled. */
1218 gcc_assert (!TREE_ADDRESSABLE (callee_type));
1220 /* Attempt to avoid creating a new temporary variable. */
1221 if (modify_dest)
1223 bool use_it = false;
1225 /* We can't use MODIFY_DEST if there's type promotion involved. */
1226 if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1227 use_it = false;
1229 /* ??? If we're assigning to a variable sized type, then we must
1230 reuse the destination variable, because we've no good way to
1231 create variable sized temporaries at this point. */
1232 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1233 use_it = true;
1235 /* If the callee cannot possibly modify MODIFY_DEST, then we can
1236 reuse it as the result of the call directly. Don't do this if
1237 it would promote MODIFY_DEST to addressable. */
1238 else if (!TREE_STATIC (modify_dest)
1239 && !TREE_ADDRESSABLE (modify_dest)
1240 && !TREE_ADDRESSABLE (result))
1241 use_it = true;
1243 if (use_it)
1245 var = modify_dest;
1246 use = NULL;
1247 goto done;
1251 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1253 var = copy_decl_for_inlining (result, callee, caller);
1255 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1256 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1257 = tree_cons (NULL_TREE, var,
1258 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1260 /* Do not have the rest of GCC warn about this variable as it should
1261 not be visible to the user. */
1262 TREE_NO_WARNING (var) = 1;
1264 /* Build the use expr. If the return type of the function was
1265 promoted, convert it back to the expected type. */
1266 use = var;
1267 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1268 use = fold_convert (caller_type, var);
1270 done:
1271 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1272 way, when the RESULT_DECL is encountered, it will be
1273 automatically replaced by the VAR_DECL. */
1274 insert_decl_map (id, result, var);
1276 /* Remember this so we can ignore it in remap_decls. */
1277 id->retvar = var;
1279 *use_p = use;
1280 return var;
1283 /* Returns nonzero if a function can be inlined as a tree. */
1285 bool
1286 tree_inlinable_function_p (tree fn)
1288 return inlinable_function_p (fn);
1291 static const char *inline_forbidden_reason;
1293 static tree
1294 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1295 void *fnp)
1297 tree node = *nodep;
1298 tree fn = (tree) fnp;
1299 tree t;
1301 switch (TREE_CODE (node))
1303 case CALL_EXPR:
1304 /* Refuse to inline alloca call unless user explicitly forced so as
1305 this may change program's memory overhead drastically when the
1306 function using alloca is called in loop. In GCC present in
1307 SPEC2000 inlining into schedule_block cause it to require 2GB of
1308 RAM instead of 256MB. */
1309 if (alloca_call_p (node)
1310 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1312 inline_forbidden_reason
1313 = G_("%Jfunction %qF can never be inlined because it uses "
1314 "alloca (override using the always_inline attribute)");
1315 return node;
1317 t = get_callee_fndecl (node);
1318 if (! t)
1319 break;
1321 /* We cannot inline functions that call setjmp. */
1322 if (setjmp_call_p (t))
1324 inline_forbidden_reason
1325 = G_("%Jfunction %qF can never be inlined because it uses setjmp");
1326 return node;
1329 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1330 switch (DECL_FUNCTION_CODE (t))
1332 /* We cannot inline functions that take a variable number of
1333 arguments. */
1334 case BUILT_IN_VA_START:
1335 case BUILT_IN_STDARG_START:
1336 case BUILT_IN_NEXT_ARG:
1337 case BUILT_IN_VA_END:
1338 inline_forbidden_reason
1339 = G_("%Jfunction %qF can never be inlined because it "
1340 "uses variable argument lists");
1341 return node;
1343 case BUILT_IN_LONGJMP:
1344 /* We can't inline functions that call __builtin_longjmp at
1345 all. The non-local goto machinery really requires the
1346 destination be in a different function. If we allow the
1347 function calling __builtin_longjmp to be inlined into the
1348 function calling __builtin_setjmp, Things will Go Awry. */
1349 inline_forbidden_reason
1350 = G_("%Jfunction %qF can never be inlined because "
1351 "it uses setjmp-longjmp exception handling");
1352 return node;
1354 case BUILT_IN_NONLOCAL_GOTO:
1355 /* Similarly. */
1356 inline_forbidden_reason
1357 = G_("%Jfunction %qF can never be inlined because "
1358 "it uses non-local goto");
1359 return node;
1361 case BUILT_IN_RETURN:
1362 case BUILT_IN_APPLY_ARGS:
1363 /* If a __builtin_apply_args caller would be inlined,
1364 it would be saving arguments of the function it has
1365 been inlined into. Similarly __builtin_return would
1366 return from the function the inline has been inlined into. */
1367 inline_forbidden_reason
1368 = G_("%Jfunction %qF can never be inlined because "
1369 "it uses __builtin_return or __builtin_apply_args");
1370 return node;
1372 default:
1373 break;
1375 break;
1377 case GOTO_EXPR:
1378 t = TREE_OPERAND (node, 0);
1380 /* We will not inline a function which uses computed goto. The
1381 addresses of its local labels, which may be tucked into
1382 global storage, are of course not constant across
1383 instantiations, which causes unexpected behavior. */
1384 if (TREE_CODE (t) != LABEL_DECL)
1386 inline_forbidden_reason
1387 = G_("%Jfunction %qF can never be inlined "
1388 "because it contains a computed goto");
1389 return node;
1391 break;
1393 case LABEL_EXPR:
1394 t = TREE_OPERAND (node, 0);
1395 if (DECL_NONLOCAL (t))
1397 /* We cannot inline a function that receives a non-local goto
1398 because we cannot remap the destination label used in the
1399 function that is performing the non-local goto. */
1400 inline_forbidden_reason
1401 = G_("%Jfunction %qF can never be inlined "
1402 "because it receives a non-local goto");
1403 return node;
1405 break;
1407 case RECORD_TYPE:
1408 case UNION_TYPE:
1409 /* We cannot inline a function of the form
1411 void F (int i) { struct S { int ar[i]; } s; }
1413 Attempting to do so produces a catch-22.
1414 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1415 UNION_TYPE nodes, then it goes into infinite recursion on a
1416 structure containing a pointer to its own type. If it doesn't,
1417 then the type node for S doesn't get adjusted properly when
1418 F is inlined.
1420 ??? This is likely no longer true, but it's too late in the 4.0
1421 cycle to try to find out. This should be checked for 4.1. */
1422 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1423 if (variably_modified_type_p (TREE_TYPE (t), NULL))
1425 inline_forbidden_reason
1426 = G_("%Jfunction %qF can never be inlined "
1427 "because it uses variable sized variables");
1428 return node;
1431 default:
1432 break;
1435 return NULL_TREE;
1438 /* Return subexpression representing possible alloca call, if any. */
1439 static tree
1440 inline_forbidden_p (tree fndecl)
1442 location_t saved_loc = input_location;
1443 block_stmt_iterator bsi;
1444 basic_block bb;
1445 tree ret = NULL_TREE;
1447 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1448 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1450 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1451 inline_forbidden_p_1, fndecl);
1452 if (ret)
1453 goto egress;
1456 egress:
1457 input_location = saved_loc;
1458 return ret;
1461 /* Returns nonzero if FN is a function that does not have any
1462 fundamental inline blocking properties. */
1464 static bool
1465 inlinable_function_p (tree fn)
1467 bool inlinable = true;
1469 /* If we've already decided this function shouldn't be inlined,
1470 there's no need to check again. */
1471 if (DECL_UNINLINABLE (fn))
1472 return false;
1474 /* See if there is any language-specific reason it cannot be
1475 inlined. (It is important that this hook be called early because
1476 in C++ it may result in template instantiation.)
1477 If the function is not inlinable for language-specific reasons,
1478 it is left up to the langhook to explain why. */
1479 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1481 /* If we don't have the function body available, we can't inline it.
1482 However, this should not be recorded since we also get here for
1483 forward declared inline functions. Therefore, return at once. */
1484 if (!DECL_SAVED_TREE (fn))
1485 return false;
1487 /* If we're not inlining at all, then we cannot inline this function. */
1488 else if (!flag_inline_trees)
1489 inlinable = false;
1491 /* Only try to inline functions if DECL_INLINE is set. This should be
1492 true for all functions declared `inline', and for all other functions
1493 as well with -finline-functions.
1495 Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1496 it's the front-end that must set DECL_INLINE in this case, because
1497 dwarf2out loses if a function that does not have DECL_INLINE set is
1498 inlined anyway. That is why we have both DECL_INLINE and
1499 DECL_DECLARED_INLINE_P. */
1500 /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1501 here should be redundant. */
1502 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1503 inlinable = false;
1505 else if (inline_forbidden_p (fn))
1507 /* See if we should warn about uninlinable functions. Previously,
1508 some of these warnings would be issued while trying to expand
1509 the function inline, but that would cause multiple warnings
1510 about functions that would for example call alloca. But since
1511 this a property of the function, just one warning is enough.
1512 As a bonus we can now give more details about the reason why a
1513 function is not inlinable.
1514 We only warn for functions declared `inline' by the user. */
1515 bool do_warning = (warn_inline
1516 && DECL_INLINE (fn)
1517 && DECL_DECLARED_INLINE_P (fn)
1518 && !DECL_IN_SYSTEM_HEADER (fn));
1520 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1521 sorry (inline_forbidden_reason, fn, fn);
1522 else if (do_warning)
1523 warning (0, inline_forbidden_reason, fn, fn);
1525 inlinable = false;
1528 /* Squirrel away the result so that we don't have to check again. */
1529 DECL_UNINLINABLE (fn) = !inlinable;
1531 return inlinable;
1534 /* Estimate the cost of a memory move. Use machine dependent
1535 word size and take possible memcpy call into account. */
1538 estimate_move_cost (tree type)
1540 HOST_WIDE_INT size;
1542 size = int_size_in_bytes (type);
1544 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1545 /* Cost of a memcpy call, 3 arguments and the call. */
1546 return 4;
1547 else
1548 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1551 /* Used by estimate_num_insns. Estimate number of instructions seen
1552 by given statement. */
1554 static tree
1555 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1557 int *count = data;
1558 tree x = *tp;
1560 if (IS_TYPE_OR_DECL_P (x))
1562 *walk_subtrees = 0;
1563 return NULL;
1565 /* Assume that constants and references counts nothing. These should
1566 be majorized by amount of operations among them we count later
1567 and are common target of CSE and similar optimizations. */
1568 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1569 return NULL;
1571 switch (TREE_CODE (x))
1573 /* Containers have no cost. */
1574 case TREE_LIST:
1575 case TREE_VEC:
1576 case BLOCK:
1577 case COMPONENT_REF:
1578 case BIT_FIELD_REF:
1579 case INDIRECT_REF:
1580 case ALIGN_INDIRECT_REF:
1581 case MISALIGNED_INDIRECT_REF:
1582 case ARRAY_REF:
1583 case ARRAY_RANGE_REF:
1584 case OBJ_TYPE_REF:
1585 case EXC_PTR_EXPR: /* ??? */
1586 case FILTER_EXPR: /* ??? */
1587 case COMPOUND_EXPR:
1588 case BIND_EXPR:
1589 case WITH_CLEANUP_EXPR:
1590 case NOP_EXPR:
1591 case VIEW_CONVERT_EXPR:
1592 case SAVE_EXPR:
1593 case ADDR_EXPR:
1594 case COMPLEX_EXPR:
1595 case RANGE_EXPR:
1596 case CASE_LABEL_EXPR:
1597 case SSA_NAME:
1598 case CATCH_EXPR:
1599 case EH_FILTER_EXPR:
1600 case STATEMENT_LIST:
1601 case ERROR_MARK:
1602 case NON_LVALUE_EXPR:
1603 case FDESC_EXPR:
1604 case VA_ARG_EXPR:
1605 case TRY_CATCH_EXPR:
1606 case TRY_FINALLY_EXPR:
1607 case LABEL_EXPR:
1608 case GOTO_EXPR:
1609 case RETURN_EXPR:
1610 case EXIT_EXPR:
1611 case LOOP_EXPR:
1612 case PHI_NODE:
1613 case WITH_SIZE_EXPR:
1614 break;
1616 /* We don't account constants for now. Assume that the cost is amortized
1617 by operations that do use them. We may re-consider this decision once
1618 we are able to optimize the tree before estimating its size and break
1619 out static initializers. */
1620 case IDENTIFIER_NODE:
1621 case INTEGER_CST:
1622 case REAL_CST:
1623 case COMPLEX_CST:
1624 case VECTOR_CST:
1625 case STRING_CST:
1626 *walk_subtrees = 0;
1627 return NULL;
1629 /* Try to estimate the cost of assignments. We have three cases to
1630 deal with:
1631 1) Simple assignments to registers;
1632 2) Stores to things that must live in memory. This includes
1633 "normal" stores to scalars, but also assignments of large
1634 structures, or constructors of big arrays;
1635 3) TARGET_EXPRs.
1637 Let us look at the first two cases, assuming we have "a = b + C":
1638 <modify_expr <var_decl "a"> <plus_expr <var_decl "b"> <constant C>>
1639 If "a" is a GIMPLE register, the assignment to it is free on almost
1640 any target, because "a" usually ends up in a real register. Hence
1641 the only cost of this expression comes from the PLUS_EXPR, and we
1642 can ignore the MODIFY_EXPR.
1643 If "a" is not a GIMPLE register, the assignment to "a" will most
1644 likely be a real store, so the cost of the MODIFY_EXPR is the cost
1645 of moving something into "a", which we compute using the function
1646 estimate_move_cost.
1648 The third case deals with TARGET_EXPRs, for which the semantics are
1649 that a temporary is assigned, unless the TARGET_EXPR itself is being
1650 assigned to something else. In the latter case we do not need the
1651 temporary. E.g. in <modify_expr <var_decl "a"> <target_expr>>, the
1652 MODIFY_EXPR is free. */
1653 case INIT_EXPR:
1654 case MODIFY_EXPR:
1655 /* Is the right and side a TARGET_EXPR? */
1656 if (TREE_CODE (TREE_OPERAND (x, 1)) == TARGET_EXPR)
1657 break;
1658 /* ... fall through ... */
1660 case TARGET_EXPR:
1661 x = TREE_OPERAND (x, 0);
1662 /* Is this an assignments to a register? */
1663 if (is_gimple_reg (x))
1664 break;
1665 /* Otherwise it's a store, so fall through to compute the move cost. */
1667 case CONSTRUCTOR:
1668 *count += estimate_move_cost (TREE_TYPE (x));
1669 break;
1671 /* Assign cost of 1 to usual operations.
1672 ??? We may consider mapping RTL costs to this. */
1673 case COND_EXPR:
1674 case VEC_COND_EXPR:
1676 case PLUS_EXPR:
1677 case MINUS_EXPR:
1678 case MULT_EXPR:
1680 case FIX_TRUNC_EXPR:
1681 case FIX_CEIL_EXPR:
1682 case FIX_FLOOR_EXPR:
1683 case FIX_ROUND_EXPR:
1685 case NEGATE_EXPR:
1686 case FLOAT_EXPR:
1687 case MIN_EXPR:
1688 case MAX_EXPR:
1689 case ABS_EXPR:
1691 case LSHIFT_EXPR:
1692 case RSHIFT_EXPR:
1693 case LROTATE_EXPR:
1694 case RROTATE_EXPR:
1695 case VEC_LSHIFT_EXPR:
1696 case VEC_RSHIFT_EXPR:
1698 case BIT_IOR_EXPR:
1699 case BIT_XOR_EXPR:
1700 case BIT_AND_EXPR:
1701 case BIT_NOT_EXPR:
1703 case TRUTH_ANDIF_EXPR:
1704 case TRUTH_ORIF_EXPR:
1705 case TRUTH_AND_EXPR:
1706 case TRUTH_OR_EXPR:
1707 case TRUTH_XOR_EXPR:
1708 case TRUTH_NOT_EXPR:
1710 case LT_EXPR:
1711 case LE_EXPR:
1712 case GT_EXPR:
1713 case GE_EXPR:
1714 case EQ_EXPR:
1715 case NE_EXPR:
1716 case ORDERED_EXPR:
1717 case UNORDERED_EXPR:
1719 case UNLT_EXPR:
1720 case UNLE_EXPR:
1721 case UNGT_EXPR:
1722 case UNGE_EXPR:
1723 case UNEQ_EXPR:
1724 case LTGT_EXPR:
1726 case CONVERT_EXPR:
1728 case CONJ_EXPR:
1730 case PREDECREMENT_EXPR:
1731 case PREINCREMENT_EXPR:
1732 case POSTDECREMENT_EXPR:
1733 case POSTINCREMENT_EXPR:
1735 case SWITCH_EXPR:
1737 case ASM_EXPR:
1739 case REALIGN_LOAD_EXPR:
1741 case REDUC_MAX_EXPR:
1742 case REDUC_MIN_EXPR:
1743 case REDUC_PLUS_EXPR:
1745 case RESX_EXPR:
1746 *count += 1;
1747 break;
1749 /* Few special cases of expensive operations. This is useful
1750 to avoid inlining on functions having too many of these. */
1751 case TRUNC_DIV_EXPR:
1752 case CEIL_DIV_EXPR:
1753 case FLOOR_DIV_EXPR:
1754 case ROUND_DIV_EXPR:
1755 case EXACT_DIV_EXPR:
1756 case TRUNC_MOD_EXPR:
1757 case CEIL_MOD_EXPR:
1758 case FLOOR_MOD_EXPR:
1759 case ROUND_MOD_EXPR:
1760 case RDIV_EXPR:
1761 *count += 10;
1762 break;
1763 case CALL_EXPR:
1765 tree decl = get_callee_fndecl (x);
1766 tree arg;
1768 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
1769 switch (DECL_FUNCTION_CODE (decl))
1771 case BUILT_IN_CONSTANT_P:
1772 *walk_subtrees = 0;
1773 return NULL_TREE;
1774 case BUILT_IN_EXPECT:
1775 return NULL_TREE;
1776 default:
1777 break;
1780 /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1781 that does use function declaration to figure out the arguments. */
1782 if (!decl)
1784 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
1785 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
1787 else
1789 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
1790 *count += estimate_move_cost (TREE_TYPE (arg));
1793 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
1794 break;
1796 default:
1797 gcc_unreachable ();
1799 return NULL;
1802 /* Estimate number of instructions that will be created by expanding EXPR. */
1805 estimate_num_insns (tree expr)
1807 int num = 0;
1808 struct pointer_set_t *visited_nodes;
1809 basic_block bb;
1810 block_stmt_iterator bsi;
1811 struct function *my_function;
1813 /* If we're given an entire function, walk the CFG. */
1814 if (TREE_CODE (expr) == FUNCTION_DECL)
1816 my_function = DECL_STRUCT_FUNCTION (expr);
1817 gcc_assert (my_function && my_function->cfg);
1818 visited_nodes = pointer_set_create ();
1819 FOR_EACH_BB_FN (bb, my_function)
1821 for (bsi = bsi_start (bb);
1822 !bsi_end_p (bsi);
1823 bsi_next (&bsi))
1825 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
1826 &num, visited_nodes);
1829 pointer_set_destroy (visited_nodes);
1831 else
1832 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
1834 return num;
1837 typedef struct function *function_p;
1839 DEF_VEC_P(function_p);
1840 DEF_VEC_ALLOC_P(function_p,heap);
1842 /* Initialized with NOGC, making this poisonous to the garbage collector. */
1843 static VEC(function_p,heap) *cfun_stack;
1845 void
1846 push_cfun (struct function *new_cfun)
1848 VEC_safe_push (function_p, heap, cfun_stack, cfun);
1849 cfun = new_cfun;
1852 void
1853 pop_cfun (void)
1855 cfun = VEC_pop (function_p, cfun_stack);
1858 /* Install new lexical TREE_BLOCK underneath 'current_block'. */
1859 static void
1860 add_lexical_block (tree current_block, tree new_block)
1862 tree *blk_p;
1864 /* Walk to the last sub-block. */
1865 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
1866 *blk_p;
1867 blk_p = &TREE_CHAIN (*blk_p))
1869 *blk_p = new_block;
1870 BLOCK_SUPERCONTEXT (new_block) = current_block;
1871 BLOCK_SUBBLOCKS (new_block) = NULL_TREE;
1874 /* If *TP is a CALL_EXPR, replace it with its inline expansion. */
1876 static bool
1877 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
1879 inline_data *id;
1880 tree t;
1881 tree use_retvar;
1882 tree fn;
1883 splay_tree st;
1884 tree args;
1885 tree return_slot_addr;
1886 tree modify_dest;
1887 location_t saved_location;
1888 struct cgraph_edge *cg_edge;
1889 const char *reason;
1890 basic_block return_block;
1891 edge e;
1892 block_stmt_iterator bsi, stmt_bsi;
1893 bool successfully_inlined = FALSE;
1894 tree t_step;
1895 tree var;
1896 struct cgraph_node *old_node;
1897 tree decl;
1899 /* See what we've got. */
1900 id = (inline_data *) data;
1901 t = *tp;
1903 /* Set input_location here so we get the right instantiation context
1904 if we call instantiate_decl from inlinable_function_p. */
1905 saved_location = input_location;
1906 if (EXPR_HAS_LOCATION (t))
1907 input_location = EXPR_LOCATION (t);
1909 /* From here on, we're only interested in CALL_EXPRs. */
1910 if (TREE_CODE (t) != CALL_EXPR)
1911 goto egress;
1913 /* First, see if we can figure out what function is being called.
1914 If we cannot, then there is no hope of inlining the function. */
1915 fn = get_callee_fndecl (t);
1916 if (!fn)
1917 goto egress;
1919 /* Turn forward declarations into real ones. */
1920 fn = cgraph_node (fn)->decl;
1922 /* If fn is a declaration of a function in a nested scope that was
1923 globally declared inline, we don't set its DECL_INITIAL.
1924 However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
1925 C++ front-end uses it for cdtors to refer to their internal
1926 declarations, that are not real functions. Fortunately those
1927 don't have trees to be saved, so we can tell by checking their
1928 DECL_SAVED_TREE. */
1929 if (! DECL_INITIAL (fn)
1930 && DECL_ABSTRACT_ORIGIN (fn)
1931 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
1932 fn = DECL_ABSTRACT_ORIGIN (fn);
1934 /* Objective C and fortran still calls tree_rest_of_compilation directly.
1935 Kill this check once this is fixed. */
1936 if (!id->current_node->analyzed)
1937 goto egress;
1939 cg_edge = cgraph_edge (id->current_node, stmt);
1941 /* Constant propagation on argument done during previous inlining
1942 may create new direct call. Produce an edge for it. */
1943 if (!cg_edge)
1945 struct cgraph_node *dest = cgraph_node (fn);
1947 /* We have missing edge in the callgraph. This can happen in one case
1948 where previous inlining turned indirect call into direct call by
1949 constant propagating arguments. In all other cases we hit a bug
1950 (incorrect node sharing is most common reason for missing edges. */
1951 gcc_assert (dest->needed || !flag_unit_at_a_time);
1952 cgraph_create_edge (id->node, dest, stmt,
1953 bb->count, bb->loop_depth)->inline_failed
1954 = N_("originally indirect function call not considered for inlining");
1955 goto egress;
1958 /* Don't try to inline functions that are not well-suited to
1959 inlining. */
1960 if (!cgraph_inline_p (cg_edge, &reason))
1962 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1964 sorry ("%Jinlining failed in call to %qF: %s", fn, fn, reason);
1965 sorry ("called from here");
1967 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
1968 && !DECL_IN_SYSTEM_HEADER (fn)
1969 && strlen (reason)
1970 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn)))
1972 warning (0, "%Jinlining failed in call to %qF: %s", fn, fn, reason);
1973 warning (0, "called from here");
1975 goto egress;
1978 #ifdef ENABLE_CHECKING
1979 if (cg_edge->callee->decl != id->node->decl)
1980 verify_cgraph_node (cg_edge->callee);
1981 #endif
1983 /* We will be inlining this callee. */
1985 id->eh_region = lookup_stmt_eh_region (stmt);
1987 /* Split the block holding the CALL_EXPR. */
1989 e = split_block (bb, stmt);
1990 bb = e->src;
1991 return_block = e->dest;
1992 remove_edge (e);
1994 /* split_block splits before the statement, work around this by moving
1995 the call into the first half_bb. Not pretty, but seems easier than
1996 doing the CFG manipulation by hand when the CALL_EXPR is in the last
1997 statement in BB. */
1998 stmt_bsi = bsi_last (bb);
1999 bsi = bsi_start (return_block);
2000 if (!bsi_end_p (bsi))
2001 bsi_move_before (&stmt_bsi, &bsi);
2002 else
2004 tree stmt = bsi_stmt (stmt_bsi);
2005 bsi_remove (&stmt_bsi);
2006 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2008 stmt_bsi = bsi_start (return_block);
2010 /* Build a block containing code to initialize the arguments, the
2011 actual inline expansion of the body, and a label for the return
2012 statements within the function to jump to. The type of the
2013 statement expression is the return type of the function call. */
2014 id->block = make_node (BLOCK);
2015 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2016 add_lexical_block (TREE_BLOCK (stmt), id->block);
2019 /* Local declarations will be replaced by their equivalents in this
2020 map. */
2021 st = id->decl_map;
2022 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2023 NULL, NULL);
2025 /* Initialize the parameters. */
2026 args = TREE_OPERAND (t, 1);
2027 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (t))
2029 return_slot_addr = TREE_VALUE (args);
2030 args = TREE_CHAIN (args);
2032 else
2033 return_slot_addr = NULL_TREE;
2035 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2037 /* Record the function we are about to inline. */
2038 id->callee = fn;
2040 /* Return statements in the function body will be replaced by jumps
2041 to the RET_LABEL. */
2043 gcc_assert (DECL_INITIAL (fn));
2044 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2046 /* Find the lhs to which the result of this call is assigned. */
2047 modify_dest = stmt;
2048 if (TREE_CODE (modify_dest) == MODIFY_EXPR)
2050 modify_dest = TREE_OPERAND (modify_dest, 0);
2052 /* The function which we are inlining might not return a value,
2053 in which case we should issue a warning that the function
2054 does not return a value. In that case the optimizers will
2055 see that the variable to which the value is assigned was not
2056 initialized. We do not want to issue a warning about that
2057 uninitialized variable. */
2058 if (DECL_P (modify_dest))
2059 TREE_NO_WARNING (modify_dest) = 1;
2061 else
2062 modify_dest = NULL;
2064 /* Declare the return variable for the function. */
2065 decl = declare_return_variable (id, return_slot_addr,
2066 modify_dest, &use_retvar);
2067 /* Do this only if declare_return_variable created a new one. */
2068 if (decl && !return_slot_addr && decl != modify_dest)
2069 declare_inline_vars (id->block, decl);
2071 /* After we've initialized the parameters, we insert the body of the
2072 function itself. */
2073 old_node = id->current_node;
2075 /* Anoint the callee-to-be-duplicated as the "current_node." When
2076 CALL_EXPRs within callee are duplicated, the edges from callee to
2077 callee's callees (caller's grandchildren) will be cloned. */
2078 id->current_node = cg_edge->callee;
2080 /* This is it. Duplicate the callee body. Assume callee is
2081 pre-gimplified. Note that we must not alter the caller
2082 function in any way before this point, as this CALL_EXPR may be
2083 a self-referential call; if we're calling ourselves, we need to
2084 duplicate our body before altering anything. */
2085 copy_body (id, bb->count, bb->frequency, bb, return_block);
2086 id->current_node = old_node;
2088 /* Clean up. */
2089 splay_tree_delete (id->decl_map);
2090 id->decl_map = st;
2092 /* If the inlined function returns a result that we care about,
2093 clobber the CALL_EXPR with a reference to the return variable. */
2094 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2096 *tp = use_retvar;
2097 maybe_clean_or_replace_eh_stmt (stmt, stmt);
2099 else
2100 /* We're modifying a TSI owned by gimple_expand_calls_inline();
2101 tsi_delink() will leave the iterator in a sane state. */
2102 bsi_remove (&stmt_bsi);
2104 bsi_next (&bsi);
2105 if (bsi_end_p (bsi))
2106 tree_purge_dead_eh_edges (return_block);
2108 /* If the value of the new expression is ignored, that's OK. We
2109 don't warn about this for CALL_EXPRs, so we shouldn't warn about
2110 the equivalent inlined version either. */
2111 TREE_USED (*tp) = 1;
2113 /* Output the inlining info for this abstract function, since it has been
2114 inlined. If we don't do this now, we can lose the information about the
2115 variables in the function when the blocks get blown away as soon as we
2116 remove the cgraph node. */
2117 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2119 /* Update callgraph if needed. */
2120 cgraph_remove_node (cg_edge->callee);
2122 /* Declare the 'auto' variables added with this inlined body. */
2123 record_vars (BLOCK_VARS (id->block));
2124 id->block = NULL_TREE;
2126 /* Add local static vars in this inlined callee to caller. */
2127 for (t_step = id->callee_cfun->unexpanded_var_list;
2128 t_step;
2129 t_step = TREE_CHAIN (t_step))
2131 var = TREE_VALUE (t_step);
2132 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2133 record_vars (var);
2135 successfully_inlined = TRUE;
2137 egress:
2138 input_location = saved_location;
2139 return successfully_inlined;
2142 /* Expand call statements reachable from STMT_P.
2143 We can only have CALL_EXPRs as the "toplevel" tree code or nested
2144 in a MODIFY_EXPR. See tree-gimple.c:get_call_expr_in(). We can
2145 unfortunately not use that function here because we need a pointer
2146 to the CALL_EXPR, not the tree itself. */
2148 static bool
2149 gimple_expand_calls_inline (basic_block bb, inline_data *id)
2151 block_stmt_iterator bsi;
2153 /* Register specific tree functions. */
2154 tree_register_cfg_hooks ();
2155 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2157 tree *expr_p = bsi_stmt_ptr (bsi);
2158 tree stmt = *expr_p;
2160 if (TREE_CODE (*expr_p) == MODIFY_EXPR)
2161 expr_p = &TREE_OPERAND (*expr_p, 1);
2162 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2163 expr_p = &TREE_OPERAND (*expr_p, 0);
2164 if (TREE_CODE (*expr_p) == CALL_EXPR)
2165 if (expand_call_inline (bb, stmt, expr_p, id))
2166 return true;
2168 return false;
2171 /* Expand calls to inline functions in the body of FN. */
2173 void
2174 optimize_inline_calls (tree fn)
2176 inline_data id;
2177 tree prev_fn;
2178 basic_block bb;
2179 /* There is no point in performing inlining if errors have already
2180 occurred -- and we might crash if we try to inline invalid
2181 code. */
2182 if (errorcount || sorrycount)
2183 return;
2185 /* Clear out ID. */
2186 memset (&id, 0, sizeof (id));
2188 id.current_node = id.node = cgraph_node (fn);
2189 id.caller = fn;
2190 /* Or any functions that aren't finished yet. */
2191 prev_fn = NULL_TREE;
2192 if (current_function_decl)
2194 id.caller = current_function_decl;
2195 prev_fn = current_function_decl;
2197 push_gimplify_context ();
2199 /* Reach the trees by walking over the CFG, and note the
2200 enclosing basic-blocks in the call edges. */
2201 /* We walk the blocks going forward, because inlined function bodies
2202 will split id->current_basic_block, and the new blocks will
2203 follow it; we'll trudge through them, processing their CALL_EXPRs
2204 along the way. */
2205 FOR_EACH_BB (bb)
2206 gimple_expand_calls_inline (bb, &id);
2209 pop_gimplify_context (NULL);
2210 /* Renumber the (code) basic_blocks consecutively. */
2211 compact_blocks ();
2212 /* Renumber the lexical scoping (non-code) blocks consecutively. */
2213 number_blocks (fn);
2215 #ifdef ENABLE_CHECKING
2217 struct cgraph_edge *e;
2219 verify_cgraph_node (id.node);
2221 /* Double check that we inlined everything we are supposed to inline. */
2222 for (e = id.node->callees; e; e = e->next_callee)
2223 gcc_assert (e->inline_failed);
2225 #endif
2226 /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2227 as inlining loops might increase the maximum. */
2228 if (ENTRY_BLOCK_PTR->count)
2229 counts_to_freqs ();
2230 fold_cond_expr_cond ();
2233 /* FN is a function that has a complete body, and CLONE is a function whose
2234 body is to be set to a copy of FN, mapping argument declarations according
2235 to the ARG_MAP splay_tree. */
2237 void
2238 clone_body (tree clone, tree fn, void *arg_map)
2240 inline_data id;
2242 /* Clone the body, as if we were making an inline call. But, remap the
2243 parameters in the callee to the parameters of caller. */
2244 memset (&id, 0, sizeof (id));
2245 id.caller = clone;
2246 id.callee = fn;
2247 id.callee_cfun = DECL_STRUCT_FUNCTION (fn);
2248 id.decl_map = (splay_tree)arg_map;
2250 /* Cloning is treated slightly differently from inlining. Set
2251 CLONING_P so that it's clear which operation we're performing. */
2252 id.cloning_p = true;
2254 /* We're not inside any EH region. */
2255 id.eh_region = -1;
2257 /* Actually copy the body. */
2258 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2261 /* Save duplicate body in FN. MAP is used to pass around splay tree
2262 used to update arguments in restore_body. */
2264 /* Make and return duplicate of body in FN. Put copies of DECL_ARGUMENTS
2265 in *arg_copy and of the static chain, if any, in *sc_copy. */
2267 void
2268 save_body (tree fn, tree *arg_copy, tree *sc_copy)
2270 inline_data id;
2271 tree newdecl, *parg;
2272 basic_block fn_entry_block;
2274 memset (&id, 0, sizeof (id));
2275 id.callee = fn;
2276 id.callee_cfun = DECL_STRUCT_FUNCTION (fn);
2277 id.caller = fn;
2278 id.node = cgraph_node (fn);
2279 id.saving_p = true;
2280 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2281 *arg_copy = DECL_ARGUMENTS (fn);
2283 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2285 tree new = copy_node (*parg);
2287 lang_hooks.dup_lang_specific_decl (new);
2288 DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*parg);
2289 insert_decl_map (&id, *parg, new);
2290 TREE_CHAIN (new) = TREE_CHAIN (*parg);
2291 *parg = new;
2294 *sc_copy = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2295 if (*sc_copy)
2297 tree new = copy_node (*sc_copy);
2299 lang_hooks.dup_lang_specific_decl (new);
2300 DECL_ABSTRACT_ORIGIN (new) = DECL_ORIGIN (*sc_copy);
2301 insert_decl_map (&id, *sc_copy, new);
2302 TREE_CHAIN (new) = TREE_CHAIN (*sc_copy);
2303 *sc_copy = new;
2306 /* We're not inside any EH region. */
2307 id.eh_region = -1;
2309 insert_decl_map (&id, DECL_RESULT (fn), DECL_RESULT (fn));
2311 /* Actually copy the body, including a new (struct function *) and CFG.
2312 EH info is also duplicated so its labels point into the copied
2313 CFG, not the original. */
2314 fn_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fn));
2315 newdecl = copy_body (&id, fn_entry_block->count, fn_entry_block->frequency, NULL, NULL);
2316 DECL_STRUCT_FUNCTION (fn)->saved_cfg = DECL_STRUCT_FUNCTION (newdecl)->cfg;
2317 DECL_STRUCT_FUNCTION (fn)->saved_eh = DECL_STRUCT_FUNCTION (newdecl)->eh;
2319 /* Clean up. */
2320 splay_tree_delete (id.decl_map);
2323 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
2325 tree
2326 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2328 enum tree_code code = TREE_CODE (*tp);
2330 /* We make copies of most nodes. */
2331 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
2332 || code == TREE_LIST
2333 || code == TREE_VEC
2334 || code == TYPE_DECL)
2336 /* Because the chain gets clobbered when we make a copy, we save it
2337 here. */
2338 tree chain = TREE_CHAIN (*tp);
2339 tree new;
2341 /* Copy the node. */
2342 new = copy_node (*tp);
2344 /* Propagate mudflap marked-ness. */
2345 if (flag_mudflap && mf_marked_p (*tp))
2346 mf_mark (new);
2348 *tp = new;
2350 /* Now, restore the chain, if appropriate. That will cause
2351 walk_tree to walk into the chain as well. */
2352 if (code == PARM_DECL || code == TREE_LIST)
2353 TREE_CHAIN (*tp) = chain;
2355 /* For now, we don't update BLOCKs when we make copies. So, we
2356 have to nullify all BIND_EXPRs. */
2357 if (TREE_CODE (*tp) == BIND_EXPR)
2358 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2361 else if (TREE_CODE_CLASS (code) == tcc_type)
2362 *walk_subtrees = 0;
2363 else if (TREE_CODE_CLASS (code) == tcc_declaration)
2364 *walk_subtrees = 0;
2365 else if (TREE_CODE_CLASS (code) == tcc_constant)
2366 *walk_subtrees = 0;
2367 else
2368 gcc_assert (code != STATEMENT_LIST);
2369 return NULL_TREE;
2372 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2373 information indicating to what new SAVE_EXPR this one should be mapped,
2374 use that one. Otherwise, create a new node and enter it in ST. FN is
2375 the function into which the copy will be placed. */
2377 static void
2378 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2380 splay_tree st = (splay_tree) st_;
2381 splay_tree_node n;
2382 tree t;
2384 /* See if we already encountered this SAVE_EXPR. */
2385 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2387 /* If we didn't already remap this SAVE_EXPR, do so now. */
2388 if (!n)
2390 t = copy_node (*tp);
2392 /* Remember this SAVE_EXPR. */
2393 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2394 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2395 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2397 else
2399 /* We've already walked into this SAVE_EXPR; don't do it again. */
2400 *walk_subtrees = 0;
2401 t = (tree) n->value;
2404 /* Replace this SAVE_EXPR with the copy. */
2405 *tp = t;
2408 /* Called via walk_tree. If *TP points to a DECL_STMT for a local label,
2409 copies the declaration and enters it in the splay_tree in DATA (which is
2410 really an `inline_data *'). */
2412 static tree
2413 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2414 void *data)
2416 inline_data *id = (inline_data *) data;
2418 /* Don't walk into types. */
2419 if (TYPE_P (*tp))
2420 *walk_subtrees = 0;
2422 else if (TREE_CODE (*tp) == LABEL_EXPR)
2424 tree decl = TREE_OPERAND (*tp, 0);
2426 /* Copy the decl and remember the copy. */
2427 insert_decl_map (id, decl,
2428 copy_decl_for_inlining (decl, DECL_CONTEXT (decl),
2429 DECL_CONTEXT (decl)));
2432 return NULL_TREE;
2435 /* Perform any modifications to EXPR required when it is unsaved. Does
2436 not recurse into EXPR's subtrees. */
2438 static void
2439 unsave_expr_1 (tree expr)
2441 switch (TREE_CODE (expr))
2443 case TARGET_EXPR:
2444 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2445 It's OK for this to happen if it was part of a subtree that
2446 isn't immediately expanded, such as operand 2 of another
2447 TARGET_EXPR. */
2448 if (TREE_OPERAND (expr, 1))
2449 break;
2451 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2452 TREE_OPERAND (expr, 3) = NULL_TREE;
2453 break;
2455 default:
2456 break;
2460 /* Called via walk_tree when an expression is unsaved. Using the
2461 splay_tree pointed to by ST (which is really a `splay_tree'),
2462 remaps all local declarations to appropriate replacements. */
2464 static tree
2465 unsave_r (tree *tp, int *walk_subtrees, void *data)
2467 inline_data *id = (inline_data *) data;
2468 splay_tree st = id->decl_map;
2469 splay_tree_node n;
2471 /* Only a local declaration (variable or label). */
2472 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2473 || TREE_CODE (*tp) == LABEL_DECL)
2475 /* Lookup the declaration. */
2476 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2478 /* If it's there, remap it. */
2479 if (n)
2480 *tp = (tree) n->value;
2483 else if (TREE_CODE (*tp) == STATEMENT_LIST)
2484 copy_statement_list (tp);
2485 else if (TREE_CODE (*tp) == BIND_EXPR)
2486 copy_bind_expr (tp, walk_subtrees, id);
2487 else if (TREE_CODE (*tp) == SAVE_EXPR)
2488 remap_save_expr (tp, st, walk_subtrees);
2489 else
2491 copy_tree_r (tp, walk_subtrees, NULL);
2493 /* Do whatever unsaving is required. */
2494 unsave_expr_1 (*tp);
2497 /* Keep iterating. */
2498 return NULL_TREE;
2501 /* Copies everything in EXPR and replaces variables, labels
2502 and SAVE_EXPRs local to EXPR. */
2504 tree
2505 unsave_expr_now (tree expr)
2507 inline_data id;
2509 /* There's nothing to do for NULL_TREE. */
2510 if (expr == 0)
2511 return expr;
2513 /* Set up ID. */
2514 memset (&id, 0, sizeof (id));
2515 id.callee = current_function_decl;
2516 id.caller = current_function_decl;
2517 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2519 /* Walk the tree once to find local labels. */
2520 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2522 /* Walk the tree again, copying, remapping, and unsaving. */
2523 walk_tree (&expr, unsave_r, &id, NULL);
2525 /* Clean up. */
2526 splay_tree_delete (id.decl_map);
2528 return expr;
2531 /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
2533 static tree
2534 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2536 if (*tp == data)
2537 return (tree) data;
2538 else
2539 return NULL;
2542 bool
2543 debug_find_tree (tree top, tree search)
2545 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2549 /* Declare the variables created by the inliner. Add all the variables in
2550 VARS to BIND_EXPR. */
2552 static void
2553 declare_inline_vars (tree block, tree vars)
2555 tree t;
2556 for (t = vars; t; t = TREE_CHAIN (t))
2557 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2559 if (block)
2560 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2563 /* Returns true if we're inlining. */
2564 static inline bool
2565 inlining_p (inline_data *id)
2567 return (!id->saving_p && !id->cloning_p);